blob: efbd02d2c626a80ec1bf187bf8fbaa697b500367 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.comed673312009-02-27 16:24:51 +00008#include "Test.h"
tomhudson@google.com889bd8b2011-09-27 17:38:17 +00009#include "SkMath.h"
reed@android.comed673312009-02-27 16:24:51 +000010#include "SkMatrix.h"
bsalomon@google.com38396322011-09-09 19:32:04 +000011#include "SkRandom.h"
reed@android.comed673312009-02-27 16:24:51 +000012
13static bool nearly_equal_scalar(SkScalar a, SkScalar b) {
epoger@google.com2047f002011-05-17 17:36:59 +000014 // Note that we get more compounded error for multiple operations when
15 // SK_SCALAR_IS_FIXED.
reed@android.comed673312009-02-27 16:24:51 +000016#ifdef SK_SCALAR_IS_FLOAT
epoger@google.com2047f002011-05-17 17:36:59 +000017 const SkScalar tolerance = SK_Scalar1 / 200000;
reed@android.comed673312009-02-27 16:24:51 +000018#else
epoger@google.com2047f002011-05-17 17:36:59 +000019 const SkScalar tolerance = SK_Scalar1 / 1024;
reed@android.comed673312009-02-27 16:24:51 +000020#endif
21
22 return SkScalarAbs(a - b) <= tolerance;
23}
24
25static bool nearly_equal(const SkMatrix& a, const SkMatrix& b) {
26 for (int i = 0; i < 9; i++) {
27 if (!nearly_equal_scalar(a[i], b[i])) {
reed@android.comd4134452011-02-09 02:24:26 +000028 printf("not equal %g %g\n", (float)a[i], (float)b[i]);
reed@android.comed673312009-02-27 16:24:51 +000029 return false;
30 }
31 }
32 return true;
33}
34
bsalomon@google.com8fe84b52012-03-26 15:24:27 +000035static bool are_equal(skiatest::Reporter* reporter,
36 const SkMatrix& a,
37 const SkMatrix& b) {
38 bool equal = a == b;
39 bool cheapEqual = a.cheapEqualTo(b);
40 if (equal != cheapEqual) {
41#if SK_SCALAR_IS_FLOAT
bsalomon@google.com39d4f3a2012-03-26 17:25:45 +000042 if (equal) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +000043 bool foundZeroSignDiff = false;
44 for (int i = 0; i < 9; ++i) {
45 float aVal = a.get(i);
46 float bVal = b.get(i);
bsalomon@google.com373ebc62012-09-26 13:08:56 +000047 int aValI = *SkTCast<int*>(&aVal);
48 int bValI = *SkTCast<int*>(&bVal);
bsalomon@google.com8fe84b52012-03-26 15:24:27 +000049 if (0 == aVal && 0 == bVal && aValI != bValI) {
50 foundZeroSignDiff = true;
51 } else {
52 REPORTER_ASSERT(reporter, aVal == bVal && aValI == aValI);
53 }
54 }
55 REPORTER_ASSERT(reporter, foundZeroSignDiff);
56 } else {
57 bool foundNaN = false;
58 for (int i = 0; i < 9; ++i) {
59 float aVal = a.get(i);
60 float bVal = b.get(i);
bsalomon@google.com373ebc62012-09-26 13:08:56 +000061 int aValI = *SkTCast<int*>(&aVal);
62 int bValI = *SkTCast<int*>(&bVal);
bsalomon@google.com8fe84b52012-03-26 15:24:27 +000063 if (sk_float_isnan(aVal) && aValI == bValI) {
64 foundNaN = true;
65 } else {
66 REPORTER_ASSERT(reporter, aVal == bVal && aValI == bValI);
67 }
68 }
69 REPORTER_ASSERT(reporter, foundNaN);
70 }
71#else
72 REPORTER_ASSERT(reporter, false);
73#endif
74 }
75 return equal;
76}
77
reed@android.comed673312009-02-27 16:24:51 +000078static bool is_identity(const SkMatrix& m) {
79 SkMatrix identity;
reed@android.com80e39a72009-04-02 16:59:40 +000080 identity.reset();
reed@android.comed673312009-02-27 16:24:51 +000081 return nearly_equal(m, identity);
82}
83
reed@google.com97cd69c2012-10-12 14:35:48 +000084static void test_matrix_recttorect(skiatest::Reporter* reporter) {
85 SkRect src, dst;
86 SkMatrix matrix;
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000087
reed@google.com97cd69c2012-10-12 14:35:48 +000088 src.set(0, 0, SK_Scalar1*10, SK_Scalar1*10);
89 dst = src;
90 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
91 REPORTER_ASSERT(reporter, SkMatrix::kIdentity_Mask == matrix.getType());
92 REPORTER_ASSERT(reporter, matrix.rectStaysRect());
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000093
reed@google.com97cd69c2012-10-12 14:35:48 +000094 dst.offset(SK_Scalar1, SK_Scalar1);
95 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
96 REPORTER_ASSERT(reporter, SkMatrix::kTranslate_Mask == matrix.getType());
97 REPORTER_ASSERT(reporter, matrix.rectStaysRect());
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000098
reed@google.com97cd69c2012-10-12 14:35:48 +000099 dst.fRight += SK_Scalar1;
100 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
101 REPORTER_ASSERT(reporter, SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask == matrix.getType());
102 REPORTER_ASSERT(reporter, matrix.rectStaysRect());
103
104 dst = src;
105 dst.fRight = src.fRight * 2;
106 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
107 REPORTER_ASSERT(reporter, SkMatrix::kScale_Mask == matrix.getType());
108 REPORTER_ASSERT(reporter, matrix.rectStaysRect());
109}
110
reed@android.com4b7577b2009-06-29 16:14:41 +0000111static void test_flatten(skiatest::Reporter* reporter, const SkMatrix& m) {
112 // add 100 in case we have a bug, I don't want to kill my stack in the test
113 char buffer[SkMatrix::kMaxFlattenSize + 100];
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000114 uint32_t size1 = m.writeToMemory(NULL);
115 uint32_t size2 = m.writeToMemory(buffer);
reed@android.com4b7577b2009-06-29 16:14:41 +0000116 REPORTER_ASSERT(reporter, size1 == size2);
117 REPORTER_ASSERT(reporter, size1 <= SkMatrix::kMaxFlattenSize);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000118
reed@android.com4b7577b2009-06-29 16:14:41 +0000119 SkMatrix m2;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000120 uint32_t size3 = m2.readFromMemory(buffer);
121 REPORTER_ASSERT(reporter, size1 == size3);
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000122 REPORTER_ASSERT(reporter, are_equal(reporter, m, m2));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000123
reed@android.com4b7577b2009-06-29 16:14:41 +0000124 char buffer2[SkMatrix::kMaxFlattenSize + 100];
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000125 size3 = m2.writeToMemory(buffer2);
126 REPORTER_ASSERT(reporter, size1 == size3);
reed@android.com4b7577b2009-06-29 16:14:41 +0000127 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
128}
129
caryclark@google.com42639cd2012-06-06 12:03:39 +0000130static void test_matrix_max_stretch(skiatest::Reporter* reporter) {
bsalomon@google.com38396322011-09-09 19:32:04 +0000131 SkMatrix identity;
132 identity.reset();
133 REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMaxStretch());
134
135 SkMatrix scale;
136 scale.setScale(SK_Scalar1 * 2, SK_Scalar1 * 4);
137 REPORTER_ASSERT(reporter, SK_Scalar1 * 4 == scale.getMaxStretch());
138
139 SkMatrix rot90Scale;
140 rot90Scale.setRotate(90 * SK_Scalar1);
141 rot90Scale.postScale(SK_Scalar1 / 4, SK_Scalar1 / 2);
142 REPORTER_ASSERT(reporter, SK_Scalar1 / 2 == rot90Scale.getMaxStretch());
143
144 SkMatrix rotate;
145 rotate.setRotate(128 * SK_Scalar1);
146 REPORTER_ASSERT(reporter, SkScalarAbs(SK_Scalar1 - rotate.getMaxStretch()) <= SK_ScalarNearlyZero);
147
148 SkMatrix translate;
149 translate.setTranslate(10 * SK_Scalar1, -5 * SK_Scalar1);
150 REPORTER_ASSERT(reporter, SK_Scalar1 == translate.getMaxStretch());
151
152 SkMatrix perspX;
153 perspX.reset();
bungeman@google.com07faed12011-10-07 21:55:56 +0000154 perspX.setPerspX(SkScalarToPersp(SK_Scalar1 / 1000));
bsalomon@google.com38396322011-09-09 19:32:04 +0000155 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMaxStretch());
156
157 SkMatrix perspY;
158 perspY.reset();
bungeman@google.com07faed12011-10-07 21:55:56 +0000159 perspY.setPerspX(SkScalarToPersp(-SK_Scalar1 / 500));
bsalomon@google.com38396322011-09-09 19:32:04 +0000160 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMaxStretch());
161
162 SkMatrix baseMats[] = {scale, rot90Scale, rotate,
163 translate, perspX, perspY};
164 SkMatrix mats[2*SK_ARRAY_COUNT(baseMats)];
tomhudson@google.com83a44462011-10-27 15:27:51 +0000165 for (size_t i = 0; i < SK_ARRAY_COUNT(baseMats); ++i) {
bsalomon@google.com38396322011-09-09 19:32:04 +0000166 mats[i] = baseMats[i];
167 bool invertable = mats[i].invert(&mats[i + SK_ARRAY_COUNT(baseMats)]);
168 REPORTER_ASSERT(reporter, invertable);
169 }
170 SkRandom rand;
171 for (int m = 0; m < 1000; ++m) {
172 SkMatrix mat;
173 mat.reset();
174 for (int i = 0; i < 4; ++i) {
175 int x = rand.nextU() % SK_ARRAY_COUNT(mats);
176 mat.postConcat(mats[x]);
177 }
178 SkScalar stretch = mat.getMaxStretch();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000179
bsalomon@google.com38396322011-09-09 19:32:04 +0000180 if ((stretch < 0) != mat.hasPerspective()) {
181 stretch = mat.getMaxStretch();
182 }
183
184 REPORTER_ASSERT(reporter, (stretch < 0) == mat.hasPerspective());
185
186 if (mat.hasPerspective()) {
187 m -= 1; // try another non-persp matrix
188 continue;
189 }
190
191 // test a bunch of vectors. None should be scaled by more than stretch
192 // (modulo some error) and we should find a vector that is scaled by
193 // almost stretch.
194 static const SkScalar gStretchTol = (105 * SK_Scalar1) / 100;
195 static const SkScalar gMaxStretchTol = (97 * SK_Scalar1) / 100;
196 SkScalar max = 0;
197 SkVector vectors[1000];
tomhudson@google.com83a44462011-10-27 15:27:51 +0000198 for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
bsalomon@google.com38396322011-09-09 19:32:04 +0000199 vectors[i].fX = rand.nextSScalar1();
200 vectors[i].fY = rand.nextSScalar1();
201 if (!vectors[i].normalize()) {
202 i -= 1;
203 continue;
204 }
205 }
206 mat.mapVectors(vectors, SK_ARRAY_COUNT(vectors));
tomhudson@google.com83a44462011-10-27 15:27:51 +0000207 for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
bsalomon@google.com38396322011-09-09 19:32:04 +0000208 SkScalar d = vectors[i].length();
209 REPORTER_ASSERT(reporter, SkScalarDiv(d, stretch) < gStretchTol);
210 if (max < d) {
211 max = d;
212 }
213 }
214 REPORTER_ASSERT(reporter, SkScalarDiv(max, stretch) >= gMaxStretchTol);
215 }
216}
217
bsalomon@google.com69afee12012-04-25 15:07:40 +0000218// This function is extracted from src/gpu/SkGpuDevice.cpp,
219// in order to make sure this function works correctly.
220static bool isSimilarityTransformation(const SkMatrix& matrix,
221 SkScalar tol = SK_ScalarNearlyZero) {
222 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
223 return true;
224 }
225 if (matrix.hasPerspective()) {
226 return false;
227 }
228
229 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
230 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
231 SkScalar my = matrix.get(SkMatrix::kMScaleY);
232 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
233
234 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
235 return false;
236 }
237
238 // it has scales or skews, but it could also be rotation, check it out.
239 SkVector vec[2];
240 vec[0].set(mx, sx);
241 vec[1].set(sy, my);
242
243 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
244 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
245 SkScalarSquare(tol));
246}
247
caryclark@google.com42639cd2012-06-06 12:03:39 +0000248static void test_matrix_is_similarity_transform(skiatest::Reporter* reporter) {
bsalomon@google.com69afee12012-04-25 15:07:40 +0000249 SkMatrix mat;
250
251 // identity
252 mat.setIdentity();
253 REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
254
255 // translation only
256 mat.reset();
257 mat.setTranslate(SkIntToScalar(100), SkIntToScalar(100));
258 REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
259
260 // scale with same size
261 mat.reset();
262 mat.setScale(SkIntToScalar(15), SkIntToScalar(15));
263 REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
264
265 // scale with one negative
266 mat.reset();
267 mat.setScale(SkIntToScalar(-15), SkIntToScalar(15));
268 REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
269
270 // scale with different size
271 mat.reset();
272 mat.setScale(SkIntToScalar(15), SkIntToScalar(20));
273 REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
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, isSimilarityTransformation(mat));
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, !isSimilarityTransformation(mat));
286
287 // skew with same size
288 mat.reset();
289 mat.setSkew(SkIntToScalar(15), SkIntToScalar(15));
290 REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
291
292 // skew with different size
293 mat.reset();
294 mat.setSkew(SkIntToScalar(15), SkIntToScalar(20));
295 REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
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, !isSimilarityTransformation(mat));
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, !isSimilarityTransformation(mat));
308
309 // perspective x
310 mat.reset();
311 mat.setPerspX(SkScalarToPersp(SK_Scalar1 / 2));
312 REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
313
314 // perspective y
315 mat.reset();
316 mat.setPerspY(SkScalarToPersp(SK_Scalar1 / 2));
317 REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
318
319#if SK_SCALAR_IS_FLOAT
320 /* We bypass the following tests for SK_SCALAR_IS_FIXED build.
321 * The long discussion can be found in this issue:
322 * http://codereview.appspot.com/5999050/
323 * In short, we haven't found a perfect way to fix the precision
324 * issue, i.e. the way we use tolerance in isSimilarityTransformation
325 * is incorrect. The situation becomes worse in fixed build, so
326 * we disabled rotation related tests for fixed build.
327 */
328
329 // rotate
330 for (int angle = 0; angle < 360; ++angle) {
331 mat.reset();
332 mat.setRotate(SkIntToScalar(angle));
333 REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
334 }
335
336 // see if there are any accumulated precision issues
337 mat.reset();
338 for (int i = 1; i < 360; i++) {
339 mat.postRotate(SkIntToScalar(1));
340 }
341 REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
342
343 // rotate + translate
344 mat.reset();
345 mat.setRotate(SkIntToScalar(30));
346 mat.postTranslate(SkIntToScalar(10), SkIntToScalar(20));
347 REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
348
349 // rotate + uniform scale
350 mat.reset();
351 mat.setRotate(SkIntToScalar(30));
352 mat.postScale(SkIntToScalar(2), SkIntToScalar(2));
353 REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
354
355 // rotate + non-uniform scale
356 mat.reset();
357 mat.setRotate(SkIntToScalar(30));
358 mat.postScale(SkIntToScalar(3), SkIntToScalar(2));
359 REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
360#endif
361
362 // all zero
363 mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, 0);
364 REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
365
366 // all zero except perspective
367 mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, SK_Scalar1);
368 REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
369
370 // scales zero, only skews
371 mat.setAll(0, SK_Scalar1, 0,
372 SK_Scalar1, 0, 0,
373 0, 0, SkMatrix::I()[8]);
374 REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
375}
376
caryclark@google.com42639cd2012-06-06 12:03:39 +0000377static void TestMatrix(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000378 SkMatrix mat, inverse, iden1, iden2;
379
380 mat.reset();
381 mat.setTranslate(SK_Scalar1, SK_Scalar1);
reed@google.com5bfa55b2012-04-19 18:59:25 +0000382 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000383 iden1.setConcat(mat, inverse);
384 REPORTER_ASSERT(reporter, is_identity(iden1));
385
386 mat.setScale(SkIntToScalar(2), SkIntToScalar(2));
reed@google.com5bfa55b2012-04-19 18:59:25 +0000387 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000388 iden1.setConcat(mat, inverse);
389 REPORTER_ASSERT(reporter, is_identity(iden1));
reed@android.com4b7577b2009-06-29 16:14:41 +0000390 test_flatten(reporter, mat);
reed@android.comed673312009-02-27 16:24:51 +0000391
392 mat.setScale(SK_Scalar1/2, SK_Scalar1/2);
reed@google.com5bfa55b2012-04-19 18:59:25 +0000393 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000394 iden1.setConcat(mat, inverse);
395 REPORTER_ASSERT(reporter, is_identity(iden1));
reed@android.com4b7577b2009-06-29 16:14:41 +0000396 test_flatten(reporter, mat);
reed@android.comed673312009-02-27 16:24:51 +0000397
398 mat.setScale(SkIntToScalar(3), SkIntToScalar(5), SkIntToScalar(20), 0);
399 mat.postRotate(SkIntToScalar(25));
400 REPORTER_ASSERT(reporter, mat.invert(NULL));
reed@google.com5bfa55b2012-04-19 18:59:25 +0000401 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000402 iden1.setConcat(mat, inverse);
403 REPORTER_ASSERT(reporter, is_identity(iden1));
404 iden2.setConcat(inverse, mat);
405 REPORTER_ASSERT(reporter, is_identity(iden2));
reed@android.com4b7577b2009-06-29 16:14:41 +0000406 test_flatten(reporter, mat);
407 test_flatten(reporter, iden2);
reed@android.com80e39a72009-04-02 16:59:40 +0000408
reed@android.comed673312009-02-27 16:24:51 +0000409 // rectStaysRect test
410 {
411 static const struct {
412 SkScalar m00, m01, m10, m11;
413 bool mStaysRect;
414 }
415 gRectStaysRectSamples[] = {
416 { 0, 0, 0, 0, false },
417 { 0, 0, 0, SK_Scalar1, false },
418 { 0, 0, SK_Scalar1, 0, false },
419 { 0, 0, SK_Scalar1, SK_Scalar1, false },
420 { 0, SK_Scalar1, 0, 0, false },
421 { 0, SK_Scalar1, 0, SK_Scalar1, false },
422 { 0, SK_Scalar1, SK_Scalar1, 0, true },
423 { 0, SK_Scalar1, SK_Scalar1, SK_Scalar1, false },
424 { SK_Scalar1, 0, 0, 0, false },
425 { SK_Scalar1, 0, 0, SK_Scalar1, true },
426 { SK_Scalar1, 0, SK_Scalar1, 0, false },
427 { SK_Scalar1, 0, SK_Scalar1, SK_Scalar1, false },
428 { SK_Scalar1, SK_Scalar1, 0, 0, false },
429 { SK_Scalar1, SK_Scalar1, 0, SK_Scalar1, false },
430 { SK_Scalar1, SK_Scalar1, SK_Scalar1, 0, false },
431 { SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1, false }
432 };
reed@android.com80e39a72009-04-02 16:59:40 +0000433
reed@android.comed673312009-02-27 16:24:51 +0000434 for (size_t i = 0; i < SK_ARRAY_COUNT(gRectStaysRectSamples); i++) {
435 SkMatrix m;
reed@android.com80e39a72009-04-02 16:59:40 +0000436
reed@android.comed673312009-02-27 16:24:51 +0000437 m.reset();
438 m.set(SkMatrix::kMScaleX, gRectStaysRectSamples[i].m00);
439 m.set(SkMatrix::kMSkewX, gRectStaysRectSamples[i].m01);
440 m.set(SkMatrix::kMSkewY, gRectStaysRectSamples[i].m10);
441 m.set(SkMatrix::kMScaleY, gRectStaysRectSamples[i].m11);
442 REPORTER_ASSERT(reporter,
443 m.rectStaysRect() == gRectStaysRectSamples[i].mStaysRect);
444 }
445 }
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000446
bungeman@google.comba7983e2011-07-13 20:18:16 +0000447 mat.reset();
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000448 mat.set(SkMatrix::kMScaleX, SkIntToScalar(1));
449 mat.set(SkMatrix::kMSkewX, SkIntToScalar(2));
450 mat.set(SkMatrix::kMTransX, SkIntToScalar(3));
451 mat.set(SkMatrix::kMSkewY, SkIntToScalar(4));
452 mat.set(SkMatrix::kMScaleY, SkIntToScalar(5));
453 mat.set(SkMatrix::kMTransY, SkIntToScalar(6));
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000454 SkScalar affine[6];
455 REPORTER_ASSERT(reporter, mat.asAffine(affine));
456
457 #define affineEqual(e) affine[SkMatrix::kA##e] == mat.get(SkMatrix::kM##e)
458 REPORTER_ASSERT(reporter, affineEqual(ScaleX));
459 REPORTER_ASSERT(reporter, affineEqual(SkewY));
460 REPORTER_ASSERT(reporter, affineEqual(SkewX));
461 REPORTER_ASSERT(reporter, affineEqual(ScaleY));
462 REPORTER_ASSERT(reporter, affineEqual(TransX));
463 REPORTER_ASSERT(reporter, affineEqual(TransY));
464 #undef affineEqual
465
bungeman@google.com07faed12011-10-07 21:55:56 +0000466 mat.set(SkMatrix::kMPersp1, SkScalarToPersp(SK_Scalar1 / 2));
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000467 REPORTER_ASSERT(reporter, !mat.asAffine(affine));
bsalomon@google.com38396322011-09-09 19:32:04 +0000468
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000469 SkMatrix mat2;
470 mat2.reset();
471 mat.reset();
472 SkScalar zero = 0;
473 mat.set(SkMatrix::kMSkewX, -zero);
474 REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2));
475
476 mat2.reset();
477 mat.reset();
478 mat.set(SkMatrix::kMSkewX, SK_ScalarNaN);
479 mat2.set(SkMatrix::kMSkewX, SK_ScalarNaN);
bsalomon@google.com9ed2ecd2012-03-26 15:57:37 +0000480 // fixed pt doesn't have the property that NaN does not equal itself.
481#ifdef SK_SCALAR_IS_FIXED
482 REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2));
483#else
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000484 REPORTER_ASSERT(reporter, !are_equal(reporter, mat, mat2));
bsalomon@google.com9ed2ecd2012-03-26 15:57:37 +0000485#endif
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000486
bsalomon@google.com38396322011-09-09 19:32:04 +0000487 test_matrix_max_stretch(reporter);
bsalomon@google.com69afee12012-04-25 15:07:40 +0000488 test_matrix_is_similarity_transform(reporter);
reed@google.com97cd69c2012-10-12 14:35:48 +0000489 test_matrix_recttorect(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000490}
491
reed@android.comd8730ea2009-02-27 22:06:06 +0000492#include "TestClassDef.h"
493DEFINE_TESTCLASS("Matrix", MatrixTestClass, TestMatrix)