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