3 * Copyright 2007 The Android Open Source Project
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #ifndef SkBitmapProcState_DEFINED
11 #define SkBitmapProcState_DEFINED
14 #include "SkBitmapFilter.h"
17 #include "SkScaledImageCache.h"
19 #define FractionalInt_IS_64BIT
21 #ifdef FractionalInt_IS_64BIT
22 typedef SkFixed48 SkFractionalInt;
23 #define SkScalarToFractionalInt(x) SkScalarToFixed48(x)
24 #define SkFractionalIntToFixed(x) SkFixed48ToFixed(x)
25 #define SkFixedToFractionalInt(x) SkFixedToFixed48(x)
26 #define SkFractionalIntToInt(x) SkFixed48ToInt(x)
28 typedef SkFixed SkFractionalInt;
29 #define SkScalarToFractionalInt(x) SkScalarToFixed(x)
30 #define SkFractionalIntToFixed(x) (x)
31 #define SkFixedToFractionalInt(x) (x)
32 #define SkFractionalIntToInt(x) ((x) >> 16)
36 struct SkConvolutionProcs;
38 struct SkBitmapProcState {
40 SkBitmapProcState(): fScaledCacheID(NULL), fBitmapFilter(NULL) {}
43 typedef void (*ShaderProc32)(const SkBitmapProcState&, int x, int y,
44 SkPMColor[], int count);
46 typedef void (*ShaderProc16)(const SkBitmapProcState&, int x, int y,
47 uint16_t[], int count);
49 typedef void (*MatrixProc)(const SkBitmapProcState&,
54 typedef void (*SampleProc32)(const SkBitmapProcState&,
59 typedef void (*SampleProc16)(const SkBitmapProcState&,
64 typedef U16CPU (*FixedTileProc)(SkFixed); // returns 0..0xFFFF
65 typedef U16CPU (*FixedTileLowBitsProc)(SkFixed, int); // returns 0..0xF
66 typedef U16CPU (*IntTileProc)(int value, int count); // returns 0..count-1
68 const SkBitmap* fBitmap; // chooseProcs - orig or scaled
69 SkMatrix fInvMatrix; // chooseProcs
70 SkMatrix::MapXYProc fInvProc; // chooseProcs
72 SkFractionalInt fInvSxFractionalInt;
73 SkFractionalInt fInvKyFractionalInt;
75 FixedTileProc fTileProcX; // chooseProcs
76 FixedTileProc fTileProcY; // chooseProcs
77 FixedTileLowBitsProc fTileLowBitsProcX; // chooseProcs
78 FixedTileLowBitsProc fTileLowBitsProcY; // chooseProcs
79 IntTileProc fIntTileProcY; // chooseProcs
83 SkPMColor fPaintPMColor; // chooseProcs - A8 config
84 SkFixed fInvSx; // chooseProcs
85 SkFixed fInvKy; // chooseProcs
86 uint16_t fAlphaScale; // chooseProcs
87 uint8_t fInvType; // chooseProcs
88 uint8_t fTileModeX; // CONSTRUCTOR
89 uint8_t fTileModeY; // CONSTRUCTOR
90 uint8_t fFilterLevel; // chooseProcs
92 /** Platforms implement this, and can optionally overwrite only the
101 They will already have valid function pointers, so a platform that does
102 not have an accelerated version can just leave that field as is. A valid
103 implementation can do nothing (see SkBitmapProcState_opts_none.cpp)
105 void platformProcs();
107 /** Platforms can also optionally overwrite the convolution functions
108 if we have SIMD versions of them.
111 void platformConvolutionProcs(SkConvolutionProcs*);
113 /** Given the byte size of the index buffer to be passed to the matrix proc,
114 return the maximum number of resulting pixels that can be computed
115 (i.e. the number of SkPMColor values to be written by the sample proc).
116 This routine takes into account that filtering and scale-vs-affine
117 affect the amount of buffer space needed.
119 Only valid to call after chooseProcs (setContext) has been called. It is
120 safe to call this inside the shader's shadeSpan() method.
122 int maxCountForBufferSize(size_t bufferSize) const;
124 // If a shader proc is present, then the corresponding matrix/sample procs
126 ShaderProc32 getShaderProc32() const { return fShaderProc32; }
127 ShaderProc16 getShaderProc16() const { return fShaderProc16; }
129 SkBitmapFilter* getBitmapFilter() const { return fBitmapFilter; }
132 MatrixProc getMatrixProc() const;
134 MatrixProc getMatrixProc() const { return fMatrixProc; }
136 SampleProc32 getSampleProc32() const { return fSampleProc32; }
137 SampleProc16 getSampleProc16() const { return fSampleProc16; }
140 friend class SkBitmapProcShader;
142 ShaderProc32 fShaderProc32; // chooseProcs
143 ShaderProc16 fShaderProc16; // chooseProcs
144 // These are used if the shaderproc is NULL
145 MatrixProc fMatrixProc; // chooseProcs
146 SampleProc32 fSampleProc32; // chooseProcs
147 SampleProc16 fSampleProc16; // chooseProcs
149 SkBitmap fOrigBitmap; // CONSTRUCTOR
150 SkBitmap fScaledBitmap; // chooseProcs
152 SkScaledImageCache::ID* fScaledCacheID;
154 MatrixProc chooseMatrixProc(bool trivial_matrix);
155 bool chooseProcs(const SkMatrix& inv, const SkPaint&);
156 ShaderProc32 chooseShaderProc32();
158 // returns false if we did not try to scale the image. In that case, we
159 // will need to "lock" its pixels some other way.
160 bool possiblyScaleImage();
162 // returns false if we failed to "lock" the pixels at all. Typically this
163 // means we have to abort the shader.
164 bool lockBaseBitmap();
166 SkBitmapFilter* fBitmapFilter;
168 // If supported, sets fShaderProc32 and fShaderProc16 and returns true,
169 // otherwise returns false.
170 bool setBitmapFilterProcs();
172 // Return false if we failed to setup for fast translate (e.g. overflow)
173 bool setupForTranslate();
176 static void DebugMatrixProc(const SkBitmapProcState&,
177 uint32_t[], int count, int x, int y);
181 /* Macros for packing and unpacking pairs of 16bit values in a 32bit uint.
182 Used to allow access to a stream of uint16_t either one at a time, or
183 2 at a time by unpacking a uint32_t
185 #ifdef SK_CPU_BENDIAN
186 #define PACK_TWO_SHORTS(pri, sec) ((pri) << 16 | (sec))
187 #define UNPACK_PRIMARY_SHORT(packed) ((uint32_t)(packed) >> 16)
188 #define UNPACK_SECONDARY_SHORT(packed) ((packed) & 0xFFFF)
190 #define PACK_TWO_SHORTS(pri, sec) ((pri) | ((sec) << 16))
191 #define UNPACK_PRIMARY_SHORT(packed) ((packed) & 0xFFFF)
192 #define UNPACK_SECONDARY_SHORT(packed) ((uint32_t)(packed) >> 16)
196 static inline uint32_t pack_two_shorts(U16CPU pri, U16CPU sec) {
197 SkASSERT((uint16_t)pri == pri);
198 SkASSERT((uint16_t)sec == sec);
199 return PACK_TWO_SHORTS(pri, sec);
202 #define pack_two_shorts(pri, sec) PACK_TWO_SHORTS(pri, sec)
205 // These functions are generated via macros, but are exposed here so that
206 // platformProcs may test for them by name.
207 void S32_opaque_D32_filter_DX(const SkBitmapProcState& s, const uint32_t xy[],
208 int count, SkPMColor colors[]);
209 void S32_alpha_D32_filter_DX(const SkBitmapProcState& s, const uint32_t xy[],
210 int count, SkPMColor colors[]);
211 void S32_opaque_D32_filter_DXDY(const SkBitmapProcState& s,
212 const uint32_t xy[], int count, SkPMColor colors[]);
213 void S32_alpha_D32_filter_DXDY(const SkBitmapProcState& s,
214 const uint32_t xy[], int count, SkPMColor colors[]);
215 void ClampX_ClampY_filter_scale(const SkBitmapProcState& s, uint32_t xy[],
216 int count, int x, int y);
217 void ClampX_ClampY_nofilter_scale(const SkBitmapProcState& s, uint32_t xy[],
218 int count, int x, int y);
219 void ClampX_ClampY_filter_affine(const SkBitmapProcState& s,
220 uint32_t xy[], int count, int x, int y);
221 void ClampX_ClampY_nofilter_affine(const SkBitmapProcState& s,
222 uint32_t xy[], int count, int x, int y);
223 void S32_D16_filter_DX(const SkBitmapProcState& s,
224 const uint32_t* xy, int count, uint16_t* colors);
226 void highQualityFilter32(const SkBitmapProcState &s, int x, int y,
227 SkPMColor *SK_RESTRICT colors, int count);
228 void highQualityFilter16(const SkBitmapProcState &s, int x, int y,
229 uint16_t *SK_RESTRICT colors, int count);