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