blob: 0f9332653a8a378dcb3f5cc3cc47084ec51a683d [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"
Brian Salomon31fddc32021-04-30 13:08:55 -040017#include "bench/MSKPBench.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#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"
Brian Osman68870aa2020-07-08 14:12:43 -040023#include "bench/SkSLBench.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/core/SkColorSpacePriv.h"
Ben Wagnerab6eefe2019-05-20 11:02:49 -040035#include "src/core/SkLeanWindows.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/core/SkOSFile.h"
37#include "src/core/SkTaskGroup.h"
38#include "src/core/SkTraceEvent.h"
John Stiles2ef627a2021-03-23 10:25:02 -040039#include "src/gpu/GrShaderUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#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"
Brian Salomon31fddc32021-04-30 13:08:55 -040044#include "tools/MSKPPlayer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050045#include "tools/ProcStats.h"
46#include "tools/Stats.h"
47#include "tools/flags/CommonFlags.h"
48#include "tools/flags/CommonFlagsConfig.h"
49#include "tools/ios_utils.h"
50#include "tools/trace/EventTracingPriv.h"
51#include "tools/trace/SkDebugfTracer.h"
mtkleinf3723212014-06-25 14:08:00 -070052
Hal Canary0f666812018-03-22 15:21:12 -040053#ifdef SK_XML
Florin Malitab3418102020-10-15 18:10:29 -040054#include "modules/svg/include/SkSVGDOM.h"
Hal Canary0f666812018-03-22 15:21:12 -040055#endif // SK_XML
56
Leon Scroggins III87caae62020-05-04 10:02:45 -040057#ifdef SK_ENABLE_ANDROID_UTILS
58#include "bench/BitmapRegionDecoderBench.h"
59#include "client_utils/android/BitmapRegionDecoder.h"
60#endif
61
Adlai Holler684838f2020-05-12 10:41:04 -040062#include <cinttypes>
bungeman60e0fee2015-08-26 05:15:46 -070063#include <stdlib.h>
John Stilesfbd050b2020-08-03 13:21:46 -040064#include <memory>
Mike Klein03141d22017-10-30 11:57:15 -040065#include <thread>
bungeman60e0fee2015-08-26 05:15:46 -070066
Mike Reedc928fe22017-06-05 10:20:31 -040067extern bool gSkForceRasterPipelineBlitter;
Mike Klein2a57e792020-01-14 12:47:52 -060068extern bool gUseSkVMBlitter;
Mike Klein813e8cc2020-08-05 09:33:38 -050069extern bool gSkVMAllowJIT;
Mike Kleinea4459d2020-01-16 13:05:04 -060070extern bool gSkVMJITViaDylib;
Mike Reedc928fe22017-06-05 10:20:31 -040071
Mike Klein8f11d4d2018-01-24 12:42:55 -050072#ifndef SK_BUILD_FOR_WIN
scroggo38ce0a72016-01-07 07:28:47 -080073 #include <unistd.h>
Brian Salomon032aaae2018-03-23 16:10:36 -040074
scroggo38ce0a72016-01-07 07:28:47 -080075#endif
76
Robert Phillips00f78de2020-07-01 16:09:43 -040077#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050078#include "src/gpu/GrCaps.h"
Adlai Hollera0693042020-10-14 11:23:11 -040079#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050080#include "src/gpu/SkGr.h"
81#include "src/gpu/gl/GrGLDefines.h"
82#include "src/gpu/gl/GrGLGpu.h"
83#include "src/gpu/gl/GrGLUtil.h"
84#include "tools/gpu/GrContextFactory.h"
Brian Salomon032aaae2018-03-23 16:10:36 -040085
Brian Osmanc7ad40f2018-05-31 14:27:17 -040086using sk_gpu_test::ContextInfo;
87using sk_gpu_test::GrContextFactory;
88using sk_gpu_test::TestContext;
Brian Salomon032aaae2018-03-23 16:10:36 -040089
Brian Osmanc7ad40f2018-05-31 14:27:17 -040090GrContextOptions grContextOpts;
bsalomon682c2692015-05-22 14:01:46 -070091
reed53249782014-10-10 09:09:52 -070092static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070093
bsalomon6eb03cc2014-08-07 14:28:50 -070094static SkString loops_help_txt() {
95 SkString help;
96 help.printf("Number of times to run each bench. Set this to %d to auto-"
97 "tune for each bench. Timings are only reported when auto-tuning.",
98 kAutoTuneLoops);
99 return help;
100}
101
cdaltone1b89582015-06-25 19:17:08 -0700102static SkString to_string(int n) {
103 SkString str;
104 str.appendS32(n);
105 return str;
106}
107
Mike Kleinac8f4432019-07-01 10:15:32 -0500108static DEFINE_int(loops, kAutoTuneLoops, loops_help_txt().c_str());
bsalomon6eb03cc2014-08-07 14:28:50 -0700109
Mike Klein5b3f3432019-03-21 11:42:21 -0500110static DEFINE_int(samples, 10, "Number of samples to measure for each bench.");
111static DEFINE_int(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
112static DEFINE_int(overheadLoops, 100000, "Loops to estimate timer overhead.");
Mike Klein84836b72019-03-21 11:31:36 -0500113static DEFINE_double(overheadGoal, 0.0001,
mtkleinf3723212014-06-25 14:08:00 -0700114 "Loop until timer overhead is at most this fraction of our measurments.");
Mike Klein84836b72019-03-21 11:31:36 -0500115static DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
Mike Klein5b3f3432019-03-21 11:42:21 -0500116static DEFINE_int(gpuFrameLag, 5,
Mike Klein84836b72019-03-21 11:31:36 -0500117 "If unknown, estimated maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -0700118
Mike Klein84836b72019-03-21 11:31:36 -0500119static DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
Mike Klein5b3f3432019-03-21 11:42:21 -0500120static DEFINE_int(maxCalibrationAttempts, 3,
mtklein55b0ffc2014-07-17 08:38:23 -0700121 "Try up to this many times to guess loops for a bench, or skip the bench.");
Mike Klein5b3f3432019-03-21 11:42:21 -0500122static DEFINE_int(maxLoops, 1000000, "Never run a bench more times than this.");
Mike Klein84836b72019-03-21 11:31:36 -0500123static DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
124static DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
125static DEFINE_string(zoom, "1.0,0",
126 "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
127 "function that ping-pongs between 1.0 and zoomMax.");
128static DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
Mike Klein84836b72019-03-21 11:31:36 -0500129static DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
Mike Klein5b3f3432019-03-21 11:42:21 -0500130static DEFINE_int(flushEvery, 10, "Flush --outResultsFile every Nth run.");
Mike Klein84836b72019-03-21 11:31:36 -0500131static DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
Chris Dalton5dfb3f42021-04-30 17:16:12 -0600132static DEFINE_bool(gpuStatsDump, false, "Dump GPU stats after each benchmark to json");
133static DEFINE_bool(dmsaaStatsDump, false, "Dump DMSAA stats after each benchmark to json");
Mike Klein84836b72019-03-21 11:31:36 -0500134static DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
135static DEFINE_bool(csv, false, "Print status in CSV format");
136static DEFINE_string(sourceType, "",
mtklein65dfd2f2016-02-03 10:40:54 -0800137 "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
Mike Klein84836b72019-03-21 11:31:36 -0500138static DEFINE_string(benchType, "",
139 "Apply usual --match rules to bench type: micro, recording, "
140 "piping, playback, skcodec, etc.");
mtklein65dfd2f2016-02-03 10:40:54 -0800141
Mike Klein84836b72019-03-21 11:31:36 -0500142static DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
Mike Klein813e8cc2020-08-05 09:33:38 -0500143static DEFINE_bool(skvm, false, "sets gUseSkVMBlitter");
Mike Kleinf2b3f912021-03-23 14:32:45 -0500144static DEFINE_bool(jit, true, "JIT SkVM?");
145static DEFINE_bool(dylib, false, "JIT via dylib (much slower compile but easier to debug/profile)");
Mike Reedc928fe22017-06-05 10:20:31 -0400146
Mike Klein19fb3972019-03-21 13:08:08 -0500147static DEFINE_bool2(pre_log, p, false,
148 "Log before running each test. May be incomprehensible when threading");
149
Leon Scroggins III1ff07062020-07-27 14:52:19 -0400150static DEFINE_bool(cpu, true, "Run CPU-bound work?");
151static DEFINE_bool(gpu, true, "Run GPU-bound work?");
Mike Klein629f5fc2019-03-22 14:55:19 -0500152static DEFINE_bool(dryRun, false,
153 "just print the tests that would be run, without actually running them.");
154static DEFINE_string(images, "",
155 "List of images and/or directories to decode. A directory with no images"
156 " is treated as a fatal error.");
157static DEFINE_bool(simpleCodec, false,
158 "Runs of a subset of the codec tests, always N32, Premul or Opaque");
159
Mike Kleinc6142d82019-03-25 10:54:59 -0500160static DEFINE_string2(match, m, nullptr,
161 "[~][^]substring[$] [...] of name to run.\n"
162 "Multiple matches may be separated by spaces.\n"
163 "~ causes a matching name to always be skipped\n"
164 "^ requires the start of the name to match\n"
165 "$ requires the end of the name to match\n"
166 "^ and $ requires an exact match\n"
167 "If a name does not match any list entry,\n"
168 "it is skipped unless some list entry starts with ~");
169
170static DEFINE_bool2(quiet, q, false, "if true, don't print status updates.");
171static DEFINE_bool2(verbose, v, false, "enable verbose output from the test driver.");
172
173
174static DEFINE_string(skps, "skps", "Directory to read skps from.");
Brian Salomon31fddc32021-04-30 13:08:55 -0400175static DEFINE_string(mskps, "mskps", "Directory to read mskps from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500176static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
Hal Canary5dfefa22019-10-24 13:52:17 -0400177static DEFINE_string(texttraces, "", "Directory to read TextBlobTrace files from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500178
179static DEFINE_int_2(threads, j, -1,
180 "Run threadsafe tests on a threadpool with this many extra threads, "
181 "defaulting to one extra thread per core.");
182
183static DEFINE_string2(writePath, w, "", "If set, write bitmaps here as .pngs.");
184
185static DEFINE_string(key, "",
186 "Space-separated key/value pairs to add to JSON identifying this builder.");
187static DEFINE_string(properties, "",
188 "Space-separated key/value pairs to add to JSON identifying this run.");
189
Mike Kleinee254232019-03-28 08:32:14 -0500190static DEFINE_bool(purgeBetweenBenches, false,
191 "Call SkGraphics::PurgeAllCaches() between each benchmark?");
192
mtkleinbbba1682015-10-28 11:36:30 -0700193static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
194
mtkleinf3723212014-06-25 14:08:00 -0700195static SkString humanize(double ms) {
Adlai Holler684838f2020-05-12 10:41:04 -0400196 if (FLAGS_verbose) return SkStringPrintf("%" PRIu64, (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800197 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700198}
mtklein55b0ffc2014-07-17 08:38:23 -0700199#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700200
tomhudsond968a6f2015-03-26 11:28:06 -0700201bool Target::init(SkImageInfo info, Benchmark* bench) {
202 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700203 this->surface = SkSurface::MakeRaster(info);
204 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700205 return false;
206 }
207 }
208 return true;
209}
210bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700211 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700212 if (!canvas) {
213 return false;
214 }
Mike Reed12e946b2017-04-17 10:53:29 -0400215 bmp->allocPixels(canvas->imageInfo());
216 if (!canvas->readPixels(*bmp, 0, 0)) {
tomhudsond968a6f2015-03-26 11:28:06 -0700217 SkDebugf("Can't read canvas pixels.\n");
218 return false;
219 }
220 return true;
221}
222
tomhudsond968a6f2015-03-26 11:28:06 -0700223struct GPUTarget : public Target {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400224 explicit GPUTarget(const Config& c) : Target(c) {}
225 ContextInfo contextInfo;
226 std::unique_ptr<GrContextFactory> factory;
tomhudsond968a6f2015-03-26 11:28:06 -0700227
Greg Daniel5ed3c112020-06-18 15:59:17 -0400228 ~GPUTarget() override {
229 // For Vulkan we need to release all our refs to the GrContext before destroy the vulkan
230 // context which happens at the end of this destructor. Thus we need to release the surface
231 // here which holds a ref to the GrContext.
232 surface.reset();
233 }
234
tomhudsond968a6f2015-03-26 11:28:06 -0700235 void setup() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400236 this->contextInfo.testContext()->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700237 // Make sure we're done with whatever came before.
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400238 this->contextInfo.testContext()->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700239 }
240 void endTiming() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400241 if (this->contextInfo.testContext()) {
Robert Phillips00f78de2020-07-01 16:09:43 -0400242 this->contextInfo.testContext()->flushAndWaitOnSync(contextInfo.directContext());
tomhudsond968a6f2015-03-26 11:28:06 -0700243 }
244 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400245 void fence() override { this->contextInfo.testContext()->finish(); }
mtkleind75c4662015-04-30 07:11:22 -0700246
cdaltond416a5b2015-06-23 13:23:44 -0700247 bool needsFrameTiming(int* maxFrameLag) const override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400248 if (!this->contextInfo.testContext()->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700249 // Frame lag is unknown.
250 *maxFrameLag = FLAGS_gpuFrameLag;
251 }
252 return true;
253 }
tomhudsond968a6f2015-03-26 11:28:06 -0700254 bool init(SkImageInfo info, Benchmark* bench) override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400255 GrContextOptions options = grContextOpts;
256 bench->modifyGrContextOptions(&options);
John Stilesfbd050b2020-08-03 13:21:46 -0400257 this->factory = std::make_unique<GrContextFactory>(options);
Chris Dalton180b4a12021-03-16 20:49:15 -0600258 SkSurfaceProps props(this->config.surfaceFlags, kRGB_H_SkPixelGeometry);
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400259 this->surface = SkSurface::MakeRenderTarget(
260 this->factory->get(this->config.ctxType, this->config.ctxOverrides),
261 SkBudgeted::kNo, info, this->config.samples, &props);
262 this->contextInfo =
263 this->factory->getContextInfo(this->config.ctxType, this->config.ctxOverrides);
John Stilesa008b0f2020-08-16 08:48:02 -0400264 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700265 return false;
266 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400267 if (!this->contextInfo.testContext()->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700268 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800269 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700270 }
tomhudsond968a6f2015-03-26 11:28:06 -0700271 return true;
272 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500273 void fillOptions(NanoJSONResultsWriter& log) override {
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400274#ifdef SK_GL
tomhudsond968a6f2015-03-26 11:28:06 -0700275 const GrGLubyte* version;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400276 if (this->contextInfo.backend() == GrBackendApi::kOpenGL) {
Brian Salomon032aaae2018-03-23 16:10:36 -0400277 const GrGLInterface* gl =
Robert Phillips00f78de2020-07-01 16:09:43 -0400278 static_cast<GrGLGpu*>(this->contextInfo.directContext()->priv().getGpu())
Brian Salomon032aaae2018-03-23 16:10:36 -0400279 ->glInterface();
bsalomonc8699322016-05-11 11:55:36 -0700280 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500281 log.appendString("GL_VERSION", (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_RENDERER));
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500284 log.appendString("GL_RENDERER", (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_VENDOR));
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500287 log.appendString("GL_VENDOR", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700288
bsalomonc8699322016-05-11 11:55:36 -0700289 GR_GL_CALL_RET(gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500290 log.appendString("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
bsalomonc8699322016-05-11 11:55:36 -0700291 }
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400292#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700293 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400294
295 void dumpStats() override {
Robert Phillips00f78de2020-07-01 16:09:43 -0400296 auto context = this->contextInfo.directContext();
297
298 context->priv().printCacheStats();
299 context->priv().printGpuStats();
300 context->priv().printContextStats();
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400301 }
tomhudsond968a6f2015-03-26 11:28:06 -0700302};
mtkleind75c4662015-04-30 07:11:22 -0700303
tomhudson75a0ebb2015-03-27 12:11:44 -0700304static double time(int loops, Benchmark* bench, Target* target) {
305 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700306 if (canvas) {
307 canvas->clear(SK_ColorWHITE);
308 }
joshualitt8a6697a2015-09-30 12:11:07 -0700309 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700310 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700311 canvas = target->beginTiming(canvas);
312 bench->draw(loops, canvas);
tomhudson75a0ebb2015-03-27 12:11:44 -0700313 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700314 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700315 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700316 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700317}
318
mtkleinf3723212014-06-25 14:08:00 -0700319static double estimate_timer_overhead() {
320 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700321 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700322 double start = now_ms();
323 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700324 }
325 return overhead / FLAGS_overheadLoops;
326}
327
reed53249782014-10-10 09:09:52 -0700328static int detect_forever_loops(int loops) {
329 // look for a magic run-forever value
330 if (loops < 0) {
331 loops = SK_MaxS32;
332 }
333 return loops;
334}
335
mtklein55b0ffc2014-07-17 08:38:23 -0700336static int clamp_loops(int loops) {
337 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800338 SkDebugf("ERROR: clamping loops from %d to 1. "
339 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700340 return 1;
341 }
342 if (loops > FLAGS_maxLoops) {
343 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
344 return FLAGS_maxLoops;
345 }
346 return loops;
347}
348
tomhudsond968a6f2015-03-26 11:28:06 -0700349static bool write_canvas_png(Target* target, const SkString& filename) {
350
bsalomon6eb03cc2014-08-07 14:28:50 -0700351 if (filename.isEmpty()) {
352 return false;
353 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700354 if (target->getCanvas() &&
355 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700356 return false;
357 }
tomhudsond968a6f2015-03-26 11:28:06 -0700358
bsalomon6eb03cc2014-08-07 14:28:50 -0700359 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700360
361 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700362 return false;
363 }
tomhudsond968a6f2015-03-26 11:28:06 -0700364
bsalomon6eb03cc2014-08-07 14:28:50 -0700365 SkString dir = SkOSPath::Dirname(filename.c_str());
366 if (!sk_mkdir(dir.c_str())) {
367 SkDebugf("Can't make dir %s.\n", dir.c_str());
368 return false;
369 }
370 SkFILEWStream stream(filename.c_str());
371 if (!stream.isValid()) {
372 SkDebugf("Can't write %s.\n", filename.c_str());
373 return false;
374 }
Hal Canarydb683012016-11-23 08:55:18 -0700375 if (!SkEncodeImage(&stream, bmp, SkEncodedImageFormat::kPNG, 100)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700376 SkDebugf("Can't encode a PNG.\n");
377 return false;
378 }
379 return true;
380}
381
382static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700383static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700384 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700385 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700386 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700387 int loops = bench->calculateLoops(FLAGS_loops);
388 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700389 while (bench_plus_overhead < overhead) {
390 if (round++ == FLAGS_maxCalibrationAttempts) {
391 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700392 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700393 return kFailedLoops;
394 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700395 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700396 }
mtklein2069e222014-08-04 13:57:39 -0700397 }
mtkleinf3723212014-06-25 14:08:00 -0700398
mtkleinbb6a0282014-07-01 08:43:42 -0700399 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700400 // We'll pick N to make timer overhead negligible:
401 //
mtkleinbb6a0282014-07-01 08:43:42 -0700402 // overhead
403 // ------------------------- < FLAGS_overheadGoal
404 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700405 //
Hal Canary55325b72017-01-03 10:36:17 -0500406 // where bench_plus_overhead ~=~ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700407 //
408 // Doing some math, we get:
409 //
mtkleinbb6a0282014-07-01 08:43:42 -0700410 // (overhead / FLAGS_overheadGoal) - overhead
411 // ------------------------------------------ < N
412 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700413 //
414 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700415 if (kAutoTuneLoops == loops) {
416 const double numer = overhead / FLAGS_overheadGoal - overhead;
417 const double denom = bench_plus_overhead - overhead;
418 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700419 loops = clamp_loops(loops);
420 } else {
421 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700422 }
mtkleinbb6a0282014-07-01 08:43:42 -0700423
mtkleinbb6a0282014-07-01 08:43:42 -0700424 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700425}
426
cdaltone1b89582015-06-25 19:17:08 -0700427static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700428 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700429 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700430 if (kAutoTuneLoops == loops) {
431 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700432 double elapsed = 0;
433 do {
mtklein527930f2014-11-06 08:04:34 -0800434 if (1<<30 == loops) {
435 // We're about to wrap. Something's wrong with the bench.
436 loops = 0;
437 break;
438 }
mtkleina189ccd2014-07-14 12:28:47 -0700439 loops *= 2;
440 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700441 // _this_ round, not still timing last round.
442 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700443 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700444 }
445 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700446
mtkleina189ccd2014-07-14 12:28:47 -0700447 // We've overshot at least a little. Scale back linearly.
448 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700449 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700450
tomhudsond968a6f2015-03-26 11:28:06 -0700451 // Make sure we're not still timing our calibration.
452 target->fence();
reed53249782014-10-10 09:09:52 -0700453 } else {
454 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700455 }
mtkleinbb6a0282014-07-01 08:43:42 -0700456 // Pretty much the same deal as the calibration: do some warmup to make
457 // sure we're timing steady-state pipelined frames.
Brian Osman01776982017-10-10 15:41:03 -0400458 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700459 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700460 }
mtkleinbb6a0282014-07-01 08:43:42 -0700461
mtkleinbb6a0282014-07-01 08:43:42 -0700462 return loops;
463}
mtkleinbb6a0282014-07-01 08:43:42 -0700464
Brian Salomon6405e712017-03-20 08:54:16 -0400465#define kBogusContextType GrContextFactory::kGL_ContextType
csmartdaltone812d492017-02-21 12:36:05 -0700466#define kBogusContextOverrides GrContextFactory::ContextOverrides::kNone
bsalomonc2553372014-07-22 13:09:05 -0700467
svaisanenc47635e2016-01-28 06:05:43 -0800468static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
svaisanenc47635e2016-01-28 06:05:43 -0800469 if (const auto* gpuConfig = config->asConfigGpu()) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400470 if (!FLAGS_gpu) {
471 SkDebugf("Skipping config '%s' as requested.\n", config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800472 return;
Ben Wagner32fa5102017-08-10 21:25:55 -0400473 }
svaisanenc47635e2016-01-28 06:05:43 -0800474
svaisanenc47635e2016-01-28 06:05:43 -0800475 const auto ctxType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700476 const auto ctxOverrides = gpuConfig->getContextOverrides();
svaisanenc47635e2016-01-28 06:05:43 -0800477 const auto sampleCount = gpuConfig->getSamples();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400478 const auto colorType = gpuConfig->getColorType();
479 auto colorSpace = gpuConfig->getColorSpace();
Brian Salomonf865b052018-03-09 09:01:53 -0500480 if (gpuConfig->getSurfType() != SkCommandLineConfigGpu::SurfType::kDefault) {
481 SkDebugf("This tool only supports the default surface type.");
482 return;
483 }
svaisanenc47635e2016-01-28 06:05:43 -0800484
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400485 GrContextFactory factory(grContextOpts);
Robert Phillipsf0288102020-07-06 13:45:34 -0400486 if (const auto ctx = factory.get(ctxType, ctxOverrides)) {
Greg Daniel6fa62e22019-08-07 15:52:37 -0400487 GrBackendFormat format = ctx->defaultBackendFormat(colorType, GrRenderable::kYes);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500488 int supportedSampleCount =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400489 ctx->priv().caps()->getRenderTargetSampleCount(sampleCount, format);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400490 if (sampleCount != supportedSampleCount) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400491 SkDebugf("Configuration '%s' sample count %d is not a supported sample count.\n",
492 config->getTag().c_str(), sampleCount);
svaisanenc47635e2016-01-28 06:05:43 -0800493 return;
494 }
495 } else {
Ben Wagner32fa5102017-08-10 21:25:55 -0400496 SkDebugf("No context was available matching config '%s'.\n",
497 config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800498 return;
499 }
500
501 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800502 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800503 Benchmark::kGPU_Backend,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400504 colorType,
svaisanenc47635e2016-01-28 06:05:43 -0800505 kPremul_SkAlphaType,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400506 sk_ref_sp(colorSpace),
svaisanenc47635e2016-01-28 06:05:43 -0800507 sampleCount,
508 ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700509 ctxOverrides,
Chris Dalton180b4a12021-03-16 20:49:15 -0600510 gpuConfig->getSurfaceFlags()
kkinnunend4e1c352016-03-01 23:41:26 -0800511 };
svaisanenc47635e2016-01-28 06:05:43 -0800512
513 configs->push_back(target);
514 return;
515 }
svaisanenc47635e2016-01-28 06:05:43 -0800516
brianosmanb109b8c2016-06-16 13:03:24 -0700517 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
518 if (config->getTag().equals(#name)) { \
Ben Wagner32fa5102017-08-10 21:25:55 -0400519 if (!FLAGS_cpu) { \
520 SkDebugf("Skipping config '%s' as requested.\n", \
521 config->getTag().c_str()); \
522 return; \
523 } \
brianosmanb109b8c2016-06-16 13:03:24 -0700524 Config config = { \
525 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
Chris Dalton94df5722021-04-19 17:40:40 -0600526 0, kBogusContextType, kBogusContextOverrides, 0 \
brianosmanb109b8c2016-06-16 13:03:24 -0700527 }; \
528 configs->push_back(config); \
529 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700530 }
mtkleine714e752014-07-31 12:13:48 -0700531
Ben Wagner32fa5102017-08-10 21:25:55 -0400532 CPU_CONFIG(nonrendering, kNonRendering_Backend,
533 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800534
Mike Klein0e4041f2018-06-19 16:00:40 -0400535 CPU_CONFIG(a8, kRaster_Backend, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr)
536 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType, nullptr)
537 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
538
539 // 'narrow' has a gamut narrower than sRGB, and different transfer function.
Brian Osman82ebe042019-01-04 17:03:00 -0500540 auto narrow = SkColorSpace::MakeRGB(SkNamedTransferFn::k2Dot2, gNarrow_toXYZD50),
Mike Klein0e4041f2018-06-19 16:00:40 -0400541 srgb = SkColorSpace::MakeSRGB(),
542 srgbLinear = SkColorSpace::MakeSRGBLinear();
543
544 CPU_CONFIG( f16, kRaster_Backend, kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbLinear)
545 CPU_CONFIG( srgb, kRaster_Backend, kRGBA_8888_SkColorType, kPremul_SkAlphaType, srgb )
546 CPU_CONFIG( esrgb, kRaster_Backend, kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgb )
547 CPU_CONFIG( narrow, kRaster_Backend, kRGBA_8888_SkColorType, kPremul_SkAlphaType, narrow )
548 CPU_CONFIG(enarrow, kRaster_Backend, kRGBA_F16_SkColorType, kPremul_SkAlphaType, narrow )
mtkleinbb6a0282014-07-01 08:43:42 -0700549
svaisanenc47635e2016-01-28 06:05:43 -0800550 #undef CPU_CONFIG
Ben Wagner32fa5102017-08-10 21:25:55 -0400551
552 SkDebugf("Unknown config '%s'.\n", config->getTag().c_str());
mtkleinf3723212014-06-25 14:08:00 -0700553}
554
svaisanenc47635e2016-01-28 06:05:43 -0800555// Append all configs that are enabled and supported.
556void create_configs(SkTArray<Config>* configs) {
557 SkCommandLineConfigArray array;
558 ParseConfigs(FLAGS_config, &array);
559 for (int i = 0; i < array.count(); ++i) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400560 create_config(array[i].get(), configs);
svaisanenc47635e2016-01-28 06:05:43 -0800561 }
Ben Wagner32fa5102017-08-10 21:25:55 -0400562
563 // If no just default configs were requested, then we're okay.
564 if (array.count() == 0 || FLAGS_config.count() == 0 ||
Ben Wagner32fa5102017-08-10 21:25:55 -0400565 // Otherwise, make sure that all specified configs have been created.
566 array.count() == configs->count()) {
567 return;
568 }
Ben Wagner32fa5102017-08-10 21:25:55 -0400569 exit(1);
svaisanenc47635e2016-01-28 06:05:43 -0800570}
571
brianosman9f1f6e22016-09-15 08:33:02 -0700572// disable warning : switch statement contains default but no 'case' labels
573#if defined _WIN32
574#pragma warning ( push )
575#pragma warning ( disable : 4065 )
576#endif
577
halcanary96fcdcc2015-08-27 07:41:13 -0700578// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700579static Target* is_enabled(Benchmark* bench, const Config& config) {
580 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700581 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700582 }
583
reede5ea5002014-09-03 11:54:58 -0700584 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700585 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700586
halcanary96fcdcc2015-08-27 07:41:13 -0700587 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700588
tomhudsond968a6f2015-03-26 11:28:06 -0700589 switch (config.backend) {
tomhudsond968a6f2015-03-26 11:28:06 -0700590 case Benchmark::kGPU_Backend:
591 target = new GPUTarget(config);
592 break;
tomhudsond968a6f2015-03-26 11:28:06 -0700593 default:
594 target = new Target(config);
595 break;
596 }
bsalomonc2553372014-07-22 13:09:05 -0700597
tomhudsond968a6f2015-03-26 11:28:06 -0700598 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700599 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700600 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700601 }
602 return target;
603}
604
brianosman9f1f6e22016-09-15 08:33:02 -0700605#if defined _WIN32
606#pragma warning ( pop )
607#endif
608
Leon Scroggins III87caae62020-05-04 10:02:45 -0400609#ifdef SK_ENABLE_ANDROID_UTILS
reed42943c82016-09-12 12:01:44 -0700610static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700611 uint32_t minOutputSize, int* width, int* height) {
Leon Scroggins III87caae62020-05-04 10:02:45 -0400612 auto brd = android::skia::BitmapRegionDecoder::Make(encoded);
John Stilesa008b0f2020-08-16 08:48:02 -0400613 if (nullptr == brd) {
msarett7f691442015-09-22 11:56:16 -0700614 // This is indicates that subset decoding is not supported for a particular image format.
615 return false;
616 }
617
msarett7f691442015-09-22 11:56:16 -0700618 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
619 (uint32_t) brd->height()) {
620 // This indicates that the image is not large enough to decode a
621 // minOutputSize x minOutputSize subset at the given sampleSize.
622 return false;
623 }
624
625 // Set the image width and height. The calling code will use this to choose subsets to decode.
626 *width = brd->width();
627 *height = brd->height();
628 return true;
629}
Leon Scroggins III87caae62020-05-04 10:02:45 -0400630#endif
msarett7f691442015-09-22 11:56:16 -0700631
egdaniel3bf92062015-06-26 08:12:46 -0700632static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700633 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700634}
635
Mike Klein88544fb2019-03-20 10:50:33 -0500636static void collect_files(const CommandLineFlags::StringArray& paths,
637 const char* ext,
638 SkTArray<SkString>* list) {
fmalita6519c212016-09-14 08:05:17 -0700639 for (int i = 0; i < paths.count(); ++i) {
640 if (SkStrEndsWith(paths[i], ext)) {
641 list->push_back(SkString(paths[i]));
642 } else {
643 SkOSFile::Iter it(paths[i], ext);
644 SkString path;
645 while (it.next(&path)) {
646 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
647 }
648 }
649 }
650}
651
mtkleine714e752014-07-31 12:13:48 -0700652class BenchmarkStream {
653public:
mtklein92007582014-08-01 07:46:52 -0700654 BenchmarkStream() : fBenches(BenchRegistry::Head())
Hal Canary2362ae72019-10-24 13:40:40 -0400655 , fGMs(skiagm::GMRegistry::Head()) {
fmalita6519c212016-09-14 08:05:17 -0700656 collect_files(FLAGS_skps, ".skp", &fSKPs);
Brian Salomon31fddc32021-04-30 13:08:55 -0400657 collect_files(FLAGS_mskps, ".mskp", &fMSKPs);
fmalita6519c212016-09-14 08:05:17 -0700658 collect_files(FLAGS_svgs, ".svg", &fSVGs);
Hal Canary5dfefa22019-10-24 13:52:17 -0400659 collect_files(FLAGS_texttraces, ".trace", &fTextBlobTraces);
mtkleine714e752014-07-31 12:13:48 -0700660
mtklein92007582014-08-01 07:46:52 -0700661 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
662 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
663 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
664 exit(1);
665 }
666
667 for (int i = 0; i < FLAGS_scales.count(); i++) {
668 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
669 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
670 exit(1);
671 }
672 }
robertphillips5b693772014-11-21 06:19:36 -0800673
cdalton63a82852015-06-29 14:06:10 -0700674 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
675 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700676 exit(1);
677 }
678
msarett95f192d2015-02-13 09:05:41 -0800679 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700680 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800681 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800682 }
mtklein95553d92015-03-12 08:24:21 -0700683
msarett95f192d2015-02-13 09:05:41 -0800684 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700685 fColorTypes.push_back(kN32_SkColorType);
686 if (!FLAGS_simpleCodec) {
687 fColorTypes.push_back(kRGB_565_SkColorType);
688 fColorTypes.push_back(kAlpha_8_SkColorType);
msarett67cb6662016-06-21 08:49:26 -0700689 fColorTypes.push_back(kGray_8_SkColorType);
690 }
mtklein92007582014-08-01 07:46:52 -0700691 }
692
reedca2622b2016-03-18 07:25:55 -0700693 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700694 // Not strictly necessary, as it will be checked again later,
695 // but helps to avoid a lot of pointless work if we're going to skip it.
Mike Klein88544fb2019-03-20 10:50:33 -0500696 if (CommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700697 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700698 }
699
bungemanf93d7112016-09-16 06:24:20 -0700700 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
701 if (!stream) {
mtkleinfd731ce2014-09-10 12:19:30 -0700702 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700703 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700704 }
705
reedca2622b2016-03-18 07:25:55 -0700706 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700707 }
708
Brian Salomon31fddc32021-04-30 13:08:55 -0400709 static std::unique_ptr<MSKPPlayer> ReadMSKP(const char* path) {
710 // Not strictly necessary, as it will be checked again later,
711 // but helps to avoid a lot of pointless work if we're going to skip it.
712 if (CommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
713 return nullptr;
714 }
715
716 std::unique_ptr<SkStreamSeekable> stream = SkStream::MakeFromFile(path);
717 if (!stream) {
718 SkDebugf("Could not read %s.\n", path);
719 return nullptr;
720 }
721
722 return MSKPPlayer::Make(stream.get());
723 }
724
fmalita6519c212016-09-14 08:05:17 -0700725 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
Mike Klein8ab65bf2020-12-04 09:20:32 -0600726 if (CommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
727 return nullptr;
728 }
Brian Osman133823d2018-09-19 14:14:15 -0400729 sk_sp<SkData> data(SkData::MakeFromFileName(path));
730 if (!data) {
fmalita6519c212016-09-14 08:05:17 -0700731 SkDebugf("Could not read %s.\n", path);
732 return nullptr;
733 }
734
Hal Canary0f666812018-03-22 15:21:12 -0400735#ifdef SK_XML
Brian Osman133823d2018-09-19 14:14:15 -0400736 SkMemoryStream stream(std::move(data));
fmalitae1baa7c2016-09-14 12:04:30 -0700737 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
fmalita6519c212016-09-14 08:05:17 -0700738 if (!svgDom) {
739 SkDebugf("Could not parse %s.\n", path);
740 return nullptr;
741 }
742
fmalitae1baa7c2016-09-14 12:04:30 -0700743 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
744 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
745 if (svgDom->containerSize().isEmpty()) {
746 svgDom->setContainerSize(kDefaultContainerSize);
747 }
748
fmalita6519c212016-09-14 08:05:17 -0700749 SkPictureRecorder recorder;
fmalitae1baa7c2016-09-14 12:04:30 -0700750 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
751 svgDom->containerSize().height()));
fmalita6519c212016-09-14 08:05:17 -0700752 return recorder.finishRecordingAsPicture();
Hal Canary0f666812018-03-22 15:21:12 -0400753#else
754 return nullptr;
755#endif // SK_XML
fmalita6519c212016-09-14 08:05:17 -0700756 }
757
mtklein92007582014-08-01 07:46:52 -0700758 Benchmark* next() {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400759 std::unique_ptr<Benchmark> bench;
mtklein65dfd2f2016-02-03 10:40:54 -0800760 do {
761 bench.reset(this->rawNext());
762 if (!bench) {
763 return nullptr;
764 }
Mike Klein88544fb2019-03-20 10:50:33 -0500765 } while (CommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
766 CommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700767 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800768 }
769
770 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700771 if (fBenches) {
Hal Canary972eba32018-07-30 17:07:07 -0400772 Benchmark* bench = fBenches->get()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700773 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700774 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700775 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700776 return bench;
777 }
mtklein92007582014-08-01 07:46:52 -0700778
mtkleine714e752014-07-31 12:13:48 -0700779 while (fGMs) {
Ben Wagner406ff502019-08-12 16:39:24 -0400780 std::unique_ptr<skiagm::GM> gm = fGMs->get()();
mtkleine714e752014-07-31 12:13:48 -0700781 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800782 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700783 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700784 fBenchType = "micro";
Ben Wagner406ff502019-08-12 16:39:24 -0400785 return new GMBench(std::move(gm));
mtkleine714e752014-07-31 12:13:48 -0700786 }
787 }
mtklein92007582014-08-01 07:46:52 -0700788
Hal Canary5dfefa22019-10-24 13:52:17 -0400789 while (fCurrentTextBlobTrace < fTextBlobTraces.count()) {
790 SkString path = fTextBlobTraces[fCurrentTextBlobTrace++];
791 SkString basename = SkOSPath::Basename(path.c_str());
792 static constexpr char kEnding[] = ".trace";
793 if (basename.endsWith(kEnding)) {
794 basename.remove(basename.size() - strlen(kEnding), strlen(kEnding));
795 }
796 fSourceType = "texttrace";
797 fBenchType = "micro";
798 return CreateDiffCanvasBench(
799 SkStringPrintf("SkDiffBench-%s", basename.c_str()),
800 [path](){ return SkStream::MakeFromFile(path.c_str()); });
801 }
802
mtkleinfd731ce2014-09-10 12:19:30 -0700803 // First add all .skps as RecordingBenches.
804 while (fCurrentRecording < fSKPs.count()) {
805 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700806 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
807 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700808 continue;
809 }
810 SkString name = SkOSPath::Basename(path.c_str());
811 fSourceType = "skp";
812 fBenchType = "recording";
Mike Klein88d90712018-01-27 17:30:04 +0000813 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
814 fSKPOps = pic->approximateOpCount();
Mike Kleina705cb92019-05-14 12:33:40 -0500815 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700816 }
817
Mike Reede45ff462017-12-06 10:47:03 -0500818 // Add all .skps as DeserializePictureBenchs.
819 while (fCurrentDeserialPicture < fSKPs.count()) {
820 const SkString& path = fSKPs[fCurrentDeserialPicture++];
821 sk_sp<SkData> data = SkData::MakeFromFileName(path.c_str());
822 if (!data) {
823 continue;
824 }
825 SkString name = SkOSPath::Basename(path.c_str());
826 fSourceType = "skp";
827 fBenchType = "deserial";
Mike Reed7557bbb2017-12-24 19:50:57 -0500828 fSKPBytes = static_cast<double>(data->size());
Mike Klein88d90712018-01-27 17:30:04 +0000829 fSKPOps = 0;
Mike Reede45ff462017-12-06 10:47:03 -0500830 return new DeserializePictureBench(name.c_str(), std::move(data));
831 }
832
mtkleinfd731ce2014-09-10 12:19:30 -0700833 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700834 while (fCurrentScale < fScales.count()) {
835 while (fCurrentSKP < fSKPs.count()) {
Brian Salomon626349b2020-08-27 10:54:36 -0400836 const SkString& path = fSKPs[fCurrentSKP++];
reedca2622b2016-03-18 07:25:55 -0700837 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
838 if (!pic) {
mtklein92007582014-08-01 07:46:52 -0700839 continue;
840 }
robertphillips5b693772014-11-21 06:19:36 -0800841
Brian Salomon626349b2020-08-27 10:54:36 -0400842 if (FLAGS_bbh) {
843 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
844 SkRTreeFactory factory;
845 SkPictureRecorder recorder;
846 pic->playback(recorder.beginRecording(pic->cullRect().width(),
847 pic->cullRect().height(),
848 &factory));
849 pic = recorder.finishRecordingAsPicture();
mtklein20840502014-08-21 15:51:22 -0700850 }
Brian Salomon626349b2020-08-27 10:54:36 -0400851 SkString name = SkOSPath::Basename(path.c_str());
852 fSourceType = "skp";
853 fBenchType = "playback";
854 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
855 FLAGS_loopSKP);
mtklein92007582014-08-01 07:46:52 -0700856 }
fmalita6519c212016-09-14 08:05:17 -0700857
Brian Salomon626349b2020-08-27 10:54:36 -0400858 while (fCurrentSVG < fSVGs.count()) {
859 const char* path = fSVGs[fCurrentSVG++].c_str();
fmalita6519c212016-09-14 08:05:17 -0700860 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
861 fSourceType = "svg";
862 fBenchType = "playback";
863 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
Brian Salomon626349b2020-08-27 10:54:36 -0400864 fScales[fCurrentScale], FLAGS_loopSKP);
fmalita6519c212016-09-14 08:05:17 -0700865 }
866 }
867
mtklein92007582014-08-01 07:46:52 -0700868 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700869 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700870 fCurrentScale++;
871 }
872
joshualitt261c3ad2015-04-27 09:16:57 -0700873 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700874 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700875 while (fCurrentAnimSKP < fSKPs.count()) {
876 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700877 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
878 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700879 fCurrentAnimSKP++;
880 continue;
881 }
882
883 fCurrentAnimSKP++;
884 SkString name = SkOSPath::Basename(path.c_str());
Ben Wagneracf98df2019-07-12 12:51:44 -0400885 sk_sp<SKPAnimationBench::Animation> animation =
886 SKPAnimationBench::MakeZoomAnimation(fZoomMax, fZoomPeriodMs);
887 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, std::move(animation),
halcanary385fe4d2015-08-26 13:07:48 -0700888 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700889 }
890 }
891
Brian Salomon31fddc32021-04-30 13:08:55 -0400892 // Read all MSKPs as benches
893 while (fCurrentMSKP < fMSKPs.count()) {
894 const SkString& path = fMSKPs[fCurrentMSKP++];
895 std::unique_ptr<MSKPPlayer> player = ReadMSKP(path.c_str());
896 if (!player) {
897 continue;
898 }
899 SkString name = SkOSPath::Basename(path.c_str());
900 fSourceType = "mskp";
901 fBenchType = "mskp";
902 return new MSKPBench(std::move(name), std::move(player));
903 }
904
scroggo60869a42015-04-01 12:09:17 -0700905 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700906 fSourceType = "image";
907 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700908 const SkString& path = fImages[fCurrentCodec];
Mike Klein88544fb2019-03-20 10:50:33 -0500909 if (CommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
mtklein6f0ff912016-01-11 09:04:21 -0800910 continue;
911 }
bungeman38d909e2016-08-02 14:40:46 -0700912 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400913 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700914 if (!codec) {
915 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700916 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700917 continue;
918 }
scroggo21027992015-04-02 13:22:38 -0700919
scroggo60869a42015-04-01 12:09:17 -0700920 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700921 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700922
msarettc7796b92016-01-07 14:20:20 -0800923 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700924 if (FLAGS_simpleCodec) {
925 if (kUnpremul_SkAlphaType == alphaType) {
926 alphaType = kPremul_SkAlphaType;
927 }
928
929 fCurrentColorType++;
930 } else {
931 switch (alphaType) {
932 case kOpaque_SkAlphaType:
933 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800934 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700935 break;
936 case kUnpremul_SkAlphaType:
937 case kPremul_SkAlphaType:
938 if (0 == fCurrentAlphaType) {
939 // Test unpremul first.
940 alphaType = kUnpremul_SkAlphaType;
941 fCurrentAlphaType++;
942 } else {
943 // Test premul.
944 alphaType = kPremul_SkAlphaType;
945 fCurrentAlphaType = 0;
946 fCurrentColorType++;
947 }
948 break;
949 default:
950 SkASSERT(false);
951 fCurrentColorType++;
952 break;
953 }
scroggo21027992015-04-02 13:22:38 -0700954 }
955
msarettc7796b92016-01-07 14:20:20 -0800956 // Make sure we can decode to this color type and alpha type.
957 SkImageInfo info =
958 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700959 const size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400960 SkAutoMalloc storage(info.computeByteSize(rowBytes));
scroggo21027992015-04-02 13:22:38 -0700961
scroggoeb602a52015-07-09 08:16:03 -0700962 const SkCodec::Result result = codec->getPixels(
Leon Scroggins571b30f2017-07-11 17:35:31 +0000963 info, storage.get(), rowBytes);
scroggo60869a42015-04-01 12:09:17 -0700964 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700965 case SkCodec::kSuccess:
966 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700967 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700968 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700969 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700970 // This is okay. Not all conversions are valid.
971 break;
scroggo60869a42015-04-01 12:09:17 -0700972 default:
973 // This represents some sort of failure.
974 SkASSERT(false);
975 break;
976 }
977 }
978 fCurrentColorType = 0;
979 }
980
msarett84451022016-02-11 06:45:51 -0800981 // Run AndroidCodecBenches
982 const int sampleSizes[] = { 2, 4, 8 };
983 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
984 fSourceType = "image";
985 fBenchType = "skandroidcodec";
986
987 const SkString& path = fImages[fCurrentAndroidCodec];
Mike Klein88544fb2019-03-20 10:50:33 -0500988 if (CommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
msarett84451022016-02-11 06:45:51 -0800989 continue;
990 }
bungeman38d909e2016-08-02 14:40:46 -0700991 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400992 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800993 if (!codec) {
994 // Nothing to time.
995 SkDebugf("Cannot find codec for %s\n", path.c_str());
996 continue;
997 }
998
999 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
1000 int sampleSize = sampleSizes[fCurrentSampleSize];
1001 fCurrentSampleSize++;
Brian Osman788b9162020-02-07 10:36:46 -05001002 if (10 * sampleSize > std::min(codec->getInfo().width(), codec->getInfo().height())) {
msarett84451022016-02-11 06:45:51 -08001003 // Avoid benchmarking scaled decodes of already small images.
1004 break;
1005 }
1006
bungeman38d909e2016-08-02 14:40:46 -07001007 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
1008 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -08001009 }
1010 fCurrentSampleSize = 0;
1011 }
1012
Leon Scroggins III87caae62020-05-04 10:02:45 -04001013#ifdef SK_ENABLE_ANDROID_UTILS
msarett7f691442015-09-22 11:56:16 -07001014 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -07001015 // We intend to create benchmarks that model the use cases in
1016 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
1017 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
1018 // the image. For that reason, we will benchmark decodes in five representative locations
1019 // in the image. Additionally, this use case utilizes power of two scaling, so we will
1020 // test on power of two sample sizes. The output tile is always 512x512, so, when a
1021 // sampleSize is used, the size of the subset that is decoded is always
1022 // (sampleSize*512)x(sampleSize*512).
1023 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -07001024 // All use cases we are aware of only scale by powers of two.
1025 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
1026 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -08001027 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -07001028 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -08001029 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -07001030 fSourceType = "image";
1031 fBenchType = "BRD";
1032
mtklein6f0ff912016-01-11 09:04:21 -08001033 const SkString& path = fImages[fCurrentBRDImage];
Mike Klein88544fb2019-03-20 10:50:33 -05001034 if (CommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
mtklein6f0ff912016-01-11 09:04:21 -08001035 continue;
1036 }
scroggo860e8a62015-10-15 07:51:28 -07001037
msarettd1227a72016-05-18 06:23:57 -07001038 while (fCurrentColorType < fColorTypes.count()) {
1039 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
1040 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -07001041
bungeman38d909e2016-08-02 14:40:46 -07001042 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -07001043 const SkColorType colorType = fColorTypes[fCurrentColorType];
1044 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
1045 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -07001046
msarettd1227a72016-05-18 06:23:57 -07001047 int width = 0;
1048 int height = 0;
reed42943c82016-09-12 12:01:44 -07001049 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -07001050 &width, &height)) {
1051 break;
msarett7f691442015-09-22 11:56:16 -07001052 }
msarettd1227a72016-05-18 06:23:57 -07001053
1054 SkString basename = SkOSPath::Basename(path.c_str());
1055 SkIRect subset;
1056 const uint32_t subsetSize = sampleSize * minOutputSize;
1057 switch (currentSubsetType) {
1058 case kTopLeft_SubsetType:
1059 basename.append("_TopLeft");
1060 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
1061 break;
1062 case kTopRight_SubsetType:
1063 basename.append("_TopRight");
1064 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
1065 subsetSize);
1066 break;
1067 case kMiddle_SubsetType:
1068 basename.append("_Middle");
1069 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
1070 (height - subsetSize) / 2, subsetSize, subsetSize);
1071 break;
1072 case kBottomLeft_SubsetType:
1073 basename.append("_BottomLeft");
1074 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
1075 subsetSize);
1076 break;
1077 case kBottomRight_SubsetType:
1078 basename.append("_BottomRight");
1079 subset = SkIRect::MakeXYWH(width - subsetSize,
1080 height - subsetSize, subsetSize, subsetSize);
1081 break;
1082 default:
1083 SkASSERT(false);
1084 }
1085
1086 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
1087 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -07001088 }
msarettd1227a72016-05-18 06:23:57 -07001089 fCurrentSubsetType = 0;
1090 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -07001091 }
msarettd1227a72016-05-18 06:23:57 -07001092 fCurrentSampleSize = 0;
1093 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -07001094 }
msarettd1227a72016-05-18 06:23:57 -07001095 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -07001096 }
Leon Scroggins III87caae62020-05-04 10:02:45 -04001097#endif // SK_ENABLE_ANDROID_UTILS
msarett7f691442015-09-22 11:56:16 -07001098
halcanary96fcdcc2015-08-27 07:41:13 -07001099 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001100 }
mtklein92007582014-08-01 07:46:52 -07001101
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001102 void fillCurrentOptions(NanoJSONResultsWriter& log) const {
1103 log.appendString("source_type", fSourceType);
1104 log.appendString("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001105 if (0 == strcmp(fSourceType, "skp")) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001106 log.appendString("clip",
mtklein92007582014-08-01 07:46:52 -07001107 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1108 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001109 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001110 log.appendString("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
mtklein92007582014-08-01 07:46:52 -07001111 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001112 }
1113
1114 void fillCurrentMetrics(NanoJSONResultsWriter& log) const {
mtklein051e56d2014-12-04 08:46:51 -08001115 if (0 == strcmp(fBenchType, "recording")) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001116 log.appendMetric("bytes", fSKPBytes);
1117 log.appendMetric("ops", fSKPOps);
mtklein051e56d2014-12-04 08:46:51 -08001118 }
mtklein92007582014-08-01 07:46:52 -07001119 }
1120
mtkleine714e752014-07-31 12:13:48 -07001121private:
Leon Scroggins III87caae62020-05-04 10:02:45 -04001122#ifdef SK_ENABLE_ANDROID_UTILS
msarettb23e6aa2015-06-09 13:56:10 -07001123 enum SubsetType {
1124 kTopLeft_SubsetType = 0,
1125 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001126 kMiddle_SubsetType = 2,
1127 kBottomLeft_SubsetType = 3,
1128 kBottomRight_SubsetType = 4,
1129 kTranslate_SubsetType = 5,
1130 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001131 kLast_SubsetType = kZoom_SubsetType,
1132 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001133 };
Leon Scroggins III87caae62020-05-04 10:02:45 -04001134#endif
msarettb23e6aa2015-06-09 13:56:10 -07001135
mtkleine714e752014-07-31 12:13:48 -07001136 const BenchRegistry* fBenches;
1137 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001138 SkIRect fClip;
1139 SkTArray<SkScalar> fScales;
1140 SkTArray<SkString> fSKPs;
Brian Salomon31fddc32021-04-30 13:08:55 -04001141 SkTArray<SkString> fMSKPs;
fmalita6519c212016-09-14 08:05:17 -07001142 SkTArray<SkString> fSVGs;
Hal Canary5dfefa22019-10-24 13:52:17 -04001143 SkTArray<SkString> fTextBlobTraces;
msarett95f192d2015-02-13 09:05:41 -08001144 SkTArray<SkString> fImages;
msarett74deb982015-10-20 16:45:56 -07001145 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001146 SkScalar fZoomMax;
1147 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001148
Mike Klein88d90712018-01-27 17:30:04 +00001149 double fSKPBytes, fSKPOps;
mtklein051e56d2014-12-04 08:46:51 -08001150
mtkleinfd731ce2014-09-10 12:19:30 -07001151 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1152 const char* fBenchType; // How we bench it: micro, recording, playback, ...
Hal Canary2362ae72019-10-24 13:40:40 -04001153 int fCurrentRecording = 0;
1154 int fCurrentDeserialPicture = 0;
Brian Salomon31fddc32021-04-30 13:08:55 -04001155 int fCurrentMSKP = 0;
Hal Canary2362ae72019-10-24 13:40:40 -04001156 int fCurrentScale = 0;
1157 int fCurrentSKP = 0;
1158 int fCurrentSVG = 0;
Hal Canary5dfefa22019-10-24 13:52:17 -04001159 int fCurrentTextBlobTrace = 0;
Hal Canary2362ae72019-10-24 13:40:40 -04001160 int fCurrentCodec = 0;
1161 int fCurrentAndroidCodec = 0;
Leon Scroggins III87caae62020-05-04 10:02:45 -04001162#ifdef SK_ENABLE_ANDROID_UTILS
Hal Canary2362ae72019-10-24 13:40:40 -04001163 int fCurrentBRDImage = 0;
Leon Scroggins III87caae62020-05-04 10:02:45 -04001164 int fCurrentSubsetType = 0;
1165#endif
Hal Canary2362ae72019-10-24 13:40:40 -04001166 int fCurrentColorType = 0;
1167 int fCurrentAlphaType = 0;
Hal Canary2362ae72019-10-24 13:40:40 -04001168 int fCurrentSampleSize = 0;
1169 int fCurrentAnimSKP = 0;
mtkleine714e752014-07-31 12:13:48 -07001170};
1171
msarettc149f0e2016-01-04 11:35:43 -08001172// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1173// This prints something every once in a while so that it knows we're still working.
1174static void start_keepalive() {
Mike Klein03141d22017-10-30 11:57:15 -04001175 static std::thread* intentionallyLeaked = new std::thread([]{
1176 for (;;) {
1177 static const int kSec = 1200;
1178 #if defined(SK_BUILD_FOR_WIN)
1179 Sleep(kSec * 1000);
1180 #else
1181 sleep(kSec);
1182 #endif
1183 SkDebugf("\nBenchmarks still running...\n");
msarettc149f0e2016-01-04 11:35:43 -08001184 }
Mike Klein03141d22017-10-30 11:57:15 -04001185 });
1186 (void)intentionallyLeaked;
msarettc149f0e2016-01-04 11:35:43 -08001187}
1188
John Stiles2ef627a2021-03-23 10:25:02 -04001189class NanobenchShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
1190 void compileError(const char* shader, const char* errors) override {
1191 // Nanobench should abort if any shader can't compile. Failure is much better than
1192 // reporting meaningless performance metrics.
1193 SkSL::String message = GrShaderUtils::BuildShaderErrorMessage(shader, errors);
John Stiles345f1402021-03-23 10:25:15 -04001194 SK_ABORT("\n%s", message.c_str());
John Stiles2ef627a2021-03-23 10:25:02 -04001195 }
1196};
1197
Mike Kleinbe28ee22017-02-06 12:46:20 -05001198int main(int argc, char** argv) {
Mike Klein88544fb2019-03-20 10:50:33 -05001199 CommandLineFlags::Parse(argc, argv);
Brian Osman53136aa2017-07-20 15:43:35 -04001200
Brian Osmanbc8150f2017-07-24 11:38:01 -04001201 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -04001202
Mike Kleinadacaef2017-02-06 09:26:14 -05001203#if defined(SK_BUILD_FOR_IOS)
1204 cd_Documents();
1205#endif
jcgregorio3b27ade2014-11-13 08:06:40 -08001206 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001207 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001208 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001209
Chris Dalton040238b2017-12-18 14:22:34 -07001210 SetCtxOptionsFromCommonFlags(&grContextOpts);
krajcevski69a55602014-08-13 10:46:31 -07001211
John Stiles2ef627a2021-03-23 10:25:02 -04001212 NanobenchShaderErrorHandler errorHandler;
1213 grContextOpts.fShaderErrorHandler = &errorHandler;
1214
bsalomon6eb03cc2014-08-07 14:28:50 -07001215 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001216 FLAGS_samples = 1;
1217 FLAGS_gpuFrameLag = 0;
1218 }
1219
bsalomon6eb03cc2014-08-07 14:28:50 -07001220 if (!FLAGS_writePath.isEmpty()) {
1221 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1222 if (!sk_mkdir(FLAGS_writePath[0])) {
1223 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001224 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001225 }
1226 }
1227
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001228 std::unique_ptr<SkWStream> logStream(new SkNullWStream);
mtklein60317d0f2014-07-14 11:30:37 -07001229 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001230#if defined(SK_RELEASE)
Chris Dalton2e1381e2019-10-23 14:35:19 -06001231 // SkJSONWriter uses a 32k in-memory cache, so it only flushes occasionally and is well
1232 // equipped for a stream that re-opens, appends, and closes the file on every write.
1233 logStream.reset(new NanoFILEAppendAndCloseStream(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001234#else
1235 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1236 return 1;
1237#endif
mtklein60317d0f2014-07-14 11:30:37 -07001238 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001239 NanoJSONResultsWriter log(logStream.get(), SkJSONWriter::Mode::kPretty);
1240 log.beginObject(); // root
mtklein1915b622014-08-20 11:45:00 -07001241
1242 if (1 == FLAGS_properties.count() % 2) {
1243 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1244 return 1;
1245 }
1246 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001247 log.appendString(FLAGS_properties[i-1], FLAGS_properties[i]);
mtklein1915b622014-08-20 11:45:00 -07001248 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001249
1250 if (1 == FLAGS_key.count() % 2) {
1251 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1252 return 1;
1253 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001254 if (FLAGS_key.count()) {
1255 log.beginObject("key");
1256 for (int i = 1; i < FLAGS_key.count(); i += 2) {
1257 log.appendString(FLAGS_key[i - 1], FLAGS_key[i]);
1258 }
1259 log.endObject(); // key
mtklein94e51562014-08-19 12:41:53 -07001260 }
mtklein60317d0f2014-07-14 11:30:37 -07001261
mtkleinf3723212014-06-25 14:08:00 -07001262 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001263 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001264
cdaltone1b89582015-06-25 19:17:08 -07001265 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001266
bsalomon6eb03cc2014-08-07 14:28:50 -07001267 if (kAutoTuneLoops != FLAGS_loops) {
1268 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001269 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001270 SkDebugf("! -> high variance, ? -> moderate variance\n");
1271 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001272 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001273 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001274 } else {
mtkleind75c4662015-04-30 07:11:22 -07001275 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001276 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001277 }
1278
Chris Dalton5dfb3f42021-04-30 17:16:12 -06001279 GrRecordingContextPriv::DMSAAStats combinedDMSAAStats;
1280
svaisanenc47635e2016-01-28 06:05:43 -08001281 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001282 create_configs(&configs);
1283
msarettc149f0e2016-01-04 11:35:43 -08001284 if (FLAGS_keepAlive) {
1285 start_keepalive();
1286 }
1287
Mike Klein03fa5d42019-03-22 11:39:09 -05001288 SetAnalyticAAFromCommonFlags();
liyuqian38911a72016-10-04 11:23:22 -07001289
Mike Klein813e8cc2020-08-05 09:33:38 -05001290 gSkForceRasterPipelineBlitter = FLAGS_forceRasterPipeline;
1291 gUseSkVMBlitter = FLAGS_skvm;
Mike Kleinf2b3f912021-03-23 14:32:45 -05001292 gSkVMAllowJIT = FLAGS_jit;
1293 gSkVMJITViaDylib = FLAGS_dylib;
Yuqian Li550148b2017-01-13 10:13:13 -05001294
mtkleine070c2b2014-10-14 08:40:43 -07001295 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001296 BenchmarkStream benchStream;
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001297 log.beginObject("results");
Jim Van Verth8a9a3712019-05-31 10:49:12 -04001298 AutoreleasePool pool;
mtklein92007582014-08-01 07:46:52 -07001299 while (Benchmark* b = benchStream.next()) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001300 std::unique_ptr<Benchmark> bench(b);
Mike Klein88544fb2019-03-20 10:50:33 -05001301 if (CommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001302 continue;
1303 }
1304
svaisanenc47635e2016-01-28 06:05:43 -08001305 if (!configs.empty()) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001306 log.beginBench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001307 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001308 }
egdaniel3bf92062015-06-26 08:12:46 -07001309 for (int i = 0; i < configs.count(); ++i) {
1310 Target* target = is_enabled(b, configs[i]);
1311 if (!target) {
1312 continue;
1313 }
mtkleinf3723212014-06-25 14:08:00 -07001314
halcanary96fcdcc2015-08-27 07:41:13 -07001315 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001316 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001317 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001318
brianosman7a5ada82016-02-08 13:49:12 -08001319 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001320 SkDebugf("Running %s\t%s\n"
1321 , bench->getUniqueName()
1322 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001323 if (FLAGS_dryRun) {
1324 continue;
1325 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001326 }
1327
Mike Kleinee254232019-03-28 08:32:14 -05001328 if (FLAGS_purgeBetweenBenches) {
1329 SkGraphics::PurgeAllCaches();
1330 }
1331
Brian Osman5ad87aa2017-09-08 16:05:23 -04001332 TRACE_EVENT2("skia", "Benchmark", "name", TRACE_STR_COPY(bench->getUniqueName()),
1333 "config", TRACE_STR_COPY(config));
Brian Osmanc3cdef52017-08-31 14:09:22 -04001334
egdaniel3bf92062015-06-26 08:12:46 -07001335 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001336 bench->perCanvasPreDraw(canvas);
1337
cdaltone1b89582015-06-25 19:17:08 -07001338 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001339 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001340 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1341 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001342
Leon Scroggins IIIb30d1132017-10-02 09:48:15 -04001343 if (kFailedLoops == loops) {
1344 // Can't be timed. A warning note has already been printed.
1345 cleanup_run(target);
1346 continue;
1347 }
1348
Yuqian Li3528eb32017-09-21 13:23:49 -04001349 if (runs == 0 && FLAGS_ms < 1000) {
1350 // Run the first bench for 1000ms to warm up the nanobench if FLAGS_ms < 1000.
1351 // Otherwise, the first few benches' measurements will be inaccurate.
1352 auto stop = now_ms() + 1000;
1353 do {
1354 time(loops, bench.get(), target);
Jim Van Verth49d6bca2020-02-28 11:05:44 -05001355 pool.drain();
Yuqian Li3528eb32017-09-21 13:23:49 -04001356 } while (now_ms() < stop);
1357 }
1358
mtkleinbbba1682015-10-28 11:36:30 -07001359 if (FLAGS_ms) {
1360 samples.reset();
1361 auto stop = now_ms() + FLAGS_ms;
1362 do {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001363 samples.push_back(time(loops, bench.get(), target) / loops);
Jim Van Verth49d6bca2020-02-28 11:05:44 -05001364 pool.drain();
mtkleinbbba1682015-10-28 11:36:30 -07001365 } while (now_ms() < stop);
1366 } else {
cdaltone1b89582015-06-25 19:17:08 -07001367 samples.reset(FLAGS_samples);
1368 for (int s = 0; s < FLAGS_samples; s++) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001369 samples[s] = time(loops, bench.get(), target) / loops;
Jim Van Verth49d6bca2020-02-28 11:05:44 -05001370 pool.drain();
cdaltone1b89582015-06-25 19:17:08 -07001371 }
cdaltone1b89582015-06-25 19:17:08 -07001372 }
mtkleinf3723212014-06-25 14:08:00 -07001373
Mike Kleinfec9b902019-05-31 10:03:07 -05001374 // Scale each result to the benchmark's own units, time/unit.
1375 for (double& sample : samples) {
1376 sample *= (1.0 / bench->getUnits());
1377 }
1378
joshualitte45c81c2015-12-02 09:05:37 -08001379 SkTArray<SkString> keys;
1380 SkTArray<double> values;
Chris Dalton5dfb3f42021-04-30 17:16:12 -06001381 if (configs[i].backend == Benchmark::kGPU_Backend) {
1382 if (FLAGS_gpuStatsDump) {
1383 // TODO cache stats
1384 bench->getGpuStats(canvas, &keys, &values);
1385 }
1386 if (FLAGS_dmsaaStatsDump && bench->getDMSAAStats(canvas->recordingContext())) {
1387 const auto& dmsaaStats = canvas->recordingContext()->priv().dmsaaStats();
1388 dmsaaStats.dumpKeyValuePairs(&keys, &values);
1389 dmsaaStats.dump();
1390 combinedDMSAAStats.merge(dmsaaStats);
1391 }
joshualitte45c81c2015-12-02 09:05:37 -08001392 }
joshualitte45c81c2015-12-02 09:05:37 -08001393
robertphillips5b693772014-11-21 06:19:36 -08001394 bench->perCanvasPostDraw(canvas);
1395
egdaniel3bf92062015-06-26 08:12:46 -07001396 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001397 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001398 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001399 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001400 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001401 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001402 }
1403
Mike Klein512a2442018-09-27 10:44:56 -04001404 // Building stats.plot often shows up in profiles,
1405 // so skip building it when we're not going to print it anyway.
1406 const bool want_plot = !FLAGS_quiet;
1407
1408 Stats stats(samples, want_plot);
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001409 log.beginObject(config);
1410
1411 log.beginObject("options");
1412 log.appendString("name", bench->getName());
1413 benchStream.fillCurrentOptions(log);
1414 target->fillOptions(log);
1415 log.endObject(); // options
1416
1417 // Metrics
1418 log.appendMetric("min_ms", stats.min);
1419 log.beginArray("samples");
1420 for (double sample : samples) {
1421 log.appendDoubleDigits(sample, 16);
1422 }
1423 log.endArray(); // samples
1424 benchStream.fillCurrentMetrics(log);
Chris Dalton5dfb3f42021-04-30 17:16:12 -06001425 if (!keys.empty()) {
joshualitte45c81c2015-12-02 09:05:37 -08001426 // dump to json, only SKPBench currently returns valid keys / values
1427 SkASSERT(keys.count() == values.count());
1428 for (int i = 0; i < keys.count(); i++) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001429 log.appendMetric(keys[i].c_str(), values[i]);
joshualitte45c81c2015-12-02 09:05:37 -08001430 }
1431 }
joshualitte45c81c2015-12-02 09:05:37 -08001432
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001433 log.endObject(); // config
1434
mtkleine070c2b2014-10-14 08:40:43 -07001435 if (runs++ % FLAGS_flushEvery == 0) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001436 log.flush();
mtkleine070c2b2014-10-14 08:40:43 -07001437 }
mtklein60317d0f2014-07-14 11:30:37 -07001438
bsalomon6eb03cc2014-08-07 14:28:50 -07001439 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001440 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001441 config = ""; // Only print the config if we run the same bench on more than one.
1442 }
mtkleind75c4662015-04-30 07:11:22 -07001443 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1444 , sk_tools::getCurrResidentSetSizeMB()
1445 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001446 , bench->getUniqueName()
1447 , config);
mtkleinf3723212014-06-25 14:08:00 -07001448 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001449 const char* mark = " ";
Hal Canarye99e8da2019-01-14 16:34:50 -05001450 const double stddev_percent =
1451 sk_ieee_double_divide(100 * sqrt(stats.var), stats.mean);
mtklein66cfcff2015-12-04 06:35:30 -08001452 if (stddev_percent > 5) mark = "?";
1453 if (stddev_percent > 10) mark = "!";
1454
1455 SkDebugf("%10.2f %s\t%s\t%s\n",
1456 stats.median*1e3, mark, bench->getUniqueName(), config);
Mike Reedc665e5b2017-07-04 22:56:06 -04001457 } else if (FLAGS_csv) {
Hal Canarye99e8da2019-01-14 16:34:50 -05001458 const double stddev_percent =
1459 sk_ieee_double_divide(100 * sqrt(stats.var), stats.mean);
Mike Reedc665e5b2017-07-04 22:56:06 -04001460 SkDebugf("%g,%g,%g,%g,%g,%s,%s\n"
1461 , stats.min
1462 , stats.median
1463 , stats.mean
1464 , stats.max
1465 , stddev_percent
1466 , config
1467 , bench->getUniqueName()
1468 );
1469 } else {
1470 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 -05001471 const double stddev_percent =
1472 sk_ieee_double_divide(100 * sqrt(stats.var), stats.mean);
Mike Reedc665e5b2017-07-04 22:56:06 -04001473 SkDebugf(format
mtkleind75c4662015-04-30 07:11:22 -07001474 , sk_tools::getCurrResidentSetSizeMB()
1475 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001476 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001477 , HUMANIZE(stats.min)
1478 , HUMANIZE(stats.median)
1479 , HUMANIZE(stats.mean)
1480 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001481 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001482 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001483 , config
mtklein96289052014-09-10 12:05:59 -07001484 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001485 );
1486 }
joshualitte45c81c2015-12-02 09:05:37 -08001487
joshualitte45c81c2015-12-02 09:05:37 -08001488 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -04001489 target->dumpStats();
bsalomon06cddec2014-10-24 10:40:50 -07001490 }
joshualitte45c81c2015-12-02 09:05:37 -08001491
cdalton2c56ba52015-06-26 13:32:53 -07001492 if (FLAGS_verbose) {
1493 SkDebugf("Samples: ");
1494 for (int i = 0; i < samples.count(); i++) {
1495 SkDebugf("%s ", HUMANIZE(samples[i]));
1496 }
1497 SkDebugf("%s\n", bench->getUniqueName());
1498 }
egdaniel3bf92062015-06-26 08:12:46 -07001499 cleanup_run(target);
Jim Van Verth8a9a3712019-05-31 10:49:12 -04001500 pool.drain();
mtkleinf3723212014-06-25 14:08:00 -07001501 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001502 if (!configs.empty()) {
1503 log.endBench();
1504 }
mtkleinf3723212014-06-25 14:08:00 -07001505 }
1506
Chris Dalton5dfb3f42021-04-30 17:16:12 -06001507 if (FLAGS_dmsaaStatsDump) {
1508 SkDebugf("<<Total Combined DMSAA Stats>>\n");
1509 combinedDMSAAStats.dump();
1510 }
1511
Herb Derby5a523fe2017-01-26 16:48:28 -05001512 SkGraphics::PurgeAllCaches();
1513
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001514 log.beginBench("memory_usage", 0, 0);
1515 log.beginObject("meta"); // config
1516 log.appendS32("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1517 log.endObject(); // config
1518 log.endBench();
1519
Brian Osman68870aa2020-07-08 14:12:43 -04001520 RunSkSLMemoryBenchmarks(&log);
1521
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001522 log.endObject(); // results
1523 log.endObject(); // root
1524 log.flush();
mtkleine1091452014-12-04 10:47:02 -08001525
mtkleinf3723212014-06-25 14:08:00 -07001526 return 0;
1527}