• 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 kRGB_888x_SkColorType:          this->append(load_8888, ctx);
194                                              this->append(force_opaque);
195                                              break;
196 
197         case kBGRA_1010102_SkColorType:      this->append(load_1010102, ctx);
198                                              this->append(swap_rb);
199                                              break;
200 
201         case kRGB_101010x_SkColorType:       this->append(load_1010102, ctx);
202                                              this->append(force_opaque);
203                                              break;
204 
205         case kBGR_101010x_SkColorType:       this->append(load_1010102, ctx);
206                                              this->append(force_opaque);
207                                              this->append(swap_rb);
208                                              break;
209 
210         case kBGRA_8888_SkColorType:         this->append(load_8888, ctx);
211                                              this->append(swap_rb);
212                                              break;
213 
214         case kSRGBA_8888_SkColorType:
215             this->append(load_8888, ctx);
216             this->append_transfer_function(*skcms_sRGB_TransferFunction());
217             break;
218     }
219 }
220 
append_load_dst(SkColorType ct,const SkRasterPipeline_MemoryCtx * ctx)221 void SkRasterPipeline::append_load_dst(SkColorType ct, const SkRasterPipeline_MemoryCtx* ctx) {
222     switch (ct) {
223         case kUnknown_SkColorType: SkASSERT(false); break;
224 
225         case kAlpha_8_SkColorType:            this->append(load_a8_dst,      ctx); break;
226         case kA16_unorm_SkColorType:          this->append(load_a16_dst,     ctx); break;
227         case kA16_float_SkColorType:          this->append(load_af16_dst,    ctx); break;
228         case kRGB_565_SkColorType:            this->append(load_565_dst,     ctx); break;
229         case kARGB_4444_SkColorType:          this->append(load_4444_dst,    ctx); break;
230         case kR8G8_unorm_SkColorType:         this->append(load_rg88_dst,    ctx); break;
231         case kR16G16_unorm_SkColorType:       this->append(load_rg1616_dst,  ctx); break;
232         case kR16G16_float_SkColorType:       this->append(load_rgf16_dst,   ctx); break;
233         case kRGBA_8888_SkColorType:          this->append(load_8888_dst,    ctx); break;
234         case kRGBA_1010102_SkColorType:       this->append(load_1010102_dst, ctx); break;
235         case kR16G16B16A16_unorm_SkColorType: this->append(load_16161616_dst,ctx); break;
236         case kRGBA_F16Norm_SkColorType:
237         case kRGBA_F16_SkColorType:           this->append(load_f16_dst,     ctx); break;
238         case kRGBA_F32_SkColorType:           this->append(load_f32_dst,     ctx); break;
239 
240         case kGray_8_SkColorType:             this->append(load_a8_dst, ctx);
241                                               this->append(alpha_to_gray_dst);
242                                               break;
243 
244         case kRGB_888x_SkColorType:           this->append(load_8888_dst, ctx);
245                                               this->append(force_opaque_dst);
246                                               break;
247 
248         case kBGRA_1010102_SkColorType:       this->append(load_1010102_dst, ctx);
249                                               this->append(swap_rb_dst);
250                                               break;
251 
252         case kRGB_101010x_SkColorType:        this->append(load_1010102_dst, ctx);
253                                               this->append(force_opaque_dst);
254                                               break;
255 
256         case kBGR_101010x_SkColorType:        this->append(load_1010102_dst, ctx);
257                                               this->append(force_opaque_dst);
258                                               this->append(swap_rb_dst);
259                                               break;
260 
261         case kBGRA_8888_SkColorType:          this->append(load_8888_dst, ctx);
262                                               this->append(swap_rb_dst);
263                                               break;
264 
265         case kSRGBA_8888_SkColorType:
266             // TODO: We could remove the double-swap if we had _dst versions of all the TF stages
267             this->append(load_8888_dst, ctx);
268             this->append(swap_src_dst);
269             this->append_transfer_function(*skcms_sRGB_TransferFunction());
270             this->append(swap_src_dst);
271             break;
272     }
273 }
274 
append_store(SkColorType ct,const SkRasterPipeline_MemoryCtx * ctx)275 void SkRasterPipeline::append_store(SkColorType ct, const SkRasterPipeline_MemoryCtx* ctx) {
276     switch (ct) {
277         case kUnknown_SkColorType: SkASSERT(false); break;
278 
279         case kAlpha_8_SkColorType:            this->append(store_a8,      ctx); break;
280         case kA16_unorm_SkColorType:          this->append(store_a16,     ctx); break;
281         case kA16_float_SkColorType:          this->append(store_af16,    ctx); break;
282         case kRGB_565_SkColorType:            this->append(store_565,     ctx); break;
283         case kARGB_4444_SkColorType:          this->append(store_4444,    ctx); break;
284         case kR8G8_unorm_SkColorType:         this->append(store_rg88,    ctx); break;
285         case kR16G16_unorm_SkColorType:       this->append(store_rg1616,  ctx); break;
286         case kR16G16_float_SkColorType:       this->append(store_rgf16,   ctx); break;
287         case kRGBA_8888_SkColorType:          this->append(store_8888,    ctx); break;
288         case kRGBA_1010102_SkColorType:       this->append(store_1010102, ctx); break;
289         case kR16G16B16A16_unorm_SkColorType: this->append(store_16161616,ctx); break;
290         case kRGBA_F16Norm_SkColorType:
291         case kRGBA_F16_SkColorType:           this->append(store_f16,     ctx); break;
292         case kRGBA_F32_SkColorType:           this->append(store_f32,     ctx); break;
293 
294         case kRGB_888x_SkColorType:           this->append(force_opaque);
295                                               this->append(store_8888, ctx);
296                                               break;
297 
298         case kBGRA_1010102_SkColorType:       this->append(swap_rb);
299                                               this->append(store_1010102, ctx);
300                                               break;
301 
302         case kRGB_101010x_SkColorType:        this->append(force_opaque);
303                                               this->append(store_1010102, ctx);
304                                               break;
305 
306         case kBGR_101010x_SkColorType:        this->append(force_opaque);
307                                               this->append(swap_rb);
308                                               this->append(store_1010102, ctx);
309                                               break;
310 
311         case kGray_8_SkColorType:             this->append(bt709_luminance_or_luma_to_alpha);
312                                               this->append(store_a8, ctx);
313                                               break;
314 
315         case kBGRA_8888_SkColorType:          this->append(swap_rb);
316                                               this->append(store_8888, ctx);
317                                               break;
318 
319         case kSRGBA_8888_SkColorType:
320             this->append_transfer_function(*skcms_sRGB_Inverse_TransferFunction());
321             this->append(store_8888, ctx);
322             break;
323     }
324 }
325 
append_transfer_function(const skcms_TransferFunction & tf)326 void SkRasterPipeline::append_transfer_function(const skcms_TransferFunction& tf) {
327     void* ctx = const_cast<void*>(static_cast<const void*>(&tf));
328     switch (classify_transfer_fn(tf)) {
329         case Bad_TF: SkASSERT(false); break;
330 
331         case TFKind::sRGBish_TF:
332             if (tf.a == 1 && tf.b == 0 && tf.c == 0 && tf.d == 0 && tf.e == 0 && tf.f == 0) {
333                 this->unchecked_append(gamma_, ctx);
334             } else {
335                 this->unchecked_append(parametric, ctx);
336             }
337             break;
338         case PQish_TF:     this->unchecked_append(PQish,     ctx); break;
339         case HLGish_TF:    this->unchecked_append(HLGish,    ctx); break;
340         case HLGinvish_TF: this->unchecked_append(HLGinvish, ctx); break;
341     }
342 }
343 
344 // Clamp premul values to [0,alpha] (logical [0,1]) to avoid the confusing
345 // scenario of being able to store a logical color channel > 1.0 when alpha < 1.0.
346 // Most software that works with normalized premul values expect r,g,b channels all <= a.
347 //
348 // In addition, GL clamps all its color channels to limits of the format just
349 // before the blend step (~here).  To match that auto-clamp, we clamp alpha to
350 // [0,1] too, just in case someone gave us a crazy alpha.
append_gamut_clamp_if_normalized(const SkImageInfo & info)351 void SkRasterPipeline::append_gamut_clamp_if_normalized(const SkImageInfo& info) {
352     if (info.alphaType() == kPremul_SkAlphaType && SkColorTypeIsNormalized(info.colorType())) {
353         this->unchecked_append(SkRasterPipeline::clamp_gamut, nullptr);
354     }
355 }
356 
build_pipeline(void ** ip) const357 SkRasterPipeline::StartPipelineFn SkRasterPipeline::build_pipeline(void** ip) const {
358     if (!gForceHighPrecisionRasterPipeline) {
359         // We'll try to build a lowp pipeline, but if that fails fallback to a highp float pipeline.
360         void** reset_point = ip;
361 
362         // Stages are stored backwards in fStages, so we reverse here, back to front.
363         *--ip = (void*)SkOpts::just_return_lowp;
364         for (const StageList* st = fStages; st; st = st->prev) {
365             if (auto fn = SkOpts::stages_lowp[st->stage]) {
366                 if (st->ctx) {
367                     *--ip = st->ctx;
368                 }
369                 *--ip = (void*)fn;
370             } else {
371                 ip = reset_point;
372                 break;
373             }
374         }
375         if (ip != reset_point) {
376             return SkOpts::start_pipeline_lowp;
377         }
378     }
379 
380     *--ip = (void*)SkOpts::just_return_highp;
381     for (const StageList* st = fStages; st; st = st->prev) {
382         if (st->ctx) {
383             *--ip = st->ctx;
384         }
385         *--ip = (void*)SkOpts::stages_highp[st->stage];
386     }
387     return SkOpts::start_pipeline_highp;
388 }
389 
run(size_t x,size_t y,size_t w,size_t h) const390 void SkRasterPipeline::run(size_t x, size_t y, size_t w, size_t h) const {
391     if (this->empty()) {
392         return;
393     }
394 
395     // Best to not use fAlloc here... we can't bound how often run() will be called.
396     SkAutoSTMalloc<64, void*> program(fSlotsNeeded);
397 
398     auto start_pipeline = this->build_pipeline(program.get() + fSlotsNeeded);
399     start_pipeline(x,y,x+w,y+h, program.get());
400 }
401 
compile() const402 std::function<void(size_t, size_t, size_t, size_t)> SkRasterPipeline::compile() const {
403     if (this->empty()) {
404         return [](size_t, size_t, size_t, size_t) {};
405     }
406 
407     void** program = fAlloc->makeArray<void*>(fSlotsNeeded);
408 
409     auto start_pipeline = this->build_pipeline(program + fSlotsNeeded);
410     return [=](size_t x, size_t y, size_t w, size_t h) {
411         start_pipeline(x,y,x+w,y+h, program);
412     };
413 }
414