remove fClipStack and attach/deattach-from-canvas

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2180793002

Review-Url: https://codereview.chromium.org/2180793002
diff --git a/include/core/SkBitmapDevice.h b/include/core/SkBitmapDevice.h
index 85beb16..bb6d6ec 100644
--- a/include/core/SkBitmapDevice.h
+++ b/include/core/SkBitmapDevice.h
@@ -152,8 +152,6 @@
     bool onWritePixels(const SkImageInfo&, const void*, size_t, int, int) override;
     bool onPeekPixels(SkPixmap*) override;
     bool onAccessPixels(SkPixmap*) override;
-    void onAttachToCanvas(SkCanvas*) override;
-    void onDetachFromCanvas() override;
 
 private:
     friend class SkCanvas;
diff --git a/include/core/SkDevice.h b/include/core/SkDevice.h
index 4a2d656..f3b484e 100644
--- a/include/core/SkDevice.h
+++ b/include/core/SkDevice.h
@@ -112,32 +112,6 @@
      */
     const SkIPoint& getOrigin() const { return fOrigin; }
 
-    /**
-     * onAttachToCanvas is invoked whenever a device is installed in a canvas
-     * (i.e., setDevice, saveLayer (for the new device created by the save),
-     * and SkCanvas' SkBaseDevice & SkBitmap -taking ctors). It allows the
-     * devices to prepare for drawing (e.g., locking their pixels, etc.)
-     */
-    virtual void onAttachToCanvas(SkCanvas*) {
-        SkASSERT(!fAttachedToCanvas);
-#ifdef SK_DEBUG
-        fAttachedToCanvas = true;
-#endif
-    };
-
-    /**
-     * onDetachFromCanvas notifies a device that it will no longer be drawn to.
-     * It gives the device a chance to clean up (e.g., unlock its pixels). It
-     * is invoked from setDevice (for the displaced device), restore and
-     * possibly from SkCanvas' dtor.
-     */
-    virtual void onDetachFromCanvas() {
-        SkASSERT(fAttachedToCanvas);
-#ifdef SK_DEBUG
-        fAttachedToCanvas = false;
-#endif
-    };
-
 protected:
     enum TileUsage {
         kPossible_TileUsage,    //!< the created device may be drawn tiled
@@ -398,10 +372,6 @@
     SkBitmap    fLegacyBitmap;
 #endif
 
-#ifdef SK_DEBUG
-    bool        fAttachedToCanvas;
-#endif
-
     typedef SkRefCnt INHERITED;
 };
 
diff --git a/src/core/SkBitmapDevice.cpp b/src/core/SkBitmapDevice.cpp
index dcdc55b..4b25e21 100644
--- a/src/core/SkBitmapDevice.cpp
+++ b/src/core/SkBitmapDevice.cpp
@@ -70,8 +70,10 @@
 
 SkBitmapDevice::SkBitmapDevice(const SkBitmap& bitmap)
     : INHERITED(SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType))
