epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | |
| 10 | #ifndef SkBitmapProcState_DEFINED |
| 11 | #define SkBitmapProcState_DEFINED |
| 12 | |
| 13 | #include "SkBitmap.h" |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 14 | #include "SkBitmapFilter.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #include "SkMatrix.h" |
reed@google.com | cee9dcb | 2013-09-13 16:04:49 +0000 | [diff] [blame] | 16 | #include "SkPaint.h" |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 17 | #include "SkScaledImageCache.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | |
reed@google.com | 77613a5 | 2012-03-14 12:21:17 +0000 | [diff] [blame] | 19 | #define FractionalInt_IS_64BIT |
reed@google.com | 4bc0a9d | 2012-03-07 21:47:41 +0000 | [diff] [blame] | 20 | |
| 21 | #ifdef FractionalInt_IS_64BIT |
| 22 | typedef SkFixed48 SkFractionalInt; |
| 23 | #define SkScalarToFractionalInt(x) SkScalarToFixed48(x) |
| 24 | #define SkFractionalIntToFixed(x) SkFixed48ToFixed(x) |
reed@google.com | 411215a | 2012-03-08 20:13:46 +0000 | [diff] [blame] | 25 | #define SkFixedToFractionalInt(x) SkFixedToFixed48(x) |
reed@google.com | 4bc0a9d | 2012-03-07 21:47:41 +0000 | [diff] [blame] | 26 | #define SkFractionalIntToInt(x) SkFixed48ToInt(x) |
| 27 | #else |
| 28 | typedef SkFixed SkFractionalInt; |
| 29 | #define SkScalarToFractionalInt(x) SkScalarToFixed(x) |
| 30 | #define SkFractionalIntToFixed(x) (x) |
reed@google.com | 411215a | 2012-03-08 20:13:46 +0000 | [diff] [blame] | 31 | #define SkFixedToFractionalInt(x) (x) |
reed@google.com | 4bc0a9d | 2012-03-07 21:47:41 +0000 | [diff] [blame] | 32 | #define SkFractionalIntToInt(x) ((x) >> 16) |
| 33 | #endif |
| 34 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | class SkPaint; |
| 36 | |
| 37 | struct SkBitmapProcState { |
skia.committer@gmail.com | fa1bd5f | 2013-07-13 07:00:56 +0000 | [diff] [blame] | 38 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 39 | SkBitmapProcState(): fScaledCacheID(NULL), fBitmapFilter(NULL) {} |
reed@google.com | 1e18225 | 2013-07-24 20:10:42 +0000 | [diff] [blame] | 40 | ~SkBitmapProcState(); |
commit-bot@chromium.org | e81d1bc | 2013-07-10 20:42:12 +0000 | [diff] [blame] | 41 | |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 42 | typedef void (*ShaderProc32)(const SkBitmapProcState&, int x, int y, |
| 43 | SkPMColor[], int count); |
| 44 | |
| 45 | typedef void (*ShaderProc16)(const SkBitmapProcState&, int x, int y, |
| 46 | uint16_t[], int count); |
| 47 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | typedef void (*MatrixProc)(const SkBitmapProcState&, |
| 49 | uint32_t bitmapXY[], |
| 50 | int count, |
| 51 | int x, int y); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 52 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | typedef void (*SampleProc32)(const SkBitmapProcState&, |
| 54 | const uint32_t[], |
| 55 | int count, |
| 56 | SkPMColor colors[]); |
| 57 | |
| 58 | typedef void (*SampleProc16)(const SkBitmapProcState&, |
| 59 | const uint32_t[], |
| 60 | int count, |
| 61 | uint16_t colors[]); |
skia.committer@gmail.com | 1f3c738 | 2013-07-20 07:00:58 +0000 | [diff] [blame] | 62 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | typedef U16CPU (*FixedTileProc)(SkFixed); // returns 0..0xFFFF |
reed@google.com | f444e8c | 2012-03-09 19:59:46 +0000 | [diff] [blame] | 64 | typedef U16CPU (*FixedTileLowBitsProc)(SkFixed, int); // returns 0..0xF |
reed@android.com | 07d1f00 | 2009-08-13 19:35:48 +0000 | [diff] [blame] | 65 | typedef U16CPU (*IntTileProc)(int value, int count); // returns 0..count-1 |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 66 | |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 67 | const SkBitmap* fBitmap; // chooseProcs - orig or scaled |
| 68 | SkMatrix fInvMatrix; // chooseProcs |
reed@android.com | eef375b | 2009-08-03 14:45:45 +0000 | [diff] [blame] | 69 | SkMatrix::MapXYProc fInvProc; // chooseProcs |
| 70 | |
reed@google.com | 4bc0a9d | 2012-03-07 21:47:41 +0000 | [diff] [blame] | 71 | SkFractionalInt fInvSxFractionalInt; |
reed@google.com | 411215a | 2012-03-08 20:13:46 +0000 | [diff] [blame] | 72 | SkFractionalInt fInvKyFractionalInt; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 73 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 74 | FixedTileProc fTileProcX; // chooseProcs |
| 75 | FixedTileProc fTileProcY; // chooseProcs |
reed@google.com | f444e8c | 2012-03-09 19:59:46 +0000 | [diff] [blame] | 76 | FixedTileLowBitsProc fTileLowBitsProcX; // chooseProcs |
| 77 | FixedTileLowBitsProc fTileLowBitsProcY; // chooseProcs |
reed@android.com | 07d1f00 | 2009-08-13 19:35:48 +0000 | [diff] [blame] | 78 | IntTileProc fIntTileProcY; // chooseProcs |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | SkFixed fFilterOneX; |
| 80 | SkFixed fFilterOneY; |
skia.committer@gmail.com | 1f3c738 | 2013-07-20 07:00:58 +0000 | [diff] [blame] | 81 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | SkPMColor fPaintPMColor; // chooseProcs - A8 config |
reed@android.com | eef375b | 2009-08-03 14:45:45 +0000 | [diff] [blame] | 83 | SkFixed fInvSx; // chooseProcs |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | SkFixed fInvKy; // chooseProcs |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | uint16_t fAlphaScale; // chooseProcs |
| 86 | uint8_t fInvType; // chooseProcs |
| 87 | uint8_t fTileModeX; // CONSTRUCTOR |
| 88 | uint8_t fTileModeY; // CONSTRUCTOR |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 89 | uint8_t fFilterLevel; // chooseProcs |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 90 | |
reed@android.com | c9a1d4b | 2009-08-03 15:05:55 +0000 | [diff] [blame] | 91 | /** Platforms implement this, and can optionally overwrite only the |
| 92 | following fields: |
| 93 | |
| 94 | fShaderProc32 |
| 95 | fShaderProc16 |
| 96 | fMatrixProc |
| 97 | fSampleProc32 |
| 98 | fSampleProc32 |
| 99 | |
| 100 | They will already have valid function pointers, so a platform that does |
| 101 | not have an accelerated version can just leave that field as is. A valid |
| 102 | implementation can do nothing (see SkBitmapProcState_opts_none.cpp) |
| 103 | */ |
| 104 | void platformProcs(); |
skia.committer@gmail.com | 1f3c738 | 2013-07-20 07:00:58 +0000 | [diff] [blame] | 105 | |
reed@android.com | 258cb22 | 2010-04-14 13:36:33 +0000 | [diff] [blame] | 106 | /** Given the byte size of the index buffer to be passed to the matrix proc, |
| 107 | return the maximum number of resulting pixels that can be computed |
| 108 | (i.e. the number of SkPMColor values to be written by the sample proc). |
| 109 | This routine takes into account that filtering and scale-vs-affine |
| 110 | affect the amount of buffer space needed. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 111 | |
reed@android.com | 4c128c4 | 2009-08-14 13:54:37 +0000 | [diff] [blame] | 112 | Only valid to call after chooseProcs (setContext) has been called. It is |
| 113 | safe to call this inside the shader's shadeSpan() method. |
| 114 | */ |
| 115 | int maxCountForBufferSize(size_t bufferSize) const; |
| 116 | |
reed@google.com | 9fe287b | 2012-03-27 15:54:28 +0000 | [diff] [blame] | 117 | // If a shader proc is present, then the corresponding matrix/sample procs |
| 118 | // are ignored |
| 119 | ShaderProc32 getShaderProc32() const { return fShaderProc32; } |
| 120 | ShaderProc16 getShaderProc16() const { return fShaderProc16; } |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 121 | |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 122 | SkBitmapFilter* getBitmapFilter() const { return fBitmapFilter; } |
reed@google.com | 9fe287b | 2012-03-27 15:54:28 +0000 | [diff] [blame] | 123 | |
| 124 | #ifdef SK_DEBUG |
| 125 | MatrixProc getMatrixProc() const; |
| 126 | #else |
| 127 | MatrixProc getMatrixProc() const { return fMatrixProc; } |
| 128 | #endif |
| 129 | SampleProc32 getSampleProc32() const { return fSampleProc32; } |
| 130 | SampleProc16 getSampleProc16() const { return fSampleProc16; } |
| 131 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 132 | private: |
reed@android.com | eef375b | 2009-08-03 14:45:45 +0000 | [diff] [blame] | 133 | friend class SkBitmapProcShader; |
| 134 | |
reed@google.com | 9fe287b | 2012-03-27 15:54:28 +0000 | [diff] [blame] | 135 | ShaderProc32 fShaderProc32; // chooseProcs |
| 136 | ShaderProc16 fShaderProc16; // chooseProcs |
| 137 | // These are used if the shaderproc is NULL |
| 138 | MatrixProc fMatrixProc; // chooseProcs |
| 139 | SampleProc32 fSampleProc32; // chooseProcs |
| 140 | SampleProc16 fSampleProc16; // chooseProcs |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 141 | |
reed@android.com | eef375b | 2009-08-03 14:45:45 +0000 | [diff] [blame] | 142 | SkBitmap fOrigBitmap; // CONSTRUCTOR |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 143 | SkBitmap fScaledBitmap; // chooseProcs |
reed@android.com | eef375b | 2009-08-03 14:45:45 +0000 | [diff] [blame] | 144 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 145 | SkScaledImageCache::ID* fScaledCacheID; |
| 146 | |
reed@android.com | 07d1f00 | 2009-08-13 19:35:48 +0000 | [diff] [blame] | 147 | MatrixProc chooseMatrixProc(bool trivial_matrix); |
reed@android.com | eef375b | 2009-08-03 14:45:45 +0000 | [diff] [blame] | 148 | bool chooseProcs(const SkMatrix& inv, const SkPaint&); |
reed@google.com | 9a4c746 | 2012-10-12 18:21:37 +0000 | [diff] [blame] | 149 | ShaderProc32 chooseShaderProc32(); |
skia.committer@gmail.com | fa1bd5f | 2013-07-13 07:00:56 +0000 | [diff] [blame] | 150 | |
reed@google.com | cee9dcb | 2013-09-13 16:04:49 +0000 | [diff] [blame] | 151 | // returns false if we did not try to scale the image. In that case, we |
| 152 | // will need to "lock" its pixels some other way. |
| 153 | bool possiblyScaleImage(); |
| 154 | |
| 155 | // returns false if we failed to "lock" the pixels at all. Typically this |
| 156 | // means we have to abort the shader. |
| 157 | bool lockBaseBitmap(); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 158 | |
humper@google.com | 138ebc3 | 2013-07-19 20:20:04 +0000 | [diff] [blame] | 159 | SkBitmapFilter* fBitmapFilter; |
skia.committer@gmail.com | b0a327e | 2012-11-21 02:02:25 +0000 | [diff] [blame] | 160 | |
mtklein@google.com | 0dc546c | 2013-08-26 16:21:35 +0000 | [diff] [blame] | 161 | // If supported, sets fShaderProc32 and fShaderProc16 and returns true, |
| 162 | // otherwise returns false. |
| 163 | bool setBitmapFilterProcs(); |
skia.committer@gmail.com | 3e2345a | 2013-05-24 07:01:26 +0000 | [diff] [blame] | 164 | |
reed@google.com | 6bb92bc | 2012-11-20 19:45:16 +0000 | [diff] [blame] | 165 | // Return false if we failed to setup for fast translate (e.g. overflow) |
| 166 | bool setupForTranslate(); |
reed@google.com | 9fe287b | 2012-03-27 15:54:28 +0000 | [diff] [blame] | 167 | |
| 168 | #ifdef SK_DEBUG |
| 169 | static void DebugMatrixProc(const SkBitmapProcState&, |
| 170 | uint32_t[], int count, int x, int y); |
| 171 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
reed@android.com | 07d1f00 | 2009-08-13 19:35:48 +0000 | [diff] [blame] | 174 | /* Macros for packing and unpacking pairs of 16bit values in a 32bit uint. |
| 175 | Used to allow access to a stream of uint16_t either one at a time, or |
| 176 | 2 at a time by unpacking a uint32_t |
| 177 | */ |
| 178 | #ifdef SK_CPU_BENDIAN |
| 179 | #define PACK_TWO_SHORTS(pri, sec) ((pri) << 16 | (sec)) |
| 180 | #define UNPACK_PRIMARY_SHORT(packed) ((uint32_t)(packed) >> 16) |
| 181 | #define UNPACK_SECONDARY_SHORT(packed) ((packed) & 0xFFFF) |
| 182 | #else |
| 183 | #define PACK_TWO_SHORTS(pri, sec) ((pri) | ((sec) << 16)) |
| 184 | #define UNPACK_PRIMARY_SHORT(packed) ((packed) & 0xFFFF) |
| 185 | #define UNPACK_SECONDARY_SHORT(packed) ((uint32_t)(packed) >> 16) |
| 186 | #endif |
| 187 | |
| 188 | #ifdef SK_DEBUG |
| 189 | static inline uint32_t pack_two_shorts(U16CPU pri, U16CPU sec) { |
| 190 | SkASSERT((uint16_t)pri == pri); |
| 191 | SkASSERT((uint16_t)sec == sec); |
| 192 | return PACK_TWO_SHORTS(pri, sec); |
| 193 | } |
| 194 | #else |
| 195 | #define pack_two_shorts(pri, sec) PACK_TWO_SHORTS(pri, sec) |
| 196 | #endif |
| 197 | |
senorblanco@chromium.org | dc7de74 | 2009-11-30 20:00:29 +0000 | [diff] [blame] | 198 | // These functions are generated via macros, but are exposed here so that |
| 199 | // platformProcs may test for them by name. |
| 200 | void S32_opaque_D32_filter_DX(const SkBitmapProcState& s, const uint32_t xy[], |
| 201 | int count, SkPMColor colors[]); |
senorblanco@chromium.org | f3f0bd7 | 2009-12-10 22:46:31 +0000 | [diff] [blame] | 202 | void S32_alpha_D32_filter_DX(const SkBitmapProcState& s, const uint32_t xy[], |
| 203 | int count, SkPMColor colors[]); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 204 | void S32_opaque_D32_filter_DXDY(const SkBitmapProcState& s, |
mtklein@google.com | 0dc546c | 2013-08-26 16:21:35 +0000 | [diff] [blame] | 205 | const uint32_t xy[], int count, SkPMColor colors[]); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 206 | void S32_alpha_D32_filter_DXDY(const SkBitmapProcState& s, |
mtklein@google.com | 0dc546c | 2013-08-26 16:21:35 +0000 | [diff] [blame] | 207 | const uint32_t xy[], int count, SkPMColor colors[]); |
tomhudson@google.com | 06a7313 | 2012-02-22 18:30:43 +0000 | [diff] [blame] | 208 | void ClampX_ClampY_filter_scale(const SkBitmapProcState& s, uint32_t xy[], |
| 209 | int count, int x, int y); |
| 210 | void ClampX_ClampY_nofilter_scale(const SkBitmapProcState& s, uint32_t xy[], |
| 211 | int count, int x, int y); |
tomhudson@google.com | 5efaf26 | 2012-02-28 15:41:49 +0000 | [diff] [blame] | 212 | void ClampX_ClampY_filter_affine(const SkBitmapProcState& s, |
| 213 | uint32_t xy[], int count, int x, int y); |
| 214 | void ClampX_ClampY_nofilter_affine(const SkBitmapProcState& s, |
| 215 | uint32_t xy[], int count, int x, int y); |
reed@google.com | 7866228 | 2012-07-24 13:53:23 +0000 | [diff] [blame] | 216 | void S32_D16_filter_DX(const SkBitmapProcState& s, |
mtklein@google.com | 0dc546c | 2013-08-26 16:21:35 +0000 | [diff] [blame] | 217 | const uint32_t* xy, int count, uint16_t* colors); |
senorblanco@chromium.org | dc7de74 | 2009-11-30 20:00:29 +0000 | [diff] [blame] | 218 | |
mtklein@google.com | 0dc546c | 2013-08-26 16:21:35 +0000 | [diff] [blame] | 219 | void highQualityFilter32(const SkBitmapProcState &s, int x, int y, |
| 220 | SkPMColor *SK_RESTRICT colors, int count); |
| 221 | void highQualityFilter16(const SkBitmapProcState &s, int x, int y, |
| 222 | uint16_t *SK_RESTRICT colors, int count); |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 223 | |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 224 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 225 | #endif |