reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #ifndef SkBitmapProcState_DEFINED |
| 9 | #define SkBitmapProcState_DEFINED |
| 10 | |
| 11 | #include "SkBitmap.h" |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 12 | #include "SkBitmapController.h" |
reed | 013e9e3 | 2015-09-15 14:46:27 -0700 | [diff] [blame] | 13 | #include "SkBitmapProvider.h" |
Mike Reed | e32500f | 2017-07-19 17:20:37 -0400 | [diff] [blame] | 14 | #include "SkFixed.h" |
fmalita | aed4d32 | 2015-12-15 06:48:48 -0800 | [diff] [blame] | 15 | #include "SkFloatBits.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | #include "SkMatrix.h" |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 17 | #include "SkMipMap.h" |
reed@google.com | cee9dcb | 2013-09-13 16:04:49 +0000 | [diff] [blame] | 18 | #include "SkPaint.h" |
reed | 013e9e3 | 2015-09-15 14:46:27 -0700 | [diff] [blame] | 19 | #include "SkShader.h" |
bungeman | f3c15b7 | 2015-08-19 11:56:48 -0700 | [diff] [blame] | 20 | #include "SkTemplates.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | |
mtklein | 1794651 | 2014-11-21 12:10:33 -0800 | [diff] [blame] | 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) |
reed@google.com | 4bc0a9d | 2012-03-07 21:47:41 +0000 | [diff] [blame] | 27 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | class SkPaint; |
| 29 | |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 30 | struct SkBitmapProcInfo { |
Brian Osman | 13bf622 | 2016-12-06 10:34:51 -0500 | [diff] [blame] | 31 | SkBitmapProcInfo(const SkBitmapProvider&, SkShader::TileMode tmx, SkShader::TileMode tmy); |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 32 | ~SkBitmapProcInfo(); |
| 33 | |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 34 | const SkBitmapProvider fProvider; |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 35 | |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 36 | SkPixmap fPixmap; |
| 37 | SkMatrix fInvMatrix; // This changes based on tile mode. |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 38 | // TODO: combine fInvMatrix and fRealInvMatrix. |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 39 | SkMatrix fRealInvMatrix; // The actual inverse matrix. |
| 40 | SkColor fPaintColor; |
| 41 | SkShader::TileMode fTileModeX; |
| 42 | SkShader::TileMode fTileModeY; |
| 43 | SkFilterQuality fFilterQuality; |
| 44 | SkMatrix::TypeMask fInvType; |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 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 | SkAlignedSStorage<kBMStateSize> fBMStateStorage; |
| 53 | SkBitmapController::State* fBMState; |
| 54 | }; |
| 55 | |
| 56 | struct SkBitmapProcState : public SkBitmapProcInfo { |
Brian Osman | 13bf622 | 2016-12-06 10:34:51 -0500 | [diff] [blame] | 57 | SkBitmapProcState(const SkBitmapProvider& prov, SkShader::TileMode tmx, SkShader::TileMode tmy) |
| 58 | : SkBitmapProcInfo(prov, tmx, tmy) {} |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 59 | |
| 60 | bool setup(const SkMatrix& inv, const SkPaint& paint) { |
| 61 | return this->init(inv, paint) && this->chooseProcs(); |
| 62 | } |
reed | 6404542 | 2015-06-04 06:31:31 -0700 | [diff] [blame] | 63 | |
herb | c7a784c | 2015-12-18 09:52:15 -0800 | [diff] [blame] | 64 | typedef void (*ShaderProc32)(const void* ctx, int x, int y, SkPMColor[], int count); |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 65 | |
herb | c7a784c | 2015-12-18 09:52:15 -0800 | [diff] [blame] | 66 | typedef void (*ShaderProc16)(const void* ctx, int x, int y, uint16_t[], int count); |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 67 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | typedef void (*MatrixProc)(const SkBitmapProcState&, |
| 69 | uint32_t bitmapXY[], |
| 70 | int count, |
| 71 | int x, int y); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 72 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | typedef void (*SampleProc32)(const SkBitmapProcState&, |
| 74 | const uint32_t[], |
| 75 | int count, |
| 76 | SkPMColor colors[]); |
| 77 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | typedef U16CPU (*FixedTileProc)(SkFixed); // returns 0..0xFFFF |
reed@android.com | 07d1f00 | 2009-08-13 19:35:48 +0000 | [diff] [blame] | 79 | typedef U16CPU (*IntTileProc)(int value, int count); // returns 0..count-1 |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 80 | |
reed@android.com | eef375b | 2009-08-03 14:45:45 +0000 | [diff] [blame] | 81 | SkMatrix::MapXYProc fInvProc; // chooseProcs |
reed@google.com | 4bc0a9d | 2012-03-07 21:47:41 +0000 | [diff] [blame] | 82 | SkFractionalInt fInvSxFractionalInt; |
reed@google.com | 411215a | 2012-03-08 20:13:46 +0000 | [diff] [blame] | 83 | SkFractionalInt fInvKyFractionalInt; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 84 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | FixedTileProc fTileProcX; // chooseProcs |
| 86 | FixedTileProc fTileProcY; // chooseProcs |
reed@android.com | 07d1f00 | 2009-08-13 19:35:48 +0000 | [diff] [blame] | 87 | IntTileProc fIntTileProcY; // chooseProcs |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | SkFixed fFilterOneX; |
| 89 | SkFixed fFilterOneY; |
skia.committer@gmail.com | 1f3c738 | 2013-07-20 07:00:58 +0000 | [diff] [blame] | 90 | |
reed@android.com | eef375b | 2009-08-03 14:45:45 +0000 | [diff] [blame] | 91 | SkFixed fInvSx; // chooseProcs |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | SkFixed fInvKy; // chooseProcs |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 93 | SkPMColor fPaintPMColor; // chooseProcs - A8 config |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | uint16_t fAlphaScale; // chooseProcs |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 95 | |
reed@android.com | c9a1d4b | 2009-08-03 15:05:55 +0000 | [diff] [blame] | 96 | /** Platforms implement this, and can optionally overwrite only the |
| 97 | following fields: |
| 98 | |
| 99 | fShaderProc32 |
| 100 | fShaderProc16 |
| 101 | fMatrixProc |
| 102 | fSampleProc32 |
| 103 | fSampleProc32 |
| 104 | |
| 105 | They will already have valid function pointers, so a platform that does |
| 106 | not have an accelerated version can just leave that field as is. A valid |
| 107 | implementation can do nothing (see SkBitmapProcState_opts_none.cpp) |
| 108 | */ |
| 109 | void platformProcs(); |
skia.committer@gmail.com | 1f3c738 | 2013-07-20 07:00:58 +0000 | [diff] [blame] | 110 | |
reed@android.com | 258cb22 | 2010-04-14 13:36:33 +0000 | [diff] [blame] | 111 | /** Given the byte size of the index buffer to be passed to the matrix proc, |
| 112 | return the maximum number of resulting pixels that can be computed |
| 113 | (i.e. the number of SkPMColor values to be written by the sample proc). |
| 114 | This routine takes into account that filtering and scale-vs-affine |
| 115 | affect the amount of buffer space needed. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 116 | |
reed@android.com | 4c128c4 | 2009-08-14 13:54:37 +0000 | [diff] [blame] | 117 | Only valid to call after chooseProcs (setContext) has been called. It is |
| 118 | safe to call this inside the shader's shadeSpan() method. |
| 119 | */ |
| 120 | int maxCountForBufferSize(size_t bufferSize) const; |
| 121 | |
reed@google.com | 9fe287b | 2012-03-27 15:54:28 +0000 | [diff] [blame] | 122 | // If a shader proc is present, then the corresponding matrix/sample procs |
| 123 | // are ignored |
| 124 | ShaderProc32 getShaderProc32() const { return fShaderProc32; } |
| 125 | ShaderProc16 getShaderProc16() const { return fShaderProc16; } |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 126 | |
reed@google.com | 9fe287b | 2012-03-27 15:54:28 +0000 | [diff] [blame] | 127 | #ifdef SK_DEBUG |
| 128 | MatrixProc getMatrixProc() const; |
| 129 | #else |
| 130 | MatrixProc getMatrixProc() const { return fMatrixProc; } |
| 131 | #endif |
| 132 | SampleProc32 getSampleProc32() const { return fSampleProc32; } |
reed@google.com | 9fe287b | 2012-03-27 15:54:28 +0000 | [diff] [blame] | 133 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | private: |
reed@google.com | 9fe287b | 2012-03-27 15:54:28 +0000 | [diff] [blame] | 135 | ShaderProc32 fShaderProc32; // chooseProcs |
| 136 | ShaderProc16 fShaderProc16; // chooseProcs |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 137 | // These are used if the shaderproc is nullptr |
reed@google.com | 9fe287b | 2012-03-27 15:54:28 +0000 | [diff] [blame] | 138 | MatrixProc fMatrixProc; // chooseProcs |
| 139 | SampleProc32 fSampleProc32; // chooseProcs |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 140 | |
reed@android.com | 07d1f00 | 2009-08-13 19:35:48 +0000 | [diff] [blame] | 141 | MatrixProc chooseMatrixProc(bool trivial_matrix); |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 142 | bool chooseProcs(); // caller must have called init() first (on our base-class) |
| 143 | bool chooseScanlineProcs(bool trivialMatrix, bool clampClamp); |
reed@google.com | 9a4c746 | 2012-10-12 18:21:37 +0000 | [diff] [blame] | 144 | ShaderProc32 chooseShaderProc32(); |
skia.committer@gmail.com | fa1bd5f | 2013-07-13 07:00:56 +0000 | [diff] [blame] | 145 | |
reed@google.com | 6bb92bc | 2012-11-20 19:45:16 +0000 | [diff] [blame] | 146 | // Return false if we failed to setup for fast translate (e.g. overflow) |
| 147 | bool setupForTranslate(); |
reed@google.com | 9fe287b | 2012-03-27 15:54:28 +0000 | [diff] [blame] | 148 | |
| 149 | #ifdef SK_DEBUG |
| 150 | static void DebugMatrixProc(const SkBitmapProcState&, |
| 151 | uint32_t[], int count, int x, int y); |
| 152 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
reed@android.com | 07d1f00 | 2009-08-13 19:35:48 +0000 | [diff] [blame] | 155 | /* Macros for packing and unpacking pairs of 16bit values in a 32bit uint. |
| 156 | Used to allow access to a stream of uint16_t either one at a time, or |
| 157 | 2 at a time by unpacking a uint32_t |
| 158 | */ |
| 159 | #ifdef SK_CPU_BENDIAN |
| 160 | #define PACK_TWO_SHORTS(pri, sec) ((pri) << 16 | (sec)) |
| 161 | #define UNPACK_PRIMARY_SHORT(packed) ((uint32_t)(packed) >> 16) |
| 162 | #define UNPACK_SECONDARY_SHORT(packed) ((packed) & 0xFFFF) |
| 163 | #else |
| 164 | #define PACK_TWO_SHORTS(pri, sec) ((pri) | ((sec) << 16)) |
| 165 | #define UNPACK_PRIMARY_SHORT(packed) ((packed) & 0xFFFF) |
| 166 | #define UNPACK_SECONDARY_SHORT(packed) ((uint32_t)(packed) >> 16) |
| 167 | #endif |
| 168 | |
| 169 | #ifdef SK_DEBUG |
| 170 | static inline uint32_t pack_two_shorts(U16CPU pri, U16CPU sec) { |
| 171 | SkASSERT((uint16_t)pri == pri); |
| 172 | SkASSERT((uint16_t)sec == sec); |
| 173 | return PACK_TWO_SHORTS(pri, sec); |
| 174 | } |
| 175 | #else |
| 176 | #define pack_two_shorts(pri, sec) PACK_TWO_SHORTS(pri, sec) |
| 177 | #endif |
| 178 | |
senorblanco@chromium.org | dc7de74 | 2009-11-30 20:00:29 +0000 | [diff] [blame] | 179 | // These functions are generated via macros, but are exposed here so that |
| 180 | // platformProcs may test for them by name. |
| 181 | void S32_opaque_D32_filter_DX(const SkBitmapProcState& s, const uint32_t xy[], |
| 182 | int count, SkPMColor colors[]); |
senorblanco@chromium.org | f3f0bd7 | 2009-12-10 22:46:31 +0000 | [diff] [blame] | 183 | void S32_alpha_D32_filter_DX(const SkBitmapProcState& s, const uint32_t xy[], |
| 184 | int count, SkPMColor colors[]); |
tomhudson@google.com | 06a7313 | 2012-02-22 18:30:43 +0000 | [diff] [blame] | 185 | void ClampX_ClampY_filter_scale(const SkBitmapProcState& s, uint32_t xy[], |
| 186 | int count, int x, int y); |
| 187 | void ClampX_ClampY_nofilter_scale(const SkBitmapProcState& s, uint32_t xy[], |
| 188 | int count, int x, int y); |
senorblanco@chromium.org | dc7de74 | 2009-11-30 20:00:29 +0000 | [diff] [blame] | 189 | |
fmalita | b3a8358 | 2016-01-04 10:28:11 -0800 | [diff] [blame] | 190 | // Helper class for mapping the middle of pixel (x, y) into SkFractionalInt bitmap space. |
herb | 5c473be | 2016-03-19 08:46:24 -0700 | [diff] [blame] | 191 | // Discussion: |
| 192 | // Overall, this code takes a point in destination space, and uses the center of the pixel |
| 193 | // at (x, y) to determine the sample point in source space. It then adjusts the pixel by different |
| 194 | // amounts based in filtering and tiling. |
| 195 | // This code can be broken into two main cases based on filtering: |
| 196 | // * no filtering (nearest neighbor) - when using nearest neighbor filtering all tile modes reduce |
| 197 | // the sampled by one ulp. If a simple point pt lies precisely on XXX.1/2 then it forced down |
| 198 | // when positive making 1/2 + 1/2 = .999999 instead of 1.0. |
| 199 | // * filtering - in the filtering case, the code calculates the -1/2 shift for starting the |
| 200 | // bilerp kernel. There is a twist; there is a big difference between clamp and the other tile |
| 201 | // modes. In tile and repeat the matrix has been reduced by an additional 1/width and 1/height |
| 202 | // factor. This maps from destination space to [0, 1) (instead of source space) to allow easy |
| 203 | // modulo arithmetic. This means that the -1/2 needed by bilerp is actually 1/2 * 1/width for x |
| 204 | // and 1/2 * 1/height for y. This is what happens when the poorly named fFilterOne{X|Y} is |
| 205 | // divided by two. |
fmalita | b3a8358 | 2016-01-04 10:28:11 -0800 | [diff] [blame] | 206 | class SkBitmapProcStateAutoMapper { |
| 207 | public: |
fmalita | 8a8eb52 | 2016-01-30 18:56:34 -0800 | [diff] [blame] | 208 | SkBitmapProcStateAutoMapper(const SkBitmapProcState& s, int x, int y, |
| 209 | SkPoint* scalarPoint = nullptr) { |
fmalita | b3a8358 | 2016-01-04 10:28:11 -0800 | [diff] [blame] | 210 | SkPoint pt; |
| 211 | s.fInvProc(s.fInvMatrix, |
| 212 | SkIntToScalar(x) + SK_ScalarHalf, |
| 213 | SkIntToScalar(y) + SK_ScalarHalf, &pt); |
| 214 | |
fmalita | 2404f03 | 2016-02-03 05:44:21 -0800 | [diff] [blame] | 215 | SkFixed biasX, biasY; |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 216 | if (s.fFilterQuality == kNone_SkFilterQuality) { |
fmalita | 2404f03 | 2016-02-03 05:44:21 -0800 | [diff] [blame] | 217 | // SkFixed epsilon bias to ensure inverse-mapped bitmap coordinates are rounded |
| 218 | // consistently WRT geometry. Note that we only need the bias for positive scales: |
| 219 | // for negative scales, the rounding is intrinsically correct. |
| 220 | // We scale it to persist SkFractionalInt -> SkFixed conversions. |
| 221 | biasX = (s.fInvMatrix.getScaleX() > 0); |
| 222 | biasY = (s.fInvMatrix.getScaleY() > 0); |
| 223 | } else { |
| 224 | biasX = s.fFilterOneX >> 1; |
| 225 | biasY = s.fFilterOneY >> 1; |
| 226 | } |
| 227 | |
Florin Malita | 9d1cc66 | 2016-12-12 15:43:51 -0500 | [diff] [blame] | 228 | // punt to unsigned for defined underflow behavior |
| 229 | fX = (SkFractionalInt)((uint64_t)SkScalarToFractionalInt(pt.x()) - |
| 230 | (uint64_t)SkFixedToFractionalInt(biasX)); |
| 231 | fY = (SkFractionalInt)((uint64_t)SkScalarToFractionalInt(pt.y()) - |
| 232 | (uint64_t)SkFixedToFractionalInt(biasY)); |
fmalita | 653c12d | 2016-01-30 10:06:46 -0800 | [diff] [blame] | 233 | |
fmalita | 8a8eb52 | 2016-01-30 18:56:34 -0800 | [diff] [blame] | 234 | if (scalarPoint) { |
| 235 | scalarPoint->set(pt.x() - SkFixedToScalar(biasX), |
| 236 | pt.y() - SkFixedToScalar(biasY)); |
| 237 | } |
fmalita | b3a8358 | 2016-01-04 10:28:11 -0800 | [diff] [blame] | 238 | } |
| 239 | |
fmalita | be5cfa9 | 2016-02-03 10:21:33 -0800 | [diff] [blame] | 240 | SkFractionalInt fractionalIntX() const { return fX; } |
| 241 | SkFractionalInt fractionalIntY() const { return fY; } |
| 242 | |
| 243 | SkFixed fixedX() const { return SkFractionalIntToFixed(fX); } |
| 244 | SkFixed fixedY() const { return SkFractionalIntToFixed(fY); } |
| 245 | |
| 246 | int intX() const { return SkFractionalIntToInt(fX); } |
| 247 | int intY() const { return SkFractionalIntToInt(fY); } |
fmalita | b3a8358 | 2016-01-04 10:28:11 -0800 | [diff] [blame] | 248 | |
| 249 | private: |
| 250 | SkFractionalInt fX, fY; |
| 251 | }; |
| 252 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 253 | #endif |