De-virtualize SkCanvas save/restore.
authorcommit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Wed, 12 Mar 2014 20:21:48 +0000 (20:21 +0000)
committercommit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Wed, 12 Mar 2014 20:21:48 +0000 (20:21 +0000)
This moves the state management logic into non-virtual SkCanvas methods,
and turns the virtuals into protected notifiers.

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

Author: fmalita@chromium.org

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

git-svn-id: http://skia.googlecode.com/svn/trunk@13776 2bbb7eff-a529-9590-31e7-b0007b416f81

21 files changed:
experimental/PdfViewer/SkNulCanvas.h
include/core/SkCanvas.h
include/utils/SkDeferredCanvas.h
include/utils/SkDumpCanvas.h
include/utils/SkLuaCanvas.h
include/utils/SkNWayCanvas.h
include/utils/SkProxyCanvas.h
src/core/SkBBoxHierarchyRecord.cpp
src/core/SkBBoxHierarchyRecord.h
src/core/SkCanvas.cpp
src/core/SkPictureRecord.cpp
src/core/SkPictureRecord.h
src/pipe/SkGPipeWrite.cpp
src/utils/SkDeferredCanvas.cpp
src/utils/SkDumpCanvas.cpp
src/utils/SkLuaCanvas.cpp
src/utils/SkNWayCanvas.cpp
src/utils/SkNoSaveLayerCanvas.h
src/utils/SkProxyCanvas.cpp
src/utils/debugger/SkDebugCanvas.cpp
src/utils/debugger/SkDebugCanvas.h

index 1c12a5b..472ea3b 100644 (file)
@@ -26,13 +26,6 @@ public:
     explicit SkNulCanvas(const SkBitmap& bitmap) : SkCanvas(bitmap) {}
     virtual ~SkNulCanvas() {}
 
-    virtual int save(SaveFlags flags = kMatrixClip_SaveFlag) SK_OVERRIDE {return 0;}
-    virtual int saveLayer(const SkRect* bounds, const SkPaint* paint,
-                          SaveFlags flags = kARGB_ClipLayer_SaveFlag) SK_OVERRIDE {return 0;}
-    int saveLayerAlpha(const SkRect* bounds, U8CPU alpha,
-                       SaveFlags flags = kARGB_ClipLayer_SaveFlag) {return 0;}
-    virtual void restore() SK_OVERRIDE {}
-    int getSaveCount() const {return 0;}
     virtual bool isDrawingToLayer() const SK_OVERRIDE {return false;}
     virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE {return true;}
     virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE {return true;}
@@ -106,6 +99,12 @@ protected:
     virtual SkCanvas* canvasForDrawIter() {return NULL;}
     virtual SkBaseDevice* setDevice(SkBaseDevice* device) {return NULL;}
 
+    virtual SaveLayerStrategy willSaveLayer(const SkRect* bounds, const SkPaint* paint,
+                                            SaveFlags flags) SK_OVERRIDE {
+        this->INHERITED::willSaveLayer(bounds, paint, flags);
+        return kNoLayer_SaveLayerStrategy;
+    }
+
     virtual void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE {}
     virtual void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE {}
     virtual void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE {}
index 5ac137d..bcfba16 100644 (file)
@@ -378,7 +378,7 @@ public:
                      by calls to save/restore.
         @return The value to pass to restoreToCount() to balance this save()
     */
-    virtual int save(SaveFlags flags = kMatrixClip_SaveFlag);
+    int save(SaveFlags flags = kMatrixClip_SaveFlag);
 
     /** This behaves the same as save(), but in addition it allocates an
         offscreen bitmap. All drawing calls are directed there, and only when
@@ -393,8 +393,8 @@ public:
         @param flags  LayerFlags
         @return The value to pass to restoreToCount() to balance this save()
     */
-    virtual int saveLayer(const SkRect* bounds, const SkPaint* paint,
-                          SaveFlags flags = kARGB_ClipLayer_SaveFlag);
+    int saveLayer(const SkRect* bounds, const SkPaint* paint,
+                  SaveFlags flags = kARGB_ClipLayer_SaveFlag);
 
     /** This behaves the same as save(), but in addition it allocates an
         offscreen bitmap. All drawing calls are directed there, and only when
@@ -416,7 +416,7 @@ public:
         call.
         It is an error to call restore() more times than save() was called.
     */
