Make GrEffect::textureAccess non-virtual. Require subclasses to append their GrTAs.
Review URL: https://codereview.appspot.com/7062063

git-svn-id: http://skia.googlecode.com/svn/trunk@7129 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/gpu/GrEffect.h b/include/gpu/GrEffect.h
index 5a15119..b12a346 100644
--- a/include/gpu/GrEffect.h
+++ b/include/gpu/GrEffect.h
@@ -18,26 +18,21 @@
 class GrContext;
 class SkString;
 
-/** Provides custom vertex shader, fragment shader, uniform data for a
-    particular stage of the Ganesh shading pipeline.
+/** Provides custom vertex shader, fragment shader, uniform data for a particular stage of the
+    Ganesh shading pipeline.
     Subclasses must have a function that produces a human-readable name:
         static const char* Name();
-    GrEffect objects *must* be immutable: after being constructed,
-    their fields may not change.  (Immutability isn't actually required
-    until they've been used in a draw call, but supporting that would require
-    setters and getters that could fail, copy-on-write, or deep copying of these
-    objects when they're stored by a GrGLEffect.)
+    GrEffect objects *must* be immutable: after being constructed, their fields may not change.
   */
 class GrEffect : public GrRefCnt {
-
 public:
     SK_DECLARE_INST_COUNT(GrEffect)
 
-    explicit GrEffect(int numTextures);
+    GrEffect() {};
     virtual ~GrEffect();
 
-    /** If given an input texture that is/is not opaque, is this
-        effect guaranteed to produce an opaque output? */
+    /** If given an input texture that is/is not opaque, is this effect guaranteed to produce an
+        opaque output? */
     virtual bool isOpaque(bool inputTextureIsOpaque) const;
 
     /** This object, besides creating back-end-specific helper objects, is used for run-time-type-
@@ -77,11 +72,11 @@
         in generated shader code. */
     const char* name() const;
 
-    int numTextures() const { return fNumTextures; }
+    int numTextures() const { return fTextureAccesses.count(); }
 
     /** Returns the access pattern for the texture at index. index must be valid according to
         numTextures(). */
-    virtual const GrTextureAccess& textureAccess(int index) const;
+    const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; }
 
     /** Shortcut for textureAccess(index).texture(); */
     GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); }
@@ -98,8 +93,16 @@
     void* operator new(size_t size);
     void operator delete(void* target);
 
+protected:
+    /**
+     * Subclasses call this from their constructor to register GrTextureAcceses. The effect subclass
+     * manages the lifetime of the accesses (this function only stores a pointer). This must only be
+     * called from the constructor because GrEffects are supposed to be immutable.
+     */
+    void addTextureAccess(const GrTextureAccess* textureAccess);
+
 private:
-    int fNumTextures;
+    SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses;
     typedef GrRefCnt INHERITED;
 };
 
diff --git a/src/effects/SkBlendImageFilter.cpp b/src/effects/SkBlendImageFilter.cpp
index 4e5e018..6dd5eab 100644
--- a/src/effects/SkBlendImageFilter.cpp
+++ b/src/effects/SkBlendImageFilter.cpp
@@ -153,8 +153,6 @@
     typedef GrGLBlendEffect GLEffect;
     static const char* Name() { return "Blend"; }
 
-    virtual const GrTextureAccess& textureAccess(int index) const SK_OVERRIDE;
-
 private:
     GrTextureAccess             fForegroundAccess;
     GrTextureAccess             fBackgroundAccess;
@@ -228,10 +226,11 @@
 GrBlendEffect::GrBlendEffect(SkBlendImageFilter::Mode mode,
                              GrTexture* foreground,
                              GrTexture* background)
-    : INHERITED(2)
-    , fForegroundAccess(foreground)
+    : fForegroundAccess(foreground)
     , fBackgroundAccess(background)
     , fMode(mode) {
+    this->addTextureAccess(&fForegroundAccess);
+    this->addTextureAccess(&fBackgroundAccess);
 }
 
 GrBlendEffect::~GrBlendEffect() {
@@ -246,11 +245,6 @@
     return GrTBackendEffectFactory<GrBlendEffect>::getInstance();
 }
 
-const GrTextureAccess& GrBlendEffect::textureAccess(int index) const {
-    SkASSERT(index >= 0 && index < 2);
-    return (0 == index) ? fForegroundAccess : fBackgroundAccess;
-}
-
 ///////////////////////////////////////////////////////////////////////////////
 
 GrGLBlendEffect::GrGLBlendEffect(const GrBackendEffectFactory& factory, const GrEffect& effect)
