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 "SkArenaAlloc.h"
9 #include "SkBlendModePriv.h"
10 #include "SkBlitter.h"
11 #include "SkColor.h"
12 #include "SkColorFilter.h"
13 #include "SkColorSpacePriv.h"
14 #include "SkColorSpaceXformer.h"
15 #include "SkColorSpaceXformSteps.h"
16 #include "SkOpts.h"
17 #include "SkRasterPipeline.h"
18 #include "SkShader.h"
19 #include "SkShaderBase.h"
20 #include "SkTo.h"
21 #include "SkUtils.h"
22
23 class SkRasterPipelineBlitter final : public SkBlitter {
24 public:
25 // This is our common entrypoint for creating the blitter once we've sorted out shaders.
26 static SkBlitter* Create(const SkPixmap&, const SkPaint&, SkArenaAlloc*,
27 const SkRasterPipeline& shaderPipeline,
28 bool is_opaque, bool is_constant);
29
SkRasterPipelineBlitter(SkPixmap dst,SkBlendMode blend,SkArenaAlloc * alloc)30 SkRasterPipelineBlitter(SkPixmap dst,
31 SkBlendMode blend,
32 SkArenaAlloc* alloc)
33 : fDst(dst)
34 , fBlend(blend)
35 , fAlloc(alloc)
36 , fColorPipeline(alloc)
37 {}
38
39 void blitH (int x, int y, int w) override;
40 void blitAntiH (int x, int y, const SkAlpha[], const int16_t[]) override;
41 void blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) override;
42 void blitAntiV2(int x, int y, U8CPU a0, U8CPU a1) override;
43 void blitMask (const SkMask&, const SkIRect& clip) override;
44 void blitRect (int x, int y, int width, int height) override;
45 void blitV (int x, int y, int height, SkAlpha alpha) override;
46
47 private:
48 void append_load_dst (SkRasterPipeline*) const;
49 void append_store (SkRasterPipeline*) const;
50
51 SkPixmap fDst;
52 SkBlendMode fBlend;
53 SkArenaAlloc* fAlloc;
54 SkRasterPipeline fColorPipeline;
55
56 SkRasterPipeline_MemoryCtx
57 fDstPtr = {nullptr,0}, // Always points to the top-left of fDst.
58 fMaskPtr = {nullptr,0}; // Updated each call to blitMask().
59 SkRasterPipeline_EmbossCtx fEmbossCtx; // Used only for k3D_Format masks.
60
61 // We may be able to specialize blitH() or blitRect() into a memset.
62 void (*fMemset2D)(SkPixmap*, int x,int y, int w,int h, uint64_t color) = nullptr;
63 uint64_t fMemsetColor = 0; // Big enough for largest memsettable dst format, F16.
64
65 // Built lazily on first use.
66 std::function<void(size_t, size_t, size_t, size_t)> fBlitRect,
67 fBlitAntiH,
68 fBlitMaskA8,
69 fBlitMaskLCD16,
70 fBlitMask3D;
71
72 // These values are pointed to by the blit pipelines above,
73 // which allows us to adjust them from call to call.
74 float fCurrentCoverage = 0.0f;
75 float fDitherRate = 0.0f;
76
77 typedef SkBlitter INHERITED;
78 };
79
SkCreateRasterPipelineBlitter(const SkPixmap & dst,const SkPaint & paint,const SkMatrix & ctm,SkArenaAlloc * alloc)80 SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst,
81 const SkPaint& paint,
82 const SkMatrix& ctm,
83 SkArenaAlloc* alloc) {
84 // For legacy/SkColorSpaceXformCanvas to keep working,
85 // we need to sometimes still need to distinguish null dstCS from sRGB.
86 #if 0
87 SkColorSpace* dstCS = dst.colorSpace() ? dst.colorSpace()
88 : sk_srgb_singleton();
89 #else
90 SkColorSpace* dstCS = dst.colorSpace();
91 #endif
92 SkColorType dstCT = dst.colorType();
93 SkColor4f paintColor = paint.getColor4f();
94 SkColorSpaceXformSteps(sk_srgb_singleton(), kUnpremul_SkAlphaType,
95 dstCS, kUnpremul_SkAlphaType).apply(paintColor.vec());
96
97 auto shader = as_SB(paint.getShader());
98
99 SkRasterPipeline_<256> shaderPipeline;
100 if (!shader) {
101 // Having no shader makes things nice and easy... just use the paint color.
102 shaderPipeline.append_constant_color(alloc, paintColor.premul().vec());
103 bool is_opaque = paintColor.fA == 1.0f,
104 is_constant = true;
105 return SkRasterPipelineBlitter::Create(dst, paint, alloc,
106 shaderPipeline, is_opaque, is_constant);
107 }
108
109 bool is_opaque = shader->isOpaque() && paintColor.fA == 1.0f;
110 bool is_constant = shader->isConstant();
111
112 if (shader->appendStages({&shaderPipeline, alloc, dstCT, dstCS, paint, nullptr, ctm})) {
113 if (paintColor.fA != 1.0f) {
114 shaderPipeline.append(SkRasterPipeline::scale_1_float,
115 alloc->make<float>(paintColor.fA));
116 }
117 return SkRasterPipelineBlitter::Create(dst, paint, alloc,
118 shaderPipeline, is_opaque, is_constant);
119 }
120
121 // The shader has opted out of drawing anything.
122 return alloc->make<SkNullBlitter>();
123 }
124
SkCreateRasterPipelineBlitter(const SkPixmap & dst,const SkPaint & paint,const SkRasterPipeline & shaderPipeline,bool is_opaque,SkArenaAlloc * alloc)125 SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst,
126 const SkPaint& paint,
127 const SkRasterPipeline& shaderPipeline,
128 bool is_opaque,
129 SkArenaAlloc* alloc) {
130 bool is_constant = false; // If this were the case, it'd be better to just set a paint color.
131 return SkRasterPipelineBlitter::Create(dst, paint, alloc,
132 shaderPipeline, is_opaque, is_constant);
133 }
134
Create(const SkPixmap & dst,const SkPaint & paint,SkArenaAlloc * alloc,const SkRasterPipeline & shaderPipeline,bool is_opaque,bool is_constant)135 SkBlitter* SkRasterPipelineBlitter::Create(const SkPixmap& dst,
136 const SkPaint& paint,
137 SkArenaAlloc* alloc,
138 const SkRasterPipeline& shaderPipeline,
139 bool is_opaque,
140 bool is_constant) {
141 auto blitter = alloc->make<SkRasterPipelineBlitter>(dst,
142 paint.getBlendMode(),
143 alloc);
144
145 // Our job in this factory is to fill out the blitter's color pipeline.
146 // This is the common front of the full blit pipelines, each constructed lazily on first use.
147 // The full blit pipelines handle reading and writing the dst, blending, coverage, dithering.
148 auto colorPipeline = &blitter->fColorPipeline;
149
150 // Let's get the shader in first.
151 colorPipeline->extend(shaderPipeline);
152
153 // If there's a color filter it comes next.
154 if (auto colorFilter = paint.getColorFilter()) {
155 colorFilter->appendStages(colorPipeline, dst.colorSpace(), alloc, is_opaque);
156 is_opaque = is_opaque && (colorFilter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag);
157 }
158
159 // Not all formats make sense to dither (think, F16). We set their dither rate
160 // to zero. We need to decide if we're going to dither now to keep is_constant accurate.
161 if (paint.isDither()) {
162 switch (dst.info().colorType()) {
163 default: blitter->fDitherRate = 0.0f; break;
164 case kARGB_4444_SkColorType: blitter->fDitherRate = 1/15.0f; break;
165 case kRGB_565_SkColorType: blitter->fDitherRate = 1/63.0f; break;
166 case kGray_8_SkColorType:
167 case kRGB_888x_SkColorType:
168 case kRGBA_8888_SkColorType:
169 case kBGRA_8888_SkColorType: blitter->fDitherRate = 1/255.0f; break;
170 case kRGB_101010x_SkColorType:
171 case kRGBA_1010102_SkColorType: blitter->fDitherRate = 1/1023.0f; break;
172 }
173 // TODO: for constant colors, we could try to measure the effect of dithering, and if
174 // it has no value (i.e. all variations result in the same 32bit color, then we
175 // could disable it (for speed, by not adding the stage).
176 }
177 is_constant = is_constant && (blitter->fDitherRate == 0.0f);
178
179 // We're logically done here. The code between here and return blitter is all optimization.
180
181 // A pipeline that's still constant here can collapse back into a constant color.
182 if (is_constant) {
183 SkColor4f constantColor;
184 SkRasterPipeline_MemoryCtx constantColorPtr = { &constantColor, 0 };
185 colorPipeline->append_gamut_clamp_if_normalized(dst.info());
186 colorPipeline->append(SkRasterPipeline::store_f32, &constantColorPtr);
187 colorPipeline->run(0,0,1,1);
188 colorPipeline->reset();
189 colorPipeline->append_constant_color(alloc, constantColor);
190
191 is_opaque = constantColor.fA == 1.0f;
192 }
193
194 // We can strength-reduce SrcOver into Src when opaque.
195 if (is_opaque && blitter->fBlend == SkBlendMode::kSrcOver) {
196 blitter->fBlend = SkBlendMode::kSrc;
197 }
198
199 // When we're drawing a constant color in Src mode, we can sometimes just memset.
200 // (The previous two optimizations help find more opportunities for this one.)
201 if (is_constant && blitter->fBlend == SkBlendMode::kSrc) {
202 // Run our color pipeline all the way through to produce what we'd memset when we can.
203 // Not all blits can memset, so we need to keep colorPipeline too.
204 SkRasterPipeline_<256> p;
205 p.extend(*colorPipeline);
206 p.append_gamut_clamp_if_normalized(dst.info());
207 blitter->fDstPtr = SkRasterPipeline_MemoryCtx{&blitter->fMemsetColor, 0};
208 blitter->append_store(&p);
209 p.run(0,0,1,1);
210
211 switch (blitter->fDst.shiftPerPixel()) {
212 case 0: blitter->fMemset2D = [](SkPixmap* dst, int x,int y, int w,int h, uint64_t c) {
213 void* p = dst->writable_addr(x,y);
214 while (h --> 0) {
215 memset(p, c, w);
216 p = SkTAddOffset<void>(p, dst->rowBytes());
217 }
218 }; break;
219
220 case 1: blitter->fMemset2D = [](SkPixmap* dst, int x,int y, int w,int h, uint64_t c) {
221 uint16_t* p = dst->writable_addr16(x,y);
222 auto fn = SkOpts::memset16;
223 while (h --> 0) {
224 fn(p, c, w);
225 p = SkTAddOffset<uint16_t>(p, dst->rowBytes());
226 }
227 }; break;
228
229 case 2: blitter->fMemset2D = [](SkPixmap* dst, int x,int y, int w,int h, uint64_t c) {
230 uint32_t* p = dst->writable_addr32(x,y);
231 auto fn = SkOpts::memset32;
232 while (h --> 0) {
233 fn(p, c, w);
234 p = SkTAddOffset<uint32_t>(p, dst->rowBytes());
235 }
236 }; break;
237
238 case 3: blitter->fMemset2D = [](SkPixmap* dst, int x,int y, int w,int h, uint64_t c) {
239 uint64_t* p = dst->writable_addr64(x,y);
240 auto fn = SkOpts::memset64;
241 while (h --> 0) {
242 fn(p, c, w);
243 p = SkTAddOffset<uint64_t>(p, dst->rowBytes());
244 }
245 }; break;
246
247 // TODO(F32)?
248 }
249 }
250
251 blitter->fDstPtr = SkRasterPipeline_MemoryCtx{
252 blitter->fDst.writable_addr(),
253 blitter->fDst.rowBytesAsPixels(),
254 };
255
256 return blitter;
257 }
258
append_load_dst(SkRasterPipeline * p) const259 void SkRasterPipelineBlitter::append_load_dst(SkRasterPipeline* p) const {
260 p->append_load_dst(fDst.info().colorType(), &fDstPtr);
261 if (fDst.info().alphaType() == kUnpremul_SkAlphaType) {
262 p->append(SkRasterPipeline::premul_dst);
263 }
264 }
265
append_store(SkRasterPipeline * p) const266 void SkRasterPipelineBlitter::append_store(SkRasterPipeline* p) const {
267 if (fDst.info().alphaType() == kUnpremul_SkAlphaType) {
268 p->append(SkRasterPipeline::unpremul);
269 }
270 if (fDitherRate > 0.0f) {
271 p->append(SkRasterPipeline::dither, &fDitherRate);
272 }
273
274 p->append_store(fDst.info().colorType(), &fDstPtr);
275 }
276
blitH(int x,int y,int w)277 void SkRasterPipelineBlitter::blitH(int x, int y, int w) {
278 this->blitRect(x,y,w,1);
279 }
280
blitRect(int x,int y,int w,int h)281 void SkRasterPipelineBlitter::blitRect(int x, int y, int w, int h) {
282 if (fMemset2D) {
283 fMemset2D(&fDst, x,y, w,h, fMemsetColor);
284 return;
285 }
286
287 if (!fBlitRect) {
288 SkRasterPipeline p(fAlloc);
289 p.extend(fColorPipeline);
290 p.append_gamut_clamp_if_normalized(fDst.info());
291 if (fBlend == SkBlendMode::kSrcOver
292 && (fDst.info().colorType() == kRGBA_8888_SkColorType ||
293 fDst.info().colorType() == kBGRA_8888_SkColorType)
294 && !fDst.colorSpace()
295 && fDst.info().alphaType() != kUnpremul_SkAlphaType
296 && fDitherRate == 0.0f) {
297 if (fDst.info().colorType() == kBGRA_8888_SkColorType) {
298 p.append(SkRasterPipeline::swap_rb);
299 }
300 p.append(SkRasterPipeline::srcover_rgba_8888, &fDstPtr);
301 } else {
302 if (fBlend != SkBlendMode::kSrc) {
303 this->append_load_dst(&p);
304 SkBlendMode_AppendStages(fBlend, &p);
305 }
306 this->append_store(&p);
307 }
308 fBlitRect = p.compile();
309 }
310
311 fBlitRect(x,y,w,h);
312 }
313
blitAntiH(int x,int y,const SkAlpha aa[],const int16_t runs[])314 void SkRasterPipelineBlitter::blitAntiH(int x, int y, const SkAlpha aa[], const int16_t runs[]) {
315 if (!fBlitAntiH) {
316 SkRasterPipeline p(fAlloc);
317 p.extend(fColorPipeline);
318 p.append_gamut_clamp_if_normalized(fDst.info());
319 if (SkBlendMode_ShouldPreScaleCoverage(fBlend, /*rgb_coverage=*/false)) {
320 p.append(SkRasterPipeline::scale_1_float, &fCurrentCoverage);
321 this->append_load_dst(&p);
322 SkBlendMode_AppendStages(fBlend, &p);
323 } else {
324 this->append_load_dst(&p);
325 SkBlendMode_AppendStages(fBlend, &p);
326 p.append(SkRasterPipeline::lerp_1_float, &fCurrentCoverage);
327 }
328
329 this->append_store(&p);
330 fBlitAntiH = p.compile();
331 }
332
333 for (int16_t run = *runs; run > 0; run = *runs) {
334 switch (*aa) {
335 case 0x00: break;
336 case 0xff: this->blitH(x,y,run); break;
337 default:
338 fCurrentCoverage = *aa * (1/255.0f);
339 fBlitAntiH(x,y,run,1);
340 }
341 x += run;
342 runs += run;
343 aa += run;
344 }
345 }
346
blitAntiH2(int x,int y,U8CPU a0,U8CPU a1)347 void SkRasterPipelineBlitter::blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) {
348 SkIRect clip = {x,y, x+2,y+1};
349 uint8_t coverage[] = { (uint8_t)a0, (uint8_t)a1 };
350
351 SkMask mask;
352 mask.fImage = coverage;
353 mask.fBounds = clip;
354 mask.fRowBytes = 2;
355 mask.fFormat = SkMask::kA8_Format;
356
357 this->blitMask(mask, clip);
358 }
359
blitAntiV2(int x,int y,U8CPU a0,U8CPU a1)360 void SkRasterPipelineBlitter::blitAntiV2(int x, int y, U8CPU a0, U8CPU a1) {
361 SkIRect clip = {x,y, x+1,y+2};
362 uint8_t coverage[] = { (uint8_t)a0, (uint8_t)a1 };
363
364 SkMask mask;
365 mask.fImage = coverage;
366 mask.fBounds = clip;
367 mask.fRowBytes = 1;
368 mask.fFormat = SkMask::kA8_Format;
369
370 this->blitMask(mask, clip);
371 }
372
blitV(int x,int y,int height,SkAlpha alpha)373 void SkRasterPipelineBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
374 SkIRect clip = {x,y, x+1,y+height};
375
376 SkMask mask;
377 mask.fImage = α
378 mask.fBounds = clip;
379 mask.fRowBytes = 0; // so we reuse the 1 "row" for all of height
380 mask.fFormat = SkMask::kA8_Format;
381
382 this->blitMask(mask, clip);
383 }
384
blitMask(const SkMask & mask,const SkIRect & clip)385 void SkRasterPipelineBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
386 if (mask.fFormat == SkMask::kBW_Format) {
387 // TODO: native BW masks?
388 return INHERITED::blitMask(mask, clip);
389 }
390
391 // ARGB and SDF masks shouldn't make it here.
392 SkASSERT(mask.fFormat == SkMask::kA8_Format
393 || mask.fFormat == SkMask::kLCD16_Format
394 || mask.fFormat == SkMask::k3D_Format);
395
396 auto extract_mask_plane = [&mask](int plane, SkRasterPipeline_MemoryCtx* ctx) {
397 // LCD is 16-bit per pixel; A8 and 3D are 8-bit per pixel.
398 size_t bpp = mask.fFormat == SkMask::kLCD16_Format ? 2 : 1;
399
400 // Select the right mask plane. Usually plane == 0 and this is just mask.fImage.
401 auto ptr = (uintptr_t)mask.fImage
402 + plane * mask.computeImageSize();
403
404 // Update ctx to point "into" this current mask, but lined up with fDstPtr at (0,0).
405 // This sort of trickery upsets UBSAN (pointer-overflow) so our ptr must be a uintptr_t.
406 // mask.fRowBytes is a uint32_t, which would break our addressing math on 64-bit builds.
407 size_t rowBytes = mask.fRowBytes;
408 ctx->stride = rowBytes / bpp;
409 ctx->pixels = (void*)(ptr - mask.fBounds.left() * bpp
410 - mask.fBounds.top() * rowBytes);
411 };
412
413 extract_mask_plane(0, &fMaskPtr);
414 if (mask.fFormat == SkMask::k3D_Format) {
415 extract_mask_plane(1, &fEmbossCtx.mul);
416 extract_mask_plane(2, &fEmbossCtx.add);
417 }
418
419 // Lazily build whichever pipeline we need, specialized for each mask format.
420 if (mask.fFormat == SkMask::kA8_Format && !fBlitMaskA8) {
421 SkRasterPipeline p(fAlloc);
422 p.extend(fColorPipeline);
423 p.append_gamut_clamp_if_normalized(fDst.info());
424 if (SkBlendMode_ShouldPreScaleCoverage(fBlend, /*rgb_coverage=*/false)) {
425 p.append(SkRasterPipeline::scale_u8, &fMaskPtr);
426 this->append_load_dst(&p);
427 SkBlendMode_AppendStages(fBlend, &p);
428 } else {
429 this->append_load_dst(&p);
430 SkBlendMode_AppendStages(fBlend, &p);
431 p.append(SkRasterPipeline::lerp_u8, &fMaskPtr);
432 }
433 this->append_store(&p);
434 fBlitMaskA8 = p.compile();
435 }
436 if (mask.fFormat == SkMask::kLCD16_Format && !fBlitMaskLCD16) {
437 SkRasterPipeline p(fAlloc);
438 p.extend(fColorPipeline);
439 p.append_gamut_clamp_if_normalized(fDst.info());
440 if (SkBlendMode_ShouldPreScaleCoverage(fBlend, /*rgb_coverage=*/true)) {
441 // Somewhat unusually, scale_565 needs dst loaded first.
442 this->append_load_dst(&p);
443 p.append(SkRasterPipeline::scale_565, &fMaskPtr);
444 SkBlendMode_AppendStages(fBlend, &p);
445 } else {
446 this->append_load_dst(&p);
447 SkBlendMode_AppendStages(fBlend, &p);
448 p.append(SkRasterPipeline::lerp_565, &fMaskPtr);
449 }
450 this->append_store(&p);
451 fBlitMaskLCD16 = p.compile();
452 }
453 if (mask.fFormat == SkMask::k3D_Format && !fBlitMask3D) {
454 SkRasterPipeline p(fAlloc);
455 p.extend(fColorPipeline);
456 // This bit is where we differ from kA8_Format:
457 p.append(SkRasterPipeline::emboss, &fEmbossCtx);
458 // Now onward just as kA8.
459 p.append_gamut_clamp_if_normalized(fDst.info());
460 if (SkBlendMode_ShouldPreScaleCoverage(fBlend, /*rgb_coverage=*/false)) {
461 p.append(SkRasterPipeline::scale_u8, &fMaskPtr);
462 this->append_load_dst(&p);
463 SkBlendMode_AppendStages(fBlend, &p);
464 } else {
465 this->append_load_dst(&p);
466 SkBlendMode_AppendStages(fBlend, &p);
467 p.append(SkRasterPipeline::lerp_u8, &fMaskPtr);
468 }
469 this->append_store(&p);
470 fBlitMask3D = p.compile();
471 }
472
473 std::function<void(size_t,size_t,size_t,size_t)>* blitter = nullptr;
474 switch (mask.fFormat) {
475 case SkMask::kA8_Format: blitter = &fBlitMaskA8; break;
476 case SkMask::kLCD16_Format: blitter = &fBlitMaskLCD16; break;
477 case SkMask::k3D_Format: blitter = &fBlitMask3D; break;
478 default:
479 SkASSERT(false);
480 return;
481 }
482
483 SkASSERT(blitter);
484 (*blitter)(clip.left(),clip.top(), clip.width(),clip.height());
485 }
486