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