blob: 4fb1ed223abdb481f2546c0e7e0083ab65747773 [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"
mtkleinf3723212014-06-25 14:08:00 -070017#include "CrashHandler.h"
mtkleine714e752014-07-31 12:13:48 -070018#include "GMBench.h"
mtkleinafb43792014-08-19 15:55:55 -070019#include "ProcStats.h"
mtklein60317d0f2014-07-14 11:30:37 -070020#include "ResultsWriter.h"
mtkleinfd731ce2014-09-10 12:19:30 -070021#include "RecordingBench.h"
joshualitt261c3ad2015-04-27 09:16:57 -070022#include "SKPAnimationBench.h"
mtklein92007582014-08-01 07:46:52 -070023#include "SKPBench.h"
mtkleinf3723212014-06-25 14:08:00 -070024#include "Stats.h"
mtkleinf3723212014-06-25 14:08:00 -070025
msarett84451022016-02-11 06:45:51 -080026#include "SkAndroidCodec.h"
msarett5cb48852015-11-06 08:56:32 -080027#include "SkBitmapRegionDecoder.h"
mtklein6838d852014-10-29 14:15:10 -070028#include "SkBBoxHierarchy.h"
mtkleinf3723212014-06-25 14:08:00 -070029#include "SkCanvas.h"
scroggo60869a42015-04-01 12:09:17 -070030#include "SkCodec.h"
caryclark17f0b6d2014-07-22 10:15:34 -070031#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080032#include "SkCommonFlagsConfig.h"
msarett95f192d2015-02-13 09:05:41 -080033#include "SkData.h"
mtkleinf3723212014-06-25 14:08:00 -070034#include "SkForceLinking.h"
35#include "SkGraphics.h"
mtklein20840502014-08-21 15:51:22 -070036#include "SkOSFile.h"
37#include "SkPictureRecorder.h"
mtklein051e56d2014-12-04 08:46:51 -080038#include "SkPictureUtils.h"
mtkleinf3723212014-06-25 14:08:00 -070039#include "SkString.h"
40#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080041#include "SkTaskGroup.h"
msarettc149f0e2016-01-04 11:35:43 -080042#include "SkThreadUtils.h"
joshualitt3ebd0502016-02-09 07:18:08 -080043#include "ThermalManager.h"
mtkleinf3723212014-06-25 14:08:00 -070044
bungeman60e0fee2015-08-26 05:15:46 -070045#include <stdlib.h>
46
scroggo38ce0a72016-01-07 07:28:47 -080047#ifndef SK_BUILD_FOR_WIN32
48 #include <unistd.h>
49#endif
50
tomhudsond968a6f2015-03-26 11:28:06 -070051#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
52 #include "nanobenchAndroid.h"
53#endif
54
mtkleinbb6a0282014-07-01 08:43:42 -070055#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070056 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070057 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070058 #include "GrContextFactory.h"
bsalomon3724e572016-03-30 18:56:19 -070059 #include "gl/GrGLUtil.h"
60 using sk_gpu_test::GrContextFactory;
bsalomonc8699322016-05-11 11:55:36 -070061 using sk_gpu_test::TestContext;
krajcevski69a55602014-08-13 10:46:31 -070062 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070063#endif
64
bsalomon682c2692015-05-22 14:01:46 -070065 struct GrContextOptions;
66
mtkleinf3723212014-06-25 14:08:00 -070067__SK_FORCE_IMAGE_DECODER_LINKING;
68
reed53249782014-10-10 09:09:52 -070069static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070070
mtkleinb5110422014-08-07 15:20:02 -070071static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070072#ifdef SK_DEBUG
73 1;
mtkleina189ccd2014-07-14 12:28:47 -070074#else
bsalomon6eb03cc2014-08-07 14:28:50 -070075 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070076#endif
77
bsalomon6eb03cc2014-08-07 14:28:50 -070078static SkString loops_help_txt() {
79 SkString help;
80 help.printf("Number of times to run each bench. Set this to %d to auto-"
81 "tune for each bench. Timings are only reported when auto-tuning.",
82 kAutoTuneLoops);
83 return help;
84}
85
cdaltone1b89582015-06-25 19:17:08 -070086static SkString to_string(int n) {
87 SkString str;
88 str.appendS32(n);
89 return str;
90}
91
bsalomon6eb03cc2014-08-07 14:28:50 -070092DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
93
mtkleinf3723212014-06-25 14:08:00 -070094DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -070095DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -070096DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
97DEFINE_double(overheadGoal, 0.0001,
98 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070099DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -0700100DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -0700101
mtklein60317d0f2014-07-14 11:30:37 -0700102DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -0700103DEFINE_int32(maxCalibrationAttempts, 3,
104 "Try up to this many times to guess loops for a bench, or skip the bench.");
105DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700106DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
107DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700108DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
109 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700110DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
robertphillips5b693772014-11-21 06:19:36 -0800111DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700112DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700113DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800114DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800115DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800116DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
msarettc149f0e2016-01-04 11:35:43 -0800117DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
joshualitt3ebd0502016-02-09 07:18:08 -0800118DEFINE_string(useThermalManager, "0,1,10,1000", "enabled,threshold,sleepTimeMs,TimeoutMs for "
119 "thermalManager\n");
mtklein92007582014-08-01 07:46:52 -0700120
mtklein65dfd2f2016-02-03 10:40:54 -0800121DEFINE_string(sourceType, "",
122 "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
123DEFINE_string(benchType, "",
124 "Apply usual --match rules to bench type: micro, recording, playback, skcodec, etc.");
125
mtkleinbbba1682015-10-28 11:36:30 -0700126static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
127
mtkleinf3723212014-06-25 14:08:00 -0700128static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700129 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800130 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700131}
mtklein55b0ffc2014-07-17 08:38:23 -0700132#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700133
tomhudsond968a6f2015-03-26 11:28:06 -0700134bool Target::init(SkImageInfo info, Benchmark* bench) {
135 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700136 this->surface = SkSurface::MakeRaster(info);
137 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700138 return false;
139 }
140 }
141 return true;
142}
143bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700144 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700145 if (!canvas) {
146 return false;
147 }
148 bmp->setInfo(canvas->imageInfo());
149 if (!canvas->readPixels(bmp, 0, 0)) {
150 SkDebugf("Can't read canvas pixels.\n");
151 return false;
152 }
153 return true;
154}
155
156#if SK_SUPPORT_GPU
157struct GPUTarget : public Target {
bsalomonc8699322016-05-11 11:55:36 -0700158 explicit GPUTarget(const Config& c) : Target(c), context(nullptr) { }
159 TestContext* context;
tomhudsond968a6f2015-03-26 11:28:06 -0700160
161 void setup() override {
bsalomonc8699322016-05-11 11:55:36 -0700162 this->context->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700163 // Make sure we're done with whatever came before.
bsalomonc8699322016-05-11 11:55:36 -0700164 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700165 }
166 void endTiming() override {
bsalomonc8699322016-05-11 11:55:36 -0700167 if (this->context) {
168 this->context->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700169 }
170 }
171 void fence() override {
bsalomonc8699322016-05-11 11:55:36 -0700172 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700173 }
mtkleind75c4662015-04-30 07:11:22 -0700174
cdaltond416a5b2015-06-23 13:23:44 -0700175 bool needsFrameTiming(int* maxFrameLag) const override {
bsalomonc8699322016-05-11 11:55:36 -0700176 if (!this->context->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700177 // Frame lag is unknown.
178 *maxFrameLag = FLAGS_gpuFrameLag;
179 }
180 return true;
181 }
tomhudsond968a6f2015-03-26 11:28:06 -0700182 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700183 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
184 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700185 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700186 this->surface = SkSurface::MakeRenderTarget(gGrFactory->get(this->config.ctxType,
187 this->config.ctxOptions),
bsalomon5ec26ae2016-02-25 08:33:02 -0800188 SkBudgeted::kNo, info,
reede8f30622016-03-23 18:59:25 -0700189 this->config.samples, &props);
bsalomonc8699322016-05-11 11:55:36 -0700190 this->context = gGrFactory->getContextInfo(this->config.ctxType,
191 this->config.ctxOptions).testContext();
tomhudsond968a6f2015-03-26 11:28:06 -0700192 if (!this->surface.get()) {
193 return false;
194 }
bsalomonc8699322016-05-11 11:55:36 -0700195 if (!this->context->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700196 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800197 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700198 }
tomhudsond968a6f2015-03-26 11:28:06 -0700199 return true;
200 }
201 void fillOptions(ResultsWriter* log) override {
202 const GrGLubyte* version;
bsalomonc8699322016-05-11 11:55:36 -0700203 if (this->context->backend() == kOpenGL_GrBackend) {
204 const GrGLInterface* gl =
205 reinterpret_cast<const GrGLInterface*>(this->context->backendContext());
206 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
207 log->configOption("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700208
bsalomonc8699322016-05-11 11:55:36 -0700209 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
210 log->configOption("GL_RENDERER", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700211
bsalomonc8699322016-05-11 11:55:36 -0700212 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VENDOR));
213 log->configOption("GL_VENDOR", (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_SHADING_LANGUAGE_VERSION));
216 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
217 }
tomhudsond968a6f2015-03-26 11:28:06 -0700218 }
219};
mtkleind75c4662015-04-30 07:11:22 -0700220
tomhudsond968a6f2015-03-26 11:28:06 -0700221#endif
222
tomhudson75a0ebb2015-03-27 12:11:44 -0700223static double time(int loops, Benchmark* bench, Target* target) {
224 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700225 if (canvas) {
226 canvas->clear(SK_ColorWHITE);
227 }
joshualitt8a6697a2015-09-30 12:11:07 -0700228 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700229 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700230 canvas = target->beginTiming(canvas);
231 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700232 if (canvas) {
233 canvas->flush();
234 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700235 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700236 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700237 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700238 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700239}
240
mtkleinf3723212014-06-25 14:08:00 -0700241static double estimate_timer_overhead() {
242 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700243 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700244 double start = now_ms();
245 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700246 }
247 return overhead / FLAGS_overheadLoops;
248}
249
reed53249782014-10-10 09:09:52 -0700250static int detect_forever_loops(int loops) {
251 // look for a magic run-forever value
252 if (loops < 0) {
253 loops = SK_MaxS32;
254 }
255 return loops;
256}
257
mtklein55b0ffc2014-07-17 08:38:23 -0700258static int clamp_loops(int loops) {
259 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800260 SkDebugf("ERROR: clamping loops from %d to 1. "
261 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700262 return 1;
263 }
264 if (loops > FLAGS_maxLoops) {
265 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
266 return FLAGS_maxLoops;
267 }
268 return loops;
269}
270
tomhudsond968a6f2015-03-26 11:28:06 -0700271static bool write_canvas_png(Target* target, const SkString& filename) {
272
bsalomon6eb03cc2014-08-07 14:28:50 -0700273 if (filename.isEmpty()) {
274 return false;
275 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700276 if (target->getCanvas() &&
277 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700278 return false;
279 }
tomhudsond968a6f2015-03-26 11:28:06 -0700280
bsalomon6eb03cc2014-08-07 14:28:50 -0700281 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700282
283 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700284 return false;
285 }
tomhudsond968a6f2015-03-26 11:28:06 -0700286
bsalomon6eb03cc2014-08-07 14:28:50 -0700287 SkString dir = SkOSPath::Dirname(filename.c_str());
288 if (!sk_mkdir(dir.c_str())) {
289 SkDebugf("Can't make dir %s.\n", dir.c_str());
290 return false;
291 }
292 SkFILEWStream stream(filename.c_str());
293 if (!stream.isValid()) {
294 SkDebugf("Can't write %s.\n", filename.c_str());
295 return false;
296 }
297 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
298 SkDebugf("Can't encode a PNG.\n");
299 return false;
300 }
301 return true;
302}
303
304static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700305static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700306 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700307 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700308 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700309 int loops = bench->calculateLoops(FLAGS_loops);
310 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700311 while (bench_plus_overhead < overhead) {
312 if (round++ == FLAGS_maxCalibrationAttempts) {
313 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700314 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700315 return kFailedLoops;
316 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700317 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700318 }
mtklein2069e222014-08-04 13:57:39 -0700319 }
mtkleinf3723212014-06-25 14:08:00 -0700320
mtkleinbb6a0282014-07-01 08:43:42 -0700321 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700322 // We'll pick N to make timer overhead negligible:
323 //
mtkleinbb6a0282014-07-01 08:43:42 -0700324 // overhead
325 // ------------------------- < FLAGS_overheadGoal
326 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700327 //
mtkleinbb6a0282014-07-01 08:43:42 -0700328 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700329 //
330 // Doing some math, we get:
331 //
mtkleinbb6a0282014-07-01 08:43:42 -0700332 // (overhead / FLAGS_overheadGoal) - overhead
333 // ------------------------------------------ < N
334 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700335 //
336 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700337 if (kAutoTuneLoops == loops) {
338 const double numer = overhead / FLAGS_overheadGoal - overhead;
339 const double denom = bench_plus_overhead - overhead;
340 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700341 loops = clamp_loops(loops);
342 } else {
343 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700344 }
mtkleinbb6a0282014-07-01 08:43:42 -0700345
mtkleinbb6a0282014-07-01 08:43:42 -0700346 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700347}
348
cdaltone1b89582015-06-25 19:17:08 -0700349static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700350 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700351 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700352 if (kAutoTuneLoops == loops) {
353 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700354 double elapsed = 0;
355 do {
mtklein527930f2014-11-06 08:04:34 -0800356 if (1<<30 == loops) {
357 // We're about to wrap. Something's wrong with the bench.
358 loops = 0;
359 break;
360 }
mtkleina189ccd2014-07-14 12:28:47 -0700361 loops *= 2;
362 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700363 // _this_ round, not still timing last round.
364 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700365 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700366 }
367 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700368
mtkleina189ccd2014-07-14 12:28:47 -0700369 // We've overshot at least a little. Scale back linearly.
370 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700371 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700372
tomhudsond968a6f2015-03-26 11:28:06 -0700373 // Make sure we're not still timing our calibration.
374 target->fence();
reed53249782014-10-10 09:09:52 -0700375 } else {
376 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700377 }
mtkleinbb6a0282014-07-01 08:43:42 -0700378
379 // Pretty much the same deal as the calibration: do some warmup to make
380 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700381 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700382 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700383 }
mtkleinbb6a0282014-07-01 08:43:42 -0700384
mtkleinbb6a0282014-07-01 08:43:42 -0700385 return loops;
386}
mtkleinbb6a0282014-07-01 08:43:42 -0700387
bsalomonc2553372014-07-22 13:09:05 -0700388#if SK_SUPPORT_GPU
bsalomon85b4b532016-04-05 11:06:27 -0700389#define kBogusContextType GrContextFactory::kNativeGL_ContextType
390#define kBogusContextOptions GrContextFactory::kNone_ContextOptions
bsalomonc2553372014-07-22 13:09:05 -0700391#else
bsalomon85b4b532016-04-05 11:06:27 -0700392#define kBogusContextType 0
393#define kBogusContextOptions 0
mtkleine714e752014-07-31 12:13:48 -0700394#endif
bsalomonc2553372014-07-22 13:09:05 -0700395
svaisanenc47635e2016-01-28 06:05:43 -0800396static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
397
398#if SK_SUPPORT_GPU
399 if (const auto* gpuConfig = config->asConfigGpu()) {
400 if (!FLAGS_gpu)
401 return;
402
bsalomon85b4b532016-04-05 11:06:27 -0700403 const auto ctxOptions = gpuConfig->getUseNVPR() ? GrContextFactory::kEnableNVPR_ContextOptions
404 : GrContextFactory::kNone_ContextOptions;
svaisanenc47635e2016-01-28 06:05:43 -0800405 const auto ctxType = gpuConfig->getContextType();
406 const auto sampleCount = gpuConfig->getSamples();
407
408 if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOptions)) {
409 const auto maxSampleCount = ctx->caps()->maxSampleCount();
410 if (sampleCount > ctx->caps()->maxSampleCount()) {
411 SkDebugf("Configuration sample count %d exceeds maximum %d.\n",
412 sampleCount, maxSampleCount);
413 return;
414 }
415 } else {
416 SkDebugf("No context was available matching config type and options.\n");
417 return;
418 }
419
420 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800421 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800422 Benchmark::kGPU_Backend,
423 kN32_SkColorType,
424 kPremul_SkAlphaType,
mtkleinbb6c41b2016-03-08 11:31:11 -0800425 kLinear_SkColorProfileType,
svaisanenc47635e2016-01-28 06:05:43 -0800426 sampleCount,
427 ctxType,
kkinnunend4e1c352016-03-01 23:41:26 -0800428 ctxOptions,
429 gpuConfig->getUseDIText()
430 };
svaisanenc47635e2016-01-28 06:05:43 -0800431
432 configs->push_back(target);
433 return;
434 }
435#endif
436
mtkleinbb6c41b2016-03-08 11:31:11 -0800437 #define CPU_CONFIG(name, backend, color, alpha, profile) \
438 if (config->getTag().equals(#name)) { \
439 Config config = { \
440 SkString(#name), Benchmark::backend, color, alpha, profile, \
bsalomon85b4b532016-04-05 11:06:27 -0700441 0, kBogusContextType, kBogusContextOptions, false \
mtkleinbb6c41b2016-03-08 11:31:11 -0800442 }; \
443 configs->push_back(config); \
444 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700445 }
mtkleine714e752014-07-31 12:13:48 -0700446
mtklein40b32be2014-07-09 08:46:49 -0700447 if (FLAGS_cpu) {
mtkleinbb6c41b2016-03-08 11:31:11 -0800448 CPU_CONFIG(nonrendering, kNonRendering_Backend,
449 kUnknown_SkColorType, kUnpremul_SkAlphaType, kLinear_SkColorProfileType);
450
451 CPU_CONFIG(8888, kRaster_Backend,
452 kN32_SkColorType, kPremul_SkAlphaType, kLinear_SkColorProfileType)
453 CPU_CONFIG(565, kRaster_Backend,
454 kRGB_565_SkColorType, kOpaque_SkAlphaType, kLinear_SkColorProfileType)
455 CPU_CONFIG(srgb, kRaster_Backend,
456 kN32_SkColorType, kPremul_SkAlphaType, kSRGB_SkColorProfileType)
457 CPU_CONFIG(f16, kRaster_Backend,
458 kRGBA_F16_SkColorType, kPremul_SkAlphaType, kLinear_SkColorProfileType)
mtklein40b32be2014-07-09 08:46:49 -0700459 }
mtkleinbb6a0282014-07-01 08:43:42 -0700460
svaisanenc47635e2016-01-28 06:05:43 -0800461 #undef CPU_CONFIG
tomhudsond968a6f2015-03-26 11:28:06 -0700462
463#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
djsollen817c72a2016-01-28 13:57:02 -0800464 if (config->getTag().equals("hwui")) {
mtkleinbb6c41b2016-03-08 11:31:11 -0800465 Config config = { SkString("hwui"), Benchmark::kHWUI_Backend,
466 kRGBA_8888_SkColorType, kPremul_SkAlphaType, kLinear_SkColorProfileType,
bsalomon85b4b532016-04-05 11:06:27 -0700467 0, kBogusContextType, kBogusContextOptions, false };
svaisanenc47635e2016-01-28 06:05:43 -0800468 configs->push_back(config);
tomhudsond968a6f2015-03-26 11:28:06 -0700469 }
470#endif
mtkleinf3723212014-06-25 14:08:00 -0700471}
472
svaisanenc47635e2016-01-28 06:05:43 -0800473// Append all configs that are enabled and supported.
474void create_configs(SkTArray<Config>* configs) {
475 SkCommandLineConfigArray array;
476 ParseConfigs(FLAGS_config, &array);
477 for (int i = 0; i < array.count(); ++i) {
478 create_config(array[i], configs);
479 }
480}
481
halcanary96fcdcc2015-08-27 07:41:13 -0700482// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700483static Target* is_enabled(Benchmark* bench, const Config& config) {
484 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700485 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700486 }
487
reede5ea5002014-09-03 11:54:58 -0700488 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
mtkleinbb6c41b2016-03-08 11:31:11 -0800489 config.color, config.alpha, config.profile);
bsalomonc2553372014-07-22 13:09:05 -0700490
halcanary96fcdcc2015-08-27 07:41:13 -0700491 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700492
tomhudsond968a6f2015-03-26 11:28:06 -0700493 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700494#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700495 case Benchmark::kGPU_Backend:
496 target = new GPUTarget(config);
497 break;
bsalomonc2553372014-07-22 13:09:05 -0700498#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700499#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
500 case Benchmark::kHWUI_Backend:
501 target = new HWUITarget(config, bench);
502 break;
503#endif
504 default:
505 target = new Target(config);
506 break;
507 }
bsalomonc2553372014-07-22 13:09:05 -0700508
tomhudsond968a6f2015-03-26 11:28:06 -0700509 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700510 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700511 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700512 }
513 return target;
514}
515
msarettd1227a72016-05-18 06:23:57 -0700516static bool valid_brd_bench(SkData* encoded, SkColorType colorType, uint32_t sampleSize,
517 uint32_t minOutputSize, int* width, int* height) {
msarett5cb48852015-11-06 08:56:32 -0800518 SkAutoTDelete<SkBitmapRegionDecoder> brd(
msarettd1227a72016-05-18 06:23:57 -0700519 SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy));
msarett7f691442015-09-22 11:56:16 -0700520 if (nullptr == brd.get()) {
521 // This is indicates that subset decoding is not supported for a particular image format.
522 return false;
523 }
524
msarett7f691442015-09-22 11:56:16 -0700525 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
526 (uint32_t) brd->height()) {
527 // This indicates that the image is not large enough to decode a
528 // minOutputSize x minOutputSize subset at the given sampleSize.
529 return false;
530 }
531
532 // Set the image width and height. The calling code will use this to choose subsets to decode.
533 *width = brd->width();
534 *height = brd->height();
535 return true;
536}
537
egdaniel3bf92062015-06-26 08:12:46 -0700538static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700539 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700540#if SK_SUPPORT_GPU
541 if (FLAGS_abandonGpuContext) {
542 gGrFactory->abandonContexts();
543 }
544 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
545 gGrFactory->destroyContexts();
546 }
547#endif
548}
549
mtkleine714e752014-07-31 12:13:48 -0700550class BenchmarkStream {
551public:
mtklein92007582014-08-01 07:46:52 -0700552 BenchmarkStream() : fBenches(BenchRegistry::Head())
553 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700554 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700555 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800556 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800557 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700558 , fCurrentCodec(0)
msarett84451022016-02-11 06:45:51 -0800559 , fCurrentAndroidCodec(0)
msarett7f691442015-09-22 11:56:16 -0700560 , fCurrentBRDImage(0)
msarett95f192d2015-02-13 09:05:41 -0800561 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800562 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700563 , fCurrentSubsetType(0)
msarett84451022016-02-11 06:45:51 -0800564 , fCurrentSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700565 , fCurrentAnimSKP(0) {
mtklein92007582014-08-01 07:46:52 -0700566 for (int i = 0; i < FLAGS_skps.count(); i++) {
567 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
568 fSKPs.push_back() = FLAGS_skps[i];
569 } else {
570 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
571 SkString path;
572 while (it.next(&path)) {
573 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
574 }
575 }
576 }
mtkleine714e752014-07-31 12:13:48 -0700577
mtklein92007582014-08-01 07:46:52 -0700578 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
579 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
580 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
581 exit(1);
582 }
583
584 for (int i = 0; i < FLAGS_scales.count(); i++) {
585 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
586 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
587 exit(1);
588 }
589 }
robertphillips5b693772014-11-21 06:19:36 -0800590
cdalton63a82852015-06-29 14:06:10 -0700591 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
592 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700593 exit(1);
594 }
595
robertphillips5b693772014-11-21 06:19:36 -0800596 if (FLAGS_mpd) {
597 fUseMPDs.push_back() = true;
598 }
mtkleinc751ecb2015-06-15 08:56:38 -0700599 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700600
msarett95f192d2015-02-13 09:05:41 -0800601 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700602 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800603 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800604 }
mtklein95553d92015-03-12 08:24:21 -0700605
msarett95f192d2015-02-13 09:05:41 -0800606 // Choose the candidate color types for image decoding
607 const SkColorType colorTypes[] =
msarettb23e6aa2015-06-09 13:56:10 -0700608 { kN32_SkColorType,
609 kRGB_565_SkColorType,
610 kAlpha_8_SkColorType,
611 kIndex_8_SkColorType,
612 kGray_8_SkColorType };
msarett74deb982015-10-20 16:45:56 -0700613 fColorTypes.reset(colorTypes, SK_ARRAY_COUNT(colorTypes));
mtklein92007582014-08-01 07:46:52 -0700614 }
615
reedca2622b2016-03-18 07:25:55 -0700616 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700617 // Not strictly necessary, as it will be checked again later,
618 // but helps to avoid a lot of pointless work if we're going to skip it.
cdalton91e457d2016-02-17 11:10:16 -0800619 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700620 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700621 }
622
scroggoa1193e42015-01-21 12:09:53 -0800623 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -0700624 if (stream.get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700625 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700626 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700627 }
628
reedca2622b2016-03-18 07:25:55 -0700629 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700630 }
631
mtklein92007582014-08-01 07:46:52 -0700632 Benchmark* next() {
mtklein65dfd2f2016-02-03 10:40:54 -0800633 SkAutoTDelete<Benchmark> bench;
634 do {
635 bench.reset(this->rawNext());
636 if (!bench) {
637 return nullptr;
638 }
639 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
640 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700641 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800642 }
643
644 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700645 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700646 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700647 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700648 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700649 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700650 return bench;
651 }
mtklein92007582014-08-01 07:46:52 -0700652
mtkleine714e752014-07-31 12:13:48 -0700653 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -0700654 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700655 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800656 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700657 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700658 fBenchType = "micro";
mtklein18300a32016-03-16 13:53:35 -0700659 return new GMBench(gm.release());
mtkleine714e752014-07-31 12:13:48 -0700660 }
661 }
mtklein92007582014-08-01 07:46:52 -0700662
mtkleinfd731ce2014-09-10 12:19:30 -0700663 // First add all .skps as RecordingBenches.
664 while (fCurrentRecording < fSKPs.count()) {
665 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700666 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
667 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700668 continue;
669 }
670 SkString name = SkOSPath::Basename(path.c_str());
671 fSourceType = "skp";
672 fBenchType = "recording";
reedca2622b2016-03-18 07:25:55 -0700673 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic.get()));
mtklein051e56d2014-12-04 08:46:51 -0800674 fSKPOps = pic->approximateOpCount();
halcanary385fe4d2015-08-26 13:07:48 -0700675 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700676 }
677
678 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700679 while (fCurrentScale < fScales.count()) {
680 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800681 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700682 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
683 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800684 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700685 continue;
686 }
robertphillips5b693772014-11-21 06:19:36 -0800687
688 while (fCurrentUseMPD < fUseMPDs.count()) {
689 if (FLAGS_bbh) {
690 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
691 SkRTreeFactory factory;
692 SkPictureRecorder recorder;
693 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
694 pic->playback(recorder.beginRecording(pic->cullRect().width(),
695 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800696 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800697 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
reedca2622b2016-03-18 07:25:55 -0700698 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800699 }
700 SkString name = SkOSPath::Basename(path.c_str());
701 fSourceType = "skp";
702 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700703 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
704 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700705 }
robertphillips5b693772014-11-21 06:19:36 -0800706 fCurrentUseMPD = 0;
707 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700708 }
709 fCurrentSKP = 0;
710 fCurrentScale++;
711 }
712
joshualitt261c3ad2015-04-27 09:16:57 -0700713 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700714 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700715 while (fCurrentAnimSKP < fSKPs.count()) {
716 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700717 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
718 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700719 fCurrentAnimSKP++;
720 continue;
721 }
722
723 fCurrentAnimSKP++;
724 SkString name = SkOSPath::Basename(path.c_str());
cdalton63a82852015-06-29 14:06:10 -0700725 SkAutoTUnref<SKPAnimationBench::Animation> animation(
726 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
halcanary385fe4d2015-08-26 13:07:48 -0700727 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation,
728 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700729 }
730 }
731
scroggo60869a42015-04-01 12:09:17 -0700732 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700733 fSourceType = "image";
734 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700735 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800736 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
737 continue;
738 }
scroggo60869a42015-04-01 12:09:17 -0700739 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
740 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700741 if (!codec) {
742 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700743 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700744 continue;
745 }
scroggo21027992015-04-02 13:22:38 -0700746
scroggo60869a42015-04-01 12:09:17 -0700747 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700748 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700749
msarettc7796b92016-01-07 14:20:20 -0800750 SkAlphaType alphaType = codec->getInfo().alphaType();
751 switch (alphaType) {
752 case kOpaque_SkAlphaType:
753 // We only need to test one alpha type (opaque).
754 fCurrentColorType++;
755 break;
756 case kUnpremul_SkAlphaType:
757 case kPremul_SkAlphaType:
758 if (0 == fCurrentAlphaType) {
759 // Test unpremul first.
760 alphaType = kUnpremul_SkAlphaType;
761 fCurrentAlphaType++;
762 } else {
763 // Test premul.
764 alphaType = kPremul_SkAlphaType;
765 fCurrentAlphaType = 0;
766 fCurrentColorType++;
767 }
768 break;
769 default:
770 SkASSERT(false);
771 fCurrentColorType++;
772 break;
scroggo21027992015-04-02 13:22:38 -0700773 }
774
msarettc7796b92016-01-07 14:20:20 -0800775 // Make sure we can decode to this color type and alpha type.
776 SkImageInfo info =
777 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700778 const size_t rowBytes = info.minRowBytes();
779 SkAutoMalloc storage(info.getSafeSize(rowBytes));
780
781 // Used if fCurrentColorType is kIndex_8_SkColorType
782 int colorCount = 256;
783 SkPMColor colors[256];
784
scroggoeb602a52015-07-09 08:16:03 -0700785 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700786 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700787 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700788 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700789 case SkCodec::kSuccess:
790 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700791 return new CodecBench(SkOSPath::Basename(path.c_str()),
msarettc7796b92016-01-07 14:20:20 -0800792 encoded, colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700793 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700794 // This is okay. Not all conversions are valid.
795 break;
scroggo60869a42015-04-01 12:09:17 -0700796 default:
797 // This represents some sort of failure.
798 SkASSERT(false);
799 break;
800 }
801 }
802 fCurrentColorType = 0;
803 }
804
msarett84451022016-02-11 06:45:51 -0800805 // Run AndroidCodecBenches
806 const int sampleSizes[] = { 2, 4, 8 };
807 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
808 fSourceType = "image";
809 fBenchType = "skandroidcodec";
810
811 const SkString& path = fImages[fCurrentAndroidCodec];
812 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
813 continue;
814 }
815 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
816 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
817 if (!codec) {
818 // Nothing to time.
819 SkDebugf("Cannot find codec for %s\n", path.c_str());
820 continue;
821 }
822
823 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
824 int sampleSize = sampleSizes[fCurrentSampleSize];
825 fCurrentSampleSize++;
826 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
827 // Avoid benchmarking scaled decodes of already small images.
828 break;
829 }
830
831 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()), encoded, sampleSize);
832 }
833 fCurrentSampleSize = 0;
834 }
835
msarett7f691442015-09-22 11:56:16 -0700836 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700837 // We intend to create benchmarks that model the use cases in
838 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
839 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
840 // the image. For that reason, we will benchmark decodes in five representative locations
841 // in the image. Additionally, this use case utilizes power of two scaling, so we will
842 // test on power of two sample sizes. The output tile is always 512x512, so, when a
843 // sampleSize is used, the size of the subset that is decoded is always
844 // (sampleSize*512)x(sampleSize*512).
845 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700846 // All use cases we are aware of only scale by powers of two.
847 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
848 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800849 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700850 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800851 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700852 fSourceType = "image";
853 fBenchType = "BRD";
854
mtklein6f0ff912016-01-11 09:04:21 -0800855 const SkString& path = fImages[fCurrentBRDImage];
856 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
857 continue;
858 }
scroggo860e8a62015-10-15 07:51:28 -0700859
msarettd1227a72016-05-18 06:23:57 -0700860 while (fCurrentColorType < fColorTypes.count()) {
861 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
862 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700863
msarettd1227a72016-05-18 06:23:57 -0700864 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
865 const SkColorType colorType = fColorTypes[fCurrentColorType];
866 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
867 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -0700868
msarettd1227a72016-05-18 06:23:57 -0700869 int width = 0;
870 int height = 0;
871 if (!valid_brd_bench(encoded.get(), colorType, sampleSize, minOutputSize,
872 &width, &height)) {
873 break;
msarett7f691442015-09-22 11:56:16 -0700874 }
msarettd1227a72016-05-18 06:23:57 -0700875
876 SkString basename = SkOSPath::Basename(path.c_str());
877 SkIRect subset;
878 const uint32_t subsetSize = sampleSize * minOutputSize;
879 switch (currentSubsetType) {
880 case kTopLeft_SubsetType:
881 basename.append("_TopLeft");
882 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
883 break;
884 case kTopRight_SubsetType:
885 basename.append("_TopRight");
886 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
887 subsetSize);
888 break;
889 case kMiddle_SubsetType:
890 basename.append("_Middle");
891 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
892 (height - subsetSize) / 2, subsetSize, subsetSize);
893 break;
894 case kBottomLeft_SubsetType:
895 basename.append("_BottomLeft");
896 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
897 subsetSize);
898 break;
899 case kBottomRight_SubsetType:
900 basename.append("_BottomRight");
901 subset = SkIRect::MakeXYWH(width - subsetSize,
902 height - subsetSize, subsetSize, subsetSize);
903 break;
904 default:
905 SkASSERT(false);
906 }
907
908 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
909 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -0700910 }
msarettd1227a72016-05-18 06:23:57 -0700911 fCurrentSubsetType = 0;
912 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -0700913 }
msarettd1227a72016-05-18 06:23:57 -0700914 fCurrentSampleSize = 0;
915 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -0700916 }
msarettd1227a72016-05-18 06:23:57 -0700917 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -0700918 }
919
halcanary96fcdcc2015-08-27 07:41:13 -0700920 return nullptr;
mtkleine714e752014-07-31 12:13:48 -0700921 }
mtklein92007582014-08-01 07:46:52 -0700922
923 void fillCurrentOptions(ResultsWriter* log) const {
924 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700925 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700926 if (0 == strcmp(fSourceType, "skp")) {
927 log->configOption("clip",
928 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
929 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -0800930 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -0700931 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -0800932 if (fCurrentUseMPD > 0) {
933 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
934 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
935 }
mtklein92007582014-08-01 07:46:52 -0700936 }
mtklein051e56d2014-12-04 08:46:51 -0800937 if (0 == strcmp(fBenchType, "recording")) {
938 log->metric("bytes", fSKPBytes);
939 log->metric("ops", fSKPOps);
940 }
mtklein92007582014-08-01 07:46:52 -0700941 }
942
mtkleine714e752014-07-31 12:13:48 -0700943private:
msarettb23e6aa2015-06-09 13:56:10 -0700944 enum SubsetType {
945 kTopLeft_SubsetType = 0,
946 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -0700947 kMiddle_SubsetType = 2,
948 kBottomLeft_SubsetType = 3,
949 kBottomRight_SubsetType = 4,
950 kTranslate_SubsetType = 5,
951 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -0700952 kLast_SubsetType = kZoom_SubsetType,
953 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -0700954 };
955
mtkleine714e752014-07-31 12:13:48 -0700956 const BenchRegistry* fBenches;
957 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700958 SkIRect fClip;
959 SkTArray<SkScalar> fScales;
960 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -0800961 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -0800962 SkTArray<SkString> fImages;
msarett74deb982015-10-20 16:45:56 -0700963 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -0700964 SkScalar fZoomMax;
965 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -0700966
mtklein051e56d2014-12-04 08:46:51 -0800967 double fSKPBytes, fSKPOps;
968
mtkleinfd731ce2014-09-10 12:19:30 -0700969 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
970 const char* fBenchType; // How we bench it: micro, recording, playback, ...
971 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700972 int fCurrentScale;
973 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -0800974 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -0700975 int fCurrentCodec;
msarett84451022016-02-11 06:45:51 -0800976 int fCurrentAndroidCodec;
msarett7f691442015-09-22 11:56:16 -0700977 int fCurrentBRDImage;
msarett95f192d2015-02-13 09:05:41 -0800978 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -0800979 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -0700980 int fCurrentSubsetType;
msarett84451022016-02-11 06:45:51 -0800981 int fCurrentSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -0700982 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -0700983};
984
msarettc149f0e2016-01-04 11:35:43 -0800985// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
986// This prints something every once in a while so that it knows we're still working.
987static void start_keepalive() {
988 struct Loop {
989 static void forever(void*) {
990 for (;;) {
991 static const int kSec = 1200;
992 #if defined(SK_BUILD_FOR_WIN)
993 Sleep(kSec * 1000);
994 #else
995 sleep(kSec);
996 #endif
997 SkDebugf("\nBenchmarks still running...\n");
998 }
999 }
1000 };
1001 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1002 intentionallyLeaked->start();
1003}
1004
jcgregorio3b27ade2014-11-13 08:06:40 -08001005int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001006int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001007 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001008 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001009 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001010
krajcevski69a55602014-08-13 10:46:31 -07001011#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001012 GrContextOptions grContextOpts;
halcanary385fe4d2015-08-26 13:07:48 -07001013 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001014#endif
1015
bsalomon06cddec2014-10-24 10:40:50 -07001016 if (FLAGS_veryVerbose) {
1017 FLAGS_verbose = true;
1018 }
1019
bsalomon6eb03cc2014-08-07 14:28:50 -07001020 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001021 FLAGS_samples = 1;
1022 FLAGS_gpuFrameLag = 0;
1023 }
1024
bsalomon6eb03cc2014-08-07 14:28:50 -07001025 if (!FLAGS_writePath.isEmpty()) {
1026 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1027 if (!sk_mkdir(FLAGS_writePath[0])) {
1028 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001029 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001030 }
1031 }
1032
halcanary385fe4d2015-08-26 13:07:48 -07001033 SkAutoTDelete<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001034 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001035#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001036 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001037#else
1038 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1039 return 1;
1040#endif
mtklein60317d0f2014-07-14 11:30:37 -07001041 }
mtklein1915b622014-08-20 11:45:00 -07001042
1043 if (1 == FLAGS_properties.count() % 2) {
1044 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1045 return 1;
1046 }
1047 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1048 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1049 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001050
1051 if (1 == FLAGS_key.count() % 2) {
1052 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1053 return 1;
1054 }
1055 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001056 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001057 }
mtklein60317d0f2014-07-14 11:30:37 -07001058
mtkleinf3723212014-06-25 14:08:00 -07001059 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001060 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001061
cdaltone1b89582015-06-25 19:17:08 -07001062 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001063
bsalomon6eb03cc2014-08-07 14:28:50 -07001064 if (kAutoTuneLoops != FLAGS_loops) {
1065 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001066 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001067 SkDebugf("! -> high variance, ? -> moderate variance\n");
1068 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001069 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001070 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001071 } else {
mtkleind75c4662015-04-30 07:11:22 -07001072 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001073 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001074 }
1075
svaisanenc47635e2016-01-28 06:05:43 -08001076 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001077 create_configs(&configs);
1078
joshualitt3ebd0502016-02-09 07:18:08 -08001079#ifdef THERMAL_MANAGER_SUPPORTED
1080 int tmEnabled, tmThreshold, tmSleepTimeMs, tmTimeoutMs;
1081 if (4 != sscanf(FLAGS_useThermalManager[0], "%d,%d,%d,%d",
1082 &tmEnabled, &tmThreshold, &tmSleepTimeMs, &tmTimeoutMs)) {
1083 SkDebugf("Can't parse %s from --useThermalManager.\n", FLAGS_useThermalManager[0]);
1084 exit(1);
1085 }
1086 ThermalManager tm(tmThreshold, tmSleepTimeMs, tmTimeoutMs);
1087#endif
1088
msarettc149f0e2016-01-04 11:35:43 -08001089 if (FLAGS_keepAlive) {
1090 start_keepalive();
1091 }
1092
mtkleine070c2b2014-10-14 08:40:43 -07001093 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001094 BenchmarkStream benchStream;
1095 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001096 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001097 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001098 continue;
1099 }
1100
svaisanenc47635e2016-01-28 06:05:43 -08001101 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001102 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001103 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001104 }
egdaniel3bf92062015-06-26 08:12:46 -07001105 for (int i = 0; i < configs.count(); ++i) {
joshualitt3ebd0502016-02-09 07:18:08 -08001106#ifdef THERMAL_MANAGER_SUPPORTED
1107 if (tmEnabled && !tm.coolOffIfNecessary()) {
1108 SkDebugf("Could not cool off, timings will be throttled\n");
1109 }
1110#endif
egdaniel3bf92062015-06-26 08:12:46 -07001111 Target* target = is_enabled(b, configs[i]);
1112 if (!target) {
1113 continue;
1114 }
mtkleinf3723212014-06-25 14:08:00 -07001115
halcanary96fcdcc2015-08-27 07:41:13 -07001116 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001117 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001118 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001119
brianosman7a5ada82016-02-08 13:49:12 -08001120 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001121 SkDebugf("Running %s\t%s\n"
1122 , bench->getUniqueName()
1123 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001124 if (FLAGS_dryRun) {
1125 continue;
1126 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001127 }
1128
egdaniel3bf92062015-06-26 08:12:46 -07001129 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001130 bench->perCanvasPreDraw(canvas);
1131
cdaltone1b89582015-06-25 19:17:08 -07001132 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001133 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001134 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1135 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001136
mtkleinbbba1682015-10-28 11:36:30 -07001137 if (FLAGS_ms) {
1138 samples.reset();
1139 auto stop = now_ms() + FLAGS_ms;
1140 do {
1141 samples.push_back(time(loops, bench, target) / loops);
1142 } while (now_ms() < stop);
1143 } else {
cdaltone1b89582015-06-25 19:17:08 -07001144 samples.reset(FLAGS_samples);
1145 for (int s = 0; s < FLAGS_samples; s++) {
egdaniel3bf92062015-06-26 08:12:46 -07001146 samples[s] = time(loops, bench, target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001147 }
cdaltone1b89582015-06-25 19:17:08 -07001148 }
mtkleinf3723212014-06-25 14:08:00 -07001149
joshualitte45c81c2015-12-02 09:05:37 -08001150#if SK_SUPPORT_GPU
1151 SkTArray<SkString> keys;
1152 SkTArray<double> values;
1153 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1154 if (gpuStatsDump) {
1155 // TODO cache stats
1156 bench->getGpuStats(canvas, &keys, &values);
1157 }
1158#endif
1159
robertphillips5b693772014-11-21 06:19:36 -08001160 bench->perCanvasPostDraw(canvas);
1161
egdaniel3bf92062015-06-26 08:12:46 -07001162 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001163 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001164 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001165 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001166 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001167 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001168 }
1169
1170 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001171 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001172 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001173 continue;
1174 }
1175
cdaltone1b89582015-06-25 19:17:08 -07001176 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001177 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001178 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001179 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001180 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001181 log->metric("min_ms", stats.min);
joshualitte45c81c2015-12-02 09:05:37 -08001182#if SK_SUPPORT_GPU
1183 if (gpuStatsDump) {
1184 // dump to json, only SKPBench currently returns valid keys / values
1185 SkASSERT(keys.count() == values.count());
1186 for (int i = 0; i < keys.count(); i++) {
1187 log->metric(keys[i].c_str(), values[i]);
1188 }
1189 }
1190#endif
1191
mtkleine070c2b2014-10-14 08:40:43 -07001192 if (runs++ % FLAGS_flushEvery == 0) {
1193 log->flush();
1194 }
mtklein60317d0f2014-07-14 11:30:37 -07001195
bsalomon6eb03cc2014-08-07 14:28:50 -07001196 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001197 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001198 config = ""; // Only print the config if we run the same bench on more than one.
1199 }
mtkleind75c4662015-04-30 07:11:22 -07001200 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1201 , sk_tools::getCurrResidentSetSizeMB()
1202 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001203 , bench->getUniqueName()
1204 , config);
mtkleinf3723212014-06-25 14:08:00 -07001205 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001206 const char* mark = " ";
1207 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1208 if (stddev_percent > 5) mark = "?";
1209 if (stddev_percent > 10) mark = "!";
1210
1211 SkDebugf("%10.2f %s\t%s\t%s\n",
1212 stats.median*1e3, mark, bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001213 } else {
1214 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001215 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1216 , sk_tools::getCurrResidentSetSizeMB()
1217 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001218 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001219 , HUMANIZE(stats.min)
1220 , HUMANIZE(stats.median)
1221 , HUMANIZE(stats.mean)
1222 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001223 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001224 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001225 , config
mtklein96289052014-09-10 12:05:59 -07001226 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001227 );
1228 }
joshualitte45c81c2015-12-02 09:05:37 -08001229
bsalomonb12ea412015-02-02 21:19:50 -08001230#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001231 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001232 GrContext* context = gGrFactory->get(configs[i].ctxType,
kkinnunen3e980c32015-12-23 01:33:00 -08001233 configs[i].ctxOptions);
kkinnunen5219fd92015-12-10 06:28:13 -08001234 context->printCacheStats();
1235 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001236 }
1237#endif
joshualitte45c81c2015-12-02 09:05:37 -08001238
cdalton2c56ba52015-06-26 13:32:53 -07001239 if (FLAGS_verbose) {
1240 SkDebugf("Samples: ");
1241 for (int i = 0; i < samples.count(); i++) {
1242 SkDebugf("%s ", HUMANIZE(samples[i]));
1243 }
1244 SkDebugf("%s\n", bench->getUniqueName());
1245 }
egdaniel3bf92062015-06-26 08:12:46 -07001246 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001247 }
mtkleinf3723212014-06-25 14:08:00 -07001248 }
1249
mtkleine1091452014-12-04 10:47:02 -08001250 log->bench("memory_usage", 0,0);
1251 log->config("meta");
1252 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1253
joshualitte0b19d42015-03-26 10:41:02 -07001254#if SK_SUPPORT_GPU
1255 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1256 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001257 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001258#endif
1259
mtkleinf3723212014-06-25 14:08:00 -07001260 return 0;
1261}
1262
jcgregorio3b27ade2014-11-13 08:06:40 -08001263#if !defined SK_BUILD_FOR_IOS
1264int main(int argc, char** argv) {
1265 SkCommandLineFlags::Parse(argc, argv);
1266 return nanobench_main();
1267}
1268#endif