Bundle common arguments to GrGradientEffect creation
authorbrianosman <brianosman@google.com>
Wed, 14 Sep 2016 17:03:46 +0000 (10:03 -0700)
committerCommit bot <commit-bot@chromium.org>
Wed, 14 Sep 2016 17:03:46 +0000 (10:03 -0700)
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

Review-Url: https://codereview.chromium.org/2332213007

src/effects/gradients/SkGradientShader.cpp
src/effects/gradients/SkGradientShaderPriv.h
src/effects/gradients/SkLinearGradient.cpp
src/effects/gradients/SkRadialGradient.cpp
src/effects/gradients/SkSweepGradient.cpp
src/effects/gradients/SkTwoPointConicalGradient.cpp
src/effects/gradients/SkTwoPointConicalGradient_gpu.cpp
src/effects/gradients/SkTwoPointConicalGradient_gpu.h

index eccc21a..14e6784 100644 (file)
@@ -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<GrTexture> 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;
             }
index ee30c9b..ec70929 100644 (file)
@@ -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();
 
index 98b621e..a93235d 100644 (file)
@@ -351,11 +351,8 @@ class GrLinearGradient : public GrGradientEffect {
 public:
     class GLSLLinearProcessor;
 
-    static sk_sp<GrFragmentProcessor> Make(GrContext* ctx,
-                                           const SkLinearGradient& shader,
-                                           const SkMatrix& matrix,
-                                           SkShader::TileMode tm) {
-        return sk_sp<GrFragmentProcessor>(new GrLinearGradient(ctx, shader, matrix, tm));
+    static sk_sp<GrFragmentProcessor> Make(const CreateArgs& args) {
+        return sk_sp<GrFragmentProcessor>(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<GrLinearGradient>();
     }
 
@@ -467,8 +461,8 @@ sk_sp<GrFragmentProcessor> SkLinearGradient::asFragmentProcessor(const AsFPArgs&
     }
     matrix.postConcat(fPtsToUnit);
 
-    sk_sp<GrFragmentProcessor> inner(
-        GrLinearGradient::Make(args.fContext, *this, matrix, fTileMode));
+    sk_sp<GrFragmentProcessor> inner(GrLinearGradient::Make(
+        GrGradientEffect::CreateArgs(args.fContext, this, &matrix, fTileMode)));
     return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner));
 }
 
index 1fbd171..5691188 100644 (file)
@@ -246,11 +246,8 @@ class GrRadialGradient : public GrGradientEffect {
 public:
     class GLSLRadialProcessor;
 
-    static sk_sp<GrFragmentProcessor> Make(GrContext* ctx,
-                                           const SkRadialGradient& shader,
-                                           const SkMatrix& matrix,
-                                           SkShader::TileMode tm) {
-        return sk_sp<GrFragmentProcessor>(new GrRadialGradient(ctx, shader, matrix, tm));
+    static sk_sp<GrFragmentProcessor> Make(const CreateArgs& args) {
+        return sk_sp<GrFragmentProcessor>(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<GrRadialGradient>();
     }
 
@@ -361,8 +355,8 @@ sk_sp<GrFragmentProcessor> SkRadialGradient::asFragmentProcessor(const AsFPArgs&
         matrix.postConcat(inv);
     }
     matrix.postConcat(fPtsToUnit);
-    sk_sp<GrFragmentProcessor> inner(
-        GrRadialGradient::Make(args.fContext, *this, matrix, fTileMode));
+    sk_sp<GrFragmentProcessor> inner(GrRadialGradient::Make(
+        GrGradientEffect::CreateArgs(args.fContext, this, &matrix, fTileMode)));
     return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner));
 }
 
