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