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" |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
69 struct Request { | 69 struct Request { |
70 Request(SkString rootUrl) : fUploadContext(nullptr), fUrlDataManager(rootUrl ) {} | 70 Request(SkString rootUrl) : fUploadContext(nullptr), fUrlDataManager(rootUrl ) {} |
71 UploadContext* fUploadContext; | 71 UploadContext* fUploadContext; |
72 SkAutoTUnref<SkPicture> fPicture; | 72 SkAutoTUnref<SkPicture> fPicture; |
73 SkAutoTUnref<SkDebugCanvas> fDebugCanvas; | 73 SkAutoTUnref<SkDebugCanvas> fDebugCanvas; |
74 SkAutoTDelete<GrContextFactory> fContextFactory; | 74 SkAutoTDelete<GrContextFactory> fContextFactory; |
75 SkAutoTUnref<SkSurface> fSurface; | 75 SkAutoTUnref<SkSurface> fSurface; |
76 UrlDataManager fUrlDataManager; | 76 UrlDataManager fUrlDataManager; |
77 }; | 77 }; |
78 | 78 |
79 // TODO factor this out into functions, also handle CPU path | |
80 SkSurface* setupSurface(GrContextFactory* factory) { | |
81 GrContext* context = factory->get(GrContextFactory::kNative_GLContextType, | |
82 GrContextFactory::kNone_GLContextOptions); | |
83 int maxRTSize = context->caps()->maxRenderTargetSize(); | |
84 SkImageInfo info = SkImageInfo::Make(SkTMin(kImageWidth, maxRTSize), | |
85 SkTMin(kImageHeight, maxRTSize), | |
86 kN32_SkColorType, kPremul_SkAlphaType); | |
87 uint32_t flags = 0; | |
88 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); | |
89 SkSurface* surface = SkSurface::NewRenderTarget(context, SkSurface::kNo_Budg eted, info, 0, | |
90 &props); | |
91 SkASSERT(surface); | |
92 | |
93 return surface; | |
94 } | |
95 | |
96 static void write_png_callback(png_structp png_ptr, png_bytep data, png_size_t l ength) { | 79 static void write_png_callback(png_structp png_ptr, png_bytep data, png_size_t l ength) { |
97 SkWStream* out = (SkWStream*) png_get_io_ptr(png_ptr); | 80 SkWStream* out = (SkWStream*) png_get_io_ptr(png_ptr); |
98 out->write(data, length); | 81 out->write(data, length); |
99 } | 82 } |
100 | 83 |
101 static void write_png(const png_bytep rgba, png_uint_32 width, png_uint_32 heigh t, SkWStream& out) { | 84 static void write_png(const png_bytep rgba, png_uint_32 width, png_uint_32 heigh t, SkWStream& out) { |
102 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); | 85 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
103 SkASSERT(png != nullptr); | 86 SkASSERT(png != nullptr); |
104 png_infop info_ptr = png_create_info_struct(png); | 87 png_infop info_ptr = png_create_info_struct(png); |
105 SkASSERT(info_ptr != nullptr); | 88 SkASSERT(info_ptr != nullptr); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
165 void drawToCanvas(Request* request, int n) { | 148 void drawToCanvas(Request* request, int n) { |
166 SkCanvas* target = getCanvasFromRequest(request); | 149 SkCanvas* target = getCanvasFromRequest(request); |
167 request->fDebugCanvas->drawTo(target, n); | 150 request->fDebugCanvas->drawTo(target, n); |
168 } | 151 } |
169 | 152 |
170 SkData* drawToPng(Request* request, int n) { | 153 SkData* drawToPng(Request* request, int n) { |
171 drawToCanvas(request, n); | 154 drawToCanvas(request, n); |
172 return writeCanvasToPng(getCanvasFromRequest(request)); | 155 return writeCanvasToPng(getCanvasFromRequest(request)); |
173 } | 156 } |
174 | 157 |
175 SkSurface* setupCpuSurface() { | 158 SkSurface* createCPUSurface() { |
176 SkImageInfo info = SkImageInfo::Make(kImageWidth, kImageHeight, kN32_SkColor Type, | 159 SkImageInfo info = SkImageInfo::Make(kImageWidth, kImageHeight, kN32_SkColor Type, |
177 kPremul_SkAlphaType); | 160 kPremul_SkAlphaType); |
178 return SkSurface::NewRaster(info); | 161 return SkSurface::NewRaster(info); |
179 } | 162 } |
180 | 163 |
164 SkSurface* createGPUSurface(Request* request) { | |
165 GrContext* context = request->fContextFactory->get(GrContextFactory::kNative _GLContextType, | |
166 GrContextFactory::kNone_G LContextOptions); | |
167 int maxRTSize = context->caps()->maxRenderTargetSize(); | |
168 SkImageInfo info = SkImageInfo::Make(SkTMin(kImageWidth, maxRTSize), | |
169 SkTMin(kImageHeight, maxRTSize), | |
170 kN32_SkColorType, kPremul_SkAlphaType); | |
171 uint32_t flags = 0; | |
172 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); | |
173 SkSurface* surface = SkSurface::NewRenderTarget(context, SkSurface::kNo_Budg eted, info, 0, | |
174 &props); | |
175 SkASSERT(surface); | |
jcgregorio
2016/02/19 15:23:56
Instead of asserting how about checking for a NULL
| |
176 | |
177 return surface; | |
178 } | |
179 | |
181 static const size_t kBufferSize = 1024; | 180 static const size_t kBufferSize = 1024; |
182 | 181 |
183 static int process_upload_data(void* cls, enum MHD_ValueKind kind, | 182 static int process_upload_data(void* cls, enum MHD_ValueKind kind, |
184 const char* key, const char* filename, | 183 const char* key, const char* filename, |
185 const char* content_type, const char* transfer_en coding, | 184 const char* content_type, const char* transfer_en coding, |
186 const char* data, uint64_t off, size_t size) { | 185 const char* data, uint64_t off, size_t size) { |
187 struct UploadContext* uc = reinterpret_cast<UploadContext*>(cls); | 186 struct UploadContext* uc = reinterpret_cast<UploadContext*>(cls); |
188 | 187 |
189 if (0 != size) { | 188 if (0 != size) { |
190 uc->fStream.write(data, size); | 189 uc->fStream.write(data, size); |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
451 } | 450 } |
452 | 451 |
453 int alpha; | 452 int alpha; |
454 sscanf(commands[1].c_str(), "%d", &alpha); | 453 sscanf(commands[1].c_str(), "%d", &alpha); |
455 | 454 |
456 request->fDebugCanvas->setClipVizColor(SkColorSetARGB(alpha, 0, 0, 0)); | 455 request->fDebugCanvas->setClipVizColor(SkColorSetARGB(alpha, 0, 0, 0)); |
457 return SendOK(connection); | 456 return SendOK(connection); |
458 } | 457 } |
459 }; | 458 }; |
460 | 459 |
460 /** | |
461 Controls whether GPU rendering is enabled. Posting to /enableGPU/1 turns GPU on, /enableGPU/0 | |
462 disables it. | |
463 */ | |
464 class EnableGPUHandler : public UrlHandler { | |
465 public: | |
466 bool canHandle(const char* method, const char* url) override { | |
467 static const char* kBasePath = "/enableGPU/"; | |
468 return 0 == strcmp(method, MHD_HTTP_METHOD_POST) && | |
469 0 == strncmp(url, kBasePath, strlen(kBasePath)); | |
470 } | |
471 | |
472 int handle(Request* request, MHD_Connection* connection, | |
473 const char* url, const char* method, | |
474 const char* upload_data, size_t* upload_data_size) override { | |
475 SkTArray<SkString> commands; | |
476 SkStrSplit(url, "/", &commands); | |
477 | |
478 if (commands.count() != 2) { | |
479 return MHD_NO; | |
480 } | |
481 | |
482 int enable; | |
483 sscanf(commands[1].c_str(), "%d", &enable); | |
484 | |
485 if (enable) { | |
486 request->fSurface.reset(createGPUSurface(request)); | |
487 } | |
488 else { | |
489 request->fSurface.reset(createCPUSurface()); | |
490 } | |
491 return SendOK(connection); | |
492 } | |
493 }; | |
494 | |
461 class PostHandler : public UrlHandler { | 495 class PostHandler : public UrlHandler { |
462 public: | 496 public: |
463 bool canHandle(const char* method, const char* url) override { | 497 bool canHandle(const char* method, const char* url) override { |
464 return 0 == strcmp(method, MHD_HTTP_METHOD_POST) && | 498 return 0 == strcmp(method, MHD_HTTP_METHOD_POST) && |
465 0 == strcmp(url, "/new"); | 499 0 == strcmp(url, "/new"); |
466 } | 500 } |
467 | 501 |
468 int handle(Request* request, MHD_Connection* connection, | 502 int handle(Request* request, MHD_Connection* connection, |
469 const char* url, const char* method, | 503 const char* url, const char* method, |
470 const char* upload_data, size_t* upload_data_size) override { | 504 const char* upload_data, size_t* upload_data_size) override { |
(...skipping 28 matching lines...) Expand all Loading... | |
499 request->fPicture.reset( | 533 request->fPicture.reset( |
500 SkPicture::CreateFromStream(request->fUploadContext->fStream.detachA sStream())); | 534 SkPicture::CreateFromStream(request->fUploadContext->fStream.detachA sStream())); |
501 if (!request->fPicture.get()) { | 535 if (!request->fPicture.get()) { |
502 fprintf(stderr, "Could not create picture from stream.\n"); | 536 fprintf(stderr, "Could not create picture from stream.\n"); |
503 return MHD_NO; | 537 return MHD_NO; |
504 } | 538 } |
505 | 539 |
506 // create surface | 540 // create surface |
507 GrContextOptions grContextOpts; | 541 GrContextOptions grContextOpts; |
508 request->fContextFactory.reset(new GrContextFactory(grContextOpts)); | 542 request->fContextFactory.reset(new GrContextFactory(grContextOpts)); |
509 request->fSurface.reset(setupSurface(request->fContextFactory.get())); | 543 request->fSurface.reset(createGPUSurface(request)); |
510 | 544 |
511 // pour picture into debug canvas | 545 // pour picture into debug canvas |
512 request->fDebugCanvas.reset(new SkDebugCanvas(kImageWidth, kImageHeight) ); | 546 request->fDebugCanvas.reset(new SkDebugCanvas(kImageWidth, kImageHeight) ); |
513 request->fDebugCanvas->drawPicture(request->fPicture); | 547 request->fDebugCanvas->drawPicture(request->fPicture); |
514 | 548 |
515 // clear upload context | 549 // clear upload context |
516 delete request->fUploadContext; | 550 delete request->fUploadContext; |
517 request->fUploadContext = nullptr; | 551 request->fUploadContext = nullptr; |
518 | 552 |
519 return SendTemplate(connection, true, "/"); | 553 return SendTemplate(connection, true, "/"); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
568 const char* url, const char* method, | 602 const char* url, const char* method, |
569 const char* upload_data, size_t* upload_data_size) override { | 603 const char* upload_data, size_t* upload_data_size) override { |
570 SkTArray<SkString> commands; | 604 SkTArray<SkString> commands; |
571 SkStrSplit(url, "/", &commands); | 605 SkStrSplit(url, "/", &commands); |
572 | 606 |
573 if (!request->fPicture.get() || commands.count() > 2) { | 607 if (!request->fPicture.get() || commands.count() > 2) { |
574 return MHD_NO; | 608 return MHD_NO; |
575 } | 609 } |
576 | 610 |
577 // drawTo | 611 // drawTo |
578 SkAutoTUnref<SkSurface> surface(setupCpuSurface()); | 612 SkAutoTUnref<SkSurface> surface(createCPUSurface()); |
579 SkCanvas* canvas = surface->getCanvas(); | 613 SkCanvas* canvas = surface->getCanvas(); |
580 | 614 |
581 int n; | 615 int n; |
582 // /info or /info/N | 616 // /info or /info/N |
583 if (commands.count() == 1) { | 617 if (commands.count() == 1) { |
584 n = request->fDebugCanvas->getSize() - 1; | 618 n = request->fDebugCanvas->getSize() - 1; |
585 } else { | 619 } else { |
586 sscanf(commands[1].c_str(), "%d", &n); | 620 sscanf(commands[1].c_str(), "%d", &n); |
587 } | 621 } |
588 | 622 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
669 }; | 703 }; |
670 | 704 |
671 class UrlManager { | 705 class UrlManager { |
672 public: | 706 public: |
673 UrlManager() { | 707 UrlManager() { |
674 // Register handlers | 708 // Register handlers |
675 fHandlers.push_back(new RootHandler); | 709 fHandlers.push_back(new RootHandler); |
676 fHandlers.push_back(new PostHandler); | 710 fHandlers.push_back(new PostHandler); |
677 fHandlers.push_back(new ImgHandler); | 711 fHandlers.push_back(new ImgHandler); |
678 fHandlers.push_back(new ClipAlphaHandler); | 712 fHandlers.push_back(new ClipAlphaHandler); |
713 fHandlers.push_back(new EnableGPUHandler); | |
679 fHandlers.push_back(new CmdHandler); | 714 fHandlers.push_back(new CmdHandler); |
680 fHandlers.push_back(new InfoHandler); | 715 fHandlers.push_back(new InfoHandler); |
681 fHandlers.push_back(new DownloadHandler); | 716 fHandlers.push_back(new DownloadHandler); |
682 fHandlers.push_back(new DataHandler); | 717 fHandlers.push_back(new DataHandler); |
683 fHandlers.push_back(new FaviconHandler); | 718 fHandlers.push_back(new FaviconHandler); |
684 fHandlers.push_back(new BreakHandler); | 719 fHandlers.push_back(new BreakHandler); |
685 } | 720 } |
686 | 721 |
687 ~UrlManager() { | 722 ~UrlManager() { |
688 for (int i = 0; i < fHandlers.count(); i++) { delete fHandlers[i]; } | 723 for (int i = 0; i < fHandlers.count(); i++) { delete fHandlers[i]; } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
736 MHD_stop_daemon(daemon); | 771 MHD_stop_daemon(daemon); |
737 return 0; | 772 return 0; |
738 } | 773 } |
739 | 774 |
740 #if !defined SK_BUILD_FOR_IOS | 775 #if !defined SK_BUILD_FOR_IOS |
741 int main(int argc, char** argv) { | 776 int main(int argc, char** argv) { |
742 SkCommandLineFlags::Parse(argc, argv); | 777 SkCommandLineFlags::Parse(argc, argv); |
743 return skiaserve_main(); | 778 return skiaserve_main(); |
744 } | 779 } |
745 #endif | 780 #endif |
OLD | NEW |