blob: e8d363b343738040d7b1376d7e12440ab1e527e3 [file] [log] [blame]
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001// helper JS that could be used anywhere in the glue code
Kevin Lubick217056c2018-09-20 17:39:31 -04002
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05003function clamp(c) {
4 return Math.round(Math.max(0, Math.min(c || 0, 255)));
5}
Kevin Lubick217056c2018-09-20 17:39:31 -04006
Nathaniel Nifonge5d32542020-03-26 09:27:48 -04007// Constructs a Color with the same API as CSS's rgba(), that is
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05008// r,g,b are 0-255, and a is 0.0 to 1.0.
9// if a is omitted, it will be assumed to be 1.0
Nathaniel Nifonge5d32542020-03-26 09:27:48 -040010// Internally, Colors are a TypedArray of four unpremultiplied 32-bit floats: a, r, g, b
11// In order to construct one with more precision or in a wider gamut, use
12// CanvasKit.Color4f
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050013CanvasKit.Color = function(r, g, b, a) {
14 if (a === undefined) {
15 a = 1;
Kevin Lubick217056c2018-09-20 17:39:31 -040016 }
Nathaniel Nifonge5d32542020-03-26 09:27:48 -040017 return CanvasKit.Color4f(clamp(r)/255, clamp(g)/255, clamp(b)/255, a);
Kevin Lubicke7c1a732020-12-04 09:10:39 -050018};
Kevin Lubick217056c2018-09-20 17:39:31 -040019
Kevin Lubick9adc1d42020-06-04 08:25:16 -040020// Constructs a Color as a 32 bit unsigned integer, with 8 bits assigned to each channel.
Kevin Lubick93f1a382020-06-02 16:15:23 -040021// Channels are expected to be between 0 and 255 and will be clamped as such.
22CanvasKit.ColorAsInt = function(r, g, b, a) {
23 // default to opaque
24 if (a === undefined) {
25 a = 255;
26 }
Kevin Lubick9adc1d42020-06-04 08:25:16 -040027 // This is consistent with how Skia represents colors in C++, as an unsigned int.
Kevin Lubick59e087e2020-06-03 12:24:07 -040028 // This is also consistent with how Flutter represents colors:
29 // https://github.com/flutter/engine/blob/243bb59c7179a7e701ce478080d6ce990710ae73/lib/web_ui/lib/src/ui/painting.dart#L50
Kevin Lubick9adc1d42020-06-04 08:25:16 -040030 return (((clamp(a) << 24) | (clamp(r) << 16) | (clamp(g) << 8) | (clamp(b) << 0)
31 & 0xFFFFFFF) // This truncates the unsigned to 32 bits and signals to JS engines they can
32 // represent the number with an int instead of a double.
33 >>> 0); // This makes the value an unsigned int.
Kevin Lubicke7c1a732020-12-04 09:10:39 -050034};
Nathaniel Nifonge5d32542020-03-26 09:27:48 -040035// Construct a 4-float color.
36// Opaque if opacity is omitted.
37CanvasKit.Color4f = function(r, g, b, a) {
38 if (a === undefined) {
39 a = 1;
40 }
41 return Float32Array.of(r, g, b, a);
Kevin Lubicke7c1a732020-12-04 09:10:39 -050042};
Nathaniel Nifonge5d32542020-03-26 09:27:48 -040043
44// Color constants use property getters to prevent other code from accidentally
45// changing them.
Kevin Lubickf8823b52020-09-03 10:02:10 -040046Object.defineProperty(CanvasKit, 'TRANSPARENT', {
Nathaniel Nifonge5d32542020-03-26 09:27:48 -040047 get: function() { return CanvasKit.Color4f(0, 0, 0, 0); }
48});
Kevin Lubickf8823b52020-09-03 10:02:10 -040049Object.defineProperty(CanvasKit, 'BLACK', {
Nathaniel Nifonge5d32542020-03-26 09:27:48 -040050 get: function() { return CanvasKit.Color4f(0, 0, 0, 1); }
51});
Kevin Lubickf8823b52020-09-03 10:02:10 -040052Object.defineProperty(CanvasKit, 'WHITE', {
Nathaniel Nifonge5d32542020-03-26 09:27:48 -040053 get: function() { return CanvasKit.Color4f(1, 1, 1, 1); }
54});
Kevin Lubickf8823b52020-09-03 10:02:10 -040055Object.defineProperty(CanvasKit, 'RED', {
Nathaniel Nifonge5d32542020-03-26 09:27:48 -040056 get: function() { return CanvasKit.Color4f(1, 0, 0, 1); }
57});
Kevin Lubickf8823b52020-09-03 10:02:10 -040058Object.defineProperty(CanvasKit, 'GREEN', {
Nathaniel Nifonge5d32542020-03-26 09:27:48 -040059 get: function() { return CanvasKit.Color4f(0, 1, 0, 1); }
60});
Kevin Lubickf8823b52020-09-03 10:02:10 -040061Object.defineProperty(CanvasKit, 'BLUE', {
Nathaniel Nifonge5d32542020-03-26 09:27:48 -040062 get: function() { return CanvasKit.Color4f(0, 0, 1, 1); }
63});
Kevin Lubickf8823b52020-09-03 10:02:10 -040064Object.defineProperty(CanvasKit, 'YELLOW', {
Nathaniel Nifonge5d32542020-03-26 09:27:48 -040065 get: function() { return CanvasKit.Color4f(1, 1, 0, 1); }
66});
Kevin Lubickf8823b52020-09-03 10:02:10 -040067Object.defineProperty(CanvasKit, 'CYAN', {
Nathaniel Nifonge5d32542020-03-26 09:27:48 -040068 get: function() { return CanvasKit.Color4f(0, 1, 1, 1); }
69});
Kevin Lubickf8823b52020-09-03 10:02:10 -040070Object.defineProperty(CanvasKit, 'MAGENTA', {
Nathaniel Nifonge5d32542020-03-26 09:27:48 -040071 get: function() { return CanvasKit.Color4f(1, 0, 1, 1); }
72});
73
74// returns a css style [r, g, b, a] from a CanvasKit.Color
75// where r, g, b are returned as ints in the range [0, 255]
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050076// where a is scaled between 0 and 1.0
77CanvasKit.getColorComponents = function(color) {
78 return [
Nathaniel Nifonge5d32542020-03-26 09:27:48 -040079 Math.floor(color[0]*255),
80 Math.floor(color[1]*255),
81 Math.floor(color[2]*255),
82 color[3]
83 ];
Kevin Lubicke7c1a732020-12-04 09:10:39 -050084};
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050085
Kevin Lubick39284662020-02-20 10:29:55 -050086// parseColorString takes in a CSS color value and returns a CanvasKit.Color
Nathaniel Nifonge5d32542020-03-26 09:27:48 -040087// (which is an array of 4 floats in RGBA order). An optional colorMap
88// may be provided which maps custom strings to values.
Kevin Lubick39284662020-02-20 10:29:55 -050089// In the CanvasKit canvas2d shim layer, we provide this map for processing
90// canvas2d calls, but not here for code size reasons.
91CanvasKit.parseColorString = function(colorStr, colorMap) {
92 colorStr = colorStr.toLowerCase();
93 // See https://drafts.csswg.org/css-color/#typedef-hex-color
94 if (colorStr.startsWith('#')) {
95 var r, g, b, a = 255;
96 switch (colorStr.length) {
97 case 9: // 8 hex chars #RRGGBBAA
98 a = parseInt(colorStr.slice(7, 9), 16);
99 case 7: // 6 hex chars #RRGGBB
100 r = parseInt(colorStr.slice(1, 3), 16);
101 g = parseInt(colorStr.slice(3, 5), 16);
102 b = parseInt(colorStr.slice(5, 7), 16);
103 break;
104 case 5: // 4 hex chars #RGBA
105 // multiplying by 17 is the same effect as
106 // appending another character of the same value
107 // e.g. e => ee == 14 => 238
108 a = parseInt(colorStr.slice(4, 5), 16) * 17;
109 case 4: // 6 hex chars #RGB
110 r = parseInt(colorStr.slice(1, 2), 16) * 17;
111 g = parseInt(colorStr.slice(2, 3), 16) * 17;
112 b = parseInt(colorStr.slice(3, 4), 16) * 17;
113 break;
114 }
115 return CanvasKit.Color(r, g, b, a/255);
116
117 } else if (colorStr.startsWith('rgba')) {
118 // Trim off rgba( and the closing )
119 colorStr = colorStr.slice(5, -1);
120 var nums = colorStr.split(',');
121 return CanvasKit.Color(+nums[0], +nums[1], +nums[2],
122 valueOrPercent(nums[3]));
123 } else if (colorStr.startsWith('rgb')) {
124 // Trim off rgba( and the closing )
125 colorStr = colorStr.slice(4, -1);
126 var nums = colorStr.split(',');
127 // rgb can take 3 or 4 arguments
128 return CanvasKit.Color(+nums[0], +nums[1], +nums[2],
129 valueOrPercent(nums[3]));
130 } else if (colorStr.startsWith('gray(')) {
131 // TODO
132 } else if (colorStr.startsWith('hsl')) {
133 // TODO
134 } else if (colorMap) {
135 // Try for named color
136 var nc = colorMap[colorStr];
137 if (nc !== undefined) {
138 return nc;
139 }
140 }
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400141 Debug('unrecognized color ' + colorStr);
Kevin Lubick39284662020-02-20 10:29:55 -0500142 return CanvasKit.BLACK;
Kevin Lubicke7c1a732020-12-04 09:10:39 -0500143};
Kevin Lubick39284662020-02-20 10:29:55 -0500144
Nathaniel Nifonge5d32542020-03-26 09:27:48 -0400145function isCanvasKitColor(ob) {
146 if (!ob) {
147 return false;
148 }
149 return (ob.constructor === Float32Array && ob.length === 4);
150}
151
152// Warning information is lost by this conversion
153function toUint32Color(c) {
154 return ((clamp(c[3]*255) << 24) | (clamp(c[0]*255) << 16) | (clamp(c[1]*255) << 8) | (clamp(c[2]*255) << 0)) >>> 0;
155}
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -0400156// Accepts various colors representations and converts them to an array of int colors.
157// Does not handle builders.
158function assureIntColors(arr) {
159 if (arr instanceof Float32Array) {
160 var count = Math.floor(arr.length / 4);
161 var result = new Uint32Array(count);
162 for (var i = 0; i < count; i ++) {
163 result[i] = toUint32Color(arr.slice(i*4, (i+1)*4));
164 }
165 return result;
166 } else if (arr instanceof Uint32Array) {
167 return arr;
168 } else if (arr instanceof Array && arr[0] instanceof Float32Array) {
169 return arr.map(toUint32Color);
170 }
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -0400171}
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -0400172
Nathaniel Nifonge5d32542020-03-26 09:27:48 -0400173function uIntColorToCanvasKitColor(c) {
174 return CanvasKit.Color(
175 (c >> 16) & 0xFF,
176 (c >> 8) & 0xFF,
177 (c >> 0) & 0xFF,
178 ((c >> 24) & 0xFF) / 255
179 );
180}
181
Kevin Lubick39284662020-02-20 10:29:55 -0500182function valueOrPercent(aStr) {
183 if (aStr === undefined) {
184 return 1; // default to opaque.
185 }
186 var a = parseFloat(aStr);
187 if (aStr && aStr.indexOf('%') !== -1) {
188 return a / 100;
189 }
190 return a;
191}
192
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500193CanvasKit.multiplyByAlpha = function(color, alpha) {
Nathaniel Nifonge5d32542020-03-26 09:27:48 -0400194 // make a copy of the color so the function remains pure.
195 var result = color.slice();
196 result[3] = Math.max(0, Math.min(result[3] * alpha, 1));
197 return result;
Kevin Lubicke7c1a732020-12-04 09:10:39 -0500198};
Kevin Lubick61ef7b22018-11-27 13:26:59 -0500199
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500200function radiansToDegrees(rad) {
201 return (rad / Math.PI) * 180;
202}
Kevin Lubick12c0e502018-11-28 12:51:56 -0500203
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500204function degreesToRadians(deg) {
205 return (deg / 180) * Math.PI;
206}
Kevin Lubick12c0e502018-11-28 12:51:56 -0500207
208// See https://stackoverflow.com/a/31090240
209// This contraption keeps closure from minifying away the check
Kevin Lubickf8823b52020-09-03 10:02:10 -0400210// if btoa is defined *and* prevents runtime 'btoa' or 'window' is not defined.
Kevin Lubick12c0e502018-11-28 12:51:56 -0500211// Defined outside any scopes to make it available in all files.
Kevin Lubickf8823b52020-09-03 10:02:10 -0400212var isNode = !(new Function('try {return this===window;}catch(e){ return false;}')());
Kevin Lubick1646e7d2018-12-07 13:03:08 -0500213
214function almostEqual(floata, floatb) {
215 return Math.abs(floata - floatb) < 0.00001;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500216}
217
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500218var nullptr = 0; // emscripten doesn't like to take null as uintptr_t
219
220// arr can be a normal JS array or a TypedArray
Kevin Lubickf8823b52020-09-03 10:02:10 -0400221// dest is a string like 'HEAPU32' that specifies the type the src array
Kevin Lubick9c401e72020-06-09 14:22:20 -0400222// should be copied into.
Kevin Lubickd6ba7252019-06-03 14:38:05 -0400223// ptr can be optionally provided if the memory was already allocated.
224function copy1dArray(arr, dest, ptr) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500225 if (!arr || !arr.length) {
226 return nullptr;
227 }
Kevin Lubicke25df6c2019-10-22 09:04:32 -0400228 // This was created with CanvasKit.Malloc, so it's already been copied.
229 if (arr['_ck']) {
230 return arr.byteOffset;
231 }
Kevin Lubick69e46da2020-06-05 07:13:48 -0400232 var bytesPerElement = CanvasKit[dest].BYTES_PER_ELEMENT;
Kevin Lubickd6ba7252019-06-03 14:38:05 -0400233 if (!ptr) {
Kevin Lubick69e46da2020-06-05 07:13:48 -0400234 ptr = CanvasKit._malloc(arr.length * bytesPerElement);
Kevin Lubickd6ba7252019-06-03 14:38:05 -0400235 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500236 // In c++ terms, the WASM heap is a uint8_t*, a long buffer/array of single
237 // byte elements. When we run _malloc, we always get an offset/pointer into
238 // that block of memory.
239 // CanvasKit exposes some different views to make it easier to work with
240 // different types. HEAPF32 for example, exposes it as a float*
241 // However, to make the ptr line up, we have to do some pointer arithmetic.
242 // Concretely, we need to convert ptr to go from an index into a 1-byte-wide
243 // buffer to an index into a 4-byte-wide buffer (in the case of HEAPF32)
244 // and thus we divide ptr by 4.
Kevin Lubick69e46da2020-06-05 07:13:48 -0400245 // It is important to make sure we are grabbing the freshest view of the
246 // memory possible because if we call _malloc and the heap needs to grow,
247 // the TypedArrayView will no longer be valid.
248 CanvasKit[dest].set(arr, ptr / bytesPerElement);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500249 return ptr;
250}
251
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -0400252// Copies an array of colors to wasm, returning an object with the pointer
253// and info necessary to use the copied colors.
254// Accepts either a flat Float32Array, flat Uint32Array or Array of Float32Arrays.
Kevin Lubick421ba882020-10-15 13:07:33 -0400255// If color is an object that was allocated with CanvasKit.Malloc, its pointer is
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -0400256// returned and no extra copy is performed.
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -0400257// TODO(nifong): have this accept color builders.
258function copyFlexibleColorArray(colors) {
259 var result = {
260 colorPtr: nullptr,
261 count: colors.length,
262 colorType: CanvasKit.ColorType.RGBA_F32,
Kevin Lubicke7c1a732020-12-04 09:10:39 -0500263 };
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -0400264 if (colors instanceof Float32Array) {
Kevin Lubickf8823b52020-09-03 10:02:10 -0400265 result.colorPtr = copy1dArray(colors, 'HEAPF32');
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -0400266 result.count = colors.length / 4;
267
268 } else if (colors instanceof Uint32Array) {
Kevin Lubickf8823b52020-09-03 10:02:10 -0400269 result.colorPtr = copy1dArray(colors, 'HEAPU32');
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -0400270 result.colorType = CanvasKit.ColorType.RGBA_8888;
271
Kevin Lubicke7c1a732020-12-04 09:10:39 -0500272 } else if (colors instanceof Array) {
273 result.colorPtr = copyColorArray(colors);
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -0400274 } else {
275 throw('Invalid argument to copyFlexibleColorArray, Not a color array '+typeof(colors));
276 }
277 return result;
278}
279
Kevin Lubicke7c1a732020-12-04 09:10:39 -0500280function copyColorArray(arr) {
281 if (!arr || !arr.length) {
282 return nullptr;
283 }
284 // 4 floats per color, 4 bytes per float.
285 var ptr = CanvasKit._malloc(arr.length * 4 * 4);
286
287 var idx = 0;
288 var adjustedPtr = ptr / 4; // cast the byte pointer into a float pointer.
289 for (var r = 0; r < arr.length; r++) {
290 for (var c = 0; c < 4; c++) {
291 CanvasKit.HEAPF32[adjustedPtr + idx] = arr[r][c];
292 idx++;
293 }
294 }
295 return ptr;
296}
297
Kevin Lubick6bffe392020-04-02 15:24:15 -0400298var defaultPerspective = Float32Array.of(0, 0, 1);
299
Kevin Lubick6aa38692020-06-01 11:25:47 -0400300var _scratch3x3MatrixPtr = nullptr;
Kevin Lubick462a8602020-06-01 15:43:03 -0400301var _scratch3x3Matrix; // the result from CanvasKit.Malloc
Kevin Lubick6aa38692020-06-01 11:25:47 -0400302
Kevin Lubick6bffe392020-04-02 15:24:15 -0400303// Copies the given DOMMatrix/Array/TypedArray to the CanvasKit heap and
304// returns a pointer to the memory. This memory is a float* of length 9.
Kevin Lubick6de1e522021-01-21 11:11:59 -0500305// If the passed in matrix is null/undefined, we return 0 (nullptr). The
306// returned pointer should NOT be freed, as it is either null or a scratch
307// pointer.
Kevin Lubick6bffe392020-04-02 15:24:15 -0400308function copy3x3MatrixToWasm(matr) {
309 if (!matr) {
310 return nullptr;
311 }
Kevin Lubick6aa38692020-06-01 11:25:47 -0400312
Kevin Lubick6bffe392020-04-02 15:24:15 -0400313 if (matr.length) {
Kevin Lubick6de1e522021-01-21 11:11:59 -0500314 if (matr.length === 6 || matr.length === 9) {
315 // matr should be an array or typed array.
316 copy1dArray(matr, 'HEAPF32', _scratch3x3MatrixPtr);
317 if (matr.length === 6) {
318 // Overwrite the last 3 floats with the default perspective. The divide
319 // by 4 casts the pointer into a float pointer.
320 CanvasKit.HEAPF32.set(defaultPerspective, 6 + _scratch3x3MatrixPtr / 4);
321 }
322 return _scratch3x3MatrixPtr;
323 } else if (matr.length === 16) {
324 // Downsample the 4x4 matrix into a 3x3
325 var wasm3x3Matrix = _scratch3x3Matrix['toTypedArray']();
326 wasm3x3Matrix[0] = matr[0];
327 wasm3x3Matrix[1] = matr[1];
328 wasm3x3Matrix[2] = matr[3];
329
330 wasm3x3Matrix[3] = matr[4];
331 wasm3x3Matrix[4] = matr[5];
332 wasm3x3Matrix[5] = matr[7];
333
334 wasm3x3Matrix[6] = matr[12];
335 wasm3x3Matrix[7] = matr[13];
336 wasm3x3Matrix[8] = matr[15];
337 return _scratch3x3MatrixPtr;
Kevin Lubick6bffe392020-04-02 15:24:15 -0400338 }
Kevin Lubick6de1e522021-01-21 11:11:59 -0500339 throw 'invalid matrix size';
Kevin Lubick6bffe392020-04-02 15:24:15 -0400340 }
Kevin Lubick462a8602020-06-01 15:43:03 -0400341 var wasm3x3Matrix = _scratch3x3Matrix['toTypedArray']();
Kevin Lubick6aa38692020-06-01 11:25:47 -0400342 // Try as if it's a DOMMatrix. Reminder that DOMMatrix is column-major.
Kevin Lubick462a8602020-06-01 15:43:03 -0400343 wasm3x3Matrix[0] = matr.m11;
344 wasm3x3Matrix[1] = matr.m21;
345 wasm3x3Matrix[2] = matr.m41;
Kevin Lubick6aa38692020-06-01 11:25:47 -0400346
Kevin Lubick462a8602020-06-01 15:43:03 -0400347 wasm3x3Matrix[3] = matr.m12;
348 wasm3x3Matrix[4] = matr.m22;
349 wasm3x3Matrix[5] = matr.m42;
Kevin Lubick6aa38692020-06-01 11:25:47 -0400350
Kevin Lubick462a8602020-06-01 15:43:03 -0400351 wasm3x3Matrix[6] = matr.m14;
352 wasm3x3Matrix[7] = matr.m24;
353 wasm3x3Matrix[8] = matr.m44;
Kevin Lubick6aa38692020-06-01 11:25:47 -0400354 return _scratch3x3MatrixPtr;
Kevin Lubick6bffe392020-04-02 15:24:15 -0400355}
356
Kevin Lubick6aa38692020-06-01 11:25:47 -0400357var _scratch4x4MatrixPtr = nullptr;
Kevin Lubick462a8602020-06-01 15:43:03 -0400358var _scratch4x4Matrix; // the result from CanvasKit.Malloc
Kevin Lubick6aa38692020-06-01 11:25:47 -0400359
Kevin Lubick6de1e522021-01-21 11:11:59 -0500360// Copies the given DOMMatrix/Array/TypedArray to the CanvasKit heap and
361// returns a pointer to the memory. This memory is a float* of length 16.
362// If the passed in matrix is null/undefined, we return 0 (nullptr). The
363// returned pointer should NOT be freed, as it is either null or a scratch
364// pointer.
Kevin Lubickc1d08982020-04-06 13:52:15 -0400365function copy4x4MatrixToWasm(matr) {
366 if (!matr) {
367 return nullptr;
368 }
Kevin Lubick462a8602020-06-01 15:43:03 -0400369 var wasm4x4Matrix = _scratch4x4Matrix['toTypedArray']();
Kevin Lubickc1d08982020-04-06 13:52:15 -0400370 if (matr.length) {
371 if (matr.length !== 16 && matr.length !== 6 && matr.length !== 9) {
372 throw 'invalid matrix size';
373 }
374 if (matr.length === 16) {
Kevin Lubickf8823b52020-09-03 10:02:10 -0400375 // matr should be an array or typed array.
376 return copy1dArray(matr, 'HEAPF32', _scratch4x4MatrixPtr);
Kevin Lubickc1d08982020-04-06 13:52:15 -0400377 }
Kevin Lubick6aa38692020-06-01 11:25:47 -0400378 // Upscale the row-major 3x3 or 3x2 matrix into a 4x4 row-major matrix
379 // TODO(skbug.com/10108) This will need to change when we convert our
380 // JS 4x4 to be column-major.
381 // When upscaling, we need to overwrite the 3rd column and the 3rd row with
382 // 0s. It's easiest to just do that with a fill command.
Kevin Lubick462a8602020-06-01 15:43:03 -0400383 wasm4x4Matrix.fill(0);
384 wasm4x4Matrix[0] = matr[0];
385 wasm4x4Matrix[1] = matr[1];
Kevin Lubick6aa38692020-06-01 11:25:47 -0400386 // skip col 2
Kevin Lubick462a8602020-06-01 15:43:03 -0400387 wasm4x4Matrix[3] = matr[2];
Kevin Lubick6aa38692020-06-01 11:25:47 -0400388
Kevin Lubick462a8602020-06-01 15:43:03 -0400389 wasm4x4Matrix[4] = matr[3];
390 wasm4x4Matrix[5] = matr[4];
Kevin Lubick6aa38692020-06-01 11:25:47 -0400391 // skip col 2
Kevin Lubick462a8602020-06-01 15:43:03 -0400392 wasm4x4Matrix[7] = matr[5];
Kevin Lubick6aa38692020-06-01 11:25:47 -0400393
394 // skip row 2
395
Kevin Lubick462a8602020-06-01 15:43:03 -0400396 wasm4x4Matrix[12] = matr[6];
397 wasm4x4Matrix[13] = matr[7];
Kevin Lubick6aa38692020-06-01 11:25:47 -0400398 // skip col 2
Kevin Lubick462a8602020-06-01 15:43:03 -0400399 wasm4x4Matrix[15] = matr[8];
Kevin Lubick6aa38692020-06-01 11:25:47 -0400400
401 if (matr.length === 6) {
402 // fix perspective for the 3x2 case (from above, they will be undefined).
Kevin Lubick462a8602020-06-01 15:43:03 -0400403 wasm4x4Matrix[12]=0;
404 wasm4x4Matrix[13]=0;
405 wasm4x4Matrix[15]=1;
Kevin Lubick6aa38692020-06-01 11:25:47 -0400406 }
407 return _scratch4x4MatrixPtr;
Kevin Lubickc1d08982020-04-06 13:52:15 -0400408 }
Kevin Lubick6aa38692020-06-01 11:25:47 -0400409 // Try as if it's a DOMMatrix. Reminder that DOMMatrix is column-major.
Kevin Lubick462a8602020-06-01 15:43:03 -0400410 wasm4x4Matrix[0] = matr.m11;
411 wasm4x4Matrix[1] = matr.m21;
412 wasm4x4Matrix[2] = matr.m31;
413 wasm4x4Matrix[3] = matr.m41;
Kevin Lubick6aa38692020-06-01 11:25:47 -0400414
Kevin Lubick462a8602020-06-01 15:43:03 -0400415 wasm4x4Matrix[4] = matr.m12;
416 wasm4x4Matrix[5] = matr.m22;
417 wasm4x4Matrix[6] = matr.m32;
418 wasm4x4Matrix[7] = matr.m42;
Kevin Lubick6aa38692020-06-01 11:25:47 -0400419
Kevin Lubick462a8602020-06-01 15:43:03 -0400420 wasm4x4Matrix[8] = matr.m13;
421 wasm4x4Matrix[9] = matr.m23;
422 wasm4x4Matrix[10] = matr.m33;
423 wasm4x4Matrix[11] = matr.m43;
Kevin Lubick6aa38692020-06-01 11:25:47 -0400424
Kevin Lubick462a8602020-06-01 15:43:03 -0400425 wasm4x4Matrix[12] = matr.m14;
426 wasm4x4Matrix[13] = matr.m24;
427 wasm4x4Matrix[14] = matr.m34;
428 wasm4x4Matrix[15] = matr.m44;
Kevin Lubick6aa38692020-06-01 11:25:47 -0400429 return _scratch4x4MatrixPtr;
Kevin Lubickc1d08982020-04-06 13:52:15 -0400430}
431
Kevin Lubick6aa38692020-06-01 11:25:47 -0400432// copies a 4x4 matrix at the given pointer into a JS array. It is the caller's
433// responsibility to free the matrPtr if needed.
Kevin Lubickc1d08982020-04-06 13:52:15 -0400434function copy4x4MatrixFromWasm(matrPtr) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400435 // read them out into an array. TODO(kjlubick): If we change Matrix to be
Kevin Lubickc1d08982020-04-06 13:52:15 -0400436 // typedArrays, then we should return a typed array here too.
437 var rv = new Array(16);
438 for (var i = 0; i < 16; i++) {
Kevin Lubickf8823b52020-09-03 10:02:10 -0400439 rv[i] = CanvasKit.HEAPF32[matrPtr/4 + i]; // divide by 4 to cast to float.
Kevin Lubickc1d08982020-04-06 13:52:15 -0400440 }
Kevin Lubickc1d08982020-04-06 13:52:15 -0400441 return rv;
442}
443
Kevin Lubick6aa38692020-06-01 11:25:47 -0400444var _scratchColorPtr = nullptr;
Kevin Lubick93f1a382020-06-02 16:15:23 -0400445var _scratchColor; // the result from CanvasKit.Malloc
Kevin Lubick6aa38692020-06-01 11:25:47 -0400446
447function copyColorToWasm(color4f, ptr) {
Kevin Lubickf8823b52020-09-03 10:02:10 -0400448 return copy1dArray(color4f, 'HEAPF32', ptr || _scratchColorPtr);
Kevin Lubick6aa38692020-06-01 11:25:47 -0400449}
450
Kevin Lubick93f1a382020-06-02 16:15:23 -0400451function copyColorComponentsToWasm(r, g, b, a) {
452 var colors = _scratchColor['toTypedArray']();
453 colors[0] = r;
454 colors[1] = g;
455 colors[2] = b;
456 colors[3] = a;
457 return _scratchColorPtr;
458}
459
Kevin Lubick6aa38692020-06-01 11:25:47 -0400460function copyColorToWasmNoScratch(color4f) {
461 // TODO(kjlubick): accept 4 floats or int color
Kevin Lubickf8823b52020-09-03 10:02:10 -0400462 return copy1dArray(color4f, 'HEAPF32');
Kevin Lubick6aa38692020-06-01 11:25:47 -0400463}
464
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400465// copies the four floats at the given pointer in a js Float32Array
466function copyColorFromWasm(colorPtr) {
467 var rv = new Float32Array(4);
468 for (var i = 0; i < 4; i++) {
Kevin Lubickf8823b52020-09-03 10:02:10 -0400469 rv[i] = CanvasKit.HEAPF32[colorPtr/4 + i]; // divide by 4 to cast to float.
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400470 }
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400471 return rv;
Kevin Lubickcf118922020-05-28 14:43:38 -0400472}
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400473
Kevin Lubickbe728012020-09-03 11:57:12 +0000474// These will be initialized after loading.
Kevin Lubickf8823b52020-09-03 10:02:10 -0400475var _scratchRect;
476var _scratchRectPtr = nullptr;
477
478var _scratchRect2;
479var _scratchRect2Ptr = nullptr;
480
481function copyRectToWasm(fourFloats, ptr) {
482 return copy1dArray(fourFloats, 'HEAPF32', ptr || _scratchRectPtr);
483}
484
485var _scratchIRect;
486var _scratchIRectPtr = nullptr;
487function copyIRectToWasm(fourInts, ptr) {
488 return copy1dArray(fourInts, 'HEAP32', ptr || _scratchIRectPtr);
489}
490
491// These will be initialized after loading.
Kevin Lubickbe728012020-09-03 11:57:12 +0000492var _scratchRRect;
493var _scratchRRectPtr = nullptr;
494
495var _scratchRRect2;
496var _scratchRRect2Ptr = nullptr;
497
498
499function copyRRectToWasm(twelveFloats, ptr) {
Kevin Lubickf8823b52020-09-03 10:02:10 -0400500 return copy1dArray(twelveFloats, 'HEAPF32', ptr || _scratchRRectPtr);
Kevin Lubickbe728012020-09-03 11:57:12 +0000501}
502
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500503// Caching the Float32Arrays can save having to reallocate them
504// over and over again.
505var Float32ArrayCache = {};
506
507// Takes a 2D array of commands and puts them into the WASM heap
508// as a 1D array. This allows them to referenced from the C++ code.
509// Returns a 2 element array, with the first item being essentially a
510// pointer to the array and the second item being the length of
511// the new 1D array.
512//
513// Example usage:
514// let cmds = [
515// [CanvasKit.MOVE_VERB, 0, 10],
516// [CanvasKit.LINE_VERB, 30, 40],
517// [CanvasKit.QUAD_VERB, 20, 50, 45, 60],
518// ];
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -0400519// TODO(kjlubick) remove this and Float32ArrayCache (superceded by Malloc).
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500520function loadCmdsTypedArray(arr) {
521 var len = 0;
522 for (var r = 0; r < arr.length; r++) {
523 len += arr[r].length;
524 }
525
526 var ta;
527 if (Float32ArrayCache[len]) {
528 ta = Float32ArrayCache[len];
529 } else {
530 ta = new Float32Array(len);
531 Float32ArrayCache[len] = ta;
532 }
533 // Flatten into a 1d array
534 var i = 0;
535 for (var r = 0; r < arr.length; r++) {
536 for (var c = 0; c < arr[r].length; c++) {
537 var item = arr[r][c];
538 ta[i] = item;
539 i++;
540 }
541 }
542
Kevin Lubickf8823b52020-09-03 10:02:10 -0400543 var ptr = copy1dArray(ta, 'HEAPF32');
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500544 return [ptr, len];
545}
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400546
Kevin Lubickcc13fd32019-04-05 13:00:01 -0400547function saveBytesToFile(bytes, fileName) {
548 if (!isNode) {
549 // https://stackoverflow.com/a/32094834
550 var blob = new Blob([bytes], {type: 'application/octet-stream'});
551 url = window.URL.createObjectURL(blob);
552 var a = document.createElement('a');
553 document.body.appendChild(a);
554 a.href = url;
555 a.download = fileName;
556 a.click();
557 // clean up after because FF might not download it synchronously
558 setTimeout(function() {
559 URL.revokeObjectURL(url);
560 a.remove();
561 }, 50);
562 } else {
563 var fs = require('fs');
564 // https://stackoverflow.com/a/42006750
565 // https://stackoverflow.com/a/47018122
566 fs.writeFile(fileName, new Buffer(bytes), function(err) {
567 if (err) throw err;
568 });
569 }
570}
Kevin Lubick97440de2020-09-29 17:58:21 -0400571
572// TODO(kjlubick) remove Builders - no longer needed now that Malloc is a thing.
Kevin Lubickee91c072019-03-29 10:39:52 -0400573/**
574 * Generic helper for dealing with an array of four floats.
575 */
576CanvasKit.FourFloatArrayHelper = function() {
577 this._floats = [];
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400578 this._ptr = null;
Kevin Lubickee91c072019-03-29 10:39:52 -0400579
580 Object.defineProperty(this, 'length', {
581 enumerable: true,
582 get: function() {
583 return this._floats.length / 4;
584 },
585 });
Kevin Lubicke7c1a732020-12-04 09:10:39 -0500586};
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400587
588/**
Kevin Lubickee91c072019-03-29 10:39:52 -0400589 * push the four floats onto the end of the array - if build() has already
590 * been called, the call will return without modifying anything.
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400591 */
Kevin Lubickee91c072019-03-29 10:39:52 -0400592CanvasKit.FourFloatArrayHelper.prototype.push = function(f1, f2, f3, f4) {
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400593 if (this._ptr) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400594 Debug('Cannot push more points - already built');
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400595 return;
596 }
Kevin Lubickee91c072019-03-29 10:39:52 -0400597 this._floats.push(f1, f2, f3, f4);
Kevin Lubicke7c1a732020-12-04 09:10:39 -0500598};
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400599
Kevin Lubickee91c072019-03-29 10:39:52 -0400600/**
601 * Set the four floats at a given index - if build() has already
602 * been called, the WASM memory will be written to directly.
603 */
604CanvasKit.FourFloatArrayHelper.prototype.set = function(idx, f1, f2, f3, f4) {
605 if (idx < 0 || idx >= this._floats.length/4) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400606 Debug('Cannot set index ' + idx + ', it is out of range', this._floats.length/4);
Kevin Lubickee91c072019-03-29 10:39:52 -0400607 return;
608 }
609 idx *= 4;
610 var BYTES_PER_ELEMENT = 4;
611 if (this._ptr) {
612 // convert this._ptr from uint8_t* to SkScalar* by dividing by 4
613 var floatPtr = (this._ptr / BYTES_PER_ELEMENT) + idx;
614 CanvasKit.HEAPF32[floatPtr] = f1;
615 CanvasKit.HEAPF32[floatPtr + 1] = f2;
616 CanvasKit.HEAPF32[floatPtr + 2] = f3;
617 CanvasKit.HEAPF32[floatPtr + 3] = f4;
618 return;
619 }
620 this._floats[idx] = f1;
621 this._floats[idx + 1] = f2;
622 this._floats[idx + 2] = f3;
623 this._floats[idx + 3] = f4;
Kevin Lubicke7c1a732020-12-04 09:10:39 -0500624};
Kevin Lubickee91c072019-03-29 10:39:52 -0400625
626/**
627 * Copies the float data to the WASM memory and returns a pointer
628 * to that allocated memory. Once build has been called, this
629 * float array cannot be made bigger.
630 */
631CanvasKit.FourFloatArrayHelper.prototype.build = function() {
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400632 if (this._ptr) {
633 return this._ptr;
634 }
Kevin Lubickf8823b52020-09-03 10:02:10 -0400635 this._ptr = copy1dArray(this._floats, 'HEAPF32');
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400636 return this._ptr;
Kevin Lubicke7c1a732020-12-04 09:10:39 -0500637};
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400638
Kevin Lubickee91c072019-03-29 10:39:52 -0400639/**
640 * Frees the wasm memory associated with this array. Of note,
641 * the points are not removed, so push/set/build can all
642 * be called to make a newly allocated (possibly bigger)
643 * float array.
644 */
645CanvasKit.FourFloatArrayHelper.prototype.delete = function() {
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400646 if (this._ptr) {
647 CanvasKit._free(this._ptr);
648 this._ptr = null;
649 }
Kevin Lubicke7c1a732020-12-04 09:10:39 -0500650};
Kevin Lubickee91c072019-03-29 10:39:52 -0400651
652/**
653 * Generic helper for dealing with an array of unsigned ints.
654 */
655CanvasKit.OneUIntArrayHelper = function() {
656 this._uints = [];
657 this._ptr = null;
658
659 Object.defineProperty(this, 'length', {
660 enumerable: true,
661 get: function() {
662 return this._uints.length;
663 },
664 });
Kevin Lubicke7c1a732020-12-04 09:10:39 -0500665};
Kevin Lubickee91c072019-03-29 10:39:52 -0400666
667/**
668 * push the unsigned int onto the end of the array - if build() has already
669 * been called, the call will return without modifying anything.
670 */
671CanvasKit.OneUIntArrayHelper.prototype.push = function(u) {
672 if (this._ptr) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400673 Debug('Cannot push more points - already built');
Kevin Lubickee91c072019-03-29 10:39:52 -0400674 return;
675 }
676 this._uints.push(u);
Kevin Lubicke7c1a732020-12-04 09:10:39 -0500677};
Kevin Lubickee91c072019-03-29 10:39:52 -0400678
679/**
680 * Set the uint at a given index - if build() has already
681 * been called, the WASM memory will be written to directly.
682 */
683CanvasKit.OneUIntArrayHelper.prototype.set = function(idx, u) {
684 if (idx < 0 || idx >= this._uints.length) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400685 Debug('Cannot set index ' + idx + ', it is out of range', this._uints.length);
Kevin Lubickee91c072019-03-29 10:39:52 -0400686 return;
687 }
688 idx *= 4;
689 var BYTES_PER_ELEMENT = 4;
690 if (this._ptr) {
691 // convert this._ptr from uint8_t* to SkScalar* by dividing by 4
692 var uintPtr = (this._ptr / BYTES_PER_ELEMENT) + idx;
693 CanvasKit.HEAPU32[uintPtr] = u;
694 return;
695 }
696 this._uints[idx] = u;
Kevin Lubicke7c1a732020-12-04 09:10:39 -0500697};
Kevin Lubickee91c072019-03-29 10:39:52 -0400698
699/**
700 * Copies the uint data to the WASM memory and returns a pointer
701 * to that allocated memory. Once build has been called, this
702 * unit array cannot be made bigger.
703 */
704CanvasKit.OneUIntArrayHelper.prototype.build = function() {
705 if (this._ptr) {
706 return this._ptr;
707 }
Kevin Lubickf8823b52020-09-03 10:02:10 -0400708 this._ptr = copy1dArray(this._uints, 'HEAPU32');
Kevin Lubickee91c072019-03-29 10:39:52 -0400709 return this._ptr;
Kevin Lubicke7c1a732020-12-04 09:10:39 -0500710};
Kevin Lubickee91c072019-03-29 10:39:52 -0400711
712/**
713 * Frees the wasm memory associated with this array. Of note,
714 * the points are not removed, so push/set/build can all
715 * be called to make a newly allocated (possibly bigger)
716 * uint array.
717 */
718CanvasKit.OneUIntArrayHelper.prototype.delete = function() {
719 if (this._ptr) {
720 CanvasKit._free(this._ptr);
721 this._ptr = null;
722 }
Kevin Lubicke7c1a732020-12-04 09:10:39 -0500723};
Kevin Lubickee91c072019-03-29 10:39:52 -0400724
725/**
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400726 * Helper for building an array of Rects (which are just structs
Kevin Lubickee91c072019-03-29 10:39:52 -0400727 * of 4 floats).
728 *
729 * It can be more performant to use this helper, as
730 * the C++-side array is only allocated once (on the first call)
731 * to build. Subsequent set() operations operate directly on
732 * the C++-side array, avoiding having to re-allocate (and free)
733 * the array every time.
734 *
735 * Input points are taken as left, top, right, bottom
736 */
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400737CanvasKit.RectBuilder = CanvasKit.FourFloatArrayHelper;
Kevin Lubickee91c072019-03-29 10:39:52 -0400738/**
739 * Helper for building an array of RSXForms (which are just structs
740 * of 4 floats).
741 *
742 * It can be more performant to use this helper, as
743 * the C++-side array is only allocated once (on the first call)
744 * to build. Subsequent set() operations operate directly on
745 * the C++-side array, avoiding having to re-allocate (and free)
746 * the array every time.
747 *
748 * An RSXForm is a compressed form of a rotation+scale matrix.
749 *
750 * [ scos -ssin tx ]
751 * [ ssin scos ty ]
752 * [ 0 0 1 ]
753 *
754 * Input points are taken as scos, ssin, tx, ty
755 */
756CanvasKit.RSXFormBuilder = CanvasKit.FourFloatArrayHelper;
757
758/**
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400759 * Helper for building an array of Color
Kevin Lubickee91c072019-03-29 10:39:52 -0400760 *
761 * It can be more performant to use this helper, as
762 * the C++-side array is only allocated once (on the first call)
763 * to build. Subsequent set() operations operate directly on
764 * the C++-side array, avoiding having to re-allocate (and free)
765 * the array every time.
766 */
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400767CanvasKit.ColorBuilder = CanvasKit.OneUIntArrayHelper;
Kevin Lubicke25df6c2019-10-22 09:04:32 -0400768
769/**
770 * Malloc returns a TypedArray backed by the C++ memory of the
771 * given length. It should only be used by advanced users who
772 * can manage memory and initialize values properly. When used
773 * correctly, it can save copying of data between JS and C++.
774 * When used incorrectly, it can lead to memory leaks.
Kevin Lubickcf118922020-05-28 14:43:38 -0400775 * Any memory allocated by CanvasKit.Malloc needs to be released with CanvasKit.Free.
Kevin Lubicke25df6c2019-10-22 09:04:32 -0400776 *
Kevin Lubick462a8602020-06-01 15:43:03 -0400777 * const mObj = CanvasKit.Malloc(Float32Array, 20);
Kevin Lubick93f1a382020-06-02 16:15:23 -0400778 * Get a TypedArray view around the malloc'd memory (this does not copy anything).
Kevin Lubick462a8602020-06-01 15:43:03 -0400779 * const ta = mObj.toTypedArray();
Kevin Lubicke25df6c2019-10-22 09:04:32 -0400780 * // store data into ta
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400781 * const cf = CanvasKit.ColorFilter.MakeMatrix(ta); // mObj could also be used.
Kevin Lubick462a8602020-06-01 15:43:03 -0400782 *
783 * // eventually...
784 * CanvasKit.Free(mObj);
Kevin Lubicke25df6c2019-10-22 09:04:32 -0400785 *
786 * @param {TypedArray} typedArray - constructor for the typedArray.
Kevin Lubick6aa38692020-06-01 11:25:47 -0400787 * @param {number} len - number of *elements* to store.
Kevin Lubicke25df6c2019-10-22 09:04:32 -0400788 */
789CanvasKit.Malloc = function(typedArray, len) {
790 var byteLen = len * typedArray.BYTES_PER_ELEMENT;
791 var ptr = CanvasKit._malloc(byteLen);
Kevin Lubick462a8602020-06-01 15:43:03 -0400792 return {
793 '_ck': true,
794 'length': len,
795 'byteOffset': ptr,
796 typedArray: null,
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -0400797 'subarray': function(start, end) {
798 var sa = this['toTypedArray']().subarray(start, end);
799 sa['_ck'] = true;
800 return sa;
801 },
Kevin Lubick462a8602020-06-01 15:43:03 -0400802 'toTypedArray': function() {
803 // Check if the previously allocated array is still usable.
804 // If it's falsey, then we haven't created an array yet.
805 // If it's empty, then WASM resized memory and emptied the array.
806 if (this.typedArray && this.typedArray.length) {
807 return this.typedArray;
808 }
809 this.typedArray = new typedArray(CanvasKit.HEAPU8.buffer, ptr, len);
810 // add a marker that this was allocated in C++ land
811 this.typedArray['_ck'] = true;
812 return this.typedArray;
813 },
814 };
Kevin Lubick26133322020-06-11 13:48:16 -0400815};
Kevin Lubickcf118922020-05-28 14:43:38 -0400816
817/**
818 * Free frees the memory returned by Malloc.
819 * Any memory allocated by CanvasKit.Malloc needs to be released with CanvasKit.Free.
820 */
Kevin Lubick462a8602020-06-01 15:43:03 -0400821CanvasKit.Free = function(mallocObj) {
822 CanvasKit._free(mallocObj['byteOffset']);
823 mallocObj['byteOffset'] = nullptr;
824 // Set these to null to make sure the TypedArrays can be garbage collected.
825 mallocObj['toTypedArray'] = null;
826 mallocObj.typedArray = null;
Kevin Lubick26133322020-06-11 13:48:16 -0400827};
Kevin Lubickcf118922020-05-28 14:43:38 -0400828
829// This helper will free the given pointer unless the provided array is one
830// that was returned by CanvasKit.Malloc.
831function freeArraysThatAreNotMallocedByUsers(ptr, arr) {
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -0400832 if (arr && !arr['_ck']) {
Kevin Lubickcf118922020-05-28 14:43:38 -0400833 CanvasKit._free(ptr);
834 }
Kevin Lubick6aa38692020-06-01 11:25:47 -0400835}