rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2012 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #ifndef SkGradientShaderPriv_DEFINED |
| 10 | #define SkGradientShaderPriv_DEFINED |
| 11 | |
| 12 | #include "SkGradientShader.h" |
| 13 | #include "SkClampRange.h" |
| 14 | #include "SkColorPriv.h" |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 15 | #include "SkFlattenableBuffers.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 | |
| 23 | #ifndef SK_DISABLE_DITHER_32BIT_GRADIENT |
| 24 | #define USE_DITHER_32BIT_GRADIENT |
| 25 | #endif |
| 26 | |
| 27 | static void sk_memset32_dither(uint32_t dst[], uint32_t v0, uint32_t v1, |
| 28 | int count) { |
| 29 | if (count > 0) { |
| 30 | if (v0 == v1) { |
| 31 | sk_memset32(dst, v0, count); |
| 32 | } else { |
| 33 | int pairs = count >> 1; |
| 34 | for (int i = 0; i < pairs; i++) { |
| 35 | *dst++ = v0; |
| 36 | *dst++ = v1; |
| 37 | } |
| 38 | if (count & 1) { |
| 39 | *dst = v0; |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // Clamp |
| 46 | |
| 47 | static SkFixed clamp_tileproc(SkFixed x) { |
| 48 | return SkClampMax(x, 0xFFFF); |
| 49 | } |
| 50 | |
| 51 | // Repeat |
| 52 | |
| 53 | static SkFixed repeat_tileproc(SkFixed x) { |
| 54 | return x & 0xFFFF; |
| 55 | } |
| 56 | |
| 57 | // Mirror |
| 58 | |
| 59 | // Visual Studio 2010 (MSC_VER=1600) optimizes bit-shift code incorrectly. |
| 60 | // See http://code.google.com/p/skia/issues/detail?id=472 |
| 61 | #if defined(_MSC_VER) && (_MSC_VER >= 1600) |
| 62 | #pragma optimize("", off) |
| 63 | #endif |
| 64 | |
| 65 | static inline SkFixed mirror_tileproc(SkFixed x) { |
| 66 | int s = x << 15 >> 31; |
| 67 | return (x ^ s) & 0xFFFF; |
| 68 | } |
| 69 | |
| 70 | #if defined(_MSC_VER) && (_MSC_VER >= 1600) |
| 71 | #pragma optimize("", on) |
| 72 | #endif |
| 73 | |
| 74 | /////////////////////////////////////////////////////////////////////////////// |
| 75 | |
| 76 | typedef SkFixed (*TileProc)(SkFixed); |
| 77 | |
| 78 | /////////////////////////////////////////////////////////////////////////////// |
| 79 | |
| 80 | static const TileProc gTileProcs[] = { |
| 81 | clamp_tileproc, |
| 82 | repeat_tileproc, |
| 83 | mirror_tileproc |
| 84 | }; |
| 85 | |
| 86 | /////////////////////////////////////////////////////////////////////////////// |
| 87 | |
| 88 | class SkGradientShaderBase : public SkShader { |
| 89 | public: |
| 90 | SkGradientShaderBase(const SkColor colors[], const SkScalar pos[], |
| 91 | int colorCount, SkShader::TileMode mode, SkUnitMapper* mapper); |
| 92 | virtual ~SkGradientShaderBase(); |
| 93 | |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 94 | virtual bool setContext(const SkBitmap&, const SkPaint&, const SkMatrix&) SK_OVERRIDE; |
| 95 | virtual uint32_t getFlags() SK_OVERRIDE { return fFlags; } |
| 96 | virtual bool isOpaque() const SK_OVERRIDE; |
| 97 | |
| 98 | void getGradientTableBitmap(SkBitmap*) const; |
| 99 | |
| 100 | enum { |
| 101 | /// Seems like enough for visual accuracy. TODO: if pos[] deserves |
| 102 | /// it, use a larger cache. |
| 103 | kCache16Bits = 8, |
| 104 | kGradient16Length = (1 << kCache16Bits), |
| 105 | /// Each cache gets 1 extra entry at the end so we don't have to |
| 106 | /// test for end-of-cache in lerps. This is also the value used |
| 107 | /// to stride *writes* into the dither cache; it must not be zero. |
| 108 | /// Total space for a cache is 2x kCache16Count entries: one |
| 109 | /// regular cache, one for dithering. |
| 110 | kCache16Count = kGradient16Length + 1, |
| 111 | kCache16Shift = 16 - kCache16Bits, |
| 112 | kSqrt16Shift = 8 - kCache16Bits, |
| 113 | |
| 114 | /// Seems like enough for visual accuracy. TODO: if pos[] deserves |
| 115 | /// it, use a larger cache. |
| 116 | kCache32Bits = 8, |
| 117 | kGradient32Length = (1 << kCache32Bits), |
| 118 | /// Each cache gets 1 extra entry at the end so we don't have to |
| 119 | /// test for end-of-cache in lerps. This is also the value used |
| 120 | /// to stride *writes* into the dither cache; it must not be zero. |
| 121 | /// Total space for a cache is 2x kCache32Count entries: one |
| 122 | /// regular cache, one for dithering. |
| 123 | kCache32Count = kGradient32Length + 1, |
| 124 | kCache32Shift = 16 - kCache32Bits, |
| 125 | kSqrt32Shift = 8 - kCache32Bits, |
| 126 | |
| 127 | /// This value is used to *read* the dither cache; it may be 0 |
| 128 | /// if dithering is disabled. |
| 129 | #ifdef USE_DITHER_32BIT_GRADIENT |
| 130 | kDitherStride32 = kCache32Count, |
| 131 | #else |
| 132 | kDitherStride32 = 0, |
| 133 | #endif |
| 134 | kDitherStride16 = kCache16Count, |
| 135 | kLerpRemainderMask32 = (1 << (16 - kCache32Bits)) - 1 |
| 136 | }; |
| 137 | |
| 138 | |
| 139 | protected: |
| 140 | SkGradientShaderBase(SkFlattenableReadBuffer& ); |
| 141 | virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; |
| 142 | |
| 143 | SkUnitMapper* fMapper; |
| 144 | SkMatrix fPtsToUnit; // set by subclass |
| 145 | SkMatrix fDstToIndex; |
| 146 | SkMatrix::MapXYProc fDstToIndexProc; |
| 147 | TileMode fTileMode; |
| 148 | TileProc fTileProc; |
| 149 | int fColorCount; |
| 150 | uint8_t fDstToIndexClass; |
| 151 | uint8_t fFlags; |
| 152 | |
| 153 | struct Rec { |
| 154 | SkFixed fPos; // 0...1 |
| 155 | uint32_t fScale; // (1 << 24) / range |
| 156 | }; |
| 157 | Rec* fRecs; |
| 158 | |
| 159 | const uint16_t* getCache16() const; |
| 160 | const SkPMColor* getCache32() const; |
| 161 | |
| 162 | void commonAsAGradient(GradientInfo*) const; |
| 163 | |
| 164 | private: |
| 165 | enum { |
| 166 | kColorStorageCount = 4, // more than this many colors, and we'll use sk_malloc for the space |
| 167 | |
| 168 | kStorageSize = kColorStorageCount * (sizeof(SkColor) + sizeof(Rec)) |
| 169 | }; |
| 170 | SkColor fStorage[(kStorageSize + 3) >> 2]; |
| 171 | SkColor* fOrigColors; // original colors, before modulation by paint in setContext |
| 172 | bool fColorsAreOpaque; |
| 173 | |
| 174 | mutable uint16_t* fCache16; // working ptr. If this is NULL, we need to recompute the cache values |
| 175 | mutable SkPMColor* fCache32; // working ptr. If this is NULL, we need to recompute the cache values |
| 176 | |
| 177 | mutable uint16_t* fCache16Storage; // storage for fCache16, allocated on demand |
| 178 | mutable SkMallocPixelRef* fCache32PixelRef; |
| 179 | mutable unsigned fCacheAlpha; // the alpha value we used when we computed the cache. larger than 8bits so we can store uninitialized value |
| 180 | |
| 181 | static void Build16bitCache(uint16_t[], SkColor c0, SkColor c1, int count); |
| 182 | static void Build32bitCache(SkPMColor[], SkColor c0, SkColor c1, int count, |
| 183 | U8CPU alpha); |
| 184 | void setCacheAlpha(U8CPU alpha) const; |
| 185 | void initCommon(); |
| 186 | |
| 187 | typedef SkShader INHERITED; |
| 188 | }; |
| 189 | |
| 190 | /////////////////////////////////////////////////////////////////////////////// |
| 191 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 192 | #if SK_SUPPORT_GPU |
| 193 | |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 194 | #include "gl/GrGLEffect.h" |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 195 | #include "gl/GrGLEffectMatrix.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 196 | |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 197 | class GrEffectStage; |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 198 | class GrBackendEffectFactory; |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 199 | |
| 200 | /* |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 201 | * 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] | 202 | * texture matrix is applied both when the texture coordinates are explicit |
| 203 | * 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] | 204 | * 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] | 205 | * values. |
| 206 | * |
| 207 | * Normal SampleMode |
| 208 | * The post-matrix texture coordinates are in normalize space with (0,0) at |
| 209 | * the top-left and (1,1) at the bottom right. |
| 210 | * RadialGradient |
| 211 | * The matrix specifies the radial gradient parameters. |
| 212 | * (0,0) in the post-matrix space is center of the radial gradient. |
| 213 | * Radial2Gradient |
| 214 | * Matrix transforms to space where first circle is centered at the |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 215 | * origin. The second circle will be centered (x, 0) where x may be |
| 216 | * 0 and is provided by setRadial2Params. The post-matrix space is |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 217 | * normalized such that 1 is the second radius - first radius. |
| 218 | * SweepGradient |
| 219 | * The angle from the origin of texture coordinates in post-matrix space |
| 220 | * determines the gradient value. |
| 221 | */ |
| 222 | |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 223 | class GrTextureStripAtlas; |
| 224 | |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 225 | // Base class for Gr gradient effects |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 226 | class GrGradientEffect : public GrEffect { |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 227 | public: |
| 228 | |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 229 | GrGradientEffect(GrContext* ctx, |
| 230 | const SkGradientShaderBase& shader, |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 231 | const SkMatrix& matrix, |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 232 | SkShader::TileMode tileMode); |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 233 | |
| 234 | virtual ~GrGradientEffect(); |
| 235 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 236 | virtual const GrTextureAccess& textureAccess(int index) const SK_OVERRIDE; |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 237 | |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 238 | bool useAtlas() const { return SkToBool(-1 != fRow); } |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 239 | SkScalar getYCoord() const { return fYCoord; }; |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 240 | const SkMatrix& getMatrix() const { return fMatrix;} |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 241 | |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 242 | virtual bool isEqual(const GrEffect& effect) const SK_OVERRIDE { |
| 243 | const GrGradientEffect& s = static_cast<const GrGradientEffect&>(effect); |
| 244 | return INHERITED::isEqual(effect) && this->useAtlas() == s.useAtlas() && |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 245 | fYCoord == s.getYCoord() && fMatrix.cheapEqualTo(s.getMatrix()); |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 246 | } |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 247 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 248 | protected: |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 249 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 250 | /** Populates a pair of arrays with colors and stop info to construct a random gradient. |
| 251 | The function decides whether stop values should be used or not. The return value indicates |
| 252 | the number of colors, which will be capped by kMaxRandomGradientColors. colors should be |
| 253 | sized to be at least kMaxRandomGradientColors. stops is a pointer to an array of at least |
| 254 | size kMaxRandomGradientColors. It may be updated to NULL, indicating that NULL should be |
| 255 | passed to the gradient factory rather than the array. |
| 256 | */ |
| 257 | static const int kMaxRandomGradientColors = 4; |
| 258 | static int RandomGradientParams(SkRandom* r, |
| 259 | SkColor colors[kMaxRandomGradientColors], |
| 260 | SkScalar** stops, |
| 261 | SkShader::TileMode* tm); |
| 262 | |
| 263 | private: |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 264 | GrTextureAccess fTextureAccess; |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 265 | SkScalar fYCoord; |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 266 | GrTextureStripAtlas* fAtlas; |
| 267 | int fRow; |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 268 | SkMatrix fMatrix; |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 269 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 270 | typedef GrEffect INHERITED; |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 271 | |
| 272 | }; |
| 273 | |
| 274 | /////////////////////////////////////////////////////////////////////////////// |
| 275 | |
bsalomon@google.com | 8ea78d8 | 2012-10-24 20:11:30 +0000 | [diff] [blame] | 276 | // Base class for GL gradient effects |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 277 | class GrGLGradientEffect : public GrGLEffect { |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 278 | public: |
bsalomon@google.com | 0707c29 | 2012-10-25 21:45:42 +0000 | [diff] [blame] | 279 | GrGLGradientEffect(const GrBackendEffectFactory& factory); |
| 280 | virtual ~GrGLGradientEffect(); |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 281 | |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 282 | virtual void setData(const GrGLUniformManager&, const GrEffectStage&) SK_OVERRIDE; |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 283 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 284 | protected: |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 285 | /** |
| 286 | * Subclasses must reserve the lower kMatrixKeyBitCnt of their key for use by |
| 287 | * GrGLGradientEffect. |
| 288 | */ |
| 289 | enum { |
| 290 | kMatrixKeyBitCnt = GrGLEffectMatrix::kKeyBits, |
| 291 | kMatrixKeyMask = (1 << kMatrixKeyBitCnt) - 1, |
| 292 | }; |
| 293 | |
| 294 | /** |
| 295 | * Subclasses must call this. It will return a value restricted to the lower kMatrixKeyBitCnt |
| 296 | * bits. |
| 297 | */ |
| 298 | static EffectKey GenMatrixKey(const GrEffectStage& s); |
| 299 | |
| 300 | /** |
| 301 | * Inserts code to implement the GrGradientEffect's matrix. This should be called before a |
| 302 | * subclass emits its own code. The name of the 2D coords is output via fsCoordName and already |
| 303 | * incorporates any perspective division. The caller can also optionally retrieve the name of |
| 304 | * the varying inserted in the VS and its type, which may be either vec2f or vec3f depending |
| 305 | * upon whether the matrix has perspective or not. It is not necessary to mask the key before |
| 306 | * calling. |
| 307 | */ |
| 308 | void setupMatrix(GrGLShaderBuilder* builder, |
| 309 | EffectKey key, |
| 310 | const char* vertexCoords, |
| 311 | const char** fsCoordName, |
| 312 | const char** vsVaryingName = NULL, |
| 313 | GrSLType* vsVaryingType = NULL); |
| 314 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 315 | // Emits the uniform used as the y-coord to texture samples in derived classes. Subclasses |
| 316 | // should call this method from their emitCode(). |
| 317 | void emitYCoordUniform(GrGLShaderBuilder* builder); |
| 318 | |
| 319 | // emit code that gets a fragment's color from an expression for t; for now this always uses the |
| 320 | // texture, but for simpler cases we'll be able to lerp. Subclasses should call this method from |
| 321 | // their emitCode(). |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 322 | void emitColorLookup(GrGLShaderBuilder* builder, |
| 323 | const char* gradientTValue, |
| 324 | const char* outputColor, |
| 325 | const char* inputColor, |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 326 | const GrGLShaderBuilder::TextureSampler&); |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 327 | |
| 328 | private: |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 329 | SkScalar fCachedYCoord; |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 330 | GrGLUniformManager::UniformHandle fFSYUni; |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 331 | GrGLEffectMatrix fEffectMatrix; |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 332 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 333 | typedef GrGLEffect INHERITED; |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 334 | }; |
| 335 | |
| 336 | #endif |
| 337 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 338 | #endif |
| 339 | |