blob: effd28973c226bd007a98b7e185b63aa04001d58 [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"
12#include "SkOSFile.h"
13#include <SkPicture.h>
14#include "SkStream.h"
15#include "SkSurface.h"
16
17// temporary junk
18#include "SkGradientShader.h"
19
20#include <stdio.h>
21#include <sys/types.h>
22#include <sys/select.h>
23#include <sys/stat.h>
24#include <sys/socket.h>
25#include <fcntl.h>
26#include <microhttpd.h>
27
28// To get image decoders linked in we have to do the below magic
29#include "SkForceLinking.h"
30#include "SkImageDecoder.h"
31__SK_FORCE_IMAGE_DECODER_LINKING;
32
33// TODO make this configurable
34#define PORT 8888
35
36DEFINE_string(dir, "skps", "Directory to read skp.");
37DEFINE_string(name, "desk_carsvg", "skp to load.");
38DEFINE_bool(useTemplate, true, "whether or not to use the skdebugger template string.");
39
40// TODO probably want to make this configurable
41static const int kImageWidth = 1920;
42static const int kImageHeight = 1080;
43
44// TODO factor this out into functions, also handle CPU path
45SkData* setupAndDrawToCanvas(const char* path, SkString* error) {
46 GrContextOptions grContextOpts;
47 SkAutoTDelete<GrContextFactory> factory(new GrContextFactory(grContextOpts));
48
49 GrContext* context = factory->get(GrContextFactory::kNative_GLContextType,
50 GrContextFactory::kNone_GLContextOptions);
51 int maxRTSize = context->caps()->maxRenderTargetSize();
52 SkImageInfo info = SkImageInfo::Make(SkTMin(kImageWidth, maxRTSize),
53 SkTMin(kImageHeight, maxRTSize),
54 kN32_SkColorType, kPremul_SkAlphaType);
55 uint32_t flags = 0;
56 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
57 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context,
58 SkSurface::kNo_Budgeted, info,
59 0, &props));
60 SkASSERT(surface.get());
61
62 SkGLContext* gl = factory->getContextInfo(GrContextFactory::kNative_GLContextType,
63 GrContextFactory::kNone_GLContextOptions).fGLContext;
64 gl->makeCurrent();
65
66 // draw
67 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
68 if (stream.get() == nullptr) {
69 error->appendf("Could not read %s.\n", path);
70 return nullptr;
71 }
72
73 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream.get()));
74 if (pic.get() == nullptr) {
75 error->appendf("Could not read %s as an SkPicture.\n", path);
76 return nullptr;
77 }
78
79 SkCanvas* canvas = surface->getCanvas();
80 canvas->drawPicture(pic);
81
82 // capture pixels
83 SkBitmap bmp;
84 bmp.setInfo(canvas->imageInfo());
85 if (!canvas->readPixels(&bmp, 0, 0)) {
86 error->appendf("Can't read canvas pixels.\n");
87 return nullptr;
88 }
89
90 // write to png
91 SkData* data = SkImageEncoder::EncodeData(bmp, SkImageEncoder::kPNG_Type, 100);
92 if (!data) {
93 error->appendf("Can't encode a PNG.\n");
94 return nullptr;
95 }
96 return data;
97}
98
99// TODO move to template file
100SkString generateTemplate(SkString source) {
101 SkString debuggerTemplate;
102 debuggerTemplate.appendf(
103 "<!DOCTYPE html>\n"
104 "<html>\n"
105 "<head>\n"
106 " <title>SkDebugger</title>\n"
107 " <meta charset=\"utf-8\" />\n"
108 " <meta http-equiv=\"X-UA-Compatible\" content=\"IE=egde,chrome=1\">\n"
109 " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n"
110 " <script src=\"%s/res/js/core.js\" type=\"text/javascript\" charset=\"utf-8\"></script>\n"
111 " <link href=\"%s/res/vul/elements.html\" rel=\"import\" />\n"
112 "</head>\n"
113 "<body class=\"fullbleed layout vertical\">\n"
114 " <debugger-app-sk>This is the app."
115 " </debugger-app-sk>\n"
116 "</body>\n"
117 "</html>", source.c_str(), source.c_str());
118 return debuggerTemplate;
119
120}
121
122int answer_to_connection(void* cls, struct MHD_Connection* connection,
123 const char* url, const char* method, const char* version,
124 const char* upload_data, size_t* upload_data_size,
125 void** con_cls) {
126 printf ("New %s request for %s using version %s\n", method, url, version);
127
128 struct MHD_Response* response;
129
130 // serve html to root
131 // TODO better url handling
132 int ret;
133 if (0 == strcmp("/", url)) {
134 SkString debuggerTemplate = generateTemplate(SkString("http://debugger.skia.org"));
135
136 response = MHD_create_response_from_buffer(debuggerTemplate.size(),
137 (void*)const_cast<char*>(debuggerTemplate.c_str()),
138 MHD_RESPMEM_MUST_COPY);
139 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
140 MHD_destroy_response(response);
141 return MHD_YES;
142 }
143
144 // otherwise serve an image
145 // TODO take from post
146 SkString resourceName;
147 resourceName.appendf("%s/%s.skp", FLAGS_dir[0], FLAGS_name[0]);
148 SkDebugf("Loading skp: %s\n", resourceName.c_str());
149
150 SkString error;
151 SkAutoTUnref<SkData> data(setupAndDrawToCanvas(resourceName.c_str(), &error));
152 if (!data) {
153 // TODO send error
154 return MHD_YES;
155 }
156
157 response = MHD_create_response_from_buffer(data->size(), const_cast<void*>(data->data()),
158 MHD_RESPMEM_MUST_COPY);
159 MHD_add_response_header(response, "Content-Type", "image/png");
160 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
161 MHD_destroy_response(response);
162 return ret;
163}
164
165int skiaserve_main() {
166 struct MHD_Daemon* daemon;
167 daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL,
168 &answer_to_connection, NULL, MHD_OPTION_END);
169 if (NULL == daemon) {
170 return 1;
171 }
172
173 getchar();
174 MHD_stop_daemon(daemon);
175 return 0;
176}
177
178#if !defined SK_BUILD_FOR_IOS
179int main(int argc, char** argv) {
180 SkCommandLineFlags::Parse(argc, argv);
181 return skiaserve_main();
182}
183#endif