Add base effect for gpu 2pt conical gradients

This is in preparation to add more "sub-effects" for the various fast path cases for 2 pt conical gradients

BUG=skia:
R=jvanverth@google.com, robertphillips@google.com, bsalomon@google.com

Author: egdaniel@google.com

Review URL: https://codereview.chromium.org/224083002

git-svn-id: http://skia.googlecode.com/svn/trunk@14059 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/effects/gradients/SkTwoPointConicalGradient.cpp b/src/effects/gradients/SkTwoPointConicalGradient.cpp
index 74a5817..de8c236 100644
--- a/src/effects/gradients/SkTwoPointConicalGradient.cpp
+++ b/src/effects/gradients/SkTwoPointConicalGradient.cpp
@@ -335,24 +335,8 @@
 GrEffectRef* SkTwoPointConicalGradient::asNewEffect(GrContext* context, const SkPaint&) const {
     SkASSERT(NULL != context);
     SkASSERT(fPtsToUnit.isIdentity());
-    // invert the localM, translate to center1, rotate so center2 is on x axis.
-    SkMatrix matrix;
-    if (!this->getLocalMatrix().invert(&matrix)) {
-        return NULL;
-    }
-    matrix.postTranslate(-fCenter1.fX, -fCenter1.fY);
 
-    SkPoint diff = fCenter2 - fCenter1;
-    SkScalar diffLen = diff.length();
-    if (0 != diffLen) {
-        SkScalar invDiffLen = SkScalarInvert(diffLen);
-        SkMatrix rot;
-        rot.setSinCos(-SkScalarMul(invDiffLen, diff.fY),
-                       SkScalarMul(invDiffLen, diff.fX));
-        matrix.postConcat(rot);
-    }
-
-    return Gr2PtConicalGradientEffect::Create(context, *this, matrix, fTileMode);
+    return Gr2PtConicalGradientEffect::Create(context, *this, fTileMode);
 }
 
 #else
diff --git a/src/effects/gradients/SkTwoPointConicalGradient.h b/src/effects/gradients/SkTwoPointConicalGradient.h
index 71ba57f..041f8b8 100644
--- a/src/effects/gradients/SkTwoPointConicalGradient.h
+++ b/src/effects/gradients/SkTwoPointConicalGradient.h
@@ -65,6 +65,9 @@
     SkScalar getCenterX1() const { return SkPoint::Distance(fCenter1, fCenter2); }
     SkScalar getStartRadius() const { return fRadius1; }
     SkScalar getDiffRadius() const { return fRadius2 - fRadius1; }
+    const SkPoint& getStartCenter() const { return fCenter1; }
+    const SkPoint& getEndCenter() const { return fCenter2; }
+    SkScalar getEndRadius() const { return fRadius2; }
 
     SK_TO_STRING_OVERRIDE()
     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkTwoPointConicalGradient)
diff --git a/src/effects/gradients/SkTwoPointConicalGradient_gpu.cpp b/src/effects/gradients/SkTwoPointConicalGradient_gpu.cpp
index bdf4dd2..f662331 100644
--- a/src/effects/gradients/SkTwoPointConicalGradient_gpu.cpp
+++ b/src/effects/gradients/SkTwoPointConicalGradient_gpu.cpp
@@ -4,6 +4,7 @@
  * Use of this source code is governed by a BSD-style license that can be
  * found in the LICENSE file.
  */
+
 #if SK_SUPPORT_GPU
 #include "SkTwoPointConicalGradient_gpu.h"
 #include "GrTBackendEffectFactory.h"
@@ -13,11 +14,105 @@
 // For brevity
 typedef GrGLUniformManager::UniformHandle UniformHandle;
 
