Adding optimization to avoid image copy in SkSurface copy on write when content is discardable
This patch also adds code to SkDeferredCanvas to trigger the optimization.
TEST=DeferredSurfaceCopy bench, Surface unit test
R=reed@google.com
Author: junov@chromium.org
Review URL: https://chromiumcodereview.appspot.com/14063015
git-svn-id: http://skia.googlecode.com/svn/trunk@8797 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/image/SkSurface_Raster.cpp b/src/image/SkSurface_Raster.cpp
index 9a1f312..ccfdd27 100644
--- a/src/image/SkSurface_Raster.cpp
+++ b/src/image/SkSurface_Raster.cpp
@@ -25,7 +25,7 @@
virtual SkImage* onNewImageSnapshot() SK_OVERRIDE;
virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y,
const SkPaint*) SK_OVERRIDE;
- virtual void onCopyOnWrite() SK_OVERRIDE;
+ virtual void onCopyOnWrite(ContentChangeMode) SK_OVERRIDE;
private:
SkBitmap fBitmap;
@@ -124,13 +124,18 @@
return SkNewImageFromBitmap(fBitmap, fWeOwnThePixels);
}
-void SkSurface_Raster::onCopyOnWrite() {
+void SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) {
// are we sharing pixelrefs with the image?
- SkASSERT(NULL !=this->getCachedImage());
+ SkASSERT(NULL != this->getCachedImage());
if (SkBitmapImageGetPixelRef(this->getCachedImage()) == fBitmap.pixelRef()) {
SkASSERT(fWeOwnThePixels);
- SkBitmap prev(fBitmap);
- prev.deepCopyTo(&fBitmap, prev.config());
+ if (kDiscard_ContentChangeMode == mode) {
+ fBitmap.setPixelRef(NULL, 0);
+ fBitmap.allocPixels();
+ } else {
+ SkBitmap prev(fBitmap);
+ prev.deepCopyTo(&fBitmap, prev.config());
+ }
// Now fBitmap is a deep copy of itself (and therefore different from
// what is being used by the image. Next we update the canvas to use
// this as its backend, so we can't modify the image's pixels anymore.