-    virtual void restore();
+    void restore();
 
     /** Returns the number of matrix/clip states on the SkCanvas' private stack.
         This will equal # save() calls - # restore() calls + 1. The save count on
@@ -1201,6 +1201,17 @@ protected:
     virtual const void* onPeekPixels(SkImageInfo*, size_t* rowBytes);
     virtual void* onAccessTopLayerPixels(SkImageInfo*, size_t* rowBytes);
 
+    // Subclass save/restore notifiers.
+    // Overriders should call the corresponding INHERITED method up the inheritance chain.
+    // willSaveLayer()'s return value may suppress full layer allocation.
+    enum SaveLayerStrategy {
+        kFullLayer_SaveLayerStrategy,
+        kNoLayer_SaveLayerStrategy
+    };
+    virtual void willSave(SaveFlags);
+    virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags);
+    virtual void willRestore();
+
     virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&);
 
     enum ClipEdgeStyle {
@@ -1301,7 +1312,7 @@ private:
                                 const SkRect& dst, const SkPaint* paint);
     void internalDrawPaint(const SkPaint& paint);
     int internalSaveLayer(const SkRect* bounds, const SkPaint* paint,
-                          SaveFlags, bool justForImageFilter);
+                          SaveFlags, bool justForImageFilter, SaveLayerStrategy strategy);
     void internalDrawDevice(SkBaseDevice*, int x, int y, const SkPaint*);
 
     // shared by save() and saveLayer()
index e82cad3..7d8b58c 100644 (file)
@@ -138,10 +138,6 @@ public:
     void silentFlush();
 
     // Overrides of the SkCanvas interface
-    virtual int save(SaveFlags flags) SK_OVERRIDE;
-    virtual int saveLayer(const SkRect* bounds, const SkPaint* paint,
-                          SaveFlags flags) SK_OVERRIDE;
-    virtual void restore() SK_OVERRIDE;
     virtual bool isDrawingToLayer() const SK_OVERRIDE;
     virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
     virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
@@ -193,6 +189,10 @@ public:
     virtual SkDrawFilter* setDrawFilter(SkDrawFilter* filter) SK_OVERRIDE;
 
 protected:
+    virtual void willSave(SaveFlags) SK_OVERRIDE;
+    virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE;
+    virtual void willRestore() SK_OVERRIDE;
+
     virtual void onDrawDRRect(const SkRRect&, const SkRRect&,
                               const SkPaint&) SK_OVERRIDE;
 
index 5e3249c..b08b2d0 100644 (file)
@@ -74,11 +74,6 @@ public:
 
     int getNestLevel() const { return fNestLevel; }
 
-    virtual int save(SaveFlags) SK_OVERRIDE;
-    virtual int saveLayer(const SkRect* bounds, const SkPaint* paint,
-                          SaveFlags) SK_OVERRIDE;
-    virtual void restore() SK_OVERRIDE;
-
     virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
     virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
     virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
@@ -124,6 +119,10 @@ public:
     virtual void endCommentGroup() SK_OVERRIDE;
 
 protected:
+    virtual void willSave(SaveFlags) SK_OVERRIDE;
+    virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE;
+    virtual void willRestore() SK_OVERRIDE;
+
     virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
     virtual void onPushCull(const SkRect& cullRect) SK_OVERRIDE;
     virtual void onPopCull() SK_OVERRIDE;
index a4f30bc..9bc109e 100644 (file)
@@ -20,11 +20,6 @@ public:
     SkLuaCanvas(int width, int height, lua_State*, const char function[]);
     virtual ~SkLuaCanvas();
 
-    virtual int save(SaveFlags flags) SK_OVERRIDE;
-    virtual int saveLayer(const SkRect* bounds, const SkPaint* paint,
-                          SaveFlags flags) SK_OVERRIDE;
-    virtual void restore() SK_OVERRIDE;
-
     virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
     virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
     virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
@@ -67,6 +62,10 @@ public:
     virtual void drawData(const void* data, size_t length) SK_OVERRIDE;
 
 protected:
+    virtual void willSave(SaveFlags) SK_OVERRIDE;
+    virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE;
+    virtual void willRestore() SK_OVERRIDE;
+
     virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
 
     virtual void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
index 84f4b4a..69ad713 100644 (file)
@@ -23,10 +23,6 @@ public:
     ///////////////////////////////////////////////////////////////////////////
     // These are forwarded to the N canvases we're referencing
 
-    virtual int save(SaveFlags) SK_OVERRIDE;
-    virtual int saveLayer(const SkRect* bounds, const SkPaint*,
-                          SaveFlags) SK_OVERRIDE;
-    virtual void restore() SK_OVERRIDE;
     virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
     virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
     virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
@@ -81,6 +77,10 @@ public:
 protected:
     SkTDArray<SkCanvas*> fList;
 
+    virtual void willSave(SaveFlags) SK_OVERRIDE;
+    virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE;
+    virtual void willRestore() SK_OVERRIDE;
+
     virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
 
     virtual void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
index d682ba3..6e2e8e0 100644 (file)
@@ -26,11 +26,6 @@ public:
     SkCanvas*   getProxy() const { return fProxy; }
     void        setProxy(SkCanvas* proxy);
 
-    virtual int save(SaveFlags flags = kMatrixClip_SaveFlag) SK_OVERRIDE;
-    virtual int saveLayer(const SkRect* bounds, const SkPaint* paint,
-                          SaveFlags flags = kARGB_ClipLayer_SaveFlag) SK_OVERRIDE;
-    virtual void restore() SK_OVERRIDE;
-
     virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
     virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
     virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
@@ -80,6 +75,10 @@ public:
     virtual SkDrawFilter* setDrawFilter(SkDrawFilter* filter) SK_OVERRIDE;
 
 protected:
+    virtual void willSave(SaveFlags) SK_OVERRIDE;
+    virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE;
+    virtual void willRestore() SK_OVERRIDE;
+
     virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
 
     virtual void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
index 5d310be..e1be2e1 100644 (file)
@@ -26,20 +26,21 @@ void SkBBoxHierarchyRecord::handleBBox(const SkRect& bounds) {
     fBoundingHierarchy->insert(draw, r, true);
 }
 
-int SkBBoxHierarchyRecord::save(SaveFlags flags) {
+void SkBBoxHierarchyRecord::willSave(SaveFlags flags) {
     fStateTree->appendSave();
-    return INHERITED::save(flags);
+    this->INHERITED::willSave(flags);
 }
 
-int SkBBoxHierarchyRecord::saveLayer(const SkRect* bounds, const SkPaint* paint,
-                                     SaveFlags flags) {
+SkCanvas::SaveLayerStrategy SkBBoxHierarchyRecord::willSaveLayer(const SkRect* bounds,
+                                                                 const SkPaint* paint,
+                                                                 SaveFlags flags) {
     fStateTree->appendSaveLayer(this->writeStream().bytesWritten());
-    return INHERITED::saveLayer(bounds, paint, flags);
+    return this->INHERITED::willSaveLayer(bounds, paint, flags);
 }
 
-void SkBBoxHierarchyRecord::restore() {
+void SkBBoxHierarchyRecord::willRestore() {
     fStateTree->appendRestore();
-    INHERITED::restore();
+    this->INHERITED::willRestore();
 }
 
 bool SkBBoxHierarchyRecord::translate(SkScalar dx, SkScalar dy) {
index 80f59c3..fdfc9f7 100644 (file)
@@ -23,11 +23,6 @@ public:
 
     virtual void handleBBox(const SkRect& bounds) SK_OVERRIDE;
 
-    virtual int save(SaveFlags flags = kMatrixClip_SaveFlag) SK_OVERRIDE;
-    virtual int saveLayer(const SkRect* bounds, const SkPaint* paint,
-                          SaveFlags flags = kARGB_ClipLayer_SaveFlag) SK_OVERRIDE;
-    virtual void restore() SK_OVERRIDE;
-
     virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
     virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
     virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
@@ -39,6 +34,10 @@ public:
     virtual bool shouldRewind(void* data) SK_OVERRIDE;
 
 protected:
+    virtual void willSave(SaveFlags) SK_OVERRIDE;
+    virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE;
+    virtual void willRestore() SK_OVERRIDE;
+
     virtual void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
     virtual void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
     virtual void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
index 4086291..6f33280 100644 (file)
@@ -347,8 +347,8 @@ public:
         if (!skipLayerForImageFilter && fOrigPaint.getImageFilter()) {
             SkPaint tmp;
             tmp.setImageFilter(fOrigPaint.getImageFilter());
-            (void)canvas->internalSaveLayer(bounds, &tmp,
-                                    SkCanvas::kARGB_ClipLayer_SaveFlag, true);
+            (void)canvas->internalSaveLayer(bounds, &tmp, SkCanvas::kARGB_ClipLayer_SaveFlag,
+                                            true, SkCanvas::kFullLayer_SaveLayerStrategy);
             // we'll clear the imageFilter for the actual draws in next(), so
             // it will only be applied during the restore().
             fDoClearImageFilter = true;
@@ -810,7 +810,12 @@ int SkCanvas::internalSave(SaveFlags flags) {
     return saveCount;
 }
 
+void SkCanvas::willSave(SaveFlags) {
+    // Do nothing. Subclasses may do something.
+}
+
 int SkCanvas::save(SaveFlags flags) {
+    this->willSave(flags);
     // call shared impl
     return this->internalSave(flags);
 }
@@ -867,9 +872,17 @@ bool SkCanvas::clipRectBounds(const SkRect* bounds, SaveFlags flags,
     return true;
 }
 
+SkCanvas::SaveLayerStrategy SkCanvas::willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) {
+
+    // Do nothing. Subclasses may do something.
+    return kFullLayer_SaveLayerStrategy;
+}
+
 int SkCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
                         SaveFlags flags) {
-    return this->internalSaveLayer(bounds, paint, flags, false);
+    // Overriding classes may return false to signal that we don't need to create a layer.
+    SaveLayerStrategy strategy = this->willSaveLayer(bounds, paint, flags);
+    return this->internalSaveLayer(bounds, paint, flags, false, strategy);
 }
 
 static SkBaseDevice* createCompatibleDevice(SkCanvas* canvas,
@@ -878,8 +891,8 @@ static SkBaseDevice* createCompatibleDevice(SkCanvas* canvas,
     return device ? device->createCompatibleDevice(info) : NULL;
 }
 
-int SkCanvas::internalSaveLayer(const SkRect* bounds, const SkPaint* paint,
-                                SaveFlags flags, bool justForImageFilter) {
+int SkCanvas::internalSaveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags,
+                                bool justForImageFilter, SaveLayerStrategy strategy) {
 #ifndef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
     flags = (SaveFlags)(flags | kClipToLayer_SaveFlag);
 #endif
@@ -895,6 +908,12 @@ int SkCanvas::internalSaveLayer(const SkRect* bounds, const SkPaint* paint,
         return count;
     }
 
+    // FIXME: do willSaveLayer() overriders returning kNoLayer_SaveLayerStrategy really care about
+    // the clipRectBounds() call above?
+    if (kNoLayer_SaveLayerStrategy == strategy) {
+        return count;
+    }
+
     // Kill the imagefilter if our device doesn't allow it
     SkLazyPaint lazyP;
     if (paint && paint->getImageFilter()) {
@@ -947,9 +966,14 @@ int SkCanvas::saveLayerAlpha(const SkRect* bounds, U8CPU alpha,
     }
 }
 
+void SkCanvas::willRestore() {
+    // Do nothing. Subclasses may do something.
+}
+
 void SkCanvas::restore() {
     // check for underflow
     if (fMCStack.count() > 1) {
+        this->willRestore();
         this->internalRestore();
     }
 }
index 08b69b8..a85ea8d 100644 (file)
@@ -146,7 +146,7 @@ static inline uint32_t getPaintOffset(DrawType op, uint32_t opSize) {
     return gPaintOffsets[op] * sizeof(uint32_t) + overflow;
 }
 
-int SkPictureRecord::save(SaveFlags flags) {
+void SkPictureRecord::willSave(SaveFlags flags) {
 
 #ifdef SK_COLLAPSE_MATRIX_CLIP_STATE
     fMCMgr.save(flags);
@@ -156,7 +156,8 @@ int SkPictureRecord::save(SaveFlags flags) {
     fRestoreOffsetStack.push(-(int32_t)fWriter.bytesWritten());
     this->recordSave(flags);
 #endif
-    return this->INHERITED::save(flags);
+
+    this->INHERITED::willSave(flags);
 }
 
 void SkPictureRecord::recordSave(SaveFlags flags) {
@@ -168,12 +169,11 @@ void SkPictureRecord::recordSave(SaveFlags flags) {
     this->validate(initialOffset, size);
 }
 
-int SkPictureRecord::saveLayer(const SkRect* bounds, const SkPaint* paint,
-                               SaveFlags flags) {
+SkCanvas::SaveLayerStrategy SkPictureRecord::willSaveLayer(const SkRect* bounds,
+                                                           const SkPaint* paint, SaveFlags flags) {
 
-    int count;
 #ifdef SK_COLLAPSE_MATRIX_CLIP_STATE
-    count = fMCMgr.saveLayer(bounds, paint, flags);
+    fMCMgr.saveLayer(bounds, paint, flags);
 #else
     // record the offset to us, making it non-positive to distinguish a save
     // from a clip entry.
@@ -184,15 +184,13 @@ int SkPictureRecord::saveLayer(const SkRect* bounds, const SkPaint* paint,
     }
 #endif
 
-    /*  Don't actually call INHERITED::saveLayer, because that will try to allocate
-        an offscreen device (potentially very big) which we don't actually need
+    this->INHERITED::willSaveLayer(bounds, paint, flags);
+    /*  No need for a (potentially very big) layer which we don't actually need
         at this time (and may not be able to afford since during record our
         clip starts out the size of the picture, which is often much larger
         than the size of the actual device we'll use during playback).
      */
