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