Get SkiaServe Request started off with a little privacy

This CL moves the surface stuff into the request object so it
can maintain its internal state consistently

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1741823002

Review URL: https://codereview.chromium.org/1741823002
diff --git a/tools/skiaserve/Request.cpp b/tools/skiaserve/Request.cpp
index 3d9e9d3..4d256d5 100644
--- a/tools/skiaserve/Request.cpp
+++ b/tools/skiaserve/Request.cpp
@@ -9,6 +9,8 @@
 
 #include "png.h"
 
+#include "SkJSONCanvas.h"
+
 const int Request::kImageWidth = 1920;
 const int Request::kImageHeight = 1080;
 
@@ -48,6 +50,16 @@
     sk_free(rows);
 }
 
+Request::Request(SkString rootUrl)
+    : fUploadContext(nullptr)
+    , fUrlDataManager(rootUrl)
+    , fGPUEnabled(false) {
+    // create surface
+    GrContextOptions grContextOpts;
+    fContextFactory.reset(new GrContextFactory(grContextOpts));
+    fSurface.reset(this->createCPUSurface());
+}
+
 SkBitmap* Request::getBitmapFromCanvas(SkCanvas* canvas) {
     SkBitmap* bmp = new SkBitmap();
     SkImageInfo info = SkImageInfo::Make(kImageWidth, kImageHeight, kRGBA_8888_SkColorType,
@@ -111,6 +123,21 @@
     return surface;
 }
 
+bool Request::enableGPU(bool enable) {    
+    if (enable) {
+        SkSurface* surface = this->createGPUSurface();
+        if (surface) {
+            fSurface.reset(surface);
+            fGPUEnabled = true;
+            return true;
+        }
+        return false;
+    }
+    fSurface.reset(this->createCPUSurface());
+    fGPUEnabled = false;
+    return true;
+}
+
 SkData* Request::getJsonOps(int n) {
     SkCanvas* canvas = this->getCanvas();
     Json::Value root = fDebugCanvas->toJSON(fUrlDataManager, n, canvas);
@@ -149,3 +176,24 @@
 
     return stream.copyToData();
 }
+
+SkData* Request::getJsonInfo(int n) {
+    // drawTo
+    SkAutoTUnref<SkSurface> surface(this->createCPUSurface());
+    SkCanvas* canvas = surface->getCanvas();
+
+    // TODO this is really slow and we should cache the matrix and clip
+    fDebugCanvas->drawTo(canvas, n);
+
+    // make some json
+    SkMatrix vm = fDebugCanvas->getCurrentMatrix();
+    SkIRect clip = fDebugCanvas->getCurrentClip();
+    Json::Value info(Json::objectValue);
+    info["ViewMatrix"] = SkJSONCanvas::MakeMatrix(vm);
+    info["ClipRect"] = SkJSONCanvas::MakeIRect(clip);
+
+    std::string json = Json::FastWriter().write(info);
+
+    // We don't want the null terminator so strlen is correct
+    return SkData::NewWithCopy(json.c_str(), strlen(json.c_str()));
+}
diff --git a/tools/skiaserve/Request.h b/tools/skiaserve/Request.h
index f6ee994..5685186 100644
--- a/tools/skiaserve/Request.h
+++ b/tools/skiaserve/Request.h
@@ -27,18 +27,14 @@
 };
 
 struct Request {
-    Request(SkString rootUrl) 
-    : fUploadContext(nullptr)
-    , fUrlDataManager(rootUrl)
-    , fGPUEnabled(false) {}
+    Request(SkString rootUrl); 
 
-    SkSurface* createCPUSurface();
-    SkSurface* createGPUSurface();
     SkData* drawToPng(int n);
     void drawToCanvas(int n);
     SkCanvas* getCanvas();
     SkData* writeCanvasToPng(SkCanvas* canvas);
     SkBitmap* getBitmapFromCanvas(SkCanvas* canvas);
+    bool enableGPU(bool enable);
     bool hasPicture() const { return SkToBool(fPicture.get()); }
     int getLastOp() const { return fDebugCanvas->getSize() - 1; }
 
@@ -48,6 +44,9 @@
     // Returns a json list of batches as an SkData
     SkData* getJsonBatchList(int n);
 
+    // Returns json with the viewMatrix and clipRect
+    SkData* getJsonInfo(int n);
+
     // TODO probably want to make this configurable
     static const int kImageWidth;
     static const int kImageHeight;
@@ -55,9 +54,14 @@
     UploadContext* fUploadContext;
     SkAutoTUnref<SkPicture> fPicture;
     SkAutoTUnref<SkDebugCanvas> fDebugCanvas;
+    UrlDataManager fUrlDataManager;
+    
+private:
+    SkSurface* createCPUSurface();
+    SkSurface* createGPUSurface();
+    
     SkAutoTDelete<GrContextFactory> fContextFactory;
     SkAutoTUnref<SkSurface> fSurface;
-    UrlDataManager fUrlDataManager;
     bool fGPUEnabled;
 };
 
