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/SkBitmap.h" 12 #include "include/core/SkPaint.h" 13 #include "include/core/SkShader.h" 14 #include "include/private/SkFixed.h" 15 #include "include/private/SkFloatBits.h" 16 #include "include/private/SkTemplates.h" 17 #include "src/core/SkArenaAlloc.h" 18 #include "src/core/SkBitmapController.h" 19 #include "src/core/SkMatrixPriv.h" 20 #include "src/core/SkMipMap.h" 21 22 typedef SkFixed3232 SkFractionalInt; 23 #define SkScalarToFractionalInt(x) SkScalarToFixed3232(x) 24 #define SkFractionalIntToFixed(x) SkFixed3232ToFixed(x) 25 #define SkFixedToFractionalInt(x) SkFixedToFixed3232(x) 26 #define SkFractionalIntToInt(x) SkFixed3232ToInt(x) 27 28 class SkPaint; 29 30 struct SkBitmapProcInfo { 31 SkBitmapProcInfo(const SkImage_Base*, SkTileMode tmx, SkTileMode tmy); 32 ~SkBitmapProcInfo(); 33 34 const SkImage_Base* fImage; 35 36 SkPixmap fPixmap; 37 SkMatrix fInvMatrix; // This changes based on tile mode. 38 // TODO: combine fInvMatrix and fRealInvMatrix. 39 SkMatrix fRealInvMatrix; // The actual inverse matrix. 40 SkColor fPaintColor; 41 SkTileMode fTileModeX; 42 SkTileMode fTileModeY; 43 SkFilterQuality fFilterQuality; 44 SkMatrix::TypeMask fInvType; 45 46 bool init(const SkMatrix& inverse, const SkPaint&); 47 48 private: 49 enum { 50 kBMStateSize = 136 // found by inspection. if too small, we will call new/delete 51 }; 52 SkSTArenaAlloc<kBMStateSize> fAlloc; 53 SkBitmapController::State* fBMState; 54 }; 55 56 struct SkBitmapProcState : public SkBitmapProcInfo { SkBitmapProcStateSkBitmapProcState57 SkBitmapProcState(const SkImage_Base* image, SkTileMode tmx, SkTileMode tmy) 58 : SkBitmapProcInfo(image, tmx, tmy) {} 59 setupSkBitmapProcState60 bool setup(const SkMatrix& inv, const SkPaint& paint) { 61 return this->init(inv, paint) && this->chooseProcs(); 62 } 63 64 typedef void (*ShaderProc32)(const void* ctx, int x, int y, SkPMColor[], int count); 65 66 typedef void (*MatrixProc)(const SkBitmapProcState&, 67 uint32_t bitmapXY[], 68 int count, 69 int x, int y); 70 71 typedef void (*SampleProc32)(const SkBitmapProcState&, 72 const uint32_t[], 73 int count, 74 SkPMColor colors[]); 75 76 SkMatrixPriv::MapXYProc fInvProc; // chooseProcs 77 SkFractionalInt fInvSxFractionalInt; 78 SkFractionalInt fInvKyFractionalInt; 79 80 SkFixed fFilterOneX; 81 SkFixed fFilterOneY; 82 83 SkFixed fInvSx; // chooseProcs 84 SkFixed fInvKy; // chooseProcs 85 SkPMColor fPaintPMColor; // chooseProcs - A8 config 86 uint16_t fAlphaScale; // chooseProcs 87 88 /** Given the byte size of the index buffer to be passed to the matrix proc, 89 return the maximum number of resulting pixels that can be computed 90 (i.e. the number of SkPMColor values to be written by the sample proc). 91 This routine takes into account that filtering and scale-vs-affine 92 affect the amount of buffer space needed. 93 94 Only valid to call after chooseProcs (setContext) has been called. It is 95 safe to call this inside the shader's shadeSpan() method. 96 */ 97 int maxCountForBufferSize(size_t bufferSize) const; 98 99 // If a shader proc is present, then the corresponding matrix/sample procs 100 // are ignored getShaderProc32SkBitmapProcState101 ShaderProc32 getShaderProc32() const { return fShaderProc32; } 102 103 #ifdef SK_DEBUG 104 MatrixProc getMatrixProc() const; 105 #else getMatrixProcSkBitmapProcState106 MatrixProc getMatrixProc() const { return fMatrixProc; } 107 #endif getSampleProc32SkBitmapProcState108 SampleProc32 getSampleProc32() const { return fSampleProc32; } 109 110 private: 111 ShaderProc32 fShaderProc32; // chooseProcs 112 // These are used if the shaderproc is nullptr 113 MatrixProc fMatrixProc; // chooseProcs 114 SampleProc32 fSampleProc32; // chooseProcs 115 116 MatrixProc chooseMatrixProc(bool trivial_matrix); 117 bool chooseProcs(); // caller must have called init() first (on our base-class) 118 ShaderProc32 chooseShaderProc32(); 119 120 // Return false if we failed to setup for fast translate (e.g. overflow) 121 bool setupForTranslate(); 122 123 #ifdef SK_DEBUG 124 static void DebugMatrixProc(const SkBitmapProcState&, 125 uint32_t[], int count, int x, int y); 126 #endif 127 }; 128 129 /* Macros for packing and unpacking pairs of 16bit values in a 32bit uint. 130 Used to allow access to a stream of uint16_t either one at a time, or 131 2 at a time by unpacking a uint32_t 132 */ 133 #ifdef SK_CPU_BENDIAN 134 #define PACK_TWO_SHORTS(pri, sec) ((pri) << 16 | (sec)) 135 #define UNPACK_PRIMARY_SHORT(packed) ((uint32_t)(packed) >> 16) 136 #define UNPACK_SECONDARY_SHORT(packed) ((packed) & 0xFFFF) 137 #else 138 #define PACK_TWO_SHORTS(pri, sec) ((pri) | ((sec) << 16)) 139 #define UNPACK_PRIMARY_SHORT(packed) ((packed) & 0xFFFF) 140 #define UNPACK_SECONDARY_SHORT(packed) ((uint32_t)(packed) >> 16) 141 #endif 142 143 #ifdef SK_DEBUG pack_two_shorts(U16CPU pri,U16CPU sec)144 static inline uint32_t pack_two_shorts(U16CPU pri, U16CPU sec) { 145 SkASSERT((uint16_t)pri == pri); 146 SkASSERT((uint16_t)sec == sec); 147 return PACK_TWO_SHORTS(pri, sec); 148 } 149 #else 150 #define pack_two_shorts(pri, sec) PACK_TWO_SHORTS(pri, sec) 151 #endif 152 153 // Helper class for mapping the middle of pixel (x, y) into SkFractionalInt bitmap space. 154 // Discussion: 155 // Overall, this code takes a point in destination space, and uses the center of the pixel 156 // at (x, y) to determine the sample point in source space. It then adjusts the pixel by different 157 // amounts based in filtering and tiling. 158 // This code can be broken into two main cases based on filtering: 159 // * no filtering (nearest neighbor) - when using nearest neighbor filtering all tile modes reduce 160 // the sampled by one ulp. If a simple point pt lies precisely on XXX.1/2 then it forced down 161 // when positive making 1/2 + 1/2 = .999999 instead of 1.0. 162 // * filtering - in the filtering case, the code calculates the -1/2 shift for starting the 163 // bilerp kernel. There is a twist; there is a big difference between clamp and the other tile 164 // modes. In tile and repeat the matrix has been reduced by an additional 1/width and 1/height 165 // factor. This maps from destination space to [0, 1) (instead of source space) to allow easy 166 // modulo arithmetic. This means that the -1/2 needed by bilerp is actually 1/2 * 1/width for x 167 // and 1/2 * 1/height for y. This is what happens when the poorly named fFilterOne{X|Y} is 168 // divided by two. 169 class SkBitmapProcStateAutoMapper { 170 public: 171 SkBitmapProcStateAutoMapper(const SkBitmapProcState& s, int x, int y, 172 SkPoint* scalarPoint = nullptr) { 173 SkPoint pt; 174 s.fInvProc(s.fInvMatrix, 175 SkIntToScalar(x) + SK_ScalarHalf, 176 SkIntToScalar(y) + SK_ScalarHalf, &pt); 177 178 SkFixed biasX, biasY; 179 if (s.fFilterQuality == kNone_SkFilterQuality) { 180 // SkFixed epsilon bias to ensure inverse-mapped bitmap coordinates are rounded 181 // consistently WRT geometry. Note that we only need the bias for positive scales: 182 // for negative scales, the rounding is intrinsically correct. 183 // We scale it to persist SkFractionalInt -> SkFixed conversions. 184 biasX = (s.fInvMatrix.getScaleX() > 0); 185 biasY = (s.fInvMatrix.getScaleY() > 0); 186 } else { 187 biasX = s.fFilterOneX >> 1; 188 biasY = s.fFilterOneY >> 1; 189 } 190 191 // punt to unsigned for defined underflow behavior 192 fX = (SkFractionalInt)((uint64_t)SkScalarToFractionalInt(pt.x()) - 193 (uint64_t)SkFixedToFractionalInt(biasX)); 194 fY = (SkFractionalInt)((uint64_t)SkScalarToFractionalInt(pt.y()) - 195 (uint64_t)SkFixedToFractionalInt(biasY)); 196 197 if (scalarPoint) { 198 scalarPoint->set(pt.x() - SkFixedToScalar(biasX), 199 pt.y() - SkFixedToScalar(biasY)); 200 } 201 } 202 fractionalIntX()203 SkFractionalInt fractionalIntX() const { return fX; } fractionalIntY()204 SkFractionalInt fractionalIntY() const { return fY; } 205 fixedX()206 SkFixed fixedX() const { return SkFractionalIntToFixed(fX); } fixedY()207 SkFixed fixedY() const { return SkFractionalIntToFixed(fY); } 208 intX()209 int intX() const { return SkFractionalIntToInt(fX); } intY()210 int intY() const { return SkFractionalIntToInt(fY); } 211 212 private: 213 SkFractionalInt fX, fY; 214 }; 215 216 #endif 217