Remove SkCanvas::isDrawingToLayer

BUG=3245

Review URL: https://codereview.chromium.org/803913005
diff --git a/experimental/PdfViewer/SkNulCanvas.h b/experimental/PdfViewer/SkNulCanvas.h
index c895863..20ac79c 100644
--- a/experimental/PdfViewer/SkNulCanvas.h
+++ b/experimental/PdfViewer/SkNulCanvas.h
@@ -26,7 +26,6 @@
     explicit SkNulCanvas(const SkBitmap& bitmap) : SkCanvas(bitmap) {}
     virtual ~SkNulCanvas() {}
 
-    virtual bool isDrawingToLayer() const SK_OVERRIDE {return false;}
     virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE {}
     virtual void drawPoints(PointMode mode, size_t count, const SkPoint pts[],
                             const SkPaint& paint) SK_OVERRIDE {}
diff --git a/include/core/SkCanvas.h b/include/core/SkCanvas.h
index 50f15aa..6477b30 100644
--- a/include/core/SkCanvas.h
+++ b/include/core/SkCanvas.h
@@ -398,11 +398,6 @@
     */
     void restoreToCount(int saveCount);
 
-    /** Returns true if drawing is currently going to a layer (from saveLayer)
-     *  rather than to the root device.
-     */
-    virtual 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
@@ -1250,7 +1245,6 @@
     const SkSurfaceProps fProps;
 
     int         fSaveCount;         // value returned by getSaveCount()
-    int         fSaveLayerCount;    // number of successful saveLayer calls
 
     SkMetaData* fMetaData;
 
diff --git a/include/utils/SkDeferredCanvas.h b/include/utils/SkDeferredCanvas.h
index e128707..b9b85e6 100644
--- a/include/utils/SkDeferredCanvas.h
+++ b/include/utils/SkDeferredCanvas.h
@@ -144,7 +144,6 @@
     void silentFlush();
 
     // Overrides of the SkCanvas interface
-    virtual bool isDrawingToLayer() const SK_OVERRIDE;
     virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
     virtual void drawPoints(PointMode mode, size_t count, const SkPoint pts[],
                             const SkPaint& paint) SK_OVERRIDE;
@@ -248,6 +247,9 @@
     void validate() const;
     void init();
 
+
+    int fSaveLevel;
+    int fFirstSaveLayerIndex;
     size_t fBitmapSizeThreshold;
     bool   fDeferredDrawing;
 
diff --git a/src/core/SkCanvas.cpp b/src/core/SkCanvas.cpp
index a72b58c..751fabd 100644
--- a/src/core/SkCanvas.cpp
+++ b/src/core/SkCanvas.cpp
@@ -416,7 +416,6 @@
     fAllowSimplifyClip = false;
     fDeviceCMDirty = true;
     fSaveCount = 1;
-    fSaveLayerCount = 0;
     fMetaData = NULL;
 
     fMCRec = (MCRec*)fMCStack.push_back();
@@ -527,7 +526,6 @@
 SkCanvas::~SkCanvas() {
     // free up the contents of our deque
     this->restoreToCount(1);    // restore everything but the last
-    SkASSERT(0 == fSaveLayerCount);
 
     this->internalRestore();    // restore the last, since we're going away
 
@@ -990,8 +988,6 @@
     layer->fNext = fMCRec->fTopLayer;
     fMCRec->fLayer = layer;
     fMCRec->fTopLayer = layer;    // this field is NOT an owner of layer
-
-    fSaveLayerCount += 1;
 }
 
 int SkCanvas::saveLayerAlpha(const SkRect* bounds, U8CPU alpha) {
@@ -1038,18 +1034,11 @@
                                      layer->fPaint);
             // reset this, since internalDrawDevice will have set it to true
             fDeviceCMDirty = true;
-
-            SkASSERT(fSaveLayerCount > 0);
-            fSaveLayerCount -= 1;
         }
         SkDELETE(layer);
     }
 }
 
