Home | History | Annotate | Download | only in tests
      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 
     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 
     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 
     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 == aValI);
     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 
     68 static bool is_identity(const SkMatrix& m) {
     69     SkMatrix identity;
     70     identity.reset();
     71     return nearly_equal(m, identity);
     72 }
     73 
     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 
     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 
    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 
    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 
    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     REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMinScale());
    217     REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMaxScale());
    218     success = big.getMinMaxScales(scales);
    219     REPORTER_ASSERT(reporter, !success);
    220 
    221     SkMatrix perspY;
    222     perspY.reset();
    223     perspY.setPerspY(-SK_Scalar1 / 500);
    224     REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMinScale());
    225     REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMaxScale());
    226     scales[0] = -5;
    227     scales[1] = -5;
    228     success = perspY.getMinMaxScales(scales);
    229     REPORTER_ASSERT(reporter, !success && -5 * SK_Scalar1 == scales[0] && -5 * SK_Scalar1  == scales[1]);
    230 
    231     SkMatrix baseMats[] = {scale, rot90Scale, rotate,
    232                            translate, perspX, perspY};
    233     SkMatrix mats[2*SK_ARRAY_COUNT(baseMats)];
    234     for (size_t i = 0; i < SK_ARRAY_COUNT(baseMats); ++i) {
    235         mats[i] = baseMats[i];
    236         bool invertable = mats[i].invert(&mats[i + SK_ARRAY_COUNT(baseMats)]);
    237         REPORTER_ASSERT(reporter, invertable);
    238     }
    239     SkRandom rand;
    240     for (int m = 0; m < 1000; ++m) {
    241         SkMatrix mat;
    242         mat.reset();
    243         for (int i = 0; i < 4; ++i) {
    244             int x = rand.nextU() % SK_ARRAY_COUNT(mats);
    245             mat.postConcat(mats[x]);
    246         }
    247 
    248         SkScalar minScale = mat.getMinScale();
    249         SkScalar maxScale = mat.getMaxScale();
    250         REPORTER_ASSERT(reporter, (minScale < 0) == (maxScale < 0));
    251         REPORTER_ASSERT(reporter, (maxScale < 0) == mat.hasPerspective());
    252 
    253         SkScalar scales[2];
    254         bool success = mat.getMinMaxScales(scales);
    255         REPORTER_ASSERT(reporter, success == !mat.hasPerspective());
    256         REPORTER_ASSERT(reporter, !success || (scales[0] == minScale && scales[1] == maxScale));
    257 
    258         if (mat.hasPerspective()) {
    259             m -= 1; // try another non-persp matrix
    260             continue;
    261         }
    262 
    263         // test a bunch of vectors. All should be scaled by between minScale and maxScale
    264         // (modulo some error) and we should find a vector that is scaled by almost each.
    265         static const SkScalar gVectorScaleTol = (105 * SK_Scalar1) / 100;
    266         static const SkScalar gCloseScaleTol = (97 * SK_Scalar1) / 100;
    267         SkScalar max = 0, min = SK_ScalarMax;
    268         SkVector vectors[1000];
    269         for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
    270             vectors[i].fX = rand.nextSScalar1();
    271             vectors[i].fY = rand.nextSScalar1();
    272             if (!vectors[i].normalize()) {
    273                 i -= 1;
    274                 continue;
    275             }
    276         }
    277         mat.mapVectors(vectors, SK_ARRAY_COUNT(vectors));
    278         for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
    279             SkScalar d = vectors[i].length();
    280             REPORTER_ASSERT(reporter, d / maxScale < gVectorScaleTol);
    281             REPORTER_ASSERT(reporter, minScale / d < gVectorScaleTol);
    282             if (max < d) {
    283                 max = d;
    284             }
    285             if (min > d) {
    286                 min = d;
    287             }
    288         }
    289         REPORTER_ASSERT(reporter, max / maxScale >= gCloseScaleTol);
    290         REPORTER_ASSERT(reporter, minScale / min >= gCloseScaleTol);
    291     }
    292 }
    293 
    294 static void test_matrix_preserve_shape(skiatest::Reporter* reporter) {
    295     SkMatrix mat;
    296 
    297     // identity
    298     mat.setIdentity();
    299     REPORTER_ASSERT(reporter, mat.isSimilarity());
    300     REPORTER_ASSERT(reporter, mat.preservesRightAngles());
    301 
    302     // translation only
    303     mat.reset();
    304     mat.setTranslate(SkIntToScalar(100), SkIntToScalar(100));
    305     REPORTER_ASSERT(reporter, mat.isSimilarity());
    306     REPORTER_ASSERT(reporter, mat.preservesRightAngles());
    307 
    308     // scale with same size
    309     mat.reset();
    310     mat.setScale(SkIntToScalar(15), SkIntToScalar(15));
    311     REPORTER_ASSERT(reporter, mat.isSimilarity());
    312     REPORTER_ASSERT(reporter, mat.preservesRightAngles());
    313 
    314     // scale with one negative
    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 different size
    321     mat.reset();
    322     mat.setScale(SkIntToScalar(15), SkIntToScalar(20));
    323     REPORTER_ASSERT(reporter, !mat.isSimilarity());
    324     REPORTER_ASSERT(reporter, mat.preservesRightAngles());
    325 
    326     // scale with same size at a pivot point
    327     mat.reset();
    328     mat.setScale(SkIntToScalar(15), SkIntToScalar(15),
    329                  SkIntToScalar(2), SkIntToScalar(2));
    330     REPORTER_ASSERT(reporter, mat.isSimilarity());
    331     REPORTER_ASSERT(reporter, mat.preservesRightAngles());
    332 
    333     // scale with different size at a pivot point
    334     mat.reset();
    335     mat.setScale(SkIntToScalar(15), SkIntToScalar(20),
    336                  SkIntToScalar(2), SkIntToScalar(2));
    337     REPORTER_ASSERT(reporter, !mat.isSimilarity());
    338     REPORTER_ASSERT(reporter, mat.preservesRightAngles());
    339 
    340     // skew with same size
    341     mat.reset();
    342     mat.setSkew(SkIntToScalar(15), SkIntToScalar(15));
    343     REPORTER_ASSERT(reporter, !mat.isSimilarity());
    344     REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
    345 
    346     // skew with different size
    347     mat.reset();
    348     mat.setSkew(SkIntToScalar(15), SkIntToScalar(20));
    349     REPORTER_ASSERT(reporter, !mat.isSimilarity());
    350     REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
    351 
    352     // skew with same size at a pivot point
    353     mat.reset();
    354     mat.setSkew(SkIntToScalar(15), SkIntToScalar(15),
    355                 SkIntToScalar(2), SkIntToScalar(2));
    356     REPORTER_ASSERT(reporter, !mat.isSimilarity());
    357     REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
    358 
    359     // skew with different size at a pivot point
    360     mat.reset();
    361     mat.setSkew(SkIntToScalar(15), SkIntToScalar(20),
    362                 SkIntToScalar(2), SkIntToScalar(2));
    363     REPORTER_ASSERT(reporter, !mat.isSimilarity());
    364     REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
    365 
    366     // perspective x
    367     mat.reset();
    368     mat.setPerspX(SK_Scalar1 / 2);
    369     REPORTER_ASSERT(reporter, !mat.isSimilarity());
    370     REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
    371 
    372     // perspective y
    373     mat.reset();
    374     mat.setPerspY(SK_Scalar1 / 2);
    375     REPORTER_ASSERT(reporter, !mat.isSimilarity());
    376     REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
    377 
    378     // rotate
    379     for (int angle = 0; angle < 360; ++angle) {
    380         mat.reset();
    381         mat.setRotate(SkIntToScalar(angle));
    382         REPORTER_ASSERT(reporter, mat.isSimilarity());
    383         REPORTER_ASSERT(reporter, mat.preservesRightAngles());
    384     }
    385 
    386     // see if there are any accumulated precision issues
    387     mat.reset();
    388     for (int i = 1; i < 360; i++) {
    389         mat.postRotate(SkIntToScalar(1));
    390     }
    391     REPORTER_ASSERT(reporter, mat.isSimilarity());
    392     REPORTER_ASSERT(reporter, mat.preservesRightAngles());
    393 
    394     // rotate + translate
    395     mat.reset();
    396     mat.setRotate(SkIntToScalar(30));
    397     mat.postTranslate(SkIntToScalar(10), SkIntToScalar(20));
    398     REPORTER_ASSERT(reporter, mat.isSimilarity());
    399     REPORTER_ASSERT(reporter, mat.preservesRightAngles());
    400 
    401     // rotate + uniform scale
    402     mat.reset();
    403     mat.setRotate(SkIntToScalar(30));
    404     mat.postScale(SkIntToScalar(2), SkIntToScalar(2));
    405     REPORTER_ASSERT(reporter, mat.isSimilarity());
    406     REPORTER_ASSERT(reporter, mat.preservesRightAngles());
    407 
    408     // rotate + non-uniform scale
    409     mat.reset();
    410     mat.setRotate(SkIntToScalar(30));
    411     mat.postScale(SkIntToScalar(3), SkIntToScalar(2));
    412     REPORTER_ASSERT(reporter, !mat.isSimilarity());
    413     REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
    414 
    415     // non-uniform scale + rotate
    416     mat.reset();
    417     mat.setScale(SkIntToScalar(3), SkIntToScalar(2));
    418     mat.postRotate(SkIntToScalar(30));
    419     REPORTER_ASSERT(reporter, !mat.isSimilarity());
    420     REPORTER_ASSERT(reporter, mat.preservesRightAngles());
    421 
    422     // all zero
    423     mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, 0);
    424     REPORTER_ASSERT(reporter, !mat.isSimilarity());
    425     REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
    426 
    427     // all zero except perspective
    428     mat.reset();
    429     mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, SK_Scalar1);
    430     REPORTER_ASSERT(reporter, !mat.isSimilarity());
    431     REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
    432 
    433     // scales zero, only skews (rotation)
    434     mat.setAll(0, SK_Scalar1, 0,
    435                -SK_Scalar1, 0, 0,
    436                0, 0, SkMatrix::I()[8]);
    437     REPORTER_ASSERT(reporter, mat.isSimilarity());
    438     REPORTER_ASSERT(reporter, mat.preservesRightAngles());
    439 
    440     // scales zero, only skews (reflection)
    441     mat.setAll(0, SK_Scalar1, 0,
    442                SK_Scalar1, 0, 0,
    443                0, 0, SkMatrix::I()[8]);
    444     REPORTER_ASSERT(reporter, mat.isSimilarity());
    445     REPORTER_ASSERT(reporter, mat.preservesRightAngles());
    446 }
    447 
    448 // For test_matrix_decomposition, below.
    449 static bool scalar_nearly_equal_relative(SkScalar a, SkScalar b,
    450                                          SkScalar tolerance = SK_ScalarNearlyZero) {
    451     // from Bruce Dawson
    452     // absolute check
    453     SkScalar diff = SkScalarAbs(a - b);
    454     if (diff < tolerance) {
    455         return true;
    456     }
    457 
    458     // relative check
    459     a = SkScalarAbs(a);
    460     b = SkScalarAbs(b);
    461     SkScalar largest = (b > a) ? b : a;
    462 
    463     if (diff <= largest*tolerance) {
    464         return true;
    465     }
    466 
    467     return false;
    468 }
    469 
    470 static bool check_matrix_recomposition(const SkMatrix& mat,
    471                                        const SkPoint& rotation1,
    472                                        const SkPoint& scale,
    473                                        const SkPoint& rotation2) {
    474     SkScalar c1 = rotation1.fX;
    475     SkScalar s1 = rotation1.fY;
    476     SkScalar scaleX = scale.fX;
    477     SkScalar scaleY = scale.fY;
    478     SkScalar c2 = rotation2.fX;
    479     SkScalar s2 = rotation2.fY;
    480 
    481     // We do a relative check here because large scale factors cause problems with an absolute check
    482     bool result = scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX],
    483                                                scaleX*c1*c2 - scaleY*s1*s2) &&
    484                   scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX],
    485                                                -scaleX*s1*c2 - scaleY*c1*s2) &&
    486                   scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY],
    487                                                scaleX*c1*s2 + scaleY*s1*c2) &&
    488                   scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY],
    489                                                -scaleX*s1*s2 + scaleY*c1*c2);
    490     return result;
    491 }
    492 
    493 static void test_matrix_decomposition(skiatest::Reporter* reporter) {
    494     SkMatrix mat;
    495     SkPoint rotation1, scale, rotation2;
    496 
    497     const float kRotation0 = 15.5f;
    498     const float kRotation1 = -50.f;
    499     const float kScale0 = 5000.f;
    500     const float kScale1 = 0.001f;
    501 
    502     // identity
    503     mat.reset();
    504     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    505     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    506     // make sure it doesn't crash if we pass in NULLs
    507     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, nullptr, nullptr, nullptr));
    508 
    509     // rotation only
    510     mat.setRotate(kRotation0);
    511     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    512     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    513 
    514     // uniform scale only
    515     mat.setScale(kScale0, kScale0);
    516     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    517     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    518 
    519     // anisotropic scale only
    520     mat.setScale(kScale1, kScale0);
    521     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    522     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    523 
    524     // rotation then uniform scale
    525     mat.setRotate(kRotation1);
    526     mat.postScale(kScale0, kScale0);
    527     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    528     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    529 
    530     // uniform scale then rotation
    531     mat.setScale(kScale0, kScale0);
    532     mat.postRotate(kRotation1);
    533     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    534     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    535 
    536     // rotation then uniform scale+reflection
    537     mat.setRotate(kRotation0);
    538     mat.postScale(kScale1, -kScale1);
    539     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    540     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    541 
    542     // uniform scale+reflection, then rotate
    543     mat.setScale(kScale0, -kScale0);
    544     mat.postRotate(kRotation1);
    545     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    546     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    547 
    548     // rotation then anisotropic scale
    549     mat.setRotate(kRotation1);
    550     mat.postScale(kScale1, kScale0);
    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(90);
    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     // anisotropic scale then rotation
    561     mat.setScale(kScale1, kScale0);
    562     mat.postRotate(kRotation0);
    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(90);
    569     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    570     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    571 
    572     // rotation, uniform scale, then different rotation
    573     mat.setRotate(kRotation1);
    574     mat.postScale(kScale0, kScale0);
    575     mat.postRotate(kRotation0);
    576     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    577     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    578 
    579     // rotation, anisotropic scale, then different rotation
    580     mat.setRotate(kRotation0);
    581     mat.postScale(kScale1, kScale0);
    582     mat.postRotate(kRotation1);
    583     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    584     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    585 
    586     // rotation, anisotropic scale + reflection, then different rotation
    587     mat.setRotate(kRotation0);
    588     mat.postScale(-kScale1, kScale0);
    589     mat.postRotate(kRotation1);
    590     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    591     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    592 
    593     // try some random matrices
    594     SkRandom rand;
    595     for (int m = 0; m < 1000; ++m) {
    596         SkScalar rot0 = rand.nextRangeF(-180, 180);
    597         SkScalar sx = rand.nextRangeF(-3000.f, 3000.f);
    598         SkScalar sy = rand.nextRangeF(-3000.f, 3000.f);
    599         SkScalar rot1 = rand.nextRangeF(-180, 180);
    600         mat.setRotate(rot0);
    601         mat.postScale(sx, sy);
    602         mat.postRotate(rot1);
    603 
    604         if (SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)) {
    605             REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    606         } else {
    607             // if the matrix is degenerate, the basis vectors should be near-parallel or near-zero
    608             SkScalar perpdot = mat[SkMatrix::kMScaleX]*mat[SkMatrix::kMScaleY] -
    609                                mat[SkMatrix::kMSkewX]*mat[SkMatrix::kMSkewY];
    610             REPORTER_ASSERT(reporter, SkScalarNearlyZero(perpdot));
    611         }
    612     }
    613 
    614     // translation shouldn't affect this
    615     mat.postTranslate(-1000.f, 1000.f);
    616     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    617     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    618 
    619     // perspective shouldn't affect this
    620     mat[SkMatrix::kMPersp0] = 12.f;
    621     mat[SkMatrix::kMPersp1] = 4.f;
    622     mat[SkMatrix::kMPersp2] = 1872.f;
    623     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    624     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    625 
    626     // degenerate matrices
    627     // mostly zero entries
    628     mat.reset();
    629     mat[SkMatrix::kMScaleX] = 0.f;
    630     REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    631     mat.reset();
    632     mat[SkMatrix::kMScaleY] = 0.f;
    633     REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    634     mat.reset();
    635     // linearly dependent entries
    636     mat[SkMatrix::kMScaleX] = 1.f;
    637     mat[SkMatrix::kMSkewX] = 2.f;
    638     mat[SkMatrix::kMSkewY] = 4.f;
    639     mat[SkMatrix::kMScaleY] = 8.f;
    640     REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    641 }
    642 
    643 // For test_matrix_homogeneous, below.
    644 static bool scalar_array_nearly_equal_relative(const SkScalar a[], const SkScalar b[], int count) {
    645     for (int i = 0; i < count; ++i) {
    646         if (!scalar_nearly_equal_relative(a[i], b[i])) {
    647             return false;
    648         }
    649     }
    650     return true;
    651 }
    652 
    653 // For test_matrix_homogeneous, below.
    654 // Maps a single triple in src using m and compares results to those in dst
    655 static bool naive_homogeneous_mapping(const SkMatrix& m, const SkScalar src[3],
    656                                       const SkScalar dst[3]) {
    657     SkScalar res[3];
    658     SkScalar ms[9] = {m[0], m[1], m[2],
    659                       m[3], m[4], m[5],
    660                       m[6], m[7], m[8]};
    661     res[0] = src[0] * ms[0] + src[1] * ms[1] + src[2] * ms[2];
    662     res[1] = src[0] * ms[3] + src[1] * ms[4] + src[2] * ms[5];
    663     res[2] = src[0] * ms[6] + src[1] * ms[7] + src[2] * ms[8];
    664     return scalar_array_nearly_equal_relative(res, dst, 3);
    665 }
    666 
    667 static void test_matrix_homogeneous(skiatest::Reporter* reporter) {
    668     SkMatrix mat;
    669 
    670     const float kRotation0 = 15.5f;
    671     const float kRotation1 = -50.f;
    672     const float kScale0 = 5000.f;
    673 
    674 #if defined(GOOGLE3)
    675     // Stack frame size is limited in GOOGLE3.
    676     const int kTripleCount = 100;
    677     const int kMatrixCount = 100;
    678 #else
    679     const int kTripleCount = 1000;
    680     const int kMatrixCount = 1000;
    681 #endif
    682     SkRandom rand;
    683 
    684     SkScalar randTriples[3*kTripleCount];
    685     for (int i = 0; i < 3*kTripleCount; ++i) {
    686         randTriples[i] = rand.nextRangeF(-3000.f, 3000.f);
    687     }
    688 
    689     SkMatrix mats[kMatrixCount];
    690     for (int i = 0; i < kMatrixCount; ++i) {
    691         for (int j = 0; j < 9; ++j) {
    692             mats[i].set(j, rand.nextRangeF(-3000.f, 3000.f));
    693         }
    694     }
    695 
    696     // identity
    697     {
    698     mat.reset();
    699     SkScalar dst[3*kTripleCount];
    700     mat.mapHomogeneousPoints(dst, randTriples, kTripleCount);
    701     REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(randTriples, dst, kTripleCount*3));
    702     }
    703 
    704     // zero matrix
    705     {
    706     mat.setAll(0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f);
    707     SkScalar dst[3*kTripleCount];
    708     mat.mapHomogeneousPoints(dst, randTriples, kTripleCount);
    709     SkScalar zeros[3] = {0.f, 0.f, 0.f};
    710     for (int i = 0; i < kTripleCount; ++i) {
    711         REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(&dst[i*3], zeros, 3));
    712     }
    713     }
    714 
    715     // zero point
    716     {
    717     SkScalar zeros[3] = {0.f, 0.f, 0.f};
    718     for (int i = 0; i < kMatrixCount; ++i) {
    719         SkScalar dst[3];
    720         mats[i].mapHomogeneousPoints(dst, zeros, 1);
    721         REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(dst, zeros, 3));
    722     }
    723     }
    724 
    725     // doesn't crash with null dst, src, count == 0
    726     {
    727     mats[0].mapHomogeneousPoints(nullptr, nullptr, 0);
    728     }
    729 
    730     // uniform scale of point
    731     {
    732     mat.setScale(kScale0, kScale0);
    733     SkScalar dst[3];
    734     SkScalar src[3] = {randTriples[0], randTriples[1], 1.f};
    735     SkPoint pnt;
    736     pnt.set(src[0], src[1]);
    737     mat.mapHomogeneousPoints(dst, src, 1);
    738     mat.mapPoints(&pnt, &pnt, 1);
    739     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX));
    740     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY));
    741     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1));
    742     }
    743 
    744     // rotation of point
    745     {
    746     mat.setRotate(kRotation0);
    747     SkScalar dst[3];
    748     SkScalar src[3] = {randTriples[0], randTriples[1], 1.f};
    749     SkPoint pnt;
    750     pnt.set(src[0], src[1]);
    751     mat.mapHomogeneousPoints(dst, src, 1);
    752     mat.mapPoints(&pnt, &pnt, 1);
    753     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX));
    754     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY));
    755     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1));
    756     }
    757 
    758     // rotation, scale, rotation of point
    759     {
    760     mat.setRotate(kRotation1);
    761     mat.postScale(kScale0, kScale0);
    762     mat.postRotate(kRotation0);
    763     SkScalar dst[3];
    764     SkScalar src[3] = {randTriples[0], randTriples[1], 1.f};
    765     SkPoint pnt;
    766     pnt.set(src[0], src[1]);
    767     mat.mapHomogeneousPoints(dst, src, 1);
    768     mat.mapPoints(&pnt, &pnt, 1);
    769     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX));
    770     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY));
    771     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1));
    772     }
    773 
    774     // compare with naive approach
    775     {
    776     for (int i = 0; i < kMatrixCount; ++i) {
    777         for (int j = 0; j < kTripleCount; ++j) {
    778             SkScalar dst[3];
    779             mats[i].mapHomogeneousPoints(dst, &randTriples[j*3], 1);
    780             REPORTER_ASSERT(reporter, naive_homogeneous_mapping(mats[i], &randTriples[j*3], dst));
    781         }
    782     }
    783     }
    784 
    785 }
    786 
    787 static bool check_decompScale(const SkMatrix& matrix) {
    788     SkSize scale;
    789     SkMatrix remaining;
    790 
    791     if (!matrix.decomposeScale(&scale, &remaining)) {
    792         return false;
    793     }
    794     if (scale.width() <= 0 || scale.height() <= 0) {
    795         return false;
    796     }
    797     remaining.preScale(scale.width(), scale.height());
    798     return nearly_equal(matrix, remaining);
    799 }
    800 
    801 static void test_decompScale(skiatest::Reporter* reporter) {
    802     SkMatrix m;
    803 
    804     m.reset();
    805     REPORTER_ASSERT(reporter, check_decompScale(m));
    806     m.setScale(2, 3);
    807     REPORTER_ASSERT(reporter, check_decompScale(m));
    808     m.setRotate(35, 0, 0);
    809     REPORTER_ASSERT(reporter, check_decompScale(m));
    810 
    811     m.setScale(1, 0);
    812     REPORTER_ASSERT(reporter, !check_decompScale(m));
    813 }
    814 
    815 DEF_TEST(Matrix, reporter) {
    816     SkMatrix    mat, inverse, iden1, iden2;
    817 
    818     mat.reset();
    819     mat.setTranslate(SK_Scalar1, SK_Scalar1);
    820     REPORTER_ASSERT(reporter, mat.invert(&inverse));
    821     iden1.setConcat(mat, inverse);
    822     REPORTER_ASSERT(reporter, is_identity(iden1));
    823 
    824     mat.setScale(SkIntToScalar(2), SkIntToScalar(4));
    825     REPORTER_ASSERT(reporter, mat.invert(&inverse));
    826     iden1.setConcat(mat, inverse);
    827     REPORTER_ASSERT(reporter, is_identity(iden1));
    828     test_flatten(reporter, mat);
    829 
    830     mat.setScale(SK_Scalar1/2, SkIntToScalar(2));
    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(SkIntToScalar(3), SkIntToScalar(5), SkIntToScalar(20), 0);
    837     mat.postRotate(SkIntToScalar(25));
    838     REPORTER_ASSERT(reporter, mat.invert(nullptr));
    839     REPORTER_ASSERT(reporter, mat.invert(&inverse));
    840     iden1.setConcat(mat, inverse);
    841     REPORTER_ASSERT(reporter, is_identity(iden1));
    842     iden2.setConcat(inverse, mat);
    843     REPORTER_ASSERT(reporter, is_identity(iden2));
    844     test_flatten(reporter, mat);
    845     test_flatten(reporter, iden2);
    846 
    847     mat.setScale(0, SK_Scalar1);
    848     REPORTER_ASSERT(reporter, !mat.invert(nullptr));
    849     REPORTER_ASSERT(reporter, !mat.invert(&inverse));
    850     mat.setScale(SK_Scalar1, 0);
    851     REPORTER_ASSERT(reporter, !mat.invert(nullptr));
    852     REPORTER_ASSERT(reporter, !mat.invert(&inverse));
    853 
    854     // Inverting this matrix results in a non-finite matrix
    855     mat.setAll(0.0f, 1.0f, 2.0f,
    856                0.0f, 1.0f, -3.40277175e+38f,
    857                1.00003040f, 1.0f, 0.0f);
    858     REPORTER_ASSERT(reporter, !mat.invert(nullptr));
    859     REPORTER_ASSERT(reporter, !mat.invert(&inverse));
    860 
    861     // rectStaysRect test
    862     {
    863         static const struct {
    864             SkScalar    m00, m01, m10, m11;
    865             bool        mStaysRect;
    866         }
    867         gRectStaysRectSamples[] = {
    868             {          0,          0,          0,           0, false },
    869             {          0,          0,          0,  SK_Scalar1, false },
    870             {          0,          0, SK_Scalar1,           0, false },
    871             {          0,          0, SK_Scalar1,  SK_Scalar1, false },
    872             {          0, SK_Scalar1,          0,           0, false },
    873             {          0, SK_Scalar1,          0,  SK_Scalar1, false },
    874             {          0, SK_Scalar1, SK_Scalar1,           0, true },
    875             {          0, SK_Scalar1, SK_Scalar1,  SK_Scalar1, false },
    876             { SK_Scalar1,          0,          0,           0, false },
    877             { SK_Scalar1,          0,          0,  SK_Scalar1, true },
    878             { SK_Scalar1,          0, SK_Scalar1,           0, false },
    879             { SK_Scalar1,          0, SK_Scalar1,  SK_Scalar1, false },
    880             { SK_Scalar1, SK_Scalar1,          0,           0, false },
    881             { SK_Scalar1, SK_Scalar1,          0,  SK_Scalar1, false },
    882             { SK_Scalar1, SK_Scalar1, SK_Scalar1,           0, false },
    883             { SK_Scalar1, SK_Scalar1, SK_Scalar1,  SK_Scalar1, false }
    884         };
    885 
    886         for (size_t i = 0; i < SK_ARRAY_COUNT(gRectStaysRectSamples); i++) {
    887             SkMatrix    m;
    888 
    889             m.reset();
    890             m.set(SkMatrix::kMScaleX, gRectStaysRectSamples[i].m00);
    891             m.set(SkMatrix::kMSkewX,  gRectStaysRectSamples[i].m01);
    892             m.set(SkMatrix::kMSkewY,  gRectStaysRectSamples[i].m10);
    893             m.set(SkMatrix::kMScaleY, gRectStaysRectSamples[i].m11);
    894             REPORTER_ASSERT(reporter,
    895                     m.rectStaysRect() == gRectStaysRectSamples[i].mStaysRect);
    896         }
    897     }
    898 
    899     mat.reset();
    900     mat.set(SkMatrix::kMScaleX, SkIntToScalar(1));
    901     mat.set(SkMatrix::kMSkewX,  SkIntToScalar(2));
    902     mat.set(SkMatrix::kMTransX, SkIntToScalar(3));
    903     mat.set(SkMatrix::kMSkewY,  SkIntToScalar(4));
    904     mat.set(SkMatrix::kMScaleY, SkIntToScalar(5));
    905     mat.set(SkMatrix::kMTransY, SkIntToScalar(6));
    906     SkScalar affine[6];
    907     REPORTER_ASSERT(reporter, mat.asAffine(affine));
    908 
    909     #define affineEqual(e) affine[SkMatrix::kA##e] == mat.get(SkMatrix::kM##e)
    910     REPORTER_ASSERT(reporter, affineEqual(ScaleX));
    911     REPORTER_ASSERT(reporter, affineEqual(SkewY));
    912     REPORTER_ASSERT(reporter, affineEqual(SkewX));
    913     REPORTER_ASSERT(reporter, affineEqual(ScaleY));
    914     REPORTER_ASSERT(reporter, affineEqual(TransX));
    915     REPORTER_ASSERT(reporter, affineEqual(TransY));
    916     #undef affineEqual
    917 
    918     mat.set(SkMatrix::kMPersp1, SK_Scalar1 / 2);
    919     REPORTER_ASSERT(reporter, !mat.asAffine(affine));
    920 
    921     SkMatrix mat2;
    922     mat2.reset();
    923     mat.reset();
    924     SkScalar zero = 0;
    925     mat.set(SkMatrix::kMSkewX, -zero);
    926     REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2));
    927 
    928     mat2.reset();
    929     mat.reset();
    930     mat.set(SkMatrix::kMSkewX, SK_ScalarNaN);
    931     mat2.set(SkMatrix::kMSkewX, SK_ScalarNaN);
    932     REPORTER_ASSERT(reporter, !are_equal(reporter, mat, mat2));
    933 
    934     test_matrix_min_max_scale(reporter);
    935     test_matrix_preserve_shape(reporter);
    936     test_matrix_recttorect(reporter);
    937     test_matrix_decomposition(reporter);
    938     test_matrix_homogeneous(reporter);
    939     test_set9(reporter);
    940 
    941     test_decompScale(reporter);
    942 }
    943 
    944 DEF_TEST(Matrix_Concat, r) {
    945     SkMatrix a;
    946     a.setTranslate(10, 20);
    947 
    948     SkMatrix b;
    949     b.setScale(3, 5);
    950 
    951     SkMatrix expected;
    952     expected.setConcat(a,b);
    953 
    954     REPORTER_ASSERT(r, expected == SkMatrix::Concat(a, b));
    955 }
    956