blob: a40fe6c72ef8b8269d7af8755363320266097d9c [file] [log] [blame]
Mike Kleinbaaf8ad2016-09-29 09:04:15 -04001/*
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#ifndef SkRasterPipeline_opts_DEFINED
9#define SkRasterPipeline_opts_DEFINED
10
Mike Klein1f49f262016-10-31 19:49:27 -040011#include "SkColorPriv.h"
raftias25636012016-11-11 15:27:39 -080012#include "SkColorLookUpTable.h"
Matt Sarettdb4d4062016-11-16 16:07:15 -050013#include "SkColorSpaceXform_A2B.h"
14#include "SkColorSpaceXformPriv.h"
Mike Kleinbaaf8ad2016-09-29 09:04:15 -040015#include "SkHalf.h"
Mike Klein46e66a22016-11-21 16:19:34 -050016#include "SkImageShaderContext.h"
Mike Kleina0c4c342016-11-29 13:58:49 -050017#include "SkMSAN.h"
Mike Kleinbaaf8ad2016-09-29 09:04:15 -040018#include "SkPM4f.h"
mtklein125b2aa2016-11-04 13:41:34 -070019#include "SkPM4fPriv.h"
Mike Kleinbaaf8ad2016-09-29 09:04:15 -040020#include "SkRasterPipeline.h"
21#include "SkSRGB.h"
mtklein125b2aa2016-11-04 13:41:34 -070022#include "SkUtils.h"
Mike Klein2878e762016-10-19 21:05:17 -040023#include <utility>
Mike Kleinbaaf8ad2016-09-29 09:04:15 -040024
Mike Kleinaebfb452016-10-25 10:27:33 -040025namespace {
26
Mike Klein2878e762016-10-19 21:05:17 -040027#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_AVX2
28 static constexpr int N = 8;
29#else
30 static constexpr int N = 4;
31#endif
32
mtkleina4a44882016-11-04 13:20:07 -070033 using SkNf = SkNx<N, float>;
Mike Kleind5de0132016-11-28 09:33:02 -050034 using SkNi = SkNx<N, int32_t>;
35 using SkNu = SkNx<N, uint32_t>;
mtkleina4a44882016-11-04 13:20:07 -070036 using SkNh = SkNx<N, uint16_t>;
Mike Klein06a65e22016-11-17 12:39:09 -050037 using SkNb = SkNx<N, uint8_t>;
Mike Kleinbaaf8ad2016-09-29 09:04:15 -040038
Mike Klein2cbc33d2016-11-28 16:30:30 -050039 struct Stage;
Mike Kleinbddd2342016-11-29 12:46:58 -050040 using Fn = void(SK_VECTORCALL *)(Stage*, size_t x_tail, SkNf,SkNf,SkNf,SkNf,
41 SkNf,SkNf,SkNf,SkNf);
Mike Klein2cbc33d2016-11-28 16:30:30 -050042 struct Stage { Fn next; void* ctx; };
Mike Kleinaebfb452016-10-25 10:27:33 -040043
Mike Kleinbddd2342016-11-29 12:46:58 -050044 // x_tail encodes two values x and tail as x*N+tail, where 0 <= tail < N.
45 // x is the induction variable we're walking along, incrementing by N each step.
46 // tail == 0 means work with a full N pixels; otherwise use only the low tail pixels.
47
Mike Kleinaebfb452016-10-25 10:27:33 -040048} // namespace
Mike Klein2878e762016-10-19 21:05:17 -040049
Mike Klein04adfda2016-10-12 09:52:55 -040050#define SI static inline
Mike Kleinbaaf8ad2016-09-29 09:04:15 -040051
Mike Klein49372e62016-10-20 18:05:23 -040052// Stages are logically a pipeline, and physically are contiguous in an array.
53// To get to the next stage, we just increment our pointer to the next array element.
Mike Klein2cbc33d2016-11-28 16:30:30 -050054SI void SK_VECTORCALL next(Stage* st, size_t x_tail, SkNf r, SkNf g, SkNf b, SkNf a,
55 SkNf dr, SkNf dg, SkNf db, SkNf da) {
56 st->next(st+1, x_tail, r,g,b,a, dr,dg,db,da);
Mike Klein49372e62016-10-20 18:05:23 -040057}
58
Mike Klein729b5822016-11-28 18:23:23 -050059// Stages defined below always call next.
60// This is always the last stage, a backstop that actually returns to the caller when done.
61SI void SK_VECTORCALL just_return(Stage*, size_t, SkNf, SkNf, SkNf, SkNf,
62 SkNf, SkNf, SkNf, SkNf) {}
63
Mike Klein2cbc33d2016-11-28 16:30:30 -050064#define STAGE(name) \
Mike Kleinaebfb452016-10-25 10:27:33 -040065 static SK_ALWAYS_INLINE void name##_kernel(void* ctx, size_t x, size_t tail, \
66 SkNf& r, SkNf& g, SkNf& b, SkNf& a, \
67 SkNf& dr, SkNf& dg, SkNf& db, SkNf& da); \
Mike Klein2cbc33d2016-11-28 16:30:30 -050068 SI void SK_VECTORCALL name(Stage* st, size_t x_tail, \
Mike Kleinaebfb452016-10-25 10:27:33 -040069 SkNf r, SkNf g, SkNf b, SkNf a, \
70 SkNf dr, SkNf dg, SkNf db, SkNf da) { \
Mike Klein2cbc33d2016-11-28 16:30:30 -050071 name##_kernel(st->ctx, x_tail/N, x_tail%N, r,g,b,a, dr,dg,db,da); \
72 next(st, x_tail, r,g,b,a, dr,dg,db,da); \
Mike Kleinaebfb452016-10-25 10:27:33 -040073 } \
Mike Kleinaebfb452016-10-25 10:27:33 -040074 static SK_ALWAYS_INLINE void name##_kernel(void* ctx, size_t x, size_t tail, \
75 SkNf& r, SkNf& g, SkNf& b, SkNf& a, \
Mike Klein04adfda2016-10-12 09:52:55 -040076 SkNf& dr, SkNf& dg, SkNf& db, SkNf& da)
Mike Kleinbaaf8ad2016-09-29 09:04:15 -040077
Mike Kleinbaaf8ad2016-09-29 09:04:15 -040078
Mike Klein9161ef02016-10-04 14:03:27 -040079// Many xfermodes apply the same logic to each channel.
Mike Kleinaebfb452016-10-25 10:27:33 -040080#define RGBA_XFERMODE(name) \
81 static SK_ALWAYS_INLINE SkNf name##_kernel(const SkNf& s, const SkNf& sa, \
82 const SkNf& d, const SkNf& da); \
Mike Klein2cbc33d2016-11-28 16:30:30 -050083 SI void SK_VECTORCALL name(Stage* st, size_t x_tail, \
Mike Kleinaebfb452016-10-25 10:27:33 -040084 SkNf r, SkNf g, SkNf b, SkNf a, \
85 SkNf dr, SkNf dg, SkNf db, SkNf da) { \
86 r = name##_kernel(r,a,dr,da); \
87 g = name##_kernel(g,a,dg,da); \
88 b = name##_kernel(b,a,db,da); \
89 a = name##_kernel(a,a,da,da); \
Mike Klein2cbc33d2016-11-28 16:30:30 -050090 next(st, x_tail, r,g,b,a, dr,dg,db,da); \
Mike Kleinaebfb452016-10-25 10:27:33 -040091 } \
92 static SK_ALWAYS_INLINE SkNf name##_kernel(const SkNf& s, const SkNf& sa, \
Mike Klein04adfda2016-10-12 09:52:55 -040093 const SkNf& d, const SkNf& da)
Mike Klein9161ef02016-10-04 14:03:27 -040094
95// Most of the rest apply the same logic to color channels and use srcover's alpha logic.
Mike Kleinaebfb452016-10-25 10:27:33 -040096#define RGB_XFERMODE(name) \
97 static SK_ALWAYS_INLINE SkNf name##_kernel(const SkNf& s, const SkNf& sa, \
98 const SkNf& d, const SkNf& da); \
Mike Klein2cbc33d2016-11-28 16:30:30 -050099 SI void SK_VECTORCALL name(Stage* st, size_t x_tail, \
Mike Kleinaebfb452016-10-25 10:27:33 -0400100 SkNf r, SkNf g, SkNf b, SkNf a, \
101 SkNf dr, SkNf dg, SkNf db, SkNf da) { \
102 r = name##_kernel(r,a,dr,da); \
103 g = name##_kernel(g,a,dg,da); \
104 b = name##_kernel(b,a,db,da); \
105 a = a + (da * (1.0f-a)); \
Mike Klein2cbc33d2016-11-28 16:30:30 -0500106 next(st, x_tail, r,g,b,a, dr,dg,db,da); \
Mike Kleinaebfb452016-10-25 10:27:33 -0400107 } \
108 static SK_ALWAYS_INLINE SkNf name##_kernel(const SkNf& s, const SkNf& sa, \
Mike Klein04adfda2016-10-12 09:52:55 -0400109 const SkNf& d, const SkNf& da)
110
Mike Klein2cbc33d2016-11-28 16:30:30 -0500111template <typename T>
Mike Kleinaebfb452016-10-25 10:27:33 -0400112SI SkNx<N,T> load(size_t tail, const T* src) {
Mike Klein2cbc33d2016-11-28 16:30:30 -0500113 if (tail) {
Mike Kleinaebfb452016-10-25 10:27:33 -0400114 T buf[8] = {0};
115 switch (tail & (N-1)) {
116 case 7: buf[6] = src[6];
117 case 6: buf[5] = src[5];
118 case 5: buf[4] = src[4];
119 case 4: buf[3] = src[3];
120 case 3: buf[2] = src[2];
121 case 2: buf[1] = src[1];
122 }
123 buf[0] = src[0];
124 return SkNx<N,T>::Load(buf);
125 }
126 return SkNx<N,T>::Load(src);
127}
Mike Klein2cbc33d2016-11-28 16:30:30 -0500128template <typename T>
Mike Klein56b50792016-11-29 08:14:49 -0500129SI SkNx<N,T> gather(size_t tail, const T* src, const SkNi& offset) {
130 if (tail) {
131 T buf[8] = {0};
132 switch (tail & (N-1)) {
133 case 7: buf[6] = src[offset[6]];
134 case 6: buf[5] = src[offset[5]];
135 case 5: buf[4] = src[offset[4]];
136 case 4: buf[3] = src[offset[3]];
137 case 3: buf[2] = src[offset[2]];
138 case 2: buf[1] = src[offset[1]];
139 }
140 buf[0] = src[offset[0]];
141 return SkNx<N,T>::Load(buf);
142 }
143 T buf[8];
144 for (size_t i = 0; i < N; i++) {
145 buf[i] = src[offset[i]];
146 }
147 return SkNx<N,T>::Load(buf);
148}
149template <typename T>
Mike Kleinaebfb452016-10-25 10:27:33 -0400150SI void store(size_t tail, const SkNx<N,T>& v, T* dst) {
Mike Klein2cbc33d2016-11-28 16:30:30 -0500151 if (tail) {
Mike Kleinaebfb452016-10-25 10:27:33 -0400152 switch (tail & (N-1)) {
153 case 7: dst[6] = v[6];
154 case 6: dst[5] = v[5];
155 case 5: dst[4] = v[4];
156 case 4: dst[3] = v[3];
157 case 3: dst[2] = v[2];
158 case 2: dst[1] = v[1];
159 }
160 dst[0] = v[0];
161 return;
162 }
163 v.store(dst);
164}
165
Mike Klein56b50792016-11-29 08:14:49 -0500166#if !defined(SKNX_NO_SIMD) && SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_AVX2
167 SI __m256i mask(size_t tail) {
168 static const int masks[][8] = {
169 {~0,~0,~0,~0, ~0,~0,~0,~0 }, // remember, tail == 0 ~~> load all N
170 {~0, 0, 0, 0, 0, 0, 0, 0 },
171 {~0,~0, 0, 0, 0, 0, 0, 0 },
172 {~0,~0,~0, 0, 0, 0, 0, 0 },
173 {~0,~0,~0,~0, 0, 0, 0, 0 },
174 {~0,~0,~0,~0, ~0, 0, 0, 0 },
175 {~0,~0,~0,~0, ~0,~0, 0, 0 },
176 {~0,~0,~0,~0, ~0,~0,~0, 0 },
177 };
178 return SkNi::Load(masks + tail).fVec;
179 }
180
181 SI SkNi load(size_t tail, const int32_t* src) {
182 return tail ? _mm256_maskload_epi32((const int*)src, mask(tail))
183 : SkNi::Load(src);
184 }
185 SI SkNu load(size_t tail, const uint32_t* src) {
186 return tail ? _mm256_maskload_epi32((const int*)src, mask(tail))
187 : SkNu::Load(src);
188 }
189 SI SkNi gather(size_t tail, const int32_t* src, const SkNi& offset) {
190 return _mm256_mask_i32gather_epi32(SkNi(0).fVec,
191 (const int*)src, offset.fVec, mask(tail), 4);
192 }
193 SI SkNu gather(size_t tail, const uint32_t* src, const SkNi& offset) {
194 return _mm256_mask_i32gather_epi32(SkNi(0).fVec,
195 (const int*)src, offset.fVec, mask(tail), 4);
196 }
Mike Kleina0c4c342016-11-29 13:58:49 -0500197
198 static const char* bug = "I don't think MSAN understands maskstore.";
199
Mike Klein56b50792016-11-29 08:14:49 -0500200 SI void store(size_t tail, const SkNi& v, int32_t* dst) {
Mike Kleina0c4c342016-11-29 13:58:49 -0500201 if (tail) {
202 _mm256_maskstore_epi32((int*)dst, mask(tail), v.fVec);
203 return sk_msan_mark_initialized(dst, dst+tail, bug);
204 }
205 v.store(dst);
Mike Klein56b50792016-11-29 08:14:49 -0500206 }
207 SI void store(size_t tail, const SkNu& v, uint32_t* dst) {
Mike Kleina0c4c342016-11-29 13:58:49 -0500208 if (tail) {
209 _mm256_maskstore_epi32((int*)dst, mask(tail), v.fVec);
210 return sk_msan_mark_initialized(dst, dst+tail, bug);
211 }
212 v.store(dst);
Mike Klein56b50792016-11-29 08:14:49 -0500213 }
214#endif
215
Mike Kleind5de0132016-11-28 09:33:02 -0500216SI void from_8888(const SkNu& _8888, SkNf* r, SkNf* g, SkNf* b, SkNf* a) {
217 auto to_float = [](const SkNu& v) { return SkNx_cast<float>(SkNi::Load(&v)); };
218 *r = (1/255.0f)*to_float((_8888 >> 0) & 0xff);
219 *g = (1/255.0f)*to_float((_8888 >> 8) & 0xff);
220 *b = (1/255.0f)*to_float((_8888 >> 16) & 0xff);
221 *a = (1/255.0f)*to_float( _8888 >> 24 );
222}
Mike Klein6b77f1c2016-11-22 15:50:12 -0500223SI void from_4444(const SkNh& _4444, SkNf* r, SkNf* g, SkNf* b, SkNf* a) {
224 auto _32_bit = SkNx_cast<int>(_4444);
225
226 *r = SkNx_cast<float>(_32_bit & (0xF << SK_R4444_SHIFT)) * (1.0f / (0xF << SK_R4444_SHIFT));
227 *g = SkNx_cast<float>(_32_bit & (0xF << SK_G4444_SHIFT)) * (1.0f / (0xF << SK_G4444_SHIFT));
228 *b = SkNx_cast<float>(_32_bit & (0xF << SK_B4444_SHIFT)) * (1.0f / (0xF << SK_B4444_SHIFT));
229 *a = SkNx_cast<float>(_32_bit & (0xF << SK_A4444_SHIFT)) * (1.0f / (0xF << SK_A4444_SHIFT));
230}
Mike Kleinaebfb452016-10-25 10:27:33 -0400231SI void from_565(const SkNh& _565, SkNf* r, SkNf* g, SkNf* b) {
232 auto _32_bit = SkNx_cast<int>(_565);
233
234 *r = SkNx_cast<float>(_32_bit & SK_R16_MASK_IN_PLACE) * (1.0f / SK_R16_MASK_IN_PLACE);
235 *g = SkNx_cast<float>(_32_bit & SK_G16_MASK_IN_PLACE) * (1.0f / SK_G16_MASK_IN_PLACE);
236 *b = SkNx_cast<float>(_32_bit & SK_B16_MASK_IN_PLACE) * (1.0f / SK_B16_MASK_IN_PLACE);
237}
238
Mike Klein2cbc33d2016-11-28 16:30:30 -0500239STAGE(trace) {
Mike Kleina9312fd2016-11-16 13:38:15 -0500240 SkDebugf("%s\n", (const char*)ctx);
241}
Mike Klein2cbc33d2016-11-28 16:30:30 -0500242STAGE(registers) {
Mike Kleina9312fd2016-11-16 13:38:15 -0500243 auto print = [](const char* name, const SkNf& v) {
244 SkDebugf("%s:", name);
245 for (int i = 0; i < N; i++) {
246 SkDebugf(" %g", v[i]);
247 }
248 SkDebugf("\n");
249 };
250 print(" r", r);
251 print(" g", g);
252 print(" b", b);
253 print(" a", a);
254 print("dr", dr);
255 print("dg", dg);
256 print("db", db);
257 print("da", da);
258}
259
Mike Klein2cbc33d2016-11-28 16:30:30 -0500260STAGE(clamp_0) {
Mike Klein130863e2016-10-27 11:29:36 -0400261 a = SkNf::Max(a, 0.0f);
262 r = SkNf::Max(r, 0.0f);
263 g = SkNf::Max(g, 0.0f);
264 b = SkNf::Max(b, 0.0f);
265}
Mike Klein2cbc33d2016-11-28 16:30:30 -0500266STAGE(clamp_a) {
Mike Klein130863e2016-10-27 11:29:36 -0400267 a = SkNf::Min(a, 1.0f);
268 r = SkNf::Min(r, a);
269 g = SkNf::Min(g, a);
270 b = SkNf::Min(b, a);
271}
Mike Klein2cbc33d2016-11-28 16:30:30 -0500272STAGE(clamp_1) {
Matt Sarettdb4d4062016-11-16 16:07:15 -0500273 a = SkNf::Min(a, 1.0f);
274 r = SkNf::Min(r, 1.0f);
275 g = SkNf::Min(g, 1.0f);
276 b = SkNf::Min(b, 1.0f);
277}
278
Mike Klein2cbc33d2016-11-28 16:30:30 -0500279STAGE(unpremul) {
Mike Klein5a130112016-11-28 09:48:31 -0500280 auto scale = (a == 0.0f).thenElse(0.0f, 1.0f/a);
281 r *= scale;
282 g *= scale;
283 b *= scale;
Mike Kleineea7c162016-11-03 10:20:35 -0400284}
Mike Klein2cbc33d2016-11-28 16:30:30 -0500285STAGE(premul) {
Mike Kleineea7c162016-11-03 10:20:35 -0400286 r *= a;
287 g *= a;
288 b *= a;
289}
290
Mike Klein7a147342016-11-29 15:33:39 -0500291STAGE(set_rgb) {
292 auto rgb = (const float*)ctx;
293 r = rgb[0];
294 g = rgb[1];
295 b = rgb[2];
296}
297
Mike Klein2cbc33d2016-11-28 16:30:30 -0500298STAGE(move_src_dst) {
Mike Kleinc5093412016-11-04 16:36:39 -0400299 dr = r;
300 dg = g;
301 db = b;
302 da = a;
Mike Kleinaebfb452016-10-25 10:27:33 -0400303}
Mike Klein2cbc33d2016-11-28 16:30:30 -0500304STAGE(move_dst_src) {
Mike Kleind5de0132016-11-28 09:33:02 -0500305 r = dr;
306 g = dg;
307 b = db;
308 a = da;
Mike Kleinfb191da2016-11-15 13:20:33 -0500309}
310
Mike Klein729b5822016-11-28 18:23:23 -0500311STAGE(swap_rb) { SkTSwap( r, b); }
312STAGE(swap_rb_d) { SkTSwap(dr, db); }
Mike Kleind5de0132016-11-28 09:33:02 -0500313
Mike Klein729b5822016-11-28 18:23:23 -0500314STAGE(from_srgb) {
Mike Kleinb04c3522016-11-28 11:55:58 -0500315 r = sk_linear_from_srgb_math(r);
316 g = sk_linear_from_srgb_math(g);
317 b = sk_linear_from_srgb_math(b);
318}
Mike Klein2cbc33d2016-11-28 16:30:30 -0500319STAGE(from_srgb_d) {
Mike Kleinb04c3522016-11-28 11:55:58 -0500320 dr = sk_linear_from_srgb_math(dr);
321 dg = sk_linear_from_srgb_math(dg);
322 db = sk_linear_from_srgb_math(db);
323}
Mike Klein2cbc33d2016-11-28 16:30:30 -0500324STAGE(to_srgb) {
Mike Kleine03339a2016-11-28 13:24:27 -0500325 r = sk_linear_to_srgb_needs_round(r);
326 g = sk_linear_to_srgb_needs_round(g);
327 b = sk_linear_to_srgb_needs_round(b);
Mike Kleinb04c3522016-11-28 11:55:58 -0500328}
Mike Kleind5de0132016-11-28 09:33:02 -0500329
Mike Kleinaebfb452016-10-25 10:27:33 -0400330// The default shader produces a constant color (from the SkPaint).
Mike Klein2cbc33d2016-11-28 16:30:30 -0500331STAGE(constant_color) {
Mike Kleinaebfb452016-10-25 10:27:33 -0400332 auto color = (const SkPM4f*)ctx;
333 r = color->r();
334 g = color->g();
335 b = color->b();
336 a = color->a();
337}
338
Mike Klein66866172016-11-03 12:22:01 -0400339// s' = sc for a constant c.
Mike Klein2cbc33d2016-11-28 16:30:30 -0500340STAGE(scale_constant_float) {
Mike Klein66866172016-11-03 12:22:01 -0400341 SkNf c = *(const float*)ctx;
342
343 r *= c;
344 g *= c;
345 b *= c;
346 a *= c;
347}
Mike Kleinaebfb452016-10-25 10:27:33 -0400348// s' = sc for 8-bit c.
Mike Klein2cbc33d2016-11-28 16:30:30 -0500349STAGE(scale_u8) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400350 auto ptr = *(const uint8_t**)ctx + x;
Mike Kleinaebfb452016-10-25 10:27:33 -0400351
Mike Klein2cbc33d2016-11-28 16:30:30 -0500352 SkNf c = SkNx_cast<float>(load(tail, ptr)) * (1/255.0f);
Mike Kleinaebfb452016-10-25 10:27:33 -0400353 r = r*c;
354 g = g*c;
355 b = b*c;
356 a = a*c;
357}
358
Mike Klein729b5822016-11-28 18:23:23 -0500359SI SkNf lerp(const SkNf& from, const SkNf& to, const SkNf& cov) {
360 return SkNx_fma(to-from, cov, from);
361}
362
363// s' = d(1-c) + sc, for a constant c.
364STAGE(lerp_constant_float) {
365 SkNf c = *(const float*)ctx;
366
367 r = lerp(dr, r, c);
368 g = lerp(dg, g, c);
369 b = lerp(db, b, c);
370 a = lerp(da, a, c);
371}
372
Mike Kleinaebfb452016-10-25 10:27:33 -0400373// s' = d(1-c) + sc for 8-bit c.
Mike Klein2cbc33d2016-11-28 16:30:30 -0500374STAGE(lerp_u8) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400375 auto ptr = *(const uint8_t**)ctx + x;
Mike Kleinaebfb452016-10-25 10:27:33 -0400376
Mike Klein2cbc33d2016-11-28 16:30:30 -0500377 SkNf c = SkNx_cast<float>(load(tail, ptr)) * (1/255.0f);
Mike Kleinaebfb452016-10-25 10:27:33 -0400378 r = lerp(dr, r, c);
379 g = lerp(dg, g, c);
380 b = lerp(db, b, c);
381 a = lerp(da, a, c);
382}
383
384// s' = d(1-c) + sc for 565 c.
Mike Klein2cbc33d2016-11-28 16:30:30 -0500385STAGE(lerp_565) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400386 auto ptr = *(const uint16_t**)ctx + x;
Mike Kleinaebfb452016-10-25 10:27:33 -0400387 SkNf cr, cg, cb;
Mike Klein2cbc33d2016-11-28 16:30:30 -0500388 from_565(load(tail, ptr), &cr, &cg, &cb);
Mike Kleinaebfb452016-10-25 10:27:33 -0400389
390 r = lerp(dr, r, cr);
391 g = lerp(dg, g, cg);
392 b = lerp(db, b, cb);
393 a = 1.0f;
394}
395
Mike Klein729b5822016-11-28 18:23:23 -0500396STAGE(load_565) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400397 auto ptr = *(const uint16_t**)ctx + x;
Mike Klein2cbc33d2016-11-28 16:30:30 -0500398 from_565(load(tail, ptr), &r,&g,&b);
Mike Kleinaebfb452016-10-25 10:27:33 -0400399 a = 1.0f;
400}
Mike Klein729b5822016-11-28 18:23:23 -0500401STAGE(load_565_d) {
402 auto ptr = *(const uint16_t**)ctx + x;
403 from_565(load(tail, ptr), &dr,&dg,&db);
404 da = 1.0f;
405}
Mike Klein2cbc33d2016-11-28 16:30:30 -0500406STAGE(store_565) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400407 auto ptr = *(uint16_t**)ctx + x;
Mike Klein729b5822016-11-28 18:23:23 -0500408 store(tail, SkNx_cast<uint16_t>( SkNx_cast<int>(r*SK_R16_MASK + 0.5f) << SK_R16_SHIFT
409 | SkNx_cast<int>(g*SK_G16_MASK + 0.5f) << SK_G16_SHIFT
410 | SkNx_cast<int>(b*SK_B16_MASK + 0.5f) << SK_B16_SHIFT), ptr);
Mike Kleinaebfb452016-10-25 10:27:33 -0400411}
412
Mike Kleinaebfb452016-10-25 10:27:33 -0400413
Mike Klein729b5822016-11-28 18:23:23 -0500414STAGE(load_f16) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400415 auto ptr = *(const uint64_t**)ctx + x;
Mike Kleinaebfb452016-10-25 10:27:33 -0400416
417 SkNh rh, gh, bh, ah;
Mike Klein2cbc33d2016-11-28 16:30:30 -0500418 if (tail) {
Mike Kleinaebfb452016-10-25 10:27:33 -0400419 uint64_t buf[8] = {0};
420 switch (tail & (N-1)) {
421 case 7: buf[6] = ptr[6];
422 case 6: buf[5] = ptr[5];
423 case 5: buf[4] = ptr[4];
424 case 4: buf[3] = ptr[3];
425 case 3: buf[2] = ptr[2];
426 case 2: buf[1] = ptr[1];
427 }
428 buf[0] = ptr[0];
429 SkNh::Load4(buf, &rh, &gh, &bh, &ah);
430 } else {
431 SkNh::Load4(ptr, &rh, &gh, &bh, &ah);
432 }
433
434 r = SkHalfToFloat_finite_ftz(rh);
435 g = SkHalfToFloat_finite_ftz(gh);
436 b = SkHalfToFloat_finite_ftz(bh);
437 a = SkHalfToFloat_finite_ftz(ah);
438}
Mike Klein729b5822016-11-28 18:23:23 -0500439STAGE(load_f16_d) {
440 auto ptr = *(const uint64_t**)ctx + x;
Mike Kleinaebfb452016-10-25 10:27:33 -0400441
Mike Klein729b5822016-11-28 18:23:23 -0500442 SkNh rh, gh, bh, ah;
443 if (tail) {
444 uint64_t buf[8] = {0};
445 switch (tail & (N-1)) {
446 case 7: buf[6] = ptr[6];
447 case 6: buf[5] = ptr[5];
448 case 5: buf[4] = ptr[4];
449 case 4: buf[3] = ptr[3];
450 case 3: buf[2] = ptr[2];
451 case 2: buf[1] = ptr[1];
452 }
453 buf[0] = ptr[0];
454 SkNh::Load4(buf, &rh, &gh, &bh, &ah);
455 } else {
456 SkNh::Load4(ptr, &rh, &gh, &bh, &ah);
457 }
458
459 dr = SkHalfToFloat_finite_ftz(rh);
460 dg = SkHalfToFloat_finite_ftz(gh);
461 db = SkHalfToFloat_finite_ftz(bh);
462 da = SkHalfToFloat_finite_ftz(ah);
463}
Mike Klein2cbc33d2016-11-28 16:30:30 -0500464STAGE(store_f16) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400465 auto ptr = *(uint64_t**)ctx + x;
Mike Kleinaebfb452016-10-25 10:27:33 -0400466
467 uint64_t buf[8];
Mike Klein2cbc33d2016-11-28 16:30:30 -0500468 SkNh::Store4(tail ? buf : ptr, SkFloatToHalf_finite_ftz(r),
469 SkFloatToHalf_finite_ftz(g),
470 SkFloatToHalf_finite_ftz(b),
471 SkFloatToHalf_finite_ftz(a));
472 if (tail) {
Mike Kleinaebfb452016-10-25 10:27:33 -0400473 switch (tail & (N-1)) {
474 case 7: ptr[6] = buf[6];
475 case 6: ptr[5] = buf[5];
476 case 5: ptr[4] = buf[4];
477 case 4: ptr[3] = buf[3];
478 case 3: ptr[2] = buf[2];
479 case 2: ptr[1] = buf[1];
480 }
481 ptr[0] = buf[0];
482 }
483}
484
Mike Klein2cbc33d2016-11-28 16:30:30 -0500485STAGE(store_f32) {
mtkleina4a44882016-11-04 13:20:07 -0700486 auto ptr = *(SkPM4f**)ctx + x;
487
488 SkPM4f buf[8];
Mike Klein2cbc33d2016-11-28 16:30:30 -0500489 SkNf::Store4(tail ? buf : ptr, r,g,b,a);
490 if (tail) {
mtkleina4a44882016-11-04 13:20:07 -0700491 switch (tail & (N-1)) {
492 case 7: ptr[6] = buf[6];
493 case 6: ptr[5] = buf[5];
494 case 5: ptr[4] = buf[4];
495 case 4: ptr[3] = buf[3];
496 case 3: ptr[2] = buf[2];
497 case 2: ptr[1] = buf[1];
498 }
499 ptr[0] = buf[0];
500 }
501}
502
Mike Kleinaebfb452016-10-25 10:27:33 -0400503
Mike Klein729b5822016-11-28 18:23:23 -0500504STAGE(load_8888) {
raftias25636012016-11-11 15:27:39 -0800505 auto ptr = *(const uint32_t**)ctx + x;
Mike Klein2cbc33d2016-11-28 16:30:30 -0500506 from_8888(load(tail, ptr), &r, &g, &b, &a);
raftias25636012016-11-11 15:27:39 -0800507}
Mike Klein729b5822016-11-28 18:23:23 -0500508STAGE(load_8888_d) {
Mike Kleine03339a2016-11-28 13:24:27 -0500509 auto ptr = *(const uint32_t**)ctx + x;
Mike Klein2cbc33d2016-11-28 16:30:30 -0500510 from_8888(load(tail, ptr), &dr, &dg, &db, &da);
Mike Kleine03339a2016-11-28 13:24:27 -0500511}
Mike Klein2cbc33d2016-11-28 16:30:30 -0500512STAGE(store_8888) {
raftias25636012016-11-11 15:27:39 -0800513 auto ptr = *(uint32_t**)ctx + x;
Mike Klein2cbc33d2016-11-28 16:30:30 -0500514 store(tail, ( SkNx_cast<int>(255.0f * r + 0.5f) << 0
515 | SkNx_cast<int>(255.0f * g + 0.5f) << 8
516 | SkNx_cast<int>(255.0f * b + 0.5f) << 16
517 | SkNx_cast<int>(255.0f * a + 0.5f) << 24 ), (int*)ptr);
raftias25636012016-11-11 15:27:39 -0800518}
519
Mike Klein729b5822016-11-28 18:23:23 -0500520SI SkNf inv(const SkNf& x) { return 1.0f - x; }
521
Mike Kleinaebfb452016-10-25 10:27:33 -0400522RGBA_XFERMODE(clear) { return 0.0f; }
Mike Kleinaebfb452016-10-25 10:27:33 -0400523RGBA_XFERMODE(srcatop) { return s*da + d*inv(sa); }
524RGBA_XFERMODE(srcin) { return s * da; }
525RGBA_XFERMODE(srcout) { return s * inv(da); }
526RGBA_XFERMODE(srcover) { return SkNx_fma(d, inv(sa), s); }
527RGBA_XFERMODE(dstatop) { return srcatop_kernel(d,da,s,sa); }
528RGBA_XFERMODE(dstin) { return srcin_kernel (d,da,s,sa); }
529RGBA_XFERMODE(dstout) { return srcout_kernel (d,da,s,sa); }
530RGBA_XFERMODE(dstover) { return srcover_kernel(d,da,s,sa); }
531
532RGBA_XFERMODE(modulate) { return s*d; }
533RGBA_XFERMODE(multiply) { return s*inv(da) + d*inv(sa) + s*d; }
534RGBA_XFERMODE(plus_) { return s + d; }
535RGBA_XFERMODE(screen) { return s + d - s*d; }
536RGBA_XFERMODE(xor_) { return s*inv(da) + d*inv(sa); }
537
538RGB_XFERMODE(colorburn) {
539 return (d == da ).thenElse(d + s*inv(da),
540 (s == 0.0f).thenElse(s + d*inv(sa),
541 sa*(da - SkNf::Min(da, (da-d)*sa/s)) + s*inv(da) + d*inv(sa)));
542}
543RGB_XFERMODE(colordodge) {
544 return (d == 0.0f).thenElse(d + s*inv(da),
545 (s == sa ).thenElse(s + d*inv(sa),
546 sa*SkNf::Min(da, (d*sa)/(sa - s)) + s*inv(da) + d*inv(sa)));
547}
548RGB_XFERMODE(darken) { return s + d - SkNf::Max(s*da, d*sa); }
549RGB_XFERMODE(difference) { return s + d - 2.0f*SkNf::Min(s*da,d*sa); }
550RGB_XFERMODE(exclusion) { return s + d - 2.0f*s*d; }
551RGB_XFERMODE(hardlight) {
552 return s*inv(da) + d*inv(sa)
553 + (2.0f*s <= sa).thenElse(2.0f*s*d, sa*da - 2.0f*(da-d)*(sa-s));
554}
555RGB_XFERMODE(lighten) { return s + d - SkNf::Min(s*da, d*sa); }
556RGB_XFERMODE(overlay) { return hardlight_kernel(d,da,s,sa); }
557RGB_XFERMODE(softlight) {
558 SkNf m = (da > 0.0f).thenElse(d / da, 0.0f),
559 s2 = 2.0f*s,
560 m4 = 4.0f*m;
561
562 // The logic forks three ways:
563 // 1. dark src?
564 // 2. light src, dark dst?
565 // 3. light src, light dst?
566 SkNf darkSrc = d*(sa + (s2 - sa)*(1.0f - m)), // Used in case 1.
567 darkDst = (m4*m4 + m4)*(m - 1.0f) + 7.0f*m, // Used in case 2.
568 liteDst = m.rsqrt().invert() - m, // Used in case 3.
569 liteSrc = d*sa + da*(s2 - sa) * (4.0f*d <= da).thenElse(darkDst, liteDst); // 2 or 3?
570 return s*inv(da) + d*inv(sa) + (s2 <= sa).thenElse(darkSrc, liteSrc); // 1 or (2 or 3)?
571}
572
Mike Klein2cbc33d2016-11-28 16:30:30 -0500573STAGE(luminance_to_alpha) {
Mike Klein1f49f262016-10-31 19:49:27 -0400574 a = SK_LUM_COEFF_R*r + SK_LUM_COEFF_G*g + SK_LUM_COEFF_B*b;
575 r = g = b = 0;
576}
577
Mike Klein2cbc33d2016-11-28 16:30:30 -0500578STAGE(matrix_2x3) {
Mike Klein06a65e22016-11-17 12:39:09 -0500579 auto m = (const float*)ctx;
580
581 auto fma = [](const SkNf& f, const SkNf& m, const SkNf& a) { return SkNx_fma(f,m,a); };
582 auto R = fma(r,m[0], fma(g,m[2], m[4])),
583 G = fma(r,m[1], fma(g,m[3], m[5]));
584 r = R;
585 g = G;
586}
Mike Klein2cbc33d2016-11-28 16:30:30 -0500587STAGE(matrix_3x4) {
raftias25636012016-11-11 15:27:39 -0800588 auto m = (const float*)ctx;
589
590 auto fma = [](const SkNf& f, const SkNf& m, const SkNf& a) { return SkNx_fma(f,m,a); };
591 auto R = fma(r,m[0], fma(g,m[3], fma(b,m[6], m[ 9]))),
592 G = fma(r,m[1], fma(g,m[4], fma(b,m[7], m[10]))),
593 B = fma(r,m[2], fma(g,m[5], fma(b,m[8], m[11])));
594 r = R;
595 g = G;
596 b = B;
597}
Mike Klein2cbc33d2016-11-28 16:30:30 -0500598STAGE(matrix_4x5) {
Mike Kleineea7c162016-11-03 10:20:35 -0400599 auto m = (const float*)ctx;
600
601 auto fma = [](const SkNf& f, const SkNf& m, const SkNf& a) { return SkNx_fma(f,m,a); };
602 auto R = fma(r,m[0], fma(g,m[4], fma(b,m[ 8], fma(a,m[12], m[16])))),
603 G = fma(r,m[1], fma(g,m[5], fma(b,m[ 9], fma(a,m[13], m[17])))),
604 B = fma(r,m[2], fma(g,m[6], fma(b,m[10], fma(a,m[14], m[18])))),
605 A = fma(r,m[3], fma(g,m[7], fma(b,m[11], fma(a,m[15], m[19]))));
606 r = R;
607 g = G;
608 b = B;
609 a = A;
610}
Mike Klein2cbc33d2016-11-28 16:30:30 -0500611STAGE(matrix_perspective) {
Mike Kleinc01e7df2016-11-17 16:27:10 -0500612 // N.B. unlike the matrix_NxM stages, this takes a row-major matrix.
613 auto m = (const float*)ctx;
614
615 auto fma = [](const SkNf& f, const SkNf& m, const SkNf& a) { return SkNx_fma(f,m,a); };
616 auto R = fma(r,m[0], fma(g,m[1], m[2])),
617 G = fma(r,m[3], fma(g,m[4], m[5])),
618 Z = fma(r,m[6], fma(g,m[7], m[8]));
619 r = R * Z.invert();
620 g = G * Z.invert();
621}
622
623
Mike Kleincfcf6242016-11-16 09:01:30 -0500624SI SkNf parametric(const SkNf& v, const SkColorSpaceTransferFn& p) {
625 float result[N]; // Unconstrained powf() doesn't vectorize well...
626 for (int i = 0; i < N; i++) {
627 float s = v[i];
628 result[i] = (s <= p.fD) ? p.fE * s + p.fF
629 : powf(s * p.fA + p.fB, p.fG) + p.fC;
630 }
631 return SkNf::Load(result);
632}
Mike Klein729b5822016-11-28 18:23:23 -0500633STAGE(parametric_r) { r = parametric(r, *(const SkColorSpaceTransferFn*)ctx); }
634STAGE(parametric_g) { g = parametric(g, *(const SkColorSpaceTransferFn*)ctx); }
635STAGE(parametric_b) { b = parametric(b, *(const SkColorSpaceTransferFn*)ctx); }
Mike Kleincfcf6242016-11-16 09:01:30 -0500636
Matt Sarettdb4d4062016-11-16 16:07:15 -0500637SI SkNf table(const SkNf& v, const SkTableTransferFn& table) {
638 float result[N];
Mike Kleincfcf6242016-11-16 09:01:30 -0500639 for (int i = 0; i < N; i++) {
Matt Sarettdb4d4062016-11-16 16:07:15 -0500640 result[i] = interp_lut(v[i], table.fData, table.fSize);
Mike Kleincfcf6242016-11-16 09:01:30 -0500641 }
642 return SkNf::Load(result);
643}
Mike Klein729b5822016-11-28 18:23:23 -0500644STAGE(table_r) { r = table(r, *(const SkTableTransferFn*)ctx); }
645STAGE(table_g) { g = table(g, *(const SkTableTransferFn*)ctx); }
646STAGE(table_b) { b = table(b, *(const SkTableTransferFn*)ctx); }
raftias25636012016-11-11 15:27:39 -0800647
Mike Klein2cbc33d2016-11-28 16:30:30 -0500648STAGE(color_lookup_table) {
raftias25636012016-11-11 15:27:39 -0800649 const SkColorLookUpTable* colorLUT = (const SkColorLookUpTable*)ctx;
650 float rgb[3];
651 float result[3][N];
652 for (int i = 0; i < N; ++i) {
653 rgb[0] = r[i];
654 rgb[1] = g[i];
655 rgb[2] = b[i];
656 colorLUT->interp3D(rgb, rgb);
657 result[0][i] = rgb[0];
658 result[1][i] = rgb[1];
659 result[2][i] = rgb[2];
660 }
661 r = SkNf::Load(result[0]);
662 g = SkNf::Load(result[1]);
663 b = SkNf::Load(result[2]);
664}
665
Mike Klein2cbc33d2016-11-28 16:30:30 -0500666STAGE(lab_to_xyz) {
raftias25636012016-11-11 15:27:39 -0800667 const auto lab_l = r * 100.0f;
668 const auto lab_a = g * 255.0f - 128.0f;
669 const auto lab_b = b * 255.0f - 128.0f;
670 auto Y = (lab_l + 16.0f) * (1/116.0f);
671 auto X = lab_a * (1/500.0f) + Y;
672 auto Z = Y - (lab_b * (1/200.0f));
673
674 const auto X3 = X*X*X;
675 X = (X3 > 0.008856f).thenElse(X3, (X - (16/116.0f)) * (1/7.787f));
676 const auto Y3 = Y*Y*Y;
677 Y = (Y3 > 0.008856f).thenElse(Y3, (Y - (16/116.0f)) * (1/7.787f));
678 const auto Z3 = Z*Z*Z;
679 Z = (Z3 > 0.008856f).thenElse(Z3, (Z - (16/116.0f)) * (1/7.787f));
680
681 // adjust to D50 illuminant
682 X *= 0.96422f;
683 Y *= 1.00000f;
684 Z *= 0.82521f;
685
686 r = X;
687 g = Y;
688 b = Z;
689}
690
Mike Kleinb273fc42016-11-17 15:42:22 -0500691SI SkNf assert_in_tile(const SkNf& v, float limit) {
692 for (int i = 0; i < N; i++) {
693 SkASSERT(0 <= v[i] && v[i] < limit);
694 }
695 return v;
Mike Klein06a65e22016-11-17 12:39:09 -0500696}
Mike Kleinb273fc42016-11-17 15:42:22 -0500697
698SI SkNf clamp(const SkNf& v, float limit) {
699 SkNf result = SkNf::Max(0, SkNf::Min(v, limit - 0.5f));
700 return assert_in_tile(result, limit);
Mike Klein06a65e22016-11-17 12:39:09 -0500701}
Mike Kleinb273fc42016-11-17 15:42:22 -0500702SI SkNf repeat(const SkNf& v, float limit) {
703 SkNf result = v - (v/limit).floor()*limit;
Mike Kleinb273fc42016-11-17 15:42:22 -0500704 // For small negative v, (v/limit).floor()*limit can dominate v in the subtraction,
705 // which leaves result == limit. We want result < limit, so clamp it one ULP.
706 result = SkNf::Min(result, nextafterf(limit, 0));
Mike Kleinb273fc42016-11-17 15:42:22 -0500707 return assert_in_tile(result, limit);
708}
Mike Klein2e35e8a2016-11-18 15:47:22 -0500709SI SkNf mirror(const SkNf& v, float l/*imit*/) {
710 SkNf result = ((v - l) - ((v - l) / (2*l)).floor()*(2*l) - l).abs();
711 // Same deal as repeat.
712 result = SkNf::Min(result, nextafterf(l, 0));
713 return assert_in_tile(result, l);
714}
Mike Klein2cbc33d2016-11-28 16:30:30 -0500715STAGE( clamp_x) { r = clamp (r, *(const int*)ctx); }
716STAGE(repeat_x) { r = repeat(r, *(const int*)ctx); }
717STAGE(mirror_x) { r = mirror(r, *(const int*)ctx); }
718STAGE( clamp_y) { g = clamp (g, *(const int*)ctx); }
719STAGE(repeat_y) { g = repeat(g, *(const int*)ctx); }
720STAGE(mirror_y) { g = mirror(g, *(const int*)ctx); }
Mike Klein06a65e22016-11-17 12:39:09 -0500721
Mike Klein2cbc33d2016-11-28 16:30:30 -0500722STAGE(top_left) {
Mike Klein46e66a22016-11-21 16:19:34 -0500723 auto sc = (SkImageShaderContext*)ctx;
Mike Klein06a65e22016-11-17 12:39:09 -0500724
Mike Klein46e66a22016-11-21 16:19:34 -0500725 r.store(sc->x);
726 g.store(sc->y);
727
728 r -= 0.5f;
729 g -= 0.5f;
730
731 auto fx = r - r.floor(),
732 fy = g - g.floor();
Mike Kleinb04c3522016-11-28 11:55:58 -0500733 ((1.0f - fx) * (1.0f - fy)).store(sc->scale);
Mike Klein46e66a22016-11-21 16:19:34 -0500734};
Mike Klein2cbc33d2016-11-28 16:30:30 -0500735STAGE(top_right) {
Mike Kleinb04c3522016-11-28 11:55:58 -0500736 auto sc = (SkImageShaderContext*)ctx;
Mike Klein46e66a22016-11-21 16:19:34 -0500737
738 r = SkNf::Load(sc->x) + 0.5f;
739 g = SkNf::Load(sc->y) - 0.5f;
740
741 auto fx = r - r.floor(),
742 fy = g - g.floor();
Mike Kleinb04c3522016-11-28 11:55:58 -0500743 (fx * (1.0f - fy)).store(sc->scale);
Mike Klein46e66a22016-11-21 16:19:34 -0500744};
Mike Klein2cbc33d2016-11-28 16:30:30 -0500745STAGE(bottom_left) {
Mike Kleinb04c3522016-11-28 11:55:58 -0500746 auto sc = (SkImageShaderContext*)ctx;
Mike Klein46e66a22016-11-21 16:19:34 -0500747
748 r = SkNf::Load(sc->x) - 0.5f;
749 g = SkNf::Load(sc->y) + 0.5f;
750
751 auto fx = r - r.floor(),
752 fy = g - g.floor();
Mike Kleinb04c3522016-11-28 11:55:58 -0500753 ((1.0f - fx) * fy).store(sc->scale);
Mike Klein46e66a22016-11-21 16:19:34 -0500754};
Mike Klein2cbc33d2016-11-28 16:30:30 -0500755STAGE(bottom_right) {
Mike Kleinb04c3522016-11-28 11:55:58 -0500756 auto sc = (SkImageShaderContext*)ctx;
Mike Klein46e66a22016-11-21 16:19:34 -0500757
758 r = SkNf::Load(sc->x) + 0.5f;
759 g = SkNf::Load(sc->y) + 0.5f;
760
761 auto fx = r - r.floor(),
762 fy = g - g.floor();
Mike Kleinb04c3522016-11-28 11:55:58 -0500763 (fx * fy).store(sc->scale);
Mike Klein06a65e22016-11-17 12:39:09 -0500764};
Mike Klein2cbc33d2016-11-28 16:30:30 -0500765STAGE(accumulate) {
Mike Kleinb04c3522016-11-28 11:55:58 -0500766 auto sc = (const SkImageShaderContext*)ctx;
767
768 auto scale = SkNf::Load(sc->scale);
Mike Kleinc51c18f2016-11-28 23:17:19 -0500769 dr = SkNx_fma(scale, r, dr);
770 dg = SkNx_fma(scale, g, dg);
771 db = SkNx_fma(scale, b, db);
772 da = SkNx_fma(scale, a, da);
Mike Kleinb04c3522016-11-28 11:55:58 -0500773}
774
Mike Kleincb2c12b2016-11-22 13:22:48 -0500775template <typename T>
776SI SkNi offset_and_ptr(T** ptr, const void* ctx, const SkNf& x, const SkNf& y) {
Mike Klein46e66a22016-11-21 16:19:34 -0500777 auto sc = (const SkImageShaderContext*)ctx;
Mike Klein06a65e22016-11-17 12:39:09 -0500778
Mike Kleincb2c12b2016-11-22 13:22:48 -0500779 SkNi ix = SkNx_cast<int>(x),
780 iy = SkNx_cast<int>(y);
Mike Klein46e66a22016-11-21 16:19:34 -0500781 SkNi offset = iy*sc->stride + ix;
Mike Klein06a65e22016-11-17 12:39:09 -0500782
Mike Kleincb2c12b2016-11-22 13:22:48 -0500783 *ptr = (const T*)sc->pixels;
784 return offset;
785}
786
Mike Klein7a147342016-11-29 15:33:39 -0500787STAGE(gather_a8) {
788 const uint8_t* p;
789 SkNi offset = offset_and_ptr(&p, ctx, r, g);
790
791 r = g = b = 0.0f;
792 a = SkNx_cast<float>(gather(tail, p, offset)) * (1/255.0f);
793}
Mike Kleinf7657e92016-11-29 12:57:22 -0500794STAGE(gather_i8) {
795 auto sc = (const SkImageShaderContext*)ctx;
796 const uint8_t* p;
797 SkNi offset = offset_and_ptr(&p, sc, r, g);
798
799 SkNi ix = SkNx_cast<int>(gather(tail, p, offset));
800 from_8888(gather(tail, sc->ctable->readColors(), ix), &r, &g, &b, &a);
801}
Mike Klein2cbc33d2016-11-28 16:30:30 -0500802STAGE(gather_g8) {
Mike Klein6b77f1c2016-11-22 15:50:12 -0500803 const uint8_t* p;
804 SkNi offset = offset_and_ptr(&p, ctx, r, g);
805
Mike Klein56b50792016-11-29 08:14:49 -0500806 r = g = b = SkNx_cast<float>(gather(tail, p, offset)) * (1/255.0f);
Mike Kleinb04c3522016-11-28 11:55:58 -0500807 a = 1.0f;
Mike Klein6b77f1c2016-11-22 15:50:12 -0500808}
Mike Klein2cbc33d2016-11-28 16:30:30 -0500809STAGE(gather_565) {
Mike Kleincb2c12b2016-11-22 13:22:48 -0500810 const uint16_t* p;
811 SkNi offset = offset_and_ptr(&p, ctx, r, g);
812
Mike Klein56b50792016-11-29 08:14:49 -0500813 from_565(gather(tail, p, offset), &r, &g, &b);
Mike Kleinb04c3522016-11-28 11:55:58 -0500814 a = 1.0f;
Mike Kleincb2c12b2016-11-22 13:22:48 -0500815}
Mike Klein2cbc33d2016-11-28 16:30:30 -0500816STAGE(gather_4444) {
Mike Kleincb5338c2016-11-22 14:58:45 -0500817 const uint16_t* p;
818 SkNi offset = offset_and_ptr(&p, ctx, r, g);
819
Mike Klein56b50792016-11-29 08:14:49 -0500820 from_4444(gather(tail, p, offset), &r, &g, &b, &a);
Mike Kleincb5338c2016-11-22 14:58:45 -0500821}
Mike Klein2cbc33d2016-11-28 16:30:30 -0500822STAGE(gather_8888) {
Mike Kleincb5338c2016-11-22 14:58:45 -0500823 const uint32_t* p;
824 SkNi offset = offset_and_ptr(&p, ctx, r, g);
825
Mike Klein56b50792016-11-29 08:14:49 -0500826 from_8888(gather(tail, p, offset), &r, &g, &b, &a);
Mike Kleincb5338c2016-11-22 14:58:45 -0500827}
Mike Klein2cbc33d2016-11-28 16:30:30 -0500828STAGE(gather_f16) {
Mike Kleincb2c12b2016-11-22 13:22:48 -0500829 const uint64_t* p;
830 SkNi offset = offset_and_ptr(&p, ctx, r, g);
831
Mike Kleind5de0132016-11-28 09:33:02 -0500832 // f16 -> f32 conversion works best with tightly packed f16s,
833 // so we gather each component rather than using gather().
Mike Kleincb2c12b2016-11-22 13:22:48 -0500834 uint16_t R[N], G[N], B[N], A[N];
Mike Kleind5de0132016-11-28 09:33:02 -0500835 size_t n = tail ? tail : N;
836 for (size_t i = 0; i < n; i++) {
Mike Kleincb2c12b2016-11-22 13:22:48 -0500837 uint64_t rgba = p[offset[i]];
838 R[i] = rgba >> 0;
839 G[i] = rgba >> 16;
840 B[i] = rgba >> 32;
841 A[i] = rgba >> 48;
842 }
Mike Kleind5de0132016-11-28 09:33:02 -0500843 for (size_t i = n; i < N; i++) {
844 R[i] = G[i] = B[i] = A[i] = 0;
845 }
Mike Kleinb04c3522016-11-28 11:55:58 -0500846 r = SkHalfToFloat_finite_ftz(SkNh::Load(R));
847 g = SkHalfToFloat_finite_ftz(SkNh::Load(G));
848 b = SkHalfToFloat_finite_ftz(SkNh::Load(B));
849 a = SkHalfToFloat_finite_ftz(SkNh::Load(A));
Mike Kleincb2c12b2016-11-22 13:22:48 -0500850}
851
Mike Klein06a65e22016-11-17 12:39:09 -0500852
Mike Kleinaebfb452016-10-25 10:27:33 -0400853SI Fn enum_to_Fn(SkRasterPipeline::StockStage st) {
854 switch (st) {
855 #define M(stage) case SkRasterPipeline::stage: return stage;
856 SK_RASTER_PIPELINE_STAGES(M)
857 #undef M
858 }
859 SkASSERT(false);
860 return just_return;
861}
Mike Klein9161ef02016-10-04 14:03:27 -0400862
Mike Kleinbaaf8ad2016-09-29 09:04:15 -0400863namespace SK_OPTS_NS {
864
Mike Kleinad48a702016-11-07 17:16:21 -0500865 struct Memset16 {
866 uint16_t** dst;
867 uint16_t val;
Mike Kleinaf49b192016-11-15 08:52:04 -0500868 void operator()(size_t x, size_t, size_t n) { sk_memset16(*dst + x, val, n); }
Mike Kleinad48a702016-11-07 17:16:21 -0500869 };
Mike Kleinad48a702016-11-07 17:16:21 -0500870 struct Memset32 {
871 uint32_t** dst;
872 uint32_t val;
Mike Kleinaf49b192016-11-15 08:52:04 -0500873 void operator()(size_t x, size_t, size_t n) { sk_memset32(*dst + x, val, n); }
Mike Kleinad48a702016-11-07 17:16:21 -0500874 };
Mike Kleinad48a702016-11-07 17:16:21 -0500875 struct Memset64 {
876 uint64_t** dst;
877 uint64_t val;
Mike Kleinaf49b192016-11-15 08:52:04 -0500878 void operator()(size_t x, size_t, size_t n) { sk_memset64(*dst + x, val, n); }
Mike Kleinad48a702016-11-07 17:16:21 -0500879 };
880
Mike Kleinaf49b192016-11-15 08:52:04 -0500881 SI std::function<void(size_t, size_t, size_t)>
882 compile_pipeline(const SkRasterPipeline::Stage* stages, int nstages) {
mtklein125b2aa2016-11-04 13:41:34 -0700883 if (nstages == 2 && stages[0].stage == SkRasterPipeline::constant_color) {
884 SkPM4f src = *(const SkPM4f*)stages[0].ctx;
885 void* dst = stages[1].ctx;
886 switch (stages[1].stage) {
Mike Kleinad48a702016-11-07 17:16:21 -0500887 case SkRasterPipeline::store_565:
888 return Memset16{(uint16_t**)dst, SkPackRGB16(src.r() * SK_R16_MASK + 0.5f,
889 src.g() * SK_G16_MASK + 0.5f,
890 src.b() * SK_B16_MASK + 0.5f)};
Mike Kleine03339a2016-11-28 13:24:27 -0500891 case SkRasterPipeline::store_8888:
892 return Memset32{(uint32_t**)dst, Sk4f_toL32(src.to4f())};
mtklein125b2aa2016-11-04 13:41:34 -0700893
Mike Kleinad48a702016-11-07 17:16:21 -0500894 case SkRasterPipeline::store_f16:
895 return Memset64{(uint64_t**)dst, src.toF16()};
mtklein125b2aa2016-11-04 13:41:34 -0700896
897 default: break;
898 }
899 }
900
Mike Kleine9f74b82016-10-25 13:31:21 -0400901 struct Compiled {
902 Compiled(const SkRasterPipeline::Stage* stages, int nstages) {
903 if (nstages == 0) {
904 return;
905 }
Mike Klein2cbc33d2016-11-28 16:30:30 -0500906 fStart = enum_to_Fn(stages[0].stage);
Mike Kleine9f74b82016-10-25 13:31:21 -0400907 for (int i = 0; i < nstages-1; i++) {
Mike Klein2cbc33d2016-11-28 16:30:30 -0500908 fStages[i].next = enum_to_Fn(stages[i+1].stage);
909 fStages[i].ctx = stages[i].ctx;
Mike Kleine9f74b82016-10-25 13:31:21 -0400910 }
Mike Klein2cbc33d2016-11-28 16:30:30 -0500911 fStages[nstages-1].next = just_return;
912 fStages[nstages-1].ctx = stages[nstages-1].ctx;
Mike Klein050ffa92016-10-20 16:20:46 -0400913 }
Mike Kleinaebfb452016-10-25 10:27:33 -0400914
Mike Kleinaf49b192016-11-15 08:52:04 -0500915 void operator()(size_t x, size_t y, size_t n) {
Mike Kleinaf49b192016-11-15 08:52:04 -0500916 float dx[] = { 0,1,2,3,4,5,6,7 };
Mike Klein0f91ea42016-11-15 10:31:38 -0500917 SkNf X = SkNf(x) + SkNf::Load(dx) + 0.5f,
Mike Kleinf7f883b2016-11-21 15:09:45 -0500918 Y = SkNf(y) + 0.5f,
919 _0 = SkNf(0),
920 _1 = SkNf(1);
Mike Kleinaf49b192016-11-15 08:52:04 -0500921
Mike Kleine9f74b82016-10-25 13:31:21 -0400922 while (n >= N) {
Mike Klein2cbc33d2016-11-28 16:30:30 -0500923 fStart(fStages, x*N, X,Y,_1,_0, _0,_0,_0,_0);
Mike Klein0f91ea42016-11-15 10:31:38 -0500924 X += (float)N;
Mike Kleine9f74b82016-10-25 13:31:21 -0400925 x += N;
926 n -= N;
927 }
928 if (n) {
Mike Klein2cbc33d2016-11-28 16:30:30 -0500929 fStart(fStages, x*N+n, X,Y,_1,_0, _0,_0,_0,_0);
Mike Kleine9f74b82016-10-25 13:31:21 -0400930 }
Mike Klein050ffa92016-10-20 16:20:46 -0400931 }
Mike Kleinbaaf8ad2016-09-29 09:04:15 -0400932
Mike Klein2cbc33d2016-11-28 16:30:30 -0500933 Fn fStart = just_return;
934 Stage fStages[SkRasterPipeline::kMaxStages];
Mike Kleine9f74b82016-10-25 13:31:21 -0400935
936 } fn { stages, nstages };
937 return fn;
Mike Kleinbaaf8ad2016-09-29 09:04:15 -0400938 }
939
Mike Kleinaebfb452016-10-25 10:27:33 -0400940} // namespace SK_OPTS_NS
Mike Kleinbaaf8ad2016-09-29 09:04:15 -0400941
Mike Klein04adfda2016-10-12 09:52:55 -0400942#undef SI
943#undef STAGE
944#undef RGBA_XFERMODE
945#undef RGB_XFERMODE
Mike Klein9161ef02016-10-04 14:03:27 -0400946
Mike Kleinbaaf8ad2016-09-29 09:04:15 -0400947#endif//SkRasterPipeline_opts_DEFINED