blob: 480a23c6b3d3340237c5d585da1797e24768d3b4 [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
19
20class SkRasterPipelineBlitter : public SkBlitter {
21public:
Mike Kleinfb191da2016-11-15 13:20:33 -050022 static SkBlitter* Create(const SkPixmap&, const SkPaint&, const SkMatrix& ctm,
23 SkTBlitterAllocator*);
mtklein9a5c47f2016-07-22 11:05:04 -070024
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.
Mike Kleinaf49b192016-11-15 08:52:04 -050053 std::function<void(size_t, size_t, size_t)> fBlitH = nullptr,
54 fBlitAntiH = nullptr,
55 fBlitMaskA8 = nullptr,
56 fBlitMaskLCD16 = nullptr;
Mike Kleinbd3fe472016-10-25 15:43:46 -040057
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,
Mike Kleinfb191da2016-11-15 13:20:33 -050074 const SkMatrix& ctm,
mtklein9a5c47f2016-07-22 11:05:04 -070075 SkTBlitterAllocator* alloc) {
Mike Kleinfb191da2016-11-15 13:20:33 -050076 return SkRasterPipelineBlitter::Create(dst, paint, ctm, alloc);
mtklein9a5c47f2016-07-22 11:05:04 -070077}
78
mtklein8e4373f2016-07-22 14:20:27 -070079static bool supported(const SkImageInfo& info) {
mtklein8e4373f2016-07-22 14:20:27 -070080 switch (info.colorType()) {
mtklein4e90e3e2016-07-25 14:35:31 -070081 case kN32_SkColorType: return info.gammaCloseToSRGB();
82 case kRGBA_F16_SkColorType: return true;
83 case kRGB_565_SkColorType: return true;
84 default: return false;
mtklein8e4373f2016-07-22 14:20:27 -070085 }
86}
mtklein9a5c47f2016-07-22 11:05:04 -070087
mtklein9a5c47f2016-07-22 11:05:04 -070088SkBlitter* SkRasterPipelineBlitter::Create(const SkPixmap& dst,
89 const SkPaint& paint,
Mike Kleinfb191da2016-11-15 13:20:33 -050090 const SkMatrix& ctm,
mtklein9a5c47f2016-07-22 11:05:04 -070091 SkTBlitterAllocator* alloc) {
Mike Klein744908e2016-11-11 12:51:36 -050092 auto blitter = alloc->createT<SkRasterPipelineBlitter>(
93 dst,
94 paint.getBlendMode(),
95 SkPM4f_from_SkColor(paint.getColor(), dst.colorSpace()));
96
Mike Kleinf98b7302016-11-06 10:18:06 -050097 auto earlyOut = [&] {
herbbf6d80a2016-11-15 06:26:56 -080098 alloc->deleteLast();
Mike Kleinf98b7302016-11-06 10:18:06 -050099 return nullptr;
100 };
101
mtkleina4a44882016-11-04 13:20:07 -0700102 SkBlendMode* blend = &blitter->fBlend;
103 SkPM4f* paintColor = &blitter->fPaintColor;
104 SkRasterPipeline* pipeline = &blitter->fShader;
105
106 SkShader* shader = paint.getShader();
107 SkColorFilter* colorFilter = paint.getColorFilter();
108
109 // TODO: all temporary
Mike Kleinb0d10e02016-11-12 10:29:26 -0500110 if (!supported(dst.info()) || !SkBlendMode_AppendStages(*blend)) {
Mike Kleinf98b7302016-11-06 10:18:06 -0500111 return earlyOut();
mtklein9a5c47f2016-07-22 11:05:04 -0700112 }
mtkleina4a44882016-11-04 13:20:07 -0700113
Mike Kleinb0d10e02016-11-12 10:29:26 -0500114 bool is_opaque = paintColor->a() == 1.0f,
115 is_constant = true;
Mike Kleinb0d10e02016-11-12 10:29:26 -0500116
mtkleina4a44882016-11-04 13:20:07 -0700117 if (shader) {
Mike Klein987de5b2016-11-17 10:47:39 -0500118 if (!shader->appendStages(pipeline, dst.colorSpace(), &blitter->fScratchFallback,
119 ctm, paint.getFilterQuality())) {
Mike Kleinb0d10e02016-11-12 10:29:26 -0500120 return earlyOut();
121 }
Mike Klein43c847b2016-11-21 13:05:23 -0500122 if (!is_opaque) {
123 pipeline->append(SkRasterPipeline::scale_constant_float,
124 &paintColor->fVec[SkPM4f::A]);
125 }
Mike Kleinfb191da2016-11-15 13:20:33 -0500126
127 is_opaque = is_opaque && shader->isOpaque();
128 is_constant = shader->isConstant();
Mike Klein43c847b2016-11-21 13:05:23 -0500129 } else {
130 pipeline->append(SkRasterPipeline::constant_color, paintColor);
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()) {
Mike Kleine03339a2016-11-28 13:24:27 -0500162 case kRGB_565_SkColorType: p->append(SkRasterPipeline::load_d_565, &fDstPtr); break;
163 case kBGRA_8888_SkColorType:
164 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::load_d_8888, &fDstPtr); break;
165 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::load_d_f16, &fDstPtr); break;
mtklein8e4373f2016-07-22 14:20:27 -0700166 default: break;
167 }
Mike Kleine03339a2016-11-28 13:24:27 -0500168 if (fDst.info().colorType() == kBGRA_8888_SkColorType) {
169 p->append(SkRasterPipeline::swap_drdb);
170 }
171 if (fDst.info().gammaCloseToSRGB()) {
172 p->append(SkRasterPipeline::from_srgb_d);
173 }
mtklein8e4373f2016-07-22 14:20:27 -0700174}
175
Mike Kleine902f8d2016-10-26 15:32:26 -0400176void SkRasterPipelineBlitter::append_store(SkRasterPipeline* p) const {
mtklein8e4373f2016-07-22 14:20:27 -0700177 SkASSERT(supported(fDst.info()));
178
Mike Kleine03339a2016-11-28 13:24:27 -0500179 if (fDst.info().gammaCloseToSRGB()) {
180 p->append(SkRasterPipeline::to_srgb);
181 }
182 if (fDst.info().colorType() == kBGRA_8888_SkColorType) {
183 p->append(SkRasterPipeline::swap_rb);
184 }
mtklein8e4373f2016-07-22 14:20:27 -0700185 switch (fDst.info().colorType()) {
Mike Kleine03339a2016-11-28 13:24:27 -0500186 case kRGB_565_SkColorType: p->append(SkRasterPipeline::store_565, &fDstPtr); break;
187 case kBGRA_8888_SkColorType:
188 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::store_8888, &fDstPtr); break;
189 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::store_f16, &fDstPtr); break;
mtklein8e4373f2016-07-22 14:20:27 -0700190 default: break;
191 }
192}
193
Mike Kleine902f8d2016-10-26 15:32:26 -0400194void SkRasterPipelineBlitter::append_blend(SkRasterPipeline* p) const {
195 SkAssertResult(SkBlendMode_AppendStages(fBlend, p));
196}
Mike Kleine9f74b82016-10-25 13:31:21 -0400197
Mike Klein130863e2016-10-27 11:29:36 -0400198void SkRasterPipelineBlitter::maybe_clamp(SkRasterPipeline* p) const {
Mike Kleine03339a2016-11-28 13:24:27 -0500199 if (SkBlendMode_CanOverflow(fBlend)) {
200 p->append(SkRasterPipeline::clamp_a);
201 }
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}