blob: 0d45fa11b1fc5b702f9055f1ad9eb8b50f286b6d [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 Klein3b840e92017-05-23 15:06:17 -040013#include "SkColorSpaceXformer.h"
Mike Kleinbaaf8ad2016-09-29 09:04:15 -040014#include "SkOpts.h"
mtklein9a5c47f2016-07-22 11:05:04 -070015#include "SkPM4f.h"
Mike Klein744908e2016-11-11 12:51:36 -050016#include "SkPM4fPriv.h"
mtklein9a5c47f2016-07-22 11:05:04 -070017#include "SkRasterPipeline.h"
18#include "SkShader.h"
Florin Malita4aed1382017-05-25 10:38:07 -040019#include "SkShaderBase.h"
Mike Klein14c8f822016-11-30 19:39:43 -050020#include "SkUtils.h"
Mike Klein581e6982017-05-03 13:05:13 -040021#include "../jumper/SkJumper.h"
mtklein9a5c47f2016-07-22 11:05:04 -070022
Mike Klein4e3bc862017-05-22 20:15:17 +000023class SkRasterPipelineBlitter final : public SkBlitter {
mtklein9a5c47f2016-07-22 11:05:04 -070024public:
Mike Klein3b840e92017-05-23 15:06:17 -040025 // This is our common entrypoint for creating the blitter once we've sorted out shaders.
Mike Kleinb35cb312017-05-19 12:01:01 -040026 static SkBlitter* Create(const SkPixmap&, const SkPaint&, SkArenaAlloc*,
Florin Malita4aed1382017-05-25 10:38:07 -040027 const SkRasterPipeline& shaderPipeline,
Florin Malita47e55a52017-06-06 12:26:54 -040028 SkShaderBase::Context*,
Mike Reedeb7dc792017-06-12 12:48:45 -040029 bool is_opaque, bool is_constant);
Mike Kleinb35cb312017-05-19 12:01:01 -040030
Mike Klein3b840e92017-05-23 15:06:17 -040031 SkRasterPipelineBlitter(SkPixmap dst,
32 SkBlendMode blend,
Florin Malita47e55a52017-06-06 12:26:54 -040033 SkArenaAlloc* alloc,
34 SkShaderBase::Context* burstCtx)
mtklein9a5c47f2016-07-22 11:05:04 -070035 : fDst(dst)
Mike Kleine902f8d2016-10-26 15:32:26 -040036 , fBlend(blend)
Mike Klein0a76b412017-05-22 12:01:59 -040037 , fAlloc(alloc)
Florin Malita47e55a52017-06-06 12:26:54 -040038 , fBurstCtx(burstCtx)
Mike Kleinb24704d2017-05-24 07:53:00 -040039 , fColorPipeline(alloc)
mtklein9a5c47f2016-07-22 11:05:04 -070040 {}
41
Mike Klein34d10d72017-06-05 07:10:01 -040042 void blitH (int x, int y, int w) override;
43 void blitAntiH (int x, int y, const SkAlpha[], const int16_t[]) override;
44 void blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) override;
Mike Reedcc7f6602017-09-06 14:00:24 -040045 void blitAntiV2(int x, int y, U8CPU a0, U8CPU a1) override;
Mike Klein34d10d72017-06-05 07:10:01 -040046 void blitMask (const SkMask&, const SkIRect& clip) override;
Mike Klein3b59af52017-07-20 16:02:12 -040047 void blitRect (int x, int y, int width, int height) override;
Mike Reede6befa52017-08-28 11:30:48 -040048 void blitV (int x, int y, int height, SkAlpha alpha) override;
mtklein9a5c47f2016-07-22 11:05:04 -070049
mtklein9a5c47f2016-07-22 11:05:04 -070050private:
Mike Kleinb34b6262017-09-04 12:17:54 -040051 void append_load_dst(SkRasterPipeline*) const;
52 void append_store (SkRasterPipeline*) const;
mtklein8e4373f2016-07-22 14:20:27 -070053
Florin Malita47e55a52017-06-06 12:26:54 -040054 // If we have an burst context, use it to fill our shader buffer.
Mike Klein45c16fa2017-07-18 18:15:13 -040055 void burst_shade(int x, int y, int w);
Florin Malita47e55a52017-06-06 12:26:54 -040056
Florin Malita4aed1382017-05-25 10:38:07 -040057 SkPixmap fDst;
58 SkBlendMode fBlend;
59 SkArenaAlloc* fAlloc;
Florin Malita47e55a52017-06-06 12:26:54 -040060 SkShaderBase::Context* fBurstCtx;
Florin Malita4aed1382017-05-25 10:38:07 -040061 SkRasterPipeline fColorPipeline;
mtklein9a5c47f2016-07-22 11:05:04 -070062
Mike Klein45c16fa2017-07-18 18:15:13 -040063 SkJumper_MemoryCtx fShaderOutput = {nullptr,0}, // Possibly updated each call to burst_shade().
64 fDstPtr = {nullptr,0}, // Always points to the top-left of fDst.
65 fMaskPtr = {nullptr,0}; // Updated each call to blitMask().
66
Mike Klein3b59af52017-07-20 16:02:12 -040067 // We may be able to specialize blitH() or blitRect() into a memset.
68 bool fCanMemsetInBlitRect = false;
Mike Klein8729e5b2017-02-16 06:51:48 -050069 uint64_t fMemsetColor = 0; // Big enough for largest dst format, F16.
Mike Kleinbd3fe472016-10-25 15:43:46 -040070
Mike Klein8729e5b2017-02-16 06:51:48 -050071 // Built lazily on first use.
Mike Klein3b59af52017-07-20 16:02:12 -040072 std::function<void(size_t, size_t, size_t, size_t)> fBlitRect,
Mike Klein45c16fa2017-07-18 18:15:13 -040073 fBlitAntiH,
74 fBlitMaskA8,
75 fBlitMaskLCD16;
Mike Klein8729e5b2017-02-16 06:51:48 -050076
77 // These values are pointed to by the blit pipelines above,
78 // which allows us to adjust them from call to call.
Mike Klein45c16fa2017-07-18 18:15:13 -040079 float fCurrentCoverage = 0.0f;
80 float fDitherRate = 0.0f;
Mike Kleinbd3fe472016-10-25 15:43:46 -040081
Florin Malita47e55a52017-06-06 12:26:54 -040082 std::vector<SkPM4f> fShaderBuffer;
83
mtklein9a5c47f2016-07-22 11:05:04 -070084 typedef SkBlitter INHERITED;
85};
86
87SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst,
88 const SkPaint& paint,
Mike Kleinfb191da2016-11-15 13:20:33 -050089 const SkMatrix& ctm,
Matt Sarett25834ff2017-02-15 15:54:35 -050090 SkArenaAlloc* alloc) {
Mike Klein3b840e92017-05-23 15:06:17 -040091 SkColorSpace* dstCS = dst.colorSpace();
Mike Reedc91e3872017-07-05 14:12:37 -040092 SkPM4f paintColor = SkPM4f_from_SkColor(paint.getColor(), dstCS);
Florin Malita4aed1382017-05-25 10:38:07 -040093 auto shader = as_SB(paint.getShader());
Mike Klein3b840e92017-05-23 15:06:17 -040094
95 SkRasterPipeline_<256> shaderPipeline;
96 if (!shader) {
97 // Having no shader makes things nice and easy... just use the paint color.
Mike Klein073073e2017-08-03 09:42:53 -040098 shaderPipeline.append_constant_color(alloc, paintColor);
Mike Reedc91e3872017-07-05 14:12:37 -040099 bool is_opaque = paintColor.a() == 1.0f,
Mike Reed98b7a6a2017-05-26 09:32:22 -0400100 is_constant = true;
Mike Klein3b840e92017-05-23 15:06:17 -0400101 return SkRasterPipelineBlitter::Create(dst, paint, alloc,
Florin Malita47e55a52017-06-06 12:26:54 -0400102 shaderPipeline, nullptr,
Mike Reedeb7dc792017-06-12 12:48:45 -0400103 is_opaque, is_constant);
Mike Klein3b840e92017-05-23 15:06:17 -0400104 }
105
Mike Reedc91e3872017-07-05 14:12:37 -0400106 bool is_opaque = shader->isOpaque() && paintColor.a() == 1.0f;
Mike Klein3b840e92017-05-23 15:06:17 -0400107 bool is_constant = shader->isConstant();
Florin Malita47e55a52017-06-06 12:26:54 -0400108
109 // Check whether the shader prefers to run in burst mode.
110 if (auto* burstCtx = shader->makeBurstPipelineContext(
111 SkShaderBase::ContextRec(paint, ctm, nullptr, SkShaderBase::ContextRec::kPM4f_DstType,
112 dstCS), alloc)) {
113 return SkRasterPipelineBlitter::Create(dst, paint, alloc,
114 shaderPipeline, burstCtx,
Mike Reedeb7dc792017-06-12 12:48:45 -0400115 is_opaque, is_constant);
Florin Malita47e55a52017-06-06 12:26:54 -0400116 }
117
Mike Reed1d8c42e2017-08-29 14:58:19 -0400118 if (shader->appendStages({&shaderPipeline, alloc, dstCS, paint, nullptr, ctm})) {
Mike Reedc91e3872017-07-05 14:12:37 -0400119 if (paintColor.a() != 1.0f) {
120 shaderPipeline.append(SkRasterPipeline::scale_1_float,
121 alloc->make<float>(paintColor.a()));
Mike Klein3b840e92017-05-23 15:06:17 -0400122 }
Florin Malita47e55a52017-06-06 12:26:54 -0400123 return SkRasterPipelineBlitter::Create(dst, paint, alloc, shaderPipeline, nullptr,
Mike Reedeb7dc792017-06-12 12:48:45 -0400124 is_opaque, is_constant);
Mike Klein3b840e92017-05-23 15:06:17 -0400125 }
126
Florin Malita47e55a52017-06-06 12:26:54 -0400127 // The shader has opted out of drawing anything.
Mike Reed6867eee2017-06-02 13:25:15 -0400128 return alloc->make<SkNullBlitter>();
Mike Reed02640952017-05-19 15:32:13 -0400129}
130
131SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst,
132 const SkPaint& paint,
Mike Reed02640952017-05-19 15:32:13 -0400133 const SkRasterPipeline& shaderPipeline,
Mike Kleinb35cb312017-05-19 12:01:01 -0400134 bool is_opaque,
Mike Reed02640952017-05-19 15:32:13 -0400135 SkArenaAlloc* alloc) {
Mike Kleinb35cb312017-05-19 12:01:01 -0400136 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 -0400137 return SkRasterPipelineBlitter::Create(dst, paint, alloc, shaderPipeline, nullptr,
Mike Reedeb7dc792017-06-12 12:48:45 -0400138 is_opaque, is_constant);
Mike Reed02640952017-05-19 15:32:13 -0400139}
140
Mike Kleinb35cb312017-05-19 12:01:01 -0400141SkBlitter* SkRasterPipelineBlitter::Create(const SkPixmap& dst,
142 const SkPaint& paint,
143 SkArenaAlloc* alloc,
144 const SkRasterPipeline& shaderPipeline,
Florin Malita47e55a52017-06-06 12:26:54 -0400145 SkShaderBase::Context* burstCtx,
Mike Kleinb35cb312017-05-19 12:01:01 -0400146 bool is_opaque,
Mike Reedeb7dc792017-06-12 12:48:45 -0400147 bool is_constant) {
Mike Klein3b840e92017-05-23 15:06:17 -0400148 auto blitter = alloc->make<SkRasterPipelineBlitter>(dst,
149 paint.getBlendMode(),
Florin Malita47e55a52017-06-06 12:26:54 -0400150 alloc,
151 burstCtx);
Mike Reed02640952017-05-19 15:32:13 -0400152
Mike Kleinb35cb312017-05-19 12:01:01 -0400153 // Our job in this factory is to fill out the blitter's color pipeline.
154 // This is the common front of the full blit pipelines, each constructed lazily on first use.
155 // The full blit pipelines handle reading and writing the dst, blending, coverage, dithering.
156 auto colorPipeline = &blitter->fColorPipeline;
157
Mike Klein3b840e92017-05-23 15:06:17 -0400158 // Let's get the shader in first.
Florin Malita47e55a52017-06-06 12:26:54 -0400159 if (burstCtx) {
160 colorPipeline->append(SkRasterPipeline::load_f32, &blitter->fShaderOutput);
161 } else {
162 colorPipeline->extend(shaderPipeline);
163 }
Mike Reed02640952017-05-19 15:32:13 -0400164
Mike Kleinb35cb312017-05-19 12:01:01 -0400165 // If there's a color filter it comes next.
166 if (auto colorFilter = paint.getColorFilter()) {
167 colorFilter->appendStages(colorPipeline, dst.colorSpace(), alloc, is_opaque);
mtkleina4a44882016-11-04 13:20:07 -0700168 is_opaque = is_opaque && (colorFilter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag);
mtklein9a5c47f2016-07-22 11:05:04 -0700169 }
170
Mike Kleinac568a92018-01-25 09:09:32 -0500171 // Not all formats make sense to dither (think, F16). We set their dither rate
172 // 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 -0400173 if (paint.isDither()) {
Mike Kleinb35cb312017-05-19 12:01:01 -0400174 switch (dst.info().colorType()) {
Mike Kleinac568a92018-01-25 09:09:32 -0500175 default: blitter->fDitherRate = 0.0f; break;
176 case kARGB_4444_SkColorType: blitter->fDitherRate = 1/15.0f; break;
177 case kRGB_565_SkColorType: blitter->fDitherRate = 1/63.0f; break;
Mike Klein95d19f32017-06-15 07:40:24 -0700178 case kGray_8_SkColorType:
Mike Kleinac568a92018-01-25 09:09:32 -0500179 case kRGB_888x_SkColorType:
Mike Kleinb35cb312017-05-19 12:01:01 -0400180 case kRGBA_8888_SkColorType:
Mike Kleinac568a92018-01-25 09:09:32 -0500181 case kBGRA_8888_SkColorType: blitter->fDitherRate = 1/255.0f; break;
182 case kRGB_101010x_SkColorType:
183 case kRGBA_1010102_SkColorType: blitter->fDitherRate = 1/1023.0f; break;
Mike Kleinb35cb312017-05-19 12:01:01 -0400184 }
Mike Reedeb7dc792017-06-12 12:48:45 -0400185 // TODO: for constant colors, we could try to measure the effect of dithering, and if
186 // it has no value (i.e. all variations result in the same 32bit color, then we
187 // could disable it (for speed, by not adding the stage).
Mike Kleinb35cb312017-05-19 12:01:01 -0400188 }
Mike Klein9b10f8f2017-06-01 13:11:16 -0400189 is_constant = is_constant && (blitter->fDitherRate == 0.0f);
Mike Kleinb35cb312017-05-19 12:01:01 -0400190
191 // We're logically done here. The code between here and return blitter is all optimization.
192
193 // A pipeline that's still constant here can collapse back into a constant color.
Mike Klein14c8f822016-11-30 19:39:43 -0500194 if (is_constant) {
Mike Klein45c16fa2017-07-18 18:15:13 -0400195 SkPM4f constantColor;
196 SkJumper_MemoryCtx constantColorPtr = { &constantColor, 0 };
197 colorPipeline->append(SkRasterPipeline::store_f32, &constantColorPtr);
198 colorPipeline->run(0,0,1,1);
Mike Kleinb24704d2017-05-24 07:53:00 -0400199 colorPipeline->reset();
Mike Klein073073e2017-08-03 09:42:53 -0400200 colorPipeline->append_constant_color(alloc, constantColor);
mtkleina4a44882016-11-04 13:20:07 -0700201
Mike Klein45c16fa2017-07-18 18:15:13 -0400202 is_opaque = constantColor.a() == 1.0f;
Mike Klein66866172016-11-03 12:22:01 -0400203 }
mtklein8e4373f2016-07-22 14:20:27 -0700204
Mike Kleinb35cb312017-05-19 12:01:01 -0400205 // We can strength-reduce SrcOver into Src when opaque.
206 if (is_opaque && blitter->fBlend == SkBlendMode::kSrcOver) {
207 blitter->fBlend = SkBlendMode::kSrc;
mtklein8e4373f2016-07-22 14:20:27 -0700208 }
209
Mike Kleinb35cb312017-05-19 12:01:01 -0400210 // When we're drawing a constant color in Src mode, we can sometimes just memset.
211 // (The previous two optimizations help find more opportunities for this one.)
212 if (is_constant && blitter->fBlend == SkBlendMode::kSrc) {
213 // Run our color pipeline all the way through to produce what we'd memset when we can.
214 // Not all blits can memset, so we need to keep colorPipeline too.
Mike Kleinb24704d2017-05-24 07:53:00 -0400215 SkRasterPipeline_<256> p;
Mike Kleinb35cb312017-05-19 12:01:01 -0400216 p.extend(*colorPipeline);
Mike Klein45c16fa2017-07-18 18:15:13 -0400217 blitter->fDstPtr = SkJumper_MemoryCtx{&blitter->fMemsetColor, 0};
Mike Kleinb35cb312017-05-19 12:01:01 -0400218 blitter->append_store(&p);
Mike Klein45c16fa2017-07-18 18:15:13 -0400219 p.run(0,0,1,1);
Mike Klein14c8f822016-11-30 19:39:43 -0500220
Mike Klein3b59af52017-07-20 16:02:12 -0400221 blitter->fCanMemsetInBlitRect = true;
Mike Klein14c8f822016-11-30 19:39:43 -0500222 }
Mike Kleinb35cb312017-05-19 12:01:01 -0400223
Mike Klein45c16fa2017-07-18 18:15:13 -0400224 blitter->fDstPtr = SkJumper_MemoryCtx{
225 blitter->fDst.writable_addr(),
226 blitter->fDst.rowBytesAsPixels(),
227 };
228
Mike Kleinb35cb312017-05-19 12:01:01 -0400229 return blitter;
mtklein9a5c47f2016-07-22 11:05:04 -0700230}
231
Mike Kleinb34b6262017-09-04 12:17:54 -0400232void SkRasterPipelineBlitter::append_load_dst(SkRasterPipeline* p) const {
Mike Kleinac568a92018-01-25 09:09:32 -0500233 const void* ctx = &fDstPtr;
mtklein8e4373f2016-07-22 14:20:27 -0700234 switch (fDst.info().colorType()) {
Mike Kleinac568a92018-01-25 09:09:32 -0500235 default: break;
236
237 case kGray_8_SkColorType: p->append(SkRasterPipeline::load_g8_dst, ctx); break;
238 case kAlpha_8_SkColorType: p->append(SkRasterPipeline::load_a8_dst, ctx); break;
239 case kRGB_565_SkColorType: p->append(SkRasterPipeline::load_565_dst, ctx); break;
240 case kARGB_4444_SkColorType: p->append(SkRasterPipeline::load_4444_dst, ctx); break;
241 case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::load_bgra_dst, ctx); break;
242 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::load_8888_dst, ctx); break;
243 case kRGBA_1010102_SkColorType: p->append(SkRasterPipeline::load_1010102_dst, ctx); break;
244 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::load_f16_dst, ctx); break;
245
246 case kRGB_888x_SkColorType: p->append(SkRasterPipeline::load_8888_dst, ctx);
247 p->append(SkRasterPipeline::force_opaque_dst ); break;
248 case kRGB_101010x_SkColorType: p->append(SkRasterPipeline::load_1010102_dst, ctx);
249 p->append(SkRasterPipeline::force_opaque_dst ); break;
Mike Kleine03339a2016-11-28 13:24:27 -0500250 }
251 if (fDst.info().gammaCloseToSRGB()) {
Mike Kleinf1f11622017-12-18 14:07:31 -0500252 p->append(SkRasterPipeline::from_srgb_dst);
Mike Kleine03339a2016-11-28 13:24:27 -0500253 }
Mike Kleinb34b6262017-09-04 12:17:54 -0400254 if (fDst.info().alphaType() == kUnpremul_SkAlphaType) {
255 p->append(SkRasterPipeline::premul_dst);
256 }
mtklein8e4373f2016-07-22 14:20:27 -0700257}
258
Mike Klein14c8f822016-11-30 19:39:43 -0500259void SkRasterPipelineBlitter::append_store(SkRasterPipeline* p) const {
Mike Kleinb34b6262017-09-04 12:17:54 -0400260 if (fDst.info().alphaType() == kUnpremul_SkAlphaType) {
261 p->append(SkRasterPipeline::unpremul);
262 }
Matt Sarettf3880932017-03-24 10:06:03 -0400263 if (fDst.info().gammaCloseToSRGB()) {
Mike Kleine03339a2016-11-28 13:24:27 -0500264 p->append(SkRasterPipeline::to_srgb);
265 }
Mike Klein9b10f8f2017-06-01 13:11:16 -0400266 if (fDitherRate > 0.0f) {
Mike Kleindb711c92017-05-03 17:57:48 -0400267 // We dither after any sRGB transfer function to make sure our 1/255.0f is sensible
268 // over the whole range. If we did it before, 1/255.0f is too big a rate near zero.
Mike Klein9b10f8f2017-06-01 13:11:16 -0400269 p->append(SkRasterPipeline::dither, &fDitherRate);
Mike Kleindb711c92017-05-03 17:57:48 -0400270 }
271
Mike Kleinac568a92018-01-25 09:09:32 -0500272 const void* ctx = &fDstPtr;
mtklein8e4373f2016-07-22 14:20:27 -0700273 switch (fDst.info().colorType()) {
mtklein8e4373f2016-07-22 14:20:27 -0700274 default: break;
Mike Kleinac568a92018-01-25 09:09:32 -0500275
276 case kGray_8_SkColorType: p->append(SkRasterPipeline::luminance_to_alpha);
277 p->append(SkRasterPipeline::store_a8, ctx); break;
278 case kAlpha_8_SkColorType: p->append(SkRasterPipeline::store_a8, ctx); break;
279 case kRGB_565_SkColorType: p->append(SkRasterPipeline::store_565, ctx); break;
280 case kARGB_4444_SkColorType: p->append(SkRasterPipeline::store_4444, ctx); break;
281 case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::store_bgra, ctx); break;
282 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::store_8888, ctx); break;
283 case kRGBA_1010102_SkColorType: p->append(SkRasterPipeline::store_1010102, ctx); break;
284 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::store_f16, ctx); break;
285
286 case kRGB_888x_SkColorType: p->append(SkRasterPipeline::force_opaque );
287 p->append(SkRasterPipeline::store_8888, ctx); break;
288 case kRGB_101010x_SkColorType: p->append(SkRasterPipeline::force_opaque );
289 p->append(SkRasterPipeline::store_1010102, ctx); break;
mtklein8e4373f2016-07-22 14:20:27 -0700290 }
291}
292
Mike Klein45c16fa2017-07-18 18:15:13 -0400293void SkRasterPipelineBlitter::burst_shade(int x, int y, int w) {
294 SkASSERT(fBurstCtx);
295 if (w > SkToInt(fShaderBuffer.size())) {
296 fShaderBuffer.resize(w);
Florin Malita47e55a52017-06-06 12:26:54 -0400297 }
Mike Klein45c16fa2017-07-18 18:15:13 -0400298 fBurstCtx->shadeSpan4f(x,y, fShaderBuffer.data(), w);
299 // We'll be reading from fShaderOutput.pixels + x, so back up by x.
300 fShaderOutput = SkJumper_MemoryCtx{ fShaderBuffer.data() - x, 0 };
Florin Malita47e55a52017-06-06 12:26:54 -0400301}
302
mtklein9a5c47f2016-07-22 11:05:04 -0700303void SkRasterPipelineBlitter::blitH(int x, int y, int w) {
Mike Klein3b59af52017-07-20 16:02:12 -0400304 this->blitRect(x,y,w,1);
305}
306
307void SkRasterPipelineBlitter::blitRect(int x, int y, int w, int h) {
308 if (fCanMemsetInBlitRect) {
309 for (int ylimit = y+h; y < ylimit; y++) {
310 switch (fDst.shiftPerPixel()) {
311 case 0: memset (fDst.writable_addr8 (x,y), fMemsetColor, w); break;
312 case 1: sk_memset16(fDst.writable_addr16(x,y), fMemsetColor, w); break;
313 case 2: sk_memset32(fDst.writable_addr32(x,y), fMemsetColor, w); break;
314 case 3: sk_memset64(fDst.writable_addr64(x,y), fMemsetColor, w); break;
315 default: break;
316 }
Mike Klein8729e5b2017-02-16 06:51:48 -0500317 }
Mike Klein3b59af52017-07-20 16:02:12 -0400318 return;
Mike Klein8729e5b2017-02-16 06:51:48 -0500319 }
320
Mike Klein3b59af52017-07-20 16:02:12 -0400321 if (!fBlitRect) {
Mike Kleinb24704d2017-05-24 07:53:00 -0400322 SkRasterPipeline p(fAlloc);
Mike Kleinb35cb312017-05-19 12:01:01 -0400323 p.extend(fColorPipeline);
Mike Klein0cb15892017-10-24 11:07:09 -0400324 if (fBlend == SkBlendMode::kSrcOver
325 && (fDst.info().colorType() == kRGBA_8888_SkColorType ||
326 fDst.info().colorType() == kBGRA_8888_SkColorType)
327 && !fDst.colorSpace()
328 && fDst.info().alphaType() != kUnpremul_SkAlphaType
329 && fDitherRate == 0.0f) {
Mike Klein0cb15892017-10-24 11:07:09 -0400330 auto stage = fDst.info().colorType() == kRGBA_8888_SkColorType
331 ? SkRasterPipeline::srcover_rgba_8888
332 : SkRasterPipeline::srcover_bgra_8888;
333 p.append(stage, &fDstPtr);
Mike Klein50626262017-05-25 13:06:57 -0400334 } else {
335 if (fBlend != SkBlendMode::kSrc) {
Mike Kleinb34b6262017-09-04 12:17:54 -0400336 this->append_load_dst(&p);
337 SkBlendMode_AppendStages(fBlend, &p);
Mike Klein50626262017-05-25 13:06:57 -0400338 }
339 this->append_store(&p);
Mike Klein66866172016-11-03 12:22:01 -0400340 }
Mike Klein3b59af52017-07-20 16:02:12 -0400341 fBlitRect = p.compile();
Mike Kleinbd3fe472016-10-25 15:43:46 -0400342 }
Mike Klein3b59af52017-07-20 16:02:12 -0400343
Mike Klein45c16fa2017-07-18 18:15:13 -0400344 if (fBurstCtx) {
Mike Klein3b59af52017-07-20 16:02:12 -0400345 // We can only burst shade one row at a time.
346 for (int ylimit = y+h; y < ylimit; y++) {
347 this->burst_shade(x,y,w);
348 fBlitRect(x,y, w,1);
349 }
350 } else {
351 // If not bursting we can blit the entire rect at once.
352 fBlitRect(x,y,w,h);
Mike Klein45c16fa2017-07-18 18:15:13 -0400353 }
mtklein9a5c47f2016-07-22 11:05:04 -0700354}
355
356void SkRasterPipelineBlitter::blitAntiH(int x, int y, const SkAlpha aa[], const int16_t runs[]) {
Mike Klein0a76b412017-05-22 12:01:59 -0400357 if (!fBlitAntiH) {
Florin Malitac3a863f2017-08-23 20:36:02 +0000358 SkRasterPipeline p(fAlloc);
359 p.extend(fColorPipeline);
Mike Kleinfb126fa2017-08-24 13:06:23 -0400360 if (SkBlendMode_ShouldPreScaleCoverage(fBlend, /*rgb_coverage=*/false)) {
Florin Malitac3a863f2017-08-23 20:36:02 +0000361 p.append(SkRasterPipeline::scale_1_float, &fCurrentCoverage);
Mike Kleinb34b6262017-09-04 12:17:54 -0400362 this->append_load_dst(&p);
363 SkBlendMode_AppendStages(fBlend, &p);
Florin Malitac3a863f2017-08-23 20:36:02 +0000364 } else {
Mike Kleinb34b6262017-09-04 12:17:54 -0400365 this->append_load_dst(&p);
366 SkBlendMode_AppendStages(fBlend, &p);
Florin Malitac3a863f2017-08-23 20:36:02 +0000367 p.append(SkRasterPipeline::lerp_1_float, &fCurrentCoverage);
368 }
Mike Kleinfb126fa2017-08-24 13:06:23 -0400369
Florin Malitac3a863f2017-08-23 20:36:02 +0000370 this->append_store(&p);
371 fBlitAntiH = p.compile();
Mike Kleinbd3fe472016-10-25 15:43:46 -0400372 }
mtklein9a5c47f2016-07-22 11:05:04 -0700373
mtklein9a5c47f2016-07-22 11:05:04 -0700374 for (int16_t run = *runs; run > 0; run = *runs) {
Mike Kleinaeb79592016-11-07 09:29:07 -0500375 switch (*aa) {
376 case 0x00: break;
377 case 0xff: this->blitH(x,y,run); break;
378 default:
Mike Kleinbabd93e2016-11-30 16:05:10 -0500379 fCurrentCoverage = *aa * (1/255.0f);
Mike Klein45c16fa2017-07-18 18:15:13 -0400380 if (fBurstCtx) {
381 this->burst_shade(x,y,run);
382 }
383 fBlitAntiH(x,y,run,1);
Mike Kleinaeb79592016-11-07 09:29:07 -0500384 }
mtklein9a5c47f2016-07-22 11:05:04 -0700385 x += run;
386 runs += run;
387 aa += run;
388 }
389}
390
Mike Klein34d10d72017-06-05 07:10:01 -0400391void SkRasterPipelineBlitter::blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) {
392 SkIRect clip = {x,y, x+2,y+1};
393 uint8_t coverage[] = { (uint8_t)a0, (uint8_t)a1 };
394
395 SkMask mask;
396 mask.fImage = coverage;
397 mask.fBounds = clip;
398 mask.fRowBytes = 2;
399 mask.fFormat = SkMask::kA8_Format;
400
401 this->blitMask(mask, clip);
402}
403
Mike Reedcc7f6602017-09-06 14:00:24 -0400404void SkRasterPipelineBlitter::blitAntiV2(int x, int y, U8CPU a0, U8CPU a1) {
405 SkIRect clip = {x,y, x+1,y+2};
406 uint8_t coverage[] = { (uint8_t)a0, (uint8_t)a1 };
407
408 SkMask mask;
409 mask.fImage = coverage;
410 mask.fBounds = clip;
411 mask.fRowBytes = 1;
412 mask.fFormat = SkMask::kA8_Format;
413
414 this->blitMask(mask, clip);
415}
416
Mike Reede6befa52017-08-28 11:30:48 -0400417void SkRasterPipelineBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
418 SkIRect clip = {x,y, x+1,y+height};
419
420 SkMask mask;
421 mask.fImage = &alpha;
422 mask.fBounds = clip;
423 mask.fRowBytes = 0; // so we reuse the 1 "row" for all of height
424 mask.fFormat = SkMask::kA8_Format;
425
426 this->blitMask(mask, clip);
427}
428
mtklein9a5c47f2016-07-22 11:05:04 -0700429void SkRasterPipelineBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
430 if (mask.fFormat == SkMask::kBW_Format) {
431 // TODO: native BW masks?
432 return INHERITED::blitMask(mask, clip);
433 }
434
Mike Kleine8228032017-11-14 13:41:30 -0500435 // We'll use the first (A8) plane of any mask and ignore the other two, just like Ganesh.
436 SkMask::Format effectiveMaskFormat = mask.fFormat == SkMask::k3D_Format ? SkMask::kA8_Format
437 : mask.fFormat;
438
439
Mike Klein45c16fa2017-07-18 18:15:13 -0400440 // Lazily build whichever pipeline we need, specialized for each mask format.
Mike Kleine8228032017-11-14 13:41:30 -0500441 if (effectiveMaskFormat == SkMask::kA8_Format && !fBlitMaskA8) {
Florin Malitac3a863f2017-08-23 20:36:02 +0000442 SkRasterPipeline p(fAlloc);
443 p.extend(fColorPipeline);
Mike Kleinfb126fa2017-08-24 13:06:23 -0400444 if (SkBlendMode_ShouldPreScaleCoverage(fBlend, /*rgb_coverage=*/false)) {
Florin Malitac3a863f2017-08-23 20:36:02 +0000445 p.append(SkRasterPipeline::scale_u8, &fMaskPtr);
Mike Kleinb34b6262017-09-04 12:17:54 -0400446 this->append_load_dst(&p);
447 SkBlendMode_AppendStages(fBlend, &p);
Florin Malitac3a863f2017-08-23 20:36:02 +0000448 } else {
Mike Kleinb34b6262017-09-04 12:17:54 -0400449 this->append_load_dst(&p);
450 SkBlendMode_AppendStages(fBlend, &p);
Florin Malitac3a863f2017-08-23 20:36:02 +0000451 p.append(SkRasterPipeline::lerp_u8, &fMaskPtr);
452 }
Florin Malitac3a863f2017-08-23 20:36:02 +0000453 this->append_store(&p);
454 fBlitMaskA8 = p.compile();
Mike Kleinbd3fe472016-10-25 15:43:46 -0400455 }
Mike Kleine8228032017-11-14 13:41:30 -0500456 if (effectiveMaskFormat == SkMask::kLCD16_Format && !fBlitMaskLCD16) {
Mike Kleinb24704d2017-05-24 07:53:00 -0400457 SkRasterPipeline p(fAlloc);
Mike Kleinb35cb312017-05-19 12:01:01 -0400458 p.extend(fColorPipeline);
Mike Kleinfb126fa2017-08-24 13:06:23 -0400459 if (SkBlendMode_ShouldPreScaleCoverage(fBlend, /*rgb_coverage=*/true)) {
460 // Somewhat unusually, scale_565 needs dst loaded first.
Mike Kleinb34b6262017-09-04 12:17:54 -0400461 this->append_load_dst(&p);
Mike Kleinfb126fa2017-08-24 13:06:23 -0400462 p.append(SkRasterPipeline::scale_565, &fMaskPtr);
Mike Kleinb34b6262017-09-04 12:17:54 -0400463 SkBlendMode_AppendStages(fBlend, &p);
Mike Kleinfb126fa2017-08-24 13:06:23 -0400464 } else {
Mike Kleinb34b6262017-09-04 12:17:54 -0400465 this->append_load_dst(&p);
466 SkBlendMode_AppendStages(fBlend, &p);
Mike Kleinfb126fa2017-08-24 13:06:23 -0400467 p.append(SkRasterPipeline::lerp_565, &fMaskPtr);
468 }
Mike Kleine902f8d2016-10-26 15:32:26 -0400469 this->append_store(&p);
Mike Kleinb24704d2017-05-24 07:53:00 -0400470 fBlitMaskLCD16 = p.compile();
Mike Kleinbd3fe472016-10-25 15:43:46 -0400471 }
472
Mike Klein45c16fa2017-07-18 18:15:13 -0400473 std::function<void(size_t,size_t,size_t,size_t)>* blitter = nullptr;
474 // Update fMaskPtr to point "into" this current mask, but lined up with fDstPtr at (0,0).
Mike Kleinec846122018-02-26 11:56:30 -0500475 // mask.fRowBytes is a uint32_t, which would break our addressing math on 64-bit builds.
476 size_t rowBytes = mask.fRowBytes;
Mike Kleine8228032017-11-14 13:41:30 -0500477 switch (effectiveMaskFormat) {
Mike Klein45c16fa2017-07-18 18:15:13 -0400478 case SkMask::kA8_Format:
Mike Kleinec846122018-02-26 11:56:30 -0500479 fMaskPtr.stride = rowBytes;
480 fMaskPtr.pixels = (uint8_t*)(mask.fImage - mask.fBounds.left() * (size_t)1
481 - mask.fBounds.top() * rowBytes);
Mike Klein45c16fa2017-07-18 18:15:13 -0400482 blitter = &fBlitMaskA8;
483 break;
484 case SkMask::kLCD16_Format:
Mike Kleinec846122018-02-26 11:56:30 -0500485 fMaskPtr.stride = rowBytes / 2;
486 fMaskPtr.pixels = (uint16_t*)(mask.fImage - mask.fBounds.left() * (size_t)2
487 - mask.fBounds.top() * rowBytes);
Mike Klein45c16fa2017-07-18 18:15:13 -0400488 blitter = &fBlitMaskLCD16;
489 break;
490 default:
491 return;
492 }
Mike Kleinbd3fe472016-10-25 15:43:46 -0400493
Mike Klein45c16fa2017-07-18 18:15:13 -0400494 SkASSERT(blitter);
495 if (fBurstCtx) {
496 // We can only burst shade one row at a time.
497 int x = clip.left();
498 for (int y = clip.top(); y < clip.bottom(); y++) {
499 this->burst_shade(x,y,clip.width());
500 (*blitter)(x,y, clip.width(),1);
mtklein9a5c47f2016-07-22 11:05:04 -0700501 }
Mike Klein45c16fa2017-07-18 18:15:13 -0400502 } else {
503 // If not bursting we can blit the entire mask at once.
504 (*blitter)(clip.left(),clip.top(), clip.width(),clip.height());
mtklein9a5c47f2016-07-22 11:05:04 -0700505 }
506}