blob: f07fa8e3003caba70eb7d7d621c93c998e6f2b36 [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
8#include "SkCommandLineFlags.h"
9#include "SkForceLinking.h"
10#include "SkGraphics.h"
11#include "SkOSFile.h"
12#include "SkPicture.h"
commit-bot@chromium.org8fe89d32014-05-09 15:00:10 +000013#include "SkPictureRecorder.h"
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000014#include "SkRecording.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 "BenchTimer.h"
19#include "Stats.h"
20
21typedef WallTimer Timer;
22
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000023__SK_FORCE_IMAGE_DECODER_LINKING;
24
mtklein90c471e2014-06-16 14:04:32 -070025DEFINE_string2(skps, r, "skps", "Directory containing SKPs to playback.");
26DEFINE_int32(samples, 10, "Gather this many samples of each picture playback.");
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000027DEFINE_bool(skr, false, "Play via SkRecord instead of SkPicture.");
28DEFINE_int32(tile, 1000000000, "Simulated tile size.");
commit-bot@chromium.org5da5b592014-04-21 14:59:59 +000029DEFINE_string(match, "", "The usual filters on file names of SKPs to bench.");
commit-bot@chromium.org172eb1b2014-04-28 19:41:17 +000030DEFINE_string(timescale, "ms", "Print times in ms, us, or ns");
mtklein90c471e2014-06-16 14:04:32 -070031DEFINE_int32(verbose, 0, "0: print min sample; "
32 "1: print min, mean, max and noise indication "
33 "2: print all samples");
commit-bot@chromium.org172eb1b2014-04-28 19:41:17 +000034
mtklein90c471e2014-06-16 14:04:32 -070035static double timescale() {
36 if (FLAGS_timescale.contains("us")) return 1000;
37 if (FLAGS_timescale.contains("ns")) return 1000000;
38 return 1;
commit-bot@chromium.org172eb1b2014-04-28 19:41:17 +000039}
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000040
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000041static SkPicture* rerecord_with_tilegrid(SkPicture& src) {
42 SkTileGridFactory::TileGridInfo info;
43 info.fTileInterval.set(FLAGS_tile, FLAGS_tile);
44 info.fMargin.setEmpty();
45 info.fOffset.setZero();
46 SkTileGridFactory factory(info);
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000047
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000048 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -070049 src.draw(recorder.beginRecording(src.width(), src.height(), &factory));
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000050 return recorder.endRecording();
51}
52
53static EXPERIMENTAL::SkPlayback* rerecord_with_skr(SkPicture& src) {
54 EXPERIMENTAL::SkRecording recording(src.width(), src.height());
55 src.draw(recording.canvas());
56 return recording.releasePlayback();
57}
58
mtklein90c471e2014-06-16 14:04:32 -070059static void draw(const EXPERIMENTAL::SkPlayback& skr, const SkPicture& skp, SkCanvas* canvas) {
60 if (FLAGS_skr) {
61 skr.draw(canvas);
62 } else {
63 skp.draw(canvas);
64 }
65}
66
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000067static void bench(SkPMColor* scratch, SkPicture& src, const char* name) {
68 SkAutoTUnref<SkPicture> picture(rerecord_with_tilegrid(src));
69 SkAutoTDelete<EXPERIMENTAL::SkPlayback> record(rerecord_with_skr(src));
commit-bot@chromium.orgad8ce572014-04-21 15:03:36 +000070
commit-bot@chromium.orgd6489c92014-04-11 19:06:46 +000071 SkAutoTDelete<SkCanvas> canvas(SkCanvas::NewRasterDirectN32(src.width(),
72 src.height(),
73 scratch,
74 src.width() * sizeof(SkPMColor)));
commit-bot@chromium.org4bffdf22014-04-11 16:13:54 +000075 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(FLAGS_tile), SkIntToScalar(FLAGS_tile)));
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000076
mtklein90c471e2014-06-16 14:04:32 -070077 // Draw once to warm any caches. The first sample otherwise can be very noisy.
78 draw(*record, *picture, canvas.get());
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000079
mtklein90c471e2014-06-16 14:04:32 -070080 Timer timer;
81 SkAutoTMalloc<double> samples(FLAGS_samples);
82 for (int i = 0; i < FLAGS_samples; i++) {
83 // We assume timer overhead (typically, ~30ns) is insignificant
84 // compared to draw runtime (at least ~100us, usually several ms).
85 timer.start(timescale());
86 draw(*record, *picture, canvas.get());
87 timer.end();
88 samples[i] = timer.fWall;
89 }
90
91 Stats stats(samples.get(), FLAGS_samples);
92 if (FLAGS_verbose == 0) {
93 printf("%g\t%s\n", stats.min, name);
94 } else if (FLAGS_verbose == 1) {
95 // Get a rough idea of how noisy the measurements were.
96 const double noisePercent = 100 * sqrt(stats.var) / stats.mean;
97 printf("%g\t%g\t%g\t±%.0f%%\t%s\n", stats.min, stats.mean, stats.max, noisePercent, name);
98 } else if (FLAGS_verbose == 2) {
99 printf("%s", name);
100 for (int i = 0; i < FLAGS_samples; i++) {
101 printf("\t%g", samples[i]);
102 }
103 printf("\n");
104 }
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +0000105}
106
107int tool_main(int argc, char** argv);
108int tool_main(int argc, char** argv) {
109 SkCommandLineFlags::Parse(argc, argv);
110 SkAutoGraphics autoGraphics;
111
112 // We share a single scratch bitmap among benches to reduce the profile noise from allocation.
113 static const int kMaxArea = 209825221; // tabl_mozilla is this big.
114 SkAutoTMalloc<SkPMColor> scratch(kMaxArea);
115
116 SkOSFile::Iter it(FLAGS_skps[0], ".skp");
117 SkString filename;
118 bool failed = false;
119 while (it.next(&filename)) {
commit-bot@chromium.org5da5b592014-04-21 14:59:59 +0000120 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, filename.c_str())) {
121 continue;
122 }
123
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +0000124 const SkString path = SkOSPath::SkPathJoin(FLAGS_skps[0], filename.c_str());
125
126 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path.c_str()));
127 if (!stream) {
128 SkDebugf("Could not read %s.\n", path.c_str());
129 failed = true;
130 continue;
131 }
132 SkAutoTUnref<SkPicture> src(SkPicture::CreateFromStream(stream));
133 if (!src) {
134 SkDebugf("Could not read %s as an SkPicture.\n", path.c_str());
135 failed = true;
136 continue;
137 }
138
139 if (src->width() * src->height() > kMaxArea) {
140 SkDebugf("%s (%dx%d) is larger than hardcoded scratch bitmap (%dpx).\n",
141 path.c_str(), src->width(), src->height(), kMaxArea);
142 failed = true;
143 continue;
144 }
145
commit-bot@chromium.orga0950412014-05-29 16:52:40 +0000146 bench(scratch.get(), *src, filename.c_str());
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +0000147 }
148 return failed ? 1 : 0;
149}
150
151#if !defined SK_BUILD_FOR_IOS
152int main(int argc, char * const argv[]) {
153 return tool_main(argc, (char**) argv);
154}
155#endif