aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/skiaserve/skiaserve.cpp
blob: 5947d3dc63e00c6121923414553a28c545a5c26f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
/*
 * 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 "GrCaps.h"
#include "GrContextFactory.h"

#include "Request.h"

#include "SkCanvas.h"
#include "SkCommandLineFlags.h"
#include "SkJSONCanvas.h"
#include "SkPictureRecorder.h"
#include "SkPixelSerializer.h"

#include <sys/socket.h>
#include <microhttpd.h>
#include "png.h"

// To get image decoders linked in we have to do the below magic
#include "SkForceLinking.h"
#include "SkImageDecoder.h"
__SK_FORCE_IMAGE_DECODER_LINKING;

DEFINE_string(source, "https://debugger.skia.org", "Where to load the web UI from.");
DEFINE_int32(port, 8888, "The port to listen on.");

// TODO probably want to make this configurable
static const int kImageWidth = 1920;
static const int kImageHeight = 1080;

SkString generateTemplate(SkString source) {
    SkString debuggerTemplate;
    debuggerTemplate.appendf(
        "<!DOCTYPE html>\n"
        "<html>\n"
        "<head>\n"
        "    <title>SkDebugger</title>\n"
        "    <meta charset=\"utf-8\" />\n"
        "    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=egde,chrome=1\">\n"
        "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n"
        "    <script src=\"%s/res/js/core.js\" type=\"text/javascript\" charset=\"utf-8\"></script>\n"
        "    <link href=\"%s/res/vul/elements.html\" rel=\"import\" />\n"
        "    <link rel='shortcut icon' href='https://debugger.skia.org/res/img/favicon.ico' type='image/x-icon'/ >"
        "</head>\n"
        "<body class=\"fullbleed layout vertical\">\n"
            "  <debugger-app-sk>This is the app."
            "  </debugger-app-sk>\n"
        "</body>\n"
        "</html>", source.c_str(), source.c_str());
    return debuggerTemplate;

}

static void write_png_callback(png_structp png_ptr, png_bytep data, png_size_t length) {
    SkWStream* out = (SkWStream*) png_get_io_ptr(png_ptr);
    out->write(data, length);
}

static void write_png(const png_bytep rgba, png_uint_32 width, png_uint_32 height, SkWStream& out) {
    png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    SkASSERT(png != nullptr);
    png_infop info_ptr = png_create_info_struct(png);
    SkASSERT(info_ptr != nullptr);
    if (setjmp(png_jmpbuf(png))) {
        SkFAIL("png encode error");
    }
    png_set_IHDR(png, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
                 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
    png_set_compression_level(png, 1);
    png_bytepp rows = (png_bytepp) sk_malloc_throw(height * sizeof(png_byte*));
    png_bytep pixels = (png_bytep) sk_malloc_throw(width * height * 3);
    for (png_size_t y = 0; y < height; ++y) {
        const png_bytep src = rgba + y * width * 4;
        rows[y] = pixels + y * width * 3;
        // convert from RGBA to RGB
        for (png_size_t x = 0; x < width; ++x) {
            rows[y][x * 3] = src[x * 4];
            rows[y][x * 3 + 1] = src[x * 4 + 1];
            rows[y][x * 3 + 2] = src[x * 4 + 2];
        }
    }
    png_set_filter(png, 0, PNG_NO_FILTERS);
    png_set_rows(png, info_ptr, &rows[0]);
    png_set_write_fn(png, &out, write_png_callback, NULL);
    png_write_png(png, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
    png_destroy_write_struct(&png, NULL);
    sk_free(rows);
}

SkBitmap* getBitmapFromCanvas(SkCanvas* canvas) {
    SkBitmap* bmp = new SkBitmap();
    SkImageInfo info = SkImageInfo::Make(kImageWidth, kImageHeight, kRGBA_8888_SkColorType,
                kOpaque_SkAlphaType);
    bmp->setInfo(info);
    if (!canvas->readPixels(bmp, 0, 0)) {
        fprintf(stderr, "Can't read pixels\n");
        return nullptr;
    }
    return bmp;
}

SkData* writeCanvasToPng(SkCanvas* canvas) {
    // capture pixels
    SkAutoTDelete<SkBitmap> bmp(getBitmapFromCanvas(canvas));
    SkASSERT(bmp);

    // write to png
    SkDynamicMemoryWStream buffer;
    write_png((const png_bytep) bmp->getPixels(), bmp->width(), bmp->height(), buffer);
    return buffer.copyToData();
}

SkCanvas* getCanvasFromRequest(Request* request) {
    GrContextFactory* factory = request->fContextFactory;
    SkGLContext* gl = factory->getContextInfo(GrContextFactory::kNative_GLContextType,
                                              GrContextFactory::kNone_GLContextOptions).fGLContext;
    gl->makeCurrent();
    SkASSERT(request->fDebugCanvas);
    SkCanvas* target = request->fSurface->getCanvas();
    return target;
}

void drawToCanvas(Request* request, int n) {
    SkCanvas* target = getCanvasFromRequest(request);
    request->fDebugCanvas->drawTo(target, n);
}

SkData* drawToPng(Request* request, int n) {
    drawToCanvas(request, n);
    return writeCanvasToPng(getCanvasFromRequest(request));
}

SkSurface* createCPUSurface() {
    SkImageInfo info = SkImageInfo::Make(kImageWidth, kImageHeight, kN32_SkColorType,
                                         kPremul_SkAlphaType);
    return SkSurface::NewRaster(info);
}

SkSurface* createGPUSurface(Request* request) {
    GrContext* context = request->fContextFactory->get(GrContextFactory::kNative_GLContextType,
                                                       GrContextFactory::kNone_GLContextOptions);
    int maxRTSize = context->caps()->maxRenderTargetSize();
    SkImageInfo info = SkImageInfo::Make(SkTMin(kImageWidth, maxRTSize),
                                         SkTMin(kImageHeight, maxRTSize),
                                         kN32_SkColorType, kPremul_SkAlphaType);
    uint32_t flags = 0;
    SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
    SkSurface* surface = SkSurface::NewRenderTarget(context, SkBudgeted::kNo, info, 0,
                                                    &props);
    return surface;
}

static const size_t kBufferSize = 1024;

static int process_upload_data(void* cls, enum MHD_ValueKind kind,
                               const char* key, const char* filename,
                               const char* content_type, const char* transfer_encoding,
                               const char* data, uint64_t off, size_t size) {
    struct UploadContext* uc = reinterpret_cast<UploadContext*>(cls);

    if (0 != size) {
        uc->fStream.write(data, size);
    }
    return MHD_YES;
}

// SendOK just sends an empty response with a 200 OK status code.
static int SendOK(MHD_Connection* connection) {
    const char* data = "";

    MHD_Response* response = MHD_create_response_from_buffer(strlen(data),
                                                             (void*)data,
                                                             MHD_RESPMEM_PERSISTENT);
    int ret = MHD_queue_response(connection, 200, response);
    MHD_destroy_response(response);
    return ret;
}

static int SendError(MHD_Connection* connection, const char* msg) {
    MHD_Response* response = MHD_create_response_from_buffer(strlen(msg),
                                                             (void*) msg,
                                                             MHD_RESPMEM_PERSISTENT);
    int ret = MHD_queue_response(connection, 500, response);
    MHD_destroy_response(response);
    return ret;
}

static int SendData(MHD_Connection* connection, const SkData* data, const char* type,
                    bool setContentDisposition = false, const char* dispositionString = nullptr) {
    MHD_Response* response = MHD_create_response_from_buffer(data->size(),
                                                             const_cast<void*>(data->data()),
                                                             MHD_RESPMEM_MUST_COPY);
    MHD_add_response_header(response, "Content-Type", type);

    if (setContentDisposition) {
        MHD_add_response_header(response, "Content-Disposition", dispositionString);
    }

    int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
    MHD_destroy_response(response);
    return ret;
}

static int SendJSON(MHD_Connection* connection, Request* request, int n) {
    SkCanvas* canvas = getCanvasFromRequest(request);
    SkDebugCanvas* debugCanvas = request->fDebugCanvas;
    UrlDataManager* urlDataManager = &request->fUrlDataManager;
    Json::Value root = debugCanvas->toJSON(*urlDataManager, n, canvas);
    root["mode"] = Json::Value(request->fGPUEnabled ? "gpu" : "cpu");
    SkDynamicMemoryWStream stream;
    stream.writeText(Json::FastWriter().write(root).c_str());

    SkAutoTUnref<SkData> data(stream.copyToData());
    return SendData(connection, data, "application/json");
}

static int SendTemplate(MHD_Connection* connection, bool redirect = false,
                        const char* redirectUrl = nullptr) {
    SkString debuggerTemplate = generateTemplate(SkString(FLAGS_source[0]));

    MHD_Response* response = MHD_create_response_from_buffer(
        debuggerTemplate.size(),
        (void*) const_cast<char*>(debuggerTemplate.c_str()),
        MHD_RESPMEM_MUST_COPY);
    MHD_add_response_header (response, "Access-Control-Allow-Origin", "*");

    int status = MHD_HTTP_OK;

    if (redirect) {
        MHD_add_response_header (response, "Location", redirectUrl);
        status = MHD_HTTP_SEE_OTHER;
    }

    int ret = MHD_queue_response(connection, status, response);
    MHD_destroy_response(response);
    return ret;
}

class UrlHandler {
public:
    virtual ~UrlHandler() {}
    virtual bool canHandle(const char* method, const char* url) = 0;
    virtual int handle(Request* request, MHD_Connection* connection,
                       const char* url, const char* method,
                       const char* upload_data, size_t* upload_data_size) = 0;
};

class CmdHandler : public UrlHandler {
public:
    bool canHandle(const char* method, const char* url) override {
        const char* kBasePath = "/cmd";
        return 0 == strncmp(url, kBasePath, strlen(kBasePath));
    }

    int handle(Request* request, MHD_Connection* connection,
               const char* url, const char* method,
               const char* upload_data, size_t* upload_data_size) override {
        SkTArray<SkString> commands;
        SkStrSplit(url, "/", &commands);

        if (!request->fPicture.get() || commands.count() > 3) {
            return MHD_NO;
        }

        // /cmd or /cmd/N
        if (0 == strcmp(method, MHD_HTTP_METHOD_GET)) {
            int n;
            if (commands.count() == 1) {
                n = request->fDebugCanvas->getSize() - 1;
            } else {
                sscanf(commands[1].c_str(), "%d", &n);
            }
            return SendJSON(connection, request, n);
        }

        // /cmd/N, for now only delete supported
        if (commands.count() == 2 && 0 == strcmp(method, MHD_HTTP_METHOD_DELETE)) {
            int n;
            sscanf(commands[1].c_str(), "%d", &n);
            request->fDebugCanvas->deleteDrawCommandAt(n);
            return SendOK(connection);
        }

        // /cmd/N/[0|1]
        if (commands.count() == 3 && 0 == strcmp(method, MHD_HTTP_METHOD_POST))  {
            int n, toggle;
            sscanf(commands[1].c_str(), "%d", &n);
            sscanf(commands[2].c_str(), "%d", &toggle);
            request->fDebugCanvas->toggleCommand(n, toggle);
            return SendOK(connection);
        }

        return MHD_NO;
    }
};

class ImgHandler : public UrlHandler {
public:
    bool canHandle(const char* method, const char* url) override {
        static const char* kBasePath = "/img";
        return 0 == strcmp(method, MHD_HTTP_METHOD_GET) &&
               0 == strncmp(url, kBasePath, strlen(kBasePath));
    }

    int handle(Request* request, MHD_Connection* connection,
               const char* url, const char* method,
               const char* upload_data, size_t* upload_data_size) override {
        SkTArray<SkString> commands;
        SkStrSplit(url, "/", &commands);

        if (!request->fPicture.get() || commands.count() > 2) {
            return MHD_NO;
        }

        int n;
        // /img or /img/N
        if (commands.count() == 1) {
            n = request->fDebugCanvas->getSize() - 1;
        } else {
            sscanf(commands[1].c_str(), "%d", &n);
        }

        SkAutoTUnref<SkData> data(drawToPng(request, n));
        return SendData(connection, data, "image/png");
    }
};

class BreakHandler : public UrlHandler {
public:
    bool canHandle(const char* method, const char* url) override {
        static const char* kBasePath = "/break";
        return 0 == strcmp(method, MHD_HTTP_METHOD_GET) &&
               0 == strncmp(url, kBasePath, strlen(kBasePath));
    }

    static SkColor GetPixel(Request* request, int x, int y) {
        SkCanvas* canvas = getCanvasFromRequest(request);
        canvas->flush();
        SkAutoTDelete<SkBitmap> bitmap(getBitmapFromCanvas(canvas));
        SkASSERT(bitmap);
        bitmap->lockPixels();
        uint8_t* start = ((uint8_t*) bitmap->getPixels()) + (y * kImageWidth + x) * 4;
        SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]);
        bitmap->unlockPixels();
        return result;
    }

    int handle(Request* request, MHD_Connection* connection,
               const char* url, const char* method,
               const char* upload_data, size_t* upload_data_size) override {
        SkTArray<SkString> commands;
        SkStrSplit(url, "/", &commands);

        if (!request->fPicture.get() || commands.count() != 4) {
            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 = getCanvasFromRequest(request);
        canvas->clear(SK_ColorWHITE);
        int saveCount = canvas->save();
        for (int i = 0; i <= n; ++i) {
            request->fDebugCanvas->getDrawCommandAt(i)->execute(canvas);
        }
        SkColor target = GetPixel(request, x, y);
        Json::Value response(Json::objectValue);
        Json::Value startColor(Json::arrayValue);
        startColor.append(Json::Value(SkColorGetR(target)));
        startColor.append(Json::Value(SkColorGetG(target)));
        startColor.append(Json::Value(SkColorGetB(target)));
        startColor.append(Json::Value(SkColorGetA(target)));
        response["startColor"] = startColor;
        response["endColor"] = startColor;
        response["endOp"] = Json::Value(n);
        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);
            SkColor current = GetPixel(request, x, y);
            if (current != target) {
                Json::Value endColor(Json::arrayValue);
                endColor.append(Json::Value(SkColorGetR(current)));
                endColor.append(Json::Value(SkColorGetG(current)));
                endColor.append(Json::Value(SkColorGetB(current)));
                endColor.append(Json::Value(SkColorGetA(current)));
                response["endColor"] = endColor;
                response["endOp"] = Json::Value(index);
                break;
            }
        }
        canvas->restoreToCount(saveCount);
        SkDynamicMemoryWStream stream;
        stream.writeText(Json::FastWriter().write(response).c_str());
        SkAutoTUnref<SkData> data(stream.copyToData());
        return SendData(connection, data, "application/json");
    }
};

/**
   Updates the clip visualization alpha. On all subsequent /img requests, the clip will be drawn in
   black with the specified alpha. 0 = no visible clip, 255 = fully opaque clip.
 */
