2016-02-25 19:28:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/skiaserve/urlhandlers/UrlHandler.h"
|
2016-02-25 19:28:18 +00:00
|
|
|
|
|
|
|
#include "microhttpd.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/skiaserve/Request.h"
|
|
|
|
#include "tools/skiaserve/Response.h"
|
2016-02-25 19:28:18 +00:00
|
|
|
|
|
|
|
using namespace Response;
|
|
|
|
|
|
|
|
bool BreakHandler::canHandle(const char* method, const char* url) {
|
|
|
|
static const char* kBasePath = "/break";
|
|
|
|
return 0 == strcmp(method, MHD_HTTP_METHOD_GET) &&
|
|
|
|
0 == strncmp(url, kBasePath, strlen(kBasePath));
|
|
|
|
}
|
|
|
|
|
|
|
|
int BreakHandler::handle(Request* request, MHD_Connection* connection,
|
|
|
|
const char* url, const char* method,
|
|
|
|
const char* upload_data, size_t* upload_data_size) {
|
|
|
|
SkTArray<SkString> commands;
|
|
|
|
SkStrSplit(url, "/", &commands);
|
|
|
|
|
2016-02-29 13:35:04 +00:00
|
|
|
if (!request->hasPicture() || commands.count() != 4) {
|
2016-02-25 19:28:18 +00:00
|
|
|
return MHD_NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
// /break/<n>/<x>/<y>
|
|
|
|
int n;
|
|
|
|
sscanf(commands[1].c_str(), "%d", &n);
|
|
|
|
int x;
|
|
|
|
sscanf(commands[2].c_str(), "%d", &x);
|
|
|
|
int y;
|
|
|
|
sscanf(commands[3].c_str(), "%d", &y);
|
|
|
|
|
|
|
|
int count = request->fDebugCanvas->getSize();
|
|
|
|
SkASSERT(n < count);
|
|
|
|
|
|
|
|
SkCanvas* canvas = request->getCanvas();
|
|
|
|
canvas->clear(SK_ColorWHITE);
|
|
|
|
int saveCount = canvas->save();
|
|
|
|
for (int i = 0; i <= n; ++i) {
|
|
|
|
request->fDebugCanvas->getDrawCommandAt(i)->execute(canvas);
|
|
|
|
}
|
2016-03-09 18:07:02 +00:00
|
|
|
SkColor target = request->getPixel(x, y);
|
2019-01-28 18:41:19 +00:00
|
|
|
|
|
|
|
SkDynamicMemoryWStream stream;
|
|
|
|
SkJSONWriter writer(&stream, SkJSONWriter::Mode::kFast);
|
|
|
|
writer.beginObject(); // root
|
|
|
|
|
2019-03-20 16:59:00 +00:00
|
|
|
writer.appendName("startColor");
|
|
|
|
DrawCommand::MakeJsonColor(writer, target);
|
2019-01-28 18:41:19 +00:00
|
|
|
|
|
|
|
bool changed = false;
|
2016-02-25 19:28:18 +00:00
|
|
|
for (int i = n + 1; i < n + count; ++i) {
|
|
|
|
int index = i % count;
|
|
|
|
if (index == 0) {
|
|
|
|
// reset canvas for wraparound
|
|
|
|
canvas->restoreToCount(saveCount);
|
|
|
|
canvas->clear(SK_ColorWHITE);
|
|
|
|
saveCount = canvas->save();
|
|
|
|
}
|
|
|
|
request->fDebugCanvas->getDrawCommandAt(index)->execute(canvas);
|
2016-03-09 18:07:02 +00:00
|
|
|
SkColor current = request->getPixel(x, y);
|
2016-02-25 19:28:18 +00:00
|
|
|
if (current != target) {
|
2019-03-20 16:59:00 +00:00
|
|
|
writer.appendName("endColor");
|
|
|
|
DrawCommand::MakeJsonColor(writer, current);
|
2019-01-28 18:41:19 +00:00
|
|
|
writer.appendS32("endOp", index);
|
|
|
|
changed = true;
|
2016-02-25 19:28:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-01-28 18:41:19 +00:00
|
|
|
if (!changed) {
|
2019-03-20 16:59:00 +00:00
|
|
|
writer.appendName("endColor");
|
|
|
|
DrawCommand::MakeJsonColor(writer, target);
|
2019-01-28 18:41:19 +00:00
|
|
|
writer.appendS32("endOp", n);
|
|
|
|
}
|
2016-02-25 19:28:18 +00:00
|
|
|
canvas->restoreToCount(saveCount);
|
2019-01-28 18:41:19 +00:00
|
|
|
|
|
|
|
writer.endObject(); // root
|
|
|
|
writer.flush();
|
2016-09-12 19:01:44 +00:00
|
|
|
return SendData(connection, stream.detachAsData().get(), "application/json");
|
2016-02-25 19:28:18 +00:00
|
|
|
}
|