From 421ba88d1526d0ec299fa736455b0f7b09e13b10 Mon Sep 17 00:00:00 2001 From: Kevin Lubick Date: Thu, 15 Oct 2020 13:07:33 -0400 Subject: [PATCH] [canvaskit] Clean up Shader docs/types/names There should be no functional changes. I also address some forgotten changes to canvaskit-wasm-tests.ts from CLs last week. Bug: skia:10717 Change-Id: If02f60813af0aa42acd637639e40f4d6d0b38bd7 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/327157 Reviewed-by: Nathaniel Nifong --- modules/canvaskit/CHANGELOG.md | 6 +- .../canvaskit/types/canvaskit-wasm-tests.ts | 109 +++++++++- modules/canvaskit/canvaskit/types/index.d.ts | 89 +++++++- modules/canvaskit/canvaskit_bindings.cpp | 198 +++++++++--------- modules/canvaskit/externs.js | 16 +- modules/canvaskit/helper.js | 2 +- modules/canvaskit/interface.js | 38 ++-- modules/canvaskit/tests/core.spec.js | 12 +- 8 files changed, 327 insertions(+), 143 deletions(-) diff --git a/modules/canvaskit/CHANGELOG.md b/modules/canvaskit/CHANGELOG.md index a094dcc4b4..7dddc08019 100644 --- a/modules/canvaskit/CHANGELOG.md +++ b/modules/canvaskit/CHANGELOG.md @@ -13,6 +13,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - We now compile CanvasKit with emsdk 2.0.6 when testing and deploying to npm. - We no longer compile with rtti on, saving about 1% in code size. + - `CanvasKit.Shader.Blend`, `...Color`, and `...Lerp` have been renamed to + `CanvasKit.Shader.MakeBlend`, `...MakeColor` and `...MakeLerp` to allign with naming conventions. + The old names will be removed in an upcoming release. ### Removed - `CanvasKit.MakePathFromCmds`; Was deprecated in favor of `CanvasKit.Path.MakeFromCmds`. @@ -21,7 +24,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Type Changes (index.d.ts) - Return value for MakeFromCmds correctly reflects the possibility of null. - - `CanvasKit.GrContext` was renamed to `CanvasKit.GrDirectContext` + - `CanvasKit.GrContext` was renamed to `CanvasKit.GrDirectContext`. + - Add docs/types for Shader Gradients (e.g. `CanvasKit.Shader.MakeLinearGradient`). ## [0.19.0] - 2020-10-08 diff --git a/modules/canvaskit/canvaskit/types/canvaskit-wasm-tests.ts b/modules/canvaskit/canvaskit/types/canvaskit-wasm-tests.ts index ce54c84380..03f41ea120 100644 --- a/modules/canvaskit/canvaskit/types/canvaskit-wasm-tests.ts +++ b/modules/canvaskit/canvaskit/types/canvaskit-wasm-tests.ts @@ -358,7 +358,7 @@ function paintTests(CK: CanvasKit, colorFilter?: ColorFilter, imageFilter?: Imag function pathTests(CK: CanvasKit) { const path = new CK.Path(); // $ExpectType Path - const p2 = CK.Path.MakeFromCmds([ // $ExpectType Path + const p2 = CK.Path.MakeFromCmds([ // $ExpectType Path | null [CK.MOVE_VERB, 0, 10], [CK.LINE_VERB, 30, 40], [CK.QUAD_VERB, 20, 50, 45, 60], @@ -367,8 +367,8 @@ function pathTests(CK: CanvasKit) { const points = CK.Malloc(Float32Array, 10); const p3 = CK.Path.MakeFromVerbsPointsWeights(verbs, [1, 2, 3, 4]); // $ExpectType Path const p4 = CK.Path.MakeFromVerbsPointsWeights([CK.CONIC_VERB], points, [2.3]); - const p5 = CK.MakePathFromOp(p4, p2, CK.PathOp.ReverseDifference); // $ExpectType Path | null - const p6 = CK.MakePathFromSVGString('M 205,5 L 795,5 z'); // $ExpectType Path | null + const p5 = CK.Path.MakeFromOp(p4, p2!, CK.PathOp.ReverseDifference); // $ExpectType Path | null + const p6 = CK.Path.MakeFromSVGString('M 205,5 L 795,5 z'); // $ExpectType Path | null const someRect = CK.LTRBRect(10, 20, 30, 40); // Making sure arrays are accepted as rrects. @@ -409,7 +409,7 @@ function pathTests(CK: CanvasKit) { path.lineTo(10, -20); path.moveTo(-20, -30); path.offset(100, 100); - ok = path.op(p2, CK.PathOp.Difference); + ok = path.op(p2!, CK.PathOp.Difference); path.quadTo(10, 20, 30, 40); path.rArcTo(10, 10, 90, false, true, 2, 4); path.rConicTo(-1, 2, 4, 9, 3); @@ -641,10 +641,105 @@ function skottieTests(CK: CanvasKit, canvas?: Canvas) { } function shaderTests(CK: CanvasKit) { - const s1 = CK.Shader.Color([0.8, 0.2, 0.5, 0.9], // $ExpectType Shader + const s1 = CK.Shader.MakeColor([0.8, 0.2, 0.5, 0.9], // $ExpectType Shader CK.ColorSpace.SRGB); - const s2 = CK.Shader.Blend(CK.BlendMode.Src, s1, s1); // $ExpectType Shader - const s3 = CK.Shader.Lerp(0.3, s1, s2); // $ExpectType Shader + const s2 = CK.Shader.MakeBlend(CK.BlendMode.Src, s1, s1); // $ExpectType Shader + const s3 = CK.Shader.MakeLerp(0.3, s1, s2); // $ExpectType Shader + const s4 = CK.Shader.MakeLinearGradient(// $ExpectType Shader + [0, 0], [50, 100], + Float32Array.of( + 0, 1, 0, 0.8, + 1, 0, 0, 1, + 0, 0, 1, 0.5, + ), + [0, 0.65, 1.0], + CK.TileMode.Mirror + ); + const s5 = CK.Shader.MakeLinearGradient(// $ExpectType Shader + [0, 0], [50, 100], + Float32Array.of( + 0, 1, 0, 0.8, + 1, 0, 0, 1, + 0, 0, 1, 0.5, + ), + null, + CK.TileMode.Clamp, + CK.Matrix.rotated(Math.PI / 4, 0, 100), + 1, + CK.ColorSpace.SRGB, + ); + const s6 = CK.Shader.MakeRadialGradient(// $ExpectType Shader + [0, 0], 50, + Float32Array.of( + 0, 1, 0, 0.8, + 1, 0, 0, 1, + 0, 0, 1, 0.5, + ), + [0, 0.65, 1.0], + CK.TileMode.Decal, + ); + const s7 = CK.Shader.MakeRadialGradient(// $ExpectType Shader + [0, 0], 50, + Float32Array.of( + 0, 1, 0, 0.8, + 1, 0, 0, 1, + 0, 0, 1, 0.5, + ), + null, + CK.TileMode.Clamp, + CK.Matrix.skewed(3, -3), + 1, + CK.ColorSpace.SRGB, + ); + const s8 = CK.Shader.MakeTwoPointConicalGradient(// $ExpectType Shader + [0, 0], 20, + [50, 100], 60, + Float32Array.of( + 0, 1, 0, 0.8, + 1, 0, 0, 1, + 0, 0, 1, 0.5, + ), + [0, 0.65, 1.0], + CK.TileMode.Mirror + ); + const s9 = CK.Shader.MakeTwoPointConicalGradient(// $ExpectType Shader + [0, 0], 20, + [50, 100], 60, + Float32Array.of( + 0, 1, 0, 0.8, + 1, 0, 0, 1, + 0, 0, 1, 0.5, + ), + [0, 0.65, 1.0], + CK.TileMode.Mirror, + CK.Matrix.rotated(Math.PI / 4, 0, 100), + 1, + CK.ColorSpace.SRGB, + ); + const s10 = CK.Shader.MakeSweepGradient(// $ExpectType Shader + 0, 20, + Float32Array.of( + 0, 1, 0, 0.8, + 1, 0, 0, 1, + 0, 0, 1, 0.5, + ), + [0, 0.65, 1.0], + CK.TileMode.Mirror + ); + const s11 = CK.Shader.MakeSweepGradient(// $ExpectType Shader + 0, 20, + Float32Array.of( + 0, 1, 0, 0.8, + 1, 0, 0, 1, + 0, 0, 1, 0.5, + ), + null, + CK.TileMode.Mirror, + CK.Matrix.rotated(Math.PI / 4, 0, 100), + 1, + 15, 275, // start, end angle in degrees. + CK.ColorSpace.SRGB, + ); } function shapedTextTests(CK: CanvasKit, textFont?: Font) { diff --git a/modules/canvaskit/canvaskit/types/index.d.ts b/modules/canvaskit/canvaskit/types/index.d.ts index a5ccbe5bf6..0fa6c6cb08 100644 --- a/modules/canvaskit/canvaskit/types/index.d.ts +++ b/modules/canvaskit/canvaskit/types/index.d.ts @@ -2924,7 +2924,6 @@ export interface RuntimeEffectFactory { /** * For more information, see SkShaders.h. - * TODO(kjlubick) Rename these to Make* as per the convention */ export interface ShaderFactory { /** @@ -2933,14 +2932,14 @@ export interface ShaderFactory { * @param one * @param two */ - Blend(mode: BlendMode, one: Shader, two: Shader): Shader; + MakeBlend(mode: BlendMode, one: Shader, two: Shader): Shader; /** * Returns a shader with a given color and colorspace. * @param color * @param space */ - Color(color: InputColor, space: ColorSpace): Shader; + MakeColor(color: InputColor, space: ColorSpace): Shader; /** * Returns a shader is a linear interpolation combines the given shaders with a BlendMode. @@ -2948,7 +2947,83 @@ export interface ShaderFactory { * @param one * @param two */ - Lerp(t: number, one: Shader, two: Shader): Shader; + MakeLerp(t: number, one: Shader, two: Shader): Shader; + + /** + * Returns a shader that generates a linear gradient between the two specified points. + * See SkGradientShader.h for more. + * @param start + * @param end + * @param colors - colors to be distributed between start and end. + * @param pos - May be null. The relative positions of colors. If supplied must be same length + * as colors. + * @param mode + * @param localMatrix + * @param flags - By default gradients will interpolate their colors in unpremul space + * and then premultiply each of the results. By setting this to 1, the + * gradients will premultiply their colors first, and then interpolate + * between them. + * @param colorSpace + */ + MakeLinearGradient(start: Point, end: Point, colors: InputFlexibleColorArray, + pos: number[] | null, mode: TileMode, localMatrix?: InputMatrix, + flags?: number, colorSpace?: ColorSpace): Shader; + + /** + * Returns a shader that generates a radial gradient given the center and radius. + * See SkGradientShader.h for more. + * @param center + * @param radius + * @param colors - colors to be distributed between the center and edge. + * @param pos - May be null. The relative positions of colors. If supplied must be same length + * as colors. Range [0.0, 1.0] + * @param mode + * @param localMatrix + * @param flags - 0 to interpolate colors in unpremul, 1 to interpolate colors in premul. + * @param colorSpace + */ + MakeRadialGradient(center: Point, radius: number, colors: InputFlexibleColorArray, + pos: number[] | null, mode: TileMode, localMatrix?: InputMatrix, + flags?: number, colorSpace?: ColorSpace): Shader; + + /** + * Returns a shader that generates a sweep gradient given a center. + * See SkGradientShader.h for more. + * @param cx + * @param cy + * @param colors - colors to be distributed around the center, within the provided angles. + * @param pos - May be null. The relative positions of colors. If supplied must be same length + * as colors. Range [0.0, 1.0] + * @param mode + * @param localMatrix + * @param flags - 0 to interpolate colors in unpremul, 1 to interpolate colors in premul. + * @param startAngle - angle corresponding to 0.0. Defaults to 0 degrees. + * @param endAngle - angle corresponding to 1.0. Defaults to 360 degrees. + * @param colorSpace + */ + MakeSweepGradient(cx: number, cy: number, colors: InputFlexibleColorArray, + pos: number[] | null, mode: TileMode, localMatrix?: InputMatrix | null, + flags?: number, startAngle?: AngleInDegrees, endAngle?: AngleInDegrees, + colorSpace?: ColorSpace): Shader; + + /** + * Returns a shader that generates a conical gradient given two circles. + * See SkGradientShader.h for more. + * @param start + * @param startRadius + * @param end + * @param endRadius + * @param colors + * @param pos + * @param mode + * @param localMatrix + * @param flags + * @param colorSpace + */ + MakeTwoPointConicalGradient(start: Point, startRadius: number, end: Point, endRadius: number, + colors: InputFlexibleColorArray, pos: number[] | null, + mode: TileMode, localMatrix?: InputMatrix, flags?: number, + colorSpace?: ColorSpace): Shader; } /** @@ -3217,6 +3292,12 @@ export type InputFlattenedPointArray = MallocObj | FlattenedPointArray | number[ * Length 4 * n for n rectangles. */ export type InputFlattenedRectangleArray = MallocObj | FlattenedRectangleArray | number[]; +/** + * Some APIs accept a flattened array of colors in one of two ways - groups of 4 float values for + * r, g, b, a or just integers that have 8 bits for each these. CanvasKit will detect which one + * it is and act accordingly. + */ +export type InputFlexibleColorArray = Float32Array | Uint32Array; /** * CanvasKit APIs accept all of these matrix types. Under the hood, we generally use 4x4 matrices. */ diff --git a/modules/canvaskit/canvaskit_bindings.cpp b/modules/canvaskit/canvaskit_bindings.cpp index 12ba3c5d29..a51e930c34 100644 --- a/modules/canvaskit/canvaskit_bindings.cpp +++ b/modules/canvaskit/canvaskit_bindings.cpp @@ -783,32 +783,6 @@ EMSCRIPTEN_BINDINGS(Skia) { return SkImage::MakeRasterData(info, pixelData, rowBytes); }), allow_raw_pointers()); - // Here and in other gradient functions, cPtr is a pointer to an array of data - // representing colors. whether this is an array of SkColor or SkColor4f is indicated - // by the colorType argument. Only RGBA_8888 and RGBA_F32 are accepted. - function("_MakeLinearGradientShader", optional_override([](SkPoint start, SkPoint end, - uintptr_t cPtr, SkColorType colorType, - uintptr_t /* SkScalar* */ pPtr, - int count, SkTileMode mode, uint32_t flags, - uintptr_t /* SkScalar* */ mPtr, - sk_sp colorSpace)->sk_sp { - SkPoint points[] = { start, end }; - const SkScalar* positions = reinterpret_cast(pPtr); - OptionalMatrix localMatrix(mPtr); - - if (colorType == SkColorType::kRGBA_F32_SkColorType) { - const SkColor4f* colors = reinterpret_cast(cPtr); - return SkGradientShader::MakeLinear(points, colors, colorSpace, positions, count, - mode, flags, &localMatrix); - } else if (colorType == SkColorType::kRGBA_8888_SkColorType) { - const SkColor* colors = reinterpret_cast(cPtr); - return SkGradientShader::MakeLinear(points, colors, positions, count, - mode, flags, &localMatrix); - } else { - SkDebugf("%d is not an accepted colorType\n", colorType); - return nullptr; - } - }), allow_raw_pointers()); #ifdef SK_SERIALIZE_SKP function("_MakePicture", optional_override([](uintptr_t /* unint8_t* */ dPtr, size_t bytes)->sk_sp { @@ -818,78 +792,6 @@ EMSCRIPTEN_BINDINGS(Skia) { return SkPicture::MakeFromData(data.get(), nullptr); }), allow_raw_pointers()); #endif - function("_MakeRadialGradientShader", optional_override([](SkPoint center, SkScalar radius, - uintptr_t cPtr, SkColorType colorType, - uintptr_t /* SkScalar* */ pPtr, - int count, SkTileMode mode, uint32_t flags, - uintptr_t /* SkScalar* */ mPtr, - sk_sp colorSpace)->sk_sp { - const SkScalar* positions = reinterpret_cast(pPtr); - OptionalMatrix localMatrix(mPtr); - if (colorType == SkColorType::kRGBA_F32_SkColorType) { - const SkColor4f* colors = reinterpret_cast(cPtr); - return SkGradientShader::MakeRadial(center, radius, colors, colorSpace, positions, count, - mode, flags, &localMatrix); - } else if (colorType == SkColorType::kRGBA_8888_SkColorType) { - const SkColor* colors = reinterpret_cast(cPtr); - return SkGradientShader::MakeRadial(center, radius, colors, positions, count, - mode, flags, &localMatrix); - } else { - SkDebugf("%d is not an accepted colorType\n", colorType); - return nullptr; - } - }), allow_raw_pointers()); - function("_MakeSweepGradientShader", optional_override([](SkScalar cx, SkScalar cy, - uintptr_t cPtr, SkColorType colorType, - uintptr_t /* SkScalar* */ pPtr, - int count, SkTileMode mode, - SkScalar startAngle, SkScalar endAngle, - uint32_t flags, - uintptr_t /* SkScalar* */ mPtr, - sk_sp colorSpace)->sk_sp { - const SkScalar* positions = reinterpret_cast(pPtr); - OptionalMatrix localMatrix(mPtr); - if (colorType == SkColorType::kRGBA_F32_SkColorType) { - const SkColor4f* colors = reinterpret_cast(cPtr); - return SkGradientShader::MakeSweep(cx, cy, colors, colorSpace, positions, count, - mode, startAngle, endAngle, flags, - &localMatrix); - } else if (colorType == SkColorType::kRGBA_8888_SkColorType) { - const SkColor* colors = reinterpret_cast(cPtr); - return SkGradientShader::MakeSweep(cx, cy, colors, positions, count, - mode, startAngle, endAngle, flags, - &localMatrix); - } else { - SkDebugf("%d is not an accepted colorType\n", colorType); - return nullptr; - } - }), allow_raw_pointers()); - function("_MakeTwoPointConicalGradientShader", optional_override([]( - SkPoint start, SkScalar startRadius, - SkPoint end, SkScalar endRadius, - uintptr_t cPtr, SkColorType colorType, - uintptr_t /* SkScalar* */ pPtr, - int count, SkTileMode mode, uint32_t flags, - uintptr_t /* SkScalar* */ mPtr, - sk_sp colorSpace)->sk_sp { - const SkScalar* positions = reinterpret_cast(pPtr); - OptionalMatrix localMatrix(mPtr); - - if (colorType == SkColorType::kRGBA_F32_SkColorType) { - const SkColor4f* colors = reinterpret_cast(cPtr); - return SkGradientShader::MakeTwoPointConical(start, startRadius, end, endRadius, - colors, colorSpace, positions, count, mode, - flags, &localMatrix); - } else if (colorType == SkColorType::kRGBA_8888_SkColorType) { - const SkColor* colors = reinterpret_cast(cPtr); - return SkGradientShader::MakeTwoPointConical(start, startRadius, end, endRadius, - colors, positions, count, mode, - flags, &localMatrix); - } else { - SkDebugf("%d is not an accepted colorType\n", colorType); - return nullptr; - } - }), allow_raw_pointers()); #ifdef SK_GL class_("GrDirectContext") @@ -1581,13 +1483,107 @@ EMSCRIPTEN_BINDINGS(Skia) { class_("Shader") .smart_ptr>("sk_sp") - .class_function("Blend", select_overload(SkBlendMode, sk_sp, sk_sp)>(&SkShaders::Blend)) - .class_function("_Color", + .class_function("MakeBlend", select_overload(SkBlendMode, sk_sp, sk_sp)>(&SkShaders::Blend)) + .class_function("_MakeColor", optional_override([](uintptr_t /* float* */ cPtr, sk_sp colorSpace)->sk_sp { return SkShaders::Color(ptrToSkColor4f(cPtr), colorSpace); }) ) - .class_function("Lerp", select_overload(float, sk_sp, sk_sp)>(&SkShaders::Lerp)); + .class_function("MakeLerp", select_overload(float, sk_sp, sk_sp)>(&SkShaders::Lerp)) + // Here and in other gradient functions, cPtr is a pointer to an array of data + // representing colors. whether this is an array of SkColor or SkColor4f is indicated + // by the colorType argument. Only RGBA_8888 and RGBA_F32 are accepted. + .class_function("_MakeLinearGradient", optional_override([](SkPoint start, SkPoint end, + uintptr_t cPtr, SkColorType colorType, + uintptr_t /* SkScalar* */ pPtr, + int count, SkTileMode mode, uint32_t flags, + uintptr_t /* SkScalar* */ mPtr, + sk_sp colorSpace)->sk_sp { + SkPoint points[] = { start, end }; + const SkScalar* positions = reinterpret_cast(pPtr); + OptionalMatrix localMatrix(mPtr); + + if (colorType == SkColorType::kRGBA_F32_SkColorType) { + const SkColor4f* colors = reinterpret_cast(cPtr); + return SkGradientShader::MakeLinear(points, colors, colorSpace, positions, count, + mode, flags, &localMatrix); + } else if (colorType == SkColorType::kRGBA_8888_SkColorType) { + const SkColor* colors = reinterpret_cast(cPtr); + return SkGradientShader::MakeLinear(points, colors, positions, count, + mode, flags, &localMatrix); + } + SkDebugf("%d is not an accepted colorType\n", colorType); + return nullptr; + }), allow_raw_pointers()) + .class_function("_MakeRadialGradient", optional_override([](SkPoint center, SkScalar radius, + uintptr_t cPtr, SkColorType colorType, + uintptr_t /* SkScalar* */ pPtr, + int count, SkTileMode mode, uint32_t flags, + uintptr_t /* SkScalar* */ mPtr, + sk_sp colorSpace)->sk_sp { + const SkScalar* positions = reinterpret_cast(pPtr); + OptionalMatrix localMatrix(mPtr); + if (colorType == SkColorType::kRGBA_F32_SkColorType) { + const SkColor4f* colors = reinterpret_cast(cPtr); + return SkGradientShader::MakeRadial(center, radius, colors, colorSpace, positions, count, + mode, flags, &localMatrix); + } else if (colorType == SkColorType::kRGBA_8888_SkColorType) { + const SkColor* colors = reinterpret_cast(cPtr); + return SkGradientShader::MakeRadial(center, radius, colors, positions, count, + mode, flags, &localMatrix); + } + SkDebugf("%d is not an accepted colorType\n", colorType); + return nullptr; + }), allow_raw_pointers()) + .class_function("_MakeSweepGradient", optional_override([](SkScalar cx, SkScalar cy, + uintptr_t cPtr, SkColorType colorType, + uintptr_t /* SkScalar* */ pPtr, + int count, SkTileMode mode, + SkScalar startAngle, SkScalar endAngle, + uint32_t flags, + uintptr_t /* SkScalar* */ mPtr, + sk_sp colorSpace)->sk_sp { + const SkScalar* positions = reinterpret_cast(pPtr); + OptionalMatrix localMatrix(mPtr); + if (colorType == SkColorType::kRGBA_F32_SkColorType) { + const SkColor4f* colors = reinterpret_cast(cPtr); + return SkGradientShader::MakeSweep(cx, cy, colors, colorSpace, positions, count, + mode, startAngle, endAngle, flags, + &localMatrix); + } else if (colorType == SkColorType::kRGBA_8888_SkColorType) { + const SkColor* colors = reinterpret_cast(cPtr); + return SkGradientShader::MakeSweep(cx, cy, colors, positions, count, + mode, startAngle, endAngle, flags, + &localMatrix); + } + SkDebugf("%d is not an accepted colorType\n", colorType); + return nullptr; + }), allow_raw_pointers()) + .class_function("_MakeTwoPointConicalGradient", optional_override([]( + SkPoint start, SkScalar startRadius, + SkPoint end, SkScalar endRadius, + uintptr_t cPtr, SkColorType colorType, + uintptr_t /* SkScalar* */ pPtr, + int count, SkTileMode mode, uint32_t flags, + uintptr_t /* SkScalar* */ mPtr, + sk_sp colorSpace)->sk_sp { + const SkScalar* positions = reinterpret_cast(pPtr); + OptionalMatrix localMatrix(mPtr); + + if (colorType == SkColorType::kRGBA_F32_SkColorType) { + const SkColor4f* colors = reinterpret_cast(cPtr); + return SkGradientShader::MakeTwoPointConical(start, startRadius, end, endRadius, + colors, colorSpace, positions, count, mode, + flags, &localMatrix); + } else if (colorType == SkColorType::kRGBA_8888_SkColorType) { + const SkColor* colors = reinterpret_cast(cPtr); + return SkGradientShader::MakeTwoPointConical(start, startRadius, end, endRadius, + colors, positions, count, mode, + flags, &localMatrix); + } + SkDebugf("%d is not an accepted colorType\n", colorType); + return nullptr; + }), allow_raw_pointers()); #ifdef SK_INCLUDE_RUNTIME_EFFECT class_("RuntimeEffect") diff --git a/modules/canvaskit/externs.js b/modules/canvaskit/externs.js index 96b5e7cf16..47913b61a3 100644 --- a/modules/canvaskit/externs.js +++ b/modules/canvaskit/externs.js @@ -77,13 +77,9 @@ var CanvasKit = { // in the pre-js file) _computeTonalColors: function() {}, _MakeImage: function() {}, - _MakeLinearGradientShader: function() {}, - _MakeRadialGradientShader: function() {}, - _MakeSweepGradientShader: function() {}, _MakeManagedAnimation: function() {}, _MakeParticles: function() {}, _MakePicture: function() {}, - _MakeTwoPointConicalGradientShader: function() {}, _decodeAnimatedImage: function() {}, _decodeImage: function() {}, _drawShapedText: function() {}, @@ -604,17 +600,25 @@ var CanvasKit = { }, Shader: { + // Deprecated names Blend: function() {}, Color: function() {}, - Empty: function() {}, Lerp: function() {}, + // public API (from JS) + MakeBlend: function() {}, + MakeColor: function() {}, + MakeLerp: function() {}, MakeLinearGradient: function() {}, MakeRadialGradient: function() {}, MakeTwoPointConicalGradient: function() {}, MakeSweepGradient: function() {}, // private API (from C++ bindings) - _Color: function() {}, + _MakeColor: function() {}, + _MakeLinearGradient: function() {}, + _MakeRadialGradient: function() {}, + _MakeSweepGradient: function() {}, + _MakeTwoPointConicalGradient: function() {}, }, Surface: { diff --git a/modules/canvaskit/helper.js b/modules/canvaskit/helper.js index 00b1cce50c..db36c6c3d8 100644 --- a/modules/canvaskit/helper.js +++ b/modules/canvaskit/helper.js @@ -278,7 +278,7 @@ function copy2dArray(arr, dest, ptr) { // Copies an array of colors to wasm, returning an object with the pointer // and info necessary to use the copied colors. // Accepts either a flat Float32Array, flat Uint32Array or Array of Float32Arrays. -// If color is an object that was allocated with CanvasKit.Malloc, it's pointer is +// If color is an object that was allocated with CanvasKit.Malloc, its pointer is // returned and no extra copy is performed. // Array of Float32Arrays is deprecated and planned to be removed, prefer flat // Float32Array diff --git a/modules/canvaskit/interface.js b/modules/canvaskit/interface.js index b0ebaa166f..793982198c 100644 --- a/modules/canvaskit/interface.js +++ b/modules/canvaskit/interface.js @@ -1333,22 +1333,26 @@ CanvasKit.onRuntimeInitialized = function() { return dpe; } - CanvasKit.Shader.Color = function(color4f, colorSpace) { + CanvasKit.Shader.MakeColor = function(color4f, colorSpace) { colorSpace = colorSpace || null var cPtr = copyColorToWasm(color4f); - var result = CanvasKit.Shader._Color(cPtr, colorSpace); - return result; + return CanvasKit.Shader._MakeColor(cPtr, colorSpace); } + // TODO(kjlubick) remove deprecated names. + CanvasKit.Shader.Blend = CanvasKit.Shader.MakeBlend; + CanvasKit.Shader.Color = CanvasKit.Shader.MakeColor; + CanvasKit.Shader.Lerp = CanvasKit.Shader.MakeLerp; + CanvasKit.Shader.MakeLinearGradient = function(start, end, colors, pos, mode, localMatrix, flags, colorSpace) { colorSpace = colorSpace || null var cPtrInfo = copyFlexibleColorArray(colors); - var posPtr = copy1dArray(pos, 'HEAPF32'); + var posPtr = copy1dArray(pos, 'HEAPF32'); flags = flags || 0; var localMatrixPtr = copy3x3MatrixToWasm(localMatrix); - var lgs = CanvasKit._MakeLinearGradientShader(start, end, cPtrInfo.colorPtr, cPtrInfo.colorType, posPtr, - cPtrInfo.count, mode, flags, localMatrixPtr, colorSpace); + var lgs = CanvasKit.Shader._MakeLinearGradient(start, end, cPtrInfo.colorPtr, cPtrInfo.colorType, posPtr, + cPtrInfo.count, mode, flags, localMatrixPtr, colorSpace); freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors); pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos); @@ -1358,12 +1362,12 @@ CanvasKit.onRuntimeInitialized = function() { CanvasKit.Shader.MakeRadialGradient = function(center, radius, colors, pos, mode, localMatrix, flags, colorSpace) { colorSpace = colorSpace || null var cPtrInfo = copyFlexibleColorArray(colors); - var posPtr = copy1dArray(pos, 'HEAPF32'); + var posPtr = copy1dArray(pos, 'HEAPF32'); flags = flags || 0; var localMatrixPtr = copy3x3MatrixToWasm(localMatrix); - var rgs = CanvasKit._MakeRadialGradientShader(center, radius, cPtrInfo.colorPtr, cPtrInfo.colorType, posPtr, - cPtrInfo.count, mode, flags, localMatrixPtr, colorSpace); + var rgs = CanvasKit.Shader._MakeRadialGradient(center, radius, cPtrInfo.colorPtr, cPtrInfo.colorType, posPtr, + cPtrInfo.count, mode, flags, localMatrixPtr, colorSpace); freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors); pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos); @@ -1373,16 +1377,16 @@ CanvasKit.onRuntimeInitialized = function() { CanvasKit.Shader.MakeSweepGradient = function(cx, cy, colors, pos, mode, localMatrix, flags, startAngle, endAngle, colorSpace) { colorSpace = colorSpace || null var cPtrInfo = copyFlexibleColorArray(colors); - var posPtr = copy1dArray(pos, 'HEAPF32'); + var posPtr = copy1dArray(pos, 'HEAPF32'); flags = flags || 0; startAngle = startAngle || 0; endAngle = endAngle || 360; var localMatrixPtr = copy3x3MatrixToWasm(localMatrix); - var sgs = CanvasKit._MakeSweepGradientShader(cx, cy, cPtrInfo.colorPtr, cPtrInfo.colorType, posPtr, - cPtrInfo.count, mode, - startAngle, endAngle, flags, - localMatrixPtr, colorSpace); + var sgs = CanvasKit.Shader._MakeSweepGradient(cx, cy, cPtrInfo.colorPtr, cPtrInfo.colorType, posPtr, + cPtrInfo.count, mode, + startAngle, endAngle, flags, + localMatrixPtr, colorSpace); freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors); pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos); @@ -1390,14 +1394,14 @@ CanvasKit.onRuntimeInitialized = function() { } CanvasKit.Shader.MakeTwoPointConicalGradient = function(start, startRadius, end, endRadius, - colors, pos, mode, localMatrix, flags, colorSpace) { + colors, pos, mode, localMatrix, flags, colorSpace) { colorSpace = colorSpace || null var cPtrInfo = copyFlexibleColorArray(colors); - var posPtr = copy1dArray(pos, 'HEAPF32'); + var posPtr = copy1dArray(pos, 'HEAPF32'); flags = flags || 0; var localMatrixPtr = copy3x3MatrixToWasm(localMatrix); - var rgs = CanvasKit._MakeTwoPointConicalGradientShader( + var rgs = CanvasKit.Shader._MakeTwoPointConicalGradient( start, startRadius, end, endRadius, cPtrInfo.colorPtr, cPtrInfo.colorType, posPtr, cPtrInfo.count, mode, flags, localMatrixPtr, colorSpace); diff --git a/modules/canvaskit/tests/core.spec.js b/modules/canvaskit/tests/core.spec.js index 4b465c3212..f359b99915 100644 --- a/modules/canvaskit/tests/core.spec.js +++ b/modules/canvaskit/tests/core.spec.js @@ -535,7 +535,7 @@ describe('Core canvas behavior', () => { [transparentGreen, CanvasKit.BLUE, CanvasKit.RED], [0, 0.65, 1.0], CanvasKit.TileMode.Mirror, - null, // color space + null, // no local matrix ); paint.setShader(cgs); let r = CanvasKit.LTRBRect(0, 0, 100, 100); @@ -763,17 +763,17 @@ describe('Core canvas behavior', () => { }); gm('combined_shaders', (canvas) => { - const rShader = CanvasKit.Shader.Color(CanvasKit.Color(255, 0, 0, 1.0)); - const gShader = CanvasKit.Shader.Color(CanvasKit.Color(0, 255, 0, 0.6)); - const bShader = CanvasKit.Shader.Color(CanvasKit.Color(0, 0, 255, 1.0)); + const rShader = CanvasKit.Shader.Color(CanvasKit.Color(255, 0, 0, 1.0)); // deprecated + const gShader = CanvasKit.Shader.MakeColor(CanvasKit.Color(0, 255, 0, 0.6)); + const bShader = CanvasKit.Shader.MakeColor(CanvasKit.Color(0, 0, 255, 1.0)); - const rgShader = CanvasKit.Shader.Blend(CanvasKit.BlendMode.SrcOver, rShader, gShader); + const rgShader = CanvasKit.Shader.MakeBlend(CanvasKit.BlendMode.SrcOver, rShader, gShader); const p = new CanvasKit.Paint(); p.setShader(rgShader); canvas.drawPaint(p); - const gbShader = CanvasKit.Shader.Lerp(0.5, gShader, bShader); + const gbShader = CanvasKit.Shader.MakeLerp(0.5, gShader, bShader); p.setShader(gbShader); canvas.drawRect(CanvasKit.LTRBRect(5, 100, 300, 400), p);