class ClipAlphaHandler : public UrlHandler {
public:
    bool canHandle(const char* method, const char* url) override {
        static const char* kBasePath = "/clipAlpha/";
        return 0 == strcmp(method, MHD_HTTP_METHOD_POST) &&
               0 == strncmp(url, kBasePath, strlen(kBasePath));
    }

    int handle(Request* request, MHD_Connection* connection,
               const char* url, const char* method,
               const char* upload_data, size_t* upload_data_size) override {
        SkTArray<SkString> commands;
        SkStrSplit(url, "/", &commands);

        if (!request->fPicture.get() || commands.count() != 2) {
            return MHD_NO;
        }

        int alpha;
        sscanf(commands[1].c_str(), "%d", &alpha);

        request->fDebugCanvas->setClipVizColor(SkColorSetARGB(alpha, 0, 0, 0));
        return SendOK(connection);
    }
};

/**
   Controls whether GPU rendering is enabled. Posting to /enableGPU/1 turns GPU on, /enableGPU/0 
   disables it.
 */
class EnableGPUHandler : public UrlHandler {
public:
    bool canHandle(const char* method, const char* url) override {
        static const char* kBasePath = "/enableGPU/";
        return 0 == strcmp(method, MHD_HTTP_METHOD_POST) &&
               0 == strncmp(url, kBasePath, strlen(kBasePath));
    }

