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