blob: d382eba80d9da837dd12ab0b26805dbe4e25d47a [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:
23 static SkBlitter* Create(const SkPixmap&, const SkPaint&, SkTBlitterAllocator*);
24
mtkleina4a44882016-11-04 13:20:07 -070025 SkRasterPipelineBlitter(SkPixmap dst, SkBlendMode blend, SkPM4f paintColor)
mtklein9a5c47f2016-07-22 11:05:04 -070026 : fDst(dst)
Mike Kleine902f8d2016-10-26 15:32:26 -040027 , fBlend(blend)
mtklein9a5c47f2016-07-22 11:05:04 -070028 , fPaintColor(paintColor)
Mike Klein744908e2016-11-11 12:51:36 -050029 , fScratchAlloc(fScratch, sizeof(fScratch))
30 , fScratchFallback(&fScratchAlloc)
mtklein9a5c47f2016-07-22 11:05:04 -070031 {}
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 SkBlendMode fBlend;
mtklein9a5c47f2016-07-22 11:05:04 -070049 SkPM4f fPaintColor;
mtkleina4a44882016-11-04 13:20:07 -070050 SkRasterPipeline fShader;
mtklein9a5c47f2016-07-22 11:05:04 -070051
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
Mike Klein744908e2016-11-11 12:51:36 -050064 // Scratch space for shaders and color filters to use.
65 char fScratch[64];
66 SkFixedAlloc fScratchAlloc;
67 SkFallbackAlloc fScratchFallback;
68
mtklein9a5c47f2016-07-22 11:05:04 -070069 typedef SkBlitter INHERITED;
70};
71
72SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst,
73 const SkPaint& paint,
74 SkTBlitterAllocator* alloc) {
75 return SkRasterPipelineBlitter::Create(dst, paint, alloc);
76}
77
mtklein8e4373f2016-07-22 14:20:27 -070078static bool supported(const SkImageInfo& info) {
mtklein8e4373f2016-07-22 14:20:27 -070079 switch (info.colorType()) {
mtklein4e90e3e2016-07-25 14:35:31 -070080 case kN32_SkColorType: return info.gammaCloseToSRGB();
81 case kRGBA_F16_SkColorType: return true;
82 case kRGB_565_SkColorType: return true;
83 default: return false;
mtklein8e4373f2016-07-22 14:20:27 -070084 }
85}
mtklein9a5c47f2016-07-22 11:05:04 -070086
mtklein9a5c47f2016-07-22 11:05:04 -070087SkBlitter* SkRasterPipelineBlitter::Create(const SkPixmap& dst,
88 const SkPaint& paint,
89 SkTBlitterAllocator* alloc) {
Mike Klein744908e2016-11-11 12:51:36 -050090 auto blitter = alloc->createT<SkRasterPipelineBlitter>(
91 dst,
92 paint.getBlendMode(),
93 SkPM4f_from_SkColor(paint.getColor(), dst.colorSpace()));
94
Mike Kleinf98b7302016-11-06 10:18:06 -050095 auto earlyOut = [&] {
mtklein7c591162016-11-09 14:50:31 -080096 blitter->~SkRasterPipelineBlitter();
97 alloc->freeLast();
Mike Kleinf98b7302016-11-06 10:18:06 -050098 return nullptr;
99 };
100
mtkleina4a44882016-11-04 13:20:07 -0700101 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)) {
Mike Kleinf98b7302016-11-06 10:18:06 -0500110 return earlyOut();
mtklein9a5c47f2016-07-22 11:05:04 -0700111 }
mtkleina4a44882016-11-04 13:20:07 -0700112
113 bool is_opaque, is_constant;
114 if (shader) {
115 is_opaque = shader->isOpaque();
116 is_constant = false; // TODO: shader->isConstant()
117 // TODO: append shader stages, of course!
118 } else {
119 is_opaque = paintColor->a() == 1.0f;
120 is_constant = true;
121 pipeline->append(SkRasterPipeline::constant_color, paintColor);
Mike Kleine902f8d2016-10-26 15:32:26 -0400122 }
mtklein9a5c47f2016-07-22 11:05:04 -0700123
mtkleina4a44882016-11-04 13:20:07 -0700124 if (colorFilter) {
Mike Klein744908e2016-11-11 12:51:36 -0500125 if (!colorFilter->appendStages(pipeline, dst.colorSpace(), &blitter->fScratchFallback,
126 is_opaque)) {
Mike Kleinf98b7302016-11-06 10:18:06 -0500127 return earlyOut();
Mike Klein66866172016-11-03 12:22:01 -0400128 }
mtkleina4a44882016-11-04 13:20:07 -0700129 is_opaque = is_opaque && (colorFilter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag);
mtklein9a5c47f2016-07-22 11:05:04 -0700130 }
131
mtkleina4a44882016-11-04 13:20:07 -0700132 if (is_constant) {
133 pipeline->append(SkRasterPipeline::store_f32, &paintColor);
134 pipeline->compile()(0,1);
135
136 *pipeline = SkRasterPipeline();
137 pipeline->append(SkRasterPipeline::constant_color, paintColor);
138
139 is_opaque = paintColor->a() == 1.0f;
Mike Klein66866172016-11-03 12:22:01 -0400140 }
mtklein8e4373f2016-07-22 14:20:27 -0700141
mtkleina4a44882016-11-04 13:20:07 -0700142 if (is_opaque && *blend == SkBlendMode::kSrcOver) {
143 *blend = SkBlendMode::kSrc;
mtklein8e4373f2016-07-22 14:20:27 -0700144 }
145
mtklein9a5c47f2016-07-22 11:05:04 -0700146 return blitter;
147}
148
Mike Kleine902f8d2016-10-26 15:32:26 -0400149void SkRasterPipelineBlitter::append_load_d(SkRasterPipeline* p) const {
mtklein8e4373f2016-07-22 14:20:27 -0700150 SkASSERT(supported(fDst.info()));
151
152 switch (fDst.info().colorType()) {
153 case kN32_SkColorType:
154 if (fDst.info().gammaCloseToSRGB()) {
Mike Kleine902f8d2016-10-26 15:32:26 -0400155 p->append(SkRasterPipeline::load_d_srgb, &fDstPtr);
mtklein8e4373f2016-07-22 14:20:27 -0700156 }
157 break;
mtklein4e90e3e2016-07-25 14:35:31 -0700158 case kRGBA_F16_SkColorType:
Mike Kleine902f8d2016-10-26 15:32:26 -0400159 p->append(SkRasterPipeline::load_d_f16, &fDstPtr);
mtklein4e90e3e2016-07-25 14:35:31 -0700160 break;
mtklein8e4373f2016-07-22 14:20:27 -0700161 case kRGB_565_SkColorType:
Mike Kleine902f8d2016-10-26 15:32:26 -0400162 p->append(SkRasterPipeline::load_d_565, &fDstPtr);
mtklein8e4373f2016-07-22 14:20:27 -0700163 break;
164 default: break;
165 }
166}
167
Mike Kleine902f8d2016-10-26 15:32:26 -0400168void SkRasterPipelineBlitter::append_store(SkRasterPipeline* p) const {
mtklein8e4373f2016-07-22 14:20:27 -0700169 SkASSERT(supported(fDst.info()));
170
171 switch (fDst.info().colorType()) {
172 case kN32_SkColorType:
173 if (fDst.info().gammaCloseToSRGB()) {
Mike Kleine902f8d2016-10-26 15:32:26 -0400174 p->append(SkRasterPipeline::store_srgb, &fDstPtr);
mtklein8e4373f2016-07-22 14:20:27 -0700175 }
176 break;
mtklein4e90e3e2016-07-25 14:35:31 -0700177 case kRGBA_F16_SkColorType:
Mike Kleine902f8d2016-10-26 15:32:26 -0400178 p->append(SkRasterPipeline::store_f16, &fDstPtr);
mtklein4e90e3e2016-07-25 14:35:31 -0700179 break;
mtklein8e4373f2016-07-22 14:20:27 -0700180 case kRGB_565_SkColorType:
Mike Kleine902f8d2016-10-26 15:32:26 -0400181 p->append(SkRasterPipeline::store_565, &fDstPtr);
mtklein8e4373f2016-07-22 14:20:27 -0700182 break;
183 default: break;
184 }
185}
186
Mike Kleine902f8d2016-10-26 15:32:26 -0400187void SkRasterPipelineBlitter::append_blend(SkRasterPipeline* p) const {
188 SkAssertResult(SkBlendMode_AppendStages(fBlend, p));
189}
Mike Kleine9f74b82016-10-25 13:31:21 -0400190
Mike Klein130863e2016-10-27 11:29:36 -0400191void SkRasterPipelineBlitter::maybe_clamp(SkRasterPipeline* p) const {
Mike Kleineea7c162016-11-03 10:20:35 -0400192 if (SkBlendMode_CanOverflow(fBlend)) { p->append(SkRasterPipeline::clamp_a); }
Mike Klein130863e2016-10-27 11:29:36 -0400193}
194
mtklein9a5c47f2016-07-22 11:05:04 -0700195void SkRasterPipelineBlitter::blitH(int x, int y, int w) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400196 if (!fBlitH) {
197 SkRasterPipeline p;
198 p.extend(fShader);
Mike Klein66866172016-11-03 12:22:01 -0400199 if (fBlend != SkBlendMode::kSrc) {
200 this->append_load_d(&p);
201 this->append_blend(&p);
202 this->maybe_clamp(&p);
203 }
Mike Kleine902f8d2016-10-26 15:32:26 -0400204 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400205 fBlitH = p.compile();
206 }
mtklein9a5c47f2016-07-22 11:05:04 -0700207
Mike Kleinbd3fe472016-10-25 15:43:46 -0400208 fDstPtr = fDst.writable_addr(0,y);
209 fBlitH(x,w);
mtklein9a5c47f2016-07-22 11:05:04 -0700210}
211
212void SkRasterPipelineBlitter::blitAntiH(int x, int y, const SkAlpha aa[], const int16_t runs[]) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400213 if (!fBlitAntiH) {
214 SkRasterPipeline p;
215 p.extend(fShader);
Mike Klein66866172016-11-03 12:22:01 -0400216 if (fBlend == SkBlendMode::kSrcOver) {
217 p.append(SkRasterPipeline::scale_constant_float, &fConstantCoverage);
218 this->append_load_d(&p);
219 this->append_blend(&p);
220 } else {
221 this->append_load_d(&p);
222 this->append_blend(&p);
223 p.append(SkRasterPipeline::lerp_constant_float, &fConstantCoverage);
224 }
Mike Klein130863e2016-10-27 11:29:36 -0400225 this->maybe_clamp(&p);
Mike Kleine902f8d2016-10-26 15:32:26 -0400226 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400227 fBlitAntiH = p.compile();
228 }
mtklein9a5c47f2016-07-22 11:05:04 -0700229
Mike Kleinbd3fe472016-10-25 15:43:46 -0400230 fDstPtr = fDst.writable_addr(0,y);
mtklein9a5c47f2016-07-22 11:05:04 -0700231 for (int16_t run = *runs; run > 0; run = *runs) {
Mike Kleinaeb79592016-11-07 09:29:07 -0500232 switch (*aa) {
233 case 0x00: break;
234 case 0xff: this->blitH(x,y,run); break;
235 default:
236 fConstantCoverage = *aa * (1/255.0f);
237 fBlitAntiH(x, run);
238 }
mtklein9a5c47f2016-07-22 11:05:04 -0700239 x += run;
240 runs += run;
241 aa += run;
242 }
243}
244
245void SkRasterPipelineBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
246 if (mask.fFormat == SkMask::kBW_Format) {
247 // TODO: native BW masks?
248 return INHERITED::blitMask(mask, clip);
249 }
250
Mike Kleinbd3fe472016-10-25 15:43:46 -0400251 if (mask.fFormat == SkMask::kA8_Format && !fBlitMaskA8) {
mtklein9a5c47f2016-07-22 11:05:04 -0700252 SkRasterPipeline p;
253 p.extend(fShader);
Mike Klein66866172016-11-03 12:22:01 -0400254 if (fBlend == SkBlendMode::kSrcOver) {
255 p.append(SkRasterPipeline::scale_u8, &fMaskPtr);
256 this->append_load_d(&p);
257 this->append_blend(&p);
258 } else {
259 this->append_load_d(&p);
260 this->append_blend(&p);
261 p.append(SkRasterPipeline::lerp_u8, &fMaskPtr);
262 }
Mike Klein130863e2016-10-27 11:29:36 -0400263 this->maybe_clamp(&p);
Mike Kleine902f8d2016-10-26 15:32:26 -0400264 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400265 fBlitMaskA8 = p.compile();
266 }
267
268 if (mask.fFormat == SkMask::kLCD16_Format && !fBlitMaskLCD16) {
269 SkRasterPipeline p;
270 p.extend(fShader);
Mike Kleine902f8d2016-10-26 15:32:26 -0400271 this->append_load_d(&p);
272 this->append_blend(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400273 p.append(SkRasterPipeline::lerp_565, &fMaskPtr);
Mike Klein130863e2016-10-27 11:29:36 -0400274 this->maybe_clamp(&p);
Mike Kleine902f8d2016-10-26 15:32:26 -0400275 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400276 fBlitMaskLCD16 = p.compile();
277 }
278
279 int x = clip.left();
280 for (int y = clip.top(); y < clip.bottom(); y++) {
281 fDstPtr = fDst.writable_addr(0,y);
282
mtklein9a5c47f2016-07-22 11:05:04 -0700283 switch (mask.fFormat) {
284 case SkMask::kA8_Format:
Mike Kleinbd3fe472016-10-25 15:43:46 -0400285 fMaskPtr = mask.getAddr8(x,y)-x;
286 fBlitMaskA8(x, clip.width());
mtklein9a5c47f2016-07-22 11:05:04 -0700287 break;
288 case SkMask::kLCD16_Format:
Mike Kleinbd3fe472016-10-25 15:43:46 -0400289 fMaskPtr = mask.getAddrLCD16(x,y)-x;
290 fBlitMaskLCD16(x, clip.width());
mtklein9a5c47f2016-07-22 11:05:04 -0700291 break;
Mike Kleinbd3fe472016-10-25 15:43:46 -0400292 default:
293 // TODO
294 break;
mtklein9a5c47f2016-07-22 11:05:04 -0700295 }
mtklein9a5c47f2016-07-22 11:05:04 -0700296 }
297}