Skip to content

Commit b8123cc

Browse files
committed
[canvaskit] Clean up readPixels API on Canvas and Image
This makes both APIs have the same arguments with the two source coordinates first and all the destination params (image info, optional buffer, optional rowBytes) after. Bug: skia:10717 Change-Id: I483e4f33f24e226793db6113d5ba5b1955cd892e Reviewed-on: https://skia-review.googlesource.com/c/skia/+/332622 Reviewed-by: Mike Reed <reed@google.com>
1 parent 15b95d6 commit b8123cc

File tree

7 files changed

+173
-158
lines changed

7 files changed

+173
-158
lines changed

modules/canvaskit/CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
### Breaking
1818
- `CanvasKit.MakePathFromSVGString` was renamed to `CanvasKit.Path.MakeFromSVGString`
1919
- `CanvasKit.MakePathFromOp` was renamed to `CanvasKit.Path.MakeFromOp`
20+
- The API for `Canvas.readPixels` and `Image.readPixels` has been reworked to more accurately
21+
reflect the C++ backend and each other. bytesPerRow is now a required parameter. They take an
22+
ImageInfo object to specify the output format. Additionally they take an optional malloc'd
23+
object as the last parameter. If provided, the data will be copied into there instead of
24+
allocating a new buffer.
2025

2126
### Changed
2227
- We now compile CanvasKit with emsdk 2.0.6 when testing and deploying to npm.
2328
- We no longer compile with rtti on, saving about 1% in code size.
2429
- `CanvasKit.Shader.Blend`, `...Color`, and `...Lerp` have been renamed to
2530
`CanvasKit.Shader.MakeBlend`, `...MakeColor` and `...MakeLerp` to align with naming conventions.
2631
The old names will be removed in an upcoming release.
27-
- `readPixels` now takes a malloc'd object as the last parameter. If provided, the data will be
28-
copied into there instead of allocating a new buffer.
2932

3033
### Removed
3134
- `CanvasKit.MakePathFromCmds`; Was deprecated in favor of `CanvasKit.Path.MakeFromCmds`.

