| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 #include "SkArithmeticMode.h" | 8 #include "SkArithmeticMode.h" |
| 9 #include "SkColorPriv.h" | 9 #include "SkColorPriv.h" |
| 10 #include "SkFlattenableBuffers.h" | 10 #include "SkFlattenableBuffers.h" |
| 11 #include "SkString.h" | 11 #include "SkString.h" |
| 12 #include "SkUnPreMultiply.h" | 12 #include "SkUnPreMultiply.h" |
| 13 #if SK_SUPPORT_GPU | 13 #if SK_SUPPORT_GPU |
| 14 #include "GrContext.h" | 14 #include "GrContext.h" |
| 15 #include "GrCoordTransform.h" |
| 15 #include "gl/GrGLEffect.h" | 16 #include "gl/GrGLEffect.h" |
| 16 #include "gl/GrGLEffectMatrix.h" | |
| 17 #include "GrTBackendEffectFactory.h" | 17 #include "GrTBackendEffectFactory.h" |
| 18 #include "SkImageFilterUtils.h" | 18 #include "SkImageFilterUtils.h" |
| 19 #endif | 19 #endif |
| 20 | 20 |
| 21 static const bool gUseUnpremul = false; | 21 static const bool gUseUnpremul = false; |
| 22 | 22 |
| 23 class SkArithmeticMode_scalar : public SkXfermode { | 23 class SkArithmeticMode_scalar : public SkXfermode { |
| 24 public: | 24 public: |
| 25 SkArithmeticMode_scalar(SkScalar k1, SkScalar k2, SkScalar k3, SkScalar k4)
{ | 25 SkArithmeticMode_scalar(SkScalar k1, SkScalar k2, SkScalar k3, SkScalar k4)
{ |
| 26 fK[0] = k1; | 26 fK[0] = k1; |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 class GrGLArithmeticEffect : public GrGLEffect { | 239 class GrGLArithmeticEffect : public GrGLEffect { |
| 240 public: | 240 public: |
| 241 GrGLArithmeticEffect(const GrBackendEffectFactory&, const GrDrawEffect&); | 241 GrGLArithmeticEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| 242 virtual ~GrGLArithmeticEffect(); | 242 virtual ~GrGLArithmeticEffect(); |
| 243 | 243 |
| 244 virtual void emitCode(GrGLShaderBuilder*, | 244 virtual void emitCode(GrGLShaderBuilder*, |
| 245 const GrDrawEffect&, | 245 const GrDrawEffect&, |
| 246 EffectKey, | 246 EffectKey, |
| 247 const char* outputColor, | 247 const char* outputColor, |
| 248 const char* inputColor, | 248 const char* inputColor, |
| 249 const TransformedCoordsArray&, |
| 249 const TextureSamplerArray&) SK_OVERRIDE; | 250 const TextureSamplerArray&) SK_OVERRIDE; |
| 250 | 251 |
| 251 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); | |
| 252 | |
| 253 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE; | 252 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE; |
| 254 | 253 |
| 255 private: | 254 private: |
| 256 static const GrEffect::CoordsType kCoordsType = GrEffect::kLocal_CoordsType; | |
| 257 GrGLEffectMatrix fBackgroundEffectMatrix; | |
| 258 GrGLUniformManager::UniformHandle fKUni; | 255 GrGLUniformManager::UniformHandle fKUni; |
| 259 | 256 |
| 260 typedef GrGLEffect INHERITED; | 257 typedef GrGLEffect INHERITED; |
| 261 }; | 258 }; |
| 262 | 259 |
| 263 /////////////////////////////////////////////////////////////////////////////// | 260 /////////////////////////////////////////////////////////////////////////////// |
| 264 | 261 |
| 265 class GrArithmeticEffect : public GrEffect { | 262 class GrArithmeticEffect : public GrEffect { |
| 266 public: | 263 public: |
| 267 static GrEffectRef* Create(float k1, float k2, float k3, float k4, GrTexture
* background) { | 264 static GrEffectRef* Create(float k1, float k2, float k3, float k4, GrTexture
* background) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 282 float k1() const { return fK1; } | 279 float k1() const { return fK1; } |
| 283 float k2() const { return fK2; } | 280 float k2() const { return fK2; } |
| 284 float k3() const { return fK3; } | 281 float k3() const { return fK3; } |
| 285 float k4() const { return fK4; } | 282 float k4() const { return fK4; } |
| 286 | 283 |
| 287 private: | 284 private: |
| 288 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; | 285 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; |
| 289 | 286 |
| 290 GrArithmeticEffect(float k1, float k2, float k3, float k4, GrTexture* backgr
ound); | 287 GrArithmeticEffect(float k1, float k2, float k3, float k4, GrTexture* backgr
ound); |
| 291 float fK1, fK2, fK3, fK4; | 288 float fK1, fK2, fK3, fK4; |
| 289 GrCoordTransform fBackgroundTransform; |
| 292 GrTextureAccess fBackgroundAccess; | 290 GrTextureAccess fBackgroundAccess; |
| 293 | 291 |
| 294 GR_DECLARE_EFFECT_TEST; | 292 GR_DECLARE_EFFECT_TEST; |
| 295 typedef GrEffect INHERITED; | 293 typedef GrEffect INHERITED; |
| 296 | 294 |
| 297 }; | 295 }; |
| 298 | 296 |
| 299 /////////////////////////////////////////////////////////////////////////////// | 297 /////////////////////////////////////////////////////////////////////////////// |
| 300 | 298 |
| 301 GrArithmeticEffect::GrArithmeticEffect(float k1, float k2, float k3, float k4, | 299 GrArithmeticEffect::GrArithmeticEffect(float k1, float k2, float k3, float k4, |
| 302 GrTexture* background) | 300 GrTexture* background) |
| 303 : fK1(k1), fK2(k2), fK3(k3), fK4(k4) { | 301 : fK1(k1), fK2(k2), fK3(k3), fK4(k4) { |
| 304 if (background) { | 302 if (background) { |
| 303 fBackgroundTransform.reset(kLocal_GrCoordSet, background); |
| 304 this->addCoordTransform(&fBackgroundTransform); |
| 305 fBackgroundAccess.reset(background); | 305 fBackgroundAccess.reset(background); |
| 306 this->addTextureAccess(&fBackgroundAccess); | 306 this->addTextureAccess(&fBackgroundAccess); |
| 307 } else { | 307 } else { |
| 308 this->setWillReadDstColor(); | 308 this->setWillReadDstColor(); |
| 309 } | 309 } |
| 310 } | 310 } |
| 311 | 311 |
| 312 GrArithmeticEffect::~GrArithmeticEffect() { | 312 GrArithmeticEffect::~GrArithmeticEffect() { |
| 313 } | 313 } |
| 314 | 314 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 327 | 327 |
| 328 void GrArithmeticEffect::getConstantColorComponents(GrColor* color, uint32_t* va
lidFlags) const { | 328 void GrArithmeticEffect::getConstantColorComponents(GrColor* color, uint32_t* va
lidFlags) const { |
| 329 // TODO: optimize this | 329 // TODO: optimize this |
| 330 *validFlags = 0; | 330 *validFlags = 0; |
| 331 } | 331 } |
| 332 | 332 |
| 333 /////////////////////////////////////////////////////////////////////////////// | 333 /////////////////////////////////////////////////////////////////////////////// |
| 334 | 334 |
| 335 GrGLArithmeticEffect::GrGLArithmeticEffect(const GrBackendEffectFactory& factory
, | 335 GrGLArithmeticEffect::GrGLArithmeticEffect(const GrBackendEffectFactory& factory
, |
| 336 const GrDrawEffect& drawEffect) | 336 const GrDrawEffect& drawEffect) |
| 337 : INHERITED(factory) | 337 : INHERITED(factory) { |
| 338 , fBackgroundEffectMatrix(kCoordsType) { | |
| 339 } | 338 } |
| 340 | 339 |
| 341 GrGLArithmeticEffect::~GrGLArithmeticEffect() { | 340 GrGLArithmeticEffect::~GrGLArithmeticEffect() { |
| 342 } | 341 } |
| 343 | 342 |
| 344 void GrGLArithmeticEffect::emitCode(GrGLShaderBuilder* builder, | 343 void GrGLArithmeticEffect::emitCode(GrGLShaderBuilder* builder, |
| 345 const GrDrawEffect& drawEffect, | 344 const GrDrawEffect& drawEffect, |
| 346 EffectKey key, | 345 EffectKey key, |
| 347 const char* outputColor, | 346 const char* outputColor, |
| 348 const char* inputColor, | 347 const char* inputColor, |
| 348 const TransformedCoordsArray& coords, |
| 349 const TextureSamplerArray& samplers) { | 349 const TextureSamplerArray& samplers) { |
| 350 | 350 |
| 351 GrTexture* backgroundTex = drawEffect.castEffect<GrArithmeticEffect>().backg
roundTexture(); | 351 GrTexture* backgroundTex = drawEffect.castEffect<GrArithmeticEffect>().backg
roundTexture(); |
| 352 const char* dstColor; | 352 const char* dstColor; |
| 353 if (backgroundTex) { | 353 if (backgroundTex) { |
| 354 SkString bgCoords; | |
| 355 GrSLType bgCoordsType = fBackgroundEffectMatrix.emitCode(builder, key, &
bgCoords, NULL, "BG"); | |
| 356 builder->fsCodeAppend("\t\tvec4 bgColor = "); | 354 builder->fsCodeAppend("\t\tvec4 bgColor = "); |
| 357 builder->fsAppendTextureLookup(samplers[0], bgCoords.c_str(), bgCoordsTy
pe); | 355 builder->fsAppendTextureLookup(samplers[0], coords[0].c_str(), coords[0]
.type()); |
| 358 builder->fsCodeAppendf(";\n"); | 356 builder->fsCodeAppendf(";\n"); |
| 359 dstColor = "bgColor"; | 357 dstColor = "bgColor"; |
| 360 } else { | 358 } else { |
| 361 dstColor = builder->dstColor(); | 359 dstColor = builder->dstColor(); |
| 362 } | 360 } |
| 363 | 361 |
| 364 SkASSERT(NULL != dstColor); | 362 SkASSERT(NULL != dstColor); |
| 365 fKUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, | 363 fKUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 366 kVec4f_GrSLType, "k"); | 364 kVec4f_GrSLType, "k"); |
| 367 const char* kUni = builder->getUniformCStr(fKUni); | 365 const char* kUni = builder->getUniformCStr(fKUni); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 386 if (gUseUnpremul) { | 384 if (gUseUnpremul) { |
| 387 builder->fsCodeAppendf("\t\t%s.rgb *= %s.a;\n", outputColor, outputColor
); | 385 builder->fsCodeAppendf("\t\t%s.rgb *= %s.a;\n", outputColor, outputColor
); |
| 388 } else { | 386 } else { |
| 389 builder->fsCodeAppendf("\t\t%s.rgb = min(%s.rgb, %s.a);\n", outputColor,
outputColor, outputColor); | 387 builder->fsCodeAppendf("\t\t%s.rgb = min(%s.rgb, %s.a);\n", outputColor,
outputColor, outputColor); |
| 390 } | 388 } |
| 391 } | 389 } |
| 392 | 390 |
| 393 void GrGLArithmeticEffect::setData(const GrGLUniformManager& uman, const GrDrawE
ffect& drawEffect) { | 391 void GrGLArithmeticEffect::setData(const GrGLUniformManager& uman, const GrDrawE
ffect& drawEffect) { |
| 394 const GrArithmeticEffect& arith = drawEffect.castEffect<GrArithmeticEffect>(
); | 392 const GrArithmeticEffect& arith = drawEffect.castEffect<GrArithmeticEffect>(
); |
| 395 uman.set4f(fKUni, arith.k1(), arith.k2(), arith.k3(), arith.k4()); | 393 uman.set4f(fKUni, arith.k1(), arith.k2(), arith.k3(), arith.k4()); |
| 396 GrTexture* bgTex = arith.backgroundTexture(); | |
| 397 if (bgTex) { | |
| 398 fBackgroundEffectMatrix.setData(uman, | |
| 399 GrEffect::MakeDivByTextureWHMatrix(bgTex
), | |
| 400 drawEffect, | |
| 401 bgTex); | |
| 402 } | |
| 403 } | |
| 404 | |
| 405 GrGLEffect::EffectKey GrGLArithmeticEffect::GenKey(const GrDrawEffect& drawEffec
t, const GrGLCaps&) { | |
| 406 const GrArithmeticEffect& effect = drawEffect.castEffect<GrArithmeticEffect>
(); | |
| 407 GrTexture* bgTex = effect.backgroundTexture(); | |
| 408 EffectKey bgKey = 0; | |
| 409 if (bgTex) { | |
| 410 bgKey = GrGLEffectMatrix::GenKey(GrEffect::MakeDivByTextureWHMatrix(bgTe
x), | |
| 411 drawEffect, | |
| 412 GrGLArithmeticEffect::kCoordsType, | |
| 413 bgTex); | |
| 414 } | |
| 415 return bgKey; | |
| 416 } | 394 } |
| 417 | 395 |
| 418 GrEffectRef* GrArithmeticEffect::TestCreate(SkRandom* rand, | 396 GrEffectRef* GrArithmeticEffect::TestCreate(SkRandom* rand, |
| 419 GrContext*, | 397 GrContext*, |
| 420 const GrDrawTargetCaps&, | 398 const GrDrawTargetCaps&, |
| 421 GrTexture*[]) { | 399 GrTexture*[]) { |
| 422 float k1 = rand->nextF(); | 400 float k1 = rand->nextF(); |
| 423 float k2 = rand->nextF(); | 401 float k2 = rand->nextF(); |
| 424 float k3 = rand->nextF(); | 402 float k3 = rand->nextF(); |
| 425 float k4 = rand->nextF(); | 403 float k4 = rand->nextF(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 443 background); | 421 background); |
| 444 } | 422 } |
| 445 return true; | 423 return true; |
| 446 } | 424 } |
| 447 | 425 |
| 448 #endif | 426 #endif |
| 449 | 427 |
| 450 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkArithmeticMode) | 428 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkArithmeticMode) |
| 451 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkArithmeticMode_scalar) | 429 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkArithmeticMode_scalar) |
| 452 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 430 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
| OLD | NEW |