    int handle(Request* request, MHD_Connection* connection,
               const char* url, const char* method,
               const char* upload_data, size_t* upload_data_size) override {
        SkTArray<SkString> commands;
        SkStrSplit(url, "/", &commands);

        if (commands.count() != 2) {
            return MHD_NO;
        }

        int enable;
        sscanf(commands[1].c_str(), "%d", &enable);

        if (enable) {
            SkSurface* surface = createGPUSurface(request);
            if (surface) {
                request->fSurface.reset(surface);
                request->fGPUEnabled = true;
                return SendOK(connection);
            }
            return SendError(connection, "Unable to create GPU surface");
        }
        request->fSurface.reset(createCPUSurface());
        request->fGPUEnabled = false;
        return SendOK(connection);
    }
};

class PostHandler : public UrlHandler {
public:
    bool canHandle(const char* method, const char* url) override {
        return 0 == strcmp(method, MHD_HTTP_METHOD_POST) &&
               0 == strcmp(url, "/new");
    }

    int handle(Request* request, MHD_Connection* connection,
               const char* url, const char* method,
               const char* upload_data, size_t* upload_data_size) override {
        UploadContext* uc =  request->fUploadContext;

        // New connection
        if (!uc) {
            // TODO make this a method on request
            uc = new UploadContext;
            uc->connection = connection;
            uc->fPostProcessor = MHD_create_post_processor(connection, kBufferSize,
                                                           &process_upload_data, uc);
            SkASSERT(uc->fPostProcessor);

            request->fUploadContext = uc;
            return MHD_YES;
        }

        // in process upload
        if (0 != *upload_data_size) {
            SkASSERT(uc->fPostProcessor);
            MHD_post_process(uc->fPostProcessor, upload_data, *upload_data_size);
            *upload_data_size = 0;
            return MHD_YES;
        }

        // end of upload
        MHD_destroy_post_processor(uc->fPostProcessor);
        uc->fPostProcessor = nullptr;

        // parse picture from stream
        request->fPicture.reset(
            SkPicture::CreateFromStream(request->fUploadContext->fStream.detachAsStream()));
        if (!request->fPicture.get()) {
            fprintf(stderr, "Could not create picture from stream.\n");
            return MHD_NO;
        }

        // pour picture into debug canvas
        request->fDebugCanvas.reset(new SkDebugCanvas(kImageWidth, kImageHeight));
        request->fDebugCanvas->drawPicture(request->fPicture);

        // clear upload context
        delete request->fUploadContext;
        request->fUploadContext = nullptr;

        return SendTemplate(connection, true, "/");
    }
};