-class GrGL2PtConicalGradientEffect : public GrGLGradientEffect {
+//////////////////////////////////////////////////////////////////////////////
+
+static void set_matrix_default_conical(const SkTwoPointConicalGradient& shader,
+                                       SkMatrix* invLMatrix) {
+    // Inverse of the current local matrix is passed in then,
+    // translate to center1, rotate so center2 is on x axis.
+    const SkPoint& center1 = shader.getStartCenter();
+    const SkPoint& center2 = shader.getEndCenter();
+
+    invLMatrix->postTranslate(-center1.fX, -center1.fY);
+
+    SkPoint diff = center2 - center1;
+    SkScalar diffLen = diff.length();
+    if (0 != diffLen) {
+        SkScalar invDiffLen = SkScalarInvert(diffLen);
+        SkMatrix rot;
+        rot.setSinCos(-SkScalarMul(invDiffLen, diff.fY),
+                       SkScalarMul(invDiffLen, diff.fX));
+        invLMatrix->postConcat(rot);
+    }
+}
+
+class GLDefault2PtConicalEffect;
+
+class Default2PtConicalEffect : public GrGradientEffect {
 public:
 
-    GrGL2PtConicalGradientEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&);
-    virtual ~GrGL2PtConicalGradientEffect() { }
+    static GrEffectRef* Create(GrContext* ctx,
+                               const SkTwoPointConicalGradient& shader,
+                               const SkMatrix& matrix,
+                               SkShader::TileMode tm) {
+        AutoEffectUnref effect(SkNEW_ARGS(Default2PtConicalEffect, (ctx, shader, matrix, tm)));
+        return CreateEffectRef(effect);
+    }
+
+    virtual ~Default2PtConicalEffect() { }
+
+    static const char* Name() { return "Two-Point Conical Gradient"; }
+    virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
+
+    // The radial gradient parameters can collapse to a linear (instead of quadratic) equation.
+    bool isDegenerate() const { return SkScalarAbs(fDiffRadius) == SkScalarAbs(fCenterX1); }
+    SkScalar center() const { return fCenterX1; }
+    SkScalar diffRadius() const { return fDiffRadius; }
+    SkScalar radius() const { return fRadius0; }
+
+    typedef GLDefault2PtConicalEffect GLEffect;
+
+private:
+    virtual bool onIsEqual(const GrEffect& sBase) const SK_OVERRIDE {
+        const Default2PtConicalEffect& s = CastEffect<Default2PtConicalEffect>(sBase);
+        return (INHERITED::onIsEqual(sBase) &&
+                this->fCenterX1 == s.fCenterX1 &&
+                this->fRadius0 == s.fRadius0 &&
+                this->fDiffRadius == s.fDiffRadius);
+    }
+
+    Default2PtConicalEffect(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()) {
+        // We pass the linear part of the quadratic as a varying.
+        //    float b = -2.0 * (fCenterX1 * x + fRadius0 * fDiffRadius * z)
+        fBTransform = this->getCoordTransform();
+        SkMatrix& bMatrix = *fBTransform.accessMatrix();
+        SkScalar r0dr = SkScalarMul(fRadius0, fDiffRadius);
+        bMatrix[SkMatrix::kMScaleX] = -2 * (SkScalarMul(fCenterX1, bMatrix[SkMatrix::kMScaleX]) +
+                                            SkScalarMul(r0dr, bMatrix[SkMatrix::kMPersp0]));
+        bMatrix[SkMatrix::kMSkewX] = -2 * (SkScalarMul(fCenterX1, bMatrix[SkMatrix::kMSkewX]) +
+                                           SkScalarMul(r0dr, bMatrix[SkMatrix::kMPersp1]));
+        bMatrix[SkMatrix::kMTransX] = -2 * (SkScalarMul(fCenterX1, bMatrix[SkMatrix::kMTransX]) +
+                                            SkScalarMul(r0dr, bMatrix[SkMatrix::kMPersp2]));
+        this->addCoordTransform(&fBTransform);
+    }
+
+    GR_DECLARE_EFFECT_TEST;
+
+    // @{
+    // Cache of values - these can change arbitrarily, EXCEPT
+    // we shouldn't change between degenerate and non-degenerate?!
+
+    GrCoordTransform fBTransform;
+    SkScalar         fCenterX1;
+    SkScalar         fRadius0;
+    SkScalar         fDiffRadius;
+
+    // @}
+
+    typedef GrGradientEffect INHERITED;
+};
+
+class GLDefault2PtConicalEffect : public GrGLGradientEffect {
+public:
+    GLDefault2PtConicalEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&);
+    virtual ~GLDefault2PtConicalEffect() { }
 
     virtual void emitCode(GrGLShaderBuilder*,
                           const GrDrawEffect&,
@@ -31,7 +126,6 @@
     static EffectKey GenKey(const GrDrawEffect&, const GrGLCaps& caps);
 
 protected:
-
     UniformHandle fParamUni;
 
     const char* fVSVaryingName;
@@ -49,40 +143,17 @@
     // @}
 
 private:
-
     typedef GrGLGradientEffect INHERITED;
 
 };
 