modules/canvaskit/canvaskit/types/canvaskit-wasm-tests.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,21 @@ function canvasTests(CK: CanvasKit, canvas?: Canvas, paint?: Paint, path?: Path,
138138
const matrThree = canvas.getTotalMatrix(); // $ExpectType number[]
139139
const surface = canvas.makeSurface(imageInfo); // $ExpectType Surface | null
140140
canvas.markCTM('more ctm');
141-
const pixels = canvas.readPixels(0, 1, 2, 3); // $ExpectType Uint8Array
142-
const pixelsTwo = canvas.readPixels(4, 5, 6, 7, CK.AlphaType.Opaque, CK.ColorType.RGBA_1010102,
143-
CK.ColorSpace.DISPLAY_P3, 16);
144-
const m = CK.Malloc(Uint8Array, 20);
145-
canvas.readPixels(4, 5, 6, 7, CK.AlphaType.Opaque, CK.ColorType.RGBA_1010102,
146-
CK.ColorSpace.DISPLAY_P3, 16, m);
141+
const pixels = canvas.readPixels(85, 1000, {// $Uint8Array | Float32Array | null
142+
width: 79,
143+
height: 205,
144+
colorType: CK.ColorType.RGBA_8888,
145+
alphaType: CK.AlphaType.Unpremul,
146+
colorSpace: CK.ColorSpace.SRGB,
147+
});
148+
const m = CK.Malloc(Uint8Array, 10);
149+
img.readPixels(85, 1000, {
150+
width: 79,
151+
height: 205,
152+
colorType: CK.ColorType.RGBA_8888,
153+
alphaType: CK.AlphaType.Unpremul,
154+
colorSpace: CK.ColorSpace.SRGB,
155+
}, m, 4 * 85);
147156
canvas.restore();
148157
canvas.restoreToCount(2);
149158
canvas.rotate(1, 2, 3);
@@ -235,21 +244,21 @@ function imageTests(CK: CanvasKit, imgElement?: HTMLImageElement) {
235244
const h = img.height();
236245
const w = img.width();
237246
const shader = img.makeShader(CK.TileMode.Decal, CK.TileMode.Repeat); // $ExpectType Shader
238-
const pixels = img.readPixels({
247+
const pixels = img.readPixels(85, 1000, {// $Uint8Array | Float32Array | null
239248
width: 79,
240249
height: 205,
241250
colorType: CK.ColorType.RGBA_8888,
242251
alphaType: CK.AlphaType.Unpremul,
243252
colorSpace: CK.ColorSpace.SRGB,
244-
}, 85, 1000);
253+
});
245254
const m = CK.Malloc(Uint8Array, 10);
246-
img.readPixels({
255+
img.readPixels(85, 1000, {
247256
width: 79,
248257
height: 205,
249258
colorType: CK.ColorType.RGBA_8888,
250259
alphaType: CK.AlphaType.Unpremul,
251260
colorSpace: CK.ColorSpace.SRGB,
252-
}, 85, 1000, m);
261+
}, m, 4 * 85);
253262
img.delete();
254263
}
255264

modules/canvaskit/canvaskit/types/index.d.ts

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,22 +1232,27 @@ export interface Canvas extends EmbindObject<Canvas> {
12321232
markCTM(marker: string): void;
12331233

12341234
/**
1235-
* Copies the given rectangle of pixels into a new Uint8Array and returns it. If alphaType,
1236-
* colorType, and colorSpace are provided, those will describe the output format.
1237-
* @param x
1238-
* @param y
1239-
* @param w
1240-
* @param h
1241-
* @param alphaType - defaults to Unpremul
1242-
* @param colorType - defaults to RGBA_8888
1243-
* @param colorSpace - defaults to SRGB
1244-
* @param dest - If provided, the pixels will be copied into the allocated buffer allowing access to the
1245-
* pixels without allocating a new TypedArray.
1246-
* @param dstRowBytes
1235+
* Returns a TypedArray containing the pixels reading starting at (srcX, srcY) and does not
1236+
* exceed the size indicated by imageInfo. See SkCanvas.h for more on the caveats.
1237+
*
1238+
* If dest is not provided, we allocate memory equal to the provided height * the provided
1239+
* bytesPerRow to fill the data with.
1240+
*
1241+
* This is generally a very expensive call for the GPU backend.
1242+
*
1243+
* @param srcX
1244+
* @param srcY
1245+
* @param imageInfo - describes the destination format of the pixels.
1246+
* @param dest - If provided, the pixels will be copied into the allocated buffer allowing
1247+
* access to the pixels without allocating a new TypedArray.
1248+
* @param bytesPerRow - number of bytes per row. Must be provided if dest is set. This
1249+
* depends on destination ColorType. For example, it must be at least 4 * width for
1250+
* the 8888 color type.
1251+
* @returns a TypedArray appropriate for the specified ColorType. Note that 16 bit floats are
1252+
* not supported in JS, so that colorType corresponds to raw bytes Uint8Array.
12471253
*/
1248-
readPixels(x: number, y: number, w: number, h: number, alphaType?: AlphaType,
1249-
colorType?: ColorType, colorSpace?: ColorSpace, dstRowBytes?: number,
1250-
dest?: MallocObj): Uint8Array;
1254+
readPixels(srcX: number, srcY: number, imageInfo: ImageInfo, dest?: MallocObj,
1255+
bytesPerRow?: number): Uint8Array | Float32Array | null;
12511256

12521257
/**
12531258
* Removes changes to the current matrix and clip since Canvas state was
@@ -1564,18 +1569,24 @@ export interface Image extends EmbindObject<Image> {
15641569

15651570
/**
15661571
* Returns a TypedArray containing the pixels reading starting at (srcX, srcY) and does not
1567-
* exceed the size indicated by imageInfo. See Image.h for more on the caveats.
1572+
* exceed the size indicated by imageInfo. See SkImage.h for more on the caveats.
1573+
*
1574+
* If dest is not provided, we allocate memory equal to the provided height * the provided
1575+
* bytesPerRow to fill the data with.
15681576
*
1569-
* @param imageInfo - describes the destination format of the pixels.
15701577
* @param srcX
15711578
* @param srcY
1572-
* @param dest - If provided, the pixels will be copied into the allocated buffer allowing access to the
1573-
* pixels without allocating a new TypedArray.
1574-
* @returns a Uint8Array if RGB_8888 was requested, Float32Array if RGBA_F32 was requested. null will be returned
1575-
* on any error.
1576-
*
1577-
*/
1578-
readPixels(imageInfo: ImageInfo, srcX: number, srcY: number, dest?: MallocObj): Uint8Array | Float32Array | null;
1579+
* @param imageInfo - describes the destination format of the pixels.
1580+
* @param dest - If provided, the pixels will be copied into the allocated buffer allowing
1581+
* access to the pixels without allocating a new TypedArray.
1582+
* @param bytesPerRow - number of bytes per row. Must be provided if dest is set. This
1583+
* depends on destination ColorType. For example, it must be at least 4 * width for
1584+
* the 8888 color type.
1585+
* @returns a TypedArray appropriate for the specified ColorType. Note that 16 bit floats are
1586+
* not supported in JS, so that colorType corresponds to raw bytes Uint8Array.
1587+
*/
1588+
readPixels(srcX: number, srcY: number, imageInfo: ImageInfo, dest?: MallocObj,
1589+
bytesPerRow?: number): Uint8Array | Float32Array | null;
15791590

15801591
/**
15811592
* Return the width in pixels of the image.

0 commit comments

Comments
 (0)