class DownloadHandler : public UrlHandler {
public:
    bool canHandle(const char* method, const char* url) override {
        return 0 == strcmp(method, MHD_HTTP_METHOD_GET) &&
               0 == strcmp(url, "/download");
    }

    int handle(Request* request, MHD_Connection* connection,
               const char* url, const char* method,
               const char* upload_data, size_t* upload_data_size) override {
        if (!request->fPicture.get()) {
            return MHD_NO;
        }

        // TODO move to a function
        // Playback into picture recorder
        SkPictureRecorder recorder;
        SkCanvas* canvas = recorder.beginRecording(kImageWidth, kImageHeight);

        request->fDebugCanvas->draw(canvas);

        SkAutoTUnref<SkPicture> picture(recorder.endRecording());

        SkDynamicMemoryWStream outStream;

        SkAutoTUnref<SkPixelSerializer> serializer(SkImageEncoder::CreatePixelSerializer());
        picture->serialize(&outStream, serializer);

        SkAutoTUnref<SkData> data(outStream.copyToData());

        // TODO fancier name handling
        return SendData(connection, data, "application/octet-stream", true,
                        "attachment; filename=something.skp;");
    }
};

class InfoHandler : public UrlHandler {
public:
    bool canHandle(const char* method, const char* url) override {
        const char* kBaseName = "/info";
        return 0 == strcmp(method, MHD_HTTP_METHOD_GET) &&
               0 == strncmp(url, kBaseName, strlen(kBaseName));
    }

