blob: 235de02f578cce7d7b77efc3988fa29bb67dbc2d [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"
bsalomon3724e572016-03-30 18:56:19 -070059 #include "gl/GrGLUtil.h"
60 using sk_gpu_test::GrContextFactory;
61 using sk_gpu_test::GLContext;
krajcevski69a55602014-08-13 10:46:31 -070062 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070063#endif
64
bsalomon682c2692015-05-22 14:01:46 -070065 struct GrContextOptions;
66
mtkleinf3723212014-06-25 14:08:00 -070067__SK_FORCE_IMAGE_DECODER_LINKING;
68
reed53249782014-10-10 09:09:52 -070069static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070070
mtkleinb5110422014-08-07 15:20:02 -070071static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070072#ifdef SK_DEBUG
73 1;
mtkleina189ccd2014-07-14 12:28:47 -070074#else
bsalomon6eb03cc2014-08-07 14:28:50 -070075 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070076#endif
77
bsalomon6eb03cc2014-08-07 14:28:50 -070078static SkString loops_help_txt() {
79 SkString help;
80 help.printf("Number of times to run each bench. Set this to %d to auto-"
81 "tune for each bench. Timings are only reported when auto-tuning.",
82 kAutoTuneLoops);
83 return help;
84}
85
cdaltone1b89582015-06-25 19:17:08 -070086static SkString to_string(int n) {
87 SkString str;
88 str.appendS32(n);
89 return str;
90}
91
bsalomon6eb03cc2014-08-07 14:28:50 -070092DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
93
mtkleinf3723212014-06-25 14:08:00 -070094DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -070095DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -070096DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
97DEFINE_double(overheadGoal, 0.0001,
98 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070099DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -0700100DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -0700101DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
102 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -0700103
mtklein60317d0f2014-07-14 11:30:37 -0700104DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -0700105DEFINE_int32(maxCalibrationAttempts, 3,
106 "Try up to this many times to guess loops for a bench, or skip the bench.");
107DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700108DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
109DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700110DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
111 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700112DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
robertphillips5b693772014-11-21 06:19:36 -0800113DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700114DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700115DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800116DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800117DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800118DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
msarettc149f0e2016-01-04 11:35:43 -0800119DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
joshualitt3ebd0502016-02-09 07:18:08 -0800120DEFINE_string(useThermalManager, "0,1,10,1000", "enabled,threshold,sleepTimeMs,TimeoutMs for "
121 "thermalManager\n");
mtklein92007582014-08-01 07:46:52 -0700122
mtklein65dfd2f2016-02-03 10:40:54 -0800123DEFINE_string(sourceType, "",
124 "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
125DEFINE_string(benchType, "",
126 "Apply usual --match rules to bench type: micro, recording, playback, skcodec, etc.");
127
mtkleinbbba1682015-10-28 11:36:30 -0700128static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
129
mtkleinf3723212014-06-25 14:08:00 -0700130static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700131 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800132 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700133}
mtklein55b0ffc2014-07-17 08:38:23 -0700134#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700135
tomhudsond968a6f2015-03-26 11:28:06 -0700136bool Target::init(SkImageInfo info, Benchmark* bench) {
137 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700138 this->surface = SkSurface::MakeRaster(info);
139 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700140 return false;
141 }
142 }
143 return true;
144}
145bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700146 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700147 if (!canvas) {
148 return false;
149 }
150 bmp->setInfo(canvas->imageInfo());
151 if (!canvas->readPixels(bmp, 0, 0)) {
152 SkDebugf("Can't read canvas pixels.\n");
153 return false;
154 }
155 return true;
156}
157
158#if SK_SUPPORT_GPU
159struct GPUTarget : public Target {
halcanary96fcdcc2015-08-27 07:41:13 -0700160 explicit GPUTarget(const Config& c) : Target(c), gl(nullptr) { }
bsalomon3724e572016-03-30 18:56:19 -0700161 GLContext* gl;
tomhudsond968a6f2015-03-26 11:28:06 -0700162
163 void setup() override {
164 this->gl->makeCurrent();
165 // Make sure we're done with whatever came before.
bsalomon3724e572016-03-30 18:56:19 -0700166 GR_GL_CALL(this->gl->gl(), Finish());
tomhudsond968a6f2015-03-26 11:28:06 -0700167 }
168 void endTiming() override {
169 if (this->gl) {
bsalomon3724e572016-03-30 18:56:19 -0700170 GR_GL_CALL(this->gl->gl(), Flush());
joshualitt01836ad2016-01-20 13:09:12 -0800171 this->gl->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700172 }
173 }
174 void fence() override {
bsalomon3724e572016-03-30 18:56:19 -0700175 GR_GL_CALL(this->gl->gl(), Finish());
tomhudsond968a6f2015-03-26 11:28:06 -0700176 }
mtkleind75c4662015-04-30 07:11:22 -0700177
cdaltond416a5b2015-06-23 13:23:44 -0700178 bool needsFrameTiming(int* maxFrameLag) const override {
179 if (!this->gl->getMaxGpuFrameLag(maxFrameLag)) {
180 // Frame lag is unknown.
181 *maxFrameLag = FLAGS_gpuFrameLag;
182 }
183 return true;
184 }
tomhudsond968a6f2015-03-26 11:28:06 -0700185 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700186 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
187 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700188 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700189 this->surface = SkSurface::MakeRenderTarget(gGrFactory->get(this->config.ctxType,
190 this->config.ctxOptions),
bsalomon5ec26ae2016-02-25 08:33:02 -0800191 SkBudgeted::kNo, info,
reede8f30622016-03-23 18:59:25 -0700192 this->config.samples, &props);
kkinnunen3e980c32015-12-23 01:33:00 -0800193 this->gl = gGrFactory->getContextInfo(this->config.ctxType,
kkinnunen34058002016-01-06 23:49:30 -0800194 this->config.ctxOptions).fGLContext;
tomhudsond968a6f2015-03-26 11:28:06 -0700195 if (!this->surface.get()) {
196 return false;
197 }
cdaltond416a5b2015-06-23 13:23:44 -0700198 if (!this->gl->fenceSyncSupport()) {
199 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800200 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700201 }
tomhudsond968a6f2015-03-26 11:28:06 -0700202 return true;
203 }
204 void fillOptions(ResultsWriter* log) override {
205 const GrGLubyte* version;
bsalomon3724e572016-03-30 18:56:19 -0700206 GR_GL_CALL_RET(this->gl->gl(), version, GetString(GR_GL_VERSION));
tomhudsond968a6f2015-03-26 11:28:06 -0700207 log->configOption("GL_VERSION", (const char*)(version));
208
bsalomon3724e572016-03-30 18:56:19 -0700209 GR_GL_CALL_RET(this->gl->gl(), version, GetString(GR_GL_RENDERER));
tomhudsond968a6f2015-03-26 11:28:06 -0700210 log->configOption("GL_RENDERER", (const char*) version);
211
bsalomon3724e572016-03-30 18:56:19 -0700212 GR_GL_CALL_RET(this->gl->gl(), version, GetString(GR_GL_VENDOR));
tomhudsond968a6f2015-03-26 11:28:06 -0700213 log->configOption("GL_VENDOR", (const char*) version);
214
bsalomon3724e572016-03-30 18:56:19 -0700215 GR_GL_CALL_RET(this->gl->gl(), version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
tomhudsond968a6f2015-03-26 11:28:06 -0700216 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
217 }
218};
mtkleind75c4662015-04-30 07:11:22 -0700219
tomhudsond968a6f2015-03-26 11:28:06 -0700220#endif
221
tomhudson75a0ebb2015-03-27 12:11:44 -0700222static double time(int loops, Benchmark* bench, Target* target) {
223 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700224 if (canvas) {
225 canvas->clear(SK_ColorWHITE);
226 }
joshualitt8a6697a2015-09-30 12:11:07 -0700227 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700228 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700229 canvas = target->beginTiming(canvas);
230 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700231 if (canvas) {
232 canvas->flush();
233 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700234 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700235 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700236 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700237 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700238}
239
mtkleinf3723212014-06-25 14:08:00 -0700240static double estimate_timer_overhead() {
241 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700242 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700243 double start = now_ms();
244 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700245 }
246 return overhead / FLAGS_overheadLoops;
247}
248
reed53249782014-10-10 09:09:52 -0700249static int detect_forever_loops(int loops) {
250 // look for a magic run-forever value
251 if (loops < 0) {
252 loops = SK_MaxS32;
253 }
254 return loops;
255}
256
mtklein55b0ffc2014-07-17 08:38:23 -0700257static int clamp_loops(int loops) {
258 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800259 SkDebugf("ERROR: clamping loops from %d to 1. "
260 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700261 return 1;
262 }
263 if (loops > FLAGS_maxLoops) {
264 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
265 return FLAGS_maxLoops;
266 }
267 return loops;
268}
269
tomhudsond968a6f2015-03-26 11:28:06 -0700270static bool write_canvas_png(Target* target, const SkString& filename) {
271
bsalomon6eb03cc2014-08-07 14:28:50 -0700272 if (filename.isEmpty()) {
273 return false;
274 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700275 if (target->getCanvas() &&
276 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700277 return false;
278 }
tomhudsond968a6f2015-03-26 11:28:06 -0700279
bsalomon6eb03cc2014-08-07 14:28:50 -0700280 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700281
282 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700283 return false;
284 }
tomhudsond968a6f2015-03-26 11:28:06 -0700285
bsalomon6eb03cc2014-08-07 14:28:50 -0700286 SkString dir = SkOSPath::Dirname(filename.c_str());
287 if (!sk_mkdir(dir.c_str())) {
288 SkDebugf("Can't make dir %s.\n", dir.c_str());
289 return false;
290 }
291 SkFILEWStream stream(filename.c_str());
292 if (!stream.isValid()) {
293 SkDebugf("Can't write %s.\n", filename.c_str());
294 return false;
295 }
296 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
297 SkDebugf("Can't encode a PNG.\n");
298 return false;
299 }
300 return true;
301}
302
303static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700304static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700305 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700306 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700307 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700308 int loops = bench->calculateLoops(FLAGS_loops);
309 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700310 while (bench_plus_overhead < overhead) {
311 if (round++ == FLAGS_maxCalibrationAttempts) {
312 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700313 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700314 return kFailedLoops;
315 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700316 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700317 }
mtklein2069e222014-08-04 13:57:39 -0700318 }
mtkleinf3723212014-06-25 14:08:00 -0700319
mtkleinbb6a0282014-07-01 08:43:42 -0700320 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700321 // We'll pick N to make timer overhead negligible:
322 //
mtkleinbb6a0282014-07-01 08:43:42 -0700323 // overhead
324 // ------------------------- < FLAGS_overheadGoal
325 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700326 //
mtkleinbb6a0282014-07-01 08:43:42 -0700327 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700328 //
329 // Doing some math, we get:
330 //
mtkleinbb6a0282014-07-01 08:43:42 -0700331 // (overhead / FLAGS_overheadGoal) - overhead
332 // ------------------------------------------ < N
333 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700334 //
335 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700336 if (kAutoTuneLoops == loops) {
337 const double numer = overhead / FLAGS_overheadGoal - overhead;
338 const double denom = bench_plus_overhead - overhead;
339 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700340 loops = clamp_loops(loops);
341 } else {
342 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700343 }
mtkleinbb6a0282014-07-01 08:43:42 -0700344
mtkleinbb6a0282014-07-01 08:43:42 -0700345 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700346}
347
cdaltone1b89582015-06-25 19:17:08 -0700348static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700349 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700350 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700351 if (kAutoTuneLoops == loops) {
352 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700353 double elapsed = 0;
354 do {
mtklein527930f2014-11-06 08:04:34 -0800355 if (1<<30 == loops) {
356 // We're about to wrap. Something's wrong with the bench.
357 loops = 0;
358 break;
359 }
mtkleina189ccd2014-07-14 12:28:47 -0700360 loops *= 2;
361 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700362 // _this_ round, not still timing last round.
363 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700364 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700365 }
366 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700367
mtkleina189ccd2014-07-14 12:28:47 -0700368 // We've overshot at least a little. Scale back linearly.
369 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700370 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700371
tomhudsond968a6f2015-03-26 11:28:06 -0700372 // Make sure we're not still timing our calibration.
373 target->fence();
reed53249782014-10-10 09:09:52 -0700374 } else {
375 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700376 }
mtkleinbb6a0282014-07-01 08:43:42 -0700377
378 // Pretty much the same deal as the calibration: do some warmup to make
379 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700380 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700381 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700382 }
mtkleinbb6a0282014-07-01 08:43:42 -0700383
mtkleinbb6a0282014-07-01 08:43:42 -0700384 return loops;
385}
mtkleinbb6a0282014-07-01 08:43:42 -0700386
bsalomonc2553372014-07-22 13:09:05 -0700387#if SK_SUPPORT_GPU
388#define kBogusGLContextType GrContextFactory::kNative_GLContextType
kkinnunen5219fd92015-12-10 06:28:13 -0800389#define kBogusGLContextOptions GrContextFactory::kNone_GLContextOptions
bsalomonc2553372014-07-22 13:09:05 -0700390#else
391#define kBogusGLContextType 0
kkinnunen5219fd92015-12-10 06:28:13 -0800392#define kBogusGLContextOptions 0
mtkleine714e752014-07-31 12:13:48 -0700393#endif
bsalomonc2553372014-07-22 13:09:05 -0700394
svaisanenc47635e2016-01-28 06:05:43 -0800395static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
396
397#if SK_SUPPORT_GPU
398 if (const auto* gpuConfig = config->asConfigGpu()) {
399 if (!FLAGS_gpu)
400 return;
401
mtkleinbb6c41b2016-03-08 11:31:11 -0800402 const auto ctxOptions = gpuConfig->getUseNVPR() ? GrContextFactory::kEnableNVPR_GLContextOptions
svaisanenc47635e2016-01-28 06:05:43 -0800403 : GrContextFactory::kNone_GLContextOptions;
404 const auto ctxType = gpuConfig->getContextType();
405 const auto sampleCount = gpuConfig->getSamples();
406
407 if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOptions)) {
408 const auto maxSampleCount = ctx->caps()->maxSampleCount();
409 if (sampleCount > ctx->caps()->maxSampleCount()) {
410 SkDebugf("Configuration sample count %d exceeds maximum %d.\n",
411 sampleCount, maxSampleCount);
412 return;
413 }
414 } else {
415 SkDebugf("No context was available matching config type and options.\n");
416 return;
417 }
418
419 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800420 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800421 Benchmark::kGPU_Backend,
422 kN32_SkColorType,
423 kPremul_SkAlphaType,
mtkleinbb6c41b2016-03-08 11:31:11 -0800424 kLinear_SkColorProfileType,
svaisanenc47635e2016-01-28 06:05:43 -0800425 sampleCount,
426 ctxType,
kkinnunend4e1c352016-03-01 23:41:26 -0800427 ctxOptions,
428 gpuConfig->getUseDIText()
429 };
svaisanenc47635e2016-01-28 06:05:43 -0800430
431 configs->push_back(target);
432 return;
433 }
434#endif
435
mtkleinbb6c41b2016-03-08 11:31:11 -0800436 #define CPU_CONFIG(name, backend, color, alpha, profile) \
437 if (config->getTag().equals(#name)) { \
438 Config config = { \
439 SkString(#name), Benchmark::backend, color, alpha, profile, \
440 0, kBogusGLContextType, kBogusGLContextOptions, false \
441 }; \
442 configs->push_back(config); \
443 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700444 }
mtkleine714e752014-07-31 12:13:48 -0700445
mtklein40b32be2014-07-09 08:46:49 -0700446 if (FLAGS_cpu) {
mtkleinbb6c41b2016-03-08 11:31:11 -0800447 CPU_CONFIG(nonrendering, kNonRendering_Backend,
448 kUnknown_SkColorType, kUnpremul_SkAlphaType, kLinear_SkColorProfileType);
449
450 CPU_CONFIG(8888, kRaster_Backend,
451 kN32_SkColorType, kPremul_SkAlphaType, kLinear_SkColorProfileType)
452 CPU_CONFIG(565, kRaster_Backend,
453 kRGB_565_SkColorType, kOpaque_SkAlphaType, kLinear_SkColorProfileType)
454 CPU_CONFIG(srgb, kRaster_Backend,
455 kN32_SkColorType, kPremul_SkAlphaType, kSRGB_SkColorProfileType)
456 CPU_CONFIG(f16, kRaster_Backend,
457 kRGBA_F16_SkColorType, kPremul_SkAlphaType, kLinear_SkColorProfileType)
mtklein40b32be2014-07-09 08:46:49 -0700458 }
mtkleinbb6a0282014-07-01 08:43:42 -0700459
svaisanenc47635e2016-01-28 06:05:43 -0800460 #undef CPU_CONFIG
tomhudsond968a6f2015-03-26 11:28:06 -0700461
462#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
djsollen817c72a2016-01-28 13:57:02 -0800463 if (config->getTag().equals("hwui")) {
mtkleinbb6c41b2016-03-08 11:31:11 -0800464 Config config = { SkString("hwui"), Benchmark::kHWUI_Backend,
465 kRGBA_8888_SkColorType, kPremul_SkAlphaType, kLinear_SkColorProfileType,
466 0, kBogusGLContextType, kBogusGLContextOptions, false };
svaisanenc47635e2016-01-28 06:05:43 -0800467 configs->push_back(config);
tomhudsond968a6f2015-03-26 11:28:06 -0700468 }
469#endif
mtkleinf3723212014-06-25 14:08:00 -0700470}
471
svaisanenc47635e2016-01-28 06:05:43 -0800472// Append all configs that are enabled and supported.
473void create_configs(SkTArray<Config>* configs) {
474 SkCommandLineConfigArray array;
475 ParseConfigs(FLAGS_config, &array);
476 for (int i = 0; i < array.count(); ++i) {
477 create_config(array[i], configs);
478 }
479}
480
halcanary96fcdcc2015-08-27 07:41:13 -0700481// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700482static Target* is_enabled(Benchmark* bench, const Config& config) {
483 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700484 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700485 }
486
reede5ea5002014-09-03 11:54:58 -0700487 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
mtkleinbb6c41b2016-03-08 11:31:11 -0800488 config.color, config.alpha, config.profile);
bsalomonc2553372014-07-22 13:09:05 -0700489
halcanary96fcdcc2015-08-27 07:41:13 -0700490 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700491
tomhudsond968a6f2015-03-26 11:28:06 -0700492 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700493#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700494 case Benchmark::kGPU_Backend:
495 target = new GPUTarget(config);
496 break;
bsalomonc2553372014-07-22 13:09:05 -0700497#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700498#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
499 case Benchmark::kHWUI_Backend:
500 target = new HWUITarget(config, bench);
501 break;
502#endif
503 default:
504 target = new Target(config);
505 break;
506 }
bsalomonc2553372014-07-22 13:09:05 -0700507
tomhudsond968a6f2015-03-26 11:28:06 -0700508 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700509 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700510 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700511 }
512 return target;
513}
514
msarett5cb48852015-11-06 08:56:32 -0800515static bool valid_brd_bench(SkData* encoded, SkBitmapRegionDecoder::Strategy strategy,
msarett7f691442015-09-22 11:56:16 -0700516 SkColorType colorType, uint32_t sampleSize, uint32_t minOutputSize, int* width,
517 int* height) {
msarett5cb48852015-11-06 08:56:32 -0800518 SkAutoTDelete<SkBitmapRegionDecoder> brd(
519 SkBitmapRegionDecoder::Create(encoded, strategy));
msarett7f691442015-09-22 11:56:16 -0700520 if (nullptr == brd.get()) {
521 // This is indicates that subset decoding is not supported for a particular image format.
522 return false;
523 }
524
msarett35e5d1b2015-10-27 12:50:25 -0700525 SkBitmap bitmap;
526 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, brd->width(), brd->height()),
527 1, colorType, false)) {
528 return false;
529 }
msarett7f691442015-09-22 11:56:16 -0700530
531 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
532 (uint32_t) brd->height()) {
533 // This indicates that the image is not large enough to decode a
534 // minOutputSize x minOutputSize subset at the given sampleSize.
535 return false;
536 }
537
538 // Set the image width and height. The calling code will use this to choose subsets to decode.
539 *width = brd->width();
540 *height = brd->height();
541 return true;
542}
543
egdaniel3bf92062015-06-26 08:12:46 -0700544static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700545 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700546#if SK_SUPPORT_GPU
547 if (FLAGS_abandonGpuContext) {
548 gGrFactory->abandonContexts();
549 }
550 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
551 gGrFactory->destroyContexts();
552 }
553#endif
554}
555
mtkleine714e752014-07-31 12:13:48 -0700556class BenchmarkStream {
557public:
mtklein92007582014-08-01 07:46:52 -0700558 BenchmarkStream() : fBenches(BenchRegistry::Head())
559 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700560 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700561 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800562 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800563 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700564 , fCurrentCodec(0)
msarett84451022016-02-11 06:45:51 -0800565 , fCurrentAndroidCodec(0)
msarett7f691442015-09-22 11:56:16 -0700566 , fCurrentBRDImage(0)
msarett95f192d2015-02-13 09:05:41 -0800567 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800568 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700569 , fCurrentSubsetType(0)
msarett7f691442015-09-22 11:56:16 -0700570 , fCurrentBRDStrategy(0)
msarett84451022016-02-11 06:45:51 -0800571 , fCurrentSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700572 , fCurrentAnimSKP(0) {
mtklein92007582014-08-01 07:46:52 -0700573 for (int i = 0; i < FLAGS_skps.count(); i++) {
574 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
575 fSKPs.push_back() = FLAGS_skps[i];
576 } else {
577 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
578 SkString path;
579 while (it.next(&path)) {
580 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
581 }
582 }
583 }
mtkleine714e752014-07-31 12:13:48 -0700584
mtklein92007582014-08-01 07:46:52 -0700585 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
586 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
587 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
588 exit(1);
589 }
590
591 for (int i = 0; i < FLAGS_scales.count(); i++) {
592 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
593 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
594 exit(1);
595 }
596 }
robertphillips5b693772014-11-21 06:19:36 -0800597
cdalton63a82852015-06-29 14:06:10 -0700598 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
599 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700600 exit(1);
601 }
602
robertphillips5b693772014-11-21 06:19:36 -0800603 if (FLAGS_mpd) {
604 fUseMPDs.push_back() = true;
605 }
mtkleinc751ecb2015-06-15 08:56:38 -0700606 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700607
msarett95f192d2015-02-13 09:05:41 -0800608 // Prepare the images for decoding
scroggo86737142016-02-03 12:19:11 -0800609 if (!CollectImages(&fImages)) {
610 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800611 }
mtklein95553d92015-03-12 08:24:21 -0700612
msarett95f192d2015-02-13 09:05:41 -0800613 // Choose the candidate color types for image decoding
614 const SkColorType colorTypes[] =
msarettb23e6aa2015-06-09 13:56:10 -0700615 { kN32_SkColorType,
616 kRGB_565_SkColorType,
617 kAlpha_8_SkColorType,
618 kIndex_8_SkColorType,
619 kGray_8_SkColorType };
msarett74deb982015-10-20 16:45:56 -0700620 fColorTypes.reset(colorTypes, SK_ARRAY_COUNT(colorTypes));
mtklein92007582014-08-01 07:46:52 -0700621 }
622
reedca2622b2016-03-18 07:25:55 -0700623 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700624 // Not strictly necessary, as it will be checked again later,
625 // but helps to avoid a lot of pointless work if we're going to skip it.
cdalton91e457d2016-02-17 11:10:16 -0800626 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700627 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700628 }
629
scroggoa1193e42015-01-21 12:09:53 -0800630 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -0700631 if (stream.get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700632 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700633 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700634 }
635
reedca2622b2016-03-18 07:25:55 -0700636 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700637 }
638
mtklein92007582014-08-01 07:46:52 -0700639 Benchmark* next() {
mtklein65dfd2f2016-02-03 10:40:54 -0800640 SkAutoTDelete<Benchmark> bench;
641 do {
642 bench.reset(this->rawNext());
643 if (!bench) {
644 return nullptr;
645 }
646 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
647 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700648 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800649 }
650
651 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700652 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700653 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700654 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700655 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700656 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700657 return bench;
658 }
mtklein92007582014-08-01 07:46:52 -0700659
mtkleine714e752014-07-31 12:13:48 -0700660 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -0700661 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700662 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800663 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700664 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700665 fBenchType = "micro";
mtklein18300a32016-03-16 13:53:35 -0700666 return new GMBench(gm.release());
mtkleine714e752014-07-31 12:13:48 -0700667 }
668 }
mtklein92007582014-08-01 07:46:52 -0700669
mtkleinfd731ce2014-09-10 12:19:30 -0700670 // First add all .skps as RecordingBenches.
671 while (fCurrentRecording < fSKPs.count()) {
672 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700673 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
674 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700675 continue;
676 }
677 SkString name = SkOSPath::Basename(path.c_str());
678 fSourceType = "skp";
679 fBenchType = "recording";
reedca2622b2016-03-18 07:25:55 -0700680 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic.get()));
mtklein051e56d2014-12-04 08:46:51 -0800681 fSKPOps = pic->approximateOpCount();
halcanary385fe4d2015-08-26 13:07:48 -0700682 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700683 }
684
685 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700686 while (fCurrentScale < fScales.count()) {
687 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800688 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700689 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
690 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800691 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700692 continue;
693 }
robertphillips5b693772014-11-21 06:19:36 -0800694
695 while (fCurrentUseMPD < fUseMPDs.count()) {
696 if (FLAGS_bbh) {
697 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
698 SkRTreeFactory factory;
699 SkPictureRecorder recorder;
700 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
701 pic->playback(recorder.beginRecording(pic->cullRect().width(),
702 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800703 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800704 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
reedca2622b2016-03-18 07:25:55 -0700705 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800706 }
707 SkString name = SkOSPath::Basename(path.c_str());
708 fSourceType = "skp";
709 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700710 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
711 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700712 }
robertphillips5b693772014-11-21 06:19:36 -0800713 fCurrentUseMPD = 0;
714 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700715 }
716 fCurrentSKP = 0;
717 fCurrentScale++;
718 }
719
joshualitt261c3ad2015-04-27 09:16:57 -0700720 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700721 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700722 while (fCurrentAnimSKP < fSKPs.count()) {
723 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700724 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
725 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700726 fCurrentAnimSKP++;
727 continue;
728 }
729
730 fCurrentAnimSKP++;
731 SkString name = SkOSPath::Basename(path.c_str());
cdalton63a82852015-06-29 14:06:10 -0700732 SkAutoTUnref<SKPAnimationBench::Animation> animation(
733 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
halcanary385fe4d2015-08-26 13:07:48 -0700734 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation,
735 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700736 }
737 }
738
scroggo60869a42015-04-01 12:09:17 -0700739 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700740 fSourceType = "image";
741 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700742 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800743 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
744 continue;
745 }
scroggo60869a42015-04-01 12:09:17 -0700746 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
747 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700748 if (!codec) {
749 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700750 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700751 continue;
752 }
scroggo21027992015-04-02 13:22:38 -0700753
scroggo60869a42015-04-01 12:09:17 -0700754 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700755 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700756
msarettc7796b92016-01-07 14:20:20 -0800757 SkAlphaType alphaType = codec->getInfo().alphaType();
758 switch (alphaType) {
759 case kOpaque_SkAlphaType:
760 // We only need to test one alpha type (opaque).
761 fCurrentColorType++;
762 break;
763 case kUnpremul_SkAlphaType:
764 case kPremul_SkAlphaType:
765 if (0 == fCurrentAlphaType) {
766 // Test unpremul first.
767 alphaType = kUnpremul_SkAlphaType;
768 fCurrentAlphaType++;
769 } else {
770 // Test premul.
771 alphaType = kPremul_SkAlphaType;
772 fCurrentAlphaType = 0;
773 fCurrentColorType++;
774 }
775 break;
776 default:
777 SkASSERT(false);
778 fCurrentColorType++;
779 break;
scroggo21027992015-04-02 13:22:38 -0700780 }
781
msarettc7796b92016-01-07 14:20:20 -0800782 // Make sure we can decode to this color type and alpha type.
783 SkImageInfo info =
784 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700785 const size_t rowBytes = info.minRowBytes();
786 SkAutoMalloc storage(info.getSafeSize(rowBytes));
787
788 // Used if fCurrentColorType is kIndex_8_SkColorType
789 int colorCount = 256;
790 SkPMColor colors[256];
791
scroggoeb602a52015-07-09 08:16:03 -0700792 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700793 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700794 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700795 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700796 case SkCodec::kSuccess:
797 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700798 return new CodecBench(SkOSPath::Basename(path.c_str()),
msarettc7796b92016-01-07 14:20:20 -0800799 encoded, colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700800 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700801 // This is okay. Not all conversions are valid.
802 break;
scroggo60869a42015-04-01 12:09:17 -0700803 default:
804 // This represents some sort of failure.
805 SkASSERT(false);
806 break;
807 }
808 }
809 fCurrentColorType = 0;
810 }
811
msarett84451022016-02-11 06:45:51 -0800812 // Run AndroidCodecBenches
813 const int sampleSizes[] = { 2, 4, 8 };
814 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
815 fSourceType = "image";
816 fBenchType = "skandroidcodec";
817
818 const SkString& path = fImages[fCurrentAndroidCodec];
819 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
820 continue;
821 }
822 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
823 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
824 if (!codec) {
825 // Nothing to time.
826 SkDebugf("Cannot find codec for %s\n", path.c_str());
827 continue;
828 }
829
830 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
831 int sampleSize = sampleSizes[fCurrentSampleSize];
832 fCurrentSampleSize++;
833 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
834 // Avoid benchmarking scaled decodes of already small images.
835 break;
836 }
837
838 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()), encoded, sampleSize);
839 }
840 fCurrentSampleSize = 0;
841 }
842
msarett7f691442015-09-22 11:56:16 -0700843 // Run the BRDBenches
844 // We will benchmark multiple BRD strategies.
scroggo303fa352015-10-05 11:03:34 -0700845 static const struct {
msarett5cb48852015-11-06 08:56:32 -0800846 SkBitmapRegionDecoder::Strategy fStrategy;
msarett0459c942015-11-10 14:52:13 -0800847 const char* fName;
scroggo303fa352015-10-05 11:03:34 -0700848 } strategies[] = {
msarett0459c942015-11-10 14:52:13 -0800849 { SkBitmapRegionDecoder::kCanvas_Strategy, "BRD_canvas" },
msarett5cb48852015-11-06 08:56:32 -0800850 { SkBitmapRegionDecoder::kAndroidCodec_Strategy, "BRD_android_codec" },
msarett7f691442015-09-22 11:56:16 -0700851 };
852
853 // We intend to create benchmarks that model the use cases in
854 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
855 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
856 // the image. For that reason, we will benchmark decodes in five representative locations
857 // in the image. Additionally, this use case utilizes power of two scaling, so we will
858 // test on power of two sample sizes. The output tile is always 512x512, so, when a
859 // sampleSize is used, the size of the subset that is decoded is always
860 // (sampleSize*512)x(sampleSize*512).
861 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700862 // All use cases we are aware of only scale by powers of two.
863 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
864 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800865 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700866 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800867 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
868 const SkString& path = fImages[fCurrentBRDImage];
869 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
870 continue;
871 }
msarett7f691442015-09-22 11:56:16 -0700872 while (fCurrentBRDStrategy < (int) SK_ARRAY_COUNT(strategies)) {
scroggo303fa352015-10-05 11:03:34 -0700873 fSourceType = "image";
874 fBenchType = strategies[fCurrentBRDStrategy].fName;
scroggo860e8a62015-10-15 07:51:28 -0700875
msarett5cb48852015-11-06 08:56:32 -0800876 const SkBitmapRegionDecoder::Strategy strategy =
scroggo860e8a62015-10-15 07:51:28 -0700877 strategies[fCurrentBRDStrategy].fStrategy;
878
msarett7f691442015-09-22 11:56:16 -0700879 while (fCurrentColorType < fColorTypes.count()) {
msarett84451022016-02-11 06:45:51 -0800880 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
msarett7f691442015-09-22 11:56:16 -0700881 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700882
msarett7f691442015-09-22 11:56:16 -0700883 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
884 const SkColorType colorType = fColorTypes[fCurrentColorType];
msarett84451022016-02-11 06:45:51 -0800885 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
msarett7f691442015-09-22 11:56:16 -0700886 int currentSubsetType = fCurrentSubsetType++;
887
888 int width = 0;
889 int height = 0;
890 if (!valid_brd_bench(encoded.get(), strategy, colorType, sampleSize,
891 minOutputSize, &width, &height)) {
892 break;
893 }
894
895 SkString basename = SkOSPath::Basename(path.c_str());
896 SkIRect subset;
897 const uint32_t subsetSize = sampleSize * minOutputSize;
898 switch (currentSubsetType) {
899 case kTopLeft_SubsetType:
900 basename.append("_TopLeft");
901 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
902 break;
903 case kTopRight_SubsetType:
904 basename.append("_TopRight");
905 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
906 subsetSize);
907 break;
908 case kMiddle_SubsetType:
909 basename.append("_Middle");
910 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
911 (height - subsetSize) / 2, subsetSize, subsetSize);
912 break;
913 case kBottomLeft_SubsetType:
914 basename.append("_BottomLeft");
915 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
916 subsetSize);
917 break;
918 case kBottomRight_SubsetType:
919 basename.append("_BottomRight");
920 subset = SkIRect::MakeXYWH(width - subsetSize,
921 height - subsetSize, subsetSize, subsetSize);
922 break;
923 default:
924 SkASSERT(false);
925 }
926
927 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
928 strategy, colorType, sampleSize, subset);
929 }
930 fCurrentSubsetType = 0;
msarett84451022016-02-11 06:45:51 -0800931 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -0700932 }
msarett84451022016-02-11 06:45:51 -0800933 fCurrentSampleSize = 0;
msarett7f691442015-09-22 11:56:16 -0700934 fCurrentColorType++;
935 }
936 fCurrentColorType = 0;
937 fCurrentBRDStrategy++;
938 }
939 fCurrentBRDStrategy = 0;
msarett7f691442015-09-22 11:56:16 -0700940 }
941
halcanary96fcdcc2015-08-27 07:41:13 -0700942 return nullptr;
mtkleine714e752014-07-31 12:13:48 -0700943 }
mtklein92007582014-08-01 07:46:52 -0700944
945 void fillCurrentOptions(ResultsWriter* log) const {
946 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700947 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700948 if (0 == strcmp(fSourceType, "skp")) {
949 log->configOption("clip",
950 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
951 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -0800952 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -0700953 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -0800954 if (fCurrentUseMPD > 0) {
955 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
956 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
957 }
mtklein92007582014-08-01 07:46:52 -0700958 }
mtklein051e56d2014-12-04 08:46:51 -0800959 if (0 == strcmp(fBenchType, "recording")) {
960 log->metric("bytes", fSKPBytes);
961 log->metric("ops", fSKPOps);
962 }
mtklein92007582014-08-01 07:46:52 -0700963 }
964
mtkleine714e752014-07-31 12:13:48 -0700965private:
msarettb23e6aa2015-06-09 13:56:10 -0700966 enum SubsetType {
967 kTopLeft_SubsetType = 0,
968 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -0700969 kMiddle_SubsetType = 2,
970 kBottomLeft_SubsetType = 3,
971 kBottomRight_SubsetType = 4,
972 kTranslate_SubsetType = 5,
973 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -0700974 kLast_SubsetType = kZoom_SubsetType,
975 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -0700976 };
977
mtkleine714e752014-07-31 12:13:48 -0700978 const BenchRegistry* fBenches;
979 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700980 SkIRect fClip;
981 SkTArray<SkScalar> fScales;
982 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -0800983 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -0800984 SkTArray<SkString> fImages;
msarett74deb982015-10-20 16:45:56 -0700985 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -0700986 SkScalar fZoomMax;
987 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -0700988
mtklein051e56d2014-12-04 08:46:51 -0800989 double fSKPBytes, fSKPOps;
990
mtkleinfd731ce2014-09-10 12:19:30 -0700991 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
992 const char* fBenchType; // How we bench it: micro, recording, playback, ...
993 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700994 int fCurrentScale;
995 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -0800996 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -0700997 int fCurrentCodec;
msarett84451022016-02-11 06:45:51 -0800998 int fCurrentAndroidCodec;
msarett7f691442015-09-22 11:56:16 -0700999 int fCurrentBRDImage;
msarett95f192d2015-02-13 09:05:41 -08001000 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -08001001 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -07001002 int fCurrentSubsetType;
msarett7f691442015-09-22 11:56:16 -07001003 int fCurrentBRDStrategy;
msarett84451022016-02-11 06:45:51 -08001004 int fCurrentSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001005 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001006};
1007
msarettc149f0e2016-01-04 11:35:43 -08001008// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1009// This prints something every once in a while so that it knows we're still working.
1010static void start_keepalive() {
1011 struct Loop {
1012 static void forever(void*) {
1013 for (;;) {
1014 static const int kSec = 1200;
1015 #if defined(SK_BUILD_FOR_WIN)
1016 Sleep(kSec * 1000);
1017 #else
1018 sleep(kSec);
1019 #endif
1020 SkDebugf("\nBenchmarks still running...\n");
1021 }
1022 }
1023 };
1024 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1025 intentionallyLeaked->start();
1026}
1027
jcgregorio3b27ade2014-11-13 08:06:40 -08001028int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001029int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001030 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001031 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001032 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001033
krajcevski69a55602014-08-13 10:46:31 -07001034#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001035 GrContextOptions grContextOpts;
krajcevski12b35442014-08-13 12:06:26 -07001036 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
halcanary385fe4d2015-08-26 13:07:48 -07001037 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001038#endif
1039
bsalomon06cddec2014-10-24 10:40:50 -07001040 if (FLAGS_veryVerbose) {
1041 FLAGS_verbose = true;
1042 }
1043
bsalomon6eb03cc2014-08-07 14:28:50 -07001044 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001045 FLAGS_samples = 1;
1046 FLAGS_gpuFrameLag = 0;
1047 }
1048
bsalomon6eb03cc2014-08-07 14:28:50 -07001049 if (!FLAGS_writePath.isEmpty()) {
1050 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1051 if (!sk_mkdir(FLAGS_writePath[0])) {
1052 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001053 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001054 }
1055 }
1056
halcanary385fe4d2015-08-26 13:07:48 -07001057 SkAutoTDelete<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001058 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001059#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001060 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001061#else
1062 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1063 return 1;
1064#endif
mtklein60317d0f2014-07-14 11:30:37 -07001065 }
mtklein1915b622014-08-20 11:45:00 -07001066
1067 if (1 == FLAGS_properties.count() % 2) {
1068 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1069 return 1;
1070 }
1071 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1072 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1073 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001074
1075 if (1 == FLAGS_key.count() % 2) {
1076 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1077 return 1;
1078 }
1079 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001080 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001081 }
mtklein60317d0f2014-07-14 11:30:37 -07001082
mtkleinf3723212014-06-25 14:08:00 -07001083 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001084 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001085
cdaltone1b89582015-06-25 19:17:08 -07001086 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001087
bsalomon6eb03cc2014-08-07 14:28:50 -07001088 if (kAutoTuneLoops != FLAGS_loops) {
1089 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001090 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001091 SkDebugf("! -> high variance, ? -> moderate variance\n");
1092 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001093 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001094 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001095 } else {
mtkleind75c4662015-04-30 07:11:22 -07001096 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001097 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001098 }
1099
svaisanenc47635e2016-01-28 06:05:43 -08001100 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001101 create_configs(&configs);
1102
joshualitt3ebd0502016-02-09 07:18:08 -08001103#ifdef THERMAL_MANAGER_SUPPORTED
1104 int tmEnabled, tmThreshold, tmSleepTimeMs, tmTimeoutMs;
1105 if (4 != sscanf(FLAGS_useThermalManager[0], "%d,%d,%d,%d",
1106 &tmEnabled, &tmThreshold, &tmSleepTimeMs, &tmTimeoutMs)) {
1107 SkDebugf("Can't parse %s from --useThermalManager.\n", FLAGS_useThermalManager[0]);
1108 exit(1);
1109 }
1110 ThermalManager tm(tmThreshold, tmSleepTimeMs, tmTimeoutMs);
1111#endif
1112
msarettc149f0e2016-01-04 11:35:43 -08001113 if (FLAGS_keepAlive) {
1114 start_keepalive();
1115 }
1116
mtkleine070c2b2014-10-14 08:40:43 -07001117 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001118 BenchmarkStream benchStream;
1119 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001120 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001121 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001122 continue;
1123 }
1124
svaisanenc47635e2016-01-28 06:05:43 -08001125 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001126 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001127 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001128 }
egdaniel3bf92062015-06-26 08:12:46 -07001129 for (int i = 0; i < configs.count(); ++i) {
joshualitt3ebd0502016-02-09 07:18:08 -08001130#ifdef THERMAL_MANAGER_SUPPORTED
1131 if (tmEnabled && !tm.coolOffIfNecessary()) {
1132 SkDebugf("Could not cool off, timings will be throttled\n");
1133 }
1134#endif
egdaniel3bf92062015-06-26 08:12:46 -07001135 Target* target = is_enabled(b, configs[i]);
1136 if (!target) {
1137 continue;
1138 }
mtkleinf3723212014-06-25 14:08:00 -07001139
halcanary96fcdcc2015-08-27 07:41:13 -07001140 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001141 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001142 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001143
brianosman7a5ada82016-02-08 13:49:12 -08001144 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001145 SkDebugf("Running %s\t%s\n"
1146 , bench->getUniqueName()
1147 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001148 if (FLAGS_dryRun) {
1149 continue;
1150 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001151 }
1152
egdaniel3bf92062015-06-26 08:12:46 -07001153 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001154 bench->perCanvasPreDraw(canvas);
1155
cdaltone1b89582015-06-25 19:17:08 -07001156 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001157 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001158 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1159 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001160
mtkleinbbba1682015-10-28 11:36:30 -07001161 if (FLAGS_ms) {
1162 samples.reset();
1163 auto stop = now_ms() + FLAGS_ms;
1164 do {
1165 samples.push_back(time(loops, bench, target) / loops);
1166 } while (now_ms() < stop);
1167 } else {
cdaltone1b89582015-06-25 19:17:08 -07001168 samples.reset(FLAGS_samples);
1169 for (int s = 0; s < FLAGS_samples; s++) {
egdaniel3bf92062015-06-26 08:12:46 -07001170 samples[s] = time(loops, bench, target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001171 }
cdaltone1b89582015-06-25 19:17:08 -07001172 }
mtkleinf3723212014-06-25 14:08:00 -07001173
joshualitte45c81c2015-12-02 09:05:37 -08001174#if SK_SUPPORT_GPU
1175 SkTArray<SkString> keys;
1176 SkTArray<double> values;
1177 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1178 if (gpuStatsDump) {
1179 // TODO cache stats
1180 bench->getGpuStats(canvas, &keys, &values);
1181 }
1182#endif
1183
robertphillips5b693772014-11-21 06:19:36 -08001184 bench->perCanvasPostDraw(canvas);
1185
egdaniel3bf92062015-06-26 08:12:46 -07001186 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001187 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001188 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001189 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001190 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001191 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001192 }
1193
1194 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001195 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001196 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001197 continue;
1198 }
1199
cdaltone1b89582015-06-25 19:17:08 -07001200 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001201 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001202 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001203 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001204 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001205 log->metric("min_ms", stats.min);
joshualitte45c81c2015-12-02 09:05:37 -08001206#if SK_SUPPORT_GPU
1207 if (gpuStatsDump) {
1208 // dump to json, only SKPBench currently returns valid keys / values
1209 SkASSERT(keys.count() == values.count());
1210 for (int i = 0; i < keys.count(); i++) {
1211 log->metric(keys[i].c_str(), values[i]);
1212 }
1213 }
1214#endif
1215
mtkleine070c2b2014-10-14 08:40:43 -07001216 if (runs++ % FLAGS_flushEvery == 0) {
1217 log->flush();
1218 }
mtklein60317d0f2014-07-14 11:30:37 -07001219
bsalomon6eb03cc2014-08-07 14:28:50 -07001220 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001221 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001222 config = ""; // Only print the config if we run the same bench on more than one.
1223 }
mtkleind75c4662015-04-30 07:11:22 -07001224 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1225 , sk_tools::getCurrResidentSetSizeMB()
1226 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001227 , bench->getUniqueName()
1228 , config);
mtkleinf3723212014-06-25 14:08:00 -07001229 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001230 const char* mark = " ";
1231 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1232 if (stddev_percent > 5) mark = "?";
1233 if (stddev_percent > 10) mark = "!";
1234
1235 SkDebugf("%10.2f %s\t%s\t%s\n",
1236 stats.median*1e3, mark, bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001237 } else {
1238 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001239 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1240 , sk_tools::getCurrResidentSetSizeMB()
1241 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001242 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001243 , HUMANIZE(stats.min)
1244 , HUMANIZE(stats.median)
1245 , HUMANIZE(stats.mean)
1246 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001247 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001248 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001249 , config
mtklein96289052014-09-10 12:05:59 -07001250 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001251 );
1252 }
joshualitte45c81c2015-12-02 09:05:37 -08001253
bsalomonb12ea412015-02-02 21:19:50 -08001254#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001255 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001256 GrContext* context = gGrFactory->get(configs[i].ctxType,
kkinnunen3e980c32015-12-23 01:33:00 -08001257 configs[i].ctxOptions);
kkinnunen5219fd92015-12-10 06:28:13 -08001258 context->printCacheStats();
1259 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001260 }
1261#endif
joshualitte45c81c2015-12-02 09:05:37 -08001262
cdalton2c56ba52015-06-26 13:32:53 -07001263 if (FLAGS_verbose) {
1264 SkDebugf("Samples: ");
1265 for (int i = 0; i < samples.count(); i++) {
1266 SkDebugf("%s ", HUMANIZE(samples[i]));
1267 }
1268 SkDebugf("%s\n", bench->getUniqueName());
1269 }
egdaniel3bf92062015-06-26 08:12:46 -07001270 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001271 }
mtkleinf3723212014-06-25 14:08:00 -07001272 }
1273
mtkleine1091452014-12-04 10:47:02 -08001274 log->bench("memory_usage", 0,0);
1275 log->config("meta");
1276 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1277
joshualitte0b19d42015-03-26 10:41:02 -07001278#if SK_SUPPORT_GPU
1279 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1280 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001281 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001282#endif
1283
mtkleinf3723212014-06-25 14:08:00 -07001284 return 0;
1285}
1286
jcgregorio3b27ade2014-11-13 08:06:40 -08001287#if !defined SK_BUILD_FOR_IOS
1288int main(int argc, char** argv) {
1289 SkCommandLineFlags::Parse(argc, argv);
1290 return nanobench_main();
1291}
1292#endif