Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1 | describe('Matrix (3x3)', () => { |
Kevin Lubick | 9e2d384 | 2020-04-01 13:42:15 -0400 | [diff] [blame] | 2 | |
| 3 | beforeEach(async () => { |
| 4 | await LoadCanvasKit; |
| 5 | }); |
| 6 | |
| 7 | it('can multiply matrices together', () => { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 8 | const first = CanvasKit.Matrix.rotated(Math.PI/2, 10, 20); |
| 9 | const second = CanvasKit.Matrix.scaled(1, 2, 3, 4); |
Kevin Lubick | 9e2d384 | 2020-04-01 13:42:15 -0400 | [diff] [blame] | 10 | function setup(ctx) {} |
| 11 | |
| 12 | function test(ctx) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 13 | ctx.result = CanvasKit.Matrix.multiply(first, second); |
Kevin Lubick | 9e2d384 | 2020-04-01 13:42:15 -0400 | [diff] [blame] | 14 | 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 Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 25 | const matr = CanvasKit.Matrix.multiply( |
| 26 | CanvasKit.Matrix.rotated(Math.PI/2, 10, 20), |
| 27 | CanvasKit.Matrix.scaled(1, 2, 3, 4), |
Kevin Lubick | 9e2d384 | 2020-04-01 13:42:15 -0400 | [diff] [blame] | 28 | ); // make an arbitrary, but interesting matrix |
| 29 | function setup(ctx) {} |
| 30 | |
| 31 | function test(ctx) { |
| 32 | for (let i = 0; i < 30; i++) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 33 | const pt = CanvasKit.Matrix.mapPoints(matr, [i, i]); |
Kevin Lubick | 9e2d384 | 2020-04-01 13:42:15 -0400 | [diff] [blame] | 34 | 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 Lubick | c89ca0b | 2020-04-02 14:30:00 -0400 | [diff] [blame] | 45 | it('can invert a matrix', () => { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 46 | const matr = CanvasKit.Matrix.multiply( |
| 47 | CanvasKit.Matrix.rotated(Math.PI/2, 10, 20), |
| 48 | CanvasKit.Matrix.scaled(1, 2, 3, 4), |
Kevin Lubick | c89ca0b | 2020-04-02 14:30:00 -0400 | [diff] [blame] | 49 | ); |
| 50 | function setup(ctx) {} |
| 51 | |
| 52 | function test(ctx) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 53 | ctx.result = CanvasKit.Matrix.invert(matr); |
Kevin Lubick | c89ca0b | 2020-04-02 14:30:00 -0400 | [diff] [blame] | 54 | 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 Lubick | 9e2d384 | 2020-04-01 13:42:15 -0400 | [diff] [blame] | 64 | it('can be used to create a shader', () => { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 65 | const matr = CanvasKit.Matrix.multiply( |
| 66 | CanvasKit.Matrix.rotated(Math.PI/2, 10, 20), |
| 67 | CanvasKit.Matrix.scaled(1, 2, 3, 4), |
Kevin Lubick | 9e2d384 | 2020-04-01 13:42:15 -0400 | [diff] [blame] | 68 | ); |
| 69 | function setup(ctx) {} |
| 70 | |
| 71 | function test(ctx) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 72 | const shader = CanvasKit.Shader.MakeSweepGradient( |
Kevin Lubick | 9e2d384 | 2020-04-01 13:42:15 -0400 | [diff] [blame] | 73 | 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 Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 88 | describe('M44 (4x4 matrix)', () => { |
Kevin Lubick | c89ca0b | 2020-04-02 14:30:00 -0400 | [diff] [blame] | 89 | |
| 90 | beforeEach(async () => { |
| 91 | await LoadCanvasKit; |
| 92 | }); |
| 93 | |
| 94 | it('can multiply matrices together', () => { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 95 | const first = CanvasKit.M44.rotated([10, 20, 30], Math.PI/2); |
| 96 | const second = CanvasKit.M44.scaled([1, 2, 3]); |
Kevin Lubick | c89ca0b | 2020-04-02 14:30:00 -0400 | [diff] [blame] | 97 | function setup(ctx) {} |
| 98 | |
| 99 | function test(ctx) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 100 | ctx.result = CanvasKit.M44.multiply(first, second); |
Kevin Lubick | c89ca0b | 2020-04-02 14:30:00 -0400 | [diff] [blame] | 101 | 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 Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 112 | const matr = CanvasKit.M44.multiply( |
| 113 | CanvasKit.M44.rotated([10, 20, 30], Math.PI/2), |
| 114 | CanvasKit.M44.scaled([1, 2, 3]), |
Kevin Lubick | c89ca0b | 2020-04-02 14:30:00 -0400 | [diff] [blame] | 115 | ); |
| 116 | function setup(ctx) {} |
| 117 | |
| 118 | function test(ctx) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 119 | ctx.result = CanvasKit.M44.invert(matr); |
Kevin Lubick | c89ca0b | 2020-04-02 14:30:00 -0400 | [diff] [blame] | 120 | 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 Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 131 | const matr = CanvasKit.M44.multiply( |
| 132 | CanvasKit.M44.rotated([10, 20, 30], Math.PI/2), |
| 133 | CanvasKit.M44.scaled([1, 2, 3]), |
Kevin Lubick | c89ca0b | 2020-04-02 14:30:00 -0400 | [diff] [blame] | 134 | ); |
| 135 | function setup(ctx) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 136 | ctx.canvas = new CanvasKit.Canvas(); |
Kevin Lubick | c89ca0b | 2020-04-02 14:30:00 -0400 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | function test(ctx) { |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 140 | ctx.canvas.concat(matr); |
Kevin Lubick | c89ca0b | 2020-04-02 14:30:00 -0400 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | function teardown(ctx) { |
| 144 | ctx.canvas.delete(); |
| 145 | } |
| 146 | |
| 147 | benchmarkAndReport('skm44_concat', setup, test, teardown); |
| 148 | }); |
| 149 | }); |
| 150 | |
Kevin Lubick | 9e2d384 | 2020-04-01 13:42:15 -0400 | [diff] [blame] | 151 | describe('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 Lubick | c89ca0b | 2020-04-02 14:30:00 -0400 | [diff] [blame] | 163 | const result = first.multiply(second) |
| 164 | if (result.length === 18) { |
Kevin Lubick | 9e2d384 | 2020-04-01 13:42:15 -0400 | [diff] [blame] | 165 | 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 Lubick | c89ca0b | 2020-04-02 14:30:00 -0400 | [diff] [blame] | 177 | |
| 178 | const reusablePt = new DOMPoint(0, 0) |
Kevin Lubick | 9e2d384 | 2020-04-01 13:42:15 -0400 | [diff] [blame] | 179 | function setup(ctx) {} |
| 180 | |
| 181 | function test(ctx) { |
| 182 | for (let i = 0; i < 30; i++) { |
Kevin Lubick | c89ca0b | 2020-04-02 14:30:00 -0400 | [diff] [blame] | 183 | reusablePt.X = i; reusablePt.Y = i; |
| 184 | const pt = matr.transformPoint(reusablePt); |
Kevin Lubick | 9e2d384 | 2020-04-01 13:42:15 -0400 | [diff] [blame] | 185 | 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 Lubick | c89ca0b | 2020-04-02 14:30:00 -0400 | [diff] [blame] | 196 | 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 Lubick | 9e2d384 | 2020-04-01 13:42:15 -0400 | [diff] [blame] | 213 | 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 Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 219 | const shader = CanvasKit.Shader.MakeSweepGradient( |
Kevin Lubick | 9e2d384 | 2020-04-01 13:42:15 -0400 | [diff] [blame] | 220 | 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 Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 233 | }); |