    int handle(Request* request, MHD_Connection* connection,
               const char* url, const char* method,
               const char* upload_data, size_t* upload_data_size) override {
        SkTArray<SkString> commands;
        SkStrSplit(url, "/", &commands);

        if (!request->fPicture.get() || commands.count() > 2) {
            return MHD_NO;
        }

        // drawTo
        SkAutoTUnref<SkSurface> surface(createCPUSurface());
        SkCanvas* canvas = surface->getCanvas();

        int n;
        // /info or /info/N
        if (commands.count() == 1) {
            n = request->fDebugCanvas->getSize() - 1;
        } else {
            sscanf(commands[1].c_str(), "%d", &n);
        }

        // TODO this is really slow and we should cache the matrix and clip
        request->fDebugCanvas->drawTo(canvas, n);

        // make some json
        SkMatrix vm = request->fDebugCanvas->getCurrentMatrix();
        SkIRect clip = request->fDebugCanvas->getCurrentClip();
        Json::Value info(Json::objectValue);
        info["ViewMatrix"] = SkJSONCanvas::MakeMatrix(vm);
        info["ClipRect"] = SkJSONCanvas::MakeIRect(clip);

        std::string json = Json::FastWriter().write(info);

        // We don't want the null terminator so strlen is correct
        SkAutoTUnref<SkData> data(SkData::NewWithCopy(json.c_str(), strlen(json.c_str())));
        return SendData(connection, data, "application/json");
    }
};

