blob: ffe9e23e555e0dde7a61704fccabd425bc3380c2 [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;
robertphillips9f1c2412014-06-09 06:25:34 -070042 src.draw(recorder.beginRecording(src.width(), src.height(), &factory));
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000043 return recorder.endRecording();
44}
45
46static EXPERIMENTAL::SkPlayback* rerecord_with_skr(SkPicture& src) {
47 EXPERIMENTAL::SkRecording recording(src.width(), src.height());
48 src.draw(recording.canvas());
49 return recording.releasePlayback();
50}
51
52static void bench(SkPMColor* scratch, SkPicture& src, const char* name) {
53 SkAutoTUnref<SkPicture> picture(rerecord_with_tilegrid(src));
54 SkAutoTDelete<EXPERIMENTAL::SkPlayback> record(rerecord_with_skr(src));
commit-bot@chromium.orgad8ce572014-04-21 15:03:36 +000055
commit-bot@chromium.orgd6489c92014-04-11 19:06:46 +000056 SkAutoTDelete<SkCanvas> canvas(SkCanvas::NewRasterDirectN32(src.width(),
57 src.height(),
58 scratch,
59 src.width() * sizeof(SkPMColor)));
commit-bot@chromium.org4bffdf22014-04-11 16:13:54 +000060 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(FLAGS_tile), SkIntToScalar(FLAGS_tile)));
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000061
commit-bot@chromium.org8400b232014-04-28 15:30:02 +000062 BenchTimer timer;
63 timer.start();
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000064 for (int i = 0; i < FLAGS_loops; i++) {
65 if (FLAGS_skr) {
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000066 record->draw(canvas.get());
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000067 } else {
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000068 picture->draw(canvas.get());
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000069 }
70 }
commit-bot@chromium.org8400b232014-04-28 15:30:02 +000071 timer.end();
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000072
commit-bot@chromium.org8400b232014-04-28 15:30:02 +000073 const double msPerLoop = timer.fCpu / (double)FLAGS_loops;
commit-bot@chromium.org172eb1b2014-04-28 19:41:17 +000074 printf("%f\t%s\n", scale_time(msPerLoop), name);
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000075}
76
77int tool_main(int argc, char** argv);
78int tool_main(int argc, char** argv) {
79 SkCommandLineFlags::Parse(argc, argv);
80 SkAutoGraphics autoGraphics;
81
82 // We share a single scratch bitmap among benches to reduce the profile noise from allocation.
83 static const int kMaxArea = 209825221; // tabl_mozilla is this big.
84 SkAutoTMalloc<SkPMColor> scratch(kMaxArea);
85
86 SkOSFile::Iter it(FLAGS_skps[0], ".skp");
87 SkString filename;
88 bool failed = false;
89 while (it.next(&filename)) {
commit-bot@chromium.org5da5b592014-04-21 14:59:59 +000090 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, filename.c_str())) {
91 continue;
92 }
93
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000094 const SkString path = SkOSPath::SkPathJoin(FLAGS_skps[0], filename.c_str());
95
96 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path.c_str()));
97 if (!stream) {
98 SkDebugf("Could not read %s.\n", path.c_str());
99 failed = true;
100 continue;
101 }
102 SkAutoTUnref<SkPicture> src(SkPicture::CreateFromStream(stream));
103 if (!src) {
104 SkDebugf("Could not read %s as an SkPicture.\n", path.c_str());
105 failed = true;
106 continue;
107 }
108
109 if (src->width() * src->height() > kMaxArea) {
110 SkDebugf("%s (%dx%d) is larger than hardcoded scratch bitmap (%dpx).\n",
111 path.c_str(), src->width(), src->height(), kMaxArea);
112 failed = true;
113 continue;
114 }
115
commit-bot@chromium.orga0950412014-05-29 16:52:40 +0000116 bench(scratch.get(), *src, filename.c_str());
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +0000117 }
118 return failed ? 1 : 0;
119}
120
121#if !defined SK_BUILD_FOR_IOS
122int main(int argc, char * const argv[]) {
123 return tool_main(argc, (char**) argv);
124}
125#endif