blob: c520eeed05510f6197152a4844f8d2cc1f72d81a [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 }
77}
78
79bool VisualBenchmarkStream::ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
80 // Not strictly necessary, as it will be checked again later,
81 // but helps to avoid a lot of pointless work if we're going to skip it.
82 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
83 return false;
84 }
85
86 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -070087 if (stream.get() == nullptr) {
joshualitt962cc982015-06-30 07:43:14 -070088 SkDebugf("Could not read %s.\n", path);
89 return false;
90 }
91
92 pic->reset(SkPicture::CreateFromStream(stream.get()));
halcanary96fcdcc2015-08-27 07:41:13 -070093 if (pic->get() == nullptr) {
joshualitt962cc982015-06-30 07:43:14 -070094 SkDebugf("Could not read %s as an SkPicture.\n", path);
95 return false;
96 }
97 return true;
98}
99
100Benchmark* VisualBenchmarkStream::next() {
joshualitt98d2e2f2015-10-05 07:23:30 -0700101 if (!fIsWarmedUp) {
102 fIsWarmedUp = true;
103 return new WarmupBench;
104 }
105
joshualitt82874f82015-07-15 08:38:02 -0700106 Benchmark* bench;
joshualitt98d2e2f2015-10-05 07:23:30 -0700107
joshualitt82874f82015-07-15 08:38:02 -0700108 // skips non matching benches
109 while ((bench = this->innerNext()) &&
joshualitt47d280d2015-07-16 14:23:22 -0700110 (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName()) ||
111 !bench->isSuitableFor(Benchmark::kGPU_Backend))) {
112 bench->unref();
joshualitt82874f82015-07-15 08:38:02 -0700113 }
mtkleinc6f5cac2015-10-01 11:56:56 -0700114 if (FLAGS_cpu) {
115 return new CpuWrappedBenchmark(bench);
116 }
joshualitt82874f82015-07-15 08:38:02 -0700117 return bench;
118}
119
120Benchmark* VisualBenchmarkStream::innerNext() {
joshualitt962cc982015-06-30 07:43:14 -0700121 while (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700122 Benchmark* bench = fBenches->factory()(nullptr);
joshualitt962cc982015-06-30 07:43:14 -0700123 fBenches = fBenches->next();
124 if (bench->isVisual()) {
125 fSourceType = "bench";
126 fBenchType = "micro";
127 return bench;
128 }
joshualitt47d280d2015-07-16 14:23:22 -0700129 bench->unref();
joshualitt962cc982015-06-30 07:43:14 -0700130 }
131
132 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -0700133 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
joshualitt962cc982015-06-30 07:43:14 -0700134 fGMs = fGMs->next();
135 if (gm->runAsBench()) {
136 fSourceType = "gm";
137 fBenchType = "micro";
halcanary385fe4d2015-08-26 13:07:48 -0700138 return new GMBench(gm.detach());
joshualitt962cc982015-06-30 07:43:14 -0700139 }
140 }
141
142 // Render skps
143 while (fCurrentSKP < fSKPs.count()) {
144 const SkString& path = fSKPs[fCurrentSKP++];
145 SkAutoTUnref<SkPicture> pic;
146 if (!ReadPicture(path.c_str(), &pic)) {
147 continue;
148 }
149
150 SkString name = SkOSPath::Basename(path.c_str());
151 fSourceType = "skp";
152 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700153 return new VisualSKPBench(name.c_str(), pic.get());
joshualitt962cc982015-06-30 07:43:14 -0700154 }
155
halcanary96fcdcc2015-08-27 07:41:13 -0700156 return nullptr;
joshualitt962cc982015-06-30 07:43:14 -0700157}