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, |
Florin Malita | 47e55a5 | 2017-06-06 12:26:54 -0400 | [diff] [blame] | 28 | SkShaderBase::Context*, |
Mike Reed | eb7dc79 | 2017-06-12 12:48:45 -0400 | [diff] [blame] | 29 | bool is_opaque, bool is_constant); |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 30 | |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame] | 31 | SkRasterPipelineBlitter(SkPixmap dst, |
| 32 | SkBlendMode blend, |
Florin Malita | 47e55a5 | 2017-06-06 12:26:54 -0400 | [diff] [blame] | 33 | SkArenaAlloc* alloc, |
| 34 | SkShaderBase::Context* burstCtx) |
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) |
Florin Malita | 47e55a5 | 2017-06-06 12:26:54 -0400 | [diff] [blame] | 38 | , fBurstCtx(burstCtx) |
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 | |
Mike Klein | 34d10d7 | 2017-06-05 07:10:01 -0400 | [diff] [blame] | 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 blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) override; |
| 45 | void blitMask (const SkMask&, const SkIRect& clip) override; |
Mike Klein | 3b59af5 | 2017-07-20 16:02:12 -0400 | [diff] [blame] | 46 | void blitRect (int x, int y, int width, int height) override; |
Mike Reed | e6befa5 | 2017-08-28 11:30:48 -0400 | [diff] [blame] | 47 | void blitV (int x, int y, int height, SkAlpha alpha) override; |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 48 | |
| 49 | // TODO: The default implementations of the other blits look fine, |
| 50 | // but some of them like blitV could probably benefit from custom |
| 51 | // blits using something like a SkRasterPipeline::runFew() method. |
| 52 | |
| 53 | private: |
Mike Klein | 14c8f82 | 2016-11-30 19:39:43 -0500 | [diff] [blame] | 54 | void append_load_d(SkRasterPipeline*) const; |
| 55 | void append_blend (SkRasterPipeline*) const; |
Mike Klein | 14c8f82 | 2016-11-30 19:39:43 -0500 | [diff] [blame] | 56 | void append_store (SkRasterPipeline*) const; |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 57 | |
Florin Malita | 47e55a5 | 2017-06-06 12:26:54 -0400 | [diff] [blame] | 58 | // If we have an burst context, use it to fill our shader buffer. |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 59 | void burst_shade(int x, int y, int w); |
Florin Malita | 47e55a5 | 2017-06-06 12:26:54 -0400 | [diff] [blame] | 60 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 61 | SkPixmap fDst; |
| 62 | SkBlendMode fBlend; |
| 63 | SkArenaAlloc* fAlloc; |
Florin Malita | 47e55a5 | 2017-06-06 12:26:54 -0400 | [diff] [blame] | 64 | SkShaderBase::Context* fBurstCtx; |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 65 | SkRasterPipeline fColorPipeline; |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 66 | |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 67 | SkJumper_MemoryCtx fShaderOutput = {nullptr,0}, // Possibly updated each call to burst_shade(). |
| 68 | fDstPtr = {nullptr,0}, // Always points to the top-left of fDst. |
| 69 | fMaskPtr = {nullptr,0}; // Updated each call to blitMask(). |
| 70 | |
Mike Klein | 3b59af5 | 2017-07-20 16:02:12 -0400 | [diff] [blame] | 71 | // We may be able to specialize blitH() or blitRect() into a memset. |
| 72 | bool fCanMemsetInBlitRect = false; |
Mike Klein | 8729e5b | 2017-02-16 06:51:48 -0500 | [diff] [blame] | 73 | uint64_t fMemsetColor = 0; // Big enough for largest dst format, F16. |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 74 | |
Mike Klein | 8729e5b | 2017-02-16 06:51:48 -0500 | [diff] [blame] | 75 | // Built lazily on first use. |
Mike Klein | 3b59af5 | 2017-07-20 16:02:12 -0400 | [diff] [blame] | 76 | std::function<void(size_t, size_t, size_t, size_t)> fBlitRect, |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 77 | fBlitAntiH, |
| 78 | fBlitMaskA8, |
| 79 | fBlitMaskLCD16; |
Mike Klein | 8729e5b | 2017-02-16 06:51:48 -0500 | [diff] [blame] | 80 | |
| 81 | // These values are pointed to by the blit pipelines above, |
| 82 | // which allows us to adjust them from call to call. |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 83 | float fCurrentCoverage = 0.0f; |
| 84 | float fDitherRate = 0.0f; |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 85 | |
Florin Malita | 47e55a5 | 2017-06-06 12:26:54 -0400 | [diff] [blame] | 86 | std::vector<SkPM4f> fShaderBuffer; |
| 87 | |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 88 | typedef SkBlitter INHERITED; |
| 89 | }; |
| 90 | |
| 91 | SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst, |
| 92 | const SkPaint& paint, |
Mike Klein | fb191da | 2016-11-15 13:20:33 -0500 | [diff] [blame] | 93 | const SkMatrix& ctm, |
Matt Sarett | 25834ff | 2017-02-15 15:54:35 -0500 | [diff] [blame] | 94 | SkArenaAlloc* alloc) { |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame] | 95 | SkColorSpace* dstCS = dst.colorSpace(); |
Mike Reed | c91e387 | 2017-07-05 14:12:37 -0400 | [diff] [blame] | 96 | SkPM4f paintColor = SkPM4f_from_SkColor(paint.getColor(), dstCS); |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 97 | auto shader = as_SB(paint.getShader()); |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame] | 98 | |
| 99 | SkRasterPipeline_<256> shaderPipeline; |
| 100 | if (!shader) { |
| 101 | // Having no shader makes things nice and easy... just use the paint color. |
Mike Klein | 073073e | 2017-08-03 09:42:53 -0400 | [diff] [blame] | 102 | shaderPipeline.append_constant_color(alloc, paintColor); |
Mike Reed | c91e387 | 2017-07-05 14:12:37 -0400 | [diff] [blame] | 103 | bool is_opaque = paintColor.a() == 1.0f, |
Mike Reed | 98b7a6a | 2017-05-26 09:32:22 -0400 | [diff] [blame] | 104 | is_constant = true; |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame] | 105 | return SkRasterPipelineBlitter::Create(dst, paint, alloc, |
Florin Malita | 47e55a5 | 2017-06-06 12:26:54 -0400 | [diff] [blame] | 106 | shaderPipeline, nullptr, |
Mike Reed | eb7dc79 | 2017-06-12 12:48:45 -0400 | [diff] [blame] | 107 | is_opaque, is_constant); |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame] | 108 | } |
| 109 | |
Mike Reed | c91e387 | 2017-07-05 14:12:37 -0400 | [diff] [blame] | 110 | bool is_opaque = shader->isOpaque() && paintColor.a() == 1.0f; |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame] | 111 | bool is_constant = shader->isConstant(); |
Florin Malita | 47e55a5 | 2017-06-06 12:26:54 -0400 | [diff] [blame] | 112 | |
| 113 | // Check whether the shader prefers to run in burst mode. |
| 114 | if (auto* burstCtx = shader->makeBurstPipelineContext( |
| 115 | SkShaderBase::ContextRec(paint, ctm, nullptr, SkShaderBase::ContextRec::kPM4f_DstType, |
| 116 | dstCS), alloc)) { |
| 117 | return SkRasterPipelineBlitter::Create(dst, paint, alloc, |
| 118 | shaderPipeline, burstCtx, |
Mike Reed | eb7dc79 | 2017-06-12 12:48:45 -0400 | [diff] [blame] | 119 | is_opaque, is_constant); |
Florin Malita | 47e55a5 | 2017-06-06 12:26:54 -0400 | [diff] [blame] | 120 | } |
| 121 | |
Mike Reed | 1d8c42e | 2017-08-29 14:58:19 -0400 | [diff] [blame^] | 122 | if (shader->appendStages({&shaderPipeline, alloc, dstCS, paint, nullptr, ctm})) { |
Mike Reed | c91e387 | 2017-07-05 14:12:37 -0400 | [diff] [blame] | 123 | if (paintColor.a() != 1.0f) { |
| 124 | shaderPipeline.append(SkRasterPipeline::scale_1_float, |
| 125 | alloc->make<float>(paintColor.a())); |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame] | 126 | } |
Florin Malita | 47e55a5 | 2017-06-06 12:26:54 -0400 | [diff] [blame] | 127 | return SkRasterPipelineBlitter::Create(dst, paint, alloc, shaderPipeline, nullptr, |
Mike Reed | eb7dc79 | 2017-06-12 12:48:45 -0400 | [diff] [blame] | 128 | is_opaque, is_constant); |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame] | 129 | } |
| 130 | |
Florin Malita | 47e55a5 | 2017-06-06 12:26:54 -0400 | [diff] [blame] | 131 | // The shader has opted out of drawing anything. |
Mike Reed | 6867eee | 2017-06-02 13:25:15 -0400 | [diff] [blame] | 132 | return alloc->make<SkNullBlitter>(); |
Mike Reed | 0264095 | 2017-05-19 15:32:13 -0400 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst, |
| 136 | const SkPaint& paint, |
Mike Reed | 0264095 | 2017-05-19 15:32:13 -0400 | [diff] [blame] | 137 | const SkRasterPipeline& shaderPipeline, |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 138 | bool is_opaque, |
Mike Reed | 0264095 | 2017-05-19 15:32:13 -0400 | [diff] [blame] | 139 | SkArenaAlloc* alloc) { |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 140 | bool is_constant = false; // If this were the case, it'd be better to just set a paint color. |
Florin Malita | 47e55a5 | 2017-06-06 12:26:54 -0400 | [diff] [blame] | 141 | return SkRasterPipelineBlitter::Create(dst, paint, alloc, shaderPipeline, nullptr, |
Mike Reed | eb7dc79 | 2017-06-12 12:48:45 -0400 | [diff] [blame] | 142 | is_opaque, is_constant); |
Mike Reed | 0264095 | 2017-05-19 15:32:13 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 145 | SkBlitter* SkRasterPipelineBlitter::Create(const SkPixmap& dst, |
| 146 | const SkPaint& paint, |
| 147 | SkArenaAlloc* alloc, |
| 148 | const SkRasterPipeline& shaderPipeline, |
Florin Malita | 47e55a5 | 2017-06-06 12:26:54 -0400 | [diff] [blame] | 149 | SkShaderBase::Context* burstCtx, |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 150 | bool is_opaque, |
Mike Reed | eb7dc79 | 2017-06-12 12:48:45 -0400 | [diff] [blame] | 151 | bool is_constant) { |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame] | 152 | auto blitter = alloc->make<SkRasterPipelineBlitter>(dst, |
| 153 | paint.getBlendMode(), |
Florin Malita | 47e55a5 | 2017-06-06 12:26:54 -0400 | [diff] [blame] | 154 | alloc, |
| 155 | burstCtx); |
Mike Reed | 0264095 | 2017-05-19 15:32:13 -0400 | [diff] [blame] | 156 | |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 157 | // Our job in this factory is to fill out the blitter's color pipeline. |
| 158 | // This is the common front of the full blit pipelines, each constructed lazily on first use. |
| 159 | // The full blit pipelines handle reading and writing the dst, blending, coverage, dithering. |
| 160 | auto colorPipeline = &blitter->fColorPipeline; |
| 161 | |
Mike Klein | 3b840e9 | 2017-05-23 15:06:17 -0400 | [diff] [blame] | 162 | // Let's get the shader in first. |
Florin Malita | 47e55a5 | 2017-06-06 12:26:54 -0400 | [diff] [blame] | 163 | if (burstCtx) { |
| 164 | colorPipeline->append(SkRasterPipeline::load_f32, &blitter->fShaderOutput); |
| 165 | } else { |
| 166 | colorPipeline->extend(shaderPipeline); |
| 167 | } |
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 | // If there's a color filter it comes next. |
| 170 | if (auto colorFilter = paint.getColorFilter()) { |
| 171 | colorFilter->appendStages(colorPipeline, dst.colorSpace(), alloc, is_opaque); |
mtklein | a4a4488 | 2016-11-04 13:20:07 -0700 | [diff] [blame] | 172 | is_opaque = is_opaque && (colorFilter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag); |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 175 | // Not all formats make sense to dither (think, F16). We set their dither rate to zero. |
| 176 | // We need to decide if we're going to dither now to keep is_constant accurate. |
Mike Reed | eb7dc79 | 2017-06-12 12:48:45 -0400 | [diff] [blame] | 177 | if (paint.isDither()) { |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 178 | switch (dst.info().colorType()) { |
Mike Klein | 9b10f8f | 2017-06-01 13:11:16 -0400 | [diff] [blame] | 179 | default: blitter->fDitherRate = 0.0f; break; |
Mike Klein | 95d19f3 | 2017-06-15 07:40:24 -0700 | [diff] [blame] | 180 | case kARGB_4444_SkColorType: blitter->fDitherRate = 1/15.0f; break; |
Mike Klein | 9b10f8f | 2017-06-01 13:11:16 -0400 | [diff] [blame] | 181 | case kRGB_565_SkColorType: blitter->fDitherRate = 1/63.0f; break; |
Mike Klein | 95d19f3 | 2017-06-15 07:40:24 -0700 | [diff] [blame] | 182 | case kGray_8_SkColorType: |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 183 | case kRGBA_8888_SkColorType: |
Mike Klein | 9b10f8f | 2017-06-01 13:11:16 -0400 | [diff] [blame] | 184 | case kBGRA_8888_SkColorType: blitter->fDitherRate = 1/255.0f; break; |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 185 | } |
Mike Reed | eb7dc79 | 2017-06-12 12:48:45 -0400 | [diff] [blame] | 186 | // TODO: for constant colors, we could try to measure the effect of dithering, and if |
| 187 | // it has no value (i.e. all variations result in the same 32bit color, then we |
| 188 | // could disable it (for speed, by not adding the stage). |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 189 | } |
Mike Klein | 9b10f8f | 2017-06-01 13:11:16 -0400 | [diff] [blame] | 190 | is_constant = is_constant && (blitter->fDitherRate == 0.0f); |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 191 | |
| 192 | // We're logically done here. The code between here and return blitter is all optimization. |
| 193 | |
| 194 | // 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] | 195 | if (is_constant) { |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 196 | SkPM4f constantColor; |
| 197 | SkJumper_MemoryCtx constantColorPtr = { &constantColor, 0 }; |
| 198 | colorPipeline->append(SkRasterPipeline::store_f32, &constantColorPtr); |
| 199 | colorPipeline->run(0,0,1,1); |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 200 | colorPipeline->reset(); |
Mike Klein | 073073e | 2017-08-03 09:42:53 -0400 | [diff] [blame] | 201 | colorPipeline->append_constant_color(alloc, constantColor); |
mtklein | a4a4488 | 2016-11-04 13:20:07 -0700 | [diff] [blame] | 202 | |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 203 | is_opaque = constantColor.a() == 1.0f; |
Mike Klein | 6686617 | 2016-11-03 12:22:01 -0400 | [diff] [blame] | 204 | } |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 205 | |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 206 | // We can strength-reduce SrcOver into Src when opaque. |
| 207 | if (is_opaque && blitter->fBlend == SkBlendMode::kSrcOver) { |
| 208 | blitter->fBlend = SkBlendMode::kSrc; |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 211 | // When we're drawing a constant color in Src mode, we can sometimes just memset. |
| 212 | // (The previous two optimizations help find more opportunities for this one.) |
| 213 | if (is_constant && blitter->fBlend == SkBlendMode::kSrc) { |
| 214 | // Run our color pipeline all the way through to produce what we'd memset when we can. |
| 215 | // Not all blits can memset, so we need to keep colorPipeline too. |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 216 | SkRasterPipeline_<256> p; |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 217 | p.extend(*colorPipeline); |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 218 | blitter->fDstPtr = SkJumper_MemoryCtx{&blitter->fMemsetColor, 0}; |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 219 | blitter->append_store(&p); |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 220 | p.run(0,0,1,1); |
Mike Klein | 14c8f82 | 2016-11-30 19:39:43 -0500 | [diff] [blame] | 221 | |
Mike Klein | 3b59af5 | 2017-07-20 16:02:12 -0400 | [diff] [blame] | 222 | blitter->fCanMemsetInBlitRect = true; |
Mike Klein | 14c8f82 | 2016-11-30 19:39:43 -0500 | [diff] [blame] | 223 | } |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 224 | |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 225 | blitter->fDstPtr = SkJumper_MemoryCtx{ |
| 226 | blitter->fDst.writable_addr(), |
| 227 | blitter->fDst.rowBytesAsPixels(), |
| 228 | }; |
| 229 | |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 230 | return blitter; |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 233 | void SkRasterPipelineBlitter::append_load_d(SkRasterPipeline* p) const { |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 234 | switch (fDst.info().colorType()) { |
Mike Reed | 279091e | 2017-06-27 16:58:00 -0400 | [diff] [blame] | 235 | case kGray_8_SkColorType: p->append(SkRasterPipeline::load_g8_dst, &fDstPtr); break; |
| 236 | case kAlpha_8_SkColorType: p->append(SkRasterPipeline::load_a8_dst, &fDstPtr); break; |
| 237 | case kRGB_565_SkColorType: p->append(SkRasterPipeline::load_565_dst, &fDstPtr); break; |
| 238 | case kARGB_4444_SkColorType: p->append(SkRasterPipeline::load_4444_dst, &fDstPtr); break; |
Mike Klein | 111f8a9 | 2017-06-27 15:56:32 -0400 | [diff] [blame] | 239 | case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::load_bgra_dst, &fDstPtr); break; |
Mike Reed | 279091e | 2017-06-27 16:58:00 -0400 | [diff] [blame] | 240 | case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::load_8888_dst, &fDstPtr); break; |
| 241 | case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::load_f16_dst, &fDstPtr); break; |
Mike Klein | 111f8a9 | 2017-06-27 15:56:32 -0400 | [diff] [blame] | 242 | default: break; |
Mike Klein | e03339a | 2016-11-28 13:24:27 -0500 | [diff] [blame] | 243 | } |
| 244 | if (fDst.info().gammaCloseToSRGB()) { |
Mike Reed | 279091e | 2017-06-27 16:58:00 -0400 | [diff] [blame] | 245 | p->append_from_srgb_dst(fDst.info().alphaType()); |
Mike Klein | e03339a | 2016-11-28 13:24:27 -0500 | [diff] [blame] | 246 | } |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Mike Klein | 14c8f82 | 2016-11-30 19:39:43 -0500 | [diff] [blame] | 249 | void SkRasterPipelineBlitter::append_store(SkRasterPipeline* p) const { |
Matt Sarett | f388093 | 2017-03-24 10:06:03 -0400 | [diff] [blame] | 250 | if (fDst.info().gammaCloseToSRGB()) { |
Mike Klein | e03339a | 2016-11-28 13:24:27 -0500 | [diff] [blame] | 251 | p->append(SkRasterPipeline::to_srgb); |
| 252 | } |
Mike Klein | 9b10f8f | 2017-06-01 13:11:16 -0400 | [diff] [blame] | 253 | if (fDitherRate > 0.0f) { |
Mike Klein | db711c9 | 2017-05-03 17:57:48 -0400 | [diff] [blame] | 254 | // We dither after any sRGB transfer function to make sure our 1/255.0f is sensible |
| 255 | // over the whole range. If we did it before, 1/255.0f is too big a rate near zero. |
Mike Klein | 9b10f8f | 2017-06-01 13:11:16 -0400 | [diff] [blame] | 256 | p->append(SkRasterPipeline::dither, &fDitherRate); |
Mike Klein | db711c9 | 2017-05-03 17:57:48 -0400 | [diff] [blame] | 257 | } |
| 258 | |
Mike Klein | 159db0a | 2017-07-25 12:02:13 -0400 | [diff] [blame] | 259 | if (fDst.info().colorType() != kRGBA_F16_SkColorType) { |
| 260 | p->clamp_if_unclamped(kPremul_SkAlphaType); |
| 261 | } |
| 262 | |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 263 | switch (fDst.info().colorType()) { |
Mike Klein | 95d19f3 | 2017-06-15 07:40:24 -0700 | [diff] [blame] | 264 | case kGray_8_SkColorType: p->append(SkRasterPipeline::luminance_to_alpha); // fallthru |
Mike Klein | e71b167 | 2017-01-13 07:59:23 -0500 | [diff] [blame] | 265 | case kAlpha_8_SkColorType: p->append(SkRasterPipeline::store_a8, &fDstPtr); break; |
Mike Klein | e03339a | 2016-11-28 13:24:27 -0500 | [diff] [blame] | 266 | case kRGB_565_SkColorType: p->append(SkRasterPipeline::store_565, &fDstPtr); break; |
Mike Klein | 95d19f3 | 2017-06-15 07:40:24 -0700 | [diff] [blame] | 267 | case kARGB_4444_SkColorType: p->append(SkRasterPipeline::store_4444, &fDstPtr); break; |
Mike Klein | 111f8a9 | 2017-06-27 15:56:32 -0400 | [diff] [blame] | 268 | case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::store_bgra, &fDstPtr); break; |
Mike Klein | e03339a | 2016-11-28 13:24:27 -0500 | [diff] [blame] | 269 | case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::store_8888, &fDstPtr); break; |
| 270 | case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::store_f16, &fDstPtr); break; |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 271 | default: break; |
| 272 | } |
| 273 | } |
| 274 | |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 275 | void SkRasterPipelineBlitter::append_blend(SkRasterPipeline* p) const { |
Mike Klein | fb126fa | 2017-08-24 13:06:23 -0400 | [diff] [blame] | 276 | if (fDst.info().alphaType() == kUnpremul_SkAlphaType) { // TODO: move to append_load_d |
Mike Reed | 883c9bc | 2017-07-19 10:57:53 -0400 | [diff] [blame] | 277 | p->append(SkRasterPipeline::premul_dst); |
| 278 | } |
Mike Klein | fb126fa | 2017-08-24 13:06:23 -0400 | [diff] [blame] | 279 | SkBlendMode_AppendStages(fBlend, p); |
| 280 | if (fDst.info().alphaType() == kUnpremul_SkAlphaType) { // TODO: move to append_store |
Mike Reed | 883c9bc | 2017-07-19 10:57:53 -0400 | [diff] [blame] | 281 | p->append(SkRasterPipeline::unpremul); |
| 282 | } |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 283 | } |
Mike Klein | e9f74b8 | 2016-10-25 13:31:21 -0400 | [diff] [blame] | 284 | |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 285 | void SkRasterPipelineBlitter::burst_shade(int x, int y, int w) { |
| 286 | SkASSERT(fBurstCtx); |
| 287 | if (w > SkToInt(fShaderBuffer.size())) { |
| 288 | fShaderBuffer.resize(w); |
Florin Malita | 47e55a5 | 2017-06-06 12:26:54 -0400 | [diff] [blame] | 289 | } |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 290 | fBurstCtx->shadeSpan4f(x,y, fShaderBuffer.data(), w); |
| 291 | // We'll be reading from fShaderOutput.pixels + x, so back up by x. |
| 292 | fShaderOutput = SkJumper_MemoryCtx{ fShaderBuffer.data() - x, 0 }; |
Florin Malita | 47e55a5 | 2017-06-06 12:26:54 -0400 | [diff] [blame] | 293 | } |
| 294 | |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 295 | void SkRasterPipelineBlitter::blitH(int x, int y, int w) { |
Mike Klein | 3b59af5 | 2017-07-20 16:02:12 -0400 | [diff] [blame] | 296 | this->blitRect(x,y,w,1); |
| 297 | } |
| 298 | |
| 299 | void SkRasterPipelineBlitter::blitRect(int x, int y, int w, int h) { |
| 300 | if (fCanMemsetInBlitRect) { |
| 301 | for (int ylimit = y+h; y < ylimit; y++) { |
| 302 | switch (fDst.shiftPerPixel()) { |
| 303 | case 0: memset (fDst.writable_addr8 (x,y), fMemsetColor, w); break; |
| 304 | case 1: sk_memset16(fDst.writable_addr16(x,y), fMemsetColor, w); break; |
| 305 | case 2: sk_memset32(fDst.writable_addr32(x,y), fMemsetColor, w); break; |
| 306 | case 3: sk_memset64(fDst.writable_addr64(x,y), fMemsetColor, w); break; |
| 307 | default: break; |
| 308 | } |
Mike Klein | 8729e5b | 2017-02-16 06:51:48 -0500 | [diff] [blame] | 309 | } |
Mike Klein | 3b59af5 | 2017-07-20 16:02:12 -0400 | [diff] [blame] | 310 | return; |
Mike Klein | 8729e5b | 2017-02-16 06:51:48 -0500 | [diff] [blame] | 311 | } |
| 312 | |
Mike Klein | 3b59af5 | 2017-07-20 16:02:12 -0400 | [diff] [blame] | 313 | if (!fBlitRect) { |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 314 | SkRasterPipeline p(fAlloc); |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 315 | p.extend(fColorPipeline); |
Mike Klein | 5062626 | 2017-05-25 13:06:57 -0400 | [diff] [blame] | 316 | if (fBlend == SkBlendMode::kSrcOver |
| 317 | && fDst.info().colorType() == kRGBA_8888_SkColorType |
| 318 | && !fDst.colorSpace() |
Mike Reed | 883c9bc | 2017-07-19 10:57:53 -0400 | [diff] [blame] | 319 | && fDst.info().alphaType() != kUnpremul_SkAlphaType |
Mike Klein | 9b10f8f | 2017-06-01 13:11:16 -0400 | [diff] [blame] | 320 | && fDitherRate == 0.0f) { |
Mike Klein | 159db0a | 2017-07-25 12:02:13 -0400 | [diff] [blame] | 321 | p.clamp_if_unclamped(kPremul_SkAlphaType); |
Mike Klein | 5062626 | 2017-05-25 13:06:57 -0400 | [diff] [blame] | 322 | p.append(SkRasterPipeline::srcover_rgba_8888, &fDstPtr); |
| 323 | } else { |
| 324 | if (fBlend != SkBlendMode::kSrc) { |
| 325 | this->append_load_d(&p); |
| 326 | this->append_blend(&p); |
Mike Klein | 5062626 | 2017-05-25 13:06:57 -0400 | [diff] [blame] | 327 | } |
| 328 | this->append_store(&p); |
Mike Klein | 6686617 | 2016-11-03 12:22:01 -0400 | [diff] [blame] | 329 | } |
Mike Klein | 3b59af5 | 2017-07-20 16:02:12 -0400 | [diff] [blame] | 330 | fBlitRect = p.compile(); |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 331 | } |
Mike Klein | 3b59af5 | 2017-07-20 16:02:12 -0400 | [diff] [blame] | 332 | |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 333 | if (fBurstCtx) { |
Mike Klein | 3b59af5 | 2017-07-20 16:02:12 -0400 | [diff] [blame] | 334 | // We can only burst shade one row at a time. |
| 335 | for (int ylimit = y+h; y < ylimit; y++) { |
| 336 | this->burst_shade(x,y,w); |
| 337 | fBlitRect(x,y, w,1); |
| 338 | } |
| 339 | } else { |
| 340 | // If not bursting we can blit the entire rect at once. |
| 341 | fBlitRect(x,y,w,h); |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 342 | } |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | 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] | 346 | if (!fBlitAntiH) { |
Florin Malita | c3a863f | 2017-08-23 20:36:02 +0000 | [diff] [blame] | 347 | SkRasterPipeline p(fAlloc); |
| 348 | p.extend(fColorPipeline); |
Mike Klein | fb126fa | 2017-08-24 13:06:23 -0400 | [diff] [blame] | 349 | if (SkBlendMode_ShouldPreScaleCoverage(fBlend, /*rgb_coverage=*/false)) { |
Florin Malita | c3a863f | 2017-08-23 20:36:02 +0000 | [diff] [blame] | 350 | p.append(SkRasterPipeline::scale_1_float, &fCurrentCoverage); |
| 351 | this->append_load_d(&p); |
| 352 | this->append_blend(&p); |
| 353 | } else { |
| 354 | this->append_load_d(&p); |
| 355 | this->append_blend(&p); |
| 356 | p.append(SkRasterPipeline::lerp_1_float, &fCurrentCoverage); |
| 357 | } |
Mike Klein | fb126fa | 2017-08-24 13:06:23 -0400 | [diff] [blame] | 358 | |
Florin Malita | c3a863f | 2017-08-23 20:36:02 +0000 | [diff] [blame] | 359 | this->append_store(&p); |
| 360 | fBlitAntiH = p.compile(); |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 361 | } |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 362 | |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 363 | for (int16_t run = *runs; run > 0; run = *runs) { |
Mike Klein | aeb7959 | 2016-11-07 09:29:07 -0500 | [diff] [blame] | 364 | switch (*aa) { |
| 365 | case 0x00: break; |
| 366 | case 0xff: this->blitH(x,y,run); break; |
| 367 | default: |
Mike Klein | babd93e | 2016-11-30 16:05:10 -0500 | [diff] [blame] | 368 | fCurrentCoverage = *aa * (1/255.0f); |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 369 | if (fBurstCtx) { |
| 370 | this->burst_shade(x,y,run); |
| 371 | } |
| 372 | fBlitAntiH(x,y,run,1); |
Mike Klein | aeb7959 | 2016-11-07 09:29:07 -0500 | [diff] [blame] | 373 | } |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 374 | x += run; |
| 375 | runs += run; |
| 376 | aa += run; |
| 377 | } |
| 378 | } |
| 379 | |
Mike Klein | 34d10d7 | 2017-06-05 07:10:01 -0400 | [diff] [blame] | 380 | void SkRasterPipelineBlitter::blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) { |
| 381 | SkIRect clip = {x,y, x+2,y+1}; |
| 382 | uint8_t coverage[] = { (uint8_t)a0, (uint8_t)a1 }; |
| 383 | |
| 384 | SkMask mask; |
| 385 | mask.fImage = coverage; |
| 386 | mask.fBounds = clip; |
| 387 | mask.fRowBytes = 2; |
| 388 | mask.fFormat = SkMask::kA8_Format; |
| 389 | |
| 390 | this->blitMask(mask, clip); |
| 391 | } |
| 392 | |
Mike Reed | e6befa5 | 2017-08-28 11:30:48 -0400 | [diff] [blame] | 393 | void SkRasterPipelineBlitter::blitV(int x, int y, int height, SkAlpha alpha) { |
| 394 | SkIRect clip = {x,y, x+1,y+height}; |
| 395 | |
| 396 | SkMask mask; |
| 397 | mask.fImage = α |
| 398 | mask.fBounds = clip; |
| 399 | mask.fRowBytes = 0; // so we reuse the 1 "row" for all of height |
| 400 | mask.fFormat = SkMask::kA8_Format; |
| 401 | |
| 402 | this->blitMask(mask, clip); |
| 403 | } |
| 404 | |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 405 | void SkRasterPipelineBlitter::blitMask(const SkMask& mask, const SkIRect& clip) { |
| 406 | if (mask.fFormat == SkMask::kBW_Format) { |
| 407 | // TODO: native BW masks? |
| 408 | return INHERITED::blitMask(mask, clip); |
| 409 | } |
| 410 | |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 411 | // Lazily build whichever pipeline we need, specialized for each mask format. |
Mike Klein | 0a76b41 | 2017-05-22 12:01:59 -0400 | [diff] [blame] | 412 | if (mask.fFormat == SkMask::kA8_Format && !fBlitMaskA8) { |
Florin Malita | c3a863f | 2017-08-23 20:36:02 +0000 | [diff] [blame] | 413 | SkRasterPipeline p(fAlloc); |
| 414 | p.extend(fColorPipeline); |
Mike Klein | fb126fa | 2017-08-24 13:06:23 -0400 | [diff] [blame] | 415 | if (SkBlendMode_ShouldPreScaleCoverage(fBlend, /*rgb_coverage=*/false)) { |
Florin Malita | c3a863f | 2017-08-23 20:36:02 +0000 | [diff] [blame] | 416 | p.append(SkRasterPipeline::scale_u8, &fMaskPtr); |
| 417 | this->append_load_d(&p); |
| 418 | this->append_blend(&p); |
| 419 | } else { |
| 420 | this->append_load_d(&p); |
| 421 | this->append_blend(&p); |
| 422 | p.append(SkRasterPipeline::lerp_u8, &fMaskPtr); |
| 423 | } |
Florin Malita | c3a863f | 2017-08-23 20:36:02 +0000 | [diff] [blame] | 424 | this->append_store(&p); |
| 425 | fBlitMaskA8 = p.compile(); |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 426 | } |
Mike Klein | 0a76b41 | 2017-05-22 12:01:59 -0400 | [diff] [blame] | 427 | if (mask.fFormat == SkMask::kLCD16_Format && !fBlitMaskLCD16) { |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 428 | SkRasterPipeline p(fAlloc); |
Mike Klein | b35cb31 | 2017-05-19 12:01:01 -0400 | [diff] [blame] | 429 | p.extend(fColorPipeline); |
Mike Klein | fb126fa | 2017-08-24 13:06:23 -0400 | [diff] [blame] | 430 | if (SkBlendMode_ShouldPreScaleCoverage(fBlend, /*rgb_coverage=*/true)) { |
| 431 | // Somewhat unusually, scale_565 needs dst loaded first. |
| 432 | this->append_load_d(&p); |
| 433 | p.append(SkRasterPipeline::scale_565, &fMaskPtr); |
| 434 | this->append_blend(&p); |
| 435 | } else { |
| 436 | this->append_load_d(&p); |
| 437 | this->append_blend(&p); |
| 438 | p.append(SkRasterPipeline::lerp_565, &fMaskPtr); |
| 439 | } |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 440 | this->append_store(&p); |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 441 | fBlitMaskLCD16 = p.compile(); |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 442 | } |
| 443 | |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 444 | std::function<void(size_t,size_t,size_t,size_t)>* blitter = nullptr; |
| 445 | // Update fMaskPtr to point "into" this current mask, but lined up with fDstPtr at (0,0). |
| 446 | switch (mask.fFormat) { |
| 447 | case SkMask::kA8_Format: |
| 448 | fMaskPtr.stride = mask.fRowBytes; |
| 449 | fMaskPtr.pixels = (uint8_t*)mask.fImage - mask.fBounds.left() |
| 450 | - mask.fBounds.top() * fMaskPtr.stride; |
| 451 | blitter = &fBlitMaskA8; |
| 452 | break; |
| 453 | case SkMask::kLCD16_Format: |
| 454 | fMaskPtr.stride = mask.fRowBytes / 2; |
| 455 | fMaskPtr.pixels = (uint16_t*)mask.fImage - mask.fBounds.left() |
| 456 | - mask.fBounds.top() * fMaskPtr.stride; |
| 457 | blitter = &fBlitMaskLCD16; |
| 458 | break; |
| 459 | default: |
| 460 | return; |
| 461 | } |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 462 | |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 463 | SkASSERT(blitter); |
| 464 | if (fBurstCtx) { |
| 465 | // We can only burst shade one row at a time. |
| 466 | int x = clip.left(); |
| 467 | for (int y = clip.top(); y < clip.bottom(); y++) { |
| 468 | this->burst_shade(x,y,clip.width()); |
| 469 | (*blitter)(x,y, clip.width(),1); |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 470 | } |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 471 | } else { |
| 472 | // If not bursting we can blit the entire mask at once. |
| 473 | (*blitter)(clip.left(),clip.top(), clip.width(),clip.height()); |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 474 | } |
| 475 | } |