blob: 95dd4820136a778845b10fefe027eb044aa117b0 [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.orgba73d282014-04-11 15:53:39 +000027
28static void bench(SkPMColor* scratch, SkPicture& src, const char* name) {
commit-bot@chromium.orgb17a24f2014-04-14 20:33:05 +000029 // We don't use the public SkRecording interface here because we need kWriteOnly_Mode.
30 // (We don't want SkPicturePlayback to be able to optimize playing into our SkRecord.)
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000031 SkRecord record;
32 SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, src.width(), src.height());
33 src.draw(&recorder);
34
commit-bot@chromium.orgad8ce572014-04-21 15:03:36 +000035 SkRecordOptimize(&record);
36
commit-bot@chromium.orgd6489c92014-04-11 19:06:46 +000037 SkAutoTDelete<SkCanvas> canvas(SkCanvas::NewRasterDirectN32(src.width(),
38 src.height(),
39 scratch,
40 src.width() * sizeof(SkPMColor)));
commit-bot@chromium.org4bffdf22014-04-11 16:13:54 +000041 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(FLAGS_tile), SkIntToScalar(FLAGS_tile)));
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000042
commit-bot@chromium.org8400b232014-04-28 15:30:02 +000043 BenchTimer timer;
44 timer.start();
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000045 for (int i = 0; i < FLAGS_loops; i++) {
46 if (FLAGS_skr) {
47 SkRecordDraw(record, canvas.get());
48 } else {
49 src.draw(canvas.get());
50 }
51 }
commit-bot@chromium.org8400b232014-04-28 15:30:02 +000052 timer.end();
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000053
commit-bot@chromium.org8400b232014-04-28 15:30:02 +000054 const double msPerLoop = timer.fCpu / (double)FLAGS_loops;
55 printf("%u\t%s\n", unsigned(1000 * msPerLoop), name);
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000056}
57
58int tool_main(int argc, char** argv);
59int tool_main(int argc, char** argv) {
60 SkCommandLineFlags::Parse(argc, argv);
61 SkAutoGraphics autoGraphics;
62
63 // We share a single scratch bitmap among benches to reduce the profile noise from allocation.
64 static const int kMaxArea = 209825221; // tabl_mozilla is this big.
65 SkAutoTMalloc<SkPMColor> scratch(kMaxArea);
66
67 SkOSFile::Iter it(FLAGS_skps[0], ".skp");
68 SkString filename;
69 bool failed = false;
70 while (it.next(&filename)) {
commit-bot@chromium.org5da5b592014-04-21 14:59:59 +000071 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, filename.c_str())) {
72 continue;
73 }
74
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000075 const SkString path = SkOSPath::SkPathJoin(FLAGS_skps[0], filename.c_str());
76
77 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path.c_str()));
78 if (!stream) {
79 SkDebugf("Could not read %s.\n", path.c_str());
80 failed = true;
81 continue;
82 }
83 SkAutoTUnref<SkPicture> src(SkPicture::CreateFromStream(stream));
84 if (!src) {
85 SkDebugf("Could not read %s as an SkPicture.\n", path.c_str());
86 failed = true;
87 continue;
88 }
89
90 if (src->width() * src->height() > kMaxArea) {
91 SkDebugf("%s (%dx%d) is larger than hardcoded scratch bitmap (%dpx).\n",
92 path.c_str(), src->width(), src->height(), kMaxArea);
93 failed = true;
94 continue;
95 }
96
97 bench(scratch.get(), *src, filename.c_str());
98 }
99 return failed ? 1 : 0;
100}
101
102#if !defined SK_BUILD_FOR_IOS
103int main(int argc, char * const argv[]) {
104 return tool_main(argc, (char**) argv);
105}
106#endif