2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "SkGraphics.h"
14 #include "SkPicture.h"
18 #include "PictureRenderer.h"
19 #include "picture_utils.h"
21 static void usage(const char* argv0) {
22 SkDebugf("SkPicture rendering tool\n");
25 " %s <input>... <outputDir> \n"
26 " [--mode pipe | pow2tile minWidth height[%] | simple\n"
27 " | tile width[%] height[%]]\n"
36 " input: A list of directories and files to use as input. Files are\n"
37 " expected to have the .skp extension.\n\n");
39 " outputDir: directory to write the rendered images.\n\n");
41 " --mode pipe | pow2tile minWidth height[%] | simple\n"
42 " | tile width[%] height[%]: Run in the corresponding mode.\n"
43 " Default is simple.\n");
45 " pipe, Render using a SkGPipe.\n");
47 " pow2tile minWidth height[%], Creates tiles with widths\n"
48 " that are all a power of two\n"
49 " such that they minimize the\n"
50 " amount of wasted tile space.\n"
51 " minWidth is the minimum width\n"
52 " of these tiles and must be a\n"
53 " power of two. A simple render\n"
54 " is done with these tiles.\n");
56 " simple, Render using the default rendering method.\n");
58 " tile width[%] height[%], Do a simple render using tiles\n"
59 " with the given dimensions.\n");
66 ": Use the corresponding device. Default is bitmap.\n");
68 " bitmap, Render to a bitmap.\n");
71 " gpu, Render to the GPU.\n");
75 static void make_output_filepath(SkString* path, const SkString& dir,
76 const SkString& name) {
77 sk_tools::make_filepath(path, dir, name);
79 path->remove(path->size() - 4, 4);
82 static bool render_picture(const SkString& inputPath, const SkString& outputDir,
83 sk_tools::PictureRenderer& renderer) {
84 SkString inputFilename;
85 sk_tools::get_basename(&inputFilename, inputPath);
87 SkFILEStream inputStream;
88 inputStream.setPath(inputPath.c_str());
89 if (!inputStream.isValid()) {
90 SkDebugf("Could not open file %s\n", inputPath.c_str());
95 SkPicture picture(&inputStream, &success);
97 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str());
101 SkDebugf("drawing... [%i %i] %s\n", picture.width(), picture.height(),
104 renderer.init(&picture);
108 make_output_filepath(&outputPath, outputDir, inputFilename);
110 success = renderer.render(&outputPath);
112 SkDebugf("Could not write to file %s\n", outputPath.c_str());
115 renderer.resetState();
121 static int process_input(const SkString& input, const SkString& outputDir,
122 sk_tools::PictureRenderer& renderer) {
123 SkOSFile::Iter iter(input.c_str(), "skp");
124 SkString inputFilename;
126 if (iter.next(&inputFilename)) {
129 sk_tools::make_filepath(&inputPath, input, inputFilename);
130 if (!render_picture(inputPath, outputDir, renderer)) {
133 } while(iter.next(&inputFilename));
134 } else if (SkStrEndsWith(input.c_str(), ".skp")) {
135 SkString inputPath(input);
136 if (!render_picture(inputPath, outputDir, renderer)) {
141 warning.printf("Warning: skipping %s\n", input.c_str());
142 SkDebugf(warning.c_str());
147 static void parse_commandline(int argc, char* const argv[], SkTArray<SkString>* inputs,
148 sk_tools::PictureRenderer*& renderer){
149 const char* argv0 = argv[0];
150 char* const* stop = argv + argc;
152 sk_tools::PictureRenderer::SkDeviceTypes deviceType =
153 sk_tools::PictureRenderer::kBitmap_DeviceType;
155 for (++argv; argv < stop; ++argv) {
156 if (0 == strcmp(*argv, "--mode")) {
161 SkDebugf("Missing mode for --mode\n");
166 if (0 == strcmp(*argv, "pipe")) {
167 renderer = SkNEW(sk_tools::PipePictureRenderer);
168 } else if (0 == strcmp(*argv, "simple")) {
169 renderer = SkNEW(sk_tools::SimplePictureRenderer);
170 } else if ((0 == strcmp(*argv, "tile")) || (0 == strcmp(*argv, "pow2tile"))) {
172 bool isPowerOf2Mode = false;
174 if (0 == strcmp(*argv, "pow2tile")) {
175 isPowerOf2Mode = true;
178 sk_tools::TiledPictureRenderer* tileRenderer =
179 SkNEW(sk_tools::TiledPictureRenderer);
182 SkDELETE(tileRenderer);
183 SkDebugf("Missing width for --mode %s\n", mode);
188 if (isPowerOf2Mode) {
189 int minWidth = atoi(*argv);
191 if (!SkIsPow2(minWidth) || minWidth <= 0) {
192 SkDELETE(tileRenderer);
193 SkDebugf("--mode %s must be given a width"
194 " value that is a power of two\n", mode);
198 tileRenderer->setTileMinPowerOf2Width(minWidth);
199 } else if (sk_tools::is_percentage(*argv)) {
200 tileRenderer->setTileWidthPercentage(atof(*argv));
201 if (!(tileRenderer->getTileWidthPercentage() > 0)) {
202 SkDELETE(tileRenderer);
203 SkDebugf("--mode %s must be given a width percentage > 0\n", mode);
207 tileRenderer->setTileWidth(atoi(*argv));
208 if (!(tileRenderer->getTileWidth() > 0)) {
209 SkDELETE(tileRenderer);
210 SkDebugf("--mode %s must be given a width > 0\n", mode);
217 SkDELETE(tileRenderer);
218 SkDebugf("Missing height for --mode %s\n", mode);
223 if (sk_tools::is_percentage(*argv)) {
224 tileRenderer->setTileHeightPercentage(atof(*argv));
225 if (!(tileRenderer->getTileHeightPercentage() > 0)) {
226 SkDELETE(tileRenderer);
228 "--mode %s must be given a height percentage > 0\n", mode);
232 tileRenderer->setTileHeight(atoi(*argv));
233 if (!(tileRenderer->getTileHeight() > 0)) {
234 SkDELETE(tileRenderer);
235 SkDebugf("--mode %s must be given a height > 0\n", mode);
240 renderer = tileRenderer;
242 SkDebugf("%s is not a valid mode for --mode\n", *argv);
246 } else if (0 == strcmp(*argv, "--device")) {
249 SkDebugf("Missing mode for --deivce\n");
254 if (0 == strcmp(*argv, "bitmap")) {
255 deviceType = sk_tools::PictureRenderer::kBitmap_DeviceType;
258 else if (0 == strcmp(*argv, "gpu")) {
259 deviceType = sk_tools::PictureRenderer::kGPU_DeviceType;
263 SkDebugf("%s is not a valid mode for --device\n", *argv);
268 } else if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) {
273 inputs->push_back(SkString(*argv));
277 if (inputs->count() < 2) {
283 if (NULL == renderer) {
284 renderer = SkNEW(sk_tools::SimplePictureRenderer);
287 renderer->setDeviceType(deviceType);
290 int tool_main(int argc, char** argv);
291 int tool_main(int argc, char** argv) {
293 SkTArray<SkString> inputs;
294 sk_tools::PictureRenderer* renderer = NULL;
296 parse_commandline(argc, argv, &inputs, renderer);
297 SkString outputDir = inputs[inputs.count() - 1];
301 for (int i = 0; i < inputs.count() - 1; i ++) {
302 failures += process_input(inputs[i], outputDir, *renderer);
305 SkDebugf("Failed to render %i pictures.\n", failures);
310 if (renderer->isUsingGpuDevice()) {
311 GrContext* ctx = renderer->getGrContext();
313 ctx->printCacheStats();
321 #if !defined SK_BUILD_FOR_IOS
322 int main(int argc, char * const argv[]) {
323 return tool_main(argc, (char**) argv);