blob: 3e7821819f4719f28fe59a13016d7aa1b4760252 [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
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +000029#define GPU_CONFIG_STRING "|gpu|msaa4|msaa16|nvprmsaa4|nvprmsaa16"
robertphillips@google.com11e10e32014-01-06 19:09:29 +000030#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");
scroggo@google.com161e1ba2013-03-04 16:41:06 +000069DEFINE_bool(pipe, false, "Use SkGPipe rendering. Currently incompatible with \"mode\".");
scroggo@google.com604e0c22013-04-09 21:25:46 +000070DEFINE_string2(readPath, r, "", "skp files or directories of skp files to process.");
scroggo@google.com161e1ba2013-03-04 16:41:06 +000071DEFINE_double(scale, 1, "Set the scale factor.");
72DEFINE_string(tiles, "", "Used with --mode copyTile to specify number of tiles per larger tile "
73 "in the x and y directions.");
74DEFINE_string(viewport, "", "width height: Set the viewport.");
75
76sk_tools::PictureRenderer* parseRenderer(SkString& error, PictureTool tool) {
77 error.reset();
78
scroggo@google.com161e1ba2013-03-04 16:41:06 +000079 bool useTiles = false;
80 const char* widthString = NULL;
81 const char* heightString = NULL;
82 bool isPowerOf2Mode = false;
83 bool isCopyMode = false;
84 const char* mode = NULL;
85 bool gridSupported = false;
86
87 SkAutoTUnref<sk_tools::PictureRenderer> renderer;
88 if (FLAGS_mode.count() >= 1) {
89 mode = FLAGS_mode[0];
90 if (0 == strcmp(mode, "record")) {
91 renderer.reset(SkNEW(sk_tools::RecordPictureRenderer));
92 gridSupported = true;
scroggo@google.com161e1ba2013-03-04 16:41:06 +000093 } else if (0 == strcmp(mode, "tile") || 0 == strcmp(mode, "pow2tile")
94 || 0 == strcmp(mode, "copyTile")) {
95 useTiles = true;
96
97 if (0 == strcmp(mode, "pow2tile")) {
98 isPowerOf2Mode = true;
99 } else if (0 == strcmp(mode, "copyTile")) {
100 isCopyMode = true;
101 } else {
102 gridSupported = true;
103 }
104
105 if (FLAGS_mode.count() < 2) {
106 error.printf("Missing width for --mode %s\n", mode);
107 return NULL;
108 }
109
110 widthString = FLAGS_mode[1];
111 if (FLAGS_mode.count() < 3) {
112 error.printf("Missing height for --mode %s\n", mode);
113 return NULL;
114 }
115
116 heightString = FLAGS_mode[2];
117 } else if (0 == strcmp(mode, "playbackCreation") && kBench_PictureTool == tool) {
118 renderer.reset(SkNEW(sk_tools::PlaybackCreationRenderer));
119 gridSupported = true;
120 // undocumented
121 } else if (0 == strcmp(mode, "gatherPixelRefs") && kBench_PictureTool == tool) {
122 renderer.reset(sk_tools::CreateGatherPixelRefsRenderer());
123 } else if (0 == strcmp(mode, "rerecord") && kRender_PictureTool == tool) {
124 renderer.reset(SkNEW(sk_tools::RecordPictureRenderer));
125 // Allow 'mode' to be set to 'simple', but do not create a renderer, so we can
126 // ensure that pipe does not override a mode besides simple. The renderer will
127 // be created below.
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000128 } else if (0 == strcmp(mode, "simple")) {
129 gridSupported = true;
130 } else {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000131 error.printf("%s is not a valid mode for --mode\n", mode);
132 return NULL;
133 }
134 }
135
136 if (useTiles) {
137 SkASSERT(NULL == renderer);
138 SkAutoTUnref<sk_tools::TiledPictureRenderer> tiledRenderer;
139 if (isCopyMode) {
140 int xTiles = -1;
141 int yTiles = -1;
142 if (FLAGS_tiles.count() > 0) {
143 if (FLAGS_tiles.count() != 2) {
144 error.printf("--tiles requires an x value and a y value.\n");
145 return NULL;
146 }
147 xTiles = atoi(FLAGS_tiles[0]);
148 yTiles = atoi(FLAGS_tiles[1]);
149 }
150
151 int x, y;
152 if (xTiles != -1 && yTiles != -1) {
153 x = xTiles;
154 y = yTiles;
155 if (x <= 0 || y <= 0) {
156 error.printf("--tiles must be given values > 0\n");
157 return NULL;
158 }
159 } else {
160 x = y = 4;
161 }
162 tiledRenderer.reset(SkNEW_ARGS(sk_tools::CopyTilesRenderer, (x, y)));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000163 } else {
164 tiledRenderer.reset(SkNEW(sk_tools::TiledPictureRenderer));
165 }
166
167 if (isPowerOf2Mode) {
168 int minWidth = atoi(widthString);
169 if (!SkIsPow2(minWidth) || minWidth < 0) {
170 SkString err;
171 error.printf("-mode %s must be given a width"
172 " value that is a power of two\n", mode);
173 return NULL;
174 }
175 tiledRenderer->setTileMinPowerOf2Width(minWidth);
176 } else if (sk_tools::is_percentage(widthString)) {
177 if (isCopyMode) {
178 error.printf("--mode %s does not support percentages.\n", mode);
179 return NULL;
180 }
181 tiledRenderer->setTileWidthPercentage(atof(widthString));
182 if (!(tiledRenderer->getTileWidthPercentage() > 0)) {
183 error.printf("--mode %s must be given a width percentage > 0\n", mode);
184 return NULL;
185 }
186 } else {
187 tiledRenderer->setTileWidth(atoi(widthString));
188 if (!(tiledRenderer->getTileWidth() > 0)) {
189 error.printf("--mode %s must be given a width > 0\n", mode);
190 return NULL;
191 }
192 }
193
194 if (sk_tools::is_percentage(heightString)) {
195 if (isCopyMode) {
196 error.printf("--mode %s does not support percentages.\n", mode);
197 return NULL;
198 }
199 tiledRenderer->setTileHeightPercentage(atof(heightString));
200 if (!(tiledRenderer->getTileHeightPercentage() > 0)) {
201 error.printf("--mode %s must be given a height percentage > 0\n", mode);
202 return NULL;
203 }
204 } else {
205 tiledRenderer->setTileHeight(atoi(heightString));
206 if (!(tiledRenderer->getTileHeight() > 0)) {
207 SkString err;
208 error.printf("--mode %s must be given a height > 0\n", mode);
209 return NULL;
210 }
211 }
212
213 renderer.reset(tiledRenderer.detach());
214 if (FLAGS_pipe) {
215 error.printf("Pipe rendering is currently not compatible with tiling.\n"
216 "Turning off pipe.\n");
217 }
218
219 } else { // useTiles
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000220 if (FLAGS_pipe) {
221 if (renderer != NULL) {
222 error.printf("Pipe is incompatible with other modes.\n");
223 return NULL;
224 }
225 renderer.reset(SkNEW(sk_tools::PipePictureRenderer));
226 }
227 }
228
229 if (NULL == renderer) {
230 renderer.reset(SkNEW(sk_tools::SimplePictureRenderer));
231 }
232
233 if (FLAGS_viewport.count() > 0) {
234 if (FLAGS_viewport.count() != 2) {
235 error.printf("--viewport requires a width and a height.\n");
236 return NULL;
237 }
238 SkISize viewport;
239 viewport.fWidth = atoi(FLAGS_viewport[0]);
240 viewport.fHeight = atoi(FLAGS_viewport[1]);
241 renderer->setViewport(viewport);
242 }
243
244 sk_tools::PictureRenderer::SkDeviceTypes deviceType =
245 sk_tools::PictureRenderer::kBitmap_DeviceType;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000246#if SK_SUPPORT_GPU
247 int sampleCount = 0;
248#endif
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000249 if (FLAGS_config.count() > 0) {
250 if (0 == strcmp(FLAGS_config[0], "8888")) {
251 deviceType = sk_tools::PictureRenderer::kBitmap_DeviceType;
252 }
253#if SK_SUPPORT_GPU
254 else if (0 == strcmp(FLAGS_config[0], "gpu")) {
255 deviceType = sk_tools::PictureRenderer::kGPU_DeviceType;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000256 }
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000257 else if (0 == strcmp(FLAGS_config[0], "msaa4")) {
258 deviceType = sk_tools::PictureRenderer::kGPU_DeviceType;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000259 sampleCount = 4;
260 }
261 else if (0 == strcmp(FLAGS_config[0], "msaa16")) {
262 deviceType = sk_tools::PictureRenderer::kGPU_DeviceType;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000263 sampleCount = 16;
264 }
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000265 else if (0 == strcmp(FLAGS_config[0], "nvprmsaa4")) {
266 deviceType = sk_tools::PictureRenderer::kNVPR_DeviceType;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000267 sampleCount = 4;
268 }
269 else if (0 == strcmp(FLAGS_config[0], "nvprmsaa16")) {
270 deviceType = sk_tools::PictureRenderer::kNVPR_DeviceType;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000271 sampleCount = 16;
272 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000273#if SK_ANGLE
274 else if (0 == strcmp(FLAGS_config[0], "angle")) {
275 deviceType = sk_tools::PictureRenderer::kAngle_DeviceType;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000276 }
277#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000278#if SK_MESA
279 else if (0 == strcmp(FLAGS_config[0], "mesa")) {
280 deviceType = sk_tools::PictureRenderer::kMesa_DeviceType;
rmistry@google.com6ab96732014-01-06 18:37:24 +0000281 }
282#endif
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000283#endif
284 else {
285 error.printf("%s is not a valid mode for --config\n", FLAGS_config[0]);
286 return NULL;
287 }
rmistry05ead8a2014-06-23 06:13:46 -0700288 renderer->setDeviceType(deviceType);
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000289#if SK_SUPPORT_GPU
290 renderer->setSampleCount(sampleCount);
291#endif
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000292 }
293
294
295 sk_tools::PictureRenderer::BBoxHierarchyType bbhType
296 = sk_tools::PictureRenderer::kNone_BBoxHierarchyType;
297 if (FLAGS_bbh.count() > 0) {
298 const char* type = FLAGS_bbh[0];
299 if (0 == strcmp(type, "none")) {
300 bbhType = sk_tools::PictureRenderer::kNone_BBoxHierarchyType;
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +0000301 } else if (0 == strcmp(type, "quadtree")) {
302 bbhType = sk_tools::PictureRenderer::kQuadTree_BBoxHierarchyType;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000303 } else if (0 == strcmp(type, "rtree")) {
304 bbhType = sk_tools::PictureRenderer::kRTree_BBoxHierarchyType;
305 } else if (0 == strcmp(type, "grid")) {
306 if (!gridSupported) {
307 error.printf("'--bbh grid' is not compatible with --mode=%s.\n", mode);
308 return NULL;
309 }
310 bbhType = sk_tools::PictureRenderer::kTileGrid_BBoxHierarchyType;
311 if (FLAGS_bbh.count() != 3) {
312 error.printf("--bbh grid requires a width and a height.\n");
313 return NULL;
314 }
315 int gridWidth = atoi(FLAGS_bbh[1]);
316 int gridHeight = atoi(FLAGS_bbh[2]);
317 renderer->setGridSize(gridWidth, gridHeight);
318
319 } else {
320 error.printf("%s is not a valid value for --bbhType\n", type);
321 return NULL;
322 }
323 if (FLAGS_pipe && sk_tools::PictureRenderer::kNone_BBoxHierarchyType != bbhType) {
324 error.printf("--pipe and --bbh cannot be used together\n");
325 return NULL;
326 }
327 }
328 renderer->setBBoxHierarchyType(bbhType);
329 renderer->setScaleFactor(SkDoubleToScalar(FLAGS_scale));
330
331 return renderer.detach();
332}