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 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 7 | // Colors are just a 32 bit number with 8 bits each of a, r, g, b |
| 8 | // The API is the same as CSS's representation of color rgba(), that is |
| 9 | // r,g,b are 0-255, and a is 0.0 to 1.0. |
| 10 | // if a is omitted, it will be assumed to be 1.0 |
| 11 | CanvasKit.Color = function(r, g, b, a) { |
| 12 | if (a === undefined) { |
| 13 | a = 1; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 14 | } |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame^] | 15 | // The >>> 0 converts the signed int to an unsigned int. Skia's |
| 16 | // SkColor object is an unsigned int. |
| 17 | // https://stackoverflow.com/a/14891172 |
| 18 | return ((clamp(a*255) << 24) | (clamp(r) << 16) | (clamp(g) << 8) | (clamp(b) << 0)) >>> 0; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 19 | } |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 20 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 21 | // returns [r, g, b, a] from a color |
| 22 | // where a is scaled between 0 and 1.0 |
| 23 | CanvasKit.getColorComponents = function(color) { |
| 24 | return [ |
| 25 | (color >> 16) & 0xFF, |
| 26 | (color >> 8) & 0xFF, |
| 27 | (color >> 0) & 0xFF, |
| 28 | ((color >> 24) & 0xFF) / 255, |
| 29 | ] |
| 30 | } |
| 31 | |
| 32 | CanvasKit.multiplyByAlpha = function(color, alpha) { |
| 33 | if (alpha === 1) { |
| 34 | return color; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 35 | } |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 36 | // extract as int from 0 to 255 |
| 37 | var a = (color >> 24) & 0xFF; |
| 38 | a *= alpha; |
| 39 | // mask off the old alpha |
| 40 | color &= 0xFFFFFF; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame^] | 41 | // back to unsigned int to match SkColor. |
| 42 | return (clamp(a) << 24 | color) >>> 0; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 43 | } |
Kevin Lubick | 61ef7b2 | 2018-11-27 13:26:59 -0500 | [diff] [blame] | 44 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 45 | function radiansToDegrees(rad) { |
| 46 | return (rad / Math.PI) * 180; |
| 47 | } |
Kevin Lubick | 12c0e50 | 2018-11-28 12:51:56 -0500 | [diff] [blame] | 48 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 49 | function degreesToRadians(deg) { |
| 50 | return (deg / 180) * Math.PI; |
| 51 | } |
Kevin Lubick | 12c0e50 | 2018-11-28 12:51:56 -0500 | [diff] [blame] | 52 | |
| 53 | // See https://stackoverflow.com/a/31090240 |
| 54 | // This contraption keeps closure from minifying away the check |
| 55 | // if btoa is defined *and* prevents runtime "btoa" or "window" is not defined. |
| 56 | // Defined outside any scopes to make it available in all files. |
| 57 | var isNode = !(new Function("try {return this===window;}catch(e){ return false;}")()); |
Kevin Lubick | 1646e7d | 2018-12-07 13:03:08 -0500 | [diff] [blame] | 58 | |
| 59 | function almostEqual(floata, floatb) { |
| 60 | return Math.abs(floata - floatb) < 0.00001; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | |
| 64 | var nullptr = 0; // emscripten doesn't like to take null as uintptr_t |
| 65 | |
| 66 | // arr can be a normal JS array or a TypedArray |
| 67 | // dest is something like CanvasKit.HEAPF32 |
| 68 | function copy1dArray(arr, dest) { |
| 69 | if (!arr || !arr.length) { |
| 70 | return nullptr; |
| 71 | } |
| 72 | var ptr = CanvasKit._malloc(arr.length * dest.BYTES_PER_ELEMENT); |
| 73 | // In c++ terms, the WASM heap is a uint8_t*, a long buffer/array of single |
| 74 | // byte elements. When we run _malloc, we always get an offset/pointer into |
| 75 | // that block of memory. |
| 76 | // CanvasKit exposes some different views to make it easier to work with |
| 77 | // different types. HEAPF32 for example, exposes it as a float* |
| 78 | // However, to make the ptr line up, we have to do some pointer arithmetic. |
| 79 | // Concretely, we need to convert ptr to go from an index into a 1-byte-wide |
| 80 | // buffer to an index into a 4-byte-wide buffer (in the case of HEAPF32) |
| 81 | // and thus we divide ptr by 4. |
| 82 | dest.set(arr, ptr / dest.BYTES_PER_ELEMENT); |
| 83 | return ptr; |
| 84 | } |
| 85 | |
| 86 | // arr should be a non-jagged 2d JS array (TypedArrays can't be nested |
| 87 | // inside themselves.) |
| 88 | // dest is something like CanvasKit.HEAPF32 |
| 89 | function copy2dArray(arr, dest) { |
| 90 | if (!arr || !arr.length) { |
| 91 | return nullptr; |
| 92 | } |
| 93 | var ptr = CanvasKit._malloc(arr.length * arr[0].length * dest.BYTES_PER_ELEMENT); |
| 94 | var idx = 0; |
| 95 | var adjustedPtr = ptr / dest.BYTES_PER_ELEMENT; |
| 96 | for (var r = 0; r < arr.length; r++) { |
| 97 | for (var c = 0; c < arr[0].length; c++) { |
| 98 | dest[adjustedPtr + idx] = arr[r][c]; |
| 99 | idx++; |
| 100 | } |
| 101 | } |
| 102 | return ptr; |
| 103 | } |
| 104 | |
| 105 | // arr should be a non-jagged 3d JS array (TypedArrays can't be nested |
| 106 | // inside themselves.) |
| 107 | // dest is something like CanvasKit.HEAPF32 |
| 108 | function copy3dArray(arr, dest) { |
| 109 | if (!arr || !arr.length || !arr[0].length) { |
| 110 | return nullptr; |
| 111 | } |
| 112 | var ptr = CanvasKit._malloc(arr.length * arr[0].length * arr[0][0].length * dest.BYTES_PER_ELEMENT); |
| 113 | var idx = 0; |
| 114 | var adjustedPtr = ptr / dest.BYTES_PER_ELEMENT; |
| 115 | for (var x = 0; x < arr.length; x++) { |
| 116 | for (var y = 0; y < arr[0].length; y++) { |
| 117 | for (var z = 0; z < arr[0][0].length; z++) { |
| 118 | dest[adjustedPtr + idx] = arr[x][y][z]; |
| 119 | idx++; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | return ptr; |
| 124 | } |
| 125 | |
| 126 | // Caching the Float32Arrays can save having to reallocate them |
| 127 | // over and over again. |
| 128 | var Float32ArrayCache = {}; |
| 129 | |
| 130 | // Takes a 2D array of commands and puts them into the WASM heap |
| 131 | // as a 1D array. This allows them to referenced from the C++ code. |
| 132 | // Returns a 2 element array, with the first item being essentially a |
| 133 | // pointer to the array and the second item being the length of |
| 134 | // the new 1D array. |
| 135 | // |
| 136 | // Example usage: |
| 137 | // let cmds = [ |
| 138 | // [CanvasKit.MOVE_VERB, 0, 10], |
| 139 | // [CanvasKit.LINE_VERB, 30, 40], |
| 140 | // [CanvasKit.QUAD_VERB, 20, 50, 45, 60], |
| 141 | // ]; |
| 142 | function loadCmdsTypedArray(arr) { |
| 143 | var len = 0; |
| 144 | for (var r = 0; r < arr.length; r++) { |
| 145 | len += arr[r].length; |
| 146 | } |
| 147 | |
| 148 | var ta; |
| 149 | if (Float32ArrayCache[len]) { |
| 150 | ta = Float32ArrayCache[len]; |
| 151 | } else { |
| 152 | ta = new Float32Array(len); |
| 153 | Float32ArrayCache[len] = ta; |
| 154 | } |
| 155 | // Flatten into a 1d array |
| 156 | var i = 0; |
| 157 | for (var r = 0; r < arr.length; r++) { |
| 158 | for (var c = 0; c < arr[r].length; c++) { |
| 159 | var item = arr[r][c]; |
| 160 | ta[i] = item; |
| 161 | i++; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | var ptr = copy1dArray(ta, CanvasKit.HEAPF32); |
| 166 | return [ptr, len]; |
| 167 | } |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 168 | |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame^] | 169 | |
| 170 | /** |
| 171 | * Generic helper for dealing with an array of four floats. |
| 172 | */ |
| 173 | CanvasKit.FourFloatArrayHelper = function() { |
| 174 | this._floats = []; |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 175 | this._ptr = null; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame^] | 176 | |
| 177 | Object.defineProperty(this, 'length', { |
| 178 | enumerable: true, |
| 179 | get: function() { |
| 180 | return this._floats.length / 4; |
| 181 | }, |
| 182 | }); |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /** |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame^] | 186 | * push the four floats onto the end of the array - if build() has already |
| 187 | * been called, the call will return without modifying anything. |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 188 | */ |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame^] | 189 | CanvasKit.FourFloatArrayHelper.prototype.push = function(f1, f2, f3, f4) { |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 190 | if (this._ptr) { |
| 191 | SkDebug('Cannot push more points - already built'); |
| 192 | return; |
| 193 | } |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame^] | 194 | this._floats.push(f1, f2, f3, f4); |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 195 | } |
| 196 | |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame^] | 197 | /** |
| 198 | * Set the four floats at a given index - if build() has already |
| 199 | * been called, the WASM memory will be written to directly. |
| 200 | */ |
| 201 | CanvasKit.FourFloatArrayHelper.prototype.set = function(idx, f1, f2, f3, f4) { |
| 202 | if (idx < 0 || idx >= this._floats.length/4) { |
| 203 | SkDebug('Cannot set index ' + idx + ', it is out of range', this._floats.length/4); |
| 204 | return; |
| 205 | } |
| 206 | idx *= 4; |
| 207 | var BYTES_PER_ELEMENT = 4; |
| 208 | if (this._ptr) { |
| 209 | // convert this._ptr from uint8_t* to SkScalar* by dividing by 4 |
| 210 | var floatPtr = (this._ptr / BYTES_PER_ELEMENT) + idx; |
| 211 | CanvasKit.HEAPF32[floatPtr] = f1; |
| 212 | CanvasKit.HEAPF32[floatPtr + 1] = f2; |
| 213 | CanvasKit.HEAPF32[floatPtr + 2] = f3; |
| 214 | CanvasKit.HEAPF32[floatPtr + 3] = f4; |
| 215 | return; |
| 216 | } |
| 217 | this._floats[idx] = f1; |
| 218 | this._floats[idx + 1] = f2; |
| 219 | this._floats[idx + 2] = f3; |
| 220 | this._floats[idx + 3] = f4; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Copies the float data to the WASM memory and returns a pointer |
| 225 | * to that allocated memory. Once build has been called, this |
| 226 | * float array cannot be made bigger. |
| 227 | */ |
| 228 | CanvasKit.FourFloatArrayHelper.prototype.build = function() { |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 229 | if (this._ptr) { |
| 230 | return this._ptr; |
| 231 | } |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame^] | 232 | this._ptr = copy1dArray(this._floats, CanvasKit.HEAPF32); |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 233 | return this._ptr; |
| 234 | } |
| 235 | |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame^] | 236 | /** |
| 237 | * Frees the wasm memory associated with this array. Of note, |
| 238 | * the points are not removed, so push/set/build can all |
| 239 | * be called to make a newly allocated (possibly bigger) |
| 240 | * float array. |
| 241 | */ |
| 242 | CanvasKit.FourFloatArrayHelper.prototype.delete = function() { |
Kevin Lubick | d3cfbca | 2019-03-15 15:36:29 -0400 | [diff] [blame] | 243 | if (this._ptr) { |
| 244 | CanvasKit._free(this._ptr); |
| 245 | this._ptr = null; |
| 246 | } |
| 247 | } |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame^] | 248 | |
| 249 | /** |
| 250 | * Generic helper for dealing with an array of unsigned ints. |
| 251 | */ |
| 252 | CanvasKit.OneUIntArrayHelper = function() { |
| 253 | this._uints = []; |
| 254 | this._ptr = null; |
| 255 | |
| 256 | Object.defineProperty(this, 'length', { |
| 257 | enumerable: true, |
| 258 | get: function() { |
| 259 | return this._uints.length; |
| 260 | }, |
| 261 | }); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * push the unsigned int onto the end of the array - if build() has already |
| 266 | * been called, the call will return without modifying anything. |
| 267 | */ |
| 268 | CanvasKit.OneUIntArrayHelper.prototype.push = function(u) { |
| 269 | if (this._ptr) { |
| 270 | SkDebug('Cannot push more points - already built'); |
| 271 | return; |
| 272 | } |
| 273 | this._uints.push(u); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Set the uint at a given index - if build() has already |
| 278 | * been called, the WASM memory will be written to directly. |
| 279 | */ |
| 280 | CanvasKit.OneUIntArrayHelper.prototype.set = function(idx, u) { |
| 281 | if (idx < 0 || idx >= this._uints.length) { |
| 282 | SkDebug('Cannot set index ' + idx + ', it is out of range', this._uints.length); |
| 283 | return; |
| 284 | } |
| 285 | idx *= 4; |
| 286 | var BYTES_PER_ELEMENT = 4; |
| 287 | if (this._ptr) { |
| 288 | // convert this._ptr from uint8_t* to SkScalar* by dividing by 4 |
| 289 | var uintPtr = (this._ptr / BYTES_PER_ELEMENT) + idx; |
| 290 | CanvasKit.HEAPU32[uintPtr] = u; |
| 291 | return; |
| 292 | } |
| 293 | this._uints[idx] = u; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Copies the uint data to the WASM memory and returns a pointer |
| 298 | * to that allocated memory. Once build has been called, this |
| 299 | * unit array cannot be made bigger. |
| 300 | */ |
| 301 | CanvasKit.OneUIntArrayHelper.prototype.build = function() { |
| 302 | if (this._ptr) { |
| 303 | return this._ptr; |
| 304 | } |
| 305 | this._ptr = copy1dArray(this._uints, CanvasKit.HEAPU32); |
| 306 | return this._ptr; |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Frees the wasm memory associated with this array. Of note, |
| 311 | * the points are not removed, so push/set/build can all |
| 312 | * be called to make a newly allocated (possibly bigger) |
| 313 | * uint array. |
| 314 | */ |
| 315 | CanvasKit.OneUIntArrayHelper.prototype.delete = function() { |
| 316 | if (this._ptr) { |
| 317 | CanvasKit._free(this._ptr); |
| 318 | this._ptr = null; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Helper for building an array of SkRects (which are just structs |
| 324 | * of 4 floats). |
| 325 | * |
| 326 | * It can be more performant to use this helper, as |
| 327 | * the C++-side array is only allocated once (on the first call) |
| 328 | * to build. Subsequent set() operations operate directly on |
| 329 | * the C++-side array, avoiding having to re-allocate (and free) |
| 330 | * the array every time. |
| 331 | * |
| 332 | * Input points are taken as left, top, right, bottom |
| 333 | */ |
| 334 | CanvasKit.SkRectBuilder = CanvasKit.FourFloatArrayHelper; |
| 335 | /** |
| 336 | * Helper for building an array of RSXForms (which are just structs |
| 337 | * of 4 floats). |
| 338 | * |
| 339 | * It can be more performant to use this helper, as |
| 340 | * the C++-side array is only allocated once (on the first call) |
| 341 | * to build. Subsequent set() operations operate directly on |
| 342 | * the C++-side array, avoiding having to re-allocate (and free) |
| 343 | * the array every time. |
| 344 | * |
| 345 | * An RSXForm is a compressed form of a rotation+scale matrix. |
| 346 | * |
| 347 | * [ scos -ssin tx ] |
| 348 | * [ ssin scos ty ] |
| 349 | * [ 0 0 1 ] |
| 350 | * |
| 351 | * Input points are taken as scos, ssin, tx, ty |
| 352 | */ |
| 353 | CanvasKit.RSXFormBuilder = CanvasKit.FourFloatArrayHelper; |
| 354 | |
| 355 | /** |
| 356 | * Helper for building an array of SkColor |
| 357 | * |
| 358 | * It can be more performant to use this helper, as |
| 359 | * the C++-side array is only allocated once (on the first call) |
| 360 | * to build. Subsequent set() operations operate directly on |
| 361 | * the C++-side array, avoiding having to re-allocate (and free) |
| 362 | * the array every time. |
| 363 | */ |
| 364 | CanvasKit.SkColorBuilder = CanvasKit.OneUIntArrayHelper; |