mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 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 | |
Herb Derby | ac04fef | 2017-01-13 17:34:33 -0500 | [diff] [blame] | 8 | #include "SkArenaAlloc.h" |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 9 | #include "SkBlitter.h" |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 10 | #include "SkBlendModePriv.h" |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 11 | #include "SkColor.h" |
| 12 | #include "SkColorFilter.h" |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame^] | 13 | #include "SkColorSpaceXformer.h" |
Mike Klein | baaf8ad | 2016-09-29 09:04:15 -0400 | [diff] [blame] | 14 | #include "SkOpts.h" |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 15 | #include "SkPM4f.h" |
Mike Klein | 744908e | 2016-11-11 12:51:36 -0500 | [diff] [blame] | 16 | #include "SkPM4fPriv.h" |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 17 | #include "SkRasterPipeline.h" |
| 18 | #include "SkShader.h" |
Mike Klein | 14c8f82 | 2016-11-30 19:39:43 -0500 | [diff] [blame] | 19 | #include "SkUtils.h" |
Mike Klein | 581e698 | 2017-05-03 13:05:13 -0400 | [diff] [blame] | 20 | #include "../jumper/SkJumper.h" |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 21 | |
Mike Klein | 4e3bc86 | 2017-05-22 20:15:17 +0000 | [diff] [blame] | 22 | class SkRasterPipelineBlitter final : public SkBlitter { |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 23 | public: |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame^] | 24 | // This is our common entrypoint for creating the blitter once we've sorted out shaders. |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 25 | static SkBlitter* Create(const SkPixmap&, const SkPaint&, SkArenaAlloc*, |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame^] | 26 | const SkRasterPipeline& shaderPipeline, SkShader::Context* shaderCtx, |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 27 | bool is_opaque, bool is_constant, bool wants_dither); |
| 28 | |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame^] | 29 | SkRasterPipelineBlitter(SkPixmap dst, |
| 30 | SkBlendMode blend, |
| 31 | SkArenaAlloc* alloc, |
| 32 | SkShader::Context* shaderCtx) |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 33 | : fDst(dst) |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 34 | , fBlend(blend) |
Mike Klein | 0a76b41 | 2017-05-22 12:01:59 -0400 | [diff] [blame] | 35 | , fAlloc(alloc) |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame^] | 36 | , fShaderCtx(shaderCtx) |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 37 | , fColorPipeline(alloc) |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 38 | {} |
| 39 | |
| 40 | void blitH (int x, int y, int w) override; |
| 41 | void blitAntiH(int x, int y, const SkAlpha[], const int16_t[]) override; |
| 42 | void blitMask (const SkMask&, const SkIRect& clip) override; |
| 43 | |
| 44 | // TODO: The default implementations of the other blits look fine, |
| 45 | // but some of them like blitV could probably benefit from custom |
| 46 | // blits using something like a SkRasterPipeline::runFew() method. |
| 47 | |
| 48 | private: |
Mike Klein | 14c8f82 | 2016-11-30 19:39:43 -0500 | [diff] [blame] | 49 | void append_load_d(SkRasterPipeline*) const; |
| 50 | void append_blend (SkRasterPipeline*) const; |
| 51 | void maybe_clamp (SkRasterPipeline*) const; |
| 52 | void append_store (SkRasterPipeline*) const; |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 53 | |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame^] | 54 | // If we have an SkShader::Context, use it to fill our shader buffer. |
| 55 | void maybe_shade(int x, int y, int w); |
| 56 | |
| 57 | SkPixmap fDst; |
| 58 | SkBlendMode fBlend; |
| 59 | SkArenaAlloc* fAlloc; |
| 60 | SkShader::Context* fShaderCtx; |
| 61 | SkRasterPipeline fColorPipeline; |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 62 | |
Mike Klein | 8729e5b | 2017-02-16 06:51:48 -0500 | [diff] [blame] | 63 | // We may be able to specialize blitH() into a memset. |
| 64 | bool fCanMemsetInBlitH = false; |
| 65 | uint64_t fMemsetColor = 0; // Big enough for largest dst format, F16. |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 66 | |
Mike Klein | 8729e5b | 2017-02-16 06:51:48 -0500 | [diff] [blame] | 67 | // Built lazily on first use. |
Mike Klein | 0a76b41 | 2017-05-22 12:01:59 -0400 | [diff] [blame] | 68 | std::function<void(size_t, size_t)> fBlitH, |
| 69 | fBlitAntiH, |
| 70 | fBlitMaskA8, |
| 71 | fBlitMaskLCD16; |
Mike Klein | 8729e5b | 2017-02-16 06:51:48 -0500 | [diff] [blame] | 72 | |
| 73 | // These values are pointed to by the blit pipelines above, |
| 74 | // which allows us to adjust them from call to call. |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame^] | 75 | void* fShaderOutput = nullptr; |
Mike Klein | db711c9 | 2017-05-03 17:57:48 -0400 | [diff] [blame] | 76 | void* fDstPtr = nullptr; |
| 77 | const void* fMaskPtr = nullptr; |
| 78 | float fCurrentCoverage = 0.0f; |
| 79 | int fCurrentY = 0; |
| 80 | SkJumper_DitherCtx fDitherCtx = { &fCurrentY, 0.0f }; |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 81 | |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame^] | 82 | std::vector<SkPM4f> fShaderBuffer; |
| 83 | |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 84 | typedef SkBlitter INHERITED; |
| 85 | }; |
| 86 | |
| 87 | SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst, |
| 88 | const SkPaint& paint, |
Mike Klein | fb191da | 2016-11-15 13:20:33 -0500 | [diff] [blame] | 89 | const SkMatrix& ctm, |
Matt Sarett | 25834ff | 2017-02-15 15:54:35 -0500 | [diff] [blame] | 90 | SkArenaAlloc* alloc) { |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame^] | 91 | SkColorSpace* dstCS = dst.colorSpace(); |
| 92 | auto paintColor = alloc->make<SkPM4f>(SkPM4f_from_SkColor(paint.getColor(), dstCS)); |
| 93 | auto shader = paint.getShader(); |
| 94 | |
| 95 | SkRasterPipeline_<256> shaderPipeline; |
| 96 | if (!shader) { |
| 97 | // Having no shader makes things nice and easy... just use the paint color. |
| 98 | shaderPipeline.append(SkRasterPipeline::constant_color, paintColor); |
| 99 | bool is_opaque = paintColor->a() == 1.0f, |
| 100 | is_constant = true, |
| 101 | wants_dither = false; |
| 102 | return SkRasterPipelineBlitter::Create(dst, paint, alloc, |
| 103 | shaderPipeline, nullptr, |
| 104 | is_opaque, is_constant, wants_dither); |
| 105 | } |
| 106 | |
| 107 | bool is_opaque = shader->isOpaque() && paintColor->a() == 1.0f; |
| 108 | bool is_constant = shader->isConstant(); |
| 109 | bool wants_dither = shader->asAGradient(nullptr) >= SkShader::kLinear_GradientType; |
| 110 | |
| 111 | // See if the shader can express itself by appending pipeline stages. |
| 112 | if (shader->appendStages(&shaderPipeline, dstCS, alloc, ctm, paint)) { |
| 113 | if (paintColor->a() != 1.0f) { |
| 114 | shaderPipeline.append(SkRasterPipeline::scale_1_float, &paintColor->fVec[SkPM4f::A]); |
| 115 | } |
| 116 | return SkRasterPipelineBlitter::Create(dst, paint, alloc, |
| 117 | shaderPipeline, nullptr, |
| 118 | is_opaque, is_constant, wants_dither); |
| 119 | } |
| 120 | |
| 121 | // No, the shader wants us to create a context and call shadeSpan4f(). |
| 122 | SkASSERT(!is_constant); // All constant shaders should be able to appendStages(). |
| 123 | |
| 124 | if (dstCS) { |
| 125 | // We need to transform the shader into the dst color space, and extend its lifetime. |
| 126 | sk_sp<SkShader> in_dstCS = SkColorSpaceXformer::Make(sk_ref_sp(dstCS))->apply(shader); |
| 127 | shader = in_dstCS.get(); |
| 128 | alloc->make<sk_sp<SkShader>>(std::move(in_dstCS)); |
| 129 | } |
| 130 | SkShader::ContextRec rec(paint, ctm, nullptr, SkShader::ContextRec::kPM4f_DstType, dstCS); |
| 131 | SkShader::Context* shaderCtx = shader->makeContext(rec, alloc); |
| 132 | if (!shaderCtx) { |
| 133 | // When a shader fails to create a context, it has vetoed drawing entirely. |
| 134 | return alloc->make<SkNullBlitter>(); |
| 135 | } |
| 136 | return SkRasterPipelineBlitter::Create(dst, paint, alloc, |
| 137 | shaderPipeline, shaderCtx, |
| 138 | is_opaque, is_constant, wants_dither); |
Mike Reed | 0264095 | 2017-05-19 15:32:13 -0400 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst, |
| 142 | const SkPaint& paint, |
Mike Reed | 0264095 | 2017-05-19 15:32:13 -0400 | [diff] [blame] | 143 | const SkRasterPipeline& shaderPipeline, |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 144 | bool is_opaque, |
| 145 | bool wants_dither, |
Mike Reed | 0264095 | 2017-05-19 15:32:13 -0400 | [diff] [blame] | 146 | SkArenaAlloc* alloc) { |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 147 | bool is_constant = false; // If this were the case, it'd be better to just set a paint color. |
| 148 | return SkRasterPipelineBlitter::Create(dst, paint, alloc, |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame^] | 149 | shaderPipeline, nullptr, |
| 150 | is_opaque, is_constant, wants_dither); |
Mike Reed | 0264095 | 2017-05-19 15:32:13 -0400 | [diff] [blame] | 151 | } |
| 152 | |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 153 | SkBlitter* SkRasterPipelineBlitter::Create(const SkPixmap& dst, |
| 154 | const SkPaint& paint, |
| 155 | SkArenaAlloc* alloc, |
| 156 | const SkRasterPipeline& shaderPipeline, |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame^] | 157 | SkShader::Context* shaderCtx, |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 158 | bool is_opaque, |
| 159 | bool is_constant, |
| 160 | bool wants_dither) { |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame^] | 161 | auto blitter = alloc->make<SkRasterPipelineBlitter>(dst, |
| 162 | paint.getBlendMode(), |
| 163 | alloc, |
| 164 | shaderCtx); |
Mike Reed | 0264095 | 2017-05-19 15:32:13 -0400 | [diff] [blame] | 165 | |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 166 | // Our job in this factory is to fill out the blitter's color pipeline. |
| 167 | // This is the common front of the full blit pipelines, each constructed lazily on first use. |
| 168 | // The full blit pipelines handle reading and writing the dst, blending, coverage, dithering. |
| 169 | auto colorPipeline = &blitter->fColorPipeline; |
| 170 | |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame^] | 171 | // Let's get the shader in first. |
| 172 | if (shaderCtx) { |
| 173 | colorPipeline->append(SkRasterPipeline::load_f32, &blitter->fShaderOutput); |
| 174 | } else { |
| 175 | // If the shader's not constant, it'll need seeding with x,y. |
| 176 | if (!is_constant) { |
| 177 | colorPipeline->append(SkRasterPipeline::seed_shader, &blitter->fCurrentY); |
| 178 | } |
| 179 | colorPipeline->extend(shaderPipeline); |
Mike Reed | 0264095 | 2017-05-19 15:32:13 -0400 | [diff] [blame] | 180 | } |
| 181 | |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 182 | // If there's a color filter it comes next. |
| 183 | if (auto colorFilter = paint.getColorFilter()) { |
| 184 | colorFilter->appendStages(colorPipeline, dst.colorSpace(), alloc, is_opaque); |
mtklein | a4a4488 | 2016-11-04 13:20:07 -0700 | [diff] [blame] | 185 | is_opaque = is_opaque && (colorFilter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag); |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 188 | // We'll dither if the shader wants to, or if we're drawing 565 and the paint wants to. |
| 189 | // Not all formats make sense to dither (think, F16). We set their dither rate to zero. |
| 190 | // We need to decide if we're going to dither now to keep is_constant accurate. |
| 191 | if (wants_dither || |
| 192 | (paint.isDither() && dst.info().colorType() == kRGB_565_SkColorType)) { |
| 193 | switch (dst.info().colorType()) { |
| 194 | default: blitter->fDitherCtx.rate = 0.0f; break; |
| 195 | case kRGB_565_SkColorType: blitter->fDitherCtx.rate = 1/63.0f; break; |
| 196 | case kRGBA_8888_SkColorType: |
| 197 | case kBGRA_8888_SkColorType: blitter->fDitherCtx.rate = 1/255.0f; break; |
| 198 | } |
| 199 | } |
| 200 | is_constant = is_constant && (blitter->fDitherCtx.rate == 0.0f); |
| 201 | |
| 202 | // We're logically done here. The code between here and return blitter is all optimization. |
| 203 | |
| 204 | // A pipeline that's still constant here can collapse back into a constant color. |
Mike Klein | 14c8f82 | 2016-11-30 19:39:43 -0500 | [diff] [blame] | 205 | if (is_constant) { |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 206 | auto constantColor = alloc->make<SkPM4f>(); |
| 207 | colorPipeline->append(SkRasterPipeline::store_f32, &constantColor); |
| 208 | colorPipeline->run(0,1); |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 209 | colorPipeline->reset(); |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 210 | colorPipeline->append(SkRasterPipeline::constant_color, constantColor); |
mtklein | a4a4488 | 2016-11-04 13:20:07 -0700 | [diff] [blame] | 211 | |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 212 | is_opaque = constantColor->a() == 1.0f; |
Mike Klein | 6686617 | 2016-11-03 12:22:01 -0400 | [diff] [blame] | 213 | } |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 214 | |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 215 | // We can strength-reduce SrcOver into Src when opaque. |
| 216 | if (is_opaque && blitter->fBlend == SkBlendMode::kSrcOver) { |
| 217 | blitter->fBlend = SkBlendMode::kSrc; |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 220 | // When we're drawing a constant color in Src mode, we can sometimes just memset. |
| 221 | // (The previous two optimizations help find more opportunities for this one.) |
| 222 | if (is_constant && blitter->fBlend == SkBlendMode::kSrc) { |
| 223 | // Run our color pipeline all the way through to produce what we'd memset when we can. |
| 224 | // Not all blits can memset, so we need to keep colorPipeline too. |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 225 | SkRasterPipeline_<256> p; |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 226 | p.extend(*colorPipeline); |
| 227 | blitter->fDstPtr = &blitter->fMemsetColor; |
| 228 | blitter->append_store(&p); |
Mike Klein | 319ba3d | 2017-01-20 15:11:54 -0500 | [diff] [blame] | 229 | p.run(0,1); |
Mike Klein | 14c8f82 | 2016-11-30 19:39:43 -0500 | [diff] [blame] | 230 | |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 231 | blitter->fCanMemsetInBlitH = true; |
Mike Klein | 14c8f82 | 2016-11-30 19:39:43 -0500 | [diff] [blame] | 232 | } |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 233 | |
| 234 | return blitter; |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 237 | void SkRasterPipelineBlitter::append_load_d(SkRasterPipeline* p) const { |
Mike Klein | 8c8cb5b | 2017-01-06 10:21:56 -0500 | [diff] [blame] | 238 | p->append(SkRasterPipeline::move_src_dst); |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 239 | switch (fDst.info().colorType()) { |
Mike Klein | e71b167 | 2017-01-13 07:59:23 -0500 | [diff] [blame] | 240 | case kAlpha_8_SkColorType: p->append(SkRasterPipeline::load_a8, &fDstPtr); break; |
Mike Klein | 8c8cb5b | 2017-01-06 10:21:56 -0500 | [diff] [blame] | 241 | case kRGB_565_SkColorType: p->append(SkRasterPipeline::load_565, &fDstPtr); break; |
Mike Klein | e03339a | 2016-11-28 13:24:27 -0500 | [diff] [blame] | 242 | case kBGRA_8888_SkColorType: |
Mike Klein | 8c8cb5b | 2017-01-06 10:21:56 -0500 | [diff] [blame] | 243 | case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::load_8888, &fDstPtr); break; |
| 244 | case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::load_f16, &fDstPtr); break; |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 245 | default: break; |
| 246 | } |
Mike Klein | e03339a | 2016-11-28 13:24:27 -0500 | [diff] [blame] | 247 | if (fDst.info().colorType() == kBGRA_8888_SkColorType) { |
Mike Klein | 8c8cb5b | 2017-01-06 10:21:56 -0500 | [diff] [blame] | 248 | p->append(SkRasterPipeline::swap_rb); |
Mike Klein | e03339a | 2016-11-28 13:24:27 -0500 | [diff] [blame] | 249 | } |
| 250 | if (fDst.info().gammaCloseToSRGB()) { |
Mike Klein | 8c8cb5b | 2017-01-06 10:21:56 -0500 | [diff] [blame] | 251 | p->append_from_srgb(fDst.info().alphaType()); |
Mike Klein | e03339a | 2016-11-28 13:24:27 -0500 | [diff] [blame] | 252 | } |
Mike Klein | 8c8cb5b | 2017-01-06 10:21:56 -0500 | [diff] [blame] | 253 | p->append(SkRasterPipeline::swap); |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Mike Klein | 14c8f82 | 2016-11-30 19:39:43 -0500 | [diff] [blame] | 256 | void SkRasterPipelineBlitter::append_store(SkRasterPipeline* p) const { |
Matt Sarett | f388093 | 2017-03-24 10:06:03 -0400 | [diff] [blame] | 257 | if (fDst.info().gammaCloseToSRGB()) { |
Mike Klein | e03339a | 2016-11-28 13:24:27 -0500 | [diff] [blame] | 258 | p->append(SkRasterPipeline::to_srgb); |
| 259 | } |
Mike Klein | db711c9 | 2017-05-03 17:57:48 -0400 | [diff] [blame] | 260 | if (fDitherCtx.rate > 0.0f) { |
| 261 | // We dither after any sRGB transfer function to make sure our 1/255.0f is sensible |
| 262 | // over the whole range. If we did it before, 1/255.0f is too big a rate near zero. |
| 263 | p->append(SkRasterPipeline::dither, &fDitherCtx); |
| 264 | } |
| 265 | |
Mike Klein | e03339a | 2016-11-28 13:24:27 -0500 | [diff] [blame] | 266 | if (fDst.info().colorType() == kBGRA_8888_SkColorType) { |
| 267 | p->append(SkRasterPipeline::swap_rb); |
| 268 | } |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 269 | switch (fDst.info().colorType()) { |
Mike Klein | e71b167 | 2017-01-13 07:59:23 -0500 | [diff] [blame] | 270 | case kAlpha_8_SkColorType: p->append(SkRasterPipeline::store_a8, &fDstPtr); break; |
Mike Klein | e03339a | 2016-11-28 13:24:27 -0500 | [diff] [blame] | 271 | case kRGB_565_SkColorType: p->append(SkRasterPipeline::store_565, &fDstPtr); break; |
| 272 | case kBGRA_8888_SkColorType: |
| 273 | case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::store_8888, &fDstPtr); break; |
| 274 | case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::store_f16, &fDstPtr); break; |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 275 | default: break; |
| 276 | } |
| 277 | } |
| 278 | |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 279 | void SkRasterPipelineBlitter::append_blend(SkRasterPipeline* p) const { |
Mike Klein | bb33833 | 2017-05-04 12:42:52 -0400 | [diff] [blame] | 280 | SkBlendMode_AppendStages(fBlend, p); |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 281 | } |
Mike Klein | e9f74b8 | 2016-10-25 13:31:21 -0400 | [diff] [blame] | 282 | |
Mike Klein | 130863e | 2016-10-27 11:29:36 -0400 | [diff] [blame] | 283 | void SkRasterPipelineBlitter::maybe_clamp(SkRasterPipeline* p) const { |
Mike Klein | e03339a | 2016-11-28 13:24:27 -0500 | [diff] [blame] | 284 | if (SkBlendMode_CanOverflow(fBlend)) { |
| 285 | p->append(SkRasterPipeline::clamp_a); |
| 286 | } |
Mike Klein | 130863e | 2016-10-27 11:29:36 -0400 | [diff] [blame] | 287 | } |
| 288 | |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame^] | 289 | void SkRasterPipelineBlitter::maybe_shade(int x, int y, int w) { |
| 290 | if (fShaderCtx) { |
| 291 | if (w > SkToInt(fShaderBuffer.size())) { |
| 292 | fShaderBuffer.resize(w); |
| 293 | } |
| 294 | fShaderCtx->shadeSpan4f(x,y, fShaderBuffer.data(), w); |
| 295 | // We'll be reading from fShaderOutput + x. |
| 296 | fShaderOutput = fShaderBuffer.data() - x; |
| 297 | } |
| 298 | } |
| 299 | |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 300 | void SkRasterPipelineBlitter::blitH(int x, int y, int w) { |
Mike Klein | 8729e5b | 2017-02-16 06:51:48 -0500 | [diff] [blame] | 301 | fDstPtr = fDst.writable_addr(0,y); |
| 302 | fCurrentY = y; |
| 303 | |
| 304 | if (fCanMemsetInBlitH) { |
| 305 | switch (fDst.shiftPerPixel()) { |
Mike Klein | 8612548 | 2017-02-17 16:10:46 +0000 | [diff] [blame] | 306 | case 0: memset ((uint8_t *)fDstPtr + x, fMemsetColor, w); return; |
| 307 | case 1: sk_memset16((uint16_t*)fDstPtr + x, fMemsetColor, w); return; |
| 308 | case 2: sk_memset32((uint32_t*)fDstPtr + x, fMemsetColor, w); return; |
| 309 | case 3: sk_memset64((uint64_t*)fDstPtr + x, fMemsetColor, w); return; |
Mike Klein | 8729e5b | 2017-02-16 06:51:48 -0500 | [diff] [blame] | 310 | default: break; |
| 311 | } |
| 312 | } |
| 313 | |
Mike Klein | 0a76b41 | 2017-05-22 12:01:59 -0400 | [diff] [blame] | 314 | if (!fBlitH) { |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 315 | SkRasterPipeline p(fAlloc); |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 316 | p.extend(fColorPipeline); |
Mike Klein | 6686617 | 2016-11-03 12:22:01 -0400 | [diff] [blame] | 317 | if (fBlend != SkBlendMode::kSrc) { |
| 318 | this->append_load_d(&p); |
| 319 | this->append_blend(&p); |
| 320 | this->maybe_clamp(&p); |
| 321 | } |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 322 | this->append_store(&p); |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 323 | fBlitH = p.compile(); |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 324 | } |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame^] | 325 | this->maybe_shade(x,y,w); |
Mike Klein | 0a76b41 | 2017-05-22 12:01:59 -0400 | [diff] [blame] | 326 | fBlitH(x,w); |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | void SkRasterPipelineBlitter::blitAntiH(int x, int y, const SkAlpha aa[], const int16_t runs[]) { |
Mike Klein | 0a76b41 | 2017-05-22 12:01:59 -0400 | [diff] [blame] | 330 | if (!fBlitAntiH) { |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 331 | SkRasterPipeline p(fAlloc); |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 332 | p.extend(fColorPipeline); |
Mike Klein | 6686617 | 2016-11-03 12:22:01 -0400 | [diff] [blame] | 333 | if (fBlend == SkBlendMode::kSrcOver) { |
Mike Klein | babd93e | 2016-11-30 16:05:10 -0500 | [diff] [blame] | 334 | p.append(SkRasterPipeline::scale_1_float, &fCurrentCoverage); |
Mike Klein | 6686617 | 2016-11-03 12:22:01 -0400 | [diff] [blame] | 335 | this->append_load_d(&p); |
| 336 | this->append_blend(&p); |
| 337 | } else { |
| 338 | this->append_load_d(&p); |
| 339 | this->append_blend(&p); |
Mike Klein | babd93e | 2016-11-30 16:05:10 -0500 | [diff] [blame] | 340 | p.append(SkRasterPipeline::lerp_1_float, &fCurrentCoverage); |
Mike Klein | 6686617 | 2016-11-03 12:22:01 -0400 | [diff] [blame] | 341 | } |
Mike Klein | 130863e | 2016-10-27 11:29:36 -0400 | [diff] [blame] | 342 | this->maybe_clamp(&p); |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 343 | this->append_store(&p); |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 344 | fBlitAntiH = p.compile(); |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 345 | } |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 346 | |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 347 | fDstPtr = fDst.writable_addr(0,y); |
Mike Klein | 319ba3d | 2017-01-20 15:11:54 -0500 | [diff] [blame] | 348 | fCurrentY = y; |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 349 | for (int16_t run = *runs; run > 0; run = *runs) { |
Mike Klein | aeb7959 | 2016-11-07 09:29:07 -0500 | [diff] [blame] | 350 | switch (*aa) { |
| 351 | case 0x00: break; |
| 352 | case 0xff: this->blitH(x,y,run); break; |
| 353 | default: |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame^] | 354 | this->maybe_shade(x,y,run); |
Mike Klein | babd93e | 2016-11-30 16:05:10 -0500 | [diff] [blame] | 355 | fCurrentCoverage = *aa * (1/255.0f); |
Mike Klein | 0a76b41 | 2017-05-22 12:01:59 -0400 | [diff] [blame] | 356 | fBlitAntiH(x,run); |
Mike Klein | aeb7959 | 2016-11-07 09:29:07 -0500 | [diff] [blame] | 357 | } |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 358 | x += run; |
| 359 | runs += run; |
| 360 | aa += run; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | void SkRasterPipelineBlitter::blitMask(const SkMask& mask, const SkIRect& clip) { |
| 365 | if (mask.fFormat == SkMask::kBW_Format) { |
| 366 | // TODO: native BW masks? |
| 367 | return INHERITED::blitMask(mask, clip); |
| 368 | } |
| 369 | |
Mike Klein | 0a76b41 | 2017-05-22 12:01:59 -0400 | [diff] [blame] | 370 | if (mask.fFormat == SkMask::kA8_Format && !fBlitMaskA8) { |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 371 | SkRasterPipeline p(fAlloc); |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 372 | p.extend(fColorPipeline); |
Mike Klein | 6686617 | 2016-11-03 12:22:01 -0400 | [diff] [blame] | 373 | if (fBlend == SkBlendMode::kSrcOver) { |
| 374 | p.append(SkRasterPipeline::scale_u8, &fMaskPtr); |
| 375 | this->append_load_d(&p); |
| 376 | this->append_blend(&p); |
| 377 | } else { |
| 378 | this->append_load_d(&p); |
| 379 | this->append_blend(&p); |
| 380 | p.append(SkRasterPipeline::lerp_u8, &fMaskPtr); |
| 381 | } |
Mike Klein | 130863e | 2016-10-27 11:29:36 -0400 | [diff] [blame] | 382 | this->maybe_clamp(&p); |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 383 | this->append_store(&p); |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 384 | fBlitMaskA8 = p.compile(); |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 385 | } |
| 386 | |
Mike Klein | 0a76b41 | 2017-05-22 12:01:59 -0400 | [diff] [blame] | 387 | if (mask.fFormat == SkMask::kLCD16_Format && !fBlitMaskLCD16) { |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 388 | SkRasterPipeline p(fAlloc); |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 389 | p.extend(fColorPipeline); |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 390 | this->append_load_d(&p); |
| 391 | this->append_blend(&p); |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 392 | p.append(SkRasterPipeline::lerp_565, &fMaskPtr); |
Mike Klein | 130863e | 2016-10-27 11:29:36 -0400 | [diff] [blame] | 393 | this->maybe_clamp(&p); |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 394 | this->append_store(&p); |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 395 | fBlitMaskLCD16 = p.compile(); |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | int x = clip.left(); |
| 399 | for (int y = clip.top(); y < clip.bottom(); y++) { |
| 400 | fDstPtr = fDst.writable_addr(0,y); |
Mike Klein | 319ba3d | 2017-01-20 15:11:54 -0500 | [diff] [blame] | 401 | fCurrentY = y; |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 402 | |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame^] | 403 | this->maybe_shade(x,y,clip.width()); |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 404 | switch (mask.fFormat) { |
| 405 | case SkMask::kA8_Format: |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 406 | fMaskPtr = mask.getAddr8(x,y)-x; |
Mike Klein | 0a76b41 | 2017-05-22 12:01:59 -0400 | [diff] [blame] | 407 | fBlitMaskA8(x,clip.width()); |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 408 | break; |
| 409 | case SkMask::kLCD16_Format: |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 410 | fMaskPtr = mask.getAddrLCD16(x,y)-x; |
Mike Klein | 0a76b41 | 2017-05-22 12:01:59 -0400 | [diff] [blame] | 411 | fBlitMaskLCD16(x,clip.width()); |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 412 | break; |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 413 | default: |
| 414 | // TODO |
| 415 | break; |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 416 | } |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 417 | } |
| 418 | } |