blob: 95977c53cb290086aa5cc3bc61686ce3b169eedb [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"
csmartdalton008b9d82017-02-22 12:00:42 -070033#include "SkCommonFlagsPathRenderer.h"
msarett95f192d2015-02-13 09:05:41 -080034#include "SkData.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040035#include "SkDebugfTracer.h"
Brian Osman53136aa2017-07-20 15:43:35 -040036#include "SkEventTracingPriv.h"
mtkleinf3723212014-06-25 14:08:00 -070037#include "SkGraphics.h"
halcanary4dbbd042016-06-07 17:21:10 -070038#include "SkLeanWindows.h"
mtklein20840502014-08-21 15:51:22 -070039#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050040#include "SkOSPath.h"
mtklein20840502014-08-21 15:51:22 -070041#include "SkPictureRecorder.h"
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"
Brian Salomondcbb9d92017-07-19 10:53:20 -040048#include "Stats.h"
joshualitt3ebd0502016-02-09 07:18:08 -080049#include "ThermalManager.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040050#include "ios_utils.h"
mtkleinf3723212014-06-25 14:08:00 -070051
bungeman60e0fee2015-08-26 05:15:46 -070052#include <stdlib.h>
53
Mike Reedc928fe22017-06-05 10:20:31 -040054extern bool gSkForceRasterPipelineBlitter;
55
scroggo38ce0a72016-01-07 07:28:47 -080056#ifndef SK_BUILD_FOR_WIN32
57 #include <unistd.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"
Greg Daniel81e7bf82017-07-19 14:47:42 -040065 #include "SkGr.h"
bsalomon3724e572016-03-30 18:56:19 -070066 using sk_gpu_test::GrContextFactory;
bsalomonc8699322016-05-11 11:55:36 -070067 using sk_gpu_test::TestContext;
Ben Wagner145dbcd2016-11-03 14:40:50 -040068 std::unique_ptr<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070069#endif
70
bsalomon682c2692015-05-22 14:01:46 -070071 struct GrContextOptions;
72
reed53249782014-10-10 09:09:52 -070073static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070074
Mike Kleind8ee67c2017-01-19 12:14:50 -050075#if !defined(__has_feature)
76 #define __has_feature(x) 0
77#endif
78
mtkleinb5110422014-08-07 15:20:02 -070079static const int kDefaultLoops =
Mike Kleind8ee67c2017-01-19 12:14:50 -050080#if defined(SK_DEBUG) || __has_feature(address_sanitizer)
bsalomon6eb03cc2014-08-07 14:28:50 -070081 1;
mtkleina189ccd2014-07-14 12:28:47 -070082#else
bsalomon6eb03cc2014-08-07 14:28:50 -070083 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070084#endif
85
bsalomon6eb03cc2014-08-07 14:28:50 -070086static SkString loops_help_txt() {
87 SkString help;
88 help.printf("Number of times to run each bench. Set this to %d to auto-"
89 "tune for each bench. Timings are only reported when auto-tuning.",
90 kAutoTuneLoops);
91 return help;
92}
93
cdaltone1b89582015-06-25 19:17:08 -070094static SkString to_string(int n) {
95 SkString str;
96 str.appendS32(n);
97 return str;
98}
99
Ben Wagner32fa5102017-08-10 21:25:55 -0400100DECLARE_bool(undefok);
101
bsalomon6eb03cc2014-08-07 14:28:50 -0700102DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
103
mtkleinf3723212014-06-25 14:08:00 -0700104DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -0700105DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -0700106DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
107DEFINE_double(overheadGoal, 0.0001,
108 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -0700109DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -0700110DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -0700111
mtklein60317d0f2014-07-14 11:30:37 -0700112DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -0700113DEFINE_int32(maxCalibrationAttempts, 3,
114 "Try up to this many times to guess loops for a bench, or skip the bench.");
115DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700116DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
117DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700118DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
119 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700120DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
mtklein8c1a4f82016-08-08 06:56:22 -0700121DEFINE_bool(lite, false, "Use SkLiteRecorder in recording benchmarks?");
robertphillips5b693772014-11-21 06:19:36 -0800122DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700123DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700124DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800125DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800126DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800127DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
msarettc149f0e2016-01-04 11:35:43 -0800128DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
joshualitt3ebd0502016-02-09 07:18:08 -0800129DEFINE_string(useThermalManager, "0,1,10,1000", "enabled,threshold,sleepTimeMs,TimeoutMs for "
130 "thermalManager\n");
Mike Reedc665e5b2017-07-04 22:56:06 -0400131DEFINE_bool(csv, false, "Print status in CSV format");
mtklein65dfd2f2016-02-03 10:40:54 -0800132DEFINE_string(sourceType, "",
133 "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
134DEFINE_string(benchType, "",
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400135 "Apply usual --match rules to bench type: micro, recording, piping, playback, skcodec, etc.");
mtklein65dfd2f2016-02-03 10:40:54 -0800136
Mike Reedc928fe22017-06-05 10:20:31 -0400137DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
138
csmartdalton008b9d82017-02-22 12:00:42 -0700139#if SK_SUPPORT_GPU
140DEFINE_pathrenderer_flag;
141#endif
142
mtkleinbbba1682015-10-28 11:36:30 -0700143static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
144
mtkleinf3723212014-06-25 14:08:00 -0700145static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700146 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800147 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700148}
mtklein55b0ffc2014-07-17 08:38:23 -0700149#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700150
tomhudsond968a6f2015-03-26 11:28:06 -0700151bool Target::init(SkImageInfo info, Benchmark* bench) {
152 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700153 this->surface = SkSurface::MakeRaster(info);
154 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700155 return false;
156 }
157 }
158 return true;
159}
160bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700161 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700162 if (!canvas) {
163 return false;
164 }
Mike Reed12e946b2017-04-17 10:53:29 -0400165 bmp->allocPixels(canvas->imageInfo());
166 if (!canvas->readPixels(*bmp, 0, 0)) {
tomhudsond968a6f2015-03-26 11:28:06 -0700167 SkDebugf("Can't read canvas pixels.\n");
168 return false;
169 }
170 return true;
171}
172
173#if SK_SUPPORT_GPU
174struct GPUTarget : public Target {
bsalomonc8699322016-05-11 11:55:36 -0700175 explicit GPUTarget(const Config& c) : Target(c), context(nullptr) { }
176 TestContext* context;
tomhudsond968a6f2015-03-26 11:28:06 -0700177
178 void setup() override {
bsalomonc8699322016-05-11 11:55:36 -0700179 this->context->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700180 // Make sure we're done with whatever came before.
bsalomonc8699322016-05-11 11:55:36 -0700181 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700182 }
183 void endTiming() override {
bsalomonc8699322016-05-11 11:55:36 -0700184 if (this->context) {
185 this->context->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700186 }
187 }
188 void fence() override {
bsalomonc8699322016-05-11 11:55:36 -0700189 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700190 }
mtkleind75c4662015-04-30 07:11:22 -0700191
cdaltond416a5b2015-06-23 13:23:44 -0700192 bool needsFrameTiming(int* maxFrameLag) const override {
bsalomonc8699322016-05-11 11:55:36 -0700193 if (!this->context->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700194 // Frame lag is unknown.
195 *maxFrameLag = FLAGS_gpuFrameLag;
196 }
197 return true;
198 }
tomhudsond968a6f2015-03-26 11:28:06 -0700199 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700200 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
201 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700202 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700203 this->surface = SkSurface::MakeRenderTarget(gGrFactory->get(this->config.ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700204 this->config.ctxOverrides),
bsalomon5ec26ae2016-02-25 08:33:02 -0800205 SkBudgeted::kNo, info,
reede8f30622016-03-23 18:59:25 -0700206 this->config.samples, &props);
bsalomonc8699322016-05-11 11:55:36 -0700207 this->context = gGrFactory->getContextInfo(this->config.ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700208 this->config.ctxOverrides).testContext();
tomhudsond968a6f2015-03-26 11:28:06 -0700209 if (!this->surface.get()) {
210 return false;
211 }
bsalomonc8699322016-05-11 11:55:36 -0700212 if (!this->context->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700213 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800214 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700215 }
tomhudsond968a6f2015-03-26 11:28:06 -0700216 return true;
217 }
218 void fillOptions(ResultsWriter* log) override {
219 const GrGLubyte* version;
bsalomonc8699322016-05-11 11:55:36 -0700220 if (this->context->backend() == kOpenGL_GrBackend) {
221 const GrGLInterface* gl =
222 reinterpret_cast<const GrGLInterface*>(this->context->backendContext());
223 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
224 log->configOption("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700225
bsalomonc8699322016-05-11 11:55:36 -0700226 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
227 log->configOption("GL_RENDERER", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700228
bsalomonc8699322016-05-11 11:55:36 -0700229 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VENDOR));
230 log->configOption("GL_VENDOR", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700231
bsalomonc8699322016-05-11 11:55:36 -0700232 GR_GL_CALL_RET(gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
233 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
234 }
tomhudsond968a6f2015-03-26 11:28:06 -0700235 }
236};
mtkleind75c4662015-04-30 07:11:22 -0700237
tomhudsond968a6f2015-03-26 11:28:06 -0700238#endif
239
tomhudson75a0ebb2015-03-27 12:11:44 -0700240static double time(int loops, Benchmark* bench, Target* target) {
241 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700242 if (canvas) {
243 canvas->clear(SK_ColorWHITE);
244 }
joshualitt8a6697a2015-09-30 12:11:07 -0700245 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700246 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700247 canvas = target->beginTiming(canvas);
248 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700249 if (canvas) {
250 canvas->flush();
251 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700252 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700253 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700254 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700255 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700256}
257
mtkleinf3723212014-06-25 14:08:00 -0700258static double estimate_timer_overhead() {
259 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700260 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700261 double start = now_ms();
262 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700263 }
264 return overhead / FLAGS_overheadLoops;
265}
266
reed53249782014-10-10 09:09:52 -0700267static int detect_forever_loops(int loops) {
268 // look for a magic run-forever value
269 if (loops < 0) {
270 loops = SK_MaxS32;
271 }
272 return loops;
273}
274
mtklein55b0ffc2014-07-17 08:38:23 -0700275static int clamp_loops(int loops) {
276 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800277 SkDebugf("ERROR: clamping loops from %d to 1. "
278 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700279 return 1;
280 }
281 if (loops > FLAGS_maxLoops) {
282 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
283 return FLAGS_maxLoops;
284 }
285 return loops;
286}
287
tomhudsond968a6f2015-03-26 11:28:06 -0700288static bool write_canvas_png(Target* target, const SkString& filename) {
289
bsalomon6eb03cc2014-08-07 14:28:50 -0700290 if (filename.isEmpty()) {
291 return false;
292 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700293 if (target->getCanvas() &&
294 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700295 return false;
296 }
tomhudsond968a6f2015-03-26 11:28:06 -0700297
bsalomon6eb03cc2014-08-07 14:28:50 -0700298 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700299
300 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700301 return false;
302 }
tomhudsond968a6f2015-03-26 11:28:06 -0700303
bsalomon6eb03cc2014-08-07 14:28:50 -0700304 SkString dir = SkOSPath::Dirname(filename.c_str());
305 if (!sk_mkdir(dir.c_str())) {
306 SkDebugf("Can't make dir %s.\n", dir.c_str());
307 return false;
308 }
309 SkFILEWStream stream(filename.c_str());
310 if (!stream.isValid()) {
311 SkDebugf("Can't write %s.\n", filename.c_str());
312 return false;
313 }
Hal Canarydb683012016-11-23 08:55:18 -0700314 if (!SkEncodeImage(&stream, bmp, SkEncodedImageFormat::kPNG, 100)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700315 SkDebugf("Can't encode a PNG.\n");
316 return false;
317 }
318 return true;
319}
320
321static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700322static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700323 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700324 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700325 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700326 int loops = bench->calculateLoops(FLAGS_loops);
327 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700328 while (bench_plus_overhead < overhead) {
329 if (round++ == FLAGS_maxCalibrationAttempts) {
330 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700331 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700332 return kFailedLoops;
333 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700334 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700335 }
mtklein2069e222014-08-04 13:57:39 -0700336 }
mtkleinf3723212014-06-25 14:08:00 -0700337
mtkleinbb6a0282014-07-01 08:43:42 -0700338 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700339 // We'll pick N to make timer overhead negligible:
340 //
mtkleinbb6a0282014-07-01 08:43:42 -0700341 // overhead
342 // ------------------------- < FLAGS_overheadGoal
343 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700344 //
Hal Canary55325b72017-01-03 10:36:17 -0500345 // where bench_plus_overhead ~=~ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700346 //
347 // Doing some math, we get:
348 //
mtkleinbb6a0282014-07-01 08:43:42 -0700349 // (overhead / FLAGS_overheadGoal) - overhead
350 // ------------------------------------------ < N
351 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700352 //
353 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700354 if (kAutoTuneLoops == loops) {
355 const double numer = overhead / FLAGS_overheadGoal - overhead;
356 const double denom = bench_plus_overhead - overhead;
357 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700358 loops = clamp_loops(loops);
359 } else {
360 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700361 }
mtkleinbb6a0282014-07-01 08:43:42 -0700362
mtkleinbb6a0282014-07-01 08:43:42 -0700363 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700364}
365
cdaltone1b89582015-06-25 19:17:08 -0700366static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700367 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700368 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700369 if (kAutoTuneLoops == loops) {
370 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700371 double elapsed = 0;
372 do {
mtklein527930f2014-11-06 08:04:34 -0800373 if (1<<30 == loops) {
374 // We're about to wrap. Something's wrong with the bench.
375 loops = 0;
376 break;
377 }
mtkleina189ccd2014-07-14 12:28:47 -0700378 loops *= 2;
379 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700380 // _this_ round, not still timing last round.
381 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700382 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700383 }
384 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700385
mtkleina189ccd2014-07-14 12:28:47 -0700386 // We've overshot at least a little. Scale back linearly.
387 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700388 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700389
tomhudsond968a6f2015-03-26 11:28:06 -0700390 // Make sure we're not still timing our calibration.
391 target->fence();
reed53249782014-10-10 09:09:52 -0700392 } else {
393 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700394 }
mtkleinbb6a0282014-07-01 08:43:42 -0700395
396 // Pretty much the same deal as the calibration: do some warmup to make
397 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700398 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700399 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700400 }
mtkleinbb6a0282014-07-01 08:43:42 -0700401
mtkleinbb6a0282014-07-01 08:43:42 -0700402 return loops;
403}
mtkleinbb6a0282014-07-01 08:43:42 -0700404
bsalomonc2553372014-07-22 13:09:05 -0700405#if SK_SUPPORT_GPU
Brian Salomon6405e712017-03-20 08:54:16 -0400406#define kBogusContextType GrContextFactory::kGL_ContextType
csmartdaltone812d492017-02-21 12:36:05 -0700407#define kBogusContextOverrides GrContextFactory::ContextOverrides::kNone
bsalomonc2553372014-07-22 13:09:05 -0700408#else
bsalomon85b4b532016-04-05 11:06:27 -0700409#define kBogusContextType 0
csmartdaltone812d492017-02-21 12:36:05 -0700410#define kBogusContextOverrides 0
mtkleine714e752014-07-31 12:13:48 -0700411#endif
bsalomonc2553372014-07-22 13:09:05 -0700412
svaisanenc47635e2016-01-28 06:05:43 -0800413static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
414
415#if SK_SUPPORT_GPU
416 if (const auto* gpuConfig = config->asConfigGpu()) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400417 if (!FLAGS_gpu) {
418 SkDebugf("Skipping config '%s' as requested.\n", config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800419 return;
Ben Wagner32fa5102017-08-10 21:25:55 -0400420 }
svaisanenc47635e2016-01-28 06:05:43 -0800421
svaisanenc47635e2016-01-28 06:05:43 -0800422 const auto ctxType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700423 const auto ctxOverrides = gpuConfig->getContextOverrides();
svaisanenc47635e2016-01-28 06:05:43 -0800424 const auto sampleCount = gpuConfig->getSamples();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400425 const auto colorType = gpuConfig->getColorType();
426 auto colorSpace = gpuConfig->getColorSpace();
svaisanenc47635e2016-01-28 06:05:43 -0800427
csmartdaltone812d492017-02-21 12:36:05 -0700428 if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOverrides)) {
Greg Daniel81e7bf82017-07-19 14:47:42 -0400429 GrPixelConfig grPixConfig = SkImageInfo2GrPixelConfig(colorType, colorSpace,
430 *ctx->caps());
431 int supportedSampleCount = ctx->caps()->getSampleCount(sampleCount, grPixConfig);
432 if (sampleCount != supportedSampleCount) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400433 SkDebugf("Configuration '%s' sample count %d is not a supported sample count.\n",
434 config->getTag().c_str(), sampleCount);
svaisanenc47635e2016-01-28 06:05:43 -0800435 return;
436 }
437 } else {
Ben Wagner32fa5102017-08-10 21:25:55 -0400438 SkDebugf("No context was available matching config '%s'.\n",
439 config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800440 return;
441 }
442
443 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800444 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800445 Benchmark::kGPU_Backend,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400446 colorType,
svaisanenc47635e2016-01-28 06:05:43 -0800447 kPremul_SkAlphaType,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400448 sk_ref_sp(colorSpace),
svaisanenc47635e2016-01-28 06:05:43 -0800449 sampleCount,
450 ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700451 ctxOverrides,
kkinnunend4e1c352016-03-01 23:41:26 -0800452 gpuConfig->getUseDIText()
453 };
svaisanenc47635e2016-01-28 06:05:43 -0800454
455 configs->push_back(target);
456 return;
457 }
458#endif
459
brianosmanb109b8c2016-06-16 13:03:24 -0700460 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
461 if (config->getTag().equals(#name)) { \
Ben Wagner32fa5102017-08-10 21:25:55 -0400462 if (!FLAGS_cpu) { \
463 SkDebugf("Skipping config '%s' as requested.\n", \
464 config->getTag().c_str()); \
465 return; \
466 } \
brianosmanb109b8c2016-06-16 13:03:24 -0700467 Config config = { \
468 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
csmartdaltone812d492017-02-21 12:36:05 -0700469 0, kBogusContextType, kBogusContextOverrides, false \
brianosmanb109b8c2016-06-16 13:03:24 -0700470 }; \
471 configs->push_back(config); \
472 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700473 }
mtkleine714e752014-07-31 12:13:48 -0700474
Ben Wagner32fa5102017-08-10 21:25:55 -0400475 CPU_CONFIG(nonrendering, kNonRendering_Backend,
476 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800477
Mike Reede6befa52017-08-28 11:30:48 -0400478 CPU_CONFIG(a8, kRaster_Backend,
479 kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr)
Ben Wagner32fa5102017-08-10 21:25:55 -0400480 CPU_CONFIG(8888, kRaster_Backend,
481 kN32_SkColorType, kPremul_SkAlphaType, nullptr)
482 CPU_CONFIG(565, kRaster_Backend,
483 kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
484 auto srgbColorSpace = SkColorSpace::MakeSRGB();
485 CPU_CONFIG(srgb, kRaster_Backend,
486 kN32_SkColorType, kPremul_SkAlphaType, srgbColorSpace)
487 auto srgbLinearColorSpace = SkColorSpace::MakeSRGBLinear();
488 CPU_CONFIG(f16, kRaster_Backend,
489 kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbLinearColorSpace)
mtkleinbb6a0282014-07-01 08:43:42 -0700490
svaisanenc47635e2016-01-28 06:05:43 -0800491 #undef CPU_CONFIG
Ben Wagner32fa5102017-08-10 21:25:55 -0400492
493 SkDebugf("Unknown config '%s'.\n", config->getTag().c_str());
mtkleinf3723212014-06-25 14:08:00 -0700494}
495
svaisanenc47635e2016-01-28 06:05:43 -0800496// Append all configs that are enabled and supported.
497void create_configs(SkTArray<Config>* configs) {
498 SkCommandLineConfigArray array;
499 ParseConfigs(FLAGS_config, &array);
500 for (int i = 0; i < array.count(); ++i) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400501 create_config(array[i].get(), configs);
svaisanenc47635e2016-01-28 06:05:43 -0800502 }
Ben Wagner32fa5102017-08-10 21:25:55 -0400503
504 // If no just default configs were requested, then we're okay.
505 if (array.count() == 0 || FLAGS_config.count() == 0 ||
506 // If we've been told to ignore undefined flags, we're okay.
507 FLAGS_undefok ||
508 // Otherwise, make sure that all specified configs have been created.
509 array.count() == configs->count()) {
510 return;
511 }
512 SkDebugf("Invalid --config. Use --undefok to bypass this warning.\n");
513 exit(1);
svaisanenc47635e2016-01-28 06:05:43 -0800514}
515
brianosman9f1f6e22016-09-15 08:33:02 -0700516// disable warning : switch statement contains default but no 'case' labels
517#if defined _WIN32
518#pragma warning ( push )
519#pragma warning ( disable : 4065 )
520#endif
521
halcanary96fcdcc2015-08-27 07:41:13 -0700522// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700523static Target* is_enabled(Benchmark* bench, const Config& config) {
524 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700525 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700526 }
527
reede5ea5002014-09-03 11:54:58 -0700528 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700529 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700530
halcanary96fcdcc2015-08-27 07:41:13 -0700531 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700532
tomhudsond968a6f2015-03-26 11:28:06 -0700533 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700534#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700535 case Benchmark::kGPU_Backend:
536 target = new GPUTarget(config);
537 break;
bsalomonc2553372014-07-22 13:09:05 -0700538#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700539 default:
540 target = new Target(config);
541 break;
542 }
bsalomonc2553372014-07-22 13:09:05 -0700543
tomhudsond968a6f2015-03-26 11:28:06 -0700544 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700545 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700546 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700547 }
548 return target;
549}
550
brianosman9f1f6e22016-09-15 08:33:02 -0700551#if defined _WIN32
552#pragma warning ( pop )
553#endif
554
reed42943c82016-09-12 12:01:44 -0700555static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700556 uint32_t minOutputSize, int* width, int* height) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400557 std::unique_ptr<SkBitmapRegionDecoder> brd(
msarettd1227a72016-05-18 06:23:57 -0700558 SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy));
msarett7f691442015-09-22 11:56:16 -0700559 if (nullptr == brd.get()) {
560 // This is indicates that subset decoding is not supported for a particular image format.
561 return false;
562 }
563
msarett7f691442015-09-22 11:56:16 -0700564 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
565 (uint32_t) brd->height()) {
566 // This indicates that the image is not large enough to decode a
567 // minOutputSize x minOutputSize subset at the given sampleSize.
568 return false;
569 }
570
571 // Set the image width and height. The calling code will use this to choose subsets to decode.
572 *width = brd->width();
573 *height = brd->height();
574 return true;
575}
576
egdaniel3bf92062015-06-26 08:12:46 -0700577static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700578 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700579#if SK_SUPPORT_GPU
580 if (FLAGS_abandonGpuContext) {
581 gGrFactory->abandonContexts();
582 }
583 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
584 gGrFactory->destroyContexts();
585 }
586#endif
587}
588
fmalita6519c212016-09-14 08:05:17 -0700589static void collect_files(const SkCommandLineFlags::StringArray& paths, const char* ext,
590 SkTArray<SkString>* list) {
591 for (int i = 0; i < paths.count(); ++i) {
592 if (SkStrEndsWith(paths[i], ext)) {
593 list->push_back(SkString(paths[i]));
594 } else {
595 SkOSFile::Iter it(paths[i], ext);
596 SkString path;
597 while (it.next(&path)) {
598 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
599 }
600 }
601 }
602}
603
mtkleine714e752014-07-31 12:13:48 -0700604class BenchmarkStream {
605public:
mtklein92007582014-08-01 07:46:52 -0700606 BenchmarkStream() : fBenches(BenchRegistry::Head())
607 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700608 , fCurrentRecording(0)
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400609 , fCurrentPiping(0)
mtklein92007582014-08-01 07:46:52 -0700610 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800611 , fCurrentSKP(0)
fmalita6519c212016-09-14 08:05:17 -0700612 , fCurrentSVG(0)
msarett95f192d2015-02-13 09:05:41 -0800613 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700614 , fCurrentCodec(0)
msarett84451022016-02-11 06:45:51 -0800615 , fCurrentAndroidCodec(0)
msarett7f691442015-09-22 11:56:16 -0700616 , fCurrentBRDImage(0)
msarett2cee9022016-06-03 08:25:21 -0700617 , fCurrentColorImage(0)
msarett95f192d2015-02-13 09:05:41 -0800618 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800619 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700620 , fCurrentSubsetType(0)
msarett84451022016-02-11 06:45:51 -0800621 , fCurrentSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700622 , fCurrentAnimSKP(0) {
fmalita6519c212016-09-14 08:05:17 -0700623 collect_files(FLAGS_skps, ".skp", &fSKPs);
624 collect_files(FLAGS_svgs, ".svg", &fSVGs);
mtkleine714e752014-07-31 12:13:48 -0700625
mtklein92007582014-08-01 07:46:52 -0700626 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
627 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
628 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
629 exit(1);
630 }
631
632 for (int i = 0; i < FLAGS_scales.count(); i++) {
633 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
634 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
635 exit(1);
636 }
637 }
robertphillips5b693772014-11-21 06:19:36 -0800638
cdalton63a82852015-06-29 14:06:10 -0700639 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
640 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700641 exit(1);
642 }
643
robertphillips5b693772014-11-21 06:19:36 -0800644 if (FLAGS_mpd) {
645 fUseMPDs.push_back() = true;
646 }
mtkleinc751ecb2015-06-15 08:56:38 -0700647 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700648
msarett95f192d2015-02-13 09:05:41 -0800649 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700650 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800651 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800652 }
msarett2cee9022016-06-03 08:25:21 -0700653 if (!CollectImages(FLAGS_colorImages, &fColorImages)) {
654 exit(1);
655 }
mtklein95553d92015-03-12 08:24:21 -0700656
msarett95f192d2015-02-13 09:05:41 -0800657 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700658 fColorTypes.push_back(kN32_SkColorType);
659 if (!FLAGS_simpleCodec) {
660 fColorTypes.push_back(kRGB_565_SkColorType);
661 fColorTypes.push_back(kAlpha_8_SkColorType);
msarett67cb6662016-06-21 08:49:26 -0700662 fColorTypes.push_back(kGray_8_SkColorType);
663 }
mtklein92007582014-08-01 07:46:52 -0700664 }
665
reedca2622b2016-03-18 07:25:55 -0700666 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700667 // Not strictly necessary, as it will be checked again later,
668 // but helps to avoid a lot of pointless work if we're going to skip it.
cdalton91e457d2016-02-17 11:10:16 -0800669 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700670 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700671 }
672
bungemanf93d7112016-09-16 06:24:20 -0700673 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
674 if (!stream) {
mtkleinfd731ce2014-09-10 12:19:30 -0700675 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700676 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700677 }
678
reedca2622b2016-03-18 07:25:55 -0700679 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700680 }
681
fmalita6519c212016-09-14 08:05:17 -0700682 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
683 SkFILEStream stream(path);
684 if (!stream.isValid()) {
685 SkDebugf("Could not read %s.\n", path);
686 return nullptr;
687 }
688
fmalitae1baa7c2016-09-14 12:04:30 -0700689 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
fmalita6519c212016-09-14 08:05:17 -0700690 if (!svgDom) {
691 SkDebugf("Could not parse %s.\n", path);
692 return nullptr;
693 }
694
fmalitae1baa7c2016-09-14 12:04:30 -0700695 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
696 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
697 if (svgDom->containerSize().isEmpty()) {
698 svgDom->setContainerSize(kDefaultContainerSize);
699 }
700
fmalita6519c212016-09-14 08:05:17 -0700701 SkPictureRecorder recorder;
fmalitae1baa7c2016-09-14 12:04:30 -0700702 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
703 svgDom->containerSize().height()));
fmalita6519c212016-09-14 08:05:17 -0700704 return recorder.finishRecordingAsPicture();
705 }
706
mtklein92007582014-08-01 07:46:52 -0700707 Benchmark* next() {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400708 std::unique_ptr<Benchmark> bench;
mtklein65dfd2f2016-02-03 10:40:54 -0800709 do {
710 bench.reset(this->rawNext());
711 if (!bench) {
712 return nullptr;
713 }
714 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
715 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700716 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800717 }
718
719 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700720 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700721 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700722 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700723 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700724 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700725 return bench;
726 }
mtklein92007582014-08-01 07:46:52 -0700727
mtkleine714e752014-07-31 12:13:48 -0700728 while (fGMs) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400729 std::unique_ptr<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700730 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800731 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700732 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700733 fBenchType = "micro";
mtklein18300a32016-03-16 13:53:35 -0700734 return new GMBench(gm.release());
mtkleine714e752014-07-31 12:13:48 -0700735 }
736 }
mtklein92007582014-08-01 07:46:52 -0700737
mtkleinfd731ce2014-09-10 12:19:30 -0700738 // First add all .skps as RecordingBenches.
739 while (fCurrentRecording < fSKPs.count()) {
740 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700741 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
742 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700743 continue;
744 }
745 SkString name = SkOSPath::Basename(path.c_str());
746 fSourceType = "skp";
747 fBenchType = "recording";
Mike Reed90b20052017-03-08 10:39:02 -0500748 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
mtklein051e56d2014-12-04 08:46:51 -0800749 fSKPOps = pic->approximateOpCount();
mtklein8c1a4f82016-08-08 06:56:22 -0700750 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh, FLAGS_lite);
mtkleinfd731ce2014-09-10 12:19:30 -0700751 }
752
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400753 // Add all .skps as PipeBenches.
754 while (fCurrentPiping < fSKPs.count()) {
755 const SkString& path = fSKPs[fCurrentPiping++];
756 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
757 if (!pic) {
758 continue;
759 }
760 SkString name = SkOSPath::Basename(path.c_str());
761 fSourceType = "skp";
762 fBenchType = "piping";
Mike Reed90b20052017-03-08 10:39:02 -0500763 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400764 fSKPOps = pic->approximateOpCount();
765 return new PipingBench(name.c_str(), pic.get());
766 }
767
mtkleinfd731ce2014-09-10 12:19:30 -0700768 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700769 while (fCurrentScale < fScales.count()) {
770 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800771 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700772 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
773 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800774 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700775 continue;
776 }
robertphillips5b693772014-11-21 06:19:36 -0800777
778 while (fCurrentUseMPD < fUseMPDs.count()) {
779 if (FLAGS_bbh) {
780 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
781 SkRTreeFactory factory;
782 SkPictureRecorder recorder;
robertphillips5b693772014-11-21 06:19:36 -0800783 pic->playback(recorder.beginRecording(pic->cullRect().width(),
784 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800785 &factory,
robertphillipsdda54452016-07-13 13:27:16 -0700786 0));
reedca2622b2016-03-18 07:25:55 -0700787 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800788 }
789 SkString name = SkOSPath::Basename(path.c_str());
790 fSourceType = "skp";
791 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700792 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
793 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700794 }
robertphillips5b693772014-11-21 06:19:36 -0800795 fCurrentUseMPD = 0;
796 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700797 }
fmalita6519c212016-09-14 08:05:17 -0700798
799 while (fCurrentSVG++ < fSVGs.count()) {
800 const char* path = fSVGs[fCurrentSVG - 1].c_str();
801 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
802 fSourceType = "svg";
803 fBenchType = "playback";
804 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
805 fScales[fCurrentScale], false, FLAGS_loopSKP);
806 }
807 }
808
mtklein92007582014-08-01 07:46:52 -0700809 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700810 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700811 fCurrentScale++;
812 }
813
joshualitt261c3ad2015-04-27 09:16:57 -0700814 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700815 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700816 while (fCurrentAnimSKP < fSKPs.count()) {
817 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700818 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
819 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700820 fCurrentAnimSKP++;
821 continue;
822 }
823
824 fCurrentAnimSKP++;
825 SkString name = SkOSPath::Basename(path.c_str());
Hal Canary2db83612016-11-04 13:02:54 -0400826 sk_sp<SKPAnimationBench::Animation> animation(
cdalton63a82852015-06-29 14:06:10 -0700827 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
Hal Canary2db83612016-11-04 13:02:54 -0400828 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation.get(),
halcanary385fe4d2015-08-26 13:07:48 -0700829 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700830 }
831 }
832
scroggo60869a42015-04-01 12:09:17 -0700833 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700834 fSourceType = "image";
835 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700836 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800837 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
838 continue;
839 }
bungeman38d909e2016-08-02 14:40:46 -0700840 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400841 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700842 if (!codec) {
843 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700844 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700845 continue;
846 }
scroggo21027992015-04-02 13:22:38 -0700847
scroggo60869a42015-04-01 12:09:17 -0700848 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700849 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700850
msarettc7796b92016-01-07 14:20:20 -0800851 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700852 if (FLAGS_simpleCodec) {
853 if (kUnpremul_SkAlphaType == alphaType) {
854 alphaType = kPremul_SkAlphaType;
855 }
856
857 fCurrentColorType++;
858 } else {
859 switch (alphaType) {
860 case kOpaque_SkAlphaType:
861 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800862 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700863 break;
864 case kUnpremul_SkAlphaType:
865 case kPremul_SkAlphaType:
866 if (0 == fCurrentAlphaType) {
867 // Test unpremul first.
868 alphaType = kUnpremul_SkAlphaType;
869 fCurrentAlphaType++;
870 } else {
871 // Test premul.
872 alphaType = kPremul_SkAlphaType;
873 fCurrentAlphaType = 0;
874 fCurrentColorType++;
875 }
876 break;
877 default:
878 SkASSERT(false);
879 fCurrentColorType++;
880 break;
881 }
scroggo21027992015-04-02 13:22:38 -0700882 }
883
msarettc7796b92016-01-07 14:20:20 -0800884 // Make sure we can decode to this color type and alpha type.
885 SkImageInfo info =
886 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700887 const size_t rowBytes = info.minRowBytes();
888 SkAutoMalloc storage(info.getSafeSize(rowBytes));
889
scroggoeb602a52015-07-09 08:16:03 -0700890 const SkCodec::Result result = codec->getPixels(
Leon Scroggins571b30f2017-07-11 17:35:31 +0000891 info, storage.get(), rowBytes);
scroggo60869a42015-04-01 12:09:17 -0700892 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700893 case SkCodec::kSuccess:
894 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700895 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700896 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700897 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700898 // This is okay. Not all conversions are valid.
899 break;
scroggo60869a42015-04-01 12:09:17 -0700900 default:
901 // This represents some sort of failure.
902 SkASSERT(false);
903 break;
904 }
905 }
906 fCurrentColorType = 0;
907 }
908
msarett84451022016-02-11 06:45:51 -0800909 // Run AndroidCodecBenches
910 const int sampleSizes[] = { 2, 4, 8 };
911 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
912 fSourceType = "image";
913 fBenchType = "skandroidcodec";
914
915 const SkString& path = fImages[fCurrentAndroidCodec];
916 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
917 continue;
918 }
bungeman38d909e2016-08-02 14:40:46 -0700919 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400920 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800921 if (!codec) {
922 // Nothing to time.
923 SkDebugf("Cannot find codec for %s\n", path.c_str());
924 continue;
925 }
926
927 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
928 int sampleSize = sampleSizes[fCurrentSampleSize];
929 fCurrentSampleSize++;
930 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
931 // Avoid benchmarking scaled decodes of already small images.
932 break;
933 }
934
bungeman38d909e2016-08-02 14:40:46 -0700935 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
936 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -0800937 }
938 fCurrentSampleSize = 0;
939 }
940
msarett7f691442015-09-22 11:56:16 -0700941 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700942 // We intend to create benchmarks that model the use cases in
943 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
944 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
945 // the image. For that reason, we will benchmark decodes in five representative locations
946 // in the image. Additionally, this use case utilizes power of two scaling, so we will
947 // test on power of two sample sizes. The output tile is always 512x512, so, when a
948 // sampleSize is used, the size of the subset that is decoded is always
949 // (sampleSize*512)x(sampleSize*512).
950 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700951 // All use cases we are aware of only scale by powers of two.
952 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
953 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800954 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700955 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800956 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700957 fSourceType = "image";
958 fBenchType = "BRD";
959
mtklein6f0ff912016-01-11 09:04:21 -0800960 const SkString& path = fImages[fCurrentBRDImage];
961 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
962 continue;
963 }
scroggo860e8a62015-10-15 07:51:28 -0700964
msarettd1227a72016-05-18 06:23:57 -0700965 while (fCurrentColorType < fColorTypes.count()) {
966 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
967 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700968
bungeman38d909e2016-08-02 14:40:46 -0700969 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -0700970 const SkColorType colorType = fColorTypes[fCurrentColorType];
971 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
972 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -0700973
msarettd1227a72016-05-18 06:23:57 -0700974 int width = 0;
975 int height = 0;
reed42943c82016-09-12 12:01:44 -0700976 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -0700977 &width, &height)) {
978 break;
msarett7f691442015-09-22 11:56:16 -0700979 }
msarettd1227a72016-05-18 06:23:57 -0700980
981 SkString basename = SkOSPath::Basename(path.c_str());
982 SkIRect subset;
983 const uint32_t subsetSize = sampleSize * minOutputSize;
984 switch (currentSubsetType) {
985 case kTopLeft_SubsetType:
986 basename.append("_TopLeft");
987 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
988 break;
989 case kTopRight_SubsetType:
990 basename.append("_TopRight");
991 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
992 subsetSize);
993 break;
994 case kMiddle_SubsetType:
995 basename.append("_Middle");
996 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
997 (height - subsetSize) / 2, subsetSize, subsetSize);
998 break;
999 case kBottomLeft_SubsetType:
1000 basename.append("_BottomLeft");
1001 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
1002 subsetSize);
1003 break;
1004 case kBottomRight_SubsetType:
1005 basename.append("_BottomRight");
1006 subset = SkIRect::MakeXYWH(width - subsetSize,
1007 height - subsetSize, subsetSize, subsetSize);
1008 break;
1009 default:
1010 SkASSERT(false);
1011 }
1012
1013 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
1014 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -07001015 }
msarettd1227a72016-05-18 06:23:57 -07001016 fCurrentSubsetType = 0;
1017 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -07001018 }
msarettd1227a72016-05-18 06:23:57 -07001019 fCurrentSampleSize = 0;
1020 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -07001021 }
msarettd1227a72016-05-18 06:23:57 -07001022 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -07001023 }
1024
msarett2cee9022016-06-03 08:25:21 -07001025 while (fCurrentColorImage < fColorImages.count()) {
1026 fSourceType = "colorimage";
1027 fBenchType = "skcolorcodec";
1028 const SkString& path = fColorImages[fCurrentColorImage];
1029 fCurrentColorImage++;
1030 sk_sp<SkData> encoded = SkData::MakeFromFileName(path.c_str());
1031 if (encoded) {
1032 return new ColorCodecBench(SkOSPath::Basename(path.c_str()).c_str(),
1033 std::move(encoded));
1034 } else {
1035 SkDebugf("Could not read file %s.\n", path.c_str());
1036 }
1037 }
1038
halcanary96fcdcc2015-08-27 07:41:13 -07001039 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001040 }
mtklein92007582014-08-01 07:46:52 -07001041
1042 void fillCurrentOptions(ResultsWriter* log) const {
1043 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001044 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001045 if (0 == strcmp(fSourceType, "skp")) {
1046 log->configOption("clip",
1047 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1048 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001049 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -07001050 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001051 if (fCurrentUseMPD > 0) {
1052 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1053 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1054 }
mtklein92007582014-08-01 07:46:52 -07001055 }
mtklein051e56d2014-12-04 08:46:51 -08001056 if (0 == strcmp(fBenchType, "recording")) {
1057 log->metric("bytes", fSKPBytes);
1058 log->metric("ops", fSKPOps);
1059 }
mtklein92007582014-08-01 07:46:52 -07001060 }
1061
mtkleine714e752014-07-31 12:13:48 -07001062private:
msarettb23e6aa2015-06-09 13:56:10 -07001063 enum SubsetType {
1064 kTopLeft_SubsetType = 0,
1065 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001066 kMiddle_SubsetType = 2,
1067 kBottomLeft_SubsetType = 3,
1068 kBottomRight_SubsetType = 4,
1069 kTranslate_SubsetType = 5,
1070 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001071 kLast_SubsetType = kZoom_SubsetType,
1072 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001073 };
1074
mtkleine714e752014-07-31 12:13:48 -07001075 const BenchRegistry* fBenches;
1076 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001077 SkIRect fClip;
1078 SkTArray<SkScalar> fScales;
1079 SkTArray<SkString> fSKPs;
fmalita6519c212016-09-14 08:05:17 -07001080 SkTArray<SkString> fSVGs;
robertphillips5b693772014-11-21 06:19:36 -08001081 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001082 SkTArray<SkString> fImages;
msarett2cee9022016-06-03 08:25:21 -07001083 SkTArray<SkString> fColorImages;
msarett74deb982015-10-20 16:45:56 -07001084 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001085 SkScalar fZoomMax;
1086 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001087
mtklein051e56d2014-12-04 08:46:51 -08001088 double fSKPBytes, fSKPOps;
1089
mtkleinfd731ce2014-09-10 12:19:30 -07001090 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1091 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1092 int fCurrentRecording;
Mike Reed9cdd2ab2016-10-21 10:43:36 -04001093 int fCurrentPiping;
mtklein92007582014-08-01 07:46:52 -07001094 int fCurrentScale;
1095 int fCurrentSKP;
fmalita6519c212016-09-14 08:05:17 -07001096 int fCurrentSVG;
robertphillips5b693772014-11-21 06:19:36 -08001097 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001098 int fCurrentCodec;
msarett84451022016-02-11 06:45:51 -08001099 int fCurrentAndroidCodec;
msarett7f691442015-09-22 11:56:16 -07001100 int fCurrentBRDImage;
msarett2cee9022016-06-03 08:25:21 -07001101 int fCurrentColorImage;
msarett95f192d2015-02-13 09:05:41 -08001102 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -08001103 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -07001104 int fCurrentSubsetType;
msarett84451022016-02-11 06:45:51 -08001105 int fCurrentSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001106 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001107};
1108
msarettc149f0e2016-01-04 11:35:43 -08001109// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1110// This prints something every once in a while so that it knows we're still working.
1111static void start_keepalive() {
1112 struct Loop {
1113 static void forever(void*) {
1114 for (;;) {
1115 static const int kSec = 1200;
1116 #if defined(SK_BUILD_FOR_WIN)
1117 Sleep(kSec * 1000);
1118 #else
1119 sleep(kSec);
1120 #endif
1121 SkDebugf("\nBenchmarks still running...\n");
1122 }
1123 }
1124 };
1125 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1126 intentionallyLeaked->start();
1127}
1128
Mike Kleinbe28ee22017-02-06 12:46:20 -05001129int main(int argc, char** argv) {
1130 SkCommandLineFlags::Parse(argc, argv);
Brian Osman53136aa2017-07-20 15:43:35 -04001131
Brian Osmanbc8150f2017-07-24 11:38:01 -04001132 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -04001133
Mike Kleinadacaef2017-02-06 09:26:14 -05001134#if defined(SK_BUILD_FOR_IOS)
1135 cd_Documents();
1136#endif
jcgregorio3b27ade2014-11-13 08:06:40 -08001137 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001138 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001139 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001140
krajcevski69a55602014-08-13 10:46:31 -07001141#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001142 GrContextOptions grContextOpts;
csmartdalton008b9d82017-02-22 12:00:42 -07001143 grContextOpts.fGpuPathRenderers = CollectGpuPathRenderersFromFlags();
halcanary385fe4d2015-08-26 13:07:48 -07001144 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001145#endif
1146
bsalomon06cddec2014-10-24 10:40:50 -07001147 if (FLAGS_veryVerbose) {
1148 FLAGS_verbose = true;
1149 }
1150
bsalomon6eb03cc2014-08-07 14:28:50 -07001151 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001152 FLAGS_samples = 1;
1153 FLAGS_gpuFrameLag = 0;
1154 }
1155
bsalomon6eb03cc2014-08-07 14:28:50 -07001156 if (!FLAGS_writePath.isEmpty()) {
1157 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1158 if (!sk_mkdir(FLAGS_writePath[0])) {
1159 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001160 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001161 }
1162 }
1163
Ben Wagner145dbcd2016-11-03 14:40:50 -04001164 std::unique_ptr<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001165 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001166#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001167 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001168#else
1169 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1170 return 1;
1171#endif
mtklein60317d0f2014-07-14 11:30:37 -07001172 }
mtklein1915b622014-08-20 11:45:00 -07001173
1174 if (1 == FLAGS_properties.count() % 2) {
1175 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1176 return 1;
1177 }
1178 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1179 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1180 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001181
1182 if (1 == FLAGS_key.count() % 2) {
1183 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1184 return 1;
1185 }
1186 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001187 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001188 }
mtklein60317d0f2014-07-14 11:30:37 -07001189
mtkleinf3723212014-06-25 14:08:00 -07001190 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001191 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001192
cdaltone1b89582015-06-25 19:17:08 -07001193 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001194
bsalomon6eb03cc2014-08-07 14:28:50 -07001195 if (kAutoTuneLoops != FLAGS_loops) {
1196 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001197 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001198 SkDebugf("! -> high variance, ? -> moderate variance\n");
1199 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001200 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001201 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001202 } else {
mtkleind75c4662015-04-30 07:11:22 -07001203 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001204 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001205 }
1206
svaisanenc47635e2016-01-28 06:05:43 -08001207 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001208 create_configs(&configs);
1209
joshualitt3ebd0502016-02-09 07:18:08 -08001210#ifdef THERMAL_MANAGER_SUPPORTED
1211 int tmEnabled, tmThreshold, tmSleepTimeMs, tmTimeoutMs;
1212 if (4 != sscanf(FLAGS_useThermalManager[0], "%d,%d,%d,%d",
1213 &tmEnabled, &tmThreshold, &tmSleepTimeMs, &tmTimeoutMs)) {
1214 SkDebugf("Can't parse %s from --useThermalManager.\n", FLAGS_useThermalManager[0]);
1215 exit(1);
1216 }
1217 ThermalManager tm(tmThreshold, tmSleepTimeMs, tmTimeoutMs);
1218#endif
1219
msarettc149f0e2016-01-04 11:35:43 -08001220 if (FLAGS_keepAlive) {
1221 start_keepalive();
1222 }
1223
Yuqian Liba62b4a2016-12-14 13:46:53 -05001224 gSkUseAnalyticAA = FLAGS_analyticAA;
Yuqian Lidf60e362017-07-25 11:26:31 -04001225 gSkUseDeltaAA = FLAGS_deltaAA;
liyuqian38911a72016-10-04 11:23:22 -07001226
Yuqian Lidf60e362017-07-25 11:26:31 -04001227 if (FLAGS_forceDeltaAA) {
1228 gSkForceDeltaAA = true;
1229 }
Yuqian Li550148b2017-01-13 10:13:13 -05001230 if (FLAGS_forceAnalyticAA) {
1231 gSkForceAnalyticAA = true;
1232 }
Mike Reedc928fe22017-06-05 10:20:31 -04001233 if (FLAGS_forceRasterPipeline) {
1234 gSkForceRasterPipelineBlitter = true;
1235 }
Yuqian Li550148b2017-01-13 10:13:13 -05001236
mtkleine070c2b2014-10-14 08:40:43 -07001237 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001238 BenchmarkStream benchStream;
1239 while (Benchmark* b = benchStream.next()) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001240 std::unique_ptr<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001241 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001242 continue;
1243 }
1244
svaisanenc47635e2016-01-28 06:05:43 -08001245 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001246 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001247 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001248 }
egdaniel3bf92062015-06-26 08:12:46 -07001249 for (int i = 0; i < configs.count(); ++i) {
joshualitt3ebd0502016-02-09 07:18:08 -08001250#ifdef THERMAL_MANAGER_SUPPORTED
1251 if (tmEnabled && !tm.coolOffIfNecessary()) {
1252 SkDebugf("Could not cool off, timings will be throttled\n");
1253 }
1254#endif
egdaniel3bf92062015-06-26 08:12:46 -07001255 Target* target = is_enabled(b, configs[i]);
1256 if (!target) {
1257 continue;
1258 }
mtkleinf3723212014-06-25 14:08:00 -07001259
halcanary96fcdcc2015-08-27 07:41:13 -07001260 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001261 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001262 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001263
brianosman7a5ada82016-02-08 13:49:12 -08001264 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001265 SkDebugf("Running %s\t%s\n"
1266 , bench->getUniqueName()
1267 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001268 if (FLAGS_dryRun) {
1269 continue;
1270 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001271 }
1272
egdaniel3bf92062015-06-26 08:12:46 -07001273 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001274 bench->perCanvasPreDraw(canvas);
1275
cdaltone1b89582015-06-25 19:17:08 -07001276 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001277 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001278 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1279 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001280
mtkleinbbba1682015-10-28 11:36:30 -07001281 if (FLAGS_ms) {
1282 samples.reset();
1283 auto stop = now_ms() + FLAGS_ms;
1284 do {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001285 samples.push_back(time(loops, bench.get(), target) / loops);
mtkleinbbba1682015-10-28 11:36:30 -07001286 } while (now_ms() < stop);
1287 } else {
cdaltone1b89582015-06-25 19:17:08 -07001288 samples.reset(FLAGS_samples);
1289 for (int s = 0; s < FLAGS_samples; s++) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001290 samples[s] = time(loops, bench.get(), target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001291 }
cdaltone1b89582015-06-25 19:17:08 -07001292 }
mtkleinf3723212014-06-25 14:08:00 -07001293
joshualitte45c81c2015-12-02 09:05:37 -08001294#if SK_SUPPORT_GPU
1295 SkTArray<SkString> keys;
1296 SkTArray<double> values;
1297 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1298 if (gpuStatsDump) {
1299 // TODO cache stats
1300 bench->getGpuStats(canvas, &keys, &values);
1301 }
1302#endif
1303
robertphillips5b693772014-11-21 06:19:36 -08001304 bench->perCanvasPostDraw(canvas);
1305
egdaniel3bf92062015-06-26 08:12:46 -07001306 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001307 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001308 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001309 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001310 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001311 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001312 }
1313
1314 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001315 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001316 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001317 continue;
1318 }
1319
cdaltone1b89582015-06-25 19:17:08 -07001320 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001321 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001322 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001323 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001324 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001325 log->metric("min_ms", stats.min);
George Burgess IV75b57182016-12-06 10:53:52 -08001326 log->metrics("samples", samples);
joshualitte45c81c2015-12-02 09:05:37 -08001327#if SK_SUPPORT_GPU
1328 if (gpuStatsDump) {
1329 // dump to json, only SKPBench currently returns valid keys / values
1330 SkASSERT(keys.count() == values.count());
1331 for (int i = 0; i < keys.count(); i++) {
1332 log->metric(keys[i].c_str(), values[i]);
1333 }
1334 }
1335#endif
1336
mtkleine070c2b2014-10-14 08:40:43 -07001337 if (runs++ % FLAGS_flushEvery == 0) {
1338 log->flush();
1339 }
mtklein60317d0f2014-07-14 11:30:37 -07001340
bsalomon6eb03cc2014-08-07 14:28:50 -07001341 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001342 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001343 config = ""; // Only print the config if we run the same bench on more than one.
1344 }
mtkleind75c4662015-04-30 07:11:22 -07001345 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1346 , sk_tools::getCurrResidentSetSizeMB()
1347 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001348 , bench->getUniqueName()
1349 , config);
mtkleinf3723212014-06-25 14:08:00 -07001350 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001351 const char* mark = " ";
1352 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1353 if (stddev_percent > 5) mark = "?";
1354 if (stddev_percent > 10) mark = "!";
1355
1356 SkDebugf("%10.2f %s\t%s\t%s\n",
1357 stats.median*1e3, mark, bench->getUniqueName(), config);
Mike Reedc665e5b2017-07-04 22:56:06 -04001358 } else if (FLAGS_csv) {
mtkleinf3723212014-06-25 14:08:00 -07001359 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
Mike Reedc665e5b2017-07-04 22:56:06 -04001360 SkDebugf("%g,%g,%g,%g,%g,%s,%s\n"
1361 , stats.min
1362 , stats.median
1363 , stats.mean
1364 , stats.max
1365 , stddev_percent
1366 , config
1367 , bench->getUniqueName()
1368 );
1369 } else {
1370 const char* format = "%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n";
1371 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1372 SkDebugf(format
mtkleind75c4662015-04-30 07:11:22 -07001373 , sk_tools::getCurrResidentSetSizeMB()
1374 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001375 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001376 , HUMANIZE(stats.min)
1377 , HUMANIZE(stats.median)
1378 , HUMANIZE(stats.mean)
1379 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001380 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001381 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001382 , config
mtklein96289052014-09-10 12:05:59 -07001383 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001384 );
1385 }
joshualitte45c81c2015-12-02 09:05:37 -08001386
bsalomonb12ea412015-02-02 21:19:50 -08001387#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001388 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001389 GrContext* context = gGrFactory->get(configs[i].ctxType,
csmartdaltone812d492017-02-21 12:36:05 -07001390 configs[i].ctxOverrides);
kkinnunen5219fd92015-12-10 06:28:13 -08001391 context->printCacheStats();
1392 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001393 }
1394#endif
joshualitte45c81c2015-12-02 09:05:37 -08001395
cdalton2c56ba52015-06-26 13:32:53 -07001396 if (FLAGS_verbose) {
1397 SkDebugf("Samples: ");
1398 for (int i = 0; i < samples.count(); i++) {
1399 SkDebugf("%s ", HUMANIZE(samples[i]));
1400 }
1401 SkDebugf("%s\n", bench->getUniqueName());
1402 }
egdaniel3bf92062015-06-26 08:12:46 -07001403 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001404 }
mtkleinf3723212014-06-25 14:08:00 -07001405 }
1406
Herb Derby5a523fe2017-01-26 16:48:28 -05001407 SkGraphics::PurgeAllCaches();
1408
mtkleine1091452014-12-04 10:47:02 -08001409 log->bench("memory_usage", 0,0);
1410 log->config("meta");
1411 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1412
joshualitte0b19d42015-03-26 10:41:02 -07001413#if SK_SUPPORT_GPU
1414 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1415 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001416 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001417#endif
1418
mtkleinf3723212014-06-25 14:08:00 -07001419 return 0;
1420}