blob: b8743c0405381ba7c513661d643b3bc8f94ddd2a [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "bench/CodecBench.h"
15#include "bench/CodecBenchPriv.h"
16#include "bench/GMBench.h"
17#include "bench/RecordingBench.h"
18#include "bench/ResultsWriter.h"
19#include "bench/SKPAnimationBench.h"
20#include "bench/SKPBench.h"
Hal Canary5dfefa22019-10-24 13:52:17 -040021#include "bench/SkGlyphCacheBench.h"
Brian Osman68870aa2020-07-08 14:12:43 -040022#include "bench/SkSLBench.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/codec/SkAndroidCodec.h"
24#include "include/codec/SkCodec.h"
25#include "include/core/SkCanvas.h"
26#include "include/core/SkData.h"
27#include "include/core/SkGraphics.h"
28#include "include/core/SkPictureRecorder.h"
29#include "include/core/SkString.h"
30#include "include/core/SkSurface.h"
Ben Wagneracf98df2019-07-12 12:51:44 -040031#include "include/core/SkTime.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/core/SkAutoMalloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/core/SkColorSpacePriv.h"
Ben Wagnerab6eefe2019-05-20 11:02:49 -040034#include "src/core/SkLeanWindows.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/core/SkOSFile.h"
36#include "src/core/SkTaskGroup.h"
37#include "src/core/SkTraceEvent.h"
John Stiles2ef627a2021-03-23 10:25:02 -040038#include "src/gpu/GrShaderUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/utils/SkJSONWriter.h"
40#include "src/utils/SkOSPath.h"
Jim Van Verth8a9a3712019-05-31 10:49:12 -040041#include "tools/AutoreleasePool.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "tools/CrashHandler.h"
43#include "tools/ProcStats.h"
44#include "tools/Stats.h"
45#include "tools/flags/CommonFlags.h"
46#include "tools/flags/CommonFlagsConfig.h"
47#include "tools/ios_utils.h"
48#include "tools/trace/EventTracingPriv.h"
49#include "tools/trace/SkDebugfTracer.h"
mtkleinf3723212014-06-25 14:08:00 -070050
Hal Canary0f666812018-03-22 15:21:12 -040051#ifdef SK_XML
Florin Malitab3418102020-10-15 18:10:29 -040052#include "modules/svg/include/SkSVGDOM.h"
Hal Canary0f666812018-03-22 15:21:12 -040053#endif // SK_XML
54
Leon Scroggins III87caae62020-05-04 10:02:45 -040055#ifdef SK_ENABLE_ANDROID_UTILS
56#include "bench/BitmapRegionDecoderBench.h"
57#include "client_utils/android/BitmapRegionDecoder.h"
58#endif
59
Adlai Holler684838f2020-05-12 10:41:04 -040060#include <cinttypes>
bungeman60e0fee2015-08-26 05:15:46 -070061#include <stdlib.h>
John Stilesfbd050b2020-08-03 13:21:46 -040062#include <memory>
Mike Klein03141d22017-10-30 11:57:15 -040063#include <thread>
bungeman60e0fee2015-08-26 05:15:46 -070064
Mike Reedc928fe22017-06-05 10:20:31 -040065extern bool gSkForceRasterPipelineBlitter;
Mike Klein2a57e792020-01-14 12:47:52 -060066extern bool gUseSkVMBlitter;
Mike Klein813e8cc2020-08-05 09:33:38 -050067extern bool gSkVMAllowJIT;
Mike Kleinea4459d2020-01-16 13:05:04 -060068extern bool gSkVMJITViaDylib;
Mike Reedc928fe22017-06-05 10:20:31 -040069
Mike Klein8f11d4d2018-01-24 12:42:55 -050070#ifndef SK_BUILD_FOR_WIN
scroggo38ce0a72016-01-07 07:28:47 -080071 #include <unistd.h>
Brian Salomon032aaae2018-03-23 16:10:36 -040072
scroggo38ce0a72016-01-07 07:28:47 -080073#endif
74
Robert Phillips00f78de2020-07-01 16:09:43 -040075#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050076#include "src/gpu/GrCaps.h"
Adlai Hollera0693042020-10-14 11:23:11 -040077#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050078#include "src/gpu/SkGr.h"
79#include "src/gpu/gl/GrGLDefines.h"
80#include "src/gpu/gl/GrGLGpu.h"
81#include "src/gpu/gl/GrGLUtil.h"
82#include "tools/gpu/GrContextFactory.h"
Brian Salomon032aaae2018-03-23 16:10:36 -040083
Brian Osmanc7ad40f2018-05-31 14:27:17 -040084using sk_gpu_test::ContextInfo;
85using sk_gpu_test::GrContextFactory;
86using sk_gpu_test::TestContext;
Brian Salomon032aaae2018-03-23 16:10:36 -040087
Brian Osmanc7ad40f2018-05-31 14:27:17 -040088GrContextOptions grContextOpts;
bsalomon682c2692015-05-22 14:01:46 -070089
reed53249782014-10-10 09:09:52 -070090static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070091
bsalomon6eb03cc2014-08-07 14:28:50 -070092static SkString loops_help_txt() {
93 SkString help;
94 help.printf("Number of times to run each bench. Set this to %d to auto-"
95 "tune for each bench. Timings are only reported when auto-tuning.",
96 kAutoTuneLoops);
97 return help;
98}
99
cdaltone1b89582015-06-25 19:17:08 -0700100static SkString to_string(int n) {
101 SkString str;
102 str.appendS32(n);
103 return str;
104}
105
Mike Kleinac8f4432019-07-01 10:15:32 -0500106static DEFINE_int(loops, kAutoTuneLoops, loops_help_txt().c_str());
bsalomon6eb03cc2014-08-07 14:28:50 -0700107
Mike Klein5b3f3432019-03-21 11:42:21 -0500108static DEFINE_int(samples, 10, "Number of samples to measure for each bench.");
109static DEFINE_int(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
110static DEFINE_int(overheadLoops, 100000, "Loops to estimate timer overhead.");
Mike Klein84836b72019-03-21 11:31:36 -0500111static DEFINE_double(overheadGoal, 0.0001,
mtkleinf3723212014-06-25 14:08:00 -0700112 "Loop until timer overhead is at most this fraction of our measurments.");
Mike Klein84836b72019-03-21 11:31:36 -0500113static DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
Mike Klein5b3f3432019-03-21 11:42:21 -0500114static DEFINE_int(gpuFrameLag, 5,
Mike Klein84836b72019-03-21 11:31:36 -0500115 "If unknown, estimated maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -0700116
Mike Klein84836b72019-03-21 11:31:36 -0500117static DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
Mike Klein5b3f3432019-03-21 11:42:21 -0500118static DEFINE_int(maxCalibrationAttempts, 3,
mtklein55b0ffc2014-07-17 08:38:23 -0700119 "Try up to this many times to guess loops for a bench, or skip the bench.");
Mike Klein5b3f3432019-03-21 11:42:21 -0500120static DEFINE_int(maxLoops, 1000000, "Never run a bench more times than this.");
Mike Klein84836b72019-03-21 11:31:36 -0500121static DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
122static DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
123static DEFINE_string(zoom, "1.0,0",
124 "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
125 "function that ping-pongs between 1.0 and zoomMax.");
126static DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
Mike Klein84836b72019-03-21 11:31:36 -0500127static DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
Mike Klein5b3f3432019-03-21 11:42:21 -0500128static DEFINE_int(flushEvery, 10, "Flush --outResultsFile every Nth run.");
Mike Klein84836b72019-03-21 11:31:36 -0500129static DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
130static DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
131static DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
132static DEFINE_bool(csv, false, "Print status in CSV format");
133static DEFINE_string(sourceType, "",
mtklein65dfd2f2016-02-03 10:40:54 -0800134 "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
Mike Klein84836b72019-03-21 11:31:36 -0500135static DEFINE_string(benchType, "",
136 "Apply usual --match rules to bench type: micro, recording, "
137 "piping, playback, skcodec, etc.");
mtklein65dfd2f2016-02-03 10:40:54 -0800138
Mike Klein84836b72019-03-21 11:31:36 -0500139static DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
Mike Klein813e8cc2020-08-05 09:33:38 -0500140static DEFINE_bool(skvm, false, "sets gUseSkVMBlitter");
Mike Kleinf2b3f912021-03-23 14:32:45 -0500141static DEFINE_bool(jit, true, "JIT SkVM?");
142static DEFINE_bool(dylib, false, "JIT via dylib (much slower compile but easier to debug/profile)");
Mike Reedc928fe22017-06-05 10:20:31 -0400143
Mike Klein19fb3972019-03-21 13:08:08 -0500144static DEFINE_bool2(pre_log, p, false,
145 "Log before running each test. May be incomprehensible when threading");
146
Leon Scroggins III1ff07062020-07-27 14:52:19 -0400147static DEFINE_bool(cpu, true, "Run CPU-bound work?");
148static DEFINE_bool(gpu, true, "Run GPU-bound work?");
Mike Klein629f5fc2019-03-22 14:55:19 -0500149static DEFINE_bool(dryRun, false,
150 "just print the tests that would be run, without actually running them.");
151static DEFINE_string(images, "",
152 "List of images and/or directories to decode. A directory with no images"
153 " is treated as a fatal error.");
154static DEFINE_bool(simpleCodec, false,
155 "Runs of a subset of the codec tests, always N32, Premul or Opaque");
156
Mike Kleinc6142d82019-03-25 10:54:59 -0500157static DEFINE_string2(match, m, nullptr,
158 "[~][^]substring[$] [...] of name to run.\n"
159 "Multiple matches may be separated by spaces.\n"
160 "~ causes a matching name to always be skipped\n"
161 "^ requires the start of the name to match\n"
162 "$ requires the end of the name to match\n"
163 "^ and $ requires an exact match\n"
164 "If a name does not match any list entry,\n"
165 "it is skipped unless some list entry starts with ~");
166
167static DEFINE_bool2(quiet, q, false, "if true, don't print status updates.");
168static DEFINE_bool2(verbose, v, false, "enable verbose output from the test driver.");
169
170
171static DEFINE_string(skps, "skps", "Directory to read skps from.");
172static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
Hal Canary5dfefa22019-10-24 13:52:17 -0400173static DEFINE_string(texttraces, "", "Directory to read TextBlobTrace files from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500174
175static DEFINE_int_2(threads, j, -1,
176 "Run threadsafe tests on a threadpool with this many extra threads, "
177 "defaulting to one extra thread per core.");
178
179static DEFINE_string2(writePath, w, "", "If set, write bitmaps here as .pngs.");
180
181static DEFINE_string(key, "",
182 "Space-separated key/value pairs to add to JSON identifying this builder.");
183static DEFINE_string(properties, "",
184 "Space-separated key/value pairs to add to JSON identifying this run.");
185
Mike Kleinee254232019-03-28 08:32:14 -0500186static DEFINE_bool(purgeBetweenBenches, false,
187 "Call SkGraphics::PurgeAllCaches() between each benchmark?");
188
mtkleinbbba1682015-10-28 11:36:30 -0700189static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
190
mtkleinf3723212014-06-25 14:08:00 -0700191static SkString humanize(double ms) {
Adlai Holler684838f2020-05-12 10:41:04 -0400192 if (FLAGS_verbose) return SkStringPrintf("%" PRIu64, (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800193 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700194}
mtklein55b0ffc2014-07-17 08:38:23 -0700195#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700196
tomhudsond968a6f2015-03-26 11:28:06 -0700197bool Target::init(SkImageInfo info, Benchmark* bench) {
198 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700199 this->surface = SkSurface::MakeRaster(info);
200 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700201 return false;
202 }
203 }
204 return true;
205}
206bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700207 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700208 if (!canvas) {
209 return false;
210 }
Mike Reed12e946b2017-04-17 10:53:29 -0400211 bmp->allocPixels(canvas->imageInfo());
212 if (!canvas->readPixels(*bmp, 0, 0)) {
tomhudsond968a6f2015-03-26 11:28:06 -0700213 SkDebugf("Can't read canvas pixels.\n");
214 return false;
215 }
216 return true;
217}
218
tomhudsond968a6f2015-03-26 11:28:06 -0700219struct GPUTarget : public Target {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400220 explicit GPUTarget(const Config& c) : Target(c) {}
221 ContextInfo contextInfo;
222 std::unique_ptr<GrContextFactory> factory;
tomhudsond968a6f2015-03-26 11:28:06 -0700223
Greg Daniel5ed3c112020-06-18 15:59:17 -0400224 ~GPUTarget() override {
225 // For Vulkan we need to release all our refs to the GrContext before destroy the vulkan
226 // context which happens at the end of this destructor. Thus we need to release the surface
227 // here which holds a ref to the GrContext.
228 surface.reset();
229 }
230
tomhudsond968a6f2015-03-26 11:28:06 -0700231 void setup() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400232 this->contextInfo.testContext()->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700233 // Make sure we're done with whatever came before.
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400234 this->contextInfo.testContext()->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700235 }
236 void endTiming() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400237 if (this->contextInfo.testContext()) {
Robert Phillips00f78de2020-07-01 16:09:43 -0400238 this->contextInfo.testContext()->flushAndWaitOnSync(contextInfo.directContext());
tomhudsond968a6f2015-03-26 11:28:06 -0700239 }
240 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400241 void fence() override { this->contextInfo.testContext()->finish(); }
mtkleind75c4662015-04-30 07:11:22 -0700242
cdaltond416a5b2015-06-23 13:23:44 -0700243 bool needsFrameTiming(int* maxFrameLag) const override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400244 if (!this->contextInfo.testContext()->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700245 // Frame lag is unknown.
246 *maxFrameLag = FLAGS_gpuFrameLag;
247 }
248 return true;
249 }
tomhudsond968a6f2015-03-26 11:28:06 -0700250 bool init(SkImageInfo info, Benchmark* bench) override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400251 GrContextOptions options = grContextOpts;
Chris Daltonfd708652021-03-17 21:10:17 -0600252 options.fAlwaysAntialias = config.useDMSAA;
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400253 bench->modifyGrContextOptions(&options);
John Stilesfbd050b2020-08-03 13:21:46 -0400254 this->factory = std::make_unique<GrContextFactory>(options);
Chris Dalton180b4a12021-03-16 20:49:15 -0600255 SkSurfaceProps props(this->config.surfaceFlags, kRGB_H_SkPixelGeometry);
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400256 this->surface = SkSurface::MakeRenderTarget(
257 this->factory->get(this->config.ctxType, this->config.ctxOverrides),
258 SkBudgeted::kNo, info, this->config.samples, &props);
259 this->contextInfo =
260 this->factory->getContextInfo(this->config.ctxType, this->config.ctxOverrides);
John Stilesa008b0f2020-08-16 08:48:02 -0400261 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700262 return false;
263 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400264 if (!this->contextInfo.testContext()->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700265 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800266 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700267 }
tomhudsond968a6f2015-03-26 11:28:06 -0700268 return true;
269 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500270 void fillOptions(NanoJSONResultsWriter& log) override {
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400271#ifdef SK_GL
tomhudsond968a6f2015-03-26 11:28:06 -0700272 const GrGLubyte* version;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400273 if (this->contextInfo.backend() == GrBackendApi::kOpenGL) {
Brian Salomon032aaae2018-03-23 16:10:36 -0400274 const GrGLInterface* gl =
Robert Phillips00f78de2020-07-01 16:09:43 -0400275 static_cast<GrGLGpu*>(this->contextInfo.directContext()->priv().getGpu())
Brian Salomon032aaae2018-03-23 16:10:36 -0400276 ->glInterface();
bsalomonc8699322016-05-11 11:55:36 -0700277 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500278 log.appendString("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700279
bsalomonc8699322016-05-11 11:55:36 -0700280 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500281 log.appendString("GL_RENDERER", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700282
bsalomonc8699322016-05-11 11:55:36 -0700283 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VENDOR));
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500284 log.appendString("GL_VENDOR", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700285
bsalomonc8699322016-05-11 11:55:36 -0700286 GR_GL_CALL_RET(gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500287 log.appendString("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
bsalomonc8699322016-05-11 11:55:36 -0700288 }
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400289#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700290 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400291
292 void dumpStats() override {
Robert Phillips00f78de2020-07-01 16:09:43 -0400293 auto context = this->contextInfo.directContext();
294
295 context->priv().printCacheStats();
296 context->priv().printGpuStats();
297 context->priv().printContextStats();
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400298 }
tomhudsond968a6f2015-03-26 11:28:06 -0700299};
mtkleind75c4662015-04-30 07:11:22 -0700300
tomhudson75a0ebb2015-03-27 12:11:44 -0700301static double time(int loops, Benchmark* bench, Target* target) {
302 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700303 if (canvas) {
304 canvas->clear(SK_ColorWHITE);
305 }
joshualitt8a6697a2015-09-30 12:11:07 -0700306 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700307 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700308 canvas = target->beginTiming(canvas);
309 bench->draw(loops, canvas);
tomhudson75a0ebb2015-03-27 12:11:44 -0700310 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700311 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700312 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700313 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700314}
315
mtkleinf3723212014-06-25 14:08:00 -0700316static double estimate_timer_overhead() {
317 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700318 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700319 double start = now_ms();
320 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700321 }
322 return overhead / FLAGS_overheadLoops;
323}
324
reed53249782014-10-10 09:09:52 -0700325static int detect_forever_loops(int loops) {
326 // look for a magic run-forever value
327 if (loops < 0) {
328 loops = SK_MaxS32;
329 }
330 return loops;
331}
332
mtklein55b0ffc2014-07-17 08:38:23 -0700333static int clamp_loops(int loops) {
334 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800335 SkDebugf("ERROR: clamping loops from %d to 1. "
336 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700337 return 1;
338 }
339 if (loops > FLAGS_maxLoops) {
340 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
341 return FLAGS_maxLoops;
342 }
343 return loops;
344}
345
tomhudsond968a6f2015-03-26 11:28:06 -0700346static bool write_canvas_png(Target* target, const SkString& filename) {
347
bsalomon6eb03cc2014-08-07 14:28:50 -0700348 if (filename.isEmpty()) {
349 return false;
350 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700351 if (target->getCanvas() &&
352 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700353 return false;
354 }
tomhudsond968a6f2015-03-26 11:28:06 -0700355
bsalomon6eb03cc2014-08-07 14:28:50 -0700356 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700357
358 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700359 return false;
360 }
tomhudsond968a6f2015-03-26 11:28:06 -0700361
bsalomon6eb03cc2014-08-07 14:28:50 -0700362 SkString dir = SkOSPath::Dirname(filename.c_str());
363 if (!sk_mkdir(dir.c_str())) {
364 SkDebugf("Can't make dir %s.\n", dir.c_str());
365 return false;
366 }
367 SkFILEWStream stream(filename.c_str());
368 if (!stream.isValid()) {
369 SkDebugf("Can't write %s.\n", filename.c_str());
370 return false;
371 }
Hal Canarydb683012016-11-23 08:55:18 -0700372 if (!SkEncodeImage(&stream, bmp, SkEncodedImageFormat::kPNG, 100)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700373 SkDebugf("Can't encode a PNG.\n");
374 return false;
375 }
376 return true;
377}
378
379static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700380static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700381 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700382 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700383 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700384 int loops = bench->calculateLoops(FLAGS_loops);
385 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700386 while (bench_plus_overhead < overhead) {
387 if (round++ == FLAGS_maxCalibrationAttempts) {
388 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700389 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700390 return kFailedLoops;
391 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700392 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700393 }
mtklein2069e222014-08-04 13:57:39 -0700394 }
mtkleinf3723212014-06-25 14:08:00 -0700395
mtkleinbb6a0282014-07-01 08:43:42 -0700396 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700397 // We'll pick N to make timer overhead negligible:
398 //
mtkleinbb6a0282014-07-01 08:43:42 -0700399 // overhead
400 // ------------------------- < FLAGS_overheadGoal
401 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700402 //
Hal Canary55325b72017-01-03 10:36:17 -0500403 // where bench_plus_overhead ~=~ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700404 //
405 // Doing some math, we get:
406 //
mtkleinbb6a0282014-07-01 08:43:42 -0700407 // (overhead / FLAGS_overheadGoal) - overhead
408 // ------------------------------------------ < N
409 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700410 //
411 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700412 if (kAutoTuneLoops == loops) {
413 const double numer = overhead / FLAGS_overheadGoal - overhead;
414 const double denom = bench_plus_overhead - overhead;
415 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700416 loops = clamp_loops(loops);
417 } else {
418 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700419 }
mtkleinbb6a0282014-07-01 08:43:42 -0700420
mtkleinbb6a0282014-07-01 08:43:42 -0700421 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700422}
423
cdaltone1b89582015-06-25 19:17:08 -0700424static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700425 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700426 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700427 if (kAutoTuneLoops == loops) {
428 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700429 double elapsed = 0;
430 do {
mtklein527930f2014-11-06 08:04:34 -0800431 if (1<<30 == loops) {
432 // We're about to wrap. Something's wrong with the bench.
433 loops = 0;
434 break;
435 }
mtkleina189ccd2014-07-14 12:28:47 -0700436 loops *= 2;
437 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700438 // _this_ round, not still timing last round.
439 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700440 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700441 }
442 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700443
mtkleina189ccd2014-07-14 12:28:47 -0700444 // We've overshot at least a little. Scale back linearly.
445 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700446 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700447
tomhudsond968a6f2015-03-26 11:28:06 -0700448 // Make sure we're not still timing our calibration.
449 target->fence();
reed53249782014-10-10 09:09:52 -0700450 } else {
451 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700452 }
mtkleinbb6a0282014-07-01 08:43:42 -0700453 // Pretty much the same deal as the calibration: do some warmup to make
454 // sure we're timing steady-state pipelined frames.
Brian Osman01776982017-10-10 15:41:03 -0400455 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700456 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700457 }
mtkleinbb6a0282014-07-01 08:43:42 -0700458
mtkleinbb6a0282014-07-01 08:43:42 -0700459 return loops;
460}
mtkleinbb6a0282014-07-01 08:43:42 -0700461
Brian Salomon6405e712017-03-20 08:54:16 -0400462#define kBogusContextType GrContextFactory::kGL_ContextType
csmartdaltone812d492017-02-21 12:36:05 -0700463#define kBogusContextOverrides GrContextFactory::ContextOverrides::kNone
bsalomonc2553372014-07-22 13:09:05 -0700464
svaisanenc47635e2016-01-28 06:05:43 -0800465static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
svaisanenc47635e2016-01-28 06:05:43 -0800466 if (const auto* gpuConfig = config->asConfigGpu()) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400467 if (!FLAGS_gpu) {
468 SkDebugf("Skipping config '%s' as requested.\n", config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800469 return;
Ben Wagner32fa5102017-08-10 21:25:55 -0400470 }
svaisanenc47635e2016-01-28 06:05:43 -0800471
svaisanenc47635e2016-01-28 06:05:43 -0800472 const auto ctxType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700473 const auto ctxOverrides = gpuConfig->getContextOverrides();
svaisanenc47635e2016-01-28 06:05:43 -0800474 const auto sampleCount = gpuConfig->getSamples();
Chris Daltonfd708652021-03-17 21:10:17 -0600475 const auto useDMSAA = gpuConfig->getUseDMSAA();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400476 const auto colorType = gpuConfig->getColorType();
477 auto colorSpace = gpuConfig->getColorSpace();
Brian Salomonf865b052018-03-09 09:01:53 -0500478 if (gpuConfig->getSurfType() != SkCommandLineConfigGpu::SurfType::kDefault) {
479 SkDebugf("This tool only supports the default surface type.");
480 return;
481 }
svaisanenc47635e2016-01-28 06:05:43 -0800482
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400483 GrContextFactory factory(grContextOpts);
Robert Phillipsf0288102020-07-06 13:45:34 -0400484 if (const auto ctx = factory.get(ctxType, ctxOverrides)) {
Greg Daniel6fa62e22019-08-07 15:52:37 -0400485 GrBackendFormat format = ctx->defaultBackendFormat(colorType, GrRenderable::kYes);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500486 int supportedSampleCount =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400487 ctx->priv().caps()->getRenderTargetSampleCount(sampleCount, format);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400488 if (sampleCount != supportedSampleCount) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400489 SkDebugf("Configuration '%s' sample count %d is not a supported sample count.\n",
490 config->getTag().c_str(), sampleCount);
svaisanenc47635e2016-01-28 06:05:43 -0800491 return;
492 }
493 } else {
Ben Wagner32fa5102017-08-10 21:25:55 -0400494 SkDebugf("No context was available matching config '%s'.\n",
495 config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800496 return;
497 }
498
499 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800500 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800501 Benchmark::kGPU_Backend,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400502 colorType,
svaisanenc47635e2016-01-28 06:05:43 -0800503 kPremul_SkAlphaType,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400504 sk_ref_sp(colorSpace),
svaisanenc47635e2016-01-28 06:05:43 -0800505 sampleCount,
Chris Daltonfd708652021-03-17 21:10:17 -0600506 useDMSAA,
svaisanenc47635e2016-01-28 06:05:43 -0800507 ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700508 ctxOverrides,
Chris Dalton180b4a12021-03-16 20:49:15 -0600509 gpuConfig->getSurfaceFlags()
kkinnunend4e1c352016-03-01 23:41:26 -0800510 };
svaisanenc47635e2016-01-28 06:05:43 -0800511
512 configs->push_back(target);
513 return;
514 }
svaisanenc47635e2016-01-28 06:05:43 -0800515
brianosmanb109b8c2016-06-16 13:03:24 -0700516 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
517 if (config->getTag().equals(#name)) { \
Ben Wagner32fa5102017-08-10 21:25:55 -0400518 if (!FLAGS_cpu) { \
519 SkDebugf("Skipping config '%s' as requested.\n", \
520 config->getTag().c_str()); \
521 return; \
522 } \
brianosmanb109b8c2016-06-16 13:03:24 -0700523 Config config = { \
524 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
Chris Daltonfd708652021-03-17 21:10:17 -0600525 0, false, kBogusContextType, kBogusContextOverrides, 0 \
brianosmanb109b8c2016-06-16 13:03:24 -0700526 }; \
527 configs->push_back(config); \
528 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700529 }
mtkleine714e752014-07-31 12:13:48 -0700530
Ben Wagner32fa5102017-08-10 21:25:55 -0400531 CPU_CONFIG(nonrendering, kNonRendering_Backend,
532 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800533
Mike Klein0e4041f2018-06-19 16:00:40 -0400534 CPU_CONFIG(a8, kRaster_Backend, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr)
535 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType, nullptr)
536 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
537
538 // 'narrow' has a gamut narrower than sRGB, and different transfer function.
Brian Osman82ebe042019-01-04 17:03:00 -0500539 auto narrow = SkColorSpace::MakeRGB(SkNamedTransferFn::k2Dot2, gNarrow_toXYZD50),
Mike Klein0e4041f2018-06-19 16:00:40 -0400540 srgb = SkColorSpace::MakeSRGB(),
541 srgbLinear = SkColorSpace::MakeSRGBLinear();
542
543 CPU_CONFIG( f16, kRaster_Backend, kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbLinear)
544 CPU_CONFIG( srgb, kRaster_Backend, kRGBA_8888_SkColorType, kPremul_SkAlphaType, srgb )
545 CPU_CONFIG( esrgb, kRaster_Backend, kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgb )
546 CPU_CONFIG( narrow, kRaster_Backend, kRGBA_8888_SkColorType, kPremul_SkAlphaType, narrow )
547 CPU_CONFIG(enarrow, kRaster_Backend, kRGBA_F16_SkColorType, kPremul_SkAlphaType, narrow )
mtkleinbb6a0282014-07-01 08:43:42 -0700548
svaisanenc47635e2016-01-28 06:05:43 -0800549 #undef CPU_CONFIG
Ben Wagner32fa5102017-08-10 21:25:55 -0400550
551 SkDebugf("Unknown config '%s'.\n", config->getTag().c_str());
mtkleinf3723212014-06-25 14:08:00 -0700552}
553
svaisanenc47635e2016-01-28 06:05:43 -0800554// Append all configs that are enabled and supported.
555void create_configs(SkTArray<Config>* configs) {
556 SkCommandLineConfigArray array;
557 ParseConfigs(FLAGS_config, &array);
558 for (int i = 0; i < array.count(); ++i) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400559 create_config(array[i].get(), configs);
svaisanenc47635e2016-01-28 06:05:43 -0800560 }
Ben Wagner32fa5102017-08-10 21:25:55 -0400561
562 // If no just default configs were requested, then we're okay.
563 if (array.count() == 0 || FLAGS_config.count() == 0 ||
Ben Wagner32fa5102017-08-10 21:25:55 -0400564 // Otherwise, make sure that all specified configs have been created.
565 array.count() == configs->count()) {
566 return;
567 }
Ben Wagner32fa5102017-08-10 21:25:55 -0400568 exit(1);
svaisanenc47635e2016-01-28 06:05:43 -0800569}
570
brianosman9f1f6e22016-09-15 08:33:02 -0700571// disable warning : switch statement contains default but no 'case' labels
572#if defined _WIN32
573#pragma warning ( push )
574#pragma warning ( disable : 4065 )
575#endif
576
halcanary96fcdcc2015-08-27 07:41:13 -0700577// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700578static Target* is_enabled(Benchmark* bench, const Config& config) {
579 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700580 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700581 }
582
reede5ea5002014-09-03 11:54:58 -0700583 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700584 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700585
halcanary96fcdcc2015-08-27 07:41:13 -0700586 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700587
tomhudsond968a6f2015-03-26 11:28:06 -0700588 switch (config.backend) {
tomhudsond968a6f2015-03-26 11:28:06 -0700589 case Benchmark::kGPU_Backend:
590 target = new GPUTarget(config);
591 break;
tomhudsond968a6f2015-03-26 11:28:06 -0700592 default:
593 target = new Target(config);
594 break;
595 }
bsalomonc2553372014-07-22 13:09:05 -0700596
tomhudsond968a6f2015-03-26 11:28:06 -0700597 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700598 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700599 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700600 }
601 return target;
602}
603
brianosman9f1f6e22016-09-15 08:33:02 -0700604#if defined _WIN32
605#pragma warning ( pop )
606#endif
607
Leon Scroggins III87caae62020-05-04 10:02:45 -0400608#ifdef SK_ENABLE_ANDROID_UTILS
reed42943c82016-09-12 12:01:44 -0700609static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700610 uint32_t minOutputSize, int* width, int* height) {
Leon Scroggins III87caae62020-05-04 10:02:45 -0400611 auto brd = android::skia::BitmapRegionDecoder::Make(encoded);
John Stilesa008b0f2020-08-16 08:48:02 -0400612 if (nullptr == brd) {
msarett7f691442015-09-22 11:56:16 -0700613 // This is indicates that subset decoding is not supported for a particular image format.
614 return false;
615 }
616
msarett7f691442015-09-22 11:56:16 -0700617 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
618 (uint32_t) brd->height()) {
619 // This indicates that the image is not large enough to decode a
620 // minOutputSize x minOutputSize subset at the given sampleSize.
621 return false;
622 }
623
624 // Set the image width and height. The calling code will use this to choose subsets to decode.
625 *width = brd->width();
626 *height = brd->height();
627 return true;
628}
Leon Scroggins III87caae62020-05-04 10:02:45 -0400629#endif
msarett7f691442015-09-22 11:56:16 -0700630
egdaniel3bf92062015-06-26 08:12:46 -0700631static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700632 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700633}
634
Mike Klein88544fb2019-03-20 10:50:33 -0500635static void collect_files(const CommandLineFlags::StringArray& paths,
636 const char* ext,
637 SkTArray<SkString>* list) {
fmalita6519c212016-09-14 08:05:17 -0700638 for (int i = 0; i < paths.count(); ++i) {
639 if (SkStrEndsWith(paths[i], ext)) {
640 list->push_back(SkString(paths[i]));
641 } else {
642 SkOSFile::Iter it(paths[i], ext);
643 SkString path;
644 while (it.next(&path)) {
645 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
646 }
647 }
648 }
649}
650
mtkleine714e752014-07-31 12:13:48 -0700651class BenchmarkStream {
652public:
mtklein92007582014-08-01 07:46:52 -0700653 BenchmarkStream() : fBenches(BenchRegistry::Head())
Hal Canary2362ae72019-10-24 13:40:40 -0400654 , fGMs(skiagm::GMRegistry::Head()) {
fmalita6519c212016-09-14 08:05:17 -0700655 collect_files(FLAGS_skps, ".skp", &fSKPs);
656 collect_files(FLAGS_svgs, ".svg", &fSVGs);
Hal Canary5dfefa22019-10-24 13:52:17 -0400657 collect_files(FLAGS_texttraces, ".trace", &fTextBlobTraces);
mtkleine714e752014-07-31 12:13:48 -0700658
mtklein92007582014-08-01 07:46:52 -0700659 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
660 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
661 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
662 exit(1);
663 }
664
665 for (int i = 0; i < FLAGS_scales.count(); i++) {
666 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
667 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
668 exit(1);
669 }
670 }
robertphillips5b693772014-11-21 06:19:36 -0800671
cdalton63a82852015-06-29 14:06:10 -0700672 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
673 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700674 exit(1);
675 }
676
msarett95f192d2015-02-13 09:05:41 -0800677 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700678 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800679 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800680 }
mtklein95553d92015-03-12 08:24:21 -0700681
msarett95f192d2015-02-13 09:05:41 -0800682 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700683 fColorTypes.push_back(kN32_SkColorType);
684 if (!FLAGS_simpleCodec) {
685 fColorTypes.push_back(kRGB_565_SkColorType);
686 fColorTypes.push_back(kAlpha_8_SkColorType);
msarett67cb6662016-06-21 08:49:26 -0700687 fColorTypes.push_back(kGray_8_SkColorType);
688 }
mtklein92007582014-08-01 07:46:52 -0700689 }
690
reedca2622b2016-03-18 07:25:55 -0700691 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700692 // Not strictly necessary, as it will be checked again later,
693 // but helps to avoid a lot of pointless work if we're going to skip it.
Mike Klein88544fb2019-03-20 10:50:33 -0500694 if (CommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700695 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700696 }
697
bungemanf93d7112016-09-16 06:24:20 -0700698 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
699 if (!stream) {
mtkleinfd731ce2014-09-10 12:19:30 -0700700 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700701 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700702 }
703
reedca2622b2016-03-18 07:25:55 -0700704 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700705 }
706
fmalita6519c212016-09-14 08:05:17 -0700707 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
Mike Klein8ab65bf2020-12-04 09:20:32 -0600708 if (CommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
709 return nullptr;
710 }
Brian Osman133823d2018-09-19 14:14:15 -0400711 sk_sp<SkData> data(SkData::MakeFromFileName(path));
712 if (!data) {
fmalita6519c212016-09-14 08:05:17 -0700713 SkDebugf("Could not read %s.\n", path);
714 return nullptr;
715 }
716
Hal Canary0f666812018-03-22 15:21:12 -0400717#ifdef SK_XML
Brian Osman133823d2018-09-19 14:14:15 -0400718 SkMemoryStream stream(std::move(data));
fmalitae1baa7c2016-09-14 12:04:30 -0700719 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
fmalita6519c212016-09-14 08:05:17 -0700720 if (!svgDom) {
721 SkDebugf("Could not parse %s.\n", path);
722 return nullptr;
723 }
724
fmalitae1baa7c2016-09-14 12:04:30 -0700725 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
726 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
727 if (svgDom->containerSize().isEmpty()) {
728 svgDom->setContainerSize(kDefaultContainerSize);
729 }
730
fmalita6519c212016-09-14 08:05:17 -0700731 SkPictureRecorder recorder;
fmalitae1baa7c2016-09-14 12:04:30 -0700732 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
733 svgDom->containerSize().height()));
fmalita6519c212016-09-14 08:05:17 -0700734 return recorder.finishRecordingAsPicture();
Hal Canary0f666812018-03-22 15:21:12 -0400735#else
736 return nullptr;
737#endif // SK_XML
fmalita6519c212016-09-14 08:05:17 -0700738 }
739
mtklein92007582014-08-01 07:46:52 -0700740 Benchmark* next() {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400741 std::unique_ptr<Benchmark> bench;
mtklein65dfd2f2016-02-03 10:40:54 -0800742 do {
743 bench.reset(this->rawNext());
744 if (!bench) {
745 return nullptr;
746 }
Mike Klein88544fb2019-03-20 10:50:33 -0500747 } while (CommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
748 CommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700749 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800750 }
751
752 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700753 if (fBenches) {
Hal Canary972eba32018-07-30 17:07:07 -0400754 Benchmark* bench = fBenches->get()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700755 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700756 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700757 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700758 return bench;
759 }
mtklein92007582014-08-01 07:46:52 -0700760
mtkleine714e752014-07-31 12:13:48 -0700761 while (fGMs) {
Ben Wagner406ff502019-08-12 16:39:24 -0400762 std::unique_ptr<skiagm::GM> gm = fGMs->get()();
mtkleine714e752014-07-31 12:13:48 -0700763 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800764 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700765 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700766 fBenchType = "micro";
Ben Wagner406ff502019-08-12 16:39:24 -0400767 return new GMBench(std::move(gm));
mtkleine714e752014-07-31 12:13:48 -0700768 }
769 }
mtklein92007582014-08-01 07:46:52 -0700770
Hal Canary5dfefa22019-10-24 13:52:17 -0400771 while (fCurrentTextBlobTrace < fTextBlobTraces.count()) {
772 SkString path = fTextBlobTraces[fCurrentTextBlobTrace++];
773 SkString basename = SkOSPath::Basename(path.c_str());
774 static constexpr char kEnding[] = ".trace";
775 if (basename.endsWith(kEnding)) {
776 basename.remove(basename.size() - strlen(kEnding), strlen(kEnding));
777 }
778 fSourceType = "texttrace";
779 fBenchType = "micro";
780 return CreateDiffCanvasBench(
781 SkStringPrintf("SkDiffBench-%s", basename.c_str()),
782 [path](){ return SkStream::MakeFromFile(path.c_str()); });
783 }
784
mtkleinfd731ce2014-09-10 12:19:30 -0700785 // First add all .skps as RecordingBenches.
786 while (fCurrentRecording < fSKPs.count()) {
787 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700788 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
789 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700790 continue;
791 }
792 SkString name = SkOSPath::Basename(path.c_str());
793 fSourceType = "skp";
794 fBenchType = "recording";
Mike Klein88d90712018-01-27 17:30:04 +0000795 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
796 fSKPOps = pic->approximateOpCount();
Mike Kleina705cb92019-05-14 12:33:40 -0500797 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700798 }
799
Mike Reede45ff462017-12-06 10:47:03 -0500800 // Add all .skps as DeserializePictureBenchs.
801 while (fCurrentDeserialPicture < fSKPs.count()) {
802 const SkString& path = fSKPs[fCurrentDeserialPicture++];
803 sk_sp<SkData> data = SkData::MakeFromFileName(path.c_str());
804 if (!data) {
805 continue;
806 }
807 SkString name = SkOSPath::Basename(path.c_str());
808 fSourceType = "skp";
809 fBenchType = "deserial";
Mike Reed7557bbb2017-12-24 19:50:57 -0500810 fSKPBytes = static_cast<double>(data->size());
Mike Klein88d90712018-01-27 17:30:04 +0000811 fSKPOps = 0;
Mike Reede45ff462017-12-06 10:47:03 -0500812 return new DeserializePictureBench(name.c_str(), std::move(data));
813 }
814
mtkleinfd731ce2014-09-10 12:19:30 -0700815 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700816 while (fCurrentScale < fScales.count()) {
817 while (fCurrentSKP < fSKPs.count()) {
Brian Salomon626349b2020-08-27 10:54:36 -0400818 const SkString& path = fSKPs[fCurrentSKP++];
reedca2622b2016-03-18 07:25:55 -0700819 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
820 if (!pic) {
mtklein92007582014-08-01 07:46:52 -0700821 continue;
822 }
robertphillips5b693772014-11-21 06:19:36 -0800823
Brian Salomon626349b2020-08-27 10:54:36 -0400824 if (FLAGS_bbh) {
825 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
826 SkRTreeFactory factory;
827 SkPictureRecorder recorder;
828 pic->playback(recorder.beginRecording(pic->cullRect().width(),
829 pic->cullRect().height(),
830 &factory));
831 pic = recorder.finishRecordingAsPicture();
mtklein20840502014-08-21 15:51:22 -0700832 }
Brian Salomon626349b2020-08-27 10:54:36 -0400833 SkString name = SkOSPath::Basename(path.c_str());
834 fSourceType = "skp";
835 fBenchType = "playback";
836 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
837 FLAGS_loopSKP);
mtklein92007582014-08-01 07:46:52 -0700838 }
fmalita6519c212016-09-14 08:05:17 -0700839
Brian Salomon626349b2020-08-27 10:54:36 -0400840 while (fCurrentSVG < fSVGs.count()) {
841 const char* path = fSVGs[fCurrentSVG++].c_str();
fmalita6519c212016-09-14 08:05:17 -0700842 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
843 fSourceType = "svg";
844 fBenchType = "playback";
845 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
Brian Salomon626349b2020-08-27 10:54:36 -0400846 fScales[fCurrentScale], FLAGS_loopSKP);
fmalita6519c212016-09-14 08:05:17 -0700847 }
848 }
849
mtklein92007582014-08-01 07:46:52 -0700850 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700851 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700852 fCurrentScale++;
853 }
854
joshualitt261c3ad2015-04-27 09:16:57 -0700855 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700856 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700857 while (fCurrentAnimSKP < fSKPs.count()) {
858 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700859 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
860 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700861 fCurrentAnimSKP++;
862 continue;
863 }
864
865 fCurrentAnimSKP++;
866 SkString name = SkOSPath::Basename(path.c_str());
Ben Wagneracf98df2019-07-12 12:51:44 -0400867 sk_sp<SKPAnimationBench::Animation> animation =
868 SKPAnimationBench::MakeZoomAnimation(fZoomMax, fZoomPeriodMs);
869 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, std::move(animation),
halcanary385fe4d2015-08-26 13:07:48 -0700870 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700871 }
872 }
873
scroggo60869a42015-04-01 12:09:17 -0700874 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700875 fSourceType = "image";
876 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700877 const SkString& path = fImages[fCurrentCodec];
Mike Klein88544fb2019-03-20 10:50:33 -0500878 if (CommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
mtklein6f0ff912016-01-11 09:04:21 -0800879 continue;
880 }
bungeman38d909e2016-08-02 14:40:46 -0700881 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400882 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700883 if (!codec) {
884 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700885 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700886 continue;
887 }
scroggo21027992015-04-02 13:22:38 -0700888
scroggo60869a42015-04-01 12:09:17 -0700889 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700890 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700891
msarettc7796b92016-01-07 14:20:20 -0800892 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700893 if (FLAGS_simpleCodec) {
894 if (kUnpremul_SkAlphaType == alphaType) {
895 alphaType = kPremul_SkAlphaType;
896 }
897
898 fCurrentColorType++;
899 } else {
900 switch (alphaType) {
901 case kOpaque_SkAlphaType:
902 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800903 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700904 break;
905 case kUnpremul_SkAlphaType:
906 case kPremul_SkAlphaType:
907 if (0 == fCurrentAlphaType) {
908 // Test unpremul first.
909 alphaType = kUnpremul_SkAlphaType;
910 fCurrentAlphaType++;
911 } else {
912 // Test premul.
913 alphaType = kPremul_SkAlphaType;
914 fCurrentAlphaType = 0;
915 fCurrentColorType++;
916 }
917 break;
918 default:
919 SkASSERT(false);
920 fCurrentColorType++;
921 break;
922 }
scroggo21027992015-04-02 13:22:38 -0700923 }
924
msarettc7796b92016-01-07 14:20:20 -0800925 // Make sure we can decode to this color type and alpha type.
926 SkImageInfo info =
927 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700928 const size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400929 SkAutoMalloc storage(info.computeByteSize(rowBytes));
scroggo21027992015-04-02 13:22:38 -0700930
scroggoeb602a52015-07-09 08:16:03 -0700931 const SkCodec::Result result = codec->getPixels(
Leon Scroggins571b30f2017-07-11 17:35:31 +0000932 info, storage.get(), rowBytes);
scroggo60869a42015-04-01 12:09:17 -0700933 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700934 case SkCodec::kSuccess:
935 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700936 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700937 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700938 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700939 // This is okay. Not all conversions are valid.
940 break;
scroggo60869a42015-04-01 12:09:17 -0700941 default:
942 // This represents some sort of failure.
943 SkASSERT(false);
944 break;
945 }
946 }
947 fCurrentColorType = 0;
948 }
949
msarett84451022016-02-11 06:45:51 -0800950 // Run AndroidCodecBenches
951 const int sampleSizes[] = { 2, 4, 8 };
952 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
953 fSourceType = "image";
954 fBenchType = "skandroidcodec";
955
956 const SkString& path = fImages[fCurrentAndroidCodec];
Mike Klein88544fb2019-03-20 10:50:33 -0500957 if (CommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
msarett84451022016-02-11 06:45:51 -0800958 continue;
959 }
bungeman38d909e2016-08-02 14:40:46 -0700960 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400961 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800962 if (!codec) {
963 // Nothing to time.
964 SkDebugf("Cannot find codec for %s\n", path.c_str());
965 continue;
966 }
967
968 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
969 int sampleSize = sampleSizes[fCurrentSampleSize];
970 fCurrentSampleSize++;
Brian Osman788b9162020-02-07 10:36:46 -0500971 if (10 * sampleSize > std::min(codec->getInfo().width(), codec->getInfo().height())) {
msarett84451022016-02-11 06:45:51 -0800972 // Avoid benchmarking scaled decodes of already small images.
973 break;
974 }
975
bungeman38d909e2016-08-02 14:40:46 -0700976 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
977 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -0800978 }
979 fCurrentSampleSize = 0;
980 }
981
Leon Scroggins III87caae62020-05-04 10:02:45 -0400982#ifdef SK_ENABLE_ANDROID_UTILS
msarett7f691442015-09-22 11:56:16 -0700983 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700984 // We intend to create benchmarks that model the use cases in
985 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
986 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
987 // the image. For that reason, we will benchmark decodes in five representative locations
988 // in the image. Additionally, this use case utilizes power of two scaling, so we will
989 // test on power of two sample sizes. The output tile is always 512x512, so, when a
990 // sampleSize is used, the size of the subset that is decoded is always
991 // (sampleSize*512)x(sampleSize*512).
992 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700993 // All use cases we are aware of only scale by powers of two.
994 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
995 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800996 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700997 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800998 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700999 fSourceType = "image";
1000 fBenchType = "BRD";
1001
mtklein6f0ff912016-01-11 09:04:21 -08001002 const SkString& path = fImages[fCurrentBRDImage];
Mike Klein88544fb2019-03-20 10:50:33 -05001003 if (CommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
mtklein6f0ff912016-01-11 09:04:21 -08001004 continue;
1005 }
scroggo860e8a62015-10-15 07:51:28 -07001006
msarettd1227a72016-05-18 06:23:57 -07001007 while (fCurrentColorType < fColorTypes.count()) {
1008 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
1009 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -07001010
bungeman38d909e2016-08-02 14:40:46 -07001011 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -07001012 const SkColorType colorType = fColorTypes[fCurrentColorType];
1013 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
1014 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -07001015
msarettd1227a72016-05-18 06:23:57 -07001016 int width = 0;
1017 int height = 0;
reed42943c82016-09-12 12:01:44 -07001018 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -07001019 &width, &height)) {
1020 break;
msarett7f691442015-09-22 11:56:16 -07001021 }
msarettd1227a72016-05-18 06:23:57 -07001022
1023 SkString basename = SkOSPath::Basename(path.c_str());
1024 SkIRect subset;
1025 const uint32_t subsetSize = sampleSize * minOutputSize;
1026 switch (currentSubsetType) {
1027 case kTopLeft_SubsetType:
1028 basename.append("_TopLeft");
1029 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
1030 break;
1031 case kTopRight_SubsetType:
1032 basename.append("_TopRight");
1033 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
1034 subsetSize);
1035 break;
1036 case kMiddle_SubsetType:
1037 basename.append("_Middle");
1038 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
1039 (height - subsetSize) / 2, subsetSize, subsetSize);
1040 break;
1041 case kBottomLeft_SubsetType:
1042 basename.append("_BottomLeft");
1043 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
1044 subsetSize);
1045 break;
1046 case kBottomRight_SubsetType:
1047 basename.append("_BottomRight");
1048 subset = SkIRect::MakeXYWH(width - subsetSize,
1049 height - subsetSize, subsetSize, subsetSize);
1050 break;
1051 default:
1052 SkASSERT(false);
1053 }
1054
1055 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
1056 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -07001057 }
msarettd1227a72016-05-18 06:23:57 -07001058 fCurrentSubsetType = 0;
1059 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -07001060 }
msarettd1227a72016-05-18 06:23:57 -07001061 fCurrentSampleSize = 0;
1062 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -07001063 }
msarettd1227a72016-05-18 06:23:57 -07001064 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -07001065 }
Leon Scroggins III87caae62020-05-04 10:02:45 -04001066#endif // SK_ENABLE_ANDROID_UTILS
msarett7f691442015-09-22 11:56:16 -07001067
halcanary96fcdcc2015-08-27 07:41:13 -07001068 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001069 }
mtklein92007582014-08-01 07:46:52 -07001070
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001071 void fillCurrentOptions(NanoJSONResultsWriter& log) const {
1072 log.appendString("source_type", fSourceType);
1073 log.appendString("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001074 if (0 == strcmp(fSourceType, "skp")) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001075 log.appendString("clip",
mtklein92007582014-08-01 07:46:52 -07001076 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1077 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001078 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001079 log.appendString("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
mtklein92007582014-08-01 07:46:52 -07001080 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001081 }
1082
1083 void fillCurrentMetrics(NanoJSONResultsWriter& log) const {
mtklein051e56d2014-12-04 08:46:51 -08001084 if (0 == strcmp(fBenchType, "recording")) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001085 log.appendMetric("bytes", fSKPBytes);
1086 log.appendMetric("ops", fSKPOps);
mtklein051e56d2014-12-04 08:46:51 -08001087 }
mtklein92007582014-08-01 07:46:52 -07001088 }
1089
mtkleine714e752014-07-31 12:13:48 -07001090private:
Leon Scroggins III87caae62020-05-04 10:02:45 -04001091#ifdef SK_ENABLE_ANDROID_UTILS
msarettb23e6aa2015-06-09 13:56:10 -07001092 enum SubsetType {
1093 kTopLeft_SubsetType = 0,
1094 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001095 kMiddle_SubsetType = 2,
1096 kBottomLeft_SubsetType = 3,
1097 kBottomRight_SubsetType = 4,
1098 kTranslate_SubsetType = 5,
1099 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001100 kLast_SubsetType = kZoom_SubsetType,
1101 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001102 };
Leon Scroggins III87caae62020-05-04 10:02:45 -04001103#endif
msarettb23e6aa2015-06-09 13:56:10 -07001104
mtkleine714e752014-07-31 12:13:48 -07001105 const BenchRegistry* fBenches;
1106 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001107 SkIRect fClip;
1108 SkTArray<SkScalar> fScales;
1109 SkTArray<SkString> fSKPs;
fmalita6519c212016-09-14 08:05:17 -07001110 SkTArray<SkString> fSVGs;
Hal Canary5dfefa22019-10-24 13:52:17 -04001111 SkTArray<SkString> fTextBlobTraces;
msarett95f192d2015-02-13 09:05:41 -08001112 SkTArray<SkString> fImages;
msarett74deb982015-10-20 16:45:56 -07001113 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001114 SkScalar fZoomMax;
1115 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001116
Mike Klein88d90712018-01-27 17:30:04 +00001117 double fSKPBytes, fSKPOps;
mtklein051e56d2014-12-04 08:46:51 -08001118
mtkleinfd731ce2014-09-10 12:19:30 -07001119 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1120 const char* fBenchType; // How we bench it: micro, recording, playback, ...
Hal Canary2362ae72019-10-24 13:40:40 -04001121 int fCurrentRecording = 0;
1122 int fCurrentDeserialPicture = 0;
1123 int fCurrentScale = 0;
1124 int fCurrentSKP = 0;
1125 int fCurrentSVG = 0;
Hal Canary5dfefa22019-10-24 13:52:17 -04001126 int fCurrentTextBlobTrace = 0;
Hal Canary2362ae72019-10-24 13:40:40 -04001127 int fCurrentCodec = 0;
1128 int fCurrentAndroidCodec = 0;
Leon Scroggins III87caae62020-05-04 10:02:45 -04001129#ifdef SK_ENABLE_ANDROID_UTILS
Hal Canary2362ae72019-10-24 13:40:40 -04001130 int fCurrentBRDImage = 0;
Leon Scroggins III87caae62020-05-04 10:02:45 -04001131 int fCurrentSubsetType = 0;
1132#endif
Hal Canary2362ae72019-10-24 13:40:40 -04001133 int fCurrentColorType = 0;
1134 int fCurrentAlphaType = 0;
Hal Canary2362ae72019-10-24 13:40:40 -04001135 int fCurrentSampleSize = 0;
1136 int fCurrentAnimSKP = 0;
mtkleine714e752014-07-31 12:13:48 -07001137};
1138
msarettc149f0e2016-01-04 11:35:43 -08001139// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1140// This prints something every once in a while so that it knows we're still working.
1141static void start_keepalive() {
Mike Klein03141d22017-10-30 11:57:15 -04001142 static std::thread* intentionallyLeaked = new std::thread([]{
1143 for (;;) {
1144 static const int kSec = 1200;
1145 #if defined(SK_BUILD_FOR_WIN)
1146 Sleep(kSec * 1000);
1147 #else
1148 sleep(kSec);
1149 #endif
1150 SkDebugf("\nBenchmarks still running...\n");
msarettc149f0e2016-01-04 11:35:43 -08001151 }
Mike Klein03141d22017-10-30 11:57:15 -04001152 });
1153 (void)intentionallyLeaked;
msarettc149f0e2016-01-04 11:35:43 -08001154}
1155
John Stiles2ef627a2021-03-23 10:25:02 -04001156class NanobenchShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
1157 void compileError(const char* shader, const char* errors) override {
1158 // Nanobench should abort if any shader can't compile. Failure is much better than
1159 // reporting meaningless performance metrics.
1160 SkSL::String message = GrShaderUtils::BuildShaderErrorMessage(shader, errors);
John Stiles345f1402021-03-23 10:25:15 -04001161 SK_ABORT("\n%s", message.c_str());
John Stiles2ef627a2021-03-23 10:25:02 -04001162 }
1163};
1164
Mike Kleinbe28ee22017-02-06 12:46:20 -05001165int main(int argc, char** argv) {
Mike Klein88544fb2019-03-20 10:50:33 -05001166 CommandLineFlags::Parse(argc, argv);
Brian Osman53136aa2017-07-20 15:43:35 -04001167
Brian Osmanbc8150f2017-07-24 11:38:01 -04001168 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -04001169
Mike Kleinadacaef2017-02-06 09:26:14 -05001170#if defined(SK_BUILD_FOR_IOS)
1171 cd_Documents();
1172#endif
jcgregorio3b27ade2014-11-13 08:06:40 -08001173 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001174 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001175 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001176
Chris Dalton040238b2017-12-18 14:22:34 -07001177 SetCtxOptionsFromCommonFlags(&grContextOpts);
krajcevski69a55602014-08-13 10:46:31 -07001178
John Stiles2ef627a2021-03-23 10:25:02 -04001179 NanobenchShaderErrorHandler errorHandler;
1180 grContextOpts.fShaderErrorHandler = &errorHandler;
1181
bsalomon6eb03cc2014-08-07 14:28:50 -07001182 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001183 FLAGS_samples = 1;
1184 FLAGS_gpuFrameLag = 0;
1185 }
1186
bsalomon6eb03cc2014-08-07 14:28:50 -07001187 if (!FLAGS_writePath.isEmpty()) {
1188 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1189 if (!sk_mkdir(FLAGS_writePath[0])) {
1190 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001191 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001192 }
1193 }
1194
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001195 std::unique_ptr<SkWStream> logStream(new SkNullWStream);
mtklein60317d0f2014-07-14 11:30:37 -07001196 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001197#if defined(SK_RELEASE)
Chris Dalton2e1381e2019-10-23 14:35:19 -06001198 // SkJSONWriter uses a 32k in-memory cache, so it only flushes occasionally and is well
1199 // equipped for a stream that re-opens, appends, and closes the file on every write.
1200 logStream.reset(new NanoFILEAppendAndCloseStream(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001201#else
1202 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1203 return 1;
1204#endif
mtklein60317d0f2014-07-14 11:30:37 -07001205 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001206 NanoJSONResultsWriter log(logStream.get(), SkJSONWriter::Mode::kPretty);
1207 log.beginObject(); // root
mtklein1915b622014-08-20 11:45:00 -07001208
1209 if (1 == FLAGS_properties.count() % 2) {
1210 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1211 return 1;
1212 }
1213 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001214 log.appendString(FLAGS_properties[i-1], FLAGS_properties[i]);
mtklein1915b622014-08-20 11:45:00 -07001215 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001216
1217 if (1 == FLAGS_key.count() % 2) {
1218 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1219 return 1;
1220 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001221 if (FLAGS_key.count()) {
1222 log.beginObject("key");
1223 for (int i = 1; i < FLAGS_key.count(); i += 2) {
1224 log.appendString(FLAGS_key[i - 1], FLAGS_key[i]);
1225 }
1226 log.endObject(); // key
mtklein94e51562014-08-19 12:41:53 -07001227 }
mtklein60317d0f2014-07-14 11:30:37 -07001228
mtkleinf3723212014-06-25 14:08:00 -07001229 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001230 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001231
cdaltone1b89582015-06-25 19:17:08 -07001232 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001233
bsalomon6eb03cc2014-08-07 14:28:50 -07001234 if (kAutoTuneLoops != FLAGS_loops) {
1235 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001236 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001237 SkDebugf("! -> high variance, ? -> moderate variance\n");
1238 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001239 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001240 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001241 } else {
mtkleind75c4662015-04-30 07:11:22 -07001242 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001243 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001244 }
1245
svaisanenc47635e2016-01-28 06:05:43 -08001246 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001247 create_configs(&configs);
1248
msarettc149f0e2016-01-04 11:35:43 -08001249 if (FLAGS_keepAlive) {
1250 start_keepalive();
1251 }
1252
Mike Klein03fa5d42019-03-22 11:39:09 -05001253 SetAnalyticAAFromCommonFlags();
liyuqian38911a72016-10-04 11:23:22 -07001254
Mike Klein813e8cc2020-08-05 09:33:38 -05001255 gSkForceRasterPipelineBlitter = FLAGS_forceRasterPipeline;
1256 gUseSkVMBlitter = FLAGS_skvm;
Mike Kleinf2b3f912021-03-23 14:32:45 -05001257 gSkVMAllowJIT = FLAGS_jit;
1258 gSkVMJITViaDylib = FLAGS_dylib;
Yuqian Li550148b2017-01-13 10:13:13 -05001259
mtkleine070c2b2014-10-14 08:40:43 -07001260 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001261 BenchmarkStream benchStream;
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001262 log.beginObject("results");
Jim Van Verth8a9a3712019-05-31 10:49:12 -04001263 AutoreleasePool pool;
mtklein92007582014-08-01 07:46:52 -07001264 while (Benchmark* b = benchStream.next()) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001265 std::unique_ptr<Benchmark> bench(b);
Mike Klein88544fb2019-03-20 10:50:33 -05001266 if (CommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001267 continue;
1268 }
1269
svaisanenc47635e2016-01-28 06:05:43 -08001270 if (!configs.empty()) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001271 log.beginBench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001272 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001273 }
egdaniel3bf92062015-06-26 08:12:46 -07001274 for (int i = 0; i < configs.count(); ++i) {
1275 Target* target = is_enabled(b, configs[i]);
1276 if (!target) {
1277 continue;
1278 }
mtkleinf3723212014-06-25 14:08:00 -07001279
halcanary96fcdcc2015-08-27 07:41:13 -07001280 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001281 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001282 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001283
brianosman7a5ada82016-02-08 13:49:12 -08001284 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001285 SkDebugf("Running %s\t%s\n"
1286 , bench->getUniqueName()
1287 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001288 if (FLAGS_dryRun) {
1289 continue;
1290 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001291 }
1292
Mike Kleinee254232019-03-28 08:32:14 -05001293 if (FLAGS_purgeBetweenBenches) {
1294 SkGraphics::PurgeAllCaches();
1295 }
1296
Brian Osman5ad87aa2017-09-08 16:05:23 -04001297 TRACE_EVENT2("skia", "Benchmark", "name", TRACE_STR_COPY(bench->getUniqueName()),
1298 "config", TRACE_STR_COPY(config));
Brian Osmanc3cdef52017-08-31 14:09:22 -04001299
egdaniel3bf92062015-06-26 08:12:46 -07001300 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001301 bench->perCanvasPreDraw(canvas);
1302
cdaltone1b89582015-06-25 19:17:08 -07001303 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001304 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001305 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1306 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001307
Leon Scroggins IIIb30d1132017-10-02 09:48:15 -04001308 if (kFailedLoops == loops) {
1309 // Can't be timed. A warning note has already been printed.
1310 cleanup_run(target);
1311 continue;
1312 }
1313
Yuqian Li3528eb32017-09-21 13:23:49 -04001314 if (runs == 0 && FLAGS_ms < 1000) {
1315 // Run the first bench for 1000ms to warm up the nanobench if FLAGS_ms < 1000.
1316 // Otherwise, the first few benches' measurements will be inaccurate.
1317 auto stop = now_ms() + 1000;
1318 do {
1319 time(loops, bench.get(), target);
Jim Van Verth49d6bca2020-02-28 11:05:44 -05001320 pool.drain();
Yuqian Li3528eb32017-09-21 13:23:49 -04001321 } while (now_ms() < stop);
1322 }
1323
mtkleinbbba1682015-10-28 11:36:30 -07001324 if (FLAGS_ms) {
1325 samples.reset();
1326 auto stop = now_ms() + FLAGS_ms;
1327 do {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001328 samples.push_back(time(loops, bench.get(), target) / loops);
Jim Van Verth49d6bca2020-02-28 11:05:44 -05001329 pool.drain();
mtkleinbbba1682015-10-28 11:36:30 -07001330 } while (now_ms() < stop);
1331 } else {
cdaltone1b89582015-06-25 19:17:08 -07001332 samples.reset(FLAGS_samples);
1333 for (int s = 0; s < FLAGS_samples; s++) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001334 samples[s] = time(loops, bench.get(), target) / loops;
Jim Van Verth49d6bca2020-02-28 11:05:44 -05001335 pool.drain();
cdaltone1b89582015-06-25 19:17:08 -07001336 }
cdaltone1b89582015-06-25 19:17:08 -07001337 }
mtkleinf3723212014-06-25 14:08:00 -07001338
Mike Kleinfec9b902019-05-31 10:03:07 -05001339 // Scale each result to the benchmark's own units, time/unit.
1340 for (double& sample : samples) {
1341 sample *= (1.0 / bench->getUnits());
1342 }
1343
joshualitte45c81c2015-12-02 09:05:37 -08001344 SkTArray<SkString> keys;
1345 SkTArray<double> values;
1346 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1347 if (gpuStatsDump) {
1348 // TODO cache stats
1349 bench->getGpuStats(canvas, &keys, &values);
1350 }
joshualitte45c81c2015-12-02 09:05:37 -08001351
robertphillips5b693772014-11-21 06:19:36 -08001352 bench->perCanvasPostDraw(canvas);
1353
egdaniel3bf92062015-06-26 08:12:46 -07001354 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001355 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001356 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001357 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001358 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001359 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001360 }
1361
Mike Klein512a2442018-09-27 10:44:56 -04001362 // Building stats.plot often shows up in profiles,
1363 // so skip building it when we're not going to print it anyway.
1364 const bool want_plot = !FLAGS_quiet;
1365
1366 Stats stats(samples, want_plot);
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001367 log.beginObject(config);
1368
1369 log.beginObject("options");
1370 log.appendString("name", bench->getName());
1371 benchStream.fillCurrentOptions(log);
1372 target->fillOptions(log);
1373 log.endObject(); // options
1374
1375 // Metrics
1376 log.appendMetric("min_ms", stats.min);
1377 log.beginArray("samples");
1378 for (double sample : samples) {
1379 log.appendDoubleDigits(sample, 16);
1380 }
1381 log.endArray(); // samples
1382 benchStream.fillCurrentMetrics(log);
joshualitte45c81c2015-12-02 09:05:37 -08001383 if (gpuStatsDump) {
1384 // dump to json, only SKPBench currently returns valid keys / values
1385 SkASSERT(keys.count() == values.count());
1386 for (int i = 0; i < keys.count(); i++) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001387 log.appendMetric(keys[i].c_str(), values[i]);
joshualitte45c81c2015-12-02 09:05:37 -08001388 }
1389 }
joshualitte45c81c2015-12-02 09:05:37 -08001390
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001391 log.endObject(); // config
1392
mtkleine070c2b2014-10-14 08:40:43 -07001393 if (runs++ % FLAGS_flushEvery == 0) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001394 log.flush();
mtkleine070c2b2014-10-14 08:40:43 -07001395 }
mtklein60317d0f2014-07-14 11:30:37 -07001396
bsalomon6eb03cc2014-08-07 14:28:50 -07001397 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001398 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001399 config = ""; // Only print the config if we run the same bench on more than one.
1400 }
mtkleind75c4662015-04-30 07:11:22 -07001401 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1402 , sk_tools::getCurrResidentSetSizeMB()
1403 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001404 , bench->getUniqueName()
1405 , config);
mtkleinf3723212014-06-25 14:08:00 -07001406 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001407 const char* mark = " ";
Hal Canarye99e8da2019-01-14 16:34:50 -05001408 const double stddev_percent =
1409 sk_ieee_double_divide(100 * sqrt(stats.var), stats.mean);
mtklein66cfcff2015-12-04 06:35:30 -08001410 if (stddev_percent > 5) mark = "?";
1411 if (stddev_percent > 10) mark = "!";
1412
1413 SkDebugf("%10.2f %s\t%s\t%s\n",
1414 stats.median*1e3, mark, bench->getUniqueName(), config);
Mike Reedc665e5b2017-07-04 22:56:06 -04001415 } else if (FLAGS_csv) {
Hal Canarye99e8da2019-01-14 16:34:50 -05001416 const double stddev_percent =
1417 sk_ieee_double_divide(100 * sqrt(stats.var), stats.mean);
Mike Reedc665e5b2017-07-04 22:56:06 -04001418 SkDebugf("%g,%g,%g,%g,%g,%s,%s\n"
1419 , stats.min
1420 , stats.median
1421 , stats.mean
1422 , stats.max
1423 , stddev_percent
1424 , config
1425 , bench->getUniqueName()
1426 );
1427 } else {
1428 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 -05001429 const double stddev_percent =
1430 sk_ieee_double_divide(100 * sqrt(stats.var), stats.mean);
Mike Reedc665e5b2017-07-04 22:56:06 -04001431 SkDebugf(format
mtkleind75c4662015-04-30 07:11:22 -07001432 , sk_tools::getCurrResidentSetSizeMB()
1433 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001434 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001435 , HUMANIZE(stats.min)
1436 , HUMANIZE(stats.median)
1437 , HUMANIZE(stats.mean)
1438 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001439 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001440 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001441 , config
mtklein96289052014-09-10 12:05:59 -07001442 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001443 );
1444 }
joshualitte45c81c2015-12-02 09:05:37 -08001445
joshualitte45c81c2015-12-02 09:05:37 -08001446 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -04001447 target->dumpStats();
bsalomon06cddec2014-10-24 10:40:50 -07001448 }
joshualitte45c81c2015-12-02 09:05:37 -08001449
cdalton2c56ba52015-06-26 13:32:53 -07001450 if (FLAGS_verbose) {
1451 SkDebugf("Samples: ");
1452 for (int i = 0; i < samples.count(); i++) {
1453 SkDebugf("%s ", HUMANIZE(samples[i]));
1454 }
1455 SkDebugf("%s\n", bench->getUniqueName());
1456 }
egdaniel3bf92062015-06-26 08:12:46 -07001457 cleanup_run(target);
Jim Van Verth8a9a3712019-05-31 10:49:12 -04001458 pool.drain();
mtkleinf3723212014-06-25 14:08:00 -07001459 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001460 if (!configs.empty()) {
1461 log.endBench();
1462 }
mtkleinf3723212014-06-25 14:08:00 -07001463 }
1464
Herb Derby5a523fe2017-01-26 16:48:28 -05001465 SkGraphics::PurgeAllCaches();
1466
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001467 log.beginBench("memory_usage", 0, 0);
1468 log.beginObject("meta"); // config
1469 log.appendS32("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1470 log.endObject(); // config
1471 log.endBench();
1472
Brian Osman68870aa2020-07-08 14:12:43 -04001473 RunSkSLMemoryBenchmarks(&log);
1474
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001475 log.endObject(); // results
1476 log.endObject(); // root
1477 log.flush();
mtkleine1091452014-12-04 10:47:02 -08001478
mtkleinf3723212014-06-25 14:08:00 -07001479 return 0;
1480}