| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SkGradientShaderPriv_DEFINED | 8 #ifndef SkGradientShaderPriv_DEFINED |
| 9 #define SkGradientShaderPriv_DEFINED | 9 #define SkGradientShaderPriv_DEFINED |
| 10 | 10 |
| 11 #include "SkGradientBitmapCache.h" | 11 #include "SkGradientBitmapCache.h" |
| 12 #include "SkGradientShader.h" | 12 #include "SkGradientShader.h" |
| 13 #include "SkClampRange.h" | 13 #include "SkClampRange.h" |
| 14 #include "SkColorPriv.h" | 14 #include "SkColorPriv.h" |
| 15 #include "SkReadBuffer.h" | 15 #include "SkReadBuffer.h" |
| 16 #include "SkWriteBuffer.h" | 16 #include "SkWriteBuffer.h" |
| 17 #include "SkMallocPixelRef.h" | 17 #include "SkMallocPixelRef.h" |
| 18 #include "SkUtils.h" | 18 #include "SkUtils.h" |
| 19 #include "SkShader.h" | 19 #include "SkShader.h" |
| 20 #include "SkOnce.h" | 20 #include "SkOnce.h" |
| 21 | 21 |
| 22 #define GR_GL_USE_ACCURATE_HARD_STOP_GRADIENTS 1 | |
| 23 | |
| 24 static inline void sk_memset32_dither(uint32_t dst[], uint32_t v0, uint32_t v1, | 22 static inline void sk_memset32_dither(uint32_t dst[], uint32_t v0, uint32_t v1, |
| 25 int count) { | 23 int count) { |
| 26 if (count > 0) { | 24 if (count > 0) { |
| 27 if (v0 == v1) { | 25 if (v0 == v1) { |
| 28 sk_memset32(dst, v0, count); | 26 sk_memset32(dst, v0, count); |
| 29 } else { | 27 } else { |
| 30 int pairs = count >> 1; | 28 int pairs = count >> 1; |
| 31 for (int i = 0; i < pairs; i++) { | 29 for (int i = 0; i < pairs; i++) { |
| 32 *dst++ = v0; | 30 *dst++ = v0; |
| 33 *dst++ = v1; | 31 *dst++ = v1; |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 | 121 |
| 124 const uint16_t* getCache16(); | 122 const uint16_t* getCache16(); |
| 125 const SkPMColor* getCache32(); | 123 const SkPMColor* getCache32(); |
| 126 | 124 |
| 127 SkMallocPixelRef* getCache32PixelRef() const { return fCache32PixelRef;
} | 125 SkMallocPixelRef* getCache32PixelRef() const { return fCache32PixelRef;
} |
| 128 | 126 |
| 129 unsigned getAlpha() const { return fCacheAlpha; } | 127 unsigned getAlpha() const { return fCacheAlpha; } |
| 130 bool getDither() const { return fCacheDither; } | 128 bool getDither() const { return fCacheDither; } |
| 131 | 129 |
| 132 private: | 130 private: |
| 133 // Working pointers. If either is nullptr, we need to recompute the corr
esponding | 131 // Working pointers. If either is nullptr, we need to recompute the corr
esponding cache values. |
| 134 // cache values. | |
| 135 uint16_t* fCache16; | 132 uint16_t* fCache16; |
| 136 SkPMColor* fCache32; | 133 SkPMColor* fCache32; |
| 137 | 134 |
| 138 uint16_t* fCache16Storage; // Storage for fCache16, allocated
on demand. | 135 uint16_t* fCache16Storage; // Storage for fCache16, allocated
on demand. |
| 139 SkMallocPixelRef* fCache32PixelRef; | 136 SkMallocPixelRef* fCache32PixelRef; |
| 140 const unsigned fCacheAlpha; // The alpha value we used when we
computed the cache. | 137 const unsigned fCacheAlpha; // The alpha value we used when we
computed the cache. |
| 141 // Larger than 8bits so we can sto
re uninitialized | 138 // Larger than 8bits so we can sto
re uninitialized |
| 142 // value. | 139 // value. |
| 143 const bool fCacheDither; // The dither flag used when we co
mputed the cache. | 140 const bool fCacheDither; // The dither flag used when we co
mputed the cache. |
| 144 | 141 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 kCache32Count = (1 << kCache32Bits), | 190 kCache32Count = (1 << kCache32Bits), |
| 194 kCache32Shift = 16 - kCache32Bits, | 191 kCache32Shift = 16 - kCache32Bits, |
| 195 kSqrt32Shift = 8 - kCache32Bits, | 192 kSqrt32Shift = 8 - kCache32Bits, |
| 196 | 193 |
| 197 /// This value is used to *read* the dither cache; it may be 0 | 194 /// This value is used to *read* the dither cache; it may be 0 |
| 198 /// if dithering is disabled. | 195 /// if dithering is disabled. |
| 199 kDitherStride32 = kCache32Count, | 196 kDitherStride32 = kCache32Count, |
| 200 kDitherStride16 = kCache16Count, | 197 kDitherStride16 = kCache16Count, |
| 201 }; | 198 }; |
| 202 | 199 |
| 200 enum GpuColorType { |
| 201 kTwo_GpuColorType, |
| 202 kThree_GpuColorType, // Symmetric three color |
| 203 kTexture_GpuColorType |
| 204 }; |
| 205 |
| 206 // Determines and returns the gradient is a two color gradient, symmetric th
ree color gradient |
| 207 // or other (texture gradient). If it is two or symmetric three color, the c
olors array will |
| 208 // also be filled with the gradient colors |
| 209 GpuColorType getGpuColorType(SkColor colors[3]) const; |
| 210 |
| 203 uint32_t getGradFlags() const { return fGradFlags; } | 211 uint32_t getGradFlags() const { return fGradFlags; } |
| 204 | 212 |
| 205 protected: | 213 protected: |
| 206 class GradientShaderBase4fContext; | 214 class GradientShaderBase4fContext; |
| 207 | 215 |
| 208 SkGradientShaderBase(SkReadBuffer& ); | 216 SkGradientShaderBase(SkReadBuffer& ); |
| 209 void flatten(SkWriteBuffer&) const override; | 217 void flatten(SkWriteBuffer&) const override; |
| 210 SK_TO_STRING_OVERRIDE() | 218 SK_TO_STRING_OVERRIDE() |
| 211 | 219 |
| 212 const SkMatrix fPtsToUnit; | 220 const SkMatrix fPtsToUnit; |
| 213 TileMode fTileMode; | 221 TileMode fTileMode; |
| 214 TileProc fTileProc; | 222 TileProc fTileProc; |
| 223 int fColorCount; |
| 215 uint8_t fGradFlags; | 224 uint8_t fGradFlags; |
| 216 | 225 |
| 217 struct Rec { | 226 struct Rec { |
| 218 SkFixed fPos; // 0...1 | 227 SkFixed fPos; // 0...1 |
| 219 uint32_t fScale; // (1 << 24) / range | 228 uint32_t fScale; // (1 << 24) / range |
| 220 }; | 229 }; |
| 221 Rec* fRecs; | 230 Rec* fRecs; |
| 222 | 231 |
| 223 void commonAsAGradient(GradientInfo*, bool flipGrad = false) const; | 232 void commonAsAGradient(GradientInfo*, bool flipGrad = false) const; |
| 224 | 233 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 238 private: | 247 private: |
| 239 enum { | 248 enum { |
| 240 kColorStorageCount = 4, // more than this many colors, and we'll use sk_
malloc for the space | 249 kColorStorageCount = 4, // more than this many colors, and we'll use sk_
malloc for the space |
| 241 | 250 |
| 242 kStorageSize = kColorStorageCount * (sizeof(SkColor) + sizeof(SkScalar)
+ sizeof(Rec)) | 251 kStorageSize = kColorStorageCount * (sizeof(SkColor) + sizeof(SkScalar)
+ sizeof(Rec)) |
| 243 }; | 252 }; |
| 244 SkColor fStorage[(kStorageSize + 3) >> 2]; | 253 SkColor fStorage[(kStorageSize + 3) >> 2]; |
| 245 public: | 254 public: |
| 246 SkColor* fOrigColors; // original colors, before modulation by paint in c
ontext. | 255 SkColor* fOrigColors; // original colors, before modulation by paint in c
ontext. |
| 247 SkScalar* fOrigPos; // original positions | 256 SkScalar* fOrigPos; // original positions |
| 248 int fColorCount; | |
| 249 | |
| 250 SkTArray<sk_sp<SkShader>> fSubGradients; | |
| 251 | 257 |
| 252 bool colorsAreOpaque() const { return fColorsAreOpaque; } | 258 bool colorsAreOpaque() const { return fColorsAreOpaque; } |
| 253 | 259 |
| 254 TileMode getTileMode() const { return fTileMode; } | |
| 255 Rec* getRecs() const { return fRecs; } | |
| 256 | |
| 257 private: | 260 private: |
| 258 bool fColorsAreOpaque; | 261 bool fColorsAreOpaque; |
| 259 | 262 |
| 260 GradientShaderCache* refCache(U8CPU alpha, bool dither) const; | 263 GradientShaderCache* refCache(U8CPU alpha, bool dither) const; |
| 261 mutable SkMutex fCacheMutex; | 264 mutable SkMutex fCacheMutex; |
| 262 mutable SkAutoTUnref<GradientShaderCache> fCache; | 265 mutable SkAutoTUnref<GradientShaderCache> fCache; |
| 263 | 266 |
| 264 void initCommon(); | 267 void initCommon(); |
| 265 | 268 |
| 266 typedef SkShader INHERITED; | 269 typedef SkShader INHERITED; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 class GLSLProcessor; | 329 class GLSLProcessor; |
| 327 | 330 |
| 328 GrGradientEffect(GrContext* ctx, | 331 GrGradientEffect(GrContext* ctx, |
| 329 const SkGradientShaderBase& shader, | 332 const SkGradientShaderBase& shader, |
| 330 const SkMatrix& matrix, | 333 const SkMatrix& matrix, |
| 331 SkShader::TileMode tileMode); | 334 SkShader::TileMode tileMode); |
| 332 | 335 |
| 333 virtual ~GrGradientEffect(); | 336 virtual ~GrGradientEffect(); |
| 334 | 337 |
| 335 bool useAtlas() const { return SkToBool(-1 != fRow); } | 338 bool useAtlas() const { return SkToBool(-1 != fRow); } |
| 336 SkScalar getYCoord() const { return fYCoord; } | 339 SkScalar getYCoord() const { return fYCoord; }; |
| 337 | 340 |
| 338 enum ColorType { | 341 SkGradientShaderBase::GpuColorType getColorType() const { return fColorType;
} |
| 339 kTwo_ColorType, | |
| 340 kThree_ColorType, // Symmetric three color | |
| 341 kTexture_ColorType, | |
| 342 | |
| 343 #if GR_GL_USE_ACCURATE_HARD_STOP_GRADIENTS | |
| 344 kHardStopCentered_ColorType, // 0, 0.5, 0.5, 1 | |
| 345 kHardStopLeftEdged_ColorType, // 0, 0, 1 | |
| 346 kHardStopRightEdged_ColorType, // 0, 1, 1 | |
| 347 #endif | |
| 348 }; | |
| 349 | |
| 350 ColorType getColorType() const { return fColorType; } | |
| 351 | |
| 352 // Determines the type of gradient, one of: | |
| 353 // - Two-color | |
| 354 // - Symmetric three-color | |
| 355 // - Texture | |
| 356 // - Centered hard stop | |
| 357 // - Left-edged hard stop | |
| 358 // - Right-edged hard stop | |
| 359 ColorType determineColorTypeAndNumHardStops(const SkGradientShaderBase& shad
er); | |
| 360 | 342 |
| 361 enum PremulType { | 343 enum PremulType { |
| 362 kBeforeInterp_PremulType, | 344 kBeforeInterp_PremulType, |
| 363 kAfterInterp_PremulType, | 345 kAfterInterp_PremulType, |
| 364 }; | 346 }; |
| 365 | 347 |
| 366 PremulType getPremulType() const { return fPremulType; } | 348 PremulType getPremulType() const { return fPremulType; } |
| 367 | 349 |
| 368 const SkColor* getColors(int pos) const { | 350 const SkColor* getColors(int pos) const { |
| 369 SkASSERT(fColorType != kTexture_ColorType); | 351 SkASSERT(fColorType != SkGradientShaderBase::kTexture_GpuColorType); |
| 370 SkASSERT(pos < fColors.count()); | 352 SkASSERT((pos-1) <= fColorType); |
| 371 return &fColors[pos]; | 353 return &fColors[pos]; |
| 372 } | 354 } |
| 373 | 355 |
| 374 protected: | 356 protected: |
| 375 /** Populates a pair of arrays with colors and stop info to construct a rand
om gradient. | 357 /** Populates a pair of arrays with colors and stop info to construct a rand
om gradient. |
| 376 The function decides whether stop values should be used or not. The retu
rn value indicates | 358 The function decides whether stop values should be used or not. The retu
rn value indicates |
| 377 the number of colors, which will be capped by kMaxRandomGradientColors.
colors should be | 359 the number of colors, which will be capped by kMaxRandomGradientColors.
colors should be |
| 378 sized to be at least kMaxRandomGradientColors. stops is a pointer to an
array of at least | 360 sized to be at least kMaxRandomGradientColors. stops is a pointer to an
array of at least |
| 379 size kMaxRandomGradientColors. It may be updated to nullptr, indicating
that nullptr should | 361 size kMaxRandomGradientColors. It may be updated to nullptr, indicating
that nullptr should be |
| 380 be passed to the gradient factory rather than the array. | 362 passed to the gradient factory rather than the array. |
| 381 */ | 363 */ |
| 382 static const int kMaxRandomGradientColors = 4; | 364 static const int kMaxRandomGradientColors = 4; |
| 383 static int RandomGradientParams(SkRandom* r, | 365 static int RandomGradientParams(SkRandom* r, |
| 384 SkColor colors[kMaxRandomGradientColors], | 366 SkColor colors[kMaxRandomGradientColors], |
| 385 SkScalar** stops, | 367 SkScalar** stops, |
| 386 SkShader::TileMode* tm); | 368 SkShader::TileMode* tm); |
| 387 | 369 |
| 388 bool onIsEqual(const GrFragmentProcessor&) const override; | 370 bool onIsEqual(const GrFragmentProcessor&) const override; |
| 389 | 371 |
| 390 void onComputeInvariantOutput(GrInvariantOutput* inout) const override; | 372 void onComputeInvariantOutput(GrInvariantOutput* inout) const override; |
| 391 | 373 |
| 392 const GrCoordTransform& getCoordTransform() const { return fCoordTransform;
} | 374 const GrCoordTransform& getCoordTransform() const { return fCoordTransform;
} |
| 393 | 375 |
| 394 private: | 376 private: |
| 395 static const GrCoordSet kCoordSet = kLocal_GrCoordSet; | 377 static const GrCoordSet kCoordSet = kLocal_GrCoordSet; |
| 396 | 378 |
| 397 SkTDArray<SkColor> fColors; | |
| 398 SkTDArray<SkScalar> fPositions; | |
| 399 SkShader::TileMode fTileMode; | |
| 400 | |
| 401 GrCoordTransform fCoordTransform; | 379 GrCoordTransform fCoordTransform; |
| 402 GrTextureAccess fTextureAccess; | 380 GrTextureAccess fTextureAccess; |
| 403 SkScalar fYCoord; | 381 SkScalar fYCoord; |
| 404 GrTextureStripAtlas* fAtlas; | 382 GrTextureStripAtlas* fAtlas; |
| 405 int fRow; | 383 int fRow; |
| 406 bool fIsOpaque; | 384 bool fIsOpaque; |
| 407 ColorType fColorType; | 385 SkGradientShaderBase::GpuColorType fColorType; |
| 408 PremulType fPremulType; // This is already baked into the table for texture
gradients, and | 386 SkColor fColors[3]; // More than 3 colors we use texture |
| 409 // only changes behavior for gradients that don't us
e a texture. | 387 PremulType fPremulType; // This only changes behavior for two and three colo
r special cases. |
| 388 // It is already baked into to the table for texture
gradients. |
| 410 typedef GrFragmentProcessor INHERITED; | 389 typedef GrFragmentProcessor INHERITED; |
| 411 | 390 |
| 412 }; | 391 }; |
| 413 | 392 |
| 414 /////////////////////////////////////////////////////////////////////////////// | 393 /////////////////////////////////////////////////////////////////////////////// |
| 415 | 394 |
| 416 // Base class for GL gradient effects | 395 // Base class for GLSL gradient effects |
| 417 class GrGradientEffect::GLSLProcessor : public GrGLSLFragmentProcessor { | 396 class GrGradientEffect::GLSLProcessor : public GrGLSLFragmentProcessor { |
| 418 public: | 397 public: |
| 419 GLSLProcessor() { | 398 GLSLProcessor(); |
| 420 fCachedYCoord = SK_ScalarMax; | |
| 421 } | |
| 422 | 399 |
| 423 protected: | 400 protected: |
| 424 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override
; | 401 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override
; |
| 425 | 402 |
| 426 protected: | 403 protected: |
| 427 /** | 404 /** |
| 428 * Subclasses must call this. It will return a key for the part of the shade
r code controlled | 405 * Subclasses must call this. It will return a key for the part of the shade
r code controlled |
| 429 * by the base class. The subclasses must stick it in their key and then pas
s it to the below | 406 * by the base class. The subclasses must stick it in their key and then pas
s it to the below |
| 430 * emit* functions from their emitCode function. | 407 * emit* functions from their emitCode function. |
| 431 */ | 408 */ |
| 432 static uint32_t GenBaseGradientKey(const GrProcessor&); | 409 static uint32_t GenBaseGradientKey(const GrProcessor&); |
| 433 | 410 |
| 434 // Emits the uniform used as the y-coord to texture samples in derived class
es. Subclasses | 411 // Emits the uniform used as the y-coord to texture samples in derived class
es. Subclasses |
| 435 // should call this method from their emitCode(). | 412 // should call this method from their emitCode(). |
| 436 void emitUniforms(GrGLSLUniformHandler*, const GrGradientEffect&); | 413 void emitUniforms(GrGLSLUniformHandler*, const GrGradientEffect&); |
| 437 | 414 |
| 438 // Emit code that gets a fragment's color from an expression for t; has bran
ches for | 415 |
| 439 // several control flows inside -- 2-color gradients, 3-color symmetric grad
ients, 4+ | 416 // emit code that gets a fragment's color from an expression for t; Has bran
ches for 3 separate |
| 440 // color gradients that use the traditional texture lookup, as well as sever
al varieties | 417 // control flows inside -- 2 color gradients, 3 color symmetric gradients (b
oth using |
| 441 // of hard stop gradients | 418 // native GLSL mix), and 4+ color gradients that use the traditional texture
lookup. |
| 442 void emitColor(GrGLSLFPFragmentBuilder* fragBuilder, | 419 void emitColor(GrGLSLFPFragmentBuilder* fragBuilder, |
| 443 GrGLSLUniformHandler* uniformHandler, | 420 GrGLSLUniformHandler* uniformHandler, |
| 444 const GrGLSLCaps* caps, | 421 const GrGLSLCaps* caps, |
| 445 const GrGradientEffect&, | 422 const GrGradientEffect&, |
| 446 const char* gradientTValue, | 423 const char* gradientTValue, |
| 447 const char* outputColor, | 424 const char* outputColor, |
| 448 const char* inputColor, | 425 const char* inputColor, |
| 449 const SamplerHandle* texSamplers); | 426 const SamplerHandle* texSamplers); |
| 450 | 427 |
| 451 private: | 428 private: |
| 452 enum { | 429 enum { |
| 453 // First bit for premul before/after interp | 430 // First bit for premul before/after interp |
| 454 kPremulBeforeInterpKey = 1, | 431 kPremulBeforeInterpKey = 1, |
| 455 | 432 |
| 456 // Next three bits for 2/3 color type or different special | 433 // Next two bits for 2/3 color type (neither means using texture atlas) |
| 457 // hard stop cases (neither means using texture atlas) | 434 kTwoColorKey = 4, |
| 458 kTwoColorKey = 2, | 435 kThreeColorKey = 6, |
| 459 kThreeColorKey = 4, | |
| 460 #if GR_GL_USE_ACCURATE_HARD_STOP_GRADIENTS | |
| 461 kHardStopCenteredKey = 6, | |
| 462 kHardStopZeroZeroOneKey = 8, | |
| 463 kHardStopZeroOneOneKey = 10, | |
| 464 | |
| 465 // Next two bits for tile mode | |
| 466 kClampTileMode = 16, | |
| 467 kRepeatTileMode = 32, | |
| 468 kMirrorTileMode = 48, | |
| 469 | |
| 470 // Lower six bits for premul, 2/3 color type, and tile mode | |
| 471 kReservedBits = 6, | |
| 472 #endif | |
| 473 }; | 436 }; |
| 474 | 437 |
| 475 SkScalar fCachedYCoord; | 438 SkScalar fCachedYCoord; |
| 476 GrGLSLProgramDataManager::UniformHandle fColorsUni; | |
| 477 GrGLSLProgramDataManager::UniformHandle fFSYUni; | 439 GrGLSLProgramDataManager::UniformHandle fFSYUni; |
| 440 GrGLSLProgramDataManager::UniformHandle fColorStartUni; |
| 441 GrGLSLProgramDataManager::UniformHandle fColorMidUni; |
| 442 GrGLSLProgramDataManager::UniformHandle fColorEndUni; |
| 478 | 443 |
| 479 typedef GrGLSLFragmentProcessor INHERITED; | 444 typedef GrGLSLFragmentProcessor INHERITED; |
| 480 }; | 445 }; |
| 481 | 446 |
| 482 #endif | 447 #endif |
| 483 | 448 |
| 484 #endif | 449 #endif |
| OLD | NEW |