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)); |
| 101 | SkBlendMode* blend = &blitter->fBlend; |
| 102 | SkPM4f* paintColor = &blitter->fPaintColor; |
| 103 | SkRasterPipeline* pipeline = &blitter->fShader; |
| 104 | |
| 105 | SkShader* shader = paint.getShader(); |
| 106 | SkColorFilter* colorFilter = paint.getColorFilter(); |
| 107 | |
| 108 | // TODO: all temporary |
| 109 | if (!supported(dst.info()) || shader || !SkBlendMode_AppendStages(*blend)) { |
| 110 | alloc->freeLast(); |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 111 | return nullptr; |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 112 | } |
mtklein | a4a4488 | 2016-11-04 13:20:07 -0700 | [diff] [blame^] | 113 | |
| 114 | bool is_opaque, is_constant; |
| 115 | if (shader) { |
| 116 | is_opaque = shader->isOpaque(); |
| 117 | is_constant = false; // TODO: shader->isConstant() |
| 118 | // TODO: append shader stages, of course! |
| 119 | } else { |
| 120 | is_opaque = paintColor->a() == 1.0f; |
| 121 | is_constant = true; |
| 122 | pipeline->append(SkRasterPipeline::constant_color, paintColor); |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 123 | } |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 124 | |
mtklein | a4a4488 | 2016-11-04 13:20:07 -0700 | [diff] [blame^] | 125 | if (colorFilter) { |
| 126 | if (!colorFilter->appendStages(pipeline, is_opaque)) { |
| 127 | alloc->freeLast(); |
Mike Klein | 6686617 | 2016-11-03 12:22:01 -0400 | [diff] [blame] | 128 | return nullptr; |
| 129 | } |
mtklein | a4a4488 | 2016-11-04 13:20:07 -0700 | [diff] [blame^] | 130 | is_opaque = is_opaque && (colorFilter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag); |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 131 | } |
| 132 | |
mtklein | a4a4488 | 2016-11-04 13:20:07 -0700 | [diff] [blame^] | 133 | if (is_constant) { |
| 134 | pipeline->append(SkRasterPipeline::store_f32, &paintColor); |
| 135 | pipeline->compile()(0,1); |
| 136 | |
| 137 | *pipeline = SkRasterPipeline(); |
| 138 | pipeline->append(SkRasterPipeline::constant_color, paintColor); |
| 139 | |
| 140 | is_opaque = paintColor->a() == 1.0f; |
Mike Klein | 6686617 | 2016-11-03 12:22:01 -0400 | [diff] [blame] | 141 | } |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 142 | |
mtklein | a4a4488 | 2016-11-04 13:20:07 -0700 | [diff] [blame^] | 143 | if (is_opaque && *blend == SkBlendMode::kSrcOver) { |
| 144 | *blend = SkBlendMode::kSrc; |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 145 | } |
| 146 | |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 147 | return blitter; |
| 148 | } |
| 149 | |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 150 | void SkRasterPipelineBlitter::append_load_d(SkRasterPipeline* p) const { |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 151 | SkASSERT(supported(fDst.info())); |
| 152 | |
| 153 | switch (fDst.info().colorType()) { |
| 154 | case kN32_SkColorType: |
| 155 | if (fDst.info().gammaCloseToSRGB()) { |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 156 | p->append(SkRasterPipeline::load_d_srgb, &fDstPtr); |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 157 | } |
| 158 | break; |
mtklein | 4e90e3e | 2016-07-25 14:35:31 -0700 | [diff] [blame] | 159 | case kRGBA_F16_SkColorType: |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 160 | p->append(SkRasterPipeline::load_d_f16, &fDstPtr); |
mtklein | 4e90e3e | 2016-07-25 14:35:31 -0700 | [diff] [blame] | 161 | break; |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 162 | case kRGB_565_SkColorType: |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 163 | p->append(SkRasterPipeline::load_d_565, &fDstPtr); |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 164 | break; |
| 165 | default: break; |
| 166 | } |
| 167 | } |
| 168 | |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 169 | void SkRasterPipelineBlitter::append_store(SkRasterPipeline* p) const { |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 170 | SkASSERT(supported(fDst.info())); |
| 171 | |
| 172 | switch (fDst.info().colorType()) { |
| 173 | case kN32_SkColorType: |
| 174 | if (fDst.info().gammaCloseToSRGB()) { |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 175 | p->append(SkRasterPipeline::store_srgb, &fDstPtr); |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 176 | } |
| 177 | break; |
mtklein | 4e90e3e | 2016-07-25 14:35:31 -0700 | [diff] [blame] | 178 | case kRGBA_F16_SkColorType: |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 179 | p->append(SkRasterPipeline::store_f16, &fDstPtr); |
mtklein | 4e90e3e | 2016-07-25 14:35:31 -0700 | [diff] [blame] | 180 | break; |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 181 | case kRGB_565_SkColorType: |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 182 | p->append(SkRasterPipeline::store_565, &fDstPtr); |
mtklein | 8e4373f | 2016-07-22 14:20:27 -0700 | [diff] [blame] | 183 | break; |
| 184 | default: break; |
| 185 | } |
| 186 | } |
| 187 | |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 188 | void SkRasterPipelineBlitter::append_blend(SkRasterPipeline* p) const { |
| 189 | SkAssertResult(SkBlendMode_AppendStages(fBlend, p)); |
| 190 | } |
Mike Klein | e9f74b8 | 2016-10-25 13:31:21 -0400 | [diff] [blame] | 191 | |
Mike Klein | 130863e | 2016-10-27 11:29:36 -0400 | [diff] [blame] | 192 | void SkRasterPipelineBlitter::maybe_clamp(SkRasterPipeline* p) const { |
Mike Klein | eea7c16 | 2016-11-03 10:20:35 -0400 | [diff] [blame] | 193 | if (SkBlendMode_CanOverflow(fBlend)) { p->append(SkRasterPipeline::clamp_a); } |
Mike Klein | 130863e | 2016-10-27 11:29:36 -0400 | [diff] [blame] | 194 | } |
| 195 | |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 196 | void SkRasterPipelineBlitter::blitH(int x, int y, int w) { |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 197 | if (!fBlitH) { |
| 198 | SkRasterPipeline p; |
| 199 | p.extend(fShader); |
Mike Klein | 6686617 | 2016-11-03 12:22:01 -0400 | [diff] [blame] | 200 | if (fBlend != SkBlendMode::kSrc) { |
| 201 | this->append_load_d(&p); |
| 202 | this->append_blend(&p); |
| 203 | this->maybe_clamp(&p); |
| 204 | } |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 205 | this->append_store(&p); |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 206 | fBlitH = p.compile(); |
| 207 | } |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 208 | |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 209 | fDstPtr = fDst.writable_addr(0,y); |
| 210 | fBlitH(x,w); |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | 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] | 214 | if (!fBlitAntiH) { |
| 215 | SkRasterPipeline p; |
| 216 | p.extend(fShader); |
Mike Klein | 6686617 | 2016-11-03 12:22:01 -0400 | [diff] [blame] | 217 | if (fBlend == SkBlendMode::kSrcOver) { |
| 218 | p.append(SkRasterPipeline::scale_constant_float, &fConstantCoverage); |
| 219 | this->append_load_d(&p); |
| 220 | this->append_blend(&p); |
| 221 | } else { |
| 222 | this->append_load_d(&p); |
| 223 | this->append_blend(&p); |
| 224 | p.append(SkRasterPipeline::lerp_constant_float, &fConstantCoverage); |
| 225 | } |
Mike Klein | 130863e | 2016-10-27 11:29:36 -0400 | [diff] [blame] | 226 | this->maybe_clamp(&p); |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 227 | this->append_store(&p); |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 228 | fBlitAntiH = p.compile(); |
| 229 | } |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 230 | |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 231 | fDstPtr = fDst.writable_addr(0,y); |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 232 | for (int16_t run = *runs; run > 0; run = *runs) { |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 233 | fConstantCoverage = *aa * (1/255.0f); |
| 234 | fBlitAntiH(x, run); |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 235 | |
| 236 | x += run; |
| 237 | runs += run; |
| 238 | aa += run; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | void SkRasterPipelineBlitter::blitMask(const SkMask& mask, const SkIRect& clip) { |
| 243 | if (mask.fFormat == SkMask::kBW_Format) { |
| 244 | // TODO: native BW masks? |
| 245 | return INHERITED::blitMask(mask, clip); |
| 246 | } |
| 247 | |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 248 | if (mask.fFormat == SkMask::kA8_Format && !fBlitMaskA8) { |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 249 | SkRasterPipeline p; |
| 250 | p.extend(fShader); |
Mike Klein | 6686617 | 2016-11-03 12:22:01 -0400 | [diff] [blame] | 251 | if (fBlend == SkBlendMode::kSrcOver) { |
| 252 | p.append(SkRasterPipeline::scale_u8, &fMaskPtr); |
| 253 | this->append_load_d(&p); |
| 254 | this->append_blend(&p); |
| 255 | } else { |
| 256 | this->append_load_d(&p); |
| 257 | this->append_blend(&p); |
| 258 | p.append(SkRasterPipeline::lerp_u8, &fMaskPtr); |
| 259 | } |
Mike Klein | 130863e | 2016-10-27 11:29:36 -0400 | [diff] [blame] | 260 | this->maybe_clamp(&p); |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 261 | this->append_store(&p); |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 262 | fBlitMaskA8 = p.compile(); |
| 263 | } |
| 264 | |
| 265 | if (mask.fFormat == SkMask::kLCD16_Format && !fBlitMaskLCD16) { |
| 266 | SkRasterPipeline p; |
| 267 | p.extend(fShader); |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 268 | this->append_load_d(&p); |
| 269 | this->append_blend(&p); |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 270 | p.append(SkRasterPipeline::lerp_565, &fMaskPtr); |
Mike Klein | 130863e | 2016-10-27 11:29:36 -0400 | [diff] [blame] | 271 | this->maybe_clamp(&p); |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 272 | this->append_store(&p); |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 273 | fBlitMaskLCD16 = p.compile(); |
| 274 | } |
| 275 | |
| 276 | int x = clip.left(); |
| 277 | for (int y = clip.top(); y < clip.bottom(); y++) { |
| 278 | fDstPtr = fDst.writable_addr(0,y); |
| 279 | |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 280 | switch (mask.fFormat) { |
| 281 | case SkMask::kA8_Format: |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 282 | fMaskPtr = mask.getAddr8(x,y)-x; |
| 283 | fBlitMaskA8(x, clip.width()); |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 284 | break; |
| 285 | case SkMask::kLCD16_Format: |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 286 | fMaskPtr = mask.getAddrLCD16(x,y)-x; |
| 287 | fBlitMaskLCD16(x, clip.width()); |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 288 | break; |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 289 | default: |
| 290 | // TODO |
| 291 | break; |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 292 | } |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 293 | } |
| 294 | } |