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