blob: c2bcb4950ac6b6ac6f5896412deb3cb8ccf44fee [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"
mtkleine714e752014-07-31 12:13:48 -070017#include "GMBench.h"
mtkleinafb43792014-08-19 15:55:55 -070018#include "ProcStats.h"
mtklein60317d0f2014-07-14 11:30:37 -070019#include "ResultsWriter.h"
mtkleinfd731ce2014-09-10 12:19:30 -070020#include "RecordingBench.h"
joshualitt261c3ad2015-04-27 09:16:57 -070021#include "SKPAnimationBench.h"
mtklein92007582014-08-01 07:46:52 -070022#include "SKPBench.h"
mtkleinf3723212014-06-25 14:08:00 -070023#include "Stats.h"
mtkleinf3723212014-06-25 14:08:00 -070024
msarett5cb48852015-11-06 08:56:32 -080025#include "SkBitmapRegionDecoder.h"
mtklein6838d852014-10-29 14:15:10 -070026#include "SkBBoxHierarchy.h"
mtkleinf3723212014-06-25 14:08:00 -070027#include "SkCanvas.h"
scroggo60869a42015-04-01 12:09:17 -070028#include "SkCodec.h"
caryclark17f0b6d2014-07-22 10:15:34 -070029#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080030#include "SkCommonFlagsConfig.h"
msarett95f192d2015-02-13 09:05:41 -080031#include "SkData.h"
mtkleinf3723212014-06-25 14:08:00 -070032#include "SkForceLinking.h"
33#include "SkGraphics.h"
mtklein20840502014-08-21 15:51:22 -070034#include "SkOSFile.h"
35#include "SkPictureRecorder.h"
mtklein051e56d2014-12-04 08:46:51 -080036#include "SkPictureUtils.h"
mtkleinf3723212014-06-25 14:08:00 -070037#include "SkString.h"
38#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080039#include "SkTaskGroup.h"
msarettc149f0e2016-01-04 11:35:43 -080040#include "SkThreadUtils.h"
mtkleinf3723212014-06-25 14:08:00 -070041
bungeman60e0fee2015-08-26 05:15:46 -070042#include <stdlib.h>
43
scroggo38ce0a72016-01-07 07:28:47 -080044#ifndef SK_BUILD_FOR_WIN32
45 #include <unistd.h>
46#endif
47
tomhudsond968a6f2015-03-26 11:28:06 -070048#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
49 #include "nanobenchAndroid.h"
50#endif
51
mtkleinbb6a0282014-07-01 08:43:42 -070052#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070053 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070054 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070055 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070056 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070057#endif
58
bsalomon682c2692015-05-22 14:01:46 -070059 struct GrContextOptions;
60
mtkleinf3723212014-06-25 14:08:00 -070061__SK_FORCE_IMAGE_DECODER_LINKING;
62
reed53249782014-10-10 09:09:52 -070063static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070064
mtkleinb5110422014-08-07 15:20:02 -070065static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070066#ifdef SK_DEBUG
67 1;
mtkleina189ccd2014-07-14 12:28:47 -070068#else
bsalomon6eb03cc2014-08-07 14:28:50 -070069 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070070#endif
71
bsalomon6eb03cc2014-08-07 14:28:50 -070072static SkString loops_help_txt() {
73 SkString help;
74 help.printf("Number of times to run each bench. Set this to %d to auto-"
75 "tune for each bench. Timings are only reported when auto-tuning.",
76 kAutoTuneLoops);
77 return help;
78}
79
cdaltone1b89582015-06-25 19:17:08 -070080static SkString to_string(int n) {
81 SkString str;
82 str.appendS32(n);
83 return str;
84}
85
bsalomon6eb03cc2014-08-07 14:28:50 -070086DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
87
mtkleinf3723212014-06-25 14:08:00 -070088DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -070089DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -070090DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
91DEFINE_double(overheadGoal, 0.0001,
92 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070093DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -070094DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070095DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
96 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070097
mtklein60317d0f2014-07-14 11:30:37 -070098DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -070099DEFINE_int32(maxCalibrationAttempts, 3,
100 "Try up to this many times to guess loops for a bench, or skip the bench.");
101DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700102DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
103DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700104DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
105 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700106DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
robertphillips5b693772014-11-21 06:19:36 -0800107DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700108DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700109DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800110DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800111DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800112DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
msarettc149f0e2016-01-04 11:35:43 -0800113DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
mtklein92007582014-08-01 07:46:52 -0700114
mtkleinbbba1682015-10-28 11:36:30 -0700115static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
116
mtkleinf3723212014-06-25 14:08:00 -0700117static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700118 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800119 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700120}
mtklein55b0ffc2014-07-17 08:38:23 -0700121#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700122
tomhudsond968a6f2015-03-26 11:28:06 -0700123bool Target::init(SkImageInfo info, Benchmark* bench) {
124 if (Benchmark::kRaster_Backend == config.backend) {
125 this->surface.reset(SkSurface::NewRaster(info));
126 if (!this->surface.get()) {
127 return false;
128 }
129 }
130 return true;
131}
132bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700133 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700134 if (!canvas) {
135 return false;
136 }
137 bmp->setInfo(canvas->imageInfo());
138 if (!canvas->readPixels(bmp, 0, 0)) {
139 SkDebugf("Can't read canvas pixels.\n");
140 return false;
141 }
142 return true;
143}
144
145#if SK_SUPPORT_GPU
146struct GPUTarget : public Target {
halcanary96fcdcc2015-08-27 07:41:13 -0700147 explicit GPUTarget(const Config& c) : Target(c), gl(nullptr) { }
tomhudsond968a6f2015-03-26 11:28:06 -0700148 SkGLContext* gl;
149
150 void setup() override {
151 this->gl->makeCurrent();
152 // Make sure we're done with whatever came before.
153 SK_GL(*this->gl, Finish());
154 }
155 void endTiming() override {
156 if (this->gl) {
157 SK_GL(*this->gl, Flush());
joshualitt01836ad2016-01-20 13:09:12 -0800158 this->gl->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700159 }
160 }
161 void fence() override {
162 SK_GL(*this->gl, Finish());
163 }
mtkleind75c4662015-04-30 07:11:22 -0700164
cdaltond416a5b2015-06-23 13:23:44 -0700165 bool needsFrameTiming(int* maxFrameLag) const override {
166 if (!this->gl->getMaxGpuFrameLag(maxFrameLag)) {
167 // Frame lag is unknown.
168 *maxFrameLag = FLAGS_gpuFrameLag;
169 }
170 return true;
171 }
tomhudsond968a6f2015-03-26 11:28:06 -0700172 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700173 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
174 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700175 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
kkinnunen5219fd92015-12-10 06:28:13 -0800176 this->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(this->config.ctxType,
kkinnunen5219fd92015-12-10 06:28:13 -0800177 this->config.ctxOptions),
tomhudsond968a6f2015-03-26 11:28:06 -0700178 SkSurface::kNo_Budgeted, info,
179 this->config.samples, &props));
kkinnunen3e980c32015-12-23 01:33:00 -0800180 this->gl = gGrFactory->getContextInfo(this->config.ctxType,
kkinnunen34058002016-01-06 23:49:30 -0800181 this->config.ctxOptions).fGLContext;
tomhudsond968a6f2015-03-26 11:28:06 -0700182 if (!this->surface.get()) {
183 return false;
184 }
cdaltond416a5b2015-06-23 13:23:44 -0700185 if (!this->gl->fenceSyncSupport()) {
186 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800187 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700188 }
tomhudsond968a6f2015-03-26 11:28:06 -0700189 return true;
190 }
191 void fillOptions(ResultsWriter* log) override {
192 const GrGLubyte* version;
193 SK_GL_RET(*this->gl, version, GetString(GR_GL_VERSION));
194 log->configOption("GL_VERSION", (const char*)(version));
195
196 SK_GL_RET(*this->gl, version, GetString(GR_GL_RENDERER));
197 log->configOption("GL_RENDERER", (const char*) version);
198
199 SK_GL_RET(*this->gl, version, GetString(GR_GL_VENDOR));
200 log->configOption("GL_VENDOR", (const char*) version);
201
202 SK_GL_RET(*this->gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
203 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
204 }
205};
mtkleind75c4662015-04-30 07:11:22 -0700206
tomhudsond968a6f2015-03-26 11:28:06 -0700207#endif
208
tomhudson75a0ebb2015-03-27 12:11:44 -0700209static double time(int loops, Benchmark* bench, Target* target) {
210 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700211 if (canvas) {
212 canvas->clear(SK_ColorWHITE);
213 }
joshualitt8a6697a2015-09-30 12:11:07 -0700214 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700215 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700216 canvas = target->beginTiming(canvas);
217 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700218 if (canvas) {
219 canvas->flush();
220 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700221 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700222 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700223 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700224 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700225}
226
mtkleinf3723212014-06-25 14:08:00 -0700227static double estimate_timer_overhead() {
228 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700229 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700230 double start = now_ms();
231 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700232 }
233 return overhead / FLAGS_overheadLoops;
234}
235
reed53249782014-10-10 09:09:52 -0700236static int detect_forever_loops(int loops) {
237 // look for a magic run-forever value
238 if (loops < 0) {
239 loops = SK_MaxS32;
240 }
241 return loops;
242}
243
mtklein55b0ffc2014-07-17 08:38:23 -0700244static int clamp_loops(int loops) {
245 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800246 SkDebugf("ERROR: clamping loops from %d to 1. "
247 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700248 return 1;
249 }
250 if (loops > FLAGS_maxLoops) {
251 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
252 return FLAGS_maxLoops;
253 }
254 return loops;
255}
256
tomhudsond968a6f2015-03-26 11:28:06 -0700257static bool write_canvas_png(Target* target, const SkString& filename) {
258
bsalomon6eb03cc2014-08-07 14:28:50 -0700259 if (filename.isEmpty()) {
260 return false;
261 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700262 if (target->getCanvas() &&
263 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700264 return false;
265 }
tomhudsond968a6f2015-03-26 11:28:06 -0700266
bsalomon6eb03cc2014-08-07 14:28:50 -0700267 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700268
269 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700270 return false;
271 }
tomhudsond968a6f2015-03-26 11:28:06 -0700272
bsalomon6eb03cc2014-08-07 14:28:50 -0700273 SkString dir = SkOSPath::Dirname(filename.c_str());
274 if (!sk_mkdir(dir.c_str())) {
275 SkDebugf("Can't make dir %s.\n", dir.c_str());
276 return false;
277 }
278 SkFILEWStream stream(filename.c_str());
279 if (!stream.isValid()) {
280 SkDebugf("Can't write %s.\n", filename.c_str());
281 return false;
282 }
283 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
284 SkDebugf("Can't encode a PNG.\n");
285 return false;
286 }
287 return true;
288}
289
290static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700291static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700292 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700293 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700294 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700295 int loops = bench->calculateLoops(FLAGS_loops);
296 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700297 while (bench_plus_overhead < overhead) {
298 if (round++ == FLAGS_maxCalibrationAttempts) {
299 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700300 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700301 return kFailedLoops;
302 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700303 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700304 }
mtklein2069e222014-08-04 13:57:39 -0700305 }
mtkleinf3723212014-06-25 14:08:00 -0700306
mtkleinbb6a0282014-07-01 08:43:42 -0700307 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700308 // We'll pick N to make timer overhead negligible:
309 //
mtkleinbb6a0282014-07-01 08:43:42 -0700310 // overhead
311 // ------------------------- < FLAGS_overheadGoal
312 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700313 //
mtkleinbb6a0282014-07-01 08:43:42 -0700314 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700315 //
316 // Doing some math, we get:
317 //
mtkleinbb6a0282014-07-01 08:43:42 -0700318 // (overhead / FLAGS_overheadGoal) - overhead
319 // ------------------------------------------ < N
320 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700321 //
322 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700323 if (kAutoTuneLoops == loops) {
324 const double numer = overhead / FLAGS_overheadGoal - overhead;
325 const double denom = bench_plus_overhead - overhead;
326 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700327 loops = clamp_loops(loops);
328 } else {
329 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700330 }
mtkleinbb6a0282014-07-01 08:43:42 -0700331
mtkleinbb6a0282014-07-01 08:43:42 -0700332 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700333}
334
cdaltone1b89582015-06-25 19:17:08 -0700335static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700336 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700337 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700338 if (kAutoTuneLoops == loops) {
339 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700340 double elapsed = 0;
341 do {
mtklein527930f2014-11-06 08:04:34 -0800342 if (1<<30 == loops) {
343 // We're about to wrap. Something's wrong with the bench.
344 loops = 0;
345 break;
346 }
mtkleina189ccd2014-07-14 12:28:47 -0700347 loops *= 2;
348 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700349 // _this_ round, not still timing last round.
350 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700351 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700352 }
353 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700354
mtkleina189ccd2014-07-14 12:28:47 -0700355 // We've overshot at least a little. Scale back linearly.
356 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700357 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700358
tomhudsond968a6f2015-03-26 11:28:06 -0700359 // Make sure we're not still timing our calibration.
360 target->fence();
reed53249782014-10-10 09:09:52 -0700361 } else {
362 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700363 }
mtkleinbb6a0282014-07-01 08:43:42 -0700364
365 // Pretty much the same deal as the calibration: do some warmup to make
366 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700367 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700368 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700369 }
mtkleinbb6a0282014-07-01 08:43:42 -0700370
mtkleinbb6a0282014-07-01 08:43:42 -0700371 return loops;
372}
mtkleinbb6a0282014-07-01 08:43:42 -0700373
bsalomonc2553372014-07-22 13:09:05 -0700374#if SK_SUPPORT_GPU
375#define kBogusGLContextType GrContextFactory::kNative_GLContextType
kkinnunen5219fd92015-12-10 06:28:13 -0800376#define kBogusGLContextOptions GrContextFactory::kNone_GLContextOptions
bsalomonc2553372014-07-22 13:09:05 -0700377#else
378#define kBogusGLContextType 0
kkinnunen5219fd92015-12-10 06:28:13 -0800379#define kBogusGLContextOptions 0
mtkleine714e752014-07-31 12:13:48 -0700380#endif
bsalomonc2553372014-07-22 13:09:05 -0700381
svaisanenc47635e2016-01-28 06:05:43 -0800382static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
383
384#if SK_SUPPORT_GPU
385 if (const auto* gpuConfig = config->asConfigGpu()) {
386 if (!FLAGS_gpu)
387 return;
388
389 const auto ctxOptions = gpuConfig->getUseNVPR() ? GrContextFactory::kEnableNVPR_GLContextOptions
390 : GrContextFactory::kNone_GLContextOptions;
391 const auto ctxType = gpuConfig->getContextType();
392 const auto sampleCount = gpuConfig->getSamples();
393
394 if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOptions)) {
395 const auto maxSampleCount = ctx->caps()->maxSampleCount();
396 if (sampleCount > ctx->caps()->maxSampleCount()) {
397 SkDebugf("Configuration sample count %d exceeds maximum %d.\n",
398 sampleCount, maxSampleCount);
399 return;
400 }
401 } else {
402 SkDebugf("No context was available matching config type and options.\n");
403 return;
404 }
405
406 Config target = {
407 config->getTag(),
408 Benchmark::kGPU_Backend,
409 kN32_SkColorType,
410 kPremul_SkAlphaType,
411 sampleCount,
412 ctxType,
413 ctxOptions,
414 false };
415
416 configs->push_back(target);
417 return;
418 }
419#endif
420
421 #define CPU_CONFIG(name, backend, color, alpha) \
422 if (config->getTag().equals(#name)) { \
423 Config config = { SkString(#name), Benchmark::backend, color, alpha, 0, \
424 kBogusGLContextType, kBogusGLContextOptions, \
425 false }; \
426 configs->push_back(config); \
427 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700428 }
mtkleine714e752014-07-31 12:13:48 -0700429
mtklein40b32be2014-07-09 08:46:49 -0700430 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700431 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
432 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
433 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700434 }
mtkleinbb6a0282014-07-01 08:43:42 -0700435
svaisanenc47635e2016-01-28 06:05:43 -0800436 #undef CPU_CONFIG
tomhudsond968a6f2015-03-26 11:28:06 -0700437
438#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
djsollen817c72a2016-01-28 13:57:02 -0800439 if (config->getTag().equals("hwui")) {
440 Config config = { SkString("hwui"), Benchmark::kHWUI_Backend, kRGBA_8888_SkColorType,
kkinnunen5219fd92015-12-10 06:28:13 -0800441 kPremul_SkAlphaType, 0, kBogusGLContextType, kBogusGLContextOptions,
442 false };
svaisanenc47635e2016-01-28 06:05:43 -0800443 configs->push_back(config);
tomhudsond968a6f2015-03-26 11:28:06 -0700444 }
445#endif
mtkleinf3723212014-06-25 14:08:00 -0700446}
447
svaisanenc47635e2016-01-28 06:05:43 -0800448// Append all configs that are enabled and supported.
449void create_configs(SkTArray<Config>* configs) {
450 SkCommandLineConfigArray array;
451 ParseConfigs(FLAGS_config, &array);
452 for (int i = 0; i < array.count(); ++i) {
453 create_config(array[i], configs);
454 }
455}
456
halcanary96fcdcc2015-08-27 07:41:13 -0700457// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700458static Target* is_enabled(Benchmark* bench, const Config& config) {
459 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700460 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700461 }
462
reede5ea5002014-09-03 11:54:58 -0700463 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
464 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700465
halcanary96fcdcc2015-08-27 07:41:13 -0700466 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700467
tomhudsond968a6f2015-03-26 11:28:06 -0700468 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700469#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700470 case Benchmark::kGPU_Backend:
471 target = new GPUTarget(config);
472 break;
bsalomonc2553372014-07-22 13:09:05 -0700473#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700474#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
475 case Benchmark::kHWUI_Backend:
476 target = new HWUITarget(config, bench);
477 break;
478#endif
479 default:
480 target = new Target(config);
481 break;
482 }
bsalomonc2553372014-07-22 13:09:05 -0700483
tomhudsond968a6f2015-03-26 11:28:06 -0700484 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700485 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700486 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700487 }
488 return target;
489}
490
msarett5cb48852015-11-06 08:56:32 -0800491static bool valid_brd_bench(SkData* encoded, SkBitmapRegionDecoder::Strategy strategy,
msarett7f691442015-09-22 11:56:16 -0700492 SkColorType colorType, uint32_t sampleSize, uint32_t minOutputSize, int* width,
493 int* height) {
msarett5cb48852015-11-06 08:56:32 -0800494 SkAutoTDelete<SkBitmapRegionDecoder> brd(
495 SkBitmapRegionDecoder::Create(encoded, strategy));
msarett7f691442015-09-22 11:56:16 -0700496 if (nullptr == brd.get()) {
497 // This is indicates that subset decoding is not supported for a particular image format.
498 return false;
499 }
500
msarett35e5d1b2015-10-27 12:50:25 -0700501 SkBitmap bitmap;
502 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, brd->width(), brd->height()),
503 1, colorType, false)) {
504 return false;
505 }
msarett7f691442015-09-22 11:56:16 -0700506
507 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
508 (uint32_t) brd->height()) {
509 // This indicates that the image is not large enough to decode a
510 // minOutputSize x minOutputSize subset at the given sampleSize.
511 return false;
512 }
513
514 // Set the image width and height. The calling code will use this to choose subsets to decode.
515 *width = brd->width();
516 *height = brd->height();
517 return true;
518}
519
egdaniel3bf92062015-06-26 08:12:46 -0700520static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700521 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700522#if SK_SUPPORT_GPU
523 if (FLAGS_abandonGpuContext) {
524 gGrFactory->abandonContexts();
525 }
526 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
527 gGrFactory->destroyContexts();
528 }
529#endif
530}
531
mtkleine714e752014-07-31 12:13:48 -0700532class BenchmarkStream {
533public:
mtklein92007582014-08-01 07:46:52 -0700534 BenchmarkStream() : fBenches(BenchRegistry::Head())
535 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700536 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700537 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800538 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800539 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700540 , fCurrentCodec(0)
msarett7f691442015-09-22 11:56:16 -0700541 , fCurrentBRDImage(0)
msarett95f192d2015-02-13 09:05:41 -0800542 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800543 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700544 , fCurrentSubsetType(0)
msarett7f691442015-09-22 11:56:16 -0700545 , fCurrentBRDStrategy(0)
546 , fCurrentBRDSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700547 , fCurrentAnimSKP(0) {
mtklein92007582014-08-01 07:46:52 -0700548 for (int i = 0; i < FLAGS_skps.count(); i++) {
549 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
550 fSKPs.push_back() = FLAGS_skps[i];
551 } else {
552 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
553 SkString path;
554 while (it.next(&path)) {
555 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
556 }
557 }
558 }
mtkleine714e752014-07-31 12:13:48 -0700559
mtklein92007582014-08-01 07:46:52 -0700560 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
561 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
562 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
563 exit(1);
564 }
565
566 for (int i = 0; i < FLAGS_scales.count(); i++) {
567 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
568 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
569 exit(1);
570 }
571 }
robertphillips5b693772014-11-21 06:19:36 -0800572
cdalton63a82852015-06-29 14:06:10 -0700573 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
574 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700575 exit(1);
576 }
577
robertphillips5b693772014-11-21 06:19:36 -0800578 if (FLAGS_mpd) {
579 fUseMPDs.push_back() = true;
580 }
mtkleinc751ecb2015-06-15 08:56:38 -0700581 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700582
msarett95f192d2015-02-13 09:05:41 -0800583 // Prepare the images for decoding
scroggoad38ed62016-01-29 14:41:55 -0800584 for (int i = 0; i < FLAGS_images.count(); i++) {
585 const char* flag = FLAGS_images[i];
586 if (sk_isdir(flag)) {
587 // If the value passed in is a directory, add all the images
588 SkOSFile::Iter it(flag);
589 SkString file;
590 while (it.next(&file)) {
591 fImages.push_back() = SkOSPath::Join(flag, file.c_str());
592 }
593 } else if (sk_exists(flag)) {
594 // Also add the value if it is a single image
595 fImages.push_back() = flag;
596 }
msarett95f192d2015-02-13 09:05:41 -0800597 }
mtklein95553d92015-03-12 08:24:21 -0700598
msarett95f192d2015-02-13 09:05:41 -0800599 // Choose the candidate color types for image decoding
600 const SkColorType colorTypes[] =
msarettb23e6aa2015-06-09 13:56:10 -0700601 { kN32_SkColorType,
602 kRGB_565_SkColorType,
603 kAlpha_8_SkColorType,
604 kIndex_8_SkColorType,
605 kGray_8_SkColorType };
msarett74deb982015-10-20 16:45:56 -0700606 fColorTypes.reset(colorTypes, SK_ARRAY_COUNT(colorTypes));
mtklein92007582014-08-01 07:46:52 -0700607 }
608
mtkleinfd731ce2014-09-10 12:19:30 -0700609 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
610 // Not strictly necessary, as it will be checked again later,
611 // but helps to avoid a lot of pointless work if we're going to skip it.
612 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
613 return false;
614 }
615
scroggoa1193e42015-01-21 12:09:53 -0800616 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -0700617 if (stream.get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700618 SkDebugf("Could not read %s.\n", path);
619 return false;
620 }
621
mtklein57f27bd2015-02-09 11:58:41 -0800622 pic->reset(SkPicture::CreateFromStream(stream.get()));
halcanary96fcdcc2015-08-27 07:41:13 -0700623 if (pic->get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700624 SkDebugf("Could not read %s as an SkPicture.\n", path);
625 return false;
626 }
627 return true;
628 }
629
mtklein92007582014-08-01 07:46:52 -0700630 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700631 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700632 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700633 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700634 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700635 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700636 return bench;
637 }
mtklein92007582014-08-01 07:46:52 -0700638
mtkleine714e752014-07-31 12:13:48 -0700639 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -0700640 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700641 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800642 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700643 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700644 fBenchType = "micro";
halcanary385fe4d2015-08-26 13:07:48 -0700645 return new GMBench(gm.detach());
mtkleine714e752014-07-31 12:13:48 -0700646 }
647 }
mtklein92007582014-08-01 07:46:52 -0700648
mtkleinfd731ce2014-09-10 12:19:30 -0700649 // First add all .skps as RecordingBenches.
650 while (fCurrentRecording < fSKPs.count()) {
651 const SkString& path = fSKPs[fCurrentRecording++];
652 SkAutoTUnref<SkPicture> pic;
653 if (!ReadPicture(path.c_str(), &pic)) {
654 continue;
655 }
656 SkString name = SkOSPath::Basename(path.c_str());
657 fSourceType = "skp";
658 fBenchType = "recording";
bsalomon0aa5cea2014-12-15 09:13:35 -0800659 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic));
mtklein051e56d2014-12-04 08:46:51 -0800660 fSKPOps = pic->approximateOpCount();
halcanary385fe4d2015-08-26 13:07:48 -0700661 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700662 }
663
664 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700665 while (fCurrentScale < fScales.count()) {
666 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800667 const SkString& path = fSKPs[fCurrentSKP];
mtkleinfd731ce2014-09-10 12:19:30 -0700668 SkAutoTUnref<SkPicture> pic;
669 if (!ReadPicture(path.c_str(), &pic)) {
robertphillips5b693772014-11-21 06:19:36 -0800670 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700671 continue;
672 }
robertphillips5b693772014-11-21 06:19:36 -0800673
674 while (fCurrentUseMPD < fUseMPDs.count()) {
675 if (FLAGS_bbh) {
676 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
677 SkRTreeFactory factory;
678 SkPictureRecorder recorder;
679 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
680 pic->playback(recorder.beginRecording(pic->cullRect().width(),
681 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800682 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800683 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
robertphillips5b693772014-11-21 06:19:36 -0800684 pic.reset(recorder.endRecording());
685 }
686 SkString name = SkOSPath::Basename(path.c_str());
687 fSourceType = "skp";
688 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700689 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
690 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700691 }
robertphillips5b693772014-11-21 06:19:36 -0800692 fCurrentUseMPD = 0;
693 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700694 }
695 fCurrentSKP = 0;
696 fCurrentScale++;
697 }
698
joshualitt261c3ad2015-04-27 09:16:57 -0700699 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700700 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700701 while (fCurrentAnimSKP < fSKPs.count()) {
702 const SkString& path = fSKPs[fCurrentAnimSKP];
703 SkAutoTUnref<SkPicture> pic;
704 if (!ReadPicture(path.c_str(), &pic)) {
705 fCurrentAnimSKP++;
706 continue;
707 }
708
709 fCurrentAnimSKP++;
710 SkString name = SkOSPath::Basename(path.c_str());
cdalton63a82852015-06-29 14:06:10 -0700711 SkAutoTUnref<SKPAnimationBench::Animation> animation(
712 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
halcanary385fe4d2015-08-26 13:07:48 -0700713 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation,
714 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700715 }
716 }
717
scroggo60869a42015-04-01 12:09:17 -0700718 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700719 fSourceType = "image";
720 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700721 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800722 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
723 continue;
724 }
scroggo60869a42015-04-01 12:09:17 -0700725 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
726 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700727 if (!codec) {
728 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700729 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700730 continue;
731 }
scroggo21027992015-04-02 13:22:38 -0700732
scroggo60869a42015-04-01 12:09:17 -0700733 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700734 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700735
msarettc7796b92016-01-07 14:20:20 -0800736 SkAlphaType alphaType = codec->getInfo().alphaType();
737 switch (alphaType) {
738 case kOpaque_SkAlphaType:
739 // We only need to test one alpha type (opaque).
740 fCurrentColorType++;
741 break;
742 case kUnpremul_SkAlphaType:
743 case kPremul_SkAlphaType:
744 if (0 == fCurrentAlphaType) {
745 // Test unpremul first.
746 alphaType = kUnpremul_SkAlphaType;
747 fCurrentAlphaType++;
748 } else {
749 // Test premul.
750 alphaType = kPremul_SkAlphaType;
751 fCurrentAlphaType = 0;
752 fCurrentColorType++;
753 }
754 break;
755 default:
756 SkASSERT(false);
757 fCurrentColorType++;
758 break;
scroggo21027992015-04-02 13:22:38 -0700759 }
760
msarettc7796b92016-01-07 14:20:20 -0800761 // Make sure we can decode to this color type and alpha type.
762 SkImageInfo info =
763 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700764 const size_t rowBytes = info.minRowBytes();
765 SkAutoMalloc storage(info.getSafeSize(rowBytes));
766
767 // Used if fCurrentColorType is kIndex_8_SkColorType
768 int colorCount = 256;
769 SkPMColor colors[256];
770
scroggoeb602a52015-07-09 08:16:03 -0700771 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700772 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700773 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700774 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700775 case SkCodec::kSuccess:
776 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700777 return new CodecBench(SkOSPath::Basename(path.c_str()),
msarettc7796b92016-01-07 14:20:20 -0800778 encoded, colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700779 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700780 // This is okay. Not all conversions are valid.
781 break;
scroggo60869a42015-04-01 12:09:17 -0700782 default:
783 // This represents some sort of failure.
784 SkASSERT(false);
785 break;
786 }
787 }
788 fCurrentColorType = 0;
789 }
790
msarett7f691442015-09-22 11:56:16 -0700791 // Run the BRDBenches
792 // We will benchmark multiple BRD strategies.
scroggo303fa352015-10-05 11:03:34 -0700793 static const struct {
msarett5cb48852015-11-06 08:56:32 -0800794 SkBitmapRegionDecoder::Strategy fStrategy;
msarett0459c942015-11-10 14:52:13 -0800795 const char* fName;
scroggo303fa352015-10-05 11:03:34 -0700796 } strategies[] = {
msarett0459c942015-11-10 14:52:13 -0800797 { SkBitmapRegionDecoder::kCanvas_Strategy, "BRD_canvas" },
msarett5cb48852015-11-06 08:56:32 -0800798 { SkBitmapRegionDecoder::kAndroidCodec_Strategy, "BRD_android_codec" },
msarett7f691442015-09-22 11:56:16 -0700799 };
800
801 // We intend to create benchmarks that model the use cases in
802 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
803 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
804 // the image. For that reason, we will benchmark decodes in five representative locations
805 // in the image. Additionally, this use case utilizes power of two scaling, so we will
806 // test on power of two sample sizes. The output tile is always 512x512, so, when a
807 // sampleSize is used, the size of the subset that is decoded is always
808 // (sampleSize*512)x(sampleSize*512).
809 // There are a few good reasons to only test on power of two sample sizes at this time:
810 // JPEG decodes using kOriginal_Strategy are broken for non-powers of two.
halcanary6950de62015-11-07 05:29:00 -0800811 // https://bug.skia.org/4319
msarett7f691442015-09-22 11:56:16 -0700812 // All use cases we are aware of only scale by powers of two.
813 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
814 // these tests are sufficient to provide good coverage of our scaling options.
scroggo501b7342015-11-03 07:55:11 -0800815 const uint32_t sampleSizes[] = { 1, 2, 4, 8, 16, 32, 64 };
msarett7f691442015-09-22 11:56:16 -0700816 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800817 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
818 const SkString& path = fImages[fCurrentBRDImage];
819 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
820 continue;
821 }
msarett7f691442015-09-22 11:56:16 -0700822 while (fCurrentBRDStrategy < (int) SK_ARRAY_COUNT(strategies)) {
scroggo303fa352015-10-05 11:03:34 -0700823 fSourceType = "image";
824 fBenchType = strategies[fCurrentBRDStrategy].fName;
scroggo860e8a62015-10-15 07:51:28 -0700825
msarett5cb48852015-11-06 08:56:32 -0800826 const SkBitmapRegionDecoder::Strategy strategy =
scroggo860e8a62015-10-15 07:51:28 -0700827 strategies[fCurrentBRDStrategy].fStrategy;
828
msarett7f691442015-09-22 11:56:16 -0700829 while (fCurrentColorType < fColorTypes.count()) {
830 while (fCurrentBRDSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
831 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700832
833
msarett7f691442015-09-22 11:56:16 -0700834 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
835 const SkColorType colorType = fColorTypes[fCurrentColorType];
836 uint32_t sampleSize = sampleSizes[fCurrentBRDSampleSize];
837 int currentSubsetType = fCurrentSubsetType++;
838
839 int width = 0;
840 int height = 0;
841 if (!valid_brd_bench(encoded.get(), strategy, colorType, sampleSize,
842 minOutputSize, &width, &height)) {
843 break;
844 }
845
846 SkString basename = SkOSPath::Basename(path.c_str());
847 SkIRect subset;
848 const uint32_t subsetSize = sampleSize * minOutputSize;
849 switch (currentSubsetType) {
850 case kTopLeft_SubsetType:
851 basename.append("_TopLeft");
852 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
853 break;
854 case kTopRight_SubsetType:
855 basename.append("_TopRight");
856 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
857 subsetSize);
858 break;
859 case kMiddle_SubsetType:
860 basename.append("_Middle");
861 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
862 (height - subsetSize) / 2, subsetSize, subsetSize);
863 break;
864 case kBottomLeft_SubsetType:
865 basename.append("_BottomLeft");
866 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
867 subsetSize);
868 break;
869 case kBottomRight_SubsetType:
870 basename.append("_BottomRight");
871 subset = SkIRect::MakeXYWH(width - subsetSize,
872 height - subsetSize, subsetSize, subsetSize);
873 break;
874 default:
875 SkASSERT(false);
876 }
877
878 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
879 strategy, colorType, sampleSize, subset);
880 }
881 fCurrentSubsetType = 0;
882 fCurrentBRDSampleSize++;
883 }
884 fCurrentBRDSampleSize = 0;
885 fCurrentColorType++;
886 }
887 fCurrentColorType = 0;
888 fCurrentBRDStrategy++;
889 }
890 fCurrentBRDStrategy = 0;
msarett7f691442015-09-22 11:56:16 -0700891 }
892
halcanary96fcdcc2015-08-27 07:41:13 -0700893 return nullptr;
mtkleine714e752014-07-31 12:13:48 -0700894 }
mtklein92007582014-08-01 07:46:52 -0700895
896 void fillCurrentOptions(ResultsWriter* log) const {
897 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700898 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700899 if (0 == strcmp(fSourceType, "skp")) {
900 log->configOption("clip",
901 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
902 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -0800903 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -0700904 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -0800905 if (fCurrentUseMPD > 0) {
906 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
907 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
908 }
mtklein92007582014-08-01 07:46:52 -0700909 }
mtklein051e56d2014-12-04 08:46:51 -0800910 if (0 == strcmp(fBenchType, "recording")) {
911 log->metric("bytes", fSKPBytes);
912 log->metric("ops", fSKPOps);
913 }
mtklein92007582014-08-01 07:46:52 -0700914 }
915
mtkleine714e752014-07-31 12:13:48 -0700916private:
msarettb23e6aa2015-06-09 13:56:10 -0700917 enum SubsetType {
918 kTopLeft_SubsetType = 0,
919 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -0700920 kMiddle_SubsetType = 2,
921 kBottomLeft_SubsetType = 3,
922 kBottomRight_SubsetType = 4,
923 kTranslate_SubsetType = 5,
924 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -0700925 kLast_SubsetType = kZoom_SubsetType,
926 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -0700927 };
928
mtkleine714e752014-07-31 12:13:48 -0700929 const BenchRegistry* fBenches;
930 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700931 SkIRect fClip;
932 SkTArray<SkScalar> fScales;
933 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -0800934 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -0800935 SkTArray<SkString> fImages;
msarett74deb982015-10-20 16:45:56 -0700936 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -0700937 SkScalar fZoomMax;
938 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -0700939
mtklein051e56d2014-12-04 08:46:51 -0800940 double fSKPBytes, fSKPOps;
941
mtkleinfd731ce2014-09-10 12:19:30 -0700942 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
943 const char* fBenchType; // How we bench it: micro, recording, playback, ...
944 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700945 int fCurrentScale;
946 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -0800947 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -0700948 int fCurrentCodec;
msarett7f691442015-09-22 11:56:16 -0700949 int fCurrentBRDImage;
msarett95f192d2015-02-13 09:05:41 -0800950 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -0800951 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -0700952 int fCurrentSubsetType;
msarett7f691442015-09-22 11:56:16 -0700953 int fCurrentBRDStrategy;
954 int fCurrentBRDSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -0700955 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -0700956};
957
msarettc149f0e2016-01-04 11:35:43 -0800958// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
959// This prints something every once in a while so that it knows we're still working.
960static void start_keepalive() {
961 struct Loop {
962 static void forever(void*) {
963 for (;;) {
964 static const int kSec = 1200;
965 #if defined(SK_BUILD_FOR_WIN)
966 Sleep(kSec * 1000);
967 #else
968 sleep(kSec);
969 #endif
970 SkDebugf("\nBenchmarks still running...\n");
971 }
972 }
973 };
974 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
975 intentionallyLeaked->start();
976}
977
jcgregorio3b27ade2014-11-13 08:06:40 -0800978int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700979int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800980 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -0700981 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -0700982 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -0700983
krajcevski69a55602014-08-13 10:46:31 -0700984#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -0700985 GrContextOptions grContextOpts;
krajcevski12b35442014-08-13 12:06:26 -0700986 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
halcanary385fe4d2015-08-26 13:07:48 -0700987 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -0700988#endif
989
bsalomon06cddec2014-10-24 10:40:50 -0700990 if (FLAGS_veryVerbose) {
991 FLAGS_verbose = true;
992 }
993
bsalomon6eb03cc2014-08-07 14:28:50 -0700994 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700995 FLAGS_samples = 1;
996 FLAGS_gpuFrameLag = 0;
997 }
998
bsalomon6eb03cc2014-08-07 14:28:50 -0700999 if (!FLAGS_writePath.isEmpty()) {
1000 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1001 if (!sk_mkdir(FLAGS_writePath[0])) {
1002 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001003 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001004 }
1005 }
1006
halcanary385fe4d2015-08-26 13:07:48 -07001007 SkAutoTDelete<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001008 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001009#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001010 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001011#else
1012 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1013 return 1;
1014#endif
mtklein60317d0f2014-07-14 11:30:37 -07001015 }
mtklein1915b622014-08-20 11:45:00 -07001016
1017 if (1 == FLAGS_properties.count() % 2) {
1018 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1019 return 1;
1020 }
1021 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1022 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1023 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001024
1025 if (1 == FLAGS_key.count() % 2) {
1026 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1027 return 1;
1028 }
1029 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001030 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001031 }
mtklein60317d0f2014-07-14 11:30:37 -07001032
mtkleinf3723212014-06-25 14:08:00 -07001033 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001034 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001035
cdaltone1b89582015-06-25 19:17:08 -07001036 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001037
bsalomon6eb03cc2014-08-07 14:28:50 -07001038 if (kAutoTuneLoops != FLAGS_loops) {
1039 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001040 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001041 SkDebugf("! -> high variance, ? -> moderate variance\n");
1042 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001043 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001044 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001045 } else {
mtkleind75c4662015-04-30 07:11:22 -07001046 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001047 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001048 }
1049
svaisanenc47635e2016-01-28 06:05:43 -08001050 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001051 create_configs(&configs);
1052
msarettc149f0e2016-01-04 11:35:43 -08001053 if (FLAGS_keepAlive) {
1054 start_keepalive();
1055 }
1056
mtkleine070c2b2014-10-14 08:40:43 -07001057 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001058 BenchmarkStream benchStream;
1059 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001060 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001061 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001062 continue;
1063 }
1064
svaisanenc47635e2016-01-28 06:05:43 -08001065 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001066 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001067 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001068 }
egdaniel3bf92062015-06-26 08:12:46 -07001069 for (int i = 0; i < configs.count(); ++i) {
1070 Target* target = is_enabled(b, configs[i]);
1071 if (!target) {
1072 continue;
1073 }
mtkleinf3723212014-06-25 14:08:00 -07001074
halcanary96fcdcc2015-08-27 07:41:13 -07001075 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001076 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001077 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001078
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001079 if (FLAGS_pre_log) {
1080 SkDebugf("Running %s\t%s\n"
1081 , bench->getUniqueName()
1082 , config);
1083 }
1084
egdaniel3bf92062015-06-26 08:12:46 -07001085 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001086 bench->perCanvasPreDraw(canvas);
1087
cdaltone1b89582015-06-25 19:17:08 -07001088 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001089 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001090 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1091 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001092
mtkleinbbba1682015-10-28 11:36:30 -07001093 if (FLAGS_ms) {
1094 samples.reset();
1095 auto stop = now_ms() + FLAGS_ms;
1096 do {
1097 samples.push_back(time(loops, bench, target) / loops);
1098 } while (now_ms() < stop);
1099 } else {
cdaltone1b89582015-06-25 19:17:08 -07001100 samples.reset(FLAGS_samples);
1101 for (int s = 0; s < FLAGS_samples; s++) {
egdaniel3bf92062015-06-26 08:12:46 -07001102 samples[s] = time(loops, bench, target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001103 }
cdaltone1b89582015-06-25 19:17:08 -07001104 }
mtkleinf3723212014-06-25 14:08:00 -07001105
joshualitte45c81c2015-12-02 09:05:37 -08001106#if SK_SUPPORT_GPU
1107 SkTArray<SkString> keys;
1108 SkTArray<double> values;
1109 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1110 if (gpuStatsDump) {
1111 // TODO cache stats
1112 bench->getGpuStats(canvas, &keys, &values);
1113 }
1114#endif
1115
robertphillips5b693772014-11-21 06:19:36 -08001116 bench->perCanvasPostDraw(canvas);
1117
egdaniel3bf92062015-06-26 08:12:46 -07001118 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001119 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001120 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001121 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001122 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001123 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001124 }
1125
1126 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001127 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001128 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001129 continue;
1130 }
1131
cdaltone1b89582015-06-25 19:17:08 -07001132 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001133 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001134 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001135 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001136 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001137 log->metric("min_ms", stats.min);
mtkleine5cf2c62016-01-11 11:28:26 -08001138 log->metric("median_ms", stats.median);
joshualitte45c81c2015-12-02 09:05:37 -08001139#if SK_SUPPORT_GPU
1140 if (gpuStatsDump) {
1141 // dump to json, only SKPBench currently returns valid keys / values
1142 SkASSERT(keys.count() == values.count());
1143 for (int i = 0; i < keys.count(); i++) {
1144 log->metric(keys[i].c_str(), values[i]);
1145 }
1146 }
1147#endif
1148
mtkleine070c2b2014-10-14 08:40:43 -07001149 if (runs++ % FLAGS_flushEvery == 0) {
1150 log->flush();
1151 }
mtklein60317d0f2014-07-14 11:30:37 -07001152
bsalomon6eb03cc2014-08-07 14:28:50 -07001153 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001154 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001155 config = ""; // Only print the config if we run the same bench on more than one.
1156 }
mtkleind75c4662015-04-30 07:11:22 -07001157 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1158 , sk_tools::getCurrResidentSetSizeMB()
1159 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001160 , bench->getUniqueName()
1161 , config);
mtkleinf3723212014-06-25 14:08:00 -07001162 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001163 const char* mark = " ";
1164 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1165 if (stddev_percent > 5) mark = "?";
1166 if (stddev_percent > 10) mark = "!";
1167
1168 SkDebugf("%10.2f %s\t%s\t%s\n",
1169 stats.median*1e3, mark, bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001170 } else {
1171 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001172 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1173 , sk_tools::getCurrResidentSetSizeMB()
1174 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001175 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001176 , HUMANIZE(stats.min)
1177 , HUMANIZE(stats.median)
1178 , HUMANIZE(stats.mean)
1179 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001180 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001181 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001182 , config
mtklein96289052014-09-10 12:05:59 -07001183 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001184 );
1185 }
joshualitte45c81c2015-12-02 09:05:37 -08001186
bsalomonb12ea412015-02-02 21:19:50 -08001187#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001188 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001189 GrContext* context = gGrFactory->get(configs[i].ctxType,
kkinnunen3e980c32015-12-23 01:33:00 -08001190 configs[i].ctxOptions);
kkinnunen5219fd92015-12-10 06:28:13 -08001191 context->printCacheStats();
1192 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001193 }
1194#endif
joshualitte45c81c2015-12-02 09:05:37 -08001195
cdalton2c56ba52015-06-26 13:32:53 -07001196 if (FLAGS_verbose) {
1197 SkDebugf("Samples: ");
1198 for (int i = 0; i < samples.count(); i++) {
1199 SkDebugf("%s ", HUMANIZE(samples[i]));
1200 }
1201 SkDebugf("%s\n", bench->getUniqueName());
1202 }
egdaniel3bf92062015-06-26 08:12:46 -07001203 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001204 }
mtkleinf3723212014-06-25 14:08:00 -07001205 }
1206
mtkleine1091452014-12-04 10:47:02 -08001207 log->bench("memory_usage", 0,0);
1208 log->config("meta");
1209 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1210
joshualitte0b19d42015-03-26 10:41:02 -07001211#if SK_SUPPORT_GPU
1212 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1213 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001214 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001215#endif
1216
mtkleinf3723212014-06-25 14:08:00 -07001217 return 0;
1218}
1219
jcgregorio3b27ade2014-11-13 08:06:40 -08001220#if !defined SK_BUILD_FOR_IOS
1221int main(int argc, char** argv) {
1222 SkCommandLineFlags::Parse(argc, argv);
1223 return nanobench_main();
1224}
1225#endif