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 | |
| 8 | #include "SkBlitter.h" |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 9 | #include "SkBlendModePriv.h" |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 10 | #include "SkColor.h" |
| 11 | #include "SkColorFilter.h" |
Mike Klein | baaf8ad | 2016-09-29 09:04:15 -0400 | [diff] [blame] | 12 | #include "SkOpts.h" |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 13 | #include "SkPM4f.h" |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 14 | #include "SkRasterPipeline.h" |
| 15 | #include "SkShader.h" |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 16 | #include "SkXfermode.h" |
| 17 | |
| 18 | |
| 19 | class SkRasterPipelineBlitter : public SkBlitter { |
| 20 | public: |
| 21 | static SkBlitter* Create(const SkPixmap&, const SkPaint&, SkTBlitterAllocator*); |
| 22 | |
| 23 | SkRasterPipelineBlitter(SkPixmap dst, |
| 24 | SkRasterPipeline shader, |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 25 | SkBlendMode blend, |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 26 | SkPM4f paintColor) |
| 27 | : fDst(dst) |
| 28 | , fShader(shader) |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 29 | , fBlend(blend) |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 30 | , fPaintColor(paintColor) |
| 31 | {} |
| 32 | |
| 33 | void blitH (int x, int y, int w) override; |
| 34 | void blitAntiH(int x, int y, const SkAlpha[], const int16_t[]) override; |
| 35 | void blitMask (const SkMask&, const SkIRect& clip) override; |
| 36 | |
| 37 | // TODO: The default implementations of the other blits look fine, |
| 38 | // but some of them like blitV could probably benefit from custom |
| 39 | // blits using something like a SkRasterPipeline::runFew() method. |
| 40 | |
| 41 | private: |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 42 | void append_load_d(SkRasterPipeline*) const; |
| 43 | void append_store (SkRasterPipeline*) const; |
| 44 | void append_blend (SkRasterPipeline*) const; |
Mike Klein | 130863e | 2016-10-27 11:29:36 -0400 | [diff] [blame] | 45 | void maybe_clamp (SkRasterPipeline*) const; |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 46 | |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 47 | SkPixmap fDst; |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 48 | SkRasterPipeline fShader; |
| 49 | SkBlendMode fBlend; |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 50 | SkPM4f fPaintColor; |
| 51 | |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 52 | // These functions are compiled lazily when first used. |
| 53 | std::function<void(size_t, size_t)> fBlitH = nullptr, |
| 54 | fBlitAntiH = nullptr, |
| 55 | fBlitMaskA8 = nullptr, |
| 56 | fBlitMaskLCD16 = nullptr; |
| 57 | |
| 58 | // These values are pointed to by the compiled blit functions |
| 59 | // above, which allows us to adjust them from call to call. |
| 60 | void* fDstPtr = nullptr; |
| 61 | const void* fMaskPtr = nullptr; |
| 62 | float fConstantCoverage = 0.0f; |
| 63 | |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 64 | typedef SkBlitter INHERITED; |
| 65 | }; |
| 66 | |
| 67 | SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst, |
| 68 | const SkPaint& paint, |
| 69 | SkTBlitterAllocator* alloc) { |
| 70 | return SkRasterPipelineBlitter::Create(dst, paint, alloc); |
| 71 | } |
| 72 | |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 73 | static bool supported(const SkImageInfo& info) { |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 74 | switch (info.colorType()) { |
mtklein | 4e90e3e | 2016-07-25 14:35:31 -0700 | [diff] [blame] | 75 | case kN32_SkColorType: return info.gammaCloseToSRGB(); |
| 76 | case kRGBA_F16_SkColorType: return true; |
| 77 | case kRGB_565_SkColorType: return true; |
| 78 | default: return false; |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 81 | |
| 82 | template <typename Effect> |
| 83 | static bool append_effect_stages(const Effect* effect, SkRasterPipeline* pipeline) { |
| 84 | return !effect || effect->appendStages(pipeline); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | SkBlitter* SkRasterPipelineBlitter::Create(const SkPixmap& dst, |
| 89 | const SkPaint& paint, |
| 90 | SkTBlitterAllocator* alloc) { |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 91 | if (!supported(dst.info())) { |
| 92 | return nullptr; |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 93 | } |
| 94 | if (paint.getShader()) { |
| 95 | return nullptr; // TODO: need to work out how shaders and their contexts work |
| 96 | } |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 97 | SkBlendMode blend = paint.getBlendMode(); |
| 98 | if (!SkBlendMode_AppendStages(blend)) { |
| 99 | return nullptr; // TODO |
| 100 | } |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 101 | |
Mike Klein | eea7c16 | 2016-11-03 10:20:35 -0400 | [diff] [blame] | 102 | uint32_t paintColor = paint.getColor(); |
| 103 | bool shaderIsOpaque = (paintColor >> 24) == 0xff; |
| 104 | |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 105 | SkRasterPipeline shader, colorFilter; |
Mike Klein | 6686617 | 2016-11-03 12:22:01 -0400 | [diff] [blame^] | 106 | if (auto s = paint.getShader()) { |
| 107 | shaderIsOpaque = s->isOpaque(); |
Mike Klein | eea7c16 | 2016-11-03 10:20:35 -0400 | [diff] [blame] | 108 | } |
Mike Klein | 6686617 | 2016-11-03 12:22:01 -0400 | [diff] [blame^] | 109 | if (auto cf = paint.getColorFilter()) { |
| 110 | if (!cf->appendStages(&colorFilter, shaderIsOpaque)) { |
| 111 | return nullptr; |
| 112 | } |
| 113 | shaderIsOpaque = shaderIsOpaque && (cf->getFlags() & SkColorFilter::kAlphaUnchanged_Flag); |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Mike Klein | 6686617 | 2016-11-03 12:22:01 -0400 | [diff] [blame^] | 116 | if (shaderIsOpaque && blend == SkBlendMode::kSrcOver) { |
| 117 | blend = SkBlendMode::kSrc; |
| 118 | } |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 119 | |
| 120 | SkColor4f color; |
Brian Osman | 7039f74 | 2016-11-01 15:56:16 -0400 | [diff] [blame] | 121 | if (dst.info().colorSpace()) { |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 122 | color = SkColor4f::FromColor(paintColor); |
| 123 | } else { |
| 124 | swizzle_rb(SkNx_cast<float>(Sk4b::Load(&paintColor)) * (1/255.0f)).store(&color); |
| 125 | } |
| 126 | |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 127 | auto blitter = alloc->createT<SkRasterPipelineBlitter>(dst, shader, blend, color.premul()); |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 128 | |
| 129 | if (!paint.getShader()) { |
Mike Klein | fa9f241 | 2016-09-29 13:40:01 -0400 | [diff] [blame] | 130 | blitter->fShader.append(SkRasterPipeline::constant_color, &blitter->fPaintColor); |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 131 | } |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 132 | blitter->fShader.extend(colorFilter); |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 133 | |
| 134 | return blitter; |
| 135 | } |
| 136 | |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 137 | void SkRasterPipelineBlitter::append_load_d(SkRasterPipeline* p) const { |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 138 | SkASSERT(supported(fDst.info())); |
| 139 | |
| 140 | switch (fDst.info().colorType()) { |
| 141 | case kN32_SkColorType: |
| 142 | if (fDst.info().gammaCloseToSRGB()) { |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 143 | p->append(SkRasterPipeline::load_d_srgb, &fDstPtr); |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 144 | } |
| 145 | break; |
mtklein | 4e90e3e | 2016-07-25 14:35:31 -0700 | [diff] [blame] | 146 | case kRGBA_F16_SkColorType: |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 147 | p->append(SkRasterPipeline::load_d_f16, &fDstPtr); |
mtklein | 4e90e3e | 2016-07-25 14:35:31 -0700 | [diff] [blame] | 148 | break; |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 149 | case kRGB_565_SkColorType: |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 150 | p->append(SkRasterPipeline::load_d_565, &fDstPtr); |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 151 | break; |
| 152 | default: break; |
| 153 | } |
| 154 | } |
| 155 | |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 156 | void SkRasterPipelineBlitter::append_store(SkRasterPipeline* p) const { |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 157 | SkASSERT(supported(fDst.info())); |
| 158 | |
| 159 | switch (fDst.info().colorType()) { |
| 160 | case kN32_SkColorType: |
| 161 | if (fDst.info().gammaCloseToSRGB()) { |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 162 | p->append(SkRasterPipeline::store_srgb, &fDstPtr); |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 163 | } |
| 164 | break; |
mtklein | 4e90e3e | 2016-07-25 14:35:31 -0700 | [diff] [blame] | 165 | case kRGBA_F16_SkColorType: |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 166 | p->append(SkRasterPipeline::store_f16, &fDstPtr); |
mtklein | 4e90e3e | 2016-07-25 14:35:31 -0700 | [diff] [blame] | 167 | break; |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 168 | case kRGB_565_SkColorType: |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 169 | p->append(SkRasterPipeline::store_565, &fDstPtr); |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 170 | break; |
| 171 | default: break; |
| 172 | } |
| 173 | } |
| 174 | |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 175 | void SkRasterPipelineBlitter::append_blend(SkRasterPipeline* p) const { |
| 176 | SkAssertResult(SkBlendMode_AppendStages(fBlend, p)); |
| 177 | } |
Mike Klein | e9f74b8 | 2016-10-25 13:31:21 -0400 | [diff] [blame] | 178 | |
Mike Klein | 130863e | 2016-10-27 11:29:36 -0400 | [diff] [blame] | 179 | void SkRasterPipelineBlitter::maybe_clamp(SkRasterPipeline* p) const { |
Mike Klein | eea7c16 | 2016-11-03 10:20:35 -0400 | [diff] [blame] | 180 | if (SkBlendMode_CanOverflow(fBlend)) { p->append(SkRasterPipeline::clamp_a); } |
Mike Klein | 130863e | 2016-10-27 11:29:36 -0400 | [diff] [blame] | 181 | } |
| 182 | |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 183 | void SkRasterPipelineBlitter::blitH(int x, int y, int w) { |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 184 | if (!fBlitH) { |
| 185 | SkRasterPipeline p; |
| 186 | p.extend(fShader); |
Mike Klein | 6686617 | 2016-11-03 12:22:01 -0400 | [diff] [blame^] | 187 | if (fBlend != SkBlendMode::kSrc) { |
| 188 | this->append_load_d(&p); |
| 189 | this->append_blend(&p); |
| 190 | this->maybe_clamp(&p); |
| 191 | } |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 192 | this->append_store(&p); |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 193 | fBlitH = p.compile(); |
| 194 | } |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 195 | |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 196 | fDstPtr = fDst.writable_addr(0,y); |
| 197 | fBlitH(x,w); |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void SkRasterPipelineBlitter::blitAntiH(int x, int y, const SkAlpha aa[], const int16_t runs[]) { |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 201 | if (!fBlitAntiH) { |
| 202 | SkRasterPipeline p; |
| 203 | p.extend(fShader); |
Mike Klein | 6686617 | 2016-11-03 12:22:01 -0400 | [diff] [blame^] | 204 | if (fBlend == SkBlendMode::kSrcOver) { |
| 205 | p.append(SkRasterPipeline::scale_constant_float, &fConstantCoverage); |
| 206 | this->append_load_d(&p); |
| 207 | this->append_blend(&p); |
| 208 | } else { |
| 209 | this->append_load_d(&p); |
| 210 | this->append_blend(&p); |
| 211 | p.append(SkRasterPipeline::lerp_constant_float, &fConstantCoverage); |
| 212 | } |
Mike Klein | 130863e | 2016-10-27 11:29:36 -0400 | [diff] [blame] | 213 | this->maybe_clamp(&p); |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 214 | this->append_store(&p); |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 215 | fBlitAntiH = p.compile(); |
| 216 | } |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 217 | |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 218 | fDstPtr = fDst.writable_addr(0,y); |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 219 | for (int16_t run = *runs; run > 0; run = *runs) { |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 220 | fConstantCoverage = *aa * (1/255.0f); |
| 221 | fBlitAntiH(x, run); |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 222 | |
| 223 | x += run; |
| 224 | runs += run; |
| 225 | aa += run; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | void SkRasterPipelineBlitter::blitMask(const SkMask& mask, const SkIRect& clip) { |
| 230 | if (mask.fFormat == SkMask::kBW_Format) { |
| 231 | // TODO: native BW masks? |
| 232 | return INHERITED::blitMask(mask, clip); |
| 233 | } |
| 234 | |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 235 | if (mask.fFormat == SkMask::kA8_Format && !fBlitMaskA8) { |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 236 | SkRasterPipeline p; |
| 237 | p.extend(fShader); |
Mike Klein | 6686617 | 2016-11-03 12:22:01 -0400 | [diff] [blame^] | 238 | if (fBlend == SkBlendMode::kSrcOver) { |
| 239 | p.append(SkRasterPipeline::scale_u8, &fMaskPtr); |
| 240 | this->append_load_d(&p); |
| 241 | this->append_blend(&p); |
| 242 | } else { |
| 243 | this->append_load_d(&p); |
| 244 | this->append_blend(&p); |
| 245 | p.append(SkRasterPipeline::lerp_u8, &fMaskPtr); |
| 246 | } |
Mike Klein | 130863e | 2016-10-27 11:29:36 -0400 | [diff] [blame] | 247 | this->maybe_clamp(&p); |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 248 | this->append_store(&p); |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 249 | fBlitMaskA8 = p.compile(); |
| 250 | } |
| 251 | |
| 252 | if (mask.fFormat == SkMask::kLCD16_Format && !fBlitMaskLCD16) { |
| 253 | SkRasterPipeline p; |
| 254 | p.extend(fShader); |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 255 | this->append_load_d(&p); |
| 256 | this->append_blend(&p); |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 257 | p.append(SkRasterPipeline::lerp_565, &fMaskPtr); |
Mike Klein | 130863e | 2016-10-27 11:29:36 -0400 | [diff] [blame] | 258 | this->maybe_clamp(&p); |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 259 | this->append_store(&p); |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 260 | fBlitMaskLCD16 = p.compile(); |
| 261 | } |
| 262 | |
| 263 | int x = clip.left(); |
| 264 | for (int y = clip.top(); y < clip.bottom(); y++) { |
| 265 | fDstPtr = fDst.writable_addr(0,y); |
| 266 | |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 267 | switch (mask.fFormat) { |
| 268 | case SkMask::kA8_Format: |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 269 | fMaskPtr = mask.getAddr8(x,y)-x; |
| 270 | fBlitMaskA8(x, clip.width()); |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 271 | break; |
| 272 | case SkMask::kLCD16_Format: |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 273 | fMaskPtr = mask.getAddrLCD16(x,y)-x; |
| 274 | fBlitMaskLCD16(x, clip.width()); |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 275 | break; |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 276 | default: |
| 277 | // TODO |
| 278 | break; |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 279 | } |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 280 | } |
| 281 | } |