blob: a5dfe50973a211abf29e39640be93e245bc9cb3f [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"
commit-bot@chromium.orgad8ce572014-04-21 15:03:36 +000014#include "SkRecordOpts.h"
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000015#include "SkRecorder.h"
16#include "SkStream.h"
17#include "SkString.h"
18#include "SkTime.h"
19
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.orgd0306a12014-04-24 20:17:24 +000043 const SkMSec start = SkTime::GetMSecs();
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000044 for (int i = 0; i < FLAGS_loops; i++) {
45 if (FLAGS_skr) {
46 SkRecordDraw(record, canvas.get());
47 } else {
48 src.draw(canvas.get());
49 }
50 }
51
commit-bot@chromium.orgd0306a12014-04-24 20:17:24 +000052 const SkMSec elapsed = SkTime::GetMSecs() - start;
53 const double msPerLoop = elapsed / (double)FLAGS_loops;
54 printf("%6.2f\t%s\n", msPerLoop, name);
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000055}
56
57int tool_main(int argc, char** argv);
58int tool_main(int argc, char** argv) {
59 SkCommandLineFlags::Parse(argc, argv);
60 SkAutoGraphics autoGraphics;
61
62 // We share a single scratch bitmap among benches to reduce the profile noise from allocation.
63 static const int kMaxArea = 209825221; // tabl_mozilla is this big.
64 SkAutoTMalloc<SkPMColor> scratch(kMaxArea);
65
66 SkOSFile::Iter it(FLAGS_skps[0], ".skp");
67 SkString filename;
68 bool failed = false;
69 while (it.next(&filename)) {
commit-bot@chromium.org5da5b592014-04-21 14:59:59 +000070 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, filename.c_str())) {
71 continue;
72 }
73
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000074 const SkString path = SkOSPath::SkPathJoin(FLAGS_skps[0], filename.c_str());
75
76 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path.c_str()));
77 if (!stream) {
78 SkDebugf("Could not read %s.\n", path.c_str());
79 failed = true;
80 continue;
81 }
82 SkAutoTUnref<SkPicture> src(SkPicture::CreateFromStream(stream));
83 if (!src) {
84 SkDebugf("Could not read %s as an SkPicture.\n", path.c_str());
85 failed = true;
86 continue;
87 }
88
89 if (src->width() * src->height() > kMaxArea) {
90 SkDebugf("%s (%dx%d) is larger than hardcoded scratch bitmap (%dpx).\n",
91 path.c_str(), src->width(), src->height(), kMaxArea);
92 failed = true;
93 continue;
94 }
95
96 bench(scratch.get(), *src, filename.c_str());
97 }
98 return failed ? 1 : 0;
99}
100
101#if !defined SK_BUILD_FOR_IOS
102int main(int argc, char * const argv[]) {
103 return tool_main(argc, (char**) argv);
104}
105#endif