Access sampler matrices directly, cleanup GrSamplerState::reset()s

Review URL: http://codereview.appspot.com/5488048/


git-svn-id: http://skia.googlecode.com/svn/trunk@2854 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gm/texdata.cpp b/gm/texdata.cpp
index 353c4c0..c68a16a 100644
--- a/gm/texdata.cpp
+++ b/gm/texdata.cpp
@@ -109,8 +109,9 @@
                 ctx->setMatrix(vm);
                 GrMatrix tm;
                 tm = vm;
-                tm.postIDiv(2*S, 2*S);
-                paint.textureSampler(0)->setMatrix(tm);
+                GrMatrix* sampleMat = paint.textureSampler(0)->matrix();
+                *sampleMat = vm;
+                sampleMat->postIDiv(2*S, 2*S);
                 paint.setTexture(0, texture);
 
                 ctx->drawRect(paint, GrRect::MakeWH(2*S, 2*S));
diff --git a/include/gpu/GrSamplerState.h b/include/gpu/GrSamplerState.h
index f1fc86d..81dfdb3 100644
--- a/include/gpu/GrSamplerState.h
+++ b/include/gpu/GrSamplerState.h
@@ -38,7 +38,9 @@
         /**
          * Apply a separable convolution kernel.
          */
-        kConvolution_Filter
+        kConvolution_Filter,
+
+        kDefault_Filter = kNearest_Filter
     };
 
     /**
@@ -68,6 +70,8 @@
         kRadial_SampleMode,     //!< treat as radial gradient
         kRadial2_SampleMode,    //!< treat as 2-point radial gradient
         kSweep_SampleMode,      //!< treat as sweep gradient
+
+        kDefault_SampleMode = kNormal_SampleMode
     };
 
     /**
@@ -77,7 +81,9 @@
     enum WrapMode {
         kClamp_WrapMode,
         kRepeat_WrapMode,
-        kMirror_WrapMode
+        kMirror_WrapMode,
+
+        kDefault_WrapMode = kClamp_WrapMode
     };
 
     /**
@@ -114,12 +120,11 @@
     void setSampleMode(SampleMode mode) { fSampleMode = mode; }
     
     /**
-     * Sets the sampler's matrix. See SampleMode for explanation of
+     * Access the sampler's matrix. See SampleMode for explanation of
      * relationship between the matrix and sample mode.
-     * @param matrix the matrix to set
      */
-    void setMatrix(const GrMatrix& matrix) { fMatrix = matrix; }
-    
+    GrMatrix* matrix() { return &fMatrix; }
+
     /**
      * Sets the sampler's texture coordinate domain to a 
      * custom rectangle, rather than the default (0,1).
@@ -151,45 +156,26 @@
      */
     void setFilter(Filter filter) { fFilter = filter; }
 
-    void reset() {
-        fWrapX = kClamp_WrapMode;
-        fWrapY = kClamp_WrapMode;
-        fSampleMode = kNormal_SampleMode;
-        fFilter = kNearest_Filter;
-        fMatrix.setIdentity();
-        fTextureDomain.setEmpty();
-        fSwapRAndB = false;
-    }
-
-    void reset(WrapMode wrapXAndY,
-               Filter filter) {
-        fWrapX = wrapXAndY;
-        fWrapY = wrapXAndY;
-        fSampleMode = kNormal_SampleMode;
-        fFilter = filter;
-        fMatrix.setIdentity();
-        fTextureDomain.setEmpty();
-        fSwapRAndB = false;
-    }
     void reset(WrapMode wrapXAndY,
                Filter filter,
                const GrMatrix& matrix) {
         fWrapX = wrapXAndY;
         fWrapY = wrapXAndY;
-        fSampleMode = kNormal_SampleMode;
+        fSampleMode = kDefault_SampleMode;
         fFilter = filter;
         fMatrix = matrix;
         fTextureDomain.setEmpty();
         fSwapRAndB = false;
     }
+    void reset(WrapMode wrapXAndY,
+               Filter filter) {
+        this->reset(wrapXAndY, filter, GrMatrix::I());
+    }
     void reset(const GrMatrix& matrix) {
-        fWrapX = kClamp_WrapMode;
-        fWrapY = kClamp_WrapMode;
-        fSampleMode = kNormal_SampleMode;
-        fFilter = kNearest_Filter;
-        fMatrix = matrix;
-        fTextureDomain.setEmpty();
-        fSwapRAndB = false;
+        this->reset(kDefault_WrapMode, kDefault_Filter, matrix);
+    }
+    void reset() {
+        this->reset(kDefault_WrapMode, kDefault_Filter, GrMatrix::I());
     }
 
     GrScalar getRadial2CenterX1() const { return fRadial2CenterX1; }
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index 4e0e587..4a16d9d 100644
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -868,8 +868,6 @@
         filter = GrSamplerState::kBilinear_Filter;
     }
 
