OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrCaps.h" | 8 #include "GrCaps.h" |
9 #include "GrContextFactory.h" | 9 #include "GrContextFactory.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkCommandLineFlags.h" | 11 #include "SkCommandLineFlags.h" |
12 #include "SkDebugCanvas.h" | 12 #include "SkDebugCanvas.h" |
13 #include "SkJSONCanvas.h" | 13 #include "SkJSONCanvas.h" |
14 #include "SkPicture.h" | 14 #include "SkPicture.h" |
| 15 #include "SkPictureRecorder.h" |
| 16 #include "SkPixelSerializer.h" |
15 #include "SkStream.h" | 17 #include "SkStream.h" |
16 #include "SkSurface.h" | 18 #include "SkSurface.h" |
17 | 19 |
18 #include <sys/socket.h> | 20 #include <sys/socket.h> |
19 #include <microhttpd.h> | 21 #include <microhttpd.h> |
20 | 22 |
21 // To get image decoders linked in we have to do the below magic | 23 // To get image decoders linked in we have to do the below magic |
22 #include "SkForceLinking.h" | 24 #include "SkForceLinking.h" |
23 #include "SkImageDecoder.h" | 25 #include "SkImageDecoder.h" |
24 __SK_FORCE_IMAGE_DECODER_LINKING; | 26 __SK_FORCE_IMAGE_DECODER_LINKING; |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 const char* content_type, const char* transfer_en
coding, | 124 const char* content_type, const char* transfer_en
coding, |
123 const char* data, uint64_t off, size_t size) { | 125 const char* data, uint64_t off, size_t size) { |
124 struct UploadContext* uc = reinterpret_cast<UploadContext*>(cls); | 126 struct UploadContext* uc = reinterpret_cast<UploadContext*>(cls); |
125 | 127 |
126 if (0 != size) { | 128 if (0 != size) { |
127 uc->fStream.write(data, size); | 129 uc->fStream.write(data, size); |
128 } | 130 } |
129 return MHD_YES; | 131 return MHD_YES; |
130 } | 132 } |
131 | 133 |
132 static int SendData(MHD_Connection* connection, const SkData* data, const char*
type) { | 134 static int SendData(MHD_Connection* connection, const SkData* data, const char*
type, |
| 135 bool setContentDisposition = false, const char* dispositionS
tring = nullptr) { |
133 MHD_Response* response = MHD_create_response_from_buffer(data->size(), | 136 MHD_Response* response = MHD_create_response_from_buffer(data->size(), |
134 const_cast<void*>(d
ata->data()), | 137 const_cast<void*>(d
ata->data()), |
135 MHD_RESPMEM_MUST_CO
PY); | 138 MHD_RESPMEM_MUST_CO
PY); |
136 MHD_add_response_header(response, "Content-Type", type); | 139 MHD_add_response_header(response, "Content-Type", type); |
137 | 140 |
| 141 if (setContentDisposition) { |
| 142 MHD_add_response_header(response, "Content-Disposition", dispositionStri
ng); |
| 143 } |
| 144 |
138 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response); | 145 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response); |
139 MHD_destroy_response(response); | 146 MHD_destroy_response(response); |
140 return ret; | 147 return ret; |
141 } | 148 } |
142 | 149 |
143 static int SendJSON(MHD_Connection* connection, SkDebugCanvas* debugCanvas, int
n) { | 150 static int SendJSON(MHD_Connection* connection, SkDebugCanvas* debugCanvas, int
n) { |
144 SkDynamicMemoryWStream stream; | 151 SkDynamicMemoryWStream stream; |
145 SkAutoTUnref<SkJSONCanvas> jsonCanvas(new SkJSONCanvas(kImageWidth, kImageHe
ight, stream)); | 152 SkAutoTUnref<SkJSONCanvas> jsonCanvas(new SkJSONCanvas(kImageWidth, kImageHe
ight, stream)); |
146 debugCanvas->drawTo(jsonCanvas, n); | 153 debugCanvas->drawTo(jsonCanvas, n); |
147 jsonCanvas->finish(); | 154 jsonCanvas->finish(); |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 request->fDebugCanvas->drawPicture(request->fPicture); | 313 request->fDebugCanvas->drawPicture(request->fPicture); |
307 | 314 |
308 // clear upload context | 315 // clear upload context |
309 delete request->fUploadContext; | 316 delete request->fUploadContext; |
310 request->fUploadContext = nullptr; | 317 request->fUploadContext = nullptr; |
311 | 318 |
312 return SendTemplate(connection, true, "/"); | 319 return SendTemplate(connection, true, "/"); |
313 } | 320 } |
314 }; | 321 }; |
315 | 322 |
| 323 class DownloadHandler : public UrlHandler { |
| 324 public: |
| 325 bool canHandle(const char* method, const char* url) override { |
| 326 return 0 == strcmp(method, MHD_HTTP_METHOD_GET) && |
| 327 0 == strcmp(url, "/download"); |
| 328 } |
| 329 |
| 330 int handle(Request* request, MHD_Connection* connection, |
| 331 const char* url, const char* method, |
| 332 const char* upload_data, size_t* upload_data_size) override { |
| 333 if (!request->fPicture.get()) { |
| 334 return MHD_NO; |
| 335 } |
| 336 |
| 337 // TODO move to a function |
| 338 // Playback into picture recorder |
| 339 SkPictureRecorder recorder; |
| 340 SkCanvas* canvas = recorder.beginRecording(kImageWidth, kImageHeight); |
| 341 |
| 342 request->fDebugCanvas->draw(canvas); |
| 343 |
| 344 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 345 |
| 346 SkDynamicMemoryWStream outStream; |
| 347 |
| 348 SkAutoTUnref<SkPixelSerializer> serializer(SkImageEncoder::CreatePixelSe
rializer()); |
| 349 picture->serialize(&outStream, serializer); |
| 350 |
| 351 SkAutoTUnref<SkData> data(outStream.copyToData()); |
| 352 |
| 353 // TODO fancier name handling |
| 354 return SendData(connection, data, "application/octet-stream", true, |
| 355 "attachment; filename=something.skp;"); |
| 356 } |
| 357 }; |
| 358 |
316 class RootHandler : public UrlHandler { | 359 class RootHandler : public UrlHandler { |
317 public: | 360 public: |
318 bool canHandle(const char* method, const char* url) override { | 361 bool canHandle(const char* method, const char* url) override { |
319 return 0 == strcmp(method, MHD_HTTP_METHOD_GET) && | 362 return 0 == strcmp(method, MHD_HTTP_METHOD_GET) && |
320 0 == strcmp(url, "/"); | 363 0 == strcmp(url, "/"); |
321 } | 364 } |
322 | 365 |
323 int handle(Request* request, MHD_Connection* connection, | 366 int handle(Request* request, MHD_Connection* connection, |
324 const char* url, const char* method, | 367 const char* url, const char* method, |
325 const char* upload_data, size_t* upload_data_size) override { | 368 const char* upload_data, size_t* upload_data_size) override { |
326 return SendTemplate(connection); | 369 return SendTemplate(connection); |
327 } | 370 } |
328 }; | 371 }; |
329 | 372 |
330 class UrlManager { | 373 class UrlManager { |
331 public: | 374 public: |
332 UrlManager() { | 375 UrlManager() { |
333 // Register handlers | 376 // Register handlers |
334 fHandlers.push_back(new RootHandler); | 377 fHandlers.push_back(new RootHandler); |
335 fHandlers.push_back(new PostHandler); | 378 fHandlers.push_back(new PostHandler); |
336 fHandlers.push_back(new ImgHandler); | 379 fHandlers.push_back(new ImgHandler); |
337 fHandlers.push_back(new InfoHandler); | 380 fHandlers.push_back(new InfoHandler); |
| 381 fHandlers.push_back(new DownloadHandler); |
338 } | 382 } |
339 | 383 |
340 ~UrlManager() { | 384 ~UrlManager() { |
341 for (int i = 0; i < fHandlers.count(); i++) { delete fHandlers[i]; } | 385 for (int i = 0; i < fHandlers.count(); i++) { delete fHandlers[i]; } |
342 } | 386 } |
343 | 387 |
344 // This is clearly not efficient for a large number of urls and handlers | 388 // This is clearly not efficient for a large number of urls and handlers |
345 int invoke(Request* request, MHD_Connection* connection, const char* url, co
nst char* method, | 389 int invoke(Request* request, MHD_Connection* connection, const char* url, co
nst char* method, |
346 const char* upload_data, size_t* upload_data_size) const { | 390 const char* upload_data, size_t* upload_data_size) const { |
347 for (int i = 0; i < fHandlers.count(); i++) { | 391 for (int i = 0; i < fHandlers.count(); i++) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 MHD_stop_daemon(daemon); | 433 MHD_stop_daemon(daemon); |
390 return 0; | 434 return 0; |
391 } | 435 } |
392 | 436 |
393 #if !defined SK_BUILD_FOR_IOS | 437 #if !defined SK_BUILD_FOR_IOS |
394 int main(int argc, char** argv) { | 438 int main(int argc, char** argv) { |
395 SkCommandLineFlags::Parse(argc, argv); | 439 SkCommandLineFlags::Parse(argc, argv); |
396 return skiaserve_main(); | 440 return skiaserve_main(); |
397 } | 441 } |
398 #endif | 442 #endif |
OLD | NEW |