blob: 0c7644f2441613caf6ed2c160de98bf5f67b1556 [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.");
scroggo@google.comcc690202013-03-04 19:56:21 +000047DEFINE_bool(trackDeferredCaching, false, "Only meaningful with --deferImageDecoding and "
48 "LAZY_CACHE_STATS set to true. Report percentage of cache hits when using deferred "
49 "image decoding.");
reed@google.com006db0f2012-06-27 19:33:29 +000050
caryclark@google.coma3622372012-11-06 21:26:13 +000051static char const * const gFilterTypes[] = {
52 "paint",
53 "point",
54 "line",
55 "bitmap",
56 "rect",
jvanverth@google.comd3c208c2013-01-22 13:54:52 +000057 "oval",
caryclark@google.coma3622372012-11-06 21:26:13 +000058 "path",
59 "text",
60 "all",
61};
62
63static const size_t kFilterTypesCount = sizeof(gFilterTypes) / sizeof(gFilterTypes[0]);
64
65static char const * const gFilterFlags[] = {
66 "antiAlias",
67 "filterBitmap",
68 "dither",
69 "underlineText",
70 "strikeThruText",
71 "fakeBoldText",
72 "linearText",
73 "subpixelText",
74 "devKernText",
75 "LCDRenderText",
76 "embeddedBitmapText",
77 "autoHinting",
78 "verticalText",
79 "genA8FromLCD",
80 "blur",
81 "hinting",
82 "slightHinting",
caryclark@google.come3e940c2012-11-07 16:42:17 +000083 "AAClip",
caryclark@google.coma3622372012-11-06 21:26:13 +000084};
85
86static const size_t kFilterFlagsCount = sizeof(gFilterFlags) / sizeof(gFilterFlags[0]);
87
88static SkString filtersName(sk_tools::PictureRenderer::DrawFilterFlags* drawFilters) {
89 int all = drawFilters[0];
90 size_t tIndex;
91 for (tIndex = 1; tIndex < SkDrawFilter::kTypeCount; ++tIndex) {
92 all &= drawFilters[tIndex];
93 }
94 SkString result;
95 for (size_t fIndex = 0; fIndex < kFilterFlagsCount; ++fIndex) {
96 SkString types;
97 if (all & (1 << fIndex)) {
98 types = gFilterTypes[SkDrawFilter::kTypeCount];
99 } else {
100 for (tIndex = 0; tIndex < SkDrawFilter::kTypeCount; ++tIndex) {
101 if (drawFilters[tIndex] & (1 << fIndex)) {
102 types += gFilterTypes[tIndex];
103 }
104 }
105 }
106 if (!types.size()) {
107 continue;
108 }
109 result += "_";
110 result += types;
111 result += ".";
112 result += gFilterFlags[fIndex];
113 }
114 return result;
115}
116
117static SkString filterTypesUsage() {
118 SkString result;
119 for (size_t index = 0; index < kFilterTypesCount; ++index) {
120 result += gFilterTypes[index];
121 if (index < kFilterTypesCount - 1) {
122 result += " | ";
123 }
124 }
125 return result;
126}
127
128static SkString filterFlagsUsage() {
129 SkString result;
130 size_t len = 0;
131 for (size_t index = 0; index < kFilterFlagsCount; ++index) {
132 result += gFilterFlags[index];
133 if (result.size() - len >= 72) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000134 result += "\n\t\t";
caryclark@google.coma3622372012-11-06 21:26:13 +0000135 len = result.size();
136 }
137 if (index < kFilterFlagsCount - 1) {
138 result += " | ";
139 }
140 }
141 return result;
142}
143
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000144#include "SkData.h"
145#include "SkLruImageCache.h"
scroggo@google.comcc690202013-03-04 19:56:21 +0000146#include "SkLazyPixelRef.h"
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000147
148static SkLruImageCache gLruImageCache(1024*1024);
149
150static bool lazy_decode_bitmap(const void* buffer, size_t size, SkBitmap* bitmap) {
151 void* copiedBuffer = sk_malloc_throw(size);
152 memcpy(copiedBuffer, buffer, size);
153 SkAutoDataUnref data(SkData::NewFromMalloc(copiedBuffer, size));
154 SkBitmapFactory factory(&SkImageDecoder::DecodeMemoryToTarget);
155 factory.setImageCache(&gLruImageCache);
156 return factory.installPixelRef(data, bitmap);
157}
158
scroggo@google.comcc690202013-03-04 19:56:21 +0000159#if LAZY_CACHE_STATS
160static int32_t gTotalCacheHits;
161static int32_t gTotalCacheMisses;
162#endif
163
borenet@google.com66bcbd12012-09-17 18:26:06 +0000164static bool run_single_benchmark(const SkString& inputPath,
keyar@chromium.org163b5672012-08-01 17:53:29 +0000165 sk_tools::PictureBenchmark& benchmark) {
reed@google.com006db0f2012-06-27 19:33:29 +0000166 SkFILEStream inputStream;
167
reed@google.com006db0f2012-06-27 19:33:29 +0000168 inputStream.setPath(inputPath.c_str());
169 if (!inputStream.isValid()) {
scroggo@google.com9a412522012-09-07 15:21:18 +0000170 SkString err;
171 err.printf("Could not open file %s\n", inputPath.c_str());
172 gLogger.logError(err);
borenet@google.com66bcbd12012-09-17 18:26:06 +0000173 return false;
reed@google.com006db0f2012-06-27 19:33:29 +0000174 }
175
borenet@google.com66bcbd12012-09-17 18:26:06 +0000176 bool success = false;
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000177 SkPicture* picture;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000178 if (FLAGS_deferImageDecoding) {
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000179 picture = SkNEW_ARGS(SkPicture, (&inputStream, &success, &lazy_decode_bitmap));
180 } else {
181 picture = SkNEW_ARGS(SkPicture, (&inputStream, &success, &SkImageDecoder::DecodeMemory));
182 }
183 SkAutoTDelete<SkPicture> ad(picture);
184
borenet@google.com66bcbd12012-09-17 18:26:06 +0000185 if (!success) {
186 SkString err;
187 err.printf("Could not read an SkPicture from %s\n", inputPath.c_str());
188 gLogger.logError(err);
189 return false;
190 }
reed@google.com006db0f2012-06-27 19:33:29 +0000191
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000192 SkString filename;
193 sk_tools::get_basename(&filename, inputPath);
keyar@chromium.orgdb9a5fb2012-08-21 17:57:59 +0000194
195 SkString result;
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000196 result.printf("running bench [%i %i] %s ", picture->width(), picture->height(),
197 filename.c_str());
scroggo@google.com9a412522012-09-07 15:21:18 +0000198 gLogger.logProgress(result);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000199
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000200 benchmark.run(picture);
scroggo@google.comcc690202013-03-04 19:56:21 +0000201
202#if LAZY_CACHE_STATS
203 if (FLAGS_trackDeferredCaching) {
204 int32_t cacheHits = SkLazyPixelRef::GetCacheHits();
205 int32_t cacheMisses = SkLazyPixelRef::GetCacheMisses();
206 SkLazyPixelRef::ResetCacheStats();
207 SkDebugf("Cache hit rate: %f\n", (double) cacheHits / (cacheHits + cacheMisses));
208 gTotalCacheHits += cacheHits;
209 gTotalCacheMisses += cacheMisses;
210 }
211#endif
212
borenet@google.com66bcbd12012-09-17 18:26:06 +0000213 return true;
keyar@chromium.org0665f252012-07-10 18:30:18 +0000214}
215
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000216static void setup_benchmark(sk_tools::PictureBenchmark* benchmark) {
caryclark@google.coma3622372012-11-06 21:26:13 +0000217 sk_tools::PictureRenderer::DrawFilterFlags drawFilters[SkDrawFilter::kTypeCount];
218 sk_bzero(drawFilters, sizeof(drawFilters));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000219
220 if (FLAGS_filter.count() > 0) {
221 const char* filters = FLAGS_filter[0];
222 const char* colon = strchr(filters, ':');
223 if (colon) {
scroggo@google.comcc690202013-03-04 19:56:21 +0000224 int32_t type = -1;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000225 size_t typeLen = colon - filters;
226 for (size_t tIndex = 0; tIndex < kFilterTypesCount; ++tIndex) {
227 if (typeLen == strlen(gFilterTypes[tIndex])
228 && !strncmp(filters, gFilterTypes[tIndex], typeLen)) {
scroggo@google.comcc690202013-03-04 19:56:21 +0000229 type = SkToS32(tIndex);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000230 break;
reed@google.com006db0f2012-06-27 19:33:29 +0000231 }
reed@google.com006db0f2012-06-27 19:33:29 +0000232 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000233 if (type < 0) {
junov@chromium.org9313ca42012-11-02 18:11:49 +0000234 SkString err;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000235 err.printf("Unknown type for --filter %s\n", filters);
junov@chromium.org9313ca42012-11-02 18:11:49 +0000236 gLogger.logError(err);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000237 exit(-1);
junov@chromium.org9313ca42012-11-02 18:11:49 +0000238 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000239 int flag = -1;
240 size_t flagLen = strlen(filters) - typeLen - 1;
241 for (size_t fIndex = 0; fIndex < kFilterFlagsCount; ++fIndex) {
242 if (flagLen == strlen(gFilterFlags[fIndex])
243 && !strncmp(colon + 1, gFilterFlags[fIndex], flagLen)) {
244 flag = 1 << fIndex;
245 break;
246 }
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000247 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000248 if (flag < 0) {
scroggo@google.com9a412522012-09-07 15:21:18 +0000249 SkString err;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000250 err.printf("Unknown flag for --filter %s\n", filters);
scroggo@google.com9a412522012-09-07 15:21:18 +0000251 gLogger.logError(err);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000252 exit(-1);
reed@google.com006db0f2012-06-27 19:33:29 +0000253 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000254 for (int index = 0; index < SkDrawFilter::kTypeCount; ++index) {
255 if (type != SkDrawFilter::kTypeCount && index != type) {
256 continue;
scroggo@google.com5239c322012-09-11 19:15:32 +0000257 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000258 drawFilters[index] = (sk_tools::PictureRenderer::DrawFilterFlags)
259 (drawFilters[index] | flag);
scroggo@google.com5239c322012-09-11 19:15:32 +0000260 }
reed@google.com006db0f2012-06-27 19:33:29 +0000261 } else {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000262 SkString err;
263 err.printf("Unknown arg for --filter %s : missing colon\n", filters);
264 gLogger.logError(err);
265 exit(-1);
reed@google.com006db0f2012-06-27 19:33:29 +0000266 }
267 }
268
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000269 if (FLAGS_timers.count() > 0) {
270 size_t index = 0;
271 bool timerWall = false;
272 bool truncatedTimerWall = false;
273 bool timerCpu = false;
274 bool truncatedTimerCpu = false;
275 bool timerGpu = false;
276 while (index < strlen(FLAGS_timers[0])) {
277 switch (FLAGS_timers[0][index]) {
278 case 'w':
279 timerWall = true;
280 break;
281 case 'c':
282 timerCpu = true;
283 break;
284 case 'W':
285 truncatedTimerWall = true;
286 break;
287 case 'C':
288 truncatedTimerCpu = true;
289 break;
290 case 'g':
291 timerGpu = true;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000292 break;
293 default:
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000294 SkDebugf("mystery character\n");
scroggo@google.com0556ea02013-02-08 19:38:21 +0000295 break;
296 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000297 index++;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000298 }
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000299 benchmark->setTimersToShow(timerWall, truncatedTimerWall, timerCpu, truncatedTimerCpu,
300 timerGpu);
reed@google.com006db0f2012-06-27 19:33:29 +0000301 }
keyar@chromium.org163b5672012-08-01 17:53:29 +0000302
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000303 SkString errorString;
304 SkAutoTUnref<sk_tools::PictureRenderer> renderer(parseRenderer(errorString,
305 kBench_PictureTool));
306
307 if (errorString.size() > 0) {
308 gLogger.logError(errorString);
keyar@chromium.org163b5672012-08-01 17:53:29 +0000309 }
junov@chromium.org9313ca42012-11-02 18:11:49 +0000310
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000311 if (NULL == renderer.get()) {
312 exit(-1);
313 }
314
315 if (FLAGS_timeIndividualTiles) {
316 if (FLAGS_multi > 1) {
317 gLogger.logError("Cannot time individual tiles with more than one thread.\n");
318 exit(-1);
319 }
320 sk_tools::TiledPictureRenderer* tiledRenderer = renderer->getTiledRenderer();
321 if (NULL == tiledRenderer) {
322 gLogger.logError("--timeIndividualTiles requires tiled rendering.\n");
323 exit(-1);
324 }
325 if (!tiledRenderer->supportsTimingIndividualTiles()) {
326 gLogger.logError("This renderer does not support --timeIndividualTiles.\n");
327 exit(-1);
328 }
329 benchmark->setTimeIndividualTiles(true);
330 }
331
332 if (FLAGS_r.count() < 1) {
333 gLogger.logError(".skp files or directories are required.\n");
334 exit(-1);
335 }
336
caryclark@google.coma3622372012-11-06 21:26:13 +0000337 renderer->setDrawFilters(drawFilters, filtersName(drawFilters));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000338 benchmark->setPrintMin(FLAGS_min);
339 benchmark->setLogPerIter(FLAGS_logPerIter);
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000340 benchmark->setRenderer(renderer);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000341 benchmark->setRepeats(FLAGS_repeat);
scroggo@google.com9a412522012-09-07 15:21:18 +0000342 benchmark->setLogger(&gLogger);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000343}
reed@google.com006db0f2012-06-27 19:33:29 +0000344
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000345static int process_input(const char* input,
borenet@google.com66bcbd12012-09-17 18:26:06 +0000346 sk_tools::PictureBenchmark& benchmark) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000347 SkString inputAsSkString(input);
348 SkOSFile::Iter iter(input, "skp");
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000349 SkString inputFilename;
borenet@google.com66bcbd12012-09-17 18:26:06 +0000350 int failures = 0;
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000351 if (iter.next(&inputFilename)) {
352 do {
353 SkString inputPath;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000354 sk_tools::make_filepath(&inputPath, inputAsSkString, inputFilename);
borenet@google.com57837bf2012-09-19 17:28:29 +0000355 if (!run_single_benchmark(inputPath, benchmark)) {
borenet@google.com66bcbd12012-09-17 18:26:06 +0000356 ++failures;
borenet@google.com57837bf2012-09-19 17:28:29 +0000357 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000358 } while(iter.next(&inputFilename));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000359 } else if (SkStrEndsWith(input, ".skp")) {
360 if (!run_single_benchmark(inputAsSkString, benchmark)) {
borenet@google.com66bcbd12012-09-17 18:26:06 +0000361 ++failures;
borenet@google.com57837bf2012-09-19 17:28:29 +0000362 }
363 } else {
364 SkString warning;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000365 warning.printf("Warning: skipping %s\n", input);
borenet@google.com57837bf2012-09-19 17:28:29 +0000366 gLogger.logError(warning);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000367 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000368 return failures;
reed@google.com006db0f2012-06-27 19:33:29 +0000369}
370
caryclark@google.com5987f582012-10-02 18:33:14 +0000371int tool_main(int argc, char** argv);
372int tool_main(int argc, char** argv) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000373 SkString usage;
374 usage.printf("Time drawing .skp files.\n"
375 "\tPossible arguments for --filter: [%s]\n\t\t[%s]",
376 filterTypesUsage().c_str(), filterFlagsUsage().c_str());
377 SkFlags::SetUsage(usage.c_str());
378 SkFlags::ParseCommandLine(argc, argv);
379
380 if (FLAGS_repeat < 1) {
381 SkString error;
382 error.printf("--repeats must be >= 1. Was %i\n", FLAGS_repeat);
383 gLogger.logError(error);
384 exit(-1);
385 }
386
387 if (FLAGS_logFile.count() == 1) {
388 if (!gLogger.SetLogFile(FLAGS_logFile[0])) {
389 SkString str;
390 str.printf("Could not open %s for writing.\n", FLAGS_logFile[0]);
391 gLogger.logError(str);
392 // TODO(borenet): We're disabling this for now, due to
393 // write-protected Android devices. The very short-term
394 // solution is to ignore the fact that we have no log file.
395 //exit(-1);
396 }
397 }
398
399
bsalomon@google.com4e230682013-01-15 20:37:04 +0000400#if SK_ENABLE_INST_COUNT
scroggo@google.com5239c322012-09-11 19:15:32 +0000401 gPrintInstCount = true;
402#endif
scroggo@google.com0a36f432012-09-10 20:29:13 +0000403 SkAutoGraphics ag;
reed@google.com006db0f2012-06-27 19:33:29 +0000404
scroggo@google.com5239c322012-09-11 19:15:32 +0000405 sk_tools::PictureBenchmark benchmark;
406
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000407 setup_benchmark(&benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000408
borenet@google.com66bcbd12012-09-17 18:26:06 +0000409 int failures = 0;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000410 for (int i = 0; i < FLAGS_r.count(); ++i) {
411 failures += process_input(FLAGS_r[i], benchmark);
borenet@google.com66bcbd12012-09-17 18:26:06 +0000412 }
413
414 if (failures != 0) {
415 SkString err;
416 err.printf("Failed to run %i benchmarks.\n", failures);
417 gLogger.logError(err);
418 return 1;
reed@google.com006db0f2012-06-27 19:33:29 +0000419 }
scroggo@google.comcc690202013-03-04 19:56:21 +0000420#if LAZY_CACHE_STATS
421 if (FLAGS_trackDeferredCaching) {
422 SkDebugf("Total cache hit rate: %f\n",
423 (double) gTotalCacheHits / (gTotalCacheHits + gTotalCacheMisses));
424 }
425#endif
caryclark@google.com868e1f62012-10-02 20:00:03 +0000426 return 0;
reed@google.com006db0f2012-06-27 19:33:29 +0000427}
caryclark@google.com5987f582012-10-02 18:33:14 +0000428
429#if !defined SK_BUILD_FOR_IOS
430int main(int argc, char * const argv[]) {
431 return tool_main(argc, (char**) argv);
432}
433#endif