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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "UrlHandler.h"
|
|
|
|
|
|
|
|
#include "microhttpd.h"
|
|
|
|
#include "../Request.h"
|
|
|
|
#include "../Response.h"
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
writer.appendName("startColor"); SkDrawCommand::MakeJsonColor(writer, target);
|
|
|
|
|
|
|
|
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-01-28 18:41:19 +00:00
|
|
|
writer.appendName("endColor"); SkDrawCommand::MakeJsonColor(writer, current);
|
|
|
|
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) {
|
|
|
|
writer.appendName("endColor"); SkDrawCommand::MakeJsonColor(writer, target);
|
|
|
|
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
|
|
|
}
|