Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1 | // 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 Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 4 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 5 | // CanvasKit.onRuntimeInitialized is called after the WASM library has loaded. |
| 6 | // Anything that modifies an exposed class (e.g. SkPath) should be set |
| 7 | // after onRuntimeInitialized, otherwise, it can happen outside of that scope. |
| 8 | CanvasKit.onRuntimeInitialized = function() { |
| 9 | // All calls to 'this' need to go in externs.js so closure doesn't minify them away. |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 10 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 11 | // Add some helpers for matrices. This is ported from SkMatrix.cpp |
| 12 | // to save complexity and overhead of going back and forth between |
| 13 | // C++ and JS layers. |
| 14 | // I would have liked to use something like DOMMatrix, except it |
| 15 | // isn't widely supported (would need polyfills) and it doesn't |
| 16 | // have a mapPoints() function (which could maybe be tacked on here). |
| 17 | // If DOMMatrix catches on, it would be worth re-considering this usage. |
| 18 | CanvasKit.SkMatrix = {}; |
| 19 | function sdot(a, b, c, d, e, f) { |
| 20 | e = e || 0; |
| 21 | f = f || 0; |
| 22 | return a * b + c * d + e * f; |
| 23 | } |
| 24 | |
| 25 | CanvasKit.SkMatrix.identity = function() { |
| 26 | return [ |
| 27 | 1, 0, 0, |
| 28 | 0, 1, 0, |
| 29 | 0, 0, 1, |
| 30 | ]; |
| 31 | }; |
| 32 | |
| 33 | // Return the inverse (if it exists) of this matrix. |
| 34 | // Otherwise, return the identity. |
| 35 | CanvasKit.SkMatrix.invert = function(m) { |
| 36 | var det = m[0]*m[4]*m[8] + m[1]*m[5]*m[6] + m[2]*m[3]*m[7] |
| 37 | - m[2]*m[4]*m[6] - m[1]*m[3]*m[8] - m[0]*m[5]*m[7]; |
| 38 | if (!det) { |
| 39 | SkDebug('Warning, uninvertible matrix'); |
| 40 | return CanvasKit.SkMatrix.identity(); |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 41 | } |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 42 | return [ |
| 43 | (m[4]*m[8] - m[5]*m[7])/det, (m[2]*m[7] - m[1]*m[8])/det, (m[1]*m[5] - m[2]*m[4])/det, |
| 44 | (m[5]*m[6] - m[3]*m[8])/det, (m[0]*m[8] - m[2]*m[6])/det, (m[2]*m[3] - m[0]*m[5])/det, |
| 45 | (m[3]*m[7] - m[4]*m[6])/det, (m[1]*m[6] - m[0]*m[7])/det, (m[0]*m[4] - m[1]*m[3])/det, |
| 46 | ]; |
| 47 | }; |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 48 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 49 | // Maps the given points according to the passed in matrix. |
| 50 | // Results are done in place. |
| 51 | // See SkMatrix.h::mapPoints for the docs on the math. |
| 52 | CanvasKit.SkMatrix.mapPoints = function(matrix, ptArr) { |
| 53 | if (ptArr.length % 2) { |
| 54 | throw 'mapPoints requires an even length arr'; |
Kevin Lubick | b9db390 | 2018-11-26 11:47:54 -0500 | [diff] [blame] | 55 | } |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 56 | for (var i = 0; i < ptArr.length; i+=2) { |
| 57 | var x = ptArr[i], y = ptArr[i+1]; |
| 58 | // Gx+Hy+I |
| 59 | var denom = matrix[6]*x + matrix[7]*y + matrix[8]; |
| 60 | // Ax+By+C |
| 61 | var xTrans = matrix[0]*x + matrix[1]*y + matrix[2]; |
| 62 | // Dx+Ey+F |
| 63 | var yTrans = matrix[3]*x + matrix[4]*y + matrix[5]; |
| 64 | ptArr[i] = xTrans/denom; |
| 65 | ptArr[i+1] = yTrans/denom; |
| 66 | } |
| 67 | return ptArr; |
| 68 | }; |
Kevin Lubick | b9db390 | 2018-11-26 11:47:54 -0500 | [diff] [blame] | 69 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 70 | CanvasKit.SkMatrix.multiply = function(m1, m2) { |
| 71 | var result = [0,0,0, 0,0,0, 0,0,0]; |
| 72 | for (var r = 0; r < 3; r++) { |
| 73 | for (var c = 0; c < 3; c++) { |
| 74 | // m1 and m2 are 1D arrays pretending to be 2D arrays |
| 75 | result[3*r + c] = sdot(m1[3*r + 0], m2[3*0 + c], |
| 76 | m1[3*r + 1], m2[3*1 + c], |
| 77 | m1[3*r + 2], m2[3*2 + c]); |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 78 | } |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 79 | } |
| 80 | return result; |
| 81 | } |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 82 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 83 | // Return a matrix representing a rotation by n radians. |
| 84 | // px, py optionally say which point the rotation should be around |
| 85 | // with the default being (0, 0); |
| 86 | CanvasKit.SkMatrix.rotated = function(radians, px, py) { |
| 87 | px = px || 0; |
| 88 | py = py || 0; |
| 89 | var sinV = Math.sin(radians); |
| 90 | var cosV = Math.cos(radians); |
| 91 | return [ |
| 92 | cosV, -sinV, sdot( sinV, py, 1 - cosV, px), |
| 93 | sinV, cosV, sdot(-sinV, px, 1 - cosV, py), |
| 94 | 0, 0, 1, |
| 95 | ]; |
| 96 | }; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 97 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 98 | CanvasKit.SkMatrix.scaled = function(sx, sy, px, py) { |
| 99 | px = px || 0; |
| 100 | py = py || 0; |
| 101 | return [ |
| 102 | sx, 0, px - sx * px, |
| 103 | 0, sy, py - sy * py, |
| 104 | 0, 0, 1, |
| 105 | ]; |
| 106 | }; |
Kevin Lubick | da3d8ac | 2019-01-07 11:08:55 -0500 | [diff] [blame] | 107 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 108 | CanvasKit.SkMatrix.skewed = function(kx, ky, px, py) { |
| 109 | px = px || 0; |
| 110 | py = py || 0; |
| 111 | return [ |
| 112 | 1, kx, -kx * px, |
| 113 | ky, 1, -ky * py, |
| 114 | 0, 0, 1, |
| 115 | ]; |
| 116 | }; |
Alexander Khovansky | 3e11933 | 2018-11-15 02:01:19 +0300 | [diff] [blame] | 117 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 118 | CanvasKit.SkMatrix.translated = function(dx, dy) { |
| 119 | return [ |
| 120 | 1, 0, dx, |
| 121 | 0, 1, dy, |
| 122 | 0, 0, 1, |
| 123 | ]; |
| 124 | }; |
Kevin Lubick | 1646e7d | 2018-12-07 13:03:08 -0500 | [diff] [blame] | 125 | |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 126 | // An SkColorMatrix is a 4x4 color matrix that transforms the 4 color channels |
| 127 | // with a 1x4 matrix that post-translates those 4 channels. |
| 128 | // For example, the following is the layout with the scale (S) and post-transform |
| 129 | // (PT) items indicated. |
| 130 | // RS, 0, 0, 0 | RPT |
| 131 | // 0, GS, 0, 0 | GPT |
| 132 | // 0, 0, BS, 0 | BPT |
| 133 | // 0, 0, 0, AS | APT |
| 134 | // |
| 135 | // Much of this was hand-transcribed from SkColorMatrix.cpp, because it's easier to |
| 136 | // deal with a Float32Array of length 20 than to try to expose the SkColorMatrix object. |
| 137 | |
| 138 | var rScale = 0; |
| 139 | var gScale = 6; |
| 140 | var bScale = 12; |
| 141 | var aScale = 18; |
| 142 | |
| 143 | var rPostTrans = 4; |
| 144 | var gPostTrans = 9; |
| 145 | var bPostTrans = 14; |
| 146 | var aPostTrans = 19; |
| 147 | |
| 148 | CanvasKit.SkColorMatrix = {}; |
| 149 | CanvasKit.SkColorMatrix.identity = function() { |
| 150 | var m = new Float32Array(20); |
| 151 | m[rScale] = 1; |
| 152 | m[gScale] = 1; |
| 153 | m[bScale] = 1; |
| 154 | m[aScale] = 1; |
| 155 | return m; |
| 156 | } |
| 157 | |
| 158 | CanvasKit.SkColorMatrix.scaled = function(rs, gs, bs, as) { |
| 159 | var m = new Float32Array(20); |
| 160 | m[rScale] = rs; |
| 161 | m[gScale] = gs; |
| 162 | m[bScale] = bs; |
| 163 | m[aScale] = as; |
| 164 | return m; |
| 165 | } |
| 166 | |
| 167 | var rotateIndices = [ |
| 168 | [6, 7, 11, 12], |
| 169 | [0, 10, 2, 12], |
| 170 | [0, 1, 5, 6], |
| 171 | ]; |
| 172 | // axis should be 0, 1, 2 for r, g, b |
| 173 | CanvasKit.SkColorMatrix.rotated = function(axis, sine, cosine) { |
| 174 | var m = CanvasKit.SkColorMatrix.identity(); |
| 175 | var indices = rotateIndices[axis]; |
| 176 | m[indices[0]] = cosine; |
| 177 | m[indices[1]] = sine; |
| 178 | m[indices[2]] = -sine; |
| 179 | m[indices[3]] = cosine; |
| 180 | return m; |
| 181 | } |
| 182 | |
| 183 | // m is a SkColorMatrix (i.e. a Float32Array), and this sets the 4 "special" |
| 184 | // params that will translate the colors after they are multiplied by the 4x4 matrix. |
| 185 | CanvasKit.SkColorMatrix.postTranslate = function(m, dr, dg, db, da) { |
| 186 | m[rPostTrans] += dr; |
| 187 | m[gPostTrans] += dg; |
| 188 | m[bPostTrans] += db; |
| 189 | m[aPostTrans] += da; |
| 190 | return m; |
| 191 | } |
| 192 | |
| 193 | // concat returns a new SkColorMatrix that is the result of multiplying outer*inner; |
| 194 | CanvasKit.SkColorMatrix.concat = function(outer, inner) { |
| 195 | var m = new Float32Array(20); |
| 196 | var index = 0; |
| 197 | for (var j = 0; j < 20; j += 5) { |
| 198 | for (var i = 0; i < 4; i++) { |
| 199 | m[index++] = outer[j + 0] * inner[i + 0] + |
| 200 | outer[j + 1] * inner[i + 5] + |
| 201 | outer[j + 2] * inner[i + 10] + |
| 202 | outer[j + 3] * inner[i + 15]; |
| 203 | } |
| 204 | m[index++] = outer[j + 0] * inner[4] + |
| 205 | outer[j + 1] * inner[9] + |
| 206 | outer[j + 2] * inner[14] + |
| 207 | outer[j + 3] * inner[19] + |
| 208 | outer[j + 4]; |
| 209 | } |
| 210 | |
| 211 | return m; |
| 212 | } |
| 213 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 214 | CanvasKit.SkPath.prototype.addArc = function(oval, startAngle, sweepAngle) { |
| 215 | // see arc() for the HTMLCanvas version |
| 216 | // note input angles are degrees. |
| 217 | this._addArc(oval, startAngle, sweepAngle); |
| 218 | return this; |
| 219 | }; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 220 | |
Kevin Lubick | e384df4 | 2019-08-26 15:48:09 -0400 | [diff] [blame] | 221 | CanvasKit.SkPath.prototype.addOval = function(oval, isCCW, startIndex) { |
| 222 | if (startIndex === undefined) { |
| 223 | startIndex = 1; |
| 224 | } |
| 225 | this._addOval(oval, !!isCCW, startIndex); |
| 226 | return this; |
| 227 | }; |
| 228 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 229 | CanvasKit.SkPath.prototype.addPath = function() { |
| 230 | // Takes 1, 2, 7, or 10 required args, where the first arg is always the path. |
| 231 | // The last arg is optional and chooses between add or extend mode. |
| 232 | // The options for the remaining args are: |
| 233 | // - an array of 6 or 9 parameters (perspective is optional) |
| 234 | // - the 9 parameters of a full matrix or |
| 235 | // the 6 non-perspective params of a matrix. |
| 236 | var args = Array.prototype.slice.call(arguments); |
| 237 | var path = args[0]; |
| 238 | var extend = false; |
| 239 | if (typeof args[args.length-1] === "boolean") { |
| 240 | extend = args.pop(); |
| 241 | } |
| 242 | if (args.length === 1) { |
| 243 | // Add path, unchanged. Use identity matrix |
| 244 | this._addPath(path, 1, 0, 0, |
| 245 | 0, 1, 0, |
| 246 | 0, 0, 1, |
| 247 | extend); |
| 248 | } else if (args.length === 2) { |
| 249 | // User provided the 9 params of a full matrix as an array. |
| 250 | var a = args[1]; |
| 251 | this._addPath(path, a[0], a[1], a[2], |
| 252 | a[3], a[4], a[5], |
| 253 | a[6] || 0, a[7] || 0, a[8] || 1, |
| 254 | extend); |
| 255 | } else if (args.length === 7 || args.length === 10) { |
| 256 | // User provided the 9 params of a (full) matrix directly. |
| 257 | // (or just the 6 non perspective ones) |
| 258 | // These are in the same order as what Skia expects. |
| 259 | var a = args; |
| 260 | this._addPath(path, a[1], a[2], a[3], |
| 261 | a[4], a[5], a[6], |
| 262 | a[7] || 0, a[8] || 0, a[9] || 1, |
| 263 | extend); |
| 264 | } else { |
| 265 | SkDebug('addPath expected to take 1, 2, 7, or 10 required args. Got ' + args.length); |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 266 | return null; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 267 | } |
| 268 | return this; |
| 269 | }; |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 270 | |
Kevin Lubick | 37ab53e | 2019-11-11 10:06:08 -0500 | [diff] [blame] | 271 | // points is either an array of [x, y] where x and y are numbers or |
| 272 | // a typed array from Malloc where the even indices will be treated |
| 273 | // as x coordinates and the odd indices will be treated as y coordinates. |
| 274 | CanvasKit.SkPath.prototype.addPoly = function(points, close) { |
| 275 | var ptr; |
| 276 | var n; |
| 277 | // This was created with CanvasKit.Malloc, so assume the user has |
| 278 | // already been filled with data. |
| 279 | if (points['_ck']) { |
| 280 | ptr = points.byteOffset; |
| 281 | n = points.length/2; |
| 282 | } else { |
| 283 | ptr = copy2dArray(points, CanvasKit.HEAPF32); |
| 284 | n = points.length; |
| 285 | } |
| 286 | this._addPoly(ptr, n, close); |
| 287 | CanvasKit._free(ptr); |
| 288 | return this; |
| 289 | }; |
| 290 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 291 | CanvasKit.SkPath.prototype.addRect = function() { |
| 292 | // Takes 1, 2, 4 or 5 args |
| 293 | // - SkRect |
| 294 | // - SkRect, isCCW |
| 295 | // - left, top, right, bottom |
| 296 | // - left, top, right, bottom, isCCW |
| 297 | if (arguments.length === 1 || arguments.length === 2) { |
| 298 | var r = arguments[0]; |
| 299 | var ccw = arguments[1] || false; |
| 300 | this._addRect(r.fLeft, r.fTop, r.fRight, r.fBottom, ccw); |
| 301 | } else if (arguments.length === 4 || arguments.length === 5) { |
| 302 | var a = arguments; |
| 303 | this._addRect(a[0], a[1], a[2], a[3], a[4] || false); |
| 304 | } else { |
| 305 | SkDebug('addRect expected to take 1, 2, 4, or 5 args. Got ' + arguments.length); |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 306 | return null; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 307 | } |
| 308 | return this; |
| 309 | }; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 310 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 311 | CanvasKit.SkPath.prototype.addRoundRect = function() { |
| 312 | // Takes 3, 4, 6 or 7 args |
| 313 | // - SkRect, radii, ccw |
| 314 | // - SkRect, rx, ry, ccw |
| 315 | // - left, top, right, bottom, radii, ccw |
| 316 | // - left, top, right, bottom, rx, ry, ccw |
| 317 | var args = arguments; |
| 318 | if (args.length === 3 || args.length === 6) { |
| 319 | var radii = args[args.length-2]; |
| 320 | } else if (args.length === 6 || args.length === 7){ |
| 321 | // duplicate the given (rx, ry) pairs for each corner. |
| 322 | var rx = args[args.length-3]; |
| 323 | var ry = args[args.length-2]; |
| 324 | var radii = [rx, ry, rx, ry, rx, ry, rx, ry]; |
| 325 | } else { |
| 326 | SkDebug('addRoundRect expected to take 3, 4, 6, or 7 args. Got ' + args.length); |
| 327 | return null; |
| 328 | } |
| 329 | if (radii.length !== 8) { |
| 330 | SkDebug('addRoundRect needs 8 radii provided. Got ' + radii.length); |
| 331 | return null; |
| 332 | } |
| 333 | var rptr = copy1dArray(radii, CanvasKit.HEAPF32); |
| 334 | if (args.length === 3 || args.length === 4) { |
| 335 | var r = args[0]; |
| 336 | var ccw = args[args.length - 1]; |
| 337 | this._addRoundRect(r.fLeft, r.fTop, r.fRight, r.fBottom, rptr, ccw); |
| 338 | } else if (args.length === 6 || args.length === 7) { |
| 339 | var a = args; |
| 340 | this._addRoundRect(a[0], a[1], a[2], a[3], rptr, ccw); |
| 341 | } |
| 342 | CanvasKit._free(rptr); |
| 343 | return this; |
| 344 | }; |
| 345 | |
| 346 | CanvasKit.SkPath.prototype.arc = function(x, y, radius, startAngle, endAngle, ccw) { |
| 347 | // emulates the HTMLCanvas behavior. See addArc() for the SkPath version. |
| 348 | // Note input angles are radians. |
| 349 | var bounds = CanvasKit.LTRBRect(x-radius, y-radius, x+radius, y+radius); |
| 350 | var sweep = radiansToDegrees(endAngle - startAngle) - (360 * !!ccw); |
| 351 | var temp = new CanvasKit.SkPath(); |
| 352 | temp.addArc(bounds, radiansToDegrees(startAngle), sweep); |
| 353 | this.addPath(temp, true); |
| 354 | temp.delete(); |
| 355 | return this; |
| 356 | }; |
| 357 | |
| 358 | CanvasKit.SkPath.prototype.arcTo = function() { |
| 359 | // takes 4, 5 or 7 args |
| 360 | // - 5 x1, y1, x2, y2, radius |
| 361 | // - 4 oval (as Rect), startAngle, sweepAngle, forceMoveTo |
Kevin Lubick | e384df4 | 2019-08-26 15:48:09 -0400 | [diff] [blame] | 362 | // - 7 rx, ry, xAxisRotate, useSmallArc, isCCW, x, y |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 363 | var args = arguments; |
| 364 | if (args.length === 5) { |
| 365 | this._arcTo(args[0], args[1], args[2], args[3], args[4]); |
| 366 | } else if (args.length === 4) { |
| 367 | this._arcTo(args[0], args[1], args[2], args[3]); |
| 368 | } else if (args.length === 7) { |
Kevin Lubick | e384df4 | 2019-08-26 15:48:09 -0400 | [diff] [blame] | 369 | this._arcTo(args[0], args[1], args[2], !!args[3], !!args[4], args[5], args[6]); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 370 | } else { |
| 371 | throw 'Invalid args for arcTo. Expected 4, 5, or 7, got '+ args.length; |
| 372 | } |
| 373 | |
| 374 | return this; |
| 375 | }; |
| 376 | |
| 377 | CanvasKit.SkPath.prototype.close = function() { |
| 378 | this._close(); |
| 379 | return this; |
| 380 | }; |
| 381 | |
| 382 | CanvasKit.SkPath.prototype.conicTo = function(x1, y1, x2, y2, w) { |
| 383 | this._conicTo(x1, y1, x2, y2, w); |
| 384 | return this; |
| 385 | }; |
| 386 | |
| 387 | CanvasKit.SkPath.prototype.cubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) { |
| 388 | this._cubicTo(cp1x, cp1y, cp2x, cp2y, x, y); |
| 389 | return this; |
| 390 | }; |
| 391 | |
| 392 | CanvasKit.SkPath.prototype.dash = function(on, off, phase) { |
| 393 | if (this._dash(on, off, phase)) { |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 394 | return this; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 395 | } |
| 396 | return null; |
| 397 | }; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 398 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 399 | CanvasKit.SkPath.prototype.lineTo = function(x, y) { |
| 400 | this._lineTo(x, y); |
| 401 | return this; |
| 402 | }; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 403 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 404 | CanvasKit.SkPath.prototype.moveTo = function(x, y) { |
| 405 | this._moveTo(x, y); |
| 406 | return this; |
| 407 | }; |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 408 | |
Kevin Lubick | e384df4 | 2019-08-26 15:48:09 -0400 | [diff] [blame] | 409 | CanvasKit.SkPath.prototype.offset = function(dx, dy) { |
| 410 | this._transform(1, 0, dx, |
| 411 | 0, 1, dy, |
| 412 | 0, 0, 1); |
| 413 | return this; |
| 414 | }; |
| 415 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 416 | CanvasKit.SkPath.prototype.quadTo = function(cpx, cpy, x, y) { |
| 417 | this._quadTo(cpx, cpy, x, y); |
| 418 | return this; |
| 419 | }; |
| 420 | |
Kevin Lubick | 79b7134 | 2019-11-01 14:36:52 -0400 | [diff] [blame] | 421 | CanvasKit.SkPath.prototype.rArcTo = function(rx, ry, xAxisRotate, useSmallArc, isCCW, dx, dy) { |
| 422 | this._rArcTo(rx, ry, xAxisRotate, useSmallArc, isCCW, dx, dy); |
| 423 | return this; |
| 424 | }; |
| 425 | |
| 426 | CanvasKit.SkPath.prototype.rConicTo = function(dx1, dy1, dx2, dy2, w) { |
| 427 | this._rConicTo(dx1, dy1, dx2, dy2, w); |
| 428 | return this; |
| 429 | }; |
| 430 | |
| 431 | // These params are all relative |
| 432 | CanvasKit.SkPath.prototype.rCubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) { |
| 433 | this._rCubicTo(cp1x, cp1y, cp2x, cp2y, x, y); |
| 434 | return this; |
| 435 | }; |
| 436 | |
| 437 | CanvasKit.SkPath.prototype.rLineTo = function(dx, dy) { |
| 438 | this._rLineTo(dx, dy); |
| 439 | return this; |
| 440 | }; |
| 441 | |
| 442 | CanvasKit.SkPath.prototype.rMoveTo = function(dx, dy) { |
| 443 | this._rMoveTo(dx, dy); |
| 444 | return this; |
| 445 | }; |
| 446 | |
| 447 | // These params are all relative |
| 448 | CanvasKit.SkPath.prototype.rQuadTo = function(cpx, cpy, x, y) { |
| 449 | this._rQuadTo(cpx, cpy, x, y); |
| 450 | return this; |
| 451 | }; |
| 452 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 453 | CanvasKit.SkPath.prototype.stroke = function(opts) { |
| 454 | // Fill out any missing values with the default values. |
| 455 | /** |
| 456 | * See externs.js for this definition |
| 457 | * @type {StrokeOpts} |
| 458 | */ |
| 459 | opts = opts || {}; |
| 460 | opts.width = opts.width || 1; |
| 461 | opts.miter_limit = opts.miter_limit || 4; |
| 462 | opts.cap = opts.cap || CanvasKit.StrokeCap.Butt; |
| 463 | opts.join = opts.join || CanvasKit.StrokeJoin.Miter; |
| 464 | opts.precision = opts.precision || 1; |
| 465 | if (this._stroke(opts)) { |
| 466 | return this; |
| 467 | } |
| 468 | return null; |
| 469 | }; |
| 470 | |
| 471 | CanvasKit.SkPath.prototype.transform = function() { |
| 472 | // Takes 1 or 9 args |
| 473 | if (arguments.length === 1) { |
| 474 | // argument 1 should be a 6 or 9 element array. |
| 475 | var a = arguments[0]; |
| 476 | this._transform(a[0], a[1], a[2], |
| 477 | a[3], a[4], a[5], |
| 478 | a[6] || 0, a[7] || 0, a[8] || 1); |
| 479 | } else if (arguments.length === 6 || arguments.length === 9) { |
| 480 | // these arguments are the 6 or 9 members of the matrix |
| 481 | var a = arguments; |
| 482 | this._transform(a[0], a[1], a[2], |
| 483 | a[3], a[4], a[5], |
| 484 | a[6] || 0, a[7] || 0, a[8] || 1); |
| 485 | } else { |
| 486 | throw 'transform expected to take 1 or 9 arguments. Got ' + arguments.length; |
| 487 | } |
| 488 | return this; |
| 489 | }; |
| 490 | // isComplement is optional, defaults to false |
| 491 | CanvasKit.SkPath.prototype.trim = function(startT, stopT, isComplement) { |
| 492 | if (this._trim(startT, stopT, !!isComplement)) { |
| 493 | return this; |
| 494 | } |
| 495 | return null; |
| 496 | }; |
| 497 | |
| 498 | // bones should be a 3d array. |
| 499 | // Each bone is a 3x2 transformation matrix in column major order: |
| 500 | // | scaleX skewX transX | |
| 501 | // | skewY scaleY transY | |
| 502 | // and bones is an array of those matrices. |
| 503 | // Returns a copy of this (SkVertices) with the bones applied. |
| 504 | CanvasKit.SkVertices.prototype.applyBones = function(bones) { |
| 505 | var bPtr = copy3dArray(bones, CanvasKit.HEAPF32); |
| 506 | var vert = this._applyBones(bPtr, bones.length); |
| 507 | CanvasKit._free(bPtr); |
| 508 | return vert; |
| 509 | } |
| 510 | |
| 511 | CanvasKit.SkImage.prototype.encodeToData = function() { |
| 512 | if (!arguments.length) { |
| 513 | return this._encodeToData(); |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 514 | } |
Kevin Lubick | 53965c9 | 2018-10-11 08:51:55 -0400 | [diff] [blame] | 515 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 516 | if (arguments.length === 2) { |
| 517 | var a = arguments; |
| 518 | return this._encodeToDataWithFormat(a[0], a[1]); |
Alexander Khovansky | 3e11933 | 2018-11-15 02:01:19 +0300 | [diff] [blame] | 519 | } |
| 520 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 521 | throw 'encodeToData expected to take 0 or 2 arguments. Got ' + arguments.length; |
| 522 | } |
Kevin Lubick | 1ba9c4d | 2019-02-22 10:04:06 -0500 | [diff] [blame] | 523 | |
Kevin Lubick | a064c28 | 2019-04-04 09:28:53 -0400 | [diff] [blame] | 524 | CanvasKit.SkImage.prototype.makeShader = function(xTileMode, yTileMode, localMatrix) { |
| 525 | if (localMatrix) { |
| 526 | // Add perspective args if not provided. |
| 527 | if (localMatrix.length === 6) { |
| 528 | localMatrix.push(0, 0, 1); |
| 529 | } |
| 530 | return this._makeShader(xTileMode, yTileMode, localMatrix); |
| 531 | } else { |
| 532 | return this._makeShader(xTileMode, yTileMode); |
| 533 | } |
| 534 | } |
| 535 | |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 536 | CanvasKit.SkImage.prototype.readPixels = function(imageInfo, srcX, srcY) { |
| 537 | var rowBytes; |
| 538 | switch (imageInfo.colorType){ |
| 539 | case CanvasKit.ColorType.RGBA_8888: |
| 540 | rowBytes = imageInfo.width * 4; // 1 byte per channel == 4 bytes per pixel in 8888 |
| 541 | break; |
| 542 | case CanvasKit.ColorType.RGBA_F32: |
| 543 | rowBytes = imageInfo.width * 16; // 4 bytes per channel == 16 bytes per pixel in F32 |
| 544 | break; |
| 545 | default: |
| 546 | SkDebug("Colortype not yet supported"); |
| 547 | return; |
| 548 | } |
| 549 | var pBytes = rowBytes * imageInfo.height; |
| 550 | var pPtr = CanvasKit._malloc(pBytes); |
| 551 | |
| 552 | if (!this._readPixels(imageInfo, pPtr, rowBytes, srcX, srcY)) { |
| 553 | SkDebug("Could not read pixels with the given inputs"); |
| 554 | return null; |
| 555 | } |
| 556 | |
| 557 | // Put those pixels into a typed array of the right format and then |
| 558 | // make a copy with slice() that we can return. |
| 559 | var retVal = null; |
| 560 | switch (imageInfo.colorType){ |
| 561 | case CanvasKit.ColorType.RGBA_8888: |
Bryce Thomas | 1fa5404 | 2020-01-14 13:46:30 -0800 | [diff] [blame^] | 562 | retVal = new Uint8Array(CanvasKit.HEAPU8.buffer, pPtr, pBytes).slice(); |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 563 | break; |
| 564 | case CanvasKit.ColorType.RGBA_F32: |
Bryce Thomas | 1fa5404 | 2020-01-14 13:46:30 -0800 | [diff] [blame^] | 565 | retVal = new Float32Array(CanvasKit.HEAPU8.buffer, pPtr, pBytes).slice(); |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 566 | break; |
| 567 | } |
| 568 | |
| 569 | // Free the allocated pixels in the WASM memory |
| 570 | CanvasKit._free(pPtr); |
| 571 | return retVal; |
| 572 | |
| 573 | } |
| 574 | |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 575 | // atlas is an SkImage, e.g. from CanvasKit.MakeImageFromEncoded |
| 576 | // srcRects and dstXforms should be CanvasKit.SkRectBuilder and CanvasKit.RSXFormBuilder |
| 577 | // or just arrays of floats in groups of 4. |
| 578 | // colors, if provided, should be a CanvasKit.SkColorBuilder or array of SkColor |
| 579 | // (from CanvasKit.Color) |
| 580 | CanvasKit.SkCanvas.prototype.drawAtlas = function(atlas, srcRects, dstXforms, paint, |
| 581 | /*optional*/ blendMode, colors) { |
| 582 | if (!atlas || !paint || !srcRects || !dstXforms) { |
| 583 | SkDebug('Doing nothing since missing a required input'); |
| 584 | return; |
| 585 | } |
| 586 | if (srcRects.length !== dstXforms.length || (colors && colors.length !== dstXforms.length)) { |
| 587 | SkDebug('Doing nothing since input arrays length mismatches'); |
| 588 | } |
| 589 | if (!blendMode) { |
| 590 | blendMode = CanvasKit.BlendMode.SrcOver; |
| 591 | } |
| 592 | |
| 593 | var srcRectPtr; |
| 594 | if (srcRects.build) { |
| 595 | srcRectPtr = srcRects.build(); |
| 596 | } else { |
| 597 | srcRectPtr = copy1dArray(srcRects, CanvasKit.HEAPF32); |
| 598 | } |
| 599 | |
| 600 | var dstXformPtr; |
| 601 | if (dstXforms.build) { |
| 602 | dstXformPtr = dstXforms.build(); |
| 603 | } else { |
| 604 | dstXformPtr = copy1dArray(dstXforms, CanvasKit.HEAPF32); |
| 605 | } |
| 606 | |
| 607 | var colorPtr = 0; // enscriptem doesn't like undefined for nullptr |
| 608 | if (colors) { |
| 609 | if (colors.build) { |
| 610 | colorPtr = colors.build(); |
| 611 | } else { |
| 612 | colorPtr = copy1dArray(colors, CanvasKit.HEAPU32); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | this._drawAtlas(atlas, dstXformPtr, srcRectPtr, colorPtr, dstXforms.length, |
| 617 | blendMode, paint); |
| 618 | |
| 619 | if (srcRectPtr && !srcRects.build) { |
| 620 | CanvasKit._free(srcRectPtr); |
| 621 | } |
| 622 | if (dstXformPtr && !dstXforms.build) { |
| 623 | CanvasKit._free(dstXformPtr); |
| 624 | } |
| 625 | if (colorPtr && !colors.build) { |
| 626 | CanvasKit._free(colorPtr); |
| 627 | } |
| 628 | |
| 629 | } |
| 630 | |
Kevin Lubick | 37ab53e | 2019-11-11 10:06:08 -0500 | [diff] [blame] | 631 | // points is either an array of [x, y] where x and y are numbers or |
| 632 | // a typed array from Malloc where the even indices will be treated |
| 633 | // as x coordinates and the odd indices will be treated as y coordinates. |
| 634 | CanvasKit.SkCanvas.prototype.drawPoints = function(mode, points, paint) { |
| 635 | var ptr; |
| 636 | var n; |
| 637 | // This was created with CanvasKit.Malloc, so assume the user has |
| 638 | // already been filled with data. |
| 639 | if (points['_ck']) { |
| 640 | ptr = points.byteOffset; |
| 641 | n = points.length/2; |
| 642 | } else { |
| 643 | ptr = copy2dArray(points, CanvasKit.HEAPF32); |
| 644 | n = points.length; |
| 645 | } |
| 646 | this._drawPoints(mode, ptr, n, paint); |
| 647 | CanvasKit._free(ptr); |
| 648 | } |
| 649 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 650 | // returns Uint8Array |
| 651 | CanvasKit.SkCanvas.prototype.readPixels = function(x, y, w, h, alphaType, |
| 652 | colorType, dstRowBytes) { |
| 653 | // supply defaults (which are compatible with HTMLCanvas's getImageData) |
| 654 | alphaType = alphaType || CanvasKit.AlphaType.Unpremul; |
| 655 | colorType = colorType || CanvasKit.ColorType.RGBA_8888; |
| 656 | dstRowBytes = dstRowBytes || (4 * w); |
| 657 | |
| 658 | var len = h * dstRowBytes |
| 659 | var pptr = CanvasKit._malloc(len); |
| 660 | var ok = this._readPixels({ |
| 661 | 'width': w, |
| 662 | 'height': h, |
Kevin Lubick | 52b9f37 | 2018-12-04 13:57:36 -0500 | [diff] [blame] | 663 | 'colorType': colorType, |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 664 | 'alphaType': alphaType, |
| 665 | }, pptr, dstRowBytes, x, y); |
| 666 | if (!ok) { |
| 667 | CanvasKit._free(pptr); |
| 668 | return null; |
| 669 | } |
| 670 | |
| 671 | // The first typed array is just a view into memory. Because we will |
| 672 | // be free-ing that, we call slice to make a persistent copy. |
Bryce Thomas | 1fa5404 | 2020-01-14 13:46:30 -0800 | [diff] [blame^] | 673 | var pixels = new Uint8Array(CanvasKit.HEAPU8.buffer, pptr, len).slice(); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 674 | CanvasKit._free(pptr); |
| 675 | return pixels; |
| 676 | } |
| 677 | |
| 678 | // pixels is a TypedArray. No matter the input size, it will be treated as |
| 679 | // a Uint8Array (essentially, a byte array). |
| 680 | CanvasKit.SkCanvas.prototype.writePixels = function(pixels, srcWidth, srcHeight, |
| 681 | destX, destY, alphaType, colorType) { |
| 682 | if (pixels.byteLength % (srcWidth * srcHeight)) { |
| 683 | throw 'pixels length must be a multiple of the srcWidth * srcHeight'; |
| 684 | } |
| 685 | var bytesPerPixel = pixels.byteLength / (srcWidth * srcHeight); |
| 686 | // supply defaults (which are compatible with HTMLCanvas's putImageData) |
| 687 | alphaType = alphaType || CanvasKit.AlphaType.Unpremul; |
| 688 | colorType = colorType || CanvasKit.ColorType.RGBA_8888; |
| 689 | var srcRowBytes = bytesPerPixel * srcWidth; |
| 690 | |
Kevin Lubick | 52b9f37 | 2018-12-04 13:57:36 -0500 | [diff] [blame] | 691 | var pptr = CanvasKit._malloc(pixels.byteLength); |
| 692 | CanvasKit.HEAPU8.set(pixels, pptr); |
Kevin Lubick | 52b9f37 | 2018-12-04 13:57:36 -0500 | [diff] [blame] | 693 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 694 | var ok = this._writePixels({ |
| 695 | 'width': srcWidth, |
| 696 | 'height': srcHeight, |
| 697 | 'colorType': colorType, |
| 698 | 'alphaType': alphaType, |
| 699 | }, pptr, srcRowBytes, destX, destY); |
| 700 | |
| 701 | CanvasKit._free(pptr); |
| 702 | return ok; |
Kevin Lubick | 52b9f37 | 2018-12-04 13:57:36 -0500 | [diff] [blame] | 703 | } |
| 704 | |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 705 | // colorMatrix is an SkColorMatrix (e.g. Float32Array of length 20) |
| 706 | CanvasKit.SkColorFilter.MakeMatrix = function(colorMatrix) { |
| 707 | if (!colorMatrix || colorMatrix.length !== 20) { |
| 708 | SkDebug('ignoring invalid color matrix'); |
| 709 | return; |
| 710 | } |
| 711 | var fptr = copy1dArray(colorMatrix, CanvasKit.HEAPF32); |
| 712 | // We know skia memcopies the floats, so we can free our memory after the call returns. |
| 713 | var m = CanvasKit.SkColorFilter._makeMatrix(fptr); |
| 714 | CanvasKit._free(fptr); |
| 715 | return m; |
| 716 | } |
| 717 | |
Kevin Lubick | 6283690 | 2019-12-09 09:04:26 -0500 | [diff] [blame] | 718 | CanvasKit.SkShader.Blend = function(mode, dst, src, localMatrix) { |
| 719 | if (!localMatrix) { |
| 720 | return this._Blend(mode, dst, src); |
| 721 | } |
| 722 | return this._Blend(mode, dst, src, localMatrix); |
| 723 | } |
| 724 | |
| 725 | CanvasKit.SkShader.Lerp = function(t, dst, src, localMatrix) { |
| 726 | if (!localMatrix) { |
| 727 | return this._Lerp(t, dst, src); |
| 728 | } |
| 729 | return this._Lerp(t, dst, src, localMatrix); |
| 730 | } |
| 731 | |
Kevin Lubick | cc13fd3 | 2019-04-05 13:00:01 -0400 | [diff] [blame] | 732 | CanvasKit.SkSurface.prototype.captureFrameAsSkPicture = function(drawFrame) { |
| 733 | // Set up SkPictureRecorder |
| 734 | var spr = new CanvasKit.SkPictureRecorder(); |
| 735 | var canvas = spr.beginRecording( |
| 736 | CanvasKit.LTRBRect(0, 0, this.width(), this.height())); |
| 737 | drawFrame(canvas); |
| 738 | var pic = spr.finishRecordingAsPicture(); |
| 739 | spr.delete(); |
| 740 | // TODO: do we need to clean up the memory for canvas? |
| 741 | // If we delete it here, saveAsFile doesn't work correctly. |
| 742 | return pic; |
| 743 | } |
| 744 | |
Kevin Lubick | 359a7e3 | 2019-03-19 09:34:37 -0400 | [diff] [blame] | 745 | CanvasKit.SkSurface.prototype.requestAnimationFrame = function(callback, dirtyRect) { |
| 746 | if (!this._cached_canvas) { |
| 747 | this._cached_canvas = this.getCanvas(); |
| 748 | } |
| 749 | window.requestAnimationFrame(function() { |
Kevin Lubick | 3902628 | 2019-03-28 12:46:40 -0400 | [diff] [blame] | 750 | if (this._context !== undefined) { |
| 751 | CanvasKit.setCurrentContext(this._context); |
| 752 | } |
Kevin Lubick | 359a7e3 | 2019-03-19 09:34:37 -0400 | [diff] [blame] | 753 | |
| 754 | callback(this._cached_canvas); |
| 755 | |
| 756 | this.flush(); |
| 757 | }.bind(this)); |
| 758 | } |
| 759 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 760 | // Run through the JS files that are added at compile time. |
| 761 | if (CanvasKit._extraInitializations) { |
| 762 | CanvasKit._extraInitializations.forEach(function(init) { |
| 763 | init(); |
| 764 | }); |
Kevin Lubick | eb2f6b0 | 2018-11-29 15:07:02 -0500 | [diff] [blame] | 765 | } |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 766 | }; // end CanvasKit.onRuntimeInitialized, that is, anything changing prototypes or dynamic. |
Kevin Lubick | eb2f6b0 | 2018-11-29 15:07:02 -0500 | [diff] [blame] | 767 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 768 | CanvasKit.LTRBRect = function(l, t, r, b) { |
| 769 | return { |
| 770 | fLeft: l, |
| 771 | fTop: t, |
| 772 | fRight: r, |
| 773 | fBottom: b, |
| 774 | }; |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 775 | } |
| 776 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 777 | CanvasKit.XYWHRect = function(x, y, w, h) { |
| 778 | return { |
| 779 | fLeft: x, |
| 780 | fTop: y, |
| 781 | fRight: x+w, |
| 782 | fBottom: y+h, |
| 783 | }; |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 784 | } |
| 785 | |
Kevin Lubick | 7d644e1 | 2019-09-11 14:22:22 -0400 | [diff] [blame] | 786 | // RRectXY returns an RRect with the given rect and a radiusX and radiusY for |
| 787 | // all 4 corners. |
| 788 | CanvasKit.RRectXY = function(rect, rx, ry) { |
| 789 | return { |
| 790 | rect: rect, |
| 791 | rx1: rx, |
| 792 | ry1: ry, |
| 793 | rx2: rx, |
| 794 | ry2: ry, |
| 795 | rx3: rx, |
| 796 | ry3: ry, |
| 797 | rx4: rx, |
| 798 | ry4: ry, |
| 799 | }; |
| 800 | } |
| 801 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 802 | CanvasKit.MakePathFromCmds = function(cmds) { |
| 803 | var ptrLen = loadCmdsTypedArray(cmds); |
| 804 | var path = CanvasKit._MakePathFromCmds(ptrLen[0], ptrLen[1]); |
| 805 | CanvasKit._free(ptrLen[0]); |
| 806 | return path; |
| 807 | } |
| 808 | |
| 809 | CanvasKit.MakeSkDashPathEffect = function(intervals, phase) { |
| 810 | if (!phase) { |
| 811 | phase = 0; |
| 812 | } |
| 813 | if (!intervals.length || intervals.length % 2 === 1) { |
| 814 | throw 'Intervals array must have even length'; |
| 815 | } |
| 816 | var ptr = copy1dArray(intervals, CanvasKit.HEAPF32); |
| 817 | var dpe = CanvasKit._MakeSkDashPathEffect(ptr, intervals.length, phase); |
| 818 | CanvasKit._free(ptr); |
| 819 | return dpe; |
| 820 | } |
| 821 | |
| 822 | // data is a TypedArray or ArrayBuffer e.g. from fetch().then(resp.arrayBuffer()) |
Kevin Lubick | 6b921b7 | 2019-09-18 16:18:17 -0400 | [diff] [blame] | 823 | CanvasKit.MakeAnimatedImageFromEncoded = function(data) { |
| 824 | data = new Uint8Array(data); |
| 825 | |
| 826 | var iptr = CanvasKit._malloc(data.byteLength); |
| 827 | CanvasKit.HEAPU8.set(data, iptr); |
| 828 | var img = CanvasKit._decodeAnimatedImage(iptr, data.byteLength); |
| 829 | if (!img) { |
| 830 | SkDebug('Could not decode animated image'); |
| 831 | return null; |
| 832 | } |
| 833 | return img; |
| 834 | } |
| 835 | |
| 836 | // data is a TypedArray or ArrayBuffer e.g. from fetch().then(resp.arrayBuffer()) |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 837 | CanvasKit.MakeImageFromEncoded = function(data) { |
| 838 | data = new Uint8Array(data); |
| 839 | |
| 840 | var iptr = CanvasKit._malloc(data.byteLength); |
| 841 | CanvasKit.HEAPU8.set(data, iptr); |
| 842 | var img = CanvasKit._decodeImage(iptr, data.byteLength); |
| 843 | if (!img) { |
| 844 | SkDebug('Could not decode image'); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 845 | return null; |
| 846 | } |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 847 | return img; |
| 848 | } |
| 849 | |
Kevin Lubick | eda0b43 | 2019-12-02 08:26:48 -0500 | [diff] [blame] | 850 | // pixels must be a Uint8Array with bytes representing the pixel values |
| 851 | // (e.g. each set of 4 bytes could represent RGBA values for a single pixel). |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 852 | CanvasKit.MakeImage = function(pixels, width, height, alphaType, colorType) { |
Kevin Lubick | eda0b43 | 2019-12-02 08:26:48 -0500 | [diff] [blame] | 853 | var bytesPerPixel = pixels.length / (width * height); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 854 | var info = { |
| 855 | 'width': width, |
| 856 | 'height': height, |
| 857 | 'alphaType': alphaType, |
| 858 | 'colorType': colorType, |
| 859 | }; |
Kevin Lubick | eda0b43 | 2019-12-02 08:26:48 -0500 | [diff] [blame] | 860 | var pptr = copy1dArray(pixels, CanvasKit.HEAPU8); |
| 861 | // No need to _free pptr, Image takes it with SkData::MakeFromMalloc |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 862 | |
Kevin Lubick | eda0b43 | 2019-12-02 08:26:48 -0500 | [diff] [blame] | 863 | return CanvasKit._MakeImage(info, pptr, pixels.length, width * bytesPerPixel); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | CanvasKit.MakeLinearGradientShader = function(start, end, colors, pos, mode, localMatrix, flags) { |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 867 | var colorPtr = copy1dArray(colors, CanvasKit.HEAPU32); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 868 | var posPtr = copy1dArray(pos, CanvasKit.HEAPF32); |
| 869 | flags = flags || 0; |
| 870 | |
| 871 | if (localMatrix) { |
| 872 | // Add perspective args if not provided. |
| 873 | if (localMatrix.length === 6) { |
| 874 | localMatrix.push(0, 0, 1); |
| 875 | } |
| 876 | var lgs = CanvasKit._MakeLinearGradientShader(start, end, colorPtr, posPtr, |
| 877 | colors.length, mode, flags, localMatrix); |
| 878 | } else { |
| 879 | var lgs = CanvasKit._MakeLinearGradientShader(start, end, colorPtr, posPtr, |
| 880 | colors.length, mode, flags); |
| 881 | } |
| 882 | |
| 883 | CanvasKit._free(colorPtr); |
| 884 | CanvasKit._free(posPtr); |
| 885 | return lgs; |
| 886 | } |
| 887 | |
| 888 | CanvasKit.MakeRadialGradientShader = function(center, radius, colors, pos, mode, localMatrix, flags) { |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 889 | var colorPtr = copy1dArray(colors, CanvasKit.HEAPU32); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 890 | var posPtr = copy1dArray(pos, CanvasKit.HEAPF32); |
| 891 | flags = flags || 0; |
| 892 | |
| 893 | if (localMatrix) { |
| 894 | // Add perspective args if not provided. |
| 895 | if (localMatrix.length === 6) { |
| 896 | localMatrix.push(0, 0, 1); |
| 897 | } |
| 898 | var rgs = CanvasKit._MakeRadialGradientShader(center, radius, colorPtr, posPtr, |
| 899 | colors.length, mode, flags, localMatrix); |
| 900 | } else { |
| 901 | var rgs = CanvasKit._MakeRadialGradientShader(center, radius, colorPtr, posPtr, |
| 902 | colors.length, mode, flags); |
| 903 | } |
| 904 | |
| 905 | CanvasKit._free(colorPtr); |
| 906 | CanvasKit._free(posPtr); |
| 907 | return rgs; |
| 908 | } |
| 909 | |
| 910 | CanvasKit.MakeTwoPointConicalGradientShader = function(start, startRadius, end, endRadius, |
| 911 | colors, pos, mode, localMatrix, flags) { |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 912 | var colorPtr = copy1dArray(colors, CanvasKit.HEAPU32); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 913 | var posPtr = copy1dArray(pos, CanvasKit.HEAPF32); |
| 914 | flags = flags || 0; |
| 915 | |
| 916 | if (localMatrix) { |
| 917 | // Add perspective args if not provided. |
| 918 | if (localMatrix.length === 6) { |
| 919 | localMatrix.push(0, 0, 1); |
| 920 | } |
| 921 | var rgs = CanvasKit._MakeTwoPointConicalGradientShader( |
| 922 | start, startRadius, end, endRadius, |
| 923 | colorPtr, posPtr, colors.length, mode, flags, localMatrix); |
| 924 | } else { |
| 925 | var rgs = CanvasKit._MakeTwoPointConicalGradientShader( |
| 926 | start, startRadius, end, endRadius, |
| 927 | colorPtr, posPtr, colors.length, mode, flags); |
| 928 | } |
| 929 | |
| 930 | CanvasKit._free(colorPtr); |
| 931 | CanvasKit._free(posPtr); |
| 932 | return rgs; |
| 933 | } |
| 934 | |
| 935 | CanvasKit.MakeSkVertices = function(mode, positions, textureCoordinates, colors, |
Kevin Lubick | b3574c9 | 2019-03-06 08:25:36 -0500 | [diff] [blame] | 936 | boneIndices, boneWeights, indices, isVolatile) { |
Kevin Lubick | b3574c9 | 2019-03-06 08:25:36 -0500 | [diff] [blame] | 937 | // Default isVolitile to true if not set |
| 938 | isVolatile = isVolatile === undefined ? true : isVolatile; |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 939 | var idxCount = (indices && indices.length) || 0; |
| 940 | |
| 941 | var flags = 0; |
| 942 | // These flags are from SkVertices.h and should be kept in sync with those. |
| 943 | if (textureCoordinates && textureCoordinates.length) { |
| 944 | flags |= (1 << 0); |
| 945 | } |
| 946 | if (colors && colors.length) { |
| 947 | flags |= (1 << 1); |
| 948 | } |
| 949 | if (boneIndices && boneIndices.length) { |
| 950 | flags |= (1 << 2); |
| 951 | } |
| 952 | if (!isVolatile) { |
| 953 | flags |= (1 << 3); |
| 954 | } |
| 955 | |
| 956 | var builder = new CanvasKit._SkVerticesBuilder(mode, positions.length, idxCount, flags); |
| 957 | |
| 958 | copy2dArray(positions, CanvasKit.HEAPF32, builder.positions()); |
| 959 | if (builder.texCoords()) { |
| 960 | copy2dArray(textureCoordinates, CanvasKit.HEAPF32, builder.texCoords()); |
| 961 | } |
| 962 | if (builder.colors()) { |
| 963 | copy1dArray(colors, CanvasKit.HEAPU32, builder.colors()); |
| 964 | } |
| 965 | if (builder.boneIndices()) { |
| 966 | copy2dArray(boneIndices, CanvasKit.HEAP32, builder.boneIndices()); |
| 967 | } |
| 968 | if (builder.boneWeights()) { |
| 969 | copy2dArray(boneWeights, CanvasKit.HEAPF32, builder.boneWeights()); |
| 970 | } |
| 971 | if (builder.indices()) { |
| 972 | copy1dArray(indices, CanvasKit.HEAPU16, builder.indices()); |
| 973 | } |
Kevin Lubick | b3574c9 | 2019-03-06 08:25:36 -0500 | [diff] [blame] | 974 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 975 | var idxCount = (indices && indices.length) || 0; |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 976 | // Create the vertices, which owns the memory that the builder had allocated. |
| 977 | return builder.detach(); |
Kevin Lubick | a4f218d | 2020-01-14 08:39:09 -0500 | [diff] [blame] | 978 | }; |