blob: e1b9d1f0af88672716823278df26794a269a2d0c [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 Lubickb5ae3b52018-11-03 07:51:19 -04004(function(CanvasKit) {
Kevin Lubick217056c2018-09-20 17:39:31 -04005 // 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.
8 CanvasKit.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
11
12 // Add some helpers for matrices. This is ported from SkMatrix.cpp
13 // to save complexity and overhead of going back and forth between
14 // C++ and JS layers.
Kevin Lubickae9dfc02018-12-06 10:14:10 -050015 // I would have liked to use something like DOMMatrix, except it
16 // isn't widely supported (would need polyfills) and it doesn't
17 // have a mapPoints() function (which could maybe be tacked on here).
18 // If DOMMatrix catches on, it would be worth re-considering this usage.
Kevin Lubick1a05fce2018-11-20 12:51:16 -050019 CanvasKit.SkMatrix = {};
20 function sdot(a, b, c, d, e, f) {
21 e = e || 0;
22 f = f || 0;
23 return a * b + c * d + e * f;
24 }
25
Kevin Lubickb9db3902018-11-26 11:47:54 -050026 CanvasKit.SkMatrix.identity = function() {
27 return [
28 1, 0, 0,
29 0, 1, 0,
30 0, 0, 1,
31 ];
32 };
33
Kevin Lubickae9dfc02018-12-06 10:14:10 -050034 // Return the inverse (if it exists) of this matrix.
35 // Otherwise, return the identity.
36 CanvasKit.SkMatrix.invert = function(m) {
37 var det = m[0]*m[4]*m[8] + m[1]*m[5]*m[6] + m[2]*m[3]*m[7]
38 - m[2]*m[4]*m[6] - m[1]*m[3]*m[8] - m[0]*m[5]*m[7];
39 if (!det) {
40 SkDebug('Warning, uninvertible matrix');
41 return CanvasKit.SkMatrix.identity();
42 }
43 return [
44 (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,
45 (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,
46 (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,
47 ];
48 };
49
Kevin Lubickb9db3902018-11-26 11:47:54 -050050 // Maps the given points according to the passed in matrix.
51 // Results are done in place.
52 // See SkMatrix.h::mapPoints for the docs on the math.
53 CanvasKit.SkMatrix.mapPoints = function(matrix, ptArr) {
54 if (ptArr.length % 2) {
55 throw 'mapPoints requires an even length arr';
56 }
57 for (var i = 0; i < ptArr.length; i+=2) {
58 var x = ptArr[i], y = ptArr[i+1];
59 // Gx+Hy+I
60 var denom = matrix[6]*x + matrix[7]*y + matrix[8];
61 // Ax+By+C
62 var xTrans = matrix[0]*x + matrix[1]*y + matrix[2];
63 // Dx+Ey+F
64 var yTrans = matrix[3]*x + matrix[4]*y + matrix[5];
65 ptArr[i] = xTrans/denom;
66 ptArr[i+1] = yTrans/denom;
67 }
68 return ptArr;
69 };
70
71 CanvasKit.SkMatrix.multiply = function(m1, m2) {
72 var result = [0,0,0, 0,0,0, 0,0,0];
73 for (var r = 0; r < 3; r++) {
74 for (var c = 0; c < 3; c++) {
75 // m1 and m2 are 1D arrays pretending to be 2D arrays
76 result[3*r + c] = sdot(m1[3*r + 0], m2[3*0 + c],
77 m1[3*r + 1], m2[3*1 + c],
78 m1[3*r + 2], m2[3*2 + c]);
79 }
80 }
81 return result;
82 }
83
84 // Return a matrix representing a rotation by n radians.
Kevin Lubick1a05fce2018-11-20 12:51:16 -050085 // px, py optionally say which point the rotation should be around
86 // with the default being (0, 0);
Kevin Lubickb9db3902018-11-26 11:47:54 -050087 CanvasKit.SkMatrix.rotated = function(radians, px, py) {
Kevin Lubick1a05fce2018-11-20 12:51:16 -050088 px = px || 0;
89 py = py || 0;
Kevin Lubickb9db3902018-11-26 11:47:54 -050090 var sinV = Math.sin(radians);
91 var cosV = Math.cos(radians);
Kevin Lubick1a05fce2018-11-20 12:51:16 -050092 return [
93 cosV, -sinV, sdot( sinV, py, 1 - cosV, px),
94 sinV, cosV, sdot(-sinV, px, 1 - cosV, py),
95 0, 0, 1,
96 ];
97 };
Kevin Lubick1a05fce2018-11-20 12:51:16 -050098
Kevin Lubickb9db3902018-11-26 11:47:54 -050099 CanvasKit.SkMatrix.scaled = function(sx, sy, px, py) {
100 px = px || 0;
101 py = py || 0;
102 return [
103 sx, 0, px - sx * px,
104 0, sy, py - sy * py,
105 0, 0, 1,
106 ];
107 };
108
109 CanvasKit.SkMatrix.skewed = function(kx, ky, px, py) {
110 px = px || 0;
111 py = py || 0;
112 return [
113 1, kx, -kx * px,
114 ky, 1, -ky * py,
115 0, 0, 1,
116 ];
117 };
118
119 CanvasKit.SkMatrix.translated = function(dx, dy) {
120 return [
121 1, 0, dx,
122 0, 1, dy,
123 0, 0, 1,
124 ];
125 };
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500126
127 CanvasKit.SkPath.prototype.addArc = function(oval, startAngle, sweepAngle) {
128 // see arc() for the HTMLCanvas version
129 // note input angles are degrees.
130 this._addArc(oval, startAngle, sweepAngle);
131 return this;
132 };
133
Kevin Lubick217056c2018-09-20 17:39:31 -0400134 CanvasKit.SkPath.prototype.addPath = function() {
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500135 // Takes 1, 2, 7, or 10 required args, where the first arg is always the path.
136 // The last arg is optional and chooses between add or extend mode.
Kevin Lubick217056c2018-09-20 17:39:31 -0400137 // The options for the remaining args are:
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500138 // - an array of 6 or 9 parameters (perspective is optional)
139 // - the 9 parameters of a full matrix or
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400140 // the 6 non-perspective params of a matrix.
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500141 var args = Array.prototype.slice.call(arguments);
142 var path = args[0];
143 var extend = false;
144 if (typeof args[args.length-1] === "boolean") {
145 extend = args.pop();
146 }
147 if (args.length === 1) {
148 // Add path, unchanged. Use identity matrix
149 this._addPath(path, 1, 0, 0,
150 0, 1, 0,
151 0, 0, 1,
152 extend);
153 } else if (args.length === 2) {
Kevin Lubick217056c2018-09-20 17:39:31 -0400154 // User provided the 9 params of a full matrix as an array.
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500155 var a = args[1];
156 this._addPath(path, a[0], a[1], a[2],
157 a[3], a[4], a[5],
158 a[6] || 0, a[7] || 0, a[8] || 1,
159 extend);
160 } else if (args.length === 7 || args.length === 10) {
Kevin Lubick217056c2018-09-20 17:39:31 -0400161 // User provided the 9 params of a (full) matrix directly.
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400162 // (or just the 6 non perspective ones)
Kevin Lubick217056c2018-09-20 17:39:31 -0400163 // These are in the same order as what Skia expects.
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500164 var a = args;
165 this._addPath(path, a[1], a[2], a[3],
166 a[4], a[5], a[6],
167 a[7] || 0, a[8] || 0, a[9] || 1,
168 extend);
Kevin Lubick217056c2018-09-20 17:39:31 -0400169 } else {
Kevin Lubick6fccc9d2018-11-20 15:55:10 -0500170 SkDebug('addPath expected to take 1, 2, 7, or 10 required args. Got ' + args.length);
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500171 return null;
172 }
173 return this;
174 };
175
176 CanvasKit.SkPath.prototype.addRect = function() {
177 // Takes 1, 2, 4 or 5 args
178 // - SkRect
179 // - SkRect, isCCW
180 // - left, top, right, bottom
181 // - left, top, right, bottom, isCCW
182 if (arguments.length === 1 || arguments.length === 2) {
183 var r = arguments[0];
184 var ccw = arguments[1] || false;
185 this._addRect(r.fLeft, r.fTop, r.fRight, r.fBottom, ccw);
186 } else if (arguments.length === 4 || arguments.length === 5) {
187 var a = arguments;
188 this._addRect(a[0], a[1], a[2], a[3], a[4] || false);
189 } else {
Kevin Lubick6fccc9d2018-11-20 15:55:10 -0500190 SkDebug('addRect expected to take 1, 2, 4, or 5 args. Got ' + arguments.length);
Kevin Lubick217056c2018-09-20 17:39:31 -0400191 return null;
192 }
193 return this;
194 };
195
Alexander Khovansky3e119332018-11-15 02:01:19 +0300196 CanvasKit.SkPath.prototype.arc = function(x, y, radius, startAngle, endAngle, ccw) {
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500197 // emulates the HTMLCanvas behavior. See addArc() for the SkPath version.
198 // Note input angles are radians.
199 var bounds = CanvasKit.LTRBRect(x-radius, y-radius, x+radius, y+radius);
200 var sweep = radiansToDegrees(endAngle - startAngle) - (360 * !!ccw);
201 var temp = new CanvasKit.SkPath();
202 temp.addArc(bounds, radiansToDegrees(startAngle), sweep);
203 this.addPath(temp, true);
204 temp.delete();
Alexander Khovansky3e119332018-11-15 02:01:19 +0300205 return this;
206 };
207
Kevin Lubick217056c2018-09-20 17:39:31 -0400208 CanvasKit.SkPath.prototype.arcTo = function(x1, y1, x2, y2, radius) {
209 this._arcTo(x1, y1, x2, y2, radius);
210 return this;
211 };
212
213 CanvasKit.SkPath.prototype.close = function() {
214 this._close();
215 return this;
216 };
217
218 CanvasKit.SkPath.prototype.conicTo = function(x1, y1, x2, y2, w) {
219 this._conicTo(x1, y1, x2, y2, w);
220 return this;
221 };
222
223 CanvasKit.SkPath.prototype.cubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) {
224 this._cubicTo(cp1x, cp1y, cp2x, cp2y, x, y);
225 return this;
226 };
227
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400228 CanvasKit.SkPath.prototype.dash = function(on, off, phase) {
229 if (this._dash(on, off, phase)) {
230 return this;
231 }
232 return null;
233 };
234
Kevin Lubick217056c2018-09-20 17:39:31 -0400235 CanvasKit.SkPath.prototype.lineTo = function(x, y) {
236 this._lineTo(x, y);
237 return this;
238 };
239
240 CanvasKit.SkPath.prototype.moveTo = function(x, y) {
241 this._moveTo(x, y);
242 return this;
243 };
244
245 CanvasKit.SkPath.prototype.op = function(otherPath, op) {
246 if (this._op(otherPath, op)) {
247 return this;
248 }
249 return null;
250 };
251
252 CanvasKit.SkPath.prototype.quadTo = function(cpx, cpy, x, y) {
253 this._quadTo(cpx, cpy, x, y);
254 return this;
255 };
256
257 CanvasKit.SkPath.prototype.simplify = function() {
258 if (this._simplify()) {
259 return this;
260 }
261 return null;
262 };
263
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400264 CanvasKit.SkPath.prototype.stroke = function(opts) {
265 // Fill out any missing values with the default values.
266 /**
267 * See externs.js for this definition
268 * @type {StrokeOpts}
269 */
270 opts = opts || {};
271 opts.width = opts.width || 1;
272 opts.miter_limit = opts.miter_limit || 4;
Kevin Lubickb9db3902018-11-26 11:47:54 -0500273 opts.cap = opts.cap || CanvasKit.StrokeCap.Butt;
274 opts.join = opts.join || CanvasKit.StrokeJoin.Miter;
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400275 if (this._stroke(opts)) {
276 return this;
277 }
278 return null;
279 };
280
Kevin Lubick217056c2018-09-20 17:39:31 -0400281 CanvasKit.SkPath.prototype.transform = function() {
282 // Takes 1 or 9 args
283 if (arguments.length === 1) {
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400284 // argument 1 should be a 6 or 9 element array.
Kevin Lubick217056c2018-09-20 17:39:31 -0400285 var a = arguments[0];
286 this._transform(a[0], a[1], a[2],
287 a[3], a[4], a[5],
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400288 a[6] || 0, a[7] || 0, a[8] || 1);
289 } else if (arguments.length === 6 || arguments.length === 9) {
290 // these arguments are the 6 or 9 members of the matrix
Kevin Lubick217056c2018-09-20 17:39:31 -0400291 var a = arguments;
292 this._transform(a[0], a[1], a[2],
293 a[3], a[4], a[5],
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400294 a[6] || 0, a[7] || 0, a[8] || 1);
Kevin Lubick217056c2018-09-20 17:39:31 -0400295 } else {
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400296 throw 'transform expected to take 1 or 9 arguments. Got ' + arguments.length;
Kevin Lubick217056c2018-09-20 17:39:31 -0400297 }
298 return this;
299 };
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400300 // isComplement is optional, defaults to false
301 CanvasKit.SkPath.prototype.trim = function(startT, stopT, isComplement) {
302 if (this._trim(startT, stopT, !!isComplement)) {
303 return this;
304 }
305 return null;
306 };
307
308 // bones should be a 3d array.
309 // Each bone is a 3x2 transformation matrix in column major order:
310 // | scaleX skewX transX |
311 // | skewY scaleY transY |
312 // and bones is an array of those matrices.
313 // Returns a copy of this (SkVertices) with the bones applied.
314 CanvasKit.SkVertices.prototype.applyBones = function(bones) {
315 var bPtr = copy3dArray(bones, CanvasKit.HEAPF32);
316 var vert = this._applyBones(bPtr, bones.length);
317 CanvasKit._free(bPtr);
318 return vert;
319 }
Kevin Lubick53965c92018-10-11 08:51:55 -0400320
Alexander Khovansky3e119332018-11-15 02:01:19 +0300321 CanvasKit.SkImage.prototype.encodeToData = function() {
Kevin Lubick52b9f372018-12-04 13:57:36 -0500322 if (!arguments.length) {
Alexander Khovansky3e119332018-11-15 02:01:19 +0300323 return this._encodeToData();
324 }
325
326 if (arguments.length === 2) {
327 var a = arguments;
328 return this._encodeToDataWithFormat(a[0], a[1]);
329 }
330
331 throw 'encodeToData expected to take 0 or 2 arguments. Got ' + arguments.length;
332 }
333
Kevin Lubick52b9f372018-12-04 13:57:36 -0500334 // returns Uint8Array
335 CanvasKit.SkCanvas.prototype.readPixels = function(x, y, w, h, alphaType,
336 colorType, dstRowBytes) {
337 // supply defaults (which are compatible with HTMLCanvas's getImageData)
338 alphaType = alphaType || CanvasKit.AlphaType.Unpremul;
339 colorType = colorType || CanvasKit.ColorType.RGBA_8888;
340 dstRowBytes = dstRowBytes || (4 * w);
341
342 var len = h * dstRowBytes
343 var pptr = CanvasKit._malloc(len);
344 var ok = this._readPixels({
345 'width': w,
346 'height': h,
347 'colorType': colorType,
348 'alphaType': alphaType,
349 }, pptr, dstRowBytes, x, y);
350 if (!ok) {
351 CanvasKit._free(pptr);
352 return null;
353 }
354
355 // The first typed array is just a view into memory. Because we will
356 // be free-ing that, we call slice to make a persistent copy.
357 var pixels = new Uint8Array(CanvasKit.HEAPU8.buffer, pptr, len).slice();
358 CanvasKit._free(pptr);
359 return pixels;
360 }
361
362 // pixels is a TypedArray. No matter the input size, it will be treated as
363 // a Uint8Array (essentially, a byte array).
364 CanvasKit.SkCanvas.prototype.writePixels = function(pixels, srcWidth, srcHeight,
365 destX, destY, alphaType, colorType) {
366 if (pixels.byteLength % (srcWidth * srcHeight)) {
367 throw 'pixels length must be a multiple of the srcWidth * srcHeight';
368 }
369 var bytesPerPixel = pixels.byteLength / (srcWidth * srcHeight);
370 // supply defaults (which are compatible with HTMLCanvas's putImageData)
371 alphaType = alphaType || CanvasKit.AlphaType.Unpremul;
372 colorType = colorType || CanvasKit.ColorType.RGBA_8888;
373 var srcRowBytes = bytesPerPixel * srcWidth;
374
375 var pptr = CanvasKit._malloc(pixels.byteLength);
376 CanvasKit.HEAPU8.set(pixels, pptr);
377
378 var ok = this._writePixels({
379 'width': srcWidth,
380 'height': srcHeight,
381 'colorType': colorType,
382 'alphaType': alphaType,
383 }, pptr, srcRowBytes, destX, destY);
384
385 CanvasKit._free(pptr);
386 return ok;
387 }
388
Kevin Lubick5b90b842018-10-17 07:57:18 -0400389 // Run through the JS files that are added at compile time.
390 if (CanvasKit._extraInitializations) {
391 CanvasKit._extraInitializations.forEach(function(init) {
392 init();
393 });
Kevin Lubick217056c2018-09-20 17:39:31 -0400394 }
Kevin Lubick3d99b1e2018-10-16 10:15:01 -0400395 } // end CanvasKit.onRuntimeInitialized, that is, anything changing prototypes or dynamic.
Kevin Lubick53965c92018-10-11 08:51:55 -0400396
Kevin Lubick217056c2018-09-20 17:39:31 -0400397 CanvasKit.LTRBRect = function(l, t, r, b) {
398 return {
399 fLeft: l,
400 fTop: t,
401 fRight: r,
402 fBottom: b,
403 };
404 }
405
Kevin Lubick0a1293c2018-12-03 12:31:04 -0500406 CanvasKit.XYWHRect = function(x, y, w, h) {
407 return {
408 fLeft: x,
409 fTop: y,
410 fRight: x+w,
411 fBottom: y+h,
412 };
413 }
414
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400415 var nullptr = 0; // emscripten doesn't like to take null as uintptr_t
416
417 // arr can be a normal JS array or a TypedArray
418 // dest is something like CanvasKit.HEAPF32
419 function copy1dArray(arr, dest) {
420 if (!arr || !arr.length) {
421 return nullptr;
422 }
423 var ptr = CanvasKit._malloc(arr.length * dest.BYTES_PER_ELEMENT);
424 // In c++ terms, the WASM heap is a uint8_t*, a long buffer/array of single
425 // byte elements. When we run _malloc, we always get an offset/pointer into
426 // that block of memory.
427 // CanvasKit exposes some different views to make it easier to work with
428 // different types. HEAPF32 for example, exposes it as a float*
429 // However, to make the ptr line up, we have to do some pointer arithmetic.
430 // Concretely, we need to convert ptr to go from an index into a 1-byte-wide
431 // buffer to an index into a 4-byte-wide buffer (in the case of HEAPF32)
432 // and thus we divide ptr by 4.
433 dest.set(arr, ptr / dest.BYTES_PER_ELEMENT);
434 return ptr;
435 }
436
437 // arr should be a non-jagged 2d JS array (TypeyArrays can't be nested
438 // inside themselves.)
439 // dest is something like CanvasKit.HEAPF32
440 function copy2dArray(arr, dest) {
441 if (!arr || !arr.length) {
442 return nullptr;
443 }
444 var ptr = CanvasKit._malloc(arr.length * arr[0].length * dest.BYTES_PER_ELEMENT);
445 var idx = 0;
446 var adjustedPtr = ptr / dest.BYTES_PER_ELEMENT;
447 for (var r = 0; r < arr.length; r++) {
448 for (var c = 0; c < arr[0].length; c++) {
449 dest[adjustedPtr + idx] = arr[r][c];
450 idx++;
451 }
452 }
453 return ptr;
454 }
455
456 // arr should be a non-jagged 3d JS array (TypeyArrays can't be nested
457 // inside themselves.)
458 // dest is something like CanvasKit.HEAPF32
459 function copy3dArray(arr, dest) {
460 if (!arr || !arr.length || !arr[0].length) {
461 return nullptr;
462 }
463 var ptr = CanvasKit._malloc(arr.length * arr[0].length * arr[0][0].length * dest.BYTES_PER_ELEMENT);
464 var idx = 0;
465 var adjustedPtr = ptr / dest.BYTES_PER_ELEMENT;
466 for (var x = 0; x < arr.length; x++) {
467 for (var y = 0; y < arr[0].length; y++) {
468 for (var z = 0; z < arr[0][0].length; z++) {
469 dest[adjustedPtr + idx] = arr[x][y][z];
470 idx++;
471 }
472 }
473 }
474 return ptr;
475 }
476
Kevin Lubick217056c2018-09-20 17:39:31 -0400477 CanvasKit.MakeSkDashPathEffect = function(intervals, phase) {
478 if (!phase) {
479 phase = 0;
480 }
481 if (!intervals.length || intervals.length % 2 === 1) {
482 throw 'Intervals array must have even length';
483 }
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400484 var ptr = copy1dArray(intervals, CanvasKit.HEAPF32);
485 var dpe = CanvasKit._MakeSkDashPathEffect(ptr, intervals.length, phase);
486 CanvasKit._free(ptr);
487 return dpe;
488 }
489
Kevin Lubick0a1293c2018-12-03 12:31:04 -0500490 // data is a TypedArray or ArrayBuffer
491 CanvasKit.MakeImageFromEncoded = function(data) {
492 data = new Uint8Array(data);
493
494 var iptr = CanvasKit._malloc(data.byteLength);
495 CanvasKit.HEAPU8.set(data, iptr);
496 var img = CanvasKit._decodeImage(iptr, data.byteLength);
497 if (!img) {
498 SkDebug('Could not decode image');
499 CanvasKit._free(iptr);
500 return null;
501 }
502 var realDelete = img.delete.bind(img);
503 img.delete = function() {
504 CanvasKit._free(iptr);
505 realDelete();
506 }
507 return img;
508 }
509
Kevin Lubick52b9f372018-12-04 13:57:36 -0500510 // imgData is an ArrayBuffer of data, e.g. from fetch().then(resp.arrayBuffer())
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400511 CanvasKit.MakeImageShader = function(imgData, xTileMode, yTileMode) {
512 var iptr = CanvasKit._malloc(imgData.byteLength);
513 CanvasKit.HEAPU8.set(new Uint8Array(imgData), iptr);
514 // No need to _free iptr, ImageShader takes it with SkData::MakeFromMalloc
515
516 return CanvasKit._MakeImageShader(iptr, imgData.byteLength, xTileMode, yTileMode);
517 }
518
Kevin Lubick52b9f372018-12-04 13:57:36 -0500519 // pixels is a Uint8Array
520 CanvasKit.MakeImage = function(pixels, width, height, alphaType, colorType) {
521 var bytesPerPixel = pixels.byteLength / (width * height);
522 var info = {
523 'width': width,
524 'height': height,
525 'alphaType': alphaType,
526 'colorType': colorType,
527 };
528 var pptr = CanvasKit._malloc(pixels.byteLength);
529 CanvasKit.HEAPU8.set(pixels, pptr);
530 // No need to _free iptr, Image takes it with SkData::MakeFromMalloc
531
532 return CanvasKit._MakeImage(info, pptr, pixels.byteLength, width * bytesPerPixel);
533 }
534
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400535 CanvasKit.MakeLinearGradientShader = function(start, end, colors, pos, mode, localMatrix, flags) {
536 var colorPtr = copy1dArray(colors, CanvasKit.HEAP32);
537 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
538 flags = flags || 0;
539
540 if (localMatrix) {
541 // Add perspective args if not provided.
542 if (localMatrix.length === 6) {
543 localMatrix.push(0, 0, 1);
544 }
545 var lgs = CanvasKit._MakeLinearGradientShader(start, end, colorPtr, posPtr,
546 colors.length, mode, flags, localMatrix);
547 } else {
548 var lgs = CanvasKit._MakeLinearGradientShader(start, end, colorPtr, posPtr,
549 colors.length, mode, flags);
Kevin Lubick217056c2018-09-20 17:39:31 -0400550 }
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400551
552 CanvasKit._free(colorPtr);
553 CanvasKit._free(posPtr);
554 return lgs;
555 }
556
557 CanvasKit.MakeRadialGradientShader = function(center, radius, colors, pos, mode, localMatrix, flags) {
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400558 var colorPtr = copy1dArray(colors, CanvasKit.HEAP32);
559 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
560 flags = flags || 0;
561
562 if (localMatrix) {
563 // Add perspective args if not provided.
564 if (localMatrix.length === 6) {
565 localMatrix.push(0, 0, 1);
566 }
567 var rgs = CanvasKit._MakeRadialGradientShader(center, radius, colorPtr, posPtr,
568 colors.length, mode, flags, localMatrix);
569 } else {
570 var rgs = CanvasKit._MakeRadialGradientShader(center, radius, colorPtr, posPtr,
571 colors.length, mode, flags);
572 }
573
574 CanvasKit._free(colorPtr);
575 CanvasKit._free(posPtr);
576 return rgs;
577 }
578
Kevin Lubickeb2f6b02018-11-29 15:07:02 -0500579 CanvasKit.MakeTwoPointConicalGradientShader = function(start, startRadius, end, endRadius,
580 colors, pos, mode, localMatrix, flags) {
581 var colorPtr = copy1dArray(colors, CanvasKit.HEAP32);
582 var posPtr = copy1dArray(pos, CanvasKit.HEAPF32);
583 flags = flags || 0;
584
585 if (localMatrix) {
586 // Add perspective args if not provided.
587 if (localMatrix.length === 6) {
588 localMatrix.push(0, 0, 1);
589 }
590 var rgs = CanvasKit._MakeTwoPointConicalGradientShader(
591 start, startRadius, end, endRadius,
592 colorPtr, posPtr, colors.length, mode, flags, localMatrix);
593 } else {
594 var rgs = CanvasKit._MakeTwoPointConicalGradientShader(
595 start, startRadius, end, endRadius,
596 colorPtr, posPtr, colors.length, mode, flags);
597 }
598
599 CanvasKit._free(colorPtr);
600 CanvasKit._free(posPtr);
601 return rgs;
602 }
603
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400604 CanvasKit.MakeSkVertices = function(mode, positions, textureCoordinates, colors,
605 boneIndices, boneWeights, indices) {
606 var positionPtr = copy2dArray(positions, CanvasKit.HEAPF32);
607 var texPtr = copy2dArray(textureCoordinates, CanvasKit.HEAPF32);
608 // Since we write the colors to memory as signed integers (JSColor), we can
609 // read them out on the other side as unsigned ints (SkColor) just fine
610 // - it's effectively casting.
611 var colorPtr = copy1dArray(colors, CanvasKit.HEAP32);
612
613 var boneIdxPtr = copy2dArray(boneIndices, CanvasKit.HEAP32);
614 var boneWtPtr = copy2dArray(boneWeights, CanvasKit.HEAPF32);
615 var idxPtr = copy1dArray(indices, CanvasKit.HEAPU16);
616
617 var idxCount = (indices && indices.length) || 0;
618 // _MakeVertices will copy all the values in, so we are free to release
619 // the memory after.
620 var vertices = CanvasKit._MakeSkVertices(mode, positions.length, positionPtr,
621 texPtr, colorPtr, boneIdxPtr, boneWtPtr,
622 idxCount, idxPtr);
623 positionPtr && CanvasKit._free(positionPtr);
624 texPtr && CanvasKit._free(texPtr);
625 colorPtr && CanvasKit._free(colorPtr);
626 idxPtr && CanvasKit._free(idxPtr);
627 boneIdxPtr && CanvasKit._free(boneIdxPtr);
628 boneWtPtr && CanvasKit._free(boneWtPtr);
629 return vertices;
Kevin Lubick217056c2018-09-20 17:39:31 -0400630 }
631
Kevin Lubick217056c2018-09-20 17:39:31 -0400632}(Module)); // When this file is loaded in, the high level object is "Module";
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500633
634// Intentionally added outside the scope to allow usage in canvas2d.js and other
635// pre-js files. These names are unlikely to cause emscripten collisions.
636function radiansToDegrees(rad) {
637 return (rad / Math.PI) * 180;
638}
639
640function degreesToRadians(deg) {
641 return (deg / 180) * Math.PI;
642}
643