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