blob: 4a951c06c3b24d527075c1206776ed1c49517186 [file] [log] [blame]
mtklein281b33f2016-07-12 15:01:26 -07001/*
2 * Copyright 2016 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 "Test.h"
Mike Klein9161ef02016-10-04 14:03:27 -04009#include "SkHalf.h"
mtklein281b33f2016-07-12 15:01:26 -070010#include "SkRasterPipeline.h"
11
mtklein281b33f2016-07-12 15:01:26 -070012DEF_TEST(SkRasterPipeline, r) {
Mike Klein9161ef02016-10-04 14:03:27 -040013 // Build and run a simple pipeline to exercise SkRasterPipeline,
14 // drawing 50% transparent blue over opaque red in half-floats.
Mike Klein26bea5d2016-10-05 10:36:38 -040015 uint64_t red = 0x3c00000000003c00ull,
16 blue = 0x3800380000000000ull,
17 result;
Mike Kleind0ccb572016-10-05 09:36:26 -040018
Mike Kleinbd3fe472016-10-25 15:43:46 -040019 void* load_s_ctx = &blue;
20 void* load_d_ctx = &red;
21 void* store_ctx = &result;
22
Mike Kleinb24704d2017-05-24 07:53:00 -040023 SkRasterPipeline_<256> p;
Mike Klein9f2b3d12017-06-27 17:26:50 -040024 p.append(SkRasterPipeline::load_f16, &load_s_ctx);
25 p.append(SkRasterPipeline::load_f16_dst, &load_d_ctx);
Mike Klein9161ef02016-10-04 14:03:27 -040026 p.append(SkRasterPipeline::srcover);
Mike Kleinbd3fe472016-10-25 15:43:46 -040027 p.append(SkRasterPipeline::store_f16, &store_ctx);
Mike Klein761d27c2017-06-01 12:37:08 -040028 p.run(0,0,1);
mtklein281b33f2016-07-12 15:01:26 -070029
Mike Klein9161ef02016-10-04 14:03:27 -040030 // We should see half-intensity magenta.
Mike Klein26bea5d2016-10-05 10:36:38 -040031 REPORTER_ASSERT(r, ((result >> 0) & 0xffff) == 0x3800);
32 REPORTER_ASSERT(r, ((result >> 16) & 0xffff) == 0x0000);
33 REPORTER_ASSERT(r, ((result >> 32) & 0xffff) == 0x3800);
34 REPORTER_ASSERT(r, ((result >> 48) & 0xffff) == 0x3c00);
mtklein281b33f2016-07-12 15:01:26 -070035}
mtklein0abddf72016-07-13 08:22:20 -070036
37DEF_TEST(SkRasterPipeline_empty, r) {
38 // No asserts... just a test that this is safe to run.
Mike Kleinb24704d2017-05-24 07:53:00 -040039 SkRasterPipeline_<256> p;
Mike Klein761d27c2017-06-01 12:37:08 -040040 p.run(0,0,20);
mtklein0abddf72016-07-13 08:22:20 -070041}
42
43DEF_TEST(SkRasterPipeline_nonsense, r) {
44 // No asserts... just a test that this is safe to run and terminates.
Mike Klein9161ef02016-10-04 14:03:27 -040045 // srcover() calls st->next(); this makes sure we've always got something there to call.
Mike Kleinb24704d2017-05-24 07:53:00 -040046 SkRasterPipeline_<256> p;
Mike Klein9161ef02016-10-04 14:03:27 -040047 p.append(SkRasterPipeline::srcover);
Mike Klein761d27c2017-06-01 12:37:08 -040048 p.run(0,0,20);
mtklein0abddf72016-07-13 08:22:20 -070049}
Mike Klein7ba89a12017-01-10 13:42:51 -050050
51DEF_TEST(SkRasterPipeline_JIT, r) {
52 // This tests a couple odd corners that a JIT backend can stumble over.
53
54 uint32_t buf[72] = {
55 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
56 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
57 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
58 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
59 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
60 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
61 };
62
63 const uint32_t* src = buf + 0;
64 uint32_t* dst = buf + 36;
65
66 // Copy buf[x] to buf[x+36] for x in [15,35).
Mike Kleinb24704d2017-05-24 07:53:00 -040067 SkRasterPipeline_<256> p;
Mike Klein7ba89a12017-01-10 13:42:51 -050068 p.append(SkRasterPipeline:: load_8888, &src);
69 p.append(SkRasterPipeline::store_8888, &dst);
Mike Klein761d27c2017-06-01 12:37:08 -040070 p.run(15,0, 20);
Mike Klein7ba89a12017-01-10 13:42:51 -050071
72 for (int i = 0; i < 36; i++) {
73 if (i < 15 || i == 35) {
74 REPORTER_ASSERT(r, dst[i] == 0);
75 } else {
76 REPORTER_ASSERT(r, dst[i] == (uint32_t)(i - 11));
77 }
78 }
79}
Herb Derbyd1f08302017-05-26 15:42:28 -040080
81static uint16_t h(float f) {
82 // Remember, a float is 1-8-23 (sign-exponent-mantissa) with 127 exponent bias.
83 uint32_t sem;
84 memcpy(&sem, &f, sizeof(sem));
85 uint32_t s = sem & 0x80000000,
86 em = sem ^ s;
87
88 // Convert to 1-5-10 half with 15 bias, flushing denorm halfs (including zero) to zero.
89 auto denorm = (int32_t)em < 0x38800000; // I32 comparison is often quicker, and always safe
90 // here.
91 return denorm ? SkTo<uint16_t>(0)
92 : SkTo<uint16_t>((s>>16) + (em>>13) - ((127-15)<<10));
93}
94
95static uint16_t n(uint16_t x) {
96 return (x<<8) | (x>>8);
97}
98
99static float a(uint16_t x) {
100 return (1/65535.0f) * x;
101}
102
103DEF_TEST(SkRasterPipeline_tail, r) {
104 {
105 float data[][4] = {
106 {00, 01, 02, 03},
107 {10, 11, 12, 13},
108 {20, 21, 22, 23},
109 {30, 31, 32, 33},
110 };
111
112 float buffer[4][4];
113 float* src = &data[0][0];
114 float* dst = &buffer[0][0];
115
Herb Derby84dcac32017-05-30 16:56:32 -0400116 for (unsigned i = 1; i <= 4; i++) {
Herb Derbyd1f08302017-05-26 15:42:28 -0400117 memset(buffer, 0xff, sizeof(buffer));
118 SkRasterPipeline_<256> p;
119 p.append(SkRasterPipeline::load_f32, &src);
120 p.append(SkRasterPipeline::store_f32, &dst);
Mike Klein761d27c2017-06-01 12:37:08 -0400121 p.run(0,0, i);
Herb Derbyd1f08302017-05-26 15:42:28 -0400122 for (unsigned j = 0; j < i; j++) {
Herb Derby84dcac32017-05-30 16:56:32 -0400123 for (unsigned k = 0; k < 4; k++) {
124 if (buffer[j][k] != data[j][k]) {
125 ERRORF(r, "(%u, %u) - a: %g r: %g\n", j, k, data[j][k], buffer[j][k]);
126 }
127 }
Herb Derbyd1f08302017-05-26 15:42:28 -0400128 }
129 for (int j = i; j < 4; j++) {
130 for (auto f : buffer[j]) {
131 REPORTER_ASSERT(r, SkScalarIsNaN(f));
132 }
133 }
134 }
135 }
136
137 {
138 uint16_t data[][4] = {
139 {h(00), h(01), h(02), h(03)},
140 {h(10), h(11), h(12), h(13)},
141 {h(20), h(21), h(22), h(23)},
142 {h(30), h(31), h(32), h(33)},
143 };
144 uint16_t buffer[4][4];
145 uint16_t* src = &data[0][0];
146 uint16_t* dst = &buffer[0][0];
147
Herb Derby84dcac32017-05-30 16:56:32 -0400148 for (unsigned i = 1; i <= 4; i++) {
Herb Derbyd1f08302017-05-26 15:42:28 -0400149 memset(buffer, 0xff, sizeof(buffer));
150 SkRasterPipeline_<256> p;
151 p.append(SkRasterPipeline::load_f16, &src);
152 p.append(SkRasterPipeline::store_f16, &dst);
Mike Klein761d27c2017-06-01 12:37:08 -0400153 p.run(0,0, i);
Herb Derbyd1f08302017-05-26 15:42:28 -0400154 for (unsigned j = 0; j < i; j++) {
155 REPORTER_ASSERT(r,
156 !memcmp(&data[j][0], &buffer[j][0], sizeof(buffer[j])));
157 }
158 for (int j = i; j < 4; j++) {
159 for (auto f : buffer[j]) {
160 REPORTER_ASSERT(r, f == 0xffff);
161 }
162 }
163 }
164 }
165
166 {
167 uint16_t data[][3] = {
168 {n(00), n(01), n(02)},
169 {n(10), n(11), n(12)},
170 {n(20), n(21), n(22)},
171 {n(30), n(31), n(32)}
172 };
173
174 float answer[][4] = {
175 {a(00), a(01), a(02), 1.0f},
176 {a(10), a(11), a(12), 1.0f},
177 {a(20), a(21), a(22), 1.0f},
178 {a(30), a(31), a(32), 1.0f}
179 };
180
181 float buffer[4][4];
182 uint16_t* src = &data[0][0];
183 float* dst = &buffer[0][0];
184
Herb Derby84dcac32017-05-30 16:56:32 -0400185 for (unsigned i = 1; i <= 4; i++) {
Herb Derbyd1f08302017-05-26 15:42:28 -0400186 memset(buffer, 0xff, sizeof(buffer));
187 SkRasterPipeline_<256> p;
188 p.append(SkRasterPipeline::load_rgb_u16_be, &src);
189 p.append(SkRasterPipeline::store_f32, &dst);
Mike Klein761d27c2017-06-01 12:37:08 -0400190 p.run(0,0, i);
Herb Derbyd1f08302017-05-26 15:42:28 -0400191 for (unsigned j = 0; j < i; j++) {
192 for (unsigned k = 0; k < 4; k++) {
193 if (buffer[j][k] != answer[j][k]) {
194 ERRORF(r, "(%u, %u) - a: %g r: %g\n", j, k, answer[j][k], buffer[j][k]);
195 }
196 }
197 }
198 for (int j = i; j < 4; j++) {
199 for (auto f : buffer[j]) {
200 REPORTER_ASSERT(r, SkScalarIsNaN(f));
201 }
202 }
203 }
204 }
205}
Mike Kleinbba02c22017-06-02 10:03:30 -0400206
207DEF_TEST(SkRasterPipeline_lowp, r) {
208 uint32_t rgba[64];
209 for (int i = 0; i < 64; i++) {
210 rgba[i] = (4*i+0) << 0
211 | (4*i+1) << 8
212 | (4*i+2) << 16
213 | (4*i+3) << 24;
214 }
215
216 void* ptr = rgba;
217
218 SkRasterPipeline_<256> p;
219 p.append(SkRasterPipeline::load_8888, &ptr);
220 p.append(SkRasterPipeline::swap_rb);
221 p.append(SkRasterPipeline::store_8888, &ptr);
222 p.run(0,0,64);
223
224 for (int i = 0; i < 64; i++) {
225 uint32_t want = (4*i+0) << 16
226 | (4*i+1) << 8
227 | (4*i+2) << 0
228 | (4*i+3) << 24;
229 if (rgba[i] != want) {
230 ERRORF(r, "got %08x, want %08x\n", rgba[i], want);
231 }
232 }
233}