blob: 5db5137ffd6f1f288da6e607bf3919586a38aa10 [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"
fmalita6519c212016-09-14 08:05:17 -070043#include "SkSVGDOM.h"
robertphillips5b693772014-11-21 06:19:36 -080044#include "SkTaskGroup.h"
msarettc149f0e2016-01-04 11:35:43 -080045#include "SkThreadUtils.h"
joshualitt3ebd0502016-02-09 07:18:08 -080046#include "ThermalManager.h"
mtkleinf3723212014-06-25 14:08:00 -070047
bungeman60e0fee2015-08-26 05:15:46 -070048#include <stdlib.h>
49
scroggo38ce0a72016-01-07 07:28:47 -080050#ifndef SK_BUILD_FOR_WIN32
51 #include <unistd.h>
52#endif
53
tomhudsond968a6f2015-03-26 11:28:06 -070054#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
55 #include "nanobenchAndroid.h"
56#endif
57
mtkleinbb6a0282014-07-01 08:43:42 -070058#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070059 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070060 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070061 #include "GrContextFactory.h"
bsalomon3724e572016-03-30 18:56:19 -070062 #include "gl/GrGLUtil.h"
63 using sk_gpu_test::GrContextFactory;
bsalomonc8699322016-05-11 11:55:36 -070064 using sk_gpu_test::TestContext;
krajcevski69a55602014-08-13 10:46:31 -070065 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070066#endif
67
bsalomon682c2692015-05-22 14:01:46 -070068 struct GrContextOptions;
69
mtkleinf3723212014-06-25 14:08:00 -070070__SK_FORCE_IMAGE_DECODER_LINKING;
71
reed53249782014-10-10 09:09:52 -070072static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070073
mtkleinb5110422014-08-07 15:20:02 -070074static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070075#ifdef SK_DEBUG
76 1;
mtkleina189ccd2014-07-14 12:28:47 -070077#else
bsalomon6eb03cc2014-08-07 14:28:50 -070078 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070079#endif
80
bsalomon6eb03cc2014-08-07 14:28:50 -070081static SkString loops_help_txt() {
82 SkString help;
83 help.printf("Number of times to run each bench. Set this to %d to auto-"
84 "tune for each bench. Timings are only reported when auto-tuning.",
85 kAutoTuneLoops);
86 return help;
87}
88
cdaltone1b89582015-06-25 19:17:08 -070089static SkString to_string(int n) {
90 SkString str;
91 str.appendS32(n);
92 return str;
93}
94
bsalomon6eb03cc2014-08-07 14:28:50 -070095DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
96
mtkleinf3723212014-06-25 14:08:00 -070097DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -070098DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -070099DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
100DEFINE_double(overheadGoal, 0.0001,
101 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -0700102DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -0700103DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -0700104
mtklein60317d0f2014-07-14 11:30:37 -0700105DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -0700106DEFINE_int32(maxCalibrationAttempts, 3,
107 "Try up to this many times to guess loops for a bench, or skip the bench.");
108DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700109DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
110DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700111DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
112 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700113DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
mtklein8c1a4f82016-08-08 06:56:22 -0700114DEFINE_bool(lite, false, "Use SkLiteRecorder in recording benchmarks?");
robertphillips5b693772014-11-21 06:19:36 -0800115DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700116DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700117DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800118DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800119DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800120DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
msarettc149f0e2016-01-04 11:35:43 -0800121DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
joshualitt3ebd0502016-02-09 07:18:08 -0800122DEFINE_string(useThermalManager, "0,1,10,1000", "enabled,threshold,sleepTimeMs,TimeoutMs for "
123 "thermalManager\n");
mtklein92007582014-08-01 07:46:52 -0700124
mtklein65dfd2f2016-02-03 10:40:54 -0800125DEFINE_string(sourceType, "",
126 "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
127DEFINE_string(benchType, "",
128 "Apply usual --match rules to bench type: micro, recording, playback, skcodec, etc.");
129
mtkleinbbba1682015-10-28 11:36:30 -0700130static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
131
mtkleinf3723212014-06-25 14:08:00 -0700132static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700133 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800134 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700135}
mtklein55b0ffc2014-07-17 08:38:23 -0700136#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700137
tomhudsond968a6f2015-03-26 11:28:06 -0700138bool Target::init(SkImageInfo info, Benchmark* bench) {
139 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700140 this->surface = SkSurface::MakeRaster(info);
141 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700142 return false;
143 }
144 }
145 return true;
146}
147bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700148 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700149 if (!canvas) {
150 return false;
151 }
152 bmp->setInfo(canvas->imageInfo());
153 if (!canvas->readPixels(bmp, 0, 0)) {
154 SkDebugf("Can't read canvas pixels.\n");
155 return false;
156 }
157 return true;
158}
159
160#if SK_SUPPORT_GPU
161struct GPUTarget : public Target {
bsalomonc8699322016-05-11 11:55:36 -0700162 explicit GPUTarget(const Config& c) : Target(c), context(nullptr) { }
163 TestContext* context;
tomhudsond968a6f2015-03-26 11:28:06 -0700164
165 void setup() override {
bsalomonc8699322016-05-11 11:55:36 -0700166 this->context->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700167 // Make sure we're done with whatever came before.
bsalomonc8699322016-05-11 11:55:36 -0700168 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700169 }
170 void endTiming() override {
bsalomonc8699322016-05-11 11:55:36 -0700171 if (this->context) {
172 this->context->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700173 }
174 }
175 void fence() override {
bsalomonc8699322016-05-11 11:55:36 -0700176 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700177 }
mtkleind75c4662015-04-30 07:11:22 -0700178
cdaltond416a5b2015-06-23 13:23:44 -0700179 bool needsFrameTiming(int* maxFrameLag) const override {
bsalomonc8699322016-05-11 11:55:36 -0700180 if (!this->context->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700181 // Frame lag is unknown.
182 *maxFrameLag = FLAGS_gpuFrameLag;
183 }
184 return true;
185 }
tomhudsond968a6f2015-03-26 11:28:06 -0700186 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700187 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
188 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700189 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700190 this->surface = SkSurface::MakeRenderTarget(gGrFactory->get(this->config.ctxType,
191 this->config.ctxOptions),
bsalomon5ec26ae2016-02-25 08:33:02 -0800192 SkBudgeted::kNo, info,
reede8f30622016-03-23 18:59:25 -0700193 this->config.samples, &props);
bsalomonc8699322016-05-11 11:55:36 -0700194 this->context = gGrFactory->getContextInfo(this->config.ctxType,
195 this->config.ctxOptions).testContext();
tomhudsond968a6f2015-03-26 11:28:06 -0700196 if (!this->surface.get()) {
197 return false;
198 }
bsalomonc8699322016-05-11 11:55:36 -0700199 if (!this->context->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700200 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800201 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700202 }
tomhudsond968a6f2015-03-26 11:28:06 -0700203 return true;
204 }
205 void fillOptions(ResultsWriter* log) override {
206 const GrGLubyte* version;
bsalomonc8699322016-05-11 11:55:36 -0700207 if (this->context->backend() == kOpenGL_GrBackend) {
208 const GrGLInterface* gl =
209 reinterpret_cast<const GrGLInterface*>(this->context->backendContext());
210 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
211 log->configOption("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700212
bsalomonc8699322016-05-11 11:55:36 -0700213 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
214 log->configOption("GL_RENDERER", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700215
bsalomonc8699322016-05-11 11:55:36 -0700216 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VENDOR));
217 log->configOption("GL_VENDOR", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700218
bsalomonc8699322016-05-11 11:55:36 -0700219 GR_GL_CALL_RET(gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
220 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
221 }
tomhudsond968a6f2015-03-26 11:28:06 -0700222 }
223};
mtkleind75c4662015-04-30 07:11:22 -0700224
tomhudsond968a6f2015-03-26 11:28:06 -0700225#endif
226
tomhudson75a0ebb2015-03-27 12:11:44 -0700227static double time(int loops, Benchmark* bench, Target* target) {
228 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700229 if (canvas) {
230 canvas->clear(SK_ColorWHITE);
231 }
joshualitt8a6697a2015-09-30 12:11:07 -0700232 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700233 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700234 canvas = target->beginTiming(canvas);
235 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700236 if (canvas) {
237 canvas->flush();
238 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700239 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700240 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700241 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700242 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700243}
244
mtkleinf3723212014-06-25 14:08:00 -0700245static double estimate_timer_overhead() {
246 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700247 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700248 double start = now_ms();
249 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700250 }
251 return overhead / FLAGS_overheadLoops;
252}
253
reed53249782014-10-10 09:09:52 -0700254static int detect_forever_loops(int loops) {
255 // look for a magic run-forever value
256 if (loops < 0) {
257 loops = SK_MaxS32;
258 }
259 return loops;
260}
261
mtklein55b0ffc2014-07-17 08:38:23 -0700262static int clamp_loops(int loops) {
263 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800264 SkDebugf("ERROR: clamping loops from %d to 1. "
265 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700266 return 1;
267 }
268 if (loops > FLAGS_maxLoops) {
269 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
270 return FLAGS_maxLoops;
271 }
272 return loops;
273}
274
tomhudsond968a6f2015-03-26 11:28:06 -0700275static bool write_canvas_png(Target* target, const SkString& filename) {
276
bsalomon6eb03cc2014-08-07 14:28:50 -0700277 if (filename.isEmpty()) {
278 return false;
279 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700280 if (target->getCanvas() &&
281 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700282 return false;
283 }
tomhudsond968a6f2015-03-26 11:28:06 -0700284
bsalomon6eb03cc2014-08-07 14:28:50 -0700285 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700286
287 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700288 return false;
289 }
tomhudsond968a6f2015-03-26 11:28:06 -0700290
bsalomon6eb03cc2014-08-07 14:28:50 -0700291 SkString dir = SkOSPath::Dirname(filename.c_str());
292 if (!sk_mkdir(dir.c_str())) {
293 SkDebugf("Can't make dir %s.\n", dir.c_str());
294 return false;
295 }
296 SkFILEWStream stream(filename.c_str());
297 if (!stream.isValid()) {
298 SkDebugf("Can't write %s.\n", filename.c_str());
299 return false;
300 }
301 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
302 SkDebugf("Can't encode a PNG.\n");
303 return false;
304 }
305 return true;
306}
307
308static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700309static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700310 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700311 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700312 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700313 int loops = bench->calculateLoops(FLAGS_loops);
314 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700315 while (bench_plus_overhead < overhead) {
316 if (round++ == FLAGS_maxCalibrationAttempts) {
317 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700318 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700319 return kFailedLoops;
320 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700321 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700322 }
mtklein2069e222014-08-04 13:57:39 -0700323 }
mtkleinf3723212014-06-25 14:08:00 -0700324
mtkleinbb6a0282014-07-01 08:43:42 -0700325 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700326 // We'll pick N to make timer overhead negligible:
327 //
mtkleinbb6a0282014-07-01 08:43:42 -0700328 // overhead
329 // ------------------------- < FLAGS_overheadGoal
330 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700331 //
mtkleinbb6a0282014-07-01 08:43:42 -0700332 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700333 //
334 // Doing some math, we get:
335 //
mtkleinbb6a0282014-07-01 08:43:42 -0700336 // (overhead / FLAGS_overheadGoal) - overhead
337 // ------------------------------------------ < N
338 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700339 //
340 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700341 if (kAutoTuneLoops == loops) {
342 const double numer = overhead / FLAGS_overheadGoal - overhead;
343 const double denom = bench_plus_overhead - overhead;
344 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700345 loops = clamp_loops(loops);
346 } else {
347 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700348 }
mtkleinbb6a0282014-07-01 08:43:42 -0700349
mtkleinbb6a0282014-07-01 08:43:42 -0700350 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700351}
352
cdaltone1b89582015-06-25 19:17:08 -0700353static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700354 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700355 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700356 if (kAutoTuneLoops == loops) {
357 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700358 double elapsed = 0;
359 do {
mtklein527930f2014-11-06 08:04:34 -0800360 if (1<<30 == loops) {
361 // We're about to wrap. Something's wrong with the bench.
362 loops = 0;
363 break;
364 }
mtkleina189ccd2014-07-14 12:28:47 -0700365 loops *= 2;
366 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700367 // _this_ round, not still timing last round.
368 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700369 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700370 }
371 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700372
mtkleina189ccd2014-07-14 12:28:47 -0700373 // We've overshot at least a little. Scale back linearly.
374 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700375 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700376
tomhudsond968a6f2015-03-26 11:28:06 -0700377 // Make sure we're not still timing our calibration.
378 target->fence();
reed53249782014-10-10 09:09:52 -0700379 } else {
380 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700381 }
mtkleinbb6a0282014-07-01 08:43:42 -0700382
383 // Pretty much the same deal as the calibration: do some warmup to make
384 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700385 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700386 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700387 }
mtkleinbb6a0282014-07-01 08:43:42 -0700388
mtkleinbb6a0282014-07-01 08:43:42 -0700389 return loops;
390}
mtkleinbb6a0282014-07-01 08:43:42 -0700391
bsalomonc2553372014-07-22 13:09:05 -0700392#if SK_SUPPORT_GPU
bsalomon85b4b532016-04-05 11:06:27 -0700393#define kBogusContextType GrContextFactory::kNativeGL_ContextType
csmartdalton6270e552016-09-13 10:41:49 -0700394#define kBogusContextOptions GrContextFactory::ContextOptions::kNone
bsalomonc2553372014-07-22 13:09:05 -0700395#else
bsalomon85b4b532016-04-05 11:06:27 -0700396#define kBogusContextType 0
397#define kBogusContextOptions 0
mtkleine714e752014-07-31 12:13:48 -0700398#endif
bsalomonc2553372014-07-22 13:09:05 -0700399
svaisanenc47635e2016-01-28 06:05:43 -0800400static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
401
402#if SK_SUPPORT_GPU
403 if (const auto* gpuConfig = config->asConfigGpu()) {
404 if (!FLAGS_gpu)
405 return;
406
svaisanenc47635e2016-01-28 06:05:43 -0800407 const auto ctxType = gpuConfig->getContextType();
csmartdalton6270e552016-09-13 10:41:49 -0700408 const auto ctxOptions = gpuConfig->getContextOptions();
svaisanenc47635e2016-01-28 06:05:43 -0800409 const auto sampleCount = gpuConfig->getSamples();
410
411 if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOptions)) {
412 const auto maxSampleCount = ctx->caps()->maxSampleCount();
413 if (sampleCount > ctx->caps()->maxSampleCount()) {
414 SkDebugf("Configuration sample count %d exceeds maximum %d.\n",
415 sampleCount, maxSampleCount);
416 return;
417 }
418 } else {
419 SkDebugf("No context was available matching config type and options.\n");
420 return;
421 }
422
423 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800424 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800425 Benchmark::kGPU_Backend,
brianosman1a48e132016-06-20 09:41:41 -0700426 gpuConfig->getColorType(),
svaisanenc47635e2016-01-28 06:05:43 -0800427 kPremul_SkAlphaType,
brianosman1a48e132016-06-20 09:41:41 -0700428 sk_ref_sp(gpuConfig->getColorSpace()),
svaisanenc47635e2016-01-28 06:05:43 -0800429 sampleCount,
430 ctxType,
kkinnunend4e1c352016-03-01 23:41:26 -0800431 ctxOptions,
432 gpuConfig->getUseDIText()
433 };
svaisanenc47635e2016-01-28 06:05:43 -0800434
435 configs->push_back(target);
436 return;
437 }
438#endif
439
brianosmanb109b8c2016-06-16 13:03:24 -0700440 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
441 if (config->getTag().equals(#name)) { \
442 Config config = { \
443 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
444 0, kBogusContextType, kBogusContextOptions, false \
445 }; \
446 configs->push_back(config); \
447 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700448 }
mtkleine714e752014-07-31 12:13:48 -0700449
mtklein40b32be2014-07-09 08:46:49 -0700450 if (FLAGS_cpu) {
mtkleinbb6c41b2016-03-08 11:31:11 -0800451 CPU_CONFIG(nonrendering, kNonRendering_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700452 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800453
454 CPU_CONFIG(8888, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700455 kN32_SkColorType, kPremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800456 CPU_CONFIG(565, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700457 kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
458 auto srgbColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
mtkleinbb6c41b2016-03-08 11:31:11 -0800459 CPU_CONFIG(srgb, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700460 kN32_SkColorType, kPremul_SkAlphaType, srgbColorSpace)
mtkleinbb6c41b2016-03-08 11:31:11 -0800461 CPU_CONFIG(f16, kRaster_Backend,
brianosman0e22eb82016-08-30 07:07:59 -0700462 kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbColorSpace->makeLinearGamma())
mtklein40b32be2014-07-09 08:46:49 -0700463 }
mtkleinbb6a0282014-07-01 08:43:42 -0700464
svaisanenc47635e2016-01-28 06:05:43 -0800465 #undef CPU_CONFIG
tomhudsond968a6f2015-03-26 11:28:06 -0700466
467#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
djsollen817c72a2016-01-28 13:57:02 -0800468 if (config->getTag().equals("hwui")) {
mtkleinbb6c41b2016-03-08 11:31:11 -0800469 Config config = { SkString("hwui"), Benchmark::kHWUI_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700470 kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr,
bsalomon85b4b532016-04-05 11:06:27 -0700471 0, kBogusContextType, kBogusContextOptions, false };
svaisanenc47635e2016-01-28 06:05:43 -0800472 configs->push_back(config);
tomhudsond968a6f2015-03-26 11:28:06 -0700473 }
474#endif
mtkleinf3723212014-06-25 14:08:00 -0700475}
476
svaisanenc47635e2016-01-28 06:05:43 -0800477// Append all configs that are enabled and supported.
478void create_configs(SkTArray<Config>* configs) {
479 SkCommandLineConfigArray array;
480 ParseConfigs(FLAGS_config, &array);
481 for (int i = 0; i < array.count(); ++i) {
482 create_config(array[i], configs);
483 }
484}
485
brianosman9f1f6e22016-09-15 08:33:02 -0700486// disable warning : switch statement contains default but no 'case' labels
487#if defined _WIN32
488#pragma warning ( push )
489#pragma warning ( disable : 4065 )
490#endif
491
halcanary96fcdcc2015-08-27 07:41:13 -0700492// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700493static Target* is_enabled(Benchmark* bench, const Config& config) {
494 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700495 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700496 }
497
reede5ea5002014-09-03 11:54:58 -0700498 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700499 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700500
halcanary96fcdcc2015-08-27 07:41:13 -0700501 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700502
tomhudsond968a6f2015-03-26 11:28:06 -0700503 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700504#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700505 case Benchmark::kGPU_Backend:
506 target = new GPUTarget(config);
507 break;
bsalomonc2553372014-07-22 13:09:05 -0700508#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700509#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
510 case Benchmark::kHWUI_Backend:
511 target = new HWUITarget(config, bench);
512 break;
513#endif
514 default:
515 target = new Target(config);
516 break;
517 }
bsalomonc2553372014-07-22 13:09:05 -0700518
tomhudsond968a6f2015-03-26 11:28:06 -0700519 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700520 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700521 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700522 }
523 return target;
524}
525
brianosman9f1f6e22016-09-15 08:33:02 -0700526#if defined _WIN32
527#pragma warning ( pop )
528#endif
529
reed42943c82016-09-12 12:01:44 -0700530static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700531 uint32_t minOutputSize, int* width, int* height) {
msarett5cb48852015-11-06 08:56:32 -0800532 SkAutoTDelete<SkBitmapRegionDecoder> brd(
msarettd1227a72016-05-18 06:23:57 -0700533 SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy));
msarett7f691442015-09-22 11:56:16 -0700534 if (nullptr == brd.get()) {
535 // This is indicates that subset decoding is not supported for a particular image format.
536 return false;
537 }
538
msarett7f691442015-09-22 11:56:16 -0700539 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
540 (uint32_t) brd->height()) {
541 // This indicates that the image is not large enough to decode a
542 // minOutputSize x minOutputSize subset at the given sampleSize.
543 return false;
544 }
545
546 // Set the image width and height. The calling code will use this to choose subsets to decode.
547 *width = brd->width();
548 *height = brd->height();
549 return true;
550}
551
egdaniel3bf92062015-06-26 08:12:46 -0700552static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700553 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700554#if SK_SUPPORT_GPU
555 if (FLAGS_abandonGpuContext) {
556 gGrFactory->abandonContexts();
557 }
558 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
559 gGrFactory->destroyContexts();
560 }
561#endif
562}
563
fmalita6519c212016-09-14 08:05:17 -0700564static void collect_files(const SkCommandLineFlags::StringArray& paths, const char* ext,
565 SkTArray<SkString>* list) {
566 for (int i = 0; i < paths.count(); ++i) {
567 if (SkStrEndsWith(paths[i], ext)) {
568 list->push_back(SkString(paths[i]));
569 } else {
570 SkOSFile::Iter it(paths[i], ext);
571 SkString path;
572 while (it.next(&path)) {
573 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
574 }
575 }
576 }
577}
578
mtkleine714e752014-07-31 12:13:48 -0700579class BenchmarkStream {
580public:
mtklein92007582014-08-01 07:46:52 -0700581 BenchmarkStream() : fBenches(BenchRegistry::Head())
582 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700583 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700584 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800585 , fCurrentSKP(0)
fmalita6519c212016-09-14 08:05:17 -0700586 , fCurrentSVG(0)
msarett95f192d2015-02-13 09:05:41 -0800587 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700588 , fCurrentCodec(0)
msarett84451022016-02-11 06:45:51 -0800589 , fCurrentAndroidCodec(0)
msarett7f691442015-09-22 11:56:16 -0700590 , fCurrentBRDImage(0)
msarett2cee9022016-06-03 08:25:21 -0700591 , fCurrentColorImage(0)
msarett95f192d2015-02-13 09:05:41 -0800592 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800593 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700594 , fCurrentSubsetType(0)
msarett84451022016-02-11 06:45:51 -0800595 , fCurrentSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700596 , fCurrentAnimSKP(0) {
fmalita6519c212016-09-14 08:05:17 -0700597 collect_files(FLAGS_skps, ".skp", &fSKPs);
598 collect_files(FLAGS_svgs, ".svg", &fSVGs);
mtkleine714e752014-07-31 12:13:48 -0700599
mtklein92007582014-08-01 07:46:52 -0700600 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
601 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
602 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
603 exit(1);
604 }
605
606 for (int i = 0; i < FLAGS_scales.count(); i++) {
607 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
608 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
609 exit(1);
610 }
611 }
robertphillips5b693772014-11-21 06:19:36 -0800612
cdalton63a82852015-06-29 14:06:10 -0700613 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
614 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700615 exit(1);
616 }
617
robertphillips5b693772014-11-21 06:19:36 -0800618 if (FLAGS_mpd) {
619 fUseMPDs.push_back() = true;
620 }
mtkleinc751ecb2015-06-15 08:56:38 -0700621 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700622
msarett95f192d2015-02-13 09:05:41 -0800623 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700624 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800625 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800626 }
msarett2cee9022016-06-03 08:25:21 -0700627 if (!CollectImages(FLAGS_colorImages, &fColorImages)) {
628 exit(1);
629 }
mtklein95553d92015-03-12 08:24:21 -0700630
msarett95f192d2015-02-13 09:05:41 -0800631 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700632 fColorTypes.push_back(kN32_SkColorType);
633 if (!FLAGS_simpleCodec) {
634 fColorTypes.push_back(kRGB_565_SkColorType);
635 fColorTypes.push_back(kAlpha_8_SkColorType);
636 fColorTypes.push_back(kIndex_8_SkColorType);
637 fColorTypes.push_back(kGray_8_SkColorType);
638 }
mtklein92007582014-08-01 07:46:52 -0700639 }
640
reedca2622b2016-03-18 07:25:55 -0700641 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700642 // Not strictly necessary, as it will be checked again later,
643 // but helps to avoid a lot of pointless work if we're going to skip it.
cdalton91e457d2016-02-17 11:10:16 -0800644 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700645 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700646 }
647
scroggoa1193e42015-01-21 12:09:53 -0800648 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -0700649 if (stream.get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700650 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700651 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700652 }
653
reedca2622b2016-03-18 07:25:55 -0700654 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700655 }
656
fmalita6519c212016-09-14 08:05:17 -0700657 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
658 SkFILEStream stream(path);
659 if (!stream.isValid()) {
660 SkDebugf("Could not read %s.\n", path);
661 return nullptr;
662 }
663
fmalitae1baa7c2016-09-14 12:04:30 -0700664 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
fmalita6519c212016-09-14 08:05:17 -0700665 if (!svgDom) {
666 SkDebugf("Could not parse %s.\n", path);
667 return nullptr;
668 }
669
fmalitae1baa7c2016-09-14 12:04:30 -0700670 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
671 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
672 if (svgDom->containerSize().isEmpty()) {
673 svgDom->setContainerSize(kDefaultContainerSize);
674 }
675
fmalita6519c212016-09-14 08:05:17 -0700676 SkPictureRecorder recorder;
fmalitae1baa7c2016-09-14 12:04:30 -0700677 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
678 svgDom->containerSize().height()));
fmalita6519c212016-09-14 08:05:17 -0700679 return recorder.finishRecordingAsPicture();
680 }
681
mtklein92007582014-08-01 07:46:52 -0700682 Benchmark* next() {
mtklein65dfd2f2016-02-03 10:40:54 -0800683 SkAutoTDelete<Benchmark> bench;
684 do {
685 bench.reset(this->rawNext());
686 if (!bench) {
687 return nullptr;
688 }
689 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
690 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700691 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800692 }
693
694 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700695 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700696 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700697 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700698 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700699 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700700 return bench;
701 }
mtklein92007582014-08-01 07:46:52 -0700702
mtkleine714e752014-07-31 12:13:48 -0700703 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -0700704 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700705 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800706 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700707 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700708 fBenchType = "micro";
mtklein18300a32016-03-16 13:53:35 -0700709 return new GMBench(gm.release());
mtkleine714e752014-07-31 12:13:48 -0700710 }
711 }
mtklein92007582014-08-01 07:46:52 -0700712
mtkleinfd731ce2014-09-10 12:19:30 -0700713 // First add all .skps as RecordingBenches.
714 while (fCurrentRecording < fSKPs.count()) {
715 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700716 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
717 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700718 continue;
719 }
720 SkString name = SkOSPath::Basename(path.c_str());
721 fSourceType = "skp";
722 fBenchType = "recording";
reedca2622b2016-03-18 07:25:55 -0700723 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic.get()));
mtklein051e56d2014-12-04 08:46:51 -0800724 fSKPOps = pic->approximateOpCount();
mtklein8c1a4f82016-08-08 06:56:22 -0700725 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh, FLAGS_lite);
mtkleinfd731ce2014-09-10 12:19:30 -0700726 }
727
728 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700729 while (fCurrentScale < fScales.count()) {
730 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800731 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700732 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
733 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800734 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700735 continue;
736 }
robertphillips5b693772014-11-21 06:19:36 -0800737
738 while (fCurrentUseMPD < fUseMPDs.count()) {
739 if (FLAGS_bbh) {
740 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
741 SkRTreeFactory factory;
742 SkPictureRecorder recorder;
robertphillips5b693772014-11-21 06:19:36 -0800743 pic->playback(recorder.beginRecording(pic->cullRect().width(),
744 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800745 &factory,
robertphillipsdda54452016-07-13 13:27:16 -0700746 0));
reedca2622b2016-03-18 07:25:55 -0700747 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800748 }
749 SkString name = SkOSPath::Basename(path.c_str());
750 fSourceType = "skp";
751 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700752 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
753 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700754 }
robertphillips5b693772014-11-21 06:19:36 -0800755 fCurrentUseMPD = 0;
756 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700757 }
fmalita6519c212016-09-14 08:05:17 -0700758
759 while (fCurrentSVG++ < fSVGs.count()) {
760 const char* path = fSVGs[fCurrentSVG - 1].c_str();
761 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
762 fSourceType = "svg";
763 fBenchType = "playback";
764 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
765 fScales[fCurrentScale], false, FLAGS_loopSKP);
766 }
767 }
768
mtklein92007582014-08-01 07:46:52 -0700769 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700770 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700771 fCurrentScale++;
772 }
773
joshualitt261c3ad2015-04-27 09:16:57 -0700774 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700775 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700776 while (fCurrentAnimSKP < fSKPs.count()) {
777 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700778 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
779 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700780 fCurrentAnimSKP++;
781 continue;
782 }
783
784 fCurrentAnimSKP++;
785 SkString name = SkOSPath::Basename(path.c_str());
cdalton63a82852015-06-29 14:06:10 -0700786 SkAutoTUnref<SKPAnimationBench::Animation> animation(
787 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
halcanary385fe4d2015-08-26 13:07:48 -0700788 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation,
789 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700790 }
791 }
792
scroggo60869a42015-04-01 12:09:17 -0700793 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700794 fSourceType = "image";
795 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700796 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800797 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
798 continue;
799 }
bungeman38d909e2016-08-02 14:40:46 -0700800 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
reed42943c82016-09-12 12:01:44 -0700801 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700802 if (!codec) {
803 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700804 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700805 continue;
806 }
scroggo21027992015-04-02 13:22:38 -0700807
scroggo60869a42015-04-01 12:09:17 -0700808 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700809 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700810
msarettc7796b92016-01-07 14:20:20 -0800811 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700812 if (FLAGS_simpleCodec) {
813 if (kUnpremul_SkAlphaType == alphaType) {
814 alphaType = kPremul_SkAlphaType;
815 }
816
817 fCurrentColorType++;
818 } else {
819 switch (alphaType) {
820 case kOpaque_SkAlphaType:
821 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800822 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700823 break;
824 case kUnpremul_SkAlphaType:
825 case kPremul_SkAlphaType:
826 if (0 == fCurrentAlphaType) {
827 // Test unpremul first.
828 alphaType = kUnpremul_SkAlphaType;
829 fCurrentAlphaType++;
830 } else {
831 // Test premul.
832 alphaType = kPremul_SkAlphaType;
833 fCurrentAlphaType = 0;
834 fCurrentColorType++;
835 }
836 break;
837 default:
838 SkASSERT(false);
839 fCurrentColorType++;
840 break;
841 }
scroggo21027992015-04-02 13:22:38 -0700842 }
843
msarettc7796b92016-01-07 14:20:20 -0800844 // Make sure we can decode to this color type and alpha type.
845 SkImageInfo info =
846 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700847 const size_t rowBytes = info.minRowBytes();
848 SkAutoMalloc storage(info.getSafeSize(rowBytes));
849
850 // Used if fCurrentColorType is kIndex_8_SkColorType
851 int colorCount = 256;
852 SkPMColor colors[256];
853
scroggoeb602a52015-07-09 08:16:03 -0700854 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700855 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700856 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700857 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700858 case SkCodec::kSuccess:
859 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700860 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700861 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700862 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700863 // This is okay. Not all conversions are valid.
864 break;
scroggo60869a42015-04-01 12:09:17 -0700865 default:
866 // This represents some sort of failure.
867 SkASSERT(false);
868 break;
869 }
870 }
871 fCurrentColorType = 0;
872 }
873
msarett84451022016-02-11 06:45:51 -0800874 // Run AndroidCodecBenches
875 const int sampleSizes[] = { 2, 4, 8 };
876 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
877 fSourceType = "image";
878 fBenchType = "skandroidcodec";
879
880 const SkString& path = fImages[fCurrentAndroidCodec];
881 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
882 continue;
883 }
bungeman38d909e2016-08-02 14:40:46 -0700884 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
reed42943c82016-09-12 12:01:44 -0700885 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800886 if (!codec) {
887 // Nothing to time.
888 SkDebugf("Cannot find codec for %s\n", path.c_str());
889 continue;
890 }
891
892 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
893 int sampleSize = sampleSizes[fCurrentSampleSize];
894 fCurrentSampleSize++;
895 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
896 // Avoid benchmarking scaled decodes of already small images.
897 break;
898 }
899
bungeman38d909e2016-08-02 14:40:46 -0700900 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
901 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -0800902 }
903 fCurrentSampleSize = 0;
904 }
905
msarett7f691442015-09-22 11:56:16 -0700906 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700907 // We intend to create benchmarks that model the use cases in
908 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
909 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
910 // the image. For that reason, we will benchmark decodes in five representative locations
911 // in the image. Additionally, this use case utilizes power of two scaling, so we will
912 // test on power of two sample sizes. The output tile is always 512x512, so, when a
913 // sampleSize is used, the size of the subset that is decoded is always
914 // (sampleSize*512)x(sampleSize*512).
915 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700916 // All use cases we are aware of only scale by powers of two.
917 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
918 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800919 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700920 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800921 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700922 fSourceType = "image";
923 fBenchType = "BRD";
924
mtklein6f0ff912016-01-11 09:04:21 -0800925 const SkString& path = fImages[fCurrentBRDImage];
926 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
927 continue;
928 }
scroggo860e8a62015-10-15 07:51:28 -0700929
msarettd1227a72016-05-18 06:23:57 -0700930 while (fCurrentColorType < fColorTypes.count()) {
931 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
932 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700933
bungeman38d909e2016-08-02 14:40:46 -0700934 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -0700935 const SkColorType colorType = fColorTypes[fCurrentColorType];
936 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
937 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -0700938
msarettd1227a72016-05-18 06:23:57 -0700939 int width = 0;
940 int height = 0;
reed42943c82016-09-12 12:01:44 -0700941 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -0700942 &width, &height)) {
943 break;
msarett7f691442015-09-22 11:56:16 -0700944 }
msarettd1227a72016-05-18 06:23:57 -0700945
946 SkString basename = SkOSPath::Basename(path.c_str());
947 SkIRect subset;
948 const uint32_t subsetSize = sampleSize * minOutputSize;
949 switch (currentSubsetType) {
950 case kTopLeft_SubsetType:
951 basename.append("_TopLeft");
952 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
953 break;
954 case kTopRight_SubsetType:
955 basename.append("_TopRight");
956 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
957 subsetSize);
958 break;
959 case kMiddle_SubsetType:
960 basename.append("_Middle");
961 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
962 (height - subsetSize) / 2, subsetSize, subsetSize);
963 break;
964 case kBottomLeft_SubsetType:
965 basename.append("_BottomLeft");
966 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
967 subsetSize);
968 break;
969 case kBottomRight_SubsetType:
970 basename.append("_BottomRight");
971 subset = SkIRect::MakeXYWH(width - subsetSize,
972 height - subsetSize, subsetSize, subsetSize);
973 break;
974 default:
975 SkASSERT(false);
976 }
977
978 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
979 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -0700980 }
msarettd1227a72016-05-18 06:23:57 -0700981 fCurrentSubsetType = 0;
982 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -0700983 }
msarettd1227a72016-05-18 06:23:57 -0700984 fCurrentSampleSize = 0;
985 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -0700986 }
msarettd1227a72016-05-18 06:23:57 -0700987 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -0700988 }
989
msarett2cee9022016-06-03 08:25:21 -0700990 while (fCurrentColorImage < fColorImages.count()) {
991 fSourceType = "colorimage";
992 fBenchType = "skcolorcodec";
993 const SkString& path = fColorImages[fCurrentColorImage];
994 fCurrentColorImage++;
995 sk_sp<SkData> encoded = SkData::MakeFromFileName(path.c_str());
996 if (encoded) {
997 return new ColorCodecBench(SkOSPath::Basename(path.c_str()).c_str(),
998 std::move(encoded));
999 } else {
1000 SkDebugf("Could not read file %s.\n", path.c_str());
1001 }
1002 }
1003
halcanary96fcdcc2015-08-27 07:41:13 -07001004 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001005 }
mtklein92007582014-08-01 07:46:52 -07001006
1007 void fillCurrentOptions(ResultsWriter* log) const {
1008 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001009 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001010 if (0 == strcmp(fSourceType, "skp")) {
1011 log->configOption("clip",
1012 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1013 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001014 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -07001015 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001016 if (fCurrentUseMPD > 0) {
1017 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1018 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1019 }
mtklein92007582014-08-01 07:46:52 -07001020 }
mtklein051e56d2014-12-04 08:46:51 -08001021 if (0 == strcmp(fBenchType, "recording")) {
1022 log->metric("bytes", fSKPBytes);
1023 log->metric("ops", fSKPOps);
1024 }
mtklein92007582014-08-01 07:46:52 -07001025 }
1026
mtkleine714e752014-07-31 12:13:48 -07001027private:
msarettb23e6aa2015-06-09 13:56:10 -07001028 enum SubsetType {
1029 kTopLeft_SubsetType = 0,
1030 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001031 kMiddle_SubsetType = 2,
1032 kBottomLeft_SubsetType = 3,
1033 kBottomRight_SubsetType = 4,
1034 kTranslate_SubsetType = 5,
1035 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001036 kLast_SubsetType = kZoom_SubsetType,
1037 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001038 };
1039
mtkleine714e752014-07-31 12:13:48 -07001040 const BenchRegistry* fBenches;
1041 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001042 SkIRect fClip;
1043 SkTArray<SkScalar> fScales;
1044 SkTArray<SkString> fSKPs;
fmalita6519c212016-09-14 08:05:17 -07001045 SkTArray<SkString> fSVGs;
robertphillips5b693772014-11-21 06:19:36 -08001046 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001047 SkTArray<SkString> fImages;
msarett2cee9022016-06-03 08:25:21 -07001048 SkTArray<SkString> fColorImages;
msarett74deb982015-10-20 16:45:56 -07001049 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001050 SkScalar fZoomMax;
1051 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001052
mtklein051e56d2014-12-04 08:46:51 -08001053 double fSKPBytes, fSKPOps;
1054
mtkleinfd731ce2014-09-10 12:19:30 -07001055 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1056 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1057 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -07001058 int fCurrentScale;
1059 int fCurrentSKP;
fmalita6519c212016-09-14 08:05:17 -07001060 int fCurrentSVG;
robertphillips5b693772014-11-21 06:19:36 -08001061 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001062 int fCurrentCodec;
msarett84451022016-02-11 06:45:51 -08001063 int fCurrentAndroidCodec;
msarett7f691442015-09-22 11:56:16 -07001064 int fCurrentBRDImage;
msarett2cee9022016-06-03 08:25:21 -07001065 int fCurrentColorImage;
msarett95f192d2015-02-13 09:05:41 -08001066 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -08001067 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -07001068 int fCurrentSubsetType;
msarett84451022016-02-11 06:45:51 -08001069 int fCurrentSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001070 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001071};
1072
msarettc149f0e2016-01-04 11:35:43 -08001073// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1074// This prints something every once in a while so that it knows we're still working.
1075static void start_keepalive() {
1076 struct Loop {
1077 static void forever(void*) {
1078 for (;;) {
1079 static const int kSec = 1200;
1080 #if defined(SK_BUILD_FOR_WIN)
1081 Sleep(kSec * 1000);
1082 #else
1083 sleep(kSec);
1084 #endif
1085 SkDebugf("\nBenchmarks still running...\n");
1086 }
1087 }
1088 };
1089 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1090 intentionallyLeaked->start();
1091}
1092
jcgregorio3b27ade2014-11-13 08:06:40 -08001093int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001094int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001095 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001096 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001097 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001098
krajcevski69a55602014-08-13 10:46:31 -07001099#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001100 GrContextOptions grContextOpts;
halcanary385fe4d2015-08-26 13:07:48 -07001101 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001102#endif
1103
bsalomon06cddec2014-10-24 10:40:50 -07001104 if (FLAGS_veryVerbose) {
1105 FLAGS_verbose = true;
1106 }
1107
bsalomon6eb03cc2014-08-07 14:28:50 -07001108 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001109 FLAGS_samples = 1;
1110 FLAGS_gpuFrameLag = 0;
1111 }
1112
bsalomon6eb03cc2014-08-07 14:28:50 -07001113 if (!FLAGS_writePath.isEmpty()) {
1114 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1115 if (!sk_mkdir(FLAGS_writePath[0])) {
1116 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001117 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001118 }
1119 }
1120
halcanary385fe4d2015-08-26 13:07:48 -07001121 SkAutoTDelete<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001122 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001123#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001124 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001125#else
1126 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1127 return 1;
1128#endif
mtklein60317d0f2014-07-14 11:30:37 -07001129 }
mtklein1915b622014-08-20 11:45:00 -07001130
1131 if (1 == FLAGS_properties.count() % 2) {
1132 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1133 return 1;
1134 }
1135 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1136 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1137 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001138
1139 if (1 == FLAGS_key.count() % 2) {
1140 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1141 return 1;
1142 }
1143 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001144 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001145 }
mtklein60317d0f2014-07-14 11:30:37 -07001146
mtkleinf3723212014-06-25 14:08:00 -07001147 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001148 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001149
cdaltone1b89582015-06-25 19:17:08 -07001150 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001151
bsalomon6eb03cc2014-08-07 14:28:50 -07001152 if (kAutoTuneLoops != FLAGS_loops) {
1153 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001154 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001155 SkDebugf("! -> high variance, ? -> moderate variance\n");
1156 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001157 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001158 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001159 } else {
mtkleind75c4662015-04-30 07:11:22 -07001160 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001161 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001162 }
1163
svaisanenc47635e2016-01-28 06:05:43 -08001164 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001165 create_configs(&configs);
1166
joshualitt3ebd0502016-02-09 07:18:08 -08001167#ifdef THERMAL_MANAGER_SUPPORTED
1168 int tmEnabled, tmThreshold, tmSleepTimeMs, tmTimeoutMs;
1169 if (4 != sscanf(FLAGS_useThermalManager[0], "%d,%d,%d,%d",
1170 &tmEnabled, &tmThreshold, &tmSleepTimeMs, &tmTimeoutMs)) {
1171 SkDebugf("Can't parse %s from --useThermalManager.\n", FLAGS_useThermalManager[0]);
1172 exit(1);
1173 }
1174 ThermalManager tm(tmThreshold, tmSleepTimeMs, tmTimeoutMs);
1175#endif
1176
msarettc149f0e2016-01-04 11:35:43 -08001177 if (FLAGS_keepAlive) {
1178 start_keepalive();
1179 }
1180
mtkleine070c2b2014-10-14 08:40:43 -07001181 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001182 BenchmarkStream benchStream;
1183 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001184 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001185 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001186 continue;
1187 }
1188
svaisanenc47635e2016-01-28 06:05:43 -08001189 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001190 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001191 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001192 }
egdaniel3bf92062015-06-26 08:12:46 -07001193 for (int i = 0; i < configs.count(); ++i) {
joshualitt3ebd0502016-02-09 07:18:08 -08001194#ifdef THERMAL_MANAGER_SUPPORTED
1195 if (tmEnabled && !tm.coolOffIfNecessary()) {
1196 SkDebugf("Could not cool off, timings will be throttled\n");
1197 }
1198#endif
egdaniel3bf92062015-06-26 08:12:46 -07001199 Target* target = is_enabled(b, configs[i]);
1200 if (!target) {
1201 continue;
1202 }
mtkleinf3723212014-06-25 14:08:00 -07001203
halcanary96fcdcc2015-08-27 07:41:13 -07001204 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001205 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001206 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001207
brianosman7a5ada82016-02-08 13:49:12 -08001208 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001209 SkDebugf("Running %s\t%s\n"
1210 , bench->getUniqueName()
1211 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001212 if (FLAGS_dryRun) {
1213 continue;
1214 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001215 }
1216
egdaniel3bf92062015-06-26 08:12:46 -07001217 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001218 bench->perCanvasPreDraw(canvas);
1219
cdaltone1b89582015-06-25 19:17:08 -07001220 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001221 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001222 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1223 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001224
mtkleinbbba1682015-10-28 11:36:30 -07001225 if (FLAGS_ms) {
1226 samples.reset();
1227 auto stop = now_ms() + FLAGS_ms;
1228 do {
1229 samples.push_back(time(loops, bench, target) / loops);
1230 } while (now_ms() < stop);
1231 } else {
cdaltone1b89582015-06-25 19:17:08 -07001232 samples.reset(FLAGS_samples);
1233 for (int s = 0; s < FLAGS_samples; s++) {
egdaniel3bf92062015-06-26 08:12:46 -07001234 samples[s] = time(loops, bench, target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001235 }
cdaltone1b89582015-06-25 19:17:08 -07001236 }
mtkleinf3723212014-06-25 14:08:00 -07001237
joshualitte45c81c2015-12-02 09:05:37 -08001238#if SK_SUPPORT_GPU
1239 SkTArray<SkString> keys;
1240 SkTArray<double> values;
1241 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1242 if (gpuStatsDump) {
1243 // TODO cache stats
1244 bench->getGpuStats(canvas, &keys, &values);
1245 }
1246#endif
1247
robertphillips5b693772014-11-21 06:19:36 -08001248 bench->perCanvasPostDraw(canvas);
1249
egdaniel3bf92062015-06-26 08:12:46 -07001250 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001251 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001252 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001253 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001254 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001255 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001256 }
1257
1258 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001259 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001260 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001261 continue;
1262 }
1263
cdaltone1b89582015-06-25 19:17:08 -07001264 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001265 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001266 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001267 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001268 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001269 log->metric("min_ms", stats.min);
joshualitte45c81c2015-12-02 09:05:37 -08001270#if SK_SUPPORT_GPU
1271 if (gpuStatsDump) {
1272 // dump to json, only SKPBench currently returns valid keys / values
1273 SkASSERT(keys.count() == values.count());
1274 for (int i = 0; i < keys.count(); i++) {
1275 log->metric(keys[i].c_str(), values[i]);
1276 }
1277 }
1278#endif
1279
mtkleine070c2b2014-10-14 08:40:43 -07001280 if (runs++ % FLAGS_flushEvery == 0) {
1281 log->flush();
1282 }
mtklein60317d0f2014-07-14 11:30:37 -07001283
bsalomon6eb03cc2014-08-07 14:28:50 -07001284 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001285 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001286 config = ""; // Only print the config if we run the same bench on more than one.
1287 }
mtkleind75c4662015-04-30 07:11:22 -07001288 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1289 , sk_tools::getCurrResidentSetSizeMB()
1290 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001291 , bench->getUniqueName()
1292 , config);
mtkleinf3723212014-06-25 14:08:00 -07001293 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001294 const char* mark = " ";
1295 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1296 if (stddev_percent > 5) mark = "?";
1297 if (stddev_percent > 10) mark = "!";
1298
1299 SkDebugf("%10.2f %s\t%s\t%s\n",
1300 stats.median*1e3, mark, bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001301 } else {
1302 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001303 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1304 , sk_tools::getCurrResidentSetSizeMB()
1305 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001306 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001307 , HUMANIZE(stats.min)
1308 , HUMANIZE(stats.median)
1309 , HUMANIZE(stats.mean)
1310 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001311 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001312 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001313 , config
mtklein96289052014-09-10 12:05:59 -07001314 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001315 );
1316 }
joshualitte45c81c2015-12-02 09:05:37 -08001317
bsalomonb12ea412015-02-02 21:19:50 -08001318#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001319 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001320 GrContext* context = gGrFactory->get(configs[i].ctxType,
kkinnunen3e980c32015-12-23 01:33:00 -08001321 configs[i].ctxOptions);
kkinnunen5219fd92015-12-10 06:28:13 -08001322 context->printCacheStats();
1323 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001324 }
1325#endif
joshualitte45c81c2015-12-02 09:05:37 -08001326
cdalton2c56ba52015-06-26 13:32:53 -07001327 if (FLAGS_verbose) {
1328 SkDebugf("Samples: ");
1329 for (int i = 0; i < samples.count(); i++) {
1330 SkDebugf("%s ", HUMANIZE(samples[i]));
1331 }
1332 SkDebugf("%s\n", bench->getUniqueName());
1333 }
egdaniel3bf92062015-06-26 08:12:46 -07001334 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001335 }
mtkleinf3723212014-06-25 14:08:00 -07001336 }
1337
mtkleine1091452014-12-04 10:47:02 -08001338 log->bench("memory_usage", 0,0);
1339 log->config("meta");
1340 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1341
joshualitte0b19d42015-03-26 10:41:02 -07001342#if SK_SUPPORT_GPU
1343 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1344 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001345 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001346#endif
1347
mtkleinf3723212014-06-25 14:08:00 -07001348 return 0;
1349}
1350
jcgregorio3b27ade2014-11-13 08:06:40 -08001351#if !defined SK_BUILD_FOR_IOS
1352int main(int argc, char** argv) {
1353 SkCommandLineFlags::Parse(argc, argv);
1354 return nanobench_main();
1355}
1356#endif