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 | 93f1a38 | 2020-06-02 16:15:23 -0400 | [diff] [blame] | 11 | _scratchColor = CanvasKit.Malloc(Float32Array, 4); // 4 color scalars. |
Kevin Lubick | b42660f | 2020-06-03 09:58:27 -0400 | [diff] [blame] | 12 | _scratchColorPtr = _scratchColor['byteOffset']; |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 13 | |
| 14 | _scratch4x4Matrix = CanvasKit.Malloc(Float32Array, 16); // 16 matrix scalars. |
Kevin Lubick | b42660f | 2020-06-03 09:58:27 -0400 | [diff] [blame] | 15 | _scratch4x4MatrixPtr = _scratch4x4Matrix['byteOffset']; |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 16 | |
| 17 | _scratch3x3Matrix = CanvasKit.Malloc(Float32Array, 9); // 9 matrix scalars. |
Kevin Lubick | b42660f | 2020-06-03 09:58:27 -0400 | [diff] [blame] | 18 | _scratch3x3MatrixPtr = _scratch3x3Matrix['byteOffset']; |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 19 | // Create single copies of all three supported color spaces |
| 20 | // These are sk_sp<SkColorSpace> |
| 21 | CanvasKit.SkColorSpace.SRGB = CanvasKit.SkColorSpace._MakeSRGB(); |
| 22 | CanvasKit.SkColorSpace.DISPLAY_P3 = CanvasKit.SkColorSpace._MakeDisplayP3(); |
| 23 | CanvasKit.SkColorSpace.ADOBE_RGB = CanvasKit.SkColorSpace._MakeAdobeRGB(); |
| 24 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 25 | // Add some helpers for matrices. This is ported from SkMatrix.cpp |
| 26 | // to save complexity and overhead of going back and forth between |
| 27 | // C++ and JS layers. |
| 28 | // I would have liked to use something like DOMMatrix, except it |
| 29 | // isn't widely supported (would need polyfills) and it doesn't |
| 30 | // have a mapPoints() function (which could maybe be tacked on here). |
| 31 | // If DOMMatrix catches on, it would be worth re-considering this usage. |
| 32 | CanvasKit.SkMatrix = {}; |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 33 | function sdot() { // to be called with an even number of scalar args |
| 34 | var acc = 0; |
| 35 | for (var i=0; i < arguments.length-1; i+=2) { |
| 36 | acc += arguments[i] * arguments[i+1]; |
| 37 | } |
| 38 | return acc; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | // Private general matrix functions used in both 3x3s and 4x4s. |
| 43 | // Return a square identity matrix of size n. |
| 44 | var identityN = function(n) { |
| 45 | var size = n*n; |
| 46 | var m = new Array(size); |
| 47 | while(size--) { |
| 48 | m[size] = size%(n+1) == 0 ? 1.0 : 0.0; |
| 49 | } |
| 50 | return m; |
| 51 | } |
| 52 | |
| 53 | // Stride, a function for compactly representing several ways of copying an array into another. |
| 54 | // Write vector `v` into matrix `m`. `m` is a matrix encoded as an array in row-major |
| 55 | // order. Its width is passed as `width`. `v` is an array with length < (m.length/width). |
| 56 | // An element of `v` is copied into `m` starting at `offset` and moving `colStride` cols right |
| 57 | // each row. |
| 58 | // |
| 59 | // For example, a width of 4, offset of 3, and stride of -1 would put the vector here. |
| 60 | // _ _ 0 _ |
| 61 | // _ 1 _ _ |
| 62 | // 2 _ _ _ |
| 63 | // _ _ _ 3 |
| 64 | // |
| 65 | var stride = function(v, m, width, offset, colStride) { |
| 66 | for (var i=0; i<v.length; i++) { |
| 67 | m[i * width + // column |
| 68 | (i * colStride + offset + width) % width // row |
| 69 | ] = v[i]; |
| 70 | } |
| 71 | return m; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | CanvasKit.SkMatrix.identity = function() { |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 75 | return identityN(3); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | // Return the inverse (if it exists) of this matrix. |
Kevin Lubick | 9b56cea | 2020-04-06 08:16:18 -0400 | [diff] [blame] | 79 | // Otherwise, return null. |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 80 | CanvasKit.SkMatrix.invert = function(m) { |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 81 | // Find the determinant by the sarrus rule. https://en.wikipedia.org/wiki/Rule_of_Sarrus |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 82 | var det = m[0]*m[4]*m[8] + m[1]*m[5]*m[6] + m[2]*m[3]*m[7] |
| 83 | - m[2]*m[4]*m[6] - m[1]*m[3]*m[8] - m[0]*m[5]*m[7]; |
| 84 | if (!det) { |
| 85 | SkDebug('Warning, uninvertible matrix'); |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 86 | return null; |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 87 | } |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 88 | // Return the inverse by the formula adj(m)/det. |
| 89 | // adj (adjugate) of a 3x3 is the transpose of it's cofactor matrix. |
| 90 | // a cofactor matrix is a matrix where each term is +-det(N) where matrix N is the 2x2 formed |
| 91 | // by removing the row and column we're currently setting from the source. |
| 92 | // the sign alternates in a checkerboard pattern with a `+` at the top left. |
| 93 | // that's all been combined here into one expression. |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 94 | return [ |
| 95 | (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, |
| 96 | (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, |
| 97 | (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, |
| 98 | ]; |
| 99 | }; |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 100 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 101 | // Maps the given points according to the passed in matrix. |
| 102 | // Results are done in place. |
| 103 | // See SkMatrix.h::mapPoints for the docs on the math. |
| 104 | CanvasKit.SkMatrix.mapPoints = function(matrix, ptArr) { |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 105 | if (skIsDebug && (ptArr.length % 2)) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 106 | throw 'mapPoints requires an even length arr'; |
Kevin Lubick | b9db390 | 2018-11-26 11:47:54 -0500 | [diff] [blame] | 107 | } |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 108 | for (var i = 0; i < ptArr.length; i+=2) { |
| 109 | var x = ptArr[i], y = ptArr[i+1]; |
| 110 | // Gx+Hy+I |
| 111 | var denom = matrix[6]*x + matrix[7]*y + matrix[8]; |
| 112 | // Ax+By+C |
| 113 | var xTrans = matrix[0]*x + matrix[1]*y + matrix[2]; |
| 114 | // Dx+Ey+F |
| 115 | var yTrans = matrix[3]*x + matrix[4]*y + matrix[5]; |
| 116 | ptArr[i] = xTrans/denom; |
| 117 | ptArr[i+1] = yTrans/denom; |
| 118 | } |
| 119 | return ptArr; |
| 120 | }; |
Kevin Lubick | b9db390 | 2018-11-26 11:47:54 -0500 | [diff] [blame] | 121 | |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 122 | function isnumber(val) { return val !== NaN; }; |
| 123 | |
Kevin Lubick | c89ca0b | 2020-04-02 14:30:00 -0400 | [diff] [blame] | 124 | // generalized iterative algorithm for multiplying two matrices. |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 125 | function multiply(m1, m2, size) { |
| 126 | |
| 127 | if (skIsDebug && (!m1.every(isnumber) || !m2.every(isnumber))) { |
| 128 | throw 'Some members of matrices are NaN m1='+m1+', m2='+m2+''; |
| 129 | } |
| 130 | if (skIsDebug && (m1.length !== m2.length)) { |
| 131 | throw 'Undefined for matrices of different sizes. m1.length='+m1.length+', m2.length='+m2.length; |
| 132 | } |
| 133 | if (skIsDebug && (size*size !== m1.length)) { |
| 134 | throw 'Undefined for non-square matrices. array size was '+size; |
| 135 | } |
| 136 | |
| 137 | var result = Array(m1.length); |
| 138 | for (var r = 0; r < size; r++) { |
| 139 | for (var c = 0; c < size; c++) { |
| 140 | // accumulate a sum of m1[r,k]*m2[k, c] |
| 141 | var acc = 0; |
| 142 | for (var k = 0; k < size; k++) { |
| 143 | acc += m1[size * r + k] * m2[size * k + c]; |
| 144 | } |
| 145 | result[r * size + c] = acc; |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 146 | } |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 147 | } |
| 148 | return result; |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | // Accept an integer indicating the size of the matrices being multiplied (3 for 3x3), and any |
| 152 | // number of matrices following it. |
| 153 | function multiplyMany(size, listOfMatrices) { |
| 154 | if (skIsDebug && (listOfMatrices.length < 2)) { |
| 155 | throw 'multiplication expected two or more matrices'; |
| 156 | } |
| 157 | var result = multiply(listOfMatrices[0], listOfMatrices[1], size); |
| 158 | var next = 2; |
| 159 | while (next < listOfMatrices.length) { |
| 160 | result = multiply(result, listOfMatrices[next], size); |
| 161 | next++; |
| 162 | } |
| 163 | return result; |
| 164 | }; |
| 165 | |
| 166 | // Accept any number 3x3 of matrices as arguments, multiply them together. |
Kevin Lubick | 9e2d384 | 2020-04-01 13:42:15 -0400 | [diff] [blame] | 167 | // Matrix multiplication is associative but not commutative. the order of the arguments |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 168 | // matters, but it does not matter that this implementation multiplies them left to right. |
| 169 | CanvasKit.SkMatrix.multiply = function() { |
| 170 | return multiplyMany(3, arguments); |
| 171 | }; |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 172 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 173 | // Return a matrix representing a rotation by n radians. |
| 174 | // px, py optionally say which point the rotation should be around |
| 175 | // with the default being (0, 0); |
| 176 | CanvasKit.SkMatrix.rotated = function(radians, px, py) { |
| 177 | px = px || 0; |
| 178 | py = py || 0; |
| 179 | var sinV = Math.sin(radians); |
| 180 | var cosV = Math.cos(radians); |
| 181 | return [ |
| 182 | cosV, -sinV, sdot( sinV, py, 1 - cosV, px), |
| 183 | sinV, cosV, sdot(-sinV, px, 1 - cosV, py), |
| 184 | 0, 0, 1, |
| 185 | ]; |
| 186 | }; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 187 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 188 | CanvasKit.SkMatrix.scaled = function(sx, sy, px, py) { |
| 189 | px = px || 0; |
| 190 | py = py || 0; |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 191 | var m = stride([sx, sy], identityN(3), 3, 0, 1); |
| 192 | return stride([px-sx*px, py-sy*py], m, 3, 2, 0); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 193 | }; |
Kevin Lubick | da3d8ac | 2019-01-07 11:08:55 -0500 | [diff] [blame] | 194 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 195 | CanvasKit.SkMatrix.skewed = function(kx, ky, px, py) { |
| 196 | px = px || 0; |
| 197 | py = py || 0; |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 198 | var m = stride([kx, ky], identityN(3), 3, 1, -1); |
| 199 | return stride([-kx*px, -ky*py], m, 3, 2, 0); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 200 | }; |
Alexander Khovansky | 3e11933 | 2018-11-15 02:01:19 +0300 | [diff] [blame] | 201 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 202 | CanvasKit.SkMatrix.translated = function(dx, dy) { |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 203 | return stride(arguments, identityN(3), 3, 2, 0); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 204 | }; |
Kevin Lubick | 1646e7d | 2018-12-07 13:03:08 -0500 | [diff] [blame] | 205 | |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 206 | // Functions for manipulating vectors. |
| 207 | // Loosely based off of SkV3 in SkM44.h but skia also has SkVec2 and Skv4. This combines them and |
| 208 | // works on vectors of any length. |
| 209 | CanvasKit.SkVector = {}; |
| 210 | CanvasKit.SkVector.dot = function(a, b) { |
| 211 | if (skIsDebug && (a.length !== b.length)) { |
| 212 | throw 'Cannot perform dot product on arrays of different length ('+a.length+' vs '+b.length+')'; |
| 213 | } |
| 214 | return a.map(function(v, i) { return v*b[i] }).reduce(function(acc, cur) { return acc + cur; }); |
| 215 | } |
| 216 | CanvasKit.SkVector.lengthSquared = function(v) { |
| 217 | return CanvasKit.SkVector.dot(v, v); |
| 218 | } |
| 219 | CanvasKit.SkVector.length = function(v) { |
| 220 | return Math.sqrt(CanvasKit.SkVector.lengthSquared(v)); |
| 221 | } |
| 222 | CanvasKit.SkVector.mulScalar = function(v, s) { |
| 223 | return v.map(function(i) { return i*s }); |
| 224 | } |
| 225 | CanvasKit.SkVector.add = function(a, b) { |
| 226 | return a.map(function(v, i) { return v+b[i] }); |
| 227 | } |
| 228 | CanvasKit.SkVector.sub = function(a, b) { |
| 229 | return a.map(function(v, i) { return v-b[i]; }); |
| 230 | } |
Nathaniel Nifong | cc5415a | 2020-02-23 14:26:33 -0500 | [diff] [blame] | 231 | CanvasKit.SkVector.dist = function(a, b) { |
| 232 | return CanvasKit.SkVector.length(CanvasKit.SkVector.sub(a, b)); |
| 233 | } |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 234 | CanvasKit.SkVector.normalize = function(v) { |
| 235 | return CanvasKit.SkVector.mulScalar(v, 1/CanvasKit.SkVector.length(v)); |
| 236 | } |
| 237 | CanvasKit.SkVector.cross = function(a, b) { |
| 238 | if (skIsDebug && (a.length !== 3 || a.length !== 3)) { |
| 239 | throw 'Cross product is only defined for 3-dimensional vectors (a.length='+a.length+', b.length='+b.length+')'; |
| 240 | } |
| 241 | return [ |
| 242 | a[1]*b[2] - a[2]*b[1], |
| 243 | a[2]*b[0] - a[0]*b[2], |
| 244 | a[0]*b[1] - a[1]*b[0], |
| 245 | ]; |
| 246 | } |
| 247 | |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 248 | // Functions for creating and manipulating (row-major) 4x4 matrices. Accepted in place of |
| 249 | // SkM44 in canvas methods, for the same reasons as the 3x3 matrices above. |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 250 | // ported from C++ code in SkM44.cpp |
| 251 | CanvasKit.SkM44 = {}; |
| 252 | // Create a 4x4 identity matrix |
| 253 | CanvasKit.SkM44.identity = function() { |
| 254 | return identityN(4); |
| 255 | } |
| 256 | |
| 257 | // Anything named vec below is an array of length 3 representing a vector/point in 3D space. |
| 258 | // Create a 4x4 matrix representing a translate by the provided 3-vec |
| 259 | CanvasKit.SkM44.translated = function(vec) { |
| 260 | return stride(vec, identityN(4), 4, 3, 0); |
| 261 | } |
| 262 | // Create a 4x4 matrix representing a scaling by the provided 3-vec |
| 263 | CanvasKit.SkM44.scaled = function(vec) { |
| 264 | return stride(vec, identityN(4), 4, 0, 1); |
| 265 | } |
| 266 | // Create a 4x4 matrix representing a rotation about the provided axis 3-vec. |
| 267 | // axis does not need to be normalized. |
| 268 | CanvasKit.SkM44.rotated = function(axisVec, radians) { |
| 269 | return CanvasKit.SkM44.rotatedUnitSinCos( |
| 270 | CanvasKit.SkVector.normalize(axisVec), Math.sin(radians), Math.cos(radians)); |
| 271 | } |
| 272 | // Create a 4x4 matrix representing a rotation about the provided normalized axis 3-vec. |
| 273 | // Rotation is provided redundantly as both sin and cos values. |
| 274 | // This rotate can be used when you already have the cosAngle and sinAngle values |
| 275 | // so you don't have to atan(cos/sin) to call roatated() which expects an angle in radians. |
| 276 | // this does no checking! Behavior for invalid sin or cos values or non-normalized axis vectors |
Nathaniel Nifong | 3392ebe | 2020-06-01 09:21:36 -0400 | [diff] [blame] | 277 | // is incorrect. Prefer rotated(). |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 278 | CanvasKit.SkM44.rotatedUnitSinCos = function(axisVec, sinAngle, cosAngle) { |
| 279 | var x = axisVec[0]; |
| 280 | var y = axisVec[1]; |
| 281 | var z = axisVec[2]; |
| 282 | var c = cosAngle; |
| 283 | var s = sinAngle; |
| 284 | var t = 1 - c; |
| 285 | return [ |
| 286 | t*x*x + c, t*x*y - s*z, t*x*z + s*y, 0, |
| 287 | t*x*y + s*z, t*y*y + c, t*y*z - s*x, 0, |
| 288 | t*x*z - s*y, t*y*z + s*x, t*z*z + c, 0, |
| 289 | 0, 0, 0, 1 |
| 290 | ]; |
| 291 | } |
| 292 | // Create a 4x4 matrix representing a camera at eyeVec, pointed at centerVec. |
| 293 | CanvasKit.SkM44.lookat = function(eyeVec, centerVec, upVec) { |
| 294 | var f = CanvasKit.SkVector.normalize(CanvasKit.SkVector.sub(centerVec, eyeVec)); |
| 295 | var u = CanvasKit.SkVector.normalize(upVec); |
| 296 | var s = CanvasKit.SkVector.normalize(CanvasKit.SkVector.cross(f, u)); |
| 297 | |
| 298 | var m = CanvasKit.SkM44.identity(); |
| 299 | // set each column's top three numbers |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 300 | stride(s, m, 4, 0, 0); |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 301 | stride(CanvasKit.SkVector.cross(s, f), m, 4, 1, 0); |
| 302 | stride(CanvasKit.SkVector.mulScalar(f, -1), m, 4, 2, 0); |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 303 | stride(eyeVec, m, 4, 3, 0); |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 304 | |
| 305 | var m2 = CanvasKit.SkM44.invert(m); |
| 306 | if (m2 === null) { |
| 307 | return CanvasKit.SkM44.identity(); |
| 308 | } |
| 309 | return m2; |
| 310 | } |
| 311 | // Create a 4x4 matrix representing a perspective. All arguments are scalars. |
| 312 | // angle is in radians. |
| 313 | CanvasKit.SkM44.perspective = function(near, far, angle) { |
| 314 | if (skIsDebug && (far <= near)) { |
| 315 | throw "far must be greater than near when constructing SkM44 using perspective."; |
| 316 | } |
| 317 | var dInv = 1 / (far - near); |
| 318 | var halfAngle = angle / 2; |
| 319 | var cot = Math.cos(halfAngle) / Math.sin(halfAngle); |
| 320 | return [ |
| 321 | cot, 0, 0, 0, |
| 322 | 0, cot, 0, 0, |
| 323 | 0, 0, (far+near)*dInv, 2*far*near*dInv, |
| 324 | 0, 0, -1, 1, |
| 325 | ]; |
| 326 | } |
| 327 | // Returns the number at the given row and column in matrix m. |
| 328 | CanvasKit.SkM44.rc = function(m, r, c) { |
| 329 | return m[r*4+c]; |
| 330 | } |
| 331 | // Accepts any number of 4x4 matrix arguments, multiplies them left to right. |
| 332 | CanvasKit.SkM44.multiply = function() { |
| 333 | return multiplyMany(4, arguments); |
| 334 | } |
| 335 | |
| 336 | // Invert the 4x4 matrix if it is invertible and return it. if not, return null. |
| 337 | // taken from SkM44.cpp (altered to use row-major order) |
| 338 | // m is not altered. |
| 339 | CanvasKit.SkM44.invert = function(m) { |
| 340 | if (skIsDebug && !m.every(isnumber)) { |
| 341 | throw 'some members of matrix are NaN m='+m; |
| 342 | } |
| 343 | |
| 344 | var a00 = m[0]; |
| 345 | var a01 = m[4]; |
| 346 | var a02 = m[8]; |
| 347 | var a03 = m[12]; |
| 348 | var a10 = m[1]; |
| 349 | var a11 = m[5]; |
| 350 | var a12 = m[9]; |
| 351 | var a13 = m[13]; |
| 352 | var a20 = m[2]; |
| 353 | var a21 = m[6]; |
| 354 | var a22 = m[10]; |
| 355 | var a23 = m[14]; |
| 356 | var a30 = m[3]; |
| 357 | var a31 = m[7]; |
| 358 | var a32 = m[11]; |
| 359 | var a33 = m[15]; |
| 360 | |
| 361 | var b00 = a00 * a11 - a01 * a10; |
| 362 | var b01 = a00 * a12 - a02 * a10; |
| 363 | var b02 = a00 * a13 - a03 * a10; |
| 364 | var b03 = a01 * a12 - a02 * a11; |
| 365 | var b04 = a01 * a13 - a03 * a11; |
| 366 | var b05 = a02 * a13 - a03 * a12; |
| 367 | var b06 = a20 * a31 - a21 * a30; |
| 368 | var b07 = a20 * a32 - a22 * a30; |
| 369 | var b08 = a20 * a33 - a23 * a30; |
| 370 | var b09 = a21 * a32 - a22 * a31; |
| 371 | var b10 = a21 * a33 - a23 * a31; |
| 372 | var b11 = a22 * a33 - a23 * a32; |
| 373 | |
| 374 | // calculate determinate |
| 375 | var det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; |
| 376 | var invdet = 1.0 / det; |
| 377 | |
| 378 | // bail out if the matrix is not invertible |
| 379 | if (det === 0 || invdet === Infinity) { |
| 380 | SkDebug('Warning, uninvertible matrix'); |
| 381 | return null; |
| 382 | } |
| 383 | |
| 384 | b00 *= invdet; |
| 385 | b01 *= invdet; |
| 386 | b02 *= invdet; |
| 387 | b03 *= invdet; |
| 388 | b04 *= invdet; |
| 389 | b05 *= invdet; |
| 390 | b06 *= invdet; |
| 391 | b07 *= invdet; |
| 392 | b08 *= invdet; |
| 393 | b09 *= invdet; |
| 394 | b10 *= invdet; |
| 395 | b11 *= invdet; |
| 396 | |
| 397 | // store result in row major order |
| 398 | var tmp = [ |
| 399 | a11 * b11 - a12 * b10 + a13 * b09, |
| 400 | a12 * b08 - a10 * b11 - a13 * b07, |
| 401 | a10 * b10 - a11 * b08 + a13 * b06, |
| 402 | a11 * b07 - a10 * b09 - a12 * b06, |
| 403 | |
| 404 | a02 * b10 - a01 * b11 - a03 * b09, |
| 405 | a00 * b11 - a02 * b08 + a03 * b07, |
| 406 | a01 * b08 - a00 * b10 - a03 * b06, |
| 407 | a00 * b09 - a01 * b07 + a02 * b06, |
| 408 | |
| 409 | a31 * b05 - a32 * b04 + a33 * b03, |
| 410 | a32 * b02 - a30 * b05 - a33 * b01, |
| 411 | a30 * b04 - a31 * b02 + a33 * b00, |
| 412 | a31 * b01 - a30 * b03 - a32 * b00, |
| 413 | |
| 414 | a22 * b04 - a21 * b05 - a23 * b03, |
| 415 | a20 * b05 - a22 * b02 + a23 * b01, |
| 416 | a21 * b02 - a20 * b04 - a23 * b00, |
| 417 | a20 * b03 - a21 * b01 + a22 * b00, |
| 418 | ]; |
| 419 | |
| 420 | |
| 421 | if (!tmp.every(function(val) { return val !== NaN && val !== Infinity && val !== -Infinity; })) { |
| 422 | SkDebug('inverted matrix contains infinities or NaN '+tmp); |
| 423 | return null; |
| 424 | } |
| 425 | return tmp; |
| 426 | } |
| 427 | |
Nathaniel Nifong | cc5415a | 2020-02-23 14:26:33 -0500 | [diff] [blame] | 428 | CanvasKit.SkM44.transpose = function(m) { |
| 429 | return [ |
| 430 | m[0], m[4], m[8], m[12], |
| 431 | m[1], m[5], m[9], m[13], |
| 432 | m[2], m[6], m[10], m[14], |
| 433 | m[3], m[7], m[11], m[15], |
| 434 | ]; |
| 435 | } |
Nathaniel Nifong | 77798b4 | 2020-02-21 17:15:22 -0500 | [diff] [blame] | 436 | |
Nathaniel Nifong | 6130d50 | 2020-07-06 19:50:13 -0400 | [diff] [blame] | 437 | // Return the inverse of an SkM44. throw an error if it's not invertible |
| 438 | CanvasKit.SkM44.mustInvert = function(m) { |
| 439 | var m2 = CanvasKit.SkM44.invert(m); |
| 440 | if (m2 === null) { |
| 441 | throw "Matrix not invertible"; |
| 442 | } |
| 443 | return m2; |
| 444 | } |
| 445 | |
| 446 | // returns a matrix that sets up a 3D perspective view from a given camera. |
| 447 | // |
| 448 | // area - a rect describing the viewport. (0, 0, canvas_width, canvas_height) suggested |
| 449 | // zscale - a scalar describing the scale of the z axis. min(width, height)/2 suggested |
| 450 | // cam - an object with the following attributes |
| 451 | // const cam = { |
| 452 | // 'eye' : [0, 0, 1 / Math.tan(Math.PI / 24) - 1], // a 3D point locating the camera |
| 453 | // 'coa' : [0, 0, 0], // center of attention - the 3D point the camera is looking at. |
| 454 | // 'up' : [0, 1, 0], // a unit vector pointing in the camera's up direction, because eye and coa alone leave roll unspecified. |
| 455 | // 'near' : 0.02, // near clipping plane |
| 456 | // 'far' : 4, // far clipping plane |
| 457 | // 'angle': Math.PI / 12, // field of view in radians |
| 458 | // }; |
| 459 | CanvasKit.SkM44.setupCamera = function(area, zscale, cam) { |
| 460 | var camera = CanvasKit.SkM44.lookat(cam['eye'], cam['coa'], cam['up']); |
| 461 | var perspective = CanvasKit.SkM44.perspective(cam['near'], cam['far'], cam['angle']); |
| 462 | var center = [(area.fLeft + area.fRight)/2, (area.fTop + area.fBottom)/2, 0]; |
| 463 | var viewScale = [(area.fRight - area.fLeft)/2, (area.fBottom - area.fTop)/2, zscale]; |
| 464 | var viewport = CanvasKit.SkM44.multiply( |
| 465 | CanvasKit.SkM44.translated(center), |
| 466 | CanvasKit.SkM44.scaled(viewScale)); |
| 467 | return CanvasKit.SkM44.multiply( |
| 468 | viewport, perspective, camera, CanvasKit.SkM44.mustInvert(viewport)); |
| 469 | } |
| 470 | |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 471 | // An SkColorMatrix is a 4x4 color matrix that transforms the 4 color channels |
| 472 | // with a 1x4 matrix that post-translates those 4 channels. |
| 473 | // For example, the following is the layout with the scale (S) and post-transform |
| 474 | // (PT) items indicated. |
| 475 | // RS, 0, 0, 0 | RPT |
| 476 | // 0, GS, 0, 0 | GPT |
| 477 | // 0, 0, BS, 0 | BPT |
| 478 | // 0, 0, 0, AS | APT |
| 479 | // |
| 480 | // Much of this was hand-transcribed from SkColorMatrix.cpp, because it's easier to |
| 481 | // deal with a Float32Array of length 20 than to try to expose the SkColorMatrix object. |
| 482 | |
Nathaniel Nifong | cc5415a | 2020-02-23 14:26:33 -0500 | [diff] [blame] | 483 | var rScale = 0; |
| 484 | var gScale = 6; |
| 485 | var bScale = 12; |
| 486 | var aScale = 18; |
| 487 | |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 488 | var rPostTrans = 4; |
| 489 | var gPostTrans = 9; |
| 490 | var bPostTrans = 14; |
| 491 | var aPostTrans = 19; |
| 492 | |
| 493 | CanvasKit.SkColorMatrix = {}; |
| 494 | CanvasKit.SkColorMatrix.identity = function() { |
Nathaniel Nifong | cc5415a | 2020-02-23 14:26:33 -0500 | [diff] [blame] | 495 | var m = new Float32Array(20); |
| 496 | m[rScale] = 1; |
| 497 | m[gScale] = 1; |
| 498 | m[bScale] = 1; |
| 499 | m[aScale] = 1; |
| 500 | return m; |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | CanvasKit.SkColorMatrix.scaled = function(rs, gs, bs, as) { |
Nathaniel Nifong | cc5415a | 2020-02-23 14:26:33 -0500 | [diff] [blame] | 504 | var m = new Float32Array(20); |
| 505 | m[rScale] = rs; |
| 506 | m[gScale] = gs; |
| 507 | m[bScale] = bs; |
| 508 | m[aScale] = as; |
| 509 | return m; |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | var rotateIndices = [ |
| 513 | [6, 7, 11, 12], |
| 514 | [0, 10, 2, 12], |
| 515 | [0, 1, 5, 6], |
| 516 | ]; |
| 517 | // axis should be 0, 1, 2 for r, g, b |
| 518 | CanvasKit.SkColorMatrix.rotated = function(axis, sine, cosine) { |
| 519 | var m = CanvasKit.SkColorMatrix.identity(); |
| 520 | var indices = rotateIndices[axis]; |
| 521 | m[indices[0]] = cosine; |
| 522 | m[indices[1]] = sine; |
| 523 | m[indices[2]] = -sine; |
| 524 | m[indices[3]] = cosine; |
| 525 | return m; |
| 526 | } |
| 527 | |
| 528 | // m is a SkColorMatrix (i.e. a Float32Array), and this sets the 4 "special" |
| 529 | // params that will translate the colors after they are multiplied by the 4x4 matrix. |
| 530 | CanvasKit.SkColorMatrix.postTranslate = function(m, dr, dg, db, da) { |
| 531 | m[rPostTrans] += dr; |
| 532 | m[gPostTrans] += dg; |
| 533 | m[bPostTrans] += db; |
| 534 | m[aPostTrans] += da; |
| 535 | return m; |
| 536 | } |
| 537 | |
| 538 | // concat returns a new SkColorMatrix that is the result of multiplying outer*inner; |
| 539 | CanvasKit.SkColorMatrix.concat = function(outer, inner) { |
| 540 | var m = new Float32Array(20); |
| 541 | var index = 0; |
| 542 | for (var j = 0; j < 20; j += 5) { |
| 543 | for (var i = 0; i < 4; i++) { |
| 544 | m[index++] = outer[j + 0] * inner[i + 0] + |
| 545 | outer[j + 1] * inner[i + 5] + |
| 546 | outer[j + 2] * inner[i + 10] + |
| 547 | outer[j + 3] * inner[i + 15]; |
| 548 | } |
| 549 | m[index++] = outer[j + 0] * inner[4] + |
| 550 | outer[j + 1] * inner[9] + |
| 551 | outer[j + 2] * inner[14] + |
| 552 | outer[j + 3] * inner[19] + |
| 553 | outer[j + 4]; |
| 554 | } |
| 555 | |
| 556 | return m; |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 557 | }; |
| 558 | |
| 559 | CanvasKit.SkPath.MakeFromCmds = function(cmds) { |
| 560 | var ptrLen = loadCmdsTypedArray(cmds); |
| 561 | var path = CanvasKit.SkPath._MakeFromCmds(ptrLen[0], ptrLen[1]); |
| 562 | CanvasKit._free(ptrLen[0]); |
| 563 | return path; |
| 564 | }; |
| 565 | |
| 566 | // Deprecated |
| 567 | CanvasKit.MakePathFromCmds = CanvasKit.SkPath.MakeFromCmds; |
| 568 | |
| 569 | // The weights array is optional (only used for conics). |
| 570 | CanvasKit.SkPath.MakeFromVerbsPointsWeights = function(verbs, pts, weights) { |
| 571 | var verbsPtr = copy1dArray(verbs, "HEAPU8"); |
| 572 | var pointsPtr = copy1dArray(pts, "HEAPF32"); |
| 573 | var weightsPtr = copy1dArray(weights, "HEAPF32"); |
| 574 | var numWeights = (weights && weights.length) || 0; |
| 575 | var path = CanvasKit.SkPath._MakeFromVerbsPointsWeights( |
| 576 | verbsPtr, verbs.length, pointsPtr, pts.length, weightsPtr, numWeights); |
| 577 | freeArraysThatAreNotMallocedByUsers(verbsPtr, verbs); |
| 578 | freeArraysThatAreNotMallocedByUsers(pointsPtr, pts); |
| 579 | freeArraysThatAreNotMallocedByUsers(weightsPtr, weights); |
| 580 | return path; |
| 581 | }; |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 582 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 583 | CanvasKit.SkPath.prototype.addArc = function(oval, startAngle, sweepAngle) { |
| 584 | // see arc() for the HTMLCanvas version |
| 585 | // note input angles are degrees. |
| 586 | this._addArc(oval, startAngle, sweepAngle); |
| 587 | return this; |
| 588 | }; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 589 | |
Kevin Lubick | e384df4 | 2019-08-26 15:48:09 -0400 | [diff] [blame] | 590 | CanvasKit.SkPath.prototype.addOval = function(oval, isCCW, startIndex) { |
| 591 | if (startIndex === undefined) { |
| 592 | startIndex = 1; |
| 593 | } |
| 594 | this._addOval(oval, !!isCCW, startIndex); |
| 595 | return this; |
| 596 | }; |
| 597 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 598 | CanvasKit.SkPath.prototype.addPath = function() { |
| 599 | // Takes 1, 2, 7, or 10 required args, where the first arg is always the path. |
| 600 | // The last arg is optional and chooses between add or extend mode. |
| 601 | // The options for the remaining args are: |
| 602 | // - an array of 6 or 9 parameters (perspective is optional) |
| 603 | // - the 9 parameters of a full matrix or |
| 604 | // the 6 non-perspective params of a matrix. |
| 605 | var args = Array.prototype.slice.call(arguments); |
| 606 | var path = args[0]; |
| 607 | var extend = false; |
| 608 | if (typeof args[args.length-1] === "boolean") { |
| 609 | extend = args.pop(); |
| 610 | } |
| 611 | if (args.length === 1) { |
| 612 | // Add path, unchanged. Use identity matrix |
| 613 | this._addPath(path, 1, 0, 0, |
| 614 | 0, 1, 0, |
| 615 | 0, 0, 1, |
| 616 | extend); |
| 617 | } else if (args.length === 2) { |
| 618 | // User provided the 9 params of a full matrix as an array. |
| 619 | var a = args[1]; |
| 620 | this._addPath(path, a[0], a[1], a[2], |
| 621 | a[3], a[4], a[5], |
| 622 | a[6] || 0, a[7] || 0, a[8] || 1, |
| 623 | extend); |
| 624 | } else if (args.length === 7 || args.length === 10) { |
| 625 | // User provided the 9 params of a (full) matrix directly. |
| 626 | // (or just the 6 non perspective ones) |
| 627 | // These are in the same order as what Skia expects. |
| 628 | var a = args; |
| 629 | this._addPath(path, a[1], a[2], a[3], |
| 630 | a[4], a[5], a[6], |
| 631 | a[7] || 0, a[8] || 0, a[9] || 1, |
| 632 | extend); |
| 633 | } else { |
| 634 | 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] | 635 | return null; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 636 | } |
| 637 | return this; |
| 638 | }; |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 639 | |
Kevin Lubick | 37ab53e | 2019-11-11 10:06:08 -0500 | [diff] [blame] | 640 | // points is either an array of [x, y] where x and y are numbers or |
| 641 | // a typed array from Malloc where the even indices will be treated |
| 642 | // as x coordinates and the odd indices will be treated as y coordinates. |
| 643 | CanvasKit.SkPath.prototype.addPoly = function(points, close) { |
| 644 | var ptr; |
| 645 | var n; |
| 646 | // This was created with CanvasKit.Malloc, so assume the user has |
| 647 | // already been filled with data. |
| 648 | if (points['_ck']) { |
| 649 | ptr = points.byteOffset; |
| 650 | n = points.length/2; |
| 651 | } else { |
Kevin Lubick | 69e46da | 2020-06-05 07:13:48 -0400 | [diff] [blame] | 652 | ptr = copy2dArray(points, "HEAPF32"); |
Kevin Lubick | 37ab53e | 2019-11-11 10:06:08 -0500 | [diff] [blame] | 653 | n = points.length; |
| 654 | } |
| 655 | this._addPoly(ptr, n, close); |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 656 | freeArraysThatAreNotMallocedByUsers(ptr, points); |
Kevin Lubick | 37ab53e | 2019-11-11 10:06:08 -0500 | [diff] [blame] | 657 | return this; |
| 658 | }; |
| 659 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 660 | CanvasKit.SkPath.prototype.addRect = function() { |
| 661 | // Takes 1, 2, 4 or 5 args |
| 662 | // - SkRect |
| 663 | // - SkRect, isCCW |
| 664 | // - left, top, right, bottom |
| 665 | // - left, top, right, bottom, isCCW |
| 666 | if (arguments.length === 1 || arguments.length === 2) { |
| 667 | var r = arguments[0]; |
| 668 | var ccw = arguments[1] || false; |
| 669 | this._addRect(r.fLeft, r.fTop, r.fRight, r.fBottom, ccw); |
| 670 | } else if (arguments.length === 4 || arguments.length === 5) { |
| 671 | var a = arguments; |
| 672 | this._addRect(a[0], a[1], a[2], a[3], a[4] || false); |
| 673 | } else { |
| 674 | 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] | 675 | return null; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 676 | } |
| 677 | return this; |
| 678 | }; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 679 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 680 | CanvasKit.SkPath.prototype.addRoundRect = function() { |
| 681 | // Takes 3, 4, 6 or 7 args |
Nathaniel Nifong | 3392ebe | 2020-06-01 09:21:36 -0400 | [diff] [blame] | 682 | // - SkRect, radii (an array of 8 numbers), ccw |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 683 | // - SkRect, rx, ry, ccw |
| 684 | // - left, top, right, bottom, radii, ccw |
| 685 | // - left, top, right, bottom, rx, ry, ccw |
| 686 | var args = arguments; |
| 687 | if (args.length === 3 || args.length === 6) { |
| 688 | var radii = args[args.length-2]; |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 689 | } else if (args.length === 4 || args.length === 7){ |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 690 | // duplicate the given (rx, ry) pairs for each corner. |
| 691 | var rx = args[args.length-3]; |
| 692 | var ry = args[args.length-2]; |
| 693 | var radii = [rx, ry, rx, ry, rx, ry, rx, ry]; |
| 694 | } else { |
| 695 | SkDebug('addRoundRect expected to take 3, 4, 6, or 7 args. Got ' + args.length); |
| 696 | return null; |
| 697 | } |
| 698 | if (radii.length !== 8) { |
| 699 | SkDebug('addRoundRect needs 8 radii provided. Got ' + radii.length); |
| 700 | return null; |
| 701 | } |
Kevin Lubick | 69e46da | 2020-06-05 07:13:48 -0400 | [diff] [blame] | 702 | var rptr = copy1dArray(radii, "HEAPF32"); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 703 | if (args.length === 3 || args.length === 4) { |
| 704 | var r = args[0]; |
| 705 | var ccw = args[args.length - 1]; |
| 706 | this._addRoundRect(r.fLeft, r.fTop, r.fRight, r.fBottom, rptr, ccw); |
| 707 | } else if (args.length === 6 || args.length === 7) { |
| 708 | var a = args; |
| 709 | this._addRoundRect(a[0], a[1], a[2], a[3], rptr, ccw); |
| 710 | } |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 711 | freeArraysThatAreNotMallocedByUsers(rptr, radii); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 712 | return this; |
| 713 | }; |
| 714 | |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 715 | // The weights array is optional (only used for conics). |
| 716 | CanvasKit.SkPath.prototype.addVerbsPointsWeights = function(verbs, points, weights) { |
| 717 | var verbsPtr = copy1dArray(verbs, "HEAPU8"); |
| 718 | var pointsPtr = copy1dArray(points, "HEAPF32"); |
| 719 | var weightsPtr = copy1dArray(weights, "HEAPF32"); |
| 720 | var numWeights = (weights && weights.length) || 0; |
| 721 | this._addVerbsPointsWeights(verbsPtr, verbs.length, pointsPtr, points.length, |
| 722 | weightsPtr, numWeights); |
| 723 | freeArraysThatAreNotMallocedByUsers(verbsPtr, verbs); |
| 724 | freeArraysThatAreNotMallocedByUsers(pointsPtr, points); |
| 725 | freeArraysThatAreNotMallocedByUsers(weightsPtr, weights); |
| 726 | }; |
| 727 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 728 | CanvasKit.SkPath.prototype.arc = function(x, y, radius, startAngle, endAngle, ccw) { |
| 729 | // emulates the HTMLCanvas behavior. See addArc() for the SkPath version. |
| 730 | // Note input angles are radians. |
| 731 | var bounds = CanvasKit.LTRBRect(x-radius, y-radius, x+radius, y+radius); |
| 732 | var sweep = radiansToDegrees(endAngle - startAngle) - (360 * !!ccw); |
| 733 | var temp = new CanvasKit.SkPath(); |
| 734 | temp.addArc(bounds, radiansToDegrees(startAngle), sweep); |
| 735 | this.addPath(temp, true); |
| 736 | temp.delete(); |
| 737 | return this; |
| 738 | }; |
| 739 | |
Nathaniel Nifong | d0c9d0c | 2020-07-15 16:46:17 -0400 | [diff] [blame^] | 740 | // Deprecated, use one of the three variants below depending on how many args you were calling it with. |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 741 | CanvasKit.SkPath.prototype.arcTo = function() { |
| 742 | // takes 4, 5 or 7 args |
| 743 | // - 5 x1, y1, x2, y2, radius |
| 744 | // - 4 oval (as Rect), startAngle, sweepAngle, forceMoveTo |
Kevin Lubick | e384df4 | 2019-08-26 15:48:09 -0400 | [diff] [blame] | 745 | // - 7 rx, ry, xAxisRotate, useSmallArc, isCCW, x, y |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 746 | var args = arguments; |
| 747 | if (args.length === 5) { |
Nathaniel Nifong | d0c9d0c | 2020-07-15 16:46:17 -0400 | [diff] [blame^] | 748 | this._arcToTangent(args[0], args[1], args[2], args[3], args[4]); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 749 | } else if (args.length === 4) { |
Nathaniel Nifong | d0c9d0c | 2020-07-15 16:46:17 -0400 | [diff] [blame^] | 750 | this._arcToOval(args[0], args[1], args[2], args[3]); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 751 | } else if (args.length === 7) { |
Nathaniel Nifong | d0c9d0c | 2020-07-15 16:46:17 -0400 | [diff] [blame^] | 752 | this._arcToRotated(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] | 753 | } else { |
| 754 | throw 'Invalid args for arcTo. Expected 4, 5, or 7, got '+ args.length; |
| 755 | } |
| 756 | |
| 757 | return this; |
| 758 | }; |
| 759 | |
Nathaniel Nifong | d0c9d0c | 2020-07-15 16:46:17 -0400 | [diff] [blame^] | 760 | // Appends arc to SkPath. Arc added is part of ellipse |
| 761 | // bounded by oval, from startAngle through sweepAngle. Both startAngle and |
| 762 | // sweepAngle are measured in degrees, where zero degrees is aligned with the |
| 763 | // positive x-axis, and positive sweeps extends arc clockwise. |
| 764 | CanvasKit.SkPath.prototype.arcToOval = function(oval, startAngle, sweepAngle, forceMoveTo) { |
| 765 | this._arcToOval(oval, startAngle, sweepAngle, forceMoveTo); |
| 766 | return this; |
| 767 | }; |
| 768 | |
| 769 | // Appends arc to SkPath. Arc is implemented by one or more conics weighted to |
| 770 | // describe part of oval with radii (rx, ry) rotated by xAxisRotate degrees. Arc |
| 771 | // curves from last SkPath SkPoint to (x, y), choosing one of four possible routes: |
| 772 | // clockwise or counterclockwise, and smaller or larger. |
| 773 | |
| 774 | // Arc sweep is always less than 360 degrees. arcTo() appends line to (x, y) if |
| 775 | // either radii are zero, or if last SkPath SkPoint equals (x, y). arcTo() scales radii |
| 776 | // (rx, ry) to fit last SkPath SkPoint and (x, y) if both are greater than zero but |
| 777 | // too small. |
| 778 | |
| 779 | // arcToRotated() appends up to four conic curves. |
| 780 | // arcToRotated() implements the functionality of SVG arc, although SVG sweep-flag value |
| 781 | // is opposite the integer value of sweep; SVG sweep-flag uses 1 for clockwise, |
| 782 | // while kCW_Direction cast to int is zero. |
| 783 | CanvasKit.SkPath.prototype.arcToRotated = function(rx, ry, xAxisRotate, useSmallArc, isCCW, x, y) { |
| 784 | this._arcToRotated(rx, ry, xAxisRotate, !!useSmallArc, !!isCCW, x, y); |
| 785 | return this; |
| 786 | }; |
| 787 | |
| 788 | // Appends arc to SkPath, after appending line if needed. Arc is implemented by conic |
| 789 | // weighted to describe part of circle. Arc is contained by tangent from |
| 790 | // last SkPath point to (x1, y1), and tangent from (x1, y1) to (x2, y2). Arc |
| 791 | // is part of circle sized to radius, positioned so it touches both tangent lines. |
| 792 | |
| 793 | // If last Path Point does not start Arc, arcTo appends connecting Line to Path. |
| 794 | // The length of Vector from (x1, y1) to (x2, y2) does not affect Arc. |
| 795 | |
| 796 | // Arc sweep is always less than 180 degrees. If radius is zero, or if |
| 797 | // tangents are nearly parallel, arcTo appends Line from last Path Point to (x1, y1). |
| 798 | |
| 799 | // arcToTangent appends at most one Line and one conic. |
| 800 | // arcToTangent implements the functionality of PostScript arct and HTML Canvas arcTo. |
| 801 | CanvasKit.SkPath.prototype.arcToTangent = function(x1, y1, x2, y2, radius) { |
| 802 | this._arcToTangent(x1, y1, x2, y2, radius); |
| 803 | return this; |
| 804 | }; |
| 805 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 806 | CanvasKit.SkPath.prototype.close = function() { |
| 807 | this._close(); |
| 808 | return this; |
| 809 | }; |
| 810 | |
| 811 | CanvasKit.SkPath.prototype.conicTo = function(x1, y1, x2, y2, w) { |
| 812 | this._conicTo(x1, y1, x2, y2, w); |
| 813 | return this; |
| 814 | }; |
| 815 | |
| 816 | CanvasKit.SkPath.prototype.cubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) { |
| 817 | this._cubicTo(cp1x, cp1y, cp2x, cp2y, x, y); |
| 818 | return this; |
| 819 | }; |
| 820 | |
| 821 | CanvasKit.SkPath.prototype.dash = function(on, off, phase) { |
| 822 | if (this._dash(on, off, phase)) { |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 823 | return this; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 824 | } |
| 825 | return null; |
| 826 | }; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 827 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 828 | CanvasKit.SkPath.prototype.lineTo = function(x, y) { |
| 829 | this._lineTo(x, y); |
| 830 | return this; |
| 831 | }; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 832 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 833 | CanvasKit.SkPath.prototype.moveTo = function(x, y) { |
| 834 | this._moveTo(x, y); |
| 835 | return this; |
| 836 | }; |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 837 | |
Kevin Lubick | e384df4 | 2019-08-26 15:48:09 -0400 | [diff] [blame] | 838 | CanvasKit.SkPath.prototype.offset = function(dx, dy) { |
| 839 | this._transform(1, 0, dx, |
| 840 | 0, 1, dy, |
| 841 | 0, 0, 1); |
| 842 | return this; |
| 843 | }; |
| 844 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 845 | CanvasKit.SkPath.prototype.quadTo = function(cpx, cpy, x, y) { |
| 846 | this._quadTo(cpx, cpy, x, y); |
| 847 | return this; |
| 848 | }; |
| 849 | |
Kevin Lubick | 79b7134 | 2019-11-01 14:36:52 -0400 | [diff] [blame] | 850 | CanvasKit.SkPath.prototype.rArcTo = function(rx, ry, xAxisRotate, useSmallArc, isCCW, dx, dy) { |
| 851 | this._rArcTo(rx, ry, xAxisRotate, useSmallArc, isCCW, dx, dy); |
| 852 | return this; |
| 853 | }; |
| 854 | |
| 855 | CanvasKit.SkPath.prototype.rConicTo = function(dx1, dy1, dx2, dy2, w) { |
| 856 | this._rConicTo(dx1, dy1, dx2, dy2, w); |
| 857 | return this; |
| 858 | }; |
| 859 | |
| 860 | // These params are all relative |
| 861 | CanvasKit.SkPath.prototype.rCubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) { |
| 862 | this._rCubicTo(cp1x, cp1y, cp2x, cp2y, x, y); |
| 863 | return this; |
| 864 | }; |
| 865 | |
| 866 | CanvasKit.SkPath.prototype.rLineTo = function(dx, dy) { |
| 867 | this._rLineTo(dx, dy); |
| 868 | return this; |
| 869 | }; |
| 870 | |
| 871 | CanvasKit.SkPath.prototype.rMoveTo = function(dx, dy) { |
| 872 | this._rMoveTo(dx, dy); |
| 873 | return this; |
| 874 | }; |
| 875 | |
| 876 | // These params are all relative |
| 877 | CanvasKit.SkPath.prototype.rQuadTo = function(cpx, cpy, x, y) { |
| 878 | this._rQuadTo(cpx, cpy, x, y); |
| 879 | return this; |
| 880 | }; |
| 881 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 882 | CanvasKit.SkPath.prototype.stroke = function(opts) { |
| 883 | // Fill out any missing values with the default values. |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 884 | opts = opts || {}; |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 885 | opts['width'] = opts['width'] || 1; |
| 886 | opts['miter_limit'] = opts['miter_limit'] || 4; |
| 887 | opts['cap'] = opts['cap'] || CanvasKit.StrokeCap.Butt; |
| 888 | opts['join'] = opts['join'] || CanvasKit.StrokeJoin.Miter; |
| 889 | opts['precision'] = opts['precision'] || 1; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 890 | if (this._stroke(opts)) { |
| 891 | return this; |
| 892 | } |
| 893 | return null; |
| 894 | }; |
| 895 | |
| 896 | CanvasKit.SkPath.prototype.transform = function() { |
| 897 | // Takes 1 or 9 args |
| 898 | if (arguments.length === 1) { |
| 899 | // argument 1 should be a 6 or 9 element array. |
| 900 | var a = arguments[0]; |
| 901 | this._transform(a[0], a[1], a[2], |
| 902 | a[3], a[4], a[5], |
| 903 | a[6] || 0, a[7] || 0, a[8] || 1); |
| 904 | } else if (arguments.length === 6 || arguments.length === 9) { |
| 905 | // these arguments are the 6 or 9 members of the matrix |
| 906 | var a = arguments; |
| 907 | this._transform(a[0], a[1], a[2], |
| 908 | a[3], a[4], a[5], |
| 909 | a[6] || 0, a[7] || 0, a[8] || 1); |
| 910 | } else { |
| 911 | throw 'transform expected to take 1 or 9 arguments. Got ' + arguments.length; |
| 912 | } |
| 913 | return this; |
| 914 | }; |
| 915 | // isComplement is optional, defaults to false |
| 916 | CanvasKit.SkPath.prototype.trim = function(startT, stopT, isComplement) { |
| 917 | if (this._trim(startT, stopT, !!isComplement)) { |
| 918 | return this; |
| 919 | } |
| 920 | return null; |
| 921 | }; |
| 922 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 923 | CanvasKit.SkImage.prototype.encodeToData = function() { |
| 924 | if (!arguments.length) { |
| 925 | return this._encodeToData(); |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 926 | } |
Kevin Lubick | 53965c9 | 2018-10-11 08:51:55 -0400 | [diff] [blame] | 927 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 928 | if (arguments.length === 2) { |
| 929 | var a = arguments; |
| 930 | return this._encodeToDataWithFormat(a[0], a[1]); |
Alexander Khovansky | 3e11933 | 2018-11-15 02:01:19 +0300 | [diff] [blame] | 931 | } |
| 932 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 933 | throw 'encodeToData expected to take 0 or 2 arguments. Got ' + arguments.length; |
| 934 | } |
Kevin Lubick | 1ba9c4d | 2019-02-22 10:04:06 -0500 | [diff] [blame] | 935 | |
Kevin Lubick | a064c28 | 2019-04-04 09:28:53 -0400 | [diff] [blame] | 936 | CanvasKit.SkImage.prototype.makeShader = function(xTileMode, yTileMode, localMatrix) { |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 937 | var localMatrixPtr = copy3x3MatrixToWasm(localMatrix); |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 938 | return this._makeShader(xTileMode, yTileMode, localMatrixPtr); |
Kevin Lubick | a064c28 | 2019-04-04 09:28:53 -0400 | [diff] [blame] | 939 | } |
| 940 | |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 941 | CanvasKit.SkImage.prototype.readPixels = function(imageInfo, srcX, srcY) { |
| 942 | var rowBytes; |
Kevin Lubick | 319524b | 2020-01-22 15:29:14 -0500 | [diff] [blame] | 943 | // Important to use ["string"] notation here, otherwise the closure compiler will |
| 944 | // minify away the colorType. |
| 945 | switch (imageInfo["colorType"]) { |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 946 | case CanvasKit.ColorType.RGBA_8888: |
| 947 | rowBytes = imageInfo.width * 4; // 1 byte per channel == 4 bytes per pixel in 8888 |
| 948 | break; |
| 949 | case CanvasKit.ColorType.RGBA_F32: |
| 950 | rowBytes = imageInfo.width * 16; // 4 bytes per channel == 16 bytes per pixel in F32 |
| 951 | break; |
| 952 | default: |
| 953 | SkDebug("Colortype not yet supported"); |
| 954 | return; |
| 955 | } |
| 956 | var pBytes = rowBytes * imageInfo.height; |
| 957 | var pPtr = CanvasKit._malloc(pBytes); |
| 958 | |
| 959 | if (!this._readPixels(imageInfo, pPtr, rowBytes, srcX, srcY)) { |
| 960 | SkDebug("Could not read pixels with the given inputs"); |
| 961 | return null; |
| 962 | } |
| 963 | |
| 964 | // Put those pixels into a typed array of the right format and then |
| 965 | // make a copy with slice() that we can return. |
| 966 | var retVal = null; |
Kevin Lubick | 319524b | 2020-01-22 15:29:14 -0500 | [diff] [blame] | 967 | switch (imageInfo["colorType"]) { |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 968 | case CanvasKit.ColorType.RGBA_8888: |
Bryce Thomas | 1fa5404 | 2020-01-14 13:46:30 -0800 | [diff] [blame] | 969 | retVal = new Uint8Array(CanvasKit.HEAPU8.buffer, pPtr, pBytes).slice(); |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 970 | break; |
| 971 | case CanvasKit.ColorType.RGBA_F32: |
Bryce Thomas | 1fa5404 | 2020-01-14 13:46:30 -0800 | [diff] [blame] | 972 | retVal = new Float32Array(CanvasKit.HEAPU8.buffer, pPtr, pBytes).slice(); |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 973 | break; |
| 974 | } |
| 975 | |
| 976 | // Free the allocated pixels in the WASM memory |
| 977 | CanvasKit._free(pPtr); |
| 978 | return retVal; |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 979 | } |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 980 | |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 981 | // Accepts an array of four numbers in the range of 0-1 representing a 4f color |
| 982 | CanvasKit.SkCanvas.prototype.clear = function (color4f) { |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 983 | var cPtr = copyColorToWasm(color4f); |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 984 | this._clear(cPtr); |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 985 | } |
| 986 | |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 987 | // concat takes a 3x2, a 3x3, or a 4x4 matrix and upscales it (if needed) to 4x4. This is because |
| 988 | // under the hood, SkCanvas uses a 4x4 matrix. |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 989 | CanvasKit.SkCanvas.prototype.concat = function(matr) { |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 990 | var matrPtr = copy4x4MatrixToWasm(matr); |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 991 | this._concat(matrPtr); |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 992 | } |
| 993 | |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 994 | // Deprecated - just use concat |
| 995 | CanvasKit.SkCanvas.prototype.concat44 = CanvasKit.SkCanvas.prototype.concat; |
| 996 | |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 997 | // atlas is an SkImage, e.g. from CanvasKit.MakeImageFromEncoded |
Nathaniel Nifong | cd75b17 | 2020-06-05 11:17:43 -0400 | [diff] [blame] | 998 | // srcRects, dstXforms, and colors should be CanvasKit.SkRectBuilder, CanvasKit.RSXFormBuilder, |
| 999 | // and CanvasKit.SkColorBuilder (fastest) |
| 1000 | // Or they can be an array of floats of length 4*number of destinations. |
| 1001 | // colors are optional and used to tint the drawn images using the optional blend mode |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1002 | // Colors may be an SkColorBuilder, a Uint32Array of int colors, |
| 1003 | // a Flat Float32Array of float colors or a 2d Array of Float32Array(4) (deprecated) |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1004 | CanvasKit.SkCanvas.prototype.drawAtlas = function(atlas, srcRects, dstXforms, paint, |
| 1005 | /*optional*/ blendMode, colors) { |
| 1006 | if (!atlas || !paint || !srcRects || !dstXforms) { |
| 1007 | SkDebug('Doing nothing since missing a required input'); |
| 1008 | return; |
| 1009 | } |
Nathaniel Nifong | cd75b17 | 2020-06-05 11:17:43 -0400 | [diff] [blame] | 1010 | |
| 1011 | // builder arguments report the length as the number of rects, but when passed as arrays |
| 1012 | // their.length attribute is 4x higher because it's the number of total components of all rects. |
| 1013 | // colors is always going to report the same length, at least until floats colors are supported |
| 1014 | // by this function. |
| 1015 | if (srcRects.length !== dstXforms.length) { |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1016 | SkDebug('Doing nothing since input arrays length mismatches'); |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 1017 | return; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1018 | } |
| 1019 | if (!blendMode) { |
| 1020 | blendMode = CanvasKit.BlendMode.SrcOver; |
| 1021 | } |
| 1022 | |
| 1023 | var srcRectPtr; |
| 1024 | if (srcRects.build) { |
| 1025 | srcRectPtr = srcRects.build(); |
| 1026 | } else { |
Kevin Lubick | 69e46da | 2020-06-05 07:13:48 -0400 | [diff] [blame] | 1027 | srcRectPtr = copy1dArray(srcRects, "HEAPF32"); |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1028 | } |
| 1029 | |
Nathaniel Nifong | cd75b17 | 2020-06-05 11:17:43 -0400 | [diff] [blame] | 1030 | var count = 1; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1031 | var dstXformPtr; |
| 1032 | if (dstXforms.build) { |
| 1033 | dstXformPtr = dstXforms.build(); |
Nathaniel Nifong | cd75b17 | 2020-06-05 11:17:43 -0400 | [diff] [blame] | 1034 | count = dstXforms.length; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1035 | } else { |
Kevin Lubick | 69e46da | 2020-06-05 07:13:48 -0400 | [diff] [blame] | 1036 | dstXformPtr = copy1dArray(dstXforms, "HEAPF32"); |
Nathaniel Nifong | cd75b17 | 2020-06-05 11:17:43 -0400 | [diff] [blame] | 1037 | count = dstXforms.length / 4; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1038 | } |
| 1039 | |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1040 | var colorPtr = nullptr; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1041 | if (colors) { |
| 1042 | if (colors.build) { |
| 1043 | colorPtr = colors.build(); |
| 1044 | } else { |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1045 | colorPtr = copy1dArray(assureIntColors(colors), "HEAPU32"); |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1046 | } |
| 1047 | } |
| 1048 | |
Nathaniel Nifong | cd75b17 | 2020-06-05 11:17:43 -0400 | [diff] [blame] | 1049 | this._drawAtlas(atlas, dstXformPtr, srcRectPtr, colorPtr, count, blendMode, paint); |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1050 | |
| 1051 | if (srcRectPtr && !srcRects.build) { |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 1052 | freeArraysThatAreNotMallocedByUsers(srcRectPtr, srcRects); |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1053 | } |
| 1054 | if (dstXformPtr && !dstXforms.build) { |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 1055 | freeArraysThatAreNotMallocedByUsers(dstXformPtr, dstXforms); |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1056 | } |
| 1057 | if (colorPtr && !colors.build) { |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 1058 | freeArraysThatAreNotMallocedByUsers(colorPtr, colors); |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1059 | } |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 1060 | } |
| 1061 | |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1062 | CanvasKit.SkCanvas.prototype.drawColor = function (color4f, mode) { |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1063 | var cPtr = copyColorToWasm(color4f); |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1064 | if (mode !== undefined) { |
| 1065 | this._drawColor(cPtr, mode); |
| 1066 | } else { |
| 1067 | this._drawColor(cPtr); |
| 1068 | } |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1069 | } |
| 1070 | |
Kevin Lubick | 93f1a38 | 2020-06-02 16:15:23 -0400 | [diff] [blame] | 1071 | CanvasKit.SkCanvas.prototype.drawColorComponents = function (r, g, b, a, mode) { |
| 1072 | var cPtr = copyColorComponentsToWasm(r, g, b, a); |
| 1073 | if (mode !== undefined) { |
| 1074 | this._drawColor(cPtr, mode); |
| 1075 | } else { |
| 1076 | this._drawColor(cPtr); |
| 1077 | } |
| 1078 | } |
| 1079 | |
Kevin Lubick | 37ab53e | 2019-11-11 10:06:08 -0500 | [diff] [blame] | 1080 | // points is either an array of [x, y] where x and y are numbers or |
| 1081 | // a typed array from Malloc where the even indices will be treated |
| 1082 | // as x coordinates and the odd indices will be treated as y coordinates. |
| 1083 | CanvasKit.SkCanvas.prototype.drawPoints = function(mode, points, paint) { |
| 1084 | var ptr; |
| 1085 | var n; |
| 1086 | // This was created with CanvasKit.Malloc, so assume the user has |
| 1087 | // already been filled with data. |
| 1088 | if (points['_ck']) { |
| 1089 | ptr = points.byteOffset; |
| 1090 | n = points.length/2; |
| 1091 | } else { |
Kevin Lubick | 69e46da | 2020-06-05 07:13:48 -0400 | [diff] [blame] | 1092 | ptr = copy2dArray(points, "HEAPF32"); |
Kevin Lubick | 37ab53e | 2019-11-11 10:06:08 -0500 | [diff] [blame] | 1093 | n = points.length; |
| 1094 | } |
| 1095 | this._drawPoints(mode, ptr, n, paint); |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 1096 | freeArraysThatAreNotMallocedByUsers(ptr, points); |
Kevin Lubick | 37ab53e | 2019-11-11 10:06:08 -0500 | [diff] [blame] | 1097 | } |
| 1098 | |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1099 | CanvasKit.SkCanvas.prototype.drawShadow = function(path, zPlaneParams, lightPos, lightRadius, ambientColor, spotColor, flags) { |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1100 | var ambiPtr = copyColorToWasmNoScratch(ambientColor); |
| 1101 | var spotPtr = copyColorToWasmNoScratch(spotColor); |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1102 | this._drawShadow(path, zPlaneParams, lightPos, lightRadius, ambiPtr, spotPtr, flags); |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 1103 | freeArraysThatAreNotMallocedByUsers(ambiPtr, ambientColor); |
| 1104 | freeArraysThatAreNotMallocedByUsers(spotPtr, spotColor); |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1105 | } |
| 1106 | |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 1107 | // getLocalToDevice returns a 4x4 matrix. |
| 1108 | CanvasKit.SkCanvas.prototype.getLocalToDevice = function() { |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 1109 | // _getLocalToDevice will copy the values into the pointer. |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1110 | this._getLocalToDevice(_scratch4x4MatrixPtr); |
| 1111 | return copy4x4MatrixFromWasm(_scratch4x4MatrixPtr); |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 1112 | } |
| 1113 | |
Nathaniel Nifong | 00de91c | 2020-05-06 16:22:33 -0400 | [diff] [blame] | 1114 | // findMarkedCTM returns a 4x4 matrix, or null if a matrix was not found at |
| 1115 | // the provided marker. |
| 1116 | CanvasKit.SkCanvas.prototype.findMarkedCTM = function(marker) { |
Nathaniel Nifong | 00de91c | 2020-05-06 16:22:33 -0400 | [diff] [blame] | 1117 | // _getLocalToDevice will copy the values into the pointer. |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1118 | var found = this._findMarkedCTM(marker, _scratch4x4MatrixPtr); |
Nathaniel Nifong | 00de91c | 2020-05-06 16:22:33 -0400 | [diff] [blame] | 1119 | if (!found) { |
| 1120 | return null; |
| 1121 | } |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1122 | return copy4x4MatrixFromWasm(_scratch4x4MatrixPtr); |
Nathaniel Nifong | 00de91c | 2020-05-06 16:22:33 -0400 | [diff] [blame] | 1123 | } |
| 1124 | |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 1125 | // getTotalMatrix returns the current matrix as a 3x3 matrix. |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1126 | CanvasKit.SkCanvas.prototype.getTotalMatrix = function() { |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1127 | // _getTotalMatrix will copy the values into the pointer. |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1128 | this._getTotalMatrix(_scratch3x3MatrixPtr); |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1129 | // read them out into an array. TODO(kjlubick): If we change SkMatrix to be |
| 1130 | // typedArrays, then we should return a typed array here too. |
| 1131 | var rv = new Array(9); |
| 1132 | for (var i = 0; i < 9; i++) { |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1133 | rv[i] = CanvasKit.HEAPF32[_scratch3x3MatrixPtr/4 + i]; // divide by 4 to "cast" to float. |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1134 | } |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1135 | return rv; |
| 1136 | } |
| 1137 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1138 | // returns Uint8Array |
| 1139 | CanvasKit.SkCanvas.prototype.readPixels = function(x, y, w, h, alphaType, |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 1140 | colorType, colorSpace, dstRowBytes) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1141 | // supply defaults (which are compatible with HTMLCanvas's getImageData) |
| 1142 | alphaType = alphaType || CanvasKit.AlphaType.Unpremul; |
| 1143 | colorType = colorType || CanvasKit.ColorType.RGBA_8888; |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 1144 | colorSpace = colorSpace || CanvasKit.SkColorSpace.SRGB; |
| 1145 | var pixBytes = 4; |
| 1146 | if (colorType === CanvasKit.ColorType.RGBA_F16) { |
| 1147 | pixBytes = 8; |
| 1148 | } |
| 1149 | dstRowBytes = dstRowBytes || (pixBytes * w); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1150 | |
| 1151 | var len = h * dstRowBytes |
| 1152 | var pptr = CanvasKit._malloc(len); |
| 1153 | var ok = this._readPixels({ |
| 1154 | 'width': w, |
| 1155 | 'height': h, |
Kevin Lubick | 52b9f37 | 2018-12-04 13:57:36 -0500 | [diff] [blame] | 1156 | 'colorType': colorType, |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1157 | 'alphaType': alphaType, |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 1158 | 'colorSpace': colorSpace, |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1159 | }, pptr, dstRowBytes, x, y); |
| 1160 | if (!ok) { |
| 1161 | CanvasKit._free(pptr); |
| 1162 | return null; |
| 1163 | } |
| 1164 | |
| 1165 | // The first typed array is just a view into memory. Because we will |
| 1166 | // be free-ing that, we call slice to make a persistent copy. |
Bryce Thomas | 1fa5404 | 2020-01-14 13:46:30 -0800 | [diff] [blame] | 1167 | var pixels = new Uint8Array(CanvasKit.HEAPU8.buffer, pptr, len).slice(); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1168 | CanvasKit._free(pptr); |
| 1169 | return pixels; |
| 1170 | } |
| 1171 | |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 1172 | // pixels should be a Uint8Array or a plain JS array. |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1173 | CanvasKit.SkCanvas.prototype.writePixels = function(pixels, srcWidth, srcHeight, |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 1174 | destX, destY, alphaType, colorType, colorSpace) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1175 | if (pixels.byteLength % (srcWidth * srcHeight)) { |
| 1176 | throw 'pixels length must be a multiple of the srcWidth * srcHeight'; |
| 1177 | } |
| 1178 | var bytesPerPixel = pixels.byteLength / (srcWidth * srcHeight); |
| 1179 | // supply defaults (which are compatible with HTMLCanvas's putImageData) |
| 1180 | alphaType = alphaType || CanvasKit.AlphaType.Unpremul; |
| 1181 | colorType = colorType || CanvasKit.ColorType.RGBA_8888; |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 1182 | colorSpace = colorSpace || CanvasKit.SkColorSpace.SRGB; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1183 | var srcRowBytes = bytesPerPixel * srcWidth; |
| 1184 | |
Kevin Lubick | 69e46da | 2020-06-05 07:13:48 -0400 | [diff] [blame] | 1185 | var pptr = copy1dArray(pixels, "HEAPU8"); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1186 | var ok = this._writePixels({ |
| 1187 | 'width': srcWidth, |
| 1188 | 'height': srcHeight, |
| 1189 | 'colorType': colorType, |
| 1190 | 'alphaType': alphaType, |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 1191 | 'colorSpace': colorSpace, |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1192 | }, pptr, srcRowBytes, destX, destY); |
| 1193 | |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 1194 | freeArraysThatAreNotMallocedByUsers(pptr, pixels); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1195 | return ok; |
Kevin Lubick | 52b9f37 | 2018-12-04 13:57:36 -0500 | [diff] [blame] | 1196 | } |
| 1197 | |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1198 | CanvasKit.SkColorFilter.MakeBlend = function(color4f, mode) { |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1199 | var cPtr = copyColorToWasm(color4f); |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1200 | var result = CanvasKit.SkColorFilter._MakeBlend(cPtr, mode); |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1201 | return result; |
| 1202 | } |
| 1203 | |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 1204 | // colorMatrix is an SkColorMatrix (e.g. Float32Array of length 20) |
| 1205 | CanvasKit.SkColorFilter.MakeMatrix = function(colorMatrix) { |
| 1206 | if (!colorMatrix || colorMatrix.length !== 20) { |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1207 | throw 'invalid color matrix'; |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 1208 | } |
Kevin Lubick | 69e46da | 2020-06-05 07:13:48 -0400 | [diff] [blame] | 1209 | var fptr = copy1dArray(colorMatrix, "HEAPF32"); |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 1210 | // We know skia memcopies the floats, so we can free our memory after the call returns. |
| 1211 | var m = CanvasKit.SkColorFilter._makeMatrix(fptr); |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 1212 | freeArraysThatAreNotMallocedByUsers(fptr, colorMatrix); |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 1213 | return m; |
| 1214 | } |
| 1215 | |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1216 | CanvasKit.SkImageFilter.MakeMatrixTransform = function(matr, filterQuality, input) { |
| 1217 | var matrPtr = copy3x3MatrixToWasm(matr); |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1218 | return CanvasKit.SkImageFilter._MakeMatrixTransform(matrPtr, filterQuality, input); |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1219 | } |
| 1220 | |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1221 | CanvasKit.SkPaint.prototype.getColor = function() { |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1222 | this._getColor(_scratchColorPtr); |
| 1223 | return copyColorFromWasm(_scratchColorPtr); |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1224 | } |
| 1225 | |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 1226 | CanvasKit.SkPaint.prototype.setColor = function(color4f, colorSpace) { |
| 1227 | colorSpace = colorSpace || null; // null will be replaced with sRGB in the C++ method. |
| 1228 | // emscripten wouldn't bind undefined to the sk_sp<SkColorSpace> expected here. |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1229 | var cPtr = copyColorToWasm(color4f); |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 1230 | this._setColor(cPtr, colorSpace); |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1231 | } |
| 1232 | |
Kevin Lubick | 93f1a38 | 2020-06-02 16:15:23 -0400 | [diff] [blame] | 1233 | // The color components here are expected to be floating point values (nominally between |
| 1234 | // 0.0 and 1.0, but with wider color gamuts, the values could exceed this range). To convert |
| 1235 | // between standard 8 bit colors and floats, just divide by 255 before passing them in. |
| 1236 | CanvasKit.SkPaint.prototype.setColorComponents = function(r, g, b, a, colorSpace) { |
| 1237 | colorSpace = colorSpace || null; // null will be replaced with sRGB in the C++ method. |
| 1238 | // emscripten wouldn't bind undefined to the sk_sp<SkColorSpace> expected here. |
| 1239 | var cPtr = copyColorComponentsToWasm(r, g, b, a); |
| 1240 | this._setColor(cPtr, colorSpace); |
| 1241 | } |
| 1242 | |
Kevin Lubick | cc13fd3 | 2019-04-05 13:00:01 -0400 | [diff] [blame] | 1243 | CanvasKit.SkSurface.prototype.captureFrameAsSkPicture = function(drawFrame) { |
| 1244 | // Set up SkPictureRecorder |
| 1245 | var spr = new CanvasKit.SkPictureRecorder(); |
| 1246 | var canvas = spr.beginRecording( |
| 1247 | CanvasKit.LTRBRect(0, 0, this.width(), this.height())); |
| 1248 | drawFrame(canvas); |
| 1249 | var pic = spr.finishRecordingAsPicture(); |
| 1250 | spr.delete(); |
| 1251 | // TODO: do we need to clean up the memory for canvas? |
| 1252 | // If we delete it here, saveAsFile doesn't work correctly. |
| 1253 | return pic; |
| 1254 | } |
| 1255 | |
Kevin Lubick | 359a7e3 | 2019-03-19 09:34:37 -0400 | [diff] [blame] | 1256 | CanvasKit.SkSurface.prototype.requestAnimationFrame = function(callback, dirtyRect) { |
| 1257 | if (!this._cached_canvas) { |
| 1258 | this._cached_canvas = this.getCanvas(); |
| 1259 | } |
| 1260 | window.requestAnimationFrame(function() { |
Kevin Lubick | 3902628 | 2019-03-28 12:46:40 -0400 | [diff] [blame] | 1261 | if (this._context !== undefined) { |
| 1262 | CanvasKit.setCurrentContext(this._context); |
| 1263 | } |
Kevin Lubick | 359a7e3 | 2019-03-19 09:34:37 -0400 | [diff] [blame] | 1264 | |
| 1265 | callback(this._cached_canvas); |
| 1266 | |
Bryce Thomas | 2c5b856 | 2020-01-22 13:49:41 -0800 | [diff] [blame] | 1267 | // We do not dispose() of the SkSurface here, as the client will typically |
| 1268 | // call requestAnimationFrame again from within the supplied callback. |
| 1269 | // For drawing a single frame, prefer drawOnce(). |
Bryce Thomas | 9331ca0 | 2020-05-29 16:51:21 -0700 | [diff] [blame] | 1270 | this.flush(dirtyRect); |
Kevin Lubick | 359a7e3 | 2019-03-19 09:34:37 -0400 | [diff] [blame] | 1271 | }.bind(this)); |
| 1272 | } |
| 1273 | |
Kevin Lubick | 5237933 | 2020-01-27 10:01:25 -0500 | [diff] [blame] | 1274 | // drawOnce will dispose of the surface after drawing the frame using the provided |
| 1275 | // callback. |
Bryce Thomas | 2c5b856 | 2020-01-22 13:49:41 -0800 | [diff] [blame] | 1276 | CanvasKit.SkSurface.prototype.drawOnce = function(callback, dirtyRect) { |
| 1277 | if (!this._cached_canvas) { |
| 1278 | this._cached_canvas = this.getCanvas(); |
| 1279 | } |
| 1280 | window.requestAnimationFrame(function() { |
| 1281 | if (this._context !== undefined) { |
| 1282 | CanvasKit.setCurrentContext(this._context); |
| 1283 | } |
| 1284 | callback(this._cached_canvas); |
| 1285 | |
Bryce Thomas | 9331ca0 | 2020-05-29 16:51:21 -0700 | [diff] [blame] | 1286 | this.flush(dirtyRect); |
Bryce Thomas | 2c5b856 | 2020-01-22 13:49:41 -0800 | [diff] [blame] | 1287 | this.dispose(); |
| 1288 | }.bind(this)); |
| 1289 | } |
| 1290 | |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1291 | CanvasKit.SkPathEffect.MakeDash = function(intervals, phase) { |
| 1292 | if (!phase) { |
| 1293 | phase = 0; |
| 1294 | } |
| 1295 | if (!intervals.length || intervals.length % 2 === 1) { |
| 1296 | throw 'Intervals array must have even length'; |
| 1297 | } |
Kevin Lubick | 69e46da | 2020-06-05 07:13:48 -0400 | [diff] [blame] | 1298 | var ptr = copy1dArray(intervals, "HEAPF32"); |
Kevin Lubick | f279c63 | 2020-03-18 09:53:55 -0400 | [diff] [blame] | 1299 | var dpe = CanvasKit.SkPathEffect._MakeDash(ptr, intervals.length, phase); |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 1300 | freeArraysThatAreNotMallocedByUsers(ptr, intervals); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1301 | return dpe; |
| 1302 | } |
| 1303 | |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 1304 | CanvasKit.SkShader.Color = function(color4f, colorSpace) { |
| 1305 | colorSpace = colorSpace || null |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1306 | var cPtr = copyColorToWasm(color4f); |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 1307 | var result = CanvasKit.SkShader._Color(cPtr, colorSpace); |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1308 | return result; |
| 1309 | } |
| 1310 | |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 1311 | CanvasKit.SkShader.MakeLinearGradient = function(start, end, colors, pos, mode, localMatrix, flags, colorSpace) { |
| 1312 | colorSpace = colorSpace || null |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1313 | var cPtrInfo = copyFlexibleColorArray(colors); |
| 1314 | var posPtr = copy1dArray(pos, "HEAPF32"); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1315 | flags = flags || 0; |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1316 | var localMatrixPtr = copy3x3MatrixToWasm(localMatrix); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1317 | |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1318 | var lgs = CanvasKit._MakeLinearGradientShader(start, end, cPtrInfo.colorPtr, cPtrInfo.colorType, posPtr, |
| 1319 | cPtrInfo.count, mode, flags, localMatrixPtr, colorSpace); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1320 | |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1321 | freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors); |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1322 | pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1323 | return lgs; |
| 1324 | } |
| 1325 | |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 1326 | CanvasKit.SkShader.MakeRadialGradient = function(center, radius, colors, pos, mode, localMatrix, flags, colorSpace) { |
| 1327 | colorSpace = colorSpace || null |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1328 | var cPtrInfo = copyFlexibleColorArray(colors); |
Kevin Lubick | 69e46da | 2020-06-05 07:13:48 -0400 | [diff] [blame] | 1329 | var posPtr = copy1dArray(pos, "HEAPF32"); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1330 | flags = flags || 0; |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1331 | var localMatrixPtr = copy3x3MatrixToWasm(localMatrix); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1332 | |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1333 | var rgs = CanvasKit._MakeRadialGradientShader(center, radius, cPtrInfo.colorPtr, cPtrInfo.colorType, posPtr, |
| 1334 | cPtrInfo.count, mode, flags, localMatrixPtr, colorSpace); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1335 | |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1336 | freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors); |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1337 | pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1338 | return rgs; |
| 1339 | } |
| 1340 | |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 1341 | CanvasKit.SkShader.MakeSweepGradient = function(cx, cy, colors, pos, mode, localMatrix, flags, startAngle, endAngle, colorSpace) { |
| 1342 | colorSpace = colorSpace || null |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1343 | var cPtrInfo = copyFlexibleColorArray(colors); |
Kevin Lubick | 69e46da | 2020-06-05 07:13:48 -0400 | [diff] [blame] | 1344 | var posPtr = copy1dArray(pos, "HEAPF32"); |
Dan Field | 3d44f73 | 2020-03-16 09:17:30 -0700 | [diff] [blame] | 1345 | flags = flags || 0; |
| 1346 | startAngle = startAngle || 0; |
| 1347 | endAngle = endAngle || 360; |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1348 | var localMatrixPtr = copy3x3MatrixToWasm(localMatrix); |
Dan Field | 3d44f73 | 2020-03-16 09:17:30 -0700 | [diff] [blame] | 1349 | |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1350 | var sgs = CanvasKit._MakeSweepGradientShader(cx, cy, cPtrInfo.colorPtr, cPtrInfo.colorType, posPtr, |
| 1351 | cPtrInfo.count, mode, |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1352 | startAngle, endAngle, flags, |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 1353 | localMatrixPtr, colorSpace); |
Dan Field | 3d44f73 | 2020-03-16 09:17:30 -0700 | [diff] [blame] | 1354 | |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1355 | freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors); |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1356 | pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos); |
Dan Field | 3d44f73 | 2020-03-16 09:17:30 -0700 | [diff] [blame] | 1357 | return sgs; |
| 1358 | } |
| 1359 | |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1360 | CanvasKit.SkShader.MakeTwoPointConicalGradient = function(start, startRadius, end, endRadius, |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 1361 | colors, pos, mode, localMatrix, flags, colorSpace) { |
| 1362 | colorSpace = colorSpace || null |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1363 | var cPtrInfo = copyFlexibleColorArray(colors); |
Kevin Lubick | 69e46da | 2020-06-05 07:13:48 -0400 | [diff] [blame] | 1364 | var posPtr = copy1dArray(pos, "HEAPF32"); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1365 | flags = flags || 0; |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1366 | var localMatrixPtr = copy3x3MatrixToWasm(localMatrix); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1367 | |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1368 | var rgs = CanvasKit._MakeTwoPointConicalGradientShader( |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1369 | start, startRadius, end, endRadius, cPtrInfo.colorPtr, cPtrInfo.colorType, |
| 1370 | posPtr, cPtrInfo.count, mode, flags, localMatrixPtr, colorSpace); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1371 | |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1372 | freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors); |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1373 | pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1374 | return rgs; |
| 1375 | } |
| 1376 | |
| 1377 | // temporary support for deprecated names. |
Nathaniel Nifong | c8f95e2 | 2020-03-09 11:52:51 -0400 | [diff] [blame] | 1378 | CanvasKit.MakeSkDashPathEffect = CanvasKit.SkPathEffect.MakeDash; |
| 1379 | CanvasKit.MakeLinearGradientShader = CanvasKit.SkShader.MakeLinearGradient; |
| 1380 | CanvasKit.MakeRadialGradientShader = CanvasKit.SkShader.MakeRadialGradient; |
| 1381 | CanvasKit.MakeTwoPointConicalGradientShader = CanvasKit.SkShader.MakeTwoPointConicalGradient; |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1382 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1383 | // Run through the JS files that are added at compile time. |
| 1384 | if (CanvasKit._extraInitializations) { |
| 1385 | CanvasKit._extraInitializations.forEach(function(init) { |
| 1386 | init(); |
| 1387 | }); |
Kevin Lubick | eb2f6b0 | 2018-11-29 15:07:02 -0500 | [diff] [blame] | 1388 | } |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1389 | }; // end CanvasKit.onRuntimeInitialized, that is, anything changing prototypes or dynamic. |
Kevin Lubick | eb2f6b0 | 2018-11-29 15:07:02 -0500 | [diff] [blame] | 1390 | |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1391 | // Accepts an object holding two canvaskit colors. |
| 1392 | // { |
| 1393 | // ambient: {r, g, b, a}, |
| 1394 | // spot: {r, g, b, a}, |
| 1395 | // } |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1396 | // Returns the same format. Note, if malloced colors are passed in, the memory |
| 1397 | // housing the passed in colors passed in will be overwritten with the computed |
| 1398 | // tonal colors. |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1399 | CanvasKit.computeTonalColors = function(tonalColors) { |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1400 | // copy the colors into WASM |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1401 | var cPtrAmbi = copyColorToWasmNoScratch(tonalColors['ambient']); |
| 1402 | var cPtrSpot = copyColorToWasmNoScratch(tonalColors['spot']); |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1403 | // The output of this function will be the same pointers we passed in. |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1404 | this._computeTonalColors(cPtrAmbi, cPtrSpot); |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1405 | // Read the results out. |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1406 | var result = { |
| 1407 | 'ambient': copyColorFromWasm(cPtrAmbi), |
| 1408 | 'spot': copyColorFromWasm(cPtrSpot), |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 1409 | }; |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1410 | // If the user passed us malloced colors in here, we don't want to clean them up. |
| 1411 | freeArraysThatAreNotMallocedByUsers(cPtrAmbi, tonalColors['ambient']); |
| 1412 | freeArraysThatAreNotMallocedByUsers(cPtrSpot, tonalColors['spot']); |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1413 | return result; |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 1414 | }; |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1415 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1416 | CanvasKit.LTRBRect = function(l, t, r, b) { |
| 1417 | return { |
| 1418 | fLeft: l, |
| 1419 | fTop: t, |
| 1420 | fRight: r, |
| 1421 | fBottom: b, |
| 1422 | }; |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 1423 | }; |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 1424 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1425 | CanvasKit.XYWHRect = function(x, y, w, h) { |
| 1426 | return { |
| 1427 | fLeft: x, |
| 1428 | fTop: y, |
| 1429 | fRight: x+w, |
| 1430 | fBottom: y+h, |
| 1431 | }; |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 1432 | }; |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 1433 | |
Kevin Lubick | 7d644e1 | 2019-09-11 14:22:22 -0400 | [diff] [blame] | 1434 | // RRectXY returns an RRect with the given rect and a radiusX and radiusY for |
| 1435 | // all 4 corners. |
| 1436 | CanvasKit.RRectXY = function(rect, rx, ry) { |
| 1437 | return { |
| 1438 | rect: rect, |
| 1439 | rx1: rx, |
| 1440 | ry1: ry, |
| 1441 | rx2: rx, |
| 1442 | ry2: ry, |
| 1443 | rx3: rx, |
| 1444 | ry3: ry, |
| 1445 | rx4: rx, |
| 1446 | ry4: ry, |
| 1447 | }; |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 1448 | }; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1449 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1450 | // 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] | 1451 | CanvasKit.MakeAnimatedImageFromEncoded = function(data) { |
| 1452 | data = new Uint8Array(data); |
| 1453 | |
| 1454 | var iptr = CanvasKit._malloc(data.byteLength); |
| 1455 | CanvasKit.HEAPU8.set(data, iptr); |
| 1456 | var img = CanvasKit._decodeAnimatedImage(iptr, data.byteLength); |
| 1457 | if (!img) { |
| 1458 | SkDebug('Could not decode animated image'); |
| 1459 | return null; |
| 1460 | } |
| 1461 | return img; |
| 1462 | } |
| 1463 | |
| 1464 | // 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] | 1465 | CanvasKit.MakeImageFromEncoded = function(data) { |
| 1466 | data = new Uint8Array(data); |
| 1467 | |
| 1468 | var iptr = CanvasKit._malloc(data.byteLength); |
| 1469 | CanvasKit.HEAPU8.set(data, iptr); |
| 1470 | var img = CanvasKit._decodeImage(iptr, data.byteLength); |
| 1471 | if (!img) { |
| 1472 | SkDebug('Could not decode image'); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1473 | return null; |
| 1474 | } |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1475 | return img; |
| 1476 | } |
| 1477 | |
Elliot Evans | 2879619 | 2020-06-15 12:53:27 -0600 | [diff] [blame] | 1478 | // A variable to hold a canvasElement which can be reused once created the first time. |
| 1479 | var memoizedCanvas2dElement = null; |
| 1480 | |
| 1481 | // Alternative to CanvasKit.MakeImageFromEncoded. Allows for CanvasKit users to take advantage of |
| 1482 | // browser APIs to decode images instead of using codecs included in the CanvasKit wasm binary. |
| 1483 | // Expects that the canvasImageSource has already loaded/decoded. |
| 1484 | // CanvasImageSource reference: https://developer.mozilla.org/en-US/docs/Web/API/CanvasImageSource |
| 1485 | CanvasKit.MakeImageFromCanvasImageSource = function(canvasImageSource) { |
| 1486 | var width = canvasImageSource.width; |
| 1487 | var height = canvasImageSource.height; |
| 1488 | |
| 1489 | if (!memoizedCanvas2dElement) { |
| 1490 | memoizedCanvas2dElement = document.createElement('canvas'); |
| 1491 | } |
| 1492 | memoizedCanvas2dElement.width = width; |
| 1493 | memoizedCanvas2dElement.height = height; |
| 1494 | |
| 1495 | var ctx2d = memoizedCanvas2dElement.getContext('2d'); |
| 1496 | ctx2d.drawImage(canvasImageSource, 0, 0); |
| 1497 | |
| 1498 | var imageData = ctx2d.getImageData(0, 0, width, height); |
| 1499 | |
| 1500 | return CanvasKit.MakeImage( |
| 1501 | imageData.data, |
| 1502 | width, |
| 1503 | height, |
| 1504 | CanvasKit.AlphaType.Unpremul, |
| 1505 | CanvasKit.ColorType.RGBA_8888, |
| 1506 | CanvasKit.SkColorSpace.SRGB |
| 1507 | ); |
| 1508 | } |
| 1509 | |
| 1510 | // pixels may be any Typed Array, but Uint8Array or Uint8ClampedArray is recommended, |
| 1511 | // with bytes representing the pixel values. |
Kevin Lubick | eda0b43 | 2019-12-02 08:26:48 -0500 | [diff] [blame] | 1512 | // (e.g. each set of 4 bytes could represent RGBA values for a single pixel). |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 1513 | CanvasKit.MakeImage = function(pixels, width, height, alphaType, colorType, colorSpace) { |
Kevin Lubick | eda0b43 | 2019-12-02 08:26:48 -0500 | [diff] [blame] | 1514 | var bytesPerPixel = pixels.length / (width * height); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1515 | var info = { |
| 1516 | 'width': width, |
| 1517 | 'height': height, |
| 1518 | 'alphaType': alphaType, |
| 1519 | 'colorType': colorType, |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 1520 | 'colorSpace': colorSpace, |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1521 | }; |
Kevin Lubick | 69e46da | 2020-06-05 07:13:48 -0400 | [diff] [blame] | 1522 | var pptr = copy1dArray(pixels, "HEAPU8"); |
Kevin Lubick | eda0b43 | 2019-12-02 08:26:48 -0500 | [diff] [blame] | 1523 | // No need to _free pptr, Image takes it with SkData::MakeFromMalloc |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1524 | |
Kevin Lubick | eda0b43 | 2019-12-02 08:26:48 -0500 | [diff] [blame] | 1525 | return CanvasKit._MakeImage(info, pptr, pixels.length, width * bytesPerPixel); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1526 | } |
| 1527 | |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1528 | // Colors may be a Uint32Array of int colors, a Flat Float32Array of float colors |
| 1529 | // or a 2d Array of Float32Array(4) (deprecated) |
| 1530 | // the underlying skia function accepts only int colors so it is recommended |
| 1531 | // to pass an array of int colors to avoid an extra conversion. |
| 1532 | // SkColorBuilder is not accepted. |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1533 | CanvasKit.MakeSkVertices = function(mode, positions, textureCoordinates, colors, |
Mike Reed | 5caf935 | 2020-03-02 14:57:09 -0500 | [diff] [blame] | 1534 | indices, isVolatile) { |
Kevin Lubick | b3574c9 | 2019-03-06 08:25:36 -0500 | [diff] [blame] | 1535 | // Default isVolitile to true if not set |
| 1536 | isVolatile = isVolatile === undefined ? true : isVolatile; |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1537 | var idxCount = (indices && indices.length) || 0; |
| 1538 | |
| 1539 | var flags = 0; |
| 1540 | // These flags are from SkVertices.h and should be kept in sync with those. |
| 1541 | if (textureCoordinates && textureCoordinates.length) { |
| 1542 | flags |= (1 << 0); |
| 1543 | } |
| 1544 | if (colors && colors.length) { |
| 1545 | flags |= (1 << 1); |
| 1546 | } |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1547 | if (!isVolatile) { |
Mike Reed | 5caf935 | 2020-03-02 14:57:09 -0500 | [diff] [blame] | 1548 | flags |= (1 << 2); |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1549 | } |
| 1550 | |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1551 | var builder = new CanvasKit._SkVerticesBuilder(mode, positions.length, idxCount, flags); |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1552 | |
Kevin Lubick | 69e46da | 2020-06-05 07:13:48 -0400 | [diff] [blame] | 1553 | copy2dArray(positions, "HEAPF32", builder.positions()); |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1554 | if (builder.texCoords()) { |
Kevin Lubick | 69e46da | 2020-06-05 07:13:48 -0400 | [diff] [blame] | 1555 | copy2dArray(textureCoordinates, "HEAPF32", builder.texCoords()); |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1556 | } |
| 1557 | if (builder.colors()) { |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1558 | if (colors.build) { |
| 1559 | throw('Color builder not accepted by MakeSkVertices, use array of ints'); |
| 1560 | } else { |
| 1561 | copy1dArray(assureIntColors(colors), "HEAPU32", builder.colors()); |
| 1562 | } |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1563 | } |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1564 | if (builder.indices()) { |
Kevin Lubick | 69e46da | 2020-06-05 07:13:48 -0400 | [diff] [blame] | 1565 | copy1dArray(indices, "HEAPU16", builder.indices()); |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1566 | } |
Kevin Lubick | b3574c9 | 2019-03-06 08:25:36 -0500 | [diff] [blame] | 1567 | |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1568 | // Create the vertices, which owns the memory that the builder had allocated. |
| 1569 | return builder.detach(); |
Kevin Lubick | a4f218d | 2020-01-14 08:39:09 -0500 | [diff] [blame] | 1570 | }; |