-const GrBackendEffectFactory& Gr2PtConicalGradientEffect::getFactory() const {
-    return GrTBackendEffectFactory<Gr2PtConicalGradientEffect>::getInstance();
+const GrBackendEffectFactory& Default2PtConicalEffect::getFactory() const {
+    return GrTBackendEffectFactory<Default2PtConicalEffect>::getInstance();
 }
 
-Gr2PtConicalGradientEffect::Gr2PtConicalGradientEffect(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()) {
-    // We pass the linear part of the quadratic as a varying.
-    //    float b = -2.0 * (fCenterX1 * x + fRadius0 * fDiffRadius * z)
-    fBTransform = this->getCoordTransform();
-    SkMatrix& bMatrix = *fBTransform.accessMatrix();
-    SkScalar r0dr = SkScalarMul(fRadius0, fDiffRadius);
-    bMatrix[SkMatrix::kMScaleX] = -2 * (SkScalarMul(fCenterX1, bMatrix[SkMatrix::kMScaleX]) +
-                                        SkScalarMul(r0dr, bMatrix[SkMatrix::kMPersp0]));
-    bMatrix[SkMatrix::kMSkewX] = -2 * (SkScalarMul(fCenterX1, bMatrix[SkMatrix::kMSkewX]) +
-                                       SkScalarMul(r0dr, bMatrix[SkMatrix::kMPersp1]));
-    bMatrix[SkMatrix::kMTransX] = -2 * (SkScalarMul(fCenterX1, bMatrix[SkMatrix::kMTransX]) +
-                                        SkScalarMul(r0dr, bMatrix[SkMatrix::kMPersp2]));
-    this->addCoordTransform(&fBTransform);
-}
+GR_DEFINE_EFFECT_TEST(Default2PtConicalEffect);
 
-GR_DEFINE_EFFECT_TEST(Gr2PtConicalGradientEffect);
-
-GrEffectRef* Gr2PtConicalGradientEffect::TestCreate(SkRandom* random,
+GrEffectRef* Default2PtConicalEffect::TestCreate(SkRandom* random,
                                             GrContext* context,
                                             const GrDrawTargetCaps&,
                                             GrTexture**) {
@@ -112,7 +183,7 @@
 
 /////////////////////////////////////////////////////////////////////
 
-GrGL2PtConicalGradientEffect::GrGL2PtConicalGradientEffect(const GrBackendEffectFactory& factory,
+GLDefault2PtConicalEffect::GLDefault2PtConicalEffect(const GrBackendEffectFactory& factory,
                                            const GrDrawEffect& drawEffect)
     : INHERITED(factory)
     , fVSVaryingName(NULL)
@@ -121,11 +192,11 @@
     , fCachedRadius(-SK_ScalarMax)
     , fCachedDiffRadius(-SK_ScalarMax) {
 
-    const Gr2PtConicalGradientEffect& data = drawEffect.castEffect<Gr2PtConicalGradientEffect>();
+    const Default2PtConicalEffect& data = drawEffect.castEffect<Default2PtConicalEffect>();
     fIsDegenerate = data.isDegenerate();
 }
 
-void GrGL2PtConicalGradientEffect::emitCode(GrGLShaderBuilder* builder,
+void GLDefault2PtConicalEffect::emitCode(GrGLShaderBuilder* builder,
                                     const GrDrawEffect&,
                                     EffectKey key,
                                     const char* outputColor,
@@ -254,10 +325,10 @@
     }
 }
 
-void GrGL2PtConicalGradientEffect::setData(const GrGLUniformManager& uman,
+void GLDefault2PtConicalEffect::setData(const GrGLUniformManager& uman,
                                    const GrDrawEffect& drawEffect) {
     INHERITED::setData(uman, drawEffect);
-    const Gr2PtConicalGradientEffect& data = drawEffect.castEffect<Gr2PtConicalGradientEffect>();
+    const Default2PtConicalEffect& data = drawEffect.castEffect<Default2PtConicalEffect>();
     SkASSERT(data.isDegenerate() == fIsDegenerate);
     SkScalar centerX1 = data.center();
     SkScalar radius0 = data.radius();
@@ -290,16 +361,32 @@
     }
 }
 
-GrGLEffect::EffectKey GrGL2PtConicalGradientEffect::GenKey(const GrDrawEffect& drawEffect,
+GrGLEffect::EffectKey GLDefault2PtConicalEffect::GenKey(const GrDrawEffect& drawEffect,
                                                    const GrGLCaps&) {
     enum {
         kIsDegenerate = 1 << kBaseKeyBitCnt,
     };
 
     EffectKey key = GenBaseGradientKey(drawEffect);
-    if (drawEffect.castEffect<Gr2PtConicalGradientEffect>().isDegenerate()) {
+    if (drawEffect.castEffect<Default2PtConicalEffect>().isDegenerate()) {
         key |= kIsDegenerate;
     }
     return key;
 }
+
+//////////////////////////////////////////////////////////////////////////////
+
+GrEffectRef* Gr2PtConicalGradientEffect::Create(GrContext* ctx,
+                                                const SkTwoPointConicalGradient& shader,
+                                                SkShader::TileMode tm) {
+
+    SkMatrix matrix;
+    if (!shader.getLocalMatrix().invert(&matrix)) {
+        return NULL;
+    }
+
+    set_matrix_default_conical(shader, &matrix);
+    return Default2PtConicalEffect::Create(ctx, shader, matrix, tm);
+}
+
 #endif
diff --git a/src/effects/gradients/SkTwoPointConicalGradient_gpu.h b/src/effects/gradients/SkTwoPointConicalGradient_gpu.h
index 8b871e5..7f8db67 100644
--- a/src/effects/gradients/SkTwoPointConicalGradient_gpu.h
+++ b/src/effects/gradients/SkTwoPointConicalGradient_gpu.h
@@ -8,66 +8,19 @@
 #ifndef SkTwoPointConicalGradient_gpu_DEFINED
 #define SkTwoPointConicalGradient_gpu_DEFINED
 
-#if SK_SUPPORT_GPU
-
 #include "SkGradientShaderPriv.h"
 
 class GrEffectRef;
 class SkTwoPointConicalGradient;
-class GrGL2PtConicalGradientEffect;
 
-class Gr2PtConicalGradientEffect : public GrGradientEffect {
-public:
-
-    static GrEffectRef* Create(GrContext* ctx,
-                               const SkTwoPointConicalGradient& shader,
-                               const SkMatrix& matrix,
-                               SkShader::TileMode tm) {
-        AutoEffectUnref effect(SkNEW_ARGS(Gr2PtConicalGradientEffect, (ctx, shader, matrix, tm)));
-        return CreateEffectRef(effect);
-    }
-
-    virtual ~Gr2PtConicalGradientEffect() { }
-
-    static const char* Name() { return "Two-Point Conical Gradient"; }
-    virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
-
-    // The radial gradient parameters can collapse to a linear (instead of quadratic) equation.
-    bool isDegenerate() const { return SkScalarAbs(fDiffRadius) == SkScalarAbs(fCenterX1); }
-    SkScalar center() const { return fCenterX1; }
-    SkScalar diffRadius() const { return fDiffRadius; }
-    SkScalar radius() const { return fRadius0; }
-
-    typedef GrGL2PtConicalGradientEffect GLEffect;
-
-private:
-    virtual bool onIsEqual(const GrEffect& sBase) const SK_OVERRIDE {
-        const Gr2PtConicalGradientEffect& s = CastEffect<Gr2PtConicalGradientEffect>(sBase);
-        return (INHERITED::onIsEqual(sBase) &&
-                this->fCenterX1 == s.fCenterX1 &&
-                this->fRadius0 == s.fRadius0 &&
-                this->fDiffRadius == s.fDiffRadius);
-    }
-
-    Gr2PtConicalGradientEffect(GrContext* ctx,
-                       const SkTwoPointConicalGradient& shader,
-                       const SkMatrix& matrix,
-                       SkShader::TileMode tm);
-
-    GR_DECLARE_EFFECT_TEST;
-
-    // @{
-    // Cache of values - these can change arbitrarily, EXCEPT
-    // we shouldn't change between degenerate and non-degenerate?!
-
-    GrCoordTransform fBTransform;
-    SkScalar         fCenterX1;
-    SkScalar         fRadius0;
-    SkScalar         fDiffRadius;
-
-    // @}
-
-    typedef GrGradientEffect INHERITED;
+namespace Gr2PtConicalGradientEffect {
+    /**
+     * Creates an effect that produces a two point conical gradient based on the
+     * shader passed in.
+     */
+    GrEffectRef* Create(GrContext* ctx, const SkTwoPointConicalGradient& shader,
+                        SkShader::TileMode tm);
 };
+
 #endif
-#endif
+