re-land hide get/setLocalMatrix

This reverts commit b1d702a43b07934f5b001b1b09db2c57ede909a1.

TBR=scroggo@google.com

Author: reed@google.com

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

git-svn-id: http://skia.googlecode.com/svn/trunk@14702 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gm/perlinnoise.cpp b/gm/perlinnoise.cpp
index 1351ed7..8ffb7af 100644
--- a/gm/perlinnoise.cpp
+++ b/gm/perlinnoise.cpp
@@ -145,7 +145,7 @@
 
         SkMatrix lm;
         lm.setScale(2, 2);
-        paint.getShader()->setLocalMatrix(lm);
+        paint.setShader(SkShader::CreateLocalMatrixShader(paint.getShader(), lm))->unref();
         r.fRight += r.width();
         r.fBottom += r.height();
 
diff --git a/include/core/SkShader.h b/include/core/SkShader.h
index a9ecad8..ce5f003 100644
--- a/include/core/SkShader.h
+++ b/include/core/SkShader.h
@@ -38,15 +38,16 @@
     virtual ~SkShader();
 
     /**
-     *  Returns true if the local matrix is not an identity matrix.
-     */
-    bool hasLocalMatrix() const { return !fLocalMatrix.isIdentity(); }
-
-    /**
      *  Returns the local matrix.
      */
     const SkMatrix& getLocalMatrix() const { return fLocalMatrix; }
 
+#ifdef SK_SUPPORT_LEGACY_SHADER_LOCALMATRIX
+    /**
+     *  Returns true if the local matrix is not an identity matrix.
+     */
+    bool hasLocalMatrix() const { return !fLocalMatrix.isIdentity(); }
+
     /**
      *  Set the shader's local matrix.
      *  @param localM   The shader's new local matrix.
@@ -57,6 +58,7 @@
      *  Reset the shader's local matrix to identity.
      */
     void resetLocalMatrix() { fLocalMatrix.reset(); }
