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