blob: 76c7e39ca46f282104a4b83835f0ff3eb07b67c6 [file] [log] [blame]
Kevin Lubick2c3cec92021-01-21 11:41:01 -05001// TODO(kjlubick)
2// The remaining functions here are deprecated and should be removed eventually.
Kevin Lubickbe728012020-09-03 11:57:12 +00003
Kevin Lubickf5ea37f2019-02-28 10:06:18 -05004// Caching the Float32Arrays can save having to reallocate them
5// over and over again.
6var 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 Lubickd9b9e5e2020-06-23 16:58:10 -040020// TODO(kjlubick) remove this and Float32ArrayCache (superceded by Malloc).
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050021function 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 Lubickf8823b52020-09-03 10:02:10 -040044 var ptr = copy1dArray(ta, 'HEAPF32');
Kevin Lubickf5ea37f2019-02-28 10:06:18 -050045 return [ptr, len];
46}
Kevin Lubickd3cfbca2019-03-15 15:36:29 -040047
Kevin Lubick97440de2020-09-29 17:58:21 -040048// TODO(kjlubick) remove Builders - no longer needed now that Malloc is a thing.
Kevin Lubickee91c072019-03-29 10:39:52 -040049/**
50 * Generic helper for dealing with an array of four floats.
51 */
52CanvasKit.FourFloatArrayHelper = function() {
53 this._floats = [];
Kevin Lubickd3cfbca2019-03-15 15:36:29 -040054 this._ptr = null;
Kevin Lubickee91c072019-03-29 10:39:52 -040055
56 Object.defineProperty(this, 'length', {
57 enumerable: true,
58 get: function() {
59 return this._floats.length / 4;
60 },
61 });
Kevin Lubicke7c1a732020-12-04 09:10:39 -050062};
Kevin Lubickd3cfbca2019-03-15 15:36:29 -040063
64/**
Kevin Lubickee91c072019-03-29 10:39:52 -040065 * 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 Lubickd3cfbca2019-03-15 15:36:29 -040067 */
Kevin Lubickee91c072019-03-29 10:39:52 -040068CanvasKit.FourFloatArrayHelper.prototype.push = function(f1, f2, f3, f4) {
Kevin Lubickd3cfbca2019-03-15 15:36:29 -040069 if (this._ptr) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040070 Debug('Cannot push more points - already built');
Kevin Lubickd3cfbca2019-03-15 15:36:29 -040071 return;
72 }
Kevin Lubickee91c072019-03-29 10:39:52 -040073 this._floats.push(f1, f2, f3, f4);
Kevin Lubicke7c1a732020-12-04 09:10:39 -050074};
Kevin Lubickd3cfbca2019-03-15 15:36:29 -040075
Kevin Lubickee91c072019-03-29 10:39:52 -040076/**
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 */
80CanvasKit.FourFloatArrayHelper.prototype.set = function(idx, f1, f2, f3, f4) {
81 if (idx < 0 || idx >= this._floats.length/4) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040082 Debug('Cannot set index ' + idx + ', it is out of range', this._floats.length/4);
Kevin Lubickee91c072019-03-29 10:39:52 -040083 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 Lubicke7c1a732020-12-04 09:10:39 -0500100};
Kevin Lubickee91c072019-03-29 10:39:52 -0400101
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 */
107CanvasKit.FourFloatArrayHelper.prototype.build = function() {
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400108 if (this._ptr) {
109 return this._ptr;
110 }
Kevin Lubickf8823b52020-09-03 10:02:10 -0400111 this._ptr = copy1dArray(this._floats, 'HEAPF32');
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400112 return this._ptr;
Kevin Lubicke7c1a732020-12-04 09:10:39 -0500113};
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400114
Kevin Lubickee91c072019-03-29 10:39:52 -0400115/**
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 */
121CanvasKit.FourFloatArrayHelper.prototype.delete = function() {
Kevin Lubickd3cfbca2019-03-15 15:36:29 -0400122 if (this._ptr) {
123 CanvasKit._free(this._ptr);
124 this._ptr = null;
125 }
Kevin Lubicke7c1a732020-12-04 09:10:39 -0500126};
Kevin Lubickee91c072019-03-29 10:39:52 -0400127
128/**
129 * Generic helper for dealing with an array of unsigned ints.
130 */
131CanvasKit.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 Lubicke7c1a732020-12-04 09:10:39 -0500141};
Kevin Lubickee91c072019-03-29 10:39:52 -0400142
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 */
147CanvasKit.OneUIntArrayHelper.prototype.push = function(u) {
148 if (this._ptr) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400149 Debug('Cannot push more points - already built');
Kevin Lubickee91c072019-03-29 10:39:52 -0400150 return;
151 }
152 this._uints.push(u);
Kevin Lubicke7c1a732020-12-04 09:10:39 -0500153};
Kevin Lubickee91c072019-03-29 10:39:52 -0400154
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 */
159CanvasKit.OneUIntArrayHelper.prototype.set = function(idx, u) {
160 if (idx < 0 || idx >= this._uints.length) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400161 Debug('Cannot set index ' + idx + ', it is out of range', this._uints.length);
Kevin Lubickee91c072019-03-29 10:39:52 -0400162 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 Lubicke7c1a732020-12-04 09:10:39 -0500173};
Kevin Lubickee91c072019-03-29 10:39:52 -0400174
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 */
180CanvasKit.OneUIntArrayHelper.prototype.build = function() {
181 if (this._ptr) {
182 return this._ptr;
183 }
Kevin Lubickf8823b52020-09-03 10:02:10 -0400184 this._ptr = copy1dArray(this._uints, 'HEAPU32');
Kevin Lubickee91c072019-03-29 10:39:52 -0400185 return this._ptr;
Kevin Lubicke7c1a732020-12-04 09:10:39 -0500186};
Kevin Lubickee91c072019-03-29 10:39:52 -0400187
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 */
194CanvasKit.OneUIntArrayHelper.prototype.delete = function() {
195 if (this._ptr) {
196 CanvasKit._free(this._ptr);
197 this._ptr = null;
198 }
Kevin Lubicke7c1a732020-12-04 09:10:39 -0500199};
Kevin Lubickee91c072019-03-29 10:39:52 -0400200
201/**
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400202 * Helper for building an array of Rects (which are just structs
Kevin Lubickee91c072019-03-29 10:39:52 -0400203 * 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 Lubick54c1b3d2020-10-07 16:09:22 -0400213CanvasKit.RectBuilder = CanvasKit.FourFloatArrayHelper;
Kevin Lubickee91c072019-03-29 10:39:52 -0400214/**
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 */
232CanvasKit.RSXFormBuilder = CanvasKit.FourFloatArrayHelper;
233
234/**
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400235 * Helper for building an array of Color
Kevin Lubickee91c072019-03-29 10:39:52 -0400236 *
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 Lubick54c1b3d2020-10-07 16:09:22 -0400243CanvasKit.ColorBuilder = CanvasKit.OneUIntArrayHelper;