blob: ccec158f95e8b9e85683027936b5ad65f02a52ba [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
kkinnunen80549fc2014-06-30 06:36:31 -070029static const char kGpuAPINameGL[] = "gl";
30static const char kGpuAPINameGLES[] = "gles";
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +000031#define GPU_CONFIG_STRING "|gpu|msaa4|msaa16|nvprmsaa4|nvprmsaa16"
robertphillips@google.com11e10e32014-01-06 19:09:29 +000032#else
33#define GPU_CONFIG_STRING ""
34#endif
35#if SK_ANGLE
36#define ANGLE_CONFIG_STRING "|angle"
37#else
38#define ANGLE_CONFIG_STRING ""
39#endif
40#if SK_MESA
41#define MESA_CONFIG_STRING "|mesa"
42#else
43#define MESA_CONFIG_STRING ""
44#endif
45
scroggo@google.com161e1ba2013-03-04 16:41:06 +000046// Although this config does not support all the same options as gm, the names should be kept
47// consistent.
rmistry@google.com6ab96732014-01-06 18:37:24 +000048DEFINE_string(config, "8888", "["
robertphillips@google.com11e10e32014-01-06 19:09:29 +000049 "8888" GPU_CONFIG_STRING ANGLE_CONFIG_STRING MESA_CONFIG_STRING
rmistry@google.com6ab96732014-01-06 18:37:24 +000050 "]: Use the corresponding config.");
scroggo@google.com161e1ba2013-03-04 16:41:06 +000051
52DEFINE_bool(deferImageDecoding, false, "Defer decoding until drawing images. "
53 "Has no effect if the provided skp does not have its images encoded.");
54DEFINE_string(mode, "simple", "Run in the corresponding mode:\n"
55 "simple: Simple rendering.\n"
56 "tile width height: Use tiles with the given dimensions or percentages.\n"
57 "pow2tile minWidth height: Use tiles with widths that are all a power\n"
58 "\tof two such that they minimize the amount of wasted tile space.\n"
59 "\tminWidth must be a power of two.\n"
60 "copyTile width height: Draw the picture, then copy into tiles. If the\n"
61 "\tpicture is large enough, it is broken into larger tiles to avoid\n"
62 "\tcreating a large canvas.\n"
63// TODO: If bench_pictures and render_pictures were two separate targets, we could use build flags
64// to determine which modes to display.
65 "record: (Only in bench_pictures) Time recording from a picture to a new\n"
66 "\tpicture.\n"
67 "playbackCreation: (Only in bench_pictures) Time creation of the \n"
68 "\tSkPicturePlayback.\n"
69 "rerecord: (Only in render_pictures) Record the picture as a new skp,\n"
70 "\twith the bitmaps PNG encoded.\n");
scroggo@google.com161e1ba2013-03-04 16:41:06 +000071DEFINE_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.");
kkinnunen80549fc2014-06-30 06:36:31 -070077#if SK_SUPPORT_GPU
78DEFINE_string(gpuAPI, "", "Force use of specific gpu API. Using \"gl\" "
79 "forces OpenGL API. Using \"gles\" forces OpenGL ES API. "
80 "Defaults to empty string, which selects the API native to the "
81 "system.");
82#endif
scroggo@google.com161e1ba2013-03-04 16:41:06 +000083
84sk_tools::PictureRenderer* parseRenderer(SkString& error, PictureTool tool) {
85 error.reset();
86
scroggo@google.com161e1ba2013-03-04 16:41:06 +000087 bool useTiles = false;
88 const char* widthString = NULL;
89 const char* heightString = NULL;
90 bool isPowerOf2Mode = false;
91 bool isCopyMode = false;
92 const char* mode = NULL;
93 bool gridSupported = false;
94
95 SkAutoTUnref<sk_tools::PictureRenderer> renderer;
96 if (FLAGS_mode.count() >= 1) {
97 mode = FLAGS_mode[0];
98 if (0 == strcmp(mode, "record")) {
99 renderer.reset(SkNEW(sk_tools::RecordPictureRenderer));
100 gridSupported = true;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000101 } else if (0 == strcmp(mode, "tile") || 0 == strcmp(mode, "pow2tile")
102 || 0 == strcmp(mode, "copyTile")) {
103 useTiles = true;
104
105 if (0 == strcmp(mode, "pow2tile")) {
106 isPowerOf2Mode = true;
107 } else if (0 == strcmp(mode, "copyTile")) {
108 isCopyMode = true;
109 } else {
110 gridSupported = true;
111 }
112
113 if (FLAGS_mode.count() < 2) {
114 error.printf("Missing width for --mode %s\n", mode);
115 return NULL;
116 }
117
118 widthString = FLAGS_mode[1];
119 if (FLAGS_mode.count() < 3) {
120 error.printf("Missing height for --mode %s\n", mode);
121 return NULL;
122 }
123
124 heightString = FLAGS_mode[2];
125 } else if (0 == strcmp(mode, "playbackCreation") && kBench_PictureTool == tool) {
126 renderer.reset(SkNEW(sk_tools::PlaybackCreationRenderer));
127 gridSupported = true;
128 // undocumented
129 } else if (0 == strcmp(mode, "gatherPixelRefs") && kBench_PictureTool == tool) {
130 renderer.reset(sk_tools::CreateGatherPixelRefsRenderer());
131 } else if (0 == strcmp(mode, "rerecord") && kRender_PictureTool == tool) {
132 renderer.reset(SkNEW(sk_tools::RecordPictureRenderer));
133 // Allow 'mode' to be set to 'simple', but do not create a renderer, so we can
134 // ensure that pipe does not override a mode besides simple. The renderer will
135 // be created below.
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000136 } else if (0 == strcmp(mode, "simple")) {
137 gridSupported = true;
138 } else {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000139 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)));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000171 } else {
172 tiledRenderer.reset(SkNEW(sk_tools::TiledPictureRenderer));
173 }
174
175 if (isPowerOf2Mode) {
176 int minWidth = atoi(widthString);
177 if (!SkIsPow2(minWidth) || minWidth < 0) {
178 SkString err;
179 error.printf("-mode %s must be given a width"
180 " value that is a power of two\n", mode);
181 return NULL;
182 }
183 tiledRenderer->setTileMinPowerOf2Width(minWidth);
184 } else if (sk_tools::is_percentage(widthString)) {
185 if (isCopyMode) {
186 error.printf("--mode %s does not support percentages.\n", mode);
187 return NULL;
188 }
189 tiledRenderer->setTileWidthPercentage(atof(widthString));
190 if (!(tiledRenderer->getTileWidthPercentage() > 0)) {
191 error.printf("--mode %s must be given a width percentage > 0\n", mode);
192 return NULL;
193 }
194 } else {
195 tiledRenderer->setTileWidth(atoi(widthString));
196 if (!(tiledRenderer->getTileWidth() > 0)) {
197 error.printf("--mode %s must be given a width > 0\n", mode);
198 return NULL;
199 }
200 }
201
202 if (sk_tools::is_percentage(heightString)) {
203 if (isCopyMode) {
204 error.printf("--mode %s does not support percentages.\n", mode);
205 return NULL;
206 }
207 tiledRenderer->setTileHeightPercentage(atof(heightString));
208 if (!(tiledRenderer->getTileHeightPercentage() > 0)) {
209 error.printf("--mode %s must be given a height percentage > 0\n", mode);
210 return NULL;
211 }
212 } else {
213 tiledRenderer->setTileHeight(atoi(heightString));
214 if (!(tiledRenderer->getTileHeight() > 0)) {
215 SkString err;
216 error.printf("--mode %s must be given a height > 0\n", mode);
217 return NULL;
218 }
219 }
220
221 renderer.reset(tiledRenderer.detach());
222 if (FLAGS_pipe) {
223 error.printf("Pipe rendering is currently not compatible with tiling.\n"
224 "Turning off pipe.\n");
225 }
226
227 } else { // useTiles
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000228 if (FLAGS_pipe) {
229 if (renderer != NULL) {
230 error.printf("Pipe is incompatible with other modes.\n");
231 return NULL;
232 }
233 renderer.reset(SkNEW(sk_tools::PipePictureRenderer));
234 }
235 }
236
237 if (NULL == renderer) {
238 renderer.reset(SkNEW(sk_tools::SimplePictureRenderer));
239 }
240
241 if (FLAGS_viewport.count() > 0) {
242 if (FLAGS_viewport.count() != 2) {
243 error.printf("--viewport requires a width and a height.\n");
244 return NULL;
245 }
246 SkISize viewport;
247 viewport.fWidth = atoi(FLAGS_viewport[0]);
248 viewport.fHeight = atoi(FLAGS_viewport[1]);
249 renderer->setViewport(viewport);
250 }
251
252 sk_tools::PictureRenderer::SkDeviceTypes deviceType =
253 sk_tools::PictureRenderer::kBitmap_DeviceType;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000254#if SK_SUPPORT_GPU
kkinnunen80549fc2014-06-30 06:36:31 -0700255 GrGLStandard gpuAPI = kNone_GrGLStandard;
256 if (1 == FLAGS_gpuAPI.count()) {
257 if (FLAGS_gpuAPI.contains(kGpuAPINameGL)) {
258 gpuAPI = kGL_GrGLStandard;
259 } else if (FLAGS_gpuAPI.contains(kGpuAPINameGLES)) {
260 gpuAPI = kGLES_GrGLStandard;
261 } else {
262 error.printf("--gpuAPI invalid api value.\n");
263 return NULL;
264 }
265 } else if (FLAGS_gpuAPI.count() > 1) {
266 error.printf("--gpuAPI invalid api value.\n");
267 return NULL;
268 }
269
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000270 int sampleCount = 0;
271#endif
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000272 if (FLAGS_config.count() > 0) {
273 if (0 == strcmp(FLAGS_config[0], "8888")) {
274 deviceType = sk_tools::PictureRenderer::kBitmap_DeviceType;
275 }
276#if SK_SUPPORT_GPU
277 else if (0 == strcmp(FLAGS_config[0], "gpu")) {
278 deviceType = sk_tools::PictureRenderer::kGPU_DeviceType;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000279 }
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000280 else if (0 == strcmp(FLAGS_config[0], "msaa4")) {
281 deviceType = sk_tools::PictureRenderer::kGPU_DeviceType;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000282 sampleCount = 4;
283 }
284 else if (0 == strcmp(FLAGS_config[0], "msaa16")) {
285 deviceType = sk_tools::PictureRenderer::kGPU_DeviceType;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000286 sampleCount = 16;
287 }
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000288 else if (0 == strcmp(FLAGS_config[0], "nvprmsaa4")) {
289 deviceType = sk_tools::PictureRenderer::kNVPR_DeviceType;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000290 sampleCount = 4;
291 }
292 else if (0 == strcmp(FLAGS_config[0], "nvprmsaa16")) {
293 deviceType = sk_tools::PictureRenderer::kNVPR_DeviceType;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000294 sampleCount = 16;
295 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000296#if SK_ANGLE
297 else if (0 == strcmp(FLAGS_config[0], "angle")) {
298 deviceType = sk_tools::PictureRenderer::kAngle_DeviceType;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000299 }
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;
rmistry@google.com6ab96732014-01-06 18:37:24 +0000304 }
305#endif
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000306#endif
307 else {
308 error.printf("%s is not a valid mode for --config\n", FLAGS_config[0]);
309 return NULL;
310 }
kkinnunen80549fc2014-06-30 06:36:31 -0700311#if SK_SUPPORT_GPU
312 if (!renderer->setDeviceType(deviceType, gpuAPI)) {
313#else
314 if (!renderer->setDeviceType(deviceType)) {
315#endif
316 error.printf("Could not create backend for --config %s\n", FLAGS_config[0]);
317 return NULL;
318 }
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000319#if SK_SUPPORT_GPU
320 renderer->setSampleCount(sampleCount);
321#endif
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000322 }
323
324
325 sk_tools::PictureRenderer::BBoxHierarchyType bbhType
326 = sk_tools::PictureRenderer::kNone_BBoxHierarchyType;
327 if (FLAGS_bbh.count() > 0) {
328 const char* type = FLAGS_bbh[0];
329 if (0 == strcmp(type, "none")) {
330 bbhType = sk_tools::PictureRenderer::kNone_BBoxHierarchyType;
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +0000331 } else if (0 == strcmp(type, "quadtree")) {
332 bbhType = sk_tools::PictureRenderer::kQuadTree_BBoxHierarchyType;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000333 } else if (0 == strcmp(type, "rtree")) {
334 bbhType = sk_tools::PictureRenderer::kRTree_BBoxHierarchyType;
335 } else if (0 == strcmp(type, "grid")) {
336 if (!gridSupported) {
337 error.printf("'--bbh grid' is not compatible with --mode=%s.\n", mode);
338 return NULL;
339 }
340 bbhType = sk_tools::PictureRenderer::kTileGrid_BBoxHierarchyType;
341 if (FLAGS_bbh.count() != 3) {
342 error.printf("--bbh grid requires a width and a height.\n");
343 return NULL;
344 }
345 int gridWidth = atoi(FLAGS_bbh[1]);
346 int gridHeight = atoi(FLAGS_bbh[2]);
347 renderer->setGridSize(gridWidth, gridHeight);
348
349 } else {
350 error.printf("%s is not a valid value for --bbhType\n", type);
351 return NULL;
352 }
353 if (FLAGS_pipe && sk_tools::PictureRenderer::kNone_BBoxHierarchyType != bbhType) {
354 error.printf("--pipe and --bbh cannot be used together\n");
355 return NULL;
356 }
357 }
358 renderer->setBBoxHierarchyType(bbhType);
359 renderer->setScaleFactor(SkDoubleToScalar(FLAGS_scale));
360
361 return renderer.detach();
362}