index bdb0e1c..0789237 100644 (file)
@@ -129,19 +129,16 @@ class GrSweepGradient : public GrGradientEffect {
 public:
     class GLSLSweepProcessor;
 
-    static sk_sp<GrFragmentProcessor> Make(GrContext* ctx, const SkSweepGradient& shader,
-                                           const SkMatrix& m) {
-        return sk_sp<GrFragmentProcessor>(new GrSweepGradient(ctx, shader, m));
+    static sk_sp<GrFragmentProcessor> Make(const CreateArgs& args) {
+        return sk_sp<GrFragmentProcessor>(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<GrSweepGradient>();
     }
 
@@ -250,7 +247,8 @@ sk_sp<GrFragmentProcessor> SkSweepGradient::asFragmentProcessor(const AsFPArgs&
     }
     matrix.postConcat(fPtsToUnit);
 
-    sk_sp<GrFragmentProcessor> inner(GrSweepGradient::Make(args.fContext, *this, matrix));
+    sk_sp<GrFragmentProcessor> inner(GrSweepGradient::Make(
+        GrGradientEffect::CreateArgs(args.fContext, this, &matrix, SkShader::kClamp_TileMode)));
     return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner));
 }
 
index 9c09701..84f1ae8 100644 (file)
@@ -360,8 +360,8 @@ sk_sp<GrFragmentProcessor> SkTwoPointConicalGradient::asFragmentProcessor(
         const AsFPArgs& args) const {
     SkASSERT(args.fContext);
     SkASSERT(fPtsToUnit.isIdentity());
-    sk_sp<GrFragmentProcessor> inner(
-        Gr2PtConicalGradientEffect::Make(args.fContext, *this, fTileMode, args.fLocalMatrix));
+    sk_sp<GrFragmentProcessor> inner(Gr2PtConicalGradientEffect::Make(
+        GrGradientEffect::CreateArgs(args.fContext, this, args.fLocalMatrix, fTileMode)));
     return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner));
 }
 
