Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(932)

Side by Side Diff: tools/skiaserve/skiaserve.cpp

Issue 1704573002: tuned skiaserve's png output for better performance (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: formatting Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « gyp/skiaserve.gyp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "SkJSONCPP.h" 14 #include "SkJSONCPP.h"
15 #include "SkPicture.h" 15 #include "SkPicture.h"
16 #include "SkPictureRecorder.h" 16 #include "SkPictureRecorder.h"
17 #include "SkPixelSerializer.h" 17 #include "SkPixelSerializer.h"
18 #include "SkStream.h" 18 #include "SkStream.h"
19 #include "SkSurface.h" 19 #include "SkSurface.h"
20 20
21 #include "UrlDataManager.h" 21 #include "UrlDataManager.h"
22 22
23 #include <sys/socket.h> 23 #include <sys/socket.h>
24 #include <microhttpd.h> 24 #include <microhttpd.h>
25 #include "png.h"
25 26
26 // To get image decoders linked in we have to do the below magic 27 // To get image decoders linked in we have to do the below magic
27 #include "SkForceLinking.h" 28 #include "SkForceLinking.h"
28 #include "SkImageDecoder.h" 29 #include "SkImageDecoder.h"
29 __SK_FORCE_IMAGE_DECODER_LINKING; 30 __SK_FORCE_IMAGE_DECODER_LINKING;
30 31
31 DEFINE_string(source, "https://debugger.skia.org", "Where to load the web UI fro m."); 32 DEFINE_string(source, "https://debugger.skia.org", "Where to load the web UI fro m.");
32 DEFINE_string(faviconDir, "tools/skiaserve", "The directory of the favicon"); 33 DEFINE_string(faviconDir, "tools/skiaserve", "The directory of the favicon");
33 DEFINE_int32(port, 8888, "The port to listen on."); 34 DEFINE_int32(port, 8888, "The port to listen on.");
34 35
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 kN32_SkColorType, kPremul_SkAlphaType); 86 kN32_SkColorType, kPremul_SkAlphaType);
86 uint32_t flags = 0; 87 uint32_t flags = 0;
87 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); 88 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
88 SkSurface* surface = SkSurface::NewRenderTarget(context, SkSurface::kNo_Budg eted, info, 0, 89 SkSurface* surface = SkSurface::NewRenderTarget(context, SkSurface::kNo_Budg eted, info, 0,
89 &props); 90 &props);
90 SkASSERT(surface); 91 SkASSERT(surface);
91 92
92 return surface; 93 return surface;
93 } 94 }
94 95
96 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);
98 out->write(data, length);
99 }
100
101 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);
103 SkASSERT(png != nullptr);
104 png_infop info_ptr = png_create_info_struct(png);
105 SkASSERT(info_ptr != nullptr);
106 if (setjmp(png_jmpbuf(png))) {
107 SkFAIL("png encode error");
108 }
109 png_set_IHDR(png, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGBA, PNG_INTER LACE_NONE,
110 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
111 png_set_compression_level(png, 1);
112 png_bytepp rows = (png_bytepp) sk_malloc_throw(height * sizeof(png_byte*));
113 for (png_size_t y = 0; y < height; ++y) {
114 rows[y] = (png_bytep) rgba + y * width * 4;
115 }
116 png_set_filter(png, 0, PNG_NO_FILTERS);
mtklein 2016/02/16 17:48:52 If you're interested in trying allowing any filter
117 png_set_rows(png, info_ptr, &rows[0]);
118 png_set_write_fn(png, &out, write_png_callback, NULL);
119 png_write_png(png, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
120 png_destroy_write_struct(&png, NULL);
121 sk_free(rows);
122 }
123
95 SkData* writeCanvasToPng(SkCanvas* canvas) { 124 SkData* writeCanvasToPng(SkCanvas* canvas) {
96 // capture pixels 125 // capture pixels
97 SkBitmap bmp; 126 SkBitmap bmp;
98 bmp.setInfo(canvas->imageInfo()); 127 bmp.setInfo(canvas->imageInfo());
99 if (!canvas->readPixels(&bmp, 0, 0)) { 128 if (!canvas->readPixels(&bmp, 0, 0)) {
100 fprintf(stderr, "Can't read pixels\n"); 129 fprintf(stderr, "Can't read pixels\n");
101 return nullptr; 130 return nullptr;
102 } 131 }
103 132
104 // write to png 133 // write to png
105 // TODO encoding to png can be quite slow, we should investigate bmp 134 SkDynamicMemoryWStream buffer;
106 SkData* png = SkImageEncoder::EncodeData(bmp, SkImageEncoder::kPNG_Type, 100 ); 135 write_png((const png_bytep) bmp.getPixels(), bmp.width(), bmp.height(), buff er);
107 if (!png) { 136 return buffer.copyToData();
108 fprintf(stderr, "Can't encode to png\n");
109 return nullptr;
110 }
111 return png;
112 } 137 }
113 138
114 SkData* setupAndDrawToCanvasReturnPng(Request* request, int n) { 139 SkData* setupAndDrawToCanvasReturnPng(Request* request, int n) {
115 GrContextFactory* factory = request->fContextFactory; 140 GrContextFactory* factory = request->fContextFactory;
116 SkGLContext* gl = factory->getContextInfo(GrContextFactory::kNative_GLContex tType, 141 SkGLContext* gl = factory->getContextInfo(GrContextFactory::kNative_GLContex tType,
117 GrContextFactory::kNone_GLContextO ptions).fGLContext; 142 GrContextFactory::kNone_GLContextO ptions).fGLContext;
118 gl->makeCurrent(); 143 gl->makeCurrent();
119 SkASSERT(request->fDebugCanvas); 144 SkASSERT(request->fDebugCanvas);
120 SkCanvas* target = request->fSurface->getCanvas(); 145 SkCanvas* target = request->fSurface->getCanvas();
121 request->fDebugCanvas->drawTo(target, n); 146 request->fDebugCanvas->drawTo(target, n);
(...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after
598 MHD_stop_daemon(daemon); 623 MHD_stop_daemon(daemon);
599 return 0; 624 return 0;
600 } 625 }
601 626
602 #if !defined SK_BUILD_FOR_IOS 627 #if !defined SK_BUILD_FOR_IOS
603 int main(int argc, char** argv) { 628 int main(int argc, char** argv) {
604 SkCommandLineFlags::Parse(argc, argv); 629 SkCommandLineFlags::Parse(argc, argv);
605 return skiaserve_main(); 630 return skiaserve_main();
606 } 631 }
607 #endif 632 #endif
OLDNEW
« no previous file with comments | « gyp/skiaserve.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698