blob: 435dd77fb980237284b4b8f6e95a9eb3bb0de063 [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
commit-bot@chromium.org8400b232014-04-28 15:30:02 +00008#include "BenchTimer.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.orga0950412014-05-29 16:52:40 +000015#include "SkRecording.h"
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000016#include "SkStream.h"
17#include "SkString.h"
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000018
19__SK_FORCE_IMAGE_DECODER_LINKING;
20
21DEFINE_string2(skps, r, "skps", "Directory containing SKPs to read and re-record.");
22DEFINE_int32(loops, 10, "Number of times to play back each SKP.");
23DEFINE_bool(skr, false, "Play via SkRecord instead of SkPicture.");
24DEFINE_int32(tile, 1000000000, "Simulated tile size.");
commit-bot@chromium.org5da5b592014-04-21 14:59:59 +000025DEFINE_string(match, "", "The usual filters on file names of SKPs to bench.");
commit-bot@chromium.org172eb1b2014-04-28 19:41:17 +000026DEFINE_string(timescale, "ms", "Print times in ms, us, or ns");
27
28static double scale_time(double ms) {
29 if (FLAGS_timescale.contains("us")) ms *= 1000;
30 if (FLAGS_timescale.contains("ns")) ms *= 1000000;
31 return ms;
32}
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000033
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000034static SkPicture* rerecord_with_tilegrid(SkPicture& src) {
35 SkTileGridFactory::TileGridInfo info;
36 info.fTileInterval.set(FLAGS_tile, FLAGS_tile);
37 info.fMargin.setEmpty();
38 info.fOffset.setZero();
39 SkTileGridFactory factory(info);
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000040
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000041 SkPictureRecorder recorder;
42 src.draw(recorder.beginRecording(src.width(), src.height(), &factory,
43 SkPicture::kUsePathBoundsForClip_RecordingFlag));
44 return recorder.endRecording();
45}
46
47static EXPERIMENTAL::SkPlayback* rerecord_with_skr(SkPicture& src) {
48 EXPERIMENTAL::SkRecording recording(src.width(), src.height());
49 src.draw(recording.canvas());
50 return recording.releasePlayback();
51}
52
53static void bench(SkPMColor* scratch, SkPicture& src, const char* name) {
54 SkAutoTUnref<SkPicture> picture(rerecord_with_tilegrid(src));
55 SkAutoTDelete<EXPERIMENTAL::SkPlayback> record(rerecord_with_skr(src));
commit-bot@chromium.orgad8ce572014-04-21 15:03:36 +000056
commit-bot@chromium.orgd6489c92014-04-11 19:06:46 +000057 SkAutoTDelete<SkCanvas> canvas(SkCanvas::NewRasterDirectN32(src.width(),
58 src.height(),
59 scratch,
60 src.width() * sizeof(SkPMColor)));
commit-bot@chromium.org4bffdf22014-04-11 16:13:54 +000061 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(FLAGS_tile), SkIntToScalar(FLAGS_tile)));
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000062
commit-bot@chromium.org8400b232014-04-28 15:30:02 +000063 BenchTimer timer;
64 timer.start();
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000065 for (int i = 0; i < FLAGS_loops; i++) {
66 if (FLAGS_skr) {
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000067 record->draw(canvas.get());
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000068 } else {
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000069 picture->draw(canvas.get());
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000070 }
71 }
commit-bot@chromium.org8400b232014-04-28 15:30:02 +000072 timer.end();
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000073
commit-bot@chromium.org8400b232014-04-28 15:30:02 +000074 const double msPerLoop = timer.fCpu / (double)FLAGS_loops;
commit-bot@chromium.org172eb1b2014-04-28 19:41:17 +000075 printf("%f\t%s\n", scale_time(msPerLoop), name);
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000076}
77
78int tool_main(int argc, char** argv);
79int tool_main(int argc, char** argv) {
80 SkCommandLineFlags::Parse(argc, argv);
81 SkAutoGraphics autoGraphics;
82
83 // We share a single scratch bitmap among benches to reduce the profile noise from allocation.
84 static const int kMaxArea = 209825221; // tabl_mozilla is this big.
85 SkAutoTMalloc<SkPMColor> scratch(kMaxArea);
86
87 SkOSFile::Iter it(FLAGS_skps[0], ".skp");
88 SkString filename;
89 bool failed = false;
90 while (it.next(&filename)) {
commit-bot@chromium.org5da5b592014-04-21 14:59:59 +000091 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, filename.c_str())) {
92 continue;
93 }
94
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000095 const SkString path = SkOSPath::SkPathJoin(FLAGS_skps[0], filename.c_str());
96
97 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path.c_str()));
98 if (!stream) {
99 SkDebugf("Could not read %s.\n", path.c_str());
100 failed = true;
101 continue;
102 }
103 SkAutoTUnref<SkPicture> src(SkPicture::CreateFromStream(stream));
104 if (!src) {
105 SkDebugf("Could not read %s as an SkPicture.\n", path.c_str());
106 failed = true;
107 continue;
108 }
109
110 if (src->width() * src->height() > kMaxArea) {
111 SkDebugf("%s (%dx%d) is larger than hardcoded scratch bitmap (%dpx).\n",
112 path.c_str(), src->width(), src->height(), kMaxArea);
113 failed = true;
114 continue;
115 }
116
commit-bot@chromium.orga0950412014-05-29 16:52:40 +0000117 bench(scratch.get(), *src, filename.c_str());
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +0000118 }
119 return failed ? 1 : 0;
120}
121
122#if !defined SK_BUILD_FOR_IOS
123int main(int argc, char * const argv[]) {
124 return tool_main(argc, (char**) argv);
125}
126#endif