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_F16Norm_SkColorType:
169 case kRGBA_F16_SkColorType: this->append(load_f16, ctx); break;
170 case kRGBA_F32_SkColorType: this->append(load_f32, ctx); break;
171
172 case kGray_8_SkColorType: this->append(load_a8, ctx);
173 this->append(alpha_to_gray);
174 break;
175
176 case kRGB_888x_SkColorType: this->append(load_8888, ctx);
177 this->append(force_opaque);
178 break;
179
180 case kRGB_101010x_SkColorType: this->append(load_1010102, ctx);
181 this->append(force_opaque);
182 break;
183
184 case kBGRA_8888_SkColorType: this->append(load_8888, ctx);
185 this->append(swap_rb);
186 break;
187 }
188 }
189
append_load_dst(SkColorType ct,const SkRasterPipeline_MemoryCtx * ctx)190 void SkRasterPipeline::append_load_dst(SkColorType ct, const SkRasterPipeline_MemoryCtx* ctx) {
191 switch (ct) {
192 case kUnknown_SkColorType: SkASSERT(false); break;
193
194 case kAlpha_8_SkColorType: this->append(load_a8_dst, ctx); break;
195 case kRGB_565_SkColorType: this->append(load_565_dst, ctx); break;
196 case kARGB_4444_SkColorType: this->append(load_4444_dst, ctx); break;
197 case kRGBA_8888_SkColorType: this->append(load_8888_dst, ctx); break;
198 case kRGBA_1010102_SkColorType: this->append(load_1010102_dst, ctx); break;
199 case kRGBA_F16Norm_SkColorType:
200 case kRGBA_F16_SkColorType: this->append(load_f16_dst, ctx); break;
201 case kRGBA_F32_SkColorType: this->append(load_f32_dst, ctx); break;
202
203 case kGray_8_SkColorType: this->append(load_a8_dst, ctx);
204 this->append(alpha_to_gray_dst);
205 break;
206
207 case kRGB_888x_SkColorType: this->append(load_8888_dst, ctx);
208 this->append(force_opaque_dst);
209 break;
210
211 case kRGB_101010x_SkColorType: this->append(load_1010102_dst, ctx);
212 this->append(force_opaque_dst);
213 break;
214
215 case kBGRA_8888_SkColorType: this->append(load_8888_dst, ctx);
216 this->append(swap_rb_dst);
217 break;
218 }
219 }
220
append_store(SkColorType ct,const SkRasterPipeline_MemoryCtx * ctx)221 void SkRasterPipeline::append_store(SkColorType ct, const SkRasterPipeline_MemoryCtx* ctx) {
222 switch (ct) {
223 case kUnknown_SkColorType: SkASSERT(false); break;
224
225 case kAlpha_8_SkColorType: this->append(store_a8, ctx); break;
226 case kRGB_565_SkColorType: this->append(store_565, ctx); break;
227 case kARGB_4444_SkColorType: this->append(store_4444, ctx); break;
228 case kRGBA_8888_SkColorType: this->append(store_8888, ctx); break;
229 case kRGBA_1010102_SkColorType: this->append(store_1010102, ctx); break;
230 case kRGBA_F16Norm_SkColorType:
231 case kRGBA_F16_SkColorType: this->append(store_f16, ctx); break;
232 case kRGBA_F32_SkColorType: this->append(store_f32, ctx); break;
233
234 case kRGB_888x_SkColorType: this->append(force_opaque);
235 this->append(store_8888, ctx);
236 break;
237
238 case kRGB_101010x_SkColorType: this->append(force_opaque);
239 this->append(store_1010102, ctx);
240 break;
241
242 case kGray_8_SkColorType: this->append(luminance_to_alpha);
243 this->append(store_a8, ctx);
244 break;
245
246 case kBGRA_8888_SkColorType: this->append(swap_rb);
247 this->append(store_8888, ctx);
248 break;
249 }
250 }
251
append_gamut_clamp_if_normalized(const SkImageInfo & dstInfo)252 void SkRasterPipeline::append_gamut_clamp_if_normalized(const SkImageInfo& dstInfo) {
253 // N.B. we _do_ clamp for kRGBA_F16Norm_SkColorType... because it's normalized.
254 if (dstInfo.colorType() != kRGBA_F16_SkColorType &&
255 dstInfo.colorType() != kRGBA_F32_SkColorType &&
256 dstInfo.alphaType() == kPremul_SkAlphaType)
257 {
258 this->unchecked_append(SkRasterPipeline::clamp_gamut, nullptr);
259 }
260 }
261
build_pipeline(void ** ip) const262 SkRasterPipeline::StartPipelineFn SkRasterPipeline::build_pipeline(void** ip) const {
263 // We'll try to build a lowp pipeline, but if that fails fallback to a highp float pipeline.
264 void** reset_point = ip;
265
266 // Stages are stored backwards in fStages, so we reverse here, back to front.
267 *--ip = (void*)SkOpts::just_return_lowp;
268 for (const StageList* st = fStages; st; st = st->prev) {
269 SkOpts::StageFn fn;
270 if (!st->rawFunction && (fn = SkOpts::stages_lowp[st->stage])) {
271 if (st->ctx) {
272 *--ip = st->ctx;
273 }
274 *--ip = (void*)fn;
275 } else {
276 ip = reset_point;
277 break;
278 }
279 }
280 if (ip != reset_point) {
281 return SkOpts::start_pipeline_lowp;
282 }
283
284 *--ip = (void*)SkOpts::just_return_highp;
285 for (const StageList* st = fStages; st; st = st->prev) {
286 if (st->ctx) {
287 *--ip = st->ctx;
288 }
289 if (st->rawFunction) {
290 *--ip = (void*)st->stage;
291 } else {
292 *--ip = (void*)SkOpts::stages_highp[st->stage];
293 }
294 }
295 return SkOpts::start_pipeline_highp;
296 }
297
run(size_t x,size_t y,size_t w,size_t h) const298 void SkRasterPipeline::run(size_t x, size_t y, size_t w, size_t h) const {
299 if (this->empty()) {
300 return;
301 }
302
303 // Best to not use fAlloc here... we can't bound how often run() will be called.
304 SkAutoSTMalloc<64, void*> program(fSlotsNeeded);
305
306 auto start_pipeline = this->build_pipeline(program.get() + fSlotsNeeded);
307 start_pipeline(x,y,x+w,y+h, program.get());
308 }
309
compile() const310 std::function<void(size_t, size_t, size_t, size_t)> SkRasterPipeline::compile() const {
311 if (this->empty()) {
312 return [](size_t, size_t, size_t, size_t) {};
313 }
314
315 void** program = fAlloc->makeArray<void*>(fSlotsNeeded);
316
317 auto start_pipeline = this->build_pipeline(program + fSlotsNeeded);
318 return [=](size_t x, size_t y, size_t w, size_t h) {
319 start_pipeline(x,y,x+w,y+h, program);
320 };
321 }
322