blob: 4e1da2bd980775f264d581df0a277f91e0138202 [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"
msarett95f192d2015-02-13 09:05:41 -080036#include "SkData.h"
mtkleinf3723212014-06-25 14:08:00 -070037#include "SkGraphics.h"
halcanary4dbbd042016-06-07 17:21:10 -070038#include "SkLeanWindows.h"
mtklein20840502014-08-21 15:51:22 -070039#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050040#include "SkOSPath.h"
mtklein20840502014-08-21 15:51:22 -070041#include "SkPictureRecorder.h"
mtklein051e56d2014-12-04 08:46:51 -080042#include "SkPictureUtils.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
mtkleinbbba1682015-10-28 11:36:30 -0700131static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
132
mtkleinf3723212014-06-25 14:08:00 -0700133static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700134 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800135 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700136}
mtklein55b0ffc2014-07-17 08:38:23 -0700137#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700138
tomhudsond968a6f2015-03-26 11:28:06 -0700139bool Target::init(SkImageInfo info, Benchmark* bench) {
140 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700141 this->surface = SkSurface::MakeRaster(info);
142 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700143 return false;
144 }
145 }
146 return true;
147}
148bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700149 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700150 if (!canvas) {
151 return false;
152 }
153 bmp->setInfo(canvas->imageInfo());
154 if (!canvas->readPixels(bmp, 0, 0)) {
155 SkDebugf("Can't read canvas pixels.\n");
156 return false;
157 }
158 return true;
159}
160
161#if SK_SUPPORT_GPU
162struct GPUTarget : public Target {
bsalomonc8699322016-05-11 11:55:36 -0700163 explicit GPUTarget(const Config& c) : Target(c), context(nullptr) { }
164 TestContext* context;
tomhudsond968a6f2015-03-26 11:28:06 -0700165
166 void setup() override {
bsalomonc8699322016-05-11 11:55:36 -0700167 this->context->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700168 // Make sure we're done with whatever came before.
bsalomonc8699322016-05-11 11:55:36 -0700169 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700170 }
171 void endTiming() override {
bsalomonc8699322016-05-11 11:55:36 -0700172 if (this->context) {
173 this->context->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700174 }
175 }
176 void fence() override {
bsalomonc8699322016-05-11 11:55:36 -0700177 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700178 }
mtkleind75c4662015-04-30 07:11:22 -0700179
cdaltond416a5b2015-06-23 13:23:44 -0700180 bool needsFrameTiming(int* maxFrameLag) const override {
bsalomonc8699322016-05-11 11:55:36 -0700181 if (!this->context->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700182 // Frame lag is unknown.
183 *maxFrameLag = FLAGS_gpuFrameLag;
184 }
185 return true;
186 }
tomhudsond968a6f2015-03-26 11:28:06 -0700187 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700188 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
189 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700190 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700191 this->surface = SkSurface::MakeRenderTarget(gGrFactory->get(this->config.ctxType,
192 this->config.ctxOptions),
bsalomon5ec26ae2016-02-25 08:33:02 -0800193 SkBudgeted::kNo, info,
reede8f30622016-03-23 18:59:25 -0700194 this->config.samples, &props);
bsalomonc8699322016-05-11 11:55:36 -0700195 this->context = gGrFactory->getContextInfo(this->config.ctxType,
196 this->config.ctxOptions).testContext();
tomhudsond968a6f2015-03-26 11:28:06 -0700197 if (!this->surface.get()) {
198 return false;
199 }
bsalomonc8699322016-05-11 11:55:36 -0700200 if (!this->context->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700201 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800202 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700203 }
tomhudsond968a6f2015-03-26 11:28:06 -0700204 return true;
205 }
206 void fillOptions(ResultsWriter* log) override {
207 const GrGLubyte* version;
bsalomonc8699322016-05-11 11:55:36 -0700208 if (this->context->backend() == kOpenGL_GrBackend) {
209 const GrGLInterface* gl =
210 reinterpret_cast<const GrGLInterface*>(this->context->backendContext());
211 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
212 log->configOption("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700213
bsalomonc8699322016-05-11 11:55:36 -0700214 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
215 log->configOption("GL_RENDERER", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700216
bsalomonc8699322016-05-11 11:55:36 -0700217 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VENDOR));
218 log->configOption("GL_VENDOR", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700219
bsalomonc8699322016-05-11 11:55:36 -0700220 GR_GL_CALL_RET(gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
221 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
222 }
tomhudsond968a6f2015-03-26 11:28:06 -0700223 }
224};
mtkleind75c4662015-04-30 07:11:22 -0700225
tomhudsond968a6f2015-03-26 11:28:06 -0700226#endif
227
tomhudson75a0ebb2015-03-27 12:11:44 -0700228static double time(int loops, Benchmark* bench, Target* target) {
229 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700230 if (canvas) {
231 canvas->clear(SK_ColorWHITE);
232 }
joshualitt8a6697a2015-09-30 12:11:07 -0700233 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700234 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700235 canvas = target->beginTiming(canvas);
236 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700237 if (canvas) {
238 canvas->flush();
239 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700240 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700241 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700242 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700243 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700244}
245
mtkleinf3723212014-06-25 14:08:00 -0700246static double estimate_timer_overhead() {
247 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700248 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700249 double start = now_ms();
250 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700251 }
252 return overhead / FLAGS_overheadLoops;
253}
254
reed53249782014-10-10 09:09:52 -0700255static int detect_forever_loops(int loops) {
256 // look for a magic run-forever value
257 if (loops < 0) {
258 loops = SK_MaxS32;
259 }
260 return loops;
261}
262
mtklein55b0ffc2014-07-17 08:38:23 -0700263static int clamp_loops(int loops) {
264 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800265 SkDebugf("ERROR: clamping loops from %d to 1. "
266 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700267 return 1;
268 }
269 if (loops > FLAGS_maxLoops) {
270 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
271 return FLAGS_maxLoops;
272 }
273 return loops;
274}
275
tomhudsond968a6f2015-03-26 11:28:06 -0700276static bool write_canvas_png(Target* target, const SkString& filename) {
277
bsalomon6eb03cc2014-08-07 14:28:50 -0700278 if (filename.isEmpty()) {
279 return false;
280 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700281 if (target->getCanvas() &&
282 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700283 return false;
284 }
tomhudsond968a6f2015-03-26 11:28:06 -0700285
bsalomon6eb03cc2014-08-07 14:28:50 -0700286 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700287
288 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700289 return false;
290 }
tomhudsond968a6f2015-03-26 11:28:06 -0700291
bsalomon6eb03cc2014-08-07 14:28:50 -0700292 SkString dir = SkOSPath::Dirname(filename.c_str());
293 if (!sk_mkdir(dir.c_str())) {
294 SkDebugf("Can't make dir %s.\n", dir.c_str());
295 return false;
296 }
297 SkFILEWStream stream(filename.c_str());
298 if (!stream.isValid()) {
299 SkDebugf("Can't write %s.\n", filename.c_str());
300 return false;
301 }
Hal Canarydb683012016-11-23 08:55:18 -0700302 if (!SkEncodeImage(&stream, bmp, SkEncodedImageFormat::kPNG, 100)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700303 SkDebugf("Can't encode a PNG.\n");
304 return false;
305 }
306 return true;
307}
308
309static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700310static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700311 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700312 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700313 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700314 int loops = bench->calculateLoops(FLAGS_loops);
315 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700316 while (bench_plus_overhead < overhead) {
317 if (round++ == FLAGS_maxCalibrationAttempts) {
318 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700319 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700320 return kFailedLoops;
321 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700322 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700323 }
mtklein2069e222014-08-04 13:57:39 -0700324 }
mtkleinf3723212014-06-25 14:08:00 -0700325
mtkleinbb6a0282014-07-01 08:43:42 -0700326 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700327 // We'll pick N to make timer overhead negligible:
328 //
mtkleinbb6a0282014-07-01 08:43:42 -0700329 // overhead
330 // ------------------------- < FLAGS_overheadGoal
331 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700332 //
Hal Canary55325b72017-01-03 10:36:17 -0500333 // where bench_plus_overhead ~=~ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700334 //
335 // Doing some math, we get:
336 //
mtkleinbb6a0282014-07-01 08:43:42 -0700337 // (overhead / FLAGS_overheadGoal) - overhead
338 // ------------------------------------------ < N
339 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700340 //
341 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700342 if (kAutoTuneLoops == loops) {
343 const double numer = overhead / FLAGS_overheadGoal - overhead;
344 const double denom = bench_plus_overhead - overhead;
345 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700346 loops = clamp_loops(loops);
347 } else {
348 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700349 }
mtkleinbb6a0282014-07-01 08:43:42 -0700350
mtkleinbb6a0282014-07-01 08:43:42 -0700351 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700352}
353
cdaltone1b89582015-06-25 19:17:08 -0700354static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700355 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700356 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700357 if (kAutoTuneLoops == loops) {
358 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700359 double elapsed = 0;
360 do {
mtklein527930f2014-11-06 08:04:34 -0800361 if (1<<30 == loops) {
362 // We're about to wrap. Something's wrong with the bench.
363 loops = 0;
364 break;
365 }
mtkleina189ccd2014-07-14 12:28:47 -0700366 loops *= 2;
367 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700368 // _this_ round, not still timing last round.
369 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700370 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700371 }
372 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700373
mtkleina189ccd2014-07-14 12:28:47 -0700374 // We've overshot at least a little. Scale back linearly.
375 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700376 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700377
tomhudsond968a6f2015-03-26 11:28:06 -0700378 // Make sure we're not still timing our calibration.
379 target->fence();
reed53249782014-10-10 09:09:52 -0700380 } else {
381 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700382 }
mtkleinbb6a0282014-07-01 08:43:42 -0700383
384 // Pretty much the same deal as the calibration: do some warmup to make
385 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700386 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700387 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700388 }
mtkleinbb6a0282014-07-01 08:43:42 -0700389
mtkleinbb6a0282014-07-01 08:43:42 -0700390 return loops;
391}
mtkleinbb6a0282014-07-01 08:43:42 -0700392
bsalomonc2553372014-07-22 13:09:05 -0700393#if SK_SUPPORT_GPU
bsalomon85b4b532016-04-05 11:06:27 -0700394#define kBogusContextType GrContextFactory::kNativeGL_ContextType
csmartdalton6270e552016-09-13 10:41:49 -0700395#define kBogusContextOptions GrContextFactory::ContextOptions::kNone
bsalomonc2553372014-07-22 13:09:05 -0700396#else
bsalomon85b4b532016-04-05 11:06:27 -0700397#define kBogusContextType 0
398#define kBogusContextOptions 0
mtkleine714e752014-07-31 12:13:48 -0700399#endif
bsalomonc2553372014-07-22 13:09:05 -0700400
svaisanenc47635e2016-01-28 06:05:43 -0800401static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
402
403#if SK_SUPPORT_GPU
404 if (const auto* gpuConfig = config->asConfigGpu()) {
405 if (!FLAGS_gpu)
406 return;
407
svaisanenc47635e2016-01-28 06:05:43 -0800408 const auto ctxType = gpuConfig->getContextType();
csmartdalton6270e552016-09-13 10:41:49 -0700409 const auto ctxOptions = gpuConfig->getContextOptions();
svaisanenc47635e2016-01-28 06:05:43 -0800410 const auto sampleCount = gpuConfig->getSamples();
411
412 if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOptions)) {
413 const auto maxSampleCount = ctx->caps()->maxSampleCount();
414 if (sampleCount > ctx->caps()->maxSampleCount()) {
415 SkDebugf("Configuration sample count %d exceeds maximum %d.\n",
416 sampleCount, maxSampleCount);
417 return;
418 }
419 } else {
420 SkDebugf("No context was available matching config type and options.\n");
421 return;
422 }
423
424 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800425 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800426 Benchmark::kGPU_Backend,
brianosman1a48e132016-06-20 09:41:41 -0700427 gpuConfig->getColorType(),
svaisanenc47635e2016-01-28 06:05:43 -0800428 kPremul_SkAlphaType,
brianosman1a48e132016-06-20 09:41:41 -0700429 sk_ref_sp(gpuConfig->getColorSpace()),
svaisanenc47635e2016-01-28 06:05:43 -0800430 sampleCount,
431 ctxType,
kkinnunend4e1c352016-03-01 23:41:26 -0800432 ctxOptions,
433 gpuConfig->getUseDIText()
434 };
svaisanenc47635e2016-01-28 06:05:43 -0800435
436 configs->push_back(target);
437 return;
438 }
439#endif
440
brianosmanb109b8c2016-06-16 13:03:24 -0700441 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
442 if (config->getTag().equals(#name)) { \
443 Config config = { \
444 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
445 0, kBogusContextType, kBogusContextOptions, false \
446 }; \
447 configs->push_back(config); \
448 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700449 }
mtkleine714e752014-07-31 12:13:48 -0700450
mtklein40b32be2014-07-09 08:46:49 -0700451 if (FLAGS_cpu) {
mtkleinbb6c41b2016-03-08 11:31:11 -0800452 CPU_CONFIG(nonrendering, kNonRendering_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700453 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800454
455 CPU_CONFIG(8888, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700456 kN32_SkColorType, kPremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800457 CPU_CONFIG(565, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700458 kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
Brian Osman526972e2016-10-24 09:24:02 -0400459 auto srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
mtkleinbb6c41b2016-03-08 11:31:11 -0800460 CPU_CONFIG(srgb, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700461 kN32_SkColorType, kPremul_SkAlphaType, srgbColorSpace)
Brian Osman526972e2016-10-24 09:24:02 -0400462 auto srgbLinearColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named);
mtkleinbb6c41b2016-03-08 11:31:11 -0800463 CPU_CONFIG(f16, kRaster_Backend,
raftias7c602de2016-10-13 10:45:44 -0700464 kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbLinearColorSpace)
mtklein40b32be2014-07-09 08:46:49 -0700465 }
mtkleinbb6a0282014-07-01 08:43:42 -0700466
svaisanenc47635e2016-01-28 06:05:43 -0800467 #undef CPU_CONFIG
mtkleinf3723212014-06-25 14:08:00 -0700468}
469
svaisanenc47635e2016-01-28 06:05:43 -0800470// Append all configs that are enabled and supported.
471void create_configs(SkTArray<Config>* configs) {
472 SkCommandLineConfigArray array;
473 ParseConfigs(FLAGS_config, &array);
474 for (int i = 0; i < array.count(); ++i) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400475 create_config(array[i].get(), configs);
svaisanenc47635e2016-01-28 06:05:43 -0800476 }
477}
478
brianosman9f1f6e22016-09-15 08:33:02 -0700479// disable warning : switch statement contains default but no 'case' labels
480#if defined _WIN32
481#pragma warning ( push )
482#pragma warning ( disable : 4065 )
483#endif
484
halcanary96fcdcc2015-08-27 07:41:13 -0700485// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700486static Target* is_enabled(Benchmark* bench, const Config& config) {
487 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700488 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700489 }
490
reede5ea5002014-09-03 11:54:58 -0700491 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700492 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700493
halcanary96fcdcc2015-08-27 07:41:13 -0700494 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700495
tomhudsond968a6f2015-03-26 11:28:06 -0700496 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700497#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700498 case Benchmark::kGPU_Backend:
499 target = new GPUTarget(config);
500 break;
bsalomonc2553372014-07-22 13:09:05 -0700501#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700502 default:
503 target = new Target(config);
504 break;
505 }
bsalomonc2553372014-07-22 13:09:05 -0700506
tomhudsond968a6f2015-03-26 11:28:06 -0700507 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700508 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700509 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700510 }
511 return target;
512}
513
brianosman9f1f6e22016-09-15 08:33:02 -0700514#if defined _WIN32
515#pragma warning ( pop )
516#endif
517
reed42943c82016-09-12 12:01:44 -0700518static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700519 uint32_t minOutputSize, int* width, int* height) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400520 std::unique_ptr<SkBitmapRegionDecoder> brd(
msarettd1227a72016-05-18 06:23:57 -0700521 SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy));
msarett7f691442015-09-22 11:56:16 -0700522 if (nullptr == brd.get()) {
523 // This is indicates that subset decoding is not supported for a particular image format.
524 return false;
525 }
526
msarett7f691442015-09-22 11:56:16 -0700527 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
528 (uint32_t) brd->height()) {
529 // This indicates that the image is not large enough to decode a
530 // minOutputSize x minOutputSize subset at the given sampleSize.
531 return false;
532 }
533
534 // Set the image width and height. The calling code will use this to choose subsets to decode.
535 *width = brd->width();
536 *height = brd->height();
537 return true;
538}
539
egdaniel3bf92062015-06-26 08:12:46 -0700540static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700541 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700542#if SK_SUPPORT_GPU
543 if (FLAGS_abandonGpuContext) {
544 gGrFactory->abandonContexts();
545 }
546 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
547 gGrFactory->destroyContexts();
548 }
549#endif
550}
551
fmalita6519c212016-09-14 08:05:17 -0700552static void collect_files(const SkCommandLineFlags::StringArray& paths, const char* ext,
553 SkTArray<SkString>* list) {
554 for (int i = 0; i < paths.count(); ++i) {
555 if (SkStrEndsWith(paths[i], ext)) {
556 list->push_back(SkString(paths[i]));
557 } else {
558 SkOSFile::Iter it(paths[i], ext);
559 SkString path;
560 while (it.next(&path)) {
561 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
562 }
563 }
564 }
565}
566
mtkleine714e752014-07-31 12:13:48 -0700567class BenchmarkStream {
568public:
mtklein92007582014-08-01 07:46:52 -0700569 BenchmarkStream() : fBenches(BenchRegistry::Head())
570 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700571 , fCurrentRecording(0)
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400572 , fCurrentPiping(0)
mtklein92007582014-08-01 07:46:52 -0700573 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800574 , fCurrentSKP(0)
fmalita6519c212016-09-14 08:05:17 -0700575 , fCurrentSVG(0)
msarett95f192d2015-02-13 09:05:41 -0800576 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700577 , fCurrentCodec(0)
msarett84451022016-02-11 06:45:51 -0800578 , fCurrentAndroidCodec(0)
msarett7f691442015-09-22 11:56:16 -0700579 , fCurrentBRDImage(0)
msarett2cee9022016-06-03 08:25:21 -0700580 , fCurrentColorImage(0)
msarett95f192d2015-02-13 09:05:41 -0800581 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800582 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700583 , fCurrentSubsetType(0)
msarett84451022016-02-11 06:45:51 -0800584 , fCurrentSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700585 , fCurrentAnimSKP(0) {
fmalita6519c212016-09-14 08:05:17 -0700586 collect_files(FLAGS_skps, ".skp", &fSKPs);
587 collect_files(FLAGS_svgs, ".svg", &fSVGs);
mtkleine714e752014-07-31 12:13:48 -0700588
mtklein92007582014-08-01 07:46:52 -0700589 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
590 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
591 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
592 exit(1);
593 }
594
595 for (int i = 0; i < FLAGS_scales.count(); i++) {
596 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
597 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
598 exit(1);
599 }
600 }
robertphillips5b693772014-11-21 06:19:36 -0800601
cdalton63a82852015-06-29 14:06:10 -0700602 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
603 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700604 exit(1);
605 }
606
robertphillips5b693772014-11-21 06:19:36 -0800607 if (FLAGS_mpd) {
608 fUseMPDs.push_back() = true;
609 }
mtkleinc751ecb2015-06-15 08:56:38 -0700610 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700611
msarett95f192d2015-02-13 09:05:41 -0800612 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700613 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800614 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800615 }
msarett2cee9022016-06-03 08:25:21 -0700616 if (!CollectImages(FLAGS_colorImages, &fColorImages)) {
617 exit(1);
618 }
mtklein95553d92015-03-12 08:24:21 -0700619
msarett95f192d2015-02-13 09:05:41 -0800620 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700621 fColorTypes.push_back(kN32_SkColorType);
622 if (!FLAGS_simpleCodec) {
623 fColorTypes.push_back(kRGB_565_SkColorType);
624 fColorTypes.push_back(kAlpha_8_SkColorType);
625 fColorTypes.push_back(kIndex_8_SkColorType);
626 fColorTypes.push_back(kGray_8_SkColorType);
627 }
mtklein92007582014-08-01 07:46:52 -0700628 }
629
reedca2622b2016-03-18 07:25:55 -0700630 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700631 // Not strictly necessary, as it will be checked again later,
632 // but helps to avoid a lot of pointless work if we're going to skip it.
cdalton91e457d2016-02-17 11:10:16 -0800633 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700634 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700635 }
636
bungemanf93d7112016-09-16 06:24:20 -0700637 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
638 if (!stream) {
mtkleinfd731ce2014-09-10 12:19:30 -0700639 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700640 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700641 }
642
reedca2622b2016-03-18 07:25:55 -0700643 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700644 }
645
fmalita6519c212016-09-14 08:05:17 -0700646 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
647 SkFILEStream stream(path);
648 if (!stream.isValid()) {
649 SkDebugf("Could not read %s.\n", path);
650 return nullptr;
651 }
652
fmalitae1baa7c2016-09-14 12:04:30 -0700653 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
fmalita6519c212016-09-14 08:05:17 -0700654 if (!svgDom) {
655 SkDebugf("Could not parse %s.\n", path);
656 return nullptr;
657 }
658
fmalitae1baa7c2016-09-14 12:04:30 -0700659 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
660 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
661 if (svgDom->containerSize().isEmpty()) {
662 svgDom->setContainerSize(kDefaultContainerSize);
663 }
664
fmalita6519c212016-09-14 08:05:17 -0700665 SkPictureRecorder recorder;
fmalitae1baa7c2016-09-14 12:04:30 -0700666 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
667 svgDom->containerSize().height()));
fmalita6519c212016-09-14 08:05:17 -0700668 return recorder.finishRecordingAsPicture();
669 }
670
mtklein92007582014-08-01 07:46:52 -0700671 Benchmark* next() {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400672 std::unique_ptr<Benchmark> bench;
mtklein65dfd2f2016-02-03 10:40:54 -0800673 do {
674 bench.reset(this->rawNext());
675 if (!bench) {
676 return nullptr;
677 }
678 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
679 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700680 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800681 }
682
683 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700684 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700685 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700686 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700687 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700688 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700689 return bench;
690 }
mtklein92007582014-08-01 07:46:52 -0700691
mtkleine714e752014-07-31 12:13:48 -0700692 while (fGMs) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400693 std::unique_ptr<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700694 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800695 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700696 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700697 fBenchType = "micro";
mtklein18300a32016-03-16 13:53:35 -0700698 return new GMBench(gm.release());
mtkleine714e752014-07-31 12:13:48 -0700699 }
700 }
mtklein92007582014-08-01 07:46:52 -0700701
mtkleinfd731ce2014-09-10 12:19:30 -0700702 // First add all .skps as RecordingBenches.
703 while (fCurrentRecording < fSKPs.count()) {
704 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700705 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
706 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700707 continue;
708 }
709 SkString name = SkOSPath::Basename(path.c_str());
710 fSourceType = "skp";
711 fBenchType = "recording";
reedca2622b2016-03-18 07:25:55 -0700712 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic.get()));
mtklein051e56d2014-12-04 08:46:51 -0800713 fSKPOps = pic->approximateOpCount();
mtklein8c1a4f82016-08-08 06:56:22 -0700714 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh, FLAGS_lite);
mtkleinfd731ce2014-09-10 12:19:30 -0700715 }
716
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400717 // Add all .skps as PipeBenches.
718 while (fCurrentPiping < fSKPs.count()) {
719 const SkString& path = fSKPs[fCurrentPiping++];
720 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
721 if (!pic) {
722 continue;
723 }
724 SkString name = SkOSPath::Basename(path.c_str());
725 fSourceType = "skp";
726 fBenchType = "piping";
727 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic.get()));
728 fSKPOps = pic->approximateOpCount();
729 return new PipingBench(name.c_str(), pic.get());
730 }
731
mtkleinfd731ce2014-09-10 12:19:30 -0700732 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700733 while (fCurrentScale < fScales.count()) {
734 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800735 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700736 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
737 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800738 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700739 continue;
740 }
robertphillips5b693772014-11-21 06:19:36 -0800741
742 while (fCurrentUseMPD < fUseMPDs.count()) {
743 if (FLAGS_bbh) {
744 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
745 SkRTreeFactory factory;
746 SkPictureRecorder recorder;
robertphillips5b693772014-11-21 06:19:36 -0800747 pic->playback(recorder.beginRecording(pic->cullRect().width(),
748 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800749 &factory,
robertphillipsdda54452016-07-13 13:27:16 -0700750 0));
reedca2622b2016-03-18 07:25:55 -0700751 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800752 }
753 SkString name = SkOSPath::Basename(path.c_str());
754 fSourceType = "skp";
755 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700756 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
757 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700758 }
robertphillips5b693772014-11-21 06:19:36 -0800759 fCurrentUseMPD = 0;
760 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700761 }
fmalita6519c212016-09-14 08:05:17 -0700762
763 while (fCurrentSVG++ < fSVGs.count()) {
764 const char* path = fSVGs[fCurrentSVG - 1].c_str();
765 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
766 fSourceType = "svg";
767 fBenchType = "playback";
768 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
769 fScales[fCurrentScale], false, FLAGS_loopSKP);
770 }
771 }
772
mtklein92007582014-08-01 07:46:52 -0700773 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700774 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700775 fCurrentScale++;
776 }
777
joshualitt261c3ad2015-04-27 09:16:57 -0700778 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700779 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700780 while (fCurrentAnimSKP < fSKPs.count()) {
781 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700782 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
783 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700784 fCurrentAnimSKP++;
785 continue;
786 }
787
788 fCurrentAnimSKP++;
789 SkString name = SkOSPath::Basename(path.c_str());
Hal Canary2db83612016-11-04 13:02:54 -0400790 sk_sp<SKPAnimationBench::Animation> animation(
cdalton63a82852015-06-29 14:06:10 -0700791 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
Hal Canary2db83612016-11-04 13:02:54 -0400792 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation.get(),
halcanary385fe4d2015-08-26 13:07:48 -0700793 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700794 }
795 }
796
scroggo60869a42015-04-01 12:09:17 -0700797 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700798 fSourceType = "image";
799 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700800 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800801 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
802 continue;
803 }
bungeman38d909e2016-08-02 14:40:46 -0700804 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Ben Wagner145dbcd2016-11-03 14:40:50 -0400805 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700806 if (!codec) {
807 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700808 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700809 continue;
810 }
scroggo21027992015-04-02 13:22:38 -0700811
scroggo60869a42015-04-01 12:09:17 -0700812 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700813 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700814
msarettc7796b92016-01-07 14:20:20 -0800815 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700816 if (FLAGS_simpleCodec) {
817 if (kUnpremul_SkAlphaType == alphaType) {
818 alphaType = kPremul_SkAlphaType;
819 }
820
821 fCurrentColorType++;
822 } else {
823 switch (alphaType) {
824 case kOpaque_SkAlphaType:
825 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800826 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700827 break;
828 case kUnpremul_SkAlphaType:
829 case kPremul_SkAlphaType:
830 if (0 == fCurrentAlphaType) {
831 // Test unpremul first.
832 alphaType = kUnpremul_SkAlphaType;
833 fCurrentAlphaType++;
834 } else {
835 // Test premul.
836 alphaType = kPremul_SkAlphaType;
837 fCurrentAlphaType = 0;
838 fCurrentColorType++;
839 }
840 break;
841 default:
842 SkASSERT(false);
843 fCurrentColorType++;
844 break;
845 }
scroggo21027992015-04-02 13:22:38 -0700846 }
847
msarettc7796b92016-01-07 14:20:20 -0800848 // Make sure we can decode to this color type and alpha type.
849 SkImageInfo info =
850 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700851 const size_t rowBytes = info.minRowBytes();
852 SkAutoMalloc storage(info.getSafeSize(rowBytes));
853
854 // Used if fCurrentColorType is kIndex_8_SkColorType
855 int colorCount = 256;
856 SkPMColor colors[256];
857
scroggoeb602a52015-07-09 08:16:03 -0700858 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700859 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700860 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700861 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700862 case SkCodec::kSuccess:
863 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700864 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700865 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700866 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700867 // This is okay. Not all conversions are valid.
868 break;
scroggo60869a42015-04-01 12:09:17 -0700869 default:
870 // This represents some sort of failure.
871 SkASSERT(false);
872 break;
873 }
874 }
875 fCurrentColorType = 0;
876 }
877
msarett84451022016-02-11 06:45:51 -0800878 // Run AndroidCodecBenches
879 const int sampleSizes[] = { 2, 4, 8 };
880 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
881 fSourceType = "image";
882 fBenchType = "skandroidcodec";
883
884 const SkString& path = fImages[fCurrentAndroidCodec];
885 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
886 continue;
887 }
bungeman38d909e2016-08-02 14:40:46 -0700888 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Ben Wagner145dbcd2016-11-03 14:40:50 -0400889 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800890 if (!codec) {
891 // Nothing to time.
892 SkDebugf("Cannot find codec for %s\n", path.c_str());
893 continue;
894 }
895
896 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
897 int sampleSize = sampleSizes[fCurrentSampleSize];
898 fCurrentSampleSize++;
899 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
900 // Avoid benchmarking scaled decodes of already small images.
901 break;
902 }
903
bungeman38d909e2016-08-02 14:40:46 -0700904 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
905 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -0800906 }
907 fCurrentSampleSize = 0;
908 }
909
msarett7f691442015-09-22 11:56:16 -0700910 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700911 // We intend to create benchmarks that model the use cases in
912 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
913 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
914 // the image. For that reason, we will benchmark decodes in five representative locations
915 // in the image. Additionally, this use case utilizes power of two scaling, so we will
916 // test on power of two sample sizes. The output tile is always 512x512, so, when a
917 // sampleSize is used, the size of the subset that is decoded is always
918 // (sampleSize*512)x(sampleSize*512).
919 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700920 // All use cases we are aware of only scale by powers of two.
921 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
922 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800923 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700924 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800925 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700926 fSourceType = "image";
927 fBenchType = "BRD";
928
mtklein6f0ff912016-01-11 09:04:21 -0800929 const SkString& path = fImages[fCurrentBRDImage];
930 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
931 continue;
932 }
scroggo860e8a62015-10-15 07:51:28 -0700933
msarettd1227a72016-05-18 06:23:57 -0700934 while (fCurrentColorType < fColorTypes.count()) {
935 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
936 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700937
bungeman38d909e2016-08-02 14:40:46 -0700938 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -0700939 const SkColorType colorType = fColorTypes[fCurrentColorType];
940 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
941 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -0700942
msarettd1227a72016-05-18 06:23:57 -0700943 int width = 0;
944 int height = 0;
reed42943c82016-09-12 12:01:44 -0700945 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -0700946 &width, &height)) {
947 break;
msarett7f691442015-09-22 11:56:16 -0700948 }
msarettd1227a72016-05-18 06:23:57 -0700949
950 SkString basename = SkOSPath::Basename(path.c_str());
951 SkIRect subset;
952 const uint32_t subsetSize = sampleSize * minOutputSize;
953 switch (currentSubsetType) {
954 case kTopLeft_SubsetType:
955 basename.append("_TopLeft");
956 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
957 break;
958 case kTopRight_SubsetType:
959 basename.append("_TopRight");
960 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
961 subsetSize);
962 break;
963 case kMiddle_SubsetType:
964 basename.append("_Middle");
965 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
966 (height - subsetSize) / 2, subsetSize, subsetSize);
967 break;
968 case kBottomLeft_SubsetType:
969 basename.append("_BottomLeft");
970 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
971 subsetSize);
972 break;
973 case kBottomRight_SubsetType:
974 basename.append("_BottomRight");
975 subset = SkIRect::MakeXYWH(width - subsetSize,
976 height - subsetSize, subsetSize, subsetSize);
977 break;
978 default:
979 SkASSERT(false);
980 }
981
982 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
983 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -0700984 }
msarettd1227a72016-05-18 06:23:57 -0700985 fCurrentSubsetType = 0;
986 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -0700987 }
msarettd1227a72016-05-18 06:23:57 -0700988 fCurrentSampleSize = 0;
989 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -0700990 }
msarettd1227a72016-05-18 06:23:57 -0700991 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -0700992 }
993
msarett2cee9022016-06-03 08:25:21 -0700994 while (fCurrentColorImage < fColorImages.count()) {
995 fSourceType = "colorimage";
996 fBenchType = "skcolorcodec";
997 const SkString& path = fColorImages[fCurrentColorImage];
998 fCurrentColorImage++;
999 sk_sp<SkData> encoded = SkData::MakeFromFileName(path.c_str());
1000 if (encoded) {
1001 return new ColorCodecBench(SkOSPath::Basename(path.c_str()).c_str(),
1002 std::move(encoded));
1003 } else {
1004 SkDebugf("Could not read file %s.\n", path.c_str());
1005 }
1006 }
1007
halcanary96fcdcc2015-08-27 07:41:13 -07001008 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001009 }
mtklein92007582014-08-01 07:46:52 -07001010
1011 void fillCurrentOptions(ResultsWriter* log) const {
1012 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001013 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001014 if (0 == strcmp(fSourceType, "skp")) {
1015 log->configOption("clip",
1016 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1017 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001018 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -07001019 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001020 if (fCurrentUseMPD > 0) {
1021 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1022 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1023 }
mtklein92007582014-08-01 07:46:52 -07001024 }
mtklein051e56d2014-12-04 08:46:51 -08001025 if (0 == strcmp(fBenchType, "recording")) {
1026 log->metric("bytes", fSKPBytes);
1027 log->metric("ops", fSKPOps);
1028 }
mtklein92007582014-08-01 07:46:52 -07001029 }
1030
mtkleine714e752014-07-31 12:13:48 -07001031private:
msarettb23e6aa2015-06-09 13:56:10 -07001032 enum SubsetType {
1033 kTopLeft_SubsetType = 0,
1034 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001035 kMiddle_SubsetType = 2,
1036 kBottomLeft_SubsetType = 3,
1037 kBottomRight_SubsetType = 4,
1038 kTranslate_SubsetType = 5,
1039 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001040 kLast_SubsetType = kZoom_SubsetType,
1041 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001042 };
1043
mtkleine714e752014-07-31 12:13:48 -07001044 const BenchRegistry* fBenches;
1045 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001046 SkIRect fClip;
1047 SkTArray<SkScalar> fScales;
1048 SkTArray<SkString> fSKPs;
fmalita6519c212016-09-14 08:05:17 -07001049 SkTArray<SkString> fSVGs;
robertphillips5b693772014-11-21 06:19:36 -08001050 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001051 SkTArray<SkString> fImages;
msarett2cee9022016-06-03 08:25:21 -07001052 SkTArray<SkString> fColorImages;
msarett74deb982015-10-20 16:45:56 -07001053 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001054 SkScalar fZoomMax;
1055 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001056
mtklein051e56d2014-12-04 08:46:51 -08001057 double fSKPBytes, fSKPOps;
1058
mtkleinfd731ce2014-09-10 12:19:30 -07001059 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1060 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1061 int fCurrentRecording;
Mike Reed9cdd2ab2016-10-21 10:43:36 -04001062 int fCurrentPiping;
mtklein92007582014-08-01 07:46:52 -07001063 int fCurrentScale;
1064 int fCurrentSKP;
fmalita6519c212016-09-14 08:05:17 -07001065 int fCurrentSVG;
robertphillips5b693772014-11-21 06:19:36 -08001066 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001067 int fCurrentCodec;
msarett84451022016-02-11 06:45:51 -08001068 int fCurrentAndroidCodec;
msarett7f691442015-09-22 11:56:16 -07001069 int fCurrentBRDImage;
msarett2cee9022016-06-03 08:25:21 -07001070 int fCurrentColorImage;
msarett95f192d2015-02-13 09:05:41 -08001071 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -08001072 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -07001073 int fCurrentSubsetType;
msarett84451022016-02-11 06:45:51 -08001074 int fCurrentSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001075 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001076};
1077
msarettc149f0e2016-01-04 11:35:43 -08001078// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1079// This prints something every once in a while so that it knows we're still working.
1080static void start_keepalive() {
1081 struct Loop {
1082 static void forever(void*) {
1083 for (;;) {
1084 static const int kSec = 1200;
1085 #if defined(SK_BUILD_FOR_WIN)
1086 Sleep(kSec * 1000);
1087 #else
1088 sleep(kSec);
1089 #endif
1090 SkDebugf("\nBenchmarks still running...\n");
1091 }
1092 }
1093 };
1094 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1095 intentionallyLeaked->start();
1096}
1097
jcgregorio3b27ade2014-11-13 08:06:40 -08001098int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001099int nanobench_main() {
Mike Kleinadacaef2017-02-06 09:26:14 -05001100#if defined(SK_BUILD_FOR_IOS)
1101 cd_Documents();
1102#endif
jcgregorio3b27ade2014-11-13 08:06:40 -08001103 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001104 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001105 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001106
krajcevski69a55602014-08-13 10:46:31 -07001107#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001108 GrContextOptions grContextOpts;
halcanary385fe4d2015-08-26 13:07:48 -07001109 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001110#endif
1111
bsalomon06cddec2014-10-24 10:40:50 -07001112 if (FLAGS_veryVerbose) {
1113 FLAGS_verbose = true;
1114 }
1115
bsalomon6eb03cc2014-08-07 14:28:50 -07001116 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001117 FLAGS_samples = 1;
1118 FLAGS_gpuFrameLag = 0;
1119 }
1120
bsalomon6eb03cc2014-08-07 14:28:50 -07001121 if (!FLAGS_writePath.isEmpty()) {
1122 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1123 if (!sk_mkdir(FLAGS_writePath[0])) {
1124 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001125 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001126 }
1127 }
1128
Ben Wagner145dbcd2016-11-03 14:40:50 -04001129 std::unique_ptr<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001130 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001131#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001132 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001133#else
1134 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1135 return 1;
1136#endif
mtklein60317d0f2014-07-14 11:30:37 -07001137 }
mtklein1915b622014-08-20 11:45:00 -07001138
1139 if (1 == FLAGS_properties.count() % 2) {
1140 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1141 return 1;
1142 }
1143 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1144 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1145 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001146
1147 if (1 == FLAGS_key.count() % 2) {
1148 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1149 return 1;
1150 }
1151 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001152 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001153 }
mtklein60317d0f2014-07-14 11:30:37 -07001154
mtkleinf3723212014-06-25 14:08:00 -07001155 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001156 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001157
cdaltone1b89582015-06-25 19:17:08 -07001158 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001159
bsalomon6eb03cc2014-08-07 14:28:50 -07001160 if (kAutoTuneLoops != FLAGS_loops) {
1161 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001162 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001163 SkDebugf("! -> high variance, ? -> moderate variance\n");
1164 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001165 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001166 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001167 } else {
mtkleind75c4662015-04-30 07:11:22 -07001168 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001169 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001170 }
1171
svaisanenc47635e2016-01-28 06:05:43 -08001172 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001173 create_configs(&configs);
1174
joshualitt3ebd0502016-02-09 07:18:08 -08001175#ifdef THERMAL_MANAGER_SUPPORTED
1176 int tmEnabled, tmThreshold, tmSleepTimeMs, tmTimeoutMs;
1177 if (4 != sscanf(FLAGS_useThermalManager[0], "%d,%d,%d,%d",
1178 &tmEnabled, &tmThreshold, &tmSleepTimeMs, &tmTimeoutMs)) {
1179 SkDebugf("Can't parse %s from --useThermalManager.\n", FLAGS_useThermalManager[0]);
1180 exit(1);
1181 }
1182 ThermalManager tm(tmThreshold, tmSleepTimeMs, tmTimeoutMs);
1183#endif
1184
msarettc149f0e2016-01-04 11:35:43 -08001185 if (FLAGS_keepAlive) {
1186 start_keepalive();
1187 }
1188
Yuqian Liba62b4a2016-12-14 13:46:53 -05001189 gSkUseAnalyticAA = FLAGS_analyticAA;
liyuqian38911a72016-10-04 11:23:22 -07001190
Yuqian Li550148b2017-01-13 10:13:13 -05001191 if (FLAGS_forceAnalyticAA) {
1192 gSkForceAnalyticAA = true;
1193 }
1194
mtkleine070c2b2014-10-14 08:40:43 -07001195 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001196 BenchmarkStream benchStream;
1197 while (Benchmark* b = benchStream.next()) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001198 std::unique_ptr<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001199 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001200 continue;
1201 }
1202
svaisanenc47635e2016-01-28 06:05:43 -08001203 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001204 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001205 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001206 }
egdaniel3bf92062015-06-26 08:12:46 -07001207 for (int i = 0; i < configs.count(); ++i) {
joshualitt3ebd0502016-02-09 07:18:08 -08001208#ifdef THERMAL_MANAGER_SUPPORTED
1209 if (tmEnabled && !tm.coolOffIfNecessary()) {
1210 SkDebugf("Could not cool off, timings will be throttled\n");
1211 }
1212#endif
egdaniel3bf92062015-06-26 08:12:46 -07001213 Target* target = is_enabled(b, configs[i]);
1214 if (!target) {
1215 continue;
1216 }
mtkleinf3723212014-06-25 14:08:00 -07001217
halcanary96fcdcc2015-08-27 07:41:13 -07001218 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001219 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001220 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001221
brianosman7a5ada82016-02-08 13:49:12 -08001222 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001223 SkDebugf("Running %s\t%s\n"
1224 , bench->getUniqueName()
1225 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001226 if (FLAGS_dryRun) {
1227 continue;
1228 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001229 }
1230
egdaniel3bf92062015-06-26 08:12:46 -07001231 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001232 bench->perCanvasPreDraw(canvas);
1233
cdaltone1b89582015-06-25 19:17:08 -07001234 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001235 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001236 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1237 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001238
mtkleinbbba1682015-10-28 11:36:30 -07001239 if (FLAGS_ms) {
1240 samples.reset();
1241 auto stop = now_ms() + FLAGS_ms;
1242 do {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001243 samples.push_back(time(loops, bench.get(), target) / loops);
mtkleinbbba1682015-10-28 11:36:30 -07001244 } while (now_ms() < stop);
1245 } else {
cdaltone1b89582015-06-25 19:17:08 -07001246 samples.reset(FLAGS_samples);
1247 for (int s = 0; s < FLAGS_samples; s++) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001248 samples[s] = time(loops, bench.get(), target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001249 }
cdaltone1b89582015-06-25 19:17:08 -07001250 }
mtkleinf3723212014-06-25 14:08:00 -07001251
joshualitte45c81c2015-12-02 09:05:37 -08001252#if SK_SUPPORT_GPU
1253 SkTArray<SkString> keys;
1254 SkTArray<double> values;
1255 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1256 if (gpuStatsDump) {
1257 // TODO cache stats
1258 bench->getGpuStats(canvas, &keys, &values);
1259 }
1260#endif
1261
robertphillips5b693772014-11-21 06:19:36 -08001262 bench->perCanvasPostDraw(canvas);
1263
egdaniel3bf92062015-06-26 08:12:46 -07001264 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001265 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001266 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001267 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001268 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001269 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001270 }
1271
1272 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001273 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001274 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001275 continue;
1276 }
1277
cdaltone1b89582015-06-25 19:17:08 -07001278 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001279 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001280 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001281 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001282 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001283 log->metric("min_ms", stats.min);
George Burgess IV75b57182016-12-06 10:53:52 -08001284 log->metrics("samples", samples);
joshualitte45c81c2015-12-02 09:05:37 -08001285#if SK_SUPPORT_GPU
1286 if (gpuStatsDump) {
1287 // dump to json, only SKPBench currently returns valid keys / values
1288 SkASSERT(keys.count() == values.count());
1289 for (int i = 0; i < keys.count(); i++) {
1290 log->metric(keys[i].c_str(), values[i]);
1291 }
1292 }
1293#endif
1294
mtkleine070c2b2014-10-14 08:40:43 -07001295 if (runs++ % FLAGS_flushEvery == 0) {
1296 log->flush();
1297 }
mtklein60317d0f2014-07-14 11:30:37 -07001298
bsalomon6eb03cc2014-08-07 14:28:50 -07001299 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001300 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001301 config = ""; // Only print the config if we run the same bench on more than one.
1302 }
mtkleind75c4662015-04-30 07:11:22 -07001303 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1304 , sk_tools::getCurrResidentSetSizeMB()
1305 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001306 , bench->getUniqueName()
1307 , config);
mtkleinf3723212014-06-25 14:08:00 -07001308 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001309 const char* mark = " ";
1310 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1311 if (stddev_percent > 5) mark = "?";
1312 if (stddev_percent > 10) mark = "!";
1313
1314 SkDebugf("%10.2f %s\t%s\t%s\n",
1315 stats.median*1e3, mark, bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001316 } else {
1317 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001318 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1319 , sk_tools::getCurrResidentSetSizeMB()
1320 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001321 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001322 , HUMANIZE(stats.min)
1323 , HUMANIZE(stats.median)
1324 , HUMANIZE(stats.mean)
1325 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001326 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001327 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001328 , config
mtklein96289052014-09-10 12:05:59 -07001329 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001330 );
1331 }
joshualitte45c81c2015-12-02 09:05:37 -08001332
bsalomonb12ea412015-02-02 21:19:50 -08001333#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001334 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001335 GrContext* context = gGrFactory->get(configs[i].ctxType,
kkinnunen3e980c32015-12-23 01:33:00 -08001336 configs[i].ctxOptions);
kkinnunen5219fd92015-12-10 06:28:13 -08001337 context->printCacheStats();
1338 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001339 }
1340#endif
joshualitte45c81c2015-12-02 09:05:37 -08001341
cdalton2c56ba52015-06-26 13:32:53 -07001342 if (FLAGS_verbose) {
1343 SkDebugf("Samples: ");
1344 for (int i = 0; i < samples.count(); i++) {
1345 SkDebugf("%s ", HUMANIZE(samples[i]));
1346 }
1347 SkDebugf("%s\n", bench->getUniqueName());
1348 }
egdaniel3bf92062015-06-26 08:12:46 -07001349 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001350 }
mtkleinf3723212014-06-25 14:08:00 -07001351 }
1352
Herb Derby5a523fe2017-01-26 16:48:28 -05001353 SkGraphics::PurgeAllCaches();
1354
mtkleine1091452014-12-04 10:47:02 -08001355 log->bench("memory_usage", 0,0);
1356 log->config("meta");
1357 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1358
joshualitte0b19d42015-03-26 10:41:02 -07001359#if SK_SUPPORT_GPU
1360 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1361 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001362 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001363#endif
1364
mtkleinf3723212014-06-25 14:08:00 -07001365 return 0;
1366}
1367
Mike Klein98736062017-01-31 13:26:15 -05001368#if !defined(SK_BUILD_FOR_IOS) || defined(NANOBENCH_DEFINE_MAIN)
jcgregorio3b27ade2014-11-13 08:06:40 -08001369int main(int argc, char** argv) {
1370 SkCommandLineFlags::Parse(argc, argv);
1371 return nanobench_main();
1372}
1373#endif