blob: 16be49fe2b88a53f32c7356295d61da88fc00f20 [file] [log] [blame]
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05001// helper JS that could be used anywhere in the glue code
Kevin Lubick217056c2018-09-20 17:39:31 -04002
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05003function clamp(c) {
4 return Math.round(Math.max(0, Math.min(c || 0, 255)));
5}
Kevin Lubick217056c2018-09-20 17:39:31 -04006
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05007// 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
11CanvasKit.Color = function(r, g, b, a) {
12 if (a === undefined) {
13 a = 1;
Kevin Lubick217056c2018-09-20 17:39:31 -040014 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050015 return (clamp(a*255) << 24) | (clamp(r) << 16) | (clamp(g) << 8) | (clamp(b) << 0);
16}
Kevin Lubick217056c2018-09-20 17:39:31 -040017
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050018// returns [r, g, b, a] from a color
19// where a is scaled between 0 and 1.0
20CanvasKit.getColorComponents = function(color) {
21 return [
22 (color >> 16) & 0xFF,
23 (color >> 8) & 0xFF,
24 (color >> 0) & 0xFF,
25 ((color >> 24) & 0xFF) / 255,
26 ]
27}
28
29CanvasKit.multiplyByAlpha = function(color, alpha) {
30 if (alpha === 1) {
31 return color;
Kevin Lubick217056c2018-09-20 17:39:31 -040032 }
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050033 // extract as int from 0 to 255
34 var a = (color >> 24) & 0xFF;
35 a *= alpha;
36 // mask off the old alpha
37 color &= 0xFFFFFF;
38 return clamp(a) << 24 | color;
39}
Kevin Lubick61ef7b22018-11-27 13:26:59 -050040
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050041function radiansToDegrees(rad) {
42 return (rad / Math.PI) * 180;
43}
Kevin Lubick12c0e502018-11-28 12:51:56 -050044
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050045function degreesToRadians(deg) {
46 return (deg / 180) * Math.PI;
47}
Kevin Lubick12c0e502018-11-28 12:51:56 -050048
49// See https://stackoverflow.com/a/31090240
50// This contraption keeps closure from minifying away the check
51// if btoa is defined *and* prevents runtime "btoa" or "window" is not defined.
52// Defined outside any scopes to make it available in all files.
53var isNode = !(new Function("try {return this===window;}catch(e){ return false;}")());
Kevin Lubick1646e7d2018-12-07 13:03:08 -050054
55function almostEqual(floata, floatb) {
56 return Math.abs(floata - floatb) < 0.00001;
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050057}
58
59
60var nullptr = 0; // emscripten doesn't like to take null as uintptr_t
61
62// arr can be a normal JS array or a TypedArray
63// dest is something like CanvasKit.HEAPF32
64function copy1dArray(arr, dest) {
65 if (!arr || !arr.length) {
66 return nullptr;
67 }
68 var ptr = CanvasKit._malloc(arr.length * dest.BYTES_PER_ELEMENT);
69 // In c++ terms, the WASM heap is a uint8_t*, a long buffer/array of single
70 // byte elements. When we run _malloc, we always get an offset/pointer into
71 // that block of memory.
72 // CanvasKit exposes some different views to make it easier to work with
73 // different types. HEAPF32 for example, exposes it as a float*
74 // However, to make the ptr line up, we have to do some pointer arithmetic.
75 // Concretely, we need to convert ptr to go from an index into a 1-byte-wide
76 // buffer to an index into a 4-byte-wide buffer (in the case of HEAPF32)
77 // and thus we divide ptr by 4.
78 dest.set(arr, ptr / dest.BYTES_PER_ELEMENT);
79 return ptr;
80}
81
82// arr should be a non-jagged 2d JS array (TypedArrays can't be nested
83// inside themselves.)
84// dest is something like CanvasKit.HEAPF32
85function copy2dArray(arr, dest) {
86 if (!arr || !arr.length) {
87 return nullptr;
88 }
89 var ptr = CanvasKit._malloc(arr.length * arr[0].length * dest.BYTES_PER_ELEMENT);
90 var idx = 0;
91 var adjustedPtr = ptr / dest.BYTES_PER_ELEMENT;
92 for (var r = 0; r < arr.length; r++) {
93 for (var c = 0; c < arr[0].length; c++) {
94 dest[adjustedPtr + idx] = arr[r][c];
95 idx++;
96 }
97 }
98 return ptr;
99}
100
101// arr should be a non-jagged 3d JS array (TypedArrays can't be nested
102// inside themselves.)
103// dest is something like CanvasKit.HEAPF32
104function copy3dArray(arr, dest) {
105 if (!arr || !arr.length || !arr[0].length) {
106 return nullptr;
107 }
108 var ptr = CanvasKit._malloc(arr.length * arr[0].length * arr[0][0].length * dest.BYTES_PER_ELEMENT);
109 var idx = 0;
110 var adjustedPtr = ptr / dest.BYTES_PER_ELEMENT;
111 for (var x = 0; x < arr.length; x++) {
112 for (var y = 0; y < arr[0].length; y++) {
113 for (var z = 0; z < arr[0][0].length; z++) {
114 dest[adjustedPtr + idx] = arr[x][y][z];
115 idx++;
116 }
117 }
118 }
119 return ptr;
120}
121
122// Caching the Float32Arrays can save having to reallocate them
123// over and over again.
124var Float32ArrayCache = {};
125
126// Takes a 2D array of commands and puts them into the WASM heap
127// as a 1D array. This allows them to referenced from the C++ code.
128// Returns a 2 element array, with the first item being essentially a
129// pointer to the array and the second item being the length of
130// the new 1D array.
131//
132// Example usage:
133// let cmds = [
134// [CanvasKit.MOVE_VERB, 0, 10],
135// [CanvasKit.LINE_VERB, 30, 40],
136// [CanvasKit.QUAD_VERB, 20, 50, 45, 60],
137// ];
138function loadCmdsTypedArray(arr) {
139 var len = 0;
140 for (var r = 0; r < arr.length; r++) {
141 len += arr[r].length;
142 }
143
144 var ta;
145 if (Float32ArrayCache[len]) {
146 ta = Float32ArrayCache[len];
147 } else {
148 ta = new Float32Array(len);
149 Float32ArrayCache[len] = ta;
150 }
151 // Flatten into a 1d array
152 var i = 0;
153 for (var r = 0; r < arr.length; r++) {
154 for (var c = 0; c < arr[r].length; c++) {
155 var item = arr[r][c];
156 ta[i] = item;
157 i++;
158 }
159 }
160
161 var ptr = copy1dArray(ta, CanvasKit.HEAPF32);
162 return [ptr, len];
163}