blob: a7e9ebb855a4d8d8558ecbacc3650b3c01001a44 [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"
mtklein20840502014-08-21 15:51:22 -070037#include "SkOSFile.h"
38#include "SkPictureRecorder.h"
mtklein051e56d2014-12-04 08:46:51 -080039#include "SkPictureUtils.h"
mtkleinf3723212014-06-25 14:08:00 -070040#include "SkString.h"
41#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080042#include "SkTaskGroup.h"
msarettc149f0e2016-01-04 11:35:43 -080043#include "SkThreadUtils.h"
joshualitt3ebd0502016-02-09 07:18:08 -080044#include "ThermalManager.h"
mtkleinf3723212014-06-25 14:08:00 -070045
bungeman60e0fee2015-08-26 05:15:46 -070046#include <stdlib.h>
47
scroggo38ce0a72016-01-07 07:28:47 -080048#ifndef SK_BUILD_FOR_WIN32
49 #include <unistd.h>
50#endif
51
tomhudsond968a6f2015-03-26 11:28:06 -070052#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
53 #include "nanobenchAndroid.h"
54#endif
55
mtkleinbb6a0282014-07-01 08:43:42 -070056#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070057 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070058 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070059 #include "GrContextFactory.h"
bsalomon3724e572016-03-30 18:56:19 -070060 #include "gl/GrGLUtil.h"
61 using sk_gpu_test::GrContextFactory;
bsalomonc8699322016-05-11 11:55:36 -070062 using sk_gpu_test::TestContext;
krajcevski69a55602014-08-13 10:46:31 -070063 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070064#endif
65
bsalomon682c2692015-05-22 14:01:46 -070066 struct GrContextOptions;
67
mtkleinf3723212014-06-25 14:08:00 -070068__SK_FORCE_IMAGE_DECODER_LINKING;
69
reed53249782014-10-10 09:09:52 -070070static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070071
mtkleinb5110422014-08-07 15:20:02 -070072static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070073#ifdef SK_DEBUG
74 1;
mtkleina189ccd2014-07-14 12:28:47 -070075#else
bsalomon6eb03cc2014-08-07 14:28:50 -070076 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070077#endif
78
bsalomon6eb03cc2014-08-07 14:28:50 -070079static SkString loops_help_txt() {
80 SkString help;
81 help.printf("Number of times to run each bench. Set this to %d to auto-"
82 "tune for each bench. Timings are only reported when auto-tuning.",
83 kAutoTuneLoops);
84 return help;
85}
86
cdaltone1b89582015-06-25 19:17:08 -070087static SkString to_string(int n) {
88 SkString str;
89 str.appendS32(n);
90 return str;
91}
92
bsalomon6eb03cc2014-08-07 14:28:50 -070093DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
94
mtkleinf3723212014-06-25 14:08:00 -070095DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -070096DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -070097DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
98DEFINE_double(overheadGoal, 0.0001,
99 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -0700100DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -0700101DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -0700102
mtklein60317d0f2014-07-14 11:30:37 -0700103DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -0700104DEFINE_int32(maxCalibrationAttempts, 3,
105 "Try up to this many times to guess loops for a bench, or skip the bench.");
106DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700107DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
108DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700109DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
110 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700111DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
robertphillips5b693772014-11-21 06:19:36 -0800112DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700113DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700114DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800115DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800116DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800117DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
msarettc149f0e2016-01-04 11:35:43 -0800118DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
joshualitt3ebd0502016-02-09 07:18:08 -0800119DEFINE_string(useThermalManager, "0,1,10,1000", "enabled,threshold,sleepTimeMs,TimeoutMs for "
120 "thermalManager\n");
mtklein92007582014-08-01 07:46:52 -0700121
mtklein65dfd2f2016-02-03 10:40:54 -0800122DEFINE_string(sourceType, "",
123 "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
124DEFINE_string(benchType, "",
125 "Apply usual --match rules to bench type: micro, recording, playback, skcodec, etc.");
126
mtkleinbbba1682015-10-28 11:36:30 -0700127static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
128
mtkleinf3723212014-06-25 14:08:00 -0700129static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700130 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800131 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700132}
mtklein55b0ffc2014-07-17 08:38:23 -0700133#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700134
tomhudsond968a6f2015-03-26 11:28:06 -0700135bool Target::init(SkImageInfo info, Benchmark* bench) {
136 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700137 this->surface = SkSurface::MakeRaster(info);
138 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700139 return false;
140 }
141 }
142 return true;
143}
144bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700145 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700146 if (!canvas) {
147 return false;
148 }
149 bmp->setInfo(canvas->imageInfo());
150 if (!canvas->readPixels(bmp, 0, 0)) {
151 SkDebugf("Can't read canvas pixels.\n");
152 return false;
153 }
154 return true;
155}
156
157#if SK_SUPPORT_GPU
158struct GPUTarget : public Target {
bsalomonc8699322016-05-11 11:55:36 -0700159 explicit GPUTarget(const Config& c) : Target(c), context(nullptr) { }
160 TestContext* context;
tomhudsond968a6f2015-03-26 11:28:06 -0700161
162 void setup() override {
bsalomonc8699322016-05-11 11:55:36 -0700163 this->context->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700164 // Make sure we're done with whatever came before.
bsalomonc8699322016-05-11 11:55:36 -0700165 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700166 }
167 void endTiming() override {
bsalomonc8699322016-05-11 11:55:36 -0700168 if (this->context) {
169 this->context->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700170 }
171 }
172 void fence() override {
bsalomonc8699322016-05-11 11:55:36 -0700173 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700174 }
mtkleind75c4662015-04-30 07:11:22 -0700175
cdaltond416a5b2015-06-23 13:23:44 -0700176 bool needsFrameTiming(int* maxFrameLag) const override {
bsalomonc8699322016-05-11 11:55:36 -0700177 if (!this->context->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700178 // Frame lag is unknown.
179 *maxFrameLag = FLAGS_gpuFrameLag;
180 }
181 return true;
182 }
tomhudsond968a6f2015-03-26 11:28:06 -0700183 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700184 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
185 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700186 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700187 this->surface = SkSurface::MakeRenderTarget(gGrFactory->get(this->config.ctxType,
188 this->config.ctxOptions),
bsalomon5ec26ae2016-02-25 08:33:02 -0800189 SkBudgeted::kNo, info,
reede8f30622016-03-23 18:59:25 -0700190 this->config.samples, &props);
bsalomonc8699322016-05-11 11:55:36 -0700191 this->context = gGrFactory->getContextInfo(this->config.ctxType,
192 this->config.ctxOptions).testContext();
tomhudsond968a6f2015-03-26 11:28:06 -0700193 if (!this->surface.get()) {
194 return false;
195 }
bsalomonc8699322016-05-11 11:55:36 -0700196 if (!this->context->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700197 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800198 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700199 }
tomhudsond968a6f2015-03-26 11:28:06 -0700200 return true;
201 }
202 void fillOptions(ResultsWriter* log) override {
203 const GrGLubyte* version;
bsalomonc8699322016-05-11 11:55:36 -0700204 if (this->context->backend() == kOpenGL_GrBackend) {
205 const GrGLInterface* gl =
206 reinterpret_cast<const GrGLInterface*>(this->context->backendContext());
207 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
208 log->configOption("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700209
bsalomonc8699322016-05-11 11:55:36 -0700210 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
211 log->configOption("GL_RENDERER", (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_VENDOR));
214 log->configOption("GL_VENDOR", (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_SHADING_LANGUAGE_VERSION));
217 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
218 }
tomhudsond968a6f2015-03-26 11:28:06 -0700219 }
220};
mtkleind75c4662015-04-30 07:11:22 -0700221
tomhudsond968a6f2015-03-26 11:28:06 -0700222#endif
223
tomhudson75a0ebb2015-03-27 12:11:44 -0700224static double time(int loops, Benchmark* bench, Target* target) {
225 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700226 if (canvas) {
227 canvas->clear(SK_ColorWHITE);
228 }
joshualitt8a6697a2015-09-30 12:11:07 -0700229 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700230 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700231 canvas = target->beginTiming(canvas);
232 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700233 if (canvas) {
234 canvas->flush();
235 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700236 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700237 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700238 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700239 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700240}
241
mtkleinf3723212014-06-25 14:08:00 -0700242static double estimate_timer_overhead() {
243 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700244 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700245 double start = now_ms();
246 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700247 }
248 return overhead / FLAGS_overheadLoops;
249}
250
reed53249782014-10-10 09:09:52 -0700251static int detect_forever_loops(int loops) {
252 // look for a magic run-forever value
253 if (loops < 0) {
254 loops = SK_MaxS32;
255 }
256 return loops;
257}
258
mtklein55b0ffc2014-07-17 08:38:23 -0700259static int clamp_loops(int loops) {
260 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800261 SkDebugf("ERROR: clamping loops from %d to 1. "
262 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700263 return 1;
264 }
265 if (loops > FLAGS_maxLoops) {
266 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
267 return FLAGS_maxLoops;
268 }
269 return loops;
270}
271
tomhudsond968a6f2015-03-26 11:28:06 -0700272static bool write_canvas_png(Target* target, const SkString& filename) {
273
bsalomon6eb03cc2014-08-07 14:28:50 -0700274 if (filename.isEmpty()) {
275 return false;
276 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700277 if (target->getCanvas() &&
278 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700279 return false;
280 }
tomhudsond968a6f2015-03-26 11:28:06 -0700281
bsalomon6eb03cc2014-08-07 14:28:50 -0700282 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700283
284 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700285 return false;
286 }
tomhudsond968a6f2015-03-26 11:28:06 -0700287
bsalomon6eb03cc2014-08-07 14:28:50 -0700288 SkString dir = SkOSPath::Dirname(filename.c_str());
289 if (!sk_mkdir(dir.c_str())) {
290 SkDebugf("Can't make dir %s.\n", dir.c_str());
291 return false;
292 }
293 SkFILEWStream stream(filename.c_str());
294 if (!stream.isValid()) {
295 SkDebugf("Can't write %s.\n", filename.c_str());
296 return false;
297 }
298 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
299 SkDebugf("Can't encode a PNG.\n");
300 return false;
301 }
302 return true;
303}
304
305static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700306static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700307 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700308 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700309 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700310 int loops = bench->calculateLoops(FLAGS_loops);
311 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700312 while (bench_plus_overhead < overhead) {
313 if (round++ == FLAGS_maxCalibrationAttempts) {
314 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700315 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700316 return kFailedLoops;
317 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700318 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700319 }
mtklein2069e222014-08-04 13:57:39 -0700320 }
mtkleinf3723212014-06-25 14:08:00 -0700321
mtkleinbb6a0282014-07-01 08:43:42 -0700322 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700323 // We'll pick N to make timer overhead negligible:
324 //
mtkleinbb6a0282014-07-01 08:43:42 -0700325 // overhead
326 // ------------------------- < FLAGS_overheadGoal
327 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700328 //
mtkleinbb6a0282014-07-01 08:43:42 -0700329 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700330 //
331 // Doing some math, we get:
332 //
mtkleinbb6a0282014-07-01 08:43:42 -0700333 // (overhead / FLAGS_overheadGoal) - overhead
334 // ------------------------------------------ < N
335 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700336 //
337 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700338 if (kAutoTuneLoops == loops) {
339 const double numer = overhead / FLAGS_overheadGoal - overhead;
340 const double denom = bench_plus_overhead - overhead;
341 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700342 loops = clamp_loops(loops);
343 } else {
344 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700345 }
mtkleinbb6a0282014-07-01 08:43:42 -0700346
mtkleinbb6a0282014-07-01 08:43:42 -0700347 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700348}
349
cdaltone1b89582015-06-25 19:17:08 -0700350static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700351 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700352 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700353 if (kAutoTuneLoops == loops) {
354 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700355 double elapsed = 0;
356 do {
mtklein527930f2014-11-06 08:04:34 -0800357 if (1<<30 == loops) {
358 // We're about to wrap. Something's wrong with the bench.
359 loops = 0;
360 break;
361 }
mtkleina189ccd2014-07-14 12:28:47 -0700362 loops *= 2;
363 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700364 // _this_ round, not still timing last round.
365 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700366 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700367 }
368 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700369
mtkleina189ccd2014-07-14 12:28:47 -0700370 // We've overshot at least a little. Scale back linearly.
371 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700372 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700373
tomhudsond968a6f2015-03-26 11:28:06 -0700374 // Make sure we're not still timing our calibration.
375 target->fence();
reed53249782014-10-10 09:09:52 -0700376 } else {
377 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700378 }
mtkleinbb6a0282014-07-01 08:43:42 -0700379
380 // Pretty much the same deal as the calibration: do some warmup to make
381 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700382 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700383 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700384 }
mtkleinbb6a0282014-07-01 08:43:42 -0700385
mtkleinbb6a0282014-07-01 08:43:42 -0700386 return loops;
387}
mtkleinbb6a0282014-07-01 08:43:42 -0700388
bsalomonc2553372014-07-22 13:09:05 -0700389#if SK_SUPPORT_GPU
bsalomon85b4b532016-04-05 11:06:27 -0700390#define kBogusContextType GrContextFactory::kNativeGL_ContextType
391#define kBogusContextOptions GrContextFactory::kNone_ContextOptions
bsalomonc2553372014-07-22 13:09:05 -0700392#else
bsalomon85b4b532016-04-05 11:06:27 -0700393#define kBogusContextType 0
394#define kBogusContextOptions 0
mtkleine714e752014-07-31 12:13:48 -0700395#endif
bsalomonc2553372014-07-22 13:09:05 -0700396
svaisanenc47635e2016-01-28 06:05:43 -0800397static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
398
399#if SK_SUPPORT_GPU
400 if (const auto* gpuConfig = config->asConfigGpu()) {
401 if (!FLAGS_gpu)
402 return;
403
bsalomon85b4b532016-04-05 11:06:27 -0700404 const auto ctxOptions = gpuConfig->getUseNVPR() ? GrContextFactory::kEnableNVPR_ContextOptions
405 : GrContextFactory::kNone_ContextOptions;
svaisanenc47635e2016-01-28 06:05:43 -0800406 const auto ctxType = gpuConfig->getContextType();
407 const auto sampleCount = gpuConfig->getSamples();
408
409 if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOptions)) {
410 const auto maxSampleCount = ctx->caps()->maxSampleCount();
411 if (sampleCount > ctx->caps()->maxSampleCount()) {
412 SkDebugf("Configuration sample count %d exceeds maximum %d.\n",
413 sampleCount, maxSampleCount);
414 return;
415 }
416 } else {
417 SkDebugf("No context was available matching config type and options.\n");
418 return;
419 }
420
421 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800422 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800423 Benchmark::kGPU_Backend,
424 kN32_SkColorType,
425 kPremul_SkAlphaType,
mtkleinbb6c41b2016-03-08 11:31:11 -0800426 kLinear_SkColorProfileType,
svaisanenc47635e2016-01-28 06:05:43 -0800427 sampleCount,
428 ctxType,
kkinnunend4e1c352016-03-01 23:41:26 -0800429 ctxOptions,
430 gpuConfig->getUseDIText()
431 };
svaisanenc47635e2016-01-28 06:05:43 -0800432
433 configs->push_back(target);
434 return;
435 }
436#endif
437
mtkleinbb6c41b2016-03-08 11:31:11 -0800438 #define CPU_CONFIG(name, backend, color, alpha, profile) \
439 if (config->getTag().equals(#name)) { \
440 Config config = { \
441 SkString(#name), Benchmark::backend, color, alpha, profile, \
bsalomon85b4b532016-04-05 11:06:27 -0700442 0, kBogusContextType, kBogusContextOptions, false \
mtkleinbb6c41b2016-03-08 11:31:11 -0800443 }; \
444 configs->push_back(config); \
445 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700446 }
mtkleine714e752014-07-31 12:13:48 -0700447
mtklein40b32be2014-07-09 08:46:49 -0700448 if (FLAGS_cpu) {
mtkleinbb6c41b2016-03-08 11:31:11 -0800449 CPU_CONFIG(nonrendering, kNonRendering_Backend,
450 kUnknown_SkColorType, kUnpremul_SkAlphaType, kLinear_SkColorProfileType);
451
452 CPU_CONFIG(8888, kRaster_Backend,
453 kN32_SkColorType, kPremul_SkAlphaType, kLinear_SkColorProfileType)
454 CPU_CONFIG(565, kRaster_Backend,
455 kRGB_565_SkColorType, kOpaque_SkAlphaType, kLinear_SkColorProfileType)
456 CPU_CONFIG(srgb, kRaster_Backend,
457 kN32_SkColorType, kPremul_SkAlphaType, kSRGB_SkColorProfileType)
458 CPU_CONFIG(f16, kRaster_Backend,
459 kRGBA_F16_SkColorType, kPremul_SkAlphaType, kLinear_SkColorProfileType)
mtklein40b32be2014-07-09 08:46:49 -0700460 }
mtkleinbb6a0282014-07-01 08:43:42 -0700461
svaisanenc47635e2016-01-28 06:05:43 -0800462 #undef CPU_CONFIG
tomhudsond968a6f2015-03-26 11:28:06 -0700463
464#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
djsollen817c72a2016-01-28 13:57:02 -0800465 if (config->getTag().equals("hwui")) {
mtkleinbb6c41b2016-03-08 11:31:11 -0800466 Config config = { SkString("hwui"), Benchmark::kHWUI_Backend,
467 kRGBA_8888_SkColorType, kPremul_SkAlphaType, kLinear_SkColorProfileType,
bsalomon85b4b532016-04-05 11:06:27 -0700468 0, kBogusContextType, kBogusContextOptions, false };
svaisanenc47635e2016-01-28 06:05:43 -0800469 configs->push_back(config);
tomhudsond968a6f2015-03-26 11:28:06 -0700470 }
471#endif
mtkleinf3723212014-06-25 14:08:00 -0700472}
473
svaisanenc47635e2016-01-28 06:05:43 -0800474// Append all configs that are enabled and supported.
475void create_configs(SkTArray<Config>* configs) {
476 SkCommandLineConfigArray array;
477 ParseConfigs(FLAGS_config, &array);
478 for (int i = 0; i < array.count(); ++i) {
479 create_config(array[i], configs);
480 }
481}
482
halcanary96fcdcc2015-08-27 07:41:13 -0700483// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700484static Target* is_enabled(Benchmark* bench, const Config& config) {
485 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700486 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700487 }
488
reede5ea5002014-09-03 11:54:58 -0700489 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
mtkleinbb6c41b2016-03-08 11:31:11 -0800490 config.color, config.alpha, config.profile);
bsalomonc2553372014-07-22 13:09:05 -0700491
halcanary96fcdcc2015-08-27 07:41:13 -0700492 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700493
tomhudsond968a6f2015-03-26 11:28:06 -0700494 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700495#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700496 case Benchmark::kGPU_Backend:
497 target = new GPUTarget(config);
498 break;
bsalomonc2553372014-07-22 13:09:05 -0700499#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700500#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
501 case Benchmark::kHWUI_Backend:
502 target = new HWUITarget(config, bench);
503 break;
504#endif
505 default:
506 target = new Target(config);
507 break;
508 }
bsalomonc2553372014-07-22 13:09:05 -0700509
tomhudsond968a6f2015-03-26 11:28:06 -0700510 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700511 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700512 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700513 }
514 return target;
515}
516
msarettd1227a72016-05-18 06:23:57 -0700517static bool valid_brd_bench(SkData* encoded, SkColorType colorType, uint32_t sampleSize,
518 uint32_t minOutputSize, int* width, int* height) {
msarett5cb48852015-11-06 08:56:32 -0800519 SkAutoTDelete<SkBitmapRegionDecoder> brd(
msarettd1227a72016-05-18 06:23:57 -0700520 SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy));
msarett7f691442015-09-22 11:56:16 -0700521 if (nullptr == brd.get()) {
522 // This is indicates that subset decoding is not supported for a particular image format.
523 return false;
524 }
525
msarett7f691442015-09-22 11:56:16 -0700526 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
527 (uint32_t) brd->height()) {
528 // This indicates that the image is not large enough to decode a
529 // minOutputSize x minOutputSize subset at the given sampleSize.
530 return false;
531 }
532
533 // Set the image width and height. The calling code will use this to choose subsets to decode.
534 *width = brd->width();
535 *height = brd->height();
536 return true;
537}
538
egdaniel3bf92062015-06-26 08:12:46 -0700539static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700540 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700541#if SK_SUPPORT_GPU
542 if (FLAGS_abandonGpuContext) {
543 gGrFactory->abandonContexts();
544 }
545 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
546 gGrFactory->destroyContexts();
547 }
548#endif
549}
550
mtkleine714e752014-07-31 12:13:48 -0700551class BenchmarkStream {
552public:
mtklein92007582014-08-01 07:46:52 -0700553 BenchmarkStream() : fBenches(BenchRegistry::Head())
554 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700555 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700556 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800557 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800558 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700559 , fCurrentCodec(0)
msarett84451022016-02-11 06:45:51 -0800560 , fCurrentAndroidCodec(0)
msarett7f691442015-09-22 11:56:16 -0700561 , fCurrentBRDImage(0)
msarett2cee9022016-06-03 08:25:21 -0700562 , fCurrentColorImage(0)
msarett95f192d2015-02-13 09:05:41 -0800563 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800564 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700565 , fCurrentSubsetType(0)
msarett84451022016-02-11 06:45:51 -0800566 , fCurrentSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700567 , fCurrentAnimSKP(0) {
mtklein92007582014-08-01 07:46:52 -0700568 for (int i = 0; i < FLAGS_skps.count(); i++) {
569 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
570 fSKPs.push_back() = FLAGS_skps[i];
571 } else {
572 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
573 SkString path;
574 while (it.next(&path)) {
575 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
576 }
577 }
578 }
mtkleine714e752014-07-31 12:13:48 -0700579
mtklein92007582014-08-01 07:46:52 -0700580 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
581 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
582 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
583 exit(1);
584 }
585
586 for (int i = 0; i < FLAGS_scales.count(); i++) {
587 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
588 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
589 exit(1);
590 }
591 }
robertphillips5b693772014-11-21 06:19:36 -0800592
cdalton63a82852015-06-29 14:06:10 -0700593 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
594 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700595 exit(1);
596 }
597
robertphillips5b693772014-11-21 06:19:36 -0800598 if (FLAGS_mpd) {
599 fUseMPDs.push_back() = true;
600 }
mtkleinc751ecb2015-06-15 08:56:38 -0700601 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700602
msarett95f192d2015-02-13 09:05:41 -0800603 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700604 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800605 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800606 }
msarett2cee9022016-06-03 08:25:21 -0700607 if (!CollectImages(FLAGS_colorImages, &fColorImages)) {
608 exit(1);
609 }
mtklein95553d92015-03-12 08:24:21 -0700610
msarett95f192d2015-02-13 09:05:41 -0800611 // Choose the candidate color types for image decoding
612 const SkColorType colorTypes[] =
msarettb23e6aa2015-06-09 13:56:10 -0700613 { kN32_SkColorType,
614 kRGB_565_SkColorType,
615 kAlpha_8_SkColorType,
616 kIndex_8_SkColorType,
617 kGray_8_SkColorType };
msarett74deb982015-10-20 16:45:56 -0700618 fColorTypes.reset(colorTypes, SK_ARRAY_COUNT(colorTypes));
mtklein92007582014-08-01 07:46:52 -0700619 }
620
reedca2622b2016-03-18 07:25:55 -0700621 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700622 // Not strictly necessary, as it will be checked again later,
623 // but helps to avoid a lot of pointless work if we're going to skip it.
cdalton91e457d2016-02-17 11:10:16 -0800624 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700625 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700626 }
627
scroggoa1193e42015-01-21 12:09:53 -0800628 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -0700629 if (stream.get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700630 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700631 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700632 }
633
reedca2622b2016-03-18 07:25:55 -0700634 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700635 }
636
mtklein92007582014-08-01 07:46:52 -0700637 Benchmark* next() {
mtklein65dfd2f2016-02-03 10:40:54 -0800638 SkAutoTDelete<Benchmark> bench;
639 do {
640 bench.reset(this->rawNext());
641 if (!bench) {
642 return nullptr;
643 }
644 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
645 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700646 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800647 }
648
649 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700650 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700651 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700652 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700653 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700654 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700655 return bench;
656 }
mtklein92007582014-08-01 07:46:52 -0700657
mtkleine714e752014-07-31 12:13:48 -0700658 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -0700659 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700660 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800661 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700662 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700663 fBenchType = "micro";
mtklein18300a32016-03-16 13:53:35 -0700664 return new GMBench(gm.release());
mtkleine714e752014-07-31 12:13:48 -0700665 }
666 }
mtklein92007582014-08-01 07:46:52 -0700667
mtkleinfd731ce2014-09-10 12:19:30 -0700668 // First add all .skps as RecordingBenches.
669 while (fCurrentRecording < fSKPs.count()) {
670 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700671 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
672 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700673 continue;
674 }
675 SkString name = SkOSPath::Basename(path.c_str());
676 fSourceType = "skp";
677 fBenchType = "recording";
reedca2622b2016-03-18 07:25:55 -0700678 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic.get()));
mtklein051e56d2014-12-04 08:46:51 -0800679 fSKPOps = pic->approximateOpCount();
halcanary385fe4d2015-08-26 13:07:48 -0700680 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700681 }
682
683 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700684 while (fCurrentScale < fScales.count()) {
685 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800686 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700687 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
688 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800689 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700690 continue;
691 }
robertphillips5b693772014-11-21 06:19:36 -0800692
693 while (fCurrentUseMPD < fUseMPDs.count()) {
694 if (FLAGS_bbh) {
695 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
696 SkRTreeFactory factory;
697 SkPictureRecorder recorder;
698 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
699 pic->playback(recorder.beginRecording(pic->cullRect().width(),
700 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800701 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800702 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
reedca2622b2016-03-18 07:25:55 -0700703 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800704 }
705 SkString name = SkOSPath::Basename(path.c_str());
706 fSourceType = "skp";
707 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700708 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
709 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700710 }
robertphillips5b693772014-11-21 06:19:36 -0800711 fCurrentUseMPD = 0;
712 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700713 }
714 fCurrentSKP = 0;
715 fCurrentScale++;
716 }
717
joshualitt261c3ad2015-04-27 09:16:57 -0700718 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700719 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700720 while (fCurrentAnimSKP < fSKPs.count()) {
721 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700722 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
723 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700724 fCurrentAnimSKP++;
725 continue;
726 }
727
728 fCurrentAnimSKP++;
729 SkString name = SkOSPath::Basename(path.c_str());
cdalton63a82852015-06-29 14:06:10 -0700730 SkAutoTUnref<SKPAnimationBench::Animation> animation(
731 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
halcanary385fe4d2015-08-26 13:07:48 -0700732 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation,
733 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700734 }
735 }
736
scroggo60869a42015-04-01 12:09:17 -0700737 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700738 fSourceType = "image";
739 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700740 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800741 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
742 continue;
743 }
scroggo60869a42015-04-01 12:09:17 -0700744 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
745 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700746 if (!codec) {
747 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700748 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700749 continue;
750 }
scroggo21027992015-04-02 13:22:38 -0700751
scroggo60869a42015-04-01 12:09:17 -0700752 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700753 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700754
msarettc7796b92016-01-07 14:20:20 -0800755 SkAlphaType alphaType = codec->getInfo().alphaType();
756 switch (alphaType) {
757 case kOpaque_SkAlphaType:
758 // We only need to test one alpha type (opaque).
759 fCurrentColorType++;
760 break;
761 case kUnpremul_SkAlphaType:
762 case kPremul_SkAlphaType:
763 if (0 == fCurrentAlphaType) {
764 // Test unpremul first.
765 alphaType = kUnpremul_SkAlphaType;
766 fCurrentAlphaType++;
767 } else {
768 // Test premul.
769 alphaType = kPremul_SkAlphaType;
770 fCurrentAlphaType = 0;
771 fCurrentColorType++;
772 }
773 break;
774 default:
775 SkASSERT(false);
776 fCurrentColorType++;
777 break;
scroggo21027992015-04-02 13:22:38 -0700778 }
779
msarettc7796b92016-01-07 14:20:20 -0800780 // Make sure we can decode to this color type and alpha type.
781 SkImageInfo info =
782 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700783 const size_t rowBytes = info.minRowBytes();
784 SkAutoMalloc storage(info.getSafeSize(rowBytes));
785
786 // Used if fCurrentColorType is kIndex_8_SkColorType
787 int colorCount = 256;
788 SkPMColor colors[256];
789
scroggoeb602a52015-07-09 08:16:03 -0700790 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700791 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700792 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700793 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700794 case SkCodec::kSuccess:
795 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700796 return new CodecBench(SkOSPath::Basename(path.c_str()),
msarettc7796b92016-01-07 14:20:20 -0800797 encoded, colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700798 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700799 // This is okay. Not all conversions are valid.
800 break;
scroggo60869a42015-04-01 12:09:17 -0700801 default:
802 // This represents some sort of failure.
803 SkASSERT(false);
804 break;
805 }
806 }
807 fCurrentColorType = 0;
808 }
809
msarett84451022016-02-11 06:45:51 -0800810 // Run AndroidCodecBenches
811 const int sampleSizes[] = { 2, 4, 8 };
812 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
813 fSourceType = "image";
814 fBenchType = "skandroidcodec";
815
816 const SkString& path = fImages[fCurrentAndroidCodec];
817 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
818 continue;
819 }
820 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
821 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
822 if (!codec) {
823 // Nothing to time.
824 SkDebugf("Cannot find codec for %s\n", path.c_str());
825 continue;
826 }
827
828 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
829 int sampleSize = sampleSizes[fCurrentSampleSize];
830 fCurrentSampleSize++;
831 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
832 // Avoid benchmarking scaled decodes of already small images.
833 break;
834 }
835
836 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()), encoded, sampleSize);
837 }
838 fCurrentSampleSize = 0;
839 }
840
msarett7f691442015-09-22 11:56:16 -0700841 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700842 // We intend to create benchmarks that model the use cases in
843 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
844 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
845 // the image. For that reason, we will benchmark decodes in five representative locations
846 // in the image. Additionally, this use case utilizes power of two scaling, so we will
847 // test on power of two sample sizes. The output tile is always 512x512, so, when a
848 // sampleSize is used, the size of the subset that is decoded is always
849 // (sampleSize*512)x(sampleSize*512).
850 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700851 // All use cases we are aware of only scale by powers of two.
852 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
853 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800854 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700855 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800856 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700857 fSourceType = "image";
858 fBenchType = "BRD";
859
mtklein6f0ff912016-01-11 09:04:21 -0800860 const SkString& path = fImages[fCurrentBRDImage];
861 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
862 continue;
863 }
scroggo860e8a62015-10-15 07:51:28 -0700864
msarettd1227a72016-05-18 06:23:57 -0700865 while (fCurrentColorType < fColorTypes.count()) {
866 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
867 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700868
msarettd1227a72016-05-18 06:23:57 -0700869 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
870 const SkColorType colorType = fColorTypes[fCurrentColorType];
871 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
872 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -0700873
msarettd1227a72016-05-18 06:23:57 -0700874 int width = 0;
875 int height = 0;
876 if (!valid_brd_bench(encoded.get(), colorType, sampleSize, minOutputSize,
877 &width, &height)) {
878 break;
msarett7f691442015-09-22 11:56:16 -0700879 }
msarettd1227a72016-05-18 06:23:57 -0700880
881 SkString basename = SkOSPath::Basename(path.c_str());
882 SkIRect subset;
883 const uint32_t subsetSize = sampleSize * minOutputSize;
884 switch (currentSubsetType) {
885 case kTopLeft_SubsetType:
886 basename.append("_TopLeft");
887 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
888 break;
889 case kTopRight_SubsetType:
890 basename.append("_TopRight");
891 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
892 subsetSize);
893 break;
894 case kMiddle_SubsetType:
895 basename.append("_Middle");
896 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
897 (height - subsetSize) / 2, subsetSize, subsetSize);
898 break;
899 case kBottomLeft_SubsetType:
900 basename.append("_BottomLeft");
901 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
902 subsetSize);
903 break;
904 case kBottomRight_SubsetType:
905 basename.append("_BottomRight");
906 subset = SkIRect::MakeXYWH(width - subsetSize,
907 height - subsetSize, subsetSize, subsetSize);
908 break;
909 default:
910 SkASSERT(false);
911 }
912
913 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
914 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -0700915 }
msarettd1227a72016-05-18 06:23:57 -0700916 fCurrentSubsetType = 0;
917 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -0700918 }
msarettd1227a72016-05-18 06:23:57 -0700919 fCurrentSampleSize = 0;
920 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -0700921 }
msarettd1227a72016-05-18 06:23:57 -0700922 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -0700923 }
924
msarett2cee9022016-06-03 08:25:21 -0700925 while (fCurrentColorImage < fColorImages.count()) {
926 fSourceType = "colorimage";
927 fBenchType = "skcolorcodec";
928 const SkString& path = fColorImages[fCurrentColorImage];
929 fCurrentColorImage++;
930 sk_sp<SkData> encoded = SkData::MakeFromFileName(path.c_str());
931 if (encoded) {
932 return new ColorCodecBench(SkOSPath::Basename(path.c_str()).c_str(),
933 std::move(encoded));
934 } else {
935 SkDebugf("Could not read file %s.\n", path.c_str());
936 }
937 }
938
halcanary96fcdcc2015-08-27 07:41:13 -0700939 return nullptr;
mtkleine714e752014-07-31 12:13:48 -0700940 }
mtklein92007582014-08-01 07:46:52 -0700941
942 void fillCurrentOptions(ResultsWriter* log) const {
943 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700944 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700945 if (0 == strcmp(fSourceType, "skp")) {
946 log->configOption("clip",
947 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
948 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -0800949 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -0700950 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -0800951 if (fCurrentUseMPD > 0) {
952 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
953 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
954 }
mtklein92007582014-08-01 07:46:52 -0700955 }
mtklein051e56d2014-12-04 08:46:51 -0800956 if (0 == strcmp(fBenchType, "recording")) {
957 log->metric("bytes", fSKPBytes);
958 log->metric("ops", fSKPOps);
959 }
mtklein92007582014-08-01 07:46:52 -0700960 }
961
mtkleine714e752014-07-31 12:13:48 -0700962private:
msarettb23e6aa2015-06-09 13:56:10 -0700963 enum SubsetType {
964 kTopLeft_SubsetType = 0,
965 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -0700966 kMiddle_SubsetType = 2,
967 kBottomLeft_SubsetType = 3,
968 kBottomRight_SubsetType = 4,
969 kTranslate_SubsetType = 5,
970 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -0700971 kLast_SubsetType = kZoom_SubsetType,
972 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -0700973 };
974
mtkleine714e752014-07-31 12:13:48 -0700975 const BenchRegistry* fBenches;
976 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700977 SkIRect fClip;
978 SkTArray<SkScalar> fScales;
979 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -0800980 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -0800981 SkTArray<SkString> fImages;
msarett2cee9022016-06-03 08:25:21 -0700982 SkTArray<SkString> fColorImages;
msarett74deb982015-10-20 16:45:56 -0700983 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -0700984 SkScalar fZoomMax;
985 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -0700986
mtklein051e56d2014-12-04 08:46:51 -0800987 double fSKPBytes, fSKPOps;
988
mtkleinfd731ce2014-09-10 12:19:30 -0700989 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
990 const char* fBenchType; // How we bench it: micro, recording, playback, ...
991 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700992 int fCurrentScale;
993 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -0800994 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -0700995 int fCurrentCodec;
msarett84451022016-02-11 06:45:51 -0800996 int fCurrentAndroidCodec;
msarett7f691442015-09-22 11:56:16 -0700997 int fCurrentBRDImage;
msarett2cee9022016-06-03 08:25:21 -0700998 int fCurrentColorImage;
msarett95f192d2015-02-13 09:05:41 -0800999 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -08001000 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -07001001 int fCurrentSubsetType;
msarett84451022016-02-11 06:45:51 -08001002 int fCurrentSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001003 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001004};
1005
msarettc149f0e2016-01-04 11:35:43 -08001006// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1007// This prints something every once in a while so that it knows we're still working.
1008static void start_keepalive() {
1009 struct Loop {
1010 static void forever(void*) {
1011 for (;;) {
1012 static const int kSec = 1200;
1013 #if defined(SK_BUILD_FOR_WIN)
1014 Sleep(kSec * 1000);
1015 #else
1016 sleep(kSec);
1017 #endif
1018 SkDebugf("\nBenchmarks still running...\n");
1019 }
1020 }
1021 };
1022 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1023 intentionallyLeaked->start();
1024}
1025
reedb8486f52016-05-26 14:06:43 -07001026extern bool gDefaultProfileIsSRGB;
1027
jcgregorio3b27ade2014-11-13 08:06:40 -08001028int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001029int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001030 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001031 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001032 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001033
krajcevski69a55602014-08-13 10:46:31 -07001034#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001035 GrContextOptions grContextOpts;
halcanary385fe4d2015-08-26 13:07:48 -07001036 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001037#endif
1038
reedb8486f52016-05-26 14:06:43 -07001039 if (FLAGS_forceSRGB) {
1040 gDefaultProfileIsSRGB = true;
1041 }
1042
bsalomon06cddec2014-10-24 10:40:50 -07001043 if (FLAGS_veryVerbose) {
1044 FLAGS_verbose = true;
1045 }
1046
bsalomon6eb03cc2014-08-07 14:28:50 -07001047 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001048 FLAGS_samples = 1;
1049 FLAGS_gpuFrameLag = 0;
1050 }
1051
bsalomon6eb03cc2014-08-07 14:28:50 -07001052 if (!FLAGS_writePath.isEmpty()) {
1053 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1054 if (!sk_mkdir(FLAGS_writePath[0])) {
1055 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001056 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001057 }
1058 }
1059
halcanary385fe4d2015-08-26 13:07:48 -07001060 SkAutoTDelete<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001061 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001062#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001063 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001064#else
1065 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1066 return 1;
1067#endif
mtklein60317d0f2014-07-14 11:30:37 -07001068 }
mtklein1915b622014-08-20 11:45:00 -07001069
1070 if (1 == FLAGS_properties.count() % 2) {
1071 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1072 return 1;
1073 }
1074 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1075 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1076 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001077
1078 if (1 == FLAGS_key.count() % 2) {
1079 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1080 return 1;
1081 }
1082 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001083 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001084 }
mtklein60317d0f2014-07-14 11:30:37 -07001085
mtkleinf3723212014-06-25 14:08:00 -07001086 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001087 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001088
cdaltone1b89582015-06-25 19:17:08 -07001089 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001090
bsalomon6eb03cc2014-08-07 14:28:50 -07001091 if (kAutoTuneLoops != FLAGS_loops) {
1092 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001093 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001094 SkDebugf("! -> high variance, ? -> moderate variance\n");
1095 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001096 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001097 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001098 } else {
mtkleind75c4662015-04-30 07:11:22 -07001099 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001100 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001101 }
1102
svaisanenc47635e2016-01-28 06:05:43 -08001103 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001104 create_configs(&configs);
1105
joshualitt3ebd0502016-02-09 07:18:08 -08001106#ifdef THERMAL_MANAGER_SUPPORTED
1107 int tmEnabled, tmThreshold, tmSleepTimeMs, tmTimeoutMs;
1108 if (4 != sscanf(FLAGS_useThermalManager[0], "%d,%d,%d,%d",
1109 &tmEnabled, &tmThreshold, &tmSleepTimeMs, &tmTimeoutMs)) {
1110 SkDebugf("Can't parse %s from --useThermalManager.\n", FLAGS_useThermalManager[0]);
1111 exit(1);
1112 }
1113 ThermalManager tm(tmThreshold, tmSleepTimeMs, tmTimeoutMs);
1114#endif
1115
msarettc149f0e2016-01-04 11:35:43 -08001116 if (FLAGS_keepAlive) {
1117 start_keepalive();
1118 }
1119
mtkleine070c2b2014-10-14 08:40:43 -07001120 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001121 BenchmarkStream benchStream;
1122 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001123 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001124 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001125 continue;
1126 }
1127
svaisanenc47635e2016-01-28 06:05:43 -08001128 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001129 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001130 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001131 }
egdaniel3bf92062015-06-26 08:12:46 -07001132 for (int i = 0; i < configs.count(); ++i) {
joshualitt3ebd0502016-02-09 07:18:08 -08001133#ifdef THERMAL_MANAGER_SUPPORTED
1134 if (tmEnabled && !tm.coolOffIfNecessary()) {
1135 SkDebugf("Could not cool off, timings will be throttled\n");
1136 }
1137#endif
egdaniel3bf92062015-06-26 08:12:46 -07001138 Target* target = is_enabled(b, configs[i]);
1139 if (!target) {
1140 continue;
1141 }
mtkleinf3723212014-06-25 14:08:00 -07001142
halcanary96fcdcc2015-08-27 07:41:13 -07001143 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001144 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001145 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001146
brianosman7a5ada82016-02-08 13:49:12 -08001147 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001148 SkDebugf("Running %s\t%s\n"
1149 , bench->getUniqueName()
1150 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001151 if (FLAGS_dryRun) {
1152 continue;
1153 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001154 }
1155
egdaniel3bf92062015-06-26 08:12:46 -07001156 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001157 bench->perCanvasPreDraw(canvas);
1158
cdaltone1b89582015-06-25 19:17:08 -07001159 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001160 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001161 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1162 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001163
mtkleinbbba1682015-10-28 11:36:30 -07001164 if (FLAGS_ms) {
1165 samples.reset();
1166 auto stop = now_ms() + FLAGS_ms;
1167 do {
1168 samples.push_back(time(loops, bench, target) / loops);
1169 } while (now_ms() < stop);
1170 } else {
cdaltone1b89582015-06-25 19:17:08 -07001171 samples.reset(FLAGS_samples);
1172 for (int s = 0; s < FLAGS_samples; s++) {
egdaniel3bf92062015-06-26 08:12:46 -07001173 samples[s] = time(loops, bench, target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001174 }
cdaltone1b89582015-06-25 19:17:08 -07001175 }
mtkleinf3723212014-06-25 14:08:00 -07001176
joshualitte45c81c2015-12-02 09:05:37 -08001177#if SK_SUPPORT_GPU
1178 SkTArray<SkString> keys;
1179 SkTArray<double> values;
1180 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1181 if (gpuStatsDump) {
1182 // TODO cache stats
1183 bench->getGpuStats(canvas, &keys, &values);
1184 }
1185#endif
1186
robertphillips5b693772014-11-21 06:19:36 -08001187 bench->perCanvasPostDraw(canvas);
1188
egdaniel3bf92062015-06-26 08:12:46 -07001189 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001190 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001191 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001192 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001193 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001194 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001195 }
1196
1197 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001198 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001199 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001200 continue;
1201 }
1202
cdaltone1b89582015-06-25 19:17:08 -07001203 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001204 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001205 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001206 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001207 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001208 log->metric("min_ms", stats.min);
joshualitte45c81c2015-12-02 09:05:37 -08001209#if SK_SUPPORT_GPU
1210 if (gpuStatsDump) {
1211 // dump to json, only SKPBench currently returns valid keys / values
1212 SkASSERT(keys.count() == values.count());
1213 for (int i = 0; i < keys.count(); i++) {
1214 log->metric(keys[i].c_str(), values[i]);
1215 }
1216 }
1217#endif
1218
mtkleine070c2b2014-10-14 08:40:43 -07001219 if (runs++ % FLAGS_flushEvery == 0) {
1220 log->flush();
1221 }
mtklein60317d0f2014-07-14 11:30:37 -07001222
bsalomon6eb03cc2014-08-07 14:28:50 -07001223 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001224 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001225 config = ""; // Only print the config if we run the same bench on more than one.
1226 }
mtkleind75c4662015-04-30 07:11:22 -07001227 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1228 , sk_tools::getCurrResidentSetSizeMB()
1229 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001230 , bench->getUniqueName()
1231 , config);
mtkleinf3723212014-06-25 14:08:00 -07001232 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001233 const char* mark = " ";
1234 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1235 if (stddev_percent > 5) mark = "?";
1236 if (stddev_percent > 10) mark = "!";
1237
1238 SkDebugf("%10.2f %s\t%s\t%s\n",
1239 stats.median*1e3, mark, bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001240 } else {
1241 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001242 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1243 , sk_tools::getCurrResidentSetSizeMB()
1244 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001245 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001246 , HUMANIZE(stats.min)
1247 , HUMANIZE(stats.median)
1248 , HUMANIZE(stats.mean)
1249 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001250 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001251 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001252 , config
mtklein96289052014-09-10 12:05:59 -07001253 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001254 );
1255 }
joshualitte45c81c2015-12-02 09:05:37 -08001256
bsalomonb12ea412015-02-02 21:19:50 -08001257#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001258 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001259 GrContext* context = gGrFactory->get(configs[i].ctxType,
kkinnunen3e980c32015-12-23 01:33:00 -08001260 configs[i].ctxOptions);
kkinnunen5219fd92015-12-10 06:28:13 -08001261 context->printCacheStats();
1262 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001263 }
1264#endif
joshualitte45c81c2015-12-02 09:05:37 -08001265
cdalton2c56ba52015-06-26 13:32:53 -07001266 if (FLAGS_verbose) {
1267 SkDebugf("Samples: ");
1268 for (int i = 0; i < samples.count(); i++) {
1269 SkDebugf("%s ", HUMANIZE(samples[i]));
1270 }
1271 SkDebugf("%s\n", bench->getUniqueName());
1272 }
egdaniel3bf92062015-06-26 08:12:46 -07001273 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001274 }
mtkleinf3723212014-06-25 14:08:00 -07001275 }
1276
mtkleine1091452014-12-04 10:47:02 -08001277 log->bench("memory_usage", 0,0);
1278 log->config("meta");
1279 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1280
joshualitte0b19d42015-03-26 10:41:02 -07001281#if SK_SUPPORT_GPU
1282 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1283 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001284 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001285#endif
1286
mtkleinf3723212014-06-25 14:08:00 -07001287 return 0;
1288}
1289
jcgregorio3b27ade2014-11-13 08:06:40 -08001290#if !defined SK_BUILD_FOR_IOS
1291int main(int argc, char** argv) {
1292 SkCommandLineFlags::Parse(argc, argv);
1293 return nanobench_main();
1294}
1295#endif