Debian Codegraph @

src/core/SkRasterPipelineBlitter.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 "include/core/SkAlphaType.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkBlender.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkColorType.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkMatrix.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkPixmap.h"
18 #include "include/core/SkRect.h"
19 #include "include/core/SkRefCnt.h"
20 #include "include/core/SkSurfaceProps.h"
21 #include "include/private/base/SkAssert.h"
22 #include "include/private/base/SkCPUTypes.h"
23 #include "include/private/base/SkOnce.h"
24 #include "include/private/base/SkTemplates.h"
25 #include "src/base/SkArenaAlloc.h"
26 #include "src/core/SkBlendModePriv.h"
27 #include "src/core/SkBlenderBase.h"
28 #include "src/core/SkBlitter.h"
29 #include "src/core/SkColorSpacePriv.h"
30 #include "src/core/SkColorSpaceXformSteps.h"
31 #include "src/core/SkConvertPixels.h"
32 #include "src/core/SkCoreBlitters.h"
33 #include "src/core/SkEffectPriv.h"
34 #include "src/core/SkMask.h"
35 #include "src/core/SkMemset.h"
36 #include "src/core/SkRasterPipeline.h"
37 #include "src/core/SkRasterPipelineOpContexts.h"
38 #include "src/core/SkRasterPipelineOpList.h"
39 #include "src/core/SkRasterPipelineVizualizer.h"
40 #include "src/effects/colorfilters/SkColorFilterBase.h"
41 #include "src/shaders/SkShaderBase.h"
42
43 #include <algorithm>
44 #include <cstdint>
45 #include <cstring>
46 #include <functional>
47 #include <optional>
48 #include <utility>
49 #include <vector>
50
51 class SkColorSpace;
52 class SkShader;
53
54 static bool can_direct_blit(const SkPaint& paint) {
55 if (paint.getShader() || paint.getColorFilter() || paint.isDither()) {
56 return false;
57 }
58 if (auto mode = paint.asBlendMode()) {
59 switch (mode.value()) {
60 case SkBlendMode::kClear: [[fallthrough]];
61 case SkBlendMode::kSrc:
62 return true;
63 case SkBlendMode::kSrcOver:
64 return paint.getAlphaf() == 1;
65 default:
66 break;
67 }
68 }
69 return false;
70 }
71
72 class SkRasterPipelineBlitter final : public SkBlitter {
73 public:
74 // This is our common entrypoint for creating the blitter once we've sorted out shaders.
75 static SkBlitter* Create(const SkPixmap& dst,
76 const SkPaint& paint,
77 const SkColor4f& dstPaintColor,
78 SkArenaAlloc* alloc,
79 const SkRasterPipeline& shaderPipeline,
80 bool is_opaque,
81 bool is_constant,
82 const SkShader* clipShader);
83
84 SkRasterPipelineBlitter(SkPixmap dst,
85 const SkPaint& paint,
86 SkArenaAlloc* alloc)
87 : fDst(std::move(dst))
88 , fAlloc(alloc)
89 , fColorPipeline(alloc)
90 , fBlendPipeline(alloc)
91 , fCanDirectBlit(can_direct_blit(paint))
92 , fDirectBlitPaintColor(paint.getColor4f())
93 {}
94
95 void blitH (int x, int y, int w) override;
96 void blitAntiH (int x, int y, const SkAlpha[], const int16_t[]) override;
97 void blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) override;
98 void blitAntiV2(int x, int y, U8CPU a0, U8CPU a1) override;
99 void blitMask (const SkMask&, const SkIRect& clip) override;
100 void blitRect (int x, int y, int width, int height) override;
101 void blitV (int x, int y, int height, SkAlpha alpha) override;
102 std::optional<DirectBlit> canDirectBlit() override;
103
104 private:
105 void appendLoadDst (SkRasterPipeline*) const;
106 void appendStore (SkRasterPipeline*) const;
107
108 // these check internally, and only append if there was a native clipShader
109 void appendClipScale (SkRasterPipeline*) const;
110 void appendClipLerp (SkRasterPipeline*) const;
111
112 SkPixmap fDst;
113 SkArenaAlloc* fAlloc;
114 SkRasterPipeline fColorPipeline;
115 SkRasterPipeline fBlendPipeline;
116 // If the blender is a blend-mode, we retain that information for late-stage optimizations
117 std::optional<SkBlendMode> fBlendMode;
118 // set to pipeline storage (for alpha) if we have a clipShader
119 void* fClipShaderBuffer = nullptr; // "native" : float or U16
120
121 bool fCanDirectBlit;
122 const SkColor4f fDirectBlitPaintColor;
123 std::optional<uint64_t> fDirectBlitValue;
124
125 SkRasterPipelineContexts::MemoryCtx
126 fDstPtr = {nullptr,0}, // Always points to the top-left of fDst.
127 fMaskPtr = {nullptr,0}; // Updated each call to blitMask().
128 SkRasterPipelineContexts::EmbossCtx fEmbossCtx; // Used only for k3D_Format masks.
129
130 // We may be able to specialize blitH() or blitRect() into a memset.
131 void (*fMemset2D)(SkPixmap*, int x,int y, int w,int h, uint64_t color) = nullptr;
132 uint64_t fMemsetColor = 0; // Big enough for largest memsettable dst format, F16.
133
134 // Built lazily on first use.
135 std::function<void(size_t, size_t, size_t, size_t)> fBlitRect,
136 fBlitAntiH,
137 fBlitMaskA8,
138 fBlitMaskLCD16,
139 fBlitMask3D;
140
141 // These values are pointed to by the blit pipelines above,
142 // which allows us to adjust them from call to call.
143 float fCurrentCoverage = 0.0f;
144 float fDitherRate = 0.0f;
145
146 using INHERITED = SkBlitter;
147 };
148
149 static SkColor4f paint_color_to_dst(const SkPaint& paint, const SkPixmap& dst) {
150 SkColor4f paintColor = paint.getColor4f();
151 SkColorSpaceXformSteps(sk_srgb_singleton(), kUnpremul_SkAlphaType,
152 dst.colorSpace(), kUnpremul_SkAlphaType).apply(paintColor.vec());
153 return paintColor;
154 }
155
156 static bool create_pipeline_for_blitter(const SkPixmap& dst,
157 const SkPaint& paint,
158 const SkMatrix& ctm,
159 SkArenaAlloc* alloc,
160 const SkSurfaceProps& props,
161 SkRasterPipeline* shaderPipeline,
162 SkColor4f* dstPaintColor,
163 bool* isOpaqueOut,
164 bool* isConstantOut,
165 const SkRect& devBounds) {
166 *dstPaintColor = paint_color_to_dst(paint, dst);
167
168 auto shader = as_SB(paint.getShader());
169
170 if (!shader) {
171 // Having no shader makes things nice and easy... just use the paint color
172 shaderPipeline->appendConstantColor(alloc, dstPaintColor->premul().vec());
173 *isOpaqueOut = dstPaintColor->fA == 1.0f;
174 *isConstantOut = true;
175 return true;
176 }
177
178 SkColorSpace* dstCS = dst.colorSpace();
179 SkColorType dstCT = dst.colorType();
180 *isOpaqueOut = shader->isOpaque() && dstPaintColor->fA == 1.0f;
181 *isConstantOut = shader->isConstant();
182 if (shader->appendRootStages(
183 SkStageRec{shaderPipeline, alloc, dstCT, dstCS, *dstPaintColor, props, devBounds},
184 ctm)) {
185 if (dstPaintColor->fA != 1.0f) {
186 shaderPipeline->append(SkRasterPipelineOp::scale_1_float,
187 alloc->make<float>(dstPaintColor->fA));
188 }
189 return true;
190 }
191 // The shader can't draw with SkRasterPipeline.
192 return false;
193 }
194
195 SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst,
196 const SkPaint& paint,
197 const SkMatrix& ctm,
198 SkArenaAlloc* alloc,
199 sk_sp<SkShader> clipShader,
200 const SkSurfaceProps& props,
201 const SkRect& devBounds) {
202 SkRasterPipeline_<256> shaderPipeline;
203 SkColor4f dstPaintColor;
204 bool is_opaque, is_constant;
205 if (!create_pipeline_for_blitter(dst,
206 paint,
207 ctm,
208 alloc,
209 props,
210 &shaderPipeline,
211 &dstPaintColor,
212 &is_opaque,
213 &is_constant,
214 devBounds)) {
215 return nullptr;
216 }
217
218 return SkRasterPipelineBlitter::Create(dst,
219 paint,
220 dstPaintColor,
221 alloc,
222 shaderPipeline,
223 is_opaque,
224 is_constant,
225 clipShader.get());
226 }
227
228 namespace SkRasterPipelineVisualizer {
229
230 SkBlitter* CreateBlitter(const SkPixmap& dst,
231 const std::vector<DebugStage>& debugStages,
232 const SkPaint& paint,
233 const SkMatrix& ctm,
234 SkArenaAlloc* alloc,
235 sk_sp<SkShader> clipShader,
236 const SkSurfaceProps& props) {
237 SkRasterPipeline_<256> shaderPipeline;
238 SkColor4f dstPaintColor;
239 bool is_opaque, is_constant;
240 if (!create_pipeline_for_blitter(dst,
241 paint,
242 ctm,
243 alloc,
244 props,
245 &shaderPipeline,
246 &dstPaintColor,
247 &is_opaque,
248 &is_constant,
249 SkRect::MakeEmpty())) {
250 return nullptr;
251 }
252
253 static SkOnce once;
254 once([&] { shaderPipeline.dump(); });
255
256 std::vector<SkRasterPipeline::StageList> stages;
257 for (auto st = shaderPipeline.getStageList(); st; st = st->prev) {
258 stages.push_back(*st);
259 }
260 std::reverse(stages.begin(), stages.end());
261
262 SkASSERT_RELEASE(stages.size() == debugStages.size());
263 SkRasterPipeline_<256> shaderPipeline2;
264
265 for (size_t i = 0; i < stages.size(); i++) {
266 shaderPipeline2.append(stages[i].stage, stages[i].ctx);
267 auto stage = debugStages[i];
268 SkASSERT_RELEASE(stage.panels.size() == stage.ops.size());
269 for (size_t j = 0; j < stage.panels.size() && j < stage.ops.size(); j++) {
270 auto panelCtx = alloc->make<SkRasterPipelineContexts::MemoryCtx>();
271 panelCtx->pixels = stage.panels[j].pixmap().writable_addr();
272 panelCtx->stride = stage.panels[j].pixmap().rowBytesAsPixels();
273 shaderPipeline2.append(stage.ops[j], panelCtx);
274 }
275 }
276
277 return SkRasterPipelineBlitter::Create(dst,
278 paint,
279 dstPaintColor,
280 alloc,
281 shaderPipeline2,
282 is_opaque,
283 is_constant,
284 clipShader.get());
285 }
286
287 } // namespace SkRasterPipelineVisualizer
288
289 SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst,
290 const SkPaint& paint,
291 const SkRasterPipeline& shaderPipeline,
292 bool is_opaque,
293 SkArenaAlloc* alloc,
294 sk_sp<SkShader> clipShader) {
295 bool is_constant = false; // If this were the case, it'd be better to just set a paint color.
296 return SkRasterPipelineBlitter::Create(dst, paint, paint_color_to_dst(paint, dst), alloc,
297 shaderPipeline, is_opaque, is_constant,
298 clipShader.get());
299 }
300
301 SkBlitter* SkRasterPipelineBlitter::Create(const SkPixmap& dst,
302 const SkPaint& paint,
303 const SkColor4f& dstPaintColor,
304 SkArenaAlloc* alloc,
305 const SkRasterPipeline& shaderPipeline,
306 bool is_opaque,
307 bool is_constant,
308 const SkShader* clipShader) {
309 auto blitter = alloc->make<SkRasterPipelineBlitter>(dst, paint, alloc);
310
311 // Our job in this factory is to fill out the blitter's color and blend pipelines.
312 // The color pipeline is the common front of the full blit pipeline. The blend pipeline is just
313 // the portion that does the actual blending math (and assumes that src and dst are already
314 // loaded).
315 //
316 // The full blit pipelines are each constructed lazily on first use, and include the color
317 // pipeline, reading the dst, the blend pipeline, coverage, dithering, and writing the dst.
318
319 // Start with the color pipeline
320 auto colorPipeline = &blitter->fColorPipeline;
321
322 if (clipShader) {
323 auto clipP = colorPipeline;
324 SkColorType clipCT = kRGBA_8888_SkColorType;
325 SkColorSpace* clipCS = nullptr;
326 SkSurfaceProps props{}; // default OK; clipShader doesn't render text
327 SkStageRec rec = {
328 clipP, alloc, clipCT, clipCS, SkColors::kBlack, props, SkRect::MakeEmpty()};
329 if (as_SB(clipShader)->appendRootStages(rec, SkMatrix::I())) {
330 struct Storage {
331 // large enough for highp (float) or lowp(U16)
332 float fA[SkRasterPipelineContexts::kMaxStride];
333 };
334 auto storage = alloc->make<Storage>();
335 clipP->append(SkRasterPipelineOp::store_src_a, storage->fA);
336 blitter->fClipShaderBuffer = storage->fA;
337 is_constant = false;
338 } else {
339 return nullptr;
340 }
341 }
342
343 // Let's get the shader in first.
344 colorPipeline->extend(shaderPipeline);
345
346 // If there's a color filter it comes next.
347 if (auto colorFilter = paint.getColorFilter()) {
348 SkSurfaceProps props{}; // default OK; colorFilter doesn't render text
349 SkStageRec rec = {colorPipeline,
350 alloc,
351 dst.colorType(),
352 dst.colorSpace(),
353 dstPaintColor,
354 props,
355 SkRect::MakeEmpty()};
356 if (!as_CFB(colorFilter)->appendStages(rec, is_opaque)) {
357 return nullptr;
358 }
359 is_opaque = is_opaque && as_CFB(colorFilter)->isAlphaUnchanged();
360 }
361
362 // Not all formats make sense to dither (think, F16). We set their dither rate
363 // to zero. We only dither non-constant shaders, so is_constant won't change here.
364 if (paint.isDither() && !is_constant) {
365 switch (dst.info().colorType()) {
366 case kARGB_4444_SkColorType:
367 blitter->fDitherRate = 1 / 15.0f;
368 break;
369 case kRGB_565_SkColorType:
370 blitter->fDitherRate = 1 / 63.0f;
371 break;
372 case kGray_8_SkColorType:
373 case kRGB_888x_SkColorType:
374 case kRGBA_8888_SkColorType:
375 case kBGRA_8888_SkColorType:
376 case kSRGBA_8888_SkColorType:
377 case kR8_unorm_SkColorType:
378 blitter->fDitherRate = 1 / 255.0f;
379 break;
380 case kRGB_101010x_SkColorType:
381 case kRGBA_1010102_SkColorType:
382 case kBGR_101010x_SkColorType:
383 case kBGRA_1010102_SkColorType:
384 case kBGRA_10101010_XR_SkColorType:
385 case kRGBA_10x6_SkColorType:
386 blitter->fDitherRate = 1 / 1023.0f;
387 break;
388
389 case kUnknown_SkColorType:
390 case kAlpha_8_SkColorType:
391 case kBGR_101010x_XR_SkColorType:
392 case kRGBA_F16_SkColorType:
393 case kRGB_F16F16F16x_SkColorType:
394 case kRGBA_F16Norm_SkColorType:
395 case kRGBA_F32_SkColorType:
396 case kR8G8_unorm_SkColorType:
397 case kA16_float_SkColorType:
398 case kA16_unorm_SkColorType:
399 case kR16G16_float_SkColorType:
400 case kR16_unorm_SkColorType:
401 case kR16G16_unorm_SkColorType:
402 case kR16G16B16A16_unorm_SkColorType:
403 blitter->fDitherRate = 0.0f;
404 break;
405 }
406 if (blitter->fDitherRate > 0.0f) {
407 colorPipeline->append(SkRasterPipelineOp::dither, &blitter->fDitherRate);
408 }
409 }
410
411 // Optimization: A pipeline that's still constant here can collapse back into a constant color.
412 if (is_constant) {
413 SkColor4f constantColor;
414 SkRasterPipelineContexts::MemoryCtx constantColorPtr = {&constantColor, 0};
415 // We could remove this clamp entirely, but if the destination is 8888, doing the clamp
416 // here allows the color pipeline to still run in lowp (we'll use uniform_color, rather than
417 // unbounded_uniform_color).
418 colorPipeline->appendClampIfNormalized(dst.info());
419 colorPipeline->append(SkRasterPipelineOp::store_f32, &constantColorPtr);
420 colorPipeline->run(0,0,1,1);
421 colorPipeline->reset();
422 colorPipeline->appendConstantColor(alloc, constantColor);
423
424 is_opaque = constantColor.fA == 1.0f;
425 }
426
427 // Now we'll build the blend pipeline
428 auto blendPipeline = &blitter->fBlendPipeline;
429
430 sk_sp<SkBlender> blender = paint.refBlender();
431 if (!blender) {
432 blender = SkBlender::Mode(SkBlendMode::kSrcOver);
433 }
434
435 // We can strength-reduce SrcOver into Src when opaque.
436 if (is_opaque && as_BB(blender)->asBlendMode() == SkBlendMode::kSrcOver) {
437 blender = SkBlender::Mode(SkBlendMode::kSrc);
438 }
439
440 // When we're drawing a constant color in Src mode, we can sometimes just memset.
441 // (The previous two optimizations help find more opportunities for this one.)
442 if (is_constant && as_BB(blender)->asBlendMode() == SkBlendMode::kSrc &&
443 dst.info().bytesPerPixel() <= static_cast<int>(sizeof(blitter->fMemsetColor))) {
444 // Run our color pipeline all the way through to produce what we'd memset when we can.
445 // Not all blits can memset, so we need to keep colorPipeline too.
446 SkRasterPipeline_<256> p;
447 p.extend(*colorPipeline);
448 blitter->fDstPtr = SkRasterPipelineContexts::MemoryCtx{&blitter->fMemsetColor, 0};
449 blitter->appendStore(&p);
450 p.run(0,0,1,1);
451
452 switch (blitter->fDst.shiftPerPixel()) {
453 case 0: blitter->fMemset2D = [](SkPixmap* dst, int x,int y, int w,int h, uint64_t c) {
454 void* p = dst->writable_addr(x,y);
455 while (h --> 0) {
456 memset(p, c, w);
457 p = SkTAddOffset<void>(p, dst->rowBytes());
458 }
459 }; break;
460
461 case 1: blitter->fMemset2D = [](SkPixmap* dst, int x,int y, int w,int h, uint64_t c) {
462 SkOpts::rect_memset16(dst->writable_addr16(x,y), c, w, dst->rowBytes(), h);
463 }; break;
464
465 case 2: blitter->fMemset2D = [](SkPixmap* dst, int x,int y, int w,int h, uint64_t c) {
466 SkOpts::rect_memset32(dst->writable_addr32(x,y), c, w, dst->rowBytes(), h);
467 }; break;
468
469 case 3: blitter->fMemset2D = [](SkPixmap* dst, int x,int y, int w,int h, uint64_t c) {
470 SkOpts::rect_memset64(dst->writable_addr64(x,y), c, w, dst->rowBytes(), h);
471 }; break;
472
473 // TODO(F32)?
474 }
475 }
476
477 {
478 SkSurfaceProps props{}; // default OK; blender doesn't render text
479 SkStageRec rec = {blendPipeline,
480 alloc,
481 dst.colorType(),
482 dst.colorSpace(),
483 dstPaintColor,
484 props,
485 SkRect::MakeEmpty()};
486 if (!as_BB(blender)->appendStages(rec)) {
487 return nullptr;
488 }
489 blitter->fBlendMode = as_BB(blender)->asBlendMode();
490 }
491
492 blitter->fDstPtr = SkRasterPipelineContexts::MemoryCtx{
493 blitter->fDst.writable_addr(),
494 blitter->fDst.rowBytesAsPixels(),
495 };
496
497 return blitter;
498 }
499
500 void SkRasterPipelineBlitter::appendLoadDst(SkRasterPipeline* p) const {
501 p->appendLoadDst(fDst.info().colorType(), &fDstPtr);
502 if (fDst.info().alphaType() == kUnpremul_SkAlphaType) {
503 p->append(SkRasterPipelineOp::premul_dst);
504 }
505 }
506
507 void SkRasterPipelineBlitter::appendStore(SkRasterPipeline* p) const {
508 if (fDst.info().alphaType() == kUnpremul_SkAlphaType) {
509 p->append(SkRasterPipelineOp::unpremul);
510 }
511 p->appendStore(fDst.info().colorType(), &fDstPtr);
512 }
513
514 void SkRasterPipelineBlitter::appendClipScale(SkRasterPipeline* p) const {
515 if (fClipShaderBuffer) {
516 p->append(SkRasterPipelineOp::scale_native, fClipShaderBuffer);
517 }
518 }
519
520 void SkRasterPipelineBlitter::appendClipLerp(SkRasterPipeline* p) const {
521 if (fClipShaderBuffer) {
522 p->append(SkRasterPipelineOp::lerp_native, fClipShaderBuffer);
523 }
524 }
525
526 void SkRasterPipelineBlitter::blitH(int x, int y, int w) {
527 this->blitRect(x,y,w,1);
528 }
529
530 void SkRasterPipelineBlitter::blitRect(int x, int y, int w, int h) {
531 if (fMemset2D) {
532 fMemset2D(&fDst, x,y, w,h, fMemsetColor);
533 return;
534 }
535
536 if (!fBlitRect) {
537 SkRasterPipeline p(fAlloc);
538 p.extend(fColorPipeline);
539 p.appendClampIfNormalized(fDst.info());
540 if (fBlendMode == SkBlendMode::kSrcOver
541 && (fDst.info().colorType() == kRGBA_8888_SkColorType ||
542 fDst.info().colorType() == kBGRA_8888_SkColorType)
543 && !fDst.colorSpace()
544 && fDst.info().alphaType() != kUnpremul_SkAlphaType
545 && fDitherRate == 0.0f) {
546 if (fDst.info().colorType() == kBGRA_8888_SkColorType) {
547 p.append(SkRasterPipelineOp::swap_rb);
548 }
549 this->appendClipScale(&p);
550 p.append(SkRasterPipelineOp::srcover_rgba_8888, &fDstPtr);
551 } else {
552 if (fBlendMode != SkBlendMode::kSrc) {
553 this->appendLoadDst(&p);
554 p.extend(fBlendPipeline);
555 this->appendClipLerp(&p);
556 } else if (fClipShaderBuffer) {
557 this->appendLoadDst(&p);
558 this->appendClipLerp(&p);
559 }
560 this->appendStore(&p);
561 }
562 fBlitRect = p.compile();
563 }
564
565 fBlitRect(x,y,w,h);
566 }
567
568 void SkRasterPipelineBlitter::blitAntiH(int x, int y, const SkAlpha aa[], const int16_t runs[]) {
569 if (!fBlitAntiH) {
570 SkRasterPipeline p(fAlloc);
571 p.extend(fColorPipeline);
572 p.appendClampIfNormalized(fDst.info());
573 if (fBlendMode.has_value() &&
574 SkBlendMode_ShouldPreScaleCoverage(*fBlendMode, /*rgb_coverage=*/false)) {
575 p.append(SkRasterPipelineOp::scale_1_float, &fCurrentCoverage);
576 this->appendClipScale(&p);
577 this->appendLoadDst(&p);
578 p.extend(fBlendPipeline);
579 } else {
580 this->appendLoadDst(&p);
581 p.extend(fBlendPipeline);
582 p.append(SkRasterPipelineOp::lerp_1_float, &fCurrentCoverage);
583 this->appendClipLerp(&p);
584 }
585
586 this->appendStore(&p);
587 fBlitAntiH = p.compile();
588 }
589
590 for (int16_t run = *runs; run > 0; run = *runs) {
591 switch (*aa) {
592 case 0x00: break;
593 case 0xff: this->blitRect(x,y,run, 1); break;
594 default:
595 fCurrentCoverage = *aa * (1/255.0f);
596 fBlitAntiH(x,y,run,1);
597 }
598 x += run;
599 runs += run;
600 aa += run;
601 }
602 }
603
604 void SkRasterPipelineBlitter::blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) {
605 SkIRect clip = {x,y, x+2,y+1};
606 uint8_t coverage[] = { (uint8_t)a0, (uint8_t)a1 };
607 SkMask mask(coverage, clip, 2, SkMask::kA8_Format);
608 this->blitMask(mask, clip);
609 }
610
611 void SkRasterPipelineBlitter::blitAntiV2(int x, int y, U8CPU a0, U8CPU a1) {
612 SkIRect clip = {x,y, x+1,y+2};
613 uint8_t coverage[] = { (uint8_t)a0, (uint8_t)a1 };
614 SkMask mask(coverage, clip, 1, SkMask::kA8_Format);
615 this->blitMask(mask, clip);
616 }
617
618 void SkRasterPipelineBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
619 SkIRect clip = {x,y, x+1,y+height};
620 SkMask mask(&alpha, clip,
621 0, // so we reuse the 1 "row" for all of height
622 SkMask::kA8_Format);
623 this->blitMask(mask, clip);
624 }
625
626 void SkRasterPipelineBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
627 if (mask.fFormat == SkMask::kBW_Format) {
628 // TODO: native BW masks?
629 return INHERITED::blitMask(mask, clip);
630 }
631
632 // ARGB and SDF masks shouldn't make it here.
633 SkASSERT(mask.fFormat == SkMask::kA8_Format
634 || mask.fFormat == SkMask::kLCD16_Format
635 || mask.fFormat == SkMask::k3D_Format);
636
637 auto extract_mask_plane = [&mask](int plane, SkRasterPipelineContexts::MemoryCtx* ctx) {
638 // LCD is 16-bit per pixel; A8 and 3D are 8-bit per pixel.
639 size_t bpp = mask.fFormat == SkMask::kLCD16_Format ? 2 : 1;
640
641 // Select the right mask plane. Usually plane == 0 and this is just mask.fImage.
642 auto ptr = (uintptr_t)mask.fImage
643 + plane * mask.computeImageSize();
644
645 // Update ctx to point "into" this current mask, but lined up with fDstPtr at (0,0).
646 // This sort of trickery upsets UBSAN (pointer-overflow) so our ptr must be a uintptr_t.
647 // mask.fRowBytes is a uint32_t, which would break our addressing math on 64-bit builds.
648 size_t rowBytes = mask.fRowBytes;
649 ctx->stride = rowBytes / bpp;
650 ctx->pixels = (void*)(ptr - mask.fBounds.left() * bpp
651 - mask.fBounds.top() * rowBytes);
652 };
653
654 extract_mask_plane(0, &fMaskPtr);
655 if (mask.fFormat == SkMask::k3D_Format) {
656 extract_mask_plane(1, &fEmbossCtx.mul);
657 extract_mask_plane(2, &fEmbossCtx.add);
658 }
659
660 // Lazily build whichever pipeline we need, specialized for each mask format.
661 if (mask.fFormat == SkMask::kA8_Format && !fBlitMaskA8) {
662 SkRasterPipeline p(fAlloc);
663 p.extend(fColorPipeline);
664 p.appendClampIfNormalized(fDst.info());
665 if (fBlendMode.has_value() &&
666 SkBlendMode_ShouldPreScaleCoverage(*fBlendMode, /*rgb_coverage=*/false)) {
667 p.append(SkRasterPipelineOp::scale_u8, &fMaskPtr);
668 this->appendClipScale(&p);
669 this->appendLoadDst(&p);
670 p.extend(fBlendPipeline);
671 } else {
672 this->appendLoadDst(&p);
673 p.extend(fBlendPipeline);
674 p.append(SkRasterPipelineOp::lerp_u8, &fMaskPtr);
675 this->appendClipLerp(&p);
676 }
677 this->appendStore(&p);
678 fBlitMaskA8 = p.compile();
679 }
680 if (mask.fFormat == SkMask::kLCD16_Format && !fBlitMaskLCD16) {
681 SkRasterPipeline p(fAlloc);
682 p.extend(fColorPipeline);
683 p.appendClampIfNormalized(fDst.info());
684 if (fBlendMode.has_value() &&
685 SkBlendMode_ShouldPreScaleCoverage(*fBlendMode, /*rgb_coverage=*/true)) {
686 // Somewhat unusually, scale_565 needs dst loaded first.
687 this->appendLoadDst(&p);
688 p.append(SkRasterPipelineOp::scale_565, &fMaskPtr);
689 this->appendClipScale(&p);
690 p.extend(fBlendPipeline);
691 } else {
692 this->appendLoadDst(&p);
693 p.extend(fBlendPipeline);
694 p.append(SkRasterPipelineOp::lerp_565, &fMaskPtr);
695 this->appendClipLerp(&p);
696 }
697 this->appendStore(&p);
698 fBlitMaskLCD16 = p.compile();
699 }
700 if (mask.fFormat == SkMask::k3D_Format && !fBlitMask3D) {
701 SkRasterPipeline p(fAlloc);
702 p.extend(fColorPipeline);
703 // This bit is where we differ from kA8_Format:
704 p.append(SkRasterPipelineOp::emboss, &fEmbossCtx);
705 // Now onward just as kA8.
706 p.appendClampIfNormalized(fDst.info());
707 if (fBlendMode.has_value() &&
708 SkBlendMode_ShouldPreScaleCoverage(*fBlendMode, /*rgb_coverage=*/false)) {
709 p.append(SkRasterPipelineOp::scale_u8, &fMaskPtr);
710 this->appendClipScale(&p);
711 this->appendLoadDst(&p);
712 p.extend(fBlendPipeline);
713 } else {
714 this->appendLoadDst(&p);
715 p.extend(fBlendPipeline);
716 p.append(SkRasterPipelineOp::lerp_u8, &fMaskPtr);
717 this->appendClipLerp(&p);
718 }
719 this->appendStore(&p);
720 fBlitMask3D = p.compile();
721 }
722
723 std::function<void(size_t,size_t,size_t,size_t)>* blitter = nullptr;
724 switch (mask.fFormat) {
725 case SkMask::kA8_Format: blitter = &fBlitMaskA8; break;
726 case SkMask::kLCD16_Format: blitter = &fBlitMaskLCD16; break;
727 case SkMask::k3D_Format: blitter = &fBlitMask3D; break;
728 default:
729 SkASSERT(false);
730 return;
731 }
732
733 SkASSERT(blitter);
734 (*blitter)(clip.left(),clip.top(), clip.width(),clip.height());
735 }
736
737 std::optional<SkBlitter::DirectBlit> SkRasterPipelineBlitter::canDirectBlit() {
738 if (fCanDirectBlit) {
739 if (!fDirectBlitValue.has_value()) {
740 // want maximum alignment (8) but legal punning for smaller int types
741 union {
742 uint64_t u8[1];
743 uint32_t u4[2];
744 uint16_t u2[4];
745 uint8_t u1[8];
746 } dstBuffer;
747 auto dst = SkImageInfo::Make(1, 1, fDst.info().colorType(), fDst.info().alphaType());
748 auto src = SkImageInfo::Make(1, 1, kRGBA_F32_SkColorType, kUnpremul_SkAlphaType);
749 if (!SkConvertPixels(dst, &dstBuffer, sizeof(dstBuffer),
750 src, &fDirectBlitPaintColor, sizeof(fDirectBlitPaintColor))) {
751 goto FAIL;
752 }
753 switch (dst.bytesPerPixel()) {
754 case 1: fDirectBlitValue = dstBuffer.u1[0]; break;
755 case 2: fDirectBlitValue = dstBuffer.u2[0]; break;
756 case 4: fDirectBlitValue = dstBuffer.u4[0]; break;
757 case 8: fDirectBlitValue = dstBuffer.u8[0]; break;
758 default: goto FAIL;
759 }
760 }
761 return {{fDst, fDirectBlitValue.value()}};
762 }
763 FAIL:
764 fCanDirectBlit = false;
765 return {};
766 }