joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
| 9 | #include <VisualBench/VisualBenchmarkStream.h> |
joshualitt | c9dd93c | 2015-10-15 09:49:31 -0700 | [diff] [blame] | 10 | #include <VisualBench/WrappedBenchmark.h> |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 11 | #include "GMBench.h" |
| 12 | #include "SkOSFile.h" |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 13 | #include "SkPath.h" |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 14 | #include "SkPictureRecorder.h" |
| 15 | #include "SkStream.h" |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 16 | #include "sk_tool_utils.h" |
joshualitt | c9dd93c | 2015-10-15 09:49:31 -0700 | [diff] [blame] | 17 | #include "VisualFlags.h" |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 18 | #include "VisualSKPBench.h" |
| 19 | |
mtklein | c6f5cac | 2015-10-01 11:56:56 -0700 | [diff] [blame] | 20 | DEFINE_bool(cpu, false, "Run in CPU mode?"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 21 | DEFINE_string2(match, m, nullptr, |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 22 | "[~][^]substring[$] [...] of bench name to run.\n" |
| 23 | "Multiple matches may be separated by spaces.\n" |
| 24 | "~ causes a matching bench to always be skipped\n" |
| 25 | "^ requires the start of the bench to match\n" |
| 26 | "$ requires the end of the bench to match\n" |
| 27 | "^ and $ requires an exact match\n" |
| 28 | "If a bench does not match any list entry,\n" |
| 29 | "it is skipped unless some list entry starts with ~"); |
| 30 | DEFINE_string(skps, "skps", "Directory to read skps from."); |
| 31 | |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 32 | // We draw a big nonAA path to warmup the gpu / cpu |
| 33 | #include "SkPerlinNoiseShader.h" |
| 34 | class WarmupBench : public Benchmark { |
| 35 | public: |
| 36 | WarmupBench() { |
| 37 | sk_tool_utils::make_big_path(fPath); |
| 38 | } |
| 39 | private: |
| 40 | const char* onGetName() override { return "warmupbench"; } |
| 41 | void onDraw(int loops, SkCanvas* canvas) override { |
| 42 | // We draw a big path to warm up the cpu, and then use perlin noise shader to warm up the |
| 43 | // gpu |
| 44 | SkPaint paint; |
| 45 | paint.setStyle(SkPaint::kStroke_Style); |
| 46 | paint.setStrokeWidth(2); |
| 47 | |
| 48 | SkPaint perlinPaint; |
| 49 | perlinPaint.setShader(SkPerlinNoiseShader::CreateTurbulence(0.1f, 0.1f, 1, 0, |
| 50 | nullptr))->unref(); |
| 51 | SkRect rect = SkRect::MakeLTRB(0., 0., 400., 400.); |
| 52 | for (int i = 0; i < loops; i++) { |
| 53 | canvas->drawPath(fPath, paint); |
| 54 | canvas->drawRect(rect, perlinPaint); |
| 55 | } |
| 56 | } |
| 57 | SkPath fPath; |
| 58 | }; |
| 59 | |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 60 | VisualBenchmarkStream::VisualBenchmarkStream() |
| 61 | : fBenches(BenchRegistry::Head()) |
| 62 | , fGMs(skiagm::GMRegistry::Head()) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 63 | , fSourceType(nullptr) |
| 64 | , fBenchType(nullptr) |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 65 | , fCurrentSKP(0) |
| 66 | , fIsWarmedUp(false) { |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 67 | for (int i = 0; i < FLAGS_skps.count(); i++) { |
| 68 | if (SkStrEndsWith(FLAGS_skps[i], ".skp")) { |
| 69 | fSKPs.push_back() = FLAGS_skps[i]; |
| 70 | } else { |
| 71 | SkOSFile::Iter it(FLAGS_skps[i], ".skp"); |
| 72 | SkString path; |
| 73 | while (it.next(&path)) { |
| 74 | fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str()); |
| 75 | } |
| 76 | } |
| 77 | } |
joshualitt | c603c14 | 2015-10-15 07:18:29 -0700 | [diff] [blame] | 78 | |
| 79 | // seed with an initial benchmark |
| 80 | // NOTE the initial benchmark will not have preTimingHooks called, but that is okay because |
| 81 | // it is the warmupbench |
| 82 | this->next(); |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | bool VisualBenchmarkStream::ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) { |
| 86 | // Not strictly necessary, as it will be checked again later, |
| 87 | // but helps to avoid a lot of pointless work if we're going to skip it. |
| 88 | if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 93 | if (stream.get() == nullptr) { |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 94 | SkDebugf("Could not read %s.\n", path); |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | pic->reset(SkPicture::CreateFromStream(stream.get())); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 99 | if (pic->get() == nullptr) { |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 100 | SkDebugf("Could not read %s as an SkPicture.\n", path); |
| 101 | return false; |
| 102 | } |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | Benchmark* VisualBenchmarkStream::next() { |
joshualitt | c603c14 | 2015-10-15 07:18:29 -0700 | [diff] [blame] | 107 | Benchmark* bench; |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 108 | if (!fIsWarmedUp) { |
| 109 | fIsWarmedUp = true; |
joshualitt | c603c14 | 2015-10-15 07:18:29 -0700 | [diff] [blame] | 110 | bench = new WarmupBench; |
| 111 | } else { |
| 112 | // skips non matching benches |
| 113 | while ((bench = this->innerNext()) && |
| 114 | (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName()) || |
| 115 | !bench->isSuitableFor(Benchmark::kGPU_Backend))) { |
| 116 | bench->unref(); |
| 117 | } |
| 118 | } |
joshualitt | c9dd93c | 2015-10-15 09:49:31 -0700 | [diff] [blame] | 119 | |
| 120 | // TODO move this all to --config |
joshualitt | c603c14 | 2015-10-15 07:18:29 -0700 | [diff] [blame] | 121 | if (bench && FLAGS_cpu) { |
| 122 | bench = new CpuWrappedBenchmark(bench); |
joshualitt | c9dd93c | 2015-10-15 09:49:31 -0700 | [diff] [blame] | 123 | } else if (bench && FLAGS_nvpr) { |
| 124 | bench = new NvprWrappedBenchmark(bench, 4); |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 125 | } |
| 126 | |
joshualitt | c603c14 | 2015-10-15 07:18:29 -0700 | [diff] [blame] | 127 | fBenchmark.reset(bench); |
| 128 | return fBenchmark; |
joshualitt | 82874f8 | 2015-07-15 08:38:02 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | Benchmark* VisualBenchmarkStream::innerNext() { |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 132 | while (fBenches) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 133 | Benchmark* bench = fBenches->factory()(nullptr); |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 134 | fBenches = fBenches->next(); |
| 135 | if (bench->isVisual()) { |
| 136 | fSourceType = "bench"; |
| 137 | fBenchType = "micro"; |
| 138 | return bench; |
| 139 | } |
joshualitt | 47d280d | 2015-07-16 14:23:22 -0700 | [diff] [blame] | 140 | bench->unref(); |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | while (fGMs) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 144 | SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr)); |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 145 | fGMs = fGMs->next(); |
| 146 | if (gm->runAsBench()) { |
| 147 | fSourceType = "gm"; |
| 148 | fBenchType = "micro"; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 149 | return new GMBench(gm.detach()); |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
| 153 | // Render skps |
| 154 | while (fCurrentSKP < fSKPs.count()) { |
| 155 | const SkString& path = fSKPs[fCurrentSKP++]; |
| 156 | SkAutoTUnref<SkPicture> pic; |
| 157 | if (!ReadPicture(path.c_str(), &pic)) { |
| 158 | continue; |
| 159 | } |
| 160 | |
| 161 | SkString name = SkOSPath::Basename(path.c_str()); |
| 162 | fSourceType = "skp"; |
| 163 | fBenchType = "playback"; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 164 | return new VisualSKPBench(name.c_str(), pic.get()); |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 165 | } |
| 166 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 167 | return nullptr; |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 168 | } |