blob: 89b384842d88d09a665e9e8211573eb4432ee1d1 [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"
14#include "SkRecordDraw.h"
commit-bot@chromium.orgad8ce572014-04-21 15:03:36 +000015#include "SkRecordOpts.h"
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000016#include "SkRecorder.h"
17#include "SkStream.h"
18#include "SkString.h"
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000019
20__SK_FORCE_IMAGE_DECODER_LINKING;
21
22DEFINE_string2(skps, r, "skps", "Directory containing SKPs to read and re-record.");
23DEFINE_int32(loops, 10, "Number of times to play back each SKP.");
24DEFINE_bool(skr, false, "Play via SkRecord instead of SkPicture.");
25DEFINE_int32(tile, 1000000000, "Simulated tile size.");
commit-bot@chromium.org5da5b592014-04-21 14:59:59 +000026DEFINE_string(match, "", "The usual filters on file names of SKPs to bench.");
commit-bot@chromium.org172eb1b2014-04-28 19:41:17 +000027DEFINE_string(timescale, "ms", "Print times in ms, us, or ns");
28
29static double scale_time(double ms) {
30 if (FLAGS_timescale.contains("us")) ms *= 1000;
31 if (FLAGS_timescale.contains("ns")) ms *= 1000000;
32 return ms;
33}
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000034
35static void bench(SkPMColor* scratch, SkPicture& src, const char* name) {
commit-bot@chromium.orgb17a24f2014-04-14 20:33:05 +000036 // We don't use the public SkRecording interface here because we need kWriteOnly_Mode.
37 // (We don't want SkPicturePlayback to be able to optimize playing into our SkRecord.)
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000038 SkRecord record;
39 SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, src.width(), src.height());
40 src.draw(&recorder);
41
commit-bot@chromium.orgad8ce572014-04-21 15:03:36 +000042 SkRecordOptimize(&record);
43
commit-bot@chromium.orgd6489c92014-04-11 19:06:46 +000044 SkAutoTDelete<SkCanvas> canvas(SkCanvas::NewRasterDirectN32(src.width(),
45 src.height(),
46 scratch,
47 src.width() * sizeof(SkPMColor)));
commit-bot@chromium.org4bffdf22014-04-11 16:13:54 +000048 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(FLAGS_tile), SkIntToScalar(FLAGS_tile)));
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000049
commit-bot@chromium.org8400b232014-04-28 15:30:02 +000050 BenchTimer timer;
51 timer.start();
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000052 for (int i = 0; i < FLAGS_loops; i++) {
53 if (FLAGS_skr) {
54 SkRecordDraw(record, canvas.get());
55 } else {
56 src.draw(canvas.get());
57 }
58 }
commit-bot@chromium.org8400b232014-04-28 15:30:02 +000059 timer.end();
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000060
commit-bot@chromium.org8400b232014-04-28 15:30:02 +000061 const double msPerLoop = timer.fCpu / (double)FLAGS_loops;
commit-bot@chromium.org172eb1b2014-04-28 19:41:17 +000062 printf("%f\t%s\n", scale_time(msPerLoop), name);
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000063}
64
65int tool_main(int argc, char** argv);
66int tool_main(int argc, char** argv) {
67 SkCommandLineFlags::Parse(argc, argv);
68 SkAutoGraphics autoGraphics;
69
70 // We share a single scratch bitmap among benches to reduce the profile noise from allocation.
71 static const int kMaxArea = 209825221; // tabl_mozilla is this big.
72 SkAutoTMalloc<SkPMColor> scratch(kMaxArea);
73
74 SkOSFile::Iter it(FLAGS_skps[0], ".skp");
75 SkString filename;
76 bool failed = false;
77 while (it.next(&filename)) {
commit-bot@chromium.org5da5b592014-04-21 14:59:59 +000078 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, filename.c_str())) {
79 continue;
80 }
81
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000082 const SkString path = SkOSPath::SkPathJoin(FLAGS_skps[0], filename.c_str());
83
84 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path.c_str()));
85 if (!stream) {
86 SkDebugf("Could not read %s.\n", path.c_str());
87 failed = true;
88 continue;
89 }
90 SkAutoTUnref<SkPicture> src(SkPicture::CreateFromStream(stream));
91 if (!src) {
92 SkDebugf("Could not read %s as an SkPicture.\n", path.c_str());
93 failed = true;
94 continue;
95 }
96
97 if (src->width() * src->height() > kMaxArea) {
98 SkDebugf("%s (%dx%d) is larger than hardcoded scratch bitmap (%dpx).\n",
99 path.c_str(), src->width(), src->height(), kMaxArea);
100 failed = true;
101 continue;
102 }
103
104 bench(scratch.get(), *src, filename.c_str());
105 }
106 return failed ? 1 : 0;
107}
108
109#if !defined SK_BUILD_FOR_IOS
110int main(int argc, char * const argv[]) {
111 return tool_main(argc, (char**) argv);
112}
113#endif