From 9557c273d8b10ad057b7932875828de204a5d00e Mon Sep 17 00:00:00 2001 From: brianosman Date: Thu, 15 Sep 2016 06:59:15 -0700 Subject: [PATCH] Bundle common arguments to GrGradientEffect creation I'm going to be adding more (to deal with color spaces), so trying to fix this before it gets out of control. BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2332213007 Committed: https://skia.googlesource.com/skia/+/f605c16a030d791a37f589e6bddd7158ba5d62cd Review-Url: https://codereview.chromium.org/2332213007 --- src/effects/gradients/SkGradientShader.cpp | 23 ++-- src/effects/gradients/SkGradientShaderPriv.h | 21 +++- src/effects/gradients/SkLinearGradient.cpp | 18 ++-- src/effects/gradients/SkRadialGradient.cpp | 18 ++-- src/effects/gradients/SkSweepGradient.cpp | 14 ++- .../gradients/SkTwoPointConicalGradient.cpp | 6 +- .../gradients/SkTwoPointConicalGradient_gpu.cpp | 120 ++++++++------------- .../gradients/SkTwoPointConicalGradient_gpu.h | 3 +- 8 files changed, 95 insertions(+), 128 deletions(-) diff --git a/src/effects/gradients/SkGradientShader.cpp b/src/effects/gradients/SkGradientShader.cpp index eccc21a..14e6784 100644 --- a/src/effects/gradients/SkGradientShader.cpp +++ b/src/effects/gradients/SkGradientShader.cpp @@ -1332,10 +1332,8 @@ void GrGradientEffect::GLSLProcessor::emitColor(GrGLSLFPFragmentBuilder* fragBui ///////////////////////////////////////////////////////////////////// -GrGradientEffect::GrGradientEffect(GrContext* ctx, - const SkGradientShaderBase& shader, - const SkMatrix& matrix, - SkShader::TileMode tileMode) { +GrGradientEffect::GrGradientEffect(const CreateArgs& args) { + const SkGradientShaderBase& shader(*args.fShader); fIsOpaque = shader.isOpaque(); @@ -1354,7 +1352,7 @@ GrGradientEffect::GrGradientEffect(GrContext* ctx, } #if GR_GL_USE_ACCURATE_HARD_STOP_GRADIENTS - fTileMode = tileMode; + fTileMode = args.fTileMode; #endif switch (fColorType) { @@ -1374,7 +1372,7 @@ GrGradientEffect::GrGradientEffect(GrContext* ctx, fPremulType = kAfterInterp_PremulType; } - fCoordTransform.reset(kCoordSet, matrix); + fCoordTransform.reset(kCoordSet, *args.fMatrix); break; case kTexture_ColorType: @@ -1389,8 +1387,8 @@ GrGradientEffect::GrGradientEffect(GrContext* ctx, desc.fWidth = bitmap.width(); desc.fHeight = 32; desc.fRowHeight = bitmap.height(); - desc.fContext = ctx; - desc.fConfig = SkImageInfo2GrPixelConfig(bitmap.info(), *ctx->caps()); + desc.fContext = args.fContext; + desc.fConfig = SkImageInfo2GrPixelConfig(bitmap.info(), *args.fContext->caps()); fAtlas = GrTextureStripAtlas::GetAtlas(desc); SkASSERT(fAtlas); @@ -1398,21 +1396,22 @@ GrGradientEffect::GrGradientEffect(GrContext* ctx, // y-clamp. GrTextureParams params; params.setFilterMode(GrTextureParams::kBilerp_FilterMode); - params.setTileModeX(tileMode); + params.setTileModeX(args.fTileMode); fRow = fAtlas->lockRow(bitmap); if (-1 != fRow) { fYCoord = fAtlas->getYOffset(fRow)+SK_ScalarHalf*fAtlas->getNormalizedTexelHeight(); - fCoordTransform.reset(kCoordSet, matrix, fAtlas->getTexture(), params.filterMode()); + fCoordTransform.reset(kCoordSet, *args.fMatrix, fAtlas->getTexture(), + params.filterMode()); fTextureAccess.reset(fAtlas->getTexture(), params); } else { SkAutoTUnref texture( - GrRefCachedBitmapTexture(ctx, bitmap, params, + GrRefCachedBitmapTexture(args.fContext, bitmap, params, SkSourceGammaTreatment::kRespect)); if (!texture) { return; } - fCoordTransform.reset(kCoordSet, matrix, texture, params.filterMode()); + fCoordTransform.reset(kCoordSet, *args.fMatrix, texture, params.filterMode()); fTextureAccess.reset(texture, params); fYCoord = SK_ScalarHalf; } diff --git a/src/effects/gradients/SkGradientShaderPriv.h b/src/effects/gradients/SkGradientShaderPriv.h index ee30c9b..ec70929 100644 --- a/src/effects/gradients/SkGradientShaderPriv.h +++ b/src/effects/gradients/SkGradientShaderPriv.h @@ -323,12 +323,25 @@ class GrInvariantOutput; // Base class for Gr gradient effects class GrGradientEffect : public GrFragmentProcessor { public: + struct CreateArgs { + CreateArgs(GrContext* context, + const SkGradientShaderBase* shader, + const SkMatrix* matrix, + SkShader::TileMode tileMode) + : fContext(context) + , fShader(shader) + , fMatrix(matrix) + , fTileMode(tileMode) {} + + GrContext* fContext; + const SkGradientShaderBase* fShader; + const SkMatrix* fMatrix; + SkShader::TileMode fTileMode; + }; + class GLSLProcessor; - GrGradientEffect(GrContext* ctx, - const SkGradientShaderBase& shader, - const SkMatrix& matrix, - SkShader::TileMode tileMode); + GrGradientEffect(const CreateArgs&); virtual ~GrGradientEffect(); diff --git a/src/effects/gradients/SkLinearGradient.cpp b/src/effects/gradients/SkLinearGradient.cpp index 98b621e..a93235d 100644 --- a/src/effects/gradients/SkLinearGradient.cpp +++ b/src/effects/gradients/SkLinearGradient.cpp @@ -351,11 +351,8 @@ class GrLinearGradient : public GrGradientEffect { public: class GLSLLinearProcessor; - static sk_sp Make(GrContext* ctx, - const SkLinearGradient& shader, - const SkMatrix& matrix, - SkShader::TileMode tm) { - return sk_sp(new GrLinearGradient(ctx, shader, matrix, tm)); + static sk_sp Make(const CreateArgs& args) { + return sk_sp(new GrLinearGradient(args)); } virtual ~GrLinearGradient() { } @@ -363,11 +360,8 @@ public: const char* name() const override { return "Linear Gradient"; } private: - GrLinearGradient(GrContext* ctx, - const SkLinearGradient& shader, - const SkMatrix& matrix, - SkShader::TileMode tm) - : INHERITED(ctx, shader, matrix, tm) { + GrLinearGradient(const CreateArgs& args) + : INHERITED(args) { this->initClassID(); } @@ -467,8 +461,8 @@ sk_sp SkLinearGradient::asFragmentProcessor(const AsFPArgs& } matrix.postConcat(fPtsToUnit); - sk_sp inner( - GrLinearGradient::Make(args.fContext, *this, matrix, fTileMode)); + sk_sp inner(GrLinearGradient::Make( + GrGradientEffect::CreateArgs(args.fContext, this, &matrix, fTileMode))); return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner)); } diff --git a/src/effects/gradients/SkRadialGradient.cpp b/src/effects/gradients/SkRadialGradient.cpp index 1fbd171..5691188 100644 --- a/src/effects/gradients/SkRadialGradient.cpp +++ b/src/effects/gradients/SkRadialGradient.cpp @@ -246,11 +246,8 @@ class GrRadialGradient : public GrGradientEffect { public: class GLSLRadialProcessor; - static sk_sp Make(GrContext* ctx, - const SkRadialGradient& shader, - const SkMatrix& matrix, - SkShader::TileMode tm) { - return sk_sp(new GrRadialGradient(ctx, shader, matrix, tm)); + static sk_sp Make(const CreateArgs& args) { + return sk_sp(new GrRadialGradient(args)); } virtual ~GrRadialGradient() { } @@ -258,11 +255,8 @@ public: const char* name() const override { return "Radial Gradient"; } private: - GrRadialGradient(GrContext* ctx, - const SkRadialGradient& shader, - const SkMatrix& matrix, - SkShader::TileMode tm) - : INHERITED(ctx, shader, matrix, tm) { + GrRadialGradient(const CreateArgs& args) + : INHERITED(args) { this->initClassID(); } @@ -361,8 +355,8 @@ sk_sp SkRadialGradient::asFragmentProcessor(const AsFPArgs& matrix.postConcat(inv); } matrix.postConcat(fPtsToUnit); - sk_sp inner( - GrRadialGradient::Make(args.fContext, *this, matrix, fTileMode)); + sk_sp inner(GrRadialGradient::Make( + GrGradientEffect::CreateArgs(args.fContext, this, &matrix, fTileMode))); return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner)); } diff --git a/src/effects/gradients/SkSweepGradient.cpp b/src/effects/gradients/SkSweepGradient.cpp index bdb0e1c..0789237 100644 --- a/src/effects/gradients/SkSweepGradient.cpp +++ b/src/effects/gradients/SkSweepGradient.cpp @@ -129,19 +129,16 @@ class GrSweepGradient : public GrGradientEffect { public: class GLSLSweepProcessor; - static sk_sp Make(GrContext* ctx, const SkSweepGradient& shader, - const SkMatrix& m) { - return sk_sp(new GrSweepGradient(ctx, shader, m)); + static sk_sp Make(const CreateArgs& args) { + return sk_sp(new GrSweepGradient(args)); } virtual ~GrSweepGradient() { } const char* name() const override { return "Sweep Gradient"; } private: - GrSweepGradient(GrContext* ctx, - const SkSweepGradient& shader, - const SkMatrix& matrix) - : INHERITED(ctx, shader, matrix, SkShader::kClamp_TileMode) { + GrSweepGradient(const CreateArgs& args) + : INHERITED(args) { this->initClassID(); } @@ -250,7 +247,8 @@ sk_sp SkSweepGradient::asFragmentProcessor(const AsFPArgs& } matrix.postConcat(fPtsToUnit); - sk_sp inner(GrSweepGradient::Make(args.fContext, *this, matrix)); + sk_sp inner(GrSweepGradient::Make( + GrGradientEffect::CreateArgs(args.fContext, this, &matrix, SkShader::kClamp_TileMode))); return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner)); } diff --git a/src/effects/gradients/SkTwoPointConicalGradient.cpp b/src/effects/gradients/SkTwoPointConicalGradient.cpp index 9c09701..9558c1f 100644 --- a/src/effects/gradients/SkTwoPointConicalGradient.cpp +++ b/src/effects/gradients/SkTwoPointConicalGradient.cpp @@ -6,7 +6,6 @@ */ #include "SkTwoPointConicalGradient.h" -#include "SkTwoPointConicalGradient_gpu.h" struct TwoPtRadialContext { const TwoPtRadial& fRec; @@ -355,13 +354,14 @@ void SkTwoPointConicalGradient::flatten(SkWriteBuffer& buffer) const { #if SK_SUPPORT_GPU #include "SkGr.h" +#include "SkTwoPointConicalGradient_gpu.h" sk_sp SkTwoPointConicalGradient::asFragmentProcessor( const AsFPArgs& args) const { SkASSERT(args.fContext); SkASSERT(fPtsToUnit.isIdentity()); - sk_sp inner( - Gr2PtConicalGradientEffect::Make(args.fContext, *this, fTileMode, args.fLocalMatrix)); + sk_sp inner(Gr2PtConicalGradientEffect::Make( + GrGradientEffect::CreateArgs(args.fContext, this, args.fLocalMatrix, fTileMode))); return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner)); } diff --git a/src/effects/gradients/SkTwoPointConicalGradient_gpu.cpp b/src/effects/gradients/SkTwoPointConicalGradient_gpu.cpp index e89193f..b7cacc4 100644 --- a/src/effects/gradients/SkTwoPointConicalGradient_gpu.cpp +++ b/src/effects/gradients/SkTwoPointConicalGradient_gpu.cpp @@ -5,7 +5,6 @@ * found in the LICENSE file. */ -#include "SkTwoPointConicalGradient_gpu.h" #include "SkTwoPointConicalGradient.h" @@ -16,6 +15,8 @@ #include "glsl/GrGLSLFragmentShaderBuilder.h" #include "glsl/GrGLSLProgramDataManager.h" #include "glsl/GrGLSLUniformHandler.h" +#include "SkTwoPointConicalGradient_gpu.h" + // For brevity typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; @@ -62,11 +63,8 @@ class Edge2PtConicalEffect : public GrGradientEffect { public: class GLSLEdge2PtConicalProcessor; - static sk_sp Make(GrContext* ctx, - const SkTwoPointConicalGradient& shader, - const SkMatrix& matrix, - SkShader::TileMode tm) { - return sk_sp(new Edge2PtConicalEffect(ctx, shader, matrix, tm)); + static sk_sp Make(const CreateArgs& args) { + return sk_sp(new Edge2PtConicalEffect(args)); } virtual ~Edge2PtConicalEffect() {} @@ -93,14 +91,13 @@ private: this->fDiffRadius == s.fDiffRadius); } - Edge2PtConicalEffect(GrContext* ctx, - const SkTwoPointConicalGradient& shader, - const SkMatrix& matrix, - SkShader::TileMode tm) - : INHERITED(ctx, shader, matrix, tm), - fCenterX1(shader.getCenterX1()), - fRadius0(shader.getStartRadius()), - fDiffRadius(shader.getDiffRadius()){ + Edge2PtConicalEffect(const CreateArgs& args) + : INHERITED(args) { + const SkTwoPointConicalGradient& shader = + *static_cast(args.fShader); + fCenterX1 = shader.getCenterX1(); + fRadius0 = shader.getStartRadius(); + fDiffRadius = shader.getDiffRadius(); this->initClassID(); // We should only be calling this shader if we are degenerate case with touching circles // When deciding if we are in edge case, we scaled by the end radius for cases when the @@ -376,13 +373,9 @@ class FocalOutside2PtConicalEffect : public GrGradientEffect { public: class GLSLFocalOutside2PtConicalProcessor; - static sk_sp Make(GrContext* ctx, - const SkTwoPointConicalGradient& shader, - const SkMatrix& matrix, - SkShader::TileMode tm, - SkScalar focalX) { + static sk_sp Make(const CreateArgs& args, SkScalar focalX) { return sk_sp( - new FocalOutside2PtConicalEffect(ctx, shader, matrix, tm, focalX)); + new FocalOutside2PtConicalEffect(args, focalX)); } virtual ~FocalOutside2PtConicalEffect() { } @@ -406,14 +399,10 @@ private: this->fIsFlipped == s.fIsFlipped); } - FocalOutside2PtConicalEffect(GrContext* ctx, - const SkTwoPointConicalGradient& shader, - const SkMatrix& matrix, - SkShader::TileMode tm, - SkScalar focalX) - : INHERITED(ctx, shader, matrix, tm) + FocalOutside2PtConicalEffect(const CreateArgs& args, SkScalar focalX) + : INHERITED(args) , fFocalX(focalX) - , fIsFlipped(shader.isFlippedGrad()) { + , fIsFlipped(static_cast(args.fShader)->isFlippedGrad()) { this->initClassID(); } @@ -591,13 +580,9 @@ class FocalInside2PtConicalEffect : public GrGradientEffect { public: class GLSLFocalInside2PtConicalProcessor; - static sk_sp Make(GrContext* ctx, - const SkTwoPointConicalGradient& shader, - const SkMatrix& matrix, - SkShader::TileMode tm, - SkScalar focalX) { + static sk_sp Make(const CreateArgs& args, SkScalar focalX) { return sk_sp( - new FocalInside2PtConicalEffect(ctx, shader, matrix, tm, focalX)); + new FocalInside2PtConicalEffect(args, focalX)); } virtual ~FocalInside2PtConicalEffect() {} @@ -621,12 +606,8 @@ private: this->fFocalX == s.fFocalX); } - FocalInside2PtConicalEffect(GrContext* ctx, - const SkTwoPointConicalGradient& shader, - const SkMatrix& matrix, - SkShader::TileMode tm, - SkScalar focalX) - : INHERITED(ctx, shader, matrix, tm), fFocalX(focalX) { + FocalInside2PtConicalEffect(const CreateArgs& args, SkScalar focalX) + : INHERITED(args), fFocalX(focalX) { this->initClassID(); } @@ -837,13 +818,9 @@ class CircleInside2PtConicalEffect : public GrGradientEffect { public: class GLSLCircleInside2PtConicalProcessor; - static sk_sp Make(GrContext* ctx, - const SkTwoPointConicalGradient& shader, - const SkMatrix& matrix, - SkShader::TileMode tm, - const CircleConicalInfo& info) { + static sk_sp Make(const CreateArgs& args, const CircleConicalInfo& info) { return sk_sp( - new CircleInside2PtConicalEffect(ctx, shader, matrix, tm, info)); + new CircleInside2PtConicalEffect(args, info)); } virtual ~CircleInside2PtConicalEffect() {} @@ -871,12 +848,8 @@ private: this->fInfo.fC == s.fInfo.fC); } - CircleInside2PtConicalEffect(GrContext* ctx, - const SkTwoPointConicalGradient& shader, - const SkMatrix& matrix, - SkShader::TileMode tm, - const CircleConicalInfo& info) - : INHERITED(ctx, shader, matrix, tm), fInfo(info) { + CircleInside2PtConicalEffect(const CreateArgs& args, const CircleConicalInfo& info) + : INHERITED(args), fInfo(info) { this->initClassID(); } @@ -1060,13 +1033,9 @@ class CircleOutside2PtConicalEffect : public GrGradientEffect { public: class GLSLCircleOutside2PtConicalProcessor; - static sk_sp Make(GrContext* ctx, - const SkTwoPointConicalGradient& shader, - const SkMatrix& matrix, - SkShader::TileMode tm, - const CircleConicalInfo& info) { + static sk_sp Make(const CreateArgs& args, const CircleConicalInfo& info) { return sk_sp( - new CircleOutside2PtConicalEffect(ctx, shader, matrix, tm, info)); + new CircleOutside2PtConicalEffect(args, info)); } virtual ~CircleOutside2PtConicalEffect() {} @@ -1097,13 +1066,11 @@ private: this->fIsFlipped == s.fIsFlipped); } - CircleOutside2PtConicalEffect(GrContext* ctx, - const SkTwoPointConicalGradient& shader, - const SkMatrix& matrix, - SkShader::TileMode tm, - const CircleConicalInfo& info) - : INHERITED(ctx, shader, matrix, tm), fInfo(info) { + CircleOutside2PtConicalEffect(const CreateArgs& args, const CircleConicalInfo& info) + : INHERITED(args), fInfo(info) { this->initClassID(); + const SkTwoPointConicalGradient& shader = + *static_cast(args.fShader); if (shader.getStartRadius() != shader.getEndRadius()) { fTLimit = shader.getStartRadius() / (shader.getStartRadius() - shader.getEndRadius()); } else { @@ -1322,32 +1289,35 @@ void CircleOutside2PtConicalEffect::GLSLCircleOutside2PtConicalProcessor::GenKey ////////////////////////////////////////////////////////////////////////////// -sk_sp Gr2PtConicalGradientEffect::Make(GrContext* ctx, - const SkTwoPointConicalGradient& shader, - SkShader::TileMode tm, - const SkMatrix* localMatrix) { +sk_sp Gr2PtConicalGradientEffect::Make( + const GrGradientEffect::CreateArgs& args) { + const SkTwoPointConicalGradient& shader = + *static_cast(args.fShader); + SkMatrix matrix; if (!shader.getLocalMatrix().invert(&matrix)) { return nullptr; } - if (localMatrix) { + if (args.fMatrix) { SkMatrix inv; - if (!localMatrix->invert(&inv)) { + if (!args.fMatrix->invert(&inv)) { return nullptr; } matrix.postConcat(inv); } + GrGradientEffect::CreateArgs newArgs(args.fContext, args.fShader, &matrix, args.fTileMode); + if (shader.getStartRadius() < kErrorTol) { SkScalar focalX; ConicalType type = set_matrix_focal_conical(shader, &matrix, &focalX); if (type == kInside_ConicalType) { - return FocalInside2PtConicalEffect::Make(ctx, shader, matrix, tm, focalX); + return FocalInside2PtConicalEffect::Make(newArgs, focalX); } else if(type == kEdge_ConicalType) { set_matrix_edge_conical(shader, &matrix); - return Edge2PtConicalEffect::Make(ctx, shader, matrix, tm); + return Edge2PtConicalEffect::Make(newArgs); } else { - return FocalOutside2PtConicalEffect::Make(ctx, shader, matrix, tm, focalX); + return FocalOutside2PtConicalEffect::Make(newArgs, focalX); } } @@ -1355,12 +1325,12 @@ sk_sp Gr2PtConicalGradientEffect::Make(GrContext* ctx, ConicalType type = set_matrix_circle_conical(shader, &matrix, &info); if (type == kInside_ConicalType) { - return CircleInside2PtConicalEffect::Make(ctx, shader, matrix, tm, info); + return CircleInside2PtConicalEffect::Make(newArgs, info); } else if (type == kEdge_ConicalType) { set_matrix_edge_conical(shader, &matrix); - return Edge2PtConicalEffect::Make(ctx, shader, matrix, tm); + return Edge2PtConicalEffect::Make(newArgs); } else { - return CircleOutside2PtConicalEffect::Make(ctx, shader, matrix, tm, info); + return CircleOutside2PtConicalEffect::Make(newArgs, info); } } diff --git a/src/effects/gradients/SkTwoPointConicalGradient_gpu.h b/src/effects/gradients/SkTwoPointConicalGradient_gpu.h index 9dd058d..46edb1f 100644 --- a/src/effects/gradients/SkTwoPointConicalGradient_gpu.h +++ b/src/effects/gradients/SkTwoPointConicalGradient_gpu.h @@ -18,8 +18,7 @@ namespace Gr2PtConicalGradientEffect { * Creates an effect that produces a two point conical gradient based on the * shader passed in. */ - sk_sp Make(GrContext* ctx, const SkTwoPointConicalGradient& shader, - SkShader::TileMode tm, const SkMatrix* localMatrix); + sk_sp Make(const GrGradientEffect::CreateArgs& args); }; #endif -- 2.7.4