blob: 5a83860713b05deca1507221918045efaaf9a4d3 [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"
msarett7f691442015-09-22 11:56:16 -070013#include "BitmapRegionDecoderBench.h"
scroggo60869a42015-04-01 12:09:17 -070014#include "CodecBench.h"
msarett7f691442015-09-22 11:56:16 -070015#include "CodecBenchPriv.h"
mtkleinf3723212014-06-25 14:08:00 -070016#include "CrashHandler.h"
msarett95f192d2015-02-13 09:05:41 -080017#include "DecodingBench.h"
mtkleine714e752014-07-31 12:13:48 -070018#include "GMBench.h"
mtkleinafb43792014-08-19 15:55:55 -070019#include "ProcStats.h"
mtklein60317d0f2014-07-14 11:30:37 -070020#include "ResultsWriter.h"
mtkleinfd731ce2014-09-10 12:19:30 -070021#include "RecordingBench.h"
joshualitt261c3ad2015-04-27 09:16:57 -070022#include "SKPAnimationBench.h"
mtklein92007582014-08-01 07:46:52 -070023#include "SKPBench.h"
mtkleinf3723212014-06-25 14:08:00 -070024#include "Stats.h"
mtkleinf3723212014-06-25 14:08:00 -070025
msarett5cb48852015-11-06 08:56:32 -080026#include "SkBitmapRegionDecoder.h"
mtklein6838d852014-10-29 14:15:10 -070027#include "SkBBoxHierarchy.h"
mtkleinf3723212014-06-25 14:08:00 -070028#include "SkCanvas.h"
scroggo60869a42015-04-01 12:09:17 -070029#include "SkCodec.h"
caryclark17f0b6d2014-07-22 10:15:34 -070030#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080031#include "SkCommonFlagsConfig.h"
msarett95f192d2015-02-13 09:05:41 -080032#include "SkData.h"
mtkleinf3723212014-06-25 14:08:00 -070033#include "SkForceLinking.h"
34#include "SkGraphics.h"
mtklein20840502014-08-21 15:51:22 -070035#include "SkOSFile.h"
36#include "SkPictureRecorder.h"
mtklein051e56d2014-12-04 08:46:51 -080037#include "SkPictureUtils.h"
mtkleinf3723212014-06-25 14:08:00 -070038#include "SkString.h"
39#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080040#include "SkTaskGroup.h"
msarettc149f0e2016-01-04 11:35:43 -080041#include "SkThreadUtils.h"
mtkleinf3723212014-06-25 14:08:00 -070042
bungeman60e0fee2015-08-26 05:15:46 -070043#include <stdlib.h>
44
scroggo38ce0a72016-01-07 07:28:47 -080045#ifndef SK_BUILD_FOR_WIN32
46 #include <unistd.h>
47#endif
48
tomhudsond968a6f2015-03-26 11:28:06 -070049#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
50 #include "nanobenchAndroid.h"
51#endif
52
mtkleinbb6a0282014-07-01 08:43:42 -070053#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070054 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070055 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070056 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070057 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070058#endif
59
bsalomon682c2692015-05-22 14:01:46 -070060 struct GrContextOptions;
61
mtkleinf3723212014-06-25 14:08:00 -070062__SK_FORCE_IMAGE_DECODER_LINKING;
63
reed53249782014-10-10 09:09:52 -070064static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070065
mtkleinb5110422014-08-07 15:20:02 -070066static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070067#ifdef SK_DEBUG
68 1;
mtkleina189ccd2014-07-14 12:28:47 -070069#else
bsalomon6eb03cc2014-08-07 14:28:50 -070070 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070071#endif
72
bsalomon6eb03cc2014-08-07 14:28:50 -070073static SkString loops_help_txt() {
74 SkString help;
75 help.printf("Number of times to run each bench. Set this to %d to auto-"
76 "tune for each bench. Timings are only reported when auto-tuning.",
77 kAutoTuneLoops);
78 return help;
79}
80
cdaltone1b89582015-06-25 19:17:08 -070081static SkString to_string(int n) {
82 SkString str;
83 str.appendS32(n);
84 return str;
85}
86
bsalomon6eb03cc2014-08-07 14:28:50 -070087DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
88
mtkleinf3723212014-06-25 14:08:00 -070089DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -070090DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -070091DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
92DEFINE_double(overheadGoal, 0.0001,
93 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070094DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -070095DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070096DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
97 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070098
mtklein60317d0f2014-07-14 11:30:37 -070099DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -0700100DEFINE_int32(maxCalibrationAttempts, 3,
101 "Try up to this many times to guess loops for a bench, or skip the bench.");
102DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700103DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
104DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700105DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
106 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700107DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
robertphillips5b693772014-11-21 06:19:36 -0800108DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700109DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700110DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800111DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800112DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800113DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
msarettc149f0e2016-01-04 11:35:43 -0800114DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
mtklein92007582014-08-01 07:46:52 -0700115
mtkleinbbba1682015-10-28 11:36:30 -0700116static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
117
mtkleinf3723212014-06-25 14:08:00 -0700118static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700119 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800120 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700121}
mtklein55b0ffc2014-07-17 08:38:23 -0700122#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700123
tomhudsond968a6f2015-03-26 11:28:06 -0700124bool Target::init(SkImageInfo info, Benchmark* bench) {
125 if (Benchmark::kRaster_Backend == config.backend) {
126 this->surface.reset(SkSurface::NewRaster(info));
127 if (!this->surface.get()) {
128 return false;
129 }
130 }
131 return true;
132}
133bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700134 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700135 if (!canvas) {
136 return false;
137 }
138 bmp->setInfo(canvas->imageInfo());
139 if (!canvas->readPixels(bmp, 0, 0)) {
140 SkDebugf("Can't read canvas pixels.\n");
141 return false;
142 }
143 return true;
144}
145
146#if SK_SUPPORT_GPU
147struct GPUTarget : public Target {
halcanary96fcdcc2015-08-27 07:41:13 -0700148 explicit GPUTarget(const Config& c) : Target(c), gl(nullptr) { }
tomhudsond968a6f2015-03-26 11:28:06 -0700149 SkGLContext* gl;
150
151 void setup() override {
152 this->gl->makeCurrent();
153 // Make sure we're done with whatever came before.
154 SK_GL(*this->gl, Finish());
155 }
156 void endTiming() override {
157 if (this->gl) {
158 SK_GL(*this->gl, Flush());
159 this->gl->swapBuffers();
160 }
161 }
162 void fence() override {
163 SK_GL(*this->gl, Finish());
164 }
mtkleind75c4662015-04-30 07:11:22 -0700165
cdaltond416a5b2015-06-23 13:23:44 -0700166 bool needsFrameTiming(int* maxFrameLag) const override {
167 if (!this->gl->getMaxGpuFrameLag(maxFrameLag)) {
168 // Frame lag is unknown.
169 *maxFrameLag = FLAGS_gpuFrameLag;
170 }
171 return true;
172 }
tomhudsond968a6f2015-03-26 11:28:06 -0700173 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700174 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
175 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700176 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
kkinnunen5219fd92015-12-10 06:28:13 -0800177 this->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(this->config.ctxType,
kkinnunen5219fd92015-12-10 06:28:13 -0800178 this->config.ctxOptions),
tomhudsond968a6f2015-03-26 11:28:06 -0700179 SkSurface::kNo_Budgeted, info,
180 this->config.samples, &props));
kkinnunen3e980c32015-12-23 01:33:00 -0800181 this->gl = gGrFactory->getContextInfo(this->config.ctxType,
kkinnunen34058002016-01-06 23:49:30 -0800182 this->config.ctxOptions).fGLContext;
tomhudsond968a6f2015-03-26 11:28:06 -0700183 if (!this->surface.get()) {
184 return false;
185 }
cdaltond416a5b2015-06-23 13:23:44 -0700186 if (!this->gl->fenceSyncSupport()) {
187 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
188 "Timings might not be accurate.\n", this->config.name);
189 }
tomhudsond968a6f2015-03-26 11:28:06 -0700190 return true;
191 }
192 void fillOptions(ResultsWriter* log) override {
193 const GrGLubyte* version;
194 SK_GL_RET(*this->gl, version, GetString(GR_GL_VERSION));
195 log->configOption("GL_VERSION", (const char*)(version));
196
197 SK_GL_RET(*this->gl, version, GetString(GR_GL_RENDERER));
198 log->configOption("GL_RENDERER", (const char*) version);
199
200 SK_GL_RET(*this->gl, version, GetString(GR_GL_VENDOR));
201 log->configOption("GL_VENDOR", (const char*) version);
202
203 SK_GL_RET(*this->gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
204 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
205 }
206};
mtkleind75c4662015-04-30 07:11:22 -0700207
tomhudsond968a6f2015-03-26 11:28:06 -0700208#endif
209
tomhudson75a0ebb2015-03-27 12:11:44 -0700210static double time(int loops, Benchmark* bench, Target* target) {
211 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700212 if (canvas) {
213 canvas->clear(SK_ColorWHITE);
214 }
joshualitt8a6697a2015-09-30 12:11:07 -0700215 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700216 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700217 canvas = target->beginTiming(canvas);
218 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700219 if (canvas) {
220 canvas->flush();
221 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700222 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700223 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700224 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700225 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700226}
227
mtkleinf3723212014-06-25 14:08:00 -0700228static double estimate_timer_overhead() {
229 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700230 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700231 double start = now_ms();
232 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700233 }
234 return overhead / FLAGS_overheadLoops;
235}
236
reed53249782014-10-10 09:09:52 -0700237static int detect_forever_loops(int loops) {
238 // look for a magic run-forever value
239 if (loops < 0) {
240 loops = SK_MaxS32;
241 }
242 return loops;
243}
244
mtklein55b0ffc2014-07-17 08:38:23 -0700245static int clamp_loops(int loops) {
246 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800247 SkDebugf("ERROR: clamping loops from %d to 1. "
248 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700249 return 1;
250 }
251 if (loops > FLAGS_maxLoops) {
252 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
253 return FLAGS_maxLoops;
254 }
255 return loops;
256}
257
tomhudsond968a6f2015-03-26 11:28:06 -0700258static bool write_canvas_png(Target* target, const SkString& filename) {
259
bsalomon6eb03cc2014-08-07 14:28:50 -0700260 if (filename.isEmpty()) {
261 return false;
262 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700263 if (target->getCanvas() &&
264 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700265 return false;
266 }
tomhudsond968a6f2015-03-26 11:28:06 -0700267
bsalomon6eb03cc2014-08-07 14:28:50 -0700268 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700269
270 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700271 return false;
272 }
tomhudsond968a6f2015-03-26 11:28:06 -0700273
bsalomon6eb03cc2014-08-07 14:28:50 -0700274 SkString dir = SkOSPath::Dirname(filename.c_str());
275 if (!sk_mkdir(dir.c_str())) {
276 SkDebugf("Can't make dir %s.\n", dir.c_str());
277 return false;
278 }
279 SkFILEWStream stream(filename.c_str());
280 if (!stream.isValid()) {
281 SkDebugf("Can't write %s.\n", filename.c_str());
282 return false;
283 }
284 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
285 SkDebugf("Can't encode a PNG.\n");
286 return false;
287 }
288 return true;
289}
290
291static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700292static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700293 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700294 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700295 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700296 int loops = bench->calculateLoops(FLAGS_loops);
297 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700298 while (bench_plus_overhead < overhead) {
299 if (round++ == FLAGS_maxCalibrationAttempts) {
300 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700301 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700302 return kFailedLoops;
303 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700304 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700305 }
mtklein2069e222014-08-04 13:57:39 -0700306 }
mtkleinf3723212014-06-25 14:08:00 -0700307
mtkleinbb6a0282014-07-01 08:43:42 -0700308 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700309 // We'll pick N to make timer overhead negligible:
310 //
mtkleinbb6a0282014-07-01 08:43:42 -0700311 // overhead
312 // ------------------------- < FLAGS_overheadGoal
313 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700314 //
mtkleinbb6a0282014-07-01 08:43:42 -0700315 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700316 //
317 // Doing some math, we get:
318 //
mtkleinbb6a0282014-07-01 08:43:42 -0700319 // (overhead / FLAGS_overheadGoal) - overhead
320 // ------------------------------------------ < N
321 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700322 //
323 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700324 if (kAutoTuneLoops == loops) {
325 const double numer = overhead / FLAGS_overheadGoal - overhead;
326 const double denom = bench_plus_overhead - overhead;
327 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700328 loops = clamp_loops(loops);
329 } else {
330 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700331 }
mtkleinbb6a0282014-07-01 08:43:42 -0700332
mtkleinbb6a0282014-07-01 08:43:42 -0700333 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700334}
335
cdaltone1b89582015-06-25 19:17:08 -0700336static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700337 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700338 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700339 if (kAutoTuneLoops == loops) {
340 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700341 double elapsed = 0;
342 do {
mtklein527930f2014-11-06 08:04:34 -0800343 if (1<<30 == loops) {
344 // We're about to wrap. Something's wrong with the bench.
345 loops = 0;
346 break;
347 }
mtkleina189ccd2014-07-14 12:28:47 -0700348 loops *= 2;
349 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700350 // _this_ round, not still timing last round.
351 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700352 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700353 }
354 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700355
mtkleina189ccd2014-07-14 12:28:47 -0700356 // We've overshot at least a little. Scale back linearly.
357 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700358 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700359
tomhudsond968a6f2015-03-26 11:28:06 -0700360 // Make sure we're not still timing our calibration.
361 target->fence();
reed53249782014-10-10 09:09:52 -0700362 } else {
363 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700364 }
mtkleinbb6a0282014-07-01 08:43:42 -0700365
366 // Pretty much the same deal as the calibration: do some warmup to make
367 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700368 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700369 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700370 }
mtkleinbb6a0282014-07-01 08:43:42 -0700371
mtkleinbb6a0282014-07-01 08:43:42 -0700372 return loops;
373}
mtkleinbb6a0282014-07-01 08:43:42 -0700374
375static SkString to_lower(const char* str) {
376 SkString lower(str);
377 for (size_t i = 0; i < lower.size(); i++) {
378 lower[i] = tolower(lower[i]);
379 }
380 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700381}
382
bsalomonc2553372014-07-22 13:09:05 -0700383static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700384 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700385 if (to_lower(FLAGS_config[i]).equals(name)) {
386 return true;
mtkleinf3723212014-06-25 14:08:00 -0700387 }
388 }
bsalomonc2553372014-07-22 13:09:05 -0700389 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700390}
391
bsalomonc2553372014-07-22 13:09:05 -0700392#if SK_SUPPORT_GPU
393static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
kkinnunen5219fd92015-12-10 06:28:13 -0800394 GrContextFactory::GLContextOptions ctxOptions,
bsalomonc2553372014-07-22 13:09:05 -0700395 int sampleCnt) {
396 if (!is_cpu_config_allowed(name)) {
397 return false;
398 }
kkinnunen3e980c32015-12-23 01:33:00 -0800399 if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOptions)) {
bsalomon76228632015-05-29 08:02:10 -0700400 return sampleCnt <= ctx->caps()->maxSampleCount();
bsalomonc2553372014-07-22 13:09:05 -0700401 }
402 return false;
403}
404#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700405
bsalomonc2553372014-07-22 13:09:05 -0700406#if SK_SUPPORT_GPU
407#define kBogusGLContextType GrContextFactory::kNative_GLContextType
kkinnunen5219fd92015-12-10 06:28:13 -0800408#define kBogusGLContextOptions GrContextFactory::kNone_GLContextOptions
bsalomonc2553372014-07-22 13:09:05 -0700409#else
410#define kBogusGLContextType 0
kkinnunen5219fd92015-12-10 06:28:13 -0800411#define kBogusGLContextOptions 0
mtkleine714e752014-07-31 12:13:48 -0700412#endif
bsalomonc2553372014-07-22 13:09:05 -0700413
414// Append all configs that are enabled and supported.
415static void create_configs(SkTDArray<Config>* configs) {
kkinnunen5219fd92015-12-10 06:28:13 -0800416 #define CPU_CONFIG(name, backend, color, alpha) \
417 if (is_cpu_config_allowed(#name)) { \
418 Config config = { #name, Benchmark::backend, color, alpha, 0, \
419 kBogusGLContextType, kBogusGLContextOptions, \
420 false }; \
421 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700422 }
mtkleine714e752014-07-31 12:13:48 -0700423
mtklein40b32be2014-07-09 08:46:49 -0700424 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700425 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
426 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
427 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700428 }
mtkleinbb6a0282014-07-01 08:43:42 -0700429
430#if SK_SUPPORT_GPU
kkinnunen5219fd92015-12-10 06:28:13 -0800431 #define GPU_CONFIG(name, ctxType, ctxOptions, samples, useDFText) \
432 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, \
433 GrContextFactory::ctxOptions, samples)) { \
bsalomonc2553372014-07-22 13:09:05 -0700434 Config config = { \
435 #name, \
436 Benchmark::kGPU_Backend, \
437 kN32_SkColorType, \
438 kPremul_SkAlphaType, \
439 samples, \
jvanverth4736e142014-11-07 07:12:46 -0800440 GrContextFactory::ctxType, \
kkinnunen5219fd92015-12-10 06:28:13 -0800441 GrContextFactory::ctxOptions, \
jvanverth4736e142014-11-07 07:12:46 -0800442 useDFText }; \
bsalomonc2553372014-07-22 13:09:05 -0700443 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700444 }
mtkleine714e752014-07-31 12:13:48 -0700445
mtklein40b32be2014-07-09 08:46:49 -0700446 if (FLAGS_gpu) {
kkinnunen5219fd92015-12-10 06:28:13 -0800447 GPU_CONFIG(gpu, kNative_GLContextType, kNone_GLContextOptions, 0, false)
448 GPU_CONFIG(msaa4, kNative_GLContextType, kNone_GLContextOptions, 4, false)
449 GPU_CONFIG(msaa16, kNative_GLContextType, kNone_GLContextOptions, 16, false)
450 GPU_CONFIG(nvprmsaa4, kNative_GLContextType, kEnableNVPR_GLContextOptions, 4, false)
451 GPU_CONFIG(nvprmsaa16, kNative_GLContextType, kEnableNVPR_GLContextOptions, 16, false)
452 GPU_CONFIG(gpudft, kNative_GLContextType, kNone_GLContextOptions, 0, true)
453 GPU_CONFIG(debug, kDebug_GLContextType, kNone_GLContextOptions, 0, false)
454 GPU_CONFIG(nullgpu, kNull_GLContextType, kNone_GLContextOptions, 0, false)
kkinnunen3e980c32015-12-23 01:33:00 -0800455#if SK_ANGLE
456#ifdef SK_BUILD_FOR_WIN
kkinnunen5219fd92015-12-10 06:28:13 -0800457 GPU_CONFIG(angle, kANGLE_GLContextType, kNone_GLContextOptions, 0, false)
kkinnunen3e980c32015-12-23 01:33:00 -0800458#endif
kkinnunen5219fd92015-12-10 06:28:13 -0800459 GPU_CONFIG(angle-gl, kANGLE_GL_GLContextType, kNone_GLContextOptions, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700460#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800461#if SK_COMMAND_BUFFER
kkinnunen5219fd92015-12-10 06:28:13 -0800462 GPU_CONFIG(commandbuffer, kCommandBuffer_GLContextType, kNone_GLContextOptions, 0, false)
hendrikw885bf092015-08-27 10:38:39 -0700463#endif
cdaltond416a5b2015-06-23 13:23:44 -0700464#if SK_MESA
kkinnunen5219fd92015-12-10 06:28:13 -0800465 GPU_CONFIG(mesa, kMESA_GLContextType, kNone_GLContextOptions, 0, false)
cdaltond416a5b2015-06-23 13:23:44 -0700466#endif
mtklein40b32be2014-07-09 08:46:49 -0700467 }
mtkleinbb6a0282014-07-01 08:43:42 -0700468#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700469
470#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
471 if (is_cpu_config_allowed("hwui")) {
472 Config config = { "hwui", Benchmark::kHWUI_Backend, kRGBA_8888_SkColorType,
kkinnunen5219fd92015-12-10 06:28:13 -0800473 kPremul_SkAlphaType, 0, kBogusGLContextType, kBogusGLContextOptions,
474 false };
tomhudsond968a6f2015-03-26 11:28:06 -0700475 configs->push(config);
476 }
477#endif
mtkleinf3723212014-06-25 14:08:00 -0700478}
479
halcanary96fcdcc2015-08-27 07:41:13 -0700480// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700481static Target* is_enabled(Benchmark* bench, const Config& config) {
482 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700483 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700484 }
485
reede5ea5002014-09-03 11:54:58 -0700486 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
487 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700488
halcanary96fcdcc2015-08-27 07:41:13 -0700489 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700490
tomhudsond968a6f2015-03-26 11:28:06 -0700491 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700492#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700493 case Benchmark::kGPU_Backend:
494 target = new GPUTarget(config);
495 break;
bsalomonc2553372014-07-22 13:09:05 -0700496#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700497#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
498 case Benchmark::kHWUI_Backend:
499 target = new HWUITarget(config, bench);
500 break;
501#endif
502 default:
503 target = new Target(config);
504 break;
505 }
bsalomonc2553372014-07-22 13:09:05 -0700506
tomhudsond968a6f2015-03-26 11:28:06 -0700507 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700508 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700509 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700510 }
511 return target;
512}
513
msarett5cb48852015-11-06 08:56:32 -0800514static bool valid_brd_bench(SkData* encoded, SkBitmapRegionDecoder::Strategy strategy,
msarett7f691442015-09-22 11:56:16 -0700515 SkColorType colorType, uint32_t sampleSize, uint32_t minOutputSize, int* width,
516 int* height) {
msarett5cb48852015-11-06 08:56:32 -0800517 SkAutoTDelete<SkBitmapRegionDecoder> brd(
518 SkBitmapRegionDecoder::Create(encoded, strategy));
msarett7f691442015-09-22 11:56:16 -0700519 if (nullptr == brd.get()) {
520 // This is indicates that subset decoding is not supported for a particular image format.
521 return false;
522 }
523
msarett35e5d1b2015-10-27 12:50:25 -0700524 SkBitmap bitmap;
525 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, brd->width(), brd->height()),
526 1, colorType, false)) {
527 return false;
528 }
msarett7f691442015-09-22 11:56:16 -0700529
530 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
531 (uint32_t) brd->height()) {
532 // This indicates that the image is not large enough to decode a
533 // minOutputSize x minOutputSize subset at the given sampleSize.
534 return false;
535 }
536
537 // Set the image width and height. The calling code will use this to choose subsets to decode.
538 *width = brd->width();
539 *height = brd->height();
540 return true;
541}
542
egdaniel3bf92062015-06-26 08:12:46 -0700543static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700544 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700545#if SK_SUPPORT_GPU
546 if (FLAGS_abandonGpuContext) {
547 gGrFactory->abandonContexts();
548 }
549 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
550 gGrFactory->destroyContexts();
551 }
552#endif
553}
554
mtkleine714e752014-07-31 12:13:48 -0700555class BenchmarkStream {
556public:
mtklein92007582014-08-01 07:46:52 -0700557 BenchmarkStream() : fBenches(BenchRegistry::Head())
558 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700559 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700560 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800561 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800562 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700563 , fCurrentCodec(0)
msarett95f192d2015-02-13 09:05:41 -0800564 , fCurrentImage(0)
msarett7f691442015-09-22 11:56:16 -0700565 , fCurrentBRDImage(0)
msarett95f192d2015-02-13 09:05:41 -0800566 , fCurrentColorType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700567 , fCurrentSubsetType(0)
msarett7f691442015-09-22 11:56:16 -0700568 , fCurrentBRDStrategy(0)
569 , fCurrentBRDSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700570 , fCurrentAnimSKP(0) {
mtklein92007582014-08-01 07:46:52 -0700571 for (int i = 0; i < FLAGS_skps.count(); i++) {
572 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
573 fSKPs.push_back() = FLAGS_skps[i];
574 } else {
575 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
576 SkString path;
577 while (it.next(&path)) {
578 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
579 }
580 }
581 }
mtkleine714e752014-07-31 12:13:48 -0700582
mtklein92007582014-08-01 07:46:52 -0700583 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
584 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
585 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
586 exit(1);
587 }
588
589 for (int i = 0; i < FLAGS_scales.count(); i++) {
590 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
591 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
592 exit(1);
593 }
594 }
robertphillips5b693772014-11-21 06:19:36 -0800595
cdalton63a82852015-06-29 14:06:10 -0700596 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
597 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700598 exit(1);
599 }
600
robertphillips5b693772014-11-21 06:19:36 -0800601 if (FLAGS_mpd) {
602 fUseMPDs.push_back() = true;
603 }
mtkleinc751ecb2015-06-15 08:56:38 -0700604 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700605
msarett95f192d2015-02-13 09:05:41 -0800606 // Prepare the images for decoding
607 for (int i = 0; i < FLAGS_images.count(); i++) {
608 const char* flag = FLAGS_images[i];
609 if (sk_isdir(flag)) {
610 // If the value passed in is a directory, add all the images
611 SkOSFile::Iter it(flag);
612 SkString file;
613 while (it.next(&file)) {
614 fImages.push_back() = SkOSPath::Join(flag, file.c_str());
615 }
616 } else if (sk_exists(flag)) {
617 // Also add the value if it is a single image
618 fImages.push_back() = flag;
619 }
620 }
mtklein95553d92015-03-12 08:24:21 -0700621
msarett95f192d2015-02-13 09:05:41 -0800622 // Choose the candidate color types for image decoding
623 const SkColorType colorTypes[] =
msarettb23e6aa2015-06-09 13:56:10 -0700624 { kN32_SkColorType,
625 kRGB_565_SkColorType,
626 kAlpha_8_SkColorType,
627 kIndex_8_SkColorType,
628 kGray_8_SkColorType };
msarett74deb982015-10-20 16:45:56 -0700629 fColorTypes.reset(colorTypes, SK_ARRAY_COUNT(colorTypes));
mtklein92007582014-08-01 07:46:52 -0700630 }
631
mtkleinfd731ce2014-09-10 12:19:30 -0700632 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
633 // Not strictly necessary, as it will be checked again later,
634 // but helps to avoid a lot of pointless work if we're going to skip it.
635 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
636 return false;
637 }
638
scroggoa1193e42015-01-21 12:09:53 -0800639 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -0700640 if (stream.get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700641 SkDebugf("Could not read %s.\n", path);
642 return false;
643 }
644
mtklein57f27bd2015-02-09 11:58:41 -0800645 pic->reset(SkPicture::CreateFromStream(stream.get()));
halcanary96fcdcc2015-08-27 07:41:13 -0700646 if (pic->get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700647 SkDebugf("Could not read %s as an SkPicture.\n", path);
648 return false;
649 }
650 return true;
651 }
652
mtklein92007582014-08-01 07:46:52 -0700653 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700654 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700655 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700656 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700657 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700658 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700659 return bench;
660 }
mtklein92007582014-08-01 07:46:52 -0700661
mtkleine714e752014-07-31 12:13:48 -0700662 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -0700663 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700664 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800665 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700666 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700667 fBenchType = "micro";
halcanary385fe4d2015-08-26 13:07:48 -0700668 return new GMBench(gm.detach());
mtkleine714e752014-07-31 12:13:48 -0700669 }
670 }
mtklein92007582014-08-01 07:46:52 -0700671
mtkleinfd731ce2014-09-10 12:19:30 -0700672 // First add all .skps as RecordingBenches.
673 while (fCurrentRecording < fSKPs.count()) {
674 const SkString& path = fSKPs[fCurrentRecording++];
675 SkAutoTUnref<SkPicture> pic;
676 if (!ReadPicture(path.c_str(), &pic)) {
677 continue;
678 }
679 SkString name = SkOSPath::Basename(path.c_str());
680 fSourceType = "skp";
681 fBenchType = "recording";
bsalomon0aa5cea2014-12-15 09:13:35 -0800682 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic));
mtklein051e56d2014-12-04 08:46:51 -0800683 fSKPOps = pic->approximateOpCount();
halcanary385fe4d2015-08-26 13:07:48 -0700684 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700685 }
686
687 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700688 while (fCurrentScale < fScales.count()) {
689 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800690 const SkString& path = fSKPs[fCurrentSKP];
mtkleinfd731ce2014-09-10 12:19:30 -0700691 SkAutoTUnref<SkPicture> pic;
692 if (!ReadPicture(path.c_str(), &pic)) {
robertphillips5b693772014-11-21 06:19:36 -0800693 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700694 continue;
695 }
robertphillips5b693772014-11-21 06:19:36 -0800696
697 while (fCurrentUseMPD < fUseMPDs.count()) {
698 if (FLAGS_bbh) {
699 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
700 SkRTreeFactory factory;
701 SkPictureRecorder recorder;
702 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
703 pic->playback(recorder.beginRecording(pic->cullRect().width(),
704 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800705 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800706 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
robertphillips5b693772014-11-21 06:19:36 -0800707 pic.reset(recorder.endRecording());
708 }
709 SkString name = SkOSPath::Basename(path.c_str());
710 fSourceType = "skp";
711 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700712 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
713 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700714 }
robertphillips5b693772014-11-21 06:19:36 -0800715 fCurrentUseMPD = 0;
716 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700717 }
718 fCurrentSKP = 0;
719 fCurrentScale++;
720 }
721
joshualitt261c3ad2015-04-27 09:16:57 -0700722 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700723 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700724 while (fCurrentAnimSKP < fSKPs.count()) {
725 const SkString& path = fSKPs[fCurrentAnimSKP];
726 SkAutoTUnref<SkPicture> pic;
727 if (!ReadPicture(path.c_str(), &pic)) {
728 fCurrentAnimSKP++;
729 continue;
730 }
731
732 fCurrentAnimSKP++;
733 SkString name = SkOSPath::Basename(path.c_str());
cdalton63a82852015-06-29 14:06:10 -0700734 SkAutoTUnref<SKPAnimationBench::Animation> animation(
735 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
halcanary385fe4d2015-08-26 13:07:48 -0700736 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation,
737 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700738 }
739 }
740
scroggo60869a42015-04-01 12:09:17 -0700741 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700742 fSourceType = "image";
743 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700744 const SkString& path = fImages[fCurrentCodec];
745 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
746 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700747 if (!codec) {
748 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700749 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700750 continue;
751 }
scroggo21027992015-04-02 13:22:38 -0700752
scroggo60869a42015-04-01 12:09:17 -0700753 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700754 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo60869a42015-04-01 12:09:17 -0700755 fCurrentColorType++;
scroggo21027992015-04-02 13:22:38 -0700756
scroggo60869a42015-04-01 12:09:17 -0700757 // Make sure we can decode to this color type.
scroggo60869a42015-04-01 12:09:17 -0700758 SkImageInfo info = codec->getInfo().makeColorType(colorType);
scroggo21027992015-04-02 13:22:38 -0700759 SkAlphaType alphaType;
760 if (!SkColorTypeValidateAlphaType(colorType, info.alphaType(),
761 &alphaType)) {
762 continue;
763 }
764 if (alphaType != info.alphaType()) {
765 info = info.makeAlphaType(alphaType);
766 }
767
768 const size_t rowBytes = info.minRowBytes();
769 SkAutoMalloc storage(info.getSafeSize(rowBytes));
770
771 // Used if fCurrentColorType is kIndex_8_SkColorType
772 int colorCount = 256;
773 SkPMColor colors[256];
774
scroggoeb602a52015-07-09 08:16:03 -0700775 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700776 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700777 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700778 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700779 case SkCodec::kSuccess:
780 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700781 return new CodecBench(SkOSPath::Basename(path.c_str()),
782 encoded, colorType);
scroggoeb602a52015-07-09 08:16:03 -0700783 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700784 // This is okay. Not all conversions are valid.
785 break;
scroggo60869a42015-04-01 12:09:17 -0700786 default:
787 // This represents some sort of failure.
788 SkASSERT(false);
789 break;
790 }
791 }
792 fCurrentColorType = 0;
793 }
794
msarett95f192d2015-02-13 09:05:41 -0800795 // Run the DecodingBenches
796 while (fCurrentImage < fImages.count()) {
scroggo303fa352015-10-05 11:03:34 -0700797 fSourceType = "image";
798 fBenchType = "skimagedecoder";
msarett95f192d2015-02-13 09:05:41 -0800799 while (fCurrentColorType < fColorTypes.count()) {
800 const SkString& path = fImages[fCurrentImage];
801 SkColorType colorType = fColorTypes[fCurrentColorType];
802 fCurrentColorType++;
scroggo60869a42015-04-01 12:09:17 -0700803 // Check if the image decodes to the right color type
804 // before creating the benchmark
msarett95f192d2015-02-13 09:05:41 -0800805 SkBitmap bitmap;
806 if (SkImageDecoder::DecodeFile(path.c_str(), &bitmap,
scroggo60869a42015-04-01 12:09:17 -0700807 colorType, SkImageDecoder::kDecodePixels_Mode)
808 && bitmap.colorType() == colorType) {
msarett95f192d2015-02-13 09:05:41 -0800809 return new DecodingBench(path, colorType);
810 }
811 }
812 fCurrentColorType = 0;
813 fCurrentImage++;
814 }
815
msarett7f691442015-09-22 11:56:16 -0700816 // Run the BRDBenches
817 // We will benchmark multiple BRD strategies.
scroggo303fa352015-10-05 11:03:34 -0700818 static const struct {
msarett5cb48852015-11-06 08:56:32 -0800819 SkBitmapRegionDecoder::Strategy fStrategy;
msarett0459c942015-11-10 14:52:13 -0800820 const char* fName;
scroggo303fa352015-10-05 11:03:34 -0700821 } strategies[] = {
msarett0459c942015-11-10 14:52:13 -0800822 { SkBitmapRegionDecoder::kCanvas_Strategy, "BRD_canvas" },
msarett5cb48852015-11-06 08:56:32 -0800823 { SkBitmapRegionDecoder::kAndroidCodec_Strategy, "BRD_android_codec" },
msarett7f691442015-09-22 11:56:16 -0700824 };
825
826 // We intend to create benchmarks that model the use cases in
827 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
828 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
829 // the image. For that reason, we will benchmark decodes in five representative locations
830 // in the image. Additionally, this use case utilizes power of two scaling, so we will
831 // test on power of two sample sizes. The output tile is always 512x512, so, when a
832 // sampleSize is used, the size of the subset that is decoded is always
833 // (sampleSize*512)x(sampleSize*512).
834 // There are a few good reasons to only test on power of two sample sizes at this time:
835 // JPEG decodes using kOriginal_Strategy are broken for non-powers of two.
halcanary6950de62015-11-07 05:29:00 -0800836 // https://bug.skia.org/4319
msarett7f691442015-09-22 11:56:16 -0700837 // All use cases we are aware of only scale by powers of two.
838 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
839 // these tests are sufficient to provide good coverage of our scaling options.
scroggo501b7342015-11-03 07:55:11 -0800840 const uint32_t sampleSizes[] = { 1, 2, 4, 8, 16, 32, 64 };
msarett7f691442015-09-22 11:56:16 -0700841 const uint32_t minOutputSize = 512;
842 while (fCurrentBRDImage < fImages.count()) {
843 while (fCurrentBRDStrategy < (int) SK_ARRAY_COUNT(strategies)) {
scroggo303fa352015-10-05 11:03:34 -0700844 fSourceType = "image";
845 fBenchType = strategies[fCurrentBRDStrategy].fName;
scroggo860e8a62015-10-15 07:51:28 -0700846
847 const SkString& path = fImages[fCurrentBRDImage];
msarett5cb48852015-11-06 08:56:32 -0800848 const SkBitmapRegionDecoder::Strategy strategy =
scroggo860e8a62015-10-15 07:51:28 -0700849 strategies[fCurrentBRDStrategy].fStrategy;
850
msarett7f691442015-09-22 11:56:16 -0700851 while (fCurrentColorType < fColorTypes.count()) {
852 while (fCurrentBRDSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
853 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700854
855
msarett7f691442015-09-22 11:56:16 -0700856 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
857 const SkColorType colorType = fColorTypes[fCurrentColorType];
858 uint32_t sampleSize = sampleSizes[fCurrentBRDSampleSize];
859 int currentSubsetType = fCurrentSubsetType++;
860
861 int width = 0;
862 int height = 0;
863 if (!valid_brd_bench(encoded.get(), strategy, colorType, sampleSize,
864 minOutputSize, &width, &height)) {
865 break;
866 }
867
868 SkString basename = SkOSPath::Basename(path.c_str());
869 SkIRect subset;
870 const uint32_t subsetSize = sampleSize * minOutputSize;
871 switch (currentSubsetType) {
872 case kTopLeft_SubsetType:
873 basename.append("_TopLeft");
874 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
875 break;
876 case kTopRight_SubsetType:
877 basename.append("_TopRight");
878 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
879 subsetSize);
880 break;
881 case kMiddle_SubsetType:
882 basename.append("_Middle");
883 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
884 (height - subsetSize) / 2, subsetSize, subsetSize);
885 break;
886 case kBottomLeft_SubsetType:
887 basename.append("_BottomLeft");
888 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
889 subsetSize);
890 break;
891 case kBottomRight_SubsetType:
892 basename.append("_BottomRight");
893 subset = SkIRect::MakeXYWH(width - subsetSize,
894 height - subsetSize, subsetSize, subsetSize);
895 break;
896 default:
897 SkASSERT(false);
898 }
899
900 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
901 strategy, colorType, sampleSize, subset);
902 }
903 fCurrentSubsetType = 0;
904 fCurrentBRDSampleSize++;
905 }
906 fCurrentBRDSampleSize = 0;
907 fCurrentColorType++;
908 }
909 fCurrentColorType = 0;
910 fCurrentBRDStrategy++;
911 }
912 fCurrentBRDStrategy = 0;
913 fCurrentBRDImage++;
914 }
915
halcanary96fcdcc2015-08-27 07:41:13 -0700916 return nullptr;
mtkleine714e752014-07-31 12:13:48 -0700917 }
mtklein92007582014-08-01 07:46:52 -0700918
919 void fillCurrentOptions(ResultsWriter* log) const {
920 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700921 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700922 if (0 == strcmp(fSourceType, "skp")) {
923 log->configOption("clip",
924 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
925 fClip.fRight, fClip.fBottom).c_str());
mtklein4dfdbb12015-10-20 07:45:29 -0700926 SK_ALWAYSBREAK(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -0700927 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -0800928 if (fCurrentUseMPD > 0) {
929 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
930 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
931 }
mtklein92007582014-08-01 07:46:52 -0700932 }
mtklein051e56d2014-12-04 08:46:51 -0800933 if (0 == strcmp(fBenchType, "recording")) {
934 log->metric("bytes", fSKPBytes);
935 log->metric("ops", fSKPOps);
936 }
mtklein92007582014-08-01 07:46:52 -0700937 }
938
mtkleine714e752014-07-31 12:13:48 -0700939private:
msarettb23e6aa2015-06-09 13:56:10 -0700940 enum SubsetType {
941 kTopLeft_SubsetType = 0,
942 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -0700943 kMiddle_SubsetType = 2,
944 kBottomLeft_SubsetType = 3,
945 kBottomRight_SubsetType = 4,
946 kTranslate_SubsetType = 5,
947 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -0700948 kLast_SubsetType = kZoom_SubsetType,
949 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -0700950 };
951
mtkleine714e752014-07-31 12:13:48 -0700952 const BenchRegistry* fBenches;
953 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700954 SkIRect fClip;
955 SkTArray<SkScalar> fScales;
956 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -0800957 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -0800958 SkTArray<SkString> fImages;
msarett74deb982015-10-20 16:45:56 -0700959 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -0700960 SkScalar fZoomMax;
961 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -0700962
mtklein051e56d2014-12-04 08:46:51 -0800963 double fSKPBytes, fSKPOps;
964
mtkleinfd731ce2014-09-10 12:19:30 -0700965 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
966 const char* fBenchType; // How we bench it: micro, recording, playback, ...
967 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700968 int fCurrentScale;
969 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -0800970 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -0700971 int fCurrentCodec;
msarett95f192d2015-02-13 09:05:41 -0800972 int fCurrentImage;
msarett7f691442015-09-22 11:56:16 -0700973 int fCurrentBRDImage;
msarett95f192d2015-02-13 09:05:41 -0800974 int fCurrentColorType;
msarettb23e6aa2015-06-09 13:56:10 -0700975 int fCurrentSubsetType;
msarett7f691442015-09-22 11:56:16 -0700976 int fCurrentBRDStrategy;
977 int fCurrentBRDSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -0700978 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -0700979};
980
msarettc149f0e2016-01-04 11:35:43 -0800981// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
982// This prints something every once in a while so that it knows we're still working.
983static void start_keepalive() {
984 struct Loop {
985 static void forever(void*) {
986 for (;;) {
987 static const int kSec = 1200;
988 #if defined(SK_BUILD_FOR_WIN)
989 Sleep(kSec * 1000);
990 #else
991 sleep(kSec);
992 #endif
993 SkDebugf("\nBenchmarks still running...\n");
994 }
995 }
996 };
997 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
998 intentionallyLeaked->start();
999}
1000
jcgregorio3b27ade2014-11-13 08:06:40 -08001001int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001002int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001003 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001004 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001005 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001006
krajcevski69a55602014-08-13 10:46:31 -07001007#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001008 GrContextOptions grContextOpts;
krajcevski12b35442014-08-13 12:06:26 -07001009 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
halcanary385fe4d2015-08-26 13:07:48 -07001010 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001011#endif
1012
bsalomon06cddec2014-10-24 10:40:50 -07001013 if (FLAGS_veryVerbose) {
1014 FLAGS_verbose = true;
1015 }
1016
bsalomon6eb03cc2014-08-07 14:28:50 -07001017 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001018 FLAGS_samples = 1;
1019 FLAGS_gpuFrameLag = 0;
1020 }
1021
bsalomon6eb03cc2014-08-07 14:28:50 -07001022 if (!FLAGS_writePath.isEmpty()) {
1023 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1024 if (!sk_mkdir(FLAGS_writePath[0])) {
1025 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001026 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001027 }
1028 }
1029
halcanary385fe4d2015-08-26 13:07:48 -07001030 SkAutoTDelete<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001031 if (!FLAGS_outResultsFile.isEmpty()) {
halcanary385fe4d2015-08-26 13:07:48 -07001032 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein60317d0f2014-07-14 11:30:37 -07001033 }
mtklein1915b622014-08-20 11:45:00 -07001034
1035 if (1 == FLAGS_properties.count() % 2) {
1036 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1037 return 1;
1038 }
1039 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1040 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1041 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001042
1043 if (1 == FLAGS_key.count() % 2) {
1044 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1045 return 1;
1046 }
1047 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001048 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001049 }
mtklein60317d0f2014-07-14 11:30:37 -07001050
mtkleinf3723212014-06-25 14:08:00 -07001051 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001052 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001053
cdaltone1b89582015-06-25 19:17:08 -07001054 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001055
bsalomon6eb03cc2014-08-07 14:28:50 -07001056 if (kAutoTuneLoops != FLAGS_loops) {
1057 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001058 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001059 SkDebugf("! -> high variance, ? -> moderate variance\n");
1060 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001061 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001062 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001063 } else {
mtkleind75c4662015-04-30 07:11:22 -07001064 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001065 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001066 }
1067
bsalomonc2553372014-07-22 13:09:05 -07001068 SkTDArray<Config> configs;
1069 create_configs(&configs);
1070
msarettc149f0e2016-01-04 11:35:43 -08001071 if (FLAGS_keepAlive) {
1072 start_keepalive();
1073 }
1074
mtkleine070c2b2014-10-14 08:40:43 -07001075 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001076 BenchmarkStream benchStream;
1077 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001078 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001079 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001080 continue;
1081 }
1082
egdaniel3bf92062015-06-26 08:12:46 -07001083 if (!configs.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -07001084 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001085 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001086 }
egdaniel3bf92062015-06-26 08:12:46 -07001087 for (int i = 0; i < configs.count(); ++i) {
1088 Target* target = is_enabled(b, configs[i]);
1089 if (!target) {
1090 continue;
1091 }
mtkleinf3723212014-06-25 14:08:00 -07001092
halcanary96fcdcc2015-08-27 07:41:13 -07001093 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001094 SkCanvas* canvas = target->getCanvas();
1095 const char* config = target->config.name;
1096
1097 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001098 bench->perCanvasPreDraw(canvas);
1099
cdaltone1b89582015-06-25 19:17:08 -07001100 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001101 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001102 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1103 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001104
mtkleinbbba1682015-10-28 11:36:30 -07001105 if (FLAGS_ms) {
1106 samples.reset();
1107 auto stop = now_ms() + FLAGS_ms;
1108 do {
1109 samples.push_back(time(loops, bench, target) / loops);
1110 } while (now_ms() < stop);
1111 } else {
cdaltone1b89582015-06-25 19:17:08 -07001112 samples.reset(FLAGS_samples);
1113 for (int s = 0; s < FLAGS_samples; s++) {
egdaniel3bf92062015-06-26 08:12:46 -07001114 samples[s] = time(loops, bench, target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001115 }
cdaltone1b89582015-06-25 19:17:08 -07001116 }
mtkleinf3723212014-06-25 14:08:00 -07001117
joshualitte45c81c2015-12-02 09:05:37 -08001118#if SK_SUPPORT_GPU
1119 SkTArray<SkString> keys;
1120 SkTArray<double> values;
1121 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1122 if (gpuStatsDump) {
1123 // TODO cache stats
1124 bench->getGpuStats(canvas, &keys, &values);
1125 }
1126#endif
1127
robertphillips5b693772014-11-21 06:19:36 -08001128 bench->perCanvasPostDraw(canvas);
1129
egdaniel3bf92062015-06-26 08:12:46 -07001130 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001131 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001132 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001133 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001134 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001135 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001136 }
1137
1138 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001139 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001140 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001141 continue;
1142 }
1143
cdaltone1b89582015-06-25 19:17:08 -07001144 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001145 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001146 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001147 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001148 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001149 log->metric("min_ms", stats.min);
joshualitte45c81c2015-12-02 09:05:37 -08001150#if SK_SUPPORT_GPU
1151 if (gpuStatsDump) {
1152 // dump to json, only SKPBench currently returns valid keys / values
1153 SkASSERT(keys.count() == values.count());
1154 for (int i = 0; i < keys.count(); i++) {
1155 log->metric(keys[i].c_str(), values[i]);
1156 }
1157 }
1158#endif
1159
mtkleine070c2b2014-10-14 08:40:43 -07001160 if (runs++ % FLAGS_flushEvery == 0) {
1161 log->flush();
1162 }
mtklein60317d0f2014-07-14 11:30:37 -07001163
bsalomon6eb03cc2014-08-07 14:28:50 -07001164 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001165 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001166 config = ""; // Only print the config if we run the same bench on more than one.
1167 }
mtkleind75c4662015-04-30 07:11:22 -07001168 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1169 , sk_tools::getCurrResidentSetSizeMB()
1170 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001171 , bench->getUniqueName()
1172 , config);
mtkleinf3723212014-06-25 14:08:00 -07001173 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001174 const char* mark = " ";
1175 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1176 if (stddev_percent > 5) mark = "?";
1177 if (stddev_percent > 10) mark = "!";
1178
1179 SkDebugf("%10.2f %s\t%s\t%s\n",
1180 stats.median*1e3, mark, bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001181 } else {
1182 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001183 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1184 , sk_tools::getCurrResidentSetSizeMB()
1185 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001186 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001187 , HUMANIZE(stats.min)
1188 , HUMANIZE(stats.median)
1189 , HUMANIZE(stats.mean)
1190 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001191 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001192 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001193 , config
mtklein96289052014-09-10 12:05:59 -07001194 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001195 );
1196 }
joshualitte45c81c2015-12-02 09:05:37 -08001197
bsalomonb12ea412015-02-02 21:19:50 -08001198#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001199 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001200 GrContext* context = gGrFactory->get(configs[i].ctxType,
kkinnunen3e980c32015-12-23 01:33:00 -08001201 configs[i].ctxOptions);
kkinnunen5219fd92015-12-10 06:28:13 -08001202 context->printCacheStats();
1203 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001204 }
1205#endif
joshualitte45c81c2015-12-02 09:05:37 -08001206
cdalton2c56ba52015-06-26 13:32:53 -07001207 if (FLAGS_verbose) {
1208 SkDebugf("Samples: ");
1209 for (int i = 0; i < samples.count(); i++) {
1210 SkDebugf("%s ", HUMANIZE(samples[i]));
1211 }
1212 SkDebugf("%s\n", bench->getUniqueName());
1213 }
egdaniel3bf92062015-06-26 08:12:46 -07001214 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001215 }
mtkleinf3723212014-06-25 14:08:00 -07001216 }
1217
mtkleine1091452014-12-04 10:47:02 -08001218 log->bench("memory_usage", 0,0);
1219 log->config("meta");
1220 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1221
joshualitte0b19d42015-03-26 10:41:02 -07001222#if SK_SUPPORT_GPU
1223 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1224 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001225 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001226#endif
1227
mtkleinf3723212014-06-25 14:08:00 -07001228 return 0;
1229}
1230
jcgregorio3b27ade2014-11-13 08:06:40 -08001231#if !defined SK_BUILD_FOR_IOS
1232int main(int argc, char** argv) {
1233 SkCommandLineFlags::Parse(argc, argv);
1234 return nanobench_main();
1235}
1236#endif