1 /*
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 "include/private/SkImageInfoPriv.h"
9 #include "include/private/SkNx.h"
10 #include "include/private/SkTemplates.h"
11 #include "src/core/SkColorSpacePriv.h"
12 #include "src/core/SkOpts.h"
13 #include "src/core/SkRasterPipeline.h"
14 #include <algorithm>
15
SkRasterPipeline(SkArenaAlloc * alloc)16 SkRasterPipeline::SkRasterPipeline(SkArenaAlloc* alloc) : fAlloc(alloc) {
17 this->reset();
18 }
reset()19 void SkRasterPipeline::reset() {
20 fStages = nullptr;
21 fNumStages = 0;
22 fSlotsNeeded = 1; // We always need one extra slot for just_return().
23 }
24
append(StockStage stage,void * ctx)25 void SkRasterPipeline::append(StockStage stage, void* ctx) {
26 SkASSERT(stage != uniform_color); // Please use append_constant_color().
27 SkASSERT(stage != unbounded_uniform_color); // Please use append_constant_color().
28 SkASSERT(stage != set_rgb); // Please use append_set_rgb().
29 SkASSERT(stage != unbounded_set_rgb); // Please use append_set_rgb().
30 SkASSERT(stage != clamp_gamut); // Please use append_gamut_clamp_if_normalized().
31 SkASSERT(stage != parametric); // Please use append_transfer_function().
32 SkASSERT(stage != gamma_); // Please use append_transfer_function().
33 SkASSERT(stage != PQish); // Please use append_transfer_function().
34 SkASSERT(stage != HLGish); // Please use append_transfer_function().
35 SkASSERT(stage != HLGinvish); // Please use append_transfer_function().
36 this->unchecked_append(stage, ctx);
37 }
unchecked_append(StockStage stage,void * ctx)38 void SkRasterPipeline::unchecked_append(StockStage stage, void* ctx) {
39 fStages = fAlloc->make<StageList>( StageList{fStages, stage, ctx} );
40 fNumStages += 1;
41 fSlotsNeeded += ctx ? 2 : 1;
42 }
append(StockStage stage,uintptr_t ctx)43 void SkRasterPipeline::append(StockStage stage, uintptr_t ctx) {
44 void* ptrCtx;
45 memcpy(&ptrCtx, &ctx, sizeof(ctx));
46 this->append(stage, ptrCtx);
47 }
48
extend(const SkRasterPipeline & src)49 void SkRasterPipeline::extend(const SkRasterPipeline& src) {
50 if (src.empty()) {
51 return;
52 }
53 auto stages = fAlloc->makeArrayDefault<StageList>(src.fNumStages);
54
55 int n = src.fNumStages;
56 const StageList* st = src.fStages;
57 while (n --> 1) {
58 stages[n] = *st;
59 stages[n].prev = &stages[n-1];
60 st = st->prev;
61 }
62 stages[0] = *st;
63 stages[0].prev = fStages;
64
65 fStages = &stages[src.fNumStages - 1];
66 fNumStages += src.fNumStages;
67 fSlotsNeeded += src.fSlotsNeeded - 1; // Don't double count just_returns().
68 }
69
dump() const70 void SkRasterPipeline::dump() const {
71 SkDebugf("SkRasterPipeline, %d stages\n", fNumStages);
72 std::vector<const char*> stages;
73 for (auto st = fStages; st; st = st->prev) {
74 const char* name = "";
75 switch (st->stage) {
76 #define M(x) case x: name = #x; break;
77 SK_RASTER_PIPELINE_STAGES(M)
78 #undef M
79 }
80 stages.push_back(name);
81 }
82 std::reverse(stages.begin(), stages.end());
83 for (const char* name : stages) {
84 SkDebugf("\t%s\n", name);
85 }
86 SkDebugf("\n");
87 }
88
append_set_rgb(SkArenaAlloc * alloc,const float rgb[3])89 void SkRasterPipeline::append_set_rgb(SkArenaAlloc* alloc, const float rgb[3]) {
90 auto arg = alloc->makeArrayDefault<float>(3);
91 arg[0] = rgb[0];
92 arg[1] = rgb[1];
93 arg[2] = rgb[2];
94
95 auto stage = unbounded_set_rgb;
96 if (0 <= rgb[0] && rgb[0] <= 1 &&
97 0 <= rgb[1] && rgb[1] <= 1 &&
98 0 <= rgb[2] && rgb[2] <= 1)
99 {
100 stage = set_rgb;
101 }
102
103 this->unchecked_append(stage, arg);
104 }
105
append_constant_color(SkArenaAlloc * alloc,const float rgba[4])106 void SkRasterPipeline::append_constant_color(SkArenaAlloc* alloc, const float rgba[4]) {
107 // r,g,b might be outside [0,1], but alpha should probably always be in [0,1].
108 SkASSERT(0 <= rgba[3] && rgba[3] <= 1);
109
110 if (rgba[0] == 0 && rgba[1] == 0 && rgba[2] == 0 && rgba[3] == 1) {
111 this->append(black_color);
112 } else if (rgba[0] == 1 && rgba[1] == 1 && rgba[2] == 1 && rgba[3] == 1) {
113 this->append(white_color);
114 } else {
115 auto ctx = alloc->make<SkRasterPipeline_UniformColorCtx>();
116 Sk4f color = Sk4f::Load(rgba);
117 color.store(&ctx->r);
118
119 // uniform_color requires colors in range and can go lowp,
120 // while unbounded_uniform_color supports out-of-range colors too but not lowp.
121 if (0 <= rgba[0] && rgba[0] <= rgba[3] &&
122 0 <= rgba[1] && rgba[1] <= rgba[3] &&
123 0 <= rgba[2] && rgba[2] <= rgba[3]) {
124 // To make loads more direct, we store 8-bit values in 16-bit slots.
125 color = color * 255.0f + 0.5f;
126 ctx->rgba[0] = (uint16_t)color[0];
127 ctx->rgba[1] = (uint16_t)color[1];
128 ctx->rgba[2] = (uint16_t)color[2];
129 ctx->rgba[3] = (uint16_t)color[3];
130 this->unchecked_append(uniform_color, ctx);
131 } else {
132 this->unchecked_append(unbounded_uniform_color, ctx);
133 }
134 }
135 }
136
append_matrix(SkArenaAlloc * alloc,const SkMatrix & matrix)137 void SkRasterPipeline::append_matrix(SkArenaAlloc* alloc, const SkMatrix& matrix) {
138 SkMatrix::TypeMask mt = matrix.getType();
139
140 if (mt == SkMatrix::kIdentity_Mask) {
141 return;
142 }
143 if (mt == SkMatrix::kTranslate_Mask) {
144 float* trans = alloc->makeArrayDefault<float>(2);
145 trans[0] = matrix.getTranslateX();
146 trans[1] = matrix.getTranslateY();
147 this->append(SkRasterPipeline::matrix_translate, trans);
148 } else if ((mt | (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) ==
149 (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) {
150 float* scaleTrans = alloc->makeArrayDefault<float>(4);
151 scaleTrans[0] = matrix.getScaleX();
152 scaleTrans[1] = matrix.getScaleY();
153 scaleTrans[2] = matrix.getTranslateX();
154 scaleTrans[3] = matrix.getTranslateY();
155 this->append(SkRasterPipeline::matrix_scale_translate, scaleTrans);
156 } else {
157 float* storage = alloc->makeArrayDefault<float>(9);
158 if (matrix.asAffine(storage)) {
159 // note: asAffine and the 2x3 stage really only need 6 entries
160 this->append(SkRasterPipeline::matrix_2x3, storage);
161 } else {
162 matrix.get9(storage);
163 this->append(SkRasterPipeline::matrix_perspective, storage);
164 }
165 }
166 }
167
append_load(SkColorType ct,const SkRasterPipeline_MemoryCtx * ctx)168 void SkRasterPipeline::append_load(SkColorType ct, const SkRasterPipeline_MemoryCtx* ctx) {
169 switch (ct) {
170 case kUnknown_SkColorType: SkASSERT(false); break;
171
172 case kAlpha_8_SkColorType: this->append(load_a8, ctx); break;
173 case kA16_unorm_SkColorType: this->append(load_a16, ctx); break;
174 case kA16_float_SkColorType: this->append(load_af16, ctx); break;
175 case kRGB_565_SkColorType: this->append(load_565, ctx); break;
176 case kARGB_4444_SkColorType: this->append(load_4444, ctx); break;
177 case kR8G8_unorm_SkColorType: this->append(load_rg88, ctx); break;
178 case kR16G16_unorm_SkColorType: this->append(load_rg1616, ctx); break;
179 case kR16G16_float_SkColorType: this->append(load_rgf16, ctx); break;
180 case kRGBA_8888_SkColorType: this->append(load_8888, ctx); break;
181 case kRGBA_1010102_SkColorType: this->append(load_1010102, ctx); break;
182 case kR16G16B16A16_unorm_SkColorType:this->append(load_16161616,ctx); break;
183 case kRGBA_F16Norm_SkColorType:
184 case kRGBA_F16_SkColorType: this->append(load_f16, ctx); break;
185 case kRGBA_F32_SkColorType: this->append(load_f32, ctx); break;
186
187 case kGray_8_SkColorType: this->append(load_a8, ctx);
188 this->append(alpha_to_gray);
189 break;
190
191 case kRGB_888x_SkColorType: this->append(load_8888, ctx);
192 this->append(force_opaque);
193 break;
194
195 case kBGRA_1010102_SkColorType: this->append(load_1010102, ctx);
196 this->append(swap_rb);
197 break;
198
199 case kRGB_101010x_SkColorType: this->append(load_1010102, ctx);
200 this->append(force_opaque);
201 break;
202
203 case kBGR_101010x_SkColorType: this->append(load_1010102, ctx);
204 this->append(force_opaque);
205 this->append(swap_rb);
206 break;
207
208 case kBGRA_8888_SkColorType: this->append(load_8888, ctx);
209 this->append(swap_rb);
210 break;
211 }
212 }
213
append_load_dst(SkColorType ct,const SkRasterPipeline_MemoryCtx * ctx)214 void SkRasterPipeline::append_load_dst(SkColorType ct, const SkRasterPipeline_MemoryCtx* ctx) {
215 switch (ct) {
216 case kUnknown_SkColorType: SkASSERT(false); break;
217
218 case kAlpha_8_SkColorType: this->append(load_a8_dst, ctx); break;
219 case kA16_unorm_SkColorType: this->append(load_a16_dst, ctx); break;
220 case kA16_float_SkColorType: this->append(load_af16_dst, ctx); break;
221 case kRGB_565_SkColorType: this->append(load_565_dst, ctx); break;
222 case kARGB_4444_SkColorType: this->append(load_4444_dst, ctx); break;
223 case kR8G8_unorm_SkColorType: this->append(load_rg88_dst, ctx); break;
224 case kR16G16_unorm_SkColorType: this->append(load_rg1616_dst, ctx); break;
225 case kR16G16_float_SkColorType: this->append(load_rgf16_dst, ctx); break;
226 case kRGBA_8888_SkColorType: this->append(load_8888_dst, ctx); break;
227 case kRGBA_1010102_SkColorType: this->append(load_1010102_dst, ctx); break;
228 case kR16G16B16A16_unorm_SkColorType: this->append(load_16161616_dst,ctx); break;
229 case kRGBA_F16Norm_SkColorType:
230 case kRGBA_F16_SkColorType: this->append(load_f16_dst, ctx); break;
231 case kRGBA_F32_SkColorType: this->append(load_f32_dst, ctx); break;
232
233 case kGray_8_SkColorType: this->append(load_a8_dst, ctx);
234 this->append(alpha_to_gray_dst);
235 break;
236
237 case kRGB_888x_SkColorType: this->append(load_8888_dst, ctx);
238 this->append(force_opaque_dst);
239 break;
240
241 case kBGRA_1010102_SkColorType: this->append(load_1010102_dst, ctx);
242 this->append(swap_rb_dst);
243 break;
244
245 case kRGB_101010x_SkColorType: this->append(load_1010102_dst, ctx);
246 this->append(force_opaque_dst);
247 break;
248
249 case kBGR_101010x_SkColorType: this->append(load_1010102_dst, ctx);
250 this->append(force_opaque_dst);
251 this->append(swap_rb_dst);
252 break;
253
254 case kBGRA_8888_SkColorType: this->append(load_8888_dst, ctx);
255 this->append(swap_rb_dst);
256 break;
257 }
258 }
259
append_store(SkColorType ct,const SkRasterPipeline_MemoryCtx * ctx)260 void SkRasterPipeline::append_store(SkColorType ct, const SkRasterPipeline_MemoryCtx* ctx) {
261 switch (ct) {
262 case kUnknown_SkColorType: SkASSERT(false); break;
263
264 case kAlpha_8_SkColorType: this->append(store_a8, ctx); break;
265 case kA16_unorm_SkColorType: this->append(store_a16, ctx); break;
266 case kA16_float_SkColorType: this->append(store_af16, ctx); break;
267 case kRGB_565_SkColorType: this->append(store_565, ctx); break;
268 case kARGB_4444_SkColorType: this->append(store_4444, ctx); break;
269 case kR8G8_unorm_SkColorType: this->append(store_rg88, ctx); break;
270 case kR16G16_unorm_SkColorType: this->append(store_rg1616, ctx); break;
271 case kR16G16_float_SkColorType: this->append(store_rgf16, ctx); break;
272 case kRGBA_8888_SkColorType: this->append(store_8888, ctx); break;
273 case kRGBA_1010102_SkColorType: this->append(store_1010102, ctx); break;
274 case kR16G16B16A16_unorm_SkColorType: this->append(store_16161616,ctx); break;
275 case kRGBA_F16Norm_SkColorType:
276 case kRGBA_F16_SkColorType: this->append(store_f16, ctx); break;
277 case kRGBA_F32_SkColorType: this->append(store_f32, ctx); break;
278
279 case kRGB_888x_SkColorType: this->append(force_opaque);
280 this->append(store_8888, ctx);
281 break;
282
283 case kBGRA_1010102_SkColorType: this->append(swap_rb);
284 this->append(store_1010102, ctx);
285 break;
286
287 case kRGB_101010x_SkColorType: this->append(force_opaque);
288 this->append(store_1010102, ctx);
289 break;
290
291 case kBGR_101010x_SkColorType: this->append(force_opaque);
292 this->append(swap_rb);
293 this->append(store_1010102, ctx);
294 break;
295
296 case kGray_8_SkColorType: this->append(bt709_luminance_or_luma_to_alpha);
297 this->append(store_a8, ctx);
298 break;
299
300 case kBGRA_8888_SkColorType: this->append(swap_rb);
301 this->append(store_8888, ctx);
302 break;
303 }
304 }
305
append_transfer_function(const skcms_TransferFunction & tf)306 void SkRasterPipeline::append_transfer_function(const skcms_TransferFunction& tf) {
307 void* ctx = const_cast<void*>(static_cast<const void*>(&tf));
308 switch (classify_transfer_fn(tf)) {
309 case Bad_TF: SkASSERT(false); break;
310
311 case TFKind::sRGBish_TF:
312 if (tf.a == 1 && tf.b == 0 && tf.c == 0 && tf.d == 0 && tf.e == 0 && tf.f == 0) {
313 this->unchecked_append(gamma_, ctx);
314 } else {
315 this->unchecked_append(parametric, ctx);
316 }
317 break;
318 case PQish_TF: this->unchecked_append(PQish, ctx); break;
319 case HLGish_TF: this->unchecked_append(HLGish, ctx); break;
320 case HLGinvish_TF: this->unchecked_append(HLGinvish, ctx); break;
321 }
322 }
323
324 // Clamp premul values to [0,alpha] (logical [0,1]) to avoid the confusing
325 // scenario of being able to store a logical color channel > 1.0 when alpha < 1.0.
326 // Most software that works with normalized premul values expect r,g,b channels all <= a.
327 //
328 // In addition, GL clamps all its color channels to limits of the format just
329 // before the blend step (~here). To match that auto-clamp, we clamp alpha to
330 // [0,1] too, just in case someone gave us a crazy alpha.
append_gamut_clamp_if_normalized(const SkImageInfo & info)331 void SkRasterPipeline::append_gamut_clamp_if_normalized(const SkImageInfo& info) {
332 if (info.alphaType() == kPremul_SkAlphaType && SkColorTypeIsNormalized(info.colorType())) {
333 this->unchecked_append(SkRasterPipeline::clamp_gamut, nullptr);
334 }
335 }
336
build_pipeline(void ** ip) const337 SkRasterPipeline::StartPipelineFn SkRasterPipeline::build_pipeline(void** ip) const {
338 // We'll try to build a lowp pipeline, but if that fails fallback to a highp float pipeline.
339 void** reset_point = ip;
340
341 // Stages are stored backwards in fStages, so we reverse here, back to front.
342 *--ip = (void*)SkOpts::just_return_lowp;
343 for (const StageList* st = fStages; st; st = st->prev) {
344 if (auto fn = SkOpts::stages_lowp[st->stage]) {
345 if (st->ctx) {
346 *--ip = st->ctx;
347 }
348 *--ip = (void*)fn;
349 } else {
350 ip = reset_point;
351 break;
352 }
353 }
354 if (ip != reset_point) {
355 return SkOpts::start_pipeline_lowp;
356 }
357
358 *--ip = (void*)SkOpts::just_return_highp;
359 for (const StageList* st = fStages; st; st = st->prev) {
360 if (st->ctx) {
361 *--ip = st->ctx;
362 }
363 *--ip = (void*)SkOpts::stages_highp[st->stage];
364 }
365 return SkOpts::start_pipeline_highp;
366 }
367
run(size_t x,size_t y,size_t w,size_t h) const368 void SkRasterPipeline::run(size_t x, size_t y, size_t w, size_t h) const {
369 if (this->empty()) {
370 return;
371 }
372
373 // Best to not use fAlloc here... we can't bound how often run() will be called.
374 SkAutoSTMalloc<64, void*> program(fSlotsNeeded);
375
376 auto start_pipeline = this->build_pipeline(program.get() + fSlotsNeeded);
377 start_pipeline(x,y,x+w,y+h, program.get());
378 }
379
compile() const380 std::function<void(size_t, size_t, size_t, size_t)> SkRasterPipeline::compile() const {
381 if (this->empty()) {
382 return [](size_t, size_t, size_t, size_t) {};
383 }
384
385 void** program = fAlloc->makeArray<void*>(fSlotsNeeded);
386
387 auto start_pipeline = this->build_pipeline(program + fSlotsNeeded);
388 return [=](size_t x, size_t y, size_t w, size_t h) {
389 start_pipeline(x,y,x+w,y+h, program);
390 };
391 }
392