blob: 4a058be27ebee36957b7b563bd3d053de85b13c5 [file] [log] [blame]
mtkleinf3723212014-06-25 14:08:00 -07001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
mtkleinbb6a0282014-07-01 08:43:42 -07008#include <ctype.h>
9
tomhudsond968a6f2015-03-26 11:28:06 -070010#include "nanobench.h"
11
msarett84451022016-02-11 06:45:51 -080012#include "AndroidCodecBench.h"
mtkleinf3723212014-06-25 14:08:00 -070013#include "Benchmark.h"
msarett7f691442015-09-22 11:56:16 -070014#include "BitmapRegionDecoderBench.h"
scroggo60869a42015-04-01 12:09:17 -070015#include "CodecBench.h"
msarett7f691442015-09-22 11:56:16 -070016#include "CodecBenchPriv.h"
msarett2cee9022016-06-03 08:25:21 -070017#include "ColorCodecBench.h"
mtkleinf3723212014-06-25 14:08:00 -070018#include "CrashHandler.h"
mtkleine714e752014-07-31 12:13:48 -070019#include "GMBench.h"
mtkleinafb43792014-08-19 15:55:55 -070020#include "ProcStats.h"
mtklein60317d0f2014-07-14 11:30:37 -070021#include "ResultsWriter.h"
mtkleinfd731ce2014-09-10 12:19:30 -070022#include "RecordingBench.h"
joshualitt261c3ad2015-04-27 09:16:57 -070023#include "SKPAnimationBench.h"
mtklein92007582014-08-01 07:46:52 -070024#include "SKPBench.h"
mtkleinf3723212014-06-25 14:08:00 -070025#include "Stats.h"
mtkleinf3723212014-06-25 14:08:00 -070026
msarett84451022016-02-11 06:45:51 -080027#include "SkAndroidCodec.h"
Hal Canary95e3c052017-01-11 12:44:43 -050028#include "SkAutoMalloc.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000029#include "SkBBoxHierarchy.h"
Hal Canary95e3c052017-01-11 12:44:43 -050030#include "SkBitmapRegionDecoder.h"
mtkleinf3723212014-06-25 14:08:00 -070031#include "SkCanvas.h"
scroggo60869a42015-04-01 12:09:17 -070032#include "SkCodec.h"
caryclark17f0b6d2014-07-22 10:15:34 -070033#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080034#include "SkCommonFlagsConfig.h"
msarett95f192d2015-02-13 09:05:41 -080035#include "SkData.h"
mtkleinf3723212014-06-25 14:08:00 -070036#include "SkGraphics.h"
halcanary4dbbd042016-06-07 17:21:10 -070037#include "SkLeanWindows.h"
mtklein20840502014-08-21 15:51:22 -070038#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050039#include "SkOSPath.h"
mtklein20840502014-08-21 15:51:22 -070040#include "SkPictureRecorder.h"
mtklein051e56d2014-12-04 08:46:51 -080041#include "SkPictureUtils.h"
Hal Canary95e3c052017-01-11 12:44:43 -050042#include "SkSVGDOM.h"
43#include "SkScan.h"
mtkleinf3723212014-06-25 14:08:00 -070044#include "SkString.h"
45#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080046#include "SkTaskGroup.h"
msarettc149f0e2016-01-04 11:35:43 -080047#include "SkThreadUtils.h"
joshualitt3ebd0502016-02-09 07:18:08 -080048#include "ThermalManager.h"
mtkleinf3723212014-06-25 14:08:00 -070049
bungeman60e0fee2015-08-26 05:15:46 -070050#include <stdlib.h>
51
scroggo38ce0a72016-01-07 07:28:47 -080052#ifndef SK_BUILD_FOR_WIN32
53 #include <unistd.h>
54#endif
55
tomhudsond968a6f2015-03-26 11:28:06 -070056#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
57 #include "nanobenchAndroid.h"
58#endif
59
mtkleinbb6a0282014-07-01 08:43:42 -070060#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070061 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070062 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070063 #include "GrContextFactory.h"
bsalomon3724e572016-03-30 18:56:19 -070064 #include "gl/GrGLUtil.h"
65 using sk_gpu_test::GrContextFactory;
bsalomonc8699322016-05-11 11:55:36 -070066 using sk_gpu_test::TestContext;
Ben Wagner145dbcd2016-11-03 14:40:50 -040067 std::unique_ptr<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070068#endif
69
bsalomon682c2692015-05-22 14:01:46 -070070 struct GrContextOptions;
71
reed53249782014-10-10 09:09:52 -070072static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070073
Mike Kleind8ee67c2017-01-19 12:14:50 -050074#if !defined(__has_feature)
75 #define __has_feature(x) 0
76#endif
77
mtkleinb5110422014-08-07 15:20:02 -070078static const int kDefaultLoops =
Mike Kleind8ee67c2017-01-19 12:14:50 -050079#if defined(SK_DEBUG) || __has_feature(address_sanitizer)
bsalomon6eb03cc2014-08-07 14:28:50 -070080 1;
mtkleina189ccd2014-07-14 12:28:47 -070081#else
bsalomon6eb03cc2014-08-07 14:28:50 -070082 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070083#endif
84
bsalomon6eb03cc2014-08-07 14:28:50 -070085static SkString loops_help_txt() {
86 SkString help;
87 help.printf("Number of times to run each bench. Set this to %d to auto-"
88 "tune for each bench. Timings are only reported when auto-tuning.",
89 kAutoTuneLoops);
90 return help;
91}
92
cdaltone1b89582015-06-25 19:17:08 -070093static SkString to_string(int n) {
94 SkString str;
95 str.appendS32(n);
96 return str;
97}
98
bsalomon6eb03cc2014-08-07 14:28:50 -070099DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
100
mtkleinf3723212014-06-25 14:08:00 -0700101DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -0700102DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -0700103DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
104DEFINE_double(overheadGoal, 0.0001,
105 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -0700106DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -0700107DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -0700108
mtklein60317d0f2014-07-14 11:30:37 -0700109DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -0700110DEFINE_int32(maxCalibrationAttempts, 3,
111 "Try up to this many times to guess loops for a bench, or skip the bench.");
112DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700113DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
114DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700115DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
116 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700117DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
mtklein8c1a4f82016-08-08 06:56:22 -0700118DEFINE_bool(lite, false, "Use SkLiteRecorder in recording benchmarks?");
robertphillips5b693772014-11-21 06:19:36 -0800119DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700120DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700121DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800122DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800123DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800124DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
msarettc149f0e2016-01-04 11:35:43 -0800125DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
joshualitt3ebd0502016-02-09 07:18:08 -0800126DEFINE_string(useThermalManager, "0,1,10,1000", "enabled,threshold,sleepTimeMs,TimeoutMs for "
127 "thermalManager\n");
mtklein92007582014-08-01 07:46:52 -0700128
mtklein65dfd2f2016-02-03 10:40:54 -0800129DEFINE_string(sourceType, "",
130 "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
131DEFINE_string(benchType, "",
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400132 "Apply usual --match rules to bench type: micro, recording, piping, playback, skcodec, etc.");
mtklein65dfd2f2016-02-03 10:40:54 -0800133
mtkleinbbba1682015-10-28 11:36:30 -0700134static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
135
mtkleinf3723212014-06-25 14:08:00 -0700136static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700137 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800138 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700139}
mtklein55b0ffc2014-07-17 08:38:23 -0700140#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700141
tomhudsond968a6f2015-03-26 11:28:06 -0700142bool Target::init(SkImageInfo info, Benchmark* bench) {
143 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700144 this->surface = SkSurface::MakeRaster(info);
145 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700146 return false;
147 }
148 }
149 return true;
150}
151bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700152 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700153 if (!canvas) {
154 return false;
155 }
156 bmp->setInfo(canvas->imageInfo());
157 if (!canvas->readPixels(bmp, 0, 0)) {
158 SkDebugf("Can't read canvas pixels.\n");
159 return false;
160 }
161 return true;
162}
163
164#if SK_SUPPORT_GPU
165struct GPUTarget : public Target {
bsalomonc8699322016-05-11 11:55:36 -0700166 explicit GPUTarget(const Config& c) : Target(c), context(nullptr) { }
167 TestContext* context;
tomhudsond968a6f2015-03-26 11:28:06 -0700168
169 void setup() override {
bsalomonc8699322016-05-11 11:55:36 -0700170 this->context->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700171 // Make sure we're done with whatever came before.
bsalomonc8699322016-05-11 11:55:36 -0700172 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700173 }
174 void endTiming() override {
bsalomonc8699322016-05-11 11:55:36 -0700175 if (this->context) {
176 this->context->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700177 }
178 }
179 void fence() override {
bsalomonc8699322016-05-11 11:55:36 -0700180 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700181 }
mtkleind75c4662015-04-30 07:11:22 -0700182
cdaltond416a5b2015-06-23 13:23:44 -0700183 bool needsFrameTiming(int* maxFrameLag) const override {
bsalomonc8699322016-05-11 11:55:36 -0700184 if (!this->context->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700185 // Frame lag is unknown.
186 *maxFrameLag = FLAGS_gpuFrameLag;
187 }
188 return true;
189 }
tomhudsond968a6f2015-03-26 11:28:06 -0700190 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700191 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
192 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700193 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700194 this->surface = SkSurface::MakeRenderTarget(gGrFactory->get(this->config.ctxType,
195 this->config.ctxOptions),
bsalomon5ec26ae2016-02-25 08:33:02 -0800196 SkBudgeted::kNo, info,
reede8f30622016-03-23 18:59:25 -0700197 this->config.samples, &props);
bsalomonc8699322016-05-11 11:55:36 -0700198 this->context = gGrFactory->getContextInfo(this->config.ctxType,
199 this->config.ctxOptions).testContext();
tomhudsond968a6f2015-03-26 11:28:06 -0700200 if (!this->surface.get()) {
201 return false;
202 }
bsalomonc8699322016-05-11 11:55:36 -0700203 if (!this->context->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700204 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800205 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700206 }
tomhudsond968a6f2015-03-26 11:28:06 -0700207 return true;
208 }
209 void fillOptions(ResultsWriter* log) override {
210 const GrGLubyte* version;
bsalomonc8699322016-05-11 11:55:36 -0700211 if (this->context->backend() == kOpenGL_GrBackend) {
212 const GrGLInterface* gl =
213 reinterpret_cast<const GrGLInterface*>(this->context->backendContext());
214 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
215 log->configOption("GL_VERSION", (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_RENDERER));
218 log->configOption("GL_RENDERER", (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_VENDOR));
221 log->configOption("GL_VENDOR", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700222
bsalomonc8699322016-05-11 11:55:36 -0700223 GR_GL_CALL_RET(gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
224 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
225 }
tomhudsond968a6f2015-03-26 11:28:06 -0700226 }
227};
mtkleind75c4662015-04-30 07:11:22 -0700228
tomhudsond968a6f2015-03-26 11:28:06 -0700229#endif
230
tomhudson75a0ebb2015-03-27 12:11:44 -0700231static double time(int loops, Benchmark* bench, Target* target) {
232 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700233 if (canvas) {
234 canvas->clear(SK_ColorWHITE);
235 }
joshualitt8a6697a2015-09-30 12:11:07 -0700236 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700237 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700238 canvas = target->beginTiming(canvas);
239 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700240 if (canvas) {
241 canvas->flush();
242 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700243 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700244 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700245 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700246 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700247}
248
mtkleinf3723212014-06-25 14:08:00 -0700249static double estimate_timer_overhead() {
250 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700251 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700252 double start = now_ms();
253 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700254 }
255 return overhead / FLAGS_overheadLoops;
256}
257
reed53249782014-10-10 09:09:52 -0700258static int detect_forever_loops(int loops) {
259 // look for a magic run-forever value
260 if (loops < 0) {
261 loops = SK_MaxS32;
262 }
263 return loops;
264}
265
mtklein55b0ffc2014-07-17 08:38:23 -0700266static int clamp_loops(int loops) {
267 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800268 SkDebugf("ERROR: clamping loops from %d to 1. "
269 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700270 return 1;
271 }
272 if (loops > FLAGS_maxLoops) {
273 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
274 return FLAGS_maxLoops;
275 }
276 return loops;
277}
278
tomhudsond968a6f2015-03-26 11:28:06 -0700279static bool write_canvas_png(Target* target, const SkString& filename) {
280
bsalomon6eb03cc2014-08-07 14:28:50 -0700281 if (filename.isEmpty()) {
282 return false;
283 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700284 if (target->getCanvas() &&
285 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700286 return false;
287 }
tomhudsond968a6f2015-03-26 11:28:06 -0700288
bsalomon6eb03cc2014-08-07 14:28:50 -0700289 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700290
291 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700292 return false;
293 }
tomhudsond968a6f2015-03-26 11:28:06 -0700294
bsalomon6eb03cc2014-08-07 14:28:50 -0700295 SkString dir = SkOSPath::Dirname(filename.c_str());
296 if (!sk_mkdir(dir.c_str())) {
297 SkDebugf("Can't make dir %s.\n", dir.c_str());
298 return false;
299 }
300 SkFILEWStream stream(filename.c_str());
301 if (!stream.isValid()) {
302 SkDebugf("Can't write %s.\n", filename.c_str());
303 return false;
304 }
Hal Canarydb683012016-11-23 08:55:18 -0700305 if (!SkEncodeImage(&stream, bmp, SkEncodedImageFormat::kPNG, 100)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700306 SkDebugf("Can't encode a PNG.\n");
307 return false;
308 }
309 return true;
310}
311
312static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700313static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700314 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700315 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700316 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700317 int loops = bench->calculateLoops(FLAGS_loops);
318 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700319 while (bench_plus_overhead < overhead) {
320 if (round++ == FLAGS_maxCalibrationAttempts) {
321 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700322 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700323 return kFailedLoops;
324 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700325 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700326 }
mtklein2069e222014-08-04 13:57:39 -0700327 }
mtkleinf3723212014-06-25 14:08:00 -0700328
mtkleinbb6a0282014-07-01 08:43:42 -0700329 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700330 // We'll pick N to make timer overhead negligible:
331 //
mtkleinbb6a0282014-07-01 08:43:42 -0700332 // overhead
333 // ------------------------- < FLAGS_overheadGoal
334 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700335 //
Hal Canary55325b72017-01-03 10:36:17 -0500336 // where bench_plus_overhead ~=~ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700337 //
338 // Doing some math, we get:
339 //
mtkleinbb6a0282014-07-01 08:43:42 -0700340 // (overhead / FLAGS_overheadGoal) - overhead
341 // ------------------------------------------ < N
342 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700343 //
344 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700345 if (kAutoTuneLoops == loops) {
346 const double numer = overhead / FLAGS_overheadGoal - overhead;
347 const double denom = bench_plus_overhead - overhead;
348 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700349 loops = clamp_loops(loops);
350 } else {
351 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700352 }
mtkleinbb6a0282014-07-01 08:43:42 -0700353
mtkleinbb6a0282014-07-01 08:43:42 -0700354 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700355}
356
cdaltone1b89582015-06-25 19:17:08 -0700357static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700358 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700359 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700360 if (kAutoTuneLoops == loops) {
361 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700362 double elapsed = 0;
363 do {
mtklein527930f2014-11-06 08:04:34 -0800364 if (1<<30 == loops) {
365 // We're about to wrap. Something's wrong with the bench.
366 loops = 0;
367 break;
368 }
mtkleina189ccd2014-07-14 12:28:47 -0700369 loops *= 2;
370 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700371 // _this_ round, not still timing last round.
372 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700373 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700374 }
375 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700376
mtkleina189ccd2014-07-14 12:28:47 -0700377 // We've overshot at least a little. Scale back linearly.
378 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700379 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700380
tomhudsond968a6f2015-03-26 11:28:06 -0700381 // Make sure we're not still timing our calibration.
382 target->fence();
reed53249782014-10-10 09:09:52 -0700383 } else {
384 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700385 }
mtkleinbb6a0282014-07-01 08:43:42 -0700386
387 // Pretty much the same deal as the calibration: do some warmup to make
388 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700389 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700390 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700391 }
mtkleinbb6a0282014-07-01 08:43:42 -0700392
mtkleinbb6a0282014-07-01 08:43:42 -0700393 return loops;
394}
mtkleinbb6a0282014-07-01 08:43:42 -0700395
bsalomonc2553372014-07-22 13:09:05 -0700396#if SK_SUPPORT_GPU
bsalomon85b4b532016-04-05 11:06:27 -0700397#define kBogusContextType GrContextFactory::kNativeGL_ContextType
csmartdalton6270e552016-09-13 10:41:49 -0700398#define kBogusContextOptions GrContextFactory::ContextOptions::kNone
bsalomonc2553372014-07-22 13:09:05 -0700399#else
bsalomon85b4b532016-04-05 11:06:27 -0700400#define kBogusContextType 0
401#define kBogusContextOptions 0
mtkleine714e752014-07-31 12:13:48 -0700402#endif
bsalomonc2553372014-07-22 13:09:05 -0700403
svaisanenc47635e2016-01-28 06:05:43 -0800404static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
405
406#if SK_SUPPORT_GPU
407 if (const auto* gpuConfig = config->asConfigGpu()) {
408 if (!FLAGS_gpu)
409 return;
410
svaisanenc47635e2016-01-28 06:05:43 -0800411 const auto ctxType = gpuConfig->getContextType();
csmartdalton6270e552016-09-13 10:41:49 -0700412 const auto ctxOptions = gpuConfig->getContextOptions();
svaisanenc47635e2016-01-28 06:05:43 -0800413 const auto sampleCount = gpuConfig->getSamples();
414
415 if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOptions)) {
416 const auto maxSampleCount = ctx->caps()->maxSampleCount();
417 if (sampleCount > ctx->caps()->maxSampleCount()) {
418 SkDebugf("Configuration sample count %d exceeds maximum %d.\n",
419 sampleCount, maxSampleCount);
420 return;
421 }
422 } else {
423 SkDebugf("No context was available matching config type and options.\n");
424 return;
425 }
426
427 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800428 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800429 Benchmark::kGPU_Backend,
brianosman1a48e132016-06-20 09:41:41 -0700430 gpuConfig->getColorType(),
svaisanenc47635e2016-01-28 06:05:43 -0800431 kPremul_SkAlphaType,
brianosman1a48e132016-06-20 09:41:41 -0700432 sk_ref_sp(gpuConfig->getColorSpace()),
svaisanenc47635e2016-01-28 06:05:43 -0800433 sampleCount,
434 ctxType,
kkinnunend4e1c352016-03-01 23:41:26 -0800435 ctxOptions,
436 gpuConfig->getUseDIText()
437 };
svaisanenc47635e2016-01-28 06:05:43 -0800438
439 configs->push_back(target);
440 return;
441 }
442#endif
443
brianosmanb109b8c2016-06-16 13:03:24 -0700444 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
445 if (config->getTag().equals(#name)) { \
446 Config config = { \
447 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
448 0, kBogusContextType, kBogusContextOptions, false \
449 }; \
450 configs->push_back(config); \
451 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700452 }
mtkleine714e752014-07-31 12:13:48 -0700453
mtklein40b32be2014-07-09 08:46:49 -0700454 if (FLAGS_cpu) {
mtkleinbb6c41b2016-03-08 11:31:11 -0800455 CPU_CONFIG(nonrendering, kNonRendering_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700456 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800457
458 CPU_CONFIG(8888, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700459 kN32_SkColorType, kPremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800460 CPU_CONFIG(565, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700461 kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
Brian Osman526972e2016-10-24 09:24:02 -0400462 auto srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
mtkleinbb6c41b2016-03-08 11:31:11 -0800463 CPU_CONFIG(srgb, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700464 kN32_SkColorType, kPremul_SkAlphaType, srgbColorSpace)
Brian Osman526972e2016-10-24 09:24:02 -0400465 auto srgbLinearColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named);
mtkleinbb6c41b2016-03-08 11:31:11 -0800466 CPU_CONFIG(f16, kRaster_Backend,
raftias7c602de2016-10-13 10:45:44 -0700467 kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbLinearColorSpace)
mtklein40b32be2014-07-09 08:46:49 -0700468 }
mtkleinbb6a0282014-07-01 08:43:42 -0700469
svaisanenc47635e2016-01-28 06:05:43 -0800470 #undef CPU_CONFIG
tomhudsond968a6f2015-03-26 11:28:06 -0700471
472#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
djsollen817c72a2016-01-28 13:57:02 -0800473 if (config->getTag().equals("hwui")) {
mtkleinbb6c41b2016-03-08 11:31:11 -0800474 Config config = { SkString("hwui"), Benchmark::kHWUI_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700475 kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr,
bsalomon85b4b532016-04-05 11:06:27 -0700476 0, kBogusContextType, kBogusContextOptions, false };
svaisanenc47635e2016-01-28 06:05:43 -0800477 configs->push_back(config);
tomhudsond968a6f2015-03-26 11:28:06 -0700478 }
479#endif
mtkleinf3723212014-06-25 14:08:00 -0700480}
481
svaisanenc47635e2016-01-28 06:05:43 -0800482// Append all configs that are enabled and supported.
483void create_configs(SkTArray<Config>* configs) {
484 SkCommandLineConfigArray array;
485 ParseConfigs(FLAGS_config, &array);
486 for (int i = 0; i < array.count(); ++i) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400487 create_config(array[i].get(), configs);
svaisanenc47635e2016-01-28 06:05:43 -0800488 }
489}
490
brianosman9f1f6e22016-09-15 08:33:02 -0700491// disable warning : switch statement contains default but no 'case' labels
492#if defined _WIN32
493#pragma warning ( push )
494#pragma warning ( disable : 4065 )
495#endif
496
halcanary96fcdcc2015-08-27 07:41:13 -0700497// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700498static Target* is_enabled(Benchmark* bench, const Config& config) {
499 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700500 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700501 }
502
reede5ea5002014-09-03 11:54:58 -0700503 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700504 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700505
halcanary96fcdcc2015-08-27 07:41:13 -0700506 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700507
tomhudsond968a6f2015-03-26 11:28:06 -0700508 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700509#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700510 case Benchmark::kGPU_Backend:
511 target = new GPUTarget(config);
512 break;
bsalomonc2553372014-07-22 13:09:05 -0700513#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700514#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
515 case Benchmark::kHWUI_Backend:
516 target = new HWUITarget(config, bench);
517 break;
518#endif
519 default:
520 target = new Target(config);
521 break;
522 }
bsalomonc2553372014-07-22 13:09:05 -0700523
tomhudsond968a6f2015-03-26 11:28:06 -0700524 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700525 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700526 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700527 }
528 return target;
529}
530
brianosman9f1f6e22016-09-15 08:33:02 -0700531#if defined _WIN32
532#pragma warning ( pop )
533#endif
534
reed42943c82016-09-12 12:01:44 -0700535static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700536 uint32_t minOutputSize, int* width, int* height) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400537 std::unique_ptr<SkBitmapRegionDecoder> brd(
msarettd1227a72016-05-18 06:23:57 -0700538 SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy));
msarett7f691442015-09-22 11:56:16 -0700539 if (nullptr == brd.get()) {
540 // This is indicates that subset decoding is not supported for a particular image format.
541 return false;
542 }
543
msarett7f691442015-09-22 11:56:16 -0700544 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
545 (uint32_t) brd->height()) {
546 // This indicates that the image is not large enough to decode a
547 // minOutputSize x minOutputSize subset at the given sampleSize.
548 return false;
549 }
550
551 // Set the image width and height. The calling code will use this to choose subsets to decode.
552 *width = brd->width();
553 *height = brd->height();
554 return true;
555}
556
egdaniel3bf92062015-06-26 08:12:46 -0700557static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700558 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700559#if SK_SUPPORT_GPU
560 if (FLAGS_abandonGpuContext) {
561 gGrFactory->abandonContexts();
562 }
563 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
564 gGrFactory->destroyContexts();
565 }
566#endif
567}
568
fmalita6519c212016-09-14 08:05:17 -0700569static void collect_files(const SkCommandLineFlags::StringArray& paths, const char* ext,
570 SkTArray<SkString>* list) {
571 for (int i = 0; i < paths.count(); ++i) {
572 if (SkStrEndsWith(paths[i], ext)) {
573 list->push_back(SkString(paths[i]));
574 } else {
575 SkOSFile::Iter it(paths[i], ext);
576 SkString path;
577 while (it.next(&path)) {
578 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
579 }
580 }
581 }
582}
583
mtkleine714e752014-07-31 12:13:48 -0700584class BenchmarkStream {
585public:
mtklein92007582014-08-01 07:46:52 -0700586 BenchmarkStream() : fBenches(BenchRegistry::Head())
587 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700588 , fCurrentRecording(0)
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400589 , fCurrentPiping(0)
mtklein92007582014-08-01 07:46:52 -0700590 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800591 , fCurrentSKP(0)
fmalita6519c212016-09-14 08:05:17 -0700592 , fCurrentSVG(0)
msarett95f192d2015-02-13 09:05:41 -0800593 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700594 , fCurrentCodec(0)
msarett84451022016-02-11 06:45:51 -0800595 , fCurrentAndroidCodec(0)
msarett7f691442015-09-22 11:56:16 -0700596 , fCurrentBRDImage(0)
msarett2cee9022016-06-03 08:25:21 -0700597 , fCurrentColorImage(0)
msarett95f192d2015-02-13 09:05:41 -0800598 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800599 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700600 , fCurrentSubsetType(0)
msarett84451022016-02-11 06:45:51 -0800601 , fCurrentSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700602 , fCurrentAnimSKP(0) {
fmalita6519c212016-09-14 08:05:17 -0700603 collect_files(FLAGS_skps, ".skp", &fSKPs);
604 collect_files(FLAGS_svgs, ".svg", &fSVGs);
mtkleine714e752014-07-31 12:13:48 -0700605
mtklein92007582014-08-01 07:46:52 -0700606 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
607 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
608 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
609 exit(1);
610 }
611
612 for (int i = 0; i < FLAGS_scales.count(); i++) {
613 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
614 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
615 exit(1);
616 }
617 }
robertphillips5b693772014-11-21 06:19:36 -0800618
cdalton63a82852015-06-29 14:06:10 -0700619 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
620 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700621 exit(1);
622 }
623
robertphillips5b693772014-11-21 06:19:36 -0800624 if (FLAGS_mpd) {
625 fUseMPDs.push_back() = true;
626 }
mtkleinc751ecb2015-06-15 08:56:38 -0700627 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700628
msarett95f192d2015-02-13 09:05:41 -0800629 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700630 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800631 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800632 }
msarett2cee9022016-06-03 08:25:21 -0700633 if (!CollectImages(FLAGS_colorImages, &fColorImages)) {
634 exit(1);
635 }
mtklein95553d92015-03-12 08:24:21 -0700636
msarett95f192d2015-02-13 09:05:41 -0800637 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700638 fColorTypes.push_back(kN32_SkColorType);
639 if (!FLAGS_simpleCodec) {
640 fColorTypes.push_back(kRGB_565_SkColorType);
641 fColorTypes.push_back(kAlpha_8_SkColorType);
642 fColorTypes.push_back(kIndex_8_SkColorType);
643 fColorTypes.push_back(kGray_8_SkColorType);
644 }
mtklein92007582014-08-01 07:46:52 -0700645 }
646
reedca2622b2016-03-18 07:25:55 -0700647 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700648 // Not strictly necessary, as it will be checked again later,
649 // but helps to avoid a lot of pointless work if we're going to skip it.
cdalton91e457d2016-02-17 11:10:16 -0800650 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700651 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700652 }
653
bungemanf93d7112016-09-16 06:24:20 -0700654 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
655 if (!stream) {
mtkleinfd731ce2014-09-10 12:19:30 -0700656 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700657 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700658 }
659
reedca2622b2016-03-18 07:25:55 -0700660 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700661 }
662
fmalita6519c212016-09-14 08:05:17 -0700663 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
664 SkFILEStream stream(path);
665 if (!stream.isValid()) {
666 SkDebugf("Could not read %s.\n", path);
667 return nullptr;
668 }
669
fmalitae1baa7c2016-09-14 12:04:30 -0700670 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
fmalita6519c212016-09-14 08:05:17 -0700671 if (!svgDom) {
672 SkDebugf("Could not parse %s.\n", path);
673 return nullptr;
674 }
675
fmalitae1baa7c2016-09-14 12:04:30 -0700676 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
677 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
678 if (svgDom->containerSize().isEmpty()) {
679 svgDom->setContainerSize(kDefaultContainerSize);
680 }
681
fmalita6519c212016-09-14 08:05:17 -0700682 SkPictureRecorder recorder;
fmalitae1baa7c2016-09-14 12:04:30 -0700683 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
684 svgDom->containerSize().height()));
fmalita6519c212016-09-14 08:05:17 -0700685 return recorder.finishRecordingAsPicture();
686 }
687
mtklein92007582014-08-01 07:46:52 -0700688 Benchmark* next() {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400689 std::unique_ptr<Benchmark> bench;
mtklein65dfd2f2016-02-03 10:40:54 -0800690 do {
691 bench.reset(this->rawNext());
692 if (!bench) {
693 return nullptr;
694 }
695 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
696 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700697 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800698 }
699
700 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700701 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700702 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700703 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700704 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700705 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700706 return bench;
707 }
mtklein92007582014-08-01 07:46:52 -0700708
mtkleine714e752014-07-31 12:13:48 -0700709 while (fGMs) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400710 std::unique_ptr<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700711 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800712 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700713 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700714 fBenchType = "micro";
mtklein18300a32016-03-16 13:53:35 -0700715 return new GMBench(gm.release());
mtkleine714e752014-07-31 12:13:48 -0700716 }
717 }
mtklein92007582014-08-01 07:46:52 -0700718
mtkleinfd731ce2014-09-10 12:19:30 -0700719 // First add all .skps as RecordingBenches.
720 while (fCurrentRecording < fSKPs.count()) {
721 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700722 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
723 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700724 continue;
725 }
726 SkString name = SkOSPath::Basename(path.c_str());
727 fSourceType = "skp";
728 fBenchType = "recording";
reedca2622b2016-03-18 07:25:55 -0700729 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic.get()));
mtklein051e56d2014-12-04 08:46:51 -0800730 fSKPOps = pic->approximateOpCount();
mtklein8c1a4f82016-08-08 06:56:22 -0700731 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh, FLAGS_lite);
mtkleinfd731ce2014-09-10 12:19:30 -0700732 }
733
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400734 // Add all .skps as PipeBenches.
735 while (fCurrentPiping < fSKPs.count()) {
736 const SkString& path = fSKPs[fCurrentPiping++];
737 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
738 if (!pic) {
739 continue;
740 }
741 SkString name = SkOSPath::Basename(path.c_str());
742 fSourceType = "skp";
743 fBenchType = "piping";
744 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic.get()));
745 fSKPOps = pic->approximateOpCount();
746 return new PipingBench(name.c_str(), pic.get());
747 }
748
mtkleinfd731ce2014-09-10 12:19:30 -0700749 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700750 while (fCurrentScale < fScales.count()) {
751 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800752 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700753 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
754 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800755 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700756 continue;
757 }
robertphillips5b693772014-11-21 06:19:36 -0800758
759 while (fCurrentUseMPD < fUseMPDs.count()) {
760 if (FLAGS_bbh) {
761 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
762 SkRTreeFactory factory;
763 SkPictureRecorder recorder;
robertphillips5b693772014-11-21 06:19:36 -0800764 pic->playback(recorder.beginRecording(pic->cullRect().width(),
765 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800766 &factory,
robertphillipsdda54452016-07-13 13:27:16 -0700767 0));
reedca2622b2016-03-18 07:25:55 -0700768 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800769 }
770 SkString name = SkOSPath::Basename(path.c_str());
771 fSourceType = "skp";
772 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700773 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
774 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700775 }
robertphillips5b693772014-11-21 06:19:36 -0800776 fCurrentUseMPD = 0;
777 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700778 }
fmalita6519c212016-09-14 08:05:17 -0700779
780 while (fCurrentSVG++ < fSVGs.count()) {
781 const char* path = fSVGs[fCurrentSVG - 1].c_str();
782 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
783 fSourceType = "svg";
784 fBenchType = "playback";
785 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
786 fScales[fCurrentScale], false, FLAGS_loopSKP);
787 }
788 }
789
mtklein92007582014-08-01 07:46:52 -0700790 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700791 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700792 fCurrentScale++;
793 }
794
joshualitt261c3ad2015-04-27 09:16:57 -0700795 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700796 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700797 while (fCurrentAnimSKP < fSKPs.count()) {
798 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700799 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
800 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700801 fCurrentAnimSKP++;
802 continue;
803 }
804
805 fCurrentAnimSKP++;
806 SkString name = SkOSPath::Basename(path.c_str());
Hal Canary2db83612016-11-04 13:02:54 -0400807 sk_sp<SKPAnimationBench::Animation> animation(
cdalton63a82852015-06-29 14:06:10 -0700808 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
Hal Canary2db83612016-11-04 13:02:54 -0400809 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation.get(),
halcanary385fe4d2015-08-26 13:07:48 -0700810 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700811 }
812 }
813
scroggo60869a42015-04-01 12:09:17 -0700814 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700815 fSourceType = "image";
816 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700817 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800818 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
819 continue;
820 }
bungeman38d909e2016-08-02 14:40:46 -0700821 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Ben Wagner145dbcd2016-11-03 14:40:50 -0400822 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700823 if (!codec) {
824 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700825 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700826 continue;
827 }
scroggo21027992015-04-02 13:22:38 -0700828
scroggo60869a42015-04-01 12:09:17 -0700829 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700830 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700831
msarettc7796b92016-01-07 14:20:20 -0800832 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700833 if (FLAGS_simpleCodec) {
834 if (kUnpremul_SkAlphaType == alphaType) {
835 alphaType = kPremul_SkAlphaType;
836 }
837
838 fCurrentColorType++;
839 } else {
840 switch (alphaType) {
841 case kOpaque_SkAlphaType:
842 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800843 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700844 break;
845 case kUnpremul_SkAlphaType:
846 case kPremul_SkAlphaType:
847 if (0 == fCurrentAlphaType) {
848 // Test unpremul first.
849 alphaType = kUnpremul_SkAlphaType;
850 fCurrentAlphaType++;
851 } else {
852 // Test premul.
853 alphaType = kPremul_SkAlphaType;
854 fCurrentAlphaType = 0;
855 fCurrentColorType++;
856 }
857 break;
858 default:
859 SkASSERT(false);
860 fCurrentColorType++;
861 break;
862 }
scroggo21027992015-04-02 13:22:38 -0700863 }
864
msarettc7796b92016-01-07 14:20:20 -0800865 // Make sure we can decode to this color type and alpha type.
866 SkImageInfo info =
867 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700868 const size_t rowBytes = info.minRowBytes();
869 SkAutoMalloc storage(info.getSafeSize(rowBytes));
870
871 // Used if fCurrentColorType is kIndex_8_SkColorType
872 int colorCount = 256;
873 SkPMColor colors[256];
874
scroggoeb602a52015-07-09 08:16:03 -0700875 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700876 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700877 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700878 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700879 case SkCodec::kSuccess:
880 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700881 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700882 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700883 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700884 // This is okay. Not all conversions are valid.
885 break;
scroggo60869a42015-04-01 12:09:17 -0700886 default:
887 // This represents some sort of failure.
888 SkASSERT(false);
889 break;
890 }
891 }
892 fCurrentColorType = 0;
893 }
894
msarett84451022016-02-11 06:45:51 -0800895 // Run AndroidCodecBenches
896 const int sampleSizes[] = { 2, 4, 8 };
897 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
898 fSourceType = "image";
899 fBenchType = "skandroidcodec";
900
901 const SkString& path = fImages[fCurrentAndroidCodec];
902 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
903 continue;
904 }
bungeman38d909e2016-08-02 14:40:46 -0700905 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Ben Wagner145dbcd2016-11-03 14:40:50 -0400906 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800907 if (!codec) {
908 // Nothing to time.
909 SkDebugf("Cannot find codec for %s\n", path.c_str());
910 continue;
911 }
912
913 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
914 int sampleSize = sampleSizes[fCurrentSampleSize];
915 fCurrentSampleSize++;
916 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
917 // Avoid benchmarking scaled decodes of already small images.
918 break;
919 }
920
bungeman38d909e2016-08-02 14:40:46 -0700921 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
922 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -0800923 }
924 fCurrentSampleSize = 0;
925 }
926
msarett7f691442015-09-22 11:56:16 -0700927 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700928 // We intend to create benchmarks that model the use cases in
929 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
930 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
931 // the image. For that reason, we will benchmark decodes in five representative locations
932 // in the image. Additionally, this use case utilizes power of two scaling, so we will
933 // test on power of two sample sizes. The output tile is always 512x512, so, when a
934 // sampleSize is used, the size of the subset that is decoded is always
935 // (sampleSize*512)x(sampleSize*512).
936 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700937 // All use cases we are aware of only scale by powers of two.
938 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
939 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800940 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700941 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800942 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700943 fSourceType = "image";
944 fBenchType = "BRD";
945
mtklein6f0ff912016-01-11 09:04:21 -0800946 const SkString& path = fImages[fCurrentBRDImage];
947 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
948 continue;
949 }
scroggo860e8a62015-10-15 07:51:28 -0700950
msarettd1227a72016-05-18 06:23:57 -0700951 while (fCurrentColorType < fColorTypes.count()) {
952 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
953 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700954
bungeman38d909e2016-08-02 14:40:46 -0700955 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -0700956 const SkColorType colorType = fColorTypes[fCurrentColorType];
957 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
958 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -0700959
msarettd1227a72016-05-18 06:23:57 -0700960 int width = 0;
961 int height = 0;
reed42943c82016-09-12 12:01:44 -0700962 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -0700963 &width, &height)) {
964 break;
msarett7f691442015-09-22 11:56:16 -0700965 }
msarettd1227a72016-05-18 06:23:57 -0700966
967 SkString basename = SkOSPath::Basename(path.c_str());
968 SkIRect subset;
969 const uint32_t subsetSize = sampleSize * minOutputSize;
970 switch (currentSubsetType) {
971 case kTopLeft_SubsetType:
972 basename.append("_TopLeft");
973 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
974 break;
975 case kTopRight_SubsetType:
976 basename.append("_TopRight");
977 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
978 subsetSize);
979 break;
980 case kMiddle_SubsetType:
981 basename.append("_Middle");
982 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
983 (height - subsetSize) / 2, subsetSize, subsetSize);
984 break;
985 case kBottomLeft_SubsetType:
986 basename.append("_BottomLeft");
987 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
988 subsetSize);
989 break;
990 case kBottomRight_SubsetType:
991 basename.append("_BottomRight");
992 subset = SkIRect::MakeXYWH(width - subsetSize,
993 height - subsetSize, subsetSize, subsetSize);
994 break;
995 default:
996 SkASSERT(false);
997 }
998
999 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
1000 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -07001001 }
msarettd1227a72016-05-18 06:23:57 -07001002 fCurrentSubsetType = 0;
1003 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -07001004 }
msarettd1227a72016-05-18 06:23:57 -07001005 fCurrentSampleSize = 0;
1006 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -07001007 }
msarettd1227a72016-05-18 06:23:57 -07001008 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -07001009 }
1010
msarett2cee9022016-06-03 08:25:21 -07001011 while (fCurrentColorImage < fColorImages.count()) {
1012 fSourceType = "colorimage";
1013 fBenchType = "skcolorcodec";
1014 const SkString& path = fColorImages[fCurrentColorImage];
1015 fCurrentColorImage++;
1016 sk_sp<SkData> encoded = SkData::MakeFromFileName(path.c_str());
1017 if (encoded) {
1018 return new ColorCodecBench(SkOSPath::Basename(path.c_str()).c_str(),
1019 std::move(encoded));
1020 } else {
1021 SkDebugf("Could not read file %s.\n", path.c_str());
1022 }
1023 }
1024
halcanary96fcdcc2015-08-27 07:41:13 -07001025 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001026 }
mtklein92007582014-08-01 07:46:52 -07001027
1028 void fillCurrentOptions(ResultsWriter* log) const {
1029 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001030 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001031 if (0 == strcmp(fSourceType, "skp")) {
1032 log->configOption("clip",
1033 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1034 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001035 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -07001036 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001037 if (fCurrentUseMPD > 0) {
1038 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1039 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1040 }
mtklein92007582014-08-01 07:46:52 -07001041 }
mtklein051e56d2014-12-04 08:46:51 -08001042 if (0 == strcmp(fBenchType, "recording")) {
1043 log->metric("bytes", fSKPBytes);
1044 log->metric("ops", fSKPOps);
1045 }
mtklein92007582014-08-01 07:46:52 -07001046 }
1047
mtkleine714e752014-07-31 12:13:48 -07001048private:
msarettb23e6aa2015-06-09 13:56:10 -07001049 enum SubsetType {
1050 kTopLeft_SubsetType = 0,
1051 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001052 kMiddle_SubsetType = 2,
1053 kBottomLeft_SubsetType = 3,
1054 kBottomRight_SubsetType = 4,
1055 kTranslate_SubsetType = 5,
1056 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001057 kLast_SubsetType = kZoom_SubsetType,
1058 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001059 };
1060
mtkleine714e752014-07-31 12:13:48 -07001061 const BenchRegistry* fBenches;
1062 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001063 SkIRect fClip;
1064 SkTArray<SkScalar> fScales;
1065 SkTArray<SkString> fSKPs;
fmalita6519c212016-09-14 08:05:17 -07001066 SkTArray<SkString> fSVGs;
robertphillips5b693772014-11-21 06:19:36 -08001067 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001068 SkTArray<SkString> fImages;
msarett2cee9022016-06-03 08:25:21 -07001069 SkTArray<SkString> fColorImages;
msarett74deb982015-10-20 16:45:56 -07001070 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001071 SkScalar fZoomMax;
1072 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001073
mtklein051e56d2014-12-04 08:46:51 -08001074 double fSKPBytes, fSKPOps;
1075
mtkleinfd731ce2014-09-10 12:19:30 -07001076 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1077 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1078 int fCurrentRecording;
Mike Reed9cdd2ab2016-10-21 10:43:36 -04001079 int fCurrentPiping;
mtklein92007582014-08-01 07:46:52 -07001080 int fCurrentScale;
1081 int fCurrentSKP;
fmalita6519c212016-09-14 08:05:17 -07001082 int fCurrentSVG;
robertphillips5b693772014-11-21 06:19:36 -08001083 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001084 int fCurrentCodec;
msarett84451022016-02-11 06:45:51 -08001085 int fCurrentAndroidCodec;
msarett7f691442015-09-22 11:56:16 -07001086 int fCurrentBRDImage;
msarett2cee9022016-06-03 08:25:21 -07001087 int fCurrentColorImage;
msarett95f192d2015-02-13 09:05:41 -08001088 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -08001089 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -07001090 int fCurrentSubsetType;
msarett84451022016-02-11 06:45:51 -08001091 int fCurrentSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001092 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001093};
1094
msarettc149f0e2016-01-04 11:35:43 -08001095// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1096// This prints something every once in a while so that it knows we're still working.
1097static void start_keepalive() {
1098 struct Loop {
1099 static void forever(void*) {
1100 for (;;) {
1101 static const int kSec = 1200;
1102 #if defined(SK_BUILD_FOR_WIN)
1103 Sleep(kSec * 1000);
1104 #else
1105 sleep(kSec);
1106 #endif
1107 SkDebugf("\nBenchmarks still running...\n");
1108 }
1109 }
1110 };
1111 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1112 intentionallyLeaked->start();
1113}
1114
jcgregorio3b27ade2014-11-13 08:06:40 -08001115int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001116int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001117 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001118 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001119 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001120
krajcevski69a55602014-08-13 10:46:31 -07001121#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001122 GrContextOptions grContextOpts;
halcanary385fe4d2015-08-26 13:07:48 -07001123 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001124#endif
1125
bsalomon06cddec2014-10-24 10:40:50 -07001126 if (FLAGS_veryVerbose) {
1127 FLAGS_verbose = true;
1128 }
1129
bsalomon6eb03cc2014-08-07 14:28:50 -07001130 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001131 FLAGS_samples = 1;
1132 FLAGS_gpuFrameLag = 0;
1133 }
1134
bsalomon6eb03cc2014-08-07 14:28:50 -07001135 if (!FLAGS_writePath.isEmpty()) {
1136 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1137 if (!sk_mkdir(FLAGS_writePath[0])) {
1138 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001139 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001140 }
1141 }
1142
Ben Wagner145dbcd2016-11-03 14:40:50 -04001143 std::unique_ptr<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001144 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001145#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001146 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001147#else
1148 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1149 return 1;
1150#endif
mtklein60317d0f2014-07-14 11:30:37 -07001151 }
mtklein1915b622014-08-20 11:45:00 -07001152
1153 if (1 == FLAGS_properties.count() % 2) {
1154 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1155 return 1;
1156 }
1157 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1158 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1159 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001160
1161 if (1 == FLAGS_key.count() % 2) {
1162 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1163 return 1;
1164 }
1165 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001166 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001167 }
mtklein60317d0f2014-07-14 11:30:37 -07001168
mtkleinf3723212014-06-25 14:08:00 -07001169 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001170 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001171
cdaltone1b89582015-06-25 19:17:08 -07001172 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001173
bsalomon6eb03cc2014-08-07 14:28:50 -07001174 if (kAutoTuneLoops != FLAGS_loops) {
1175 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001176 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001177 SkDebugf("! -> high variance, ? -> moderate variance\n");
1178 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001179 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001180 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001181 } else {
mtkleind75c4662015-04-30 07:11:22 -07001182 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001183 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001184 }
1185
svaisanenc47635e2016-01-28 06:05:43 -08001186 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001187 create_configs(&configs);
1188
joshualitt3ebd0502016-02-09 07:18:08 -08001189#ifdef THERMAL_MANAGER_SUPPORTED
1190 int tmEnabled, tmThreshold, tmSleepTimeMs, tmTimeoutMs;
1191 if (4 != sscanf(FLAGS_useThermalManager[0], "%d,%d,%d,%d",
1192 &tmEnabled, &tmThreshold, &tmSleepTimeMs, &tmTimeoutMs)) {
1193 SkDebugf("Can't parse %s from --useThermalManager.\n", FLAGS_useThermalManager[0]);
1194 exit(1);
1195 }
1196 ThermalManager tm(tmThreshold, tmSleepTimeMs, tmTimeoutMs);
1197#endif
1198
msarettc149f0e2016-01-04 11:35:43 -08001199 if (FLAGS_keepAlive) {
1200 start_keepalive();
1201 }
1202
Yuqian Liba62b4a2016-12-14 13:46:53 -05001203 gSkUseAnalyticAA = FLAGS_analyticAA;
liyuqian38911a72016-10-04 11:23:22 -07001204
Yuqian Li550148b2017-01-13 10:13:13 -05001205 if (FLAGS_forceAnalyticAA) {
1206 gSkForceAnalyticAA = true;
1207 }
1208
mtkleine070c2b2014-10-14 08:40:43 -07001209 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001210 BenchmarkStream benchStream;
1211 while (Benchmark* b = benchStream.next()) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001212 std::unique_ptr<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001213 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001214 continue;
1215 }
1216
svaisanenc47635e2016-01-28 06:05:43 -08001217 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001218 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001219 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001220 }
egdaniel3bf92062015-06-26 08:12:46 -07001221 for (int i = 0; i < configs.count(); ++i) {
joshualitt3ebd0502016-02-09 07:18:08 -08001222#ifdef THERMAL_MANAGER_SUPPORTED
1223 if (tmEnabled && !tm.coolOffIfNecessary()) {
1224 SkDebugf("Could not cool off, timings will be throttled\n");
1225 }
1226#endif
egdaniel3bf92062015-06-26 08:12:46 -07001227 Target* target = is_enabled(b, configs[i]);
1228 if (!target) {
1229 continue;
1230 }
mtkleinf3723212014-06-25 14:08:00 -07001231
halcanary96fcdcc2015-08-27 07:41:13 -07001232 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001233 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001234 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001235
brianosman7a5ada82016-02-08 13:49:12 -08001236 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001237 SkDebugf("Running %s\t%s\n"
1238 , bench->getUniqueName()
1239 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001240 if (FLAGS_dryRun) {
1241 continue;
1242 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001243 }
1244
egdaniel3bf92062015-06-26 08:12:46 -07001245 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001246 bench->perCanvasPreDraw(canvas);
1247
cdaltone1b89582015-06-25 19:17:08 -07001248 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001249 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001250 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1251 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001252
mtkleinbbba1682015-10-28 11:36:30 -07001253 if (FLAGS_ms) {
1254 samples.reset();
1255 auto stop = now_ms() + FLAGS_ms;
1256 do {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001257 samples.push_back(time(loops, bench.get(), target) / loops);
mtkleinbbba1682015-10-28 11:36:30 -07001258 } while (now_ms() < stop);
1259 } else {
cdaltone1b89582015-06-25 19:17:08 -07001260 samples.reset(FLAGS_samples);
1261 for (int s = 0; s < FLAGS_samples; s++) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001262 samples[s] = time(loops, bench.get(), target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001263 }
cdaltone1b89582015-06-25 19:17:08 -07001264 }
mtkleinf3723212014-06-25 14:08:00 -07001265
joshualitte45c81c2015-12-02 09:05:37 -08001266#if SK_SUPPORT_GPU
1267 SkTArray<SkString> keys;
1268 SkTArray<double> values;
1269 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1270 if (gpuStatsDump) {
1271 // TODO cache stats
1272 bench->getGpuStats(canvas, &keys, &values);
1273 }
1274#endif
1275
robertphillips5b693772014-11-21 06:19:36 -08001276 bench->perCanvasPostDraw(canvas);
1277
egdaniel3bf92062015-06-26 08:12:46 -07001278 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001279 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001280 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001281 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001282 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001283 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001284 }
1285
1286 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001287 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001288 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001289 continue;
1290 }
1291
cdaltone1b89582015-06-25 19:17:08 -07001292 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001293 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001294 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001295 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001296 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001297 log->metric("min_ms", stats.min);
George Burgess IV75b57182016-12-06 10:53:52 -08001298 log->metrics("samples", samples);
joshualitte45c81c2015-12-02 09:05:37 -08001299#if SK_SUPPORT_GPU
1300 if (gpuStatsDump) {
1301 // dump to json, only SKPBench currently returns valid keys / values
1302 SkASSERT(keys.count() == values.count());
1303 for (int i = 0; i < keys.count(); i++) {
1304 log->metric(keys[i].c_str(), values[i]);
1305 }
1306 }
1307#endif
1308
mtkleine070c2b2014-10-14 08:40:43 -07001309 if (runs++ % FLAGS_flushEvery == 0) {
1310 log->flush();
1311 }
mtklein60317d0f2014-07-14 11:30:37 -07001312
bsalomon6eb03cc2014-08-07 14:28:50 -07001313 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001314 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001315 config = ""; // Only print the config if we run the same bench on more than one.
1316 }
mtkleind75c4662015-04-30 07:11:22 -07001317 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1318 , sk_tools::getCurrResidentSetSizeMB()
1319 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001320 , bench->getUniqueName()
1321 , config);
mtkleinf3723212014-06-25 14:08:00 -07001322 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001323 const char* mark = " ";
1324 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1325 if (stddev_percent > 5) mark = "?";
1326 if (stddev_percent > 10) mark = "!";
1327
1328 SkDebugf("%10.2f %s\t%s\t%s\n",
1329 stats.median*1e3, mark, bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001330 } else {
1331 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001332 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1333 , sk_tools::getCurrResidentSetSizeMB()
1334 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001335 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001336 , HUMANIZE(stats.min)
1337 , HUMANIZE(stats.median)
1338 , HUMANIZE(stats.mean)
1339 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001340 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001341 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001342 , config
mtklein96289052014-09-10 12:05:59 -07001343 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001344 );
1345 }
joshualitte45c81c2015-12-02 09:05:37 -08001346
bsalomonb12ea412015-02-02 21:19:50 -08001347#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001348 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001349 GrContext* context = gGrFactory->get(configs[i].ctxType,
kkinnunen3e980c32015-12-23 01:33:00 -08001350 configs[i].ctxOptions);
kkinnunen5219fd92015-12-10 06:28:13 -08001351 context->printCacheStats();
1352 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001353 }
1354#endif
joshualitte45c81c2015-12-02 09:05:37 -08001355
cdalton2c56ba52015-06-26 13:32:53 -07001356 if (FLAGS_verbose) {
1357 SkDebugf("Samples: ");
1358 for (int i = 0; i < samples.count(); i++) {
1359 SkDebugf("%s ", HUMANIZE(samples[i]));
1360 }
1361 SkDebugf("%s\n", bench->getUniqueName());
1362 }
egdaniel3bf92062015-06-26 08:12:46 -07001363 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001364 }
mtkleinf3723212014-06-25 14:08:00 -07001365 }
1366
mtkleine1091452014-12-04 10:47:02 -08001367 log->bench("memory_usage", 0,0);
1368 log->config("meta");
1369 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1370
joshualitte0b19d42015-03-26 10:41:02 -07001371#if SK_SUPPORT_GPU
1372 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1373 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001374 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001375#endif
1376
mtkleinf3723212014-06-25 14:08:00 -07001377 return 0;
1378}
1379
jcgregorio3b27ade2014-11-13 08:06:40 -08001380#if !defined SK_BUILD_FOR_IOS
1381int main(int argc, char** argv) {
1382 SkCommandLineFlags::Parse(argc, argv);
1383 return nanobench_main();
1384}
1385#endif