-    count = this->INHERITED::save(flags);
-    this->clipRectBounds(bounds, flags, NULL);
-    return count;
+    return kNoLayer_SaveLayerStrategy;
 }
 
 void SkPictureRecord::recordSaveLayer(const SkRect* bounds, const SkPaint* paint,
@@ -605,7 +603,7 @@ static void apply_optimization_to_bbh(PictureRecordOptType opt, SkPictureStateTr
     }
 }
 
-void SkPictureRecord::restore() {
+void SkPictureRecord::willRestore() {
     // FIXME: SkDeferredCanvas needs to be refactored to respect
     // save/restore balancing so that the following test can be
     // turned on permanently.
@@ -653,7 +651,7 @@ void SkPictureRecord::restore() {
     fRestoreOffsetStack.pop();
 #endif
 
-    return this->INHERITED::restore();
+    this->INHERITED::willRestore();
 }
 
 void SkPictureRecord::recordRestore(bool fillInSkips) {
index e91c1c5..d1d6362 100644 (file)
@@ -37,9 +37,6 @@ public:
     SkPictureRecord(const SkISize& dimensions, uint32_t recordFlags);
     virtual ~SkPictureRecord();
 
-    virtual int save(SaveFlags) SK_OVERRIDE;
-    virtual int saveLayer(const SkRect* bounds, const SkPaint*, SaveFlags) SK_OVERRIDE;
-    virtual void restore() SK_OVERRIDE;
     virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
     virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
     virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
@@ -227,6 +224,11 @@ protected:
     const void* onPeekPixels(SkImageInfo*, size_t*) SK_OVERRIDE {
         return NULL;
     }
+
+    virtual void willSave(SaveFlags) SK_OVERRIDE;
+    virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE;
+    virtual void willRestore() SK_OVERRIDE;
+
     virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
     virtual void onPushCull(const SkRect&) SK_OVERRIDE;
     virtual void onPopCull() SK_OVERRIDE;
index 43209f6..7f2f1a1 100644 (file)
@@ -229,10 +229,6 @@ public:
     }
 
     // overrides from SkCanvas
-    virtual int save(SaveFlags) SK_OVERRIDE;
-    virtual int saveLayer(const SkRect* bounds, const SkPaint*,
-                          SaveFlags) SK_OVERRIDE;
-    virtual void restore() SK_OVERRIDE;
     virtual bool isDrawingToLayer() const SK_OVERRIDE;
     virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
     virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
@@ -287,6 +283,10 @@ public:
     bool shuttleBitmap(const SkBitmap&, int32_t slot);
 
 protected:
+    virtual void willSave(SaveFlags) SK_OVERRIDE;
+    virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE;
+    virtual void willRestore() SK_OVERRIDE;
+
     virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
 
     virtual void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
@@ -514,16 +514,17 @@ uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
 #define NOTIFY_SETUP(canvas)    \
     AutoPipeNotify apn(canvas)
 
-int SkGPipeCanvas::save(SaveFlags flags) {
+void SkGPipeCanvas::willSave(SaveFlags flags) {
     NOTIFY_SETUP(this);
     if (this->needOpBytes()) {
         this->writeOp(kSave_DrawOp, 0, flags);
     }
-    return this->INHERITED::save(flags);
+
+    this->INHERITED::willSave(flags);
 }
 
-int SkGPipeCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
-                             SaveFlags saveFlags) {
+SkCanvas::SaveLayerStrategy SkGPipeCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint,
+                                                         SaveFlags saveFlags) {
     NOTIFY_SETUP(this);
     size_t size = 0;
     unsigned opFlags = 0;
@@ -547,21 +548,23 @@ int SkGPipeCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
     if (kNoSaveLayer == fFirstSaveLayerStackLevel){
         fFirstSaveLayerStackLevel = this->getSaveCount();
     }
-    // we just pass on the save, so we don't create a layer
-    return this->INHERITED::save(saveFlags);
+
+    this->INHERITED::willSaveLayer(bounds, paint, saveFlags);
+    // we don't create a layer
+    return kNoLayer_SaveLayerStrategy;
 }
 
-void SkGPipeCanvas::restore() {
+void SkGPipeCanvas::willRestore() {
     NOTIFY_SETUP(this);
     if (this->needOpBytes()) {
         this->writeOp(kRestore_DrawOp);
     }
 
-    this->INHERITED::restore();
-
-    if (this->getSaveCount() == fFirstSaveLayerStackLevel){
+    if (this->getSaveCount() - 1 == fFirstSaveLayerStackLevel){
         fFirstSaveLayerStackLevel = kNoSaveLayer;
     }
+
+    this->INHERITED::willRestore();
 }
 
 bool SkGPipeCanvas::isDrawingToLayer() const {
index 31d62bb..6592c60 100644 (file)
@@ -739,28 +739,25 @@ bool SkDeferredCanvas::isFullFrame(const SkRect* rect,
         SkIntToScalar(canvasSize.fWidth), SkIntToScalar(canvasSize.fHeight)));
 }
 
-int SkDeferredCanvas::save(SaveFlags flags) {
+void SkDeferredCanvas::willSave(SaveFlags flags) {
     this->drawingCanvas()->save(flags);
-    int val = this->INHERITED::save(flags);
     this->recordedDrawCommand();
-
-    return val;
+    this->INHERITED::willSave(flags);
 }
 
-int SkDeferredCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
-                                SaveFlags flags) {
+SkCanvas::SaveLayerStrategy SkDeferredCanvas::willSaveLayer(const SkRect* bounds,
+                                                            const SkPaint* paint, SaveFlags flags) {
     this->drawingCanvas()->saveLayer(bounds, paint, flags);
-    int count = this->INHERITED::save(flags);
-    this->clipRectBounds(bounds, flags, NULL);
     this->recordedDrawCommand();
-
-    return count;
+    this->INHERITED::willSaveLayer(bounds, paint, flags);
+    // No need for a full layer.
+    return kNoLayer_SaveLayerStrategy;
 }
 
-void SkDeferredCanvas::restore() {
+void SkDeferredCanvas::willRestore() {
     this->drawingCanvas()->restore();
-    this->INHERITED::restore();
     this->recordedDrawCommand();
+    this->INHERITED::willRestore();
 }
 
 bool SkDeferredCanvas::isDrawingToLayer() const {
index 4805d62..af59cd6 100644 (file)
@@ -192,13 +192,13 @@ void SkDumpCanvas::dump(Verb verb, const SkPaint* paint,
 
 ///////////////////////////////////////////////////////////////////////////////
 
-int SkDumpCanvas::save(SaveFlags flags) {
+void SkDumpCanvas::willSave(SaveFlags flags) {
     this->dump(kSave_Verb, NULL, "save(0x%X)", flags);
-    return this->INHERITED::save(flags);
+    this->INHERITED::willSave(flags);
 }
 
-int SkDumpCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
-                             SaveFlags flags) {
+SkCanvas::SaveLayerStrategy SkDumpCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint,
+                                                        SaveFlags flags) {
     SkString str;
     str.printf("saveLayer(0x%X)", flags);
     if (bounds) {
@@ -214,12 +214,12 @@ int SkDumpCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
         }
     }
     this->dump(kSave_Verb, paint, str.c_str());
-    return this->INHERITED::saveLayer(bounds, paint, flags);
+    return this->INHERITED::willSaveLayer(bounds, paint, flags);
 }
 
-void SkDumpCanvas::restore() {
-    this->INHERITED::restore();
+void SkDumpCanvas::willRestore() {
     this->dump(kRestore_Verb, NULL, "restore");
+    this->INHERITED::willRestore();
 }
 
 bool SkDumpCanvas::translate(SkScalar dx, SkScalar dy) {
index 0f13073..2c8b9fa 100644 (file)
@@ -81,13 +81,13 @@ SkLuaCanvas::SkLuaCanvas(int width, int height, lua_State* L, const char func[])
 
 SkLuaCanvas::~SkLuaCanvas() {}
 
-int SkLuaCanvas::save(SaveFlags flags) {
+void SkLuaCanvas::willSave(SaveFlags flags) {
     AUTO_LUA("save");
-    return this->INHERITED::save(flags);
+    this->INHERITED::willSave(flags);
 }
 
-int SkLuaCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
-                             SaveFlags flags) {
+SkCanvas::SaveLayerStrategy SkLuaCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint,
+                                                       SaveFlags flags) {
     AUTO_LUA("saveLayer");
     if (bounds) {
         lua.pushRect(*bounds, "bounds");
@@ -95,12 +95,15 @@ int SkLuaCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
     if (paint) {
         lua.pushPaint(*paint, "paint");
     }
-    return this->INHERITED::save(flags);
+
+    this->INHERITED::willSaveLayer(bounds, paint, flags);
+    // No need for a layer.
+    return kNoLayer_SaveLayerStrategy;
 }
 
-void SkLuaCanvas::restore() {
+void SkLuaCanvas::willRestore() {
     AUTO_LUA("restore");
-    this->INHERITED::restore();
+    this->INHERITED::willRestore();
 }
 
 bool SkLuaCanvas::translate(SkScalar dx, SkScalar dy) {
index a9543f9..58b5e6e 100644 (file)
@@ -57,29 +57,33 @@ private:
     SkCanvas* fCanvas;
 };
 
-int SkNWayCanvas::save(SaveFlags flags) {
+void SkNWayCanvas::willSave(SaveFlags flags) {
     Iter iter(fList);
     while (iter.next()) {
         iter->save(flags);
     }
-    return this->INHERITED::save(flags);
+
+    this->INHERITED::willSave(flags);
 }
 
-int SkNWayCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
-                                    SaveFlags flags) {
+SkCanvas::SaveLayerStrategy SkNWayCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint,
+                                                        SaveFlags flags) {
     Iter iter(fList);
     while (iter.next()) {
         iter->saveLayer(bounds, paint, flags);
     }
-    return this->INHERITED::saveLayer(bounds, paint, flags);
+
+    this->INHERITED::willSaveLayer(bounds, paint, flags);
+    // No need for a layer.
+    return kNoLayer_SaveLayerStrategy;
 }
 
-void SkNWayCanvas::restore() {
+void SkNWayCanvas::willRestore() {
     Iter iter(fList);
     while (iter.next()) {
         iter->restore();
     }
-    this->INHERITED::restore();
+    this->INHERITED::willRestore();
 }
 
 bool SkNWayCanvas::translate(SkScalar dx, SkScalar dy) {
index 60fad87..6b34684 100644 (file)
@@ -18,21 +18,13 @@ class SkNoSaveLayerCanvas : public SkCanvas {
 public:
     SkNoSaveLayerCanvas(SkBaseDevice* device) : INHERITED(device) {}
 
-    // turn saveLayer() into save() for speed, should not affect correctness.
-    virtual int saveLayer(const SkRect* bounds,
-                          const SkPaint* paint,
-                          SaveFlags flags) SK_OVERRIDE {
-
-        // Like SkPictureRecord, we don't want to create layers, but we do need
-        // to respect the save and (possibly) its rect-clip.
-        int count = this->INHERITED::save(flags);
-        if (NULL != bounds) {
-            this->INHERITED::clipRectBounds(bounds, flags, NULL);
-        }
-        return count;
+protected:
+    virtual SaveLayerStrategy willSaveLayer(const SkRect* bounds, const SkPaint* paint,
+                                            SaveFlags flags) SK_OVERRIDE {
+        this->INHERITED::willSaveLayer(bounds, paint, flags);
+        return kNoLayer_SaveLayerStrategy;
     }
 
-protected:
     // disable aa for speed
     virtual void onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle) SK_OVERRIDE {
         this->INHERITED::onClipRect(rect, op, kHard_ClipEdgeStyle);
index 0a9d7a8..a32819d 100644 (file)
@@ -21,17 +21,22 @@ void SkProxyCanvas::setProxy(SkCanvas* proxy) {
 
 ///////////////////////////////// Overrides ///////////
 
-int SkProxyCanvas::save(SaveFlags flags) {
-    return fProxy->save(flags);
+void SkProxyCanvas::willSave(SaveFlags flags) {
+    fProxy->save(flags);
+    this->INHERITED::willSave(flags);
 }
 
-int SkProxyCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
-                             SaveFlags flags) {
-    return fProxy->saveLayer(bounds, paint, flags);
+SkCanvas::SaveLayerStrategy SkProxyCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint,
+                                                         SaveFlags flags) {
+    fProxy->saveLayer(bounds, paint, flags);
+    this->INHERITED::willSaveLayer(bounds, paint, flags);
+    // No need for a layer.
+    return kNoLayer_SaveLayerStrategy;
 }
 
-void SkProxyCanvas::restore() {
+void SkProxyCanvas::willRestore() {
     fProxy->restore();
+    this->INHERITED::willRestore();
 }
 
 bool SkProxyCanvas::translate(SkScalar dx, SkScalar dy) {
index 08e6d94..279976a 100644 (file)
@@ -530,8 +530,9 @@ void SkDebugCanvas::onPopCull() {
     this->addDrawCommand(new SkPopCullCommand());
 }
 
-void SkDebugCanvas::restore() {
-    addDrawCommand(new SkRestoreCommand());
+void SkDebugCanvas::willRestore() {
+    this->addDrawCommand(new SkRestoreCommand());
+    this->INHERITED::willRestore();
 }
 
 bool SkDebugCanvas::rotate(SkScalar degrees) {
@@ -539,15 +540,17 @@ bool SkDebugCanvas::rotate(SkScalar degrees) {
     return true;
 }
 
-int SkDebugCanvas::save(SaveFlags flags) {
-    addDrawCommand(new SkSaveCommand(flags));
-    return true;
+void SkDebugCanvas::willSave(SaveFlags flags) {
+    this->addDrawCommand(new SkSaveCommand(flags));
+    this->INHERITED::willSave(flags);
 }
 
-int SkDebugCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
-        SaveFlags flags) {
-    addDrawCommand(new SkSaveLayerCommand(bounds, paint, flags));
-    return true;
+SkCanvas::SaveLayerStrategy SkDebugCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint,
+                                                         SaveFlags flags) {
+    this->addDrawCommand(new SkSaveLayerCommand(bounds, paint, flags));
+    this->INHERITED::willSaveLayer(bounds, paint, flags);
+    // No need for a full layer.
+    return kNoLayer_SaveLayerStrategy;
 }
 
 bool SkDebugCanvas::scale(SkScalar sx, SkScalar sy) {
index 0bbb640..bf3758d 100644 (file)
@@ -206,14 +206,8 @@ public:
                               const uint16_t indices[], int indexCount,
                               const SkPaint&) SK_OVERRIDE;
 
-    virtual void restore() SK_OVERRIDE;
-
     virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
 
-    virtual int save(SaveFlags) SK_OVERRIDE;
-
-    virtual int saveLayer(const SkRect* bounds, const SkPaint*, SaveFlags) SK_OVERRIDE;
-
     virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
 
     virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
@@ -248,6 +242,10 @@ public:
     }
 
 protected:
+    virtual void willSave(SaveFlags) SK_OVERRIDE;
+    virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE;
+    virtual void willRestore() SK_OVERRIDE;
+
     virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
     virtual void onPushCull(const SkRect& cullRect) SK_OVERRIDE;
     virtual void onPopCull() SK_OVERRIDE;