1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
/*
* Copyright 2017 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "Benchmark.h"
#include "SkData.h"
#include "SkOSFile.h"
#include "SkPicture.h"
#include "Timer.h"
#include "gm.h"
#include "ok.h"
#include <algorithm>
#include <chrono>
#include <limits>
#include <stdlib.h>
#include <vector>
struct GMStream : Stream {
const skiagm::GMRegistry* registry = skiagm::GMRegistry::Head();
static std::unique_ptr<Stream> Create(Options) {
GMStream stream;
return move_unique(stream);
}
struct GMSrc : Src {
skiagm::GM* (*factory)(void*);
std::unique_ptr<skiagm::GM> gm;
void init() {
if (gm) { return; }
gm.reset(factory(nullptr));
}
std::string name() override {
this->init();
return gm->getName();
}
SkISize size() override {
this->init();
return gm->getISize();
}
Status draw(SkCanvas* canvas) override {
this->init();
canvas->clear(0xffffffff);
gm->draw(canvas);
return Status::OK;
}
};
std::unique_ptr<Src> next() override {
if (!registry) {
return nullptr;
}
GMSrc src;
src.factory = registry->factory();
registry = registry->next();
return move_unique(src);
}
};
static Register gm{"gm", "draw GMs linked into this binary", GMStream::Create};
struct SKPStream : Stream {
std::string dir;
std::vector<std::string> skps;
static std::unique_ptr<Stream> Create(Options options) {
SKPStream stream;
stream.dir = options("dir", "skps");
SkOSFile::Iter it{stream.dir.c_str(), ".skp"};
for (SkString path; it.next(&path); ) {
stream.skps.push_back(path.c_str());
}
return move_unique(stream);
}
struct SKPSrc : Src {
std::string dir, path;
sk_sp<SkPicture> pic;
void init() {
if (pic) { return; }
auto skp = SkData::MakeFromFileName((dir+"/"+path).c_str());
pic = SkPicture::MakeFromData(skp.get());
}
std::string name() override {
return path;
}
SkISize size() override {
this->init();
return pic->cullRect().roundOut().size();
}
Status draw(SkCanvas* canvas) override {
this->init();
canvas->clear(0xffffffff);
pic->playback(canvas);
return Status::OK;
}
};
std::unique_ptr<Src> next() override {
if (skps.empty()) {
return nullptr;
}
SKPSrc src;
src.dir = dir;
src.path = skps.back();
skps.pop_back();
return move_unique(src);
}
};
static Register skp{"skp", "draw SKPs from dir=skps", SKPStream::Create};
struct BenchStream : Stream {
const BenchRegistry* registry = BenchRegistry::Head();
int samples;
static std::unique_ptr<Stream> Create(Options options) {
BenchStream stream;
stream.samples = std::max(1, atoi(options("samples", "20").c_str()));
return move_unique(stream);
}
struct BenchSrc : Src {
Benchmark* (*factory)(void*);
std::unique_ptr<Benchmark> bench;
int samples;
void init() {
if (bench) { return; }
bench.reset(factory(nullptr));
}
std::string name() override {
this->init();
return bench->getName();
}
SkISize size() override {
this->init();
return { bench->getSize().x(), bench->getSize().y() };
}
Status draw(SkCanvas* canvas) override {
this->init();
using ms = std::chrono::duration<double, std::milli>;
std::vector<ms> sample(samples);
bench->delayedSetup();
if (canvas) {
bench->perCanvasPreDraw(canvas);
}
for (int i = 0; i < samples; i++) {
using clock = std::chrono::high_resolution_clock;
for (int loops = 1; loops < 1000000000; loops *= 2) {
bench->preDraw(canvas);
auto start = clock::now();
bench->draw(loops, canvas);
ms elapsed = clock::now() - start;
bench->postDraw(canvas);
if (elapsed.count() < 10) {
continue;
}
sample[i] = elapsed / loops;
break;
}
}
if (canvas) {
bench->perCanvasPostDraw(canvas);
}
std::sort(sample.begin(), sample.end());
SkString msg = SkStringPrintf("%s\t@0", HumanizeMs(sample[0].count()).c_str());
if (samples > 2) {
msg.appendf("\t%s\t@%g", HumanizeMs(sample[samples-2].count()).c_str()
, 100.0*(samples-1) / samples);
}
if (samples > 1) {
msg.appendf("\t%s\t@100", HumanizeMs(sample[samples-1].count()).c_str());
}
ok_log(msg.c_str());
return Status::OK;
}
};
std::unique_ptr<Src> next() override {
if (!registry) {
return nullptr;
}
BenchSrc src;
src.factory = registry->factory();
src.samples = samples;
registry = registry->next();
return move_unique(src);
}
};
static Register bench{
"bench",
"time benchmarks linked into this binary samples=20 times each",
BenchStream::Create,
};
|