SkBitmapProcState.h 8.6 KB

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