blob: d37dcafd23f2f82fae39a8fb9db8012e967180d3 [file] [log] [blame]
joshualitt7f6a1e02016-01-22 11:21:43 -08001/*
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"
joshualitt609d9792016-01-27 11:07:23 -080012#include "SkJSONCanvas.h"
joshualitt9a4e1882016-01-27 07:03:29 -080013#include "SkPicture.h"
joshualitt7f6a1e02016-01-22 11:21:43 -080014#include "SkStream.h"
15#include "SkSurface.h"
16
joshualitt7f6a1e02016-01-22 11:21:43 -080017#include <sys/socket.h>
joshualitt7f6a1e02016-01-22 11:21:43 -080018#include <microhttpd.h>
19
20// To get image decoders linked in we have to do the below magic
21#include "SkForceLinking.h"
22#include "SkImageDecoder.h"
23__SK_FORCE_IMAGE_DECODER_LINKING;
24
jcgregorio21ab1202016-01-28 06:24:19 -080025DEFINE_string(source, "https://debugger.skia.org", "Where to load the web UI from.");
26DEFINE_int32(port, 8888, "The port to listen on.");
joshualitt7f6a1e02016-01-22 11:21:43 -080027
28// TODO probably want to make this configurable
29static const int kImageWidth = 1920;
30static const int kImageHeight = 1080;
31
joshualitt7f6a1e02016-01-22 11:21:43 -080032// TODO move to template file
33SkString generateTemplate(SkString source) {
34 SkString debuggerTemplate;
35 debuggerTemplate.appendf(
36 "<!DOCTYPE html>\n"
37 "<html>\n"
38 "<head>\n"
39 " <title>SkDebugger</title>\n"
40 " <meta charset=\"utf-8\" />\n"
41 " <meta http-equiv=\"X-UA-Compatible\" content=\"IE=egde,chrome=1\">\n"
42 " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n"
43 " <script src=\"%s/res/js/core.js\" type=\"text/javascript\" charset=\"utf-8\"></script>\n"
44 " <link href=\"%s/res/vul/elements.html\" rel=\"import\" />\n"
45 "</head>\n"
46 "<body class=\"fullbleed layout vertical\">\n"
47 " <debugger-app-sk>This is the app."
48 " </debugger-app-sk>\n"
49 "</body>\n"
50 "</html>", source.c_str(), source.c_str());
51 return debuggerTemplate;
52
53}
54
joshualitt9a4e1882016-01-27 07:03:29 -080055struct UploadContext {
joshualitt609d9792016-01-27 11:07:23 -080056 SkDynamicMemoryWStream fStream;
57 MHD_PostProcessor* fPostProcessor;
joshualitt9a4e1882016-01-27 07:03:29 -080058 MHD_Connection* connection;
59};
60
61struct Request {
62 Request() : fUploadContext(nullptr) {}
63 UploadContext* fUploadContext;
64 SkAutoTUnref<SkData> fPNG;
joshualitt609d9792016-01-27 11:07:23 -080065 SkAutoTUnref<SkPicture> fPicture;
joshualitt9a4e1882016-01-27 07:03:29 -080066};
67
joshualitt609d9792016-01-27 11:07:23 -080068// TODO factor this out into functions, also handle CPU path
69bool setupAndDrawToCanvas(Request* request, SkString* error) {
70 GrContextOptions grContextOpts;
71 SkAutoTDelete<GrContextFactory> factory(new GrContextFactory(grContextOpts));
72
73 GrContext* context = factory->get(GrContextFactory::kNative_GLContextType,
74 GrContextFactory::kNone_GLContextOptions);
75 int maxRTSize = context->caps()->maxRenderTargetSize();
76 SkImageInfo info = SkImageInfo::Make(SkTMin(kImageWidth, maxRTSize),
77 SkTMin(kImageHeight, maxRTSize),
78 kN32_SkColorType, kPremul_SkAlphaType);
79 uint32_t flags = 0;
80 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
81 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context,
82 SkSurface::kNo_Budgeted, info,
83 0, &props));
84 SkASSERT(surface.get());
85
86 SkGLContext* gl = factory->getContextInfo(GrContextFactory::kNative_GLContextType,
87 GrContextFactory::kNone_GLContextOptions).fGLContext;
88 gl->makeCurrent();
89
90 // draw
91 request->fPicture.reset(
92 SkPicture::CreateFromStream(request->fUploadContext->fStream.detachAsStream()));
93 if (!request->fPicture.get()) {
94 error->appendf("Could not create picture from stream.\n");
95 return false;
96 }
97
98 SkCanvas* canvas = surface->getCanvas();
99 canvas->drawPicture(request->fPicture);
100
101 // capture pixels
102 SkBitmap bmp;
103 bmp.setInfo(canvas->imageInfo());
104 if (!canvas->readPixels(&bmp, 0, 0)) {
105 error->appendf("Can't read canvas pixels.\n");
106 return false;
107 }
108
109 // write to png
110 request->fPNG.reset(SkImageEncoder::EncodeData(bmp, SkImageEncoder::kPNG_Type, 100));
111 if (!request->fPNG) {
112 error->appendf("Can't encode a PNG.\n");
113 return false;
114 }
115 return true;
116}
117
joshualitt9a4e1882016-01-27 07:03:29 -0800118static const size_t kBufferSize = 1024;
119
120static int process_upload_data(void* cls, enum MHD_ValueKind kind,
121 const char* key, const char* filename,
122 const char* content_type, const char* transfer_encoding,
123 const char* data, uint64_t off, size_t size) {
124 struct UploadContext* uc = reinterpret_cast<UploadContext*>(cls);
125
126 if (0 != size) {
joshualitt609d9792016-01-27 11:07:23 -0800127 uc->fStream.write(data, size);
joshualitt9a4e1882016-01-27 07:03:29 -0800128 }
129 return MHD_YES;
130}
131
joshualitt609d9792016-01-27 11:07:23 -0800132static int SendData(MHD_Connection* connection, const SkData* data, const char* type) {
joshualittccfdaa52016-01-27 07:40:29 -0800133 MHD_Response* response = MHD_create_response_from_buffer(data->size(),
134 const_cast<void*>(data->data()),
135 MHD_RESPMEM_MUST_COPY);
joshualitt609d9792016-01-27 11:07:23 -0800136 MHD_add_response_header(response, "Content-Type", type);
joshualittccfdaa52016-01-27 07:40:29 -0800137
138 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
139 MHD_destroy_response(response);
140 return ret;
141}
142
joshualitt609d9792016-01-27 11:07:23 -0800143static int SendJSON(MHD_Connection* connection, SkPicture* picture) {
144 SkDynamicMemoryWStream stream;
145 SkAutoTUnref<SkJSONCanvas> jsonCanvas(new SkJSONCanvas(kImageWidth, kImageHeight, stream));
146 jsonCanvas->drawPicture(picture);
147 jsonCanvas->finish();
148
149 SkAutoTUnref<SkData> data(stream.copyToData());
150 return SendData(connection, data, "application/json");
151}
152
153static int SendTemplate(MHD_Connection* connection, bool redirect = false,
154 const char* redirectUrl = nullptr) {
jcgregorio21ab1202016-01-28 06:24:19 -0800155 SkString debuggerTemplate = generateTemplate(SkString(FLAGS_source[0]));
joshualittccfdaa52016-01-27 07:40:29 -0800156
157 MHD_Response* response = MHD_create_response_from_buffer(
158 debuggerTemplate.size(),
159 (void*) const_cast<char*>(debuggerTemplate.c_str()),
160 MHD_RESPMEM_MUST_COPY);
jcgregorio6a2046e2016-01-28 05:31:31 -0800161 MHD_add_response_header (response, "Access-Control-Allow-Origin", "*");
joshualittccfdaa52016-01-27 07:40:29 -0800162
jcgregorio6f17bc52016-01-27 11:44:38 -0800163 int status = MHD_HTTP_OK;
164
joshualitt609d9792016-01-27 11:07:23 -0800165 if (redirect) {
166 MHD_add_response_header (response, "Location", redirectUrl);
jcgregorio6f17bc52016-01-27 11:44:38 -0800167 status = MHD_HTTP_SEE_OTHER;
joshualitt609d9792016-01-27 11:07:23 -0800168 }
169
jcgregorio6f17bc52016-01-27 11:44:38 -0800170 int ret = MHD_queue_response(connection, status, response);
joshualittccfdaa52016-01-27 07:40:29 -0800171 MHD_destroy_response(response);
172 return ret;
173}
174
175typedef int (*UrlHandler)(Request* request, MHD_Connection* connection,
176 const char* upload_data, size_t* upload_data_size);
177
178int rootHandler(Request* request, MHD_Connection* connection,
179 const char* upload_data, size_t* upload_data_size) {
180 return SendTemplate(connection);
181}
182
183int postHandler(Request* request, MHD_Connection* connection,
184 const char* upload_data, size_t* upload_data_size) {
185 UploadContext* uc = request->fUploadContext;
186
187 // New connection
188 if (!uc) {
189 // TODO make this a method on request
190 uc = new UploadContext;
191 uc->connection = connection;
joshualitt609d9792016-01-27 11:07:23 -0800192 uc->fPostProcessor = MHD_create_post_processor(connection, kBufferSize,
193 &process_upload_data, uc);
194 SkASSERT(uc->fPostProcessor);
joshualittccfdaa52016-01-27 07:40:29 -0800195
196 request->fUploadContext = uc;
197 return MHD_YES;
198 }
199
200 // in process upload
201 if (0 != *upload_data_size) {
joshualitt609d9792016-01-27 11:07:23 -0800202 SkASSERT(uc->fPostProcessor);
203 MHD_post_process(uc->fPostProcessor, upload_data, *upload_data_size);
joshualittccfdaa52016-01-27 07:40:29 -0800204 *upload_data_size = 0;
205 return MHD_YES;
206 }
207
208 // end of upload
joshualitt609d9792016-01-27 11:07:23 -0800209 MHD_destroy_post_processor(uc->fPostProcessor);
210 uc->fPostProcessor = nullptr;
joshualittccfdaa52016-01-27 07:40:29 -0800211
212 // TODO response
213 SkString error;
joshualitt609d9792016-01-27 11:07:23 -0800214 if (!setupAndDrawToCanvas(request, &error)) {
joshualittccfdaa52016-01-27 07:40:29 -0800215 // TODO send error
216 return MHD_YES;
217 }
218
joshualitt609d9792016-01-27 11:07:23 -0800219 return SendTemplate(connection, true, "/");
joshualittccfdaa52016-01-27 07:40:29 -0800220}
221
222int imgHandler(Request* request, MHD_Connection* connection,
223 const char* upload_data, size_t* upload_data_size) {
224 if (request->fPNG.get()) {
225 SkData* data = request->fPNG.get();
joshualitt609d9792016-01-27 11:07:23 -0800226 return SendData(connection, data, "image/png");
227 }
228 return MHD_NO;
229}
230
231int infoHandler(Request* request, MHD_Connection* connection,
232 const char* upload_data, size_t* upload_data_size) {
233 if (request->fPicture.get()) {
234 return SendJSON(connection, request->fPicture);
joshualittccfdaa52016-01-27 07:40:29 -0800235 }
236 return MHD_NO;
237}
238
239class UrlManager {
240public:
241 UrlManager() {
242 // Register handlers
243 fHandlers.push_back({MHD_HTTP_METHOD_GET, "/", rootHandler});
244 fHandlers.push_back({MHD_HTTP_METHOD_POST, "/new", postHandler});
245 fHandlers.push_back({MHD_HTTP_METHOD_GET, "/img", imgHandler});
jcgregorio9ce41102016-01-27 12:15:38 -0800246 fHandlers.push_back({MHD_HTTP_METHOD_GET, "/cmd", infoHandler});
joshualittccfdaa52016-01-27 07:40:29 -0800247 }
248
249 // This is clearly not efficient for a large number of urls and handlers
250 int invoke(Request* request, MHD_Connection* connection, const char* url, const char* method,
251 const char* upload_data, size_t* upload_data_size) const {
252 for (int i = 0; i < fHandlers.count(); i++) {
253 const Url& urlHandler = fHandlers[i];
254 if (0 == strcmp(method, urlHandler.fMethod) &&
255 0 == strcmp(url, urlHandler.fPath)) {
256 return (*urlHandler.fHandler)(request, connection, upload_data,
257 upload_data_size);
258 }
259 }
260 return MHD_NO;
261 }
262
263private:
264 struct Url {
265 const char* fMethod;
266 const char* fPath;
267 UrlHandler fHandler;
268 };
269 SkTArray<Url> fHandlers;
270};
271
272const UrlManager kUrlManager;
273
joshualitt7f6a1e02016-01-22 11:21:43 -0800274int answer_to_connection(void* cls, struct MHD_Connection* connection,
275 const char* url, const char* method, const char* version,
276 const char* upload_data, size_t* upload_data_size,
277 void** con_cls) {
joshualitt9a4e1882016-01-27 07:03:29 -0800278 SkDebugf("New %s request for %s using version %s\n", method, url, version);
joshualitt7f6a1e02016-01-22 11:21:43 -0800279
joshualitt9a4e1882016-01-27 07:03:29 -0800280 Request* request = reinterpret_cast<Request*>(cls);
joshualittccfdaa52016-01-27 07:40:29 -0800281 return kUrlManager.invoke(request, connection, url, method, upload_data, upload_data_size);
joshualitt7f6a1e02016-01-22 11:21:43 -0800282}
283
284int skiaserve_main() {
joshualitt9a4e1882016-01-27 07:03:29 -0800285 Request request; // This simple server has one request
joshualitt7f6a1e02016-01-22 11:21:43 -0800286 struct MHD_Daemon* daemon;
jcgregorio21ab1202016-01-28 06:24:19 -0800287 // TODO Add option to bind this strictly to an address, e.g. localhost, for security.
288 daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, FLAGS_port, nullptr, nullptr,
joshualitt9a4e1882016-01-27 07:03:29 -0800289 &answer_to_connection, &request,
290 MHD_OPTION_END);
joshualitt7f6a1e02016-01-22 11:21:43 -0800291 if (NULL == daemon) {
292 return 1;
293 }
294
295 getchar();
296 MHD_stop_daemon(daemon);
297 return 0;
298}
299
300#if !defined SK_BUILD_FOR_IOS
301int main(int argc, char** argv) {
302 SkCommandLineFlags::Parse(argc, argv);
303 return skiaserve_main();
304}
305#endif