blob: 7f91cbf334723e39dfaaa4bf6a57a36caa8e0756 [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
Herb Derbyac04fef2017-01-13 17:34:33 -05008#include "SkArenaAlloc.h"
mtklein9a5c47f2016-07-22 11:05:04 -07009#include "SkBlitter.h"
Mike Kleine902f8d2016-10-26 15:32:26 -040010#include "SkBlendModePriv.h"
mtklein9a5c47f2016-07-22 11:05:04 -070011#include "SkColor.h"
12#include "SkColorFilter.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)
30 {}
31
32 void blitH (int x, int y, int w) override;
33 void blitAntiH(int x, int y, const SkAlpha[], const int16_t[]) override;
34 void blitMask (const SkMask&, const SkIRect& clip) override;
35
36 // TODO: The default implementations of the other blits look fine,
37 // but some of them like blitV could probably benefit from custom
38 // blits using something like a SkRasterPipeline::runFew() method.
39
40private:
Mike Klein14c8f822016-11-30 19:39:43 -050041 void append_load_d(SkRasterPipeline*) const;
42 void append_blend (SkRasterPipeline*) const;
43 void maybe_clamp (SkRasterPipeline*) const;
44 void append_store (SkRasterPipeline*) const;
mtklein8e4373f2016-07-22 14:20:27 -070045
mtklein9a5c47f2016-07-22 11:05:04 -070046 SkPixmap fDst;
Mike Kleine902f8d2016-10-26 15:32:26 -040047 SkBlendMode fBlend;
mtklein9a5c47f2016-07-22 11:05:04 -070048 SkPM4f fPaintColor;
mtkleina4a44882016-11-04 13:20:07 -070049 SkRasterPipeline fShader;
mtklein9a5c47f2016-07-22 11:05:04 -070050
Mike Kleinbd3fe472016-10-25 15:43:46 -040051 // These functions are compiled lazily when first used.
Mike Kleinaf49b192016-11-15 08:52:04 -050052 std::function<void(size_t, size_t, size_t)> fBlitH = nullptr,
53 fBlitAntiH = nullptr,
54 fBlitMaskA8 = nullptr,
55 fBlitMaskLCD16 = nullptr;
Mike Kleinbd3fe472016-10-25 15:43:46 -040056
57 // These values are pointed to by the compiled blit functions
58 // above, which allows us to adjust them from call to call.
Mike Kleinbabd93e2016-11-30 16:05:10 -050059 void* fDstPtr = nullptr;
60 const void* fMaskPtr = nullptr;
61 float fCurrentCoverage = 0.0f;
Mike Kleinbd3fe472016-10-25 15:43:46 -040062
Mike Klein744908e2016-11-11 12:51:36 -050063 // Scratch space for shaders and color filters to use.
64 char fScratch[64];
Herb Derbyac04fef2017-01-13 17:34:33 -050065 SkArenaAlloc fArena{fScratch, sizeof(fScratch), 128};
Mike Klein744908e2016-11-11 12:51:36 -050066
mtklein9a5c47f2016-07-22 11:05:04 -070067 typedef SkBlitter INHERITED;
68};
69
70SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst,
71 const SkPaint& paint,
Mike Kleinfb191da2016-11-15 13:20:33 -050072 const SkMatrix& ctm,
mtklein9a5c47f2016-07-22 11:05:04 -070073 SkTBlitterAllocator* alloc) {
Mike Kleinfb191da2016-11-15 13:20:33 -050074 return SkRasterPipelineBlitter::Create(dst, paint, ctm, alloc);
mtklein9a5c47f2016-07-22 11:05:04 -070075}
76
mtklein8e4373f2016-07-22 14:20:27 -070077static bool supported(const SkImageInfo& info) {
mtklein8e4373f2016-07-22 14:20:27 -070078 switch (info.colorType()) {
Mike Kleine71b1672017-01-13 07:59:23 -050079 case kAlpha_8_SkColorType: return true;
80 case kRGB_565_SkColorType: return true;
mtklein4e90e3e2016-07-25 14:35:31 -070081 case kN32_SkColorType: return info.gammaCloseToSRGB();
82 case kRGBA_F16_SkColorType: return true;
mtklein4e90e3e2016-07-25 14:35:31 -070083 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,
Mike Kleinfb191da2016-11-15 13:20:33 -050089 const SkMatrix& ctm,
mtklein9a5c47f2016-07-22 11:05:04 -070090 SkTBlitterAllocator* alloc) {
Mike Klein744908e2016-11-11 12:51:36 -050091 auto blitter = alloc->createT<SkRasterPipelineBlitter>(
92 dst,
93 paint.getBlendMode(),
94 SkPM4f_from_SkColor(paint.getColor(), dst.colorSpace()));
95
Mike Kleinf98b7302016-11-06 10:18:06 -050096 auto earlyOut = [&] {
herbbf6d80a2016-11-15 06:26:56 -080097 alloc->deleteLast();
Mike Kleinf98b7302016-11-06 10:18:06 -050098 return nullptr;
99 };
100
Mike Klein21aa3d02016-11-30 16:40:56 -0500101 SkBlendMode* blend = &blitter->fBlend;
102 SkPM4f* paintColor = &blitter->fPaintColor;
103 SkRasterPipeline* pipeline = &blitter->fShader;
mtkleina4a44882016-11-04 13:20:07 -0700104
105 SkShader* shader = paint.getShader();
106 SkColorFilter* colorFilter = paint.getColorFilter();
107
108 // TODO: all temporary
Mike Kleinb0d10e02016-11-12 10:29:26 -0500109 if (!supported(dst.info()) || !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
Mike Klein14c8f822016-11-30 19:39:43 -0500113 bool is_opaque = paintColor->a() == 1.0f,
114 is_constant = true;
mtkleina4a44882016-11-04 13:20:07 -0700115 if (shader) {
Herb Derbyac04fef2017-01-13 17:34:33 -0500116 if (!shader->appendStages(pipeline, dst.colorSpace(), &blitter->fArena,
Mike Klein7a147342016-11-29 15:33:39 -0500117 ctm, paint)) {
Mike Kleinb0d10e02016-11-12 10:29:26 -0500118 return earlyOut();
119 }
Mike Klein43c847b2016-11-21 13:05:23 -0500120 if (!is_opaque) {
Mike Kleinbabd93e2016-11-30 16:05:10 -0500121 pipeline->append(SkRasterPipeline::scale_1_float,
Mike Klein43c847b2016-11-21 13:05:23 -0500122 &paintColor->fVec[SkPM4f::A]);
123 }
Mike Kleinfb191da2016-11-15 13:20:33 -0500124
125 is_opaque = is_opaque && shader->isOpaque();
Mike Klein14c8f822016-11-30 19:39:43 -0500126 is_constant = shader->isConstant();
Mike Klein43c847b2016-11-21 13:05:23 -0500127 } else {
128 pipeline->append(SkRasterPipeline::constant_color, paintColor);
Mike Kleine902f8d2016-10-26 15:32:26 -0400129 }
mtklein9a5c47f2016-07-22 11:05:04 -0700130
mtkleina4a44882016-11-04 13:20:07 -0700131 if (colorFilter) {
Herb Derbyac04fef2017-01-13 17:34:33 -0500132 if (!colorFilter->appendStages(pipeline, dst.colorSpace(), &blitter->fArena,
Mike Klein744908e2016-11-11 12:51:36 -0500133 is_opaque)) {
Mike Kleinf98b7302016-11-06 10:18:06 -0500134 return earlyOut();
Mike Klein66866172016-11-03 12:22:01 -0400135 }
mtkleina4a44882016-11-04 13:20:07 -0700136 is_opaque = is_opaque && (colorFilter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag);
mtklein9a5c47f2016-07-22 11:05:04 -0700137 }
138
Mike Klein14c8f822016-11-30 19:39:43 -0500139 if (is_constant) {
mtkleina4a44882016-11-04 13:20:07 -0700140 pipeline->append(SkRasterPipeline::store_f32, &paintColor);
Mike Kleinc789b612016-11-30 13:45:06 -0500141 pipeline->run(0,0, 1);
mtkleina4a44882016-11-04 13:20:07 -0700142
143 *pipeline = SkRasterPipeline();
144 pipeline->append(SkRasterPipeline::constant_color, paintColor);
145
146 is_opaque = paintColor->a() == 1.0f;
Mike Klein66866172016-11-03 12:22:01 -0400147 }
mtklein8e4373f2016-07-22 14:20:27 -0700148
mtkleina4a44882016-11-04 13:20:07 -0700149 if (is_opaque && *blend == SkBlendMode::kSrcOver) {
150 *blend = SkBlendMode::kSrc;
mtklein8e4373f2016-07-22 14:20:27 -0700151 }
152
Mike Klein14c8f822016-11-30 19:39:43 -0500153 if (is_constant && *blend == SkBlendMode::kSrc) {
154 uint64_t color; // Big enough for largest dst format, F16.
155 SkRasterPipeline p;
156 p.extend(*pipeline);
157 blitter->fDstPtr = &color;
158 blitter->append_store(&p);
159 p.run(0,0, 1);
160
161 switch (dst.shiftPerPixel()) {
162 case 1:
163 blitter->fBlitH = [blitter,color](size_t x, size_t, size_t n) {
164 sk_memset16((uint16_t*)blitter->fDstPtr + x, color, n);
165 };
166 break;
167 case 2:
168 blitter->fBlitH = [blitter,color](size_t x, size_t, size_t n) {
169 sk_memset32((uint32_t*)blitter->fDstPtr + x, color, n);
170 };
171 break;
172 case 3:
173 blitter->fBlitH = [blitter,color](size_t x, size_t, size_t n) {
174 sk_memset64((uint64_t*)blitter->fDstPtr + x, color, n);
175 };
176 break;
177 default: break;
178 }
179 }
180
mtklein9a5c47f2016-07-22 11:05:04 -0700181 return blitter;
182}
183
Mike Kleine902f8d2016-10-26 15:32:26 -0400184void SkRasterPipelineBlitter::append_load_d(SkRasterPipeline* p) const {
mtklein8e4373f2016-07-22 14:20:27 -0700185 SkASSERT(supported(fDst.info()));
186
Mike Klein8c8cb5b2017-01-06 10:21:56 -0500187 p->append(SkRasterPipeline::move_src_dst);
mtklein8e4373f2016-07-22 14:20:27 -0700188 switch (fDst.info().colorType()) {
Mike Kleine71b1672017-01-13 07:59:23 -0500189 case kAlpha_8_SkColorType: p->append(SkRasterPipeline::load_a8, &fDstPtr); break;
Mike Klein8c8cb5b2017-01-06 10:21:56 -0500190 case kRGB_565_SkColorType: p->append(SkRasterPipeline::load_565, &fDstPtr); break;
Mike Kleine03339a2016-11-28 13:24:27 -0500191 case kBGRA_8888_SkColorType:
Mike Klein8c8cb5b2017-01-06 10:21:56 -0500192 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::load_8888, &fDstPtr); break;
193 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::load_f16, &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 Klein8c8cb5b2017-01-06 10:21:56 -0500197 p->append(SkRasterPipeline::swap_rb);
Mike Kleine03339a2016-11-28 13:24:27 -0500198 }
199 if (fDst.info().gammaCloseToSRGB()) {
Mike Klein8c8cb5b2017-01-06 10:21:56 -0500200 p->append_from_srgb(fDst.info().alphaType());
Mike Kleine03339a2016-11-28 13:24:27 -0500201 }
Mike Klein8c8cb5b2017-01-06 10:21:56 -0500202 p->append(SkRasterPipeline::swap);
mtklein8e4373f2016-07-22 14:20:27 -0700203}
204
Mike Klein14c8f822016-11-30 19:39:43 -0500205void SkRasterPipelineBlitter::append_store(SkRasterPipeline* p) const {
Mike Kleine03339a2016-11-28 13:24:27 -0500206 if (fDst.info().gammaCloseToSRGB()) {
207 p->append(SkRasterPipeline::to_srgb);
208 }
209 if (fDst.info().colorType() == kBGRA_8888_SkColorType) {
210 p->append(SkRasterPipeline::swap_rb);
211 }
Mike Klein21aa3d02016-11-30 16:40:56 -0500212
Mike Klein21aa3d02016-11-30 16:40:56 -0500213 SkASSERT(supported(fDst.info()));
mtklein8e4373f2016-07-22 14:20:27 -0700214 switch (fDst.info().colorType()) {
Mike Kleine71b1672017-01-13 07:59:23 -0500215 case kAlpha_8_SkColorType: p->append(SkRasterPipeline::store_a8, &fDstPtr); break;
Mike Kleine03339a2016-11-28 13:24:27 -0500216 case kRGB_565_SkColorType: p->append(SkRasterPipeline::store_565, &fDstPtr); break;
217 case kBGRA_8888_SkColorType:
218 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::store_8888, &fDstPtr); break;
219 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::store_f16, &fDstPtr); break;
mtklein8e4373f2016-07-22 14:20:27 -0700220 default: break;
221 }
222}
223
Mike Kleine902f8d2016-10-26 15:32:26 -0400224void SkRasterPipelineBlitter::append_blend(SkRasterPipeline* p) const {
225 SkAssertResult(SkBlendMode_AppendStages(fBlend, p));
226}
Mike Kleine9f74b82016-10-25 13:31:21 -0400227
Mike Klein130863e2016-10-27 11:29:36 -0400228void SkRasterPipelineBlitter::maybe_clamp(SkRasterPipeline* p) const {
Mike Kleine03339a2016-11-28 13:24:27 -0500229 if (SkBlendMode_CanOverflow(fBlend)) {
230 p->append(SkRasterPipeline::clamp_a);
231 }
Mike Klein130863e2016-10-27 11:29:36 -0400232}
233
mtklein9a5c47f2016-07-22 11:05:04 -0700234void SkRasterPipelineBlitter::blitH(int x, int y, int w) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400235 if (!fBlitH) {
236 SkRasterPipeline p;
237 p.extend(fShader);
Mike Klein66866172016-11-03 12:22:01 -0400238 if (fBlend != SkBlendMode::kSrc) {
239 this->append_load_d(&p);
240 this->append_blend(&p);
241 this->maybe_clamp(&p);
242 }
Mike Kleine902f8d2016-10-26 15:32:26 -0400243 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400244 fBlitH = p.compile();
245 }
Mike Kleinbd3fe472016-10-25 15:43:46 -0400246 fDstPtr = fDst.writable_addr(0,y);
Mike Kleinaf49b192016-11-15 08:52:04 -0500247 fBlitH(x,y, w);
mtklein9a5c47f2016-07-22 11:05:04 -0700248}
249
250void SkRasterPipelineBlitter::blitAntiH(int x, int y, const SkAlpha aa[], const int16_t runs[]) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400251 if (!fBlitAntiH) {
252 SkRasterPipeline p;
253 p.extend(fShader);
Mike Klein66866172016-11-03 12:22:01 -0400254 if (fBlend == SkBlendMode::kSrcOver) {
Mike Kleinbabd93e2016-11-30 16:05:10 -0500255 p.append(SkRasterPipeline::scale_1_float, &fCurrentCoverage);
Mike Klein66866172016-11-03 12:22:01 -0400256 this->append_load_d(&p);
257 this->append_blend(&p);
258 } else {
259 this->append_load_d(&p);
260 this->append_blend(&p);
Mike Kleinbabd93e2016-11-30 16:05:10 -0500261 p.append(SkRasterPipeline::lerp_1_float, &fCurrentCoverage);
Mike Klein66866172016-11-03 12:22:01 -0400262 }
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 fBlitAntiH = p.compile();
266 }
mtklein9a5c47f2016-07-22 11:05:04 -0700267
Mike Kleinbd3fe472016-10-25 15:43:46 -0400268 fDstPtr = fDst.writable_addr(0,y);
mtklein9a5c47f2016-07-22 11:05:04 -0700269 for (int16_t run = *runs; run > 0; run = *runs) {
Mike Kleinaeb79592016-11-07 09:29:07 -0500270 switch (*aa) {
271 case 0x00: break;
272 case 0xff: this->blitH(x,y,run); break;
273 default:
Mike Kleinbabd93e2016-11-30 16:05:10 -0500274 fCurrentCoverage = *aa * (1/255.0f);
Mike Kleinaf49b192016-11-15 08:52:04 -0500275 fBlitAntiH(x,y, run);
Mike Kleinaeb79592016-11-07 09:29:07 -0500276 }
mtklein9a5c47f2016-07-22 11:05:04 -0700277 x += run;
278 runs += run;
279 aa += run;
280 }
281}
282
283void SkRasterPipelineBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
284 if (mask.fFormat == SkMask::kBW_Format) {
285 // TODO: native BW masks?
286 return INHERITED::blitMask(mask, clip);
287 }
288
Mike Kleinbd3fe472016-10-25 15:43:46 -0400289 if (mask.fFormat == SkMask::kA8_Format && !fBlitMaskA8) {
mtklein9a5c47f2016-07-22 11:05:04 -0700290 SkRasterPipeline p;
291 p.extend(fShader);
Mike Klein66866172016-11-03 12:22:01 -0400292 if (fBlend == SkBlendMode::kSrcOver) {
293 p.append(SkRasterPipeline::scale_u8, &fMaskPtr);
294 this->append_load_d(&p);
295 this->append_blend(&p);
296 } else {
297 this->append_load_d(&p);
298 this->append_blend(&p);
299 p.append(SkRasterPipeline::lerp_u8, &fMaskPtr);
300 }
Mike Klein130863e2016-10-27 11:29:36 -0400301 this->maybe_clamp(&p);
Mike Kleine902f8d2016-10-26 15:32:26 -0400302 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400303 fBlitMaskA8 = p.compile();
304 }
305
306 if (mask.fFormat == SkMask::kLCD16_Format && !fBlitMaskLCD16) {
307 SkRasterPipeline p;
308 p.extend(fShader);
Mike Kleine902f8d2016-10-26 15:32:26 -0400309 this->append_load_d(&p);
310 this->append_blend(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400311 p.append(SkRasterPipeline::lerp_565, &fMaskPtr);
Mike Klein130863e2016-10-27 11:29:36 -0400312 this->maybe_clamp(&p);
Mike Kleine902f8d2016-10-26 15:32:26 -0400313 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400314 fBlitMaskLCD16 = p.compile();
315 }
316
317 int x = clip.left();
318 for (int y = clip.top(); y < clip.bottom(); y++) {
319 fDstPtr = fDst.writable_addr(0,y);
320
mtklein9a5c47f2016-07-22 11:05:04 -0700321 switch (mask.fFormat) {
322 case SkMask::kA8_Format:
Mike Kleinbd3fe472016-10-25 15:43:46 -0400323 fMaskPtr = mask.getAddr8(x,y)-x;
Mike Kleinaf49b192016-11-15 08:52:04 -0500324 fBlitMaskA8(x,y, clip.width());
mtklein9a5c47f2016-07-22 11:05:04 -0700325 break;
326 case SkMask::kLCD16_Format:
Mike Kleinbd3fe472016-10-25 15:43:46 -0400327 fMaskPtr = mask.getAddrLCD16(x,y)-x;
Mike Kleinaf49b192016-11-15 08:52:04 -0500328 fBlitMaskLCD16(x,y, clip.width());
mtklein9a5c47f2016-07-22 11:05:04 -0700329 break;
Mike Kleinbd3fe472016-10-25 15:43:46 -0400330 default:
331 // TODO
332 break;
mtklein9a5c47f2016-07-22 11:05:04 -0700333 }
mtklein9a5c47f2016-07-22 11:05:04 -0700334 }
335}