blob: 5343191974036add6d849da5c71745871fff842c [file] [log] [blame]
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +00001/*
2 * Copyright 2014 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
Mike Klein744fb732014-06-23 15:13:26 -04008#include "SkCanvas.h"
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +00009#include "SkCommandLineFlags.h"
10#include "SkForceLinking.h"
11#include "SkGraphics.h"
12#include "SkOSFile.h"
13#include "SkPicture.h"
commit-bot@chromium.org8fe89d32014-05-09 15:00:10 +000014#include "SkPictureRecorder.h"
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000015#include "SkStream.h"
16#include "SkString.h"
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000017
mtklein90c471e2014-06-16 14:04:32 -070018#include "Stats.h"
mtklein9ac68ee2014-06-20 11:29:20 -070019#include "Timer.h"
mtklein90c471e2014-06-16 14:04:32 -070020
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000021__SK_FORCE_IMAGE_DECODER_LINKING;
22
mtklein90c471e2014-06-16 14:04:32 -070023DEFINE_string2(skps, r, "skps", "Directory containing SKPs to playback.");
24DEFINE_int32(samples, 10, "Gather this many samples of each picture playback.");
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000025DEFINE_bool(skr, false, "Play via SkRecord instead of SkPicture.");
26DEFINE_int32(tile, 1000000000, "Simulated tile size.");
commit-bot@chromium.org5da5b592014-04-21 14:59:59 +000027DEFINE_string(match, "", "The usual filters on file names of SKPs to bench.");
commit-bot@chromium.org172eb1b2014-04-28 19:41:17 +000028DEFINE_string(timescale, "ms", "Print times in ms, us, or ns");
mtklein90c471e2014-06-16 14:04:32 -070029DEFINE_int32(verbose, 0, "0: print min sample; "
30 "1: print min, mean, max and noise indication "
31 "2: print all samples");
commit-bot@chromium.org172eb1b2014-04-28 19:41:17 +000032
mtklein90c471e2014-06-16 14:04:32 -070033static double timescale() {
34 if (FLAGS_timescale.contains("us")) return 1000;
35 if (FLAGS_timescale.contains("ns")) return 1000000;
36 return 1;
commit-bot@chromium.org172eb1b2014-04-28 19:41:17 +000037}
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000038
Mike Klein744fb732014-06-23 15:13:26 -040039static SkPicture* rerecord(const SkPicture& src, bool skr) {
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000040 SkTileGridFactory::TileGridInfo info;
41 info.fTileInterval.set(FLAGS_tile, FLAGS_tile);
42 info.fMargin.setEmpty();
43 info.fOffset.setZero();
44 SkTileGridFactory factory(info);
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000045
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000046 SkPictureRecorder recorder;
Mike Klein744fb732014-06-23 15:13:26 -040047 src.draw(skr ? recorder.EXPERIMENTAL_beginRecording(src.width(), src.height(), &factory)
48 : recorder. beginRecording(src.width(), src.height(), &factory));
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000049 return recorder.endRecording();
50}
51
Mike Klein744fb732014-06-23 15:13:26 -040052static void bench(SkPMColor* scratch, const SkPicture& src, const char* name) {
53 SkAutoTUnref<const SkPicture> picture(rerecord(src, FLAGS_skr));
commit-bot@chromium.orgad8ce572014-04-21 15:03:36 +000054
commit-bot@chromium.orgd6489c92014-04-11 19:06:46 +000055 SkAutoTDelete<SkCanvas> canvas(SkCanvas::NewRasterDirectN32(src.width(),
56 src.height(),
57 scratch,
58 src.width() * sizeof(SkPMColor)));
commit-bot@chromium.org4bffdf22014-04-11 16:13:54 +000059 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(FLAGS_tile), SkIntToScalar(FLAGS_tile)));
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000060
mtklein90c471e2014-06-16 14:04:32 -070061 // Draw once to warm any caches. The first sample otherwise can be very noisy.
Mike Klein744fb732014-06-23 15:13:26 -040062 picture->draw(canvas.get());
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000063
mtklein9ac68ee2014-06-20 11:29:20 -070064 WallTimer timer;
65 const double scale = timescale();
mtklein90c471e2014-06-16 14:04:32 -070066 SkAutoTMalloc<double> samples(FLAGS_samples);
67 for (int i = 0; i < FLAGS_samples; i++) {
68 // We assume timer overhead (typically, ~30ns) is insignificant
69 // compared to draw runtime (at least ~100us, usually several ms).
mtklein9ac68ee2014-06-20 11:29:20 -070070 timer.start();
Mike Klein744fb732014-06-23 15:13:26 -040071 picture->draw(canvas.get());
mtklein90c471e2014-06-16 14:04:32 -070072 timer.end();
mtklein9ac68ee2014-06-20 11:29:20 -070073 samples[i] = timer.fWall * scale;
mtklein90c471e2014-06-16 14:04:32 -070074 }
75
76 Stats stats(samples.get(), FLAGS_samples);
77 if (FLAGS_verbose == 0) {
78 printf("%g\t%s\n", stats.min, name);
79 } else if (FLAGS_verbose == 1) {
80 // Get a rough idea of how noisy the measurements were.
81 const double noisePercent = 100 * sqrt(stats.var) / stats.mean;
82 printf("%g\t%g\t%g\t±%.0f%%\t%s\n", stats.min, stats.mean, stats.max, noisePercent, name);
83 } else if (FLAGS_verbose == 2) {
84 printf("%s", name);
85 for (int i = 0; i < FLAGS_samples; i++) {
86 printf("\t%g", samples[i]);
87 }
88 printf("\n");
89 }
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000090}
91
92int tool_main(int argc, char** argv);
93int tool_main(int argc, char** argv) {
94 SkCommandLineFlags::Parse(argc, argv);
95 SkAutoGraphics autoGraphics;
96
97 // We share a single scratch bitmap among benches to reduce the profile noise from allocation.
98 static const int kMaxArea = 209825221; // tabl_mozilla is this big.
99 SkAutoTMalloc<SkPMColor> scratch(kMaxArea);
100
101 SkOSFile::Iter it(FLAGS_skps[0], ".skp");
102 SkString filename;
103 bool failed = false;
104 while (it.next(&filename)) {
commit-bot@chromium.org5da5b592014-04-21 14:59:59 +0000105 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, filename.c_str())) {
106 continue;
107 }
108
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +0000109 const SkString path = SkOSPath::SkPathJoin(FLAGS_skps[0], filename.c_str());
110
111 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path.c_str()));
112 if (!stream) {
113 SkDebugf("Could not read %s.\n", path.c_str());
114 failed = true;
115 continue;
116 }
Mike Klein744fb732014-06-23 15:13:26 -0400117 SkAutoTUnref<const SkPicture> src(SkPicture::CreateFromStream(stream));
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +0000118 if (!src) {
119 SkDebugf("Could not read %s as an SkPicture.\n", path.c_str());
120 failed = true;
121 continue;
122 }
123
124 if (src->width() * src->height() > kMaxArea) {
125 SkDebugf("%s (%dx%d) is larger than hardcoded scratch bitmap (%dpx).\n",
126 path.c_str(), src->width(), src->height(), kMaxArea);
127 failed = true;
128 continue;
129 }
130
commit-bot@chromium.orga0950412014-05-29 16:52:40 +0000131 bench(scratch.get(), *src, filename.c_str());
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +0000132 }
133 return failed ? 1 : 0;
134}
135
136#if !defined SK_BUILD_FOR_IOS
137int main(int argc, char * const argv[]) {
138 return tool_main(argc, (char**) argv);
139}
140#endif