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