blob: 3b44742ec827cd26d5149bac2c6118590dbc68b2 [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 Lubick93f1a382020-06-02 16:15:23 -040011 _scratchColor = CanvasKit.Malloc(Float32Array, 4); // 4 color scalars.
Kevin Lubickb42660f2020-06-03 09:58:27 -040012 _scratchColorPtr = _scratchColor['byteOffset'];
Kevin Lubick6aa38692020-06-01 11:25:47 -040013
14 _scratch4x4Matrix = CanvasKit.Malloc(Float32Array, 16); // 16 matrix scalars.
Kevin Lubickb42660f2020-06-03 09:58:27 -040015 _scratch4x4MatrixPtr = _scratch4x4Matrix['byteOffset'];
Kevin Lubick6aa38692020-06-01 11:25:47 -040016
17 _scratch3x3Matrix = CanvasKit.Malloc(Float32Array, 9); // 9 matrix scalars.
Kevin Lubickb42660f2020-06-03 09:58:27 -040018 _scratch3x3MatrixPtr = _scratch3x3Matrix['byteOffset'];
Kevin Lubickbe728012020-09-03 11:57:12 +000019
20 _scratchRRect = CanvasKit.Malloc(Float32Array, 12); // 4 scalars for rrect, 8 for radii.
21 _scratchRRectPtr = _scratchRRect['byteOffset'];
22
23 _scratchRRect2 = CanvasKit.Malloc(Float32Array, 12); // 4 scalars for rrect, 8 for radii.
24 _scratchRRect2Ptr = _scratchRRect2['byteOffset'];
25
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -040026 // Create single copies of all three supported color spaces
27 // These are sk_sp<SkColorSpace>
28 CanvasKit.SkColorSpace.SRGB = CanvasKit.SkColorSpace._MakeSRGB();
29 CanvasKit.SkColorSpace.DISPLAY_P3 = CanvasKit.SkColorSpace._MakeDisplayP3();
30 CanvasKit.SkColorSpace.ADOBE_RGB = CanvasKit.SkColorSpace._MakeAdobeRGB();
31
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050032 // Add some helpers for matrices. This is ported from SkMatrix.cpp
33 // to save complexity and overhead of going back and forth between
34 // C++ and JS layers.
35 // I would have liked to use something like DOMMatrix, except it
36 // isn't widely supported (would need polyfills) and it doesn't
37 // have a mapPoints() function (which could maybe be tacked on here).
38 // If DOMMatrix catches on, it would be worth re-considering this usage.
39 CanvasKit.SkMatrix = {};
Nathaniel Nifong77798b42020-02-21 17:15:22 -050040 function sdot() { // to be called with an even number of scalar args
41 var acc = 0;
42 for (var i=0; i < arguments.length-1; i+=2) {
43 acc += arguments[i] * arguments[i+1];
44 }
45 return acc;
46 }
47
48
49 // Private general matrix functions used in both 3x3s and 4x4s.
50 // Return a square identity matrix of size n.
51 var identityN = function(n) {
52 var size = n*n;
53 var m = new Array(size);
54 while(size--) {
55 m[size] = size%(n+1) == 0 ? 1.0 : 0.0;
56 }
57 return m;
58 }
59
60 // Stride, a function for compactly representing several ways of copying an array into another.
61 // Write vector `v` into matrix `m`. `m` is a matrix encoded as an array in row-major
62 // order. Its width is passed as `width`. `v` is an array with length < (m.length/width).
63 // An element of `v` is copied into `m` starting at `offset` and moving `colStride` cols right
64 // each row.
65 //
66 // For example, a width of 4, offset of 3, and stride of -1 would put the vector here.
67 // _ _ 0 _
68 // _ 1 _ _
69 // 2 _ _ _
70 // _ _ _ 3
71 //
72 var stride = function(v, m, width, offset, colStride) {
73 for (var i=0; i<v.length; i++) {
74 m[i * width + // column
75 (i * colStride + offset + width) % width // row
76 ] = v[i];
77 }
78 return m;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050079 }
80
81 CanvasKit.SkMatrix.identity = function() {
Nathaniel Nifong77798b42020-02-21 17:15:22 -050082 return identityN(3);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050083 };
84
85 // Return the inverse (if it exists) of this matrix.
Kevin Lubick9b56cea2020-04-06 08:16:18 -040086 // Otherwise, return null.
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050087 CanvasKit.SkMatrix.invert = function(m) {
Nathaniel Nifong77798b42020-02-21 17:15:22 -050088 // Find the determinant by the sarrus rule. https://en.wikipedia.org/wiki/Rule_of_Sarrus
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050089 var det = m[0]*m[4]*m[8] + m[1]*m[5]*m[6] + m[2]*m[3]*m[7]
90 - m[2]*m[4]*m[6] - m[1]*m[3]*m[8] - m[0]*m[5]*m[7];
91 if (!det) {
92 SkDebug('Warning, uninvertible matrix');
Nathaniel Nifong77798b42020-02-21 17:15:22 -050093 return null;
Kevin Lubick1a05fce2018-11-20 12:51:16 -050094 }
Nathaniel Nifong77798b42020-02-21 17:15:22 -050095 // Return the inverse by the formula adj(m)/det.
96 // adj (adjugate) of a 3x3 is the transpose of it's cofactor matrix.
97 // a cofactor matrix is a matrix where each term is +-det(N) where matrix N is the 2x2 formed
98 // by removing the row and column we're currently setting from the source.
99 // the sign alternates in a checkerboard pattern with a `+` at the top left.
100 // that's all been combined here into one expression.
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500101 return [
102 (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,
103 (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,
104 (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,
105 ];
106 };
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500107
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500108 // Maps the given points according to the passed in matrix.
109 // Results are done in place.
110 // See SkMatrix.h::mapPoints for the docs on the math.
111 CanvasKit.SkMatrix.mapPoints = function(matrix, ptArr) {
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500112 if (skIsDebug && (ptArr.length % 2)) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500113 throw 'mapPoints requires an even length arr';
Kevin Lubickb9db3902018-11-26 11:47:54 -0500114 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500115 for (var i = 0; i < ptArr.length; i+=2) {
116 var x = ptArr[i], y = ptArr[i+1];
117 // Gx+Hy+I
118 var denom = matrix[6]*x + matrix[7]*y + matrix[8];
119 // Ax+By+C
120 var xTrans = matrix[0]*x + matrix[1]*y + matrix[2];
121 // Dx+Ey+F
122 var yTrans = matrix[3]*x + matrix[4]*y + matrix[5];
123 ptArr[i] = xTrans/denom;
124 ptArr[i+1] = yTrans/denom;
125 }
126 return ptArr;
127 };
Kevin Lubickb9db3902018-11-26 11:47:54 -0500128
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500129 function isnumber(val) { return val !== NaN; };
130
Kevin Lubickc89ca0b2020-04-02 14:30:00 -0400131 // generalized iterative algorithm for multiplying two matrices.
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500132 function multiply(m1, m2, size) {
133
134 if (skIsDebug && (!m1.every(isnumber) || !m2.every(isnumber))) {
135 throw 'Some members of matrices are NaN m1='+m1+', m2='+m2+'';
136 }
137 if (skIsDebug && (m1.length !== m2.length)) {
138 throw 'Undefined for matrices of different sizes. m1.length='+m1.length+', m2.length='+m2.length;
139 }
140 if (skIsDebug && (size*size !== m1.length)) {
141 throw 'Undefined for non-square matrices. array size was '+size;
142 }
143
144 var result = Array(m1.length);
145 for (var r = 0; r < size; r++) {
146 for (var c = 0; c < size; c++) {
147 // accumulate a sum of m1[r,k]*m2[k, c]
148 var acc = 0;
149 for (var k = 0; k < size; k++) {
150 acc += m1[size * r + k] * m2[size * k + c];
151 }
152 result[r * size + c] = acc;
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500153 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500154 }
155 return result;
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500156 };
157
158 // Accept an integer indicating the size of the matrices being multiplied (3 for 3x3), and any
159 // number of matrices following it.
160 function multiplyMany(size, listOfMatrices) {
161 if (skIsDebug && (listOfMatrices.length < 2)) {
162 throw 'multiplication expected two or more matrices';
163 }
164 var result = multiply(listOfMatrices[0], listOfMatrices[1], size);
165 var next = 2;
166 while (next < listOfMatrices.length) {
167 result = multiply(result, listOfMatrices[next], size);
168 next++;
169 }
170 return result;
171 };
172
173 // Accept any number 3x3 of matrices as arguments, multiply them together.
Kevin Lubick9e2d3842020-04-01 13:42:15 -0400174 // Matrix multiplication is associative but not commutative. the order of the arguments
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500175 // matters, but it does not matter that this implementation multiplies them left to right.
176 CanvasKit.SkMatrix.multiply = function() {
177 return multiplyMany(3, arguments);
178 };
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500179
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500180 // Return a matrix representing a rotation by n radians.
181 // px, py optionally say which point the rotation should be around
182 // with the default being (0, 0);
183 CanvasKit.SkMatrix.rotated = function(radians, px, py) {
184 px = px || 0;
185 py = py || 0;
186 var sinV = Math.sin(radians);
187 var cosV = Math.cos(radians);
188 return [
189 cosV, -sinV, sdot( sinV, py, 1 - cosV, px),
190 sinV, cosV, sdot(-sinV, px, 1 - cosV, py),
191 0, 0, 1,
192 ];
193 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400194
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500195 CanvasKit.SkMatrix.scaled = function(sx, sy, px, py) {
196 px = px || 0;
197 py = py || 0;
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500198 var m = stride([sx, sy], identityN(3), 3, 0, 1);
199 return stride([px-sx*px, py-sy*py], m, 3, 2, 0);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500200 };
Kevin Lubickda3d8ac2019-01-07 11:08:55 -0500201
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500202 CanvasKit.SkMatrix.skewed = function(kx, ky, px, py) {
203 px = px || 0;
204 py = py || 0;
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500205 var m = stride([kx, ky], identityN(3), 3, 1, -1);
206 return stride([-kx*px, -ky*py], m, 3, 2, 0);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500207 };
Alexander Khovansky3e119332018-11-15 02:01:19 +0300208
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500209 CanvasKit.SkMatrix.translated = function(dx, dy) {
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500210 return stride(arguments, identityN(3), 3, 2, 0);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500211 };
Kevin Lubick1646e7d2018-12-07 13:03:08 -0500212
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500213 // Functions for manipulating vectors.
214 // Loosely based off of SkV3 in SkM44.h but skia also has SkVec2 and Skv4. This combines them and
215 // works on vectors of any length.
216 CanvasKit.SkVector = {};
217 CanvasKit.SkVector.dot = function(a, b) {
218 if (skIsDebug && (a.length !== b.length)) {
219 throw 'Cannot perform dot product on arrays of different length ('+a.length+' vs '+b.length+')';
220 }
221 return a.map(function(v, i) { return v*b[i] }).reduce(function(acc, cur) { return acc + cur; });
222 }
223 CanvasKit.SkVector.lengthSquared = function(v) {
224 return CanvasKit.SkVector.dot(v, v);
225 }
226 CanvasKit.SkVector.length = function(v) {
227 return Math.sqrt(CanvasKit.SkVector.lengthSquared(v));
228 }
229 CanvasKit.SkVector.mulScalar = function(v, s) {
230 return v.map(function(i) { return i*s });
231 }
232 CanvasKit.SkVector.add = function(a, b) {
233 return a.map(function(v, i) { return v+b[i] });
234 }
235 CanvasKit.SkVector.sub = function(a, b) {
236 return a.map(function(v, i) { return v-b[i]; });
237 }
Nathaniel Nifongcc5415a2020-02-23 14:26:33 -0500238 CanvasKit.SkVector.dist = function(a, b) {
239 return CanvasKit.SkVector.length(CanvasKit.SkVector.sub(a, b));
240 }
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500241 CanvasKit.SkVector.normalize = function(v) {
242 return CanvasKit.SkVector.mulScalar(v, 1/CanvasKit.SkVector.length(v));
243 }
244 CanvasKit.SkVector.cross = function(a, b) {
245 if (skIsDebug && (a.length !== 3 || a.length !== 3)) {
246 throw 'Cross product is only defined for 3-dimensional vectors (a.length='+a.length+', b.length='+b.length+')';
247 }
248 return [
249 a[1]*b[2] - a[2]*b[1],
250 a[2]*b[0] - a[0]*b[2],
251 a[0]*b[1] - a[1]*b[0],
252 ];
253 }
254
Kevin Lubickc1d08982020-04-06 13:52:15 -0400255 // Functions for creating and manipulating (row-major) 4x4 matrices. Accepted in place of
256 // SkM44 in canvas methods, for the same reasons as the 3x3 matrices above.
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500257 // ported from C++ code in SkM44.cpp
258 CanvasKit.SkM44 = {};
259 // Create a 4x4 identity matrix
260 CanvasKit.SkM44.identity = function() {
261 return identityN(4);
262 }
263
264 // Anything named vec below is an array of length 3 representing a vector/point in 3D space.
265 // Create a 4x4 matrix representing a translate by the provided 3-vec
266 CanvasKit.SkM44.translated = function(vec) {
267 return stride(vec, identityN(4), 4, 3, 0);
268 }
269 // Create a 4x4 matrix representing a scaling by the provided 3-vec
270 CanvasKit.SkM44.scaled = function(vec) {
271 return stride(vec, identityN(4), 4, 0, 1);
272 }
273 // Create a 4x4 matrix representing a rotation about the provided axis 3-vec.
274 // axis does not need to be normalized.
275 CanvasKit.SkM44.rotated = function(axisVec, radians) {
276 return CanvasKit.SkM44.rotatedUnitSinCos(
277 CanvasKit.SkVector.normalize(axisVec), Math.sin(radians), Math.cos(radians));
278 }
279 // Create a 4x4 matrix representing a rotation about the provided normalized axis 3-vec.
280 // Rotation is provided redundantly as both sin and cos values.
281 // This rotate can be used when you already have the cosAngle and sinAngle values
282 // so you don't have to atan(cos/sin) to call roatated() which expects an angle in radians.
283 // this does no checking! Behavior for invalid sin or cos values or non-normalized axis vectors
Nathaniel Nifong3392ebe2020-06-01 09:21:36 -0400284 // is incorrect. Prefer rotated().
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500285 CanvasKit.SkM44.rotatedUnitSinCos = function(axisVec, sinAngle, cosAngle) {
286 var x = axisVec[0];
287 var y = axisVec[1];
288 var z = axisVec[2];
289 var c = cosAngle;
290 var s = sinAngle;
291 var t = 1 - c;
292 return [
293 t*x*x + c, t*x*y - s*z, t*x*z + s*y, 0,
294 t*x*y + s*z, t*y*y + c, t*y*z - s*x, 0,
295 t*x*z - s*y, t*y*z + s*x, t*z*z + c, 0,
296 0, 0, 0, 1
297 ];
298 }
299 // Create a 4x4 matrix representing a camera at eyeVec, pointed at centerVec.
300 CanvasKit.SkM44.lookat = function(eyeVec, centerVec, upVec) {
301 var f = CanvasKit.SkVector.normalize(CanvasKit.SkVector.sub(centerVec, eyeVec));
302 var u = CanvasKit.SkVector.normalize(upVec);
303 var s = CanvasKit.SkVector.normalize(CanvasKit.SkVector.cross(f, u));
304
305 var m = CanvasKit.SkM44.identity();
306 // set each column's top three numbers
Kevin Lubickc1d08982020-04-06 13:52:15 -0400307 stride(s, m, 4, 0, 0);
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500308 stride(CanvasKit.SkVector.cross(s, f), m, 4, 1, 0);
309 stride(CanvasKit.SkVector.mulScalar(f, -1), m, 4, 2, 0);
Kevin Lubickc1d08982020-04-06 13:52:15 -0400310 stride(eyeVec, m, 4, 3, 0);
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500311
312 var m2 = CanvasKit.SkM44.invert(m);
313 if (m2 === null) {
314 return CanvasKit.SkM44.identity();
315 }
316 return m2;
317 }
318 // Create a 4x4 matrix representing a perspective. All arguments are scalars.
319 // angle is in radians.
320 CanvasKit.SkM44.perspective = function(near, far, angle) {
321 if (skIsDebug && (far <= near)) {
Kevin Lubickbe728012020-09-03 11:57:12 +0000322 throw 'far must be greater than near when constructing SkM44 using perspective.';
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500323 }
324 var dInv = 1 / (far - near);
325 var halfAngle = angle / 2;
326 var cot = Math.cos(halfAngle) / Math.sin(halfAngle);
327 return [
328 cot, 0, 0, 0,
329 0, cot, 0, 0,
330 0, 0, (far+near)*dInv, 2*far*near*dInv,
331 0, 0, -1, 1,
332 ];
333 }
334 // Returns the number at the given row and column in matrix m.
335 CanvasKit.SkM44.rc = function(m, r, c) {
336 return m[r*4+c];
337 }
338 // Accepts any number of 4x4 matrix arguments, multiplies them left to right.
339 CanvasKit.SkM44.multiply = function() {
340 return multiplyMany(4, arguments);
341 }
342
343 // Invert the 4x4 matrix if it is invertible and return it. if not, return null.
344 // taken from SkM44.cpp (altered to use row-major order)
345 // m is not altered.
346 CanvasKit.SkM44.invert = function(m) {
347 if (skIsDebug && !m.every(isnumber)) {
348 throw 'some members of matrix are NaN m='+m;
349 }
350
351 var a00 = m[0];
352 var a01 = m[4];
353 var a02 = m[8];
354 var a03 = m[12];
355 var a10 = m[1];
356 var a11 = m[5];
357 var a12 = m[9];
358 var a13 = m[13];
359 var a20 = m[2];
360 var a21 = m[6];
361 var a22 = m[10];
362 var a23 = m[14];
363 var a30 = m[3];
364 var a31 = m[7];
365 var a32 = m[11];
366 var a33 = m[15];
367
368 var b00 = a00 * a11 - a01 * a10;
369 var b01 = a00 * a12 - a02 * a10;
370 var b02 = a00 * a13 - a03 * a10;
371 var b03 = a01 * a12 - a02 * a11;
372 var b04 = a01 * a13 - a03 * a11;
373 var b05 = a02 * a13 - a03 * a12;
374 var b06 = a20 * a31 - a21 * a30;
375 var b07 = a20 * a32 - a22 * a30;
376 var b08 = a20 * a33 - a23 * a30;
377 var b09 = a21 * a32 - a22 * a31;
378 var b10 = a21 * a33 - a23 * a31;
379 var b11 = a22 * a33 - a23 * a32;
380
381 // calculate determinate
382 var det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
383 var invdet = 1.0 / det;
384
385 // bail out if the matrix is not invertible
386 if (det === 0 || invdet === Infinity) {
387 SkDebug('Warning, uninvertible matrix');
388 return null;
389 }
390
391 b00 *= invdet;
392 b01 *= invdet;
393 b02 *= invdet;
394 b03 *= invdet;
395 b04 *= invdet;
396 b05 *= invdet;
397 b06 *= invdet;
398 b07 *= invdet;
399 b08 *= invdet;
400 b09 *= invdet;
401 b10 *= invdet;
402 b11 *= invdet;
403
404 // store result in row major order
405 var tmp = [
406 a11 * b11 - a12 * b10 + a13 * b09,
407 a12 * b08 - a10 * b11 - a13 * b07,
408 a10 * b10 - a11 * b08 + a13 * b06,
409 a11 * b07 - a10 * b09 - a12 * b06,
410
411 a02 * b10 - a01 * b11 - a03 * b09,
412 a00 * b11 - a02 * b08 + a03 * b07,
413 a01 * b08 - a00 * b10 - a03 * b06,
414 a00 * b09 - a01 * b07 + a02 * b06,
415
416 a31 * b05 - a32 * b04 + a33 * b03,
417 a32 * b02 - a30 * b05 - a33 * b01,
418 a30 * b04 - a31 * b02 + a33 * b00,
419 a31 * b01 - a30 * b03 - a32 * b00,
420
421 a22 * b04 - a21 * b05 - a23 * b03,
422 a20 * b05 - a22 * b02 + a23 * b01,
423 a21 * b02 - a20 * b04 - a23 * b00,
424 a20 * b03 - a21 * b01 + a22 * b00,
425 ];
426
427
428 if (!tmp.every(function(val) { return val !== NaN && val !== Infinity && val !== -Infinity; })) {
429 SkDebug('inverted matrix contains infinities or NaN '+tmp);
430 return null;
431 }
432 return tmp;
433 }
434
Nathaniel Nifongcc5415a2020-02-23 14:26:33 -0500435 CanvasKit.SkM44.transpose = function(m) {
436 return [
437 m[0], m[4], m[8], m[12],
438 m[1], m[5], m[9], m[13],
439 m[2], m[6], m[10], m[14],
440 m[3], m[7], m[11], m[15],
441 ];
442 }
Nathaniel Nifong77798b42020-02-21 17:15:22 -0500443
Nathaniel Nifong6130d502020-07-06 19:50:13 -0400444 // Return the inverse of an SkM44. throw an error if it's not invertible
445 CanvasKit.SkM44.mustInvert = function(m) {
446 var m2 = CanvasKit.SkM44.invert(m);
447 if (m2 === null) {
Kevin Lubickbe728012020-09-03 11:57:12 +0000448 throw 'Matrix not invertible';
Nathaniel Nifong6130d502020-07-06 19:50:13 -0400449 }
450 return m2;
451 }
452
453 // returns a matrix that sets up a 3D perspective view from a given camera.
454 //
455 // area - a rect describing the viewport. (0, 0, canvas_width, canvas_height) suggested
456 // zscale - a scalar describing the scale of the z axis. min(width, height)/2 suggested
457 // cam - an object with the following attributes
458 // const cam = {
459 // 'eye' : [0, 0, 1 / Math.tan(Math.PI / 24) - 1], // a 3D point locating the camera
460 // 'coa' : [0, 0, 0], // center of attention - the 3D point the camera is looking at.
461 // 'up' : [0, 1, 0], // a unit vector pointing in the camera's up direction, because eye and coa alone leave roll unspecified.
462 // 'near' : 0.02, // near clipping plane
463 // 'far' : 4, // far clipping plane
464 // 'angle': Math.PI / 12, // field of view in radians
465 // };
466 CanvasKit.SkM44.setupCamera = function(area, zscale, cam) {
467 var camera = CanvasKit.SkM44.lookat(cam['eye'], cam['coa'], cam['up']);
468 var perspective = CanvasKit.SkM44.perspective(cam['near'], cam['far'], cam['angle']);
Michael Ludwig1f49ceb2020-09-02 21:20:44 +0000469 var center = [(area.fLeft + area.fRight)/2, (area.fTop + area.fBottom)/2, 0];
470 var viewScale = [(area.fRight - area.fLeft)/2, (area.fBottom - area.fTop)/2, zscale];
Nathaniel Nifong6130d502020-07-06 19:50:13 -0400471 var viewport = CanvasKit.SkM44.multiply(
472 CanvasKit.SkM44.translated(center),
473 CanvasKit.SkM44.scaled(viewScale));
474 return CanvasKit.SkM44.multiply(
475 viewport, perspective, camera, CanvasKit.SkM44.mustInvert(viewport));
476 }
477
Kevin Lubickd3729342019-09-12 11:11:25 -0400478 // An SkColorMatrix is a 4x4 color matrix that transforms the 4 color channels
479 // with a 1x4 matrix that post-translates those 4 channels.
480 // For example, the following is the layout with the scale (S) and post-transform
481 // (PT) items indicated.
482 // RS, 0, 0, 0 | RPT
483 // 0, GS, 0, 0 | GPT
484 // 0, 0, BS, 0 | BPT
485 // 0, 0, 0, AS | APT
486 //
487 // Much of this was hand-transcribed from SkColorMatrix.cpp, because it's easier to
488 // deal with a Float32Array of length 20 than to try to expose the SkColorMatrix object.
489
Nathaniel Nifongcc5415a2020-02-23 14:26:33 -0500490 var rScale = 0;
491 var gScale = 6;
492 var bScale = 12;
493 var aScale = 18;
494
Kevin Lubickd3729342019-09-12 11:11:25 -0400495 var rPostTrans = 4;
496 var gPostTrans = 9;
497 var bPostTrans = 14;
498 var aPostTrans = 19;
499
500 CanvasKit.SkColorMatrix = {};
501 CanvasKit.SkColorMatrix.identity = function() {
Nathaniel Nifongcc5415a2020-02-23 14:26:33 -0500502 var m = new Float32Array(20);
503 m[rScale] = 1;
504 m[gScale] = 1;
505 m[bScale] = 1;
506 m[aScale] = 1;
507 return m;
Kevin Lubickd3729342019-09-12 11:11:25 -0400508 }
509
510 CanvasKit.SkColorMatrix.scaled = function(rs, gs, bs, as) {
Nathaniel Nifongcc5415a2020-02-23 14:26:33 -0500511 var m = new Float32Array(20);
512 m[rScale] = rs;
513 m[gScale] = gs;
514 m[bScale] = bs;
515 m[aScale] = as;
516 return m;
Kevin Lubickd3729342019-09-12 11:11:25 -0400517 }
518
519 var rotateIndices = [
520 [6, 7, 11, 12],
521 [0, 10, 2, 12],
522 [0, 1, 5, 6],
523 ];
524 // axis should be 0, 1, 2 for r, g, b
525 CanvasKit.SkColorMatrix.rotated = function(axis, sine, cosine) {
526 var m = CanvasKit.SkColorMatrix.identity();
527 var indices = rotateIndices[axis];
528 m[indices[0]] = cosine;
529 m[indices[1]] = sine;
530 m[indices[2]] = -sine;
531 m[indices[3]] = cosine;
532 return m;
533 }
534
535 // m is a SkColorMatrix (i.e. a Float32Array), and this sets the 4 "special"
536 // params that will translate the colors after they are multiplied by the 4x4 matrix.
537 CanvasKit.SkColorMatrix.postTranslate = function(m, dr, dg, db, da) {
538 m[rPostTrans] += dr;
539 m[gPostTrans] += dg;
540 m[bPostTrans] += db;
541 m[aPostTrans] += da;
542 return m;
543 }
544
545 // concat returns a new SkColorMatrix that is the result of multiplying outer*inner;
546 CanvasKit.SkColorMatrix.concat = function(outer, inner) {
547 var m = new Float32Array(20);
548 var index = 0;
549 for (var j = 0; j < 20; j += 5) {
550 for (var i = 0; i < 4; i++) {
551 m[index++] = outer[j + 0] * inner[i + 0] +
552 outer[j + 1] * inner[i + 5] +
553 outer[j + 2] * inner[i + 10] +
554 outer[j + 3] * inner[i + 15];
555 }
556 m[index++] = outer[j + 0] * inner[4] +
557 outer[j + 1] * inner[9] +
558 outer[j + 2] * inner[14] +
559 outer[j + 3] * inner[19] +
560 outer[j + 4];
561 }
562
563 return m;
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -0400564 };
565
566 CanvasKit.SkPath.MakeFromCmds = function(cmds) {
567 var ptrLen = loadCmdsTypedArray(cmds);
568 var path = CanvasKit.SkPath._MakeFromCmds(ptrLen[0], ptrLen[1]);
569 CanvasKit._free(ptrLen[0]);
570 return path;
571 };
572
573 // Deprecated
574 CanvasKit.MakePathFromCmds = CanvasKit.SkPath.MakeFromCmds;
575
576 // The weights array is optional (only used for conics).
577 CanvasKit.SkPath.MakeFromVerbsPointsWeights = function(verbs, pts, weights) {
Kevin Lubickbe728012020-09-03 11:57:12 +0000578 var verbsPtr = copy1dArray(verbs, 'HEAPU8');
579 var pointsPtr = copy1dArray(pts, 'HEAPF32');
580 var weightsPtr = copy1dArray(weights, 'HEAPF32');
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -0400581 var numWeights = (weights && weights.length) || 0;
582 var path = CanvasKit.SkPath._MakeFromVerbsPointsWeights(
583 verbsPtr, verbs.length, pointsPtr, pts.length, weightsPtr, numWeights);
584 freeArraysThatAreNotMallocedByUsers(verbsPtr, verbs);
585 freeArraysThatAreNotMallocedByUsers(pointsPtr, pts);
586 freeArraysThatAreNotMallocedByUsers(weightsPtr, weights);
587 return path;
588 };
Kevin Lubickd3729342019-09-12 11:11:25 -0400589
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500590 CanvasKit.SkPath.prototype.addArc = function(oval, startAngle, sweepAngle) {
591 // see arc() for the HTMLCanvas version
592 // note input angles are degrees.
Michael Ludwig1f49ceb2020-09-02 21:20:44 +0000593 this._addArc(oval, startAngle, sweepAngle);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500594 return this;
595 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400596
Kevin Lubicke384df42019-08-26 15:48:09 -0400597 CanvasKit.SkPath.prototype.addOval = function(oval, isCCW, startIndex) {
598 if (startIndex === undefined) {
599 startIndex = 1;
600 }
Michael Ludwig1f49ceb2020-09-02 21:20:44 +0000601 this._addOval(oval, !!isCCW, startIndex);
Kevin Lubicke384df42019-08-26 15:48:09 -0400602 return this;
603 };
604
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500605 CanvasKit.SkPath.prototype.addPath = function() {
606 // Takes 1, 2, 7, or 10 required args, where the first arg is always the path.
607 // The last arg is optional and chooses between add or extend mode.
608 // The options for the remaining args are:
609 // - an array of 6 or 9 parameters (perspective is optional)
610 // - the 9 parameters of a full matrix or
611 // the 6 non-perspective params of a matrix.
612 var args = Array.prototype.slice.call(arguments);
613 var path = args[0];
614 var extend = false;
Kevin Lubickbe728012020-09-03 11:57:12 +0000615 if (typeof args[args.length-1] === 'boolean') {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500616 extend = args.pop();
617 }
618 if (args.length === 1) {
619 // Add path, unchanged. Use identity matrix
620 this._addPath(path, 1, 0, 0,
621 0, 1, 0,
622 0, 0, 1,
623 extend);
624 } else if (args.length === 2) {
625 // User provided the 9 params of a full matrix as an array.
626 var a = args[1];
627 this._addPath(path, a[0], a[1], a[2],
628 a[3], a[4], a[5],
629 a[6] || 0, a[7] || 0, a[8] || 1,
630 extend);
631 } else if (args.length === 7 || args.length === 10) {
632 // User provided the 9 params of a (full) matrix directly.
633 // (or just the 6 non perspective ones)
634 // These are in the same order as what Skia expects.
635 var a = args;
636 this._addPath(path, a[1], a[2], a[3],
637 a[4], a[5], a[6],
638 a[7] || 0, a[8] || 0, a[9] || 1,
639 extend);
640 } else {
641 SkDebug('addPath expected to take 1, 2, 7, or 10 required args. Got ' + args.length);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400642 return null;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500643 }
644 return this;
645 };
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400646
Kevin Lubick37ab53e2019-11-11 10:06:08 -0500647 // points is either an array of [x, y] where x and y are numbers or
648 // a typed array from Malloc where the even indices will be treated
649 // as x coordinates and the odd indices will be treated as y coordinates.
650 CanvasKit.SkPath.prototype.addPoly = function(points, close) {
651 var ptr;
652 var n;
653 // This was created with CanvasKit.Malloc, so assume the user has
654 // already been filled with data.
655 if (points['_ck']) {
656 ptr = points.byteOffset;
657 n = points.length/2;
658 } else {
Kevin Lubickbe728012020-09-03 11:57:12 +0000659 ptr = copy2dArray(points, 'HEAPF32');
Kevin Lubick37ab53e2019-11-11 10:06:08 -0500660 n = points.length;
661 }
662 this._addPoly(ptr, n, close);
Kevin Lubickcf118922020-05-28 14:43:38 -0400663 freeArraysThatAreNotMallocedByUsers(ptr, points);
Kevin Lubick37ab53e2019-11-11 10:06:08 -0500664 return this;
665 };
666
Michael Ludwig1f49ceb2020-09-02 21:20:44 +0000667 CanvasKit.SkPath.prototype.addRect = function() {
668 // Takes 1, 2, 4 or 5 args
669 // - SkRect
670 // - SkRect, isCCW
671 // - left, top, right, bottom
672 // - left, top, right, bottom, isCCW
673 if (arguments.length === 1 || arguments.length === 2) {
674 var r = arguments[0];
675 var ccw = arguments[1] || false;
676 this._addRect(r.fLeft, r.fTop, r.fRight, r.fBottom, ccw);
677 } else if (arguments.length === 4 || arguments.length === 5) {
678 var a = arguments;
679 this._addRect(a[0], a[1], a[2], a[3], a[4] || false);
680 } else {
681 SkDebug('addRect expected to take 1, 2, 4, or 5 args. Got ' + arguments.length);
682 return null;
683 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500684 return this;
685 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400686
Michael Ludwig1f49ceb2020-09-02 21:20:44 +0000687 CanvasKit.SkPath.prototype.addRoundRect = function() {
688 // Takes 3, 4, 6 or 7 args
689 // - SkRect, radii (an array of 8 numbers), ccw
690 // - SkRect, rx, ry, ccw
691 // - left, top, right, bottom, radii, ccw
692 // - left, top, right, bottom, rx, ry, ccw
693 var args = arguments;
694 if (args.length === 3 || args.length === 6) {
695 var radii = args[args.length-2];
696 } else if (args.length === 4 || args.length === 7){
697 // duplicate the given (rx, ry) pairs for each corner.
698 var rx = args[args.length-3];
699 var ry = args[args.length-2];
700 var radii = [rx, ry, rx, ry, rx, ry, rx, ry];
701 } else {
702 SkDebug('addRoundRect expected to take 3, 4, 6, or 7 args. Got ' + args.length);
703 return null;
704 }
705 if (radii.length !== 8) {
706 SkDebug('addRoundRect needs 8 radii provided. Got ' + radii.length);
707 return null;
708 }
Kevin Lubickbe728012020-09-03 11:57:12 +0000709 var rptr = copy1dArray(radii, 'HEAPF32');
Michael Ludwig1f49ceb2020-09-02 21:20:44 +0000710 if (args.length === 3 || args.length === 4) {
711 var r = args[0];
712 var ccw = args[args.length - 1];
713 this._addRoundRect(r.fLeft, r.fTop, r.fRight, r.fBottom, rptr, ccw);
714 } else if (args.length === 6 || args.length === 7) {
715 var a = args;
716 this._addRoundRect(a[0], a[1], a[2], a[3], rptr, ccw);
717 }
718 freeArraysThatAreNotMallocedByUsers(rptr, radii);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500719 return this;
720 };
721
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -0400722 // The weights array is optional (only used for conics).
723 CanvasKit.SkPath.prototype.addVerbsPointsWeights = function(verbs, points, weights) {
Kevin Lubickbe728012020-09-03 11:57:12 +0000724 var verbsPtr = copy1dArray(verbs, 'HEAPU8');
725 var pointsPtr = copy1dArray(points, 'HEAPF32');
726 var weightsPtr = copy1dArray(weights, 'HEAPF32');
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -0400727 var numWeights = (weights && weights.length) || 0;
728 this._addVerbsPointsWeights(verbsPtr, verbs.length, pointsPtr, points.length,
729 weightsPtr, numWeights);
730 freeArraysThatAreNotMallocedByUsers(verbsPtr, verbs);
731 freeArraysThatAreNotMallocedByUsers(pointsPtr, points);
732 freeArraysThatAreNotMallocedByUsers(weightsPtr, weights);
733 };
734
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500735 CanvasKit.SkPath.prototype.arc = function(x, y, radius, startAngle, endAngle, ccw) {
736 // emulates the HTMLCanvas behavior. See addArc() for the SkPath version.
737 // Note input angles are radians.
738 var bounds = CanvasKit.LTRBRect(x-radius, y-radius, x+radius, y+radius);
739 var sweep = radiansToDegrees(endAngle - startAngle) - (360 * !!ccw);
740 var temp = new CanvasKit.SkPath();
741 temp.addArc(bounds, radiansToDegrees(startAngle), sweep);
742 this.addPath(temp, true);
743 temp.delete();
744 return this;
745 };
746
Michael Ludwig1f49ceb2020-09-02 21:20:44 +0000747 // Deprecated, use one of the three variants below depending on how many args you were calling it with.
748 CanvasKit.SkPath.prototype.arcTo = function() {
749 // takes 4, 5 or 7 args
750 // - 5 x1, y1, x2, y2, radius
751 // - 4 oval (as Rect), startAngle, sweepAngle, forceMoveTo
752 // - 7 rx, ry, xAxisRotate, useSmallArc, isCCW, x, y
753 var args = arguments;
754 if (args.length === 5) {
755 this._arcToTangent(args[0], args[1], args[2], args[3], args[4]);
756 } else if (args.length === 4) {
757 this._arcToOval(args[0], args[1], args[2], args[3]);
758 } else if (args.length === 7) {
759 this._arcToRotated(args[0], args[1], args[2], !!args[3], !!args[4], args[5], args[6]);
760 } else {
761 throw 'Invalid args for arcTo. Expected 4, 5, or 7, got '+ args.length;
762 }
763
764 return this;
765 };
766
Nathaniel Nifongd0c9d0c2020-07-15 16:46:17 -0400767 // Appends arc to SkPath. Arc added is part of ellipse
768 // bounded by oval, from startAngle through sweepAngle. Both startAngle and
769 // sweepAngle are measured in degrees, where zero degrees is aligned with the
770 // positive x-axis, and positive sweeps extends arc clockwise.
771 CanvasKit.SkPath.prototype.arcToOval = function(oval, startAngle, sweepAngle, forceMoveTo) {
Michael Ludwig1f49ceb2020-09-02 21:20:44 +0000772 this._arcToOval(oval, startAngle, sweepAngle, forceMoveTo);
Nathaniel Nifongd0c9d0c2020-07-15 16:46:17 -0400773 return this;
774 };
775
776 // Appends arc to SkPath. Arc is implemented by one or more conics weighted to
777 // describe part of oval with radii (rx, ry) rotated by xAxisRotate degrees. Arc
778 // curves from last SkPath SkPoint to (x, y), choosing one of four possible routes:
779 // clockwise or counterclockwise, and smaller or larger.
780
781 // Arc sweep is always less than 360 degrees. arcTo() appends line to (x, y) if
782 // either radii are zero, or if last SkPath SkPoint equals (x, y). arcTo() scales radii
783 // (rx, ry) to fit last SkPath SkPoint and (x, y) if both are greater than zero but
784 // too small.
785
786 // arcToRotated() appends up to four conic curves.
787 // arcToRotated() implements the functionality of SVG arc, although SVG sweep-flag value
788 // is opposite the integer value of sweep; SVG sweep-flag uses 1 for clockwise,
789 // while kCW_Direction cast to int is zero.
790 CanvasKit.SkPath.prototype.arcToRotated = function(rx, ry, xAxisRotate, useSmallArc, isCCW, x, y) {
791 this._arcToRotated(rx, ry, xAxisRotate, !!useSmallArc, !!isCCW, x, y);
792 return this;
793 };
794
795 // Appends arc to SkPath, after appending line if needed. Arc is implemented by conic
796 // weighted to describe part of circle. Arc is contained by tangent from
797 // last SkPath point to (x1, y1), and tangent from (x1, y1) to (x2, y2). Arc
798 // is part of circle sized to radius, positioned so it touches both tangent lines.
799
800 // If last Path Point does not start Arc, arcTo appends connecting Line to Path.
801 // The length of Vector from (x1, y1) to (x2, y2) does not affect Arc.
802
803 // Arc sweep is always less than 180 degrees. If radius is zero, or if
804 // tangents are nearly parallel, arcTo appends Line from last Path Point to (x1, y1).
805
806 // arcToTangent appends at most one Line and one conic.
807 // arcToTangent implements the functionality of PostScript arct and HTML Canvas arcTo.
808 CanvasKit.SkPath.prototype.arcToTangent = function(x1, y1, x2, y2, radius) {
809 this._arcToTangent(x1, y1, x2, y2, radius);
810 return this;
811 };
812
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500813 CanvasKit.SkPath.prototype.close = function() {
814 this._close();
815 return this;
816 };
817
818 CanvasKit.SkPath.prototype.conicTo = function(x1, y1, x2, y2, w) {
819 this._conicTo(x1, y1, x2, y2, w);
820 return this;
821 };
822
823 CanvasKit.SkPath.prototype.cubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) {
824 this._cubicTo(cp1x, cp1y, cp2x, cp2y, x, y);
825 return this;
826 };
827
828 CanvasKit.SkPath.prototype.dash = function(on, off, phase) {
829 if (this._dash(on, off, phase)) {
Kevin Lubick217056c2018-09-20 17:39:31 -0400830 return this;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500831 }
832 return null;
833 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400834
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500835 CanvasKit.SkPath.prototype.lineTo = function(x, y) {
836 this._lineTo(x, y);
837 return this;
838 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400839
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500840 CanvasKit.SkPath.prototype.moveTo = function(x, y) {
841 this._moveTo(x, y);
842 return this;
843 };
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400844
Kevin Lubicke384df42019-08-26 15:48:09 -0400845 CanvasKit.SkPath.prototype.offset = function(dx, dy) {
846 this._transform(1, 0, dx,
847 0, 1, dy,
848 0, 0, 1);
849 return this;
850 };
851
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500852 CanvasKit.SkPath.prototype.quadTo = function(cpx, cpy, x, y) {
853 this._quadTo(cpx, cpy, x, y);
854 return this;
855 };
856
Kevin Lubick79b71342019-11-01 14:36:52 -0400857 CanvasKit.SkPath.prototype.rArcTo = function(rx, ry, xAxisRotate, useSmallArc, isCCW, dx, dy) {
858 this._rArcTo(rx, ry, xAxisRotate, useSmallArc, isCCW, dx, dy);
859 return this;
860 };
861
862 CanvasKit.SkPath.prototype.rConicTo = function(dx1, dy1, dx2, dy2, w) {
863 this._rConicTo(dx1, dy1, dx2, dy2, w);
864 return this;
865 };
866
867 // These params are all relative
868 CanvasKit.SkPath.prototype.rCubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) {
869 this._rCubicTo(cp1x, cp1y, cp2x, cp2y, x, y);
870 return this;
871 };
872
873 CanvasKit.SkPath.prototype.rLineTo = function(dx, dy) {
874 this._rLineTo(dx, dy);
875 return this;
876 };
877
878 CanvasKit.SkPath.prototype.rMoveTo = function(dx, dy) {
879 this._rMoveTo(dx, dy);
880 return this;
881 };
882
883 // These params are all relative
884 CanvasKit.SkPath.prototype.rQuadTo = function(cpx, cpy, x, y) {
885 this._rQuadTo(cpx, cpy, x, y);
886 return this;
887 };
888
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500889 CanvasKit.SkPath.prototype.stroke = function(opts) {
890 // Fill out any missing values with the default values.
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500891 opts = opts || {};
Kevin Lubick6aa38692020-06-01 11:25:47 -0400892 opts['width'] = opts['width'] || 1;
893 opts['miter_limit'] = opts['miter_limit'] || 4;
894 opts['cap'] = opts['cap'] || CanvasKit.StrokeCap.Butt;
895 opts['join'] = opts['join'] || CanvasKit.StrokeJoin.Miter;
896 opts['precision'] = opts['precision'] || 1;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500897 if (this._stroke(opts)) {
898 return this;
899 }
900 return null;
901 };
902
903 CanvasKit.SkPath.prototype.transform = function() {
904 // Takes 1 or 9 args
905 if (arguments.length === 1) {
906 // argument 1 should be a 6 or 9 element array.
907 var a = arguments[0];
908 this._transform(a[0], a[1], a[2],
909 a[3], a[4], a[5],
910 a[6] || 0, a[7] || 0, a[8] || 1);
911 } else if (arguments.length === 6 || arguments.length === 9) {
912 // these arguments are the 6 or 9 members of the matrix
913 var a = arguments;
914 this._transform(a[0], a[1], a[2],
915 a[3], a[4], a[5],
916 a[6] || 0, a[7] || 0, a[8] || 1);
917 } else {
918 throw 'transform expected to take 1 or 9 arguments. Got ' + arguments.length;
919 }
920 return this;
921 };
922 // isComplement is optional, defaults to false
923 CanvasKit.SkPath.prototype.trim = function(startT, stopT, isComplement) {
924 if (this._trim(startT, stopT, !!isComplement)) {
925 return this;
926 }
927 return null;
928 };
929
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500930 CanvasKit.SkImage.prototype.encodeToData = function() {
931 if (!arguments.length) {
932 return this._encodeToData();
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400933 }
Kevin Lubick53965c92018-10-11 08:51:55 -0400934
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500935 if (arguments.length === 2) {
936 var a = arguments;
937 return this._encodeToDataWithFormat(a[0], a[1]);
Alexander Khovansky3e119332018-11-15 02:01:19 +0300938 }
939
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500940 throw 'encodeToData expected to take 0 or 2 arguments. Got ' + arguments.length;
941 }
Kevin Lubick1ba9c4d2019-02-22 10:04:06 -0500942
Kevin Lubicka064c282019-04-04 09:28:53 -0400943 CanvasKit.SkImage.prototype.makeShader = function(xTileMode, yTileMode, localMatrix) {
Kevin Lubick6bffe392020-04-02 15:24:15 -0400944 var localMatrixPtr = copy3x3MatrixToWasm(localMatrix);
Kevin Lubick6aa38692020-06-01 11:25:47 -0400945 return this._makeShader(xTileMode, yTileMode, localMatrixPtr);
Kevin Lubicka064c282019-04-04 09:28:53 -0400946 }
947
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400948 CanvasKit.SkImage.prototype.readPixels = function(imageInfo, srcX, srcY) {
949 var rowBytes;
Kevin Lubickbe728012020-09-03 11:57:12 +0000950 // Important to use ['string'] notation here, otherwise the closure compiler will
Kevin Lubick319524b2020-01-22 15:29:14 -0500951 // minify away the colorType.
Kevin Lubickbe728012020-09-03 11:57:12 +0000952 switch (imageInfo['colorType']) {
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400953 case CanvasKit.ColorType.RGBA_8888:
954 rowBytes = imageInfo.width * 4; // 1 byte per channel == 4 bytes per pixel in 8888
955 break;
956 case CanvasKit.ColorType.RGBA_F32:
957 rowBytes = imageInfo.width * 16; // 4 bytes per channel == 16 bytes per pixel in F32
958 break;
959 default:
Kevin Lubickbe728012020-09-03 11:57:12 +0000960 SkDebug('Colortype not yet supported');
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400961 return;
962 }
963 var pBytes = rowBytes * imageInfo.height;
964 var pPtr = CanvasKit._malloc(pBytes);
965
966 if (!this._readPixels(imageInfo, pPtr, rowBytes, srcX, srcY)) {
Kevin Lubickbe728012020-09-03 11:57:12 +0000967 SkDebug('Could not read pixels with the given inputs');
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400968 return null;
969 }
970
971 // Put those pixels into a typed array of the right format and then
972 // make a copy with slice() that we can return.
973 var retVal = null;
Kevin Lubickbe728012020-09-03 11:57:12 +0000974 switch (imageInfo['colorType']) {
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400975 case CanvasKit.ColorType.RGBA_8888:
Bryce Thomas1fa54042020-01-14 13:46:30 -0800976 retVal = new Uint8Array(CanvasKit.HEAPU8.buffer, pPtr, pBytes).slice();
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400977 break;
978 case CanvasKit.ColorType.RGBA_F32:
Bryce Thomas1fa54042020-01-14 13:46:30 -0800979 retVal = new Float32Array(CanvasKit.HEAPU8.buffer, pPtr, pBytes).slice();
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400980 break;
981 }
982
983 // Free the allocated pixels in the WASM memory
984 CanvasKit._free(pPtr);
985 return retVal;
Kevin Lubick6bffe392020-04-02 15:24:15 -0400986 }
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400987
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400988 // Accepts an array of four numbers in the range of 0-1 representing a 4f color
Kevin Lubickbe728012020-09-03 11:57:12 +0000989 CanvasKit.SkCanvas.prototype.clear = function(color4f) {
Kevin Lubick6aa38692020-06-01 11:25:47 -0400990 var cPtr = copyColorToWasm(color4f);
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400991 this._clear(cPtr);
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400992 }
993
Kevin Lubickbe728012020-09-03 11:57:12 +0000994 CanvasKit.SkCanvas.prototype.clipRRect = function(rrect, op, antialias) {
995 var rPtr = copyRRectToWasm(rrect);
996 this._clipRRect(rPtr, op, antialias);
997 }
998
Kevin Lubickc1d08982020-04-06 13:52:15 -0400999 // concat takes a 3x2, a 3x3, or a 4x4 matrix and upscales it (if needed) to 4x4. This is because
1000 // under the hood, SkCanvas uses a 4x4 matrix.
Kevin Lubick6bffe392020-04-02 15:24:15 -04001001 CanvasKit.SkCanvas.prototype.concat = function(matr) {
Kevin Lubickc1d08982020-04-06 13:52:15 -04001002 var matrPtr = copy4x4MatrixToWasm(matr);
Kevin Lubick6bffe392020-04-02 15:24:15 -04001003 this._concat(matrPtr);
Kevin Lubickd6b32ed2019-05-06 13:04:03 -04001004 }
1005
Kevin Lubickc1d08982020-04-06 13:52:15 -04001006 // Deprecated - just use concat
1007 CanvasKit.SkCanvas.prototype.concat44 = CanvasKit.SkCanvas.prototype.concat;
1008
Kevin Lubickee91c072019-03-29 10:39:52 -04001009 // atlas is an SkImage, e.g. from CanvasKit.MakeImageFromEncoded
Nathaniel Nifongcd75b172020-06-05 11:17:43 -04001010 // srcRects, dstXforms, and colors should be CanvasKit.SkRectBuilder, CanvasKit.RSXFormBuilder,
1011 // and CanvasKit.SkColorBuilder (fastest)
1012 // Or they can be an array of floats of length 4*number of destinations.
1013 // colors are optional and used to tint the drawn images using the optional blend mode
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001014 // Colors may be an SkColorBuilder, a Uint32Array of int colors,
1015 // a Flat Float32Array of float colors or a 2d Array of Float32Array(4) (deprecated)
Kevin Lubickee91c072019-03-29 10:39:52 -04001016 CanvasKit.SkCanvas.prototype.drawAtlas = function(atlas, srcRects, dstXforms, paint,
1017 /*optional*/ blendMode, colors) {
1018 if (!atlas || !paint || !srcRects || !dstXforms) {
1019 SkDebug('Doing nothing since missing a required input');
1020 return;
1021 }
Nathaniel Nifongcd75b172020-06-05 11:17:43 -04001022
1023 // builder arguments report the length as the number of rects, but when passed as arrays
1024 // their.length attribute is 4x higher because it's the number of total components of all rects.
1025 // colors is always going to report the same length, at least until floats colors are supported
1026 // by this function.
1027 if (srcRects.length !== dstXforms.length) {
Kevin Lubickee91c072019-03-29 10:39:52 -04001028 SkDebug('Doing nothing since input arrays length mismatches');
Nathaniel Nifonge5d32542020-03-26 09:27:48 -04001029 return;
Kevin Lubickee91c072019-03-29 10:39:52 -04001030 }
1031 if (!blendMode) {
1032 blendMode = CanvasKit.BlendMode.SrcOver;
1033 }
1034
1035 var srcRectPtr;
1036 if (srcRects.build) {
1037 srcRectPtr = srcRects.build();
1038 } else {
Kevin Lubickbe728012020-09-03 11:57:12 +00001039 srcRectPtr = copy1dArray(srcRects, 'HEAPF32');
Kevin Lubickee91c072019-03-29 10:39:52 -04001040 }
1041
Nathaniel Nifongcd75b172020-06-05 11:17:43 -04001042 var count = 1;
Kevin Lubickee91c072019-03-29 10:39:52 -04001043 var dstXformPtr;
1044 if (dstXforms.build) {
1045 dstXformPtr = dstXforms.build();
Nathaniel Nifongcd75b172020-06-05 11:17:43 -04001046 count = dstXforms.length;
Kevin Lubickee91c072019-03-29 10:39:52 -04001047 } else {
Kevin Lubickbe728012020-09-03 11:57:12 +00001048 dstXformPtr = copy1dArray(dstXforms, 'HEAPF32');
Nathaniel Nifongcd75b172020-06-05 11:17:43 -04001049 count = dstXforms.length / 4;
Kevin Lubickee91c072019-03-29 10:39:52 -04001050 }
1051
Kevin Lubick6bffe392020-04-02 15:24:15 -04001052 var colorPtr = nullptr;
Kevin Lubickee91c072019-03-29 10:39:52 -04001053 if (colors) {
1054 if (colors.build) {
1055 colorPtr = colors.build();
1056 } else {
Kevin Lubickbe728012020-09-03 11:57:12 +00001057 colorPtr = copy1dArray(assureIntColors(colors), 'HEAPU32');
Kevin Lubickee91c072019-03-29 10:39:52 -04001058 }
1059 }
1060
Nathaniel Nifongcd75b172020-06-05 11:17:43 -04001061 this._drawAtlas(atlas, dstXformPtr, srcRectPtr, colorPtr, count, blendMode, paint);
Kevin Lubickee91c072019-03-29 10:39:52 -04001062
1063 if (srcRectPtr && !srcRects.build) {
Kevin Lubickcf118922020-05-28 14:43:38 -04001064 freeArraysThatAreNotMallocedByUsers(srcRectPtr, srcRects);
Kevin Lubickee91c072019-03-29 10:39:52 -04001065 }
1066 if (dstXformPtr && !dstXforms.build) {
Kevin Lubickcf118922020-05-28 14:43:38 -04001067 freeArraysThatAreNotMallocedByUsers(dstXformPtr, dstXforms);
Kevin Lubickee91c072019-03-29 10:39:52 -04001068 }
1069 if (colorPtr && !colors.build) {
Kevin Lubickcf118922020-05-28 14:43:38 -04001070 freeArraysThatAreNotMallocedByUsers(colorPtr, colors);
Kevin Lubickee91c072019-03-29 10:39:52 -04001071 }
Kevin Lubickee91c072019-03-29 10:39:52 -04001072 }
1073
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001074 CanvasKit.SkCanvas.prototype.drawColor = function (color4f, mode) {
Kevin Lubick6aa38692020-06-01 11:25:47 -04001075 var cPtr = copyColorToWasm(color4f);
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001076 if (mode !== undefined) {
1077 this._drawColor(cPtr, mode);
1078 } else {
1079 this._drawColor(cPtr);
1080 }
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001081 }
1082
Kevin Lubick93f1a382020-06-02 16:15:23 -04001083 CanvasKit.SkCanvas.prototype.drawColorComponents = function (r, g, b, a, mode) {
1084 var cPtr = copyColorComponentsToWasm(r, g, b, a);
1085 if (mode !== undefined) {
1086 this._drawColor(cPtr, mode);
1087 } else {
1088 this._drawColor(cPtr);
1089 }
1090 }
1091
Kevin Lubickbe728012020-09-03 11:57:12 +00001092 CanvasKit.SkCanvas.prototype.drawDRRect = function(outer, inner, paint) {
1093 var oPtr = copyRRectToWasm(outer, _scratchRRectPtr);
1094 var iPtr = copyRRectToWasm(inner, _scratchRRect2Ptr);
1095 this._drawDRRect(oPtr, iPtr, paint);
1096 }
1097
Kevin Lubick37ab53e2019-11-11 10:06:08 -05001098 // points is either an array of [x, y] where x and y are numbers or
1099 // a typed array from Malloc where the even indices will be treated
1100 // as x coordinates and the odd indices will be treated as y coordinates.
1101 CanvasKit.SkCanvas.prototype.drawPoints = function(mode, points, paint) {
1102 var ptr;
1103 var n;
1104 // This was created with CanvasKit.Malloc, so assume the user has
1105 // already been filled with data.
1106 if (points['_ck']) {
1107 ptr = points.byteOffset;
1108 n = points.length/2;
1109 } else {
Kevin Lubickbe728012020-09-03 11:57:12 +00001110 ptr = copy2dArray(points, 'HEAPF32');
Kevin Lubick37ab53e2019-11-11 10:06:08 -05001111 n = points.length;
1112 }
1113 this._drawPoints(mode, ptr, n, paint);
Kevin Lubickcf118922020-05-28 14:43:38 -04001114 freeArraysThatAreNotMallocedByUsers(ptr, points);
Kevin Lubick37ab53e2019-11-11 10:06:08 -05001115 }
1116
Kevin Lubickbe728012020-09-03 11:57:12 +00001117 CanvasKit.SkCanvas.prototype.drawRRect = function(rrect, paint) {
1118 var rPtr = copyRRectToWasm(rrect);
1119 this._drawRRect(rPtr, paint);
1120 }
1121
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001122 CanvasKit.SkCanvas.prototype.drawShadow = function(path, zPlaneParams, lightPos, lightRadius, ambientColor, spotColor, flags) {
Kevin Lubick6aa38692020-06-01 11:25:47 -04001123 var ambiPtr = copyColorToWasmNoScratch(ambientColor);
1124 var spotPtr = copyColorToWasmNoScratch(spotColor);
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001125 this._drawShadow(path, zPlaneParams, lightPos, lightRadius, ambiPtr, spotPtr, flags);
Kevin Lubickcf118922020-05-28 14:43:38 -04001126 freeArraysThatAreNotMallocedByUsers(ambiPtr, ambientColor);
1127 freeArraysThatAreNotMallocedByUsers(spotPtr, spotColor);
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001128 }
1129
Kevin Lubickc1d08982020-04-06 13:52:15 -04001130 // getLocalToDevice returns a 4x4 matrix.
1131 CanvasKit.SkCanvas.prototype.getLocalToDevice = function() {
Kevin Lubickc1d08982020-04-06 13:52:15 -04001132 // _getLocalToDevice will copy the values into the pointer.
Kevin Lubick6aa38692020-06-01 11:25:47 -04001133 this._getLocalToDevice(_scratch4x4MatrixPtr);
1134 return copy4x4MatrixFromWasm(_scratch4x4MatrixPtr);
Kevin Lubickc1d08982020-04-06 13:52:15 -04001135 }
1136
Nathaniel Nifong00de91c2020-05-06 16:22:33 -04001137 // findMarkedCTM returns a 4x4 matrix, or null if a matrix was not found at
1138 // the provided marker.
1139 CanvasKit.SkCanvas.prototype.findMarkedCTM = function(marker) {
Nathaniel Nifong00de91c2020-05-06 16:22:33 -04001140 // _getLocalToDevice will copy the values into the pointer.
Kevin Lubick6aa38692020-06-01 11:25:47 -04001141 var found = this._findMarkedCTM(marker, _scratch4x4MatrixPtr);
Nathaniel Nifong00de91c2020-05-06 16:22:33 -04001142 if (!found) {
1143 return null;
1144 }
Kevin Lubick6aa38692020-06-01 11:25:47 -04001145 return copy4x4MatrixFromWasm(_scratch4x4MatrixPtr);
Nathaniel Nifong00de91c2020-05-06 16:22:33 -04001146 }
1147
Kevin Lubickc1d08982020-04-06 13:52:15 -04001148 // getTotalMatrix returns the current matrix as a 3x3 matrix.
Kevin Lubick6bffe392020-04-02 15:24:15 -04001149 CanvasKit.SkCanvas.prototype.getTotalMatrix = function() {
Kevin Lubick6bffe392020-04-02 15:24:15 -04001150 // _getTotalMatrix will copy the values into the pointer.
Kevin Lubick6aa38692020-06-01 11:25:47 -04001151 this._getTotalMatrix(_scratch3x3MatrixPtr);
Kevin Lubick6bffe392020-04-02 15:24:15 -04001152 // read them out into an array. TODO(kjlubick): If we change SkMatrix to be
1153 // typedArrays, then we should return a typed array here too.
1154 var rv = new Array(9);
1155 for (var i = 0; i < 9; i++) {
Kevin Lubick6aa38692020-06-01 11:25:47 -04001156 rv[i] = CanvasKit.HEAPF32[_scratch3x3MatrixPtr/4 + i]; // divide by 4 to "cast" to float.
Kevin Lubick6bffe392020-04-02 15:24:15 -04001157 }
Kevin Lubick6bffe392020-04-02 15:24:15 -04001158 return rv;
1159 }
1160
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001161 // returns Uint8Array
1162 CanvasKit.SkCanvas.prototype.readPixels = function(x, y, w, h, alphaType,
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001163 colorType, colorSpace, dstRowBytes) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001164 // supply defaults (which are compatible with HTMLCanvas's getImageData)
1165 alphaType = alphaType || CanvasKit.AlphaType.Unpremul;
1166 colorType = colorType || CanvasKit.ColorType.RGBA_8888;
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001167 colorSpace = colorSpace || CanvasKit.SkColorSpace.SRGB;
1168 var pixBytes = 4;
1169 if (colorType === CanvasKit.ColorType.RGBA_F16) {
1170 pixBytes = 8;
1171 }
1172 dstRowBytes = dstRowBytes || (pixBytes * w);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001173
1174 var len = h * dstRowBytes
1175 var pptr = CanvasKit._malloc(len);
1176 var ok = this._readPixels({
1177 'width': w,
1178 'height': h,
Kevin Lubick52b9f372018-12-04 13:57:36 -05001179 'colorType': colorType,
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001180 'alphaType': alphaType,
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001181 'colorSpace': colorSpace,
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001182 }, pptr, dstRowBytes, x, y);
1183 if (!ok) {
1184 CanvasKit._free(pptr);
1185 return null;
1186 }
1187
1188 // The first typed array is just a view into memory. Because we will
1189 // be free-ing that, we call slice to make a persistent copy.
Bryce Thomas1fa54042020-01-14 13:46:30 -08001190 var pixels = new Uint8Array(CanvasKit.HEAPU8.buffer, pptr, len).slice();
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001191 CanvasKit._free(pptr);
1192 return pixels;
1193 }
1194
Kevin Lubickcf118922020-05-28 14:43:38 -04001195 // pixels should be a Uint8Array or a plain JS array.
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001196 CanvasKit.SkCanvas.prototype.writePixels = function(pixels, srcWidth, srcHeight,
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001197 destX, destY, alphaType, colorType, colorSpace) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001198 if (pixels.byteLength % (srcWidth * srcHeight)) {
1199 throw 'pixels length must be a multiple of the srcWidth * srcHeight';
1200 }
1201 var bytesPerPixel = pixels.byteLength / (srcWidth * srcHeight);
1202 // supply defaults (which are compatible with HTMLCanvas's putImageData)
1203 alphaType = alphaType || CanvasKit.AlphaType.Unpremul;
1204 colorType = colorType || CanvasKit.ColorType.RGBA_8888;
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001205 colorSpace = colorSpace || CanvasKit.SkColorSpace.SRGB;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001206 var srcRowBytes = bytesPerPixel * srcWidth;
1207
Kevin Lubickbe728012020-09-03 11:57:12 +00001208 var pptr = copy1dArray(pixels, 'HEAPU8');
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001209 var ok = this._writePixels({
1210 'width': srcWidth,
1211 'height': srcHeight,
1212 'colorType': colorType,
1213 'alphaType': alphaType,
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001214 'colorSpace': colorSpace,
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001215 }, pptr, srcRowBytes, destX, destY);
1216
Kevin Lubickcf118922020-05-28 14:43:38 -04001217 freeArraysThatAreNotMallocedByUsers(pptr, pixels);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001218 return ok;
Kevin Lubick52b9f372018-12-04 13:57:36 -05001219 }
1220
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001221 CanvasKit.SkColorFilter.MakeBlend = function(color4f, mode) {
Kevin Lubick6aa38692020-06-01 11:25:47 -04001222 var cPtr = copyColorToWasm(color4f);
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001223 var result = CanvasKit.SkColorFilter._MakeBlend(cPtr, mode);
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001224 return result;
1225 }
1226
Kevin Lubickd3729342019-09-12 11:11:25 -04001227 // colorMatrix is an SkColorMatrix (e.g. Float32Array of length 20)
1228 CanvasKit.SkColorFilter.MakeMatrix = function(colorMatrix) {
1229 if (!colorMatrix || colorMatrix.length !== 20) {
Kevin Lubick6bffe392020-04-02 15:24:15 -04001230 throw 'invalid color matrix';
Kevin Lubickd3729342019-09-12 11:11:25 -04001231 }
Kevin Lubickbe728012020-09-03 11:57:12 +00001232 var fptr = copy1dArray(colorMatrix, 'HEAPF32');
Kevin Lubickd3729342019-09-12 11:11:25 -04001233 // We know skia memcopies the floats, so we can free our memory after the call returns.
1234 var m = CanvasKit.SkColorFilter._makeMatrix(fptr);
Kevin Lubickcf118922020-05-28 14:43:38 -04001235 freeArraysThatAreNotMallocedByUsers(fptr, colorMatrix);
Kevin Lubickd3729342019-09-12 11:11:25 -04001236 return m;
1237 }
1238
Kevin Lubick6bffe392020-04-02 15:24:15 -04001239 CanvasKit.SkImageFilter.MakeMatrixTransform = function(matr, filterQuality, input) {
1240 var matrPtr = copy3x3MatrixToWasm(matr);
Kevin Lubick6aa38692020-06-01 11:25:47 -04001241 return CanvasKit.SkImageFilter._MakeMatrixTransform(matrPtr, filterQuality, input);
Kevin Lubick6bffe392020-04-02 15:24:15 -04001242 }
1243
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001244 CanvasKit.SkPaint.prototype.getColor = function() {
Kevin Lubick6aa38692020-06-01 11:25:47 -04001245 this._getColor(_scratchColorPtr);
1246 return copyColorFromWasm(_scratchColorPtr);
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001247 }
1248
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001249 CanvasKit.SkPaint.prototype.setColor = function(color4f, colorSpace) {
1250 colorSpace = colorSpace || null; // null will be replaced with sRGB in the C++ method.
1251 // emscripten wouldn't bind undefined to the sk_sp<SkColorSpace> expected here.
Kevin Lubick6aa38692020-06-01 11:25:47 -04001252 var cPtr = copyColorToWasm(color4f);
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001253 this._setColor(cPtr, colorSpace);
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001254 }
1255
Kevin Lubick93f1a382020-06-02 16:15:23 -04001256 // The color components here are expected to be floating point values (nominally between
1257 // 0.0 and 1.0, but with wider color gamuts, the values could exceed this range). To convert
1258 // between standard 8 bit colors and floats, just divide by 255 before passing them in.
1259 CanvasKit.SkPaint.prototype.setColorComponents = function(r, g, b, a, colorSpace) {
1260 colorSpace = colorSpace || null; // null will be replaced with sRGB in the C++ method.
1261 // emscripten wouldn't bind undefined to the sk_sp<SkColorSpace> expected here.
1262 var cPtr = copyColorComponentsToWasm(r, g, b, a);
1263 this._setColor(cPtr, colorSpace);
1264 }
1265
Kevin Lubickcc13fd32019-04-05 13:00:01 -04001266 CanvasKit.SkSurface.prototype.captureFrameAsSkPicture = function(drawFrame) {
1267 // Set up SkPictureRecorder
1268 var spr = new CanvasKit.SkPictureRecorder();
1269 var canvas = spr.beginRecording(
1270 CanvasKit.LTRBRect(0, 0, this.width(), this.height()));
1271 drawFrame(canvas);
1272 var pic = spr.finishRecordingAsPicture();
1273 spr.delete();
1274 // TODO: do we need to clean up the memory for canvas?
1275 // If we delete it here, saveAsFile doesn't work correctly.
1276 return pic;
1277 }
1278
Kevin Lubick359a7e32019-03-19 09:34:37 -04001279 CanvasKit.SkSurface.prototype.requestAnimationFrame = function(callback, dirtyRect) {
1280 if (!this._cached_canvas) {
1281 this._cached_canvas = this.getCanvas();
1282 }
Elliot Evans1ec3b1a2020-07-23 10:25:58 -04001283 requestAnimationFrame(function() {
Kevin Lubick39026282019-03-28 12:46:40 -04001284 if (this._context !== undefined) {
1285 CanvasKit.setCurrentContext(this._context);
1286 }
Kevin Lubick359a7e32019-03-19 09:34:37 -04001287
1288 callback(this._cached_canvas);
1289
Bryce Thomas2c5b8562020-01-22 13:49:41 -08001290 // We do not dispose() of the SkSurface here, as the client will typically
1291 // call requestAnimationFrame again from within the supplied callback.
1292 // For drawing a single frame, prefer drawOnce().
Bryce Thomas9331ca02020-05-29 16:51:21 -07001293 this.flush(dirtyRect);
Kevin Lubick359a7e32019-03-19 09:34:37 -04001294 }.bind(this));
1295 }
1296
Kevin Lubick52379332020-01-27 10:01:25 -05001297 // drawOnce will dispose of the surface after drawing the frame using the provided
1298 // callback.
Bryce Thomas2c5b8562020-01-22 13:49:41 -08001299 CanvasKit.SkSurface.prototype.drawOnce = function(callback, dirtyRect) {
1300 if (!this._cached_canvas) {
1301 this._cached_canvas = this.getCanvas();
1302 }
Elliot Evans1ec3b1a2020-07-23 10:25:58 -04001303 requestAnimationFrame(function() {
Bryce Thomas2c5b8562020-01-22 13:49:41 -08001304 if (this._context !== undefined) {
1305 CanvasKit.setCurrentContext(this._context);
1306 }
1307 callback(this._cached_canvas);
1308
Bryce Thomas9331ca02020-05-29 16:51:21 -07001309 this.flush(dirtyRect);
Bryce Thomas2c5b8562020-01-22 13:49:41 -08001310 this.dispose();
1311 }.bind(this));
1312 }
1313
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001314 CanvasKit.SkPathEffect.MakeDash = function(intervals, phase) {
1315 if (!phase) {
1316 phase = 0;
1317 }
1318 if (!intervals.length || intervals.length % 2 === 1) {
1319 throw 'Intervals array must have even length';
1320 }
Kevin Lubickbe728012020-09-03 11:57:12 +00001321 var ptr = copy1dArray(intervals, 'HEAPF32');
Kevin Lubickf279c632020-03-18 09:53:55 -04001322 var dpe = CanvasKit.SkPathEffect._MakeDash(ptr, intervals.length, phase);
Kevin Lubickcf118922020-05-28 14:43:38 -04001323 freeArraysThatAreNotMallocedByUsers(ptr, intervals);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001324 return dpe;
1325 }
1326
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001327 CanvasKit.SkShader.Color = function(color4f, colorSpace) {
1328 colorSpace = colorSpace || null
Kevin Lubick6aa38692020-06-01 11:25:47 -04001329 var cPtr = copyColorToWasm(color4f);
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001330 var result = CanvasKit.SkShader._Color(cPtr, colorSpace);
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001331 return result;
1332 }
1333
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001334 CanvasKit.SkShader.MakeLinearGradient = function(start, end, colors, pos, mode, localMatrix, flags, colorSpace) {
1335 colorSpace = colorSpace || null
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001336 var cPtrInfo = copyFlexibleColorArray(colors);
Kevin Lubickbe728012020-09-03 11:57:12 +00001337 var posPtr = copy1dArray(pos, 'HEAPF32');
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001338 flags = flags || 0;
Kevin Lubick6bffe392020-04-02 15:24:15 -04001339 var localMatrixPtr = copy3x3MatrixToWasm(localMatrix);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001340
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001341 var lgs = CanvasKit._MakeLinearGradientShader(start, end, cPtrInfo.colorPtr, cPtrInfo.colorType, posPtr,
1342 cPtrInfo.count, mode, flags, localMatrixPtr, colorSpace);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001343
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001344 freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors);
Kevin Lubicke7b329e2020-06-05 15:58:01 -04001345 pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001346 return lgs;
1347 }
1348
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001349 CanvasKit.SkShader.MakeRadialGradient = function(center, radius, colors, pos, mode, localMatrix, flags, colorSpace) {
1350 colorSpace = colorSpace || null
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001351 var cPtrInfo = copyFlexibleColorArray(colors);
Kevin Lubickbe728012020-09-03 11:57:12 +00001352 var posPtr = copy1dArray(pos, 'HEAPF32');
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001353 flags = flags || 0;
Kevin Lubick6bffe392020-04-02 15:24:15 -04001354 var localMatrixPtr = copy3x3MatrixToWasm(localMatrix);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001355
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001356 var rgs = CanvasKit._MakeRadialGradientShader(center, radius, cPtrInfo.colorPtr, cPtrInfo.colorType, posPtr,
1357 cPtrInfo.count, mode, flags, localMatrixPtr, colorSpace);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001358
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001359 freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors);
Kevin Lubicke7b329e2020-06-05 15:58:01 -04001360 pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001361 return rgs;
1362 }
1363
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001364 CanvasKit.SkShader.MakeSweepGradient = function(cx, cy, colors, pos, mode, localMatrix, flags, startAngle, endAngle, colorSpace) {
1365 colorSpace = colorSpace || null
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001366 var cPtrInfo = copyFlexibleColorArray(colors);
Kevin Lubickbe728012020-09-03 11:57:12 +00001367 var posPtr = copy1dArray(pos, 'HEAPF32');
Dan Field3d44f732020-03-16 09:17:30 -07001368 flags = flags || 0;
1369 startAngle = startAngle || 0;
1370 endAngle = endAngle || 360;
Kevin Lubick6bffe392020-04-02 15:24:15 -04001371 var localMatrixPtr = copy3x3MatrixToWasm(localMatrix);
Dan Field3d44f732020-03-16 09:17:30 -07001372
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001373 var sgs = CanvasKit._MakeSweepGradientShader(cx, cy, cPtrInfo.colorPtr, cPtrInfo.colorType, posPtr,
1374 cPtrInfo.count, mode,
Kevin Lubick6bffe392020-04-02 15:24:15 -04001375 startAngle, endAngle, flags,
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001376 localMatrixPtr, colorSpace);
Dan Field3d44f732020-03-16 09:17:30 -07001377
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001378 freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors);
Kevin Lubicke7b329e2020-06-05 15:58:01 -04001379 pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos);
Dan Field3d44f732020-03-16 09:17:30 -07001380 return sgs;
1381 }
1382
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001383 CanvasKit.SkShader.MakeTwoPointConicalGradient = function(start, startRadius, end, endRadius,
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001384 colors, pos, mode, localMatrix, flags, colorSpace) {
1385 colorSpace = colorSpace || null
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001386 var cPtrInfo = copyFlexibleColorArray(colors);
Kevin Lubickbe728012020-09-03 11:57:12 +00001387 var posPtr = copy1dArray(pos, 'HEAPF32');
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001388 flags = flags || 0;
Kevin Lubick6bffe392020-04-02 15:24:15 -04001389 var localMatrixPtr = copy3x3MatrixToWasm(localMatrix);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001390
Kevin Lubick6bffe392020-04-02 15:24:15 -04001391 var rgs = CanvasKit._MakeTwoPointConicalGradientShader(
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001392 start, startRadius, end, endRadius, cPtrInfo.colorPtr, cPtrInfo.colorType,
1393 posPtr, cPtrInfo.count, mode, flags, localMatrixPtr, colorSpace);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001394
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001395 freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors);
Kevin Lubicke7b329e2020-06-05 15:58:01 -04001396 pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001397 return rgs;
1398 }
1399
1400 // temporary support for deprecated names.
Nathaniel Nifongc8f95e22020-03-09 11:52:51 -04001401 CanvasKit.MakeSkDashPathEffect = CanvasKit.SkPathEffect.MakeDash;
1402 CanvasKit.MakeLinearGradientShader = CanvasKit.SkShader.MakeLinearGradient;
1403 CanvasKit.MakeRadialGradientShader = CanvasKit.SkShader.MakeRadialGradient;
1404 CanvasKit.MakeTwoPointConicalGradientShader = CanvasKit.SkShader.MakeTwoPointConicalGradient;
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -05001405
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001406 // Run through the JS files that are added at compile time.
1407 if (CanvasKit._extraInitializations) {
1408 CanvasKit._extraInitializations.forEach(function(init) {
1409 init();
1410 });
Kevin Lubickeb2f6b02018-11-29 15:07:02 -05001411 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001412}; // end CanvasKit.onRuntimeInitialized, that is, anything changing prototypes or dynamic.
Kevin Lubickeb2f6b02018-11-29 15:07:02 -05001413
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001414// Accepts an object holding two canvaskit colors.
1415// {
1416// ambient: {r, g, b, a},
1417// spot: {r, g, b, a},
1418// }
Kevin Lubicke7b329e2020-06-05 15:58:01 -04001419// Returns the same format. Note, if malloced colors are passed in, the memory
1420// housing the passed in colors passed in will be overwritten with the computed
1421// tonal colors.
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001422CanvasKit.computeTonalColors = function(tonalColors) {
Kevin Lubicke7b329e2020-06-05 15:58:01 -04001423 // copy the colors into WASM
Kevin Lubick6aa38692020-06-01 11:25:47 -04001424 var cPtrAmbi = copyColorToWasmNoScratch(tonalColors['ambient']);
1425 var cPtrSpot = copyColorToWasmNoScratch(tonalColors['spot']);
Kevin Lubicke7b329e2020-06-05 15:58:01 -04001426 // The output of this function will be the same pointers we passed in.
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001427 this._computeTonalColors(cPtrAmbi, cPtrSpot);
Kevin Lubicke7b329e2020-06-05 15:58:01 -04001428 // Read the results out.
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001429 var result = {
1430 'ambient': copyColorFromWasm(cPtrAmbi),
1431 'spot': copyColorFromWasm(cPtrSpot),
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -04001432 };
Kevin Lubicke7b329e2020-06-05 15:58:01 -04001433 // If the user passed us malloced colors in here, we don't want to clean them up.
1434 freeArraysThatAreNotMallocedByUsers(cPtrAmbi, tonalColors['ambient']);
1435 freeArraysThatAreNotMallocedByUsers(cPtrSpot, tonalColors['spot']);
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001436 return result;
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -04001437};
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -04001438
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001439CanvasKit.LTRBRect = function(l, t, r, b) {
Michael Ludwig1f49ceb2020-09-02 21:20:44 +00001440 return {
1441 fLeft: l,
1442 fTop: t,
1443 fRight: r,
1444 fBottom: b,
1445 };
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -04001446};
Kevin Lubick1a05fce2018-11-20 12:51:16 -05001447
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001448CanvasKit.XYWHRect = function(x, y, w, h) {
Michael Ludwig1f49ceb2020-09-02 21:20:44 +00001449 return {
1450 fLeft: x,
1451 fTop: y,
1452 fRight: x+w,
1453 fBottom: y+h,
1454 };
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -04001455};
Kevin Lubick1a05fce2018-11-20 12:51:16 -05001456
Kevin Lubickbe728012020-09-03 11:57:12 +00001457// RRectXY returns a TypedArray representing an RRect with the given rect and a radiusX and
1458// radiusY for all 4 corners.
Kevin Lubick7d644e12019-09-11 14:22:22 -04001459CanvasKit.RRectXY = function(rect, rx, ry) {
Kevin Lubickbe728012020-09-03 11:57:12 +00001460 return Float32Array.of(
1461 rect['fLeft'], rect['fTop'], rect['fRight'], rect['fBottom'],
1462 rx, ry,
1463 rx, ry,
1464 rx, ry,
1465 rx, ry,
1466 );
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -04001467};
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001468
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001469// data is a TypedArray or ArrayBuffer e.g. from fetch().then(resp.arrayBuffer())
Kevin Lubick6b921b72019-09-18 16:18:17 -04001470CanvasKit.MakeAnimatedImageFromEncoded = function(data) {
1471 data = new Uint8Array(data);
1472
1473 var iptr = CanvasKit._malloc(data.byteLength);
1474 CanvasKit.HEAPU8.set(data, iptr);
1475 var img = CanvasKit._decodeAnimatedImage(iptr, data.byteLength);
1476 if (!img) {
1477 SkDebug('Could not decode animated image');
1478 return null;
1479 }
1480 return img;
1481}
1482
1483// data is a TypedArray or ArrayBuffer e.g. from fetch().then(resp.arrayBuffer())
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001484CanvasKit.MakeImageFromEncoded = function(data) {
1485 data = new Uint8Array(data);
1486
1487 var iptr = CanvasKit._malloc(data.byteLength);
1488 CanvasKit.HEAPU8.set(data, iptr);
1489 var img = CanvasKit._decodeImage(iptr, data.byteLength);
1490 if (!img) {
1491 SkDebug('Could not decode image');
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001492 return null;
1493 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001494 return img;
1495}
1496
Elliot Evans28796192020-06-15 12:53:27 -06001497// A variable to hold a canvasElement which can be reused once created the first time.
1498var memoizedCanvas2dElement = null;
1499
1500// Alternative to CanvasKit.MakeImageFromEncoded. Allows for CanvasKit users to take advantage of
1501// browser APIs to decode images instead of using codecs included in the CanvasKit wasm binary.
1502// Expects that the canvasImageSource has already loaded/decoded.
1503// CanvasImageSource reference: https://developer.mozilla.org/en-US/docs/Web/API/CanvasImageSource
1504CanvasKit.MakeImageFromCanvasImageSource = function(canvasImageSource) {
1505 var width = canvasImageSource.width;
1506 var height = canvasImageSource.height;
1507
1508 if (!memoizedCanvas2dElement) {
1509 memoizedCanvas2dElement = document.createElement('canvas');
1510 }
1511 memoizedCanvas2dElement.width = width;
1512 memoizedCanvas2dElement.height = height;
1513
1514 var ctx2d = memoizedCanvas2dElement.getContext('2d');
1515 ctx2d.drawImage(canvasImageSource, 0, 0);
1516
1517 var imageData = ctx2d.getImageData(0, 0, width, height);
1518
1519 return CanvasKit.MakeImage(
1520 imageData.data,
1521 width,
1522 height,
1523 CanvasKit.AlphaType.Unpremul,
1524 CanvasKit.ColorType.RGBA_8888,
1525 CanvasKit.SkColorSpace.SRGB
1526 );
1527}
1528
1529// pixels may be any Typed Array, but Uint8Array or Uint8ClampedArray is recommended,
1530// with bytes representing the pixel values.
Kevin Lubickeda0b432019-12-02 08:26:48 -05001531// (e.g. each set of 4 bytes could represent RGBA values for a single pixel).
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001532CanvasKit.MakeImage = function(pixels, width, height, alphaType, colorType, colorSpace) {
Kevin Lubickeda0b432019-12-02 08:26:48 -05001533 var bytesPerPixel = pixels.length / (width * height);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001534 var info = {
1535 'width': width,
1536 'height': height,
1537 'alphaType': alphaType,
1538 'colorType': colorType,
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -04001539 'colorSpace': colorSpace,
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001540 };
Kevin Lubickbe728012020-09-03 11:57:12 +00001541 var pptr = copy1dArray(pixels, 'HEAPU8');
Kevin Lubickeda0b432019-12-02 08:26:48 -05001542 // No need to _free pptr, Image takes it with SkData::MakeFromMalloc
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001543
Kevin Lubickeda0b432019-12-02 08:26:48 -05001544 return CanvasKit._MakeImage(info, pptr, pixels.length, width * bytesPerPixel);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001545}
1546
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001547// Colors may be a Uint32Array of int colors, a Flat Float32Array of float colors
1548// or a 2d Array of Float32Array(4) (deprecated)
1549// the underlying skia function accepts only int colors so it is recommended
1550// to pass an array of int colors to avoid an extra conversion.
1551// SkColorBuilder is not accepted.
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001552CanvasKit.MakeSkVertices = function(mode, positions, textureCoordinates, colors,
Mike Reed5caf9352020-03-02 14:57:09 -05001553 indices, isVolatile) {
Kevin Lubickb3574c92019-03-06 08:25:36 -05001554 // Default isVolitile to true if not set
1555 isVolatile = isVolatile === undefined ? true : isVolatile;
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001556 var idxCount = (indices && indices.length) || 0;
1557
1558 var flags = 0;
1559 // These flags are from SkVertices.h and should be kept in sync with those.
1560 if (textureCoordinates && textureCoordinates.length) {
1561 flags |= (1 << 0);
1562 }
1563 if (colors && colors.length) {
1564 flags |= (1 << 1);
1565 }
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001566 if (!isVolatile) {
Mike Reed5caf9352020-03-02 14:57:09 -05001567 flags |= (1 << 2);
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001568 }
1569
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001570 var builder = new CanvasKit._SkVerticesBuilder(mode, positions.length, idxCount, flags);
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001571
Kevin Lubickbe728012020-09-03 11:57:12 +00001572 copy2dArray(positions, 'HEAPF32', builder.positions());
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001573 if (builder.texCoords()) {
Kevin Lubickbe728012020-09-03 11:57:12 +00001574 copy2dArray(textureCoordinates, 'HEAPF32', builder.texCoords());
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001575 }
1576 if (builder.colors()) {
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001577 if (colors.build) {
1578 throw('Color builder not accepted by MakeSkVertices, use array of ints');
1579 } else {
Kevin Lubickbe728012020-09-03 11:57:12 +00001580 copy1dArray(assureIntColors(colors), 'HEAPU32', builder.colors());
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001581 }
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001582 }
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001583 if (builder.indices()) {
Kevin Lubickbe728012020-09-03 11:57:12 +00001584 copy1dArray(indices, 'HEAPU16', builder.indices());
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001585 }
Kevin Lubickb3574c92019-03-06 08:25:36 -05001586
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001587 // Create the vertices, which owns the memory that the builder had allocated.
1588 return builder.detach();
Kevin Lubicka4f218d2020-01-14 08:39:09 -05001589};