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