blob: aa6d803264c2e06ce26a31fa121d88f4036e1923 [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"
joshualitt9a4e1882016-01-27 07:03:29 -080012#include "SkPicture.h"
joshualitt7f6a1e02016-01-22 11:21:43 -080013#include "SkStream.h"
14#include "SkSurface.h"
15
joshualitt7f6a1e02016-01-22 11:21:43 -080016#include <sys/socket.h>
joshualitt7f6a1e02016-01-22 11:21:43 -080017#include <microhttpd.h>
18
19// To get image decoders linked in we have to do the below magic
20#include "SkForceLinking.h"
21#include "SkImageDecoder.h"
22__SK_FORCE_IMAGE_DECODER_LINKING;
23
24// TODO make this configurable
25#define PORT 8888
26
27DEFINE_string(dir, "skps", "Directory to read skp.");
28DEFINE_string(name, "desk_carsvg", "skp to load.");
29DEFINE_bool(useTemplate, true, "whether or not to use the skdebugger template string.");
30
31// TODO probably want to make this configurable
32static const int kImageWidth = 1920;
33static const int kImageHeight = 1080;
34
35// TODO factor this out into functions, also handle CPU path
joshualitt9a4e1882016-01-27 07:03:29 -080036SkData* setupAndDrawToCanvas(SkStream* stream, SkString* error) {
joshualitt7f6a1e02016-01-22 11:21:43 -080037 GrContextOptions grContextOpts;
38 SkAutoTDelete<GrContextFactory> factory(new GrContextFactory(grContextOpts));
39
40 GrContext* context = factory->get(GrContextFactory::kNative_GLContextType,
41 GrContextFactory::kNone_GLContextOptions);
42 int maxRTSize = context->caps()->maxRenderTargetSize();
43 SkImageInfo info = SkImageInfo::Make(SkTMin(kImageWidth, maxRTSize),
44 SkTMin(kImageHeight, maxRTSize),
45 kN32_SkColorType, kPremul_SkAlphaType);
46 uint32_t flags = 0;
47 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
48 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context,
49 SkSurface::kNo_Budgeted, info,
50 0, &props));
51 SkASSERT(surface.get());
52
53 SkGLContext* gl = factory->getContextInfo(GrContextFactory::kNative_GLContextType,
54 GrContextFactory::kNone_GLContextOptions).fGLContext;
55 gl->makeCurrent();
56
57 // draw
joshualitt9a4e1882016-01-27 07:03:29 -080058 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream));
joshualitt7f6a1e02016-01-22 11:21:43 -080059 if (pic.get() == nullptr) {
joshualitt9a4e1882016-01-27 07:03:29 -080060 error->appendf("Could not create picture from stream.\n");
joshualitt7f6a1e02016-01-22 11:21:43 -080061 return nullptr;
62 }
63
64 SkCanvas* canvas = surface->getCanvas();
65 canvas->drawPicture(pic);
66
67 // capture pixels
68 SkBitmap bmp;
69 bmp.setInfo(canvas->imageInfo());
70 if (!canvas->readPixels(&bmp, 0, 0)) {
71 error->appendf("Can't read canvas pixels.\n");
72 return nullptr;
73 }
74
75 // write to png
76 SkData* data = SkImageEncoder::EncodeData(bmp, SkImageEncoder::kPNG_Type, 100);
77 if (!data) {
78 error->appendf("Can't encode a PNG.\n");
79 return nullptr;
80 }
81 return data;
82}
joshualitt7f6a1e02016-01-22 11:21:43 -080083// TODO move to template file
84SkString generateTemplate(SkString source) {
85 SkString debuggerTemplate;
86 debuggerTemplate.appendf(
87 "<!DOCTYPE html>\n"
88 "<html>\n"
89 "<head>\n"
90 " <title>SkDebugger</title>\n"
91 " <meta charset=\"utf-8\" />\n"
92 " <meta http-equiv=\"X-UA-Compatible\" content=\"IE=egde,chrome=1\">\n"
93 " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n"
94 " <script src=\"%s/res/js/core.js\" type=\"text/javascript\" charset=\"utf-8\"></script>\n"
95 " <link href=\"%s/res/vul/elements.html\" rel=\"import\" />\n"
96 "</head>\n"
97 "<body class=\"fullbleed layout vertical\">\n"
98 " <debugger-app-sk>This is the app."
99 " </debugger-app-sk>\n"
100 "</body>\n"
101 "</html>", source.c_str(), source.c_str());
102 return debuggerTemplate;
103
104}
105
joshualitt9a4e1882016-01-27 07:03:29 -0800106struct UploadContext {
107 SkDynamicMemoryWStream stream;
108 MHD_PostProcessor* pp;
109 MHD_Connection* connection;
110};
111
112struct Request {
113 Request() : fUploadContext(nullptr) {}
114 UploadContext* fUploadContext;
115 SkAutoTUnref<SkData> fPNG;
116};
117
118static 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) {
127 uc->stream.write(data, size);
128 }
129 return MHD_YES;
130}
131
joshualittccfdaa52016-01-27 07:40:29 -0800132static int SendImage(MHD_Connection* connection, const SkData* data) {
133 MHD_Response* response = MHD_create_response_from_buffer(data->size(),
134 const_cast<void*>(data->data()),
135 MHD_RESPMEM_MUST_COPY);
136 MHD_add_response_header(response, "Content-Type", "image/png");
137
138 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
139 MHD_destroy_response(response);
140 return ret;
141}
142
143static int SendTemplate(MHD_Connection* connection) {
144 SkString debuggerTemplate = generateTemplate(SkString("http://debugger.skia.org"));
145
146 MHD_Response* response = MHD_create_response_from_buffer(
147 debuggerTemplate.size(),
148 (void*) const_cast<char*>(debuggerTemplate.c_str()),
149 MHD_RESPMEM_MUST_COPY);
150
151 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
152 MHD_destroy_response(response);
153 return ret;
154}
155
156typedef int (*UrlHandler)(Request* request, MHD_Connection* connection,
157 const char* upload_data, size_t* upload_data_size);
158
159int rootHandler(Request* request, MHD_Connection* connection,
160 const char* upload_data, size_t* upload_data_size) {
161 return SendTemplate(connection);
162}
163
164int postHandler(Request* request, MHD_Connection* connection,
165 const char* upload_data, size_t* upload_data_size) {
166 UploadContext* uc = request->fUploadContext;
167
168 // New connection
169 if (!uc) {
170 // TODO make this a method on request
171 uc = new UploadContext;
172 uc->connection = connection;
173 uc->pp = MHD_create_post_processor(connection, kBufferSize, &process_upload_data, uc);
174 SkASSERT(uc->pp);
175
176 request->fUploadContext = uc;
177 return MHD_YES;
178 }
179
180 // in process upload
181 if (0 != *upload_data_size) {
182 SkASSERT(uc->pp);
183 MHD_post_process(uc->pp, upload_data, *upload_data_size);
184 *upload_data_size = 0;
185 return MHD_YES;
186 }
187
188 // end of upload
189 MHD_destroy_post_processor(uc->pp);
190 uc->pp = nullptr;
191
192 // TODO response
193 SkString error;
194 SkData* data = setupAndDrawToCanvas(uc->stream.detachAsStream(), &error);
195 if (!data) {
196 // TODO send error
197 return MHD_YES;
198 }
199
200 request->fPNG.reset(data);
201 return SendTemplate(connection);
202}
203
204int imgHandler(Request* request, MHD_Connection* connection,
205 const char* upload_data, size_t* upload_data_size) {
206 if (request->fPNG.get()) {
207 SkData* data = request->fPNG.get();
208 return SendImage(connection, data);
209 }
210 return MHD_NO;
211}
212
213class UrlManager {
214public:
215 UrlManager() {
216 // Register handlers
217 fHandlers.push_back({MHD_HTTP_METHOD_GET, "/", rootHandler});
218 fHandlers.push_back({MHD_HTTP_METHOD_POST, "/new", postHandler});
219 fHandlers.push_back({MHD_HTTP_METHOD_GET, "/img", imgHandler});
220 }
221
222 // This is clearly not efficient for a large number of urls and handlers
223 int invoke(Request* request, MHD_Connection* connection, const char* url, const char* method,
224 const char* upload_data, size_t* upload_data_size) const {
225 for (int i = 0; i < fHandlers.count(); i++) {
226 const Url& urlHandler = fHandlers[i];
227 if (0 == strcmp(method, urlHandler.fMethod) &&
228 0 == strcmp(url, urlHandler.fPath)) {
229 return (*urlHandler.fHandler)(request, connection, upload_data,
230 upload_data_size);
231 }
232 }
233 return MHD_NO;
234 }
235
236private:
237 struct Url {
238 const char* fMethod;
239 const char* fPath;
240 UrlHandler fHandler;
241 };
242 SkTArray<Url> fHandlers;
243};
244
245const UrlManager kUrlManager;
246
joshualitt7f6a1e02016-01-22 11:21:43 -0800247int answer_to_connection(void* cls, struct MHD_Connection* connection,
248 const char* url, const char* method, const char* version,
249 const char* upload_data, size_t* upload_data_size,
250 void** con_cls) {
joshualitt9a4e1882016-01-27 07:03:29 -0800251 SkDebugf("New %s request for %s using version %s\n", method, url, version);
joshualitt7f6a1e02016-01-22 11:21:43 -0800252
joshualitt9a4e1882016-01-27 07:03:29 -0800253 Request* request = reinterpret_cast<Request*>(cls);
joshualittccfdaa52016-01-27 07:40:29 -0800254 return kUrlManager.invoke(request, connection, url, method, upload_data, upload_data_size);
joshualitt7f6a1e02016-01-22 11:21:43 -0800255}
256
257int skiaserve_main() {
joshualitt9a4e1882016-01-27 07:03:29 -0800258 Request request; // This simple server has one request
joshualitt7f6a1e02016-01-22 11:21:43 -0800259 struct MHD_Daemon* daemon;
joshualitt9a4e1882016-01-27 07:03:29 -0800260 daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, PORT, nullptr, nullptr,
261 &answer_to_connection, &request,
262 MHD_OPTION_END);
joshualitt7f6a1e02016-01-22 11:21:43 -0800263 if (NULL == daemon) {
264 return 1;
265 }
266
267 getchar();
268 MHD_stop_daemon(daemon);
269 return 0;
270}
271
272#if !defined SK_BUILD_FOR_IOS
273int main(int argc, char** argv) {
274 SkCommandLineFlags::Parse(argc, argv);
275 return skiaserve_main();
276}
277#endif