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" |
| 13 | #include "SkPictureRecorder.h" |
| 14 | #include "SkStream.h" |
| 15 | #include "VisualSKPBench.h" |
| 16 | |
mtklein | c6f5cac | 2015-10-01 11:56:56 -0700 | [diff] [blame^] | 17 | DEFINE_bool(cpu, false, "Run in CPU mode?"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 18 | DEFINE_string2(match, m, nullptr, |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 19 | "[~][^]substring[$] [...] of bench name to run.\n" |
| 20 | "Multiple matches may be separated by spaces.\n" |
| 21 | "~ causes a matching bench to always be skipped\n" |
| 22 | "^ requires the start of the bench to match\n" |
| 23 | "$ requires the end of the bench to match\n" |
| 24 | "^ and $ requires an exact match\n" |
| 25 | "If a bench does not match any list entry,\n" |
| 26 | "it is skipped unless some list entry starts with ~"); |
| 27 | DEFINE_string(skps, "skps", "Directory to read skps from."); |
| 28 | |
| 29 | VisualBenchmarkStream::VisualBenchmarkStream() |
| 30 | : fBenches(BenchRegistry::Head()) |
| 31 | , fGMs(skiagm::GMRegistry::Head()) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 32 | , fSourceType(nullptr) |
| 33 | , fBenchType(nullptr) |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 34 | , fCurrentSKP(0) { |
| 35 | for (int i = 0; i < FLAGS_skps.count(); i++) { |
| 36 | if (SkStrEndsWith(FLAGS_skps[i], ".skp")) { |
| 37 | fSKPs.push_back() = FLAGS_skps[i]; |
| 38 | } else { |
| 39 | SkOSFile::Iter it(FLAGS_skps[i], ".skp"); |
| 40 | SkString path; |
| 41 | while (it.next(&path)) { |
| 42 | fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str()); |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | bool VisualBenchmarkStream::ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) { |
| 49 | // Not strictly necessary, as it will be checked again later, |
| 50 | // but helps to avoid a lot of pointless work if we're going to skip it. |
| 51 | if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) { |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 56 | if (stream.get() == nullptr) { |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 57 | SkDebugf("Could not read %s.\n", path); |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | pic->reset(SkPicture::CreateFromStream(stream.get())); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 62 | if (pic->get() == nullptr) { |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 63 | SkDebugf("Could not read %s as an SkPicture.\n", path); |
| 64 | return false; |
| 65 | } |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | Benchmark* VisualBenchmarkStream::next() { |
joshualitt | 82874f8 | 2015-07-15 08:38:02 -0700 | [diff] [blame] | 70 | Benchmark* bench; |
| 71 | // skips non matching benches |
| 72 | while ((bench = this->innerNext()) && |
joshualitt | 47d280d | 2015-07-16 14:23:22 -0700 | [diff] [blame] | 73 | (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName()) || |
| 74 | !bench->isSuitableFor(Benchmark::kGPU_Backend))) { |
| 75 | bench->unref(); |
joshualitt | 82874f8 | 2015-07-15 08:38:02 -0700 | [diff] [blame] | 76 | } |
mtklein | c6f5cac | 2015-10-01 11:56:56 -0700 | [diff] [blame^] | 77 | if (FLAGS_cpu) { |
| 78 | return new CpuWrappedBenchmark(bench); |
| 79 | } |
joshualitt | 82874f8 | 2015-07-15 08:38:02 -0700 | [diff] [blame] | 80 | return bench; |
| 81 | } |
| 82 | |
| 83 | Benchmark* VisualBenchmarkStream::innerNext() { |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 84 | while (fBenches) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 85 | Benchmark* bench = fBenches->factory()(nullptr); |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 86 | fBenches = fBenches->next(); |
| 87 | if (bench->isVisual()) { |
| 88 | fSourceType = "bench"; |
| 89 | fBenchType = "micro"; |
| 90 | return bench; |
| 91 | } |
joshualitt | 47d280d | 2015-07-16 14:23:22 -0700 | [diff] [blame] | 92 | bench->unref(); |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | while (fGMs) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 96 | SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr)); |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 97 | fGMs = fGMs->next(); |
| 98 | if (gm->runAsBench()) { |
| 99 | fSourceType = "gm"; |
| 100 | fBenchType = "micro"; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 101 | return new GMBench(gm.detach()); |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | // Render skps |
| 106 | while (fCurrentSKP < fSKPs.count()) { |
| 107 | const SkString& path = fSKPs[fCurrentSKP++]; |
| 108 | SkAutoTUnref<SkPicture> pic; |
| 109 | if (!ReadPicture(path.c_str(), &pic)) { |
| 110 | continue; |
| 111 | } |
| 112 | |
| 113 | SkString name = SkOSPath::Basename(path.c_str()); |
| 114 | fSourceType = "skp"; |
| 115 | fBenchType = "playback"; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 116 | return new VisualSKPBench(name.c_str(), pic.get()); |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 117 | } |
| 118 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 119 | return nullptr; |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 120 | } |