[canvaskit] Support image assets for Skottie
There's a lot of refactoring here to fix up the
namespace of the JS (see pre/postamble js).
A reviewer can skip all the changes to interface.js,
as they are mostly whitespace changes.
Bug: skia:
Change-Id: I7cedeb98f04d4446ac4dfb555a416f30490b2b94
Reviewed-on: https://skia-review.googlesource.com/c/195885
Reviewed-by: Florin Malita <fmalita@chromium.org>
diff --git a/experimental/canvaskit/CHANGELOG.md b/experimental/canvaskit/CHANGELOG.md
index 1224921..1cd6916 100644
--- a/experimental/canvaskit/CHANGELOG.md
+++ b/experimental/canvaskit/CHANGELOG.md
@@ -6,6 +6,8 @@
## [Unreleased]
+### Added
+ - Optional arguments to `MakeManagedAnimation` for supplying external assets (like images).
## [0.4.0] - 2019-02-25
diff --git a/experimental/canvaskit/canvaskit/example.html b/experimental/canvaskit/canvaskit/example.html
index e39cbd7..0f207a2 100644
--- a/experimental/canvaskit/canvaskit/example.html
+++ b/experimental/canvaskit/canvaskit/example.html
@@ -52,6 +52,7 @@
<canvas id=sk_drinks width=500 height=500></canvas>
<canvas id=sk_party width=500 height=500></canvas>
<canvas id=sk_onboarding width=500 height=500></canvas>
+<canvas id=sk_animated_gif width=500 height=500></canvas>
<script type="text/javascript" src="/node_modules/canvaskit/bin/canvaskit.js"></script>
@@ -62,12 +63,14 @@
var drinksJSON = null;
var confettiJSON = null;
var onboardingJSON = null;
+ var multiFrameJSON = null;
var fullBounds = {fLeft: 0, fTop: 0, fRight: 500, fBottom: 500};
var robotoData = null;
var notoserifData = null;
var bonesImageData = null;
+ var flightAnimGif = null;
CanvasKitInit({
locateFile: (file) => '/node_modules/canvaskit/bin/'+file,
}).ready().then((CK) => {
@@ -82,6 +85,7 @@
SkottieExample(CanvasKit, 'sk_drinks', drinksJSON, fullBounds);
SkottieExample(CanvasKit, 'sk_party', confettiJSON, fullBounds);
SkottieExample(CanvasKit, 'sk_onboarding', onboardingJSON, fullBounds);
+ SkottieAssetExample(CanvasKit, multiFrameJSON, flightAnimGif);
CanvasAPI1(CanvasKit);
CanvasAPI2(CanvasKit);
@@ -130,6 +134,13 @@
});
});
+ fetch('https://storage.googleapis.com/skia-cdn/misc/skottie_sample_multiframe.json').then((resp) => {
+ resp.text().then((str) => {
+ multiFrameJSON = str;
+ SkottieAssetExample(CanvasKit, multiFrameJSON, flightAnimGif);
+ });
+ });
+
fetch('https://storage.googleapis.com/skia-cdn/misc/bones.jpg').then((resp) => {
resp.arrayBuffer().then((buffer) => {
bonesImageData = buffer;
@@ -137,6 +148,13 @@
});
});
+ fetch('https://storage.googleapis.com/skia-cdn/misc/flightAnim.gif').then((resp) => {
+ resp.arrayBuffer().then((buffer) => {
+ flightAnimGif = buffer;
+ SkottieAssetExample(CanvasKit, multiFrameJSON, flightAnimGif);
+ });
+ });
+
fetch('./Roboto-Regular.woff').then((resp) => {
resp.arrayBuffer().then((buffer) => {
robotoData = buffer;
@@ -425,6 +443,49 @@
return surface;
}
+ function SkottieAssetExample(CanvasKit, jsonStr, imgdata) {
+ if (!CanvasKit || !jsonStr || !imgdata) {
+ return;
+ }
+ const animation = CanvasKit.MakeManagedAnimation(jsonStr, {
+ 'images/image_0.png': imgdata,
+ });
+ const duration = animation.duration() * 1000;
+ const size = animation.size();
+ let c = document.getElementById('sk_animated_gif');
+
+ const surface = CanvasKit.MakeCanvasSurface('sk_animated_gif');
+ if (!surface) {
+ console.error('Could not make surface');
+ return;
+ }
+ const context = CanvasKit.currentContext();
+ const canvas = surface.getCanvas();
+
+ const textPaint = new CanvasKit.SkPaint();
+ const textFont = new CanvasKit.SkFont(null, 20);
+
+ let firstFrame = Date.now();
+
+ function drawFrame() {
+ let seek = ((Date.now() - firstFrame) / duration) % 1.0;
+ CanvasKit.setCurrentContext(context);
+ animation.seek(seek);
+ canvas.clear(CanvasKit.WHITE);
+ animation.render(canvas, fullBounds);
+ canvas.drawText('Embed an animated gif and transforming it',
+ 5, 20, textPaint, textFont)
+ surface.flush();
+
+ window.requestAnimationFrame(drawFrame);
+ }
+ window.requestAnimationFrame(drawFrame);
+
+ // animation.delete();
+ // assetProvider.delete(); // This will clean up anything we passed in.
+ return surface;
+ }
+
function CanvasAPI1(CanvasKit) {
let skcanvas = CanvasKit.MakeCanvas(300, 300);
let realCanvas = document.getElementById('api1_c');
diff --git a/experimental/canvaskit/compile.sh b/experimental/canvaskit/compile.sh
index 66c418f..692695c 100755
--- a/experimental/canvaskit/compile.sh
+++ b/experimental/canvaskit/compile.sh
@@ -53,6 +53,7 @@
# TODO(fmalita,kjlubick): reduce this list to one item by fixing
# the libskottie.a and libsksg.a builds
+SKOTTIE_JS="--pre-js $BASE_DIR/skottie.js"
SKOTTIE_BINDINGS="$BASE_DIR/skottie_bindings.cpp\
src/core/SkColorMatrixFilterRowMajor255.cpp \
src/core/SkCubicMap.cpp \
@@ -68,6 +69,7 @@
if [[ $@ == *no_skottie* ]]; then
echo "Omitting Skottie"
+ SKOTTIE_JS=""
SKOTTIE_LIB=""
SKOTTIE_BINDINGS=""
fi
@@ -165,6 +167,7 @@
skia_use_system_freetype2=true \
skia_use_system_libjpeg_turbo = false \
skia_use_vulkan=false \
+ skia_use_wuffs = true \
skia_use_zlib=true \
\
${GN_SHAPER} \
@@ -217,10 +220,13 @@
$WASM_GPU \
-std=c++14 \
--bind \
+ --pre-js $BASE_DIR/preamble.js \
--pre-js $BASE_DIR/helper.js \
--pre-js $BASE_DIR/interface.js \
- --post-js $BASE_DIR/ready.js \
+ $SKOTTIE_JS \
$HTML_CANVAS_API \
+ --pre-js $BASE_DIR/postamble.js \
+ --post-js $BASE_DIR/ready.js \
$BUILTIN_FONT \
$BASE_DIR/canvaskit_bindings.cpp \
$SKOTTIE_BINDINGS \
diff --git a/experimental/canvaskit/externs.js b/experimental/canvaskit/externs.js
index 02c61b7..2abc058 100644
--- a/experimental/canvaskit/externs.js
+++ b/experimental/canvaskit/externs.js
@@ -44,6 +44,7 @@
MakePathFromSVGString: function() {},
MakeRadialGradientShader: function() {},
MakeSWCanvasSurface: function() {},
+ MakeManagedAnimation: function() {},
MakeSkDashPathEffect: function() {},
MakeSkVertices: function() {},
MakeSurface: function() {},
@@ -63,6 +64,7 @@
_MakeLinearGradientShader: function() {},
_MakePathFromCmds: function() {},
_MakeRadialGradientShader: function() {},
+ _MakeManagedAnimation: function() {},
_MakeSkDashPathEffect: function() {},
_MakeSkVertices: function() {},
_MakeTwoPointConicalGradientShader: function() {},
@@ -438,7 +440,10 @@
* @type {Int32Array}
*/
HEAP32: {},
-
+ /**
+ * @type {Uint32Array}
+ */
+ HEAPU32: {},
_malloc: function() {},
_free: function() {},
onRuntimeInitialized: function() {},
diff --git a/experimental/canvaskit/helper.js b/experimental/canvaskit/helper.js
index e49d2b7..16be49f 100644
--- a/experimental/canvaskit/helper.js
+++ b/experimental/canvaskit/helper.js
@@ -1,47 +1,50 @@
+// helper JS that could be used anywhere in the glue code
-// Adds any extra JS functions/helpers we want to add to CanvasKit.
-// Wrapped in a function to avoid leaking global variables.
-(function(CanvasKit){
+function clamp(c) {
+ return Math.round(Math.max(0, Math.min(c || 0, 255)));
+}
- function clamp(c) {
- return Math.round(Math.max(0, Math.min(c || 0, 255)));
+// Colors are just a 32 bit number with 8 bits each of a, r, g, b
+// The API is the same as CSS's representation of color rgba(), that is
+// r,g,b are 0-255, and a is 0.0 to 1.0.
+// if a is omitted, it will be assumed to be 1.0
+CanvasKit.Color = function(r, g, b, a) {
+ if (a === undefined) {
+ a = 1;
}
+ return (clamp(a*255) << 24) | (clamp(r) << 16) | (clamp(g) << 8) | (clamp(b) << 0);
+}
- // Colors are just a 32 bit number with 8 bits each of a, r, g, b
- // The API is the same as CSS's representation of color rgba(), that is
- // r,g,b are 0-255, and a is 0.0 to 1.0.
- // if a is omitted, it will be assumed to be 1.0
- CanvasKit.Color = function(r, g, b, a) {
- if (a === undefined) {
- a = 1;
- }
- return (clamp(a*255) << 24) | (clamp(r) << 16) | (clamp(g) << 8) | (clamp(b) << 0);
+// returns [r, g, b, a] from a color
+// where a is scaled between 0 and 1.0
+CanvasKit.getColorComponents = function(color) {
+ return [
+ (color >> 16) & 0xFF,
+ (color >> 8) & 0xFF,
+ (color >> 0) & 0xFF,
+ ((color >> 24) & 0xFF) / 255,
+ ]
+}
+
+CanvasKit.multiplyByAlpha = function(color, alpha) {
+ if (alpha === 1) {
+ return color;
}
+ // extract as int from 0 to 255
+ var a = (color >> 24) & 0xFF;
+ a *= alpha;
+ // mask off the old alpha
+ color &= 0xFFFFFF;
+ return clamp(a) << 24 | color;
+}
- // returns [r, g, b, a] from a color
- // where a is scaled between 0 and 1.0
- CanvasKit.getColorComponents = function(color) {
- return [
- (color >> 16) & 0xFF,
- (color >> 8) & 0xFF,
- (color >> 0) & 0xFF,
- ((color >> 24) & 0xFF) / 255,
- ]
- }
+function radiansToDegrees(rad) {
+ return (rad / Math.PI) * 180;
+}
- CanvasKit.multiplyByAlpha = function(color, alpha) {
- if (alpha === 1) {
- return color;
- }
- // extract as int from 0 to 255
- var a = (color >> 24) & 0xFF;
- a *= alpha;
- // mask off the old alpha
- color &= 0xFFFFFF;
- return clamp(a) << 24 | color;
-
- }
-}(Module)); // When this file is loaded in, the high level object is "Module";
+function degreesToRadians(deg) {
+ return (deg / 180) * Math.PI;
+}
// See https://stackoverflow.com/a/31090240
// This contraption keeps closure from minifying away the check
@@ -51,4 +54,110 @@
function almostEqual(floata, floatb) {
return Math.abs(floata - floatb) < 0.00001;
-}
\ No newline at end of file
+}
+
+
+var nullptr = 0; // emscripten doesn't like to take null as uintptr_t
+
+// arr can be a normal JS array or a TypedArray
+// dest is something like CanvasKit.HEAPF32
+function copy1dArray(arr, dest) {
+ if (!arr || !arr.length) {
+ return nullptr;
+ }
+ var ptr = CanvasKit._malloc(arr.length * dest.BYTES_PER_ELEMENT);
+ // In c++ terms, the WASM heap is a uint8_t*, a long buffer/array of single
+ // byte elements. When we run _malloc, we always get an offset/pointer into
+ // that block of memory.
+ // CanvasKit exposes some different views to make it easier to work with
+ // different types. HEAPF32 for example, exposes it as a float*
+ // However, to make the ptr line up, we have to do some pointer arithmetic.
+ // Concretely, we need to convert ptr to go from an index into a 1-byte-wide
+ // buffer to an index into a 4-byte-wide buffer (in the case of HEAPF32)
+ // and thus we divide ptr by 4.
+ dest.set(arr, ptr / dest.BYTES_PER_ELEMENT);
+ return ptr;
+}
+
+// arr should be a non-jagged 2d JS array (TypedArrays can't be nested
+// inside themselves.)
+// dest is something like CanvasKit.HEAPF32
+function copy2dArray(arr, dest) {
+ if (!arr || !arr.length) {
+ return nullptr;
+ }
+ var ptr = CanvasKit._malloc(arr.length * arr[0].length * dest.BYTES_PER_ELEMENT);
+ var idx = 0;
+ var adjustedPtr = ptr / dest.BYTES_PER_ELEMENT;
+ for (var r = 0; r < arr.length; r++) {
+ for (var c = 0; c < arr[0].length; c++) {
+ dest[adjustedPtr + idx] = arr[r][c];
+ idx++;
+ }
+ }
+ return ptr;
+}
+
+// arr should be a non-jagged 3d JS array (TypedArrays can't be nested
+// inside themselves.)
+// dest is something like CanvasKit.HEAPF32
+function copy3dArray(arr, dest) {
+ if (!arr || !arr.length || !arr[0].length) {
+ return nullptr;
+ }
+ var ptr = CanvasKit._malloc(arr.length * arr[0].length * arr[0][0].length * dest.BYTES_PER_ELEMENT);
+ var idx = 0;
+ var adjustedPtr = ptr / dest.BYTES_PER_ELEMENT;
+ for (var x = 0; x < arr.length; x++) {
+ for (var y = 0; y < arr[0].length; y++) {
+ for (var z = 0; z < arr[0][0].length; z++) {
+ dest[adjustedPtr + idx] = arr[x][y][z];
+ idx++;
+ }
+ }
+ }
+ return ptr;
+}
+
+// Caching the Float32Arrays can save having to reallocate them
+// over and over again.
+var Float32ArrayCache = {};
+
+// Takes a 2D array of commands and puts them into the WASM heap
+// as a 1D array. This allows them to referenced from the C++ code.
+// Returns a 2 element array, with the first item being essentially a
+// pointer to the array and the second item being the length of
+// the new 1D array.
+//
+// Example usage:
+// let cmds = [
+// [CanvasKit.MOVE_VERB, 0, 10],
+// [CanvasKit.LINE_VERB, 30, 40],
+// [CanvasKit.QUAD_VERB, 20, 50, 45, 60],
+// ];
+function loadCmdsTypedArray(arr) {
+ var len = 0;
+ for (var r = 0; r < arr.length; r++) {
+ len += arr[r].length;
+ }
+
+ var ta;
+ if (Float32ArrayCache[len]) {
+ ta = Float32ArrayCache[len];
+ } else {
+ ta = new Float32Array(len);
+ Float32ArrayCache[len] = ta;
+ }
+ // Flatten into a 1d array
+ var i = 0;
+ for (var r = 0; r < arr.length; r++) {
+ for (var c = 0; c < arr[r].length; c++) {
+ var item = arr[r][c];
+ ta[i] = item;
+ i++;
+ }
+ }
+
+ var ptr = copy1dArray(ta, CanvasKit.HEAPF32);
+ return [ptr, len];
+}
diff --git a/experimental/canvaskit/htmlcanvas/postamble.js b/experimental/canvaskit/htmlcanvas/postamble.js
index 592e6c5..8d2471e 100644
--- a/experimental/canvaskit/htmlcanvas/postamble.js
+++ b/experimental/canvaskit/htmlcanvas/postamble.js
@@ -1,2 +1,2 @@
// This closes the scope started in preamble.js
-}(Module)); // When this file is loaded in, the high level object is "Module";
+}());
diff --git a/experimental/canvaskit/htmlcanvas/preamble.js b/experimental/canvaskit/htmlcanvas/preamble.js
index 4ee1fe1..3711bf3 100644
--- a/experimental/canvaskit/htmlcanvas/preamble.js
+++ b/experimental/canvaskit/htmlcanvas/preamble.js
@@ -2,7 +2,7 @@
// Specifically, the code that emulates the HTML Canvas interface
// (which is called HTMLCanvas or similar to avoid confusion with
// SkCanvas).
-(function(CanvasKit) {
+(function() {
// This allows us to expose internal functions (e.g. color
// parsing) for unit-testing, even in the minified version.
diff --git a/experimental/canvaskit/interface.js b/experimental/canvaskit/interface.js
index f1428d0..91456d5 100644
--- a/experimental/canvaskit/interface.js
+++ b/experimental/canvaskit/interface.js
@@ -1,807 +1,688 @@
// Adds JS functions to augment the CanvasKit interface.
// For example, if there is a wrapper around the C++ call or logic to allow
// chaining, it should go here.
-(function(CanvasKit) {
- // CanvasKit.onRuntimeInitialized is called after the WASM library has loaded.
- // Anything that modifies an exposed class (e.g. SkPath) should be set
- // after onRuntimeInitialized, otherwise, it can happen outside of that scope.
- CanvasKit.onRuntimeInitialized = function() {
- // All calls to 'this' need to go in externs.js so closure doesn't minify them away.
+// CanvasKit.onRuntimeInitialized is called after the WASM library has loaded.
+// Anything that modifies an exposed class (e.g. SkPath) should be set
+// after onRuntimeInitialized, otherwise, it can happen outside of that scope.
+CanvasKit.onRuntimeInitialized = function() {
+ // All calls to 'this' need to go in externs.js so closure doesn't minify them away.
- // Add some helpers for matrices. This is ported from SkMatrix.cpp
- // to save complexity and overhead of going back and forth between
- // C++ and JS layers.
- // I would have liked to use something like DOMMatrix, except it
- // isn't widely supported (would need polyfills) and it doesn't
- // have a mapPoints() function (which could maybe be tacked on here).
- // If DOMMatrix catches on, it would be worth re-considering this usage.
- CanvasKit.SkMatrix = {};
- function sdot(a, b, c, d, e, f) {
- e = e || 0;
- f = f || 0;
- return a * b + c * d + e * f;
+ // Add some helpers for matrices. This is ported from SkMatrix.cpp
+ // to save complexity and overhead of going back and forth between
+ // C++ and JS layers.
+ // I would have liked to use something like DOMMatrix, except it
+ // isn't widely supported (would need polyfills) and it doesn't
+ // have a mapPoints() function (which could maybe be tacked on here).
+ // If DOMMatrix catches on, it would be worth re-considering this usage.
+ CanvasKit.SkMatrix = {};
+ function sdot(a, b, c, d, e, f) {
+ e = e || 0;
+ f = f || 0;
+ return a * b + c * d + e * f;
+ }
+
+ CanvasKit.SkMatrix.identity = function() {
+ return [
+ 1, 0, 0,
+ 0, 1, 0,
+ 0, 0, 1,
+ ];
+ };
+
+ // Return the inverse (if it exists) of this matrix.
+ // Otherwise, return the identity.
+ CanvasKit.SkMatrix.invert = function(m) {
+ var det = m[0]*m[4]*m[8] + m[1]*m[5]*m[6] + m[2]*m[3]*m[7]
+ - m[2]*m[4]*m[6] - m[1]*m[3]*m[8] - m[0]*m[5]*m[7];
+ if (!det) {
+ SkDebug('Warning, uninvertible matrix');
+ return CanvasKit.SkMatrix.identity();
}
+ return [
+ (m[4]*m[8] - m[5]*m[7])/det, (m[2]*m[7] - m[1]*m[8])/det, (m[1]*m[5] - m[2]*m[4])/det,
+ (m[5]*m[6] - m[3]*m[8])/det, (m[0]*m[8] - m[2]*m[6])/det, (m[2]*m[3] - m[0]*m[5])/det,
+ (m[3]*m[7] - m[4]*m[6])/det, (m[1]*m[6] - m[0]*m[7])/det, (m[0]*m[4] - m[1]*m[3])/det,
+ ];
+ };
- CanvasKit.SkMatrix.identity = function() {
- return [
- 1, 0, 0,
- 0, 1, 0,
- 0, 0, 1,
- ];
- };
-
- // Return the inverse (if it exists) of this matrix.
- // Otherwise, return the identity.
- CanvasKit.SkMatrix.invert = function(m) {
- var det = m[0]*m[4]*m[8] + m[1]*m[5]*m[6] + m[2]*m[3]*m[7]
- - m[2]*m[4]*m[6] - m[1]*m[3]*m[8] - m[0]*m[5]*m[7];
- if (!det) {
- SkDebug('Warning, uninvertible matrix');
- return CanvasKit.SkMatrix.identity();
- }
- return [
- (m[4]*m[8] - m[5]*m[7])/det, (m[2]*m[7] - m[1]*m[8])/det, (m[1]*m[5] - m[2]*m[4])/det,
- (m[5]*m[6] - m[3]*m[8])/det, (m[0]*m[8] - m[2]*m[6])/det, (m[2]*m[3] - m[0]*m[5])/det,
- (m[3]*m[7] - m[4]*m[6])/det, (m[1]*m[6] - m[0]*m[7])/det, (m[0]*m[4] - m[1]*m[3])/det,
- ];
- };
-
- // Maps the given points according to the passed in matrix.
- // Results are done in place.
- // See SkMatrix.h::mapPoints for the docs on the math.
- CanvasKit.SkMatrix.mapPoints = function(matrix, ptArr) {
- if (ptArr.length % 2) {
- throw 'mapPoints requires an even length arr';
- }
- for (var i = 0; i < ptArr.length; i+=2) {
- var x = ptArr[i], y = ptArr[i+1];
- // Gx+Hy+I
- var denom = matrix[6]*x + matrix[7]*y + matrix[8];
- // Ax+By+C
- var xTrans = matrix[0]*x + matrix[1]*y + matrix[2];
- // Dx+Ey+F
- var yTrans = matrix[3]*x + matrix[4]*y + matrix[5];
- ptArr[i] = xTrans/denom;
- ptArr[i+1] = yTrans/denom;
- }
- return ptArr;
- };
-
- CanvasKit.SkMatrix.multiply = function(m1, m2) {
- var result = [0,0,0, 0,0,0, 0,0,0];
- for (var r = 0; r < 3; r++) {
- for (var c = 0; c < 3; c++) {
- // m1 and m2 are 1D arrays pretending to be 2D arrays
- result[3*r + c] = sdot(m1[3*r + 0], m2[3*0 + c],
- m1[3*r + 1], m2[3*1 + c],
- m1[3*r + 2], m2[3*2 + c]);
- }
- }
- return result;
+ // Maps the given points according to the passed in matrix.
+ // Results are done in place.
+ // See SkMatrix.h::mapPoints for the docs on the math.
+ CanvasKit.SkMatrix.mapPoints = function(matrix, ptArr) {
+ if (ptArr.length % 2) {
+ throw 'mapPoints requires an even length arr';
}
+ for (var i = 0; i < ptArr.length; i+=2) {
+ var x = ptArr[i], y = ptArr[i+1];
+ // Gx+Hy+I
+ var denom = matrix[6]*x + matrix[7]*y + matrix[8];
+ // Ax+By+C
+ var xTrans = matrix[0]*x + matrix[1]*y + matrix[2];
+ // Dx+Ey+F
+ var yTrans = matrix[3]*x + matrix[4]*y + matrix[5];
+ ptArr[i] = xTrans/denom;
+ ptArr[i+1] = yTrans/denom;
+ }
+ return ptArr;
+ };
- // Return a matrix representing a rotation by n radians.
- // px, py optionally say which point the rotation should be around
- // with the default being (0, 0);
- CanvasKit.SkMatrix.rotated = function(radians, px, py) {
- px = px || 0;
- py = py || 0;
- var sinV = Math.sin(radians);
- var cosV = Math.cos(radians);
- return [
- cosV, -sinV, sdot( sinV, py, 1 - cosV, px),
- sinV, cosV, sdot(-sinV, px, 1 - cosV, py),
- 0, 0, 1,
- ];
- };
-
- CanvasKit.SkMatrix.scaled = function(sx, sy, px, py) {
- px = px || 0;
- py = py || 0;
- return [
- sx, 0, px - sx * px,
- 0, sy, py - sy * py,
- 0, 0, 1,
- ];
- };
-
- CanvasKit.SkMatrix.skewed = function(kx, ky, px, py) {
- px = px || 0;
- py = py || 0;
- return [
- 1, kx, -kx * px,
- ky, 1, -ky * py,
- 0, 0, 1,
- ];
- };
-
- CanvasKit.SkMatrix.translated = function(dx, dy) {
- return [
- 1, 0, dx,
- 0, 1, dy,
- 0, 0, 1,
- ];
- };
-
- CanvasKit.SkPath.prototype.addArc = function(oval, startAngle, sweepAngle) {
- // see arc() for the HTMLCanvas version
- // note input angles are degrees.
- this._addArc(oval, startAngle, sweepAngle);
- return this;
- };
-
- CanvasKit.SkPath.prototype.addPath = function() {
- // Takes 1, 2, 7, or 10 required args, where the first arg is always the path.
- // The last arg is optional and chooses between add or extend mode.
- // The options for the remaining args are:
- // - an array of 6 or 9 parameters (perspective is optional)
- // - the 9 parameters of a full matrix or
- // the 6 non-perspective params of a matrix.
- var args = Array.prototype.slice.call(arguments);
- var path = args[0];
- var extend = false;
- if (typeof args[args.length-1] === "boolean") {
- extend = args.pop();
+ CanvasKit.SkMatrix.multiply = function(m1, m2) {
+ var result = [0,0,0, 0,0,0, 0,0,0];
+ for (var r = 0; r < 3; r++) {
+ for (var c = 0; c < 3; c++) {
+ // m1 and m2 are 1D arrays pretending to be 2D arrays
+ result[3*r + c] = sdot(m1[3*r + 0], m2[3*0 + c],
+ m1[3*r + 1], m2[3*1 + c],
+ m1[3*r + 2], m2[3*2 + c]);
}
- if (args.length === 1) {
- // Add path, unchanged. Use identity matrix
- this._addPath(path, 1, 0, 0,
- 0, 1, 0,
- 0, 0, 1,
- extend);
- } else if (args.length === 2) {
- // User provided the 9 params of a full matrix as an array.
- var a = args[1];
- this._addPath(path, a[0], a[1], a[2],
- a[3], a[4], a[5],
- a[6] || 0, a[7] || 0, a[8] || 1,
- extend);
- } else if (args.length === 7 || args.length === 10) {
- // User provided the 9 params of a (full) matrix directly.
- // (or just the 6 non perspective ones)
- // These are in the same order as what Skia expects.
- var a = args;
- this._addPath(path, a[1], a[2], a[3],
- a[4], a[5], a[6],
- a[7] || 0, a[8] || 0, a[9] || 1,
- extend);
- } else {
- SkDebug('addPath expected to take 1, 2, 7, or 10 required args. Got ' + args.length);
- return null;
- }
- return this;
- };
+ }
+ return result;
+ }
- CanvasKit.SkPath.prototype.addRect = function() {
- // Takes 1, 2, 4 or 5 args
- // - SkRect
- // - SkRect, isCCW
- // - left, top, right, bottom
- // - left, top, right, bottom, isCCW
- if (arguments.length === 1 || arguments.length === 2) {
- var r = arguments[0];
- var ccw = arguments[1] || false;
- this._addRect(r.fLeft, r.fTop, r.fRight, r.fBottom, ccw);
- } else if (arguments.length === 4 || arguments.length === 5) {
- var a = arguments;
- this._addRect(a[0], a[1], a[2], a[3], a[4] || false);
- } else {
- SkDebug('addRect expected to take 1, 2, 4, or 5 args. Got ' + arguments.length);
- return null;
- }
- return this;
- };
+ // Return a matrix representing a rotation by n radians.
+ // px, py optionally say which point the rotation should be around
+ // with the default being (0, 0);
+ CanvasKit.SkMatrix.rotated = function(radians, px, py) {
+ px = px || 0;
+ py = py || 0;
+ var sinV = Math.sin(radians);
+ var cosV = Math.cos(radians);
+ return [
+ cosV, -sinV, sdot( sinV, py, 1 - cosV, px),
+ sinV, cosV, sdot(-sinV, px, 1 - cosV, py),
+ 0, 0, 1,
+ ];
+ };
- CanvasKit.SkPath.prototype.addRoundRect = function() {
- // Takes 3, 4, 6 or 7 args
- // - SkRect, radii, ccw
- // - SkRect, rx, ry, ccw
- // - left, top, right, bottom, radii, ccw
- // - left, top, right, bottom, rx, ry, ccw
- var args = arguments;
- if (args.length === 3 || args.length === 6) {
- var radii = args[args.length-2];
- } else if (args.length === 6 || args.length === 7){
- // duplicate the given (rx, ry) pairs for each corner.
- var rx = args[args.length-3];
- var ry = args[args.length-2];
- var radii = [rx, ry, rx, ry, rx, ry, rx, ry];
- } else {
- SkDebug('addRoundRect expected to take 3, 4, 6, or 7 args. Got ' + args.length);
- return null;
- }
- if (radii.length !== 8) {
- SkDebug('addRoundRect needs 8 radii provided. Got ' + radii.length);
- return null;
- }
- var rptr = copy1dArray(radii, CanvasKit.HEAPF32);
- if (args.length === 3 || args.length === 4) {
- var r = args[0];
- var ccw = args[args.length - 1];
- this._addRoundRect(r.fLeft, r.fTop, r.fRight, r.fBottom, rptr, ccw);
- } else if (args.length === 6 || args.length === 7) {
- var a = args;
- this._addRoundRect(a[0], a[1], a[2], a[3], rptr, ccw);
- }
- CanvasKit._free(rptr);
- return this;
- };
+ CanvasKit.SkMatrix.scaled = function(sx, sy, px, py) {
+ px = px || 0;
+ py = py || 0;
+ return [
+ sx, 0, px - sx * px,
+ 0, sy, py - sy * py,
+ 0, 0, 1,
+ ];
+ };
- CanvasKit.SkPath.prototype.arc = function(x, y, radius, startAngle, endAngle, ccw) {
- // emulates the HTMLCanvas behavior. See addArc() for the SkPath version.
- // Note input angles are radians.
- var bounds = CanvasKit.LTRBRect(x-radius, y-radius, x+radius, y+radius);
- var sweep = radiansToDegrees(endAngle - startAngle) - (360 * !!ccw);
- var temp = new CanvasKit.SkPath();
- temp.addArc(bounds, radiansToDegrees(startAngle), sweep);
- this.addPath(temp, true);
- temp.delete();
- return this;
- };
+ CanvasKit.SkMatrix.skewed = function(kx, ky, px, py) {
+ px = px || 0;
+ py = py || 0;
+ return [
+ 1, kx, -kx * px,
+ ky, 1, -ky * py,
+ 0, 0, 1,
+ ];
+ };
- CanvasKit.SkPath.prototype.arcTo = function() {
- // takes 4, 5 or 7 args
- // - 5 x1, y1, x2, y2, radius
- // - 4 oval (as Rect), startAngle, sweepAngle, forceMoveTo
- // - 7 x1, y1, x2, y2, startAngle, sweepAngle, forceMoveTo
- var args = arguments;
- if (args.length === 5) {
- this._arcTo(args[0], args[1], args[2], args[3], args[4]);
- } else if (args.length === 4) {
- this._arcTo(args[0], args[1], args[2], args[3]);
- } else if (args.length === 7) {
- this._arcTo(CanvasKit.LTRBRect(args[0], args[1], args[2], args[3]),
- args[4], args[5], args[6]);
- } else {
- throw 'Invalid args for arcTo. Expected 4, 5, or 7, got '+ args.length;
- }
+ CanvasKit.SkMatrix.translated = function(dx, dy) {
+ return [
+ 1, 0, dx,
+ 0, 1, dy,
+ 0, 0, 1,
+ ];
+ };
- return this;
- };
+ CanvasKit.SkPath.prototype.addArc = function(oval, startAngle, sweepAngle) {
+ // see arc() for the HTMLCanvas version
+ // note input angles are degrees.
+ this._addArc(oval, startAngle, sweepAngle);
+ return this;
+ };
- CanvasKit.SkPath.prototype.close = function() {
- this._close();
- return this;
- };
-
- CanvasKit.SkPath.prototype.conicTo = function(x1, y1, x2, y2, w) {
- this._conicTo(x1, y1, x2, y2, w);
- return this;
- };
-
- CanvasKit.SkPath.prototype.cubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) {
- this._cubicTo(cp1x, cp1y, cp2x, cp2y, x, y);
- return this;
- };
-
- CanvasKit.SkPath.prototype.dash = function(on, off, phase) {
- if (this._dash(on, off, phase)) {
- return this;
- }
+ CanvasKit.SkPath.prototype.addPath = function() {
+ // Takes 1, 2, 7, or 10 required args, where the first arg is always the path.
+ // The last arg is optional and chooses between add or extend mode.
+ // The options for the remaining args are:
+ // - an array of 6 or 9 parameters (perspective is optional)
+ // - the 9 parameters of a full matrix or
+ // the 6 non-perspective params of a matrix.
+ var args = Array.prototype.slice.call(arguments);
+ var path = args[0];
+ var extend = false;
+ if (typeof args[args.length-1] === "boolean") {
+ extend = args.pop();
+ }
+ if (args.length === 1) {
+ // Add path, unchanged. Use identity matrix
+ this._addPath(path, 1, 0, 0,
+ 0, 1, 0,
+ 0, 0, 1,
+ extend);
+ } else if (args.length === 2) {
+ // User provided the 9 params of a full matrix as an array.
+ var a = args[1];
+ this._addPath(path, a[0], a[1], a[2],
+ a[3], a[4], a[5],
+ a[6] || 0, a[7] || 0, a[8] || 1,
+ extend);
+ } else if (args.length === 7 || args.length === 10) {
+ // User provided the 9 params of a (full) matrix directly.
+ // (or just the 6 non perspective ones)
+ // These are in the same order as what Skia expects.
+ var a = args;
+ this._addPath(path, a[1], a[2], a[3],
+ a[4], a[5], a[6],
+ a[7] || 0, a[8] || 0, a[9] || 1,
+ extend);
+ } else {
+ SkDebug('addPath expected to take 1, 2, 7, or 10 required args. Got ' + args.length);
return null;
- };
+ }
+ return this;
+ };
- CanvasKit.SkPath.prototype.lineTo = function(x, y) {
- this._lineTo(x, y);
- return this;
- };
-
- CanvasKit.SkPath.prototype.moveTo = function(x, y) {
- this._moveTo(x, y);
- return this;
- };
-
- CanvasKit.SkPath.prototype.op = function(otherPath, op) {
- if (this._op(otherPath, op)) {
- return this;
- }
+ CanvasKit.SkPath.prototype.addRect = function() {
+ // Takes 1, 2, 4 or 5 args
+ // - SkRect
+ // - SkRect, isCCW
+ // - left, top, right, bottom
+ // - left, top, right, bottom, isCCW
+ if (arguments.length === 1 || arguments.length === 2) {
+ var r = arguments[0];
+ var ccw = arguments[1] || false;
+ this._addRect(r.fLeft, r.fTop, r.fRight, r.fBottom, ccw);
+ } else if (arguments.length === 4 || arguments.length === 5) {
+ var a = arguments;
+ this._addRect(a[0], a[1], a[2], a[3], a[4] || false);
+ } else {
+ SkDebug('addRect expected to take 1, 2, 4, or 5 args. Got ' + arguments.length);
return null;
- };
+ }
+ return this;
+ };
- CanvasKit.SkPath.prototype.quadTo = function(cpx, cpy, x, y) {
- this._quadTo(cpx, cpy, x, y);
+ CanvasKit.SkPath.prototype.addRoundRect = function() {
+ // Takes 3, 4, 6 or 7 args
+ // - SkRect, radii, ccw
+ // - SkRect, rx, ry, ccw
+ // - left, top, right, bottom, radii, ccw
+ // - left, top, right, bottom, rx, ry, ccw
+ var args = arguments;
+ if (args.length === 3 || args.length === 6) {
+ var radii = args[args.length-2];
+ } else if (args.length === 6 || args.length === 7){
+ // duplicate the given (rx, ry) pairs for each corner.
+ var rx = args[args.length-3];
+ var ry = args[args.length-2];
+ var radii = [rx, ry, rx, ry, rx, ry, rx, ry];
+ } else {
+ SkDebug('addRoundRect expected to take 3, 4, 6, or 7 args. Got ' + args.length);
+ return null;
+ }
+ if (radii.length !== 8) {
+ SkDebug('addRoundRect needs 8 radii provided. Got ' + radii.length);
+ return null;
+ }
+ var rptr = copy1dArray(radii, CanvasKit.HEAPF32);
+ if (args.length === 3 || args.length === 4) {
+ var r = args[0];
+ var ccw = args[args.length - 1];
+ this._addRoundRect(r.fLeft, r.fTop, r.fRight, r.fBottom, rptr, ccw);
+ } else if (args.length === 6 || args.length === 7) {
+ var a = args;
+ this._addRoundRect(a[0], a[1], a[2], a[3], rptr, ccw);
+ }
+ CanvasKit._free(rptr);
+ return this;
+ };
+
+ CanvasKit.SkPath.prototype.arc = function(x, y, radius, startAngle, endAngle, ccw) {
+ // emulates the HTMLCanvas behavior. See addArc() for the SkPath version.
+ // Note input angles are radians.
+ var bounds = CanvasKit.LTRBRect(x-radius, y-radius, x+radius, y+radius);
+ var sweep = radiansToDegrees(endAngle - startAngle) - (360 * !!ccw);
+ var temp = new CanvasKit.SkPath();
+ temp.addArc(bounds, radiansToDegrees(startAngle), sweep);
+ this.addPath(temp, true);
+ temp.delete();
+ return this;
+ };
+
+ CanvasKit.SkPath.prototype.arcTo = function() {
+ // takes 4, 5 or 7 args
+ // - 5 x1, y1, x2, y2, radius
+ // - 4 oval (as Rect), startAngle, sweepAngle, forceMoveTo
+ // - 7 x1, y1, x2, y2, startAngle, sweepAngle, forceMoveTo
+ var args = arguments;
+ if (args.length === 5) {
+ this._arcTo(args[0], args[1], args[2], args[3], args[4]);
+ } else if (args.length === 4) {
+ this._arcTo(args[0], args[1], args[2], args[3]);
+ } else if (args.length === 7) {
+ this._arcTo(CanvasKit.LTRBRect(args[0], args[1], args[2], args[3]),
+ args[4], args[5], args[6]);
+ } else {
+ throw 'Invalid args for arcTo. Expected 4, 5, or 7, got '+ args.length;
+ }
+
+ return this;
+ };
+
+ CanvasKit.SkPath.prototype.close = function() {
+ this._close();
+ return this;
+ };
+
+ CanvasKit.SkPath.prototype.conicTo = function(x1, y1, x2, y2, w) {
+ this._conicTo(x1, y1, x2, y2, w);
+ return this;
+ };
+
+ CanvasKit.SkPath.prototype.cubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) {
+ this._cubicTo(cp1x, cp1y, cp2x, cp2y, x, y);
+ return this;
+ };
+
+ CanvasKit.SkPath.prototype.dash = function(on, off, phase) {
+ if (this._dash(on, off, phase)) {
return this;
- };
+ }
+ return null;
+ };
- CanvasKit.SkPath.prototype.simplify = function() {
- if (this._simplify()) {
- return this;
- }
- return null;
- };
+ CanvasKit.SkPath.prototype.lineTo = function(x, y) {
+ this._lineTo(x, y);
+ return this;
+ };
- CanvasKit.SkPath.prototype.stroke = function(opts) {
- // Fill out any missing values with the default values.
- /**
- * See externs.js for this definition
- * @type {StrokeOpts}
- */
- opts = opts || {};
- opts.width = opts.width || 1;
- opts.miter_limit = opts.miter_limit || 4;
- opts.cap = opts.cap || CanvasKit.StrokeCap.Butt;
- opts.join = opts.join || CanvasKit.StrokeJoin.Miter;
- opts.precision = opts.precision || 1;
- if (this._stroke(opts)) {
- return this;
- }
- return null;
- };
+ CanvasKit.SkPath.prototype.moveTo = function(x, y) {
+ this._moveTo(x, y);
+ return this;
+ };
- CanvasKit.SkPath.prototype.transform = function() {
- // Takes 1 or 9 args
- if (arguments.length === 1) {
- // argument 1 should be a 6 or 9 element array.
- var a = arguments[0];
- this._transform(a[0], a[1], a[2],
- a[3], a[4], a[5],
- a[6] || 0, a[7] || 0, a[8] || 1);
- } else if (arguments.length === 6 || arguments.length === 9) {
- // these arguments are the 6 or 9 members of the matrix
- var a = arguments;
- this._transform(a[0], a[1], a[2],
- a[3], a[4], a[5],
- a[6] || 0, a[7] || 0, a[8] || 1);
- } else {
- throw 'transform expected to take 1 or 9 arguments. Got ' + arguments.length;
- }
+ CanvasKit.SkPath.prototype.op = function(otherPath, op) {
+ if (this._op(otherPath, op)) {
return this;
- };
- // isComplement is optional, defaults to false
- CanvasKit.SkPath.prototype.trim = function(startT, stopT, isComplement) {
- if (this._trim(startT, stopT, !!isComplement)) {
- return this;
- }
- return null;
- };
+ }
+ return null;
+ };
- // bones should be a 3d array.
- // Each bone is a 3x2 transformation matrix in column major order:
- // | scaleX skewX transX |
- // | skewY scaleY transY |
- // and bones is an array of those matrices.
- // Returns a copy of this (SkVertices) with the bones applied.
- CanvasKit.SkVertices.prototype.applyBones = function(bones) {
- var bPtr = copy3dArray(bones, CanvasKit.HEAPF32);
- var vert = this._applyBones(bPtr, bones.length);
- CanvasKit._free(bPtr);
- return vert;
+ CanvasKit.SkPath.prototype.quadTo = function(cpx, cpy, x, y) {
+ this._quadTo(cpx, cpy, x, y);
+ return this;
+ };
+
+ CanvasKit.SkPath.prototype.simplify = function() {
+ if (this._simplify()) {
+ return this;
+ }
+ return null;
+ };
+
+ CanvasKit.SkPath.prototype.stroke = function(opts) {
+ // Fill out any missing values with the default values.
+ /**
+ * See externs.js for this definition
+ * @type {StrokeOpts}
+ */
+ opts = opts || {};
+ opts.width = opts.width || 1;
+ opts.miter_limit = opts.miter_limit || 4;
+ opts.cap = opts.cap || CanvasKit.StrokeCap.Butt;
+ opts.join = opts.join || CanvasKit.StrokeJoin.Miter;
+ opts.precision = opts.precision || 1;
+ if (this._stroke(opts)) {
+ return this;
+ }
+ return null;
+ };
+
+ CanvasKit.SkPath.prototype.transform = function() {
+ // Takes 1 or 9 args
+ if (arguments.length === 1) {
+ // argument 1 should be a 6 or 9 element array.
+ var a = arguments[0];
+ this._transform(a[0], a[1], a[2],
+ a[3], a[4], a[5],
+ a[6] || 0, a[7] || 0, a[8] || 1);
+ } else if (arguments.length === 6 || arguments.length === 9) {
+ // these arguments are the 6 or 9 members of the matrix
+ var a = arguments;
+ this._transform(a[0], a[1], a[2],
+ a[3], a[4], a[5],
+ a[6] || 0, a[7] || 0, a[8] || 1);
+ } else {
+ throw 'transform expected to take 1 or 9 arguments. Got ' + arguments.length;
+ }
+ return this;
+ };
+ // isComplement is optional, defaults to false
+ CanvasKit.SkPath.prototype.trim = function(startT, stopT, isComplement) {
+ if (this._trim(startT, stopT, !!isComplement)) {
+ return this;
+ }
+ return null;
+ };
+
+ // bones should be a 3d array.
+ // Each bone is a 3x2 transformation matrix in column major order:
+ // | scaleX skewX transX |
+ // | skewY scaleY transY |
+ // and bones is an array of those matrices.
+ // Returns a copy of this (SkVertices) with the bones applied.
+ CanvasKit.SkVertices.prototype.applyBones = function(bones) {
+ var bPtr = copy3dArray(bones, CanvasKit.HEAPF32);
+ var vert = this._applyBones(bPtr, bones.length);
+ CanvasKit._free(bPtr);
+ return vert;
+ }
+
+ CanvasKit.SkImage.prototype.encodeToData = function() {
+ if (!arguments.length) {
+ return this._encodeToData();
}
- CanvasKit.SkImage.prototype.encodeToData = function() {
- if (!arguments.length) {
- return this._encodeToData();
- }
-
- if (arguments.length === 2) {
- var a = arguments;
- return this._encodeToDataWithFormat(a[0], a[1]);
- }
-
- throw 'encodeToData expected to take 0 or 2 arguments. Got ' + arguments.length;
+ if (arguments.length === 2) {
+ var a = arguments;
+ return this._encodeToDataWithFormat(a[0], a[1]);
}
- // str can be either a text string or a ShapedText object
- CanvasKit.SkCanvas.prototype.drawText = function(str, x, y, paint, font) {
- if (typeof str === 'string') {
- // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
- // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
- // Add 1 for null terminator
- var strLen = lengthBytesUTF8(str) + 1;
- var strPtr = CanvasKit._malloc(strLen);
+ throw 'encodeToData expected to take 0 or 2 arguments. Got ' + arguments.length;
+ }
- stringToUTF8(str, strPtr, strLen);
- this._drawSimpleText(strPtr, strLen, x, y, font, paint);
- } else {
- this._drawShapedText(str, x, y, paint);
- }
- }
-
- // returns Uint8Array
- CanvasKit.SkCanvas.prototype.readPixels = function(x, y, w, h, alphaType,
- colorType, dstRowBytes) {
- // supply defaults (which are compatible with HTMLCanvas's getImageData)
- alphaType = alphaType || CanvasKit.AlphaType.Unpremul;
- colorType = colorType || CanvasKit.ColorType.RGBA_8888;
- dstRowBytes = dstRowBytes || (4 * w);
-
- var len = h * dstRowBytes
- var pptr = CanvasKit._malloc(len);
- var ok = this._readPixels({
- 'width': w,
- 'height': h,
- 'colorType': colorType,
- 'alphaType': alphaType,
- }, pptr, dstRowBytes, x, y);
- if (!ok) {
- CanvasKit._free(pptr);
- return null;
- }
-
- // The first typed array is just a view into memory. Because we will
- // be free-ing that, we call slice to make a persistent copy.
- var pixels = new Uint8Array(CanvasKit.HEAPU8.buffer, pptr, len).slice();
- CanvasKit._free(pptr);
- return pixels;
- }
-
- // pixels is a TypedArray. No matter the input size, it will be treated as
- // a Uint8Array (essentially, a byte array).
- CanvasKit.SkCanvas.prototype.writePixels = function(pixels, srcWidth, srcHeight,
- destX, destY, alphaType, colorType) {
- if (pixels.byteLength % (srcWidth * srcHeight)) {
- throw 'pixels length must be a multiple of the srcWidth * srcHeight';
- }
- var bytesPerPixel = pixels.byteLength / (srcWidth * srcHeight);
- // supply defaults (which are compatible with HTMLCanvas's putImageData)
- alphaType = alphaType || CanvasKit.AlphaType.Unpremul;
- colorType = colorType || CanvasKit.ColorType.RGBA_8888;
- var srcRowBytes = bytesPerPixel * srcWidth;
-
- var pptr = CanvasKit._malloc(pixels.byteLength);
- CanvasKit.HEAPU8.set(pixels, pptr);
-
- var ok = this._writePixels({
- 'width': srcWidth,
- 'height': srcHeight,
- 'colorType': colorType,
- 'alphaType': alphaType,
- }, pptr, srcRowBytes, destX, destY);
-
- CanvasKit._free(pptr);
- return ok;
- }
-
- // fontData should be an arrayBuffer
- CanvasKit.SkFontMgr.prototype.MakeTypefaceFromData = function(fontData) {
- var data = new Uint8Array(fontData);
-
- var fptr = CanvasKit._malloc(data.byteLength);
- CanvasKit.HEAPU8.set(data, fptr);
- var font = this._makeTypefaceFromData(fptr, data.byteLength);
- if (!font) {
- SkDebug('Could not decode font data');
- // We do not need to free the data since the C++ will do that for us
- // when the font is deleted (or fails to decode);
- return null;
- }
- return font;
- }
-
- CanvasKit.SkTextBlob.MakeFromText = function(str, font) {
+ // str can be either a text string or a ShapedText object
+ CanvasKit.SkCanvas.prototype.drawText = function(str, x, y, paint, font) {
+ if (typeof str === 'string') {
// lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
// JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
// Add 1 for null terminator
var strLen = lengthBytesUTF8(str) + 1;
var strPtr = CanvasKit._malloc(strLen);
- // Add 1 for the null terminator.
+
stringToUTF8(str, strPtr, strLen);
-
- var blob = CanvasKit.SkTextBlob._MakeFromText(strPtr, strLen - 1, font, CanvasKit.TextEncoding.UTF8);
- if (!blob) {
- SkDebug('Could not make textblob from string "' + str + '"');
- return null;
- }
-
- var origDelete = blob.delete.bind(blob);
- blob.delete = function() {
- CanvasKit._free(strPtr);
- origDelete();
- }
- return blob;
- }
-
- // Run through the JS files that are added at compile time.
- if (CanvasKit._extraInitializations) {
- CanvasKit._extraInitializations.forEach(function(init) {
- init();
- });
- }
- } // end CanvasKit.onRuntimeInitialized, that is, anything changing prototypes or dynamic.
-
- CanvasKit.LTRBRect = function(l, t, r, b) {
- return {
- fLeft: l,
- fTop: t,
- fRight: r,
- fBottom: b,
- };
- }
-
- CanvasKit.XYWHRect = function(x, y, w, h) {
- return {
- fLeft: x,
- fTop: y,
- fRight: x+w,
- fBottom: y+h,
- };
- }
-
- var nullptr = 0; // emscripten doesn't like to take null as uintptr_t
-
- // arr can be a normal JS array or a TypedArray
- // dest is something like CanvasKit.HEAPF32
- function copy1dArray(arr, dest) {
- if (!arr || !arr.length) {
- return nullptr;
- }
- var ptr = CanvasKit._malloc(arr.length * dest.BYTES_PER_ELEMENT);
- // In c++ terms, the WASM heap is a uint8_t*, a long buffer/array of single
- // byte elements. When we run _malloc, we always get an offset/pointer into
- // that block of memory.
- // CanvasKit exposes some different views to make it easier to work with
- // different types. HEAPF32 for example, exposes it as a float*
- // However, to make the ptr line up, we have to do some pointer arithmetic.
- // Concretely, we need to convert ptr to go from an index into a 1-byte-wide
- // buffer to an index into a 4-byte-wide buffer (in the case of HEAPF32)
- // and thus we divide ptr by 4.
- dest.set(arr, ptr / dest.BYTES_PER_ELEMENT);
- return ptr;
- }
-
- // arr should be a non-jagged 2d JS array (TypeyArrays can't be nested
- // inside themselves.)
- // dest is something like CanvasKit.HEAPF32
- function copy2dArray(arr, dest) {
- if (!arr || !arr.length) {
- return nullptr;
- }
- var ptr = CanvasKit._malloc(arr.length * arr[0].length * dest.BYTES_PER_ELEMENT);
- var idx = 0;
- var adjustedPtr = ptr / dest.BYTES_PER_ELEMENT;
- for (var r = 0; r < arr.length; r++) {
- for (var c = 0; c < arr[0].length; c++) {
- dest[adjustedPtr + idx] = arr[r][c];
- idx++;
- }
- }
- return ptr;
- }
-
- // arr should be a non-jagged 3d JS array (TypeyArrays can't be nested
- // inside themselves.)
- // dest is something like CanvasKit.HEAPF32
- function copy3dArray(arr, dest) {
- if (!arr || !arr.length || !arr[0].length) {
- return nullptr;
- }
- var ptr = CanvasKit._malloc(arr.length * arr[0].length * arr[0][0].length * dest.BYTES_PER_ELEMENT);
- var idx = 0;
- var adjustedPtr = ptr / dest.BYTES_PER_ELEMENT;
- for (var x = 0; x < arr.length; x++) {
- for (var y = 0; y < arr[0].length; y++) {
- for (var z = 0; z < arr[0][0].length; z++) {
- dest[adjustedPtr + idx] = arr[x][y][z];
- idx++;
- }
- }
- }
- return ptr;
- }
-
- // Caching the Float32Arrays can save having to reallocate them
- // over and over again.
- var Float32ArrayCache = {};
-
- // Takes a 2D array of commands and puts them into the WASM heap
- // as a 1D array. This allows them to referenced from the C++ code.
- // Returns a 2 element array, with the first item being essentially a
- // pointer to the array and the second item being the length of
- // the new 1D array.
- //
- // Example usage:
- // let cmds = [
- // [CanvasKit.MOVE_VERB, 0, 10],
- // [CanvasKit.LINE_VERB, 30, 40],
- // [CanvasKit.QUAD_VERB, 20, 50, 45, 60],
- // ];
- function loadCmdsTypedArray(arr) {
- var len = 0;
- for (var r = 0; r < arr.length; r++) {
- len += arr[r].length;
- }
-
- var ta;
- if (Float32ArrayCache[len]) {
- ta = Float32ArrayCache[len];
+ this._drawSimpleText(strPtr, strLen, x, y, font, paint);
} else {
- ta = new Float32Array(len);
- Float32ArrayCache[len] = ta;
- }
- // Flatten into a 1d array
- var i = 0;
- for (var r = 0; r < arr.length; r++) {
- for (var c = 0; c < arr[r].length; c++) {
- var item = arr[r][c];
- ta[i] = item;
- i++;
- }
- }
-
- var ptr = copy1dArray(ta, CanvasKit.HEAPF32);
- return [ptr, len];
- }
-
- CanvasKit.MakePathFromCmds = function(cmds) {
- var ptrLen = loadCmdsTypedArray(cmds);
- var path = CanvasKit._MakePathFromCmds(ptrLen[0], ptrLen[1]);
- CanvasKit._free(ptrLen[0]);
- return path;
- }
-
- CanvasKit.MakeSkDashPathEffect = function(intervals, phase) {
- if (!phase) {
- phase = 0;
- }
- if (!intervals.length || intervals.length % 2 === 1) {
- throw 'Intervals array must have even length';
- }
- var ptr = copy1dArray(intervals, CanvasKit.HEAPF32);
- var dpe = CanvasKit._MakeSkDashPathEffect(ptr, intervals.length, phase);
- CanvasKit._free(ptr);
- return dpe;
- }
-
- // data is a TypedArray or ArrayBuffer e.g. from fetch().then(resp.arrayBuffer())
- CanvasKit.MakeImageFromEncoded = function(data) {
- data = new Uint8Array(data);
-
- var iptr = CanvasKit._malloc(data.byteLength);
- CanvasKit.HEAPU8.set(data, iptr);
- var img = CanvasKit._decodeImage(iptr, data.byteLength);
- if (!img) {
- SkDebug('Could not decode image');
- CanvasKit._free(iptr);
- return null;
- }
- var realDelete = img.delete.bind(img);
- img.delete = function() {
- CanvasKit._free(iptr);
- realDelete();
- }
- return img;
- }
-
- // imgData is an Encoded SkImage, e.g. from MakeImageFromEncoded
- CanvasKit.MakeImageShader = function(img, xTileMode, yTileMode, clampUnpremul, localMatrix) {
- if (!img) {
- return null;
- }
- clampUnpremul = clampUnpremul || false;
- if (localMatrix) {
- // Add perspective args if not provided.
- if (localMatrix.length === 6) {
- localMatrix.push(0, 0, 1);
- }
- return CanvasKit._MakeImageShader(img, xTileMode, yTileMode, clampUnpremul, localMatrix);
- } else {
- return CanvasKit._MakeImageShader(img, xTileMode, yTileMode, clampUnpremul);
+ this._drawShapedText(str, x, y, paint);
}
}
- // pixels is a Uint8Array
- CanvasKit.MakeImage = function(pixels, width, height, alphaType, colorType) {
- var bytesPerPixel = pixels.byteLength / (width * height);
- var info = {
- 'width': width,
- 'height': height,
- 'alphaType': alphaType,
+ // returns Uint8Array
+ CanvasKit.SkCanvas.prototype.readPixels = function(x, y, w, h, alphaType,
+ colorType, dstRowBytes) {
+ // supply defaults (which are compatible with HTMLCanvas's getImageData)
+ alphaType = alphaType || CanvasKit.AlphaType.Unpremul;
+ colorType = colorType || CanvasKit.ColorType.RGBA_8888;
+ dstRowBytes = dstRowBytes || (4 * w);
+
+ var len = h * dstRowBytes
+ var pptr = CanvasKit._malloc(len);
+ var ok = this._readPixels({
+ 'width': w,
+ 'height': h,
'colorType': colorType,
- };
+ 'alphaType': alphaType,
+ }, pptr, dstRowBytes, x, y);
+ if (!ok) {
+ CanvasKit._free(pptr);
+ return null;
+ }
+
+ // The first typed array is just a view into memory. Because we will
+ // be free-ing that, we call slice to make a persistent copy.
+ var pixels = new Uint8Array(CanvasKit.HEAPU8.buffer, pptr, len).slice();
+ CanvasKit._free(pptr);
+ return pixels;
+ }
+
+ // pixels is a TypedArray. No matter the input size, it will be treated as
+ // a Uint8Array (essentially, a byte array).
+ CanvasKit.SkCanvas.prototype.writePixels = function(pixels, srcWidth, srcHeight,
+ destX, destY, alphaType, colorType) {
+ if (pixels.byteLength % (srcWidth * srcHeight)) {
+ throw 'pixels length must be a multiple of the srcWidth * srcHeight';
+ }
+ var bytesPerPixel = pixels.byteLength / (srcWidth * srcHeight);
+ // supply defaults (which are compatible with HTMLCanvas's putImageData)
+ alphaType = alphaType || CanvasKit.AlphaType.Unpremul;
+ colorType = colorType || CanvasKit.ColorType.RGBA_8888;
+ var srcRowBytes = bytesPerPixel * srcWidth;
+
var pptr = CanvasKit._malloc(pixels.byteLength);
CanvasKit.HEAPU8.set(pixels, pptr);
- // No need to _free iptr, Image takes it with SkData::MakeFromMalloc
- return CanvasKit._MakeImage(info, pptr, pixels.byteLength, width * bytesPerPixel);
+ var ok = this._writePixels({
+ 'width': srcWidth,
+ 'height': srcHeight,
+ 'colorType': colorType,
+ 'alphaType': alphaType,
+ }, pptr, srcRowBytes, destX, destY);
+
+ CanvasKit._free(pptr);
+ return ok;
}
- CanvasKit.MakeLinearGradientShader = function(start, end, colors, pos, mode, localMatrix, flags) {
- var colorPtr = copy1dArray(colors, CanvasKit.HEAP32);
- var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
- flags = flags || 0;
+ // fontData should be an arrayBuffer
+ CanvasKit.SkFontMgr.prototype.MakeTypefaceFromData = function(fontData) {
+ var data = new Uint8Array(fontData);
- if (localMatrix) {
- // Add perspective args if not provided.
- if (localMatrix.length === 6) {
- localMatrix.push(0, 0, 1);
- }
- var lgs = CanvasKit._MakeLinearGradientShader(start, end, colorPtr, posPtr,
- colors.length, mode, flags, localMatrix);
- } else {
- var lgs = CanvasKit._MakeLinearGradientShader(start, end, colorPtr, posPtr,
- colors.length, mode, flags);
+ var fptr = CanvasKit._malloc(data.byteLength);
+ CanvasKit.HEAPU8.set(data, fptr);
+ var font = this._makeTypefaceFromData(fptr, data.byteLength);
+ if (!font) {
+ SkDebug('Could not decode font data');
+ // We do not need to free the data since the C++ will do that for us
+ // when the font is deleted (or fails to decode);
+ return null;
+ }
+ return font;
+ }
+
+ CanvasKit.SkTextBlob.MakeFromText = function(str, font) {
+ // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
+ // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
+ // Add 1 for null terminator
+ var strLen = lengthBytesUTF8(str) + 1;
+ var strPtr = CanvasKit._malloc(strLen);
+ // Add 1 for the null terminator.
+ stringToUTF8(str, strPtr, strLen);
+
+ var blob = CanvasKit.SkTextBlob._MakeFromText(strPtr, strLen - 1, font, CanvasKit.TextEncoding.UTF8);
+ if (!blob) {
+ SkDebug('Could not make textblob from string "' + str + '"');
+ return null;
}
- CanvasKit._free(colorPtr);
- CanvasKit._free(posPtr);
- return lgs;
- }
-
- CanvasKit.MakeRadialGradientShader = function(center, radius, colors, pos, mode, localMatrix, flags) {
- var colorPtr = copy1dArray(colors, CanvasKit.HEAP32);
- var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
- flags = flags || 0;
-
- if (localMatrix) {
- // Add perspective args if not provided.
- if (localMatrix.length === 6) {
- localMatrix.push(0, 0, 1);
- }
- var rgs = CanvasKit._MakeRadialGradientShader(center, radius, colorPtr, posPtr,
- colors.length, mode, flags, localMatrix);
- } else {
- var rgs = CanvasKit._MakeRadialGradientShader(center, radius, colorPtr, posPtr,
- colors.length, mode, flags);
+ var origDelete = blob.delete.bind(blob);
+ blob.delete = function() {
+ CanvasKit._free(strPtr);
+ origDelete();
}
-
- CanvasKit._free(colorPtr);
- CanvasKit._free(posPtr);
- return rgs;
+ return blob;
}
- CanvasKit.MakeTwoPointConicalGradientShader = function(start, startRadius, end, endRadius,
- colors, pos, mode, localMatrix, flags) {
- var colorPtr = copy1dArray(colors, CanvasKit.HEAP32);
- var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
- flags = flags || 0;
-
- if (localMatrix) {
- // Add perspective args if not provided.
- if (localMatrix.length === 6) {
- localMatrix.push(0, 0, 1);
- }
- var rgs = CanvasKit._MakeTwoPointConicalGradientShader(
- start, startRadius, end, endRadius,
- colorPtr, posPtr, colors.length, mode, flags, localMatrix);
- } else {
- var rgs = CanvasKit._MakeTwoPointConicalGradientShader(
- start, startRadius, end, endRadius,
- colorPtr, posPtr, colors.length, mode, flags);
- }
-
- CanvasKit._free(colorPtr);
- CanvasKit._free(posPtr);
- return rgs;
+ // Run through the JS files that are added at compile time.
+ if (CanvasKit._extraInitializations) {
+ CanvasKit._extraInitializations.forEach(function(init) {
+ init();
+ });
}
+}; // end CanvasKit.onRuntimeInitialized, that is, anything changing prototypes or dynamic.
- CanvasKit.MakeSkVertices = function(mode, positions, textureCoordinates, colors,
- boneIndices, boneWeights, indices) {
- var positionPtr = copy2dArray(positions, CanvasKit.HEAPF32);
- var texPtr = copy2dArray(textureCoordinates, CanvasKit.HEAPF32);
- // Since we write the colors to memory as signed integers (JSColor), we can
- // read them out on the other side as unsigned ints (SkColor) just fine
- // - it's effectively casting.
- var colorPtr = copy1dArray(colors, CanvasKit.HEAP32);
-
- var boneIdxPtr = copy2dArray(boneIndices, CanvasKit.HEAP32);
- var boneWtPtr = copy2dArray(boneWeights, CanvasKit.HEAPF32);
- var idxPtr = copy1dArray(indices, CanvasKit.HEAPU16);
-
- var idxCount = (indices && indices.length) || 0;
- // _MakeVertices will copy all the values in, so we are free to release
- // the memory after.
- var vertices = CanvasKit._MakeSkVertices(mode, positions.length, positionPtr,
- texPtr, colorPtr, boneIdxPtr, boneWtPtr,
- idxCount, idxPtr);
- positionPtr && CanvasKit._free(positionPtr);
- texPtr && CanvasKit._free(texPtr);
- colorPtr && CanvasKit._free(colorPtr);
- idxPtr && CanvasKit._free(idxPtr);
- boneIdxPtr && CanvasKit._free(boneIdxPtr);
- boneWtPtr && CanvasKit._free(boneWtPtr);
- return vertices;
- }
-
-}(Module)); // When this file is loaded in, the high level object is "Module";
-
-// Intentionally added outside the scope to allow usage in canvas2d.js and other
-// pre-js files. These names are unlikely to cause emscripten collisions.
-function radiansToDegrees(rad) {
- return (rad / Math.PI) * 180;
+CanvasKit.LTRBRect = function(l, t, r, b) {
+ return {
+ fLeft: l,
+ fTop: t,
+ fRight: r,
+ fBottom: b,
+ };
}
-function degreesToRadians(deg) {
- return (deg / 180) * Math.PI;
+CanvasKit.XYWHRect = function(x, y, w, h) {
+ return {
+ fLeft: x,
+ fTop: y,
+ fRight: x+w,
+ fBottom: y+h,
+ };
}
+CanvasKit.MakePathFromCmds = function(cmds) {
+ var ptrLen = loadCmdsTypedArray(cmds);
+ var path = CanvasKit._MakePathFromCmds(ptrLen[0], ptrLen[1]);
+ CanvasKit._free(ptrLen[0]);
+ return path;
+}
+
+CanvasKit.MakeSkDashPathEffect = function(intervals, phase) {
+ if (!phase) {
+ phase = 0;
+ }
+ if (!intervals.length || intervals.length % 2 === 1) {
+ throw 'Intervals array must have even length';
+ }
+ var ptr = copy1dArray(intervals, CanvasKit.HEAPF32);
+ var dpe = CanvasKit._MakeSkDashPathEffect(ptr, intervals.length, phase);
+ CanvasKit._free(ptr);
+ return dpe;
+}
+
+// data is a TypedArray or ArrayBuffer e.g. from fetch().then(resp.arrayBuffer())
+CanvasKit.MakeImageFromEncoded = function(data) {
+ data = new Uint8Array(data);
+
+ var iptr = CanvasKit._malloc(data.byteLength);
+ CanvasKit.HEAPU8.set(data, iptr);
+ var img = CanvasKit._decodeImage(iptr, data.byteLength);
+ if (!img) {
+ SkDebug('Could not decode image');
+ CanvasKit._free(iptr);
+ return null;
+ }
+ var realDelete = img.delete.bind(img);
+ img.delete = function() {
+ CanvasKit._free(iptr);
+ realDelete();
+ }
+ return img;
+}
+
+// imgData is an Encoded SkImage, e.g. from MakeImageFromEncoded
+CanvasKit.MakeImageShader = function(img, xTileMode, yTileMode, clampUnpremul, localMatrix) {
+ if (!img) {
+ return null;
+ }
+ clampUnpremul = clampUnpremul || false;
+ if (localMatrix) {
+ // Add perspective args if not provided.
+ if (localMatrix.length === 6) {
+ localMatrix.push(0, 0, 1);
+ }
+ return CanvasKit._MakeImageShader(img, xTileMode, yTileMode, clampUnpremul, localMatrix);
+ } else {
+ return CanvasKit._MakeImageShader(img, xTileMode, yTileMode, clampUnpremul);
+ }
+}
+
+// pixels is a Uint8Array
+CanvasKit.MakeImage = function(pixels, width, height, alphaType, colorType) {
+ var bytesPerPixel = pixels.byteLength / (width * height);
+ var info = {
+ 'width': width,
+ 'height': height,
+ 'alphaType': alphaType,
+ 'colorType': colorType,
+ };
+ var pptr = CanvasKit._malloc(pixels.byteLength);
+ CanvasKit.HEAPU8.set(pixels, pptr);
+ // No need to _free iptr, Image takes it with SkData::MakeFromMalloc
+
+ return CanvasKit._MakeImage(info, pptr, pixels.byteLength, width * bytesPerPixel);
+}
+
+CanvasKit.MakeLinearGradientShader = function(start, end, colors, pos, mode, localMatrix, flags) {
+ var colorPtr = copy1dArray(colors, CanvasKit.HEAP32);
+ var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
+ flags = flags || 0;
+
+ if (localMatrix) {
+ // Add perspective args if not provided.
+ if (localMatrix.length === 6) {
+ localMatrix.push(0, 0, 1);
+ }
+ var lgs = CanvasKit._MakeLinearGradientShader(start, end, colorPtr, posPtr,
+ colors.length, mode, flags, localMatrix);
+ } else {
+ var lgs = CanvasKit._MakeLinearGradientShader(start, end, colorPtr, posPtr,
+ colors.length, mode, flags);
+ }
+
+ CanvasKit._free(colorPtr);
+ CanvasKit._free(posPtr);
+ return lgs;
+}
+
+CanvasKit.MakeRadialGradientShader = function(center, radius, colors, pos, mode, localMatrix, flags) {
+ var colorPtr = copy1dArray(colors, CanvasKit.HEAP32);
+ var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
+ flags = flags || 0;
+
+ if (localMatrix) {
+ // Add perspective args if not provided.
+ if (localMatrix.length === 6) {
+ localMatrix.push(0, 0, 1);
+ }
+ var rgs = CanvasKit._MakeRadialGradientShader(center, radius, colorPtr, posPtr,
+ colors.length, mode, flags, localMatrix);
+ } else {
+ var rgs = CanvasKit._MakeRadialGradientShader(center, radius, colorPtr, posPtr,
+ colors.length, mode, flags);
+ }
+
+ CanvasKit._free(colorPtr);
+ CanvasKit._free(posPtr);
+ return rgs;
+}
+
+CanvasKit.MakeTwoPointConicalGradientShader = function(start, startRadius, end, endRadius,
+ colors, pos, mode, localMatrix, flags) {
+ var colorPtr = copy1dArray(colors, CanvasKit.HEAP32);
+ var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
+ flags = flags || 0;
+
+ if (localMatrix) {
+ // Add perspective args if not provided.
+ if (localMatrix.length === 6) {
+ localMatrix.push(0, 0, 1);
+ }
+ var rgs = CanvasKit._MakeTwoPointConicalGradientShader(
+ start, startRadius, end, endRadius,
+ colorPtr, posPtr, colors.length, mode, flags, localMatrix);
+ } else {
+ var rgs = CanvasKit._MakeTwoPointConicalGradientShader(
+ start, startRadius, end, endRadius,
+ colorPtr, posPtr, colors.length, mode, flags);
+ }
+
+ CanvasKit._free(colorPtr);
+ CanvasKit._free(posPtr);
+ return rgs;
+}
+
+CanvasKit.MakeSkVertices = function(mode, positions, textureCoordinates, colors,
+ boneIndices, boneWeights, indices) {
+ var positionPtr = copy2dArray(positions, CanvasKit.HEAPF32);
+ var texPtr = copy2dArray(textureCoordinates, CanvasKit.HEAPF32);
+ // Since we write the colors to memory as signed integers (JSColor), we can
+ // read them out on the other side as unsigned ints (SkColor) just fine
+ // - it's effectively casting.
+ var colorPtr = copy1dArray(colors, CanvasKit.HEAP32);
+
+ var boneIdxPtr = copy2dArray(boneIndices, CanvasKit.HEAP32);
+ var boneWtPtr = copy2dArray(boneWeights, CanvasKit.HEAPF32);
+ var idxPtr = copy1dArray(indices, CanvasKit.HEAPU16);
+
+ var idxCount = (indices && indices.length) || 0;
+ // _MakeVertices will copy all the values in, so we are free to release
+ // the memory after.
+ var vertices = CanvasKit._MakeSkVertices(mode, positions.length, positionPtr,
+ texPtr, colorPtr, boneIdxPtr, boneWtPtr,
+ idxCount, idxPtr);
+ positionPtr && CanvasKit._free(positionPtr);
+ texPtr && CanvasKit._free(texPtr);
+ colorPtr && CanvasKit._free(colorPtr);
+ idxPtr && CanvasKit._free(idxPtr);
+ boneIdxPtr && CanvasKit._free(boneIdxPtr);
+ boneWtPtr && CanvasKit._free(boneWtPtr);
+ return vertices;
+};
\ No newline at end of file
diff --git a/experimental/canvaskit/postamble.js b/experimental/canvaskit/postamble.js
new file mode 100644
index 0000000..592e6c5
--- /dev/null
+++ b/experimental/canvaskit/postamble.js
@@ -0,0 +1,2 @@
+// This closes the scope started in preamble.js
+}(Module)); // When this file is loaded in, the high level object is "Module";
diff --git a/experimental/canvaskit/preamble.js b/experimental/canvaskit/preamble.js
new file mode 100644
index 0000000..c6fd434
--- /dev/null
+++ b/experimental/canvaskit/preamble.js
@@ -0,0 +1,9 @@
+// Adds compile-time JS functions to augment the CanvasKit interface.
+(function(CanvasKit) {
+
+
+// This intentionally dangles because we want all the
+// JS code to be in the same scope, but JS doesn't support
+// namespaces like C++ does. Thus, we simply include this
+// preamble.js file, all the source .js files and then postamble.js
+// to bundle everything in the same scope.
\ No newline at end of file
diff --git a/experimental/canvaskit/skottie.js b/experimental/canvaskit/skottie.js
new file mode 100644
index 0000000..a5ac1ff
--- /dev/null
+++ b/experimental/canvaskit/skottie.js
@@ -0,0 +1,54 @@
+// Adds compile-time JS functions to augment the CanvasKit interface.
+// Specifically, anything that should only be on the Skottie builds of canvaskit.
+
+
+CanvasKit.MakeManagedAnimation = function(json, imgs, fonts) {
+ if (!CanvasKit._MakeManagedAnimation) {
+ throw 'Not compiled with MakeManagedAnimation';
+ }
+ if (!imgs && !fonts) {
+ return CanvasKit._MakeManagedAnimation(json, 0, nullptr, nullptr, nullptr,
+ 0, nullptr, nullptr, nullptr);
+ }
+ var imgNamePtrs = [];
+ var imgDataPtrs = [];
+ var imgSizes = [];
+
+ var keys = Object.keys(imgs);
+ for (var i = 0; i < keys.length; i++) {
+ var key = keys[i];
+ var buffer = imgs[key];
+ var data = new Uint8Array(buffer);
+
+ var iptr = CanvasKit._malloc(data.byteLength);
+ CanvasKit.HEAPU8.set(data, iptr);
+ imgDataPtrs.push(iptr);
+ imgSizes.push(data.byteLength);
+
+ // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
+ // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
+ // Add 1 for null terminator
+ var strLen = lengthBytesUTF8(key) + 1;
+ var strPtr = CanvasKit._malloc(strLen);
+
+ stringToUTF8(key, strPtr, strLen);
+ imgNamePtrs.push(strPtr);
+ }
+
+ // Not entirely sure if it matters, but the uintptr_t are 32 bits
+ // we want to copy our array of uintptr_t into the right size memory.
+ var namesPtr = copy1dArray(imgNamePtrs, CanvasKit.HEAPU32);
+ var imgsPtr = copy1dArray(imgDataPtrs, CanvasKit.HEAPU32);
+ var imgSizesPtr = copy1dArray(imgSizes, CanvasKit.HEAPU32);
+
+ var anim = CanvasKit._MakeManagedAnimation(json, keys.length, namesPtr, imgsPtr, imgSizesPtr,
+ 0, nullptr, nullptr, nullptr);
+
+ // We leave the image data arrays and string data live and assume
+ // it is now owned by the C++ code
+ CanvasKit._free(namesPtr);
+ CanvasKit._free(imgsPtr);
+ CanvasKit._free(imgSizesPtr);
+
+ return anim;
+};
diff --git a/experimental/canvaskit/skottie_bindings.cpp b/experimental/canvaskit/skottie_bindings.cpp
index 66be7e0..67ee4d3 100644
--- a/experimental/canvaskit/skottie_bindings.cpp
+++ b/experimental/canvaskit/skottie_bindings.cpp
@@ -6,11 +6,14 @@
*/
#include "SkCanvas.h"
+#include "SkImage.h"
#include "SkMakeUnique.h"
#include "SkTypes.h"
+#include "SkString.h"
#include "Skottie.h"
#include <string>
+#include <vector>
#include <emscripten.h>
#include <emscripten/bind.h>
@@ -26,13 +29,57 @@
#if SK_INCLUDE_MANAGED_SKOTTIE
namespace {
+class SkottieAssetProvider : public skottie::ResourceProvider {
+public:
+ ~SkottieAssetProvider() override = default;
+
+ static sk_sp<SkottieAssetProvider> Make() {
+ return sk_sp<SkottieAssetProvider>(new SkottieAssetProvider());
+ }
+
+ void addImage(const SkString& name, sk_sp<SkData> img) {
+ sk_sp<skottie_utils::MultiFrameImageAsset> asset =
+ skottie_utils::MultiFrameImageAsset::Make(std::move(img));
+ fImgs.emplace_back(std::make_pair(name, std::move(asset)));
+ }
+
+ void addFont(const SkString& name, sk_sp<SkData> fontData) {
+ // TODO(kjlubick)
+ }
+
+ sk_sp<skottie::ImageAsset> loadImageAsset(const char path[],
+ const char name[]) const override {
+ auto combined = SkString(path);
+ combined.append(name);
+
+ for(int i = 0; i < fImgs.size(); i++) {
+ if (fImgs[i].first == combined) {
+ return fImgs[i].second;
+ }
+ }
+ SkDebugf("Could not find %s\n", combined.c_str());
+ return nullptr;
+ }
+
+private:
+ SkottieAssetProvider() = default;
+
+ // Tried using a map, but that gave strange errors like
+ // https://emscripten.org/docs/porting/guidelines/function_pointer_issues.html
+ // Not entirely sure why, but perhaps the iterator in the map was
+ // confusing enscripten.
+ std::vector<std::pair<SkString, sk_sp<skottie_utils::MultiFrameImageAsset>>> fImgs;
+
+};
+
class ManagedAnimation final : public SkRefCnt {
public:
- static sk_sp<ManagedAnimation> Make(const std::string& json) {
+ static sk_sp<ManagedAnimation> Make(const std::string& json, sk_sp<SkottieAssetProvider> ap) {
auto mgr = skstd::make_unique<skottie_utils::CustomPropertyManager>();
auto animation = skottie::Animation::Builder()
.setMarkerObserver(mgr->getMarkerObserver())
.setPropertyObserver(mgr->getPropertyObserver())
+ .setResourceProvider(ap)
.make(json.c_str(), json.size());
return animation
@@ -40,6 +87,8 @@
: nullptr;
}
+ ~ManagedAnimation() override = default;
+
// skottie::Animation API
void render(SkCanvas* canvas) const { fAnimation->render(canvas, nullptr); }
void render(SkCanvas* canvas, const SkRect& dst) const { fAnimation->render(canvas, &dst); }
@@ -147,7 +196,32 @@
.function("getColorProps" , &ManagedAnimation::getColorProps)
.function("getOpacityProps", &ManagedAnimation::getOpacityProps);
- function("MakeManagedAnimation", &ManagedAnimation::Make);
+ function("_MakeManagedAnimation", optional_override([](std::string json,
+ size_t imageCount,
+ uintptr_t /* char** */ inptr,
+ uintptr_t /* uint8_t** */ idptr,
+ uintptr_t /* size_t* */ isptr,
+ size_t fontCount,
+ uintptr_t /* char** */ fnptr,
+ uintptr_t /* uint8_t** */ fdptr,
+ uintptr_t /* uint8_t* */ fsptr)
+ ->sk_sp<ManagedAnimation> {
+ if (imageCount == 0 && fontCount == 0) {
+ return ManagedAnimation::Make(json, nullptr);
+ }
+ auto assetProvider = SkottieAssetProvider::Make();
+ char** imgNames = reinterpret_cast<char**>(inptr);
+ uint8_t** imgDatas = reinterpret_cast<uint8_t**>(idptr);
+ size_t* imgSizes = reinterpret_cast<size_t*>(isptr);
+ for (int i = 0; i < imageCount; i++) {
+ SkString name = SkString(imgNames[i]);
+ sk_sp<SkData> bytes = SkData::MakeFromMalloc(imgDatas[i],
+ imgSizes[i]);
+ assetProvider->addImage(name, std::move(bytes));
+ }
+
+ return ManagedAnimation::Make(json, std::move(assetProvider));
+ }));
constant("managed_skottie", true);
#endif // SK_INCLUDE_MANAGED_SKOTTIE
}