2 * Copyright 2016 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkArenaAlloc.h"
10 #include "SkBlendModePriv.h"
12 #include "SkColorFilter.h"
15 #include "SkPM4fPriv.h"
16 #include "SkRasterPipeline.h"
19 #include "../jumper/SkJumper.h"
21 class SkRasterPipelineBlitter final : public SkBlitter {
23 // Create using paint.getShader() or paint.getColor() if there is no shader.
24 // If there's a shader, we will modulate the shader color by the paint alpha.
25 static SkBlitter* Create(const SkPixmap&, const SkPaint&, SkArenaAlloc*, const SkMatrix& ctm);
27 // Create using pre-built shader pipeline.
28 // This pre-built pipeline must already have handled modulating with the paint alpha.
29 static SkBlitter* Create(const SkPixmap&, const SkPaint&, SkArenaAlloc*,
30 const SkRasterPipeline& shaderPipeline,
31 bool is_opaque, bool is_constant, bool wants_dither);
33 SkRasterPipelineBlitter(SkPixmap dst, SkBlendMode blend, SkArenaAlloc* alloc)
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 blitMask (const SkMask&, const SkIRect& clip) override;
43 // TODO: The default implementations of the other blits look fine,
44 // but some of them like blitV could probably benefit from custom
45 // blits using something like a SkRasterPipeline::runFew() method.
48 void append_load_d(SkRasterPipeline*) const;
49 void append_blend (SkRasterPipeline*) const;
50 void maybe_clamp (SkRasterPipeline*) const;
51 void append_store (SkRasterPipeline*) const;
56 SkRasterPipeline fColorPipeline;
58 // We may be able to specialize blitH() into a memset.
59 bool fCanMemsetInBlitH = false;
60 uint64_t fMemsetColor = 0; // Big enough for largest dst format, F16.
62 // Built lazily on first use.
63 std::function<void(size_t, size_t)> fBlitH,
68 // These values are pointed to by the blit pipelines above,
69 // which allows us to adjust them from call to call.
70 void* fDstPtr = nullptr;
71 const void* fMaskPtr = nullptr;
72 float fCurrentCoverage = 0.0f;
74 SkJumper_DitherCtx fDitherCtx = { &fCurrentY, 0.0f };
76 typedef SkBlitter INHERITED;
79 SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst,
82 SkArenaAlloc* alloc) {
83 return SkRasterPipelineBlitter::Create(dst, paint, alloc, ctm);
86 SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst,
88 const SkRasterPipeline& shaderPipeline,
91 SkArenaAlloc* alloc) {
92 bool is_constant = false; // If this were the case, it'd be better to just set a paint color.
93 return SkRasterPipelineBlitter::Create(dst, paint, alloc,
94 shaderPipeline, is_opaque, is_constant, wants_dither);
97 SkBlitter* SkRasterPipelineBlitter::Create(const SkPixmap& dst,
100 const SkMatrix& ctm) {
101 auto paintColor = alloc->make<SkPM4f>(SkPM4f_from_SkColor(paint.getColor(),
103 if (auto shader = paint.getShader()) {
104 SkRasterPipeline shaderPipeline;
105 if (!shader->appendStages(&shaderPipeline, dst.colorSpace(), alloc, ctm, paint)) {
106 // When a shader fails to append stages, it means it has vetoed drawing entirely.
107 return alloc->make<SkNullBlitter>();
110 bool is_opaque = shader->isOpaque();
111 if (paintColor->a() != 1.0f) {
112 shaderPipeline.append(SkRasterPipeline::scale_1_float, &paintColor->fVec[SkPM4f::A]);
116 bool is_constant = shader->isConstant();
117 bool wants_dither = shader->asAGradient(nullptr) >= SkShader::kLinear_GradientType;
118 return Create(dst, paint, alloc, shaderPipeline, is_opaque, is_constant, wants_dither);
121 SkRasterPipeline shaderPipeline;
122 shaderPipeline.append(SkRasterPipeline::constant_color, paintColor);
123 bool is_opaque = paintColor->a() == 1.0f,
125 wants_dither = false;
126 return Create(dst, paint, alloc, shaderPipeline, is_opaque, is_constant, wants_dither);
129 SkBlitter* SkRasterPipelineBlitter::Create(const SkPixmap& dst,
130 const SkPaint& paint,
132 const SkRasterPipeline& shaderPipeline,
136 auto blitter = alloc->make<SkRasterPipelineBlitter>(dst, paint.getBlendMode(), alloc);
138 // Our job in this factory is to fill out the blitter's color pipeline.
139 // This is the common front of the full blit pipelines, each constructed lazily on first use.
140 // The full blit pipelines handle reading and writing the dst, blending, coverage, dithering.
141 auto colorPipeline = &blitter->fColorPipeline;
143 // Let's get the shader in first. If the shader's not constant, it'll need seeding with x,y.
145 colorPipeline->append(SkRasterPipeline::seed_shader, &blitter->fCurrentY);
147 colorPipeline->extend(shaderPipeline);
149 // If there's a color filter it comes next.
150 if (auto colorFilter = paint.getColorFilter()) {
151 colorFilter->appendStages(colorPipeline, dst.colorSpace(), alloc, is_opaque);
152 is_opaque = is_opaque && (colorFilter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag);
155 // We'll dither if the shader wants to, or if we're drawing 565 and the paint wants to.
156 // Not all formats make sense to dither (think, F16). We set their dither rate to zero.
157 // We need to decide if we're going to dither now to keep is_constant accurate.
159 (paint.isDither() && dst.info().colorType() == kRGB_565_SkColorType)) {
160 switch (dst.info().colorType()) {
161 default: blitter->fDitherCtx.rate = 0.0f; break;
162 case kRGB_565_SkColorType: blitter->fDitherCtx.rate = 1/63.0f; break;
163 case kRGBA_8888_SkColorType:
164 case kBGRA_8888_SkColorType: blitter->fDitherCtx.rate = 1/255.0f; break;
167 is_constant = is_constant && (blitter->fDitherCtx.rate == 0.0f);
169 // We're logically done here. The code between here and return blitter is all optimization.
171 // A pipeline that's still constant here can collapse back into a constant color.
173 auto constantColor = alloc->make<SkPM4f>();
174 colorPipeline->append(SkRasterPipeline::store_f32, &constantColor);
175 colorPipeline->run(0,1);
176 *colorPipeline = SkRasterPipeline();
177 colorPipeline->append(SkRasterPipeline::constant_color, constantColor);
179 is_opaque = constantColor->a() == 1.0f;
182 // We can strength-reduce SrcOver into Src when opaque.
183 if (is_opaque && blitter->fBlend == SkBlendMode::kSrcOver) {
184 blitter->fBlend = SkBlendMode::kSrc;
187 // When we're drawing a constant color in Src mode, we can sometimes just memset.
188 // (The previous two optimizations help find more opportunities for this one.)
189 if (is_constant && blitter->fBlend == SkBlendMode::kSrc) {
190 // Run our color pipeline all the way through to produce what we'd memset when we can.
191 // Not all blits can memset, so we need to keep colorPipeline too.
193 p.extend(*colorPipeline);
194 blitter->fDstPtr = &blitter->fMemsetColor;
195 blitter->append_store(&p);
198 blitter->fCanMemsetInBlitH = true;
204 void SkRasterPipelineBlitter::append_load_d(SkRasterPipeline* p) const {
205 p->append(SkRasterPipeline::move_src_dst);
206 switch (fDst.info().colorType()) {
207 case kAlpha_8_SkColorType: p->append(SkRasterPipeline::load_a8, &fDstPtr); break;
208 case kRGB_565_SkColorType: p->append(SkRasterPipeline::load_565, &fDstPtr); break;
209 case kBGRA_8888_SkColorType:
210 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::load_8888, &fDstPtr); break;
211 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::load_f16, &fDstPtr); break;
214 if (fDst.info().colorType() == kBGRA_8888_SkColorType) {
215 p->append(SkRasterPipeline::swap_rb);
217 if (fDst.info().gammaCloseToSRGB()) {
218 p->append_from_srgb(fDst.info().alphaType());
220 p->append(SkRasterPipeline::swap);
223 void SkRasterPipelineBlitter::append_store(SkRasterPipeline* p) const {
224 if (fDst.info().gammaCloseToSRGB()) {
225 p->append(SkRasterPipeline::to_srgb);
227 if (fDitherCtx.rate > 0.0f) {
228 // We dither after any sRGB transfer function to make sure our 1/255.0f is sensible
229 // over the whole range. If we did it before, 1/255.0f is too big a rate near zero.
230 p->append(SkRasterPipeline::dither, &fDitherCtx);
233 if (fDst.info().colorType() == kBGRA_8888_SkColorType) {
234 p->append(SkRasterPipeline::swap_rb);
236 switch (fDst.info().colorType()) {
237 case kAlpha_8_SkColorType: p->append(SkRasterPipeline::store_a8, &fDstPtr); break;
238 case kRGB_565_SkColorType: p->append(SkRasterPipeline::store_565, &fDstPtr); break;
239 case kBGRA_8888_SkColorType:
240 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::store_8888, &fDstPtr); break;
241 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::store_f16, &fDstPtr); break;
246 void SkRasterPipelineBlitter::append_blend(SkRasterPipeline* p) const {
247 SkBlendMode_AppendStages(fBlend, p);
250 void SkRasterPipelineBlitter::maybe_clamp(SkRasterPipeline* p) const {
251 if (SkBlendMode_CanOverflow(fBlend)) {
252 p->append(SkRasterPipeline::clamp_a);
256 void SkRasterPipelineBlitter::blitH(int x, int y, int w) {
257 fDstPtr = fDst.writable_addr(0,y);
260 if (fCanMemsetInBlitH) {
261 switch (fDst.shiftPerPixel()) {
262 case 0: memset ((uint8_t *)fDstPtr + x, fMemsetColor, w); return;
263 case 1: sk_memset16((uint16_t*)fDstPtr + x, fMemsetColor, w); return;
264 case 2: sk_memset32((uint32_t*)fDstPtr + x, fMemsetColor, w); return;
265 case 3: sk_memset64((uint64_t*)fDstPtr + x, fMemsetColor, w); return;
272 p.extend(fColorPipeline);
273 if (fBlend != SkBlendMode::kSrc) {
274 this->append_load_d(&p);
275 this->append_blend(&p);
276 this->maybe_clamp(&p);
278 this->append_store(&p);
279 fBlitH = p.compile(fAlloc);
284 void SkRasterPipelineBlitter::blitAntiH(int x, int y, const SkAlpha aa[], const int16_t runs[]) {
287 p.extend(fColorPipeline);
288 if (fBlend == SkBlendMode::kSrcOver) {
289 p.append(SkRasterPipeline::scale_1_float, &fCurrentCoverage);
290 this->append_load_d(&p);
291 this->append_blend(&p);
293 this->append_load_d(&p);
294 this->append_blend(&p);
295 p.append(SkRasterPipeline::lerp_1_float, &fCurrentCoverage);
297 this->maybe_clamp(&p);
298 this->append_store(&p);
299 fBlitAntiH = p.compile(fAlloc);
302 fDstPtr = fDst.writable_addr(0,y);
304 for (int16_t run = *runs; run > 0; run = *runs) {
307 case 0xff: this->blitH(x,y,run); break;
309 fCurrentCoverage = *aa * (1/255.0f);
318 void SkRasterPipelineBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
319 if (mask.fFormat == SkMask::kBW_Format) {
320 // TODO: native BW masks?
321 return INHERITED::blitMask(mask, clip);
324 if (mask.fFormat == SkMask::kA8_Format && !fBlitMaskA8) {
326 p.extend(fColorPipeline);
327 if (fBlend == SkBlendMode::kSrcOver) {
328 p.append(SkRasterPipeline::scale_u8, &fMaskPtr);
329 this->append_load_d(&p);
330 this->append_blend(&p);
332 this->append_load_d(&p);
333 this->append_blend(&p);
334 p.append(SkRasterPipeline::lerp_u8, &fMaskPtr);
336 this->maybe_clamp(&p);
337 this->append_store(&p);
338 fBlitMaskA8 = p.compile(fAlloc);
341 if (mask.fFormat == SkMask::kLCD16_Format && !fBlitMaskLCD16) {
343 p.extend(fColorPipeline);
344 this->append_load_d(&p);
345 this->append_blend(&p);
346 p.append(SkRasterPipeline::lerp_565, &fMaskPtr);
347 this->maybe_clamp(&p);
348 this->append_store(&p);
349 fBlitMaskLCD16 = p.compile(fAlloc);
353 for (int y = clip.top(); y < clip.bottom(); y++) {
354 fDstPtr = fDst.writable_addr(0,y);
357 switch (mask.fFormat) {
358 case SkMask::kA8_Format:
359 fMaskPtr = mask.getAddr8(x,y)-x;
360 fBlitMaskA8(x,clip.width());
362 case SkMask::kLCD16_Format:
363 fMaskPtr = mask.getAddrLCD16(x,y)-x;
364 fBlitMaskLCD16(x,clip.width());