Flutter Engine
The Flutter Engine
BreakHandler.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
9
10#include "microhttpd.h"
16
17using namespace skia_private;
18using namespace Response;
19
20bool BreakHandler::canHandle(const char* method, const char* url) {
21 static const char* kBasePath = "/break";
22 return 0 == strcmp(method, MHD_HTTP_METHOD_GET) &&
23 0 == strncmp(url, kBasePath, strlen(kBasePath));
24}
25
26int BreakHandler::handle(Request* request, MHD_Connection* connection,
27 const char* url, const char* method,
28 const char* upload_data, size_t* upload_data_size) {
30 SkStrSplit(url, "/", &commands);
31
32 if (!request->hasPicture() || commands.size() != 4) {
33 return MHD_NO;
34 }
35
36 // /break/<n>/<x>/<y>
37 int n;
38 sscanf(commands[1].c_str(), "%d", &n);
39 int x;
40 sscanf(commands[2].c_str(), "%d", &x);
41 int y;
42 sscanf(commands[3].c_str(), "%d", &y);
43
44 int count = request->fDebugCanvas->getSize();
45 SkASSERT(n < count);
46
47 SkCanvas* canvas = request->getCanvas();
48 canvas->clear(SK_ColorWHITE);
49 int saveCount = canvas->save();
50 for (int i = 0; i <= n; ++i) {
51 request->fDebugCanvas->getDrawCommandAt(i)->execute(canvas);
52 }
53 SkColor target = request->getPixel(x, y);
54
57 writer.beginObject(); // root
58
59 writer.appendName("startColor");
61
62 bool changed = false;
63 for (int i = n + 1; i < n + count; ++i) {
64 int index = i % count;
65 if (index == 0) {
66 // reset canvas for wraparound
67 canvas->restoreToCount(saveCount);
68 canvas->clear(SK_ColorWHITE);
69 saveCount = canvas->save();
70 }
71 request->fDebugCanvas->getDrawCommandAt(index)->execute(canvas);
72 SkColor current = request->getPixel(x, y);
73 if (current != target) {
74 writer.appendName("endColor");
75 DrawCommand::MakeJsonColor(writer, current);
76 writer.appendS32("endOp", index);
77 changed = true;
78 break;
79 }
80 }
81 if (!changed) {
82 writer.appendName("endColor");
84 writer.appendS32("endOp", n);
85 }
86 canvas->restoreToCount(saveCount);
87
88 writer.endObject(); // root
89 writer.flush();
90 return SendData(connection, stream.detachAsData().get(), "application/json");
91}
int count
Definition: FontMgrTest.cpp:50
#define SkASSERT(cond)
Definition: SkAssert.h:116
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
void SkStrSplit(const char *str, const char *delimiters, SkStrSplitMode splitMode, TArray< SkString > *out)
int handle(Request *request, MHD_Connection *connection, const char *url, const char *method, const char *upload_data, size_t *upload_data_size) override
bool canHandle(const char *method, const char *url) override
static void MakeJsonColor(SkJSONWriter &, const SkColor color)
void clear(SkColor color)
Definition: SkCanvas.h:1199
void restoreToCount(int saveCount)
Definition: SkCanvas.cpp:478
int save()
Definition: SkCanvas.cpp:447
void appendS32(int32_t value)
Definition: SkJSONWriter.h:237
void beginObject(const char *name=nullptr, bool multiline=true)
Definition: SkJSONWriter.h:114
void endObject()
Definition: SkJSONWriter.h:126
void appendName(const char *name)
Definition: SkJSONWriter.h:90
void flush()
Definition: SkJSONWriter.h:78
uint32_t * target
double y
double x
int SendData(MHD_Connection *connection, const SkData *data, const char *type, bool setContentDisposition, const char *dispositionString)
Definition: Response.cpp:64
dictionary commands
Definition: dom.py:171