Adding setSurface public API method to SkDeferredCanvas

The purpose of this change is to provide an API that Blink 2D canvas layers can use
to install a new render target when recovering from a lost graphics context.

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

git-svn-id: http://skia.googlecode.com/svn/trunk@9276 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/utils/SkDeferredCanvas.cpp b/src/utils/SkDeferredCanvas.cpp
index f2aae06..b396a08 100644
--- a/src/utils/SkDeferredCanvas.cpp
+++ b/src/utils/SkDeferredCanvas.cpp
@@ -148,6 +148,7 @@
     SkCanvas* immediateCanvas() const {return fImmediateCanvas;}
     SkDevice* immediateDevice() const {return fImmediateCanvas->getTopDevice();}
     SkImage* newImageSnapshot();
+    void setSurface(SkSurface* surface);
     bool isFreshFrame();
     bool hasPendingCommands();
     size_t storageAllocatedForRecording() const;
@@ -261,6 +262,7 @@
                immediateDevice->getDeviceProperties()) {
     fSurface = NULL;
     fImmediateCanvas = SkNEW_ARGS(SkCanvas, (immediateDevice));
+    fPipeController.setPlaybackCanvas(fImmediateCanvas);
     this->init();
 }
 
@@ -272,13 +274,18 @@
                surface->getCanvas()->getDevice()->getDeviceProperties()) {
     fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
     fNotificationClient = NULL;
-    fImmediateCanvas = surface->getCanvas();
-    SkSafeRef(fImmediateCanvas);
-    fSurface = surface;
-    SkSafeRef(fSurface);
+    fImmediateCanvas = NULL;
+    fSurface = NULL;
+    this->setSurface(surface);
     this->init();
 }
 
+void DeferredDevice::setSurface(SkSurface* surface) {
+    SkRefCnt_SafeAssign(fImmediateCanvas, surface->getCanvas());
+    SkRefCnt_SafeAssign(fSurface, surface);
+    fPipeController.setPlaybackCanvas(fImmediateCanvas);
+}
+
 void DeferredDevice::init() {
     fRecordingCanvas = NULL;
     fFreshFrame = true;
@@ -287,7 +294,6 @@
     fBitmapSizeThreshold = kDeferredCanvasBitmapSizeThreshold;
     fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
     fNotificationClient = NULL;
-    fPipeController.setPlaybackCanvas(fImmediateCanvas);
     this->beginRecording();
 }
 
@@ -620,6 +626,19 @@
     return device;
 }
 
+SkSurface* SkDeferredCanvas::setSurface(SkSurface* surface) {
+    DeferredDevice* deferredDevice = this->getDeferredDevice();
+    if (NULL != deferredDevice) {
+        // By swapping the surface into the existing device, we preserve
+        // all pending commands, which can help to seamlessly recover from
+        // a lost accelerated graphics context.
+        deferredDevice->setSurface(surface);
+    } else {
+        this->INHERITED::setDevice(SkNEW_ARGS(DeferredDevice, (surface)))->unref();
+    }
+    return surface;
+}
+
 SkDeferredCanvas::NotificationClient* SkDeferredCanvas::setNotificationClient(
     NotificationClient* notificationClient) {