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]));
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;
}
/* 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;
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);
Json::Value makeEdgeStyle(SkCanvas::ClipEdgeStyle edgeStyle);
Json::Value makePointMode(SkCanvas::PointMode mode);
-
- Json::Value makeMatrix(const SkMatrix& matrix);
-
+
void updateMatrix();
SkWStream& fOut;
#include "SkCommandLineFlags.h"
#include "SkDebugCanvas.h"
#include "SkJSONCanvas.h"
+#include "SkJSONCPP.h"
#include "SkPicture.h"
#include "SkPictureRecorder.h"
#include "SkPixelSerializer.h"
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,
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";
}
};
+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<SkString> commands;
+ SkStrSplit(url, "/", &commands);
+
+ if (!request->fPicture.get() || commands.count() > 2) {
+ return MHD_NO;
+ }
+
+ // drawTo
+ SkAutoTUnref<SkSurface> 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<SkData> 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 {
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);
}