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