blob: b90ae6c1c67c379d0aae2e150e4391bff19a8631 [file] [log] [blame]
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04001describe('Matrix (3x3)', () => {
Kevin Lubick9e2d3842020-04-01 13:42:15 -04002
3 beforeEach(async () => {
4 await LoadCanvasKit;
5 });
6
7 it('can multiply matrices together', () => {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -04008 const first = CanvasKit.Matrix.rotated(Math.PI/2, 10, 20);
9 const second = CanvasKit.Matrix.scaled(1, 2, 3, 4);
Kevin Lubick9e2d3842020-04-01 13:42:15 -040010 function setup(ctx) {}
11
12 function test(ctx) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040013 ctx.result = CanvasKit.Matrix.multiply(first, second);
Kevin Lubick9e2d3842020-04-01 13:42:15 -040014 if (ctx.result.length === 18) {
15 throw 'this is here to keep the result from being optimized away';
16 }
17 }
18
19 function teardown(ctx) {}
20
21 benchmarkAndReport('skmatrix_multiply', setup, test, teardown);
22 });
23
24 it('can transform a point using a matrix (mapPoint)', () => {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040025 const matr = CanvasKit.Matrix.multiply(
26 CanvasKit.Matrix.rotated(Math.PI/2, 10, 20),
27 CanvasKit.Matrix.scaled(1, 2, 3, 4),
Kevin Lubick9e2d3842020-04-01 13:42:15 -040028 ); // make an arbitrary, but interesting matrix
29 function setup(ctx) {}
30
31 function test(ctx) {
32 for (let i = 0; i < 30; i++) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040033 const pt = CanvasKit.Matrix.mapPoints(matr, [i, i]);
Kevin Lubick9e2d3842020-04-01 13:42:15 -040034 if (pt.length === 18) {
35 throw 'this is here to keep pt from being optimized away';
36 }
37 }
38 }
39
40 function teardown(ctx) {}
41
42 benchmarkAndReport('skmatrix_transformPoint', setup, test, teardown);
43 });
44
Kevin Lubickc89ca0b2020-04-02 14:30:00 -040045 it('can invert a matrix', () => {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040046 const matr = CanvasKit.Matrix.multiply(
47 CanvasKit.Matrix.rotated(Math.PI/2, 10, 20),
48 CanvasKit.Matrix.scaled(1, 2, 3, 4),
Kevin Lubickc89ca0b2020-04-02 14:30:00 -040049 );
50 function setup(ctx) {}
51
52 function test(ctx) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040053 ctx.result = CanvasKit.Matrix.invert(matr);
Kevin Lubickc89ca0b2020-04-02 14:30:00 -040054 if (ctx.result.length === 18) {
55 throw 'this is here to keep the result from being optimized away';
56 }
57 }
58
59 function teardown(ctx) {}
60
61 benchmarkAndReport('skmatrix_invert', setup, test, teardown);
62 });
63
Kevin Lubick9e2d3842020-04-01 13:42:15 -040064 it('can be used to create a shader', () => {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040065 const matr = CanvasKit.Matrix.multiply(
66 CanvasKit.Matrix.rotated(Math.PI/2, 10, 20),
67 CanvasKit.Matrix.scaled(1, 2, 3, 4),
Kevin Lubick9e2d3842020-04-01 13:42:15 -040068 );
69 function setup(ctx) {}
70
71 function test(ctx) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040072 const shader = CanvasKit.Shader.MakeSweepGradient(
Kevin Lubick9e2d3842020-04-01 13:42:15 -040073 100, 100,
74 [CanvasKit.GREEN, CanvasKit.BLUE],
75 [0.0, 1.0],
76 CanvasKit.TileMode.Clamp,
77 matr,
78 );
79 shader.delete();
80 }
81
82 function teardown(ctx) {}
83
84 benchmarkAndReport('skmatrix_makeShader', setup, test, teardown);
85 });
86});
87
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040088describe('M44 (4x4 matrix)', () => {
Kevin Lubickc89ca0b2020-04-02 14:30:00 -040089
90 beforeEach(async () => {
91 await LoadCanvasKit;
92 });
93
94 it('can multiply matrices together', () => {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040095 const first = CanvasKit.M44.rotated([10, 20, 30], Math.PI/2);
96 const second = CanvasKit.M44.scaled([1, 2, 3]);
Kevin Lubickc89ca0b2020-04-02 14:30:00 -040097 function setup(ctx) {}
98
99 function test(ctx) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400100 ctx.result = CanvasKit.M44.multiply(first, second);
Kevin Lubickc89ca0b2020-04-02 14:30:00 -0400101 if (ctx.result.length === 18) {
102 throw 'this is here to keep the result from being optimized away';
103 }
104 }
105
106 function teardown(ctx) {}
107
108 benchmarkAndReport('skm44_multiply', setup, test, teardown);
109 });
110
111 it('can invert a matrix', () => {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400112 const matr = CanvasKit.M44.multiply(
113 CanvasKit.M44.rotated([10, 20, 30], Math.PI/2),
114 CanvasKit.M44.scaled([1, 2, 3]),
Kevin Lubickc89ca0b2020-04-02 14:30:00 -0400115 );
116 function setup(ctx) {}
117
118 function test(ctx) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400119 ctx.result = CanvasKit.M44.invert(matr);
Kevin Lubickc89ca0b2020-04-02 14:30:00 -0400120 if (ctx.result.length === 18) {
121 throw 'this is here to keep the result from being optimized away';
122 }
123 }
124
125 function teardown(ctx) {}
126
127 benchmarkAndReport('skm44_invert', setup, test, teardown);
128 });
129
130 it('can be used on a canvas', () => {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400131 const matr = CanvasKit.M44.multiply(
132 CanvasKit.M44.rotated([10, 20, 30], Math.PI/2),
133 CanvasKit.M44.scaled([1, 2, 3]),
Kevin Lubickc89ca0b2020-04-02 14:30:00 -0400134 );
135 function setup(ctx) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400136 ctx.canvas = new CanvasKit.Canvas();
Kevin Lubickc89ca0b2020-04-02 14:30:00 -0400137 }
138
139 function test(ctx) {
Kevin Lubickc1d08982020-04-06 13:52:15 -0400140 ctx.canvas.concat(matr);
Kevin Lubickc89ca0b2020-04-02 14:30:00 -0400141 }
142
143 function teardown(ctx) {
144 ctx.canvas.delete();
145 }
146
147 benchmarkAndReport('skm44_concat', setup, test, teardown);
148 });
149});
150
Kevin Lubick9e2d3842020-04-01 13:42:15 -0400151describe('DOMMatrix', () => {
152
153 beforeEach(async () => {
154 await LoadCanvasKit;
155 });
156
157 it('can multiply matrices together', () => {
158 const first = new DOMMatrix().translate(10, 20).rotate(90).translate(-10, -20);
159 const second = new DOMMatrix().translate(3, 4).scale(1, 2).translate(-3, -4);
160 function setup(ctx) {}
161
162 function test(ctx) {
Kevin Lubickc89ca0b2020-04-02 14:30:00 -0400163 const result = first.multiply(second)
164 if (result.length === 18) {
Kevin Lubick9e2d3842020-04-01 13:42:15 -0400165 throw 'this is here to keep the result from being optimized away';
166 }
167 }
168
169 function teardown(ctx) {}
170
171 benchmarkAndReport('dommatrix_multiply', setup, test, teardown);
172 });
173
174 it('can transform a point using a matrix (transformPoint)', () => {
175 const matr = new DOMMatrix().translate(10, 20).rotate(90).translate(-10, -20)
176 .multiply(new DOMMatrix().translate(3, 4).scale(1, 2).translate(-3, -4));
Kevin Lubickc89ca0b2020-04-02 14:30:00 -0400177
178 const reusablePt = new DOMPoint(0, 0)
Kevin Lubick9e2d3842020-04-01 13:42:15 -0400179 function setup(ctx) {}
180
181 function test(ctx) {
182 for (let i = 0; i < 30; i++) {
Kevin Lubickc89ca0b2020-04-02 14:30:00 -0400183 reusablePt.X = i; reusablePt.Y = i;
184 const pt = matr.transformPoint(reusablePt);
Kevin Lubick9e2d3842020-04-01 13:42:15 -0400185 if (pt.length === 18) {
186 throw 'this is here to keep pt from being optimized away';
187 }
188 }
189 }
190
191 function teardown(ctx) {}
192
193 benchmarkAndReport('dommatrix_transformPoint', setup, test, teardown);
194 });
195
Kevin Lubickc89ca0b2020-04-02 14:30:00 -0400196 it('can invert a matrix', () => {
197 const matr = new DOMMatrix().translate(10, 20).rotate(90).translate(-10, -20)
198 .multiply(new DOMMatrix().translate(3, 4).scale(1, 2).translate(-3, -4));
199 function setup(ctx) {}
200
201 function test(ctx) {
202 const inverted = matr.inverse();
203 if (inverted.length === 18) {
204 throw 'this is here to keep the result from being optimized away';
205 }
206 }
207
208 function teardown(ctx) {}
209
210 benchmarkAndReport('dommatrix_invert', setup, test, teardown);
211 });
212
Kevin Lubick9e2d3842020-04-01 13:42:15 -0400213 it('can be used to create a shader', () => {
214 const matr = new DOMMatrix().translate(10, 20).rotate(90).translate(-10, -20)
215 .multiply(new DOMMatrix().translate(3, 4).scale(1, 2).translate(-3, -4));
216 function setup(ctx) {}
217
218 function test(ctx) {
Kevin Lubick54c1b3d2020-10-07 16:09:22 -0400219 const shader = CanvasKit.Shader.MakeSweepGradient(
Kevin Lubick9e2d3842020-04-01 13:42:15 -0400220 100, 100,
221 [CanvasKit.GREEN, CanvasKit.BLUE],
222 [0.0, 1.0],
223 CanvasKit.TileMode.Clamp,
224 matr,
225 );
226 shader.delete();
227 }
228
229 function teardown(ctx) {}
230
231 benchmarkAndReport('dommatrix_makeShader', setup, test, teardown);
232 });
Kevin Lubick6bffe392020-04-02 15:24:15 -0400233});