blob: c0f674fc59d141ed246437aaafb55cf077821549 [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
msarett84451022016-02-11 06:45:51 -080012#include "AndroidCodecBench.h"
mtkleinf3723212014-06-25 14:08:00 -070013#include "Benchmark.h"
msarett7f691442015-09-22 11:56:16 -070014#include "BitmapRegionDecoderBench.h"
scroggo60869a42015-04-01 12:09:17 -070015#include "CodecBench.h"
msarett7f691442015-09-22 11:56:16 -070016#include "CodecBenchPriv.h"
mtkleinf3723212014-06-25 14:08:00 -070017#include "CrashHandler.h"
mtkleine714e752014-07-31 12:13:48 -070018#include "GMBench.h"
mtkleinafb43792014-08-19 15:55:55 -070019#include "ProcStats.h"
mtklein60317d0f2014-07-14 11:30:37 -070020#include "ResultsWriter.h"
mtkleinfd731ce2014-09-10 12:19:30 -070021#include "RecordingBench.h"
joshualitt261c3ad2015-04-27 09:16:57 -070022#include "SKPAnimationBench.h"
mtklein92007582014-08-01 07:46:52 -070023#include "SKPBench.h"
mtkleinf3723212014-06-25 14:08:00 -070024#include "Stats.h"
mtkleinf3723212014-06-25 14:08:00 -070025
msarett84451022016-02-11 06:45:51 -080026#include "SkAndroidCodec.h"
msarett5cb48852015-11-06 08:56:32 -080027#include "SkBitmapRegionDecoder.h"
mtklein6838d852014-10-29 14:15:10 -070028#include "SkBBoxHierarchy.h"
mtkleinf3723212014-06-25 14:08:00 -070029#include "SkCanvas.h"
scroggo60869a42015-04-01 12:09:17 -070030#include "SkCodec.h"
caryclark17f0b6d2014-07-22 10:15:34 -070031#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080032#include "SkCommonFlagsConfig.h"
msarett95f192d2015-02-13 09:05:41 -080033#include "SkData.h"
mtkleinf3723212014-06-25 14:08:00 -070034#include "SkForceLinking.h"
35#include "SkGraphics.h"
mtklein20840502014-08-21 15:51:22 -070036#include "SkOSFile.h"
37#include "SkPictureRecorder.h"
mtklein051e56d2014-12-04 08:46:51 -080038#include "SkPictureUtils.h"
mtkleinf3723212014-06-25 14:08:00 -070039#include "SkString.h"
40#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080041#include "SkTaskGroup.h"
msarettc149f0e2016-01-04 11:35:43 -080042#include "SkThreadUtils.h"
joshualitt3ebd0502016-02-09 07:18:08 -080043#include "ThermalManager.h"
mtkleinf3723212014-06-25 14:08:00 -070044
bungeman60e0fee2015-08-26 05:15:46 -070045#include <stdlib.h>
46
scroggo38ce0a72016-01-07 07:28:47 -080047#ifndef SK_BUILD_FOR_WIN32
48 #include <unistd.h>
49#endif
50
tomhudsond968a6f2015-03-26 11:28:06 -070051#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
52 #include "nanobenchAndroid.h"
53#endif
54
mtkleinbb6a0282014-07-01 08:43:42 -070055#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070056 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070057 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070058 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070059 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070060#endif
61
bsalomon682c2692015-05-22 14:01:46 -070062 struct GrContextOptions;
63
mtkleinf3723212014-06-25 14:08:00 -070064__SK_FORCE_IMAGE_DECODER_LINKING;
65
reed53249782014-10-10 09:09:52 -070066static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070067
mtkleinb5110422014-08-07 15:20:02 -070068static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070069#ifdef SK_DEBUG
70 1;
mtkleina189ccd2014-07-14 12:28:47 -070071#else
bsalomon6eb03cc2014-08-07 14:28:50 -070072 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070073#endif
74
bsalomon6eb03cc2014-08-07 14:28:50 -070075static SkString loops_help_txt() {
76 SkString help;
77 help.printf("Number of times to run each bench. Set this to %d to auto-"
78 "tune for each bench. Timings are only reported when auto-tuning.",
79 kAutoTuneLoops);
80 return help;
81}
82
cdaltone1b89582015-06-25 19:17:08 -070083static SkString to_string(int n) {
84 SkString str;
85 str.appendS32(n);
86 return str;
87}
88
bsalomon6eb03cc2014-08-07 14:28:50 -070089DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
90
mtkleinf3723212014-06-25 14:08:00 -070091DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -070092DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -070093DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
94DEFINE_double(overheadGoal, 0.0001,
95 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070096DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -070097DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070098DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
99 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -0700100
mtklein60317d0f2014-07-14 11:30:37 -0700101DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -0700102DEFINE_int32(maxCalibrationAttempts, 3,
103 "Try up to this many times to guess loops for a bench, or skip the bench.");
104DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700105DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
106DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700107DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
108 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700109DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
robertphillips5b693772014-11-21 06:19:36 -0800110DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700111DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700112DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800113DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800114DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800115DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
msarettc149f0e2016-01-04 11:35:43 -0800116DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
joshualitt3ebd0502016-02-09 07:18:08 -0800117DEFINE_string(useThermalManager, "0,1,10,1000", "enabled,threshold,sleepTimeMs,TimeoutMs for "
118 "thermalManager\n");
mtklein92007582014-08-01 07:46:52 -0700119
mtklein65dfd2f2016-02-03 10:40:54 -0800120DEFINE_string(sourceType, "",
121 "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
122DEFINE_string(benchType, "",
123 "Apply usual --match rules to bench type: micro, recording, playback, skcodec, etc.");
124
mtkleinbbba1682015-10-28 11:36:30 -0700125static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
126
mtkleinf3723212014-06-25 14:08:00 -0700127static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700128 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800129 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700130}
mtklein55b0ffc2014-07-17 08:38:23 -0700131#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700132
tomhudsond968a6f2015-03-26 11:28:06 -0700133bool Target::init(SkImageInfo info, Benchmark* bench) {
134 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700135 this->surface = SkSurface::MakeRaster(info);
136 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700137 return false;
138 }
139 }
140 return true;
141}
142bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700143 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700144 if (!canvas) {
145 return false;
146 }
147 bmp->setInfo(canvas->imageInfo());
148 if (!canvas->readPixels(bmp, 0, 0)) {
149 SkDebugf("Can't read canvas pixels.\n");
150 return false;
151 }
152 return true;
153}
154
155#if SK_SUPPORT_GPU
156struct GPUTarget : public Target {
halcanary96fcdcc2015-08-27 07:41:13 -0700157 explicit GPUTarget(const Config& c) : Target(c), gl(nullptr) { }
tomhudsond968a6f2015-03-26 11:28:06 -0700158 SkGLContext* gl;
159
160 void setup() override {
161 this->gl->makeCurrent();
162 // Make sure we're done with whatever came before.
163 SK_GL(*this->gl, Finish());
164 }
165 void endTiming() override {
166 if (this->gl) {
167 SK_GL(*this->gl, Flush());
joshualitt01836ad2016-01-20 13:09:12 -0800168 this->gl->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700169 }
170 }
171 void fence() override {
172 SK_GL(*this->gl, Finish());
173 }
mtkleind75c4662015-04-30 07:11:22 -0700174
cdaltond416a5b2015-06-23 13:23:44 -0700175 bool needsFrameTiming(int* maxFrameLag) const override {
176 if (!this->gl->getMaxGpuFrameLag(maxFrameLag)) {
177 // Frame lag is unknown.
178 *maxFrameLag = FLAGS_gpuFrameLag;
179 }
180 return true;
181 }
tomhudsond968a6f2015-03-26 11:28:06 -0700182 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700183 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
184 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700185 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700186 this->surface = SkSurface::MakeRenderTarget(gGrFactory->get(this->config.ctxType,
187 this->config.ctxOptions),
bsalomon5ec26ae2016-02-25 08:33:02 -0800188 SkBudgeted::kNo, info,
reede8f30622016-03-23 18:59:25 -0700189 this->config.samples, &props);
kkinnunen3e980c32015-12-23 01:33:00 -0800190 this->gl = gGrFactory->getContextInfo(this->config.ctxType,
kkinnunen34058002016-01-06 23:49:30 -0800191 this->config.ctxOptions).fGLContext;
tomhudsond968a6f2015-03-26 11:28:06 -0700192 if (!this->surface.get()) {
193 return false;
194 }
cdaltond416a5b2015-06-23 13:23:44 -0700195 if (!this->gl->fenceSyncSupport()) {
196 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800197 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700198 }
tomhudsond968a6f2015-03-26 11:28:06 -0700199 return true;
200 }
201 void fillOptions(ResultsWriter* log) override {
202 const GrGLubyte* version;
203 SK_GL_RET(*this->gl, version, GetString(GR_GL_VERSION));
204 log->configOption("GL_VERSION", (const char*)(version));
205
206 SK_GL_RET(*this->gl, version, GetString(GR_GL_RENDERER));
207 log->configOption("GL_RENDERER", (const char*) version);
208
209 SK_GL_RET(*this->gl, version, GetString(GR_GL_VENDOR));
210 log->configOption("GL_VENDOR", (const char*) version);
211
212 SK_GL_RET(*this->gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
213 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
214 }
215};
mtkleind75c4662015-04-30 07:11:22 -0700216
tomhudsond968a6f2015-03-26 11:28:06 -0700217#endif
218
tomhudson75a0ebb2015-03-27 12:11:44 -0700219static double time(int loops, Benchmark* bench, Target* target) {
220 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700221 if (canvas) {
222 canvas->clear(SK_ColorWHITE);
223 }
joshualitt8a6697a2015-09-30 12:11:07 -0700224 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700225 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700226 canvas = target->beginTiming(canvas);
227 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700228 if (canvas) {
229 canvas->flush();
230 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700231 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700232 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700233 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700234 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700235}
236
mtkleinf3723212014-06-25 14:08:00 -0700237static double estimate_timer_overhead() {
238 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700239 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700240 double start = now_ms();
241 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700242 }
243 return overhead / FLAGS_overheadLoops;
244}
245
reed53249782014-10-10 09:09:52 -0700246static int detect_forever_loops(int loops) {
247 // look for a magic run-forever value
248 if (loops < 0) {
249 loops = SK_MaxS32;
250 }
251 return loops;
252}
253
mtklein55b0ffc2014-07-17 08:38:23 -0700254static int clamp_loops(int loops) {
255 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800256 SkDebugf("ERROR: clamping loops from %d to 1. "
257 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700258 return 1;
259 }
260 if (loops > FLAGS_maxLoops) {
261 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
262 return FLAGS_maxLoops;
263 }
264 return loops;
265}
266
tomhudsond968a6f2015-03-26 11:28:06 -0700267static bool write_canvas_png(Target* target, const SkString& filename) {
268
bsalomon6eb03cc2014-08-07 14:28:50 -0700269 if (filename.isEmpty()) {
270 return false;
271 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700272 if (target->getCanvas() &&
273 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700274 return false;
275 }
tomhudsond968a6f2015-03-26 11:28:06 -0700276
bsalomon6eb03cc2014-08-07 14:28:50 -0700277 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700278
279 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700280 return false;
281 }
tomhudsond968a6f2015-03-26 11:28:06 -0700282
bsalomon6eb03cc2014-08-07 14:28:50 -0700283 SkString dir = SkOSPath::Dirname(filename.c_str());
284 if (!sk_mkdir(dir.c_str())) {
285 SkDebugf("Can't make dir %s.\n", dir.c_str());
286 return false;
287 }
288 SkFILEWStream stream(filename.c_str());
289 if (!stream.isValid()) {
290 SkDebugf("Can't write %s.\n", filename.c_str());
291 return false;
292 }
293 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
294 SkDebugf("Can't encode a PNG.\n");
295 return false;
296 }
297 return true;
298}
299
300static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700301static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700302 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700303 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700304 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700305 int loops = bench->calculateLoops(FLAGS_loops);
306 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700307 while (bench_plus_overhead < overhead) {
308 if (round++ == FLAGS_maxCalibrationAttempts) {
309 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700310 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700311 return kFailedLoops;
312 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700313 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700314 }
mtklein2069e222014-08-04 13:57:39 -0700315 }
mtkleinf3723212014-06-25 14:08:00 -0700316
mtkleinbb6a0282014-07-01 08:43:42 -0700317 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700318 // We'll pick N to make timer overhead negligible:
319 //
mtkleinbb6a0282014-07-01 08:43:42 -0700320 // overhead
321 // ------------------------- < FLAGS_overheadGoal
322 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700323 //
mtkleinbb6a0282014-07-01 08:43:42 -0700324 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700325 //
326 // Doing some math, we get:
327 //
mtkleinbb6a0282014-07-01 08:43:42 -0700328 // (overhead / FLAGS_overheadGoal) - overhead
329 // ------------------------------------------ < N
330 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700331 //
332 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700333 if (kAutoTuneLoops == loops) {
334 const double numer = overhead / FLAGS_overheadGoal - overhead;
335 const double denom = bench_plus_overhead - overhead;
336 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700337 loops = clamp_loops(loops);
338 } else {
339 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700340 }
mtkleinbb6a0282014-07-01 08:43:42 -0700341
mtkleinbb6a0282014-07-01 08:43:42 -0700342 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700343}
344
cdaltone1b89582015-06-25 19:17:08 -0700345static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700346 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700347 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700348 if (kAutoTuneLoops == loops) {
349 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700350 double elapsed = 0;
351 do {
mtklein527930f2014-11-06 08:04:34 -0800352 if (1<<30 == loops) {
353 // We're about to wrap. Something's wrong with the bench.
354 loops = 0;
355 break;
356 }
mtkleina189ccd2014-07-14 12:28:47 -0700357 loops *= 2;
358 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700359 // _this_ round, not still timing last round.
360 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700361 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700362 }
363 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700364
mtkleina189ccd2014-07-14 12:28:47 -0700365 // We've overshot at least a little. Scale back linearly.
366 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700367 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700368
tomhudsond968a6f2015-03-26 11:28:06 -0700369 // Make sure we're not still timing our calibration.
370 target->fence();
reed53249782014-10-10 09:09:52 -0700371 } else {
372 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700373 }
mtkleinbb6a0282014-07-01 08:43:42 -0700374
375 // Pretty much the same deal as the calibration: do some warmup to make
376 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700377 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700378 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700379 }
mtkleinbb6a0282014-07-01 08:43:42 -0700380
mtkleinbb6a0282014-07-01 08:43:42 -0700381 return loops;
382}
mtkleinbb6a0282014-07-01 08:43:42 -0700383
bsalomonc2553372014-07-22 13:09:05 -0700384#if SK_SUPPORT_GPU
385#define kBogusGLContextType GrContextFactory::kNative_GLContextType
kkinnunen5219fd92015-12-10 06:28:13 -0800386#define kBogusGLContextOptions GrContextFactory::kNone_GLContextOptions
bsalomonc2553372014-07-22 13:09:05 -0700387#else
388#define kBogusGLContextType 0
kkinnunen5219fd92015-12-10 06:28:13 -0800389#define kBogusGLContextOptions 0
mtkleine714e752014-07-31 12:13:48 -0700390#endif
bsalomonc2553372014-07-22 13:09:05 -0700391
svaisanenc47635e2016-01-28 06:05:43 -0800392static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
393
394#if SK_SUPPORT_GPU
395 if (const auto* gpuConfig = config->asConfigGpu()) {
396 if (!FLAGS_gpu)
397 return;
398
mtkleinbb6c41b2016-03-08 11:31:11 -0800399 const auto ctxOptions = gpuConfig->getUseNVPR() ? GrContextFactory::kEnableNVPR_GLContextOptions
svaisanenc47635e2016-01-28 06:05:43 -0800400 : GrContextFactory::kNone_GLContextOptions;
401 const auto ctxType = gpuConfig->getContextType();
402 const auto sampleCount = gpuConfig->getSamples();
403
404 if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOptions)) {
405 const auto maxSampleCount = ctx->caps()->maxSampleCount();
406 if (sampleCount > ctx->caps()->maxSampleCount()) {
407 SkDebugf("Configuration sample count %d exceeds maximum %d.\n",
408 sampleCount, maxSampleCount);
409 return;
410 }
411 } else {
412 SkDebugf("No context was available matching config type and options.\n");
413 return;
414 }
415
416 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800417 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800418 Benchmark::kGPU_Backend,
419 kN32_SkColorType,
420 kPremul_SkAlphaType,
mtkleinbb6c41b2016-03-08 11:31:11 -0800421 kLinear_SkColorProfileType,
svaisanenc47635e2016-01-28 06:05:43 -0800422 sampleCount,
423 ctxType,
kkinnunend4e1c352016-03-01 23:41:26 -0800424 ctxOptions,
425 gpuConfig->getUseDIText()
426 };
svaisanenc47635e2016-01-28 06:05:43 -0800427
428 configs->push_back(target);
429 return;
430 }
431#endif
432
mtkleinbb6c41b2016-03-08 11:31:11 -0800433 #define CPU_CONFIG(name, backend, color, alpha, profile) \
434 if (config->getTag().equals(#name)) { \
435 Config config = { \
436 SkString(#name), Benchmark::backend, color, alpha, profile, \
437 0, kBogusGLContextType, kBogusGLContextOptions, false \
438 }; \
439 configs->push_back(config); \
440 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700441 }
mtkleine714e752014-07-31 12:13:48 -0700442
mtklein40b32be2014-07-09 08:46:49 -0700443 if (FLAGS_cpu) {
mtkleinbb6c41b2016-03-08 11:31:11 -0800444 CPU_CONFIG(nonrendering, kNonRendering_Backend,
445 kUnknown_SkColorType, kUnpremul_SkAlphaType, kLinear_SkColorProfileType);
446
447 CPU_CONFIG(8888, kRaster_Backend,
448 kN32_SkColorType, kPremul_SkAlphaType, kLinear_SkColorProfileType)
449 CPU_CONFIG(565, kRaster_Backend,
450 kRGB_565_SkColorType, kOpaque_SkAlphaType, kLinear_SkColorProfileType)
451 CPU_CONFIG(srgb, kRaster_Backend,
452 kN32_SkColorType, kPremul_SkAlphaType, kSRGB_SkColorProfileType)
453 CPU_CONFIG(f16, kRaster_Backend,
454 kRGBA_F16_SkColorType, kPremul_SkAlphaType, kLinear_SkColorProfileType)
mtklein40b32be2014-07-09 08:46:49 -0700455 }
mtkleinbb6a0282014-07-01 08:43:42 -0700456
svaisanenc47635e2016-01-28 06:05:43 -0800457 #undef CPU_CONFIG
tomhudsond968a6f2015-03-26 11:28:06 -0700458
459#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
djsollen817c72a2016-01-28 13:57:02 -0800460 if (config->getTag().equals("hwui")) {
mtkleinbb6c41b2016-03-08 11:31:11 -0800461 Config config = { SkString("hwui"), Benchmark::kHWUI_Backend,
462 kRGBA_8888_SkColorType, kPremul_SkAlphaType, kLinear_SkColorProfileType,
463 0, kBogusGLContextType, kBogusGLContextOptions, false };
svaisanenc47635e2016-01-28 06:05:43 -0800464 configs->push_back(config);
tomhudsond968a6f2015-03-26 11:28:06 -0700465 }
466#endif
mtkleinf3723212014-06-25 14:08:00 -0700467}
468
svaisanenc47635e2016-01-28 06:05:43 -0800469// Append all configs that are enabled and supported.
470void create_configs(SkTArray<Config>* configs) {
471 SkCommandLineConfigArray array;
472 ParseConfigs(FLAGS_config, &array);
473 for (int i = 0; i < array.count(); ++i) {
474 create_config(array[i], configs);
475 }
476}
477
halcanary96fcdcc2015-08-27 07:41:13 -0700478// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700479static Target* is_enabled(Benchmark* bench, const Config& config) {
480 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700481 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700482 }
483
reede5ea5002014-09-03 11:54:58 -0700484 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
mtkleinbb6c41b2016-03-08 11:31:11 -0800485 config.color, config.alpha, config.profile);
bsalomonc2553372014-07-22 13:09:05 -0700486
halcanary96fcdcc2015-08-27 07:41:13 -0700487 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700488
tomhudsond968a6f2015-03-26 11:28:06 -0700489 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700490#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700491 case Benchmark::kGPU_Backend:
492 target = new GPUTarget(config);
493 break;
bsalomonc2553372014-07-22 13:09:05 -0700494#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700495#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
496 case Benchmark::kHWUI_Backend:
497 target = new HWUITarget(config, bench);
498 break;
499#endif
500 default:
501 target = new Target(config);
502 break;
503 }
bsalomonc2553372014-07-22 13:09:05 -0700504
tomhudsond968a6f2015-03-26 11:28:06 -0700505 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700506 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700507 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700508 }
509 return target;
510}
511
msarett5cb48852015-11-06 08:56:32 -0800512static bool valid_brd_bench(SkData* encoded, SkBitmapRegionDecoder::Strategy strategy,
msarett7f691442015-09-22 11:56:16 -0700513 SkColorType colorType, uint32_t sampleSize, uint32_t minOutputSize, int* width,
514 int* height) {
msarett5cb48852015-11-06 08:56:32 -0800515 SkAutoTDelete<SkBitmapRegionDecoder> brd(
516 SkBitmapRegionDecoder::Create(encoded, strategy));
msarett7f691442015-09-22 11:56:16 -0700517 if (nullptr == brd.get()) {
518 // This is indicates that subset decoding is not supported for a particular image format.
519 return false;
520 }
521
msarett35e5d1b2015-10-27 12:50:25 -0700522 SkBitmap bitmap;
523 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, brd->width(), brd->height()),
524 1, colorType, false)) {
525 return false;
526 }
msarett7f691442015-09-22 11:56:16 -0700527
528 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
529 (uint32_t) brd->height()) {
530 // This indicates that the image is not large enough to decode a
531 // minOutputSize x minOutputSize subset at the given sampleSize.
532 return false;
533 }
534
535 // Set the image width and height. The calling code will use this to choose subsets to decode.
536 *width = brd->width();
537 *height = brd->height();
538 return true;
539}
540
egdaniel3bf92062015-06-26 08:12:46 -0700541static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700542 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700543#if SK_SUPPORT_GPU
544 if (FLAGS_abandonGpuContext) {
545 gGrFactory->abandonContexts();
546 }
547 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
548 gGrFactory->destroyContexts();
549 }
550#endif
551}
552
mtkleine714e752014-07-31 12:13:48 -0700553class BenchmarkStream {
554public:
mtklein92007582014-08-01 07:46:52 -0700555 BenchmarkStream() : fBenches(BenchRegistry::Head())
556 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700557 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700558 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800559 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800560 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700561 , fCurrentCodec(0)
msarett84451022016-02-11 06:45:51 -0800562 , fCurrentAndroidCodec(0)
msarett7f691442015-09-22 11:56:16 -0700563 , fCurrentBRDImage(0)
msarett95f192d2015-02-13 09:05:41 -0800564 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800565 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700566 , fCurrentSubsetType(0)
msarett7f691442015-09-22 11:56:16 -0700567 , fCurrentBRDStrategy(0)
msarett84451022016-02-11 06:45:51 -0800568 , fCurrentSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700569 , fCurrentAnimSKP(0) {
mtklein92007582014-08-01 07:46:52 -0700570 for (int i = 0; i < FLAGS_skps.count(); i++) {
571 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
572 fSKPs.push_back() = FLAGS_skps[i];
573 } else {
574 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
575 SkString path;
576 while (it.next(&path)) {
577 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
578 }
579 }
580 }
mtkleine714e752014-07-31 12:13:48 -0700581
mtklein92007582014-08-01 07:46:52 -0700582 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
583 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
584 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
585 exit(1);
586 }
587
588 for (int i = 0; i < FLAGS_scales.count(); i++) {
589 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
590 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
591 exit(1);
592 }
593 }
robertphillips5b693772014-11-21 06:19:36 -0800594
cdalton63a82852015-06-29 14:06:10 -0700595 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
596 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700597 exit(1);
598 }
599
robertphillips5b693772014-11-21 06:19:36 -0800600 if (FLAGS_mpd) {
601 fUseMPDs.push_back() = true;
602 }
mtkleinc751ecb2015-06-15 08:56:38 -0700603 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700604
msarett95f192d2015-02-13 09:05:41 -0800605 // Prepare the images for decoding
scroggo86737142016-02-03 12:19:11 -0800606 if (!CollectImages(&fImages)) {
607 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800608 }
mtklein95553d92015-03-12 08:24:21 -0700609
msarett95f192d2015-02-13 09:05:41 -0800610 // Choose the candidate color types for image decoding
611 const SkColorType colorTypes[] =
msarettb23e6aa2015-06-09 13:56:10 -0700612 { kN32_SkColorType,
613 kRGB_565_SkColorType,
614 kAlpha_8_SkColorType,
615 kIndex_8_SkColorType,
616 kGray_8_SkColorType };
msarett74deb982015-10-20 16:45:56 -0700617 fColorTypes.reset(colorTypes, SK_ARRAY_COUNT(colorTypes));
mtklein92007582014-08-01 07:46:52 -0700618 }
619
reedca2622b2016-03-18 07:25:55 -0700620 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700621 // Not strictly necessary, as it will be checked again later,
622 // but helps to avoid a lot of pointless work if we're going to skip it.
cdalton91e457d2016-02-17 11:10:16 -0800623 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700624 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700625 }
626
scroggoa1193e42015-01-21 12:09:53 -0800627 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -0700628 if (stream.get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700629 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700630 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700631 }
632
reedca2622b2016-03-18 07:25:55 -0700633 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700634 }
635
mtklein92007582014-08-01 07:46:52 -0700636 Benchmark* next() {
mtklein65dfd2f2016-02-03 10:40:54 -0800637 SkAutoTDelete<Benchmark> bench;
638 do {
639 bench.reset(this->rawNext());
640 if (!bench) {
641 return nullptr;
642 }
643 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
644 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700645 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800646 }
647
648 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700649 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700650 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700651 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700652 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700653 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700654 return bench;
655 }
mtklein92007582014-08-01 07:46:52 -0700656
mtkleine714e752014-07-31 12:13:48 -0700657 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -0700658 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700659 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800660 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700661 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700662 fBenchType = "micro";
mtklein18300a32016-03-16 13:53:35 -0700663 return new GMBench(gm.release());
mtkleine714e752014-07-31 12:13:48 -0700664 }
665 }
mtklein92007582014-08-01 07:46:52 -0700666
mtkleinfd731ce2014-09-10 12:19:30 -0700667 // First add all .skps as RecordingBenches.
668 while (fCurrentRecording < fSKPs.count()) {
669 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700670 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
671 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700672 continue;
673 }
674 SkString name = SkOSPath::Basename(path.c_str());
675 fSourceType = "skp";
676 fBenchType = "recording";
reedca2622b2016-03-18 07:25:55 -0700677 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic.get()));
mtklein051e56d2014-12-04 08:46:51 -0800678 fSKPOps = pic->approximateOpCount();
halcanary385fe4d2015-08-26 13:07:48 -0700679 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700680 }
681
682 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700683 while (fCurrentScale < fScales.count()) {
684 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800685 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700686 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
687 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800688 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700689 continue;
690 }
robertphillips5b693772014-11-21 06:19:36 -0800691
692 while (fCurrentUseMPD < fUseMPDs.count()) {
693 if (FLAGS_bbh) {
694 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
695 SkRTreeFactory factory;
696 SkPictureRecorder recorder;
697 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
698 pic->playback(recorder.beginRecording(pic->cullRect().width(),
699 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800700 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800701 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
reedca2622b2016-03-18 07:25:55 -0700702 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800703 }
704 SkString name = SkOSPath::Basename(path.c_str());
705 fSourceType = "skp";
706 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700707 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
708 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700709 }
robertphillips5b693772014-11-21 06:19:36 -0800710 fCurrentUseMPD = 0;
711 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700712 }
713 fCurrentSKP = 0;
714 fCurrentScale++;
715 }
716
joshualitt261c3ad2015-04-27 09:16:57 -0700717 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700718 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700719 while (fCurrentAnimSKP < fSKPs.count()) {
720 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700721 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
722 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700723 fCurrentAnimSKP++;
724 continue;
725 }
726
727 fCurrentAnimSKP++;
728 SkString name = SkOSPath::Basename(path.c_str());
cdalton63a82852015-06-29 14:06:10 -0700729 SkAutoTUnref<SKPAnimationBench::Animation> animation(
730 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
halcanary385fe4d2015-08-26 13:07:48 -0700731 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation,
732 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700733 }
734 }
735
scroggo60869a42015-04-01 12:09:17 -0700736 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700737 fSourceType = "image";
738 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700739 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800740 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
741 continue;
742 }
scroggo60869a42015-04-01 12:09:17 -0700743 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
744 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700745 if (!codec) {
746 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700747 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700748 continue;
749 }
scroggo21027992015-04-02 13:22:38 -0700750
scroggo60869a42015-04-01 12:09:17 -0700751 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700752 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700753
msarettc7796b92016-01-07 14:20:20 -0800754 SkAlphaType alphaType = codec->getInfo().alphaType();
755 switch (alphaType) {
756 case kOpaque_SkAlphaType:
757 // We only need to test one alpha type (opaque).
758 fCurrentColorType++;
759 break;
760 case kUnpremul_SkAlphaType:
761 case kPremul_SkAlphaType:
762 if (0 == fCurrentAlphaType) {
763 // Test unpremul first.
764 alphaType = kUnpremul_SkAlphaType;
765 fCurrentAlphaType++;
766 } else {
767 // Test premul.
768 alphaType = kPremul_SkAlphaType;
769 fCurrentAlphaType = 0;
770 fCurrentColorType++;
771 }
772 break;
773 default:
774 SkASSERT(false);
775 fCurrentColorType++;
776 break;
scroggo21027992015-04-02 13:22:38 -0700777 }
778
msarettc7796b92016-01-07 14:20:20 -0800779 // Make sure we can decode to this color type and alpha type.
780 SkImageInfo info =
781 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700782 const size_t rowBytes = info.minRowBytes();
783 SkAutoMalloc storage(info.getSafeSize(rowBytes));
784
785 // Used if fCurrentColorType is kIndex_8_SkColorType
786 int colorCount = 256;
787 SkPMColor colors[256];
788
scroggoeb602a52015-07-09 08:16:03 -0700789 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700790 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700791 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700792 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700793 case SkCodec::kSuccess:
794 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700795 return new CodecBench(SkOSPath::Basename(path.c_str()),
msarettc7796b92016-01-07 14:20:20 -0800796 encoded, colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700797 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700798 // This is okay. Not all conversions are valid.
799 break;
scroggo60869a42015-04-01 12:09:17 -0700800 default:
801 // This represents some sort of failure.
802 SkASSERT(false);
803 break;
804 }
805 }
806 fCurrentColorType = 0;
807 }
808
msarett84451022016-02-11 06:45:51 -0800809 // Run AndroidCodecBenches
810 const int sampleSizes[] = { 2, 4, 8 };
811 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
812 fSourceType = "image";
813 fBenchType = "skandroidcodec";
814
815 const SkString& path = fImages[fCurrentAndroidCodec];
816 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
817 continue;
818 }
819 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
820 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
821 if (!codec) {
822 // Nothing to time.
823 SkDebugf("Cannot find codec for %s\n", path.c_str());
824 continue;
825 }
826
827 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
828 int sampleSize = sampleSizes[fCurrentSampleSize];
829 fCurrentSampleSize++;
830 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
831 // Avoid benchmarking scaled decodes of already small images.
832 break;
833 }
834
835 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()), encoded, sampleSize);
836 }
837 fCurrentSampleSize = 0;
838 }
839
msarett7f691442015-09-22 11:56:16 -0700840 // Run the BRDBenches
841 // We will benchmark multiple BRD strategies.
scroggo303fa352015-10-05 11:03:34 -0700842 static const struct {
msarett5cb48852015-11-06 08:56:32 -0800843 SkBitmapRegionDecoder::Strategy fStrategy;
msarett0459c942015-11-10 14:52:13 -0800844 const char* fName;
scroggo303fa352015-10-05 11:03:34 -0700845 } strategies[] = {
msarett0459c942015-11-10 14:52:13 -0800846 { SkBitmapRegionDecoder::kCanvas_Strategy, "BRD_canvas" },
msarett5cb48852015-11-06 08:56:32 -0800847 { SkBitmapRegionDecoder::kAndroidCodec_Strategy, "BRD_android_codec" },
msarett7f691442015-09-22 11:56:16 -0700848 };
849
850 // We intend to create benchmarks that model the use cases in
851 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
852 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
853 // the image. For that reason, we will benchmark decodes in five representative locations
854 // in the image. Additionally, this use case utilizes power of two scaling, so we will
855 // test on power of two sample sizes. The output tile is always 512x512, so, when a
856 // sampleSize is used, the size of the subset that is decoded is always
857 // (sampleSize*512)x(sampleSize*512).
858 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700859 // All use cases we are aware of only scale by powers of two.
860 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
861 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800862 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700863 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800864 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
865 const SkString& path = fImages[fCurrentBRDImage];
866 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
867 continue;
868 }
msarett7f691442015-09-22 11:56:16 -0700869 while (fCurrentBRDStrategy < (int) SK_ARRAY_COUNT(strategies)) {
scroggo303fa352015-10-05 11:03:34 -0700870 fSourceType = "image";
871 fBenchType = strategies[fCurrentBRDStrategy].fName;
scroggo860e8a62015-10-15 07:51:28 -0700872
msarett5cb48852015-11-06 08:56:32 -0800873 const SkBitmapRegionDecoder::Strategy strategy =
scroggo860e8a62015-10-15 07:51:28 -0700874 strategies[fCurrentBRDStrategy].fStrategy;
875
msarett7f691442015-09-22 11:56:16 -0700876 while (fCurrentColorType < fColorTypes.count()) {
msarett84451022016-02-11 06:45:51 -0800877 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
msarett7f691442015-09-22 11:56:16 -0700878 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700879
msarett7f691442015-09-22 11:56:16 -0700880 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
881 const SkColorType colorType = fColorTypes[fCurrentColorType];
msarett84451022016-02-11 06:45:51 -0800882 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
msarett7f691442015-09-22 11:56:16 -0700883 int currentSubsetType = fCurrentSubsetType++;
884
885 int width = 0;
886 int height = 0;
887 if (!valid_brd_bench(encoded.get(), strategy, colorType, sampleSize,
888 minOutputSize, &width, &height)) {
889 break;
890 }
891
892 SkString basename = SkOSPath::Basename(path.c_str());
893 SkIRect subset;
894 const uint32_t subsetSize = sampleSize * minOutputSize;
895 switch (currentSubsetType) {
896 case kTopLeft_SubsetType:
897 basename.append("_TopLeft");
898 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
899 break;
900 case kTopRight_SubsetType:
901 basename.append("_TopRight");
902 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
903 subsetSize);
904 break;
905 case kMiddle_SubsetType:
906 basename.append("_Middle");
907 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
908 (height - subsetSize) / 2, subsetSize, subsetSize);
909 break;
910 case kBottomLeft_SubsetType:
911 basename.append("_BottomLeft");
912 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
913 subsetSize);
914 break;
915 case kBottomRight_SubsetType:
916 basename.append("_BottomRight");
917 subset = SkIRect::MakeXYWH(width - subsetSize,
918 height - subsetSize, subsetSize, subsetSize);
919 break;
920 default:
921 SkASSERT(false);
922 }
923
924 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
925 strategy, colorType, sampleSize, subset);
926 }
927 fCurrentSubsetType = 0;
msarett84451022016-02-11 06:45:51 -0800928 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -0700929 }
msarett84451022016-02-11 06:45:51 -0800930 fCurrentSampleSize = 0;
msarett7f691442015-09-22 11:56:16 -0700931 fCurrentColorType++;
932 }
933 fCurrentColorType = 0;
934 fCurrentBRDStrategy++;
935 }
936 fCurrentBRDStrategy = 0;
msarett7f691442015-09-22 11:56:16 -0700937 }
938
halcanary96fcdcc2015-08-27 07:41:13 -0700939 return nullptr;
mtkleine714e752014-07-31 12:13:48 -0700940 }
mtklein92007582014-08-01 07:46:52 -0700941
942 void fillCurrentOptions(ResultsWriter* log) const {
943 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700944 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700945 if (0 == strcmp(fSourceType, "skp")) {
946 log->configOption("clip",
947 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
948 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -0800949 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -0700950 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -0800951 if (fCurrentUseMPD > 0) {
952 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
953 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
954 }
mtklein92007582014-08-01 07:46:52 -0700955 }
mtklein051e56d2014-12-04 08:46:51 -0800956 if (0 == strcmp(fBenchType, "recording")) {
957 log->metric("bytes", fSKPBytes);
958 log->metric("ops", fSKPOps);
959 }
mtklein92007582014-08-01 07:46:52 -0700960 }
961
mtkleine714e752014-07-31 12:13:48 -0700962private:
msarettb23e6aa2015-06-09 13:56:10 -0700963 enum SubsetType {
964 kTopLeft_SubsetType = 0,
965 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -0700966 kMiddle_SubsetType = 2,
967 kBottomLeft_SubsetType = 3,
968 kBottomRight_SubsetType = 4,
969 kTranslate_SubsetType = 5,
970 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -0700971 kLast_SubsetType = kZoom_SubsetType,
972 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -0700973 };
974
mtkleine714e752014-07-31 12:13:48 -0700975 const BenchRegistry* fBenches;
976 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700977 SkIRect fClip;
978 SkTArray<SkScalar> fScales;
979 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -0800980 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -0800981 SkTArray<SkString> fImages;
msarett74deb982015-10-20 16:45:56 -0700982 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -0700983 SkScalar fZoomMax;
984 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -0700985
mtklein051e56d2014-12-04 08:46:51 -0800986 double fSKPBytes, fSKPOps;
987
mtkleinfd731ce2014-09-10 12:19:30 -0700988 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
989 const char* fBenchType; // How we bench it: micro, recording, playback, ...
990 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700991 int fCurrentScale;
992 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -0800993 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -0700994 int fCurrentCodec;
msarett84451022016-02-11 06:45:51 -0800995 int fCurrentAndroidCodec;
msarett7f691442015-09-22 11:56:16 -0700996 int fCurrentBRDImage;
msarett95f192d2015-02-13 09:05:41 -0800997 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -0800998 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -0700999 int fCurrentSubsetType;
msarett7f691442015-09-22 11:56:16 -07001000 int fCurrentBRDStrategy;
msarett84451022016-02-11 06:45:51 -08001001 int fCurrentSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001002 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001003};
1004
msarettc149f0e2016-01-04 11:35:43 -08001005// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1006// This prints something every once in a while so that it knows we're still working.
1007static void start_keepalive() {
1008 struct Loop {
1009 static void forever(void*) {
1010 for (;;) {
1011 static const int kSec = 1200;
1012 #if defined(SK_BUILD_FOR_WIN)
1013 Sleep(kSec * 1000);
1014 #else
1015 sleep(kSec);
1016 #endif
1017 SkDebugf("\nBenchmarks still running...\n");
1018 }
1019 }
1020 };
1021 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1022 intentionallyLeaked->start();
1023}
1024
jcgregorio3b27ade2014-11-13 08:06:40 -08001025int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001026int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001027 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001028 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001029 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001030
krajcevski69a55602014-08-13 10:46:31 -07001031#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001032 GrContextOptions grContextOpts;
krajcevski12b35442014-08-13 12:06:26 -07001033 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
halcanary385fe4d2015-08-26 13:07:48 -07001034 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001035#endif
1036
bsalomon06cddec2014-10-24 10:40:50 -07001037 if (FLAGS_veryVerbose) {
1038 FLAGS_verbose = true;
1039 }
1040
bsalomon6eb03cc2014-08-07 14:28:50 -07001041 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001042 FLAGS_samples = 1;
1043 FLAGS_gpuFrameLag = 0;
1044 }
1045
bsalomon6eb03cc2014-08-07 14:28:50 -07001046 if (!FLAGS_writePath.isEmpty()) {
1047 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1048 if (!sk_mkdir(FLAGS_writePath[0])) {
1049 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001050 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001051 }
1052 }
1053
halcanary385fe4d2015-08-26 13:07:48 -07001054 SkAutoTDelete<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001055 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001056#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001057 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001058#else
1059 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1060 return 1;
1061#endif
mtklein60317d0f2014-07-14 11:30:37 -07001062 }
mtklein1915b622014-08-20 11:45:00 -07001063
1064 if (1 == FLAGS_properties.count() % 2) {
1065 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1066 return 1;
1067 }
1068 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1069 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1070 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001071
1072 if (1 == FLAGS_key.count() % 2) {
1073 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1074 return 1;
1075 }
1076 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001077 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001078 }
mtklein60317d0f2014-07-14 11:30:37 -07001079
mtkleinf3723212014-06-25 14:08:00 -07001080 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001081 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001082
cdaltone1b89582015-06-25 19:17:08 -07001083 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001084
bsalomon6eb03cc2014-08-07 14:28:50 -07001085 if (kAutoTuneLoops != FLAGS_loops) {
1086 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001087 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001088 SkDebugf("! -> high variance, ? -> moderate variance\n");
1089 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001090 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001091 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001092 } else {
mtkleind75c4662015-04-30 07:11:22 -07001093 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001094 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001095 }
1096
svaisanenc47635e2016-01-28 06:05:43 -08001097 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001098 create_configs(&configs);
1099
joshualitt3ebd0502016-02-09 07:18:08 -08001100#ifdef THERMAL_MANAGER_SUPPORTED
1101 int tmEnabled, tmThreshold, tmSleepTimeMs, tmTimeoutMs;
1102 if (4 != sscanf(FLAGS_useThermalManager[0], "%d,%d,%d,%d",
1103 &tmEnabled, &tmThreshold, &tmSleepTimeMs, &tmTimeoutMs)) {
1104 SkDebugf("Can't parse %s from --useThermalManager.\n", FLAGS_useThermalManager[0]);
1105 exit(1);
1106 }
1107 ThermalManager tm(tmThreshold, tmSleepTimeMs, tmTimeoutMs);
1108#endif
1109
msarettc149f0e2016-01-04 11:35:43 -08001110 if (FLAGS_keepAlive) {
1111 start_keepalive();
1112 }
1113
mtkleine070c2b2014-10-14 08:40:43 -07001114 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001115 BenchmarkStream benchStream;
1116 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001117 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001118 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001119 continue;
1120 }
1121
svaisanenc47635e2016-01-28 06:05:43 -08001122 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001123 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001124 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001125 }
egdaniel3bf92062015-06-26 08:12:46 -07001126 for (int i = 0; i < configs.count(); ++i) {
joshualitt3ebd0502016-02-09 07:18:08 -08001127#ifdef THERMAL_MANAGER_SUPPORTED
1128 if (tmEnabled && !tm.coolOffIfNecessary()) {
1129 SkDebugf("Could not cool off, timings will be throttled\n");
1130 }
1131#endif
egdaniel3bf92062015-06-26 08:12:46 -07001132 Target* target = is_enabled(b, configs[i]);
1133 if (!target) {
1134 continue;
1135 }
mtkleinf3723212014-06-25 14:08:00 -07001136
halcanary96fcdcc2015-08-27 07:41:13 -07001137 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001138 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001139 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001140
brianosman7a5ada82016-02-08 13:49:12 -08001141 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001142 SkDebugf("Running %s\t%s\n"
1143 , bench->getUniqueName()
1144 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001145 if (FLAGS_dryRun) {
1146 continue;
1147 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001148 }
1149
egdaniel3bf92062015-06-26 08:12:46 -07001150 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001151 bench->perCanvasPreDraw(canvas);
1152
cdaltone1b89582015-06-25 19:17:08 -07001153 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001154 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001155 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1156 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001157
mtkleinbbba1682015-10-28 11:36:30 -07001158 if (FLAGS_ms) {
1159 samples.reset();
1160 auto stop = now_ms() + FLAGS_ms;
1161 do {
1162 samples.push_back(time(loops, bench, target) / loops);
1163 } while (now_ms() < stop);
1164 } else {
cdaltone1b89582015-06-25 19:17:08 -07001165 samples.reset(FLAGS_samples);
1166 for (int s = 0; s < FLAGS_samples; s++) {
egdaniel3bf92062015-06-26 08:12:46 -07001167 samples[s] = time(loops, bench, target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001168 }
cdaltone1b89582015-06-25 19:17:08 -07001169 }
mtkleinf3723212014-06-25 14:08:00 -07001170
joshualitte45c81c2015-12-02 09:05:37 -08001171#if SK_SUPPORT_GPU
1172 SkTArray<SkString> keys;
1173 SkTArray<double> values;
1174 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1175 if (gpuStatsDump) {
1176 // TODO cache stats
1177 bench->getGpuStats(canvas, &keys, &values);
1178 }
1179#endif
1180
robertphillips5b693772014-11-21 06:19:36 -08001181 bench->perCanvasPostDraw(canvas);
1182
egdaniel3bf92062015-06-26 08:12:46 -07001183 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001184 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001185 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001186 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001187 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001188 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001189 }
1190
1191 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001192 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001193 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001194 continue;
1195 }
1196
cdaltone1b89582015-06-25 19:17:08 -07001197 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001198 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001199 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001200 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001201 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001202 log->metric("min_ms", stats.min);
joshualitte45c81c2015-12-02 09:05:37 -08001203#if SK_SUPPORT_GPU
1204 if (gpuStatsDump) {
1205 // dump to json, only SKPBench currently returns valid keys / values
1206 SkASSERT(keys.count() == values.count());
1207 for (int i = 0; i < keys.count(); i++) {
1208 log->metric(keys[i].c_str(), values[i]);
1209 }
1210 }
1211#endif
1212
mtkleine070c2b2014-10-14 08:40:43 -07001213 if (runs++ % FLAGS_flushEvery == 0) {
1214 log->flush();
1215 }
mtklein60317d0f2014-07-14 11:30:37 -07001216
bsalomon6eb03cc2014-08-07 14:28:50 -07001217 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001218 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001219 config = ""; // Only print the config if we run the same bench on more than one.
1220 }
mtkleind75c4662015-04-30 07:11:22 -07001221 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1222 , sk_tools::getCurrResidentSetSizeMB()
1223 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001224 , bench->getUniqueName()
1225 , config);
mtkleinf3723212014-06-25 14:08:00 -07001226 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001227 const char* mark = " ";
1228 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1229 if (stddev_percent > 5) mark = "?";
1230 if (stddev_percent > 10) mark = "!";
1231
1232 SkDebugf("%10.2f %s\t%s\t%s\n",
1233 stats.median*1e3, mark, bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001234 } else {
1235 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001236 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1237 , sk_tools::getCurrResidentSetSizeMB()
1238 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001239 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001240 , HUMANIZE(stats.min)
1241 , HUMANIZE(stats.median)
1242 , HUMANIZE(stats.mean)
1243 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001244 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001245 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001246 , config
mtklein96289052014-09-10 12:05:59 -07001247 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001248 );
1249 }
joshualitte45c81c2015-12-02 09:05:37 -08001250
bsalomonb12ea412015-02-02 21:19:50 -08001251#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001252 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001253 GrContext* context = gGrFactory->get(configs[i].ctxType,
kkinnunen3e980c32015-12-23 01:33:00 -08001254 configs[i].ctxOptions);
kkinnunen5219fd92015-12-10 06:28:13 -08001255 context->printCacheStats();
1256 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001257 }
1258#endif
joshualitte45c81c2015-12-02 09:05:37 -08001259
cdalton2c56ba52015-06-26 13:32:53 -07001260 if (FLAGS_verbose) {
1261 SkDebugf("Samples: ");
1262 for (int i = 0; i < samples.count(); i++) {
1263 SkDebugf("%s ", HUMANIZE(samples[i]));
1264 }
1265 SkDebugf("%s\n", bench->getUniqueName());
1266 }
egdaniel3bf92062015-06-26 08:12:46 -07001267 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001268 }
mtkleinf3723212014-06-25 14:08:00 -07001269 }
1270
mtkleine1091452014-12-04 10:47:02 -08001271 log->bench("memory_usage", 0,0);
1272 log->config("meta");
1273 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1274
joshualitte0b19d42015-03-26 10:41:02 -07001275#if SK_SUPPORT_GPU
1276 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1277 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001278 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001279#endif
1280
mtkleinf3723212014-06-25 14:08:00 -07001281 return 0;
1282}
1283
jcgregorio3b27ade2014-11-13 08:06:40 -08001284#if !defined SK_BUILD_FOR_IOS
1285int main(int argc, char** argv) {
1286 SkCommandLineFlags::Parse(argc, argv);
1287 return nanobench_main();
1288}
1289#endif