blob: 01711e9c204b31965f932d929808f05e87558558 [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 Lubickf5ea37f2019-02-28 10:06:18 -0500219 CanvasKit.SkPath.prototype.addArc = function(oval, startAngle, sweepAngle) {
220 // see arc() for the HTMLCanvas version
221 // note input angles are degrees.
222 this._addArc(oval, startAngle, sweepAngle);
223 return this;
224 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400225
Kevin Lubicke384df42019-08-26 15:48:09 -0400226 CanvasKit.SkPath.prototype.addOval = function(oval, isCCW, startIndex) {
227 if (startIndex === undefined) {
228 startIndex = 1;
229 }
230 this._addOval(oval, !!isCCW, startIndex);
231 return this;
232 };
233
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500234 CanvasKit.SkPath.prototype.addPath = function() {
235 // Takes 1, 2, 7, or 10 required args, where the first arg is always the path.
236 // The last arg is optional and chooses between add or extend mode.
237 // The options for the remaining args are:
238 // - an array of 6 or 9 parameters (perspective is optional)
239 // - the 9 parameters of a full matrix or
240 // the 6 non-perspective params of a matrix.
241 var args = Array.prototype.slice.call(arguments);
242 var path = args[0];
243 var extend = false;
244 if (typeof args[args.length-1] === "boolean") {
245 extend = args.pop();
246 }
247 if (args.length === 1) {
248 // Add path, unchanged. Use identity matrix
249 this._addPath(path, 1, 0, 0,
250 0, 1, 0,
251 0, 0, 1,
252 extend);
253 } else if (args.length === 2) {
254 // User provided the 9 params of a full matrix as an array.
255 var a = args[1];
256 this._addPath(path, a[0], a[1], a[2],
257 a[3], a[4], a[5],
258 a[6] || 0, a[7] || 0, a[8] || 1,
259 extend);
260 } else if (args.length === 7 || args.length === 10) {
261 // User provided the 9 params of a (full) matrix directly.
262 // (or just the 6 non perspective ones)
263 // These are in the same order as what Skia expects.
264 var a = args;
265 this._addPath(path, a[1], a[2], a[3],
266 a[4], a[5], a[6],
267 a[7] || 0, a[8] || 0, a[9] || 1,
268 extend);
269 } else {
270 SkDebug('addPath expected to take 1, 2, 7, or 10 required args. Got ' + args.length);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400271 return null;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500272 }
273 return this;
274 };
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400275
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500276 CanvasKit.SkPath.prototype.addRect = function() {
277 // Takes 1, 2, 4 or 5 args
278 // - SkRect
279 // - SkRect, isCCW
280 // - left, top, right, bottom
281 // - left, top, right, bottom, isCCW
282 if (arguments.length === 1 || arguments.length === 2) {
283 var r = arguments[0];
284 var ccw = arguments[1] || false;
285 this._addRect(r.fLeft, r.fTop, r.fRight, r.fBottom, ccw);
286 } else if (arguments.length === 4 || arguments.length === 5) {
287 var a = arguments;
288 this._addRect(a[0], a[1], a[2], a[3], a[4] || false);
289 } else {
290 SkDebug('addRect expected to take 1, 2, 4, or 5 args. Got ' + arguments.length);
Kevin Lubick217056c2018-09-20 17:39:31 -0400291 return null;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500292 }
293 return this;
294 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400295
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500296 CanvasKit.SkPath.prototype.addRoundRect = function() {
297 // Takes 3, 4, 6 or 7 args
298 // - SkRect, radii, ccw
299 // - SkRect, rx, ry, ccw
300 // - left, top, right, bottom, radii, ccw
301 // - left, top, right, bottom, rx, ry, ccw
302 var args = arguments;
303 if (args.length === 3 || args.length === 6) {
304 var radii = args[args.length-2];
305 } else if (args.length === 6 || args.length === 7){
306 // duplicate the given (rx, ry) pairs for each corner.
307 var rx = args[args.length-3];
308 var ry = args[args.length-2];
309 var radii = [rx, ry, rx, ry, rx, ry, rx, ry];
310 } else {
311 SkDebug('addRoundRect expected to take 3, 4, 6, or 7 args. Got ' + args.length);
312 return null;
313 }
314 if (radii.length !== 8) {
315 SkDebug('addRoundRect needs 8 radii provided. Got ' + radii.length);
316 return null;
317 }
318 var rptr = copy1dArray(radii, CanvasKit.HEAPF32);
319 if (args.length === 3 || args.length === 4) {
320 var r = args[0];
321 var ccw = args[args.length - 1];
322 this._addRoundRect(r.fLeft, r.fTop, r.fRight, r.fBottom, rptr, ccw);
323 } else if (args.length === 6 || args.length === 7) {
324 var a = args;
325 this._addRoundRect(a[0], a[1], a[2], a[3], rptr, ccw);
326 }
327 CanvasKit._free(rptr);
328 return this;
329 };
330
331 CanvasKit.SkPath.prototype.arc = function(x, y, radius, startAngle, endAngle, ccw) {
332 // emulates the HTMLCanvas behavior. See addArc() for the SkPath version.
333 // Note input angles are radians.
334 var bounds = CanvasKit.LTRBRect(x-radius, y-radius, x+radius, y+radius);
335 var sweep = radiansToDegrees(endAngle - startAngle) - (360 * !!ccw);
336 var temp = new CanvasKit.SkPath();
337 temp.addArc(bounds, radiansToDegrees(startAngle), sweep);
338 this.addPath(temp, true);
339 temp.delete();
340 return this;
341 };
342
343 CanvasKit.SkPath.prototype.arcTo = function() {
344 // takes 4, 5 or 7 args
345 // - 5 x1, y1, x2, y2, radius
346 // - 4 oval (as Rect), startAngle, sweepAngle, forceMoveTo
Kevin Lubicke384df42019-08-26 15:48:09 -0400347 // - 7 rx, ry, xAxisRotate, useSmallArc, isCCW, x, y
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500348 var args = arguments;
349 if (args.length === 5) {
350 this._arcTo(args[0], args[1], args[2], args[3], args[4]);
351 } else if (args.length === 4) {
352 this._arcTo(args[0], args[1], args[2], args[3]);
353 } else if (args.length === 7) {
Kevin Lubicke384df42019-08-26 15:48:09 -0400354 this._arcTo(args[0], args[1], args[2], !!args[3], !!args[4], args[5], args[6]);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500355 } else {
356 throw 'Invalid args for arcTo. Expected 4, 5, or 7, got '+ args.length;
357 }
358
359 return this;
360 };
361
362 CanvasKit.SkPath.prototype.close = function() {
363 this._close();
364 return this;
365 };
366
367 CanvasKit.SkPath.prototype.conicTo = function(x1, y1, x2, y2, w) {
368 this._conicTo(x1, y1, x2, y2, w);
369 return this;
370 };
371
372 CanvasKit.SkPath.prototype.cubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) {
373 this._cubicTo(cp1x, cp1y, cp2x, cp2y, x, y);
374 return this;
375 };
376
377 CanvasKit.SkPath.prototype.dash = function(on, off, phase) {
378 if (this._dash(on, off, phase)) {
Kevin Lubick217056c2018-09-20 17:39:31 -0400379 return this;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500380 }
381 return null;
382 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400383
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500384 CanvasKit.SkPath.prototype.lineTo = function(x, y) {
385 this._lineTo(x, y);
386 return this;
387 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400388
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500389 CanvasKit.SkPath.prototype.moveTo = function(x, y) {
390 this._moveTo(x, y);
391 return this;
392 };
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400393
Kevin Lubicke384df42019-08-26 15:48:09 -0400394 CanvasKit.SkPath.prototype.offset = function(dx, dy) {
395 this._transform(1, 0, dx,
396 0, 1, dy,
397 0, 0, 1);
398 return this;
399 };
400
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500401 CanvasKit.SkPath.prototype.op = function(otherPath, op) {
402 if (this._op(otherPath, op)) {
Kevin Lubick217056c2018-09-20 17:39:31 -0400403 return this;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500404 }
405 return null;
406 };
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400407
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500408 CanvasKit.SkPath.prototype.quadTo = function(cpx, cpy, x, y) {
409 this._quadTo(cpx, cpy, x, y);
410 return this;
411 };
412
Kevin Lubick79b71342019-11-01 14:36:52 -0400413 CanvasKit.SkPath.prototype.rArcTo = function(rx, ry, xAxisRotate, useSmallArc, isCCW, dx, dy) {
414 this._rArcTo(rx, ry, xAxisRotate, useSmallArc, isCCW, dx, dy);
415 return this;
416 };
417
418 CanvasKit.SkPath.prototype.rConicTo = function(dx1, dy1, dx2, dy2, w) {
419 this._rConicTo(dx1, dy1, dx2, dy2, w);
420 return this;
421 };
422
423 // These params are all relative
424 CanvasKit.SkPath.prototype.rCubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) {
425 this._rCubicTo(cp1x, cp1y, cp2x, cp2y, x, y);
426 return this;
427 };
428
429 CanvasKit.SkPath.prototype.rLineTo = function(dx, dy) {
430 this._rLineTo(dx, dy);
431 return this;
432 };
433
434 CanvasKit.SkPath.prototype.rMoveTo = function(dx, dy) {
435 this._rMoveTo(dx, dy);
436 return this;
437 };
438
439 // These params are all relative
440 CanvasKit.SkPath.prototype.rQuadTo = function(cpx, cpy, x, y) {
441 this._rQuadTo(cpx, cpy, x, y);
442 return this;
443 };
444
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500445 CanvasKit.SkPath.prototype.simplify = function() {
446 if (this._simplify()) {
447 return this;
448 }
449 return null;
450 };
451
452 CanvasKit.SkPath.prototype.stroke = function(opts) {
453 // Fill out any missing values with the default values.
454 /**
455 * See externs.js for this definition
456 * @type {StrokeOpts}
457 */
458 opts = opts || {};
459 opts.width = opts.width || 1;
460 opts.miter_limit = opts.miter_limit || 4;
461 opts.cap = opts.cap || CanvasKit.StrokeCap.Butt;
462 opts.join = opts.join || CanvasKit.StrokeJoin.Miter;
463 opts.precision = opts.precision || 1;
464 if (this._stroke(opts)) {
465 return this;
466 }
467 return null;
468 };
469
470 CanvasKit.SkPath.prototype.transform = function() {
471 // Takes 1 or 9 args
472 if (arguments.length === 1) {
473 // argument 1 should be a 6 or 9 element array.
474 var a = arguments[0];
475 this._transform(a[0], a[1], a[2],
476 a[3], a[4], a[5],
477 a[6] || 0, a[7] || 0, a[8] || 1);
478 } else if (arguments.length === 6 || arguments.length === 9) {
479 // these arguments are the 6 or 9 members of the matrix
480 var a = arguments;
481 this._transform(a[0], a[1], a[2],
482 a[3], a[4], a[5],
483 a[6] || 0, a[7] || 0, a[8] || 1);
484 } else {
485 throw 'transform expected to take 1 or 9 arguments. Got ' + arguments.length;
486 }
487 return this;
488 };
489 // isComplement is optional, defaults to false
490 CanvasKit.SkPath.prototype.trim = function(startT, stopT, isComplement) {
491 if (this._trim(startT, stopT, !!isComplement)) {
492 return this;
493 }
494 return null;
495 };
496
497 // bones should be a 3d array.
498 // Each bone is a 3x2 transformation matrix in column major order:
499 // | scaleX skewX transX |
500 // | skewY scaleY transY |
501 // and bones is an array of those matrices.
502 // Returns a copy of this (SkVertices) with the bones applied.
503 CanvasKit.SkVertices.prototype.applyBones = function(bones) {
504 var bPtr = copy3dArray(bones, CanvasKit.HEAPF32);
505 var vert = this._applyBones(bPtr, bones.length);
506 CanvasKit._free(bPtr);
507 return vert;
508 }
509
510 CanvasKit.SkImage.prototype.encodeToData = function() {
511 if (!arguments.length) {
512 return this._encodeToData();
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400513 }
Kevin Lubick53965c92018-10-11 08:51:55 -0400514
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500515 if (arguments.length === 2) {
516 var a = arguments;
517 return this._encodeToDataWithFormat(a[0], a[1]);
Alexander Khovansky3e119332018-11-15 02:01:19 +0300518 }
519
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500520 throw 'encodeToData expected to take 0 or 2 arguments. Got ' + arguments.length;
521 }
Kevin Lubick1ba9c4d2019-02-22 10:04:06 -0500522
Kevin Lubicka064c282019-04-04 09:28:53 -0400523 CanvasKit.SkImage.prototype.makeShader = function(xTileMode, yTileMode, localMatrix) {
524 if (localMatrix) {
525 // Add perspective args if not provided.
526 if (localMatrix.length === 6) {
527 localMatrix.push(0, 0, 1);
528 }
529 return this._makeShader(xTileMode, yTileMode, localMatrix);
530 } else {
531 return this._makeShader(xTileMode, yTileMode);
532 }
533 }
534
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400535 CanvasKit.SkImage.prototype.readPixels = function(imageInfo, srcX, srcY) {
536 var rowBytes;
537 switch (imageInfo.colorType){
538 case CanvasKit.ColorType.RGBA_8888:
539 rowBytes = imageInfo.width * 4; // 1 byte per channel == 4 bytes per pixel in 8888
540 break;
541 case CanvasKit.ColorType.RGBA_F32:
542 rowBytes = imageInfo.width * 16; // 4 bytes per channel == 16 bytes per pixel in F32
543 break;
544 default:
545 SkDebug("Colortype not yet supported");
546 return;
547 }
548 var pBytes = rowBytes * imageInfo.height;
549 var pPtr = CanvasKit._malloc(pBytes);
550
551 if (!this._readPixels(imageInfo, pPtr, rowBytes, srcX, srcY)) {
552 SkDebug("Could not read pixels with the given inputs");
553 return null;
554 }
555
556 // Put those pixels into a typed array of the right format and then
557 // make a copy with slice() that we can return.
558 var retVal = null;
559 switch (imageInfo.colorType){
560 case CanvasKit.ColorType.RGBA_8888:
561 retVal = new Uint8Array(CanvasKit.buffer, pPtr, pBytes).slice();
562 break;
563 case CanvasKit.ColorType.RGBA_F32:
564 retVal = new Float32Array(CanvasKit.buffer, pPtr, pBytes).slice();
565 break;
566 }
567
568 // Free the allocated pixels in the WASM memory
569 CanvasKit._free(pPtr);
570 return retVal;
571
572 }
573
Kevin Lubickee91c072019-03-29 10:39:52 -0400574 // atlas is an SkImage, e.g. from CanvasKit.MakeImageFromEncoded
575 // srcRects and dstXforms should be CanvasKit.SkRectBuilder and CanvasKit.RSXFormBuilder
576 // or just arrays of floats in groups of 4.
577 // colors, if provided, should be a CanvasKit.SkColorBuilder or array of SkColor
578 // (from CanvasKit.Color)
579 CanvasKit.SkCanvas.prototype.drawAtlas = function(atlas, srcRects, dstXforms, paint,
580 /*optional*/ blendMode, colors) {
581 if (!atlas || !paint || !srcRects || !dstXforms) {
582 SkDebug('Doing nothing since missing a required input');
583 return;
584 }
585 if (srcRects.length !== dstXforms.length || (colors && colors.length !== dstXforms.length)) {
586 SkDebug('Doing nothing since input arrays length mismatches');
587 }
588 if (!blendMode) {
589 blendMode = CanvasKit.BlendMode.SrcOver;
590 }
591
592 var srcRectPtr;
593 if (srcRects.build) {
594 srcRectPtr = srcRects.build();
595 } else {
596 srcRectPtr = copy1dArray(srcRects, CanvasKit.HEAPF32);
597 }
598
599 var dstXformPtr;
600 if (dstXforms.build) {
601 dstXformPtr = dstXforms.build();
602 } else {
603 dstXformPtr = copy1dArray(dstXforms, CanvasKit.HEAPF32);
604 }
605
606 var colorPtr = 0; // enscriptem doesn't like undefined for nullptr
607 if (colors) {
608 if (colors.build) {
609 colorPtr = colors.build();
610 } else {
611 colorPtr = copy1dArray(colors, CanvasKit.HEAPU32);
612 }
613 }
614
615 this._drawAtlas(atlas, dstXformPtr, srcRectPtr, colorPtr, dstXforms.length,
616 blendMode, paint);
617
618 if (srcRectPtr && !srcRects.build) {
619 CanvasKit._free(srcRectPtr);
620 }
621 if (dstXformPtr && !dstXforms.build) {
622 CanvasKit._free(dstXformPtr);
623 }
624 if (colorPtr && !colors.build) {
625 CanvasKit._free(colorPtr);
626 }
627
628 }
629
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500630 // str can be either a text string or a ShapedText object
631 CanvasKit.SkCanvas.prototype.drawText = function(str, x, y, paint, font) {
632 if (typeof str === 'string') {
Kevin Lubickec4903d2019-01-14 08:36:08 -0500633 // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
634 // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
Kevin Lubick16d998f2019-09-26 13:25:26 -0400635 var strLen = lengthBytesUTF8(str);
636 // Add 1 for null terminator, which we need when copying/converting, but can ignore
637 // when we call into Skia.
638 var strPtr = CanvasKit._malloc(strLen + 1);
639 stringToUTF8(str, strPtr, strLen + 1);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500640 this._drawSimpleText(strPtr, strLen, x, y, font, paint);
Kevin Lubickda3d8ac2019-01-07 11:08:55 -0500641 } else {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500642 this._drawShapedText(str, x, y, paint);
Kevin Lubickd29edd72018-12-07 08:29:52 -0500643 }
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400644 }
645
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500646 // returns Uint8Array
647 CanvasKit.SkCanvas.prototype.readPixels = function(x, y, w, h, alphaType,
648 colorType, dstRowBytes) {
649 // supply defaults (which are compatible with HTMLCanvas's getImageData)
650 alphaType = alphaType || CanvasKit.AlphaType.Unpremul;
651 colorType = colorType || CanvasKit.ColorType.RGBA_8888;
652 dstRowBytes = dstRowBytes || (4 * w);
653
654 var len = h * dstRowBytes
655 var pptr = CanvasKit._malloc(len);
656 var ok = this._readPixels({
657 'width': w,
658 'height': h,
Kevin Lubick52b9f372018-12-04 13:57:36 -0500659 'colorType': colorType,
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500660 'alphaType': alphaType,
661 }, pptr, dstRowBytes, x, y);
662 if (!ok) {
663 CanvasKit._free(pptr);
664 return null;
665 }
666
667 // The first typed array is just a view into memory. Because we will
668 // be free-ing that, we call slice to make a persistent copy.
Kevin Lubickfa5a1382019-10-09 10:46:14 -0400669 var pixels = new Uint8Array(CanvasKit.buffer, pptr, len).slice();
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500670 CanvasKit._free(pptr);
671 return pixels;
672 }
673
674 // pixels is a TypedArray. No matter the input size, it will be treated as
675 // a Uint8Array (essentially, a byte array).
676 CanvasKit.SkCanvas.prototype.writePixels = function(pixels, srcWidth, srcHeight,
677 destX, destY, alphaType, colorType) {
678 if (pixels.byteLength % (srcWidth * srcHeight)) {
679 throw 'pixels length must be a multiple of the srcWidth * srcHeight';
680 }
681 var bytesPerPixel = pixels.byteLength / (srcWidth * srcHeight);
682 // supply defaults (which are compatible with HTMLCanvas's putImageData)
683 alphaType = alphaType || CanvasKit.AlphaType.Unpremul;
684 colorType = colorType || CanvasKit.ColorType.RGBA_8888;
685 var srcRowBytes = bytesPerPixel * srcWidth;
686
Kevin Lubick52b9f372018-12-04 13:57:36 -0500687 var pptr = CanvasKit._malloc(pixels.byteLength);
688 CanvasKit.HEAPU8.set(pixels, pptr);
Kevin Lubick52b9f372018-12-04 13:57:36 -0500689
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500690 var ok = this._writePixels({
691 'width': srcWidth,
692 'height': srcHeight,
693 'colorType': colorType,
694 'alphaType': alphaType,
695 }, pptr, srcRowBytes, destX, destY);
696
697 CanvasKit._free(pptr);
698 return ok;
Kevin Lubick52b9f372018-12-04 13:57:36 -0500699 }
700
Kevin Lubickd3729342019-09-12 11:11:25 -0400701 // colorMatrix is an SkColorMatrix (e.g. Float32Array of length 20)
702 CanvasKit.SkColorFilter.MakeMatrix = function(colorMatrix) {
703 if (!colorMatrix || colorMatrix.length !== 20) {
704 SkDebug('ignoring invalid color matrix');
705 return;
706 }
707 var fptr = copy1dArray(colorMatrix, CanvasKit.HEAPF32);
708 // We know skia memcopies the floats, so we can free our memory after the call returns.
709 var m = CanvasKit.SkColorFilter._makeMatrix(fptr);
710 CanvasKit._free(fptr);
711 return m;
712 }
713
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400714 // Returns an array of the widths of the glyphs in this string.
715 CanvasKit.SkFont.prototype.getWidths = function(str) {
716 // add 1 for null terminator
717 var codePoints = str.length + 1;
718 // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
719 // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
720 // Add 1 for null terminator
721 var strBytes = lengthBytesUTF8(str) + 1;
722 var strPtr = CanvasKit._malloc(strBytes);
723 stringToUTF8(str, strPtr, strBytes);
724
725 var bytesPerFloat = 4;
726 // allocate widths == numCodePoints
727 var widthPtr = CanvasKit._malloc(codePoints * bytesPerFloat);
728 if (!this._getWidths(strPtr, strBytes, codePoints, widthPtr)) {
729 SkDebug('Could not compute widths');
730 CanvasKit._free(strPtr);
731 CanvasKit._free(widthPtr);
732 return null;
733 }
734 // reminder, this shouldn't copy the data, just is a nice way to
735 // wrap 4 bytes together into a float.
736 var widths = new Float32Array(CanvasKit.buffer, widthPtr, codePoints);
737 // This copies the data so we can free the CanvasKit memory
738 var retVal = Array.from(widths);
739 CanvasKit._free(strPtr);
740 CanvasKit._free(widthPtr);
741 return retVal;
742 }
743
Kevin Lubick369f6a52019-10-03 11:22:08 -0400744 // arguments should all be arrayBuffers or be an array of arrayBuffers.
Kevin Lubick61887c72019-09-26 13:20:50 -0400745 CanvasKit.SkFontMgr.FromData = function() {
746 if (!arguments.length) {
747 SkDebug('Could not make SkFontMgr from no font sources');
748 return null;
749 }
750 var fonts = arguments;
Kevin Lubick369f6a52019-10-03 11:22:08 -0400751 if (fonts.length === 1 && Array.isArray(fonts[0])) {
Kevin Lubick61887c72019-09-26 13:20:50 -0400752 fonts = arguments[0];
753 }
754 if (!fonts.length) {
755 SkDebug('Could not make SkFontMgr from no font sources');
756 return null;
757 }
758 var dPtrs = [];
759 var sizes = [];
760 for (var i = 0; i < fonts.length; i++) {
761 var data = new Uint8Array(fonts[i]);
762 var dptr = copy1dArray(data, CanvasKit.HEAPU8);
763 dPtrs.push(dptr);
764 sizes.push(data.byteLength);
765 }
766 // Pointers are 32 bit unsigned ints
767 var datasPtr = copy1dArray(dPtrs, CanvasKit.HEAPU32);
768 var sizesPtr = copy1dArray(sizes, CanvasKit.HEAPU32);
769 var fm = CanvasKit.SkFontMgr._fromData(datasPtr, sizesPtr, fonts.length);
770 // The SkFontMgr has taken ownership of the bytes we allocated in the for loop.
771 CanvasKit._free(datasPtr);
772 CanvasKit._free(sizesPtr);
773 return fm;
774 }
775
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500776 // fontData should be an arrayBuffer
777 CanvasKit.SkFontMgr.prototype.MakeTypefaceFromData = function(fontData) {
778 var data = new Uint8Array(fontData);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400779
Kevin Lubick61887c72019-09-26 13:20:50 -0400780 var fptr = copy1dArray(data, CanvasKit.HEAPU8);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500781 var font = this._makeTypefaceFromData(fptr, data.byteLength);
782 if (!font) {
783 SkDebug('Could not decode font data');
784 // We do not need to free the data since the C++ will do that for us
785 // when the font is deleted (or fails to decode);
786 return null;
787 }
788 return font;
789 }
790
Kevin Lubickcc13fd32019-04-05 13:00:01 -0400791 // The serialized format of an SkPicture (informally called an "skp"), is not something
792 // that clients should ever rely on. It is useful when filing bug reports, but that's
793 // about it. The format may change at anytime and no promises are made for backwards
794 // or forward compatibility.
795 CanvasKit.SkPicture.prototype.DEBUGONLY_saveAsFile = function(skpName) {
796 var data = this.DEBUGONLY_serialize();
797 if (!data) {
798 SkDebug('Could not serialize to skpicture.');
799 return;
800 }
801 var bytes = CanvasKit.getSkDataBytes(data);
802 saveBytesToFile(bytes, skpName);
803 data.delete();
804 }
805
806 CanvasKit.SkSurface.prototype.captureFrameAsSkPicture = function(drawFrame) {
807 // Set up SkPictureRecorder
808 var spr = new CanvasKit.SkPictureRecorder();
809 var canvas = spr.beginRecording(
810 CanvasKit.LTRBRect(0, 0, this.width(), this.height()));
811 drawFrame(canvas);
812 var pic = spr.finishRecordingAsPicture();
813 spr.delete();
814 // TODO: do we need to clean up the memory for canvas?
815 // If we delete it here, saveAsFile doesn't work correctly.
816 return pic;
817 }
818
Kevin Lubick359a7e32019-03-19 09:34:37 -0400819 CanvasKit.SkSurface.prototype.requestAnimationFrame = function(callback, dirtyRect) {
820 if (!this._cached_canvas) {
821 this._cached_canvas = this.getCanvas();
822 }
823 window.requestAnimationFrame(function() {
Kevin Lubick39026282019-03-28 12:46:40 -0400824 if (this._context !== undefined) {
825 CanvasKit.setCurrentContext(this._context);
826 }
Kevin Lubick359a7e32019-03-19 09:34:37 -0400827
828 callback(this._cached_canvas);
829
830 this.flush();
831 }.bind(this));
832 }
833
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400834 CanvasKit.SkTextBlob.MakeOnPath = function(str, path, font, initialOffset) {
835 if (!str || !str.length) {
836 SkDebug('ignoring 0 length string');
837 return;
838 }
839 if (!path || !path.countPoints()) {
840 SkDebug('ignoring empty path');
841 return;
842 }
843 if (path.countPoints() === 1) {
844 SkDebug('path has 1 point, returning normal textblob');
845 return this.MakeFromText(str, font);
846 }
847
848 if (!initialOffset) {
849 initialOffset = 0;
850 }
851
852 var widths = font.getWidths(str);
853
854 var rsx = new CanvasKit.RSXFormBuilder();
855 var meas = new CanvasKit.SkPathMeasure(path, false, 1);
856 var dist = initialOffset;
857 for (var i = 0; i < str.length; i++) {
858 var width = widths[i];
859 dist += width/2;
860 if (dist > meas.getLength()) {
861 // jump to next contour
862 if (!meas.nextContour()) {
863 // We have come to the end of the path - terminate the string
864 // right here.
865 str = str.substring(0, i);
866 break;
867 }
868 dist = width/2;
869 }
870
871 // Gives us the (x, y) coordinates as well as the cos/sin of the tangent
872 // line at that position.
873 var xycs = meas.getPosTan(dist);
874 var cx = xycs[0];
875 var cy = xycs[1];
876 var cosT = xycs[2];
877 var sinT = xycs[3];
878
879 var adjustedX = cx - (width/2 * cosT);
880 var adjustedY = cy - (width/2 * sinT);
881
882 rsx.push(cosT, sinT, adjustedX, adjustedY);
883 dist += width/2;
884 }
885 var retVal = this.MakeFromRSXform(str, rsx, font);
886 rsx.delete();
887 meas.delete();
888 return retVal;
889 }
890
891 CanvasKit.SkTextBlob.MakeFromRSXform = function(str, rsxBuilder, font) {
892 // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
893 // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
894 // Add 1 for null terminator
895 var strLen = lengthBytesUTF8(str) + 1;
896 var strPtr = CanvasKit._malloc(strLen);
897 // Add 1 for the null terminator.
898 stringToUTF8(str, strPtr, strLen);
899 var rptr = rsxBuilder.build();
900
901 var blob = CanvasKit.SkTextBlob._MakeFromRSXform(strPtr, strLen - 1,
902 rptr, font, CanvasKit.TextEncoding.UTF8);
903 if (!blob) {
904 SkDebug('Could not make textblob from string "' + str + '"');
905 return null;
906 }
907
908 var origDelete = blob.delete.bind(blob);
909 blob.delete = function() {
910 CanvasKit._free(strPtr);
911 origDelete();
912 }
913 return blob;
914 }
915
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500916 CanvasKit.SkTextBlob.MakeFromText = function(str, font) {
917 // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
918 // JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
919 // Add 1 for null terminator
920 var strLen = lengthBytesUTF8(str) + 1;
921 var strPtr = CanvasKit._malloc(strLen);
922 // Add 1 for the null terminator.
923 stringToUTF8(str, strPtr, strLen);
924
925 var blob = CanvasKit.SkTextBlob._MakeFromText(strPtr, strLen - 1, font, CanvasKit.TextEncoding.UTF8);
926 if (!blob) {
927 SkDebug('Could not make textblob from string "' + str + '"');
928 return null;
Kevin Lubick217056c2018-09-20 17:39:31 -0400929 }
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400930
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500931 var origDelete = blob.delete.bind(blob);
932 blob.delete = function() {
933 CanvasKit._free(strPtr);
934 origDelete();
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400935 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500936 return blob;
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400937 }
938
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500939 // Run through the JS files that are added at compile time.
940 if (CanvasKit._extraInitializations) {
941 CanvasKit._extraInitializations.forEach(function(init) {
942 init();
943 });
Kevin Lubickeb2f6b02018-11-29 15:07:02 -0500944 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500945}; // end CanvasKit.onRuntimeInitialized, that is, anything changing prototypes or dynamic.
Kevin Lubickeb2f6b02018-11-29 15:07:02 -0500946
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500947CanvasKit.LTRBRect = function(l, t, r, b) {
948 return {
949 fLeft: l,
950 fTop: t,
951 fRight: r,
952 fBottom: b,
953 };
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500954}
955
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500956CanvasKit.XYWHRect = function(x, y, w, h) {
957 return {
958 fLeft: x,
959 fTop: y,
960 fRight: x+w,
961 fBottom: y+h,
962 };
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500963}
964
Kevin Lubick7d644e12019-09-11 14:22:22 -0400965// RRectXY returns an RRect with the given rect and a radiusX and radiusY for
966// all 4 corners.
967CanvasKit.RRectXY = function(rect, rx, ry) {
968 return {
969 rect: rect,
970 rx1: rx,
971 ry1: ry,
972 rx2: rx,
973 ry2: ry,
974 rx3: rx,
975 ry3: ry,
976 rx4: rx,
977 ry4: ry,
978 };
979}
980
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500981CanvasKit.MakePathFromCmds = function(cmds) {
982 var ptrLen = loadCmdsTypedArray(cmds);
983 var path = CanvasKit._MakePathFromCmds(ptrLen[0], ptrLen[1]);
984 CanvasKit._free(ptrLen[0]);
985 return path;
986}
987
988CanvasKit.MakeSkDashPathEffect = function(intervals, phase) {
989 if (!phase) {
990 phase = 0;
991 }
992 if (!intervals.length || intervals.length % 2 === 1) {
993 throw 'Intervals array must have even length';
994 }
995 var ptr = copy1dArray(intervals, CanvasKit.HEAPF32);
996 var dpe = CanvasKit._MakeSkDashPathEffect(ptr, intervals.length, phase);
997 CanvasKit._free(ptr);
998 return dpe;
999}
1000
1001// data is a TypedArray or ArrayBuffer e.g. from fetch().then(resp.arrayBuffer())
Kevin Lubick6b921b72019-09-18 16:18:17 -04001002CanvasKit.MakeAnimatedImageFromEncoded = function(data) {
1003 data = new Uint8Array(data);
1004
1005 var iptr = CanvasKit._malloc(data.byteLength);
1006 CanvasKit.HEAPU8.set(data, iptr);
1007 var img = CanvasKit._decodeAnimatedImage(iptr, data.byteLength);
1008 if (!img) {
1009 SkDebug('Could not decode animated image');
1010 return null;
1011 }
1012 return img;
1013}
1014
1015// data is a TypedArray or ArrayBuffer e.g. from fetch().then(resp.arrayBuffer())
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001016CanvasKit.MakeImageFromEncoded = function(data) {
1017 data = new Uint8Array(data);
1018
1019 var iptr = CanvasKit._malloc(data.byteLength);
1020 CanvasKit.HEAPU8.set(data, iptr);
1021 var img = CanvasKit._decodeImage(iptr, data.byteLength);
1022 if (!img) {
1023 SkDebug('Could not decode image');
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001024 return null;
1025 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001026 return img;
1027}
1028
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001029// pixels is a Uint8Array
1030CanvasKit.MakeImage = function(pixels, width, height, alphaType, colorType) {
1031 var bytesPerPixel = pixels.byteLength / (width * height);
1032 var info = {
1033 'width': width,
1034 'height': height,
1035 'alphaType': alphaType,
1036 'colorType': colorType,
1037 };
1038 var pptr = CanvasKit._malloc(pixels.byteLength);
1039 CanvasKit.HEAPU8.set(pixels, pptr);
1040 // No need to _free iptr, Image takes it with SkData::MakeFromMalloc
1041
1042 return CanvasKit._MakeImage(info, pptr, pixels.byteLength, width * bytesPerPixel);
1043}
1044
1045CanvasKit.MakeLinearGradientShader = function(start, end, colors, pos, mode, localMatrix, flags) {
Kevin Lubickee91c072019-03-29 10:39:52 -04001046 var colorPtr = copy1dArray(colors, CanvasKit.HEAPU32);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001047 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
1048 flags = flags || 0;
1049
1050 if (localMatrix) {
1051 // Add perspective args if not provided.
1052 if (localMatrix.length === 6) {
1053 localMatrix.push(0, 0, 1);
1054 }
1055 var lgs = CanvasKit._MakeLinearGradientShader(start, end, colorPtr, posPtr,
1056 colors.length, mode, flags, localMatrix);
1057 } else {
1058 var lgs = CanvasKit._MakeLinearGradientShader(start, end, colorPtr, posPtr,
1059 colors.length, mode, flags);
1060 }
1061
1062 CanvasKit._free(colorPtr);
1063 CanvasKit._free(posPtr);
1064 return lgs;
1065}
1066
1067CanvasKit.MakeRadialGradientShader = function(center, radius, colors, pos, mode, localMatrix, flags) {
Kevin Lubickee91c072019-03-29 10:39:52 -04001068 var colorPtr = copy1dArray(colors, CanvasKit.HEAPU32);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001069 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
1070 flags = flags || 0;
1071
1072 if (localMatrix) {
1073 // Add perspective args if not provided.
1074 if (localMatrix.length === 6) {
1075 localMatrix.push(0, 0, 1);
1076 }
1077 var rgs = CanvasKit._MakeRadialGradientShader(center, radius, colorPtr, posPtr,
1078 colors.length, mode, flags, localMatrix);
1079 } else {
1080 var rgs = CanvasKit._MakeRadialGradientShader(center, radius, colorPtr, posPtr,
1081 colors.length, mode, flags);
1082 }
1083
1084 CanvasKit._free(colorPtr);
1085 CanvasKit._free(posPtr);
1086 return rgs;
1087}
1088
1089CanvasKit.MakeTwoPointConicalGradientShader = function(start, startRadius, end, endRadius,
1090 colors, pos, mode, localMatrix, flags) {
Kevin Lubickee91c072019-03-29 10:39:52 -04001091 var colorPtr = copy1dArray(colors, CanvasKit.HEAPU32);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001092 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
1093 flags = flags || 0;
1094
1095 if (localMatrix) {
1096 // Add perspective args if not provided.
1097 if (localMatrix.length === 6) {
1098 localMatrix.push(0, 0, 1);
1099 }
1100 var rgs = CanvasKit._MakeTwoPointConicalGradientShader(
1101 start, startRadius, end, endRadius,
1102 colorPtr, posPtr, colors.length, mode, flags, localMatrix);
1103 } else {
1104 var rgs = CanvasKit._MakeTwoPointConicalGradientShader(
1105 start, startRadius, end, endRadius,
1106 colorPtr, posPtr, colors.length, mode, flags);
1107 }
1108
1109 CanvasKit._free(colorPtr);
1110 CanvasKit._free(posPtr);
1111 return rgs;
1112}
1113
1114CanvasKit.MakeSkVertices = function(mode, positions, textureCoordinates, colors,
Kevin Lubickb3574c92019-03-06 08:25:36 -05001115 boneIndices, boneWeights, indices, isVolatile) {
Kevin Lubickb3574c92019-03-06 08:25:36 -05001116 // Default isVolitile to true if not set
1117 isVolatile = isVolatile === undefined ? true : isVolatile;
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001118 var idxCount = (indices && indices.length) || 0;
1119
1120 var flags = 0;
1121 // These flags are from SkVertices.h and should be kept in sync with those.
1122 if (textureCoordinates && textureCoordinates.length) {
1123 flags |= (1 << 0);
1124 }
1125 if (colors && colors.length) {
1126 flags |= (1 << 1);
1127 }
1128 if (boneIndices && boneIndices.length) {
1129 flags |= (1 << 2);
1130 }
1131 if (!isVolatile) {
1132 flags |= (1 << 3);
1133 }
1134
1135 var builder = new CanvasKit._SkVerticesBuilder(mode, positions.length, idxCount, flags);
1136
1137 copy2dArray(positions, CanvasKit.HEAPF32, builder.positions());
1138 if (builder.texCoords()) {
1139 copy2dArray(textureCoordinates, CanvasKit.HEAPF32, builder.texCoords());
1140 }
1141 if (builder.colors()) {
1142 copy1dArray(colors, CanvasKit.HEAPU32, builder.colors());
1143 }
1144 if (builder.boneIndices()) {
1145 copy2dArray(boneIndices, CanvasKit.HEAP32, builder.boneIndices());
1146 }
1147 if (builder.boneWeights()) {
1148 copy2dArray(boneWeights, CanvasKit.HEAPF32, builder.boneWeights());
1149 }
1150 if (builder.indices()) {
1151 copy1dArray(indices, CanvasKit.HEAPU16, builder.indices());
1152 }
Kevin Lubickb3574c92019-03-06 08:25:36 -05001153
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001154 var idxCount = (indices && indices.length) || 0;
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001155 // Create the vertices, which owns the memory that the builder had allocated.
1156 return builder.detach();
Kevin Lubicka064c282019-04-04 09:28:53 -04001157};