blob: 519d3209857dca282a29de8f642e26af91ab6a67 [file] [log] [blame]
mtkleinf3723212014-06-25 14:08:00 -07001/*
2 * Copyright 2014 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
mtkleinbb6a0282014-07-01 08:43:42 -07008#include <ctype.h>
9
tomhudsond968a6f2015-03-26 11:28:06 -070010#include "nanobench.h"
11
mtkleinf3723212014-06-25 14:08:00 -070012#include "Benchmark.h"
scroggo60869a42015-04-01 12:09:17 -070013#include "CodecBench.h"
mtkleinf3723212014-06-25 14:08:00 -070014#include "CrashHandler.h"
msarett95f192d2015-02-13 09:05:41 -080015#include "DecodingBench.h"
mtkleine714e752014-07-31 12:13:48 -070016#include "GMBench.h"
mtkleinafb43792014-08-19 15:55:55 -070017#include "ProcStats.h"
mtklein60317d0f2014-07-14 11:30:37 -070018#include "ResultsWriter.h"
mtkleinfd731ce2014-09-10 12:19:30 -070019#include "RecordingBench.h"
joshualitt261c3ad2015-04-27 09:16:57 -070020#include "SKPAnimationBench.h"
mtklein92007582014-08-01 07:46:52 -070021#include "SKPBench.h"
msarettb23e6aa2015-06-09 13:56:10 -070022#include "SubsetBenchPriv.h"
msarettb23e6aa2015-06-09 13:56:10 -070023#include "SubsetSingleBench.h"
24#include "SubsetTranslateBench.h"
25#include "SubsetZoomBench.h"
mtkleinf3723212014-06-25 14:08:00 -070026#include "Stats.h"
27#include "Timer.h"
28
mtklein6838d852014-10-29 14:15:10 -070029#include "SkBBoxHierarchy.h"
mtkleinf3723212014-06-25 14:08:00 -070030#include "SkCanvas.h"
scroggo60869a42015-04-01 12:09:17 -070031#include "SkCodec.h"
caryclark17f0b6d2014-07-22 10:15:34 -070032#include "SkCommonFlags.h"
msarett95f192d2015-02-13 09:05:41 -080033#include "SkData.h"
mtkleinf3723212014-06-25 14:08:00 -070034#include "SkForceLinking.h"
35#include "SkGraphics.h"
mtklein20840502014-08-21 15:51:22 -070036#include "SkOSFile.h"
37#include "SkPictureRecorder.h"
mtklein051e56d2014-12-04 08:46:51 -080038#include "SkPictureUtils.h"
mtkleinf3723212014-06-25 14:08:00 -070039#include "SkString.h"
40#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080041#include "SkTaskGroup.h"
mtkleinf3723212014-06-25 14:08:00 -070042
tomhudsond968a6f2015-03-26 11:28:06 -070043#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
44 #include "nanobenchAndroid.h"
45#endif
46
mtkleinbb6a0282014-07-01 08:43:42 -070047#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070048 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070049 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070050 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070051 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070052#endif
53
bsalomon682c2692015-05-22 14:01:46 -070054 struct GrContextOptions;
55
mtkleinf3723212014-06-25 14:08:00 -070056__SK_FORCE_IMAGE_DECODER_LINKING;
57
cdaltone1b89582015-06-25 19:17:08 -070058static const int kTimedSampling = 0;
59
reed53249782014-10-10 09:09:52 -070060static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070061
mtkleinb5110422014-08-07 15:20:02 -070062static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070063#ifdef SK_DEBUG
64 1;
mtkleina189ccd2014-07-14 12:28:47 -070065#else
bsalomon6eb03cc2014-08-07 14:28:50 -070066 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070067#endif
68
bsalomon6eb03cc2014-08-07 14:28:50 -070069static SkString loops_help_txt() {
70 SkString help;
71 help.printf("Number of times to run each bench. Set this to %d to auto-"
72 "tune for each bench. Timings are only reported when auto-tuning.",
73 kAutoTuneLoops);
74 return help;
75}
76
cdaltone1b89582015-06-25 19:17:08 -070077static SkString to_string(int n) {
78 SkString str;
79 str.appendS32(n);
80 return str;
81}
82
bsalomon6eb03cc2014-08-07 14:28:50 -070083DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
84
mtkleinf3723212014-06-25 14:08:00 -070085DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
cdaltone1b89582015-06-25 19:17:08 -070086DEFINE_string(samplingTime, "0", "Amount of time to run each bench. Takes precedence over samples."
87 "Must be \"0\", \"%%lfs\", or \"%%lfms\"");
mtkleinf3723212014-06-25 14:08:00 -070088DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
89DEFINE_double(overheadGoal, 0.0001,
90 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070091DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -070092DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070093DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
94 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070095
mtklein60317d0f2014-07-14 11:30:37 -070096DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -070097DEFINE_int32(maxCalibrationAttempts, 3,
98 "Try up to this many times to guess loops for a bench, or skip the bench.");
99DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700100DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
101DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700102DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
103 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700104DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
robertphillips5b693772014-11-21 06:19:36 -0800105DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700106DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700107DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800108DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800109DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
mtklein92007582014-08-01 07:46:52 -0700110
mtkleinf3723212014-06-25 14:08:00 -0700111static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700112 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800113 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700114}
mtklein55b0ffc2014-07-17 08:38:23 -0700115#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700116
tomhudsond968a6f2015-03-26 11:28:06 -0700117bool Target::init(SkImageInfo info, Benchmark* bench) {
118 if (Benchmark::kRaster_Backend == config.backend) {
119 this->surface.reset(SkSurface::NewRaster(info));
120 if (!this->surface.get()) {
121 return false;
122 }
123 }
124 return true;
125}
126bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700127 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700128 if (!canvas) {
129 return false;
130 }
131 bmp->setInfo(canvas->imageInfo());
132 if (!canvas->readPixels(bmp, 0, 0)) {
133 SkDebugf("Can't read canvas pixels.\n");
134 return false;
135 }
136 return true;
137}
138
139#if SK_SUPPORT_GPU
140struct GPUTarget : public Target {
141 explicit GPUTarget(const Config& c) : Target(c), gl(NULL) { }
142 SkGLContext* gl;
143
144 void setup() override {
145 this->gl->makeCurrent();
146 // Make sure we're done with whatever came before.
147 SK_GL(*this->gl, Finish());
148 }
149 void endTiming() override {
150 if (this->gl) {
151 SK_GL(*this->gl, Flush());
152 this->gl->swapBuffers();
153 }
154 }
155 void fence() override {
156 SK_GL(*this->gl, Finish());
157 }
mtkleind75c4662015-04-30 07:11:22 -0700158
cdaltond416a5b2015-06-23 13:23:44 -0700159 bool needsFrameTiming(int* maxFrameLag) const override {
160 if (!this->gl->getMaxGpuFrameLag(maxFrameLag)) {
161 // Frame lag is unknown.
162 *maxFrameLag = FLAGS_gpuFrameLag;
163 }
164 return true;
165 }
tomhudsond968a6f2015-03-26 11:28:06 -0700166 bool init(SkImageInfo info, Benchmark* bench) override {
167 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDistanceFieldFonts_Flag : 0;
168 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
169 this->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(this->config.ctxType),
170 SkSurface::kNo_Budgeted, info,
171 this->config.samples, &props));
172 this->gl = gGrFactory->getGLContext(this->config.ctxType);
173 if (!this->surface.get()) {
174 return false;
175 }
cdaltond416a5b2015-06-23 13:23:44 -0700176 if (!this->gl->fenceSyncSupport()) {
177 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
178 "Timings might not be accurate.\n", this->config.name);
179 }
tomhudsond968a6f2015-03-26 11:28:06 -0700180 return true;
181 }
182 void fillOptions(ResultsWriter* log) override {
183 const GrGLubyte* version;
184 SK_GL_RET(*this->gl, version, GetString(GR_GL_VERSION));
185 log->configOption("GL_VERSION", (const char*)(version));
186
187 SK_GL_RET(*this->gl, version, GetString(GR_GL_RENDERER));
188 log->configOption("GL_RENDERER", (const char*) version);
189
190 SK_GL_RET(*this->gl, version, GetString(GR_GL_VENDOR));
191 log->configOption("GL_VENDOR", (const char*) version);
192
193 SK_GL_RET(*this->gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
194 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
195 }
196};
mtkleind75c4662015-04-30 07:11:22 -0700197
tomhudsond968a6f2015-03-26 11:28:06 -0700198#endif
199
tomhudson75a0ebb2015-03-27 12:11:44 -0700200static double time(int loops, Benchmark* bench, Target* target) {
201 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700202 if (canvas) {
203 canvas->clear(SK_ColorWHITE);
204 }
mtkleinbb6a0282014-07-01 08:43:42 -0700205 WallTimer timer;
206 timer.start();
tomhudson75a0ebb2015-03-27 12:11:44 -0700207 canvas = target->beginTiming(canvas);
208 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700209 if (canvas) {
210 canvas->flush();
211 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700212 target->endTiming();
mtkleinbb6a0282014-07-01 08:43:42 -0700213 timer.end();
214 return timer.fWall;
215}
216
mtkleinf3723212014-06-25 14:08:00 -0700217static double estimate_timer_overhead() {
218 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700219 for (int i = 0; i < FLAGS_overheadLoops; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700220 WallTimer timer;
221 timer.start();
222 timer.end();
223 overhead += timer.fWall;
mtkleinf3723212014-06-25 14:08:00 -0700224 }
225 return overhead / FLAGS_overheadLoops;
226}
227
reed53249782014-10-10 09:09:52 -0700228static int detect_forever_loops(int loops) {
229 // look for a magic run-forever value
230 if (loops < 0) {
231 loops = SK_MaxS32;
232 }
233 return loops;
234}
235
mtklein55b0ffc2014-07-17 08:38:23 -0700236static int clamp_loops(int loops) {
237 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800238 SkDebugf("ERROR: clamping loops from %d to 1. "
239 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700240 return 1;
241 }
242 if (loops > FLAGS_maxLoops) {
243 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
244 return FLAGS_maxLoops;
245 }
246 return loops;
247}
248
tomhudsond968a6f2015-03-26 11:28:06 -0700249static bool write_canvas_png(Target* target, const SkString& filename) {
250
bsalomon6eb03cc2014-08-07 14:28:50 -0700251 if (filename.isEmpty()) {
252 return false;
253 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700254 if (target->getCanvas() &&
255 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700256 return false;
257 }
tomhudsond968a6f2015-03-26 11:28:06 -0700258
bsalomon6eb03cc2014-08-07 14:28:50 -0700259 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700260
261 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700262 return false;
263 }
tomhudsond968a6f2015-03-26 11:28:06 -0700264
bsalomon6eb03cc2014-08-07 14:28:50 -0700265 SkString dir = SkOSPath::Dirname(filename.c_str());
266 if (!sk_mkdir(dir.c_str())) {
267 SkDebugf("Can't make dir %s.\n", dir.c_str());
268 return false;
269 }
270 SkFILEWStream stream(filename.c_str());
271 if (!stream.isValid()) {
272 SkDebugf("Can't write %s.\n", filename.c_str());
273 return false;
274 }
275 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
276 SkDebugf("Can't encode a PNG.\n");
277 return false;
278 }
279 return true;
280}
281
282static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700283static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700284 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700285 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700286 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700287 int loops = bench->calculateLoops(FLAGS_loops);
288 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700289 while (bench_plus_overhead < overhead) {
290 if (round++ == FLAGS_maxCalibrationAttempts) {
291 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700292 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700293 return kFailedLoops;
294 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700295 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700296 }
mtklein2069e222014-08-04 13:57:39 -0700297 }
mtkleinf3723212014-06-25 14:08:00 -0700298
mtkleinbb6a0282014-07-01 08:43:42 -0700299 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700300 // We'll pick N to make timer overhead negligible:
301 //
mtkleinbb6a0282014-07-01 08:43:42 -0700302 // overhead
303 // ------------------------- < FLAGS_overheadGoal
304 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700305 //
mtkleinbb6a0282014-07-01 08:43:42 -0700306 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700307 //
308 // Doing some math, we get:
309 //
mtkleinbb6a0282014-07-01 08:43:42 -0700310 // (overhead / FLAGS_overheadGoal) - overhead
311 // ------------------------------------------ < N
312 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700313 //
314 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700315 if (kAutoTuneLoops == loops) {
316 const double numer = overhead / FLAGS_overheadGoal - overhead;
317 const double denom = bench_plus_overhead - overhead;
318 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700319 loops = clamp_loops(loops);
320 } else {
321 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700322 }
mtkleinbb6a0282014-07-01 08:43:42 -0700323
mtkleinbb6a0282014-07-01 08:43:42 -0700324 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700325}
326
cdaltone1b89582015-06-25 19:17:08 -0700327static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700328 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700329 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700330 if (kAutoTuneLoops == loops) {
331 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700332 double elapsed = 0;
333 do {
mtklein527930f2014-11-06 08:04:34 -0800334 if (1<<30 == loops) {
335 // We're about to wrap. Something's wrong with the bench.
336 loops = 0;
337 break;
338 }
mtkleina189ccd2014-07-14 12:28:47 -0700339 loops *= 2;
340 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700341 // _this_ round, not still timing last round.
342 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700343 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700344 }
345 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700346
mtkleina189ccd2014-07-14 12:28:47 -0700347 // We've overshot at least a little. Scale back linearly.
348 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700349 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700350
tomhudsond968a6f2015-03-26 11:28:06 -0700351 // Make sure we're not still timing our calibration.
352 target->fence();
reed53249782014-10-10 09:09:52 -0700353 } else {
354 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700355 }
mtkleinbb6a0282014-07-01 08:43:42 -0700356
357 // Pretty much the same deal as the calibration: do some warmup to make
358 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700359 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700360 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700361 }
mtkleinbb6a0282014-07-01 08:43:42 -0700362
mtkleinbb6a0282014-07-01 08:43:42 -0700363 return loops;
364}
mtkleinbb6a0282014-07-01 08:43:42 -0700365
366static SkString to_lower(const char* str) {
367 SkString lower(str);
368 for (size_t i = 0; i < lower.size(); i++) {
369 lower[i] = tolower(lower[i]);
370 }
371 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700372}
373
bsalomonc2553372014-07-22 13:09:05 -0700374static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700375 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700376 if (to_lower(FLAGS_config[i]).equals(name)) {
377 return true;
mtkleinf3723212014-06-25 14:08:00 -0700378 }
379 }
bsalomonc2553372014-07-22 13:09:05 -0700380 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700381}
382
bsalomonc2553372014-07-22 13:09:05 -0700383#if SK_SUPPORT_GPU
384static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
385 int sampleCnt) {
386 if (!is_cpu_config_allowed(name)) {
387 return false;
388 }
krajcevski69a55602014-08-13 10:46:31 -0700389 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomon76228632015-05-29 08:02:10 -0700390 return sampleCnt <= ctx->caps()->maxSampleCount();
bsalomonc2553372014-07-22 13:09:05 -0700391 }
392 return false;
393}
394#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700395
bsalomonc2553372014-07-22 13:09:05 -0700396#if SK_SUPPORT_GPU
397#define kBogusGLContextType GrContextFactory::kNative_GLContextType
398#else
399#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700400#endif
bsalomonc2553372014-07-22 13:09:05 -0700401
402// Append all configs that are enabled and supported.
403static void create_configs(SkTDArray<Config>* configs) {
jvanverth4736e142014-11-07 07:12:46 -0800404 #define CPU_CONFIG(name, backend, color, alpha) \
405 if (is_cpu_config_allowed(#name)) { \
406 Config config = { #name, Benchmark::backend, color, alpha, 0, \
407 kBogusGLContextType, false }; \
408 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700409 }
mtkleine714e752014-07-31 12:13:48 -0700410
mtklein40b32be2014-07-09 08:46:49 -0700411 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700412 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
413 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
414 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700415 }
mtkleinbb6a0282014-07-01 08:43:42 -0700416
417#if SK_SUPPORT_GPU
jvanverth4736e142014-11-07 07:12:46 -0800418 #define GPU_CONFIG(name, ctxType, samples, useDFText) \
bsalomonc2553372014-07-22 13:09:05 -0700419 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
420 Config config = { \
421 #name, \
422 Benchmark::kGPU_Backend, \
423 kN32_SkColorType, \
424 kPremul_SkAlphaType, \
425 samples, \
jvanverth4736e142014-11-07 07:12:46 -0800426 GrContextFactory::ctxType, \
427 useDFText }; \
bsalomonc2553372014-07-22 13:09:05 -0700428 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700429 }
mtkleine714e752014-07-31 12:13:48 -0700430
mtklein40b32be2014-07-09 08:46:49 -0700431 if (FLAGS_gpu) {
jvanverth4736e142014-11-07 07:12:46 -0800432 GPU_CONFIG(gpu, kNative_GLContextType, 0, false)
433 GPU_CONFIG(msaa4, kNative_GLContextType, 4, false)
434 GPU_CONFIG(msaa16, kNative_GLContextType, 16, false)
435 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4, false)
436 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16, false)
437 GPU_CONFIG(gpudft, kNative_GLContextType, 0, true)
438 GPU_CONFIG(debug, kDebug_GLContextType, 0, false)
439 GPU_CONFIG(nullgpu, kNull_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700440#ifdef SK_ANGLE
jvanverth4736e142014-11-07 07:12:46 -0800441 GPU_CONFIG(angle, kANGLE_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700442#endif
cdaltond416a5b2015-06-23 13:23:44 -0700443#if SK_MESA
444 GPU_CONFIG(mesa, kMESA_GLContextType, 0, false)
445#endif
mtklein40b32be2014-07-09 08:46:49 -0700446 }
mtkleinbb6a0282014-07-01 08:43:42 -0700447#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700448
449#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
450 if (is_cpu_config_allowed("hwui")) {
451 Config config = { "hwui", Benchmark::kHWUI_Backend, kRGBA_8888_SkColorType,
452 kPremul_SkAlphaType, 0, kBogusGLContextType, false };
453 configs->push(config);
454 }
455#endif
mtkleinf3723212014-06-25 14:08:00 -0700456}
457
bsalomonc2553372014-07-22 13:09:05 -0700458// If bench is enabled for config, returns a Target* for it, otherwise NULL.
459static Target* is_enabled(Benchmark* bench, const Config& config) {
460 if (!bench->isSuitableFor(config.backend)) {
461 return NULL;
462 }
463
reede5ea5002014-09-03 11:54:58 -0700464 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
465 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700466
tomhudsond968a6f2015-03-26 11:28:06 -0700467 Target* target = NULL;
bsalomonc2553372014-07-22 13:09:05 -0700468
tomhudsond968a6f2015-03-26 11:28:06 -0700469 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700470#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700471 case Benchmark::kGPU_Backend:
472 target = new GPUTarget(config);
473 break;
bsalomonc2553372014-07-22 13:09:05 -0700474#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700475#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
476 case Benchmark::kHWUI_Backend:
477 target = new HWUITarget(config, bench);
478 break;
479#endif
480 default:
481 target = new Target(config);
482 break;
483 }
bsalomonc2553372014-07-22 13:09:05 -0700484
tomhudsond968a6f2015-03-26 11:28:06 -0700485 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700486 delete target;
487 return NULL;
488 }
489 return target;
490}
491
msarettb23e6aa2015-06-09 13:56:10 -0700492/*
493 * Returns true if set up for a subset decode succeeds, false otherwise
494 * If the set-up succeeds, the width and height parameters will be set
495 */
496static bool valid_subset_bench(const SkString& path, SkColorType colorType, bool useCodec,
497 int* width, int* height) {
498 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
499 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded));
500
msarettab80e352015-06-17 10:28:22 -0700501 // Check that we can create a codec or image decoder.
msarettb23e6aa2015-06-09 13:56:10 -0700502 if (useCodec) {
503 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
504 if (NULL == codec) {
505 SkDebugf("Could not create codec for %s. Skipping bench.\n", path.c_str());
506 return false;
507 }
508
509 // These will be initialized by SkCodec if the color type is kIndex8 and
510 // unused otherwise.
511 SkPMColor colors[256];
512 int colorCount;
513 const SkImageInfo info = codec->getInfo().makeColorType(colorType);
514 SkAutoTDeleteArray<uint8_t> row(SkNEW_ARRAY(uint8_t, info.minRowBytes()));
515 SkScanlineDecoder* scanlineDecoder = codec->getScanlineDecoder(info, NULL,
516 colors, &colorCount);
517 if (NULL == scanlineDecoder) {
518 SkDebugf("Could not create scanline decoder for %s with color type %s. "
519 "Skipping bench.\n", path.c_str(), get_color_name(colorType));
520 return false;
521 }
522 *width = info.width();
523 *height = info.height();
524 } else {
525 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream));
526 if (NULL == decoder) {
527 SkDebugf("Could not create decoder for %s. Skipping bench.\n", path.c_str());
528 return false;
529 }
530 //FIXME: See skbug.com/3921
531 if (kIndex_8_SkColorType == colorType || kGray_8_SkColorType == colorType) {
532 SkDebugf("Cannot use image subset decoder for %s with color type %s. "
533 "Skipping bench.\n", path.c_str(), get_color_name(colorType));
534 return false;
535 }
536 if (!decoder->buildTileIndex(stream.detach(), width, height)) {
537 SkDebugf("Could not build tile index for %s. Skipping bench.\n", path.c_str());
538 return false;
539 }
540 }
msarettab80e352015-06-17 10:28:22 -0700541
542 // Check if the image is large enough for a meaningful subset benchmark.
543 if (*width <= 512 && *height <= 512) {
544 // This should not print a message since it is not an error.
545 return false;
546 }
547
msarettb23e6aa2015-06-09 13:56:10 -0700548 return true;
549}
jcgregoriobf5e5232014-07-17 13:14:16 -0700550
egdaniel3bf92062015-06-26 08:12:46 -0700551static void cleanup_run(Target* target) {
552 SkDELETE(target);
553#if SK_SUPPORT_GPU
554 if (FLAGS_abandonGpuContext) {
555 gGrFactory->abandonContexts();
556 }
557 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
558 gGrFactory->destroyContexts();
559 }
560#endif
561}
562
mtkleine714e752014-07-31 12:13:48 -0700563class BenchmarkStream {
564public:
mtklein92007582014-08-01 07:46:52 -0700565 BenchmarkStream() : fBenches(BenchRegistry::Head())
566 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700567 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700568 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800569 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800570 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700571 , fCurrentCodec(0)
msarett95f192d2015-02-13 09:05:41 -0800572 , fCurrentImage(0)
573 , fCurrentSubsetImage(0)
574 , fCurrentColorType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700575 , fCurrentSubsetType(0)
576 , fUseCodec(0)
577 , fCurrentAnimSKP(0) {
mtklein92007582014-08-01 07:46:52 -0700578 for (int i = 0; i < FLAGS_skps.count(); i++) {
579 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
580 fSKPs.push_back() = FLAGS_skps[i];
581 } else {
582 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
583 SkString path;
584 while (it.next(&path)) {
585 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
586 }
587 }
588 }
mtkleine714e752014-07-31 12:13:48 -0700589
mtklein92007582014-08-01 07:46:52 -0700590 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
591 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
592 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
593 exit(1);
594 }
595
596 for (int i = 0; i < FLAGS_scales.count(); i++) {
597 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
598 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
599 exit(1);
600 }
601 }
robertphillips5b693772014-11-21 06:19:36 -0800602
cdalton63a82852015-06-29 14:06:10 -0700603 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
604 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700605 exit(1);
606 }
607
robertphillips5b693772014-11-21 06:19:36 -0800608 if (FLAGS_mpd) {
609 fUseMPDs.push_back() = true;
610 }
mtkleinc751ecb2015-06-15 08:56:38 -0700611 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700612
msarett95f192d2015-02-13 09:05:41 -0800613 // Prepare the images for decoding
614 for (int i = 0; i < FLAGS_images.count(); i++) {
615 const char* flag = FLAGS_images[i];
616 if (sk_isdir(flag)) {
617 // If the value passed in is a directory, add all the images
618 SkOSFile::Iter it(flag);
619 SkString file;
620 while (it.next(&file)) {
621 fImages.push_back() = SkOSPath::Join(flag, file.c_str());
622 }
623 } else if (sk_exists(flag)) {
624 // Also add the value if it is a single image
625 fImages.push_back() = flag;
626 }
627 }
mtklein95553d92015-03-12 08:24:21 -0700628
msarett95f192d2015-02-13 09:05:41 -0800629 // Choose the candidate color types for image decoding
630 const SkColorType colorTypes[] =
msarettb23e6aa2015-06-09 13:56:10 -0700631 { kN32_SkColorType,
632 kRGB_565_SkColorType,
633 kAlpha_8_SkColorType,
634 kIndex_8_SkColorType,
635 kGray_8_SkColorType };
msarett95f192d2015-02-13 09:05:41 -0800636 fColorTypes.push_back_n(SK_ARRAY_COUNT(colorTypes), colorTypes);
mtklein92007582014-08-01 07:46:52 -0700637 }
638
mtkleinfd731ce2014-09-10 12:19:30 -0700639 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
640 // Not strictly necessary, as it will be checked again later,
641 // but helps to avoid a lot of pointless work if we're going to skip it.
642 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
643 return false;
644 }
645
scroggoa1193e42015-01-21 12:09:53 -0800646 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
mtkleinfd731ce2014-09-10 12:19:30 -0700647 if (stream.get() == NULL) {
648 SkDebugf("Could not read %s.\n", path);
649 return false;
650 }
651
mtklein57f27bd2015-02-09 11:58:41 -0800652 pic->reset(SkPicture::CreateFromStream(stream.get()));
mtkleinfd731ce2014-09-10 12:19:30 -0700653 if (pic->get() == NULL) {
654 SkDebugf("Could not read %s as an SkPicture.\n", path);
655 return false;
656 }
657 return true;
658 }
659
mtklein92007582014-08-01 07:46:52 -0700660 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700661 if (fBenches) {
662 Benchmark* bench = fBenches->factory()(NULL);
663 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700664 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700665 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700666 return bench;
667 }
mtklein92007582014-08-01 07:46:52 -0700668
mtkleine714e752014-07-31 12:13:48 -0700669 while (fGMs) {
670 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
671 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800672 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700673 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700674 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700675 return SkNEW_ARGS(GMBench, (gm.detach()));
676 }
677 }
mtklein92007582014-08-01 07:46:52 -0700678
mtkleinfd731ce2014-09-10 12:19:30 -0700679 // First add all .skps as RecordingBenches.
680 while (fCurrentRecording < fSKPs.count()) {
681 const SkString& path = fSKPs[fCurrentRecording++];
682 SkAutoTUnref<SkPicture> pic;
683 if (!ReadPicture(path.c_str(), &pic)) {
684 continue;
685 }
686 SkString name = SkOSPath::Basename(path.c_str());
687 fSourceType = "skp";
688 fBenchType = "recording";
bsalomon0aa5cea2014-12-15 09:13:35 -0800689 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic));
mtklein051e56d2014-12-04 08:46:51 -0800690 fSKPOps = pic->approximateOpCount();
mtkleinfd731ce2014-09-10 12:19:30 -0700691 return SkNEW_ARGS(RecordingBench, (name.c_str(), pic.get(), FLAGS_bbh));
692 }
693
694 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700695 while (fCurrentScale < fScales.count()) {
696 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800697 const SkString& path = fSKPs[fCurrentSKP];
mtkleinfd731ce2014-09-10 12:19:30 -0700698 SkAutoTUnref<SkPicture> pic;
699 if (!ReadPicture(path.c_str(), &pic)) {
robertphillips5b693772014-11-21 06:19:36 -0800700 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700701 continue;
702 }
robertphillips5b693772014-11-21 06:19:36 -0800703
704 while (fCurrentUseMPD < fUseMPDs.count()) {
705 if (FLAGS_bbh) {
706 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
707 SkRTreeFactory factory;
708 SkPictureRecorder recorder;
709 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
710 pic->playback(recorder.beginRecording(pic->cullRect().width(),
711 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800712 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800713 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
robertphillips5b693772014-11-21 06:19:36 -0800714 pic.reset(recorder.endRecording());
715 }
716 SkString name = SkOSPath::Basename(path.c_str());
717 fSourceType = "skp";
718 fBenchType = "playback";
719 return SkNEW_ARGS(SKPBench,
cdaltonb4022962015-06-25 10:51:56 -0700720 (name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
721 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP));
joshualitt261c3ad2015-04-27 09:16:57 -0700722
mtklein20840502014-08-21 15:51:22 -0700723 }
robertphillips5b693772014-11-21 06:19:36 -0800724 fCurrentUseMPD = 0;
725 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700726 }
727 fCurrentSKP = 0;
728 fCurrentScale++;
729 }
730
joshualitt261c3ad2015-04-27 09:16:57 -0700731 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700732 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700733 while (fCurrentAnimSKP < fSKPs.count()) {
734 const SkString& path = fSKPs[fCurrentAnimSKP];
735 SkAutoTUnref<SkPicture> pic;
736 if (!ReadPicture(path.c_str(), &pic)) {
737 fCurrentAnimSKP++;
738 continue;
739 }
740
741 fCurrentAnimSKP++;
742 SkString name = SkOSPath::Basename(path.c_str());
cdalton63a82852015-06-29 14:06:10 -0700743 SkAutoTUnref<SKPAnimationBench::Animation> animation(
744 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
745 return SkNEW_ARGS(SKPAnimationBench, (name.c_str(), pic.get(), fClip, animation,
746 FLAGS_loopSKP));
joshualitt261c3ad2015-04-27 09:16:57 -0700747 }
748 }
749
750
scroggo60869a42015-04-01 12:09:17 -0700751 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
752 const SkString& path = fImages[fCurrentCodec];
753 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
754 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700755 if (!codec) {
756 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700757 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700758 continue;
759 }
scroggo21027992015-04-02 13:22:38 -0700760
scroggo60869a42015-04-01 12:09:17 -0700761 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700762 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo60869a42015-04-01 12:09:17 -0700763 fCurrentColorType++;
scroggo21027992015-04-02 13:22:38 -0700764
scroggo60869a42015-04-01 12:09:17 -0700765 // Make sure we can decode to this color type.
scroggo60869a42015-04-01 12:09:17 -0700766 SkImageInfo info = codec->getInfo().makeColorType(colorType);
scroggo21027992015-04-02 13:22:38 -0700767 SkAlphaType alphaType;
768 if (!SkColorTypeValidateAlphaType(colorType, info.alphaType(),
769 &alphaType)) {
770 continue;
771 }
772 if (alphaType != info.alphaType()) {
773 info = info.makeAlphaType(alphaType);
774 }
775
776 const size_t rowBytes = info.minRowBytes();
777 SkAutoMalloc storage(info.getSafeSize(rowBytes));
778
779 // Used if fCurrentColorType is kIndex_8_SkColorType
780 int colorCount = 256;
781 SkPMColor colors[256];
782
scroggo60869a42015-04-01 12:09:17 -0700783 const SkImageGenerator::Result result = codec->getPixels(
scroggo21027992015-04-02 13:22:38 -0700784 info, storage.get(), rowBytes, NULL, colors,
785 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700786 switch (result) {
787 case SkImageGenerator::kSuccess:
788 case SkImageGenerator::kIncompleteInput:
789 return new CodecBench(SkOSPath::Basename(path.c_str()),
790 encoded, colorType);
791 case SkImageGenerator::kInvalidConversion:
792 // This is okay. Not all conversions are valid.
793 break;
scroggo60869a42015-04-01 12:09:17 -0700794 default:
795 // This represents some sort of failure.
796 SkASSERT(false);
797 break;
798 }
799 }
800 fCurrentColorType = 0;
801 }
802
msarett95f192d2015-02-13 09:05:41 -0800803 // Run the DecodingBenches
804 while (fCurrentImage < fImages.count()) {
805 while (fCurrentColorType < fColorTypes.count()) {
806 const SkString& path = fImages[fCurrentImage];
807 SkColorType colorType = fColorTypes[fCurrentColorType];
808 fCurrentColorType++;
scroggo60869a42015-04-01 12:09:17 -0700809 // Check if the image decodes to the right color type
810 // before creating the benchmark
msarett95f192d2015-02-13 09:05:41 -0800811 SkBitmap bitmap;
812 if (SkImageDecoder::DecodeFile(path.c_str(), &bitmap,
scroggo60869a42015-04-01 12:09:17 -0700813 colorType, SkImageDecoder::kDecodePixels_Mode)
814 && bitmap.colorType() == colorType) {
msarett95f192d2015-02-13 09:05:41 -0800815 return new DecodingBench(path, colorType);
816 }
817 }
818 fCurrentColorType = 0;
819 fCurrentImage++;
820 }
821
msarettb23e6aa2015-06-09 13:56:10 -0700822 // Run the SubsetBenches
823 bool useCodecOpts[] = { true, false };
824 while (fUseCodec < 2) {
825 bool useCodec = useCodecOpts[fUseCodec];
826 while (fCurrentSubsetImage < fImages.count()) {
827 while (fCurrentColorType < fColorTypes.count()) {
828 const SkString& path = fImages[fCurrentSubsetImage];
829 SkColorType colorType = fColorTypes[fCurrentColorType];
830 while (fCurrentSubsetType <= kLast_SubsetType) {
831 int width = 0;
832 int height = 0;
833 int currentSubsetType = fCurrentSubsetType++;
834 if (valid_subset_bench(path, colorType, useCodec, &width, &height)) {
835 switch (currentSubsetType) {
836 case kTopLeft_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700837 return new SubsetSingleBench(path, colorType, width/3,
838 height/3, 0, 0, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700839 case kTopRight_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700840 return new SubsetSingleBench(path, colorType, width/3,
841 height/3, 2*width/3, 0, useCodec);
842 case kMiddle_SubsetType:
843 return new SubsetSingleBench(path, colorType, width/3,
844 height/3, width/3, height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700845 case kBottomLeft_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700846 return new SubsetSingleBench(path, colorType, width/3,
847 height/3, 0, 2*height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700848 case kBottomRight_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700849 return new SubsetSingleBench(path, colorType, width/3,
850 height/3, 2*width/3, 2*height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700851 case kTranslate_SubsetType:
852 return new SubsetTranslateBench(path, colorType, 512, 512,
853 useCodec);
854 case kZoom_SubsetType:
855 return new SubsetZoomBench(path, colorType, 512, 512,
856 useCodec);
msarett95f192d2015-02-13 09:05:41 -0800857 }
msarettb23e6aa2015-06-09 13:56:10 -0700858 } else {
859 break;
msarett95f192d2015-02-13 09:05:41 -0800860 }
861 }
msarettb23e6aa2015-06-09 13:56:10 -0700862 fCurrentSubsetType = 0;
863 fCurrentColorType++;
msarett95f192d2015-02-13 09:05:41 -0800864 }
msarettb23e6aa2015-06-09 13:56:10 -0700865 fCurrentColorType = 0;
866 fCurrentSubsetImage++;
msarett95f192d2015-02-13 09:05:41 -0800867 }
msarettb23e6aa2015-06-09 13:56:10 -0700868 fCurrentSubsetImage = 0;
869 fUseCodec++;
msarett95f192d2015-02-13 09:05:41 -0800870 }
871
mtkleine714e752014-07-31 12:13:48 -0700872 return NULL;
873 }
mtklein92007582014-08-01 07:46:52 -0700874
875 void fillCurrentOptions(ResultsWriter* log) const {
876 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700877 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700878 if (0 == strcmp(fSourceType, "skp")) {
879 log->configOption("clip",
880 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
881 fClip.fRight, fClip.fBottom).c_str());
882 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -0800883 if (fCurrentUseMPD > 0) {
884 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
885 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
886 }
mtklein92007582014-08-01 07:46:52 -0700887 }
mtklein051e56d2014-12-04 08:46:51 -0800888 if (0 == strcmp(fBenchType, "recording")) {
889 log->metric("bytes", fSKPBytes);
890 log->metric("ops", fSKPOps);
891 }
mtklein92007582014-08-01 07:46:52 -0700892 }
893
mtkleine714e752014-07-31 12:13:48 -0700894private:
msarettb23e6aa2015-06-09 13:56:10 -0700895 enum SubsetType {
896 kTopLeft_SubsetType = 0,
897 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -0700898 kMiddle_SubsetType = 2,
899 kBottomLeft_SubsetType = 3,
900 kBottomRight_SubsetType = 4,
901 kTranslate_SubsetType = 5,
902 kZoom_SubsetType = 6,
msarettb23e6aa2015-06-09 13:56:10 -0700903 kLast_SubsetType = kZoom_SubsetType
904 };
905
mtkleine714e752014-07-31 12:13:48 -0700906 const BenchRegistry* fBenches;
907 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700908 SkIRect fClip;
909 SkTArray<SkScalar> fScales;
910 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -0800911 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -0800912 SkTArray<SkString> fImages;
913 SkTArray<SkColorType> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -0700914 SkScalar fZoomMax;
915 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -0700916
mtklein051e56d2014-12-04 08:46:51 -0800917 double fSKPBytes, fSKPOps;
918
mtkleinfd731ce2014-09-10 12:19:30 -0700919 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
920 const char* fBenchType; // How we bench it: micro, recording, playback, ...
921 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700922 int fCurrentScale;
923 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -0800924 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -0700925 int fCurrentCodec;
msarett95f192d2015-02-13 09:05:41 -0800926 int fCurrentImage;
927 int fCurrentSubsetImage;
928 int fCurrentColorType;
msarettb23e6aa2015-06-09 13:56:10 -0700929 int fCurrentSubsetType;
930 int fUseCodec;
joshualitt261c3ad2015-04-27 09:16:57 -0700931 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -0700932};
933
jcgregorio3b27ade2014-11-13 08:06:40 -0800934int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700935int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800936 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -0700937 SkAutoGraphics ag;
mtklein4f108442014-12-03 13:07:39 -0800938 SkTaskGroup::Enabler enabled;
mtkleinf3723212014-06-25 14:08:00 -0700939
krajcevski69a55602014-08-13 10:46:31 -0700940#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -0700941 GrContextOptions grContextOpts;
krajcevski12b35442014-08-13 12:06:26 -0700942 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
943 gGrFactory.reset(SkNEW_ARGS(GrContextFactory, (grContextOpts)));
krajcevski69a55602014-08-13 10:46:31 -0700944#endif
945
bsalomon06cddec2014-10-24 10:40:50 -0700946 if (FLAGS_veryVerbose) {
947 FLAGS_verbose = true;
948 }
949
cdaltone1b89582015-06-25 19:17:08 -0700950 double samplingTimeMs = 0;
951 if (0 != strcmp("0", FLAGS_samplingTime[0])) {
952 SkSTArray<8, char> timeUnit;
953 timeUnit.push_back_n(static_cast<int>(strlen(FLAGS_samplingTime[0])) + 1);
954 if (2 != sscanf(FLAGS_samplingTime[0], "%lf%s", &samplingTimeMs, timeUnit.begin()) ||
955 (0 != strcmp("s", timeUnit.begin()) && 0 != strcmp("ms", timeUnit.begin()))) {
956 SkDebugf("Invalid --samplingTime \"%s\". Must be \"0\", \"%%lfs\", or \"%%lfms\"\n",
957 FLAGS_samplingTime[0]);
958 exit(0);
959 }
960 if (0 == strcmp("s", timeUnit.begin())) {
961 samplingTimeMs *= 1000;
962 }
963 if (samplingTimeMs) {
964 FLAGS_samples = kTimedSampling;
965 }
966 }
967
bsalomon6eb03cc2014-08-07 14:28:50 -0700968 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700969 FLAGS_samples = 1;
970 FLAGS_gpuFrameLag = 0;
971 }
972
bsalomon6eb03cc2014-08-07 14:28:50 -0700973 if (!FLAGS_writePath.isEmpty()) {
974 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
975 if (!sk_mkdir(FLAGS_writePath[0])) {
976 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
977 FLAGS_writePath.set(0, NULL);
978 }
979 }
980
mtklein1915b622014-08-20 11:45:00 -0700981 SkAutoTDelete<ResultsWriter> log(SkNEW(ResultsWriter));
mtklein60317d0f2014-07-14 11:30:37 -0700982 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700983 log.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0])));
mtklein60317d0f2014-07-14 11:30:37 -0700984 }
mtklein1915b622014-08-20 11:45:00 -0700985
986 if (1 == FLAGS_properties.count() % 2) {
987 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
988 return 1;
989 }
990 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
991 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
992 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700993
994 if (1 == FLAGS_key.count() % 2) {
995 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
996 return 1;
997 }
998 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -0700999 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001000 }
mtklein60317d0f2014-07-14 11:30:37 -07001001
mtkleinf3723212014-06-25 14:08:00 -07001002 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001003 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001004
cdaltone1b89582015-06-25 19:17:08 -07001005 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001006
bsalomon6eb03cc2014-08-07 14:28:50 -07001007 if (kAutoTuneLoops != FLAGS_loops) {
1008 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001009 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -07001010 SkDebugf("median\tbench\tconfig\n");
cdaltone1b89582015-06-25 19:17:08 -07001011 } else if (kTimedSampling == FLAGS_samples) {
1012 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001013 } else {
mtkleind75c4662015-04-30 07:11:22 -07001014 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001015 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001016 }
1017
bsalomonc2553372014-07-22 13:09:05 -07001018 SkTDArray<Config> configs;
1019 create_configs(&configs);
1020
mtkleine070c2b2014-10-14 08:40:43 -07001021 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001022 BenchmarkStream benchStream;
1023 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001024 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001025 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001026 continue;
1027 }
1028
egdaniel3bf92062015-06-26 08:12:46 -07001029 if (!configs.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -07001030 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
jcgregoriobf5e5232014-07-17 13:14:16 -07001031 bench->preDraw();
1032 }
egdaniel3bf92062015-06-26 08:12:46 -07001033 for (int i = 0; i < configs.count(); ++i) {
1034 Target* target = is_enabled(b, configs[i]);
1035 if (!target) {
1036 continue;
1037 }
mtkleinf3723212014-06-25 14:08:00 -07001038
egdaniel3bf92062015-06-26 08:12:46 -07001039 // During HWUI output this canvas may be NULL.
1040 SkCanvas* canvas = target->getCanvas();
1041 const char* config = target->config.name;
1042
1043 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001044 bench->perCanvasPreDraw(canvas);
1045
cdaltone1b89582015-06-25 19:17:08 -07001046 int maxFrameLag;
egdaniel3bf92062015-06-26 08:12:46 -07001047 const int loops = target->needsFrameTiming(&maxFrameLag)
1048 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1049 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001050
1051 if (kTimedSampling != FLAGS_samples) {
1052 samples.reset(FLAGS_samples);
1053 for (int s = 0; s < FLAGS_samples; s++) {
egdaniel3bf92062015-06-26 08:12:46 -07001054 samples[s] = time(loops, bench, target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001055 }
1056 } else if (samplingTimeMs) {
1057 samples.reset();
1058 if (FLAGS_verbose) {
1059 SkDebugf("Begin sampling %s for %ims\n",
1060 bench->getUniqueName(), static_cast<int>(samplingTimeMs));
1061 }
1062 WallTimer timer;
1063 timer.start();
1064 do {
egdaniel3bf92062015-06-26 08:12:46 -07001065 samples.push_back(time(loops, bench, target) / loops);
cdaltone1b89582015-06-25 19:17:08 -07001066 timer.end();
1067 } while (timer.fWall < samplingTimeMs);
1068 }
mtkleinf3723212014-06-25 14:08:00 -07001069
robertphillips5b693772014-11-21 06:19:36 -08001070 bench->perCanvasPostDraw(canvas);
1071
egdaniel3bf92062015-06-26 08:12:46 -07001072 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001073 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001074 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001075 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001076 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001077 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001078 }
1079
1080 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001081 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001082 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001083 continue;
1084 }
1085
cdaltone1b89582015-06-25 19:17:08 -07001086 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001087 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001088 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001089 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001090 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001091 log->metric("min_ms", stats.min);
mtkleine070c2b2014-10-14 08:40:43 -07001092 if (runs++ % FLAGS_flushEvery == 0) {
1093 log->flush();
1094 }
mtklein60317d0f2014-07-14 11:30:37 -07001095
bsalomon6eb03cc2014-08-07 14:28:50 -07001096 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001097 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001098 config = ""; // Only print the config if we run the same bench on more than one.
1099 }
mtkleind75c4662015-04-30 07:11:22 -07001100 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1101 , sk_tools::getCurrResidentSetSizeMB()
1102 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001103 , bench->getUniqueName()
1104 , config);
mtkleinf3723212014-06-25 14:08:00 -07001105 } else if (FLAGS_quiet) {
egdaniel3bf92062015-06-26 08:12:46 -07001106 if (configs.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -07001107 config = ""; // Only print the config if we run the same bench on more than one.
1108 }
mtklein96289052014-09-10 12:05:59 -07001109 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001110 } else {
1111 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001112 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1113 , sk_tools::getCurrResidentSetSizeMB()
1114 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001115 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001116 , HUMANIZE(stats.min)
1117 , HUMANIZE(stats.median)
1118 , HUMANIZE(stats.mean)
1119 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001120 , stddev_percent
cdaltone1b89582015-06-25 19:17:08 -07001121 , kTimedSampling != FLAGS_samples ? stats.plot.c_str()
1122 : to_string(samples.count()).c_str()
mtkleinf3723212014-06-25 14:08:00 -07001123 , config
mtklein96289052014-09-10 12:05:59 -07001124 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001125 );
1126 }
bsalomonb12ea412015-02-02 21:19:50 -08001127#if SK_SUPPORT_GPU
1128 if (FLAGS_gpuStats &&
egdaniel3bf92062015-06-26 08:12:46 -07001129 Benchmark::kGPU_Backend == configs[i].backend) {
1130 gGrFactory->get(configs[i].ctxType)->printCacheStats();
1131 gGrFactory->get(configs[i].ctxType)->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001132 }
1133#endif
cdalton2c56ba52015-06-26 13:32:53 -07001134 if (FLAGS_verbose) {
1135 SkDebugf("Samples: ");
1136 for (int i = 0; i < samples.count(); i++) {
1137 SkDebugf("%s ", HUMANIZE(samples[i]));
1138 }
1139 SkDebugf("%s\n", bench->getUniqueName());
1140 }
egdaniel3bf92062015-06-26 08:12:46 -07001141 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001142 }
mtkleinf3723212014-06-25 14:08:00 -07001143 }
1144
mtkleine1091452014-12-04 10:47:02 -08001145 log->bench("memory_usage", 0,0);
1146 log->config("meta");
1147 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1148
joshualitte0b19d42015-03-26 10:41:02 -07001149#if SK_SUPPORT_GPU
1150 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1151 // SkEventTracer destructor
1152 gGrFactory.reset(NULL);
1153#endif
1154
mtkleinf3723212014-06-25 14:08:00 -07001155 return 0;
1156}
1157
jcgregorio3b27ade2014-11-13 08:06:40 -08001158#if !defined SK_BUILD_FOR_IOS
1159int main(int argc, char** argv) {
1160 SkCommandLineFlags::Parse(argc, argv);
1161 return nanobench_main();
1162}
1163#endif