• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/core/SkMath.h"
9 #include "include/core/SkPoint3.h"
10 #include "include/utils/SkRandom.h"
11 #include "src/core/SkMatrixPriv.h"
12 #include "src/core/SkMatrixUtils.h"
13 #include "tests/Test.h"
14 
nearly_equal_scalar(SkScalar a,SkScalar b)15 static bool nearly_equal_scalar(SkScalar a, SkScalar b) {
16     const SkScalar tolerance = SK_Scalar1 / 200000;
17     return SkScalarAbs(a - b) <= tolerance;
18 }
19 
nearly_equal(const SkMatrix & a,const SkMatrix & b)20 static bool nearly_equal(const SkMatrix& a, const SkMatrix& b) {
21     for (int i = 0; i < 9; i++) {
22         if (!nearly_equal_scalar(a[i], b[i])) {
23             SkDebugf("matrices not equal [%d] %g %g\n", i, (float)a[i], (float)b[i]);
24             return false;
25         }
26     }
27     return true;
28 }
29 
float_bits(float f)30 static int float_bits(float f) {
31     int result;
32     memcpy(&result, &f, 4);
33     return result;
34 }
35 
are_equal(skiatest::Reporter * reporter,const SkMatrix & a,const SkMatrix & b)36 static bool are_equal(skiatest::Reporter* reporter,
37                       const SkMatrix& a,
38                       const SkMatrix& b) {
39     bool equal = a == b;
40     bool cheapEqual = SkMatrixPriv::CheapEqual(a, b);
41     if (equal != cheapEqual) {
42         if (equal) {
43             bool foundZeroSignDiff = false;
44             for (int i = 0; i < 9; ++i) {
45                 float aVal = a.get(i);
46                 float bVal = b.get(i);
47                 int aValI = float_bits(aVal);
48                 int bValI = float_bits(bVal);
49                 if (0 == aVal && 0 == bVal && aValI != bValI) {
50                     foundZeroSignDiff = true;
51                 } else {
52                     REPORTER_ASSERT(reporter, aVal == bVal && aValI == bValI);
53                 }
54             }
55             REPORTER_ASSERT(reporter, foundZeroSignDiff);
56         } else {
57             bool foundNaN = false;
58             for (int i = 0; i < 9; ++i) {
59                 float aVal = a.get(i);
60                 float bVal = b.get(i);
61                 int aValI = float_bits(aVal);
62                 int bValI = float_bits(bVal);
63                 if (sk_float_isnan(aVal) && aValI == bValI) {
64                     foundNaN = true;
65                 } else {
66                     REPORTER_ASSERT(reporter, aVal == bVal && aValI == bValI);
67                 }
68             }
69             REPORTER_ASSERT(reporter, foundNaN);
70         }
71     }
72     return equal;
73 }
74 
is_identity(const SkMatrix & m)75 static bool is_identity(const SkMatrix& m) {
76     SkMatrix identity;
77     identity.reset();
78     return nearly_equal(m, identity);
79 }
80 
assert9(skiatest::Reporter * reporter,const SkMatrix & m,SkScalar a,SkScalar b,SkScalar c,SkScalar d,SkScalar e,SkScalar f,SkScalar g,SkScalar h,SkScalar i)81 static void assert9(skiatest::Reporter* reporter, const SkMatrix& m,
82                     SkScalar a, SkScalar b, SkScalar c,
83                     SkScalar d, SkScalar e, SkScalar f,
84                     SkScalar g, SkScalar h, SkScalar i) {
85     SkScalar buffer[9];
86     m.get9(buffer);
87     REPORTER_ASSERT(reporter, buffer[0] == a);
88     REPORTER_ASSERT(reporter, buffer[1] == b);
89     REPORTER_ASSERT(reporter, buffer[2] == c);
90     REPORTER_ASSERT(reporter, buffer[3] == d);
91     REPORTER_ASSERT(reporter, buffer[4] == e);
92     REPORTER_ASSERT(reporter, buffer[5] == f);
93     REPORTER_ASSERT(reporter, buffer[6] == g);
94     REPORTER_ASSERT(reporter, buffer[7] == h);
95     REPORTER_ASSERT(reporter, buffer[8] == i);
96 
97     REPORTER_ASSERT(reporter, m.rc(0, 0) == a);
98     REPORTER_ASSERT(reporter, m.rc(0, 1) == b);
99     REPORTER_ASSERT(reporter, m.rc(0, 2) == c);
100     REPORTER_ASSERT(reporter, m.rc(1, 0) == d);
101     REPORTER_ASSERT(reporter, m.rc(1, 1) == e);
102     REPORTER_ASSERT(reporter, m.rc(1, 2) == f);
103     REPORTER_ASSERT(reporter, m.rc(2, 0) == g);
104     REPORTER_ASSERT(reporter, m.rc(2, 1) == h);
105     REPORTER_ASSERT(reporter, m.rc(2, 2) == i);
106 }
107 
test_set9(skiatest::Reporter * reporter)108 static void test_set9(skiatest::Reporter* reporter) {
109 
110     SkMatrix m;
111     m.reset();
112     assert9(reporter, m, 1, 0, 0, 0, 1, 0, 0, 0, 1);
113 
114     m.setScale(2, 3);
115     assert9(reporter, m, 2, 0, 0, 0, 3, 0, 0, 0, 1);
116 
117     m.postTranslate(4, 5);
118     assert9(reporter, m, 2, 0, 4, 0, 3, 5, 0, 0, 1);
119 
120     SkScalar buffer[9];
121     sk_bzero(buffer, sizeof(buffer));
122     buffer[SkMatrix::kMScaleX] = 1;
123     buffer[SkMatrix::kMScaleY] = 1;
124     buffer[SkMatrix::kMPersp2] = 1;
125     REPORTER_ASSERT(reporter, !m.isIdentity());
126     m.set9(buffer);
127     REPORTER_ASSERT(reporter, m.isIdentity());
128 }
129 
test_matrix_recttorect(skiatest::Reporter * reporter)130 static void test_matrix_recttorect(skiatest::Reporter* reporter) {
131     SkRect src, dst;
132     SkMatrix matrix;
133 
134     src.setLTRB(0, 0, 10, 10);
135     dst = src;
136     matrix = SkMatrix::RectToRect(src, dst);
137     REPORTER_ASSERT(reporter, SkMatrix::kIdentity_Mask == matrix.getType());
138     REPORTER_ASSERT(reporter, matrix.rectStaysRect());
139 
140     dst.offset(1, 1);
141     matrix = SkMatrix::RectToRect(src, dst);
142     REPORTER_ASSERT(reporter, SkMatrix::kTranslate_Mask == matrix.getType());
143     REPORTER_ASSERT(reporter, matrix.rectStaysRect());
144 
145     dst.fRight += 1;
146     matrix = SkMatrix::RectToRect(src, dst);
147     REPORTER_ASSERT(reporter,
148                     (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask) == matrix.getType());
149     REPORTER_ASSERT(reporter, matrix.rectStaysRect());
150 
151     dst = src;
152     dst.fRight = src.fRight * 2;
153     matrix = SkMatrix::RectToRect(src, dst);
154     REPORTER_ASSERT(reporter, SkMatrix::kScale_Mask == matrix.getType());
155     REPORTER_ASSERT(reporter, matrix.rectStaysRect());
156 }
157 
test_flatten(skiatest::Reporter * reporter,const SkMatrix & m)158 static void test_flatten(skiatest::Reporter* reporter, const SkMatrix& m) {
159     // add 100 in case we have a bug, I don't want to kill my stack in the test
160     static const size_t kBufferSize = SkMatrixPriv::kMaxFlattenSize + 100;
161     char buffer[kBufferSize];
162     size_t size1 = SkMatrixPriv::WriteToMemory(m, nullptr);
163     size_t size2 = SkMatrixPriv::WriteToMemory(m, buffer);
164     REPORTER_ASSERT(reporter, size1 == size2);
165     REPORTER_ASSERT(reporter, size1 <= SkMatrixPriv::kMaxFlattenSize);
166 
167     SkMatrix m2;
168     size_t size3 = SkMatrixPriv::ReadFromMemory(&m2, buffer, kBufferSize);
169     REPORTER_ASSERT(reporter, size1 == size3);
170     REPORTER_ASSERT(reporter, are_equal(reporter, m, m2));
171 
172     char buffer2[kBufferSize];
173     size3 = SkMatrixPriv::WriteToMemory(m2, buffer2);
174     REPORTER_ASSERT(reporter, size1 == size3);
175     REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
176 }
177 
test_matrix_min_max_scale(skiatest::Reporter * reporter)178 static void test_matrix_min_max_scale(skiatest::Reporter* reporter) {
179     SkScalar scales[2];
180     bool success;
181 
182     SkMatrix identity;
183     identity.reset();
184     REPORTER_ASSERT(reporter, 1 == identity.getMinScale());
185     REPORTER_ASSERT(reporter, 1 == identity.getMaxScale());
186     success = identity.getMinMaxScales(scales);
187     REPORTER_ASSERT(reporter, success && 1 == scales[0] && 1 == scales[1]);
188 
189     SkMatrix scale;
190     scale.setScale(2, 4);
191     REPORTER_ASSERT(reporter, 2 == scale.getMinScale());
192     REPORTER_ASSERT(reporter, 4 == scale.getMaxScale());
193     success = scale.getMinMaxScales(scales);
194     REPORTER_ASSERT(reporter, success && 2 == scales[0] && 4 == scales[1]);
195 
196     SkMatrix rot90Scale;
197     rot90Scale.setRotate(90).postScale(SK_Scalar1 / 4, SK_Scalar1 / 2);
198     REPORTER_ASSERT(reporter, SK_Scalar1 / 4 == rot90Scale.getMinScale());
199     REPORTER_ASSERT(reporter, SK_Scalar1 / 2 == rot90Scale.getMaxScale());
200     success = rot90Scale.getMinMaxScales(scales);
201     REPORTER_ASSERT(reporter, success && SK_Scalar1 / 4  == scales[0] && SK_Scalar1 / 2 == scales[1]);
202 
203     SkMatrix rotate;
204     rotate.setRotate(128);
205     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(1, rotate.getMinScale(), SK_ScalarNearlyZero));
206     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(1, rotate.getMaxScale(), SK_ScalarNearlyZero));
207     success = rotate.getMinMaxScales(scales);
208     REPORTER_ASSERT(reporter, success);
209     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(1, scales[0], SK_ScalarNearlyZero));
210     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(1, scales[1], SK_ScalarNearlyZero));
211 
212     SkMatrix translate;
213     translate.setTranslate(10, -5);
214     REPORTER_ASSERT(reporter, 1 == translate.getMinScale());
215     REPORTER_ASSERT(reporter, 1 == translate.getMaxScale());
216     success = translate.getMinMaxScales(scales);
217     REPORTER_ASSERT(reporter, success && 1 == scales[0] && 1 == scales[1]);
218 
219     SkMatrix perspX;
220     perspX.reset().setPerspX(SK_Scalar1 / 1000);
221     REPORTER_ASSERT(reporter, -1 == perspX.getMinScale());
222     REPORTER_ASSERT(reporter, -1 == perspX.getMaxScale());
223     success = perspX.getMinMaxScales(scales);
224     REPORTER_ASSERT(reporter, !success);
225 
226     // skbug.com/4718
227     SkMatrix big;
228     big.setAll(2.39394089e+36f, 8.85347779e+36f, 9.26526204e+36f,
229                3.9159619e+36f, 1.44823453e+37f, 1.51559342e+37f,
230                0.f, 0.f, 1.f);
231     success = big.getMinMaxScales(scales);
232     REPORTER_ASSERT(reporter, !success);
233 
234     // skbug.com/4718
235     SkMatrix givingNegativeNearlyZeros;
236     givingNegativeNearlyZeros.setAll(0.00436534f, 0.114138f, 0.37141f,
237                                      0.00358857f, 0.0936228f, -0.0174198f,
238                                      0.f, 0.f, 1.f);
239     success = givingNegativeNearlyZeros.getMinMaxScales(scales);
240     REPORTER_ASSERT(reporter, success && 0 == scales[0]);
241 
242     SkMatrix perspY;
243     perspY.reset().setPerspY(-SK_Scalar1 / 500);
244     REPORTER_ASSERT(reporter, -1 == perspY.getMinScale());
245     REPORTER_ASSERT(reporter, -1 == perspY.getMaxScale());
246     scales[0] = -5;
247     scales[1] = -5;
248     success = perspY.getMinMaxScales(scales);
249     REPORTER_ASSERT(reporter, !success && -5 == scales[0] && -5  == scales[1]);
250 
251     SkMatrix baseMats[] = {scale, rot90Scale, rotate,
252                            translate, perspX, perspY};
253     SkMatrix mats[2*SK_ARRAY_COUNT(baseMats)];
254     for (size_t i = 0; i < SK_ARRAY_COUNT(baseMats); ++i) {
255         mats[i] = baseMats[i];
256         bool invertible = mats[i].invert(&mats[i + SK_ARRAY_COUNT(baseMats)]);
257         REPORTER_ASSERT(reporter, invertible);
258     }
259     SkRandom rand;
260     for (int m = 0; m < 1000; ++m) {
261         SkMatrix mat;
262         mat.reset();
263         for (int i = 0; i < 4; ++i) {
264             int x = rand.nextU() % SK_ARRAY_COUNT(mats);
265             mat.postConcat(mats[x]);
266         }
267 
268         SkScalar minScale = mat.getMinScale();
269         SkScalar maxScale = mat.getMaxScale();
270         REPORTER_ASSERT(reporter, (minScale < 0) == (maxScale < 0));
271         REPORTER_ASSERT(reporter, (maxScale < 0) == mat.hasPerspective());
272 
273         success = mat.getMinMaxScales(scales);
274         REPORTER_ASSERT(reporter, success == !mat.hasPerspective());
275         REPORTER_ASSERT(reporter, !success || (scales[0] == minScale && scales[1] == maxScale));
276 
277         if (mat.hasPerspective()) {
278             m -= 1; // try another non-persp matrix
279             continue;
280         }
281 
282         // test a bunch of vectors. All should be scaled by between minScale and maxScale
283         // (modulo some error) and we should find a vector that is scaled by almost each.
284         static const SkScalar gVectorScaleTol = (105 * SK_Scalar1) / 100;
285         static const SkScalar gCloseScaleTol = (97 * SK_Scalar1) / 100;
286         SkScalar max = 0, min = SK_ScalarMax;
287         SkVector vectors[1000];
288         for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
289             vectors[i].fX = rand.nextSScalar1();
290             vectors[i].fY = rand.nextSScalar1();
291             if (!vectors[i].normalize()) {
292                 i -= 1;
293                 continue;
294             }
295         }
296         mat.mapVectors(vectors, SK_ARRAY_COUNT(vectors));
297         for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
298             SkScalar d = vectors[i].length();
299             REPORTER_ASSERT(reporter, d / maxScale < gVectorScaleTol);
300             REPORTER_ASSERT(reporter, minScale / d < gVectorScaleTol);
301             if (max < d) {
302                 max = d;
303             }
304             if (min > d) {
305                 min = d;
306             }
307         }
308         REPORTER_ASSERT(reporter, max / maxScale >= gCloseScaleTol);
309         REPORTER_ASSERT(reporter, minScale / min >= gCloseScaleTol);
310     }
311 }
312 
test_matrix_preserve_shape(skiatest::Reporter * reporter)313 static void test_matrix_preserve_shape(skiatest::Reporter* reporter) {
314     SkMatrix mat;
315 
316     // identity
317     mat.setIdentity();
318     REPORTER_ASSERT(reporter, mat.isSimilarity());
319     REPORTER_ASSERT(reporter, mat.preservesRightAngles());
320 
321     // translation only
322     mat.setTranslate(100, 100);
323     REPORTER_ASSERT(reporter, mat.isSimilarity());
324     REPORTER_ASSERT(reporter, mat.preservesRightAngles());
325 
326     // scale with same size
327     mat.setScale(15, 15);
328     REPORTER_ASSERT(reporter, mat.isSimilarity());
329     REPORTER_ASSERT(reporter, mat.preservesRightAngles());
330 
331     // scale with one negative
332     mat.setScale(-15, 15);
333     REPORTER_ASSERT(reporter, mat.isSimilarity());
334     REPORTER_ASSERT(reporter, mat.preservesRightAngles());
335 
336     // scale with different size
337     mat.setScale(15, 20);
338     REPORTER_ASSERT(reporter, !mat.isSimilarity());
339     REPORTER_ASSERT(reporter, mat.preservesRightAngles());
340 
341     // scale with same size at a pivot point
342     mat.setScale(15, 15, 2, 2);
343     REPORTER_ASSERT(reporter, mat.isSimilarity());
344     REPORTER_ASSERT(reporter, mat.preservesRightAngles());
345 
346     // scale with different size at a pivot point
347     mat.setScale(15, 20, 2, 2);
348     REPORTER_ASSERT(reporter, !mat.isSimilarity());
349     REPORTER_ASSERT(reporter, mat.preservesRightAngles());
350 
351     // skew with same size
352     mat.setSkew(15, 15);
353     REPORTER_ASSERT(reporter, !mat.isSimilarity());
354     REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
355 
356     // skew with different size
357     mat.setSkew(15, 20);
358     REPORTER_ASSERT(reporter, !mat.isSimilarity());
359     REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
360 
361     // skew with same size at a pivot point
362     mat.setSkew(15, 15, 2, 2);
363     REPORTER_ASSERT(reporter, !mat.isSimilarity());
364     REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
365 
366     // skew with different size at a pivot point
367     mat.setSkew(15, 20, 2, 2);
368     REPORTER_ASSERT(reporter, !mat.isSimilarity());
369     REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
370 
371     // perspective x
372     mat.reset().setPerspX(SK_Scalar1 / 2);
373     REPORTER_ASSERT(reporter, !mat.isSimilarity());
374     REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
375 
376     // perspective y
377     mat.reset().setPerspY(SK_Scalar1 / 2);
378     REPORTER_ASSERT(reporter, !mat.isSimilarity());
379     REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
380 
381     // rotate
382     for (int angle = 0; angle < 360; ++angle) {
383         mat.setRotate(SkIntToScalar(angle));
384         REPORTER_ASSERT(reporter, mat.isSimilarity());
385         REPORTER_ASSERT(reporter, mat.preservesRightAngles());
386     }
387 
388     // see if there are any accumulated precision issues
389     mat.reset();
390     for (int i = 1; i < 360; i++) {
391         mat.postRotate(1);
392     }
393     REPORTER_ASSERT(reporter, mat.isSimilarity());
394     REPORTER_ASSERT(reporter, mat.preservesRightAngles());
395 
396     // rotate + translate
397     mat.setRotate(30).postTranslate(10, 20);
398     REPORTER_ASSERT(reporter, mat.isSimilarity());
399     REPORTER_ASSERT(reporter, mat.preservesRightAngles());
400 
401     // rotate + uniform scale
402     mat.setRotate(30).postScale(2, 2);
403     REPORTER_ASSERT(reporter, mat.isSimilarity());
404     REPORTER_ASSERT(reporter, mat.preservesRightAngles());
405 
406     // rotate + non-uniform scale
407     mat.setRotate(30).postScale(3, 2);
408     REPORTER_ASSERT(reporter, !mat.isSimilarity());
409     REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
410 
411     // non-uniform scale + rotate
412     mat.setScale(3, 2).postRotate(30);
413     REPORTER_ASSERT(reporter, !mat.isSimilarity());
414     REPORTER_ASSERT(reporter, mat.preservesRightAngles());
415 
416     // all zero
417     mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, 0);
418     REPORTER_ASSERT(reporter, !mat.isSimilarity());
419     REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
420 
421     // all zero except perspective
422     mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, 1);
423     REPORTER_ASSERT(reporter, !mat.isSimilarity());
424     REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
425 
426     // scales zero, only skews (rotation)
427     mat.setAll(0, 1, 0,
428                -1, 0, 0,
429                0, 0, 1);
430     REPORTER_ASSERT(reporter, mat.isSimilarity());
431     REPORTER_ASSERT(reporter, mat.preservesRightAngles());
432 
433     // scales zero, only skews (reflection)
434     mat.setAll(0, 1, 0,
435                1, 0, 0,
436                0, 0, 1);
437     REPORTER_ASSERT(reporter, mat.isSimilarity());
438     REPORTER_ASSERT(reporter, mat.preservesRightAngles());
439 }
440 
441 // For test_matrix_decomposition, below.
scalar_nearly_equal_relative(SkScalar a,SkScalar b,SkScalar tolerance=SK_ScalarNearlyZero)442 static bool scalar_nearly_equal_relative(SkScalar a, SkScalar b,
443                                          SkScalar tolerance = SK_ScalarNearlyZero) {
444     // from Bruce Dawson
445     // absolute check
446     SkScalar diff = SkScalarAbs(a - b);
447     if (diff < tolerance) {
448         return true;
449     }
450 
451     // relative check
452     a = SkScalarAbs(a);
453     b = SkScalarAbs(b);
454     SkScalar largest = (b > a) ? b : a;
455 
456     if (diff <= largest*tolerance) {
457         return true;
458     }
459 
460     return false;
461 }
462 
check_matrix_recomposition(const SkMatrix & mat,const SkPoint & rotation1,const SkPoint & scale,const SkPoint & rotation2)463 static bool check_matrix_recomposition(const SkMatrix& mat,
464                                        const SkPoint& rotation1,
465                                        const SkPoint& scale,
466                                        const SkPoint& rotation2) {
467     SkScalar c1 = rotation1.fX;
468     SkScalar s1 = rotation1.fY;
469     SkScalar scaleX = scale.fX;
470     SkScalar scaleY = scale.fY;
471     SkScalar c2 = rotation2.fX;
472     SkScalar s2 = rotation2.fY;
473 
474     // We do a relative check here because large scale factors cause problems with an absolute check
475     bool result = scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX],
476                                                scaleX*c1*c2 - scaleY*s1*s2) &&
477                   scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX],
478                                                -scaleX*s1*c2 - scaleY*c1*s2) &&
479                   scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY],
480                                                scaleX*c1*s2 + scaleY*s1*c2) &&
481                   scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY],
482                                                -scaleX*s1*s2 + scaleY*c1*c2);
483     return result;
484 }
485 
test_matrix_decomposition(skiatest::Reporter * reporter)486 static void test_matrix_decomposition(skiatest::Reporter* reporter) {
487     SkMatrix mat;
488     SkPoint rotation1, scale, rotation2;
489 
490     const float kRotation0 = 15.5f;
491     const float kRotation1 = -50.f;
492     const float kScale0 = 5000.f;
493     const float kScale1 = 0.001f;
494 
495     // identity
496     mat.reset();
497     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
498     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
499     // make sure it doesn't crash if we pass in NULLs
500     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, nullptr, nullptr, nullptr));
501 
502     // rotation only
503     mat.setRotate(kRotation0);
504     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
505     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
506 
507     // uniform scale only
508     mat.setScale(kScale0, kScale0);
509     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
510     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
511 
512     // anisotropic scale only
513     mat.setScale(kScale1, kScale0);
514     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
515     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
516 
517     // rotation then uniform scale
518     mat.setRotate(kRotation1).postScale(kScale0, kScale0);
519     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
520     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
521 
522     // uniform scale then rotation
523     mat.setScale(kScale0, kScale0).postRotate(kRotation1);
524     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
525     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
526 
527     // rotation then uniform scale+reflection
528     mat.setRotate(kRotation0).postScale(kScale1, -kScale1);
529     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
530     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
531 
532     // uniform scale+reflection, then rotate
533     mat.setScale(kScale0, -kScale0).postRotate(kRotation1);
534     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
535     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
536 
537     // rotation then anisotropic scale
538     mat.setRotate(kRotation1).postScale(kScale1, kScale0);
539     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
540     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
541 
542     // rotation then anisotropic scale
543     mat.setRotate(90).postScale(kScale1, kScale0);
544     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
545     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
546 
547     // anisotropic scale then rotation
548     mat.setScale(kScale1, kScale0).postRotate(kRotation0);
549     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
550     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
551 
552     // anisotropic scale then rotation
553     mat.setScale(kScale1, kScale0).postRotate(90);
554     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
555     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
556 
557     // rotation, uniform scale, then different rotation
558     mat.setRotate(kRotation1).postScale(kScale0, kScale0).postRotate(kRotation0);
559     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
560     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
561 
562     // rotation, anisotropic scale, then different rotation
563     mat.setRotate(kRotation0).postScale(kScale1, kScale0).postRotate(kRotation1);
564     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
565     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
566 
567     // rotation, anisotropic scale + reflection, then different rotation
568     mat.setRotate(kRotation0).postScale(-kScale1, kScale0).postRotate(kRotation1);
569     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
570     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
571 
572     // try some random matrices
573     SkRandom rand;
574     for (int m = 0; m < 1000; ++m) {
575         SkScalar rot0 = rand.nextRangeF(-180, 180);
576         SkScalar sx = rand.nextRangeF(-3000.f, 3000.f);
577         SkScalar sy = rand.nextRangeF(-3000.f, 3000.f);
578         SkScalar rot1 = rand.nextRangeF(-180, 180);
579         mat.setRotate(rot0).postScale(sx, sy).postRotate(rot1);
580 
581         if (SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)) {
582             REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
583         } else {
584             // if the matrix is degenerate, the basis vectors should be near-parallel or near-zero
585             SkScalar perpdot = mat[SkMatrix::kMScaleX]*mat[SkMatrix::kMScaleY] -
586                                mat[SkMatrix::kMSkewX]*mat[SkMatrix::kMSkewY];
587             REPORTER_ASSERT(reporter, SkScalarNearlyZero(perpdot));
588         }
589     }
590 
591     // translation shouldn't affect this
592     mat.postTranslate(-1000.f, 1000.f);
593     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
594     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
595 
596     // perspective shouldn't affect this
597     mat[SkMatrix::kMPersp0] = 12.f;
598     mat[SkMatrix::kMPersp1] = 4.f;
599     mat[SkMatrix::kMPersp2] = 1872.f;
600     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
601     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
602 
603     // degenerate matrices
604     // mostly zero entries
605     mat.reset();
606     mat[SkMatrix::kMScaleX] = 0.f;
607     REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
608     mat.reset();
609     mat[SkMatrix::kMScaleY] = 0.f;
610     REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
611     mat.reset();
612     // linearly dependent entries
613     mat[SkMatrix::kMScaleX] = 1.f;
614     mat[SkMatrix::kMSkewX] = 2.f;
615     mat[SkMatrix::kMSkewY] = 4.f;
616     mat[SkMatrix::kMScaleY] = 8.f;
617     REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
618 }
619 
620 // For test_matrix_homogeneous, below.
point3_array_nearly_equal_relative(const SkPoint3 a[],const SkPoint3 b[],int count)621 static bool point3_array_nearly_equal_relative(const SkPoint3 a[], const SkPoint3 b[], int count) {
622     for (int i = 0; i < count; ++i) {
623         if (!scalar_nearly_equal_relative(a[i].fX, b[i].fX)) {
624             return false;
625         }
626         if (!scalar_nearly_equal_relative(a[i].fY, b[i].fY)) {
627             return false;
628         }
629         if (!scalar_nearly_equal_relative(a[i].fZ, b[i].fZ)) {
630             return false;
631         }
632     }
633     return true;
634 }
635 
636 // For test_matrix_homogeneous, below.
637 // Maps a single triple in src using m and compares results to those in dst
naive_homogeneous_mapping(const SkMatrix & m,const SkPoint3 & src,const SkPoint3 & dst)638 static bool naive_homogeneous_mapping(const SkMatrix& m, const SkPoint3& src,
639                                       const SkPoint3& dst) {
640     SkPoint3 res;
641     SkScalar ms[9] = {m[0], m[1], m[2],
642                       m[3], m[4], m[5],
643                       m[6], m[7], m[8]};
644     res.fX = src.fX * ms[0] + src.fY * ms[1] + src.fZ * ms[2];
645     res.fY = src.fX * ms[3] + src.fY * ms[4] + src.fZ * ms[5];
646     res.fZ = src.fX * ms[6] + src.fY * ms[7] + src.fZ * ms[8];
647     return point3_array_nearly_equal_relative(&res, &dst, 1);
648 }
649 
test_matrix_homogeneous(skiatest::Reporter * reporter)650 static void test_matrix_homogeneous(skiatest::Reporter* reporter) {
651     SkMatrix mat;
652 
653     const float kRotation0 = 15.5f;
654     const float kRotation1 = -50.f;
655     const float kScale0 = 5000.f;
656 
657 #if defined(SK_BUILD_FOR_GOOGLE3)
658     // Stack frame size is limited in SK_BUILD_FOR_GOOGLE3.
659     const int kTripleCount = 100;
660     const int kMatrixCount = 100;
661 #else
662     const int kTripleCount = 1000;
663     const int kMatrixCount = 1000;
664 #endif
665     SkRandom rand;
666 
667     SkPoint3 randTriples[kTripleCount];
668     for (int i = 0; i < kTripleCount; ++i) {
669         randTriples[i].fX = rand.nextRangeF(-3000.f, 3000.f);
670         randTriples[i].fY = rand.nextRangeF(-3000.f, 3000.f);
671         randTriples[i].fZ = rand.nextRangeF(-3000.f, 3000.f);
672     }
673 
674     SkMatrix mats[kMatrixCount];
675     for (int i = 0; i < kMatrixCount; ++i) {
676         for (int j = 0; j < 9; ++j) {
677             mats[i].set(j, rand.nextRangeF(-3000.f, 3000.f));
678         }
679     }
680 
681     // identity
682     {
683     mat.reset();
684     SkPoint3 dst[kTripleCount];
685     mat.mapHomogeneousPoints(dst, randTriples, kTripleCount);
686     REPORTER_ASSERT(reporter, point3_array_nearly_equal_relative(randTriples, dst, kTripleCount));
687     }
688 
689     const SkPoint3 zeros = {0.f, 0.f, 0.f};
690     // zero matrix
691     {
692     mat.setAll(0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f);
693     SkPoint3 dst[kTripleCount];
694     mat.mapHomogeneousPoints(dst, randTriples, kTripleCount);
695     for (int i = 0; i < kTripleCount; ++i) {
696         REPORTER_ASSERT(reporter, point3_array_nearly_equal_relative(&dst[i], &zeros, 1));
697     }
698     }
699 
700     // zero point
701     {
702     for (int i = 0; i < kMatrixCount; ++i) {
703         SkPoint3 dst;
704         mats[i].mapHomogeneousPoints(&dst, &zeros, 1);
705         REPORTER_ASSERT(reporter, point3_array_nearly_equal_relative(&dst, &zeros, 1));
706     }
707     }
708 
709     // doesn't crash with null dst, src, count == 0
710     {
711     mats[0].mapHomogeneousPoints(nullptr, (const SkPoint3*)nullptr, 0);
712     }
713 
714     // uniform scale of point
715     {
716     mat.setScale(kScale0, kScale0);
717     SkPoint3 dst;
718     SkPoint3 src = {randTriples[0].fX, randTriples[0].fY, 1.f};
719     SkPoint pnt;
720     pnt.set(src.fX, src.fY);
721     mat.mapHomogeneousPoints(&dst, &src, 1);
722     mat.mapPoints(&pnt, &pnt, 1);
723     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fX, pnt.fX));
724     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fY, pnt.fY));
725     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fZ, 1));
726     }
727 
728     // rotation of point
729     {
730     mat.setRotate(kRotation0);
731     SkPoint3 dst;
732     SkPoint3 src = {randTriples[0].fX, randTriples[0].fY, 1.f};
733     SkPoint pnt;
734     pnt.set(src.fX, src.fY);
735     mat.mapHomogeneousPoints(&dst, &src, 1);
736     mat.mapPoints(&pnt, &pnt, 1);
737     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fX, pnt.fX));
738     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fY, pnt.fY));
739     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fZ, 1));
740     }
741 
742     // rotation, scale, rotation of point
743     {
744     mat.setRotate(kRotation1);
745     mat.postScale(kScale0, kScale0);
746     mat.postRotate(kRotation0);
747     SkPoint3 dst;
748     SkPoint3 src = {randTriples[0].fX, randTriples[0].fY, 1.f};
749     SkPoint pnt;
750     pnt.set(src.fX, src.fY);
751     mat.mapHomogeneousPoints(&dst, &src, 1);
752     mat.mapPoints(&pnt, &pnt, 1);
753     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fX, pnt.fX));
754     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fY, pnt.fY));
755     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fZ, 1));
756     }
757 
758     // compare with naive approach
759     {
760     for (int i = 0; i < kMatrixCount; ++i) {
761         for (int j = 0; j < kTripleCount; ++j) {
762             SkPoint3 dst;
763             mats[i].mapHomogeneousPoints(&dst, &randTriples[j], 1);
764             REPORTER_ASSERT(reporter, naive_homogeneous_mapping(mats[i], randTriples[j], dst));
765         }
766     }
767     }
768 
769 }
770 
check_decompScale(const SkMatrix & original)771 static bool check_decompScale(const SkMatrix& original) {
772     SkSize scale;
773     SkMatrix remaining;
774 
775     if (!original.decomposeScale(&scale, &remaining)) {
776         return false;
777     }
778     if (scale.width() <= 0 || scale.height() <= 0) {
779         return false;
780     }
781 
782     // First ensure that the decomposition reconstitutes back to the original
783     {
784         SkMatrix reconstituted = remaining;
785 
786         reconstituted.preScale(scale.width(), scale.height());
787         if (!nearly_equal(original, reconstituted)) {
788             return false;
789         }
790     }
791 
792     // Then push some points through both paths and make sure they are the same.
793     static const int kNumPoints = 5;
794     const SkPoint testPts[kNumPoints] = {
795         {  0.0f,  0.0f },
796         {  1.0f,  1.0f },
797         {  1.0f,  0.5f },
798         { -1.0f, -0.5f },
799         { -1.0f,  2.0f }
800     };
801 
802     SkPoint v1[kNumPoints];
803     original.mapPoints(v1, testPts, kNumPoints);
804 
805     SkPoint v2[kNumPoints];
806     SkMatrix scaleMat = SkMatrix::Scale(scale.width(), scale.height());
807 
808     // Note, we intend the decomposition to be applied in the order scale and then remainder but,
809     // due to skbug.com/7211, the order is reversed!
810     scaleMat.mapPoints(v2, testPts, kNumPoints);
811     remaining.mapPoints(v2, kNumPoints);
812 
813     for (int i = 0; i < kNumPoints; ++i) {
814         if (!SkPointPriv::EqualsWithinTolerance(v1[i], v2[i], 0.00001f)) {
815             return false;
816         }
817     }
818 
819     return true;
820 }
821 
test_decompScale(skiatest::Reporter * reporter)822 static void test_decompScale(skiatest::Reporter* reporter) {
823     SkMatrix m;
824 
825     m.reset();
826     REPORTER_ASSERT(reporter, check_decompScale(m));
827     m.setScale(2, 3);
828     REPORTER_ASSERT(reporter, check_decompScale(m));
829     m.setRotate(35, 0, 0);
830     REPORTER_ASSERT(reporter, check_decompScale(m));
831 
832     m.setScale(1, 0);
833     REPORTER_ASSERT(reporter, !check_decompScale(m));
834 
835     m.setRotate(35, 0, 0).preScale(2, 3);
836     REPORTER_ASSERT(reporter, check_decompScale(m));
837 
838     m.setRotate(35, 0, 0).postScale(2, 3);
839     REPORTER_ASSERT(reporter, check_decompScale(m));
840 }
841 
DEF_TEST(Matrix,reporter)842 DEF_TEST(Matrix, reporter) {
843     SkMatrix    mat, inverse, iden1, iden2;
844 
845     mat.reset();
846     mat.setTranslate(1, 1);
847     REPORTER_ASSERT(reporter, mat.invert(&inverse));
848     iden1.setConcat(mat, inverse);
849     REPORTER_ASSERT(reporter, is_identity(iden1));
850 
851     mat.setScale(2, 4);
852     REPORTER_ASSERT(reporter, mat.invert(&inverse));
853     iden1.setConcat(mat, inverse);
854     REPORTER_ASSERT(reporter, is_identity(iden1));
855     test_flatten(reporter, mat);
856 
857     mat.setScale(SK_Scalar1/2, 2);
858     REPORTER_ASSERT(reporter, mat.invert(&inverse));
859     iden1.setConcat(mat, inverse);
860     REPORTER_ASSERT(reporter, is_identity(iden1));
861     test_flatten(reporter, mat);
862 
863     mat.setScale(3, 5, 20, 0).postRotate(25);
864     REPORTER_ASSERT(reporter, mat.invert(nullptr));
865     REPORTER_ASSERT(reporter, mat.invert(&inverse));
866     iden1.setConcat(mat, inverse);
867     REPORTER_ASSERT(reporter, is_identity(iden1));
868     iden2.setConcat(inverse, mat);
869     REPORTER_ASSERT(reporter, is_identity(iden2));
870     test_flatten(reporter, mat);
871     test_flatten(reporter, iden2);
872 
873     mat.setScale(0, 1);
874     REPORTER_ASSERT(reporter, !mat.invert(nullptr));
875     REPORTER_ASSERT(reporter, !mat.invert(&inverse));
876     mat.setScale(1, 0);
877     REPORTER_ASSERT(reporter, !mat.invert(nullptr));
878     REPORTER_ASSERT(reporter, !mat.invert(&inverse));
879 
880     // Inverting this matrix results in a non-finite matrix
881     mat.setAll(0.0f, 1.0f, 2.0f,
882                0.0f, 1.0f, -3.40277175e+38f,
883                1.00003040f, 1.0f, 0.0f);
884     REPORTER_ASSERT(reporter, !mat.invert(nullptr));
885     REPORTER_ASSERT(reporter, !mat.invert(&inverse));
886 
887     // rectStaysRect test
888     {
889         static const struct {
890             SkScalar    m00, m01, m10, m11;
891             bool        mStaysRect;
892         }
893         gRectStaysRectSamples[] = {
894             { 0, 0, 0, 0, false },
895             { 0, 0, 0, 1, false },
896             { 0, 0, 1, 0, false },
897             { 0, 0, 1, 1, false },
898             { 0, 1, 0, 0, false },
899             { 0, 1, 0, 1, false },
900             { 0, 1, 1, 0, true },
901             { 0, 1, 1, 1, false },
902             { 1, 0, 0, 0, false },
903             { 1, 0, 0, 1, true },
904             { 1, 0, 1, 0, false },
905             { 1, 0, 1, 1, false },
906             { 1, 1, 0, 0, false },
907             { 1, 1, 0, 1, false },
908             { 1, 1, 1, 0, false },
909             { 1, 1, 1, 1, false }
910         };
911 
912         for (size_t i = 0; i < SK_ARRAY_COUNT(gRectStaysRectSamples); i++) {
913             SkMatrix    m;
914 
915             m.reset();
916             m.set(SkMatrix::kMScaleX, gRectStaysRectSamples[i].m00);
917             m.set(SkMatrix::kMSkewX,  gRectStaysRectSamples[i].m01);
918             m.set(SkMatrix::kMSkewY,  gRectStaysRectSamples[i].m10);
919             m.set(SkMatrix::kMScaleY, gRectStaysRectSamples[i].m11);
920             REPORTER_ASSERT(reporter,
921                     m.rectStaysRect() == gRectStaysRectSamples[i].mStaysRect);
922         }
923     }
924 
925     mat.reset();
926     mat.set(SkMatrix::kMScaleX, 1)
927        .set(SkMatrix::kMSkewX,  2)
928        .set(SkMatrix::kMTransX, 3)
929        .set(SkMatrix::kMSkewY,  4)
930        .set(SkMatrix::kMScaleY, 5)
931        .set(SkMatrix::kMTransY, 6);
932     SkScalar affine[6];
933     REPORTER_ASSERT(reporter, mat.asAffine(affine));
934 
935     #define affineEqual(e) affine[SkMatrix::kA##e] == mat.get(SkMatrix::kM##e)
936     REPORTER_ASSERT(reporter, affineEqual(ScaleX));
937     REPORTER_ASSERT(reporter, affineEqual(SkewY));
938     REPORTER_ASSERT(reporter, affineEqual(SkewX));
939     REPORTER_ASSERT(reporter, affineEqual(ScaleY));
940     REPORTER_ASSERT(reporter, affineEqual(TransX));
941     REPORTER_ASSERT(reporter, affineEqual(TransY));
942     #undef affineEqual
943 
944     mat.set(SkMatrix::kMPersp1, SK_Scalar1 / 2);
945     REPORTER_ASSERT(reporter, !mat.asAffine(affine));
946 
947     SkMatrix mat2;
948     mat2.reset();
949     mat.reset();
950     SkScalar zero = 0;
951     mat.set(SkMatrix::kMSkewX, -zero);
952     REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2));
953 
954     mat2.reset();
955     mat.reset();
956     mat.set(SkMatrix::kMSkewX, SK_ScalarNaN);
957     mat2.set(SkMatrix::kMSkewX, SK_ScalarNaN);
958     REPORTER_ASSERT(reporter, !are_equal(reporter, mat, mat2));
959 
960     test_matrix_min_max_scale(reporter);
961     test_matrix_preserve_shape(reporter);
962     test_matrix_recttorect(reporter);
963     test_matrix_decomposition(reporter);
964     test_matrix_homogeneous(reporter);
965     test_set9(reporter);
966 
967     test_decompScale(reporter);
968 
969     mat.setScaleTranslate(2, 3, 1, 4);
970     mat2.setScale(2, 3).postTranslate(1, 4);
971     REPORTER_ASSERT(reporter, mat == mat2);
972 }
973 
DEF_TEST(Matrix_Concat,r)974 DEF_TEST(Matrix_Concat, r) {
975     SkMatrix a;
976     a.setTranslate(10, 20);
977 
978     SkMatrix b;
979     b.setScale(3, 5);
980 
981     SkMatrix expected;
982     expected.setConcat(a,b);
983 
984     REPORTER_ASSERT(r, expected == SkMatrix::Concat(a, b));
985 }
986 
987 // Test that all variants of maprect are correct.
DEF_TEST(Matrix_maprects,r)988 DEF_TEST(Matrix_maprects, r) {
989     const SkScalar scale = 1000;
990 
991     SkMatrix mat;
992     mat.setScale(2, 3).postTranslate(1, 4);
993 
994     SkRandom rand;
995     for (int i = 0; i < 10000; ++i) {
996         SkRect src = SkRect::MakeLTRB(rand.nextSScalar1() * scale,
997                                       rand.nextSScalar1() * scale,
998                                       rand.nextSScalar1() * scale,
999                                       rand.nextSScalar1() * scale);
1000         SkRect dst[4];
1001 
1002         mat.mapPoints((SkPoint*)&dst[0].fLeft, (SkPoint*)&src.fLeft, 2);
1003         dst[0].sort();
1004         mat.mapRect(&dst[1], src);
1005         mat.mapRectScaleTranslate(&dst[2], src);
1006         dst[3] = mat.mapRect(src);
1007 
1008         REPORTER_ASSERT(r, dst[0] == dst[1]);
1009         REPORTER_ASSERT(r, dst[0] == dst[2]);
1010         REPORTER_ASSERT(r, dst[0] == dst[3]);
1011     }
1012 
1013     // We should report nonfinite-ness after a mapping
1014     {
1015         // We have special-cases in mapRect for different matrix types
1016         SkMatrix m0 = SkMatrix::Scale(1e20f, 1e20f);
1017         SkMatrix m1; m1.setRotate(30); m1.postScale(1e20f, 1e20f);
1018 
1019         for (const auto& m : { m0, m1 }) {
1020             SkRect rect = { 0, 0, 1e20f, 1e20f };
1021             REPORTER_ASSERT(r, rect.isFinite());
1022             rect = m.mapRect(rect);
1023             REPORTER_ASSERT(r, !rect.isFinite());
1024         }
1025     }
1026 }
1027 
DEF_TEST(Matrix_mapRect_skbug12335,r)1028 DEF_TEST(Matrix_mapRect_skbug12335, r) {
1029     // Stripped down test case from skbug.com/12335. Essentially, the corners of this rect would
1030     // map to homogoneous coords with very small w's (below the old value of kW0PlaneDistance) and
1031     // so they would be clipped "behind" the plane, resulting in an empty mapped rect. Coordinates
1032     // with positive that wouldn't overflow when divided by w should still be included in the mapped
1033     // rectangle.
1034     SkRect rect = SkRect::MakeLTRB(0, 0, 319, 620);
1035     SkMatrix m = SkMatrix::MakeAll( 0.000152695269f, 0.00000000f,     -6.53848401e-05f,
1036                                    -1.75697533e-05f, 0.000157153074f, -1.10847975e-06f,
1037                                    -6.00415362e-08f, 0.00000000f,      0.000169880834f);
1038     SkRect out = m.mapRect(rect);
1039     REPORTER_ASSERT(r, !out.isEmpty());
1040 }
1041 
DEF_TEST(Matrix_Ctor,r)1042 DEF_TEST(Matrix_Ctor, r) {
1043     REPORTER_ASSERT(r, SkMatrix{} == SkMatrix::I());
1044 }
1045