blob: ae415fa06b694ee8c278f400b4ad94526d449d23 [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"
msarett2cee9022016-06-03 08:25:21 -070017#include "ColorCodecBench.h"
mtkleinf3723212014-06-25 14:08:00 -070018#include "CrashHandler.h"
mtkleine714e752014-07-31 12:13:48 -070019#include "GMBench.h"
mtkleinafb43792014-08-19 15:55:55 -070020#include "ProcStats.h"
mtklein60317d0f2014-07-14 11:30:37 -070021#include "ResultsWriter.h"
mtkleinfd731ce2014-09-10 12:19:30 -070022#include "RecordingBench.h"
joshualitt261c3ad2015-04-27 09:16:57 -070023#include "SKPAnimationBench.h"
mtklein92007582014-08-01 07:46:52 -070024#include "SKPBench.h"
mtkleinf3723212014-06-25 14:08:00 -070025#include "Stats.h"
mtkleinf3723212014-06-25 14:08:00 -070026
msarett84451022016-02-11 06:45:51 -080027#include "SkAndroidCodec.h"
msarett5cb48852015-11-06 08:56:32 -080028#include "SkBitmapRegionDecoder.h"
mtklein6838d852014-10-29 14:15:10 -070029#include "SkBBoxHierarchy.h"
mtkleinf3723212014-06-25 14:08:00 -070030#include "SkCanvas.h"
scroggo60869a42015-04-01 12:09:17 -070031#include "SkCodec.h"
caryclark17f0b6d2014-07-22 10:15:34 -070032#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080033#include "SkCommonFlagsConfig.h"
msarett95f192d2015-02-13 09:05:41 -080034#include "SkData.h"
mtkleinf3723212014-06-25 14:08:00 -070035#include "SkForceLinking.h"
36#include "SkGraphics.h"
halcanary4dbbd042016-06-07 17:21:10 -070037#include "SkLeanWindows.h"
mtklein20840502014-08-21 15:51:22 -070038#include "SkOSFile.h"
39#include "SkPictureRecorder.h"
mtklein051e56d2014-12-04 08:46:51 -080040#include "SkPictureUtils.h"
mtkleinf3723212014-06-25 14:08:00 -070041#include "SkString.h"
42#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080043#include "SkTaskGroup.h"
msarettc149f0e2016-01-04 11:35:43 -080044#include "SkThreadUtils.h"
joshualitt3ebd0502016-02-09 07:18:08 -080045#include "ThermalManager.h"
mtkleinf3723212014-06-25 14:08:00 -070046
bungeman60e0fee2015-08-26 05:15:46 -070047#include <stdlib.h>
48
scroggo38ce0a72016-01-07 07:28:47 -080049#ifndef SK_BUILD_FOR_WIN32
50 #include <unistd.h>
51#endif
52
tomhudsond968a6f2015-03-26 11:28:06 -070053#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
54 #include "nanobenchAndroid.h"
55#endif
56
mtkleinbb6a0282014-07-01 08:43:42 -070057#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070058 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070059 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070060 #include "GrContextFactory.h"
bsalomon3724e572016-03-30 18:56:19 -070061 #include "gl/GrGLUtil.h"
62 using sk_gpu_test::GrContextFactory;
bsalomonc8699322016-05-11 11:55:36 -070063 using sk_gpu_test::TestContext;
krajcevski69a55602014-08-13 10:46:31 -070064 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070065#endif
66
bsalomon682c2692015-05-22 14:01:46 -070067 struct GrContextOptions;
68
mtkleinf3723212014-06-25 14:08:00 -070069__SK_FORCE_IMAGE_DECODER_LINKING;
70
reed53249782014-10-10 09:09:52 -070071static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070072
mtkleinb5110422014-08-07 15:20:02 -070073static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070074#ifdef SK_DEBUG
75 1;
mtkleina189ccd2014-07-14 12:28:47 -070076#else
bsalomon6eb03cc2014-08-07 14:28:50 -070077 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070078#endif
79
bsalomon6eb03cc2014-08-07 14:28:50 -070080static SkString loops_help_txt() {
81 SkString help;
82 help.printf("Number of times to run each bench. Set this to %d to auto-"
83 "tune for each bench. Timings are only reported when auto-tuning.",
84 kAutoTuneLoops);
85 return help;
86}
87
cdaltone1b89582015-06-25 19:17:08 -070088static SkString to_string(int n) {
89 SkString str;
90 str.appendS32(n);
91 return str;
92}
93
bsalomon6eb03cc2014-08-07 14:28:50 -070094DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
95
mtkleinf3723212014-06-25 14:08:00 -070096DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -070097DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -070098DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
99DEFINE_double(overheadGoal, 0.0001,
100 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -0700101DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -0700102DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
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?");
mtklein8c1a4f82016-08-08 06:56:22 -0700113DEFINE_bool(lite, false, "Use SkLiteRecorder in recording benchmarks?");
robertphillips5b693772014-11-21 06:19:36 -0800114DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700115DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700116DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800117DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800118DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800119DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
msarettc149f0e2016-01-04 11:35:43 -0800120DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
joshualitt3ebd0502016-02-09 07:18:08 -0800121DEFINE_string(useThermalManager, "0,1,10,1000", "enabled,threshold,sleepTimeMs,TimeoutMs for "
122 "thermalManager\n");
mtklein92007582014-08-01 07:46:52 -0700123
mtklein65dfd2f2016-02-03 10:40:54 -0800124DEFINE_string(sourceType, "",
125 "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
126DEFINE_string(benchType, "",
127 "Apply usual --match rules to bench type: micro, recording, playback, skcodec, etc.");
128
mtkleinbbba1682015-10-28 11:36:30 -0700129static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
130
mtkleinf3723212014-06-25 14:08:00 -0700131static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700132 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800133 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700134}
mtklein55b0ffc2014-07-17 08:38:23 -0700135#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700136
tomhudsond968a6f2015-03-26 11:28:06 -0700137bool Target::init(SkImageInfo info, Benchmark* bench) {
138 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700139 this->surface = SkSurface::MakeRaster(info);
140 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700141 return false;
142 }
143 }
144 return true;
145}
146bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700147 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700148 if (!canvas) {
149 return false;
150 }
151 bmp->setInfo(canvas->imageInfo());
152 if (!canvas->readPixels(bmp, 0, 0)) {
153 SkDebugf("Can't read canvas pixels.\n");
154 return false;
155 }
156 return true;
157}
158
159#if SK_SUPPORT_GPU
160struct GPUTarget : public Target {
bsalomonc8699322016-05-11 11:55:36 -0700161 explicit GPUTarget(const Config& c) : Target(c), context(nullptr) { }
162 TestContext* context;
tomhudsond968a6f2015-03-26 11:28:06 -0700163
164 void setup() override {
bsalomonc8699322016-05-11 11:55:36 -0700165 this->context->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700166 // Make sure we're done with whatever came before.
bsalomonc8699322016-05-11 11:55:36 -0700167 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700168 }
169 void endTiming() override {
bsalomonc8699322016-05-11 11:55:36 -0700170 if (this->context) {
171 this->context->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700172 }
173 }
174 void fence() override {
bsalomonc8699322016-05-11 11:55:36 -0700175 this->context->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 {
bsalomonc8699322016-05-11 11:55:36 -0700179 if (!this->context->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700180 // 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);
bsalomonc8699322016-05-11 11:55:36 -0700193 this->context = gGrFactory->getContextInfo(this->config.ctxType,
194 this->config.ctxOptions).testContext();
tomhudsond968a6f2015-03-26 11:28:06 -0700195 if (!this->surface.get()) {
196 return false;
197 }
bsalomonc8699322016-05-11 11:55:36 -0700198 if (!this->context->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700199 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;
bsalomonc8699322016-05-11 11:55:36 -0700206 if (this->context->backend() == kOpenGL_GrBackend) {
207 const GrGLInterface* gl =
208 reinterpret_cast<const GrGLInterface*>(this->context->backendContext());
209 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
210 log->configOption("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700211
bsalomonc8699322016-05-11 11:55:36 -0700212 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
213 log->configOption("GL_RENDERER", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700214
bsalomonc8699322016-05-11 11:55:36 -0700215 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VENDOR));
216 log->configOption("GL_VENDOR", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700217
bsalomonc8699322016-05-11 11:55:36 -0700218 GR_GL_CALL_RET(gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
219 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
220 }
tomhudsond968a6f2015-03-26 11:28:06 -0700221 }
222};
mtkleind75c4662015-04-30 07:11:22 -0700223
tomhudsond968a6f2015-03-26 11:28:06 -0700224#endif
225
tomhudson75a0ebb2015-03-27 12:11:44 -0700226static double time(int loops, Benchmark* bench, Target* target) {
227 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700228 if (canvas) {
229 canvas->clear(SK_ColorWHITE);
230 }
joshualitt8a6697a2015-09-30 12:11:07 -0700231 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700232 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700233 canvas = target->beginTiming(canvas);
234 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700235 if (canvas) {
236 canvas->flush();
237 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700238 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700239 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700240 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700241 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700242}
243
mtkleinf3723212014-06-25 14:08:00 -0700244static double estimate_timer_overhead() {
245 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700246 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700247 double start = now_ms();
248 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700249 }
250 return overhead / FLAGS_overheadLoops;
251}
252
reed53249782014-10-10 09:09:52 -0700253static int detect_forever_loops(int loops) {
254 // look for a magic run-forever value
255 if (loops < 0) {
256 loops = SK_MaxS32;
257 }
258 return loops;
259}
260
mtklein55b0ffc2014-07-17 08:38:23 -0700261static int clamp_loops(int loops) {
262 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800263 SkDebugf("ERROR: clamping loops from %d to 1. "
264 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700265 return 1;
266 }
267 if (loops > FLAGS_maxLoops) {
268 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
269 return FLAGS_maxLoops;
270 }
271 return loops;
272}
273
tomhudsond968a6f2015-03-26 11:28:06 -0700274static bool write_canvas_png(Target* target, const SkString& filename) {
275
bsalomon6eb03cc2014-08-07 14:28:50 -0700276 if (filename.isEmpty()) {
277 return false;
278 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700279 if (target->getCanvas() &&
280 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700281 return false;
282 }
tomhudsond968a6f2015-03-26 11:28:06 -0700283
bsalomon6eb03cc2014-08-07 14:28:50 -0700284 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700285
286 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700287 return false;
288 }
tomhudsond968a6f2015-03-26 11:28:06 -0700289
bsalomon6eb03cc2014-08-07 14:28:50 -0700290 SkString dir = SkOSPath::Dirname(filename.c_str());
291 if (!sk_mkdir(dir.c_str())) {
292 SkDebugf("Can't make dir %s.\n", dir.c_str());
293 return false;
294 }
295 SkFILEWStream stream(filename.c_str());
296 if (!stream.isValid()) {
297 SkDebugf("Can't write %s.\n", filename.c_str());
298 return false;
299 }
300 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
301 SkDebugf("Can't encode a PNG.\n");
302 return false;
303 }
304 return true;
305}
306
307static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700308static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700309 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700310 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700311 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700312 int loops = bench->calculateLoops(FLAGS_loops);
313 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700314 while (bench_plus_overhead < overhead) {
315 if (round++ == FLAGS_maxCalibrationAttempts) {
316 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700317 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700318 return kFailedLoops;
319 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700320 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700321 }
mtklein2069e222014-08-04 13:57:39 -0700322 }
mtkleinf3723212014-06-25 14:08:00 -0700323
mtkleinbb6a0282014-07-01 08:43:42 -0700324 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700325 // We'll pick N to make timer overhead negligible:
326 //
mtkleinbb6a0282014-07-01 08:43:42 -0700327 // overhead
328 // ------------------------- < FLAGS_overheadGoal
329 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700330 //
mtkleinbb6a0282014-07-01 08:43:42 -0700331 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700332 //
333 // Doing some math, we get:
334 //
mtkleinbb6a0282014-07-01 08:43:42 -0700335 // (overhead / FLAGS_overheadGoal) - overhead
336 // ------------------------------------------ < N
337 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700338 //
339 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700340 if (kAutoTuneLoops == loops) {
341 const double numer = overhead / FLAGS_overheadGoal - overhead;
342 const double denom = bench_plus_overhead - overhead;
343 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700344 loops = clamp_loops(loops);
345 } else {
346 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700347 }
mtkleinbb6a0282014-07-01 08:43:42 -0700348
mtkleinbb6a0282014-07-01 08:43:42 -0700349 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700350}
351
cdaltone1b89582015-06-25 19:17:08 -0700352static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700353 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700354 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700355 if (kAutoTuneLoops == loops) {
356 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700357 double elapsed = 0;
358 do {
mtklein527930f2014-11-06 08:04:34 -0800359 if (1<<30 == loops) {
360 // We're about to wrap. Something's wrong with the bench.
361 loops = 0;
362 break;
363 }
mtkleina189ccd2014-07-14 12:28:47 -0700364 loops *= 2;
365 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700366 // _this_ round, not still timing last round.
367 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700368 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700369 }
370 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700371
mtkleina189ccd2014-07-14 12:28:47 -0700372 // We've overshot at least a little. Scale back linearly.
373 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700374 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700375
tomhudsond968a6f2015-03-26 11:28:06 -0700376 // Make sure we're not still timing our calibration.
377 target->fence();
reed53249782014-10-10 09:09:52 -0700378 } else {
379 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700380 }
mtkleinbb6a0282014-07-01 08:43:42 -0700381
382 // Pretty much the same deal as the calibration: do some warmup to make
383 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700384 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700385 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700386 }
mtkleinbb6a0282014-07-01 08:43:42 -0700387
mtkleinbb6a0282014-07-01 08:43:42 -0700388 return loops;
389}
mtkleinbb6a0282014-07-01 08:43:42 -0700390
bsalomonc2553372014-07-22 13:09:05 -0700391#if SK_SUPPORT_GPU
bsalomon85b4b532016-04-05 11:06:27 -0700392#define kBogusContextType GrContextFactory::kNativeGL_ContextType
393#define kBogusContextOptions GrContextFactory::kNone_ContextOptions
bsalomonc2553372014-07-22 13:09:05 -0700394#else
bsalomon85b4b532016-04-05 11:06:27 -0700395#define kBogusContextType 0
396#define kBogusContextOptions 0
mtkleine714e752014-07-31 12:13:48 -0700397#endif
bsalomonc2553372014-07-22 13:09:05 -0700398
svaisanenc47635e2016-01-28 06:05:43 -0800399static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
400
401#if SK_SUPPORT_GPU
402 if (const auto* gpuConfig = config->asConfigGpu()) {
403 if (!FLAGS_gpu)
404 return;
405
brianosman1a48e132016-06-20 09:41:41 -0700406 auto ctxOptions = GrContextFactory::kNone_ContextOptions;
407 if (gpuConfig->getUseNVPR()) {
408 ctxOptions = static_cast<GrContextFactory::ContextOptions>(
409 ctxOptions | GrContextFactory::kEnableNVPR_ContextOptions);
410 }
csmartdaltone0d36292016-07-29 08:14:20 -0700411 if (gpuConfig->getUseInstanced()) {
412 ctxOptions = static_cast<GrContextFactory::ContextOptions>(
413 ctxOptions | GrContextFactory::kUseInstanced_ContextOptions);
414 }
brianosman1a48e132016-06-20 09:41:41 -0700415 if (SkColorAndColorSpaceAreGammaCorrect(gpuConfig->getColorType(),
416 gpuConfig->getColorSpace())) {
417 ctxOptions = static_cast<GrContextFactory::ContextOptions>(
418 ctxOptions | GrContextFactory::kRequireSRGBSupport_ContextOptions);
419 }
svaisanenc47635e2016-01-28 06:05:43 -0800420 const auto ctxType = gpuConfig->getContextType();
421 const auto sampleCount = gpuConfig->getSamples();
422
423 if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOptions)) {
424 const auto maxSampleCount = ctx->caps()->maxSampleCount();
425 if (sampleCount > ctx->caps()->maxSampleCount()) {
426 SkDebugf("Configuration sample count %d exceeds maximum %d.\n",
427 sampleCount, maxSampleCount);
428 return;
429 }
430 } else {
431 SkDebugf("No context was available matching config type and options.\n");
432 return;
433 }
434
435 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800436 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800437 Benchmark::kGPU_Backend,
brianosman1a48e132016-06-20 09:41:41 -0700438 gpuConfig->getColorType(),
svaisanenc47635e2016-01-28 06:05:43 -0800439 kPremul_SkAlphaType,
brianosman1a48e132016-06-20 09:41:41 -0700440 sk_ref_sp(gpuConfig->getColorSpace()),
svaisanenc47635e2016-01-28 06:05:43 -0800441 sampleCount,
442 ctxType,
kkinnunend4e1c352016-03-01 23:41:26 -0800443 ctxOptions,
444 gpuConfig->getUseDIText()
445 };
svaisanenc47635e2016-01-28 06:05:43 -0800446
447 configs->push_back(target);
448 return;
449 }
450#endif
451
brianosmanb109b8c2016-06-16 13:03:24 -0700452 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
453 if (config->getTag().equals(#name)) { \
454 Config config = { \
455 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
456 0, kBogusContextType, kBogusContextOptions, false \
457 }; \
458 configs->push_back(config); \
459 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700460 }
mtkleine714e752014-07-31 12:13:48 -0700461
mtklein40b32be2014-07-09 08:46:49 -0700462 if (FLAGS_cpu) {
mtkleinbb6c41b2016-03-08 11:31:11 -0800463 CPU_CONFIG(nonrendering, kNonRendering_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700464 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800465
466 CPU_CONFIG(8888, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700467 kN32_SkColorType, kPremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800468 CPU_CONFIG(565, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700469 kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
470 auto srgbColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
mtkleinbb6c41b2016-03-08 11:31:11 -0800471 CPU_CONFIG(srgb, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700472 kN32_SkColorType, kPremul_SkAlphaType, srgbColorSpace)
mtkleinbb6c41b2016-03-08 11:31:11 -0800473 CPU_CONFIG(f16, kRaster_Backend,
brianosman0e22eb82016-08-30 07:07:59 -0700474 kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbColorSpace->makeLinearGamma())
mtklein40b32be2014-07-09 08:46:49 -0700475 }
mtkleinbb6a0282014-07-01 08:43:42 -0700476
svaisanenc47635e2016-01-28 06:05:43 -0800477 #undef CPU_CONFIG
tomhudsond968a6f2015-03-26 11:28:06 -0700478
479#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
djsollen817c72a2016-01-28 13:57:02 -0800480 if (config->getTag().equals("hwui")) {
mtkleinbb6c41b2016-03-08 11:31:11 -0800481 Config config = { SkString("hwui"), Benchmark::kHWUI_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700482 kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr,
bsalomon85b4b532016-04-05 11:06:27 -0700483 0, kBogusContextType, kBogusContextOptions, false };
svaisanenc47635e2016-01-28 06:05:43 -0800484 configs->push_back(config);
tomhudsond968a6f2015-03-26 11:28:06 -0700485 }
486#endif
mtkleinf3723212014-06-25 14:08:00 -0700487}
488
svaisanenc47635e2016-01-28 06:05:43 -0800489// Append all configs that are enabled and supported.
490void create_configs(SkTArray<Config>* configs) {
491 SkCommandLineConfigArray array;
492 ParseConfigs(FLAGS_config, &array);
493 for (int i = 0; i < array.count(); ++i) {
494 create_config(array[i], configs);
495 }
496}
497
halcanary96fcdcc2015-08-27 07:41:13 -0700498// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700499static Target* is_enabled(Benchmark* bench, const Config& config) {
500 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700501 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700502 }
503
reede5ea5002014-09-03 11:54:58 -0700504 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700505 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700506
halcanary96fcdcc2015-08-27 07:41:13 -0700507 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700508
tomhudsond968a6f2015-03-26 11:28:06 -0700509 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700510#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700511 case Benchmark::kGPU_Backend:
512 target = new GPUTarget(config);
513 break;
bsalomonc2553372014-07-22 13:09:05 -0700514#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700515#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
516 case Benchmark::kHWUI_Backend:
517 target = new HWUITarget(config, bench);
518 break;
519#endif
520 default:
521 target = new Target(config);
522 break;
523 }
bsalomonc2553372014-07-22 13:09:05 -0700524
tomhudsond968a6f2015-03-26 11:28:06 -0700525 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700526 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700527 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700528 }
529 return target;
530}
531
reed42943c82016-09-12 12:01:44 -0700532static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700533 uint32_t minOutputSize, int* width, int* height) {
msarett5cb48852015-11-06 08:56:32 -0800534 SkAutoTDelete<SkBitmapRegionDecoder> brd(
msarettd1227a72016-05-18 06:23:57 -0700535 SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy));
msarett7f691442015-09-22 11:56:16 -0700536 if (nullptr == brd.get()) {
537 // This is indicates that subset decoding is not supported for a particular image format.
538 return false;
539 }
540
msarett7f691442015-09-22 11:56:16 -0700541 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
542 (uint32_t) brd->height()) {
543 // This indicates that the image is not large enough to decode a
544 // minOutputSize x minOutputSize subset at the given sampleSize.
545 return false;
546 }
547
548 // Set the image width and height. The calling code will use this to choose subsets to decode.
549 *width = brd->width();
550 *height = brd->height();
551 return true;
552}
553
egdaniel3bf92062015-06-26 08:12:46 -0700554static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700555 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700556#if SK_SUPPORT_GPU
557 if (FLAGS_abandonGpuContext) {
558 gGrFactory->abandonContexts();
559 }
560 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
561 gGrFactory->destroyContexts();
562 }
563#endif
564}
565
mtkleine714e752014-07-31 12:13:48 -0700566class BenchmarkStream {
567public:
mtklein92007582014-08-01 07:46:52 -0700568 BenchmarkStream() : fBenches(BenchRegistry::Head())
569 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700570 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700571 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800572 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800573 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700574 , fCurrentCodec(0)
msarett84451022016-02-11 06:45:51 -0800575 , fCurrentAndroidCodec(0)
msarett7f691442015-09-22 11:56:16 -0700576 , fCurrentBRDImage(0)
msarett2cee9022016-06-03 08:25:21 -0700577 , fCurrentColorImage(0)
msarett95f192d2015-02-13 09:05:41 -0800578 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800579 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700580 , fCurrentSubsetType(0)
msarett84451022016-02-11 06:45:51 -0800581 , fCurrentSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700582 , fCurrentAnimSKP(0) {
mtklein92007582014-08-01 07:46:52 -0700583 for (int i = 0; i < FLAGS_skps.count(); i++) {
584 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
585 fSKPs.push_back() = FLAGS_skps[i];
586 } else {
587 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
588 SkString path;
589 while (it.next(&path)) {
590 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
591 }
592 }
593 }
mtkleine714e752014-07-31 12:13:48 -0700594
mtklein92007582014-08-01 07:46:52 -0700595 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
596 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
597 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
598 exit(1);
599 }
600
601 for (int i = 0; i < FLAGS_scales.count(); i++) {
602 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
603 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
604 exit(1);
605 }
606 }
robertphillips5b693772014-11-21 06:19:36 -0800607
cdalton63a82852015-06-29 14:06:10 -0700608 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
609 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700610 exit(1);
611 }
612
robertphillips5b693772014-11-21 06:19:36 -0800613 if (FLAGS_mpd) {
614 fUseMPDs.push_back() = true;
615 }
mtkleinc751ecb2015-06-15 08:56:38 -0700616 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700617
msarett95f192d2015-02-13 09:05:41 -0800618 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700619 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800620 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800621 }
msarett2cee9022016-06-03 08:25:21 -0700622 if (!CollectImages(FLAGS_colorImages, &fColorImages)) {
623 exit(1);
624 }
mtklein95553d92015-03-12 08:24:21 -0700625
msarett95f192d2015-02-13 09:05:41 -0800626 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700627 fColorTypes.push_back(kN32_SkColorType);
628 if (!FLAGS_simpleCodec) {
629 fColorTypes.push_back(kRGB_565_SkColorType);
630 fColorTypes.push_back(kAlpha_8_SkColorType);
631 fColorTypes.push_back(kIndex_8_SkColorType);
632 fColorTypes.push_back(kGray_8_SkColorType);
633 }
mtklein92007582014-08-01 07:46:52 -0700634 }
635
reedca2622b2016-03-18 07:25:55 -0700636 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700637 // Not strictly necessary, as it will be checked again later,
638 // but helps to avoid a lot of pointless work if we're going to skip it.
cdalton91e457d2016-02-17 11:10:16 -0800639 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700640 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700641 }
642
scroggoa1193e42015-01-21 12:09:53 -0800643 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -0700644 if (stream.get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700645 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700646 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700647 }
648
reedca2622b2016-03-18 07:25:55 -0700649 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700650 }
651
mtklein92007582014-08-01 07:46:52 -0700652 Benchmark* next() {
mtklein65dfd2f2016-02-03 10:40:54 -0800653 SkAutoTDelete<Benchmark> bench;
654 do {
655 bench.reset(this->rawNext());
656 if (!bench) {
657 return nullptr;
658 }
659 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
660 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700661 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800662 }
663
664 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700665 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700666 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700667 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700668 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700669 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700670 return bench;
671 }
mtklein92007582014-08-01 07:46:52 -0700672
mtkleine714e752014-07-31 12:13:48 -0700673 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -0700674 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700675 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800676 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700677 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700678 fBenchType = "micro";
mtklein18300a32016-03-16 13:53:35 -0700679 return new GMBench(gm.release());
mtkleine714e752014-07-31 12:13:48 -0700680 }
681 }
mtklein92007582014-08-01 07:46:52 -0700682
mtkleinfd731ce2014-09-10 12:19:30 -0700683 // First add all .skps as RecordingBenches.
684 while (fCurrentRecording < fSKPs.count()) {
685 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700686 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
687 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700688 continue;
689 }
690 SkString name = SkOSPath::Basename(path.c_str());
691 fSourceType = "skp";
692 fBenchType = "recording";
reedca2622b2016-03-18 07:25:55 -0700693 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic.get()));
mtklein051e56d2014-12-04 08:46:51 -0800694 fSKPOps = pic->approximateOpCount();
mtklein8c1a4f82016-08-08 06:56:22 -0700695 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh, FLAGS_lite);
mtkleinfd731ce2014-09-10 12:19:30 -0700696 }
697
698 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700699 while (fCurrentScale < fScales.count()) {
700 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800701 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700702 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
703 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800704 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700705 continue;
706 }
robertphillips5b693772014-11-21 06:19:36 -0800707
708 while (fCurrentUseMPD < fUseMPDs.count()) {
709 if (FLAGS_bbh) {
710 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
711 SkRTreeFactory factory;
712 SkPictureRecorder recorder;
robertphillips5b693772014-11-21 06:19:36 -0800713 pic->playback(recorder.beginRecording(pic->cullRect().width(),
714 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800715 &factory,
robertphillipsdda54452016-07-13 13:27:16 -0700716 0));
reedca2622b2016-03-18 07:25:55 -0700717 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800718 }
719 SkString name = SkOSPath::Basename(path.c_str());
720 fSourceType = "skp";
721 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700722 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
723 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700724 }
robertphillips5b693772014-11-21 06:19:36 -0800725 fCurrentUseMPD = 0;
726 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700727 }
728 fCurrentSKP = 0;
729 fCurrentScale++;
730 }
731
joshualitt261c3ad2015-04-27 09:16:57 -0700732 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700733 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700734 while (fCurrentAnimSKP < fSKPs.count()) {
735 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700736 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
737 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700738 fCurrentAnimSKP++;
739 continue;
740 }
741
742 fCurrentAnimSKP++;
743 SkString name = SkOSPath::Basename(path.c_str());
cdalton63a82852015-06-29 14:06:10 -0700744 SkAutoTUnref<SKPAnimationBench::Animation> animation(
745 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
halcanary385fe4d2015-08-26 13:07:48 -0700746 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation,
747 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700748 }
749 }
750
scroggo60869a42015-04-01 12:09:17 -0700751 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700752 fSourceType = "image";
753 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700754 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800755 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
756 continue;
757 }
bungeman38d909e2016-08-02 14:40:46 -0700758 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
reed42943c82016-09-12 12:01:44 -0700759 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700760 if (!codec) {
761 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700762 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700763 continue;
764 }
scroggo21027992015-04-02 13:22:38 -0700765
scroggo60869a42015-04-01 12:09:17 -0700766 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700767 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700768
msarettc7796b92016-01-07 14:20:20 -0800769 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700770 if (FLAGS_simpleCodec) {
771 if (kUnpremul_SkAlphaType == alphaType) {
772 alphaType = kPremul_SkAlphaType;
773 }
774
775 fCurrentColorType++;
776 } else {
777 switch (alphaType) {
778 case kOpaque_SkAlphaType:
779 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800780 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700781 break;
782 case kUnpremul_SkAlphaType:
783 case kPremul_SkAlphaType:
784 if (0 == fCurrentAlphaType) {
785 // Test unpremul first.
786 alphaType = kUnpremul_SkAlphaType;
787 fCurrentAlphaType++;
788 } else {
789 // Test premul.
790 alphaType = kPremul_SkAlphaType;
791 fCurrentAlphaType = 0;
792 fCurrentColorType++;
793 }
794 break;
795 default:
796 SkASSERT(false);
797 fCurrentColorType++;
798 break;
799 }
scroggo21027992015-04-02 13:22:38 -0700800 }
801
msarettc7796b92016-01-07 14:20:20 -0800802 // Make sure we can decode to this color type and alpha type.
803 SkImageInfo info =
804 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700805 const size_t rowBytes = info.minRowBytes();
806 SkAutoMalloc storage(info.getSafeSize(rowBytes));
807
808 // Used if fCurrentColorType is kIndex_8_SkColorType
809 int colorCount = 256;
810 SkPMColor colors[256];
811
scroggoeb602a52015-07-09 08:16:03 -0700812 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700813 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700814 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700815 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700816 case SkCodec::kSuccess:
817 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700818 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700819 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700820 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700821 // This is okay. Not all conversions are valid.
822 break;
scroggo60869a42015-04-01 12:09:17 -0700823 default:
824 // This represents some sort of failure.
825 SkASSERT(false);
826 break;
827 }
828 }
829 fCurrentColorType = 0;
830 }
831
msarett84451022016-02-11 06:45:51 -0800832 // Run AndroidCodecBenches
833 const int sampleSizes[] = { 2, 4, 8 };
834 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
835 fSourceType = "image";
836 fBenchType = "skandroidcodec";
837
838 const SkString& path = fImages[fCurrentAndroidCodec];
839 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
840 continue;
841 }
bungeman38d909e2016-08-02 14:40:46 -0700842 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
reed42943c82016-09-12 12:01:44 -0700843 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800844 if (!codec) {
845 // Nothing to time.
846 SkDebugf("Cannot find codec for %s\n", path.c_str());
847 continue;
848 }
849
850 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
851 int sampleSize = sampleSizes[fCurrentSampleSize];
852 fCurrentSampleSize++;
853 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
854 // Avoid benchmarking scaled decodes of already small images.
855 break;
856 }
857
bungeman38d909e2016-08-02 14:40:46 -0700858 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
859 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -0800860 }
861 fCurrentSampleSize = 0;
862 }
863
msarett7f691442015-09-22 11:56:16 -0700864 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700865 // We intend to create benchmarks that model the use cases in
866 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
867 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
868 // the image. For that reason, we will benchmark decodes in five representative locations
869 // in the image. Additionally, this use case utilizes power of two scaling, so we will
870 // test on power of two sample sizes. The output tile is always 512x512, so, when a
871 // sampleSize is used, the size of the subset that is decoded is always
872 // (sampleSize*512)x(sampleSize*512).
873 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700874 // All use cases we are aware of only scale by powers of two.
875 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
876 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800877 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700878 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800879 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700880 fSourceType = "image";
881 fBenchType = "BRD";
882
mtklein6f0ff912016-01-11 09:04:21 -0800883 const SkString& path = fImages[fCurrentBRDImage];
884 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
885 continue;
886 }
scroggo860e8a62015-10-15 07:51:28 -0700887
msarettd1227a72016-05-18 06:23:57 -0700888 while (fCurrentColorType < fColorTypes.count()) {
889 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
890 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700891
bungeman38d909e2016-08-02 14:40:46 -0700892 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -0700893 const SkColorType colorType = fColorTypes[fCurrentColorType];
894 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
895 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -0700896
msarettd1227a72016-05-18 06:23:57 -0700897 int width = 0;
898 int height = 0;
reed42943c82016-09-12 12:01:44 -0700899 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -0700900 &width, &height)) {
901 break;
msarett7f691442015-09-22 11:56:16 -0700902 }
msarettd1227a72016-05-18 06:23:57 -0700903
904 SkString basename = SkOSPath::Basename(path.c_str());
905 SkIRect subset;
906 const uint32_t subsetSize = sampleSize * minOutputSize;
907 switch (currentSubsetType) {
908 case kTopLeft_SubsetType:
909 basename.append("_TopLeft");
910 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
911 break;
912 case kTopRight_SubsetType:
913 basename.append("_TopRight");
914 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
915 subsetSize);
916 break;
917 case kMiddle_SubsetType:
918 basename.append("_Middle");
919 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
920 (height - subsetSize) / 2, subsetSize, subsetSize);
921 break;
922 case kBottomLeft_SubsetType:
923 basename.append("_BottomLeft");
924 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
925 subsetSize);
926 break;
927 case kBottomRight_SubsetType:
928 basename.append("_BottomRight");
929 subset = SkIRect::MakeXYWH(width - subsetSize,
930 height - subsetSize, subsetSize, subsetSize);
931 break;
932 default:
933 SkASSERT(false);
934 }
935
936 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
937 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -0700938 }
msarettd1227a72016-05-18 06:23:57 -0700939 fCurrentSubsetType = 0;
940 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -0700941 }
msarettd1227a72016-05-18 06:23:57 -0700942 fCurrentSampleSize = 0;
943 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -0700944 }
msarettd1227a72016-05-18 06:23:57 -0700945 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -0700946 }
947
msarett2cee9022016-06-03 08:25:21 -0700948 while (fCurrentColorImage < fColorImages.count()) {
949 fSourceType = "colorimage";
950 fBenchType = "skcolorcodec";
951 const SkString& path = fColorImages[fCurrentColorImage];
952 fCurrentColorImage++;
953 sk_sp<SkData> encoded = SkData::MakeFromFileName(path.c_str());
954 if (encoded) {
955 return new ColorCodecBench(SkOSPath::Basename(path.c_str()).c_str(),
956 std::move(encoded));
957 } else {
958 SkDebugf("Could not read file %s.\n", path.c_str());
959 }
960 }
961
halcanary96fcdcc2015-08-27 07:41:13 -0700962 return nullptr;
mtkleine714e752014-07-31 12:13:48 -0700963 }
mtklein92007582014-08-01 07:46:52 -0700964
965 void fillCurrentOptions(ResultsWriter* log) const {
966 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700967 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700968 if (0 == strcmp(fSourceType, "skp")) {
969 log->configOption("clip",
970 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
971 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -0800972 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -0700973 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -0800974 if (fCurrentUseMPD > 0) {
975 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
976 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
977 }
mtklein92007582014-08-01 07:46:52 -0700978 }
mtklein051e56d2014-12-04 08:46:51 -0800979 if (0 == strcmp(fBenchType, "recording")) {
980 log->metric("bytes", fSKPBytes);
981 log->metric("ops", fSKPOps);
982 }
mtklein92007582014-08-01 07:46:52 -0700983 }
984
mtkleine714e752014-07-31 12:13:48 -0700985private:
msarettb23e6aa2015-06-09 13:56:10 -0700986 enum SubsetType {
987 kTopLeft_SubsetType = 0,
988 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -0700989 kMiddle_SubsetType = 2,
990 kBottomLeft_SubsetType = 3,
991 kBottomRight_SubsetType = 4,
992 kTranslate_SubsetType = 5,
993 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -0700994 kLast_SubsetType = kZoom_SubsetType,
995 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -0700996 };
997
mtkleine714e752014-07-31 12:13:48 -0700998 const BenchRegistry* fBenches;
999 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001000 SkIRect fClip;
1001 SkTArray<SkScalar> fScales;
1002 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -08001003 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001004 SkTArray<SkString> fImages;
msarett2cee9022016-06-03 08:25:21 -07001005 SkTArray<SkString> fColorImages;
msarett74deb982015-10-20 16:45:56 -07001006 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001007 SkScalar fZoomMax;
1008 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001009
mtklein051e56d2014-12-04 08:46:51 -08001010 double fSKPBytes, fSKPOps;
1011
mtkleinfd731ce2014-09-10 12:19:30 -07001012 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1013 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1014 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -07001015 int fCurrentScale;
1016 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -08001017 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001018 int fCurrentCodec;
msarett84451022016-02-11 06:45:51 -08001019 int fCurrentAndroidCodec;
msarett7f691442015-09-22 11:56:16 -07001020 int fCurrentBRDImage;
msarett2cee9022016-06-03 08:25:21 -07001021 int fCurrentColorImage;
msarett95f192d2015-02-13 09:05:41 -08001022 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -08001023 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -07001024 int fCurrentSubsetType;
msarett84451022016-02-11 06:45:51 -08001025 int fCurrentSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001026 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001027};
1028
msarettc149f0e2016-01-04 11:35:43 -08001029// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1030// This prints something every once in a while so that it knows we're still working.
1031static void start_keepalive() {
1032 struct Loop {
1033 static void forever(void*) {
1034 for (;;) {
1035 static const int kSec = 1200;
1036 #if defined(SK_BUILD_FOR_WIN)
1037 Sleep(kSec * 1000);
1038 #else
1039 sleep(kSec);
1040 #endif
1041 SkDebugf("\nBenchmarks still running...\n");
1042 }
1043 }
1044 };
1045 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1046 intentionallyLeaked->start();
1047}
1048
jcgregorio3b27ade2014-11-13 08:06:40 -08001049int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001050int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001051 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001052 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001053 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001054
krajcevski69a55602014-08-13 10:46:31 -07001055#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001056 GrContextOptions grContextOpts;
halcanary385fe4d2015-08-26 13:07:48 -07001057 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001058#endif
1059
bsalomon06cddec2014-10-24 10:40:50 -07001060 if (FLAGS_veryVerbose) {
1061 FLAGS_verbose = true;
1062 }
1063
bsalomon6eb03cc2014-08-07 14:28:50 -07001064 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001065 FLAGS_samples = 1;
1066 FLAGS_gpuFrameLag = 0;
1067 }
1068
bsalomon6eb03cc2014-08-07 14:28:50 -07001069 if (!FLAGS_writePath.isEmpty()) {
1070 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1071 if (!sk_mkdir(FLAGS_writePath[0])) {
1072 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001073 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001074 }
1075 }
1076
halcanary385fe4d2015-08-26 13:07:48 -07001077 SkAutoTDelete<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001078 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001079#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001080 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001081#else
1082 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1083 return 1;
1084#endif
mtklein60317d0f2014-07-14 11:30:37 -07001085 }
mtklein1915b622014-08-20 11:45:00 -07001086
1087 if (1 == FLAGS_properties.count() % 2) {
1088 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1089 return 1;
1090 }
1091 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1092 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1093 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001094
1095 if (1 == FLAGS_key.count() % 2) {
1096 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1097 return 1;
1098 }
1099 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001100 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001101 }
mtklein60317d0f2014-07-14 11:30:37 -07001102
mtkleinf3723212014-06-25 14:08:00 -07001103 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001104 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001105
cdaltone1b89582015-06-25 19:17:08 -07001106 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001107
bsalomon6eb03cc2014-08-07 14:28:50 -07001108 if (kAutoTuneLoops != FLAGS_loops) {
1109 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001110 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001111 SkDebugf("! -> high variance, ? -> moderate variance\n");
1112 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001113 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001114 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001115 } else {
mtkleind75c4662015-04-30 07:11:22 -07001116 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001117 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001118 }
1119
svaisanenc47635e2016-01-28 06:05:43 -08001120 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001121 create_configs(&configs);
1122
joshualitt3ebd0502016-02-09 07:18:08 -08001123#ifdef THERMAL_MANAGER_SUPPORTED
1124 int tmEnabled, tmThreshold, tmSleepTimeMs, tmTimeoutMs;
1125 if (4 != sscanf(FLAGS_useThermalManager[0], "%d,%d,%d,%d",
1126 &tmEnabled, &tmThreshold, &tmSleepTimeMs, &tmTimeoutMs)) {
1127 SkDebugf("Can't parse %s from --useThermalManager.\n", FLAGS_useThermalManager[0]);
1128 exit(1);
1129 }
1130 ThermalManager tm(tmThreshold, tmSleepTimeMs, tmTimeoutMs);
1131#endif
1132
msarettc149f0e2016-01-04 11:35:43 -08001133 if (FLAGS_keepAlive) {
1134 start_keepalive();
1135 }
1136
mtkleine070c2b2014-10-14 08:40:43 -07001137 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001138 BenchmarkStream benchStream;
1139 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001140 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001141 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001142 continue;
1143 }
1144
svaisanenc47635e2016-01-28 06:05:43 -08001145 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001146 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001147 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001148 }
egdaniel3bf92062015-06-26 08:12:46 -07001149 for (int i = 0; i < configs.count(); ++i) {
joshualitt3ebd0502016-02-09 07:18:08 -08001150#ifdef THERMAL_MANAGER_SUPPORTED
1151 if (tmEnabled && !tm.coolOffIfNecessary()) {
1152 SkDebugf("Could not cool off, timings will be throttled\n");
1153 }
1154#endif
egdaniel3bf92062015-06-26 08:12:46 -07001155 Target* target = is_enabled(b, configs[i]);
1156 if (!target) {
1157 continue;
1158 }
mtkleinf3723212014-06-25 14:08:00 -07001159
halcanary96fcdcc2015-08-27 07:41:13 -07001160 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001161 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001162 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001163
brianosman7a5ada82016-02-08 13:49:12 -08001164 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001165 SkDebugf("Running %s\t%s\n"
1166 , bench->getUniqueName()
1167 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001168 if (FLAGS_dryRun) {
1169 continue;
1170 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001171 }
1172
egdaniel3bf92062015-06-26 08:12:46 -07001173 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001174 bench->perCanvasPreDraw(canvas);
1175
cdaltone1b89582015-06-25 19:17:08 -07001176 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001177 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001178 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1179 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001180
mtkleinbbba1682015-10-28 11:36:30 -07001181 if (FLAGS_ms) {
1182 samples.reset();
1183 auto stop = now_ms() + FLAGS_ms;
1184 do {
1185 samples.push_back(time(loops, bench, target) / loops);
1186 } while (now_ms() < stop);
1187 } else {
cdaltone1b89582015-06-25 19:17:08 -07001188 samples.reset(FLAGS_samples);
1189 for (int s = 0; s < FLAGS_samples; s++) {
egdaniel3bf92062015-06-26 08:12:46 -07001190 samples[s] = time(loops, bench, target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001191 }
cdaltone1b89582015-06-25 19:17:08 -07001192 }
mtkleinf3723212014-06-25 14:08:00 -07001193
joshualitte45c81c2015-12-02 09:05:37 -08001194#if SK_SUPPORT_GPU
1195 SkTArray<SkString> keys;
1196 SkTArray<double> values;
1197 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1198 if (gpuStatsDump) {
1199 // TODO cache stats
1200 bench->getGpuStats(canvas, &keys, &values);
1201 }
1202#endif
1203
robertphillips5b693772014-11-21 06:19:36 -08001204 bench->perCanvasPostDraw(canvas);
1205
egdaniel3bf92062015-06-26 08:12:46 -07001206 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001207 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001208 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001209 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001210 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001211 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001212 }
1213
1214 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001215 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001216 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001217 continue;
1218 }
1219
cdaltone1b89582015-06-25 19:17:08 -07001220 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001221 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001222 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001223 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001224 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001225 log->metric("min_ms", stats.min);
joshualitte45c81c2015-12-02 09:05:37 -08001226#if SK_SUPPORT_GPU
1227 if (gpuStatsDump) {
1228 // dump to json, only SKPBench currently returns valid keys / values
1229 SkASSERT(keys.count() == values.count());
1230 for (int i = 0; i < keys.count(); i++) {
1231 log->metric(keys[i].c_str(), values[i]);
1232 }
1233 }
1234#endif
1235
mtkleine070c2b2014-10-14 08:40:43 -07001236 if (runs++ % FLAGS_flushEvery == 0) {
1237 log->flush();
1238 }
mtklein60317d0f2014-07-14 11:30:37 -07001239
bsalomon6eb03cc2014-08-07 14:28:50 -07001240 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001241 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001242 config = ""; // Only print the config if we run the same bench on more than one.
1243 }
mtkleind75c4662015-04-30 07:11:22 -07001244 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1245 , sk_tools::getCurrResidentSetSizeMB()
1246 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001247 , bench->getUniqueName()
1248 , config);
mtkleinf3723212014-06-25 14:08:00 -07001249 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001250 const char* mark = " ";
1251 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1252 if (stddev_percent > 5) mark = "?";
1253 if (stddev_percent > 10) mark = "!";
1254
1255 SkDebugf("%10.2f %s\t%s\t%s\n",
1256 stats.median*1e3, mark, bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001257 } else {
1258 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001259 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1260 , sk_tools::getCurrResidentSetSizeMB()
1261 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001262 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001263 , HUMANIZE(stats.min)
1264 , HUMANIZE(stats.median)
1265 , HUMANIZE(stats.mean)
1266 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001267 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001268 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001269 , config
mtklein96289052014-09-10 12:05:59 -07001270 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001271 );
1272 }
joshualitte45c81c2015-12-02 09:05:37 -08001273
bsalomonb12ea412015-02-02 21:19:50 -08001274#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001275 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001276 GrContext* context = gGrFactory->get(configs[i].ctxType,
kkinnunen3e980c32015-12-23 01:33:00 -08001277 configs[i].ctxOptions);
kkinnunen5219fd92015-12-10 06:28:13 -08001278 context->printCacheStats();
1279 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001280 }
1281#endif
joshualitte45c81c2015-12-02 09:05:37 -08001282
cdalton2c56ba52015-06-26 13:32:53 -07001283 if (FLAGS_verbose) {
1284 SkDebugf("Samples: ");
1285 for (int i = 0; i < samples.count(); i++) {
1286 SkDebugf("%s ", HUMANIZE(samples[i]));
1287 }
1288 SkDebugf("%s\n", bench->getUniqueName());
1289 }
egdaniel3bf92062015-06-26 08:12:46 -07001290 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001291 }
mtkleinf3723212014-06-25 14:08:00 -07001292 }
1293
mtkleine1091452014-12-04 10:47:02 -08001294 log->bench("memory_usage", 0,0);
1295 log->config("meta");
1296 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1297
joshualitte0b19d42015-03-26 10:41:02 -07001298#if SK_SUPPORT_GPU
1299 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1300 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001301 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001302#endif
1303
mtkleinf3723212014-06-25 14:08:00 -07001304 return 0;
1305}
1306
jcgregorio3b27ade2014-11-13 08:06:40 -08001307#if !defined SK_BUILD_FOR_IOS
1308int main(int argc, char** argv) {
1309 SkCommandLineFlags::Parse(argc, argv);
1310 return nanobench_main();
1311}
1312#endif