pass surface to device-manager

Bug: skia:3216
Change-Id: I8e00e9eca3763593a4071c16a3ab04c46bf83a3e
Reviewed-on: https://skia-review.googlesource.com/26020
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Mike Reed <reed@google.com>
diff --git a/include/views/SkWindow.h b/include/views/SkWindow.h
index 8b6c291..e744b19 100644
--- a/include/views/SkWindow.h
+++ b/include/views/SkWindow.h
@@ -79,6 +79,7 @@
     void    postConcat(const SkMatrix&);
 
     virtual sk_sp<SkSurface> makeSurface();
+    virtual void drawIntoSurface();
 
 #if SK_SUPPORT_GPU
     sk_sp<SkSurface> makeGpuBackedSurface(const AttachmentInfo& attachmentInfo,
diff --git a/samplecode/SampleApp.cpp b/samplecode/SampleApp.cpp
index 1676fc3..102c67a 100644
--- a/samplecode/SampleApp.cpp
+++ b/samplecode/SampleApp.cpp
@@ -332,8 +332,8 @@
         return nullptr;
     }
 
-    void publishCanvas(SampleWindow::DeviceType dType,
-                       SkCanvas* renderingCanvas, SampleWindow* win) override {
+    void publishCanvas(SampleWindow::DeviceType dType, SkSurface* surface,
+                       SampleWindow* win) override {
 #if SK_SUPPORT_GPU
         if (!IsGpuDeviceType(dType) ||
             kRGBA_F16_SkColorType == win->info().colorType() ||
@@ -345,7 +345,7 @@
             auto data = SkData::MakeUninitialized(size);
             SkASSERT(data);
 
-            if (!renderingCanvas->readPixels(info, data->writable_data(), rowBytes, 0, 0)) {
+            if (!surface->readPixels(info, data->writable_data(), rowBytes, 0, 0)) {
                 SkDEBUGFAIL("Failed to read canvas pixels");
                 return;
             }
@@ -1091,6 +1091,14 @@
 #include "SkDeferredCanvas.h"
 #include "SkDumpCanvas.h"
 
+void SampleWindow::drawIntoSurface() {
+    auto surf = this->makeSurface();
+
+    this->draw(surf->getCanvas());
+
+    fDevManager->publishCanvas(fDeviceType, surf.get(), this);
+}
+
 void SampleWindow::draw(SkCanvas* canvas) {
     std::unique_ptr<SkThreadedBMPDevice> tDev;
     std::unique_ptr<SkCanvas> tCanvas;
@@ -1164,9 +1172,6 @@
     }
 
     canvas->flush();
-
-    // do this last
-    fDevManager->publishCanvas(fDeviceType, canvas, this);
 }
 
 static float clipW = 200;
diff --git a/samplecode/SampleApp.h b/samplecode/SampleApp.h
index 6ddb5c5..954b855 100644
--- a/samplecode/SampleApp.h
+++ b/samplecode/SampleApp.h
@@ -92,9 +92,7 @@
 
         // called after drawing, should get the results onto the
         // screen.
-        virtual void publishCanvas(DeviceType dType,
-                                   SkCanvas* canvas,
-                                   SampleWindow* win) = 0;
+        virtual void publishCanvas(DeviceType, SkSurface*, SampleWindow*) = 0;
 
         // called when window changes size, guaranteed to be called
         // at least once before first draw (after init)
@@ -127,6 +125,8 @@
         return surface;
     }
 
+    void drawIntoSurface() override;
+
     void draw(SkCanvas*) override;
 
     void setDeviceType(DeviceType type);
diff --git a/src/views/SkWindow.cpp b/src/views/SkWindow.cpp
index ba06a1f..f01368b 100644
--- a/src/views/SkWindow.cpp
+++ b/src/views/SkWindow.cpp
@@ -35,6 +35,10 @@
     return SkSurface::MakeRasterDirect(bm.info(), bm.getPixels(), bm.rowBytes(), &fSurfaceProps);
 }
 
+void SkWindow::drawIntoSurface() {
+    this->draw(this->makeSurface()->getCanvas());
+}
+
 void SkWindow::setMatrix(const SkMatrix& matrix) {
     if (fMatrix != matrix) {
         fMatrix = matrix;
diff --git a/src/views/mac/SkNSView.mm b/src/views/mac/SkNSView.mm
index 64c02ca..7366c5c 100644
--- a/src/views/mac/SkNSView.mm
+++ b/src/views/mac/SkNSView.mm
@@ -13,7 +13,6 @@
 static_assert(SK_SUPPORT_GPU, "not_implemented_for_non_gpu_build");
 #include <OpenGL/gl.h>
 
-//#define FORCE_REDRAW
 // Can be dropped when we no longer support 10.6.
 #if defined(MAC_OS_X_VERSION_10_7) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
     #define RETINA_API_AVAILABLE 1
@@ -140,11 +139,7 @@
 - (void)drawSkia {
     fRedrawRequestPending = false;
     if (fWind) {
-        sk_sp<SkSurface> surface(fWind->makeSurface());
-        fWind->draw(surface->getCanvas());
-#ifdef FORCE_REDRAW
-        fWind->inval(NULL);
-#endif
+        fWind->drawIntoSurface();
     }
 }