-    , fBitmap(bitmap) {
+    , fBitmap(bitmap)
+{
     SkASSERT(valid_for_bitmap_device(bitmap.info(), nullptr));
+    fBitmap.lockPixels();
 }
 
 SkBitmapDevice* SkBitmapDevice::Create(const SkImageInfo& info) {
@@ -80,8 +82,10 @@
 
 SkBitmapDevice::SkBitmapDevice(const SkBitmap& bitmap, const SkSurfaceProps& surfaceProps)
     : INHERITED(surfaceProps)
-    , fBitmap(bitmap) {
+    , fBitmap(bitmap)
+{
     SkASSERT(valid_for_bitmap_device(bitmap.info(), nullptr));
+    fBitmap.lockPixels();
 }
 
 SkBitmapDevice* SkBitmapDevice::Create(const SkImageInfo& origInfo,
@@ -183,16 +187,6 @@
     return fBitmap.readPixels(dstInfo, dstPixels, dstRowBytes, x, y);
 }
 
-void SkBitmapDevice::onAttachToCanvas(SkCanvas* canvas) {
-    INHERITED::onAttachToCanvas(canvas);
-    fBitmap.lockPixels();
-}
-
-void SkBitmapDevice::onDetachFromCanvas() {
-    INHERITED::onDetachFromCanvas();
-    fBitmap.unlockPixels();
-}
-
 ///////////////////////////////////////////////////////////////////////////////
 
 void SkBitmapDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
diff --git a/src/core/SkCanvas.cpp b/src/core/SkCanvas.cpp
index 2cfe8ae..ffdcf9d 100644
--- a/src/core/SkCanvas.cpp
+++ b/src/core/SkCanvas.cpp
@@ -207,19 +207,13 @@
         , fClip(conservativeRasterClip)
         , fStashedMatrix(stashed)
     {
-        if (nullptr != device) {
-            device->ref();
-            device->onAttachToCanvas(canvas);
-        }
+        SkSafeRef(device);
         fDevice = device;
         fPaint = paint ? new SkPaint(*paint) : nullptr;
     }
 
     ~DeviceCM() {
-        if (fDevice) {
-            fDevice->onDetachFromCanvas();
-            fDevice->unref();
-        }
+        SkSafeUnref(fDevice);
         delete fPaint;
     }
 
@@ -684,7 +678,6 @@
     if (device) {
         // The root device and the canvas should always have the same pixel geometry
         SkASSERT(fProps.pixelGeometry() == device->surfaceProps().pixelGeometry());
-        device->onAttachToCanvas(this);
         fMCRec->fLayer->fDevice = SkRef(device);
         fMCRec->fRasterClip.setRect(device->getGlobalBounds());
     }
diff --git a/src/core/SkDevice.cpp b/src/core/SkDevice.cpp
index 75d481e..14250ab 100644
--- a/src/core/SkDevice.cpp
+++ b/src/core/SkDevice.cpp
@@ -23,12 +23,7 @@
 #include "SkTextBlobRunIterator.h"
 #include "SkTextToPathIter.h"
 
-SkBaseDevice::SkBaseDevice(const SkSurfaceProps& surfaceProps)
-    : fSurfaceProps(surfaceProps)
-#ifdef SK_DEBUG
-    , fAttachedToCanvas(false)
-#endif
-{
+SkBaseDevice::SkBaseDevice(const SkSurfaceProps& surfaceProps) : fSurfaceProps(surfaceProps) {
     fOrigin.setZero();
     fMetaData = nullptr;
 }
diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp
index 88f5847..602396f 100644
--- a/src/gpu/SkGpuDevice.cpp
+++ b/src/gpu/SkGpuDevice.cpp
@@ -287,30 +287,12 @@
     return false;
 }
 
-void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
-    ASSERT_SINGLE_OWNER
-    INHERITED::onAttachToCanvas(canvas);
-
-    // Canvas promises that this ptr is valid until onDetachFromCanvas is called
-    fClipStack.reset(SkRef(canvas->getClipStack()));
-}
-
-void SkGpuDevice::onDetachFromCanvas() {
-    ASSERT_SINGLE_OWNER
-    INHERITED::onDetachFromCanvas();
-    fClip.reset();
-    fClipStack.reset(nullptr);
-}
-
 // call this every draw call, to ensure that the context reflects our state,
 // and not the state from some other canvas/device
 void SkGpuDevice::prepareDraw(const SkDraw& draw) {
     ASSERT_SINGLE_OWNER
-    SkASSERT(fClipStack.get());
 
-    SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
-
-    fClip.reset(fClipStack, &this->getOrigin());
+    fClip.reset(draw.fClipStack, &this->getOrigin());
 }
 
 GrDrawContext* SkGpuDevice::accessDrawContext() {
diff --git a/src/gpu/SkGpuDevice.h b/src/gpu/SkGpuDevice.h
index 7e646a1..4cfd024 100644
--- a/src/gpu/SkGpuDevice.h
+++ b/src/gpu/SkGpuDevice.h
@@ -146,9 +146,6 @@
 
     void flush() override;
 
-    void onAttachToCanvas(SkCanvas* canvas) override;
-    void onDetachFromCanvas() override;
-
     bool onAccessPixels(SkPixmap*) override;
 
     // for debugging purposes only
@@ -165,7 +162,6 @@
     sk_sp<GrRenderTarget>           fRenderTarget;
     sk_sp<GrDrawContext>            fDrawContext;
 
-    SkAutoTUnref<const SkClipStack> fClipStack;
     SkIPoint                        fClipOrigin;
     GrClipStackClip                 fClip;
     SkISize                         fSize;
diff --git a/src/pdf/SkPDFDevice.cpp b/src/pdf/SkPDFDevice.cpp
index 8370eab..4181def 100644
--- a/src/pdf/SkPDFDevice.cpp
+++ b/src/pdf/SkPDFDevice.cpp
@@ -653,7 +653,6 @@
     , fPageSize(pageSize)
     , fContentSize(pageSize)
     , fExistingClipRegion(SkIRect::MakeSize(pageSize))
-    , fClipStack(nullptr)
     , fRasterDpi(rasterDpi)
     , fDocument(doc) {
     SkASSERT(pageSize.width() > 0);
@@ -1405,19 +1404,6 @@
     return info;
 }
 
-void SkPDFDevice::onAttachToCanvas(SkCanvas* canvas) {
-    INHERITED::onAttachToCanvas(canvas);
-
-    // Canvas promises that this ptr is valid until onDetachFromCanvas is called
-    fClipStack = canvas->getClipStack();
-}
-
-void SkPDFDevice::onDetachFromCanvas() {
-    INHERITED::onDetachFromCanvas();
-
-    fClipStack = nullptr;
-}
-
 sk_sp<SkSurface> SkPDFDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
     return SkSurface::MakeRaster(info, &props);
 }
diff --git a/src/pdf/SkPDFDevice.h b/src/pdf/SkPDFDevice.h
index f8497c6..6a167d9 100644
--- a/src/pdf/SkPDFDevice.h
+++ b/src/pdf/SkPDFDevice.h
@@ -119,8 +119,6 @@
     void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
                     const SkPaint&) override;
 
-    void onAttachToCanvas(SkCanvas* canvas) override;
-    void onDetachFromCanvas() override;
     SkImageInfo imageInfo() const override;
 
     // PDF specific methods.
@@ -256,8 +254,6 @@
     };
     SkSinglyLinkedList<ContentEntry> fContentEntries;
 
-    const SkClipStack* fClipStack;
-
     SkScalar fRasterDpi;
 
     SkPDFDocument* fDocument;