index e89193f..a581321 100644 (file)
@@ -62,11 +62,8 @@ class Edge2PtConicalEffect : public GrGradientEffect {
 public:
     class GLSLEdge2PtConicalProcessor;
 
-    static sk_sp<GrFragmentProcessor> Make(GrContext* ctx,
-                                           const SkTwoPointConicalGradient& shader,
-                                           const SkMatrix& matrix,
-                                           SkShader::TileMode tm) {
-        return sk_sp<GrFragmentProcessor>(new Edge2PtConicalEffect(ctx, shader, matrix, tm));
+    static sk_sp<GrFragmentProcessor> Make(const CreateArgs& args) {
+        return sk_sp<GrFragmentProcessor>(new Edge2PtConicalEffect(args));
     }
 
     virtual ~Edge2PtConicalEffect() {}
@@ -93,14 +90,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<const SkTwoPointConicalGradient*>(args.fShader);
+        fCenterX1 = shader.getCenterX1();
+        fRadius0 = shader.getStartRadius();
+        fDiffRadius = shader.getDiffRadius();
         this->initClassID<Edge2PtConicalEffect>();
         // 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 +372,9 @@ class FocalOutside2PtConicalEffect : public GrGradientEffect {
 public:
     class GLSLFocalOutside2PtConicalProcessor;
 
-    static sk_sp<GrFragmentProcessor> Make(GrContext* ctx,
-                                           const SkTwoPointConicalGradient& shader,
-                                           const SkMatrix& matrix,
-                                           SkShader::TileMode tm,
-                                           SkScalar focalX) {
+    static sk_sp<GrFragmentProcessor> Make(const CreateArgs& args, SkScalar focalX) {
         return sk_sp<GrFragmentProcessor>(
-            new FocalOutside2PtConicalEffect(ctx, shader, matrix, tm, focalX));
+            new FocalOutside2PtConicalEffect(args, focalX));
     }
 
     virtual ~FocalOutside2PtConicalEffect() { }
@@ -406,14 +398,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<const SkTwoPointConicalGradient*>(args.fShader)->isFlippedGrad()) {
         this->initClassID<FocalOutside2PtConicalEffect>();
     }
 
@@ -591,13 +579,9 @@ class FocalInside2PtConicalEffect : public GrGradientEffect {
 public:
     class GLSLFocalInside2PtConicalProcessor;
 
-    static sk_sp<GrFragmentProcessor> Make(GrContext* ctx,
-                                           const SkTwoPointConicalGradient& shader,
-                                           const SkMatrix& matrix,
-                                           SkShader::TileMode tm,
-                                           SkScalar focalX) {
+    static sk_sp<GrFragmentProcessor> Make(const CreateArgs& args, SkScalar focalX) {
         return sk_sp<GrFragmentProcessor>(
-            new FocalInside2PtConicalEffect(ctx, shader, matrix, tm, focalX));
+            new FocalInside2PtConicalEffect(args, focalX));
     }
 
     virtual ~FocalInside2PtConicalEffect() {}
@@ -621,12 +605,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<FocalInside2PtConicalEffect>();
     }
 
@@ -837,13 +817,9 @@ class CircleInside2PtConicalEffect : public GrGradientEffect {
 public:
     class GLSLCircleInside2PtConicalProcessor;
 
-    static sk_sp<GrFragmentProcessor> Make(GrContext* ctx,
-                                           const SkTwoPointConicalGradient& shader,
-                                           const SkMatrix& matrix,
-                                           SkShader::TileMode tm,
-                                           const CircleConicalInfo& info) {
+    static sk_sp<GrFragmentProcessor> Make(const CreateArgs& args, const CircleConicalInfo& info) {
         return sk_sp<GrFragmentProcessor>(
-            new CircleInside2PtConicalEffect(ctx, shader, matrix, tm, info));
+            new CircleInside2PtConicalEffect(args, info));
     }
 
     virtual ~CircleInside2PtConicalEffect() {}
@@ -871,12 +847,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<CircleInside2PtConicalEffect>();
     }
 
@@ -1060,13 +1032,9 @@ class CircleOutside2PtConicalEffect : public GrGradientEffect {
 public:
     class GLSLCircleOutside2PtConicalProcessor;
 
-    static sk_sp<GrFragmentProcessor> Make(GrContext* ctx,
-                                           const SkTwoPointConicalGradient& shader,
-                                           const SkMatrix& matrix,
-                                           SkShader::TileMode tm,
-                                           const CircleConicalInfo& info) {
+    static sk_sp<GrFragmentProcessor> Make(const CreateArgs& args, const CircleConicalInfo& info) {
         return sk_sp<GrFragmentProcessor>(
-            new CircleOutside2PtConicalEffect(ctx, shader, matrix, tm, info));
+            new CircleOutside2PtConicalEffect(args, info));
     }
 
     virtual ~CircleOutside2PtConicalEffect() {}
@@ -1097,13 +1065,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<CircleOutside2PtConicalEffect>();
+        const SkTwoPointConicalGradient& shader =
+            *static_cast<const SkTwoPointConicalGradient*>(args.fShader);
         if (shader.getStartRadius() != shader.getEndRadius()) {
             fTLimit = shader.getStartRadius() / (shader.getStartRadius() - shader.getEndRadius());
         } else {
@@ -1322,32 +1288,35 @@ void CircleOutside2PtConicalEffect::GLSLCircleOutside2PtConicalProcessor::GenKey
 
 //////////////////////////////////////////////////////////////////////////////
 
-sk_sp<GrFragmentProcessor> Gr2PtConicalGradientEffect::Make(GrContext* ctx,
-                                                            const SkTwoPointConicalGradient& shader,
-                                                            SkShader::TileMode tm,
-                                                            const SkMatrix* localMatrix) {
+sk_sp<GrFragmentProcessor> Gr2PtConicalGradientEffect::Make(
+                                                         const GrGradientEffect::CreateArgs& args) {
+    const SkTwoPointConicalGradient& shader =
+        *static_cast<const SkTwoPointConicalGradient*>(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 +1324,12 @@ sk_sp<GrFragmentProcessor> 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);
     }
 }
 
index 9dd058d..46edb1f 100644 (file)
@@ -18,8 +18,7 @@ namespace Gr2PtConicalGradientEffect {
      * Creates an effect that produces a two point conical gradient based on the
      * shader passed in.
      */
-    sk_sp<GrFragmentProcessor> Make(GrContext* ctx,  const SkTwoPointConicalGradient& shader,
-                                    SkShader::TileMode tm, const SkMatrix* localMatrix);
+    sk_sp<GrFragmentProcessor> Make(const GrGradientEffect::CreateArgs& args);
 };
 
 #endif