blob: db22c9de4c6ec9339f537a1c0d9e00dceb3d813e [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "bench/nanobench.h"
tomhudsond968a6f2015-03-26 11:28:06 -070011
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "bench/AndroidCodecBench.h"
13#include "bench/Benchmark.h"
14#include "bench/BitmapRegionDecoderBench.h"
15#include "bench/CodecBench.h"
16#include "bench/CodecBenchPriv.h"
17#include "bench/GMBench.h"
18#include "bench/RecordingBench.h"
19#include "bench/ResultsWriter.h"
20#include "bench/SKPAnimationBench.h"
21#include "bench/SKPBench.h"
Hal Canary5dfefa22019-10-24 13:52:17 -040022#include "bench/SkGlyphCacheBench.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/android/SkBitmapRegionDecoder.h"
24#include "include/codec/SkAndroidCodec.h"
25#include "include/codec/SkCodec.h"
26#include "include/core/SkCanvas.h"
27#include "include/core/SkData.h"
28#include "include/core/SkGraphics.h"
29#include "include/core/SkPictureRecorder.h"
30#include "include/core/SkString.h"
31#include "include/core/SkSurface.h"
Ben Wagneracf98df2019-07-12 12:51:44 -040032#include "include/core/SkTime.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/core/SkAutoMalloc.h"
34#include "src/core/SkBBoxHierarchy.h"
35#include "src/core/SkColorSpacePriv.h"
Ben Wagnerab6eefe2019-05-20 11:02:49 -040036#include "src/core/SkLeanWindows.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/core/SkOSFile.h"
38#include "src/core/SkTaskGroup.h"
39#include "src/core/SkTraceEvent.h"
40#include "src/utils/SkJSONWriter.h"
41#include "src/utils/SkOSPath.h"
Jim Van Verth8a9a3712019-05-31 10:49:12 -040042#include "tools/AutoreleasePool.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "tools/CrashHandler.h"
44#include "tools/ProcStats.h"
45#include "tools/Stats.h"
46#include "tools/flags/CommonFlags.h"
47#include "tools/flags/CommonFlagsConfig.h"
48#include "tools/ios_utils.h"
49#include "tools/trace/EventTracingPriv.h"
50#include "tools/trace/SkDebugfTracer.h"
mtkleinf3723212014-06-25 14:08:00 -070051
Hal Canary0f666812018-03-22 15:21:12 -040052#ifdef SK_XML
Mike Kleinc0bd9f92019-04-23 12:05:21 -050053#include "experimental/svg/model/SkSVGDOM.h"
Hal Canary0f666812018-03-22 15:21:12 -040054#endif // SK_XML
55
bungeman60e0fee2015-08-26 05:15:46 -070056#include <stdlib.h>
Mike Klein03141d22017-10-30 11:57:15 -040057#include <thread>
bungeman60e0fee2015-08-26 05:15:46 -070058
Mike Reedc928fe22017-06-05 10:20:31 -040059extern bool gSkForceRasterPipelineBlitter;
60
Mike Klein8f11d4d2018-01-24 12:42:55 -050061#ifndef SK_BUILD_FOR_WIN
scroggo38ce0a72016-01-07 07:28:47 -080062 #include <unistd.h>
Brian Salomon032aaae2018-03-23 16:10:36 -040063
scroggo38ce0a72016-01-07 07:28:47 -080064#endif
65
Mike Kleinc0bd9f92019-04-23 12:05:21 -050066#include "src/gpu/GrCaps.h"
67#include "src/gpu/GrContextPriv.h"
68#include "src/gpu/SkGr.h"
69#include "src/gpu/gl/GrGLDefines.h"
70#include "src/gpu/gl/GrGLGpu.h"
71#include "src/gpu/gl/GrGLUtil.h"
72#include "tools/gpu/GrContextFactory.h"
Brian Salomon032aaae2018-03-23 16:10:36 -040073
Brian Osmanc7ad40f2018-05-31 14:27:17 -040074using sk_gpu_test::ContextInfo;
75using sk_gpu_test::GrContextFactory;
76using sk_gpu_test::TestContext;
Brian Salomon032aaae2018-03-23 16:10:36 -040077
Brian Osmanc7ad40f2018-05-31 14:27:17 -040078GrContextOptions grContextOpts;
bsalomon682c2692015-05-22 14:01:46 -070079
reed53249782014-10-10 09:09:52 -070080static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070081
bsalomon6eb03cc2014-08-07 14:28:50 -070082static SkString loops_help_txt() {
83 SkString help;
84 help.printf("Number of times to run each bench. Set this to %d to auto-"
85 "tune for each bench. Timings are only reported when auto-tuning.",
86 kAutoTuneLoops);
87 return help;
88}
89
cdaltone1b89582015-06-25 19:17:08 -070090static SkString to_string(int n) {
91 SkString str;
92 str.appendS32(n);
93 return str;
94}
95
Mike Kleinac8f4432019-07-01 10:15:32 -050096static DEFINE_int(loops, kAutoTuneLoops, loops_help_txt().c_str());
bsalomon6eb03cc2014-08-07 14:28:50 -070097
Mike Klein5b3f3432019-03-21 11:42:21 -050098static DEFINE_int(samples, 10, "Number of samples to measure for each bench.");
99static DEFINE_int(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
100static DEFINE_int(overheadLoops, 100000, "Loops to estimate timer overhead.");
Mike Klein84836b72019-03-21 11:31:36 -0500101static DEFINE_double(overheadGoal, 0.0001,
mtkleinf3723212014-06-25 14:08:00 -0700102 "Loop until timer overhead is at most this fraction of our measurments.");
Mike Klein84836b72019-03-21 11:31:36 -0500103static DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
Mike Klein5b3f3432019-03-21 11:42:21 -0500104static DEFINE_int(gpuFrameLag, 5,
Mike Klein84836b72019-03-21 11:31:36 -0500105 "If unknown, estimated maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -0700106
Mike Klein84836b72019-03-21 11:31:36 -0500107static DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
Mike Klein5b3f3432019-03-21 11:42:21 -0500108static DEFINE_int(maxCalibrationAttempts, 3,
mtklein55b0ffc2014-07-17 08:38:23 -0700109 "Try up to this many times to guess loops for a bench, or skip the bench.");
Mike Klein5b3f3432019-03-21 11:42:21 -0500110static DEFINE_int(maxLoops, 1000000, "Never run a bench more times than this.");
Mike Klein84836b72019-03-21 11:31:36 -0500111static DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
112static DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
113static DEFINE_string(zoom, "1.0,0",
114 "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
115 "function that ping-pongs between 1.0 and zoomMax.");
116static DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
Mike Klein84836b72019-03-21 11:31:36 -0500117static DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
118static DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
Mike Klein5b3f3432019-03-21 11:42:21 -0500119static DEFINE_int(flushEvery, 10, "Flush --outResultsFile every Nth run.");
Mike Klein84836b72019-03-21 11:31:36 -0500120static DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
121static DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
122static DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
123static DEFINE_bool(csv, false, "Print status in CSV format");
124static DEFINE_string(sourceType, "",
mtklein65dfd2f2016-02-03 10:40:54 -0800125 "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
Mike Klein84836b72019-03-21 11:31:36 -0500126static DEFINE_string(benchType, "",
127 "Apply usual --match rules to bench type: micro, recording, "
128 "piping, playback, skcodec, etc.");
mtklein65dfd2f2016-02-03 10:40:54 -0800129
Mike Klein84836b72019-03-21 11:31:36 -0500130static DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
Mike Reedc928fe22017-06-05 10:20:31 -0400131
Mike Klein19fb3972019-03-21 13:08:08 -0500132static DEFINE_bool2(pre_log, p, false,
133 "Log before running each test. May be incomprehensible when threading");
134
Mike Klein629f5fc2019-03-22 14:55:19 -0500135static DEFINE_bool(cpu, true, "master switch for running CPU-bound work.");
136static DEFINE_bool(gpu, true, "master switch for running GPU-bound work.");
137static DEFINE_bool(dryRun, false,
138 "just print the tests that would be run, without actually running them.");
139static DEFINE_string(images, "",
140 "List of images and/or directories to decode. A directory with no images"
141 " is treated as a fatal error.");
142static DEFINE_bool(simpleCodec, false,
143 "Runs of a subset of the codec tests, always N32, Premul or Opaque");
144
Mike Kleinc6142d82019-03-25 10:54:59 -0500145static DEFINE_string2(match, m, nullptr,
146 "[~][^]substring[$] [...] of name to run.\n"
147 "Multiple matches may be separated by spaces.\n"
148 "~ causes a matching name to always be skipped\n"
149 "^ requires the start of the name to match\n"
150 "$ requires the end of the name to match\n"
151 "^ and $ requires an exact match\n"
152 "If a name does not match any list entry,\n"
153 "it is skipped unless some list entry starts with ~");
154
155static DEFINE_bool2(quiet, q, false, "if true, don't print status updates.");
156static DEFINE_bool2(verbose, v, false, "enable verbose output from the test driver.");
157
158
159static DEFINE_string(skps, "skps", "Directory to read skps from.");
160static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
Hal Canary5dfefa22019-10-24 13:52:17 -0400161static DEFINE_string(texttraces, "", "Directory to read TextBlobTrace files from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500162
163static DEFINE_int_2(threads, j, -1,
164 "Run threadsafe tests on a threadpool with this many extra threads, "
165 "defaulting to one extra thread per core.");
166
167static DEFINE_string2(writePath, w, "", "If set, write bitmaps here as .pngs.");
168
169static DEFINE_string(key, "",
170 "Space-separated key/value pairs to add to JSON identifying this builder.");
171static DEFINE_string(properties, "",
172 "Space-separated key/value pairs to add to JSON identifying this run.");
173
Mike Kleinee254232019-03-28 08:32:14 -0500174static DEFINE_bool(purgeBetweenBenches, false,
175 "Call SkGraphics::PurgeAllCaches() between each benchmark?");
176
mtkleinbbba1682015-10-28 11:36:30 -0700177static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
178
mtkleinf3723212014-06-25 14:08:00 -0700179static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700180 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800181 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700182}
mtklein55b0ffc2014-07-17 08:38:23 -0700183#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700184
tomhudsond968a6f2015-03-26 11:28:06 -0700185bool Target::init(SkImageInfo info, Benchmark* bench) {
186 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700187 this->surface = SkSurface::MakeRaster(info);
188 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700189 return false;
190 }
191 }
192 return true;
193}
194bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700195 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700196 if (!canvas) {
197 return false;
198 }
Mike Reed12e946b2017-04-17 10:53:29 -0400199 bmp->allocPixels(canvas->imageInfo());
200 if (!canvas->readPixels(*bmp, 0, 0)) {
tomhudsond968a6f2015-03-26 11:28:06 -0700201 SkDebugf("Can't read canvas pixels.\n");
202 return false;
203 }
204 return true;
205}
206
tomhudsond968a6f2015-03-26 11:28:06 -0700207struct GPUTarget : public Target {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400208 explicit GPUTarget(const Config& c) : Target(c) {}
209 ContextInfo contextInfo;
210 std::unique_ptr<GrContextFactory> factory;
tomhudsond968a6f2015-03-26 11:28:06 -0700211
212 void setup() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400213 this->contextInfo.testContext()->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700214 // Make sure we're done with whatever came before.
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400215 this->contextInfo.testContext()->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700216 }
217 void endTiming() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400218 if (this->contextInfo.testContext()) {
219 this->contextInfo.testContext()->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700220 }
221 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400222 void fence() override { this->contextInfo.testContext()->finish(); }
mtkleind75c4662015-04-30 07:11:22 -0700223
cdaltond416a5b2015-06-23 13:23:44 -0700224 bool needsFrameTiming(int* maxFrameLag) const override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400225 if (!this->contextInfo.testContext()->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700226 // Frame lag is unknown.
227 *maxFrameLag = FLAGS_gpuFrameLag;
228 }
229 return true;
230 }
tomhudsond968a6f2015-03-26 11:28:06 -0700231 bool init(SkImageInfo info, Benchmark* bench) override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400232 GrContextOptions options = grContextOpts;
233 bench->modifyGrContextOptions(&options);
234 this->factory.reset(new GrContextFactory(options));
bsalomonafcd7cd2015-08-31 12:39:41 -0700235 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
236 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700237 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400238 this->surface = SkSurface::MakeRenderTarget(
239 this->factory->get(this->config.ctxType, this->config.ctxOverrides),
240 SkBudgeted::kNo, info, this->config.samples, &props);
241 this->contextInfo =
242 this->factory->getContextInfo(this->config.ctxType, this->config.ctxOverrides);
tomhudsond968a6f2015-03-26 11:28:06 -0700243 if (!this->surface.get()) {
244 return false;
245 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400246 if (!this->contextInfo.testContext()->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700247 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800248 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700249 }
tomhudsond968a6f2015-03-26 11:28:06 -0700250 return true;
251 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500252 void fillOptions(NanoJSONResultsWriter& log) override {
tomhudsond968a6f2015-03-26 11:28:06 -0700253 const GrGLubyte* version;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400254 if (this->contextInfo.backend() == GrBackendApi::kOpenGL) {
Brian Salomon032aaae2018-03-23 16:10:36 -0400255 const GrGLInterface* gl =
Robert Phillips9da87e02019-02-04 13:26:26 -0500256 static_cast<GrGLGpu*>(this->contextInfo.grContext()->priv().getGpu())
Brian Salomon032aaae2018-03-23 16:10:36 -0400257 ->glInterface();
bsalomonc8699322016-05-11 11:55:36 -0700258 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500259 log.appendString("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700260
bsalomonc8699322016-05-11 11:55:36 -0700261 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500262 log.appendString("GL_RENDERER", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700263
bsalomonc8699322016-05-11 11:55:36 -0700264 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VENDOR));
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500265 log.appendString("GL_VENDOR", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700266
bsalomonc8699322016-05-11 11:55:36 -0700267 GR_GL_CALL_RET(gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500268 log.appendString("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
bsalomonc8699322016-05-11 11:55:36 -0700269 }
tomhudsond968a6f2015-03-26 11:28:06 -0700270 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400271
272 void dumpStats() override {
Robert Phillips9da87e02019-02-04 13:26:26 -0500273 this->contextInfo.grContext()->priv().printCacheStats();
274 this->contextInfo.grContext()->priv().printGpuStats();
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400275 }
tomhudsond968a6f2015-03-26 11:28:06 -0700276};
mtkleind75c4662015-04-30 07:11:22 -0700277
tomhudson75a0ebb2015-03-27 12:11:44 -0700278static double time(int loops, Benchmark* bench, Target* target) {
279 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700280 if (canvas) {
281 canvas->clear(SK_ColorWHITE);
282 }
joshualitt8a6697a2015-09-30 12:11:07 -0700283 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700284 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700285 canvas = target->beginTiming(canvas);
286 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700287 if (canvas) {
288 canvas->flush();
289 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700290 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700291 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700292 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700293 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700294}
295
mtkleinf3723212014-06-25 14:08:00 -0700296static double estimate_timer_overhead() {
297 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700298 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700299 double start = now_ms();
300 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700301 }
302 return overhead / FLAGS_overheadLoops;
303}
304
reed53249782014-10-10 09:09:52 -0700305static int detect_forever_loops(int loops) {
306 // look for a magic run-forever value
307 if (loops < 0) {
308 loops = SK_MaxS32;
309 }
310 return loops;
311}
312
mtklein55b0ffc2014-07-17 08:38:23 -0700313static int clamp_loops(int loops) {
314 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800315 SkDebugf("ERROR: clamping loops from %d to 1. "
316 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700317 return 1;
318 }
319 if (loops > FLAGS_maxLoops) {
320 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
321 return FLAGS_maxLoops;
322 }
323 return loops;
324}
325
tomhudsond968a6f2015-03-26 11:28:06 -0700326static bool write_canvas_png(Target* target, const SkString& filename) {
327
bsalomon6eb03cc2014-08-07 14:28:50 -0700328 if (filename.isEmpty()) {
329 return false;
330 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700331 if (target->getCanvas() &&
332 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700333 return false;
334 }
tomhudsond968a6f2015-03-26 11:28:06 -0700335
bsalomon6eb03cc2014-08-07 14:28:50 -0700336 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700337
338 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700339 return false;
340 }
tomhudsond968a6f2015-03-26 11:28:06 -0700341
bsalomon6eb03cc2014-08-07 14:28:50 -0700342 SkString dir = SkOSPath::Dirname(filename.c_str());
343 if (!sk_mkdir(dir.c_str())) {
344 SkDebugf("Can't make dir %s.\n", dir.c_str());
345 return false;
346 }
347 SkFILEWStream stream(filename.c_str());
348 if (!stream.isValid()) {
349 SkDebugf("Can't write %s.\n", filename.c_str());
350 return false;
351 }
Hal Canarydb683012016-11-23 08:55:18 -0700352 if (!SkEncodeImage(&stream, bmp, SkEncodedImageFormat::kPNG, 100)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700353 SkDebugf("Can't encode a PNG.\n");
354 return false;
355 }
356 return true;
357}
358
359static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700360static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700361 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700362 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700363 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700364 int loops = bench->calculateLoops(FLAGS_loops);
365 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700366 while (bench_plus_overhead < overhead) {
367 if (round++ == FLAGS_maxCalibrationAttempts) {
368 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700369 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700370 return kFailedLoops;
371 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700372 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700373 }
mtklein2069e222014-08-04 13:57:39 -0700374 }
mtkleinf3723212014-06-25 14:08:00 -0700375
mtkleinbb6a0282014-07-01 08:43:42 -0700376 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700377 // We'll pick N to make timer overhead negligible:
378 //
mtkleinbb6a0282014-07-01 08:43:42 -0700379 // overhead
380 // ------------------------- < FLAGS_overheadGoal
381 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700382 //
Hal Canary55325b72017-01-03 10:36:17 -0500383 // where bench_plus_overhead ~=~ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700384 //
385 // Doing some math, we get:
386 //
mtkleinbb6a0282014-07-01 08:43:42 -0700387 // (overhead / FLAGS_overheadGoal) - overhead
388 // ------------------------------------------ < N
389 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700390 //
391 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700392 if (kAutoTuneLoops == loops) {
393 const double numer = overhead / FLAGS_overheadGoal - overhead;
394 const double denom = bench_plus_overhead - overhead;
395 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700396 loops = clamp_loops(loops);
397 } else {
398 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700399 }
mtkleinbb6a0282014-07-01 08:43:42 -0700400
mtkleinbb6a0282014-07-01 08:43:42 -0700401 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700402}
403
cdaltone1b89582015-06-25 19:17:08 -0700404static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700405 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700406 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700407 if (kAutoTuneLoops == loops) {
408 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700409 double elapsed = 0;
410 do {
mtklein527930f2014-11-06 08:04:34 -0800411 if (1<<30 == loops) {
412 // We're about to wrap. Something's wrong with the bench.
413 loops = 0;
414 break;
415 }
mtkleina189ccd2014-07-14 12:28:47 -0700416 loops *= 2;
417 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700418 // _this_ round, not still timing last round.
419 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700420 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700421 }
422 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700423
mtkleina189ccd2014-07-14 12:28:47 -0700424 // We've overshot at least a little. Scale back linearly.
425 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700426 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700427
tomhudsond968a6f2015-03-26 11:28:06 -0700428 // Make sure we're not still timing our calibration.
429 target->fence();
reed53249782014-10-10 09:09:52 -0700430 } else {
431 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700432 }
mtkleinbb6a0282014-07-01 08:43:42 -0700433 // Pretty much the same deal as the calibration: do some warmup to make
434 // sure we're timing steady-state pipelined frames.
Brian Osman01776982017-10-10 15:41:03 -0400435 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700436 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700437 }
mtkleinbb6a0282014-07-01 08:43:42 -0700438
mtkleinbb6a0282014-07-01 08:43:42 -0700439 return loops;
440}
mtkleinbb6a0282014-07-01 08:43:42 -0700441
Brian Salomon6405e712017-03-20 08:54:16 -0400442#define kBogusContextType GrContextFactory::kGL_ContextType
csmartdaltone812d492017-02-21 12:36:05 -0700443#define kBogusContextOverrides GrContextFactory::ContextOverrides::kNone
bsalomonc2553372014-07-22 13:09:05 -0700444
svaisanenc47635e2016-01-28 06:05:43 -0800445static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
svaisanenc47635e2016-01-28 06:05:43 -0800446 if (const auto* gpuConfig = config->asConfigGpu()) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400447 if (!FLAGS_gpu) {
448 SkDebugf("Skipping config '%s' as requested.\n", config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800449 return;
Ben Wagner32fa5102017-08-10 21:25:55 -0400450 }
svaisanenc47635e2016-01-28 06:05:43 -0800451
svaisanenc47635e2016-01-28 06:05:43 -0800452 const auto ctxType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700453 const auto ctxOverrides = gpuConfig->getContextOverrides();
svaisanenc47635e2016-01-28 06:05:43 -0800454 const auto sampleCount = gpuConfig->getSamples();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400455 const auto colorType = gpuConfig->getColorType();
456 auto colorSpace = gpuConfig->getColorSpace();
Brian Salomonf865b052018-03-09 09:01:53 -0500457 if (gpuConfig->getSurfType() != SkCommandLineConfigGpu::SurfType::kDefault) {
458 SkDebugf("This tool only supports the default surface type.");
459 return;
460 }
svaisanenc47635e2016-01-28 06:05:43 -0800461
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400462 GrContextFactory factory(grContextOpts);
463 if (const GrContext* ctx = factory.get(ctxType, ctxOverrides)) {
Greg Daniel6fa62e22019-08-07 15:52:37 -0400464 GrBackendFormat format = ctx->defaultBackendFormat(colorType, GrRenderable::kYes);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500465 int supportedSampleCount =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400466 ctx->priv().caps()->getRenderTargetSampleCount(sampleCount, format);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400467 if (sampleCount != supportedSampleCount) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400468 SkDebugf("Configuration '%s' sample count %d is not a supported sample count.\n",
469 config->getTag().c_str(), sampleCount);
svaisanenc47635e2016-01-28 06:05:43 -0800470 return;
471 }
472 } else {
Ben Wagner32fa5102017-08-10 21:25:55 -0400473 SkDebugf("No context was available matching config '%s'.\n",
474 config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800475 return;
476 }
477
478 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800479 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800480 Benchmark::kGPU_Backend,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400481 colorType,
svaisanenc47635e2016-01-28 06:05:43 -0800482 kPremul_SkAlphaType,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400483 sk_ref_sp(colorSpace),
svaisanenc47635e2016-01-28 06:05:43 -0800484 sampleCount,
485 ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700486 ctxOverrides,
kkinnunend4e1c352016-03-01 23:41:26 -0800487 gpuConfig->getUseDIText()
488 };
svaisanenc47635e2016-01-28 06:05:43 -0800489
490 configs->push_back(target);
491 return;
492 }
svaisanenc47635e2016-01-28 06:05:43 -0800493
brianosmanb109b8c2016-06-16 13:03:24 -0700494 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
495 if (config->getTag().equals(#name)) { \
Ben Wagner32fa5102017-08-10 21:25:55 -0400496 if (!FLAGS_cpu) { \
497 SkDebugf("Skipping config '%s' as requested.\n", \
498 config->getTag().c_str()); \
499 return; \
500 } \
brianosmanb109b8c2016-06-16 13:03:24 -0700501 Config config = { \
502 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
csmartdaltone812d492017-02-21 12:36:05 -0700503 0, kBogusContextType, kBogusContextOverrides, false \
brianosmanb109b8c2016-06-16 13:03:24 -0700504 }; \
505 configs->push_back(config); \
506 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700507 }
mtkleine714e752014-07-31 12:13:48 -0700508
Ben Wagner32fa5102017-08-10 21:25:55 -0400509 CPU_CONFIG(nonrendering, kNonRendering_Backend,
510 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800511
Mike Klein0e4041f2018-06-19 16:00:40 -0400512 CPU_CONFIG(a8, kRaster_Backend, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr)
513 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType, nullptr)
514 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
515
516 // 'narrow' has a gamut narrower than sRGB, and different transfer function.
Brian Osman82ebe042019-01-04 17:03:00 -0500517 auto narrow = SkColorSpace::MakeRGB(SkNamedTransferFn::k2Dot2, gNarrow_toXYZD50),
Mike Klein0e4041f2018-06-19 16:00:40 -0400518 srgb = SkColorSpace::MakeSRGB(),
519 srgbLinear = SkColorSpace::MakeSRGBLinear();
520
521 CPU_CONFIG( f16, kRaster_Backend, kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbLinear)
522 CPU_CONFIG( srgb, kRaster_Backend, kRGBA_8888_SkColorType, kPremul_SkAlphaType, srgb )
523 CPU_CONFIG( esrgb, kRaster_Backend, kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgb )
524 CPU_CONFIG( narrow, kRaster_Backend, kRGBA_8888_SkColorType, kPremul_SkAlphaType, narrow )
525 CPU_CONFIG(enarrow, kRaster_Backend, kRGBA_F16_SkColorType, kPremul_SkAlphaType, narrow )
mtkleinbb6a0282014-07-01 08:43:42 -0700526
svaisanenc47635e2016-01-28 06:05:43 -0800527 #undef CPU_CONFIG
Ben Wagner32fa5102017-08-10 21:25:55 -0400528
529 SkDebugf("Unknown config '%s'.\n", config->getTag().c_str());
mtkleinf3723212014-06-25 14:08:00 -0700530}
531
svaisanenc47635e2016-01-28 06:05:43 -0800532// Append all configs that are enabled and supported.
533void create_configs(SkTArray<Config>* configs) {
534 SkCommandLineConfigArray array;
535 ParseConfigs(FLAGS_config, &array);
536 for (int i = 0; i < array.count(); ++i) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400537 create_config(array[i].get(), configs);
svaisanenc47635e2016-01-28 06:05:43 -0800538 }
Ben Wagner32fa5102017-08-10 21:25:55 -0400539
540 // If no just default configs were requested, then we're okay.
541 if (array.count() == 0 || FLAGS_config.count() == 0 ||
Ben Wagner32fa5102017-08-10 21:25:55 -0400542 // Otherwise, make sure that all specified configs have been created.
543 array.count() == configs->count()) {
544 return;
545 }
Ben Wagner32fa5102017-08-10 21:25:55 -0400546 exit(1);
svaisanenc47635e2016-01-28 06:05:43 -0800547}
548
brianosman9f1f6e22016-09-15 08:33:02 -0700549// disable warning : switch statement contains default but no 'case' labels
550#if defined _WIN32
551#pragma warning ( push )
552#pragma warning ( disable : 4065 )
553#endif
554
halcanary96fcdcc2015-08-27 07:41:13 -0700555// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700556static Target* is_enabled(Benchmark* bench, const Config& config) {
557 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700558 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700559 }
560
reede5ea5002014-09-03 11:54:58 -0700561 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700562 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700563
halcanary96fcdcc2015-08-27 07:41:13 -0700564 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700565
tomhudsond968a6f2015-03-26 11:28:06 -0700566 switch (config.backend) {
tomhudsond968a6f2015-03-26 11:28:06 -0700567 case Benchmark::kGPU_Backend:
568 target = new GPUTarget(config);
569 break;
tomhudsond968a6f2015-03-26 11:28:06 -0700570 default:
571 target = new Target(config);
572 break;
573 }
bsalomonc2553372014-07-22 13:09:05 -0700574
tomhudsond968a6f2015-03-26 11:28:06 -0700575 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700576 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700577 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700578 }
579 return target;
580}
581
brianosman9f1f6e22016-09-15 08:33:02 -0700582#if defined _WIN32
583#pragma warning ( pop )
584#endif
585
reed42943c82016-09-12 12:01:44 -0700586static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700587 uint32_t minOutputSize, int* width, int* height) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400588 std::unique_ptr<SkBitmapRegionDecoder> brd(
msarettd1227a72016-05-18 06:23:57 -0700589 SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy));
msarett7f691442015-09-22 11:56:16 -0700590 if (nullptr == brd.get()) {
591 // This is indicates that subset decoding is not supported for a particular image format.
592 return false;
593 }
594
msarett7f691442015-09-22 11:56:16 -0700595 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
596 (uint32_t) brd->height()) {
597 // This indicates that the image is not large enough to decode a
598 // minOutputSize x minOutputSize subset at the given sampleSize.
599 return false;
600 }
601
602 // Set the image width and height. The calling code will use this to choose subsets to decode.
603 *width = brd->width();
604 *height = brd->height();
605 return true;
606}
607
egdaniel3bf92062015-06-26 08:12:46 -0700608static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700609 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700610}
611
Mike Klein88544fb2019-03-20 10:50:33 -0500612static void collect_files(const CommandLineFlags::StringArray& paths,
613 const char* ext,
614 SkTArray<SkString>* list) {
fmalita6519c212016-09-14 08:05:17 -0700615 for (int i = 0; i < paths.count(); ++i) {
616 if (SkStrEndsWith(paths[i], ext)) {
617 list->push_back(SkString(paths[i]));
618 } else {
619 SkOSFile::Iter it(paths[i], ext);
620 SkString path;
621 while (it.next(&path)) {
622 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
623 }
624 }
625 }
626}
627
mtkleine714e752014-07-31 12:13:48 -0700628class BenchmarkStream {
629public:
mtklein92007582014-08-01 07:46:52 -0700630 BenchmarkStream() : fBenches(BenchRegistry::Head())
Hal Canary2362ae72019-10-24 13:40:40 -0400631 , fGMs(skiagm::GMRegistry::Head()) {
fmalita6519c212016-09-14 08:05:17 -0700632 collect_files(FLAGS_skps, ".skp", &fSKPs);
633 collect_files(FLAGS_svgs, ".svg", &fSVGs);
Hal Canary5dfefa22019-10-24 13:52:17 -0400634 collect_files(FLAGS_texttraces, ".trace", &fTextBlobTraces);
mtkleine714e752014-07-31 12:13:48 -0700635
mtklein92007582014-08-01 07:46:52 -0700636 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
637 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
638 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
639 exit(1);
640 }
641
642 for (int i = 0; i < FLAGS_scales.count(); i++) {
643 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
644 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
645 exit(1);
646 }
647 }
robertphillips5b693772014-11-21 06:19:36 -0800648
cdalton63a82852015-06-29 14:06:10 -0700649 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
650 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700651 exit(1);
652 }
653
robertphillips5b693772014-11-21 06:19:36 -0800654 if (FLAGS_mpd) {
655 fUseMPDs.push_back() = true;
656 }
mtkleinc751ecb2015-06-15 08:56:38 -0700657 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700658
msarett95f192d2015-02-13 09:05:41 -0800659 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700660 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800661 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800662 }
mtklein95553d92015-03-12 08:24:21 -0700663
msarett95f192d2015-02-13 09:05:41 -0800664 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700665 fColorTypes.push_back(kN32_SkColorType);
666 if (!FLAGS_simpleCodec) {
667 fColorTypes.push_back(kRGB_565_SkColorType);
668 fColorTypes.push_back(kAlpha_8_SkColorType);
msarett67cb6662016-06-21 08:49:26 -0700669 fColorTypes.push_back(kGray_8_SkColorType);
670 }
mtklein92007582014-08-01 07:46:52 -0700671 }
672
reedca2622b2016-03-18 07:25:55 -0700673 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700674 // Not strictly necessary, as it will be checked again later,
675 // but helps to avoid a lot of pointless work if we're going to skip it.
Mike Klein88544fb2019-03-20 10:50:33 -0500676 if (CommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700677 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700678 }
679
bungemanf93d7112016-09-16 06:24:20 -0700680 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
681 if (!stream) {
mtkleinfd731ce2014-09-10 12:19:30 -0700682 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700683 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700684 }
685
reedca2622b2016-03-18 07:25:55 -0700686 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700687 }
688
fmalita6519c212016-09-14 08:05:17 -0700689 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
Brian Osman133823d2018-09-19 14:14:15 -0400690 sk_sp<SkData> data(SkData::MakeFromFileName(path));
691 if (!data) {
fmalita6519c212016-09-14 08:05:17 -0700692 SkDebugf("Could not read %s.\n", path);
693 return nullptr;
694 }
695
Hal Canary0f666812018-03-22 15:21:12 -0400696#ifdef SK_XML
Brian Osman133823d2018-09-19 14:14:15 -0400697 SkMemoryStream stream(std::move(data));
fmalitae1baa7c2016-09-14 12:04:30 -0700698 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
fmalita6519c212016-09-14 08:05:17 -0700699 if (!svgDom) {
700 SkDebugf("Could not parse %s.\n", path);
701 return nullptr;
702 }
703
fmalitae1baa7c2016-09-14 12:04:30 -0700704 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
705 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
706 if (svgDom->containerSize().isEmpty()) {
707 svgDom->setContainerSize(kDefaultContainerSize);
708 }
709
fmalita6519c212016-09-14 08:05:17 -0700710 SkPictureRecorder recorder;
fmalitae1baa7c2016-09-14 12:04:30 -0700711 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
712 svgDom->containerSize().height()));
fmalita6519c212016-09-14 08:05:17 -0700713 return recorder.finishRecordingAsPicture();
Hal Canary0f666812018-03-22 15:21:12 -0400714#else
715 return nullptr;
716#endif // SK_XML
fmalita6519c212016-09-14 08:05:17 -0700717 }
718
mtklein92007582014-08-01 07:46:52 -0700719 Benchmark* next() {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400720 std::unique_ptr<Benchmark> bench;
mtklein65dfd2f2016-02-03 10:40:54 -0800721 do {
722 bench.reset(this->rawNext());
723 if (!bench) {
724 return nullptr;
725 }
Mike Klein88544fb2019-03-20 10:50:33 -0500726 } while (CommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
727 CommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700728 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800729 }
730
731 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700732 if (fBenches) {
Hal Canary972eba32018-07-30 17:07:07 -0400733 Benchmark* bench = fBenches->get()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700734 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700735 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700736 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700737 return bench;
738 }
mtklein92007582014-08-01 07:46:52 -0700739
mtkleine714e752014-07-31 12:13:48 -0700740 while (fGMs) {
Ben Wagner406ff502019-08-12 16:39:24 -0400741 std::unique_ptr<skiagm::GM> gm = fGMs->get()();
mtkleine714e752014-07-31 12:13:48 -0700742 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800743 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700744 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700745 fBenchType = "micro";
Ben Wagner406ff502019-08-12 16:39:24 -0400746 return new GMBench(std::move(gm));
mtkleine714e752014-07-31 12:13:48 -0700747 }
748 }
mtklein92007582014-08-01 07:46:52 -0700749
Hal Canary5dfefa22019-10-24 13:52:17 -0400750 while (fCurrentTextBlobTrace < fTextBlobTraces.count()) {
751 SkString path = fTextBlobTraces[fCurrentTextBlobTrace++];
752 SkString basename = SkOSPath::Basename(path.c_str());
753 static constexpr char kEnding[] = ".trace";
754 if (basename.endsWith(kEnding)) {
755 basename.remove(basename.size() - strlen(kEnding), strlen(kEnding));
756 }
757 fSourceType = "texttrace";
758 fBenchType = "micro";
759 return CreateDiffCanvasBench(
760 SkStringPrintf("SkDiffBench-%s", basename.c_str()),
761 [path](){ return SkStream::MakeFromFile(path.c_str()); });
762 }
763
mtkleinfd731ce2014-09-10 12:19:30 -0700764 // First add all .skps as RecordingBenches.
765 while (fCurrentRecording < fSKPs.count()) {
766 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700767 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
768 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700769 continue;
770 }
771 SkString name = SkOSPath::Basename(path.c_str());
772 fSourceType = "skp";
773 fBenchType = "recording";
Mike Klein88d90712018-01-27 17:30:04 +0000774 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
775 fSKPOps = pic->approximateOpCount();
Mike Kleina705cb92019-05-14 12:33:40 -0500776 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700777 }
778
Mike Reede45ff462017-12-06 10:47:03 -0500779 // Add all .skps as DeserializePictureBenchs.
780 while (fCurrentDeserialPicture < fSKPs.count()) {
781 const SkString& path = fSKPs[fCurrentDeserialPicture++];
782 sk_sp<SkData> data = SkData::MakeFromFileName(path.c_str());
783 if (!data) {
784 continue;
785 }
786 SkString name = SkOSPath::Basename(path.c_str());
787 fSourceType = "skp";
788 fBenchType = "deserial";
Mike Reed7557bbb2017-12-24 19:50:57 -0500789 fSKPBytes = static_cast<double>(data->size());
Mike Klein88d90712018-01-27 17:30:04 +0000790 fSKPOps = 0;
Mike Reede45ff462017-12-06 10:47:03 -0500791 return new DeserializePictureBench(name.c_str(), std::move(data));
792 }
793
mtkleinfd731ce2014-09-10 12:19:30 -0700794 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700795 while (fCurrentScale < fScales.count()) {
796 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800797 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700798 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
799 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800800 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700801 continue;
802 }
robertphillips5b693772014-11-21 06:19:36 -0800803
804 while (fCurrentUseMPD < fUseMPDs.count()) {
805 if (FLAGS_bbh) {
806 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
807 SkRTreeFactory factory;
808 SkPictureRecorder recorder;
robertphillips5b693772014-11-21 06:19:36 -0800809 pic->playback(recorder.beginRecording(pic->cullRect().width(),
810 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800811 &factory,
robertphillipsdda54452016-07-13 13:27:16 -0700812 0));
reedca2622b2016-03-18 07:25:55 -0700813 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800814 }
815 SkString name = SkOSPath::Basename(path.c_str());
816 fSourceType = "skp";
817 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700818 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
819 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700820 }
robertphillips5b693772014-11-21 06:19:36 -0800821 fCurrentUseMPD = 0;
822 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700823 }
fmalita6519c212016-09-14 08:05:17 -0700824
825 while (fCurrentSVG++ < fSVGs.count()) {
826 const char* path = fSVGs[fCurrentSVG - 1].c_str();
827 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
828 fSourceType = "svg";
829 fBenchType = "playback";
830 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
831 fScales[fCurrentScale], false, FLAGS_loopSKP);
832 }
833 }
834
mtklein92007582014-08-01 07:46:52 -0700835 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700836 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700837 fCurrentScale++;
838 }
839
joshualitt261c3ad2015-04-27 09:16:57 -0700840 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700841 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700842 while (fCurrentAnimSKP < fSKPs.count()) {
843 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700844 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
845 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700846 fCurrentAnimSKP++;
847 continue;
848 }
849
850 fCurrentAnimSKP++;
851 SkString name = SkOSPath::Basename(path.c_str());
Ben Wagneracf98df2019-07-12 12:51:44 -0400852 sk_sp<SKPAnimationBench::Animation> animation =
853 SKPAnimationBench::MakeZoomAnimation(fZoomMax, fZoomPeriodMs);
854 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, std::move(animation),
halcanary385fe4d2015-08-26 13:07:48 -0700855 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700856 }
857 }
858
scroggo60869a42015-04-01 12:09:17 -0700859 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700860 fSourceType = "image";
861 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700862 const SkString& path = fImages[fCurrentCodec];
Mike Klein88544fb2019-03-20 10:50:33 -0500863 if (CommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
mtklein6f0ff912016-01-11 09:04:21 -0800864 continue;
865 }
bungeman38d909e2016-08-02 14:40:46 -0700866 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400867 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700868 if (!codec) {
869 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700870 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700871 continue;
872 }
scroggo21027992015-04-02 13:22:38 -0700873
scroggo60869a42015-04-01 12:09:17 -0700874 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700875 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700876
msarettc7796b92016-01-07 14:20:20 -0800877 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700878 if (FLAGS_simpleCodec) {
879 if (kUnpremul_SkAlphaType == alphaType) {
880 alphaType = kPremul_SkAlphaType;
881 }
882
883 fCurrentColorType++;
884 } else {
885 switch (alphaType) {
886 case kOpaque_SkAlphaType:
887 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800888 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700889 break;
890 case kUnpremul_SkAlphaType:
891 case kPremul_SkAlphaType:
892 if (0 == fCurrentAlphaType) {
893 // Test unpremul first.
894 alphaType = kUnpremul_SkAlphaType;
895 fCurrentAlphaType++;
896 } else {
897 // Test premul.
898 alphaType = kPremul_SkAlphaType;
899 fCurrentAlphaType = 0;
900 fCurrentColorType++;
901 }
902 break;
903 default:
904 SkASSERT(false);
905 fCurrentColorType++;
906 break;
907 }
scroggo21027992015-04-02 13:22:38 -0700908 }
909
msarettc7796b92016-01-07 14:20:20 -0800910 // Make sure we can decode to this color type and alpha type.
911 SkImageInfo info =
912 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700913 const size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400914 SkAutoMalloc storage(info.computeByteSize(rowBytes));
scroggo21027992015-04-02 13:22:38 -0700915
scroggoeb602a52015-07-09 08:16:03 -0700916 const SkCodec::Result result = codec->getPixels(
Leon Scroggins571b30f2017-07-11 17:35:31 +0000917 info, storage.get(), rowBytes);
scroggo60869a42015-04-01 12:09:17 -0700918 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700919 case SkCodec::kSuccess:
920 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700921 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700922 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700923 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700924 // This is okay. Not all conversions are valid.
925 break;
scroggo60869a42015-04-01 12:09:17 -0700926 default:
927 // This represents some sort of failure.
928 SkASSERT(false);
929 break;
930 }
931 }
932 fCurrentColorType = 0;
933 }
934
msarett84451022016-02-11 06:45:51 -0800935 // Run AndroidCodecBenches
936 const int sampleSizes[] = { 2, 4, 8 };
937 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
938 fSourceType = "image";
939 fBenchType = "skandroidcodec";
940
941 const SkString& path = fImages[fCurrentAndroidCodec];
Mike Klein88544fb2019-03-20 10:50:33 -0500942 if (CommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
msarett84451022016-02-11 06:45:51 -0800943 continue;
944 }
bungeman38d909e2016-08-02 14:40:46 -0700945 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400946 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800947 if (!codec) {
948 // Nothing to time.
949 SkDebugf("Cannot find codec for %s\n", path.c_str());
950 continue;
951 }
952
953 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
954 int sampleSize = sampleSizes[fCurrentSampleSize];
955 fCurrentSampleSize++;
956 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
957 // Avoid benchmarking scaled decodes of already small images.
958 break;
959 }
960
bungeman38d909e2016-08-02 14:40:46 -0700961 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
962 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -0800963 }
964 fCurrentSampleSize = 0;
965 }
966
msarett7f691442015-09-22 11:56:16 -0700967 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700968 // We intend to create benchmarks that model the use cases in
969 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
970 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
971 // the image. For that reason, we will benchmark decodes in five representative locations
972 // in the image. Additionally, this use case utilizes power of two scaling, so we will
973 // test on power of two sample sizes. The output tile is always 512x512, so, when a
974 // sampleSize is used, the size of the subset that is decoded is always
975 // (sampleSize*512)x(sampleSize*512).
976 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700977 // All use cases we are aware of only scale by powers of two.
978 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
979 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800980 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700981 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800982 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700983 fSourceType = "image";
984 fBenchType = "BRD";
985
mtklein6f0ff912016-01-11 09:04:21 -0800986 const SkString& path = fImages[fCurrentBRDImage];
Mike Klein88544fb2019-03-20 10:50:33 -0500987 if (CommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
mtklein6f0ff912016-01-11 09:04:21 -0800988 continue;
989 }
scroggo860e8a62015-10-15 07:51:28 -0700990
msarettd1227a72016-05-18 06:23:57 -0700991 while (fCurrentColorType < fColorTypes.count()) {
992 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
993 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700994
bungeman38d909e2016-08-02 14:40:46 -0700995 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -0700996 const SkColorType colorType = fColorTypes[fCurrentColorType];
997 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
998 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -0700999
msarettd1227a72016-05-18 06:23:57 -07001000 int width = 0;
1001 int height = 0;
reed42943c82016-09-12 12:01:44 -07001002 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -07001003 &width, &height)) {
1004 break;
msarett7f691442015-09-22 11:56:16 -07001005 }
msarettd1227a72016-05-18 06:23:57 -07001006
1007 SkString basename = SkOSPath::Basename(path.c_str());
1008 SkIRect subset;
1009 const uint32_t subsetSize = sampleSize * minOutputSize;
1010 switch (currentSubsetType) {
1011 case kTopLeft_SubsetType:
1012 basename.append("_TopLeft");
1013 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
1014 break;
1015 case kTopRight_SubsetType:
1016 basename.append("_TopRight");
1017 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
1018 subsetSize);
1019 break;
1020 case kMiddle_SubsetType:
1021 basename.append("_Middle");
1022 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
1023 (height - subsetSize) / 2, subsetSize, subsetSize);
1024 break;
1025 case kBottomLeft_SubsetType:
1026 basename.append("_BottomLeft");
1027 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
1028 subsetSize);
1029 break;
1030 case kBottomRight_SubsetType:
1031 basename.append("_BottomRight");
1032 subset = SkIRect::MakeXYWH(width - subsetSize,
1033 height - subsetSize, subsetSize, subsetSize);
1034 break;
1035 default:
1036 SkASSERT(false);
1037 }
1038
1039 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
1040 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -07001041 }
msarettd1227a72016-05-18 06:23:57 -07001042 fCurrentSubsetType = 0;
1043 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -07001044 }
msarettd1227a72016-05-18 06:23:57 -07001045 fCurrentSampleSize = 0;
1046 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -07001047 }
msarettd1227a72016-05-18 06:23:57 -07001048 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -07001049 }
1050
halcanary96fcdcc2015-08-27 07:41:13 -07001051 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001052 }
mtklein92007582014-08-01 07:46:52 -07001053
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001054 void fillCurrentOptions(NanoJSONResultsWriter& log) const {
1055 log.appendString("source_type", fSourceType);
1056 log.appendString("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001057 if (0 == strcmp(fSourceType, "skp")) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001058 log.appendString("clip",
mtklein92007582014-08-01 07:46:52 -07001059 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1060 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001061 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001062 log.appendString("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001063 if (fCurrentUseMPD > 0) {
1064 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001065 log.appendString("multi_picture_draw",
1066 fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
robertphillips5b693772014-11-21 06:19:36 -08001067 }
mtklein92007582014-08-01 07:46:52 -07001068 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001069 }
1070
1071 void fillCurrentMetrics(NanoJSONResultsWriter& log) const {
mtklein051e56d2014-12-04 08:46:51 -08001072 if (0 == strcmp(fBenchType, "recording")) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001073 log.appendMetric("bytes", fSKPBytes);
1074 log.appendMetric("ops", fSKPOps);
mtklein051e56d2014-12-04 08:46:51 -08001075 }
mtklein92007582014-08-01 07:46:52 -07001076 }
1077
mtkleine714e752014-07-31 12:13:48 -07001078private:
msarettb23e6aa2015-06-09 13:56:10 -07001079 enum SubsetType {
1080 kTopLeft_SubsetType = 0,
1081 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001082 kMiddle_SubsetType = 2,
1083 kBottomLeft_SubsetType = 3,
1084 kBottomRight_SubsetType = 4,
1085 kTranslate_SubsetType = 5,
1086 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001087 kLast_SubsetType = kZoom_SubsetType,
1088 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001089 };
1090
mtkleine714e752014-07-31 12:13:48 -07001091 const BenchRegistry* fBenches;
1092 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001093 SkIRect fClip;
1094 SkTArray<SkScalar> fScales;
1095 SkTArray<SkString> fSKPs;
fmalita6519c212016-09-14 08:05:17 -07001096 SkTArray<SkString> fSVGs;
Hal Canary5dfefa22019-10-24 13:52:17 -04001097 SkTArray<SkString> fTextBlobTraces;
robertphillips5b693772014-11-21 06:19:36 -08001098 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001099 SkTArray<SkString> fImages;
msarett74deb982015-10-20 16:45:56 -07001100 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001101 SkScalar fZoomMax;
1102 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001103
Mike Klein88d90712018-01-27 17:30:04 +00001104 double fSKPBytes, fSKPOps;
mtklein051e56d2014-12-04 08:46:51 -08001105
mtkleinfd731ce2014-09-10 12:19:30 -07001106 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1107 const char* fBenchType; // How we bench it: micro, recording, playback, ...
Hal Canary2362ae72019-10-24 13:40:40 -04001108 int fCurrentRecording = 0;
1109 int fCurrentDeserialPicture = 0;
1110 int fCurrentScale = 0;
1111 int fCurrentSKP = 0;
1112 int fCurrentSVG = 0;
Hal Canary5dfefa22019-10-24 13:52:17 -04001113 int fCurrentTextBlobTrace = 0;
Hal Canary2362ae72019-10-24 13:40:40 -04001114 int fCurrentUseMPD = 0;
1115 int fCurrentCodec = 0;
1116 int fCurrentAndroidCodec = 0;
1117 int fCurrentBRDImage = 0;
1118 int fCurrentColorType = 0;
1119 int fCurrentAlphaType = 0;
1120 int fCurrentSubsetType = 0;
1121 int fCurrentSampleSize = 0;
1122 int fCurrentAnimSKP = 0;
mtkleine714e752014-07-31 12:13:48 -07001123};
1124
msarettc149f0e2016-01-04 11:35:43 -08001125// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1126// This prints something every once in a while so that it knows we're still working.
1127static void start_keepalive() {
Mike Klein03141d22017-10-30 11:57:15 -04001128 static std::thread* intentionallyLeaked = new std::thread([]{
1129 for (;;) {
1130 static const int kSec = 1200;
1131 #if defined(SK_BUILD_FOR_WIN)
1132 Sleep(kSec * 1000);
1133 #else
1134 sleep(kSec);
1135 #endif
1136 SkDebugf("\nBenchmarks still running...\n");
msarettc149f0e2016-01-04 11:35:43 -08001137 }
Mike Klein03141d22017-10-30 11:57:15 -04001138 });
1139 (void)intentionallyLeaked;
msarettc149f0e2016-01-04 11:35:43 -08001140}
1141
Mike Kleinbe28ee22017-02-06 12:46:20 -05001142int main(int argc, char** argv) {
Mike Klein88544fb2019-03-20 10:50:33 -05001143 CommandLineFlags::Parse(argc, argv);
Brian Osman53136aa2017-07-20 15:43:35 -04001144
Brian Osmanbc8150f2017-07-24 11:38:01 -04001145 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -04001146
Mike Kleinadacaef2017-02-06 09:26:14 -05001147#if defined(SK_BUILD_FOR_IOS)
1148 cd_Documents();
1149#endif
jcgregorio3b27ade2014-11-13 08:06:40 -08001150 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001151 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001152 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001153
Chris Dalton040238b2017-12-18 14:22:34 -07001154 SetCtxOptionsFromCommonFlags(&grContextOpts);
krajcevski69a55602014-08-13 10:46:31 -07001155
bsalomon6eb03cc2014-08-07 14:28:50 -07001156 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001157 FLAGS_samples = 1;
1158 FLAGS_gpuFrameLag = 0;
1159 }
1160
bsalomon6eb03cc2014-08-07 14:28:50 -07001161 if (!FLAGS_writePath.isEmpty()) {
1162 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1163 if (!sk_mkdir(FLAGS_writePath[0])) {
1164 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001165 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001166 }
1167 }
1168
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001169 std::unique_ptr<SkWStream> logStream(new SkNullWStream);
mtklein60317d0f2014-07-14 11:30:37 -07001170 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001171#if defined(SK_RELEASE)
Chris Dalton2e1381e2019-10-23 14:35:19 -06001172 // SkJSONWriter uses a 32k in-memory cache, so it only flushes occasionally and is well
1173 // equipped for a stream that re-opens, appends, and closes the file on every write.
1174 logStream.reset(new NanoFILEAppendAndCloseStream(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001175#else
1176 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1177 return 1;
1178#endif
mtklein60317d0f2014-07-14 11:30:37 -07001179 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001180 NanoJSONResultsWriter log(logStream.get(), SkJSONWriter::Mode::kPretty);
1181 log.beginObject(); // root
mtklein1915b622014-08-20 11:45:00 -07001182
1183 if (1 == FLAGS_properties.count() % 2) {
1184 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1185 return 1;
1186 }
1187 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001188 log.appendString(FLAGS_properties[i-1], FLAGS_properties[i]);
mtklein1915b622014-08-20 11:45:00 -07001189 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001190
1191 if (1 == FLAGS_key.count() % 2) {
1192 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1193 return 1;
1194 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001195 if (FLAGS_key.count()) {
1196 log.beginObject("key");
1197 for (int i = 1; i < FLAGS_key.count(); i += 2) {
1198 log.appendString(FLAGS_key[i - 1], FLAGS_key[i]);
1199 }
1200 log.endObject(); // key
mtklein94e51562014-08-19 12:41:53 -07001201 }
mtklein60317d0f2014-07-14 11:30:37 -07001202
mtkleinf3723212014-06-25 14:08:00 -07001203 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001204 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001205
cdaltone1b89582015-06-25 19:17:08 -07001206 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001207
bsalomon6eb03cc2014-08-07 14:28:50 -07001208 if (kAutoTuneLoops != FLAGS_loops) {
1209 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001210 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001211 SkDebugf("! -> high variance, ? -> moderate variance\n");
1212 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001213 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001214 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001215 } else {
mtkleind75c4662015-04-30 07:11:22 -07001216 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001217 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001218 }
1219
svaisanenc47635e2016-01-28 06:05:43 -08001220 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001221 create_configs(&configs);
1222
msarettc149f0e2016-01-04 11:35:43 -08001223 if (FLAGS_keepAlive) {
1224 start_keepalive();
1225 }
1226
Mike Klein03fa5d42019-03-22 11:39:09 -05001227 SetAnalyticAAFromCommonFlags();
liyuqian38911a72016-10-04 11:23:22 -07001228
Mike Reedc928fe22017-06-05 10:20:31 -04001229 if (FLAGS_forceRasterPipeline) {
1230 gSkForceRasterPipelineBlitter = true;
1231 }
Yuqian Li550148b2017-01-13 10:13:13 -05001232
mtkleine070c2b2014-10-14 08:40:43 -07001233 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001234 BenchmarkStream benchStream;
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001235 log.beginObject("results");
Jim Van Verth8a9a3712019-05-31 10:49:12 -04001236 AutoreleasePool pool;
mtklein92007582014-08-01 07:46:52 -07001237 while (Benchmark* b = benchStream.next()) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001238 std::unique_ptr<Benchmark> bench(b);
Mike Klein88544fb2019-03-20 10:50:33 -05001239 if (CommandLineFlags::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()) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001244 log.beginBench(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) {
1248 Target* target = is_enabled(b, configs[i]);
1249 if (!target) {
1250 continue;
1251 }
mtkleinf3723212014-06-25 14:08:00 -07001252
halcanary96fcdcc2015-08-27 07:41:13 -07001253 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001254 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001255 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001256
brianosman7a5ada82016-02-08 13:49:12 -08001257 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001258 SkDebugf("Running %s\t%s\n"
1259 , bench->getUniqueName()
1260 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001261 if (FLAGS_dryRun) {
1262 continue;
1263 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001264 }
1265
Mike Kleinee254232019-03-28 08:32:14 -05001266 if (FLAGS_purgeBetweenBenches) {
1267 SkGraphics::PurgeAllCaches();
1268 }
1269
Brian Osman5ad87aa2017-09-08 16:05:23 -04001270 TRACE_EVENT2("skia", "Benchmark", "name", TRACE_STR_COPY(bench->getUniqueName()),
1271 "config", TRACE_STR_COPY(config));
Brian Osmanc3cdef52017-08-31 14:09:22 -04001272
egdaniel3bf92062015-06-26 08:12:46 -07001273 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001274 bench->perCanvasPreDraw(canvas);
1275
cdaltone1b89582015-06-25 19:17:08 -07001276 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001277 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001278 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1279 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001280
Leon Scroggins IIIb30d1132017-10-02 09:48:15 -04001281 if (kFailedLoops == loops) {
1282 // Can't be timed. A warning note has already been printed.
1283 cleanup_run(target);
1284 continue;
1285 }
1286
Yuqian Li3528eb32017-09-21 13:23:49 -04001287 if (runs == 0 && FLAGS_ms < 1000) {
1288 // Run the first bench for 1000ms to warm up the nanobench if FLAGS_ms < 1000.
1289 // Otherwise, the first few benches' measurements will be inaccurate.
1290 auto stop = now_ms() + 1000;
1291 do {
1292 time(loops, bench.get(), target);
1293 } while (now_ms() < stop);
1294 }
1295
mtkleinbbba1682015-10-28 11:36:30 -07001296 if (FLAGS_ms) {
1297 samples.reset();
1298 auto stop = now_ms() + FLAGS_ms;
1299 do {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001300 samples.push_back(time(loops, bench.get(), target) / loops);
mtkleinbbba1682015-10-28 11:36:30 -07001301 } while (now_ms() < stop);
1302 } else {
cdaltone1b89582015-06-25 19:17:08 -07001303 samples.reset(FLAGS_samples);
1304 for (int s = 0; s < FLAGS_samples; s++) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001305 samples[s] = time(loops, bench.get(), target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001306 }
cdaltone1b89582015-06-25 19:17:08 -07001307 }
mtkleinf3723212014-06-25 14:08:00 -07001308
Mike Kleinfec9b902019-05-31 10:03:07 -05001309 // Scale each result to the benchmark's own units, time/unit.
1310 for (double& sample : samples) {
1311 sample *= (1.0 / bench->getUnits());
1312 }
1313
joshualitte45c81c2015-12-02 09:05:37 -08001314 SkTArray<SkString> keys;
1315 SkTArray<double> values;
1316 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1317 if (gpuStatsDump) {
1318 // TODO cache stats
1319 bench->getGpuStats(canvas, &keys, &values);
1320 }
joshualitte45c81c2015-12-02 09:05:37 -08001321
robertphillips5b693772014-11-21 06:19:36 -08001322 bench->perCanvasPostDraw(canvas);
1323
egdaniel3bf92062015-06-26 08:12:46 -07001324 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001325 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001326 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001327 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001328 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001329 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001330 }
1331
Mike Klein512a2442018-09-27 10:44:56 -04001332 // Building stats.plot often shows up in profiles,
1333 // so skip building it when we're not going to print it anyway.
1334 const bool want_plot = !FLAGS_quiet;
1335
1336 Stats stats(samples, want_plot);
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001337 log.beginObject(config);
1338
1339 log.beginObject("options");
1340 log.appendString("name", bench->getName());
1341 benchStream.fillCurrentOptions(log);
1342 target->fillOptions(log);
1343 log.endObject(); // options
1344
1345 // Metrics
1346 log.appendMetric("min_ms", stats.min);
1347 log.beginArray("samples");
1348 for (double sample : samples) {
1349 log.appendDoubleDigits(sample, 16);
1350 }
1351 log.endArray(); // samples
1352 benchStream.fillCurrentMetrics(log);
joshualitte45c81c2015-12-02 09:05:37 -08001353 if (gpuStatsDump) {
1354 // dump to json, only SKPBench currently returns valid keys / values
1355 SkASSERT(keys.count() == values.count());
1356 for (int i = 0; i < keys.count(); i++) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001357 log.appendMetric(keys[i].c_str(), values[i]);
joshualitte45c81c2015-12-02 09:05:37 -08001358 }
1359 }
joshualitte45c81c2015-12-02 09:05:37 -08001360
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001361 log.endObject(); // config
1362
mtkleine070c2b2014-10-14 08:40:43 -07001363 if (runs++ % FLAGS_flushEvery == 0) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001364 log.flush();
mtkleine070c2b2014-10-14 08:40:43 -07001365 }
mtklein60317d0f2014-07-14 11:30:37 -07001366
bsalomon6eb03cc2014-08-07 14:28:50 -07001367 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001368 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001369 config = ""; // Only print the config if we run the same bench on more than one.
1370 }
mtkleind75c4662015-04-30 07:11:22 -07001371 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1372 , sk_tools::getCurrResidentSetSizeMB()
1373 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001374 , bench->getUniqueName()
1375 , config);
mtkleinf3723212014-06-25 14:08:00 -07001376 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001377 const char* mark = " ";
Hal Canarye99e8da2019-01-14 16:34:50 -05001378 const double stddev_percent =
1379 sk_ieee_double_divide(100 * sqrt(stats.var), stats.mean);
mtklein66cfcff2015-12-04 06:35:30 -08001380 if (stddev_percent > 5) mark = "?";
1381 if (stddev_percent > 10) mark = "!";
1382
1383 SkDebugf("%10.2f %s\t%s\t%s\n",
1384 stats.median*1e3, mark, bench->getUniqueName(), config);
Mike Reedc665e5b2017-07-04 22:56:06 -04001385 } else if (FLAGS_csv) {
Hal Canarye99e8da2019-01-14 16:34:50 -05001386 const double stddev_percent =
1387 sk_ieee_double_divide(100 * sqrt(stats.var), stats.mean);
Mike Reedc665e5b2017-07-04 22:56:06 -04001388 SkDebugf("%g,%g,%g,%g,%g,%s,%s\n"
1389 , stats.min
1390 , stats.median
1391 , stats.mean
1392 , stats.max
1393 , stddev_percent
1394 , config
1395 , bench->getUniqueName()
1396 );
1397 } else {
1398 const char* format = "%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n";
Hal Canarye99e8da2019-01-14 16:34:50 -05001399 const double stddev_percent =
1400 sk_ieee_double_divide(100 * sqrt(stats.var), stats.mean);
Mike Reedc665e5b2017-07-04 22:56:06 -04001401 SkDebugf(format
mtkleind75c4662015-04-30 07:11:22 -07001402 , sk_tools::getCurrResidentSetSizeMB()
1403 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001404 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001405 , HUMANIZE(stats.min)
1406 , HUMANIZE(stats.median)
1407 , HUMANIZE(stats.mean)
1408 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001409 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001410 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001411 , config
mtklein96289052014-09-10 12:05:59 -07001412 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001413 );
1414 }
joshualitte45c81c2015-12-02 09:05:37 -08001415
joshualitte45c81c2015-12-02 09:05:37 -08001416 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -04001417 target->dumpStats();
bsalomon06cddec2014-10-24 10:40:50 -07001418 }
joshualitte45c81c2015-12-02 09:05:37 -08001419
cdalton2c56ba52015-06-26 13:32:53 -07001420 if (FLAGS_verbose) {
1421 SkDebugf("Samples: ");
1422 for (int i = 0; i < samples.count(); i++) {
1423 SkDebugf("%s ", HUMANIZE(samples[i]));
1424 }
1425 SkDebugf("%s\n", bench->getUniqueName());
1426 }
egdaniel3bf92062015-06-26 08:12:46 -07001427 cleanup_run(target);
Jim Van Verth8a9a3712019-05-31 10:49:12 -04001428 pool.drain();
mtkleinf3723212014-06-25 14:08:00 -07001429 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001430 if (!configs.empty()) {
1431 log.endBench();
1432 }
mtkleinf3723212014-06-25 14:08:00 -07001433 }
1434
Herb Derby5a523fe2017-01-26 16:48:28 -05001435 SkGraphics::PurgeAllCaches();
1436
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001437 log.beginBench("memory_usage", 0, 0);
1438 log.beginObject("meta"); // config
1439 log.appendS32("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1440 log.endObject(); // config
1441 log.endBench();
1442
1443 log.endObject(); // results
1444 log.endObject(); // root
1445 log.flush();
mtkleine1091452014-12-04 10:47:02 -08001446
mtkleinf3723212014-06-25 14:08:00 -07001447 return 0;
1448}