blob: ab7c6893090e34c8aba04a7d1b91548656bd0598 [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
Ben Wagner32fa5102017-08-10 21:25:55 -0400478 CPU_CONFIG(8888, kRaster_Backend,
479 kN32_SkColorType, kPremul_SkAlphaType, nullptr)
480 CPU_CONFIG(565, kRaster_Backend,
481 kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
482 auto srgbColorSpace = SkColorSpace::MakeSRGB();
483 CPU_CONFIG(srgb, kRaster_Backend,
484 kN32_SkColorType, kPremul_SkAlphaType, srgbColorSpace)
485 auto srgbLinearColorSpace = SkColorSpace::MakeSRGBLinear();
486 CPU_CONFIG(f16, kRaster_Backend,
487 kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbLinearColorSpace)
mtkleinbb6a0282014-07-01 08:43:42 -0700488
svaisanenc47635e2016-01-28 06:05:43 -0800489 #undef CPU_CONFIG
Ben Wagner32fa5102017-08-10 21:25:55 -0400490
491 SkDebugf("Unknown config '%s'.\n", config->getTag().c_str());
mtkleinf3723212014-06-25 14:08:00 -0700492}
493
svaisanenc47635e2016-01-28 06:05:43 -0800494// Append all configs that are enabled and supported.
495void create_configs(SkTArray<Config>* configs) {
496 SkCommandLineConfigArray array;
497 ParseConfigs(FLAGS_config, &array);
498 for (int i = 0; i < array.count(); ++i) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400499 create_config(array[i].get(), configs);
svaisanenc47635e2016-01-28 06:05:43 -0800500 }
Ben Wagner32fa5102017-08-10 21:25:55 -0400501
502 // If no just default configs were requested, then we're okay.
503 if (array.count() == 0 || FLAGS_config.count() == 0 ||
504 // If we've been told to ignore undefined flags, we're okay.
505 FLAGS_undefok ||
506 // Otherwise, make sure that all specified configs have been created.
507 array.count() == configs->count()) {
508 return;
509 }
510 SkDebugf("Invalid --config. Use --undefok to bypass this warning.\n");
511 exit(1);
svaisanenc47635e2016-01-28 06:05:43 -0800512}
513
brianosman9f1f6e22016-09-15 08:33:02 -0700514// disable warning : switch statement contains default but no 'case' labels
515#if defined _WIN32
516#pragma warning ( push )
517#pragma warning ( disable : 4065 )
518#endif
519
halcanary96fcdcc2015-08-27 07:41:13 -0700520// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700521static Target* is_enabled(Benchmark* bench, const Config& config) {
522 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700523 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700524 }
525
reede5ea5002014-09-03 11:54:58 -0700526 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700527 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700528
halcanary96fcdcc2015-08-27 07:41:13 -0700529 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700530
tomhudsond968a6f2015-03-26 11:28:06 -0700531 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700532#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700533 case Benchmark::kGPU_Backend:
534 target = new GPUTarget(config);
535 break;
bsalomonc2553372014-07-22 13:09:05 -0700536#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700537 default:
538 target = new Target(config);
539 break;
540 }
bsalomonc2553372014-07-22 13:09:05 -0700541
tomhudsond968a6f2015-03-26 11:28:06 -0700542 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700543 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700544 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700545 }
546 return target;
547}
548
brianosman9f1f6e22016-09-15 08:33:02 -0700549#if defined _WIN32
550#pragma warning ( pop )
551#endif
552
reed42943c82016-09-12 12:01:44 -0700553static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700554 uint32_t minOutputSize, int* width, int* height) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400555 std::unique_ptr<SkBitmapRegionDecoder> brd(
msarettd1227a72016-05-18 06:23:57 -0700556 SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy));
msarett7f691442015-09-22 11:56:16 -0700557 if (nullptr == brd.get()) {
558 // This is indicates that subset decoding is not supported for a particular image format.
559 return false;
560 }
561
msarett7f691442015-09-22 11:56:16 -0700562 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
563 (uint32_t) brd->height()) {
564 // This indicates that the image is not large enough to decode a
565 // minOutputSize x minOutputSize subset at the given sampleSize.
566 return false;
567 }
568
569 // Set the image width and height. The calling code will use this to choose subsets to decode.
570 *width = brd->width();
571 *height = brd->height();
572 return true;
573}
574
egdaniel3bf92062015-06-26 08:12:46 -0700575static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700576 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700577#if SK_SUPPORT_GPU
578 if (FLAGS_abandonGpuContext) {
579 gGrFactory->abandonContexts();
580 }
581 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
582 gGrFactory->destroyContexts();
583 }
584#endif
585}
586
fmalita6519c212016-09-14 08:05:17 -0700587static void collect_files(const SkCommandLineFlags::StringArray& paths, const char* ext,
588 SkTArray<SkString>* list) {
589 for (int i = 0; i < paths.count(); ++i) {
590 if (SkStrEndsWith(paths[i], ext)) {
591 list->push_back(SkString(paths[i]));
592 } else {
593 SkOSFile::Iter it(paths[i], ext);
594 SkString path;
595 while (it.next(&path)) {
596 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
597 }
598 }
599 }
600}
601
mtkleine714e752014-07-31 12:13:48 -0700602class BenchmarkStream {
603public:
mtklein92007582014-08-01 07:46:52 -0700604 BenchmarkStream() : fBenches(BenchRegistry::Head())
605 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700606 , fCurrentRecording(0)
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400607 , fCurrentPiping(0)
mtklein92007582014-08-01 07:46:52 -0700608 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800609 , fCurrentSKP(0)
fmalita6519c212016-09-14 08:05:17 -0700610 , fCurrentSVG(0)
msarett95f192d2015-02-13 09:05:41 -0800611 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700612 , fCurrentCodec(0)
msarett84451022016-02-11 06:45:51 -0800613 , fCurrentAndroidCodec(0)
msarett7f691442015-09-22 11:56:16 -0700614 , fCurrentBRDImage(0)
msarett2cee9022016-06-03 08:25:21 -0700615 , fCurrentColorImage(0)
msarett95f192d2015-02-13 09:05:41 -0800616 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800617 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700618 , fCurrentSubsetType(0)
msarett84451022016-02-11 06:45:51 -0800619 , fCurrentSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700620 , fCurrentAnimSKP(0) {
fmalita6519c212016-09-14 08:05:17 -0700621 collect_files(FLAGS_skps, ".skp", &fSKPs);
622 collect_files(FLAGS_svgs, ".svg", &fSVGs);
mtkleine714e752014-07-31 12:13:48 -0700623
mtklein92007582014-08-01 07:46:52 -0700624 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
625 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
626 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
627 exit(1);
628 }
629
630 for (int i = 0; i < FLAGS_scales.count(); i++) {
631 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
632 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
633 exit(1);
634 }
635 }
robertphillips5b693772014-11-21 06:19:36 -0800636
cdalton63a82852015-06-29 14:06:10 -0700637 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
638 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700639 exit(1);
640 }
641
robertphillips5b693772014-11-21 06:19:36 -0800642 if (FLAGS_mpd) {
643 fUseMPDs.push_back() = true;
644 }
mtkleinc751ecb2015-06-15 08:56:38 -0700645 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700646
msarett95f192d2015-02-13 09:05:41 -0800647 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700648 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800649 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800650 }
msarett2cee9022016-06-03 08:25:21 -0700651 if (!CollectImages(FLAGS_colorImages, &fColorImages)) {
652 exit(1);
653 }
mtklein95553d92015-03-12 08:24:21 -0700654
msarett95f192d2015-02-13 09:05:41 -0800655 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700656 fColorTypes.push_back(kN32_SkColorType);
657 if (!FLAGS_simpleCodec) {
658 fColorTypes.push_back(kRGB_565_SkColorType);
659 fColorTypes.push_back(kAlpha_8_SkColorType);
msarett67cb6662016-06-21 08:49:26 -0700660 fColorTypes.push_back(kGray_8_SkColorType);
661 }
mtklein92007582014-08-01 07:46:52 -0700662 }
663
reedca2622b2016-03-18 07:25:55 -0700664 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700665 // Not strictly necessary, as it will be checked again later,
666 // but helps to avoid a lot of pointless work if we're going to skip it.
cdalton91e457d2016-02-17 11:10:16 -0800667 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700668 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700669 }
670
bungemanf93d7112016-09-16 06:24:20 -0700671 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
672 if (!stream) {
mtkleinfd731ce2014-09-10 12:19:30 -0700673 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700674 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700675 }
676
reedca2622b2016-03-18 07:25:55 -0700677 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700678 }
679
fmalita6519c212016-09-14 08:05:17 -0700680 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
681 SkFILEStream stream(path);
682 if (!stream.isValid()) {
683 SkDebugf("Could not read %s.\n", path);
684 return nullptr;
685 }
686
fmalitae1baa7c2016-09-14 12:04:30 -0700687 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
fmalita6519c212016-09-14 08:05:17 -0700688 if (!svgDom) {
689 SkDebugf("Could not parse %s.\n", path);
690 return nullptr;
691 }
692
fmalitae1baa7c2016-09-14 12:04:30 -0700693 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
694 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
695 if (svgDom->containerSize().isEmpty()) {
696 svgDom->setContainerSize(kDefaultContainerSize);
697 }
698
fmalita6519c212016-09-14 08:05:17 -0700699 SkPictureRecorder recorder;
fmalitae1baa7c2016-09-14 12:04:30 -0700700 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
701 svgDom->containerSize().height()));
fmalita6519c212016-09-14 08:05:17 -0700702 return recorder.finishRecordingAsPicture();
703 }
704
mtklein92007582014-08-01 07:46:52 -0700705 Benchmark* next() {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400706 std::unique_ptr<Benchmark> bench;
mtklein65dfd2f2016-02-03 10:40:54 -0800707 do {
708 bench.reset(this->rawNext());
709 if (!bench) {
710 return nullptr;
711 }
712 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
713 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700714 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800715 }
716
717 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700718 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700719 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700720 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700721 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700722 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700723 return bench;
724 }
mtklein92007582014-08-01 07:46:52 -0700725
mtkleine714e752014-07-31 12:13:48 -0700726 while (fGMs) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400727 std::unique_ptr<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700728 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800729 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700730 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700731 fBenchType = "micro";
mtklein18300a32016-03-16 13:53:35 -0700732 return new GMBench(gm.release());
mtkleine714e752014-07-31 12:13:48 -0700733 }
734 }
mtklein92007582014-08-01 07:46:52 -0700735
mtkleinfd731ce2014-09-10 12:19:30 -0700736 // First add all .skps as RecordingBenches.
737 while (fCurrentRecording < fSKPs.count()) {
738 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700739 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
740 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700741 continue;
742 }
743 SkString name = SkOSPath::Basename(path.c_str());
744 fSourceType = "skp";
745 fBenchType = "recording";
Mike Reed90b20052017-03-08 10:39:02 -0500746 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
mtklein051e56d2014-12-04 08:46:51 -0800747 fSKPOps = pic->approximateOpCount();
mtklein8c1a4f82016-08-08 06:56:22 -0700748 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh, FLAGS_lite);
mtkleinfd731ce2014-09-10 12:19:30 -0700749 }
750
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400751 // Add all .skps as PipeBenches.
752 while (fCurrentPiping < fSKPs.count()) {
753 const SkString& path = fSKPs[fCurrentPiping++];
754 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
755 if (!pic) {
756 continue;
757 }
758 SkString name = SkOSPath::Basename(path.c_str());
759 fSourceType = "skp";
760 fBenchType = "piping";
Mike Reed90b20052017-03-08 10:39:02 -0500761 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400762 fSKPOps = pic->approximateOpCount();
763 return new PipingBench(name.c_str(), pic.get());
764 }
765
mtkleinfd731ce2014-09-10 12:19:30 -0700766 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700767 while (fCurrentScale < fScales.count()) {
768 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800769 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700770 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
771 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800772 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700773 continue;
774 }
robertphillips5b693772014-11-21 06:19:36 -0800775
776 while (fCurrentUseMPD < fUseMPDs.count()) {
777 if (FLAGS_bbh) {
778 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
779 SkRTreeFactory factory;
780 SkPictureRecorder recorder;
robertphillips5b693772014-11-21 06:19:36 -0800781 pic->playback(recorder.beginRecording(pic->cullRect().width(),
782 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800783 &factory,
robertphillipsdda54452016-07-13 13:27:16 -0700784 0));
reedca2622b2016-03-18 07:25:55 -0700785 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800786 }
787 SkString name = SkOSPath::Basename(path.c_str());
788 fSourceType = "skp";
789 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700790 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
791 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700792 }
robertphillips5b693772014-11-21 06:19:36 -0800793 fCurrentUseMPD = 0;
794 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700795 }
fmalita6519c212016-09-14 08:05:17 -0700796
797 while (fCurrentSVG++ < fSVGs.count()) {
798 const char* path = fSVGs[fCurrentSVG - 1].c_str();
799 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
800 fSourceType = "svg";
801 fBenchType = "playback";
802 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
803 fScales[fCurrentScale], false, FLAGS_loopSKP);
804 }
805 }
806
mtklein92007582014-08-01 07:46:52 -0700807 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700808 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700809 fCurrentScale++;
810 }
811
joshualitt261c3ad2015-04-27 09:16:57 -0700812 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700813 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700814 while (fCurrentAnimSKP < fSKPs.count()) {
815 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700816 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
817 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700818 fCurrentAnimSKP++;
819 continue;
820 }
821
822 fCurrentAnimSKP++;
823 SkString name = SkOSPath::Basename(path.c_str());
Hal Canary2db83612016-11-04 13:02:54 -0400824 sk_sp<SKPAnimationBench::Animation> animation(
cdalton63a82852015-06-29 14:06:10 -0700825 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
Hal Canary2db83612016-11-04 13:02:54 -0400826 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation.get(),
halcanary385fe4d2015-08-26 13:07:48 -0700827 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700828 }
829 }
830
scroggo60869a42015-04-01 12:09:17 -0700831 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700832 fSourceType = "image";
833 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700834 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800835 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
836 continue;
837 }
bungeman38d909e2016-08-02 14:40:46 -0700838 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400839 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700840 if (!codec) {
841 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700842 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700843 continue;
844 }
scroggo21027992015-04-02 13:22:38 -0700845
scroggo60869a42015-04-01 12:09:17 -0700846 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700847 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700848
msarettc7796b92016-01-07 14:20:20 -0800849 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700850 if (FLAGS_simpleCodec) {
851 if (kUnpremul_SkAlphaType == alphaType) {
852 alphaType = kPremul_SkAlphaType;
853 }
854
855 fCurrentColorType++;
856 } else {
857 switch (alphaType) {
858 case kOpaque_SkAlphaType:
859 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800860 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700861 break;
862 case kUnpremul_SkAlphaType:
863 case kPremul_SkAlphaType:
864 if (0 == fCurrentAlphaType) {
865 // Test unpremul first.
866 alphaType = kUnpremul_SkAlphaType;
867 fCurrentAlphaType++;
868 } else {
869 // Test premul.
870 alphaType = kPremul_SkAlphaType;
871 fCurrentAlphaType = 0;
872 fCurrentColorType++;
873 }
874 break;
875 default:
876 SkASSERT(false);
877 fCurrentColorType++;
878 break;
879 }
scroggo21027992015-04-02 13:22:38 -0700880 }
881
msarettc7796b92016-01-07 14:20:20 -0800882 // Make sure we can decode to this color type and alpha type.
883 SkImageInfo info =
884 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700885 const size_t rowBytes = info.minRowBytes();
886 SkAutoMalloc storage(info.getSafeSize(rowBytes));
887
scroggoeb602a52015-07-09 08:16:03 -0700888 const SkCodec::Result result = codec->getPixels(
Leon Scroggins571b30f2017-07-11 17:35:31 +0000889 info, storage.get(), rowBytes);
scroggo60869a42015-04-01 12:09:17 -0700890 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700891 case SkCodec::kSuccess:
892 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700893 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700894 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700895 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700896 // This is okay. Not all conversions are valid.
897 break;
scroggo60869a42015-04-01 12:09:17 -0700898 default:
899 // This represents some sort of failure.
900 SkASSERT(false);
901 break;
902 }
903 }
904 fCurrentColorType = 0;
905 }
906
msarett84451022016-02-11 06:45:51 -0800907 // Run AndroidCodecBenches
908 const int sampleSizes[] = { 2, 4, 8 };
909 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
910 fSourceType = "image";
911 fBenchType = "skandroidcodec";
912
913 const SkString& path = fImages[fCurrentAndroidCodec];
914 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
915 continue;
916 }
bungeman38d909e2016-08-02 14:40:46 -0700917 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400918 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800919 if (!codec) {
920 // Nothing to time.
921 SkDebugf("Cannot find codec for %s\n", path.c_str());
922 continue;
923 }
924
925 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
926 int sampleSize = sampleSizes[fCurrentSampleSize];
927 fCurrentSampleSize++;
928 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
929 // Avoid benchmarking scaled decodes of already small images.
930 break;
931 }
932
bungeman38d909e2016-08-02 14:40:46 -0700933 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
934 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -0800935 }
936 fCurrentSampleSize = 0;
937 }
938
msarett7f691442015-09-22 11:56:16 -0700939 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700940 // We intend to create benchmarks that model the use cases in
941 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
942 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
943 // the image. For that reason, we will benchmark decodes in five representative locations
944 // in the image. Additionally, this use case utilizes power of two scaling, so we will
945 // test on power of two sample sizes. The output tile is always 512x512, so, when a
946 // sampleSize is used, the size of the subset that is decoded is always
947 // (sampleSize*512)x(sampleSize*512).
948 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700949 // All use cases we are aware of only scale by powers of two.
950 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
951 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800952 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700953 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800954 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700955 fSourceType = "image";
956 fBenchType = "BRD";
957
mtklein6f0ff912016-01-11 09:04:21 -0800958 const SkString& path = fImages[fCurrentBRDImage];
959 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
960 continue;
961 }
scroggo860e8a62015-10-15 07:51:28 -0700962
msarettd1227a72016-05-18 06:23:57 -0700963 while (fCurrentColorType < fColorTypes.count()) {
964 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
965 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700966
bungeman38d909e2016-08-02 14:40:46 -0700967 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -0700968 const SkColorType colorType = fColorTypes[fCurrentColorType];
969 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
970 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -0700971
msarettd1227a72016-05-18 06:23:57 -0700972 int width = 0;
973 int height = 0;
reed42943c82016-09-12 12:01:44 -0700974 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -0700975 &width, &height)) {
976 break;
msarett7f691442015-09-22 11:56:16 -0700977 }
msarettd1227a72016-05-18 06:23:57 -0700978
979 SkString basename = SkOSPath::Basename(path.c_str());
980 SkIRect subset;
981 const uint32_t subsetSize = sampleSize * minOutputSize;
982 switch (currentSubsetType) {
983 case kTopLeft_SubsetType:
984 basename.append("_TopLeft");
985 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
986 break;
987 case kTopRight_SubsetType:
988 basename.append("_TopRight");
989 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
990 subsetSize);
991 break;
992 case kMiddle_SubsetType:
993 basename.append("_Middle");
994 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
995 (height - subsetSize) / 2, subsetSize, subsetSize);
996 break;
997 case kBottomLeft_SubsetType:
998 basename.append("_BottomLeft");
999 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
1000 subsetSize);
1001 break;
1002 case kBottomRight_SubsetType:
1003 basename.append("_BottomRight");
1004 subset = SkIRect::MakeXYWH(width - subsetSize,
1005 height - subsetSize, subsetSize, subsetSize);
1006 break;
1007 default:
1008 SkASSERT(false);
1009 }
1010
1011 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
1012 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -07001013 }
msarettd1227a72016-05-18 06:23:57 -07001014 fCurrentSubsetType = 0;
1015 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -07001016 }
msarettd1227a72016-05-18 06:23:57 -07001017 fCurrentSampleSize = 0;
1018 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -07001019 }
msarettd1227a72016-05-18 06:23:57 -07001020 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -07001021 }
1022
msarett2cee9022016-06-03 08:25:21 -07001023 while (fCurrentColorImage < fColorImages.count()) {
1024 fSourceType = "colorimage";
1025 fBenchType = "skcolorcodec";
1026 const SkString& path = fColorImages[fCurrentColorImage];
1027 fCurrentColorImage++;
1028 sk_sp<SkData> encoded = SkData::MakeFromFileName(path.c_str());
1029 if (encoded) {
1030 return new ColorCodecBench(SkOSPath::Basename(path.c_str()).c_str(),
1031 std::move(encoded));
1032 } else {
1033 SkDebugf("Could not read file %s.\n", path.c_str());
1034 }
1035 }
1036
halcanary96fcdcc2015-08-27 07:41:13 -07001037 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001038 }
mtklein92007582014-08-01 07:46:52 -07001039
1040 void fillCurrentOptions(ResultsWriter* log) const {
1041 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001042 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001043 if (0 == strcmp(fSourceType, "skp")) {
1044 log->configOption("clip",
1045 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1046 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001047 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -07001048 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001049 if (fCurrentUseMPD > 0) {
1050 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1051 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1052 }
mtklein92007582014-08-01 07:46:52 -07001053 }
mtklein051e56d2014-12-04 08:46:51 -08001054 if (0 == strcmp(fBenchType, "recording")) {
1055 log->metric("bytes", fSKPBytes);
1056 log->metric("ops", fSKPOps);
1057 }
mtklein92007582014-08-01 07:46:52 -07001058 }
1059
mtkleine714e752014-07-31 12:13:48 -07001060private:
msarettb23e6aa2015-06-09 13:56:10 -07001061 enum SubsetType {
1062 kTopLeft_SubsetType = 0,
1063 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001064 kMiddle_SubsetType = 2,
1065 kBottomLeft_SubsetType = 3,
1066 kBottomRight_SubsetType = 4,
1067 kTranslate_SubsetType = 5,
1068 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001069 kLast_SubsetType = kZoom_SubsetType,
1070 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001071 };
1072
mtkleine714e752014-07-31 12:13:48 -07001073 const BenchRegistry* fBenches;
1074 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001075 SkIRect fClip;
1076 SkTArray<SkScalar> fScales;
1077 SkTArray<SkString> fSKPs;
fmalita6519c212016-09-14 08:05:17 -07001078 SkTArray<SkString> fSVGs;
robertphillips5b693772014-11-21 06:19:36 -08001079 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001080 SkTArray<SkString> fImages;
msarett2cee9022016-06-03 08:25:21 -07001081 SkTArray<SkString> fColorImages;
msarett74deb982015-10-20 16:45:56 -07001082 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001083 SkScalar fZoomMax;
1084 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001085
mtklein051e56d2014-12-04 08:46:51 -08001086 double fSKPBytes, fSKPOps;
1087
mtkleinfd731ce2014-09-10 12:19:30 -07001088 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1089 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1090 int fCurrentRecording;
Mike Reed9cdd2ab2016-10-21 10:43:36 -04001091 int fCurrentPiping;
mtklein92007582014-08-01 07:46:52 -07001092 int fCurrentScale;
1093 int fCurrentSKP;
fmalita6519c212016-09-14 08:05:17 -07001094 int fCurrentSVG;
robertphillips5b693772014-11-21 06:19:36 -08001095 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001096 int fCurrentCodec;
msarett84451022016-02-11 06:45:51 -08001097 int fCurrentAndroidCodec;
msarett7f691442015-09-22 11:56:16 -07001098 int fCurrentBRDImage;
msarett2cee9022016-06-03 08:25:21 -07001099 int fCurrentColorImage;
msarett95f192d2015-02-13 09:05:41 -08001100 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -08001101 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -07001102 int fCurrentSubsetType;
msarett84451022016-02-11 06:45:51 -08001103 int fCurrentSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001104 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001105};
1106
msarettc149f0e2016-01-04 11:35:43 -08001107// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1108// This prints something every once in a while so that it knows we're still working.
1109static void start_keepalive() {
1110 struct Loop {
1111 static void forever(void*) {
1112 for (;;) {
1113 static const int kSec = 1200;
1114 #if defined(SK_BUILD_FOR_WIN)
1115 Sleep(kSec * 1000);
1116 #else
1117 sleep(kSec);
1118 #endif
1119 SkDebugf("\nBenchmarks still running...\n");
1120 }
1121 }
1122 };
1123 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1124 intentionallyLeaked->start();
1125}
1126
Mike Kleinbe28ee22017-02-06 12:46:20 -05001127int main(int argc, char** argv) {
1128 SkCommandLineFlags::Parse(argc, argv);
Brian Osman53136aa2017-07-20 15:43:35 -04001129
Brian Osmanbc8150f2017-07-24 11:38:01 -04001130 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -04001131
Mike Kleinadacaef2017-02-06 09:26:14 -05001132#if defined(SK_BUILD_FOR_IOS)
1133 cd_Documents();
1134#endif
jcgregorio3b27ade2014-11-13 08:06:40 -08001135 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001136 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001137 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001138
krajcevski69a55602014-08-13 10:46:31 -07001139#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001140 GrContextOptions grContextOpts;
csmartdalton008b9d82017-02-22 12:00:42 -07001141 grContextOpts.fGpuPathRenderers = CollectGpuPathRenderersFromFlags();
halcanary385fe4d2015-08-26 13:07:48 -07001142 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001143#endif
1144
bsalomon06cddec2014-10-24 10:40:50 -07001145 if (FLAGS_veryVerbose) {
1146 FLAGS_verbose = true;
1147 }
1148
bsalomon6eb03cc2014-08-07 14:28:50 -07001149 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001150 FLAGS_samples = 1;
1151 FLAGS_gpuFrameLag = 0;
1152 }
1153
bsalomon6eb03cc2014-08-07 14:28:50 -07001154 if (!FLAGS_writePath.isEmpty()) {
1155 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1156 if (!sk_mkdir(FLAGS_writePath[0])) {
1157 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001158 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001159 }
1160 }
1161
Ben Wagner145dbcd2016-11-03 14:40:50 -04001162 std::unique_ptr<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001163 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001164#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001165 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001166#else
1167 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1168 return 1;
1169#endif
mtklein60317d0f2014-07-14 11:30:37 -07001170 }
mtklein1915b622014-08-20 11:45:00 -07001171
1172 if (1 == FLAGS_properties.count() % 2) {
1173 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1174 return 1;
1175 }
1176 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1177 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1178 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001179
1180 if (1 == FLAGS_key.count() % 2) {
1181 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1182 return 1;
1183 }
1184 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001185 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001186 }
mtklein60317d0f2014-07-14 11:30:37 -07001187
mtkleinf3723212014-06-25 14:08:00 -07001188 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001189 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001190
cdaltone1b89582015-06-25 19:17:08 -07001191 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001192
bsalomon6eb03cc2014-08-07 14:28:50 -07001193 if (kAutoTuneLoops != FLAGS_loops) {
1194 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001195 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001196 SkDebugf("! -> high variance, ? -> moderate variance\n");
1197 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001198 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001199 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001200 } else {
mtkleind75c4662015-04-30 07:11:22 -07001201 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001202 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001203 }
1204
svaisanenc47635e2016-01-28 06:05:43 -08001205 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001206 create_configs(&configs);
1207
joshualitt3ebd0502016-02-09 07:18:08 -08001208#ifdef THERMAL_MANAGER_SUPPORTED
1209 int tmEnabled, tmThreshold, tmSleepTimeMs, tmTimeoutMs;
1210 if (4 != sscanf(FLAGS_useThermalManager[0], "%d,%d,%d,%d",
1211 &tmEnabled, &tmThreshold, &tmSleepTimeMs, &tmTimeoutMs)) {
1212 SkDebugf("Can't parse %s from --useThermalManager.\n", FLAGS_useThermalManager[0]);
1213 exit(1);
1214 }
1215 ThermalManager tm(tmThreshold, tmSleepTimeMs, tmTimeoutMs);
1216#endif
1217
msarettc149f0e2016-01-04 11:35:43 -08001218 if (FLAGS_keepAlive) {
1219 start_keepalive();
1220 }
1221
Yuqian Liba62b4a2016-12-14 13:46:53 -05001222 gSkUseAnalyticAA = FLAGS_analyticAA;
Yuqian Lidf60e362017-07-25 11:26:31 -04001223 gSkUseDeltaAA = FLAGS_deltaAA;
liyuqian38911a72016-10-04 11:23:22 -07001224
Yuqian Lidf60e362017-07-25 11:26:31 -04001225 if (FLAGS_forceDeltaAA) {
1226 gSkForceDeltaAA = true;
1227 }
Yuqian Li550148b2017-01-13 10:13:13 -05001228 if (FLAGS_forceAnalyticAA) {
1229 gSkForceAnalyticAA = true;
1230 }
Mike Reedc928fe22017-06-05 10:20:31 -04001231 if (FLAGS_forceRasterPipeline) {
1232 gSkForceRasterPipelineBlitter = true;
1233 }
Yuqian Li550148b2017-01-13 10:13:13 -05001234
mtkleine070c2b2014-10-14 08:40:43 -07001235 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001236 BenchmarkStream benchStream;
1237 while (Benchmark* b = benchStream.next()) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001238 std::unique_ptr<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001239 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001240 continue;
1241 }
1242
svaisanenc47635e2016-01-28 06:05:43 -08001243 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001244 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001245 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001246 }
egdaniel3bf92062015-06-26 08:12:46 -07001247 for (int i = 0; i < configs.count(); ++i) {
joshualitt3ebd0502016-02-09 07:18:08 -08001248#ifdef THERMAL_MANAGER_SUPPORTED
1249 if (tmEnabled && !tm.coolOffIfNecessary()) {
1250 SkDebugf("Could not cool off, timings will be throttled\n");
1251 }
1252#endif
egdaniel3bf92062015-06-26 08:12:46 -07001253 Target* target = is_enabled(b, configs[i]);
1254 if (!target) {
1255 continue;
1256 }
mtkleinf3723212014-06-25 14:08:00 -07001257
halcanary96fcdcc2015-08-27 07:41:13 -07001258 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001259 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001260 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001261
brianosman7a5ada82016-02-08 13:49:12 -08001262 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001263 SkDebugf("Running %s\t%s\n"
1264 , bench->getUniqueName()
1265 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001266 if (FLAGS_dryRun) {
1267 continue;
1268 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001269 }
1270
egdaniel3bf92062015-06-26 08:12:46 -07001271 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001272 bench->perCanvasPreDraw(canvas);
1273
cdaltone1b89582015-06-25 19:17:08 -07001274 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001275 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001276 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1277 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001278
mtkleinbbba1682015-10-28 11:36:30 -07001279 if (FLAGS_ms) {
1280 samples.reset();
1281 auto stop = now_ms() + FLAGS_ms;
1282 do {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001283 samples.push_back(time(loops, bench.get(), target) / loops);
mtkleinbbba1682015-10-28 11:36:30 -07001284 } while (now_ms() < stop);
1285 } else {
cdaltone1b89582015-06-25 19:17:08 -07001286 samples.reset(FLAGS_samples);
1287 for (int s = 0; s < FLAGS_samples; s++) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001288 samples[s] = time(loops, bench.get(), target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001289 }
cdaltone1b89582015-06-25 19:17:08 -07001290 }
mtkleinf3723212014-06-25 14:08:00 -07001291
joshualitte45c81c2015-12-02 09:05:37 -08001292#if SK_SUPPORT_GPU
1293 SkTArray<SkString> keys;
1294 SkTArray<double> values;
1295 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1296 if (gpuStatsDump) {
1297 // TODO cache stats
1298 bench->getGpuStats(canvas, &keys, &values);
1299 }
1300#endif
1301
robertphillips5b693772014-11-21 06:19:36 -08001302 bench->perCanvasPostDraw(canvas);
1303
egdaniel3bf92062015-06-26 08:12:46 -07001304 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001305 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001306 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001307 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001308 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001309 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001310 }
1311
1312 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001313 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001314 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001315 continue;
1316 }
1317
cdaltone1b89582015-06-25 19:17:08 -07001318 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001319 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001320 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001321 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001322 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001323 log->metric("min_ms", stats.min);
George Burgess IV75b57182016-12-06 10:53:52 -08001324 log->metrics("samples", samples);
joshualitte45c81c2015-12-02 09:05:37 -08001325#if SK_SUPPORT_GPU
1326 if (gpuStatsDump) {
1327 // dump to json, only SKPBench currently returns valid keys / values
1328 SkASSERT(keys.count() == values.count());
1329 for (int i = 0; i < keys.count(); i++) {
1330 log->metric(keys[i].c_str(), values[i]);
1331 }
1332 }
1333#endif
1334
mtkleine070c2b2014-10-14 08:40:43 -07001335 if (runs++ % FLAGS_flushEvery == 0) {
1336 log->flush();
1337 }
mtklein60317d0f2014-07-14 11:30:37 -07001338
bsalomon6eb03cc2014-08-07 14:28:50 -07001339 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001340 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001341 config = ""; // Only print the config if we run the same bench on more than one.
1342 }
mtkleind75c4662015-04-30 07:11:22 -07001343 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1344 , sk_tools::getCurrResidentSetSizeMB()
1345 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001346 , bench->getUniqueName()
1347 , config);
mtkleinf3723212014-06-25 14:08:00 -07001348 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001349 const char* mark = " ";
1350 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1351 if (stddev_percent > 5) mark = "?";
1352 if (stddev_percent > 10) mark = "!";
1353
1354 SkDebugf("%10.2f %s\t%s\t%s\n",
1355 stats.median*1e3, mark, bench->getUniqueName(), config);
Mike Reedc665e5b2017-07-04 22:56:06 -04001356 } else if (FLAGS_csv) {
mtkleinf3723212014-06-25 14:08:00 -07001357 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
Mike Reedc665e5b2017-07-04 22:56:06 -04001358 SkDebugf("%g,%g,%g,%g,%g,%s,%s\n"
1359 , stats.min
1360 , stats.median
1361 , stats.mean
1362 , stats.max
1363 , stddev_percent
1364 , config
1365 , bench->getUniqueName()
1366 );
1367 } else {
1368 const char* format = "%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n";
1369 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1370 SkDebugf(format
mtkleind75c4662015-04-30 07:11:22 -07001371 , sk_tools::getCurrResidentSetSizeMB()
1372 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001373 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001374 , HUMANIZE(stats.min)
1375 , HUMANIZE(stats.median)
1376 , HUMANIZE(stats.mean)
1377 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001378 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001379 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001380 , config
mtklein96289052014-09-10 12:05:59 -07001381 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001382 );
1383 }
joshualitte45c81c2015-12-02 09:05:37 -08001384
bsalomonb12ea412015-02-02 21:19:50 -08001385#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001386 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001387 GrContext* context = gGrFactory->get(configs[i].ctxType,
csmartdaltone812d492017-02-21 12:36:05 -07001388 configs[i].ctxOverrides);
kkinnunen5219fd92015-12-10 06:28:13 -08001389 context->printCacheStats();
1390 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001391 }
1392#endif
joshualitte45c81c2015-12-02 09:05:37 -08001393
cdalton2c56ba52015-06-26 13:32:53 -07001394 if (FLAGS_verbose) {
1395 SkDebugf("Samples: ");
1396 for (int i = 0; i < samples.count(); i++) {
1397 SkDebugf("%s ", HUMANIZE(samples[i]));
1398 }
1399 SkDebugf("%s\n", bench->getUniqueName());
1400 }
egdaniel3bf92062015-06-26 08:12:46 -07001401 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001402 }
mtkleinf3723212014-06-25 14:08:00 -07001403 }
1404
Herb Derby5a523fe2017-01-26 16:48:28 -05001405 SkGraphics::PurgeAllCaches();
1406
mtkleine1091452014-12-04 10:47:02 -08001407 log->bench("memory_usage", 0,0);
1408 log->config("meta");
1409 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1410
joshualitte0b19d42015-03-26 10:41:02 -07001411#if SK_SUPPORT_GPU
1412 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1413 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001414 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001415#endif
1416
mtkleinf3723212014-06-25 14:08:00 -07001417 return 0;
1418}