class DataHandler : public UrlHandler {
public:
    bool canHandle(const char* method, const char* url) override {
        static const char* kBaseUrl = "/data";
        return 0 == strcmp(method, MHD_HTTP_METHOD_GET) &&
               0 == strncmp(url, kBaseUrl, strlen(kBaseUrl));
    }

    int handle(Request* request, MHD_Connection* connection,
               const char* url, const char* method,
               const char* upload_data, size_t* upload_data_size) override {
        SkTArray<SkString> commands;
        SkStrSplit(url, "/", &commands);

        if (!request->fPicture.get() || commands.count() != 2) {
            return MHD_NO;
        }

        SkAutoTUnref<UrlDataManager::UrlData> urlData(
            SkRef(request->fUrlDataManager.getDataFromUrl(SkString(url))));

        if (urlData) {
            return SendData(connection, urlData->fData.get(), urlData->fContentType.c_str());
        }
        return MHD_NO;
    }
};

class RootHandler : public UrlHandler {
public:
    bool canHandle(const char* method, const char* url) override {
        return 0 == strcmp(method, MHD_HTTP_METHOD_GET) &&
               0 == strcmp(url, "/");
    }

    int handle(Request* request, MHD_Connection* connection,
               const char* url, const char* method,
               const char* upload_data, size_t* upload_data_size) override {
        return SendTemplate(connection);
    }
};

class UrlManager {
public:
    UrlManager() {
        // Register handlers
        fHandlers.push_back(new RootHandler);
        fHandlers.push_back(new PostHandler);
        fHandlers.push_back(new ImgHandler);
        fHandlers.push_back(new ClipAlphaHandler);
        fHandlers.push_back(new EnableGPUHandler);
        fHandlers.push_back(new CmdHandler);
        fHandlers.push_back(new InfoHandler);
        fHandlers.push_back(new DownloadHandler);
        fHandlers.push_back(new DataHandler);
        fHandlers.push_back(new BreakHandler);
    }

    ~UrlManager() {
        for (int i = 0; i < fHandlers.count(); i++) { delete fHandlers[i]; }
    }

    // This is clearly not efficient for a large number of urls and handlers
    int invoke(Request* request, MHD_Connection* connection, const char* url, const char* method,
               const char* upload_data, size_t* upload_data_size) const {
        for (int i = 0; i < fHandlers.count(); i++) {
            if (fHandlers[i]->canHandle(method, url)) {
                return fHandlers[i]->handle(request, connection, url, method, upload_data,
                                            upload_data_size);
            }
        }
        return MHD_NO;
    }

private:
    SkTArray<UrlHandler*> fHandlers;
};

const UrlManager kUrlManager;

int answer_to_connection(void* cls, struct MHD_Connection* connection,
                         const char* url, const char* method, const char* version,
                         const char* upload_data, size_t* upload_data_size,
                         void** con_cls) {
    SkDebugf("New %s request for %s using version %s\n", method, url, version);

    Request* request = reinterpret_cast<Request*>(cls);
    int result = kUrlManager.invoke(request, connection, url, method, upload_data,
                                    upload_data_size);
    if (MHD_NO == result) {
        fprintf(stderr, "Invalid method and / or url: %s %s\n", method, url);
    }
    return result;
}

int skiaserve_main() {
    Request request(SkString("/data")); // This simple server has one request

    // create surface
    GrContextOptions grContextOpts;
    request.fContextFactory.reset(new GrContextFactory(grContextOpts));
    request.fSurface.reset(createCPUSurface());

    struct MHD_Daemon* daemon;
    // TODO Add option to bind this strictly to an address, e.g. localhost, for security.
    daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, FLAGS_port, nullptr, nullptr,
                              &answer_to_connection, &request,
                              MHD_OPTION_END);
    if (NULL == daemon) {
        return 1;
    }

    getchar();
    MHD_stop_daemon(daemon);
    return 0;
}

#if !defined SK_BUILD_FOR_IOS
int main(int argc, char** argv) {
    SkCommandLineFlags::Parse(argc, argv);
    return skiaserve_main();
}
#endif