junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 8 | #include "CopyTilesRenderer.h" |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 9 | #include "SkBitmap.h" |
| 10 | #include "SkCanvas.h" |
keyar@chromium.org | 472b379 | 2012-07-20 22:34:27 +0000 | [diff] [blame] | 11 | #include "SkDevice.h" |
borenet@google.com | 10ef79e | 2012-09-10 17:19:06 +0000 | [diff] [blame] | 12 | #include "SkGraphics.h" |
scroggo@google.com | 5a7c6be | 2012-10-04 21:46:08 +0000 | [diff] [blame] | 13 | #include "SkImageDecoder.h" |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 14 | #include "SkMath.h" |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 15 | #include "SkOSFile.h" |
| 16 | #include "SkPicture.h" |
| 17 | #include "SkStream.h" |
| 18 | #include "SkString.h" |
senorblanco@chromium.org | 3cbbb54 | 2012-07-13 18:55:53 +0000 | [diff] [blame] | 19 | #include "SkTArray.h" |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 20 | #include "PictureRenderer.h" |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 21 | #include "picture_utils.h" |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 22 | |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 23 | static void usage(const char* argv0) { |
| 24 | SkDebugf("SkPicture rendering tool\n"); |
| 25 | SkDebugf("\n" |
| 26 | "Usage: \n" |
borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 27 | " %s <input>... \n" |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 28 | " [-w <outputDir>]\n" |
| 29 | " [--mode pow2tile minWidth height | copyTile width height | simple\n" |
| 30 | " | tile width height]\n" |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 31 | " [--pipe]\n" |
| 32 | " [--multi count]\n" |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame^] | 33 | " [--viewport width height]\n" |
keyar@chromium.org | c81686c | 2012-08-20 15:04:04 +0000 | [diff] [blame] | 34 | " [--device bitmap" |
| 35 | #if SK_SUPPORT_GPU |
| 36 | " | gpu" |
| 37 | #endif |
| 38 | "]" |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 39 | , argv0); |
keyar@chromium.org | 472b379 | 2012-07-20 22:34:27 +0000 | [diff] [blame] | 40 | SkDebugf("\n\n"); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 41 | SkDebugf( |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 42 | " input: A list of directories and files to use as input. Files are\n" |
| 43 | " expected to have the .skp extension.\n\n"); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 44 | SkDebugf( |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 45 | " outputDir: directory to write the rendered images.\n\n"); |
keyar@chromium.org | 472b379 | 2012-07-20 22:34:27 +0000 | [diff] [blame] | 46 | SkDebugf( |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 47 | " --mode pow2tile minWidth height | copyTile width height | simple\n" |
| 48 | " | tile width height | rerecord: Run in the corresponding mode.\n" |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 49 | " Default is simple.\n"); |
keyar@chromium.org | 472b379 | 2012-07-20 22:34:27 +0000 | [diff] [blame] | 50 | SkDebugf( |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 51 | " pow2tile minWidth height, Creates tiles with widths\n" |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 52 | " that are all a power of two\n" |
| 53 | " such that they minimize the\n" |
| 54 | " amount of wasted tile space.\n" |
| 55 | " minWidth is the minimum width\n" |
| 56 | " of these tiles and must be a\n" |
| 57 | " power of two. A simple render\n" |
| 58 | " is done with these tiles.\n"); |
| 59 | SkDebugf( |
scroggo@google.com | a9e3a36 | 2012-11-07 17:52:48 +0000 | [diff] [blame] | 60 | " simple, Render using the default rendering method.\n" |
| 61 | " rerecord, Record the picture as a new skp, with the bitmaps PNG encoded.\n" |
| 62 | ); |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 63 | SkDebugf( |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 64 | " tile width height, Do a simple render using tiles\n" |
| 65 | " with the given dimensions.\n" |
| 66 | " copyTile width height, Draw the picture, then copy it into tiles.\n" |
| 67 | " Does not support percentages.\n" |
| 68 | " If the picture is large enough, breaks it into\n" |
| 69 | " larger tiles (and draws the picture once per\n" |
| 70 | " larger tile) to avoid creating a large canvas.\n" |
| 71 | " Add --tiles x y to specify the number of tiles\n" |
| 72 | " per larger tile in the x and y direction.\n" |
| 73 | ); |
keyar@chromium.org | c81686c | 2012-08-20 15:04:04 +0000 | [diff] [blame] | 74 | SkDebugf("\n"); |
| 75 | SkDebugf( |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 76 | " --multi count : Set the number of threads for multi threaded drawing. Must be greater\n" |
| 77 | " than 1. Only works with tiled rendering.\n" |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame^] | 78 | " --viewport width height : Set the viewport.\n" |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 79 | " --pipe: Benchmark SkGPipe rendering. Currently incompatible with \"mode\".\n"); |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 80 | SkDebugf( |
keyar@chromium.org | c81686c | 2012-08-20 15:04:04 +0000 | [diff] [blame] | 81 | " --device bitmap" |
| 82 | #if SK_SUPPORT_GPU |
| 83 | " | gpu" |
| 84 | #endif |
| 85 | ": Use the corresponding device. Default is bitmap.\n"); |
| 86 | SkDebugf( |
| 87 | " bitmap, Render to a bitmap.\n"); |
keyar@chromium.org | c81686c | 2012-08-20 15:04:04 +0000 | [diff] [blame] | 88 | #if SK_SUPPORT_GPU |
keyar@chromium.org | a40c20d | 2012-08-20 15:04:12 +0000 | [diff] [blame] | 89 | SkDebugf( |
keyar@chromium.org | c81686c | 2012-08-20 15:04:04 +0000 | [diff] [blame] | 90 | " gpu, Render to the GPU.\n"); |
| 91 | #endif |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 92 | } |
| 93 | |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 94 | static void make_output_filepath(SkString* path, const SkString& dir, |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 95 | const SkString& name) { |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 96 | sk_tools::make_filepath(path, dir, name); |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 97 | // Remove ".skp" |
| 98 | path->remove(path->size() - 4, 4); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 99 | } |
| 100 | |
borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 101 | static bool render_picture(const SkString& inputPath, const SkString* outputDir, |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 102 | sk_tools::PictureRenderer& renderer) { |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 103 | SkString inputFilename; |
| 104 | sk_tools::get_basename(&inputFilename, inputPath); |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 105 | |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 106 | SkFILEStream inputStream; |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 107 | inputStream.setPath(inputPath.c_str()); |
| 108 | if (!inputStream.isValid()) { |
| 109 | SkDebugf("Could not open file %s\n", inputPath.c_str()); |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 110 | return false; |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 111 | } |
| 112 | |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 113 | bool success = false; |
scroggo@google.com | 5a7c6be | 2012-10-04 21:46:08 +0000 | [diff] [blame] | 114 | SkPicture picture(&inputStream, &success, &SkImageDecoder::DecodeStream); |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 115 | if (!success) { |
| 116 | SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str()); |
| 117 | return false; |
| 118 | } |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 119 | |
borenet@google.com | 2d2b9a0 | 2012-09-20 18:54:04 +0000 | [diff] [blame] | 120 | SkDebugf("drawing... [%i %i] %s\n", picture.width(), picture.height(), |
borenet@google.com | 03fcee8 | 2012-09-10 18:18:38 +0000 | [diff] [blame] | 121 | inputPath.c_str()); |
skia.committer@gmail.com | 1d225f2 | 2012-09-14 02:01:10 +0000 | [diff] [blame] | 122 | |
borenet@google.com | 2d2b9a0 | 2012-09-20 18:54:04 +0000 | [diff] [blame] | 123 | renderer.init(&picture); |
scroggo@google.com | b4773b4 | 2012-10-01 20:06:09 +0000 | [diff] [blame] | 124 | renderer.setup(); |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 125 | |
borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 126 | SkString* outputPath = NULL; |
| 127 | if (NULL != outputDir) { |
| 128 | outputPath = SkNEW(SkString); |
| 129 | make_output_filepath(outputPath, *outputDir, inputFilename); |
| 130 | } |
| 131 | success = renderer.render(outputPath); |
| 132 | if (outputPath) { |
| 133 | if (!success) { |
| 134 | SkDebugf("Could not write to file %s\n", outputPath->c_str()); |
| 135 | } |
| 136 | SkDELETE(outputPath); |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 137 | } |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 138 | |
| 139 | renderer.resetState(); |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 140 | |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 141 | renderer.end(); |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 142 | return success; |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 143 | } |
| 144 | |
borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 145 | static int process_input(const SkString& input, const SkString* outputDir, |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 146 | sk_tools::PictureRenderer& renderer) { |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 147 | SkOSFile::Iter iter(input.c_str(), "skp"); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 148 | SkString inputFilename; |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 149 | int failures = 0; |
borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 150 | SkDebugf("process_input, %s\n", input.c_str()); |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 151 | if (iter.next(&inputFilename)) { |
| 152 | do { |
| 153 | SkString inputPath; |
| 154 | sk_tools::make_filepath(&inputPath, input, inputFilename); |
borenet@google.com | 57837bf | 2012-09-19 17:28:29 +0000 | [diff] [blame] | 155 | if (!render_picture(inputPath, outputDir, renderer)) { |
| 156 | ++failures; |
| 157 | } |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 158 | } while(iter.next(&inputFilename)); |
borenet@google.com | 57837bf | 2012-09-19 17:28:29 +0000 | [diff] [blame] | 159 | } else if (SkStrEndsWith(input.c_str(), ".skp")) { |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 160 | SkString inputPath(input); |
borenet@google.com | 57837bf | 2012-09-19 17:28:29 +0000 | [diff] [blame] | 161 | if (!render_picture(inputPath, outputDir, renderer)) { |
| 162 | ++failures; |
| 163 | } |
| 164 | } else { |
| 165 | SkString warning; |
| 166 | warning.printf("Warning: skipping %s\n", input.c_str()); |
| 167 | SkDebugf(warning.c_str()); |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 168 | } |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 169 | return failures; |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static void parse_commandline(int argc, char* const argv[], SkTArray<SkString>* inputs, |
borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 173 | sk_tools::PictureRenderer*& renderer, SkString*& outputDir){ |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 174 | const char* argv0 = argv[0]; |
| 175 | char* const* stop = argv + argc; |
| 176 | |
keyar@chromium.org | c81686c | 2012-08-20 15:04:04 +0000 | [diff] [blame] | 177 | sk_tools::PictureRenderer::SkDeviceTypes deviceType = |
| 178 | sk_tools::PictureRenderer::kBitmap_DeviceType; |
| 179 | |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 180 | bool usePipe = false; |
| 181 | int numThreads = 1; |
| 182 | bool useTiles = false; |
| 183 | const char* widthString = NULL; |
| 184 | const char* heightString = NULL; |
| 185 | bool isPowerOf2Mode = false; |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 186 | bool isCopyMode = false; |
| 187 | const char* xTilesString = NULL; |
| 188 | const char* yTilesString = NULL; |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 189 | const char* mode = NULL; |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame^] | 190 | SkISize viewport; |
| 191 | viewport.setEmpty(); |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 192 | for (++argv; argv < stop; ++argv) { |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 193 | if (0 == strcmp(*argv, "--mode")) { |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 194 | if (renderer != NULL) { |
| 195 | renderer->unref(); |
| 196 | SkDebugf("Cannot combine modes.\n"); |
| 197 | usage(argv0); |
| 198 | exit(-1); |
| 199 | } |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 200 | |
keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 201 | ++argv; |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 202 | if (argv >= stop) { |
| 203 | SkDebugf("Missing mode for --mode\n"); |
| 204 | usage(argv0); |
| 205 | exit(-1); |
| 206 | } |
| 207 | |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 208 | if (0 == strcmp(*argv, "simple")) { |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 209 | renderer = SkNEW(sk_tools::SimplePictureRenderer); |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 210 | } else if ((0 == strcmp(*argv, "tile")) || (0 == strcmp(*argv, "pow2tile")) |
| 211 | || 0 == strcmp(*argv, "copyTile")) { |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 212 | useTiles = true; |
| 213 | mode = *argv; |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 214 | |
| 215 | if (0 == strcmp(*argv, "pow2tile")) { |
| 216 | isPowerOf2Mode = true; |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 217 | } else if (0 == strcmp(*argv, "copyTile")) { |
| 218 | isCopyMode = true; |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 219 | } |
| 220 | |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 221 | ++argv; |
| 222 | if (argv >= stop) { |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 223 | SkDebugf("Missing width for --mode %s\n", mode); |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 224 | usage(argv0); |
| 225 | exit(-1); |
| 226 | } |
| 227 | |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 228 | widthString = *argv; |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 229 | ++argv; |
| 230 | if (argv >= stop) { |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 231 | SkDebugf("Missing height for --mode %s\n", mode); |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 232 | usage(argv0); |
| 233 | exit(-1); |
| 234 | } |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 235 | heightString = *argv; |
scroggo@google.com | a9e3a36 | 2012-11-07 17:52:48 +0000 | [diff] [blame] | 236 | } else if (0 == strcmp(*argv, "rerecord")) { |
| 237 | renderer = SkNEW(sk_tools::RecordPictureRenderer); |
keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 238 | } else { |
keyar@chromium.org | 795cd47 | 2012-08-02 18:57:53 +0000 | [diff] [blame] | 239 | SkDebugf("%s is not a valid mode for --mode\n", *argv); |
keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 240 | usage(argv0); |
| 241 | exit(-1); |
| 242 | } |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame^] | 243 | } else if (0 == strcmp(*argv, "--viewport")) { |
| 244 | ++argv; |
| 245 | if (argv >= stop) { |
| 246 | SkDebugf("Missing width for --viewport\n"); |
| 247 | usage(argv0); |
| 248 | exit(-1); |
| 249 | } |
| 250 | viewport.fWidth = atoi(*argv); |
| 251 | ++argv; |
| 252 | if (argv >= stop) { |
| 253 | SkDebugf("Missing height for --viewport\n"); |
| 254 | usage(argv0); |
| 255 | exit(-1); |
| 256 | } |
| 257 | viewport.fHeight = atoi(*argv); |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 258 | } else if (0 == strcmp(*argv, "--tiles")) { |
| 259 | ++argv; |
| 260 | if (argv >= stop) { |
| 261 | SkDebugf("Missing x for --tiles\n"); |
| 262 | usage(argv0); |
| 263 | exit(-1); |
| 264 | } |
| 265 | xTilesString = *argv; |
| 266 | ++argv; |
| 267 | if (argv >= stop) { |
| 268 | SkDebugf("Missing y for --tiles\n"); |
| 269 | usage(argv0); |
| 270 | exit(-1); |
| 271 | } |
| 272 | yTilesString = *argv; |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 273 | } else if (0 == strcmp(*argv, "--pipe")) { |
| 274 | usePipe = true; |
| 275 | } else if (0 == strcmp(*argv, "--multi")) { |
| 276 | ++argv; |
| 277 | if (argv >= stop) { |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 278 | SkSafeUnref(renderer); |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 279 | SkDebugf("Missing arg for --multi\n"); |
| 280 | usage(argv0); |
| 281 | exit(-1); |
| 282 | } |
| 283 | numThreads = atoi(*argv); |
| 284 | if (numThreads < 2) { |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 285 | SkSafeUnref(renderer); |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 286 | SkDebugf("Number of threads must be at least 2.\n"); |
| 287 | usage(argv0); |
| 288 | exit(-1); |
| 289 | } |
keyar@chromium.org | c81686c | 2012-08-20 15:04:04 +0000 | [diff] [blame] | 290 | } else if (0 == strcmp(*argv, "--device")) { |
| 291 | ++argv; |
| 292 | if (argv >= stop) { |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 293 | SkSafeUnref(renderer); |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 294 | SkDebugf("Missing mode for --device\n"); |
keyar@chromium.org | c81686c | 2012-08-20 15:04:04 +0000 | [diff] [blame] | 295 | usage(argv0); |
| 296 | exit(-1); |
| 297 | } |
| 298 | |
| 299 | if (0 == strcmp(*argv, "bitmap")) { |
| 300 | deviceType = sk_tools::PictureRenderer::kBitmap_DeviceType; |
| 301 | } |
| 302 | #if SK_SUPPORT_GPU |
| 303 | else if (0 == strcmp(*argv, "gpu")) { |
| 304 | deviceType = sk_tools::PictureRenderer::kGPU_DeviceType; |
| 305 | } |
| 306 | #endif |
| 307 | else { |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 308 | SkSafeUnref(renderer); |
keyar@chromium.org | c81686c | 2012-08-20 15:04:04 +0000 | [diff] [blame] | 309 | SkDebugf("%s is not a valid mode for --device\n", *argv); |
| 310 | usage(argv0); |
| 311 | exit(-1); |
| 312 | } |
| 313 | |
keyar@chromium.org | 472b379 | 2012-07-20 22:34:27 +0000 | [diff] [blame] | 314 | } else if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) { |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 315 | SkSafeUnref(renderer); |
keyar@chromium.org | 472b379 | 2012-07-20 22:34:27 +0000 | [diff] [blame] | 316 | usage(argv0); |
| 317 | exit(-1); |
borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 318 | } else if (0 == strcmp(*argv, "-w")) { |
| 319 | ++argv; |
| 320 | if (argv >= stop) { |
| 321 | SkDebugf("Missing output directory for -w\n"); |
| 322 | usage(argv0); |
| 323 | exit(-1); |
| 324 | } |
| 325 | outputDir = SkNEW_ARGS(SkString, (*argv)); |
keyar@chromium.org | a2333d9 | 2012-07-16 17:29:16 +0000 | [diff] [blame] | 326 | } else { |
| 327 | inputs->push_back(SkString(*argv)); |
| 328 | } |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 329 | } |
keyar@chromium.org | 472b379 | 2012-07-20 22:34:27 +0000 | [diff] [blame] | 330 | |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 331 | if (numThreads > 1 && !useTiles) { |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 332 | SkSafeUnref(renderer); |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 333 | SkDebugf("Multithreaded drawing requires tiled rendering.\n"); |
| 334 | usage(argv0); |
| 335 | exit(-1); |
| 336 | } |
| 337 | |
| 338 | if (useTiles) { |
| 339 | SkASSERT(NULL == renderer); |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 340 | sk_tools::TiledPictureRenderer* tiledRenderer; |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 341 | if (isCopyMode) { |
| 342 | int x, y; |
| 343 | if (xTilesString != NULL) { |
| 344 | SkASSERT(yTilesString != NULL); |
| 345 | x = atoi(xTilesString); |
| 346 | y = atoi(yTilesString); |
| 347 | if (x <= 0 || y <= 0) { |
| 348 | SkDebugf("--tiles must be given values > 0\n"); |
| 349 | usage(argv0); |
| 350 | exit(-1); |
| 351 | } |
| 352 | } else { |
| 353 | x = y = 4; |
| 354 | } |
| 355 | tiledRenderer = SkNEW_ARGS(sk_tools::CopyTilesRenderer, (x, y)); |
| 356 | } else if (numThreads > 1) { |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 357 | tiledRenderer = SkNEW_ARGS(sk_tools::MultiCorePictureRenderer, (numThreads)); |
| 358 | } else { |
| 359 | tiledRenderer = SkNEW(sk_tools::TiledPictureRenderer); |
| 360 | } |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 361 | if (isPowerOf2Mode) { |
| 362 | int minWidth = atoi(widthString); |
| 363 | if (!SkIsPow2(minWidth) || minWidth < 0) { |
| 364 | tiledRenderer->unref(); |
| 365 | SkString err; |
| 366 | err.printf("-mode %s must be given a width" |
| 367 | " value that is a power of two\n", mode); |
| 368 | SkDebugf(err.c_str()); |
| 369 | usage(argv0); |
| 370 | exit(-1); |
| 371 | } |
| 372 | tiledRenderer->setTileMinPowerOf2Width(minWidth); |
| 373 | } else if (sk_tools::is_percentage(widthString)) { |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 374 | if (isCopyMode) { |
| 375 | tiledRenderer->unref(); |
| 376 | SkString err; |
| 377 | err.printf("--mode %s does not support percentages.\n", mode); |
| 378 | SkDebugf(err.c_str()); |
| 379 | usage(argv0); |
| 380 | exit(-1); |
| 381 | } |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 382 | tiledRenderer->setTileWidthPercentage(atof(widthString)); |
| 383 | if (!(tiledRenderer->getTileWidthPercentage() > 0)) { |
| 384 | tiledRenderer->unref(); |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 385 | SkDebugf("--mode %s must be given a width percentage > 0\n", mode); |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 386 | usage(argv0); |
| 387 | exit(-1); |
| 388 | } |
| 389 | } else { |
| 390 | tiledRenderer->setTileWidth(atoi(widthString)); |
| 391 | if (!(tiledRenderer->getTileWidth() > 0)) { |
| 392 | tiledRenderer->unref(); |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 393 | SkDebugf("--mode %s must be given a width > 0\n", mode); |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 394 | usage(argv0); |
| 395 | exit(-1); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | if (sk_tools::is_percentage(heightString)) { |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 400 | if (isCopyMode) { |
| 401 | tiledRenderer->unref(); |
| 402 | SkString err; |
| 403 | err.printf("--mode %s does not support percentages.\n", mode); |
| 404 | SkDebugf(err.c_str()); |
| 405 | usage(argv0); |
| 406 | exit(-1); |
| 407 | } |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 408 | tiledRenderer->setTileHeightPercentage(atof(heightString)); |
| 409 | if (!(tiledRenderer->getTileHeightPercentage() > 0)) { |
| 410 | tiledRenderer->unref(); |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 411 | SkDebugf("--mode %s must be given a height percentage > 0\n", mode); |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 412 | usage(argv0); |
| 413 | exit(-1); |
| 414 | } |
| 415 | } else { |
| 416 | tiledRenderer->setTileHeight(atoi(heightString)); |
| 417 | if (!(tiledRenderer->getTileHeight() > 0)) { |
| 418 | tiledRenderer->unref(); |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 419 | SkDebugf("--mode %s must be given a height > 0\n", mode); |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 420 | usage(argv0); |
| 421 | exit(-1); |
| 422 | } |
| 423 | } |
| 424 | if (numThreads > 1) { |
| 425 | #if SK_SUPPORT_GPU |
| 426 | if (sk_tools::PictureRenderer::kGPU_DeviceType == deviceType) { |
| 427 | tiledRenderer->unref(); |
| 428 | SkDebugf("GPU not compatible with multithreaded tiling.\n"); |
| 429 | usage(argv0); |
| 430 | exit(-1); |
| 431 | } |
| 432 | #endif |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 433 | } |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 434 | renderer = tiledRenderer; |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 435 | if (usePipe) { |
| 436 | SkDebugf("Pipe rendering is currently not compatible with tiling.\n" |
| 437 | "Turning off pipe.\n"); |
| 438 | } |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 439 | } else if (usePipe) { |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 440 | if (renderer != NULL) { |
| 441 | renderer->unref(); |
| 442 | SkDebugf("Pipe is incompatible with other modes.\n"); |
| 443 | usage(argv0); |
| 444 | exit(-1); |
| 445 | } |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 446 | renderer = SkNEW(sk_tools::PipePictureRenderer); |
| 447 | } |
| 448 | |
borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 449 | if (inputs->empty()) { |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 450 | SkSafeUnref(renderer); |
borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 451 | if (NULL != outputDir) { |
| 452 | SkDELETE(outputDir); |
| 453 | } |
keyar@chromium.org | 472b379 | 2012-07-20 22:34:27 +0000 | [diff] [blame] | 454 | usage(argv0); |
| 455 | exit(-1); |
| 456 | } |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 457 | |
| 458 | if (NULL == renderer) { |
| 459 | renderer = SkNEW(sk_tools::SimplePictureRenderer); |
| 460 | } |
keyar@chromium.org | 4ea96c5 | 2012-08-20 15:03:29 +0000 | [diff] [blame] | 461 | |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame^] | 462 | renderer->setViewport(viewport); |
keyar@chromium.org | c81686c | 2012-08-20 15:04:04 +0000 | [diff] [blame] | 463 | renderer->setDeviceType(deviceType); |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 464 | } |
| 465 | |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 466 | int tool_main(int argc, char** argv); |
| 467 | int tool_main(int argc, char** argv) { |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 468 | SkAutoGraphics ag; |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 469 | SkTArray<SkString> inputs; |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 470 | sk_tools::PictureRenderer* renderer = NULL; |
borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 471 | SkString* outputDir = NULL; |
| 472 | parse_commandline(argc, argv, &inputs, renderer, outputDir); |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 473 | SkASSERT(renderer); |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 474 | |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 475 | int failures = 0; |
borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 476 | for (int i = 0; i < inputs.count(); i ++) { |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 477 | failures += process_input(inputs[i], outputDir, *renderer); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 478 | } |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 479 | if (failures != 0) { |
| 480 | SkDebugf("Failed to render %i pictures.\n", failures); |
| 481 | return 1; |
| 482 | } |
robertphillips@google.com | 163c84b | 2012-09-13 15:40:37 +0000 | [diff] [blame] | 483 | #if SK_SUPPORT_GPU |
| 484 | #if GR_CACHE_STATS |
| 485 | if (renderer->isUsingGpuDevice()) { |
| 486 | GrContext* ctx = renderer->getGrContext(); |
| 487 | |
| 488 | ctx->printCacheStats(); |
| 489 | } |
| 490 | #endif |
| 491 | #endif |
borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 492 | if (NULL != outputDir) { |
| 493 | SkDELETE(outputDir); |
| 494 | } |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 495 | SkDELETE(renderer); |
caryclark@google.com | 868e1f6 | 2012-10-02 20:00:03 +0000 | [diff] [blame] | 496 | return 0; |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 497 | } |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 498 | |
| 499 | #if !defined SK_BUILD_FOR_IOS |
| 500 | int main(int argc, char * const argv[]) { |
| 501 | return tool_main(argc, (char**) argv); |
| 502 | } |
| 503 | #endif |