blob: 7e053198e42f73b6a2c62d31e9562aab7585ee84 [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
mtkleinb5110422014-08-07 15:20:02 -070074static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070075#ifdef SK_DEBUG
76 1;
mtkleina189ccd2014-07-14 12:28:47 -070077#else
bsalomon6eb03cc2014-08-07 14:28:50 -070078 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070079#endif
80
bsalomon6eb03cc2014-08-07 14:28:50 -070081static SkString loops_help_txt() {
82 SkString help;
83 help.printf("Number of times to run each bench. Set this to %d to auto-"
84 "tune for each bench. Timings are only reported when auto-tuning.",
85 kAutoTuneLoops);
86 return help;
87}
88
cdaltone1b89582015-06-25 19:17:08 -070089static SkString to_string(int n) {
90 SkString str;
91 str.appendS32(n);
92 return str;
93}
94
bsalomon6eb03cc2014-08-07 14:28:50 -070095DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
96
mtkleinf3723212014-06-25 14:08:00 -070097DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -070098DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -070099DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
100DEFINE_double(overheadGoal, 0.0001,
101 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -0700102DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -0700103DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -0700104
mtklein60317d0f2014-07-14 11:30:37 -0700105DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -0700106DEFINE_int32(maxCalibrationAttempts, 3,
107 "Try up to this many times to guess loops for a bench, or skip the bench.");
108DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700109DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
110DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700111DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
112 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700113DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
mtklein8c1a4f82016-08-08 06:56:22 -0700114DEFINE_bool(lite, false, "Use SkLiteRecorder in recording benchmarks?");
robertphillips5b693772014-11-21 06:19:36 -0800115DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700116DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700117DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800118DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800119DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800120DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
msarettc149f0e2016-01-04 11:35:43 -0800121DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
joshualitt3ebd0502016-02-09 07:18:08 -0800122DEFINE_string(useThermalManager, "0,1,10,1000", "enabled,threshold,sleepTimeMs,TimeoutMs for "
123 "thermalManager\n");
mtklein92007582014-08-01 07:46:52 -0700124
mtklein65dfd2f2016-02-03 10:40:54 -0800125DEFINE_string(sourceType, "",
126 "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
127DEFINE_string(benchType, "",
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400128 "Apply usual --match rules to bench type: micro, recording, piping, playback, skcodec, etc.");
mtklein65dfd2f2016-02-03 10:40:54 -0800129
mtkleinbbba1682015-10-28 11:36:30 -0700130static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
131
mtkleinf3723212014-06-25 14:08:00 -0700132static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700133 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800134 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700135}
mtklein55b0ffc2014-07-17 08:38:23 -0700136#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700137
tomhudsond968a6f2015-03-26 11:28:06 -0700138bool Target::init(SkImageInfo info, Benchmark* bench) {
139 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700140 this->surface = SkSurface::MakeRaster(info);
141 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700142 return false;
143 }
144 }
145 return true;
146}
147bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700148 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700149 if (!canvas) {
150 return false;
151 }
152 bmp->setInfo(canvas->imageInfo());
153 if (!canvas->readPixels(bmp, 0, 0)) {
154 SkDebugf("Can't read canvas pixels.\n");
155 return false;
156 }
157 return true;
158}
159
160#if SK_SUPPORT_GPU
161struct GPUTarget : public Target {
bsalomonc8699322016-05-11 11:55:36 -0700162 explicit GPUTarget(const Config& c) : Target(c), context(nullptr) { }
163 TestContext* context;
tomhudsond968a6f2015-03-26 11:28:06 -0700164
165 void setup() override {
bsalomonc8699322016-05-11 11:55:36 -0700166 this->context->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700167 // Make sure we're done with whatever came before.
bsalomonc8699322016-05-11 11:55:36 -0700168 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700169 }
170 void endTiming() override {
bsalomonc8699322016-05-11 11:55:36 -0700171 if (this->context) {
172 this->context->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700173 }
174 }
175 void fence() override {
bsalomonc8699322016-05-11 11:55:36 -0700176 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700177 }
mtkleind75c4662015-04-30 07:11:22 -0700178
cdaltond416a5b2015-06-23 13:23:44 -0700179 bool needsFrameTiming(int* maxFrameLag) const override {
bsalomonc8699322016-05-11 11:55:36 -0700180 if (!this->context->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700181 // Frame lag is unknown.
182 *maxFrameLag = FLAGS_gpuFrameLag;
183 }
184 return true;
185 }
tomhudsond968a6f2015-03-26 11:28:06 -0700186 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700187 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
188 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700189 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700190 this->surface = SkSurface::MakeRenderTarget(gGrFactory->get(this->config.ctxType,
191 this->config.ctxOptions),
bsalomon5ec26ae2016-02-25 08:33:02 -0800192 SkBudgeted::kNo, info,
reede8f30622016-03-23 18:59:25 -0700193 this->config.samples, &props);
bsalomonc8699322016-05-11 11:55:36 -0700194 this->context = gGrFactory->getContextInfo(this->config.ctxType,
195 this->config.ctxOptions).testContext();
tomhudsond968a6f2015-03-26 11:28:06 -0700196 if (!this->surface.get()) {
197 return false;
198 }
bsalomonc8699322016-05-11 11:55:36 -0700199 if (!this->context->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700200 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800201 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700202 }
tomhudsond968a6f2015-03-26 11:28:06 -0700203 return true;
204 }
205 void fillOptions(ResultsWriter* log) override {
206 const GrGLubyte* version;
bsalomonc8699322016-05-11 11:55:36 -0700207 if (this->context->backend() == kOpenGL_GrBackend) {
208 const GrGLInterface* gl =
209 reinterpret_cast<const GrGLInterface*>(this->context->backendContext());
210 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
211 log->configOption("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700212
bsalomonc8699322016-05-11 11:55:36 -0700213 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
214 log->configOption("GL_RENDERER", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700215
bsalomonc8699322016-05-11 11:55:36 -0700216 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VENDOR));
217 log->configOption("GL_VENDOR", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700218
bsalomonc8699322016-05-11 11:55:36 -0700219 GR_GL_CALL_RET(gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
220 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
221 }
tomhudsond968a6f2015-03-26 11:28:06 -0700222 }
223};
mtkleind75c4662015-04-30 07:11:22 -0700224
tomhudsond968a6f2015-03-26 11:28:06 -0700225#endif
226
tomhudson75a0ebb2015-03-27 12:11:44 -0700227static double time(int loops, Benchmark* bench, Target* target) {
228 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700229 if (canvas) {
230 canvas->clear(SK_ColorWHITE);
231 }
joshualitt8a6697a2015-09-30 12:11:07 -0700232 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700233 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700234 canvas = target->beginTiming(canvas);
235 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700236 if (canvas) {
237 canvas->flush();
238 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700239 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700240 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700241 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700242 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700243}
244
mtkleinf3723212014-06-25 14:08:00 -0700245static double estimate_timer_overhead() {
246 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700247 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700248 double start = now_ms();
249 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700250 }
251 return overhead / FLAGS_overheadLoops;
252}
253
reed53249782014-10-10 09:09:52 -0700254static int detect_forever_loops(int loops) {
255 // look for a magic run-forever value
256 if (loops < 0) {
257 loops = SK_MaxS32;
258 }
259 return loops;
260}
261
mtklein55b0ffc2014-07-17 08:38:23 -0700262static int clamp_loops(int loops) {
263 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800264 SkDebugf("ERROR: clamping loops from %d to 1. "
265 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700266 return 1;
267 }
268 if (loops > FLAGS_maxLoops) {
269 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
270 return FLAGS_maxLoops;
271 }
272 return loops;
273}
274
tomhudsond968a6f2015-03-26 11:28:06 -0700275static bool write_canvas_png(Target* target, const SkString& filename) {
276
bsalomon6eb03cc2014-08-07 14:28:50 -0700277 if (filename.isEmpty()) {
278 return false;
279 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700280 if (target->getCanvas() &&
281 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700282 return false;
283 }
tomhudsond968a6f2015-03-26 11:28:06 -0700284
bsalomon6eb03cc2014-08-07 14:28:50 -0700285 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700286
287 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700288 return false;
289 }
tomhudsond968a6f2015-03-26 11:28:06 -0700290
bsalomon6eb03cc2014-08-07 14:28:50 -0700291 SkString dir = SkOSPath::Dirname(filename.c_str());
292 if (!sk_mkdir(dir.c_str())) {
293 SkDebugf("Can't make dir %s.\n", dir.c_str());
294 return false;
295 }
296 SkFILEWStream stream(filename.c_str());
297 if (!stream.isValid()) {
298 SkDebugf("Can't write %s.\n", filename.c_str());
299 return false;
300 }
Hal Canarydb683012016-11-23 08:55:18 -0700301 if (!SkEncodeImage(&stream, bmp, SkEncodedImageFormat::kPNG, 100)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700302 SkDebugf("Can't encode a PNG.\n");
303 return false;
304 }
305 return true;
306}
307
308static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700309static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700310 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700311 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700312 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700313 int loops = bench->calculateLoops(FLAGS_loops);
314 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700315 while (bench_plus_overhead < overhead) {
316 if (round++ == FLAGS_maxCalibrationAttempts) {
317 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700318 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700319 return kFailedLoops;
320 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700321 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700322 }
mtklein2069e222014-08-04 13:57:39 -0700323 }
mtkleinf3723212014-06-25 14:08:00 -0700324
mtkleinbb6a0282014-07-01 08:43:42 -0700325 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700326 // We'll pick N to make timer overhead negligible:
327 //
mtkleinbb6a0282014-07-01 08:43:42 -0700328 // overhead
329 // ------------------------- < FLAGS_overheadGoal
330 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700331 //
Hal Canary55325b72017-01-03 10:36:17 -0500332 // where bench_plus_overhead ~=~ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700333 //
334 // Doing some math, we get:
335 //
mtkleinbb6a0282014-07-01 08:43:42 -0700336 // (overhead / FLAGS_overheadGoal) - overhead
337 // ------------------------------------------ < N
338 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700339 //
340 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700341 if (kAutoTuneLoops == loops) {
342 const double numer = overhead / FLAGS_overheadGoal - overhead;
343 const double denom = bench_plus_overhead - overhead;
344 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700345 loops = clamp_loops(loops);
346 } else {
347 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700348 }
mtkleinbb6a0282014-07-01 08:43:42 -0700349
mtkleinbb6a0282014-07-01 08:43:42 -0700350 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700351}
352
cdaltone1b89582015-06-25 19:17:08 -0700353static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700354 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700355 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700356 if (kAutoTuneLoops == loops) {
357 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700358 double elapsed = 0;
359 do {
mtklein527930f2014-11-06 08:04:34 -0800360 if (1<<30 == loops) {
361 // We're about to wrap. Something's wrong with the bench.
362 loops = 0;
363 break;
364 }
mtkleina189ccd2014-07-14 12:28:47 -0700365 loops *= 2;
366 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700367 // _this_ round, not still timing last round.
368 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700369 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700370 }
371 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700372
mtkleina189ccd2014-07-14 12:28:47 -0700373 // We've overshot at least a little. Scale back linearly.
374 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700375 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700376
tomhudsond968a6f2015-03-26 11:28:06 -0700377 // Make sure we're not still timing our calibration.
378 target->fence();
reed53249782014-10-10 09:09:52 -0700379 } else {
380 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700381 }
mtkleinbb6a0282014-07-01 08:43:42 -0700382
383 // Pretty much the same deal as the calibration: do some warmup to make
384 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700385 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700386 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700387 }
mtkleinbb6a0282014-07-01 08:43:42 -0700388
mtkleinbb6a0282014-07-01 08:43:42 -0700389 return loops;
390}
mtkleinbb6a0282014-07-01 08:43:42 -0700391
bsalomonc2553372014-07-22 13:09:05 -0700392#if SK_SUPPORT_GPU
bsalomon85b4b532016-04-05 11:06:27 -0700393#define kBogusContextType GrContextFactory::kNativeGL_ContextType
csmartdalton6270e552016-09-13 10:41:49 -0700394#define kBogusContextOptions GrContextFactory::ContextOptions::kNone
bsalomonc2553372014-07-22 13:09:05 -0700395#else
bsalomon85b4b532016-04-05 11:06:27 -0700396#define kBogusContextType 0
397#define kBogusContextOptions 0
mtkleine714e752014-07-31 12:13:48 -0700398#endif
bsalomonc2553372014-07-22 13:09:05 -0700399
svaisanenc47635e2016-01-28 06:05:43 -0800400static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
401
402#if SK_SUPPORT_GPU
403 if (const auto* gpuConfig = config->asConfigGpu()) {
404 if (!FLAGS_gpu)
405 return;
406
svaisanenc47635e2016-01-28 06:05:43 -0800407 const auto ctxType = gpuConfig->getContextType();
csmartdalton6270e552016-09-13 10:41:49 -0700408 const auto ctxOptions = gpuConfig->getContextOptions();
svaisanenc47635e2016-01-28 06:05:43 -0800409 const auto sampleCount = gpuConfig->getSamples();
410
411 if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOptions)) {
412 const auto maxSampleCount = ctx->caps()->maxSampleCount();
413 if (sampleCount > ctx->caps()->maxSampleCount()) {
414 SkDebugf("Configuration sample count %d exceeds maximum %d.\n",
415 sampleCount, maxSampleCount);
416 return;
417 }
418 } else {
419 SkDebugf("No context was available matching config type and options.\n");
420 return;
421 }
422
423 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800424 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800425 Benchmark::kGPU_Backend,
brianosman1a48e132016-06-20 09:41:41 -0700426 gpuConfig->getColorType(),
svaisanenc47635e2016-01-28 06:05:43 -0800427 kPremul_SkAlphaType,
brianosman1a48e132016-06-20 09:41:41 -0700428 sk_ref_sp(gpuConfig->getColorSpace()),
svaisanenc47635e2016-01-28 06:05:43 -0800429 sampleCount,
430 ctxType,
kkinnunend4e1c352016-03-01 23:41:26 -0800431 ctxOptions,
432 gpuConfig->getUseDIText()
433 };
svaisanenc47635e2016-01-28 06:05:43 -0800434
435 configs->push_back(target);
436 return;
437 }
438#endif
439
brianosmanb109b8c2016-06-16 13:03:24 -0700440 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
441 if (config->getTag().equals(#name)) { \
442 Config config = { \
443 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
444 0, kBogusContextType, kBogusContextOptions, false \
445 }; \
446 configs->push_back(config); \
447 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700448 }
mtkleine714e752014-07-31 12:13:48 -0700449
mtklein40b32be2014-07-09 08:46:49 -0700450 if (FLAGS_cpu) {
mtkleinbb6c41b2016-03-08 11:31:11 -0800451 CPU_CONFIG(nonrendering, kNonRendering_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700452 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800453
454 CPU_CONFIG(8888, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700455 kN32_SkColorType, kPremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800456 CPU_CONFIG(565, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700457 kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
Brian Osman526972e2016-10-24 09:24:02 -0400458 auto srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
mtkleinbb6c41b2016-03-08 11:31:11 -0800459 CPU_CONFIG(srgb, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700460 kN32_SkColorType, kPremul_SkAlphaType, srgbColorSpace)
Brian Osman526972e2016-10-24 09:24:02 -0400461 auto srgbLinearColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named);
mtkleinbb6c41b2016-03-08 11:31:11 -0800462 CPU_CONFIG(f16, kRaster_Backend,
raftias7c602de2016-10-13 10:45:44 -0700463 kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbLinearColorSpace)
mtklein40b32be2014-07-09 08:46:49 -0700464 }
mtkleinbb6a0282014-07-01 08:43:42 -0700465
svaisanenc47635e2016-01-28 06:05:43 -0800466 #undef CPU_CONFIG
tomhudsond968a6f2015-03-26 11:28:06 -0700467
468#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
djsollen817c72a2016-01-28 13:57:02 -0800469 if (config->getTag().equals("hwui")) {
mtkleinbb6c41b2016-03-08 11:31:11 -0800470 Config config = { SkString("hwui"), Benchmark::kHWUI_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700471 kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr,
bsalomon85b4b532016-04-05 11:06:27 -0700472 0, kBogusContextType, kBogusContextOptions, false };
svaisanenc47635e2016-01-28 06:05:43 -0800473 configs->push_back(config);
tomhudsond968a6f2015-03-26 11:28:06 -0700474 }
475#endif
mtkleinf3723212014-06-25 14:08:00 -0700476}
477
svaisanenc47635e2016-01-28 06:05:43 -0800478// Append all configs that are enabled and supported.
479void create_configs(SkTArray<Config>* configs) {
480 SkCommandLineConfigArray array;
481 ParseConfigs(FLAGS_config, &array);
482 for (int i = 0; i < array.count(); ++i) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400483 create_config(array[i].get(), configs);
svaisanenc47635e2016-01-28 06:05:43 -0800484 }
485}
486
brianosman9f1f6e22016-09-15 08:33:02 -0700487// disable warning : switch statement contains default but no 'case' labels
488#if defined _WIN32
489#pragma warning ( push )
490#pragma warning ( disable : 4065 )
491#endif
492
halcanary96fcdcc2015-08-27 07:41:13 -0700493// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700494static Target* is_enabled(Benchmark* bench, const Config& config) {
495 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700496 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700497 }
498
reede5ea5002014-09-03 11:54:58 -0700499 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700500 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700501
halcanary96fcdcc2015-08-27 07:41:13 -0700502 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700503
tomhudsond968a6f2015-03-26 11:28:06 -0700504 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700505#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700506 case Benchmark::kGPU_Backend:
507 target = new GPUTarget(config);
508 break;
bsalomonc2553372014-07-22 13:09:05 -0700509#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700510#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
511 case Benchmark::kHWUI_Backend:
512 target = new HWUITarget(config, bench);
513 break;
514#endif
515 default:
516 target = new Target(config);
517 break;
518 }
bsalomonc2553372014-07-22 13:09:05 -0700519
tomhudsond968a6f2015-03-26 11:28:06 -0700520 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700521 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700522 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700523 }
524 return target;
525}
526
brianosman9f1f6e22016-09-15 08:33:02 -0700527#if defined _WIN32
528#pragma warning ( pop )
529#endif
530
reed42943c82016-09-12 12:01:44 -0700531static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700532 uint32_t minOutputSize, int* width, int* height) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400533 std::unique_ptr<SkBitmapRegionDecoder> brd(
msarettd1227a72016-05-18 06:23:57 -0700534 SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy));
msarett7f691442015-09-22 11:56:16 -0700535 if (nullptr == brd.get()) {
536 // This is indicates that subset decoding is not supported for a particular image format.
537 return false;
538 }
539
msarett7f691442015-09-22 11:56:16 -0700540 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
541 (uint32_t) brd->height()) {
542 // This indicates that the image is not large enough to decode a
543 // minOutputSize x minOutputSize subset at the given sampleSize.
544 return false;
545 }
546
547 // Set the image width and height. The calling code will use this to choose subsets to decode.
548 *width = brd->width();
549 *height = brd->height();
550 return true;
551}
552
egdaniel3bf92062015-06-26 08:12:46 -0700553static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700554 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700555#if SK_SUPPORT_GPU
556 if (FLAGS_abandonGpuContext) {
557 gGrFactory->abandonContexts();
558 }
559 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
560 gGrFactory->destroyContexts();
561 }
562#endif
563}
564
fmalita6519c212016-09-14 08:05:17 -0700565static void collect_files(const SkCommandLineFlags::StringArray& paths, const char* ext,
566 SkTArray<SkString>* list) {
567 for (int i = 0; i < paths.count(); ++i) {
568 if (SkStrEndsWith(paths[i], ext)) {
569 list->push_back(SkString(paths[i]));
570 } else {
571 SkOSFile::Iter it(paths[i], ext);
572 SkString path;
573 while (it.next(&path)) {
574 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
575 }
576 }
577 }
578}
579
mtkleine714e752014-07-31 12:13:48 -0700580class BenchmarkStream {
581public:
mtklein92007582014-08-01 07:46:52 -0700582 BenchmarkStream() : fBenches(BenchRegistry::Head())
583 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700584 , fCurrentRecording(0)
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400585 , fCurrentPiping(0)
mtklein92007582014-08-01 07:46:52 -0700586 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800587 , fCurrentSKP(0)
fmalita6519c212016-09-14 08:05:17 -0700588 , fCurrentSVG(0)
msarett95f192d2015-02-13 09:05:41 -0800589 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700590 , fCurrentCodec(0)
msarett84451022016-02-11 06:45:51 -0800591 , fCurrentAndroidCodec(0)
msarett7f691442015-09-22 11:56:16 -0700592 , fCurrentBRDImage(0)
msarett2cee9022016-06-03 08:25:21 -0700593 , fCurrentColorImage(0)
msarett95f192d2015-02-13 09:05:41 -0800594 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800595 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700596 , fCurrentSubsetType(0)
msarett84451022016-02-11 06:45:51 -0800597 , fCurrentSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700598 , fCurrentAnimSKP(0) {
fmalita6519c212016-09-14 08:05:17 -0700599 collect_files(FLAGS_skps, ".skp", &fSKPs);
600 collect_files(FLAGS_svgs, ".svg", &fSVGs);
mtkleine714e752014-07-31 12:13:48 -0700601
mtklein92007582014-08-01 07:46:52 -0700602 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
603 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
604 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
605 exit(1);
606 }
607
608 for (int i = 0; i < FLAGS_scales.count(); i++) {
609 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
610 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
611 exit(1);
612 }
613 }
robertphillips5b693772014-11-21 06:19:36 -0800614
cdalton63a82852015-06-29 14:06:10 -0700615 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
616 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700617 exit(1);
618 }
619
robertphillips5b693772014-11-21 06:19:36 -0800620 if (FLAGS_mpd) {
621 fUseMPDs.push_back() = true;
622 }
mtkleinc751ecb2015-06-15 08:56:38 -0700623 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700624
msarett95f192d2015-02-13 09:05:41 -0800625 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700626 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800627 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800628 }
msarett2cee9022016-06-03 08:25:21 -0700629 if (!CollectImages(FLAGS_colorImages, &fColorImages)) {
630 exit(1);
631 }
mtklein95553d92015-03-12 08:24:21 -0700632
msarett95f192d2015-02-13 09:05:41 -0800633 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700634 fColorTypes.push_back(kN32_SkColorType);
635 if (!FLAGS_simpleCodec) {
636 fColorTypes.push_back(kRGB_565_SkColorType);
637 fColorTypes.push_back(kAlpha_8_SkColorType);
638 fColorTypes.push_back(kIndex_8_SkColorType);
639 fColorTypes.push_back(kGray_8_SkColorType);
640 }
mtklein92007582014-08-01 07:46:52 -0700641 }
642
reedca2622b2016-03-18 07:25:55 -0700643 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700644 // Not strictly necessary, as it will be checked again later,
645 // but helps to avoid a lot of pointless work if we're going to skip it.
cdalton91e457d2016-02-17 11:10:16 -0800646 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700647 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700648 }
649
bungemanf93d7112016-09-16 06:24:20 -0700650 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
651 if (!stream) {
mtkleinfd731ce2014-09-10 12:19:30 -0700652 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700653 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700654 }
655
reedca2622b2016-03-18 07:25:55 -0700656 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700657 }
658
fmalita6519c212016-09-14 08:05:17 -0700659 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
660 SkFILEStream stream(path);
661 if (!stream.isValid()) {
662 SkDebugf("Could not read %s.\n", path);
663 return nullptr;
664 }
665
fmalitae1baa7c2016-09-14 12:04:30 -0700666 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
fmalita6519c212016-09-14 08:05:17 -0700667 if (!svgDom) {
668 SkDebugf("Could not parse %s.\n", path);
669 return nullptr;
670 }
671
fmalitae1baa7c2016-09-14 12:04:30 -0700672 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
673 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
674 if (svgDom->containerSize().isEmpty()) {
675 svgDom->setContainerSize(kDefaultContainerSize);
676 }
677
fmalita6519c212016-09-14 08:05:17 -0700678 SkPictureRecorder recorder;
fmalitae1baa7c2016-09-14 12:04:30 -0700679 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
680 svgDom->containerSize().height()));
fmalita6519c212016-09-14 08:05:17 -0700681 return recorder.finishRecordingAsPicture();
682 }
683
mtklein92007582014-08-01 07:46:52 -0700684 Benchmark* next() {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400685 std::unique_ptr<Benchmark> bench;
mtklein65dfd2f2016-02-03 10:40:54 -0800686 do {
687 bench.reset(this->rawNext());
688 if (!bench) {
689 return nullptr;
690 }
691 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
692 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700693 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800694 }
695
696 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700697 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700698 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700699 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700700 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700701 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700702 return bench;
703 }
mtklein92007582014-08-01 07:46:52 -0700704
mtkleine714e752014-07-31 12:13:48 -0700705 while (fGMs) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400706 std::unique_ptr<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700707 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800708 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700709 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700710 fBenchType = "micro";
mtklein18300a32016-03-16 13:53:35 -0700711 return new GMBench(gm.release());
mtkleine714e752014-07-31 12:13:48 -0700712 }
713 }
mtklein92007582014-08-01 07:46:52 -0700714
mtkleinfd731ce2014-09-10 12:19:30 -0700715 // First add all .skps as RecordingBenches.
716 while (fCurrentRecording < fSKPs.count()) {
717 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700718 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
719 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700720 continue;
721 }
722 SkString name = SkOSPath::Basename(path.c_str());
723 fSourceType = "skp";
724 fBenchType = "recording";
reedca2622b2016-03-18 07:25:55 -0700725 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic.get()));
mtklein051e56d2014-12-04 08:46:51 -0800726 fSKPOps = pic->approximateOpCount();
mtklein8c1a4f82016-08-08 06:56:22 -0700727 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh, FLAGS_lite);
mtkleinfd731ce2014-09-10 12:19:30 -0700728 }
729
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400730 // Add all .skps as PipeBenches.
731 while (fCurrentPiping < fSKPs.count()) {
732 const SkString& path = fSKPs[fCurrentPiping++];
733 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
734 if (!pic) {
735 continue;
736 }
737 SkString name = SkOSPath::Basename(path.c_str());
738 fSourceType = "skp";
739 fBenchType = "piping";
740 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic.get()));
741 fSKPOps = pic->approximateOpCount();
742 return new PipingBench(name.c_str(), pic.get());
743 }
744
mtkleinfd731ce2014-09-10 12:19:30 -0700745 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700746 while (fCurrentScale < fScales.count()) {
747 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800748 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700749 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
750 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800751 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700752 continue;
753 }
robertphillips5b693772014-11-21 06:19:36 -0800754
755 while (fCurrentUseMPD < fUseMPDs.count()) {
756 if (FLAGS_bbh) {
757 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
758 SkRTreeFactory factory;
759 SkPictureRecorder recorder;
robertphillips5b693772014-11-21 06:19:36 -0800760 pic->playback(recorder.beginRecording(pic->cullRect().width(),
761 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800762 &factory,
robertphillipsdda54452016-07-13 13:27:16 -0700763 0));
reedca2622b2016-03-18 07:25:55 -0700764 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800765 }
766 SkString name = SkOSPath::Basename(path.c_str());
767 fSourceType = "skp";
768 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700769 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
770 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700771 }
robertphillips5b693772014-11-21 06:19:36 -0800772 fCurrentUseMPD = 0;
773 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700774 }
fmalita6519c212016-09-14 08:05:17 -0700775
776 while (fCurrentSVG++ < fSVGs.count()) {
777 const char* path = fSVGs[fCurrentSVG - 1].c_str();
778 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
779 fSourceType = "svg";
780 fBenchType = "playback";
781 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
782 fScales[fCurrentScale], false, FLAGS_loopSKP);
783 }
784 }
785
mtklein92007582014-08-01 07:46:52 -0700786 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700787 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700788 fCurrentScale++;
789 }
790
joshualitt261c3ad2015-04-27 09:16:57 -0700791 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700792 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700793 while (fCurrentAnimSKP < fSKPs.count()) {
794 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700795 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
796 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700797 fCurrentAnimSKP++;
798 continue;
799 }
800
801 fCurrentAnimSKP++;
802 SkString name = SkOSPath::Basename(path.c_str());
Hal Canary2db83612016-11-04 13:02:54 -0400803 sk_sp<SKPAnimationBench::Animation> animation(
cdalton63a82852015-06-29 14:06:10 -0700804 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
Hal Canary2db83612016-11-04 13:02:54 -0400805 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation.get(),
halcanary385fe4d2015-08-26 13:07:48 -0700806 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700807 }
808 }
809
scroggo60869a42015-04-01 12:09:17 -0700810 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700811 fSourceType = "image";
812 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700813 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800814 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
815 continue;
816 }
bungeman38d909e2016-08-02 14:40:46 -0700817 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Ben Wagner145dbcd2016-11-03 14:40:50 -0400818 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700819 if (!codec) {
820 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700821 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700822 continue;
823 }
scroggo21027992015-04-02 13:22:38 -0700824
scroggo60869a42015-04-01 12:09:17 -0700825 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700826 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700827
msarettc7796b92016-01-07 14:20:20 -0800828 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700829 if (FLAGS_simpleCodec) {
830 if (kUnpremul_SkAlphaType == alphaType) {
831 alphaType = kPremul_SkAlphaType;
832 }
833
834 fCurrentColorType++;
835 } else {
836 switch (alphaType) {
837 case kOpaque_SkAlphaType:
838 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800839 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700840 break;
841 case kUnpremul_SkAlphaType:
842 case kPremul_SkAlphaType:
843 if (0 == fCurrentAlphaType) {
844 // Test unpremul first.
845 alphaType = kUnpremul_SkAlphaType;
846 fCurrentAlphaType++;
847 } else {
848 // Test premul.
849 alphaType = kPremul_SkAlphaType;
850 fCurrentAlphaType = 0;
851 fCurrentColorType++;
852 }
853 break;
854 default:
855 SkASSERT(false);
856 fCurrentColorType++;
857 break;
858 }
scroggo21027992015-04-02 13:22:38 -0700859 }
860
msarettc7796b92016-01-07 14:20:20 -0800861 // Make sure we can decode to this color type and alpha type.
862 SkImageInfo info =
863 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700864 const size_t rowBytes = info.minRowBytes();
865 SkAutoMalloc storage(info.getSafeSize(rowBytes));
866
867 // Used if fCurrentColorType is kIndex_8_SkColorType
868 int colorCount = 256;
869 SkPMColor colors[256];
870
scroggoeb602a52015-07-09 08:16:03 -0700871 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700872 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700873 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700874 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700875 case SkCodec::kSuccess:
876 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700877 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700878 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700879 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700880 // This is okay. Not all conversions are valid.
881 break;
scroggo60869a42015-04-01 12:09:17 -0700882 default:
883 // This represents some sort of failure.
884 SkASSERT(false);
885 break;
886 }
887 }
888 fCurrentColorType = 0;
889 }
890
msarett84451022016-02-11 06:45:51 -0800891 // Run AndroidCodecBenches
892 const int sampleSizes[] = { 2, 4, 8 };
893 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
894 fSourceType = "image";
895 fBenchType = "skandroidcodec";
896
897 const SkString& path = fImages[fCurrentAndroidCodec];
898 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
899 continue;
900 }
bungeman38d909e2016-08-02 14:40:46 -0700901 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Ben Wagner145dbcd2016-11-03 14:40:50 -0400902 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800903 if (!codec) {
904 // Nothing to time.
905 SkDebugf("Cannot find codec for %s\n", path.c_str());
906 continue;
907 }
908
909 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
910 int sampleSize = sampleSizes[fCurrentSampleSize];
911 fCurrentSampleSize++;
912 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
913 // Avoid benchmarking scaled decodes of already small images.
914 break;
915 }
916
bungeman38d909e2016-08-02 14:40:46 -0700917 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
918 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -0800919 }
920 fCurrentSampleSize = 0;
921 }
922
msarett7f691442015-09-22 11:56:16 -0700923 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700924 // We intend to create benchmarks that model the use cases in
925 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
926 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
927 // the image. For that reason, we will benchmark decodes in five representative locations
928 // in the image. Additionally, this use case utilizes power of two scaling, so we will
929 // test on power of two sample sizes. The output tile is always 512x512, so, when a
930 // sampleSize is used, the size of the subset that is decoded is always
931 // (sampleSize*512)x(sampleSize*512).
932 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700933 // All use cases we are aware of only scale by powers of two.
934 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
935 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800936 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700937 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800938 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700939 fSourceType = "image";
940 fBenchType = "BRD";
941
mtklein6f0ff912016-01-11 09:04:21 -0800942 const SkString& path = fImages[fCurrentBRDImage];
943 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
944 continue;
945 }
scroggo860e8a62015-10-15 07:51:28 -0700946
msarettd1227a72016-05-18 06:23:57 -0700947 while (fCurrentColorType < fColorTypes.count()) {
948 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
949 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700950
bungeman38d909e2016-08-02 14:40:46 -0700951 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -0700952 const SkColorType colorType = fColorTypes[fCurrentColorType];
953 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
954 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -0700955
msarettd1227a72016-05-18 06:23:57 -0700956 int width = 0;
957 int height = 0;
reed42943c82016-09-12 12:01:44 -0700958 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -0700959 &width, &height)) {
960 break;
msarett7f691442015-09-22 11:56:16 -0700961 }
msarettd1227a72016-05-18 06:23:57 -0700962
963 SkString basename = SkOSPath::Basename(path.c_str());
964 SkIRect subset;
965 const uint32_t subsetSize = sampleSize * minOutputSize;
966 switch (currentSubsetType) {
967 case kTopLeft_SubsetType:
968 basename.append("_TopLeft");
969 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
970 break;
971 case kTopRight_SubsetType:
972 basename.append("_TopRight");
973 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
974 subsetSize);
975 break;
976 case kMiddle_SubsetType:
977 basename.append("_Middle");
978 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
979 (height - subsetSize) / 2, subsetSize, subsetSize);
980 break;
981 case kBottomLeft_SubsetType:
982 basename.append("_BottomLeft");
983 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
984 subsetSize);
985 break;
986 case kBottomRight_SubsetType:
987 basename.append("_BottomRight");
988 subset = SkIRect::MakeXYWH(width - subsetSize,
989 height - subsetSize, subsetSize, subsetSize);
990 break;
991 default:
992 SkASSERT(false);
993 }
994
995 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
996 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -0700997 }
msarettd1227a72016-05-18 06:23:57 -0700998 fCurrentSubsetType = 0;
999 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -07001000 }
msarettd1227a72016-05-18 06:23:57 -07001001 fCurrentSampleSize = 0;
1002 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -07001003 }
msarettd1227a72016-05-18 06:23:57 -07001004 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -07001005 }
1006
msarett2cee9022016-06-03 08:25:21 -07001007 while (fCurrentColorImage < fColorImages.count()) {
1008 fSourceType = "colorimage";
1009 fBenchType = "skcolorcodec";
1010 const SkString& path = fColorImages[fCurrentColorImage];
1011 fCurrentColorImage++;
1012 sk_sp<SkData> encoded = SkData::MakeFromFileName(path.c_str());
1013 if (encoded) {
1014 return new ColorCodecBench(SkOSPath::Basename(path.c_str()).c_str(),
1015 std::move(encoded));
1016 } else {
1017 SkDebugf("Could not read file %s.\n", path.c_str());
1018 }
1019 }
1020
halcanary96fcdcc2015-08-27 07:41:13 -07001021 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001022 }
mtklein92007582014-08-01 07:46:52 -07001023
1024 void fillCurrentOptions(ResultsWriter* log) const {
1025 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001026 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001027 if (0 == strcmp(fSourceType, "skp")) {
1028 log->configOption("clip",
1029 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1030 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001031 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -07001032 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001033 if (fCurrentUseMPD > 0) {
1034 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1035 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1036 }
mtklein92007582014-08-01 07:46:52 -07001037 }
mtklein051e56d2014-12-04 08:46:51 -08001038 if (0 == strcmp(fBenchType, "recording")) {
1039 log->metric("bytes", fSKPBytes);
1040 log->metric("ops", fSKPOps);
1041 }
mtklein92007582014-08-01 07:46:52 -07001042 }
1043
mtkleine714e752014-07-31 12:13:48 -07001044private:
msarettb23e6aa2015-06-09 13:56:10 -07001045 enum SubsetType {
1046 kTopLeft_SubsetType = 0,
1047 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001048 kMiddle_SubsetType = 2,
1049 kBottomLeft_SubsetType = 3,
1050 kBottomRight_SubsetType = 4,
1051 kTranslate_SubsetType = 5,
1052 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001053 kLast_SubsetType = kZoom_SubsetType,
1054 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001055 };
1056
mtkleine714e752014-07-31 12:13:48 -07001057 const BenchRegistry* fBenches;
1058 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001059 SkIRect fClip;
1060 SkTArray<SkScalar> fScales;
1061 SkTArray<SkString> fSKPs;
fmalita6519c212016-09-14 08:05:17 -07001062 SkTArray<SkString> fSVGs;
robertphillips5b693772014-11-21 06:19:36 -08001063 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001064 SkTArray<SkString> fImages;
msarett2cee9022016-06-03 08:25:21 -07001065 SkTArray<SkString> fColorImages;
msarett74deb982015-10-20 16:45:56 -07001066 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001067 SkScalar fZoomMax;
1068 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001069
mtklein051e56d2014-12-04 08:46:51 -08001070 double fSKPBytes, fSKPOps;
1071
mtkleinfd731ce2014-09-10 12:19:30 -07001072 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1073 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1074 int fCurrentRecording;
Mike Reed9cdd2ab2016-10-21 10:43:36 -04001075 int fCurrentPiping;
mtklein92007582014-08-01 07:46:52 -07001076 int fCurrentScale;
1077 int fCurrentSKP;
fmalita6519c212016-09-14 08:05:17 -07001078 int fCurrentSVG;
robertphillips5b693772014-11-21 06:19:36 -08001079 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001080 int fCurrentCodec;
msarett84451022016-02-11 06:45:51 -08001081 int fCurrentAndroidCodec;
msarett7f691442015-09-22 11:56:16 -07001082 int fCurrentBRDImage;
msarett2cee9022016-06-03 08:25:21 -07001083 int fCurrentColorImage;
msarett95f192d2015-02-13 09:05:41 -08001084 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -08001085 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -07001086 int fCurrentSubsetType;
msarett84451022016-02-11 06:45:51 -08001087 int fCurrentSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001088 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001089};
1090
msarettc149f0e2016-01-04 11:35:43 -08001091// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1092// This prints something every once in a while so that it knows we're still working.
1093static void start_keepalive() {
1094 struct Loop {
1095 static void forever(void*) {
1096 for (;;) {
1097 static const int kSec = 1200;
1098 #if defined(SK_BUILD_FOR_WIN)
1099 Sleep(kSec * 1000);
1100 #else
1101 sleep(kSec);
1102 #endif
1103 SkDebugf("\nBenchmarks still running...\n");
1104 }
1105 }
1106 };
1107 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1108 intentionallyLeaked->start();
1109}
1110
jcgregorio3b27ade2014-11-13 08:06:40 -08001111int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001112int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001113 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001114 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001115 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001116
krajcevski69a55602014-08-13 10:46:31 -07001117#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001118 GrContextOptions grContextOpts;
halcanary385fe4d2015-08-26 13:07:48 -07001119 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001120#endif
1121
bsalomon06cddec2014-10-24 10:40:50 -07001122 if (FLAGS_veryVerbose) {
1123 FLAGS_verbose = true;
1124 }
1125
bsalomon6eb03cc2014-08-07 14:28:50 -07001126 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001127 FLAGS_samples = 1;
1128 FLAGS_gpuFrameLag = 0;
1129 }
1130
bsalomon6eb03cc2014-08-07 14:28:50 -07001131 if (!FLAGS_writePath.isEmpty()) {
1132 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1133 if (!sk_mkdir(FLAGS_writePath[0])) {
1134 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001135 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001136 }
1137 }
1138
Ben Wagner145dbcd2016-11-03 14:40:50 -04001139 std::unique_ptr<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001140 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001141#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001142 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001143#else
1144 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1145 return 1;
1146#endif
mtklein60317d0f2014-07-14 11:30:37 -07001147 }
mtklein1915b622014-08-20 11:45:00 -07001148
1149 if (1 == FLAGS_properties.count() % 2) {
1150 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1151 return 1;
1152 }
1153 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1154 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1155 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001156
1157 if (1 == FLAGS_key.count() % 2) {
1158 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1159 return 1;
1160 }
1161 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001162 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001163 }
mtklein60317d0f2014-07-14 11:30:37 -07001164
mtkleinf3723212014-06-25 14:08:00 -07001165 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001166 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001167
cdaltone1b89582015-06-25 19:17:08 -07001168 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001169
bsalomon6eb03cc2014-08-07 14:28:50 -07001170 if (kAutoTuneLoops != FLAGS_loops) {
1171 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001172 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001173 SkDebugf("! -> high variance, ? -> moderate variance\n");
1174 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001175 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001176 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001177 } else {
mtkleind75c4662015-04-30 07:11:22 -07001178 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001179 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001180 }
1181
svaisanenc47635e2016-01-28 06:05:43 -08001182 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001183 create_configs(&configs);
1184
joshualitt3ebd0502016-02-09 07:18:08 -08001185#ifdef THERMAL_MANAGER_SUPPORTED
1186 int tmEnabled, tmThreshold, tmSleepTimeMs, tmTimeoutMs;
1187 if (4 != sscanf(FLAGS_useThermalManager[0], "%d,%d,%d,%d",
1188 &tmEnabled, &tmThreshold, &tmSleepTimeMs, &tmTimeoutMs)) {
1189 SkDebugf("Can't parse %s from --useThermalManager.\n", FLAGS_useThermalManager[0]);
1190 exit(1);
1191 }
1192 ThermalManager tm(tmThreshold, tmSleepTimeMs, tmTimeoutMs);
1193#endif
1194
msarettc149f0e2016-01-04 11:35:43 -08001195 if (FLAGS_keepAlive) {
1196 start_keepalive();
1197 }
1198
Yuqian Liba62b4a2016-12-14 13:46:53 -05001199 gSkUseAnalyticAA = FLAGS_analyticAA;
liyuqian38911a72016-10-04 11:23:22 -07001200
Yuqian Li550148b2017-01-13 10:13:13 -05001201 if (FLAGS_forceAnalyticAA) {
1202 gSkForceAnalyticAA = true;
1203 }
1204
mtkleine070c2b2014-10-14 08:40:43 -07001205 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001206 BenchmarkStream benchStream;
1207 while (Benchmark* b = benchStream.next()) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001208 std::unique_ptr<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001209 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001210 continue;
1211 }
1212
svaisanenc47635e2016-01-28 06:05:43 -08001213 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001214 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001215 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001216 }
egdaniel3bf92062015-06-26 08:12:46 -07001217 for (int i = 0; i < configs.count(); ++i) {
joshualitt3ebd0502016-02-09 07:18:08 -08001218#ifdef THERMAL_MANAGER_SUPPORTED
1219 if (tmEnabled && !tm.coolOffIfNecessary()) {
1220 SkDebugf("Could not cool off, timings will be throttled\n");
1221 }
1222#endif
egdaniel3bf92062015-06-26 08:12:46 -07001223 Target* target = is_enabled(b, configs[i]);
1224 if (!target) {
1225 continue;
1226 }
mtkleinf3723212014-06-25 14:08:00 -07001227
halcanary96fcdcc2015-08-27 07:41:13 -07001228 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001229 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001230 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001231
brianosman7a5ada82016-02-08 13:49:12 -08001232 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001233 SkDebugf("Running %s\t%s\n"
1234 , bench->getUniqueName()
1235 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001236 if (FLAGS_dryRun) {
1237 continue;
1238 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001239 }
1240
egdaniel3bf92062015-06-26 08:12:46 -07001241 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001242 bench->perCanvasPreDraw(canvas);
1243
cdaltone1b89582015-06-25 19:17:08 -07001244 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001245 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001246 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1247 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001248
mtkleinbbba1682015-10-28 11:36:30 -07001249 if (FLAGS_ms) {
1250 samples.reset();
1251 auto stop = now_ms() + FLAGS_ms;
1252 do {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001253 samples.push_back(time(loops, bench.get(), target) / loops);
mtkleinbbba1682015-10-28 11:36:30 -07001254 } while (now_ms() < stop);
1255 } else {
cdaltone1b89582015-06-25 19:17:08 -07001256 samples.reset(FLAGS_samples);
1257 for (int s = 0; s < FLAGS_samples; s++) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001258 samples[s] = time(loops, bench.get(), target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001259 }
cdaltone1b89582015-06-25 19:17:08 -07001260 }
mtkleinf3723212014-06-25 14:08:00 -07001261
joshualitte45c81c2015-12-02 09:05:37 -08001262#if SK_SUPPORT_GPU
1263 SkTArray<SkString> keys;
1264 SkTArray<double> values;
1265 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1266 if (gpuStatsDump) {
1267 // TODO cache stats
1268 bench->getGpuStats(canvas, &keys, &values);
1269 }
1270#endif
1271
robertphillips5b693772014-11-21 06:19:36 -08001272 bench->perCanvasPostDraw(canvas);
1273
egdaniel3bf92062015-06-26 08:12:46 -07001274 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001275 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001276 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001277 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001278 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001279 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001280 }
1281
1282 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001283 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001284 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001285 continue;
1286 }
1287
cdaltone1b89582015-06-25 19:17:08 -07001288 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001289 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001290 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001291 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001292 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001293 log->metric("min_ms", stats.min);
George Burgess IV75b57182016-12-06 10:53:52 -08001294 log->metrics("samples", samples);
joshualitte45c81c2015-12-02 09:05:37 -08001295#if SK_SUPPORT_GPU
1296 if (gpuStatsDump) {
1297 // dump to json, only SKPBench currently returns valid keys / values
1298 SkASSERT(keys.count() == values.count());
1299 for (int i = 0; i < keys.count(); i++) {
1300 log->metric(keys[i].c_str(), values[i]);
1301 }
1302 }
1303#endif
1304
mtkleine070c2b2014-10-14 08:40:43 -07001305 if (runs++ % FLAGS_flushEvery == 0) {
1306 log->flush();
1307 }
mtklein60317d0f2014-07-14 11:30:37 -07001308
bsalomon6eb03cc2014-08-07 14:28:50 -07001309 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001310 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001311 config = ""; // Only print the config if we run the same bench on more than one.
1312 }
mtkleind75c4662015-04-30 07:11:22 -07001313 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1314 , sk_tools::getCurrResidentSetSizeMB()
1315 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001316 , bench->getUniqueName()
1317 , config);
mtkleinf3723212014-06-25 14:08:00 -07001318 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001319 const char* mark = " ";
1320 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1321 if (stddev_percent > 5) mark = "?";
1322 if (stddev_percent > 10) mark = "!";
1323
1324 SkDebugf("%10.2f %s\t%s\t%s\n",
1325 stats.median*1e3, mark, bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001326 } else {
1327 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001328 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1329 , sk_tools::getCurrResidentSetSizeMB()
1330 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001331 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001332 , HUMANIZE(stats.min)
1333 , HUMANIZE(stats.median)
1334 , HUMANIZE(stats.mean)
1335 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001336 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001337 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001338 , config
mtklein96289052014-09-10 12:05:59 -07001339 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001340 );
1341 }
joshualitte45c81c2015-12-02 09:05:37 -08001342
bsalomonb12ea412015-02-02 21:19:50 -08001343#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001344 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001345 GrContext* context = gGrFactory->get(configs[i].ctxType,
kkinnunen3e980c32015-12-23 01:33:00 -08001346 configs[i].ctxOptions);
kkinnunen5219fd92015-12-10 06:28:13 -08001347 context->printCacheStats();
1348 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001349 }
1350#endif
joshualitte45c81c2015-12-02 09:05:37 -08001351
cdalton2c56ba52015-06-26 13:32:53 -07001352 if (FLAGS_verbose) {
1353 SkDebugf("Samples: ");
1354 for (int i = 0; i < samples.count(); i++) {
1355 SkDebugf("%s ", HUMANIZE(samples[i]));
1356 }
1357 SkDebugf("%s\n", bench->getUniqueName());
1358 }
egdaniel3bf92062015-06-26 08:12:46 -07001359 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001360 }
mtkleinf3723212014-06-25 14:08:00 -07001361 }
1362
mtkleine1091452014-12-04 10:47:02 -08001363 log->bench("memory_usage", 0,0);
1364 log->config("meta");
1365 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1366
joshualitte0b19d42015-03-26 10:41:02 -07001367#if SK_SUPPORT_GPU
1368 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1369 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001370 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001371#endif
1372
mtkleinf3723212014-06-25 14:08:00 -07001373 return 0;
1374}
1375
jcgregorio3b27ade2014-11-13 08:06:40 -08001376#if !defined SK_BUILD_FOR_IOS
1377int main(int argc, char** argv) {
1378 SkCommandLineFlags::Parse(argc, argv);
1379 return nanobench_main();
1380}
1381#endif