blob: 0adf33af4bc8f3b78c9d5c03b74a8344f54646d2 [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 Klein744908e2016-11-11 12:51:36 -050012#include "SkFixedAlloc.h"
Mike Kleinbaaf8ad2016-09-29 09:04:15 -040013#include "SkOpts.h"
mtklein9a5c47f2016-07-22 11:05:04 -070014#include "SkPM4f.h"
Mike Klein744908e2016-11-11 12:51:36 -050015#include "SkPM4fPriv.h"
mtklein9a5c47f2016-07-22 11:05:04 -070016#include "SkRasterPipeline.h"
17#include "SkShader.h"
mtklein9a5c47f2016-07-22 11:05:04 -070018#include "SkXfermode.h"
19
20
21class SkRasterPipelineBlitter : public SkBlitter {
22public:
Mike Kleinfb191da2016-11-15 13:20:33 -050023 static SkBlitter* Create(const SkPixmap&, const SkPaint&, const SkMatrix& ctm,
24 SkTBlitterAllocator*);
mtklein9a5c47f2016-07-22 11:05:04 -070025
mtkleina4a44882016-11-04 13:20:07 -070026 SkRasterPipelineBlitter(SkPixmap dst, SkBlendMode blend, SkPM4f paintColor)
mtklein9a5c47f2016-07-22 11:05:04 -070027 : fDst(dst)
Mike Kleine902f8d2016-10-26 15:32:26 -040028 , fBlend(blend)
mtklein9a5c47f2016-07-22 11:05:04 -070029 , fPaintColor(paintColor)
Mike Klein744908e2016-11-11 12:51:36 -050030 , fScratchAlloc(fScratch, sizeof(fScratch))
31 , fScratchFallback(&fScratchAlloc)
mtklein9a5c47f2016-07-22 11:05:04 -070032 {}
33
34 void blitH (int x, int y, int w) override;
35 void blitAntiH(int x, int y, const SkAlpha[], const int16_t[]) override;
36 void blitMask (const SkMask&, const SkIRect& clip) override;
37
38 // TODO: The default implementations of the other blits look fine,
39 // but some of them like blitV could probably benefit from custom
40 // blits using something like a SkRasterPipeline::runFew() method.
41
42private:
Mike Kleine902f8d2016-10-26 15:32:26 -040043 void append_load_d(SkRasterPipeline*) const;
44 void append_store (SkRasterPipeline*) const;
45 void append_blend (SkRasterPipeline*) const;
Mike Klein130863e2016-10-27 11:29:36 -040046 void maybe_clamp (SkRasterPipeline*) const;
mtklein8e4373f2016-07-22 14:20:27 -070047
mtklein9a5c47f2016-07-22 11:05:04 -070048 SkPixmap fDst;
Mike Kleine902f8d2016-10-26 15:32:26 -040049 SkBlendMode fBlend;
mtklein9a5c47f2016-07-22 11:05:04 -070050 SkPM4f fPaintColor;
mtkleina4a44882016-11-04 13:20:07 -070051 SkRasterPipeline fShader;
mtklein9a5c47f2016-07-22 11:05:04 -070052
Mike Kleinbd3fe472016-10-25 15:43:46 -040053 // These functions are compiled lazily when first used.
Mike Kleinaf49b192016-11-15 08:52:04 -050054 std::function<void(size_t, size_t, size_t)> fBlitH = nullptr,
55 fBlitAntiH = nullptr,
56 fBlitMaskA8 = nullptr,
57 fBlitMaskLCD16 = nullptr;
Mike Kleinbd3fe472016-10-25 15:43:46 -040058
59 // These values are pointed to by the compiled blit functions
60 // above, which allows us to adjust them from call to call.
61 void* fDstPtr = nullptr;
62 const void* fMaskPtr = nullptr;
63 float fConstantCoverage = 0.0f;
64
Mike Klein744908e2016-11-11 12:51:36 -050065 // Scratch space for shaders and color filters to use.
66 char fScratch[64];
67 SkFixedAlloc fScratchAlloc;
68 SkFallbackAlloc fScratchFallback;
69
mtklein9a5c47f2016-07-22 11:05:04 -070070 typedef SkBlitter INHERITED;
71};
72
73SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst,
74 const SkPaint& paint,
Mike Kleinfb191da2016-11-15 13:20:33 -050075 const SkMatrix& ctm,
mtklein9a5c47f2016-07-22 11:05:04 -070076 SkTBlitterAllocator* alloc) {
Mike Kleinfb191da2016-11-15 13:20:33 -050077 return SkRasterPipelineBlitter::Create(dst, paint, ctm, alloc);
mtklein9a5c47f2016-07-22 11:05:04 -070078}
79
mtklein8e4373f2016-07-22 14:20:27 -070080static bool supported(const SkImageInfo& info) {
mtklein8e4373f2016-07-22 14:20:27 -070081 switch (info.colorType()) {
mtklein4e90e3e2016-07-25 14:35:31 -070082 case kN32_SkColorType: return info.gammaCloseToSRGB();
83 case kRGBA_F16_SkColorType: return true;
84 case kRGB_565_SkColorType: return true;
85 default: return false;
mtklein8e4373f2016-07-22 14:20:27 -070086 }
87}
mtklein9a5c47f2016-07-22 11:05:04 -070088
mtklein9a5c47f2016-07-22 11:05:04 -070089SkBlitter* SkRasterPipelineBlitter::Create(const SkPixmap& dst,
90 const SkPaint& paint,
Mike Kleinfb191da2016-11-15 13:20:33 -050091 const SkMatrix& ctm,
mtklein9a5c47f2016-07-22 11:05:04 -070092 SkTBlitterAllocator* alloc) {
Mike Klein744908e2016-11-11 12:51:36 -050093 auto blitter = alloc->createT<SkRasterPipelineBlitter>(
94 dst,
95 paint.getBlendMode(),
96 SkPM4f_from_SkColor(paint.getColor(), dst.colorSpace()));
97
Mike Kleinf98b7302016-11-06 10:18:06 -050098 auto earlyOut = [&] {
herbbf6d80a2016-11-15 06:26:56 -080099 alloc->deleteLast();
Mike Kleinf98b7302016-11-06 10:18:06 -0500100 return nullptr;
101 };
102
mtkleina4a44882016-11-04 13:20:07 -0700103 SkBlendMode* blend = &blitter->fBlend;
104 SkPM4f* paintColor = &blitter->fPaintColor;
105 SkRasterPipeline* pipeline = &blitter->fShader;
106
107 SkShader* shader = paint.getShader();
108 SkColorFilter* colorFilter = paint.getColorFilter();
109
110 // TODO: all temporary
Mike Kleinb0d10e02016-11-12 10:29:26 -0500111 if (!supported(dst.info()) || !SkBlendMode_AppendStages(*blend)) {
Mike Kleinf98b7302016-11-06 10:18:06 -0500112 return earlyOut();
mtklein9a5c47f2016-07-22 11:05:04 -0700113 }
mtkleina4a44882016-11-04 13:20:07 -0700114
Mike Kleinb0d10e02016-11-12 10:29:26 -0500115 bool is_opaque = paintColor->a() == 1.0f,
116 is_constant = true;
117 pipeline->append(SkRasterPipeline::constant_color, paintColor);
118
mtkleina4a44882016-11-04 13:20:07 -0700119 if (shader) {
Mike Kleinfb191da2016-11-15 13:20:33 -0500120 // Shaders start with the paint color in (r,g,b,a) and dst-space (x,y) in (dr,dg).
121 // Before the shader runs, move the paint color to (dr,dg,db,da), and put (x,y) in (r,g).
122 pipeline->append(SkRasterPipeline::swap_src_dst);
123 if (!shader->appendStages(pipeline, dst.colorSpace(), &blitter->fScratchFallback, ctm)) {
Mike Kleinb0d10e02016-11-12 10:29:26 -0500124 return earlyOut();
125 }
Mike Kleinfb191da2016-11-15 13:20:33 -0500126 // srcin, s' = s * da, i.e. modulate the output of the shader by the paint alpha.
127 pipeline->append(SkRasterPipeline::srcin);
128
129 is_opaque = is_opaque && shader->isOpaque();
130 is_constant = shader->isConstant();
Mike Kleine902f8d2016-10-26 15:32:26 -0400131 }
mtklein9a5c47f2016-07-22 11:05:04 -0700132
mtkleina4a44882016-11-04 13:20:07 -0700133 if (colorFilter) {
Mike Klein744908e2016-11-11 12:51:36 -0500134 if (!colorFilter->appendStages(pipeline, dst.colorSpace(), &blitter->fScratchFallback,
135 is_opaque)) {
Mike Kleinf98b7302016-11-06 10:18:06 -0500136 return earlyOut();
Mike Klein66866172016-11-03 12:22:01 -0400137 }
mtkleina4a44882016-11-04 13:20:07 -0700138 is_opaque = is_opaque && (colorFilter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag);
mtklein9a5c47f2016-07-22 11:05:04 -0700139 }
140
mtkleina4a44882016-11-04 13:20:07 -0700141 if (is_constant) {
142 pipeline->append(SkRasterPipeline::store_f32, &paintColor);
Mike Kleinaf49b192016-11-15 08:52:04 -0500143 pipeline->compile()(0,0, 1);
mtkleina4a44882016-11-04 13:20:07 -0700144
145 *pipeline = SkRasterPipeline();
146 pipeline->append(SkRasterPipeline::constant_color, paintColor);
147
148 is_opaque = paintColor->a() == 1.0f;
Mike Klein66866172016-11-03 12:22:01 -0400149 }
mtklein8e4373f2016-07-22 14:20:27 -0700150
mtkleina4a44882016-11-04 13:20:07 -0700151 if (is_opaque && *blend == SkBlendMode::kSrcOver) {
152 *blend = SkBlendMode::kSrc;
mtklein8e4373f2016-07-22 14:20:27 -0700153 }
154
mtklein9a5c47f2016-07-22 11:05:04 -0700155 return blitter;
156}
157
Mike Kleine902f8d2016-10-26 15:32:26 -0400158void SkRasterPipelineBlitter::append_load_d(SkRasterPipeline* p) const {
mtklein8e4373f2016-07-22 14:20:27 -0700159 SkASSERT(supported(fDst.info()));
160
161 switch (fDst.info().colorType()) {
162 case kN32_SkColorType:
163 if (fDst.info().gammaCloseToSRGB()) {
Mike Kleine902f8d2016-10-26 15:32:26 -0400164 p->append(SkRasterPipeline::load_d_srgb, &fDstPtr);
mtklein8e4373f2016-07-22 14:20:27 -0700165 }
166 break;
mtklein4e90e3e2016-07-25 14:35:31 -0700167 case kRGBA_F16_SkColorType:
Mike Kleine902f8d2016-10-26 15:32:26 -0400168 p->append(SkRasterPipeline::load_d_f16, &fDstPtr);
mtklein4e90e3e2016-07-25 14:35:31 -0700169 break;
mtklein8e4373f2016-07-22 14:20:27 -0700170 case kRGB_565_SkColorType:
Mike Kleine902f8d2016-10-26 15:32:26 -0400171 p->append(SkRasterPipeline::load_d_565, &fDstPtr);
mtklein8e4373f2016-07-22 14:20:27 -0700172 break;
173 default: break;
174 }
175}
176
Mike Kleine902f8d2016-10-26 15:32:26 -0400177void SkRasterPipelineBlitter::append_store(SkRasterPipeline* p) const {
mtklein8e4373f2016-07-22 14:20:27 -0700178 SkASSERT(supported(fDst.info()));
179
180 switch (fDst.info().colorType()) {
181 case kN32_SkColorType:
182 if (fDst.info().gammaCloseToSRGB()) {
Mike Kleine902f8d2016-10-26 15:32:26 -0400183 p->append(SkRasterPipeline::store_srgb, &fDstPtr);
mtklein8e4373f2016-07-22 14:20:27 -0700184 }
185 break;
mtklein4e90e3e2016-07-25 14:35:31 -0700186 case kRGBA_F16_SkColorType:
Mike Kleine902f8d2016-10-26 15:32:26 -0400187 p->append(SkRasterPipeline::store_f16, &fDstPtr);
mtklein4e90e3e2016-07-25 14:35:31 -0700188 break;
mtklein8e4373f2016-07-22 14:20:27 -0700189 case kRGB_565_SkColorType:
Mike Kleine902f8d2016-10-26 15:32:26 -0400190 p->append(SkRasterPipeline::store_565, &fDstPtr);
mtklein8e4373f2016-07-22 14:20:27 -0700191 break;
192 default: break;
193 }
194}
195
Mike Kleine902f8d2016-10-26 15:32:26 -0400196void SkRasterPipelineBlitter::append_blend(SkRasterPipeline* p) const {
197 SkAssertResult(SkBlendMode_AppendStages(fBlend, p));
198}
Mike Kleine9f74b82016-10-25 13:31:21 -0400199
Mike Klein130863e2016-10-27 11:29:36 -0400200void SkRasterPipelineBlitter::maybe_clamp(SkRasterPipeline* p) const {
Mike Kleineea7c162016-11-03 10:20:35 -0400201 if (SkBlendMode_CanOverflow(fBlend)) { p->append(SkRasterPipeline::clamp_a); }
Mike Klein130863e2016-10-27 11:29:36 -0400202}
203
mtklein9a5c47f2016-07-22 11:05:04 -0700204void SkRasterPipelineBlitter::blitH(int x, int y, int w) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400205 if (!fBlitH) {
206 SkRasterPipeline p;
207 p.extend(fShader);
Mike Klein66866172016-11-03 12:22:01 -0400208 if (fBlend != SkBlendMode::kSrc) {
209 this->append_load_d(&p);
210 this->append_blend(&p);
211 this->maybe_clamp(&p);
212 }
Mike Kleine902f8d2016-10-26 15:32:26 -0400213 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400214 fBlitH = p.compile();
215 }
mtklein9a5c47f2016-07-22 11:05:04 -0700216
Mike Kleinbd3fe472016-10-25 15:43:46 -0400217 fDstPtr = fDst.writable_addr(0,y);
Mike Kleinaf49b192016-11-15 08:52:04 -0500218 fBlitH(x,y, w);
mtklein9a5c47f2016-07-22 11:05:04 -0700219}
220
221void SkRasterPipelineBlitter::blitAntiH(int x, int y, const SkAlpha aa[], const int16_t runs[]) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400222 if (!fBlitAntiH) {
223 SkRasterPipeline p;
224 p.extend(fShader);
Mike Klein66866172016-11-03 12:22:01 -0400225 if (fBlend == SkBlendMode::kSrcOver) {
226 p.append(SkRasterPipeline::scale_constant_float, &fConstantCoverage);
227 this->append_load_d(&p);
228 this->append_blend(&p);
229 } else {
230 this->append_load_d(&p);
231 this->append_blend(&p);
232 p.append(SkRasterPipeline::lerp_constant_float, &fConstantCoverage);
233 }
Mike Klein130863e2016-10-27 11:29:36 -0400234 this->maybe_clamp(&p);
Mike Kleine902f8d2016-10-26 15:32:26 -0400235 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400236 fBlitAntiH = p.compile();
237 }
mtklein9a5c47f2016-07-22 11:05:04 -0700238
Mike Kleinbd3fe472016-10-25 15:43:46 -0400239 fDstPtr = fDst.writable_addr(0,y);
mtklein9a5c47f2016-07-22 11:05:04 -0700240 for (int16_t run = *runs; run > 0; run = *runs) {
Mike Kleinaeb79592016-11-07 09:29:07 -0500241 switch (*aa) {
242 case 0x00: break;
243 case 0xff: this->blitH(x,y,run); break;
244 default:
245 fConstantCoverage = *aa * (1/255.0f);
Mike Kleinaf49b192016-11-15 08:52:04 -0500246 fBlitAntiH(x,y, run);
Mike Kleinaeb79592016-11-07 09:29:07 -0500247 }
mtklein9a5c47f2016-07-22 11:05:04 -0700248 x += run;
249 runs += run;
250 aa += run;
251 }
252}
253
254void SkRasterPipelineBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
255 if (mask.fFormat == SkMask::kBW_Format) {
256 // TODO: native BW masks?
257 return INHERITED::blitMask(mask, clip);
258 }
259
Mike Kleinbd3fe472016-10-25 15:43:46 -0400260 if (mask.fFormat == SkMask::kA8_Format && !fBlitMaskA8) {
mtklein9a5c47f2016-07-22 11:05:04 -0700261 SkRasterPipeline p;
262 p.extend(fShader);
Mike Klein66866172016-11-03 12:22:01 -0400263 if (fBlend == SkBlendMode::kSrcOver) {
264 p.append(SkRasterPipeline::scale_u8, &fMaskPtr);
265 this->append_load_d(&p);
266 this->append_blend(&p);
267 } else {
268 this->append_load_d(&p);
269 this->append_blend(&p);
270 p.append(SkRasterPipeline::lerp_u8, &fMaskPtr);
271 }
Mike Klein130863e2016-10-27 11:29:36 -0400272 this->maybe_clamp(&p);
Mike Kleine902f8d2016-10-26 15:32:26 -0400273 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400274 fBlitMaskA8 = p.compile();
275 }
276
277 if (mask.fFormat == SkMask::kLCD16_Format && !fBlitMaskLCD16) {
278 SkRasterPipeline p;
279 p.extend(fShader);
Mike Kleine902f8d2016-10-26 15:32:26 -0400280 this->append_load_d(&p);
281 this->append_blend(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400282 p.append(SkRasterPipeline::lerp_565, &fMaskPtr);
Mike Klein130863e2016-10-27 11:29:36 -0400283 this->maybe_clamp(&p);
Mike Kleine902f8d2016-10-26 15:32:26 -0400284 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400285 fBlitMaskLCD16 = p.compile();
286 }
287
288 int x = clip.left();
289 for (int y = clip.top(); y < clip.bottom(); y++) {
290 fDstPtr = fDst.writable_addr(0,y);
291
mtklein9a5c47f2016-07-22 11:05:04 -0700292 switch (mask.fFormat) {
293 case SkMask::kA8_Format:
Mike Kleinbd3fe472016-10-25 15:43:46 -0400294 fMaskPtr = mask.getAddr8(x,y)-x;
Mike Kleinaf49b192016-11-15 08:52:04 -0500295 fBlitMaskA8(x,y, clip.width());
mtklein9a5c47f2016-07-22 11:05:04 -0700296 break;
297 case SkMask::kLCD16_Format:
Mike Kleinbd3fe472016-10-25 15:43:46 -0400298 fMaskPtr = mask.getAddrLCD16(x,y)-x;
Mike Kleinaf49b192016-11-15 08:52:04 -0500299 fBlitMaskLCD16(x,y, clip.width());
mtklein9a5c47f2016-07-22 11:05:04 -0700300 break;
Mike Kleinbd3fe472016-10-25 15:43:46 -0400301 default:
302 // TODO
303 break;
mtklein9a5c47f2016-07-22 11:05:04 -0700304 }
mtklein9a5c47f2016-07-22 11:05:04 -0700305 }
306}