blob: b5dc57bd1d78679325daf6a11fe31efb12915612 [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.");
25
26static void bench(SkPMColor* scratch, SkPicture& src, const char* name) {
27 SkRecord record;
28 SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, src.width(), src.height());
29 src.draw(&recorder);
30
commit-bot@chromium.orgd6489c92014-04-11 19:06:46 +000031 SkAutoTDelete<SkCanvas> canvas(SkCanvas::NewRasterDirectN32(src.width(),
32 src.height(),
33 scratch,
34 src.width() * sizeof(SkPMColor)));
commit-bot@chromium.org4bffdf22014-04-11 16:13:54 +000035 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(FLAGS_tile), SkIntToScalar(FLAGS_tile)));
commit-bot@chromium.orgba73d282014-04-11 15:53:39 +000036
37 const SkMSec start = SkTime::GetMSecs();
38 for (int i = 0; i < FLAGS_loops; i++) {
39 if (FLAGS_skr) {
40 SkRecordDraw(record, canvas.get());
41 } else {
42 src.draw(canvas.get());
43 }
44 }
45
46 const SkMSec elapsed = SkTime::GetMSecs() - start;
47 const double msPerLoop = elapsed / (double)FLAGS_loops;
48 printf("%5.1f\t%s\n", msPerLoop, name);
49}
50
51int tool_main(int argc, char** argv);
52int tool_main(int argc, char** argv) {
53 SkCommandLineFlags::Parse(argc, argv);
54 SkAutoGraphics autoGraphics;
55
56 // We share a single scratch bitmap among benches to reduce the profile noise from allocation.
57 static const int kMaxArea = 209825221; // tabl_mozilla is this big.
58 SkAutoTMalloc<SkPMColor> scratch(kMaxArea);
59
60 SkOSFile::Iter it(FLAGS_skps[0], ".skp");
61 SkString filename;
62 bool failed = false;
63 while (it.next(&filename)) {
64 const SkString path = SkOSPath::SkPathJoin(FLAGS_skps[0], filename.c_str());
65
66 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path.c_str()));
67 if (!stream) {
68 SkDebugf("Could not read %s.\n", path.c_str());
69 failed = true;
70 continue;
71 }
72 SkAutoTUnref<SkPicture> src(SkPicture::CreateFromStream(stream));
73 if (!src) {
74 SkDebugf("Could not read %s as an SkPicture.\n", path.c_str());
75 failed = true;
76 continue;
77 }
78
79 if (src->width() * src->height() > kMaxArea) {
80 SkDebugf("%s (%dx%d) is larger than hardcoded scratch bitmap (%dpx).\n",
81 path.c_str(), src->width(), src->height(), kMaxArea);
82 failed = true;
83 continue;
84 }
85
86 bench(scratch.get(), *src, filename.c_str());
87 }
88 return failed ? 1 : 0;
89}
90
91#if !defined SK_BUILD_FOR_IOS
92int main(int argc, char * const argv[]) {
93 return tool_main(argc, (char**) argv);
94}
95#endif