blob: 49bafbfceece254481e2a8a92e1196ba7656baa8 [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"
joshualitt98d2e2f2015-10-05 07:23:30 -070013#include "SkPath.h"
joshualitt962cc982015-06-30 07:43:14 -070014#include "SkPictureRecorder.h"
15#include "SkStream.h"
joshualitt98d2e2f2015-10-05 07:23:30 -070016#include "sk_tool_utils.h"
joshualitt962cc982015-06-30 07:43:14 -070017#include "VisualSKPBench.h"
18
mtkleinc6f5cac2015-10-01 11:56:56 -070019DEFINE_bool(cpu, false, "Run in CPU mode?");
halcanary96fcdcc2015-08-27 07:41:13 -070020DEFINE_string2(match, m, nullptr,
joshualitt962cc982015-06-30 07:43:14 -070021 "[~][^]substring[$] [...] of bench name to run.\n"
22 "Multiple matches may be separated by spaces.\n"
23 "~ causes a matching bench to always be skipped\n"
24 "^ requires the start of the bench to match\n"
25 "$ requires the end of the bench to match\n"
26 "^ and $ requires an exact match\n"
27 "If a bench does not match any list entry,\n"
28 "it is skipped unless some list entry starts with ~");
29DEFINE_string(skps, "skps", "Directory to read skps from.");
30
joshualitt98d2e2f2015-10-05 07:23:30 -070031// We draw a big nonAA path to warmup the gpu / cpu
32#include "SkPerlinNoiseShader.h"
33class WarmupBench : public Benchmark {
34public:
35 WarmupBench() {
36 sk_tool_utils::make_big_path(fPath);
37 }
38private:
39 const char* onGetName() override { return "warmupbench"; }
40 void onDraw(int loops, SkCanvas* canvas) override {
41 // We draw a big path to warm up the cpu, and then use perlin noise shader to warm up the
42 // gpu
43 SkPaint paint;
44 paint.setStyle(SkPaint::kStroke_Style);
45 paint.setStrokeWidth(2);
46
47 SkPaint perlinPaint;
48 perlinPaint.setShader(SkPerlinNoiseShader::CreateTurbulence(0.1f, 0.1f, 1, 0,
49 nullptr))->unref();
50 SkRect rect = SkRect::MakeLTRB(0., 0., 400., 400.);
51 for (int i = 0; i < loops; i++) {
52 canvas->drawPath(fPath, paint);
53 canvas->drawRect(rect, perlinPaint);
54 }
55 }
56 SkPath fPath;
57};
58
joshualitt962cc982015-06-30 07:43:14 -070059VisualBenchmarkStream::VisualBenchmarkStream()
60 : fBenches(BenchRegistry::Head())
61 , fGMs(skiagm::GMRegistry::Head())
halcanary96fcdcc2015-08-27 07:41:13 -070062 , fSourceType(nullptr)
63 , fBenchType(nullptr)
joshualitt98d2e2f2015-10-05 07:23:30 -070064 , fCurrentSKP(0)
65 , fIsWarmedUp(false) {
joshualitt962cc982015-06-30 07:43:14 -070066 for (int i = 0; i < FLAGS_skps.count(); i++) {
67 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
68 fSKPs.push_back() = FLAGS_skps[i];
69 } else {
70 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
71 SkString path;
72 while (it.next(&path)) {
73 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
74 }
75 }
76 }
joshualitt691b6af2015-10-14 08:04:22 -070077
78 // seed with an initial benchmark
79 this->next();
joshualitt962cc982015-06-30 07:43:14 -070080}
81
82bool VisualBenchmarkStream::ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
83 // Not strictly necessary, as it will be checked again later,
84 // but helps to avoid a lot of pointless work if we're going to skip it.
85 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
86 return false;
87 }
88
89 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -070090 if (stream.get() == nullptr) {
joshualitt962cc982015-06-30 07:43:14 -070091 SkDebugf("Could not read %s.\n", path);
92 return false;
93 }
94
95 pic->reset(SkPicture::CreateFromStream(stream.get()));
halcanary96fcdcc2015-08-27 07:41:13 -070096 if (pic->get() == nullptr) {
joshualitt962cc982015-06-30 07:43:14 -070097 SkDebugf("Could not read %s as an SkPicture.\n", path);
98 return false;
99 }
100 return true;
101}
102
103Benchmark* VisualBenchmarkStream::next() {
joshualitt691b6af2015-10-14 08:04:22 -0700104 Benchmark* bench;
joshualitt98d2e2f2015-10-05 07:23:30 -0700105 if (!fIsWarmedUp) {
106 fIsWarmedUp = true;
joshualitt691b6af2015-10-14 08:04:22 -0700107 bench = new WarmupBench;
108 } else {
109 // skips non matching benches
110 while ((bench = this->innerNext()) &&
111 (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName()) ||
112 !bench->isSuitableFor(Benchmark::kGPU_Backend))) {
113 bench->unref();
114 }
115 }
116 if (bench && FLAGS_cpu) {
117 bench = new CpuWrappedBenchmark(bench);
joshualitt98d2e2f2015-10-05 07:23:30 -0700118 }
119
joshualitt691b6af2015-10-14 08:04:22 -0700120 fBenchmark.reset(bench);
121 return fBenchmark;
joshualitt82874f82015-07-15 08:38:02 -0700122}
123
124Benchmark* VisualBenchmarkStream::innerNext() {
joshualitt962cc982015-06-30 07:43:14 -0700125 while (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700126 Benchmark* bench = fBenches->factory()(nullptr);
joshualitt962cc982015-06-30 07:43:14 -0700127 fBenches = fBenches->next();
128 if (bench->isVisual()) {
129 fSourceType = "bench";
130 fBenchType = "micro";
131 return bench;
132 }
joshualitt47d280d2015-07-16 14:23:22 -0700133 bench->unref();
joshualitt962cc982015-06-30 07:43:14 -0700134 }
135
136 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -0700137 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
joshualitt962cc982015-06-30 07:43:14 -0700138 fGMs = fGMs->next();
139 if (gm->runAsBench()) {
140 fSourceType = "gm";
141 fBenchType = "micro";
halcanary385fe4d2015-08-26 13:07:48 -0700142 return new GMBench(gm.detach());
joshualitt962cc982015-06-30 07:43:14 -0700143 }
144 }
145
146 // Render skps
147 while (fCurrentSKP < fSKPs.count()) {
148 const SkString& path = fSKPs[fCurrentSKP++];
149 SkAutoTUnref<SkPicture> pic;
150 if (!ReadPicture(path.c_str(), &pic)) {
151 continue;
152 }
153
154 SkString name = SkOSPath::Basename(path.c_str());
155 fSourceType = "skp";
156 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700157 return new VisualSKPBench(name.c_str(), pic.get());
joshualitt962cc982015-06-30 07:43:14 -0700158 }
159
halcanary96fcdcc2015-08-27 07:41:13 -0700160 return nullptr;
joshualitt962cc982015-06-30 07:43:14 -0700161}