+#endif
 
     enum TileMode {
         /** replicate the edge color if the shader draws outside of its
diff --git a/samplecode/SampleFatBits.cpp b/samplecode/SampleFatBits.cpp
index 9ea1d9e..6057c68 100644
--- a/samplecode/SampleFatBits.cpp
+++ b/samplecode/SampleFatBits.cpp
@@ -35,7 +35,7 @@
     surface->getCanvas()->clear(SK_ColorTRANSPARENT);
 }
 
-static SkShader* createChecker() {
+static SkShader* createChecker(const SkMatrix& localMatrix) {
 //    SkColor colors[] = { 0xFFFDFDFD, 0xFFF4F4F4 };
     SkColor colors[] = { 0xFFFFFFFF, 0xFFFFFFFF };
     SkBitmap bm;
@@ -43,15 +43,13 @@
     bm.lockPixels();
     *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = SkPreMultiplyColor(colors[0]);
     *bm.getAddr32(0, 1) = *bm.getAddr32(1, 0) = SkPreMultiplyColor(colors[1]);
-    SkMatrix m;
-    m.setScale(12, 12);
     return SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
-                                        SkShader::kRepeat_TileMode, &m);
+                                        SkShader::kRepeat_TileMode, &localMatrix);
 }
 
 class FatBits {
 public:
-    FatBits() : fShader(createChecker()) {
+    FatBits() {
         fAA = false;
         fStyle = kHair_Style;
         fGrid = true;
@@ -100,7 +98,7 @@
         fBounds.set(0, 0, SkIntToScalar(width * zoom), SkIntToScalar(height * zoom));
         fMatrix.setScale(SkIntToScalar(zoom), SkIntToScalar(zoom));
         fInverse.setScale(SK_Scalar1 / zoom, SK_Scalar1 / zoom);
-        fShader->setLocalMatrix(fMatrix);
+        fShader.reset(createChecker(fMatrix));
 
         SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
         fMinSurface.reset(SkSurface::NewRaster(info));
diff --git a/src/core/SkLocalMatrixShader.cpp b/src/core/SkLocalMatrixShader.cpp
index 7af025c..53580e6 100644
--- a/src/core/SkLocalMatrixShader.cpp
+++ b/src/core/SkLocalMatrixShader.cpp
@@ -5,65 +5,11 @@
  * found in the LICENSE file.
  */
 
-#include "SkShader.h"
-#include "SkReadBuffer.h"
-#include "SkWriteBuffer.h"
-
-class SkLocalMatrixShader : public SkShader {
-public:
-    SkLocalMatrixShader(SkShader* proxy, const SkMatrix& localMatrix)
-        : fProxyShader(SkRef(proxy))
-        , fProxyLocalMatrix(localMatrix)
-    {}
-
-    virtual size_t contextSize() const SK_OVERRIDE {
-        return fProxyShader->contextSize();
-    }
-
-    virtual BitmapType asABitmap(SkBitmap* bitmap, SkMatrix* matrix,
-                                 TileMode* mode) const SK_OVERRIDE {
-        return fProxyShader->asABitmap(bitmap, matrix, mode);
-    }
-
-    virtual GradientType asAGradient(GradientInfo* info) const SK_OVERRIDE {
-        return fProxyShader->asAGradient(info);
-    }
-
-    // TODO: need to augment this API to pass in a localmatrix (which we can augment)
-    virtual GrEffectRef* asNewEffect(GrContext* ctx, const SkPaint& paint,
-                                     const SkMatrix* localMatrix) const SK_OVERRIDE {
-        SkMatrix tmp = fProxyLocalMatrix;
-        if (localMatrix) {
-            tmp.preConcat(*localMatrix);
-        }
-        return fProxyShader->asNewEffect(ctx, paint, &tmp);
-    }
-
-    virtual SkShader* refAsALocalMatrixShader(SkMatrix* localMatrix) const SK_OVERRIDE {
-        if (localMatrix) {
-            *localMatrix = fProxyLocalMatrix;
-        }
-        return SkRef(fProxyShader.get());
-    }
-
-    SK_TO_STRING_OVERRIDE()
-    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLocalMatrixShader)
-
-protected:
-    SkLocalMatrixShader(SkReadBuffer&);
-    virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
-    virtual Context* onCreateContext(const ContextRec&, void*) const SK_OVERRIDE;
-
-private:
-    SkAutoTUnref<SkShader> fProxyShader;
-    SkMatrix  fProxyLocalMatrix;
-
-    typedef SkShader INHERITED;
-};
+#include "SkLocalMatrixShader.h"
 
 SkLocalMatrixShader::SkLocalMatrixShader(SkReadBuffer& buffer) : INHERITED(buffer) {
     buffer.readMatrix(&fProxyLocalMatrix);
-    fProxyShader.reset(buffer.readFlattenable<SkShader>());
+    fProxyShader.reset(buffer.readShader());
     if (NULL == fProxyShader.get()) {
         sk_throw();
     }
diff --git a/src/core/SkLocalMatrixShader.h b/src/core/SkLocalMatrixShader.h
new file mode 100644
index 0000000..95e6237
--- /dev/null
+++ b/src/core/SkLocalMatrixShader.h
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkLocalMatrixShader_DEFINED
+#define SkLocalMatrixShader_DEFINED
+
+#include "SkShader.h"
+#include "SkReadBuffer.h"
+#include "SkWriteBuffer.h"
+
+class SkLocalMatrixShader : public SkShader {
+public:
+    SkLocalMatrixShader(SkShader* proxy, const SkMatrix& localMatrix)
+    : fProxyShader(SkRef(proxy))
+    , fProxyLocalMatrix(localMatrix)
+    {}
+    
+    virtual size_t contextSize() const SK_OVERRIDE {
+        return fProxyShader->contextSize();
+    }
+    
+    virtual BitmapType asABitmap(SkBitmap* bitmap, SkMatrix* matrix,
+                                 TileMode* mode) const SK_OVERRIDE {
+        return fProxyShader->asABitmap(bitmap, matrix, mode);
+    }
+    
+    virtual GradientType asAGradient(GradientInfo* info) const SK_OVERRIDE {
+        return fProxyShader->asAGradient(info);
+    }
+    
+    virtual GrEffectRef* asNewEffect(GrContext* ctx, const SkPaint& paint,
+                                     const SkMatrix* localMatrix) const SK_OVERRIDE {
+        SkMatrix tmp = fProxyLocalMatrix;
+        if (localMatrix) {
+            tmp.preConcat(*localMatrix);
+        }
+        return fProxyShader->asNewEffect(ctx, paint, &tmp);
+    }
+    
+    virtual SkShader* refAsALocalMatrixShader(SkMatrix* localMatrix) const SK_OVERRIDE {
+        if (localMatrix) {
+            *localMatrix = fProxyLocalMatrix;
+        }
+        return SkRef(fProxyShader.get());
+    }
+    
+    SK_TO_STRING_OVERRIDE()
+    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLocalMatrixShader)
+    
+protected:
+    SkLocalMatrixShader(SkReadBuffer&);
+    virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
+    virtual Context* onCreateContext(const ContextRec&, void*) const SK_OVERRIDE;
+    
+private:
+    SkAutoTUnref<SkShader> fProxyShader;
+    SkMatrix  fProxyLocalMatrix;
+    
+    typedef SkShader INHERITED;
+};
+
+#endif
diff --git a/src/core/SkPictureShader.cpp b/src/core/SkPictureShader.cpp
index 300a653..ecb86e7 100644
--- a/src/core/SkPictureShader.cpp
+++ b/src/core/SkPictureShader.cpp
@@ -56,11 +56,7 @@
     SkASSERT(fPicture && fPicture->width() > 0 && fPicture->height() > 0);
 
     SkMatrix m;
