rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 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 SkGradientShaderPriv_DEFINED |
| 9 | #define SkGradientShaderPriv_DEFINED |
| 10 | |
| 11 | #include "SkGradientShader.h" |
| 12 | #include "SkClampRange.h" |
| 13 | #include "SkColorPriv.h" |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 14 | #include "SkFlattenableBuffers.h" |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 15 | #include "SkMallocPixelRef.h" |
| 16 | #include "SkUnitMapper.h" |
| 17 | #include "SkUtils.h" |
| 18 | #include "SkTemplates.h" |
| 19 | #include "SkBitmapCache.h" |
| 20 | #include "SkShader.h" |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 21 | |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 22 | static inline void sk_memset32_dither(uint32_t dst[], uint32_t v0, uint32_t v1, |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 23 | int count) { |
| 24 | if (count > 0) { |
| 25 | if (v0 == v1) { |
| 26 | sk_memset32(dst, v0, count); |
| 27 | } else { |
| 28 | int pairs = count >> 1; |
| 29 | for (int i = 0; i < pairs; i++) { |
| 30 | *dst++ = v0; |
| 31 | *dst++ = v1; |
| 32 | } |
| 33 | if (count & 1) { |
| 34 | *dst = v0; |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // Clamp |
| 41 | |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 42 | static inline SkFixed clamp_tileproc(SkFixed x) { |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 43 | return SkClampMax(x, 0xFFFF); |
| 44 | } |
| 45 | |
| 46 | // Repeat |
| 47 | |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 48 | static inline SkFixed repeat_tileproc(SkFixed x) { |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 49 | return x & 0xFFFF; |
| 50 | } |
| 51 | |
| 52 | // Mirror |
| 53 | |
| 54 | // Visual Studio 2010 (MSC_VER=1600) optimizes bit-shift code incorrectly. |
| 55 | // See http://code.google.com/p/skia/issues/detail?id=472 |
| 56 | #if defined(_MSC_VER) && (_MSC_VER >= 1600) |
| 57 | #pragma optimize("", off) |
| 58 | #endif |
| 59 | |
| 60 | static inline SkFixed mirror_tileproc(SkFixed x) { |
| 61 | int s = x << 15 >> 31; |
| 62 | return (x ^ s) & 0xFFFF; |
| 63 | } |
| 64 | |
| 65 | #if defined(_MSC_VER) && (_MSC_VER >= 1600) |
| 66 | #pragma optimize("", on) |
| 67 | #endif |
| 68 | |
| 69 | /////////////////////////////////////////////////////////////////////////////// |
| 70 | |
| 71 | typedef SkFixed (*TileProc)(SkFixed); |
| 72 | |
| 73 | /////////////////////////////////////////////////////////////////////////////// |
| 74 | |
| 75 | static const TileProc gTileProcs[] = { |
| 76 | clamp_tileproc, |
| 77 | repeat_tileproc, |
| 78 | mirror_tileproc |
| 79 | }; |
| 80 | |
| 81 | /////////////////////////////////////////////////////////////////////////////// |
| 82 | |
| 83 | class SkGradientShaderBase : public SkShader { |
| 84 | public: |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 85 | struct Descriptor { |
| 86 | Descriptor() { |
| 87 | sk_bzero(this, sizeof(*this)); |
| 88 | fTileMode = SkShader::kClamp_TileMode; |
| 89 | } |
skia.committer@gmail.com | 3e2345a | 2013-05-24 07:01:26 +0000 | [diff] [blame] | 90 | |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 91 | const SkColor* fColors; |
| 92 | const SkScalar* fPos; |
| 93 | int fCount; |
| 94 | SkShader::TileMode fTileMode; |
| 95 | SkUnitMapper* fMapper; |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 96 | uint32_t fFlags; |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | public: |
| 100 | SkGradientShaderBase(const Descriptor& desc); |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 101 | virtual ~SkGradientShaderBase(); |
| 102 | |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 103 | virtual bool setContext(const SkBitmap&, const SkPaint&, const SkMatrix&) SK_OVERRIDE; |
| 104 | virtual uint32_t getFlags() SK_OVERRIDE { return fFlags; } |
| 105 | virtual bool isOpaque() const SK_OVERRIDE; |
| 106 | |
| 107 | void getGradientTableBitmap(SkBitmap*) const; |
| 108 | |
| 109 | enum { |
| 110 | /// Seems like enough for visual accuracy. TODO: if pos[] deserves |
| 111 | /// it, use a larger cache. |
| 112 | kCache16Bits = 8, |
reed@google.com | 3c2102c | 2013-02-01 12:59:40 +0000 | [diff] [blame] | 113 | kCache16Count = (1 << kCache16Bits), |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 114 | kCache16Shift = 16 - kCache16Bits, |
| 115 | kSqrt16Shift = 8 - kCache16Bits, |
| 116 | |
| 117 | /// Seems like enough for visual accuracy. TODO: if pos[] deserves |
| 118 | /// it, use a larger cache. |
| 119 | kCache32Bits = 8, |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 120 | kCache32Count = (1 << kCache32Bits), |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 121 | kCache32Shift = 16 - kCache32Bits, |
| 122 | kSqrt32Shift = 8 - kCache32Bits, |
| 123 | |
| 124 | /// This value is used to *read* the dither cache; it may be 0 |
| 125 | /// if dithering is disabled. |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 126 | kDitherStride32 = kCache32Count, |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 127 | kDitherStride16 = kCache16Count, |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | |
| 131 | protected: |
| 132 | SkGradientShaderBase(SkFlattenableReadBuffer& ); |
| 133 | virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 134 | SK_DEVELOPER_TO_STRING() |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 135 | |
| 136 | SkUnitMapper* fMapper; |
| 137 | SkMatrix fPtsToUnit; // set by subclass |
| 138 | SkMatrix fDstToIndex; |
| 139 | SkMatrix::MapXYProc fDstToIndexProc; |
| 140 | TileMode fTileMode; |
| 141 | TileProc fTileProc; |
| 142 | int fColorCount; |
| 143 | uint8_t fDstToIndexClass; |
| 144 | uint8_t fFlags; |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 145 | uint8_t fGradFlags; |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 146 | |
| 147 | struct Rec { |
| 148 | SkFixed fPos; // 0...1 |
| 149 | uint32_t fScale; // (1 << 24) / range |
| 150 | }; |
| 151 | Rec* fRecs; |
| 152 | |
| 153 | const uint16_t* getCache16() const; |
| 154 | const SkPMColor* getCache32() const; |
| 155 | |
| 156 | void commonAsAGradient(GradientInfo*) const; |
| 157 | |
| 158 | private: |
| 159 | enum { |
| 160 | kColorStorageCount = 4, // more than this many colors, and we'll use sk_malloc for the space |
| 161 | |
| 162 | kStorageSize = kColorStorageCount * (sizeof(SkColor) + sizeof(Rec)) |
| 163 | }; |
| 164 | SkColor fStorage[(kStorageSize + 3) >> 2]; |
| 165 | SkColor* fOrigColors; // original colors, before modulation by paint in setContext |
| 166 | bool fColorsAreOpaque; |
| 167 | |
| 168 | mutable uint16_t* fCache16; // working ptr. If this is NULL, we need to recompute the cache values |
| 169 | mutable SkPMColor* fCache32; // working ptr. If this is NULL, we need to recompute the cache values |
| 170 | |
| 171 | mutable uint16_t* fCache16Storage; // storage for fCache16, allocated on demand |
| 172 | mutable SkMallocPixelRef* fCache32PixelRef; |
| 173 | mutable unsigned fCacheAlpha; // the alpha value we used when we computed the cache. larger than 8bits so we can store uninitialized value |
| 174 | |
| 175 | static void Build16bitCache(uint16_t[], SkColor c0, SkColor c1, int count); |
| 176 | static void Build32bitCache(SkPMColor[], SkColor c0, SkColor c1, int count, |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 177 | U8CPU alpha, uint32_t gradFlags); |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 178 | void setCacheAlpha(U8CPU alpha) const; |
| 179 | void initCommon(); |
| 180 | |
| 181 | typedef SkShader INHERITED; |
| 182 | }; |
| 183 | |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 184 | static inline int init_dither_toggle(int x, int y) { |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 185 | x &= 1; |
| 186 | y = (y & 1) << 1; |
| 187 | return (x | y) * SkGradientShaderBase::kDitherStride32; |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static inline int next_dither_toggle(int toggle) { |
| 191 | return toggle ^ SkGradientShaderBase::kDitherStride32; |
| 192 | } |
| 193 | |
| 194 | static inline int init_dither_toggle16(int x, int y) { |
| 195 | return ((x ^ y) & 1) * SkGradientShaderBase::kDitherStride16; |
| 196 | } |
| 197 | |
| 198 | static inline int next_dither_toggle16(int toggle) { |
| 199 | return toggle ^ SkGradientShaderBase::kDitherStride16; |
| 200 | } |
| 201 | |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 202 | /////////////////////////////////////////////////////////////////////////////// |
| 203 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 204 | #if SK_SUPPORT_GPU |
| 205 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 206 | #include "GrCoordTransform.h" |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 207 | #include "gl/GrGLEffect.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 208 | |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 209 | class GrEffectStage; |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 210 | class GrBackendEffectFactory; |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 211 | |
| 212 | /* |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 213 | * The interpretation of the texture matrix depends on the sample mode. The |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 214 | * texture matrix is applied both when the texture coordinates are explicit |
| 215 | * and when vertex positions are used as texture coordinates. In the latter |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 216 | * case the texture matrix is applied to the pre-view-matrix position |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 217 | * values. |
| 218 | * |
| 219 | * Normal SampleMode |
| 220 | * The post-matrix texture coordinates are in normalize space with (0,0) at |
| 221 | * the top-left and (1,1) at the bottom right. |
| 222 | * RadialGradient |
| 223 | * The matrix specifies the radial gradient parameters. |
| 224 | * (0,0) in the post-matrix space is center of the radial gradient. |
| 225 | * Radial2Gradient |
| 226 | * Matrix transforms to space where first circle is centered at the |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 227 | * origin. The second circle will be centered (x, 0) where x may be |
| 228 | * 0 and is provided by setRadial2Params. The post-matrix space is |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 229 | * normalized such that 1 is the second radius - first radius. |
| 230 | * SweepGradient |
| 231 | * The angle from the origin of texture coordinates in post-matrix space |
| 232 | * determines the gradient value. |
| 233 | */ |
| 234 | |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 235 | class GrTextureStripAtlas; |
| 236 | |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 237 | // Base class for Gr gradient effects |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 238 | class GrGradientEffect : public GrEffect { |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 239 | public: |
| 240 | |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 241 | GrGradientEffect(GrContext* ctx, |
| 242 | const SkGradientShaderBase& shader, |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 243 | const SkMatrix& matrix, |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 244 | SkShader::TileMode tileMode); |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 245 | |
| 246 | virtual ~GrGradientEffect(); |
| 247 | |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 248 | bool useAtlas() const { return SkToBool(-1 != fRow); } |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 249 | SkScalar getYCoord() const { return fYCoord; }; |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 250 | |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 251 | virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; |
skia.committer@gmail.com | 9a070f2 | 2013-09-10 07:01:44 +0000 | [diff] [blame] | 252 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 253 | enum ColorType { |
| 254 | kTwo_ColorType, |
| 255 | kThree_ColorType, |
| 256 | kTexture_ColorType |
| 257 | }; |
| 258 | |
| 259 | ColorType getColorType() const { return fColorType; } |
| 260 | |
| 261 | enum PremulType { |
| 262 | kBeforeInterp_PremulType, |
| 263 | kAfterInterp_PremulType, |
| 264 | }; |
| 265 | |
| 266 | PremulType getPremulType() const { return fPremulType; } |
| 267 | |
| 268 | const SkColor* getColors(int pos) const { |
| 269 | SkASSERT(fColorType != kTexture_ColorType); |
| 270 | SkASSERT((pos-1) <= fColorType); |
| 271 | return &fColors[pos]; |
| 272 | } |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 273 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 274 | protected: |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 275 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 276 | /** Populates a pair of arrays with colors and stop info to construct a random gradient. |
| 277 | The function decides whether stop values should be used or not. The return value indicates |
| 278 | the number of colors, which will be capped by kMaxRandomGradientColors. colors should be |
| 279 | sized to be at least kMaxRandomGradientColors. stops is a pointer to an array of at least |
| 280 | size kMaxRandomGradientColors. It may be updated to NULL, indicating that NULL should be |
| 281 | passed to the gradient factory rather than the array. |
| 282 | */ |
| 283 | static const int kMaxRandomGradientColors = 4; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 284 | static int RandomGradientParams(SkRandom* r, |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 285 | SkColor colors[kMaxRandomGradientColors], |
| 286 | SkScalar** stops, |
| 287 | SkShader::TileMode* tm); |
| 288 | |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 289 | virtual bool onIsEqual(const GrEffect& effect) const SK_OVERRIDE; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 290 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 291 | private: |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 292 | static const GrCoordSet kCoordSet = kLocal_GrCoordSet; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 293 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 294 | enum { |
| 295 | kMaxAnalyticColors = 3 // if more colors use texture |
| 296 | }; |
| 297 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 298 | GrCoordTransform fCoordTransform; |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 299 | GrTextureAccess fTextureAccess; |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 300 | SkScalar fYCoord; |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 301 | GrTextureStripAtlas* fAtlas; |
| 302 | int fRow; |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 303 | bool fIsOpaque; |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 304 | ColorType fColorType; |
| 305 | SkColor fColors[kMaxAnalyticColors]; |
| 306 | PremulType fPremulType; // This only changes behavior for two and three color special cases. |
| 307 | // It is already baked into to the table for texture gradients. |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 308 | typedef GrEffect INHERITED; |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 309 | |
| 310 | }; |
| 311 | |
| 312 | /////////////////////////////////////////////////////////////////////////////// |
| 313 | |
bsalomon@google.com | 8ea78d8 | 2012-10-24 20:11:30 +0000 | [diff] [blame] | 314 | // Base class for GL gradient effects |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 315 | class GrGLGradientEffect : public GrGLEffect { |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 316 | public: |
bsalomon@google.com | 0707c29 | 2012-10-25 21:45:42 +0000 | [diff] [blame] | 317 | GrGLGradientEffect(const GrBackendEffectFactory& factory); |
| 318 | virtual ~GrGLGradientEffect(); |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 319 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 320 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 321 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 322 | protected: |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 323 | enum { |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 324 | kPremulTypeKeyBitCnt = 1, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 325 | kPremulTypeMask = 1, |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 326 | kPremulBeforeInterpKey = kPremulTypeMask, |
| 327 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 328 | kTwoColorKey = 2 << kPremulTypeKeyBitCnt, |
| 329 | kThreeColorKey = 3 << kPremulTypeKeyBitCnt, |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 330 | kColorKeyMask = kTwoColorKey | kThreeColorKey, |
| 331 | kColorKeyBitCnt = 2, |
| 332 | |
| 333 | // Subclasses must shift any key bits they produce up by this amount |
| 334 | // and combine with the result of GenBaseGradientKey. |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame^] | 335 | kBaseKeyBitCnt = (kPremulTypeKeyBitCnt + kColorKeyBitCnt) |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 336 | }; |
| 337 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 338 | static GrGradientEffect::ColorType ColorTypeFromKey(EffectKey key){ |
| 339 | if (kTwoColorKey == (key & kColorKeyMask)) { |
| 340 | return GrGradientEffect::kTwo_ColorType; |
| 341 | } else if (kThreeColorKey == (key & kColorKeyMask)) { |
| 342 | return GrGradientEffect::kThree_ColorType; |
| 343 | } else {return GrGradientEffect::kTexture_ColorType;} |
| 344 | } |
| 345 | |
| 346 | static GrGradientEffect::PremulType PremulTypeFromKey(EffectKey key){ |
| 347 | if (kPremulBeforeInterpKey == (key & kPremulTypeMask)) { |
| 348 | return GrGradientEffect::kBeforeInterp_PremulType; |
| 349 | } else { |
| 350 | return GrGradientEffect::kAfterInterp_PremulType; |
| 351 | } |
| 352 | } |
| 353 | |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 354 | /** |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 355 | * Subclasses must call this. It will return a value restricted to the lower kBaseKeyBitCnt |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 356 | * bits. |
| 357 | */ |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 358 | static EffectKey GenBaseGradientKey(const GrDrawEffect&); |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 359 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 360 | // Emits the uniform used as the y-coord to texture samples in derived classes. Subclasses |
| 361 | // should call this method from their emitCode(). |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 362 | void emitUniforms(GrGLShaderBuilder* builder, EffectKey key); |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 363 | |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 364 | |
| 365 | // emit code that gets a fragment's color from an expression for t; Has branches for 3 separate |
| 366 | // control flows inside -- 2 color gradients, 3 color symmetric gradients (both using |
| 367 | // native GLSL mix), and 4+ color gradients that use the traditional texture lookup. |
| 368 | void emitColor(GrGLShaderBuilder* builder, |
| 369 | const char* gradientTValue, |
| 370 | EffectKey key, |
| 371 | const char* outputColor, |
| 372 | const char* inputColor, |
| 373 | const GrGLShaderBuilder::TextureSamplerArray& samplers); |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 374 | |
| 375 | private: |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 376 | SkScalar fCachedYCoord; |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 377 | GrGLUniformManager::UniformHandle fFSYUni; |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 378 | GrGLUniformManager::UniformHandle fColorStartUni; |
| 379 | GrGLUniformManager::UniformHandle fColorMidUni; |
| 380 | GrGLUniformManager::UniformHandle fColorEndUni; |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 381 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 382 | typedef GrGLEffect INHERITED; |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 383 | }; |
| 384 | |
| 385 | #endif |
| 386 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 387 | #endif |