1 | /* |
2 | * Copyright 2006 The Android Open Source Project |
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 "include/core/SkColorSpace.h" |
9 | #include "src/core/SkArenaAlloc.h" |
10 | #include "src/core/SkColorSpacePriv.h" |
11 | #include "src/core/SkColorSpaceXformSteps.h" |
12 | #include "src/core/SkCoreBlitters.h" |
13 | #include "src/core/SkOpts.h" |
14 | #include "src/core/SkRasterPipeline.h" |
15 | #include "src/core/SkSpriteBlitter.h" |
16 | |
17 | extern bool gUseSkVMBlitter; |
18 | |
19 | SkSpriteBlitter::SkSpriteBlitter(const SkPixmap& source) |
20 | : fSource(source) {} |
21 | |
22 | void SkSpriteBlitter::setup(const SkPixmap& dst, int left, int top, const SkPaint& paint) { |
23 | fDst = dst; |
24 | fLeft = left; |
25 | fTop = top; |
26 | fPaint = &paint; |
27 | } |
28 | |
29 | void SkSpriteBlitter::blitH(int x, int y, int width) { |
30 | SkDEBUGFAIL("how did we get here?" ); |
31 | |
32 | // Fallback to blitRect. |
33 | this->blitRect(x, y, width, 1); |
34 | } |
35 | |
36 | void SkSpriteBlitter::blitAntiH(int x, int y, const SkAlpha antialias[], const int16_t runs[]) { |
37 | SkDEBUGFAIL("how did we get here?" ); |
38 | |
39 | // No fallback strategy. |
40 | } |
41 | |
42 | void SkSpriteBlitter::blitV(int x, int y, int height, SkAlpha alpha) { |
43 | SkDEBUGFAIL("how did we get here?" ); |
44 | |
45 | // Fall back to superclass if the code gets here in release mode. |
46 | INHERITED::blitV(x, y, height, alpha); |
47 | } |
48 | |
49 | void SkSpriteBlitter::blitMask(const SkMask& mask, const SkIRect& clip) { |
50 | SkDEBUGFAIL("how did we get here?" ); |
51 | |
52 | // Fall back to superclass if the code gets here in release mode. |
53 | INHERITED::blitMask(mask, clip); |
54 | } |
55 | |
56 | /////////////////////////////////////////////////////////////////////////////// |
57 | |
58 | class SkSpriteBlitter_Memcpy final : public SkSpriteBlitter { |
59 | public: |
60 | static bool Supports(const SkPixmap& dst, const SkPixmap& src, const SkPaint& paint) { |
61 | // the caller has already inspected the colorspace on src and dst |
62 | SkASSERT(0 == SkColorSpaceXformSteps(src,dst).flags.mask()); |
63 | |
64 | if (dst.colorType() != src.colorType()) { |
65 | return false; |
66 | } |
67 | if (paint.getMaskFilter() || paint.getColorFilter() || paint.getImageFilter()) { |
68 | return false; |
69 | } |
70 | if (0xFF != paint.getAlpha()) { |
71 | return false; |
72 | } |
73 | SkBlendMode mode = paint.getBlendMode(); |
74 | return SkBlendMode::kSrc == mode || (SkBlendMode::kSrcOver == mode && src.isOpaque()); |
75 | } |
76 | |
77 | SkSpriteBlitter_Memcpy(const SkPixmap& src) |
78 | : INHERITED(src) {} |
79 | |
80 | void blitRect(int x, int y, int width, int height) override { |
81 | SkASSERT(fDst.colorType() == fSource.colorType()); |
82 | SkASSERT(width > 0 && height > 0); |
83 | |
84 | char* dst = (char*)fDst.writable_addr(x, y); |
85 | const char* src = (const char*)fSource.addr(x - fLeft, y - fTop); |
86 | const size_t dstRB = fDst.rowBytes(); |
87 | const size_t srcRB = fSource.rowBytes(); |
88 | const size_t bytesToCopy = width << fSource.shiftPerPixel(); |
89 | |
90 | while (height --> 0) { |
91 | memcpy(dst, src, bytesToCopy); |
92 | dst += dstRB; |
93 | src += srcRB; |
94 | } |
95 | } |
96 | |
97 | private: |
98 | typedef SkSpriteBlitter INHERITED; |
99 | }; |
100 | |
101 | class SkRasterPipelineSpriteBlitter : public SkSpriteBlitter { |
102 | public: |
103 | SkRasterPipelineSpriteBlitter(const SkPixmap& src, SkArenaAlloc* alloc, |
104 | sk_sp<SkShader> clipShader) |
105 | : INHERITED(src) |
106 | , fAlloc(alloc) |
107 | , fBlitter(nullptr) |
108 | , fSrcPtr{nullptr, 0} |
109 | , fClipShader(std::move(clipShader)) |
110 | {} |
111 | |
112 | void setup(const SkPixmap& dst, int left, int top, const SkPaint& paint) override { |
113 | fDst = dst; |
114 | fLeft = left; |
115 | fTop = top; |
116 | fPaintColor = paint.getColor4f(); |
117 | |
118 | SkRasterPipeline p(fAlloc); |
119 | p.append_load(fSource.colorType(), &fSrcPtr); |
120 | |
121 | if (fSource.colorType() == kAlpha_8_SkColorType) { |
122 | // The color for A8 images comes from the (sRGB) paint color. |
123 | p.append_set_rgb(fAlloc, fPaintColor); |
124 | p.append(SkRasterPipeline::premul); |
125 | } |
126 | if (auto dstCS = fDst.colorSpace()) { |
127 | auto srcCS = fSource.colorSpace(); |
128 | if (!srcCS || fSource.colorType() == kAlpha_8_SkColorType) { |
129 | // We treat untagged images as sRGB. |
130 | // A8 images get their r,g,b from the paint color, so they're also sRGB. |
131 | srcCS = sk_srgb_singleton(); |
132 | } |
133 | auto srcAT = fSource.isOpaque() ? kOpaque_SkAlphaType |
134 | : kPremul_SkAlphaType; |
135 | fAlloc->make<SkColorSpaceXformSteps>(srcCS, srcAT, |
136 | dstCS, kPremul_SkAlphaType) |
137 | ->apply(&p, fSource.colorType()); |
138 | } |
139 | if (fPaintColor.fA != 1.0f) { |
140 | p.append(SkRasterPipeline::scale_1_float, &fPaintColor.fA); |
141 | } |
142 | |
143 | bool is_opaque = fSource.isOpaque() && fPaintColor.fA == 1.0f; |
144 | fBlitter = SkCreateRasterPipelineBlitter(fDst, paint, p, is_opaque, fAlloc, fClipShader); |
145 | } |
146 | |
147 | void blitRect(int x, int y, int width, int height) override { |
148 | fSrcPtr.stride = fSource.rowBytesAsPixels(); |
149 | |
150 | // We really want fSrcPtr.pixels = fSource.addr(-fLeft, -fTop) here, but that asserts. |
151 | // Instead we ask for addr(-fLeft+x, -fTop+y), then back up (x,y) manually. |
152 | // Representing bpp as a size_t keeps all this math in size_t instead of int, |
153 | // which could wrap around with large enough fSrcPtr.stride and y. |
154 | size_t bpp = fSource.info().bytesPerPixel(); |
155 | fSrcPtr.pixels = (char*)fSource.addr(-fLeft+x, -fTop+y) - bpp * x |
156 | - bpp * y * fSrcPtr.stride; |
157 | |
158 | fBlitter->blitRect(x,y,width,height); |
159 | } |
160 | |
161 | private: |
162 | SkArenaAlloc* fAlloc; |
163 | SkBlitter* fBlitter; |
164 | SkRasterPipeline_MemoryCtx fSrcPtr; |
165 | SkColor4f fPaintColor; |
166 | sk_sp<SkShader> fClipShader; |
167 | |
168 | typedef SkSpriteBlitter INHERITED; |
169 | }; |
170 | |
171 | // returning null means the caller will call SkBlitter::Choose() and |
172 | // have wrapped the source bitmap inside a shader |
173 | SkBlitter* SkBlitter::ChooseSprite(const SkPixmap& dst, const SkPaint& paint, |
174 | const SkPixmap& source, int left, int top, |
175 | SkArenaAlloc* alloc, sk_sp<SkShader> clipShader) { |
176 | /* We currently ignore antialiasing and filtertype, meaning we will take our |
177 | special blitters regardless of these settings. Ignoring filtertype seems fine |
178 | since by definition there is no scale in the matrix. Ignoring antialiasing is |
179 | a bit of a hack, since we "could" pass in the fractional left/top for the bitmap, |
180 | and respect that by blending the edges of the bitmap against the device. To support |
181 | this we could either add more special blitters here, or detect antialiasing in the |
182 | paint and return null if it is set, forcing the client to take the slow shader case |
183 | (which does respect soft edges). |
184 | */ |
185 | SkASSERT(alloc != nullptr); |
186 | |
187 | if (gUseSkVMBlitter) { |
188 | // TODO: one day, focused SkVMBlitters with the sprite as a varying? |
189 | // For now, returning nullptr here will make it fall back to normal non-sprite blitting. |
190 | return nullptr; |
191 | } |
192 | |
193 | // TODO: in principle SkRasterPipelineSpriteBlitter could be made to handle this. |
194 | if (source.alphaType() == kUnpremul_SkAlphaType) { |
195 | return nullptr; |
196 | } |
197 | |
198 | SkSpriteBlitter* blitter = nullptr; |
199 | |
200 | if (0 == SkColorSpaceXformSteps(source,dst).flags.mask() && !clipShader) { |
201 | if (!blitter && SkSpriteBlitter_Memcpy::Supports(dst, source, paint)) { |
202 | blitter = alloc->make<SkSpriteBlitter_Memcpy>(source); |
203 | } |
204 | if (!blitter) { |
205 | switch (dst.colorType()) { |
206 | case kN32_SkColorType: |
207 | blitter = SkSpriteBlitter::ChooseL32(source, paint, alloc); |
208 | break; |
209 | case kRGB_565_SkColorType: |
210 | blitter = SkSpriteBlitter::ChooseL565(source, paint, alloc); |
211 | break; |
212 | case kAlpha_8_SkColorType: |
213 | blitter = SkSpriteBlitter::ChooseLA8(source, paint, alloc); |
214 | break; |
215 | default: |
216 | break; |
217 | } |
218 | } |
219 | } |
220 | if (!blitter && !paint.getMaskFilter()) { |
221 | blitter = alloc->make<SkRasterPipelineSpriteBlitter>(source, alloc, clipShader); |
222 | } |
223 | |
224 | if (blitter) { |
225 | blitter->setup(dst, left, top, paint); |
226 | } |
227 | return blitter; |
228 | } |
229 | |