blob: 8b37bef24fa16427128e6cab29e5a4b2b6a6a44e [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
Hal Canaryc640d0d2018-06-13 09:59:02 -04008#include "../jumper/SkJumper.h"
Herb Derbyac04fef2017-01-13 17:34:33 -05009#include "SkArenaAlloc.h"
Hal Canaryfdcfb8b2018-06-13 09:42:32 -040010#include "SkBlendModePriv.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040011#include "SkBlitter.h"
mtklein9a5c47f2016-07-22 11:05:04 -070012#include "SkColor.h"
13#include "SkColorFilter.h"
Mike Klein3b840e92017-05-23 15:06:17 -040014#include "SkColorSpaceXformer.h"
Mike Kleinbaaf8ad2016-09-29 09:04:15 -040015#include "SkOpts.h"
mtklein9a5c47f2016-07-22 11:05:04 -070016#include "SkPM4f.h"
Mike Klein744908e2016-11-11 12:51:36 -050017#include "SkPM4fPriv.h"
mtklein9a5c47f2016-07-22 11:05:04 -070018#include "SkRasterPipeline.h"
19#include "SkShader.h"
Florin Malita4aed1382017-05-25 10:38:07 -040020#include "SkShaderBase.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040021#include "SkTo.h"
Mike Klein14c8f822016-11-30 19:39:43 -050022#include "SkUtils.h"
mtklein9a5c47f2016-07-22 11:05:04 -070023
Mike Klein4e3bc862017-05-22 20:15:17 +000024class SkRasterPipelineBlitter final : public SkBlitter {
mtklein9a5c47f2016-07-22 11:05:04 -070025public:
Mike Klein3b840e92017-05-23 15:06:17 -040026 // This is our common entrypoint for creating the blitter once we've sorted out shaders.
Mike Kleinb35cb312017-05-19 12:01:01 -040027 static SkBlitter* Create(const SkPixmap&, const SkPaint&, SkArenaAlloc*,
Florin Malita4aed1382017-05-25 10:38:07 -040028 const SkRasterPipeline& shaderPipeline,
Florin Malita47e55a52017-06-06 12:26:54 -040029 SkShaderBase::Context*,
Mike Reedeb7dc792017-06-12 12:48:45 -040030 bool is_opaque, bool is_constant);
Mike Kleinb35cb312017-05-19 12:01:01 -040031
Mike Klein3b840e92017-05-23 15:06:17 -040032 SkRasterPipelineBlitter(SkPixmap dst,
33 SkBlendMode blend,
Florin Malita47e55a52017-06-06 12:26:54 -040034 SkArenaAlloc* alloc,
35 SkShaderBase::Context* burstCtx)
mtklein9a5c47f2016-07-22 11:05:04 -070036 : fDst(dst)
Mike Kleine902f8d2016-10-26 15:32:26 -040037 , fBlend(blend)
Mike Klein0a76b412017-05-22 12:01:59 -040038 , fAlloc(alloc)
Florin Malita47e55a52017-06-06 12:26:54 -040039 , fBurstCtx(burstCtx)
Mike Kleinb24704d2017-05-24 07:53:00 -040040 , fColorPipeline(alloc)
mtklein9a5c47f2016-07-22 11:05:04 -070041 {}
42
Mike Klein34d10d72017-06-05 07:10:01 -040043 void blitH (int x, int y, int w) override;
44 void blitAntiH (int x, int y, const SkAlpha[], const int16_t[]) override;
45 void blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) override;
Mike Reedcc7f6602017-09-06 14:00:24 -040046 void blitAntiV2(int x, int y, U8CPU a0, U8CPU a1) override;
Mike Klein34d10d72017-06-05 07:10:01 -040047 void blitMask (const SkMask&, const SkIRect& clip) override;
Mike Klein3b59af52017-07-20 16:02:12 -040048 void blitRect (int x, int y, int width, int height) override;
Mike Reede6befa52017-08-28 11:30:48 -040049 void blitV (int x, int y, int height, SkAlpha alpha) override;
mtklein9a5c47f2016-07-22 11:05:04 -070050
mtklein9a5c47f2016-07-22 11:05:04 -070051private:
Mike Kleinb34b6262017-09-04 12:17:54 -040052 void append_load_dst(SkRasterPipeline*) const;
53 void append_store (SkRasterPipeline*) const;
mtklein8e4373f2016-07-22 14:20:27 -070054
Florin Malita47e55a52017-06-06 12:26:54 -040055 // If we have an burst context, use it to fill our shader buffer.
Mike Klein45c16fa2017-07-18 18:15:13 -040056 void burst_shade(int x, int y, int w);
Florin Malita47e55a52017-06-06 12:26:54 -040057
Florin Malita4aed1382017-05-25 10:38:07 -040058 SkPixmap fDst;
59 SkBlendMode fBlend;
60 SkArenaAlloc* fAlloc;
Florin Malita47e55a52017-06-06 12:26:54 -040061 SkShaderBase::Context* fBurstCtx;
Florin Malita4aed1382017-05-25 10:38:07 -040062 SkRasterPipeline fColorPipeline;
mtklein9a5c47f2016-07-22 11:05:04 -070063
Mike Klein45c16fa2017-07-18 18:15:13 -040064 SkJumper_MemoryCtx fShaderOutput = {nullptr,0}, // Possibly updated each call to burst_shade().
65 fDstPtr = {nullptr,0}, // Always points to the top-left of fDst.
66 fMaskPtr = {nullptr,0}; // Updated each call to blitMask().
67
Mike Klein3b59af52017-07-20 16:02:12 -040068 // We may be able to specialize blitH() or blitRect() into a memset.
69 bool fCanMemsetInBlitRect = false;
Mike Klein8729e5b2017-02-16 06:51:48 -050070 uint64_t fMemsetColor = 0; // Big enough for largest dst format, F16.
Mike Kleinbd3fe472016-10-25 15:43:46 -040071
Mike Klein8729e5b2017-02-16 06:51:48 -050072 // Built lazily on first use.
Mike Klein3b59af52017-07-20 16:02:12 -040073 std::function<void(size_t, size_t, size_t, size_t)> fBlitRect,
Mike Klein45c16fa2017-07-18 18:15:13 -040074 fBlitAntiH,
75 fBlitMaskA8,
76 fBlitMaskLCD16;
Mike Klein8729e5b2017-02-16 06:51:48 -050077
78 // These values are pointed to by the blit pipelines above,
79 // which allows us to adjust them from call to call.
Mike Klein45c16fa2017-07-18 18:15:13 -040080 float fCurrentCoverage = 0.0f;
81 float fDitherRate = 0.0f;
Mike Kleinbd3fe472016-10-25 15:43:46 -040082
Florin Malita47e55a52017-06-06 12:26:54 -040083 std::vector<SkPM4f> fShaderBuffer;
84
mtklein9a5c47f2016-07-22 11:05:04 -070085 typedef SkBlitter INHERITED;
86};
87
88SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst,
89 const SkPaint& paint,
Mike Kleinfb191da2016-11-15 13:20:33 -050090 const SkMatrix& ctm,
Matt Sarett25834ff2017-02-15 15:54:35 -050091 SkArenaAlloc* alloc) {
Mike Klein3b840e92017-05-23 15:06:17 -040092 SkColorSpace* dstCS = dst.colorSpace();
Mike Reedc91e3872017-07-05 14:12:37 -040093 SkPM4f paintColor = SkPM4f_from_SkColor(paint.getColor(), dstCS);
Florin Malita4aed1382017-05-25 10:38:07 -040094 auto shader = as_SB(paint.getShader());
Mike Klein3b840e92017-05-23 15:06:17 -040095
96 SkRasterPipeline_<256> shaderPipeline;
97 if (!shader) {
98 // Having no shader makes things nice and easy... just use the paint color.
Mike Klein073073e2017-08-03 09:42:53 -040099 shaderPipeline.append_constant_color(alloc, paintColor);
Mike Reedc91e3872017-07-05 14:12:37 -0400100 bool is_opaque = paintColor.a() == 1.0f,
Mike Reed98b7a6a2017-05-26 09:32:22 -0400101 is_constant = true;
Mike Klein3b840e92017-05-23 15:06:17 -0400102 return SkRasterPipelineBlitter::Create(dst, paint, alloc,
Florin Malita47e55a52017-06-06 12:26:54 -0400103 shaderPipeline, nullptr,
Mike Reedeb7dc792017-06-12 12:48:45 -0400104 is_opaque, is_constant);
Mike Klein3b840e92017-05-23 15:06:17 -0400105 }
106
Mike Reedc91e3872017-07-05 14:12:37 -0400107 bool is_opaque = shader->isOpaque() && paintColor.a() == 1.0f;
Mike Klein3b840e92017-05-23 15:06:17 -0400108 bool is_constant = shader->isConstant();
Florin Malita47e55a52017-06-06 12:26:54 -0400109
110 // Check whether the shader prefers to run in burst mode.
111 if (auto* burstCtx = shader->makeBurstPipelineContext(
112 SkShaderBase::ContextRec(paint, ctm, nullptr, SkShaderBase::ContextRec::kPM4f_DstType,
113 dstCS), alloc)) {
114 return SkRasterPipelineBlitter::Create(dst, paint, alloc,
115 shaderPipeline, burstCtx,
Mike Reedeb7dc792017-06-12 12:48:45 -0400116 is_opaque, is_constant);
Florin Malita47e55a52017-06-06 12:26:54 -0400117 }
118
Mike Reed1d8c42e2017-08-29 14:58:19 -0400119 if (shader->appendStages({&shaderPipeline, alloc, dstCS, paint, nullptr, ctm})) {
Mike Reedc91e3872017-07-05 14:12:37 -0400120 if (paintColor.a() != 1.0f) {
121 shaderPipeline.append(SkRasterPipeline::scale_1_float,
122 alloc->make<float>(paintColor.a()));
Mike Klein3b840e92017-05-23 15:06:17 -0400123 }
Florin Malita47e55a52017-06-06 12:26:54 -0400124 return SkRasterPipelineBlitter::Create(dst, paint, alloc, shaderPipeline, nullptr,
Mike Reedeb7dc792017-06-12 12:48:45 -0400125 is_opaque, is_constant);
Mike Klein3b840e92017-05-23 15:06:17 -0400126 }
127
Florin Malita47e55a52017-06-06 12:26:54 -0400128 // The shader has opted out of drawing anything.
Mike Reed6867eee2017-06-02 13:25:15 -0400129 return alloc->make<SkNullBlitter>();
Mike Reed02640952017-05-19 15:32:13 -0400130}
131
132SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst,
133 const SkPaint& paint,
Mike Reed02640952017-05-19 15:32:13 -0400134 const SkRasterPipeline& shaderPipeline,
Mike Kleinb35cb312017-05-19 12:01:01 -0400135 bool is_opaque,
Mike Reed02640952017-05-19 15:32:13 -0400136 SkArenaAlloc* alloc) {
Mike Kleinb35cb312017-05-19 12:01:01 -0400137 bool is_constant = false; // If this were the case, it'd be better to just set a paint color.
Florin Malita47e55a52017-06-06 12:26:54 -0400138 return SkRasterPipelineBlitter::Create(dst, paint, alloc, shaderPipeline, nullptr,
Mike Reedeb7dc792017-06-12 12:48:45 -0400139 is_opaque, is_constant);
Mike Reed02640952017-05-19 15:32:13 -0400140}
141
Mike Kleinb35cb312017-05-19 12:01:01 -0400142SkBlitter* SkRasterPipelineBlitter::Create(const SkPixmap& dst,
143 const SkPaint& paint,
144 SkArenaAlloc* alloc,
145 const SkRasterPipeline& shaderPipeline,
Florin Malita47e55a52017-06-06 12:26:54 -0400146 SkShaderBase::Context* burstCtx,
Mike Kleinb35cb312017-05-19 12:01:01 -0400147 bool is_opaque,
Mike Reedeb7dc792017-06-12 12:48:45 -0400148 bool is_constant) {
Mike Klein3b840e92017-05-23 15:06:17 -0400149 auto blitter = alloc->make<SkRasterPipelineBlitter>(dst,
150 paint.getBlendMode(),
Florin Malita47e55a52017-06-06 12:26:54 -0400151 alloc,
152 burstCtx);
Mike Reed02640952017-05-19 15:32:13 -0400153
Mike Kleinb35cb312017-05-19 12:01:01 -0400154 // Our job in this factory is to fill out the blitter's color pipeline.
155 // This is the common front of the full blit pipelines, each constructed lazily on first use.
156 // The full blit pipelines handle reading and writing the dst, blending, coverage, dithering.
157 auto colorPipeline = &blitter->fColorPipeline;
158
Mike Klein3b840e92017-05-23 15:06:17 -0400159 // Let's get the shader in first.
Florin Malita47e55a52017-06-06 12:26:54 -0400160 if (burstCtx) {
161 colorPipeline->append(SkRasterPipeline::load_f32, &blitter->fShaderOutput);
162 } else {
163 colorPipeline->extend(shaderPipeline);
164 }
Mike Reed02640952017-05-19 15:32:13 -0400165
Mike Kleinb35cb312017-05-19 12:01:01 -0400166 // If there's a color filter it comes next.
167 if (auto colorFilter = paint.getColorFilter()) {
168 colorFilter->appendStages(colorPipeline, dst.colorSpace(), alloc, is_opaque);
mtkleina4a44882016-11-04 13:20:07 -0700169 is_opaque = is_opaque && (colorFilter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag);
mtklein9a5c47f2016-07-22 11:05:04 -0700170 }
171
Mike Kleinac568a92018-01-25 09:09:32 -0500172 // Not all formats make sense to dither (think, F16). We set their dither rate
173 // to zero. We need to decide if we're going to dither now to keep is_constant accurate.
Mike Reedeb7dc792017-06-12 12:48:45 -0400174 if (paint.isDither()) {
Mike Kleinb35cb312017-05-19 12:01:01 -0400175 switch (dst.info().colorType()) {
Mike Kleinac568a92018-01-25 09:09:32 -0500176 default: blitter->fDitherRate = 0.0f; break;
177 case kARGB_4444_SkColorType: blitter->fDitherRate = 1/15.0f; break;
178 case kRGB_565_SkColorType: blitter->fDitherRate = 1/63.0f; break;
Mike Klein95d19f32017-06-15 07:40:24 -0700179 case kGray_8_SkColorType:
Mike Kleinac568a92018-01-25 09:09:32 -0500180 case kRGB_888x_SkColorType:
Mike Kleinb35cb312017-05-19 12:01:01 -0400181 case kRGBA_8888_SkColorType:
Mike Kleinac568a92018-01-25 09:09:32 -0500182 case kBGRA_8888_SkColorType: blitter->fDitherRate = 1/255.0f; break;
183 case kRGB_101010x_SkColorType:
184 case kRGBA_1010102_SkColorType: blitter->fDitherRate = 1/1023.0f; break;
Mike Kleinb35cb312017-05-19 12:01:01 -0400185 }
Mike Reedeb7dc792017-06-12 12:48:45 -0400186 // TODO: for constant colors, we could try to measure the effect of dithering, and if
187 // it has no value (i.e. all variations result in the same 32bit color, then we
188 // could disable it (for speed, by not adding the stage).
Mike Kleinb35cb312017-05-19 12:01:01 -0400189 }
Mike Klein9b10f8f2017-06-01 13:11:16 -0400190 is_constant = is_constant && (blitter->fDitherRate == 0.0f);
Mike Kleinb35cb312017-05-19 12:01:01 -0400191
192 // We're logically done here. The code between here and return blitter is all optimization.
193
194 // A pipeline that's still constant here can collapse back into a constant color.
Mike Klein14c8f822016-11-30 19:39:43 -0500195 if (is_constant) {
Mike Klein45c16fa2017-07-18 18:15:13 -0400196 SkPM4f constantColor;
197 SkJumper_MemoryCtx constantColorPtr = { &constantColor, 0 };
198 colorPipeline->append(SkRasterPipeline::store_f32, &constantColorPtr);
199 colorPipeline->run(0,0,1,1);
Mike Kleinb24704d2017-05-24 07:53:00 -0400200 colorPipeline->reset();
Mike Klein073073e2017-08-03 09:42:53 -0400201 colorPipeline->append_constant_color(alloc, constantColor);
mtkleina4a44882016-11-04 13:20:07 -0700202
Mike Klein45c16fa2017-07-18 18:15:13 -0400203 is_opaque = constantColor.a() == 1.0f;
Mike Klein66866172016-11-03 12:22:01 -0400204 }
mtklein8e4373f2016-07-22 14:20:27 -0700205
Mike Kleinb35cb312017-05-19 12:01:01 -0400206 // We can strength-reduce SrcOver into Src when opaque.
207 if (is_opaque && blitter->fBlend == SkBlendMode::kSrcOver) {
208 blitter->fBlend = SkBlendMode::kSrc;
mtklein8e4373f2016-07-22 14:20:27 -0700209 }
210
Mike Kleinb35cb312017-05-19 12:01:01 -0400211 // When we're drawing a constant color in Src mode, we can sometimes just memset.
212 // (The previous two optimizations help find more opportunities for this one.)
213 if (is_constant && blitter->fBlend == SkBlendMode::kSrc) {
214 // Run our color pipeline all the way through to produce what we'd memset when we can.
215 // Not all blits can memset, so we need to keep colorPipeline too.
Mike Kleinb24704d2017-05-24 07:53:00 -0400216 SkRasterPipeline_<256> p;
Mike Kleinb35cb312017-05-19 12:01:01 -0400217 p.extend(*colorPipeline);
Mike Klein45c16fa2017-07-18 18:15:13 -0400218 blitter->fDstPtr = SkJumper_MemoryCtx{&blitter->fMemsetColor, 0};
Mike Kleinb35cb312017-05-19 12:01:01 -0400219 blitter->append_store(&p);
Mike Klein45c16fa2017-07-18 18:15:13 -0400220 p.run(0,0,1,1);
Mike Klein14c8f822016-11-30 19:39:43 -0500221
Mike Klein3b59af52017-07-20 16:02:12 -0400222 blitter->fCanMemsetInBlitRect = true;
Mike Klein14c8f822016-11-30 19:39:43 -0500223 }
Mike Kleinb35cb312017-05-19 12:01:01 -0400224
Mike Klein45c16fa2017-07-18 18:15:13 -0400225 blitter->fDstPtr = SkJumper_MemoryCtx{
226 blitter->fDst.writable_addr(),
227 blitter->fDst.rowBytesAsPixels(),
228 };
229
Mike Kleinb35cb312017-05-19 12:01:01 -0400230 return blitter;
mtklein9a5c47f2016-07-22 11:05:04 -0700231}
232
Mike Kleinb34b6262017-09-04 12:17:54 -0400233void SkRasterPipelineBlitter::append_load_dst(SkRasterPipeline* p) const {
Mike Kleinac568a92018-01-25 09:09:32 -0500234 const void* ctx = &fDstPtr;
mtklein8e4373f2016-07-22 14:20:27 -0700235 switch (fDst.info().colorType()) {
Mike Kleinac568a92018-01-25 09:09:32 -0500236 default: break;
237
238 case kGray_8_SkColorType: p->append(SkRasterPipeline::load_g8_dst, ctx); break;
239 case kAlpha_8_SkColorType: p->append(SkRasterPipeline::load_a8_dst, ctx); break;
240 case kRGB_565_SkColorType: p->append(SkRasterPipeline::load_565_dst, ctx); break;
241 case kARGB_4444_SkColorType: p->append(SkRasterPipeline::load_4444_dst, ctx); break;
242 case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::load_bgra_dst, ctx); break;
243 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::load_8888_dst, ctx); break;
244 case kRGBA_1010102_SkColorType: p->append(SkRasterPipeline::load_1010102_dst, ctx); break;
245 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::load_f16_dst, ctx); break;
246
247 case kRGB_888x_SkColorType: p->append(SkRasterPipeline::load_8888_dst, ctx);
248 p->append(SkRasterPipeline::force_opaque_dst ); break;
249 case kRGB_101010x_SkColorType: p->append(SkRasterPipeline::load_1010102_dst, ctx);
250 p->append(SkRasterPipeline::force_opaque_dst ); break;
Mike Kleine03339a2016-11-28 13:24:27 -0500251 }
Mike Kleinb34b6262017-09-04 12:17:54 -0400252 if (fDst.info().alphaType() == kUnpremul_SkAlphaType) {
253 p->append(SkRasterPipeline::premul_dst);
254 }
mtklein8e4373f2016-07-22 14:20:27 -0700255}
256
Mike Klein14c8f822016-11-30 19:39:43 -0500257void SkRasterPipelineBlitter::append_store(SkRasterPipeline* p) const {
Mike Kleinb34b6262017-09-04 12:17:54 -0400258 if (fDst.info().alphaType() == kUnpremul_SkAlphaType) {
259 p->append(SkRasterPipeline::unpremul);
260 }
Mike Klein9b10f8f2017-06-01 13:11:16 -0400261 if (fDitherRate > 0.0f) {
Mike Klein9b10f8f2017-06-01 13:11:16 -0400262 p->append(SkRasterPipeline::dither, &fDitherRate);
Mike Kleindb711c92017-05-03 17:57:48 -0400263 }
264
Mike Kleinac568a92018-01-25 09:09:32 -0500265 const void* ctx = &fDstPtr;
mtklein8e4373f2016-07-22 14:20:27 -0700266 switch (fDst.info().colorType()) {
mtklein8e4373f2016-07-22 14:20:27 -0700267 default: break;
Mike Kleinac568a92018-01-25 09:09:32 -0500268
269 case kGray_8_SkColorType: p->append(SkRasterPipeline::luminance_to_alpha);
270 p->append(SkRasterPipeline::store_a8, ctx); break;
271 case kAlpha_8_SkColorType: p->append(SkRasterPipeline::store_a8, ctx); break;
272 case kRGB_565_SkColorType: p->append(SkRasterPipeline::store_565, ctx); break;
273 case kARGB_4444_SkColorType: p->append(SkRasterPipeline::store_4444, ctx); break;
274 case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::store_bgra, ctx); break;
275 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::store_8888, ctx); break;
276 case kRGBA_1010102_SkColorType: p->append(SkRasterPipeline::store_1010102, ctx); break;
277 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::store_f16, ctx); break;
278
279 case kRGB_888x_SkColorType: p->append(SkRasterPipeline::force_opaque );
280 p->append(SkRasterPipeline::store_8888, ctx); break;
281 case kRGB_101010x_SkColorType: p->append(SkRasterPipeline::force_opaque );
282 p->append(SkRasterPipeline::store_1010102, ctx); break;
mtklein8e4373f2016-07-22 14:20:27 -0700283 }
284}
285
Mike Klein45c16fa2017-07-18 18:15:13 -0400286void SkRasterPipelineBlitter::burst_shade(int x, int y, int w) {
287 SkASSERT(fBurstCtx);
288 if (w > SkToInt(fShaderBuffer.size())) {
289 fShaderBuffer.resize(w);
Florin Malita47e55a52017-06-06 12:26:54 -0400290 }
Mike Klein45c16fa2017-07-18 18:15:13 -0400291 fBurstCtx->shadeSpan4f(x,y, fShaderBuffer.data(), w);
292 // We'll be reading from fShaderOutput.pixels + x, so back up by x.
293 fShaderOutput = SkJumper_MemoryCtx{ fShaderBuffer.data() - x, 0 };
Florin Malita47e55a52017-06-06 12:26:54 -0400294}
295
mtklein9a5c47f2016-07-22 11:05:04 -0700296void SkRasterPipelineBlitter::blitH(int x, int y, int w) {
Mike Klein3b59af52017-07-20 16:02:12 -0400297 this->blitRect(x,y,w,1);
298}
299
300void SkRasterPipelineBlitter::blitRect(int x, int y, int w, int h) {
301 if (fCanMemsetInBlitRect) {
302 for (int ylimit = y+h; y < ylimit; y++) {
303 switch (fDst.shiftPerPixel()) {
304 case 0: memset (fDst.writable_addr8 (x,y), fMemsetColor, w); break;
305 case 1: sk_memset16(fDst.writable_addr16(x,y), fMemsetColor, w); break;
306 case 2: sk_memset32(fDst.writable_addr32(x,y), fMemsetColor, w); break;
307 case 3: sk_memset64(fDst.writable_addr64(x,y), fMemsetColor, w); break;
308 default: break;
309 }
Mike Klein8729e5b2017-02-16 06:51:48 -0500310 }
Mike Klein3b59af52017-07-20 16:02:12 -0400311 return;
Mike Klein8729e5b2017-02-16 06:51:48 -0500312 }
313
Mike Klein3b59af52017-07-20 16:02:12 -0400314 if (!fBlitRect) {
Mike Kleinb24704d2017-05-24 07:53:00 -0400315 SkRasterPipeline p(fAlloc);
Mike Kleinb35cb312017-05-19 12:01:01 -0400316 p.extend(fColorPipeline);
Mike Klein0cb15892017-10-24 11:07:09 -0400317 if (fBlend == SkBlendMode::kSrcOver
318 && (fDst.info().colorType() == kRGBA_8888_SkColorType ||
319 fDst.info().colorType() == kBGRA_8888_SkColorType)
320 && !fDst.colorSpace()
321 && fDst.info().alphaType() != kUnpremul_SkAlphaType
322 && fDitherRate == 0.0f) {
Mike Klein0cb15892017-10-24 11:07:09 -0400323 auto stage = fDst.info().colorType() == kRGBA_8888_SkColorType
324 ? SkRasterPipeline::srcover_rgba_8888
325 : SkRasterPipeline::srcover_bgra_8888;
326 p.append(stage, &fDstPtr);
Mike Klein50626262017-05-25 13:06:57 -0400327 } else {
328 if (fBlend != SkBlendMode::kSrc) {
Mike Kleinb34b6262017-09-04 12:17:54 -0400329 this->append_load_dst(&p);
330 SkBlendMode_AppendStages(fBlend, &p);
Mike Klein50626262017-05-25 13:06:57 -0400331 }
332 this->append_store(&p);
Mike Klein66866172016-11-03 12:22:01 -0400333 }
Mike Klein3b59af52017-07-20 16:02:12 -0400334 fBlitRect = p.compile();
Mike Kleinbd3fe472016-10-25 15:43:46 -0400335 }
Mike Klein3b59af52017-07-20 16:02:12 -0400336
Mike Klein45c16fa2017-07-18 18:15:13 -0400337 if (fBurstCtx) {
Mike Klein3b59af52017-07-20 16:02:12 -0400338 // We can only burst shade one row at a time.
339 for (int ylimit = y+h; y < ylimit; y++) {
340 this->burst_shade(x,y,w);
341 fBlitRect(x,y, w,1);
342 }
343 } else {
344 // If not bursting we can blit the entire rect at once.
345 fBlitRect(x,y,w,h);
Mike Klein45c16fa2017-07-18 18:15:13 -0400346 }
mtklein9a5c47f2016-07-22 11:05:04 -0700347}
348
349void SkRasterPipelineBlitter::blitAntiH(int x, int y, const SkAlpha aa[], const int16_t runs[]) {
Mike Klein0a76b412017-05-22 12:01:59 -0400350 if (!fBlitAntiH) {
Florin Malitac3a863f2017-08-23 20:36:02 +0000351 SkRasterPipeline p(fAlloc);
352 p.extend(fColorPipeline);
Mike Kleinfb126fa2017-08-24 13:06:23 -0400353 if (SkBlendMode_ShouldPreScaleCoverage(fBlend, /*rgb_coverage=*/false)) {
Florin Malitac3a863f2017-08-23 20:36:02 +0000354 p.append(SkRasterPipeline::scale_1_float, &fCurrentCoverage);
Mike Kleinb34b6262017-09-04 12:17:54 -0400355 this->append_load_dst(&p);
356 SkBlendMode_AppendStages(fBlend, &p);
Florin Malitac3a863f2017-08-23 20:36:02 +0000357 } else {
Mike Kleinb34b6262017-09-04 12:17:54 -0400358 this->append_load_dst(&p);
359 SkBlendMode_AppendStages(fBlend, &p);
Florin Malitac3a863f2017-08-23 20:36:02 +0000360 p.append(SkRasterPipeline::lerp_1_float, &fCurrentCoverage);
361 }
Mike Kleinfb126fa2017-08-24 13:06:23 -0400362
Florin Malitac3a863f2017-08-23 20:36:02 +0000363 this->append_store(&p);
364 fBlitAntiH = p.compile();
Mike Kleinbd3fe472016-10-25 15:43:46 -0400365 }
mtklein9a5c47f2016-07-22 11:05:04 -0700366
mtklein9a5c47f2016-07-22 11:05:04 -0700367 for (int16_t run = *runs; run > 0; run = *runs) {
Mike Kleinaeb79592016-11-07 09:29:07 -0500368 switch (*aa) {
369 case 0x00: break;
370 case 0xff: this->blitH(x,y,run); break;
371 default:
Mike Kleinbabd93e2016-11-30 16:05:10 -0500372 fCurrentCoverage = *aa * (1/255.0f);
Mike Klein45c16fa2017-07-18 18:15:13 -0400373 if (fBurstCtx) {
374 this->burst_shade(x,y,run);
375 }
376 fBlitAntiH(x,y,run,1);
Mike Kleinaeb79592016-11-07 09:29:07 -0500377 }
mtklein9a5c47f2016-07-22 11:05:04 -0700378 x += run;
379 runs += run;
380 aa += run;
381 }
382}
383
Mike Klein34d10d72017-06-05 07:10:01 -0400384void SkRasterPipelineBlitter::blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) {
385 SkIRect clip = {x,y, x+2,y+1};
386 uint8_t coverage[] = { (uint8_t)a0, (uint8_t)a1 };
387
388 SkMask mask;
389 mask.fImage = coverage;
390 mask.fBounds = clip;
391 mask.fRowBytes = 2;
392 mask.fFormat = SkMask::kA8_Format;
393
394 this->blitMask(mask, clip);
395}
396
Mike Reedcc7f6602017-09-06 14:00:24 -0400397void SkRasterPipelineBlitter::blitAntiV2(int x, int y, U8CPU a0, U8CPU a1) {
398 SkIRect clip = {x,y, x+1,y+2};
399 uint8_t coverage[] = { (uint8_t)a0, (uint8_t)a1 };
400
401 SkMask mask;
402 mask.fImage = coverage;
403 mask.fBounds = clip;
404 mask.fRowBytes = 1;
405 mask.fFormat = SkMask::kA8_Format;
406
407 this->blitMask(mask, clip);
408}
409
Mike Reede6befa52017-08-28 11:30:48 -0400410void SkRasterPipelineBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
411 SkIRect clip = {x,y, x+1,y+height};
412
413 SkMask mask;
414 mask.fImage = &alpha;
415 mask.fBounds = clip;
416 mask.fRowBytes = 0; // so we reuse the 1 "row" for all of height
417 mask.fFormat = SkMask::kA8_Format;
418
419 this->blitMask(mask, clip);
420}
421
mtklein9a5c47f2016-07-22 11:05:04 -0700422void SkRasterPipelineBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
423 if (mask.fFormat == SkMask::kBW_Format) {
424 // TODO: native BW masks?
425 return INHERITED::blitMask(mask, clip);
426 }
427
Mike Kleine8228032017-11-14 13:41:30 -0500428 // We'll use the first (A8) plane of any mask and ignore the other two, just like Ganesh.
429 SkMask::Format effectiveMaskFormat = mask.fFormat == SkMask::k3D_Format ? SkMask::kA8_Format
430 : mask.fFormat;
431
432
Mike Klein45c16fa2017-07-18 18:15:13 -0400433 // Lazily build whichever pipeline we need, specialized for each mask format.
Mike Kleine8228032017-11-14 13:41:30 -0500434 if (effectiveMaskFormat == SkMask::kA8_Format && !fBlitMaskA8) {
Florin Malitac3a863f2017-08-23 20:36:02 +0000435 SkRasterPipeline p(fAlloc);
436 p.extend(fColorPipeline);
Mike Kleinfb126fa2017-08-24 13:06:23 -0400437 if (SkBlendMode_ShouldPreScaleCoverage(fBlend, /*rgb_coverage=*/false)) {
Florin Malitac3a863f2017-08-23 20:36:02 +0000438 p.append(SkRasterPipeline::scale_u8, &fMaskPtr);
Mike Kleinb34b6262017-09-04 12:17:54 -0400439 this->append_load_dst(&p);
440 SkBlendMode_AppendStages(fBlend, &p);
Florin Malitac3a863f2017-08-23 20:36:02 +0000441 } else {
Mike Kleinb34b6262017-09-04 12:17:54 -0400442 this->append_load_dst(&p);
443 SkBlendMode_AppendStages(fBlend, &p);
Florin Malitac3a863f2017-08-23 20:36:02 +0000444 p.append(SkRasterPipeline::lerp_u8, &fMaskPtr);
445 }
Florin Malitac3a863f2017-08-23 20:36:02 +0000446 this->append_store(&p);
447 fBlitMaskA8 = p.compile();
Mike Kleinbd3fe472016-10-25 15:43:46 -0400448 }
Mike Kleine8228032017-11-14 13:41:30 -0500449 if (effectiveMaskFormat == SkMask::kLCD16_Format && !fBlitMaskLCD16) {
Mike Kleinb24704d2017-05-24 07:53:00 -0400450 SkRasterPipeline p(fAlloc);
Mike Kleinb35cb312017-05-19 12:01:01 -0400451 p.extend(fColorPipeline);
Mike Kleinfb126fa2017-08-24 13:06:23 -0400452 if (SkBlendMode_ShouldPreScaleCoverage(fBlend, /*rgb_coverage=*/true)) {
453 // Somewhat unusually, scale_565 needs dst loaded first.
Mike Kleinb34b6262017-09-04 12:17:54 -0400454 this->append_load_dst(&p);
Mike Kleinfb126fa2017-08-24 13:06:23 -0400455 p.append(SkRasterPipeline::scale_565, &fMaskPtr);
Mike Kleinb34b6262017-09-04 12:17:54 -0400456 SkBlendMode_AppendStages(fBlend, &p);
Mike Kleinfb126fa2017-08-24 13:06:23 -0400457 } else {
Mike Kleinb34b6262017-09-04 12:17:54 -0400458 this->append_load_dst(&p);
459 SkBlendMode_AppendStages(fBlend, &p);
Mike Kleinfb126fa2017-08-24 13:06:23 -0400460 p.append(SkRasterPipeline::lerp_565, &fMaskPtr);
461 }
Mike Kleine902f8d2016-10-26 15:32:26 -0400462 this->append_store(&p);
Mike Kleinb24704d2017-05-24 07:53:00 -0400463 fBlitMaskLCD16 = p.compile();
Mike Kleinbd3fe472016-10-25 15:43:46 -0400464 }
465
Mike Klein45c16fa2017-07-18 18:15:13 -0400466 std::function<void(size_t,size_t,size_t,size_t)>* blitter = nullptr;
467 // Update fMaskPtr to point "into" this current mask, but lined up with fDstPtr at (0,0).
Mike Klein804d0f22018-05-31 13:16:59 -0400468 // This sort of trickery upsets UBSAN (pointer-overflow) so we do our math in uintptr_t.
469
Mike Kleinec846122018-02-26 11:56:30 -0500470 // mask.fRowBytes is a uint32_t, which would break our addressing math on 64-bit builds.
471 size_t rowBytes = mask.fRowBytes;
Mike Kleine8228032017-11-14 13:41:30 -0500472 switch (effectiveMaskFormat) {
Mike Klein45c16fa2017-07-18 18:15:13 -0400473 case SkMask::kA8_Format:
Mike Kleinec846122018-02-26 11:56:30 -0500474 fMaskPtr.stride = rowBytes;
Mike Klein804d0f22018-05-31 13:16:59 -0400475 fMaskPtr.pixels = (void*)((uintptr_t)mask.fImage - mask.fBounds.left() * (size_t)1
476 - mask.fBounds.top() * rowBytes);
Mike Klein45c16fa2017-07-18 18:15:13 -0400477 blitter = &fBlitMaskA8;
478 break;
479 case SkMask::kLCD16_Format:
Mike Kleinec846122018-02-26 11:56:30 -0500480 fMaskPtr.stride = rowBytes / 2;
Mike Klein804d0f22018-05-31 13:16:59 -0400481 fMaskPtr.pixels = (void*)((uintptr_t)mask.fImage - mask.fBounds.left() * (size_t)2
482 - mask.fBounds.top() * rowBytes);
Mike Klein45c16fa2017-07-18 18:15:13 -0400483 blitter = &fBlitMaskLCD16;
484 break;
485 default:
486 return;
487 }
Mike Kleinbd3fe472016-10-25 15:43:46 -0400488
Mike Klein45c16fa2017-07-18 18:15:13 -0400489 SkASSERT(blitter);
490 if (fBurstCtx) {
491 // We can only burst shade one row at a time.
492 int x = clip.left();
493 for (int y = clip.top(); y < clip.bottom(); y++) {
494 this->burst_shade(x,y,clip.width());
495 (*blitter)(x,y, clip.width(),1);
mtklein9a5c47f2016-07-22 11:05:04 -0700496 }
Mike Klein45c16fa2017-07-18 18:15:13 -0400497 } else {
498 // If not bursting we can blit the entire mask at once.
499 (*blitter)(clip.left(),clip.top(), clip.width(),clip.height());
mtklein9a5c47f2016-07-22 11:05:04 -0700500 }
501}