-bool SkCanvas::isDrawingToLayer() const {
-    return fSaveLayerCount > 0;
-}
-
 SkSurface* SkCanvas::newSurface(const SkImageInfo& info, const SkSurfaceProps* props) {
     if (NULL == props) {
         props = &fProps;
diff --git a/src/core/SkPictureRecord.cpp b/src/core/SkPictureRecord.cpp
index eec6bc0..6448d1e 100644
--- a/src/core/SkPictureRecord.cpp
+++ b/src/core/SkPictureRecord.cpp
@@ -31,7 +31,6 @@
 
 SkPictureRecord::SkPictureRecord(const SkISize& dimensions, uint32_t flags)
     : INHERITED(dimensions.width(), dimensions.height())
-    , fFirstSavedLayerIndex(kNoSavedLayerIndex)
     , fRecordFlags(flags)
     , fInitialSaveCount(kNoInitialSave) {
 }
@@ -151,9 +150,6 @@
     // from a clip entry.
     fRestoreOffsetStack.push(-(int32_t)fWriter.bytesWritten());
     this->recordSaveLayer(bounds, paint, flags);
-    if (kNoSavedLayerIndex == fFirstSavedLayerIndex) {
-        fFirstSavedLayerIndex = fRestoreOffsetStack.count();
-    }
 
     this->INHERITED::willSaveLayer(bounds, paint, flags);
     /*  No need for a (potentially very big) layer which we don't actually need
@@ -187,10 +183,6 @@
     this->validate(initialOffset, size);
 }
 
-bool SkPictureRecord::isDrawingToLayer() const {
-    return fFirstSavedLayerIndex != kNoSavedLayerIndex;
-}
-
 #ifdef SK_DEBUG
 /*
  * Read the op code from 'offset' in 'writer' and extract the size too.
@@ -221,10 +213,6 @@
         return;
     }
 
-    if (fRestoreOffsetStack.count() == fFirstSavedLayerIndex) {
-        fFirstSavedLayerIndex = kNoSavedLayerIndex;
-    }
-
     this->recordRestore();
 
     fRestoreOffsetStack.pop();
diff --git a/src/core/SkPictureRecord.h b/src/core/SkPictureRecord.h
index facf30d..d1a9e92 100644
--- a/src/core/SkPictureRecord.h
+++ b/src/core/SkPictureRecord.h
@@ -54,7 +54,6 @@
     virtual void beginCommentGroup(const char* description) SK_OVERRIDE;
     virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
     virtual void endCommentGroup() SK_OVERRIDE;
-    virtual bool isDrawingToLayer() const SK_OVERRIDE;
 
     const SkTDArray<const SkPicture* >& getPictureRefs() const {
         return fPictureRefs;
@@ -102,10 +101,6 @@
     void fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset);
 
     SkTDArray<int32_t> fRestoreOffsetStack;
-    int fFirstSavedLayerIndex;
-    enum {
-        kNoSavedLayerIndex = -1
-    };
 
     SkTDArray<uint32_t> fCullOffsetStack;
 
diff --git a/src/core/SkRecorder.cpp b/src/core/SkRecorder.cpp
index 71a1167..7d5d252 100644
--- a/src/core/SkRecorder.cpp
+++ b/src/core/SkRecorder.cpp
@@ -33,13 +33,11 @@
 
 SkRecorder::SkRecorder(SkRecord* record, int width, int height)
     : SkCanvas(SkIRect::MakeWH(width, height), SkCanvas::kConservativeRasterClip_InitFlag)
-    , fRecord(record)
-    , fSaveLayerCount(0) {}
+    , fRecord(record) {}
 
 SkRecorder::SkRecorder(SkRecord* record, const SkRect& bounds)
     : SkCanvas(bounds.roundOut(), SkCanvas::kConservativeRasterClip_InitFlag)
-    , fRecord(record)
-    , fSaveLayerCount(0) {}
+    , fRecord(record) {}
 
 void SkRecorder::forgetRecord() {
     fDrawableList.reset(NULL);
@@ -273,25 +271,17 @@
 }
 
 void SkRecorder::willSave() {
-    fSaveIsSaveLayer.push(false);
     APPEND(Save);
 }
 
 SkCanvas::SaveLayerStrategy SkRecorder::willSaveLayer(const SkRect* bounds,
                                                       const SkPaint* paint,
                                                       SkCanvas::SaveFlags flags) {
-    fSaveLayerCount++;
-    fSaveIsSaveLayer.push(true);
     APPEND(SaveLayer, this->copy(bounds), this->copy(paint), flags);
     return SkCanvas::kNoLayer_SaveLayerStrategy;
 }
 
 void SkRecorder::didRestore() {
-    SkBool8 saveLayer;
-    fSaveIsSaveLayer.pop(&saveLayer);
-    if (saveLayer) {
-        fSaveLayerCount--;
-    }
     APPEND(Restore, this->devBounds(), this->getTotalMatrix());
 }
 
@@ -343,10 +333,6 @@
     APPEND(EndCommentGroup);
 }
 
-bool SkRecorder::isDrawingToLayer() const {
-    return fSaveLayerCount > 0;
-}
-
 void SkRecorder::drawData(const void* data, size_t length) {
     APPEND(DrawData, copy((const char*)data), length);
 }
diff --git a/src/core/SkRecorder.h b/src/core/SkRecorder.h
index 5d80ac1..6f37ff3 100644
--- a/src/core/SkRecorder.h
+++ b/src/core/SkRecorder.h
@@ -135,7 +135,6 @@
     void endCommentGroup() SK_OVERRIDE;
     void drawData(const void*, size_t) SK_OVERRIDE;
 
-    bool isDrawingToLayer() const SK_OVERRIDE;
     SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) SK_OVERRIDE { return NULL; }
 
 private:
@@ -153,8 +152,6 @@
 
     SkRecord* fRecord;
 
-    int fSaveLayerCount;
-    SkTDArray<SkBool8> fSaveIsSaveLayer;
     SkAutoTDelete<SkCanvasDrawableList> fDrawableList;
 };
 
diff --git a/src/pipe/SkGPipeWrite.cpp b/src/pipe/SkGPipeWrite.cpp
index 9b73c63..9f56f6f 100644
--- a/src/pipe/SkGPipeWrite.cpp
+++ b/src/pipe/SkGPipeWrite.cpp
@@ -232,7 +232,6 @@
     }
 
     // overrides from SkCanvas
-    virtual bool isDrawingToLayer() const SK_OVERRIDE;
     virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
     virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
                             const SkPaint&) SK_OVERRIDE;
@@ -299,11 +298,7 @@
     void recordScale(const SkMatrix&);
     void recordConcat(const SkMatrix&);
 
-    enum {
-        kNoSaveLayer = -1,
-    };
     SkNamedFactorySet* fFactorySet;
-    int                fFirstSaveLayerStackLevel;
     SkBitmapHeap*      fBitmapHeap;
     SkGPipeController* fController;
     SkWriter32&        fWriter;
@@ -448,7 +443,6 @@
     fDone = false;
     fBlockSize = 0; // need first block from controller
     fBytesNotified = 0;
-    fFirstSaveLayerStackLevel = kNoSaveLayer;
     sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
 
     // Tell the reader the appropriate flags to use.
@@ -556,10 +550,6 @@
         }
     }
 
-    if (kNoSaveLayer == fFirstSaveLayerStackLevel){
-        fFirstSaveLayerStackLevel = this->getSaveCount();
-    }
-
     this->INHERITED::willSaveLayer(bounds, paint, saveFlags);
     // we don't create a layer
     return kNoLayer_SaveLayerStrategy;
@@ -571,17 +561,9 @@
         this->writeOp(kRestore_DrawOp);
     }
 
-    if (this->getSaveCount() - 1 == fFirstSaveLayerStackLevel){
-        fFirstSaveLayerStackLevel = kNoSaveLayer;
-    }
-
     this->INHERITED::willRestore();
 }
 
-bool SkGPipeCanvas::isDrawingToLayer() const {
-    return kNoSaveLayer != fFirstSaveLayerStackLevel;
-}
-
 void SkGPipeCanvas::recordTranslate(const SkMatrix& m) {
     if (this->needOpBytes(2 * sizeof(SkScalar))) {
         this->writeOp(kTranslate_DrawOp);
diff --git a/src/utils/SkDeferredCanvas.cpp b/src/utils/SkDeferredCanvas.cpp
index 94b3694..5083cf7 100644
--- a/src/utils/SkDeferredCanvas.cpp
+++ b/src/utils/SkDeferredCanvas.cpp
@@ -23,6 +23,8 @@
     // Deferred canvas will auto-flush when recording reaches this limit
     kDefaultMaxRecordingStorageBytes = 64*1024*1024,
     kDeferredCanvasBitmapSizeThreshold = ~0U, // Disables this feature
+
+    kNoSaveLayerIndex = -1,
 };
 
 enum PlaybackMode {
@@ -154,6 +156,7 @@
     void skipPendingCommands();
     void setMaxRecordingStorage(size_t);
     void recordedDrawCommand();
+    void setIsDrawingToLayer(bool value) {fIsDrawingToLayer = value;}
 
     virtual SkImageInfo imageInfo() const SK_OVERRIDE;
 
@@ -256,6 +259,7 @@
     SkDeferredCanvas::NotificationClient* fNotificationClient;
     bool fFreshFrame;
     bool fCanDiscardCanvasContents;
+    bool fIsDrawingToLayer;
     size_t fMaxRecordingStorageBytes;
     size_t fPreviousStorageAllocated;
 };
@@ -278,6 +282,7 @@
 void SkDeferredDevice::init() {
     fRecordingCanvas = NULL;
     fFreshFrame = true;
+    fIsDrawingToLayer = false;
     fCanDiscardCanvasContents = false;
     fPreviousStorageAllocated = 0;
     fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
@@ -308,7 +313,7 @@
 }
 
 void SkDeferredDevice::skipPendingCommands() {
-    if (!fRecordingCanvas->isDrawingToLayer()) {
+    if (!fIsDrawingToLayer) {
         fCanDiscardCanvasContents = true;
         if (fPipeController.hasPendingCommands()) {
             fFreshFrame = true;
@@ -522,6 +527,8 @@
     fDeferredDrawing = true; // On by default
     fCachedCanvasSize.setEmpty();
     fCachedCanvasSizeDirty = true;
+    fSaveLevel = 0;
+    fFirstSaveLayerIndex = kNoSaveLayerIndex;
 }
 
 void SkDeferredCanvas::setMaxRecordingStorage(size_t maxStorage) {
@@ -673,6 +680,7 @@
 }
 
 void SkDeferredCanvas::willSave() {
+    fSaveLevel++;
     this->drawingCanvas()->save();
     this->recordedDrawCommand();
     this->INHERITED::willSave();
@@ -680,6 +688,11 @@
 
 SkCanvas::SaveLayerStrategy SkDeferredCanvas::willSaveLayer(const SkRect* bounds,
                                                             const SkPaint* paint, SaveFlags flags) {
+    fSaveLevel++;
+    if (fFirstSaveLayerIndex == kNoSaveLayerIndex) {
+        fFirstSaveLayerIndex = fSaveLevel;
+        this->getDeferredDevice()->setIsDrawingToLayer(true);
+    }
     this->drawingCanvas()->saveLayer(bounds, paint, flags);
     this->recordedDrawCommand();
     this->INHERITED::willSaveLayer(bounds, paint, flags);
@@ -688,15 +701,17 @@
 }
 
 void SkDeferredCanvas::willRestore() {
+    SkASSERT(fFirstSaveLayerIndex == kNoSaveLayerIndex || fFirstSaveLayerIndex <= fSaveLevel);
+    if (fFirstSaveLayerIndex == fSaveLevel) {
+        fFirstSaveLayerIndex = kNoSaveLayerIndex;
+        this->getDeferredDevice()->setIsDrawingToLayer(false);
+    }
+    fSaveLevel--;
     this->drawingCanvas()->restore();
     this->recordedDrawCommand();
     this->INHERITED::willRestore();
 }
 
-bool SkDeferredCanvas::isDrawingToLayer() const {
-    return this->drawingCanvas()->isDrawingToLayer();
-}
-
 void SkDeferredCanvas::didConcat(const SkMatrix& matrix) {
     this->drawingCanvas()->concat(matrix);
     this->recordedDrawCommand();
diff --git a/tests/CanvasTest.cpp b/tests/CanvasTest.cpp
index 1f8f276..b55f833 100644
--- a/tests/CanvasTest.cpp
+++ b/tests/CanvasTest.cpp
@@ -471,39 +471,6 @@
 }
 TEST_STEP(SaveRestore, SaveRestoreTestStep);
 
-static void DrawLayerTestStep(SkCanvas* canvas, const TestData& d,
-                              skiatest::Reporter* reporter, CanvasTestStep* testStep) {
-    REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
-        testStep->assertMessage());
-    canvas->save();
-    REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
-        testStep->assertMessage());
-    canvas->restore();
-
-    const SkRect* bounds = NULL;    // null means include entire bounds
-    const SkPaint* paint = NULL;
-
-    canvas->saveLayer(bounds, paint);
-    REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
-        testStep->assertMessage());
-    canvas->restore();
-    REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
-        testStep->assertMessage());
-
-    canvas->saveLayer(bounds, paint);
-    canvas->saveLayer(bounds, paint);
-    REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
-        testStep->assertMessage());
-    canvas->restore();
-    REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
-        testStep->assertMessage());
-    canvas->restore();
-    // now layer count should be 0
-    REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
-        testStep->assertMessage());
-}
-TEST_STEP(DrawLayer, DrawLayerTestStep);
-
 static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas, const TestData& d,
                                                     skiatest::Reporter*, CanvasTestStep*) {
     // This test step challenges the TestDeferredCanvasStateConsistency
@@ -551,8 +518,6 @@
         canvas2->getDeviceSize(), testStep->assertMessage());
     REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
         canvas2->getSaveCount(), testStep->assertMessage());
-    REPORTER_ASSERT_MESSAGE(reporter, canvas1->isDrawingToLayer() ==
-        canvas2->isDrawingToLayer(), testStep->assertMessage());
 
     SkRect bounds1, bounds2;
     REPORTER_ASSERT_MESSAGE(reporter,
diff --git a/tests/RecorderTest.cpp b/tests/RecorderTest.cpp
index 7a27556..e0abace 100644
--- a/tests/RecorderTest.cpp
+++ b/tests/RecorderTest.cpp
@@ -125,32 +125,6 @@
     REPORTER_ASSERT(r, pic->unique());
 }
 
-DEF_TEST(Recorder_IsDrawingToLayer, r) {
-    SkRecord record;
-    SkRecorder recorder(&record, 100, 100);
-
-    // We'll save, saveLayer, save, and saveLayer, then restore them all,
-    // checking that isDrawingToLayer() is correct at each step.
-
-    REPORTER_ASSERT(r, !recorder.isDrawingToLayer());
-    recorder.save();
-        REPORTER_ASSERT(r, !recorder.isDrawingToLayer());
-        recorder.saveLayer(NULL, NULL);
-            REPORTER_ASSERT(r, recorder.isDrawingToLayer());
-            recorder.save();
-                REPORTER_ASSERT(r, recorder.isDrawingToLayer());
-                recorder.saveLayer(NULL, NULL);
-                    REPORTER_ASSERT(r, recorder.isDrawingToLayer());
-                recorder.restore();
-                REPORTER_ASSERT(r, recorder.isDrawingToLayer());
-            recorder.restore();
-            REPORTER_ASSERT(r, recorder.isDrawingToLayer());
-        recorder.restore();
-        REPORTER_ASSERT(r, !recorder.isDrawingToLayer());
-    recorder.restore();
-    REPORTER_ASSERT(r, !recorder.isDrawingToLayer());
-}
-
 DEF_TEST(Recorder_drawImage_takeReference, reporter) {
 
     SkAutoTUnref<SkImage> image;