blob: f73ae464cc5132034f4c9f55acb763269d35da1c [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"
Mike Kleinadacaef2017-02-06 09:26:14 -050026#include "ios_utils.h"
mtkleinf3723212014-06-25 14:08:00 -070027
msarett84451022016-02-11 06:45:51 -080028#include "SkAndroidCodec.h"
Hal Canary95e3c052017-01-11 12:44:43 -050029#include "SkAutoMalloc.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000030#include "SkBBoxHierarchy.h"
Hal Canary95e3c052017-01-11 12:44:43 -050031#include "SkBitmapRegionDecoder.h"
mtkleinf3723212014-06-25 14:08:00 -070032#include "SkCanvas.h"
scroggo60869a42015-04-01 12:09:17 -070033#include "SkCodec.h"
caryclark17f0b6d2014-07-22 10:15:34 -070034#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080035#include "SkCommonFlagsConfig.h"
csmartdalton008b9d82017-02-22 12:00:42 -070036#include "SkCommonFlagsPathRenderer.h"
msarett95f192d2015-02-13 09:05:41 -080037#include "SkData.h"
mtkleinf3723212014-06-25 14:08:00 -070038#include "SkGraphics.h"
halcanary4dbbd042016-06-07 17:21:10 -070039#include "SkLeanWindows.h"
mtklein20840502014-08-21 15:51:22 -070040#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050041#include "SkOSPath.h"
mtklein20840502014-08-21 15:51:22 -070042#include "SkPictureRecorder.h"
mtklein051e56d2014-12-04 08:46:51 -080043#include "SkPictureUtils.h"
Hal Canary95e3c052017-01-11 12:44:43 -050044#include "SkSVGDOM.h"
45#include "SkScan.h"
mtkleinf3723212014-06-25 14:08:00 -070046#include "SkString.h"
47#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080048#include "SkTaskGroup.h"
msarettc149f0e2016-01-04 11:35:43 -080049#include "SkThreadUtils.h"
joshualitt3ebd0502016-02-09 07:18:08 -080050#include "ThermalManager.h"
mtkleinf3723212014-06-25 14:08:00 -070051
bungeman60e0fee2015-08-26 05:15:46 -070052#include <stdlib.h>
53
scroggo38ce0a72016-01-07 07:28:47 -080054#ifndef SK_BUILD_FOR_WIN32
55 #include <unistd.h>
56#endif
57
mtkleinbb6a0282014-07-01 08:43:42 -070058#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070059 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070060 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070061 #include "GrContextFactory.h"
bsalomon3724e572016-03-30 18:56:19 -070062 #include "gl/GrGLUtil.h"
63 using sk_gpu_test::GrContextFactory;
bsalomonc8699322016-05-11 11:55:36 -070064 using sk_gpu_test::TestContext;
Ben Wagner145dbcd2016-11-03 14:40:50 -040065 std::unique_ptr<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070066#endif
67
bsalomon682c2692015-05-22 14:01:46 -070068 struct GrContextOptions;
69
reed53249782014-10-10 09:09:52 -070070static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070071
Mike Kleind8ee67c2017-01-19 12:14:50 -050072#if !defined(__has_feature)
73 #define __has_feature(x) 0
74#endif
75
mtkleinb5110422014-08-07 15:20:02 -070076static const int kDefaultLoops =
Mike Kleind8ee67c2017-01-19 12:14:50 -050077#if defined(SK_DEBUG) || __has_feature(address_sanitizer)
bsalomon6eb03cc2014-08-07 14:28:50 -070078 1;
mtkleina189ccd2014-07-14 12:28:47 -070079#else
bsalomon6eb03cc2014-08-07 14:28:50 -070080 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070081#endif
82
bsalomon6eb03cc2014-08-07 14:28:50 -070083static SkString loops_help_txt() {
84 SkString help;
85 help.printf("Number of times to run each bench. Set this to %d to auto-"
86 "tune for each bench. Timings are only reported when auto-tuning.",
87 kAutoTuneLoops);
88 return help;
89}
90
cdaltone1b89582015-06-25 19:17:08 -070091static SkString to_string(int n) {
92 SkString str;
93 str.appendS32(n);
94 return str;
95}
96
bsalomon6eb03cc2014-08-07 14:28:50 -070097DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
98
mtkleinf3723212014-06-25 14:08:00 -070099DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -0700100DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -0700101DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
102DEFINE_double(overheadGoal, 0.0001,
103 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -0700104DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -0700105DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -0700106
mtklein60317d0f2014-07-14 11:30:37 -0700107DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -0700108DEFINE_int32(maxCalibrationAttempts, 3,
109 "Try up to this many times to guess loops for a bench, or skip the bench.");
110DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700111DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
112DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700113DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
114 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700115DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
mtklein8c1a4f82016-08-08 06:56:22 -0700116DEFINE_bool(lite, false, "Use SkLiteRecorder in recording benchmarks?");
robertphillips5b693772014-11-21 06:19:36 -0800117DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700118DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700119DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800120DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800121DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800122DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
msarettc149f0e2016-01-04 11:35:43 -0800123DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
joshualitt3ebd0502016-02-09 07:18:08 -0800124DEFINE_string(useThermalManager, "0,1,10,1000", "enabled,threshold,sleepTimeMs,TimeoutMs for "
125 "thermalManager\n");
mtklein92007582014-08-01 07:46:52 -0700126
mtklein65dfd2f2016-02-03 10:40:54 -0800127DEFINE_string(sourceType, "",
128 "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
129DEFINE_string(benchType, "",
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400130 "Apply usual --match rules to bench type: micro, recording, piping, playback, skcodec, etc.");
mtklein65dfd2f2016-02-03 10:40:54 -0800131
csmartdalton008b9d82017-02-22 12:00:42 -0700132#if SK_SUPPORT_GPU
133DEFINE_pathrenderer_flag;
134#endif
135
mtkleinbbba1682015-10-28 11:36:30 -0700136static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
137
mtkleinf3723212014-06-25 14:08:00 -0700138static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700139 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800140 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700141}
mtklein55b0ffc2014-07-17 08:38:23 -0700142#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700143
tomhudsond968a6f2015-03-26 11:28:06 -0700144bool Target::init(SkImageInfo info, Benchmark* bench) {
145 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700146 this->surface = SkSurface::MakeRaster(info);
147 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700148 return false;
149 }
150 }
151 return true;
152}
153bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700154 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700155 if (!canvas) {
156 return false;
157 }
158 bmp->setInfo(canvas->imageInfo());
159 if (!canvas->readPixels(bmp, 0, 0)) {
160 SkDebugf("Can't read canvas pixels.\n");
161 return false;
162 }
163 return true;
164}
165
166#if SK_SUPPORT_GPU
167struct GPUTarget : public Target {
bsalomonc8699322016-05-11 11:55:36 -0700168 explicit GPUTarget(const Config& c) : Target(c), context(nullptr) { }
169 TestContext* context;
tomhudsond968a6f2015-03-26 11:28:06 -0700170
171 void setup() override {
bsalomonc8699322016-05-11 11:55:36 -0700172 this->context->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700173 // Make sure we're done with whatever came before.
bsalomonc8699322016-05-11 11:55:36 -0700174 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700175 }
176 void endTiming() override {
bsalomonc8699322016-05-11 11:55:36 -0700177 if (this->context) {
178 this->context->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700179 }
180 }
181 void fence() override {
bsalomonc8699322016-05-11 11:55:36 -0700182 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700183 }
mtkleind75c4662015-04-30 07:11:22 -0700184
cdaltond416a5b2015-06-23 13:23:44 -0700185 bool needsFrameTiming(int* maxFrameLag) const override {
bsalomonc8699322016-05-11 11:55:36 -0700186 if (!this->context->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700187 // Frame lag is unknown.
188 *maxFrameLag = FLAGS_gpuFrameLag;
189 }
190 return true;
191 }
tomhudsond968a6f2015-03-26 11:28:06 -0700192 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700193 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
194 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700195 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700196 this->surface = SkSurface::MakeRenderTarget(gGrFactory->get(this->config.ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700197 this->config.ctxOverrides),
bsalomon5ec26ae2016-02-25 08:33:02 -0800198 SkBudgeted::kNo, info,
reede8f30622016-03-23 18:59:25 -0700199 this->config.samples, &props);
bsalomonc8699322016-05-11 11:55:36 -0700200 this->context = gGrFactory->getContextInfo(this->config.ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700201 this->config.ctxOverrides).testContext();
tomhudsond968a6f2015-03-26 11:28:06 -0700202 if (!this->surface.get()) {
203 return false;
204 }
bsalomonc8699322016-05-11 11:55:36 -0700205 if (!this->context->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700206 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800207 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700208 }
tomhudsond968a6f2015-03-26 11:28:06 -0700209 return true;
210 }
211 void fillOptions(ResultsWriter* log) override {
212 const GrGLubyte* version;
bsalomonc8699322016-05-11 11:55:36 -0700213 if (this->context->backend() == kOpenGL_GrBackend) {
214 const GrGLInterface* gl =
215 reinterpret_cast<const GrGLInterface*>(this->context->backendContext());
216 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
217 log->configOption("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700218
bsalomonc8699322016-05-11 11:55:36 -0700219 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
220 log->configOption("GL_RENDERER", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700221
bsalomonc8699322016-05-11 11:55:36 -0700222 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VENDOR));
223 log->configOption("GL_VENDOR", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700224
bsalomonc8699322016-05-11 11:55:36 -0700225 GR_GL_CALL_RET(gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
226 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
227 }
tomhudsond968a6f2015-03-26 11:28:06 -0700228 }
229};
mtkleind75c4662015-04-30 07:11:22 -0700230
tomhudsond968a6f2015-03-26 11:28:06 -0700231#endif
232
tomhudson75a0ebb2015-03-27 12:11:44 -0700233static double time(int loops, Benchmark* bench, Target* target) {
234 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700235 if (canvas) {
236 canvas->clear(SK_ColorWHITE);
237 }
joshualitt8a6697a2015-09-30 12:11:07 -0700238 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700239 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700240 canvas = target->beginTiming(canvas);
241 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700242 if (canvas) {
243 canvas->flush();
244 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700245 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700246 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700247 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700248 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700249}
250
mtkleinf3723212014-06-25 14:08:00 -0700251static double estimate_timer_overhead() {
252 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700253 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700254 double start = now_ms();
255 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700256 }
257 return overhead / FLAGS_overheadLoops;
258}
259
reed53249782014-10-10 09:09:52 -0700260static int detect_forever_loops(int loops) {
261 // look for a magic run-forever value
262 if (loops < 0) {
263 loops = SK_MaxS32;
264 }
265 return loops;
266}
267
mtklein55b0ffc2014-07-17 08:38:23 -0700268static int clamp_loops(int loops) {
269 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800270 SkDebugf("ERROR: clamping loops from %d to 1. "
271 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700272 return 1;
273 }
274 if (loops > FLAGS_maxLoops) {
275 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
276 return FLAGS_maxLoops;
277 }
278 return loops;
279}
280
tomhudsond968a6f2015-03-26 11:28:06 -0700281static bool write_canvas_png(Target* target, const SkString& filename) {
282
bsalomon6eb03cc2014-08-07 14:28:50 -0700283 if (filename.isEmpty()) {
284 return false;
285 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700286 if (target->getCanvas() &&
287 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700288 return false;
289 }
tomhudsond968a6f2015-03-26 11:28:06 -0700290
bsalomon6eb03cc2014-08-07 14:28:50 -0700291 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700292
293 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700294 return false;
295 }
tomhudsond968a6f2015-03-26 11:28:06 -0700296
bsalomon6eb03cc2014-08-07 14:28:50 -0700297 SkString dir = SkOSPath::Dirname(filename.c_str());
298 if (!sk_mkdir(dir.c_str())) {
299 SkDebugf("Can't make dir %s.\n", dir.c_str());
300 return false;
301 }
302 SkFILEWStream stream(filename.c_str());
303 if (!stream.isValid()) {
304 SkDebugf("Can't write %s.\n", filename.c_str());
305 return false;
306 }
Hal Canarydb683012016-11-23 08:55:18 -0700307 if (!SkEncodeImage(&stream, bmp, SkEncodedImageFormat::kPNG, 100)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700308 SkDebugf("Can't encode a PNG.\n");
309 return false;
310 }
311 return true;
312}
313
314static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700315static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700316 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700317 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700318 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700319 int loops = bench->calculateLoops(FLAGS_loops);
320 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700321 while (bench_plus_overhead < overhead) {
322 if (round++ == FLAGS_maxCalibrationAttempts) {
323 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700324 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700325 return kFailedLoops;
326 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700327 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700328 }
mtklein2069e222014-08-04 13:57:39 -0700329 }
mtkleinf3723212014-06-25 14:08:00 -0700330
mtkleinbb6a0282014-07-01 08:43:42 -0700331 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700332 // We'll pick N to make timer overhead negligible:
333 //
mtkleinbb6a0282014-07-01 08:43:42 -0700334 // overhead
335 // ------------------------- < FLAGS_overheadGoal
336 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700337 //
Hal Canary55325b72017-01-03 10:36:17 -0500338 // where bench_plus_overhead ~=~ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700339 //
340 // Doing some math, we get:
341 //
mtkleinbb6a0282014-07-01 08:43:42 -0700342 // (overhead / FLAGS_overheadGoal) - overhead
343 // ------------------------------------------ < N
344 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700345 //
346 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700347 if (kAutoTuneLoops == loops) {
348 const double numer = overhead / FLAGS_overheadGoal - overhead;
349 const double denom = bench_plus_overhead - overhead;
350 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700351 loops = clamp_loops(loops);
352 } else {
353 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700354 }
mtkleinbb6a0282014-07-01 08:43:42 -0700355
mtkleinbb6a0282014-07-01 08:43:42 -0700356 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700357}
358
cdaltone1b89582015-06-25 19:17:08 -0700359static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700360 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700361 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700362 if (kAutoTuneLoops == loops) {
363 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700364 double elapsed = 0;
365 do {
mtklein527930f2014-11-06 08:04:34 -0800366 if (1<<30 == loops) {
367 // We're about to wrap. Something's wrong with the bench.
368 loops = 0;
369 break;
370 }
mtkleina189ccd2014-07-14 12:28:47 -0700371 loops *= 2;
372 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700373 // _this_ round, not still timing last round.
374 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700375 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700376 }
377 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700378
mtkleina189ccd2014-07-14 12:28:47 -0700379 // We've overshot at least a little. Scale back linearly.
380 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700381 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700382
tomhudsond968a6f2015-03-26 11:28:06 -0700383 // Make sure we're not still timing our calibration.
384 target->fence();
reed53249782014-10-10 09:09:52 -0700385 } else {
386 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700387 }
mtkleinbb6a0282014-07-01 08:43:42 -0700388
389 // Pretty much the same deal as the calibration: do some warmup to make
390 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700391 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700392 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700393 }
mtkleinbb6a0282014-07-01 08:43:42 -0700394
mtkleinbb6a0282014-07-01 08:43:42 -0700395 return loops;
396}
mtkleinbb6a0282014-07-01 08:43:42 -0700397
bsalomonc2553372014-07-22 13:09:05 -0700398#if SK_SUPPORT_GPU
bsalomon85b4b532016-04-05 11:06:27 -0700399#define kBogusContextType GrContextFactory::kNativeGL_ContextType
csmartdaltone812d492017-02-21 12:36:05 -0700400#define kBogusContextOverrides GrContextFactory::ContextOverrides::kNone
bsalomonc2553372014-07-22 13:09:05 -0700401#else
bsalomon85b4b532016-04-05 11:06:27 -0700402#define kBogusContextType 0
csmartdaltone812d492017-02-21 12:36:05 -0700403#define kBogusContextOverrides 0
mtkleine714e752014-07-31 12:13:48 -0700404#endif
bsalomonc2553372014-07-22 13:09:05 -0700405
svaisanenc47635e2016-01-28 06:05:43 -0800406static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
407
408#if SK_SUPPORT_GPU
409 if (const auto* gpuConfig = config->asConfigGpu()) {
410 if (!FLAGS_gpu)
411 return;
412
svaisanenc47635e2016-01-28 06:05:43 -0800413 const auto ctxType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700414 const auto ctxOverrides = gpuConfig->getContextOverrides();
svaisanenc47635e2016-01-28 06:05:43 -0800415 const auto sampleCount = gpuConfig->getSamples();
416
csmartdaltone812d492017-02-21 12:36:05 -0700417 if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOverrides)) {
svaisanenc47635e2016-01-28 06:05:43 -0800418 const auto maxSampleCount = ctx->caps()->maxSampleCount();
419 if (sampleCount > ctx->caps()->maxSampleCount()) {
420 SkDebugf("Configuration sample count %d exceeds maximum %d.\n",
421 sampleCount, maxSampleCount);
422 return;
423 }
424 } else {
425 SkDebugf("No context was available matching config type and options.\n");
426 return;
427 }
428
429 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800430 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800431 Benchmark::kGPU_Backend,
brianosman1a48e132016-06-20 09:41:41 -0700432 gpuConfig->getColorType(),
svaisanenc47635e2016-01-28 06:05:43 -0800433 kPremul_SkAlphaType,
brianosman1a48e132016-06-20 09:41:41 -0700434 sk_ref_sp(gpuConfig->getColorSpace()),
svaisanenc47635e2016-01-28 06:05:43 -0800435 sampleCount,
436 ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700437 ctxOverrides,
kkinnunend4e1c352016-03-01 23:41:26 -0800438 gpuConfig->getUseDIText()
439 };
svaisanenc47635e2016-01-28 06:05:43 -0800440
441 configs->push_back(target);
442 return;
443 }
444#endif
445
brianosmanb109b8c2016-06-16 13:03:24 -0700446 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
447 if (config->getTag().equals(#name)) { \
448 Config config = { \
449 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
csmartdaltone812d492017-02-21 12:36:05 -0700450 0, kBogusContextType, kBogusContextOverrides, false \
brianosmanb109b8c2016-06-16 13:03:24 -0700451 }; \
452 configs->push_back(config); \
453 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700454 }
mtkleine714e752014-07-31 12:13:48 -0700455
mtklein40b32be2014-07-09 08:46:49 -0700456 if (FLAGS_cpu) {
mtkleinbb6c41b2016-03-08 11:31:11 -0800457 CPU_CONFIG(nonrendering, kNonRendering_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700458 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800459
460 CPU_CONFIG(8888, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700461 kN32_SkColorType, kPremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800462 CPU_CONFIG(565, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700463 kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500464 auto srgbColorSpace = SkColorSpace::MakeSRGB();
mtkleinbb6c41b2016-03-08 11:31:11 -0800465 CPU_CONFIG(srgb, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700466 kN32_SkColorType, kPremul_SkAlphaType, srgbColorSpace)
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500467 auto srgbLinearColorSpace = SkColorSpace::MakeSRGBLinear();
mtkleinbb6c41b2016-03-08 11:31:11 -0800468 CPU_CONFIG(f16, kRaster_Backend,
raftias7c602de2016-10-13 10:45:44 -0700469 kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbLinearColorSpace)
mtklein40b32be2014-07-09 08:46:49 -0700470 }
mtkleinbb6a0282014-07-01 08:43:42 -0700471
svaisanenc47635e2016-01-28 06:05:43 -0800472 #undef CPU_CONFIG
mtkleinf3723212014-06-25 14:08:00 -0700473}
474
svaisanenc47635e2016-01-28 06:05:43 -0800475// Append all configs that are enabled and supported.
476void create_configs(SkTArray<Config>* configs) {
477 SkCommandLineConfigArray array;
478 ParseConfigs(FLAGS_config, &array);
479 for (int i = 0; i < array.count(); ++i) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400480 create_config(array[i].get(), configs);
svaisanenc47635e2016-01-28 06:05:43 -0800481 }
482}
483
brianosman9f1f6e22016-09-15 08:33:02 -0700484// disable warning : switch statement contains default but no 'case' labels
485#if defined _WIN32
486#pragma warning ( push )
487#pragma warning ( disable : 4065 )
488#endif
489
halcanary96fcdcc2015-08-27 07:41:13 -0700490// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700491static Target* is_enabled(Benchmark* bench, const Config& config) {
492 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700493 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700494 }
495
reede5ea5002014-09-03 11:54:58 -0700496 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700497 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700498
halcanary96fcdcc2015-08-27 07:41:13 -0700499 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700500
tomhudsond968a6f2015-03-26 11:28:06 -0700501 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700502#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700503 case Benchmark::kGPU_Backend:
504 target = new GPUTarget(config);
505 break;
bsalomonc2553372014-07-22 13:09:05 -0700506#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700507 default:
508 target = new Target(config);
509 break;
510 }
bsalomonc2553372014-07-22 13:09:05 -0700511
tomhudsond968a6f2015-03-26 11:28:06 -0700512 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700513 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700514 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700515 }
516 return target;
517}
518
brianosman9f1f6e22016-09-15 08:33:02 -0700519#if defined _WIN32
520#pragma warning ( pop )
521#endif
522
reed42943c82016-09-12 12:01:44 -0700523static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700524 uint32_t minOutputSize, int* width, int* height) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400525 std::unique_ptr<SkBitmapRegionDecoder> brd(
msarettd1227a72016-05-18 06:23:57 -0700526 SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy));
msarett7f691442015-09-22 11:56:16 -0700527 if (nullptr == brd.get()) {
528 // This is indicates that subset decoding is not supported for a particular image format.
529 return false;
530 }
531
msarett7f691442015-09-22 11:56:16 -0700532 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
533 (uint32_t) brd->height()) {
534 // This indicates that the image is not large enough to decode a
535 // minOutputSize x minOutputSize subset at the given sampleSize.
536 return false;
537 }
538
539 // Set the image width and height. The calling code will use this to choose subsets to decode.
540 *width = brd->width();
541 *height = brd->height();
542 return true;
543}
544
egdaniel3bf92062015-06-26 08:12:46 -0700545static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700546 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700547#if SK_SUPPORT_GPU
548 if (FLAGS_abandonGpuContext) {
549 gGrFactory->abandonContexts();
550 }
551 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
552 gGrFactory->destroyContexts();
553 }
554#endif
555}
556
fmalita6519c212016-09-14 08:05:17 -0700557static void collect_files(const SkCommandLineFlags::StringArray& paths, const char* ext,
558 SkTArray<SkString>* list) {
559 for (int i = 0; i < paths.count(); ++i) {
560 if (SkStrEndsWith(paths[i], ext)) {
561 list->push_back(SkString(paths[i]));
562 } else {
563 SkOSFile::Iter it(paths[i], ext);
564 SkString path;
565 while (it.next(&path)) {
566 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
567 }
568 }
569 }
570}
571
mtkleine714e752014-07-31 12:13:48 -0700572class BenchmarkStream {
573public:
mtklein92007582014-08-01 07:46:52 -0700574 BenchmarkStream() : fBenches(BenchRegistry::Head())
575 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700576 , fCurrentRecording(0)
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400577 , fCurrentPiping(0)
mtklein92007582014-08-01 07:46:52 -0700578 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800579 , fCurrentSKP(0)
fmalita6519c212016-09-14 08:05:17 -0700580 , fCurrentSVG(0)
msarett95f192d2015-02-13 09:05:41 -0800581 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700582 , fCurrentCodec(0)
msarett84451022016-02-11 06:45:51 -0800583 , fCurrentAndroidCodec(0)
msarett7f691442015-09-22 11:56:16 -0700584 , fCurrentBRDImage(0)
msarett2cee9022016-06-03 08:25:21 -0700585 , fCurrentColorImage(0)
msarett95f192d2015-02-13 09:05:41 -0800586 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800587 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700588 , fCurrentSubsetType(0)
msarett84451022016-02-11 06:45:51 -0800589 , fCurrentSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700590 , fCurrentAnimSKP(0) {
fmalita6519c212016-09-14 08:05:17 -0700591 collect_files(FLAGS_skps, ".skp", &fSKPs);
592 collect_files(FLAGS_svgs, ".svg", &fSVGs);
mtkleine714e752014-07-31 12:13:48 -0700593
mtklein92007582014-08-01 07:46:52 -0700594 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
595 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
596 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
597 exit(1);
598 }
599
600 for (int i = 0; i < FLAGS_scales.count(); i++) {
601 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
602 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
603 exit(1);
604 }
605 }
robertphillips5b693772014-11-21 06:19:36 -0800606
cdalton63a82852015-06-29 14:06:10 -0700607 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
608 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700609 exit(1);
610 }
611
robertphillips5b693772014-11-21 06:19:36 -0800612 if (FLAGS_mpd) {
613 fUseMPDs.push_back() = true;
614 }
mtkleinc751ecb2015-06-15 08:56:38 -0700615 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700616
msarett95f192d2015-02-13 09:05:41 -0800617 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700618 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800619 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800620 }
msarett2cee9022016-06-03 08:25:21 -0700621 if (!CollectImages(FLAGS_colorImages, &fColorImages)) {
622 exit(1);
623 }
mtklein95553d92015-03-12 08:24:21 -0700624
msarett95f192d2015-02-13 09:05:41 -0800625 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700626 fColorTypes.push_back(kN32_SkColorType);
627 if (!FLAGS_simpleCodec) {
628 fColorTypes.push_back(kRGB_565_SkColorType);
629 fColorTypes.push_back(kAlpha_8_SkColorType);
630 fColorTypes.push_back(kIndex_8_SkColorType);
631 fColorTypes.push_back(kGray_8_SkColorType);
632 }
mtklein92007582014-08-01 07:46:52 -0700633 }
634
reedca2622b2016-03-18 07:25:55 -0700635 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700636 // Not strictly necessary, as it will be checked again later,
637 // but helps to avoid a lot of pointless work if we're going to skip it.
cdalton91e457d2016-02-17 11:10:16 -0800638 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700639 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700640 }
641
bungemanf93d7112016-09-16 06:24:20 -0700642 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
643 if (!stream) {
mtkleinfd731ce2014-09-10 12:19:30 -0700644 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700645 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700646 }
647
reedca2622b2016-03-18 07:25:55 -0700648 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700649 }
650
fmalita6519c212016-09-14 08:05:17 -0700651 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
652 SkFILEStream stream(path);
653 if (!stream.isValid()) {
654 SkDebugf("Could not read %s.\n", path);
655 return nullptr;
656 }
657
fmalitae1baa7c2016-09-14 12:04:30 -0700658 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
fmalita6519c212016-09-14 08:05:17 -0700659 if (!svgDom) {
660 SkDebugf("Could not parse %s.\n", path);
661 return nullptr;
662 }
663
fmalitae1baa7c2016-09-14 12:04:30 -0700664 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
665 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
666 if (svgDom->containerSize().isEmpty()) {
667 svgDom->setContainerSize(kDefaultContainerSize);
668 }
669
fmalita6519c212016-09-14 08:05:17 -0700670 SkPictureRecorder recorder;
fmalitae1baa7c2016-09-14 12:04:30 -0700671 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
672 svgDom->containerSize().height()));
fmalita6519c212016-09-14 08:05:17 -0700673 return recorder.finishRecordingAsPicture();
674 }
675
mtklein92007582014-08-01 07:46:52 -0700676 Benchmark* next() {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400677 std::unique_ptr<Benchmark> bench;
mtklein65dfd2f2016-02-03 10:40:54 -0800678 do {
679 bench.reset(this->rawNext());
680 if (!bench) {
681 return nullptr;
682 }
683 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
684 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700685 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800686 }
687
688 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700689 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700690 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700691 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700692 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700693 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700694 return bench;
695 }
mtklein92007582014-08-01 07:46:52 -0700696
mtkleine714e752014-07-31 12:13:48 -0700697 while (fGMs) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400698 std::unique_ptr<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700699 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800700 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700701 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700702 fBenchType = "micro";
mtklein18300a32016-03-16 13:53:35 -0700703 return new GMBench(gm.release());
mtkleine714e752014-07-31 12:13:48 -0700704 }
705 }
mtklein92007582014-08-01 07:46:52 -0700706
mtkleinfd731ce2014-09-10 12:19:30 -0700707 // First add all .skps as RecordingBenches.
708 while (fCurrentRecording < fSKPs.count()) {
709 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700710 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
711 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700712 continue;
713 }
714 SkString name = SkOSPath::Basename(path.c_str());
715 fSourceType = "skp";
716 fBenchType = "recording";
reedca2622b2016-03-18 07:25:55 -0700717 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic.get()));
mtklein051e56d2014-12-04 08:46:51 -0800718 fSKPOps = pic->approximateOpCount();
mtklein8c1a4f82016-08-08 06:56:22 -0700719 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh, FLAGS_lite);
mtkleinfd731ce2014-09-10 12:19:30 -0700720 }
721
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400722 // Add all .skps as PipeBenches.
723 while (fCurrentPiping < fSKPs.count()) {
724 const SkString& path = fSKPs[fCurrentPiping++];
725 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
726 if (!pic) {
727 continue;
728 }
729 SkString name = SkOSPath::Basename(path.c_str());
730 fSourceType = "skp";
731 fBenchType = "piping";
732 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic.get()));
733 fSKPOps = pic->approximateOpCount();
734 return new PipingBench(name.c_str(), pic.get());
735 }
736
mtkleinfd731ce2014-09-10 12:19:30 -0700737 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700738 while (fCurrentScale < fScales.count()) {
739 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800740 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700741 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
742 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800743 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700744 continue;
745 }
robertphillips5b693772014-11-21 06:19:36 -0800746
747 while (fCurrentUseMPD < fUseMPDs.count()) {
748 if (FLAGS_bbh) {
749 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
750 SkRTreeFactory factory;
751 SkPictureRecorder recorder;
robertphillips5b693772014-11-21 06:19:36 -0800752 pic->playback(recorder.beginRecording(pic->cullRect().width(),
753 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800754 &factory,
robertphillipsdda54452016-07-13 13:27:16 -0700755 0));
reedca2622b2016-03-18 07:25:55 -0700756 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800757 }
758 SkString name = SkOSPath::Basename(path.c_str());
759 fSourceType = "skp";
760 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700761 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
762 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700763 }
robertphillips5b693772014-11-21 06:19:36 -0800764 fCurrentUseMPD = 0;
765 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700766 }
fmalita6519c212016-09-14 08:05:17 -0700767
768 while (fCurrentSVG++ < fSVGs.count()) {
769 const char* path = fSVGs[fCurrentSVG - 1].c_str();
770 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
771 fSourceType = "svg";
772 fBenchType = "playback";
773 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
774 fScales[fCurrentScale], false, FLAGS_loopSKP);
775 }
776 }
777
mtklein92007582014-08-01 07:46:52 -0700778 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700779 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700780 fCurrentScale++;
781 }
782
joshualitt261c3ad2015-04-27 09:16:57 -0700783 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700784 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700785 while (fCurrentAnimSKP < fSKPs.count()) {
786 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700787 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
788 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700789 fCurrentAnimSKP++;
790 continue;
791 }
792
793 fCurrentAnimSKP++;
794 SkString name = SkOSPath::Basename(path.c_str());
Hal Canary2db83612016-11-04 13:02:54 -0400795 sk_sp<SKPAnimationBench::Animation> animation(
cdalton63a82852015-06-29 14:06:10 -0700796 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
Hal Canary2db83612016-11-04 13:02:54 -0400797 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation.get(),
halcanary385fe4d2015-08-26 13:07:48 -0700798 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700799 }
800 }
801
scroggo60869a42015-04-01 12:09:17 -0700802 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700803 fSourceType = "image";
804 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700805 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800806 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
807 continue;
808 }
bungeman38d909e2016-08-02 14:40:46 -0700809 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Ben Wagner145dbcd2016-11-03 14:40:50 -0400810 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700811 if (!codec) {
812 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700813 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700814 continue;
815 }
scroggo21027992015-04-02 13:22:38 -0700816
scroggo60869a42015-04-01 12:09:17 -0700817 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700818 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700819
msarettc7796b92016-01-07 14:20:20 -0800820 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700821 if (FLAGS_simpleCodec) {
822 if (kUnpremul_SkAlphaType == alphaType) {
823 alphaType = kPremul_SkAlphaType;
824 }
825
826 fCurrentColorType++;
827 } else {
828 switch (alphaType) {
829 case kOpaque_SkAlphaType:
830 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800831 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700832 break;
833 case kUnpremul_SkAlphaType:
834 case kPremul_SkAlphaType:
835 if (0 == fCurrentAlphaType) {
836 // Test unpremul first.
837 alphaType = kUnpremul_SkAlphaType;
838 fCurrentAlphaType++;
839 } else {
840 // Test premul.
841 alphaType = kPremul_SkAlphaType;
842 fCurrentAlphaType = 0;
843 fCurrentColorType++;
844 }
845 break;
846 default:
847 SkASSERT(false);
848 fCurrentColorType++;
849 break;
850 }
scroggo21027992015-04-02 13:22:38 -0700851 }
852
msarettc7796b92016-01-07 14:20:20 -0800853 // Make sure we can decode to this color type and alpha type.
854 SkImageInfo info =
855 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700856 const size_t rowBytes = info.minRowBytes();
857 SkAutoMalloc storage(info.getSafeSize(rowBytes));
858
859 // Used if fCurrentColorType is kIndex_8_SkColorType
860 int colorCount = 256;
861 SkPMColor colors[256];
862
scroggoeb602a52015-07-09 08:16:03 -0700863 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700864 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700865 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700866 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700867 case SkCodec::kSuccess:
868 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700869 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700870 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700871 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700872 // This is okay. Not all conversions are valid.
873 break;
scroggo60869a42015-04-01 12:09:17 -0700874 default:
875 // This represents some sort of failure.
876 SkASSERT(false);
877 break;
878 }
879 }
880 fCurrentColorType = 0;
881 }
882
msarett84451022016-02-11 06:45:51 -0800883 // Run AndroidCodecBenches
884 const int sampleSizes[] = { 2, 4, 8 };
885 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
886 fSourceType = "image";
887 fBenchType = "skandroidcodec";
888
889 const SkString& path = fImages[fCurrentAndroidCodec];
890 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
891 continue;
892 }
bungeman38d909e2016-08-02 14:40:46 -0700893 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Ben Wagner145dbcd2016-11-03 14:40:50 -0400894 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800895 if (!codec) {
896 // Nothing to time.
897 SkDebugf("Cannot find codec for %s\n", path.c_str());
898 continue;
899 }
900
901 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
902 int sampleSize = sampleSizes[fCurrentSampleSize];
903 fCurrentSampleSize++;
904 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
905 // Avoid benchmarking scaled decodes of already small images.
906 break;
907 }
908
bungeman38d909e2016-08-02 14:40:46 -0700909 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
910 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -0800911 }
912 fCurrentSampleSize = 0;
913 }
914
msarett7f691442015-09-22 11:56:16 -0700915 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700916 // We intend to create benchmarks that model the use cases in
917 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
918 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
919 // the image. For that reason, we will benchmark decodes in five representative locations
920 // in the image. Additionally, this use case utilizes power of two scaling, so we will
921 // test on power of two sample sizes. The output tile is always 512x512, so, when a
922 // sampleSize is used, the size of the subset that is decoded is always
923 // (sampleSize*512)x(sampleSize*512).
924 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700925 // All use cases we are aware of only scale by powers of two.
926 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
927 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800928 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700929 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800930 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700931 fSourceType = "image";
932 fBenchType = "BRD";
933
mtklein6f0ff912016-01-11 09:04:21 -0800934 const SkString& path = fImages[fCurrentBRDImage];
935 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
936 continue;
937 }
scroggo860e8a62015-10-15 07:51:28 -0700938
msarettd1227a72016-05-18 06:23:57 -0700939 while (fCurrentColorType < fColorTypes.count()) {
940 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
941 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700942
bungeman38d909e2016-08-02 14:40:46 -0700943 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -0700944 const SkColorType colorType = fColorTypes[fCurrentColorType];
945 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
946 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -0700947
msarettd1227a72016-05-18 06:23:57 -0700948 int width = 0;
949 int height = 0;
reed42943c82016-09-12 12:01:44 -0700950 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -0700951 &width, &height)) {
952 break;
msarett7f691442015-09-22 11:56:16 -0700953 }
msarettd1227a72016-05-18 06:23:57 -0700954
955 SkString basename = SkOSPath::Basename(path.c_str());
956 SkIRect subset;
957 const uint32_t subsetSize = sampleSize * minOutputSize;
958 switch (currentSubsetType) {
959 case kTopLeft_SubsetType:
960 basename.append("_TopLeft");
961 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
962 break;
963 case kTopRight_SubsetType:
964 basename.append("_TopRight");
965 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
966 subsetSize);
967 break;
968 case kMiddle_SubsetType:
969 basename.append("_Middle");
970 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
971 (height - subsetSize) / 2, subsetSize, subsetSize);
972 break;
973 case kBottomLeft_SubsetType:
974 basename.append("_BottomLeft");
975 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
976 subsetSize);
977 break;
978 case kBottomRight_SubsetType:
979 basename.append("_BottomRight");
980 subset = SkIRect::MakeXYWH(width - subsetSize,
981 height - subsetSize, subsetSize, subsetSize);
982 break;
983 default:
984 SkASSERT(false);
985 }
986
987 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
988 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -0700989 }
msarettd1227a72016-05-18 06:23:57 -0700990 fCurrentSubsetType = 0;
991 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -0700992 }
msarettd1227a72016-05-18 06:23:57 -0700993 fCurrentSampleSize = 0;
994 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -0700995 }
msarettd1227a72016-05-18 06:23:57 -0700996 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -0700997 }
998
msarett2cee9022016-06-03 08:25:21 -0700999 while (fCurrentColorImage < fColorImages.count()) {
1000 fSourceType = "colorimage";
1001 fBenchType = "skcolorcodec";
1002 const SkString& path = fColorImages[fCurrentColorImage];
1003 fCurrentColorImage++;
1004 sk_sp<SkData> encoded = SkData::MakeFromFileName(path.c_str());
1005 if (encoded) {
1006 return new ColorCodecBench(SkOSPath::Basename(path.c_str()).c_str(),
1007 std::move(encoded));
1008 } else {
1009 SkDebugf("Could not read file %s.\n", path.c_str());
1010 }
1011 }
1012
halcanary96fcdcc2015-08-27 07:41:13 -07001013 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001014 }
mtklein92007582014-08-01 07:46:52 -07001015
1016 void fillCurrentOptions(ResultsWriter* log) const {
1017 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001018 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001019 if (0 == strcmp(fSourceType, "skp")) {
1020 log->configOption("clip",
1021 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1022 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001023 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -07001024 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001025 if (fCurrentUseMPD > 0) {
1026 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1027 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1028 }
mtklein92007582014-08-01 07:46:52 -07001029 }
mtklein051e56d2014-12-04 08:46:51 -08001030 if (0 == strcmp(fBenchType, "recording")) {
1031 log->metric("bytes", fSKPBytes);
1032 log->metric("ops", fSKPOps);
1033 }
mtklein92007582014-08-01 07:46:52 -07001034 }
1035
mtkleine714e752014-07-31 12:13:48 -07001036private:
msarettb23e6aa2015-06-09 13:56:10 -07001037 enum SubsetType {
1038 kTopLeft_SubsetType = 0,
1039 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001040 kMiddle_SubsetType = 2,
1041 kBottomLeft_SubsetType = 3,
1042 kBottomRight_SubsetType = 4,
1043 kTranslate_SubsetType = 5,
1044 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001045 kLast_SubsetType = kZoom_SubsetType,
1046 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001047 };
1048
mtkleine714e752014-07-31 12:13:48 -07001049 const BenchRegistry* fBenches;
1050 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001051 SkIRect fClip;
1052 SkTArray<SkScalar> fScales;
1053 SkTArray<SkString> fSKPs;
fmalita6519c212016-09-14 08:05:17 -07001054 SkTArray<SkString> fSVGs;
robertphillips5b693772014-11-21 06:19:36 -08001055 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001056 SkTArray<SkString> fImages;
msarett2cee9022016-06-03 08:25:21 -07001057 SkTArray<SkString> fColorImages;
msarett74deb982015-10-20 16:45:56 -07001058 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001059 SkScalar fZoomMax;
1060 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001061
mtklein051e56d2014-12-04 08:46:51 -08001062 double fSKPBytes, fSKPOps;
1063
mtkleinfd731ce2014-09-10 12:19:30 -07001064 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1065 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1066 int fCurrentRecording;
Mike Reed9cdd2ab2016-10-21 10:43:36 -04001067 int fCurrentPiping;
mtklein92007582014-08-01 07:46:52 -07001068 int fCurrentScale;
1069 int fCurrentSKP;
fmalita6519c212016-09-14 08:05:17 -07001070 int fCurrentSVG;
robertphillips5b693772014-11-21 06:19:36 -08001071 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001072 int fCurrentCodec;
msarett84451022016-02-11 06:45:51 -08001073 int fCurrentAndroidCodec;
msarett7f691442015-09-22 11:56:16 -07001074 int fCurrentBRDImage;
msarett2cee9022016-06-03 08:25:21 -07001075 int fCurrentColorImage;
msarett95f192d2015-02-13 09:05:41 -08001076 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -08001077 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -07001078 int fCurrentSubsetType;
msarett84451022016-02-11 06:45:51 -08001079 int fCurrentSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001080 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001081};
1082
msarettc149f0e2016-01-04 11:35:43 -08001083// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1084// This prints something every once in a while so that it knows we're still working.
1085static void start_keepalive() {
1086 struct Loop {
1087 static void forever(void*) {
1088 for (;;) {
1089 static const int kSec = 1200;
1090 #if defined(SK_BUILD_FOR_WIN)
1091 Sleep(kSec * 1000);
1092 #else
1093 sleep(kSec);
1094 #endif
1095 SkDebugf("\nBenchmarks still running...\n");
1096 }
1097 }
1098 };
1099 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1100 intentionallyLeaked->start();
1101}
1102
Mike Kleinbe28ee22017-02-06 12:46:20 -05001103int main(int argc, char** argv) {
1104 SkCommandLineFlags::Parse(argc, argv);
Mike Kleinadacaef2017-02-06 09:26:14 -05001105#if defined(SK_BUILD_FOR_IOS)
1106 cd_Documents();
1107#endif
jcgregorio3b27ade2014-11-13 08:06:40 -08001108 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001109 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001110 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001111
krajcevski69a55602014-08-13 10:46:31 -07001112#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001113 GrContextOptions grContextOpts;
csmartdalton008b9d82017-02-22 12:00:42 -07001114 grContextOpts.fGpuPathRenderers = CollectGpuPathRenderersFromFlags();
halcanary385fe4d2015-08-26 13:07:48 -07001115 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001116#endif
1117
bsalomon06cddec2014-10-24 10:40:50 -07001118 if (FLAGS_veryVerbose) {
1119 FLAGS_verbose = true;
1120 }
1121
bsalomon6eb03cc2014-08-07 14:28:50 -07001122 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001123 FLAGS_samples = 1;
1124 FLAGS_gpuFrameLag = 0;
1125 }
1126
bsalomon6eb03cc2014-08-07 14:28:50 -07001127 if (!FLAGS_writePath.isEmpty()) {
1128 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1129 if (!sk_mkdir(FLAGS_writePath[0])) {
1130 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001131 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001132 }
1133 }
1134
Ben Wagner145dbcd2016-11-03 14:40:50 -04001135 std::unique_ptr<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001136 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001137#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001138 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001139#else
1140 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1141 return 1;
1142#endif
mtklein60317d0f2014-07-14 11:30:37 -07001143 }
mtklein1915b622014-08-20 11:45:00 -07001144
1145 if (1 == FLAGS_properties.count() % 2) {
1146 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1147 return 1;
1148 }
1149 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1150 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1151 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001152
1153 if (1 == FLAGS_key.count() % 2) {
1154 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1155 return 1;
1156 }
1157 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001158 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001159 }
mtklein60317d0f2014-07-14 11:30:37 -07001160
mtkleinf3723212014-06-25 14:08:00 -07001161 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001162 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001163
cdaltone1b89582015-06-25 19:17:08 -07001164 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001165
bsalomon6eb03cc2014-08-07 14:28:50 -07001166 if (kAutoTuneLoops != FLAGS_loops) {
1167 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001168 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001169 SkDebugf("! -> high variance, ? -> moderate variance\n");
1170 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001171 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001172 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001173 } else {
mtkleind75c4662015-04-30 07:11:22 -07001174 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001175 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001176 }
1177
svaisanenc47635e2016-01-28 06:05:43 -08001178 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001179 create_configs(&configs);
1180
joshualitt3ebd0502016-02-09 07:18:08 -08001181#ifdef THERMAL_MANAGER_SUPPORTED
1182 int tmEnabled, tmThreshold, tmSleepTimeMs, tmTimeoutMs;
1183 if (4 != sscanf(FLAGS_useThermalManager[0], "%d,%d,%d,%d",
1184 &tmEnabled, &tmThreshold, &tmSleepTimeMs, &tmTimeoutMs)) {
1185 SkDebugf("Can't parse %s from --useThermalManager.\n", FLAGS_useThermalManager[0]);
1186 exit(1);
1187 }
1188 ThermalManager tm(tmThreshold, tmSleepTimeMs, tmTimeoutMs);
1189#endif
1190
msarettc149f0e2016-01-04 11:35:43 -08001191 if (FLAGS_keepAlive) {
1192 start_keepalive();
1193 }
1194
Yuqian Liba62b4a2016-12-14 13:46:53 -05001195 gSkUseAnalyticAA = FLAGS_analyticAA;
liyuqian38911a72016-10-04 11:23:22 -07001196
Yuqian Li550148b2017-01-13 10:13:13 -05001197 if (FLAGS_forceAnalyticAA) {
1198 gSkForceAnalyticAA = true;
1199 }
1200
mtkleine070c2b2014-10-14 08:40:43 -07001201 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001202 BenchmarkStream benchStream;
1203 while (Benchmark* b = benchStream.next()) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001204 std::unique_ptr<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001205 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001206 continue;
1207 }
1208
svaisanenc47635e2016-01-28 06:05:43 -08001209 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001210 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001211 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001212 }
egdaniel3bf92062015-06-26 08:12:46 -07001213 for (int i = 0; i < configs.count(); ++i) {
joshualitt3ebd0502016-02-09 07:18:08 -08001214#ifdef THERMAL_MANAGER_SUPPORTED
1215 if (tmEnabled && !tm.coolOffIfNecessary()) {
1216 SkDebugf("Could not cool off, timings will be throttled\n");
1217 }
1218#endif
egdaniel3bf92062015-06-26 08:12:46 -07001219 Target* target = is_enabled(b, configs[i]);
1220 if (!target) {
1221 continue;
1222 }
mtkleinf3723212014-06-25 14:08:00 -07001223
halcanary96fcdcc2015-08-27 07:41:13 -07001224 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001225 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001226 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001227
brianosman7a5ada82016-02-08 13:49:12 -08001228 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001229 SkDebugf("Running %s\t%s\n"
1230 , bench->getUniqueName()
1231 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001232 if (FLAGS_dryRun) {
1233 continue;
1234 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001235 }
1236
egdaniel3bf92062015-06-26 08:12:46 -07001237 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001238 bench->perCanvasPreDraw(canvas);
1239
cdaltone1b89582015-06-25 19:17:08 -07001240 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001241 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001242 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1243 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001244
mtkleinbbba1682015-10-28 11:36:30 -07001245 if (FLAGS_ms) {
1246 samples.reset();
1247 auto stop = now_ms() + FLAGS_ms;
1248 do {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001249 samples.push_back(time(loops, bench.get(), target) / loops);
mtkleinbbba1682015-10-28 11:36:30 -07001250 } while (now_ms() < stop);
1251 } else {
cdaltone1b89582015-06-25 19:17:08 -07001252 samples.reset(FLAGS_samples);
1253 for (int s = 0; s < FLAGS_samples; s++) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001254 samples[s] = time(loops, bench.get(), target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001255 }
cdaltone1b89582015-06-25 19:17:08 -07001256 }
mtkleinf3723212014-06-25 14:08:00 -07001257
joshualitte45c81c2015-12-02 09:05:37 -08001258#if SK_SUPPORT_GPU
1259 SkTArray<SkString> keys;
1260 SkTArray<double> values;
1261 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1262 if (gpuStatsDump) {
1263 // TODO cache stats
1264 bench->getGpuStats(canvas, &keys, &values);
1265 }
1266#endif
1267
robertphillips5b693772014-11-21 06:19:36 -08001268 bench->perCanvasPostDraw(canvas);
1269
egdaniel3bf92062015-06-26 08:12:46 -07001270 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001271 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001272 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001273 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001274 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001275 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001276 }
1277
1278 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001279 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001280 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001281 continue;
1282 }
1283
cdaltone1b89582015-06-25 19:17:08 -07001284 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001285 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001286 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001287 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001288 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001289 log->metric("min_ms", stats.min);
George Burgess IV75b57182016-12-06 10:53:52 -08001290 log->metrics("samples", samples);
joshualitte45c81c2015-12-02 09:05:37 -08001291#if SK_SUPPORT_GPU
1292 if (gpuStatsDump) {
1293 // dump to json, only SKPBench currently returns valid keys / values
1294 SkASSERT(keys.count() == values.count());
1295 for (int i = 0; i < keys.count(); i++) {
1296 log->metric(keys[i].c_str(), values[i]);
1297 }
1298 }
1299#endif
1300
mtkleine070c2b2014-10-14 08:40:43 -07001301 if (runs++ % FLAGS_flushEvery == 0) {
1302 log->flush();
1303 }
mtklein60317d0f2014-07-14 11:30:37 -07001304
bsalomon6eb03cc2014-08-07 14:28:50 -07001305 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001306 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001307 config = ""; // Only print the config if we run the same bench on more than one.
1308 }
mtkleind75c4662015-04-30 07:11:22 -07001309 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1310 , sk_tools::getCurrResidentSetSizeMB()
1311 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001312 , bench->getUniqueName()
1313 , config);
mtkleinf3723212014-06-25 14:08:00 -07001314 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001315 const char* mark = " ";
1316 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1317 if (stddev_percent > 5) mark = "?";
1318 if (stddev_percent > 10) mark = "!";
1319
1320 SkDebugf("%10.2f %s\t%s\t%s\n",
1321 stats.median*1e3, mark, bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001322 } else {
1323 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001324 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1325 , sk_tools::getCurrResidentSetSizeMB()
1326 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001327 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001328 , HUMANIZE(stats.min)
1329 , HUMANIZE(stats.median)
1330 , HUMANIZE(stats.mean)
1331 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001332 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001333 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001334 , config
mtklein96289052014-09-10 12:05:59 -07001335 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001336 );
1337 }
joshualitte45c81c2015-12-02 09:05:37 -08001338
bsalomonb12ea412015-02-02 21:19:50 -08001339#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001340 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001341 GrContext* context = gGrFactory->get(configs[i].ctxType,
csmartdaltone812d492017-02-21 12:36:05 -07001342 configs[i].ctxOverrides);
kkinnunen5219fd92015-12-10 06:28:13 -08001343 context->printCacheStats();
1344 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001345 }
1346#endif
joshualitte45c81c2015-12-02 09:05:37 -08001347
cdalton2c56ba52015-06-26 13:32:53 -07001348 if (FLAGS_verbose) {
1349 SkDebugf("Samples: ");
1350 for (int i = 0; i < samples.count(); i++) {
1351 SkDebugf("%s ", HUMANIZE(samples[i]));
1352 }
1353 SkDebugf("%s\n", bench->getUniqueName());
1354 }
egdaniel3bf92062015-06-26 08:12:46 -07001355 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001356 }
mtkleinf3723212014-06-25 14:08:00 -07001357 }
1358
Herb Derby5a523fe2017-01-26 16:48:28 -05001359 SkGraphics::PurgeAllCaches();
1360
mtkleine1091452014-12-04 10:47:02 -08001361 log->bench("memory_usage", 0,0);
1362 log->config("meta");
1363 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1364
joshualitte0b19d42015-03-26 10:41:02 -07001365#if SK_SUPPORT_GPU
1366 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1367 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001368 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001369#endif
1370
mtkleinf3723212014-06-25 14:08:00 -07001371 return 0;
1372}