blob: bd23c3e39a6e63f8dfea26c6bec6b87cdba72bb5 [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"
mtkleinfd731ce2014-09-10 12:19:30 -070021#include "RecordingBench.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040022#include "ResultsWriter.h"
joshualitt261c3ad2015-04-27 09:16:57 -070023#include "SKPAnimationBench.h"
mtklein92007582014-08-01 07:46:52 -070024#include "SKPBench.h"
msarett84451022016-02-11 06:45:51 -080025#include "SkAndroidCodec.h"
Hal Canary95e3c052017-01-11 12:44:43 -050026#include "SkAutoMalloc.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000027#include "SkBBoxHierarchy.h"
Hal Canary95e3c052017-01-11 12:44:43 -050028#include "SkBitmapRegionDecoder.h"
mtkleinf3723212014-06-25 14:08:00 -070029#include "SkCanvas.h"
scroggo60869a42015-04-01 12:09:17 -070030#include "SkCodec.h"
caryclark17f0b6d2014-07-22 10:15:34 -070031#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080032#include "SkCommonFlagsConfig.h"
Brian Osmanf9810662017-08-30 10:02:10 -040033#include "SkCommonFlagsGpuThreads.h"
csmartdalton008b9d82017-02-22 12:00:42 -070034#include "SkCommonFlagsPathRenderer.h"
msarett95f192d2015-02-13 09:05:41 -080035#include "SkData.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040036#include "SkDebugfTracer.h"
Brian Osman53136aa2017-07-20 15:43:35 -040037#include "SkEventTracingPriv.h"
mtkleinf3723212014-06-25 14:08:00 -070038#include "SkGraphics.h"
halcanary4dbbd042016-06-07 17:21:10 -070039#include "SkLeanWindows.h"
mtklein20840502014-08-21 15:51:22 -070040#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050041#include "SkOSPath.h"
mtklein20840502014-08-21 15:51:22 -070042#include "SkPictureRecorder.h"
Hal Canary95e3c052017-01-11 12:44:43 -050043#include "SkSVGDOM.h"
44#include "SkScan.h"
mtkleinf3723212014-06-25 14:08:00 -070045#include "SkString.h"
46#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080047#include "SkTaskGroup.h"
msarettc149f0e2016-01-04 11:35:43 -080048#include "SkThreadUtils.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040049#include "Stats.h"
joshualitt3ebd0502016-02-09 07:18:08 -080050#include "ThermalManager.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040051#include "ios_utils.h"
mtkleinf3723212014-06-25 14:08:00 -070052
bungeman60e0fee2015-08-26 05:15:46 -070053#include <stdlib.h>
54
Mike Reedc928fe22017-06-05 10:20:31 -040055extern bool gSkForceRasterPipelineBlitter;
56
scroggo38ce0a72016-01-07 07:28:47 -080057#ifndef SK_BUILD_FOR_WIN32
58 #include <unistd.h>
59#endif
60
mtkleinbb6a0282014-07-01 08:43:42 -070061#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070062 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070063 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070064 #include "GrContextFactory.h"
bsalomon3724e572016-03-30 18:56:19 -070065 #include "gl/GrGLUtil.h"
Greg Daniel81e7bf82017-07-19 14:47:42 -040066 #include "SkGr.h"
bsalomon3724e572016-03-30 18:56:19 -070067 using sk_gpu_test::GrContextFactory;
bsalomonc8699322016-05-11 11:55:36 -070068 using sk_gpu_test::TestContext;
Ben Wagner145dbcd2016-11-03 14:40:50 -040069 std::unique_ptr<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070070#endif
71
bsalomon682c2692015-05-22 14:01:46 -070072 struct GrContextOptions;
73
reed53249782014-10-10 09:09:52 -070074static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070075
Mike Kleind8ee67c2017-01-19 12:14:50 -050076#if !defined(__has_feature)
77 #define __has_feature(x) 0
78#endif
79
mtkleinb5110422014-08-07 15:20:02 -070080static const int kDefaultLoops =
Mike Kleind8ee67c2017-01-19 12:14:50 -050081#if defined(SK_DEBUG) || __has_feature(address_sanitizer)
bsalomon6eb03cc2014-08-07 14:28:50 -070082 1;
mtkleina189ccd2014-07-14 12:28:47 -070083#else
bsalomon6eb03cc2014-08-07 14:28:50 -070084 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070085#endif
86
bsalomon6eb03cc2014-08-07 14:28:50 -070087static SkString loops_help_txt() {
88 SkString help;
89 help.printf("Number of times to run each bench. Set this to %d to auto-"
90 "tune for each bench. Timings are only reported when auto-tuning.",
91 kAutoTuneLoops);
92 return help;
93}
94
cdaltone1b89582015-06-25 19:17:08 -070095static SkString to_string(int n) {
96 SkString str;
97 str.appendS32(n);
98 return str;
99}
100
Ben Wagner32fa5102017-08-10 21:25:55 -0400101DECLARE_bool(undefok);
102
bsalomon6eb03cc2014-08-07 14:28:50 -0700103DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
104
mtkleinf3723212014-06-25 14:08:00 -0700105DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -0700106DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -0700107DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
108DEFINE_double(overheadGoal, 0.0001,
109 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -0700110DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -0700111DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -0700112
mtklein60317d0f2014-07-14 11:30:37 -0700113DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -0700114DEFINE_int32(maxCalibrationAttempts, 3,
115 "Try up to this many times to guess loops for a bench, or skip the bench.");
116DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700117DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
118DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700119DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
120 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700121DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
mtklein8c1a4f82016-08-08 06:56:22 -0700122DEFINE_bool(lite, false, "Use SkLiteRecorder in recording benchmarks?");
robertphillips5b693772014-11-21 06:19:36 -0800123DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700124DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700125DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800126DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800127DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800128DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
msarettc149f0e2016-01-04 11:35:43 -0800129DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
joshualitt3ebd0502016-02-09 07:18:08 -0800130DEFINE_string(useThermalManager, "0,1,10,1000", "enabled,threshold,sleepTimeMs,TimeoutMs for "
131 "thermalManager\n");
Mike Reedc665e5b2017-07-04 22:56:06 -0400132DEFINE_bool(csv, false, "Print status in CSV format");
mtklein65dfd2f2016-02-03 10:40:54 -0800133DEFINE_string(sourceType, "",
134 "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
135DEFINE_string(benchType, "",
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400136 "Apply usual --match rules to bench type: micro, recording, piping, playback, skcodec, etc.");
mtklein65dfd2f2016-02-03 10:40:54 -0800137
Mike Reedc928fe22017-06-05 10:20:31 -0400138DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
139
csmartdalton008b9d82017-02-22 12:00:42 -0700140#if SK_SUPPORT_GPU
141DEFINE_pathrenderer_flag;
142#endif
143
mtkleinbbba1682015-10-28 11:36:30 -0700144static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
145
mtkleinf3723212014-06-25 14:08:00 -0700146static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700147 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800148 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700149}
mtklein55b0ffc2014-07-17 08:38:23 -0700150#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700151
tomhudsond968a6f2015-03-26 11:28:06 -0700152bool Target::init(SkImageInfo info, Benchmark* bench) {
153 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700154 this->surface = SkSurface::MakeRaster(info);
155 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700156 return false;
157 }
158 }
159 return true;
160}
161bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700162 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700163 if (!canvas) {
164 return false;
165 }
Mike Reed12e946b2017-04-17 10:53:29 -0400166 bmp->allocPixels(canvas->imageInfo());
167 if (!canvas->readPixels(*bmp, 0, 0)) {
tomhudsond968a6f2015-03-26 11:28:06 -0700168 SkDebugf("Can't read canvas pixels.\n");
169 return false;
170 }
171 return true;
172}
173
174#if SK_SUPPORT_GPU
175struct GPUTarget : public Target {
bsalomonc8699322016-05-11 11:55:36 -0700176 explicit GPUTarget(const Config& c) : Target(c), context(nullptr) { }
177 TestContext* context;
tomhudsond968a6f2015-03-26 11:28:06 -0700178
179 void setup() override {
bsalomonc8699322016-05-11 11:55:36 -0700180 this->context->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700181 // Make sure we're done with whatever came before.
bsalomonc8699322016-05-11 11:55:36 -0700182 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700183 }
184 void endTiming() override {
bsalomonc8699322016-05-11 11:55:36 -0700185 if (this->context) {
186 this->context->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700187 }
188 }
189 void fence() override {
bsalomonc8699322016-05-11 11:55:36 -0700190 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700191 }
mtkleind75c4662015-04-30 07:11:22 -0700192
cdaltond416a5b2015-06-23 13:23:44 -0700193 bool needsFrameTiming(int* maxFrameLag) const override {
bsalomonc8699322016-05-11 11:55:36 -0700194 if (!this->context->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700195 // Frame lag is unknown.
196 *maxFrameLag = FLAGS_gpuFrameLag;
197 }
198 return true;
199 }
tomhudsond968a6f2015-03-26 11:28:06 -0700200 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700201 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
202 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700203 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700204 this->surface = SkSurface::MakeRenderTarget(gGrFactory->get(this->config.ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700205 this->config.ctxOverrides),
bsalomon5ec26ae2016-02-25 08:33:02 -0800206 SkBudgeted::kNo, info,
reede8f30622016-03-23 18:59:25 -0700207 this->config.samples, &props);
bsalomonc8699322016-05-11 11:55:36 -0700208 this->context = gGrFactory->getContextInfo(this->config.ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700209 this->config.ctxOverrides).testContext();
tomhudsond968a6f2015-03-26 11:28:06 -0700210 if (!this->surface.get()) {
211 return false;
212 }
bsalomonc8699322016-05-11 11:55:36 -0700213 if (!this->context->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700214 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800215 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700216 }
tomhudsond968a6f2015-03-26 11:28:06 -0700217 return true;
218 }
219 void fillOptions(ResultsWriter* log) override {
220 const GrGLubyte* version;
bsalomonc8699322016-05-11 11:55:36 -0700221 if (this->context->backend() == kOpenGL_GrBackend) {
222 const GrGLInterface* gl =
223 reinterpret_cast<const GrGLInterface*>(this->context->backendContext());
224 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
225 log->configOption("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700226
bsalomonc8699322016-05-11 11:55:36 -0700227 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
228 log->configOption("GL_RENDERER", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700229
bsalomonc8699322016-05-11 11:55:36 -0700230 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VENDOR));
231 log->configOption("GL_VENDOR", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700232
bsalomonc8699322016-05-11 11:55:36 -0700233 GR_GL_CALL_RET(gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
234 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
235 }
tomhudsond968a6f2015-03-26 11:28:06 -0700236 }
237};
mtkleind75c4662015-04-30 07:11:22 -0700238
tomhudsond968a6f2015-03-26 11:28:06 -0700239#endif
240
tomhudson75a0ebb2015-03-27 12:11:44 -0700241static double time(int loops, Benchmark* bench, Target* target) {
242 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700243 if (canvas) {
244 canvas->clear(SK_ColorWHITE);
245 }
joshualitt8a6697a2015-09-30 12:11:07 -0700246 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700247 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700248 canvas = target->beginTiming(canvas);
249 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700250 if (canvas) {
251 canvas->flush();
252 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700253 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700254 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700255 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700256 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700257}
258
mtkleinf3723212014-06-25 14:08:00 -0700259static double estimate_timer_overhead() {
260 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700261 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700262 double start = now_ms();
263 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700264 }
265 return overhead / FLAGS_overheadLoops;
266}
267
reed53249782014-10-10 09:09:52 -0700268static int detect_forever_loops(int loops) {
269 // look for a magic run-forever value
270 if (loops < 0) {
271 loops = SK_MaxS32;
272 }
273 return loops;
274}
275
mtklein55b0ffc2014-07-17 08:38:23 -0700276static int clamp_loops(int loops) {
277 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800278 SkDebugf("ERROR: clamping loops from %d to 1. "
279 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700280 return 1;
281 }
282 if (loops > FLAGS_maxLoops) {
283 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
284 return FLAGS_maxLoops;
285 }
286 return loops;
287}
288
tomhudsond968a6f2015-03-26 11:28:06 -0700289static bool write_canvas_png(Target* target, const SkString& filename) {
290
bsalomon6eb03cc2014-08-07 14:28:50 -0700291 if (filename.isEmpty()) {
292 return false;
293 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700294 if (target->getCanvas() &&
295 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700296 return false;
297 }
tomhudsond968a6f2015-03-26 11:28:06 -0700298
bsalomon6eb03cc2014-08-07 14:28:50 -0700299 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700300
301 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700302 return false;
303 }
tomhudsond968a6f2015-03-26 11:28:06 -0700304
bsalomon6eb03cc2014-08-07 14:28:50 -0700305 SkString dir = SkOSPath::Dirname(filename.c_str());
306 if (!sk_mkdir(dir.c_str())) {
307 SkDebugf("Can't make dir %s.\n", dir.c_str());
308 return false;
309 }
310 SkFILEWStream stream(filename.c_str());
311 if (!stream.isValid()) {
312 SkDebugf("Can't write %s.\n", filename.c_str());
313 return false;
314 }
Hal Canarydb683012016-11-23 08:55:18 -0700315 if (!SkEncodeImage(&stream, bmp, SkEncodedImageFormat::kPNG, 100)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700316 SkDebugf("Can't encode a PNG.\n");
317 return false;
318 }
319 return true;
320}
321
322static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700323static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700324 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700325 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700326 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700327 int loops = bench->calculateLoops(FLAGS_loops);
328 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700329 while (bench_plus_overhead < overhead) {
330 if (round++ == FLAGS_maxCalibrationAttempts) {
331 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700332 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700333 return kFailedLoops;
334 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700335 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700336 }
mtklein2069e222014-08-04 13:57:39 -0700337 }
mtkleinf3723212014-06-25 14:08:00 -0700338
mtkleinbb6a0282014-07-01 08:43:42 -0700339 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700340 // We'll pick N to make timer overhead negligible:
341 //
mtkleinbb6a0282014-07-01 08:43:42 -0700342 // overhead
343 // ------------------------- < FLAGS_overheadGoal
344 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700345 //
Hal Canary55325b72017-01-03 10:36:17 -0500346 // where bench_plus_overhead ~=~ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700347 //
348 // Doing some math, we get:
349 //
mtkleinbb6a0282014-07-01 08:43:42 -0700350 // (overhead / FLAGS_overheadGoal) - overhead
351 // ------------------------------------------ < N
352 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700353 //
354 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700355 if (kAutoTuneLoops == loops) {
356 const double numer = overhead / FLAGS_overheadGoal - overhead;
357 const double denom = bench_plus_overhead - overhead;
358 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700359 loops = clamp_loops(loops);
360 } else {
361 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700362 }
mtkleinbb6a0282014-07-01 08:43:42 -0700363
mtkleinbb6a0282014-07-01 08:43:42 -0700364 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700365}
366
cdaltone1b89582015-06-25 19:17:08 -0700367static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700368 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700369 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700370 if (kAutoTuneLoops == loops) {
371 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700372 double elapsed = 0;
373 do {
mtklein527930f2014-11-06 08:04:34 -0800374 if (1<<30 == loops) {
375 // We're about to wrap. Something's wrong with the bench.
376 loops = 0;
377 break;
378 }
mtkleina189ccd2014-07-14 12:28:47 -0700379 loops *= 2;
380 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700381 // _this_ round, not still timing last round.
382 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700383 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700384 }
385 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700386
mtkleina189ccd2014-07-14 12:28:47 -0700387 // We've overshot at least a little. Scale back linearly.
388 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700389 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700390
tomhudsond968a6f2015-03-26 11:28:06 -0700391 // Make sure we're not still timing our calibration.
392 target->fence();
reed53249782014-10-10 09:09:52 -0700393 } else {
394 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700395 }
mtkleinbb6a0282014-07-01 08:43:42 -0700396
397 // Pretty much the same deal as the calibration: do some warmup to make
398 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700399 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700400 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700401 }
mtkleinbb6a0282014-07-01 08:43:42 -0700402
mtkleinbb6a0282014-07-01 08:43:42 -0700403 return loops;
404}
mtkleinbb6a0282014-07-01 08:43:42 -0700405
bsalomonc2553372014-07-22 13:09:05 -0700406#if SK_SUPPORT_GPU
Brian Salomon6405e712017-03-20 08:54:16 -0400407#define kBogusContextType GrContextFactory::kGL_ContextType
csmartdaltone812d492017-02-21 12:36:05 -0700408#define kBogusContextOverrides GrContextFactory::ContextOverrides::kNone
bsalomonc2553372014-07-22 13:09:05 -0700409#else
bsalomon85b4b532016-04-05 11:06:27 -0700410#define kBogusContextType 0
csmartdaltone812d492017-02-21 12:36:05 -0700411#define kBogusContextOverrides 0
mtkleine714e752014-07-31 12:13:48 -0700412#endif
bsalomonc2553372014-07-22 13:09:05 -0700413
svaisanenc47635e2016-01-28 06:05:43 -0800414static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
415
416#if SK_SUPPORT_GPU
417 if (const auto* gpuConfig = config->asConfigGpu()) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400418 if (!FLAGS_gpu) {
419 SkDebugf("Skipping config '%s' as requested.\n", config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800420 return;
Ben Wagner32fa5102017-08-10 21:25:55 -0400421 }
svaisanenc47635e2016-01-28 06:05:43 -0800422
svaisanenc47635e2016-01-28 06:05:43 -0800423 const auto ctxType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700424 const auto ctxOverrides = gpuConfig->getContextOverrides();
svaisanenc47635e2016-01-28 06:05:43 -0800425 const auto sampleCount = gpuConfig->getSamples();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400426 const auto colorType = gpuConfig->getColorType();
427 auto colorSpace = gpuConfig->getColorSpace();
svaisanenc47635e2016-01-28 06:05:43 -0800428
csmartdaltone812d492017-02-21 12:36:05 -0700429 if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOverrides)) {
Greg Daniel81e7bf82017-07-19 14:47:42 -0400430 GrPixelConfig grPixConfig = SkImageInfo2GrPixelConfig(colorType, colorSpace,
431 *ctx->caps());
432 int supportedSampleCount = ctx->caps()->getSampleCount(sampleCount, grPixConfig);
433 if (sampleCount != supportedSampleCount) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400434 SkDebugf("Configuration '%s' sample count %d is not a supported sample count.\n",
435 config->getTag().c_str(), sampleCount);
svaisanenc47635e2016-01-28 06:05:43 -0800436 return;
437 }
438 } else {
Ben Wagner32fa5102017-08-10 21:25:55 -0400439 SkDebugf("No context was available matching config '%s'.\n",
440 config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800441 return;
442 }
443
444 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800445 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800446 Benchmark::kGPU_Backend,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400447 colorType,
svaisanenc47635e2016-01-28 06:05:43 -0800448 kPremul_SkAlphaType,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400449 sk_ref_sp(colorSpace),
svaisanenc47635e2016-01-28 06:05:43 -0800450 sampleCount,
451 ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700452 ctxOverrides,
kkinnunend4e1c352016-03-01 23:41:26 -0800453 gpuConfig->getUseDIText()
454 };
svaisanenc47635e2016-01-28 06:05:43 -0800455
456 configs->push_back(target);
457 return;
458 }
459#endif
460
brianosmanb109b8c2016-06-16 13:03:24 -0700461 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
462 if (config->getTag().equals(#name)) { \
Ben Wagner32fa5102017-08-10 21:25:55 -0400463 if (!FLAGS_cpu) { \
464 SkDebugf("Skipping config '%s' as requested.\n", \
465 config->getTag().c_str()); \
466 return; \
467 } \
brianosmanb109b8c2016-06-16 13:03:24 -0700468 Config config = { \
469 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
csmartdaltone812d492017-02-21 12:36:05 -0700470 0, kBogusContextType, kBogusContextOverrides, false \
brianosmanb109b8c2016-06-16 13:03:24 -0700471 }; \
472 configs->push_back(config); \
473 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700474 }
mtkleine714e752014-07-31 12:13:48 -0700475
Ben Wagner32fa5102017-08-10 21:25:55 -0400476 CPU_CONFIG(nonrendering, kNonRendering_Backend,
477 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800478
Mike Reede6befa52017-08-28 11:30:48 -0400479 CPU_CONFIG(a8, kRaster_Backend,
480 kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr)
Ben Wagner32fa5102017-08-10 21:25:55 -0400481 CPU_CONFIG(8888, kRaster_Backend,
482 kN32_SkColorType, kPremul_SkAlphaType, nullptr)
483 CPU_CONFIG(565, kRaster_Backend,
484 kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
485 auto srgbColorSpace = SkColorSpace::MakeSRGB();
486 CPU_CONFIG(srgb, kRaster_Backend,
487 kN32_SkColorType, kPremul_SkAlphaType, srgbColorSpace)
488 auto srgbLinearColorSpace = SkColorSpace::MakeSRGBLinear();
489 CPU_CONFIG(f16, kRaster_Backend,
490 kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbLinearColorSpace)
mtkleinbb6a0282014-07-01 08:43:42 -0700491
svaisanenc47635e2016-01-28 06:05:43 -0800492 #undef CPU_CONFIG
Ben Wagner32fa5102017-08-10 21:25:55 -0400493
494 SkDebugf("Unknown config '%s'.\n", config->getTag().c_str());
mtkleinf3723212014-06-25 14:08:00 -0700495}
496
svaisanenc47635e2016-01-28 06:05:43 -0800497// Append all configs that are enabled and supported.
498void create_configs(SkTArray<Config>* configs) {
499 SkCommandLineConfigArray array;
500 ParseConfigs(FLAGS_config, &array);
501 for (int i = 0; i < array.count(); ++i) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400502 create_config(array[i].get(), configs);
svaisanenc47635e2016-01-28 06:05:43 -0800503 }
Ben Wagner32fa5102017-08-10 21:25:55 -0400504
505 // If no just default configs were requested, then we're okay.
506 if (array.count() == 0 || FLAGS_config.count() == 0 ||
507 // If we've been told to ignore undefined flags, we're okay.
508 FLAGS_undefok ||
509 // Otherwise, make sure that all specified configs have been created.
510 array.count() == configs->count()) {
511 return;
512 }
513 SkDebugf("Invalid --config. Use --undefok to bypass this warning.\n");
514 exit(1);
svaisanenc47635e2016-01-28 06:05:43 -0800515}
516
brianosman9f1f6e22016-09-15 08:33:02 -0700517// disable warning : switch statement contains default but no 'case' labels
518#if defined _WIN32
519#pragma warning ( push )
520#pragma warning ( disable : 4065 )
521#endif
522
halcanary96fcdcc2015-08-27 07:41:13 -0700523// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700524static Target* is_enabled(Benchmark* bench, const Config& config) {
525 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700526 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700527 }
528
reede5ea5002014-09-03 11:54:58 -0700529 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700530 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700531
halcanary96fcdcc2015-08-27 07:41:13 -0700532 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700533
tomhudsond968a6f2015-03-26 11:28:06 -0700534 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700535#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700536 case Benchmark::kGPU_Backend:
537 target = new GPUTarget(config);
538 break;
bsalomonc2553372014-07-22 13:09:05 -0700539#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700540 default:
541 target = new Target(config);
542 break;
543 }
bsalomonc2553372014-07-22 13:09:05 -0700544
tomhudsond968a6f2015-03-26 11:28:06 -0700545 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700546 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700547 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700548 }
549 return target;
550}
551
brianosman9f1f6e22016-09-15 08:33:02 -0700552#if defined _WIN32
553#pragma warning ( pop )
554#endif
555
reed42943c82016-09-12 12:01:44 -0700556static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700557 uint32_t minOutputSize, int* width, int* height) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400558 std::unique_ptr<SkBitmapRegionDecoder> brd(
msarettd1227a72016-05-18 06:23:57 -0700559 SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy));
msarett7f691442015-09-22 11:56:16 -0700560 if (nullptr == brd.get()) {
561 // This is indicates that subset decoding is not supported for a particular image format.
562 return false;
563 }
564
msarett7f691442015-09-22 11:56:16 -0700565 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
566 (uint32_t) brd->height()) {
567 // This indicates that the image is not large enough to decode a
568 // minOutputSize x minOutputSize subset at the given sampleSize.
569 return false;
570 }
571
572 // Set the image width and height. The calling code will use this to choose subsets to decode.
573 *width = brd->width();
574 *height = brd->height();
575 return true;
576}
577
egdaniel3bf92062015-06-26 08:12:46 -0700578static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700579 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700580#if SK_SUPPORT_GPU
581 if (FLAGS_abandonGpuContext) {
582 gGrFactory->abandonContexts();
583 }
584 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
585 gGrFactory->destroyContexts();
586 }
587#endif
588}
589
fmalita6519c212016-09-14 08:05:17 -0700590static void collect_files(const SkCommandLineFlags::StringArray& paths, const char* ext,
591 SkTArray<SkString>* list) {
592 for (int i = 0; i < paths.count(); ++i) {
593 if (SkStrEndsWith(paths[i], ext)) {
594 list->push_back(SkString(paths[i]));
595 } else {
596 SkOSFile::Iter it(paths[i], ext);
597 SkString path;
598 while (it.next(&path)) {
599 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
600 }
601 }
602 }
603}
604
mtkleine714e752014-07-31 12:13:48 -0700605class BenchmarkStream {
606public:
mtklein92007582014-08-01 07:46:52 -0700607 BenchmarkStream() : fBenches(BenchRegistry::Head())
608 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700609 , fCurrentRecording(0)
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400610 , fCurrentPiping(0)
mtklein92007582014-08-01 07:46:52 -0700611 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800612 , fCurrentSKP(0)
fmalita6519c212016-09-14 08:05:17 -0700613 , fCurrentSVG(0)
msarett95f192d2015-02-13 09:05:41 -0800614 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700615 , fCurrentCodec(0)
msarett84451022016-02-11 06:45:51 -0800616 , fCurrentAndroidCodec(0)
msarett7f691442015-09-22 11:56:16 -0700617 , fCurrentBRDImage(0)
msarett2cee9022016-06-03 08:25:21 -0700618 , fCurrentColorImage(0)
msarett95f192d2015-02-13 09:05:41 -0800619 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800620 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700621 , fCurrentSubsetType(0)
msarett84451022016-02-11 06:45:51 -0800622 , fCurrentSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700623 , fCurrentAnimSKP(0) {
fmalita6519c212016-09-14 08:05:17 -0700624 collect_files(FLAGS_skps, ".skp", &fSKPs);
625 collect_files(FLAGS_svgs, ".svg", &fSVGs);
mtkleine714e752014-07-31 12:13:48 -0700626
mtklein92007582014-08-01 07:46:52 -0700627 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
628 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
629 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
630 exit(1);
631 }
632
633 for (int i = 0; i < FLAGS_scales.count(); i++) {
634 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
635 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
636 exit(1);
637 }
638 }
robertphillips5b693772014-11-21 06:19:36 -0800639
cdalton63a82852015-06-29 14:06:10 -0700640 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
641 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700642 exit(1);
643 }
644
robertphillips5b693772014-11-21 06:19:36 -0800645 if (FLAGS_mpd) {
646 fUseMPDs.push_back() = true;
647 }
mtkleinc751ecb2015-06-15 08:56:38 -0700648 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700649
msarett95f192d2015-02-13 09:05:41 -0800650 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700651 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800652 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800653 }
msarett2cee9022016-06-03 08:25:21 -0700654 if (!CollectImages(FLAGS_colorImages, &fColorImages)) {
655 exit(1);
656 }
mtklein95553d92015-03-12 08:24:21 -0700657
msarett95f192d2015-02-13 09:05:41 -0800658 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700659 fColorTypes.push_back(kN32_SkColorType);
660 if (!FLAGS_simpleCodec) {
661 fColorTypes.push_back(kRGB_565_SkColorType);
662 fColorTypes.push_back(kAlpha_8_SkColorType);
msarett67cb6662016-06-21 08:49:26 -0700663 fColorTypes.push_back(kGray_8_SkColorType);
664 }
mtklein92007582014-08-01 07:46:52 -0700665 }
666
reedca2622b2016-03-18 07:25:55 -0700667 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700668 // Not strictly necessary, as it will be checked again later,
669 // but helps to avoid a lot of pointless work if we're going to skip it.
cdalton91e457d2016-02-17 11:10:16 -0800670 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700671 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700672 }
673
bungemanf93d7112016-09-16 06:24:20 -0700674 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
675 if (!stream) {
mtkleinfd731ce2014-09-10 12:19:30 -0700676 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700677 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700678 }
679
reedca2622b2016-03-18 07:25:55 -0700680 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700681 }
682
fmalita6519c212016-09-14 08:05:17 -0700683 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
684 SkFILEStream stream(path);
685 if (!stream.isValid()) {
686 SkDebugf("Could not read %s.\n", path);
687 return nullptr;
688 }
689
fmalitae1baa7c2016-09-14 12:04:30 -0700690 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
fmalita6519c212016-09-14 08:05:17 -0700691 if (!svgDom) {
692 SkDebugf("Could not parse %s.\n", path);
693 return nullptr;
694 }
695
fmalitae1baa7c2016-09-14 12:04:30 -0700696 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
697 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
698 if (svgDom->containerSize().isEmpty()) {
699 svgDom->setContainerSize(kDefaultContainerSize);
700 }
701
fmalita6519c212016-09-14 08:05:17 -0700702 SkPictureRecorder recorder;
fmalitae1baa7c2016-09-14 12:04:30 -0700703 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
704 svgDom->containerSize().height()));
fmalita6519c212016-09-14 08:05:17 -0700705 return recorder.finishRecordingAsPicture();
706 }
707
mtklein92007582014-08-01 07:46:52 -0700708 Benchmark* next() {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400709 std::unique_ptr<Benchmark> bench;
mtklein65dfd2f2016-02-03 10:40:54 -0800710 do {
711 bench.reset(this->rawNext());
712 if (!bench) {
713 return nullptr;
714 }
715 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
716 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700717 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800718 }
719
720 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700721 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700722 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700723 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700724 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700725 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700726 return bench;
727 }
mtklein92007582014-08-01 07:46:52 -0700728
mtkleine714e752014-07-31 12:13:48 -0700729 while (fGMs) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400730 std::unique_ptr<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700731 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800732 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700733 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700734 fBenchType = "micro";
mtklein18300a32016-03-16 13:53:35 -0700735 return new GMBench(gm.release());
mtkleine714e752014-07-31 12:13:48 -0700736 }
737 }
mtklein92007582014-08-01 07:46:52 -0700738
mtkleinfd731ce2014-09-10 12:19:30 -0700739 // First add all .skps as RecordingBenches.
740 while (fCurrentRecording < fSKPs.count()) {
741 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700742 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
743 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700744 continue;
745 }
746 SkString name = SkOSPath::Basename(path.c_str());
747 fSourceType = "skp";
748 fBenchType = "recording";
Mike Reed90b20052017-03-08 10:39:02 -0500749 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
mtklein051e56d2014-12-04 08:46:51 -0800750 fSKPOps = pic->approximateOpCount();
mtklein8c1a4f82016-08-08 06:56:22 -0700751 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh, FLAGS_lite);
mtkleinfd731ce2014-09-10 12:19:30 -0700752 }
753
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400754 // Add all .skps as PipeBenches.
755 while (fCurrentPiping < fSKPs.count()) {
756 const SkString& path = fSKPs[fCurrentPiping++];
757 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
758 if (!pic) {
759 continue;
760 }
761 SkString name = SkOSPath::Basename(path.c_str());
762 fSourceType = "skp";
763 fBenchType = "piping";
Mike Reed90b20052017-03-08 10:39:02 -0500764 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400765 fSKPOps = pic->approximateOpCount();
766 return new PipingBench(name.c_str(), pic.get());
767 }
768
mtkleinfd731ce2014-09-10 12:19:30 -0700769 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700770 while (fCurrentScale < fScales.count()) {
771 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800772 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700773 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
774 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800775 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700776 continue;
777 }
robertphillips5b693772014-11-21 06:19:36 -0800778
779 while (fCurrentUseMPD < fUseMPDs.count()) {
780 if (FLAGS_bbh) {
781 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
782 SkRTreeFactory factory;
783 SkPictureRecorder recorder;
robertphillips5b693772014-11-21 06:19:36 -0800784 pic->playback(recorder.beginRecording(pic->cullRect().width(),
785 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800786 &factory,
robertphillipsdda54452016-07-13 13:27:16 -0700787 0));
reedca2622b2016-03-18 07:25:55 -0700788 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800789 }
790 SkString name = SkOSPath::Basename(path.c_str());
791 fSourceType = "skp";
792 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700793 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
794 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700795 }
robertphillips5b693772014-11-21 06:19:36 -0800796 fCurrentUseMPD = 0;
797 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700798 }
fmalita6519c212016-09-14 08:05:17 -0700799
800 while (fCurrentSVG++ < fSVGs.count()) {
801 const char* path = fSVGs[fCurrentSVG - 1].c_str();
802 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
803 fSourceType = "svg";
804 fBenchType = "playback";
805 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
806 fScales[fCurrentScale], false, FLAGS_loopSKP);
807 }
808 }
809
mtklein92007582014-08-01 07:46:52 -0700810 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700811 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700812 fCurrentScale++;
813 }
814
joshualitt261c3ad2015-04-27 09:16:57 -0700815 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700816 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700817 while (fCurrentAnimSKP < fSKPs.count()) {
818 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700819 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
820 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700821 fCurrentAnimSKP++;
822 continue;
823 }
824
825 fCurrentAnimSKP++;
826 SkString name = SkOSPath::Basename(path.c_str());
Hal Canary2db83612016-11-04 13:02:54 -0400827 sk_sp<SKPAnimationBench::Animation> animation(
cdalton63a82852015-06-29 14:06:10 -0700828 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
Hal Canary2db83612016-11-04 13:02:54 -0400829 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation.get(),
halcanary385fe4d2015-08-26 13:07:48 -0700830 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700831 }
832 }
833
scroggo60869a42015-04-01 12:09:17 -0700834 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700835 fSourceType = "image";
836 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700837 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800838 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
839 continue;
840 }
bungeman38d909e2016-08-02 14:40:46 -0700841 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400842 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700843 if (!codec) {
844 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700845 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700846 continue;
847 }
scroggo21027992015-04-02 13:22:38 -0700848
scroggo60869a42015-04-01 12:09:17 -0700849 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700850 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700851
msarettc7796b92016-01-07 14:20:20 -0800852 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700853 if (FLAGS_simpleCodec) {
854 if (kUnpremul_SkAlphaType == alphaType) {
855 alphaType = kPremul_SkAlphaType;
856 }
857
858 fCurrentColorType++;
859 } else {
860 switch (alphaType) {
861 case kOpaque_SkAlphaType:
862 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800863 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700864 break;
865 case kUnpremul_SkAlphaType:
866 case kPremul_SkAlphaType:
867 if (0 == fCurrentAlphaType) {
868 // Test unpremul first.
869 alphaType = kUnpremul_SkAlphaType;
870 fCurrentAlphaType++;
871 } else {
872 // Test premul.
873 alphaType = kPremul_SkAlphaType;
874 fCurrentAlphaType = 0;
875 fCurrentColorType++;
876 }
877 break;
878 default:
879 SkASSERT(false);
880 fCurrentColorType++;
881 break;
882 }
scroggo21027992015-04-02 13:22:38 -0700883 }
884
msarettc7796b92016-01-07 14:20:20 -0800885 // Make sure we can decode to this color type and alpha type.
886 SkImageInfo info =
887 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700888 const size_t rowBytes = info.minRowBytes();
889 SkAutoMalloc storage(info.getSafeSize(rowBytes));
890
scroggoeb602a52015-07-09 08:16:03 -0700891 const SkCodec::Result result = codec->getPixels(
Leon Scroggins571b30f2017-07-11 17:35:31 +0000892 info, storage.get(), rowBytes);
scroggo60869a42015-04-01 12:09:17 -0700893 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700894 case SkCodec::kSuccess:
895 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700896 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700897 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700898 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700899 // This is okay. Not all conversions are valid.
900 break;
scroggo60869a42015-04-01 12:09:17 -0700901 default:
902 // This represents some sort of failure.
903 SkASSERT(false);
904 break;
905 }
906 }
907 fCurrentColorType = 0;
908 }
909
msarett84451022016-02-11 06:45:51 -0800910 // Run AndroidCodecBenches
911 const int sampleSizes[] = { 2, 4, 8 };
912 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
913 fSourceType = "image";
914 fBenchType = "skandroidcodec";
915
916 const SkString& path = fImages[fCurrentAndroidCodec];
917 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
918 continue;
919 }
bungeman38d909e2016-08-02 14:40:46 -0700920 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400921 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800922 if (!codec) {
923 // Nothing to time.
924 SkDebugf("Cannot find codec for %s\n", path.c_str());
925 continue;
926 }
927
928 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
929 int sampleSize = sampleSizes[fCurrentSampleSize];
930 fCurrentSampleSize++;
931 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
932 // Avoid benchmarking scaled decodes of already small images.
933 break;
934 }
935
bungeman38d909e2016-08-02 14:40:46 -0700936 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
937 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -0800938 }
939 fCurrentSampleSize = 0;
940 }
941
msarett7f691442015-09-22 11:56:16 -0700942 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700943 // We intend to create benchmarks that model the use cases in
944 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
945 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
946 // the image. For that reason, we will benchmark decodes in five representative locations
947 // in the image. Additionally, this use case utilizes power of two scaling, so we will
948 // test on power of two sample sizes. The output tile is always 512x512, so, when a
949 // sampleSize is used, the size of the subset that is decoded is always
950 // (sampleSize*512)x(sampleSize*512).
951 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700952 // All use cases we are aware of only scale by powers of two.
953 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
954 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800955 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700956 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800957 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700958 fSourceType = "image";
959 fBenchType = "BRD";
960
mtklein6f0ff912016-01-11 09:04:21 -0800961 const SkString& path = fImages[fCurrentBRDImage];
962 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
963 continue;
964 }
scroggo860e8a62015-10-15 07:51:28 -0700965
msarettd1227a72016-05-18 06:23:57 -0700966 while (fCurrentColorType < fColorTypes.count()) {
967 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
968 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700969
bungeman38d909e2016-08-02 14:40:46 -0700970 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -0700971 const SkColorType colorType = fColorTypes[fCurrentColorType];
972 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
973 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -0700974
msarettd1227a72016-05-18 06:23:57 -0700975 int width = 0;
976 int height = 0;
reed42943c82016-09-12 12:01:44 -0700977 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -0700978 &width, &height)) {
979 break;
msarett7f691442015-09-22 11:56:16 -0700980 }
msarettd1227a72016-05-18 06:23:57 -0700981
982 SkString basename = SkOSPath::Basename(path.c_str());
983 SkIRect subset;
984 const uint32_t subsetSize = sampleSize * minOutputSize;
985 switch (currentSubsetType) {
986 case kTopLeft_SubsetType:
987 basename.append("_TopLeft");
988 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
989 break;
990 case kTopRight_SubsetType:
991 basename.append("_TopRight");
992 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
993 subsetSize);
994 break;
995 case kMiddle_SubsetType:
996 basename.append("_Middle");
997 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
998 (height - subsetSize) / 2, subsetSize, subsetSize);
999 break;
1000 case kBottomLeft_SubsetType:
1001 basename.append("_BottomLeft");
1002 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
1003 subsetSize);
1004 break;
1005 case kBottomRight_SubsetType:
1006 basename.append("_BottomRight");
1007 subset = SkIRect::MakeXYWH(width - subsetSize,
1008 height - subsetSize, subsetSize, subsetSize);
1009 break;
1010 default:
1011 SkASSERT(false);
1012 }
1013
1014 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
1015 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -07001016 }
msarettd1227a72016-05-18 06:23:57 -07001017 fCurrentSubsetType = 0;
1018 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -07001019 }
msarettd1227a72016-05-18 06:23:57 -07001020 fCurrentSampleSize = 0;
1021 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -07001022 }
msarettd1227a72016-05-18 06:23:57 -07001023 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -07001024 }
1025
msarett2cee9022016-06-03 08:25:21 -07001026 while (fCurrentColorImage < fColorImages.count()) {
1027 fSourceType = "colorimage";
1028 fBenchType = "skcolorcodec";
1029 const SkString& path = fColorImages[fCurrentColorImage];
1030 fCurrentColorImage++;
1031 sk_sp<SkData> encoded = SkData::MakeFromFileName(path.c_str());
1032 if (encoded) {
1033 return new ColorCodecBench(SkOSPath::Basename(path.c_str()).c_str(),
1034 std::move(encoded));
1035 } else {
1036 SkDebugf("Could not read file %s.\n", path.c_str());
1037 }
1038 }
1039
halcanary96fcdcc2015-08-27 07:41:13 -07001040 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001041 }
mtklein92007582014-08-01 07:46:52 -07001042
1043 void fillCurrentOptions(ResultsWriter* log) const {
1044 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001045 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001046 if (0 == strcmp(fSourceType, "skp")) {
1047 log->configOption("clip",
1048 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1049 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001050 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -07001051 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001052 if (fCurrentUseMPD > 0) {
1053 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1054 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1055 }
mtklein92007582014-08-01 07:46:52 -07001056 }
mtklein051e56d2014-12-04 08:46:51 -08001057 if (0 == strcmp(fBenchType, "recording")) {
1058 log->metric("bytes", fSKPBytes);
1059 log->metric("ops", fSKPOps);
1060 }
mtklein92007582014-08-01 07:46:52 -07001061 }
1062
mtkleine714e752014-07-31 12:13:48 -07001063private:
msarettb23e6aa2015-06-09 13:56:10 -07001064 enum SubsetType {
1065 kTopLeft_SubsetType = 0,
1066 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001067 kMiddle_SubsetType = 2,
1068 kBottomLeft_SubsetType = 3,
1069 kBottomRight_SubsetType = 4,
1070 kTranslate_SubsetType = 5,
1071 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001072 kLast_SubsetType = kZoom_SubsetType,
1073 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001074 };
1075
mtkleine714e752014-07-31 12:13:48 -07001076 const BenchRegistry* fBenches;
1077 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001078 SkIRect fClip;
1079 SkTArray<SkScalar> fScales;
1080 SkTArray<SkString> fSKPs;
fmalita6519c212016-09-14 08:05:17 -07001081 SkTArray<SkString> fSVGs;
robertphillips5b693772014-11-21 06:19:36 -08001082 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001083 SkTArray<SkString> fImages;
msarett2cee9022016-06-03 08:25:21 -07001084 SkTArray<SkString> fColorImages;
msarett74deb982015-10-20 16:45:56 -07001085 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001086 SkScalar fZoomMax;
1087 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001088
mtklein051e56d2014-12-04 08:46:51 -08001089 double fSKPBytes, fSKPOps;
1090
mtkleinfd731ce2014-09-10 12:19:30 -07001091 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1092 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1093 int fCurrentRecording;
Mike Reed9cdd2ab2016-10-21 10:43:36 -04001094 int fCurrentPiping;
mtklein92007582014-08-01 07:46:52 -07001095 int fCurrentScale;
1096 int fCurrentSKP;
fmalita6519c212016-09-14 08:05:17 -07001097 int fCurrentSVG;
robertphillips5b693772014-11-21 06:19:36 -08001098 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001099 int fCurrentCodec;
msarett84451022016-02-11 06:45:51 -08001100 int fCurrentAndroidCodec;
msarett7f691442015-09-22 11:56:16 -07001101 int fCurrentBRDImage;
msarett2cee9022016-06-03 08:25:21 -07001102 int fCurrentColorImage;
msarett95f192d2015-02-13 09:05:41 -08001103 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -08001104 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -07001105 int fCurrentSubsetType;
msarett84451022016-02-11 06:45:51 -08001106 int fCurrentSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001107 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001108};
1109
msarettc149f0e2016-01-04 11:35:43 -08001110// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1111// This prints something every once in a while so that it knows we're still working.
1112static void start_keepalive() {
1113 struct Loop {
1114 static void forever(void*) {
1115 for (;;) {
1116 static const int kSec = 1200;
1117 #if defined(SK_BUILD_FOR_WIN)
1118 Sleep(kSec * 1000);
1119 #else
1120 sleep(kSec);
1121 #endif
1122 SkDebugf("\nBenchmarks still running...\n");
1123 }
1124 }
1125 };
1126 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1127 intentionallyLeaked->start();
1128}
1129
Mike Kleinbe28ee22017-02-06 12:46:20 -05001130int main(int argc, char** argv) {
1131 SkCommandLineFlags::Parse(argc, argv);
Brian Osman53136aa2017-07-20 15:43:35 -04001132
Brian Osmanbc8150f2017-07-24 11:38:01 -04001133 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -04001134
Mike Kleinadacaef2017-02-06 09:26:14 -05001135#if defined(SK_BUILD_FOR_IOS)
1136 cd_Documents();
1137#endif
jcgregorio3b27ade2014-11-13 08:06:40 -08001138 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001139 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001140 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001141
krajcevski69a55602014-08-13 10:46:31 -07001142#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001143 GrContextOptions grContextOpts;
csmartdalton008b9d82017-02-22 12:00:42 -07001144 grContextOpts.fGpuPathRenderers = CollectGpuPathRenderersFromFlags();
Brian Osmanf9810662017-08-30 10:02:10 -04001145 grContextOpts.fExecutor = GpuExecutorForTools();
halcanary385fe4d2015-08-26 13:07:48 -07001146 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001147#endif
1148
bsalomon06cddec2014-10-24 10:40:50 -07001149 if (FLAGS_veryVerbose) {
1150 FLAGS_verbose = true;
1151 }
1152
bsalomon6eb03cc2014-08-07 14:28:50 -07001153 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001154 FLAGS_samples = 1;
1155 FLAGS_gpuFrameLag = 0;
1156 }
1157
bsalomon6eb03cc2014-08-07 14:28:50 -07001158 if (!FLAGS_writePath.isEmpty()) {
1159 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1160 if (!sk_mkdir(FLAGS_writePath[0])) {
1161 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001162 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001163 }
1164 }
1165
Ben Wagner145dbcd2016-11-03 14:40:50 -04001166 std::unique_ptr<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001167 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001168#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001169 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001170#else
1171 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1172 return 1;
1173#endif
mtklein60317d0f2014-07-14 11:30:37 -07001174 }
mtklein1915b622014-08-20 11:45:00 -07001175
1176 if (1 == FLAGS_properties.count() % 2) {
1177 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1178 return 1;
1179 }
1180 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1181 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1182 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001183
1184 if (1 == FLAGS_key.count() % 2) {
1185 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1186 return 1;
1187 }
1188 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001189 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001190 }
mtklein60317d0f2014-07-14 11:30:37 -07001191
mtkleinf3723212014-06-25 14:08:00 -07001192 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001193 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001194
cdaltone1b89582015-06-25 19:17:08 -07001195 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001196
bsalomon6eb03cc2014-08-07 14:28:50 -07001197 if (kAutoTuneLoops != FLAGS_loops) {
1198 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001199 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001200 SkDebugf("! -> high variance, ? -> moderate variance\n");
1201 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001202 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001203 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001204 } else {
mtkleind75c4662015-04-30 07:11:22 -07001205 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001206 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001207 }
1208
svaisanenc47635e2016-01-28 06:05:43 -08001209 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001210 create_configs(&configs);
1211
joshualitt3ebd0502016-02-09 07:18:08 -08001212#ifdef THERMAL_MANAGER_SUPPORTED
1213 int tmEnabled, tmThreshold, tmSleepTimeMs, tmTimeoutMs;
1214 if (4 != sscanf(FLAGS_useThermalManager[0], "%d,%d,%d,%d",
1215 &tmEnabled, &tmThreshold, &tmSleepTimeMs, &tmTimeoutMs)) {
1216 SkDebugf("Can't parse %s from --useThermalManager.\n", FLAGS_useThermalManager[0]);
1217 exit(1);
1218 }
1219 ThermalManager tm(tmThreshold, tmSleepTimeMs, tmTimeoutMs);
1220#endif
1221
msarettc149f0e2016-01-04 11:35:43 -08001222 if (FLAGS_keepAlive) {
1223 start_keepalive();
1224 }
1225
Yuqian Liba62b4a2016-12-14 13:46:53 -05001226 gSkUseAnalyticAA = FLAGS_analyticAA;
Yuqian Lidf60e362017-07-25 11:26:31 -04001227 gSkUseDeltaAA = FLAGS_deltaAA;
liyuqian38911a72016-10-04 11:23:22 -07001228
Yuqian Lidf60e362017-07-25 11:26:31 -04001229 if (FLAGS_forceDeltaAA) {
1230 gSkForceDeltaAA = true;
1231 }
Yuqian Li550148b2017-01-13 10:13:13 -05001232 if (FLAGS_forceAnalyticAA) {
1233 gSkForceAnalyticAA = true;
1234 }
Mike Reedc928fe22017-06-05 10:20:31 -04001235 if (FLAGS_forceRasterPipeline) {
1236 gSkForceRasterPipelineBlitter = true;
1237 }
Yuqian Li550148b2017-01-13 10:13:13 -05001238
mtkleine070c2b2014-10-14 08:40:43 -07001239 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001240 BenchmarkStream benchStream;
1241 while (Benchmark* b = benchStream.next()) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001242 std::unique_ptr<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001243 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001244 continue;
1245 }
1246
svaisanenc47635e2016-01-28 06:05:43 -08001247 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001248 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001249 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001250 }
egdaniel3bf92062015-06-26 08:12:46 -07001251 for (int i = 0; i < configs.count(); ++i) {
joshualitt3ebd0502016-02-09 07:18:08 -08001252#ifdef THERMAL_MANAGER_SUPPORTED
1253 if (tmEnabled && !tm.coolOffIfNecessary()) {
1254 SkDebugf("Could not cool off, timings will be throttled\n");
1255 }
1256#endif
egdaniel3bf92062015-06-26 08:12:46 -07001257 Target* target = is_enabled(b, configs[i]);
1258 if (!target) {
1259 continue;
1260 }
mtkleinf3723212014-06-25 14:08:00 -07001261
halcanary96fcdcc2015-08-27 07:41:13 -07001262 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001263 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001264 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001265
brianosman7a5ada82016-02-08 13:49:12 -08001266 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001267 SkDebugf("Running %s\t%s\n"
1268 , bench->getUniqueName()
1269 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001270 if (FLAGS_dryRun) {
1271 continue;
1272 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001273 }
1274
egdaniel3bf92062015-06-26 08:12:46 -07001275 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001276 bench->perCanvasPreDraw(canvas);
1277
cdaltone1b89582015-06-25 19:17:08 -07001278 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001279 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001280 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1281 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001282
mtkleinbbba1682015-10-28 11:36:30 -07001283 if (FLAGS_ms) {
1284 samples.reset();
1285 auto stop = now_ms() + FLAGS_ms;
1286 do {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001287 samples.push_back(time(loops, bench.get(), target) / loops);
mtkleinbbba1682015-10-28 11:36:30 -07001288 } while (now_ms() < stop);
1289 } else {
cdaltone1b89582015-06-25 19:17:08 -07001290 samples.reset(FLAGS_samples);
1291 for (int s = 0; s < FLAGS_samples; s++) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001292 samples[s] = time(loops, bench.get(), target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001293 }
cdaltone1b89582015-06-25 19:17:08 -07001294 }
mtkleinf3723212014-06-25 14:08:00 -07001295
joshualitte45c81c2015-12-02 09:05:37 -08001296#if SK_SUPPORT_GPU
1297 SkTArray<SkString> keys;
1298 SkTArray<double> values;
1299 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1300 if (gpuStatsDump) {
1301 // TODO cache stats
1302 bench->getGpuStats(canvas, &keys, &values);
1303 }
1304#endif
1305
robertphillips5b693772014-11-21 06:19:36 -08001306 bench->perCanvasPostDraw(canvas);
1307
egdaniel3bf92062015-06-26 08:12:46 -07001308 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001309 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001310 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001311 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001312 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001313 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001314 }
1315
1316 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001317 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001318 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001319 continue;
1320 }
1321
cdaltone1b89582015-06-25 19:17:08 -07001322 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001323 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001324 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001325 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001326 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001327 log->metric("min_ms", stats.min);
George Burgess IV75b57182016-12-06 10:53:52 -08001328 log->metrics("samples", samples);
joshualitte45c81c2015-12-02 09:05:37 -08001329#if SK_SUPPORT_GPU
1330 if (gpuStatsDump) {
1331 // dump to json, only SKPBench currently returns valid keys / values
1332 SkASSERT(keys.count() == values.count());
1333 for (int i = 0; i < keys.count(); i++) {
1334 log->metric(keys[i].c_str(), values[i]);
1335 }
1336 }
1337#endif
1338
mtkleine070c2b2014-10-14 08:40:43 -07001339 if (runs++ % FLAGS_flushEvery == 0) {
1340 log->flush();
1341 }
mtklein60317d0f2014-07-14 11:30:37 -07001342
bsalomon6eb03cc2014-08-07 14:28:50 -07001343 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001344 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001345 config = ""; // Only print the config if we run the same bench on more than one.
1346 }
mtkleind75c4662015-04-30 07:11:22 -07001347 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1348 , sk_tools::getCurrResidentSetSizeMB()
1349 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001350 , bench->getUniqueName()
1351 , config);
mtkleinf3723212014-06-25 14:08:00 -07001352 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001353 const char* mark = " ";
1354 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1355 if (stddev_percent > 5) mark = "?";
1356 if (stddev_percent > 10) mark = "!";
1357
1358 SkDebugf("%10.2f %s\t%s\t%s\n",
1359 stats.median*1e3, mark, bench->getUniqueName(), config);
Mike Reedc665e5b2017-07-04 22:56:06 -04001360 } else if (FLAGS_csv) {
mtkleinf3723212014-06-25 14:08:00 -07001361 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
Mike Reedc665e5b2017-07-04 22:56:06 -04001362 SkDebugf("%g,%g,%g,%g,%g,%s,%s\n"
1363 , stats.min
1364 , stats.median
1365 , stats.mean
1366 , stats.max
1367 , stddev_percent
1368 , config
1369 , bench->getUniqueName()
1370 );
1371 } else {
1372 const char* format = "%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n";
1373 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1374 SkDebugf(format
mtkleind75c4662015-04-30 07:11:22 -07001375 , sk_tools::getCurrResidentSetSizeMB()
1376 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001377 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001378 , HUMANIZE(stats.min)
1379 , HUMANIZE(stats.median)
1380 , HUMANIZE(stats.mean)
1381 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001382 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001383 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001384 , config
mtklein96289052014-09-10 12:05:59 -07001385 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001386 );
1387 }
joshualitte45c81c2015-12-02 09:05:37 -08001388
bsalomonb12ea412015-02-02 21:19:50 -08001389#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001390 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001391 GrContext* context = gGrFactory->get(configs[i].ctxType,
csmartdaltone812d492017-02-21 12:36:05 -07001392 configs[i].ctxOverrides);
kkinnunen5219fd92015-12-10 06:28:13 -08001393 context->printCacheStats();
1394 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001395 }
1396#endif
joshualitte45c81c2015-12-02 09:05:37 -08001397
cdalton2c56ba52015-06-26 13:32:53 -07001398 if (FLAGS_verbose) {
1399 SkDebugf("Samples: ");
1400 for (int i = 0; i < samples.count(); i++) {
1401 SkDebugf("%s ", HUMANIZE(samples[i]));
1402 }
1403 SkDebugf("%s\n", bench->getUniqueName());
1404 }
egdaniel3bf92062015-06-26 08:12:46 -07001405 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001406 }
mtkleinf3723212014-06-25 14:08:00 -07001407 }
1408
Herb Derby5a523fe2017-01-26 16:48:28 -05001409 SkGraphics::PurgeAllCaches();
1410
mtkleine1091452014-12-04 10:47:02 -08001411 log->bench("memory_usage", 0,0);
1412 log->config("meta");
1413 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1414
joshualitte0b19d42015-03-26 10:41:02 -07001415#if SK_SUPPORT_GPU
1416 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1417 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001418 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001419#endif
1420
mtkleinf3723212014-06-25 14:08:00 -07001421 return 0;
1422}