Fix rendering artifacts in pull-saveLayers-forward mode

This CL fixes 2 bugs in the pre-rendering of saveLayers:

1) The drawBitmapRect call wasn't occurring in device space so could sometimes be double transformed

2) The BBH op skipping in SkPicturePlayback could sometimes mess up the SkPictureStateTree's state

It also reduces the number of layers that are pre-rendered when a BBH is not in use.

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

R=bsalomon@google.com, reed@google.com

Author: robertphillips@google.com

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

git-svn-id: http://skia.googlecode.com/svn/trunk@14659 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/gpu/GrPictureUtils.cpp b/src/gpu/GrPictureUtils.cpp
index a66f34c..84a13be 100644
--- a/src/gpu/GrPictureUtils.cpp
+++ b/src/gpu/GrPictureUtils.cpp
@@ -137,16 +137,7 @@
         device->fInfo.fCTM.postTranslate(SkIntToScalar(-device->getOrigin().fX),
                                          SkIntToScalar(-device->getOrigin().fY));
 
-        // We need the x & y values that will yield 'getOrigin' when transformed
-        // by 'draw.fMatrix'.
-        device->fInfo.fOffset.iset(device->getOrigin());
-
-        SkMatrix invMatrix;
-        if (draw.fMatrix->invert(&invMatrix)) {
-            invMatrix.mapPoints(&device->fInfo.fOffset, 1);
-        } else {
-            device->fInfo.fValid = false;
-        }
+        device->fInfo.fOffset = device->getOrigin();
 
         if (NeedsDeepCopy(paint)) {
             // This NULL acts as a signal that the paint was uncopyable (for now)
diff --git a/src/gpu/GrPictureUtils.h b/src/gpu/GrPictureUtils.h
index b8a7814..a280a16 100644
--- a/src/gpu/GrPictureUtils.h
+++ b/src/gpu/GrPictureUtils.h
@@ -27,8 +27,8 @@
         // the translation needed to map the layer's top-left point to the origin.
         SkMatrix fCTM;
         // The offset that needs to be passed to drawBitmap to correctly
-        // position the pre-rendered layer.
-        SkPoint fOffset;
+        // position the pre-rendered layer. It is in device space.
+        SkIPoint fOffset;
         // The paint to use on restore. NULL if the paint was not copyable (and
         // thus that this layer should not be pulled forward).
         const SkPaint* fPaint;
diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp
index b279c43..483a149 100644
--- a/src/gpu/SkGpuDevice.cpp
+++ b/src/gpu/SkGpuDevice.cpp
@@ -1997,12 +1997,19 @@
         }
     } else {
         // In this case there is no BBH associated with the picture. Pre-render
-        // all the layers
-        // TODO: intersect the bounds of each layer with the clip region to
-        // reduce the number of pre-rendered layers
+        // all the layers that intersect the drawn region
         for (int j = 0; j < gpuData->numSaveLayers(); ++j) {
             const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
 
+            SkIRect layerRect = SkIRect::MakeXYWH(info.fOffset.fX,
+                                                  info.fOffset.fY,
+                                                  info.fSize.fWidth,
+                                                  info.fSize.fHeight);
+
+            if (!SkIRect::Intersects(query, layerRect)) {
+                continue;
+            }
+
             // TODO: once this code is more stable unsuitable layers can
             // just be omitted during the optimization stage
             if (!info.fValid ||