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