| 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 |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 const SkMatrix& matrix, | 243 const SkMatrix& matrix, |
| 244 SkShader::TileMode tileMode); | 244 SkShader::TileMode tileMode); |
| 245 | 245 |
| 246 virtual ~GrGradientEffect(); | 246 virtual ~GrGradientEffect(); |
| 247 | 247 |
| 248 bool useAtlas() const { return SkToBool(-1 != fRow); } | 248 bool useAtlas() const { return SkToBool(-1 != fRow); } |
| 249 SkScalar getYCoord() const { return fYCoord; }; | 249 SkScalar getYCoord() const { return fYCoord; }; |
| 250 const SkMatrix& getMatrix() const { return fMatrix;} | 250 const SkMatrix& getMatrix() const { return fMatrix;} |
| 251 | 251 |
| 252 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags
) const SK_OVERRIDE; | 252 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags
) const SK_OVERRIDE; |
| 253 |
| 254 enum ColorType { |
| 255 kTwo_ColorType, |
| 256 kThree_ColorType, |
| 257 kTexture_ColorType |
| 258 }; |
| 259 |
| 260 ColorType getColorType() const { return fColorType; } |
| 261 |
| 262 enum PremulType { |
| 263 kBeforeInterp_PremulType, |
| 264 kAfterInterp_PremulType, |
| 265 }; |
| 266 |
| 267 PremulType getPremulType() const { return fPremulType; } |
| 268 |
| 269 const SkColor* getColors(int pos) const { |
| 270 SkASSERT(fColorType != kTexture_ColorType); |
| 271 SkASSERT((pos-1) <= fColorType); |
| 272 return &fColors[pos]; |
| 273 } |
| 253 | 274 |
| 254 protected: | 275 protected: |
| 255 | 276 |
| 256 /** Populates a pair of arrays with colors and stop info to construct a rand
om gradient. | 277 /** Populates a pair of arrays with colors and stop info to construct a rand
om gradient. |
| 257 The function decides whether stop values should be used or not. The retu
rn value indicates | 278 The function decides whether stop values should be used or not. The retu
rn value indicates |
| 258 the number of colors, which will be capped by kMaxRandomGradientColors.
colors should be | 279 the number of colors, which will be capped by kMaxRandomGradientColors.
colors should be |
| 259 sized to be at least kMaxRandomGradientColors. stops is a pointer to an
array of at least | 280 sized to be at least kMaxRandomGradientColors. stops is a pointer to an
array of at least |
| 260 size kMaxRandomGradientColors. It may be updated to NULL, indicating tha
t NULL should be | 281 size kMaxRandomGradientColors. It may be updated to NULL, indicating tha
t NULL should be |
| 261 passed to the gradient factory rather than the array. | 282 passed to the gradient factory rather than the array. |
| 262 */ | 283 */ |
| 263 static const int kMaxRandomGradientColors = 4; | 284 static const int kMaxRandomGradientColors = 4; |
| 264 static int RandomGradientParams(SkMWCRandom* r, | 285 static int RandomGradientParams(SkMWCRandom* r, |
| 265 SkColor colors[kMaxRandomGradientColors], | 286 SkColor colors[kMaxRandomGradientColors], |
| 266 SkScalar** stops, | 287 SkScalar** stops, |
| 267 SkShader::TileMode* tm); | 288 SkShader::TileMode* tm); |
| 268 | 289 |
| 269 virtual bool onIsEqual(const GrEffect& effect) const SK_OVERRIDE; | 290 virtual bool onIsEqual(const GrEffect& effect) const SK_OVERRIDE; |
| 270 | 291 |
| 271 private: | 292 private: |
| 272 | 293 |
| 294 enum { |
| 295 kMaxAnalyticColors = 3 // if more colors use texture |
| 296 }; |
| 297 |
| 273 GrTextureAccess fTextureAccess; | 298 GrTextureAccess fTextureAccess; |
| 274 SkScalar fYCoord; | 299 SkScalar fYCoord; |
| 275 GrTextureStripAtlas* fAtlas; | 300 GrTextureStripAtlas* fAtlas; |
| 276 int fRow; | 301 int fRow; |
| 277 SkMatrix fMatrix; | 302 SkMatrix fMatrix; |
| 278 bool fIsOpaque; | 303 bool fIsOpaque; |
| 279 | 304 ColorType fColorType; |
| 305 SkColor fColors[kMaxAnalyticColors]; |
| 306 PremulType fPremulType; // This only changes behavior for two and three colo
r special cases. |
| 307 // It is already baked into to the table for texture
gradients. |
| 280 typedef GrEffect INHERITED; | 308 typedef GrEffect INHERITED; |
| 281 | 309 |
| 282 }; | 310 }; |
| 283 | 311 |
| 284 /////////////////////////////////////////////////////////////////////////////// | 312 /////////////////////////////////////////////////////////////////////////////// |
| 285 | 313 |
| 286 // Base class for GL gradient effects | 314 // Base class for GL gradient effects |
| 287 class GrGLGradientEffect : public GrGLEffect { | 315 class GrGLGradientEffect : public GrGLEffect { |
| 288 public: | 316 public: |
| 289 GrGLGradientEffect(const GrBackendEffectFactory& factory); | 317 GrGLGradientEffect(const GrBackendEffectFactory& factory); |
| 290 virtual ~GrGLGradientEffect(); | 318 virtual ~GrGLGradientEffect(); |
| 291 | 319 |
| 292 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE; | 320 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE; |
| 293 | 321 |
| 294 protected: | 322 protected: |
| 295 /** | 323 /** |
| 296 * Subclasses must reserve the lower kMatrixKeyBitCnt of their key for use b
y | 324 * Subclasses must reserve the lower kMatrixKeyBitCnt of their key for use b
y |
| 297 * GrGLGradientEffect. | 325 * GrGLGradientEffect. |
| 298 */ | 326 */ |
| 299 enum { | 327 enum { |
| 300 kMatrixKeyBitCnt = GrGLEffectMatrix::kKeyBits, | 328 kMatrixKeyBitCnt = GrGLEffectMatrix::kKeyBits, |
| 301 kMatrixKeyMask = (1 << kMatrixKeyBitCnt) - 1, | 329 kMatrixKeyMask = (1 << kMatrixKeyBitCnt) - 1, |
| 330 |
| 331 kPremulTypeKeyBitCnt = 1, |
| 332 kPremulTypeMask = 1 << kMatrixKeyBitCnt, |
| 333 kPremulBeforeInterpKey = kPremulTypeMask, |
| 334 |
| 335 kTwoColorKey = 2 << (kMatrixKeyBitCnt + kPremulTypeKeyBitCnt), |
| 336 kThreeColorKey = 3 << (kMatrixKeyBitCnt + kPremulTypeKeyBitCnt), |
| 337 kColorKeyMask = kTwoColorKey | kThreeColorKey, |
| 338 kColorKeyBitCnt = 2, |
| 339 |
| 340 // Subclasses must shift any key bits they produce up by this amount |
| 341 // and combine with the result of GenBaseGradientKey. |
| 342 kBaseKeyBitCnt = (kMatrixKeyBitCnt + kPremulTypeKeyBitCnt + kColorKeyBit
Cnt) |
| 302 }; | 343 }; |
| 303 | 344 |
| 345 static GrGradientEffect::ColorType ColorTypeFromKey(EffectKey key){ |
| 346 if (kTwoColorKey == (key & kColorKeyMask)) { |
| 347 return GrGradientEffect::kTwo_ColorType; |
| 348 } else if (kThreeColorKey == (key & kColorKeyMask)) { |
| 349 return GrGradientEffect::kThree_ColorType; |
| 350 } else {return GrGradientEffect::kTexture_ColorType;} |
| 351 } |
| 352 |
| 353 static GrGradientEffect::PremulType PremulTypeFromKey(EffectKey key){ |
| 354 if (kPremulBeforeInterpKey == (key & kPremulTypeMask)) { |
| 355 return GrGradientEffect::kBeforeInterp_PremulType; |
| 356 } else { |
| 357 return GrGradientEffect::kAfterInterp_PremulType; |
| 358 } |
| 359 } |
| 360 |
| 304 /** | 361 /** |
| 305 * Subclasses must call this. It will return a value restricted to the lower
kMatrixKeyBitCnt | 362 * Subclasses must call this. It will return a value restricted to the lower
kBaseKeyBitCnt |
| 306 * bits. | 363 * bits. |
| 307 */ | 364 */ |
| 308 static EffectKey GenMatrixKey(const GrDrawEffect&); | 365 static EffectKey GenBaseGradientKey(const GrDrawEffect&); |
| 309 | 366 |
| 310 /** | 367 /** |
| 311 * Inserts code to implement the GrGradientEffect's matrix. This should be c
alled before a | 368 * Inserts code to implement the GrGradientEffect's matrix. This should be c
alled before a |
| 312 * subclass emits its own code. The name of the 2D coords is output via fsCo
ordName and already | 369 * subclass emits its own code. The name of the 2D coords is output via fsCo
ordName and already |
| 313 * incorporates any perspective division. The caller can also optionally ret
rieve the name of | 370 * incorporates any perspective division. The caller can also optionally ret
rieve the name of |
| 314 * the varying inserted in the VS and its type, which may be either vec2f or
vec3f depending | 371 * the varying inserted in the VS and its type, which may be either vec2f or
vec3f depending |
| 315 * upon whether the matrix has perspective or not. It is not necessary to ma
sk the key before | 372 * upon whether the matrix has perspective or not. It is not necessary to ma
sk the key before |
| 316 * calling. | 373 * calling. |
| 317 */ | 374 */ |
| 318 void setupMatrix(GrGLShaderBuilder* builder, | 375 void setupMatrix(GrGLShaderBuilder* builder, |
| 319 EffectKey key, | 376 EffectKey key, |
| 320 SkString* fsCoordName, | 377 SkString* fsCoordName, |
| 321 SkString* vsVaryingName = NULL, | 378 SkString* vsVaryingName = NULL, |
| 322 GrSLType* vsVaryingType = NULL); | 379 GrSLType* vsVaryingType = NULL); |
| 323 | 380 |
| 324 // Emits the uniform used as the y-coord to texture samples in derived class
es. Subclasses | 381 // Emits the uniform used as the y-coord to texture samples in derived class
es. Subclasses |
| 325 // should call this method from their emitCode(). | 382 // should call this method from their emitCode(). |
| 326 void emitYCoordUniform(GrGLShaderBuilder* builder); | 383 void emitUniforms(GrGLShaderBuilder* builder, EffectKey key); |
| 327 | 384 |
| 328 // emit code that gets a fragment's color from an expression for t; for now
this always uses the | 385 |
| 329 // texture, but for simpler cases we'll be able to lerp. Subclasses should c
all this method from | 386 // emit code that gets a fragment's color from an expression for t; Has bran
ches for 3 separate |
| 330 // their emitCode(). | 387 // control flows inside -- 2 color gradients, 3 color symmetric gradients (b
oth using |
| 331 void emitColorLookup(GrGLShaderBuilder* builder, | 388 // native GLSL mix), and 4+ color gradients that use the traditional texture
lookup. |
| 332 const char* gradientTValue, | 389 void emitColor(GrGLShaderBuilder* builder, |
| 333 const char* outputColor, | 390 const char* gradientTValue, |
| 334 const char* inputColor, | 391 EffectKey key, |
| 335 const GrGLShaderBuilder::TextureSampler&); | 392 const char* outputColor, |
| 393 const char* inputColor, |
| 394 const GrGLShaderBuilder::TextureSamplerArray& samplers); |
| 336 | 395 |
| 337 private: | 396 private: |
| 338 static const GrEffect::CoordsType kCoordsType = GrEffect::kLocal_CoordsType; | 397 static const GrEffect::CoordsType kCoordsType = GrEffect::kLocal_CoordsType; |
| 339 | 398 |
| 340 SkScalar fCachedYCoord; | 399 SkScalar fCachedYCoord; |
| 341 GrGLUniformManager::UniformHandle fFSYUni; | 400 GrGLUniformManager::UniformHandle fFSYUni; |
| 401 GrGLUniformManager::UniformHandle fColorStartUni; |
| 402 GrGLUniformManager::UniformHandle fColorMidUni; |
| 403 GrGLUniformManager::UniformHandle fColorEndUni; |
| 342 GrGLEffectMatrix fEffectMatrix; | 404 GrGLEffectMatrix fEffectMatrix; |
| 343 | 405 |
| 344 typedef GrGLEffect INHERITED; | 406 typedef GrGLEffect INHERITED; |
| 345 }; | 407 }; |
| 346 | 408 |
| 347 #endif | 409 #endif |
| 348 | 410 |
| 349 #endif | 411 #endif |
| OLD | NEW |