blob: e5a63a022d9d1b68b98e70ae6ac3457d656c3d08 [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?");
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 {
bsalomonc8699322016-05-11 11:55:36 -0700160 explicit GPUTarget(const Config& c) : Target(c), context(nullptr) { }
161 TestContext* context;
tomhudsond968a6f2015-03-26 11:28:06 -0700162
163 void setup() override {
bsalomonc8699322016-05-11 11:55:36 -0700164 this->context->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700165 // Make sure we're done with whatever came before.
bsalomonc8699322016-05-11 11:55:36 -0700166 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700167 }
168 void endTiming() override {
bsalomonc8699322016-05-11 11:55:36 -0700169 if (this->context) {
170 this->context->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700171 }
172 }
173 void fence() override {
bsalomonc8699322016-05-11 11:55:36 -0700174 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700175 }
mtkleind75c4662015-04-30 07:11:22 -0700176
cdaltond416a5b2015-06-23 13:23:44 -0700177 bool needsFrameTiming(int* maxFrameLag) const override {
bsalomonc8699322016-05-11 11:55:36 -0700178 if (!this->context->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700179 // Frame lag is unknown.
180 *maxFrameLag = FLAGS_gpuFrameLag;
181 }
182 return true;
183 }
tomhudsond968a6f2015-03-26 11:28:06 -0700184 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700185 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
186 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700187 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700188 this->surface = SkSurface::MakeRenderTarget(gGrFactory->get(this->config.ctxType,
189 this->config.ctxOptions),
bsalomon5ec26ae2016-02-25 08:33:02 -0800190 SkBudgeted::kNo, info,
reede8f30622016-03-23 18:59:25 -0700191 this->config.samples, &props);
bsalomonc8699322016-05-11 11:55:36 -0700192 this->context = gGrFactory->getContextInfo(this->config.ctxType,
193 this->config.ctxOptions).testContext();
tomhudsond968a6f2015-03-26 11:28:06 -0700194 if (!this->surface.get()) {
195 return false;
196 }
bsalomonc8699322016-05-11 11:55:36 -0700197 if (!this->context->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700198 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800199 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700200 }
tomhudsond968a6f2015-03-26 11:28:06 -0700201 return true;
202 }
203 void fillOptions(ResultsWriter* log) override {
204 const GrGLubyte* version;
bsalomonc8699322016-05-11 11:55:36 -0700205 if (this->context->backend() == kOpenGL_GrBackend) {
206 const GrGLInterface* gl =
207 reinterpret_cast<const GrGLInterface*>(this->context->backendContext());
208 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
209 log->configOption("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700210
bsalomonc8699322016-05-11 11:55:36 -0700211 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
212 log->configOption("GL_RENDERER", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700213
bsalomonc8699322016-05-11 11:55:36 -0700214 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VENDOR));
215 log->configOption("GL_VENDOR", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700216
bsalomonc8699322016-05-11 11:55:36 -0700217 GR_GL_CALL_RET(gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
218 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
219 }
tomhudsond968a6f2015-03-26 11:28:06 -0700220 }
221};
mtkleind75c4662015-04-30 07:11:22 -0700222
tomhudsond968a6f2015-03-26 11:28:06 -0700223#endif
224
tomhudson75a0ebb2015-03-27 12:11:44 -0700225static double time(int loops, Benchmark* bench, Target* target) {
226 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700227 if (canvas) {
228 canvas->clear(SK_ColorWHITE);
229 }
joshualitt8a6697a2015-09-30 12:11:07 -0700230 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700231 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700232 canvas = target->beginTiming(canvas);
233 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700234 if (canvas) {
235 canvas->flush();
236 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700237 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700238 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700239 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700240 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700241}
242
mtkleinf3723212014-06-25 14:08:00 -0700243static double estimate_timer_overhead() {
244 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700245 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700246 double start = now_ms();
247 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700248 }
249 return overhead / FLAGS_overheadLoops;
250}
251
reed53249782014-10-10 09:09:52 -0700252static int detect_forever_loops(int loops) {
253 // look for a magic run-forever value
254 if (loops < 0) {
255 loops = SK_MaxS32;
256 }
257 return loops;
258}
259
mtklein55b0ffc2014-07-17 08:38:23 -0700260static int clamp_loops(int loops) {
261 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800262 SkDebugf("ERROR: clamping loops from %d to 1. "
263 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700264 return 1;
265 }
266 if (loops > FLAGS_maxLoops) {
267 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
268 return FLAGS_maxLoops;
269 }
270 return loops;
271}
272
tomhudsond968a6f2015-03-26 11:28:06 -0700273static bool write_canvas_png(Target* target, const SkString& filename) {
274
bsalomon6eb03cc2014-08-07 14:28:50 -0700275 if (filename.isEmpty()) {
276 return false;
277 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700278 if (target->getCanvas() &&
279 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700280 return false;
281 }
tomhudsond968a6f2015-03-26 11:28:06 -0700282
bsalomon6eb03cc2014-08-07 14:28:50 -0700283 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700284
285 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700286 return false;
287 }
tomhudsond968a6f2015-03-26 11:28:06 -0700288
bsalomon6eb03cc2014-08-07 14:28:50 -0700289 SkString dir = SkOSPath::Dirname(filename.c_str());
290 if (!sk_mkdir(dir.c_str())) {
291 SkDebugf("Can't make dir %s.\n", dir.c_str());
292 return false;
293 }
294 SkFILEWStream stream(filename.c_str());
295 if (!stream.isValid()) {
296 SkDebugf("Can't write %s.\n", filename.c_str());
297 return false;
298 }
299 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
300 SkDebugf("Can't encode a PNG.\n");
301 return false;
302 }
303 return true;
304}
305
306static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700307static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700308 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700309 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700310 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700311 int loops = bench->calculateLoops(FLAGS_loops);
312 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700313 while (bench_plus_overhead < overhead) {
314 if (round++ == FLAGS_maxCalibrationAttempts) {
315 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700316 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700317 return kFailedLoops;
318 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700319 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700320 }
mtklein2069e222014-08-04 13:57:39 -0700321 }
mtkleinf3723212014-06-25 14:08:00 -0700322
mtkleinbb6a0282014-07-01 08:43:42 -0700323 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700324 // We'll pick N to make timer overhead negligible:
325 //
mtkleinbb6a0282014-07-01 08:43:42 -0700326 // overhead
327 // ------------------------- < FLAGS_overheadGoal
328 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700329 //
mtkleinbb6a0282014-07-01 08:43:42 -0700330 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700331 //
332 // Doing some math, we get:
333 //
mtkleinbb6a0282014-07-01 08:43:42 -0700334 // (overhead / FLAGS_overheadGoal) - overhead
335 // ------------------------------------------ < N
336 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700337 //
338 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700339 if (kAutoTuneLoops == loops) {
340 const double numer = overhead / FLAGS_overheadGoal - overhead;
341 const double denom = bench_plus_overhead - overhead;
342 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700343 loops = clamp_loops(loops);
344 } else {
345 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700346 }
mtkleinbb6a0282014-07-01 08:43:42 -0700347
mtkleinbb6a0282014-07-01 08:43:42 -0700348 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700349}
350
cdaltone1b89582015-06-25 19:17:08 -0700351static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700352 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700353 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700354 if (kAutoTuneLoops == loops) {
355 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700356 double elapsed = 0;
357 do {
mtklein527930f2014-11-06 08:04:34 -0800358 if (1<<30 == loops) {
359 // We're about to wrap. Something's wrong with the bench.
360 loops = 0;
361 break;
362 }
mtkleina189ccd2014-07-14 12:28:47 -0700363 loops *= 2;
364 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700365 // _this_ round, not still timing last round.
366 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700367 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700368 }
369 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700370
mtkleina189ccd2014-07-14 12:28:47 -0700371 // We've overshot at least a little. Scale back linearly.
372 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700373 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700374
tomhudsond968a6f2015-03-26 11:28:06 -0700375 // Make sure we're not still timing our calibration.
376 target->fence();
reed53249782014-10-10 09:09:52 -0700377 } else {
378 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700379 }
mtkleinbb6a0282014-07-01 08:43:42 -0700380
381 // Pretty much the same deal as the calibration: do some warmup to make
382 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700383 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700384 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700385 }
mtkleinbb6a0282014-07-01 08:43:42 -0700386
mtkleinbb6a0282014-07-01 08:43:42 -0700387 return loops;
388}
mtkleinbb6a0282014-07-01 08:43:42 -0700389
bsalomonc2553372014-07-22 13:09:05 -0700390#if SK_SUPPORT_GPU
bsalomon85b4b532016-04-05 11:06:27 -0700391#define kBogusContextType GrContextFactory::kNativeGL_ContextType
392#define kBogusContextOptions GrContextFactory::kNone_ContextOptions
bsalomonc2553372014-07-22 13:09:05 -0700393#else
bsalomon85b4b532016-04-05 11:06:27 -0700394#define kBogusContextType 0
395#define kBogusContextOptions 0
mtkleine714e752014-07-31 12:13:48 -0700396#endif
bsalomonc2553372014-07-22 13:09:05 -0700397
svaisanenc47635e2016-01-28 06:05:43 -0800398static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
399
400#if SK_SUPPORT_GPU
401 if (const auto* gpuConfig = config->asConfigGpu()) {
402 if (!FLAGS_gpu)
403 return;
404
bsalomon85b4b532016-04-05 11:06:27 -0700405 const auto ctxOptions = gpuConfig->getUseNVPR() ? GrContextFactory::kEnableNVPR_ContextOptions
406 : GrContextFactory::kNone_ContextOptions;
svaisanenc47635e2016-01-28 06:05:43 -0800407 const auto ctxType = gpuConfig->getContextType();
408 const auto sampleCount = gpuConfig->getSamples();
409
410 if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOptions)) {
411 const auto maxSampleCount = ctx->caps()->maxSampleCount();
412 if (sampleCount > ctx->caps()->maxSampleCount()) {
413 SkDebugf("Configuration sample count %d exceeds maximum %d.\n",
414 sampleCount, maxSampleCount);
415 return;
416 }
417 } else {
418 SkDebugf("No context was available matching config type and options.\n");
419 return;
420 }
421
422 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800423 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800424 Benchmark::kGPU_Backend,
425 kN32_SkColorType,
426 kPremul_SkAlphaType,
mtkleinbb6c41b2016-03-08 11:31:11 -0800427 kLinear_SkColorProfileType,
svaisanenc47635e2016-01-28 06:05:43 -0800428 sampleCount,
429 ctxType,
kkinnunend4e1c352016-03-01 23:41:26 -0800430 ctxOptions,
431 gpuConfig->getUseDIText()
432 };
svaisanenc47635e2016-01-28 06:05:43 -0800433
434 configs->push_back(target);
435 return;
436 }
437#endif
438
mtkleinbb6c41b2016-03-08 11:31:11 -0800439 #define CPU_CONFIG(name, backend, color, alpha, profile) \
440 if (config->getTag().equals(#name)) { \
441 Config config = { \
442 SkString(#name), Benchmark::backend, color, alpha, profile, \
bsalomon85b4b532016-04-05 11:06:27 -0700443 0, kBogusContextType, kBogusContextOptions, false \
mtkleinbb6c41b2016-03-08 11:31:11 -0800444 }; \
445 configs->push_back(config); \
446 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700447 }
mtkleine714e752014-07-31 12:13:48 -0700448
mtklein40b32be2014-07-09 08:46:49 -0700449 if (FLAGS_cpu) {
mtkleinbb6c41b2016-03-08 11:31:11 -0800450 CPU_CONFIG(nonrendering, kNonRendering_Backend,
451 kUnknown_SkColorType, kUnpremul_SkAlphaType, kLinear_SkColorProfileType);
452
453 CPU_CONFIG(8888, kRaster_Backend,
454 kN32_SkColorType, kPremul_SkAlphaType, kLinear_SkColorProfileType)
455 CPU_CONFIG(565, kRaster_Backend,
456 kRGB_565_SkColorType, kOpaque_SkAlphaType, kLinear_SkColorProfileType)
457 CPU_CONFIG(srgb, kRaster_Backend,
458 kN32_SkColorType, kPremul_SkAlphaType, kSRGB_SkColorProfileType)
459 CPU_CONFIG(f16, kRaster_Backend,
460 kRGBA_F16_SkColorType, kPremul_SkAlphaType, kLinear_SkColorProfileType)
mtklein40b32be2014-07-09 08:46:49 -0700461 }
mtkleinbb6a0282014-07-01 08:43:42 -0700462
svaisanenc47635e2016-01-28 06:05:43 -0800463 #undef CPU_CONFIG
tomhudsond968a6f2015-03-26 11:28:06 -0700464
465#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
djsollen817c72a2016-01-28 13:57:02 -0800466 if (config->getTag().equals("hwui")) {
mtkleinbb6c41b2016-03-08 11:31:11 -0800467 Config config = { SkString("hwui"), Benchmark::kHWUI_Backend,
468 kRGBA_8888_SkColorType, kPremul_SkAlphaType, kLinear_SkColorProfileType,
bsalomon85b4b532016-04-05 11:06:27 -0700469 0, kBogusContextType, kBogusContextOptions, false };
svaisanenc47635e2016-01-28 06:05:43 -0800470 configs->push_back(config);
tomhudsond968a6f2015-03-26 11:28:06 -0700471 }
472#endif
mtkleinf3723212014-06-25 14:08:00 -0700473}
474
svaisanenc47635e2016-01-28 06:05:43 -0800475// Append all configs that are enabled and supported.
476void create_configs(SkTArray<Config>* configs) {
477 SkCommandLineConfigArray array;
478 ParseConfigs(FLAGS_config, &array);
479 for (int i = 0; i < array.count(); ++i) {
480 create_config(array[i], configs);
481 }
482}
483
halcanary96fcdcc2015-08-27 07:41:13 -0700484// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700485static Target* is_enabled(Benchmark* bench, const Config& config) {
486 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700487 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700488 }
489
reede5ea5002014-09-03 11:54:58 -0700490 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
mtkleinbb6c41b2016-03-08 11:31:11 -0800491 config.color, config.alpha, config.profile);
bsalomonc2553372014-07-22 13:09:05 -0700492
halcanary96fcdcc2015-08-27 07:41:13 -0700493 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700494
tomhudsond968a6f2015-03-26 11:28:06 -0700495 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700496#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700497 case Benchmark::kGPU_Backend:
498 target = new GPUTarget(config);
499 break;
bsalomonc2553372014-07-22 13:09:05 -0700500#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700501#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
502 case Benchmark::kHWUI_Backend:
503 target = new HWUITarget(config, bench);
504 break;
505#endif
506 default:
507 target = new Target(config);
508 break;
509 }
bsalomonc2553372014-07-22 13:09:05 -0700510
tomhudsond968a6f2015-03-26 11:28:06 -0700511 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700512 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700513 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700514 }
515 return target;
516}
517
msarettd1227a72016-05-18 06:23:57 -0700518static bool valid_brd_bench(SkData* encoded, SkColorType colorType, uint32_t sampleSize,
519 uint32_t minOutputSize, int* width, int* height) {
msarett5cb48852015-11-06 08:56:32 -0800520 SkAutoTDelete<SkBitmapRegionDecoder> brd(
msarettd1227a72016-05-18 06:23:57 -0700521 SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy));
msarett7f691442015-09-22 11:56:16 -0700522 if (nullptr == brd.get()) {
523 // This is indicates that subset decoding is not supported for a particular image format.
524 return false;
525 }
526
msarett7f691442015-09-22 11:56:16 -0700527 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
528 (uint32_t) brd->height()) {
529 // This indicates that the image is not large enough to decode a
530 // minOutputSize x minOutputSize subset at the given sampleSize.
531 return false;
532 }
533
534 // Set the image width and height. The calling code will use this to choose subsets to decode.
535 *width = brd->width();
536 *height = brd->height();
537 return true;
538}
539
egdaniel3bf92062015-06-26 08:12:46 -0700540static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700541 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700542#if SK_SUPPORT_GPU
543 if (FLAGS_abandonGpuContext) {
544 gGrFactory->abandonContexts();
545 }
546 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
547 gGrFactory->destroyContexts();
548 }
549#endif
550}
551
mtkleine714e752014-07-31 12:13:48 -0700552class BenchmarkStream {
553public:
mtklein92007582014-08-01 07:46:52 -0700554 BenchmarkStream() : fBenches(BenchRegistry::Head())
555 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700556 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700557 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800558 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800559 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700560 , fCurrentCodec(0)
msarett84451022016-02-11 06:45:51 -0800561 , fCurrentAndroidCodec(0)
msarett7f691442015-09-22 11:56:16 -0700562 , fCurrentBRDImage(0)
msarett2cee9022016-06-03 08:25:21 -0700563 , fCurrentColorImage(0)
msarett95f192d2015-02-13 09:05:41 -0800564 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800565 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700566 , fCurrentSubsetType(0)
msarett84451022016-02-11 06:45:51 -0800567 , fCurrentSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700568 , fCurrentAnimSKP(0) {
mtklein92007582014-08-01 07:46:52 -0700569 for (int i = 0; i < FLAGS_skps.count(); i++) {
570 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
571 fSKPs.push_back() = FLAGS_skps[i];
572 } else {
573 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
574 SkString path;
575 while (it.next(&path)) {
576 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
577 }
578 }
579 }
mtkleine714e752014-07-31 12:13:48 -0700580
mtklein92007582014-08-01 07:46:52 -0700581 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
582 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
583 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
584 exit(1);
585 }
586
587 for (int i = 0; i < FLAGS_scales.count(); i++) {
588 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
589 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
590 exit(1);
591 }
592 }
robertphillips5b693772014-11-21 06:19:36 -0800593
cdalton63a82852015-06-29 14:06:10 -0700594 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
595 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700596 exit(1);
597 }
598
robertphillips5b693772014-11-21 06:19:36 -0800599 if (FLAGS_mpd) {
600 fUseMPDs.push_back() = true;
601 }
mtkleinc751ecb2015-06-15 08:56:38 -0700602 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700603
msarett95f192d2015-02-13 09:05:41 -0800604 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700605 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800606 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800607 }
msarett2cee9022016-06-03 08:25:21 -0700608 if (!CollectImages(FLAGS_colorImages, &fColorImages)) {
609 exit(1);
610 }
mtklein95553d92015-03-12 08:24:21 -0700611
msarett95f192d2015-02-13 09:05:41 -0800612 // Choose the candidate color types for image decoding
613 const SkColorType colorTypes[] =
msarettb23e6aa2015-06-09 13:56:10 -0700614 { kN32_SkColorType,
615 kRGB_565_SkColorType,
616 kAlpha_8_SkColorType,
617 kIndex_8_SkColorType,
618 kGray_8_SkColorType };
msarett74deb982015-10-20 16:45:56 -0700619 fColorTypes.reset(colorTypes, SK_ARRAY_COUNT(colorTypes));
mtklein92007582014-08-01 07:46:52 -0700620 }
621
reedca2622b2016-03-18 07:25:55 -0700622 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700623 // Not strictly necessary, as it will be checked again later,
624 // but helps to avoid a lot of pointless work if we're going to skip it.
cdalton91e457d2016-02-17 11:10:16 -0800625 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700626 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700627 }
628
scroggoa1193e42015-01-21 12:09:53 -0800629 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -0700630 if (stream.get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700631 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700632 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700633 }
634
reedca2622b2016-03-18 07:25:55 -0700635 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700636 }
637
mtklein92007582014-08-01 07:46:52 -0700638 Benchmark* next() {
mtklein65dfd2f2016-02-03 10:40:54 -0800639 SkAutoTDelete<Benchmark> bench;
640 do {
641 bench.reset(this->rawNext());
642 if (!bench) {
643 return nullptr;
644 }
645 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
646 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700647 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800648 }
649
650 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700651 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700652 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700653 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700654 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700655 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700656 return bench;
657 }
mtklein92007582014-08-01 07:46:52 -0700658
mtkleine714e752014-07-31 12:13:48 -0700659 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -0700660 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700661 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800662 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700663 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700664 fBenchType = "micro";
mtklein18300a32016-03-16 13:53:35 -0700665 return new GMBench(gm.release());
mtkleine714e752014-07-31 12:13:48 -0700666 }
667 }
mtklein92007582014-08-01 07:46:52 -0700668
mtkleinfd731ce2014-09-10 12:19:30 -0700669 // First add all .skps as RecordingBenches.
670 while (fCurrentRecording < fSKPs.count()) {
671 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700672 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
673 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700674 continue;
675 }
676 SkString name = SkOSPath::Basename(path.c_str());
677 fSourceType = "skp";
678 fBenchType = "recording";
reedca2622b2016-03-18 07:25:55 -0700679 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic.get()));
mtklein051e56d2014-12-04 08:46:51 -0800680 fSKPOps = pic->approximateOpCount();
halcanary385fe4d2015-08-26 13:07:48 -0700681 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700682 }
683
684 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700685 while (fCurrentScale < fScales.count()) {
686 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800687 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700688 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
689 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800690 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700691 continue;
692 }
robertphillips5b693772014-11-21 06:19:36 -0800693
694 while (fCurrentUseMPD < fUseMPDs.count()) {
695 if (FLAGS_bbh) {
696 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
697 SkRTreeFactory factory;
698 SkPictureRecorder recorder;
699 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
700 pic->playback(recorder.beginRecording(pic->cullRect().width(),
701 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800702 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800703 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
reedca2622b2016-03-18 07:25:55 -0700704 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800705 }
706 SkString name = SkOSPath::Basename(path.c_str());
707 fSourceType = "skp";
708 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700709 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
710 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700711 }
robertphillips5b693772014-11-21 06:19:36 -0800712 fCurrentUseMPD = 0;
713 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700714 }
715 fCurrentSKP = 0;
716 fCurrentScale++;
717 }
718
joshualitt261c3ad2015-04-27 09:16:57 -0700719 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700720 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700721 while (fCurrentAnimSKP < fSKPs.count()) {
722 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700723 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
724 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700725 fCurrentAnimSKP++;
726 continue;
727 }
728
729 fCurrentAnimSKP++;
730 SkString name = SkOSPath::Basename(path.c_str());
cdalton63a82852015-06-29 14:06:10 -0700731 SkAutoTUnref<SKPAnimationBench::Animation> animation(
732 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
halcanary385fe4d2015-08-26 13:07:48 -0700733 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation,
734 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700735 }
736 }
737
scroggo60869a42015-04-01 12:09:17 -0700738 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700739 fSourceType = "image";
740 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700741 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800742 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
743 continue;
744 }
scroggo60869a42015-04-01 12:09:17 -0700745 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
746 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700747 if (!codec) {
748 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700749 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700750 continue;
751 }
scroggo21027992015-04-02 13:22:38 -0700752
scroggo60869a42015-04-01 12:09:17 -0700753 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700754 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700755
msarettc7796b92016-01-07 14:20:20 -0800756 SkAlphaType alphaType = codec->getInfo().alphaType();
757 switch (alphaType) {
758 case kOpaque_SkAlphaType:
759 // We only need to test one alpha type (opaque).
760 fCurrentColorType++;
761 break;
762 case kUnpremul_SkAlphaType:
763 case kPremul_SkAlphaType:
764 if (0 == fCurrentAlphaType) {
765 // Test unpremul first.
766 alphaType = kUnpremul_SkAlphaType;
767 fCurrentAlphaType++;
768 } else {
769 // Test premul.
770 alphaType = kPremul_SkAlphaType;
771 fCurrentAlphaType = 0;
772 fCurrentColorType++;
773 }
774 break;
775 default:
776 SkASSERT(false);
777 fCurrentColorType++;
778 break;
scroggo21027992015-04-02 13:22:38 -0700779 }
780
msarettc7796b92016-01-07 14:20:20 -0800781 // Make sure we can decode to this color type and alpha type.
782 SkImageInfo info =
783 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700784 const size_t rowBytes = info.minRowBytes();
785 SkAutoMalloc storage(info.getSafeSize(rowBytes));
786
787 // Used if fCurrentColorType is kIndex_8_SkColorType
788 int colorCount = 256;
789 SkPMColor colors[256];
790
scroggoeb602a52015-07-09 08:16:03 -0700791 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700792 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700793 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700794 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700795 case SkCodec::kSuccess:
796 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700797 return new CodecBench(SkOSPath::Basename(path.c_str()),
msarettc7796b92016-01-07 14:20:20 -0800798 encoded, colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700799 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700800 // This is okay. Not all conversions are valid.
801 break;
scroggo60869a42015-04-01 12:09:17 -0700802 default:
803 // This represents some sort of failure.
804 SkASSERT(false);
805 break;
806 }
807 }
808 fCurrentColorType = 0;
809 }
810
msarett84451022016-02-11 06:45:51 -0800811 // Run AndroidCodecBenches
812 const int sampleSizes[] = { 2, 4, 8 };
813 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
814 fSourceType = "image";
815 fBenchType = "skandroidcodec";
816
817 const SkString& path = fImages[fCurrentAndroidCodec];
818 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
819 continue;
820 }
821 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
822 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
823 if (!codec) {
824 // Nothing to time.
825 SkDebugf("Cannot find codec for %s\n", path.c_str());
826 continue;
827 }
828
829 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
830 int sampleSize = sampleSizes[fCurrentSampleSize];
831 fCurrentSampleSize++;
832 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
833 // Avoid benchmarking scaled decodes of already small images.
834 break;
835 }
836
837 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()), encoded, sampleSize);
838 }
839 fCurrentSampleSize = 0;
840 }
841
msarett7f691442015-09-22 11:56:16 -0700842 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700843 // We intend to create benchmarks that model the use cases in
844 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
845 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
846 // the image. For that reason, we will benchmark decodes in five representative locations
847 // in the image. Additionally, this use case utilizes power of two scaling, so we will
848 // test on power of two sample sizes. The output tile is always 512x512, so, when a
849 // sampleSize is used, the size of the subset that is decoded is always
850 // (sampleSize*512)x(sampleSize*512).
851 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700852 // All use cases we are aware of only scale by powers of two.
853 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
854 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800855 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700856 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800857 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700858 fSourceType = "image";
859 fBenchType = "BRD";
860
mtklein6f0ff912016-01-11 09:04:21 -0800861 const SkString& path = fImages[fCurrentBRDImage];
862 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
863 continue;
864 }
scroggo860e8a62015-10-15 07:51:28 -0700865
msarettd1227a72016-05-18 06:23:57 -0700866 while (fCurrentColorType < fColorTypes.count()) {
867 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
868 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700869
msarettd1227a72016-05-18 06:23:57 -0700870 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
871 const SkColorType colorType = fColorTypes[fCurrentColorType];
872 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
873 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -0700874
msarettd1227a72016-05-18 06:23:57 -0700875 int width = 0;
876 int height = 0;
877 if (!valid_brd_bench(encoded.get(), colorType, sampleSize, minOutputSize,
878 &width, &height)) {
879 break;
msarett7f691442015-09-22 11:56:16 -0700880 }
msarettd1227a72016-05-18 06:23:57 -0700881
882 SkString basename = SkOSPath::Basename(path.c_str());
883 SkIRect subset;
884 const uint32_t subsetSize = sampleSize * minOutputSize;
885 switch (currentSubsetType) {
886 case kTopLeft_SubsetType:
887 basename.append("_TopLeft");
888 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
889 break;
890 case kTopRight_SubsetType:
891 basename.append("_TopRight");
892 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
893 subsetSize);
894 break;
895 case kMiddle_SubsetType:
896 basename.append("_Middle");
897 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
898 (height - subsetSize) / 2, subsetSize, subsetSize);
899 break;
900 case kBottomLeft_SubsetType:
901 basename.append("_BottomLeft");
902 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
903 subsetSize);
904 break;
905 case kBottomRight_SubsetType:
906 basename.append("_BottomRight");
907 subset = SkIRect::MakeXYWH(width - subsetSize,
908 height - subsetSize, subsetSize, subsetSize);
909 break;
910 default:
911 SkASSERT(false);
912 }
913
914 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
915 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -0700916 }
msarettd1227a72016-05-18 06:23:57 -0700917 fCurrentSubsetType = 0;
918 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -0700919 }
msarettd1227a72016-05-18 06:23:57 -0700920 fCurrentSampleSize = 0;
921 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -0700922 }
msarettd1227a72016-05-18 06:23:57 -0700923 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -0700924 }
925
msarett2cee9022016-06-03 08:25:21 -0700926 while (fCurrentColorImage < fColorImages.count()) {
927 fSourceType = "colorimage";
928 fBenchType = "skcolorcodec";
929 const SkString& path = fColorImages[fCurrentColorImage];
930 fCurrentColorImage++;
931 sk_sp<SkData> encoded = SkData::MakeFromFileName(path.c_str());
932 if (encoded) {
933 return new ColorCodecBench(SkOSPath::Basename(path.c_str()).c_str(),
934 std::move(encoded));
935 } else {
936 SkDebugf("Could not read file %s.\n", path.c_str());
937 }
938 }
939
halcanary96fcdcc2015-08-27 07:41:13 -0700940 return nullptr;
mtkleine714e752014-07-31 12:13:48 -0700941 }
mtklein92007582014-08-01 07:46:52 -0700942
943 void fillCurrentOptions(ResultsWriter* log) const {
944 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700945 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700946 if (0 == strcmp(fSourceType, "skp")) {
947 log->configOption("clip",
948 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
949 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -0800950 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -0700951 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -0800952 if (fCurrentUseMPD > 0) {
953 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
954 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
955 }
mtklein92007582014-08-01 07:46:52 -0700956 }
mtklein051e56d2014-12-04 08:46:51 -0800957 if (0 == strcmp(fBenchType, "recording")) {
958 log->metric("bytes", fSKPBytes);
959 log->metric("ops", fSKPOps);
960 }
mtklein92007582014-08-01 07:46:52 -0700961 }
962
mtkleine714e752014-07-31 12:13:48 -0700963private:
msarettb23e6aa2015-06-09 13:56:10 -0700964 enum SubsetType {
965 kTopLeft_SubsetType = 0,
966 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -0700967 kMiddle_SubsetType = 2,
968 kBottomLeft_SubsetType = 3,
969 kBottomRight_SubsetType = 4,
970 kTranslate_SubsetType = 5,
971 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -0700972 kLast_SubsetType = kZoom_SubsetType,
973 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -0700974 };
975
mtkleine714e752014-07-31 12:13:48 -0700976 const BenchRegistry* fBenches;
977 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700978 SkIRect fClip;
979 SkTArray<SkScalar> fScales;
980 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -0800981 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -0800982 SkTArray<SkString> fImages;
msarett2cee9022016-06-03 08:25:21 -0700983 SkTArray<SkString> fColorImages;
msarett74deb982015-10-20 16:45:56 -0700984 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -0700985 SkScalar fZoomMax;
986 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -0700987
mtklein051e56d2014-12-04 08:46:51 -0800988 double fSKPBytes, fSKPOps;
989
mtkleinfd731ce2014-09-10 12:19:30 -0700990 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
991 const char* fBenchType; // How we bench it: micro, recording, playback, ...
992 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700993 int fCurrentScale;
994 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -0800995 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -0700996 int fCurrentCodec;
msarett84451022016-02-11 06:45:51 -0800997 int fCurrentAndroidCodec;
msarett7f691442015-09-22 11:56:16 -0700998 int fCurrentBRDImage;
msarett2cee9022016-06-03 08:25:21 -0700999 int fCurrentColorImage;
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;
msarett84451022016-02-11 06:45:51 -08001003 int fCurrentSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001004 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001005};
1006
msarettc149f0e2016-01-04 11:35:43 -08001007// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1008// This prints something every once in a while so that it knows we're still working.
1009static void start_keepalive() {
1010 struct Loop {
1011 static void forever(void*) {
1012 for (;;) {
1013 static const int kSec = 1200;
1014 #if defined(SK_BUILD_FOR_WIN)
1015 Sleep(kSec * 1000);
1016 #else
1017 sleep(kSec);
1018 #endif
1019 SkDebugf("\nBenchmarks still running...\n");
1020 }
1021 }
1022 };
1023 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1024 intentionallyLeaked->start();
1025}
1026
reedb8486f52016-05-26 14:06:43 -07001027extern bool gDefaultProfileIsSRGB;
1028
jcgregorio3b27ade2014-11-13 08:06:40 -08001029int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001030int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001031 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001032 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001033 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001034
krajcevski69a55602014-08-13 10:46:31 -07001035#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001036 GrContextOptions grContextOpts;
halcanary385fe4d2015-08-26 13:07:48 -07001037 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001038#endif
1039
reedb8486f52016-05-26 14:06:43 -07001040 if (FLAGS_forceSRGB) {
1041 gDefaultProfileIsSRGB = true;
1042 }
1043
bsalomon06cddec2014-10-24 10:40:50 -07001044 if (FLAGS_veryVerbose) {
1045 FLAGS_verbose = true;
1046 }
1047
bsalomon6eb03cc2014-08-07 14:28:50 -07001048 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001049 FLAGS_samples = 1;
1050 FLAGS_gpuFrameLag = 0;
1051 }
1052
bsalomon6eb03cc2014-08-07 14:28:50 -07001053 if (!FLAGS_writePath.isEmpty()) {
1054 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1055 if (!sk_mkdir(FLAGS_writePath[0])) {
1056 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001057 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001058 }
1059 }
1060
halcanary385fe4d2015-08-26 13:07:48 -07001061 SkAutoTDelete<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001062 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001063#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001064 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001065#else
1066 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1067 return 1;
1068#endif
mtklein60317d0f2014-07-14 11:30:37 -07001069 }
mtklein1915b622014-08-20 11:45:00 -07001070
1071 if (1 == FLAGS_properties.count() % 2) {
1072 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1073 return 1;
1074 }
1075 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1076 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1077 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001078
1079 if (1 == FLAGS_key.count() % 2) {
1080 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1081 return 1;
1082 }
1083 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001084 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001085 }
mtklein60317d0f2014-07-14 11:30:37 -07001086
mtkleinf3723212014-06-25 14:08:00 -07001087 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001088 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001089
cdaltone1b89582015-06-25 19:17:08 -07001090 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001091
bsalomon6eb03cc2014-08-07 14:28:50 -07001092 if (kAutoTuneLoops != FLAGS_loops) {
1093 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001094 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001095 SkDebugf("! -> high variance, ? -> moderate variance\n");
1096 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001097 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001098 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001099 } else {
mtkleind75c4662015-04-30 07:11:22 -07001100 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001101 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001102 }
1103
svaisanenc47635e2016-01-28 06:05:43 -08001104 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001105 create_configs(&configs);
1106
joshualitt3ebd0502016-02-09 07:18:08 -08001107#ifdef THERMAL_MANAGER_SUPPORTED
1108 int tmEnabled, tmThreshold, tmSleepTimeMs, tmTimeoutMs;
1109 if (4 != sscanf(FLAGS_useThermalManager[0], "%d,%d,%d,%d",
1110 &tmEnabled, &tmThreshold, &tmSleepTimeMs, &tmTimeoutMs)) {
1111 SkDebugf("Can't parse %s from --useThermalManager.\n", FLAGS_useThermalManager[0]);
1112 exit(1);
1113 }
1114 ThermalManager tm(tmThreshold, tmSleepTimeMs, tmTimeoutMs);
1115#endif
1116
msarettc149f0e2016-01-04 11:35:43 -08001117 if (FLAGS_keepAlive) {
1118 start_keepalive();
1119 }
1120
mtkleine070c2b2014-10-14 08:40:43 -07001121 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001122 BenchmarkStream benchStream;
1123 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001124 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001125 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001126 continue;
1127 }
1128
svaisanenc47635e2016-01-28 06:05:43 -08001129 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001130 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001131 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001132 }
egdaniel3bf92062015-06-26 08:12:46 -07001133 for (int i = 0; i < configs.count(); ++i) {
joshualitt3ebd0502016-02-09 07:18:08 -08001134#ifdef THERMAL_MANAGER_SUPPORTED
1135 if (tmEnabled && !tm.coolOffIfNecessary()) {
1136 SkDebugf("Could not cool off, timings will be throttled\n");
1137 }
1138#endif
egdaniel3bf92062015-06-26 08:12:46 -07001139 Target* target = is_enabled(b, configs[i]);
1140 if (!target) {
1141 continue;
1142 }
mtkleinf3723212014-06-25 14:08:00 -07001143
halcanary96fcdcc2015-08-27 07:41:13 -07001144 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001145 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001146 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001147
brianosman7a5ada82016-02-08 13:49:12 -08001148 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001149 SkDebugf("Running %s\t%s\n"
1150 , bench->getUniqueName()
1151 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001152 if (FLAGS_dryRun) {
1153 continue;
1154 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001155 }
1156
egdaniel3bf92062015-06-26 08:12:46 -07001157 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001158 bench->perCanvasPreDraw(canvas);
1159
cdaltone1b89582015-06-25 19:17:08 -07001160 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001161 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001162 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1163 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001164
mtkleinbbba1682015-10-28 11:36:30 -07001165 if (FLAGS_ms) {
1166 samples.reset();
1167 auto stop = now_ms() + FLAGS_ms;
1168 do {
1169 samples.push_back(time(loops, bench, target) / loops);
1170 } while (now_ms() < stop);
1171 } else {
cdaltone1b89582015-06-25 19:17:08 -07001172 samples.reset(FLAGS_samples);
1173 for (int s = 0; s < FLAGS_samples; s++) {
egdaniel3bf92062015-06-26 08:12:46 -07001174 samples[s] = time(loops, bench, target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001175 }
cdaltone1b89582015-06-25 19:17:08 -07001176 }
mtkleinf3723212014-06-25 14:08:00 -07001177
joshualitte45c81c2015-12-02 09:05:37 -08001178#if SK_SUPPORT_GPU
1179 SkTArray<SkString> keys;
1180 SkTArray<double> values;
1181 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1182 if (gpuStatsDump) {
1183 // TODO cache stats
1184 bench->getGpuStats(canvas, &keys, &values);
1185 }
1186#endif
1187
robertphillips5b693772014-11-21 06:19:36 -08001188 bench->perCanvasPostDraw(canvas);
1189
egdaniel3bf92062015-06-26 08:12:46 -07001190 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001191 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001192 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001193 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001194 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001195 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001196 }
1197
1198 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001199 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001200 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001201 continue;
1202 }
1203
cdaltone1b89582015-06-25 19:17:08 -07001204 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001205 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001206 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001207 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001208 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001209 log->metric("min_ms", stats.min);
joshualitte45c81c2015-12-02 09:05:37 -08001210#if SK_SUPPORT_GPU
1211 if (gpuStatsDump) {
1212 // dump to json, only SKPBench currently returns valid keys / values
1213 SkASSERT(keys.count() == values.count());
1214 for (int i = 0; i < keys.count(); i++) {
1215 log->metric(keys[i].c_str(), values[i]);
1216 }
1217 }
1218#endif
1219
mtkleine070c2b2014-10-14 08:40:43 -07001220 if (runs++ % FLAGS_flushEvery == 0) {
1221 log->flush();
1222 }
mtklein60317d0f2014-07-14 11:30:37 -07001223
bsalomon6eb03cc2014-08-07 14:28:50 -07001224 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001225 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001226 config = ""; // Only print the config if we run the same bench on more than one.
1227 }
mtkleind75c4662015-04-30 07:11:22 -07001228 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1229 , sk_tools::getCurrResidentSetSizeMB()
1230 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001231 , bench->getUniqueName()
1232 , config);
mtkleinf3723212014-06-25 14:08:00 -07001233 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001234 const char* mark = " ";
1235 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1236 if (stddev_percent > 5) mark = "?";
1237 if (stddev_percent > 10) mark = "!";
1238
1239 SkDebugf("%10.2f %s\t%s\t%s\n",
1240 stats.median*1e3, mark, bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001241 } else {
1242 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001243 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1244 , sk_tools::getCurrResidentSetSizeMB()
1245 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001246 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001247 , HUMANIZE(stats.min)
1248 , HUMANIZE(stats.median)
1249 , HUMANIZE(stats.mean)
1250 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001251 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001252 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001253 , config
mtklein96289052014-09-10 12:05:59 -07001254 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001255 );
1256 }
joshualitte45c81c2015-12-02 09:05:37 -08001257
bsalomonb12ea412015-02-02 21:19:50 -08001258#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001259 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001260 GrContext* context = gGrFactory->get(configs[i].ctxType,
kkinnunen3e980c32015-12-23 01:33:00 -08001261 configs[i].ctxOptions);
kkinnunen5219fd92015-12-10 06:28:13 -08001262 context->printCacheStats();
1263 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001264 }
1265#endif
joshualitte45c81c2015-12-02 09:05:37 -08001266
cdalton2c56ba52015-06-26 13:32:53 -07001267 if (FLAGS_verbose) {
1268 SkDebugf("Samples: ");
1269 for (int i = 0; i < samples.count(); i++) {
1270 SkDebugf("%s ", HUMANIZE(samples[i]));
1271 }
1272 SkDebugf("%s\n", bench->getUniqueName());
1273 }
egdaniel3bf92062015-06-26 08:12:46 -07001274 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001275 }
mtkleinf3723212014-06-25 14:08:00 -07001276 }
1277
mtkleine1091452014-12-04 10:47:02 -08001278 log->bench("memory_usage", 0,0);
1279 log->config("meta");
1280 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1281
joshualitte0b19d42015-03-26 10:41:02 -07001282#if SK_SUPPORT_GPU
1283 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1284 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001285 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001286#endif
1287
mtkleinf3723212014-06-25 14:08:00 -07001288 return 0;
1289}
1290
jcgregorio3b27ade2014-11-13 08:06:40 -08001291#if !defined SK_BUILD_FOR_IOS
1292int main(int argc, char** argv) {
1293 SkCommandLineFlags::Parse(argc, argv);
1294 return nanobench_main();
1295}
1296#endif