blob: 5e13a296718005dc2de172008248526efb0b98e1 [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"
Hal Canary95e3c052017-01-11 12:44:43 -050043#include "SkSVGDOM.h"
44#include "SkScan.h"
mtkleinf3723212014-06-25 14:08:00 -070045#include "SkString.h"
46#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080047#include "SkTaskGroup.h"
msarettc149f0e2016-01-04 11:35:43 -080048#include "SkThreadUtils.h"
joshualitt3ebd0502016-02-09 07:18:08 -080049#include "ThermalManager.h"
mtkleinf3723212014-06-25 14:08:00 -070050
bungeman60e0fee2015-08-26 05:15:46 -070051#include <stdlib.h>
52
scroggo38ce0a72016-01-07 07:28:47 -080053#ifndef SK_BUILD_FOR_WIN32
54 #include <unistd.h>
55#endif
56
mtkleinbb6a0282014-07-01 08:43:42 -070057#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070058 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070059 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070060 #include "GrContextFactory.h"
bsalomon3724e572016-03-30 18:56:19 -070061 #include "gl/GrGLUtil.h"
62 using sk_gpu_test::GrContextFactory;
bsalomonc8699322016-05-11 11:55:36 -070063 using sk_gpu_test::TestContext;
Ben Wagner145dbcd2016-11-03 14:40:50 -040064 std::unique_ptr<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070065#endif
66
bsalomon682c2692015-05-22 14:01:46 -070067 struct GrContextOptions;
68
reed53249782014-10-10 09:09:52 -070069static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070070
Mike Kleind8ee67c2017-01-19 12:14:50 -050071#if !defined(__has_feature)
72 #define __has_feature(x) 0
73#endif
74
mtkleinb5110422014-08-07 15:20:02 -070075static const int kDefaultLoops =
Mike Kleind8ee67c2017-01-19 12:14:50 -050076#if defined(SK_DEBUG) || __has_feature(address_sanitizer)
bsalomon6eb03cc2014-08-07 14:28:50 -070077 1;
mtkleina189ccd2014-07-14 12:28:47 -070078#else
bsalomon6eb03cc2014-08-07 14:28:50 -070079 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070080#endif
81
bsalomon6eb03cc2014-08-07 14:28:50 -070082static SkString loops_help_txt() {
83 SkString help;
84 help.printf("Number of times to run each bench. Set this to %d to auto-"
85 "tune for each bench. Timings are only reported when auto-tuning.",
86 kAutoTuneLoops);
87 return help;
88}
89
cdaltone1b89582015-06-25 19:17:08 -070090static SkString to_string(int n) {
91 SkString str;
92 str.appendS32(n);
93 return str;
94}
95
bsalomon6eb03cc2014-08-07 14:28:50 -070096DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
97
mtkleinf3723212014-06-25 14:08:00 -070098DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -070099DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -0700100DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
101DEFINE_double(overheadGoal, 0.0001,
102 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -0700103DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -0700104DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -0700105
mtklein60317d0f2014-07-14 11:30:37 -0700106DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -0700107DEFINE_int32(maxCalibrationAttempts, 3,
108 "Try up to this many times to guess loops for a bench, or skip the bench.");
109DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700110DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
111DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700112DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
113 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700114DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
mtklein8c1a4f82016-08-08 06:56:22 -0700115DEFINE_bool(lite, false, "Use SkLiteRecorder in recording benchmarks?");
robertphillips5b693772014-11-21 06:19:36 -0800116DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700117DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700118DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800119DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800120DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800121DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
msarettc149f0e2016-01-04 11:35:43 -0800122DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
joshualitt3ebd0502016-02-09 07:18:08 -0800123DEFINE_string(useThermalManager, "0,1,10,1000", "enabled,threshold,sleepTimeMs,TimeoutMs for "
124 "thermalManager\n");
mtklein92007582014-08-01 07:46:52 -0700125
mtklein65dfd2f2016-02-03 10:40:54 -0800126DEFINE_string(sourceType, "",
127 "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
128DEFINE_string(benchType, "",
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400129 "Apply usual --match rules to bench type: micro, recording, piping, playback, skcodec, etc.");
mtklein65dfd2f2016-02-03 10:40:54 -0800130
csmartdalton008b9d82017-02-22 12:00:42 -0700131#if SK_SUPPORT_GPU
132DEFINE_pathrenderer_flag;
133#endif
134
mtkleinbbba1682015-10-28 11:36:30 -0700135static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
136
mtkleinf3723212014-06-25 14:08:00 -0700137static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700138 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800139 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700140}
mtklein55b0ffc2014-07-17 08:38:23 -0700141#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700142
tomhudsond968a6f2015-03-26 11:28:06 -0700143bool Target::init(SkImageInfo info, Benchmark* bench) {
144 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700145 this->surface = SkSurface::MakeRaster(info);
146 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700147 return false;
148 }
149 }
150 return true;
151}
152bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700153 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700154 if (!canvas) {
155 return false;
156 }
157 bmp->setInfo(canvas->imageInfo());
158 if (!canvas->readPixels(bmp, 0, 0)) {
159 SkDebugf("Can't read canvas pixels.\n");
160 return false;
161 }
162 return true;
163}
164
165#if SK_SUPPORT_GPU
166struct GPUTarget : public Target {
bsalomonc8699322016-05-11 11:55:36 -0700167 explicit GPUTarget(const Config& c) : Target(c), context(nullptr) { }
168 TestContext* context;
tomhudsond968a6f2015-03-26 11:28:06 -0700169
170 void setup() override {
bsalomonc8699322016-05-11 11:55:36 -0700171 this->context->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700172 // Make sure we're done with whatever came before.
bsalomonc8699322016-05-11 11:55:36 -0700173 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700174 }
175 void endTiming() override {
bsalomonc8699322016-05-11 11:55:36 -0700176 if (this->context) {
177 this->context->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700178 }
179 }
180 void fence() override {
bsalomonc8699322016-05-11 11:55:36 -0700181 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700182 }
mtkleind75c4662015-04-30 07:11:22 -0700183
cdaltond416a5b2015-06-23 13:23:44 -0700184 bool needsFrameTiming(int* maxFrameLag) const override {
bsalomonc8699322016-05-11 11:55:36 -0700185 if (!this->context->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700186 // Frame lag is unknown.
187 *maxFrameLag = FLAGS_gpuFrameLag;
188 }
189 return true;
190 }
tomhudsond968a6f2015-03-26 11:28:06 -0700191 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700192 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
193 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700194 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700195 this->surface = SkSurface::MakeRenderTarget(gGrFactory->get(this->config.ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700196 this->config.ctxOverrides),
bsalomon5ec26ae2016-02-25 08:33:02 -0800197 SkBudgeted::kNo, info,
reede8f30622016-03-23 18:59:25 -0700198 this->config.samples, &props);
bsalomonc8699322016-05-11 11:55:36 -0700199 this->context = gGrFactory->getContextInfo(this->config.ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700200 this->config.ctxOverrides).testContext();
tomhudsond968a6f2015-03-26 11:28:06 -0700201 if (!this->surface.get()) {
202 return false;
203 }
bsalomonc8699322016-05-11 11:55:36 -0700204 if (!this->context->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700205 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800206 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700207 }
tomhudsond968a6f2015-03-26 11:28:06 -0700208 return true;
209 }
210 void fillOptions(ResultsWriter* log) override {
211 const GrGLubyte* version;
bsalomonc8699322016-05-11 11:55:36 -0700212 if (this->context->backend() == kOpenGL_GrBackend) {
213 const GrGLInterface* gl =
214 reinterpret_cast<const GrGLInterface*>(this->context->backendContext());
215 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
216 log->configOption("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700217
bsalomonc8699322016-05-11 11:55:36 -0700218 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
219 log->configOption("GL_RENDERER", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700220
bsalomonc8699322016-05-11 11:55:36 -0700221 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VENDOR));
222 log->configOption("GL_VENDOR", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700223
bsalomonc8699322016-05-11 11:55:36 -0700224 GR_GL_CALL_RET(gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
225 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
226 }
tomhudsond968a6f2015-03-26 11:28:06 -0700227 }
228};
mtkleind75c4662015-04-30 07:11:22 -0700229
tomhudsond968a6f2015-03-26 11:28:06 -0700230#endif
231
tomhudson75a0ebb2015-03-27 12:11:44 -0700232static double time(int loops, Benchmark* bench, Target* target) {
233 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700234 if (canvas) {
235 canvas->clear(SK_ColorWHITE);
236 }
joshualitt8a6697a2015-09-30 12:11:07 -0700237 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700238 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700239 canvas = target->beginTiming(canvas);
240 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700241 if (canvas) {
242 canvas->flush();
243 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700244 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700245 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700246 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700247 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700248}
249
mtkleinf3723212014-06-25 14:08:00 -0700250static double estimate_timer_overhead() {
251 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700252 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700253 double start = now_ms();
254 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700255 }
256 return overhead / FLAGS_overheadLoops;
257}
258
reed53249782014-10-10 09:09:52 -0700259static int detect_forever_loops(int loops) {
260 // look for a magic run-forever value
261 if (loops < 0) {
262 loops = SK_MaxS32;
263 }
264 return loops;
265}
266
mtklein55b0ffc2014-07-17 08:38:23 -0700267static int clamp_loops(int loops) {
268 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800269 SkDebugf("ERROR: clamping loops from %d to 1. "
270 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700271 return 1;
272 }
273 if (loops > FLAGS_maxLoops) {
274 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
275 return FLAGS_maxLoops;
276 }
277 return loops;
278}
279
tomhudsond968a6f2015-03-26 11:28:06 -0700280static bool write_canvas_png(Target* target, const SkString& filename) {
281
bsalomon6eb03cc2014-08-07 14:28:50 -0700282 if (filename.isEmpty()) {
283 return false;
284 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700285 if (target->getCanvas() &&
286 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700287 return false;
288 }
tomhudsond968a6f2015-03-26 11:28:06 -0700289
bsalomon6eb03cc2014-08-07 14:28:50 -0700290 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700291
292 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700293 return false;
294 }
tomhudsond968a6f2015-03-26 11:28:06 -0700295
bsalomon6eb03cc2014-08-07 14:28:50 -0700296 SkString dir = SkOSPath::Dirname(filename.c_str());
297 if (!sk_mkdir(dir.c_str())) {
298 SkDebugf("Can't make dir %s.\n", dir.c_str());
299 return false;
300 }
301 SkFILEWStream stream(filename.c_str());
302 if (!stream.isValid()) {
303 SkDebugf("Can't write %s.\n", filename.c_str());
304 return false;
305 }
Hal Canarydb683012016-11-23 08:55:18 -0700306 if (!SkEncodeImage(&stream, bmp, SkEncodedImageFormat::kPNG, 100)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700307 SkDebugf("Can't encode a PNG.\n");
308 return false;
309 }
310 return true;
311}
312
313static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700314static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700315 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700316 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700317 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700318 int loops = bench->calculateLoops(FLAGS_loops);
319 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700320 while (bench_plus_overhead < overhead) {
321 if (round++ == FLAGS_maxCalibrationAttempts) {
322 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700323 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700324 return kFailedLoops;
325 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700326 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700327 }
mtklein2069e222014-08-04 13:57:39 -0700328 }
mtkleinf3723212014-06-25 14:08:00 -0700329
mtkleinbb6a0282014-07-01 08:43:42 -0700330 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700331 // We'll pick N to make timer overhead negligible:
332 //
mtkleinbb6a0282014-07-01 08:43:42 -0700333 // overhead
334 // ------------------------- < FLAGS_overheadGoal
335 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700336 //
Hal Canary55325b72017-01-03 10:36:17 -0500337 // where bench_plus_overhead ~=~ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700338 //
339 // Doing some math, we get:
340 //
mtkleinbb6a0282014-07-01 08:43:42 -0700341 // (overhead / FLAGS_overheadGoal) - overhead
342 // ------------------------------------------ < N
343 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700344 //
345 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700346 if (kAutoTuneLoops == loops) {
347 const double numer = overhead / FLAGS_overheadGoal - overhead;
348 const double denom = bench_plus_overhead - overhead;
349 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700350 loops = clamp_loops(loops);
351 } else {
352 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700353 }
mtkleinbb6a0282014-07-01 08:43:42 -0700354
mtkleinbb6a0282014-07-01 08:43:42 -0700355 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700356}
357
cdaltone1b89582015-06-25 19:17:08 -0700358static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700359 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700360 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700361 if (kAutoTuneLoops == loops) {
362 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700363 double elapsed = 0;
364 do {
mtklein527930f2014-11-06 08:04:34 -0800365 if (1<<30 == loops) {
366 // We're about to wrap. Something's wrong with the bench.
367 loops = 0;
368 break;
369 }
mtkleina189ccd2014-07-14 12:28:47 -0700370 loops *= 2;
371 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700372 // _this_ round, not still timing last round.
373 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700374 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700375 }
376 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700377
mtkleina189ccd2014-07-14 12:28:47 -0700378 // We've overshot at least a little. Scale back linearly.
379 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700380 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700381
tomhudsond968a6f2015-03-26 11:28:06 -0700382 // Make sure we're not still timing our calibration.
383 target->fence();
reed53249782014-10-10 09:09:52 -0700384 } else {
385 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700386 }
mtkleinbb6a0282014-07-01 08:43:42 -0700387
388 // Pretty much the same deal as the calibration: do some warmup to make
389 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700390 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700391 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700392 }
mtkleinbb6a0282014-07-01 08:43:42 -0700393
mtkleinbb6a0282014-07-01 08:43:42 -0700394 return loops;
395}
mtkleinbb6a0282014-07-01 08:43:42 -0700396
bsalomonc2553372014-07-22 13:09:05 -0700397#if SK_SUPPORT_GPU
bsalomon85b4b532016-04-05 11:06:27 -0700398#define kBogusContextType GrContextFactory::kNativeGL_ContextType
csmartdaltone812d492017-02-21 12:36:05 -0700399#define kBogusContextOverrides GrContextFactory::ContextOverrides::kNone
bsalomonc2553372014-07-22 13:09:05 -0700400#else
bsalomon85b4b532016-04-05 11:06:27 -0700401#define kBogusContextType 0
csmartdaltone812d492017-02-21 12:36:05 -0700402#define kBogusContextOverrides 0
mtkleine714e752014-07-31 12:13:48 -0700403#endif
bsalomonc2553372014-07-22 13:09:05 -0700404
svaisanenc47635e2016-01-28 06:05:43 -0800405static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
406
407#if SK_SUPPORT_GPU
408 if (const auto* gpuConfig = config->asConfigGpu()) {
409 if (!FLAGS_gpu)
410 return;
411
svaisanenc47635e2016-01-28 06:05:43 -0800412 const auto ctxType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700413 const auto ctxOverrides = gpuConfig->getContextOverrides();
svaisanenc47635e2016-01-28 06:05:43 -0800414 const auto sampleCount = gpuConfig->getSamples();
415
csmartdaltone812d492017-02-21 12:36:05 -0700416 if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOverrides)) {
svaisanenc47635e2016-01-28 06:05:43 -0800417 const auto maxSampleCount = ctx->caps()->maxSampleCount();
418 if (sampleCount > ctx->caps()->maxSampleCount()) {
419 SkDebugf("Configuration sample count %d exceeds maximum %d.\n",
420 sampleCount, maxSampleCount);
421 return;
422 }
423 } else {
424 SkDebugf("No context was available matching config type and options.\n");
425 return;
426 }
427
428 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800429 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800430 Benchmark::kGPU_Backend,
brianosman1a48e132016-06-20 09:41:41 -0700431 gpuConfig->getColorType(),
svaisanenc47635e2016-01-28 06:05:43 -0800432 kPremul_SkAlphaType,
brianosman1a48e132016-06-20 09:41:41 -0700433 sk_ref_sp(gpuConfig->getColorSpace()),
svaisanenc47635e2016-01-28 06:05:43 -0800434 sampleCount,
435 ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700436 ctxOverrides,
kkinnunend4e1c352016-03-01 23:41:26 -0800437 gpuConfig->getUseDIText()
438 };
svaisanenc47635e2016-01-28 06:05:43 -0800439
440 configs->push_back(target);
441 return;
442 }
443#endif
444
brianosmanb109b8c2016-06-16 13:03:24 -0700445 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
446 if (config->getTag().equals(#name)) { \
447 Config config = { \
448 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
csmartdaltone812d492017-02-21 12:36:05 -0700449 0, kBogusContextType, kBogusContextOverrides, false \
brianosmanb109b8c2016-06-16 13:03:24 -0700450 }; \
451 configs->push_back(config); \
452 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700453 }
mtkleine714e752014-07-31 12:13:48 -0700454
mtklein40b32be2014-07-09 08:46:49 -0700455 if (FLAGS_cpu) {
mtkleinbb6c41b2016-03-08 11:31:11 -0800456 CPU_CONFIG(nonrendering, kNonRendering_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700457 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800458
459 CPU_CONFIG(8888, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700460 kN32_SkColorType, kPremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800461 CPU_CONFIG(565, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700462 kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500463 auto srgbColorSpace = SkColorSpace::MakeSRGB();
mtkleinbb6c41b2016-03-08 11:31:11 -0800464 CPU_CONFIG(srgb, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700465 kN32_SkColorType, kPremul_SkAlphaType, srgbColorSpace)
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500466 auto srgbLinearColorSpace = SkColorSpace::MakeSRGBLinear();
mtkleinbb6c41b2016-03-08 11:31:11 -0800467 CPU_CONFIG(f16, kRaster_Backend,
raftias7c602de2016-10-13 10:45:44 -0700468 kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbLinearColorSpace)
mtklein40b32be2014-07-09 08:46:49 -0700469 }
mtkleinbb6a0282014-07-01 08:43:42 -0700470
svaisanenc47635e2016-01-28 06:05:43 -0800471 #undef CPU_CONFIG
mtkleinf3723212014-06-25 14:08:00 -0700472}
473
svaisanenc47635e2016-01-28 06:05:43 -0800474// Append all configs that are enabled and supported.
475void create_configs(SkTArray<Config>* configs) {
476 SkCommandLineConfigArray array;
477 ParseConfigs(FLAGS_config, &array);
478 for (int i = 0; i < array.count(); ++i) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400479 create_config(array[i].get(), configs);
svaisanenc47635e2016-01-28 06:05:43 -0800480 }
481}
482
brianosman9f1f6e22016-09-15 08:33:02 -0700483// disable warning : switch statement contains default but no 'case' labels
484#if defined _WIN32
485#pragma warning ( push )
486#pragma warning ( disable : 4065 )
487#endif
488
halcanary96fcdcc2015-08-27 07:41:13 -0700489// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700490static Target* is_enabled(Benchmark* bench, const Config& config) {
491 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700492 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700493 }
494
reede5ea5002014-09-03 11:54:58 -0700495 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700496 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700497
halcanary96fcdcc2015-08-27 07:41:13 -0700498 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700499
tomhudsond968a6f2015-03-26 11:28:06 -0700500 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700501#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700502 case Benchmark::kGPU_Backend:
503 target = new GPUTarget(config);
504 break;
bsalomonc2553372014-07-22 13:09:05 -0700505#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700506 default:
507 target = new Target(config);
508 break;
509 }
bsalomonc2553372014-07-22 13:09:05 -0700510
tomhudsond968a6f2015-03-26 11:28:06 -0700511 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700512 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700513 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700514 }
515 return target;
516}
517
brianosman9f1f6e22016-09-15 08:33:02 -0700518#if defined _WIN32
519#pragma warning ( pop )
520#endif
521
reed42943c82016-09-12 12:01:44 -0700522static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700523 uint32_t minOutputSize, int* width, int* height) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400524 std::unique_ptr<SkBitmapRegionDecoder> brd(
msarettd1227a72016-05-18 06:23:57 -0700525 SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy));
msarett7f691442015-09-22 11:56:16 -0700526 if (nullptr == brd.get()) {
527 // This is indicates that subset decoding is not supported for a particular image format.
528 return false;
529 }
530
msarett7f691442015-09-22 11:56:16 -0700531 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
532 (uint32_t) brd->height()) {
533 // This indicates that the image is not large enough to decode a
534 // minOutputSize x minOutputSize subset at the given sampleSize.
535 return false;
536 }
537
538 // Set the image width and height. The calling code will use this to choose subsets to decode.
539 *width = brd->width();
540 *height = brd->height();
541 return true;
542}
543
egdaniel3bf92062015-06-26 08:12:46 -0700544static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700545 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700546#if SK_SUPPORT_GPU
547 if (FLAGS_abandonGpuContext) {
548 gGrFactory->abandonContexts();
549 }
550 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
551 gGrFactory->destroyContexts();
552 }
553#endif
554}
555
fmalita6519c212016-09-14 08:05:17 -0700556static void collect_files(const SkCommandLineFlags::StringArray& paths, const char* ext,
557 SkTArray<SkString>* list) {
558 for (int i = 0; i < paths.count(); ++i) {
559 if (SkStrEndsWith(paths[i], ext)) {
560 list->push_back(SkString(paths[i]));
561 } else {
562 SkOSFile::Iter it(paths[i], ext);
563 SkString path;
564 while (it.next(&path)) {
565 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
566 }
567 }
568 }
569}
570
mtkleine714e752014-07-31 12:13:48 -0700571class BenchmarkStream {
572public:
mtklein92007582014-08-01 07:46:52 -0700573 BenchmarkStream() : fBenches(BenchRegistry::Head())
574 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700575 , fCurrentRecording(0)
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400576 , fCurrentPiping(0)
mtklein92007582014-08-01 07:46:52 -0700577 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800578 , fCurrentSKP(0)
fmalita6519c212016-09-14 08:05:17 -0700579 , fCurrentSVG(0)
msarett95f192d2015-02-13 09:05:41 -0800580 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700581 , fCurrentCodec(0)
msarett84451022016-02-11 06:45:51 -0800582 , fCurrentAndroidCodec(0)
msarett7f691442015-09-22 11:56:16 -0700583 , fCurrentBRDImage(0)
msarett2cee9022016-06-03 08:25:21 -0700584 , fCurrentColorImage(0)
msarett95f192d2015-02-13 09:05:41 -0800585 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800586 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700587 , fCurrentSubsetType(0)
msarett84451022016-02-11 06:45:51 -0800588 , fCurrentSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700589 , fCurrentAnimSKP(0) {
fmalita6519c212016-09-14 08:05:17 -0700590 collect_files(FLAGS_skps, ".skp", &fSKPs);
591 collect_files(FLAGS_svgs, ".svg", &fSVGs);
mtkleine714e752014-07-31 12:13:48 -0700592
mtklein92007582014-08-01 07:46:52 -0700593 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
594 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
595 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
596 exit(1);
597 }
598
599 for (int i = 0; i < FLAGS_scales.count(); i++) {
600 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
601 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
602 exit(1);
603 }
604 }
robertphillips5b693772014-11-21 06:19:36 -0800605
cdalton63a82852015-06-29 14:06:10 -0700606 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
607 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700608 exit(1);
609 }
610
robertphillips5b693772014-11-21 06:19:36 -0800611 if (FLAGS_mpd) {
612 fUseMPDs.push_back() = true;
613 }
mtkleinc751ecb2015-06-15 08:56:38 -0700614 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700615
msarett95f192d2015-02-13 09:05:41 -0800616 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700617 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800618 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800619 }
msarett2cee9022016-06-03 08:25:21 -0700620 if (!CollectImages(FLAGS_colorImages, &fColorImages)) {
621 exit(1);
622 }
mtklein95553d92015-03-12 08:24:21 -0700623
msarett95f192d2015-02-13 09:05:41 -0800624 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700625 fColorTypes.push_back(kN32_SkColorType);
626 if (!FLAGS_simpleCodec) {
627 fColorTypes.push_back(kRGB_565_SkColorType);
628 fColorTypes.push_back(kAlpha_8_SkColorType);
629 fColorTypes.push_back(kIndex_8_SkColorType);
630 fColorTypes.push_back(kGray_8_SkColorType);
631 }
mtklein92007582014-08-01 07:46:52 -0700632 }
633
reedca2622b2016-03-18 07:25:55 -0700634 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700635 // Not strictly necessary, as it will be checked again later,
636 // but helps to avoid a lot of pointless work if we're going to skip it.
cdalton91e457d2016-02-17 11:10:16 -0800637 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700638 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700639 }
640
bungemanf93d7112016-09-16 06:24:20 -0700641 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
642 if (!stream) {
mtkleinfd731ce2014-09-10 12:19:30 -0700643 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700644 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700645 }
646
reedca2622b2016-03-18 07:25:55 -0700647 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700648 }
649
fmalita6519c212016-09-14 08:05:17 -0700650 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
651 SkFILEStream stream(path);
652 if (!stream.isValid()) {
653 SkDebugf("Could not read %s.\n", path);
654 return nullptr;
655 }
656
fmalitae1baa7c2016-09-14 12:04:30 -0700657 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
fmalita6519c212016-09-14 08:05:17 -0700658 if (!svgDom) {
659 SkDebugf("Could not parse %s.\n", path);
660 return nullptr;
661 }
662
fmalitae1baa7c2016-09-14 12:04:30 -0700663 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
664 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
665 if (svgDom->containerSize().isEmpty()) {
666 svgDom->setContainerSize(kDefaultContainerSize);
667 }
668
fmalita6519c212016-09-14 08:05:17 -0700669 SkPictureRecorder recorder;
fmalitae1baa7c2016-09-14 12:04:30 -0700670 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
671 svgDom->containerSize().height()));
fmalita6519c212016-09-14 08:05:17 -0700672 return recorder.finishRecordingAsPicture();
673 }
674
mtklein92007582014-08-01 07:46:52 -0700675 Benchmark* next() {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400676 std::unique_ptr<Benchmark> bench;
mtklein65dfd2f2016-02-03 10:40:54 -0800677 do {
678 bench.reset(this->rawNext());
679 if (!bench) {
680 return nullptr;
681 }
682 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
683 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700684 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800685 }
686
687 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700688 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700689 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700690 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700691 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700692 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700693 return bench;
694 }
mtklein92007582014-08-01 07:46:52 -0700695
mtkleine714e752014-07-31 12:13:48 -0700696 while (fGMs) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400697 std::unique_ptr<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700698 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800699 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700700 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700701 fBenchType = "micro";
mtklein18300a32016-03-16 13:53:35 -0700702 return new GMBench(gm.release());
mtkleine714e752014-07-31 12:13:48 -0700703 }
704 }
mtklein92007582014-08-01 07:46:52 -0700705
mtkleinfd731ce2014-09-10 12:19:30 -0700706 // First add all .skps as RecordingBenches.
707 while (fCurrentRecording < fSKPs.count()) {
708 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700709 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
710 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700711 continue;
712 }
713 SkString name = SkOSPath::Basename(path.c_str());
714 fSourceType = "skp";
715 fBenchType = "recording";
Mike Reed90b20052017-03-08 10:39:02 -0500716 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
mtklein051e56d2014-12-04 08:46:51 -0800717 fSKPOps = pic->approximateOpCount();
mtklein8c1a4f82016-08-08 06:56:22 -0700718 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh, FLAGS_lite);
mtkleinfd731ce2014-09-10 12:19:30 -0700719 }
720
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400721 // Add all .skps as PipeBenches.
722 while (fCurrentPiping < fSKPs.count()) {
723 const SkString& path = fSKPs[fCurrentPiping++];
724 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
725 if (!pic) {
726 continue;
727 }
728 SkString name = SkOSPath::Basename(path.c_str());
729 fSourceType = "skp";
730 fBenchType = "piping";
Mike Reed90b20052017-03-08 10:39:02 -0500731 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400732 fSKPOps = pic->approximateOpCount();
733 return new PipingBench(name.c_str(), pic.get());
734 }
735
mtkleinfd731ce2014-09-10 12:19:30 -0700736 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700737 while (fCurrentScale < fScales.count()) {
738 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800739 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700740 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
741 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800742 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700743 continue;
744 }
robertphillips5b693772014-11-21 06:19:36 -0800745
746 while (fCurrentUseMPD < fUseMPDs.count()) {
747 if (FLAGS_bbh) {
748 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
749 SkRTreeFactory factory;
750 SkPictureRecorder recorder;
robertphillips5b693772014-11-21 06:19:36 -0800751 pic->playback(recorder.beginRecording(pic->cullRect().width(),
752 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800753 &factory,
robertphillipsdda54452016-07-13 13:27:16 -0700754 0));
reedca2622b2016-03-18 07:25:55 -0700755 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800756 }
757 SkString name = SkOSPath::Basename(path.c_str());
758 fSourceType = "skp";
759 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700760 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
761 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700762 }
robertphillips5b693772014-11-21 06:19:36 -0800763 fCurrentUseMPD = 0;
764 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700765 }
fmalita6519c212016-09-14 08:05:17 -0700766
767 while (fCurrentSVG++ < fSVGs.count()) {
768 const char* path = fSVGs[fCurrentSVG - 1].c_str();
769 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
770 fSourceType = "svg";
771 fBenchType = "playback";
772 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
773 fScales[fCurrentScale], false, FLAGS_loopSKP);
774 }
775 }
776
mtklein92007582014-08-01 07:46:52 -0700777 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700778 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700779 fCurrentScale++;
780 }
781
joshualitt261c3ad2015-04-27 09:16:57 -0700782 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700783 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700784 while (fCurrentAnimSKP < fSKPs.count()) {
785 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700786 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
787 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700788 fCurrentAnimSKP++;
789 continue;
790 }
791
792 fCurrentAnimSKP++;
793 SkString name = SkOSPath::Basename(path.c_str());
Hal Canary2db83612016-11-04 13:02:54 -0400794 sk_sp<SKPAnimationBench::Animation> animation(
cdalton63a82852015-06-29 14:06:10 -0700795 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
Hal Canary2db83612016-11-04 13:02:54 -0400796 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation.get(),
halcanary385fe4d2015-08-26 13:07:48 -0700797 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700798 }
799 }
800
scroggo60869a42015-04-01 12:09:17 -0700801 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700802 fSourceType = "image";
803 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700804 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800805 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
806 continue;
807 }
bungeman38d909e2016-08-02 14:40:46 -0700808 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Ben Wagner145dbcd2016-11-03 14:40:50 -0400809 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700810 if (!codec) {
811 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700812 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700813 continue;
814 }
scroggo21027992015-04-02 13:22:38 -0700815
scroggo60869a42015-04-01 12:09:17 -0700816 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700817 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700818
msarettc7796b92016-01-07 14:20:20 -0800819 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700820 if (FLAGS_simpleCodec) {
821 if (kUnpremul_SkAlphaType == alphaType) {
822 alphaType = kPremul_SkAlphaType;
823 }
824
825 fCurrentColorType++;
826 } else {
827 switch (alphaType) {
828 case kOpaque_SkAlphaType:
829 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800830 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700831 break;
832 case kUnpremul_SkAlphaType:
833 case kPremul_SkAlphaType:
834 if (0 == fCurrentAlphaType) {
835 // Test unpremul first.
836 alphaType = kUnpremul_SkAlphaType;
837 fCurrentAlphaType++;
838 } else {
839 // Test premul.
840 alphaType = kPremul_SkAlphaType;
841 fCurrentAlphaType = 0;
842 fCurrentColorType++;
843 }
844 break;
845 default:
846 SkASSERT(false);
847 fCurrentColorType++;
848 break;
849 }
scroggo21027992015-04-02 13:22:38 -0700850 }
851
msarettc7796b92016-01-07 14:20:20 -0800852 // Make sure we can decode to this color type and alpha type.
853 SkImageInfo info =
854 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700855 const size_t rowBytes = info.minRowBytes();
856 SkAutoMalloc storage(info.getSafeSize(rowBytes));
857
858 // Used if fCurrentColorType is kIndex_8_SkColorType
859 int colorCount = 256;
860 SkPMColor colors[256];
861
scroggoeb602a52015-07-09 08:16:03 -0700862 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700863 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700864 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700865 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700866 case SkCodec::kSuccess:
867 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700868 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700869 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700870 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700871 // This is okay. Not all conversions are valid.
872 break;
scroggo60869a42015-04-01 12:09:17 -0700873 default:
874 // This represents some sort of failure.
875 SkASSERT(false);
876 break;
877 }
878 }
879 fCurrentColorType = 0;
880 }
881
msarett84451022016-02-11 06:45:51 -0800882 // Run AndroidCodecBenches
883 const int sampleSizes[] = { 2, 4, 8 };
884 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
885 fSourceType = "image";
886 fBenchType = "skandroidcodec";
887
888 const SkString& path = fImages[fCurrentAndroidCodec];
889 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
890 continue;
891 }
bungeman38d909e2016-08-02 14:40:46 -0700892 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Ben Wagner145dbcd2016-11-03 14:40:50 -0400893 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800894 if (!codec) {
895 // Nothing to time.
896 SkDebugf("Cannot find codec for %s\n", path.c_str());
897 continue;
898 }
899
900 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
901 int sampleSize = sampleSizes[fCurrentSampleSize];
902 fCurrentSampleSize++;
903 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
904 // Avoid benchmarking scaled decodes of already small images.
905 break;
906 }
907
bungeman38d909e2016-08-02 14:40:46 -0700908 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
909 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -0800910 }
911 fCurrentSampleSize = 0;
912 }
913
msarett7f691442015-09-22 11:56:16 -0700914 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700915 // We intend to create benchmarks that model the use cases in
916 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
917 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
918 // the image. For that reason, we will benchmark decodes in five representative locations
919 // in the image. Additionally, this use case utilizes power of two scaling, so we will
920 // test on power of two sample sizes. The output tile is always 512x512, so, when a
921 // sampleSize is used, the size of the subset that is decoded is always
922 // (sampleSize*512)x(sampleSize*512).
923 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700924 // All use cases we are aware of only scale by powers of two.
925 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
926 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800927 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700928 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800929 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700930 fSourceType = "image";
931 fBenchType = "BRD";
932
mtklein6f0ff912016-01-11 09:04:21 -0800933 const SkString& path = fImages[fCurrentBRDImage];
934 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
935 continue;
936 }
scroggo860e8a62015-10-15 07:51:28 -0700937
msarettd1227a72016-05-18 06:23:57 -0700938 while (fCurrentColorType < fColorTypes.count()) {
939 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
940 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700941
bungeman38d909e2016-08-02 14:40:46 -0700942 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -0700943 const SkColorType colorType = fColorTypes[fCurrentColorType];
944 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
945 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -0700946
msarettd1227a72016-05-18 06:23:57 -0700947 int width = 0;
948 int height = 0;
reed42943c82016-09-12 12:01:44 -0700949 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -0700950 &width, &height)) {
951 break;
msarett7f691442015-09-22 11:56:16 -0700952 }
msarettd1227a72016-05-18 06:23:57 -0700953
954 SkString basename = SkOSPath::Basename(path.c_str());
955 SkIRect subset;
956 const uint32_t subsetSize = sampleSize * minOutputSize;
957 switch (currentSubsetType) {
958 case kTopLeft_SubsetType:
959 basename.append("_TopLeft");
960 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
961 break;
962 case kTopRight_SubsetType:
963 basename.append("_TopRight");
964 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
965 subsetSize);
966 break;
967 case kMiddle_SubsetType:
968 basename.append("_Middle");
969 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
970 (height - subsetSize) / 2, subsetSize, subsetSize);
971 break;
972 case kBottomLeft_SubsetType:
973 basename.append("_BottomLeft");
974 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
975 subsetSize);
976 break;
977 case kBottomRight_SubsetType:
978 basename.append("_BottomRight");
979 subset = SkIRect::MakeXYWH(width - subsetSize,
980 height - subsetSize, subsetSize, subsetSize);
981 break;
982 default:
983 SkASSERT(false);
984 }
985
986 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
987 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -0700988 }
msarettd1227a72016-05-18 06:23:57 -0700989 fCurrentSubsetType = 0;
990 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -0700991 }
msarettd1227a72016-05-18 06:23:57 -0700992 fCurrentSampleSize = 0;
993 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -0700994 }
msarettd1227a72016-05-18 06:23:57 -0700995 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -0700996 }
997
msarett2cee9022016-06-03 08:25:21 -0700998 while (fCurrentColorImage < fColorImages.count()) {
999 fSourceType = "colorimage";
1000 fBenchType = "skcolorcodec";
1001 const SkString& path = fColorImages[fCurrentColorImage];
1002 fCurrentColorImage++;
1003 sk_sp<SkData> encoded = SkData::MakeFromFileName(path.c_str());
1004 if (encoded) {
1005 return new ColorCodecBench(SkOSPath::Basename(path.c_str()).c_str(),
1006 std::move(encoded));
1007 } else {
1008 SkDebugf("Could not read file %s.\n", path.c_str());
1009 }
1010 }
1011
halcanary96fcdcc2015-08-27 07:41:13 -07001012 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001013 }
mtklein92007582014-08-01 07:46:52 -07001014
1015 void fillCurrentOptions(ResultsWriter* log) const {
1016 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001017 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001018 if (0 == strcmp(fSourceType, "skp")) {
1019 log->configOption("clip",
1020 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1021 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001022 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -07001023 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001024 if (fCurrentUseMPD > 0) {
1025 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1026 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1027 }
mtklein92007582014-08-01 07:46:52 -07001028 }
mtklein051e56d2014-12-04 08:46:51 -08001029 if (0 == strcmp(fBenchType, "recording")) {
1030 log->metric("bytes", fSKPBytes);
1031 log->metric("ops", fSKPOps);
1032 }
mtklein92007582014-08-01 07:46:52 -07001033 }
1034
mtkleine714e752014-07-31 12:13:48 -07001035private:
msarettb23e6aa2015-06-09 13:56:10 -07001036 enum SubsetType {
1037 kTopLeft_SubsetType = 0,
1038 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001039 kMiddle_SubsetType = 2,
1040 kBottomLeft_SubsetType = 3,
1041 kBottomRight_SubsetType = 4,
1042 kTranslate_SubsetType = 5,
1043 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001044 kLast_SubsetType = kZoom_SubsetType,
1045 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001046 };
1047
mtkleine714e752014-07-31 12:13:48 -07001048 const BenchRegistry* fBenches;
1049 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001050 SkIRect fClip;
1051 SkTArray<SkScalar> fScales;
1052 SkTArray<SkString> fSKPs;
fmalita6519c212016-09-14 08:05:17 -07001053 SkTArray<SkString> fSVGs;
robertphillips5b693772014-11-21 06:19:36 -08001054 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001055 SkTArray<SkString> fImages;
msarett2cee9022016-06-03 08:25:21 -07001056 SkTArray<SkString> fColorImages;
msarett74deb982015-10-20 16:45:56 -07001057 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001058 SkScalar fZoomMax;
1059 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001060
mtklein051e56d2014-12-04 08:46:51 -08001061 double fSKPBytes, fSKPOps;
1062
mtkleinfd731ce2014-09-10 12:19:30 -07001063 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1064 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1065 int fCurrentRecording;
Mike Reed9cdd2ab2016-10-21 10:43:36 -04001066 int fCurrentPiping;
mtklein92007582014-08-01 07:46:52 -07001067 int fCurrentScale;
1068 int fCurrentSKP;
fmalita6519c212016-09-14 08:05:17 -07001069 int fCurrentSVG;
robertphillips5b693772014-11-21 06:19:36 -08001070 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001071 int fCurrentCodec;
msarett84451022016-02-11 06:45:51 -08001072 int fCurrentAndroidCodec;
msarett7f691442015-09-22 11:56:16 -07001073 int fCurrentBRDImage;
msarett2cee9022016-06-03 08:25:21 -07001074 int fCurrentColorImage;
msarett95f192d2015-02-13 09:05:41 -08001075 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -08001076 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -07001077 int fCurrentSubsetType;
msarett84451022016-02-11 06:45:51 -08001078 int fCurrentSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001079 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001080};
1081
msarettc149f0e2016-01-04 11:35:43 -08001082// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1083// This prints something every once in a while so that it knows we're still working.
1084static void start_keepalive() {
1085 struct Loop {
1086 static void forever(void*) {
1087 for (;;) {
1088 static const int kSec = 1200;
1089 #if defined(SK_BUILD_FOR_WIN)
1090 Sleep(kSec * 1000);
1091 #else
1092 sleep(kSec);
1093 #endif
1094 SkDebugf("\nBenchmarks still running...\n");
1095 }
1096 }
1097 };
1098 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1099 intentionallyLeaked->start();
1100}
1101
Mike Kleinbe28ee22017-02-06 12:46:20 -05001102int main(int argc, char** argv) {
1103 SkCommandLineFlags::Parse(argc, argv);
Mike Kleinadacaef2017-02-06 09:26:14 -05001104#if defined(SK_BUILD_FOR_IOS)
1105 cd_Documents();
1106#endif
jcgregorio3b27ade2014-11-13 08:06:40 -08001107 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001108 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001109 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001110
krajcevski69a55602014-08-13 10:46:31 -07001111#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001112 GrContextOptions grContextOpts;
csmartdalton008b9d82017-02-22 12:00:42 -07001113 grContextOpts.fGpuPathRenderers = CollectGpuPathRenderersFromFlags();
halcanary385fe4d2015-08-26 13:07:48 -07001114 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001115#endif
1116
bsalomon06cddec2014-10-24 10:40:50 -07001117 if (FLAGS_veryVerbose) {
1118 FLAGS_verbose = true;
1119 }
1120
bsalomon6eb03cc2014-08-07 14:28:50 -07001121 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001122 FLAGS_samples = 1;
1123 FLAGS_gpuFrameLag = 0;
1124 }
1125
bsalomon6eb03cc2014-08-07 14:28:50 -07001126 if (!FLAGS_writePath.isEmpty()) {
1127 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1128 if (!sk_mkdir(FLAGS_writePath[0])) {
1129 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001130 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001131 }
1132 }
1133
Ben Wagner145dbcd2016-11-03 14:40:50 -04001134 std::unique_ptr<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001135 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001136#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001137 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001138#else
1139 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1140 return 1;
1141#endif
mtklein60317d0f2014-07-14 11:30:37 -07001142 }
mtklein1915b622014-08-20 11:45:00 -07001143
1144 if (1 == FLAGS_properties.count() % 2) {
1145 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1146 return 1;
1147 }
1148 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1149 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1150 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001151
1152 if (1 == FLAGS_key.count() % 2) {
1153 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1154 return 1;
1155 }
1156 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001157 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001158 }
mtklein60317d0f2014-07-14 11:30:37 -07001159
mtkleinf3723212014-06-25 14:08:00 -07001160 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001161 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001162
cdaltone1b89582015-06-25 19:17:08 -07001163 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001164
bsalomon6eb03cc2014-08-07 14:28:50 -07001165 if (kAutoTuneLoops != FLAGS_loops) {
1166 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001167 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001168 SkDebugf("! -> high variance, ? -> moderate variance\n");
1169 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001170 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001171 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001172 } else {
mtkleind75c4662015-04-30 07:11:22 -07001173 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001174 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001175 }
1176
svaisanenc47635e2016-01-28 06:05:43 -08001177 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001178 create_configs(&configs);
1179
joshualitt3ebd0502016-02-09 07:18:08 -08001180#ifdef THERMAL_MANAGER_SUPPORTED
1181 int tmEnabled, tmThreshold, tmSleepTimeMs, tmTimeoutMs;
1182 if (4 != sscanf(FLAGS_useThermalManager[0], "%d,%d,%d,%d",
1183 &tmEnabled, &tmThreshold, &tmSleepTimeMs, &tmTimeoutMs)) {
1184 SkDebugf("Can't parse %s from --useThermalManager.\n", FLAGS_useThermalManager[0]);
1185 exit(1);
1186 }
1187 ThermalManager tm(tmThreshold, tmSleepTimeMs, tmTimeoutMs);
1188#endif
1189
msarettc149f0e2016-01-04 11:35:43 -08001190 if (FLAGS_keepAlive) {
1191 start_keepalive();
1192 }
1193
Yuqian Liba62b4a2016-12-14 13:46:53 -05001194 gSkUseAnalyticAA = FLAGS_analyticAA;
liyuqian38911a72016-10-04 11:23:22 -07001195
Yuqian Li550148b2017-01-13 10:13:13 -05001196 if (FLAGS_forceAnalyticAA) {
1197 gSkForceAnalyticAA = true;
1198 }
1199
mtkleine070c2b2014-10-14 08:40:43 -07001200 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001201 BenchmarkStream benchStream;
1202 while (Benchmark* b = benchStream.next()) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001203 std::unique_ptr<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001204 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001205 continue;
1206 }
1207
svaisanenc47635e2016-01-28 06:05:43 -08001208 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001209 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001210 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001211 }
egdaniel3bf92062015-06-26 08:12:46 -07001212 for (int i = 0; i < configs.count(); ++i) {
joshualitt3ebd0502016-02-09 07:18:08 -08001213#ifdef THERMAL_MANAGER_SUPPORTED
1214 if (tmEnabled && !tm.coolOffIfNecessary()) {
1215 SkDebugf("Could not cool off, timings will be throttled\n");
1216 }
1217#endif
egdaniel3bf92062015-06-26 08:12:46 -07001218 Target* target = is_enabled(b, configs[i]);
1219 if (!target) {
1220 continue;
1221 }
mtkleinf3723212014-06-25 14:08:00 -07001222
halcanary96fcdcc2015-08-27 07:41:13 -07001223 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001224 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001225 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001226
brianosman7a5ada82016-02-08 13:49:12 -08001227 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001228 SkDebugf("Running %s\t%s\n"
1229 , bench->getUniqueName()
1230 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001231 if (FLAGS_dryRun) {
1232 continue;
1233 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001234 }
1235
egdaniel3bf92062015-06-26 08:12:46 -07001236 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001237 bench->perCanvasPreDraw(canvas);
1238
cdaltone1b89582015-06-25 19:17:08 -07001239 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001240 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001241 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1242 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001243
mtkleinbbba1682015-10-28 11:36:30 -07001244 if (FLAGS_ms) {
1245 samples.reset();
1246 auto stop = now_ms() + FLAGS_ms;
1247 do {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001248 samples.push_back(time(loops, bench.get(), target) / loops);
mtkleinbbba1682015-10-28 11:36:30 -07001249 } while (now_ms() < stop);
1250 } else {
cdaltone1b89582015-06-25 19:17:08 -07001251 samples.reset(FLAGS_samples);
1252 for (int s = 0; s < FLAGS_samples; s++) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001253 samples[s] = time(loops, bench.get(), target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001254 }
cdaltone1b89582015-06-25 19:17:08 -07001255 }
mtkleinf3723212014-06-25 14:08:00 -07001256
joshualitte45c81c2015-12-02 09:05:37 -08001257#if SK_SUPPORT_GPU
1258 SkTArray<SkString> keys;
1259 SkTArray<double> values;
1260 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1261 if (gpuStatsDump) {
1262 // TODO cache stats
1263 bench->getGpuStats(canvas, &keys, &values);
1264 }
1265#endif
1266
robertphillips5b693772014-11-21 06:19:36 -08001267 bench->perCanvasPostDraw(canvas);
1268
egdaniel3bf92062015-06-26 08:12:46 -07001269 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001270 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001271 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001272 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001273 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001274 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001275 }
1276
1277 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001278 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001279 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001280 continue;
1281 }
1282
cdaltone1b89582015-06-25 19:17:08 -07001283 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001284 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001285 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001286 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001287 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001288 log->metric("min_ms", stats.min);
George Burgess IV75b57182016-12-06 10:53:52 -08001289 log->metrics("samples", samples);
joshualitte45c81c2015-12-02 09:05:37 -08001290#if SK_SUPPORT_GPU
1291 if (gpuStatsDump) {
1292 // dump to json, only SKPBench currently returns valid keys / values
1293 SkASSERT(keys.count() == values.count());
1294 for (int i = 0; i < keys.count(); i++) {
1295 log->metric(keys[i].c_str(), values[i]);
1296 }
1297 }
1298#endif
1299
mtkleine070c2b2014-10-14 08:40:43 -07001300 if (runs++ % FLAGS_flushEvery == 0) {
1301 log->flush();
1302 }
mtklein60317d0f2014-07-14 11:30:37 -07001303
bsalomon6eb03cc2014-08-07 14:28:50 -07001304 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001305 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001306 config = ""; // Only print the config if we run the same bench on more than one.
1307 }
mtkleind75c4662015-04-30 07:11:22 -07001308 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1309 , sk_tools::getCurrResidentSetSizeMB()
1310 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001311 , bench->getUniqueName()
1312 , config);
mtkleinf3723212014-06-25 14:08:00 -07001313 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001314 const char* mark = " ";
1315 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1316 if (stddev_percent > 5) mark = "?";
1317 if (stddev_percent > 10) mark = "!";
1318
1319 SkDebugf("%10.2f %s\t%s\t%s\n",
1320 stats.median*1e3, mark, bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001321 } else {
1322 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001323 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1324 , sk_tools::getCurrResidentSetSizeMB()
1325 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001326 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001327 , HUMANIZE(stats.min)
1328 , HUMANIZE(stats.median)
1329 , HUMANIZE(stats.mean)
1330 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001331 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001332 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001333 , config
mtklein96289052014-09-10 12:05:59 -07001334 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001335 );
1336 }
joshualitte45c81c2015-12-02 09:05:37 -08001337
bsalomonb12ea412015-02-02 21:19:50 -08001338#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001339 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001340 GrContext* context = gGrFactory->get(configs[i].ctxType,
csmartdaltone812d492017-02-21 12:36:05 -07001341 configs[i].ctxOverrides);
kkinnunen5219fd92015-12-10 06:28:13 -08001342 context->printCacheStats();
1343 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001344 }
1345#endif
joshualitte45c81c2015-12-02 09:05:37 -08001346
cdalton2c56ba52015-06-26 13:32:53 -07001347 if (FLAGS_verbose) {
1348 SkDebugf("Samples: ");
1349 for (int i = 0; i < samples.count(); i++) {
1350 SkDebugf("%s ", HUMANIZE(samples[i]));
1351 }
1352 SkDebugf("%s\n", bench->getUniqueName());
1353 }
egdaniel3bf92062015-06-26 08:12:46 -07001354 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001355 }
mtkleinf3723212014-06-25 14:08:00 -07001356 }
1357
Herb Derby5a523fe2017-01-26 16:48:28 -05001358 SkGraphics::PurgeAllCaches();
1359
mtkleine1091452014-12-04 10:47:02 -08001360 log->bench("memory_usage", 0,0);
1361 log->config("meta");
1362 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1363
joshualitte0b19d42015-03-26 10:41:02 -07001364#if SK_SUPPORT_GPU
1365 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1366 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001367 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001368#endif
1369
mtkleinf3723212014-06-25 14:08:00 -07001370 return 0;
1371}