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