Apply matrix early in draw bitmap

Committed: http://code.google.com/p/skia/source/detail?r=11930

Reverted: https://code.google.com/p/skia/source/detail?r=11932

R=robertphillips@google.com, senorblanco@chromium.org

Author: bsalomon@google.com

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

git-svn-id: http://skia.googlecode.com/svn/trunk/src@11933 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/core/SkImageFilterUtils.cpp b/core/SkImageFilterUtils.cpp
index a4ce51e..8385fb4 100644
--- a/core/SkImageFilterUtils.cpp
+++ b/core/SkImageFilterUtils.cpp
@@ -23,6 +23,11 @@
 bool SkImageFilterUtils::GetInputResultGPU(SkImageFilter* filter, SkImageFilter::Proxy* proxy,
                                            const SkBitmap& src, const SkMatrix& ctm,
                                            SkBitmap* result, SkIPoint* offset) {
+    // Ensure that GrContext calls under filterImage and filterImageGPU below will see an identity
+    // matrix with no clip and that the matrix, clip, and render target set before this function was
+    // called are restored before we return to the caller.
+    GrContext* context = src.getTexture()->getContext();
+    GrContext::AutoWideOpenIdentityDraw awoid(context, NULL);
     if (!filter) {
         *result = src;
         return true;
@@ -31,9 +36,7 @@
     } else {
         if (filter->filterImage(proxy, src, ctm, result, offset)) {
             if (!result->getTexture()) {
-                GrContext* context = ((GrTexture *) src.getTexture())->getContext();
-                GrTexture* resultTex = GrLockAndRefCachedBitmapTexture(context,
-                    *result, NULL);
+                GrTexture* resultTex = GrLockAndRefCachedBitmapTexture(context, *result, NULL);
                 result->setPixelRef(new SkGrPixelRef(resultTex))->unref();
                 GrUnlockAndUnrefCachedBitmapTexture(resultTex);
             }
diff --git a/gpu/SkGpuDevice.cpp b/gpu/SkGpuDevice.cpp
index 47ddd52..6617989 100644
--- a/gpu/SkGpuDevice.cpp
+++ b/gpu/SkGpuDevice.cpp
@@ -1164,6 +1164,8 @@
         return;
     }
 
+    fContext->concatMatrix(m);
+
     GrTextureParams params;
     SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
     GrTextureParams::FilterMode textureFilterMode;
@@ -1195,9 +1197,9 @@
 
     if (!this->shouldTileBitmap(bitmap, params, srcRectPtr)) {
         // take the simple case
-        this->internalDrawBitmap(bitmap, srcRect, m, params, paint, flags);
+        this->internalDrawBitmap(bitmap, srcRect, params, paint, flags);
     } else {
-        this->drawTiledBitmap(bitmap, srcRect, m, params, paint, flags);
+        this->drawTiledBitmap(bitmap, srcRect, params, paint, flags);
     }
 }
 
@@ -1205,7 +1207,6 @@
 // been determined to be too large to fit in VRAM
 void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
                                   const SkRect& srcRect,
-                                  const SkMatrix& m,
                                   const GrTextureParams& params,
                                   const SkPaint& paint,
                                   SkCanvas::DrawBitmapRectFlags flags) {
@@ -1226,9 +1227,8 @@
         if (!fContext->getClip()->fClipStack->intersectRectWithClip(&clipRect)) {
             return;
         }
-        SkMatrix matrix, inverse;
-        matrix.setConcat(fContext->getMatrix(), m);
-        if (!matrix.invert(&inverse)) {
+        SkMatrix inverse;
+        if (!fContext->getMatrix().invert(&inverse)) {
             return;
         }
         inverse.mapRect(&clipRect);
@@ -1278,10 +1278,11 @@
             if (bitmap.extractSubset(&tmpB, iTileR)) {
                 // now offset it to make it "local" to our tmp bitmap
                 tileR.offset(-offset.fX, -offset.fY);
-                SkMatrix tmpM(m);
-                tmpM.preTranslate(offset.fX, offset.fY);
-
-                this->internalDrawBitmap(tmpB, tileR, tmpM, params, paint, flags);
+                SkMatrix tmpM;
+                tmpM.setTranslate(offset.fX, offset.fY);
+                GrContext::AutoMatrix am;
+                am.setPreConcat(fContext, tmpM);
+                this->internalDrawBitmap(tmpB, tileR, params, paint, flags);
             }
         }
     }
@@ -1338,7 +1339,6 @@
  */
 void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
                                      const SkRect& srcRect,
-                                     const SkMatrix& m,
                                      const GrTextureParams& params,
                                      const SkPaint& paint,
                                      SkCanvas::DrawBitmapRectFlags flags) {
@@ -1366,19 +1366,18 @@
         // Need texture domain if drawing a sub rect.
         needsTextureDomain = srcRect.width() < bitmap.width() ||
                              srcRect.height() < bitmap.height();
-        if (needsTextureDomain && m.rectStaysRect() && fContext->getMatrix().rectStaysRect()) {
+        if (needsTextureDomain && fContext->getMatrix().rectStaysRect()) {
+            const SkMatrix& matrix = fContext->getMatrix();
             // sampling is axis-aligned
             SkRect transformedRect;
-            SkMatrix srcToDeviceMatrix(m);
-            srcToDeviceMatrix.postConcat(fContext->getMatrix());
-            srcToDeviceMatrix.mapRect(&transformedRect, srcRect);
-
+            matrix.mapRect(&transformedRect, srcRect);
+            
             if (has_aligned_samples(srcRect, transformedRect)) {
                 // We could also turn off filtering here (but we already did a cache lookup with
                 // params).
                 needsTextureDomain = false;
             } else {
-                needsTextureDomain = may_color_bleed(srcRect, transformedRect, m);
+                needsTextureDomain = may_color_bleed(srcRect, transformedRect, matrix);
             }
         }
     }
@@ -1421,7 +1420,7 @@
         return;
     }
 
-    fContext->drawRectToRect(grPaint, dstRect, paintRect, &m);
+    fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
 }
 
 static bool filter_texture(SkBaseDevice* device, GrContext* context,