blob: 283ba6ca579bab638129793dcec42fc57c695e54 [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
robertphillips@google.com84b18c72014-04-13 19:09:42 +000036typedef SkPictureFactory* (*PictureFactory)(int* recordingFlags);
commit-bot@chromium.org450d9ef2014-03-03 17:51:12 +000037
robertphillips@google.com84b18c72014-04-13 19:09:42 +000038static SkPictureFactory* vanilla_factory(int* recordingFlags) {
39 return NULL;
commit-bot@chromium.org450d9ef2014-03-03 17:51:12 +000040}
41
robertphillips@google.com84b18c72014-04-13 19:09:42 +000042static SkPictureFactory* rtree_factory(int* recordingFlags) {
commit-bot@chromium.org450d9ef2014-03-03 17:51:12 +000043 *recordingFlags |= SkPicture::kOptimizeForClippedPlayback_RecordingFlag;
robertphillips@google.com84b18c72014-04-13 19:09:42 +000044 return NULL;
commit-bot@chromium.org450d9ef2014-03-03 17:51:12 +000045}
46
robertphillips@google.com84b18c72014-04-13 19:09:42 +000047static SkPictureFactory* tilegrid_factory(int* recordingFlags) {
commit-bot@chromium.org450d9ef2014-03-03 17:51:12 +000048 *recordingFlags |= SkPicture::kOptimizeForClippedPlayback_RecordingFlag;
49 SkTileGridPicture::TileGridInfo info;
50 info.fTileInterval.set(FLAGS_tileGridSize, FLAGS_tileGridSize);
51 info.fMargin.setEmpty();
52 info.fOffset.setZero();
robertphillips@google.com84b18c72014-04-13 19:09:42 +000053 return SkNEW_ARGS(SkTileGridPictureFactory, (info));
commit-bot@chromium.org450d9ef2014-03-03 17:51:12 +000054}
55
robertphillips@google.com84b18c72014-04-13 19:09:42 +000056static SkPictureFactory* quadtree_factory(int* recordingFlags) {
commit-bot@chromium.org450d9ef2014-03-03 17:51:12 +000057 *recordingFlags |= SkPicture::kOptimizeForClippedPlayback_RecordingFlag;
robertphillips@google.com84b18c72014-04-13 19:09:42 +000058 return SkNEW(SkQuadTreePictureFactory);
commit-bot@chromium.org450d9ef2014-03-03 17:51:12 +000059}
60
61static PictureFactory parse_FLAGS_bbh() {
robertphillips@google.com84b18c72014-04-13 19:09:42 +000062 if (FLAGS_bbh.isEmpty()) {
63 return &vanilla_factory;
64 }
commit-bot@chromium.org450d9ef2014-03-03 17:51:12 +000065 if (FLAGS_bbh.count() != 1) {
66 SkDebugf("Multiple bbh arguments supplied.\n");
67 return NULL;
68 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +000069 if (FLAGS_bbh.contains("rtree")) {
70 return rtree_factory;
71 }
72 if (FLAGS_bbh.contains("tilegrid")) {
73 return tilegrid_factory;
74 }
75 if (FLAGS_bbh.contains("quadtree")) {
76 return quadtree_factory;
77 }
commit-bot@chromium.org450d9ef2014-03-03 17:51:12 +000078 SkDebugf("Invalid bbh type %s, must be one of rtree, tilegrid, quadtree.\n", FLAGS_bbh[0]);
79 return NULL;
80}
81
82static void bench_record(SkPicture* src, const char* name, PictureFactory pictureFactory) {
commit-bot@chromium.orga8582232014-01-13 19:09:36 +000083 const SkMSec start = SkTime::GetMSecs();
84 const int width = src ? src->width() : FLAGS_nullSize;
85 const int height = src ? src->height() : FLAGS_nullSize;
86
87 for (int i = 0; i < FLAGS_loops; i++) {
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000088 if (FLAGS_skr) {
89 SkRecord record;
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000090 SkRecorder canvas(SkRecorder::kWriteOnly_Mode, &record, width, height);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000091 if (NULL != src) {
92 src->draw(&canvas);
93 }
94 } else {
95 int recordingFlags = FLAGS_flags;
robertphillips@google.com84b18c72014-04-13 19:09:42 +000096 SkAutoTUnref<SkPictureFactory> factory(pictureFactory(&recordingFlags));
97 SkPictureRecorder recorder(factory);
98 SkCanvas* canvas = recorder.beginRecording(width, height, recordingFlags);
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +000099 if (NULL != src) {
100 src->draw(canvas);
101 }
102 if (FLAGS_endRecording) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000103 SkAutoTUnref<SkPicture> dst(recorder.endRecording());
commit-bot@chromium.orge3ff5582014-04-01 16:24:06 +0000104 }
commit-bot@chromium.org70512af2014-03-18 17:45:32 +0000105 }
commit-bot@chromium.orga8582232014-01-13 19:09:36 +0000106 }
107
108 const SkMSec elapsed = SkTime::GetMSecs() - start;
109 const double msPerLoop = elapsed / (double)FLAGS_loops;
110 printf("%.2g\t%s\n", msPerLoop, name);
111}
commit-bot@chromium.org15ac3222014-01-13 12:03:47 +0000112
113int tool_main(int argc, char** argv);
114int tool_main(int argc, char** argv) {
115 SkCommandLineFlags::Parse(argc, argv);
116 SkAutoGraphics autoGraphics;
117
commit-bot@chromium.org450d9ef2014-03-03 17:51:12 +0000118 PictureFactory pictureFactory = parse_FLAGS_bbh();
119 if (pictureFactory == NULL) {
120 return 1;
121 }
122 bench_record(NULL, "NULL", pictureFactory);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +0000123 if (FLAGS_skps.isEmpty()) {
124 return 0;
125 }
commit-bot@chromium.orga8582232014-01-13 19:09:36 +0000126
commit-bot@chromium.org15ac3222014-01-13 12:03:47 +0000127 SkOSFile::Iter it(FLAGS_skps[0], ".skp");
128 SkString filename;
commit-bot@chromium.orga8582232014-01-13 19:09:36 +0000129 bool failed = false;
commit-bot@chromium.org15ac3222014-01-13 12:03:47 +0000130 while (it.next(&filename)) {
131 const SkString path = SkOSPath::SkPathJoin(FLAGS_skps[0], filename.c_str());
132
133 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path.c_str()));
134 if (!stream) {
135 SkDebugf("Could not read %s.\n", path.c_str());
commit-bot@chromium.orga8582232014-01-13 19:09:36 +0000136 failed = true;
commit-bot@chromium.org15ac3222014-01-13 12:03:47 +0000137 continue;
138 }
commit-bot@chromium.org16182f72014-03-28 16:08:18 +0000139 SkAutoTUnref<SkPicture> src(
140 SkPicture::CreateFromStream(stream, sk_tools::LazyDecodeBitmap));
commit-bot@chromium.org15ac3222014-01-13 12:03:47 +0000141 if (!src) {
142 SkDebugf("Could not read %s as an SkPicture.\n", path.c_str());
commit-bot@chromium.orga8582232014-01-13 19:09:36 +0000143 failed = true;
commit-bot@chromium.org15ac3222014-01-13 12:03:47 +0000144 continue;
145 }
commit-bot@chromium.org450d9ef2014-03-03 17:51:12 +0000146 bench_record(src, filename.c_str(), pictureFactory);
commit-bot@chromium.org15ac3222014-01-13 12:03:47 +0000147 }
commit-bot@chromium.orga8582232014-01-13 19:09:36 +0000148 return failed ? 1 : 0;
commit-bot@chromium.org15ac3222014-01-13 12:03:47 +0000149}
150
151#if !defined SK_BUILD_FOR_IOS
152int main(int argc, char * const argv[]) {
153 return tool_main(argc, (char**) argv);
154}
155#endif