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