diff --git a/tools/json/SkJSONCanvas.cpp b/tools/json/SkJSONCanvas.cpp index efc4acd1a3..2272f88152 100644 --- a/tools/json/SkJSONCanvas.cpp +++ b/tools/json/SkJSONCanvas.cpp @@ -382,7 +382,16 @@ Json::Value SkJSONCanvas::makePaint(const SkPaint& paint) { return result; } -Json::Value SkJSONCanvas::makeMatrix(const SkMatrix& matrix) { +Json::Value SkJSONCanvas::MakeIRect(const SkIRect& rect) { + Json::Value result(Json::arrayValue); + result.append(Json::Value(rect.left())); + result.append(Json::Value(rect.top())); + result.append(Json::Value(rect.right())); + result.append(Json::Value(rect.bottom())); + return result; +} + +Json::Value SkJSONCanvas::MakeMatrix(const SkMatrix& matrix) { Json::Value result(Json::arrayValue); Json::Value row1(Json::arrayValue); row1.append(Json::Value(matrix[0])); @@ -441,7 +450,7 @@ void SkJSONCanvas::updateMatrix() { if (matrix != fLastMatrix) { Json::Value command(Json::objectValue); command[SKJSONCANVAS_COMMAND] = Json::Value(SKJSONCANVAS_COMMAND_MATRIX); - command[SKJSONCANVAS_ATTRIBUTE_MATRIX] = this->makeMatrix(matrix); + command[SKJSONCANVAS_ATTRIBUTE_MATRIX] = MakeMatrix(matrix); fCommands.append(command); fLastMatrix = matrix; } diff --git a/tools/json/SkJSONCanvas.h b/tools/json/SkJSONCanvas.h index 76d1be0ac5..b919efc66c 100644 --- a/tools/json/SkJSONCanvas.h +++ b/tools/json/SkJSONCanvas.h @@ -149,6 +149,10 @@ public: /* Complete the JSON document. */ void finish(); + static Json::Value MakeMatrix(const SkMatrix& matrix); + + static Json::Value MakeIRect(const SkIRect& irect); + // overridden SkCanvas API void onDrawPaint(const SkPaint&) override; @@ -226,6 +230,7 @@ public: SkCanvas::SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec& rec) override; private: + // Helpers to turn values into JSON, these could probably be static Json::Value makePoint(const SkPoint& point); Json::Value makePoint(SkScalar x, SkScalar y); @@ -245,9 +250,7 @@ private: Json::Value makeEdgeStyle(SkCanvas::ClipEdgeStyle edgeStyle); Json::Value makePointMode(SkCanvas::PointMode mode); - - Json::Value makeMatrix(const SkMatrix& matrix); - + void updateMatrix(); SkWStream& fOut; diff --git a/tools/skiaserve/skiaserve.cpp b/tools/skiaserve/skiaserve.cpp index fad8aa4ace..85e9c87c17 100644 --- a/tools/skiaserve/skiaserve.cpp +++ b/tools/skiaserve/skiaserve.cpp @@ -11,6 +11,7 @@ #include "SkCommandLineFlags.h" #include "SkDebugCanvas.h" #include "SkJSONCanvas.h" +#include "SkJSONCPP.h" #include "SkPicture.h" #include "SkPictureRecorder.h" #include "SkPixelSerializer.h" @@ -117,6 +118,12 @@ SkData* setupAndDrawToCanvasReturnPng(SkDebugCanvas* debugCanvas, int n) { return writeCanvasToPng(canvas); } +SkSurface* setupCpuSurface() { + SkImageInfo info = SkImageInfo::Make(kImageWidth, kImageHeight, kN32_SkColorType, + kPremul_SkAlphaType); + return SkSurface::NewRaster(info); +} + static const size_t kBufferSize = 1024; static int process_upload_data(void* cls, enum MHD_ValueKind kind, @@ -188,7 +195,7 @@ public: const char* upload_data, size_t* upload_data_size) = 0; }; -class InfoHandler : public UrlHandler { +class CmdHandler : public UrlHandler { public: bool canHandle(const char* method, const char* url) override { const char* kBasePath = "/cmd"; @@ -356,6 +363,55 @@ public: } }; +class InfoHandler : public UrlHandler { +public: + bool canHandle(const char* method, const char* url) override { + const char* kBaseName = "/info"; + return 0 == strcmp(method, MHD_HTTP_METHOD_GET) && + 0 == strncmp(url, kBaseName, strlen(kBaseName)); + } + + int handle(Request* request, MHD_Connection* connection, + const char* url, const char* method, + const char* upload_data, size_t* upload_data_size) override { + SkTArray commands; + SkStrSplit(url, "/", &commands); + + if (!request->fPicture.get() || commands.count() > 2) { + return MHD_NO; + } + + // drawTo + SkAutoTUnref surface(setupCpuSurface()); + SkCanvas* canvas = surface->getCanvas(); + + int n; + // /info or /info/N + if (commands.count() == 1) { + n = request->fDebugCanvas->getSize() - 1; + } 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 data(SkData::NewWithCopy(json.c_str(), strlen(json.c_str()))); + return SendData(connection, data, "application/json"); + } +}; + + class RootHandler : public UrlHandler { public: bool canHandle(const char* method, const char* url) override { @@ -377,6 +433,7 @@ public: fHandlers.push_back(new RootHandler); fHandlers.push_back(new PostHandler); fHandlers.push_back(new ImgHandler); + fHandlers.push_back(new CmdHandler); fHandlers.push_back(new InfoHandler); fHandlers.push_back(new DownloadHandler); }