Debian Codegraph @

src/core/SkRasterPipeline.cpp

raw
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/SkRasterPipeline.h"
9
10 #include "include/core/SkColorType.h"
11 #include "include/core/SkImageInfo.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/private/base/SkDebug.h"
14 #include "include/private/base/SkTemplates.h"
15 #include "modules/skcms/skcms.h"
16 #include "src/base/SkVx.h"
17 #include "src/core/SkImageInfoPriv.h"
18 #include "src/core/SkOpts.h"
19 #include "src/core/SkRasterPipelineOpContexts.h"
20 #include "src/core/SkRasterPipelineOpList.h"
21
22 #include <algorithm>
23 #include <cstring>
24 #include <vector>
25
26 using namespace skia_private;
27 using Op = SkRasterPipelineOp;
28
29 bool gForceHighPrecisionRasterPipeline;
30
31 SkRasterPipeline::SkRasterPipeline(SkArenaAlloc* alloc) : fAlloc(alloc) {
32 this->reset();
33 }
34
35 void SkRasterPipeline::reset() {
36 // We intentionally leave the alloc alone here; we don't own it.
37 fRewindCtx = nullptr;
38 fStages = nullptr;
39 fTailPointer = nullptr;
40 fNumStages = 0;
41 fMemoryCtxInfos.clear();
42 }
43
44 void SkRasterPipeline::append(SkRasterPipelineOp op, void* ctx) {
45 SkASSERT(op != Op::uniform_color); // Please use appendConstantColor().
46 SkASSERT(op != Op::unbounded_uniform_color); // Please use appendConstantColor().
47 SkASSERT(op != Op::set_rgb); // Please use appendSetRGB().
48 SkASSERT(op != Op::unbounded_set_rgb); // Please use appendSetRGB().
49 SkASSERT(op != Op::parametric); // Please use appendTransferFunction().
50 SkASSERT(op != Op::gamma_); // Please use appendTransferFunction().
51 SkASSERT(op != Op::PQish); // Please use appendTransferFunction().
52 SkASSERT(op != Op::HLGish); // Please use appendTransferFunction().
53 SkASSERT(op != Op::HLGinvish); // Please use appendTransferFunction().
54 SkASSERT(op != Op::stack_checkpoint); // Please use appendStackRewind().
55 SkASSERT(op != Op::stack_rewind); // Please use appendStackRewind().
56 this->uncheckedAppend(op, ctx);
57 }
58
59 uint8_t* SkRasterPipeline::tailPointer() {
60 if (!fTailPointer) {
61 // All ops in the pipeline that use the tail value share the same value.
62 fTailPointer = fAlloc->make<uint8_t>(0xFF);
63 }
64 return fTailPointer;
65 }
66
67 void SkRasterPipeline::uncheckedAppend(SkRasterPipelineOp op, void* ctx) {
68 bool isLoad = false, isStore = false;
69 SkColorType ct = kUnknown_SkColorType;
70
71 #define COLOR_TYPE_CASE(stage_ct, sk_ct) \
72 case Op::load_##stage_ct: \
73 case Op::load_##stage_ct##_dst: \
74 ct = sk_ct; \
75 isLoad = true; \
76 break; \
77 case Op::store_##stage_ct: \
78 ct = sk_ct; \
79 isStore = true; \
80 break;
81
82 switch (op) {
83 COLOR_TYPE_CASE(a8, kAlpha_8_SkColorType)
84 COLOR_TYPE_CASE(565, kRGB_565_SkColorType)
85 COLOR_TYPE_CASE(4444, kARGB_4444_SkColorType)
86 COLOR_TYPE_CASE(8888, kRGBA_8888_SkColorType)
87 COLOR_TYPE_CASE(rg88, kR8G8_unorm_SkColorType)
88 COLOR_TYPE_CASE(16161616, kR16G16B16A16_unorm_SkColorType)
89 COLOR_TYPE_CASE(a16, kA16_unorm_SkColorType)
90 COLOR_TYPE_CASE(r16, kR16_unorm_SkColorType)
91 COLOR_TYPE_CASE(rg1616, kR16G16_unorm_SkColorType)
92 COLOR_TYPE_CASE(f16, kRGBA_F16_SkColorType)
93 COLOR_TYPE_CASE(af16, kA16_float_SkColorType)
94 COLOR_TYPE_CASE(rgf16, kR16G16_float_SkColorType)
95 COLOR_TYPE_CASE(f32, kRGBA_F32_SkColorType)
96 COLOR_TYPE_CASE(1010102, kRGBA_1010102_SkColorType)
97 COLOR_TYPE_CASE(1010102_xr, kBGR_101010x_XR_SkColorType)
98 COLOR_TYPE_CASE(10101010_xr, kBGRA_10101010_XR_SkColorType)
99 COLOR_TYPE_CASE(10x6, kRGBA_10x6_SkColorType)
100
101 #undef COLOR_TYPE_CASE
102
103 case Op::debug_r:
104 case Op::debug_g:
105 case Op::debug_b:
106 case Op::debug_a:
107 case Op::debug_r_255:
108 case Op::debug_g_255:
109 case Op::debug_b_255:
110 case Op::debug_a_255:
111 case Op::debug_x:
112 case Op::debug_y: {
113 ct = kRGBA_8888_SkColorType;
114 isStore = true;
115 break;
116 }
117 // Odd stage that doesn't have a load variant (appendLoad uses load_a8 + alpha_to_red)
118 case Op::store_r8: {
119 ct = kR8_unorm_SkColorType;
120 isStore = true;
121 break;
122 }
123 case Op::srcover_rgba_8888: {
124 ct = kRGBA_8888_SkColorType;
125 isLoad = true;
126 isStore = true;
127 break;
128 }
129 case Op::scale_u8:
130 case Op::lerp_u8: {
131 ct = kAlpha_8_SkColorType;
132 isLoad = true;
133 break;
134 }
135 case Op::scale_565:
136 case Op::lerp_565: {
137 ct = kRGB_565_SkColorType;
138 isLoad = true;
139 break;
140 }
141 case Op::emboss: {
142 // Special-case, this op uses a context that holds *two* MemoryCtxs
143 SkRasterPipelineContexts::EmbossCtx* embossCtx =
144 (SkRasterPipelineContexts::EmbossCtx*)ctx;
145 this->addMemoryContext(&embossCtx->add,
146 SkColorTypeBytesPerPixel(kAlpha_8_SkColorType),
147 /*load=*/true, /*store=*/false);
148 this->addMemoryContext(&embossCtx->mul,
149 SkColorTypeBytesPerPixel(kAlpha_8_SkColorType),
150 /*load=*/true, /*store=*/false);
151 break;
152 }
153 case Op::init_lane_masks: {
154 auto* initCtx = (SkRasterPipelineContexts::InitLaneMasksCtx*)ctx;
155 initCtx->tail = this->tailPointer();
156 break;
157 }
158 case Op::branch_if_all_lanes_active: {
159 auto* branchCtx = (SkRasterPipelineContexts::BranchIfAllLanesActiveCtx*)ctx;
160 branchCtx->tail = this->tailPointer();
161 break;
162 }
163 default:
164 break;
165 }
166
167 fStages = fAlloc->make<StageList>(StageList{fStages, op, ctx});
168 fNumStages += 1;
169
170 if (isLoad || isStore) {
171 SkASSERT(ct != kUnknown_SkColorType);
172 this->addMemoryContext((SkRasterPipelineContexts::MemoryCtx*)ctx,
173 SkColorTypeBytesPerPixel(ct),
174 isLoad,
175 isStore);
176 }
177 }
178
179 void SkRasterPipeline::append(SkRasterPipelineOp op, uintptr_t ctx) {
180 void* ptrCtx;
181 memcpy(&ptrCtx, &ctx, sizeof(ctx));
182 this->append(op, ptrCtx);
183 }
184
185 void SkRasterPipeline::extend(const SkRasterPipeline& src) {
186 if (src.empty()) {
187 return;
188 }
189 // Create a rewind context if `src` has one already, but we don't. If we _do_ already have one,
190 // we need to keep it, since we already have rewind ops that reference it. Either way, we need
191 // to rewrite all the rewind ops to point to _our_ rewind context; we only get that checkpoint.
192 if (src.fRewindCtx && !fRewindCtx) {
193 fRewindCtx = fAlloc->make<SkRasterPipelineContexts::RewindCtx>();
194 }
195 auto stages = fAlloc->makeArrayDefault<StageList>(src.fNumStages);
196
197 int n = src.fNumStages;
198 const StageList* st = src.fStages;
199 while (n --> 1) {
200 stages[n] = *st;
201 stages[n].prev = &stages[n-1];
202
203 // We make sure that all ops use _our_ stack context and tail pointer.
204 switch (stages[n].stage) {
205 case Op::stack_rewind: {
206 stages[n].ctx = fRewindCtx;
207 break;
208 }
209 case Op::init_lane_masks: {
210 auto* ctx = (SkRasterPipelineContexts::InitLaneMasksCtx*)stages[n].ctx;
211 ctx->tail = this->tailPointer();
212 break;
213 }
214 case Op::branch_if_all_lanes_active: {
215 auto* ctx = (SkRasterPipelineContexts::BranchIfAllLanesActiveCtx*)stages[n].ctx;
216 ctx->tail = this->tailPointer();
217 break;
218 }
219 default:
220 break;
221 }
222
223 st = st->prev;
224 }
225 stages[0] = *st;
226 stages[0].prev = fStages;
227
228 fStages = &stages[src.fNumStages - 1];
229 fNumStages += src.fNumStages;
230 for (const SkRasterPipelineContexts::MemoryCtxInfo& info : src.fMemoryCtxInfos) {
231 this->addMemoryContext(info.context, info.bytesPerPixel, info.load, info.store);
232 }
233 }
234
235 const char* SkRasterPipeline::GetOpName(SkRasterPipelineOp op) {
236 const char* name = "";
237 switch (op) {
238 #define M(x) case Op::x: name = #x; break;
239 SK_RASTER_PIPELINE_OPS_ALL(M)
240 #undef M
241 }
242 return name;
243 }
244
245 void SkRasterPipeline::dump() const {
246 SkDebugf("SkRasterPipeline, %d stages\n", fNumStages);
247 std::vector<const char*> stages;
248 for (auto st = fStages; st; st = st->prev) {
249 stages.push_back(GetOpName(st->stage));
250 }
251 std::reverse(stages.begin(), stages.end());
252 for (const char* name : stages) {
253 SkDebugf("\t%s\n", name);
254 }
255 SkDebugf("\n");
256 }
257
258 void SkRasterPipeline::appendSetRGB(SkArenaAlloc* alloc, const float rgb[3]) {
259 auto arg = alloc->makeArrayDefault<float>(3);
260 arg[0] = rgb[0];
261 arg[1] = rgb[1];
262 arg[2] = rgb[2];
263
264 auto op = Op::unbounded_set_rgb;
265 if (0 <= rgb[0] && rgb[0] <= 1 &&
266 0 <= rgb[1] && rgb[1] <= 1 &&
267 0 <= rgb[2] && rgb[2] <= 1)
268 {
269 op = Op::set_rgb;
270 }
271
272 this->uncheckedAppend(op, arg);
273 }
274
275 void SkRasterPipeline::appendConstantColor(SkArenaAlloc* alloc, const float rgba[4]) {
276 // r,g,b might be outside [0,1], but alpha should probably always be in [0,1].
277 SkASSERT(0 <= rgba[3] && rgba[3] <= 1);
278
279 if (rgba[0] == 0 && rgba[1] == 0 && rgba[2] == 0 && rgba[3] == 1) {
280 this->append(Op::black_color);
281 } else if (rgba[0] == 1 && rgba[1] == 1 && rgba[2] == 1 && rgba[3] == 1) {
282 this->append(Op::white_color);
283 } else {
284 auto ctx = alloc->make<SkRasterPipelineContexts::UniformColorCtx>();
285 skvx::float4 color = skvx::float4::Load(rgba);
286 color.store(&ctx->r);
287
288 // uniform_color requires colors in range and can go lowp,
289 // while unbounded_uniform_color supports out-of-range colors too but not lowp.
290 if (0 <= rgba[0] && rgba[0] <= rgba[3] &&
291 0 <= rgba[1] && rgba[1] <= rgba[3] &&
292 0 <= rgba[2] && rgba[2] <= rgba[3]) {
293 // To make loads more direct, we store 8-bit values in 16-bit slots.
294 color = color * 255.0f + 0.5f;
295 ctx->rgba[0] = (uint16_t)color[0];
296 ctx->rgba[1] = (uint16_t)color[1];
297 ctx->rgba[2] = (uint16_t)color[2];
298 ctx->rgba[3] = (uint16_t)color[3];
299 this->uncheckedAppend(Op::uniform_color, ctx);
300 } else {
301 this->uncheckedAppend(Op::unbounded_uniform_color, ctx);
302 }
303 }
304 }
305
306 void SkRasterPipeline::appendMatrix(SkArenaAlloc* alloc, const SkMatrix& matrix) {
307 SkMatrix::TypeMask mt = matrix.getType();
308
309 if (mt == SkMatrix::kIdentity_Mask) {
310 return;
311 }
312 if (mt == SkMatrix::kTranslate_Mask) {
313 float* trans = alloc->makeArrayDefault<float>(2);
314 trans[0] = matrix.getTranslateX();
315 trans[1] = matrix.getTranslateY();
316 this->append(Op::matrix_translate, trans);
317 } else if ((mt | (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) ==
318 (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) {
319 float* scaleTrans = alloc->makeArrayDefault<float>(4);
320 scaleTrans[0] = matrix.getScaleX();
321 scaleTrans[1] = matrix.getScaleY();
322 scaleTrans[2] = matrix.getTranslateX();
323 scaleTrans[3] = matrix.getTranslateY();
324 this->append(Op::matrix_scale_translate, scaleTrans);
325 } else {
326 float* storage = alloc->makeArrayDefault<float>(9);
327 matrix.get9(storage);
328 if (!matrix.hasPerspective()) {
329 // note: asAffine and the 2x3 stage really only need 6 entries
330 this->append(Op::matrix_2x3, storage);
331 } else {
332 this->append(Op::matrix_perspective, storage);
333 }
334 }
335 }
336
337 void SkRasterPipeline::appendLoad(SkColorType ct, const SkRasterPipelineContexts::MemoryCtx* ctx) {
338 switch (ct) {
339 case kUnknown_SkColorType: SkASSERT(false); break;
340
341 case kAlpha_8_SkColorType: this->append(Op::load_a8, ctx); break;
342 case kA16_unorm_SkColorType: this->append(Op::load_a16, ctx); break;
343 case kA16_float_SkColorType: this->append(Op::load_af16, ctx); break;
344 case kRGB_565_SkColorType: this->append(Op::load_565, ctx); break;
345 case kARGB_4444_SkColorType: this->append(Op::load_4444, ctx); break;
346 case kR8G8_unorm_SkColorType: this->append(Op::load_rg88, ctx); break;
347 case kR16_unorm_SkColorType: this->append(Op::load_r16, ctx); break;
348 case kR16G16_unorm_SkColorType: this->append(Op::load_rg1616, ctx); break;
349 case kR16G16_float_SkColorType: this->append(Op::load_rgf16, ctx); break;
350 case kRGBA_8888_SkColorType: this->append(Op::load_8888, ctx); break;
351 case kRGBA_1010102_SkColorType: this->append(Op::load_1010102, ctx); break;
352 case kR16G16B16A16_unorm_SkColorType:this->append(Op::load_16161616,ctx); break;
353 case kRGBA_F16Norm_SkColorType:
354 case kRGBA_F16_SkColorType: this->append(Op::load_f16, ctx); break;
355 case kRGBA_F32_SkColorType: this->append(Op::load_f32, ctx); break;
356 case kRGBA_10x6_SkColorType: this->append(Op::load_10x6, ctx); break;
357
358 case kGray_8_SkColorType: this->append(Op::load_a8, ctx);
359 this->append(Op::alpha_to_gray);
360 break;
361
362 case kR8_unorm_SkColorType: this->append(Op::load_a8, ctx);
363 this->append(Op::alpha_to_red);
364 break;
365
366 case kRGB_888x_SkColorType: this->append(Op::load_8888, ctx);
367 this->append(Op::force_opaque);
368 break;
369
370 case kBGRA_1010102_SkColorType: this->append(Op::load_1010102, ctx);
371 this->append(Op::swap_rb);
372 break;
373
374 case kRGB_101010x_SkColorType: this->append(Op::load_1010102, ctx);
375 this->append(Op::force_opaque);
376 break;
377
378 case kBGR_101010x_SkColorType: this->append(Op::load_1010102, ctx);
379 this->append(Op::force_opaque);
380 this->append(Op::swap_rb);
381 break;
382
383 case kBGRA_10101010_XR_SkColorType: this->append(Op::load_10101010_xr, ctx);
384 this->append(Op::swap_rb);
385 break;
386
387 case kBGR_101010x_XR_SkColorType: this->append(Op::load_1010102_xr, ctx);
388 this->append(Op::force_opaque);
389 this->append(Op::swap_rb);
390 break;
391 case kRGB_F16F16F16x_SkColorType: this->append(Op::load_f16, ctx);
392 this->append(Op::force_opaque);
393 break;
394
395 case kBGRA_8888_SkColorType: this->append(Op::load_8888, ctx);
396 this->append(Op::swap_rb);
397 break;
398
399 case kSRGBA_8888_SkColorType:
400 this->append(Op::load_8888, ctx);
401 this->appendTransferFunction(*skcms_sRGB_TransferFunction());
402 break;
403 }
404 }
405
406 void SkRasterPipeline::appendLoadDst(SkColorType ct,
407 const SkRasterPipelineContexts::MemoryCtx* ctx) {
408 switch (ct) {
409 case kUnknown_SkColorType: SkASSERT(false); break;
410
411 case kAlpha_8_SkColorType: this->append(Op::load_a8_dst, ctx); break;
412 case kA16_unorm_SkColorType: this->append(Op::load_a16_dst, ctx); break;
413 case kA16_float_SkColorType: this->append(Op::load_af16_dst, ctx); break;
414 case kRGB_565_SkColorType: this->append(Op::load_565_dst, ctx); break;
415 case kARGB_4444_SkColorType: this->append(Op::load_4444_dst, ctx); break;
416 case kR8G8_unorm_SkColorType: this->append(Op::load_rg88_dst, ctx); break;
417 case kR16_unorm_SkColorType: this->append(Op::load_r16_dst, ctx); break;
418 case kR16G16_unorm_SkColorType: this->append(Op::load_rg1616_dst, ctx); break;
419 case kR16G16_float_SkColorType: this->append(Op::load_rgf16_dst, ctx); break;
420 case kRGBA_8888_SkColorType: this->append(Op::load_8888_dst, ctx); break;
421 case kRGBA_1010102_SkColorType: this->append(Op::load_1010102_dst, ctx); break;
422 case kR16G16B16A16_unorm_SkColorType: this->append(Op::load_16161616_dst,ctx); break;
423 case kRGBA_F16Norm_SkColorType:
424 case kRGBA_F16_SkColorType: this->append(Op::load_f16_dst, ctx); break;
425 case kRGBA_F32_SkColorType: this->append(Op::load_f32_dst, ctx); break;
426 case kRGBA_10x6_SkColorType: this->append(Op::load_10x6_dst, ctx); break;
427
428 case kGray_8_SkColorType: this->append(Op::load_a8_dst, ctx);
429 this->append(Op::alpha_to_gray_dst);
430 break;
431
432 case kR8_unorm_SkColorType: this->append(Op::load_a8_dst, ctx);
433 this->append(Op::alpha_to_red_dst);
434 break;
435
436 case kRGB_888x_SkColorType: this->append(Op::load_8888_dst, ctx);
437 this->append(Op::force_opaque_dst);
438 break;
439
440 case kBGRA_1010102_SkColorType: this->append(Op::load_1010102_dst, ctx);
441 this->append(Op::swap_rb_dst);
442 break;
443
444 case kRGB_101010x_SkColorType: this->append(Op::load_1010102_dst, ctx);
445 this->append(Op::force_opaque_dst);
446 break;
447
448 case kBGR_101010x_SkColorType: this->append(Op::load_1010102_dst, ctx);
449 this->append(Op::force_opaque_dst);
450 this->append(Op::swap_rb_dst);
451 break;
452
453 case kBGR_101010x_XR_SkColorType: this->append(Op::load_1010102_xr_dst, ctx);
454 this->append(Op::force_opaque_dst);
455 this->append(Op::swap_rb_dst);
456 break;
457
458 case kBGRA_10101010_XR_SkColorType: this->append(Op::load_10101010_xr_dst, ctx);
459 this->append(Op::swap_rb_dst);
460 break;
461 case kRGB_F16F16F16x_SkColorType: this->append(Op::load_f16_dst, ctx);
462 this->append(Op::force_opaque_dst);
463 break;
464
465 case kBGRA_8888_SkColorType: this->append(Op::load_8888_dst, ctx);
466 this->append(Op::swap_rb_dst);
467 break;
468
469 case kSRGBA_8888_SkColorType:
470 // TODO: We could remove the double-swap if we had _dst versions of all the TF stages
471 this->append(Op::load_8888_dst, ctx);
472 this->append(Op::swap_src_dst);
473 this->appendTransferFunction(*skcms_sRGB_TransferFunction());
474 this->append(Op::swap_src_dst);
475 break;
476 }
477 }
478
479 void SkRasterPipeline::appendStore(SkColorType ct, const SkRasterPipelineContexts::MemoryCtx* ctx) {
480 switch (ct) {
481 case kUnknown_SkColorType: SkASSERT(false); break;
482
483 case kAlpha_8_SkColorType: this->append(Op::store_a8, ctx); break;
484 case kR8_unorm_SkColorType: this->append(Op::store_r8, ctx); break;
485 case kA16_unorm_SkColorType: this->append(Op::store_a16, ctx); break;
486 case kA16_float_SkColorType: this->append(Op::store_af16, ctx); break;
487 case kRGB_565_SkColorType: this->append(Op::store_565, ctx); break;
488 case kARGB_4444_SkColorType: this->append(Op::store_4444, ctx); break;
489 case kR8G8_unorm_SkColorType: this->append(Op::store_rg88, ctx); break;
490 case kR16_unorm_SkColorType: this->append(Op::store_r16, ctx); break;
491 case kR16G16_unorm_SkColorType: this->append(Op::store_rg1616, ctx); break;
492 case kR16G16_float_SkColorType: this->append(Op::store_rgf16, ctx); break;
493 case kRGBA_8888_SkColorType: this->append(Op::store_8888, ctx); break;
494 case kRGBA_1010102_SkColorType: this->append(Op::store_1010102, ctx); break;
495 case kR16G16B16A16_unorm_SkColorType: this->append(Op::store_16161616,ctx); break;
496 case kRGBA_F16Norm_SkColorType:
497 case kRGBA_F16_SkColorType: this->append(Op::store_f16, ctx); break;
498 case kRGBA_F32_SkColorType: this->append(Op::store_f32, ctx); break;
499 case kRGBA_10x6_SkColorType: this->append(Op::store_10x6, ctx); break;
500
501 case kRGB_888x_SkColorType: this->append(Op::force_opaque);
502 this->append(Op::store_8888, ctx);
503 break;
504
505 case kBGRA_1010102_SkColorType: this->append(Op::swap_rb);
506 this->append(Op::store_1010102, ctx);
507 break;
508
509 case kRGB_101010x_SkColorType: this->append(Op::force_opaque);
510 this->append(Op::store_1010102, ctx);
511 break;
512
513 case kBGR_101010x_SkColorType: this->append(Op::force_opaque);
514 this->append(Op::swap_rb);
515 this->append(Op::store_1010102, ctx);
516 break;
517
518 case kBGR_101010x_XR_SkColorType: this->append(Op::force_opaque);
519 this->append(Op::swap_rb);
520 this->append(Op::store_1010102_xr, ctx);
521 break;
522 case kRGB_F16F16F16x_SkColorType: this->append(Op::force_opaque);
523 this->append(Op::store_f16, ctx);
524 break;
525
526 case kBGRA_10101010_XR_SkColorType: this->append(Op::swap_rb);
527 this->append(Op::store_10101010_xr, ctx);
528 break;
529
530 case kGray_8_SkColorType: this->append(Op::bt709_luminance_or_luma_to_alpha);
531 this->append(Op::store_a8, ctx);
532 break;
533
534 case kBGRA_8888_SkColorType: this->append(Op::swap_rb);
535 this->append(Op::store_8888, ctx);
536 break;
537
538 case kSRGBA_8888_SkColorType:
539 this->appendTransferFunction(*skcms_sRGB_Inverse_TransferFunction());
540 this->append(Op::store_8888, ctx);
541 break;
542 }
543 }
544
545 void SkRasterPipeline::appendTransferFunction(const skcms_TransferFunction& tf) {
546 void* ctx = const_cast<void*>(static_cast<const void*>(&tf));
547 switch (skcms_TransferFunction_getType(&tf)) {
548 default: SkASSERT(false); break;
549
550 case skcms_TFType_sRGBish:
551 if (tf.a == 1 && tf.b == 0 && tf.c == 0 && tf.d == 0 && tf.e == 0 && tf.f == 0) {
552 this->uncheckedAppend(Op::gamma_, ctx);
553 } else {
554 this->uncheckedAppend(Op::parametric, ctx);
555 }
556 break;
557 case skcms_TFType_PQish: this->uncheckedAppend(Op::PQish, ctx); break;
558 case skcms_TFType_HLGish: this->uncheckedAppend(Op::HLGish, ctx); break;
559 case skcms_TFType_HLGinvish: this->uncheckedAppend(Op::HLGinvish, ctx); break;
560 }
561 }
562
563 // GPUs clamp all color channels to the limits of the format just before the blend step. To match
564 // that auto-clamp, the RP blitter uses this helper immediately before appending blending stages.
565 void SkRasterPipeline::appendClampIfNormalized(const SkImageInfo& info) {
566 if (SkColorTypeIsNormalized(info.colorType())) {
567 this->uncheckedAppend(Op::clamp_01, nullptr);
568 }
569 }
570
571 void SkRasterPipeline::appendStackRewind() {
572 if (!fRewindCtx) {
573 fRewindCtx = fAlloc->make<SkRasterPipelineContexts::RewindCtx>();
574 }
575 this->uncheckedAppend(Op::stack_rewind, fRewindCtx);
576 }
577
578 static void prepend_to_pipeline(SkRasterPipelineStage*& ip, SkOpts::StageFn stageFn, void* ctx) {
579 --ip;
580 ip->fn = stageFn;
581 ip->ctx = ctx;
582 }
583
584 bool SkRasterPipeline::buildLowpPipeline(SkRasterPipelineStage* ip) const {
585 if (gForceHighPrecisionRasterPipeline || fRewindCtx) {
586 return false;
587 }
588 // Stages are stored backwards in fStages; to compensate, we assemble the pipeline in reverse
589 // here, back to front.
590 prepend_to_pipeline(ip, SkOpts::just_return_lowp, /*ctx=*/nullptr);
591 for (const StageList* st = fStages; st; st = st->prev) {
592 int opIndex = (int)st->stage;
593 if (opIndex >= kNumRasterPipelineLowpOps || !SkOpts::ops_lowp[opIndex]) {
594 // This program contains a stage that doesn't exist in lowp.
595 return false;
596 }
597 prepend_to_pipeline(ip, SkOpts::ops_lowp[opIndex], st->ctx);
598 }
599 return true;
600 }
601
602 void SkRasterPipeline::buildHighpPipeline(SkRasterPipelineStage* ip) const {
603 // We assemble the pipeline in reverse, since the stage list is stored backwards.
604 prepend_to_pipeline(ip, SkOpts::just_return_highp, /*ctx=*/nullptr);
605 for (const StageList* st = fStages; st; st = st->prev) {
606 int opIndex = (int)st->stage;
607 prepend_to_pipeline(ip, SkOpts::ops_highp[opIndex], st->ctx);
608 }
609
610 // stack_checkpoint and stack_rewind are only implemented in highp. We only need these stages
611 // when generating long (or looping) pipelines from SkSL. The other stages used by the SkSL
612 // Raster Pipeline generator will only have highp implementations, because we can't execute SkSL
613 // code without floating point.
614 if (fRewindCtx) {
615 const int rewindIndex = (int)Op::stack_checkpoint;
616 prepend_to_pipeline(ip, SkOpts::ops_highp[rewindIndex], fRewindCtx);
617 }
618 }
619
620 SkRasterPipeline::StartPipelineFn SkRasterPipeline::buildPipeline(SkRasterPipelineStage* ip) const {
621 // We try to build a lowp pipeline first; if that fails, we fall back to a highp float pipeline.
622 if (this->buildLowpPipeline(ip)) {
623 return SkOpts::start_pipeline_lowp;
624 }
625
626 this->buildHighpPipeline(ip);
627 return SkOpts::start_pipeline_highp;
628 }
629
630 int SkRasterPipeline::stagesNeeded() const {
631 // Add 1 to budget for a `just_return` stage at the end.
632 int stages = fNumStages + 1;
633
634 // If we have any stack_rewind stages, we will need to inject a stack_checkpoint stage.
635 if (fRewindCtx) {
636 stages += 1;
637 }
638 return stages;
639 }
640
641 void SkRasterPipeline::run(size_t x, size_t y, size_t w, size_t h) const {
642 if (this->empty()) {
643 return;
644 }
645
646 int stagesNeeded = this->stagesNeeded();
647
648 // Best to not use fAlloc here... we can't bound how often run() will be called.
649 AutoSTMalloc<32, SkRasterPipelineStage> program(stagesNeeded);
650
651 size_t numMemoryCtxs = fMemoryCtxInfos.size();
652 AutoSTMalloc<2, SkRasterPipelineContexts::MemoryCtxPatch> patches(numMemoryCtxs);
653 for (size_t i = 0; i < numMemoryCtxs; ++i) {
654 patches[i].info = fMemoryCtxInfos[i];
655 patches[i].backup = nullptr;
656 memset(patches[i].scratch, 0, sizeof(patches[i].scratch));
657 }
658
659 auto start_pipeline = this->buildPipeline(program.get() + stagesNeeded);
660 start_pipeline(x, y, x + w, y + h, program.get(),
661 SkSpan{patches.data(), numMemoryCtxs},
662 fTailPointer);
663 }
664
665 std::function<void(size_t, size_t, size_t, size_t)> SkRasterPipeline::compile() const {
666 if (this->empty()) {
667 return [](size_t, size_t, size_t, size_t) {};
668 }
669
670 int stagesNeeded = this->stagesNeeded();
671
672 SkRasterPipelineStage* program = fAlloc->makeArray<SkRasterPipelineStage>(stagesNeeded);
673
674 size_t numMemoryCtxs = fMemoryCtxInfos.size();
675 SkRasterPipelineContexts::MemoryCtxPatch* patches =
676 fAlloc->makeArray<SkRasterPipelineContexts::MemoryCtxPatch>(numMemoryCtxs);
677 for (size_t i = 0; i < numMemoryCtxs; ++i) {
678 patches[i].info = fMemoryCtxInfos[i];
679 patches[i].backup = nullptr;
680 memset(patches[i].scratch, 0, sizeof(patches[i].scratch));
681 }
682 uint8_t* tailPointer = fTailPointer;
683
684 auto start_pipeline = this->buildPipeline(program + stagesNeeded);
685 return [=](size_t x, size_t y, size_t w, size_t h) {
686 start_pipeline(x, y, x + w, y + h, program,
687 {patches, numMemoryCtxs},
688 tailPointer);
689 };
690 }
691
692 void SkRasterPipeline::addMemoryContext(SkRasterPipelineContexts::MemoryCtx* ctx,
693 int bytesPerPixel,
694 bool load,
695 bool store) {
696 SkRasterPipelineContexts::MemoryCtxInfo* info = std::find_if(
697 fMemoryCtxInfos.begin(),
698 fMemoryCtxInfos.end(),
699 [=](const SkRasterPipelineContexts::MemoryCtxInfo& i) { return i.context == ctx; });
700 if (info != fMemoryCtxInfos.end()) {
701 SkASSERT(bytesPerPixel == info->bytesPerPixel);
702 info->load = info->load || load;
703 info->store = info->store || store;
704 } else {
705 fMemoryCtxInfos.push_back(
706 SkRasterPipelineContexts::MemoryCtxInfo{ctx, bytesPerPixel, load, store});
707 }
708 }