joshualitt | 7f6a1e0 | 2016-01-22 11:21:43 -0800 | [diff] [blame] | 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 "GrCaps.h" |
| 9 | #include "GrContextFactory.h" |
joshualitt | 24dd687 | 2016-02-25 08:37:54 -0800 | [diff] [blame] | 10 | |
| 11 | #include "Request.h" |
| 12 | |
joshualitt | 7f6a1e0 | 2016-01-22 11:21:43 -0800 | [diff] [blame] | 13 | #include "SkCanvas.h" |
| 14 | #include "SkCommandLineFlags.h" |
joshualitt | 609d979 | 2016-01-27 11:07:23 -0800 | [diff] [blame] | 15 | #include "SkJSONCanvas.h" |
joshualitt | 792345f | 2016-02-02 13:02:33 -0800 | [diff] [blame] | 16 | #include "SkPictureRecorder.h" |
| 17 | #include "SkPixelSerializer.h" |
joshualitt | cdad12f | 2016-02-08 07:08:21 -0800 | [diff] [blame] | 18 | |
joshualitt | 7f6a1e0 | 2016-01-22 11:21:43 -0800 | [diff] [blame] | 19 | #include <sys/socket.h> |
joshualitt | 7f6a1e0 | 2016-01-22 11:21:43 -0800 | [diff] [blame] | 20 | #include <microhttpd.h> |
| 21 | |
| 22 | // To get image decoders linked in we have to do the below magic |
| 23 | #include "SkForceLinking.h" |
| 24 | #include "SkImageDecoder.h" |
| 25 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 26 | |
jcgregorio | 21ab120 | 2016-01-28 06:24:19 -0800 | [diff] [blame] | 27 | DEFINE_string(source, "https://debugger.skia.org", "Where to load the web UI from."); |
| 28 | DEFINE_int32(port, 8888, "The port to listen on."); |
joshualitt | 7f6a1e0 | 2016-01-22 11:21:43 -0800 | [diff] [blame] | 29 | |
joshualitt | 7f6a1e0 | 2016-01-22 11:21:43 -0800 | [diff] [blame] | 30 | SkString generateTemplate(SkString source) { |
| 31 | SkString debuggerTemplate; |
| 32 | debuggerTemplate.appendf( |
| 33 | "<!DOCTYPE html>\n" |
| 34 | "<html>\n" |
| 35 | "<head>\n" |
| 36 | " <title>SkDebugger</title>\n" |
| 37 | " <meta charset=\"utf-8\" />\n" |
| 38 | " <meta http-equiv=\"X-UA-Compatible\" content=\"IE=egde,chrome=1\">\n" |
| 39 | " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" |
| 40 | " <script src=\"%s/res/js/core.js\" type=\"text/javascript\" charset=\"utf-8\"></script>\n" |
jcgregorio | 50fe38e | 2016-02-25 07:02:13 -0800 | [diff] [blame] | 41 | " <link href=\"%s/res/vul/elements.html\" rel=\"import\" />\n" |
| 42 | " <link rel='shortcut icon' href='https://debugger.skia.org/res/img/favicon.ico' type='image/x-icon'/ >" |
joshualitt | 7f6a1e0 | 2016-01-22 11:21:43 -0800 | [diff] [blame] | 43 | "</head>\n" |
| 44 | "<body class=\"fullbleed layout vertical\">\n" |
| 45 | " <debugger-app-sk>This is the app." |
| 46 | " </debugger-app-sk>\n" |
| 47 | "</body>\n" |
| 48 | "</html>", source.c_str(), source.c_str()); |
| 49 | return debuggerTemplate; |
| 50 | |
| 51 | } |
| 52 | |
joshualitt | 9a4e188 | 2016-01-27 07:03:29 -0800 | [diff] [blame] | 53 | static const size_t kBufferSize = 1024; |
| 54 | |
| 55 | static int process_upload_data(void* cls, enum MHD_ValueKind kind, |
| 56 | const char* key, const char* filename, |
| 57 | const char* content_type, const char* transfer_encoding, |
| 58 | const char* data, uint64_t off, size_t size) { |
| 59 | struct UploadContext* uc = reinterpret_cast<UploadContext*>(cls); |
| 60 | |
| 61 | if (0 != size) { |
joshualitt | 609d979 | 2016-01-27 11:07:23 -0800 | [diff] [blame] | 62 | uc->fStream.write(data, size); |
joshualitt | 9a4e188 | 2016-01-27 07:03:29 -0800 | [diff] [blame] | 63 | } |
| 64 | return MHD_YES; |
| 65 | } |
| 66 | |
jcgregorio | 12d47ce | 2016-02-10 14:10:37 -0800 | [diff] [blame] | 67 | // SendOK just sends an empty response with a 200 OK status code. |
| 68 | static int SendOK(MHD_Connection* connection) { |
| 69 | const char* data = ""; |
| 70 | |
| 71 | MHD_Response* response = MHD_create_response_from_buffer(strlen(data), |
| 72 | (void*)data, |
| 73 | MHD_RESPMEM_PERSISTENT); |
| 74 | int ret = MHD_queue_response(connection, 200, response); |
| 75 | MHD_destroy_response(response); |
| 76 | return ret; |
| 77 | } |
| 78 | |
ethannicholas | 85fca85 | 2016-02-19 08:40:59 -0800 | [diff] [blame] | 79 | static int SendError(MHD_Connection* connection, const char* msg) { |
| 80 | MHD_Response* response = MHD_create_response_from_buffer(strlen(msg), |
| 81 | (void*) msg, |
| 82 | MHD_RESPMEM_PERSISTENT); |
| 83 | int ret = MHD_queue_response(connection, 500, response); |
| 84 | MHD_destroy_response(response); |
| 85 | return ret; |
| 86 | } |
| 87 | |
joshualitt | 792345f | 2016-02-02 13:02:33 -0800 | [diff] [blame] | 88 | static int SendData(MHD_Connection* connection, const SkData* data, const char* type, |
| 89 | bool setContentDisposition = false, const char* dispositionString = nullptr) { |
joshualitt | ccfdaa5 | 2016-01-27 07:40:29 -0800 | [diff] [blame] | 90 | MHD_Response* response = MHD_create_response_from_buffer(data->size(), |
| 91 | const_cast<void*>(data->data()), |
| 92 | MHD_RESPMEM_MUST_COPY); |
joshualitt | 609d979 | 2016-01-27 11:07:23 -0800 | [diff] [blame] | 93 | MHD_add_response_header(response, "Content-Type", type); |
joshualitt | ccfdaa5 | 2016-01-27 07:40:29 -0800 | [diff] [blame] | 94 | |
joshualitt | 792345f | 2016-02-02 13:02:33 -0800 | [diff] [blame] | 95 | if (setContentDisposition) { |
| 96 | MHD_add_response_header(response, "Content-Disposition", dispositionString); |
| 97 | } |
| 98 | |
joshualitt | ccfdaa5 | 2016-01-27 07:40:29 -0800 | [diff] [blame] | 99 | int ret = MHD_queue_response(connection, MHD_HTTP_OK, response); |
| 100 | MHD_destroy_response(response); |
| 101 | return ret; |
| 102 | } |
| 103 | |
ethannicholas | 3ff5d8c | 2016-02-22 08:59:57 -0800 | [diff] [blame] | 104 | static int SendJSON(MHD_Connection* connection, Request* request, int n) { |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame^] | 105 | SkCanvas* canvas = request->getCanvas(); |
ethannicholas | 3ff5d8c | 2016-02-22 08:59:57 -0800 | [diff] [blame] | 106 | SkDebugCanvas* debugCanvas = request->fDebugCanvas; |
| 107 | UrlDataManager* urlDataManager = &request->fUrlDataManager; |
joshualitt | 6b3cf73 | 2016-02-17 11:20:26 -0800 | [diff] [blame] | 108 | Json::Value root = debugCanvas->toJSON(*urlDataManager, n, canvas); |
ethannicholas | 3ff5d8c | 2016-02-22 08:59:57 -0800 | [diff] [blame] | 109 | root["mode"] = Json::Value(request->fGPUEnabled ? "gpu" : "cpu"); |
joshualitt | 609d979 | 2016-01-27 11:07:23 -0800 | [diff] [blame] | 110 | SkDynamicMemoryWStream stream; |
joshualitt | db6a254 | 2016-02-11 07:09:51 -0800 | [diff] [blame] | 111 | stream.writeText(Json::FastWriter().write(root).c_str()); |
joshualitt | 609d979 | 2016-01-27 11:07:23 -0800 | [diff] [blame] | 112 | |
| 113 | SkAutoTUnref<SkData> data(stream.copyToData()); |
| 114 | return SendData(connection, data, "application/json"); |
| 115 | } |
| 116 | |
| 117 | static int SendTemplate(MHD_Connection* connection, bool redirect = false, |
| 118 | const char* redirectUrl = nullptr) { |
jcgregorio | 21ab120 | 2016-01-28 06:24:19 -0800 | [diff] [blame] | 119 | SkString debuggerTemplate = generateTemplate(SkString(FLAGS_source[0])); |
joshualitt | ccfdaa5 | 2016-01-27 07:40:29 -0800 | [diff] [blame] | 120 | |
| 121 | MHD_Response* response = MHD_create_response_from_buffer( |
| 122 | debuggerTemplate.size(), |
| 123 | (void*) const_cast<char*>(debuggerTemplate.c_str()), |
| 124 | MHD_RESPMEM_MUST_COPY); |
jcgregorio | 6a2046e | 2016-01-28 05:31:31 -0800 | [diff] [blame] | 125 | MHD_add_response_header (response, "Access-Control-Allow-Origin", "*"); |
joshualitt | ccfdaa5 | 2016-01-27 07:40:29 -0800 | [diff] [blame] | 126 | |
jcgregorio | 6f17bc5 | 2016-01-27 11:44:38 -0800 | [diff] [blame] | 127 | int status = MHD_HTTP_OK; |
| 128 | |
joshualitt | 609d979 | 2016-01-27 11:07:23 -0800 | [diff] [blame] | 129 | if (redirect) { |
| 130 | MHD_add_response_header (response, "Location", redirectUrl); |
jcgregorio | 6f17bc5 | 2016-01-27 11:44:38 -0800 | [diff] [blame] | 131 | status = MHD_HTTP_SEE_OTHER; |
joshualitt | 609d979 | 2016-01-27 11:07:23 -0800 | [diff] [blame] | 132 | } |
| 133 | |
jcgregorio | 6f17bc5 | 2016-01-27 11:44:38 -0800 | [diff] [blame] | 134 | int ret = MHD_queue_response(connection, status, response); |
joshualitt | ccfdaa5 | 2016-01-27 07:40:29 -0800 | [diff] [blame] | 135 | MHD_destroy_response(response); |
| 136 | return ret; |
| 137 | } |
| 138 | |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 139 | class UrlHandler { |
| 140 | public: |
| 141 | virtual ~UrlHandler() {} |
| 142 | virtual bool canHandle(const char* method, const char* url) = 0; |
| 143 | virtual int handle(Request* request, MHD_Connection* connection, |
joshualitt | 136f517 | 2016-02-02 11:07:39 -0800 | [diff] [blame] | 144 | const char* url, const char* method, |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 145 | const char* upload_data, size_t* upload_data_size) = 0; |
| 146 | }; |
joshualitt | ccfdaa5 | 2016-01-27 07:40:29 -0800 | [diff] [blame] | 147 | |
joshualitt | 29e5a89 | 2016-02-04 06:08:33 -0800 | [diff] [blame] | 148 | class CmdHandler : public UrlHandler { |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 149 | public: |
| 150 | bool canHandle(const char* method, const char* url) override { |
joshualitt | 136f517 | 2016-02-02 11:07:39 -0800 | [diff] [blame] | 151 | const char* kBasePath = "/cmd"; |
| 152 | return 0 == strncmp(url, kBasePath, strlen(kBasePath)); |
joshualitt | ccfdaa5 | 2016-01-27 07:40:29 -0800 | [diff] [blame] | 153 | } |
| 154 | |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 155 | int handle(Request* request, MHD_Connection* connection, |
joshualitt | 136f517 | 2016-02-02 11:07:39 -0800 | [diff] [blame] | 156 | const char* url, const char* method, |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 157 | const char* upload_data, size_t* upload_data_size) override { |
joshualitt | 136f517 | 2016-02-02 11:07:39 -0800 | [diff] [blame] | 158 | SkTArray<SkString> commands; |
| 159 | SkStrSplit(url, "/", &commands); |
| 160 | |
| 161 | if (!request->fPicture.get() || commands.count() > 3) { |
| 162 | return MHD_NO; |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 163 | } |
joshualitt | 136f517 | 2016-02-02 11:07:39 -0800 | [diff] [blame] | 164 | |
joshualitt | db6a254 | 2016-02-11 07:09:51 -0800 | [diff] [blame] | 165 | // /cmd or /cmd/N |
| 166 | if (0 == strcmp(method, MHD_HTTP_METHOD_GET)) { |
| 167 | int n; |
| 168 | if (commands.count() == 1) { |
ethannicholas | 0a0520a | 2016-02-12 12:06:53 -0800 | [diff] [blame] | 169 | n = request->fDebugCanvas->getSize() - 1; |
joshualitt | db6a254 | 2016-02-11 07:09:51 -0800 | [diff] [blame] | 170 | } else { |
| 171 | sscanf(commands[1].c_str(), "%d", &n); |
| 172 | } |
ethannicholas | 3ff5d8c | 2016-02-22 08:59:57 -0800 | [diff] [blame] | 173 | return SendJSON(connection, request, n); |
joshualitt | 136f517 | 2016-02-02 11:07:39 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // /cmd/N, for now only delete supported |
| 177 | if (commands.count() == 2 && 0 == strcmp(method, MHD_HTTP_METHOD_DELETE)) { |
| 178 | int n; |
| 179 | sscanf(commands[1].c_str(), "%d", &n); |
| 180 | request->fDebugCanvas->deleteDrawCommandAt(n); |
jcgregorio | 12d47ce | 2016-02-10 14:10:37 -0800 | [diff] [blame] | 181 | return SendOK(connection); |
joshualitt | 136f517 | 2016-02-02 11:07:39 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // /cmd/N/[0|1] |
| 185 | if (commands.count() == 3 && 0 == strcmp(method, MHD_HTTP_METHOD_POST)) { |
| 186 | int n, toggle; |
| 187 | sscanf(commands[1].c_str(), "%d", &n); |
| 188 | sscanf(commands[2].c_str(), "%d", &toggle); |
| 189 | request->fDebugCanvas->toggleCommand(n, toggle); |
jcgregorio | 12d47ce | 2016-02-10 14:10:37 -0800 | [diff] [blame] | 190 | return SendOK(connection); |
joshualitt | 136f517 | 2016-02-02 11:07:39 -0800 | [diff] [blame] | 191 | } |
| 192 | |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 193 | return MHD_NO; |
| 194 | } |
| 195 | }; |
| 196 | |
| 197 | class ImgHandler : public UrlHandler { |
| 198 | public: |
| 199 | bool canHandle(const char* method, const char* url) override { |
| 200 | static const char* kBasePath = "/img"; |
| 201 | return 0 == strcmp(method, MHD_HTTP_METHOD_GET) && |
| 202 | 0 == strncmp(url, kBasePath, strlen(kBasePath)); |
joshualitt | ccfdaa5 | 2016-01-27 07:40:29 -0800 | [diff] [blame] | 203 | } |
| 204 | |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 205 | int handle(Request* request, MHD_Connection* connection, |
joshualitt | 136f517 | 2016-02-02 11:07:39 -0800 | [diff] [blame] | 206 | const char* url, const char* method, |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 207 | const char* upload_data, size_t* upload_data_size) override { |
joshualitt | 136f517 | 2016-02-02 11:07:39 -0800 | [diff] [blame] | 208 | SkTArray<SkString> commands; |
| 209 | SkStrSplit(url, "/", &commands); |
| 210 | |
| 211 | if (!request->fPicture.get() || commands.count() > 2) { |
| 212 | return MHD_NO; |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 213 | } |
joshualitt | a341b90 | 2016-02-02 07:37:21 -0800 | [diff] [blame] | 214 | |
joshualitt | 136f517 | 2016-02-02 11:07:39 -0800 | [diff] [blame] | 215 | int n; |
| 216 | // /img or /img/N |
| 217 | if (commands.count() == 1) { |
| 218 | n = request->fDebugCanvas->getSize() - 1; |
| 219 | } else { |
| 220 | sscanf(commands[1].c_str(), "%d", &n); |
| 221 | } |
| 222 | |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame^] | 223 | SkAutoTUnref<SkData> data(request->drawToPng(n)); |
joshualitt | 136f517 | 2016-02-02 11:07:39 -0800 | [diff] [blame] | 224 | return SendData(connection, data, "image/png"); |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 225 | } |
| 226 | }; |
joshualitt | ccfdaa5 | 2016-01-27 07:40:29 -0800 | [diff] [blame] | 227 | |
ethannicholas | 3ca1e1a | 2016-02-18 10:22:34 -0800 | [diff] [blame] | 228 | class BreakHandler : public UrlHandler { |
| 229 | public: |
| 230 | bool canHandle(const char* method, const char* url) override { |
| 231 | static const char* kBasePath = "/break"; |
| 232 | return 0 == strcmp(method, MHD_HTTP_METHOD_GET) && |
| 233 | 0 == strncmp(url, kBasePath, strlen(kBasePath)); |
| 234 | } |
| 235 | |
| 236 | static SkColor GetPixel(Request* request, int x, int y) { |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame^] | 237 | SkCanvas* canvas = request->getCanvas(); |
ethannicholas | 3ca1e1a | 2016-02-18 10:22:34 -0800 | [diff] [blame] | 238 | canvas->flush(); |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame^] | 239 | SkAutoTDelete<SkBitmap> bitmap(request->getBitmapFromCanvas(canvas)); |
ethannicholas | 3ca1e1a | 2016-02-18 10:22:34 -0800 | [diff] [blame] | 240 | SkASSERT(bitmap); |
| 241 | bitmap->lockPixels(); |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame^] | 242 | uint8_t* start = ((uint8_t*) bitmap->getPixels()) + (y * Request::kImageWidth + x) * 4; |
ethannicholas | 3ca1e1a | 2016-02-18 10:22:34 -0800 | [diff] [blame] | 243 | SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]); |
| 244 | bitmap->unlockPixels(); |
| 245 | return result; |
| 246 | } |
| 247 | |
| 248 | int handle(Request* request, MHD_Connection* connection, |
| 249 | const char* url, const char* method, |
| 250 | const char* upload_data, size_t* upload_data_size) override { |
| 251 | SkTArray<SkString> commands; |
| 252 | SkStrSplit(url, "/", &commands); |
| 253 | |
| 254 | if (!request->fPicture.get() || commands.count() != 4) { |
| 255 | return MHD_NO; |
| 256 | } |
| 257 | |
| 258 | // /break/<n>/<x>/<y> |
| 259 | int n; |
| 260 | sscanf(commands[1].c_str(), "%d", &n); |
| 261 | int x; |
| 262 | sscanf(commands[2].c_str(), "%d", &x); |
| 263 | int y; |
| 264 | sscanf(commands[3].c_str(), "%d", &y); |
| 265 | |
| 266 | int count = request->fDebugCanvas->getSize(); |
| 267 | SkASSERT(n < count); |
| 268 | |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame^] | 269 | SkCanvas* canvas = request->getCanvas(); |
ethannicholas | 3ca1e1a | 2016-02-18 10:22:34 -0800 | [diff] [blame] | 270 | canvas->clear(SK_ColorWHITE); |
| 271 | int saveCount = canvas->save(); |
| 272 | for (int i = 0; i <= n; ++i) { |
| 273 | request->fDebugCanvas->getDrawCommandAt(i)->execute(canvas); |
| 274 | } |
| 275 | SkColor target = GetPixel(request, x, y); |
| 276 | Json::Value response(Json::objectValue); |
| 277 | Json::Value startColor(Json::arrayValue); |
| 278 | startColor.append(Json::Value(SkColorGetR(target))); |
| 279 | startColor.append(Json::Value(SkColorGetG(target))); |
| 280 | startColor.append(Json::Value(SkColorGetB(target))); |
| 281 | startColor.append(Json::Value(SkColorGetA(target))); |
| 282 | response["startColor"] = startColor; |
| 283 | response["endColor"] = startColor; |
| 284 | response["endOp"] = Json::Value(n); |
| 285 | for (int i = n + 1; i < n + count; ++i) { |
| 286 | int index = i % count; |
| 287 | if (index == 0) { |
| 288 | // reset canvas for wraparound |
| 289 | canvas->restoreToCount(saveCount); |
| 290 | canvas->clear(SK_ColorWHITE); |
| 291 | saveCount = canvas->save(); |
| 292 | } |
| 293 | request->fDebugCanvas->getDrawCommandAt(index)->execute(canvas); |
| 294 | SkColor current = GetPixel(request, x, y); |
| 295 | if (current != target) { |
| 296 | Json::Value endColor(Json::arrayValue); |
| 297 | endColor.append(Json::Value(SkColorGetR(current))); |
| 298 | endColor.append(Json::Value(SkColorGetG(current))); |
| 299 | endColor.append(Json::Value(SkColorGetB(current))); |
| 300 | endColor.append(Json::Value(SkColorGetA(current))); |
| 301 | response["endColor"] = endColor; |
| 302 | response["endOp"] = Json::Value(index); |
| 303 | break; |
| 304 | } |
| 305 | } |
| 306 | canvas->restoreToCount(saveCount); |
| 307 | SkDynamicMemoryWStream stream; |
| 308 | stream.writeText(Json::FastWriter().write(response).c_str()); |
| 309 | SkAutoTUnref<SkData> data(stream.copyToData()); |
| 310 | return SendData(connection, data, "application/json"); |
| 311 | } |
| 312 | }; |
| 313 | |
ethannicholas | 0a0520a | 2016-02-12 12:06:53 -0800 | [diff] [blame] | 314 | /** |
| 315 | Updates the clip visualization alpha. On all subsequent /img requests, the clip will be drawn in |
| 316 | black with the specified alpha. 0 = no visible clip, 255 = fully opaque clip. |
| 317 | */ |
| 318 | class ClipAlphaHandler : public UrlHandler { |
| 319 | public: |
| 320 | bool canHandle(const char* method, const char* url) override { |
| 321 | static const char* kBasePath = "/clipAlpha/"; |
jcgregorio | 3341d42 | 2016-02-16 10:31:07 -0800 | [diff] [blame] | 322 | return 0 == strcmp(method, MHD_HTTP_METHOD_POST) && |
ethannicholas | 0a0520a | 2016-02-12 12:06:53 -0800 | [diff] [blame] | 323 | 0 == strncmp(url, kBasePath, strlen(kBasePath)); |
| 324 | } |
| 325 | |
| 326 | int handle(Request* request, MHD_Connection* connection, |
| 327 | const char* url, const char* method, |
| 328 | const char* upload_data, size_t* upload_data_size) override { |
| 329 | SkTArray<SkString> commands; |
| 330 | SkStrSplit(url, "/", &commands); |
| 331 | |
| 332 | if (!request->fPicture.get() || commands.count() != 2) { |
| 333 | return MHD_NO; |
| 334 | } |
| 335 | |
| 336 | int alpha; |
| 337 | sscanf(commands[1].c_str(), "%d", &alpha); |
| 338 | |
| 339 | request->fDebugCanvas->setClipVizColor(SkColorSetARGB(alpha, 0, 0, 0)); |
| 340 | return SendOK(connection); |
| 341 | } |
| 342 | }; |
| 343 | |
ethannicholas | 85fca85 | 2016-02-19 08:40:59 -0800 | [diff] [blame] | 344 | /** |
| 345 | Controls whether GPU rendering is enabled. Posting to /enableGPU/1 turns GPU on, /enableGPU/0 |
| 346 | disables it. |
| 347 | */ |
| 348 | class EnableGPUHandler : public UrlHandler { |
| 349 | public: |
| 350 | bool canHandle(const char* method, const char* url) override { |
| 351 | static const char* kBasePath = "/enableGPU/"; |
| 352 | return 0 == strcmp(method, MHD_HTTP_METHOD_POST) && |
| 353 | 0 == strncmp(url, kBasePath, strlen(kBasePath)); |
| 354 | } |
| 355 | |
| 356 | int handle(Request* request, MHD_Connection* connection, |
| 357 | const char* url, const char* method, |
| 358 | const char* upload_data, size_t* upload_data_size) override { |
| 359 | SkTArray<SkString> commands; |
| 360 | SkStrSplit(url, "/", &commands); |
| 361 | |
| 362 | if (commands.count() != 2) { |
| 363 | return MHD_NO; |
| 364 | } |
| 365 | |
| 366 | int enable; |
| 367 | sscanf(commands[1].c_str(), "%d", &enable); |
| 368 | |
| 369 | if (enable) { |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame^] | 370 | SkSurface* surface = request->createGPUSurface(); |
ethannicholas | 85fca85 | 2016-02-19 08:40:59 -0800 | [diff] [blame] | 371 | if (surface) { |
| 372 | request->fSurface.reset(surface); |
ethannicholas | 3ff5d8c | 2016-02-22 08:59:57 -0800 | [diff] [blame] | 373 | request->fGPUEnabled = true; |
ethannicholas | 85fca85 | 2016-02-19 08:40:59 -0800 | [diff] [blame] | 374 | return SendOK(connection); |
| 375 | } |
| 376 | return SendError(connection, "Unable to create GPU surface"); |
| 377 | } |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame^] | 378 | request->fSurface.reset(request->createCPUSurface()); |
ethannicholas | 3ff5d8c | 2016-02-22 08:59:57 -0800 | [diff] [blame] | 379 | request->fGPUEnabled = false; |
ethannicholas | 85fca85 | 2016-02-19 08:40:59 -0800 | [diff] [blame] | 380 | return SendOK(connection); |
| 381 | } |
| 382 | }; |
| 383 | |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 384 | class PostHandler : public UrlHandler { |
| 385 | public: |
| 386 | bool canHandle(const char* method, const char* url) override { |
| 387 | return 0 == strcmp(method, MHD_HTTP_METHOD_POST) && |
| 388 | 0 == strcmp(url, "/new"); |
joshualitt | ccfdaa5 | 2016-01-27 07:40:29 -0800 | [diff] [blame] | 389 | } |
| 390 | |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 391 | int handle(Request* request, MHD_Connection* connection, |
joshualitt | 136f517 | 2016-02-02 11:07:39 -0800 | [diff] [blame] | 392 | const char* url, const char* method, |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 393 | const char* upload_data, size_t* upload_data_size) override { |
| 394 | UploadContext* uc = request->fUploadContext; |
joshualitt | ccfdaa5 | 2016-01-27 07:40:29 -0800 | [diff] [blame] | 395 | |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 396 | // New connection |
| 397 | if (!uc) { |
| 398 | // TODO make this a method on request |
| 399 | uc = new UploadContext; |
| 400 | uc->connection = connection; |
| 401 | uc->fPostProcessor = MHD_create_post_processor(connection, kBufferSize, |
| 402 | &process_upload_data, uc); |
| 403 | SkASSERT(uc->fPostProcessor); |
joshualitt | 609d979 | 2016-01-27 11:07:23 -0800 | [diff] [blame] | 404 | |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 405 | request->fUploadContext = uc; |
| 406 | return MHD_YES; |
| 407 | } |
| 408 | |
| 409 | // in process upload |
| 410 | if (0 != *upload_data_size) { |
| 411 | SkASSERT(uc->fPostProcessor); |
| 412 | MHD_post_process(uc->fPostProcessor, upload_data, *upload_data_size); |
| 413 | *upload_data_size = 0; |
| 414 | return MHD_YES; |
| 415 | } |
| 416 | |
| 417 | // end of upload |
| 418 | MHD_destroy_post_processor(uc->fPostProcessor); |
| 419 | uc->fPostProcessor = nullptr; |
| 420 | |
joshualitt | 136f517 | 2016-02-02 11:07:39 -0800 | [diff] [blame] | 421 | // parse picture from stream |
| 422 | request->fPicture.reset( |
| 423 | SkPicture::CreateFromStream(request->fUploadContext->fStream.detachAsStream())); |
| 424 | if (!request->fPicture.get()) { |
| 425 | fprintf(stderr, "Could not create picture from stream.\n"); |
| 426 | return MHD_NO; |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 427 | } |
| 428 | |
joshualitt | 136f517 | 2016-02-02 11:07:39 -0800 | [diff] [blame] | 429 | // pour picture into debug canvas |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame^] | 430 | request->fDebugCanvas.reset(new SkDebugCanvas(Request::kImageWidth, |
| 431 | Request::kImageHeight)); |
joshualitt | 136f517 | 2016-02-02 11:07:39 -0800 | [diff] [blame] | 432 | request->fDebugCanvas->drawPicture(request->fPicture); |
| 433 | |
joshualitt | a341b90 | 2016-02-02 07:37:21 -0800 | [diff] [blame] | 434 | // clear upload context |
| 435 | delete request->fUploadContext; |
| 436 | request->fUploadContext = nullptr; |
| 437 | |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 438 | return SendTemplate(connection, true, "/"); |
joshualitt | ccfdaa5 | 2016-01-27 07:40:29 -0800 | [diff] [blame] | 439 | } |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 440 | }; |
| 441 | |
joshualitt | 792345f | 2016-02-02 13:02:33 -0800 | [diff] [blame] | 442 | class DownloadHandler : public UrlHandler { |
| 443 | public: |
| 444 | bool canHandle(const char* method, const char* url) override { |
| 445 | return 0 == strcmp(method, MHD_HTTP_METHOD_GET) && |
| 446 | 0 == strcmp(url, "/download"); |
| 447 | } |
| 448 | |
| 449 | int handle(Request* request, MHD_Connection* connection, |
| 450 | const char* url, const char* method, |
| 451 | const char* upload_data, size_t* upload_data_size) override { |
| 452 | if (!request->fPicture.get()) { |
| 453 | return MHD_NO; |
| 454 | } |
| 455 | |
| 456 | // TODO move to a function |
| 457 | // Playback into picture recorder |
| 458 | SkPictureRecorder recorder; |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame^] | 459 | SkCanvas* canvas = recorder.beginRecording(Request::kImageWidth, |
| 460 | Request::kImageHeight); |
joshualitt | 792345f | 2016-02-02 13:02:33 -0800 | [diff] [blame] | 461 | |
| 462 | request->fDebugCanvas->draw(canvas); |
| 463 | |
| 464 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 465 | |
| 466 | SkDynamicMemoryWStream outStream; |
| 467 | |
| 468 | SkAutoTUnref<SkPixelSerializer> serializer(SkImageEncoder::CreatePixelSerializer()); |
| 469 | picture->serialize(&outStream, serializer); |
| 470 | |
| 471 | SkAutoTUnref<SkData> data(outStream.copyToData()); |
| 472 | |
| 473 | // TODO fancier name handling |
| 474 | return SendData(connection, data, "application/octet-stream", true, |
| 475 | "attachment; filename=something.skp;"); |
| 476 | } |
| 477 | }; |
| 478 | |
joshualitt | 29e5a89 | 2016-02-04 06:08:33 -0800 | [diff] [blame] | 479 | class InfoHandler : public UrlHandler { |
| 480 | public: |
| 481 | bool canHandle(const char* method, const char* url) override { |
| 482 | const char* kBaseName = "/info"; |
| 483 | return 0 == strcmp(method, MHD_HTTP_METHOD_GET) && |
| 484 | 0 == strncmp(url, kBaseName, strlen(kBaseName)); |
| 485 | } |
| 486 | |
| 487 | int handle(Request* request, MHD_Connection* connection, |
| 488 | const char* url, const char* method, |
| 489 | const char* upload_data, size_t* upload_data_size) override { |
| 490 | SkTArray<SkString> commands; |
| 491 | SkStrSplit(url, "/", &commands); |
| 492 | |
| 493 | if (!request->fPicture.get() || commands.count() > 2) { |
| 494 | return MHD_NO; |
| 495 | } |
| 496 | |
| 497 | // drawTo |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame^] | 498 | SkAutoTUnref<SkSurface> surface(request->createCPUSurface()); |
joshualitt | 29e5a89 | 2016-02-04 06:08:33 -0800 | [diff] [blame] | 499 | SkCanvas* canvas = surface->getCanvas(); |
| 500 | |
| 501 | int n; |
| 502 | // /info or /info/N |
| 503 | if (commands.count() == 1) { |
| 504 | n = request->fDebugCanvas->getSize() - 1; |
| 505 | } else { |
| 506 | sscanf(commands[1].c_str(), "%d", &n); |
| 507 | } |
| 508 | |
| 509 | // TODO this is really slow and we should cache the matrix and clip |
| 510 | request->fDebugCanvas->drawTo(canvas, n); |
| 511 | |
| 512 | // make some json |
| 513 | SkMatrix vm = request->fDebugCanvas->getCurrentMatrix(); |
| 514 | SkIRect clip = request->fDebugCanvas->getCurrentClip(); |
| 515 | Json::Value info(Json::objectValue); |
| 516 | info["ViewMatrix"] = SkJSONCanvas::MakeMatrix(vm); |
| 517 | info["ClipRect"] = SkJSONCanvas::MakeIRect(clip); |
| 518 | |
| 519 | std::string json = Json::FastWriter().write(info); |
| 520 | |
| 521 | // We don't want the null terminator so strlen is correct |
| 522 | SkAutoTUnref<SkData> data(SkData::NewWithCopy(json.c_str(), strlen(json.c_str()))); |
| 523 | return SendData(connection, data, "application/json"); |
| 524 | } |
| 525 | }; |
| 526 | |
joshualitt | cdad12f | 2016-02-08 07:08:21 -0800 | [diff] [blame] | 527 | class DataHandler : public UrlHandler { |
| 528 | public: |
| 529 | bool canHandle(const char* method, const char* url) override { |
| 530 | static const char* kBaseUrl = "/data"; |
| 531 | return 0 == strcmp(method, MHD_HTTP_METHOD_GET) && |
| 532 | 0 == strncmp(url, kBaseUrl, strlen(kBaseUrl)); |
| 533 | } |
| 534 | |
| 535 | int handle(Request* request, MHD_Connection* connection, |
| 536 | const char* url, const char* method, |
| 537 | const char* upload_data, size_t* upload_data_size) override { |
| 538 | SkTArray<SkString> commands; |
| 539 | SkStrSplit(url, "/", &commands); |
| 540 | |
| 541 | if (!request->fPicture.get() || commands.count() != 2) { |
| 542 | return MHD_NO; |
| 543 | } |
| 544 | |
| 545 | SkAutoTUnref<UrlDataManager::UrlData> urlData( |
| 546 | SkRef(request->fUrlDataManager.getDataFromUrl(SkString(url)))); |
| 547 | |
| 548 | if (urlData) { |
| 549 | return SendData(connection, urlData->fData.get(), urlData->fContentType.c_str()); |
| 550 | } |
| 551 | return MHD_NO; |
| 552 | } |
| 553 | }; |
joshualitt | 29e5a89 | 2016-02-04 06:08:33 -0800 | [diff] [blame] | 554 | |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 555 | class RootHandler : public UrlHandler { |
| 556 | public: |
| 557 | bool canHandle(const char* method, const char* url) override { |
| 558 | return 0 == strcmp(method, MHD_HTTP_METHOD_GET) && |
| 559 | 0 == strcmp(url, "/"); |
| 560 | } |
| 561 | |
| 562 | int handle(Request* request, MHD_Connection* connection, |
joshualitt | 136f517 | 2016-02-02 11:07:39 -0800 | [diff] [blame] | 563 | const char* url, const char* method, |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 564 | const char* upload_data, size_t* upload_data_size) override { |
| 565 | return SendTemplate(connection); |
| 566 | } |
| 567 | }; |
joshualitt | ccfdaa5 | 2016-01-27 07:40:29 -0800 | [diff] [blame] | 568 | |
| 569 | class UrlManager { |
| 570 | public: |
| 571 | UrlManager() { |
| 572 | // Register handlers |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 573 | fHandlers.push_back(new RootHandler); |
| 574 | fHandlers.push_back(new PostHandler); |
| 575 | fHandlers.push_back(new ImgHandler); |
ethannicholas | 0a0520a | 2016-02-12 12:06:53 -0800 | [diff] [blame] | 576 | fHandlers.push_back(new ClipAlphaHandler); |
ethannicholas | 85fca85 | 2016-02-19 08:40:59 -0800 | [diff] [blame] | 577 | fHandlers.push_back(new EnableGPUHandler); |
joshualitt | 29e5a89 | 2016-02-04 06:08:33 -0800 | [diff] [blame] | 578 | fHandlers.push_back(new CmdHandler); |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 579 | fHandlers.push_back(new InfoHandler); |
joshualitt | 792345f | 2016-02-02 13:02:33 -0800 | [diff] [blame] | 580 | fHandlers.push_back(new DownloadHandler); |
joshualitt | cdad12f | 2016-02-08 07:08:21 -0800 | [diff] [blame] | 581 | fHandlers.push_back(new DataHandler); |
ethannicholas | 3ca1e1a | 2016-02-18 10:22:34 -0800 | [diff] [blame] | 582 | fHandlers.push_back(new BreakHandler); |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | ~UrlManager() { |
| 586 | for (int i = 0; i < fHandlers.count(); i++) { delete fHandlers[i]; } |
joshualitt | ccfdaa5 | 2016-01-27 07:40:29 -0800 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | // This is clearly not efficient for a large number of urls and handlers |
| 590 | int invoke(Request* request, MHD_Connection* connection, const char* url, const char* method, |
| 591 | const char* upload_data, size_t* upload_data_size) const { |
| 592 | for (int i = 0; i < fHandlers.count(); i++) { |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 593 | if (fHandlers[i]->canHandle(method, url)) { |
joshualitt | 136f517 | 2016-02-02 11:07:39 -0800 | [diff] [blame] | 594 | return fHandlers[i]->handle(request, connection, url, method, upload_data, |
| 595 | upload_data_size); |
joshualitt | ccfdaa5 | 2016-01-27 07:40:29 -0800 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | return MHD_NO; |
| 599 | } |
| 600 | |
| 601 | private: |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 602 | SkTArray<UrlHandler*> fHandlers; |
joshualitt | ccfdaa5 | 2016-01-27 07:40:29 -0800 | [diff] [blame] | 603 | }; |
| 604 | |
| 605 | const UrlManager kUrlManager; |
| 606 | |
joshualitt | 7f6a1e0 | 2016-01-22 11:21:43 -0800 | [diff] [blame] | 607 | int answer_to_connection(void* cls, struct MHD_Connection* connection, |
| 608 | const char* url, const char* method, const char* version, |
| 609 | const char* upload_data, size_t* upload_data_size, |
| 610 | void** con_cls) { |
joshualitt | 9a4e188 | 2016-01-27 07:03:29 -0800 | [diff] [blame] | 611 | SkDebugf("New %s request for %s using version %s\n", method, url, version); |
joshualitt | 7f6a1e0 | 2016-01-22 11:21:43 -0800 | [diff] [blame] | 612 | |
joshualitt | 9a4e188 | 2016-01-27 07:03:29 -0800 | [diff] [blame] | 613 | Request* request = reinterpret_cast<Request*>(cls); |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 614 | int result = kUrlManager.invoke(request, connection, url, method, upload_data, |
| 615 | upload_data_size); |
| 616 | if (MHD_NO == result) { |
joshualitt | 873d624 | 2016-02-08 13:57:44 -0800 | [diff] [blame] | 617 | fprintf(stderr, "Invalid method and / or url: %s %s\n", method, url); |
joshualitt | 483b901 | 2016-02-02 07:16:24 -0800 | [diff] [blame] | 618 | } |
| 619 | return result; |
joshualitt | 7f6a1e0 | 2016-01-22 11:21:43 -0800 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | int skiaserve_main() { |
joshualitt | cdad12f | 2016-02-08 07:08:21 -0800 | [diff] [blame] | 623 | Request request(SkString("/data")); // This simple server has one request |
ethannicholas | 85fca85 | 2016-02-19 08:40:59 -0800 | [diff] [blame] | 624 | |
| 625 | // create surface |
| 626 | GrContextOptions grContextOpts; |
| 627 | request.fContextFactory.reset(new GrContextFactory(grContextOpts)); |
joshualitt | 4dcbe43 | 2016-02-25 10:50:28 -0800 | [diff] [blame^] | 628 | request.fSurface.reset(request.createCPUSurface()); |
ethannicholas | 85fca85 | 2016-02-19 08:40:59 -0800 | [diff] [blame] | 629 | |
joshualitt | 7f6a1e0 | 2016-01-22 11:21:43 -0800 | [diff] [blame] | 630 | struct MHD_Daemon* daemon; |
jcgregorio | 21ab120 | 2016-01-28 06:24:19 -0800 | [diff] [blame] | 631 | // TODO Add option to bind this strictly to an address, e.g. localhost, for security. |
| 632 | daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, FLAGS_port, nullptr, nullptr, |
joshualitt | 9a4e188 | 2016-01-27 07:03:29 -0800 | [diff] [blame] | 633 | &answer_to_connection, &request, |
| 634 | MHD_OPTION_END); |
joshualitt | 7f6a1e0 | 2016-01-22 11:21:43 -0800 | [diff] [blame] | 635 | if (NULL == daemon) { |
| 636 | return 1; |
| 637 | } |
| 638 | |
| 639 | getchar(); |
| 640 | MHD_stop_daemon(daemon); |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | #if !defined SK_BUILD_FOR_IOS |
| 645 | int main(int argc, char** argv) { |
| 646 | SkCommandLineFlags::Parse(argc, argv); |
| 647 | return skiaserve_main(); |
| 648 | } |
| 649 | #endif |