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 test_matrix_recttorect(skiatest::Reporter* reporter) {
     75     SkRect src, dst;
     76     SkMatrix matrix;
     77 
     78     src.set(0, 0, SK_Scalar1*10, SK_Scalar1*10);
     79     dst = src;
     80     matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
     81     REPORTER_ASSERT(reporter, SkMatrix::kIdentity_Mask == matrix.getType());
     82     REPORTER_ASSERT(reporter, matrix.rectStaysRect());
     83 
     84     dst.offset(SK_Scalar1, SK_Scalar1);
     85     matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
     86     REPORTER_ASSERT(reporter, SkMatrix::kTranslate_Mask == matrix.getType());
     87     REPORTER_ASSERT(reporter, matrix.rectStaysRect());
     88 
     89     dst.fRight += SK_Scalar1;
     90     matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
     91     REPORTER_ASSERT(reporter,
     92                     (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask) == matrix.getType());
     93     REPORTER_ASSERT(reporter, matrix.rectStaysRect());
     94 
     95     dst = src;
     96     dst.fRight = src.fRight * 2;
     97     matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
     98     REPORTER_ASSERT(reporter, SkMatrix::kScale_Mask == matrix.getType());
     99     REPORTER_ASSERT(reporter, matrix.rectStaysRect());
    100 }
    101 
    102 static void test_flatten(skiatest::Reporter* reporter, const SkMatrix& m) {
    103     // add 100 in case we have a bug, I don't want to kill my stack in the test
    104     static const size_t kBufferSize = SkMatrix::kMaxFlattenSize + 100;
    105     char buffer[kBufferSize];
    106     size_t size1 = m.writeToMemory(NULL);
    107     size_t size2 = m.writeToMemory(buffer);
    108     REPORTER_ASSERT(reporter, size1 == size2);
    109     REPORTER_ASSERT(reporter, size1 <= SkMatrix::kMaxFlattenSize);
    110 
    111     SkMatrix m2;
    112     size_t size3 = m2.readFromMemory(buffer, kBufferSize);
    113     REPORTER_ASSERT(reporter, size1 == size3);
    114     REPORTER_ASSERT(reporter, are_equal(reporter, m, m2));
    115 
    116     char buffer2[kBufferSize];
    117     size3 = m2.writeToMemory(buffer2);
    118     REPORTER_ASSERT(reporter, size1 == size3);
    119     REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
    120 }
    121 
    122 static void test_matrix_min_max_scale(skiatest::Reporter* reporter) {
    123     SkScalar scales[2];
    124     bool success;
    125 
    126     SkMatrix identity;
    127     identity.reset();
    128     REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMinScale());
    129     REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMaxScale());
    130     success = identity.getMinMaxScales(scales);
    131     REPORTER_ASSERT(reporter, success && SK_Scalar1 == scales[0] && SK_Scalar1 == scales[1]);
    132 
    133     SkMatrix scale;
    134     scale.setScale(SK_Scalar1 * 2, SK_Scalar1 * 4);
    135     REPORTER_ASSERT(reporter, SK_Scalar1 * 2 == scale.getMinScale());
    136     REPORTER_ASSERT(reporter, SK_Scalar1 * 4 == scale.getMaxScale());
    137     success = scale.getMinMaxScales(scales);
    138     REPORTER_ASSERT(reporter, success && SK_Scalar1 * 2 == scales[0] && SK_Scalar1 * 4 == scales[1]);
    139 
    140     SkMatrix rot90Scale;
    141     rot90Scale.setRotate(90 * SK_Scalar1);
    142     rot90Scale.postScale(SK_Scalar1 / 4, SK_Scalar1 / 2);
    143     REPORTER_ASSERT(reporter, SK_Scalar1 / 4 == rot90Scale.getMinScale());
    144     REPORTER_ASSERT(reporter, SK_Scalar1 / 2 == rot90Scale.getMaxScale());
    145     success = rot90Scale.getMinMaxScales(scales);
    146     REPORTER_ASSERT(reporter, success && SK_Scalar1 / 4  == scales[0] && SK_Scalar1 / 2 == scales[1]);
    147 
    148     SkMatrix rotate;
    149     rotate.setRotate(128 * SK_Scalar1);
    150     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, rotate.getMinScale(), SK_ScalarNearlyZero));
    151     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, rotate.getMaxScale(), SK_ScalarNearlyZero));
    152     success = rotate.getMinMaxScales(scales);
    153     REPORTER_ASSERT(reporter, success);
    154     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, scales[0], SK_ScalarNearlyZero));
    155     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, scales[1], SK_ScalarNearlyZero));
    156 
    157     SkMatrix translate;
    158     translate.setTranslate(10 * SK_Scalar1, -5 * SK_Scalar1);
    159     REPORTER_ASSERT(reporter, SK_Scalar1 == translate.getMinScale());
    160     REPORTER_ASSERT(reporter, SK_Scalar1 == translate.getMaxScale());
    161     success = translate.getMinMaxScales(scales);
    162     REPORTER_ASSERT(reporter, success && SK_Scalar1 == scales[0] && SK_Scalar1 == scales[1]);
    163 
    164     SkMatrix perspX;
    165     perspX.reset();
    166     perspX.setPerspX(SkScalarToPersp(SK_Scalar1 / 1000));
    167     REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMinScale());
    168     REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMaxScale());
    169     // Verify that getMinMaxScales() doesn't update the scales array on failure.
    170     scales[0] = -5;
    171     scales[1] = -5;
    172     success = perspX.getMinMaxScales(scales);
    173     REPORTER_ASSERT(reporter, !success && -5 * SK_Scalar1 == scales[0] && -5 * SK_Scalar1  == scales[1]);
    174 
    175     SkMatrix perspY;
    176     perspY.reset();
    177     perspY.setPerspY(SkScalarToPersp(-SK_Scalar1 / 500));
    178     REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMinScale());
    179     REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMaxScale());
    180     scales[0] = -5;
    181     scales[1] = -5;
    182     success = perspY.getMinMaxScales(scales);
    183     REPORTER_ASSERT(reporter, !success && -5 * SK_Scalar1 == scales[0] && -5 * SK_Scalar1  == scales[1]);
    184 
    185     SkMatrix baseMats[] = {scale, rot90Scale, rotate,
    186                            translate, perspX, perspY};
    187     SkMatrix mats[2*SK_ARRAY_COUNT(baseMats)];
    188     for (size_t i = 0; i < SK_ARRAY_COUNT(baseMats); ++i) {
    189         mats[i] = baseMats[i];
    190         bool invertable = mats[i].invert(&mats[i + SK_ARRAY_COUNT(baseMats)]);
    191         REPORTER_ASSERT(reporter, invertable);
    192     }
    193     SkRandom rand;
    194     for (int m = 0; m < 1000; ++m) {
    195         SkMatrix mat;
    196         mat.reset();
    197         for (int i = 0; i < 4; ++i) {
    198             int x = rand.nextU() % SK_ARRAY_COUNT(mats);
    199             mat.postConcat(mats[x]);
    200         }
    201 
    202         SkScalar minScale = mat.getMinScale();
    203         SkScalar maxScale = mat.getMaxScale();
    204         REPORTER_ASSERT(reporter, (minScale < 0) == (maxScale < 0));
    205         REPORTER_ASSERT(reporter, (maxScale < 0) == mat.hasPerspective());
    206 
    207         SkScalar scales[2];
    208         bool success = mat.getMinMaxScales(scales);
    209         REPORTER_ASSERT(reporter, success == !mat.hasPerspective());
    210         REPORTER_ASSERT(reporter, !success || (scales[0] == minScale && scales[1] == maxScale));
    211 
    212         if (mat.hasPerspective()) {
    213             m -= 1; // try another non-persp matrix
    214             continue;
    215         }
    216 
    217         // test a bunch of vectors. All should be scaled by between minScale and maxScale
    218         // (modulo some error) and we should find a vector that is scaled by almost each.
    219         static const SkScalar gVectorScaleTol = (105 * SK_Scalar1) / 100;
    220         static const SkScalar gCloseScaleTol = (97 * SK_Scalar1) / 100;
    221         SkScalar max = 0, min = SK_ScalarMax;
    222         SkVector vectors[1000];
    223         for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
    224             vectors[i].fX = rand.nextSScalar1();
    225             vectors[i].fY = rand.nextSScalar1();
    226             if (!vectors[i].normalize()) {
    227                 i -= 1;
    228                 continue;
    229             }
    230         }
    231         mat.mapVectors(vectors, SK_ARRAY_COUNT(vectors));
    232         for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
    233             SkScalar d = vectors[i].length();
    234             REPORTER_ASSERT(reporter, SkScalarDiv(d, maxScale) < gVectorScaleTol);
    235             REPORTER_ASSERT(reporter, SkScalarDiv(minScale, d) < gVectorScaleTol);
    236             if (max < d) {
    237                 max = d;
    238             }
    239             if (min > d) {
    240                 min = d;
    241             }
    242         }
    243         REPORTER_ASSERT(reporter, SkScalarDiv(max, maxScale) >= gCloseScaleTol);
    244         REPORTER_ASSERT(reporter, SkScalarDiv(minScale, min) >= gCloseScaleTol);
    245     }
    246 }
    247 
    248 static void test_matrix_is_similarity(skiatest::Reporter* reporter) {
    249     SkMatrix mat;
    250 
    251     // identity
    252     mat.setIdentity();
    253     REPORTER_ASSERT(reporter, mat.isSimilarity());
    254 
    255     // translation only
    256     mat.reset();
    257     mat.setTranslate(SkIntToScalar(100), SkIntToScalar(100));
    258     REPORTER_ASSERT(reporter, mat.isSimilarity());
    259 
    260     // scale with same size
    261     mat.reset();
    262     mat.setScale(SkIntToScalar(15), SkIntToScalar(15));
    263     REPORTER_ASSERT(reporter, mat.isSimilarity());
    264 
    265     // scale with one negative
    266     mat.reset();
    267     mat.setScale(SkIntToScalar(-15), SkIntToScalar(15));
    268     REPORTER_ASSERT(reporter, mat.isSimilarity());
    269 
    270     // scale with different size
    271     mat.reset();
    272     mat.setScale(SkIntToScalar(15), SkIntToScalar(20));
    273     REPORTER_ASSERT(reporter, !mat.isSimilarity());
    274 
    275     // scale with same size at a pivot point
    276     mat.reset();
    277     mat.setScale(SkIntToScalar(15), SkIntToScalar(15),
    278                  SkIntToScalar(2), SkIntToScalar(2));
    279     REPORTER_ASSERT(reporter, mat.isSimilarity());
    280 
    281     // scale with different size at a pivot point
    282     mat.reset();
    283     mat.setScale(SkIntToScalar(15), SkIntToScalar(20),
    284                  SkIntToScalar(2), SkIntToScalar(2));
    285     REPORTER_ASSERT(reporter, !mat.isSimilarity());
    286 
    287     // skew with same size
    288     mat.reset();
    289     mat.setSkew(SkIntToScalar(15), SkIntToScalar(15));
    290     REPORTER_ASSERT(reporter, !mat.isSimilarity());
    291 
    292     // skew with different size
    293     mat.reset();
    294     mat.setSkew(SkIntToScalar(15), SkIntToScalar(20));
    295     REPORTER_ASSERT(reporter, !mat.isSimilarity());
    296 
    297     // skew with same size at a pivot point
    298     mat.reset();
    299     mat.setSkew(SkIntToScalar(15), SkIntToScalar(15),
    300                 SkIntToScalar(2), SkIntToScalar(2));
    301     REPORTER_ASSERT(reporter, !mat.isSimilarity());
    302 
    303     // skew with different size at a pivot point
    304     mat.reset();
    305     mat.setSkew(SkIntToScalar(15), SkIntToScalar(20),
    306                 SkIntToScalar(2), SkIntToScalar(2));
    307     REPORTER_ASSERT(reporter, !mat.isSimilarity());
    308 
    309     // perspective x
    310     mat.reset();
    311     mat.setPerspX(SkScalarToPersp(SK_Scalar1 / 2));
    312     REPORTER_ASSERT(reporter, !mat.isSimilarity());
    313 
    314     // perspective y
    315     mat.reset();
    316     mat.setPerspY(SkScalarToPersp(SK_Scalar1 / 2));
    317     REPORTER_ASSERT(reporter, !mat.isSimilarity());
    318 
    319     // rotate
    320     for (int angle = 0; angle < 360; ++angle) {
    321         mat.reset();
    322         mat.setRotate(SkIntToScalar(angle));
    323 #ifndef SK_CPU_ARM64
    324         REPORTER_ASSERT(reporter, mat.isSimilarity());
    325 #else
    326         // 64-bit ARM devices built with -O2 and -ffp-contract=fast have a loss
    327         // of precision and require that we have a higher tolerance
    328         REPORTER_ASSERT(reporter, mat.isSimilarity(SK_ScalarNearlyZero + 0.00010113f));
    329 #endif
    330     }
    331 
    332     // see if there are any accumulated precision issues
    333     mat.reset();
    334     for (int i = 1; i < 360; i++) {
    335         mat.postRotate(SkIntToScalar(1));
    336     }
    337     REPORTER_ASSERT(reporter, mat.isSimilarity());
    338 
    339     // rotate + translate
    340     mat.reset();
    341     mat.setRotate(SkIntToScalar(30));
    342     mat.postTranslate(SkIntToScalar(10), SkIntToScalar(20));
    343     REPORTER_ASSERT(reporter, mat.isSimilarity());
    344 
    345     // rotate + uniform scale
    346     mat.reset();
    347     mat.setRotate(SkIntToScalar(30));
    348     mat.postScale(SkIntToScalar(2), SkIntToScalar(2));
    349     REPORTER_ASSERT(reporter, mat.isSimilarity());
    350 
    351     // rotate + non-uniform scale
    352     mat.reset();
    353     mat.setRotate(SkIntToScalar(30));
    354     mat.postScale(SkIntToScalar(3), SkIntToScalar(2));
    355     REPORTER_ASSERT(reporter, !mat.isSimilarity());
    356 
    357     // all zero
    358     mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, 0);
    359     REPORTER_ASSERT(reporter, !mat.isSimilarity());
    360 
    361     // all zero except perspective
    362     mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, SK_Scalar1);
    363     REPORTER_ASSERT(reporter, !mat.isSimilarity());
    364 
    365     // scales zero, only skews
    366     mat.setAll(0, SK_Scalar1, 0,
    367                SK_Scalar1, 0, 0,
    368                0, 0, SkMatrix::I()[8]);
    369     REPORTER_ASSERT(reporter, mat.isSimilarity());
    370 }
    371 
    372 // For test_matrix_decomposition, below.
    373 static bool scalar_nearly_equal_relative(SkScalar a, SkScalar b,
    374                                          SkScalar tolerance = SK_ScalarNearlyZero) {
    375     // from Bruce Dawson
    376     // absolute check
    377     SkScalar diff = SkScalarAbs(a - b);
    378     if (diff < tolerance) {
    379         return true;
    380     }
    381 
    382     // relative check
    383     a = SkScalarAbs(a);
    384     b = SkScalarAbs(b);
    385     SkScalar largest = (b > a) ? b : a;
    386 
    387     if (diff <= largest*tolerance) {
    388         return true;
    389     }
    390 
    391     return false;
    392 }
    393 
    394 static bool check_matrix_recomposition(const SkMatrix& mat,
    395                                        const SkPoint& rotation1,
    396                                        const SkPoint& scale,
    397                                        const SkPoint& rotation2) {
    398     SkScalar c1 = rotation1.fX;
    399     SkScalar s1 = rotation1.fY;
    400     SkScalar scaleX = scale.fX;
    401     SkScalar scaleY = scale.fY;
    402     SkScalar c2 = rotation2.fX;
    403     SkScalar s2 = rotation2.fY;
    404 
    405     // We do a relative check here because large scale factors cause problems with an absolute check
    406     bool result = scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX],
    407                                                scaleX*c1*c2 - scaleY*s1*s2) &&
    408                   scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX],
    409                                                -scaleX*s1*c2 - scaleY*c1*s2) &&
    410                   scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY],
    411                                                scaleX*c1*s2 + scaleY*s1*c2) &&
    412                   scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY],
    413                                                -scaleX*s1*s2 + scaleY*c1*c2);
    414     return result;
    415 }
    416 
    417 static void test_matrix_decomposition(skiatest::Reporter* reporter) {
    418     SkMatrix mat;
    419     SkPoint rotation1, scale, rotation2;
    420 
    421     const float kRotation0 = 15.5f;
    422     const float kRotation1 = -50.f;
    423     const float kScale0 = 5000.f;
    424     const float kScale1 = 0.001f;
    425 
    426     // identity
    427     mat.reset();
    428     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    429     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    430     // make sure it doesn't crash if we pass in NULLs
    431     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, NULL, NULL, NULL));
    432 
    433     // rotation only
    434     mat.setRotate(kRotation0);
    435     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    436     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    437 
    438     // uniform scale only
    439     mat.setScale(kScale0, kScale0);
    440     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    441     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    442 
    443     // anisotropic scale only
    444     mat.setScale(kScale1, kScale0);
    445     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    446     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    447 
    448     // rotation then uniform scale
    449     mat.setRotate(kRotation1);
    450     mat.postScale(kScale0, kScale0);
    451     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    452     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    453 
    454     // uniform scale then rotation
    455     mat.setScale(kScale0, kScale0);
    456     mat.postRotate(kRotation1);
    457     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    458     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    459 
    460     // rotation then uniform scale+reflection
    461     mat.setRotate(kRotation0);
    462     mat.postScale(kScale1, -kScale1);
    463     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    464     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    465 
    466     // uniform scale+reflection, then rotate
    467     mat.setScale(kScale0, -kScale0);
    468     mat.postRotate(kRotation1);
    469     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    470     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    471 
    472     // rotation then anisotropic scale
    473     mat.setRotate(kRotation1);
    474     mat.postScale(kScale1, kScale0);
    475     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    476     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    477 
    478     // rotation then anisotropic scale
    479     mat.setRotate(90);
    480     mat.postScale(kScale1, kScale0);
    481     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    482     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    483 
    484     // anisotropic scale then rotation
    485     mat.setScale(kScale1, kScale0);
    486     mat.postRotate(kRotation0);
    487     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    488     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    489 
    490     // anisotropic scale then rotation
    491     mat.setScale(kScale1, kScale0);
    492     mat.postRotate(90);
    493     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    494     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    495 
    496     // rotation, uniform scale, then different rotation
    497     mat.setRotate(kRotation1);
    498     mat.postScale(kScale0, kScale0);
    499     mat.postRotate(kRotation0);
    500     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    501     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    502 
    503     // rotation, anisotropic scale, then different rotation
    504     mat.setRotate(kRotation0);
    505     mat.postScale(kScale1, kScale0);
    506     mat.postRotate(kRotation1);
    507     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    508     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    509 
    510     // rotation, anisotropic scale + reflection, then different rotation
    511     mat.setRotate(kRotation0);
    512     mat.postScale(-kScale1, kScale0);
    513     mat.postRotate(kRotation1);
    514     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    515     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    516 
    517     // try some random matrices
    518     SkRandom rand;
    519     for (int m = 0; m < 1000; ++m) {
    520         SkScalar rot0 = rand.nextRangeF(-180, 180);
    521         SkScalar sx = rand.nextRangeF(-3000.f, 3000.f);
    522         SkScalar sy = rand.nextRangeF(-3000.f, 3000.f);
    523         SkScalar rot1 = rand.nextRangeF(-180, 180);
    524         mat.setRotate(rot0);
    525         mat.postScale(sx, sy);
    526         mat.postRotate(rot1);
    527 
    528         if (SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)) {
    529             REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    530         } else {
    531             // if the matrix is degenerate, the basis vectors should be near-parallel or near-zero
    532             SkScalar perpdot = mat[SkMatrix::kMScaleX]*mat[SkMatrix::kMScaleY] -
    533                                mat[SkMatrix::kMSkewX]*mat[SkMatrix::kMSkewY];
    534             REPORTER_ASSERT(reporter, SkScalarNearlyZero(perpdot));
    535         }
    536     }
    537 
    538     // translation shouldn't affect this
    539     mat.postTranslate(-1000.f, 1000.f);
    540     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    541     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    542 
    543     // perspective shouldn't affect this
    544     mat[SkMatrix::kMPersp0] = 12.f;
    545     mat[SkMatrix::kMPersp1] = 4.f;
    546     mat[SkMatrix::kMPersp2] = 1872.f;
    547     REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    548     REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
    549 
    550     // degenerate matrices
    551     // mostly zero entries
    552     mat.reset();
    553     mat[SkMatrix::kMScaleX] = 0.f;
    554     REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    555     mat.reset();
    556     mat[SkMatrix::kMScaleY] = 0.f;
    557     REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    558     mat.reset();
    559     // linearly dependent entries
    560     mat[SkMatrix::kMScaleX] = 1.f;
    561     mat[SkMatrix::kMSkewX] = 2.f;
    562     mat[SkMatrix::kMSkewY] = 4.f;
    563     mat[SkMatrix::kMScaleY] = 8.f;
    564     REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
    565 }
    566 
    567 // For test_matrix_homogeneous, below.
    568 static bool scalar_array_nearly_equal_relative(const SkScalar a[], const SkScalar b[], int count) {
    569     for (int i = 0; i < count; ++i) {
    570         if (!scalar_nearly_equal_relative(a[i], b[i])) {
    571             return false;
    572         }
    573     }
    574     return true;
    575 }
    576 
    577 // For test_matrix_homogeneous, below.
    578 // Maps a single triple in src using m and compares results to those in dst
    579 static bool naive_homogeneous_mapping(const SkMatrix& m, const SkScalar src[3],
    580                                       const SkScalar dst[3]) {
    581     SkScalar res[3];
    582     SkScalar ms[9] = {m[0], m[1], m[2],
    583                       m[3], m[4], m[5],
    584                       m[6], m[7], m[8]};
    585     res[0] = src[0] * ms[0] + src[1] * ms[1] + src[2] * ms[2];
    586     res[1] = src[0] * ms[3] + src[1] * ms[4] + src[2] * ms[5];
    587     res[2] = src[0] * ms[6] + src[1] * ms[7] + src[2] * ms[8];
    588     return scalar_array_nearly_equal_relative(res, dst, 3);
    589 }
    590 
    591 static void test_matrix_homogeneous(skiatest::Reporter* reporter) {
    592     SkMatrix mat;
    593 
    594     const float kRotation0 = 15.5f;
    595     const float kRotation1 = -50.f;
    596     const float kScale0 = 5000.f;
    597 
    598     const int kTripleCount = 1000;
    599     const int kMatrixCount = 1000;
    600     SkRandom rand;
    601 
    602     SkScalar randTriples[3*kTripleCount];
    603     for (int i = 0; i < 3*kTripleCount; ++i) {
    604         randTriples[i] = rand.nextRangeF(-3000.f, 3000.f);
    605     }
    606 
    607     SkMatrix mats[kMatrixCount];
    608     for (int i = 0; i < kMatrixCount; ++i) {
    609         for (int j = 0; j < 9; ++j) {
    610             mats[i].set(j, rand.nextRangeF(-3000.f, 3000.f));
    611         }
    612     }
    613 
    614     // identity
    615     {
    616     mat.reset();
    617     SkScalar dst[3*kTripleCount];
    618     mat.mapHomogeneousPoints(dst, randTriples, kTripleCount);
    619     REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(randTriples, dst, kTripleCount*3));
    620     }
    621 
    622     // zero matrix
    623     {
    624     mat.setAll(0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f);
    625     SkScalar dst[3*kTripleCount];
    626     mat.mapHomogeneousPoints(dst, randTriples, kTripleCount);
    627     SkScalar zeros[3] = {0.f, 0.f, 0.f};
    628     for (int i = 0; i < kTripleCount; ++i) {
    629         REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(&dst[i*3], zeros, 3));
    630     }
    631     }
    632 
    633     // zero point
    634     {
    635     SkScalar zeros[3] = {0.f, 0.f, 0.f};
    636     for (int i = 0; i < kMatrixCount; ++i) {
    637         SkScalar dst[3];
    638         mats[i].mapHomogeneousPoints(dst, zeros, 1);
    639         REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(dst, zeros, 3));
    640     }
    641     }
    642 
    643     // doesn't crash with null dst, src, count == 0
    644     {
    645     mats[0].mapHomogeneousPoints(NULL, NULL, 0);
    646     }
    647 
    648     // uniform scale of point
    649     {
    650     mat.setScale(kScale0, kScale0);
    651     SkScalar dst[3];
    652     SkScalar src[3] = {randTriples[0], randTriples[1], 1.f};
    653     SkPoint pnt;
    654     pnt.set(src[0], src[1]);
    655     mat.mapHomogeneousPoints(dst, src, 1);
    656     mat.mapPoints(&pnt, &pnt, 1);
    657     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX));
    658     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY));
    659     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1));
    660     }
    661 
    662     // rotation of point
    663     {
    664     mat.setRotate(kRotation0);
    665     SkScalar dst[3];
    666     SkScalar src[3] = {randTriples[0], randTriples[1], 1.f};
    667     SkPoint pnt;
    668     pnt.set(src[0], src[1]);
    669     mat.mapHomogeneousPoints(dst, src, 1);
    670     mat.mapPoints(&pnt, &pnt, 1);
    671     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX));
    672     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY));
    673     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1));
    674     }
    675 
    676     // rotation, scale, rotation of point
    677     {
    678     mat.setRotate(kRotation1);
    679     mat.postScale(kScale0, kScale0);
    680     mat.postRotate(kRotation0);
    681     SkScalar dst[3];
    682     SkScalar src[3] = {randTriples[0], randTriples[1], 1.f};
    683     SkPoint pnt;
    684     pnt.set(src[0], src[1]);
    685     mat.mapHomogeneousPoints(dst, src, 1);
    686     mat.mapPoints(&pnt, &pnt, 1);
    687     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX));
    688     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY));
    689     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1));
    690     }
    691 
    692     // compare with naive approach
    693     {
    694     for (int i = 0; i < kMatrixCount; ++i) {
    695         for (int j = 0; j < kTripleCount; ++j) {
    696             SkScalar dst[3];
    697             mats[i].mapHomogeneousPoints(dst, &randTriples[j*3], 1);
    698             REPORTER_ASSERT(reporter, naive_homogeneous_mapping(mats[i], &randTriples[j*3], dst));
    699         }
    700     }
    701     }
    702 
    703 }
    704 
    705 DEF_TEST(Matrix, reporter) {
    706     SkMatrix    mat, inverse, iden1, iden2;
    707 
    708     mat.reset();
    709     mat.setTranslate(SK_Scalar1, SK_Scalar1);
    710     REPORTER_ASSERT(reporter, mat.invert(&inverse));
    711     iden1.setConcat(mat, inverse);
    712     REPORTER_ASSERT(reporter, is_identity(iden1));
    713 
    714     mat.setScale(SkIntToScalar(2), SkIntToScalar(4));
    715     REPORTER_ASSERT(reporter, mat.invert(&inverse));
    716     iden1.setConcat(mat, inverse);
    717     REPORTER_ASSERT(reporter, is_identity(iden1));
    718     test_flatten(reporter, mat);
    719 
    720     mat.setScale(SK_Scalar1/2, SkIntToScalar(2));
    721     REPORTER_ASSERT(reporter, mat.invert(&inverse));
    722     iden1.setConcat(mat, inverse);
    723     REPORTER_ASSERT(reporter, is_identity(iden1));
    724     test_flatten(reporter, mat);
    725 
    726     mat.setScale(SkIntToScalar(3), SkIntToScalar(5), SkIntToScalar(20), 0);
    727     mat.postRotate(SkIntToScalar(25));
    728     REPORTER_ASSERT(reporter, mat.invert(NULL));
    729     REPORTER_ASSERT(reporter, mat.invert(&inverse));
    730     iden1.setConcat(mat, inverse);
    731     REPORTER_ASSERT(reporter, is_identity(iden1));
    732     iden2.setConcat(inverse, mat);
    733     REPORTER_ASSERT(reporter, is_identity(iden2));
    734     test_flatten(reporter, mat);
    735     test_flatten(reporter, iden2);
    736 
    737     mat.setScale(0, SK_Scalar1);
    738     REPORTER_ASSERT(reporter, !mat.invert(NULL));
    739     REPORTER_ASSERT(reporter, !mat.invert(&inverse));
    740     mat.setScale(SK_Scalar1, 0);
    741     REPORTER_ASSERT(reporter, !mat.invert(NULL));
    742     REPORTER_ASSERT(reporter, !mat.invert(&inverse));
    743 
    744     // rectStaysRect test
    745     {
    746         static const struct {
    747             SkScalar    m00, m01, m10, m11;
    748             bool        mStaysRect;
    749         }
    750         gRectStaysRectSamples[] = {
    751             {          0,          0,          0,           0, false },
    752             {          0,          0,          0,  SK_Scalar1, false },
    753             {          0,          0, SK_Scalar1,           0, false },
    754             {          0,          0, SK_Scalar1,  SK_Scalar1, false },
    755             {          0, SK_Scalar1,          0,           0, false },
    756             {          0, SK_Scalar1,          0,  SK_Scalar1, false },
    757             {          0, SK_Scalar1, SK_Scalar1,           0, true },
    758             {          0, SK_Scalar1, SK_Scalar1,  SK_Scalar1, false },
    759             { SK_Scalar1,          0,          0,           0, false },
    760             { SK_Scalar1,          0,          0,  SK_Scalar1, true },
    761             { SK_Scalar1,          0, SK_Scalar1,           0, false },
    762             { SK_Scalar1,          0, SK_Scalar1,  SK_Scalar1, false },
    763             { SK_Scalar1, SK_Scalar1,          0,           0, false },
    764             { SK_Scalar1, SK_Scalar1,          0,  SK_Scalar1, false },
    765             { SK_Scalar1, SK_Scalar1, SK_Scalar1,           0, false },
    766             { SK_Scalar1, SK_Scalar1, SK_Scalar1,  SK_Scalar1, false }
    767         };
    768 
    769         for (size_t i = 0; i < SK_ARRAY_COUNT(gRectStaysRectSamples); i++) {
    770             SkMatrix    m;
    771 
    772             m.reset();
    773             m.set(SkMatrix::kMScaleX, gRectStaysRectSamples[i].m00);
    774             m.set(SkMatrix::kMSkewX,  gRectStaysRectSamples[i].m01);
    775             m.set(SkMatrix::kMSkewY,  gRectStaysRectSamples[i].m10);
    776             m.set(SkMatrix::kMScaleY, gRectStaysRectSamples[i].m11);
    777             REPORTER_ASSERT(reporter,
    778                     m.rectStaysRect() == gRectStaysRectSamples[i].mStaysRect);
    779         }
    780     }
    781 
    782     mat.reset();
    783     mat.set(SkMatrix::kMScaleX, SkIntToScalar(1));
    784     mat.set(SkMatrix::kMSkewX,  SkIntToScalar(2));
    785     mat.set(SkMatrix::kMTransX, SkIntToScalar(3));
    786     mat.set(SkMatrix::kMSkewY,  SkIntToScalar(4));
    787     mat.set(SkMatrix::kMScaleY, SkIntToScalar(5));
    788     mat.set(SkMatrix::kMTransY, SkIntToScalar(6));
    789     SkScalar affine[6];
    790     REPORTER_ASSERT(reporter, mat.asAffine(affine));
    791 
    792     #define affineEqual(e) affine[SkMatrix::kA##e] == mat.get(SkMatrix::kM##e)
    793     REPORTER_ASSERT(reporter, affineEqual(ScaleX));
    794     REPORTER_ASSERT(reporter, affineEqual(SkewY));
    795     REPORTER_ASSERT(reporter, affineEqual(SkewX));
    796     REPORTER_ASSERT(reporter, affineEqual(ScaleY));
    797     REPORTER_ASSERT(reporter, affineEqual(TransX));
    798     REPORTER_ASSERT(reporter, affineEqual(TransY));
    799     #undef affineEqual
    800 
    801     mat.set(SkMatrix::kMPersp1, SkScalarToPersp(SK_Scalar1 / 2));
    802     REPORTER_ASSERT(reporter, !mat.asAffine(affine));
    803 
    804     SkMatrix mat2;
    805     mat2.reset();
    806     mat.reset();
    807     SkScalar zero = 0;
    808     mat.set(SkMatrix::kMSkewX, -zero);
    809     REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2));
    810 
    811     mat2.reset();
    812     mat.reset();
    813     mat.set(SkMatrix::kMSkewX, SK_ScalarNaN);
    814     mat2.set(SkMatrix::kMSkewX, SK_ScalarNaN);
    815     REPORTER_ASSERT(reporter, !are_equal(reporter, mat, mat2));
    816 
    817     test_matrix_min_max_scale(reporter);
    818     test_matrix_is_similarity(reporter);
    819     test_matrix_recttorect(reporter);
    820     test_matrix_decomposition(reporter);
    821     test_matrix_homogeneous(reporter);
    822 }
    823 
    824 DEF_TEST(Matrix_Concat, r) {
    825     SkMatrix a;
    826     a.setTranslate(10, 20);
    827 
    828     SkMatrix b;
    829     b.setScale(3, 5);
    830 
    831     SkMatrix expected;
    832     expected.setConcat(a,b);
    833 
    834     REPORTER_ASSERT(r, expected == SkMatrix::Concat(a, b));
    835 }
    836