2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "BenchTimer.h"
9 #include "CopyTilesRenderer.h"
10 #include "LazyDecodeBitmap.h"
11 #include "PictureBenchmark.h"
12 #include "PictureRenderingFlags.h"
13 #include "SkBenchLogger.h"
14 #include "SkCommandLineFlags.h"
16 #include "SkDiscardableMemoryPool.h"
17 #include "SkGraphics.h"
18 #include "SkImageDecoder.h"
21 #include "SkPicture.h"
23 #include "picture_utils.h"
24 #include "PictureResultsWriter.h"
26 SkBenchLogger gLogger;
27 PictureResultsLoggerWriter gLogWriter(&gLogger);
28 PictureResultsMultiWriter gWriter;
30 // Flags used by this file, in alphabetical order.
31 DEFINE_bool(countRAM, false, "Count the RAM used for bitmap pixels in each skp file");
32 DECLARE_bool(deferImageDecoding);
33 DEFINE_string(filter, "",
34 "type:flag : Enable canvas filtering to disable a paint flag, "
35 "use no blur or low quality blur, or use no hinting or "
36 "slight hinting. For all flags except AAClip, specify the "
37 "type of primitive to effect, or choose all. for AAClip "
38 "alone, the filter affects all clips independent of type. "
39 "Specific flags are listed above.");
40 DEFINE_string(logFile, "", "Destination for writing log output, in addition to stdout.");
41 DEFINE_bool(logPerIter, false, "Log each repeat timer instead of mean.");
42 #ifdef SK_BUILD_JSON_WRITER
43 DEFINE_string(jsonLog, "", "Destination for writing JSON data.");
45 DEFINE_bool(min, false, "Print the minimum times (instead of average).");
47 DECLARE_string(readPath);
48 DEFINE_int32(repeat, 1, "Set the number of times to repeat each test.");
49 DEFINE_bool(timeIndividualTiles, false, "Report times for drawing individual tiles, rather than "
50 "times for drawing the whole page. Requires tiled rendering.");
51 DEFINE_bool(purgeDecodedTex, false, "Purge decoded and GPU-uploaded textures "
52 "after each iteration.");
53 DEFINE_string(timers, "c", "[wcgWC]*: Display wall, cpu, gpu, truncated wall or truncated cpu time"
54 " for each picture.");
55 DEFINE_bool(trackDeferredCaching, false, "Only meaningful with --deferImageDecoding and "
56 "SK_LAZY_CACHE_STATS set to true. Report percentage of cache hits when using "
57 "deferred image decoding.");
59 DEFINE_bool(preprocess, false, "If true, perform device specific preprocessing before timing.");
61 static char const * const gFilterTypes[] = {
73 static const size_t kFilterTypesCount = sizeof(gFilterTypes) / sizeof(gFilterTypes[0]);
75 static char const * const gFilterFlags[] = {
96 static const size_t kFilterFlagsCount = sizeof(gFilterFlags) / sizeof(gFilterFlags[0]);
98 static SkString filtersName(sk_tools::PictureRenderer::DrawFilterFlags* drawFilters) {
99 int all = drawFilters[0];
101 for (tIndex = 1; tIndex < SkDrawFilter::kTypeCount; ++tIndex) {
102 all &= drawFilters[tIndex];
105 for (size_t fIndex = 0; fIndex < kFilterFlagsCount; ++fIndex) {
107 if (all & (1 << fIndex)) {
108 types = gFilterTypes[SkDrawFilter::kTypeCount];
110 for (tIndex = 0; tIndex < SkDrawFilter::kTypeCount; ++tIndex) {
111 if (drawFilters[tIndex] & (1 << fIndex)) {
112 types += gFilterTypes[tIndex];
122 result += gFilterFlags[fIndex];
127 static SkString filterTypesUsage() {
129 for (size_t index = 0; index < kFilterTypesCount; ++index) {
130 result += gFilterTypes[index];
131 if (index < kFilterTypesCount - 1) {
138 static SkString filterFlagsUsage() {
141 for (size_t index = 0; index < kFilterFlagsCount; ++index) {
142 result += gFilterFlags[index];
143 if (result.size() - len >= 72) {
147 if (index < kFilterFlagsCount - 1) {
154 #if SK_LAZY_CACHE_STATS
155 static int32_t gTotalCacheHits;
156 static int32_t gTotalCacheMisses;
159 static bool run_single_benchmark(const SkString& inputPath,
160 sk_tools::PictureBenchmark& benchmark) {
161 SkFILEStream inputStream;
163 inputStream.setPath(inputPath.c_str());
164 if (!inputStream.isValid()) {
166 err.printf("Could not open file %s\n", inputPath.c_str());
167 gLogger.logError(err);
171 SkDiscardableMemoryPool* pool = SkGetGlobalDiscardableMemoryPool();
172 // Since the old picture has been deleted, all pixels should be cleared.
173 SkASSERT(pool->getRAMUsed() == 0);
174 if (FLAGS_countRAM) {
175 pool->setRAMBudget(SK_MaxU32);
176 // Set the limit to max, so all pixels will be kept
179 SkPicture::InstallPixelRefProc proc;
180 if (FLAGS_deferImageDecoding) {
181 proc = &sk_tools::LazyDecodeBitmap;
183 proc = &SkImageDecoder::DecodeMemory;
185 SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(&inputStream, proc));
187 if (NULL == picture.get()) {
189 err.printf("Could not read an SkPicture from %s\n", inputPath.c_str());
190 gLogger.logError(err);
194 SkString filename = SkOSPath::SkBasename(inputPath.c_str());
196 gWriter.bench(filename.c_str(), picture->width(), picture->height());
198 benchmark.run(picture);
200 #if SK_LAZY_CACHE_STATS
201 if (FLAGS_trackDeferredCaching) {
202 int cacheHits = pool->getCacheHits();
203 int cacheMisses = pool->getCacheMisses();
204 pool->resetCacheHitsAndMisses();
206 hitString.printf("Cache hit rate: %f\n", (double) cacheHits / (cacheHits + cacheMisses));
207 gLogger.logProgress(hitString);
208 gTotalCacheHits += cacheHits;
209 gTotalCacheMisses += cacheMisses;
212 if (FLAGS_countRAM) {
213 SkString ramCount("RAM used for bitmaps: ");
214 size_t bytes = pool->getRAMUsed();
216 size_t kb = bytes / 1024;
218 size_t mb = kb / 1024;
219 ramCount.appendf("%zi MB\n", mb);
221 ramCount.appendf("%zi KB\n", kb);
224 ramCount.appendf("%zi bytes\n", bytes);
226 gLogger.logProgress(ramCount);
232 static void setup_benchmark(sk_tools::PictureBenchmark* benchmark) {
233 sk_tools::PictureRenderer::DrawFilterFlags drawFilters[SkDrawFilter::kTypeCount];
234 sk_bzero(drawFilters, sizeof(drawFilters));
236 if (FLAGS_filter.count() > 0) {
237 const char* filters = FLAGS_filter[0];
238 const char* colon = strchr(filters, ':');
241 size_t typeLen = colon - filters;
242 for (size_t tIndex = 0; tIndex < kFilterTypesCount; ++tIndex) {
243 if (typeLen == strlen(gFilterTypes[tIndex])
244 && !strncmp(filters, gFilterTypes[tIndex], typeLen)) {
245 type = SkToS32(tIndex);
251 err.printf("Unknown type for --filter %s\n", filters);
252 gLogger.logError(err);
256 size_t flagLen = strlen(filters) - typeLen - 1;
257 for (size_t fIndex = 0; fIndex < kFilterFlagsCount; ++fIndex) {
258 if (flagLen == strlen(gFilterFlags[fIndex])
259 && !strncmp(colon + 1, gFilterFlags[fIndex], flagLen)) {
266 err.printf("Unknown flag for --filter %s\n", filters);
267 gLogger.logError(err);
270 for (int index = 0; index < SkDrawFilter::kTypeCount; ++index) {
271 if (type != SkDrawFilter::kTypeCount && index != type) {
274 drawFilters[index] = (sk_tools::PictureRenderer::DrawFilterFlags)
275 (drawFilters[index] | flag);
279 err.printf("Unknown arg for --filter %s : missing colon\n", filters);
280 gLogger.logError(err);
285 if (FLAGS_timers.count() > 0) {
287 bool timerWall = false;
288 bool truncatedTimerWall = false;
289 bool timerCpu = false;
290 bool truncatedTimerCpu = false;
291 bool timerGpu = false;
292 while (index < strlen(FLAGS_timers[0])) {
293 switch (FLAGS_timers[0][index]) {
301 truncatedTimerWall = true;
304 truncatedTimerCpu = true;
310 SkDebugf("mystery character\n");
315 benchmark->setTimersToShow(timerWall, truncatedTimerWall, timerCpu, truncatedTimerCpu,
319 SkString errorString;
320 SkAutoTUnref<sk_tools::PictureRenderer> renderer(parseRenderer(errorString,
321 kBench_PictureTool));
323 if (errorString.size() > 0) {
324 gLogger.logError(errorString);
327 if (NULL == renderer.get()) {
331 if (FLAGS_timeIndividualTiles) {
332 if (FLAGS_multi > 1) {
333 gLogger.logError("Cannot time individual tiles with more than one thread.\n");
336 sk_tools::TiledPictureRenderer* tiledRenderer = renderer->getTiledRenderer();
337 if (NULL == tiledRenderer) {
338 gLogger.logError("--timeIndividualTiles requires tiled rendering.\n");
341 if (!tiledRenderer->supportsTimingIndividualTiles()) {
342 gLogger.logError("This renderer does not support --timeIndividualTiles.\n");
345 benchmark->setTimeIndividualTiles(true);
348 benchmark->setPurgeDecodedTex(FLAGS_purgeDecodedTex);
349 benchmark->setPreprocess(FLAGS_preprocess);
351 if (FLAGS_readPath.count() < 1) {
352 gLogger.logError(".skp files or directories are required.\n");
356 renderer->setDrawFilters(drawFilters, filtersName(drawFilters));
357 if (FLAGS_logPerIter) {
358 benchmark->setTimerResultType(TimerData::kPerIter_Result);
359 } else if (FLAGS_min) {
360 benchmark->setTimerResultType(TimerData::kMin_Result);
362 benchmark->setTimerResultType(TimerData::kAvg_Result);
364 benchmark->setRenderer(renderer);
365 benchmark->setRepeats(FLAGS_repeat);
366 benchmark->setWriter(&gWriter);
369 static int process_input(const char* input,
370 sk_tools::PictureBenchmark& benchmark) {
371 SkString inputAsSkString(input);
372 SkOSFile::Iter iter(input, "skp");
373 SkString inputFilename;
375 if (iter.next(&inputFilename)) {
378 sk_tools::make_filepath(&inputPath, inputAsSkString, inputFilename);
379 if (!run_single_benchmark(inputPath, benchmark)) {
382 } while(iter.next(&inputFilename));
383 } else if (SkStrEndsWith(input, ".skp")) {
384 if (!run_single_benchmark(inputAsSkString, benchmark)) {
389 warning.printf("Warning: skipping %s\n", input);
390 gLogger.logError(warning);
395 int tool_main(int argc, char** argv);
396 int tool_main(int argc, char** argv) {
398 usage.printf("Time drawing .skp files.\n"
399 "\tPossible arguments for --filter: [%s]\n\t\t[%s]",
400 filterTypesUsage().c_str(), filterFlagsUsage().c_str());
401 SkCommandLineFlags::SetUsage(usage.c_str());
402 SkCommandLineFlags::Parse(argc, argv);
404 if (FLAGS_repeat < 1) {
406 error.printf("--repeats must be >= 1. Was %i\n", FLAGS_repeat);
407 gLogger.logError(error);
411 if (FLAGS_logFile.count() == 1) {
412 if (!gLogger.SetLogFile(FLAGS_logFile[0])) {
414 str.printf("Could not open %s for writing.\n", FLAGS_logFile[0]);
415 gLogger.logError(str);
416 // TODO(borenet): We're disabling this for now, due to
417 // write-protected Android devices. The very short-term
418 // solution is to ignore the fact that we have no log file.
423 #ifdef SK_BUILD_JSON_WRITER
424 SkAutoTDelete<PictureJSONResultsWriter> jsonWriter;
425 if (FLAGS_jsonLog.count() == 1) {
426 jsonWriter.reset(SkNEW(PictureJSONResultsWriter(FLAGS_jsonLog[0])));
427 gWriter.add(jsonWriter.get());
431 gWriter.add(&gLogWriter);
434 #if SK_ENABLE_INST_COUNT
435 gPrintInstCount = true;
439 sk_tools::PictureBenchmark benchmark;
441 setup_benchmark(&benchmark);
444 for (int i = 0; i < FLAGS_readPath.count(); ++i) {
445 failures += process_input(FLAGS_readPath[i], benchmark);
450 err.printf("Failed to run %i benchmarks.\n", failures);
451 gLogger.logError(err);
454 #if SK_LAZY_CACHE_STATS
455 if (FLAGS_trackDeferredCaching) {
456 SkDebugf("Total cache hit rate: %f\n",
457 (double) gTotalCacheHits / (gTotalCacheHits + gTotalCacheMisses));
464 #if !defined SK_BUILD_FOR_IOS
465 int main(int argc, char * const argv[]) {
466 return tool_main(argc, (char**) argv);