blob: e59744b1dc8d410f12976d2ee83f4410d87e7544 [file] [log] [blame]
commit-bot@chromium.org15ac3222014-01-13 12:03:47 +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"
commit-bot@chromium.org450d9ef2014-03-03 17:51:12 +000013#include "SkQuadTreePicture.h"
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000014#include "SkRecorder.h"
commit-bot@chromium.org15ac3222014-01-13 12:03:47 +000015#include "SkStream.h"
16#include "SkString.h"
commit-bot@chromium.orge8926152014-02-28 19:25:11 +000017#include "SkTileGridPicture.h"
commit-bot@chromium.org15ac3222014-01-13 12:03:47 +000018#include "SkTime.h"
commit-bot@chromium.org16182f72014-03-28 16:08:18 +000019#include "LazyDecodeBitmap.h"
commit-bot@chromium.org15ac3222014-01-13 12:03:47 +000020
21__SK_FORCE_IMAGE_DECODER_LINKING;
22
23// Just reading all the SKPs takes about 2 seconds for me, which is the same as about 100 loops of
24// rerecording all the SKPs. So we default to --loops=900, which makes ~90% of our time spent in
25// recording, and this should take ~20 seconds to run.
26
27DEFINE_string2(skps, r, "skps", "Directory containing SKPs to read and re-record.");
28DEFINE_int32(loops, 900, "Number of times to re-record each SKP.");
29DEFINE_int32(flags, SkPicture::kUsePathBoundsForClip_RecordingFlag, "RecordingFlags to use.");
30DEFINE_bool(endRecording, true, "If false, don't time SkPicture::endRecording()");
commit-bot@chromium.orga8582232014-01-13 19:09:36 +000031DEFINE_int32(nullSize, 1000, "Pretend dimension of null source picture.");
commit-bot@chromium.org450d9ef2014-03-03 17:51:12 +000032DEFINE_int32(tileGridSize, 512, "Set the tile grid size. Has no effect if bbh is not set to tilegrid.");
33DEFINE_string(bbh, "", "Turn on the bbh and select the type, one of rtree, tilegrid, quadtree");
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000034DEFINE_bool(skr, false, "Record SKR instead of SKP.");
commit-bot@chromium.orga8582232014-01-13 19:09:36 +000035
commit-bot@chromium.org450d9ef2014-03-03 17:51:12 +000036typedef SkPicture* (*PictureFactory)(const int width, const int height, int* recordingFlags);
37
38static SkPicture* vanilla_factory(const int width, const int height, int* recordingFlags) {
39 return SkNEW(SkPicture);
40}
41
42static SkPicture* rtree_factory(const int width, const int height, int* recordingFlags) {
43 *recordingFlags |= SkPicture::kOptimizeForClippedPlayback_RecordingFlag;
44 return SkNEW(SkPicture);
45}
46
47static SkPicture* tilegrid_factory(const int width, const int height, int* recordingFlags) {
48 *recordingFlags |= SkPicture::kOptimizeForClippedPlayback_RecordingFlag;
49 SkTileGridPicture::TileGridInfo info;
50 info.fTileInterval.set(FLAGS_tileGridSize, FLAGS_tileGridSize);
51 info.fMargin.setEmpty();
52 info.fOffset.setZero();
53 return SkNEW_ARGS(SkTileGridPicture, (width, height, info));
54}
55
56static SkPicture* quadtree_factory(const int width, const int height, int* recordingFlags) {
57 *recordingFlags |= SkPicture::kOptimizeForClippedPlayback_RecordingFlag;
58 return SkNEW_ARGS(SkQuadTreePicture, (SkIRect::MakeWH(width, height)));
59}
60
61static PictureFactory parse_FLAGS_bbh() {
62 if (FLAGS_bbh.isEmpty()) { return &vanilla_factory; }
63 if (FLAGS_bbh.count() != 1) {
64 SkDebugf("Multiple bbh arguments supplied.\n");
65 return NULL;
66 }
67 if (FLAGS_bbh.contains("rtree")) { return rtree_factory; }
68 if (FLAGS_bbh.contains("tilegrid")) { return tilegrid_factory; }
69 if (FLAGS_bbh.contains("quadtree")) { return quadtree_factory; }
70 SkDebugf("Invalid bbh type %s, must be one of rtree, tilegrid, quadtree.\n", FLAGS_bbh[0]);
71 return NULL;
72}
73
74static void bench_record(SkPicture* src, const char* name, PictureFactory pictureFactory) {
commit-bot@chromium.orga8582232014-01-13 19:09:36 +000075 const SkMSec start = SkTime::GetMSecs();
76 const int width = src ? src->width() : FLAGS_nullSize;
77 const int height = src ? src->height() : FLAGS_nullSize;
78
79 for (int i = 0; i < FLAGS_loops; i++) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000080 if (FLAGS_skr) {
81 SkRecord record;
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000082 SkRecorder canvas(SkRecorder::kWriteOnly_Mode, &record, width, height);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000083 if (NULL != src) {
84 src->draw(&canvas);
85 }
86 } else {
87 int recordingFlags = FLAGS_flags;
88 SkAutoTUnref<SkPicture> dst(pictureFactory(width, height, &recordingFlags));
89 SkCanvas* canvas = dst->beginRecording(width, height, recordingFlags);
90 if (NULL != src) {
91 src->draw(canvas);
92 }
93 if (FLAGS_endRecording) {
94 dst->endRecording();
95 }
commit-bot@chromium.org70512af2014-03-18 17:45:32 +000096 }
commit-bot@chromium.orga8582232014-01-13 19:09:36 +000097 }
98
99 const SkMSec elapsed = SkTime::GetMSecs() - start;
100 const double msPerLoop = elapsed / (double)FLAGS_loops;
101 printf("%.2g\t%s\n", msPerLoop, name);
102}
commit-bot@chromium.org15ac3222014-01-13 12:03:47 +0000103
104int tool_main(int argc, char** argv);
105int tool_main(int argc, char** argv) {
106 SkCommandLineFlags::Parse(argc, argv);
107 SkAutoGraphics autoGraphics;
108
commit-bot@chromium.org450d9ef2014-03-03 17:51:12 +0000109 PictureFactory pictureFactory = parse_FLAGS_bbh();
110 if (pictureFactory == NULL) {
111 return 1;
112 }
113 bench_record(NULL, "NULL", pictureFactory);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +0000114 if (FLAGS_skps.isEmpty()) {
115 return 0;
116 }
commit-bot@chromium.orga8582232014-01-13 19:09:36 +0000117
commit-bot@chromium.org15ac3222014-01-13 12:03:47 +0000118 SkOSFile::Iter it(FLAGS_skps[0], ".skp");
119 SkString filename;
commit-bot@chromium.orga8582232014-01-13 19:09:36 +0000120 bool failed = false;
commit-bot@chromium.org15ac3222014-01-13 12:03:47 +0000121 while (it.next(&filename)) {
122 const SkString path = SkOSPath::SkPathJoin(FLAGS_skps[0], filename.c_str());
123
124 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path.c_str()));
125 if (!stream) {
126 SkDebugf("Could not read %s.\n", path.c_str());
commit-bot@chromium.orga8582232014-01-13 19:09:36 +0000127 failed = true;
commit-bot@chromium.org15ac3222014-01-13 12:03:47 +0000128 continue;
129 }
commit-bot@chromium.org16182f72014-03-28 16:08:18 +0000130 SkAutoTUnref<SkPicture> src(
131 SkPicture::CreateFromStream(stream, sk_tools::LazyDecodeBitmap));
commit-bot@chromium.org15ac3222014-01-13 12:03:47 +0000132 if (!src) {
133 SkDebugf("Could not read %s as an SkPicture.\n", path.c_str());
commit-bot@chromium.orga8582232014-01-13 19:09:36 +0000134 failed = true;
commit-bot@chromium.org15ac3222014-01-13 12:03:47 +0000135 continue;
136 }
commit-bot@chromium.org450d9ef2014-03-03 17:51:12 +0000137 bench_record(src, filename.c_str(), pictureFactory);
commit-bot@chromium.org15ac3222014-01-13 12:03:47 +0000138 }
commit-bot@chromium.orga8582232014-01-13 19:09:36 +0000139 return failed ? 1 : 0;
commit-bot@chromium.org15ac3222014-01-13 12:03:47 +0000140}
141
142#if !defined SK_BUILD_FOR_IOS
143int main(int argc, char * const argv[]) {
144 return tool_main(argc, (char**) argv);
145}
146#endif