blob: 0c6354b9ca41029bec0fc4988c744f03cd3d8f01 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/private/SkHalf.h"
9#include "include/private/SkTo.h"
10#include "src/core/SkRasterPipeline.h"
11#include "tests/Test.h"
mtklein281b33f2016-07-12 15:01:26 -070012
mtklein281b33f2016-07-12 15:01:26 -070013DEF_TEST(SkRasterPipeline, r) {
Mike Klein9161ef02016-10-04 14:03:27 -040014 // Build and run a simple pipeline to exercise SkRasterPipeline,
15 // drawing 50% transparent blue over opaque red in half-floats.
Mike Klein26bea5d2016-10-05 10:36:38 -040016 uint64_t red = 0x3c00000000003c00ull,
17 blue = 0x3800380000000000ull,
18 result;
Mike Kleind0ccb572016-10-05 09:36:26 -040019
Mike Kleinb11ab572018-10-24 06:42:14 -040020 SkRasterPipeline_MemoryCtx load_s_ctx = { &blue, 0 },
21 load_d_ctx = { &red, 0 },
22 store_ctx = { &result, 0 };
Mike Kleinbd3fe472016-10-25 15:43:46 -040023
Mike Kleinb24704d2017-05-24 07:53:00 -040024 SkRasterPipeline_<256> p;
Mike Klein9f2b3d12017-06-27 17:26:50 -040025 p.append(SkRasterPipeline::load_f16, &load_s_ctx);
26 p.append(SkRasterPipeline::load_f16_dst, &load_d_ctx);
Mike Klein9161ef02016-10-04 14:03:27 -040027 p.append(SkRasterPipeline::srcover);
Mike Kleinbd3fe472016-10-25 15:43:46 -040028 p.append(SkRasterPipeline::store_f16, &store_ctx);
Mike Klein45c16fa2017-07-18 18:15:13 -040029 p.run(0,0,1,1);
mtklein281b33f2016-07-12 15:01:26 -070030
Mike Klein9161ef02016-10-04 14:03:27 -040031 // We should see half-intensity magenta.
Mike Klein26bea5d2016-10-05 10:36:38 -040032 REPORTER_ASSERT(r, ((result >> 0) & 0xffff) == 0x3800);
33 REPORTER_ASSERT(r, ((result >> 16) & 0xffff) == 0x0000);
34 REPORTER_ASSERT(r, ((result >> 32) & 0xffff) == 0x3800);
35 REPORTER_ASSERT(r, ((result >> 48) & 0xffff) == 0x3c00);
mtklein281b33f2016-07-12 15:01:26 -070036}
mtklein0abddf72016-07-13 08:22:20 -070037
38DEF_TEST(SkRasterPipeline_empty, r) {
39 // No asserts... just a test that this is safe to run.
Mike Kleinb24704d2017-05-24 07:53:00 -040040 SkRasterPipeline_<256> p;
Mike Klein45c16fa2017-07-18 18:15:13 -040041 p.run(0,0,20,1);
mtklein0abddf72016-07-13 08:22:20 -070042}
43
44DEF_TEST(SkRasterPipeline_nonsense, r) {
45 // No asserts... just a test that this is safe to run and terminates.
Mike Klein9161ef02016-10-04 14:03:27 -040046 // srcover() calls st->next(); this makes sure we've always got something there to call.
Mike Kleinb24704d2017-05-24 07:53:00 -040047 SkRasterPipeline_<256> p;
Mike Klein9161ef02016-10-04 14:03:27 -040048 p.append(SkRasterPipeline::srcover);
Mike Klein45c16fa2017-07-18 18:15:13 -040049 p.run(0,0,20,1);
mtklein0abddf72016-07-13 08:22:20 -070050}
Mike Klein7ba89a12017-01-10 13:42:51 -050051
52DEF_TEST(SkRasterPipeline_JIT, r) {
53 // This tests a couple odd corners that a JIT backend can stumble over.
54
55 uint32_t buf[72] = {
56 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
58 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62 };
63
Mike Kleinb11ab572018-10-24 06:42:14 -040064 SkRasterPipeline_MemoryCtx src = { buf + 0, 0 },
Mike Klein45c16fa2017-07-18 18:15:13 -040065 dst = { buf + 36, 0 };
Mike Klein7ba89a12017-01-10 13:42:51 -050066
67 // Copy buf[x] to buf[x+36] for x in [15,35).
Mike Kleinb24704d2017-05-24 07:53:00 -040068 SkRasterPipeline_<256> p;
Mike Klein7ba89a12017-01-10 13:42:51 -050069 p.append(SkRasterPipeline:: load_8888, &src);
70 p.append(SkRasterPipeline::store_8888, &dst);
Mike Klein45c16fa2017-07-18 18:15:13 -040071 p.run(15,0, 20,1);
Mike Klein7ba89a12017-01-10 13:42:51 -050072
73 for (int i = 0; i < 36; i++) {
74 if (i < 15 || i == 35) {
Mike Klein45c16fa2017-07-18 18:15:13 -040075 REPORTER_ASSERT(r, buf[i+36] == 0);
Mike Klein7ba89a12017-01-10 13:42:51 -050076 } else {
Mike Klein45c16fa2017-07-18 18:15:13 -040077 REPORTER_ASSERT(r, buf[i+36] == (uint32_t)(i - 11));
Mike Klein7ba89a12017-01-10 13:42:51 -050078 }
79 }
80}
Herb Derbyd1f08302017-05-26 15:42:28 -040081
82static uint16_t h(float f) {
83 // Remember, a float is 1-8-23 (sign-exponent-mantissa) with 127 exponent bias.
84 uint32_t sem;
85 memcpy(&sem, &f, sizeof(sem));
86 uint32_t s = sem & 0x80000000,
87 em = sem ^ s;
88
89 // Convert to 1-5-10 half with 15 bias, flushing denorm halfs (including zero) to zero.
90 auto denorm = (int32_t)em < 0x38800000; // I32 comparison is often quicker, and always safe
91 // here.
92 return denorm ? SkTo<uint16_t>(0)
93 : SkTo<uint16_t>((s>>16) + (em>>13) - ((127-15)<<10));
94}
95
Herb Derbyd1f08302017-05-26 15:42:28 -040096DEF_TEST(SkRasterPipeline_tail, r) {
97 {
98 float data[][4] = {
99 {00, 01, 02, 03},
100 {10, 11, 12, 13},
101 {20, 21, 22, 23},
102 {30, 31, 32, 33},
103 };
104
105 float buffer[4][4];
Mike Klein45c16fa2017-07-18 18:15:13 -0400106
Mike Kleinb11ab572018-10-24 06:42:14 -0400107 SkRasterPipeline_MemoryCtx src = { &data[0][0], 0 },
Mike Klein45c16fa2017-07-18 18:15:13 -0400108 dst = { &buffer[0][0], 0 };
Herb Derbyd1f08302017-05-26 15:42:28 -0400109
Herb Derby84dcac32017-05-30 16:56:32 -0400110 for (unsigned i = 1; i <= 4; i++) {
Herb Derbyd1f08302017-05-26 15:42:28 -0400111 memset(buffer, 0xff, sizeof(buffer));
112 SkRasterPipeline_<256> p;
113 p.append(SkRasterPipeline::load_f32, &src);
114 p.append(SkRasterPipeline::store_f32, &dst);
Mike Klein45c16fa2017-07-18 18:15:13 -0400115 p.run(0,0, i,1);
Herb Derbyd1f08302017-05-26 15:42:28 -0400116 for (unsigned j = 0; j < i; j++) {
Herb Derby84dcac32017-05-30 16:56:32 -0400117 for (unsigned k = 0; k < 4; k++) {
118 if (buffer[j][k] != data[j][k]) {
119 ERRORF(r, "(%u, %u) - a: %g r: %g\n", j, k, data[j][k], buffer[j][k]);
120 }
121 }
Herb Derbyd1f08302017-05-26 15:42:28 -0400122 }
123 for (int j = i; j < 4; j++) {
124 for (auto f : buffer[j]) {
125 REPORTER_ASSERT(r, SkScalarIsNaN(f));
126 }
127 }
128 }
129 }
130
131 {
Mike Klein0bf89262018-02-26 13:34:21 -0500132 alignas(8) uint16_t data[][4] = {
Herb Derbyd1f08302017-05-26 15:42:28 -0400133 {h(00), h(01), h(02), h(03)},
134 {h(10), h(11), h(12), h(13)},
135 {h(20), h(21), h(22), h(23)},
136 {h(30), h(31), h(32), h(33)},
137 };
Mike Klein0bf89262018-02-26 13:34:21 -0500138 alignas(8) uint16_t buffer[4][4];
Mike Kleinb11ab572018-10-24 06:42:14 -0400139 SkRasterPipeline_MemoryCtx src = { &data[0][0], 0 },
Mike Klein45c16fa2017-07-18 18:15:13 -0400140 dst = { &buffer[0][0], 0 };
Herb Derbyd1f08302017-05-26 15:42:28 -0400141
Herb Derby84dcac32017-05-30 16:56:32 -0400142 for (unsigned i = 1; i <= 4; i++) {
Herb Derbyd1f08302017-05-26 15:42:28 -0400143 memset(buffer, 0xff, sizeof(buffer));
144 SkRasterPipeline_<256> p;
145 p.append(SkRasterPipeline::load_f16, &src);
146 p.append(SkRasterPipeline::store_f16, &dst);
Mike Klein45c16fa2017-07-18 18:15:13 -0400147 p.run(0,0, i,1);
Herb Derbyd1f08302017-05-26 15:42:28 -0400148 for (unsigned j = 0; j < i; j++) {
149 REPORTER_ASSERT(r,
150 !memcmp(&data[j][0], &buffer[j][0], sizeof(buffer[j])));
151 }
152 for (int j = i; j < 4; j++) {
153 for (auto f : buffer[j]) {
154 REPORTER_ASSERT(r, f == 0xffff);
155 }
156 }
157 }
158 }
Herb Derbyd1f08302017-05-26 15:42:28 -0400159}
Mike Kleinbba02c22017-06-02 10:03:30 -0400160
161DEF_TEST(SkRasterPipeline_lowp, r) {
162 uint32_t rgba[64];
163 for (int i = 0; i < 64; i++) {
164 rgba[i] = (4*i+0) << 0
165 | (4*i+1) << 8
166 | (4*i+2) << 16
167 | (4*i+3) << 24;
168 }
169
Mike Kleinb11ab572018-10-24 06:42:14 -0400170 SkRasterPipeline_MemoryCtx ptr = { rgba, 0 };
Mike Kleinbba02c22017-06-02 10:03:30 -0400171
172 SkRasterPipeline_<256> p;
Mike Klein1a3eb522018-10-18 10:11:00 -0400173 p.append(SkRasterPipeline::load_8888, &ptr);
174 p.append(SkRasterPipeline::swap_rb);
Mike Kleinbba02c22017-06-02 10:03:30 -0400175 p.append(SkRasterPipeline::store_8888, &ptr);
Mike Klein45c16fa2017-07-18 18:15:13 -0400176 p.run(0,0,64,1);
Mike Kleinbba02c22017-06-02 10:03:30 -0400177
178 for (int i = 0; i < 64; i++) {
179 uint32_t want = (4*i+0) << 16
180 | (4*i+1) << 8
181 | (4*i+2) << 0
182 | (4*i+3) << 24;
183 if (rgba[i] != want) {
184 ERRORF(r, "got %08x, want %08x\n", rgba[i], want);
185 }
186 }
187}
Mike Kleinea045b52018-08-23 12:13:58 -0400188
189DEF_TEST(SkRasterPipeline_lowp_clamp01, r) {
190 // This may seem like a funny pipeline to create,
191 // but it certainly shouldn't crash when you run it.
192
193 uint32_t rgba = 0xff00ff00;
194
Mike Kleinb11ab572018-10-24 06:42:14 -0400195 SkRasterPipeline_MemoryCtx ptr = { &rgba, 0 };
Mike Kleinea045b52018-08-23 12:13:58 -0400196
197 SkRasterPipeline_<256> p;
Mike Klein1a3eb522018-10-18 10:11:00 -0400198 p.append(SkRasterPipeline::load_8888, &ptr);
199 p.append(SkRasterPipeline::swap_rb);
Mike Kleinea045b52018-08-23 12:13:58 -0400200 p.append(SkRasterPipeline::clamp_0);
201 p.append(SkRasterPipeline::clamp_1);
202 p.append(SkRasterPipeline::store_8888, &ptr);
203 p.run(0,0,1,1);
204}