-    if (this->hasLocalMatrix()) {
-        m.setConcat(matrix, this->getLocalMatrix());
-    } else {
-        m = matrix;
-    }
+    m.setConcat(matrix, this->getLocalMatrix());
     if (localM) {
         m.preConcat(*localM);
     }
diff --git a/src/core/SkShader.cpp b/src/core/SkShader.cpp
index 75a5fdb..6a418b6 100644
--- a/src/core/SkShader.cpp
+++ b/src/core/SkShader.cpp
@@ -60,7 +60,7 @@
 
 void SkShader::flatten(SkWriteBuffer& buffer) const {
     this->INHERITED::flatten(buffer);
-    bool hasLocalM = this->hasLocalMatrix();
+    bool hasLocalM = !fLocalMatrix.isIdentity();
     buffer.writeBool(hasLocalM);
     if (hasLocalM) {
         buffer.writeMatrix(fLocalMatrix);
@@ -68,13 +68,10 @@
 }
 
 bool SkShader::computeTotalInverse(const ContextRec& rec, SkMatrix* totalInverse) const {
-    const SkMatrix* m = rec.fMatrix;
-    SkMatrix        total;
+    SkMatrix total;
+    total.setConcat(*rec.fMatrix, fLocalMatrix);
 
-    if (this->hasLocalMatrix()) {
-        total.setConcat(*m, this->getLocalMatrix());
-        m = &total;
-    }
+    const SkMatrix* m = &total;
     if (rec.fLocalMatrix) {
         total.setConcat(*m, *rec.fLocalMatrix);
         m = &total;
@@ -235,9 +232,9 @@
 
 #ifndef SK_IGNORE_TO_STRING
 void SkShader::toString(SkString* str) const {
-    if (this->hasLocalMatrix()) {
+    if (!fLocalMatrix.isIdentity()) {
         str->append(" ");
-        this->getLocalMatrix().toString(str);
+        fLocalMatrix.toString(str);
     }
 }
 #endif
diff --git a/src/effects/SkRectShaderImageFilter.cpp b/src/effects/SkRectShaderImageFilter.cpp
index 13e59c2..bed017c 100644
--- a/src/effects/SkRectShaderImageFilter.cpp
+++ b/src/effects/SkRectShaderImageFilter.cpp
@@ -66,13 +66,15 @@
         return false;
     }
     SkCanvas canvas(device.get());
+
     SkPaint paint;
-    paint.setShader(fShader);
     SkMatrix matrix(ctx.ctm());
     matrix.postTranslate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.top()));
-    fShader->setLocalMatrix(matrix);
+    paint.setShader(SkShader::CreateLocalMatrixShader(fShader, matrix))->unref();
+
     SkRect rect = SkRect::MakeWH(SkIntToScalar(bounds.width()), SkIntToScalar(bounds.height()));
     canvas.drawRect(rect, paint);
+
     *result = device.get()->accessBitmap(false);
     offset->fX = bounds.fLeft;
     offset->fY = bounds.fTop;
diff --git a/src/pdf/SkPDFDeviceFlattener.cpp b/src/pdf/SkPDFDeviceFlattener.cpp
index 91c9803..aea87f6 100644
--- a/src/pdf/SkPDFDeviceFlattener.cpp
+++ b/src/pdf/SkPDFDeviceFlattener.cpp
@@ -6,7 +6,6 @@
  */
 
 #include "SkPDFDeviceFlattener.h"
-
 #include "SkDraw.h"
 
 static SkISize SkSizeToISize(const SkSize& size) {
@@ -25,9 +24,9 @@
 
 static void flattenPaint(const SkDraw& d, SkPaint* paint) {
     if (paint->getShader()) {
-        SkMatrix local = paint->getShader()->getLocalMatrix();
-        local.preConcat(*d.fMatrix);
-        paint->getShader()->setLocalMatrix(local);
+        SkAutoTUnref<SkShader> lms(SkShader::CreateLocalMatrixShader(paint->getShader(),
+                                                                     *d.fMatrix));
+        paint->setShader(lms);
     }
 }
 
diff --git a/src/ports/SkGlobalInitialization_chromium.cpp b/src/ports/SkGlobalInitialization_chromium.cpp
index a3ef5b2..37e5450 100644
--- a/src/ports/SkGlobalInitialization_chromium.cpp
+++ b/src/ports/SkGlobalInitialization_chromium.cpp
@@ -42,6 +42,7 @@
 #include "SkLayerRasterizer.h"
 #include "SkLerpXfermode.h"
 #include "SkLightingImageFilter.h"
+#include "SkLocalMatrixShader.h"
 #include "SkLumaColorFilter.h"
 #include "SkMagnifierImageFilter.h"
 #include "SkMatrixConvolutionImageFilter.h"
@@ -84,6 +85,7 @@
     SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLayerDrawLooper)
     SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLayerRasterizer)
     SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLerpXfermode)
