blob: c486028efaec653425e5803fa92a5e8e11ba211a [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 Reed0e2fb662021-07-23 11:00:40 -040068extern bool gForceHighPrecisionRasterPipeline;
Mike Klein2a57e792020-01-14 12:47:52 -060069extern bool gUseSkVMBlitter;
Mike Klein813e8cc2020-08-05 09:33:38 -050070extern bool gSkVMAllowJIT;
Mike Kleinea4459d2020-01-16 13:05:04 -060071extern bool gSkVMJITViaDylib;
Mike Reedc928fe22017-06-05 10:20:31 -040072
Mike Klein8f11d4d2018-01-24 12:42:55 -050073#ifndef SK_BUILD_FOR_WIN
scroggo38ce0a72016-01-07 07:28:47 -080074 #include <unistd.h>
Brian Salomon032aaae2018-03-23 16:10:36 -040075
scroggo38ce0a72016-01-07 07:28:47 -080076#endif
77
Robert Phillips00f78de2020-07-01 16:09:43 -040078#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050079#include "src/gpu/GrCaps.h"
Adlai Hollera0693042020-10-14 11:23:11 -040080#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050081#include "src/gpu/SkGr.h"
82#include "src/gpu/gl/GrGLDefines.h"
83#include "src/gpu/gl/GrGLGpu.h"
84#include "src/gpu/gl/GrGLUtil.h"
85#include "tools/gpu/GrContextFactory.h"
Brian Salomon032aaae2018-03-23 16:10:36 -040086
Brian Osmanc7ad40f2018-05-31 14:27:17 -040087using sk_gpu_test::ContextInfo;
88using sk_gpu_test::GrContextFactory;
89using sk_gpu_test::TestContext;
Brian Salomon032aaae2018-03-23 16:10:36 -040090
Brian Osmanc7ad40f2018-05-31 14:27:17 -040091GrContextOptions grContextOpts;
bsalomon682c2692015-05-22 14:01:46 -070092
reed53249782014-10-10 09:09:52 -070093static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070094
bsalomon6eb03cc2014-08-07 14:28:50 -070095static SkString loops_help_txt() {
96 SkString help;
97 help.printf("Number of times to run each bench. Set this to %d to auto-"
98 "tune for each bench. Timings are only reported when auto-tuning.",
99 kAutoTuneLoops);
100 return help;
101}
102
cdaltone1b89582015-06-25 19:17:08 -0700103static SkString to_string(int n) {
104 SkString str;
105 str.appendS32(n);
106 return str;
107}
108
Mike Kleinac8f4432019-07-01 10:15:32 -0500109static DEFINE_int(loops, kAutoTuneLoops, loops_help_txt().c_str());
bsalomon6eb03cc2014-08-07 14:28:50 -0700110
Mike Klein5b3f3432019-03-21 11:42:21 -0500111static DEFINE_int(samples, 10, "Number of samples to measure for each bench.");
112static DEFINE_int(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
113static DEFINE_int(overheadLoops, 100000, "Loops to estimate timer overhead.");
Mike Klein84836b72019-03-21 11:31:36 -0500114static DEFINE_double(overheadGoal, 0.0001,
mtkleinf3723212014-06-25 14:08:00 -0700115 "Loop until timer overhead is at most this fraction of our measurments.");
Mike Klein84836b72019-03-21 11:31:36 -0500116static DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
Mike Klein5b3f3432019-03-21 11:42:21 -0500117static DEFINE_int(gpuFrameLag, 5,
Mike Klein84836b72019-03-21 11:31:36 -0500118 "If unknown, estimated maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -0700119
Mike Klein84836b72019-03-21 11:31:36 -0500120static DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
Mike Klein5b3f3432019-03-21 11:42:21 -0500121static DEFINE_int(maxCalibrationAttempts, 3,
mtklein55b0ffc2014-07-17 08:38:23 -0700122 "Try up to this many times to guess loops for a bench, or skip the bench.");
Mike Klein5b3f3432019-03-21 11:42:21 -0500123static DEFINE_int(maxLoops, 1000000, "Never run a bench more times than this.");
Mike Klein84836b72019-03-21 11:31:36 -0500124static DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
125static DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
126static DEFINE_string(zoom, "1.0,0",
127 "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
128 "function that ping-pongs between 1.0 and zoomMax.");
129static DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
Mike Klein84836b72019-03-21 11:31:36 -0500130static DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
Mike Klein5b3f3432019-03-21 11:42:21 -0500131static DEFINE_int(flushEvery, 10, "Flush --outResultsFile every Nth run.");
Mike Klein84836b72019-03-21 11:31:36 -0500132static DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
Chris Dalton5dfb3f42021-04-30 17:16:12 -0600133static DEFINE_bool(gpuStatsDump, false, "Dump GPU stats after each benchmark to json");
134static DEFINE_bool(dmsaaStatsDump, false, "Dump DMSAA stats after each benchmark to json");
Mike Klein84836b72019-03-21 11:31:36 -0500135static DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
136static DEFINE_bool(csv, false, "Print status in CSV format");
137static DEFINE_string(sourceType, "",
mtklein65dfd2f2016-02-03 10:40:54 -0800138 "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
Mike Klein84836b72019-03-21 11:31:36 -0500139static DEFINE_string(benchType, "",
140 "Apply usual --match rules to bench type: micro, recording, "
141 "piping, playback, skcodec, etc.");
mtklein65dfd2f2016-02-03 10:40:54 -0800142
Mike Klein84836b72019-03-21 11:31:36 -0500143static DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
Mike Reed0e2fb662021-07-23 11:00:40 -0400144static DEFINE_bool(forceRasterPipelineHP, false, "sets gSkForceRasterPipelineBlitter and gForceHighPrecisionRasterPipeline");
Mike Klein813e8cc2020-08-05 09:33:38 -0500145static DEFINE_bool(skvm, false, "sets gUseSkVMBlitter");
Mike Kleinf2b3f912021-03-23 14:32:45 -0500146static DEFINE_bool(jit, true, "JIT SkVM?");
147static DEFINE_bool(dylib, false, "JIT via dylib (much slower compile but easier to debug/profile)");
Mike Reedc928fe22017-06-05 10:20:31 -0400148
Mike Klein19fb3972019-03-21 13:08:08 -0500149static DEFINE_bool2(pre_log, p, false,
150 "Log before running each test. May be incomprehensible when threading");
151
Leon Scroggins III1ff07062020-07-27 14:52:19 -0400152static DEFINE_bool(cpu, true, "Run CPU-bound work?");
153static DEFINE_bool(gpu, true, "Run GPU-bound work?");
Mike Klein629f5fc2019-03-22 14:55:19 -0500154static DEFINE_bool(dryRun, false,
155 "just print the tests that would be run, without actually running them.");
156static DEFINE_string(images, "",
157 "List of images and/or directories to decode. A directory with no images"
158 " is treated as a fatal error.");
159static DEFINE_bool(simpleCodec, false,
160 "Runs of a subset of the codec tests, always N32, Premul or Opaque");
161
Mike Kleinc6142d82019-03-25 10:54:59 -0500162static DEFINE_string2(match, m, nullptr,
163 "[~][^]substring[$] [...] of name to run.\n"
164 "Multiple matches may be separated by spaces.\n"
165 "~ causes a matching name to always be skipped\n"
166 "^ requires the start of the name to match\n"
167 "$ requires the end of the name to match\n"
168 "^ and $ requires an exact match\n"
169 "If a name does not match any list entry,\n"
170 "it is skipped unless some list entry starts with ~");
171
172static DEFINE_bool2(quiet, q, false, "if true, don't print status updates.");
173static DEFINE_bool2(verbose, v, false, "enable verbose output from the test driver.");
174
175
176static DEFINE_string(skps, "skps", "Directory to read skps from.");
Brian Salomon31fddc32021-04-30 13:08:55 -0400177static DEFINE_string(mskps, "mskps", "Directory to read mskps from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500178static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
Hal Canary5dfefa22019-10-24 13:52:17 -0400179static DEFINE_string(texttraces, "", "Directory to read TextBlobTrace files from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500180
181static DEFINE_int_2(threads, j, -1,
182 "Run threadsafe tests on a threadpool with this many extra threads, "
183 "defaulting to one extra thread per core.");
184
185static DEFINE_string2(writePath, w, "", "If set, write bitmaps here as .pngs.");
186
187static DEFINE_string(key, "",
188 "Space-separated key/value pairs to add to JSON identifying this builder.");
189static DEFINE_string(properties, "",
190 "Space-separated key/value pairs to add to JSON identifying this run.");
191
Mike Kleinee254232019-03-28 08:32:14 -0500192static DEFINE_bool(purgeBetweenBenches, false,
193 "Call SkGraphics::PurgeAllCaches() between each benchmark?");
194
mtkleinbbba1682015-10-28 11:36:30 -0700195static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
196
mtkleinf3723212014-06-25 14:08:00 -0700197static SkString humanize(double ms) {
Adlai Holler684838f2020-05-12 10:41:04 -0400198 if (FLAGS_verbose) return SkStringPrintf("%" PRIu64, (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800199 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700200}
mtklein55b0ffc2014-07-17 08:38:23 -0700201#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700202
tomhudsond968a6f2015-03-26 11:28:06 -0700203bool Target::init(SkImageInfo info, Benchmark* bench) {
204 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700205 this->surface = SkSurface::MakeRaster(info);
206 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700207 return false;
208 }
209 }
210 return true;
211}
212bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700213 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700214 if (!canvas) {
215 return false;
216 }
Mike Reed12e946b2017-04-17 10:53:29 -0400217 bmp->allocPixels(canvas->imageInfo());
218 if (!canvas->readPixels(*bmp, 0, 0)) {
tomhudsond968a6f2015-03-26 11:28:06 -0700219 SkDebugf("Can't read canvas pixels.\n");
220 return false;
221 }
222 return true;
223}
224
tomhudsond968a6f2015-03-26 11:28:06 -0700225struct GPUTarget : public Target {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400226 explicit GPUTarget(const Config& c) : Target(c) {}
227 ContextInfo contextInfo;
228 std::unique_ptr<GrContextFactory> factory;
tomhudsond968a6f2015-03-26 11:28:06 -0700229
Greg Daniel5ed3c112020-06-18 15:59:17 -0400230 ~GPUTarget() override {
231 // For Vulkan we need to release all our refs to the GrContext before destroy the vulkan
232 // context which happens at the end of this destructor. Thus we need to release the surface
233 // here which holds a ref to the GrContext.
234 surface.reset();
235 }
236
tomhudsond968a6f2015-03-26 11:28:06 -0700237 void setup() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400238 this->contextInfo.testContext()->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700239 // Make sure we're done with whatever came before.
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400240 this->contextInfo.testContext()->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700241 }
242 void endTiming() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400243 if (this->contextInfo.testContext()) {
Robert Phillips00f78de2020-07-01 16:09:43 -0400244 this->contextInfo.testContext()->flushAndWaitOnSync(contextInfo.directContext());
tomhudsond968a6f2015-03-26 11:28:06 -0700245 }
246 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400247 void fence() override { this->contextInfo.testContext()->finish(); }
mtkleind75c4662015-04-30 07:11:22 -0700248
cdaltond416a5b2015-06-23 13:23:44 -0700249 bool needsFrameTiming(int* maxFrameLag) const override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400250 if (!this->contextInfo.testContext()->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700251 // Frame lag is unknown.
252 *maxFrameLag = FLAGS_gpuFrameLag;
253 }
254 return true;
255 }
tomhudsond968a6f2015-03-26 11:28:06 -0700256 bool init(SkImageInfo info, Benchmark* bench) override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400257 GrContextOptions options = grContextOpts;
258 bench->modifyGrContextOptions(&options);
John Stilesfbd050b2020-08-03 13:21:46 -0400259 this->factory = std::make_unique<GrContextFactory>(options);
Chris Dalton180b4a12021-03-16 20:49:15 -0600260 SkSurfaceProps props(this->config.surfaceFlags, kRGB_H_SkPixelGeometry);
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400261 this->surface = SkSurface::MakeRenderTarget(
262 this->factory->get(this->config.ctxType, this->config.ctxOverrides),
263 SkBudgeted::kNo, info, this->config.samples, &props);
264 this->contextInfo =
265 this->factory->getContextInfo(this->config.ctxType, this->config.ctxOverrides);
John Stilesa008b0f2020-08-16 08:48:02 -0400266 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700267 return false;
268 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400269 if (!this->contextInfo.testContext()->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700270 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800271 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700272 }
tomhudsond968a6f2015-03-26 11:28:06 -0700273 return true;
274 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500275 void fillOptions(NanoJSONResultsWriter& log) override {
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400276#ifdef SK_GL
tomhudsond968a6f2015-03-26 11:28:06 -0700277 const GrGLubyte* version;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400278 if (this->contextInfo.backend() == GrBackendApi::kOpenGL) {
Brian Salomon032aaae2018-03-23 16:10:36 -0400279 const GrGLInterface* gl =
Robert Phillips00f78de2020-07-01 16:09:43 -0400280 static_cast<GrGLGpu*>(this->contextInfo.directContext()->priv().getGpu())
Brian Salomon032aaae2018-03-23 16:10:36 -0400281 ->glInterface();
bsalomonc8699322016-05-11 11:55:36 -0700282 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500283 log.appendString("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700284
bsalomonc8699322016-05-11 11:55:36 -0700285 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500286 log.appendString("GL_RENDERER", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700287
bsalomonc8699322016-05-11 11:55:36 -0700288 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VENDOR));
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500289 log.appendString("GL_VENDOR", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700290
bsalomonc8699322016-05-11 11:55:36 -0700291 GR_GL_CALL_RET(gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500292 log.appendString("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
bsalomonc8699322016-05-11 11:55:36 -0700293 }
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400294#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700295 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400296
297 void dumpStats() override {
Robert Phillips00f78de2020-07-01 16:09:43 -0400298 auto context = this->contextInfo.directContext();
299
300 context->priv().printCacheStats();
301 context->priv().printGpuStats();
302 context->priv().printContextStats();
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400303 }
tomhudsond968a6f2015-03-26 11:28:06 -0700304};
mtkleind75c4662015-04-30 07:11:22 -0700305
tomhudson75a0ebb2015-03-27 12:11:44 -0700306static double time(int loops, Benchmark* bench, Target* target) {
307 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700308 if (canvas) {
309 canvas->clear(SK_ColorWHITE);
310 }
joshualitt8a6697a2015-09-30 12:11:07 -0700311 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700312 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700313 canvas = target->beginTiming(canvas);
314 bench->draw(loops, canvas);
tomhudson75a0ebb2015-03-27 12:11:44 -0700315 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700316 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700317 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700318 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700319}
320
mtkleinf3723212014-06-25 14:08:00 -0700321static double estimate_timer_overhead() {
322 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700323 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700324 double start = now_ms();
325 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700326 }
327 return overhead / FLAGS_overheadLoops;
328}
329
reed53249782014-10-10 09:09:52 -0700330static int detect_forever_loops(int loops) {
331 // look for a magic run-forever value
332 if (loops < 0) {
333 loops = SK_MaxS32;
334 }
335 return loops;
336}
337
mtklein55b0ffc2014-07-17 08:38:23 -0700338static int clamp_loops(int loops) {
339 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800340 SkDebugf("ERROR: clamping loops from %d to 1. "
341 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700342 return 1;
343 }
344 if (loops > FLAGS_maxLoops) {
345 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
346 return FLAGS_maxLoops;
347 }
348 return loops;
349}
350
tomhudsond968a6f2015-03-26 11:28:06 -0700351static bool write_canvas_png(Target* target, const SkString& filename) {
352
bsalomon6eb03cc2014-08-07 14:28:50 -0700353 if (filename.isEmpty()) {
354 return false;
355 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700356 if (target->getCanvas() &&
357 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700358 return false;
359 }
tomhudsond968a6f2015-03-26 11:28:06 -0700360
bsalomon6eb03cc2014-08-07 14:28:50 -0700361 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700362
363 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700364 return false;
365 }
tomhudsond968a6f2015-03-26 11:28:06 -0700366
bsalomon6eb03cc2014-08-07 14:28:50 -0700367 SkString dir = SkOSPath::Dirname(filename.c_str());
368 if (!sk_mkdir(dir.c_str())) {
369 SkDebugf("Can't make dir %s.\n", dir.c_str());
370 return false;
371 }
372 SkFILEWStream stream(filename.c_str());
373 if (!stream.isValid()) {
374 SkDebugf("Can't write %s.\n", filename.c_str());
375 return false;
376 }
Hal Canarydb683012016-11-23 08:55:18 -0700377 if (!SkEncodeImage(&stream, bmp, SkEncodedImageFormat::kPNG, 100)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700378 SkDebugf("Can't encode a PNG.\n");
379 return false;
380 }
381 return true;
382}
383
384static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700385static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700386 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700387 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700388 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700389 int loops = bench->calculateLoops(FLAGS_loops);
390 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700391 while (bench_plus_overhead < overhead) {
392 if (round++ == FLAGS_maxCalibrationAttempts) {
393 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700394 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700395 return kFailedLoops;
396 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700397 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700398 }
mtklein2069e222014-08-04 13:57:39 -0700399 }
mtkleinf3723212014-06-25 14:08:00 -0700400
mtkleinbb6a0282014-07-01 08:43:42 -0700401 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700402 // We'll pick N to make timer overhead negligible:
403 //
mtkleinbb6a0282014-07-01 08:43:42 -0700404 // overhead
405 // ------------------------- < FLAGS_overheadGoal
406 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700407 //
Hal Canary55325b72017-01-03 10:36:17 -0500408 // where bench_plus_overhead ~=~ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700409 //
410 // Doing some math, we get:
411 //
mtkleinbb6a0282014-07-01 08:43:42 -0700412 // (overhead / FLAGS_overheadGoal) - overhead
413 // ------------------------------------------ < N
414 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700415 //
416 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700417 if (kAutoTuneLoops == loops) {
418 const double numer = overhead / FLAGS_overheadGoal - overhead;
419 const double denom = bench_plus_overhead - overhead;
420 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700421 loops = clamp_loops(loops);
422 } else {
423 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700424 }
mtkleinbb6a0282014-07-01 08:43:42 -0700425
mtkleinbb6a0282014-07-01 08:43:42 -0700426 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700427}
428
cdaltone1b89582015-06-25 19:17:08 -0700429static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700430 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700431 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700432 if (kAutoTuneLoops == loops) {
433 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700434 double elapsed = 0;
435 do {
mtklein527930f2014-11-06 08:04:34 -0800436 if (1<<30 == loops) {
437 // We're about to wrap. Something's wrong with the bench.
438 loops = 0;
439 break;
440 }
mtkleina189ccd2014-07-14 12:28:47 -0700441 loops *= 2;
442 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700443 // _this_ round, not still timing last round.
444 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700445 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700446 }
447 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700448
mtkleina189ccd2014-07-14 12:28:47 -0700449 // We've overshot at least a little. Scale back linearly.
450 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700451 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700452
tomhudsond968a6f2015-03-26 11:28:06 -0700453 // Make sure we're not still timing our calibration.
454 target->fence();
reed53249782014-10-10 09:09:52 -0700455 } else {
456 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700457 }
mtkleinbb6a0282014-07-01 08:43:42 -0700458 // Pretty much the same deal as the calibration: do some warmup to make
459 // sure we're timing steady-state pipelined frames.
Brian Osman01776982017-10-10 15:41:03 -0400460 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700461 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700462 }
mtkleinbb6a0282014-07-01 08:43:42 -0700463
mtkleinbb6a0282014-07-01 08:43:42 -0700464 return loops;
465}
mtkleinbb6a0282014-07-01 08:43:42 -0700466
Brian Salomon6405e712017-03-20 08:54:16 -0400467#define kBogusContextType GrContextFactory::kGL_ContextType
csmartdaltone812d492017-02-21 12:36:05 -0700468#define kBogusContextOverrides GrContextFactory::ContextOverrides::kNone
bsalomonc2553372014-07-22 13:09:05 -0700469
svaisanenc47635e2016-01-28 06:05:43 -0800470static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
svaisanenc47635e2016-01-28 06:05:43 -0800471 if (const auto* gpuConfig = config->asConfigGpu()) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400472 if (!FLAGS_gpu) {
473 SkDebugf("Skipping config '%s' as requested.\n", config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800474 return;
Ben Wagner32fa5102017-08-10 21:25:55 -0400475 }
svaisanenc47635e2016-01-28 06:05:43 -0800476
svaisanenc47635e2016-01-28 06:05:43 -0800477 const auto ctxType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700478 const auto ctxOverrides = gpuConfig->getContextOverrides();
svaisanenc47635e2016-01-28 06:05:43 -0800479 const auto sampleCount = gpuConfig->getSamples();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400480 const auto colorType = gpuConfig->getColorType();
481 auto colorSpace = gpuConfig->getColorSpace();
Brian Salomonf865b052018-03-09 09:01:53 -0500482 if (gpuConfig->getSurfType() != SkCommandLineConfigGpu::SurfType::kDefault) {
483 SkDebugf("This tool only supports the default surface type.");
484 return;
485 }
svaisanenc47635e2016-01-28 06:05:43 -0800486
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400487 GrContextFactory factory(grContextOpts);
Robert Phillipsf0288102020-07-06 13:45:34 -0400488 if (const auto ctx = factory.get(ctxType, ctxOverrides)) {
Greg Daniel6fa62e22019-08-07 15:52:37 -0400489 GrBackendFormat format = ctx->defaultBackendFormat(colorType, GrRenderable::kYes);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500490 int supportedSampleCount =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400491 ctx->priv().caps()->getRenderTargetSampleCount(sampleCount, format);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400492 if (sampleCount != supportedSampleCount) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400493 SkDebugf("Configuration '%s' sample count %d is not a supported sample count.\n",
494 config->getTag().c_str(), sampleCount);
svaisanenc47635e2016-01-28 06:05:43 -0800495 return;
496 }
497 } else {
Ben Wagner32fa5102017-08-10 21:25:55 -0400498 SkDebugf("No context was available matching config '%s'.\n",
499 config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800500 return;
501 }
502
503 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800504 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800505 Benchmark::kGPU_Backend,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400506 colorType,
svaisanenc47635e2016-01-28 06:05:43 -0800507 kPremul_SkAlphaType,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400508 sk_ref_sp(colorSpace),
svaisanenc47635e2016-01-28 06:05:43 -0800509 sampleCount,
510 ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700511 ctxOverrides,
Chris Dalton180b4a12021-03-16 20:49:15 -0600512 gpuConfig->getSurfaceFlags()
kkinnunend4e1c352016-03-01 23:41:26 -0800513 };
svaisanenc47635e2016-01-28 06:05:43 -0800514
515 configs->push_back(target);
516 return;
517 }
svaisanenc47635e2016-01-28 06:05:43 -0800518
brianosmanb109b8c2016-06-16 13:03:24 -0700519 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
520 if (config->getTag().equals(#name)) { \
Ben Wagner32fa5102017-08-10 21:25:55 -0400521 if (!FLAGS_cpu) { \
522 SkDebugf("Skipping config '%s' as requested.\n", \
523 config->getTag().c_str()); \
524 return; \
525 } \
brianosmanb109b8c2016-06-16 13:03:24 -0700526 Config config = { \
527 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
Chris Dalton94df5722021-04-19 17:40:40 -0600528 0, kBogusContextType, kBogusContextOverrides, 0 \
brianosmanb109b8c2016-06-16 13:03:24 -0700529 }; \
530 configs->push_back(config); \
531 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700532 }
mtkleine714e752014-07-31 12:13:48 -0700533
Ben Wagner32fa5102017-08-10 21:25:55 -0400534 CPU_CONFIG(nonrendering, kNonRendering_Backend,
535 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800536
Mike Klein0e4041f2018-06-19 16:00:40 -0400537 CPU_CONFIG(a8, kRaster_Backend, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr)
538 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType, nullptr)
539 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
540
541 // 'narrow' has a gamut narrower than sRGB, and different transfer function.
Brian Osman82ebe042019-01-04 17:03:00 -0500542 auto narrow = SkColorSpace::MakeRGB(SkNamedTransferFn::k2Dot2, gNarrow_toXYZD50),
Mike Klein0e4041f2018-06-19 16:00:40 -0400543 srgb = SkColorSpace::MakeSRGB(),
544 srgbLinear = SkColorSpace::MakeSRGBLinear();
545
546 CPU_CONFIG( f16, kRaster_Backend, kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbLinear)
547 CPU_CONFIG( srgb, kRaster_Backend, kRGBA_8888_SkColorType, kPremul_SkAlphaType, srgb )
548 CPU_CONFIG( esrgb, kRaster_Backend, kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgb )
549 CPU_CONFIG( narrow, kRaster_Backend, kRGBA_8888_SkColorType, kPremul_SkAlphaType, narrow )
550 CPU_CONFIG(enarrow, kRaster_Backend, kRGBA_F16_SkColorType, kPremul_SkAlphaType, narrow )
mtkleinbb6a0282014-07-01 08:43:42 -0700551
svaisanenc47635e2016-01-28 06:05:43 -0800552 #undef CPU_CONFIG
Ben Wagner32fa5102017-08-10 21:25:55 -0400553
554 SkDebugf("Unknown config '%s'.\n", config->getTag().c_str());
mtkleinf3723212014-06-25 14:08:00 -0700555}
556
svaisanenc47635e2016-01-28 06:05:43 -0800557// Append all configs that are enabled and supported.
558void create_configs(SkTArray<Config>* configs) {
559 SkCommandLineConfigArray array;
560 ParseConfigs(FLAGS_config, &array);
561 for (int i = 0; i < array.count(); ++i) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400562 create_config(array[i].get(), configs);
svaisanenc47635e2016-01-28 06:05:43 -0800563 }
Ben Wagner32fa5102017-08-10 21:25:55 -0400564
565 // If no just default configs were requested, then we're okay.
566 if (array.count() == 0 || FLAGS_config.count() == 0 ||
Ben Wagner32fa5102017-08-10 21:25:55 -0400567 // Otherwise, make sure that all specified configs have been created.
568 array.count() == configs->count()) {
569 return;
570 }
Ben Wagner32fa5102017-08-10 21:25:55 -0400571 exit(1);
svaisanenc47635e2016-01-28 06:05:43 -0800572}
573
brianosman9f1f6e22016-09-15 08:33:02 -0700574// disable warning : switch statement contains default but no 'case' labels
575#if defined _WIN32
576#pragma warning ( push )
577#pragma warning ( disable : 4065 )
578#endif
579
halcanary96fcdcc2015-08-27 07:41:13 -0700580// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700581static Target* is_enabled(Benchmark* bench, const Config& config) {
582 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700583 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700584 }
585
reede5ea5002014-09-03 11:54:58 -0700586 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700587 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700588
halcanary96fcdcc2015-08-27 07:41:13 -0700589 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700590
tomhudsond968a6f2015-03-26 11:28:06 -0700591 switch (config.backend) {
tomhudsond968a6f2015-03-26 11:28:06 -0700592 case Benchmark::kGPU_Backend:
593 target = new GPUTarget(config);
594 break;
tomhudsond968a6f2015-03-26 11:28:06 -0700595 default:
596 target = new Target(config);
597 break;
598 }
bsalomonc2553372014-07-22 13:09:05 -0700599
tomhudsond968a6f2015-03-26 11:28:06 -0700600 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700601 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700602 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700603 }
604 return target;
605}
606
brianosman9f1f6e22016-09-15 08:33:02 -0700607#if defined _WIN32
608#pragma warning ( pop )
609#endif
610
Leon Scroggins III87caae62020-05-04 10:02:45 -0400611#ifdef SK_ENABLE_ANDROID_UTILS
reed42943c82016-09-12 12:01:44 -0700612static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700613 uint32_t minOutputSize, int* width, int* height) {
Leon Scroggins III87caae62020-05-04 10:02:45 -0400614 auto brd = android::skia::BitmapRegionDecoder::Make(encoded);
John Stilesa008b0f2020-08-16 08:48:02 -0400615 if (nullptr == brd) {
msarett7f691442015-09-22 11:56:16 -0700616 // This is indicates that subset decoding is not supported for a particular image format.
617 return false;
618 }
619
msarett7f691442015-09-22 11:56:16 -0700620 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
621 (uint32_t) brd->height()) {
622 // This indicates that the image is not large enough to decode a
623 // minOutputSize x minOutputSize subset at the given sampleSize.
624 return false;
625 }
626
627 // Set the image width and height. The calling code will use this to choose subsets to decode.
628 *width = brd->width();
629 *height = brd->height();
630 return true;
631}
Leon Scroggins III87caae62020-05-04 10:02:45 -0400632#endif
msarett7f691442015-09-22 11:56:16 -0700633
egdaniel3bf92062015-06-26 08:12:46 -0700634static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700635 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700636}
637
Mike Klein88544fb2019-03-20 10:50:33 -0500638static void collect_files(const CommandLineFlags::StringArray& paths,
639 const char* ext,
640 SkTArray<SkString>* list) {
fmalita6519c212016-09-14 08:05:17 -0700641 for (int i = 0; i < paths.count(); ++i) {
642 if (SkStrEndsWith(paths[i], ext)) {
643 list->push_back(SkString(paths[i]));
644 } else {
645 SkOSFile::Iter it(paths[i], ext);
646 SkString path;
647 while (it.next(&path)) {
648 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
649 }
650 }
651 }
652}
653
mtkleine714e752014-07-31 12:13:48 -0700654class BenchmarkStream {
655public:
mtklein92007582014-08-01 07:46:52 -0700656 BenchmarkStream() : fBenches(BenchRegistry::Head())
Hal Canary2362ae72019-10-24 13:40:40 -0400657 , fGMs(skiagm::GMRegistry::Head()) {
fmalita6519c212016-09-14 08:05:17 -0700658 collect_files(FLAGS_skps, ".skp", &fSKPs);
Brian Salomon31fddc32021-04-30 13:08:55 -0400659 collect_files(FLAGS_mskps, ".mskp", &fMSKPs);
fmalita6519c212016-09-14 08:05:17 -0700660 collect_files(FLAGS_svgs, ".svg", &fSVGs);
Hal Canary5dfefa22019-10-24 13:52:17 -0400661 collect_files(FLAGS_texttraces, ".trace", &fTextBlobTraces);
mtkleine714e752014-07-31 12:13:48 -0700662
mtklein92007582014-08-01 07:46:52 -0700663 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
664 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
665 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
666 exit(1);
667 }
668
669 for (int i = 0; i < FLAGS_scales.count(); i++) {
670 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
671 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
672 exit(1);
673 }
674 }
robertphillips5b693772014-11-21 06:19:36 -0800675
cdalton63a82852015-06-29 14:06:10 -0700676 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
677 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700678 exit(1);
679 }
680
msarett95f192d2015-02-13 09:05:41 -0800681 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700682 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800683 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800684 }
mtklein95553d92015-03-12 08:24:21 -0700685
msarett95f192d2015-02-13 09:05:41 -0800686 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700687 fColorTypes.push_back(kN32_SkColorType);
688 if (!FLAGS_simpleCodec) {
689 fColorTypes.push_back(kRGB_565_SkColorType);
690 fColorTypes.push_back(kAlpha_8_SkColorType);
msarett67cb6662016-06-21 08:49:26 -0700691 fColorTypes.push_back(kGray_8_SkColorType);
692 }
mtklein92007582014-08-01 07:46:52 -0700693 }
694
reedca2622b2016-03-18 07:25:55 -0700695 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700696 // Not strictly necessary, as it will be checked again later,
697 // but helps to avoid a lot of pointless work if we're going to skip it.
Mike Klein88544fb2019-03-20 10:50:33 -0500698 if (CommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700699 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700700 }
701
bungemanf93d7112016-09-16 06:24:20 -0700702 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
703 if (!stream) {
mtkleinfd731ce2014-09-10 12:19:30 -0700704 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700705 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700706 }
707
reedca2622b2016-03-18 07:25:55 -0700708 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700709 }
710
Brian Salomon31fddc32021-04-30 13:08:55 -0400711 static std::unique_ptr<MSKPPlayer> ReadMSKP(const char* path) {
712 // Not strictly necessary, as it will be checked again later,
713 // but helps to avoid a lot of pointless work if we're going to skip it.
714 if (CommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
715 return nullptr;
716 }
717
718 std::unique_ptr<SkStreamSeekable> stream = SkStream::MakeFromFile(path);
719 if (!stream) {
720 SkDebugf("Could not read %s.\n", path);
721 return nullptr;
722 }
723
724 return MSKPPlayer::Make(stream.get());
725 }
726
fmalita6519c212016-09-14 08:05:17 -0700727 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
Mike Klein8ab65bf2020-12-04 09:20:32 -0600728 if (CommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
729 return nullptr;
730 }
Brian Osman133823d2018-09-19 14:14:15 -0400731 sk_sp<SkData> data(SkData::MakeFromFileName(path));
732 if (!data) {
fmalita6519c212016-09-14 08:05:17 -0700733 SkDebugf("Could not read %s.\n", path);
734 return nullptr;
735 }
736
Hal Canary0f666812018-03-22 15:21:12 -0400737#ifdef SK_XML
Brian Osman133823d2018-09-19 14:14:15 -0400738 SkMemoryStream stream(std::move(data));
fmalitae1baa7c2016-09-14 12:04:30 -0700739 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
fmalita6519c212016-09-14 08:05:17 -0700740 if (!svgDom) {
741 SkDebugf("Could not parse %s.\n", path);
742 return nullptr;
743 }
744
fmalitae1baa7c2016-09-14 12:04:30 -0700745 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
746 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
747 if (svgDom->containerSize().isEmpty()) {
748 svgDom->setContainerSize(kDefaultContainerSize);
749 }
750
fmalita6519c212016-09-14 08:05:17 -0700751 SkPictureRecorder recorder;
fmalitae1baa7c2016-09-14 12:04:30 -0700752 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
753 svgDom->containerSize().height()));
fmalita6519c212016-09-14 08:05:17 -0700754 return recorder.finishRecordingAsPicture();
Hal Canary0f666812018-03-22 15:21:12 -0400755#else
756 return nullptr;
757#endif // SK_XML
fmalita6519c212016-09-14 08:05:17 -0700758 }
759
mtklein92007582014-08-01 07:46:52 -0700760 Benchmark* next() {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400761 std::unique_ptr<Benchmark> bench;
mtklein65dfd2f2016-02-03 10:40:54 -0800762 do {
763 bench.reset(this->rawNext());
764 if (!bench) {
765 return nullptr;
766 }
Mike Klein88544fb2019-03-20 10:50:33 -0500767 } while (CommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
768 CommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700769 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800770 }
771
772 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700773 if (fBenches) {
Hal Canary972eba32018-07-30 17:07:07 -0400774 Benchmark* bench = fBenches->get()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700775 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700776 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700777 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700778 return bench;
779 }
mtklein92007582014-08-01 07:46:52 -0700780
mtkleine714e752014-07-31 12:13:48 -0700781 while (fGMs) {
Ben Wagner406ff502019-08-12 16:39:24 -0400782 std::unique_ptr<skiagm::GM> gm = fGMs->get()();
mtkleine714e752014-07-31 12:13:48 -0700783 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800784 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700785 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700786 fBenchType = "micro";
Ben Wagner406ff502019-08-12 16:39:24 -0400787 return new GMBench(std::move(gm));
mtkleine714e752014-07-31 12:13:48 -0700788 }
789 }
mtklein92007582014-08-01 07:46:52 -0700790
Hal Canary5dfefa22019-10-24 13:52:17 -0400791 while (fCurrentTextBlobTrace < fTextBlobTraces.count()) {
792 SkString path = fTextBlobTraces[fCurrentTextBlobTrace++];
793 SkString basename = SkOSPath::Basename(path.c_str());
794 static constexpr char kEnding[] = ".trace";
795 if (basename.endsWith(kEnding)) {
796 basename.remove(basename.size() - strlen(kEnding), strlen(kEnding));
797 }
798 fSourceType = "texttrace";
799 fBenchType = "micro";
800 return CreateDiffCanvasBench(
801 SkStringPrintf("SkDiffBench-%s", basename.c_str()),
802 [path](){ return SkStream::MakeFromFile(path.c_str()); });
803 }
804
mtkleinfd731ce2014-09-10 12:19:30 -0700805 // First add all .skps as RecordingBenches.
806 while (fCurrentRecording < fSKPs.count()) {
807 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700808 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
809 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700810 continue;
811 }
812 SkString name = SkOSPath::Basename(path.c_str());
813 fSourceType = "skp";
814 fBenchType = "recording";
Mike Klein88d90712018-01-27 17:30:04 +0000815 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
816 fSKPOps = pic->approximateOpCount();
Mike Kleina705cb92019-05-14 12:33:40 -0500817 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700818 }
819
Mike Reede45ff462017-12-06 10:47:03 -0500820 // Add all .skps as DeserializePictureBenchs.
821 while (fCurrentDeserialPicture < fSKPs.count()) {
822 const SkString& path = fSKPs[fCurrentDeserialPicture++];
823 sk_sp<SkData> data = SkData::MakeFromFileName(path.c_str());
824 if (!data) {
825 continue;
826 }
827 SkString name = SkOSPath::Basename(path.c_str());
828 fSourceType = "skp";
829 fBenchType = "deserial";
Mike Reed7557bbb2017-12-24 19:50:57 -0500830 fSKPBytes = static_cast<double>(data->size());
Mike Klein88d90712018-01-27 17:30:04 +0000831 fSKPOps = 0;
Mike Reede45ff462017-12-06 10:47:03 -0500832 return new DeserializePictureBench(name.c_str(), std::move(data));
833 }
834
mtkleinfd731ce2014-09-10 12:19:30 -0700835 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700836 while (fCurrentScale < fScales.count()) {
837 while (fCurrentSKP < fSKPs.count()) {
Brian Salomon626349b2020-08-27 10:54:36 -0400838 const SkString& path = fSKPs[fCurrentSKP++];
reedca2622b2016-03-18 07:25:55 -0700839 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
840 if (!pic) {
mtklein92007582014-08-01 07:46:52 -0700841 continue;
842 }
robertphillips5b693772014-11-21 06:19:36 -0800843
Brian Salomon626349b2020-08-27 10:54:36 -0400844 if (FLAGS_bbh) {
845 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
846 SkRTreeFactory factory;
847 SkPictureRecorder recorder;
848 pic->playback(recorder.beginRecording(pic->cullRect().width(),
849 pic->cullRect().height(),
850 &factory));
851 pic = recorder.finishRecordingAsPicture();
mtklein20840502014-08-21 15:51:22 -0700852 }
Brian Salomon626349b2020-08-27 10:54:36 -0400853 SkString name = SkOSPath::Basename(path.c_str());
854 fSourceType = "skp";
855 fBenchType = "playback";
856 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
857 FLAGS_loopSKP);
mtklein92007582014-08-01 07:46:52 -0700858 }
fmalita6519c212016-09-14 08:05:17 -0700859
Brian Salomon626349b2020-08-27 10:54:36 -0400860 while (fCurrentSVG < fSVGs.count()) {
861 const char* path = fSVGs[fCurrentSVG++].c_str();
fmalita6519c212016-09-14 08:05:17 -0700862 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
863 fSourceType = "svg";
864 fBenchType = "playback";
865 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
Brian Salomon626349b2020-08-27 10:54:36 -0400866 fScales[fCurrentScale], FLAGS_loopSKP);
fmalita6519c212016-09-14 08:05:17 -0700867 }
868 }
869
mtklein92007582014-08-01 07:46:52 -0700870 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700871 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700872 fCurrentScale++;
873 }
874
joshualitt261c3ad2015-04-27 09:16:57 -0700875 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700876 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700877 while (fCurrentAnimSKP < fSKPs.count()) {
878 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700879 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
880 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700881 fCurrentAnimSKP++;
882 continue;
883 }
884
885 fCurrentAnimSKP++;
886 SkString name = SkOSPath::Basename(path.c_str());
Ben Wagneracf98df2019-07-12 12:51:44 -0400887 sk_sp<SKPAnimationBench::Animation> animation =
888 SKPAnimationBench::MakeZoomAnimation(fZoomMax, fZoomPeriodMs);
889 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, std::move(animation),
halcanary385fe4d2015-08-26 13:07:48 -0700890 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700891 }
892 }
893
Brian Salomon31fddc32021-04-30 13:08:55 -0400894 // Read all MSKPs as benches
895 while (fCurrentMSKP < fMSKPs.count()) {
896 const SkString& path = fMSKPs[fCurrentMSKP++];
897 std::unique_ptr<MSKPPlayer> player = ReadMSKP(path.c_str());
898 if (!player) {
899 continue;
900 }
901 SkString name = SkOSPath::Basename(path.c_str());
902 fSourceType = "mskp";
903 fBenchType = "mskp";
904 return new MSKPBench(std::move(name), std::move(player));
905 }
906
scroggo60869a42015-04-01 12:09:17 -0700907 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700908 fSourceType = "image";
909 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700910 const SkString& path = fImages[fCurrentCodec];
Mike Klein88544fb2019-03-20 10:50:33 -0500911 if (CommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
mtklein6f0ff912016-01-11 09:04:21 -0800912 continue;
913 }
bungeman38d909e2016-08-02 14:40:46 -0700914 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400915 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700916 if (!codec) {
917 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700918 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700919 continue;
920 }
scroggo21027992015-04-02 13:22:38 -0700921
scroggo60869a42015-04-01 12:09:17 -0700922 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700923 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700924
msarettc7796b92016-01-07 14:20:20 -0800925 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700926 if (FLAGS_simpleCodec) {
927 if (kUnpremul_SkAlphaType == alphaType) {
928 alphaType = kPremul_SkAlphaType;
929 }
930
931 fCurrentColorType++;
932 } else {
933 switch (alphaType) {
934 case kOpaque_SkAlphaType:
935 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800936 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700937 break;
938 case kUnpremul_SkAlphaType:
939 case kPremul_SkAlphaType:
940 if (0 == fCurrentAlphaType) {
941 // Test unpremul first.
942 alphaType = kUnpremul_SkAlphaType;
943 fCurrentAlphaType++;
944 } else {
945 // Test premul.
946 alphaType = kPremul_SkAlphaType;
947 fCurrentAlphaType = 0;
948 fCurrentColorType++;
949 }
950 break;
951 default:
952 SkASSERT(false);
953 fCurrentColorType++;
954 break;
955 }
scroggo21027992015-04-02 13:22:38 -0700956 }
957
msarettc7796b92016-01-07 14:20:20 -0800958 // Make sure we can decode to this color type and alpha type.
959 SkImageInfo info =
960 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700961 const size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400962 SkAutoMalloc storage(info.computeByteSize(rowBytes));
scroggo21027992015-04-02 13:22:38 -0700963
scroggoeb602a52015-07-09 08:16:03 -0700964 const SkCodec::Result result = codec->getPixels(
Leon Scroggins571b30f2017-07-11 17:35:31 +0000965 info, storage.get(), rowBytes);
scroggo60869a42015-04-01 12:09:17 -0700966 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700967 case SkCodec::kSuccess:
968 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700969 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700970 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700971 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700972 // This is okay. Not all conversions are valid.
973 break;
scroggo60869a42015-04-01 12:09:17 -0700974 default:
975 // This represents some sort of failure.
976 SkASSERT(false);
977 break;
978 }
979 }
980 fCurrentColorType = 0;
981 }
982
msarett84451022016-02-11 06:45:51 -0800983 // Run AndroidCodecBenches
984 const int sampleSizes[] = { 2, 4, 8 };
985 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
986 fSourceType = "image";
987 fBenchType = "skandroidcodec";
988
989 const SkString& path = fImages[fCurrentAndroidCodec];
Mike Klein88544fb2019-03-20 10:50:33 -0500990 if (CommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
msarett84451022016-02-11 06:45:51 -0800991 continue;
992 }
bungeman38d909e2016-08-02 14:40:46 -0700993 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400994 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800995 if (!codec) {
996 // Nothing to time.
997 SkDebugf("Cannot find codec for %s\n", path.c_str());
998 continue;
999 }
1000
1001 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
1002 int sampleSize = sampleSizes[fCurrentSampleSize];
1003 fCurrentSampleSize++;
Brian Osman788b9162020-02-07 10:36:46 -05001004 if (10 * sampleSize > std::min(codec->getInfo().width(), codec->getInfo().height())) {
msarett84451022016-02-11 06:45:51 -08001005 // Avoid benchmarking scaled decodes of already small images.
1006 break;
1007 }
1008
bungeman38d909e2016-08-02 14:40:46 -07001009 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
1010 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -08001011 }
1012 fCurrentSampleSize = 0;
1013 }
1014
Leon Scroggins III87caae62020-05-04 10:02:45 -04001015#ifdef SK_ENABLE_ANDROID_UTILS
msarett7f691442015-09-22 11:56:16 -07001016 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -07001017 // We intend to create benchmarks that model the use cases in
1018 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
1019 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
1020 // the image. For that reason, we will benchmark decodes in five representative locations
1021 // in the image. Additionally, this use case utilizes power of two scaling, so we will
1022 // test on power of two sample sizes. The output tile is always 512x512, so, when a
1023 // sampleSize is used, the size of the subset that is decoded is always
1024 // (sampleSize*512)x(sampleSize*512).
1025 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -07001026 // All use cases we are aware of only scale by powers of two.
1027 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
1028 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -08001029 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -07001030 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -08001031 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -07001032 fSourceType = "image";
1033 fBenchType = "BRD";
1034
mtklein6f0ff912016-01-11 09:04:21 -08001035 const SkString& path = fImages[fCurrentBRDImage];
Mike Klein88544fb2019-03-20 10:50:33 -05001036 if (CommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
mtklein6f0ff912016-01-11 09:04:21 -08001037 continue;
1038 }
scroggo860e8a62015-10-15 07:51:28 -07001039
msarettd1227a72016-05-18 06:23:57 -07001040 while (fCurrentColorType < fColorTypes.count()) {
1041 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
1042 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -07001043
bungeman38d909e2016-08-02 14:40:46 -07001044 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -07001045 const SkColorType colorType = fColorTypes[fCurrentColorType];
1046 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
1047 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -07001048
msarettd1227a72016-05-18 06:23:57 -07001049 int width = 0;
1050 int height = 0;
reed42943c82016-09-12 12:01:44 -07001051 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -07001052 &width, &height)) {
1053 break;
msarett7f691442015-09-22 11:56:16 -07001054 }
msarettd1227a72016-05-18 06:23:57 -07001055
1056 SkString basename = SkOSPath::Basename(path.c_str());
1057 SkIRect subset;
1058 const uint32_t subsetSize = sampleSize * minOutputSize;
1059 switch (currentSubsetType) {
1060 case kTopLeft_SubsetType:
1061 basename.append("_TopLeft");
1062 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
1063 break;
1064 case kTopRight_SubsetType:
1065 basename.append("_TopRight");
1066 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
1067 subsetSize);
1068 break;
1069 case kMiddle_SubsetType:
1070 basename.append("_Middle");
1071 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
1072 (height - subsetSize) / 2, subsetSize, subsetSize);
1073 break;
1074 case kBottomLeft_SubsetType:
1075 basename.append("_BottomLeft");
1076 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
1077 subsetSize);
1078 break;
1079 case kBottomRight_SubsetType:
1080 basename.append("_BottomRight");
1081 subset = SkIRect::MakeXYWH(width - subsetSize,
1082 height - subsetSize, subsetSize, subsetSize);
1083 break;
1084 default:
1085 SkASSERT(false);
1086 }
1087
1088 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
1089 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -07001090 }
msarettd1227a72016-05-18 06:23:57 -07001091 fCurrentSubsetType = 0;
1092 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -07001093 }
msarettd1227a72016-05-18 06:23:57 -07001094 fCurrentSampleSize = 0;
1095 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -07001096 }
msarettd1227a72016-05-18 06:23:57 -07001097 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -07001098 }
Leon Scroggins III87caae62020-05-04 10:02:45 -04001099#endif // SK_ENABLE_ANDROID_UTILS
msarett7f691442015-09-22 11:56:16 -07001100
halcanary96fcdcc2015-08-27 07:41:13 -07001101 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001102 }
mtklein92007582014-08-01 07:46:52 -07001103
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001104 void fillCurrentOptions(NanoJSONResultsWriter& log) const {
1105 log.appendString("source_type", fSourceType);
1106 log.appendString("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001107 if (0 == strcmp(fSourceType, "skp")) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001108 log.appendString("clip",
mtklein92007582014-08-01 07:46:52 -07001109 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1110 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001111 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001112 log.appendString("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
mtklein92007582014-08-01 07:46:52 -07001113 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001114 }
1115
1116 void fillCurrentMetrics(NanoJSONResultsWriter& log) const {
mtklein051e56d2014-12-04 08:46:51 -08001117 if (0 == strcmp(fBenchType, "recording")) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001118 log.appendMetric("bytes", fSKPBytes);
1119 log.appendMetric("ops", fSKPOps);
mtklein051e56d2014-12-04 08:46:51 -08001120 }
mtklein92007582014-08-01 07:46:52 -07001121 }
1122
mtkleine714e752014-07-31 12:13:48 -07001123private:
Leon Scroggins III87caae62020-05-04 10:02:45 -04001124#ifdef SK_ENABLE_ANDROID_UTILS
msarettb23e6aa2015-06-09 13:56:10 -07001125 enum SubsetType {
1126 kTopLeft_SubsetType = 0,
1127 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001128 kMiddle_SubsetType = 2,
1129 kBottomLeft_SubsetType = 3,
1130 kBottomRight_SubsetType = 4,
1131 kTranslate_SubsetType = 5,
1132 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001133 kLast_SubsetType = kZoom_SubsetType,
1134 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001135 };
Leon Scroggins III87caae62020-05-04 10:02:45 -04001136#endif
msarettb23e6aa2015-06-09 13:56:10 -07001137
mtkleine714e752014-07-31 12:13:48 -07001138 const BenchRegistry* fBenches;
1139 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001140 SkIRect fClip;
1141 SkTArray<SkScalar> fScales;
1142 SkTArray<SkString> fSKPs;
Brian Salomon31fddc32021-04-30 13:08:55 -04001143 SkTArray<SkString> fMSKPs;
fmalita6519c212016-09-14 08:05:17 -07001144 SkTArray<SkString> fSVGs;
Hal Canary5dfefa22019-10-24 13:52:17 -04001145 SkTArray<SkString> fTextBlobTraces;
msarett95f192d2015-02-13 09:05:41 -08001146 SkTArray<SkString> fImages;
msarett74deb982015-10-20 16:45:56 -07001147 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001148 SkScalar fZoomMax;
1149 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001150
Mike Klein88d90712018-01-27 17:30:04 +00001151 double fSKPBytes, fSKPOps;
mtklein051e56d2014-12-04 08:46:51 -08001152
mtkleinfd731ce2014-09-10 12:19:30 -07001153 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1154 const char* fBenchType; // How we bench it: micro, recording, playback, ...
Hal Canary2362ae72019-10-24 13:40:40 -04001155 int fCurrentRecording = 0;
1156 int fCurrentDeserialPicture = 0;
Brian Salomon31fddc32021-04-30 13:08:55 -04001157 int fCurrentMSKP = 0;
Hal Canary2362ae72019-10-24 13:40:40 -04001158 int fCurrentScale = 0;
1159 int fCurrentSKP = 0;
1160 int fCurrentSVG = 0;
Hal Canary5dfefa22019-10-24 13:52:17 -04001161 int fCurrentTextBlobTrace = 0;
Hal Canary2362ae72019-10-24 13:40:40 -04001162 int fCurrentCodec = 0;
1163 int fCurrentAndroidCodec = 0;
Leon Scroggins III87caae62020-05-04 10:02:45 -04001164#ifdef SK_ENABLE_ANDROID_UTILS
Hal Canary2362ae72019-10-24 13:40:40 -04001165 int fCurrentBRDImage = 0;
Leon Scroggins III87caae62020-05-04 10:02:45 -04001166 int fCurrentSubsetType = 0;
1167#endif
Hal Canary2362ae72019-10-24 13:40:40 -04001168 int fCurrentColorType = 0;
1169 int fCurrentAlphaType = 0;
Hal Canary2362ae72019-10-24 13:40:40 -04001170 int fCurrentSampleSize = 0;
1171 int fCurrentAnimSKP = 0;
mtkleine714e752014-07-31 12:13:48 -07001172};
1173
msarettc149f0e2016-01-04 11:35:43 -08001174// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1175// This prints something every once in a while so that it knows we're still working.
1176static void start_keepalive() {
Mike Klein03141d22017-10-30 11:57:15 -04001177 static std::thread* intentionallyLeaked = new std::thread([]{
1178 for (;;) {
1179 static const int kSec = 1200;
1180 #if defined(SK_BUILD_FOR_WIN)
1181 Sleep(kSec * 1000);
1182 #else
1183 sleep(kSec);
1184 #endif
1185 SkDebugf("\nBenchmarks still running...\n");
msarettc149f0e2016-01-04 11:35:43 -08001186 }
Mike Klein03141d22017-10-30 11:57:15 -04001187 });
1188 (void)intentionallyLeaked;
msarettc149f0e2016-01-04 11:35:43 -08001189}
1190
John Stiles2ef627a2021-03-23 10:25:02 -04001191class NanobenchShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
1192 void compileError(const char* shader, const char* errors) override {
1193 // Nanobench should abort if any shader can't compile. Failure is much better than
1194 // reporting meaningless performance metrics.
1195 SkSL::String message = GrShaderUtils::BuildShaderErrorMessage(shader, errors);
John Stiles345f1402021-03-23 10:25:15 -04001196 SK_ABORT("\n%s", message.c_str());
John Stiles2ef627a2021-03-23 10:25:02 -04001197 }
1198};
1199
Mike Kleinbe28ee22017-02-06 12:46:20 -05001200int main(int argc, char** argv) {
Mike Klein88544fb2019-03-20 10:50:33 -05001201 CommandLineFlags::Parse(argc, argv);
Brian Osman53136aa2017-07-20 15:43:35 -04001202
Brian Osmanbc8150f2017-07-24 11:38:01 -04001203 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -04001204
Mike Kleinadacaef2017-02-06 09:26:14 -05001205#if defined(SK_BUILD_FOR_IOS)
1206 cd_Documents();
1207#endif
jcgregorio3b27ade2014-11-13 08:06:40 -08001208 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001209 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001210 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001211
Chris Dalton040238b2017-12-18 14:22:34 -07001212 SetCtxOptionsFromCommonFlags(&grContextOpts);
krajcevski69a55602014-08-13 10:46:31 -07001213
John Stiles2ef627a2021-03-23 10:25:02 -04001214 NanobenchShaderErrorHandler errorHandler;
1215 grContextOpts.fShaderErrorHandler = &errorHandler;
1216
bsalomon6eb03cc2014-08-07 14:28:50 -07001217 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001218 FLAGS_samples = 1;
1219 FLAGS_gpuFrameLag = 0;
1220 }
1221
bsalomon6eb03cc2014-08-07 14:28:50 -07001222 if (!FLAGS_writePath.isEmpty()) {
1223 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1224 if (!sk_mkdir(FLAGS_writePath[0])) {
1225 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001226 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001227 }
1228 }
1229
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001230 std::unique_ptr<SkWStream> logStream(new SkNullWStream);
mtklein60317d0f2014-07-14 11:30:37 -07001231 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001232#if defined(SK_RELEASE)
Herb Derbyefe9df32021-06-18 20:31:43 +00001233 logStream.reset(new SkFILEWStream(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 Reed0e2fb662021-07-23 11:00:40 -04001290 gSkForceRasterPipelineBlitter = FLAGS_forceRasterPipelineHP || FLAGS_forceRasterPipeline;
1291 gForceHighPrecisionRasterPipeline = FLAGS_forceRasterPipelineHP;
Mike Klein813e8cc2020-08-05 09:33:38 -05001292 gUseSkVMBlitter = FLAGS_skvm;
Mike Kleinf2b3f912021-03-23 14:32:45 -05001293 gSkVMAllowJIT = FLAGS_jit;
1294 gSkVMJITViaDylib = FLAGS_dylib;
Yuqian Li550148b2017-01-13 10:13:13 -05001295
mtkleine070c2b2014-10-14 08:40:43 -07001296 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001297 BenchmarkStream benchStream;
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001298 log.beginObject("results");
Jim Van Verth8a9a3712019-05-31 10:49:12 -04001299 AutoreleasePool pool;
mtklein92007582014-08-01 07:46:52 -07001300 while (Benchmark* b = benchStream.next()) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001301 std::unique_ptr<Benchmark> bench(b);
Mike Klein88544fb2019-03-20 10:50:33 -05001302 if (CommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001303 continue;
1304 }
1305
svaisanenc47635e2016-01-28 06:05:43 -08001306 if (!configs.empty()) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001307 log.beginBench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001308 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001309 }
egdaniel3bf92062015-06-26 08:12:46 -07001310 for (int i = 0; i < configs.count(); ++i) {
1311 Target* target = is_enabled(b, configs[i]);
1312 if (!target) {
1313 continue;
1314 }
mtkleinf3723212014-06-25 14:08:00 -07001315
halcanary96fcdcc2015-08-27 07:41:13 -07001316 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001317 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001318 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001319
brianosman7a5ada82016-02-08 13:49:12 -08001320 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001321 SkDebugf("Running %s\t%s\n"
1322 , bench->getUniqueName()
1323 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001324 if (FLAGS_dryRun) {
1325 continue;
1326 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001327 }
1328
Mike Kleinee254232019-03-28 08:32:14 -05001329 if (FLAGS_purgeBetweenBenches) {
1330 SkGraphics::PurgeAllCaches();
1331 }
1332
Brian Osman5ad87aa2017-09-08 16:05:23 -04001333 TRACE_EVENT2("skia", "Benchmark", "name", TRACE_STR_COPY(bench->getUniqueName()),
1334 "config", TRACE_STR_COPY(config));
Brian Osmanc3cdef52017-08-31 14:09:22 -04001335
egdaniel3bf92062015-06-26 08:12:46 -07001336 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001337 bench->perCanvasPreDraw(canvas);
1338
cdaltone1b89582015-06-25 19:17:08 -07001339 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001340 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001341 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1342 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001343
Leon Scroggins IIIb30d1132017-10-02 09:48:15 -04001344 if (kFailedLoops == loops) {
1345 // Can't be timed. A warning note has already been printed.
1346 cleanup_run(target);
1347 continue;
1348 }
1349
Yuqian Li3528eb32017-09-21 13:23:49 -04001350 if (runs == 0 && FLAGS_ms < 1000) {
1351 // Run the first bench for 1000ms to warm up the nanobench if FLAGS_ms < 1000.
1352 // Otherwise, the first few benches' measurements will be inaccurate.
1353 auto stop = now_ms() + 1000;
1354 do {
1355 time(loops, bench.get(), target);
Jim Van Verth49d6bca2020-02-28 11:05:44 -05001356 pool.drain();
Yuqian Li3528eb32017-09-21 13:23:49 -04001357 } while (now_ms() < stop);
1358 }
1359
mtkleinbbba1682015-10-28 11:36:30 -07001360 if (FLAGS_ms) {
1361 samples.reset();
1362 auto stop = now_ms() + FLAGS_ms;
1363 do {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001364 samples.push_back(time(loops, bench.get(), target) / loops);
Jim Van Verth49d6bca2020-02-28 11:05:44 -05001365 pool.drain();
mtkleinbbba1682015-10-28 11:36:30 -07001366 } while (now_ms() < stop);
1367 } else {
cdaltone1b89582015-06-25 19:17:08 -07001368 samples.reset(FLAGS_samples);
1369 for (int s = 0; s < FLAGS_samples; s++) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001370 samples[s] = time(loops, bench.get(), target) / loops;
Jim Van Verth49d6bca2020-02-28 11:05:44 -05001371 pool.drain();
cdaltone1b89582015-06-25 19:17:08 -07001372 }
cdaltone1b89582015-06-25 19:17:08 -07001373 }
mtkleinf3723212014-06-25 14:08:00 -07001374
Mike Kleinfec9b902019-05-31 10:03:07 -05001375 // Scale each result to the benchmark's own units, time/unit.
1376 for (double& sample : samples) {
1377 sample *= (1.0 / bench->getUnits());
1378 }
1379
joshualitte45c81c2015-12-02 09:05:37 -08001380 SkTArray<SkString> keys;
1381 SkTArray<double> values;
Chris Dalton5dfb3f42021-04-30 17:16:12 -06001382 if (configs[i].backend == Benchmark::kGPU_Backend) {
1383 if (FLAGS_gpuStatsDump) {
1384 // TODO cache stats
1385 bench->getGpuStats(canvas, &keys, &values);
1386 }
1387 if (FLAGS_dmsaaStatsDump && bench->getDMSAAStats(canvas->recordingContext())) {
1388 const auto& dmsaaStats = canvas->recordingContext()->priv().dmsaaStats();
1389 dmsaaStats.dumpKeyValuePairs(&keys, &values);
1390 dmsaaStats.dump();
1391 combinedDMSAAStats.merge(dmsaaStats);
1392 }
joshualitte45c81c2015-12-02 09:05:37 -08001393 }
joshualitte45c81c2015-12-02 09:05:37 -08001394
robertphillips5b693772014-11-21 06:19:36 -08001395 bench->perCanvasPostDraw(canvas);
1396
egdaniel3bf92062015-06-26 08:12:46 -07001397 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001398 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001399 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001400 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001401 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001402 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001403 }
1404
Mike Klein512a2442018-09-27 10:44:56 -04001405 // Building stats.plot often shows up in profiles,
1406 // so skip building it when we're not going to print it anyway.
1407 const bool want_plot = !FLAGS_quiet;
1408
1409 Stats stats(samples, want_plot);
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001410 log.beginObject(config);
1411
1412 log.beginObject("options");
1413 log.appendString("name", bench->getName());
1414 benchStream.fillCurrentOptions(log);
1415 target->fillOptions(log);
1416 log.endObject(); // options
1417
1418 // Metrics
1419 log.appendMetric("min_ms", stats.min);
Joe Gregoriof4f9c3b2021-06-01 09:39:11 -04001420 log.appendMetric("min_ratio", sk_ieee_double_divide(stats.median, stats.min));
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001421 log.beginArray("samples");
1422 for (double sample : samples) {
1423 log.appendDoubleDigits(sample, 16);
1424 }
1425 log.endArray(); // samples
1426 benchStream.fillCurrentMetrics(log);
Chris Dalton5dfb3f42021-04-30 17:16:12 -06001427 if (!keys.empty()) {
joshualitte45c81c2015-12-02 09:05:37 -08001428 // dump to json, only SKPBench currently returns valid keys / values
1429 SkASSERT(keys.count() == values.count());
1430 for (int i = 0; i < keys.count(); i++) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001431 log.appendMetric(keys[i].c_str(), values[i]);
joshualitte45c81c2015-12-02 09:05:37 -08001432 }
1433 }
joshualitte45c81c2015-12-02 09:05:37 -08001434
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001435 log.endObject(); // config
1436
mtkleine070c2b2014-10-14 08:40:43 -07001437 if (runs++ % FLAGS_flushEvery == 0) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001438 log.flush();
mtkleine070c2b2014-10-14 08:40:43 -07001439 }
mtklein60317d0f2014-07-14 11:30:37 -07001440
bsalomon6eb03cc2014-08-07 14:28:50 -07001441 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001442 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001443 config = ""; // Only print the config if we run the same bench on more than one.
1444 }
mtkleind75c4662015-04-30 07:11:22 -07001445 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1446 , sk_tools::getCurrResidentSetSizeMB()
1447 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001448 , bench->getUniqueName()
1449 , config);
mtkleinf3723212014-06-25 14:08:00 -07001450 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001451 const char* mark = " ";
Hal Canarye99e8da2019-01-14 16:34:50 -05001452 const double stddev_percent =
1453 sk_ieee_double_divide(100 * sqrt(stats.var), stats.mean);
mtklein66cfcff2015-12-04 06:35:30 -08001454 if (stddev_percent > 5) mark = "?";
1455 if (stddev_percent > 10) mark = "!";
1456
1457 SkDebugf("%10.2f %s\t%s\t%s\n",
1458 stats.median*1e3, mark, bench->getUniqueName(), config);
Mike Reedc665e5b2017-07-04 22:56:06 -04001459 } else if (FLAGS_csv) {
Hal Canarye99e8da2019-01-14 16:34:50 -05001460 const double stddev_percent =
1461 sk_ieee_double_divide(100 * sqrt(stats.var), stats.mean);
Mike Reedc665e5b2017-07-04 22:56:06 -04001462 SkDebugf("%g,%g,%g,%g,%g,%s,%s\n"
1463 , stats.min
1464 , stats.median
1465 , stats.mean
1466 , stats.max
1467 , stddev_percent
1468 , config
1469 , bench->getUniqueName()
1470 );
1471 } else {
1472 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 -05001473 const double stddev_percent =
1474 sk_ieee_double_divide(100 * sqrt(stats.var), stats.mean);
Mike Reedc665e5b2017-07-04 22:56:06 -04001475 SkDebugf(format
mtkleind75c4662015-04-30 07:11:22 -07001476 , sk_tools::getCurrResidentSetSizeMB()
1477 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001478 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001479 , HUMANIZE(stats.min)
1480 , HUMANIZE(stats.median)
1481 , HUMANIZE(stats.mean)
1482 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001483 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001484 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001485 , config
mtklein96289052014-09-10 12:05:59 -07001486 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001487 );
1488 }
joshualitte45c81c2015-12-02 09:05:37 -08001489
joshualitte45c81c2015-12-02 09:05:37 -08001490 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -04001491 target->dumpStats();
bsalomon06cddec2014-10-24 10:40:50 -07001492 }
joshualitte45c81c2015-12-02 09:05:37 -08001493
cdalton2c56ba52015-06-26 13:32:53 -07001494 if (FLAGS_verbose) {
1495 SkDebugf("Samples: ");
1496 for (int i = 0; i < samples.count(); i++) {
1497 SkDebugf("%s ", HUMANIZE(samples[i]));
1498 }
1499 SkDebugf("%s\n", bench->getUniqueName());
1500 }
egdaniel3bf92062015-06-26 08:12:46 -07001501 cleanup_run(target);
Jim Van Verth8a9a3712019-05-31 10:49:12 -04001502 pool.drain();
mtkleinf3723212014-06-25 14:08:00 -07001503 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001504 if (!configs.empty()) {
1505 log.endBench();
1506 }
mtkleinf3723212014-06-25 14:08:00 -07001507 }
1508
Chris Dalton5dfb3f42021-04-30 17:16:12 -06001509 if (FLAGS_dmsaaStatsDump) {
1510 SkDebugf("<<Total Combined DMSAA Stats>>\n");
1511 combinedDMSAAStats.dump();
1512 }
1513
Herb Derby5a523fe2017-01-26 16:48:28 -05001514 SkGraphics::PurgeAllCaches();
1515
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001516 log.beginBench("memory_usage", 0, 0);
1517 log.beginObject("meta"); // config
1518 log.appendS32("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1519 log.endObject(); // config
1520 log.endBench();
1521
Brian Osman68870aa2020-07-08 14:12:43 -04001522 RunSkSLMemoryBenchmarks(&log);
1523
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001524 log.endObject(); // results
1525 log.endObject(); // root
1526 log.flush();
mtkleine1091452014-12-04 10:47:02 -08001527
mtkleinf3723212014-06-25 14:08:00 -07001528 return 0;
1529}