blob: ffcab747720e5e765cab6e06b66b2615c935d715 [file] [log] [blame]
reed@google.com006db0f2012-06-27 19:33:29 +00001/*
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
8#include "BenchTimer.h"
scroggo@google.com4a26d9d2012-11-07 18:01:46 +00009#include "CopyTilesRenderer.h"
keyar@chromium.org163b5672012-08-01 17:53:29 +000010#include "PictureBenchmark.h"
scroggo@google.com161e1ba2013-03-04 16:41:06 +000011#include "PictureRenderingFlags.h"
scroggo@google.com9a412522012-09-07 15:21:18 +000012#include "SkBenchLogger.h"
scroggo@google.comf8d7d272013-02-22 21:38:35 +000013#include "SkBitmapFactory.h"
reed@google.com006db0f2012-06-27 19:33:29 +000014#include "SkCanvas.h"
scroggo@google.com161e1ba2013-03-04 16:41:06 +000015#include "SkFlags.h"
scroggo@google.com0a36f432012-09-10 20:29:13 +000016#include "SkGraphics.h"
scroggo@google.com5a7c6be2012-10-04 21:46:08 +000017#include "SkImageDecoder.h"
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +000018#include "SkMath.h"
reed@google.com006db0f2012-06-27 19:33:29 +000019#include "SkOSFile.h"
20#include "SkPicture.h"
21#include "SkStream.h"
22#include "SkTArray.h"
23#include "picture_utils.h"
24
scroggo@google.com161e1ba2013-03-04 16:41:06 +000025
26SkBenchLogger gLogger;
27
28// Flags used by this file, in alphabetical order.
29DECLARE_bool(deferImageDecoding);
30DEFINE_string(filter, "",
31 "type:flag : Enable canvas filtering to disable a paint flag, "
32 "use no blur or low quality blur, or use no hinting or "
33 "slight hinting. For all flags except AAClip, specify the "
34 "type of primitive to effect, or choose all. for AAClip "
35 "alone, the filter affects all clips independent of type. "
36 "Specific flags are listed above.");
37DEFINE_string(logFile, "", "Destination for writing log output, in addition to stdout.");
38DEFINE_bool(logPerIter, false, "Log each repeat timer instead of mean.");
39DEFINE_bool(min, false, "Print the minimum times (instead of average).");
40DECLARE_int32(multi);
41DECLARE_string(r);
42DEFINE_int32(repeat, 1, "Set the number of times to repeat each test.");
43DEFINE_bool(timeIndividualTiles, false, "Report times for drawing individual tiles, rather than "
44 "times for drawing the whole page. Requires tiled rendering.");
45DEFINE_string(timers, "", "[wcgWC]*: Display wall, cpu, gpu, truncated wall or truncated cpu time"
46 " for each picture.");
reed@google.com006db0f2012-06-27 19:33:29 +000047
caryclark@google.coma3622372012-11-06 21:26:13 +000048static char const * const gFilterTypes[] = {
49 "paint",
50 "point",
51 "line",
52 "bitmap",
53 "rect",
jvanverth@google.comd3c208c2013-01-22 13:54:52 +000054 "oval",
caryclark@google.coma3622372012-11-06 21:26:13 +000055 "path",
56 "text",
57 "all",
58};
59
60static const size_t kFilterTypesCount = sizeof(gFilterTypes) / sizeof(gFilterTypes[0]);
61
62static char const * const gFilterFlags[] = {
63 "antiAlias",
64 "filterBitmap",
65 "dither",
66 "underlineText",
67 "strikeThruText",
68 "fakeBoldText",
69 "linearText",
70 "subpixelText",
71 "devKernText",
72 "LCDRenderText",
73 "embeddedBitmapText",
74 "autoHinting",
75 "verticalText",
76 "genA8FromLCD",
77 "blur",
78 "hinting",
79 "slightHinting",
caryclark@google.come3e940c2012-11-07 16:42:17 +000080 "AAClip",
caryclark@google.coma3622372012-11-06 21:26:13 +000081};
82
83static const size_t kFilterFlagsCount = sizeof(gFilterFlags) / sizeof(gFilterFlags[0]);
84
85static SkString filtersName(sk_tools::PictureRenderer::DrawFilterFlags* drawFilters) {
86 int all = drawFilters[0];
87 size_t tIndex;
88 for (tIndex = 1; tIndex < SkDrawFilter::kTypeCount; ++tIndex) {
89 all &= drawFilters[tIndex];
90 }
91 SkString result;
92 for (size_t fIndex = 0; fIndex < kFilterFlagsCount; ++fIndex) {
93 SkString types;
94 if (all & (1 << fIndex)) {
95 types = gFilterTypes[SkDrawFilter::kTypeCount];
96 } else {
97 for (tIndex = 0; tIndex < SkDrawFilter::kTypeCount; ++tIndex) {
98 if (drawFilters[tIndex] & (1 << fIndex)) {
99 types += gFilterTypes[tIndex];
100 }
101 }
102 }
103 if (!types.size()) {
104 continue;
105 }
106 result += "_";
107 result += types;
108 result += ".";
109 result += gFilterFlags[fIndex];
110 }
111 return result;
112}
113
114static SkString filterTypesUsage() {
115 SkString result;
116 for (size_t index = 0; index < kFilterTypesCount; ++index) {
117 result += gFilterTypes[index];
118 if (index < kFilterTypesCount - 1) {
119 result += " | ";
120 }
121 }
122 return result;
123}
124
125static SkString filterFlagsUsage() {
126 SkString result;
127 size_t len = 0;
128 for (size_t index = 0; index < kFilterFlagsCount; ++index) {
129 result += gFilterFlags[index];
130 if (result.size() - len >= 72) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000131 result += "\n\t\t";
caryclark@google.coma3622372012-11-06 21:26:13 +0000132 len = result.size();
133 }
134 if (index < kFilterFlagsCount - 1) {
135 result += " | ";
136 }
137 }
138 return result;
139}
140
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000141#include "SkData.h"
142#include "SkLruImageCache.h"
143
144static SkLruImageCache gLruImageCache(1024*1024);
145
146static bool lazy_decode_bitmap(const void* buffer, size_t size, SkBitmap* bitmap) {
147 void* copiedBuffer = sk_malloc_throw(size);
148 memcpy(copiedBuffer, buffer, size);
149 SkAutoDataUnref data(SkData::NewFromMalloc(copiedBuffer, size));
150 SkBitmapFactory factory(&SkImageDecoder::DecodeMemoryToTarget);
151 factory.setImageCache(&gLruImageCache);
152 return factory.installPixelRef(data, bitmap);
153}
154
borenet@google.com66bcbd12012-09-17 18:26:06 +0000155static bool run_single_benchmark(const SkString& inputPath,
keyar@chromium.org163b5672012-08-01 17:53:29 +0000156 sk_tools::PictureBenchmark& benchmark) {
reed@google.com006db0f2012-06-27 19:33:29 +0000157 SkFILEStream inputStream;
158
reed@google.com006db0f2012-06-27 19:33:29 +0000159 inputStream.setPath(inputPath.c_str());
160 if (!inputStream.isValid()) {
scroggo@google.com9a412522012-09-07 15:21:18 +0000161 SkString err;
162 err.printf("Could not open file %s\n", inputPath.c_str());
163 gLogger.logError(err);
borenet@google.com66bcbd12012-09-17 18:26:06 +0000164 return false;
reed@google.com006db0f2012-06-27 19:33:29 +0000165 }
166
borenet@google.com66bcbd12012-09-17 18:26:06 +0000167 bool success = false;
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000168 SkPicture* picture;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000169 if (FLAGS_deferImageDecoding) {
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000170 picture = SkNEW_ARGS(SkPicture, (&inputStream, &success, &lazy_decode_bitmap));
171 } else {
172 picture = SkNEW_ARGS(SkPicture, (&inputStream, &success, &SkImageDecoder::DecodeMemory));
173 }
174 SkAutoTDelete<SkPicture> ad(picture);
175
borenet@google.com66bcbd12012-09-17 18:26:06 +0000176 if (!success) {
177 SkString err;
178 err.printf("Could not read an SkPicture from %s\n", inputPath.c_str());
179 gLogger.logError(err);
180 return false;
181 }
reed@google.com006db0f2012-06-27 19:33:29 +0000182
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000183 SkString filename;
184 sk_tools::get_basename(&filename, inputPath);
keyar@chromium.orgdb9a5fb2012-08-21 17:57:59 +0000185
186 SkString result;
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000187 result.printf("running bench [%i %i] %s ", picture->width(), picture->height(),
188 filename.c_str());
scroggo@google.com9a412522012-09-07 15:21:18 +0000189 gLogger.logProgress(result);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000190
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000191 benchmark.run(picture);
borenet@google.com66bcbd12012-09-17 18:26:06 +0000192 return true;
keyar@chromium.org0665f252012-07-10 18:30:18 +0000193}
194
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000195static void setup_benchmark(sk_tools::PictureBenchmark* benchmark) {
caryclark@google.coma3622372012-11-06 21:26:13 +0000196 sk_tools::PictureRenderer::DrawFilterFlags drawFilters[SkDrawFilter::kTypeCount];
197 sk_bzero(drawFilters, sizeof(drawFilters));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000198
199 if (FLAGS_filter.count() > 0) {
200 const char* filters = FLAGS_filter[0];
201 const char* colon = strchr(filters, ':');
202 if (colon) {
203 int type = -1;
204 size_t typeLen = colon - filters;
205 for (size_t tIndex = 0; tIndex < kFilterTypesCount; ++tIndex) {
206 if (typeLen == strlen(gFilterTypes[tIndex])
207 && !strncmp(filters, gFilterTypes[tIndex], typeLen)) {
208 type = tIndex;
209 break;
reed@google.com006db0f2012-06-27 19:33:29 +0000210 }
reed@google.com006db0f2012-06-27 19:33:29 +0000211 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000212 if (type < 0) {
junov@chromium.org9313ca42012-11-02 18:11:49 +0000213 SkString err;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000214 err.printf("Unknown type for --filter %s\n", filters);
junov@chromium.org9313ca42012-11-02 18:11:49 +0000215 gLogger.logError(err);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000216 exit(-1);
junov@chromium.org9313ca42012-11-02 18:11:49 +0000217 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000218 int flag = -1;
219 size_t flagLen = strlen(filters) - typeLen - 1;
220 for (size_t fIndex = 0; fIndex < kFilterFlagsCount; ++fIndex) {
221 if (flagLen == strlen(gFilterFlags[fIndex])
222 && !strncmp(colon + 1, gFilterFlags[fIndex], flagLen)) {
223 flag = 1 << fIndex;
224 break;
225 }
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000226 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000227 if (flag < 0) {
scroggo@google.com9a412522012-09-07 15:21:18 +0000228 SkString err;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000229 err.printf("Unknown flag for --filter %s\n", filters);
scroggo@google.com9a412522012-09-07 15:21:18 +0000230 gLogger.logError(err);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000231 exit(-1);
reed@google.com006db0f2012-06-27 19:33:29 +0000232 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000233 for (int index = 0; index < SkDrawFilter::kTypeCount; ++index) {
234 if (type != SkDrawFilter::kTypeCount && index != type) {
235 continue;
scroggo@google.com5239c322012-09-11 19:15:32 +0000236 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000237 drawFilters[index] = (sk_tools::PictureRenderer::DrawFilterFlags)
238 (drawFilters[index] | flag);
scroggo@google.com5239c322012-09-11 19:15:32 +0000239 }
reed@google.com006db0f2012-06-27 19:33:29 +0000240 } else {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000241 SkString err;
242 err.printf("Unknown arg for --filter %s : missing colon\n", filters);
243 gLogger.logError(err);
244 exit(-1);
reed@google.com006db0f2012-06-27 19:33:29 +0000245 }
246 }
247
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000248 if (FLAGS_timers.count() > 0) {
249 size_t index = 0;
250 bool timerWall = false;
251 bool truncatedTimerWall = false;
252 bool timerCpu = false;
253 bool truncatedTimerCpu = false;
254 bool timerGpu = false;
255 while (index < strlen(FLAGS_timers[0])) {
256 switch (FLAGS_timers[0][index]) {
257 case 'w':
258 timerWall = true;
259 break;
260 case 'c':
261 timerCpu = true;
262 break;
263 case 'W':
264 truncatedTimerWall = true;
265 break;
266 case 'C':
267 truncatedTimerCpu = true;
268 break;
269 case 'g':
270 timerGpu = true;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000271 break;
272 default:
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000273 SkDebugf("mystery character\n");
scroggo@google.com0556ea02013-02-08 19:38:21 +0000274 break;
275 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000276 index++;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000277 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000278 benchmark->setTimersToShow(timerWall, truncatedTimerWall, timerCpu, truncatedTimerCpu,
279 timerGpu);
reed@google.com006db0f2012-06-27 19:33:29 +0000280 }
keyar@chromium.org163b5672012-08-01 17:53:29 +0000281
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000282 SkString errorString;
283 SkAutoTUnref<sk_tools::PictureRenderer> renderer(parseRenderer(errorString,
284 kBench_PictureTool));
285
286 if (errorString.size() > 0) {
287 gLogger.logError(errorString);
keyar@chromium.org163b5672012-08-01 17:53:29 +0000288 }
junov@chromium.org9313ca42012-11-02 18:11:49 +0000289
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000290 if (NULL == renderer.get()) {
291 exit(-1);
292 }
293
294 if (FLAGS_timeIndividualTiles) {
295 if (FLAGS_multi > 1) {
296 gLogger.logError("Cannot time individual tiles with more than one thread.\n");
297 exit(-1);
298 }
299 sk_tools::TiledPictureRenderer* tiledRenderer = renderer->getTiledRenderer();
300 if (NULL == tiledRenderer) {
301 gLogger.logError("--timeIndividualTiles requires tiled rendering.\n");
302 exit(-1);
303 }
304 if (!tiledRenderer->supportsTimingIndividualTiles()) {
305 gLogger.logError("This renderer does not support --timeIndividualTiles.\n");
306 exit(-1);
307 }
308 benchmark->setTimeIndividualTiles(true);
309 }
310
311 if (FLAGS_r.count() < 1) {
312 gLogger.logError(".skp files or directories are required.\n");
313 exit(-1);
314 }
315
caryclark@google.coma3622372012-11-06 21:26:13 +0000316 renderer->setDrawFilters(drawFilters, filtersName(drawFilters));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000317 benchmark->setPrintMin(FLAGS_min);
318 benchmark->setLogPerIter(FLAGS_logPerIter);
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000319 benchmark->setRenderer(renderer);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000320 benchmark->setRepeats(FLAGS_repeat);
scroggo@google.com9a412522012-09-07 15:21:18 +0000321 benchmark->setLogger(&gLogger);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000322}
reed@google.com006db0f2012-06-27 19:33:29 +0000323
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000324static int process_input(const char* input,
borenet@google.com66bcbd12012-09-17 18:26:06 +0000325 sk_tools::PictureBenchmark& benchmark) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000326 SkString inputAsSkString(input);
327 SkOSFile::Iter iter(input, "skp");
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000328 SkString inputFilename;
borenet@google.com66bcbd12012-09-17 18:26:06 +0000329 int failures = 0;
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000330 if (iter.next(&inputFilename)) {
331 do {
332 SkString inputPath;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000333 sk_tools::make_filepath(&inputPath, inputAsSkString, inputFilename);
borenet@google.com57837bf2012-09-19 17:28:29 +0000334 if (!run_single_benchmark(inputPath, benchmark)) {
borenet@google.com66bcbd12012-09-17 18:26:06 +0000335 ++failures;
borenet@google.com57837bf2012-09-19 17:28:29 +0000336 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000337 } while(iter.next(&inputFilename));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000338 } else if (SkStrEndsWith(input, ".skp")) {
339 if (!run_single_benchmark(inputAsSkString, benchmark)) {
borenet@google.com66bcbd12012-09-17 18:26:06 +0000340 ++failures;
borenet@google.com57837bf2012-09-19 17:28:29 +0000341 }
342 } else {
343 SkString warning;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000344 warning.printf("Warning: skipping %s\n", input);
borenet@google.com57837bf2012-09-19 17:28:29 +0000345 gLogger.logError(warning);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000346 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000347 return failures;
reed@google.com006db0f2012-06-27 19:33:29 +0000348}
349
caryclark@google.com5987f582012-10-02 18:33:14 +0000350int tool_main(int argc, char** argv);
351int tool_main(int argc, char** argv) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000352 SkString usage;
353 usage.printf("Time drawing .skp files.\n"
354 "\tPossible arguments for --filter: [%s]\n\t\t[%s]",
355 filterTypesUsage().c_str(), filterFlagsUsage().c_str());
356 SkFlags::SetUsage(usage.c_str());
357 SkFlags::ParseCommandLine(argc, argv);
358
359 if (FLAGS_repeat < 1) {
360 SkString error;
361 error.printf("--repeats must be >= 1. Was %i\n", FLAGS_repeat);
362 gLogger.logError(error);
363 exit(-1);
364 }
365
366 if (FLAGS_logFile.count() == 1) {
367 if (!gLogger.SetLogFile(FLAGS_logFile[0])) {
368 SkString str;
369 str.printf("Could not open %s for writing.\n", FLAGS_logFile[0]);
370 gLogger.logError(str);
371 // TODO(borenet): We're disabling this for now, due to
372 // write-protected Android devices. The very short-term
373 // solution is to ignore the fact that we have no log file.
374 //exit(-1);
375 }
376 }
377
378
bsalomon@google.com4e230682013-01-15 20:37:04 +0000379#if SK_ENABLE_INST_COUNT
scroggo@google.com5239c322012-09-11 19:15:32 +0000380 gPrintInstCount = true;
381#endif
scroggo@google.com0a36f432012-09-10 20:29:13 +0000382 SkAutoGraphics ag;
reed@google.com006db0f2012-06-27 19:33:29 +0000383
scroggo@google.com5239c322012-09-11 19:15:32 +0000384 sk_tools::PictureBenchmark benchmark;
385
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000386 setup_benchmark(&benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000387
borenet@google.com66bcbd12012-09-17 18:26:06 +0000388 int failures = 0;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000389 for (int i = 0; i < FLAGS_r.count(); ++i) {
390 failures += process_input(FLAGS_r[i], benchmark);
borenet@google.com66bcbd12012-09-17 18:26:06 +0000391 }
392
393 if (failures != 0) {
394 SkString err;
395 err.printf("Failed to run %i benchmarks.\n", failures);
396 gLogger.logError(err);
397 return 1;
reed@google.com006db0f2012-06-27 19:33:29 +0000398 }
caryclark@google.com868e1f62012-10-02 20:00:03 +0000399 return 0;
reed@google.com006db0f2012-06-27 19:33:29 +0000400}
caryclark@google.com5987f582012-10-02 18:33:14 +0000401
402#if !defined SK_BUILD_FOR_IOS
403int main(int argc, char * const argv[]) {
404 return tool_main(argc, (char**) argv);
405}
406#endif