blob: 3d0d4ef54b29826d6f0834a965e1a840d9d0ed4f [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"
13#include "SkRecordDraw.h"
14#include "SkRecorder.h"
15#include "SkStream.h"
16#include "SkString.h"
17#include "SkTime.h"
18
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.orgba73d282014-04-11 15:53:39 +000026
27static void bench(SkPMColor* scratch, SkPicture& src, const char* name) {
commit-bot@chromium.orgb17a24f2014-04-14 20:33:05 +000028 // We don't use the public SkRecording interface here because we need kWriteOnly_Mode.
29 // (We don't want SkPicturePlayback to be able to optimize playing into our SkRecord.)
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000030 SkRecord record;
31 SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, src.width(), src.height());
32 src.draw(&recorder);
33
commit-bot@chromium.orgd6489c92014-04-11 19:06:46 +000034 SkAutoTDelete<SkCanvas> canvas(SkCanvas::NewRasterDirectN32(src.width(),
35 src.height(),
36 scratch,
37 src.width() * sizeof(SkPMColor)));
commit-bot@chromium.org4bffdf22014-04-11 16:13:54 +000038 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(FLAGS_tile), SkIntToScalar(FLAGS_tile)));
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000039
40 const SkMSec start = SkTime::GetMSecs();
41 for (int i = 0; i < FLAGS_loops; i++) {
42 if (FLAGS_skr) {
43 SkRecordDraw(record, canvas.get());
44 } else {
45 src.draw(canvas.get());
46 }
47 }
48
49 const SkMSec elapsed = SkTime::GetMSecs() - start;
50 const double msPerLoop = elapsed / (double)FLAGS_loops;
commit-bot@chromium.org5da5b592014-04-21 14:59:59 +000051 printf("%6.2f\t%s\n", msPerLoop, name);
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000052}
53
54int tool_main(int argc, char** argv);
55int tool_main(int argc, char** argv) {
56 SkCommandLineFlags::Parse(argc, argv);
57 SkAutoGraphics autoGraphics;
58
59 // We share a single scratch bitmap among benches to reduce the profile noise from allocation.
60 static const int kMaxArea = 209825221; // tabl_mozilla is this big.
61 SkAutoTMalloc<SkPMColor> scratch(kMaxArea);
62
63 SkOSFile::Iter it(FLAGS_skps[0], ".skp");
64 SkString filename;
65 bool failed = false;
66 while (it.next(&filename)) {
commit-bot@chromium.org5da5b592014-04-21 14:59:59 +000067 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, filename.c_str())) {
68 continue;
69 }
70
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000071 const SkString path = SkOSPath::SkPathJoin(FLAGS_skps[0], filename.c_str());
72
73 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path.c_str()));
74 if (!stream) {
75 SkDebugf("Could not read %s.\n", path.c_str());
76 failed = true;
77 continue;
78 }
79 SkAutoTUnref<SkPicture> src(SkPicture::CreateFromStream(stream));
80 if (!src) {
81 SkDebugf("Could not read %s as an SkPicture.\n", path.c_str());
82 failed = true;
83 continue;
84 }
85
86 if (src->width() * src->height() > kMaxArea) {
87 SkDebugf("%s (%dx%d) is larger than hardcoded scratch bitmap (%dpx).\n",
88 path.c_str(), src->width(), src->height(), kMaxArea);
89 failed = true;
90 continue;
91 }
92
93 bench(scratch.get(), *src, filename.c_str());
94 }
95 return failed ? 1 : 0;
96}
97
98#if !defined SK_BUILD_FOR_IOS
99int main(int argc, char * const argv[]) {
100 return tool_main(argc, (char**) argv);
101}
102#endif