Debian Codegraph @

src/core/SkBitmapProcState.h

raw
1 /*
2 * Copyright 2007 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 #ifndef SkBitmapProcState_DEFINED
9 #define SkBitmapProcState_DEFINED
10
11 #include "include/core/SkColor.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkPixmap.h"
14 #include "include/core/SkPoint.h"
15 #include "include/core/SkSamplingOptions.h"
16 #include "include/core/SkScalar.h"
17 #include "include/private/base/SkAssert.h"
18 #include "include/private/base/SkCPUTypes.h"
19 #include "include/private/base/SkDebug.h"
20 #include "include/private/base/SkFixed.h"
21 #include "src/base/SkArenaAlloc.h"
22
23 #include <cstddef>
24 #include <cstdint>
25
26 class SkImage_Base;
27 enum class SkTileMode;
28
29 struct SkBitmapProcState {
30 SkBitmapProcState(const SkImage_Base* image, SkTileMode tmx, SkTileMode tmy);
31
32 bool setup(const SkMatrix& inv, SkColor color, const SkSamplingOptions& sampling) {
33 return this->init(inv, color, sampling)
34 && this->chooseProcs();
35 }
36
37 using ShaderProc32 = void (*)(const void* ctx, int x, int y, SkPMColor[], int count);
38
39 using MatrixProc = void (*)(const SkBitmapProcState&,
40 uint32_t bitmapXY[],
41 int count,
42 int x, int y);
43
44 using SampleProc32 = void (*)(const SkBitmapProcState&,
45 const uint32_t[],
46 int count,
47 SkPMColor colors[]);
48
49 const SkImage_Base* fImage;
50
51 SkPixmap fPixmap;
52 SkMatrix fInvMatrix; // This changes based on tile mode.
53 SkAlpha fPaintAlpha;
54 SkTileMode fTileModeX;
55 SkTileMode fTileModeY;
56 bool fBilerp;
57
58 SkFixed3232 fInvSx;
59 SkFixed3232 fInvKy;
60
61 SkFixed fFilterOneX;
62 SkFixed fFilterOneY;
63
64 uint16_t fAlphaScale; // chooseProcs
65
66 /** Given the byte size of the index buffer to be passed to the matrix proc,
67 return the maximum number of resulting pixels that can be computed
68 (i.e. the number of SkPMColor values to be written by the sample proc).
69 This routine takes into account that filtering and scale-vs-affine
70 affect the amount of buffer space needed.
71
72 Only valid to call after chooseProcs (setContext) has been called. It is
73 safe to call this inside the shader's shadeSpan() method.
74 */
75 int maxCountForBufferSize(size_t bufferSize) const;
76
77 // If a shader proc is present, then the corresponding matrix/sample procs
78 // are ignored
79 ShaderProc32 getShaderProc32() const { return fShaderProc32; }
80
81 #ifdef SK_DEBUG
82 MatrixProc getMatrixProc() const;
83 #else
84 MatrixProc getMatrixProc() const { return fMatrixProc; }
85 #endif
86 SampleProc32 getSampleProc32() const { return fSampleProc32; }
87
88 private:
89 // found by inspection. If this is too small, we will allocate on the heap.
90 static constexpr size_t kBMStateSize = 136;
91 SkSTArenaAlloc<kBMStateSize> fAlloc;
92
93 ShaderProc32 fShaderProc32; // chooseProcs
94 // These are used if the shaderproc is nullptr
95 MatrixProc fMatrixProc; // chooseProcs
96 SampleProc32 fSampleProc32; // chooseProcs
97
98 bool init(const SkMatrix& inverse, SkAlpha, const SkSamplingOptions&);
99 bool chooseProcs();
100 MatrixProc chooseMatrixProc(bool trivial_matrix);
101 ShaderProc32 chooseShaderProc32();
102
103 // Return false if we failed to setup for fast translate (e.g. overflow)
104 bool setupForTranslate();
105
106 #ifdef SK_DEBUG
107 static void DebugMatrixProc(const SkBitmapProcState&,
108 uint32_t[], int count, int x, int y);
109 #endif
110 };
111
112 /* Macros for packing and unpacking pairs of 16bit values in a 32bit uint.
113 Used to allow access to a stream of uint16_t either one at a time, or
114 2 at a time by unpacking a uint32_t
115 */
116 #ifdef SK_CPU_BENDIAN
117 #define PACK_TWO_SHORTS(pri, sec) ((pri) << 16 | (sec))
118 #define UNPACK_PRIMARY_SHORT(packed) ((uint32_t)(packed) >> 16)
119 #define UNPACK_SECONDARY_SHORT(packed) ((packed) & 0xFFFF)
120 #else
121 #define PACK_TWO_SHORTS(pri, sec) ((pri) | ((sec) << 16))
122 #define UNPACK_PRIMARY_SHORT(packed) ((packed) & 0xFFFF)
123 #define UNPACK_SECONDARY_SHORT(packed) ((uint32_t)(packed) >> 16)
124 #endif
125
126 #ifdef SK_DEBUG
127 static inline uint32_t pack_two_shorts(U16CPU pri, U16CPU sec) {
128 SkASSERT((uint16_t)pri == pri);
129 SkASSERT((uint16_t)sec == sec);
130 return PACK_TWO_SHORTS(pri, sec);
131 }
132 #else
133 #define pack_two_shorts(pri, sec) PACK_TWO_SHORTS(pri, sec)
134 #endif
135
136 // Helper class for mapping the middle of pixel (x, y) into SkFixed3232 bitmap space.
137 // Discussion:
138 // Overall, this code takes a point in destination space, and uses the center of the pixel
139 // at (x, y) to determine the sample point in source space. It then adjusts the pixel by different
140 // amounts based in filtering and tiling.
141 // This code can be broken into two main cases based on filtering:
142 // * no filtering (nearest neighbor) - when using nearest neighbor filtering all tile modes reduce
143 // the sampled by one ulp. If a simple point pt lies precisely on XXX.1/2 then it forced down
144 // when positive making 1/2 + 1/2 = .999999 instead of 1.0.
145 // * filtering - in the filtering case, the code calculates the -1/2 shift for starting the
146 // bilerp kernel. There is a twist; there is a big difference between clamp and the other tile
147 // modes. In tile and repeat the matrix has been reduced by an additional 1/width and 1/height
148 // factor. This maps from destination space to [0, 1) (instead of source space) to allow easy
149 // modulo arithmetic. This means that the -1/2 needed by bilerp is actually 1/2 * 1/width for x
150 // and 1/2 * 1/height for y. This is what happens when the poorly named fFilterOne{X|Y} is
151 // divided by two.
152 class SkBitmapProcStateAutoMapper {
153 public:
154 SkBitmapProcStateAutoMapper(const SkBitmapProcState& s, int x, int y,
155 SkPoint* scalarPoint = nullptr) {
156 SkPoint pt = s.fInvMatrix.mapPoint({
157 SkIntToScalar(x) + SK_ScalarHalf,
158 SkIntToScalar(y) + SK_ScalarHalf,
159 });
160
161 SkFixed biasX = 0, biasY = 0;
162 if (s.fBilerp) {
163 biasX = s.fFilterOneX >> 1;
164 biasY = s.fFilterOneY >> 1;
165 } else {
166 // Our rasterizer biases upward. That is a rect from 0.5...1.5 fills pixel 1 and not
167 // pixel 0. To make an image that is mapped 1:1 with device pixels but at a half pixel
168 // offset select every pixel from the src image once we make exact integer pixel sample
169 // values round down not up. Note that a mirror mapping will not have this property.
170 biasX = 1;
171 biasY = 1;
172 }
173
174 // punt to unsigned for defined underflow behavior
175 fX = (SkFixed3232)((uint64_t)SkScalarToFixed3232(pt.x()) -
176 (uint64_t)SkFixedToFixed3232(biasX));
177 fY = (SkFixed3232)((uint64_t)SkScalarToFixed3232(pt.y()) -
178 (uint64_t)SkFixedToFixed3232(biasY));
179
180 if (scalarPoint) {
181 scalarPoint->set(pt.x() - SkFixedToScalar(biasX),
182 pt.y() - SkFixedToScalar(biasY));
183 }
184 }
185
186 SkFixed3232 fixed3232X() const { return fX; }
187 SkFixed3232 fixed3232Y() const { return fY; }
188
189 SkFixed fixedX() const { return SkFixed3232ToFixed(fX); }
190 SkFixed fixedY() const { return SkFixed3232ToFixed(fY); }
191
192 int intX() const { return SkFixed3232ToInt(fX); }
193 int intY() const { return SkFixed3232ToInt(fY); }
194
195 private:
196 SkFixed3232 fX, fY;
197 };
198
199 namespace sktests {
200 // f is the value to pack, max is the largest the value can be.
201 uint32_t pack_clamp(SkFixed f, unsigned max);
202 // As above, but width is the width of the pretend bitmap.
203 uint32_t pack_repeat(SkFixed f, unsigned max, size_t width);
204 uint32_t pack_mirror(SkFixed f, unsigned max, size_t width);
205 }
206
207 namespace SkOpts {
208 // SkBitmapProcState optimized Shader, Sample, or Matrix procs.
209 extern void (*S32_alpha_D32_filter_DX)(const SkBitmapProcState&,
210 const uint32_t* xy, int count, SkPMColor*);
211 extern void (*S32_alpha_D32_filter_DXDY)(const SkBitmapProcState&,
212 const uint32_t* xy, int count, SkPMColor*);
213
214 void Init_BitmapProcState();
215 } // namespace SkOpts
216
217 #endif