blob: 197233c3cd52f9e11c47455214a0075cc52348c7 [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,
Matt Sarett25834ff2017-02-15 15:54:35 -050024 SkArenaAlloc*);
mtklein9a5c47f2016-07-22 11:05:04 -070025
Mike Klein688ded22017-02-07 11:57:27 -050026 SkRasterPipelineBlitter(SkPixmap dst, SkBlendMode blend, SkPM4f paintColor, bool blendCorrectly)
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 Klein688ded22017-02-07 11:57:27 -050030 , fBlendCorrectly(blendCorrectly)
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 Klein14c8f822016-11-30 19:39:43 -050042 void append_load_d(SkRasterPipeline*) const;
43 void append_blend (SkRasterPipeline*) const;
44 void maybe_clamp (SkRasterPipeline*) const;
45 void append_store (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;
Mike Klein688ded22017-02-07 11:57:27 -050051 bool fBlendCorrectly;
mtklein9a5c47f2016-07-22 11:05:04 -070052
Mike Klein8729e5b2017-02-16 06:51:48 -050053 // We may be able to specialize blitH() into a memset.
54 bool fCanMemsetInBlitH = false;
55 uint64_t fMemsetColor = 0; // Big enough for largest dst format, F16.
Mike Kleinbd3fe472016-10-25 15:43:46 -040056
Mike Klein8729e5b2017-02-16 06:51:48 -050057 // Built lazily on first use.
58 SkRasterPipeline fBlitH,
59 fBlitAntiH,
60 fBlitMaskA8,
61 fBlitMaskLCD16;
62
63 // These values are pointed to by the blit pipelines above,
64 // which allows us to adjust them from call to call.
Mike Kleinbabd93e2016-11-30 16:05:10 -050065 void* fDstPtr = nullptr;
66 const void* fMaskPtr = nullptr;
67 float fCurrentCoverage = 0.0f;
Mike Klein319ba3d2017-01-20 15:11:54 -050068 int fCurrentY = 0;
Mike Kleinbd3fe472016-10-25 15:43:46 -040069
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,
Matt Sarett25834ff2017-02-15 15:54:35 -050076 SkArenaAlloc* alloc) {
77 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()) {
Mike Kleine71b1672017-01-13 07:59:23 -050082 case kAlpha_8_SkColorType: return true;
83 case kRGB_565_SkColorType: return true;
mtklein4e90e3e2016-07-25 14:35:31 -070084 case kN32_SkColorType: return info.gammaCloseToSRGB();
85 case kRGBA_F16_SkColorType: return true;
mtklein4e90e3e2016-07-25 14:35:31 -070086 default: return false;
mtklein8e4373f2016-07-22 14:20:27 -070087 }
88}
mtklein9a5c47f2016-07-22 11:05:04 -070089
mtklein9a5c47f2016-07-22 11:05:04 -070090SkBlitter* SkRasterPipelineBlitter::Create(const SkPixmap& dst,
91 const SkPaint& paint,
Mike Kleinfb191da2016-11-15 13:20:33 -050092 const SkMatrix& ctm,
Matt Sarett25834ff2017-02-15 15:54:35 -050093 SkArenaAlloc* alloc) {
94 bool blendCorrectly = !(dst.colorSpace() && as_CSB(dst.colorSpace())->nonLinearBlending());
Herb Derby83e939b2017-02-07 14:25:11 -050095 auto blitter = alloc->make<SkRasterPipelineBlitter>(
Mike Klein744908e2016-11-11 12:51:36 -050096 dst,
97 paint.getBlendMode(),
Mike Klein688ded22017-02-07 11:57:27 -050098 SkPM4f_from_SkColor(paint.getColor(), dst.colorSpace()),
99 blendCorrectly);
Mike Klein744908e2016-11-11 12:51:36 -0500100
Mike Kleinf98b7302016-11-06 10:18:06 -0500101
Mike Klein21aa3d02016-11-30 16:40:56 -0500102 SkBlendMode* blend = &blitter->fBlend;
103 SkPM4f* paintColor = &blitter->fPaintColor;
104 SkRasterPipeline* pipeline = &blitter->fShader;
mtkleina4a44882016-11-04 13:20:07 -0700105
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)) {
Herb Derby83e939b2017-02-07 14:25:11 -0500111 return nullptr;
mtklein9a5c47f2016-07-22 11:05:04 -0700112 }
mtkleina4a44882016-11-04 13:20:07 -0700113
Mike Klein14c8f822016-11-30 19:39:43 -0500114 bool is_opaque = paintColor->a() == 1.0f,
115 is_constant = true;
mtkleina4a44882016-11-04 13:20:07 -0700116 if (shader) {
Mike Klein319ba3d2017-01-20 15:11:54 -0500117 pipeline->append(SkRasterPipeline::seed_shader, &blitter->fCurrentY);
Mike Klein3a846152017-02-07 12:05:10 -0500118 if (!shader->appendStages(pipeline, dst.colorSpace(), alloc, ctm, paint)) {
Herb Derby83e939b2017-02-07 14:25:11 -0500119 return nullptr;
Mike Kleinb0d10e02016-11-12 10:29:26 -0500120 }
Mike Klein43c847b2016-11-21 13:05:23 -0500121 if (!is_opaque) {
Mike Kleinbabd93e2016-11-30 16:05:10 -0500122 pipeline->append(SkRasterPipeline::scale_1_float,
Mike Klein43c847b2016-11-21 13:05:23 -0500123 &paintColor->fVec[SkPM4f::A]);
124 }
Mike Kleinfb191da2016-11-15 13:20:33 -0500125
126 is_opaque = is_opaque && shader->isOpaque();
Mike Klein14c8f822016-11-30 19:39:43 -0500127 is_constant = shader->isConstant();
Mike Klein43c847b2016-11-21 13:05:23 -0500128 } else {
129 pipeline->append(SkRasterPipeline::constant_color, paintColor);
Mike Kleine902f8d2016-10-26 15:32:26 -0400130 }
mtklein9a5c47f2016-07-22 11:05:04 -0700131
Mike Klein688ded22017-02-07 11:57:27 -0500132 // Some people want the rest of the pipeline to operate on sRGB encoded color channels...
133 if (!blendCorrectly && dst.info().gammaCloseToSRGB()) {
134 pipeline->append(SkRasterPipeline::to_srgb);
135 }
136
mtkleina4a44882016-11-04 13:20:07 -0700137 if (colorFilter) {
Mike Klein3a846152017-02-07 12:05:10 -0500138 if (!colorFilter->appendStages(pipeline, dst.colorSpace(), alloc, is_opaque)) {
Herb Derby83e939b2017-02-07 14:25:11 -0500139 return nullptr;
Mike Klein66866172016-11-03 12:22:01 -0400140 }
mtkleina4a44882016-11-04 13:20:07 -0700141 is_opaque = is_opaque && (colorFilter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag);
mtklein9a5c47f2016-07-22 11:05:04 -0700142 }
143
Mike Klein14c8f822016-11-30 19:39:43 -0500144 if (is_constant) {
mtkleina4a44882016-11-04 13:20:07 -0700145 pipeline->append(SkRasterPipeline::store_f32, &paintColor);
Mike Klein319ba3d2017-01-20 15:11:54 -0500146 pipeline->run(0,1);
mtkleina4a44882016-11-04 13:20:07 -0700147
148 *pipeline = SkRasterPipeline();
149 pipeline->append(SkRasterPipeline::constant_color, paintColor);
150
151 is_opaque = paintColor->a() == 1.0f;
Mike Klein66866172016-11-03 12:22:01 -0400152 }
mtklein8e4373f2016-07-22 14:20:27 -0700153
mtkleina4a44882016-11-04 13:20:07 -0700154 if (is_opaque && *blend == SkBlendMode::kSrcOver) {
155 *blend = SkBlendMode::kSrc;
mtklein8e4373f2016-07-22 14:20:27 -0700156 }
157
Mike Klein14c8f822016-11-30 19:39:43 -0500158 if (is_constant && *blend == SkBlendMode::kSrc) {
Mike Klein14c8f822016-11-30 19:39:43 -0500159 SkRasterPipeline p;
160 p.extend(*pipeline);
Mike Klein8729e5b2017-02-16 06:51:48 -0500161 blitter->fDstPtr = &blitter->fMemsetColor;
Mike Klein14c8f822016-11-30 19:39:43 -0500162 blitter->append_store(&p);
Mike Klein319ba3d2017-01-20 15:11:54 -0500163 p.run(0,1);
Mike Klein14c8f822016-11-30 19:39:43 -0500164
Mike Klein8729e5b2017-02-16 06:51:48 -0500165 blitter->fCanMemsetInBlitH = true;
Mike Klein14c8f822016-11-30 19:39:43 -0500166 }
167
mtklein9a5c47f2016-07-22 11:05:04 -0700168 return blitter;
169}
170
Mike Kleine902f8d2016-10-26 15:32:26 -0400171void SkRasterPipelineBlitter::append_load_d(SkRasterPipeline* p) const {
mtklein8e4373f2016-07-22 14:20:27 -0700172 SkASSERT(supported(fDst.info()));
173
Mike Klein8c8cb5b2017-01-06 10:21:56 -0500174 p->append(SkRasterPipeline::move_src_dst);
mtklein8e4373f2016-07-22 14:20:27 -0700175 switch (fDst.info().colorType()) {
Mike Kleine71b1672017-01-13 07:59:23 -0500176 case kAlpha_8_SkColorType: p->append(SkRasterPipeline::load_a8, &fDstPtr); break;
Mike Klein8c8cb5b2017-01-06 10:21:56 -0500177 case kRGB_565_SkColorType: p->append(SkRasterPipeline::load_565, &fDstPtr); break;
Mike Kleine03339a2016-11-28 13:24:27 -0500178 case kBGRA_8888_SkColorType:
Mike Klein8c8cb5b2017-01-06 10:21:56 -0500179 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::load_8888, &fDstPtr); break;
180 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::load_f16, &fDstPtr); break;
mtklein8e4373f2016-07-22 14:20:27 -0700181 default: break;
182 }
Mike Kleine03339a2016-11-28 13:24:27 -0500183 if (fDst.info().colorType() == kBGRA_8888_SkColorType) {
Mike Klein8c8cb5b2017-01-06 10:21:56 -0500184 p->append(SkRasterPipeline::swap_rb);
Mike Kleine03339a2016-11-28 13:24:27 -0500185 }
186 if (fDst.info().gammaCloseToSRGB()) {
Mike Klein8c8cb5b2017-01-06 10:21:56 -0500187 p->append_from_srgb(fDst.info().alphaType());
Mike Kleine03339a2016-11-28 13:24:27 -0500188 }
Mike Klein8c8cb5b2017-01-06 10:21:56 -0500189 p->append(SkRasterPipeline::swap);
mtklein8e4373f2016-07-22 14:20:27 -0700190}
191
Mike Klein14c8f822016-11-30 19:39:43 -0500192void SkRasterPipelineBlitter::append_store(SkRasterPipeline* p) const {
Mike Klein688ded22017-02-07 11:57:27 -0500193 if (fBlendCorrectly && fDst.info().gammaCloseToSRGB()) {
Mike Kleine03339a2016-11-28 13:24:27 -0500194 p->append(SkRasterPipeline::to_srgb);
195 }
196 if (fDst.info().colorType() == kBGRA_8888_SkColorType) {
197 p->append(SkRasterPipeline::swap_rb);
198 }
Mike Klein21aa3d02016-11-30 16:40:56 -0500199
Mike Klein21aa3d02016-11-30 16:40:56 -0500200 SkASSERT(supported(fDst.info()));
mtklein8e4373f2016-07-22 14:20:27 -0700201 switch (fDst.info().colorType()) {
Mike Kleine71b1672017-01-13 07:59:23 -0500202 case kAlpha_8_SkColorType: p->append(SkRasterPipeline::store_a8, &fDstPtr); break;
Mike Kleine03339a2016-11-28 13:24:27 -0500203 case kRGB_565_SkColorType: p->append(SkRasterPipeline::store_565, &fDstPtr); break;
204 case kBGRA_8888_SkColorType:
205 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::store_8888, &fDstPtr); break;
206 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::store_f16, &fDstPtr); break;
mtklein8e4373f2016-07-22 14:20:27 -0700207 default: break;
208 }
209}
210
Mike Kleine902f8d2016-10-26 15:32:26 -0400211void SkRasterPipelineBlitter::append_blend(SkRasterPipeline* p) const {
212 SkAssertResult(SkBlendMode_AppendStages(fBlend, p));
213}
Mike Kleine9f74b82016-10-25 13:31:21 -0400214
Mike Klein130863e2016-10-27 11:29:36 -0400215void SkRasterPipelineBlitter::maybe_clamp(SkRasterPipeline* p) const {
Mike Kleine03339a2016-11-28 13:24:27 -0500216 if (SkBlendMode_CanOverflow(fBlend)) {
217 p->append(SkRasterPipeline::clamp_a);
218 }
Mike Klein130863e2016-10-27 11:29:36 -0400219}
220
mtklein9a5c47f2016-07-22 11:05:04 -0700221void SkRasterPipelineBlitter::blitH(int x, int y, int w) {
Mike Klein8729e5b2017-02-16 06:51:48 -0500222 fDstPtr = fDst.writable_addr(0,y);
223 fCurrentY = y;
224
225 if (fCanMemsetInBlitH) {
226 switch (fDst.shiftPerPixel()) {
227 // TODO: case 0: memset (for A8)
228 case 1:
229 sk_memset16((uint16_t*)fDstPtr + x, fMemsetColor, w);
230 return;
231 case 2:
232 sk_memset32((uint32_t*)fDstPtr + x, fMemsetColor, w);
233 return;
234 case 3:
235 sk_memset64((uint64_t*)fDstPtr + x, fMemsetColor, w);
236 return;
237 default: break;
238 }
239 }
240
241 auto& p = fBlitH;
242 if (p.empty()) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400243 p.extend(fShader);
Mike Klein66866172016-11-03 12:22:01 -0400244 if (fBlend != SkBlendMode::kSrc) {
245 this->append_load_d(&p);
246 this->append_blend(&p);
247 this->maybe_clamp(&p);
248 }
Mike Kleine902f8d2016-10-26 15:32:26 -0400249 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400250 }
Mike Klein8729e5b2017-02-16 06:51:48 -0500251 p.run(x,w);
mtklein9a5c47f2016-07-22 11:05:04 -0700252}
253
254void SkRasterPipelineBlitter::blitAntiH(int x, int y, const SkAlpha aa[], const int16_t runs[]) {
Mike Klein8729e5b2017-02-16 06:51:48 -0500255 auto& p = fBlitAntiH;
256 if (p.empty()) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400257 p.extend(fShader);
Mike Klein66866172016-11-03 12:22:01 -0400258 if (fBlend == SkBlendMode::kSrcOver) {
Mike Kleinbabd93e2016-11-30 16:05:10 -0500259 p.append(SkRasterPipeline::scale_1_float, &fCurrentCoverage);
Mike Klein66866172016-11-03 12:22:01 -0400260 this->append_load_d(&p);
261 this->append_blend(&p);
262 } else {
263 this->append_load_d(&p);
264 this->append_blend(&p);
Mike Kleinbabd93e2016-11-30 16:05:10 -0500265 p.append(SkRasterPipeline::lerp_1_float, &fCurrentCoverage);
Mike Klein66866172016-11-03 12:22:01 -0400266 }
Mike Klein130863e2016-10-27 11:29:36 -0400267 this->maybe_clamp(&p);
Mike Kleine902f8d2016-10-26 15:32:26 -0400268 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400269 }
mtklein9a5c47f2016-07-22 11:05:04 -0700270
Mike Kleinbd3fe472016-10-25 15:43:46 -0400271 fDstPtr = fDst.writable_addr(0,y);
Mike Klein319ba3d2017-01-20 15:11:54 -0500272 fCurrentY = y;
mtklein9a5c47f2016-07-22 11:05:04 -0700273 for (int16_t run = *runs; run > 0; run = *runs) {
Mike Kleinaeb79592016-11-07 09:29:07 -0500274 switch (*aa) {
275 case 0x00: break;
276 case 0xff: this->blitH(x,y,run); break;
277 default:
Mike Kleinbabd93e2016-11-30 16:05:10 -0500278 fCurrentCoverage = *aa * (1/255.0f);
Mike Klein8729e5b2017-02-16 06:51:48 -0500279 p.run(x,run);
Mike Kleinaeb79592016-11-07 09:29:07 -0500280 }
mtklein9a5c47f2016-07-22 11:05:04 -0700281 x += run;
282 runs += run;
283 aa += run;
284 }
285}
286
287void SkRasterPipelineBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
288 if (mask.fFormat == SkMask::kBW_Format) {
289 // TODO: native BW masks?
290 return INHERITED::blitMask(mask, clip);
291 }
292
Mike Klein8729e5b2017-02-16 06:51:48 -0500293 if (mask.fFormat == SkMask::kA8_Format && fBlitMaskA8.empty()) {
294 auto& p = fBlitMaskA8;
mtklein9a5c47f2016-07-22 11:05:04 -0700295 p.extend(fShader);
Mike Klein66866172016-11-03 12:22:01 -0400296 if (fBlend == SkBlendMode::kSrcOver) {
297 p.append(SkRasterPipeline::scale_u8, &fMaskPtr);
298 this->append_load_d(&p);
299 this->append_blend(&p);
300 } else {
301 this->append_load_d(&p);
302 this->append_blend(&p);
303 p.append(SkRasterPipeline::lerp_u8, &fMaskPtr);
304 }
Mike Klein130863e2016-10-27 11:29:36 -0400305 this->maybe_clamp(&p);
Mike Kleine902f8d2016-10-26 15:32:26 -0400306 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400307 }
308
Mike Klein8729e5b2017-02-16 06:51:48 -0500309 if (mask.fFormat == SkMask::kLCD16_Format && fBlitMaskLCD16.empty()) {
310 auto& p = fBlitMaskLCD16;
Mike Kleinbd3fe472016-10-25 15:43:46 -0400311 p.extend(fShader);
Mike Kleine902f8d2016-10-26 15:32:26 -0400312 this->append_load_d(&p);
313 this->append_blend(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400314 p.append(SkRasterPipeline::lerp_565, &fMaskPtr);
Mike Klein130863e2016-10-27 11:29:36 -0400315 this->maybe_clamp(&p);
Mike Kleine902f8d2016-10-26 15:32:26 -0400316 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400317 }
318
319 int x = clip.left();
320 for (int y = clip.top(); y < clip.bottom(); y++) {
321 fDstPtr = fDst.writable_addr(0,y);
Mike Klein319ba3d2017-01-20 15:11:54 -0500322 fCurrentY = y;
Mike Kleinbd3fe472016-10-25 15:43:46 -0400323
mtklein9a5c47f2016-07-22 11:05:04 -0700324 switch (mask.fFormat) {
325 case SkMask::kA8_Format:
Mike Kleinbd3fe472016-10-25 15:43:46 -0400326 fMaskPtr = mask.getAddr8(x,y)-x;
Mike Klein8729e5b2017-02-16 06:51:48 -0500327 fBlitMaskA8.run(x,clip.width());
mtklein9a5c47f2016-07-22 11:05:04 -0700328 break;
329 case SkMask::kLCD16_Format:
Mike Kleinbd3fe472016-10-25 15:43:46 -0400330 fMaskPtr = mask.getAddrLCD16(x,y)-x;
Mike Klein8729e5b2017-02-16 06:51:48 -0500331 fBlitMaskLCD16.run(x,clip.width());
mtklein9a5c47f2016-07-22 11:05:04 -0700332 break;
Mike Kleinbd3fe472016-10-25 15:43:46 -0400333 default:
334 // TODO
335 break;
mtklein9a5c47f2016-07-22 11:05:04 -0700336 }
mtklein9a5c47f2016-07-22 11:05:04 -0700337 }
338}