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