Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 0f1f87d

Browse files
reed-at-googleSkia Commit-Bot
authored andcommitted
transition matrix44 to opt-in only
In service of https://chromium-review.googlesource.com/c/chromium/src/+/2067862/ Change-Id: Ib6fd24c16c295fb4211dc295268af1e6f7f3fc45 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/283661 Reviewed-by: Florin Malita <fmalita@chromium.org> Commit-Queue: Mike Reed <reed@google.com>
1 parent 484b63b commit 0f1f87d

File tree

5 files changed

+197
-186
lines changed

5 files changed

+197
-186
lines changed

gn/tests.gni

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ tests_sources = [
142142
"$_tests/LRUCacheTest.cpp",
143143
"$_tests/LayerDrawLooperTest.cpp",
144144
"$_tests/LazyProxyTest.cpp",
145+
"$_tests/M44Test.cpp",
145146
"$_tests/MD5Test.cpp",
146147
"$_tests/MallocPixelRefTest.cpp",
147148
"$_tests/MaskCacheTest.cpp",

include/core/SkMatrix44.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include "include/core/SkMatrix.h"
1212
#include "include/core/SkScalar.h"
1313

14+
#ifdef SK_SUPPORT_LEGACY_MATRIX44
15+
1416
#include <atomic>
1517
#include <cstring>
1618

@@ -390,3 +392,4 @@ class SK_API SkMatrix44 {
390392
};
391393

392394
#endif
395+
#endif

src/core/SkMatrix44.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <type_traits>
1010
#include <utility>
1111

12+
#ifdef SK_SUPPORT_LEGACY_MATRIX44
13+
1214
// Copying SkMatrix44 byte-wise is performance-critical to Blink. This class is
1315
// contained in several Transform classes, which are copied multiple times
1416
// during the rendering life cycle. See crbug.com/938563 for reference.
@@ -997,3 +999,5 @@ SkMatrix44::operator SkMatrix() const {
997999

9981000
return dst;
9991001
}
1002+
1003+
#endif

tests/M44Test.cpp

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
* Copyright 2020 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 "include/core/SkM44.h"
9+
#include "tests/Test.h"
10+
11+
static bool eq(const SkM44& a, const SkM44& b, float tol) {
12+
float fa[16], fb[16];
13+
a.getColMajor(fa);
14+
b.getColMajor(fb);
15+
for (int i = 0; i < 16; ++i) {
16+
if (!SkScalarNearlyEqual(fa[i], fb[i], tol)) {
17+
return false;
18+
}
19+
}
20+
return true;
21+
}
22+
23+
DEF_TEST(M44, reporter) {
24+
SkM44 m, im;
25+
26+
REPORTER_ASSERT(reporter, SkM44(1, 0, 0, 0,
27+
0, 1, 0, 0,
28+
0, 0, 1, 0,
29+
0, 0, 0, 1) == m);
30+
REPORTER_ASSERT(reporter, SkM44() == m);
31+
REPORTER_ASSERT(reporter, m.invert(&im));
32+
REPORTER_ASSERT(reporter, SkM44() == im);
33+
34+
m.setTranslate(3, 4, 2);
35+
REPORTER_ASSERT(reporter, SkM44(1, 0, 0, 3,
36+
0, 1, 0, 4,
37+
0, 0, 1, 2,
38+
0, 0, 0, 1) == m);
39+
40+
const float f[] = { 1, 0, 0, 2, 3, 1, 2, 5, 0, 5, 3, 0, 0, 1, 0, 2 };
41+
m = SkM44::ColMajor(f);
42+
REPORTER_ASSERT(reporter, SkM44(f[0], f[4], f[ 8], f[12],
43+
f[1], f[5], f[ 9], f[13],
44+
f[2], f[6], f[10], f[14],
45+
f[3], f[7], f[11], f[15]) == m);
46+
47+
{
48+
SkM44 t = m.transpose();
49+
REPORTER_ASSERT(reporter, t != m);
50+
REPORTER_ASSERT(reporter, t.rc(1,0) == m.rc(0,1));
51+
SkM44 tt = t.transpose();
52+
REPORTER_ASSERT(reporter, tt == m);
53+
}
54+
55+
m = SkM44::RowMajor(f);
56+
REPORTER_ASSERT(reporter, SkM44(f[ 0], f[ 1], f[ 2], f[ 3],
57+
f[ 4], f[ 5], f[ 6], f[ 7],
58+
f[ 8], f[ 9], f[10], f[14],
59+
f[12], f[13], f[14], f[15]) == m);
60+
61+
REPORTER_ASSERT(reporter, m.invert(&im));
62+
63+
m = m * im;
64+
// m should be identity now, but our calc is not perfect...
65+
REPORTER_ASSERT(reporter, eq(SkM44(), m, 0.0000005f));
66+
REPORTER_ASSERT(reporter, SkM44() != m);
67+
}
68+
69+
DEF_TEST(M44_v3, reporter) {
70+
SkV3 a = {1, 2, 3},
71+
b = {1, 2, 2};
72+
73+
REPORTER_ASSERT(reporter, a.lengthSquared() == 1 + 4 + 9);
74+
REPORTER_ASSERT(reporter, b.length() == 3);
75+
REPORTER_ASSERT(reporter, a.dot(b) == 1 + 4 + 6);
76+
REPORTER_ASSERT(reporter, b.dot(a) == 1 + 4 + 6);
77+
REPORTER_ASSERT(reporter, (a.cross(b) == SkV3{-2, 1, 0}));
78+
REPORTER_ASSERT(reporter, (b.cross(a) == SkV3{ 2, -1, 0}));
79+
80+
SkM44 m = {
81+
2, 0, 0, 3,
82+
0, 1, 0, 5,
83+
0, 0, 3, 1,
84+
0, 0, 0, 1
85+
};
86+
87+
SkV3 c = m * a;
88+
REPORTER_ASSERT(reporter, (c == SkV3{2, 2, 9}));
89+
SkV4 d = m.map(4, 3, 2, 1);
90+
REPORTER_ASSERT(reporter, (d == SkV4{11, 8, 7, 1}));
91+
}
92+
93+
DEF_TEST(M44_v4, reporter) {
94+
SkM44 m( 1, 2, 3, 4,
95+
5, 6, 7, 8,
96+
9, 10, 11, 12,
97+
13, 14, 15, 16);
98+
99+
SkV4 r0 = m.row(0),
100+
r1 = m.row(1),
101+
r2 = m.row(2),
102+
r3 = m.row(3);
103+
104+
REPORTER_ASSERT(reporter, (r0 == SkV4{ 1, 2, 3, 4}));
105+
REPORTER_ASSERT(reporter, (r1 == SkV4{ 5, 6, 7, 8}));
106+
REPORTER_ASSERT(reporter, (r2 == SkV4{ 9, 10, 11, 12}));
107+
REPORTER_ASSERT(reporter, (r3 == SkV4{13, 14, 15, 16}));
108+
109+
REPORTER_ASSERT(reporter, SkM44::Rows(r0, r1, r2, r3) == m);
110+
111+
SkV4 c0 = m.col(0),
112+
c1 = m.col(1),
113+
c2 = m.col(2),
114+
c3 = m.col(3);
115+
116+
REPORTER_ASSERT(reporter, (c0 == SkV4{1, 5, 9, 13}));
117+
REPORTER_ASSERT(reporter, (c1 == SkV4{2, 6, 10, 14}));
118+
REPORTER_ASSERT(reporter, (c2 == SkV4{3, 7, 11, 15}));
119+
REPORTER_ASSERT(reporter, (c3 == SkV4{4, 8, 12, 16}));
120+
121+
REPORTER_ASSERT(reporter, SkM44::Cols(c0, c1, c2, c3) == m);
122+
123+
// implement matrix * vector using column vectors
124+
SkV4 v = {1, 2, 3, 4};
125+
SkV4 v1 = m * v;
126+
SkV4 v2 = c0 * v.x + c1 * v.y + c2 * v.z + c3 * v.w;
127+
REPORTER_ASSERT(reporter, v1 == v2);
128+
129+
REPORTER_ASSERT(reporter, (c0 + r0 == SkV4{c0.x+r0.x, c0.y+r0.y, c0.z+r0.z, c0.w+r0.w}));
130+
REPORTER_ASSERT(reporter, (c0 - r0 == SkV4{c0.x-r0.x, c0.y-r0.y, c0.z-r0.z, c0.w-r0.w}));
131+
REPORTER_ASSERT(reporter, (c0 * r0 == SkV4{c0.x*r0.x, c0.y*r0.y, c0.z*r0.z, c0.w*r0.w}));
132+
}
133+
134+
DEF_TEST(M44_rotate, reporter) {
135+
const SkV3 x = {1, 0, 0},
136+
y = {0, 1, 0},
137+
z = {0, 0, 1};
138+
139+
// We have radians version of setRotateAbout methods, but even with our best approx
140+
// for PI, sin(SK_ScalarPI) != 0, so to make the comparisons in the unittest clear,
141+
// I'm using the variants that explicitly take the sin,cos values.
142+
143+
struct {
144+
SkScalar sinAngle, cosAngle;
145+
SkV3 aboutAxis;
146+
SkV3 expectedX, expectedY, expectedZ;
147+
} recs[] = {
148+
{ 0, 1, x, x, y, z}, // angle = 0
149+
{ 0, 1, y, x, y, z}, // angle = 0
150+
{ 0, 1, z, x, y, z}, // angle = 0
151+
152+
{ 0,-1, x, x,-y,-z}, // angle = 180
153+
{ 0,-1, y, -x, y,-z}, // angle = 180
154+
{ 0,-1, z, -x,-y, z}, // angle = 180
155+
156+
// Skia coordinate system is right-handed
157+
158+
{ 1, 0, x, x, z,-y}, // angle = 90
159+
{ 1, 0, y, -z, y, x}, // angle = 90
160+
{ 1, 0, z, y,-x, z}, // angle = 90
161+
162+
{-1, 0, x, x,-z, y}, // angle = -90
163+
{-1, 0, y, z, y,-x}, // angle = -90
164+
{-1, 0, z, -y, x, z}, // angle = -90
165+
};
166+
167+
for (const auto& r : recs) {
168+
SkM44 m(SkM44::kNaN_Constructor);
169+
m.setRotateUnitSinCos(r.aboutAxis, r.sinAngle, r.cosAngle);
170+
171+
auto mx = m * x;
172+
auto my = m * y;
173+
auto mz = m * z;
174+
REPORTER_ASSERT(reporter, mx == r.expectedX);
175+
REPORTER_ASSERT(reporter, my == r.expectedY);
176+
REPORTER_ASSERT(reporter, mz == r.expectedZ);
177+
178+
// flipping the axis-of-rotation should flip the results
179+
mx = m * -x;
180+
my = m * -y;
181+
mz = m * -z;
182+
REPORTER_ASSERT(reporter, mx == -r.expectedX);
183+
REPORTER_ASSERT(reporter, my == -r.expectedY);
184+
REPORTER_ASSERT(reporter, mz == -r.expectedZ);
185+
}
186+
}

0 commit comments

Comments
 (0)