blob: 26ba05ff4f15bc583a00372107e8a873169c0bc4 [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
fmalita6519c212016-09-14 08:05:17 -0700130DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
131
mtkleinbbba1682015-10-28 11:36:30 -0700132static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
133
mtkleinf3723212014-06-25 14:08:00 -0700134static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700135 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800136 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700137}
mtklein55b0ffc2014-07-17 08:38:23 -0700138#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700139
tomhudsond968a6f2015-03-26 11:28:06 -0700140bool Target::init(SkImageInfo info, Benchmark* bench) {
141 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700142 this->surface = SkSurface::MakeRaster(info);
143 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700144 return false;
145 }
146 }
147 return true;
148}
149bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700150 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700151 if (!canvas) {
152 return false;
153 }
154 bmp->setInfo(canvas->imageInfo());
155 if (!canvas->readPixels(bmp, 0, 0)) {
156 SkDebugf("Can't read canvas pixels.\n");
157 return false;
158 }
159 return true;
160}
161
162#if SK_SUPPORT_GPU
163struct GPUTarget : public Target {
bsalomonc8699322016-05-11 11:55:36 -0700164 explicit GPUTarget(const Config& c) : Target(c), context(nullptr) { }
165 TestContext* context;
tomhudsond968a6f2015-03-26 11:28:06 -0700166
167 void setup() override {
bsalomonc8699322016-05-11 11:55:36 -0700168 this->context->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700169 // Make sure we're done with whatever came before.
bsalomonc8699322016-05-11 11:55:36 -0700170 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700171 }
172 void endTiming() override {
bsalomonc8699322016-05-11 11:55:36 -0700173 if (this->context) {
174 this->context->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700175 }
176 }
177 void fence() override {
bsalomonc8699322016-05-11 11:55:36 -0700178 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700179 }
mtkleind75c4662015-04-30 07:11:22 -0700180
cdaltond416a5b2015-06-23 13:23:44 -0700181 bool needsFrameTiming(int* maxFrameLag) const override {
bsalomonc8699322016-05-11 11:55:36 -0700182 if (!this->context->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700183 // Frame lag is unknown.
184 *maxFrameLag = FLAGS_gpuFrameLag;
185 }
186 return true;
187 }
tomhudsond968a6f2015-03-26 11:28:06 -0700188 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700189 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
190 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700191 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700192 this->surface = SkSurface::MakeRenderTarget(gGrFactory->get(this->config.ctxType,
193 this->config.ctxOptions),
bsalomon5ec26ae2016-02-25 08:33:02 -0800194 SkBudgeted::kNo, info,
reede8f30622016-03-23 18:59:25 -0700195 this->config.samples, &props);
bsalomonc8699322016-05-11 11:55:36 -0700196 this->context = gGrFactory->getContextInfo(this->config.ctxType,
197 this->config.ctxOptions).testContext();
tomhudsond968a6f2015-03-26 11:28:06 -0700198 if (!this->surface.get()) {
199 return false;
200 }
bsalomonc8699322016-05-11 11:55:36 -0700201 if (!this->context->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700202 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800203 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700204 }
tomhudsond968a6f2015-03-26 11:28:06 -0700205 return true;
206 }
207 void fillOptions(ResultsWriter* log) override {
208 const GrGLubyte* version;
bsalomonc8699322016-05-11 11:55:36 -0700209 if (this->context->backend() == kOpenGL_GrBackend) {
210 const GrGLInterface* gl =
211 reinterpret_cast<const GrGLInterface*>(this->context->backendContext());
212 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
213 log->configOption("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700214
bsalomonc8699322016-05-11 11:55:36 -0700215 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
216 log->configOption("GL_RENDERER", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700217
bsalomonc8699322016-05-11 11:55:36 -0700218 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VENDOR));
219 log->configOption("GL_VENDOR", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700220
bsalomonc8699322016-05-11 11:55:36 -0700221 GR_GL_CALL_RET(gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
222 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
223 }
tomhudsond968a6f2015-03-26 11:28:06 -0700224 }
225};
mtkleind75c4662015-04-30 07:11:22 -0700226
tomhudsond968a6f2015-03-26 11:28:06 -0700227#endif
228
tomhudson75a0ebb2015-03-27 12:11:44 -0700229static double time(int loops, Benchmark* bench, Target* target) {
230 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700231 if (canvas) {
232 canvas->clear(SK_ColorWHITE);
233 }
joshualitt8a6697a2015-09-30 12:11:07 -0700234 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700235 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700236 canvas = target->beginTiming(canvas);
237 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700238 if (canvas) {
239 canvas->flush();
240 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700241 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700242 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700243 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700244 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700245}
246
mtkleinf3723212014-06-25 14:08:00 -0700247static double estimate_timer_overhead() {
248 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700249 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700250 double start = now_ms();
251 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700252 }
253 return overhead / FLAGS_overheadLoops;
254}
255
reed53249782014-10-10 09:09:52 -0700256static int detect_forever_loops(int loops) {
257 // look for a magic run-forever value
258 if (loops < 0) {
259 loops = SK_MaxS32;
260 }
261 return loops;
262}
263
mtklein55b0ffc2014-07-17 08:38:23 -0700264static int clamp_loops(int loops) {
265 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800266 SkDebugf("ERROR: clamping loops from %d to 1. "
267 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700268 return 1;
269 }
270 if (loops > FLAGS_maxLoops) {
271 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
272 return FLAGS_maxLoops;
273 }
274 return loops;
275}
276
tomhudsond968a6f2015-03-26 11:28:06 -0700277static bool write_canvas_png(Target* target, const SkString& filename) {
278
bsalomon6eb03cc2014-08-07 14:28:50 -0700279 if (filename.isEmpty()) {
280 return false;
281 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700282 if (target->getCanvas() &&
283 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700284 return false;
285 }
tomhudsond968a6f2015-03-26 11:28:06 -0700286
bsalomon6eb03cc2014-08-07 14:28:50 -0700287 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700288
289 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700290 return false;
291 }
tomhudsond968a6f2015-03-26 11:28:06 -0700292
bsalomon6eb03cc2014-08-07 14:28:50 -0700293 SkString dir = SkOSPath::Dirname(filename.c_str());
294 if (!sk_mkdir(dir.c_str())) {
295 SkDebugf("Can't make dir %s.\n", dir.c_str());
296 return false;
297 }
298 SkFILEWStream stream(filename.c_str());
299 if (!stream.isValid()) {
300 SkDebugf("Can't write %s.\n", filename.c_str());
301 return false;
302 }
303 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
304 SkDebugf("Can't encode a PNG.\n");
305 return false;
306 }
307 return true;
308}
309
310static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700311static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700312 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700313 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700314 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700315 int loops = bench->calculateLoops(FLAGS_loops);
316 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700317 while (bench_plus_overhead < overhead) {
318 if (round++ == FLAGS_maxCalibrationAttempts) {
319 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700320 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700321 return kFailedLoops;
322 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700323 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700324 }
mtklein2069e222014-08-04 13:57:39 -0700325 }
mtkleinf3723212014-06-25 14:08:00 -0700326
mtkleinbb6a0282014-07-01 08:43:42 -0700327 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700328 // We'll pick N to make timer overhead negligible:
329 //
mtkleinbb6a0282014-07-01 08:43:42 -0700330 // overhead
331 // ------------------------- < FLAGS_overheadGoal
332 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700333 //
mtkleinbb6a0282014-07-01 08:43:42 -0700334 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700335 //
336 // Doing some math, we get:
337 //
mtkleinbb6a0282014-07-01 08:43:42 -0700338 // (overhead / FLAGS_overheadGoal) - overhead
339 // ------------------------------------------ < N
340 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700341 //
342 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700343 if (kAutoTuneLoops == loops) {
344 const double numer = overhead / FLAGS_overheadGoal - overhead;
345 const double denom = bench_plus_overhead - overhead;
346 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700347 loops = clamp_loops(loops);
348 } else {
349 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700350 }
mtkleinbb6a0282014-07-01 08:43:42 -0700351
mtkleinbb6a0282014-07-01 08:43:42 -0700352 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700353}
354
cdaltone1b89582015-06-25 19:17:08 -0700355static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700356 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700357 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700358 if (kAutoTuneLoops == loops) {
359 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700360 double elapsed = 0;
361 do {
mtklein527930f2014-11-06 08:04:34 -0800362 if (1<<30 == loops) {
363 // We're about to wrap. Something's wrong with the bench.
364 loops = 0;
365 break;
366 }
mtkleina189ccd2014-07-14 12:28:47 -0700367 loops *= 2;
368 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700369 // _this_ round, not still timing last round.
370 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700371 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700372 }
373 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700374
mtkleina189ccd2014-07-14 12:28:47 -0700375 // We've overshot at least a little. Scale back linearly.
376 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700377 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700378
tomhudsond968a6f2015-03-26 11:28:06 -0700379 // Make sure we're not still timing our calibration.
380 target->fence();
reed53249782014-10-10 09:09:52 -0700381 } else {
382 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700383 }
mtkleinbb6a0282014-07-01 08:43:42 -0700384
385 // Pretty much the same deal as the calibration: do some warmup to make
386 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700387 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700388 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700389 }
mtkleinbb6a0282014-07-01 08:43:42 -0700390
mtkleinbb6a0282014-07-01 08:43:42 -0700391 return loops;
392}
mtkleinbb6a0282014-07-01 08:43:42 -0700393
bsalomonc2553372014-07-22 13:09:05 -0700394#if SK_SUPPORT_GPU
bsalomon85b4b532016-04-05 11:06:27 -0700395#define kBogusContextType GrContextFactory::kNativeGL_ContextType
csmartdalton6270e552016-09-13 10:41:49 -0700396#define kBogusContextOptions GrContextFactory::ContextOptions::kNone
bsalomonc2553372014-07-22 13:09:05 -0700397#else
bsalomon85b4b532016-04-05 11:06:27 -0700398#define kBogusContextType 0
399#define kBogusContextOptions 0
mtkleine714e752014-07-31 12:13:48 -0700400#endif
bsalomonc2553372014-07-22 13:09:05 -0700401
svaisanenc47635e2016-01-28 06:05:43 -0800402static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
403
404#if SK_SUPPORT_GPU
405 if (const auto* gpuConfig = config->asConfigGpu()) {
406 if (!FLAGS_gpu)
407 return;
408
svaisanenc47635e2016-01-28 06:05:43 -0800409 const auto ctxType = gpuConfig->getContextType();
csmartdalton6270e552016-09-13 10:41:49 -0700410 const auto ctxOptions = gpuConfig->getContextOptions();
svaisanenc47635e2016-01-28 06:05:43 -0800411 const auto sampleCount = gpuConfig->getSamples();
412
413 if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOptions)) {
414 const auto maxSampleCount = ctx->caps()->maxSampleCount();
415 if (sampleCount > ctx->caps()->maxSampleCount()) {
416 SkDebugf("Configuration sample count %d exceeds maximum %d.\n",
417 sampleCount, maxSampleCount);
418 return;
419 }
420 } else {
421 SkDebugf("No context was available matching config type and options.\n");
422 return;
423 }
424
425 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800426 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800427 Benchmark::kGPU_Backend,
brianosman1a48e132016-06-20 09:41:41 -0700428 gpuConfig->getColorType(),
svaisanenc47635e2016-01-28 06:05:43 -0800429 kPremul_SkAlphaType,
brianosman1a48e132016-06-20 09:41:41 -0700430 sk_ref_sp(gpuConfig->getColorSpace()),
svaisanenc47635e2016-01-28 06:05:43 -0800431 sampleCount,
432 ctxType,
kkinnunend4e1c352016-03-01 23:41:26 -0800433 ctxOptions,
434 gpuConfig->getUseDIText()
435 };
svaisanenc47635e2016-01-28 06:05:43 -0800436
437 configs->push_back(target);
438 return;
439 }
440#endif
441
brianosmanb109b8c2016-06-16 13:03:24 -0700442 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
443 if (config->getTag().equals(#name)) { \
444 Config config = { \
445 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
446 0, kBogusContextType, kBogusContextOptions, false \
447 }; \
448 configs->push_back(config); \
449 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700450 }
mtkleine714e752014-07-31 12:13:48 -0700451
mtklein40b32be2014-07-09 08:46:49 -0700452 if (FLAGS_cpu) {
mtkleinbb6c41b2016-03-08 11:31:11 -0800453 CPU_CONFIG(nonrendering, kNonRendering_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700454 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800455
456 CPU_CONFIG(8888, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700457 kN32_SkColorType, kPremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800458 CPU_CONFIG(565, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700459 kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
460 auto srgbColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
mtkleinbb6c41b2016-03-08 11:31:11 -0800461 CPU_CONFIG(srgb, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700462 kN32_SkColorType, kPremul_SkAlphaType, srgbColorSpace)
mtkleinbb6c41b2016-03-08 11:31:11 -0800463 CPU_CONFIG(f16, kRaster_Backend,
brianosman0e22eb82016-08-30 07:07:59 -0700464 kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbColorSpace->makeLinearGamma())
mtklein40b32be2014-07-09 08:46:49 -0700465 }
mtkleinbb6a0282014-07-01 08:43:42 -0700466
svaisanenc47635e2016-01-28 06:05:43 -0800467 #undef CPU_CONFIG
tomhudsond968a6f2015-03-26 11:28:06 -0700468
469#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
djsollen817c72a2016-01-28 13:57:02 -0800470 if (config->getTag().equals("hwui")) {
mtkleinbb6c41b2016-03-08 11:31:11 -0800471 Config config = { SkString("hwui"), Benchmark::kHWUI_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700472 kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr,
bsalomon85b4b532016-04-05 11:06:27 -0700473 0, kBogusContextType, kBogusContextOptions, false };
svaisanenc47635e2016-01-28 06:05:43 -0800474 configs->push_back(config);
tomhudsond968a6f2015-03-26 11:28:06 -0700475 }
476#endif
mtkleinf3723212014-06-25 14:08:00 -0700477}
478
svaisanenc47635e2016-01-28 06:05:43 -0800479// Append all configs that are enabled and supported.
480void create_configs(SkTArray<Config>* configs) {
481 SkCommandLineConfigArray array;
482 ParseConfigs(FLAGS_config, &array);
483 for (int i = 0; i < array.count(); ++i) {
484 create_config(array[i], configs);
485 }
486}
487
halcanary96fcdcc2015-08-27 07:41:13 -0700488// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700489static Target* is_enabled(Benchmark* bench, const Config& config) {
490 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700491 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700492 }
493
reede5ea5002014-09-03 11:54:58 -0700494 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700495 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700496
halcanary96fcdcc2015-08-27 07:41:13 -0700497 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700498
tomhudsond968a6f2015-03-26 11:28:06 -0700499 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700500#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700501 case Benchmark::kGPU_Backend:
502 target = new GPUTarget(config);
503 break;
bsalomonc2553372014-07-22 13:09:05 -0700504#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700505#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
506 case Benchmark::kHWUI_Backend:
507 target = new HWUITarget(config, bench);
508 break;
509#endif
510 default:
511 target = new Target(config);
512 break;
513 }
bsalomonc2553372014-07-22 13:09:05 -0700514
tomhudsond968a6f2015-03-26 11:28:06 -0700515 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700516 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700517 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700518 }
519 return target;
520}
521
reed42943c82016-09-12 12:01:44 -0700522static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700523 uint32_t minOutputSize, int* width, int* height) {
msarett5cb48852015-11-06 08:56:32 -0800524 SkAutoTDelete<SkBitmapRegionDecoder> brd(
msarettd1227a72016-05-18 06:23:57 -0700525 SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy));
msarett7f691442015-09-22 11:56:16 -0700526 if (nullptr == brd.get()) {
527 // This is indicates that subset decoding is not supported for a particular image format.
528 return false;
529 }
530
msarett7f691442015-09-22 11:56:16 -0700531 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
532 (uint32_t) brd->height()) {
533 // This indicates that the image is not large enough to decode a
534 // minOutputSize x minOutputSize subset at the given sampleSize.
535 return false;
536 }
537
538 // Set the image width and height. The calling code will use this to choose subsets to decode.
539 *width = brd->width();
540 *height = brd->height();
541 return true;
542}
543
egdaniel3bf92062015-06-26 08:12:46 -0700544static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700545 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700546#if SK_SUPPORT_GPU
547 if (FLAGS_abandonGpuContext) {
548 gGrFactory->abandonContexts();
549 }
550 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
551 gGrFactory->destroyContexts();
552 }
553#endif
554}
555
fmalita6519c212016-09-14 08:05:17 -0700556static void collect_files(const SkCommandLineFlags::StringArray& paths, const char* ext,
557 SkTArray<SkString>* list) {
558 for (int i = 0; i < paths.count(); ++i) {
559 if (SkStrEndsWith(paths[i], ext)) {
560 list->push_back(SkString(paths[i]));
561 } else {
562 SkOSFile::Iter it(paths[i], ext);
563 SkString path;
564 while (it.next(&path)) {
565 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
566 }
567 }
568 }
569}
570
mtkleine714e752014-07-31 12:13:48 -0700571class BenchmarkStream {
572public:
mtklein92007582014-08-01 07:46:52 -0700573 BenchmarkStream() : fBenches(BenchRegistry::Head())
574 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700575 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700576 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800577 , fCurrentSKP(0)
fmalita6519c212016-09-14 08:05:17 -0700578 , fCurrentSVG(0)
msarett95f192d2015-02-13 09:05:41 -0800579 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700580 , fCurrentCodec(0)
msarett84451022016-02-11 06:45:51 -0800581 , fCurrentAndroidCodec(0)
msarett7f691442015-09-22 11:56:16 -0700582 , fCurrentBRDImage(0)
msarett2cee9022016-06-03 08:25:21 -0700583 , fCurrentColorImage(0)
msarett95f192d2015-02-13 09:05:41 -0800584 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800585 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700586 , fCurrentSubsetType(0)
msarett84451022016-02-11 06:45:51 -0800587 , fCurrentSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700588 , fCurrentAnimSKP(0) {
fmalita6519c212016-09-14 08:05:17 -0700589 collect_files(FLAGS_skps, ".skp", &fSKPs);
590 collect_files(FLAGS_svgs, ".svg", &fSVGs);
mtkleine714e752014-07-31 12:13:48 -0700591
mtklein92007582014-08-01 07:46:52 -0700592 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
593 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
594 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
595 exit(1);
596 }
597
598 for (int i = 0; i < FLAGS_scales.count(); i++) {
599 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
600 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
601 exit(1);
602 }
603 }
robertphillips5b693772014-11-21 06:19:36 -0800604
cdalton63a82852015-06-29 14:06:10 -0700605 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
606 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700607 exit(1);
608 }
609
robertphillips5b693772014-11-21 06:19:36 -0800610 if (FLAGS_mpd) {
611 fUseMPDs.push_back() = true;
612 }
mtkleinc751ecb2015-06-15 08:56:38 -0700613 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700614
msarett95f192d2015-02-13 09:05:41 -0800615 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700616 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800617 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800618 }
msarett2cee9022016-06-03 08:25:21 -0700619 if (!CollectImages(FLAGS_colorImages, &fColorImages)) {
620 exit(1);
621 }
mtklein95553d92015-03-12 08:24:21 -0700622
msarett95f192d2015-02-13 09:05:41 -0800623 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700624 fColorTypes.push_back(kN32_SkColorType);
625 if (!FLAGS_simpleCodec) {
626 fColorTypes.push_back(kRGB_565_SkColorType);
627 fColorTypes.push_back(kAlpha_8_SkColorType);
628 fColorTypes.push_back(kIndex_8_SkColorType);
629 fColorTypes.push_back(kGray_8_SkColorType);
630 }
mtklein92007582014-08-01 07:46:52 -0700631 }
632
reedca2622b2016-03-18 07:25:55 -0700633 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700634 // Not strictly necessary, as it will be checked again later,
635 // but helps to avoid a lot of pointless work if we're going to skip it.
cdalton91e457d2016-02-17 11:10:16 -0800636 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700637 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700638 }
639
scroggoa1193e42015-01-21 12:09:53 -0800640 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -0700641 if (stream.get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700642 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700643 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700644 }
645
reedca2622b2016-03-18 07:25:55 -0700646 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700647 }
648
fmalita6519c212016-09-14 08:05:17 -0700649 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
650 SkFILEStream stream(path);
651 if (!stream.isValid()) {
652 SkDebugf("Could not read %s.\n", path);
653 return nullptr;
654 }
655
656 // TODO: use intrinsic size? make tunable via flag?
657 static const SkSize kContainerSize = SkSize::Make(128, 128);
658 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream, kContainerSize);
659 if (!svgDom) {
660 SkDebugf("Could not parse %s.\n", path);
661 return nullptr;
662 }
663
664 SkPictureRecorder recorder;
665 svgDom->render(recorder.beginRecording(kContainerSize.width(), kContainerSize.height()));
666 return recorder.finishRecordingAsPicture();
667 }
668
mtklein92007582014-08-01 07:46:52 -0700669 Benchmark* next() {
mtklein65dfd2f2016-02-03 10:40:54 -0800670 SkAutoTDelete<Benchmark> bench;
671 do {
672 bench.reset(this->rawNext());
673 if (!bench) {
674 return nullptr;
675 }
676 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
677 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700678 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800679 }
680
681 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700682 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700683 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700684 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700685 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700686 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700687 return bench;
688 }
mtklein92007582014-08-01 07:46:52 -0700689
mtkleine714e752014-07-31 12:13:48 -0700690 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -0700691 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700692 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800693 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700694 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700695 fBenchType = "micro";
mtklein18300a32016-03-16 13:53:35 -0700696 return new GMBench(gm.release());
mtkleine714e752014-07-31 12:13:48 -0700697 }
698 }
mtklein92007582014-08-01 07:46:52 -0700699
mtkleinfd731ce2014-09-10 12:19:30 -0700700 // First add all .skps as RecordingBenches.
701 while (fCurrentRecording < fSKPs.count()) {
702 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700703 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
704 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700705 continue;
706 }
707 SkString name = SkOSPath::Basename(path.c_str());
708 fSourceType = "skp";
709 fBenchType = "recording";
reedca2622b2016-03-18 07:25:55 -0700710 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic.get()));
mtklein051e56d2014-12-04 08:46:51 -0800711 fSKPOps = pic->approximateOpCount();
mtklein8c1a4f82016-08-08 06:56:22 -0700712 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh, FLAGS_lite);
mtkleinfd731ce2014-09-10 12:19:30 -0700713 }
714
715 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700716 while (fCurrentScale < fScales.count()) {
717 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800718 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700719 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
720 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800721 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700722 continue;
723 }
robertphillips5b693772014-11-21 06:19:36 -0800724
725 while (fCurrentUseMPD < fUseMPDs.count()) {
726 if (FLAGS_bbh) {
727 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
728 SkRTreeFactory factory;
729 SkPictureRecorder recorder;
robertphillips5b693772014-11-21 06:19:36 -0800730 pic->playback(recorder.beginRecording(pic->cullRect().width(),
731 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800732 &factory,
robertphillipsdda54452016-07-13 13:27:16 -0700733 0));
reedca2622b2016-03-18 07:25:55 -0700734 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800735 }
736 SkString name = SkOSPath::Basename(path.c_str());
737 fSourceType = "skp";
738 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700739 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
740 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700741 }
robertphillips5b693772014-11-21 06:19:36 -0800742 fCurrentUseMPD = 0;
743 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700744 }
fmalita6519c212016-09-14 08:05:17 -0700745
746 while (fCurrentSVG++ < fSVGs.count()) {
747 const char* path = fSVGs[fCurrentSVG - 1].c_str();
748 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
749 fSourceType = "svg";
750 fBenchType = "playback";
751 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
752 fScales[fCurrentScale], false, FLAGS_loopSKP);
753 }
754 }
755
mtklein92007582014-08-01 07:46:52 -0700756 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700757 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700758 fCurrentScale++;
759 }
760
joshualitt261c3ad2015-04-27 09:16:57 -0700761 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700762 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700763 while (fCurrentAnimSKP < fSKPs.count()) {
764 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700765 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
766 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700767 fCurrentAnimSKP++;
768 continue;
769 }
770
771 fCurrentAnimSKP++;
772 SkString name = SkOSPath::Basename(path.c_str());
cdalton63a82852015-06-29 14:06:10 -0700773 SkAutoTUnref<SKPAnimationBench::Animation> animation(
774 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
halcanary385fe4d2015-08-26 13:07:48 -0700775 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation,
776 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700777 }
778 }
779
scroggo60869a42015-04-01 12:09:17 -0700780 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700781 fSourceType = "image";
782 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700783 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800784 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
785 continue;
786 }
bungeman38d909e2016-08-02 14:40:46 -0700787 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
reed42943c82016-09-12 12:01:44 -0700788 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700789 if (!codec) {
790 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700791 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700792 continue;
793 }
scroggo21027992015-04-02 13:22:38 -0700794
scroggo60869a42015-04-01 12:09:17 -0700795 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700796 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700797
msarettc7796b92016-01-07 14:20:20 -0800798 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700799 if (FLAGS_simpleCodec) {
800 if (kUnpremul_SkAlphaType == alphaType) {
801 alphaType = kPremul_SkAlphaType;
802 }
803
804 fCurrentColorType++;
805 } else {
806 switch (alphaType) {
807 case kOpaque_SkAlphaType:
808 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800809 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700810 break;
811 case kUnpremul_SkAlphaType:
812 case kPremul_SkAlphaType:
813 if (0 == fCurrentAlphaType) {
814 // Test unpremul first.
815 alphaType = kUnpremul_SkAlphaType;
816 fCurrentAlphaType++;
817 } else {
818 // Test premul.
819 alphaType = kPremul_SkAlphaType;
820 fCurrentAlphaType = 0;
821 fCurrentColorType++;
822 }
823 break;
824 default:
825 SkASSERT(false);
826 fCurrentColorType++;
827 break;
828 }
scroggo21027992015-04-02 13:22:38 -0700829 }
830
msarettc7796b92016-01-07 14:20:20 -0800831 // Make sure we can decode to this color type and alpha type.
832 SkImageInfo info =
833 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700834 const size_t rowBytes = info.minRowBytes();
835 SkAutoMalloc storage(info.getSafeSize(rowBytes));
836
837 // Used if fCurrentColorType is kIndex_8_SkColorType
838 int colorCount = 256;
839 SkPMColor colors[256];
840
scroggoeb602a52015-07-09 08:16:03 -0700841 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700842 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700843 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700844 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700845 case SkCodec::kSuccess:
846 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700847 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700848 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700849 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700850 // This is okay. Not all conversions are valid.
851 break;
scroggo60869a42015-04-01 12:09:17 -0700852 default:
853 // This represents some sort of failure.
854 SkASSERT(false);
855 break;
856 }
857 }
858 fCurrentColorType = 0;
859 }
860
msarett84451022016-02-11 06:45:51 -0800861 // Run AndroidCodecBenches
862 const int sampleSizes[] = { 2, 4, 8 };
863 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
864 fSourceType = "image";
865 fBenchType = "skandroidcodec";
866
867 const SkString& path = fImages[fCurrentAndroidCodec];
868 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
869 continue;
870 }
bungeman38d909e2016-08-02 14:40:46 -0700871 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
reed42943c82016-09-12 12:01:44 -0700872 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800873 if (!codec) {
874 // Nothing to time.
875 SkDebugf("Cannot find codec for %s\n", path.c_str());
876 continue;
877 }
878
879 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
880 int sampleSize = sampleSizes[fCurrentSampleSize];
881 fCurrentSampleSize++;
882 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
883 // Avoid benchmarking scaled decodes of already small images.
884 break;
885 }
886
bungeman38d909e2016-08-02 14:40:46 -0700887 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
888 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -0800889 }
890 fCurrentSampleSize = 0;
891 }
892
msarett7f691442015-09-22 11:56:16 -0700893 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700894 // We intend to create benchmarks that model the use cases in
895 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
896 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
897 // the image. For that reason, we will benchmark decodes in five representative locations
898 // in the image. Additionally, this use case utilizes power of two scaling, so we will
899 // test on power of two sample sizes. The output tile is always 512x512, so, when a
900 // sampleSize is used, the size of the subset that is decoded is always
901 // (sampleSize*512)x(sampleSize*512).
902 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700903 // All use cases we are aware of only scale by powers of two.
904 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
905 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800906 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700907 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800908 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700909 fSourceType = "image";
910 fBenchType = "BRD";
911
mtklein6f0ff912016-01-11 09:04:21 -0800912 const SkString& path = fImages[fCurrentBRDImage];
913 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
914 continue;
915 }
scroggo860e8a62015-10-15 07:51:28 -0700916
msarettd1227a72016-05-18 06:23:57 -0700917 while (fCurrentColorType < fColorTypes.count()) {
918 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
919 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700920
bungeman38d909e2016-08-02 14:40:46 -0700921 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -0700922 const SkColorType colorType = fColorTypes[fCurrentColorType];
923 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
924 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -0700925
msarettd1227a72016-05-18 06:23:57 -0700926 int width = 0;
927 int height = 0;
reed42943c82016-09-12 12:01:44 -0700928 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -0700929 &width, &height)) {
930 break;
msarett7f691442015-09-22 11:56:16 -0700931 }
msarettd1227a72016-05-18 06:23:57 -0700932
933 SkString basename = SkOSPath::Basename(path.c_str());
934 SkIRect subset;
935 const uint32_t subsetSize = sampleSize * minOutputSize;
936 switch (currentSubsetType) {
937 case kTopLeft_SubsetType:
938 basename.append("_TopLeft");
939 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
940 break;
941 case kTopRight_SubsetType:
942 basename.append("_TopRight");
943 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
944 subsetSize);
945 break;
946 case kMiddle_SubsetType:
947 basename.append("_Middle");
948 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
949 (height - subsetSize) / 2, subsetSize, subsetSize);
950 break;
951 case kBottomLeft_SubsetType:
952 basename.append("_BottomLeft");
953 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
954 subsetSize);
955 break;
956 case kBottomRight_SubsetType:
957 basename.append("_BottomRight");
958 subset = SkIRect::MakeXYWH(width - subsetSize,
959 height - subsetSize, subsetSize, subsetSize);
960 break;
961 default:
962 SkASSERT(false);
963 }
964
965 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
966 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -0700967 }
msarettd1227a72016-05-18 06:23:57 -0700968 fCurrentSubsetType = 0;
969 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -0700970 }
msarettd1227a72016-05-18 06:23:57 -0700971 fCurrentSampleSize = 0;
972 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -0700973 }
msarettd1227a72016-05-18 06:23:57 -0700974 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -0700975 }
976
msarett2cee9022016-06-03 08:25:21 -0700977 while (fCurrentColorImage < fColorImages.count()) {
978 fSourceType = "colorimage";
979 fBenchType = "skcolorcodec";
980 const SkString& path = fColorImages[fCurrentColorImage];
981 fCurrentColorImage++;
982 sk_sp<SkData> encoded = SkData::MakeFromFileName(path.c_str());
983 if (encoded) {
984 return new ColorCodecBench(SkOSPath::Basename(path.c_str()).c_str(),
985 std::move(encoded));
986 } else {
987 SkDebugf("Could not read file %s.\n", path.c_str());
988 }
989 }
990
halcanary96fcdcc2015-08-27 07:41:13 -0700991 return nullptr;
mtkleine714e752014-07-31 12:13:48 -0700992 }
mtklein92007582014-08-01 07:46:52 -0700993
994 void fillCurrentOptions(ResultsWriter* log) const {
995 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700996 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700997 if (0 == strcmp(fSourceType, "skp")) {
998 log->configOption("clip",
999 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1000 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001001 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -07001002 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001003 if (fCurrentUseMPD > 0) {
1004 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1005 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1006 }
mtklein92007582014-08-01 07:46:52 -07001007 }
mtklein051e56d2014-12-04 08:46:51 -08001008 if (0 == strcmp(fBenchType, "recording")) {
1009 log->metric("bytes", fSKPBytes);
1010 log->metric("ops", fSKPOps);
1011 }
mtklein92007582014-08-01 07:46:52 -07001012 }
1013
mtkleine714e752014-07-31 12:13:48 -07001014private:
msarettb23e6aa2015-06-09 13:56:10 -07001015 enum SubsetType {
1016 kTopLeft_SubsetType = 0,
1017 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001018 kMiddle_SubsetType = 2,
1019 kBottomLeft_SubsetType = 3,
1020 kBottomRight_SubsetType = 4,
1021 kTranslate_SubsetType = 5,
1022 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001023 kLast_SubsetType = kZoom_SubsetType,
1024 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001025 };
1026
mtkleine714e752014-07-31 12:13:48 -07001027 const BenchRegistry* fBenches;
1028 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001029 SkIRect fClip;
1030 SkTArray<SkScalar> fScales;
1031 SkTArray<SkString> fSKPs;
fmalita6519c212016-09-14 08:05:17 -07001032 SkTArray<SkString> fSVGs;
robertphillips5b693772014-11-21 06:19:36 -08001033 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001034 SkTArray<SkString> fImages;
msarett2cee9022016-06-03 08:25:21 -07001035 SkTArray<SkString> fColorImages;
msarett74deb982015-10-20 16:45:56 -07001036 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001037 SkScalar fZoomMax;
1038 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001039
mtklein051e56d2014-12-04 08:46:51 -08001040 double fSKPBytes, fSKPOps;
1041
mtkleinfd731ce2014-09-10 12:19:30 -07001042 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1043 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1044 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -07001045 int fCurrentScale;
1046 int fCurrentSKP;
fmalita6519c212016-09-14 08:05:17 -07001047 int fCurrentSVG;
robertphillips5b693772014-11-21 06:19:36 -08001048 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001049 int fCurrentCodec;
msarett84451022016-02-11 06:45:51 -08001050 int fCurrentAndroidCodec;
msarett7f691442015-09-22 11:56:16 -07001051 int fCurrentBRDImage;
msarett2cee9022016-06-03 08:25:21 -07001052 int fCurrentColorImage;
msarett95f192d2015-02-13 09:05:41 -08001053 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -08001054 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -07001055 int fCurrentSubsetType;
msarett84451022016-02-11 06:45:51 -08001056 int fCurrentSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001057 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001058};
1059
msarettc149f0e2016-01-04 11:35:43 -08001060// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1061// This prints something every once in a while so that it knows we're still working.
1062static void start_keepalive() {
1063 struct Loop {
1064 static void forever(void*) {
1065 for (;;) {
1066 static const int kSec = 1200;
1067 #if defined(SK_BUILD_FOR_WIN)
1068 Sleep(kSec * 1000);
1069 #else
1070 sleep(kSec);
1071 #endif
1072 SkDebugf("\nBenchmarks still running...\n");
1073 }
1074 }
1075 };
1076 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1077 intentionallyLeaked->start();
1078}
1079
jcgregorio3b27ade2014-11-13 08:06:40 -08001080int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001081int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001082 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001083 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001084 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001085
krajcevski69a55602014-08-13 10:46:31 -07001086#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001087 GrContextOptions grContextOpts;
halcanary385fe4d2015-08-26 13:07:48 -07001088 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001089#endif
1090
bsalomon06cddec2014-10-24 10:40:50 -07001091 if (FLAGS_veryVerbose) {
1092 FLAGS_verbose = true;
1093 }
1094
bsalomon6eb03cc2014-08-07 14:28:50 -07001095 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001096 FLAGS_samples = 1;
1097 FLAGS_gpuFrameLag = 0;
1098 }
1099
bsalomon6eb03cc2014-08-07 14:28:50 -07001100 if (!FLAGS_writePath.isEmpty()) {
1101 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1102 if (!sk_mkdir(FLAGS_writePath[0])) {
1103 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001104 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001105 }
1106 }
1107
halcanary385fe4d2015-08-26 13:07:48 -07001108 SkAutoTDelete<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001109 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001110#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001111 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001112#else
1113 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1114 return 1;
1115#endif
mtklein60317d0f2014-07-14 11:30:37 -07001116 }
mtklein1915b622014-08-20 11:45:00 -07001117
1118 if (1 == FLAGS_properties.count() % 2) {
1119 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1120 return 1;
1121 }
1122 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1123 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1124 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001125
1126 if (1 == FLAGS_key.count() % 2) {
1127 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1128 return 1;
1129 }
1130 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001131 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001132 }
mtklein60317d0f2014-07-14 11:30:37 -07001133
mtkleinf3723212014-06-25 14:08:00 -07001134 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001135 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001136
cdaltone1b89582015-06-25 19:17:08 -07001137 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001138
bsalomon6eb03cc2014-08-07 14:28:50 -07001139 if (kAutoTuneLoops != FLAGS_loops) {
1140 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001141 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001142 SkDebugf("! -> high variance, ? -> moderate variance\n");
1143 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001144 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001145 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001146 } else {
mtkleind75c4662015-04-30 07:11:22 -07001147 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001148 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001149 }
1150
svaisanenc47635e2016-01-28 06:05:43 -08001151 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001152 create_configs(&configs);
1153
joshualitt3ebd0502016-02-09 07:18:08 -08001154#ifdef THERMAL_MANAGER_SUPPORTED
1155 int tmEnabled, tmThreshold, tmSleepTimeMs, tmTimeoutMs;
1156 if (4 != sscanf(FLAGS_useThermalManager[0], "%d,%d,%d,%d",
1157 &tmEnabled, &tmThreshold, &tmSleepTimeMs, &tmTimeoutMs)) {
1158 SkDebugf("Can't parse %s from --useThermalManager.\n", FLAGS_useThermalManager[0]);
1159 exit(1);
1160 }
1161 ThermalManager tm(tmThreshold, tmSleepTimeMs, tmTimeoutMs);
1162#endif
1163
msarettc149f0e2016-01-04 11:35:43 -08001164 if (FLAGS_keepAlive) {
1165 start_keepalive();
1166 }
1167
mtkleine070c2b2014-10-14 08:40:43 -07001168 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001169 BenchmarkStream benchStream;
1170 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001171 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001172 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001173 continue;
1174 }
1175
svaisanenc47635e2016-01-28 06:05:43 -08001176 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001177 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001178 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001179 }
egdaniel3bf92062015-06-26 08:12:46 -07001180 for (int i = 0; i < configs.count(); ++i) {
joshualitt3ebd0502016-02-09 07:18:08 -08001181#ifdef THERMAL_MANAGER_SUPPORTED
1182 if (tmEnabled && !tm.coolOffIfNecessary()) {
1183 SkDebugf("Could not cool off, timings will be throttled\n");
1184 }
1185#endif
egdaniel3bf92062015-06-26 08:12:46 -07001186 Target* target = is_enabled(b, configs[i]);
1187 if (!target) {
1188 continue;
1189 }
mtkleinf3723212014-06-25 14:08:00 -07001190
halcanary96fcdcc2015-08-27 07:41:13 -07001191 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001192 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001193 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001194
brianosman7a5ada82016-02-08 13:49:12 -08001195 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001196 SkDebugf("Running %s\t%s\n"
1197 , bench->getUniqueName()
1198 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001199 if (FLAGS_dryRun) {
1200 continue;
1201 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001202 }
1203
egdaniel3bf92062015-06-26 08:12:46 -07001204 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001205 bench->perCanvasPreDraw(canvas);
1206
cdaltone1b89582015-06-25 19:17:08 -07001207 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001208 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001209 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1210 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001211
mtkleinbbba1682015-10-28 11:36:30 -07001212 if (FLAGS_ms) {
1213 samples.reset();
1214 auto stop = now_ms() + FLAGS_ms;
1215 do {
1216 samples.push_back(time(loops, bench, target) / loops);
1217 } while (now_ms() < stop);
1218 } else {
cdaltone1b89582015-06-25 19:17:08 -07001219 samples.reset(FLAGS_samples);
1220 for (int s = 0; s < FLAGS_samples; s++) {
egdaniel3bf92062015-06-26 08:12:46 -07001221 samples[s] = time(loops, bench, target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001222 }
cdaltone1b89582015-06-25 19:17:08 -07001223 }
mtkleinf3723212014-06-25 14:08:00 -07001224
joshualitte45c81c2015-12-02 09:05:37 -08001225#if SK_SUPPORT_GPU
1226 SkTArray<SkString> keys;
1227 SkTArray<double> values;
1228 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1229 if (gpuStatsDump) {
1230 // TODO cache stats
1231 bench->getGpuStats(canvas, &keys, &values);
1232 }
1233#endif
1234
robertphillips5b693772014-11-21 06:19:36 -08001235 bench->perCanvasPostDraw(canvas);
1236
egdaniel3bf92062015-06-26 08:12:46 -07001237 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001238 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001239 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001240 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001241 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001242 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001243 }
1244
1245 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001246 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001247 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001248 continue;
1249 }
1250
cdaltone1b89582015-06-25 19:17:08 -07001251 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001252 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001253 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001254 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001255 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001256 log->metric("min_ms", stats.min);
joshualitte45c81c2015-12-02 09:05:37 -08001257#if SK_SUPPORT_GPU
1258 if (gpuStatsDump) {
1259 // dump to json, only SKPBench currently returns valid keys / values
1260 SkASSERT(keys.count() == values.count());
1261 for (int i = 0; i < keys.count(); i++) {
1262 log->metric(keys[i].c_str(), values[i]);
1263 }
1264 }
1265#endif
1266
mtkleine070c2b2014-10-14 08:40:43 -07001267 if (runs++ % FLAGS_flushEvery == 0) {
1268 log->flush();
1269 }
mtklein60317d0f2014-07-14 11:30:37 -07001270
bsalomon6eb03cc2014-08-07 14:28:50 -07001271 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001272 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001273 config = ""; // Only print the config if we run the same bench on more than one.
1274 }
mtkleind75c4662015-04-30 07:11:22 -07001275 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1276 , sk_tools::getCurrResidentSetSizeMB()
1277 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001278 , bench->getUniqueName()
1279 , config);
mtkleinf3723212014-06-25 14:08:00 -07001280 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001281 const char* mark = " ";
1282 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1283 if (stddev_percent > 5) mark = "?";
1284 if (stddev_percent > 10) mark = "!";
1285
1286 SkDebugf("%10.2f %s\t%s\t%s\n",
1287 stats.median*1e3, mark, bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001288 } else {
1289 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001290 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1291 , sk_tools::getCurrResidentSetSizeMB()
1292 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001293 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001294 , HUMANIZE(stats.min)
1295 , HUMANIZE(stats.median)
1296 , HUMANIZE(stats.mean)
1297 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001298 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001299 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001300 , config
mtklein96289052014-09-10 12:05:59 -07001301 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001302 );
1303 }
joshualitte45c81c2015-12-02 09:05:37 -08001304
bsalomonb12ea412015-02-02 21:19:50 -08001305#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001306 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001307 GrContext* context = gGrFactory->get(configs[i].ctxType,
kkinnunen3e980c32015-12-23 01:33:00 -08001308 configs[i].ctxOptions);
kkinnunen5219fd92015-12-10 06:28:13 -08001309 context->printCacheStats();
1310 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001311 }
1312#endif
joshualitte45c81c2015-12-02 09:05:37 -08001313
cdalton2c56ba52015-06-26 13:32:53 -07001314 if (FLAGS_verbose) {
1315 SkDebugf("Samples: ");
1316 for (int i = 0; i < samples.count(); i++) {
1317 SkDebugf("%s ", HUMANIZE(samples[i]));
1318 }
1319 SkDebugf("%s\n", bench->getUniqueName());
1320 }
egdaniel3bf92062015-06-26 08:12:46 -07001321 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001322 }
mtkleinf3723212014-06-25 14:08:00 -07001323 }
1324
mtkleine1091452014-12-04 10:47:02 -08001325 log->bench("memory_usage", 0,0);
1326 log->config("meta");
1327 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1328
joshualitte0b19d42015-03-26 10:41:02 -07001329#if SK_SUPPORT_GPU
1330 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1331 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001332 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001333#endif
1334
mtkleinf3723212014-06-25 14:08:00 -07001335 return 0;
1336}
1337
jcgregorio3b27ade2014-11-13 08:06:40 -08001338#if !defined SK_BUILD_FOR_IOS
1339int main(int argc, char** argv) {
1340 SkCommandLineFlags::Parse(argc, argv);
1341 return nanobench_main();
1342}
1343#endif