commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 1 | /* |
| 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" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 13 | #include "SkPictureRecorder.h" |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 14 | #include "SkStream.h" |
| 15 | #include "SkString.h" |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 16 | |
commit-bot@chromium.org | 16182f7 | 2014-03-28 16:08:18 +0000 | [diff] [blame] | 17 | #include "LazyDecodeBitmap.h" |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 18 | #include "Stats.h" |
mtklein | 9ac68ee | 2014-06-20 11:29:20 -0700 | [diff] [blame^] | 19 | #include "Timer.h" |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 20 | |
| 21 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 22 | |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 23 | DEFINE_string2(skps, r, "skps", "Directory containing SKPs to read and re-record."); |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 24 | DEFINE_int32(samples, 10, "Number of times to re-record each SKP."); |
commit-bot@chromium.org | 450d9ef | 2014-03-03 17:51:12 +0000 | [diff] [blame] | 25 | DEFINE_int32(tileGridSize, 512, "Set the tile grid size. Has no effect if bbh is not set to tilegrid."); |
| 26 | DEFINE_string(bbh, "", "Turn on the bbh and select the type, one of rtree, tilegrid, quadtree"); |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 27 | DEFINE_bool(skr, false, "Record SKR instead of SKP."); |
commit-bot@chromium.org | 5da5b59 | 2014-04-21 14:59:59 +0000 | [diff] [blame] | 28 | DEFINE_string(match, "", "The usual filters on file names of SKPs to bench."); |
commit-bot@chromium.org | 172eb1b | 2014-04-28 19:41:17 +0000 | [diff] [blame] | 29 | DEFINE_string(timescale, "us", "Print times in ms, us, or ns"); |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 30 | DEFINE_double(overheadGoal, 0.0001, |
| 31 | "Try to make timer overhead at most this fraction of our sample measurements."); |
| 32 | DEFINE_int32(verbose, 0, "0: print min sample; " |
| 33 | "1: print min, mean, max and noise indication " |
| 34 | "2: print all samples"); |
commit-bot@chromium.org | 172eb1b | 2014-04-28 19:41:17 +0000 | [diff] [blame] | 35 | |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 36 | static double timescale() { |
| 37 | if (FLAGS_timescale.contains("us")) return 1000; |
| 38 | if (FLAGS_timescale.contains("ns")) return 1000000; |
| 39 | return 1; |
commit-bot@chromium.org | 172eb1b | 2014-04-28 19:41:17 +0000 | [diff] [blame] | 40 | } |
commit-bot@chromium.org | a858223 | 2014-01-13 19:09:36 +0000 | [diff] [blame] | 41 | |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 42 | static SkBBHFactory* parse_FLAGS_bbh() { |
skia.committer@gmail.com | 5199701 | 2014-04-14 03:04:57 +0000 | [diff] [blame] | 43 | if (FLAGS_bbh.isEmpty()) { |
commit-bot@chromium.org | 450d9ef | 2014-03-03 17:51:12 +0000 | [diff] [blame] | 44 | return NULL; |
| 45 | } |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 46 | |
skia.committer@gmail.com | 5199701 | 2014-04-14 03:04:57 +0000 | [diff] [blame] | 47 | if (FLAGS_bbh.contains("rtree")) { |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 48 | return SkNEW(SkRTreeFactory); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 49 | } |
skia.committer@gmail.com | 5199701 | 2014-04-14 03:04:57 +0000 | [diff] [blame] | 50 | if (FLAGS_bbh.contains("tilegrid")) { |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 51 | SkTileGridFactory::TileGridInfo info; |
| 52 | info.fTileInterval.set(FLAGS_tileGridSize, FLAGS_tileGridSize); |
| 53 | info.fMargin.setEmpty(); |
| 54 | info.fOffset.setZero(); |
| 55 | return SkNEW_ARGS(SkTileGridFactory, (info)); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 56 | } |
skia.committer@gmail.com | 5199701 | 2014-04-14 03:04:57 +0000 | [diff] [blame] | 57 | if (FLAGS_bbh.contains("quadtree")) { |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 58 | return SkNEW(SkQuadTreeFactory); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 59 | } |
commit-bot@chromium.org | 450d9ef | 2014-03-03 17:51:12 +0000 | [diff] [blame] | 60 | SkDebugf("Invalid bbh type %s, must be one of rtree, tilegrid, quadtree.\n", FLAGS_bbh[0]); |
| 61 | return NULL; |
| 62 | } |
| 63 | |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 64 | static void rerecord(const SkPicture& src, SkBBHFactory* bbhFactory) { |
mtklein | 887f397 | 2014-06-17 12:08:15 -0700 | [diff] [blame] | 65 | SkPictureRecorder recorder; |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 66 | if (FLAGS_skr) { |
mtklein | 887f397 | 2014-06-17 12:08:15 -0700 | [diff] [blame] | 67 | src.draw(recorder.EXPERIMENTAL_beginRecording(src.width(), src.height(), bbhFactory)); |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 68 | } else { |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 69 | src.draw(recorder.beginRecording(src.width(), src.height(), bbhFactory)); |
commit-bot@chromium.org | a858223 | 2014-01-13 19:09:36 +0000 | [diff] [blame] | 70 | } |
mtklein | 887f397 | 2014-06-17 12:08:15 -0700 | [diff] [blame] | 71 | SkAutoTUnref<SkPicture> pic(recorder.endRecording()); |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 72 | } |
commit-bot@chromium.org | a858223 | 2014-01-13 19:09:36 +0000 | [diff] [blame] | 73 | |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 74 | static void bench_record(const SkPicture& src, |
| 75 | const double timerOverhead, |
| 76 | const char* name, |
| 77 | SkBBHFactory* bbhFactory) { |
| 78 | // Rerecord once to warm up any caches. Otherwise the first sample can be very noisy. |
| 79 | rerecord(src, bbhFactory); |
| 80 | |
| 81 | // Rerecord once to see how many times we should loop to make timer overhead insignificant. |
mtklein | 9ac68ee | 2014-06-20 11:29:20 -0700 | [diff] [blame^] | 82 | WallTimer timer; |
| 83 | const double scale = timescale(); |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 84 | do { |
mtklein | 9ac68ee | 2014-06-20 11:29:20 -0700 | [diff] [blame^] | 85 | timer.start(); |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 86 | rerecord(src, bbhFactory); |
| 87 | timer.end(); |
mtklein | 9ac68ee | 2014-06-20 11:29:20 -0700 | [diff] [blame^] | 88 | } while (timer.fWall * scale < timerOverhead); // Loop just in case something bizarre happens. |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 89 | |
| 90 | // We want (timer overhead / measurement) to be less than FLAGS_overheadGoal. |
| 91 | // So in each sample, we'll loop enough times to have made that true for our first measurement. |
| 92 | const int loops = (int)ceil(timerOverhead / timer.fWall / FLAGS_overheadGoal); |
| 93 | |
| 94 | SkAutoTMalloc<double> samples(FLAGS_samples); |
| 95 | for (int i = 0; i < FLAGS_samples; i++) { |
mtklein | 9ac68ee | 2014-06-20 11:29:20 -0700 | [diff] [blame^] | 96 | timer.start(); |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 97 | for (int j = 0; j < loops; j++) { |
| 98 | rerecord(src, bbhFactory); |
| 99 | } |
| 100 | timer.end(); |
mtklein | 9ac68ee | 2014-06-20 11:29:20 -0700 | [diff] [blame^] | 101 | samples[i] = timer.fWall * scale / loops; |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | Stats stats(samples.get(), FLAGS_samples); |
| 105 | if (FLAGS_verbose == 0) { |
| 106 | printf("%g\t%s\n", stats.min, name); |
| 107 | } else if (FLAGS_verbose == 1) { |
| 108 | // Get a rough idea of how noisy the measurements were. |
| 109 | const double noisePercent = 100 * sqrt(stats.var) / stats.mean; |
| 110 | printf("%g\t%g\t%g\t±%.0f%%\t%s\n", stats.min, stats.mean, stats.max, noisePercent, name); |
| 111 | } else if (FLAGS_verbose == 2) { |
| 112 | printf("%s", name); |
| 113 | for (int i = 0; i < FLAGS_samples; i++) { |
| 114 | printf("\t%g", samples[i]); |
| 115 | } |
| 116 | printf("\n"); |
| 117 | } |
commit-bot@chromium.org | a858223 | 2014-01-13 19:09:36 +0000 | [diff] [blame] | 118 | } |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 119 | |
| 120 | int tool_main(int argc, char** argv); |
| 121 | int tool_main(int argc, char** argv) { |
| 122 | SkCommandLineFlags::Parse(argc, argv); |
| 123 | SkAutoGraphics autoGraphics; |
| 124 | |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 125 | if (FLAGS_bbh.count() > 1) { |
| 126 | SkDebugf("Multiple bbh arguments supplied.\n"); |
commit-bot@chromium.org | 450d9ef | 2014-03-03 17:51:12 +0000 | [diff] [blame] | 127 | return 1; |
| 128 | } |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 129 | |
| 130 | SkAutoTDelete<SkBBHFactory> bbhFactory(parse_FLAGS_bbh()); |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 131 | |
| 132 | // Each run will use this timer overhead estimate to guess how many times it should run. |
| 133 | static const int kOverheadLoops = 10000000; |
mtklein | 9ac68ee | 2014-06-20 11:29:20 -0700 | [diff] [blame^] | 134 | WallTimer timer; |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 135 | double overheadEstimate = 0.0; |
mtklein | 9ac68ee | 2014-06-20 11:29:20 -0700 | [diff] [blame^] | 136 | const double scale = timescale(); |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 137 | for (int i = 0; i < kOverheadLoops; i++) { |
mtklein | 9ac68ee | 2014-06-20 11:29:20 -0700 | [diff] [blame^] | 138 | timer.start(); |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 139 | timer.end(); |
mtklein | 9ac68ee | 2014-06-20 11:29:20 -0700 | [diff] [blame^] | 140 | overheadEstimate += timer.fWall * scale; |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 141 | } |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 142 | overheadEstimate /= kOverheadLoops; |
commit-bot@chromium.org | a858223 | 2014-01-13 19:09:36 +0000 | [diff] [blame] | 143 | |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 144 | SkOSFile::Iter it(FLAGS_skps[0], ".skp"); |
| 145 | SkString filename; |
commit-bot@chromium.org | a858223 | 2014-01-13 19:09:36 +0000 | [diff] [blame] | 146 | bool failed = false; |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 147 | while (it.next(&filename)) { |
commit-bot@chromium.org | 5da5b59 | 2014-04-21 14:59:59 +0000 | [diff] [blame] | 148 | if (SkCommandLineFlags::ShouldSkip(FLAGS_match, filename.c_str())) { |
| 149 | continue; |
| 150 | } |
| 151 | |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 152 | const SkString path = SkOSPath::SkPathJoin(FLAGS_skps[0], filename.c_str()); |
| 153 | |
| 154 | SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path.c_str())); |
| 155 | if (!stream) { |
| 156 | SkDebugf("Could not read %s.\n", path.c_str()); |
commit-bot@chromium.org | a858223 | 2014-01-13 19:09:36 +0000 | [diff] [blame] | 157 | failed = true; |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 158 | continue; |
| 159 | } |
commit-bot@chromium.org | 16182f7 | 2014-03-28 16:08:18 +0000 | [diff] [blame] | 160 | SkAutoTUnref<SkPicture> src( |
| 161 | SkPicture::CreateFromStream(stream, sk_tools::LazyDecodeBitmap)); |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 162 | if (!src) { |
| 163 | SkDebugf("Could not read %s as an SkPicture.\n", path.c_str()); |
commit-bot@chromium.org | a858223 | 2014-01-13 19:09:36 +0000 | [diff] [blame] | 164 | failed = true; |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 165 | continue; |
| 166 | } |
mtklein | 90c471e | 2014-06-16 14:04:32 -0700 | [diff] [blame] | 167 | bench_record(*src, overheadEstimate, filename.c_str(), bbhFactory.get()); |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 168 | } |
commit-bot@chromium.org | a858223 | 2014-01-13 19:09:36 +0000 | [diff] [blame] | 169 | return failed ? 1 : 0; |
commit-bot@chromium.org | 15ac322 | 2014-01-13 12:03:47 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | #if !defined SK_BUILD_FOR_IOS |
| 173 | int main(int argc, char * const argv[]) { |
| 174 | return tool_main(argc, (char**) argv); |
| 175 | } |
| 176 | #endif |