blob: 02c415fc7305d8e20f0e46b28e2999864f1263dd [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 Lubickf5ea37f2019-02-28 10:06:18 -050011 // Add some helpers for matrices. This is ported from SkMatrix.cpp
12 // to save complexity and overhead of going back and forth between
13 // C++ and JS layers.
14 // I would have liked to use something like DOMMatrix, except it
15 // isn't widely supported (would need polyfills) and it doesn't
16 // have a mapPoints() function (which could maybe be tacked on here).
17 // If DOMMatrix catches on, it would be worth re-considering this usage.
18 CanvasKit.SkMatrix = {};
19 function sdot(a, b, c, d, e, f) {
20 e = e || 0;
21 f = f || 0;
22 return a * b + c * d + e * f;
23 }
24
25 CanvasKit.SkMatrix.identity = function() {
26 return [
27 1, 0, 0,
28 0, 1, 0,
29 0, 0, 1,
30 ];
31 };
32
33 // Return the inverse (if it exists) of this matrix.
34 // Otherwise, return the identity.
35 CanvasKit.SkMatrix.invert = function(m) {
36 var det = m[0]*m[4]*m[8] + m[1]*m[5]*m[6] + m[2]*m[3]*m[7]
37 - m[2]*m[4]*m[6] - m[1]*m[3]*m[8] - m[0]*m[5]*m[7];
38 if (!det) {
39 SkDebug('Warning, uninvertible matrix');
40 return CanvasKit.SkMatrix.identity();
Kevin Lubick1a05fce2018-11-20 12:51:16 -050041 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050042 return [
43 (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,
44 (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,
45 (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,
46 ];
47 };
Kevin Lubick1a05fce2018-11-20 12:51:16 -050048
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050049 // Maps the given points according to the passed in matrix.
50 // Results are done in place.
51 // See SkMatrix.h::mapPoints for the docs on the math.
52 CanvasKit.SkMatrix.mapPoints = function(matrix, ptArr) {
53 if (ptArr.length % 2) {
54 throw 'mapPoints requires an even length arr';
Kevin Lubickb9db3902018-11-26 11:47:54 -050055 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050056 for (var i = 0; i < ptArr.length; i+=2) {
57 var x = ptArr[i], y = ptArr[i+1];
58 // Gx+Hy+I
59 var denom = matrix[6]*x + matrix[7]*y + matrix[8];
60 // Ax+By+C
61 var xTrans = matrix[0]*x + matrix[1]*y + matrix[2];
62 // Dx+Ey+F
63 var yTrans = matrix[3]*x + matrix[4]*y + matrix[5];
64 ptArr[i] = xTrans/denom;
65 ptArr[i+1] = yTrans/denom;
66 }
67 return ptArr;
68 };
Kevin Lubickb9db3902018-11-26 11:47:54 -050069
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050070 CanvasKit.SkMatrix.multiply = function(m1, m2) {
71 var result = [0,0,0, 0,0,0, 0,0,0];
72 for (var r = 0; r < 3; r++) {
73 for (var c = 0; c < 3; c++) {
74 // m1 and m2 are 1D arrays pretending to be 2D arrays
75 result[3*r + c] = sdot(m1[3*r + 0], m2[3*0 + c],
76 m1[3*r + 1], m2[3*1 + c],
77 m1[3*r + 2], m2[3*2 + c]);
Kevin Lubick1a05fce2018-11-20 12:51:16 -050078 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050079 }
80 return result;
81 }
Kevin Lubick1a05fce2018-11-20 12:51:16 -050082
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050083 // Return a matrix representing a rotation by n radians.
84 // px, py optionally say which point the rotation should be around
85 // with the default being (0, 0);
86 CanvasKit.SkMatrix.rotated = function(radians, px, py) {
87 px = px || 0;
88 py = py || 0;
89 var sinV = Math.sin(radians);
90 var cosV = Math.cos(radians);
91 return [
92 cosV, -sinV, sdot( sinV, py, 1 - cosV, px),
93 sinV, cosV, sdot(-sinV, px, 1 - cosV, py),
94 0, 0, 1,
95 ];
96 };
Kevin Lubick217056c2018-09-20 17:39:31 -040097
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050098 CanvasKit.SkMatrix.scaled = function(sx, sy, px, py) {
99 px = px || 0;
100 py = py || 0;
101 return [
102 sx, 0, px - sx * px,
103 0, sy, py - sy * py,
104 0, 0, 1,
105 ];
106 };
Kevin Lubickda3d8ac2019-01-07 11:08:55 -0500107
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500108 CanvasKit.SkMatrix.skewed = function(kx, ky, px, py) {
109 px = px || 0;
110 py = py || 0;
111 return [
112 1, kx, -kx * px,
113 ky, 1, -ky * py,
114 0, 0, 1,
115 ];
116 };
Alexander Khovansky3e119332018-11-15 02:01:19 +0300117
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500118 CanvasKit.SkMatrix.translated = function(dx, dy) {
119 return [
120 1, 0, dx,
121 0, 1, dy,
122 0, 0, 1,
123 ];
124 };
Kevin Lubick1646e7d2018-12-07 13:03:08 -0500125
Kevin Lubickd3729342019-09-12 11:11:25 -0400126 // An SkColorMatrix is a 4x4 color matrix that transforms the 4 color channels
127 // with a 1x4 matrix that post-translates those 4 channels.
128 // For example, the following is the layout with the scale (S) and post-transform
129 // (PT) items indicated.
130 // RS, 0, 0, 0 | RPT
131 // 0, GS, 0, 0 | GPT
132 // 0, 0, BS, 0 | BPT
133 // 0, 0, 0, AS | APT
134 //
135 // Much of this was hand-transcribed from SkColorMatrix.cpp, because it's easier to
136 // deal with a Float32Array of length 20 than to try to expose the SkColorMatrix object.
137
138 var rScale = 0;
139 var gScale = 6;
140 var bScale = 12;
141 var aScale = 18;
142
143 var rPostTrans = 4;
144 var gPostTrans = 9;
145 var bPostTrans = 14;
146 var aPostTrans = 19;
147
148 CanvasKit.SkColorMatrix = {};
149 CanvasKit.SkColorMatrix.identity = function() {
150 var m = new Float32Array(20);
151 m[rScale] = 1;
152 m[gScale] = 1;
153 m[bScale] = 1;
154 m[aScale] = 1;
155 return m;
156 }
157
158 CanvasKit.SkColorMatrix.scaled = function(rs, gs, bs, as) {
159 var m = new Float32Array(20);
160 m[rScale] = rs;
161 m[gScale] = gs;
162 m[bScale] = bs;
163 m[aScale] = as;
164 return m;
165 }
166
167 var rotateIndices = [
168 [6, 7, 11, 12],
169 [0, 10, 2, 12],
170 [0, 1, 5, 6],
171 ];
172 // axis should be 0, 1, 2 for r, g, b
173 CanvasKit.SkColorMatrix.rotated = function(axis, sine, cosine) {
174 var m = CanvasKit.SkColorMatrix.identity();
175 var indices = rotateIndices[axis];
176 m[indices[0]] = cosine;
177 m[indices[1]] = sine;
178 m[indices[2]] = -sine;
179 m[indices[3]] = cosine;
180 return m;
181 }
182
183 // m is a SkColorMatrix (i.e. a Float32Array), and this sets the 4 "special"
184 // params that will translate the colors after they are multiplied by the 4x4 matrix.
185 CanvasKit.SkColorMatrix.postTranslate = function(m, dr, dg, db, da) {
186 m[rPostTrans] += dr;
187 m[gPostTrans] += dg;
188 m[bPostTrans] += db;
189 m[aPostTrans] += da;
190 return m;
191 }
192
193 // concat returns a new SkColorMatrix that is the result of multiplying outer*inner;
194 CanvasKit.SkColorMatrix.concat = function(outer, inner) {
195 var m = new Float32Array(20);
196 var index = 0;
197 for (var j = 0; j < 20; j += 5) {
198 for (var i = 0; i < 4; i++) {
199 m[index++] = outer[j + 0] * inner[i + 0] +
200 outer[j + 1] * inner[i + 5] +
201 outer[j + 2] * inner[i + 10] +
202 outer[j + 3] * inner[i + 15];
203 }
204 m[index++] = outer[j + 0] * inner[4] +
205 outer[j + 1] * inner[9] +
206 outer[j + 2] * inner[14] +
207 outer[j + 3] * inner[19] +
208 outer[j + 4];
209 }
210
211 return m;
212 }
213
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500214 CanvasKit.SkPath.prototype.addArc = function(oval, startAngle, sweepAngle) {
215 // see arc() for the HTMLCanvas version
216 // note input angles are degrees.
217 this._addArc(oval, startAngle, sweepAngle);
218 return this;
219 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400220
Kevin Lubicke384df42019-08-26 15:48:09 -0400221 CanvasKit.SkPath.prototype.addOval = function(oval, isCCW, startIndex) {
222 if (startIndex === undefined) {
223 startIndex = 1;
224 }
225 this._addOval(oval, !!isCCW, startIndex);
226 return this;
227 };
228
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500229 CanvasKit.SkPath.prototype.addPath = function() {
230 // Takes 1, 2, 7, or 10 required args, where the first arg is always the path.
231 // The last arg is optional and chooses between add or extend mode.
232 // The options for the remaining args are:
233 // - an array of 6 or 9 parameters (perspective is optional)
234 // - the 9 parameters of a full matrix or
235 // the 6 non-perspective params of a matrix.
236 var args = Array.prototype.slice.call(arguments);
237 var path = args[0];
238 var extend = false;
239 if (typeof args[args.length-1] === "boolean") {
240 extend = args.pop();
241 }
242 if (args.length === 1) {
243 // Add path, unchanged. Use identity matrix
244 this._addPath(path, 1, 0, 0,
245 0, 1, 0,
246 0, 0, 1,
247 extend);
248 } else if (args.length === 2) {
249 // User provided the 9 params of a full matrix as an array.
250 var a = args[1];
251 this._addPath(path, a[0], a[1], a[2],
252 a[3], a[4], a[5],
253 a[6] || 0, a[7] || 0, a[8] || 1,
254 extend);
255 } else if (args.length === 7 || args.length === 10) {
256 // User provided the 9 params of a (full) matrix directly.
257 // (or just the 6 non perspective ones)
258 // These are in the same order as what Skia expects.
259 var a = args;
260 this._addPath(path, a[1], a[2], a[3],
261 a[4], a[5], a[6],
262 a[7] || 0, a[8] || 0, a[9] || 1,
263 extend);
264 } else {
265 SkDebug('addPath expected to take 1, 2, 7, or 10 required args. Got ' + args.length);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400266 return null;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500267 }
268 return this;
269 };
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400270
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500271 CanvasKit.SkPath.prototype.addRect = function() {
272 // Takes 1, 2, 4 or 5 args
273 // - SkRect
274 // - SkRect, isCCW
275 // - left, top, right, bottom
276 // - left, top, right, bottom, isCCW
277 if (arguments.length === 1 || arguments.length === 2) {
278 var r = arguments[0];
279 var ccw = arguments[1] || false;
280 this._addRect(r.fLeft, r.fTop, r.fRight, r.fBottom, ccw);
281 } else if (arguments.length === 4 || arguments.length === 5) {
282 var a = arguments;
283 this._addRect(a[0], a[1], a[2], a[3], a[4] || false);
284 } else {
285 SkDebug('addRect expected to take 1, 2, 4, or 5 args. Got ' + arguments.length);
Kevin Lubick217056c2018-09-20 17:39:31 -0400286 return null;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500287 }
288 return this;
289 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400290
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500291 CanvasKit.SkPath.prototype.addRoundRect = function() {
292 // Takes 3, 4, 6 or 7 args
293 // - SkRect, radii, ccw
294 // - SkRect, rx, ry, ccw
295 // - left, top, right, bottom, radii, ccw
296 // - left, top, right, bottom, rx, ry, ccw
297 var args = arguments;
298 if (args.length === 3 || args.length === 6) {
299 var radii = args[args.length-2];
300 } else if (args.length === 6 || args.length === 7){
301 // duplicate the given (rx, ry) pairs for each corner.
302 var rx = args[args.length-3];
303 var ry = args[args.length-2];
304 var radii = [rx, ry, rx, ry, rx, ry, rx, ry];
305 } else {
306 SkDebug('addRoundRect expected to take 3, 4, 6, or 7 args. Got ' + args.length);
307 return null;
308 }
309 if (radii.length !== 8) {
310 SkDebug('addRoundRect needs 8 radii provided. Got ' + radii.length);
311 return null;
312 }
313 var rptr = copy1dArray(radii, CanvasKit.HEAPF32);
314 if (args.length === 3 || args.length === 4) {
315 var r = args[0];
316 var ccw = args[args.length - 1];
317 this._addRoundRect(r.fLeft, r.fTop, r.fRight, r.fBottom, rptr, ccw);
318 } else if (args.length === 6 || args.length === 7) {
319 var a = args;
320 this._addRoundRect(a[0], a[1], a[2], a[3], rptr, ccw);
321 }
322 CanvasKit._free(rptr);
323 return this;
324 };
325
326 CanvasKit.SkPath.prototype.arc = function(x, y, radius, startAngle, endAngle, ccw) {
327 // emulates the HTMLCanvas behavior. See addArc() for the SkPath version.
328 // Note input angles are radians.
329 var bounds = CanvasKit.LTRBRect(x-radius, y-radius, x+radius, y+radius);
330 var sweep = radiansToDegrees(endAngle - startAngle) - (360 * !!ccw);
331 var temp = new CanvasKit.SkPath();
332 temp.addArc(bounds, radiansToDegrees(startAngle), sweep);
333 this.addPath(temp, true);
334 temp.delete();
335 return this;
336 };
337
338 CanvasKit.SkPath.prototype.arcTo = function() {
339 // takes 4, 5 or 7 args
340 // - 5 x1, y1, x2, y2, radius
341 // - 4 oval (as Rect), startAngle, sweepAngle, forceMoveTo
Kevin Lubicke384df42019-08-26 15:48:09 -0400342 // - 7 rx, ry, xAxisRotate, useSmallArc, isCCW, x, y
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500343 var args = arguments;
344 if (args.length === 5) {
345 this._arcTo(args[0], args[1], args[2], args[3], args[4]);
346 } else if (args.length === 4) {
347 this._arcTo(args[0], args[1], args[2], args[3]);
348 } else if (args.length === 7) {
Kevin Lubicke384df42019-08-26 15:48:09 -0400349 this._arcTo(args[0], args[1], args[2], !!args[3], !!args[4], args[5], args[6]);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500350 } else {
351 throw 'Invalid args for arcTo. Expected 4, 5, or 7, got '+ args.length;
352 }
353
354 return this;
355 };
356
357 CanvasKit.SkPath.prototype.close = function() {
358 this._close();
359 return this;
360 };
361
362 CanvasKit.SkPath.prototype.conicTo = function(x1, y1, x2, y2, w) {
363 this._conicTo(x1, y1, x2, y2, w);
364 return this;
365 };
366
367 CanvasKit.SkPath.prototype.cubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) {
368 this._cubicTo(cp1x, cp1y, cp2x, cp2y, x, y);
369 return this;
370 };
371
372 CanvasKit.SkPath.prototype.dash = function(on, off, phase) {
373 if (this._dash(on, off, phase)) {
Kevin Lubick217056c2018-09-20 17:39:31 -0400374 return this;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500375 }
376 return null;
377 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400378
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500379 CanvasKit.SkPath.prototype.lineTo = function(x, y) {
380 this._lineTo(x, y);
381 return this;
382 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400383
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500384 CanvasKit.SkPath.prototype.moveTo = function(x, y) {
385 this._moveTo(x, y);
386 return this;
387 };
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400388
Kevin Lubicke384df42019-08-26 15:48:09 -0400389 CanvasKit.SkPath.prototype.offset = function(dx, dy) {
390 this._transform(1, 0, dx,
391 0, 1, dy,
392 0, 0, 1);
393 return this;
394 };
395
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500396 CanvasKit.SkPath.prototype.op = function(otherPath, op) {
397 if (this._op(otherPath, op)) {
Kevin Lubick217056c2018-09-20 17:39:31 -0400398 return this;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500399 }
400 return null;
401 };
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400402
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500403 CanvasKit.SkPath.prototype.quadTo = function(cpx, cpy, x, y) {
404 this._quadTo(cpx, cpy, x, y);
405 return this;
406 };
407
408 CanvasKit.SkPath.prototype.simplify = function() {
409 if (this._simplify()) {
410 return this;
411 }
412 return null;
413 };
414
415 CanvasKit.SkPath.prototype.stroke = function(opts) {
416 // Fill out any missing values with the default values.
417 /**
418 * See externs.js for this definition
419 * @type {StrokeOpts}
420 */
421 opts = opts || {};
422 opts.width = opts.width || 1;
423 opts.miter_limit = opts.miter_limit || 4;
424 opts.cap = opts.cap || CanvasKit.StrokeCap.Butt;
425 opts.join = opts.join || CanvasKit.StrokeJoin.Miter;
426 opts.precision = opts.precision || 1;
427 if (this._stroke(opts)) {
428 return this;
429 }
430 return null;
431 };
432
433 CanvasKit.SkPath.prototype.transform = function() {
434 // Takes 1 or 9 args
435 if (arguments.length === 1) {
436 // argument 1 should be a 6 or 9 element array.
437 var a = arguments[0];
438 this._transform(a[0], a[1], a[2],
439 a[3], a[4], a[5],
440 a[6] || 0, a[7] || 0, a[8] || 1);
441 } else if (arguments.length === 6 || arguments.length === 9) {
442 // these arguments are the 6 or 9 members of the matrix
443 var a = arguments;
444 this._transform(a[0], a[1], a[2],
445 a[3], a[4], a[5],
446 a[6] || 0, a[7] || 0, a[8] || 1);
447 } else {
448 throw 'transform expected to take 1 or 9 arguments. Got ' + arguments.length;
449 }
450 return this;
451 };
452 // isComplement is optional, defaults to false
453 CanvasKit.SkPath.prototype.trim = function(startT, stopT, isComplement) {
454 if (this._trim(startT, stopT, !!isComplement)) {
455 return this;
456 }
457 return null;
458 };
459
460 // bones should be a 3d array.
461 // Each bone is a 3x2 transformation matrix in column major order:
462 // | scaleX skewX transX |
463 // | skewY scaleY transY |
464 // and bones is an array of those matrices.
465 // Returns a copy of this (SkVertices) with the bones applied.
466 CanvasKit.SkVertices.prototype.applyBones = function(bones) {
467 var bPtr = copy3dArray(bones, CanvasKit.HEAPF32);
468 var vert = this._applyBones(bPtr, bones.length);
469 CanvasKit._free(bPtr);
470 return vert;
471 }
472
473 CanvasKit.SkImage.prototype.encodeToData = function() {
474 if (!arguments.length) {
475 return this._encodeToData();
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400476 }
Kevin Lubick53965c92018-10-11 08:51:55 -0400477
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500478 if (arguments.length === 2) {
479 var a = arguments;
480 return this._encodeToDataWithFormat(a[0], a[1]);
Alexander Khovansky3e119332018-11-15 02:01:19 +0300481 }
482
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500483 throw 'encodeToData expected to take 0 or 2 arguments. Got ' + arguments.length;
484 }
Kevin Lubick1ba9c4d2019-02-22 10:04:06 -0500485
Kevin Lubicka064c282019-04-04 09:28:53 -0400486 CanvasKit.SkImage.prototype.makeShader = function(xTileMode, yTileMode, localMatrix) {
487 if (localMatrix) {
488 // Add perspective args if not provided.
489 if (localMatrix.length === 6) {
490 localMatrix.push(0, 0, 1);
491 }
492 return this._makeShader(xTileMode, yTileMode, localMatrix);
493 } else {
494 return this._makeShader(xTileMode, yTileMode);
495 }
496 }
497
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400498 CanvasKit.SkImage.prototype.readPixels = function(imageInfo, srcX, srcY) {
499 var rowBytes;
500 switch (imageInfo.colorType){
501 case CanvasKit.ColorType.RGBA_8888:
502 rowBytes = imageInfo.width * 4; // 1 byte per channel == 4 bytes per pixel in 8888
503 break;
504 case CanvasKit.ColorType.RGBA_F32:
505 rowBytes = imageInfo.width * 16; // 4 bytes per channel == 16 bytes per pixel in F32
506 break;
507 default:
508 SkDebug("Colortype not yet supported");
509 return;
510 }
511 var pBytes = rowBytes * imageInfo.height;
512 var pPtr = CanvasKit._malloc(pBytes);
513
514 if (!this._readPixels(imageInfo, pPtr, rowBytes, srcX, srcY)) {
515 SkDebug("Could not read pixels with the given inputs");
516 return null;
517 }
518
519 // Put those pixels into a typed array of the right format and then
520 // make a copy with slice() that we can return.
521 var retVal = null;
522 switch (imageInfo.colorType){
523 case CanvasKit.ColorType.RGBA_8888:
524 retVal = new Uint8Array(CanvasKit.buffer, pPtr, pBytes).slice();
525 break;
526 case CanvasKit.ColorType.RGBA_F32:
527 retVal = new Float32Array(CanvasKit.buffer, pPtr, pBytes).slice();
528 break;
529 }
530
531 // Free the allocated pixels in the WASM memory
532 CanvasKit._free(pPtr);
533 return retVal;
534
535 }
536
Kevin Lubickee91c072019-03-29 10:39:52 -0400537 // atlas is an SkImage, e.g. from CanvasKit.MakeImageFromEncoded
538 // srcRects and dstXforms should be CanvasKit.SkRectBuilder and CanvasKit.RSXFormBuilder
539 // or just arrays of floats in groups of 4.
540 // colors, if provided, should be a CanvasKit.SkColorBuilder or array of SkColor
541 // (from CanvasKit.Color)
542 CanvasKit.SkCanvas.prototype.drawAtlas = function(atlas, srcRects, dstXforms, paint,
543 /*optional*/ blendMode, colors) {
544 if (!atlas || !paint || !srcRects || !dstXforms) {
545 SkDebug('Doing nothing since missing a required input');
546 return;
547 }
548 if (srcRects.length !== dstXforms.length || (colors && colors.length !== dstXforms.length)) {
549 SkDebug('Doing nothing since input arrays length mismatches');
550 }
551 if (!blendMode) {
552 blendMode = CanvasKit.BlendMode.SrcOver;
553 }
554
555 var srcRectPtr;
556 if (srcRects.build) {
557 srcRectPtr = srcRects.build();
558 } else {
559 srcRectPtr = copy1dArray(srcRects, CanvasKit.HEAPF32);
560 }
561
562 var dstXformPtr;
563 if (dstXforms.build) {
564 dstXformPtr = dstXforms.build();
565 } else {
566 dstXformPtr = copy1dArray(dstXforms, CanvasKit.HEAPF32);
567 }
568
569 var colorPtr = 0; // enscriptem doesn't like undefined for nullptr
570 if (colors) {
571 if (colors.build) {
572 colorPtr = colors.build();
573 } else {
574 colorPtr = copy1dArray(colors, CanvasKit.HEAPU32);
575 }
576 }
577
578 this._drawAtlas(atlas, dstXformPtr, srcRectPtr, colorPtr, dstXforms.length,
579 blendMode, paint);
580
581 if (srcRectPtr && !srcRects.build) {
582 CanvasKit._free(srcRectPtr);
583 }
584 if (dstXformPtr && !dstXforms.build) {
585 CanvasKit._free(dstXformPtr);
586 }
587 if (colorPtr && !colors.build) {
588 CanvasKit._free(colorPtr);
589 }
590
591 }
592
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500593 // str can be either a text string or a ShapedText object
594 CanvasKit.SkCanvas.prototype.drawText = function(str, x, y, paint, font) {
595 if (typeof str === 'string') {
Kevin Lubickec4903d2019-01-14 08:36:08 -0500596 // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
597 // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
598 // Add 1 for null terminator
599 var strLen = lengthBytesUTF8(str) + 1;
600 var strPtr = CanvasKit._malloc(strLen);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500601
Kevin Lubickec4903d2019-01-14 08:36:08 -0500602 stringToUTF8(str, strPtr, strLen);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500603 this._drawSimpleText(strPtr, strLen, x, y, font, paint);
Kevin Lubickda3d8ac2019-01-07 11:08:55 -0500604 } else {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500605 this._drawShapedText(str, x, y, paint);
Kevin Lubickd29edd72018-12-07 08:29:52 -0500606 }
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400607 }
608
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500609 // returns Uint8Array
610 CanvasKit.SkCanvas.prototype.readPixels = function(x, y, w, h, alphaType,
611 colorType, dstRowBytes) {
612 // supply defaults (which are compatible with HTMLCanvas's getImageData)
613 alphaType = alphaType || CanvasKit.AlphaType.Unpremul;
614 colorType = colorType || CanvasKit.ColorType.RGBA_8888;
615 dstRowBytes = dstRowBytes || (4 * w);
616
617 var len = h * dstRowBytes
618 var pptr = CanvasKit._malloc(len);
619 var ok = this._readPixels({
620 'width': w,
621 'height': h,
Kevin Lubick52b9f372018-12-04 13:57:36 -0500622 'colorType': colorType,
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500623 'alphaType': alphaType,
624 }, pptr, dstRowBytes, x, y);
625 if (!ok) {
626 CanvasKit._free(pptr);
627 return null;
628 }
629
630 // The first typed array is just a view into memory. Because we will
631 // be free-ing that, we call slice to make a persistent copy.
632 var pixels = new Uint8Array(CanvasKit.HEAPU8.buffer, pptr, len).slice();
633 CanvasKit._free(pptr);
634 return pixels;
635 }
636
637 // pixels is a TypedArray. No matter the input size, it will be treated as
638 // a Uint8Array (essentially, a byte array).
639 CanvasKit.SkCanvas.prototype.writePixels = function(pixels, srcWidth, srcHeight,
640 destX, destY, alphaType, colorType) {
641 if (pixels.byteLength % (srcWidth * srcHeight)) {
642 throw 'pixels length must be a multiple of the srcWidth * srcHeight';
643 }
644 var bytesPerPixel = pixels.byteLength / (srcWidth * srcHeight);
645 // supply defaults (which are compatible with HTMLCanvas's putImageData)
646 alphaType = alphaType || CanvasKit.AlphaType.Unpremul;
647 colorType = colorType || CanvasKit.ColorType.RGBA_8888;
648 var srcRowBytes = bytesPerPixel * srcWidth;
649
Kevin Lubick52b9f372018-12-04 13:57:36 -0500650 var pptr = CanvasKit._malloc(pixels.byteLength);
651 CanvasKit.HEAPU8.set(pixels, pptr);
Kevin Lubick52b9f372018-12-04 13:57:36 -0500652
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500653 var ok = this._writePixels({
654 'width': srcWidth,
655 'height': srcHeight,
656 'colorType': colorType,
657 'alphaType': alphaType,
658 }, pptr, srcRowBytes, destX, destY);
659
660 CanvasKit._free(pptr);
661 return ok;
Kevin Lubick52b9f372018-12-04 13:57:36 -0500662 }
663
Kevin Lubickd3729342019-09-12 11:11:25 -0400664 // colorMatrix is an SkColorMatrix (e.g. Float32Array of length 20)
665 CanvasKit.SkColorFilter.MakeMatrix = function(colorMatrix) {
666 if (!colorMatrix || colorMatrix.length !== 20) {
667 SkDebug('ignoring invalid color matrix');
668 return;
669 }
670 var fptr = copy1dArray(colorMatrix, CanvasKit.HEAPF32);
671 // We know skia memcopies the floats, so we can free our memory after the call returns.
672 var m = CanvasKit.SkColorFilter._makeMatrix(fptr);
673 CanvasKit._free(fptr);
674 return m;
675 }
676
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400677 // Returns an array of the widths of the glyphs in this string.
678 CanvasKit.SkFont.prototype.getWidths = function(str) {
679 // add 1 for null terminator
680 var codePoints = str.length + 1;
681 // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
682 // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
683 // Add 1 for null terminator
684 var strBytes = lengthBytesUTF8(str) + 1;
685 var strPtr = CanvasKit._malloc(strBytes);
686 stringToUTF8(str, strPtr, strBytes);
687
688 var bytesPerFloat = 4;
689 // allocate widths == numCodePoints
690 var widthPtr = CanvasKit._malloc(codePoints * bytesPerFloat);
691 if (!this._getWidths(strPtr, strBytes, codePoints, widthPtr)) {
692 SkDebug('Could not compute widths');
693 CanvasKit._free(strPtr);
694 CanvasKit._free(widthPtr);
695 return null;
696 }
697 // reminder, this shouldn't copy the data, just is a nice way to
698 // wrap 4 bytes together into a float.
699 var widths = new Float32Array(CanvasKit.buffer, widthPtr, codePoints);
700 // This copies the data so we can free the CanvasKit memory
701 var retVal = Array.from(widths);
702 CanvasKit._free(strPtr);
703 CanvasKit._free(widthPtr);
704 return retVal;
705 }
706
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500707 // fontData should be an arrayBuffer
708 CanvasKit.SkFontMgr.prototype.MakeTypefaceFromData = function(fontData) {
709 var data = new Uint8Array(fontData);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400710
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500711 var fptr = CanvasKit._malloc(data.byteLength);
712 CanvasKit.HEAPU8.set(data, fptr);
713 var font = this._makeTypefaceFromData(fptr, data.byteLength);
714 if (!font) {
715 SkDebug('Could not decode font data');
716 // We do not need to free the data since the C++ will do that for us
717 // when the font is deleted (or fails to decode);
718 return null;
719 }
720 return font;
721 }
722
Kevin Lubickcc13fd32019-04-05 13:00:01 -0400723 // The serialized format of an SkPicture (informally called an "skp"), is not something
724 // that clients should ever rely on. It is useful when filing bug reports, but that's
725 // about it. The format may change at anytime and no promises are made for backwards
726 // or forward compatibility.
727 CanvasKit.SkPicture.prototype.DEBUGONLY_saveAsFile = function(skpName) {
728 var data = this.DEBUGONLY_serialize();
729 if (!data) {
730 SkDebug('Could not serialize to skpicture.');
731 return;
732 }
733 var bytes = CanvasKit.getSkDataBytes(data);
734 saveBytesToFile(bytes, skpName);
735 data.delete();
736 }
737
738 CanvasKit.SkSurface.prototype.captureFrameAsSkPicture = function(drawFrame) {
739 // Set up SkPictureRecorder
740 var spr = new CanvasKit.SkPictureRecorder();
741 var canvas = spr.beginRecording(
742 CanvasKit.LTRBRect(0, 0, this.width(), this.height()));
743 drawFrame(canvas);
744 var pic = spr.finishRecordingAsPicture();
745 spr.delete();
746 // TODO: do we need to clean up the memory for canvas?
747 // If we delete it here, saveAsFile doesn't work correctly.
748 return pic;
749 }
750
Kevin Lubick359a7e32019-03-19 09:34:37 -0400751 CanvasKit.SkSurface.prototype.requestAnimationFrame = function(callback, dirtyRect) {
752 if (!this._cached_canvas) {
753 this._cached_canvas = this.getCanvas();
754 }
755 window.requestAnimationFrame(function() {
Kevin Lubick39026282019-03-28 12:46:40 -0400756 if (this._context !== undefined) {
757 CanvasKit.setCurrentContext(this._context);
758 }
Kevin Lubick359a7e32019-03-19 09:34:37 -0400759
760 callback(this._cached_canvas);
761
762 this.flush();
763 }.bind(this));
764 }
765
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400766 CanvasKit.SkTextBlob.MakeOnPath = function(str, path, font, initialOffset) {
767 if (!str || !str.length) {
768 SkDebug('ignoring 0 length string');
769 return;
770 }
771 if (!path || !path.countPoints()) {
772 SkDebug('ignoring empty path');
773 return;
774 }
775 if (path.countPoints() === 1) {
776 SkDebug('path has 1 point, returning normal textblob');
777 return this.MakeFromText(str, font);
778 }
779
780 if (!initialOffset) {
781 initialOffset = 0;
782 }
783
784 var widths = font.getWidths(str);
785
786 var rsx = new CanvasKit.RSXFormBuilder();
787 var meas = new CanvasKit.SkPathMeasure(path, false, 1);
788 var dist = initialOffset;
789 for (var i = 0; i < str.length; i++) {
790 var width = widths[i];
791 dist += width/2;
792 if (dist > meas.getLength()) {
793 // jump to next contour
794 if (!meas.nextContour()) {
795 // We have come to the end of the path - terminate the string
796 // right here.
797 str = str.substring(0, i);
798 break;
799 }
800 dist = width/2;
801 }
802
803 // Gives us the (x, y) coordinates as well as the cos/sin of the tangent
804 // line at that position.
805 var xycs = meas.getPosTan(dist);
806 var cx = xycs[0];
807 var cy = xycs[1];
808 var cosT = xycs[2];
809 var sinT = xycs[3];
810
811 var adjustedX = cx - (width/2 * cosT);
812 var adjustedY = cy - (width/2 * sinT);
813
814 rsx.push(cosT, sinT, adjustedX, adjustedY);
815 dist += width/2;
816 }
817 var retVal = this.MakeFromRSXform(str, rsx, font);
818 rsx.delete();
819 meas.delete();
820 return retVal;
821 }
822
823 CanvasKit.SkTextBlob.MakeFromRSXform = function(str, rsxBuilder, font) {
824 // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
825 // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
826 // Add 1 for null terminator
827 var strLen = lengthBytesUTF8(str) + 1;
828 var strPtr = CanvasKit._malloc(strLen);
829 // Add 1 for the null terminator.
830 stringToUTF8(str, strPtr, strLen);
831 var rptr = rsxBuilder.build();
832
833 var blob = CanvasKit.SkTextBlob._MakeFromRSXform(strPtr, strLen - 1,
834 rptr, font, CanvasKit.TextEncoding.UTF8);
835 if (!blob) {
836 SkDebug('Could not make textblob from string "' + str + '"');
837 return null;
838 }
839
840 var origDelete = blob.delete.bind(blob);
841 blob.delete = function() {
842 CanvasKit._free(strPtr);
843 origDelete();
844 }
845 return blob;
846 }
847
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500848 CanvasKit.SkTextBlob.MakeFromText = function(str, font) {
849 // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
850 // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
851 // Add 1 for null terminator
852 var strLen = lengthBytesUTF8(str) + 1;
853 var strPtr = CanvasKit._malloc(strLen);
854 // Add 1 for the null terminator.
855 stringToUTF8(str, strPtr, strLen);
856
857 var blob = CanvasKit.SkTextBlob._MakeFromText(strPtr, strLen - 1, font, CanvasKit.TextEncoding.UTF8);
858 if (!blob) {
859 SkDebug('Could not make textblob from string "' + str + '"');
860 return null;
Kevin Lubick217056c2018-09-20 17:39:31 -0400861 }
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400862
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500863 var origDelete = blob.delete.bind(blob);
864 blob.delete = function() {
865 CanvasKit._free(strPtr);
866 origDelete();
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400867 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500868 return blob;
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400869 }
870
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500871 // Run through the JS files that are added at compile time.
872 if (CanvasKit._extraInitializations) {
873 CanvasKit._extraInitializations.forEach(function(init) {
874 init();
875 });
Kevin Lubickeb2f6b02018-11-29 15:07:02 -0500876 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500877}; // end CanvasKit.onRuntimeInitialized, that is, anything changing prototypes or dynamic.
Kevin Lubickeb2f6b02018-11-29 15:07:02 -0500878
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500879CanvasKit.LTRBRect = function(l, t, r, b) {
880 return {
881 fLeft: l,
882 fTop: t,
883 fRight: r,
884 fBottom: b,
885 };
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500886}
887
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500888CanvasKit.XYWHRect = function(x, y, w, h) {
889 return {
890 fLeft: x,
891 fTop: y,
892 fRight: x+w,
893 fBottom: y+h,
894 };
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500895}
896
Kevin Lubick7d644e12019-09-11 14:22:22 -0400897// RRectXY returns an RRect with the given rect and a radiusX and radiusY for
898// all 4 corners.
899CanvasKit.RRectXY = function(rect, rx, ry) {
900 return {
901 rect: rect,
902 rx1: rx,
903 ry1: ry,
904 rx2: rx,
905 ry2: ry,
906 rx3: rx,
907 ry3: ry,
908 rx4: rx,
909 ry4: ry,
910 };
911}
912
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500913CanvasKit.MakePathFromCmds = function(cmds) {
914 var ptrLen = loadCmdsTypedArray(cmds);
915 var path = CanvasKit._MakePathFromCmds(ptrLen[0], ptrLen[1]);
916 CanvasKit._free(ptrLen[0]);
917 return path;
918}
919
920CanvasKit.MakeSkDashPathEffect = function(intervals, phase) {
921 if (!phase) {
922 phase = 0;
923 }
924 if (!intervals.length || intervals.length % 2 === 1) {
925 throw 'Intervals array must have even length';
926 }
927 var ptr = copy1dArray(intervals, CanvasKit.HEAPF32);
928 var dpe = CanvasKit._MakeSkDashPathEffect(ptr, intervals.length, phase);
929 CanvasKit._free(ptr);
930 return dpe;
931}
932
933// data is a TypedArray or ArrayBuffer e.g. from fetch().then(resp.arrayBuffer())
934CanvasKit.MakeImageFromEncoded = function(data) {
935 data = new Uint8Array(data);
936
937 var iptr = CanvasKit._malloc(data.byteLength);
938 CanvasKit.HEAPU8.set(data, iptr);
939 var img = CanvasKit._decodeImage(iptr, data.byteLength);
940 if (!img) {
941 SkDebug('Could not decode image');
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500942 return null;
943 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500944 return img;
945}
946
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500947// pixels is a Uint8Array
948CanvasKit.MakeImage = function(pixels, width, height, alphaType, colorType) {
949 var bytesPerPixel = pixels.byteLength / (width * height);
950 var info = {
951 'width': width,
952 'height': height,
953 'alphaType': alphaType,
954 'colorType': colorType,
955 };
956 var pptr = CanvasKit._malloc(pixels.byteLength);
957 CanvasKit.HEAPU8.set(pixels, pptr);
958 // No need to _free iptr, Image takes it with SkData::MakeFromMalloc
959
960 return CanvasKit._MakeImage(info, pptr, pixels.byteLength, width * bytesPerPixel);
961}
962
963CanvasKit.MakeLinearGradientShader = function(start, end, colors, pos, mode, localMatrix, flags) {
Kevin Lubickee91c072019-03-29 10:39:52 -0400964 var colorPtr = copy1dArray(colors, CanvasKit.HEAPU32);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500965 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
966 flags = flags || 0;
967
968 if (localMatrix) {
969 // Add perspective args if not provided.
970 if (localMatrix.length === 6) {
971 localMatrix.push(0, 0, 1);
972 }
973 var lgs = CanvasKit._MakeLinearGradientShader(start, end, colorPtr, posPtr,
974 colors.length, mode, flags, localMatrix);
975 } else {
976 var lgs = CanvasKit._MakeLinearGradientShader(start, end, colorPtr, posPtr,
977 colors.length, mode, flags);
978 }
979
980 CanvasKit._free(colorPtr);
981 CanvasKit._free(posPtr);
982 return lgs;
983}
984
985CanvasKit.MakeRadialGradientShader = function(center, radius, colors, pos, mode, localMatrix, flags) {
Kevin Lubickee91c072019-03-29 10:39:52 -0400986 var colorPtr = copy1dArray(colors, CanvasKit.HEAPU32);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500987 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
988 flags = flags || 0;
989
990 if (localMatrix) {
991 // Add perspective args if not provided.
992 if (localMatrix.length === 6) {
993 localMatrix.push(0, 0, 1);
994 }
995 var rgs = CanvasKit._MakeRadialGradientShader(center, radius, colorPtr, posPtr,
996 colors.length, mode, flags, localMatrix);
997 } else {
998 var rgs = CanvasKit._MakeRadialGradientShader(center, radius, colorPtr, posPtr,
999 colors.length, mode, flags);
1000 }
1001
1002 CanvasKit._free(colorPtr);
1003 CanvasKit._free(posPtr);
1004 return rgs;
1005}
1006
1007CanvasKit.MakeTwoPointConicalGradientShader = function(start, startRadius, end, endRadius,
1008 colors, pos, mode, localMatrix, flags) {
Kevin Lubickee91c072019-03-29 10:39:52 -04001009 var colorPtr = copy1dArray(colors, CanvasKit.HEAPU32);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001010 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
1011 flags = flags || 0;
1012
1013 if (localMatrix) {
1014 // Add perspective args if not provided.
1015 if (localMatrix.length === 6) {
1016 localMatrix.push(0, 0, 1);
1017 }
1018 var rgs = CanvasKit._MakeTwoPointConicalGradientShader(
1019 start, startRadius, end, endRadius,
1020 colorPtr, posPtr, colors.length, mode, flags, localMatrix);
1021 } else {
1022 var rgs = CanvasKit._MakeTwoPointConicalGradientShader(
1023 start, startRadius, end, endRadius,
1024 colorPtr, posPtr, colors.length, mode, flags);
1025 }
1026
1027 CanvasKit._free(colorPtr);
1028 CanvasKit._free(posPtr);
1029 return rgs;
1030}
1031
1032CanvasKit.MakeSkVertices = function(mode, positions, textureCoordinates, colors,
Kevin Lubickb3574c92019-03-06 08:25:36 -05001033 boneIndices, boneWeights, indices, isVolatile) {
Kevin Lubickb3574c92019-03-06 08:25:36 -05001034 // Default isVolitile to true if not set
1035 isVolatile = isVolatile === undefined ? true : isVolatile;
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001036 var idxCount = (indices && indices.length) || 0;
1037
1038 var flags = 0;
1039 // These flags are from SkVertices.h and should be kept in sync with those.
1040 if (textureCoordinates && textureCoordinates.length) {
1041 flags |= (1 << 0);
1042 }
1043 if (colors && colors.length) {
1044 flags |= (1 << 1);
1045 }
1046 if (boneIndices && boneIndices.length) {
1047 flags |= (1 << 2);
1048 }
1049 if (!isVolatile) {
1050 flags |= (1 << 3);
1051 }
1052
1053 var builder = new CanvasKit._SkVerticesBuilder(mode, positions.length, idxCount, flags);
1054
1055 copy2dArray(positions, CanvasKit.HEAPF32, builder.positions());
1056 if (builder.texCoords()) {
1057 copy2dArray(textureCoordinates, CanvasKit.HEAPF32, builder.texCoords());
1058 }
1059 if (builder.colors()) {
1060 copy1dArray(colors, CanvasKit.HEAPU32, builder.colors());
1061 }
1062 if (builder.boneIndices()) {
1063 copy2dArray(boneIndices, CanvasKit.HEAP32, builder.boneIndices());
1064 }
1065 if (builder.boneWeights()) {
1066 copy2dArray(boneWeights, CanvasKit.HEAPF32, builder.boneWeights());
1067 }
1068 if (builder.indices()) {
1069 copy1dArray(indices, CanvasKit.HEAPU16, builder.indices());
1070 }
Kevin Lubickb3574c92019-03-06 08:25:36 -05001071
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001072 var idxCount = (indices && indices.length) || 0;
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001073 // Create the vertices, which owns the memory that the builder had allocated.
1074 return builder.detach();
Kevin Lubicka064c282019-04-04 09:28:53 -04001075};