blob: f261e0eea6301e27112e43f6554f1b64bd7f7b2c [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.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04006// Anything that modifies an exposed class (e.g. Path) should be set
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05007// 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
Kevin Lubicked962642021-02-02 08:18:11 -050026 _scratchFourFloatsA = CanvasKit.Malloc(Float32Array, 4);
27 _scratchFourFloatsAPtr = _scratchFourFloatsA['byteOffset'];
Kevin Lubickf8823b52020-09-03 10:02:10 -040028
Kevin Lubicked962642021-02-02 08:18:11 -050029 _scratchFourFloatsB = CanvasKit.Malloc(Float32Array, 4);
30 _scratchFourFloatsBPtr = _scratchFourFloatsB['byteOffset'];
Kevin Lubickf8823b52020-09-03 10:02:10 -040031
Kevin Lubickf6040ef2021-02-02 08:26:48 -050032 _scratchThreeFloatsA = CanvasKit.Malloc(Float32Array, 3); // 3 floats to represent SkVector3
33 _scratchThreeFloatsAPtr = _scratchThreeFloatsA['byteOffset'];
34
35 _scratchThreeFloatsB = CanvasKit.Malloc(Float32Array, 3); // 3 floats to represent SkVector3
36 _scratchThreeFloatsBPtr = _scratchThreeFloatsB['byteOffset'];
37
Kevin Lubickf8823b52020-09-03 10:02:10 -040038 _scratchIRect = CanvasKit.Malloc(Int32Array, 4);
39 _scratchIRectPtr = _scratchIRect['byteOffset'];
40
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -040041 // Create single copies of all three supported color spaces
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040042 // These are sk_sp<ColorSpace>
43 CanvasKit.ColorSpace.SRGB = CanvasKit.ColorSpace._MakeSRGB();
44 CanvasKit.ColorSpace.DISPLAY_P3 = CanvasKit.ColorSpace._MakeDisplayP3();
45 CanvasKit.ColorSpace.ADOBE_RGB = CanvasKit.ColorSpace._MakeAdobeRGB();
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -040046
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040047 CanvasKit.Path.MakeFromCmds = function(cmds) {
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -040048 var ptrLen = loadCmdsTypedArray(cmds);
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040049 var path = CanvasKit.Path._MakeFromCmds(ptrLen[0], ptrLen[1]);
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -040050 CanvasKit._free(ptrLen[0]);
51 return path;
52 };
53
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -040054 // The weights array is optional (only used for conics).
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040055 CanvasKit.Path.MakeFromVerbsPointsWeights = function(verbs, pts, weights) {
Kevin Lubickbe728012020-09-03 11:57:12 +000056 var verbsPtr = copy1dArray(verbs, 'HEAPU8');
57 var pointsPtr = copy1dArray(pts, 'HEAPF32');
58 var weightsPtr = copy1dArray(weights, 'HEAPF32');
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -040059 var numWeights = (weights && weights.length) || 0;
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040060 var path = CanvasKit.Path._MakeFromVerbsPointsWeights(
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -040061 verbsPtr, verbs.length, pointsPtr, pts.length, weightsPtr, numWeights);
62 freeArraysThatAreNotMallocedByUsers(verbsPtr, verbs);
63 freeArraysThatAreNotMallocedByUsers(pointsPtr, pts);
64 freeArraysThatAreNotMallocedByUsers(weightsPtr, weights);
65 return path;
66 };
Kevin Lubickd3729342019-09-12 11:11:25 -040067
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040068 CanvasKit.Path.prototype.addArc = function(oval, startAngle, sweepAngle) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050069 // see arc() for the HTMLCanvas version
70 // note input angles are degrees.
Kevin Lubickf8823b52020-09-03 10:02:10 -040071 var oPtr = copyRectToWasm(oval);
72 this._addArc(oPtr, startAngle, sweepAngle);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050073 return this;
74 };
Kevin Lubick217056c2018-09-20 17:39:31 -040075
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040076 CanvasKit.Path.prototype.addOval = function(oval, isCCW, startIndex) {
Kevin Lubicke384df42019-08-26 15:48:09 -040077 if (startIndex === undefined) {
78 startIndex = 1;
79 }
Kevin Lubickf8823b52020-09-03 10:02:10 -040080 var oPtr = copyRectToWasm(oval);
81 this._addOval(oPtr, !!isCCW, startIndex);
Kevin Lubicke384df42019-08-26 15:48:09 -040082 return this;
83 };
84
Kevin Lubicka2535af2020-10-02 08:01:57 -040085 // TODO(kjlubick) clean up this API - split it apart if necessary
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040086 CanvasKit.Path.prototype.addPath = function() {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050087 // Takes 1, 2, 7, or 10 required args, where the first arg is always the path.
88 // The last arg is optional and chooses between add or extend mode.
89 // The options for the remaining args are:
90 // - an array of 6 or 9 parameters (perspective is optional)
91 // - the 9 parameters of a full matrix or
92 // the 6 non-perspective params of a matrix.
93 var args = Array.prototype.slice.call(arguments);
94 var path = args[0];
95 var extend = false;
Kevin Lubickbe728012020-09-03 11:57:12 +000096 if (typeof args[args.length-1] === 'boolean') {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050097 extend = args.pop();
98 }
99 if (args.length === 1) {
100 // Add path, unchanged. Use identity matrix
101 this._addPath(path, 1, 0, 0,
102 0, 1, 0,
103 0, 0, 1,
104 extend);
105 } else if (args.length === 2) {
106 // User provided the 9 params of a full matrix as an array.
107 var a = args[1];
108 this._addPath(path, a[0], a[1], a[2],
109 a[3], a[4], a[5],
110 a[6] || 0, a[7] || 0, a[8] || 1,
111 extend);
112 } else if (args.length === 7 || args.length === 10) {
113 // User provided the 9 params of a (full) matrix directly.
114 // (or just the 6 non perspective ones)
115 // These are in the same order as what Skia expects.
116 var a = args;
117 this._addPath(path, a[1], a[2], a[3],
118 a[4], a[5], a[6],
119 a[7] || 0, a[8] || 0, a[9] || 1,
120 extend);
121 } else {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400122 Debug('addPath expected to take 1, 2, 7, or 10 required args. Got ' + args.length);
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400123 return null;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500124 }
125 return this;
126 };
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400127
Kevin Lubicke7c1a732020-12-04 09:10:39 -0500128 // points is a 1d array of length 2n representing n points where the even indices
129 // will be treated as x coordinates and the odd indices will be treated as y coordinates.
130 // Like other APIs, this accepts a malloced type array or malloc obj.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400131 CanvasKit.Path.prototype.addPoly = function(points, close) {
Kevin Lubicke7c1a732020-12-04 09:10:39 -0500132 var ptr = copy1dArray(points, 'HEAPF32');
133 this._addPoly(ptr, points.length / 2, close);
Kevin Lubickcf118922020-05-28 14:43:38 -0400134 freeArraysThatAreNotMallocedByUsers(ptr, points);
Kevin Lubick37ab53e2019-11-11 10:06:08 -0500135 return this;
136 };
137
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400138 CanvasKit.Path.prototype.addRect = function(rect, isCCW) {
Kevin Lubickf8823b52020-09-03 10:02:10 -0400139 var rPtr = copyRectToWasm(rect);
140 this._addRect(rPtr, !!isCCW);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500141 return this;
142 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400143
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400144 CanvasKit.Path.prototype.addRRect = function(rrect, isCCW) {
Kevin Lubickf8823b52020-09-03 10:02:10 -0400145 var rPtr = copyRRectToWasm(rrect);
146 this._addRRect(rPtr, !!isCCW);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500147 return this;
148 };
149
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -0400150 // The weights array is optional (only used for conics).
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400151 CanvasKit.Path.prototype.addVerbsPointsWeights = function(verbs, points, weights) {
Kevin Lubickbe728012020-09-03 11:57:12 +0000152 var verbsPtr = copy1dArray(verbs, 'HEAPU8');
153 var pointsPtr = copy1dArray(points, 'HEAPF32');
154 var weightsPtr = copy1dArray(weights, 'HEAPF32');
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -0400155 var numWeights = (weights && weights.length) || 0;
156 this._addVerbsPointsWeights(verbsPtr, verbs.length, pointsPtr, points.length,
157 weightsPtr, numWeights);
158 freeArraysThatAreNotMallocedByUsers(verbsPtr, verbs);
159 freeArraysThatAreNotMallocedByUsers(pointsPtr, points);
160 freeArraysThatAreNotMallocedByUsers(weightsPtr, weights);
161 };
162
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400163 CanvasKit.Path.prototype.arc = function(x, y, radius, startAngle, endAngle, ccw) {
164 // emulates the HTMLCanvas behavior. See addArc() for the Path version.
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500165 // Note input angles are radians.
166 var bounds = CanvasKit.LTRBRect(x-radius, y-radius, x+radius, y+radius);
167 var sweep = radiansToDegrees(endAngle - startAngle) - (360 * !!ccw);
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400168 var temp = new CanvasKit.Path();
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500169 temp.addArc(bounds, radiansToDegrees(startAngle), sweep);
170 this.addPath(temp, true);
171 temp.delete();
172 return this;
173 };
174
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400175 // Appends arc to Path. Arc added is part of ellipse
Nathaniel Nifongd0c9d0c2020-07-15 16:46:17 -0400176 // bounded by oval, from startAngle through sweepAngle. Both startAngle and
177 // sweepAngle are measured in degrees, where zero degrees is aligned with the
178 // positive x-axis, and positive sweeps extends arc clockwise.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400179 CanvasKit.Path.prototype.arcToOval = function(oval, startAngle, sweepAngle, forceMoveTo) {
Kevin Lubickf8823b52020-09-03 10:02:10 -0400180 var oPtr = copyRectToWasm(oval);
181 this._arcToOval(oPtr, startAngle, sweepAngle, forceMoveTo);
Nathaniel Nifongd0c9d0c2020-07-15 16:46:17 -0400182 return this;
183 };
184
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400185 // Appends arc to Path. Arc is implemented by one or more conics weighted to
Nathaniel Nifongd0c9d0c2020-07-15 16:46:17 -0400186 // describe part of oval with radii (rx, ry) rotated by xAxisRotate degrees. Arc
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400187 // curves from last point to (x, y), choosing one of four possible routes:
Nathaniel Nifongd0c9d0c2020-07-15 16:46:17 -0400188 // clockwise or counterclockwise, and smaller or larger.
189
190 // Arc sweep is always less than 360 degrees. arcTo() appends line to (x, y) if
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400191 // either radii are zero, or if last point equals (x, y). arcTo() scales radii
192 // (rx, ry) to fit last point and (x, y) if both are greater than zero but
Nathaniel Nifongd0c9d0c2020-07-15 16:46:17 -0400193 // too small.
194
195 // arcToRotated() appends up to four conic curves.
196 // arcToRotated() implements the functionality of SVG arc, although SVG sweep-flag value
197 // is opposite the integer value of sweep; SVG sweep-flag uses 1 for clockwise,
198 // while kCW_Direction cast to int is zero.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400199 CanvasKit.Path.prototype.arcToRotated = function(rx, ry, xAxisRotate, useSmallArc, isCCW, x, y) {
Nathaniel Nifongd0c9d0c2020-07-15 16:46:17 -0400200 this._arcToRotated(rx, ry, xAxisRotate, !!useSmallArc, !!isCCW, x, y);
201 return this;
202 };
203
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400204 // Appends arc to Path, after appending line if needed. Arc is implemented by conic
Nathaniel Nifongd0c9d0c2020-07-15 16:46:17 -0400205 // weighted to describe part of circle. Arc is contained by tangent from
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400206 // last Path point to (x1, y1), and tangent from (x1, y1) to (x2, y2). Arc
Nathaniel Nifongd0c9d0c2020-07-15 16:46:17 -0400207 // is part of circle sized to radius, positioned so it touches both tangent lines.
208
209 // If last Path Point does not start Arc, arcTo appends connecting Line to Path.
210 // The length of Vector from (x1, y1) to (x2, y2) does not affect Arc.
211
212 // Arc sweep is always less than 180 degrees. If radius is zero, or if
213 // tangents are nearly parallel, arcTo appends Line from last Path Point to (x1, y1).
214
215 // arcToTangent appends at most one Line and one conic.
216 // arcToTangent implements the functionality of PostScript arct and HTML Canvas arcTo.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400217 CanvasKit.Path.prototype.arcToTangent = function(x1, y1, x2, y2, radius) {
Nathaniel Nifongd0c9d0c2020-07-15 16:46:17 -0400218 this._arcToTangent(x1, y1, x2, y2, radius);
219 return this;
220 };
221
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400222 CanvasKit.Path.prototype.close = function() {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500223 this._close();
224 return this;
225 };
226
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400227 CanvasKit.Path.prototype.conicTo = function(x1, y1, x2, y2, w) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500228 this._conicTo(x1, y1, x2, y2, w);
229 return this;
230 };
231
Kevin Lubick7d96c5c2020-10-01 10:55:16 -0400232 // Clients can pass in a Float32Array with length 4 to this and the results
233 // will be copied into that array. Otherwise, a new TypedArray will be allocated
234 // and returned.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400235 CanvasKit.Path.prototype.computeTightBounds = function(optionalOutputArray) {
Kevin Lubicked962642021-02-02 08:18:11 -0500236 this._computeTightBounds(_scratchFourFloatsAPtr);
237 var ta = _scratchFourFloatsA['toTypedArray']();
Kevin Lubick7d96c5c2020-10-01 10:55:16 -0400238 if (optionalOutputArray) {
239 optionalOutputArray.set(ta);
240 return optionalOutputArray;
241 }
242 return ta.slice();
243 };
244
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400245 CanvasKit.Path.prototype.cubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500246 this._cubicTo(cp1x, cp1y, cp2x, cp2y, x, y);
247 return this;
248 };
249
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400250 CanvasKit.Path.prototype.dash = function(on, off, phase) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500251 if (this._dash(on, off, phase)) {
Kevin Lubick217056c2018-09-20 17:39:31 -0400252 return this;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500253 }
254 return null;
255 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400256
Kevin Lubickf8823b52020-09-03 10:02:10 -0400257 // Clients can pass in a Float32Array with length 4 to this and the results
258 // will be copied into that array. Otherwise, a new TypedArray will be allocated
259 // and returned.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400260 CanvasKit.Path.prototype.getBounds = function(optionalOutputArray) {
Kevin Lubicked962642021-02-02 08:18:11 -0500261 this._getBounds(_scratchFourFloatsAPtr);
262 var ta = _scratchFourFloatsA['toTypedArray']();
Kevin Lubickf8823b52020-09-03 10:02:10 -0400263 if (optionalOutputArray) {
264 optionalOutputArray.set(ta);
265 return optionalOutputArray;
266 }
267 return ta.slice();
268 };
269
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400270 CanvasKit.Path.prototype.lineTo = function(x, y) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500271 this._lineTo(x, y);
272 return this;
273 };
Kevin Lubick217056c2018-09-20 17:39:31 -0400274
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400275 CanvasKit.Path.prototype.moveTo = function(x, y) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500276 this._moveTo(x, y);
277 return this;
278 };
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400279
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400280 CanvasKit.Path.prototype.offset = function(dx, dy) {
Kevin Lubicke384df42019-08-26 15:48:09 -0400281 this._transform(1, 0, dx,
282 0, 1, dy,
283 0, 0, 1);
284 return this;
285 };
286
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400287 CanvasKit.Path.prototype.quadTo = function(cpx, cpy, x, y) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500288 this._quadTo(cpx, cpy, x, y);
289 return this;
290 };
291
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400292 CanvasKit.Path.prototype.rArcTo = function(rx, ry, xAxisRotate, useSmallArc, isCCW, dx, dy) {
Kevin Lubick79b71342019-11-01 14:36:52 -0400293 this._rArcTo(rx, ry, xAxisRotate, useSmallArc, isCCW, dx, dy);
294 return this;
295 };
296
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400297 CanvasKit.Path.prototype.rConicTo = function(dx1, dy1, dx2, dy2, w) {
Kevin Lubick79b71342019-11-01 14:36:52 -0400298 this._rConicTo(dx1, dy1, dx2, dy2, w);
299 return this;
300 };
301
302 // These params are all relative
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400303 CanvasKit.Path.prototype.rCubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) {
Kevin Lubick79b71342019-11-01 14:36:52 -0400304 this._rCubicTo(cp1x, cp1y, cp2x, cp2y, x, y);
305 return this;
306 };
307
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400308 CanvasKit.Path.prototype.rLineTo = function(dx, dy) {
Kevin Lubick79b71342019-11-01 14:36:52 -0400309 this._rLineTo(dx, dy);
310 return this;
311 };
312
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400313 CanvasKit.Path.prototype.rMoveTo = function(dx, dy) {
Kevin Lubick79b71342019-11-01 14:36:52 -0400314 this._rMoveTo(dx, dy);
315 return this;
316 };
317
318 // These params are all relative
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400319 CanvasKit.Path.prototype.rQuadTo = function(cpx, cpy, x, y) {
Kevin Lubick79b71342019-11-01 14:36:52 -0400320 this._rQuadTo(cpx, cpy, x, y);
321 return this;
322 };
323
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400324 CanvasKit.Path.prototype.stroke = function(opts) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500325 // Fill out any missing values with the default values.
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500326 opts = opts || {};
Kevin Lubick6aa38692020-06-01 11:25:47 -0400327 opts['width'] = opts['width'] || 1;
328 opts['miter_limit'] = opts['miter_limit'] || 4;
329 opts['cap'] = opts['cap'] || CanvasKit.StrokeCap.Butt;
330 opts['join'] = opts['join'] || CanvasKit.StrokeJoin.Miter;
331 opts['precision'] = opts['precision'] || 1;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500332 if (this._stroke(opts)) {
333 return this;
334 }
335 return null;
336 };
337
Kevin Lubicka2535af2020-10-02 08:01:57 -0400338 // TODO(kjlubick) Change this to take a 3x3 or 4x4 matrix (optionally malloc'd)
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400339 CanvasKit.Path.prototype.transform = function() {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500340 // Takes 1 or 9 args
341 if (arguments.length === 1) {
342 // argument 1 should be a 6 or 9 element array.
343 var a = arguments[0];
344 this._transform(a[0], a[1], a[2],
345 a[3], a[4], a[5],
346 a[6] || 0, a[7] || 0, a[8] || 1);
347 } else if (arguments.length === 6 || arguments.length === 9) {
348 // these arguments are the 6 or 9 members of the matrix
349 var a = arguments;
350 this._transform(a[0], a[1], a[2],
351 a[3], a[4], a[5],
352 a[6] || 0, a[7] || 0, a[8] || 1);
353 } else {
354 throw 'transform expected to take 1 or 9 arguments. Got ' + arguments.length;
355 }
356 return this;
357 };
358 // isComplement is optional, defaults to false
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400359 CanvasKit.Path.prototype.trim = function(startT, stopT, isComplement) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500360 if (this._trim(startT, stopT, !!isComplement)) {
361 return this;
362 }
363 return null;
364 };
365
Kevin Lubick652d7902020-12-11 14:51:36 -0500366 // makeShaderCubic returns a shader for a given image, allowing it to be used on
367 // a paint as well as other purposes. This shader will be higher quality than
368 // other shader functions. See CubicResampler in SkSamplingOptions.h for more information
369 // on the cubicResampler params.
370 CanvasKit.Image.prototype.makeShaderCubic = function(xTileMode, yTileMode,
371 cubicResamplerB, cubicResamplerC,
372 localMatrix) {
Kevin Lubick6bffe392020-04-02 15:24:15 -0400373 var localMatrixPtr = copy3x3MatrixToWasm(localMatrix);
Kevin Lubick652d7902020-12-11 14:51:36 -0500374 return this._makeShaderCubic(xTileMode, yTileMode, cubicResamplerB,
375 cubicResamplerC, localMatrixPtr);
376 };
377
378 // makeShaderCubic returns a shader for a given image, allowing it to be used on
379 // a paint as well as other purposes. This shader will draw more quickly than
380 // other shader functions, but at a lower quality.
381 CanvasKit.Image.prototype.makeShaderOptions = function(xTileMode, yTileMode,
382 filterMode, mipmapMode,
383 localMatrix) {
384 var localMatrixPtr = copy3x3MatrixToWasm(localMatrix);
385 return this._makeShaderOptions(xTileMode, yTileMode, filterMode, mipmapMode, localMatrixPtr);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500386 };
Kevin Lubicka064c282019-04-04 09:28:53 -0400387
Kevin Lubickb8123cc2020-11-06 13:05:37 -0500388 function readPixels(source, srcX, srcY, imageInfo, destMallocObj, bytesPerRow) {
389 if (!bytesPerRow) {
390 bytesPerRow = 4 * imageInfo['width'];
391 if (imageInfo['colorType'] === CanvasKit.ColorType.RGBA_F16) {
392 bytesPerRow *= 2;
393 }
394 else if (imageInfo['colorType'] === CanvasKit.ColorType.RGBA_F32) {
395 bytesPerRow *= 4;
396 }
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400397 }
Kevin Lubickb8123cc2020-11-06 13:05:37 -0500398 var pBytes = bytesPerRow * imageInfo.height;
Kevin Lubickc4ab0872020-11-03 17:13:09 -0500399 var pPtr;
400 if (destMallocObj) {
401 pPtr = destMallocObj['byteOffset'];
402 } else {
403 pPtr = CanvasKit._malloc(pBytes);
404 }
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400405
Kevin Lubickb8123cc2020-11-06 13:05:37 -0500406 if (!source._readPixels(imageInfo, pPtr, bytesPerRow, srcX, srcY)) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400407 Debug('Could not read pixels with the given inputs');
Kevin Lubickc4ab0872020-11-03 17:13:09 -0500408 if (!destMallocObj) {
409 CanvasKit._free(pPtr);
410 }
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400411 return null;
412 }
413
Kevin Lubickc4ab0872020-11-03 17:13:09 -0500414 // If the user provided us a buffer to copy into, we don't need to allocate a new TypedArray.
415 if (destMallocObj) {
416 return destMallocObj['toTypedArray'](); // Return the typed array wrapper w/o allocating.
417 }
418
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400419 // Put those pixels into a typed array of the right format and then
420 // make a copy with slice() that we can return.
421 var retVal = null;
Kevin Lubickbe728012020-09-03 11:57:12 +0000422 switch (imageInfo['colorType']) {
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400423 case CanvasKit.ColorType.RGBA_8888:
Kevin Lubickb8123cc2020-11-06 13:05:37 -0500424 case CanvasKit.ColorType.RGBA_F16: // there is no half-float JS type, so we return raw bytes.
Bryce Thomas1fa54042020-01-14 13:46:30 -0800425 retVal = new Uint8Array(CanvasKit.HEAPU8.buffer, pPtr, pBytes).slice();
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400426 break;
427 case CanvasKit.ColorType.RGBA_F32:
Bryce Thomas1fa54042020-01-14 13:46:30 -0800428 retVal = new Float32Array(CanvasKit.HEAPU8.buffer, pPtr, pBytes).slice();
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400429 break;
Kevin Lubickb8123cc2020-11-06 13:05:37 -0500430 default:
431 Debug('ColorType not yet supported');
432 return null;
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400433 }
434
435 // Free the allocated pixels in the WASM memory
436 CanvasKit._free(pPtr);
437 return retVal;
Kevin Lubickb8123cc2020-11-06 13:05:37 -0500438 }
439
440 CanvasKit.Image.prototype.readPixels = function(srcX, srcY, imageInfo, destMallocObj,
441 bytesPerRow) {
442 return readPixels(this, srcX, srcY, imageInfo, destMallocObj, bytesPerRow);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500443 };
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400444
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400445 // Accepts an array of four numbers in the range of 0-1 representing a 4f color
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400446 CanvasKit.Canvas.prototype.clear = function(color4f) {
Kevin Lubick6aa38692020-06-01 11:25:47 -0400447 var cPtr = copyColorToWasm(color4f);
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400448 this._clear(cPtr);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500449 };
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400450
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400451 CanvasKit.Canvas.prototype.clipRRect = function(rrect, op, antialias) {
Kevin Lubickbe728012020-09-03 11:57:12 +0000452 var rPtr = copyRRectToWasm(rrect);
453 this._clipRRect(rPtr, op, antialias);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500454 };
Kevin Lubickbe728012020-09-03 11:57:12 +0000455
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400456 CanvasKit.Canvas.prototype.clipRect = function(rect, op, antialias) {
Kevin Lubickf8823b52020-09-03 10:02:10 -0400457 var rPtr = copyRectToWasm(rect);
458 this._clipRect(rPtr, op, antialias);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500459 };
Kevin Lubickf8823b52020-09-03 10:02:10 -0400460
Kevin Lubickc1d08982020-04-06 13:52:15 -0400461 // concat takes a 3x2, a 3x3, or a 4x4 matrix and upscales it (if needed) to 4x4. This is because
462 // under the hood, SkCanvas uses a 4x4 matrix.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400463 CanvasKit.Canvas.prototype.concat = function(matr) {
Kevin Lubickc1d08982020-04-06 13:52:15 -0400464 var matrPtr = copy4x4MatrixToWasm(matr);
Kevin Lubick6bffe392020-04-02 15:24:15 -0400465 this._concat(matrPtr);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500466 };
Kevin Lubickd6b32ed2019-05-06 13:04:03 -0400467
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400468 CanvasKit.Canvas.prototype.drawArc = function(oval, startAngle, sweepAngle, useCenter, paint) {
Kevin Lubickf8823b52020-09-03 10:02:10 -0400469 var oPtr = copyRectToWasm(oval);
470 this._drawArc(oPtr, startAngle, sweepAngle, useCenter, paint);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500471 };
Kevin Lubickf8823b52020-09-03 10:02:10 -0400472
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400473 // atlas is an Image, e.g. from CanvasKit.MakeImageFromEncoded
474 // srcRects, dstXforms, and colors should be CanvasKit.RectBuilder, CanvasKit.RSXFormBuilder,
475 // and CanvasKit.ColorBuilder (fastest)
Nathaniel Nifongcd75b172020-06-05 11:17:43 -0400476 // Or they can be an array of floats of length 4*number of destinations.
477 // colors are optional and used to tint the drawn images using the optional blend mode
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400478 // Colors may be an ColorBuilder, a Uint32Array of int colors,
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -0400479 // a Flat Float32Array of float colors or a 2d Array of Float32Array(4) (deprecated)
Kevin Lubick97440de2020-09-29 17:58:21 -0400480 // TODO(kjlubick) remove Builders - no longer needed now that Malloc is a thing.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400481 CanvasKit.Canvas.prototype.drawAtlas = function(atlas, srcRects, dstXforms, paint,
Kevin Lubickee91c072019-03-29 10:39:52 -0400482 /*optional*/ blendMode, colors) {
483 if (!atlas || !paint || !srcRects || !dstXforms) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400484 Debug('Doing nothing since missing a required input');
Kevin Lubickee91c072019-03-29 10:39:52 -0400485 return;
486 }
Nathaniel Nifongcd75b172020-06-05 11:17:43 -0400487
488 // builder arguments report the length as the number of rects, but when passed as arrays
489 // their.length attribute is 4x higher because it's the number of total components of all rects.
490 // colors is always going to report the same length, at least until floats colors are supported
491 // by this function.
492 if (srcRects.length !== dstXforms.length) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400493 Debug('Doing nothing since input arrays length mismatches');
Nathaniel Nifonge5d32542020-03-26 09:27:48 -0400494 return;
Kevin Lubickee91c072019-03-29 10:39:52 -0400495 }
496 if (!blendMode) {
497 blendMode = CanvasKit.BlendMode.SrcOver;
498 }
499
500 var srcRectPtr;
501 if (srcRects.build) {
502 srcRectPtr = srcRects.build();
503 } else {
Kevin Lubickbe728012020-09-03 11:57:12 +0000504 srcRectPtr = copy1dArray(srcRects, 'HEAPF32');
Kevin Lubickee91c072019-03-29 10:39:52 -0400505 }
506
Nathaniel Nifongcd75b172020-06-05 11:17:43 -0400507 var count = 1;
Kevin Lubickee91c072019-03-29 10:39:52 -0400508 var dstXformPtr;
509 if (dstXforms.build) {
510 dstXformPtr = dstXforms.build();
Nathaniel Nifongcd75b172020-06-05 11:17:43 -0400511 count = dstXforms.length;
Kevin Lubickee91c072019-03-29 10:39:52 -0400512 } else {
Kevin Lubickbe728012020-09-03 11:57:12 +0000513 dstXformPtr = copy1dArray(dstXforms, 'HEAPF32');
Nathaniel Nifongcd75b172020-06-05 11:17:43 -0400514 count = dstXforms.length / 4;
Kevin Lubickee91c072019-03-29 10:39:52 -0400515 }
516
Kevin Lubick6bffe392020-04-02 15:24:15 -0400517 var colorPtr = nullptr;
Kevin Lubickee91c072019-03-29 10:39:52 -0400518 if (colors) {
519 if (colors.build) {
520 colorPtr = colors.build();
521 } else {
Kevin Lubickbe728012020-09-03 11:57:12 +0000522 colorPtr = copy1dArray(assureIntColors(colors), 'HEAPU32');
Kevin Lubickee91c072019-03-29 10:39:52 -0400523 }
524 }
525
Nathaniel Nifongcd75b172020-06-05 11:17:43 -0400526 this._drawAtlas(atlas, dstXformPtr, srcRectPtr, colorPtr, count, blendMode, paint);
Kevin Lubickee91c072019-03-29 10:39:52 -0400527
528 if (srcRectPtr && !srcRects.build) {
Kevin Lubickcf118922020-05-28 14:43:38 -0400529 freeArraysThatAreNotMallocedByUsers(srcRectPtr, srcRects);
Kevin Lubickee91c072019-03-29 10:39:52 -0400530 }
531 if (dstXformPtr && !dstXforms.build) {
Kevin Lubickcf118922020-05-28 14:43:38 -0400532 freeArraysThatAreNotMallocedByUsers(dstXformPtr, dstXforms);
Kevin Lubickee91c072019-03-29 10:39:52 -0400533 }
534 if (colorPtr && !colors.build) {
Kevin Lubickcf118922020-05-28 14:43:38 -0400535 freeArraysThatAreNotMallocedByUsers(colorPtr, colors);
Kevin Lubickee91c072019-03-29 10:39:52 -0400536 }
Kevin Lubick9fe83912020-11-03 17:08:34 -0500537 };
Kevin Lubickee91c072019-03-29 10:39:52 -0400538
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400539 CanvasKit.Canvas.prototype.drawColor = function (color4f, mode) {
Kevin Lubick6aa38692020-06-01 11:25:47 -0400540 var cPtr = copyColorToWasm(color4f);
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400541 if (mode !== undefined) {
542 this._drawColor(cPtr, mode);
543 } else {
544 this._drawColor(cPtr);
545 }
Kevin Lubick9fe83912020-11-03 17:08:34 -0500546 };
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400547
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400548 CanvasKit.Canvas.prototype.drawColorComponents = function (r, g, b, a, mode) {
Kevin Lubick93f1a382020-06-02 16:15:23 -0400549 var cPtr = copyColorComponentsToWasm(r, g, b, a);
550 if (mode !== undefined) {
551 this._drawColor(cPtr, mode);
552 } else {
553 this._drawColor(cPtr);
554 }
Kevin Lubick9fe83912020-11-03 17:08:34 -0500555 };
Kevin Lubick93f1a382020-06-02 16:15:23 -0400556
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400557 CanvasKit.Canvas.prototype.drawDRRect = function(outer, inner, paint) {
Kevin Lubickbe728012020-09-03 11:57:12 +0000558 var oPtr = copyRRectToWasm(outer, _scratchRRectPtr);
559 var iPtr = copyRRectToWasm(inner, _scratchRRect2Ptr);
560 this._drawDRRect(oPtr, iPtr, paint);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500561 };
Kevin Lubickbe728012020-09-03 11:57:12 +0000562
Kevin Lubick2d633492020-12-17 09:58:32 -0500563 CanvasKit.Canvas.prototype.drawImageNine = function(img, center, dest, filter, paint) {
Kevin Lubickf8823b52020-09-03 10:02:10 -0400564 var cPtr = copyIRectToWasm(center);
565 var dPtr = copyRectToWasm(dest);
Kevin Lubick2d633492020-12-17 09:58:32 -0500566 this._drawImageNine(img, cPtr, dPtr, filter, paint || null);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500567 };
Kevin Lubickf8823b52020-09-03 10:02:10 -0400568
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400569 CanvasKit.Canvas.prototype.drawImageRect = function(img, src, dest, paint, fastSample) {
Kevin Lubicked962642021-02-02 08:18:11 -0500570 copyRectToWasm(src, _scratchFourFloatsAPtr);
571 copyRectToWasm(dest, _scratchFourFloatsBPtr);
572 this._drawImageRect(img, _scratchFourFloatsAPtr, _scratchFourFloatsBPtr, paint, !!fastSample);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500573 };
Kevin Lubickf8823b52020-09-03 10:02:10 -0400574
Kevin Lubick72f40762020-12-16 16:00:55 -0500575 CanvasKit.Canvas.prototype.drawImageRectCubic = function(img, src, dest, B, C, paint) {
Kevin Lubicked962642021-02-02 08:18:11 -0500576 copyRectToWasm(src, _scratchFourFloatsAPtr);
577 copyRectToWasm(dest, _scratchFourFloatsBPtr);
578 this._drawImageRectCubic(img, _scratchFourFloatsAPtr, _scratchFourFloatsBPtr, B, C,
579 paint || null);
Kevin Lubick72f40762020-12-16 16:00:55 -0500580 };
581
582 CanvasKit.Canvas.prototype.drawImageRectOptions = function(img, src, dest, filter, mipmap, paint) {
Kevin Lubicked962642021-02-02 08:18:11 -0500583 copyRectToWasm(src, _scratchFourFloatsAPtr);
584 copyRectToWasm(dest, _scratchFourFloatsBPtr);
585 this._drawImageRectOptions(img, _scratchFourFloatsAPtr, _scratchFourFloatsBPtr, filter, mipmap,
586 paint || null);
Kevin Lubick72f40762020-12-16 16:00:55 -0500587 };
588
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400589 CanvasKit.Canvas.prototype.drawOval = function(oval, paint) {
Kevin Lubickf8823b52020-09-03 10:02:10 -0400590 var oPtr = copyRectToWasm(oval);
591 this._drawOval(oPtr, paint);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500592 };
Kevin Lubickf8823b52020-09-03 10:02:10 -0400593
Kevin Lubick658cb712020-12-01 14:55:02 -0500594 // points is a 1d array of length 2n representing n points where the even indices
595 // will be treated as x coordinates and the odd indices will be treated as y coordinates.
596 // Like other APIs, this accepts a malloced type array or malloc obj.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400597 CanvasKit.Canvas.prototype.drawPoints = function(mode, points, paint) {
Kevin Lubick658cb712020-12-01 14:55:02 -0500598 var ptr = copy1dArray(points, 'HEAPF32');
599 this._drawPoints(mode, ptr, points.length / 2, paint);
Kevin Lubickcf118922020-05-28 14:43:38 -0400600 freeArraysThatAreNotMallocedByUsers(ptr, points);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500601 };
Kevin Lubick37ab53e2019-11-11 10:06:08 -0500602
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400603 CanvasKit.Canvas.prototype.drawRRect = function(rrect, paint) {
Kevin Lubickbe728012020-09-03 11:57:12 +0000604 var rPtr = copyRRectToWasm(rrect);
605 this._drawRRect(rPtr, paint);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500606 };
Kevin Lubickbe728012020-09-03 11:57:12 +0000607
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400608 CanvasKit.Canvas.prototype.drawRect = function(rect, paint) {
Kevin Lubickf8823b52020-09-03 10:02:10 -0400609 var rPtr = copyRectToWasm(rect);
610 this._drawRect(rPtr, paint);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500611 };
Kevin Lubickf8823b52020-09-03 10:02:10 -0400612
Kevin Lubickcbaea292021-01-13 14:16:58 -0500613 CanvasKit.Canvas.prototype.drawShadow = function(path, zPlaneParams, lightPos, lightRadius,
614 ambientColor, spotColor, flags) {
Kevin Lubick6aa38692020-06-01 11:25:47 -0400615 var ambiPtr = copyColorToWasmNoScratch(ambientColor);
616 var spotPtr = copyColorToWasmNoScratch(spotColor);
Kevin Lubickf6040ef2021-02-02 08:26:48 -0500617 // We use the return value from copy1dArray in case the passed in arrays are malloc'd.
618 var zPlanePtr = copy1dArray(zPlaneParams, 'HEAPF32', _scratchThreeFloatsAPtr);
619 var lightPosPtr = copy1dArray(lightPos, 'HEAPF32', _scratchThreeFloatsBPtr);
620 this._drawShadow(path, zPlanePtr, lightPosPtr, lightRadius, ambiPtr, spotPtr, flags);
Kevin Lubickcf118922020-05-28 14:43:38 -0400621 freeArraysThatAreNotMallocedByUsers(ambiPtr, ambientColor);
622 freeArraysThatAreNotMallocedByUsers(spotPtr, spotColor);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500623 };
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400624
Kevin Lubickcbaea292021-01-13 14:16:58 -0500625 CanvasKit.getShadowLocalBounds = function(ctm, path, zPlaneParams, lightPos, lightRadius,
626 flags, optOutputRect) {
627 var ctmPtr = copy3x3MatrixToWasm(ctm);
Kevin Lubickf6040ef2021-02-02 08:26:48 -0500628 // We use the return value from copy1dArray in case the passed in arrays are malloc'd.
629 var zPlanePtr = copy1dArray(zPlaneParams, 'HEAPF32', _scratchThreeFloatsAPtr);
630 var lightPosPtr = copy1dArray(lightPos, 'HEAPF32', _scratchThreeFloatsBPtr);
631 var ok = this._getShadowLocalBounds(ctmPtr, path, zPlanePtr, lightPosPtr, lightRadius,
Kevin Lubicked962642021-02-02 08:18:11 -0500632 flags, _scratchFourFloatsAPtr);
Kevin Lubickcbaea292021-01-13 14:16:58 -0500633 if (!ok) {
634 return null;
635 }
Kevin Lubicked962642021-02-02 08:18:11 -0500636 var ta = _scratchFourFloatsA['toTypedArray']();
Kevin Lubickcbaea292021-01-13 14:16:58 -0500637 if (optOutputRect) {
638 optOutputRect.set(ta);
639 return optOutputRect;
640 }
641 return ta.slice();
642 };
643
Kevin Lubickc1d08982020-04-06 13:52:15 -0400644 // getLocalToDevice returns a 4x4 matrix.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400645 CanvasKit.Canvas.prototype.getLocalToDevice = function() {
Kevin Lubickc1d08982020-04-06 13:52:15 -0400646 // _getLocalToDevice will copy the values into the pointer.
Kevin Lubick6aa38692020-06-01 11:25:47 -0400647 this._getLocalToDevice(_scratch4x4MatrixPtr);
648 return copy4x4MatrixFromWasm(_scratch4x4MatrixPtr);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500649 };
Kevin Lubickc1d08982020-04-06 13:52:15 -0400650
Nathaniel Nifong00de91c2020-05-06 16:22:33 -0400651 // findMarkedCTM returns a 4x4 matrix, or null if a matrix was not found at
652 // the provided marker.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400653 CanvasKit.Canvas.prototype.findMarkedCTM = function(marker) {
Nathaniel Nifong00de91c2020-05-06 16:22:33 -0400654 // _getLocalToDevice will copy the values into the pointer.
Kevin Lubick6aa38692020-06-01 11:25:47 -0400655 var found = this._findMarkedCTM(marker, _scratch4x4MatrixPtr);
Nathaniel Nifong00de91c2020-05-06 16:22:33 -0400656 if (!found) {
657 return null;
658 }
Kevin Lubick6aa38692020-06-01 11:25:47 -0400659 return copy4x4MatrixFromWasm(_scratch4x4MatrixPtr);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500660 };
Nathaniel Nifong00de91c2020-05-06 16:22:33 -0400661
Kevin Lubickc1d08982020-04-06 13:52:15 -0400662 // getTotalMatrix returns the current matrix as a 3x3 matrix.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400663 CanvasKit.Canvas.prototype.getTotalMatrix = function() {
Kevin Lubick6bffe392020-04-02 15:24:15 -0400664 // _getTotalMatrix will copy the values into the pointer.
Kevin Lubick6aa38692020-06-01 11:25:47 -0400665 this._getTotalMatrix(_scratch3x3MatrixPtr);
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400666 // read them out into an array. TODO(kjlubick): If we change Matrix to be
Kevin Lubick6bffe392020-04-02 15:24:15 -0400667 // typedArrays, then we should return a typed array here too.
668 var rv = new Array(9);
669 for (var i = 0; i < 9; i++) {
Kevin Lubick6aa38692020-06-01 11:25:47 -0400670 rv[i] = CanvasKit.HEAPF32[_scratch3x3MatrixPtr/4 + i]; // divide by 4 to "cast" to float.
Kevin Lubick6bffe392020-04-02 15:24:15 -0400671 }
Kevin Lubick6bffe392020-04-02 15:24:15 -0400672 return rv;
Kevin Lubick9fe83912020-11-03 17:08:34 -0500673 };
Kevin Lubick6bffe392020-04-02 15:24:15 -0400674
Kevin Lubickb8123cc2020-11-06 13:05:37 -0500675 CanvasKit.Canvas.prototype.readPixels = function(srcX, srcY, imageInfo, destMallocObj,
676 bytesPerRow) {
677 return readPixels(this, srcX, srcY, imageInfo, destMallocObj, bytesPerRow);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500678 };
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500679
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400680 CanvasKit.Canvas.prototype.saveLayer = function(paint, boundsRect, backdrop, flags) {
Kevin Lubickf8823b52020-09-03 10:02:10 -0400681 // bPtr will be 0 (nullptr) if boundsRect is undefined/null.
682 var bPtr = copyRectToWasm(boundsRect);
683 // These or clauses help emscripten, which does not deal with undefined well.
684 return this._saveLayer(paint || null, bPtr, backdrop || null, flags || 0);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500685 };
Kevin Lubickf8823b52020-09-03 10:02:10 -0400686
Kevin Lubickcf118922020-05-28 14:43:38 -0400687 // pixels should be a Uint8Array or a plain JS array.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400688 CanvasKit.Canvas.prototype.writePixels = function(pixels, srcWidth, srcHeight,
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -0400689 destX, destY, alphaType, colorType, colorSpace) {
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500690 if (pixels.byteLength % (srcWidth * srcHeight)) {
691 throw 'pixels length must be a multiple of the srcWidth * srcHeight';
692 }
693 var bytesPerPixel = pixels.byteLength / (srcWidth * srcHeight);
694 // supply defaults (which are compatible with HTMLCanvas's putImageData)
695 alphaType = alphaType || CanvasKit.AlphaType.Unpremul;
696 colorType = colorType || CanvasKit.ColorType.RGBA_8888;
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400697 colorSpace = colorSpace || CanvasKit.ColorSpace.SRGB;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500698 var srcRowBytes = bytesPerPixel * srcWidth;
699
Kevin Lubickbe728012020-09-03 11:57:12 +0000700 var pptr = copy1dArray(pixels, 'HEAPU8');
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500701 var ok = this._writePixels({
702 'width': srcWidth,
703 'height': srcHeight,
704 'colorType': colorType,
705 'alphaType': alphaType,
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -0400706 'colorSpace': colorSpace,
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500707 }, pptr, srcRowBytes, destX, destY);
708
Kevin Lubickcf118922020-05-28 14:43:38 -0400709 freeArraysThatAreNotMallocedByUsers(pptr, pixels);
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500710 return ok;
Kevin Lubick9fe83912020-11-03 17:08:34 -0500711 };
Kevin Lubick52b9f372018-12-04 13:57:36 -0500712
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400713 CanvasKit.ColorFilter.MakeBlend = function(color4f, mode) {
Kevin Lubick6aa38692020-06-01 11:25:47 -0400714 var cPtr = copyColorToWasm(color4f);
Kevin Lubick33645792021-01-29 08:37:41 -0500715 return CanvasKit.ColorFilter._MakeBlend(cPtr, mode);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500716 };
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400717
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400718 // colorMatrix is an ColorMatrix (e.g. Float32Array of length 20)
719 CanvasKit.ColorFilter.MakeMatrix = function(colorMatrix) {
Kevin Lubickd3729342019-09-12 11:11:25 -0400720 if (!colorMatrix || colorMatrix.length !== 20) {
Kevin Lubick6bffe392020-04-02 15:24:15 -0400721 throw 'invalid color matrix';
Kevin Lubickd3729342019-09-12 11:11:25 -0400722 }
Kevin Lubickbe728012020-09-03 11:57:12 +0000723 var fptr = copy1dArray(colorMatrix, 'HEAPF32');
Kevin Lubickd3729342019-09-12 11:11:25 -0400724 // We know skia memcopies the floats, so we can free our memory after the call returns.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400725 var m = CanvasKit.ColorFilter._makeMatrix(fptr);
Kevin Lubickcf118922020-05-28 14:43:38 -0400726 freeArraysThatAreNotMallocedByUsers(fptr, colorMatrix);
Kevin Lubickd3729342019-09-12 11:11:25 -0400727 return m;
Kevin Lubick9fe83912020-11-03 17:08:34 -0500728 };
Kevin Lubickd3729342019-09-12 11:11:25 -0400729
Kevin Lubicke9e8e5d2021-02-02 10:21:24 -0500730 CanvasKit.ContourMeasure.prototype.getPosTan = function(distance, optionalOutput) {
731 this._getPosTan(distance, _scratchFourFloatsAPtr);
732 var ta = _scratchFourFloatsA['toTypedArray']();
733 if (optionalOutput) {
734 optionalOutput.set(ta);
735 return optionalOutput;
736 }
737 return ta.slice();
738 };
739
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400740 CanvasKit.ImageFilter.MakeMatrixTransform = function(matr, filterQuality, input) {
Kevin Lubick6bffe392020-04-02 15:24:15 -0400741 var matrPtr = copy3x3MatrixToWasm(matr);
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400742 return CanvasKit.ImageFilter._MakeMatrixTransform(matrPtr, filterQuality, input);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500743 };
Kevin Lubick6bffe392020-04-02 15:24:15 -0400744
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400745 CanvasKit.Paint.prototype.getColor = function() {
Kevin Lubick6aa38692020-06-01 11:25:47 -0400746 this._getColor(_scratchColorPtr);
747 return copyColorFromWasm(_scratchColorPtr);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500748 };
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400749
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400750 CanvasKit.Paint.prototype.setColor = function(color4f, colorSpace) {
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -0400751 colorSpace = colorSpace || null; // null will be replaced with sRGB in the C++ method.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400752 // emscripten wouldn't bind undefined to the sk_sp<ColorSpace> expected here.
Kevin Lubick6aa38692020-06-01 11:25:47 -0400753 var cPtr = copyColorToWasm(color4f);
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -0400754 this._setColor(cPtr, colorSpace);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500755 };
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400756
Kevin Lubick93f1a382020-06-02 16:15:23 -0400757 // The color components here are expected to be floating point values (nominally between
758 // 0.0 and 1.0, but with wider color gamuts, the values could exceed this range). To convert
759 // between standard 8 bit colors and floats, just divide by 255 before passing them in.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400760 CanvasKit.Paint.prototype.setColorComponents = function(r, g, b, a, colorSpace) {
Kevin Lubick93f1a382020-06-02 16:15:23 -0400761 colorSpace = colorSpace || null; // null will be replaced with sRGB in the C++ method.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400762 // emscripten wouldn't bind undefined to the sk_sp<ColorSpace> expected here.
Kevin Lubick93f1a382020-06-02 16:15:23 -0400763 var cPtr = copyColorComponentsToWasm(r, g, b, a);
764 this._setColor(cPtr, colorSpace);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500765 };
Kevin Lubick93f1a382020-06-02 16:15:23 -0400766
Kevin Lubicked962642021-02-02 08:18:11 -0500767 CanvasKit.Path.prototype.getPoint = function(idx, optionalOutput) {
768 // This will copy 2 floats into a space for 4 floats
769 this._getPoint(idx, _scratchFourFloatsAPtr);
770 var ta = _scratchFourFloatsA['toTypedArray']();
771 if (optionalOutput) {
772 // We cannot call optionalOutput.set() because it is an error to call .set() with
773 // a source bigger than the destination.
774 optionalOutput[0] = ta[0];
775 optionalOutput[1] = ta[1];
776 return optionalOutput;
777 }
778 // Be sure to return a copy of just the first 2 values.
779 return ta.slice(0, 2);
780 };
781
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400782 CanvasKit.PictureRecorder.prototype.beginRecording = function(bounds) {
Kevin Lubickf8823b52020-09-03 10:02:10 -0400783 var bPtr = copyRectToWasm(bounds);
784 return this._beginRecording(bPtr);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500785 };
Kevin Lubickf8823b52020-09-03 10:02:10 -0400786
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400787 CanvasKit.Surface.prototype.makeImageSnapshot = function(optionalBoundsRect) {
Kevin Lubickf8823b52020-09-03 10:02:10 -0400788 var bPtr = copyIRectToWasm(optionalBoundsRect);
789 return this._makeImageSnapshot(bPtr);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500790 };
Kevin Lubickf8823b52020-09-03 10:02:10 -0400791
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400792 CanvasKit.Surface.prototype.requestAnimationFrame = function(callback, dirtyRect) {
Kevin Lubick359a7e32019-03-19 09:34:37 -0400793 if (!this._cached_canvas) {
794 this._cached_canvas = this.getCanvas();
795 }
Elliot Evans1ec3b1a2020-07-23 10:25:58 -0400796 requestAnimationFrame(function() {
Kevin Lubick39026282019-03-28 12:46:40 -0400797 if (this._context !== undefined) {
798 CanvasKit.setCurrentContext(this._context);
799 }
Kevin Lubick359a7e32019-03-19 09:34:37 -0400800
801 callback(this._cached_canvas);
802
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400803 // We do not dispose() of the Surface here, as the client will typically
Bryce Thomas2c5b8562020-01-22 13:49:41 -0800804 // call requestAnimationFrame again from within the supplied callback.
805 // For drawing a single frame, prefer drawOnce().
Bryce Thomas9331ca02020-05-29 16:51:21 -0700806 this.flush(dirtyRect);
Kevin Lubick359a7e32019-03-19 09:34:37 -0400807 }.bind(this));
Kevin Lubick9fe83912020-11-03 17:08:34 -0500808 };
Kevin Lubick359a7e32019-03-19 09:34:37 -0400809
Kevin Lubick52379332020-01-27 10:01:25 -0500810 // drawOnce will dispose of the surface after drawing the frame using the provided
811 // callback.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400812 CanvasKit.Surface.prototype.drawOnce = function(callback, dirtyRect) {
Bryce Thomas2c5b8562020-01-22 13:49:41 -0800813 if (!this._cached_canvas) {
814 this._cached_canvas = this.getCanvas();
815 }
Elliot Evans1ec3b1a2020-07-23 10:25:58 -0400816 requestAnimationFrame(function() {
Bryce Thomas2c5b8562020-01-22 13:49:41 -0800817 if (this._context !== undefined) {
818 CanvasKit.setCurrentContext(this._context);
819 }
820 callback(this._cached_canvas);
821
Bryce Thomas9331ca02020-05-29 16:51:21 -0700822 this.flush(dirtyRect);
Bryce Thomas2c5b8562020-01-22 13:49:41 -0800823 this.dispose();
824 }.bind(this));
Kevin Lubick9fe83912020-11-03 17:08:34 -0500825 };
Bryce Thomas2c5b8562020-01-22 13:49:41 -0800826
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400827 CanvasKit.PathEffect.MakeDash = function(intervals, phase) {
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -0500828 if (!phase) {
829 phase = 0;
830 }
831 if (!intervals.length || intervals.length % 2 === 1) {
832 throw 'Intervals array must have even length';
833 }
Kevin Lubickbe728012020-09-03 11:57:12 +0000834 var ptr = copy1dArray(intervals, 'HEAPF32');
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400835 var dpe = CanvasKit.PathEffect._MakeDash(ptr, intervals.length, phase);
Kevin Lubickcf118922020-05-28 14:43:38 -0400836 freeArraysThatAreNotMallocedByUsers(ptr, intervals);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -0500837 return dpe;
Kevin Lubick9fe83912020-11-03 17:08:34 -0500838 };
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -0500839
Kevin Lubick421ba882020-10-15 13:07:33 -0400840 CanvasKit.Shader.MakeColor = function(color4f, colorSpace) {
Kevin Lubicked962642021-02-02 08:18:11 -0500841 colorSpace = colorSpace || null;
Kevin Lubick6aa38692020-06-01 11:25:47 -0400842 var cPtr = copyColorToWasm(color4f);
Kevin Lubick421ba882020-10-15 13:07:33 -0400843 return CanvasKit.Shader._MakeColor(cPtr, colorSpace);
Kevin Lubick9fe83912020-11-03 17:08:34 -0500844 };
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400845
Kevin Lubick421ba882020-10-15 13:07:33 -0400846 // TODO(kjlubick) remove deprecated names.
847 CanvasKit.Shader.Blend = CanvasKit.Shader.MakeBlend;
848 CanvasKit.Shader.Color = CanvasKit.Shader.MakeColor;
849 CanvasKit.Shader.Lerp = CanvasKit.Shader.MakeLerp;
850
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400851 CanvasKit.Shader.MakeLinearGradient = function(start, end, colors, pos, mode, localMatrix, flags, colorSpace) {
Kevin Lubickb8123cc2020-11-06 13:05:37 -0500852 colorSpace = colorSpace || null;
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -0400853 var cPtrInfo = copyFlexibleColorArray(colors);
Kevin Lubick421ba882020-10-15 13:07:33 -0400854 var posPtr = copy1dArray(pos, 'HEAPF32');
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -0500855 flags = flags || 0;
Kevin Lubick6bffe392020-04-02 15:24:15 -0400856 var localMatrixPtr = copy3x3MatrixToWasm(localMatrix);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -0500857
Kevin Lubicked962642021-02-02 08:18:11 -0500858 // Copy start and end to _scratchFourFloatsAPtr.
859 var startEndPts = _scratchFourFloatsA['toTypedArray']();
860 startEndPts.set(start);
861 startEndPts.set(end, 2);
862
863 var lgs = CanvasKit.Shader._MakeLinearGradient(_scratchFourFloatsAPtr, cPtrInfo.colorPtr, cPtrInfo.colorType, posPtr,
Kevin Lubick421ba882020-10-15 13:07:33 -0400864 cPtrInfo.count, mode, flags, localMatrixPtr, colorSpace);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -0500865
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -0400866 freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors);
Kevin Lubicke7b329e2020-06-05 15:58:01 -0400867 pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -0500868 return lgs;
Kevin Lubick9fe83912020-11-03 17:08:34 -0500869 };
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -0500870
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400871 CanvasKit.Shader.MakeRadialGradient = function(center, radius, colors, pos, mode, localMatrix, flags, colorSpace) {
Kevin Lubicked962642021-02-02 08:18:11 -0500872 colorSpace = colorSpace || null;
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -0400873 var cPtrInfo = copyFlexibleColorArray(colors);
Kevin Lubick421ba882020-10-15 13:07:33 -0400874 var posPtr = copy1dArray(pos, 'HEAPF32');
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -0500875 flags = flags || 0;
Kevin Lubick6bffe392020-04-02 15:24:15 -0400876 var localMatrixPtr = copy3x3MatrixToWasm(localMatrix);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -0500877
Kevin Lubicked962642021-02-02 08:18:11 -0500878 var rgs = CanvasKit.Shader._MakeRadialGradient(center[0], center[1], radius, cPtrInfo.colorPtr,
879 cPtrInfo.colorType, posPtr, cPtrInfo.count, mode,
880 flags, localMatrixPtr, colorSpace);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -0500881
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -0400882 freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors);
Kevin Lubicke7b329e2020-06-05 15:58:01 -0400883 pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -0500884 return rgs;
Kevin Lubick9fe83912020-11-03 17:08:34 -0500885 };
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -0500886
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400887 CanvasKit.Shader.MakeSweepGradient = function(cx, cy, colors, pos, mode, localMatrix, flags, startAngle, endAngle, colorSpace) {
Kevin Lubicked962642021-02-02 08:18:11 -0500888 colorSpace = colorSpace || null;
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -0400889 var cPtrInfo = copyFlexibleColorArray(colors);
Kevin Lubick421ba882020-10-15 13:07:33 -0400890 var posPtr = copy1dArray(pos, 'HEAPF32');
Dan Field3d44f732020-03-16 09:17:30 -0700891 flags = flags || 0;
892 startAngle = startAngle || 0;
893 endAngle = endAngle || 360;
Kevin Lubick6bffe392020-04-02 15:24:15 -0400894 var localMatrixPtr = copy3x3MatrixToWasm(localMatrix);
Dan Field3d44f732020-03-16 09:17:30 -0700895
Kevin Lubick421ba882020-10-15 13:07:33 -0400896 var sgs = CanvasKit.Shader._MakeSweepGradient(cx, cy, cPtrInfo.colorPtr, cPtrInfo.colorType, posPtr,
897 cPtrInfo.count, mode,
898 startAngle, endAngle, flags,
899 localMatrixPtr, colorSpace);
Dan Field3d44f732020-03-16 09:17:30 -0700900
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -0400901 freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors);
Kevin Lubicke7b329e2020-06-05 15:58:01 -0400902 pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos);
Dan Field3d44f732020-03-16 09:17:30 -0700903 return sgs;
Kevin Lubick9fe83912020-11-03 17:08:34 -0500904 };
Dan Field3d44f732020-03-16 09:17:30 -0700905
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400906 CanvasKit.Shader.MakeTwoPointConicalGradient = function(start, startRadius, end, endRadius,
Kevin Lubick421ba882020-10-15 13:07:33 -0400907 colors, pos, mode, localMatrix, flags, colorSpace) {
Kevin Lubicked962642021-02-02 08:18:11 -0500908 colorSpace = colorSpace || null;
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -0400909 var cPtrInfo = copyFlexibleColorArray(colors);
Kevin Lubick421ba882020-10-15 13:07:33 -0400910 var posPtr = copy1dArray(pos, 'HEAPF32');
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -0500911 flags = flags || 0;
Kevin Lubick6bffe392020-04-02 15:24:15 -0400912 var localMatrixPtr = copy3x3MatrixToWasm(localMatrix);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -0500913
Kevin Lubicked962642021-02-02 08:18:11 -0500914 // Copy start and end to _scratchFourFloatsAPtr.
915 var startEndPts = _scratchFourFloatsA['toTypedArray']();
916 startEndPts.set(start);
917 startEndPts.set(end, 2);
918
919 var rgs = CanvasKit.Shader._MakeTwoPointConicalGradient(_scratchFourFloatsAPtr,
920 startRadius, endRadius, cPtrInfo.colorPtr, cPtrInfo.colorType,
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -0400921 posPtr, cPtrInfo.count, mode, flags, localMatrixPtr, colorSpace);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -0500922
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -0400923 freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors);
Kevin Lubicke7b329e2020-06-05 15:58:01 -0400924 pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos);
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -0500925 return rgs;
Kevin Lubick9fe83912020-11-03 17:08:34 -0500926 };
Nathaniel Nifong23b0ed92020-03-04 15:43:50 -0500927
Kevin Lubickf8823b52020-09-03 10:02:10 -0400928 // Clients can pass in a Float32Array with length 4 to this and the results
929 // will be copied into that array. Otherwise, a new TypedArray will be allocated
930 // and returned.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400931 CanvasKit.Vertices.prototype.bounds = function(optionalOutputArray) {
Kevin Lubicked962642021-02-02 08:18:11 -0500932 this._bounds(_scratchFourFloatsAPtr);
933 var ta = _scratchFourFloatsA['toTypedArray']();
Kevin Lubickf8823b52020-09-03 10:02:10 -0400934 if (optionalOutputArray) {
935 optionalOutputArray.set(ta);
936 return optionalOutputArray;
937 }
938 return ta.slice();
Kevin Lubick9fe83912020-11-03 17:08:34 -0500939 };
Kevin Lubickf8823b52020-09-03 10:02:10 -0400940
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500941 // Run through the JS files that are added at compile time.
942 if (CanvasKit._extraInitializations) {
943 CanvasKit._extraInitializations.forEach(function(init) {
944 init();
945 });
Kevin Lubickeb2f6b02018-11-29 15:07:02 -0500946 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500947}; // end CanvasKit.onRuntimeInitialized, that is, anything changing prototypes or dynamic.
Kevin Lubickeb2f6b02018-11-29 15:07:02 -0500948
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400949// Accepts an object holding two canvaskit colors.
950// {
Kevin Lubick3d00e3a2020-10-02 14:59:28 -0400951// ambient: [r, g, b, a],
952// spot: [r, g, b, a],
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400953// }
Kevin Lubicke7b329e2020-06-05 15:58:01 -0400954// Returns the same format. Note, if malloced colors are passed in, the memory
955// housing the passed in colors passed in will be overwritten with the computed
956// tonal colors.
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400957CanvasKit.computeTonalColors = function(tonalColors) {
Kevin Lubicke7b329e2020-06-05 15:58:01 -0400958 // copy the colors into WASM
Kevin Lubick6aa38692020-06-01 11:25:47 -0400959 var cPtrAmbi = copyColorToWasmNoScratch(tonalColors['ambient']);
960 var cPtrSpot = copyColorToWasmNoScratch(tonalColors['spot']);
Kevin Lubicke7b329e2020-06-05 15:58:01 -0400961 // The output of this function will be the same pointers we passed in.
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400962 this._computeTonalColors(cPtrAmbi, cPtrSpot);
Kevin Lubicke7b329e2020-06-05 15:58:01 -0400963 // Read the results out.
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400964 var result = {
965 'ambient': copyColorFromWasm(cPtrAmbi),
966 'spot': copyColorFromWasm(cPtrSpot),
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -0400967 };
Kevin Lubicke7b329e2020-06-05 15:58:01 -0400968 // If the user passed us malloced colors in here, we don't want to clean them up.
969 freeArraysThatAreNotMallocedByUsers(cPtrAmbi, tonalColors['ambient']);
970 freeArraysThatAreNotMallocedByUsers(cPtrSpot, tonalColors['spot']);
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400971 return result;
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -0400972};
Nathaniel Nifong1bedbeb2020-05-04 16:46:17 -0400973
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500974CanvasKit.LTRBRect = function(l, t, r, b) {
Kevin Lubickf8823b52020-09-03 10:02:10 -0400975 return Float32Array.of(l, t, r, b);
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -0400976};
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500977
Kevin Lubickf5ea37f2019-02-28 10:06:18 -0500978CanvasKit.XYWHRect = function(x, y, w, h) {
Kevin Lubickf8823b52020-09-03 10:02:10 -0400979 return Float32Array.of(x, y, x+w, y+h);
980};
981
982CanvasKit.LTRBiRect = function(l, t, r, b) {
983 return Int32Array.of(l, t, r, b);
984};
985
986CanvasKit.XYWHiRect = function(x, y, w, h) {
987 return Int32Array.of(x, y, x+w, y+h);
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -0400988};
Kevin Lubick1a05fce2018-11-20 12:51:16 -0500989
Kevin Lubickbe728012020-09-03 11:57:12 +0000990// RRectXY returns a TypedArray representing an RRect with the given rect and a radiusX and
991// radiusY for all 4 corners.
Kevin Lubick7d644e12019-09-11 14:22:22 -0400992CanvasKit.RRectXY = function(rect, rx, ry) {
Kevin Lubickbe728012020-09-03 11:57:12 +0000993 return Float32Array.of(
Kevin Lubickf8823b52020-09-03 10:02:10 -0400994 rect[0], rect[1], rect[2], rect[3],
Kevin Lubickbe728012020-09-03 11:57:12 +0000995 rx, ry,
996 rx, ry,
997 rx, ry,
998 rx, ry,
999 );
Kevin Lubickd9b9e5e2020-06-23 16:58:10 -04001000};
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001001
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001002// data is a TypedArray or ArrayBuffer e.g. from fetch().then(resp.arrayBuffer())
Kevin Lubick6b921b72019-09-18 16:18:17 -04001003CanvasKit.MakeAnimatedImageFromEncoded = function(data) {
1004 data = new Uint8Array(data);
1005
1006 var iptr = CanvasKit._malloc(data.byteLength);
1007 CanvasKit.HEAPU8.set(data, iptr);
1008 var img = CanvasKit._decodeAnimatedImage(iptr, data.byteLength);
1009 if (!img) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001010 Debug('Could not decode animated image');
Kevin Lubick6b921b72019-09-18 16:18:17 -04001011 return null;
1012 }
1013 return img;
Kevin Lubick9fe83912020-11-03 17:08:34 -05001014};
Kevin Lubick6b921b72019-09-18 16:18:17 -04001015
1016// data is a TypedArray or ArrayBuffer e.g. from fetch().then(resp.arrayBuffer())
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001017CanvasKit.MakeImageFromEncoded = function(data) {
1018 data = new Uint8Array(data);
1019
1020 var iptr = CanvasKit._malloc(data.byteLength);
1021 CanvasKit.HEAPU8.set(data, iptr);
1022 var img = CanvasKit._decodeImage(iptr, data.byteLength);
1023 if (!img) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001024 Debug('Could not decode image');
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001025 return null;
1026 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001027 return img;
Kevin Lubick9fe83912020-11-03 17:08:34 -05001028};
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001029
Elliot Evans28796192020-06-15 12:53:27 -06001030// A variable to hold a canvasElement which can be reused once created the first time.
1031var memoizedCanvas2dElement = null;
1032
1033// Alternative to CanvasKit.MakeImageFromEncoded. Allows for CanvasKit users to take advantage of
1034// browser APIs to decode images instead of using codecs included in the CanvasKit wasm binary.
1035// Expects that the canvasImageSource has already loaded/decoded.
1036// CanvasImageSource reference: https://developer.mozilla.org/en-US/docs/Web/API/CanvasImageSource
1037CanvasKit.MakeImageFromCanvasImageSource = function(canvasImageSource) {
1038 var width = canvasImageSource.width;
1039 var height = canvasImageSource.height;
1040
1041 if (!memoizedCanvas2dElement) {
1042 memoizedCanvas2dElement = document.createElement('canvas');
1043 }
1044 memoizedCanvas2dElement.width = width;
1045 memoizedCanvas2dElement.height = height;
1046
1047 var ctx2d = memoizedCanvas2dElement.getContext('2d');
1048 ctx2d.drawImage(canvasImageSource, 0, 0);
1049
1050 var imageData = ctx2d.getImageData(0, 0, width, height);
1051
Kevin Lubickae0d3ff2020-11-18 11:23:15 -05001052 return CanvasKit.MakeImage({
1053 'width': width,
1054 'height': height,
1055 'alphaType': CanvasKit.AlphaType.Unpremul,
1056 'colorType': CanvasKit.ColorType.RGBA_8888,
1057 'colorSpace': CanvasKit.ColorSpace.SRGB
1058 }, imageData.data, 4 * width);
Kevin Lubick9fe83912020-11-03 17:08:34 -05001059};
Elliot Evans28796192020-06-15 12:53:27 -06001060
Kevin Lubickae0d3ff2020-11-18 11:23:15 -05001061// pixels may be an array but Uint8Array or Uint8ClampedArray is recommended,
1062// with the bytes representing the pixel values.
Kevin Lubickeda0b432019-12-02 08:26:48 -05001063// (e.g. each set of 4 bytes could represent RGBA values for a single pixel).
Kevin Lubickae0d3ff2020-11-18 11:23:15 -05001064CanvasKit.MakeImage = function(info, pixels, bytesPerRow) {
1065 var pptr = CanvasKit._malloc(pixels.length);
1066 CanvasKit.HEAPU8.set(pixels, pptr); // We always want to copy the bytes into the WASM heap.
Kevin Lubickeda0b432019-12-02 08:26:48 -05001067 // No need to _free pptr, Image takes it with SkData::MakeFromMalloc
Kevin Lubickae0d3ff2020-11-18 11:23:15 -05001068 return CanvasKit._MakeImage(info, pptr, pixels.length, bytesPerRow);
Kevin Lubick9fe83912020-11-03 17:08:34 -05001069};
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001070
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001071// Colors may be a Uint32Array of int colors, a Flat Float32Array of float colors
1072// or a 2d Array of Float32Array(4) (deprecated)
Kevin Lubicke7c1a732020-12-04 09:10:39 -05001073// the underlying Skia function accepts only int colors so it is recommended
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001074// to pass an array of int colors to avoid an extra conversion.
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001075// ColorBuilder is not accepted.
1076CanvasKit.MakeVertices = function(mode, positions, textureCoordinates, colors,
1077 indices, isVolatile) {
Kevin Lubicke7c1a732020-12-04 09:10:39 -05001078 // Default isVolatile to true if not set
Kevin Lubickb3574c92019-03-06 08:25:36 -05001079 isVolatile = isVolatile === undefined ? true : isVolatile;
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001080 var idxCount = (indices && indices.length) || 0;
1081
1082 var flags = 0;
1083 // These flags are from SkVertices.h and should be kept in sync with those.
1084 if (textureCoordinates && textureCoordinates.length) {
1085 flags |= (1 << 0);
1086 }
1087 if (colors && colors.length) {
1088 flags |= (1 << 1);
1089 }
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001090 if (!isVolatile) {
Mike Reed5caf9352020-03-02 14:57:09 -05001091 flags |= (1 << 2);
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001092 }
1093
Kevin Lubicke7c1a732020-12-04 09:10:39 -05001094 var builder = new CanvasKit._VerticesBuilder(mode, positions.length / 2, idxCount, flags);
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001095
Kevin Lubicke7c1a732020-12-04 09:10:39 -05001096 copy1dArray(positions, 'HEAPF32', builder.positions());
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001097 if (builder.texCoords()) {
Kevin Lubicke7c1a732020-12-04 09:10:39 -05001098 copy1dArray(textureCoordinates, 'HEAPF32', builder.texCoords());
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001099 }
1100 if (builder.colors()) {
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001101 if (colors.build) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001102 throw('Color builder not accepted by MakeVertices, use array of ints');
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001103 } else {
Kevin Lubickbe728012020-09-03 11:57:12 +00001104 copy1dArray(assureIntColors(colors), 'HEAPU32', builder.colors());
Nathaniel Nifongd05fd0c2020-06-11 08:44:20 -04001105 }
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001106 }
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001107 if (builder.indices()) {
Kevin Lubickbe728012020-09-03 11:57:12 +00001108 copy1dArray(indices, 'HEAPU16', builder.indices());
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001109 }
Kevin Lubickb3574c92019-03-06 08:25:36 -05001110
Kevin Lubickd6ba7252019-06-03 14:38:05 -04001111 // Create the vertices, which owns the memory that the builder had allocated.
1112 return builder.detach();
Kevin Lubicka4f218d2020-01-14 08:39:09 -05001113};