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 | e7c1a73 | 2020-12-04 09:10:39 -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 | e7c1a73 | 2020-12-04 09:10:39 -0500 | [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); |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame^] | 42 | }; |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 43 | |
| 44 | // Color constants use property getters to prevent other code from accidentally |
| 45 | // changing them. |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 46 | Object.defineProperty(CanvasKit, 'TRANSPARENT', { |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 47 | get: function() { return CanvasKit.Color4f(0, 0, 0, 0); } |
| 48 | }); |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 49 | Object.defineProperty(CanvasKit, 'BLACK', { |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 50 | get: function() { return CanvasKit.Color4f(0, 0, 0, 1); } |
| 51 | }); |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 52 | Object.defineProperty(CanvasKit, 'WHITE', { |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 53 | get: function() { return CanvasKit.Color4f(1, 1, 1, 1); } |
| 54 | }); |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 55 | Object.defineProperty(CanvasKit, 'RED', { |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 56 | get: function() { return CanvasKit.Color4f(1, 0, 0, 1); } |
| 57 | }); |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 58 | Object.defineProperty(CanvasKit, 'GREEN', { |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 59 | get: function() { return CanvasKit.Color4f(0, 1, 0, 1); } |
| 60 | }); |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 61 | Object.defineProperty(CanvasKit, 'BLUE', { |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 62 | get: function() { return CanvasKit.Color4f(0, 0, 1, 1); } |
| 63 | }); |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 64 | Object.defineProperty(CanvasKit, 'YELLOW', { |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 65 | get: function() { return CanvasKit.Color4f(1, 1, 0, 1); } |
| 66 | }); |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 67 | Object.defineProperty(CanvasKit, 'CYAN', { |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 68 | get: function() { return CanvasKit.Color4f(0, 1, 1, 1); } |
| 69 | }); |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 70 | Object.defineProperty(CanvasKit, 'MAGENTA', { |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 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 | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame^] | 84 | }; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 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 | } |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 141 | Debug('unrecognized color ' + colorStr); |
Kevin Lubick | 3928466 | 2020-02-20 10:29:55 -0500 | [diff] [blame] | 142 | return CanvasKit.BLACK; |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame^] | 143 | }; |
Kevin Lubick | 3928466 | 2020-02-20 10:29:55 -0500 | [diff] [blame] | 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 | } |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 156 | // Accepts various colors representations and converts them to an array of int colors. |
| 157 | // Does not handle builders. |
| 158 | function assureIntColors(arr) { |
| 159 | if (arr instanceof Float32Array) { |
| 160 | var count = Math.floor(arr.length / 4); |
| 161 | var result = new Uint32Array(count); |
| 162 | for (var i = 0; i < count; i ++) { |
| 163 | result[i] = toUint32Color(arr.slice(i*4, (i+1)*4)); |
| 164 | } |
| 165 | return result; |
| 166 | } else if (arr instanceof Uint32Array) { |
| 167 | return arr; |
| 168 | } else if (arr instanceof Array && arr[0] instanceof Float32Array) { |
| 169 | return arr.map(toUint32Color); |
| 170 | } |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 171 | } |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 172 | |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 173 | function uIntColorToCanvasKitColor(c) { |
| 174 | return CanvasKit.Color( |
| 175 | (c >> 16) & 0xFF, |
| 176 | (c >> 8) & 0xFF, |
| 177 | (c >> 0) & 0xFF, |
| 178 | ((c >> 24) & 0xFF) / 255 |
| 179 | ); |
| 180 | } |
| 181 | |
Kevin Lubick | 3928466 | 2020-02-20 10:29:55 -0500 | [diff] [blame] | 182 | function valueOrPercent(aStr) { |
| 183 | if (aStr === undefined) { |
| 184 | return 1; // default to opaque. |
| 185 | } |
| 186 | var a = parseFloat(aStr); |
| 187 | if (aStr && aStr.indexOf('%') !== -1) { |
| 188 | return a / 100; |
| 189 | } |
| 190 | return a; |
| 191 | } |
| 192 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 193 | CanvasKit.multiplyByAlpha = function(color, alpha) { |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 194 | // make a copy of the color so the function remains pure. |
| 195 | var result = color.slice(); |
| 196 | result[3] = Math.max(0, Math.min(result[3] * alpha, 1)); |
| 197 | return result; |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame^] | 198 | }; |
Kevin Lubick | 61ef7b2 | 2018-11-27 13:26:59 -0500 | [diff] [blame] | 199 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 200 | function radiansToDegrees(rad) { |
| 201 | return (rad / Math.PI) * 180; |
| 202 | } |
Kevin Lubick | 12c0e50 | 2018-11-28 12:51:56 -0500 | [diff] [blame] | 203 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 204 | function degreesToRadians(deg) { |
| 205 | return (deg / 180) * Math.PI; |
| 206 | } |
Kevin Lubick | 12c0e50 | 2018-11-28 12:51:56 -0500 | [diff] [blame] | 207 | |
| 208 | // See https://stackoverflow.com/a/31090240 |
| 209 | // This contraption keeps closure from minifying away the check |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 210 | // if btoa is defined *and* prevents runtime 'btoa' or 'window' is not defined. |
Kevin Lubick | 12c0e50 | 2018-11-28 12:51:56 -0500 | [diff] [blame] | 211 | // Defined outside any scopes to make it available in all files. |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 212 | var isNode = !(new Function('try {return this===window;}catch(e){ return false;}')()); |
Kevin Lubick | 1646e7d | 2018-12-07 13:03:08 -0500 | [diff] [blame] | 213 | |
| 214 | function almostEqual(floata, floatb) { |
| 215 | return Math.abs(floata - floatb) < 0.00001; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 216 | } |
| 217 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 218 | var nullptr = 0; // emscripten doesn't like to take null as uintptr_t |
| 219 | |
| 220 | // arr can be a normal JS array or a TypedArray |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 221 | // dest is a string like 'HEAPU32' that specifies the type the src array |
Kevin Lubick | 9c401e7 | 2020-06-09 14:22:20 -0400 | [diff] [blame] | 222 | // should be copied into. |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 223 | // ptr can be optionally provided if the memory was already allocated. |
| 224 | function copy1dArray(arr, dest, ptr) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 225 | if (!arr || !arr.length) { |
| 226 | return nullptr; |
| 227 | } |
Kevin Lubick | e25df6c | 2019-10-22 09:04:32 -0400 | [diff] [blame] | 228 | // This was created with CanvasKit.Malloc, so it's already been copied. |
| 229 | if (arr['_ck']) { |
| 230 | return arr.byteOffset; |
| 231 | } |
Kevin Lubick | 69e46da | 2020-06-05 07:13:48 -0400 | [diff] [blame] | 232 | var bytesPerElement = CanvasKit[dest].BYTES_PER_ELEMENT; |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 233 | if (!ptr) { |
Kevin Lubick | 69e46da | 2020-06-05 07:13:48 -0400 | [diff] [blame] | 234 | ptr = CanvasKit._malloc(arr.length * bytesPerElement); |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 235 | } |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 236 | // In c++ terms, the WASM heap is a uint8_t*, a long buffer/array of single |
| 237 | // byte elements. When we run _malloc, we always get an offset/pointer into |
| 238 | // that block of memory. |
| 239 | // CanvasKit exposes some different views to make it easier to work with |
| 240 | // different types. HEAPF32 for example, exposes it as a float* |
| 241 | // However, to make the ptr line up, we have to do some pointer arithmetic. |
| 242 | // Concretely, we need to convert ptr to go from an index into a 1-byte-wide |
| 243 | // buffer to an index into a 4-byte-wide buffer (in the case of HEAPF32) |
| 244 | // and thus we divide ptr by 4. |
Kevin Lubick | 69e46da | 2020-06-05 07:13:48 -0400 | [diff] [blame] | 245 | // It is important to make sure we are grabbing the freshest view of the |
| 246 | // memory possible because if we call _malloc and the heap needs to grow, |
| 247 | // the TypedArrayView will no longer be valid. |
| 248 | CanvasKit[dest].set(arr, ptr / bytesPerElement); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 249 | return ptr; |
| 250 | } |
| 251 | |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 252 | // Copies an array of colors to wasm, returning an object with the pointer |
| 253 | // and info necessary to use the copied colors. |
| 254 | // Accepts either a flat Float32Array, flat Uint32Array or Array of Float32Arrays. |
Kevin Lubick | 421ba88 | 2020-10-15 13:07:33 -0400 | [diff] [blame] | 255 | // If color is an object that was allocated with CanvasKit.Malloc, its pointer is |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 256 | // returned and no extra copy is performed. |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 257 | // TODO(nifong): have this accept color builders. |
| 258 | function copyFlexibleColorArray(colors) { |
| 259 | var result = { |
| 260 | colorPtr: nullptr, |
| 261 | count: colors.length, |
| 262 | colorType: CanvasKit.ColorType.RGBA_F32, |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame^] | 263 | }; |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 264 | if (colors instanceof Float32Array) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 265 | result.colorPtr = copy1dArray(colors, 'HEAPF32'); |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 266 | result.count = colors.length / 4; |
| 267 | |
| 268 | } else if (colors instanceof Uint32Array) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 269 | result.colorPtr = copy1dArray(colors, 'HEAPU32'); |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 270 | result.colorType = CanvasKit.ColorType.RGBA_8888; |
| 271 | |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame^] | 272 | } else if (colors instanceof Array) { |
| 273 | result.colorPtr = copyColorArray(colors); |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 274 | } else { |
| 275 | throw('Invalid argument to copyFlexibleColorArray, Not a color array '+typeof(colors)); |
| 276 | } |
| 277 | return result; |
| 278 | } |
| 279 | |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame^] | 280 | function copyColorArray(arr) { |
| 281 | if (!arr || !arr.length) { |
| 282 | return nullptr; |
| 283 | } |
| 284 | // 4 floats per color, 4 bytes per float. |
| 285 | var ptr = CanvasKit._malloc(arr.length * 4 * 4); |
| 286 | |
| 287 | var idx = 0; |
| 288 | var adjustedPtr = ptr / 4; // cast the byte pointer into a float pointer. |
| 289 | for (var r = 0; r < arr.length; r++) { |
| 290 | for (var c = 0; c < 4; c++) { |
| 291 | CanvasKit.HEAPF32[adjustedPtr + idx] = arr[r][c]; |
| 292 | idx++; |
| 293 | } |
| 294 | } |
| 295 | return ptr; |
| 296 | } |
| 297 | |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 298 | var defaultPerspective = Float32Array.of(0, 0, 1); |
| 299 | |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 300 | var _scratch3x3MatrixPtr = nullptr; |
Kevin Lubick | 462a860 | 2020-06-01 15:43:03 -0400 | [diff] [blame] | 301 | var _scratch3x3Matrix; // the result from CanvasKit.Malloc |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 302 | |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 303 | // Copies the given DOMMatrix/Array/TypedArray to the CanvasKit heap and |
| 304 | // returns a pointer to the memory. This memory is a float* of length 9. |
| 305 | // If the passed in matrix is null/undefined, we return 0 (nullptr). All calls |
| 306 | // on the C++ side should check for nullptr where appropriate. It is generally |
| 307 | // the responsibility of the JS side code to call CanvasKit._free on the |
| 308 | // allocated memory before returning to the user code. |
| 309 | function copy3x3MatrixToWasm(matr) { |
| 310 | if (!matr) { |
| 311 | return nullptr; |
| 312 | } |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 313 | |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 314 | if (matr.length) { |
| 315 | // TODO(kjlubick): Downsample a 16 length (4x4 matrix) |
| 316 | if (matr.length !== 6 && matr.length !== 9) { |
| 317 | throw 'invalid matrix size'; |
| 318 | } |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 319 | // matr should be an array or typed array. |
| 320 | var mPtr = copy1dArray(matr, 'HEAPF32', _scratch3x3MatrixPtr); |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 321 | if (matr.length === 6) { |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 322 | // Overwrite the last 3 floats with the default perspective. The divide |
| 323 | // by 4 casts the pointer into a float pointer. |
| 324 | CanvasKit.HEAPF32.set(defaultPerspective, 6 + mPtr / 4); |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 325 | } |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 326 | return mPtr; |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 327 | } |
Kevin Lubick | 462a860 | 2020-06-01 15:43:03 -0400 | [diff] [blame] | 328 | var wasm3x3Matrix = _scratch3x3Matrix['toTypedArray'](); |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 329 | // 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] | 330 | wasm3x3Matrix[0] = matr.m11; |
| 331 | wasm3x3Matrix[1] = matr.m21; |
| 332 | wasm3x3Matrix[2] = matr.m41; |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 333 | |
Kevin Lubick | 462a860 | 2020-06-01 15:43:03 -0400 | [diff] [blame] | 334 | wasm3x3Matrix[3] = matr.m12; |
| 335 | wasm3x3Matrix[4] = matr.m22; |
| 336 | wasm3x3Matrix[5] = matr.m42; |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 337 | |
Kevin Lubick | 462a860 | 2020-06-01 15:43:03 -0400 | [diff] [blame] | 338 | wasm3x3Matrix[6] = matr.m14; |
| 339 | wasm3x3Matrix[7] = matr.m24; |
| 340 | wasm3x3Matrix[8] = matr.m44; |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 341 | return _scratch3x3MatrixPtr; |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 342 | } |
| 343 | |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 344 | var _scratch4x4MatrixPtr = nullptr; |
Kevin Lubick | 462a860 | 2020-06-01 15:43:03 -0400 | [diff] [blame] | 345 | var _scratch4x4Matrix; // the result from CanvasKit.Malloc |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 346 | |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 347 | function copy4x4MatrixToWasm(matr) { |
| 348 | if (!matr) { |
| 349 | return nullptr; |
| 350 | } |
Kevin Lubick | 462a860 | 2020-06-01 15:43:03 -0400 | [diff] [blame] | 351 | var wasm4x4Matrix = _scratch4x4Matrix['toTypedArray'](); |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 352 | if (matr.length) { |
| 353 | if (matr.length !== 16 && matr.length !== 6 && matr.length !== 9) { |
| 354 | throw 'invalid matrix size'; |
| 355 | } |
| 356 | if (matr.length === 16) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 357 | // matr should be an array or typed array. |
| 358 | return copy1dArray(matr, 'HEAPF32', _scratch4x4MatrixPtr); |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 359 | } |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 360 | // Upscale the row-major 3x3 or 3x2 matrix into a 4x4 row-major matrix |
| 361 | // TODO(skbug.com/10108) This will need to change when we convert our |
| 362 | // JS 4x4 to be column-major. |
| 363 | // When upscaling, we need to overwrite the 3rd column and the 3rd row with |
| 364 | // 0s. It's easiest to just do that with a fill command. |
Kevin Lubick | 462a860 | 2020-06-01 15:43:03 -0400 | [diff] [blame] | 365 | wasm4x4Matrix.fill(0); |
| 366 | wasm4x4Matrix[0] = matr[0]; |
| 367 | wasm4x4Matrix[1] = matr[1]; |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 368 | // skip col 2 |
Kevin Lubick | 462a860 | 2020-06-01 15:43:03 -0400 | [diff] [blame] | 369 | wasm4x4Matrix[3] = matr[2]; |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 370 | |
Kevin Lubick | 462a860 | 2020-06-01 15:43:03 -0400 | [diff] [blame] | 371 | wasm4x4Matrix[4] = matr[3]; |
| 372 | wasm4x4Matrix[5] = matr[4]; |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 373 | // skip col 2 |
Kevin Lubick | 462a860 | 2020-06-01 15:43:03 -0400 | [diff] [blame] | 374 | wasm4x4Matrix[7] = matr[5]; |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 375 | |
| 376 | // skip row 2 |
| 377 | |
Kevin Lubick | 462a860 | 2020-06-01 15:43:03 -0400 | [diff] [blame] | 378 | wasm4x4Matrix[12] = matr[6]; |
| 379 | wasm4x4Matrix[13] = matr[7]; |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 380 | // skip col 2 |
Kevin Lubick | 462a860 | 2020-06-01 15:43:03 -0400 | [diff] [blame] | 381 | wasm4x4Matrix[15] = matr[8]; |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 382 | |
| 383 | if (matr.length === 6) { |
| 384 | // fix perspective for the 3x2 case (from above, they will be undefined). |
Kevin Lubick | 462a860 | 2020-06-01 15:43:03 -0400 | [diff] [blame] | 385 | wasm4x4Matrix[12]=0; |
| 386 | wasm4x4Matrix[13]=0; |
| 387 | wasm4x4Matrix[15]=1; |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 388 | } |
| 389 | return _scratch4x4MatrixPtr; |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 390 | } |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 391 | // 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] | 392 | wasm4x4Matrix[0] = matr.m11; |
| 393 | wasm4x4Matrix[1] = matr.m21; |
| 394 | wasm4x4Matrix[2] = matr.m31; |
| 395 | wasm4x4Matrix[3] = matr.m41; |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 396 | |
Kevin Lubick | 462a860 | 2020-06-01 15:43:03 -0400 | [diff] [blame] | 397 | wasm4x4Matrix[4] = matr.m12; |
| 398 | wasm4x4Matrix[5] = matr.m22; |
| 399 | wasm4x4Matrix[6] = matr.m32; |
| 400 | wasm4x4Matrix[7] = matr.m42; |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 401 | |
Kevin Lubick | 462a860 | 2020-06-01 15:43:03 -0400 | [diff] [blame] | 402 | wasm4x4Matrix[8] = matr.m13; |
| 403 | wasm4x4Matrix[9] = matr.m23; |
| 404 | wasm4x4Matrix[10] = matr.m33; |
| 405 | wasm4x4Matrix[11] = matr.m43; |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 406 | |
Kevin Lubick | 462a860 | 2020-06-01 15:43:03 -0400 | [diff] [blame] | 407 | wasm4x4Matrix[12] = matr.m14; |
| 408 | wasm4x4Matrix[13] = matr.m24; |
| 409 | wasm4x4Matrix[14] = matr.m34; |
| 410 | wasm4x4Matrix[15] = matr.m44; |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 411 | return _scratch4x4MatrixPtr; |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 412 | } |
| 413 | |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 414 | // copies a 4x4 matrix at the given pointer into a JS array. It is the caller's |
| 415 | // responsibility to free the matrPtr if needed. |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 416 | function copy4x4MatrixFromWasm(matrPtr) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 417 | // read them out into an array. TODO(kjlubick): If we change Matrix to be |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 418 | // typedArrays, then we should return a typed array here too. |
| 419 | var rv = new Array(16); |
| 420 | for (var i = 0; i < 16; i++) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 421 | rv[i] = CanvasKit.HEAPF32[matrPtr/4 + i]; // divide by 4 to cast to float. |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 422 | } |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 423 | return rv; |
| 424 | } |
| 425 | |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 426 | var _scratchColorPtr = nullptr; |
Kevin Lubick | 93f1a38 | 2020-06-02 16:15:23 -0400 | [diff] [blame] | 427 | var _scratchColor; // the result from CanvasKit.Malloc |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 428 | |
| 429 | function copyColorToWasm(color4f, ptr) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 430 | return copy1dArray(color4f, 'HEAPF32', ptr || _scratchColorPtr); |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 431 | } |
| 432 | |
Kevin Lubick | 93f1a38 | 2020-06-02 16:15:23 -0400 | [diff] [blame] | 433 | function copyColorComponentsToWasm(r, g, b, a) { |
| 434 | var colors = _scratchColor['toTypedArray'](); |
| 435 | colors[0] = r; |
| 436 | colors[1] = g; |
| 437 | colors[2] = b; |
| 438 | colors[3] = a; |
| 439 | return _scratchColorPtr; |
| 440 | } |
| 441 | |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 442 | function copyColorToWasmNoScratch(color4f) { |
| 443 | // TODO(kjlubick): accept 4 floats or int color |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 444 | return copy1dArray(color4f, 'HEAPF32'); |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 445 | } |
| 446 | |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 447 | // copies the four floats at the given pointer in a js Float32Array |
| 448 | function copyColorFromWasm(colorPtr) { |
| 449 | var rv = new Float32Array(4); |
| 450 | for (var i = 0; i < 4; i++) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 451 | rv[i] = CanvasKit.HEAPF32[colorPtr/4 + i]; // divide by 4 to cast to float. |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 452 | } |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 453 | return rv; |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 454 | } |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 455 | |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 456 | // These will be initialized after loading. |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 457 | var _scratchRect; |
| 458 | var _scratchRectPtr = nullptr; |
| 459 | |
| 460 | var _scratchRect2; |
| 461 | var _scratchRect2Ptr = nullptr; |
| 462 | |
| 463 | function copyRectToWasm(fourFloats, ptr) { |
| 464 | return copy1dArray(fourFloats, 'HEAPF32', ptr || _scratchRectPtr); |
| 465 | } |
| 466 | |
| 467 | var _scratchIRect; |
| 468 | var _scratchIRectPtr = nullptr; |
| 469 | function copyIRectToWasm(fourInts, ptr) { |
| 470 | return copy1dArray(fourInts, 'HEAP32', ptr || _scratchIRectPtr); |
| 471 | } |
| 472 | |
| 473 | // These will be initialized after loading. |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 474 | var _scratchRRect; |
| 475 | var _scratchRRectPtr = nullptr; |
| 476 | |
| 477 | var _scratchRRect2; |
| 478 | var _scratchRRect2Ptr = nullptr; |
| 479 | |
| 480 | |
| 481 | function copyRRectToWasm(twelveFloats, ptr) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 482 | return copy1dArray(twelveFloats, 'HEAPF32', ptr || _scratchRRectPtr); |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 485 | // Caching the Float32Arrays can save having to reallocate them |
| 486 | // over and over again. |
| 487 | var Float32ArrayCache = {}; |
| 488 | |
| 489 | // Takes a 2D array of commands and puts them into the WASM heap |
| 490 | // as a 1D array. This allows them to referenced from the C++ code. |
| 491 | // Returns a 2 element array, with the first item being essentially a |
| 492 | // pointer to the array and the second item being the length of |
| 493 | // the new 1D array. |
| 494 | // |
| 495 | // Example usage: |
| 496 | // let cmds = [ |
| 497 | // [CanvasKit.MOVE_VERB, 0, 10], |
| 498 | // [CanvasKit.LINE_VERB, 30, 40], |
| 499 | // [CanvasKit.QUAD_VERB, 20, 50, 45, 60], |
| 500 | // ]; |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 501 | // TODO(kjlubick) remove this and Float32ArrayCache (superceded by Malloc). |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 502 | function loadCmdsTypedArray(arr) { |
| 503 | var len = 0; |
| 504 | for (var r = 0; r < arr.length; r++) { |
| 505 | len += arr[r].length; |
| 506 | } |
| 507 | |
| 508 | var ta; |
| 509 | if (Float32ArrayCache[len]) { |
| 510 | ta = Float32ArrayCache[len]; |
| 511 | } else { |
| 512 | ta = new Float32Array(len); |
| 513 | Float32ArrayCache[len] = ta; |
| 514 | } |
| 515 | // Flatten into a 1d array |
| 516 | var i = 0; |
| 517 | for (var r = 0; r < arr.length; r++) { |
| 518 | for (var c = 0; c < arr[r].length; c++) { |
| 519 | var item = arr[r][c]; |
| 520 | ta[i] = item; |
| 521 | i++; |
| 522 | } |
| 523 | } |
| 524 | |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 525 | var ptr = copy1dArray(ta, 'HEAPF32'); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 526 | return [ptr, len]; |
| 527 | } |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 528 | |
Kevin Lubick | cc13fd3 | 2019-04-05 13:00:01 -0400 | [diff] [blame] | 529 | function saveBytesToFile(bytes, fileName) { |
| 530 | if (!isNode) { |
| 531 | // https://stackoverflow.com/a/32094834 |
| 532 | var blob = new Blob([bytes], {type: 'application/octet-stream'}); |
| 533 | url = window.URL.createObjectURL(blob); |
| 534 | var a = document.createElement('a'); |
| 535 | document.body.appendChild(a); |
| 536 | a.href = url; |
| 537 | a.download = fileName; |
| 538 | a.click(); |
| 539 | // clean up after because FF might not download it synchronously |
| 540 | setTimeout(function() { |
| 541 | URL.revokeObjectURL(url); |
| 542 | a.remove(); |
| 543 | }, 50); |
| 544 | } else { |
| 545 | var fs = require('fs'); |
| 546 | // https://stackoverflow.com/a/42006750 |
| 547 | // https://stackoverflow.com/a/47018122 |
| 548 | fs.writeFile(fileName, new Buffer(bytes), function(err) { |
| 549 | if (err) throw err; |
| 550 | }); |
| 551 | } |
| 552 | } |
Kevin Lubick | 97440de | 2020-09-29 17:58:21 -0400 | [diff] [blame] | 553 | |
| 554 | // TODO(kjlubick) remove Builders - no longer needed now that Malloc is a thing. |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 555 | /** |
| 556 | * Generic helper for dealing with an array of four floats. |
| 557 | */ |
| 558 | CanvasKit.FourFloatArrayHelper = function() { |
| 559 | this._floats = []; |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 560 | this._ptr = null; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 561 | |
| 562 | Object.defineProperty(this, 'length', { |
| 563 | enumerable: true, |
| 564 | get: function() { |
| 565 | return this._floats.length / 4; |
| 566 | }, |
| 567 | }); |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame^] | 568 | }; |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 569 | |
| 570 | /** |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 571 | * push the four floats onto the end of the array - if build() has already |
| 572 | * been called, the call will return without modifying anything. |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 573 | */ |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 574 | CanvasKit.FourFloatArrayHelper.prototype.push = function(f1, f2, f3, f4) { |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 575 | if (this._ptr) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 576 | Debug('Cannot push more points - already built'); |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 577 | return; |
| 578 | } |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 579 | this._floats.push(f1, f2, f3, f4); |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame^] | 580 | }; |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 581 | |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 582 | /** |
| 583 | * Set the four floats at a given index - if build() has already |
| 584 | * been called, the WASM memory will be written to directly. |
| 585 | */ |
| 586 | CanvasKit.FourFloatArrayHelper.prototype.set = function(idx, f1, f2, f3, f4) { |
| 587 | if (idx < 0 || idx >= this._floats.length/4) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 588 | Debug('Cannot set index ' + idx + ', it is out of range', this._floats.length/4); |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 589 | return; |
| 590 | } |
| 591 | idx *= 4; |
| 592 | var BYTES_PER_ELEMENT = 4; |
| 593 | if (this._ptr) { |
| 594 | // convert this._ptr from uint8_t* to SkScalar* by dividing by 4 |
| 595 | var floatPtr = (this._ptr / BYTES_PER_ELEMENT) + idx; |
| 596 | CanvasKit.HEAPF32[floatPtr] = f1; |
| 597 | CanvasKit.HEAPF32[floatPtr + 1] = f2; |
| 598 | CanvasKit.HEAPF32[floatPtr + 2] = f3; |
| 599 | CanvasKit.HEAPF32[floatPtr + 3] = f4; |
| 600 | return; |
| 601 | } |
| 602 | this._floats[idx] = f1; |
| 603 | this._floats[idx + 1] = f2; |
| 604 | this._floats[idx + 2] = f3; |
| 605 | this._floats[idx + 3] = f4; |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame^] | 606 | }; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 607 | |
| 608 | /** |
| 609 | * Copies the float data to the WASM memory and returns a pointer |
| 610 | * to that allocated memory. Once build has been called, this |
| 611 | * float array cannot be made bigger. |
| 612 | */ |
| 613 | CanvasKit.FourFloatArrayHelper.prototype.build = function() { |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 614 | if (this._ptr) { |
| 615 | return this._ptr; |
| 616 | } |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 617 | this._ptr = copy1dArray(this._floats, 'HEAPF32'); |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 618 | return this._ptr; |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame^] | 619 | }; |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 620 | |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 621 | /** |
| 622 | * Frees the wasm memory associated with this array. Of note, |
| 623 | * the points are not removed, so push/set/build can all |
| 624 | * be called to make a newly allocated (possibly bigger) |
| 625 | * float array. |
| 626 | */ |
| 627 | CanvasKit.FourFloatArrayHelper.prototype.delete = function() { |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 628 | if (this._ptr) { |
| 629 | CanvasKit._free(this._ptr); |
| 630 | this._ptr = null; |
| 631 | } |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame^] | 632 | }; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 633 | |
| 634 | /** |
| 635 | * Generic helper for dealing with an array of unsigned ints. |
| 636 | */ |
| 637 | CanvasKit.OneUIntArrayHelper = function() { |
| 638 | this._uints = []; |
| 639 | this._ptr = null; |
| 640 | |
| 641 | Object.defineProperty(this, 'length', { |
| 642 | enumerable: true, |
| 643 | get: function() { |
| 644 | return this._uints.length; |
| 645 | }, |
| 646 | }); |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame^] | 647 | }; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 648 | |
| 649 | /** |
| 650 | * push the unsigned int onto the end of the array - if build() has already |
| 651 | * been called, the call will return without modifying anything. |
| 652 | */ |
| 653 | CanvasKit.OneUIntArrayHelper.prototype.push = function(u) { |
| 654 | if (this._ptr) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 655 | Debug('Cannot push more points - already built'); |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 656 | return; |
| 657 | } |
| 658 | this._uints.push(u); |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame^] | 659 | }; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 660 | |
| 661 | /** |
| 662 | * Set the uint at a given index - if build() has already |
| 663 | * been called, the WASM memory will be written to directly. |
| 664 | */ |
| 665 | CanvasKit.OneUIntArrayHelper.prototype.set = function(idx, u) { |
| 666 | if (idx < 0 || idx >= this._uints.length) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 667 | Debug('Cannot set index ' + idx + ', it is out of range', this._uints.length); |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 668 | return; |
| 669 | } |
| 670 | idx *= 4; |
| 671 | var BYTES_PER_ELEMENT = 4; |
| 672 | if (this._ptr) { |
| 673 | // convert this._ptr from uint8_t* to SkScalar* by dividing by 4 |
| 674 | var uintPtr = (this._ptr / BYTES_PER_ELEMENT) + idx; |
| 675 | CanvasKit.HEAPU32[uintPtr] = u; |
| 676 | return; |
| 677 | } |
| 678 | this._uints[idx] = u; |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame^] | 679 | }; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 680 | |
| 681 | /** |
| 682 | * Copies the uint data to the WASM memory and returns a pointer |
| 683 | * to that allocated memory. Once build has been called, this |
| 684 | * unit array cannot be made bigger. |
| 685 | */ |
| 686 | CanvasKit.OneUIntArrayHelper.prototype.build = function() { |
| 687 | if (this._ptr) { |
| 688 | return this._ptr; |
| 689 | } |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 690 | this._ptr = copy1dArray(this._uints, 'HEAPU32'); |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 691 | return this._ptr; |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame^] | 692 | }; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 693 | |
| 694 | /** |
| 695 | * Frees the wasm memory associated with this array. Of note, |
| 696 | * the points are not removed, so push/set/build can all |
| 697 | * be called to make a newly allocated (possibly bigger) |
| 698 | * uint array. |
| 699 | */ |
| 700 | CanvasKit.OneUIntArrayHelper.prototype.delete = function() { |
| 701 | if (this._ptr) { |
| 702 | CanvasKit._free(this._ptr); |
| 703 | this._ptr = null; |
| 704 | } |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame^] | 705 | }; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 706 | |
| 707 | /** |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 708 | * Helper for building an array of Rects (which are just structs |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 709 | * of 4 floats). |
| 710 | * |
| 711 | * It can be more performant to use this helper, as |
| 712 | * the C++-side array is only allocated once (on the first call) |
| 713 | * to build. Subsequent set() operations operate directly on |
| 714 | * the C++-side array, avoiding having to re-allocate (and free) |
| 715 | * the array every time. |
| 716 | * |
| 717 | * Input points are taken as left, top, right, bottom |
| 718 | */ |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 719 | CanvasKit.RectBuilder = CanvasKit.FourFloatArrayHelper; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 720 | /** |
| 721 | * Helper for building an array of RSXForms (which are just structs |
| 722 | * of 4 floats). |
| 723 | * |
| 724 | * It can be more performant to use this helper, as |
| 725 | * the C++-side array is only allocated once (on the first call) |
| 726 | * to build. Subsequent set() operations operate directly on |
| 727 | * the C++-side array, avoiding having to re-allocate (and free) |
| 728 | * the array every time. |
| 729 | * |
| 730 | * An RSXForm is a compressed form of a rotation+scale matrix. |
| 731 | * |
| 732 | * [ scos -ssin tx ] |
| 733 | * [ ssin scos ty ] |
| 734 | * [ 0 0 1 ] |
| 735 | * |
| 736 | * Input points are taken as scos, ssin, tx, ty |
| 737 | */ |
| 738 | CanvasKit.RSXFormBuilder = CanvasKit.FourFloatArrayHelper; |
| 739 | |
| 740 | /** |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 741 | * Helper for building an array of Color |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 742 | * |
| 743 | * It can be more performant to use this helper, as |
| 744 | * the C++-side array is only allocated once (on the first call) |
| 745 | * to build. Subsequent set() operations operate directly on |
| 746 | * the C++-side array, avoiding having to re-allocate (and free) |
| 747 | * the array every time. |
| 748 | */ |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 749 | CanvasKit.ColorBuilder = CanvasKit.OneUIntArrayHelper; |
Kevin Lubick | e25df6c | 2019-10-22 09:04:32 -0400 | [diff] [blame] | 750 | |
| 751 | /** |
| 752 | * Malloc returns a TypedArray backed by the C++ memory of the |
| 753 | * given length. It should only be used by advanced users who |
| 754 | * can manage memory and initialize values properly. When used |
| 755 | * correctly, it can save copying of data between JS and C++. |
| 756 | * When used incorrectly, it can lead to memory leaks. |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 757 | * 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] | 758 | * |
Kevin Lubick | 462a860 | 2020-06-01 15:43:03 -0400 | [diff] [blame] | 759 | * const mObj = CanvasKit.Malloc(Float32Array, 20); |
Kevin Lubick | 93f1a38 | 2020-06-02 16:15:23 -0400 | [diff] [blame] | 760 | * 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] | 761 | * const ta = mObj.toTypedArray(); |
Kevin Lubick | e25df6c | 2019-10-22 09:04:32 -0400 | [diff] [blame] | 762 | * // store data into ta |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 763 | * const cf = CanvasKit.ColorFilter.MakeMatrix(ta); // mObj could also be used. |
Kevin Lubick | 462a860 | 2020-06-01 15:43:03 -0400 | [diff] [blame] | 764 | * |
| 765 | * // eventually... |
| 766 | * CanvasKit.Free(mObj); |
Kevin Lubick | e25df6c | 2019-10-22 09:04:32 -0400 | [diff] [blame] | 767 | * |
| 768 | * @param {TypedArray} typedArray - constructor for the typedArray. |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 769 | * @param {number} len - number of *elements* to store. |
Kevin Lubick | e25df6c | 2019-10-22 09:04:32 -0400 | [diff] [blame] | 770 | */ |
| 771 | CanvasKit.Malloc = function(typedArray, len) { |
| 772 | var byteLen = len * typedArray.BYTES_PER_ELEMENT; |
| 773 | var ptr = CanvasKit._malloc(byteLen); |
Kevin Lubick | 462a860 | 2020-06-01 15:43:03 -0400 | [diff] [blame] | 774 | return { |
| 775 | '_ck': true, |
| 776 | 'length': len, |
| 777 | 'byteOffset': ptr, |
| 778 | typedArray: null, |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 779 | 'subarray': function(start, end) { |
| 780 | var sa = this['toTypedArray']().subarray(start, end); |
| 781 | sa['_ck'] = true; |
| 782 | return sa; |
| 783 | }, |
Kevin Lubick | 462a860 | 2020-06-01 15:43:03 -0400 | [diff] [blame] | 784 | 'toTypedArray': function() { |
| 785 | // Check if the previously allocated array is still usable. |
| 786 | // If it's falsey, then we haven't created an array yet. |
| 787 | // If it's empty, then WASM resized memory and emptied the array. |
| 788 | if (this.typedArray && this.typedArray.length) { |
| 789 | return this.typedArray; |
| 790 | } |
| 791 | this.typedArray = new typedArray(CanvasKit.HEAPU8.buffer, ptr, len); |
| 792 | // add a marker that this was allocated in C++ land |
| 793 | this.typedArray['_ck'] = true; |
| 794 | return this.typedArray; |
| 795 | }, |
| 796 | }; |
Kevin Lubick | 2613332 | 2020-06-11 13:48:16 -0400 | [diff] [blame] | 797 | }; |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 798 | |
| 799 | /** |
| 800 | * Free frees the memory returned by Malloc. |
| 801 | * Any memory allocated by CanvasKit.Malloc needs to be released with CanvasKit.Free. |
| 802 | */ |
Kevin Lubick | 462a860 | 2020-06-01 15:43:03 -0400 | [diff] [blame] | 803 | CanvasKit.Free = function(mallocObj) { |
| 804 | CanvasKit._free(mallocObj['byteOffset']); |
| 805 | mallocObj['byteOffset'] = nullptr; |
| 806 | // Set these to null to make sure the TypedArrays can be garbage collected. |
| 807 | mallocObj['toTypedArray'] = null; |
| 808 | mallocObj.typedArray = null; |
Kevin Lubick | 2613332 | 2020-06-11 13:48:16 -0400 | [diff] [blame] | 809 | }; |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 810 | |
| 811 | // This helper will free the given pointer unless the provided array is one |
| 812 | // that was returned by CanvasKit.Malloc. |
| 813 | function freeArraysThatAreNotMallocedByUsers(ptr, arr) { |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 814 | if (arr && !arr['_ck']) { |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 815 | CanvasKit._free(ptr); |
| 816 | } |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 817 | } |