blob: a994de3ed9e5cfb5953f8fdfd638978498023445 [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 Kleinbd3fe472016-10-25 15:43:46 -040053 // These functions are compiled lazily when first used.
Mike Klein319ba3d2017-01-20 15:11:54 -050054 std::function<void(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 Klein319ba3d2017-01-20 15:11:54 -050064 int fCurrentY = 0;
Mike Kleinbd3fe472016-10-25 15:43:46 -040065
mtklein9a5c47f2016-07-22 11:05:04 -070066 typedef SkBlitter INHERITED;
67};
68
69SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst,
70 const SkPaint& paint,
Mike Kleinfb191da2016-11-15 13:20:33 -050071 const SkMatrix& ctm,
Matt Sarett25834ff2017-02-15 15:54:35 -050072 SkArenaAlloc* alloc) {
73 return SkRasterPipelineBlitter::Create(dst, paint, ctm, alloc);
mtklein9a5c47f2016-07-22 11:05:04 -070074}
75
mtklein8e4373f2016-07-22 14:20:27 -070076static bool supported(const SkImageInfo& info) {
mtklein8e4373f2016-07-22 14:20:27 -070077 switch (info.colorType()) {
Mike Kleine71b1672017-01-13 07:59:23 -050078 case kAlpha_8_SkColorType: return true;
79 case kRGB_565_SkColorType: return true;
mtklein4e90e3e2016-07-25 14:35:31 -070080 case kN32_SkColorType: return info.gammaCloseToSRGB();
81 case kRGBA_F16_SkColorType: return true;
mtklein4e90e3e2016-07-25 14:35:31 -070082 default: return false;
mtklein8e4373f2016-07-22 14:20:27 -070083 }
84}
mtklein9a5c47f2016-07-22 11:05:04 -070085
mtklein9a5c47f2016-07-22 11:05:04 -070086SkBlitter* SkRasterPipelineBlitter::Create(const SkPixmap& dst,
87 const SkPaint& paint,
Mike Kleinfb191da2016-11-15 13:20:33 -050088 const SkMatrix& ctm,
Matt Sarett25834ff2017-02-15 15:54:35 -050089 SkArenaAlloc* alloc) {
90 bool blendCorrectly = !(dst.colorSpace() && as_CSB(dst.colorSpace())->nonLinearBlending());
Herb Derby83e939b2017-02-07 14:25:11 -050091 auto blitter = alloc->make<SkRasterPipelineBlitter>(
Mike Klein744908e2016-11-11 12:51:36 -050092 dst,
93 paint.getBlendMode(),
Mike Klein688ded22017-02-07 11:57:27 -050094 SkPM4f_from_SkColor(paint.getColor(), dst.colorSpace()),
95 blendCorrectly);
Mike Klein744908e2016-11-11 12:51:36 -050096
Mike Kleinf98b7302016-11-06 10:18:06 -050097
Mike Klein21aa3d02016-11-30 16:40:56 -050098 SkBlendMode* blend = &blitter->fBlend;
99 SkPM4f* paintColor = &blitter->fPaintColor;
100 SkRasterPipeline* pipeline = &blitter->fShader;
mtkleina4a44882016-11-04 13:20:07 -0700101
102 SkShader* shader = paint.getShader();
103 SkColorFilter* colorFilter = paint.getColorFilter();
104
105 // TODO: all temporary
Mike Kleinb0d10e02016-11-12 10:29:26 -0500106 if (!supported(dst.info()) || !SkBlendMode_AppendStages(*blend)) {
Herb Derby83e939b2017-02-07 14:25:11 -0500107 return nullptr;
mtklein9a5c47f2016-07-22 11:05:04 -0700108 }
mtkleina4a44882016-11-04 13:20:07 -0700109
Mike Klein14c8f822016-11-30 19:39:43 -0500110 bool is_opaque = paintColor->a() == 1.0f,
111 is_constant = true;
mtkleina4a44882016-11-04 13:20:07 -0700112 if (shader) {
Mike Klein319ba3d2017-01-20 15:11:54 -0500113 pipeline->append(SkRasterPipeline::seed_shader, &blitter->fCurrentY);
Mike Klein3a846152017-02-07 12:05:10 -0500114 if (!shader->appendStages(pipeline, dst.colorSpace(), alloc, ctm, paint)) {
Herb Derby83e939b2017-02-07 14:25:11 -0500115 return nullptr;
Mike Kleinb0d10e02016-11-12 10:29:26 -0500116 }
Mike Klein43c847b2016-11-21 13:05:23 -0500117 if (!is_opaque) {
Mike Kleinbabd93e2016-11-30 16:05:10 -0500118 pipeline->append(SkRasterPipeline::scale_1_float,
Mike Klein43c847b2016-11-21 13:05:23 -0500119 &paintColor->fVec[SkPM4f::A]);
120 }
Mike Kleinfb191da2016-11-15 13:20:33 -0500121
122 is_opaque = is_opaque && shader->isOpaque();
Mike Klein14c8f822016-11-30 19:39:43 -0500123 is_constant = shader->isConstant();
Mike Klein43c847b2016-11-21 13:05:23 -0500124 } else {
125 pipeline->append(SkRasterPipeline::constant_color, paintColor);
Mike Kleine902f8d2016-10-26 15:32:26 -0400126 }
mtklein9a5c47f2016-07-22 11:05:04 -0700127
Mike Klein688ded22017-02-07 11:57:27 -0500128 // Some people want the rest of the pipeline to operate on sRGB encoded color channels...
129 if (!blendCorrectly && dst.info().gammaCloseToSRGB()) {
130 pipeline->append(SkRasterPipeline::to_srgb);
131 }
132
mtkleina4a44882016-11-04 13:20:07 -0700133 if (colorFilter) {
Mike Klein3a846152017-02-07 12:05:10 -0500134 if (!colorFilter->appendStages(pipeline, dst.colorSpace(), alloc, is_opaque)) {
Herb Derby83e939b2017-02-07 14:25:11 -0500135 return nullptr;
Mike Klein66866172016-11-03 12:22:01 -0400136 }
mtkleina4a44882016-11-04 13:20:07 -0700137 is_opaque = is_opaque && (colorFilter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag);
mtklein9a5c47f2016-07-22 11:05:04 -0700138 }
139
Mike Klein14c8f822016-11-30 19:39:43 -0500140 if (is_constant) {
mtkleina4a44882016-11-04 13:20:07 -0700141 pipeline->append(SkRasterPipeline::store_f32, &paintColor);
Mike Klein319ba3d2017-01-20 15:11:54 -0500142 pipeline->run(0,1);
mtkleina4a44882016-11-04 13:20:07 -0700143
144 *pipeline = SkRasterPipeline();
145 pipeline->append(SkRasterPipeline::constant_color, paintColor);
146
147 is_opaque = paintColor->a() == 1.0f;
Mike Klein66866172016-11-03 12:22:01 -0400148 }
mtklein8e4373f2016-07-22 14:20:27 -0700149
mtkleina4a44882016-11-04 13:20:07 -0700150 if (is_opaque && *blend == SkBlendMode::kSrcOver) {
151 *blend = SkBlendMode::kSrc;
mtklein8e4373f2016-07-22 14:20:27 -0700152 }
153
Mike Klein14c8f822016-11-30 19:39:43 -0500154 if (is_constant && *blend == SkBlendMode::kSrc) {
155 uint64_t color; // Big enough for largest dst format, F16.
156 SkRasterPipeline p;
157 p.extend(*pipeline);
158 blitter->fDstPtr = &color;
159 blitter->append_store(&p);
Mike Klein319ba3d2017-01-20 15:11:54 -0500160 p.run(0,1);
Mike Klein14c8f822016-11-30 19:39:43 -0500161
162 switch (dst.shiftPerPixel()) {
163 case 1:
Mike Klein319ba3d2017-01-20 15:11:54 -0500164 blitter->fBlitH = [blitter,color](size_t x, size_t n) {
Mike Klein14c8f822016-11-30 19:39:43 -0500165 sk_memset16((uint16_t*)blitter->fDstPtr + x, color, n);
166 };
167 break;
168 case 2:
Mike Klein319ba3d2017-01-20 15:11:54 -0500169 blitter->fBlitH = [blitter,color](size_t x, size_t n) {
Mike Klein14c8f822016-11-30 19:39:43 -0500170 sk_memset32((uint32_t*)blitter->fDstPtr + x, color, n);
171 };
172 break;
173 case 3:
Mike Klein319ba3d2017-01-20 15:11:54 -0500174 blitter->fBlitH = [blitter,color](size_t x, size_t n) {
Mike Klein14c8f822016-11-30 19:39:43 -0500175 sk_memset64((uint64_t*)blitter->fDstPtr + x, color, n);
176 };
177 break;
178 default: break;
179 }
180 }
181
mtklein9a5c47f2016-07-22 11:05:04 -0700182 return blitter;
183}
184
Mike Kleine902f8d2016-10-26 15:32:26 -0400185void SkRasterPipelineBlitter::append_load_d(SkRasterPipeline* p) const {
mtklein8e4373f2016-07-22 14:20:27 -0700186 SkASSERT(supported(fDst.info()));
187
Mike Klein8c8cb5b2017-01-06 10:21:56 -0500188 p->append(SkRasterPipeline::move_src_dst);
mtklein8e4373f2016-07-22 14:20:27 -0700189 switch (fDst.info().colorType()) {
Mike Kleine71b1672017-01-13 07:59:23 -0500190 case kAlpha_8_SkColorType: p->append(SkRasterPipeline::load_a8, &fDstPtr); break;
Mike Klein8c8cb5b2017-01-06 10:21:56 -0500191 case kRGB_565_SkColorType: p->append(SkRasterPipeline::load_565, &fDstPtr); break;
Mike Kleine03339a2016-11-28 13:24:27 -0500192 case kBGRA_8888_SkColorType:
Mike Klein8c8cb5b2017-01-06 10:21:56 -0500193 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::load_8888, &fDstPtr); break;
194 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::load_f16, &fDstPtr); break;
mtklein8e4373f2016-07-22 14:20:27 -0700195 default: break;
196 }
Mike Kleine03339a2016-11-28 13:24:27 -0500197 if (fDst.info().colorType() == kBGRA_8888_SkColorType) {
Mike Klein8c8cb5b2017-01-06 10:21:56 -0500198 p->append(SkRasterPipeline::swap_rb);
Mike Kleine03339a2016-11-28 13:24:27 -0500199 }
200 if (fDst.info().gammaCloseToSRGB()) {
Mike Klein8c8cb5b2017-01-06 10:21:56 -0500201 p->append_from_srgb(fDst.info().alphaType());
Mike Kleine03339a2016-11-28 13:24:27 -0500202 }
Mike Klein8c8cb5b2017-01-06 10:21:56 -0500203 p->append(SkRasterPipeline::swap);
mtklein8e4373f2016-07-22 14:20:27 -0700204}
205
Mike Klein14c8f822016-11-30 19:39:43 -0500206void SkRasterPipelineBlitter::append_store(SkRasterPipeline* p) const {
Mike Klein688ded22017-02-07 11:57:27 -0500207 if (fBlendCorrectly && fDst.info().gammaCloseToSRGB()) {
Mike Kleine03339a2016-11-28 13:24:27 -0500208 p->append(SkRasterPipeline::to_srgb);
209 }
210 if (fDst.info().colorType() == kBGRA_8888_SkColorType) {
211 p->append(SkRasterPipeline::swap_rb);
212 }
Mike Klein21aa3d02016-11-30 16:40:56 -0500213
Mike Klein21aa3d02016-11-30 16:40:56 -0500214 SkASSERT(supported(fDst.info()));
mtklein8e4373f2016-07-22 14:20:27 -0700215 switch (fDst.info().colorType()) {
Mike Kleine71b1672017-01-13 07:59:23 -0500216 case kAlpha_8_SkColorType: p->append(SkRasterPipeline::store_a8, &fDstPtr); break;
Mike Kleine03339a2016-11-28 13:24:27 -0500217 case kRGB_565_SkColorType: p->append(SkRasterPipeline::store_565, &fDstPtr); break;
218 case kBGRA_8888_SkColorType:
219 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::store_8888, &fDstPtr); break;
220 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::store_f16, &fDstPtr); break;
mtklein8e4373f2016-07-22 14:20:27 -0700221 default: break;
222 }
223}
224
Mike Kleine902f8d2016-10-26 15:32:26 -0400225void SkRasterPipelineBlitter::append_blend(SkRasterPipeline* p) const {
226 SkAssertResult(SkBlendMode_AppendStages(fBlend, p));
227}
Mike Kleine9f74b82016-10-25 13:31:21 -0400228
Mike Klein130863e2016-10-27 11:29:36 -0400229void SkRasterPipelineBlitter::maybe_clamp(SkRasterPipeline* p) const {
Mike Kleine03339a2016-11-28 13:24:27 -0500230 if (SkBlendMode_CanOverflow(fBlend)) {
231 p->append(SkRasterPipeline::clamp_a);
232 }
Mike Klein130863e2016-10-27 11:29:36 -0400233}
234
mtklein9a5c47f2016-07-22 11:05:04 -0700235void SkRasterPipelineBlitter::blitH(int x, int y, int w) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400236 if (!fBlitH) {
237 SkRasterPipeline p;
238 p.extend(fShader);
Mike Klein66866172016-11-03 12:22:01 -0400239 if (fBlend != SkBlendMode::kSrc) {
240 this->append_load_d(&p);
241 this->append_blend(&p);
242 this->maybe_clamp(&p);
243 }
Mike Kleine902f8d2016-10-26 15:32:26 -0400244 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400245 fBlitH = p.compile();
246 }
Mike Kleinbd3fe472016-10-25 15:43:46 -0400247 fDstPtr = fDst.writable_addr(0,y);
Mike Klein319ba3d2017-01-20 15:11:54 -0500248 fCurrentY = y;
249 fBlitH(x,w);
mtklein9a5c47f2016-07-22 11:05:04 -0700250}
251
252void SkRasterPipelineBlitter::blitAntiH(int x, int y, const SkAlpha aa[], const int16_t runs[]) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400253 if (!fBlitAntiH) {
254 SkRasterPipeline p;
255 p.extend(fShader);
Mike Klein66866172016-11-03 12:22:01 -0400256 if (fBlend == SkBlendMode::kSrcOver) {
Mike Kleinbabd93e2016-11-30 16:05:10 -0500257 p.append(SkRasterPipeline::scale_1_float, &fCurrentCoverage);
Mike Klein66866172016-11-03 12:22:01 -0400258 this->append_load_d(&p);
259 this->append_blend(&p);
260 } else {
261 this->append_load_d(&p);
262 this->append_blend(&p);
Mike Kleinbabd93e2016-11-30 16:05:10 -0500263 p.append(SkRasterPipeline::lerp_1_float, &fCurrentCoverage);
Mike Klein66866172016-11-03 12:22:01 -0400264 }
Mike Klein130863e2016-10-27 11:29:36 -0400265 this->maybe_clamp(&p);
Mike Kleine902f8d2016-10-26 15:32:26 -0400266 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400267 fBlitAntiH = p.compile();
268 }
mtklein9a5c47f2016-07-22 11:05:04 -0700269
Mike Kleinbd3fe472016-10-25 15:43:46 -0400270 fDstPtr = fDst.writable_addr(0,y);
Mike Klein319ba3d2017-01-20 15:11:54 -0500271 fCurrentY = y;
mtklein9a5c47f2016-07-22 11:05:04 -0700272 for (int16_t run = *runs; run > 0; run = *runs) {
Mike Kleinaeb79592016-11-07 09:29:07 -0500273 switch (*aa) {
274 case 0x00: break;
275 case 0xff: this->blitH(x,y,run); break;
276 default:
Mike Kleinbabd93e2016-11-30 16:05:10 -0500277 fCurrentCoverage = *aa * (1/255.0f);
Mike Klein319ba3d2017-01-20 15:11:54 -0500278 fBlitAntiH(x,run);
Mike Kleinaeb79592016-11-07 09:29:07 -0500279 }
mtklein9a5c47f2016-07-22 11:05:04 -0700280 x += run;
281 runs += run;
282 aa += run;
283 }
284}
285
286void SkRasterPipelineBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
287 if (mask.fFormat == SkMask::kBW_Format) {
288 // TODO: native BW masks?
289 return INHERITED::blitMask(mask, clip);
290 }
291
Mike Kleinbd3fe472016-10-25 15:43:46 -0400292 if (mask.fFormat == SkMask::kA8_Format && !fBlitMaskA8) {
mtklein9a5c47f2016-07-22 11:05:04 -0700293 SkRasterPipeline p;
294 p.extend(fShader);
Mike Klein66866172016-11-03 12:22:01 -0400295 if (fBlend == SkBlendMode::kSrcOver) {
296 p.append(SkRasterPipeline::scale_u8, &fMaskPtr);
297 this->append_load_d(&p);
298 this->append_blend(&p);
299 } else {
300 this->append_load_d(&p);
301 this->append_blend(&p);
302 p.append(SkRasterPipeline::lerp_u8, &fMaskPtr);
303 }
Mike Klein130863e2016-10-27 11:29:36 -0400304 this->maybe_clamp(&p);
Mike Kleine902f8d2016-10-26 15:32:26 -0400305 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400306 fBlitMaskA8 = p.compile();
307 }
308
309 if (mask.fFormat == SkMask::kLCD16_Format && !fBlitMaskLCD16) {
310 SkRasterPipeline p;
311 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 fBlitMaskLCD16 = p.compile();
318 }
319
320 int x = clip.left();
321 for (int y = clip.top(); y < clip.bottom(); y++) {
322 fDstPtr = fDst.writable_addr(0,y);
Mike Klein319ba3d2017-01-20 15:11:54 -0500323 fCurrentY = y;
Mike Kleinbd3fe472016-10-25 15:43:46 -0400324
mtklein9a5c47f2016-07-22 11:05:04 -0700325 switch (mask.fFormat) {
326 case SkMask::kA8_Format:
Mike Kleinbd3fe472016-10-25 15:43:46 -0400327 fMaskPtr = mask.getAddr8(x,y)-x;
Mike Klein319ba3d2017-01-20 15:11:54 -0500328 fBlitMaskA8(x,clip.width());
mtklein9a5c47f2016-07-22 11:05:04 -0700329 break;
330 case SkMask::kLCD16_Format:
Mike Kleinbd3fe472016-10-25 15:43:46 -0400331 fMaskPtr = mask.getAddrLCD16(x,y)-x;
Mike Klein319ba3d2017-01-20 15:11:54 -0500332 fBlitMaskLCD16(x,clip.width());
mtklein9a5c47f2016-07-22 11:05:04 -0700333 break;
Mike Kleinbd3fe472016-10-25 15:43:46 -0400334 default:
335 // TODO
336 break;
mtklein9a5c47f2016-07-22 11:05:04 -0700337 }
mtklein9a5c47f2016-07-22 11:05:04 -0700338 }
339}