blob: daf34a2a6d800eb99fafde60681f72d36473d3d3 [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
8#include "SkBlitter.h"
Mike Kleine902f8d2016-10-26 15:32:26 -04009#include "SkBlendModePriv.h"
mtklein9a5c47f2016-07-22 11:05:04 -070010#include "SkColor.h"
11#include "SkColorFilter.h"
Mike Kleinbaaf8ad2016-09-29 09:04:15 -040012#include "SkOpts.h"
mtklein9a5c47f2016-07-22 11:05:04 -070013#include "SkPM4f.h"
mtklein9a5c47f2016-07-22 11:05:04 -070014#include "SkRasterPipeline.h"
15#include "SkShader.h"
mtklein9a5c47f2016-07-22 11:05:04 -070016#include "SkXfermode.h"
17
18
19class SkRasterPipelineBlitter : public SkBlitter {
20public:
21 static SkBlitter* Create(const SkPixmap&, const SkPaint&, SkTBlitterAllocator*);
22
23 SkRasterPipelineBlitter(SkPixmap dst,
24 SkRasterPipeline shader,
Mike Kleine902f8d2016-10-26 15:32:26 -040025 SkBlendMode blend,
mtklein9a5c47f2016-07-22 11:05:04 -070026 SkPM4f paintColor)
27 : fDst(dst)
28 , fShader(shader)
Mike Kleine902f8d2016-10-26 15:32:26 -040029 , fBlend(blend)
mtklein9a5c47f2016-07-22 11:05:04 -070030 , fPaintColor(paintColor)
31 {}
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 Kleine902f8d2016-10-26 15:32:26 -040042 void append_load_d(SkRasterPipeline*) const;
43 void append_store (SkRasterPipeline*) const;
44 void append_blend (SkRasterPipeline*) const;
mtklein8e4373f2016-07-22 14:20:27 -070045
mtklein9a5c47f2016-07-22 11:05:04 -070046 SkPixmap fDst;
Mike Kleine902f8d2016-10-26 15:32:26 -040047 SkRasterPipeline fShader;
48 SkBlendMode fBlend;
mtklein9a5c47f2016-07-22 11:05:04 -070049 SkPM4f fPaintColor;
50
Mike Kleinbd3fe472016-10-25 15:43:46 -040051 // These functions are compiled lazily when first used.
52 std::function<void(size_t, size_t)> fBlitH = nullptr,
53 fBlitAntiH = nullptr,
54 fBlitMaskA8 = nullptr,
55 fBlitMaskLCD16 = nullptr;
56
57 // These values are pointed to by the compiled blit functions
58 // above, which allows us to adjust them from call to call.
59 void* fDstPtr = nullptr;
60 const void* fMaskPtr = nullptr;
61 float fConstantCoverage = 0.0f;
62
mtklein9a5c47f2016-07-22 11:05:04 -070063 typedef SkBlitter INHERITED;
64};
65
66SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst,
67 const SkPaint& paint,
68 SkTBlitterAllocator* alloc) {
69 return SkRasterPipelineBlitter::Create(dst, paint, alloc);
70}
71
mtklein8e4373f2016-07-22 14:20:27 -070072static bool supported(const SkImageInfo& info) {
mtklein8e4373f2016-07-22 14:20:27 -070073 switch (info.colorType()) {
mtklein4e90e3e2016-07-25 14:35:31 -070074 case kN32_SkColorType: return info.gammaCloseToSRGB();
75 case kRGBA_F16_SkColorType: return true;
76 case kRGB_565_SkColorType: return true;
77 default: return false;
mtklein8e4373f2016-07-22 14:20:27 -070078 }
79}
mtklein9a5c47f2016-07-22 11:05:04 -070080
81template <typename Effect>
82static bool append_effect_stages(const Effect* effect, SkRasterPipeline* pipeline) {
83 return !effect || effect->appendStages(pipeline);
84}
85
86
87SkBlitter* SkRasterPipelineBlitter::Create(const SkPixmap& dst,
88 const SkPaint& paint,
89 SkTBlitterAllocator* alloc) {
mtklein8e4373f2016-07-22 14:20:27 -070090 if (!supported(dst.info())) {
91 return nullptr;
mtklein9a5c47f2016-07-22 11:05:04 -070092 }
93 if (paint.getShader()) {
94 return nullptr; // TODO: need to work out how shaders and their contexts work
95 }
Mike Kleine902f8d2016-10-26 15:32:26 -040096 SkBlendMode blend = paint.getBlendMode();
97 if (!SkBlendMode_AppendStages(blend)) {
98 return nullptr; // TODO
99 }
mtklein9a5c47f2016-07-22 11:05:04 -0700100
Mike Kleine902f8d2016-10-26 15:32:26 -0400101 SkRasterPipeline shader, colorFilter;
102 if (paint.getColorFilter() && !paint.getColorFilter()->appendStages(&colorFilter)) {
mtklein9a5c47f2016-07-22 11:05:04 -0700103 return nullptr;
104 }
105
mtklein8e4373f2016-07-22 14:20:27 -0700106 uint32_t paintColor = paint.getColor();
107
108 SkColor4f color;
mtklein4e90e3e2016-07-25 14:35:31 -0700109 if (SkImageInfoIsGammaCorrect(dst.info())) {
mtklein8e4373f2016-07-22 14:20:27 -0700110 color = SkColor4f::FromColor(paintColor);
111 } else {
112 swizzle_rb(SkNx_cast<float>(Sk4b::Load(&paintColor)) * (1/255.0f)).store(&color);
113 }
114
Mike Kleine902f8d2016-10-26 15:32:26 -0400115 auto blitter = alloc->createT<SkRasterPipelineBlitter>(dst, shader, blend, color.premul());
mtklein9a5c47f2016-07-22 11:05:04 -0700116
117 if (!paint.getShader()) {
Mike Kleinfa9f2412016-09-29 13:40:01 -0400118 blitter->fShader.append(SkRasterPipeline::constant_color, &blitter->fPaintColor);
mtklein9a5c47f2016-07-22 11:05:04 -0700119 }
Mike Kleine902f8d2016-10-26 15:32:26 -0400120 blitter->fShader.extend(colorFilter);
mtklein9a5c47f2016-07-22 11:05:04 -0700121
122 return blitter;
123}
124
Mike Kleine902f8d2016-10-26 15:32:26 -0400125void SkRasterPipelineBlitter::append_load_d(SkRasterPipeline* p) const {
mtklein8e4373f2016-07-22 14:20:27 -0700126 SkASSERT(supported(fDst.info()));
127
128 switch (fDst.info().colorType()) {
129 case kN32_SkColorType:
130 if (fDst.info().gammaCloseToSRGB()) {
Mike Kleine902f8d2016-10-26 15:32:26 -0400131 p->append(SkRasterPipeline::load_d_srgb, &fDstPtr);
mtklein8e4373f2016-07-22 14:20:27 -0700132 }
133 break;
mtklein4e90e3e2016-07-25 14:35:31 -0700134 case kRGBA_F16_SkColorType:
Mike Kleine902f8d2016-10-26 15:32:26 -0400135 p->append(SkRasterPipeline::load_d_f16, &fDstPtr);
mtklein4e90e3e2016-07-25 14:35:31 -0700136 break;
mtklein8e4373f2016-07-22 14:20:27 -0700137 case kRGB_565_SkColorType:
Mike Kleine902f8d2016-10-26 15:32:26 -0400138 p->append(SkRasterPipeline::load_d_565, &fDstPtr);
mtklein8e4373f2016-07-22 14:20:27 -0700139 break;
140 default: break;
141 }
142}
143
Mike Kleine902f8d2016-10-26 15:32:26 -0400144void SkRasterPipelineBlitter::append_store(SkRasterPipeline* p) const {
mtklein8e4373f2016-07-22 14:20:27 -0700145 SkASSERT(supported(fDst.info()));
146
147 switch (fDst.info().colorType()) {
148 case kN32_SkColorType:
149 if (fDst.info().gammaCloseToSRGB()) {
Mike Kleine902f8d2016-10-26 15:32:26 -0400150 p->append(SkRasterPipeline::store_srgb, &fDstPtr);
mtklein8e4373f2016-07-22 14:20:27 -0700151 }
152 break;
mtklein4e90e3e2016-07-25 14:35:31 -0700153 case kRGBA_F16_SkColorType:
Mike Kleine902f8d2016-10-26 15:32:26 -0400154 p->append(SkRasterPipeline::store_f16, &fDstPtr);
mtklein4e90e3e2016-07-25 14:35:31 -0700155 break;
mtklein8e4373f2016-07-22 14:20:27 -0700156 case kRGB_565_SkColorType:
Mike Kleine902f8d2016-10-26 15:32:26 -0400157 p->append(SkRasterPipeline::store_565, &fDstPtr);
mtklein8e4373f2016-07-22 14:20:27 -0700158 break;
159 default: break;
160 }
161}
162
Mike Kleine902f8d2016-10-26 15:32:26 -0400163void SkRasterPipelineBlitter::append_blend(SkRasterPipeline* p) const {
164 SkAssertResult(SkBlendMode_AppendStages(fBlend, p));
165}
Mike Kleine9f74b82016-10-25 13:31:21 -0400166
mtklein9a5c47f2016-07-22 11:05:04 -0700167void SkRasterPipelineBlitter::blitH(int x, int y, int w) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400168 if (!fBlitH) {
169 SkRasterPipeline p;
170 p.extend(fShader);
Mike Kleine902f8d2016-10-26 15:32:26 -0400171 this->append_load_d(&p);
172 this->append_blend(&p);
173 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400174 fBlitH = p.compile();
175 }
mtklein9a5c47f2016-07-22 11:05:04 -0700176
Mike Kleinbd3fe472016-10-25 15:43:46 -0400177 fDstPtr = fDst.writable_addr(0,y);
178 fBlitH(x,w);
mtklein9a5c47f2016-07-22 11:05:04 -0700179}
180
181void SkRasterPipelineBlitter::blitAntiH(int x, int y, const SkAlpha aa[], const int16_t runs[]) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400182 if (!fBlitAntiH) {
183 SkRasterPipeline p;
184 p.extend(fShader);
Mike Kleine902f8d2016-10-26 15:32:26 -0400185 this->append_load_d(&p);
186 this->append_blend(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400187 p.append(SkRasterPipeline::lerp_constant_float, &fConstantCoverage);
Mike Kleine902f8d2016-10-26 15:32:26 -0400188 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400189 fBlitAntiH = p.compile();
190 }
mtklein9a5c47f2016-07-22 11:05:04 -0700191
Mike Kleinbd3fe472016-10-25 15:43:46 -0400192 fDstPtr = fDst.writable_addr(0,y);
mtklein9a5c47f2016-07-22 11:05:04 -0700193 for (int16_t run = *runs; run > 0; run = *runs) {
Mike Kleinbd3fe472016-10-25 15:43:46 -0400194 fConstantCoverage = *aa * (1/255.0f);
195 fBlitAntiH(x, run);
mtklein9a5c47f2016-07-22 11:05:04 -0700196
197 x += run;
198 runs += run;
199 aa += run;
200 }
201}
202
203void SkRasterPipelineBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
204 if (mask.fFormat == SkMask::kBW_Format) {
205 // TODO: native BW masks?
206 return INHERITED::blitMask(mask, clip);
207 }
208
Mike Kleinbd3fe472016-10-25 15:43:46 -0400209 if (mask.fFormat == SkMask::kA8_Format && !fBlitMaskA8) {
mtklein9a5c47f2016-07-22 11:05:04 -0700210 SkRasterPipeline p;
211 p.extend(fShader);
Mike Kleine902f8d2016-10-26 15:32:26 -0400212 this->append_load_d(&p);
213 this->append_blend(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400214 p.append(SkRasterPipeline::lerp_u8, &fMaskPtr);
Mike Kleine902f8d2016-10-26 15:32:26 -0400215 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400216 fBlitMaskA8 = p.compile();
217 }
218
219 if (mask.fFormat == SkMask::kLCD16_Format && !fBlitMaskLCD16) {
220 SkRasterPipeline p;
221 p.extend(fShader);
Mike Kleine902f8d2016-10-26 15:32:26 -0400222 this->append_load_d(&p);
223 this->append_blend(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400224 p.append(SkRasterPipeline::lerp_565, &fMaskPtr);
Mike Kleine902f8d2016-10-26 15:32:26 -0400225 this->append_store(&p);
Mike Kleinbd3fe472016-10-25 15:43:46 -0400226 fBlitMaskLCD16 = p.compile();
227 }
228
229 int x = clip.left();
230 for (int y = clip.top(); y < clip.bottom(); y++) {
231 fDstPtr = fDst.writable_addr(0,y);
232
mtklein9a5c47f2016-07-22 11:05:04 -0700233 switch (mask.fFormat) {
234 case SkMask::kA8_Format:
Mike Kleinbd3fe472016-10-25 15:43:46 -0400235 fMaskPtr = mask.getAddr8(x,y)-x;
236 fBlitMaskA8(x, clip.width());
mtklein9a5c47f2016-07-22 11:05:04 -0700237 break;
238 case SkMask::kLCD16_Format:
Mike Kleinbd3fe472016-10-25 15:43:46 -0400239 fMaskPtr = mask.getAddrLCD16(x,y)-x;
240 fBlitMaskLCD16(x, clip.width());
mtklein9a5c47f2016-07-22 11:05:04 -0700241 break;
Mike Kleinbd3fe472016-10-25 15:43:46 -0400242 default:
243 // TODO
244 break;
mtklein9a5c47f2016-07-22 11:05:04 -0700245 }
mtklein9a5c47f2016-07-22 11:05:04 -0700246 }
247}