blob: 196cb220d00b9dd463853cdb76097557fef26541 [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
bsalomon6eb03cc2014-08-07 14:28:50 -0700100DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
101
mtkleinf3723212014-06-25 14:08:00 -0700102DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -0700103DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -0700104DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
105DEFINE_double(overheadGoal, 0.0001,
106 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -0700107DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -0700108DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -0700109
mtklein60317d0f2014-07-14 11:30:37 -0700110DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -0700111DEFINE_int32(maxCalibrationAttempts, 3,
112 "Try up to this many times to guess loops for a bench, or skip the bench.");
113DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700114DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
115DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700116DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
117 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700118DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
mtklein8c1a4f82016-08-08 06:56:22 -0700119DEFINE_bool(lite, false, "Use SkLiteRecorder in recording benchmarks?");
robertphillips5b693772014-11-21 06:19:36 -0800120DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700121DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700122DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800123DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800124DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800125DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
msarettc149f0e2016-01-04 11:35:43 -0800126DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
joshualitt3ebd0502016-02-09 07:18:08 -0800127DEFINE_string(useThermalManager, "0,1,10,1000", "enabled,threshold,sleepTimeMs,TimeoutMs for "
128 "thermalManager\n");
Mike Reedc665e5b2017-07-04 22:56:06 -0400129DEFINE_bool(csv, false, "Print status in CSV format");
mtklein65dfd2f2016-02-03 10:40:54 -0800130DEFINE_string(sourceType, "",
131 "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
132DEFINE_string(benchType, "",
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400133 "Apply usual --match rules to bench type: micro, recording, piping, playback, skcodec, etc.");
mtklein65dfd2f2016-02-03 10:40:54 -0800134
Mike Reedc928fe22017-06-05 10:20:31 -0400135DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
136
csmartdalton008b9d82017-02-22 12:00:42 -0700137#if SK_SUPPORT_GPU
138DEFINE_pathrenderer_flag;
139#endif
140
mtkleinbbba1682015-10-28 11:36:30 -0700141static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
142
mtkleinf3723212014-06-25 14:08:00 -0700143static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700144 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800145 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700146}
mtklein55b0ffc2014-07-17 08:38:23 -0700147#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700148
tomhudsond968a6f2015-03-26 11:28:06 -0700149bool Target::init(SkImageInfo info, Benchmark* bench) {
150 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700151 this->surface = SkSurface::MakeRaster(info);
152 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700153 return false;
154 }
155 }
156 return true;
157}
158bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700159 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700160 if (!canvas) {
161 return false;
162 }
Mike Reed12e946b2017-04-17 10:53:29 -0400163 bmp->allocPixels(canvas->imageInfo());
164 if (!canvas->readPixels(*bmp, 0, 0)) {
tomhudsond968a6f2015-03-26 11:28:06 -0700165 SkDebugf("Can't read canvas pixels.\n");
166 return false;
167 }
168 return true;
169}
170
171#if SK_SUPPORT_GPU
172struct GPUTarget : public Target {
bsalomonc8699322016-05-11 11:55:36 -0700173 explicit GPUTarget(const Config& c) : Target(c), context(nullptr) { }
174 TestContext* context;
tomhudsond968a6f2015-03-26 11:28:06 -0700175
176 void setup() override {
bsalomonc8699322016-05-11 11:55:36 -0700177 this->context->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700178 // Make sure we're done with whatever came before.
bsalomonc8699322016-05-11 11:55:36 -0700179 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700180 }
181 void endTiming() override {
bsalomonc8699322016-05-11 11:55:36 -0700182 if (this->context) {
183 this->context->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700184 }
185 }
186 void fence() override {
bsalomonc8699322016-05-11 11:55:36 -0700187 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700188 }
mtkleind75c4662015-04-30 07:11:22 -0700189
cdaltond416a5b2015-06-23 13:23:44 -0700190 bool needsFrameTiming(int* maxFrameLag) const override {
bsalomonc8699322016-05-11 11:55:36 -0700191 if (!this->context->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700192 // Frame lag is unknown.
193 *maxFrameLag = FLAGS_gpuFrameLag;
194 }
195 return true;
196 }
tomhudsond968a6f2015-03-26 11:28:06 -0700197 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700198 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
199 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700200 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700201 this->surface = SkSurface::MakeRenderTarget(gGrFactory->get(this->config.ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700202 this->config.ctxOverrides),
bsalomon5ec26ae2016-02-25 08:33:02 -0800203 SkBudgeted::kNo, info,
reede8f30622016-03-23 18:59:25 -0700204 this->config.samples, &props);
bsalomonc8699322016-05-11 11:55:36 -0700205 this->context = gGrFactory->getContextInfo(this->config.ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700206 this->config.ctxOverrides).testContext();
tomhudsond968a6f2015-03-26 11:28:06 -0700207 if (!this->surface.get()) {
208 return false;
209 }
bsalomonc8699322016-05-11 11:55:36 -0700210 if (!this->context->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700211 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800212 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700213 }
tomhudsond968a6f2015-03-26 11:28:06 -0700214 return true;
215 }
216 void fillOptions(ResultsWriter* log) override {
217 const GrGLubyte* version;
bsalomonc8699322016-05-11 11:55:36 -0700218 if (this->context->backend() == kOpenGL_GrBackend) {
219 const GrGLInterface* gl =
220 reinterpret_cast<const GrGLInterface*>(this->context->backendContext());
221 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
222 log->configOption("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700223
bsalomonc8699322016-05-11 11:55:36 -0700224 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
225 log->configOption("GL_RENDERER", (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_VENDOR));
228 log->configOption("GL_VENDOR", (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_SHADING_LANGUAGE_VERSION));
231 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
232 }
tomhudsond968a6f2015-03-26 11:28:06 -0700233 }
234};
mtkleind75c4662015-04-30 07:11:22 -0700235
tomhudsond968a6f2015-03-26 11:28:06 -0700236#endif
237
tomhudson75a0ebb2015-03-27 12:11:44 -0700238static double time(int loops, Benchmark* bench, Target* target) {
239 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700240 if (canvas) {
241 canvas->clear(SK_ColorWHITE);
242 }
joshualitt8a6697a2015-09-30 12:11:07 -0700243 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700244 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700245 canvas = target->beginTiming(canvas);
246 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700247 if (canvas) {
248 canvas->flush();
249 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700250 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700251 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700252 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700253 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700254}
255
mtkleinf3723212014-06-25 14:08:00 -0700256static double estimate_timer_overhead() {
257 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700258 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700259 double start = now_ms();
260 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700261 }
262 return overhead / FLAGS_overheadLoops;
263}
264
reed53249782014-10-10 09:09:52 -0700265static int detect_forever_loops(int loops) {
266 // look for a magic run-forever value
267 if (loops < 0) {
268 loops = SK_MaxS32;
269 }
270 return loops;
271}
272
mtklein55b0ffc2014-07-17 08:38:23 -0700273static int clamp_loops(int loops) {
274 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800275 SkDebugf("ERROR: clamping loops from %d to 1. "
276 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700277 return 1;
278 }
279 if (loops > FLAGS_maxLoops) {
280 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
281 return FLAGS_maxLoops;
282 }
283 return loops;
284}
285
tomhudsond968a6f2015-03-26 11:28:06 -0700286static bool write_canvas_png(Target* target, const SkString& filename) {
287
bsalomon6eb03cc2014-08-07 14:28:50 -0700288 if (filename.isEmpty()) {
289 return false;
290 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700291 if (target->getCanvas() &&
292 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700293 return false;
294 }
tomhudsond968a6f2015-03-26 11:28:06 -0700295
bsalomon6eb03cc2014-08-07 14:28:50 -0700296 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700297
298 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700299 return false;
300 }
tomhudsond968a6f2015-03-26 11:28:06 -0700301
bsalomon6eb03cc2014-08-07 14:28:50 -0700302 SkString dir = SkOSPath::Dirname(filename.c_str());
303 if (!sk_mkdir(dir.c_str())) {
304 SkDebugf("Can't make dir %s.\n", dir.c_str());
305 return false;
306 }
307 SkFILEWStream stream(filename.c_str());
308 if (!stream.isValid()) {
309 SkDebugf("Can't write %s.\n", filename.c_str());
310 return false;
311 }
Hal Canarydb683012016-11-23 08:55:18 -0700312 if (!SkEncodeImage(&stream, bmp, SkEncodedImageFormat::kPNG, 100)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700313 SkDebugf("Can't encode a PNG.\n");
314 return false;
315 }
316 return true;
317}
318
319static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700320static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700321 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700322 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700323 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700324 int loops = bench->calculateLoops(FLAGS_loops);
325 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700326 while (bench_plus_overhead < overhead) {
327 if (round++ == FLAGS_maxCalibrationAttempts) {
328 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700329 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700330 return kFailedLoops;
331 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700332 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700333 }
mtklein2069e222014-08-04 13:57:39 -0700334 }
mtkleinf3723212014-06-25 14:08:00 -0700335
mtkleinbb6a0282014-07-01 08:43:42 -0700336 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700337 // We'll pick N to make timer overhead negligible:
338 //
mtkleinbb6a0282014-07-01 08:43:42 -0700339 // overhead
340 // ------------------------- < FLAGS_overheadGoal
341 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700342 //
Hal Canary55325b72017-01-03 10:36:17 -0500343 // where bench_plus_overhead ~=~ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700344 //
345 // Doing some math, we get:
346 //
mtkleinbb6a0282014-07-01 08:43:42 -0700347 // (overhead / FLAGS_overheadGoal) - overhead
348 // ------------------------------------------ < N
349 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700350 //
351 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700352 if (kAutoTuneLoops == loops) {
353 const double numer = overhead / FLAGS_overheadGoal - overhead;
354 const double denom = bench_plus_overhead - overhead;
355 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700356 loops = clamp_loops(loops);
357 } else {
358 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700359 }
mtkleinbb6a0282014-07-01 08:43:42 -0700360
mtkleinbb6a0282014-07-01 08:43:42 -0700361 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700362}
363
cdaltone1b89582015-06-25 19:17:08 -0700364static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700365 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700366 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700367 if (kAutoTuneLoops == loops) {
368 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700369 double elapsed = 0;
370 do {
mtklein527930f2014-11-06 08:04:34 -0800371 if (1<<30 == loops) {
372 // We're about to wrap. Something's wrong with the bench.
373 loops = 0;
374 break;
375 }
mtkleina189ccd2014-07-14 12:28:47 -0700376 loops *= 2;
377 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700378 // _this_ round, not still timing last round.
379 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700380 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700381 }
382 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700383
mtkleina189ccd2014-07-14 12:28:47 -0700384 // We've overshot at least a little. Scale back linearly.
385 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700386 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700387
tomhudsond968a6f2015-03-26 11:28:06 -0700388 // Make sure we're not still timing our calibration.
389 target->fence();
reed53249782014-10-10 09:09:52 -0700390 } else {
391 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700392 }
mtkleinbb6a0282014-07-01 08:43:42 -0700393
394 // Pretty much the same deal as the calibration: do some warmup to make
395 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700396 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700397 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700398 }
mtkleinbb6a0282014-07-01 08:43:42 -0700399
mtkleinbb6a0282014-07-01 08:43:42 -0700400 return loops;
401}
mtkleinbb6a0282014-07-01 08:43:42 -0700402
bsalomonc2553372014-07-22 13:09:05 -0700403#if SK_SUPPORT_GPU
Brian Salomon6405e712017-03-20 08:54:16 -0400404#define kBogusContextType GrContextFactory::kGL_ContextType
csmartdaltone812d492017-02-21 12:36:05 -0700405#define kBogusContextOverrides GrContextFactory::ContextOverrides::kNone
bsalomonc2553372014-07-22 13:09:05 -0700406#else
bsalomon85b4b532016-04-05 11:06:27 -0700407#define kBogusContextType 0
csmartdaltone812d492017-02-21 12:36:05 -0700408#define kBogusContextOverrides 0
mtkleine714e752014-07-31 12:13:48 -0700409#endif
bsalomonc2553372014-07-22 13:09:05 -0700410
svaisanenc47635e2016-01-28 06:05:43 -0800411static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
412
413#if SK_SUPPORT_GPU
414 if (const auto* gpuConfig = config->asConfigGpu()) {
415 if (!FLAGS_gpu)
416 return;
417
svaisanenc47635e2016-01-28 06:05:43 -0800418 const auto ctxType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700419 const auto ctxOverrides = gpuConfig->getContextOverrides();
svaisanenc47635e2016-01-28 06:05:43 -0800420 const auto sampleCount = gpuConfig->getSamples();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400421 const auto colorType = gpuConfig->getColorType();
422 auto colorSpace = gpuConfig->getColorSpace();
svaisanenc47635e2016-01-28 06:05:43 -0800423
csmartdaltone812d492017-02-21 12:36:05 -0700424 if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOverrides)) {
Greg Daniel81e7bf82017-07-19 14:47:42 -0400425 GrPixelConfig grPixConfig = SkImageInfo2GrPixelConfig(colorType, colorSpace,
426 *ctx->caps());
427 int supportedSampleCount = ctx->caps()->getSampleCount(sampleCount, grPixConfig);
428 if (sampleCount != supportedSampleCount) {
429 SkDebugf("Configuration sample count %d is not a supported sample count.\n",
430 sampleCount);
svaisanenc47635e2016-01-28 06:05:43 -0800431 return;
432 }
433 } else {
434 SkDebugf("No context was available matching config type and options.\n");
435 return;
436 }
437
438 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800439 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800440 Benchmark::kGPU_Backend,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400441 colorType,
svaisanenc47635e2016-01-28 06:05:43 -0800442 kPremul_SkAlphaType,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400443 sk_ref_sp(colorSpace),
svaisanenc47635e2016-01-28 06:05:43 -0800444 sampleCount,
445 ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700446 ctxOverrides,
kkinnunend4e1c352016-03-01 23:41:26 -0800447 gpuConfig->getUseDIText()
448 };
svaisanenc47635e2016-01-28 06:05:43 -0800449
450 configs->push_back(target);
451 return;
452 }
453#endif
454
brianosmanb109b8c2016-06-16 13:03:24 -0700455 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
456 if (config->getTag().equals(#name)) { \
457 Config config = { \
458 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
csmartdaltone812d492017-02-21 12:36:05 -0700459 0, kBogusContextType, kBogusContextOverrides, false \
brianosmanb109b8c2016-06-16 13:03:24 -0700460 }; \
461 configs->push_back(config); \
462 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700463 }
mtkleine714e752014-07-31 12:13:48 -0700464
mtklein40b32be2014-07-09 08:46:49 -0700465 if (FLAGS_cpu) {
mtkleinbb6c41b2016-03-08 11:31:11 -0800466 CPU_CONFIG(nonrendering, kNonRendering_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700467 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800468
469 CPU_CONFIG(8888, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700470 kN32_SkColorType, kPremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800471 CPU_CONFIG(565, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700472 kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500473 auto srgbColorSpace = SkColorSpace::MakeSRGB();
mtkleinbb6c41b2016-03-08 11:31:11 -0800474 CPU_CONFIG(srgb, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700475 kN32_SkColorType, kPremul_SkAlphaType, srgbColorSpace)
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500476 auto srgbLinearColorSpace = SkColorSpace::MakeSRGBLinear();
mtkleinbb6c41b2016-03-08 11:31:11 -0800477 CPU_CONFIG(f16, kRaster_Backend,
raftias7c602de2016-10-13 10:45:44 -0700478 kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbLinearColorSpace)
mtklein40b32be2014-07-09 08:46:49 -0700479 }
mtkleinbb6a0282014-07-01 08:43:42 -0700480
svaisanenc47635e2016-01-28 06:05:43 -0800481 #undef CPU_CONFIG
mtkleinf3723212014-06-25 14:08:00 -0700482}
483
svaisanenc47635e2016-01-28 06:05:43 -0800484// Append all configs that are enabled and supported.
485void create_configs(SkTArray<Config>* configs) {
486 SkCommandLineConfigArray array;
487 ParseConfigs(FLAGS_config, &array);
488 for (int i = 0; i < array.count(); ++i) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400489 create_config(array[i].get(), configs);
svaisanenc47635e2016-01-28 06:05:43 -0800490 }
491}
492
brianosman9f1f6e22016-09-15 08:33:02 -0700493// disable warning : switch statement contains default but no 'case' labels
494#if defined _WIN32
495#pragma warning ( push )
496#pragma warning ( disable : 4065 )
497#endif
498
halcanary96fcdcc2015-08-27 07:41:13 -0700499// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700500static Target* is_enabled(Benchmark* bench, const Config& config) {
501 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700502 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700503 }
504
reede5ea5002014-09-03 11:54:58 -0700505 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700506 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700507
halcanary96fcdcc2015-08-27 07:41:13 -0700508 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700509
tomhudsond968a6f2015-03-26 11:28:06 -0700510 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700511#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700512 case Benchmark::kGPU_Backend:
513 target = new GPUTarget(config);
514 break;
bsalomonc2553372014-07-22 13:09:05 -0700515#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700516 default:
517 target = new Target(config);
518 break;
519 }
bsalomonc2553372014-07-22 13:09:05 -0700520
tomhudsond968a6f2015-03-26 11:28:06 -0700521 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700522 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700523 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700524 }
525 return target;
526}
527
brianosman9f1f6e22016-09-15 08:33:02 -0700528#if defined _WIN32
529#pragma warning ( pop )
530#endif
531
reed42943c82016-09-12 12:01:44 -0700532static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700533 uint32_t minOutputSize, int* width, int* height) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400534 std::unique_ptr<SkBitmapRegionDecoder> brd(
msarettd1227a72016-05-18 06:23:57 -0700535 SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy));
msarett7f691442015-09-22 11:56:16 -0700536 if (nullptr == brd.get()) {
537 // This is indicates that subset decoding is not supported for a particular image format.
538 return false;
539 }
540
msarett7f691442015-09-22 11:56:16 -0700541 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
542 (uint32_t) brd->height()) {
543 // This indicates that the image is not large enough to decode a
544 // minOutputSize x minOutputSize subset at the given sampleSize.
545 return false;
546 }
547
548 // Set the image width and height. The calling code will use this to choose subsets to decode.
549 *width = brd->width();
550 *height = brd->height();
551 return true;
552}
553
egdaniel3bf92062015-06-26 08:12:46 -0700554static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700555 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700556#if SK_SUPPORT_GPU
557 if (FLAGS_abandonGpuContext) {
558 gGrFactory->abandonContexts();
559 }
560 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
561 gGrFactory->destroyContexts();
562 }
563#endif
564}
565
fmalita6519c212016-09-14 08:05:17 -0700566static void collect_files(const SkCommandLineFlags::StringArray& paths, const char* ext,
567 SkTArray<SkString>* list) {
568 for (int i = 0; i < paths.count(); ++i) {
569 if (SkStrEndsWith(paths[i], ext)) {
570 list->push_back(SkString(paths[i]));
571 } else {
572 SkOSFile::Iter it(paths[i], ext);
573 SkString path;
574 while (it.next(&path)) {
575 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
576 }
577 }
578 }
579}
580
mtkleine714e752014-07-31 12:13:48 -0700581class BenchmarkStream {
582public:
mtklein92007582014-08-01 07:46:52 -0700583 BenchmarkStream() : fBenches(BenchRegistry::Head())
584 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700585 , fCurrentRecording(0)
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400586 , fCurrentPiping(0)
mtklein92007582014-08-01 07:46:52 -0700587 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800588 , fCurrentSKP(0)
fmalita6519c212016-09-14 08:05:17 -0700589 , fCurrentSVG(0)
msarett95f192d2015-02-13 09:05:41 -0800590 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700591 , fCurrentCodec(0)
msarett84451022016-02-11 06:45:51 -0800592 , fCurrentAndroidCodec(0)
msarett7f691442015-09-22 11:56:16 -0700593 , fCurrentBRDImage(0)
msarett2cee9022016-06-03 08:25:21 -0700594 , fCurrentColorImage(0)
msarett95f192d2015-02-13 09:05:41 -0800595 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800596 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700597 , fCurrentSubsetType(0)
msarett84451022016-02-11 06:45:51 -0800598 , fCurrentSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700599 , fCurrentAnimSKP(0) {
fmalita6519c212016-09-14 08:05:17 -0700600 collect_files(FLAGS_skps, ".skp", &fSKPs);
601 collect_files(FLAGS_svgs, ".svg", &fSVGs);
mtkleine714e752014-07-31 12:13:48 -0700602
mtklein92007582014-08-01 07:46:52 -0700603 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
604 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
605 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
606 exit(1);
607 }
608
609 for (int i = 0; i < FLAGS_scales.count(); i++) {
610 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
611 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
612 exit(1);
613 }
614 }
robertphillips5b693772014-11-21 06:19:36 -0800615
cdalton63a82852015-06-29 14:06:10 -0700616 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
617 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700618 exit(1);
619 }
620
robertphillips5b693772014-11-21 06:19:36 -0800621 if (FLAGS_mpd) {
622 fUseMPDs.push_back() = true;
623 }
mtkleinc751ecb2015-06-15 08:56:38 -0700624 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700625
msarett95f192d2015-02-13 09:05:41 -0800626 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700627 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800628 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800629 }
msarett2cee9022016-06-03 08:25:21 -0700630 if (!CollectImages(FLAGS_colorImages, &fColorImages)) {
631 exit(1);
632 }
mtklein95553d92015-03-12 08:24:21 -0700633
msarett95f192d2015-02-13 09:05:41 -0800634 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700635 fColorTypes.push_back(kN32_SkColorType);
636 if (!FLAGS_simpleCodec) {
637 fColorTypes.push_back(kRGB_565_SkColorType);
638 fColorTypes.push_back(kAlpha_8_SkColorType);
msarett67cb6662016-06-21 08:49:26 -0700639 fColorTypes.push_back(kGray_8_SkColorType);
640 }
mtklein92007582014-08-01 07:46:52 -0700641 }
642
reedca2622b2016-03-18 07:25:55 -0700643 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700644 // Not strictly necessary, as it will be checked again later,
645 // but helps to avoid a lot of pointless work if we're going to skip it.
cdalton91e457d2016-02-17 11:10:16 -0800646 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700647 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700648 }
649
bungemanf93d7112016-09-16 06:24:20 -0700650 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
651 if (!stream) {
mtkleinfd731ce2014-09-10 12:19:30 -0700652 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700653 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700654 }
655
reedca2622b2016-03-18 07:25:55 -0700656 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700657 }
658
fmalita6519c212016-09-14 08:05:17 -0700659 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
660 SkFILEStream stream(path);
661 if (!stream.isValid()) {
662 SkDebugf("Could not read %s.\n", path);
663 return nullptr;
664 }
665
fmalitae1baa7c2016-09-14 12:04:30 -0700666 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
fmalita6519c212016-09-14 08:05:17 -0700667 if (!svgDom) {
668 SkDebugf("Could not parse %s.\n", path);
669 return nullptr;
670 }
671
fmalitae1baa7c2016-09-14 12:04:30 -0700672 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
673 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
674 if (svgDom->containerSize().isEmpty()) {
675 svgDom->setContainerSize(kDefaultContainerSize);
676 }
677
fmalita6519c212016-09-14 08:05:17 -0700678 SkPictureRecorder recorder;
fmalitae1baa7c2016-09-14 12:04:30 -0700679 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
680 svgDom->containerSize().height()));
fmalita6519c212016-09-14 08:05:17 -0700681 return recorder.finishRecordingAsPicture();
682 }
683
mtklein92007582014-08-01 07:46:52 -0700684 Benchmark* next() {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400685 std::unique_ptr<Benchmark> bench;
mtklein65dfd2f2016-02-03 10:40:54 -0800686 do {
687 bench.reset(this->rawNext());
688 if (!bench) {
689 return nullptr;
690 }
691 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
692 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700693 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800694 }
695
696 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700697 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700698 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700699 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700700 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700701 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700702 return bench;
703 }
mtklein92007582014-08-01 07:46:52 -0700704
mtkleine714e752014-07-31 12:13:48 -0700705 while (fGMs) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400706 std::unique_ptr<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700707 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800708 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700709 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700710 fBenchType = "micro";
mtklein18300a32016-03-16 13:53:35 -0700711 return new GMBench(gm.release());
mtkleine714e752014-07-31 12:13:48 -0700712 }
713 }
mtklein92007582014-08-01 07:46:52 -0700714
mtkleinfd731ce2014-09-10 12:19:30 -0700715 // First add all .skps as RecordingBenches.
716 while (fCurrentRecording < fSKPs.count()) {
717 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700718 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
719 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700720 continue;
721 }
722 SkString name = SkOSPath::Basename(path.c_str());
723 fSourceType = "skp";
724 fBenchType = "recording";
Mike Reed90b20052017-03-08 10:39:02 -0500725 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
mtklein051e56d2014-12-04 08:46:51 -0800726 fSKPOps = pic->approximateOpCount();
mtklein8c1a4f82016-08-08 06:56:22 -0700727 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh, FLAGS_lite);
mtkleinfd731ce2014-09-10 12:19:30 -0700728 }
729
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400730 // Add all .skps as PipeBenches.
731 while (fCurrentPiping < fSKPs.count()) {
732 const SkString& path = fSKPs[fCurrentPiping++];
733 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
734 if (!pic) {
735 continue;
736 }
737 SkString name = SkOSPath::Basename(path.c_str());
738 fSourceType = "skp";
739 fBenchType = "piping";
Mike Reed90b20052017-03-08 10:39:02 -0500740 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400741 fSKPOps = pic->approximateOpCount();
742 return new PipingBench(name.c_str(), pic.get());
743 }
744
mtkleinfd731ce2014-09-10 12:19:30 -0700745 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700746 while (fCurrentScale < fScales.count()) {
747 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800748 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700749 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
750 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800751 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700752 continue;
753 }
robertphillips5b693772014-11-21 06:19:36 -0800754
755 while (fCurrentUseMPD < fUseMPDs.count()) {
756 if (FLAGS_bbh) {
757 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
758 SkRTreeFactory factory;
759 SkPictureRecorder recorder;
robertphillips5b693772014-11-21 06:19:36 -0800760 pic->playback(recorder.beginRecording(pic->cullRect().width(),
761 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800762 &factory,
robertphillipsdda54452016-07-13 13:27:16 -0700763 0));
reedca2622b2016-03-18 07:25:55 -0700764 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800765 }
766 SkString name = SkOSPath::Basename(path.c_str());
767 fSourceType = "skp";
768 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700769 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
770 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700771 }
robertphillips5b693772014-11-21 06:19:36 -0800772 fCurrentUseMPD = 0;
773 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700774 }
fmalita6519c212016-09-14 08:05:17 -0700775
776 while (fCurrentSVG++ < fSVGs.count()) {
777 const char* path = fSVGs[fCurrentSVG - 1].c_str();
778 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
779 fSourceType = "svg";
780 fBenchType = "playback";
781 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
782 fScales[fCurrentScale], false, FLAGS_loopSKP);
783 }
784 }
785
mtklein92007582014-08-01 07:46:52 -0700786 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700787 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700788 fCurrentScale++;
789 }
790
joshualitt261c3ad2015-04-27 09:16:57 -0700791 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700792 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700793 while (fCurrentAnimSKP < fSKPs.count()) {
794 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700795 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
796 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700797 fCurrentAnimSKP++;
798 continue;
799 }
800
801 fCurrentAnimSKP++;
802 SkString name = SkOSPath::Basename(path.c_str());
Hal Canary2db83612016-11-04 13:02:54 -0400803 sk_sp<SKPAnimationBench::Animation> animation(
cdalton63a82852015-06-29 14:06:10 -0700804 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
Hal Canary2db83612016-11-04 13:02:54 -0400805 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation.get(),
halcanary385fe4d2015-08-26 13:07:48 -0700806 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700807 }
808 }
809
scroggo60869a42015-04-01 12:09:17 -0700810 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700811 fSourceType = "image";
812 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700813 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800814 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
815 continue;
816 }
bungeman38d909e2016-08-02 14:40:46 -0700817 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400818 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700819 if (!codec) {
820 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700821 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700822 continue;
823 }
scroggo21027992015-04-02 13:22:38 -0700824
scroggo60869a42015-04-01 12:09:17 -0700825 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700826 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700827
msarettc7796b92016-01-07 14:20:20 -0800828 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700829 if (FLAGS_simpleCodec) {
830 if (kUnpremul_SkAlphaType == alphaType) {
831 alphaType = kPremul_SkAlphaType;
832 }
833
834 fCurrentColorType++;
835 } else {
836 switch (alphaType) {
837 case kOpaque_SkAlphaType:
838 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800839 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700840 break;
841 case kUnpremul_SkAlphaType:
842 case kPremul_SkAlphaType:
843 if (0 == fCurrentAlphaType) {
844 // Test unpremul first.
845 alphaType = kUnpremul_SkAlphaType;
846 fCurrentAlphaType++;
847 } else {
848 // Test premul.
849 alphaType = kPremul_SkAlphaType;
850 fCurrentAlphaType = 0;
851 fCurrentColorType++;
852 }
853 break;
854 default:
855 SkASSERT(false);
856 fCurrentColorType++;
857 break;
858 }
scroggo21027992015-04-02 13:22:38 -0700859 }
860
msarettc7796b92016-01-07 14:20:20 -0800861 // Make sure we can decode to this color type and alpha type.
862 SkImageInfo info =
863 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700864 const size_t rowBytes = info.minRowBytes();
865 SkAutoMalloc storage(info.getSafeSize(rowBytes));
866
scroggoeb602a52015-07-09 08:16:03 -0700867 const SkCodec::Result result = codec->getPixels(
Leon Scroggins571b30f2017-07-11 17:35:31 +0000868 info, storage.get(), rowBytes);
scroggo60869a42015-04-01 12:09:17 -0700869 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700870 case SkCodec::kSuccess:
871 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700872 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700873 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700874 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700875 // This is okay. Not all conversions are valid.
876 break;
scroggo60869a42015-04-01 12:09:17 -0700877 default:
878 // This represents some sort of failure.
879 SkASSERT(false);
880 break;
881 }
882 }
883 fCurrentColorType = 0;
884 }
885
msarett84451022016-02-11 06:45:51 -0800886 // Run AndroidCodecBenches
887 const int sampleSizes[] = { 2, 4, 8 };
888 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
889 fSourceType = "image";
890 fBenchType = "skandroidcodec";
891
892 const SkString& path = fImages[fCurrentAndroidCodec];
893 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
894 continue;
895 }
bungeman38d909e2016-08-02 14:40:46 -0700896 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400897 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800898 if (!codec) {
899 // Nothing to time.
900 SkDebugf("Cannot find codec for %s\n", path.c_str());
901 continue;
902 }
903
904 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
905 int sampleSize = sampleSizes[fCurrentSampleSize];
906 fCurrentSampleSize++;
907 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
908 // Avoid benchmarking scaled decodes of already small images.
909 break;
910 }
911
bungeman38d909e2016-08-02 14:40:46 -0700912 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
913 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -0800914 }
915 fCurrentSampleSize = 0;
916 }
917
msarett7f691442015-09-22 11:56:16 -0700918 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700919 // We intend to create benchmarks that model the use cases in
920 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
921 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
922 // the image. For that reason, we will benchmark decodes in five representative locations
923 // in the image. Additionally, this use case utilizes power of two scaling, so we will
924 // test on power of two sample sizes. The output tile is always 512x512, so, when a
925 // sampleSize is used, the size of the subset that is decoded is always
926 // (sampleSize*512)x(sampleSize*512).
927 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700928 // All use cases we are aware of only scale by powers of two.
929 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
930 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800931 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700932 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800933 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700934 fSourceType = "image";
935 fBenchType = "BRD";
936
mtklein6f0ff912016-01-11 09:04:21 -0800937 const SkString& path = fImages[fCurrentBRDImage];
938 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
939 continue;
940 }
scroggo860e8a62015-10-15 07:51:28 -0700941
msarettd1227a72016-05-18 06:23:57 -0700942 while (fCurrentColorType < fColorTypes.count()) {
943 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
944 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700945
bungeman38d909e2016-08-02 14:40:46 -0700946 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -0700947 const SkColorType colorType = fColorTypes[fCurrentColorType];
948 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
949 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -0700950
msarettd1227a72016-05-18 06:23:57 -0700951 int width = 0;
952 int height = 0;
reed42943c82016-09-12 12:01:44 -0700953 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -0700954 &width, &height)) {
955 break;
msarett7f691442015-09-22 11:56:16 -0700956 }
msarettd1227a72016-05-18 06:23:57 -0700957
958 SkString basename = SkOSPath::Basename(path.c_str());
959 SkIRect subset;
960 const uint32_t subsetSize = sampleSize * minOutputSize;
961 switch (currentSubsetType) {
962 case kTopLeft_SubsetType:
963 basename.append("_TopLeft");
964 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
965 break;
966 case kTopRight_SubsetType:
967 basename.append("_TopRight");
968 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
969 subsetSize);
970 break;
971 case kMiddle_SubsetType:
972 basename.append("_Middle");
973 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
974 (height - subsetSize) / 2, subsetSize, subsetSize);
975 break;
976 case kBottomLeft_SubsetType:
977 basename.append("_BottomLeft");
978 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
979 subsetSize);
980 break;
981 case kBottomRight_SubsetType:
982 basename.append("_BottomRight");
983 subset = SkIRect::MakeXYWH(width - subsetSize,
984 height - subsetSize, subsetSize, subsetSize);
985 break;
986 default:
987 SkASSERT(false);
988 }
989
990 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
991 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -0700992 }
msarettd1227a72016-05-18 06:23:57 -0700993 fCurrentSubsetType = 0;
994 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -0700995 }
msarettd1227a72016-05-18 06:23:57 -0700996 fCurrentSampleSize = 0;
997 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -0700998 }
msarettd1227a72016-05-18 06:23:57 -0700999 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -07001000 }
1001
msarett2cee9022016-06-03 08:25:21 -07001002 while (fCurrentColorImage < fColorImages.count()) {
1003 fSourceType = "colorimage";
1004 fBenchType = "skcolorcodec";
1005 const SkString& path = fColorImages[fCurrentColorImage];
1006 fCurrentColorImage++;
1007 sk_sp<SkData> encoded = SkData::MakeFromFileName(path.c_str());
1008 if (encoded) {
1009 return new ColorCodecBench(SkOSPath::Basename(path.c_str()).c_str(),
1010 std::move(encoded));
1011 } else {
1012 SkDebugf("Could not read file %s.\n", path.c_str());
1013 }
1014 }
1015
halcanary96fcdcc2015-08-27 07:41:13 -07001016 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001017 }
mtklein92007582014-08-01 07:46:52 -07001018
1019 void fillCurrentOptions(ResultsWriter* log) const {
1020 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001021 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001022 if (0 == strcmp(fSourceType, "skp")) {
1023 log->configOption("clip",
1024 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1025 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001026 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -07001027 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001028 if (fCurrentUseMPD > 0) {
1029 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1030 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1031 }
mtklein92007582014-08-01 07:46:52 -07001032 }
mtklein051e56d2014-12-04 08:46:51 -08001033 if (0 == strcmp(fBenchType, "recording")) {
1034 log->metric("bytes", fSKPBytes);
1035 log->metric("ops", fSKPOps);
1036 }
mtklein92007582014-08-01 07:46:52 -07001037 }
1038
mtkleine714e752014-07-31 12:13:48 -07001039private:
msarettb23e6aa2015-06-09 13:56:10 -07001040 enum SubsetType {
1041 kTopLeft_SubsetType = 0,
1042 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001043 kMiddle_SubsetType = 2,
1044 kBottomLeft_SubsetType = 3,
1045 kBottomRight_SubsetType = 4,
1046 kTranslate_SubsetType = 5,
1047 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001048 kLast_SubsetType = kZoom_SubsetType,
1049 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001050 };
1051
mtkleine714e752014-07-31 12:13:48 -07001052 const BenchRegistry* fBenches;
1053 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001054 SkIRect fClip;
1055 SkTArray<SkScalar> fScales;
1056 SkTArray<SkString> fSKPs;
fmalita6519c212016-09-14 08:05:17 -07001057 SkTArray<SkString> fSVGs;
robertphillips5b693772014-11-21 06:19:36 -08001058 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001059 SkTArray<SkString> fImages;
msarett2cee9022016-06-03 08:25:21 -07001060 SkTArray<SkString> fColorImages;
msarett74deb982015-10-20 16:45:56 -07001061 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001062 SkScalar fZoomMax;
1063 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001064
mtklein051e56d2014-12-04 08:46:51 -08001065 double fSKPBytes, fSKPOps;
1066
mtkleinfd731ce2014-09-10 12:19:30 -07001067 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1068 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1069 int fCurrentRecording;
Mike Reed9cdd2ab2016-10-21 10:43:36 -04001070 int fCurrentPiping;
mtklein92007582014-08-01 07:46:52 -07001071 int fCurrentScale;
1072 int fCurrentSKP;
fmalita6519c212016-09-14 08:05:17 -07001073 int fCurrentSVG;
robertphillips5b693772014-11-21 06:19:36 -08001074 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001075 int fCurrentCodec;
msarett84451022016-02-11 06:45:51 -08001076 int fCurrentAndroidCodec;
msarett7f691442015-09-22 11:56:16 -07001077 int fCurrentBRDImage;
msarett2cee9022016-06-03 08:25:21 -07001078 int fCurrentColorImage;
msarett95f192d2015-02-13 09:05:41 -08001079 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -08001080 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -07001081 int fCurrentSubsetType;
msarett84451022016-02-11 06:45:51 -08001082 int fCurrentSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001083 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001084};
1085
msarettc149f0e2016-01-04 11:35:43 -08001086// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1087// This prints something every once in a while so that it knows we're still working.
1088static void start_keepalive() {
1089 struct Loop {
1090 static void forever(void*) {
1091 for (;;) {
1092 static const int kSec = 1200;
1093 #if defined(SK_BUILD_FOR_WIN)
1094 Sleep(kSec * 1000);
1095 #else
1096 sleep(kSec);
1097 #endif
1098 SkDebugf("\nBenchmarks still running...\n");
1099 }
1100 }
1101 };
1102 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1103 intentionallyLeaked->start();
1104}
1105
Mike Kleinbe28ee22017-02-06 12:46:20 -05001106int main(int argc, char** argv) {
1107 SkCommandLineFlags::Parse(argc, argv);
Brian Osman53136aa2017-07-20 15:43:35 -04001108
Brian Osmanbc8150f2017-07-24 11:38:01 -04001109 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -04001110
Mike Kleinadacaef2017-02-06 09:26:14 -05001111#if defined(SK_BUILD_FOR_IOS)
1112 cd_Documents();
1113#endif
jcgregorio3b27ade2014-11-13 08:06:40 -08001114 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001115 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001116 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001117
krajcevski69a55602014-08-13 10:46:31 -07001118#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001119 GrContextOptions grContextOpts;
csmartdalton008b9d82017-02-22 12:00:42 -07001120 grContextOpts.fGpuPathRenderers = CollectGpuPathRenderersFromFlags();
halcanary385fe4d2015-08-26 13:07:48 -07001121 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001122#endif
1123
bsalomon06cddec2014-10-24 10:40:50 -07001124 if (FLAGS_veryVerbose) {
1125 FLAGS_verbose = true;
1126 }
1127
bsalomon6eb03cc2014-08-07 14:28:50 -07001128 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001129 FLAGS_samples = 1;
1130 FLAGS_gpuFrameLag = 0;
1131 }
1132
bsalomon6eb03cc2014-08-07 14:28:50 -07001133 if (!FLAGS_writePath.isEmpty()) {
1134 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1135 if (!sk_mkdir(FLAGS_writePath[0])) {
1136 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001137 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001138 }
1139 }
1140
Ben Wagner145dbcd2016-11-03 14:40:50 -04001141 std::unique_ptr<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001142 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001143#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001144 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001145#else
1146 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1147 return 1;
1148#endif
mtklein60317d0f2014-07-14 11:30:37 -07001149 }
mtklein1915b622014-08-20 11:45:00 -07001150
1151 if (1 == FLAGS_properties.count() % 2) {
1152 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1153 return 1;
1154 }
1155 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1156 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1157 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001158
1159 if (1 == FLAGS_key.count() % 2) {
1160 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1161 return 1;
1162 }
1163 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001164 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001165 }
mtklein60317d0f2014-07-14 11:30:37 -07001166
mtkleinf3723212014-06-25 14:08:00 -07001167 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001168 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001169
cdaltone1b89582015-06-25 19:17:08 -07001170 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001171
bsalomon6eb03cc2014-08-07 14:28:50 -07001172 if (kAutoTuneLoops != FLAGS_loops) {
1173 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001174 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001175 SkDebugf("! -> high variance, ? -> moderate variance\n");
1176 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001177 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001178 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001179 } else {
mtkleind75c4662015-04-30 07:11:22 -07001180 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001181 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001182 }
1183
svaisanenc47635e2016-01-28 06:05:43 -08001184 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001185 create_configs(&configs);
1186
joshualitt3ebd0502016-02-09 07:18:08 -08001187#ifdef THERMAL_MANAGER_SUPPORTED
1188 int tmEnabled, tmThreshold, tmSleepTimeMs, tmTimeoutMs;
1189 if (4 != sscanf(FLAGS_useThermalManager[0], "%d,%d,%d,%d",
1190 &tmEnabled, &tmThreshold, &tmSleepTimeMs, &tmTimeoutMs)) {
1191 SkDebugf("Can't parse %s from --useThermalManager.\n", FLAGS_useThermalManager[0]);
1192 exit(1);
1193 }
1194 ThermalManager tm(tmThreshold, tmSleepTimeMs, tmTimeoutMs);
1195#endif
1196
msarettc149f0e2016-01-04 11:35:43 -08001197 if (FLAGS_keepAlive) {
1198 start_keepalive();
1199 }
1200
Yuqian Liba62b4a2016-12-14 13:46:53 -05001201 gSkUseAnalyticAA = FLAGS_analyticAA;
liyuqian38911a72016-10-04 11:23:22 -07001202
Yuqian Li550148b2017-01-13 10:13:13 -05001203 if (FLAGS_forceAnalyticAA) {
1204 gSkForceAnalyticAA = true;
1205 }
Mike Reedc928fe22017-06-05 10:20:31 -04001206 if (FLAGS_forceRasterPipeline) {
1207 gSkForceRasterPipelineBlitter = true;
1208 }
Yuqian Li550148b2017-01-13 10:13:13 -05001209
mtkleine070c2b2014-10-14 08:40:43 -07001210 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001211 BenchmarkStream benchStream;
1212 while (Benchmark* b = benchStream.next()) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001213 std::unique_ptr<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001214 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001215 continue;
1216 }
1217
svaisanenc47635e2016-01-28 06:05:43 -08001218 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001219 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001220 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001221 }
egdaniel3bf92062015-06-26 08:12:46 -07001222 for (int i = 0; i < configs.count(); ++i) {
joshualitt3ebd0502016-02-09 07:18:08 -08001223#ifdef THERMAL_MANAGER_SUPPORTED
1224 if (tmEnabled && !tm.coolOffIfNecessary()) {
1225 SkDebugf("Could not cool off, timings will be throttled\n");
1226 }
1227#endif
egdaniel3bf92062015-06-26 08:12:46 -07001228 Target* target = is_enabled(b, configs[i]);
1229 if (!target) {
1230 continue;
1231 }
mtkleinf3723212014-06-25 14:08:00 -07001232
halcanary96fcdcc2015-08-27 07:41:13 -07001233 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001234 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001235 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001236
brianosman7a5ada82016-02-08 13:49:12 -08001237 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001238 SkDebugf("Running %s\t%s\n"
1239 , bench->getUniqueName()
1240 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001241 if (FLAGS_dryRun) {
1242 continue;
1243 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001244 }
1245
egdaniel3bf92062015-06-26 08:12:46 -07001246 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001247 bench->perCanvasPreDraw(canvas);
1248
cdaltone1b89582015-06-25 19:17:08 -07001249 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001250 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001251 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1252 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001253
mtkleinbbba1682015-10-28 11:36:30 -07001254 if (FLAGS_ms) {
1255 samples.reset();
1256 auto stop = now_ms() + FLAGS_ms;
1257 do {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001258 samples.push_back(time(loops, bench.get(), target) / loops);
mtkleinbbba1682015-10-28 11:36:30 -07001259 } while (now_ms() < stop);
1260 } else {
cdaltone1b89582015-06-25 19:17:08 -07001261 samples.reset(FLAGS_samples);
1262 for (int s = 0; s < FLAGS_samples; s++) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001263 samples[s] = time(loops, bench.get(), target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001264 }
cdaltone1b89582015-06-25 19:17:08 -07001265 }
mtkleinf3723212014-06-25 14:08:00 -07001266
joshualitte45c81c2015-12-02 09:05:37 -08001267#if SK_SUPPORT_GPU
1268 SkTArray<SkString> keys;
1269 SkTArray<double> values;
1270 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1271 if (gpuStatsDump) {
1272 // TODO cache stats
1273 bench->getGpuStats(canvas, &keys, &values);
1274 }
1275#endif
1276
robertphillips5b693772014-11-21 06:19:36 -08001277 bench->perCanvasPostDraw(canvas);
1278
egdaniel3bf92062015-06-26 08:12:46 -07001279 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001280 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001281 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001282 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001283 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001284 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001285 }
1286
1287 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001288 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001289 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001290 continue;
1291 }
1292
cdaltone1b89582015-06-25 19:17:08 -07001293 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001294 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001295 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001296 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001297 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001298 log->metric("min_ms", stats.min);
George Burgess IV75b57182016-12-06 10:53:52 -08001299 log->metrics("samples", samples);
joshualitte45c81c2015-12-02 09:05:37 -08001300#if SK_SUPPORT_GPU
1301 if (gpuStatsDump) {
1302 // dump to json, only SKPBench currently returns valid keys / values
1303 SkASSERT(keys.count() == values.count());
1304 for (int i = 0; i < keys.count(); i++) {
1305 log->metric(keys[i].c_str(), values[i]);
1306 }
1307 }
1308#endif
1309
mtkleine070c2b2014-10-14 08:40:43 -07001310 if (runs++ % FLAGS_flushEvery == 0) {
1311 log->flush();
1312 }
mtklein60317d0f2014-07-14 11:30:37 -07001313
bsalomon6eb03cc2014-08-07 14:28:50 -07001314 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001315 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001316 config = ""; // Only print the config if we run the same bench on more than one.
1317 }
mtkleind75c4662015-04-30 07:11:22 -07001318 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1319 , sk_tools::getCurrResidentSetSizeMB()
1320 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001321 , bench->getUniqueName()
1322 , config);
mtkleinf3723212014-06-25 14:08:00 -07001323 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001324 const char* mark = " ";
1325 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1326 if (stddev_percent > 5) mark = "?";
1327 if (stddev_percent > 10) mark = "!";
1328
1329 SkDebugf("%10.2f %s\t%s\t%s\n",
1330 stats.median*1e3, mark, bench->getUniqueName(), config);
Mike Reedc665e5b2017-07-04 22:56:06 -04001331 } else if (FLAGS_csv) {
mtkleinf3723212014-06-25 14:08:00 -07001332 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
Mike Reedc665e5b2017-07-04 22:56:06 -04001333 SkDebugf("%g,%g,%g,%g,%g,%s,%s\n"
1334 , stats.min
1335 , stats.median
1336 , stats.mean
1337 , stats.max
1338 , stddev_percent
1339 , config
1340 , bench->getUniqueName()
1341 );
1342 } else {
1343 const char* format = "%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n";
1344 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1345 SkDebugf(format
mtkleind75c4662015-04-30 07:11:22 -07001346 , sk_tools::getCurrResidentSetSizeMB()
1347 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001348 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001349 , HUMANIZE(stats.min)
1350 , HUMANIZE(stats.median)
1351 , HUMANIZE(stats.mean)
1352 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001353 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001354 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001355 , config
mtklein96289052014-09-10 12:05:59 -07001356 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001357 );
1358 }
joshualitte45c81c2015-12-02 09:05:37 -08001359
bsalomonb12ea412015-02-02 21:19:50 -08001360#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001361 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001362 GrContext* context = gGrFactory->get(configs[i].ctxType,
csmartdaltone812d492017-02-21 12:36:05 -07001363 configs[i].ctxOverrides);
kkinnunen5219fd92015-12-10 06:28:13 -08001364 context->printCacheStats();
1365 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001366 }
1367#endif
joshualitte45c81c2015-12-02 09:05:37 -08001368
cdalton2c56ba52015-06-26 13:32:53 -07001369 if (FLAGS_verbose) {
1370 SkDebugf("Samples: ");
1371 for (int i = 0; i < samples.count(); i++) {
1372 SkDebugf("%s ", HUMANIZE(samples[i]));
1373 }
1374 SkDebugf("%s\n", bench->getUniqueName());
1375 }
egdaniel3bf92062015-06-26 08:12:46 -07001376 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001377 }
mtkleinf3723212014-06-25 14:08:00 -07001378 }
1379
Herb Derby5a523fe2017-01-26 16:48:28 -05001380 SkGraphics::PurgeAllCaches();
1381
mtkleine1091452014-12-04 10:47:02 -08001382 log->bench("memory_usage", 0,0);
1383 log->config("meta");
1384 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1385
joshualitte0b19d42015-03-26 10:41:02 -07001386#if SK_SUPPORT_GPU
1387 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1388 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001389 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001390#endif
1391
mtkleinf3723212014-06-25 14:08:00 -07001392 return 0;
1393}