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 "SkRasterPipeline.h"
9 #include "SkOpts.h"
10 #include <algorithm>
11 
SkRasterPipeline(SkArenaAlloc * alloc)12 SkRasterPipeline::SkRasterPipeline(SkArenaAlloc* alloc) : fAlloc(alloc) {
13     this->reset();
14 }
reset()15 void SkRasterPipeline::reset() {
16     fStages      = nullptr;
17     fNumStages   = 0;
18     fSlotsNeeded = 1;  // We always need one extra slot for just_return().
19 }
20 
append(StockStage stage,void * ctx)21 void SkRasterPipeline::append(StockStage stage, void* ctx) {
22     SkASSERT(stage !=           uniform_color);  // Please use append_constant_color().
23     SkASSERT(stage != unbounded_uniform_color);  // Please use append_constant_color().
24     SkASSERT(stage !=                 set_rgb);  // Please use append_set_rgb().
25     SkASSERT(stage !=       unbounded_set_rgb);  // Please use append_set_rgb().
26     SkASSERT(stage !=             clamp_gamut);  // Please use append_gamut_clamp_if_normalized().
27     this->unchecked_append(stage, ctx);
28 }
unchecked_append(StockStage stage,void * ctx)29 void SkRasterPipeline::unchecked_append(StockStage stage, void* ctx) {
30     fStages = fAlloc->make<StageList>( StageList{fStages, (uint64_t) stage, ctx, false} );
31     fNumStages   += 1;
32     fSlotsNeeded += ctx ? 2 : 1;
33 }
append(void * fn,void * ctx)34 void SkRasterPipeline::append(void* fn, void* ctx) {
35     fStages = fAlloc->make<StageList>( StageList{fStages, (uint64_t) fn, ctx, true} );
36     fNumStages   += 1;
37     fSlotsNeeded += ctx ? 2 : 1;
38 }
39 
extend(const SkRasterPipeline & src)40 void SkRasterPipeline::extend(const SkRasterPipeline& src) {
41     if (src.empty()) {
42         return;
43     }
44     auto stages = fAlloc->makeArrayDefault<StageList>(src.fNumStages);
45 
46     int n = src.fNumStages;
47     const StageList* st = src.fStages;
48     while (n --> 1) {
49         stages[n]      = *st;
50         stages[n].prev = &stages[n-1];
51         st = st->prev;
52     }
53     stages[0]      = *st;
54     stages[0].prev = fStages;
55 
56     fStages = &stages[src.fNumStages - 1];
57     fNumStages   += src.fNumStages;
58     fSlotsNeeded += src.fSlotsNeeded - 1;  // Don't double count just_returns().
59 }
60 
dump() const61 void SkRasterPipeline::dump() const {
62     SkDebugf("SkRasterPipeline, %d stages\n", fNumStages);
63     std::vector<const char*> stages;
64     for (auto st = fStages; st; st = st->prev) {
65         const char* name = "";
66         switch (st->stage) {
67         #define M(x) case x: name = #x; break;
68             SK_RASTER_PIPELINE_STAGES(M)
69         #undef M
70         }
71         stages.push_back(name);
72     }
73     std::reverse(stages.begin(), stages.end());
74     for (const char* name : stages) {
75         SkDebugf("\t%s\n", name);
76     }
77     SkDebugf("\n");
78 }
79 
append_set_rgb(SkArenaAlloc * alloc,const float rgb[3])80 void SkRasterPipeline::append_set_rgb(SkArenaAlloc* alloc, const float rgb[3]) {
81     auto arg = alloc->makeArrayDefault<float>(3);
82     arg[0] = rgb[0];
83     arg[1] = rgb[1];
84     arg[2] = rgb[2];
85 
86     auto stage = unbounded_set_rgb;
87     if (0 <= rgb[0] && rgb[0] <= 1 &&
88         0 <= rgb[1] && rgb[1] <= 1 &&
89         0 <= rgb[2] && rgb[2] <= 1)
90     {
91         stage = set_rgb;
92     }
93 
94     this->unchecked_append(stage, arg);
95 }
96 
append_constant_color(SkArenaAlloc * alloc,const float rgba[4])97 void SkRasterPipeline::append_constant_color(SkArenaAlloc* alloc, const float rgba[4]) {
98     // r,g,b might be outside [0,1], but alpha should probably always be in [0,1].
99     SkASSERT(0 <= rgba[3] && rgba[3] <= 1);
100 
101     if (rgba[0] == 0 && rgba[1] == 0 && rgba[2] == 0 && rgba[3] == 1) {
102         this->append(black_color);
103     } else if (rgba[0] == 1 && rgba[1] == 1 && rgba[2] == 1 && rgba[3] == 1) {
104         this->append(white_color);
105     } else {
106         auto ctx = alloc->make<SkRasterPipeline_UniformColorCtx>();
107         Sk4f color = Sk4f::Load(rgba);
108         color.store(&ctx->r);
109 
110         // uniform_color requires colors in range and can go lowp,
111         // while unbounded_uniform_color supports out-of-range colors too but not lowp.
112         if (0 <= rgba[0] && rgba[0] <= rgba[3] &&
113             0 <= rgba[1] && rgba[1] <= rgba[3] &&
114             0 <= rgba[2] && rgba[2] <= rgba[3]) {
115             // To make loads more direct, we store 8-bit values in 16-bit slots.
116             color = color * 255.0f + 0.5f;
117             ctx->rgba[0] = (uint16_t)color[0];
118             ctx->rgba[1] = (uint16_t)color[1];
119             ctx->rgba[2] = (uint16_t)color[2];
120             ctx->rgba[3] = (uint16_t)color[3];
121             this->unchecked_append(uniform_color, ctx);
122         } else {
123             this->unchecked_append(unbounded_uniform_color, ctx);
124         }
125     }
126 }
127 
append_matrix(SkArenaAlloc * alloc,const SkMatrix & matrix)128 void SkRasterPipeline::append_matrix(SkArenaAlloc* alloc, const SkMatrix& matrix) {
129     SkMatrix::TypeMask mt = matrix.getType();
130 
131     if (mt == SkMatrix::kIdentity_Mask) {
132         return;
133     }
134     if (mt == SkMatrix::kTranslate_Mask) {
135         float* trans = alloc->makeArrayDefault<float>(2);
136         trans[0] = matrix.getTranslateX();
137         trans[1] = matrix.getTranslateY();
138         this->append(SkRasterPipeline::matrix_translate, trans);
139     } else if ((mt | (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) ==
140                      (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) {
141         float* scaleTrans = alloc->makeArrayDefault<float>(4);
142         scaleTrans[0] = matrix.getScaleX();
143         scaleTrans[1] = matrix.getScaleY();
144         scaleTrans[2] = matrix.getTranslateX();
145         scaleTrans[3] = matrix.getTranslateY();
146         this->append(SkRasterPipeline::matrix_scale_translate, scaleTrans);
147     } else {
148         float* storage = alloc->makeArrayDefault<float>(9);
149         if (matrix.asAffine(storage)) {
150             // note: asAffine and the 2x3 stage really only need 6 entries
151             this->append(SkRasterPipeline::matrix_2x3, storage);
152         } else {
153             matrix.get9(storage);
154             this->append(SkRasterPipeline::matrix_perspective, storage);
155         }
156     }
157 }
158 
append_load(SkColorType ct,const SkRasterPipeline_MemoryCtx * ctx)159 void SkRasterPipeline::append_load(SkColorType ct, const SkRasterPipeline_MemoryCtx* ctx) {
160     switch (ct) {
161         case kUnknown_SkColorType: SkASSERT(false); break;
162 
163         case kAlpha_8_SkColorType:      this->append(load_a8,      ctx); break;
164         case kRGB_565_SkColorType:      this->append(load_565,     ctx); break;
165         case kARGB_4444_SkColorType:    this->append(load_4444,    ctx); break;
166         case kRGBA_8888_SkColorType:    this->append(load_8888,    ctx); break;
167         case kRGBA_1010102_SkColorType: this->append(load_1010102, ctx); break;
168         case kRGBA_F16_SkColorType:     this->append(load_f16,     ctx); break;
169         case kRGBA_F32_SkColorType:     this->append(load_f32,     ctx); break;
170 
171         case kGray_8_SkColorType:       this->append(load_a8, ctx);
172                                         this->append(alpha_to_gray);
173                                         break;
174 
175         case kRGB_888x_SkColorType:     this->append(load_8888, ctx);
176                                         this->append(force_opaque);
177                                         break;
178 
179         case kRGB_101010x_SkColorType:  this->append(load_1010102, ctx);
180                                         this->append(force_opaque);
181                                         break;
182 
183         case kBGRA_8888_SkColorType:    this->append(load_8888, ctx);
184                                         this->append(swap_rb);
185                                         break;
186     }
187 }
188 
append_load_dst(SkColorType ct,const SkRasterPipeline_MemoryCtx * ctx)189 void SkRasterPipeline::append_load_dst(SkColorType ct, const SkRasterPipeline_MemoryCtx* ctx) {
190     switch (ct) {
191         case kUnknown_SkColorType: SkASSERT(false); break;
192 
193         case kAlpha_8_SkColorType:      this->append(load_a8_dst,      ctx); break;
194         case kRGB_565_SkColorType:      this->append(load_565_dst,     ctx); break;
195         case kARGB_4444_SkColorType:    this->append(load_4444_dst,    ctx); break;
196         case kRGBA_8888_SkColorType:    this->append(load_8888_dst,    ctx); break;
197         case kRGBA_1010102_SkColorType: this->append(load_1010102_dst, ctx); break;
198         case kRGBA_F16_SkColorType:     this->append(load_f16_dst,     ctx); break;
199         case kRGBA_F32_SkColorType:     this->append(load_f32_dst,     ctx); break;
200 
201         case kGray_8_SkColorType:       this->append(load_a8_dst, ctx);
202                                         this->append(alpha_to_gray_dst);
203                                         break;
204 
205         case kRGB_888x_SkColorType:     this->append(load_8888_dst, ctx);
206                                         this->append(force_opaque_dst);
207                                         break;
208 
209         case kRGB_101010x_SkColorType:  this->append(load_1010102_dst, ctx);
210                                         this->append(force_opaque_dst);
211                                         break;
212 
213         case kBGRA_8888_SkColorType:    this->append(load_8888_dst, ctx);
214                                         this->append(swap_rb_dst);
215                                         break;
216     }
217 }
218 
append_store(SkColorType ct,const SkRasterPipeline_MemoryCtx * ctx)219 void SkRasterPipeline::append_store(SkColorType ct, const SkRasterPipeline_MemoryCtx* ctx) {
220     switch (ct) {
221         case kUnknown_SkColorType: SkASSERT(false); break;
222 
223         case kAlpha_8_SkColorType:      this->append(store_a8,      ctx); break;
224         case kRGB_565_SkColorType:      this->append(store_565,     ctx); break;
225         case kARGB_4444_SkColorType:    this->append(store_4444,    ctx); break;
226         case kRGBA_8888_SkColorType:    this->append(store_8888,    ctx); break;
227         case kRGBA_1010102_SkColorType: this->append(store_1010102, ctx); break;
228         case kRGBA_F16_SkColorType:     this->append(store_f16,     ctx); break;
229         case kRGBA_F32_SkColorType:     this->append(store_f32,     ctx); break;
230 
231         case kRGB_888x_SkColorType:     this->append(force_opaque);
232                                         this->append(store_8888, ctx);
233                                         break;
234 
235         case kRGB_101010x_SkColorType:  this->append(force_opaque);
236                                         this->append(store_1010102, ctx);
237                                         break;
238 
239         case kGray_8_SkColorType:       this->append(luminance_to_alpha);
240                                         this->append(store_a8, ctx);
241                                         break;
242 
243         case kBGRA_8888_SkColorType:    this->append(swap_rb);
244                                         this->append(store_8888, ctx);
245                                         break;
246     }
247 }
248 
append_gamut_clamp_if_normalized(const SkImageInfo & dstInfo)249 void SkRasterPipeline::append_gamut_clamp_if_normalized(const SkImageInfo& dstInfo) {
250     if (dstInfo.colorType() != kRGBA_F16_SkColorType &&
251         dstInfo.colorType() != kRGBA_F32_SkColorType &&
252         dstInfo.alphaType() == kPremul_SkAlphaType)
253     {
254         this->unchecked_append(SkRasterPipeline::clamp_gamut, nullptr);
255     }
256 }
257 
build_pipeline(void ** ip) const258 SkRasterPipeline::StartPipelineFn SkRasterPipeline::build_pipeline(void** ip) const {
259     // We'll try to build a lowp pipeline, but if that fails fallback to a highp float pipeline.
260     void** reset_point = ip;
261 
262     // Stages are stored backwards in fStages, so we reverse here, back to front.
263     *--ip = (void*)SkOpts::just_return_lowp;
264     for (const StageList* st = fStages; st; st = st->prev) {
265         SkOpts::StageFn fn;
266         if (!st->rawFunction && (fn = SkOpts::stages_lowp[st->stage])) {
267             if (st->ctx) {
268                 *--ip = st->ctx;
269             }
270             *--ip = (void*)fn;
271         } else {
272             ip = reset_point;
273             break;
274         }
275     }
276     if (ip != reset_point) {
277         return SkOpts::start_pipeline_lowp;
278     }
279 
280     *--ip = (void*)SkOpts::just_return_highp;
281     for (const StageList* st = fStages; st; st = st->prev) {
282         if (st->ctx) {
283             *--ip = st->ctx;
284         }
285         if (st->rawFunction) {
286             *--ip = (void*)st->stage;
287         } else {
288             *--ip = (void*)SkOpts::stages_highp[st->stage];
289         }
290     }
291     return SkOpts::start_pipeline_highp;
292 }
293 
run(size_t x,size_t y,size_t w,size_t h) const294 void SkRasterPipeline::run(size_t x, size_t y, size_t w, size_t h) const {
295     if (this->empty()) {
296         return;
297     }
298 
299     // Best to not use fAlloc here... we can't bound how often run() will be called.
300     SkAutoSTMalloc<64, void*> program(fSlotsNeeded);
301 
302     auto start_pipeline = this->build_pipeline(program.get() + fSlotsNeeded);
303     start_pipeline(x,y,x+w,y+h, program.get());
304 }
305 
compile() const306 std::function<void(size_t, size_t, size_t, size_t)> SkRasterPipeline::compile() const {
307     if (this->empty()) {
308         return [](size_t, size_t, size_t, size_t) {};
309     }
310 
311     void** program = fAlloc->makeArray<void*>(fSlotsNeeded);
312 
313     auto start_pipeline = this->build_pipeline(program + fSlotsNeeded);
314     return [=](size_t x, size_t y, size_t w, size_t h) {
315         start_pipeline(x,y,x+w,y+h, program);
316     };
317 }
318