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