Flutter Engine
The Flutter Engine
MatrixProcsTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2013 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 "tests/Test.h"
9
14
15#include <cstddef>
16#include <cstdint>
17#include <string>
18
19uint32_t highBits(uint32_t rv) {
20 return (rv >> 18) & ((1 << 14) - 1);
21}
22
23uint32_t middleBits(uint32_t rv) {
24 return (rv >> 14) & ((1 << 4) - 1);
25}
26
27uint32_t lowBits(uint32_t rv) {
28 return rv & ((1 << 14) - 1);
29}
30
31DEF_TEST(MatrixProcs_pack_clamp, r) {
32 struct TestCase {
33 std::string name;
34 SkFixed input;
35 unsigned max;
36 uint32_t expectedOutput;
37 };
38 // The input values are somewhat arbitrary, inspired by real-world values
39 // with some edge cases added as well.
40 TestCase tests[] = {
41 // Negative values keep the fractional part out of convenience, but it is effectively
42 // ignored later.
43 {"-2.100 => {0x00, 0xe, 0x00}", SkFloatToFixed(-2.100f), 63, 0x38000},
44 {"-1.900 => {0x00, 0x1, 0x00}", SkFloatToFixed(-1.900f), 63, 0x04000},
45 {"-0.500 => {0x00, 0x8, 0x00}", SkFloatToFixed(-0.500f), 63, 0x20000},
46 {"0.0000 => {0x00, 0x0, 0x01}", SkFloatToFixed(0.0000f), 63, 0x000001},
47 {"0.0416 => {0x00, 0x0, 0x01}", SkFloatToFixed(0.0416f), 63, 0x000001},
48 {"1.8583 => {0x01, 0xd, 0x02}", SkFloatToFixed(1.8583f), 63, 0x074002},
49 {"3.6749 => {0x03, 0xa, 0x04}", SkFloatToFixed(3.6749f), 63, 0x0e8004},
50 {"5.4916 => {0x05, 0x7, 0x06}", SkFloatToFixed(5.4916f), 63, 0x15c006},
51 {"7.3083 => {0x07, 0x4, 0x08}", SkFloatToFixed(7.3083f), 63, 0x1d0008},
52 {"9.0000 => {0x09, 0x0, 0x0a}", SkFloatToFixed(9.0000f), 63, 0x24000a},
53 {"50.000 => {0x32, 0x0, 0x33}", SkFloatToFixed(50.000f), 63, 0xc80033},
54 {"50.875 => {0x32, 0xe, 0x33}", SkFloatToFixed(50.875f), 63, 0xcb8033},
55 {"62.123 => {0x3e, 0x1, 0x3f}", SkFloatToFixed(62.123f), 63, 0xf8403f},
56 {"62.999 => {0x3e, 0xf, 0x3f}", SkFloatToFixed(62.999f), 63, 0xfbc03f},
57 {"63.000 => {0x3f, 0x0, 0x3f}", SkFloatToFixed(63.000f), 63, 0xfc003f},
58 // Similarly, overflow keeps the fractional part.
59 {"64.500 => {0x3f, 0x8, 0x3f}", SkFloatToFixed(64.500f), 63, 0xfe003f},
60 {"127.20 => {0x3f, 0x3, 0x3f}", SkFloatToFixed(127.20f), 63, 0xfcc03f},
61 // Maximum has changed from 63 to 256
62 {"64.510 => {0x40, 0x8, 0x41}", SkFloatToFixed(64.510f), 256, 0x1020041},
63 {"127.21 => {0x7f, 0x3, 0x80}", SkFloatToFixed(127.21f), 256, 0x1fcc080},
64 {"256.77 => {0x100, 0xc, 0x100}", SkFloatToFixed(256.77f), 256, 0x4030100},
65 {"10000. => {0x100, 0x0, 0x100}", SkFloatToFixed(10000.f), 256, 0x4000100},
66 };
67
68 constexpr size_t NUM_TESTS = sizeof(tests) / sizeof(TestCase);
69
70 for (size_t i = 0; i < NUM_TESTS; i++) {
71 TestCase tc = tests[i];
72 uint32_t rv = sktests::pack_clamp(tc.input, tc.max);
73 uint32_t exp = tc.expectedOutput;
74
75 REPORTER_ASSERT(r, rv == tc.expectedOutput,
76 "%s | %x != %x | {%x, %x, %x} != {%x, %x, %x}\n",
77 tc.name.c_str(), rv, exp,
79 highBits(exp), middleBits(exp), lowBits(exp));
80 }
81}
82
83DEF_TEST(MatrixProcs_pack_clamp_out_of_range, r) {
84 // See https://crbug.com/1357122
85 struct TestCase {
86 std::string name;
87 SkFixed input;
88 unsigned max;
89 uint32_t expectedOutput;
90 };
91
92 // None of these should crash or cause UBSAN errors, although if they were used in drawing,
93 // they might be incorrect due to packing 16 bits of SkFixed into 14 bits of space.
94 static constexpr unsigned MAX_PACKED_VALUE = (1 << 14) - 1;
95 TestCase tests[] = {
96 {"16000.42=>{0xff, 6, 0xff}", SkFloatToFixed(16000.42f), 255, 0x3fd80ff},
97 {"17000.42=>{0xff, 6, 0xff}", SkFloatToFixed(17000.42f), 255, 0x3fd80ff},
98 {"18000.42=>{0xff, 6, 0xff}", SkFloatToFixed(18000.42f), 255, 0x3fd80ff},
99
100 {"16000.42=>{0x3e80, 6, 0x3e81}", SkFloatToFixed(16000.42f), MAX_PACKED_VALUE, 0xfa01be81},
101 {"16382.00=>{0x3ffe, 0, 0x3fff}", SkFloatToFixed(16382.00f), MAX_PACKED_VALUE, 0xfff83fff},
102 {"16382.51=>{0x3ffe, 8, 0x3fff}}", SkFloatToFixed(16382.51f), MAX_PACKED_VALUE, 0xfffa3fff},
103 {"17000.42=>{0x3fff, 6, 0x3fff}", SkFloatToFixed(17000.42f), MAX_PACKED_VALUE, 0xfffdbfff},
104 {"18000.42=>{0x3fff, 6, 0x3fff}", SkFloatToFixed(18000.42f), MAX_PACKED_VALUE, 0xfffdbfff},
105 // Adding 1 to this would overflow and cause an UBSAN issue, if it were not suppressed.
106 // We suppress the warning and it wraps around.
107 {"32767.90=>{0x3fff, e, 0x0}", SkFloatToFixed(32767.90f), MAX_PACKED_VALUE, 0xffff8000},
108 };
109
110 constexpr size_t NUM_TESTS = sizeof(tests) / sizeof(TestCase);
111
112 for (size_t i = 0; i < NUM_TESTS; i++) {
113 TestCase tc = tests[i];
114 uint32_t rv = sktests::pack_clamp(tc.input, tc.max);
115 uint32_t exp = tc.expectedOutput;
116
117 REPORTER_ASSERT(r, rv == tc.expectedOutput,
118 "%s | %x != %x | {%x, %x, %x} != {%x, %x, %x}\n",
119 tc.name.c_str(), rv, exp,
121 highBits(exp), middleBits(exp), lowBits(exp));
122 }
123}
124
125DEF_TEST(MatrixProcs_pack_repeat, r) {
126 struct TestCase {
127 std::string name;
128 SkFixed input;
129 unsigned max;
130 uint32_t expectedOutput;
131 size_t width;
132 };
133 TestCase tests[] = {
134 // negative values wrap back around
135 {"-0.300 => {0x2c, 0xc, 0x2d}", SkFloatToFixed(-0.300f), 63, 0xb3002d, 63},
136 {"-0.200 => {0x33, 0x3, 0x34}", SkFloatToFixed(-0.200f), 63, 0xccc034, 63},
137 {"-0.100 => {0x39, 0x9, 0x3a}", SkFloatToFixed(-0.100f), 63, 0xe6403a, 63},
138 // The domain of the function is primarily [0.0, 1.0)
139 {"0.0000 => {0x00, 0x0, 0x01}", SkFloatToFixed(0.0000f), 63, 0x000001, 63},
140 {"0.1000 => {0x06, 0x6, 0x07}", SkFloatToFixed(0.1000f), 63, 0x198007, 63},
141 {"0.1234 => {0x07, 0xe, 0x08}", SkFloatToFixed(0.1234f), 63, 0x1f8008, 63},
142 {"0.2000 => {0x0c, 0xc, 0x0d}", SkFloatToFixed(0.2000f), 63, 0x33000d, 63},
143 {"0.3000 => {0x13, 0x3, 0x14}", SkFloatToFixed(0.3000f), 63, 0x4cc014, 63},
144 {"0.4000 => {0x19, 0x9, 0x1a}", SkFloatToFixed(0.4000f), 63, 0x66401a, 63},
145 {"0.5000 => {0x20, 0x0, 0x21}", SkFloatToFixed(0.5000f), 63, 0x800021, 63},
146 {"0.5678 => {0x24, 0x5, 0x25}", SkFloatToFixed(0.5678f), 63, 0x914025, 63},
147 {"0.6000 => {0x26, 0x6, 0x27}", SkFloatToFixed(0.6000f), 63, 0x998027, 63},
148 {"0.7000 => {0x2c, 0xc, 0x2d}", SkFloatToFixed(0.7000f), 63, 0xb3002d, 63},
149 {"0.8000 => {0x33, 0x3, 0x34}", SkFloatToFixed(0.8000f), 63, 0xccc034, 63},
150 {"0.9000 => {0x39, 0x9, 0x3a}", SkFloatToFixed(0.9000f), 63, 0xe6403a, 63},
151 {"0.9500 => {0x3c, 0xc, 0x3d}", SkFloatToFixed(0.9500f), 63, 0xf3003d, 63},
152 {"0.9990 => {0x3f, 0xe, 0x00}", SkFloatToFixed(0.9990f), 63, 0xff8000, 63},
153 // As we go past 1.0, we wrap around, conceptually similar to modular arithmetic.
154 {"1.0000 => {0x00, 0x0, 0x01}", SkFloatToFixed(1.0000f), 63, 0x000001, 63},
155 {"1.1000 => {0x06, 0x6, 0x07}", SkFloatToFixed(1.1000f), 63, 0x198007, 63},
156 {"1.1234 => {0x07, 0xe, 0x08}", SkFloatToFixed(1.1234f), 63, 0x1f8008, 63},
157 {"1.9500 => {0x3c, 0xc, 0x3d}", SkFloatToFixed(1.9500f), 63, 0xf3003d, 63},
158 // Maximum has changed from 63 to 256
159 {"0.4567 => {0x75, 0x5, 0x76}", SkFloatToFixed(0.4567f), 256, 0x1d54076, 256},
160 {"1.0000 => {0x00, 0x0, 0x01}", SkFloatToFixed(1.0000f), 256, 0x0000001, 256},
161 {"1.2345 => {0x3c, 0x4, 0x3d}", SkFloatToFixed(1.2345f), 256, 0x0f1003d, 256},
162 // width does not have to match the maximum value (e.g. rescaling)
163 {"0.1111 [64,128] => {0x07, 0x3, 0x07}", SkFloatToFixed(0.1111f), 64, 0x1cc007, 128},
164 {"0.1111 [64,256] => {0x07, 0x3, 0x07}", SkFloatToFixed(0.1111f), 64, 0x1cc007, 256},
165 {"0.1111 [64,512] => {0x07, 0x3, 0x07}", SkFloatToFixed(0.1111f), 64, 0x1cc007, 512},
166 {"0.1111 [64, 32] => {0x07, 0x3, 0x09}", SkFloatToFixed(0.1111f), 64, 0x1cc009, 32},
167 {"0.1111 [64, 8] => {0x07, 0x3, 0x0f}", SkFloatToFixed(0.1111f), 64, 0x1cc00f, 8},
168 };
169
170 constexpr size_t NUM_TESTS = sizeof(tests) / sizeof(TestCase);
171
172 for (size_t i = 0; i < NUM_TESTS; i++) {
173 TestCase tc = tests[i];
174 uint32_t rv = sktests::pack_repeat(tc.input, tc.max, tc.width);
175 uint32_t exp = tc.expectedOutput;
176
177 REPORTER_ASSERT(r, rv == tc.expectedOutput,
178 "%s | %x != %x | {%x, %x, %x} != {%x, %x, %x}\n",
179 tc.name.c_str(), rv, exp,
181 highBits(exp), middleBits(exp), lowBits(exp));
182 }
183}
184
185DEF_TEST(MatrixProcs_pack_mirror, r) {
186 struct TestCase {
187 std::string name;
188 SkFixed input;
189 unsigned max;
190 uint32_t expectedOutput;
191 size_t width;
192 };
193 TestCase tests[] = {
194 // negative values are treated similarly to absolute values, except
195 // the first integer is bigger than the last integer.
196 {"-0.300 => {0x13, 0xc, 0x12}", SkFloatToFixed(-0.300f), 63, 0x4f0012, 63},
197 {"-0.200 => {0x0c, 0x3, 0x0b}", SkFloatToFixed(-0.200f), 63, 0x30c00b, 63},
198 {"-0.100 => {0x06, 0x9, 0x05}", SkFloatToFixed(-0.100f), 63, 0x1a4005, 63},
199 // The domain of the function is primarily [0.0, 1.0)
200 {"0.0000 => {0x00, 0x0, 0x01}", SkFloatToFixed(0.0000f), 63, 0x000001, 63},
201 {"0.1000 => {0x06, 0x6, 0x07}", SkFloatToFixed(0.1000f), 63, 0x198007, 63},
202 {"0.1234 => {0x07, 0xe, 0x08}", SkFloatToFixed(0.1234f), 63, 0x1f8008, 63},
203 {"0.2000 => {0x0c, 0xc, 0x0d}", SkFloatToFixed(0.2000f), 63, 0x33000d, 63},
204 {"0.3000 => {0x13, 0x3, 0x14}", SkFloatToFixed(0.3000f), 63, 0x4cc014, 63},
205 {"0.4000 => {0x19, 0x9, 0x1a}", SkFloatToFixed(0.4000f), 63, 0x66401a, 63},
206 {"0.5000 => {0x20, 0x0, 0x21}", SkFloatToFixed(0.5000f), 63, 0x800021, 63},
207 {"0.5678 => {0x24, 0x5, 0x25}", SkFloatToFixed(0.5678f), 63, 0x914025, 63},
208 {"0.6000 => {0x26, 0x6, 0x27}", SkFloatToFixed(0.6000f), 63, 0x998027, 63},
209 {"0.7000 => {0x2c, 0xc, 0x2d}", SkFloatToFixed(0.7000f), 63, 0xb3002d, 63},
210 {"0.8000 => {0x33, 0x3, 0x34}", SkFloatToFixed(0.8000f), 63, 0xccc034, 63},
211 {"0.9000 => {0x39, 0x9, 0x3a}", SkFloatToFixed(0.9000f), 63, 0xe6403a, 63},
212 {"0.9500 => {0x3c, 0xc, 0x3d}", SkFloatToFixed(0.9500f), 63, 0xf3003d, 63},
213 {"0.9990 => {0x3f, 0xe, 0x3f}", SkFloatToFixed(0.9990f), 63, 0xff803f, 63},
214 // As we go past 1.0, we bounce back, as off a wall or reflecting off a mirror.
215 {"1.0000 => {0x3f, 0x0, 0x3e}", SkFloatToFixed(1.0000f), 63, 0xfc003e, 63},
216 {"1.1000 => {0x39, 0x6, 0x38}", SkFloatToFixed(1.1000f), 63, 0xe58038, 63},
217 {"1.1234 => {0x38, 0xe, 0x37}", SkFloatToFixed(1.1234f), 63, 0xe38037, 63},
218 {"1.9500 => {0x03, 0xc, 0x02}", SkFloatToFixed(1.9500f), 63, 0xf0002, 63},
219 // Maximum has changed from 63 to 256
220 {"0.4567 => {0x75, 0x5, 0x76}", SkFloatToFixed(0.4567f), 256, 0x1d54076, 256},
221 {"1.0000 => {0x100,0x0, 0xff}", SkFloatToFixed(1.0000f), 256, 0x40000ff, 256},
222 {"1.2345 => {0xc4, 0x4, 0xc3}", SkFloatToFixed(1.2345f), 256, 0x31100c3, 256},
223 // width does not have to match the maximum value (e.g. rescaling)
224 {"0.1111 [64,128] => {0x07, 0x3, 0x07}", SkFloatToFixed(0.1111f), 64, 0x1cc007, 128},
225 {"0.1111 [64,256] => {0x07, 0x3, 0x07}", SkFloatToFixed(0.1111f), 64, 0x1cc007, 256},
226 {"0.1111 [64,512] => {0x07, 0x3, 0x07}", SkFloatToFixed(0.1111f), 64, 0x1cc007, 512},
227 {"0.1111 [64, 32] => {0x07, 0x3, 0x09}", SkFloatToFixed(0.1111f), 64, 0x1cc009, 32},
228 {"0.1111 [64, 8] => {0x07, 0x3, 0x0f}", SkFloatToFixed(0.1111f), 64, 0x1cc00f, 8},
229 };
230
231 constexpr size_t NUM_TESTS = sizeof(tests) / sizeof(TestCase);
232
233 for (size_t i = 0; i < NUM_TESTS; i++) {
234 TestCase tc = tests[i];
235 uint32_t rv = sktests::pack_mirror(tc.input, tc.max, tc.width);
236 uint32_t exp = tc.expectedOutput;
237
238 REPORTER_ASSERT(r, rv == tc.expectedOutput,
239 "%s | %x != %x | {%x, %x, %x} != {%x, %x, %x}\n",
240 tc.name.c_str(), rv, exp,
242 highBits(exp), middleBits(exp), lowBits(exp));
243 }
244}
245
246DEF_TEST(MatrixProcs_unpack_int, r) {
247 struct TestCase {
248 std::string name;
249 uint32_t input;
250 uint32_t expectedLowerBound;
251 uint32_t expectedLerp;
252 uint32_t expectedUpperBound;
253 };
254 // These are selected from earlier tests to make sure the packed values unpack correctly.
255 TestCase tests[] = {
256 {"0x000000 => {0x00, 0x0, 0x00}", 0x000000, 0x00, 0x0, 0x00},
257 {"0x074002 => {0x01, 0xd, 0x02}", 0x074002, 0x01, 0xd, 0x02},
258 {"0x15c006 => {0x05, 0x7, 0x06}", 0x15c006, 0x05, 0x7, 0x06},
259 {"0x1d0008 => {0x07, 0x4, 0x08}", 0x1d0008, 0x07, 0x4, 0x08},
260 {"0x24000a => {0x09, 0x0, 0x0a}", 0x24000a, 0x09, 0x0, 0x0a},
261 {"0xfc003f => {0x3f, 0x0, 0x3f}", 0xfc003f, 0x3f, 0x0, 0x3f},
262 {"0x4000100 => {0x100, 0x0, 0x100}", 0x4000100, 0x100, 0x0, 0x100},
263 };
264
265 constexpr size_t NUM_TESTS = sizeof(tests) / sizeof(TestCase);
266
267 for (size_t i = 0; i < NUM_TESTS; i++) {
268 TestCase tc = tests[i];
269 uint32_t lower, upper, lerp;
271
272 REPORTER_ASSERT(r, lower == tc.expectedLowerBound,
273 "%s lower %x != %x", tc.name.c_str(), lower, tc.expectedLowerBound);
274 REPORTER_ASSERT(r, lerp == tc.expectedLerp,
275 "%s lerp %x != %x", tc.name.c_str(), lerp, tc.expectedLerp);
276 REPORTER_ASSERT(r, upper == tc.expectedUpperBound,
277 "%s upper %x != %x", tc.name.c_str(), upper, tc.expectedUpperBound);
278 // Make sure our helpers work as expected.
279 SkASSERT(tc.expectedLowerBound == highBits(tc.input));
280 SkASSERT(tc.expectedLerp == middleBits(tc.input));
281 SkASSERT(tc.expectedUpperBound == lowBits(tc.input));
282 }
283}
static BlurTest tests[]
Definition: BlurTest.cpp:84
SkPoint lerp(const SkPoint &a, const SkPoint &b, float t)
DEF_TEST(MatrixProcs_pack_clamp, r)
uint32_t highBits(uint32_t rv)
uint32_t lowBits(uint32_t rv)
uint32_t middleBits(uint32_t rv)
#define SkASSERT(cond)
Definition: SkAssert.h:116
int32_t SkFixed
Definition: SkFixed.h:25
#define SkFloatToFixed(x)
Definition: SkFixed.h:42
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
static float max(float r, float g, float b)
Definition: hsl.cpp:49
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
uint32_t pack_repeat(SkFixed f, unsigned max, size_t width)
uint32_t pack_mirror(SkFixed f, unsigned max, size_t width)
void decode_packed_coordinates_and_weight(U32 packed, Out *v0, Out *v1, Out *w)
uint32_t pack_clamp(SkFixed f, unsigned max)
int32_t width