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