diff --git a/src/effects/SkColorMatrixFilter.cpp b/src/effects/SkColorMatrixFilter.cpp
index e460325..ed34f64 100644
--- a/src/effects/SkColorMatrixFilter.cpp
+++ b/src/effects/SkColorMatrixFilter.cpp
@@ -327,7 +327,7 @@
 public:
     static const char* Name() { return "Color Matrix"; }
 
-    ColorMatrixEffect(const SkColorMatrix& matrix) : GrEffect(0), fMatrix(matrix) {}
+    ColorMatrixEffect(const SkColorMatrix& matrix) : fMatrix(matrix) {}
 
     virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
         return GrTBackendEffectFactory<ColorMatrixEffect>::getInstance();
diff --git a/src/effects/SkTableColorFilter.cpp b/src/effects/SkTableColorFilter.cpp
index ff30636..eb59425 100644
--- a/src/effects/SkTableColorFilter.cpp
+++ b/src/effects/SkTableColorFilter.cpp
@@ -233,8 +233,6 @@
     virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
     virtual bool isEqual(const GrEffect&) const SK_OVERRIDE;
 
-    virtual const GrTextureAccess& textureAccess(int index) const SK_OVERRIDE;
-
     typedef GLColorTableEffect GLEffect;
 
 private:
@@ -324,8 +322,8 @@
 ///////////////////////////////////////////////////////////////////////////////
 
 ColorTableEffect::ColorTableEffect(GrTexture* texture)
-    : INHERITED(1)
-    , fTextureAccess(texture, "a") {
+    : fTextureAccess(texture, "a") {
+    this->addTextureAccess(&fTextureAccess);
 }
 
 ColorTableEffect::~ColorTableEffect() {
@@ -339,11 +337,6 @@
     return INHERITED::isEqual(sBase);
 }
 
-const GrTextureAccess& ColorTableEffect::textureAccess(int index) const {
-    GrAssert(0 == index);
-    return fTextureAccess;
-}
-
 ///////////////////////////////////////////////////////////////////////////////
 
 GR_DEFINE_EFFECT_TEST(ColorTableEffect);
diff --git a/src/effects/gradients/SkGradientShader.cpp b/src/effects/gradients/SkGradientShader.cpp
index 5b2a60e..8521bdb 100644
--- a/src/effects/gradients/SkGradientShader.cpp
+++ b/src/effects/gradients/SkGradientShader.cpp
@@ -742,8 +742,7 @@
 GrGradientEffect::GrGradientEffect(GrContext* ctx,
                                    const SkGradientShaderBase& shader,
                                    const SkMatrix& matrix,
-                                   SkShader::TileMode tileMode)
-    : INHERITED(1) {
+                                   SkShader::TileMode tileMode) {
     // TODO: check for simple cases where we don't need a texture:
     //GradientInfo info;
     //shader.asAGradient(&info);
@@ -783,6 +782,7 @@
         // the cache, but it'll still be ref'd until it's no longer being used.
         GrUnlockCachedBitmapTexture(texture);
     }
+    this->addTextureAccess(&fTextureAccess);
 }
 
 GrGradientEffect::~GrGradientEffect() {
@@ -791,11 +791,6 @@
     }
 }
 
