| 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 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 } | 196 } |
| 197 | 197 |
| 198 static inline int next_dither_toggle16(int toggle) { | 198 static inline int next_dither_toggle16(int toggle) { |
| 199 return toggle ^ SkGradientShaderBase::kDitherStride16; | 199 return toggle ^ SkGradientShaderBase::kDitherStride16; |
| 200 } | 200 } |
| 201 | 201 |
| 202 /////////////////////////////////////////////////////////////////////////////// | 202 /////////////////////////////////////////////////////////////////////////////// |
| 203 | 203 |
| 204 #if SK_SUPPORT_GPU | 204 #if SK_SUPPORT_GPU |
| 205 | 205 |
| 206 #include "GrCoordTransform.h" |
| 206 #include "gl/GrGLEffect.h" | 207 #include "gl/GrGLEffect.h" |
| 207 #include "gl/GrGLEffectMatrix.h" | |
| 208 | 208 |
| 209 class GrEffectStage; | 209 class GrEffectStage; |
| 210 class GrBackendEffectFactory; | 210 class GrBackendEffectFactory; |
| 211 | 211 |
| 212 /* | 212 /* |
| 213 * The interpretation of the texture matrix depends on the sample mode. The | 213 * The interpretation of the texture matrix depends on the sample mode. The |
| 214 * texture matrix is applied both when the texture coordinates are explicit | 214 * texture matrix is applied both when the texture coordinates are explicit |
| 215 * and when vertex positions are used as texture coordinates. In the latter | 215 * and when vertex positions are used as texture coordinates. In the latter |
| 216 * case the texture matrix is applied to the pre-view-matrix position | 216 * case the texture matrix is applied to the pre-view-matrix position |
| 217 * values. | 217 * values. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 240 | 240 |
| 241 GrGradientEffect(GrContext* ctx, | 241 GrGradientEffect(GrContext* ctx, |
| 242 const SkGradientShaderBase& shader, | 242 const SkGradientShaderBase& shader, |
| 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;} | |
| 251 | 250 |
| 252 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags
) const SK_OVERRIDE; | 251 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags
) const SK_OVERRIDE; |
| 253 | 252 |
| 254 enum ColorType { | 253 enum ColorType { |
| 255 kTwo_ColorType, | 254 kTwo_ColorType, |
| 256 kThree_ColorType, | 255 kThree_ColorType, |
| 257 kTexture_ColorType | 256 kTexture_ColorType |
| 258 }; | 257 }; |
| 259 | 258 |
| 260 ColorType getColorType() const { return fColorType; } | 259 ColorType getColorType() const { return fColorType; } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 283 */ | 282 */ |
| 284 static const int kMaxRandomGradientColors = 4; | 283 static const int kMaxRandomGradientColors = 4; |
| 285 static int RandomGradientParams(SkRandom* r, | 284 static int RandomGradientParams(SkRandom* r, |
| 286 SkColor colors[kMaxRandomGradientColors], | 285 SkColor colors[kMaxRandomGradientColors], |
| 287 SkScalar** stops, | 286 SkScalar** stops, |
| 288 SkShader::TileMode* tm); | 287 SkShader::TileMode* tm); |
| 289 | 288 |
| 290 virtual bool onIsEqual(const GrEffect& effect) const SK_OVERRIDE; | 289 virtual bool onIsEqual(const GrEffect& effect) const SK_OVERRIDE; |
| 291 | 290 |
| 292 private: | 291 private: |
| 292 static const GrCoordSet kCoordSet = kLocal_GrCoordSet; |
| 293 | 293 |
| 294 enum { | 294 enum { |
| 295 kMaxAnalyticColors = 3 // if more colors use texture | 295 kMaxAnalyticColors = 3 // if more colors use texture |
| 296 }; | 296 }; |
| 297 | 297 |
| 298 GrCoordTransform fCoordTransform; |
| 298 GrTextureAccess fTextureAccess; | 299 GrTextureAccess fTextureAccess; |
| 299 SkScalar fYCoord; | 300 SkScalar fYCoord; |
| 300 GrTextureStripAtlas* fAtlas; | 301 GrTextureStripAtlas* fAtlas; |
| 301 int fRow; | 302 int fRow; |
| 302 SkMatrix fMatrix; | |
| 303 bool fIsOpaque; | 303 bool fIsOpaque; |
| 304 ColorType fColorType; | 304 ColorType fColorType; |
| 305 SkColor fColors[kMaxAnalyticColors]; | 305 SkColor fColors[kMaxAnalyticColors]; |
| 306 PremulType fPremulType; // This only changes behavior for two and three colo
r special cases. | 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. | 307 // It is already baked into to the table for texture
gradients. |
| 308 typedef GrEffect INHERITED; | 308 typedef GrEffect INHERITED; |
| 309 | 309 |
| 310 }; | 310 }; |
| 311 | 311 |
| 312 /////////////////////////////////////////////////////////////////////////////// | 312 /////////////////////////////////////////////////////////////////////////////// |
| 313 | 313 |
| 314 // Base class for GL gradient effects | 314 // Base class for GL gradient effects |
| 315 class GrGLGradientEffect : public GrGLEffect { | 315 class GrGLGradientEffect : public GrGLEffect { |
| 316 public: | 316 public: |
| 317 GrGLGradientEffect(const GrBackendEffectFactory& factory); | 317 GrGLGradientEffect(const GrBackendEffectFactory& factory); |
| 318 virtual ~GrGLGradientEffect(); | 318 virtual ~GrGLGradientEffect(); |
| 319 | 319 |
| 320 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE; | 320 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE; |
| 321 | 321 |
| 322 protected: | 322 protected: |
| 323 /** | |
| 324 * Subclasses must reserve the lower kMatrixKeyBitCnt of their key for use b
y | |
| 325 * GrGLGradientEffect. | |
| 326 */ | |
| 327 enum { | 323 enum { |
| 328 kMatrixKeyBitCnt = GrGLEffectMatrix::kKeyBits, | |
| 329 kMatrixKeyMask = (1 << kMatrixKeyBitCnt) - 1, | |
| 330 | |
| 331 kPremulTypeKeyBitCnt = 1, | 324 kPremulTypeKeyBitCnt = 1, |
| 332 kPremulTypeMask = 1 << kMatrixKeyBitCnt, | 325 kPremulTypeMask = 1, |
| 333 kPremulBeforeInterpKey = kPremulTypeMask, | 326 kPremulBeforeInterpKey = kPremulTypeMask, |
| 334 | 327 |
| 335 kTwoColorKey = 2 << (kMatrixKeyBitCnt + kPremulTypeKeyBitCnt), | 328 kTwoColorKey = 2 << kPremulTypeKeyBitCnt, |
| 336 kThreeColorKey = 3 << (kMatrixKeyBitCnt + kPremulTypeKeyBitCnt), | 329 kThreeColorKey = 3 << kPremulTypeKeyBitCnt, |
| 337 kColorKeyMask = kTwoColorKey | kThreeColorKey, | 330 kColorKeyMask = kTwoColorKey | kThreeColorKey, |
| 338 kColorKeyBitCnt = 2, | 331 kColorKeyBitCnt = 2, |
| 339 | 332 |
| 340 // Subclasses must shift any key bits they produce up by this amount | 333 // Subclasses must shift any key bits they produce up by this amount |
| 341 // and combine with the result of GenBaseGradientKey. | 334 // and combine with the result of GenBaseGradientKey. |
| 342 kBaseKeyBitCnt = (kMatrixKeyBitCnt + kPremulTypeKeyBitCnt + kColorKeyBit
Cnt) | 335 kBaseKeyBitCnt = (kPremulTypeKeyBitCnt + kColorKeyBitCnt) |
| 343 }; | 336 }; |
| 344 | 337 |
| 345 static GrGradientEffect::ColorType ColorTypeFromKey(EffectKey key){ | 338 static GrGradientEffect::ColorType ColorTypeFromKey(EffectKey key){ |
| 346 if (kTwoColorKey == (key & kColorKeyMask)) { | 339 if (kTwoColorKey == (key & kColorKeyMask)) { |
| 347 return GrGradientEffect::kTwo_ColorType; | 340 return GrGradientEffect::kTwo_ColorType; |
| 348 } else if (kThreeColorKey == (key & kColorKeyMask)) { | 341 } else if (kThreeColorKey == (key & kColorKeyMask)) { |
| 349 return GrGradientEffect::kThree_ColorType; | 342 return GrGradientEffect::kThree_ColorType; |
| 350 } else {return GrGradientEffect::kTexture_ColorType;} | 343 } else {return GrGradientEffect::kTexture_ColorType;} |
| 351 } | 344 } |
| 352 | 345 |
| 353 static GrGradientEffect::PremulType PremulTypeFromKey(EffectKey key){ | 346 static GrGradientEffect::PremulType PremulTypeFromKey(EffectKey key){ |
| 354 if (kPremulBeforeInterpKey == (key & kPremulTypeMask)) { | 347 if (kPremulBeforeInterpKey == (key & kPremulTypeMask)) { |
| 355 return GrGradientEffect::kBeforeInterp_PremulType; | 348 return GrGradientEffect::kBeforeInterp_PremulType; |
| 356 } else { | 349 } else { |
| 357 return GrGradientEffect::kAfterInterp_PremulType; | 350 return GrGradientEffect::kAfterInterp_PremulType; |
| 358 } | 351 } |
| 359 } | 352 } |
| 360 | 353 |
| 361 /** | 354 /** |
| 362 * Subclasses must call this. It will return a value restricted to the lower
kBaseKeyBitCnt | 355 * Subclasses must call this. It will return a value restricted to the lower
kBaseKeyBitCnt |
| 363 * bits. | 356 * bits. |
| 364 */ | 357 */ |
| 365 static EffectKey GenBaseGradientKey(const GrDrawEffect&); | 358 static EffectKey GenBaseGradientKey(const GrDrawEffect&); |
| 366 | 359 |
| 367 /** | |
| 368 * Inserts code to implement the GrGradientEffect's matrix. This should be c
alled before a | |
| 369 * subclass emits its own code. The name of the 2D coords is output via fsCo
ordName and already | |
| 370 * incorporates any perspective division. The caller can also optionally ret
rieve the name of | |
| 371 * the varying inserted in the VS and its type, which may be either vec2f or
vec3f depending | |
| 372 * upon whether the matrix has perspective or not. It is not necessary to ma
sk the key before | |
| 373 * calling. | |
| 374 */ | |
| 375 void setupMatrix(GrGLShaderBuilder* builder, | |
| 376 EffectKey key, | |
| 377 SkString* fsCoordName, | |
| 378 SkString* vsVaryingName = NULL, | |
| 379 GrSLType* vsVaryingType = NULL); | |
| 380 | |
| 381 // Emits the uniform used as the y-coord to texture samples in derived class
es. Subclasses | 360 // Emits the uniform used as the y-coord to texture samples in derived class
es. Subclasses |
| 382 // should call this method from their emitCode(). | 361 // should call this method from their emitCode(). |
| 383 void emitUniforms(GrGLShaderBuilder* builder, EffectKey key); | 362 void emitUniforms(GrGLShaderBuilder* builder, EffectKey key); |
| 384 | 363 |
| 385 | 364 |
| 386 // emit code that gets a fragment's color from an expression for t; Has bran
ches for 3 separate | 365 // emit code that gets a fragment's color from an expression for t; Has bran
ches for 3 separate |
| 387 // control flows inside -- 2 color gradients, 3 color symmetric gradients (b
oth using | 366 // control flows inside -- 2 color gradients, 3 color symmetric gradients (b
oth using |
| 388 // native GLSL mix), and 4+ color gradients that use the traditional texture
lookup. | 367 // native GLSL mix), and 4+ color gradients that use the traditional texture
lookup. |
| 389 void emitColor(GrGLShaderBuilder* builder, | 368 void emitColor(GrGLShaderBuilder* builder, |
| 390 const char* gradientTValue, | 369 const char* gradientTValue, |
| 391 EffectKey key, | 370 EffectKey key, |
| 392 const char* outputColor, | 371 const char* outputColor, |
| 393 const char* inputColor, | 372 const char* inputColor, |
| 394 const GrGLShaderBuilder::TextureSamplerArray& samplers); | 373 const GrGLShaderBuilder::TextureSamplerArray& samplers); |
| 395 | 374 |
| 396 private: | 375 private: |
| 397 static const GrEffect::CoordsType kCoordsType = GrEffect::kLocal_CoordsType; | |
| 398 | |
| 399 SkScalar fCachedYCoord; | 376 SkScalar fCachedYCoord; |
| 400 GrGLUniformManager::UniformHandle fFSYUni; | 377 GrGLUniformManager::UniformHandle fFSYUni; |
| 401 GrGLUniformManager::UniformHandle fColorStartUni; | 378 GrGLUniformManager::UniformHandle fColorStartUni; |
| 402 GrGLUniformManager::UniformHandle fColorMidUni; | 379 GrGLUniformManager::UniformHandle fColorMidUni; |
| 403 GrGLUniformManager::UniformHandle fColorEndUni; | 380 GrGLUniformManager::UniformHandle fColorEndUni; |
| 404 GrGLEffectMatrix fEffectMatrix; | |
| 405 | 381 |
| 406 typedef GrGLEffect INHERITED; | 382 typedef GrGLEffect INHERITED; |
| 407 }; | 383 }; |
| 408 | 384 |
| 409 #endif | 385 #endif |
| 410 | 386 |
| 411 #endif | 387 #endif |
| OLD | NEW |