diff options
author | 2016-02-26 08:36:25 -0800 | |
---|---|---|
committer | 2016-02-26 08:36:25 -0800 | |
commit | ee5348b069d53e5d843b2cebbac728ca7868d9fa (patch) | |
tree | 047fcf0109be8c0ddd6882660abaa77e378c887d /tools/skiaserve | |
parent | 67acb83ce96149edf2c5dd56b0d08ffa8011dae9 (diff) |
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
Diffstat (limited to 'tools/skiaserve')
-rw-r--r-- | tools/skiaserve/Request.cpp | 48 | ||||
-rw-r--r-- | tools/skiaserve/Request.h | 18 | ||||
-rw-r--r-- | tools/skiaserve/skiaserve.cpp | 8 | ||||
-rw-r--r-- | tools/skiaserve/urlhandlers/EnableGPUHandler.cpp | 11 | ||||
-rw-r--r-- | tools/skiaserve/urlhandlers/InfoHandler.cpp | 24 |
5 files changed, 64 insertions, 45 deletions
diff --git a/tools/skiaserve/Request.cpp b/tools/skiaserve/Request.cpp index 3d9e9d37b3..4d256d56d8 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 @@ static void write_png(const png_bytep rgba, png_uint_32 width, png_uint_32 heigh 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 @@ SkSurface* Request::createGPUSurface() { 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 @@ SkData* Request::getJsonBatchList(int n) { 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 f6ee994ecd..568518644c 100644 --- a/tools/skiaserve/Request.h +++ b/tools/skiaserve/Request.h @@ -27,18 +27,14 @@ struct UploadContext { }; 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 @@ struct Request { // 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 @@ struct Request { 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 d493f7d923..b593e7224f 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 answer_to_connection(void* cls, struct MHD_Connection* connection, 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 2dc9bb60c6..b5d0c7a245 100644 --- a/tools/skiaserve/urlhandlers/EnableGPUHandler.cpp +++ b/tools/skiaserve/urlhandlers/EnableGPUHandler.cpp @@ -32,17 +32,10 @@ int EnableGPUHandler::handle(Request* request, MHD_Connection* connection, 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 16dbefb415..65bbeb9526 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 @@ int InfoHandler::handle(Request* request, MHD_Connection* connection, 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"); } |