blob: 894278e84b3c24e37bf7d86cf2cad8d86bc364ff [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);
skia.committer@gmail.come659c2e2012-12-04 02:01:25 +0000101 REPORTER_ASSERT(reporter,
robertphillips@google.com93f03322012-12-03 17:35:19 +0000102 (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask) == matrix.getType());
reed@google.com97cd69c2012-10-12 14:35:48 +0000103 REPORTER_ASSERT(reporter, matrix.rectStaysRect());
104
105 dst = src;
106 dst.fRight = src.fRight * 2;
107 matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
108 REPORTER_ASSERT(reporter, SkMatrix::kScale_Mask == matrix.getType());
109 REPORTER_ASSERT(reporter, matrix.rectStaysRect());
110}
111
reed@android.com4b7577b2009-06-29 16:14:41 +0000112static void test_flatten(skiatest::Reporter* reporter, const SkMatrix& m) {
113 // add 100 in case we have a bug, I don't want to kill my stack in the test
114 char buffer[SkMatrix::kMaxFlattenSize + 100];
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000115 uint32_t size1 = m.writeToMemory(NULL);
116 uint32_t size2 = m.writeToMemory(buffer);
reed@android.com4b7577b2009-06-29 16:14:41 +0000117 REPORTER_ASSERT(reporter, size1 == size2);
118 REPORTER_ASSERT(reporter, size1 <= SkMatrix::kMaxFlattenSize);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000119
reed@android.com4b7577b2009-06-29 16:14:41 +0000120 SkMatrix m2;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000121 uint32_t size3 = m2.readFromMemory(buffer);
122 REPORTER_ASSERT(reporter, size1 == size3);
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000123 REPORTER_ASSERT(reporter, are_equal(reporter, m, m2));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000124
reed@android.com4b7577b2009-06-29 16:14:41 +0000125 char buffer2[SkMatrix::kMaxFlattenSize + 100];
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000126 size3 = m2.writeToMemory(buffer2);
127 REPORTER_ASSERT(reporter, size1 == size3);
reed@android.com4b7577b2009-06-29 16:14:41 +0000128 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
129}
130
caryclark@google.com42639cd2012-06-06 12:03:39 +0000131static void test_matrix_max_stretch(skiatest::Reporter* reporter) {
bsalomon@google.com38396322011-09-09 19:32:04 +0000132 SkMatrix identity;
133 identity.reset();
134 REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMaxStretch());
135
136 SkMatrix scale;
137 scale.setScale(SK_Scalar1 * 2, SK_Scalar1 * 4);
138 REPORTER_ASSERT(reporter, SK_Scalar1 * 4 == scale.getMaxStretch());
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 / 2 == rot90Scale.getMaxStretch());
144
145 SkMatrix rotate;
146 rotate.setRotate(128 * SK_Scalar1);
147 REPORTER_ASSERT(reporter, SkScalarAbs(SK_Scalar1 - rotate.getMaxStretch()) <= SK_ScalarNearlyZero);
148
149 SkMatrix translate;
150 translate.setTranslate(10 * SK_Scalar1, -5 * SK_Scalar1);
151 REPORTER_ASSERT(reporter, SK_Scalar1 == translate.getMaxStretch());
152
153 SkMatrix perspX;
154 perspX.reset();
bungeman@google.com07faed12011-10-07 21:55:56 +0000155 perspX.setPerspX(SkScalarToPersp(SK_Scalar1 / 1000));
bsalomon@google.com38396322011-09-09 19:32:04 +0000156 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMaxStretch());
157
158 SkMatrix perspY;
159 perspY.reset();
bungeman@google.com07faed12011-10-07 21:55:56 +0000160 perspY.setPerspX(SkScalarToPersp(-SK_Scalar1 / 500));
bsalomon@google.com38396322011-09-09 19:32:04 +0000161 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMaxStretch());
162
163 SkMatrix baseMats[] = {scale, rot90Scale, rotate,
164 translate, perspX, perspY};
165 SkMatrix mats[2*SK_ARRAY_COUNT(baseMats)];
tomhudson@google.com83a44462011-10-27 15:27:51 +0000166 for (size_t i = 0; i < SK_ARRAY_COUNT(baseMats); ++i) {
bsalomon@google.com38396322011-09-09 19:32:04 +0000167 mats[i] = baseMats[i];
168 bool invertable = mats[i].invert(&mats[i + SK_ARRAY_COUNT(baseMats)]);
169 REPORTER_ASSERT(reporter, invertable);
170 }
171 SkRandom rand;
172 for (int m = 0; m < 1000; ++m) {
173 SkMatrix mat;
174 mat.reset();
175 for (int i = 0; i < 4; ++i) {
176 int x = rand.nextU() % SK_ARRAY_COUNT(mats);
177 mat.postConcat(mats[x]);
178 }
179 SkScalar stretch = mat.getMaxStretch();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000180
bsalomon@google.com38396322011-09-09 19:32:04 +0000181 if ((stretch < 0) != mat.hasPerspective()) {
182 stretch = mat.getMaxStretch();
183 }
184
185 REPORTER_ASSERT(reporter, (stretch < 0) == mat.hasPerspective());
186
187 if (mat.hasPerspective()) {
188 m -= 1; // try another non-persp matrix
189 continue;
190 }
191
192 // test a bunch of vectors. None should be scaled by more than stretch
193 // (modulo some error) and we should find a vector that is scaled by
194 // almost stretch.
195 static const SkScalar gStretchTol = (105 * SK_Scalar1) / 100;
196 static const SkScalar gMaxStretchTol = (97 * SK_Scalar1) / 100;
197 SkScalar max = 0;
198 SkVector vectors[1000];
tomhudson@google.com83a44462011-10-27 15:27:51 +0000199 for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
bsalomon@google.com38396322011-09-09 19:32:04 +0000200 vectors[i].fX = rand.nextSScalar1();
201 vectors[i].fY = rand.nextSScalar1();
202 if (!vectors[i].normalize()) {
203 i -= 1;
204 continue;
205 }
206 }
207 mat.mapVectors(vectors, SK_ARRAY_COUNT(vectors));
tomhudson@google.com83a44462011-10-27 15:27:51 +0000208 for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
bsalomon@google.com38396322011-09-09 19:32:04 +0000209 SkScalar d = vectors[i].length();
210 REPORTER_ASSERT(reporter, SkScalarDiv(d, stretch) < gStretchTol);
211 if (max < d) {
212 max = d;
213 }
214 }
215 REPORTER_ASSERT(reporter, SkScalarDiv(max, stretch) >= gMaxStretchTol);
216 }
217}
218
bsalomon@google.com69afee12012-04-25 15:07:40 +0000219// This function is extracted from src/gpu/SkGpuDevice.cpp,
220// in order to make sure this function works correctly.
221static bool isSimilarityTransformation(const SkMatrix& matrix,
222 SkScalar tol = SK_ScalarNearlyZero) {
223 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
224 return true;
225 }
226 if (matrix.hasPerspective()) {
227 return false;
228 }
229
230 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
231 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
232 SkScalar my = matrix.get(SkMatrix::kMScaleY);
233 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
234
235 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
236 return false;
237 }
238
239 // it has scales or skews, but it could also be rotation, check it out.
240 SkVector vec[2];
241 vec[0].set(mx, sx);
242 vec[1].set(sy, my);
243
244 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
245 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
246 SkScalarSquare(tol));
247}
248
caryclark@google.com42639cd2012-06-06 12:03:39 +0000249static void test_matrix_is_similarity_transform(skiatest::Reporter* reporter) {
bsalomon@google.com69afee12012-04-25 15:07:40 +0000250 SkMatrix mat;
251
252 // identity
253 mat.setIdentity();
254 REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
255
256 // translation only
257 mat.reset();
258 mat.setTranslate(SkIntToScalar(100), SkIntToScalar(100));
259 REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
260
261 // scale with same size
262 mat.reset();
263 mat.setScale(SkIntToScalar(15), SkIntToScalar(15));
264 REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
265
266 // scale with one negative
267 mat.reset();
268 mat.setScale(SkIntToScalar(-15), SkIntToScalar(15));
269 REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
270
271 // scale with different size
272 mat.reset();
273 mat.setScale(SkIntToScalar(15), SkIntToScalar(20));
274 REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
275
276 // scale with same size at a pivot point
277 mat.reset();
278 mat.setScale(SkIntToScalar(15), SkIntToScalar(15),
279 SkIntToScalar(2), SkIntToScalar(2));
280 REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
281
282 // scale with different size at a pivot point
283 mat.reset();
284 mat.setScale(SkIntToScalar(15), SkIntToScalar(20),
285 SkIntToScalar(2), SkIntToScalar(2));
286 REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
287
288 // skew with same size
289 mat.reset();
290 mat.setSkew(SkIntToScalar(15), SkIntToScalar(15));
291 REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
292
293 // skew with different size
294 mat.reset();
295 mat.setSkew(SkIntToScalar(15), SkIntToScalar(20));
296 REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
297
298 // skew with same size at a pivot point
299 mat.reset();
300 mat.setSkew(SkIntToScalar(15), SkIntToScalar(15),
301 SkIntToScalar(2), SkIntToScalar(2));
302 REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
303
304 // skew with different size at a pivot point
305 mat.reset();
306 mat.setSkew(SkIntToScalar(15), SkIntToScalar(20),
307 SkIntToScalar(2), SkIntToScalar(2));
308 REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
309
310 // perspective x
311 mat.reset();
312 mat.setPerspX(SkScalarToPersp(SK_Scalar1 / 2));
313 REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
314
315 // perspective y
316 mat.reset();
317 mat.setPerspY(SkScalarToPersp(SK_Scalar1 / 2));
318 REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
319
320#if SK_SCALAR_IS_FLOAT
321 /* We bypass the following tests for SK_SCALAR_IS_FIXED build.
322 * The long discussion can be found in this issue:
323 * http://codereview.appspot.com/5999050/
324 * In short, we haven't found a perfect way to fix the precision
325 * issue, i.e. the way we use tolerance in isSimilarityTransformation
326 * is incorrect. The situation becomes worse in fixed build, so
327 * we disabled rotation related tests for fixed build.
328 */
329
330 // rotate
331 for (int angle = 0; angle < 360; ++angle) {
332 mat.reset();
333 mat.setRotate(SkIntToScalar(angle));
334 REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
335 }
336
337 // see if there are any accumulated precision issues
338 mat.reset();
339 for (int i = 1; i < 360; i++) {
340 mat.postRotate(SkIntToScalar(1));
341 }
342 REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
343
344 // rotate + translate
345 mat.reset();
346 mat.setRotate(SkIntToScalar(30));
347 mat.postTranslate(SkIntToScalar(10), SkIntToScalar(20));
348 REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
349
350 // rotate + uniform scale
351 mat.reset();
352 mat.setRotate(SkIntToScalar(30));
353 mat.postScale(SkIntToScalar(2), SkIntToScalar(2));
354 REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
355
356 // rotate + non-uniform scale
357 mat.reset();
358 mat.setRotate(SkIntToScalar(30));
359 mat.postScale(SkIntToScalar(3), SkIntToScalar(2));
360 REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
361#endif
362
363 // all zero
364 mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, 0);
365 REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
366
367 // all zero except perspective
368 mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, SK_Scalar1);
369 REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
370
371 // scales zero, only skews
372 mat.setAll(0, SK_Scalar1, 0,
373 SK_Scalar1, 0, 0,
374 0, 0, SkMatrix::I()[8]);
375 REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
376}
377
caryclark@google.com42639cd2012-06-06 12:03:39 +0000378static void TestMatrix(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000379 SkMatrix mat, inverse, iden1, iden2;
380
381 mat.reset();
382 mat.setTranslate(SK_Scalar1, SK_Scalar1);
reed@google.com5bfa55b2012-04-19 18:59:25 +0000383 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000384 iden1.setConcat(mat, inverse);
385 REPORTER_ASSERT(reporter, is_identity(iden1));
386
387 mat.setScale(SkIntToScalar(2), SkIntToScalar(2));
reed@google.com5bfa55b2012-04-19 18:59:25 +0000388 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000389 iden1.setConcat(mat, inverse);
390 REPORTER_ASSERT(reporter, is_identity(iden1));
reed@android.com4b7577b2009-06-29 16:14:41 +0000391 test_flatten(reporter, mat);
reed@android.comed673312009-02-27 16:24:51 +0000392
393 mat.setScale(SK_Scalar1/2, SK_Scalar1/2);
reed@google.com5bfa55b2012-04-19 18:59:25 +0000394 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000395 iden1.setConcat(mat, inverse);
396 REPORTER_ASSERT(reporter, is_identity(iden1));
reed@android.com4b7577b2009-06-29 16:14:41 +0000397 test_flatten(reporter, mat);
reed@android.comed673312009-02-27 16:24:51 +0000398
399 mat.setScale(SkIntToScalar(3), SkIntToScalar(5), SkIntToScalar(20), 0);
400 mat.postRotate(SkIntToScalar(25));
401 REPORTER_ASSERT(reporter, mat.invert(NULL));
reed@google.com5bfa55b2012-04-19 18:59:25 +0000402 REPORTER_ASSERT(reporter, mat.invert(&inverse));
reed@android.comed673312009-02-27 16:24:51 +0000403 iden1.setConcat(mat, inverse);
404 REPORTER_ASSERT(reporter, is_identity(iden1));
405 iden2.setConcat(inverse, mat);
406 REPORTER_ASSERT(reporter, is_identity(iden2));
reed@android.com4b7577b2009-06-29 16:14:41 +0000407 test_flatten(reporter, mat);
408 test_flatten(reporter, iden2);
reed@android.com80e39a72009-04-02 16:59:40 +0000409
reed@android.comed673312009-02-27 16:24:51 +0000410 // rectStaysRect test
411 {
412 static const struct {
413 SkScalar m00, m01, m10, m11;
414 bool mStaysRect;
415 }
416 gRectStaysRectSamples[] = {
417 { 0, 0, 0, 0, false },
418 { 0, 0, 0, SK_Scalar1, false },
419 { 0, 0, SK_Scalar1, 0, false },
420 { 0, 0, SK_Scalar1, SK_Scalar1, false },
421 { 0, SK_Scalar1, 0, 0, false },
422 { 0, SK_Scalar1, 0, SK_Scalar1, false },
423 { 0, SK_Scalar1, SK_Scalar1, 0, true },
424 { 0, SK_Scalar1, SK_Scalar1, SK_Scalar1, false },
425 { SK_Scalar1, 0, 0, 0, false },
426 { SK_Scalar1, 0, 0, SK_Scalar1, true },
427 { SK_Scalar1, 0, SK_Scalar1, 0, false },
428 { SK_Scalar1, 0, SK_Scalar1, SK_Scalar1, false },
429 { SK_Scalar1, SK_Scalar1, 0, 0, false },
430 { SK_Scalar1, SK_Scalar1, 0, SK_Scalar1, false },
431 { SK_Scalar1, SK_Scalar1, SK_Scalar1, 0, false },
432 { SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1, false }
433 };
reed@android.com80e39a72009-04-02 16:59:40 +0000434
reed@android.comed673312009-02-27 16:24:51 +0000435 for (size_t i = 0; i < SK_ARRAY_COUNT(gRectStaysRectSamples); i++) {
436 SkMatrix m;
reed@android.com80e39a72009-04-02 16:59:40 +0000437
reed@android.comed673312009-02-27 16:24:51 +0000438 m.reset();
439 m.set(SkMatrix::kMScaleX, gRectStaysRectSamples[i].m00);
440 m.set(SkMatrix::kMSkewX, gRectStaysRectSamples[i].m01);
441 m.set(SkMatrix::kMSkewY, gRectStaysRectSamples[i].m10);
442 m.set(SkMatrix::kMScaleY, gRectStaysRectSamples[i].m11);
443 REPORTER_ASSERT(reporter,
444 m.rectStaysRect() == gRectStaysRectSamples[i].mStaysRect);
445 }
446 }
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000447
bungeman@google.comba7983e2011-07-13 20:18:16 +0000448 mat.reset();
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000449 mat.set(SkMatrix::kMScaleX, SkIntToScalar(1));
450 mat.set(SkMatrix::kMSkewX, SkIntToScalar(2));
451 mat.set(SkMatrix::kMTransX, SkIntToScalar(3));
452 mat.set(SkMatrix::kMSkewY, SkIntToScalar(4));
453 mat.set(SkMatrix::kMScaleY, SkIntToScalar(5));
454 mat.set(SkMatrix::kMTransY, SkIntToScalar(6));
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000455 SkScalar affine[6];
456 REPORTER_ASSERT(reporter, mat.asAffine(affine));
457
458 #define affineEqual(e) affine[SkMatrix::kA##e] == mat.get(SkMatrix::kM##e)
459 REPORTER_ASSERT(reporter, affineEqual(ScaleX));
460 REPORTER_ASSERT(reporter, affineEqual(SkewY));
461 REPORTER_ASSERT(reporter, affineEqual(SkewX));
462 REPORTER_ASSERT(reporter, affineEqual(ScaleY));
463 REPORTER_ASSERT(reporter, affineEqual(TransX));
464 REPORTER_ASSERT(reporter, affineEqual(TransY));
465 #undef affineEqual
466
bungeman@google.com07faed12011-10-07 21:55:56 +0000467 mat.set(SkMatrix::kMPersp1, SkScalarToPersp(SK_Scalar1 / 2));
bungeman@google.com1ddd7c32011-07-13 19:41:55 +0000468 REPORTER_ASSERT(reporter, !mat.asAffine(affine));
bsalomon@google.com38396322011-09-09 19:32:04 +0000469
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000470 SkMatrix mat2;
471 mat2.reset();
472 mat.reset();
473 SkScalar zero = 0;
474 mat.set(SkMatrix::kMSkewX, -zero);
475 REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2));
476
477 mat2.reset();
478 mat.reset();
479 mat.set(SkMatrix::kMSkewX, SK_ScalarNaN);
480 mat2.set(SkMatrix::kMSkewX, SK_ScalarNaN);
bsalomon@google.com9ed2ecd2012-03-26 15:57:37 +0000481 // fixed pt doesn't have the property that NaN does not equal itself.
482#ifdef SK_SCALAR_IS_FIXED
483 REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2));
484#else
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000485 REPORTER_ASSERT(reporter, !are_equal(reporter, mat, mat2));
bsalomon@google.com9ed2ecd2012-03-26 15:57:37 +0000486#endif
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000487
bsalomon@google.com38396322011-09-09 19:32:04 +0000488 test_matrix_max_stretch(reporter);
bsalomon@google.com69afee12012-04-25 15:07:40 +0000489 test_matrix_is_similarity_transform(reporter);
reed@google.com97cd69c2012-10-12 14:35:48 +0000490 test_matrix_recttorect(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000491}
492
reed@android.comd8730ea2009-02-27 22:06:06 +0000493#include "TestClassDef.h"
494DEFINE_TESTCLASS("Matrix", MatrixTestClass, TestMatrix)