-const GrTextureAccess& GrGradientEffect::textureAccess(int index) const {
-    GrAssert(0 == index);
-    return fTextureAccess;
-}
-
 int GrGradientEffect::RandomGradientParams(SkRandom* random,
                                            SkColor colors[],
                                            SkScalar** stops,
diff --git a/src/effects/gradients/SkGradientShaderPriv.h b/src/effects/gradients/SkGradientShaderPriv.h
index 1662188..552013e 100644
--- a/src/effects/gradients/SkGradientShaderPriv.h
+++ b/src/effects/gradients/SkGradientShaderPriv.h
@@ -233,8 +233,6 @@
 
     virtual ~GrGradientEffect();
 
-    virtual const GrTextureAccess& textureAccess(int index) const SK_OVERRIDE;
-
     bool useAtlas() const { return SkToBool(-1 != fRow); }
     SkScalar getYCoord() const { return fYCoord; };
     const SkMatrix& getMatrix() const { return fMatrix;}
diff --git a/src/gpu/GrEffect.cpp b/src/gpu/GrEffect.cpp
index d470c9c..dbfb6b0 100644
--- a/src/gpu/GrEffect.cpp
+++ b/src/gpu/GrEffect.cpp
@@ -58,12 +58,7 @@
 
 int32_t GrBackendEffectFactory::fCurrEffectClassID = GrBackendEffectFactory::kIllegalEffectClassID;
 
-GrEffect::GrEffect(int numTextures)
-    : fNumTextures(numTextures) {
-}
-
 GrEffect::~GrEffect() {
-
 }
 
 bool GrEffect::isOpaque(bool inputTextureIsOpaque) const {
@@ -74,7 +69,6 @@
     return this->getFactory().name();
 }
 
-
 bool GrEffect::isEqual(const GrEffect& s) const {
     if (this->numTextures() != s.numTextures()) {
         return false;
@@ -87,10 +81,8 @@
     return true;
 }
 
-const GrTextureAccess& GrEffect::textureAccess(int index) const {
-    GrCrash("We shouldn't be calling this function on the base class.");
-    static GrTextureAccess kDummy;
-    return kDummy;
+void GrEffect::addTextureAccess(const GrTextureAccess* access) {
+    fTextureAccesses.push_back(access);
 }
 
 void * GrEffect::operator new(size_t size) {
diff --git a/src/gpu/effects/GrSingleTextureEffect.cpp b/src/gpu/effects/GrSingleTextureEffect.cpp
index 05eff6b..14f5b64 100644
--- a/src/gpu/effects/GrSingleTextureEffect.cpp
+++ b/src/gpu/effects/GrSingleTextureEffect.cpp
@@ -58,51 +58,46 @@
 ///////////////////////////////////////////////////////////////////////////////
 
 GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture)
-    : INHERITED(1)
-    , fTextureAccess(texture) {
+    : fTextureAccess(texture) {
     fMatrix.reset();
+    this->addTextureAccess(&fTextureAccess);
 }
 
 GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture, bool bilerp)
-    : INHERITED(1)
-    , fTextureAccess(texture, bilerp) {
+    : fTextureAccess(texture, bilerp) {
     fMatrix.reset();
+    this->addTextureAccess(&fTextureAccess);
 }
 
 GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture, const GrTextureParams& params)
-    : INHERITED(1)
-    , fTextureAccess(texture, params) {
+    : fTextureAccess(texture, params) {
     fMatrix.reset();
+    this->addTextureAccess(&fTextureAccess);
 }
 
 GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture, const SkMatrix& m)
-    : INHERITED(1)
-    , fTextureAccess(texture)
+    : fTextureAccess(texture)
     , fMatrix(m) {
+    this->addTextureAccess(&fTextureAccess);
 }
 
 GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture, const SkMatrix& m, bool bilerp)
-    : INHERITED(1)
-    , fTextureAccess(texture, bilerp)
+    : fTextureAccess(texture, bilerp)
     , fMatrix(m) {
+    this->addTextureAccess(&fTextureAccess);
 }
 
 GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture,
                                              const SkMatrix& m,
                                              const GrTextureParams& params)
-    : INHERITED(1)
-    , fTextureAccess(texture, params)
+    : fTextureAccess(texture, params)
     , fMatrix(m) {
+    this->addTextureAccess(&fTextureAccess);
 }
 
 GrSingleTextureEffect::~GrSingleTextureEffect() {
 }
 
-const GrTextureAccess& GrSingleTextureEffect::textureAccess(int index) const {
-    GrAssert(0 == index);
-    return fTextureAccess;
-}
-
 const GrBackendEffectFactory& GrSingleTextureEffect::getFactory() const {
     return GrTBackendEffectFactory<GrSingleTextureEffect>::getInstance();
 }
diff --git a/src/gpu/effects/GrSingleTextureEffect.h b/src/gpu/effects/GrSingleTextureEffect.h
index a2f8bff..fca5e93 100644
--- a/src/gpu/effects/GrSingleTextureEffect.h
+++ b/src/gpu/effects/GrSingleTextureEffect.h
@@ -33,8 +33,6 @@
 
     virtual ~GrSingleTextureEffect();
 
-    virtual const GrTextureAccess& textureAccess(int index) const SK_OVERRIDE;
-
     static const char* Name() { return "Single Texture"; }
 
     const SkMatrix& getMatrix() const { return fMatrix; }