OLD | NEW |
1 #include <sys/time.h> | 1 #include <sys/time.h> |
2 #include <sys/resource.h> | 2 #include <sys/resource.h> |
3 | 3 |
4 #include "GrContextFactory.h" | 4 #include "GrContextFactory.h" |
5 | 5 |
6 #include "SkCanvas.h" | 6 #include "SkCanvas.h" |
7 #include "SkCommandLineFlags.h" | 7 #include "SkCommandLineFlags.h" |
8 #include "SkData.h" | 8 #include "SkData.h" |
9 #include "SkForceLinking.h" | 9 #include "SkForceLinking.h" |
10 #include "SkGraphics.h" | 10 #include "SkGraphics.h" |
11 #include "SkImageDecoder.h" | 11 #include "SkImageDecoder.h" |
12 #include "SkImageEncoder.h" | 12 #include "SkImageEncoder.h" |
13 #include "SkImageInfo.h" | 13 #include "SkImageInfo.h" |
14 #include "SkOSFile.h" | 14 #include "SkOSFile.h" |
15 #include "SkStream.h" | 15 #include "SkStream.h" |
16 #include "SkSurface.h" | 16 #include "SkSurface.h" |
17 | 17 |
18 #include "seccomp_bpf.h" | 18 #include "seccomp_bpf.h" |
19 | 19 |
20 __SK_FORCE_IMAGE_DECODER_LINKING; | 20 __SK_FORCE_IMAGE_DECODER_LINKING; |
21 | 21 |
22 DEFINE_string(out, "", "Filename of the PNG to write to."); | 22 DEFINE_string(out, "", "Output basename; fiddle will append the config used and
the appropriate extension"); |
23 DEFINE_string(source, "", "Filename of the source image."); | 23 DEFINE_string(source, "", "Filename of the source image."); |
24 DEFINE_int32(width, 256, "Width of output image."); | 24 DEFINE_int32(width, 256, "Width of output image."); |
25 DEFINE_int32(height, 256, "Height of output image."); | 25 DEFINE_int32(height, 256, "Height of output image."); |
26 DEFINE_bool(gpu, false, "Use GPU (Mesa) rendering."); | 26 DEFINE_bool(gpu, false, "Use GPU (Mesa) rendering."); |
| 27 DEFINE_bool(raster, true, "Use Raster rendering."); |
| 28 DEFINE_bool(pdf, false, "Use PDF rendering."); |
27 | 29 |
28 // Defined in template.cpp. | 30 // Defined in template.cpp. |
29 extern SkBitmap source; | 31 extern SkBitmap source; |
30 | 32 |
31 static bool install_syscall_filter() { | 33 static bool install_syscall_filter() { |
32 | 34 |
33 #ifndef SK_UNSAFE_BUILD_DESKTOP_ONLY | 35 #ifndef SK_UNSAFE_BUILD_DESKTOP_ONLY |
34 struct sock_filter filter[] = { | 36 struct sock_filter filter[] = { |
35 /* Grab the system call number. */ | 37 /* Grab the system call number. */ |
36 EXAMINE_SYSCALL, | 38 EXAMINE_SYSCALL, |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 // Limit to 150M of Address space. | 93 // Limit to 150M of Address space. |
92 n.rlim_cur = 150000000; | 94 n.rlim_cur = 150000000; |
93 n.rlim_max = 150000000; | 95 n.rlim_max = 150000000; |
94 if (setrlimit(RLIMIT_AS, &n)) { | 96 if (setrlimit(RLIMIT_AS, &n)) { |
95 perror("setrlimit(RLIMIT_CPU)"); | 97 perror("setrlimit(RLIMIT_CPU)"); |
96 } | 98 } |
97 } | 99 } |
98 | 100 |
99 extern void draw(SkCanvas* canvas); | 101 extern void draw(SkCanvas* canvas); |
100 | 102 |
| 103 static void drawAndDump(SkSurface* surface, SkWStream* stream) { |
| 104 SkCanvas *canvas = surface->getCanvas(); |
| 105 draw(canvas); |
| 106 |
| 107 // Write out the image as a PNG. |
| 108 SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); |
| 109 SkAutoTUnref<SkData> data(image->encode(SkImageEncoder::kPNG_Type, 100)); |
| 110 if (NULL == data.get()) { |
| 111 printf("Failed to encode\n"); |
| 112 exit(1); |
| 113 } |
| 114 stream->write(data->data(), data->size()); |
| 115 } |
| 116 |
| 117 static void drawRaster(SkWStream* stream, SkImageInfo info) { |
| 118 SkAutoTUnref<SkSurface> surface; |
| 119 surface.reset(SkSurface::NewRaster(info)); |
| 120 drawAndDump(surface, stream); |
| 121 } |
| 122 |
| 123 static void drawGPU(SkWStream* stream, SkImageInfo info) { |
| 124 SkAutoTUnref<SkSurface> surface; |
| 125 GrContextFactory* grFactory = NULL; |
| 126 |
| 127 GrContext::Options grContextOpts; |
| 128 grFactory = new GrContextFactory(grContextOpts); |
| 129 GrContext* gr = grFactory->get(GrContextFactory::kMESA_GLContextType); |
| 130 surface.reset(SkSurface::NewRenderTarget(gr,info)); |
| 131 |
| 132 drawAndDump(surface, stream); |
| 133 |
| 134 delete grFactory; |
| 135 } |
| 136 |
| 137 static void drawPDF(SkWStream* stream, SkImageInfo info) { |
| 138 printf( "Not implemented yet...\n"); |
| 139 } |
| 140 |
101 int main(int argc, char** argv) { | 141 int main(int argc, char** argv) { |
102 SkCommandLineFlags::Parse(argc, argv); | 142 SkCommandLineFlags::Parse(argc, argv); |
103 SkAutoGraphics init; | 143 SkAutoGraphics init; |
104 | 144 |
105 if (FLAGS_out.count() == 0) { | 145 if (FLAGS_out.count() == 0) { |
106 perror("The --out flag must have an argument."); | 146 perror("The --out flag must have an argument."); |
107 return 1; | 147 return 1; |
108 } | 148 } |
109 | 149 |
110 if (FLAGS_source.count() == 1) { | 150 if (FLAGS_source.count() == 1) { |
111 const char *sourceDir = getenv("WEBTRY_INOUT"); | 151 const char *sourceDir = getenv("WEBTRY_INOUT"); |
112 if (NULL == sourceDir) { | 152 if (NULL == sourceDir) { |
113 sourceDir = "/skia_build/inout"; | 153 sourceDir = "/skia_build/inout"; |
114 } | 154 } |
115 | 155 |
116 SkString sourcePath = SkOSPath::Join(sourceDir, FLAGS_source[0]); | 156 SkString sourcePath = SkOSPath::Join(sourceDir, FLAGS_source[0]); |
117 if (!SkImageDecoder::DecodeFile(sourcePath.c_str(), &source)) { | 157 if (!SkImageDecoder::DecodeFile(sourcePath.c_str(), &source)) { |
118 perror("Unable to read the source image."); | 158 perror("Unable to read the source image."); |
119 } | 159 } |
120 } | 160 } |
121 | 161 |
122 SkFILEWStream stream(FLAGS_out[0]); | 162 // make sure to open any needed output files before we set up the security |
| 163 // jail |
| 164 |
| 165 SkWStream* streams[3]; |
| 166 |
| 167 if (FLAGS_raster) { |
| 168 SkString outPath; |
| 169 outPath.printf("%s_raster.png", FLAGS_out[0]); |
| 170 streams[0] = SkNEW_ARGS(SkFILEWStream,(outPath.c_str())); |
| 171 } |
| 172 if (FLAGS_gpu) { |
| 173 SkString outPath; |
| 174 outPath.printf("%s_gpu.png", FLAGS_out[0]); |
| 175 streams[1] = SkNEW_ARGS(SkFILEWStream,(outPath.c_str())); |
| 176 } |
| 177 if (FLAGS_pdf) { |
| 178 SkString outPath; |
| 179 outPath.printf("%s.pdf", FLAGS_out[0]); |
| 180 streams[2] = SkNEW_ARGS(SkFILEWStream,(outPath.c_str())); |
| 181 } |
123 | 182 |
124 SkImageInfo info = SkImageInfo::MakeN32(FLAGS_width, FLAGS_height, kPremul_S
kAlphaType); | 183 SkImageInfo info = SkImageInfo::MakeN32(FLAGS_width, FLAGS_height, kPremul_S
kAlphaType); |
125 | 184 |
126 SkCanvas* canvas; | |
127 SkAutoTUnref<SkSurface> surface; | |
128 | |
129 GrContextFactory* grFactory = NULL; | |
130 | |
131 if (FLAGS_gpu) { | |
132 GrContext::Options grContextOpts; | |
133 grFactory = new GrContextFactory(grContextOpts); | |
134 GrContext* gr = grFactory->get(GrContextFactory::kMESA_GLContextType); | |
135 surface.reset(SkSurface::NewRenderTarget(gr,info)); | |
136 } else { | |
137 surface.reset(SkSurface::NewRaster(info)); | |
138 } | |
139 | |
140 canvas = surface->getCanvas(); | |
141 | |
142 setLimits(); | 185 setLimits(); |
143 | 186 |
144 if (!install_syscall_filter()) { | 187 if (!install_syscall_filter()) { |
145 return 1; | 188 return 1; |
146 } | 189 } |
147 | 190 |
148 draw(canvas); | 191 if (FLAGS_raster) { |
149 | 192 drawRaster(streams[0], info); |
150 // Write out the image as a PNG. | |
151 SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); | |
152 SkAutoTUnref<SkData> data(image->encode(SkImageEncoder::kPNG_Type, 100)); | |
153 if (NULL == data.get()) { | |
154 printf("Failed to encode\n"); | |
155 exit(1); | |
156 } | 193 } |
157 stream.write(data->data(), data->size()); | 194 if (FLAGS_gpu) { |
158 delete grFactory; | 195 drawGPU(streams[1], info); |
| 196 } |
| 197 if (FLAGS_pdf) { |
| 198 drawPDF(streams[2], info); |
| 199 } |
159 } | 200 } |
OLD | NEW |