blob: 4cc819ded5e0f252ae2df288f04023f04ffec4e8 [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>
10#include "GMBench.h"
11#include "SkOSFile.h"
12#include "SkPictureRecorder.h"
13#include "SkStream.h"
14#include "VisualSKPBench.h"
15
halcanary96fcdcc2015-08-27 07:41:13 -070016DEFINE_string2(match, m, nullptr,
joshualitt962cc982015-06-30 07:43:14 -070017 "[~][^]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 ~");
25DEFINE_string(skps, "skps", "Directory to read skps from.");
26
27VisualBenchmarkStream::VisualBenchmarkStream()
28 : fBenches(BenchRegistry::Head())
29 , fGMs(skiagm::GMRegistry::Head())
halcanary96fcdcc2015-08-27 07:41:13 -070030 , fSourceType(nullptr)
31 , fBenchType(nullptr)
joshualitt962cc982015-06-30 07:43:14 -070032 , 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
46bool 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));
halcanary96fcdcc2015-08-27 07:41:13 -070054 if (stream.get() == nullptr) {
joshualitt962cc982015-06-30 07:43:14 -070055 SkDebugf("Could not read %s.\n", path);
56 return false;
57 }
58
59 pic->reset(SkPicture::CreateFromStream(stream.get()));
halcanary96fcdcc2015-08-27 07:41:13 -070060 if (pic->get() == nullptr) {
joshualitt962cc982015-06-30 07:43:14 -070061 SkDebugf("Could not read %s as an SkPicture.\n", path);
62 return false;
63 }
64 return true;
65}
66
67Benchmark* VisualBenchmarkStream::next() {
joshualitt82874f82015-07-15 08:38:02 -070068 Benchmark* bench;
69 // skips non matching benches
70 while ((bench = this->innerNext()) &&
joshualitt47d280d2015-07-16 14:23:22 -070071 (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName()) ||
72 !bench->isSuitableFor(Benchmark::kGPU_Backend))) {
73 bench->unref();
joshualitt82874f82015-07-15 08:38:02 -070074 }
75 return bench;
76}
77
78Benchmark* VisualBenchmarkStream::innerNext() {
joshualitt962cc982015-06-30 07:43:14 -070079 while (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -070080 Benchmark* bench = fBenches->factory()(nullptr);
joshualitt962cc982015-06-30 07:43:14 -070081 fBenches = fBenches->next();
82 if (bench->isVisual()) {
83 fSourceType = "bench";
84 fBenchType = "micro";
85 return bench;
86 }
joshualitt47d280d2015-07-16 14:23:22 -070087 bench->unref();
joshualitt962cc982015-06-30 07:43:14 -070088 }
89
90 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -070091 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
joshualitt962cc982015-06-30 07:43:14 -070092 fGMs = fGMs->next();
93 if (gm->runAsBench()) {
94 fSourceType = "gm";
95 fBenchType = "micro";
halcanary385fe4d2015-08-26 13:07:48 -070096 return new GMBench(gm.detach());
joshualitt962cc982015-06-30 07:43:14 -070097 }
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";
halcanary385fe4d2015-08-26 13:07:48 -0700111 return new VisualSKPBench(name.c_str(), pic.get());
joshualitt962cc982015-06-30 07:43:14 -0700112 }
113
halcanary96fcdcc2015-08-27 07:41:13 -0700114 return nullptr;
joshualitt962cc982015-06-30 07:43:14 -0700115}