blob: d2a0b3d4c17e37285deb54a1888e5b84760fd0bb [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"
Mike Klein14c8f822016-11-30 19:39:43 -050018#include "SkUtils.h"
mtklein9a5c47f2016-07-22 11:05:04 -070019
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 Klein14c8f822016-11-30 19:39:43 -050043 void append_load_d(SkRasterPipeline*) const;
44 void append_blend (SkRasterPipeline*) const;
45 void maybe_clamp (SkRasterPipeline*) const;
46 void append_store (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.
Mike Kleinbabd93e2016-11-30 16:05:10 -050061 void* fDstPtr = nullptr;
62 const void* fMaskPtr = nullptr;
63 float fCurrentCoverage = 0.0f;
Mike Kleinbd3fe472016-10-25 15:43:46 -040064
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
Mike Klein21aa3d02016-11-30 16:40:56 -0500103 SkBlendMode* blend = &blitter->fBlend;
104 SkPM4f* paintColor = &blitter->fPaintColor;
105 SkRasterPipeline* pipeline = &blitter->fShader;
mtkleina4a44882016-11-04 13:20:07 -0700106
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 Klein14c8f822016-11-30 19:39:43 -0500115 bool is_opaque = paintColor->a() == 1.0f,
116 is_constant = true;
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,
Mike Klein7a147342016-11-29 15:33:39 -0500119 ctm, paint)) {
Mike Kleinb0d10e02016-11-12 10:29:26 -0500120 return earlyOut();
121 }
Mike Klein43c847b2016-11-21 13:05:23 -0500122 if (!is_opaque) {
Mike Kleinbabd93e2016-11-30 16:05:10 -0500123 pipeline->append(SkRasterPipeline::scale_1_float,
Mike Klein43c847b2016-11-21 13:05:23 -0500124 &paintColor->fVec[SkPM4f::A]);
125 }
Mike Kleinfb191da2016-11-15 13:20:33 -0500126
127 is_opaque = is_opaque && shader->isOpaque();
Mike Klein14c8f822016-11-30 19:39:43 -0500128 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
Mike Klein14c8f822016-11-30 19:39:43 -0500141 if (is_constant) {
mtkleina4a44882016-11-04 13:20:07 -0700142 pipeline->append(SkRasterPipeline::store_f32, &paintColor);
Mike Kleinc789b612016-11-30 13:45:06 -0500143 pipeline->run(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
Mike Klein14c8f822016-11-30 19:39:43 -0500155 if (is_constant && *blend == SkBlendMode::kSrc) {
156 uint64_t color; // Big enough for largest dst format, F16.
157 SkRasterPipeline p;
158 p.extend(*pipeline);
159 blitter->fDstPtr = &color;
160 blitter->append_store(&p);
161 p.run(0,0, 1);
162
163 switch (dst.shiftPerPixel()) {
164 case 1:
165 blitter->fBlitH = [blitter,color](size_t x, size_t, size_t n) {
166 sk_memset16((uint16_t*)blitter->fDstPtr + x, color, n);
167 };
168 break;
169 case 2:
170 blitter->fBlitH = [blitter,color](size_t x, size_t, size_t n) {
171 sk_memset32((uint32_t*)blitter->fDstPtr + x, color, n);
172 };
173 break;
174 case 3:
175 blitter->fBlitH = [blitter,color](size_t x, size_t, size_t n) {
176 sk_memset64((uint64_t*)blitter->fDstPtr + x, color, n);
177 };
178 break;
179 default: break;
180 }
181 }
182
mtklein9a5c47f2016-07-22 11:05:04 -0700183 return blitter;
184}
185
Mike Kleine902f8d2016-10-26 15:32:26 -0400186void SkRasterPipelineBlitter::append_load_d(SkRasterPipeline* p) const {
mtklein8e4373f2016-07-22 14:20:27 -0700187 SkASSERT(supported(fDst.info()));
188
189 switch (fDst.info().colorType()) {
Mike Klein729b5822016-11-28 18:23:23 -0500190 case kRGB_565_SkColorType: p->append(SkRasterPipeline::load_565_d, &fDstPtr); break;
Mike Kleine03339a2016-11-28 13:24:27 -0500191 case kBGRA_8888_SkColorType:
Mike Klein729b5822016-11-28 18:23:23 -0500192 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::load_8888_d, &fDstPtr); break;
193 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::load_f16_d, &fDstPtr); break;
mtklein8e4373f2016-07-22 14:20:27 -0700194 default: break;
195 }
Mike Kleine03339a2016-11-28 13:24:27 -0500196 if (fDst.info().colorType() == kBGRA_8888_SkColorType) {
Mike Klein729b5822016-11-28 18:23:23 -0500197 p->append(SkRasterPipeline::swap_rb_d);
Mike Kleine03339a2016-11-28 13:24:27 -0500198 }
199 if (fDst.info().gammaCloseToSRGB()) {
200 p->append(SkRasterPipeline::from_srgb_d);
201 }
mtklein8e4373f2016-07-22 14:20:27 -0700202}
203
Mike Klein14c8f822016-11-30 19:39:43 -0500204void SkRasterPipelineBlitter::append_store(SkRasterPipeline* p) const {
Mike Kleine03339a2016-11-28 13:24:27 -0500205 if (fDst.info().gammaCloseToSRGB()) {
206 p->append(SkRasterPipeline::to_srgb);
207 }
208 if (fDst.info().colorType() == kBGRA_8888_SkColorType) {
209 p->append(SkRasterPipeline::swap_rb);
210 }
Mike Klein21aa3d02016-11-30 16:40:56 -0500211
Mike Klein21aa3d02016-11-30 16:40:56 -0500212 SkASSERT(supported(fDst.info()));
mtklein8e4373f2016-07-22 14:20:27 -0700213 switch (fDst.info().colorType()) {
Mike Kleine03339a2016-11-28 13:24:27 -0500214 case kRGB_565_SkColorType: p->append(SkRasterPipeline::store_565, &fDstPtr); break;
215 case kBGRA_8888_SkColorType:
216 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::store_8888, &fDstPtr); break;
217 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::store_f16, &fDstPtr); break;
mtklein8e4373f2016-07-22 14:20:27 -0700218 default: break;
219 }
220}
221
Mike Kleine902f8d2016-10-26 15:32:26 -0400222void SkRasterPipelineBlitter::append_blend(SkRasterPipeline* p) const {
223 SkAssertResult(SkBlendMode_AppendStages(fBlend, p));
224}
Mike Kleine9f74b82016-10-25 13:31:21 -0400225
Mike Klein130863e2016-10-27 11:29:36 -0400226void SkRasterPipelineBlitter::maybe_clamp(SkRasterPipeline* p) const {
Mike Kleine03339a2016-11-28 13:24:27 -0500227 if (SkBlendMode_CanOverflow(fBlend)) {
228 p->append(SkRasterPipeline::clamp_a);
229 }
Mike Klein130863e2016-10-27 11:29:36 -0400230}
231
mtklein9a5c47f2016-07-22 11:05:04 -0700232void SkRasterPipelineBlitter::blitH(int x, int y, int w) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400233 if (!fBlitH) {
234 SkRasterPipeline p;
235 p.extend(fShader);
Mike Klein66866172016-11-03 12:22:01 -0400236 if (fBlend != SkBlendMode::kSrc) {
237 this->append_load_d(&p);
238 this->append_blend(&p);
239 this->maybe_clamp(&p);
240 }
Mike Kleine902f8d2016-10-26 15:32:26 -0400241 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400242 fBlitH = p.compile();
243 }
Mike Kleinbd3fe472016-10-25 15:43:46 -0400244 fDstPtr = fDst.writable_addr(0,y);
Mike Kleinaf49b192016-11-15 08:52:04 -0500245 fBlitH(x,y, w);
mtklein9a5c47f2016-07-22 11:05:04 -0700246}
247
248void SkRasterPipelineBlitter::blitAntiH(int x, int y, const SkAlpha aa[], const int16_t runs[]) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400249 if (!fBlitAntiH) {
250 SkRasterPipeline p;
251 p.extend(fShader);
Mike Klein66866172016-11-03 12:22:01 -0400252 if (fBlend == SkBlendMode::kSrcOver) {
Mike Kleinbabd93e2016-11-30 16:05:10 -0500253 p.append(SkRasterPipeline::scale_1_float, &fCurrentCoverage);
Mike Klein66866172016-11-03 12:22:01 -0400254 this->append_load_d(&p);
255 this->append_blend(&p);
256 } else {
257 this->append_load_d(&p);
258 this->append_blend(&p);
Mike Kleinbabd93e2016-11-30 16:05:10 -0500259 p.append(SkRasterPipeline::lerp_1_float, &fCurrentCoverage);
Mike Klein66866172016-11-03 12:22:01 -0400260 }
Mike Klein130863e2016-10-27 11:29:36 -0400261 this->maybe_clamp(&p);
Mike Kleine902f8d2016-10-26 15:32:26 -0400262 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400263 fBlitAntiH = p.compile();
264 }
mtklein9a5c47f2016-07-22 11:05:04 -0700265
Mike Kleinbd3fe472016-10-25 15:43:46 -0400266 fDstPtr = fDst.writable_addr(0,y);
mtklein9a5c47f2016-07-22 11:05:04 -0700267 for (int16_t run = *runs; run > 0; run = *runs) {
Mike Kleinaeb79592016-11-07 09:29:07 -0500268 switch (*aa) {
269 case 0x00: break;
270 case 0xff: this->blitH(x,y,run); break;
271 default:
Mike Kleinbabd93e2016-11-30 16:05:10 -0500272 fCurrentCoverage = *aa * (1/255.0f);
Mike Kleinaf49b192016-11-15 08:52:04 -0500273 fBlitAntiH(x,y, run);
Mike Kleinaeb79592016-11-07 09:29:07 -0500274 }
mtklein9a5c47f2016-07-22 11:05:04 -0700275 x += run;
276 runs += run;
277 aa += run;
278 }
279}
280
281void SkRasterPipelineBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
282 if (mask.fFormat == SkMask::kBW_Format) {
283 // TODO: native BW masks?
284 return INHERITED::blitMask(mask, clip);
285 }
286
Mike Kleinbd3fe472016-10-25 15:43:46 -0400287 if (mask.fFormat == SkMask::kA8_Format && !fBlitMaskA8) {
mtklein9a5c47f2016-07-22 11:05:04 -0700288 SkRasterPipeline p;
289 p.extend(fShader);
Mike Klein66866172016-11-03 12:22:01 -0400290 if (fBlend == SkBlendMode::kSrcOver) {
291 p.append(SkRasterPipeline::scale_u8, &fMaskPtr);
292 this->append_load_d(&p);
293 this->append_blend(&p);
294 } else {
295 this->append_load_d(&p);
296 this->append_blend(&p);
297 p.append(SkRasterPipeline::lerp_u8, &fMaskPtr);
298 }
Mike Klein130863e2016-10-27 11:29:36 -0400299 this->maybe_clamp(&p);
Mike Kleine902f8d2016-10-26 15:32:26 -0400300 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400301 fBlitMaskA8 = p.compile();
302 }
303
304 if (mask.fFormat == SkMask::kLCD16_Format && !fBlitMaskLCD16) {
305 SkRasterPipeline p;
306 p.extend(fShader);
Mike Kleine902f8d2016-10-26 15:32:26 -0400307 this->append_load_d(&p);
308 this->append_blend(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400309 p.append(SkRasterPipeline::lerp_565, &fMaskPtr);
Mike Klein130863e2016-10-27 11:29:36 -0400310 this->maybe_clamp(&p);
Mike Kleine902f8d2016-10-26 15:32:26 -0400311 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400312 fBlitMaskLCD16 = p.compile();
313 }
314
315 int x = clip.left();
316 for (int y = clip.top(); y < clip.bottom(); y++) {
317 fDstPtr = fDst.writable_addr(0,y);
318
mtklein9a5c47f2016-07-22 11:05:04 -0700319 switch (mask.fFormat) {
320 case SkMask::kA8_Format:
Mike Kleinbd3fe472016-10-25 15:43:46 -0400321 fMaskPtr = mask.getAddr8(x,y)-x;
Mike Kleinaf49b192016-11-15 08:52:04 -0500322 fBlitMaskA8(x,y, clip.width());
mtklein9a5c47f2016-07-22 11:05:04 -0700323 break;
324 case SkMask::kLCD16_Format:
Mike Kleinbd3fe472016-10-25 15:43:46 -0400325 fMaskPtr = mask.getAddrLCD16(x,y)-x;
Mike Kleinaf49b192016-11-15 08:52:04 -0500326 fBlitMaskLCD16(x,y, clip.width());
mtklein9a5c47f2016-07-22 11:05:04 -0700327 break;
Mike Kleinbd3fe472016-10-25 15:43:46 -0400328 default:
329 // TODO
330 break;
mtklein9a5c47f2016-07-22 11:05:04 -0700331 }
mtklein9a5c47f2016-07-22 11:05:04 -0700332 }
333}