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