blob: e16186a646f70d3d1278bbff03536eba3fd323ca [file] [log] [blame]
Kevin Lubick217056c2018-09-20 17:39:31 -04001// Adds JS functions to augment the CanvasKit interface.
2// For example, if there is a wrapper around the C++ call or logic to allow
3// chaining, it should go here.
Kevin Lubick1a05fce2018-11-20 12:51:16 -05004
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05005// CanvasKit.onRuntimeInitialized is called after the WASM library has loaded.
6// Anything that modifies an exposed class (e.g. SkPath) should be set
7// after onRuntimeInitialized, otherwise, it can happen outside of that scope.
8CanvasKit.onRuntimeInitialized = function() {
9 // All calls to 'this' need to go in externs.js so closure doesn't minify them away.
Kevin Lubick1a05fce2018-11-20 12:51:16 -050010
Kevin Lubickfa5a1382019-10-09 10:46:14 -040011 // buffer is the underlying ArrayBuffer that is the WASM memory blob.
12 // It was removed from Emscripten proper in https://github.com/emscripten-core/emscripten/pull/8277
13 // but it is convenient to have a reference to, so we add it back in.
14 CanvasKit.buffer = CanvasKit.HEAPU8.buffer;
15
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050016 // Add some helpers for matrices. This is ported from SkMatrix.cpp
17 // to save complexity and overhead of going back and forth between
18 // C++ and JS layers.
19 // I would have liked to use something like DOMMatrix, except it
20 // isn't widely supported (would need polyfills) and it doesn't
21 // have a mapPoints() function (which could maybe be tacked on here).
22 // If DOMMatrix catches on, it would be worth re-considering this usage.
23 CanvasKit.SkMatrix = {};
24 function sdot(a, b, c, d, e, f) {
25 e = e || 0;
26 f = f || 0;
27 return a * b + c * d + e * f;
28 }
29
30 CanvasKit.SkMatrix.identity = function() {
31 return [
32 1, 0, 0,
33 0, 1, 0,
34 0, 0, 1,
35 ];
36 };
37
38 // Return the inverse (if it exists) of this matrix.
39 // Otherwise, return the identity.
40 CanvasKit.SkMatrix.invert = function(m) {
41 var det = m[0]*m[4]*m[8] + m[1]*m[5]*m[6] + m[2]*m[3]*m[7]
42 - m[2]*m[4]*m[6] - m[1]*m[3]*m[8] - m[0]*m[5]*m[7];
43 if (!det) {
44 SkDebug('Warning, uninvertible matrix');
45 return CanvasKit.SkMatrix.identity();
Kevin Lubick1a05fce2018-11-20 12:51:16 -050046 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050047 return [
48 (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,
49 (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,
50 (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,
51 ];
52 };
Kevin Lubick1a05fce2018-11-20 12:51:16 -050053
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050054 // Maps the given points according to the passed in matrix.
55 // Results are done in place.
56 // See SkMatrix.h::mapPoints for the docs on the math.
57 CanvasKit.SkMatrix.mapPoints = function(matrix, ptArr) {
58 if (ptArr.length % 2) {
59 throw 'mapPoints requires an even length arr';
Kevin Lubickb9db3902018-11-26 11:47:54 -050060 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050061 for (var i = 0; i < ptArr.length; i+=2) {
62 var x = ptArr[i], y = ptArr[i+1];
63 // Gx+Hy+I
64 var denom = matrix[6]*x + matrix[7]*y + matrix[8];
65 // Ax+By+C
66 var xTrans = matrix[0]*x + matrix[1]*y + matrix[2];
67 // Dx+Ey+F
68 var yTrans = matrix[3]*x + matrix[4]*y + matrix[5];
69 ptArr[i] = xTrans/denom;
70 ptArr[i+1] = yTrans/denom;
71 }
72 return ptArr;
73 };
Kevin Lubickb9db3902018-11-26 11:47:54 -050074
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050075 CanvasKit.SkMatrix.multiply = function(m1, m2) {
76 var result = [0,0,0, 0,0,0, 0,0,0];
77 for (var r = 0; r < 3; r++) {
78 for (var c = 0; c < 3; c++) {
79 // m1 and m2 are 1D arrays pretending to be 2D arrays
80 result[3*r + c] = sdot(m1[3*r + 0], m2[3*0 + c],
81 m1[3*r + 1], m2[3*1 + c],
82 m1[3*r + 2], m2[3*2 + c]);
Kevin Lubick1a05fce2018-11-20 12:51:16 -050083 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050084 }
85 return result;
86 }
Kevin Lubick1a05fce2018-11-20 12:51:16 -050087
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050088 // Return a matrix representing a rotation by n radians.
89 // px, py optionally say which point the rotation should be around
90 // with the default being (0, 0);
91 CanvasKit.SkMatrix.rotated = function(radians, px, py) {
92 px = px || 0;
93 py = py || 0;
94 var sinV = Math.sin(radians);
95 var cosV = Math.cos(radians);
96 return [
97 cosV, -sinV, sdot( sinV, py, 1 - cosV, px),
98 sinV, cosV, sdot(-sinV, px, 1 - cosV, py),
99 0, 0, 1,
100 ];
101 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400102
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500103 CanvasKit.SkMatrix.scaled = function(sx, sy, px, py) {
104 px = px || 0;
105 py = py || 0;
106 return [
107 sx, 0, px - sx * px,
108 0, sy, py - sy * py,
109 0, 0, 1,
110 ];
111 };
Kevin Lubickda3d8ac2019-01-07 11:08:55 -0500112
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500113 CanvasKit.SkMatrix.skewed = function(kx, ky, px, py) {
114 px = px || 0;
115 py = py || 0;
116 return [
117 1, kx, -kx * px,
118 ky, 1, -ky * py,
119 0, 0, 1,
120 ];
121 };
Alexander Khovansky3e119332018-11-15 02:01:19 +0300122
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500123 CanvasKit.SkMatrix.translated = function(dx, dy) {
124 return [
125 1, 0, dx,
126 0, 1, dy,
127 0, 0, 1,
128 ];
129 };
Kevin Lubick1646e7d2018-12-07 13:03:08 -0500130
Kevin Lubickd3729342019-09-12 11:11:25 -0400131 // An SkColorMatrix is a 4x4 color matrix that transforms the 4 color channels
132 // with a 1x4 matrix that post-translates those 4 channels.
133 // For example, the following is the layout with the scale (S) and post-transform
134 // (PT) items indicated.
135 // RS, 0, 0, 0 | RPT
136 // 0, GS, 0, 0 | GPT
137 // 0, 0, BS, 0 | BPT
138 // 0, 0, 0, AS | APT
139 //
140 // Much of this was hand-transcribed from SkColorMatrix.cpp, because it's easier to
141 // deal with a Float32Array of length 20 than to try to expose the SkColorMatrix object.
142
143 var rScale = 0;
144 var gScale = 6;
145 var bScale = 12;
146 var aScale = 18;
147
148 var rPostTrans = 4;
149 var gPostTrans = 9;
150 var bPostTrans = 14;
151 var aPostTrans = 19;
152
153 CanvasKit.SkColorMatrix = {};
154 CanvasKit.SkColorMatrix.identity = function() {
155 var m = new Float32Array(20);
156 m[rScale] = 1;
157 m[gScale] = 1;
158 m[bScale] = 1;
159 m[aScale] = 1;
160 return m;
161 }
162
163 CanvasKit.SkColorMatrix.scaled = function(rs, gs, bs, as) {
164 var m = new Float32Array(20);
165 m[rScale] = rs;
166 m[gScale] = gs;
167 m[bScale] = bs;
168 m[aScale] = as;
169 return m;
170 }
171
172 var rotateIndices = [
173 [6, 7, 11, 12],
174 [0, 10, 2, 12],
175 [0, 1, 5, 6],
176 ];
177 // axis should be 0, 1, 2 for r, g, b
178 CanvasKit.SkColorMatrix.rotated = function(axis, sine, cosine) {
179 var m = CanvasKit.SkColorMatrix.identity();
180 var indices = rotateIndices[axis];
181 m[indices[0]] = cosine;
182 m[indices[1]] = sine;
183 m[indices[2]] = -sine;
184 m[indices[3]] = cosine;
185 return m;
186 }
187
188 // m is a SkColorMatrix (i.e. a Float32Array), and this sets the 4 "special"
189 // params that will translate the colors after they are multiplied by the 4x4 matrix.
190 CanvasKit.SkColorMatrix.postTranslate = function(m, dr, dg, db, da) {
191 m[rPostTrans] += dr;
192 m[gPostTrans] += dg;
193 m[bPostTrans] += db;
194 m[aPostTrans] += da;
195 return m;
196 }
197
198 // concat returns a new SkColorMatrix that is the result of multiplying outer*inner;
199 CanvasKit.SkColorMatrix.concat = function(outer, inner) {
200 var m = new Float32Array(20);
201 var index = 0;
202 for (var j = 0; j < 20; j += 5) {
203 for (var i = 0; i < 4; i++) {
204 m[index++] = outer[j + 0] * inner[i + 0] +
205 outer[j + 1] * inner[i + 5] +
206 outer[j + 2] * inner[i + 10] +
207 outer[j + 3] * inner[i + 15];
208 }
209 m[index++] = outer[j + 0] * inner[4] +
210 outer[j + 1] * inner[9] +
211 outer[j + 2] * inner[14] +
212 outer[j + 3] * inner[19] +
213 outer[j + 4];
214 }
215
216 return m;
217 }
218
Kevin Lubick4b5b6452019-12-06 13:55:58 -0500219 CanvasKit._RTShaderFactory.prototype.make = function(floats, matrix) {
220 var fptr = copy1dArray(floats, CanvasKit.HEAPF32);
221 // Our array has 4 bytes per float, so be sure to account for that before
222 // sending it over the wire.
223 if (!matrix) {
224 return this._make(fptr, floats.length * 4);
225 }
226 return this._make(fptr, floats.length * 4, matrix);
227 }
228
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500229 CanvasKit.SkPath.prototype.addArc = function(oval, startAngle, sweepAngle) {
230 // see arc() for the HTMLCanvas version
231 // note input angles are degrees.
232 this._addArc(oval, startAngle, sweepAngle);
233 return this;
234 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400235
Kevin Lubicke384df42019-08-26 15:48:09 -0400236 CanvasKit.SkPath.prototype.addOval = function(oval, isCCW, startIndex) {
237 if (startIndex === undefined) {
238 startIndex = 1;
239 }
240 this._addOval(oval, !!isCCW, startIndex);
241 return this;
242 };
243
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500244 CanvasKit.SkPath.prototype.addPath = function() {
245 // Takes 1, 2, 7, or 10 required args, where the first arg is always the path.
246 // The last arg is optional and chooses between add or extend mode.
247 // The options for the remaining args are:
248 // - an array of 6 or 9 parameters (perspective is optional)
249 // - the 9 parameters of a full matrix or
250 // the 6 non-perspective params of a matrix.
251 var args = Array.prototype.slice.call(arguments);
252 var path = args[0];
253 var extend = false;
254 if (typeof args[args.length-1] === "boolean") {
255 extend = args.pop();
256 }
257 if (args.length === 1) {
258 // Add path, unchanged. Use identity matrix
259 this._addPath(path, 1, 0, 0,
260 0, 1, 0,
261 0, 0, 1,
262 extend);
263 } else if (args.length === 2) {
264 // User provided the 9 params of a full matrix as an array.
265 var a = args[1];
266 this._addPath(path, a[0], a[1], a[2],
267 a[3], a[4], a[5],
268 a[6] || 0, a[7] || 0, a[8] || 1,
269 extend);
270 } else if (args.length === 7 || args.length === 10) {
271 // User provided the 9 params of a (full) matrix directly.
272 // (or just the 6 non perspective ones)
273 // These are in the same order as what Skia expects.
274 var a = args;
275 this._addPath(path, a[1], a[2], a[3],
276 a[4], a[5], a[6],
277 a[7] || 0, a[8] || 0, a[9] || 1,
278 extend);
279 } else {
280 SkDebug('addPath expected to take 1, 2, 7, or 10 required args. Got ' + args.length);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400281 return null;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500282 }
283 return this;
284 };
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400285
Kevin Lubick37ab53e2019-11-11 10:06:08 -0500286 // points is either an array of [x, y] where x and y are numbers or
287 // a typed array from Malloc where the even indices will be treated
288 // as x coordinates and the odd indices will be treated as y coordinates.
289 CanvasKit.SkPath.prototype.addPoly = function(points, close) {
290 var ptr;
291 var n;
292 // This was created with CanvasKit.Malloc, so assume the user has
293 // already been filled with data.
294 if (points['_ck']) {
295 ptr = points.byteOffset;
296 n = points.length/2;
297 } else {
298 ptr = copy2dArray(points, CanvasKit.HEAPF32);
299 n = points.length;
300 }
301 this._addPoly(ptr, n, close);
302 CanvasKit._free(ptr);
303 return this;
304 };
305
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500306 CanvasKit.SkPath.prototype.addRect = function() {
307 // Takes 1, 2, 4 or 5 args
308 // - SkRect
309 // - SkRect, isCCW
310 // - left, top, right, bottom
311 // - left, top, right, bottom, isCCW
312 if (arguments.length === 1 || arguments.length === 2) {
313 var r = arguments[0];
314 var ccw = arguments[1] || false;
315 this._addRect(r.fLeft, r.fTop, r.fRight, r.fBottom, ccw);
316 } else if (arguments.length === 4 || arguments.length === 5) {
317 var a = arguments;
318 this._addRect(a[0], a[1], a[2], a[3], a[4] || false);
319 } else {
320 SkDebug('addRect expected to take 1, 2, 4, or 5 args. Got ' + arguments.length);
Kevin Lubick217056c2018-09-20 17:39:31 -0400321 return null;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500322 }
323 return this;
324 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400325
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500326 CanvasKit.SkPath.prototype.addRoundRect = function() {
327 // Takes 3, 4, 6 or 7 args
328 // - SkRect, radii, ccw
329 // - SkRect, rx, ry, ccw
330 // - left, top, right, bottom, radii, ccw
331 // - left, top, right, bottom, rx, ry, ccw
332 var args = arguments;
333 if (args.length === 3 || args.length === 6) {
334 var radii = args[args.length-2];
335 } else if (args.length === 6 || args.length === 7){
336 // duplicate the given (rx, ry) pairs for each corner.
337 var rx = args[args.length-3];
338 var ry = args[args.length-2];
339 var radii = [rx, ry, rx, ry, rx, ry, rx, ry];
340 } else {
341 SkDebug('addRoundRect expected to take 3, 4, 6, or 7 args. Got ' + args.length);
342 return null;
343 }
344 if (radii.length !== 8) {
345 SkDebug('addRoundRect needs 8 radii provided. Got ' + radii.length);
346 return null;
347 }
348 var rptr = copy1dArray(radii, CanvasKit.HEAPF32);
349 if (args.length === 3 || args.length === 4) {
350 var r = args[0];
351 var ccw = args[args.length - 1];
352 this._addRoundRect(r.fLeft, r.fTop, r.fRight, r.fBottom, rptr, ccw);
353 } else if (args.length === 6 || args.length === 7) {
354 var a = args;
355 this._addRoundRect(a[0], a[1], a[2], a[3], rptr, ccw);
356 }
357 CanvasKit._free(rptr);
358 return this;
359 };
360
361 CanvasKit.SkPath.prototype.arc = function(x, y, radius, startAngle, endAngle, ccw) {
362 // emulates the HTMLCanvas behavior. See addArc() for the SkPath version.
363 // Note input angles are radians.
364 var bounds = CanvasKit.LTRBRect(x-radius, y-radius, x+radius, y+radius);
365 var sweep = radiansToDegrees(endAngle - startAngle) - (360 * !!ccw);
366 var temp = new CanvasKit.SkPath();
367 temp.addArc(bounds, radiansToDegrees(startAngle), sweep);
368 this.addPath(temp, true);
369 temp.delete();
370 return this;
371 };
372
373 CanvasKit.SkPath.prototype.arcTo = function() {
374 // takes 4, 5 or 7 args
375 // - 5 x1, y1, x2, y2, radius
376 // - 4 oval (as Rect), startAngle, sweepAngle, forceMoveTo
Kevin Lubicke384df42019-08-26 15:48:09 -0400377 // - 7 rx, ry, xAxisRotate, useSmallArc, isCCW, x, y
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500378 var args = arguments;
379 if (args.length === 5) {
380 this._arcTo(args[0], args[1], args[2], args[3], args[4]);
381 } else if (args.length === 4) {
382 this._arcTo(args[0], args[1], args[2], args[3]);
383 } else if (args.length === 7) {
Kevin Lubicke384df42019-08-26 15:48:09 -0400384 this._arcTo(args[0], args[1], args[2], !!args[3], !!args[4], args[5], args[6]);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500385 } else {
386 throw 'Invalid args for arcTo. Expected 4, 5, or 7, got '+ args.length;
387 }
388
389 return this;
390 };
391
392 CanvasKit.SkPath.prototype.close = function() {
393 this._close();
394 return this;
395 };
396
397 CanvasKit.SkPath.prototype.conicTo = function(x1, y1, x2, y2, w) {
398 this._conicTo(x1, y1, x2, y2, w);
399 return this;
400 };
401
402 CanvasKit.SkPath.prototype.cubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) {
403 this._cubicTo(cp1x, cp1y, cp2x, cp2y, x, y);
404 return this;
405 };
406
407 CanvasKit.SkPath.prototype.dash = function(on, off, phase) {
408 if (this._dash(on, off, phase)) {
Kevin Lubick217056c2018-09-20 17:39:31 -0400409 return this;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500410 }
411 return null;
412 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400413
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500414 CanvasKit.SkPath.prototype.lineTo = function(x, y) {
415 this._lineTo(x, y);
416 return this;
417 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400418
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500419 CanvasKit.SkPath.prototype.moveTo = function(x, y) {
420 this._moveTo(x, y);
421 return this;
422 };
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400423
Kevin Lubicke384df42019-08-26 15:48:09 -0400424 CanvasKit.SkPath.prototype.offset = function(dx, dy) {
425 this._transform(1, 0, dx,
426 0, 1, dy,
427 0, 0, 1);
428 return this;
429 };
430
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500431 CanvasKit.SkPath.prototype.op = function(otherPath, op) {
432 if (this._op(otherPath, op)) {
Kevin Lubick217056c2018-09-20 17:39:31 -0400433 return this;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500434 }
435 return null;
436 };
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400437
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500438 CanvasKit.SkPath.prototype.quadTo = function(cpx, cpy, x, y) {
439 this._quadTo(cpx, cpy, x, y);
440 return this;
441 };
442
Kevin Lubick79b71342019-11-01 14:36:52 -0400443 CanvasKit.SkPath.prototype.rArcTo = function(rx, ry, xAxisRotate, useSmallArc, isCCW, dx, dy) {
444 this._rArcTo(rx, ry, xAxisRotate, useSmallArc, isCCW, dx, dy);
445 return this;
446 };
447
448 CanvasKit.SkPath.prototype.rConicTo = function(dx1, dy1, dx2, dy2, w) {
449 this._rConicTo(dx1, dy1, dx2, dy2, w);
450 return this;
451 };
452
453 // These params are all relative
454 CanvasKit.SkPath.prototype.rCubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) {
455 this._rCubicTo(cp1x, cp1y, cp2x, cp2y, x, y);
456 return this;
457 };
458
459 CanvasKit.SkPath.prototype.rLineTo = function(dx, dy) {
460 this._rLineTo(dx, dy);
461 return this;
462 };
463
464 CanvasKit.SkPath.prototype.rMoveTo = function(dx, dy) {
465 this._rMoveTo(dx, dy);
466 return this;
467 };
468
469 // These params are all relative
470 CanvasKit.SkPath.prototype.rQuadTo = function(cpx, cpy, x, y) {
471 this._rQuadTo(cpx, cpy, x, y);
472 return this;
473 };
474
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500475 CanvasKit.SkPath.prototype.simplify = function() {
476 if (this._simplify()) {
477 return this;
478 }
479 return null;
480 };
481
482 CanvasKit.SkPath.prototype.stroke = function(opts) {
483 // Fill out any missing values with the default values.
484 /**
485 * See externs.js for this definition
486 * @type {StrokeOpts}
487 */
488 opts = opts || {};
489 opts.width = opts.width || 1;
490 opts.miter_limit = opts.miter_limit || 4;
491 opts.cap = opts.cap || CanvasKit.StrokeCap.Butt;
492 opts.join = opts.join || CanvasKit.StrokeJoin.Miter;
493 opts.precision = opts.precision || 1;
494 if (this._stroke(opts)) {
495 return this;
496 }
497 return null;
498 };
499
500 CanvasKit.SkPath.prototype.transform = function() {
501 // Takes 1 or 9 args
502 if (arguments.length === 1) {
503 // argument 1 should be a 6 or 9 element array.
504 var a = arguments[0];
505 this._transform(a[0], a[1], a[2],
506 a[3], a[4], a[5],
507 a[6] || 0, a[7] || 0, a[8] || 1);
508 } else if (arguments.length === 6 || arguments.length === 9) {
509 // these arguments are the 6 or 9 members of the matrix
510 var a = arguments;
511 this._transform(a[0], a[1], a[2],
512 a[3], a[4], a[5],
513 a[6] || 0, a[7] || 0, a[8] || 1);
514 } else {
515 throw 'transform expected to take 1 or 9 arguments. Got ' + arguments.length;
516 }
517 return this;
518 };
519 // isComplement is optional, defaults to false
520 CanvasKit.SkPath.prototype.trim = function(startT, stopT, isComplement) {
521 if (this._trim(startT, stopT, !!isComplement)) {
522 return this;
523 }
524 return null;
525 };
526
527 // bones should be a 3d array.
528 // Each bone is a 3x2 transformation matrix in column major order:
529 // | scaleX skewX transX |
530 // | skewY scaleY transY |
531 // and bones is an array of those matrices.
532 // Returns a copy of this (SkVertices) with the bones applied.
533 CanvasKit.SkVertices.prototype.applyBones = function(bones) {
534 var bPtr = copy3dArray(bones, CanvasKit.HEAPF32);
535 var vert = this._applyBones(bPtr, bones.length);
536 CanvasKit._free(bPtr);
537 return vert;
538 }
539
540 CanvasKit.SkImage.prototype.encodeToData = function() {
541 if (!arguments.length) {
542 return this._encodeToData();
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400543 }
Kevin Lubick53965c92018-10-11 08:51:55 -0400544
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500545 if (arguments.length === 2) {
546 var a = arguments;
547 return this._encodeToDataWithFormat(a[0], a[1]);
Alexander Khovansky3e119332018-11-15 02:01:19 +0300548 }
549
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500550 throw 'encodeToData expected to take 0 or 2 arguments. Got ' + arguments.length;
551 }
Kevin Lubick1ba9c4d2019-02-22 10:04:06 -0500552
Kevin Lubicka064c282019-04-04 09:28:53 -0400553 CanvasKit.SkImage.prototype.makeShader = function(xTileMode, yTileMode, localMatrix) {
554 if (localMatrix) {
555 // Add perspective args if not provided.
556 if (localMatrix.length === 6) {
557 localMatrix.push(0, 0, 1);
558 }
559 return this._makeShader(xTileMode, yTileMode, localMatrix);
560 } else {
561 return this._makeShader(xTileMode, yTileMode);
562 }
563 }
564
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400565 CanvasKit.SkImage.prototype.readPixels = function(imageInfo, srcX, srcY) {
566 var rowBytes;
567 switch (imageInfo.colorType){
568 case CanvasKit.ColorType.RGBA_8888:
569 rowBytes = imageInfo.width * 4; // 1 byte per channel == 4 bytes per pixel in 8888
570 break;
571 case CanvasKit.ColorType.RGBA_F32:
572 rowBytes = imageInfo.width * 16; // 4 bytes per channel == 16 bytes per pixel in F32
573 break;
574 default:
575 SkDebug("Colortype not yet supported");
576 return;
577 }
578 var pBytes = rowBytes * imageInfo.height;
579 var pPtr = CanvasKit._malloc(pBytes);
580
581 if (!this._readPixels(imageInfo, pPtr, rowBytes, srcX, srcY)) {
582 SkDebug("Could not read pixels with the given inputs");
583 return null;
584 }
585
586 // Put those pixels into a typed array of the right format and then
587 // make a copy with slice() that we can return.
588 var retVal = null;
589 switch (imageInfo.colorType){
590 case CanvasKit.ColorType.RGBA_8888:
591 retVal = new Uint8Array(CanvasKit.buffer, pPtr, pBytes).slice();
592 break;
593 case CanvasKit.ColorType.RGBA_F32:
594 retVal = new Float32Array(CanvasKit.buffer, pPtr, pBytes).slice();
595 break;
596 }
597
598 // Free the allocated pixels in the WASM memory
599 CanvasKit._free(pPtr);
600 return retVal;
601
602 }
603
Kevin Lubickee91c072019-03-29 10:39:52 -0400604 // atlas is an SkImage, e.g. from CanvasKit.MakeImageFromEncoded
605 // srcRects and dstXforms should be CanvasKit.SkRectBuilder and CanvasKit.RSXFormBuilder
606 // or just arrays of floats in groups of 4.
607 // colors, if provided, should be a CanvasKit.SkColorBuilder or array of SkColor
608 // (from CanvasKit.Color)
609 CanvasKit.SkCanvas.prototype.drawAtlas = function(atlas, srcRects, dstXforms, paint,
610 /*optional*/ blendMode, colors) {
611 if (!atlas || !paint || !srcRects || !dstXforms) {
612 SkDebug('Doing nothing since missing a required input');
613 return;
614 }
615 if (srcRects.length !== dstXforms.length || (colors && colors.length !== dstXforms.length)) {
616 SkDebug('Doing nothing since input arrays length mismatches');
617 }
618 if (!blendMode) {
619 blendMode = CanvasKit.BlendMode.SrcOver;
620 }
621
622 var srcRectPtr;
623 if (srcRects.build) {
624 srcRectPtr = srcRects.build();
625 } else {
626 srcRectPtr = copy1dArray(srcRects, CanvasKit.HEAPF32);
627 }
628
629 var dstXformPtr;
630 if (dstXforms.build) {
631 dstXformPtr = dstXforms.build();
632 } else {
633 dstXformPtr = copy1dArray(dstXforms, CanvasKit.HEAPF32);
634 }
635
636 var colorPtr = 0; // enscriptem doesn't like undefined for nullptr
637 if (colors) {
638 if (colors.build) {
639 colorPtr = colors.build();
640 } else {
641 colorPtr = copy1dArray(colors, CanvasKit.HEAPU32);
642 }
643 }
644
645 this._drawAtlas(atlas, dstXformPtr, srcRectPtr, colorPtr, dstXforms.length,
646 blendMode, paint);
647
648 if (srcRectPtr && !srcRects.build) {
649 CanvasKit._free(srcRectPtr);
650 }
651 if (dstXformPtr && !dstXforms.build) {
652 CanvasKit._free(dstXformPtr);
653 }
654 if (colorPtr && !colors.build) {
655 CanvasKit._free(colorPtr);
656 }
657
658 }
659
Kevin Lubick37ab53e2019-11-11 10:06:08 -0500660 // points is either an array of [x, y] where x and y are numbers or
661 // a typed array from Malloc where the even indices will be treated
662 // as x coordinates and the odd indices will be treated as y coordinates.
663 CanvasKit.SkCanvas.prototype.drawPoints = function(mode, points, paint) {
664 var ptr;
665 var n;
666 // This was created with CanvasKit.Malloc, so assume the user has
667 // already been filled with data.
668 if (points['_ck']) {
669 ptr = points.byteOffset;
670 n = points.length/2;
671 } else {
672 ptr = copy2dArray(points, CanvasKit.HEAPF32);
673 n = points.length;
674 }
675 this._drawPoints(mode, ptr, n, paint);
676 CanvasKit._free(ptr);
677 }
678
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500679 // str can be either a text string or a ShapedText object
680 CanvasKit.SkCanvas.prototype.drawText = function(str, x, y, paint, font) {
681 if (typeof str === 'string') {
Kevin Lubickec4903d2019-01-14 08:36:08 -0500682 // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
683 // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
Kevin Lubick16d998f2019-09-26 13:25:26 -0400684 var strLen = lengthBytesUTF8(str);
685 // Add 1 for null terminator, which we need when copying/converting, but can ignore
686 // when we call into Skia.
687 var strPtr = CanvasKit._malloc(strLen + 1);
688 stringToUTF8(str, strPtr, strLen + 1);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500689 this._drawSimpleText(strPtr, strLen, x, y, font, paint);
Kevin Lubickda3d8ac2019-01-07 11:08:55 -0500690 } else {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500691 this._drawShapedText(str, x, y, paint);
Kevin Lubickd29edd72018-12-07 08:29:52 -0500692 }
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400693 }
694
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500695 // returns Uint8Array
696 CanvasKit.SkCanvas.prototype.readPixels = function(x, y, w, h, alphaType,
697 colorType, dstRowBytes) {
698 // supply defaults (which are compatible with HTMLCanvas's getImageData)
699 alphaType = alphaType || CanvasKit.AlphaType.Unpremul;
700 colorType = colorType || CanvasKit.ColorType.RGBA_8888;
701 dstRowBytes = dstRowBytes || (4 * w);
702
703 var len = h * dstRowBytes
704 var pptr = CanvasKit._malloc(len);
705 var ok = this._readPixels({
706 'width': w,
707 'height': h,
Kevin Lubick52b9f372018-12-04 13:57:36 -0500708 'colorType': colorType,
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500709 'alphaType': alphaType,
710 }, pptr, dstRowBytes, x, y);
711 if (!ok) {
712 CanvasKit._free(pptr);
713 return null;
714 }
715
716 // The first typed array is just a view into memory. Because we will
717 // be free-ing that, we call slice to make a persistent copy.
Kevin Lubickfa5a1382019-10-09 10:46:14 -0400718 var pixels = new Uint8Array(CanvasKit.buffer, pptr, len).slice();
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500719 CanvasKit._free(pptr);
720 return pixels;
721 }
722
723 // pixels is a TypedArray. No matter the input size, it will be treated as
724 // a Uint8Array (essentially, a byte array).
725 CanvasKit.SkCanvas.prototype.writePixels = function(pixels, srcWidth, srcHeight,
726 destX, destY, alphaType, colorType) {
727 if (pixels.byteLength % (srcWidth * srcHeight)) {
728 throw 'pixels length must be a multiple of the srcWidth * srcHeight';
729 }
730 var bytesPerPixel = pixels.byteLength / (srcWidth * srcHeight);
731 // supply defaults (which are compatible with HTMLCanvas's putImageData)
732 alphaType = alphaType || CanvasKit.AlphaType.Unpremul;
733 colorType = colorType || CanvasKit.ColorType.RGBA_8888;
734 var srcRowBytes = bytesPerPixel * srcWidth;
735
Kevin Lubick52b9f372018-12-04 13:57:36 -0500736 var pptr = CanvasKit._malloc(pixels.byteLength);
737 CanvasKit.HEAPU8.set(pixels, pptr);
Kevin Lubick52b9f372018-12-04 13:57:36 -0500738
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500739 var ok = this._writePixels({
740 'width': srcWidth,
741 'height': srcHeight,
742 'colorType': colorType,
743 'alphaType': alphaType,
744 }, pptr, srcRowBytes, destX, destY);
745
746 CanvasKit._free(pptr);
747 return ok;
Kevin Lubick52b9f372018-12-04 13:57:36 -0500748 }
749
Kevin Lubickd3729342019-09-12 11:11:25 -0400750 // colorMatrix is an SkColorMatrix (e.g. Float32Array of length 20)
751 CanvasKit.SkColorFilter.MakeMatrix = function(colorMatrix) {
752 if (!colorMatrix || colorMatrix.length !== 20) {
753 SkDebug('ignoring invalid color matrix');
754 return;
755 }
756 var fptr = copy1dArray(colorMatrix, CanvasKit.HEAPF32);
757 // We know skia memcopies the floats, so we can free our memory after the call returns.
758 var m = CanvasKit.SkColorFilter._makeMatrix(fptr);
759 CanvasKit._free(fptr);
760 return m;
761 }
762
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400763 // Returns an array of the widths of the glyphs in this string.
764 CanvasKit.SkFont.prototype.getWidths = function(str) {
765 // add 1 for null terminator
766 var codePoints = str.length + 1;
767 // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
768 // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
769 // Add 1 for null terminator
770 var strBytes = lengthBytesUTF8(str) + 1;
771 var strPtr = CanvasKit._malloc(strBytes);
772 stringToUTF8(str, strPtr, strBytes);
773
774 var bytesPerFloat = 4;
775 // allocate widths == numCodePoints
776 var widthPtr = CanvasKit._malloc(codePoints * bytesPerFloat);
777 if (!this._getWidths(strPtr, strBytes, codePoints, widthPtr)) {
778 SkDebug('Could not compute widths');
779 CanvasKit._free(strPtr);
780 CanvasKit._free(widthPtr);
781 return null;
782 }
783 // reminder, this shouldn't copy the data, just is a nice way to
784 // wrap 4 bytes together into a float.
785 var widths = new Float32Array(CanvasKit.buffer, widthPtr, codePoints);
786 // This copies the data so we can free the CanvasKit memory
787 var retVal = Array.from(widths);
788 CanvasKit._free(strPtr);
789 CanvasKit._free(widthPtr);
790 return retVal;
791 }
792
Kevin Lubick369f6a52019-10-03 11:22:08 -0400793 // arguments should all be arrayBuffers or be an array of arrayBuffers.
Kevin Lubick61887c72019-09-26 13:20:50 -0400794 CanvasKit.SkFontMgr.FromData = function() {
795 if (!arguments.length) {
796 SkDebug('Could not make SkFontMgr from no font sources');
797 return null;
798 }
799 var fonts = arguments;
Kevin Lubick369f6a52019-10-03 11:22:08 -0400800 if (fonts.length === 1 && Array.isArray(fonts[0])) {
Kevin Lubick61887c72019-09-26 13:20:50 -0400801 fonts = arguments[0];
802 }
803 if (!fonts.length) {
804 SkDebug('Could not make SkFontMgr from no font sources');
805 return null;
806 }
807 var dPtrs = [];
808 var sizes = [];
809 for (var i = 0; i < fonts.length; i++) {
810 var data = new Uint8Array(fonts[i]);
811 var dptr = copy1dArray(data, CanvasKit.HEAPU8);
812 dPtrs.push(dptr);
813 sizes.push(data.byteLength);
814 }
815 // Pointers are 32 bit unsigned ints
816 var datasPtr = copy1dArray(dPtrs, CanvasKit.HEAPU32);
817 var sizesPtr = copy1dArray(sizes, CanvasKit.HEAPU32);
818 var fm = CanvasKit.SkFontMgr._fromData(datasPtr, sizesPtr, fonts.length);
819 // The SkFontMgr has taken ownership of the bytes we allocated in the for loop.
820 CanvasKit._free(datasPtr);
821 CanvasKit._free(sizesPtr);
822 return fm;
823 }
824
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500825 // fontData should be an arrayBuffer
826 CanvasKit.SkFontMgr.prototype.MakeTypefaceFromData = function(fontData) {
827 var data = new Uint8Array(fontData);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400828
Kevin Lubick61887c72019-09-26 13:20:50 -0400829 var fptr = copy1dArray(data, CanvasKit.HEAPU8);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500830 var font = this._makeTypefaceFromData(fptr, data.byteLength);
831 if (!font) {
832 SkDebug('Could not decode font data');
833 // We do not need to free the data since the C++ will do that for us
834 // when the font is deleted (or fails to decode);
835 return null;
836 }
837 return font;
838 }
839
Kevin Lubickcc13fd32019-04-05 13:00:01 -0400840 // The serialized format of an SkPicture (informally called an "skp"), is not something
841 // that clients should ever rely on. It is useful when filing bug reports, but that's
842 // about it. The format may change at anytime and no promises are made for backwards
843 // or forward compatibility.
844 CanvasKit.SkPicture.prototype.DEBUGONLY_saveAsFile = function(skpName) {
845 var data = this.DEBUGONLY_serialize();
846 if (!data) {
847 SkDebug('Could not serialize to skpicture.');
848 return;
849 }
850 var bytes = CanvasKit.getSkDataBytes(data);
851 saveBytesToFile(bytes, skpName);
852 data.delete();
853 }
854
Kevin Lubick62836902019-12-09 09:04:26 -0500855 CanvasKit.SkShader.Blend = function(mode, dst, src, localMatrix) {
856 if (!localMatrix) {
857 return this._Blend(mode, dst, src);
858 }
859 return this._Blend(mode, dst, src, localMatrix);
860 }
861
862 CanvasKit.SkShader.Lerp = function(t, dst, src, localMatrix) {
863 if (!localMatrix) {
864 return this._Lerp(t, dst, src);
865 }
866 return this._Lerp(t, dst, src, localMatrix);
867 }
868
Kevin Lubickcc13fd32019-04-05 13:00:01 -0400869 CanvasKit.SkSurface.prototype.captureFrameAsSkPicture = function(drawFrame) {
870 // Set up SkPictureRecorder
871 var spr = new CanvasKit.SkPictureRecorder();
872 var canvas = spr.beginRecording(
873 CanvasKit.LTRBRect(0, 0, this.width(), this.height()));
874 drawFrame(canvas);
875 var pic = spr.finishRecordingAsPicture();
876 spr.delete();
877 // TODO: do we need to clean up the memory for canvas?
878 // If we delete it here, saveAsFile doesn't work correctly.
879 return pic;
880 }
881
Kevin Lubick359a7e32019-03-19 09:34:37 -0400882 CanvasKit.SkSurface.prototype.requestAnimationFrame = function(callback, dirtyRect) {
883 if (!this._cached_canvas) {
884 this._cached_canvas = this.getCanvas();
885 }
886 window.requestAnimationFrame(function() {
Kevin Lubick39026282019-03-28 12:46:40 -0400887 if (this._context !== undefined) {
888 CanvasKit.setCurrentContext(this._context);
889 }
Kevin Lubick359a7e32019-03-19 09:34:37 -0400890
891 callback(this._cached_canvas);
892
893 this.flush();
894 }.bind(this));
895 }
896
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400897 CanvasKit.SkTextBlob.MakeOnPath = function(str, path, font, initialOffset) {
898 if (!str || !str.length) {
899 SkDebug('ignoring 0 length string');
900 return;
901 }
902 if (!path || !path.countPoints()) {
903 SkDebug('ignoring empty path');
904 return;
905 }
906 if (path.countPoints() === 1) {
907 SkDebug('path has 1 point, returning normal textblob');
908 return this.MakeFromText(str, font);
909 }
910
911 if (!initialOffset) {
912 initialOffset = 0;
913 }
914
915 var widths = font.getWidths(str);
916
917 var rsx = new CanvasKit.RSXFormBuilder();
918 var meas = new CanvasKit.SkPathMeasure(path, false, 1);
919 var dist = initialOffset;
920 for (var i = 0; i < str.length; i++) {
921 var width = widths[i];
922 dist += width/2;
923 if (dist > meas.getLength()) {
924 // jump to next contour
925 if (!meas.nextContour()) {
926 // We have come to the end of the path - terminate the string
927 // right here.
928 str = str.substring(0, i);
929 break;
930 }
931 dist = width/2;
932 }
933
934 // Gives us the (x, y) coordinates as well as the cos/sin of the tangent
935 // line at that position.
936 var xycs = meas.getPosTan(dist);
937 var cx = xycs[0];
938 var cy = xycs[1];
939 var cosT = xycs[2];
940 var sinT = xycs[3];
941
942 var adjustedX = cx - (width/2 * cosT);
943 var adjustedY = cy - (width/2 * sinT);
944
945 rsx.push(cosT, sinT, adjustedX, adjustedY);
946 dist += width/2;
947 }
948 var retVal = this.MakeFromRSXform(str, rsx, font);
949 rsx.delete();
950 meas.delete();
951 return retVal;
952 }
953
954 CanvasKit.SkTextBlob.MakeFromRSXform = function(str, rsxBuilder, font) {
955 // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
956 // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
957 // Add 1 for null terminator
958 var strLen = lengthBytesUTF8(str) + 1;
959 var strPtr = CanvasKit._malloc(strLen);
960 // Add 1 for the null terminator.
961 stringToUTF8(str, strPtr, strLen);
962 var rptr = rsxBuilder.build();
963
964 var blob = CanvasKit.SkTextBlob._MakeFromRSXform(strPtr, strLen - 1,
965 rptr, font, CanvasKit.TextEncoding.UTF8);
966 if (!blob) {
967 SkDebug('Could not make textblob from string "' + str + '"');
968 return null;
969 }
970
971 var origDelete = blob.delete.bind(blob);
972 blob.delete = function() {
973 CanvasKit._free(strPtr);
974 origDelete();
975 }
976 return blob;
977 }
978
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500979 CanvasKit.SkTextBlob.MakeFromText = function(str, font) {
980 // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
981 // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
982 // Add 1 for null terminator
983 var strLen = lengthBytesUTF8(str) + 1;
984 var strPtr = CanvasKit._malloc(strLen);
985 // Add 1 for the null terminator.
986 stringToUTF8(str, strPtr, strLen);
987
988 var blob = CanvasKit.SkTextBlob._MakeFromText(strPtr, strLen - 1, font, CanvasKit.TextEncoding.UTF8);
989 if (!blob) {
990 SkDebug('Could not make textblob from string "' + str + '"');
991 return null;
Kevin Lubick217056c2018-09-20 17:39:31 -0400992 }
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400993
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500994 var origDelete = blob.delete.bind(blob);
995 blob.delete = function() {
996 CanvasKit._free(strPtr);
997 origDelete();
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400998 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500999 return blob;
Kevin Lubickb5ae3b52018-11-03 07:51:19 -04001000 }
1001
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001002 // Run through the JS files that are added at compile time.
1003 if (CanvasKit._extraInitializations) {
1004 CanvasKit._extraInitializations.forEach(function(init) {
1005 init();
1006 });
Kevin Lubickeb2f6b02018-11-29 15:07:02 -05001007 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001008}; // end CanvasKit.onRuntimeInitialized, that is, anything changing prototypes or dynamic.
Kevin Lubickeb2f6b02018-11-29 15:07:02 -05001009
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001010CanvasKit.LTRBRect = function(l, t, r, b) {
1011 return {
1012 fLeft: l,
1013 fTop: t,
1014 fRight: r,
1015 fBottom: b,
1016 };
Kevin Lubick1a05fce2018-11-20 12:51:16 -05001017}
1018
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001019CanvasKit.XYWHRect = function(x, y, w, h) {
1020 return {
1021 fLeft: x,
1022 fTop: y,
1023 fRight: x+w,
1024 fBottom: y+h,
1025 };
Kevin Lubick1a05fce2018-11-20 12:51:16 -05001026}
1027
Kevin Lubick7d644e12019-09-11 14:22:22 -04001028// RRectXY returns an RRect with the given rect and a radiusX and radiusY for
1029// all 4 corners.
1030CanvasKit.RRectXY = function(rect, rx, ry) {
1031 return {
1032 rect: rect,
1033 rx1: rx,
1034 ry1: ry,
1035 rx2: rx,
1036 ry2: ry,
1037 rx3: rx,
1038 ry3: ry,
1039 rx4: rx,
1040 ry4: ry,
1041 };
1042}
1043
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001044CanvasKit.MakePathFromCmds = function(cmds) {
1045 var ptrLen = loadCmdsTypedArray(cmds);
1046 var path = CanvasKit._MakePathFromCmds(ptrLen[0], ptrLen[1]);
1047 CanvasKit._free(ptrLen[0]);
1048 return path;
1049}
1050
1051CanvasKit.MakeSkDashPathEffect = function(intervals, phase) {
1052 if (!phase) {
1053 phase = 0;
1054 }
1055 if (!intervals.length || intervals.length % 2 === 1) {
1056 throw 'Intervals array must have even length';
1057 }
1058 var ptr = copy1dArray(intervals, CanvasKit.HEAPF32);
1059 var dpe = CanvasKit._MakeSkDashPathEffect(ptr, intervals.length, phase);
1060 CanvasKit._free(ptr);
1061 return dpe;
1062}
1063
1064// data is a TypedArray or ArrayBuffer e.g. from fetch().then(resp.arrayBuffer())
Kevin Lubick6b921b72019-09-18 16:18:17 -04001065CanvasKit.MakeAnimatedImageFromEncoded = function(data) {
1066 data = new Uint8Array(data);
1067
1068 var iptr = CanvasKit._malloc(data.byteLength);
1069 CanvasKit.HEAPU8.set(data, iptr);
1070 var img = CanvasKit._decodeAnimatedImage(iptr, data.byteLength);
1071 if (!img) {
1072 SkDebug('Could not decode animated image');
1073 return null;
1074 }
1075 return img;
1076}
1077
1078// data is a TypedArray or ArrayBuffer e.g. from fetch().then(resp.arrayBuffer())
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001079CanvasKit.MakeImageFromEncoded = function(data) {
1080 data = new Uint8Array(data);
1081
1082 var iptr = CanvasKit._malloc(data.byteLength);
1083 CanvasKit.HEAPU8.set(data, iptr);
1084 var img = CanvasKit._decodeImage(iptr, data.byteLength);
1085 if (!img) {
1086 SkDebug('Could not decode image');
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001087 return null;
1088 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001089 return img;
1090}
1091
Kevin Lubickeda0b432019-12-02 08:26:48 -05001092// pixels must be a Uint8Array with bytes representing the pixel values
1093// (e.g. each set of 4 bytes could represent RGBA values for a single pixel).
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001094CanvasKit.MakeImage = function(pixels, width, height, alphaType, colorType) {
Kevin Lubickeda0b432019-12-02 08:26:48 -05001095 var bytesPerPixel = pixels.length / (width * height);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001096 var info = {
1097 'width': width,
1098 'height': height,
1099 'alphaType': alphaType,
1100 'colorType': colorType,
1101 };
Kevin Lubickeda0b432019-12-02 08:26:48 -05001102 var pptr = copy1dArray(pixels, CanvasKit.HEAPU8);
1103 // No need to _free pptr, Image takes it with SkData::MakeFromMalloc
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001104
Kevin Lubickeda0b432019-12-02 08:26:48 -05001105 return CanvasKit._MakeImage(info, pptr, pixels.length, width * bytesPerPixel);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001106}
1107
1108CanvasKit.MakeLinearGradientShader = function(start, end, colors, pos, mode, localMatrix, flags) {
Kevin Lubickee91c072019-03-29 10:39:52 -04001109 var colorPtr = copy1dArray(colors, CanvasKit.HEAPU32);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001110 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
1111 flags = flags || 0;
1112
1113 if (localMatrix) {
1114 // Add perspective args if not provided.
1115 if (localMatrix.length === 6) {
1116 localMatrix.push(0, 0, 1);
1117 }
1118 var lgs = CanvasKit._MakeLinearGradientShader(start, end, colorPtr, posPtr,
1119 colors.length, mode, flags, localMatrix);
1120 } else {
1121 var lgs = CanvasKit._MakeLinearGradientShader(start, end, colorPtr, posPtr,
1122 colors.length, mode, flags);
1123 }
1124
1125 CanvasKit._free(colorPtr);
1126 CanvasKit._free(posPtr);
1127 return lgs;
1128}
1129
1130CanvasKit.MakeRadialGradientShader = function(center, radius, colors, pos, mode, localMatrix, flags) {
Kevin Lubickee91c072019-03-29 10:39:52 -04001131 var colorPtr = copy1dArray(colors, CanvasKit.HEAPU32);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001132 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
1133 flags = flags || 0;
1134
1135 if (localMatrix) {
1136 // Add perspective args if not provided.
1137 if (localMatrix.length === 6) {
1138 localMatrix.push(0, 0, 1);
1139 }
1140 var rgs = CanvasKit._MakeRadialGradientShader(center, radius, colorPtr, posPtr,
1141 colors.length, mode, flags, localMatrix);
1142 } else {
1143 var rgs = CanvasKit._MakeRadialGradientShader(center, radius, colorPtr, posPtr,
1144 colors.length, mode, flags);
1145 }
1146
1147 CanvasKit._free(colorPtr);
1148 CanvasKit._free(posPtr);
1149 return rgs;
1150}
1151
1152CanvasKit.MakeTwoPointConicalGradientShader = function(start, startRadius, end, endRadius,
1153 colors, pos, mode, localMatrix, flags) {
Kevin Lubickee91c072019-03-29 10:39:52 -04001154 var colorPtr = copy1dArray(colors, CanvasKit.HEAPU32);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001155 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
1156 flags = flags || 0;
1157
1158 if (localMatrix) {
1159 // Add perspective args if not provided.
1160 if (localMatrix.length === 6) {
1161 localMatrix.push(0, 0, 1);
1162 }
1163 var rgs = CanvasKit._MakeTwoPointConicalGradientShader(
1164 start, startRadius, end, endRadius,
1165 colorPtr, posPtr, colors.length, mode, flags, localMatrix);
1166 } else {
1167 var rgs = CanvasKit._MakeTwoPointConicalGradientShader(
1168 start, startRadius, end, endRadius,
1169 colorPtr, posPtr, colors.length, mode, flags);
1170 }
1171
1172 CanvasKit._free(colorPtr);
1173 CanvasKit._free(posPtr);
1174 return rgs;
1175}
1176
1177CanvasKit.MakeSkVertices = function(mode, positions, textureCoordinates, colors,
Kevin Lubickb3574c92019-03-06 08:25:36 -05001178 boneIndices, boneWeights, indices, isVolatile) {
Kevin Lubickb3574c92019-03-06 08:25:36 -05001179 // Default isVolitile to true if not set
1180 isVolatile = isVolatile === undefined ? true : isVolatile;
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001181 var idxCount = (indices && indices.length) || 0;
1182
1183 var flags = 0;
1184 // These flags are from SkVertices.h and should be kept in sync with those.
1185 if (textureCoordinates && textureCoordinates.length) {
1186 flags |= (1 << 0);
1187 }
1188 if (colors && colors.length) {
1189 flags |= (1 << 1);
1190 }
1191 if (boneIndices && boneIndices.length) {
1192 flags |= (1 << 2);
1193 }
1194 if (!isVolatile) {
1195 flags |= (1 << 3);
1196 }
1197
1198 var builder = new CanvasKit._SkVerticesBuilder(mode, positions.length, idxCount, flags);
1199
1200 copy2dArray(positions, CanvasKit.HEAPF32, builder.positions());
1201 if (builder.texCoords()) {
1202 copy2dArray(textureCoordinates, CanvasKit.HEAPF32, builder.texCoords());
1203 }
1204 if (builder.colors()) {
1205 copy1dArray(colors, CanvasKit.HEAPU32, builder.colors());
1206 }
1207 if (builder.boneIndices()) {
1208 copy2dArray(boneIndices, CanvasKit.HEAP32, builder.boneIndices());
1209 }
1210 if (builder.boneWeights()) {
1211 copy2dArray(boneWeights, CanvasKit.HEAPF32, builder.boneWeights());
1212 }
1213 if (builder.indices()) {
1214 copy1dArray(indices, CanvasKit.HEAPU16, builder.indices());
1215 }
Kevin Lubickb3574c92019-03-06 08:25:36 -05001216
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001217 var idxCount = (indices && indices.length) || 0;
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001218 // Create the vertices, which owns the memory that the builder had allocated.
1219 return builder.detach();
Kevin Lubick4b5b6452019-12-06 13:55:58 -05001220};