1 2 /* 3 * Copyright 2007 The Android Open Source Project 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10 #ifndef SkBitmapProcState_DEFINED 11 #define SkBitmapProcState_DEFINED 12 13 #include "SkBitmap.h" 14 #include "SkBitmapFilter.h" 15 #include "SkMatrix.h" 16 #include "SkPaint.h" 17 #include "SkScaledImageCache.h" 18 19 #define FractionalInt_IS_64BIT 20 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) 27 #else 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) 33 #endif 34 35 class SkPaint; 36 struct SkConvolutionProcs; 37 38 struct SkBitmapProcState { 39 40 SkBitmapProcState(): fScaledCacheID(NULL), fBitmapFilter(NULL) {} 41 ~SkBitmapProcState(); 42 43 typedef void (*ShaderProc32)(const SkBitmapProcState&, int x, int y, 44 SkPMColor[], int count); 45 46 typedef void (*ShaderProc16)(const SkBitmapProcState&, int x, int y, 47 uint16_t[], int count); 48 49 typedef void (*MatrixProc)(const SkBitmapProcState&, 50 uint32_t bitmapXY[], 51 int count, 52 int x, int y); 53 54 typedef void (*SampleProc32)(const SkBitmapProcState&, 55 const uint32_t[], 56 int count, 57 SkPMColor colors[]); 58 59 typedef void (*SampleProc16)(const SkBitmapProcState&, 60 const uint32_t[], 61 int count, 62 uint16_t colors[]); 63 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 67 68 const SkBitmap* fBitmap; // chooseProcs - orig or scaled 69 SkMatrix fInvMatrix; // chooseProcs 70 SkMatrix::MapXYProc fInvProc; // chooseProcs 71 72 SkFractionalInt fInvSxFractionalInt; 73 SkFractionalInt fInvKyFractionalInt; 74 75 FixedTileProc fTileProcX; // chooseProcs 76 FixedTileProc fTileProcY; // chooseProcs 77 FixedTileLowBitsProc fTileLowBitsProcX; // chooseProcs 78 FixedTileLowBitsProc fTileLowBitsProcY; // chooseProcs 79 IntTileProc fIntTileProcY; // chooseProcs 80 SkFixed fFilterOneX; 81 SkFixed fFilterOneY; 82 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 91 92 /** The shader will let us know when we can release some of our resources 93 * like scaled bitmaps. 94 */ 95 96 void endContext(); 97 98 /** Platforms implement this, and can optionally overwrite only the 99 following fields: 100 101 fShaderProc32 102 fShaderProc16 103 fMatrixProc 104 fSampleProc32 105 fSampleProc32 106 107 They will already have valid function pointers, so a platform that does 108 not have an accelerated version can just leave that field as is. A valid 109 implementation can do nothing (see SkBitmapProcState_opts_none.cpp) 110 */ 111 void platformProcs(); 112 113 /** Platforms can also optionally overwrite the convolution functions 114 if we have SIMD versions of them. 115 */ 116 117 void platformConvolutionProcs(SkConvolutionProcs*); 118 119 /** Given the byte size of the index buffer to be passed to the matrix proc, 120 return the maximum number of resulting pixels that can be computed 121 (i.e. the number of SkPMColor values to be written by the sample proc). 122 This routine takes into account that filtering and scale-vs-affine 123 affect the amount of buffer space needed. 124 125 Only valid to call after chooseProcs (setContext) has been called. It is 126 safe to call this inside the shader's shadeSpan() method. 127 */ 128 int maxCountForBufferSize(size_t bufferSize) const; 129 130 // If a shader proc is present, then the corresponding matrix/sample procs 131 // are ignored 132 ShaderProc32 getShaderProc32() const { return fShaderProc32; } 133 ShaderProc16 getShaderProc16() const { return fShaderProc16; } 134 135 SkBitmapFilter* getBitmapFilter() const { return fBitmapFilter; } 136 137 #ifdef SK_DEBUG 138 MatrixProc getMatrixProc() const; 139 #else 140 MatrixProc getMatrixProc() const { return fMatrixProc; } 141 #endif 142 SampleProc32 getSampleProc32() const { return fSampleProc32; } 143 SampleProc16 getSampleProc16() const { return fSampleProc16; } 144 145 private: 146 friend class SkBitmapProcShader; 147 148 ShaderProc32 fShaderProc32; // chooseProcs 149 ShaderProc16 fShaderProc16; // chooseProcs 150 // These are used if the shaderproc is NULL 151 MatrixProc fMatrixProc; // chooseProcs 152 SampleProc32 fSampleProc32; // chooseProcs 153 SampleProc16 fSampleProc16; // chooseProcs 154 155 SkBitmap fOrigBitmap; // CONSTRUCTOR 156 SkBitmap fScaledBitmap; // chooseProcs 157 158 SkScaledImageCache::ID* fScaledCacheID; 159 160 MatrixProc chooseMatrixProc(bool trivial_matrix); 161 bool chooseProcs(const SkMatrix& inv, const SkPaint&); 162 ShaderProc32 chooseShaderProc32(); 163 164 // returns false if we did not try to scale the image. In that case, we 165 // will need to "lock" its pixels some other way. 166 bool possiblyScaleImage(); 167 168 // returns false if we failed to "lock" the pixels at all. Typically this 169 // means we have to abort the shader. 170 bool lockBaseBitmap(); 171 172 SkBitmapFilter* fBitmapFilter; 173 174 // If supported, sets fShaderProc32 and fShaderProc16 and returns true, 175 // otherwise returns false. 176 bool setBitmapFilterProcs(); 177 178 // Return false if we failed to setup for fast translate (e.g. overflow) 179 bool setupForTranslate(); 180 181 #ifdef SK_DEBUG 182 static void DebugMatrixProc(const SkBitmapProcState&, 183 uint32_t[], int count, int x, int y); 184 #endif 185 }; 186 187 /* Macros for packing and unpacking pairs of 16bit values in a 32bit uint. 188 Used to allow access to a stream of uint16_t either one at a time, or 189 2 at a time by unpacking a uint32_t 190 */ 191 #ifdef SK_CPU_BENDIAN 192 #define PACK_TWO_SHORTS(pri, sec) ((pri) << 16 | (sec)) 193 #define UNPACK_PRIMARY_SHORT(packed) ((uint32_t)(packed) >> 16) 194 #define UNPACK_SECONDARY_SHORT(packed) ((packed) & 0xFFFF) 195 #else 196 #define PACK_TWO_SHORTS(pri, sec) ((pri) | ((sec) << 16)) 197 #define UNPACK_PRIMARY_SHORT(packed) ((packed) & 0xFFFF) 198 #define UNPACK_SECONDARY_SHORT(packed) ((uint32_t)(packed) >> 16) 199 #endif 200 201 #ifdef SK_DEBUG 202 static inline uint32_t pack_two_shorts(U16CPU pri, U16CPU sec) { 203 SkASSERT((uint16_t)pri == pri); 204 SkASSERT((uint16_t)sec == sec); 205 return PACK_TWO_SHORTS(pri, sec); 206 } 207 #else 208 #define pack_two_shorts(pri, sec) PACK_TWO_SHORTS(pri, sec) 209 #endif 210 211 // These functions are generated via macros, but are exposed here so that 212 // platformProcs may test for them by name. 213 void S32_opaque_D32_filter_DX(const SkBitmapProcState& s, const uint32_t xy[], 214 int count, SkPMColor colors[]); 215 void S32_alpha_D32_filter_DX(const SkBitmapProcState& s, const uint32_t xy[], 216 int count, SkPMColor colors[]); 217 void S32_opaque_D32_filter_DXDY(const SkBitmapProcState& s, 218 const uint32_t xy[], int count, SkPMColor colors[]); 219 void S32_alpha_D32_filter_DXDY(const SkBitmapProcState& s, 220 const uint32_t xy[], int count, SkPMColor colors[]); 221 void ClampX_ClampY_filter_scale(const SkBitmapProcState& s, uint32_t xy[], 222 int count, int x, int y); 223 void ClampX_ClampY_nofilter_scale(const SkBitmapProcState& s, uint32_t xy[], 224 int count, int x, int y); 225 void ClampX_ClampY_filter_affine(const SkBitmapProcState& s, 226 uint32_t xy[], int count, int x, int y); 227 void ClampX_ClampY_nofilter_affine(const SkBitmapProcState& s, 228 uint32_t xy[], int count, int x, int y); 229 void S32_D16_filter_DX(const SkBitmapProcState& s, 230 const uint32_t* xy, int count, uint16_t* colors); 231 232 void highQualityFilter32(const SkBitmapProcState &s, int x, int y, 233 SkPMColor *SK_RESTRICT colors, int count); 234 void highQualityFilter16(const SkBitmapProcState &s, int x, int y, 235 uint16_t *SK_RESTRICT colors, int count); 236 237 238 #endif 239