blob: a50138a624f27e3ed08a8bc5d0343b51e882a192 [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"
Chris Dalton040238b2017-12-18 14:22:34 -070033#include "SkCommonFlagsGpu.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"
Brian Osmanc3cdef52017-08-31 14:09:22 -040047#include "SkTraceEvent.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040048#include "Stats.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040049#include "ios_utils.h"
mtkleinf3723212014-06-25 14:08:00 -070050
bungeman60e0fee2015-08-26 05:15:46 -070051#include <stdlib.h>
Mike Klein03141d22017-10-30 11:57:15 -040052#include <thread>
bungeman60e0fee2015-08-26 05:15:46 -070053
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"
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040066 using sk_gpu_test::ContextInfo;
bsalomon3724e572016-03-30 18:56:19 -070067 using sk_gpu_test::GrContextFactory;
bsalomonc8699322016-05-11 11:55:36 -070068 using sk_gpu_test::TestContext;
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040069 GrContextOptions grContextOpts;
mtkleinbb6a0282014-07-01 08:43:42 -070070#endif
71
bsalomon682c2692015-05-22 14:01:46 -070072 struct GrContextOptions;
73
reed53249782014-10-10 09:09:52 -070074static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070075
Mike Kleind8ee67c2017-01-19 12:14:50 -050076#if !defined(__has_feature)
77 #define __has_feature(x) 0
78#endif
79
mtkleinb5110422014-08-07 15:20:02 -070080static const int kDefaultLoops =
Mike Kleind8ee67c2017-01-19 12:14:50 -050081#if defined(SK_DEBUG) || __has_feature(address_sanitizer)
bsalomon6eb03cc2014-08-07 14:28:50 -070082 1;
mtkleina189ccd2014-07-14 12:28:47 -070083#else
bsalomon6eb03cc2014-08-07 14:28:50 -070084 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070085#endif
86
bsalomon6eb03cc2014-08-07 14:28:50 -070087static SkString loops_help_txt() {
88 SkString help;
89 help.printf("Number of times to run each bench. Set this to %d to auto-"
90 "tune for each bench. Timings are only reported when auto-tuning.",
91 kAutoTuneLoops);
92 return help;
93}
94
cdaltone1b89582015-06-25 19:17:08 -070095static SkString to_string(int n) {
96 SkString str;
97 str.appendS32(n);
98 return str;
99}
100
Ben Wagner32fa5102017-08-10 21:25:55 -0400101DECLARE_bool(undefok);
102
bsalomon6eb03cc2014-08-07 14:28:50 -0700103DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
104
mtkleinf3723212014-06-25 14:08:00 -0700105DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -0700106DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -0700107DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
108DEFINE_double(overheadGoal, 0.0001,
109 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -0700110DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -0700111DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -0700112
mtklein60317d0f2014-07-14 11:30:37 -0700113DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -0700114DEFINE_int32(maxCalibrationAttempts, 3,
115 "Try up to this many times to guess loops for a bench, or skip the bench.");
116DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700117DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
118DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700119DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
120 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700121DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
mtklein8c1a4f82016-08-08 06:56:22 -0700122DEFINE_bool(lite, false, "Use SkLiteRecorder in recording benchmarks?");
robertphillips5b693772014-11-21 06:19:36 -0800123DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700124DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700125DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
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");
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
mtkleinbbba1682015-10-28 11:36:30 -0700137static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
138
mtkleinf3723212014-06-25 14:08:00 -0700139static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700140 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800141 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700142}
mtklein55b0ffc2014-07-17 08:38:23 -0700143#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700144
tomhudsond968a6f2015-03-26 11:28:06 -0700145bool Target::init(SkImageInfo info, Benchmark* bench) {
146 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700147 this->surface = SkSurface::MakeRaster(info);
148 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700149 return false;
150 }
151 }
152 return true;
153}
154bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700155 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700156 if (!canvas) {
157 return false;
158 }
Mike Reed12e946b2017-04-17 10:53:29 -0400159 bmp->allocPixels(canvas->imageInfo());
160 if (!canvas->readPixels(*bmp, 0, 0)) {
tomhudsond968a6f2015-03-26 11:28:06 -0700161 SkDebugf("Can't read canvas pixels.\n");
162 return false;
163 }
164 return true;
165}
166
167#if SK_SUPPORT_GPU
168struct GPUTarget : public Target {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400169 explicit GPUTarget(const Config& c) : Target(c) {}
170 ContextInfo contextInfo;
171 std::unique_ptr<GrContextFactory> factory;
tomhudsond968a6f2015-03-26 11:28:06 -0700172
173 void setup() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400174 this->contextInfo.testContext()->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700175 // Make sure we're done with whatever came before.
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400176 this->contextInfo.testContext()->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700177 }
178 void endTiming() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400179 if (this->contextInfo.testContext()) {
180 this->contextInfo.testContext()->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700181 }
182 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400183 void fence() override { this->contextInfo.testContext()->finish(); }
mtkleind75c4662015-04-30 07:11:22 -0700184
cdaltond416a5b2015-06-23 13:23:44 -0700185 bool needsFrameTiming(int* maxFrameLag) const override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400186 if (!this->contextInfo.testContext()->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700187 // Frame lag is unknown.
188 *maxFrameLag = FLAGS_gpuFrameLag;
189 }
190 return true;
191 }
tomhudsond968a6f2015-03-26 11:28:06 -0700192 bool init(SkImageInfo info, Benchmark* bench) override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400193 GrContextOptions options = grContextOpts;
194 bench->modifyGrContextOptions(&options);
195 this->factory.reset(new GrContextFactory(options));
bsalomonafcd7cd2015-08-31 12:39:41 -0700196 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
197 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700198 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400199 this->surface = SkSurface::MakeRenderTarget(
200 this->factory->get(this->config.ctxType, this->config.ctxOverrides),
201 SkBudgeted::kNo, info, this->config.samples, &props);
202 this->contextInfo =
203 this->factory->getContextInfo(this->config.ctxType, this->config.ctxOverrides);
tomhudsond968a6f2015-03-26 11:28:06 -0700204 if (!this->surface.get()) {
205 return false;
206 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400207 if (!this->contextInfo.testContext()->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700208 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800209 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700210 }
tomhudsond968a6f2015-03-26 11:28:06 -0700211 return true;
212 }
213 void fillOptions(ResultsWriter* log) override {
214 const GrGLubyte* version;
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400215 if (this->contextInfo.backend() == kOpenGL_GrBackend) {
216 const GrGLInterface* gl = reinterpret_cast<const GrGLInterface*>(
217 this->contextInfo.testContext()->backendContext());
bsalomonc8699322016-05-11 11:55:36 -0700218 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
219 log->configOption("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700220
bsalomonc8699322016-05-11 11:55:36 -0700221 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
222 log->configOption("GL_RENDERER", (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_VENDOR));
225 log->configOption("GL_VENDOR", (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_SHADING_LANGUAGE_VERSION));
228 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
229 }
tomhudsond968a6f2015-03-26 11:28:06 -0700230 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400231
232 void dumpStats() override {
233 this->contextInfo.grContext()->printCacheStats();
234 this->contextInfo.grContext()->printGpuStats();
235 }
tomhudsond968a6f2015-03-26 11:28:06 -0700236};
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 // Pretty much the same deal as the calibration: do some warmup to make
396 // sure we're timing steady-state pipelined frames.
Brian Osman01776982017-10-10 15:41:03 -0400397 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700398 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700399 }
mtkleinbb6a0282014-07-01 08:43:42 -0700400
mtkleinbb6a0282014-07-01 08:43:42 -0700401 return loops;
402}
mtkleinbb6a0282014-07-01 08:43:42 -0700403
bsalomonc2553372014-07-22 13:09:05 -0700404#if SK_SUPPORT_GPU
Brian Salomon6405e712017-03-20 08:54:16 -0400405#define kBogusContextType GrContextFactory::kGL_ContextType
csmartdaltone812d492017-02-21 12:36:05 -0700406#define kBogusContextOverrides GrContextFactory::ContextOverrides::kNone
bsalomonc2553372014-07-22 13:09:05 -0700407#else
bsalomon85b4b532016-04-05 11:06:27 -0700408#define kBogusContextType 0
csmartdaltone812d492017-02-21 12:36:05 -0700409#define kBogusContextOverrides 0
mtkleine714e752014-07-31 12:13:48 -0700410#endif
bsalomonc2553372014-07-22 13:09:05 -0700411
svaisanenc47635e2016-01-28 06:05:43 -0800412static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
413
414#if SK_SUPPORT_GPU
415 if (const auto* gpuConfig = config->asConfigGpu()) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400416 if (!FLAGS_gpu) {
417 SkDebugf("Skipping config '%s' as requested.\n", config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800418 return;
Ben Wagner32fa5102017-08-10 21:25:55 -0400419 }
svaisanenc47635e2016-01-28 06:05:43 -0800420
svaisanenc47635e2016-01-28 06:05:43 -0800421 const auto ctxType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700422 const auto ctxOverrides = gpuConfig->getContextOverrides();
svaisanenc47635e2016-01-28 06:05:43 -0800423 const auto sampleCount = gpuConfig->getSamples();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400424 const auto colorType = gpuConfig->getColorType();
425 auto colorSpace = gpuConfig->getColorSpace();
svaisanenc47635e2016-01-28 06:05:43 -0800426
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400427 GrContextFactory factory(grContextOpts);
428 if (const GrContext* ctx = factory.get(ctxType, ctxOverrides)) {
Greg Daniel81e7bf82017-07-19 14:47:42 -0400429 GrPixelConfig grPixConfig = SkImageInfo2GrPixelConfig(colorType, colorSpace,
430 *ctx->caps());
431 int supportedSampleCount = ctx->caps()->getSampleCount(sampleCount, grPixConfig);
432 if (sampleCount != supportedSampleCount) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400433 SkDebugf("Configuration '%s' sample count %d is not a supported sample count.\n",
434 config->getTag().c_str(), sampleCount);
svaisanenc47635e2016-01-28 06:05:43 -0800435 return;
436 }
437 } else {
Ben Wagner32fa5102017-08-10 21:25:55 -0400438 SkDebugf("No context was available matching config '%s'.\n",
439 config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800440 return;
441 }
442
443 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800444 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800445 Benchmark::kGPU_Backend,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400446 colorType,
svaisanenc47635e2016-01-28 06:05:43 -0800447 kPremul_SkAlphaType,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400448 sk_ref_sp(colorSpace),
svaisanenc47635e2016-01-28 06:05:43 -0800449 sampleCount,
450 ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700451 ctxOverrides,
kkinnunend4e1c352016-03-01 23:41:26 -0800452 gpuConfig->getUseDIText()
453 };
svaisanenc47635e2016-01-28 06:05:43 -0800454
455 configs->push_back(target);
456 return;
457 }
458#endif
459
brianosmanb109b8c2016-06-16 13:03:24 -0700460 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
461 if (config->getTag().equals(#name)) { \
Ben Wagner32fa5102017-08-10 21:25:55 -0400462 if (!FLAGS_cpu) { \
463 SkDebugf("Skipping config '%s' as requested.\n", \
464 config->getTag().c_str()); \
465 return; \
466 } \
brianosmanb109b8c2016-06-16 13:03:24 -0700467 Config config = { \
468 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
csmartdaltone812d492017-02-21 12:36:05 -0700469 0, kBogusContextType, kBogusContextOverrides, false \
brianosmanb109b8c2016-06-16 13:03:24 -0700470 }; \
471 configs->push_back(config); \
472 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700473 }
mtkleine714e752014-07-31 12:13:48 -0700474
Ben Wagner32fa5102017-08-10 21:25:55 -0400475 CPU_CONFIG(nonrendering, kNonRendering_Backend,
476 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800477
Mike Reede6befa52017-08-28 11:30:48 -0400478 CPU_CONFIG(a8, kRaster_Backend,
479 kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr)
Ben Wagner32fa5102017-08-10 21:25:55 -0400480 CPU_CONFIG(8888, kRaster_Backend,
481 kN32_SkColorType, kPremul_SkAlphaType, nullptr)
482 CPU_CONFIG(565, kRaster_Backend,
483 kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
484 auto srgbColorSpace = SkColorSpace::MakeSRGB();
485 CPU_CONFIG(srgb, kRaster_Backend,
486 kN32_SkColorType, kPremul_SkAlphaType, srgbColorSpace)
487 auto srgbLinearColorSpace = SkColorSpace::MakeSRGBLinear();
488 CPU_CONFIG(f16, kRaster_Backend,
489 kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbLinearColorSpace)
mtkleinbb6a0282014-07-01 08:43:42 -0700490
svaisanenc47635e2016-01-28 06:05:43 -0800491 #undef CPU_CONFIG
Ben Wagner32fa5102017-08-10 21:25:55 -0400492
493 SkDebugf("Unknown config '%s'.\n", config->getTag().c_str());
mtkleinf3723212014-06-25 14:08:00 -0700494}
495
svaisanenc47635e2016-01-28 06:05:43 -0800496// Append all configs that are enabled and supported.
497void create_configs(SkTArray<Config>* configs) {
498 SkCommandLineConfigArray array;
499 ParseConfigs(FLAGS_config, &array);
500 for (int i = 0; i < array.count(); ++i) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400501 create_config(array[i].get(), configs);
svaisanenc47635e2016-01-28 06:05:43 -0800502 }
Ben Wagner32fa5102017-08-10 21:25:55 -0400503
504 // If no just default configs were requested, then we're okay.
505 if (array.count() == 0 || FLAGS_config.count() == 0 ||
506 // If we've been told to ignore undefined flags, we're okay.
507 FLAGS_undefok ||
508 // Otherwise, make sure that all specified configs have been created.
509 array.count() == configs->count()) {
510 return;
511 }
512 SkDebugf("Invalid --config. Use --undefok to bypass this warning.\n");
513 exit(1);
svaisanenc47635e2016-01-28 06:05:43 -0800514}
515
brianosman9f1f6e22016-09-15 08:33:02 -0700516// disable warning : switch statement contains default but no 'case' labels
517#if defined _WIN32
518#pragma warning ( push )
519#pragma warning ( disable : 4065 )
520#endif
521
halcanary96fcdcc2015-08-27 07:41:13 -0700522// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700523static Target* is_enabled(Benchmark* bench, const Config& config) {
524 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700525 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700526 }
527
reede5ea5002014-09-03 11:54:58 -0700528 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700529 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700530
halcanary96fcdcc2015-08-27 07:41:13 -0700531 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700532
tomhudsond968a6f2015-03-26 11:28:06 -0700533 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700534#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700535 case Benchmark::kGPU_Backend:
536 target = new GPUTarget(config);
537 break;
bsalomonc2553372014-07-22 13:09:05 -0700538#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700539 default:
540 target = new Target(config);
541 break;
542 }
bsalomonc2553372014-07-22 13:09:05 -0700543
tomhudsond968a6f2015-03-26 11:28:06 -0700544 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700545 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700546 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700547 }
548 return target;
549}
550
brianosman9f1f6e22016-09-15 08:33:02 -0700551#if defined _WIN32
552#pragma warning ( pop )
553#endif
554
reed42943c82016-09-12 12:01:44 -0700555static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700556 uint32_t minOutputSize, int* width, int* height) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400557 std::unique_ptr<SkBitmapRegionDecoder> brd(
msarettd1227a72016-05-18 06:23:57 -0700558 SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy));
msarett7f691442015-09-22 11:56:16 -0700559 if (nullptr == brd.get()) {
560 // This is indicates that subset decoding is not supported for a particular image format.
561 return false;
562 }
563
msarett7f691442015-09-22 11:56:16 -0700564 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
565 (uint32_t) brd->height()) {
566 // This indicates that the image is not large enough to decode a
567 // minOutputSize x minOutputSize subset at the given sampleSize.
568 return false;
569 }
570
571 // Set the image width and height. The calling code will use this to choose subsets to decode.
572 *width = brd->width();
573 *height = brd->height();
574 return true;
575}
576
egdaniel3bf92062015-06-26 08:12:46 -0700577static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700578 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700579}
580
fmalita6519c212016-09-14 08:05:17 -0700581static void collect_files(const SkCommandLineFlags::StringArray& paths, const char* ext,
582 SkTArray<SkString>* list) {
583 for (int i = 0; i < paths.count(); ++i) {
584 if (SkStrEndsWith(paths[i], ext)) {
585 list->push_back(SkString(paths[i]));
586 } else {
587 SkOSFile::Iter it(paths[i], ext);
588 SkString path;
589 while (it.next(&path)) {
590 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
591 }
592 }
593 }
594}
595
mtkleine714e752014-07-31 12:13:48 -0700596class BenchmarkStream {
597public:
mtklein92007582014-08-01 07:46:52 -0700598 BenchmarkStream() : fBenches(BenchRegistry::Head())
599 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700600 , fCurrentRecording(0)
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400601 , fCurrentPiping(0)
Mike Reede45ff462017-12-06 10:47:03 -0500602 , fCurrentDeserialPicture(0)
mtklein92007582014-08-01 07:46:52 -0700603 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800604 , fCurrentSKP(0)
fmalita6519c212016-09-14 08:05:17 -0700605 , fCurrentSVG(0)
msarett95f192d2015-02-13 09:05:41 -0800606 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700607 , fCurrentCodec(0)
msarett84451022016-02-11 06:45:51 -0800608 , fCurrentAndroidCodec(0)
msarett7f691442015-09-22 11:56:16 -0700609 , fCurrentBRDImage(0)
msarett2cee9022016-06-03 08:25:21 -0700610 , fCurrentColorImage(0)
msarett95f192d2015-02-13 09:05:41 -0800611 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800612 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700613 , fCurrentSubsetType(0)
msarett84451022016-02-11 06:45:51 -0800614 , fCurrentSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700615 , fCurrentAnimSKP(0) {
fmalita6519c212016-09-14 08:05:17 -0700616 collect_files(FLAGS_skps, ".skp", &fSKPs);
617 collect_files(FLAGS_svgs, ".svg", &fSVGs);
mtkleine714e752014-07-31 12:13:48 -0700618
mtklein92007582014-08-01 07:46:52 -0700619 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
620 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
621 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
622 exit(1);
623 }
624
625 for (int i = 0; i < FLAGS_scales.count(); i++) {
626 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
627 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
628 exit(1);
629 }
630 }
robertphillips5b693772014-11-21 06:19:36 -0800631
cdalton63a82852015-06-29 14:06:10 -0700632 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
633 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700634 exit(1);
635 }
636
robertphillips5b693772014-11-21 06:19:36 -0800637 if (FLAGS_mpd) {
638 fUseMPDs.push_back() = true;
639 }
mtkleinc751ecb2015-06-15 08:56:38 -0700640 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700641
msarett95f192d2015-02-13 09:05:41 -0800642 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700643 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800644 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800645 }
msarett2cee9022016-06-03 08:25:21 -0700646 if (!CollectImages(FLAGS_colorImages, &fColorImages)) {
647 exit(1);
648 }
mtklein95553d92015-03-12 08:24:21 -0700649
msarett95f192d2015-02-13 09:05:41 -0800650 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700651 fColorTypes.push_back(kN32_SkColorType);
652 if (!FLAGS_simpleCodec) {
653 fColorTypes.push_back(kRGB_565_SkColorType);
654 fColorTypes.push_back(kAlpha_8_SkColorType);
msarett67cb6662016-06-21 08:49:26 -0700655 fColorTypes.push_back(kGray_8_SkColorType);
656 }
mtklein92007582014-08-01 07:46:52 -0700657 }
658
reedca2622b2016-03-18 07:25:55 -0700659 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700660 // Not strictly necessary, as it will be checked again later,
661 // but helps to avoid a lot of pointless work if we're going to skip it.
cdalton91e457d2016-02-17 11:10:16 -0800662 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700663 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700664 }
665
bungemanf93d7112016-09-16 06:24:20 -0700666 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
667 if (!stream) {
mtkleinfd731ce2014-09-10 12:19:30 -0700668 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700669 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700670 }
671
reedca2622b2016-03-18 07:25:55 -0700672 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700673 }
674
fmalita6519c212016-09-14 08:05:17 -0700675 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
676 SkFILEStream stream(path);
677 if (!stream.isValid()) {
678 SkDebugf("Could not read %s.\n", path);
679 return nullptr;
680 }
681
fmalitae1baa7c2016-09-14 12:04:30 -0700682 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
fmalita6519c212016-09-14 08:05:17 -0700683 if (!svgDom) {
684 SkDebugf("Could not parse %s.\n", path);
685 return nullptr;
686 }
687
fmalitae1baa7c2016-09-14 12:04:30 -0700688 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
689 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
690 if (svgDom->containerSize().isEmpty()) {
691 svgDom->setContainerSize(kDefaultContainerSize);
692 }
693
fmalita6519c212016-09-14 08:05:17 -0700694 SkPictureRecorder recorder;
fmalitae1baa7c2016-09-14 12:04:30 -0700695 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
696 svgDom->containerSize().height()));
fmalita6519c212016-09-14 08:05:17 -0700697 return recorder.finishRecordingAsPicture();
698 }
699
mtklein92007582014-08-01 07:46:52 -0700700 Benchmark* next() {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400701 std::unique_ptr<Benchmark> bench;
mtklein65dfd2f2016-02-03 10:40:54 -0800702 do {
703 bench.reset(this->rawNext());
704 if (!bench) {
705 return nullptr;
706 }
707 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
708 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700709 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800710 }
711
712 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700713 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700714 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700715 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700716 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700717 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700718 return bench;
719 }
mtklein92007582014-08-01 07:46:52 -0700720
mtkleine714e752014-07-31 12:13:48 -0700721 while (fGMs) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400722 std::unique_ptr<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700723 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800724 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700725 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700726 fBenchType = "micro";
mtklein18300a32016-03-16 13:53:35 -0700727 return new GMBench(gm.release());
mtkleine714e752014-07-31 12:13:48 -0700728 }
729 }
mtklein92007582014-08-01 07:46:52 -0700730
mtkleinfd731ce2014-09-10 12:19:30 -0700731 // First add all .skps as RecordingBenches.
732 while (fCurrentRecording < fSKPs.count()) {
733 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700734 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
735 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700736 continue;
737 }
738 SkString name = SkOSPath::Basename(path.c_str());
739 fSourceType = "skp";
740 fBenchType = "recording";
Mike Reed90b20052017-03-08 10:39:02 -0500741 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
mtklein051e56d2014-12-04 08:46:51 -0800742 fSKPOps = pic->approximateOpCount();
mtklein8c1a4f82016-08-08 06:56:22 -0700743 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh, FLAGS_lite);
mtkleinfd731ce2014-09-10 12:19:30 -0700744 }
745
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400746 // Add all .skps as PipeBenches.
747 while (fCurrentPiping < fSKPs.count()) {
748 const SkString& path = fSKPs[fCurrentPiping++];
749 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
750 if (!pic) {
751 continue;
752 }
753 SkString name = SkOSPath::Basename(path.c_str());
754 fSourceType = "skp";
755 fBenchType = "piping";
Mike Reed90b20052017-03-08 10:39:02 -0500756 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400757 fSKPOps = pic->approximateOpCount();
758 return new PipingBench(name.c_str(), pic.get());
759 }
760
Mike Reede45ff462017-12-06 10:47:03 -0500761 // Add all .skps as DeserializePictureBenchs.
762 while (fCurrentDeserialPicture < fSKPs.count()) {
763 const SkString& path = fSKPs[fCurrentDeserialPicture++];
764 sk_sp<SkData> data = SkData::MakeFromFileName(path.c_str());
765 if (!data) {
766 continue;
767 }
768 SkString name = SkOSPath::Basename(path.c_str());
769 fSourceType = "skp";
770 fBenchType = "deserial";
771 fSKPBytes = static_cast<double>(data->size());
772 fSKPOps = 0;
773 return new DeserializePictureBench(name.c_str(), std::move(data));
774 }
775
mtkleinfd731ce2014-09-10 12:19:30 -0700776 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700777 while (fCurrentScale < fScales.count()) {
778 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800779 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700780 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
781 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800782 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700783 continue;
784 }
robertphillips5b693772014-11-21 06:19:36 -0800785
786 while (fCurrentUseMPD < fUseMPDs.count()) {
787 if (FLAGS_bbh) {
788 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
789 SkRTreeFactory factory;
790 SkPictureRecorder recorder;
robertphillips5b693772014-11-21 06:19:36 -0800791 pic->playback(recorder.beginRecording(pic->cullRect().width(),
792 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800793 &factory,
robertphillipsdda54452016-07-13 13:27:16 -0700794 0));
reedca2622b2016-03-18 07:25:55 -0700795 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800796 }
797 SkString name = SkOSPath::Basename(path.c_str());
798 fSourceType = "skp";
799 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700800 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
801 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700802 }
robertphillips5b693772014-11-21 06:19:36 -0800803 fCurrentUseMPD = 0;
804 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700805 }
fmalita6519c212016-09-14 08:05:17 -0700806
807 while (fCurrentSVG++ < fSVGs.count()) {
808 const char* path = fSVGs[fCurrentSVG - 1].c_str();
809 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
810 fSourceType = "svg";
811 fBenchType = "playback";
812 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
813 fScales[fCurrentScale], false, FLAGS_loopSKP);
814 }
815 }
816
mtklein92007582014-08-01 07:46:52 -0700817 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700818 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700819 fCurrentScale++;
820 }
821
joshualitt261c3ad2015-04-27 09:16:57 -0700822 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700823 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700824 while (fCurrentAnimSKP < fSKPs.count()) {
825 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700826 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
827 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700828 fCurrentAnimSKP++;
829 continue;
830 }
831
832 fCurrentAnimSKP++;
833 SkString name = SkOSPath::Basename(path.c_str());
Hal Canary2db83612016-11-04 13:02:54 -0400834 sk_sp<SKPAnimationBench::Animation> animation(
cdalton63a82852015-06-29 14:06:10 -0700835 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
Hal Canary2db83612016-11-04 13:02:54 -0400836 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation.get(),
halcanary385fe4d2015-08-26 13:07:48 -0700837 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700838 }
839 }
840
scroggo60869a42015-04-01 12:09:17 -0700841 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700842 fSourceType = "image";
843 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700844 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800845 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
846 continue;
847 }
bungeman38d909e2016-08-02 14:40:46 -0700848 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400849 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700850 if (!codec) {
851 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700852 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700853 continue;
854 }
scroggo21027992015-04-02 13:22:38 -0700855
scroggo60869a42015-04-01 12:09:17 -0700856 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700857 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700858
msarettc7796b92016-01-07 14:20:20 -0800859 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700860 if (FLAGS_simpleCodec) {
861 if (kUnpremul_SkAlphaType == alphaType) {
862 alphaType = kPremul_SkAlphaType;
863 }
864
865 fCurrentColorType++;
866 } else {
867 switch (alphaType) {
868 case kOpaque_SkAlphaType:
869 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800870 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700871 break;
872 case kUnpremul_SkAlphaType:
873 case kPremul_SkAlphaType:
874 if (0 == fCurrentAlphaType) {
875 // Test unpremul first.
876 alphaType = kUnpremul_SkAlphaType;
877 fCurrentAlphaType++;
878 } else {
879 // Test premul.
880 alphaType = kPremul_SkAlphaType;
881 fCurrentAlphaType = 0;
882 fCurrentColorType++;
883 }
884 break;
885 default:
886 SkASSERT(false);
887 fCurrentColorType++;
888 break;
889 }
scroggo21027992015-04-02 13:22:38 -0700890 }
891
msarettc7796b92016-01-07 14:20:20 -0800892 // Make sure we can decode to this color type and alpha type.
893 SkImageInfo info =
894 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700895 const size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400896 SkAutoMalloc storage(info.computeByteSize(rowBytes));
scroggo21027992015-04-02 13:22:38 -0700897
scroggoeb602a52015-07-09 08:16:03 -0700898 const SkCodec::Result result = codec->getPixels(
Leon Scroggins571b30f2017-07-11 17:35:31 +0000899 info, storage.get(), rowBytes);
scroggo60869a42015-04-01 12:09:17 -0700900 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700901 case SkCodec::kSuccess:
902 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700903 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700904 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700905 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700906 // This is okay. Not all conversions are valid.
907 break;
scroggo60869a42015-04-01 12:09:17 -0700908 default:
909 // This represents some sort of failure.
910 SkASSERT(false);
911 break;
912 }
913 }
914 fCurrentColorType = 0;
915 }
916
msarett84451022016-02-11 06:45:51 -0800917 // Run AndroidCodecBenches
918 const int sampleSizes[] = { 2, 4, 8 };
919 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
920 fSourceType = "image";
921 fBenchType = "skandroidcodec";
922
923 const SkString& path = fImages[fCurrentAndroidCodec];
924 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
925 continue;
926 }
bungeman38d909e2016-08-02 14:40:46 -0700927 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400928 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800929 if (!codec) {
930 // Nothing to time.
931 SkDebugf("Cannot find codec for %s\n", path.c_str());
932 continue;
933 }
934
935 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
936 int sampleSize = sampleSizes[fCurrentSampleSize];
937 fCurrentSampleSize++;
938 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
939 // Avoid benchmarking scaled decodes of already small images.
940 break;
941 }
942
bungeman38d909e2016-08-02 14:40:46 -0700943 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
944 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -0800945 }
946 fCurrentSampleSize = 0;
947 }
948
msarett7f691442015-09-22 11:56:16 -0700949 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700950 // We intend to create benchmarks that model the use cases in
951 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
952 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
953 // the image. For that reason, we will benchmark decodes in five representative locations
954 // in the image. Additionally, this use case utilizes power of two scaling, so we will
955 // test on power of two sample sizes. The output tile is always 512x512, so, when a
956 // sampleSize is used, the size of the subset that is decoded is always
957 // (sampleSize*512)x(sampleSize*512).
958 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700959 // All use cases we are aware of only scale by powers of two.
960 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
961 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800962 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700963 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800964 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700965 fSourceType = "image";
966 fBenchType = "BRD";
967
mtklein6f0ff912016-01-11 09:04:21 -0800968 const SkString& path = fImages[fCurrentBRDImage];
969 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
970 continue;
971 }
scroggo860e8a62015-10-15 07:51:28 -0700972
msarettd1227a72016-05-18 06:23:57 -0700973 while (fCurrentColorType < fColorTypes.count()) {
974 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
975 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700976
bungeman38d909e2016-08-02 14:40:46 -0700977 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -0700978 const SkColorType colorType = fColorTypes[fCurrentColorType];
979 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
980 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -0700981
msarettd1227a72016-05-18 06:23:57 -0700982 int width = 0;
983 int height = 0;
reed42943c82016-09-12 12:01:44 -0700984 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -0700985 &width, &height)) {
986 break;
msarett7f691442015-09-22 11:56:16 -0700987 }
msarettd1227a72016-05-18 06:23:57 -0700988
989 SkString basename = SkOSPath::Basename(path.c_str());
990 SkIRect subset;
991 const uint32_t subsetSize = sampleSize * minOutputSize;
992 switch (currentSubsetType) {
993 case kTopLeft_SubsetType:
994 basename.append("_TopLeft");
995 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
996 break;
997 case kTopRight_SubsetType:
998 basename.append("_TopRight");
999 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
1000 subsetSize);
1001 break;
1002 case kMiddle_SubsetType:
1003 basename.append("_Middle");
1004 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
1005 (height - subsetSize) / 2, subsetSize, subsetSize);
1006 break;
1007 case kBottomLeft_SubsetType:
1008 basename.append("_BottomLeft");
1009 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
1010 subsetSize);
1011 break;
1012 case kBottomRight_SubsetType:
1013 basename.append("_BottomRight");
1014 subset = SkIRect::MakeXYWH(width - subsetSize,
1015 height - subsetSize, subsetSize, subsetSize);
1016 break;
1017 default:
1018 SkASSERT(false);
1019 }
1020
1021 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
1022 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -07001023 }
msarettd1227a72016-05-18 06:23:57 -07001024 fCurrentSubsetType = 0;
1025 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -07001026 }
msarettd1227a72016-05-18 06:23:57 -07001027 fCurrentSampleSize = 0;
1028 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -07001029 }
msarettd1227a72016-05-18 06:23:57 -07001030 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -07001031 }
1032
msarett2cee9022016-06-03 08:25:21 -07001033 while (fCurrentColorImage < fColorImages.count()) {
1034 fSourceType = "colorimage";
1035 fBenchType = "skcolorcodec";
1036 const SkString& path = fColorImages[fCurrentColorImage];
1037 fCurrentColorImage++;
1038 sk_sp<SkData> encoded = SkData::MakeFromFileName(path.c_str());
1039 if (encoded) {
1040 return new ColorCodecBench(SkOSPath::Basename(path.c_str()).c_str(),
1041 std::move(encoded));
1042 } else {
1043 SkDebugf("Could not read file %s.\n", path.c_str());
1044 }
1045 }
1046
halcanary96fcdcc2015-08-27 07:41:13 -07001047 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001048 }
mtklein92007582014-08-01 07:46:52 -07001049
1050 void fillCurrentOptions(ResultsWriter* log) const {
1051 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001052 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001053 if (0 == strcmp(fSourceType, "skp")) {
1054 log->configOption("clip",
1055 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1056 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001057 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -07001058 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001059 if (fCurrentUseMPD > 0) {
1060 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1061 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1062 }
mtklein92007582014-08-01 07:46:52 -07001063 }
mtklein051e56d2014-12-04 08:46:51 -08001064 if (0 == strcmp(fBenchType, "recording")) {
1065 log->metric("bytes", fSKPBytes);
1066 log->metric("ops", fSKPOps);
1067 }
mtklein92007582014-08-01 07:46:52 -07001068 }
1069
mtkleine714e752014-07-31 12:13:48 -07001070private:
msarettb23e6aa2015-06-09 13:56:10 -07001071 enum SubsetType {
1072 kTopLeft_SubsetType = 0,
1073 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001074 kMiddle_SubsetType = 2,
1075 kBottomLeft_SubsetType = 3,
1076 kBottomRight_SubsetType = 4,
1077 kTranslate_SubsetType = 5,
1078 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001079 kLast_SubsetType = kZoom_SubsetType,
1080 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001081 };
1082
mtkleine714e752014-07-31 12:13:48 -07001083 const BenchRegistry* fBenches;
1084 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001085 SkIRect fClip;
1086 SkTArray<SkScalar> fScales;
1087 SkTArray<SkString> fSKPs;
fmalita6519c212016-09-14 08:05:17 -07001088 SkTArray<SkString> fSVGs;
robertphillips5b693772014-11-21 06:19:36 -08001089 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001090 SkTArray<SkString> fImages;
msarett2cee9022016-06-03 08:25:21 -07001091 SkTArray<SkString> fColorImages;
msarett74deb982015-10-20 16:45:56 -07001092 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001093 SkScalar fZoomMax;
1094 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001095
mtklein051e56d2014-12-04 08:46:51 -08001096 double fSKPBytes, fSKPOps;
1097
mtkleinfd731ce2014-09-10 12:19:30 -07001098 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1099 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1100 int fCurrentRecording;
Mike Reed9cdd2ab2016-10-21 10:43:36 -04001101 int fCurrentPiping;
Mike Reede45ff462017-12-06 10:47:03 -05001102 int fCurrentDeserialPicture;
mtklein92007582014-08-01 07:46:52 -07001103 int fCurrentScale;
1104 int fCurrentSKP;
fmalita6519c212016-09-14 08:05:17 -07001105 int fCurrentSVG;
robertphillips5b693772014-11-21 06:19:36 -08001106 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001107 int fCurrentCodec;
msarett84451022016-02-11 06:45:51 -08001108 int fCurrentAndroidCodec;
msarett7f691442015-09-22 11:56:16 -07001109 int fCurrentBRDImage;
msarett2cee9022016-06-03 08:25:21 -07001110 int fCurrentColorImage;
msarett95f192d2015-02-13 09:05:41 -08001111 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -08001112 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -07001113 int fCurrentSubsetType;
msarett84451022016-02-11 06:45:51 -08001114 int fCurrentSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001115 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001116};
1117
msarettc149f0e2016-01-04 11:35:43 -08001118// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1119// This prints something every once in a while so that it knows we're still working.
1120static void start_keepalive() {
Mike Klein03141d22017-10-30 11:57:15 -04001121 static std::thread* intentionallyLeaked = new std::thread([]{
1122 for (;;) {
1123 static const int kSec = 1200;
1124 #if defined(SK_BUILD_FOR_WIN)
1125 Sleep(kSec * 1000);
1126 #else
1127 sleep(kSec);
1128 #endif
1129 SkDebugf("\nBenchmarks still running...\n");
msarettc149f0e2016-01-04 11:35:43 -08001130 }
Mike Klein03141d22017-10-30 11:57:15 -04001131 });
1132 (void)intentionallyLeaked;
msarettc149f0e2016-01-04 11:35:43 -08001133}
1134
Mike Kleinbe28ee22017-02-06 12:46:20 -05001135int main(int argc, char** argv) {
1136 SkCommandLineFlags::Parse(argc, argv);
Brian Osman53136aa2017-07-20 15:43:35 -04001137
Brian Osmanbc8150f2017-07-24 11:38:01 -04001138 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -04001139
Mike Kleinadacaef2017-02-06 09:26:14 -05001140#if defined(SK_BUILD_FOR_IOS)
1141 cd_Documents();
1142#endif
jcgregorio3b27ade2014-11-13 08:06:40 -08001143 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001144 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001145 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001146
krajcevski69a55602014-08-13 10:46:31 -07001147#if SK_SUPPORT_GPU
Chris Dalton040238b2017-12-18 14:22:34 -07001148 SetCtxOptionsFromCommonFlags(&grContextOpts);
krajcevski69a55602014-08-13 10:46:31 -07001149#endif
1150
bsalomon06cddec2014-10-24 10:40:50 -07001151 if (FLAGS_veryVerbose) {
1152 FLAGS_verbose = true;
1153 }
1154
bsalomon6eb03cc2014-08-07 14:28:50 -07001155 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001156 FLAGS_samples = 1;
1157 FLAGS_gpuFrameLag = 0;
1158 }
1159
bsalomon6eb03cc2014-08-07 14:28:50 -07001160 if (!FLAGS_writePath.isEmpty()) {
1161 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1162 if (!sk_mkdir(FLAGS_writePath[0])) {
1163 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001164 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001165 }
1166 }
1167
Ben Wagner145dbcd2016-11-03 14:40:50 -04001168 std::unique_ptr<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001169 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001170#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001171 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001172#else
1173 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1174 return 1;
1175#endif
mtklein60317d0f2014-07-14 11:30:37 -07001176 }
mtklein1915b622014-08-20 11:45:00 -07001177
1178 if (1 == FLAGS_properties.count() % 2) {
1179 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1180 return 1;
1181 }
1182 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1183 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1184 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001185
1186 if (1 == FLAGS_key.count() % 2) {
1187 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1188 return 1;
1189 }
1190 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001191 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001192 }
mtklein60317d0f2014-07-14 11:30:37 -07001193
mtkleinf3723212014-06-25 14:08:00 -07001194 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001195 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001196
cdaltone1b89582015-06-25 19:17:08 -07001197 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001198
bsalomon6eb03cc2014-08-07 14:28:50 -07001199 if (kAutoTuneLoops != FLAGS_loops) {
1200 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001201 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001202 SkDebugf("! -> high variance, ? -> moderate variance\n");
1203 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001204 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001205 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001206 } else {
mtkleind75c4662015-04-30 07:11:22 -07001207 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001208 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001209 }
1210
svaisanenc47635e2016-01-28 06:05:43 -08001211 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001212 create_configs(&configs);
1213
msarettc149f0e2016-01-04 11:35:43 -08001214 if (FLAGS_keepAlive) {
1215 start_keepalive();
1216 }
1217
Yuqian Liba62b4a2016-12-14 13:46:53 -05001218 gSkUseAnalyticAA = FLAGS_analyticAA;
Yuqian Lidf60e362017-07-25 11:26:31 -04001219 gSkUseDeltaAA = FLAGS_deltaAA;
liyuqian38911a72016-10-04 11:23:22 -07001220
Yuqian Lidf60e362017-07-25 11:26:31 -04001221 if (FLAGS_forceDeltaAA) {
1222 gSkForceDeltaAA = true;
1223 }
Yuqian Li550148b2017-01-13 10:13:13 -05001224 if (FLAGS_forceAnalyticAA) {
1225 gSkForceAnalyticAA = true;
1226 }
Mike Reedc928fe22017-06-05 10:20:31 -04001227 if (FLAGS_forceRasterPipeline) {
1228 gSkForceRasterPipelineBlitter = true;
1229 }
Yuqian Li550148b2017-01-13 10:13:13 -05001230
mtkleine070c2b2014-10-14 08:40:43 -07001231 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001232 BenchmarkStream benchStream;
1233 while (Benchmark* b = benchStream.next()) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001234 std::unique_ptr<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001235 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001236 continue;
1237 }
1238
svaisanenc47635e2016-01-28 06:05:43 -08001239 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001240 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001241 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001242 }
egdaniel3bf92062015-06-26 08:12:46 -07001243 for (int i = 0; i < configs.count(); ++i) {
1244 Target* target = is_enabled(b, configs[i]);
1245 if (!target) {
1246 continue;
1247 }
mtkleinf3723212014-06-25 14:08:00 -07001248
halcanary96fcdcc2015-08-27 07:41:13 -07001249 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001250 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001251 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001252
brianosman7a5ada82016-02-08 13:49:12 -08001253 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001254 SkDebugf("Running %s\t%s\n"
1255 , bench->getUniqueName()
1256 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001257 if (FLAGS_dryRun) {
1258 continue;
1259 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001260 }
1261
Brian Osman5ad87aa2017-09-08 16:05:23 -04001262 TRACE_EVENT2("skia", "Benchmark", "name", TRACE_STR_COPY(bench->getUniqueName()),
1263 "config", TRACE_STR_COPY(config));
Brian Osmanc3cdef52017-08-31 14:09:22 -04001264
egdaniel3bf92062015-06-26 08:12:46 -07001265 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001266 bench->perCanvasPreDraw(canvas);
1267
cdaltone1b89582015-06-25 19:17:08 -07001268 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001269 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001270 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1271 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001272
Leon Scroggins IIIb30d1132017-10-02 09:48:15 -04001273 if (kFailedLoops == loops) {
1274 // Can't be timed. A warning note has already been printed.
1275 cleanup_run(target);
1276 continue;
1277 }
1278
Yuqian Li3528eb32017-09-21 13:23:49 -04001279 if (runs == 0 && FLAGS_ms < 1000) {
1280 // Run the first bench for 1000ms to warm up the nanobench if FLAGS_ms < 1000.
1281 // Otherwise, the first few benches' measurements will be inaccurate.
1282 auto stop = now_ms() + 1000;
1283 do {
1284 time(loops, bench.get(), target);
1285 } while (now_ms() < stop);
1286 }
1287
mtkleinbbba1682015-10-28 11:36:30 -07001288 if (FLAGS_ms) {
1289 samples.reset();
1290 auto stop = now_ms() + FLAGS_ms;
1291 do {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001292 samples.push_back(time(loops, bench.get(), target) / loops);
mtkleinbbba1682015-10-28 11:36:30 -07001293 } while (now_ms() < stop);
1294 } else {
cdaltone1b89582015-06-25 19:17:08 -07001295 samples.reset(FLAGS_samples);
1296 for (int s = 0; s < FLAGS_samples; s++) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001297 samples[s] = time(loops, bench.get(), target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001298 }
cdaltone1b89582015-06-25 19:17:08 -07001299 }
mtkleinf3723212014-06-25 14:08:00 -07001300
joshualitte45c81c2015-12-02 09:05:37 -08001301#if SK_SUPPORT_GPU
1302 SkTArray<SkString> keys;
1303 SkTArray<double> values;
1304 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1305 if (gpuStatsDump) {
1306 // TODO cache stats
1307 bench->getGpuStats(canvas, &keys, &values);
1308 }
1309#endif
1310
robertphillips5b693772014-11-21 06:19:36 -08001311 bench->perCanvasPostDraw(canvas);
1312
egdaniel3bf92062015-06-26 08:12:46 -07001313 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001314 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001315 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001316 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001317 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001318 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001319 }
1320
cdaltone1b89582015-06-25 19:17:08 -07001321 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001322 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001323 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001324 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001325 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001326 log->metric("min_ms", stats.min);
George Burgess IV75b57182016-12-06 10:53:52 -08001327 log->metrics("samples", samples);
joshualitte45c81c2015-12-02 09:05:37 -08001328#if SK_SUPPORT_GPU
1329 if (gpuStatsDump) {
1330 // dump to json, only SKPBench currently returns valid keys / values
1331 SkASSERT(keys.count() == values.count());
1332 for (int i = 0; i < keys.count(); i++) {
1333 log->metric(keys[i].c_str(), values[i]);
1334 }
1335 }
1336#endif
1337
mtkleine070c2b2014-10-14 08:40:43 -07001338 if (runs++ % FLAGS_flushEvery == 0) {
1339 log->flush();
1340 }
mtklein60317d0f2014-07-14 11:30:37 -07001341
bsalomon6eb03cc2014-08-07 14:28:50 -07001342 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001343 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001344 config = ""; // Only print the config if we run the same bench on more than one.
1345 }
mtkleind75c4662015-04-30 07:11:22 -07001346 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1347 , sk_tools::getCurrResidentSetSizeMB()
1348 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001349 , bench->getUniqueName()
1350 , config);
mtkleinf3723212014-06-25 14:08:00 -07001351 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001352 const char* mark = " ";
1353 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1354 if (stddev_percent > 5) mark = "?";
1355 if (stddev_percent > 10) mark = "!";
1356
1357 SkDebugf("%10.2f %s\t%s\t%s\n",
1358 stats.median*1e3, mark, bench->getUniqueName(), config);
Mike Reedc665e5b2017-07-04 22:56:06 -04001359 } else if (FLAGS_csv) {
mtkleinf3723212014-06-25 14:08:00 -07001360 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
Mike Reedc665e5b2017-07-04 22:56:06 -04001361 SkDebugf("%g,%g,%g,%g,%g,%s,%s\n"
1362 , stats.min
1363 , stats.median
1364 , stats.mean
1365 , stats.max
1366 , stddev_percent
1367 , config
1368 , bench->getUniqueName()
1369 );
1370 } else {
1371 const char* format = "%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n";
1372 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1373 SkDebugf(format
mtkleind75c4662015-04-30 07:11:22 -07001374 , sk_tools::getCurrResidentSetSizeMB()
1375 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001376 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001377 , HUMANIZE(stats.min)
1378 , HUMANIZE(stats.median)
1379 , HUMANIZE(stats.mean)
1380 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001381 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001382 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001383 , config
mtklein96289052014-09-10 12:05:59 -07001384 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001385 );
1386 }
joshualitte45c81c2015-12-02 09:05:37 -08001387
bsalomonb12ea412015-02-02 21:19:50 -08001388#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001389 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -04001390 target->dumpStats();
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
mtkleinf3723212014-06-25 14:08:00 -07001411 return 0;
1412}