blob: b8c0107e262fd916b28a52ea6051a2bb19ce2fac [file] [log] [blame]
mtklein9a5c47f2016-07-22 11:05:04 -07001/*
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 Kleine902f8d2016-10-26 15:32:26 -04009#include "SkBlendModePriv.h"
mtklein9a5c47f2016-07-22 11:05:04 -070010#include "SkColor.h"
11#include "SkColorFilter.h"
Mike Kleinbaaf8ad2016-09-29 09:04:15 -040012#include "SkOpts.h"
mtklein9a5c47f2016-07-22 11:05:04 -070013#include "SkPM4f.h"
mtklein9a5c47f2016-07-22 11:05:04 -070014#include "SkRasterPipeline.h"
15#include "SkShader.h"
mtklein9a5c47f2016-07-22 11:05:04 -070016#include "SkXfermode.h"
17
18
19class SkRasterPipelineBlitter : public SkBlitter {
20public:
21 static SkBlitter* Create(const SkPixmap&, const SkPaint&, SkTBlitterAllocator*);
22
mtkleina4a44882016-11-04 13:20:07 -070023 SkRasterPipelineBlitter(SkPixmap dst, SkBlendMode blend, SkPM4f paintColor)
mtklein9a5c47f2016-07-22 11:05:04 -070024 : fDst(dst)
Mike Kleine902f8d2016-10-26 15:32:26 -040025 , fBlend(blend)
mtklein9a5c47f2016-07-22 11:05:04 -070026 , 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
37private:
Mike Kleine902f8d2016-10-26 15:32:26 -040038 void append_load_d(SkRasterPipeline*) const;
39 void append_store (SkRasterPipeline*) const;
40 void append_blend (SkRasterPipeline*) const;
Mike Klein130863e2016-10-27 11:29:36 -040041 void maybe_clamp (SkRasterPipeline*) const;
mtklein8e4373f2016-07-22 14:20:27 -070042
mtklein9a5c47f2016-07-22 11:05:04 -070043 SkPixmap fDst;
Mike Kleine902f8d2016-10-26 15:32:26 -040044 SkBlendMode fBlend;
mtklein9a5c47f2016-07-22 11:05:04 -070045 SkPM4f fPaintColor;
mtkleina4a44882016-11-04 13:20:07 -070046 SkRasterPipeline fShader;
mtklein9a5c47f2016-07-22 11:05:04 -070047
Mike Kleinbd3fe472016-10-25 15:43:46 -040048 // 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
mtklein9a5c47f2016-07-22 11:05:04 -070060 typedef SkBlitter INHERITED;
61};
62
63SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst,
64 const SkPaint& paint,
65 SkTBlitterAllocator* alloc) {
66 return SkRasterPipelineBlitter::Create(dst, paint, alloc);
67}
68
mtklein8e4373f2016-07-22 14:20:27 -070069static bool supported(const SkImageInfo& info) {
mtklein8e4373f2016-07-22 14:20:27 -070070 switch (info.colorType()) {
mtklein4e90e3e2016-07-25 14:35:31 -070071 case kN32_SkColorType: return info.gammaCloseToSRGB();
72 case kRGBA_F16_SkColorType: return true;
73 case kRGB_565_SkColorType: return true;
74 default: return false;
mtklein8e4373f2016-07-22 14:20:27 -070075 }
76}
mtklein9a5c47f2016-07-22 11:05:04 -070077
78template <typename Effect>
79static bool append_effect_stages(const Effect* effect, SkRasterPipeline* pipeline) {
80 return !effect || effect->appendStages(pipeline);
81}
82
mtkleina4a44882016-11-04 13:20:07 -070083static 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}
mtklein9a5c47f2016-07-22 11:05:04 -070094
95SkBlitter* SkRasterPipelineBlitter::Create(const SkPixmap& dst,
96 const SkPaint& paint,
97 SkTBlitterAllocator* alloc) {
mtkleina4a44882016-11-04 13:20:07 -070098 auto blitter = alloc->createT<SkRasterPipelineBlitter>(dst,
99 paint.getBlendMode(),
100 paint_color(dst, paint));
Mike Kleinf98b7302016-11-06 10:18:06 -0500101 auto earlyOut = [&] {
102 blitter->~SkRasterPipelineBlitter();
103 alloc->freeLast();
104 return nullptr;
105 };
106
mtkleina4a44882016-11-04 13:20:07 -0700107 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 Kleinf98b7302016-11-06 10:18:06 -0500116 return earlyOut();
mtklein9a5c47f2016-07-22 11:05:04 -0700117 }
mtkleina4a44882016-11-04 13:20:07 -0700118
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 Kleine902f8d2016-10-26 15:32:26 -0400128 }
mtklein9a5c47f2016-07-22 11:05:04 -0700129
mtkleina4a44882016-11-04 13:20:07 -0700130 if (colorFilter) {
131 if (!colorFilter->appendStages(pipeline, is_opaque)) {
Mike Kleinf98b7302016-11-06 10:18:06 -0500132 return earlyOut();
Mike Klein66866172016-11-03 12:22:01 -0400133 }
mtkleina4a44882016-11-04 13:20:07 -0700134 is_opaque = is_opaque && (colorFilter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag);
mtklein9a5c47f2016-07-22 11:05:04 -0700135 }
136
mtkleina4a44882016-11-04 13:20:07 -0700137 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 Klein66866172016-11-03 12:22:01 -0400145 }
mtklein8e4373f2016-07-22 14:20:27 -0700146
mtkleina4a44882016-11-04 13:20:07 -0700147 if (is_opaque && *blend == SkBlendMode::kSrcOver) {
148 *blend = SkBlendMode::kSrc;
mtklein8e4373f2016-07-22 14:20:27 -0700149 }
150
mtklein9a5c47f2016-07-22 11:05:04 -0700151 return blitter;
152}
153
Mike Kleine902f8d2016-10-26 15:32:26 -0400154void SkRasterPipelineBlitter::append_load_d(SkRasterPipeline* p) const {
mtklein8e4373f2016-07-22 14:20:27 -0700155 SkASSERT(supported(fDst.info()));
156
157 switch (fDst.info().colorType()) {
158 case kN32_SkColorType:
159 if (fDst.info().gammaCloseToSRGB()) {
Mike Kleine902f8d2016-10-26 15:32:26 -0400160 p->append(SkRasterPipeline::load_d_srgb, &fDstPtr);
mtklein8e4373f2016-07-22 14:20:27 -0700161 }
162 break;
mtklein4e90e3e2016-07-25 14:35:31 -0700163 case kRGBA_F16_SkColorType:
Mike Kleine902f8d2016-10-26 15:32:26 -0400164 p->append(SkRasterPipeline::load_d_f16, &fDstPtr);
mtklein4e90e3e2016-07-25 14:35:31 -0700165 break;
mtklein8e4373f2016-07-22 14:20:27 -0700166 case kRGB_565_SkColorType:
Mike Kleine902f8d2016-10-26 15:32:26 -0400167 p->append(SkRasterPipeline::load_d_565, &fDstPtr);
mtklein8e4373f2016-07-22 14:20:27 -0700168 break;
169 default: break;
170 }
171}
172
Mike Kleine902f8d2016-10-26 15:32:26 -0400173void SkRasterPipelineBlitter::append_store(SkRasterPipeline* p) const {
mtklein8e4373f2016-07-22 14:20:27 -0700174 SkASSERT(supported(fDst.info()));
175
176 switch (fDst.info().colorType()) {
177 case kN32_SkColorType:
178 if (fDst.info().gammaCloseToSRGB()) {
Mike Kleine902f8d2016-10-26 15:32:26 -0400179 p->append(SkRasterPipeline::store_srgb, &fDstPtr);
mtklein8e4373f2016-07-22 14:20:27 -0700180 }
181 break;
mtklein4e90e3e2016-07-25 14:35:31 -0700182 case kRGBA_F16_SkColorType:
Mike Kleine902f8d2016-10-26 15:32:26 -0400183 p->append(SkRasterPipeline::store_f16, &fDstPtr);
mtklein4e90e3e2016-07-25 14:35:31 -0700184 break;
mtklein8e4373f2016-07-22 14:20:27 -0700185 case kRGB_565_SkColorType:
Mike Kleine902f8d2016-10-26 15:32:26 -0400186 p->append(SkRasterPipeline::store_565, &fDstPtr);
mtklein8e4373f2016-07-22 14:20:27 -0700187 break;
188 default: break;
189 }
190}
191
Mike Kleine902f8d2016-10-26 15:32:26 -0400192void SkRasterPipelineBlitter::append_blend(SkRasterPipeline* p) const {
193 SkAssertResult(SkBlendMode_AppendStages(fBlend, p));
194}
Mike Kleine9f74b82016-10-25 13:31:21 -0400195
Mike Klein130863e2016-10-27 11:29:36 -0400196void SkRasterPipelineBlitter::maybe_clamp(SkRasterPipeline* p) const {
Mike Kleineea7c162016-11-03 10:20:35 -0400197 if (SkBlendMode_CanOverflow(fBlend)) { p->append(SkRasterPipeline::clamp_a); }
Mike Klein130863e2016-10-27 11:29:36 -0400198}
199
mtklein9a5c47f2016-07-22 11:05:04 -0700200void SkRasterPipelineBlitter::blitH(int x, int y, int w) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400201 if (!fBlitH) {
202 SkRasterPipeline p;
203 p.extend(fShader);
Mike Klein66866172016-11-03 12:22:01 -0400204 if (fBlend != SkBlendMode::kSrc) {
205 this->append_load_d(&p);
206 this->append_blend(&p);
207 this->maybe_clamp(&p);
208 }
Mike Kleine902f8d2016-10-26 15:32:26 -0400209 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400210 fBlitH = p.compile();
211 }
mtklein9a5c47f2016-07-22 11:05:04 -0700212
Mike Kleinbd3fe472016-10-25 15:43:46 -0400213 fDstPtr = fDst.writable_addr(0,y);
214 fBlitH(x,w);
mtklein9a5c47f2016-07-22 11:05:04 -0700215}
216
217void SkRasterPipelineBlitter::blitAntiH(int x, int y, const SkAlpha aa[], const int16_t runs[]) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400218 if (!fBlitAntiH) {
219 SkRasterPipeline p;
220 p.extend(fShader);
Mike Klein66866172016-11-03 12:22:01 -0400221 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 Klein130863e2016-10-27 11:29:36 -0400230 this->maybe_clamp(&p);
Mike Kleine902f8d2016-10-26 15:32:26 -0400231 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400232 fBlitAntiH = p.compile();
233 }
mtklein9a5c47f2016-07-22 11:05:04 -0700234
Mike Kleinbd3fe472016-10-25 15:43:46 -0400235 fDstPtr = fDst.writable_addr(0,y);
mtklein9a5c47f2016-07-22 11:05:04 -0700236 for (int16_t run = *runs; run > 0; run = *runs) {
Mike Kleinaeb79592016-11-07 09:29:07 -0500237 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 }
mtklein9a5c47f2016-07-22 11:05:04 -0700244 x += run;
245 runs += run;
246 aa += run;
247 }
248}
249
250void 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 Kleinbd3fe472016-10-25 15:43:46 -0400256 if (mask.fFormat == SkMask::kA8_Format && !fBlitMaskA8) {
mtklein9a5c47f2016-07-22 11:05:04 -0700257 SkRasterPipeline p;
258 p.extend(fShader);
Mike Klein66866172016-11-03 12:22:01 -0400259 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 Klein130863e2016-10-27 11:29:36 -0400268 this->maybe_clamp(&p);
Mike Kleine902f8d2016-10-26 15:32:26 -0400269 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400270 fBlitMaskA8 = p.compile();
271 }
272
273 if (mask.fFormat == SkMask::kLCD16_Format && !fBlitMaskLCD16) {
274 SkRasterPipeline p;
275 p.extend(fShader);
Mike Kleine902f8d2016-10-26 15:32:26 -0400276 this->append_load_d(&p);
277 this->append_blend(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400278 p.append(SkRasterPipeline::lerp_565, &fMaskPtr);
Mike Klein130863e2016-10-27 11:29:36 -0400279 this->maybe_clamp(&p);
Mike Kleine902f8d2016-10-26 15:32:26 -0400280 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400281 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
mtklein9a5c47f2016-07-22 11:05:04 -0700288 switch (mask.fFormat) {
289 case SkMask::kA8_Format:
Mike Kleinbd3fe472016-10-25 15:43:46 -0400290 fMaskPtr = mask.getAddr8(x,y)-x;
291 fBlitMaskA8(x, clip.width());
mtklein9a5c47f2016-07-22 11:05:04 -0700292 break;
293 case SkMask::kLCD16_Format:
Mike Kleinbd3fe472016-10-25 15:43:46 -0400294 fMaskPtr = mask.getAddrLCD16(x,y)-x;
295 fBlitMaskLCD16(x, clip.width());
mtklein9a5c47f2016-07-22 11:05:04 -0700296 break;
Mike Kleinbd3fe472016-10-25 15:43:46 -0400297 default:
298 // TODO
299 break;
mtklein9a5c47f2016-07-22 11:05:04 -0700300 }
mtklein9a5c47f2016-07-22 11:05:04 -0700301 }
302}