add canvas::isDrawingToLayer(), as a fast query for chrome; faster than
setting up a drawiter and counting the layers.



git-svn-id: http://skia.googlecode.com/svn/trunk@2875 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/core/SkCanvas.h b/include/core/SkCanvas.h
index 08bd0f0..24e4141 100644
--- a/include/core/SkCanvas.h
+++ b/include/core/SkCanvas.h
@@ -287,6 +287,11 @@
     */
     void restoreToCount(int saveCount);
 
+    /** Returns true if drawing is currently going to a layer (from saveLayer)
+     *  rather than to the root device.
+     */
+    bool isDrawingToLayer() const;
+
     /** Preconcat the current matrix with the specified translation
         @param dx   The distance to translate in X
         @param dy   The distance to translate in Y
@@ -932,6 +937,7 @@
 
     SkBounder*  fBounder;
     SkDevice*   fLastDeviceToGainFocus;
+    int         fLayerCount;    // number of successful saveLayer calls
 
     void prepareForDeviceDraw(SkDevice*, const SkMatrix&, const SkRegion&,
                               const SkClipStack& clipStack);
diff --git a/src/core/SkCanvas.cpp b/src/core/SkCanvas.cpp
index f382893..353c92c 100644
--- a/src/core/SkCanvas.cpp
+++ b/src/core/SkCanvas.cpp
@@ -404,6 +404,7 @@
     fLocalBoundsCompareTypeDirtyBW = true;
     fLastDeviceToGainFocus = NULL;
     fDeviceCMDirty = false;
+    fLayerCount = 0;
 
     fMCRec = (MCRec*)fMCStack.push_back();
     new (fMCRec) MCRec(NULL, 0);
@@ -443,6 +444,8 @@
 SkCanvas::~SkCanvas() {
     // free up the contents of our deque
     this->restoreToCount(1);    // restore everything but the last
+    SkASSERT(0 == fLayerCount);
+
     this->internalRestore();    // restore the last, since we're going away
 
     SkSafeUnref(fBounder);
@@ -747,6 +750,7 @@
     fMCRec->fLayer = layer;
     fMCRec->fTopLayer = layer;    // this field is NOT an owner of layer
 
+    fLayerCount += 1;
     return count;
 }
 
@@ -797,6 +801,9 @@
                              layer->fPaint);
             // reset this, since drawDevice will have set it to true
             fDeviceCMDirty = true;
+
+            SkASSERT(fLayerCount > 0);
+            fLayerCount -= 1;
         }
         SkDELETE(layer);
     }
@@ -820,6 +827,10 @@
     }
 }
 
+bool SkCanvas::isDrawingToLayer() const {
+    return fLayerCount > 0;
+}
+
 /////////////////////////////////////////////////////////////////////////////
 
 // can't draw it if its empty, or its too big for a fixed-point width or height
diff --git a/tests/CanvasTest.cpp b/tests/CanvasTest.cpp
index 7128a22..da1fafd 100644
--- a/tests/CanvasTest.cpp
+++ b/tests/CanvasTest.cpp
@@ -9,6 +9,34 @@
 #include "SkBitmap.h"
 #include "SkCanvas.h"
 
+static void test_isDrawingToLayer(skiatest::Reporter* reporter) {
+    SkBitmap bm;
+    bm.setConfig(SkBitmap::kARGB_8888_Config, 256, 256);
+    bm.allocPixels();
+    
+    SkCanvas canvas(bm);
+
+    REPORTER_ASSERT(reporter, !canvas.isDrawingToLayer());
+    canvas.save();
+    REPORTER_ASSERT(reporter, !canvas.isDrawingToLayer());
+    
+    const SkRect* bounds = NULL;    // null means include entire bounds
+    const SkPaint* paint = NULL;
+
+    canvas.saveLayer(bounds, paint);
+    REPORTER_ASSERT(reporter, canvas.isDrawingToLayer());
+    canvas.restore();
+    REPORTER_ASSERT(reporter, !canvas.isDrawingToLayer());
+
+    canvas.saveLayer(bounds, paint);
+    canvas.saveLayer(bounds, paint);
+    REPORTER_ASSERT(reporter, canvas.isDrawingToLayer());
+    canvas.restore();
+    REPORTER_ASSERT(reporter, canvas.isDrawingToLayer());
+    canvas.restore();
+    // now layer count should be 0
+    REPORTER_ASSERT(reporter, !canvas.isDrawingToLayer());
+}
 
 static void TestCanvas(skiatest::Reporter* reporter) {
     SkBitmap bm;
@@ -31,6 +59,8 @@
     // should this pin to 1, or be a no-op, or crash?
     canvas.restoreToCount(0);
     REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
+
+    test_isDrawingToLayer(reporter);
 }
 
 #include "TestClassDef.h"