aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--include/core/SkCanvas.h6
-rw-r--r--src/core/SkCanvas.cpp11
-rw-r--r--tests/CanvasTest.cpp30
3 files changed, 47 insertions, 0 deletions
diff --git a/include/core/SkCanvas.h b/include/core/SkCanvas.h
index 08bd0f027d..24e4141442 100644
--- a/include/core/SkCanvas.h
+++ b/include/core/SkCanvas.h
@@ -287,6 +287,11 @@ public:
*/
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 @@ private:
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 f382893f8c..353c92c6f0 100644
--- a/src/core/SkCanvas.cpp
+++ b/src/core/SkCanvas.cpp
@@ -404,6 +404,7 @@ SkDevice* SkCanvas::init(SkDevice* device) {
fLocalBoundsCompareTypeDirtyBW = true;
fLastDeviceToGainFocus = NULL;
fDeviceCMDirty = false;
+ fLayerCount = 0;
fMCRec = (MCRec*)fMCStack.push_back();
new (fMCRec) MCRec(NULL, 0);
@@ -443,6 +444,8 @@ SkCanvas::SkCanvas(const SkBitmap& bitmap)
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 @@ int SkCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
fMCRec->fLayer = layer;
fMCRec->fTopLayer = layer; // this field is NOT an owner of layer
+ fLayerCount += 1;
return count;
}
@@ -797,6 +801,9 @@ void SkCanvas::internalRestore() {
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 @@ void SkCanvas::restoreToCount(int count) {
}
}
+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 7128a2267b..da1fafd394 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 @@ static void TestCanvas(skiatest::Reporter* reporter) {
// 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"