blob: a9666ad1c7682a0828f093a474e6f14bbbc68666 [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
Kevin Lubick16d998f2019-09-26 13:25:26 -0400598 var strLen = lengthBytesUTF8(str);
599 // Add 1 for null terminator, which we need when copying/converting, but can ignore
600 // when we call into Skia.
601 var strPtr = CanvasKit._malloc(strLen + 1);
602 stringToUTF8(str, strPtr, strLen + 1);
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 Lubick61887c72019-09-26 13:20:50 -0400707 // arguments should all be an arrayBuffer or be an array of arrayBuffers.
708 CanvasKit.SkFontMgr.FromData = function() {
709 if (!arguments.length) {
710 SkDebug('Could not make SkFontMgr from no font sources');
711 return null;
712 }
713 var fonts = arguments;
714 if (Array.isArray(fonts) && fonts.length === 1) {
715 fonts = arguments[0];
716 }
717 if (!fonts.length) {
718 SkDebug('Could not make SkFontMgr from no font sources');
719 return null;
720 }
721 var dPtrs = [];
722 var sizes = [];
723 for (var i = 0; i < fonts.length; i++) {
724 var data = new Uint8Array(fonts[i]);
725 var dptr = copy1dArray(data, CanvasKit.HEAPU8);
726 dPtrs.push(dptr);
727 sizes.push(data.byteLength);
728 }
729 // Pointers are 32 bit unsigned ints
730 var datasPtr = copy1dArray(dPtrs, CanvasKit.HEAPU32);
731 var sizesPtr = copy1dArray(sizes, CanvasKit.HEAPU32);
732 var fm = CanvasKit.SkFontMgr._fromData(datasPtr, sizesPtr, fonts.length);
733 // The SkFontMgr has taken ownership of the bytes we allocated in the for loop.
734 CanvasKit._free(datasPtr);
735 CanvasKit._free(sizesPtr);
736 return fm;
737 }
738
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500739 // fontData should be an arrayBuffer
740 CanvasKit.SkFontMgr.prototype.MakeTypefaceFromData = function(fontData) {
741 var data = new Uint8Array(fontData);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400742
Kevin Lubick61887c72019-09-26 13:20:50 -0400743 var fptr = copy1dArray(data, CanvasKit.HEAPU8);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500744 var font = this._makeTypefaceFromData(fptr, data.byteLength);
745 if (!font) {
746 SkDebug('Could not decode font data');
747 // We do not need to free the data since the C++ will do that for us
748 // when the font is deleted (or fails to decode);
749 return null;
750 }
751 return font;
752 }
753
Kevin Lubickcc13fd32019-04-05 13:00:01 -0400754 // The serialized format of an SkPicture (informally called an "skp"), is not something
755 // that clients should ever rely on. It is useful when filing bug reports, but that's
756 // about it. The format may change at anytime and no promises are made for backwards
757 // or forward compatibility.
758 CanvasKit.SkPicture.prototype.DEBUGONLY_saveAsFile = function(skpName) {
759 var data = this.DEBUGONLY_serialize();
760 if (!data) {
761 SkDebug('Could not serialize to skpicture.');
762 return;
763 }
764 var bytes = CanvasKit.getSkDataBytes(data);
765 saveBytesToFile(bytes, skpName);
766 data.delete();
767 }
768
769 CanvasKit.SkSurface.prototype.captureFrameAsSkPicture = function(drawFrame) {
770 // Set up SkPictureRecorder
771 var spr = new CanvasKit.SkPictureRecorder();
772 var canvas = spr.beginRecording(
773 CanvasKit.LTRBRect(0, 0, this.width(), this.height()));
774 drawFrame(canvas);
775 var pic = spr.finishRecordingAsPicture();
776 spr.delete();
777 // TODO: do we need to clean up the memory for canvas?
778 // If we delete it here, saveAsFile doesn't work correctly.
779 return pic;
780 }
781
Kevin Lubick359a7e32019-03-19 09:34:37 -0400782 CanvasKit.SkSurface.prototype.requestAnimationFrame = function(callback, dirtyRect) {
783 if (!this._cached_canvas) {
784 this._cached_canvas = this.getCanvas();
785 }
786 window.requestAnimationFrame(function() {
Kevin Lubick39026282019-03-28 12:46:40 -0400787 if (this._context !== undefined) {
788 CanvasKit.setCurrentContext(this._context);
789 }
Kevin Lubick359a7e32019-03-19 09:34:37 -0400790
791 callback(this._cached_canvas);
792
793 this.flush();
794 }.bind(this));
795 }
796
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400797 CanvasKit.SkTextBlob.MakeOnPath = function(str, path, font, initialOffset) {
798 if (!str || !str.length) {
799 SkDebug('ignoring 0 length string');
800 return;
801 }
802 if (!path || !path.countPoints()) {
803 SkDebug('ignoring empty path');
804 return;
805 }
806 if (path.countPoints() === 1) {
807 SkDebug('path has 1 point, returning normal textblob');
808 return this.MakeFromText(str, font);
809 }
810
811 if (!initialOffset) {
812 initialOffset = 0;
813 }
814
815 var widths = font.getWidths(str);
816
817 var rsx = new CanvasKit.RSXFormBuilder();
818 var meas = new CanvasKit.SkPathMeasure(path, false, 1);
819 var dist = initialOffset;
820 for (var i = 0; i < str.length; i++) {
821 var width = widths[i];
822 dist += width/2;
823 if (dist > meas.getLength()) {
824 // jump to next contour
825 if (!meas.nextContour()) {
826 // We have come to the end of the path - terminate the string
827 // right here.
828 str = str.substring(0, i);
829 break;
830 }
831 dist = width/2;
832 }
833
834 // Gives us the (x, y) coordinates as well as the cos/sin of the tangent
835 // line at that position.
836 var xycs = meas.getPosTan(dist);
837 var cx = xycs[0];
838 var cy = xycs[1];
839 var cosT = xycs[2];
840 var sinT = xycs[3];
841
842 var adjustedX = cx - (width/2 * cosT);
843 var adjustedY = cy - (width/2 * sinT);
844
845 rsx.push(cosT, sinT, adjustedX, adjustedY);
846 dist += width/2;
847 }
848 var retVal = this.MakeFromRSXform(str, rsx, font);
849 rsx.delete();
850 meas.delete();
851 return retVal;
852 }
853
854 CanvasKit.SkTextBlob.MakeFromRSXform = function(str, rsxBuilder, font) {
855 // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
856 // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
857 // Add 1 for null terminator
858 var strLen = lengthBytesUTF8(str) + 1;
859 var strPtr = CanvasKit._malloc(strLen);
860 // Add 1 for the null terminator.
861 stringToUTF8(str, strPtr, strLen);
862 var rptr = rsxBuilder.build();
863
864 var blob = CanvasKit.SkTextBlob._MakeFromRSXform(strPtr, strLen - 1,
865 rptr, font, CanvasKit.TextEncoding.UTF8);
866 if (!blob) {
867 SkDebug('Could not make textblob from string "' + str + '"');
868 return null;
869 }
870
871 var origDelete = blob.delete.bind(blob);
872 blob.delete = function() {
873 CanvasKit._free(strPtr);
874 origDelete();
875 }
876 return blob;
877 }
878
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500879 CanvasKit.SkTextBlob.MakeFromText = function(str, font) {
880 // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
881 // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
882 // Add 1 for null terminator
883 var strLen = lengthBytesUTF8(str) + 1;
884 var strPtr = CanvasKit._malloc(strLen);
885 // Add 1 for the null terminator.
886 stringToUTF8(str, strPtr, strLen);
887
888 var blob = CanvasKit.SkTextBlob._MakeFromText(strPtr, strLen - 1, font, CanvasKit.TextEncoding.UTF8);
889 if (!blob) {
890 SkDebug('Could not make textblob from string "' + str + '"');
891 return null;
Kevin Lubick217056c2018-09-20 17:39:31 -0400892 }
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400893
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500894 var origDelete = blob.delete.bind(blob);
895 blob.delete = function() {
896 CanvasKit._free(strPtr);
897 origDelete();
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400898 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500899 return blob;
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400900 }
901
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500902 // Run through the JS files that are added at compile time.
903 if (CanvasKit._extraInitializations) {
904 CanvasKit._extraInitializations.forEach(function(init) {
905 init();
906 });
Kevin Lubickeb2f6b02018-11-29 15:07:02 -0500907 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500908}; // end CanvasKit.onRuntimeInitialized, that is, anything changing prototypes or dynamic.
Kevin Lubickeb2f6b02018-11-29 15:07:02 -0500909
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500910CanvasKit.LTRBRect = function(l, t, r, b) {
911 return {
912 fLeft: l,
913 fTop: t,
914 fRight: r,
915 fBottom: b,
916 };
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500917}
918
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500919CanvasKit.XYWHRect = function(x, y, w, h) {
920 return {
921 fLeft: x,
922 fTop: y,
923 fRight: x+w,
924 fBottom: y+h,
925 };
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500926}
927
Kevin Lubick7d644e12019-09-11 14:22:22 -0400928// RRectXY returns an RRect with the given rect and a radiusX and radiusY for
929// all 4 corners.
930CanvasKit.RRectXY = function(rect, rx, ry) {
931 return {
932 rect: rect,
933 rx1: rx,
934 ry1: ry,
935 rx2: rx,
936 ry2: ry,
937 rx3: rx,
938 ry3: ry,
939 rx4: rx,
940 ry4: ry,
941 };
942}
943
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500944CanvasKit.MakePathFromCmds = function(cmds) {
945 var ptrLen = loadCmdsTypedArray(cmds);
946 var path = CanvasKit._MakePathFromCmds(ptrLen[0], ptrLen[1]);
947 CanvasKit._free(ptrLen[0]);
948 return path;
949}
950
951CanvasKit.MakeSkDashPathEffect = function(intervals, phase) {
952 if (!phase) {
953 phase = 0;
954 }
955 if (!intervals.length || intervals.length % 2 === 1) {
956 throw 'Intervals array must have even length';
957 }
958 var ptr = copy1dArray(intervals, CanvasKit.HEAPF32);
959 var dpe = CanvasKit._MakeSkDashPathEffect(ptr, intervals.length, phase);
960 CanvasKit._free(ptr);
961 return dpe;
962}
963
964// data is a TypedArray or ArrayBuffer e.g. from fetch().then(resp.arrayBuffer())
Kevin Lubick6b921b72019-09-18 16:18:17 -0400965CanvasKit.MakeAnimatedImageFromEncoded = function(data) {
966 data = new Uint8Array(data);
967
968 var iptr = CanvasKit._malloc(data.byteLength);
969 CanvasKit.HEAPU8.set(data, iptr);
970 var img = CanvasKit._decodeAnimatedImage(iptr, data.byteLength);
971 if (!img) {
972 SkDebug('Could not decode animated image');
973 return null;
974 }
975 return img;
976}
977
978// data is a TypedArray or ArrayBuffer e.g. from fetch().then(resp.arrayBuffer())
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500979CanvasKit.MakeImageFromEncoded = function(data) {
980 data = new Uint8Array(data);
981
982 var iptr = CanvasKit._malloc(data.byteLength);
983 CanvasKit.HEAPU8.set(data, iptr);
984 var img = CanvasKit._decodeImage(iptr, data.byteLength);
985 if (!img) {
986 SkDebug('Could not decode image');
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500987 return null;
988 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500989 return img;
990}
991
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500992// pixels is a Uint8Array
993CanvasKit.MakeImage = function(pixels, width, height, alphaType, colorType) {
994 var bytesPerPixel = pixels.byteLength / (width * height);
995 var info = {
996 'width': width,
997 'height': height,
998 'alphaType': alphaType,
999 'colorType': colorType,
1000 };
1001 var pptr = CanvasKit._malloc(pixels.byteLength);
1002 CanvasKit.HEAPU8.set(pixels, pptr);
1003 // No need to _free iptr, Image takes it with SkData::MakeFromMalloc
1004
1005 return CanvasKit._MakeImage(info, pptr, pixels.byteLength, width * bytesPerPixel);
1006}
1007
1008CanvasKit.MakeLinearGradientShader = function(start, end, 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 lgs = CanvasKit._MakeLinearGradientShader(start, end, colorPtr, posPtr,
1019 colors.length, mode, flags, localMatrix);
1020 } else {
1021 var lgs = CanvasKit._MakeLinearGradientShader(start, end, colorPtr, posPtr,
1022 colors.length, mode, flags);
1023 }
1024
1025 CanvasKit._free(colorPtr);
1026 CanvasKit._free(posPtr);
1027 return lgs;
1028}
1029
1030CanvasKit.MakeRadialGradientShader = function(center, radius, colors, pos, mode, localMatrix, flags) {
Kevin Lubickee91c072019-03-29 10:39:52 -04001031 var colorPtr = copy1dArray(colors, CanvasKit.HEAPU32);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001032 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
1033 flags = flags || 0;
1034
1035 if (localMatrix) {
1036 // Add perspective args if not provided.
1037 if (localMatrix.length === 6) {
1038 localMatrix.push(0, 0, 1);
1039 }
1040 var rgs = CanvasKit._MakeRadialGradientShader(center, radius, colorPtr, posPtr,
1041 colors.length, mode, flags, localMatrix);
1042 } else {
1043 var rgs = CanvasKit._MakeRadialGradientShader(center, radius, colorPtr, posPtr,
1044 colors.length, mode, flags);
1045 }
1046
1047 CanvasKit._free(colorPtr);
1048 CanvasKit._free(posPtr);
1049 return rgs;
1050}
1051
1052CanvasKit.MakeTwoPointConicalGradientShader = function(start, startRadius, end, endRadius,
1053 colors, pos, mode, localMatrix, flags) {
Kevin Lubickee91c072019-03-29 10:39:52 -04001054 var colorPtr = copy1dArray(colors, CanvasKit.HEAPU32);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001055 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
1056 flags = flags || 0;
1057
1058 if (localMatrix) {
1059 // Add perspective args if not provided.
1060 if (localMatrix.length === 6) {
1061 localMatrix.push(0, 0, 1);
1062 }
1063 var rgs = CanvasKit._MakeTwoPointConicalGradientShader(
1064 start, startRadius, end, endRadius,
1065 colorPtr, posPtr, colors.length, mode, flags, localMatrix);
1066 } else {
1067 var rgs = CanvasKit._MakeTwoPointConicalGradientShader(
1068 start, startRadius, end, endRadius,
1069 colorPtr, posPtr, colors.length, mode, flags);
1070 }
1071
1072 CanvasKit._free(colorPtr);
1073 CanvasKit._free(posPtr);
1074 return rgs;
1075}
1076
1077CanvasKit.MakeSkVertices = function(mode, positions, textureCoordinates, colors,
Kevin Lubickb3574c92019-03-06 08:25:36 -05001078 boneIndices, boneWeights, indices, isVolatile) {
Kevin Lubickb3574c92019-03-06 08:25:36 -05001079 // Default isVolitile to true if not set
1080 isVolatile = isVolatile === undefined ? true : isVolatile;
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001081 var idxCount = (indices && indices.length) || 0;
1082
1083 var flags = 0;
1084 // These flags are from SkVertices.h and should be kept in sync with those.
1085 if (textureCoordinates && textureCoordinates.length) {
1086 flags |= (1 << 0);
1087 }
1088 if (colors && colors.length) {
1089 flags |= (1 << 1);
1090 }
1091 if (boneIndices && boneIndices.length) {
1092 flags |= (1 << 2);
1093 }
1094 if (!isVolatile) {
1095 flags |= (1 << 3);
1096 }
1097
1098 var builder = new CanvasKit._SkVerticesBuilder(mode, positions.length, idxCount, flags);
1099
1100 copy2dArray(positions, CanvasKit.HEAPF32, builder.positions());
1101 if (builder.texCoords()) {
1102 copy2dArray(textureCoordinates, CanvasKit.HEAPF32, builder.texCoords());
1103 }
1104 if (builder.colors()) {
1105 copy1dArray(colors, CanvasKit.HEAPU32, builder.colors());
1106 }
1107 if (builder.boneIndices()) {
1108 copy2dArray(boneIndices, CanvasKit.HEAP32, builder.boneIndices());
1109 }
1110 if (builder.boneWeights()) {
1111 copy2dArray(boneWeights, CanvasKit.HEAPF32, builder.boneWeights());
1112 }
1113 if (builder.indices()) {
1114 copy1dArray(indices, CanvasKit.HEAPU16, builder.indices());
1115 }
Kevin Lubickb3574c92019-03-06 08:25:36 -05001116
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001117 var idxCount = (indices && indices.length) || 0;
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001118 // Create the vertices, which owns the memory that the builder had allocated.
1119 return builder.detach();
Kevin Lubicka064c282019-04-04 09:28:53 -04001120};