+    SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLocalMatrixShader)
     SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLumaColorFilter)
     SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPath1DPathEffect)
     SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(Sk2DPathEffect)
diff --git a/src/ports/SkGlobalInitialization_default.cpp b/src/ports/SkGlobalInitialization_default.cpp
index a3ef5b2..1a12dee 100644
--- a/src/ports/SkGlobalInitialization_default.cpp
+++ b/src/ports/SkGlobalInitialization_default.cpp
@@ -42,6 +42,7 @@
 #include "SkLayerRasterizer.h"
 #include "SkLerpXfermode.h"
 #include "SkLightingImageFilter.h"
+#include "SkLocalMatrixShader.h"
 #include "SkLumaColorFilter.h"
 #include "SkMagnifierImageFilter.h"
 #include "SkMatrixConvolutionImageFilter.h"
@@ -84,6 +85,7 @@
     SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLayerDrawLooper)
     SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLayerRasterizer)
     SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLerpXfermode)
+    SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLocalMatrixShader)
     SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLumaColorFilter)
     SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPath1DPathEffect)
     SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(Sk2DPathEffect)
@@ -117,6 +119,7 @@
     SkLightingImageFilter::InitializeFlattenables();
     SkTableColorFilter::InitializeFlattenables();
     SkXfermode::InitializeFlattenables();
+    
 }
 
 void SkFlattenable::InitializeFlattenablesIfNeeded() {