diff --git a/tools/skiaserve/skiaserve.cpp b/tools/skiaserve/skiaserve.cpp
index d493f7d..b593e72 100644
--- a/tools/skiaserve/skiaserve.cpp
+++ b/tools/skiaserve/skiaserve.cpp
@@ -5,9 +5,6 @@
  * found in the LICENSE file.
  */
 
-#include "GrCaps.h"
-#include "GrContextFactory.h"
-
 #include "Request.h"
 #include "Response.h"
 
@@ -84,11 +81,6 @@
 int skiaserve_main() {
     Request request(SkString("/data")); // This simple server has one request
 
-    // create surface
-    GrContextOptions grContextOpts;
-    request.fContextFactory.reset(new GrContextFactory(grContextOpts));
-    request.fSurface.reset(request.createCPUSurface());
-
     struct MHD_Daemon* daemon;
     // TODO Add option to bind this strictly to an address, e.g. localhost, for security.
     daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, FLAGS_port, nullptr, nullptr,
diff --git a/tools/skiaserve/urlhandlers/EnableGPUHandler.cpp b/tools/skiaserve/urlhandlers/EnableGPUHandler.cpp
index 2dc9bb6..b5d0c7a 100644
--- a/tools/skiaserve/urlhandlers/EnableGPUHandler.cpp
+++ b/tools/skiaserve/urlhandlers/EnableGPUHandler.cpp
@@ -32,17 +32,10 @@
     int enable;
     sscanf(commands[1].c_str(), "%d", &enable);
 
-    if (enable) {
-        SkSurface* surface = request->createGPUSurface();
-        if (surface) {
-            request->fSurface.reset(surface);
-            request->fGPUEnabled = true;
-            return SendOK(connection);
-        }
+    bool success = request->enableGPU(enable);
+    if (!success) {
         return SendError(connection, "Unable to create GPU surface");
     }
-    request->fSurface.reset(request->createCPUSurface());
-    request->fGPUEnabled = false;
     return SendOK(connection);
 }
 
diff --git a/tools/skiaserve/urlhandlers/InfoHandler.cpp b/tools/skiaserve/urlhandlers/InfoHandler.cpp
index 16dbefb..65bbeb9 100644
--- a/tools/skiaserve/urlhandlers/InfoHandler.cpp
+++ b/tools/skiaserve/urlhandlers/InfoHandler.cpp
@@ -8,7 +8,6 @@
 #include "UrlHandler.h"
 
 #include "microhttpd.h"
-#include "SkJSONCanvas.h"
 #include "../Request.h"
 #include "../Response.h"
 
@@ -26,36 +25,19 @@
     SkTArray<SkString> commands;
     SkStrSplit(url, "/", &commands);
 
-    if (!request->fPicture.get() || commands.count() > 2) {
+    if (!request->hasPicture() || commands.count() > 2) {
         return MHD_NO;
     }
 
-    // drawTo
-    SkAutoTUnref<SkSurface> surface(request->createCPUSurface());
-    SkCanvas* canvas = surface->getCanvas();
-
     int n;
     // /info or /info/N
     if (commands.count() == 1) {
-        n = request->fDebugCanvas->getSize() - 1;
+        n = request->getLastOp();
     } else {
         sscanf(commands[1].c_str(), "%d", &n);
     }
 
-    // TODO this is really slow and we should cache the matrix and clip
-    request->fDebugCanvas->drawTo(canvas, n);
-
-    // make some json
-    SkMatrix vm = request->fDebugCanvas->getCurrentMatrix();
-    SkIRect clip = request->fDebugCanvas->getCurrentClip();
-    Json::Value info(Json::objectValue);
-    info["ViewMatrix"] = SkJSONCanvas::MakeMatrix(vm);
-    info["ClipRect"] = SkJSONCanvas::MakeIRect(clip);
-
-    std::string json = Json::FastWriter().write(info);
-
-    // We don't want the null terminator so strlen is correct
-    SkAutoTUnref<SkData> data(SkData::NewWithCopy(json.c_str(), strlen(json.c_str())));
+    SkAutoTUnref<SkData> data(request->getJsonInfo(n));
     return SendData(connection, data, "application/json");
 }