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