-    GrMatrix sampleM;
-
     GrTexture* src = record->fOffscreen0.texture();
     int scale;
 
@@ -888,11 +886,10 @@
         drawState->setTexture(kOffscreenStage, src);
         drawState->setRenderTarget(dst);
         drawState->setViewMatrix(GrMatrix::I());
-        sampleM.setScale(scale * GR_Scalar1 / src->width(),
-                         scale * GR_Scalar1 / src->height());
         GrSamplerState* sampler = drawState->sampler(kOffscreenStage);
         sampler->reset(GrSamplerState::kClamp_WrapMode, filter);
-        sampler->setMatrix(sampleM);
+        sampler->matrix()->setScale(scale * GR_Scalar1 / src->width(),
+                                    scale * GR_Scalar1 / src->height());
         GrRect rect = SkRect::MakeWH(SkIntToScalar(scale * tileRect.width()),
                                      SkIntToScalar(scale * tileRect.height()));
         target->drawSimpleRect(rect, NULL, 1 << kOffscreenStage);
@@ -930,13 +927,10 @@
     drawState->setTexture(kOffscreenStage, src);
     GrSamplerState* sampler = drawState->sampler(kOffscreenStage);
     sampler->reset(GrSamplerState::kClamp_WrapMode, filter);
-    sampleM.setScale(scale * GR_Scalar1 / src->width(),
-                     scale * GR_Scalar1 / src->height());
-    
-    sampler->setMatrix(sampleM);
-    sampleM.setTranslate(SkIntToScalar(-tileRect.fLeft),
-                         SkIntToScalar(-tileRect.fTop));
-    sampler->preConcatMatrix(sampleM);
+    sampler->matrix()->setScale(scale * GR_Scalar1 / src->width(),
+                                scale * GR_Scalar1 / src->height());
+    sampler->matrix()->preTranslate(SkIntToScalar(-tileRect.fLeft),
+                                    SkIntToScalar(-tileRect.fTop));
 
     GrRect dstRect;
     int stages = (1 << kOffscreenStage) | stageMask;
diff --git a/src/gpu/GrDrawTarget.cpp b/src/gpu/GrDrawTarget.cpp
index aa5cf20..4211da9 100644
--- a/src/gpu/GrDrawTarget.cpp
+++ b/src/gpu/GrDrawTarget.cpp
@@ -1085,7 +1085,7 @@
     drawState->setViewMatrix(fViewMatrix);
     for (int s = 0; s < GrDrawState::kNumStages; ++s) {
         if (fStageMask & (1 << s)) {
-            drawState->sampler(s)->setMatrix(fSamplerMatrices[s]);
+            *drawState->sampler(s)->matrix() = fSamplerMatrices[s];
         }
     }
 }
diff --git a/src/gpu/GrGpuGL.cpp b/src/gpu/GrGpuGL.cpp
index fb16b22..f266368 100644
--- a/src/gpu/GrGpuGL.cpp
+++ b/src/gpu/GrGpuGL.cpp
@@ -518,7 +518,7 @@
         fHWDrawState.sampler(s)->setRadial2Params(-GR_ScalarMax,
                                                   -GR_ScalarMax,
                                                   true);
-        fHWDrawState.sampler(s)->setMatrix(GrMatrix::InvalidMatrix());
+        *fHWDrawState.sampler(s)->matrix() = GrMatrix::InvalidMatrix();
         fHWDrawState.sampler(s)->setConvolutionParams(0, NULL, NULL);
     }
 
diff --git a/src/gpu/GrGpuGLShaders.cpp b/src/gpu/GrGpuGLShaders.cpp
index 5534d05..5fe90b8 100644
--- a/src/gpu/GrGpuGLShaders.cpp
+++ b/src/gpu/GrGpuGLShaders.cpp
@@ -374,7 +374,7 @@
     GrAssert(fProgramData);
     if (GrGLProgram::kSetAsAttribute == 
         fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
-        fHWDrawState.sampler(stage)->setMatrix(matrix);
+        *fHWDrawState.sampler(stage)->matrix() = matrix;
     } else {
         fProgramData->fTextureMatrices[stage] = matrix;
     }
diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp
index bf00fa3..b80d3a6 100644
--- a/src/gpu/SkGpuDevice.cpp
+++ b/src/gpu/SkGpuDevice.cpp
@@ -473,10 +473,10 @@
     }
 
     SkBitmap bitmap;
-    SkMatrix matrix;
+    SkMatrix* matrix = grPaint->textureSampler(kShaderTextureIdx)->matrix();
     SkShader::TileMode tileModes[2];
     SkScalar twoPointParams[3];
-    SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, &matrix,
+    SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
                                                      tileModes, twoPointParams);
 
     GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype];
@@ -528,18 +528,17 @@
     if (shader->getLocalMatrix(&localM)) {
         SkMatrix inverse;
         if (localM.invert(&inverse)) {
-            matrix.preConcat(inverse);
+            matrix->preConcat(inverse);
         }
     }
     if (SkShader::kDefault_BitmapType == bmptype) {
         GrScalar sx = GrFixedToScalar(GR_Fixed1 / bitmap.width());
         GrScalar sy = GrFixedToScalar(GR_Fixed1 / bitmap.height());
-        matrix.postScale(sx, sy);
+        matrix->postScale(sx, sy);
     } else if (SkShader::kRadial_BitmapType == bmptype) {
         GrScalar s = GrFixedToScalar(GR_Fixed1 / bitmap.width());
-        matrix.postScale(s, s);
+        matrix->postScale(s, s);
     }
-    sampler->setMatrix(matrix);
 
     return true;
 }
@@ -762,9 +761,8 @@
     paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
 
     for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
-        GrMatrix sampleM;
-        sampleM.setIDiv(srcTexture->width(), srcTexture->height());
-        paint.textureSampler(0)->setMatrix(sampleM);
+        paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
+                                                   srcTexture->height());
         context->setRenderTarget(dstTexture->asRenderTarget());
         SkRect dstRect(srcRect);
         scaleRect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f, 
@@ -826,9 +824,8 @@
         context->clear(&clearRect, 0x0);
         // FIXME:  This should be mitchell, not bilinear.
         paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
-        GrMatrix sampleM;
-        sampleM.setIDiv(srcTexture->width(), srcTexture->height());
-        paint.textureSampler(0)->setMatrix(sampleM);
+        paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
+                                                   srcTexture->height());
         context->setRenderTarget(dstTexture->asRenderTarget());
         paint.setTexture(0, srcTexture);
         SkRect dstRect(srcRect);
@@ -935,9 +932,8 @@
         GrPaint paint;
         paint.reset();
         paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
-        GrMatrix sampleM;
-        sampleM.setIDiv(pathTexture->width(), pathTexture->height());
-        paint.textureSampler(0)->setMatrix(sampleM);
+        paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
+                                                   pathTexture->height());
         // Blend pathTexture over blurTexture.
         context->setRenderTarget(blurTexture->asRenderTarget());
         paint.setTexture(0, pathTexture);
@@ -975,10 +971,10 @@
     grp->setMask(MASK_IDX, blurTexture);
     grp->maskSampler(MASK_IDX)->reset();
 
-    GrMatrix m;
-    m.setTranslate(-finalRect.fLeft, -finalRect.fTop);
-    m.postIDiv(blurTexture->width(), blurTexture->height());
-    grp->maskSampler(MASK_IDX)->setMatrix(m);
+    grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
+                                                       -finalRect.fTop);
+    grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
+                                                   blurTexture->height());
     context->drawRect(*grp, finalRect);
     return true;
 }
@@ -1049,12 +1045,10 @@
               GrIntToScalar(dstM.fBounds.fRight),
               GrIntToScalar(dstM.fBounds.fBottom));
 
-    GrMatrix m;
-    m.setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
-                   -dstM.fBounds.fTop*SK_Scalar1);
-    m.postIDiv(texture->width(), texture->height());
-    grp->maskSampler(MASK_IDX)->setMatrix(m);
-    
+    GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
+    m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
+                         -dstM.fBounds.fTop*SK_Scalar1);
+    m->postIDiv(texture->width(), texture->height());
     context->drawRect(*grp, d);
     return true;
 }
@@ -1404,7 +1398,7 @@
     sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
     sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
     sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
-    sampler->setMatrix(GrMatrix::I());
+    sampler->matrix()->reset();
 
     GrTexture* texture;
     SkAutoCachedTexture act(this, bitmap, sampler, &texture);