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