blob: 9252da85dcba497504ca8c52b43abf760a32cba9 [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
joshualitt7f6a1e02016-01-22 11:21:43 -0800132int answer_to_connection(void* cls, struct MHD_Connection* connection,
133 const char* url, const char* method, const char* version,
134 const char* upload_data, size_t* upload_data_size,
135 void** con_cls) {
joshualitt9a4e1882016-01-27 07:03:29 -0800136 SkDebugf("New %s request for %s using version %s\n", method, url, version);
joshualitt7f6a1e02016-01-22 11:21:43 -0800137
joshualitt9a4e1882016-01-27 07:03:29 -0800138 Request* request = reinterpret_cast<Request*>(cls);
joshualitt7f6a1e02016-01-22 11:21:43 -0800139
joshualitt9a4e1882016-01-27 07:03:29 -0800140 MHD_Response* response;
141 int ret = MHD_NO;
142
143 // TODO url handlers
144 // handle uploads
145 if (0 == strcmp(method, MHD_HTTP_METHOD_POST) &&
146 0 == strcmp(url, "/new")) {
147 UploadContext* uc = request->fUploadContext;
148
149 // New connection
150 if (!uc) {
151 // TODO make this a method on request
152 uc = new UploadContext;
153 uc->connection = connection;
154 uc->pp = MHD_create_post_processor(connection, kBufferSize, &process_upload_data, uc);
155 SkASSERT(uc->pp);
156
157 request->fUploadContext = uc;
158 return MHD_YES;
159 }
160
161 // in process upload
162 if (0 != *upload_data_size) {
163 SkASSERT(uc->pp);
164 MHD_post_process(uc->pp, upload_data, *upload_data_size);
165 *upload_data_size = 0;
166 return MHD_YES;
167 }
168
169 // end of upload
170 MHD_destroy_post_processor(uc->pp);
171 uc->pp = nullptr;
172
173 // TODO response
174 SkString error;
175 SkData* data = setupAndDrawToCanvas(uc->stream.detachAsStream(), &error);
176 if (!data) {
177 // TODO send error
178 return MHD_YES;
179 }
180
181 request->fPNG.reset(data);
182 // TODO Hack
joshualitt7f6a1e02016-01-22 11:21:43 -0800183 SkString debuggerTemplate = generateTemplate(SkString("http://debugger.skia.org"));
184
185 response = MHD_create_response_from_buffer(debuggerTemplate.size(),
joshualitt9a4e1882016-01-27 07:03:29 -0800186 (void*) const_cast<char*>(debuggerTemplate.c_str()),
joshualitt7f6a1e02016-01-22 11:21:43 -0800187 MHD_RESPMEM_MUST_COPY);
joshualitt9a4e1882016-01-27 07:03:29 -0800188
joshualitt7f6a1e02016-01-22 11:21:43 -0800189 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
190 MHD_destroy_response(response);
joshualitt9a4e1882016-01-27 07:03:29 -0800191 } else if (0 == strcmp(method, MHD_HTTP_METHOD_GET)) {
192 if (0 == strcmp(url, "/")) {
193 SkString debuggerTemplate = generateTemplate(SkString("http://debugger.skia.org"));
194
195 response = MHD_create_response_from_buffer(debuggerTemplate.size(),
196 (void*) const_cast<char*>(debuggerTemplate.c_str()),
197 MHD_RESPMEM_MUST_COPY);
198
199 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
200 MHD_destroy_response(response);
201 } else if (0 == strcmp(url, "/img")) {
202 if (request->fPNG.get()) {
203 SkData* data = request->fPNG.get();
204 response = MHD_create_response_from_buffer(data->size(),
205 const_cast<void*>(data->data()),
206 MHD_RESPMEM_MUST_COPY);
207 MHD_add_response_header(response, "Content-Type", "image/png");
208
209 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
210 MHD_destroy_response(response);
211 }
212 }
213 } else {
214 SkFAIL("whoops, need proper error handling");
215 return MHD_NO;
joshualitt7f6a1e02016-01-22 11:21:43 -0800216 }
217
joshualitt7f6a1e02016-01-22 11:21:43 -0800218 return ret;
219}
220
221int skiaserve_main() {
joshualitt9a4e1882016-01-27 07:03:29 -0800222 Request request; // This simple server has one request
joshualitt7f6a1e02016-01-22 11:21:43 -0800223 struct MHD_Daemon* daemon;
joshualitt9a4e1882016-01-27 07:03:29 -0800224 daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, PORT, nullptr, nullptr,
225 &answer_to_connection, &request,
226 MHD_OPTION_END);
joshualitt7f6a1e02016-01-22 11:21:43 -0800227 if (NULL == daemon) {
228 return 1;
229 }
230
231 getchar();
232 MHD_stop_daemon(daemon);
233 return 0;
234}
235
236#if !defined SK_BUILD_FOR_IOS
237int main(int argc, char** argv) {
238 SkCommandLineFlags::Parse(argc, argv);
239 return skiaserve_main();
240}
241#endif