blob: 31c188e4f9afab98d53dad21ceae8162d3880876 [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
23 SkRasterPipelineBlitter(SkPixmap dst,
24 SkRasterPipeline shader,
Mike Kleine902f8d2016-10-26 15:32:26 -040025 SkBlendMode blend,
mtklein9a5c47f2016-07-22 11:05:04 -070026 SkPM4f paintColor)
27 : fDst(dst)
28 , fShader(shader)
Mike Kleine902f8d2016-10-26 15:32:26 -040029 , fBlend(blend)
mtklein9a5c47f2016-07-22 11:05:04 -070030 , 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
41private:
Mike Kleine902f8d2016-10-26 15:32:26 -040042 void append_load_d(SkRasterPipeline*) const;
43 void append_store (SkRasterPipeline*) const;
44 void append_blend (SkRasterPipeline*) const;
Mike Klein130863e2016-10-27 11:29:36 -040045 void maybe_clamp (SkRasterPipeline*) const;
mtklein8e4373f2016-07-22 14:20:27 -070046
mtklein9a5c47f2016-07-22 11:05:04 -070047 SkPixmap fDst;
Mike Kleine902f8d2016-10-26 15:32:26 -040048 SkRasterPipeline fShader;
49 SkBlendMode fBlend;
mtklein9a5c47f2016-07-22 11:05:04 -070050 SkPM4f fPaintColor;
51
Mike Kleinbd3fe472016-10-25 15:43:46 -040052 // 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
mtklein9a5c47f2016-07-22 11:05:04 -070064 typedef SkBlitter INHERITED;
65};
66
67SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst,
68 const SkPaint& paint,
69 SkTBlitterAllocator* alloc) {
70 return SkRasterPipelineBlitter::Create(dst, paint, alloc);
71}
72
mtklein8e4373f2016-07-22 14:20:27 -070073static bool supported(const SkImageInfo& info) {
mtklein8e4373f2016-07-22 14:20:27 -070074 switch (info.colorType()) {
mtklein4e90e3e2016-07-25 14:35:31 -070075 case kN32_SkColorType: return info.gammaCloseToSRGB();
76 case kRGBA_F16_SkColorType: return true;
77 case kRGB_565_SkColorType: return true;
78 default: return false;
mtklein8e4373f2016-07-22 14:20:27 -070079 }
80}
mtklein9a5c47f2016-07-22 11:05:04 -070081
82template <typename Effect>
83static bool append_effect_stages(const Effect* effect, SkRasterPipeline* pipeline) {
84 return !effect || effect->appendStages(pipeline);
85}
86
87
88SkBlitter* SkRasterPipelineBlitter::Create(const SkPixmap& dst,
89 const SkPaint& paint,
90 SkTBlitterAllocator* alloc) {
mtklein8e4373f2016-07-22 14:20:27 -070091 if (!supported(dst.info())) {
92 return nullptr;
mtklein9a5c47f2016-07-22 11:05:04 -070093 }
94 if (paint.getShader()) {
95 return nullptr; // TODO: need to work out how shaders and their contexts work
96 }
Mike Kleine902f8d2016-10-26 15:32:26 -040097 SkBlendMode blend = paint.getBlendMode();
98 if (!SkBlendMode_AppendStages(blend)) {
99 return nullptr; // TODO
100 }
mtklein9a5c47f2016-07-22 11:05:04 -0700101
Mike Kleineea7c162016-11-03 10:20:35 -0400102 uint32_t paintColor = paint.getColor();
103 bool shaderIsOpaque = (paintColor >> 24) == 0xff;
104
Mike Kleine902f8d2016-10-26 15:32:26 -0400105 SkRasterPipeline shader, colorFilter;
Mike Klein66866172016-11-03 12:22:01 -0400106 if (auto s = paint.getShader()) {
107 shaderIsOpaque = s->isOpaque();
Mike Kleineea7c162016-11-03 10:20:35 -0400108 }
Mike Klein66866172016-11-03 12:22:01 -0400109 if (auto cf = paint.getColorFilter()) {
110 if (!cf->appendStages(&colorFilter, shaderIsOpaque)) {
111 return nullptr;
112 }
113 shaderIsOpaque = shaderIsOpaque && (cf->getFlags() & SkColorFilter::kAlphaUnchanged_Flag);
mtklein9a5c47f2016-07-22 11:05:04 -0700114 }
115
Mike Klein66866172016-11-03 12:22:01 -0400116 if (shaderIsOpaque && blend == SkBlendMode::kSrcOver) {
117 blend = SkBlendMode::kSrc;
118 }
mtklein8e4373f2016-07-22 14:20:27 -0700119
120 SkColor4f color;
Brian Osman7039f742016-11-01 15:56:16 -0400121 if (dst.info().colorSpace()) {
mtklein8e4373f2016-07-22 14:20:27 -0700122 color = SkColor4f::FromColor(paintColor);
123 } else {
124 swizzle_rb(SkNx_cast<float>(Sk4b::Load(&paintColor)) * (1/255.0f)).store(&color);
125 }
126
Mike Kleine902f8d2016-10-26 15:32:26 -0400127 auto blitter = alloc->createT<SkRasterPipelineBlitter>(dst, shader, blend, color.premul());
mtklein9a5c47f2016-07-22 11:05:04 -0700128
129 if (!paint.getShader()) {
Mike Kleinfa9f2412016-09-29 13:40:01 -0400130 blitter->fShader.append(SkRasterPipeline::constant_color, &blitter->fPaintColor);
mtklein9a5c47f2016-07-22 11:05:04 -0700131 }
Mike Kleine902f8d2016-10-26 15:32:26 -0400132 blitter->fShader.extend(colorFilter);
mtklein9a5c47f2016-07-22 11:05:04 -0700133
134 return blitter;
135}
136
Mike Kleine902f8d2016-10-26 15:32:26 -0400137void SkRasterPipelineBlitter::append_load_d(SkRasterPipeline* p) const {
mtklein8e4373f2016-07-22 14:20:27 -0700138 SkASSERT(supported(fDst.info()));
139
140 switch (fDst.info().colorType()) {
141 case kN32_SkColorType:
142 if (fDst.info().gammaCloseToSRGB()) {
Mike Kleine902f8d2016-10-26 15:32:26 -0400143 p->append(SkRasterPipeline::load_d_srgb, &fDstPtr);
mtklein8e4373f2016-07-22 14:20:27 -0700144 }
145 break;
mtklein4e90e3e2016-07-25 14:35:31 -0700146 case kRGBA_F16_SkColorType:
Mike Kleine902f8d2016-10-26 15:32:26 -0400147 p->append(SkRasterPipeline::load_d_f16, &fDstPtr);
mtklein4e90e3e2016-07-25 14:35:31 -0700148 break;
mtklein8e4373f2016-07-22 14:20:27 -0700149 case kRGB_565_SkColorType:
Mike Kleine902f8d2016-10-26 15:32:26 -0400150 p->append(SkRasterPipeline::load_d_565, &fDstPtr);
mtklein8e4373f2016-07-22 14:20:27 -0700151 break;
152 default: break;
153 }
154}
155
Mike Kleine902f8d2016-10-26 15:32:26 -0400156void SkRasterPipelineBlitter::append_store(SkRasterPipeline* p) const {
mtklein8e4373f2016-07-22 14:20:27 -0700157 SkASSERT(supported(fDst.info()));
158
159 switch (fDst.info().colorType()) {
160 case kN32_SkColorType:
161 if (fDst.info().gammaCloseToSRGB()) {
Mike Kleine902f8d2016-10-26 15:32:26 -0400162 p->append(SkRasterPipeline::store_srgb, &fDstPtr);
mtklein8e4373f2016-07-22 14:20:27 -0700163 }
164 break;
mtklein4e90e3e2016-07-25 14:35:31 -0700165 case kRGBA_F16_SkColorType:
Mike Kleine902f8d2016-10-26 15:32:26 -0400166 p->append(SkRasterPipeline::store_f16, &fDstPtr);
mtklein4e90e3e2016-07-25 14:35:31 -0700167 break;
mtklein8e4373f2016-07-22 14:20:27 -0700168 case kRGB_565_SkColorType:
Mike Kleine902f8d2016-10-26 15:32:26 -0400169 p->append(SkRasterPipeline::store_565, &fDstPtr);
mtklein8e4373f2016-07-22 14:20:27 -0700170 break;
171 default: break;
172 }
173}
174
Mike Kleine902f8d2016-10-26 15:32:26 -0400175void SkRasterPipelineBlitter::append_blend(SkRasterPipeline* p) const {
176 SkAssertResult(SkBlendMode_AppendStages(fBlend, p));
177}
Mike Kleine9f74b82016-10-25 13:31:21 -0400178
Mike Klein130863e2016-10-27 11:29:36 -0400179void SkRasterPipelineBlitter::maybe_clamp(SkRasterPipeline* p) const {
Mike Kleineea7c162016-11-03 10:20:35 -0400180 if (SkBlendMode_CanOverflow(fBlend)) { p->append(SkRasterPipeline::clamp_a); }
Mike Klein130863e2016-10-27 11:29:36 -0400181}
182
mtklein9a5c47f2016-07-22 11:05:04 -0700183void SkRasterPipelineBlitter::blitH(int x, int y, int w) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400184 if (!fBlitH) {
185 SkRasterPipeline p;
186 p.extend(fShader);
Mike Klein66866172016-11-03 12:22:01 -0400187 if (fBlend != SkBlendMode::kSrc) {
188 this->append_load_d(&p);
189 this->append_blend(&p);
190 this->maybe_clamp(&p);
191 }
Mike Kleine902f8d2016-10-26 15:32:26 -0400192 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400193 fBlitH = p.compile();
194 }
mtklein9a5c47f2016-07-22 11:05:04 -0700195
Mike Kleinbd3fe472016-10-25 15:43:46 -0400196 fDstPtr = fDst.writable_addr(0,y);
197 fBlitH(x,w);
mtklein9a5c47f2016-07-22 11:05:04 -0700198}
199
200void SkRasterPipelineBlitter::blitAntiH(int x, int y, const SkAlpha aa[], const int16_t runs[]) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400201 if (!fBlitAntiH) {
202 SkRasterPipeline p;
203 p.extend(fShader);
Mike Klein66866172016-11-03 12:22:01 -0400204 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 Klein130863e2016-10-27 11:29:36 -0400213 this->maybe_clamp(&p);
Mike Kleine902f8d2016-10-26 15:32:26 -0400214 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400215 fBlitAntiH = p.compile();
216 }
mtklein9a5c47f2016-07-22 11:05:04 -0700217
Mike Kleinbd3fe472016-10-25 15:43:46 -0400218 fDstPtr = fDst.writable_addr(0,y);
mtklein9a5c47f2016-07-22 11:05:04 -0700219 for (int16_t run = *runs; run > 0; run = *runs) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400220 fConstantCoverage = *aa * (1/255.0f);
221 fBlitAntiH(x, run);
mtklein9a5c47f2016-07-22 11:05:04 -0700222
223 x += run;
224 runs += run;
225 aa += run;
226 }
227}
228
229void 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 Kleinbd3fe472016-10-25 15:43:46 -0400235 if (mask.fFormat == SkMask::kA8_Format && !fBlitMaskA8) {
mtklein9a5c47f2016-07-22 11:05:04 -0700236 SkRasterPipeline p;
237 p.extend(fShader);
Mike Klein66866172016-11-03 12:22:01 -0400238 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 Klein130863e2016-10-27 11:29:36 -0400247 this->maybe_clamp(&p);
Mike Kleine902f8d2016-10-26 15:32:26 -0400248 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400249 fBlitMaskA8 = p.compile();
250 }
251
252 if (mask.fFormat == SkMask::kLCD16_Format && !fBlitMaskLCD16) {
253 SkRasterPipeline p;
254 p.extend(fShader);
Mike Kleine902f8d2016-10-26 15:32:26 -0400255 this->append_load_d(&p);
256 this->append_blend(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400257 p.append(SkRasterPipeline::lerp_565, &fMaskPtr);
Mike Klein130863e2016-10-27 11:29:36 -0400258 this->maybe_clamp(&p);
Mike Kleine902f8d2016-10-26 15:32:26 -0400259 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400260 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
mtklein9a5c47f2016-07-22 11:05:04 -0700267 switch (mask.fFormat) {
268 case SkMask::kA8_Format:
Mike Kleinbd3fe472016-10-25 15:43:46 -0400269 fMaskPtr = mask.getAddr8(x,y)-x;
270 fBlitMaskA8(x, clip.width());
mtklein9a5c47f2016-07-22 11:05:04 -0700271 break;
272 case SkMask::kLCD16_Format:
Mike Kleinbd3fe472016-10-25 15:43:46 -0400273 fMaskPtr = mask.getAddrLCD16(x,y)-x;
274 fBlitMaskLCD16(x, clip.width());
mtklein9a5c47f2016-07-22 11:05:04 -0700275 break;
Mike Kleinbd3fe472016-10-25 15:43:46 -0400276 default:
277 // TODO
278 break;
mtklein9a5c47f2016-07-22 11:05:04 -0700279 }
mtklein9a5c47f2016-07-22 11:05:04 -0700280 }
281}