Move GrTextureParams from GrSamplerState to GrTextureAccess

Review URL: https://codereview.appspot.com/6496135/



git-svn-id: http://skia.googlecode.com/svn/trunk@5582 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/gpu/GrSamplerState.h b/include/gpu/GrSamplerState.h
index e8182f9..385dba7 100644
--- a/include/gpu/GrSamplerState.h
+++ b/include/gpu/GrSamplerState.h
@@ -17,69 +17,6 @@
 
 #include "SkShader.h"
 
-class GrTextureParams {
-public:
-    GrTextureParams() {
-        this->reset();
-    }
-
-    GrTextureParams(const GrTextureParams& params) {
-        *this = params;
-    }
-
-    GrTextureParams& operator =(const GrTextureParams& params) {
-        fTileModes[0] = params.fTileModes[0];
-        fTileModes[1] = params.fTileModes[1];
-        fBilerp = params.fBilerp;
-        return *this;
-    }
-
-    void reset() {
-        this->reset(SkShader::kClamp_TileMode, false);
-    }
-
-    void reset(SkShader::TileMode tileXAndY, bool filter) {
-        fTileModes[0] = fTileModes[1] = tileXAndY;
-        fBilerp = filter;
-    }
-    void reset(SkShader::TileMode tileModes[2], bool filter) {
-        fTileModes[0] = tileModes[0];
-        fTileModes[1] = tileModes[1];
-        fBilerp = filter;
-    }
-
-    void setClampNoFilter() {
-        fTileModes[0] = fTileModes[1] = SkShader::kClamp_TileMode;
-        fBilerp = false;
-    }
-
-    void setClamp() {
-        fTileModes[0] = fTileModes[1] = SkShader::kClamp_TileMode;
-    }
-
-    void setBilerp(bool bilerp) { fBilerp = bilerp; }
-
-    void setTileModeX(const SkShader::TileMode tm) { fTileModes[0] = tm; }
-    void setTileModeY(const SkShader::TileMode tm) { fTileModes[1] = tm; }
-    void setTileModeXAndY(const SkShader::TileMode tm) { fTileModes[0] = fTileModes[1] = tm; }
-
-    SkShader::TileMode getTileModeX() const { return fTileModes[0]; }
-
-    SkShader::TileMode getTileModeY() const { return fTileModes[1]; }
-
-    bool isTiled() const {
-        return SkShader::kClamp_TileMode != fTileModes[0] ||
-               SkShader::kClamp_TileMode != fTileModes[1];
-    }
-
-    bool isBilerp() const { return fBilerp; }
-
-private:
-
-    SkShader::TileMode fTileModes[2];
-    bool               fBilerp;
-};
-
 class GrSamplerState {
 public:
     static const bool kBilerpDefault = false;
@@ -118,19 +55,13 @@
     bool operator !=(const GrSamplerState& s) const { return !(*this == s); }
 
     GrSamplerState& operator =(const GrSamplerState& s) {
-        // memcpy() breaks refcounting
-        fTextureParams = s.fTextureParams;
         fMatrix = s.fMatrix;
-
         GrSafeAssign(fCustomStage, s.fCustomStage);
-
         return *this;
     }
 
     const GrMatrix& getMatrix() const { return fMatrix; }
 
-    GrTextureParams* textureParams() { return &fTextureParams; }
-    const GrTextureParams& getTextureParams() const { return fTextureParams; }
     /**
      * Access the sampler's matrix. See SampleMode for explanation of
      * relationship between the matrix and sample mode.
@@ -149,21 +80,14 @@
      */
     void preConcatMatrix(const GrMatrix& matrix) { fMatrix.preConcat(matrix); }
 
-    void reset(SkShader::TileMode tileXAndY,
-               bool filter,
-               const GrMatrix& matrix) {
-        fTextureParams.reset(tileXAndY, filter);
+    void reset(const GrMatrix& matrix) {
         fMatrix = matrix;
         GrSafeSetNull(fCustomStage);
     }
-    void reset(SkShader::TileMode wrapXAndY, bool filter) {
-        this->reset(wrapXAndY, filter, GrMatrix::I());
-    }
-    void reset(const GrMatrix& matrix) {
-        this->reset(kTileModeDefault, kBilerpDefault, matrix);
-    }
+
     void reset() {
-        this->reset(kTileModeDefault, kBilerpDefault, GrMatrix::I());
+        fMatrix.reset();
+        GrSafeSetNull(fCustomStage);
     }
 
     GrCustomStage* setCustomStage(GrCustomStage* stage) {
@@ -173,7 +97,6 @@
     const GrCustomStage* getCustomStage() const { return fCustomStage; }
 
 private:
-    GrTextureParams     fTextureParams;
     GrMatrix            fMatrix;
 
     GrCustomStage*      fCustomStage;
diff --git a/include/gpu/GrTextureAccess.h b/include/gpu/GrTextureAccess.h
index e4e3732..7f47b6e 100644
--- a/include/gpu/GrTextureAccess.h
+++ b/include/gpu/GrTextureAccess.h
@@ -10,9 +10,96 @@
 
 #include "GrNoncopyable.h"
 #include "SkRefCnt.h"
+#include "SkShader.h"
 
 class GrTexture;
 
+/**
+ * Represents the filtering and tile modes used to access a texture. It is mostly used with
+ * GrTextureAccess (defined below). Also, some of the texture cache methods require knowledge about
+ * filtering and tiling to perform a cache lookup. If it wasn't for this latter usage this would
+ * be folded into GrTextureAccess.
+ */
+class GrTextureParams {
+public:
+    GrTextureParams() {
+        this->reset();
+    }
+
+    GrTextureParams(SkShader::TileMode tileXAndY, bool bilerp) {
+        this->reset(tileXAndY, bilerp);
+    }
+
+    GrTextureParams(SkShader::TileMode tileModes[2], bool bilerp) {
+        this->reset(tileModes, bilerp);
+    }
+
+    GrTextureParams(const GrTextureParams& params) {
+        *this = params;
+    }
+
+    GrTextureParams& operator= (const GrTextureParams& params) {
+        fTileModes[0] = params.fTileModes[0];
+        fTileModes[1] = params.fTileModes[1];
+        fBilerp = params.fBilerp;
+        return *this;
+    }
+
+    void reset() {
+        this->reset(SkShader::kClamp_TileMode, false);
+    }
+
+    void reset(SkShader::TileMode tileXAndY, bool bilerp) {
+        fTileModes[0] = fTileModes[1] = tileXAndY;
+        fBilerp = bilerp;
+    }
+
+    void reset(SkShader::TileMode tileModes[2], bool bilerp) {
+        fTileModes[0] = tileModes[0];
+        fTileModes[1] = tileModes[1];
+        fBilerp = bilerp;
+    }
+
+    void setClampNoFilter() {
+        fTileModes[0] = fTileModes[1] = SkShader::kClamp_TileMode;
+        fBilerp = false;
+    }
+
+    void setClamp() {
+        fTileModes[0] = fTileModes[1] = SkShader::kClamp_TileMode;
+    }
+
+    void setBilerp(bool bilerp) { fBilerp = bilerp; }
+
+    void setTileModeX(const SkShader::TileMode tm) { fTileModes[0] = tm; }
+    void setTileModeY(const SkShader::TileMode tm) { fTileModes[1] = tm; }
+    void setTileModeXAndY(const SkShader::TileMode tm) { fTileModes[0] = fTileModes[1] = tm; }
+
+    SkShader::TileMode getTileModeX() const { return fTileModes[0]; }
+
+    SkShader::TileMode getTileModeY() const { return fTileModes[1]; }
+
+    bool isTiled() const {
+        return SkShader::kClamp_TileMode != fTileModes[0] ||
+               SkShader::kClamp_TileMode != fTileModes[1];
+    }
+
+    bool isBilerp() const { return fBilerp; }
+
+    bool operator== (const GrTextureParams& other) const {
+        return fTileModes[0] == other.fTileModes[0] &&
+               fTileModes[1] == other.fTileModes[1] &&
+               fBilerp == other.fBilerp;
+    }
+
+    bool operator!= (const GrTextureParams& other) const { return !(*this == other); }
+
+private:
+
+    SkShader::TileMode fTileModes[2];
+    bool               fBilerp;
+};
+
 /** A class representing the swizzle access pattern for a texture. Note that if the texture is
  *  an alpha-only texture then the alpha channel is substituted for other components. Any mangling
  *  to handle the r,g,b->a conversions for alpha textures is automatically included in the stage
@@ -28,18 +115,44 @@
     GrTextureAccess();
 
     /**
+     * Uses the default swizzle, "rgba".
+     */
+    GrTextureAccess(GrTexture*, const GrTextureParams&);
+    explicit GrTextureAccess(GrTexture*,
+                             bool bilerp = false,
+                             SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
+
+    /**
      * swizzle must be a string between one and four (inclusive) characters containing only 'r',
      * 'g', 'b',  and/or 'a'.
      */
-    GrTextureAccess(GrTexture*, const char* swizzle);
+    GrTextureAccess(GrTexture*, const char* swizzle, const GrTextureParams&);
+    GrTextureAccess(GrTexture*,
+                    const char* swizzle,
+                    bool bilerp = false,
+                    SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
 
-    /**
-     * Uses the default swizzle, "rgba".
-     */
-    GrTextureAccess(GrTexture*);
+    void reset(GrTexture*, const GrTextureParams&);
+    void reset(GrTexture*,
+               bool bilerp = false,
+               SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
+    void reset(GrTexture*, const char* swizzle, const GrTextureParams&);
+    void reset(GrTexture*,
+               const char* swizzle,
+               bool bilerp = false,
+               SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
 
-    void reset(GrTexture*, const char* swizzle);
-    void reset(GrTexture*);
+    bool operator== (const GrTextureAccess& other) const {
+#if GR_DEBUG
+        // below assumes all chars in fSwizzle are initialized even if string is < 4 chars long.
+        GrAssert(memcmp(fSwizzle, other.fSwizzle, sizeof(fSwizzle)) ==
+                 strcmp(fSwizzle, other.fSwizzle));
+#endif
+        return fParams == other.fParams &&
+               memcmp(fSwizzle, other.fSwizzle, sizeof(fSwizzle));
+    }
+
+    bool operator!= (const GrTextureAccess& other) const { return !(*this == other); }
 
     GrTexture* getTexture() const { return fTexture.get(); }
 
@@ -60,10 +173,17 @@
     /** Returns a mask indicating which components are referenced in the swizzle. */
     uint32_t swizzleMask() const { return fSwizzleMask; }
 
+    const GrTextureParams& getParams() const { return fParams; }
+
 private:
+    void setSwizzle(const char*);
+
+    GrTextureParams         fParams;
     SkAutoTUnref<GrTexture> fTexture;
     uint32_t                fSwizzleMask;
     char                    fSwizzle[5];
+
+    typedef GrNoncopyable INHERITED;
 };
 
 #endif
diff --git a/include/gpu/SkGpuDevice.h b/include/gpu/SkGpuDevice.h
index 49eea26..04c4e0f 100644
--- a/include/gpu/SkGpuDevice.h
+++ b/include/gpu/SkGpuDevice.h
@@ -156,8 +156,12 @@
                           const GrTextureParams& sampler,
                           const SkIRect* srcRectPtr,
                           int* tileSize) const;
-    void internalDrawBitmap(const SkDraw&, const SkBitmap&,
-                            const SkIRect&, const SkMatrix&, GrPaint* grPaint);
+    void internalDrawBitmap(const SkDraw&,
+                            const SkBitmap&,
+                            const SkIRect&,
+                            const SkMatrix&,
+                            const GrTextureParams& params,
+                            GrPaint* grPaint);
 
     /**
      * Returns non-initialized instance.