From c624d9d212c4168fc6c202a8535ddff8a3bfb16a Mon Sep 17 00:00:00 2001 From: Brian Osman Date: Wed, 8 Mar 2017 11:42:02 -0500 Subject: Refactor GrColorSpaceXformHelper Nonlinear blending mode is going to (sometimes) require additional uniforms and shader code for color space transformation. This change just alters the usage of the helper struct so that we can hide any new logic (without having to change all the FPs that use it). BUG=skia: Change-Id: I913478a387973f5bad5aa09a29f85d21daacab94 Reviewed-on: https://skia-review.googlesource.com/9414 Reviewed-by: Robert Phillips Commit-Queue: Brian Osman --- src/gpu/effects/GrBicubicEffect.cpp | 11 +++++----- src/gpu/effects/GrSimpleTextureEffect.cpp | 10 ++++------ src/gpu/effects/GrTextureDomain.cpp | 10 ++++------ src/gpu/glsl/GrGLSLColorSpaceXformHelper.h | 32 +++++++++++++++++------------- src/gpu/glsl/GrGLSLShaderBuilder.cpp | 7 ++++--- 5 files changed, 35 insertions(+), 35 deletions(-) (limited to 'src/gpu') diff --git a/src/gpu/effects/GrBicubicEffect.cpp b/src/gpu/effects/GrBicubicEffect.cpp index 5f06fc166a..ec7ad79475 100644 --- a/src/gpu/effects/GrBicubicEffect.cpp +++ b/src/gpu/effects/GrBicubicEffect.cpp @@ -32,7 +32,7 @@ private: typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; UniformHandle fImageIncrementUni; - UniformHandle fColorSpaceXformUni; + GrGLSLColorSpaceXformHelper fColorSpaceHelper; GrTextureDomain::GLDomain fDomain; typedef GrGLSLFragmentProcessor INHERITED; @@ -48,8 +48,7 @@ void GrGLBicubicEffect::emitCode(EmitArgs& args) { const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni); - GrGLSLColorSpaceXformHelper colorSpaceHelper(uniformHandler, bicubicEffect.colorSpaceXform(), - &fColorSpaceXformUni); + fColorSpaceHelper.emitCode(uniformHandler, bicubicEffect.colorSpaceXform()); GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); @@ -107,9 +106,9 @@ void GrGLBicubicEffect::emitCode(EmitArgs& args) { y); } SkString bicubicColor("(wy.x * s0 + wy.y * s1 + wy.z * s2 + wy.w * s3)"); - if (colorSpaceHelper.getXformMatrix()) { + if (fColorSpaceHelper.isValid()) { SkString xformedColor; - fragBuilder->appendColorGamutXform(&xformedColor, bicubicColor.c_str(), &colorSpaceHelper); + fragBuilder->appendColorGamutXform(&xformedColor, bicubicColor.c_str(), &fColorSpaceHelper); bicubicColor.swap(xformedColor); } fragBuilder->codeAppendf("%s = %s;", @@ -127,7 +126,7 @@ void GrGLBicubicEffect::onSetData(const GrGLSLProgramDataManager& pdman, pdman.set2fv(fImageIncrementUni, 1, imageIncrement); fDomain.setData(pdman, bicubicEffect.domain(), texture); if (SkToBool(bicubicEffect.colorSpaceXform())) { - pdman.setSkMatrix44(fColorSpaceXformUni, bicubicEffect.colorSpaceXform()->srcToDst()); + fColorSpaceHelper.setData(pdman, bicubicEffect.colorSpaceXform()); } } diff --git a/src/gpu/effects/GrSimpleTextureEffect.cpp b/src/gpu/effects/GrSimpleTextureEffect.cpp index bd4e7206f8..8492f5b80b 100644 --- a/src/gpu/effects/GrSimpleTextureEffect.cpp +++ b/src/gpu/effects/GrSimpleTextureEffect.cpp @@ -42,9 +42,7 @@ class GrGLSimpleTextureEffect : public GrGLSLFragmentProcessor { public: void emitCode(EmitArgs& args) override { const GrSimpleTextureEffect& textureEffect = args.fFp.cast(); - GrGLSLColorSpaceXformHelper colorSpaceHelper(args.fUniformHandler, - textureEffect.colorSpaceXform(), - &fColorSpaceXformUni); + fColorSpaceHelper.emitCode(args.fUniformHandler, textureEffect.colorSpaceXform()); GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; fragBuilder->codeAppendf("%s = ", args.fOutputColor); @@ -52,7 +50,7 @@ public: args.fTexSamplers[0], args.fTransformedCoords[0].c_str(), args.fTransformedCoords[0].getType(), - &colorSpaceHelper); + &fColorSpaceHelper); fragBuilder->codeAppend(";"); } @@ -66,14 +64,14 @@ protected: void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& processor) override { const GrSimpleTextureEffect& textureEffect = processor.cast(); if (SkToBool(textureEffect.colorSpaceXform())) { - pdman.setSkMatrix44(fColorSpaceXformUni, textureEffect.colorSpaceXform()->srcToDst()); + fColorSpaceHelper.setData(pdman, textureEffect.colorSpaceXform()); } } private: typedef GrGLSLFragmentProcessor INHERITED; - UniformHandle fColorSpaceXformUni; + GrGLSLColorSpaceXformHelper fColorSpaceHelper; }; /////////////////////////////////////////////////////////////////////////////// diff --git a/src/gpu/effects/GrTextureDomain.cpp b/src/gpu/effects/GrTextureDomain.cpp index 2ac6af8375..7a43328bc1 100644 --- a/src/gpu/effects/GrTextureDomain.cpp +++ b/src/gpu/effects/GrTextureDomain.cpp @@ -321,9 +321,7 @@ GrGLSLFragmentProcessor* GrTextureDomainEffect::onCreateGLSLInstance() const { GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); - GrGLSLColorSpaceXformHelper colorSpaceHelper(args.fUniformHandler, - tde.colorSpaceXform(), - &fColorSpaceXformUni); + fColorSpaceHelper.emitCode(args.fUniformHandler, tde.colorSpaceXform()); fGLDomain.sampleTexture(fragBuilder, args.fUniformHandler, args.fShaderCaps, @@ -332,7 +330,7 @@ GrGLSLFragmentProcessor* GrTextureDomainEffect::onCreateGLSLInstance() const { coords2D, args.fTexSamplers[0], args.fInputColor, - &colorSpaceHelper); + &fColorSpaceHelper); } protected: @@ -341,13 +339,13 @@ GrGLSLFragmentProcessor* GrTextureDomainEffect::onCreateGLSLInstance() const { const GrTextureDomain& domain = tde.fTextureDomain; fGLDomain.setData(pdman, domain, tde.textureSampler(0).texture()); if (SkToBool(tde.colorSpaceXform())) { - pdman.setSkMatrix44(fColorSpaceXformUni, tde.colorSpaceXform()->srcToDst()); + fColorSpaceHelper.setData(pdman, tde.colorSpaceXform()); } } private: GrTextureDomain::GLDomain fGLDomain; - UniformHandle fColorSpaceXformUni; + GrGLSLColorSpaceXformHelper fColorSpaceHelper; }; return new GLSLProcessor; diff --git a/src/gpu/glsl/GrGLSLColorSpaceXformHelper.h b/src/gpu/glsl/GrGLSLColorSpaceXformHelper.h index 5e112f9f91..1571b06231 100644 --- a/src/gpu/glsl/GrGLSLColorSpaceXformHelper.h +++ b/src/gpu/glsl/GrGLSLColorSpaceXformHelper.h @@ -12,29 +12,33 @@ #include "GrGLSLUniformHandler.h" /** - * Stack helper class to assist with using GrColorSpaceXform within an FP's emitCode function. - * This injects the uniform declaration, and stores the information needed to generate correct - * gamut-transformation shader code. + * Helper class to assist with using GrColorSpaceXform within an FP. This manages all of the + * uniforms needed, and can be passed to shader builder functions to automatically generate the + * correct color space transformation code. */ class GrGLSLColorSpaceXformHelper : public SkNoncopyable { public: - GrGLSLColorSpaceXformHelper(GrGLSLUniformHandler* uniformHandler, - GrColorSpaceXform* colorSpaceXform, - GrGLSLProgramDataManager::UniformHandle* handle) { - SkASSERT(uniformHandler && handle); + GrGLSLColorSpaceXformHelper() : fValid(false) {} + + void emitCode(GrGLSLUniformHandler* uniformHandler, GrColorSpaceXform* colorSpaceXform) { + SkASSERT(uniformHandler); if (colorSpaceXform) { - *handle = uniformHandler->addUniform(kFragment_GrShaderFlag, kMat44f_GrSLType, - kDefault_GrSLPrecision, "ColorXform", - &fXformMatrix); - } else { - fXformMatrix = nullptr; + fGamutXformVar = uniformHandler->addUniform(kFragment_GrShaderFlag, kMat44f_GrSLType, + kDefault_GrSLPrecision, "ColorXform"); + fValid = true; } } - const char* getXformMatrix() const { return fXformMatrix; } + void setData(const GrGLSLProgramDataManager& pdman, GrColorSpaceXform* colorSpaceXform) { + pdman.setSkMatrix44(fGamutXformVar, colorSpaceXform->srcToDst()); + } + + bool isValid() const { return fValid; } + GrGLSLProgramDataManager::UniformHandle const gamutXformUniform() { return fGamutXformVar; } private: - const char* fXformMatrix; + GrGLSLProgramDataManager::UniformHandle fGamutXformVar; + bool fValid; }; #endif diff --git a/src/gpu/glsl/GrGLSLShaderBuilder.cpp b/src/gpu/glsl/GrGLSLShaderBuilder.cpp index 17f7f10287..865876611a 100644 --- a/src/gpu/glsl/GrGLSLShaderBuilder.cpp +++ b/src/gpu/glsl/GrGLSLShaderBuilder.cpp @@ -91,7 +91,7 @@ void GrGLSLShaderBuilder::appendTextureLookup(SamplerHandle samplerHandle, const char* coordName, GrSLType varyingType, GrGLSLColorSpaceXformHelper* colorXformHelper) { - if (colorXformHelper && colorXformHelper->getXformMatrix()) { + if (colorXformHelper && colorXformHelper->isValid()) { // With a color gamut transform, we need to wrap the lookup in another function call SkString lookup; this->appendTextureLookup(&lookup, samplerHandle, coordName, varyingType); @@ -109,7 +109,7 @@ void GrGLSLShaderBuilder::appendTextureLookupAndModulate( GrGLSLColorSpaceXformHelper* colorXformHelper) { SkString lookup; this->appendTextureLookup(&lookup, samplerHandle, coordName, varyingType); - if (colorXformHelper && colorXformHelper->getXformMatrix()) { + if (colorXformHelper && colorXformHelper->isValid()) { SkString xform; this->appendColorGamutXform(&xform, lookup.c_str(), colorXformHelper); this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(xform)).c_str()); @@ -141,8 +141,9 @@ void GrGLSLShaderBuilder::appendColorGamutXform(SkString* out, functionBody.c_str(), &colorGamutXformFuncName); + GrGLSLUniformHandler* uniformHandler = fProgramBuilder->uniformHandler(); out->appendf("%s(%s, %s)", colorGamutXformFuncName.c_str(), srcColor, - colorXformHelper->getXformMatrix()); + uniformHandler->getUniformCStr(colorXformHelper->gamutXformUniform())); } void GrGLSLShaderBuilder::appendColorGamutXform(const char* srcColor, -- cgit v1.2.3