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