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