xref: /aosp_15_r20/external/skia/tools/skiaserve/urlhandlers/BreakHandler.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 
8 #include "tools/skiaserve/urlhandlers/UrlHandler.h"
9 
10 #include "microhttpd.h"
11 #include "src/core/SkStringUtils.h"
12 #include "src/utils/SkJSONWriter.h"
13 #include "tools/debugger/DrawCommand.h"
14 #include "tools/skiaserve/Request.h"
15 #include "tools/skiaserve/Response.h"
16 
17 using namespace skia_private;
18 using namespace Response;
19 
canHandle(const char * method,const char * url)20 bool 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 
handle(Request * request,MHD_Connection * connection,const char * url,const char * method,const char * upload_data,size_t * upload_data_size)26 int BreakHandler::handle(Request* request, MHD_Connection* connection,
27                          const char* url, const char* method,
28                          const char* upload_data, size_t* upload_data_size) {
29     TArray<SkString> commands;
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 
55     SkDynamicMemoryWStream stream;
56     SkJSONWriter writer(&stream, SkJSONWriter::Mode::kFast);
57     writer.beginObject(); // root
58 
59     writer.appendName("startColor");
60     DrawCommand::MakeJsonColor(writer, target);
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");
83         DrawCommand::MakeJsonColor(writer, target);
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 }
92