Kevin Lubick | 2c3cec9 | 2021-01-21 11:41:01 -0500 | [diff] [blame] | 1 | // TODO(kjlubick) |
| 2 | // The remaining functions here are deprecated and should be removed eventually. |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 3 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 4 | // Caching the Float32Arrays can save having to reallocate them |
| 5 | // over and over again. |
| 6 | var Float32ArrayCache = {}; |
| 7 | |
| 8 | // Takes a 2D array of commands and puts them into the WASM heap |
| 9 | // as a 1D array. This allows them to referenced from the C++ code. |
| 10 | // Returns a 2 element array, with the first item being essentially a |
| 11 | // pointer to the array and the second item being the length of |
| 12 | // the new 1D array. |
| 13 | // |
| 14 | // Example usage: |
| 15 | // let cmds = [ |
| 16 | // [CanvasKit.MOVE_VERB, 0, 10], |
| 17 | // [CanvasKit.LINE_VERB, 30, 40], |
| 18 | // [CanvasKit.QUAD_VERB, 20, 50, 45, 60], |
| 19 | // ]; |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 20 | // TODO(kjlubick) remove this and Float32ArrayCache (superceded by Malloc). |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 21 | function loadCmdsTypedArray(arr) { |
| 22 | var len = 0; |
| 23 | for (var r = 0; r < arr.length; r++) { |
| 24 | len += arr[r].length; |
| 25 | } |
| 26 | |
| 27 | var ta; |
| 28 | if (Float32ArrayCache[len]) { |
| 29 | ta = Float32ArrayCache[len]; |
| 30 | } else { |
| 31 | ta = new Float32Array(len); |
| 32 | Float32ArrayCache[len] = ta; |
| 33 | } |
| 34 | // Flatten into a 1d array |
| 35 | var i = 0; |
| 36 | for (var r = 0; r < arr.length; r++) { |
| 37 | for (var c = 0; c < arr[r].length; c++) { |
| 38 | var item = arr[r][c]; |
| 39 | ta[i] = item; |
| 40 | i++; |
| 41 | } |
| 42 | } |
| 43 | |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 44 | var ptr = copy1dArray(ta, 'HEAPF32'); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 45 | return [ptr, len]; |
| 46 | } |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 47 | |
Kevin Lubick | 97440de | 2020-09-29 17:58:21 -0400 | [diff] [blame] | 48 | // 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] | 49 | /** |
| 50 | * Generic helper for dealing with an array of four floats. |
| 51 | */ |
| 52 | CanvasKit.FourFloatArrayHelper = function() { |
| 53 | this._floats = []; |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 54 | this._ptr = null; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 55 | |
| 56 | Object.defineProperty(this, 'length', { |
| 57 | enumerable: true, |
| 58 | get: function() { |
| 59 | return this._floats.length / 4; |
| 60 | }, |
| 61 | }); |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame] | 62 | }; |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 63 | |
| 64 | /** |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 65 | * push the four floats onto the end of the array - if build() has already |
| 66 | * been called, the call will return without modifying anything. |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 67 | */ |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 68 | CanvasKit.FourFloatArrayHelper.prototype.push = function(f1, f2, f3, f4) { |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 69 | if (this._ptr) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 70 | Debug('Cannot push more points - already built'); |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 71 | return; |
| 72 | } |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 73 | this._floats.push(f1, f2, f3, f4); |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame] | 74 | }; |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 75 | |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 76 | /** |
| 77 | * Set the four floats at a given index - if build() has already |
| 78 | * been called, the WASM memory will be written to directly. |
| 79 | */ |
| 80 | CanvasKit.FourFloatArrayHelper.prototype.set = function(idx, f1, f2, f3, f4) { |
| 81 | if (idx < 0 || idx >= this._floats.length/4) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 82 | 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] | 83 | return; |
| 84 | } |
| 85 | idx *= 4; |
| 86 | var BYTES_PER_ELEMENT = 4; |
| 87 | if (this._ptr) { |
| 88 | // convert this._ptr from uint8_t* to SkScalar* by dividing by 4 |
| 89 | var floatPtr = (this._ptr / BYTES_PER_ELEMENT) + idx; |
| 90 | CanvasKit.HEAPF32[floatPtr] = f1; |
| 91 | CanvasKit.HEAPF32[floatPtr + 1] = f2; |
| 92 | CanvasKit.HEAPF32[floatPtr + 2] = f3; |
| 93 | CanvasKit.HEAPF32[floatPtr + 3] = f4; |
| 94 | return; |
| 95 | } |
| 96 | this._floats[idx] = f1; |
| 97 | this._floats[idx + 1] = f2; |
| 98 | this._floats[idx + 2] = f3; |
| 99 | this._floats[idx + 3] = f4; |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame] | 100 | }; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * Copies the float data to the WASM memory and returns a pointer |
| 104 | * to that allocated memory. Once build has been called, this |
| 105 | * float array cannot be made bigger. |
| 106 | */ |
| 107 | CanvasKit.FourFloatArrayHelper.prototype.build = function() { |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 108 | if (this._ptr) { |
| 109 | return this._ptr; |
| 110 | } |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 111 | this._ptr = copy1dArray(this._floats, 'HEAPF32'); |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 112 | return this._ptr; |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame] | 113 | }; |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 114 | |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 115 | /** |
| 116 | * Frees the wasm memory associated with this array. Of note, |
| 117 | * the points are not removed, so push/set/build can all |
| 118 | * be called to make a newly allocated (possibly bigger) |
| 119 | * float array. |
| 120 | */ |
| 121 | CanvasKit.FourFloatArrayHelper.prototype.delete = function() { |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 122 | if (this._ptr) { |
| 123 | CanvasKit._free(this._ptr); |
| 124 | this._ptr = null; |
| 125 | } |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame] | 126 | }; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 127 | |
| 128 | /** |
| 129 | * Generic helper for dealing with an array of unsigned ints. |
| 130 | */ |
| 131 | CanvasKit.OneUIntArrayHelper = function() { |
| 132 | this._uints = []; |
| 133 | this._ptr = null; |
| 134 | |
| 135 | Object.defineProperty(this, 'length', { |
| 136 | enumerable: true, |
| 137 | get: function() { |
| 138 | return this._uints.length; |
| 139 | }, |
| 140 | }); |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame] | 141 | }; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 142 | |
| 143 | /** |
| 144 | * push the unsigned int onto the end of the array - if build() has already |
| 145 | * been called, the call will return without modifying anything. |
| 146 | */ |
| 147 | CanvasKit.OneUIntArrayHelper.prototype.push = function(u) { |
| 148 | if (this._ptr) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 149 | Debug('Cannot push more points - already built'); |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 150 | return; |
| 151 | } |
| 152 | this._uints.push(u); |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame] | 153 | }; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 154 | |
| 155 | /** |
| 156 | * Set the uint at a given index - if build() has already |
| 157 | * been called, the WASM memory will be written to directly. |
| 158 | */ |
| 159 | CanvasKit.OneUIntArrayHelper.prototype.set = function(idx, u) { |
| 160 | if (idx < 0 || idx >= this._uints.length) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 161 | 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] | 162 | return; |
| 163 | } |
| 164 | idx *= 4; |
| 165 | var BYTES_PER_ELEMENT = 4; |
| 166 | if (this._ptr) { |
| 167 | // convert this._ptr from uint8_t* to SkScalar* by dividing by 4 |
| 168 | var uintPtr = (this._ptr / BYTES_PER_ELEMENT) + idx; |
| 169 | CanvasKit.HEAPU32[uintPtr] = u; |
| 170 | return; |
| 171 | } |
| 172 | this._uints[idx] = u; |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame] | 173 | }; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 174 | |
| 175 | /** |
| 176 | * Copies the uint data to the WASM memory and returns a pointer |
| 177 | * to that allocated memory. Once build has been called, this |
| 178 | * unit array cannot be made bigger. |
| 179 | */ |
| 180 | CanvasKit.OneUIntArrayHelper.prototype.build = function() { |
| 181 | if (this._ptr) { |
| 182 | return this._ptr; |
| 183 | } |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 184 | this._ptr = copy1dArray(this._uints, 'HEAPU32'); |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 185 | return this._ptr; |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame] | 186 | }; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 187 | |
| 188 | /** |
| 189 | * Frees the wasm memory associated with this array. Of note, |
| 190 | * the points are not removed, so push/set/build can all |
| 191 | * be called to make a newly allocated (possibly bigger) |
| 192 | * uint array. |
| 193 | */ |
| 194 | CanvasKit.OneUIntArrayHelper.prototype.delete = function() { |
| 195 | if (this._ptr) { |
| 196 | CanvasKit._free(this._ptr); |
| 197 | this._ptr = null; |
| 198 | } |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame] | 199 | }; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 200 | |
| 201 | /** |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 202 | * Helper for building an array of Rects (which are just structs |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 203 | * of 4 floats). |
| 204 | * |
| 205 | * It can be more performant to use this helper, as |
| 206 | * the C++-side array is only allocated once (on the first call) |
| 207 | * to build. Subsequent set() operations operate directly on |
| 208 | * the C++-side array, avoiding having to re-allocate (and free) |
| 209 | * the array every time. |
| 210 | * |
| 211 | * Input points are taken as left, top, right, bottom |
| 212 | */ |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 213 | CanvasKit.RectBuilder = CanvasKit.FourFloatArrayHelper; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 214 | /** |
| 215 | * Helper for building an array of RSXForms (which are just structs |
| 216 | * of 4 floats). |
| 217 | * |
| 218 | * It can be more performant to use this helper, as |
| 219 | * the C++-side array is only allocated once (on the first call) |
| 220 | * to build. Subsequent set() operations operate directly on |
| 221 | * the C++-side array, avoiding having to re-allocate (and free) |
| 222 | * the array every time. |
| 223 | * |
| 224 | * An RSXForm is a compressed form of a rotation+scale matrix. |
| 225 | * |
| 226 | * [ scos -ssin tx ] |
| 227 | * [ ssin scos ty ] |
| 228 | * [ 0 0 1 ] |
| 229 | * |
| 230 | * Input points are taken as scos, ssin, tx, ty |
| 231 | */ |
| 232 | CanvasKit.RSXFormBuilder = CanvasKit.FourFloatArrayHelper; |
| 233 | |
| 234 | /** |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 235 | * Helper for building an array of Color |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 236 | * |
| 237 | * It can be more performant to use this helper, as |
| 238 | * the C++-side array is only allocated once (on the first call) |
| 239 | * to build. Subsequent set() operations operate directly on |
| 240 | * the C++-side array, avoiding having to re-allocate (and free) |
| 241 | * the array every time. |
| 242 | */ |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 243 | CanvasKit.ColorBuilder = CanvasKit.OneUIntArrayHelper; |