blob: 4d8ba4497de889a3acbbb4f66db5db71856579f8 [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"
joshualitt3ebd0502016-02-09 07:18:08 -080041#include "ThermalManager.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");
joshualitt3ebd0502016-02-09 07:18:08 -0800115DEFINE_string(useThermalManager, "0,1,10,1000", "enabled,threshold,sleepTimeMs,TimeoutMs for "
116 "thermalManager\n");
mtklein92007582014-08-01 07:46:52 -0700117
mtklein65dfd2f2016-02-03 10:40:54 -0800118DEFINE_string(sourceType, "",
119 "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
120DEFINE_string(benchType, "",
121 "Apply usual --match rules to bench type: micro, recording, playback, skcodec, etc.");
122
mtkleinbbba1682015-10-28 11:36:30 -0700123static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
124
mtkleinf3723212014-06-25 14:08:00 -0700125static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700126 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800127 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700128}
mtklein55b0ffc2014-07-17 08:38:23 -0700129#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700130
tomhudsond968a6f2015-03-26 11:28:06 -0700131bool Target::init(SkImageInfo info, Benchmark* bench) {
132 if (Benchmark::kRaster_Backend == config.backend) {
133 this->surface.reset(SkSurface::NewRaster(info));
134 if (!this->surface.get()) {
135 return false;
136 }
137 }
138 return true;
139}
140bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700141 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700142 if (!canvas) {
143 return false;
144 }
145 bmp->setInfo(canvas->imageInfo());
146 if (!canvas->readPixels(bmp, 0, 0)) {
147 SkDebugf("Can't read canvas pixels.\n");
148 return false;
149 }
150 return true;
151}
152
153#if SK_SUPPORT_GPU
154struct GPUTarget : public Target {
halcanary96fcdcc2015-08-27 07:41:13 -0700155 explicit GPUTarget(const Config& c) : Target(c), gl(nullptr) { }
tomhudsond968a6f2015-03-26 11:28:06 -0700156 SkGLContext* gl;
157
158 void setup() override {
159 this->gl->makeCurrent();
160 // Make sure we're done with whatever came before.
161 SK_GL(*this->gl, Finish());
162 }
163 void endTiming() override {
164 if (this->gl) {
165 SK_GL(*this->gl, Flush());
joshualitt01836ad2016-01-20 13:09:12 -0800166 this->gl->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700167 }
168 }
169 void fence() override {
170 SK_GL(*this->gl, Finish());
171 }
mtkleind75c4662015-04-30 07:11:22 -0700172
cdaltond416a5b2015-06-23 13:23:44 -0700173 bool needsFrameTiming(int* maxFrameLag) const override {
174 if (!this->gl->getMaxGpuFrameLag(maxFrameLag)) {
175 // Frame lag is unknown.
176 *maxFrameLag = FLAGS_gpuFrameLag;
177 }
178 return true;
179 }
tomhudsond968a6f2015-03-26 11:28:06 -0700180 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700181 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
182 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700183 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
kkinnunen5219fd92015-12-10 06:28:13 -0800184 this->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(this->config.ctxType,
kkinnunen5219fd92015-12-10 06:28:13 -0800185 this->config.ctxOptions),
tomhudsond968a6f2015-03-26 11:28:06 -0700186 SkSurface::kNo_Budgeted, info,
187 this->config.samples, &props));
kkinnunen3e980c32015-12-23 01:33:00 -0800188 this->gl = gGrFactory->getContextInfo(this->config.ctxType,
kkinnunen34058002016-01-06 23:49:30 -0800189 this->config.ctxOptions).fGLContext;
tomhudsond968a6f2015-03-26 11:28:06 -0700190 if (!this->surface.get()) {
191 return false;
192 }
cdaltond416a5b2015-06-23 13:23:44 -0700193 if (!this->gl->fenceSyncSupport()) {
194 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800195 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700196 }
tomhudsond968a6f2015-03-26 11:28:06 -0700197 return true;
198 }
199 void fillOptions(ResultsWriter* log) override {
200 const GrGLubyte* version;
201 SK_GL_RET(*this->gl, version, GetString(GR_GL_VERSION));
202 log->configOption("GL_VERSION", (const char*)(version));
203
204 SK_GL_RET(*this->gl, version, GetString(GR_GL_RENDERER));
205 log->configOption("GL_RENDERER", (const char*) version);
206
207 SK_GL_RET(*this->gl, version, GetString(GR_GL_VENDOR));
208 log->configOption("GL_VENDOR", (const char*) version);
209
210 SK_GL_RET(*this->gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
211 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
212 }
213};
mtkleind75c4662015-04-30 07:11:22 -0700214
tomhudsond968a6f2015-03-26 11:28:06 -0700215#endif
216
tomhudson75a0ebb2015-03-27 12:11:44 -0700217static double time(int loops, Benchmark* bench, Target* target) {
218 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700219 if (canvas) {
220 canvas->clear(SK_ColorWHITE);
221 }
joshualitt8a6697a2015-09-30 12:11:07 -0700222 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700223 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700224 canvas = target->beginTiming(canvas);
225 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700226 if (canvas) {
227 canvas->flush();
228 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700229 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700230 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700231 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700232 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700233}
234
mtkleinf3723212014-06-25 14:08:00 -0700235static double estimate_timer_overhead() {
236 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700237 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700238 double start = now_ms();
239 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700240 }
241 return overhead / FLAGS_overheadLoops;
242}
243
reed53249782014-10-10 09:09:52 -0700244static int detect_forever_loops(int loops) {
245 // look for a magic run-forever value
246 if (loops < 0) {
247 loops = SK_MaxS32;
248 }
249 return loops;
250}
251
mtklein55b0ffc2014-07-17 08:38:23 -0700252static int clamp_loops(int loops) {
253 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800254 SkDebugf("ERROR: clamping loops from %d to 1. "
255 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700256 return 1;
257 }
258 if (loops > FLAGS_maxLoops) {
259 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
260 return FLAGS_maxLoops;
261 }
262 return loops;
263}
264
tomhudsond968a6f2015-03-26 11:28:06 -0700265static bool write_canvas_png(Target* target, const SkString& filename) {
266
bsalomon6eb03cc2014-08-07 14:28:50 -0700267 if (filename.isEmpty()) {
268 return false;
269 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700270 if (target->getCanvas() &&
271 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700272 return false;
273 }
tomhudsond968a6f2015-03-26 11:28:06 -0700274
bsalomon6eb03cc2014-08-07 14:28:50 -0700275 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700276
277 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700278 return false;
279 }
tomhudsond968a6f2015-03-26 11:28:06 -0700280
bsalomon6eb03cc2014-08-07 14:28:50 -0700281 SkString dir = SkOSPath::Dirname(filename.c_str());
282 if (!sk_mkdir(dir.c_str())) {
283 SkDebugf("Can't make dir %s.\n", dir.c_str());
284 return false;
285 }
286 SkFILEWStream stream(filename.c_str());
287 if (!stream.isValid()) {
288 SkDebugf("Can't write %s.\n", filename.c_str());
289 return false;
290 }
291 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
292 SkDebugf("Can't encode a PNG.\n");
293 return false;
294 }
295 return true;
296}
297
298static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700299static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700300 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700301 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700302 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700303 int loops = bench->calculateLoops(FLAGS_loops);
304 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700305 while (bench_plus_overhead < overhead) {
306 if (round++ == FLAGS_maxCalibrationAttempts) {
307 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700308 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700309 return kFailedLoops;
310 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700311 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700312 }
mtklein2069e222014-08-04 13:57:39 -0700313 }
mtkleinf3723212014-06-25 14:08:00 -0700314
mtkleinbb6a0282014-07-01 08:43:42 -0700315 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700316 // We'll pick N to make timer overhead negligible:
317 //
mtkleinbb6a0282014-07-01 08:43:42 -0700318 // overhead
319 // ------------------------- < FLAGS_overheadGoal
320 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700321 //
mtkleinbb6a0282014-07-01 08:43:42 -0700322 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700323 //
324 // Doing some math, we get:
325 //
mtkleinbb6a0282014-07-01 08:43:42 -0700326 // (overhead / FLAGS_overheadGoal) - overhead
327 // ------------------------------------------ < N
328 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700329 //
330 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700331 if (kAutoTuneLoops == loops) {
332 const double numer = overhead / FLAGS_overheadGoal - overhead;
333 const double denom = bench_plus_overhead - overhead;
334 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700335 loops = clamp_loops(loops);
336 } else {
337 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700338 }
mtkleinbb6a0282014-07-01 08:43:42 -0700339
mtkleinbb6a0282014-07-01 08:43:42 -0700340 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700341}
342
cdaltone1b89582015-06-25 19:17:08 -0700343static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700344 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700345 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700346 if (kAutoTuneLoops == loops) {
347 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700348 double elapsed = 0;
349 do {
mtklein527930f2014-11-06 08:04:34 -0800350 if (1<<30 == loops) {
351 // We're about to wrap. Something's wrong with the bench.
352 loops = 0;
353 break;
354 }
mtkleina189ccd2014-07-14 12:28:47 -0700355 loops *= 2;
356 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700357 // _this_ round, not still timing last round.
358 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700359 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700360 }
361 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700362
mtkleina189ccd2014-07-14 12:28:47 -0700363 // We've overshot at least a little. Scale back linearly.
364 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700365 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700366
tomhudsond968a6f2015-03-26 11:28:06 -0700367 // Make sure we're not still timing our calibration.
368 target->fence();
reed53249782014-10-10 09:09:52 -0700369 } else {
370 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700371 }
mtkleinbb6a0282014-07-01 08:43:42 -0700372
373 // Pretty much the same deal as the calibration: do some warmup to make
374 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700375 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700376 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700377 }
mtkleinbb6a0282014-07-01 08:43:42 -0700378
mtkleinbb6a0282014-07-01 08:43:42 -0700379 return loops;
380}
mtkleinbb6a0282014-07-01 08:43:42 -0700381
bsalomonc2553372014-07-22 13:09:05 -0700382#if SK_SUPPORT_GPU
383#define kBogusGLContextType GrContextFactory::kNative_GLContextType
kkinnunen5219fd92015-12-10 06:28:13 -0800384#define kBogusGLContextOptions GrContextFactory::kNone_GLContextOptions
bsalomonc2553372014-07-22 13:09:05 -0700385#else
386#define kBogusGLContextType 0
kkinnunen5219fd92015-12-10 06:28:13 -0800387#define kBogusGLContextOptions 0
mtkleine714e752014-07-31 12:13:48 -0700388#endif
bsalomonc2553372014-07-22 13:09:05 -0700389
svaisanenc47635e2016-01-28 06:05:43 -0800390static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
391
392#if SK_SUPPORT_GPU
393 if (const auto* gpuConfig = config->asConfigGpu()) {
394 if (!FLAGS_gpu)
395 return;
396
397 const auto ctxOptions = gpuConfig->getUseNVPR() ? GrContextFactory::kEnableNVPR_GLContextOptions
398 : GrContextFactory::kNone_GLContextOptions;
399 const auto ctxType = gpuConfig->getContextType();
400 const auto sampleCount = gpuConfig->getSamples();
401
402 if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOptions)) {
403 const auto maxSampleCount = ctx->caps()->maxSampleCount();
404 if (sampleCount > ctx->caps()->maxSampleCount()) {
405 SkDebugf("Configuration sample count %d exceeds maximum %d.\n",
406 sampleCount, maxSampleCount);
407 return;
408 }
409 } else {
410 SkDebugf("No context was available matching config type and options.\n");
411 return;
412 }
413
414 Config target = {
415 config->getTag(),
416 Benchmark::kGPU_Backend,
417 kN32_SkColorType,
418 kPremul_SkAlphaType,
419 sampleCount,
420 ctxType,
421 ctxOptions,
422 false };
423
424 configs->push_back(target);
425 return;
426 }
427#endif
428
429 #define CPU_CONFIG(name, backend, color, alpha) \
430 if (config->getTag().equals(#name)) { \
431 Config config = { SkString(#name), Benchmark::backend, color, alpha, 0, \
432 kBogusGLContextType, kBogusGLContextOptions, \
433 false }; \
434 configs->push_back(config); \
435 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700436 }
mtkleine714e752014-07-31 12:13:48 -0700437
mtklein40b32be2014-07-09 08:46:49 -0700438 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700439 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
440 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
441 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700442 }
mtkleinbb6a0282014-07-01 08:43:42 -0700443
svaisanenc47635e2016-01-28 06:05:43 -0800444 #undef CPU_CONFIG
tomhudsond968a6f2015-03-26 11:28:06 -0700445
446#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
djsollen817c72a2016-01-28 13:57:02 -0800447 if (config->getTag().equals("hwui")) {
448 Config config = { SkString("hwui"), Benchmark::kHWUI_Backend, kRGBA_8888_SkColorType,
kkinnunen5219fd92015-12-10 06:28:13 -0800449 kPremul_SkAlphaType, 0, kBogusGLContextType, kBogusGLContextOptions,
450 false };
svaisanenc47635e2016-01-28 06:05:43 -0800451 configs->push_back(config);
tomhudsond968a6f2015-03-26 11:28:06 -0700452 }
453#endif
mtkleinf3723212014-06-25 14:08:00 -0700454}
455
svaisanenc47635e2016-01-28 06:05:43 -0800456// Append all configs that are enabled and supported.
457void create_configs(SkTArray<Config>* configs) {
458 SkCommandLineConfigArray array;
459 ParseConfigs(FLAGS_config, &array);
460 for (int i = 0; i < array.count(); ++i) {
461 create_config(array[i], configs);
462 }
463}
464
halcanary96fcdcc2015-08-27 07:41:13 -0700465// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700466static Target* is_enabled(Benchmark* bench, const Config& config) {
467 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700468 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700469 }
470
reede5ea5002014-09-03 11:54:58 -0700471 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
472 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700473
halcanary96fcdcc2015-08-27 07:41:13 -0700474 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700475
tomhudsond968a6f2015-03-26 11:28:06 -0700476 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700477#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700478 case Benchmark::kGPU_Backend:
479 target = new GPUTarget(config);
480 break;
bsalomonc2553372014-07-22 13:09:05 -0700481#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700482#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
483 case Benchmark::kHWUI_Backend:
484 target = new HWUITarget(config, bench);
485 break;
486#endif
487 default:
488 target = new Target(config);
489 break;
490 }
bsalomonc2553372014-07-22 13:09:05 -0700491
tomhudsond968a6f2015-03-26 11:28:06 -0700492 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700493 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700494 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700495 }
496 return target;
497}
498
msarett5cb48852015-11-06 08:56:32 -0800499static bool valid_brd_bench(SkData* encoded, SkBitmapRegionDecoder::Strategy strategy,
msarett7f691442015-09-22 11:56:16 -0700500 SkColorType colorType, uint32_t sampleSize, uint32_t minOutputSize, int* width,
501 int* height) {
msarett5cb48852015-11-06 08:56:32 -0800502 SkAutoTDelete<SkBitmapRegionDecoder> brd(
503 SkBitmapRegionDecoder::Create(encoded, strategy));
msarett7f691442015-09-22 11:56:16 -0700504 if (nullptr == brd.get()) {
505 // This is indicates that subset decoding is not supported for a particular image format.
506 return false;
507 }
508
msarett35e5d1b2015-10-27 12:50:25 -0700509 SkBitmap bitmap;
510 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, brd->width(), brd->height()),
511 1, colorType, false)) {
512 return false;
513 }
msarett7f691442015-09-22 11:56:16 -0700514
515 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
516 (uint32_t) brd->height()) {
517 // This indicates that the image is not large enough to decode a
518 // minOutputSize x minOutputSize subset at the given sampleSize.
519 return false;
520 }
521
522 // Set the image width and height. The calling code will use this to choose subsets to decode.
523 *width = brd->width();
524 *height = brd->height();
525 return true;
526}
527
egdaniel3bf92062015-06-26 08:12:46 -0700528static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700529 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700530#if SK_SUPPORT_GPU
531 if (FLAGS_abandonGpuContext) {
532 gGrFactory->abandonContexts();
533 }
534 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
535 gGrFactory->destroyContexts();
536 }
537#endif
538}
539
mtkleine714e752014-07-31 12:13:48 -0700540class BenchmarkStream {
541public:
mtklein92007582014-08-01 07:46:52 -0700542 BenchmarkStream() : fBenches(BenchRegistry::Head())
543 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700544 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700545 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800546 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800547 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700548 , fCurrentCodec(0)
msarett7f691442015-09-22 11:56:16 -0700549 , fCurrentBRDImage(0)
msarett95f192d2015-02-13 09:05:41 -0800550 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800551 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700552 , fCurrentSubsetType(0)
msarett7f691442015-09-22 11:56:16 -0700553 , fCurrentBRDStrategy(0)
554 , fCurrentBRDSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700555 , fCurrentAnimSKP(0) {
mtklein92007582014-08-01 07:46:52 -0700556 for (int i = 0; i < FLAGS_skps.count(); i++) {
557 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
558 fSKPs.push_back() = FLAGS_skps[i];
559 } else {
560 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
561 SkString path;
562 while (it.next(&path)) {
563 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
564 }
565 }
566 }
mtkleine714e752014-07-31 12:13:48 -0700567
mtklein92007582014-08-01 07:46:52 -0700568 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
569 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
570 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
571 exit(1);
572 }
573
574 for (int i = 0; i < FLAGS_scales.count(); i++) {
575 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
576 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
577 exit(1);
578 }
579 }
robertphillips5b693772014-11-21 06:19:36 -0800580
cdalton63a82852015-06-29 14:06:10 -0700581 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
582 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700583 exit(1);
584 }
585
robertphillips5b693772014-11-21 06:19:36 -0800586 if (FLAGS_mpd) {
587 fUseMPDs.push_back() = true;
588 }
mtkleinc751ecb2015-06-15 08:56:38 -0700589 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700590
msarett95f192d2015-02-13 09:05:41 -0800591 // Prepare the images for decoding
scroggo86737142016-02-03 12:19:11 -0800592 if (!CollectImages(&fImages)) {
593 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800594 }
mtklein95553d92015-03-12 08:24:21 -0700595
msarett95f192d2015-02-13 09:05:41 -0800596 // Choose the candidate color types for image decoding
597 const SkColorType colorTypes[] =
msarettb23e6aa2015-06-09 13:56:10 -0700598 { kN32_SkColorType,
599 kRGB_565_SkColorType,
600 kAlpha_8_SkColorType,
601 kIndex_8_SkColorType,
602 kGray_8_SkColorType };
msarett74deb982015-10-20 16:45:56 -0700603 fColorTypes.reset(colorTypes, SK_ARRAY_COUNT(colorTypes));
mtklein92007582014-08-01 07:46:52 -0700604 }
605
mtkleinfd731ce2014-09-10 12:19:30 -0700606 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
607 // Not strictly necessary, as it will be checked again later,
608 // but helps to avoid a lot of pointless work if we're going to skip it.
609 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
610 return false;
611 }
612
scroggoa1193e42015-01-21 12:09:53 -0800613 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -0700614 if (stream.get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700615 SkDebugf("Could not read %s.\n", path);
616 return false;
617 }
618
mtklein57f27bd2015-02-09 11:58:41 -0800619 pic->reset(SkPicture::CreateFromStream(stream.get()));
halcanary96fcdcc2015-08-27 07:41:13 -0700620 if (pic->get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700621 SkDebugf("Could not read %s as an SkPicture.\n", path);
622 return false;
623 }
624 return true;
625 }
626
mtklein92007582014-08-01 07:46:52 -0700627 Benchmark* next() {
mtklein65dfd2f2016-02-03 10:40:54 -0800628 SkAutoTDelete<Benchmark> bench;
629 do {
630 bench.reset(this->rawNext());
631 if (!bench) {
632 return nullptr;
633 }
634 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
635 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
636 return bench.detach();
637 }
638
639 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700640 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700641 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700642 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700643 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700644 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700645 return bench;
646 }
mtklein92007582014-08-01 07:46:52 -0700647
mtkleine714e752014-07-31 12:13:48 -0700648 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -0700649 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700650 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800651 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700652 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700653 fBenchType = "micro";
halcanary385fe4d2015-08-26 13:07:48 -0700654 return new GMBench(gm.detach());
mtkleine714e752014-07-31 12:13:48 -0700655 }
656 }
mtklein92007582014-08-01 07:46:52 -0700657
mtkleinfd731ce2014-09-10 12:19:30 -0700658 // First add all .skps as RecordingBenches.
659 while (fCurrentRecording < fSKPs.count()) {
660 const SkString& path = fSKPs[fCurrentRecording++];
661 SkAutoTUnref<SkPicture> pic;
662 if (!ReadPicture(path.c_str(), &pic)) {
663 continue;
664 }
665 SkString name = SkOSPath::Basename(path.c_str());
666 fSourceType = "skp";
667 fBenchType = "recording";
bsalomon0aa5cea2014-12-15 09:13:35 -0800668 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic));
mtklein051e56d2014-12-04 08:46:51 -0800669 fSKPOps = pic->approximateOpCount();
halcanary385fe4d2015-08-26 13:07:48 -0700670 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700671 }
672
673 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700674 while (fCurrentScale < fScales.count()) {
675 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800676 const SkString& path = fSKPs[fCurrentSKP];
mtkleinfd731ce2014-09-10 12:19:30 -0700677 SkAutoTUnref<SkPicture> pic;
678 if (!ReadPicture(path.c_str(), &pic)) {
robertphillips5b693772014-11-21 06:19:36 -0800679 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700680 continue;
681 }
robertphillips5b693772014-11-21 06:19:36 -0800682
683 while (fCurrentUseMPD < fUseMPDs.count()) {
684 if (FLAGS_bbh) {
685 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
686 SkRTreeFactory factory;
687 SkPictureRecorder recorder;
688 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
689 pic->playback(recorder.beginRecording(pic->cullRect().width(),
690 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800691 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800692 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
robertphillips5b693772014-11-21 06:19:36 -0800693 pic.reset(recorder.endRecording());
694 }
695 SkString name = SkOSPath::Basename(path.c_str());
696 fSourceType = "skp";
697 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700698 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
699 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700700 }
robertphillips5b693772014-11-21 06:19:36 -0800701 fCurrentUseMPD = 0;
702 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700703 }
704 fCurrentSKP = 0;
705 fCurrentScale++;
706 }
707
joshualitt261c3ad2015-04-27 09:16:57 -0700708 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700709 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700710 while (fCurrentAnimSKP < fSKPs.count()) {
711 const SkString& path = fSKPs[fCurrentAnimSKP];
712 SkAutoTUnref<SkPicture> pic;
713 if (!ReadPicture(path.c_str(), &pic)) {
714 fCurrentAnimSKP++;
715 continue;
716 }
717
718 fCurrentAnimSKP++;
719 SkString name = SkOSPath::Basename(path.c_str());
cdalton63a82852015-06-29 14:06:10 -0700720 SkAutoTUnref<SKPAnimationBench::Animation> animation(
721 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
halcanary385fe4d2015-08-26 13:07:48 -0700722 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation,
723 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700724 }
725 }
726
scroggo60869a42015-04-01 12:09:17 -0700727 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700728 fSourceType = "image";
729 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700730 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800731 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
732 continue;
733 }
scroggo60869a42015-04-01 12:09:17 -0700734 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
735 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700736 if (!codec) {
737 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700738 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700739 continue;
740 }
scroggo21027992015-04-02 13:22:38 -0700741
scroggo60869a42015-04-01 12:09:17 -0700742 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700743 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700744
msarettc7796b92016-01-07 14:20:20 -0800745 SkAlphaType alphaType = codec->getInfo().alphaType();
746 switch (alphaType) {
747 case kOpaque_SkAlphaType:
748 // We only need to test one alpha type (opaque).
749 fCurrentColorType++;
750 break;
751 case kUnpremul_SkAlphaType:
752 case kPremul_SkAlphaType:
753 if (0 == fCurrentAlphaType) {
754 // Test unpremul first.
755 alphaType = kUnpremul_SkAlphaType;
756 fCurrentAlphaType++;
757 } else {
758 // Test premul.
759 alphaType = kPremul_SkAlphaType;
760 fCurrentAlphaType = 0;
761 fCurrentColorType++;
762 }
763 break;
764 default:
765 SkASSERT(false);
766 fCurrentColorType++;
767 break;
scroggo21027992015-04-02 13:22:38 -0700768 }
769
msarettc7796b92016-01-07 14:20:20 -0800770 // Make sure we can decode to this color type and alpha type.
771 SkImageInfo info =
772 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700773 const size_t rowBytes = info.minRowBytes();
774 SkAutoMalloc storage(info.getSafeSize(rowBytes));
775
776 // Used if fCurrentColorType is kIndex_8_SkColorType
777 int colorCount = 256;
778 SkPMColor colors[256];
779
scroggoeb602a52015-07-09 08:16:03 -0700780 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700781 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700782 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700783 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700784 case SkCodec::kSuccess:
785 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700786 return new CodecBench(SkOSPath::Basename(path.c_str()),
msarettc7796b92016-01-07 14:20:20 -0800787 encoded, colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700788 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700789 // This is okay. Not all conversions are valid.
790 break;
scroggo60869a42015-04-01 12:09:17 -0700791 default:
792 // This represents some sort of failure.
793 SkASSERT(false);
794 break;
795 }
796 }
797 fCurrentColorType = 0;
798 }
799
msarett7f691442015-09-22 11:56:16 -0700800 // Run the BRDBenches
801 // We will benchmark multiple BRD strategies.
scroggo303fa352015-10-05 11:03:34 -0700802 static const struct {
msarett5cb48852015-11-06 08:56:32 -0800803 SkBitmapRegionDecoder::Strategy fStrategy;
msarett0459c942015-11-10 14:52:13 -0800804 const char* fName;
scroggo303fa352015-10-05 11:03:34 -0700805 } strategies[] = {
msarett0459c942015-11-10 14:52:13 -0800806 { SkBitmapRegionDecoder::kCanvas_Strategy, "BRD_canvas" },
msarett5cb48852015-11-06 08:56:32 -0800807 { SkBitmapRegionDecoder::kAndroidCodec_Strategy, "BRD_android_codec" },
msarett7f691442015-09-22 11:56:16 -0700808 };
809
810 // We intend to create benchmarks that model the use cases in
811 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
812 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
813 // the image. For that reason, we will benchmark decodes in five representative locations
814 // in the image. Additionally, this use case utilizes power of two scaling, so we will
815 // test on power of two sample sizes. The output tile is always 512x512, so, when a
816 // sampleSize is used, the size of the subset that is decoded is always
817 // (sampleSize*512)x(sampleSize*512).
818 // There are a few good reasons to only test on power of two sample sizes at this time:
819 // JPEG decodes using kOriginal_Strategy are broken for non-powers of two.
halcanary6950de62015-11-07 05:29:00 -0800820 // https://bug.skia.org/4319
msarett7f691442015-09-22 11:56:16 -0700821 // All use cases we are aware of only scale by powers of two.
822 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
823 // these tests are sufficient to provide good coverage of our scaling options.
scroggo501b7342015-11-03 07:55:11 -0800824 const uint32_t sampleSizes[] = { 1, 2, 4, 8, 16, 32, 64 };
msarett7f691442015-09-22 11:56:16 -0700825 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800826 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
827 const SkString& path = fImages[fCurrentBRDImage];
828 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
829 continue;
830 }
msarett7f691442015-09-22 11:56:16 -0700831 while (fCurrentBRDStrategy < (int) SK_ARRAY_COUNT(strategies)) {
scroggo303fa352015-10-05 11:03:34 -0700832 fSourceType = "image";
833 fBenchType = strategies[fCurrentBRDStrategy].fName;
scroggo860e8a62015-10-15 07:51:28 -0700834
msarett5cb48852015-11-06 08:56:32 -0800835 const SkBitmapRegionDecoder::Strategy strategy =
scroggo860e8a62015-10-15 07:51:28 -0700836 strategies[fCurrentBRDStrategy].fStrategy;
837
msarett7f691442015-09-22 11:56:16 -0700838 while (fCurrentColorType < fColorTypes.count()) {
839 while (fCurrentBRDSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
840 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700841
842
msarett7f691442015-09-22 11:56:16 -0700843 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
844 const SkColorType colorType = fColorTypes[fCurrentColorType];
845 uint32_t sampleSize = sampleSizes[fCurrentBRDSampleSize];
846 int currentSubsetType = fCurrentSubsetType++;
847
848 int width = 0;
849 int height = 0;
850 if (!valid_brd_bench(encoded.get(), strategy, colorType, sampleSize,
851 minOutputSize, &width, &height)) {
852 break;
853 }
854
855 SkString basename = SkOSPath::Basename(path.c_str());
856 SkIRect subset;
857 const uint32_t subsetSize = sampleSize * minOutputSize;
858 switch (currentSubsetType) {
859 case kTopLeft_SubsetType:
860 basename.append("_TopLeft");
861 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
862 break;
863 case kTopRight_SubsetType:
864 basename.append("_TopRight");
865 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
866 subsetSize);
867 break;
868 case kMiddle_SubsetType:
869 basename.append("_Middle");
870 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
871 (height - subsetSize) / 2, subsetSize, subsetSize);
872 break;
873 case kBottomLeft_SubsetType:
874 basename.append("_BottomLeft");
875 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
876 subsetSize);
877 break;
878 case kBottomRight_SubsetType:
879 basename.append("_BottomRight");
880 subset = SkIRect::MakeXYWH(width - subsetSize,
881 height - subsetSize, subsetSize, subsetSize);
882 break;
883 default:
884 SkASSERT(false);
885 }
886
887 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
888 strategy, colorType, sampleSize, subset);
889 }
890 fCurrentSubsetType = 0;
891 fCurrentBRDSampleSize++;
892 }
893 fCurrentBRDSampleSize = 0;
894 fCurrentColorType++;
895 }
896 fCurrentColorType = 0;
897 fCurrentBRDStrategy++;
898 }
899 fCurrentBRDStrategy = 0;
msarett7f691442015-09-22 11:56:16 -0700900 }
901
halcanary96fcdcc2015-08-27 07:41:13 -0700902 return nullptr;
mtkleine714e752014-07-31 12:13:48 -0700903 }
mtklein92007582014-08-01 07:46:52 -0700904
905 void fillCurrentOptions(ResultsWriter* log) const {
906 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700907 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700908 if (0 == strcmp(fSourceType, "skp")) {
909 log->configOption("clip",
910 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
911 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -0800912 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -0700913 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -0800914 if (fCurrentUseMPD > 0) {
915 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
916 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
917 }
mtklein92007582014-08-01 07:46:52 -0700918 }
mtklein051e56d2014-12-04 08:46:51 -0800919 if (0 == strcmp(fBenchType, "recording")) {
920 log->metric("bytes", fSKPBytes);
921 log->metric("ops", fSKPOps);
922 }
mtklein92007582014-08-01 07:46:52 -0700923 }
924
mtkleine714e752014-07-31 12:13:48 -0700925private:
msarettb23e6aa2015-06-09 13:56:10 -0700926 enum SubsetType {
927 kTopLeft_SubsetType = 0,
928 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -0700929 kMiddle_SubsetType = 2,
930 kBottomLeft_SubsetType = 3,
931 kBottomRight_SubsetType = 4,
932 kTranslate_SubsetType = 5,
933 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -0700934 kLast_SubsetType = kZoom_SubsetType,
935 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -0700936 };
937
mtkleine714e752014-07-31 12:13:48 -0700938 const BenchRegistry* fBenches;
939 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700940 SkIRect fClip;
941 SkTArray<SkScalar> fScales;
942 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -0800943 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -0800944 SkTArray<SkString> fImages;
msarett74deb982015-10-20 16:45:56 -0700945 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -0700946 SkScalar fZoomMax;
947 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -0700948
mtklein051e56d2014-12-04 08:46:51 -0800949 double fSKPBytes, fSKPOps;
950
mtkleinfd731ce2014-09-10 12:19:30 -0700951 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
952 const char* fBenchType; // How we bench it: micro, recording, playback, ...
953 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700954 int fCurrentScale;
955 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -0800956 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -0700957 int fCurrentCodec;
msarett7f691442015-09-22 11:56:16 -0700958 int fCurrentBRDImage;
msarett95f192d2015-02-13 09:05:41 -0800959 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -0800960 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -0700961 int fCurrentSubsetType;
msarett7f691442015-09-22 11:56:16 -0700962 int fCurrentBRDStrategy;
963 int fCurrentBRDSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -0700964 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -0700965};
966
msarettc149f0e2016-01-04 11:35:43 -0800967// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
968// This prints something every once in a while so that it knows we're still working.
969static void start_keepalive() {
970 struct Loop {
971 static void forever(void*) {
972 for (;;) {
973 static const int kSec = 1200;
974 #if defined(SK_BUILD_FOR_WIN)
975 Sleep(kSec * 1000);
976 #else
977 sleep(kSec);
978 #endif
979 SkDebugf("\nBenchmarks still running...\n");
980 }
981 }
982 };
983 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
984 intentionallyLeaked->start();
985}
986
jcgregorio3b27ade2014-11-13 08:06:40 -0800987int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700988int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800989 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -0700990 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -0700991 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -0700992
krajcevski69a55602014-08-13 10:46:31 -0700993#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -0700994 GrContextOptions grContextOpts;
krajcevski12b35442014-08-13 12:06:26 -0700995 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
halcanary385fe4d2015-08-26 13:07:48 -0700996 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -0700997#endif
998
bsalomon06cddec2014-10-24 10:40:50 -0700999 if (FLAGS_veryVerbose) {
1000 FLAGS_verbose = true;
1001 }
1002
bsalomon6eb03cc2014-08-07 14:28:50 -07001003 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001004 FLAGS_samples = 1;
1005 FLAGS_gpuFrameLag = 0;
1006 }
1007
bsalomon6eb03cc2014-08-07 14:28:50 -07001008 if (!FLAGS_writePath.isEmpty()) {
1009 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1010 if (!sk_mkdir(FLAGS_writePath[0])) {
1011 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001012 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001013 }
1014 }
1015
halcanary385fe4d2015-08-26 13:07:48 -07001016 SkAutoTDelete<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001017 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001018#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001019 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001020#else
1021 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1022 return 1;
1023#endif
mtklein60317d0f2014-07-14 11:30:37 -07001024 }
mtklein1915b622014-08-20 11:45:00 -07001025
1026 if (1 == FLAGS_properties.count() % 2) {
1027 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1028 return 1;
1029 }
1030 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1031 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1032 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001033
1034 if (1 == FLAGS_key.count() % 2) {
1035 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1036 return 1;
1037 }
1038 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001039 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001040 }
mtklein60317d0f2014-07-14 11:30:37 -07001041
mtkleinf3723212014-06-25 14:08:00 -07001042 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001043 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001044
cdaltone1b89582015-06-25 19:17:08 -07001045 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001046
bsalomon6eb03cc2014-08-07 14:28:50 -07001047 if (kAutoTuneLoops != FLAGS_loops) {
1048 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001049 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001050 SkDebugf("! -> high variance, ? -> moderate variance\n");
1051 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001052 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001053 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001054 } else {
mtkleind75c4662015-04-30 07:11:22 -07001055 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001056 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001057 }
1058
svaisanenc47635e2016-01-28 06:05:43 -08001059 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001060 create_configs(&configs);
1061
joshualitt3ebd0502016-02-09 07:18:08 -08001062#ifdef THERMAL_MANAGER_SUPPORTED
1063 int tmEnabled, tmThreshold, tmSleepTimeMs, tmTimeoutMs;
1064 if (4 != sscanf(FLAGS_useThermalManager[0], "%d,%d,%d,%d",
1065 &tmEnabled, &tmThreshold, &tmSleepTimeMs, &tmTimeoutMs)) {
1066 SkDebugf("Can't parse %s from --useThermalManager.\n", FLAGS_useThermalManager[0]);
1067 exit(1);
1068 }
1069 ThermalManager tm(tmThreshold, tmSleepTimeMs, tmTimeoutMs);
1070#endif
1071
msarettc149f0e2016-01-04 11:35:43 -08001072 if (FLAGS_keepAlive) {
1073 start_keepalive();
1074 }
1075
mtkleine070c2b2014-10-14 08:40:43 -07001076 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001077 BenchmarkStream benchStream;
1078 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001079 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001080 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001081 continue;
1082 }
1083
svaisanenc47635e2016-01-28 06:05:43 -08001084 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001085 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001086 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001087 }
egdaniel3bf92062015-06-26 08:12:46 -07001088 for (int i = 0; i < configs.count(); ++i) {
joshualitt3ebd0502016-02-09 07:18:08 -08001089#ifdef THERMAL_MANAGER_SUPPORTED
1090 if (tmEnabled && !tm.coolOffIfNecessary()) {
1091 SkDebugf("Could not cool off, timings will be throttled\n");
1092 }
1093#endif
egdaniel3bf92062015-06-26 08:12:46 -07001094 Target* target = is_enabled(b, configs[i]);
1095 if (!target) {
1096 continue;
1097 }
mtkleinf3723212014-06-25 14:08:00 -07001098
halcanary96fcdcc2015-08-27 07:41:13 -07001099 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001100 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001101 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001102
brianosman7a5ada82016-02-08 13:49:12 -08001103 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001104 SkDebugf("Running %s\t%s\n"
1105 , bench->getUniqueName()
1106 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001107 if (FLAGS_dryRun) {
1108 continue;
1109 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001110 }
1111
egdaniel3bf92062015-06-26 08:12:46 -07001112 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001113 bench->perCanvasPreDraw(canvas);
1114
cdaltone1b89582015-06-25 19:17:08 -07001115 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001116 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001117 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1118 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001119
mtkleinbbba1682015-10-28 11:36:30 -07001120 if (FLAGS_ms) {
1121 samples.reset();
1122 auto stop = now_ms() + FLAGS_ms;
1123 do {
1124 samples.push_back(time(loops, bench, target) / loops);
1125 } while (now_ms() < stop);
1126 } else {
cdaltone1b89582015-06-25 19:17:08 -07001127 samples.reset(FLAGS_samples);
1128 for (int s = 0; s < FLAGS_samples; s++) {
egdaniel3bf92062015-06-26 08:12:46 -07001129 samples[s] = time(loops, bench, target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001130 }
cdaltone1b89582015-06-25 19:17:08 -07001131 }
mtkleinf3723212014-06-25 14:08:00 -07001132
joshualitte45c81c2015-12-02 09:05:37 -08001133#if SK_SUPPORT_GPU
1134 SkTArray<SkString> keys;
1135 SkTArray<double> values;
1136 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1137 if (gpuStatsDump) {
1138 // TODO cache stats
1139 bench->getGpuStats(canvas, &keys, &values);
1140 }
1141#endif
1142
robertphillips5b693772014-11-21 06:19:36 -08001143 bench->perCanvasPostDraw(canvas);
1144
egdaniel3bf92062015-06-26 08:12:46 -07001145 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001146 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001147 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001148 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001149 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001150 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001151 }
1152
1153 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001154 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001155 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001156 continue;
1157 }
1158
cdaltone1b89582015-06-25 19:17:08 -07001159 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001160 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001161 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001162 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001163 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001164 log->metric("min_ms", stats.min);
mtkleine5cf2c62016-01-11 11:28:26 -08001165 log->metric("median_ms", stats.median);
joshualitte45c81c2015-12-02 09:05:37 -08001166#if SK_SUPPORT_GPU
1167 if (gpuStatsDump) {
1168 // dump to json, only SKPBench currently returns valid keys / values
1169 SkASSERT(keys.count() == values.count());
1170 for (int i = 0; i < keys.count(); i++) {
1171 log->metric(keys[i].c_str(), values[i]);
1172 }
1173 }
1174#endif
1175
mtkleine070c2b2014-10-14 08:40:43 -07001176 if (runs++ % FLAGS_flushEvery == 0) {
1177 log->flush();
1178 }
mtklein60317d0f2014-07-14 11:30:37 -07001179
bsalomon6eb03cc2014-08-07 14:28:50 -07001180 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001181 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001182 config = ""; // Only print the config if we run the same bench on more than one.
1183 }
mtkleind75c4662015-04-30 07:11:22 -07001184 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1185 , sk_tools::getCurrResidentSetSizeMB()
1186 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001187 , bench->getUniqueName()
1188 , config);
mtkleinf3723212014-06-25 14:08:00 -07001189 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001190 const char* mark = " ";
1191 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1192 if (stddev_percent > 5) mark = "?";
1193 if (stddev_percent > 10) mark = "!";
1194
1195 SkDebugf("%10.2f %s\t%s\t%s\n",
1196 stats.median*1e3, mark, bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001197 } else {
1198 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001199 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1200 , sk_tools::getCurrResidentSetSizeMB()
1201 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001202 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001203 , HUMANIZE(stats.min)
1204 , HUMANIZE(stats.median)
1205 , HUMANIZE(stats.mean)
1206 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001207 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001208 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001209 , config
mtklein96289052014-09-10 12:05:59 -07001210 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001211 );
1212 }
joshualitte45c81c2015-12-02 09:05:37 -08001213
bsalomonb12ea412015-02-02 21:19:50 -08001214#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001215 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001216 GrContext* context = gGrFactory->get(configs[i].ctxType,
kkinnunen3e980c32015-12-23 01:33:00 -08001217 configs[i].ctxOptions);
kkinnunen5219fd92015-12-10 06:28:13 -08001218 context->printCacheStats();
1219 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001220 }
1221#endif
joshualitte45c81c2015-12-02 09:05:37 -08001222
cdalton2c56ba52015-06-26 13:32:53 -07001223 if (FLAGS_verbose) {
1224 SkDebugf("Samples: ");
1225 for (int i = 0; i < samples.count(); i++) {
1226 SkDebugf("%s ", HUMANIZE(samples[i]));
1227 }
1228 SkDebugf("%s\n", bench->getUniqueName());
1229 }
egdaniel3bf92062015-06-26 08:12:46 -07001230 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001231 }
mtkleinf3723212014-06-25 14:08:00 -07001232 }
1233
mtkleine1091452014-12-04 10:47:02 -08001234 log->bench("memory_usage", 0,0);
1235 log->config("meta");
1236 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1237
joshualitte0b19d42015-03-26 10:41:02 -07001238#if SK_SUPPORT_GPU
1239 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1240 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001241 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001242#endif
1243
mtkleinf3723212014-06-25 14:08:00 -07001244 return 0;
1245}
1246
jcgregorio3b27ade2014-11-13 08:06:40 -08001247#if !defined SK_BUILD_FOR_IOS
1248int main(int argc, char** argv) {
1249 SkCommandLineFlags::Parse(argc, argv);
1250 return nanobench_main();
1251}
1252#endif