blob: 9e1ce36fbc12ec189a101ba832b3ee0288c7c76f [file] [log] [blame]
joshualitt962cc982015-06-30 07:43:14 -07001/*
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>
mtkleinc6f5cac2015-10-01 11:56:56 -070010#include "CpuWrappedBenchmark.h"
joshualitt962cc982015-06-30 07:43:14 -070011#include "GMBench.h"
12#include "SkOSFile.h"
13#include "SkPictureRecorder.h"
14#include "SkStream.h"
15#include "VisualSKPBench.h"
16
mtkleinc6f5cac2015-10-01 11:56:56 -070017DEFINE_bool(cpu, false, "Run in CPU mode?");
halcanary96fcdcc2015-08-27 07:41:13 -070018DEFINE_string2(match, m, nullptr,
joshualitt962cc982015-06-30 07:43:14 -070019 "[~][^]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 ~");
27DEFINE_string(skps, "skps", "Directory to read skps from.");
28
29VisualBenchmarkStream::VisualBenchmarkStream()
30 : fBenches(BenchRegistry::Head())
31 , fGMs(skiagm::GMRegistry::Head())
halcanary96fcdcc2015-08-27 07:41:13 -070032 , fSourceType(nullptr)
33 , fBenchType(nullptr)
joshualitt962cc982015-06-30 07:43:14 -070034 , 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
48bool 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));
halcanary96fcdcc2015-08-27 07:41:13 -070056 if (stream.get() == nullptr) {
joshualitt962cc982015-06-30 07:43:14 -070057 SkDebugf("Could not read %s.\n", path);
58 return false;
59 }
60
61 pic->reset(SkPicture::CreateFromStream(stream.get()));
halcanary96fcdcc2015-08-27 07:41:13 -070062 if (pic->get() == nullptr) {
joshualitt962cc982015-06-30 07:43:14 -070063 SkDebugf("Could not read %s as an SkPicture.\n", path);
64 return false;
65 }
66 return true;
67}
68
69Benchmark* VisualBenchmarkStream::next() {
joshualitt82874f82015-07-15 08:38:02 -070070 Benchmark* bench;
71 // skips non matching benches
72 while ((bench = this->innerNext()) &&
joshualitt47d280d2015-07-16 14:23:22 -070073 (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName()) ||
74 !bench->isSuitableFor(Benchmark::kGPU_Backend))) {
75 bench->unref();
joshualitt82874f82015-07-15 08:38:02 -070076 }
mtkleinc6f5cac2015-10-01 11:56:56 -070077 if (FLAGS_cpu) {
78 return new CpuWrappedBenchmark(bench);
79 }
joshualitt82874f82015-07-15 08:38:02 -070080 return bench;
81}
82
83Benchmark* VisualBenchmarkStream::innerNext() {
joshualitt962cc982015-06-30 07:43:14 -070084 while (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -070085 Benchmark* bench = fBenches->factory()(nullptr);
joshualitt962cc982015-06-30 07:43:14 -070086 fBenches = fBenches->next();
87 if (bench->isVisual()) {
88 fSourceType = "bench";
89 fBenchType = "micro";
90 return bench;
91 }
joshualitt47d280d2015-07-16 14:23:22 -070092 bench->unref();
joshualitt962cc982015-06-30 07:43:14 -070093 }
94
95 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -070096 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
joshualitt962cc982015-06-30 07:43:14 -070097 fGMs = fGMs->next();
98 if (gm->runAsBench()) {
99 fSourceType = "gm";
100 fBenchType = "micro";
halcanary385fe4d2015-08-26 13:07:48 -0700101 return new GMBench(gm.detach());
joshualitt962cc982015-06-30 07:43:14 -0700102 }
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";
halcanary385fe4d2015-08-26 13:07:48 -0700116 return new VisualSKPBench(name.c_str(), pic.get());
joshualitt962cc982015-06-30 07:43:14 -0700117 }
118
halcanary96fcdcc2015-08-27 07:41:13 -0700119 return nullptr;
joshualitt962cc982015-06-30 07:43:14 -0700120}