Kevin Lubick | 9e2d384 | 2020-04-01 13:42:15 -0400 | [diff] [blame^] | 1 | describe('SkMatrix', () => { |
| 2 | |
| 3 | beforeEach(async () => { |
| 4 | await LoadCanvasKit; |
| 5 | }); |
| 6 | |
| 7 | it('can multiply matrices together', () => { |
| 8 | const first = CanvasKit.SkMatrix.rotated(Math.PI/2, 10, 20); |
| 9 | const second = CanvasKit.SkMatrix.scaled(1, 2, 3, 4); |
| 10 | function setup(ctx) {} |
| 11 | |
| 12 | function test(ctx) { |
| 13 | ctx.result = CanvasKit.SkMatrix.multiply(first, second); |
| 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)', () => { |
| 25 | const matr = CanvasKit.SkMatrix.multiply( |
| 26 | CanvasKit.SkMatrix.rotated(Math.PI/2, 10, 20), |
| 27 | CanvasKit.SkMatrix.scaled(1, 2, 3, 4), |
| 28 | ); // make an arbitrary, but interesting matrix |
| 29 | function setup(ctx) {} |
| 30 | |
| 31 | function test(ctx) { |
| 32 | for (let i = 0; i < 30; i++) { |
| 33 | const pt = CanvasKit.SkMatrix.mapPoints(matr, [i, i]); |
| 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 | |
| 45 | it('can be used to create a shader', () => { |
| 46 | const matr = CanvasKit.SkMatrix.multiply( |
| 47 | CanvasKit.SkMatrix.rotated(Math.PI/2, 10, 20), |
| 48 | CanvasKit.SkMatrix.scaled(1, 2, 3, 4), |
| 49 | ); |
| 50 | function setup(ctx) {} |
| 51 | |
| 52 | function test(ctx) { |
| 53 | const shader = CanvasKit.SkShader.MakeSweepGradient( |
| 54 | 100, 100, |
| 55 | [CanvasKit.GREEN, CanvasKit.BLUE], |
| 56 | [0.0, 1.0], |
| 57 | CanvasKit.TileMode.Clamp, |
| 58 | matr, |
| 59 | ); |
| 60 | shader.delete(); |
| 61 | } |
| 62 | |
| 63 | function teardown(ctx) {} |
| 64 | |
| 65 | benchmarkAndReport('skmatrix_makeShader', setup, test, teardown); |
| 66 | }); |
| 67 | }); |
| 68 | |
| 69 | describe('DOMMatrix', () => { |
| 70 | |
| 71 | beforeEach(async () => { |
| 72 | await LoadCanvasKit; |
| 73 | }); |
| 74 | |
| 75 | it('can multiply matrices together', () => { |
| 76 | const first = new DOMMatrix().translate(10, 20).rotate(90).translate(-10, -20); |
| 77 | const second = new DOMMatrix().translate(3, 4).scale(1, 2).translate(-3, -4); |
| 78 | function setup(ctx) {} |
| 79 | |
| 80 | function test(ctx) { |
| 81 | ctx.result = first.multiply(second) |
| 82 | if (ctx.result.length === 18) { |
| 83 | throw 'this is here to keep the result from being optimized away'; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | function teardown(ctx) {} |
| 88 | |
| 89 | benchmarkAndReport('dommatrix_multiply', setup, test, teardown); |
| 90 | }); |
| 91 | |
| 92 | it('can transform a point using a matrix (transformPoint)', () => { |
| 93 | const matr = new DOMMatrix().translate(10, 20).rotate(90).translate(-10, -20) |
| 94 | .multiply(new DOMMatrix().translate(3, 4).scale(1, 2).translate(-3, -4)); |
| 95 | function setup(ctx) {} |
| 96 | |
| 97 | function test(ctx) { |
| 98 | for (let i = 0; i < 30; i++) { |
| 99 | const pt = matr.transformPoint(new DOMPoint(i, i)) |
| 100 | if (pt.length === 18) { |
| 101 | throw 'this is here to keep pt from being optimized away'; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | function teardown(ctx) {} |
| 107 | |
| 108 | benchmarkAndReport('dommatrix_transformPoint', setup, test, teardown); |
| 109 | }); |
| 110 | |
| 111 | it('can be used to create a shader', () => { |
| 112 | const matr = new DOMMatrix().translate(10, 20).rotate(90).translate(-10, -20) |
| 113 | .multiply(new DOMMatrix().translate(3, 4).scale(1, 2).translate(-3, -4)); |
| 114 | function setup(ctx) {} |
| 115 | |
| 116 | function test(ctx) { |
| 117 | const shader = CanvasKit.SkShader.MakeSweepGradient( |
| 118 | 100, 100, |
| 119 | [CanvasKit.GREEN, CanvasKit.BLUE], |
| 120 | [0.0, 1.0], |
| 121 | CanvasKit.TileMode.Clamp, |
| 122 | matr, |
| 123 | ); |
| 124 | shader.delete(); |
| 125 | } |
| 126 | |
| 127 | function teardown(ctx) {} |
| 128 | |
| 129 | benchmarkAndReport('dommatrix_makeShader', setup, test, teardown); |
| 130 | }); |
| 131 | }); |