blob: c5226534d58ca380fbe0089064a37a084673735a [file] [log] [blame]
scroggo@google.com161e1ba2013-03-04 16:41:06 +00001/*
2 * Copyright 2013 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
8#include "PictureRenderingFlags.h"
9
10#include "CopyTilesRenderer.h"
11#include "PictureRenderer.h"
12#include "picture_utils.h"
scroggo@google.comd9ba9a02013-03-21 19:43:15 +000013#include "SkCommandLineFlags.h"
scroggo@google.combb281f72013-03-18 21:37:39 +000014#include "SkData.h"
scroggo@google.combb281f72013-03-18 21:37:39 +000015#include "SkImage.h"
16#include "SkImageDecoder.h"
scroggo@google.combb281f72013-03-18 21:37:39 +000017#include "SkString.h"
scroggo@google.com161e1ba2013-03-04 16:41:06 +000018
19// Alphabetized list of flags used by this file or bench_ and render_pictures.
20DEFINE_string(bbh, "none", "bbhType [width height]: Set the bounding box hierarchy type to "
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +000021 "be used. Accepted values are: none, rtree, quadtree, grid. "
scroggo@google.com161e1ba2013-03-04 16:41:06 +000022 "Not compatible with --pipe. With value "
23 "'grid', width and height must be specified. 'grid' can "
24 "only be used with modes tile, record, and "
25 "playbackCreation.");
robertphillips@google.com11e10e32014-01-06 19:09:29 +000026
27
28#if SK_SUPPORT_GPU
29#define GPU_CONFIG_STRING "|gpu|msaa4|msaa16"
30#else
31#define GPU_CONFIG_STRING ""
32#endif
33#if SK_ANGLE
34#define ANGLE_CONFIG_STRING "|angle"
35#else
36#define ANGLE_CONFIG_STRING ""
37#endif
38#if SK_MESA
39#define MESA_CONFIG_STRING "|mesa"
40#else
41#define MESA_CONFIG_STRING ""
42#endif
43
scroggo@google.com161e1ba2013-03-04 16:41:06 +000044// Although this config does not support all the same options as gm, the names should be kept
45// consistent.
rmistry@google.com6ab96732014-01-06 18:37:24 +000046DEFINE_string(config, "8888", "["
robertphillips@google.com11e10e32014-01-06 19:09:29 +000047 "8888" GPU_CONFIG_STRING ANGLE_CONFIG_STRING MESA_CONFIG_STRING
rmistry@google.com6ab96732014-01-06 18:37:24 +000048 "]: Use the corresponding config.");
scroggo@google.com161e1ba2013-03-04 16:41:06 +000049
50DEFINE_bool(deferImageDecoding, false, "Defer decoding until drawing images. "
51 "Has no effect if the provided skp does not have its images encoded.");
52DEFINE_string(mode, "simple", "Run in the corresponding mode:\n"
53 "simple: Simple rendering.\n"
54 "tile width height: Use tiles with the given dimensions or percentages.\n"
55 "pow2tile minWidth height: Use tiles with widths that are all a power\n"
56 "\tof two such that they minimize the amount of wasted tile space.\n"
57 "\tminWidth must be a power of two.\n"
58 "copyTile width height: Draw the picture, then copy into tiles. If the\n"
59 "\tpicture is large enough, it is broken into larger tiles to avoid\n"
60 "\tcreating a large canvas.\n"
61// TODO: If bench_pictures and render_pictures were two separate targets, we could use build flags
62// to determine which modes to display.
63 "record: (Only in bench_pictures) Time recording from a picture to a new\n"
64 "\tpicture.\n"
65 "playbackCreation: (Only in bench_pictures) Time creation of the \n"
66 "\tSkPicturePlayback.\n"
67 "rerecord: (Only in render_pictures) Record the picture as a new skp,\n"
68 "\twith the bitmaps PNG encoded.\n");
69DEFINE_int32(multi, 1, "Set the number of threads for multi threaded drawing. "
70 "If > 1, requires tiled rendering.");
71DEFINE_bool(pipe, false, "Use SkGPipe rendering. Currently incompatible with \"mode\".");
scroggo@google.com604e0c22013-04-09 21:25:46 +000072DEFINE_string2(readPath, r, "", "skp files or directories of skp files to process.");
scroggo@google.com161e1ba2013-03-04 16:41:06 +000073DEFINE_double(scale, 1, "Set the scale factor.");
74DEFINE_string(tiles, "", "Used with --mode copyTile to specify number of tiles per larger tile "
75 "in the x and y directions.");
76DEFINE_string(viewport, "", "width height: Set the viewport.");
77
78sk_tools::PictureRenderer* parseRenderer(SkString& error, PictureTool tool) {
79 error.reset();
80
81 if (FLAGS_multi <= 0) {
82 error.printf("--multi must be > 0, was %i", FLAGS_multi);
83 return NULL;
84 }
85
86 bool useTiles = false;
87 const char* widthString = NULL;
88 const char* heightString = NULL;
89 bool isPowerOf2Mode = false;
90 bool isCopyMode = false;
91 const char* mode = NULL;
92 bool gridSupported = false;
93
94 SkAutoTUnref<sk_tools::PictureRenderer> renderer;
95 if (FLAGS_mode.count() >= 1) {
96 mode = FLAGS_mode[0];
97 if (0 == strcmp(mode, "record")) {
98 renderer.reset(SkNEW(sk_tools::RecordPictureRenderer));
99 gridSupported = true;
100 // undocumented
101 } else if (0 == strcmp(mode, "clone")) {
102 renderer.reset(sk_tools::CreatePictureCloneRenderer());
103 } else if (0 == strcmp(mode, "tile") || 0 == strcmp(mode, "pow2tile")
104 || 0 == strcmp(mode, "copyTile")) {
105 useTiles = true;
106
107 if (0 == strcmp(mode, "pow2tile")) {
108 isPowerOf2Mode = true;
109 } else if (0 == strcmp(mode, "copyTile")) {
110 isCopyMode = true;
111 } else {
112 gridSupported = true;
113 }
114
115 if (FLAGS_mode.count() < 2) {
116 error.printf("Missing width for --mode %s\n", mode);
117 return NULL;
118 }
119
120 widthString = FLAGS_mode[1];
121 if (FLAGS_mode.count() < 3) {
122 error.printf("Missing height for --mode %s\n", mode);
123 return NULL;
124 }
125
126 heightString = FLAGS_mode[2];
127 } else if (0 == strcmp(mode, "playbackCreation") && kBench_PictureTool == tool) {
128 renderer.reset(SkNEW(sk_tools::PlaybackCreationRenderer));
129 gridSupported = true;
130 // undocumented
131 } else if (0 == strcmp(mode, "gatherPixelRefs") && kBench_PictureTool == tool) {
132 renderer.reset(sk_tools::CreateGatherPixelRefsRenderer());
133 } else if (0 == strcmp(mode, "rerecord") && kRender_PictureTool == tool) {
134 renderer.reset(SkNEW(sk_tools::RecordPictureRenderer));
135 // Allow 'mode' to be set to 'simple', but do not create a renderer, so we can
136 // ensure that pipe does not override a mode besides simple. The renderer will
137 // be created below.
138 } else if (0 != strcmp(mode, "simple")) {
139 error.printf("%s is not a valid mode for --mode\n", mode);
140 return NULL;
141 }
142 }
143
144 if (useTiles) {
145 SkASSERT(NULL == renderer);
146 SkAutoTUnref<sk_tools::TiledPictureRenderer> tiledRenderer;
147 if (isCopyMode) {
148 int xTiles = -1;
149 int yTiles = -1;
150 if (FLAGS_tiles.count() > 0) {
151 if (FLAGS_tiles.count() != 2) {
152 error.printf("--tiles requires an x value and a y value.\n");
153 return NULL;
154 }
155 xTiles = atoi(FLAGS_tiles[0]);
156 yTiles = atoi(FLAGS_tiles[1]);
157 }
158
159 int x, y;
160 if (xTiles != -1 && yTiles != -1) {
161 x = xTiles;
162 y = yTiles;
163 if (x <= 0 || y <= 0) {
164 error.printf("--tiles must be given values > 0\n");
165 return NULL;
166 }
167 } else {
168 x = y = 4;
169 }
170 tiledRenderer.reset(SkNEW_ARGS(sk_tools::CopyTilesRenderer, (x, y)));
171 } else if (FLAGS_multi > 1) {
172 tiledRenderer.reset(SkNEW_ARGS(sk_tools::MultiCorePictureRenderer,
173 (FLAGS_multi)));
174 } else {
175 tiledRenderer.reset(SkNEW(sk_tools::TiledPictureRenderer));
176 }
177
178 if (isPowerOf2Mode) {
179 int minWidth = atoi(widthString);
180 if (!SkIsPow2(minWidth) || minWidth < 0) {
181 SkString err;
182 error.printf("-mode %s must be given a width"
183 " value that is a power of two\n", mode);
184 return NULL;
185 }
186 tiledRenderer->setTileMinPowerOf2Width(minWidth);
187 } else if (sk_tools::is_percentage(widthString)) {
188 if (isCopyMode) {
189 error.printf("--mode %s does not support percentages.\n", mode);
190 return NULL;
191 }
192 tiledRenderer->setTileWidthPercentage(atof(widthString));
193 if (!(tiledRenderer->getTileWidthPercentage() > 0)) {
194 error.printf("--mode %s must be given a width percentage > 0\n", mode);
195 return NULL;
196 }
197 } else {
198 tiledRenderer->setTileWidth(atoi(widthString));
199 if (!(tiledRenderer->getTileWidth() > 0)) {
200 error.printf("--mode %s must be given a width > 0\n", mode);
201 return NULL;
202 }
203 }
204
205 if (sk_tools::is_percentage(heightString)) {
206 if (isCopyMode) {
207 error.printf("--mode %s does not support percentages.\n", mode);
208 return NULL;
209 }
210 tiledRenderer->setTileHeightPercentage(atof(heightString));
211 if (!(tiledRenderer->getTileHeightPercentage() > 0)) {
212 error.printf("--mode %s must be given a height percentage > 0\n", mode);
213 return NULL;
214 }
215 } else {
216 tiledRenderer->setTileHeight(atoi(heightString));
217 if (!(tiledRenderer->getTileHeight() > 0)) {
218 SkString err;
219 error.printf("--mode %s must be given a height > 0\n", mode);
220 return NULL;
221 }
222 }
223
224 renderer.reset(tiledRenderer.detach());
225 if (FLAGS_pipe) {
226 error.printf("Pipe rendering is currently not compatible with tiling.\n"
227 "Turning off pipe.\n");
228 }
229
230 } else { // useTiles
231 if (FLAGS_multi > 1) {
232 error.printf("Multithreaded drawing requires tiled rendering.\n");
233 return NULL;
234 }
235 if (FLAGS_pipe) {
236 if (renderer != NULL) {
237 error.printf("Pipe is incompatible with other modes.\n");
238 return NULL;
239 }
240 renderer.reset(SkNEW(sk_tools::PipePictureRenderer));
241 }
242 }
243
244 if (NULL == renderer) {
245 renderer.reset(SkNEW(sk_tools::SimplePictureRenderer));
246 }
247
248 if (FLAGS_viewport.count() > 0) {
249 if (FLAGS_viewport.count() != 2) {
250 error.printf("--viewport requires a width and a height.\n");
251 return NULL;
252 }
253 SkISize viewport;
254 viewport.fWidth = atoi(FLAGS_viewport[0]);
255 viewport.fHeight = atoi(FLAGS_viewport[1]);
256 renderer->setViewport(viewport);
257 }
258
259 sk_tools::PictureRenderer::SkDeviceTypes deviceType =
260 sk_tools::PictureRenderer::kBitmap_DeviceType;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000261#if SK_SUPPORT_GPU
262 int sampleCount = 0;
263#endif
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000264 if (FLAGS_config.count() > 0) {
265 if (0 == strcmp(FLAGS_config[0], "8888")) {
266 deviceType = sk_tools::PictureRenderer::kBitmap_DeviceType;
267 }
268#if SK_SUPPORT_GPU
269 else if (0 == strcmp(FLAGS_config[0], "gpu")) {
270 deviceType = sk_tools::PictureRenderer::kGPU_DeviceType;
271 if (FLAGS_multi > 1) {
272 error.printf("GPU not compatible with multithreaded tiling.\n");
273 return NULL;
274 }
275 }
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000276 else if (0 == strcmp(FLAGS_config[0], "msaa4")) {
277 deviceType = sk_tools::PictureRenderer::kGPU_DeviceType;
278 if (FLAGS_multi > 1) {
279 error.printf("GPU not compatible with multithreaded tiling.\n");
280 return NULL;
281 }
282 sampleCount = 4;
283 }
284 else if (0 == strcmp(FLAGS_config[0], "msaa16")) {
285 deviceType = sk_tools::PictureRenderer::kGPU_DeviceType;
286 if (FLAGS_multi > 1) {
287 error.printf("GPU not compatible with multithreaded tiling.\n");
288 return NULL;
289 }
290 sampleCount = 16;
291 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000292#if SK_ANGLE
293 else if (0 == strcmp(FLAGS_config[0], "angle")) {
294 deviceType = sk_tools::PictureRenderer::kAngle_DeviceType;
295 if (FLAGS_multi > 1) {
296 error.printf("Angle not compatible with multithreaded tiling.\n");
297 return NULL;
298 }
299 }
300#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000301#if SK_MESA
302 else if (0 == strcmp(FLAGS_config[0], "mesa")) {
303 deviceType = sk_tools::PictureRenderer::kMesa_DeviceType;
304 if (FLAGS_multi > 1) {
305 error.printf("Mesa not compatible with multithreaded tiling.\n");
306 return NULL;
307 }
308 }
309#endif
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000310#endif
311 else {
312 error.printf("%s is not a valid mode for --config\n", FLAGS_config[0]);
313 return NULL;
314 }
315 renderer->setDeviceType(deviceType);
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000316#if SK_SUPPORT_GPU
317 renderer->setSampleCount(sampleCount);
318#endif
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000319 }
320
321
322 sk_tools::PictureRenderer::BBoxHierarchyType bbhType
323 = sk_tools::PictureRenderer::kNone_BBoxHierarchyType;
324 if (FLAGS_bbh.count() > 0) {
325 const char* type = FLAGS_bbh[0];
326 if (0 == strcmp(type, "none")) {
327 bbhType = sk_tools::PictureRenderer::kNone_BBoxHierarchyType;
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +0000328 } else if (0 == strcmp(type, "quadtree")) {
329 bbhType = sk_tools::PictureRenderer::kQuadTree_BBoxHierarchyType;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000330 } else if (0 == strcmp(type, "rtree")) {
331 bbhType = sk_tools::PictureRenderer::kRTree_BBoxHierarchyType;
332 } else if (0 == strcmp(type, "grid")) {
333 if (!gridSupported) {
334 error.printf("'--bbh grid' is not compatible with --mode=%s.\n", mode);
335 return NULL;
336 }
337 bbhType = sk_tools::PictureRenderer::kTileGrid_BBoxHierarchyType;
338 if (FLAGS_bbh.count() != 3) {
339 error.printf("--bbh grid requires a width and a height.\n");
340 return NULL;
341 }
342 int gridWidth = atoi(FLAGS_bbh[1]);
343 int gridHeight = atoi(FLAGS_bbh[2]);
344 renderer->setGridSize(gridWidth, gridHeight);
345
346 } else {
347 error.printf("%s is not a valid value for --bbhType\n", type);
348 return NULL;
349 }
350 if (FLAGS_pipe && sk_tools::PictureRenderer::kNone_BBoxHierarchyType != bbhType) {
351 error.printf("--pipe and --bbh cannot be used together\n");
352 return NULL;
353 }
354 }
355 renderer->setBBoxHierarchyType(bbhType);
356 renderer->setScaleFactor(SkDoubleToScalar(FLAGS_scale));
357
358 return renderer.detach();
359}