blob: 014683c9b9ed7be38dc574181beca00dbb664c6f [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 Klein729b5822016-11-28 18:23:23 -050024 p.append(SkRasterPipeline::load_f16, &load_s_ctx);
Mike Klein8c8cb5b2017-01-06 10:21:56 -050025 p.append(SkRasterPipeline::move_src_dst);
26 p.append(SkRasterPipeline::load_f16, &load_d_ctx);
27 p.append(SkRasterPipeline::swap);
Mike Klein9161ef02016-10-04 14:03:27 -040028 p.append(SkRasterPipeline::srcover);
Mike Kleinbd3fe472016-10-25 15:43:46 -040029 p.append(SkRasterPipeline::store_f16, &store_ctx);
Mike Klein761d27c2017-06-01 12:37:08 -040030 p.run(0,0,1);
mtklein281b33f2016-07-12 15:01:26 -070031
Mike Klein9161ef02016-10-04 14:03:27 -040032 // We should see half-intensity magenta.
Mike Klein26bea5d2016-10-05 10:36:38 -040033 REPORTER_ASSERT(r, ((result >> 0) & 0xffff) == 0x3800);
34 REPORTER_ASSERT(r, ((result >> 16) & 0xffff) == 0x0000);
35 REPORTER_ASSERT(r, ((result >> 32) & 0xffff) == 0x3800);
36 REPORTER_ASSERT(r, ((result >> 48) & 0xffff) == 0x3c00);
mtklein281b33f2016-07-12 15:01:26 -070037}
mtklein0abddf72016-07-13 08:22:20 -070038
39DEF_TEST(SkRasterPipeline_empty, r) {
40 // No asserts... just a test that this is safe to run.
Mike Kleinb24704d2017-05-24 07:53:00 -040041 SkRasterPipeline_<256> p;
Mike Klein761d27c2017-06-01 12:37:08 -040042 p.run(0,0,20);
mtklein0abddf72016-07-13 08:22:20 -070043}
44
45DEF_TEST(SkRasterPipeline_nonsense, r) {
46 // No asserts... just a test that this is safe to run and terminates.
Mike Klein9161ef02016-10-04 14:03:27 -040047 // srcover() calls st->next(); this makes sure we've always got something there to call.
Mike Kleinb24704d2017-05-24 07:53:00 -040048 SkRasterPipeline_<256> p;
Mike Klein9161ef02016-10-04 14:03:27 -040049 p.append(SkRasterPipeline::srcover);
Mike Klein761d27c2017-06-01 12:37:08 -040050 p.run(0,0,20);
mtklein0abddf72016-07-13 08:22:20 -070051}
Mike Klein7ba89a12017-01-10 13:42:51 -050052
53DEF_TEST(SkRasterPipeline_JIT, r) {
54 // This tests a couple odd corners that a JIT backend can stumble over.
55
56 uint32_t buf[72] = {
57 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
58 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
59 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
60 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
61 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63 };
64
65 const uint32_t* src = buf + 0;
66 uint32_t* dst = buf + 36;
67
68 // Copy buf[x] to buf[x+36] for x in [15,35).
Mike Kleinb24704d2017-05-24 07:53:00 -040069 SkRasterPipeline_<256> p;
Mike Klein7ba89a12017-01-10 13:42:51 -050070 p.append(SkRasterPipeline:: load_8888, &src);
71 p.append(SkRasterPipeline::store_8888, &dst);
Mike Klein761d27c2017-06-01 12:37:08 -040072 p.run(15,0, 20);
Mike Klein7ba89a12017-01-10 13:42:51 -050073
74 for (int i = 0; i < 36; i++) {
75 if (i < 15 || i == 35) {
76 REPORTER_ASSERT(r, dst[i] == 0);
77 } else {
78 REPORTER_ASSERT(r, dst[i] == (uint32_t)(i - 11));
79 }
80 }
81}
Herb Derbyd1f08302017-05-26 15:42:28 -040082
83static uint16_t h(float f) {
84 // Remember, a float is 1-8-23 (sign-exponent-mantissa) with 127 exponent bias.
85 uint32_t sem;
86 memcpy(&sem, &f, sizeof(sem));
87 uint32_t s = sem & 0x80000000,
88 em = sem ^ s;
89
90 // Convert to 1-5-10 half with 15 bias, flushing denorm halfs (including zero) to zero.
91 auto denorm = (int32_t)em < 0x38800000; // I32 comparison is often quicker, and always safe
92 // here.
93 return denorm ? SkTo<uint16_t>(0)
94 : SkTo<uint16_t>((s>>16) + (em>>13) - ((127-15)<<10));
95}
96
97static uint16_t n(uint16_t x) {
98 return (x<<8) | (x>>8);
99}
100
101static float a(uint16_t x) {
102 return (1/65535.0f) * x;
103}
104
105DEF_TEST(SkRasterPipeline_tail, r) {
106 {
107 float data[][4] = {
108 {00, 01, 02, 03},
109 {10, 11, 12, 13},
110 {20, 21, 22, 23},
111 {30, 31, 32, 33},
112 };
113
114 float buffer[4][4];
115 float* src = &data[0][0];
116 float* dst = &buffer[0][0];
117
Herb Derby84dcac32017-05-30 16:56:32 -0400118 for (unsigned i = 1; i <= 4; i++) {
Herb Derbyd1f08302017-05-26 15:42:28 -0400119 memset(buffer, 0xff, sizeof(buffer));
120 SkRasterPipeline_<256> p;
121 p.append(SkRasterPipeline::load_f32, &src);
122 p.append(SkRasterPipeline::store_f32, &dst);
Mike Klein761d27c2017-06-01 12:37:08 -0400123 p.run(0,0, i);
Herb Derbyd1f08302017-05-26 15:42:28 -0400124 for (unsigned j = 0; j < i; j++) {
Herb Derby84dcac32017-05-30 16:56:32 -0400125 for (unsigned k = 0; k < 4; k++) {
126 if (buffer[j][k] != data[j][k]) {
127 ERRORF(r, "(%u, %u) - a: %g r: %g\n", j, k, data[j][k], buffer[j][k]);
128 }
129 }
Herb Derbyd1f08302017-05-26 15:42:28 -0400130 }
131 for (int j = i; j < 4; j++) {
132 for (auto f : buffer[j]) {
133 REPORTER_ASSERT(r, SkScalarIsNaN(f));
134 }
135 }
136 }
137 }
138
139 {
140 uint16_t data[][4] = {
141 {h(00), h(01), h(02), h(03)},
142 {h(10), h(11), h(12), h(13)},
143 {h(20), h(21), h(22), h(23)},
144 {h(30), h(31), h(32), h(33)},
145 };
146 uint16_t buffer[4][4];
147 uint16_t* src = &data[0][0];
148 uint16_t* dst = &buffer[0][0];
149
Herb Derby84dcac32017-05-30 16:56:32 -0400150 for (unsigned i = 1; i <= 4; i++) {
Herb Derbyd1f08302017-05-26 15:42:28 -0400151 memset(buffer, 0xff, sizeof(buffer));
152 SkRasterPipeline_<256> p;
153 p.append(SkRasterPipeline::load_f16, &src);
154 p.append(SkRasterPipeline::store_f16, &dst);
Mike Klein761d27c2017-06-01 12:37:08 -0400155 p.run(0,0, i);
Herb Derbyd1f08302017-05-26 15:42:28 -0400156 for (unsigned j = 0; j < i; j++) {
157 REPORTER_ASSERT(r,
158 !memcmp(&data[j][0], &buffer[j][0], sizeof(buffer[j])));
159 }
160 for (int j = i; j < 4; j++) {
161 for (auto f : buffer[j]) {
162 REPORTER_ASSERT(r, f == 0xffff);
163 }
164 }
165 }
166 }
167
168 {
169 uint16_t data[][3] = {
170 {n(00), n(01), n(02)},
171 {n(10), n(11), n(12)},
172 {n(20), n(21), n(22)},
173 {n(30), n(31), n(32)}
174 };
175
176 float answer[][4] = {
177 {a(00), a(01), a(02), 1.0f},
178 {a(10), a(11), a(12), 1.0f},
179 {a(20), a(21), a(22), 1.0f},
180 {a(30), a(31), a(32), 1.0f}
181 };
182
183 float buffer[4][4];
184 uint16_t* src = &data[0][0];
185 float* dst = &buffer[0][0];
186
Herb Derby84dcac32017-05-30 16:56:32 -0400187 for (unsigned i = 1; i <= 4; i++) {
Herb Derbyd1f08302017-05-26 15:42:28 -0400188 memset(buffer, 0xff, sizeof(buffer));
189 SkRasterPipeline_<256> p;
190 p.append(SkRasterPipeline::load_rgb_u16_be, &src);
191 p.append(SkRasterPipeline::store_f32, &dst);
Mike Klein761d27c2017-06-01 12:37:08 -0400192 p.run(0,0, i);
Herb Derbyd1f08302017-05-26 15:42:28 -0400193 for (unsigned j = 0; j < i; j++) {
194 for (unsigned k = 0; k < 4; k++) {
195 if (buffer[j][k] != answer[j][k]) {
196 ERRORF(r, "(%u, %u) - a: %g r: %g\n", j, k, answer[j][k], buffer[j][k]);
197 }
198 }
199 }
200 for (int j = i; j < 4; j++) {
201 for (auto f : buffer[j]) {
202 REPORTER_ASSERT(r, SkScalarIsNaN(f));
203 }
204 }
205 }
206 }
207}
Mike Kleinbba02c22017-06-02 10:03:30 -0400208
209DEF_TEST(SkRasterPipeline_lowp, r) {
210 uint32_t rgba[64];
211 for (int i = 0; i < 64; i++) {
212 rgba[i] = (4*i+0) << 0
213 | (4*i+1) << 8
214 | (4*i+2) << 16
215 | (4*i+3) << 24;
216 }
217
218 void* ptr = rgba;
219
220 SkRasterPipeline_<256> p;
221 p.append(SkRasterPipeline::load_8888, &ptr);
222 p.append(SkRasterPipeline::swap_rb);
223 p.append(SkRasterPipeline::store_8888, &ptr);
224 p.run(0,0,64);
225
226 for (int i = 0; i < 64; i++) {
227 uint32_t want = (4*i+0) << 16
228 | (4*i+1) << 8
229 | (4*i+2) << 0
230 | (4*i+3) << 24;
231 if (rgba[i] != want) {
232 ERRORF(r, "got %08x, want %08x\n", rgba[i], want);
233 }
234 }
235}