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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 SkDynamicMemoryWStream fStream; | 63 SkDynamicMemoryWStream fStream; |
64 MHD_PostProcessor* fPostProcessor; | 64 MHD_PostProcessor* fPostProcessor; |
65 MHD_Connection* connection; | 65 MHD_Connection* connection; |
66 }; | 66 }; |
67 | 67 |
68 struct Request { | 68 struct Request { |
69 Request(SkString rootUrl) : fUploadContext(nullptr), fUrlDataManager(rootUrl
) {} | 69 Request(SkString rootUrl) : fUploadContext(nullptr), fUrlDataManager(rootUrl
) {} |
70 UploadContext* fUploadContext; | 70 UploadContext* fUploadContext; |
71 SkAutoTUnref<SkPicture> fPicture; | 71 SkAutoTUnref<SkPicture> fPicture; |
72 SkAutoTUnref<SkDebugCanvas> fDebugCanvas; | 72 SkAutoTUnref<SkDebugCanvas> fDebugCanvas; |
| 73 SkAutoTDelete<GrContextFactory> fContextFactory; |
| 74 SkAutoTUnref<SkSurface> fSurface; |
73 UrlDataManager fUrlDataManager; | 75 UrlDataManager fUrlDataManager; |
74 }; | 76 }; |
75 | 77 |
76 // TODO factor this out into functions, also handle CPU path | 78 // TODO factor this out into functions, also handle CPU path |
77 SkSurface* setupSurface(GrContextFactory* factory) { | 79 SkSurface* setupSurface(GrContextFactory* factory) { |
78 GrContext* context = factory->get(GrContextFactory::kNative_GLContextType, | 80 GrContext* context = factory->get(GrContextFactory::kNative_GLContextType, |
79 GrContextFactory::kNone_GLContextOptions); | 81 GrContextFactory::kNone_GLContextOptions); |
80 int maxRTSize = context->caps()->maxRenderTargetSize(); | 82 int maxRTSize = context->caps()->maxRenderTargetSize(); |
81 SkImageInfo info = SkImageInfo::Make(SkTMin(kImageWidth, maxRTSize), | 83 SkImageInfo info = SkImageInfo::Make(SkTMin(kImageWidth, maxRTSize), |
82 SkTMin(kImageHeight, maxRTSize), | 84 SkTMin(kImageHeight, maxRTSize), |
83 kN32_SkColorType, kPremul_SkAlphaType); | 85 kN32_SkColorType, kPremul_SkAlphaType); |
84 uint32_t flags = 0; | 86 uint32_t flags = 0; |
85 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); | 87 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); |
86 SkSurface* surface = SkSurface::NewRenderTarget(context, SkSurface::kNo_Budg
eted, info, 0, | 88 SkSurface* surface = SkSurface::NewRenderTarget(context, SkSurface::kNo_Budg
eted, info, 0, |
87 &props); | 89 &props); |
88 SkASSERT(surface); | 90 SkASSERT(surface); |
89 | 91 |
90 SkGLContext* gl = factory->getContextInfo(GrContextFactory::kNative_GLContex
tType, | |
91 GrContextFactory::kNone_GLContextO
ptions).fGLContext; | |
92 gl->makeCurrent(); | |
93 return surface; | 92 return surface; |
94 } | 93 } |
95 | 94 |
96 SkData* writeCanvasToPng(SkCanvas* canvas) { | 95 SkData* writeCanvasToPng(SkCanvas* canvas) { |
97 // capture pixels | 96 // capture pixels |
98 SkBitmap bmp; | 97 SkBitmap bmp; |
99 bmp.setInfo(canvas->imageInfo()); | 98 bmp.setInfo(canvas->imageInfo()); |
100 if (!canvas->readPixels(&bmp, 0, 0)) { | 99 if (!canvas->readPixels(&bmp, 0, 0)) { |
101 fprintf(stderr, "Can't read pixels\n"); | 100 fprintf(stderr, "Can't read pixels\n"); |
102 return nullptr; | 101 return nullptr; |
103 } | 102 } |
104 | 103 |
105 // write to png | 104 // write to png |
106 // TODO encoding to png can be quite slow, we should investigate bmp | 105 // TODO encoding to png can be quite slow, we should investigate bmp |
107 SkData* png = SkImageEncoder::EncodeData(bmp, SkImageEncoder::kPNG_Type, 100
); | 106 SkData* png = SkImageEncoder::EncodeData(bmp, SkImageEncoder::kPNG_Type, 100
); |
108 if (!png) { | 107 if (!png) { |
109 fprintf(stderr, "Can't encode to png\n"); | 108 fprintf(stderr, "Can't encode to png\n"); |
110 return nullptr; | 109 return nullptr; |
111 } | 110 } |
112 return png; | 111 return png; |
113 } | 112 } |
114 | 113 |
115 SkData* setupAndDrawToCanvasReturnPng(SkDebugCanvas* debugCanvas, int n) { | 114 SkData* setupAndDrawToCanvasReturnPng(Request* request, int n) { |
116 GrContextOptions grContextOpts; | 115 GrContextFactory* factory = request->fContextFactory; |
117 SkAutoTDelete<GrContextFactory> factory(new GrContextFactory(grContextOpts))
; | 116 SkGLContext* gl = factory->getContextInfo(GrContextFactory::kNative_GLContex
tType, |
118 SkAutoTUnref<SkSurface> surface(setupSurface(factory.get())); | 117 GrContextFactory::kNone_GLContextO
ptions).fGLContext; |
119 | 118 gl->makeCurrent(); |
120 SkASSERT(debugCanvas); | 119 SkASSERT(request->fDebugCanvas); |
121 SkCanvas* canvas = surface->getCanvas(); | 120 SkCanvas* target = request->fSurface->getCanvas(); |
122 debugCanvas->drawTo(canvas, n); | 121 request->fDebugCanvas->drawTo(target, n); |
123 return writeCanvasToPng(canvas); | 122 return writeCanvasToPng(target); |
124 } | 123 } |
125 | 124 |
126 SkSurface* setupCpuSurface() { | 125 SkSurface* setupCpuSurface() { |
127 SkImageInfo info = SkImageInfo::Make(kImageWidth, kImageHeight, kN32_SkColor
Type, | 126 SkImageInfo info = SkImageInfo::Make(kImageWidth, kImageHeight, kN32_SkColor
Type, |
128 kPremul_SkAlphaType); | 127 kPremul_SkAlphaType); |
129 return SkSurface::NewRaster(info); | 128 return SkSurface::NewRaster(info); |
130 } | 129 } |
131 | 130 |
132 static const size_t kBufferSize = 1024; | 131 static const size_t kBufferSize = 1024; |
133 | 132 |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 } | 279 } |
281 | 280 |
282 int n; | 281 int n; |
283 // /img or /img/N | 282 // /img or /img/N |
284 if (commands.count() == 1) { | 283 if (commands.count() == 1) { |
285 n = request->fDebugCanvas->getSize() - 1; | 284 n = request->fDebugCanvas->getSize() - 1; |
286 } else { | 285 } else { |
287 sscanf(commands[1].c_str(), "%d", &n); | 286 sscanf(commands[1].c_str(), "%d", &n); |
288 } | 287 } |
289 | 288 |
290 SkAutoTUnref<SkData> data(setupAndDrawToCanvasReturnPng(request->fDebugC
anvas, n)); | 289 SkAutoTUnref<SkData> data(setupAndDrawToCanvasReturnPng(request, n)); |
291 return SendData(connection, data, "image/png"); | 290 return SendData(connection, data, "image/png"); |
292 } | 291 } |
293 }; | 292 }; |
294 | 293 |
295 class PostHandler : public UrlHandler { | 294 class PostHandler : public UrlHandler { |
296 public: | 295 public: |
297 bool canHandle(const char* method, const char* url) override { | 296 bool canHandle(const char* method, const char* url) override { |
298 return 0 == strcmp(method, MHD_HTTP_METHOD_POST) && | 297 return 0 == strcmp(method, MHD_HTTP_METHOD_POST) && |
299 0 == strcmp(url, "/new"); | 298 0 == strcmp(url, "/new"); |
300 } | 299 } |
(...skipping 29 matching lines...) Expand all Loading... |
330 uc->fPostProcessor = nullptr; | 329 uc->fPostProcessor = nullptr; |
331 | 330 |
332 // parse picture from stream | 331 // parse picture from stream |
333 request->fPicture.reset( | 332 request->fPicture.reset( |
334 SkPicture::CreateFromStream(request->fUploadContext->fStream.detachA
sStream())); | 333 SkPicture::CreateFromStream(request->fUploadContext->fStream.detachA
sStream())); |
335 if (!request->fPicture.get()) { | 334 if (!request->fPicture.get()) { |
336 fprintf(stderr, "Could not create picture from stream.\n"); | 335 fprintf(stderr, "Could not create picture from stream.\n"); |
337 return MHD_NO; | 336 return MHD_NO; |
338 } | 337 } |
339 | 338 |
| 339 // create surface |
| 340 GrContextOptions grContextOpts; |
| 341 request->fContextFactory.reset(new GrContextFactory(grContextOpts)); |
| 342 request->fSurface.reset(setupSurface(request->fContextFactory.get())); |
| 343 |
340 // pour picture into debug canvas | 344 // pour picture into debug canvas |
341 request->fDebugCanvas.reset(new SkDebugCanvas(kImageWidth, kImageHeight)
); | 345 request->fDebugCanvas.reset(new SkDebugCanvas(kImageWidth, kImageHeight)
); |
342 request->fDebugCanvas->drawPicture(request->fPicture); | 346 request->fDebugCanvas->drawPicture(request->fPicture); |
343 | 347 |
344 // clear upload context | 348 // clear upload context |
345 delete request->fUploadContext; | 349 delete request->fUploadContext; |
346 request->fUploadContext = nullptr; | 350 request->fUploadContext = nullptr; |
347 | 351 |
348 return SendTemplate(connection, true, "/"); | 352 return SendTemplate(connection, true, "/"); |
349 } | 353 } |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
563 MHD_stop_daemon(daemon); | 567 MHD_stop_daemon(daemon); |
564 return 0; | 568 return 0; |
565 } | 569 } |
566 | 570 |
567 #if !defined SK_BUILD_FOR_IOS | 571 #if !defined SK_BUILD_FOR_IOS |
568 int main(int argc, char** argv) { | 572 int main(int argc, char** argv) { |
569 SkCommandLineFlags::Parse(argc, argv); | 573 SkCommandLineFlags::Parse(argc, argv); |
570 return skiaserve_main(); | 574 return skiaserve_main(); |
571 } | 575 } |
572 #endif | 576 #endif |
OLD | NEW |