SkImage_Raster's pixels are always immutable.

To make this work, we tag their pixelrefs as temporarily immutable, allowing
ourselves to restore the pixels to mutability only when the image drops away.

This should allow us to wobble back and forth between writing to the Surface
and reading from the Image without a COW, with the Surface seeing mutable
pixels and the Image seeing immutable pixels.

The big idea is, Image doesn't need forever-immutable pixels, it just needs
pixels that are immutable as long as it's alive.

BUG=skia:

patch from issue 804523002 at patchset 40001 (http://crrev.com/804523002#ps40001)

Review URL: https://codereview.chromium.org/1254383006
diff --git a/src/image/SkSurface_Raster.cpp b/src/image/SkSurface_Raster.cpp
index d59aef7..d0a6553 100644
--- a/src/image/SkSurface_Raster.cpp
+++ b/src/image/SkSurface_Raster.cpp
@@ -27,6 +27,7 @@
     SkImage* onNewImageSnapshot(Budgeted) override;
     void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) override;
     void onCopyOnWrite(ContentChangeMode) override;
+    void onRestoreBackingMutability() override;
 
 private:
     SkBitmap    fBitmap;
@@ -118,10 +119,24 @@
 }
 
 SkImage* SkSurface_Raster::onNewImageSnapshot(Budgeted) {
+    if (fWeOwnThePixels) {
+        // SkImage_raster requires these pixels are immutable for its full lifetime.
+        // We'll undo this via onRestoreBackingMutability() if we can avoid the COW.
+        if (SkPixelRef* pr = fBitmap.pixelRef()) {
+            pr->setTemporarilyImmutable();
+        }
+    }
     // Our pixels are in memory, so read access on the snapshot SkImage could be cheap.
     // Lock the shared pixel ref to ensure peekPixels() is usable.
-    return SkNewImageFromRasterBitmap(fBitmap, fWeOwnThePixels, &this->props(),
-                                      kLocked_SharedPixelRefMode);
+    return SkNewImageFromRasterBitmap(fBitmap, &this->props(), kLocked_SharedPixelRefMode,
+                                      fWeOwnThePixels ? kNo_ForceCopyMode : kYes_ForceCopyMode);
+}
+
+void SkSurface_Raster::onRestoreBackingMutability() {
+    SkASSERT(!this->hasCachedImage());  // Shouldn't be any snapshots out there.
+    if (SkPixelRef* pr = fBitmap.pixelRef()) {
+        pr->restoreMutability();
+    }
 }
 
 void SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) {
@@ -158,7 +173,7 @@
     if (NULL == pixels) {
         return NULL;
     }
-    
+
     return SkNEW_ARGS(SkSurface_Raster, (info, pixels, rb, releaseProc, context, props));
 }