blob: 4c484a0439e87b51b2bd20665f7854c1ac9b017b [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;
45 void blitMask (const SkMask&, const SkIRect& clip) override;
Mike Klein3b59af52017-07-20 16:02:12 -040046 void blitRect (int x, int y, int width, int height) override;
mtklein9a5c47f2016-07-22 11:05:04 -070047
48 // TODO: The default implementations of the other blits look fine,
49 // but some of them like blitV could probably benefit from custom
50 // blits using something like a SkRasterPipeline::runFew() method.
51
52private:
Mike Klein14c8f822016-11-30 19:39:43 -050053 void append_load_d(SkRasterPipeline*) const;
54 void append_blend (SkRasterPipeline*) const;
Mike Klein14c8f822016-11-30 19:39:43 -050055 void append_store (SkRasterPipeline*) const;
mtklein8e4373f2016-07-22 14:20:27 -070056
Florin Malita47e55a52017-06-06 12:26:54 -040057 // If we have an burst context, use it to fill our shader buffer.
Mike Klein45c16fa2017-07-18 18:15:13 -040058 void burst_shade(int x, int y, int w);
Florin Malita47e55a52017-06-06 12:26:54 -040059
Florin Malita4aed1382017-05-25 10:38:07 -040060 SkPixmap fDst;
61 SkBlendMode fBlend;
62 SkArenaAlloc* fAlloc;
Florin Malita47e55a52017-06-06 12:26:54 -040063 SkShaderBase::Context* fBurstCtx;
Florin Malita4aed1382017-05-25 10:38:07 -040064 SkRasterPipeline fColorPipeline;
mtklein9a5c47f2016-07-22 11:05:04 -070065
Mike Klein45c16fa2017-07-18 18:15:13 -040066 SkJumper_MemoryCtx fShaderOutput = {nullptr,0}, // Possibly updated each call to burst_shade().
67 fDstPtr = {nullptr,0}, // Always points to the top-left of fDst.
68 fMaskPtr = {nullptr,0}; // Updated each call to blitMask().
69
Mike Klein3b59af52017-07-20 16:02:12 -040070 // We may be able to specialize blitH() or blitRect() into a memset.
71 bool fCanMemsetInBlitRect = false;
Mike Klein8729e5b2017-02-16 06:51:48 -050072 uint64_t fMemsetColor = 0; // Big enough for largest dst format, F16.
Mike Kleinbd3fe472016-10-25 15:43:46 -040073
Mike Klein8729e5b2017-02-16 06:51:48 -050074 // Built lazily on first use.
Mike Klein3b59af52017-07-20 16:02:12 -040075 std::function<void(size_t, size_t, size_t, size_t)> fBlitRect,
Mike Klein45c16fa2017-07-18 18:15:13 -040076 fBlitAntiH,
77 fBlitMaskA8,
78 fBlitMaskLCD16;
Mike Klein8729e5b2017-02-16 06:51:48 -050079
80 // These values are pointed to by the blit pipelines above,
81 // which allows us to adjust them from call to call.
Mike Klein45c16fa2017-07-18 18:15:13 -040082 float fCurrentCoverage = 0.0f;
83 float fDitherRate = 0.0f;
Mike Kleinbd3fe472016-10-25 15:43:46 -040084
Florin Malita47e55a52017-06-06 12:26:54 -040085 std::vector<SkPM4f> fShaderBuffer;
86
mtklein9a5c47f2016-07-22 11:05:04 -070087 typedef SkBlitter INHERITED;
88};
89
90SkBlitter* SkCreateRasterPipelineBlitter(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) {
Mike Klein3b840e92017-05-23 15:06:17 -040094 SkColorSpace* dstCS = dst.colorSpace();
Mike Reedc91e3872017-07-05 14:12:37 -040095 SkPM4f paintColor = SkPM4f_from_SkColor(paint.getColor(), dstCS);
Florin Malita4aed1382017-05-25 10:38:07 -040096 auto shader = as_SB(paint.getShader());
Mike Klein3b840e92017-05-23 15:06:17 -040097
98 SkRasterPipeline_<256> shaderPipeline;
99 if (!shader) {
100 // Having no shader makes things nice and easy... just use the paint color.
Mike Klein073073e2017-08-03 09:42:53 -0400101 shaderPipeline.append_constant_color(alloc, paintColor);
Mike Reedc91e3872017-07-05 14:12:37 -0400102 bool is_opaque = paintColor.a() == 1.0f,
Mike Reed98b7a6a2017-05-26 09:32:22 -0400103 is_constant = true;
Mike Klein3b840e92017-05-23 15:06:17 -0400104 return SkRasterPipelineBlitter::Create(dst, paint, alloc,
Florin Malita47e55a52017-06-06 12:26:54 -0400105 shaderPipeline, nullptr,
Mike Reedeb7dc792017-06-12 12:48:45 -0400106 is_opaque, is_constant);
Mike Klein3b840e92017-05-23 15:06:17 -0400107 }
108
Mike Reedc91e3872017-07-05 14:12:37 -0400109 bool is_opaque = shader->isOpaque() && paintColor.a() == 1.0f;
Mike Klein3b840e92017-05-23 15:06:17 -0400110 bool is_constant = shader->isConstant();
Florin Malita47e55a52017-06-06 12:26:54 -0400111
112 // Check whether the shader prefers to run in burst mode.
113 if (auto* burstCtx = shader->makeBurstPipelineContext(
114 SkShaderBase::ContextRec(paint, ctm, nullptr, SkShaderBase::ContextRec::kPM4f_DstType,
115 dstCS), alloc)) {
116 return SkRasterPipelineBlitter::Create(dst, paint, alloc,
117 shaderPipeline, burstCtx,
Mike Reedeb7dc792017-06-12 12:48:45 -0400118 is_opaque, is_constant);
Florin Malita47e55a52017-06-06 12:26:54 -0400119 }
120
Mike Klein3b840e92017-05-23 15:06:17 -0400121 if (shader->appendStages(&shaderPipeline, dstCS, alloc, ctm, paint)) {
Mike Reedc91e3872017-07-05 14:12:37 -0400122 if (paintColor.a() != 1.0f) {
123 shaderPipeline.append(SkRasterPipeline::scale_1_float,
124 alloc->make<float>(paintColor.a()));
Mike Klein3b840e92017-05-23 15:06:17 -0400125 }
Florin Malita47e55a52017-06-06 12:26:54 -0400126 return SkRasterPipelineBlitter::Create(dst, paint, alloc, shaderPipeline, nullptr,
Mike Reedeb7dc792017-06-12 12:48:45 -0400127 is_opaque, is_constant);
Mike Klein3b840e92017-05-23 15:06:17 -0400128 }
129
Florin Malita47e55a52017-06-06 12:26:54 -0400130 // The shader has opted out of drawing anything.
Mike Reed6867eee2017-06-02 13:25:15 -0400131 return alloc->make<SkNullBlitter>();
Mike Reed02640952017-05-19 15:32:13 -0400132}
133
134SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst,
135 const SkPaint& paint,
Mike Reed02640952017-05-19 15:32:13 -0400136 const SkRasterPipeline& shaderPipeline,
Mike Kleinb35cb312017-05-19 12:01:01 -0400137 bool is_opaque,
Mike Reed02640952017-05-19 15:32:13 -0400138 SkArenaAlloc* alloc) {
Mike Kleinb35cb312017-05-19 12:01:01 -0400139 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 -0400140 return SkRasterPipelineBlitter::Create(dst, paint, alloc, shaderPipeline, nullptr,
Mike Reedeb7dc792017-06-12 12:48:45 -0400141 is_opaque, is_constant);
Mike Reed02640952017-05-19 15:32:13 -0400142}
143
Mike Kleinb35cb312017-05-19 12:01:01 -0400144SkBlitter* SkRasterPipelineBlitter::Create(const SkPixmap& dst,
145 const SkPaint& paint,
146 SkArenaAlloc* alloc,
147 const SkRasterPipeline& shaderPipeline,
Florin Malita47e55a52017-06-06 12:26:54 -0400148 SkShaderBase::Context* burstCtx,
Mike Kleinb35cb312017-05-19 12:01:01 -0400149 bool is_opaque,
Mike Reedeb7dc792017-06-12 12:48:45 -0400150 bool is_constant) {
Mike Klein3b840e92017-05-23 15:06:17 -0400151 auto blitter = alloc->make<SkRasterPipelineBlitter>(dst,
152 paint.getBlendMode(),
Florin Malita47e55a52017-06-06 12:26:54 -0400153 alloc,
154 burstCtx);
Mike Reed02640952017-05-19 15:32:13 -0400155
Mike Kleinb35cb312017-05-19 12:01:01 -0400156 // Our job in this factory is to fill out the blitter's color pipeline.
157 // This is the common front of the full blit pipelines, each constructed lazily on first use.
158 // The full blit pipelines handle reading and writing the dst, blending, coverage, dithering.
159 auto colorPipeline = &blitter->fColorPipeline;
160
Mike Klein3b840e92017-05-23 15:06:17 -0400161 // Let's get the shader in first.
Florin Malita47e55a52017-06-06 12:26:54 -0400162 if (burstCtx) {
163 colorPipeline->append(SkRasterPipeline::load_f32, &blitter->fShaderOutput);
164 } else {
165 colorPipeline->extend(shaderPipeline);
166 }
Mike Reed02640952017-05-19 15:32:13 -0400167
Mike Kleinb35cb312017-05-19 12:01:01 -0400168 // If there's a color filter it comes next.
169 if (auto colorFilter = paint.getColorFilter()) {
170 colorFilter->appendStages(colorPipeline, dst.colorSpace(), alloc, is_opaque);
mtkleina4a44882016-11-04 13:20:07 -0700171 is_opaque = is_opaque && (colorFilter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag);
mtklein9a5c47f2016-07-22 11:05:04 -0700172 }
173
Mike Kleinb35cb312017-05-19 12:01:01 -0400174 // Not all formats make sense to dither (think, F16). We set their dither rate to zero.
175 // We need to decide if we're going to dither now to keep is_constant accurate.
Mike Reedeb7dc792017-06-12 12:48:45 -0400176 if (paint.isDither()) {
Mike Kleinb35cb312017-05-19 12:01:01 -0400177 switch (dst.info().colorType()) {
Mike Klein9b10f8f2017-06-01 13:11:16 -0400178 default: blitter->fDitherRate = 0.0f; break;
Mike Klein95d19f32017-06-15 07:40:24 -0700179 case kARGB_4444_SkColorType: blitter->fDitherRate = 1/15.0f; break;
Mike Klein9b10f8f2017-06-01 13:11:16 -0400180 case kRGB_565_SkColorType: blitter->fDitherRate = 1/63.0f; break;
Mike Klein95d19f32017-06-15 07:40:24 -0700181 case kGray_8_SkColorType:
Mike Kleinb35cb312017-05-19 12:01:01 -0400182 case kRGBA_8888_SkColorType:
Mike Klein9b10f8f2017-06-01 13:11:16 -0400183 case kBGRA_8888_SkColorType: blitter->fDitherRate = 1/255.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 Kleine902f8d2016-10-26 15:32:26 -0400232void SkRasterPipelineBlitter::append_load_d(SkRasterPipeline* p) const {
mtklein8e4373f2016-07-22 14:20:27 -0700233 switch (fDst.info().colorType()) {
Mike Reed279091e2017-06-27 16:58:00 -0400234 case kGray_8_SkColorType: p->append(SkRasterPipeline::load_g8_dst, &fDstPtr); break;
235 case kAlpha_8_SkColorType: p->append(SkRasterPipeline::load_a8_dst, &fDstPtr); break;
236 case kRGB_565_SkColorType: p->append(SkRasterPipeline::load_565_dst, &fDstPtr); break;
237 case kARGB_4444_SkColorType: p->append(SkRasterPipeline::load_4444_dst, &fDstPtr); break;
Mike Klein111f8a92017-06-27 15:56:32 -0400238 case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::load_bgra_dst, &fDstPtr); break;
Mike Reed279091e2017-06-27 16:58:00 -0400239 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::load_8888_dst, &fDstPtr); break;
240 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::load_f16_dst, &fDstPtr); break;
Mike Klein111f8a92017-06-27 15:56:32 -0400241 default: break;
Mike Kleine03339a2016-11-28 13:24:27 -0500242 }
243 if (fDst.info().gammaCloseToSRGB()) {
Mike Reed279091e2017-06-27 16:58:00 -0400244 p->append_from_srgb_dst(fDst.info().alphaType());
Mike Kleine03339a2016-11-28 13:24:27 -0500245 }
mtklein8e4373f2016-07-22 14:20:27 -0700246}
247
Mike Klein14c8f822016-11-30 19:39:43 -0500248void SkRasterPipelineBlitter::append_store(SkRasterPipeline* p) const {
Matt Sarettf3880932017-03-24 10:06:03 -0400249 if (fDst.info().gammaCloseToSRGB()) {
Mike Kleine03339a2016-11-28 13:24:27 -0500250 p->append(SkRasterPipeline::to_srgb);
251 }
Mike Klein9b10f8f2017-06-01 13:11:16 -0400252 if (fDitherRate > 0.0f) {
Mike Kleindb711c92017-05-03 17:57:48 -0400253 // We dither after any sRGB transfer function to make sure our 1/255.0f is sensible
254 // 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 -0400255 p->append(SkRasterPipeline::dither, &fDitherRate);
Mike Kleindb711c92017-05-03 17:57:48 -0400256 }
257
Mike Klein159db0a2017-07-25 12:02:13 -0400258 if (fDst.info().colorType() != kRGBA_F16_SkColorType) {
259 p->clamp_if_unclamped(kPremul_SkAlphaType);
260 }
261
mtklein8e4373f2016-07-22 14:20:27 -0700262 switch (fDst.info().colorType()) {
Mike Klein95d19f32017-06-15 07:40:24 -0700263 case kGray_8_SkColorType: p->append(SkRasterPipeline::luminance_to_alpha); // fallthru
Mike Kleine71b1672017-01-13 07:59:23 -0500264 case kAlpha_8_SkColorType: p->append(SkRasterPipeline::store_a8, &fDstPtr); break;
Mike Kleine03339a2016-11-28 13:24:27 -0500265 case kRGB_565_SkColorType: p->append(SkRasterPipeline::store_565, &fDstPtr); break;
Mike Klein95d19f32017-06-15 07:40:24 -0700266 case kARGB_4444_SkColorType: p->append(SkRasterPipeline::store_4444, &fDstPtr); break;
Mike Klein111f8a92017-06-27 15:56:32 -0400267 case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::store_bgra, &fDstPtr); break;
Mike Kleine03339a2016-11-28 13:24:27 -0500268 case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::store_8888, &fDstPtr); break;
269 case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::store_f16, &fDstPtr); break;
mtklein8e4373f2016-07-22 14:20:27 -0700270 default: break;
271 }
272}
273
Mike Kleine902f8d2016-10-26 15:32:26 -0400274void SkRasterPipelineBlitter::append_blend(SkRasterPipeline* p) const {
Mike Kleinfb126fa2017-08-24 13:06:23 -0400275 if (fDst.info().alphaType() == kUnpremul_SkAlphaType) { // TODO: move to append_load_d
Mike Reed883c9bc2017-07-19 10:57:53 -0400276 p->append(SkRasterPipeline::premul_dst);
277 }
Mike Kleinfb126fa2017-08-24 13:06:23 -0400278 SkBlendMode_AppendStages(fBlend, p);
279 if (fDst.info().alphaType() == kUnpremul_SkAlphaType) { // TODO: move to append_store
Mike Reed883c9bc2017-07-19 10:57:53 -0400280 p->append(SkRasterPipeline::unpremul);
281 }
Mike Kleine902f8d2016-10-26 15:32:26 -0400282}
Mike Kleine9f74b82016-10-25 13:31:21 -0400283
Mike Klein45c16fa2017-07-18 18:15:13 -0400284void SkRasterPipelineBlitter::burst_shade(int x, int y, int w) {
285 SkASSERT(fBurstCtx);
286 if (w > SkToInt(fShaderBuffer.size())) {
287 fShaderBuffer.resize(w);
Florin Malita47e55a52017-06-06 12:26:54 -0400288 }
Mike Klein45c16fa2017-07-18 18:15:13 -0400289 fBurstCtx->shadeSpan4f(x,y, fShaderBuffer.data(), w);
290 // We'll be reading from fShaderOutput.pixels + x, so back up by x.
291 fShaderOutput = SkJumper_MemoryCtx{ fShaderBuffer.data() - x, 0 };
Florin Malita47e55a52017-06-06 12:26:54 -0400292}
293
mtklein9a5c47f2016-07-22 11:05:04 -0700294void SkRasterPipelineBlitter::blitH(int x, int y, int w) {
Mike Klein3b59af52017-07-20 16:02:12 -0400295 this->blitRect(x,y,w,1);
296}
297
298void SkRasterPipelineBlitter::blitRect(int x, int y, int w, int h) {
299 if (fCanMemsetInBlitRect) {
300 for (int ylimit = y+h; y < ylimit; y++) {
301 switch (fDst.shiftPerPixel()) {
302 case 0: memset (fDst.writable_addr8 (x,y), fMemsetColor, w); break;
303 case 1: sk_memset16(fDst.writable_addr16(x,y), fMemsetColor, w); break;
304 case 2: sk_memset32(fDst.writable_addr32(x,y), fMemsetColor, w); break;
305 case 3: sk_memset64(fDst.writable_addr64(x,y), fMemsetColor, w); break;
306 default: break;
307 }
Mike Klein8729e5b2017-02-16 06:51:48 -0500308 }
Mike Klein3b59af52017-07-20 16:02:12 -0400309 return;
Mike Klein8729e5b2017-02-16 06:51:48 -0500310 }
311
Mike Klein3b59af52017-07-20 16:02:12 -0400312 if (!fBlitRect) {
Mike Kleinb24704d2017-05-24 07:53:00 -0400313 SkRasterPipeline p(fAlloc);
Mike Kleinb35cb312017-05-19 12:01:01 -0400314 p.extend(fColorPipeline);
Mike Klein50626262017-05-25 13:06:57 -0400315 if (fBlend == SkBlendMode::kSrcOver
316 && fDst.info().colorType() == kRGBA_8888_SkColorType
317 && !fDst.colorSpace()
Mike Reed883c9bc2017-07-19 10:57:53 -0400318 && fDst.info().alphaType() != kUnpremul_SkAlphaType
Mike Klein9b10f8f2017-06-01 13:11:16 -0400319 && fDitherRate == 0.0f) {
Mike Klein159db0a2017-07-25 12:02:13 -0400320 p.clamp_if_unclamped(kPremul_SkAlphaType);
Mike Klein50626262017-05-25 13:06:57 -0400321 p.append(SkRasterPipeline::srcover_rgba_8888, &fDstPtr);
322 } else {
323 if (fBlend != SkBlendMode::kSrc) {
324 this->append_load_d(&p);
325 this->append_blend(&p);
Mike Klein50626262017-05-25 13:06:57 -0400326 }
327 this->append_store(&p);
Mike Klein66866172016-11-03 12:22:01 -0400328 }
Mike Klein3b59af52017-07-20 16:02:12 -0400329 fBlitRect = p.compile();
Mike Kleinbd3fe472016-10-25 15:43:46 -0400330 }
Mike Klein3b59af52017-07-20 16:02:12 -0400331
Mike Klein45c16fa2017-07-18 18:15:13 -0400332 if (fBurstCtx) {
Mike Klein3b59af52017-07-20 16:02:12 -0400333 // We can only burst shade one row at a time.
334 for (int ylimit = y+h; y < ylimit; y++) {
335 this->burst_shade(x,y,w);
336 fBlitRect(x,y, w,1);
337 }
338 } else {
339 // If not bursting we can blit the entire rect at once.
340 fBlitRect(x,y,w,h);
Mike Klein45c16fa2017-07-18 18:15:13 -0400341 }
mtklein9a5c47f2016-07-22 11:05:04 -0700342}
343
344void SkRasterPipelineBlitter::blitAntiH(int x, int y, const SkAlpha aa[], const int16_t runs[]) {
Mike Klein0a76b412017-05-22 12:01:59 -0400345 if (!fBlitAntiH) {
Florin Malitac3a863f2017-08-23 20:36:02 +0000346 SkRasterPipeline p(fAlloc);
347 p.extend(fColorPipeline);
Mike Kleinfb126fa2017-08-24 13:06:23 -0400348 if (SkBlendMode_ShouldPreScaleCoverage(fBlend, /*rgb_coverage=*/false)) {
Florin Malitac3a863f2017-08-23 20:36:02 +0000349 p.append(SkRasterPipeline::scale_1_float, &fCurrentCoverage);
350 this->append_load_d(&p);
351 this->append_blend(&p);
352 } else {
353 this->append_load_d(&p);
354 this->append_blend(&p);
355 p.append(SkRasterPipeline::lerp_1_float, &fCurrentCoverage);
356 }
Mike Kleinfb126fa2017-08-24 13:06:23 -0400357
Florin Malitac3a863f2017-08-23 20:36:02 +0000358 this->append_store(&p);
359 fBlitAntiH = p.compile();
Mike Kleinbd3fe472016-10-25 15:43:46 -0400360 }
mtklein9a5c47f2016-07-22 11:05:04 -0700361
mtklein9a5c47f2016-07-22 11:05:04 -0700362 for (int16_t run = *runs; run > 0; run = *runs) {
Mike Kleinaeb79592016-11-07 09:29:07 -0500363 switch (*aa) {
364 case 0x00: break;
365 case 0xff: this->blitH(x,y,run); break;
366 default:
Mike Kleinbabd93e2016-11-30 16:05:10 -0500367 fCurrentCoverage = *aa * (1/255.0f);
Mike Klein45c16fa2017-07-18 18:15:13 -0400368 if (fBurstCtx) {
369 this->burst_shade(x,y,run);
370 }
371 fBlitAntiH(x,y,run,1);
Mike Kleinaeb79592016-11-07 09:29:07 -0500372 }
mtklein9a5c47f2016-07-22 11:05:04 -0700373 x += run;
374 runs += run;
375 aa += run;
376 }
377}
378
Mike Klein34d10d72017-06-05 07:10:01 -0400379void SkRasterPipelineBlitter::blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) {
380 SkIRect clip = {x,y, x+2,y+1};
381 uint8_t coverage[] = { (uint8_t)a0, (uint8_t)a1 };
382
383 SkMask mask;
384 mask.fImage = coverage;
385 mask.fBounds = clip;
386 mask.fRowBytes = 2;
387 mask.fFormat = SkMask::kA8_Format;
388
389 this->blitMask(mask, clip);
390}
391
mtklein9a5c47f2016-07-22 11:05:04 -0700392void SkRasterPipelineBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
393 if (mask.fFormat == SkMask::kBW_Format) {
394 // TODO: native BW masks?
395 return INHERITED::blitMask(mask, clip);
396 }
397
Mike Klein45c16fa2017-07-18 18:15:13 -0400398 // Lazily build whichever pipeline we need, specialized for each mask format.
Mike Klein0a76b412017-05-22 12:01:59 -0400399 if (mask.fFormat == SkMask::kA8_Format && !fBlitMaskA8) {
Florin Malitac3a863f2017-08-23 20:36:02 +0000400 SkRasterPipeline p(fAlloc);
401 p.extend(fColorPipeline);
Mike Kleinfb126fa2017-08-24 13:06:23 -0400402 if (SkBlendMode_ShouldPreScaleCoverage(fBlend, /*rgb_coverage=*/false)) {
Florin Malitac3a863f2017-08-23 20:36:02 +0000403 p.append(SkRasterPipeline::scale_u8, &fMaskPtr);
404 this->append_load_d(&p);
405 this->append_blend(&p);
406 } else {
407 this->append_load_d(&p);
408 this->append_blend(&p);
409 p.append(SkRasterPipeline::lerp_u8, &fMaskPtr);
410 }
Florin Malitac3a863f2017-08-23 20:36:02 +0000411 this->append_store(&p);
412 fBlitMaskA8 = p.compile();
Mike Kleinbd3fe472016-10-25 15:43:46 -0400413 }
Mike Klein0a76b412017-05-22 12:01:59 -0400414 if (mask.fFormat == SkMask::kLCD16_Format && !fBlitMaskLCD16) {
Mike Kleinb24704d2017-05-24 07:53:00 -0400415 SkRasterPipeline p(fAlloc);
Mike Kleinb35cb312017-05-19 12:01:01 -0400416 p.extend(fColorPipeline);
Mike Kleinfb126fa2017-08-24 13:06:23 -0400417 if (SkBlendMode_ShouldPreScaleCoverage(fBlend, /*rgb_coverage=*/true)) {
418 // Somewhat unusually, scale_565 needs dst loaded first.
419 this->append_load_d(&p);
420 p.append(SkRasterPipeline::scale_565, &fMaskPtr);
421 this->append_blend(&p);
422 } else {
423 this->append_load_d(&p);
424 this->append_blend(&p);
425 p.append(SkRasterPipeline::lerp_565, &fMaskPtr);
426 }
Mike Kleine902f8d2016-10-26 15:32:26 -0400427 this->append_store(&p);
Mike Kleinb24704d2017-05-24 07:53:00 -0400428 fBlitMaskLCD16 = p.compile();
Mike Kleinbd3fe472016-10-25 15:43:46 -0400429 }
430
Mike Klein45c16fa2017-07-18 18:15:13 -0400431 std::function<void(size_t,size_t,size_t,size_t)>* blitter = nullptr;
432 // Update fMaskPtr to point "into" this current mask, but lined up with fDstPtr at (0,0).
433 switch (mask.fFormat) {
434 case SkMask::kA8_Format:
435 fMaskPtr.stride = mask.fRowBytes;
436 fMaskPtr.pixels = (uint8_t*)mask.fImage - mask.fBounds.left()
437 - mask.fBounds.top() * fMaskPtr.stride;
438 blitter = &fBlitMaskA8;
439 break;
440 case SkMask::kLCD16_Format:
441 fMaskPtr.stride = mask.fRowBytes / 2;
442 fMaskPtr.pixels = (uint16_t*)mask.fImage - mask.fBounds.left()
443 - mask.fBounds.top() * fMaskPtr.stride;
444 blitter = &fBlitMaskLCD16;
445 break;
446 default:
447 return;
448 }
Mike Kleinbd3fe472016-10-25 15:43:46 -0400449
Mike Klein45c16fa2017-07-18 18:15:13 -0400450 SkASSERT(blitter);
451 if (fBurstCtx) {
452 // We can only burst shade one row at a time.
453 int x = clip.left();
454 for (int y = clip.top(); y < clip.bottom(); y++) {
455 this->burst_shade(x,y,clip.width());
456 (*blitter)(x,y, clip.width(),1);
mtklein9a5c47f2016-07-22 11:05:04 -0700457 }
Mike Klein45c16fa2017-07-18 18:15:13 -0400458 } else {
459 // If not bursting we can blit the entire mask at once.
460 (*blitter)(clip.left(),clip.top(), clip.width(),clip.height());
mtklein9a5c47f2016-07-22 11:05:04 -0700461 }
462}