Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1 | // helper JS that could be used anywhere in the glue code |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 2 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 3 | function clamp(c) { |
| 4 | return Math.round(Math.max(0, Math.min(c || 0, 255))); |
| 5 | } |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 6 | |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 7 | // Constructs a Color with the same API as CSS's rgba(), that is |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 8 | // r,g,b are 0-255, and a is 0.0 to 1.0. |
| 9 | // if a is omitted, it will be assumed to be 1.0 |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 10 | // Internally, Colors are a TypedArray of four unpremultiplied 32-bit floats: a, r, g, b |
| 11 | // In order to construct one with more precision or in a wider gamut, use |
| 12 | // CanvasKit.Color4f |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 13 | CanvasKit.Color = function(r, g, b, a) { |
| 14 | if (a === undefined) { |
| 15 | a = 1; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 16 | } |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 17 | return CanvasKit.Color4f(clamp(r)/255, clamp(g)/255, clamp(b)/255, a); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 18 | } |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 19 | |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 20 | // Construct a 4-float color. |
| 21 | // Opaque if opacity is omitted. |
| 22 | CanvasKit.Color4f = function(r, g, b, a) { |
| 23 | if (a === undefined) { |
| 24 | a = 1; |
| 25 | } |
| 26 | return Float32Array.of(r, g, b, a); |
| 27 | } |
| 28 | |
| 29 | // Color constants use property getters to prevent other code from accidentally |
| 30 | // changing them. |
| 31 | Object.defineProperty(CanvasKit, "TRANSPARENT", { |
| 32 | get: function() { return CanvasKit.Color4f(0, 0, 0, 0); } |
| 33 | }); |
| 34 | Object.defineProperty(CanvasKit, "BLACK", { |
| 35 | get: function() { return CanvasKit.Color4f(0, 0, 0, 1); } |
| 36 | }); |
| 37 | Object.defineProperty(CanvasKit, "WHITE", { |
| 38 | get: function() { return CanvasKit.Color4f(1, 1, 1, 1); } |
| 39 | }); |
| 40 | Object.defineProperty(CanvasKit, "RED", { |
| 41 | get: function() { return CanvasKit.Color4f(1, 0, 0, 1); } |
| 42 | }); |
| 43 | Object.defineProperty(CanvasKit, "GREEN", { |
| 44 | get: function() { return CanvasKit.Color4f(0, 1, 0, 1); } |
| 45 | }); |
| 46 | Object.defineProperty(CanvasKit, "BLUE", { |
| 47 | get: function() { return CanvasKit.Color4f(0, 0, 1, 1); } |
| 48 | }); |
| 49 | Object.defineProperty(CanvasKit, "YELLOW", { |
| 50 | get: function() { return CanvasKit.Color4f(1, 1, 0, 1); } |
| 51 | }); |
| 52 | Object.defineProperty(CanvasKit, "CYAN", { |
| 53 | get: function() { return CanvasKit.Color4f(0, 1, 1, 1); } |
| 54 | }); |
| 55 | Object.defineProperty(CanvasKit, "MAGENTA", { |
| 56 | get: function() { return CanvasKit.Color4f(1, 0, 1, 1); } |
| 57 | }); |
| 58 | |
| 59 | // returns a css style [r, g, b, a] from a CanvasKit.Color |
| 60 | // where r, g, b are returned as ints in the range [0, 255] |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 61 | // where a is scaled between 0 and 1.0 |
| 62 | CanvasKit.getColorComponents = function(color) { |
| 63 | return [ |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 64 | Math.floor(color[0]*255), |
| 65 | Math.floor(color[1]*255), |
| 66 | Math.floor(color[2]*255), |
| 67 | color[3] |
| 68 | ]; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 69 | } |
| 70 | |
Kevin Lubick | 3928466 | 2020-02-20 10:29:55 -0500 | [diff] [blame] | 71 | // parseColorString takes in a CSS color value and returns a CanvasKit.Color |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 72 | // (which is an array of 4 floats in RGBA order). An optional colorMap |
| 73 | // may be provided which maps custom strings to values. |
Kevin Lubick | 3928466 | 2020-02-20 10:29:55 -0500 | [diff] [blame] | 74 | // In the CanvasKit canvas2d shim layer, we provide this map for processing |
| 75 | // canvas2d calls, but not here for code size reasons. |
| 76 | CanvasKit.parseColorString = function(colorStr, colorMap) { |
| 77 | colorStr = colorStr.toLowerCase(); |
| 78 | // See https://drafts.csswg.org/css-color/#typedef-hex-color |
| 79 | if (colorStr.startsWith('#')) { |
| 80 | var r, g, b, a = 255; |
| 81 | switch (colorStr.length) { |
| 82 | case 9: // 8 hex chars #RRGGBBAA |
| 83 | a = parseInt(colorStr.slice(7, 9), 16); |
| 84 | case 7: // 6 hex chars #RRGGBB |
| 85 | r = parseInt(colorStr.slice(1, 3), 16); |
| 86 | g = parseInt(colorStr.slice(3, 5), 16); |
| 87 | b = parseInt(colorStr.slice(5, 7), 16); |
| 88 | break; |
| 89 | case 5: // 4 hex chars #RGBA |
| 90 | // multiplying by 17 is the same effect as |
| 91 | // appending another character of the same value |
| 92 | // e.g. e => ee == 14 => 238 |
| 93 | a = parseInt(colorStr.slice(4, 5), 16) * 17; |
| 94 | case 4: // 6 hex chars #RGB |
| 95 | r = parseInt(colorStr.slice(1, 2), 16) * 17; |
| 96 | g = parseInt(colorStr.slice(2, 3), 16) * 17; |
| 97 | b = parseInt(colorStr.slice(3, 4), 16) * 17; |
| 98 | break; |
| 99 | } |
| 100 | return CanvasKit.Color(r, g, b, a/255); |
| 101 | |
| 102 | } else if (colorStr.startsWith('rgba')) { |
| 103 | // Trim off rgba( and the closing ) |
| 104 | colorStr = colorStr.slice(5, -1); |
| 105 | var nums = colorStr.split(','); |
| 106 | return CanvasKit.Color(+nums[0], +nums[1], +nums[2], |
| 107 | valueOrPercent(nums[3])); |
| 108 | } else if (colorStr.startsWith('rgb')) { |
| 109 | // Trim off rgba( and the closing ) |
| 110 | colorStr = colorStr.slice(4, -1); |
| 111 | var nums = colorStr.split(','); |
| 112 | // rgb can take 3 or 4 arguments |
| 113 | return CanvasKit.Color(+nums[0], +nums[1], +nums[2], |
| 114 | valueOrPercent(nums[3])); |
| 115 | } else if (colorStr.startsWith('gray(')) { |
| 116 | // TODO |
| 117 | } else if (colorStr.startsWith('hsl')) { |
| 118 | // TODO |
| 119 | } else if (colorMap) { |
| 120 | // Try for named color |
| 121 | var nc = colorMap[colorStr]; |
| 122 | if (nc !== undefined) { |
| 123 | return nc; |
| 124 | } |
| 125 | } |
| 126 | SkDebug('unrecognized color ' + colorStr); |
| 127 | return CanvasKit.BLACK; |
| 128 | } |
| 129 | |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 130 | function isCanvasKitColor(ob) { |
| 131 | if (!ob) { |
| 132 | return false; |
| 133 | } |
| 134 | return (ob.constructor === Float32Array && ob.length === 4); |
| 135 | } |
| 136 | |
| 137 | // Warning information is lost by this conversion |
| 138 | function toUint32Color(c) { |
| 139 | return ((clamp(c[3]*255) << 24) | (clamp(c[0]*255) << 16) | (clamp(c[1]*255) << 8) | (clamp(c[2]*255) << 0)) >>> 0; |
| 140 | } |
| 141 | function uIntColorToCanvasKitColor(c) { |
| 142 | return CanvasKit.Color( |
| 143 | (c >> 16) & 0xFF, |
| 144 | (c >> 8) & 0xFF, |
| 145 | (c >> 0) & 0xFF, |
| 146 | ((c >> 24) & 0xFF) / 255 |
| 147 | ); |
| 148 | } |
| 149 | |
Kevin Lubick | 3928466 | 2020-02-20 10:29:55 -0500 | [diff] [blame] | 150 | function valueOrPercent(aStr) { |
| 151 | if (aStr === undefined) { |
| 152 | return 1; // default to opaque. |
| 153 | } |
| 154 | var a = parseFloat(aStr); |
| 155 | if (aStr && aStr.indexOf('%') !== -1) { |
| 156 | return a / 100; |
| 157 | } |
| 158 | return a; |
| 159 | } |
| 160 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 161 | CanvasKit.multiplyByAlpha = function(color, alpha) { |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 162 | // make a copy of the color so the function remains pure. |
| 163 | var result = color.slice(); |
| 164 | result[3] = Math.max(0, Math.min(result[3] * alpha, 1)); |
| 165 | return result; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 166 | } |
Kevin Lubick | 61ef7b2 | 2018-11-27 13:26:59 -0500 | [diff] [blame] | 167 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 168 | function radiansToDegrees(rad) { |
| 169 | return (rad / Math.PI) * 180; |
| 170 | } |
Kevin Lubick | 12c0e50 | 2018-11-28 12:51:56 -0500 | [diff] [blame] | 171 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 172 | function degreesToRadians(deg) { |
| 173 | return (deg / 180) * Math.PI; |
| 174 | } |
Kevin Lubick | 12c0e50 | 2018-11-28 12:51:56 -0500 | [diff] [blame] | 175 | |
| 176 | // See https://stackoverflow.com/a/31090240 |
| 177 | // This contraption keeps closure from minifying away the check |
| 178 | // if btoa is defined *and* prevents runtime "btoa" or "window" is not defined. |
| 179 | // Defined outside any scopes to make it available in all files. |
| 180 | var isNode = !(new Function("try {return this===window;}catch(e){ return false;}")()); |
Kevin Lubick | 1646e7d | 2018-12-07 13:03:08 -0500 | [diff] [blame] | 181 | |
| 182 | function almostEqual(floata, floatb) { |
| 183 | return Math.abs(floata - floatb) < 0.00001; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | |
| 187 | var nullptr = 0; // emscripten doesn't like to take null as uintptr_t |
| 188 | |
| 189 | // arr can be a normal JS array or a TypedArray |
| 190 | // dest is something like CanvasKit.HEAPF32 |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 191 | // ptr can be optionally provided if the memory was already allocated. |
| 192 | function copy1dArray(arr, dest, ptr) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 193 | if (!arr || !arr.length) { |
| 194 | return nullptr; |
| 195 | } |
Kevin Lubick | e25df6c | 2019-10-22 09:04:32 -0400 | [diff] [blame] | 196 | // This was created with CanvasKit.Malloc, so it's already been copied. |
| 197 | if (arr['_ck']) { |
| 198 | return arr.byteOffset; |
| 199 | } |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 200 | if (!ptr) { |
| 201 | ptr = CanvasKit._malloc(arr.length * dest.BYTES_PER_ELEMENT); |
| 202 | } |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 203 | // In c++ terms, the WASM heap is a uint8_t*, a long buffer/array of single |
| 204 | // byte elements. When we run _malloc, we always get an offset/pointer into |
| 205 | // that block of memory. |
| 206 | // CanvasKit exposes some different views to make it easier to work with |
| 207 | // different types. HEAPF32 for example, exposes it as a float* |
| 208 | // However, to make the ptr line up, we have to do some pointer arithmetic. |
| 209 | // Concretely, we need to convert ptr to go from an index into a 1-byte-wide |
| 210 | // buffer to an index into a 4-byte-wide buffer (in the case of HEAPF32) |
| 211 | // and thus we divide ptr by 4. |
| 212 | dest.set(arr, ptr / dest.BYTES_PER_ELEMENT); |
| 213 | return ptr; |
| 214 | } |
| 215 | |
| 216 | // arr should be a non-jagged 2d JS array (TypedArrays can't be nested |
Kevin Lubick | 37ab53e | 2019-11-11 10:06:08 -0500 | [diff] [blame] | 217 | // inside themselves). A common use case is points. |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 218 | // dest is something like CanvasKit.HEAPF32 |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 219 | // ptr can be optionally provided if the memory was already allocated. |
| 220 | function copy2dArray(arr, dest, ptr) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 221 | if (!arr || !arr.length) { |
| 222 | return nullptr; |
| 223 | } |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 224 | if (!ptr) { |
| 225 | ptr = CanvasKit._malloc(arr.length * arr[0].length * dest.BYTES_PER_ELEMENT); |
| 226 | } |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 227 | var idx = 0; |
| 228 | var adjustedPtr = ptr / dest.BYTES_PER_ELEMENT; |
| 229 | for (var r = 0; r < arr.length; r++) { |
| 230 | for (var c = 0; c < arr[0].length; c++) { |
| 231 | dest[adjustedPtr + idx] = arr[r][c]; |
| 232 | idx++; |
| 233 | } |
| 234 | } |
| 235 | return ptr; |
| 236 | } |
| 237 | |
| 238 | // arr should be a non-jagged 3d JS array (TypedArrays can't be nested |
| 239 | // inside themselves.) |
| 240 | // dest is something like CanvasKit.HEAPF32 |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 241 | // ptr can be optionally provided if the memory was already allocated. |
| 242 | function copy3dArray(arr, dest, ptr) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 243 | if (!arr || !arr.length || !arr[0].length) { |
| 244 | return nullptr; |
| 245 | } |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 246 | if (!ptr) { |
| 247 | ptr = CanvasKit._malloc(arr.length * arr[0].length * arr[0][0].length * dest.BYTES_PER_ELEMENT); |
| 248 | } |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 249 | var idx = 0; |
| 250 | var adjustedPtr = ptr / dest.BYTES_PER_ELEMENT; |
| 251 | for (var x = 0; x < arr.length; x++) { |
| 252 | for (var y = 0; y < arr[0].length; y++) { |
| 253 | for (var z = 0; z < arr[0][0].length; z++) { |
| 254 | dest[adjustedPtr + idx] = arr[x][y][z]; |
| 255 | idx++; |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | return ptr; |
| 260 | } |
| 261 | |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 262 | var defaultPerspective = Float32Array.of(0, 0, 1); |
| 263 | |
| 264 | // Copies the given DOMMatrix/Array/TypedArray to the CanvasKit heap and |
| 265 | // returns a pointer to the memory. This memory is a float* of length 9. |
| 266 | // If the passed in matrix is null/undefined, we return 0 (nullptr). All calls |
| 267 | // on the C++ side should check for nullptr where appropriate. It is generally |
| 268 | // the responsibility of the JS side code to call CanvasKit._free on the |
| 269 | // allocated memory before returning to the user code. |
| 270 | function copy3x3MatrixToWasm(matr) { |
| 271 | if (!matr) { |
| 272 | return nullptr; |
| 273 | } |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame^] | 274 | if (matr['_ck']) { |
| 275 | return matr.byteOffset; |
| 276 | } |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 277 | var mPtr = CanvasKit._malloc(9 * 4); // 9 matrix scalars, each at 4 bytes. |
| 278 | if (matr.length) { |
| 279 | // TODO(kjlubick): Downsample a 16 length (4x4 matrix) |
| 280 | if (matr.length !== 6 && matr.length !== 9) { |
| 281 | throw 'invalid matrix size'; |
| 282 | } |
| 283 | // This should be an array or typed array. |
| 284 | // have to divide the pointer by 4 to "cast" it from bytes to float. |
| 285 | CanvasKit.HEAPF32.set(matr, mPtr / 4); |
| 286 | if (matr.length === 6) { |
| 287 | CanvasKit.HEAPF32.set(defaultPerspective, 6 + mPtr / 4); |
| 288 | } |
| 289 | } else { |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 290 | // Try as if it's a DOMMatrix. Reminder that DOMMatrix is column-major. |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 291 | var floats = Float32Array.of( |
| 292 | matr.m11, matr.m21, matr.m41, |
| 293 | matr.m12, matr.m22, matr.m42, |
| 294 | matr.m14, matr.m24, matr.m44); |
| 295 | // have to divide the pointer by 4 to "cast" it from bytes to float. |
| 296 | CanvasKit.HEAPF32.set(floats, mPtr / 4); |
| 297 | } |
| 298 | return mPtr; |
| 299 | } |
| 300 | |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 301 | function copy4x4MatrixToWasm(matr) { |
| 302 | if (!matr) { |
| 303 | return nullptr; |
| 304 | } |
| 305 | var mPtr = CanvasKit._malloc(16 * 4); // 9 matrix scalars, each at 4 bytes. |
| 306 | if (matr.length) { |
| 307 | if (matr.length !== 16 && matr.length !== 6 && matr.length !== 9) { |
| 308 | throw 'invalid matrix size'; |
| 309 | } |
| 310 | if (matr.length === 16) { |
| 311 | // This should be an array or typed array. |
| 312 | // have to divide the pointer by 4 to "cast" it from bytes to float. |
| 313 | CanvasKit.HEAPF32.set(matr, mPtr / 4); |
| 314 | } else { |
| 315 | // Upscale the row-major 3x3 or 3x2 matrix into a 4x4 row-major matrix |
| 316 | // TODO(skbug.com/10108) This will need to change when we convert our |
| 317 | // JS 4x4 to be column-major. |
| 318 | var floats = Float32Array.of( |
| 319 | matr[0], matr[1], 0, matr[2], |
| 320 | matr[3], matr[4], 0, matr[5], |
| 321 | 0, 0, 0, 0, |
| 322 | matr[6], matr[7], 0, matr[8]); |
| 323 | if (matr.length === 6) { |
| 324 | // fix perspective for the 3x2 case (from above, they will be undefined). |
| 325 | floats[4*3+0]=0; |
| 326 | floats[4*3+1]=0; |
| 327 | floats[4*3+3]=1; |
| 328 | } |
| 329 | CanvasKit.HEAPF32.set(floats, mPtr / 4); |
| 330 | } |
| 331 | } else { |
| 332 | // Try as if it's a DOMMatrix. Reminder that DOMMatrix is column-major. |
| 333 | // TODO(skbug.com/10108) use toFloat32Array(). |
| 334 | var floats = Float32Array.of( |
| 335 | matr.m11, matr.m21, matr.m31, matr.m41, |
| 336 | matr.m12, matr.m22, matr.m32, matr.m42, |
| 337 | matr.m13, matr.m23, matr.m33, matr.m43, |
| 338 | matr.m14, matr.m24, matr.m34, matr.m44); |
| 339 | // have to divide the pointer by 4 to "cast" it from bytes to float. |
| 340 | CanvasKit.HEAPF32.set(floats, mPtr / 4); |
| 341 | } |
| 342 | return mPtr; |
| 343 | } |
| 344 | |
| 345 | // copies a 4x4 matrix at the given pointer into a JS array. |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame^] | 346 | // TODO(kjlubick) Add a scratch array for this. |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 347 | function copy4x4MatrixFromWasm(matrPtr) { |
| 348 | // read them out into an array. TODO(kjlubick): If we change SkMatrix to be |
| 349 | // typedArrays, then we should return a typed array here too. |
| 350 | var rv = new Array(16); |
| 351 | for (var i = 0; i < 16; i++) { |
| 352 | rv[i] = CanvasKit.HEAPF32[matrPtr/4 + i]; // divide by 4 to "cast" to float. |
| 353 | } |
| 354 | CanvasKit._free(matrPtr); |
| 355 | return rv; |
| 356 | } |
| 357 | |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 358 | // copies the four floats at the given pointer in a js Float32Array |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame^] | 359 | // TODO(kjlubick) Add a scratch array for this. |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 360 | function copyColorFromWasm(colorPtr) { |
| 361 | var rv = new Float32Array(4); |
| 362 | for (var i = 0; i < 4; i++) { |
| 363 | rv[i] = CanvasKit.HEAPF32[colorPtr/4 + i]; // divide by 4 to "cast" to float. |
| 364 | } |
| 365 | CanvasKit._free(colorPtr); |
| 366 | return rv; |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame^] | 367 | } |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 368 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 369 | // Caching the Float32Arrays can save having to reallocate them |
| 370 | // over and over again. |
| 371 | var Float32ArrayCache = {}; |
| 372 | |
| 373 | // Takes a 2D array of commands and puts them into the WASM heap |
| 374 | // as a 1D array. This allows them to referenced from the C++ code. |
| 375 | // Returns a 2 element array, with the first item being essentially a |
| 376 | // pointer to the array and the second item being the length of |
| 377 | // the new 1D array. |
| 378 | // |
| 379 | // Example usage: |
| 380 | // let cmds = [ |
| 381 | // [CanvasKit.MOVE_VERB, 0, 10], |
| 382 | // [CanvasKit.LINE_VERB, 30, 40], |
| 383 | // [CanvasKit.QUAD_VERB, 20, 50, 45, 60], |
| 384 | // ]; |
| 385 | function loadCmdsTypedArray(arr) { |
| 386 | var len = 0; |
| 387 | for (var r = 0; r < arr.length; r++) { |
| 388 | len += arr[r].length; |
| 389 | } |
| 390 | |
| 391 | var ta; |
| 392 | if (Float32ArrayCache[len]) { |
| 393 | ta = Float32ArrayCache[len]; |
| 394 | } else { |
| 395 | ta = new Float32Array(len); |
| 396 | Float32ArrayCache[len] = ta; |
| 397 | } |
| 398 | // Flatten into a 1d array |
| 399 | var i = 0; |
| 400 | for (var r = 0; r < arr.length; r++) { |
| 401 | for (var c = 0; c < arr[r].length; c++) { |
| 402 | var item = arr[r][c]; |
| 403 | ta[i] = item; |
| 404 | i++; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | var ptr = copy1dArray(ta, CanvasKit.HEAPF32); |
| 409 | return [ptr, len]; |
| 410 | } |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 411 | |
Kevin Lubick | cc13fd3 | 2019-04-05 13:00:01 -0400 | [diff] [blame] | 412 | function saveBytesToFile(bytes, fileName) { |
| 413 | if (!isNode) { |
| 414 | // https://stackoverflow.com/a/32094834 |
| 415 | var blob = new Blob([bytes], {type: 'application/octet-stream'}); |
| 416 | url = window.URL.createObjectURL(blob); |
| 417 | var a = document.createElement('a'); |
| 418 | document.body.appendChild(a); |
| 419 | a.href = url; |
| 420 | a.download = fileName; |
| 421 | a.click(); |
| 422 | // clean up after because FF might not download it synchronously |
| 423 | setTimeout(function() { |
| 424 | URL.revokeObjectURL(url); |
| 425 | a.remove(); |
| 426 | }, 50); |
| 427 | } else { |
| 428 | var fs = require('fs'); |
| 429 | // https://stackoverflow.com/a/42006750 |
| 430 | // https://stackoverflow.com/a/47018122 |
| 431 | fs.writeFile(fileName, new Buffer(bytes), function(err) { |
| 432 | if (err) throw err; |
| 433 | }); |
| 434 | } |
| 435 | } |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 436 | /** |
| 437 | * Generic helper for dealing with an array of four floats. |
| 438 | */ |
| 439 | CanvasKit.FourFloatArrayHelper = function() { |
| 440 | this._floats = []; |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 441 | this._ptr = null; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 442 | |
| 443 | Object.defineProperty(this, 'length', { |
| 444 | enumerable: true, |
| 445 | get: function() { |
| 446 | return this._floats.length / 4; |
| 447 | }, |
| 448 | }); |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /** |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 452 | * push the four floats onto the end of the array - if build() has already |
| 453 | * been called, the call will return without modifying anything. |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 454 | */ |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 455 | CanvasKit.FourFloatArrayHelper.prototype.push = function(f1, f2, f3, f4) { |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 456 | if (this._ptr) { |
| 457 | SkDebug('Cannot push more points - already built'); |
| 458 | return; |
| 459 | } |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 460 | this._floats.push(f1, f2, f3, f4); |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 461 | } |
| 462 | |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 463 | /** |
| 464 | * Set the four floats at a given index - if build() has already |
| 465 | * been called, the WASM memory will be written to directly. |
| 466 | */ |
| 467 | CanvasKit.FourFloatArrayHelper.prototype.set = function(idx, f1, f2, f3, f4) { |
| 468 | if (idx < 0 || idx >= this._floats.length/4) { |
| 469 | SkDebug('Cannot set index ' + idx + ', it is out of range', this._floats.length/4); |
| 470 | return; |
| 471 | } |
| 472 | idx *= 4; |
| 473 | var BYTES_PER_ELEMENT = 4; |
| 474 | if (this._ptr) { |
| 475 | // convert this._ptr from uint8_t* to SkScalar* by dividing by 4 |
| 476 | var floatPtr = (this._ptr / BYTES_PER_ELEMENT) + idx; |
| 477 | CanvasKit.HEAPF32[floatPtr] = f1; |
| 478 | CanvasKit.HEAPF32[floatPtr + 1] = f2; |
| 479 | CanvasKit.HEAPF32[floatPtr + 2] = f3; |
| 480 | CanvasKit.HEAPF32[floatPtr + 3] = f4; |
| 481 | return; |
| 482 | } |
| 483 | this._floats[idx] = f1; |
| 484 | this._floats[idx + 1] = f2; |
| 485 | this._floats[idx + 2] = f3; |
| 486 | this._floats[idx + 3] = f4; |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * Copies the float data to the WASM memory and returns a pointer |
| 491 | * to that allocated memory. Once build has been called, this |
| 492 | * float array cannot be made bigger. |
| 493 | */ |
| 494 | CanvasKit.FourFloatArrayHelper.prototype.build = function() { |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 495 | if (this._ptr) { |
| 496 | return this._ptr; |
| 497 | } |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 498 | this._ptr = copy1dArray(this._floats, CanvasKit.HEAPF32); |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 499 | return this._ptr; |
| 500 | } |
| 501 | |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 502 | /** |
| 503 | * Frees the wasm memory associated with this array. Of note, |
| 504 | * the points are not removed, so push/set/build can all |
| 505 | * be called to make a newly allocated (possibly bigger) |
| 506 | * float array. |
| 507 | */ |
| 508 | CanvasKit.FourFloatArrayHelper.prototype.delete = function() { |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 509 | if (this._ptr) { |
| 510 | CanvasKit._free(this._ptr); |
| 511 | this._ptr = null; |
| 512 | } |
| 513 | } |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 514 | |
| 515 | /** |
| 516 | * Generic helper for dealing with an array of unsigned ints. |
| 517 | */ |
| 518 | CanvasKit.OneUIntArrayHelper = function() { |
| 519 | this._uints = []; |
| 520 | this._ptr = null; |
| 521 | |
| 522 | Object.defineProperty(this, 'length', { |
| 523 | enumerable: true, |
| 524 | get: function() { |
| 525 | return this._uints.length; |
| 526 | }, |
| 527 | }); |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * push the unsigned int onto the end of the array - if build() has already |
| 532 | * been called, the call will return without modifying anything. |
| 533 | */ |
| 534 | CanvasKit.OneUIntArrayHelper.prototype.push = function(u) { |
| 535 | if (this._ptr) { |
| 536 | SkDebug('Cannot push more points - already built'); |
| 537 | return; |
| 538 | } |
| 539 | this._uints.push(u); |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * Set the uint at a given index - if build() has already |
| 544 | * been called, the WASM memory will be written to directly. |
| 545 | */ |
| 546 | CanvasKit.OneUIntArrayHelper.prototype.set = function(idx, u) { |
| 547 | if (idx < 0 || idx >= this._uints.length) { |
| 548 | SkDebug('Cannot set index ' + idx + ', it is out of range', this._uints.length); |
| 549 | return; |
| 550 | } |
| 551 | idx *= 4; |
| 552 | var BYTES_PER_ELEMENT = 4; |
| 553 | if (this._ptr) { |
| 554 | // convert this._ptr from uint8_t* to SkScalar* by dividing by 4 |
| 555 | var uintPtr = (this._ptr / BYTES_PER_ELEMENT) + idx; |
| 556 | CanvasKit.HEAPU32[uintPtr] = u; |
| 557 | return; |
| 558 | } |
| 559 | this._uints[idx] = u; |
| 560 | } |
| 561 | |
| 562 | /** |
| 563 | * Copies the uint data to the WASM memory and returns a pointer |
| 564 | * to that allocated memory. Once build has been called, this |
| 565 | * unit array cannot be made bigger. |
| 566 | */ |
| 567 | CanvasKit.OneUIntArrayHelper.prototype.build = function() { |
| 568 | if (this._ptr) { |
| 569 | return this._ptr; |
| 570 | } |
| 571 | this._ptr = copy1dArray(this._uints, CanvasKit.HEAPU32); |
| 572 | return this._ptr; |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * Frees the wasm memory associated with this array. Of note, |
| 577 | * the points are not removed, so push/set/build can all |
| 578 | * be called to make a newly allocated (possibly bigger) |
| 579 | * uint array. |
| 580 | */ |
| 581 | CanvasKit.OneUIntArrayHelper.prototype.delete = function() { |
| 582 | if (this._ptr) { |
| 583 | CanvasKit._free(this._ptr); |
| 584 | this._ptr = null; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * Helper for building an array of SkRects (which are just structs |
| 590 | * of 4 floats). |
| 591 | * |
| 592 | * It can be more performant to use this helper, as |
| 593 | * the C++-side array is only allocated once (on the first call) |
| 594 | * to build. Subsequent set() operations operate directly on |
| 595 | * the C++-side array, avoiding having to re-allocate (and free) |
| 596 | * the array every time. |
| 597 | * |
| 598 | * Input points are taken as left, top, right, bottom |
| 599 | */ |
| 600 | CanvasKit.SkRectBuilder = CanvasKit.FourFloatArrayHelper; |
| 601 | /** |
| 602 | * Helper for building an array of RSXForms (which are just structs |
| 603 | * of 4 floats). |
| 604 | * |
| 605 | * It can be more performant to use this helper, as |
| 606 | * the C++-side array is only allocated once (on the first call) |
| 607 | * to build. Subsequent set() operations operate directly on |
| 608 | * the C++-side array, avoiding having to re-allocate (and free) |
| 609 | * the array every time. |
| 610 | * |
| 611 | * An RSXForm is a compressed form of a rotation+scale matrix. |
| 612 | * |
| 613 | * [ scos -ssin tx ] |
| 614 | * [ ssin scos ty ] |
| 615 | * [ 0 0 1 ] |
| 616 | * |
| 617 | * Input points are taken as scos, ssin, tx, ty |
| 618 | */ |
| 619 | CanvasKit.RSXFormBuilder = CanvasKit.FourFloatArrayHelper; |
| 620 | |
| 621 | /** |
| 622 | * Helper for building an array of SkColor |
| 623 | * |
| 624 | * It can be more performant to use this helper, as |
| 625 | * the C++-side array is only allocated once (on the first call) |
| 626 | * to build. Subsequent set() operations operate directly on |
| 627 | * the C++-side array, avoiding having to re-allocate (and free) |
| 628 | * the array every time. |
| 629 | */ |
| 630 | CanvasKit.SkColorBuilder = CanvasKit.OneUIntArrayHelper; |
Kevin Lubick | e25df6c | 2019-10-22 09:04:32 -0400 | [diff] [blame] | 631 | |
| 632 | /** |
| 633 | * Malloc returns a TypedArray backed by the C++ memory of the |
| 634 | * given length. It should only be used by advanced users who |
| 635 | * can manage memory and initialize values properly. When used |
| 636 | * correctly, it can save copying of data between JS and C++. |
| 637 | * When used incorrectly, it can lead to memory leaks. |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame^] | 638 | * Any memory allocated by CanvasKit.Malloc needs to be released with CanvasKit.Free. |
Kevin Lubick | e25df6c | 2019-10-22 09:04:32 -0400 | [diff] [blame] | 639 | * |
| 640 | * const ta = CanvasKit.Malloc(Float32Array, 20); |
| 641 | * // store data into ta |
| 642 | * const cf = CanvasKit.SkColorFilter.MakeMatrix(ta); |
Kevin Lubick | e25df6c | 2019-10-22 09:04:32 -0400 | [diff] [blame] | 643 | * |
| 644 | * @param {TypedArray} typedArray - constructor for the typedArray. |
| 645 | * @param {number} len - number of elements to store. |
| 646 | */ |
| 647 | CanvasKit.Malloc = function(typedArray, len) { |
| 648 | var byteLen = len * typedArray.BYTES_PER_ELEMENT; |
| 649 | var ptr = CanvasKit._malloc(byteLen); |
Bryce Thomas | 1fa5404 | 2020-01-14 13:46:30 -0800 | [diff] [blame] | 650 | var ta = new typedArray(CanvasKit.HEAPU8.buffer, ptr, len); |
Kevin Lubick | e25df6c | 2019-10-22 09:04:32 -0400 | [diff] [blame] | 651 | // add a marker that this was allocated in C++ land |
| 652 | ta['_ck'] = true; |
| 653 | return ta; |
| 654 | } |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame^] | 655 | |
| 656 | /** |
| 657 | * Free frees the memory returned by Malloc. |
| 658 | * Any memory allocated by CanvasKit.Malloc needs to be released with CanvasKit.Free. |
| 659 | */ |
| 660 | CanvasKit.Free = function(typedArray) { |
| 661 | CanvasKit._free(typedArray.byteOffset) |
| 662 | } |
| 663 | |
| 664 | // This helper will free the given pointer unless the provided array is one |
| 665 | // that was returned by CanvasKit.Malloc. |
| 666 | function freeArraysThatAreNotMallocedByUsers(ptr, arr) { |
| 667 | if (!arr || !arr['_ck']) { |
| 668 | CanvasKit._free(ptr); |
| 669 | } |
| 670 | } |