blob: 87789c1f44314589fed84e4b266d248478aebb76 [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"
Brian Osman979b5ae2021-08-12 12:21:39 -040033#include "include/private/SkTOptional.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/core/SkAutoMalloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/core/SkColorSpacePriv.h"
Ben Wagnerab6eefe2019-05-20 11:02:49 -040036#include "src/core/SkLeanWindows.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/core/SkOSFile.h"
38#include "src/core/SkTaskGroup.h"
39#include "src/core/SkTraceEvent.h"
John Stiles2ef627a2021-03-23 10:25:02 -040040#include "src/gpu/GrShaderUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041#include "src/utils/SkJSONWriter.h"
42#include "src/utils/SkOSPath.h"
Jim Van Verth8a9a3712019-05-31 10:49:12 -040043#include "tools/AutoreleasePool.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "tools/CrashHandler.h"
Brian Salomon31fddc32021-04-30 13:08:55 -040045#include "tools/MSKPPlayer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050046#include "tools/ProcStats.h"
47#include "tools/Stats.h"
48#include "tools/flags/CommonFlags.h"
49#include "tools/flags/CommonFlagsConfig.h"
50#include "tools/ios_utils.h"
51#include "tools/trace/EventTracingPriv.h"
52#include "tools/trace/SkDebugfTracer.h"
mtkleinf3723212014-06-25 14:08:00 -070053
Robert Phillips2af13c12021-09-01 16:47:01 +000054#if defined(SK_ENABLE_SVG)
Florin Malitab3418102020-10-15 18:10:29 -040055#include "modules/svg/include/SkSVGDOM.h"
Robert Phillips2af13c12021-09-01 16:47:01 +000056#include "modules/svg/include/SkSVGNode.h"
57#endif
Hal Canary0f666812018-03-22 15:21:12 -040058
Leon Scroggins III87caae62020-05-04 10:02:45 -040059#ifdef SK_ENABLE_ANDROID_UTILS
60#include "bench/BitmapRegionDecoderBench.h"
61#include "client_utils/android/BitmapRegionDecoder.h"
62#endif
63
Adlai Holler684838f2020-05-12 10:41:04 -040064#include <cinttypes>
bungeman60e0fee2015-08-26 05:15:46 -070065#include <stdlib.h>
John Stilesfbd050b2020-08-03 13:21:46 -040066#include <memory>
Mike Klein03141d22017-10-30 11:57:15 -040067#include <thread>
bungeman60e0fee2015-08-26 05:15:46 -070068
Mike Reedc928fe22017-06-05 10:20:31 -040069extern bool gSkForceRasterPipelineBlitter;
Mike Reed0e2fb662021-07-23 11:00:40 -040070extern bool gForceHighPrecisionRasterPipeline;
Mike Klein2a57e792020-01-14 12:47:52 -060071extern bool gUseSkVMBlitter;
Mike Klein813e8cc2020-08-05 09:33:38 -050072extern bool gSkVMAllowJIT;
Mike Kleinea4459d2020-01-16 13:05:04 -060073extern bool gSkVMJITViaDylib;
Mike Reedc928fe22017-06-05 10:20:31 -040074
Mike Klein8f11d4d2018-01-24 12:42:55 -050075#ifndef SK_BUILD_FOR_WIN
scroggo38ce0a72016-01-07 07:28:47 -080076 #include <unistd.h>
Brian Salomon032aaae2018-03-23 16:10:36 -040077
scroggo38ce0a72016-01-07 07:28:47 -080078#endif
79
Robert Phillips00f78de2020-07-01 16:09:43 -040080#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050081#include "src/gpu/GrCaps.h"
Adlai Hollera0693042020-10-14 11:23:11 -040082#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050083#include "src/gpu/SkGr.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050084#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 Reed0e2fb662021-07-23 11:00:40 -0400143static DEFINE_bool(forceRasterPipelineHP, false, "sets gSkForceRasterPipelineBlitter and gForceHighPrecisionRasterPipeline");
Mike Klein813e8cc2020-08-05 09:33:38 -0500144static DEFINE_bool(skvm, false, "sets gUseSkVMBlitter");
Mike Kleinf2b3f912021-03-23 14:32:45 -0500145static DEFINE_bool(jit, true, "JIT SkVM?");
146static DEFINE_bool(dylib, false, "JIT via dylib (much slower compile but easier to debug/profile)");
Mike Reedc928fe22017-06-05 10:20:31 -0400147
Mike Klein19fb3972019-03-21 13:08:08 -0500148static DEFINE_bool2(pre_log, p, false,
149 "Log before running each test. May be incomprehensible when threading");
150
Leon Scroggins III1ff07062020-07-27 14:52:19 -0400151static DEFINE_bool(cpu, true, "Run CPU-bound work?");
152static DEFINE_bool(gpu, true, "Run GPU-bound work?");
Mike Klein629f5fc2019-03-22 14:55:19 -0500153static DEFINE_bool(dryRun, false,
154 "just print the tests that would be run, without actually running them.");
155static DEFINE_string(images, "",
156 "List of images and/or directories to decode. A directory with no images"
157 " is treated as a fatal error.");
158static DEFINE_bool(simpleCodec, false,
159 "Runs of a subset of the codec tests, always N32, Premul or Opaque");
160
Mike Kleinc6142d82019-03-25 10:54:59 -0500161static DEFINE_string2(match, m, nullptr,
162 "[~][^]substring[$] [...] of name to run.\n"
163 "Multiple matches may be separated by spaces.\n"
164 "~ causes a matching name to always be skipped\n"
165 "^ requires the start of the name to match\n"
166 "$ requires the end of the name to match\n"
167 "^ and $ requires an exact match\n"
168 "If a name does not match any list entry,\n"
169 "it is skipped unless some list entry starts with ~");
170
171static DEFINE_bool2(quiet, q, false, "if true, don't print status updates.");
172static DEFINE_bool2(verbose, v, false, "enable verbose output from the test driver.");
173
174
175static DEFINE_string(skps, "skps", "Directory to read skps from.");
Brian Salomon31fddc32021-04-30 13:08:55 -0400176static DEFINE_string(mskps, "mskps", "Directory to read mskps from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500177static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
Hal Canary5dfefa22019-10-24 13:52:17 -0400178static DEFINE_string(texttraces, "", "Directory to read TextBlobTrace files from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500179
180static DEFINE_int_2(threads, j, -1,
181 "Run threadsafe tests on a threadpool with this many extra threads, "
182 "defaulting to one extra thread per core.");
183
184static DEFINE_string2(writePath, w, "", "If set, write bitmaps here as .pngs.");
185
186static DEFINE_string(key, "",
187 "Space-separated key/value pairs to add to JSON identifying this builder.");
188static DEFINE_string(properties, "",
189 "Space-separated key/value pairs to add to JSON identifying this run.");
190
Mike Kleinee254232019-03-28 08:32:14 -0500191static DEFINE_bool(purgeBetweenBenches, false,
192 "Call SkGraphics::PurgeAllCaches() between each benchmark?");
193
mtkleinbbba1682015-10-28 11:36:30 -0700194static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
195
mtkleinf3723212014-06-25 14:08:00 -0700196static SkString humanize(double ms) {
Adlai Holler684838f2020-05-12 10:41:04 -0400197 if (FLAGS_verbose) return SkStringPrintf("%" PRIu64, (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800198 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700199}
mtklein55b0ffc2014-07-17 08:38:23 -0700200#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700201
tomhudsond968a6f2015-03-26 11:28:06 -0700202bool Target::init(SkImageInfo info, Benchmark* bench) {
203 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700204 this->surface = SkSurface::MakeRaster(info);
205 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700206 return false;
207 }
208 }
209 return true;
210}
211bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700212 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700213 if (!canvas) {
214 return false;
215 }
Mike Reed12e946b2017-04-17 10:53:29 -0400216 bmp->allocPixels(canvas->imageInfo());
217 if (!canvas->readPixels(*bmp, 0, 0)) {
tomhudsond968a6f2015-03-26 11:28:06 -0700218 SkDebugf("Can't read canvas pixels.\n");
219 return false;
220 }
221 return true;
222}
223
tomhudsond968a6f2015-03-26 11:28:06 -0700224struct GPUTarget : public Target {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400225 explicit GPUTarget(const Config& c) : Target(c) {}
226 ContextInfo contextInfo;
227 std::unique_ptr<GrContextFactory> factory;
tomhudsond968a6f2015-03-26 11:28:06 -0700228
Greg Daniel5ed3c112020-06-18 15:59:17 -0400229 ~GPUTarget() override {
230 // For Vulkan we need to release all our refs to the GrContext before destroy the vulkan
231 // context which happens at the end of this destructor. Thus we need to release the surface
232 // here which holds a ref to the GrContext.
233 surface.reset();
234 }
235
tomhudsond968a6f2015-03-26 11:28:06 -0700236 void setup() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400237 this->contextInfo.testContext()->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700238 // Make sure we're done with whatever came before.
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400239 this->contextInfo.testContext()->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700240 }
241 void endTiming() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400242 if (this->contextInfo.testContext()) {
Robert Phillips00f78de2020-07-01 16:09:43 -0400243 this->contextInfo.testContext()->flushAndWaitOnSync(contextInfo.directContext());
tomhudsond968a6f2015-03-26 11:28:06 -0700244 }
245 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400246 void fence() override { this->contextInfo.testContext()->finish(); }
mtkleind75c4662015-04-30 07:11:22 -0700247
cdaltond416a5b2015-06-23 13:23:44 -0700248 bool needsFrameTiming(int* maxFrameLag) const override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400249 if (!this->contextInfo.testContext()->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700250 // Frame lag is unknown.
251 *maxFrameLag = FLAGS_gpuFrameLag;
252 }
253 return true;
254 }
tomhudsond968a6f2015-03-26 11:28:06 -0700255 bool init(SkImageInfo info, Benchmark* bench) override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400256 GrContextOptions options = grContextOpts;
257 bench->modifyGrContextOptions(&options);
John Stilesfbd050b2020-08-03 13:21:46 -0400258 this->factory = std::make_unique<GrContextFactory>(options);
Chris Dalton180b4a12021-03-16 20:49:15 -0600259 SkSurfaceProps props(this->config.surfaceFlags, kRGB_H_SkPixelGeometry);
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400260 this->surface = SkSurface::MakeRenderTarget(
261 this->factory->get(this->config.ctxType, this->config.ctxOverrides),
262 SkBudgeted::kNo, info, this->config.samples, &props);
263 this->contextInfo =
264 this->factory->getContextInfo(this->config.ctxType, this->config.ctxOverrides);
John Stilesa008b0f2020-08-16 08:48:02 -0400265 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700266 return false;
267 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400268 if (!this->contextInfo.testContext()->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700269 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800270 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700271 }
tomhudsond968a6f2015-03-26 11:28:06 -0700272 return true;
273 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400274
275 void dumpStats() override {
Robert Phillips00f78de2020-07-01 16:09:43 -0400276 auto context = this->contextInfo.directContext();
277
278 context->priv().printCacheStats();
279 context->priv().printGpuStats();
280 context->priv().printContextStats();
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400281 }
tomhudsond968a6f2015-03-26 11:28:06 -0700282};
mtkleind75c4662015-04-30 07:11:22 -0700283
tomhudson75a0ebb2015-03-27 12:11:44 -0700284static double time(int loops, Benchmark* bench, Target* target) {
285 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700286 if (canvas) {
287 canvas->clear(SK_ColorWHITE);
288 }
joshualitt8a6697a2015-09-30 12:11:07 -0700289 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700290 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700291 canvas = target->beginTiming(canvas);
292 bench->draw(loops, canvas);
tomhudson75a0ebb2015-03-27 12:11:44 -0700293 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700294 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700295 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700296 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700297}
298
mtkleinf3723212014-06-25 14:08:00 -0700299static double estimate_timer_overhead() {
300 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700301 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700302 double start = now_ms();
303 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700304 }
305 return overhead / FLAGS_overheadLoops;
306}
307
reed53249782014-10-10 09:09:52 -0700308static int detect_forever_loops(int loops) {
309 // look for a magic run-forever value
310 if (loops < 0) {
311 loops = SK_MaxS32;
312 }
313 return loops;
314}
315
mtklein55b0ffc2014-07-17 08:38:23 -0700316static int clamp_loops(int loops) {
317 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800318 SkDebugf("ERROR: clamping loops from %d to 1. "
319 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700320 return 1;
321 }
322 if (loops > FLAGS_maxLoops) {
323 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
324 return FLAGS_maxLoops;
325 }
326 return loops;
327}
328
tomhudsond968a6f2015-03-26 11:28:06 -0700329static bool write_canvas_png(Target* target, const SkString& filename) {
330
bsalomon6eb03cc2014-08-07 14:28:50 -0700331 if (filename.isEmpty()) {
332 return false;
333 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700334 if (target->getCanvas() &&
335 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700336 return false;
337 }
tomhudsond968a6f2015-03-26 11:28:06 -0700338
bsalomon6eb03cc2014-08-07 14:28:50 -0700339 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700340
341 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700342 return false;
343 }
tomhudsond968a6f2015-03-26 11:28:06 -0700344
bsalomon6eb03cc2014-08-07 14:28:50 -0700345 SkString dir = SkOSPath::Dirname(filename.c_str());
346 if (!sk_mkdir(dir.c_str())) {
347 SkDebugf("Can't make dir %s.\n", dir.c_str());
348 return false;
349 }
350 SkFILEWStream stream(filename.c_str());
351 if (!stream.isValid()) {
352 SkDebugf("Can't write %s.\n", filename.c_str());
353 return false;
354 }
Hal Canarydb683012016-11-23 08:55:18 -0700355 if (!SkEncodeImage(&stream, bmp, SkEncodedImageFormat::kPNG, 100)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700356 SkDebugf("Can't encode a PNG.\n");
357 return false;
358 }
359 return true;
360}
361
362static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700363static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700364 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700365 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700366 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700367 int loops = bench->calculateLoops(FLAGS_loops);
368 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700369 while (bench_plus_overhead < overhead) {
370 if (round++ == FLAGS_maxCalibrationAttempts) {
371 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700372 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700373 return kFailedLoops;
374 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700375 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700376 }
mtklein2069e222014-08-04 13:57:39 -0700377 }
mtkleinf3723212014-06-25 14:08:00 -0700378
mtkleinbb6a0282014-07-01 08:43:42 -0700379 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700380 // We'll pick N to make timer overhead negligible:
381 //
mtkleinbb6a0282014-07-01 08:43:42 -0700382 // overhead
383 // ------------------------- < FLAGS_overheadGoal
384 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700385 //
Hal Canary55325b72017-01-03 10:36:17 -0500386 // where bench_plus_overhead ~=~ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700387 //
388 // Doing some math, we get:
389 //
mtkleinbb6a0282014-07-01 08:43:42 -0700390 // (overhead / FLAGS_overheadGoal) - overhead
391 // ------------------------------------------ < N
392 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700393 //
394 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700395 if (kAutoTuneLoops == loops) {
396 const double numer = overhead / FLAGS_overheadGoal - overhead;
397 const double denom = bench_plus_overhead - overhead;
398 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700399 loops = clamp_loops(loops);
400 } else {
401 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700402 }
mtkleinbb6a0282014-07-01 08:43:42 -0700403
mtkleinbb6a0282014-07-01 08:43:42 -0700404 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700405}
406
cdaltone1b89582015-06-25 19:17:08 -0700407static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700408 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700409 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700410 if (kAutoTuneLoops == loops) {
411 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700412 double elapsed = 0;
413 do {
mtklein527930f2014-11-06 08:04:34 -0800414 if (1<<30 == loops) {
415 // We're about to wrap. Something's wrong with the bench.
416 loops = 0;
417 break;
418 }
mtkleina189ccd2014-07-14 12:28:47 -0700419 loops *= 2;
420 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700421 // _this_ round, not still timing last round.
422 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700423 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700424 }
425 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700426
mtkleina189ccd2014-07-14 12:28:47 -0700427 // We've overshot at least a little. Scale back linearly.
428 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700429 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700430
tomhudsond968a6f2015-03-26 11:28:06 -0700431 // Make sure we're not still timing our calibration.
432 target->fence();
reed53249782014-10-10 09:09:52 -0700433 } else {
434 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700435 }
mtkleinbb6a0282014-07-01 08:43:42 -0700436 // Pretty much the same deal as the calibration: do some warmup to make
437 // sure we're timing steady-state pipelined frames.
Brian Osman01776982017-10-10 15:41:03 -0400438 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700439 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700440 }
mtkleinbb6a0282014-07-01 08:43:42 -0700441
mtkleinbb6a0282014-07-01 08:43:42 -0700442 return loops;
443}
mtkleinbb6a0282014-07-01 08:43:42 -0700444
Brian Salomon6405e712017-03-20 08:54:16 -0400445#define kBogusContextType GrContextFactory::kGL_ContextType
csmartdaltone812d492017-02-21 12:36:05 -0700446#define kBogusContextOverrides GrContextFactory::ContextOverrides::kNone
bsalomonc2553372014-07-22 13:09:05 -0700447
Brian Osmanfcb50232021-08-12 10:51:48 -0400448static skstd::optional<Config> create_config(const SkCommandLineConfig* config) {
svaisanenc47635e2016-01-28 06:05:43 -0800449 if (const auto* gpuConfig = config->asConfigGpu()) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400450 if (!FLAGS_gpu) {
451 SkDebugf("Skipping config '%s' as requested.\n", config->getTag().c_str());
Brian Osman979b5ae2021-08-12 12:21:39 -0400452 return skstd::nullopt;
Ben Wagner32fa5102017-08-10 21:25:55 -0400453 }
svaisanenc47635e2016-01-28 06:05:43 -0800454
svaisanenc47635e2016-01-28 06:05:43 -0800455 const auto ctxType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700456 const auto ctxOverrides = gpuConfig->getContextOverrides();
svaisanenc47635e2016-01-28 06:05:43 -0800457 const auto sampleCount = gpuConfig->getSamples();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400458 const auto colorType = gpuConfig->getColorType();
Brian Salomonf865b052018-03-09 09:01:53 -0500459 if (gpuConfig->getSurfType() != SkCommandLineConfigGpu::SurfType::kDefault) {
460 SkDebugf("This tool only supports the default surface type.");
Brian Osman979b5ae2021-08-12 12:21:39 -0400461 return skstd::nullopt;
Brian Salomonf865b052018-03-09 09:01:53 -0500462 }
svaisanenc47635e2016-01-28 06:05:43 -0800463
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400464 GrContextFactory factory(grContextOpts);
Robert Phillipsf0288102020-07-06 13:45:34 -0400465 if (const auto ctx = factory.get(ctxType, ctxOverrides)) {
Greg Daniel6fa62e22019-08-07 15:52:37 -0400466 GrBackendFormat format = ctx->defaultBackendFormat(colorType, GrRenderable::kYes);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500467 int supportedSampleCount =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400468 ctx->priv().caps()->getRenderTargetSampleCount(sampleCount, format);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400469 if (sampleCount != supportedSampleCount) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400470 SkDebugf("Configuration '%s' sample count %d is not a supported sample count.\n",
Brian Osman979b5ae2021-08-12 12:21:39 -0400471 config->getTag().c_str(),
472 sampleCount);
473 return skstd::nullopt;
svaisanenc47635e2016-01-28 06:05:43 -0800474 }
475 } else {
Brian Osman979b5ae2021-08-12 12:21:39 -0400476 SkDebugf("No context was available matching config '%s'.\n", config->getTag().c_str());
477 return skstd::nullopt;
svaisanenc47635e2016-01-28 06:05:43 -0800478 }
479
Brian Osman979b5ae2021-08-12 12:21:39 -0400480 return Config{gpuConfig->getTag(),
481 Benchmark::kGPU_Backend,
482 colorType,
483 kPremul_SkAlphaType,
Brian Osmanfcb50232021-08-12 10:51:48 -0400484 config->refColorSpace(),
Brian Osman979b5ae2021-08-12 12:21:39 -0400485 sampleCount,
486 ctxType,
487 ctxOverrides,
488 gpuConfig->getSurfaceFlags()};
489 }
svaisanenc47635e2016-01-28 06:05:43 -0800490
Brian Osman979b5ae2021-08-12 12:21:39 -0400491#define CPU_CONFIG(name, backend, color, alpha) \
492 if (config->getBackend().equals(name)) { \
493 if (!FLAGS_cpu) { \
494 SkDebugf("Skipping config '%s' as requested.\n", config->getTag().c_str()); \
495 return skstd::nullopt; \
496 } \
497 return Config{SkString(name), \
498 Benchmark::backend, \
499 color, \
500 alpha, \
Brian Osmanfcb50232021-08-12 10:51:48 -0400501 config->refColorSpace(), \
Brian Osman979b5ae2021-08-12 12:21:39 -0400502 0, \
503 kBogusContextType, \
504 kBogusContextOverrides, \
505 0}; \
506 }
507
508 CPU_CONFIG("nonrendering", kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
509
Brian Osman9f1e06a2021-08-10 14:39:18 -0400510 CPU_CONFIG("a8", kRaster_Backend, kAlpha_8_SkColorType, kPremul_SkAlphaType)
511 CPU_CONFIG("565", kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
512 CPU_CONFIG("8888", kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
513 CPU_CONFIG("rgba", kRaster_Backend, kRGBA_8888_SkColorType, kPremul_SkAlphaType)
514 CPU_CONFIG("bgra", kRaster_Backend, kBGRA_8888_SkColorType, kPremul_SkAlphaType)
515 CPU_CONFIG("f16", kRaster_Backend, kRGBA_F16_SkColorType, kPremul_SkAlphaType)
516 CPU_CONFIG("srgba", kRaster_Backend, kSRGBA_8888_SkColorType, kPremul_SkAlphaType)
Brian Osman979b5ae2021-08-12 12:21:39 -0400517
518#undef CPU_CONFIG
519
520 SkDebugf("Unknown config '%s'.\n", config->getTag().c_str());
521 return skstd::nullopt;
522}
523
svaisanenc47635e2016-01-28 06:05:43 -0800524// Append all configs that are enabled and supported.
525void create_configs(SkTArray<Config>* configs) {
526 SkCommandLineConfigArray array;
527 ParseConfigs(FLAGS_config, &array);
528 for (int i = 0; i < array.count(); ++i) {
Brian Osmanfcb50232021-08-12 10:51:48 -0400529 if (skstd::optional<Config> config = create_config(array[i].get())) {
530 configs->push_back(*config);
531 }
svaisanenc47635e2016-01-28 06:05:43 -0800532 }
Ben Wagner32fa5102017-08-10 21:25:55 -0400533
534 // If no just default configs were requested, then we're okay.
535 if (array.count() == 0 || FLAGS_config.count() == 0 ||
Ben Wagner32fa5102017-08-10 21:25:55 -0400536 // Otherwise, make sure that all specified configs have been created.
537 array.count() == configs->count()) {
538 return;
539 }
Ben Wagner32fa5102017-08-10 21:25:55 -0400540 exit(1);
svaisanenc47635e2016-01-28 06:05:43 -0800541}
542
brianosman9f1f6e22016-09-15 08:33:02 -0700543// disable warning : switch statement contains default but no 'case' labels
544#if defined _WIN32
545#pragma warning ( push )
546#pragma warning ( disable : 4065 )
547#endif
548
halcanary96fcdcc2015-08-27 07:41:13 -0700549// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700550static Target* is_enabled(Benchmark* bench, const Config& config) {
551 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700552 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700553 }
554
reede5ea5002014-09-03 11:54:58 -0700555 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700556 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700557
halcanary96fcdcc2015-08-27 07:41:13 -0700558 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700559
tomhudsond968a6f2015-03-26 11:28:06 -0700560 switch (config.backend) {
tomhudsond968a6f2015-03-26 11:28:06 -0700561 case Benchmark::kGPU_Backend:
562 target = new GPUTarget(config);
563 break;
tomhudsond968a6f2015-03-26 11:28:06 -0700564 default:
565 target = new Target(config);
566 break;
567 }
bsalomonc2553372014-07-22 13:09:05 -0700568
tomhudsond968a6f2015-03-26 11:28:06 -0700569 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700570 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700571 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700572 }
573 return target;
574}
575
brianosman9f1f6e22016-09-15 08:33:02 -0700576#if defined _WIN32
577#pragma warning ( pop )
578#endif
579
Leon Scroggins III87caae62020-05-04 10:02:45 -0400580#ifdef SK_ENABLE_ANDROID_UTILS
reed42943c82016-09-12 12:01:44 -0700581static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700582 uint32_t minOutputSize, int* width, int* height) {
Leon Scroggins III87caae62020-05-04 10:02:45 -0400583 auto brd = android::skia::BitmapRegionDecoder::Make(encoded);
John Stilesa008b0f2020-08-16 08:48:02 -0400584 if (nullptr == brd) {
msarett7f691442015-09-22 11:56:16 -0700585 // This is indicates that subset decoding is not supported for a particular image format.
586 return false;
587 }
588
msarett7f691442015-09-22 11:56:16 -0700589 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
590 (uint32_t) brd->height()) {
591 // This indicates that the image is not large enough to decode a
592 // minOutputSize x minOutputSize subset at the given sampleSize.
593 return false;
594 }
595
596 // Set the image width and height. The calling code will use this to choose subsets to decode.
597 *width = brd->width();
598 *height = brd->height();
599 return true;
600}
Leon Scroggins III87caae62020-05-04 10:02:45 -0400601#endif
msarett7f691442015-09-22 11:56:16 -0700602
egdaniel3bf92062015-06-26 08:12:46 -0700603static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700604 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700605}
606
Mike Klein88544fb2019-03-20 10:50:33 -0500607static void collect_files(const CommandLineFlags::StringArray& paths,
608 const char* ext,
609 SkTArray<SkString>* list) {
fmalita6519c212016-09-14 08:05:17 -0700610 for (int i = 0; i < paths.count(); ++i) {
611 if (SkStrEndsWith(paths[i], ext)) {
612 list->push_back(SkString(paths[i]));
613 } else {
614 SkOSFile::Iter it(paths[i], ext);
615 SkString path;
616 while (it.next(&path)) {
617 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
618 }
619 }
620 }
621}
622
mtkleine714e752014-07-31 12:13:48 -0700623class BenchmarkStream {
624public:
mtklein92007582014-08-01 07:46:52 -0700625 BenchmarkStream() : fBenches(BenchRegistry::Head())
Hal Canary2362ae72019-10-24 13:40:40 -0400626 , fGMs(skiagm::GMRegistry::Head()) {
fmalita6519c212016-09-14 08:05:17 -0700627 collect_files(FLAGS_skps, ".skp", &fSKPs);
Brian Salomon31fddc32021-04-30 13:08:55 -0400628 collect_files(FLAGS_mskps, ".mskp", &fMSKPs);
fmalita6519c212016-09-14 08:05:17 -0700629 collect_files(FLAGS_svgs, ".svg", &fSVGs);
Hal Canary5dfefa22019-10-24 13:52:17 -0400630 collect_files(FLAGS_texttraces, ".trace", &fTextBlobTraces);
mtkleine714e752014-07-31 12:13:48 -0700631
mtklein92007582014-08-01 07:46:52 -0700632 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
633 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
634 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
635 exit(1);
636 }
637
638 for (int i = 0; i < FLAGS_scales.count(); i++) {
639 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
640 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
641 exit(1);
642 }
643 }
robertphillips5b693772014-11-21 06:19:36 -0800644
cdalton63a82852015-06-29 14:06:10 -0700645 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
646 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700647 exit(1);
648 }
649
msarett95f192d2015-02-13 09:05:41 -0800650 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700651 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800652 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800653 }
mtklein95553d92015-03-12 08:24:21 -0700654
msarett95f192d2015-02-13 09:05:41 -0800655 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700656 fColorTypes.push_back(kN32_SkColorType);
657 if (!FLAGS_simpleCodec) {
658 fColorTypes.push_back(kRGB_565_SkColorType);
659 fColorTypes.push_back(kAlpha_8_SkColorType);
msarett67cb6662016-06-21 08:49:26 -0700660 fColorTypes.push_back(kGray_8_SkColorType);
661 }
mtklein92007582014-08-01 07:46:52 -0700662 }
663
reedca2622b2016-03-18 07:25:55 -0700664 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700665 // Not strictly necessary, as it will be checked again later,
666 // but helps to avoid a lot of pointless work if we're going to skip it.
Mike Klein88544fb2019-03-20 10:50:33 -0500667 if (CommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700668 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700669 }
670
bungemanf93d7112016-09-16 06:24:20 -0700671 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
672 if (!stream) {
mtkleinfd731ce2014-09-10 12:19:30 -0700673 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700674 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700675 }
676
reedca2622b2016-03-18 07:25:55 -0700677 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700678 }
679
Brian Salomon31fddc32021-04-30 13:08:55 -0400680 static std::unique_ptr<MSKPPlayer> ReadMSKP(const char* path) {
681 // Not strictly necessary, as it will be checked again later,
682 // but helps to avoid a lot of pointless work if we're going to skip it.
683 if (CommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
684 return nullptr;
685 }
686
687 std::unique_ptr<SkStreamSeekable> stream = SkStream::MakeFromFile(path);
688 if (!stream) {
689 SkDebugf("Could not read %s.\n", path);
690 return nullptr;
691 }
692
693 return MSKPPlayer::Make(stream.get());
694 }
695
fmalita6519c212016-09-14 08:05:17 -0700696 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
Mike Klein8ab65bf2020-12-04 09:20:32 -0600697 if (CommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
698 return nullptr;
699 }
Brian Osman133823d2018-09-19 14:14:15 -0400700 sk_sp<SkData> data(SkData::MakeFromFileName(path));
701 if (!data) {
fmalita6519c212016-09-14 08:05:17 -0700702 SkDebugf("Could not read %s.\n", path);
703 return nullptr;
704 }
705
Robert Phillips2af13c12021-09-01 16:47:01 +0000706#if defined(SK_ENABLE_SVG)
Brian Osman133823d2018-09-19 14:14:15 -0400707 SkMemoryStream stream(std::move(data));
fmalitae1baa7c2016-09-14 12:04:30 -0700708 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
fmalita6519c212016-09-14 08:05:17 -0700709 if (!svgDom) {
710 SkDebugf("Could not parse %s.\n", path);
711 return nullptr;
712 }
713
fmalitae1baa7c2016-09-14 12:04:30 -0700714 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
715 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
716 if (svgDom->containerSize().isEmpty()) {
717 svgDom->setContainerSize(kDefaultContainerSize);
718 }
719
fmalita6519c212016-09-14 08:05:17 -0700720 SkPictureRecorder recorder;
fmalitae1baa7c2016-09-14 12:04:30 -0700721 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
722 svgDom->containerSize().height()));
fmalita6519c212016-09-14 08:05:17 -0700723 return recorder.finishRecordingAsPicture();
Hal Canary0f666812018-03-22 15:21:12 -0400724#else
725 return nullptr;
Robert Phillips2af13c12021-09-01 16:47:01 +0000726#endif // defined(SK_ENABLE_SVG)
fmalita6519c212016-09-14 08:05:17 -0700727 }
728
mtklein92007582014-08-01 07:46:52 -0700729 Benchmark* next() {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400730 std::unique_ptr<Benchmark> bench;
mtklein65dfd2f2016-02-03 10:40:54 -0800731 do {
732 bench.reset(this->rawNext());
733 if (!bench) {
734 return nullptr;
735 }
Mike Klein88544fb2019-03-20 10:50:33 -0500736 } while (CommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
737 CommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700738 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800739 }
740
741 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700742 if (fBenches) {
Hal Canary972eba32018-07-30 17:07:07 -0400743 Benchmark* bench = fBenches->get()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700744 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700745 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700746 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700747 return bench;
748 }
mtklein92007582014-08-01 07:46:52 -0700749
mtkleine714e752014-07-31 12:13:48 -0700750 while (fGMs) {
Ben Wagner406ff502019-08-12 16:39:24 -0400751 std::unique_ptr<skiagm::GM> gm = fGMs->get()();
mtkleine714e752014-07-31 12:13:48 -0700752 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800753 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700754 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700755 fBenchType = "micro";
Ben Wagner406ff502019-08-12 16:39:24 -0400756 return new GMBench(std::move(gm));
mtkleine714e752014-07-31 12:13:48 -0700757 }
758 }
mtklein92007582014-08-01 07:46:52 -0700759
Hal Canary5dfefa22019-10-24 13:52:17 -0400760 while (fCurrentTextBlobTrace < fTextBlobTraces.count()) {
761 SkString path = fTextBlobTraces[fCurrentTextBlobTrace++];
762 SkString basename = SkOSPath::Basename(path.c_str());
763 static constexpr char kEnding[] = ".trace";
764 if (basename.endsWith(kEnding)) {
765 basename.remove(basename.size() - strlen(kEnding), strlen(kEnding));
766 }
767 fSourceType = "texttrace";
768 fBenchType = "micro";
769 return CreateDiffCanvasBench(
770 SkStringPrintf("SkDiffBench-%s", basename.c_str()),
771 [path](){ return SkStream::MakeFromFile(path.c_str()); });
772 }
773
mtkleinfd731ce2014-09-10 12:19:30 -0700774 // First add all .skps as RecordingBenches.
775 while (fCurrentRecording < fSKPs.count()) {
776 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700777 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
778 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700779 continue;
780 }
781 SkString name = SkOSPath::Basename(path.c_str());
782 fSourceType = "skp";
783 fBenchType = "recording";
Mike Klein88d90712018-01-27 17:30:04 +0000784 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
785 fSKPOps = pic->approximateOpCount();
Mike Kleina705cb92019-05-14 12:33:40 -0500786 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700787 }
788
Mike Reede45ff462017-12-06 10:47:03 -0500789 // Add all .skps as DeserializePictureBenchs.
790 while (fCurrentDeserialPicture < fSKPs.count()) {
791 const SkString& path = fSKPs[fCurrentDeserialPicture++];
792 sk_sp<SkData> data = SkData::MakeFromFileName(path.c_str());
793 if (!data) {
794 continue;
795 }
796 SkString name = SkOSPath::Basename(path.c_str());
797 fSourceType = "skp";
798 fBenchType = "deserial";
Mike Reed7557bbb2017-12-24 19:50:57 -0500799 fSKPBytes = static_cast<double>(data->size());
Mike Klein88d90712018-01-27 17:30:04 +0000800 fSKPOps = 0;
Mike Reede45ff462017-12-06 10:47:03 -0500801 return new DeserializePictureBench(name.c_str(), std::move(data));
802 }
803
mtkleinfd731ce2014-09-10 12:19:30 -0700804 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700805 while (fCurrentScale < fScales.count()) {
806 while (fCurrentSKP < fSKPs.count()) {
Brian Salomon626349b2020-08-27 10:54:36 -0400807 const SkString& path = fSKPs[fCurrentSKP++];
reedca2622b2016-03-18 07:25:55 -0700808 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
809 if (!pic) {
mtklein92007582014-08-01 07:46:52 -0700810 continue;
811 }
robertphillips5b693772014-11-21 06:19:36 -0800812
Brian Salomon626349b2020-08-27 10:54:36 -0400813 if (FLAGS_bbh) {
814 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
815 SkRTreeFactory factory;
816 SkPictureRecorder recorder;
817 pic->playback(recorder.beginRecording(pic->cullRect().width(),
818 pic->cullRect().height(),
819 &factory));
820 pic = recorder.finishRecordingAsPicture();
mtklein20840502014-08-21 15:51:22 -0700821 }
Brian Salomon626349b2020-08-27 10:54:36 -0400822 SkString name = SkOSPath::Basename(path.c_str());
823 fSourceType = "skp";
824 fBenchType = "playback";
825 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
826 FLAGS_loopSKP);
mtklein92007582014-08-01 07:46:52 -0700827 }
fmalita6519c212016-09-14 08:05:17 -0700828
Brian Salomon626349b2020-08-27 10:54:36 -0400829 while (fCurrentSVG < fSVGs.count()) {
830 const char* path = fSVGs[fCurrentSVG++].c_str();
fmalita6519c212016-09-14 08:05:17 -0700831 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
832 fSourceType = "svg";
833 fBenchType = "playback";
834 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
Brian Salomon626349b2020-08-27 10:54:36 -0400835 fScales[fCurrentScale], FLAGS_loopSKP);
fmalita6519c212016-09-14 08:05:17 -0700836 }
837 }
838
mtklein92007582014-08-01 07:46:52 -0700839 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700840 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700841 fCurrentScale++;
842 }
843
joshualitt261c3ad2015-04-27 09:16:57 -0700844 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700845 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700846 while (fCurrentAnimSKP < fSKPs.count()) {
847 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700848 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
849 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700850 fCurrentAnimSKP++;
851 continue;
852 }
853
854 fCurrentAnimSKP++;
855 SkString name = SkOSPath::Basename(path.c_str());
Ben Wagneracf98df2019-07-12 12:51:44 -0400856 sk_sp<SKPAnimationBench::Animation> animation =
857 SKPAnimationBench::MakeZoomAnimation(fZoomMax, fZoomPeriodMs);
858 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, std::move(animation),
halcanary385fe4d2015-08-26 13:07:48 -0700859 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700860 }
861 }
862
Brian Salomon31fddc32021-04-30 13:08:55 -0400863 // Read all MSKPs as benches
864 while (fCurrentMSKP < fMSKPs.count()) {
865 const SkString& path = fMSKPs[fCurrentMSKP++];
866 std::unique_ptr<MSKPPlayer> player = ReadMSKP(path.c_str());
867 if (!player) {
868 continue;
869 }
870 SkString name = SkOSPath::Basename(path.c_str());
871 fSourceType = "mskp";
872 fBenchType = "mskp";
873 return new MSKPBench(std::move(name), std::move(player));
874 }
875
scroggo60869a42015-04-01 12:09:17 -0700876 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700877 fSourceType = "image";
878 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700879 const SkString& path = fImages[fCurrentCodec];
Mike Klein88544fb2019-03-20 10:50:33 -0500880 if (CommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
mtklein6f0ff912016-01-11 09:04:21 -0800881 continue;
882 }
bungeman38d909e2016-08-02 14:40:46 -0700883 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400884 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700885 if (!codec) {
886 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700887 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700888 continue;
889 }
scroggo21027992015-04-02 13:22:38 -0700890
scroggo60869a42015-04-01 12:09:17 -0700891 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700892 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700893
msarettc7796b92016-01-07 14:20:20 -0800894 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700895 if (FLAGS_simpleCodec) {
896 if (kUnpremul_SkAlphaType == alphaType) {
897 alphaType = kPremul_SkAlphaType;
898 }
899
900 fCurrentColorType++;
901 } else {
902 switch (alphaType) {
903 case kOpaque_SkAlphaType:
904 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800905 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700906 break;
907 case kUnpremul_SkAlphaType:
908 case kPremul_SkAlphaType:
909 if (0 == fCurrentAlphaType) {
910 // Test unpremul first.
911 alphaType = kUnpremul_SkAlphaType;
912 fCurrentAlphaType++;
913 } else {
914 // Test premul.
915 alphaType = kPremul_SkAlphaType;
916 fCurrentAlphaType = 0;
917 fCurrentColorType++;
918 }
919 break;
920 default:
921 SkASSERT(false);
922 fCurrentColorType++;
923 break;
924 }
scroggo21027992015-04-02 13:22:38 -0700925 }
926
msarettc7796b92016-01-07 14:20:20 -0800927 // Make sure we can decode to this color type and alpha type.
928 SkImageInfo info =
929 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700930 const size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400931 SkAutoMalloc storage(info.computeByteSize(rowBytes));
scroggo21027992015-04-02 13:22:38 -0700932
scroggoeb602a52015-07-09 08:16:03 -0700933 const SkCodec::Result result = codec->getPixels(
Leon Scroggins571b30f2017-07-11 17:35:31 +0000934 info, storage.get(), rowBytes);
scroggo60869a42015-04-01 12:09:17 -0700935 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700936 case SkCodec::kSuccess:
937 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700938 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700939 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700940 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700941 // This is okay. Not all conversions are valid.
942 break;
scroggo60869a42015-04-01 12:09:17 -0700943 default:
944 // This represents some sort of failure.
945 SkASSERT(false);
946 break;
947 }
948 }
949 fCurrentColorType = 0;
950 }
951
msarett84451022016-02-11 06:45:51 -0800952 // Run AndroidCodecBenches
953 const int sampleSizes[] = { 2, 4, 8 };
954 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
955 fSourceType = "image";
956 fBenchType = "skandroidcodec";
957
958 const SkString& path = fImages[fCurrentAndroidCodec];
Mike Klein88544fb2019-03-20 10:50:33 -0500959 if (CommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
msarett84451022016-02-11 06:45:51 -0800960 continue;
961 }
bungeman38d909e2016-08-02 14:40:46 -0700962 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400963 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800964 if (!codec) {
965 // Nothing to time.
966 SkDebugf("Cannot find codec for %s\n", path.c_str());
967 continue;
968 }
969
970 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
971 int sampleSize = sampleSizes[fCurrentSampleSize];
972 fCurrentSampleSize++;
Brian Osman788b9162020-02-07 10:36:46 -0500973 if (10 * sampleSize > std::min(codec->getInfo().width(), codec->getInfo().height())) {
msarett84451022016-02-11 06:45:51 -0800974 // Avoid benchmarking scaled decodes of already small images.
975 break;
976 }
977
bungeman38d909e2016-08-02 14:40:46 -0700978 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
979 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -0800980 }
981 fCurrentSampleSize = 0;
982 }
983
Leon Scroggins III87caae62020-05-04 10:02:45 -0400984#ifdef SK_ENABLE_ANDROID_UTILS
msarett7f691442015-09-22 11:56:16 -0700985 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700986 // We intend to create benchmarks that model the use cases in
987 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
988 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
989 // the image. For that reason, we will benchmark decodes in five representative locations
990 // in the image. Additionally, this use case utilizes power of two scaling, so we will
991 // test on power of two sample sizes. The output tile is always 512x512, so, when a
992 // sampleSize is used, the size of the subset that is decoded is always
993 // (sampleSize*512)x(sampleSize*512).
994 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700995 // All use cases we are aware of only scale by powers of two.
996 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
997 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800998 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700999 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -08001000 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -07001001 fSourceType = "image";
1002 fBenchType = "BRD";
1003
mtklein6f0ff912016-01-11 09:04:21 -08001004 const SkString& path = fImages[fCurrentBRDImage];
Mike Klein88544fb2019-03-20 10:50:33 -05001005 if (CommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
mtklein6f0ff912016-01-11 09:04:21 -08001006 continue;
1007 }
scroggo860e8a62015-10-15 07:51:28 -07001008
msarettd1227a72016-05-18 06:23:57 -07001009 while (fCurrentColorType < fColorTypes.count()) {
1010 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
1011 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -07001012
bungeman38d909e2016-08-02 14:40:46 -07001013 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -07001014 const SkColorType colorType = fColorTypes[fCurrentColorType];
1015 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
1016 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -07001017
msarettd1227a72016-05-18 06:23:57 -07001018 int width = 0;
1019 int height = 0;
reed42943c82016-09-12 12:01:44 -07001020 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -07001021 &width, &height)) {
1022 break;
msarett7f691442015-09-22 11:56:16 -07001023 }
msarettd1227a72016-05-18 06:23:57 -07001024
1025 SkString basename = SkOSPath::Basename(path.c_str());
1026 SkIRect subset;
1027 const uint32_t subsetSize = sampleSize * minOutputSize;
1028 switch (currentSubsetType) {
1029 case kTopLeft_SubsetType:
1030 basename.append("_TopLeft");
1031 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
1032 break;
1033 case kTopRight_SubsetType:
1034 basename.append("_TopRight");
1035 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
1036 subsetSize);
1037 break;
1038 case kMiddle_SubsetType:
1039 basename.append("_Middle");
1040 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
1041 (height - subsetSize) / 2, subsetSize, subsetSize);
1042 break;
1043 case kBottomLeft_SubsetType:
1044 basename.append("_BottomLeft");
1045 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
1046 subsetSize);
1047 break;
1048 case kBottomRight_SubsetType:
1049 basename.append("_BottomRight");
1050 subset = SkIRect::MakeXYWH(width - subsetSize,
1051 height - subsetSize, subsetSize, subsetSize);
1052 break;
1053 default:
1054 SkASSERT(false);
1055 }
1056
1057 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
1058 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -07001059 }
msarettd1227a72016-05-18 06:23:57 -07001060 fCurrentSubsetType = 0;
1061 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -07001062 }
msarettd1227a72016-05-18 06:23:57 -07001063 fCurrentSampleSize = 0;
1064 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -07001065 }
msarettd1227a72016-05-18 06:23:57 -07001066 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -07001067 }
Leon Scroggins III87caae62020-05-04 10:02:45 -04001068#endif // SK_ENABLE_ANDROID_UTILS
msarett7f691442015-09-22 11:56:16 -07001069
halcanary96fcdcc2015-08-27 07:41:13 -07001070 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001071 }
mtklein92007582014-08-01 07:46:52 -07001072
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001073 void fillCurrentOptions(NanoJSONResultsWriter& log) const {
1074 log.appendString("source_type", fSourceType);
1075 log.appendString("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001076 if (0 == strcmp(fSourceType, "skp")) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001077 log.appendString("clip",
mtklein92007582014-08-01 07:46:52 -07001078 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1079 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001080 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001081 log.appendString("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
mtklein92007582014-08-01 07:46:52 -07001082 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001083 }
1084
1085 void fillCurrentMetrics(NanoJSONResultsWriter& log) const {
mtklein051e56d2014-12-04 08:46:51 -08001086 if (0 == strcmp(fBenchType, "recording")) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001087 log.appendMetric("bytes", fSKPBytes);
1088 log.appendMetric("ops", fSKPOps);
mtklein051e56d2014-12-04 08:46:51 -08001089 }
mtklein92007582014-08-01 07:46:52 -07001090 }
1091
mtkleine714e752014-07-31 12:13:48 -07001092private:
Leon Scroggins III87caae62020-05-04 10:02:45 -04001093#ifdef SK_ENABLE_ANDROID_UTILS
msarettb23e6aa2015-06-09 13:56:10 -07001094 enum SubsetType {
1095 kTopLeft_SubsetType = 0,
1096 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001097 kMiddle_SubsetType = 2,
1098 kBottomLeft_SubsetType = 3,
1099 kBottomRight_SubsetType = 4,
1100 kTranslate_SubsetType = 5,
1101 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001102 kLast_SubsetType = kZoom_SubsetType,
1103 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001104 };
Leon Scroggins III87caae62020-05-04 10:02:45 -04001105#endif
msarettb23e6aa2015-06-09 13:56:10 -07001106
mtkleine714e752014-07-31 12:13:48 -07001107 const BenchRegistry* fBenches;
1108 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001109 SkIRect fClip;
1110 SkTArray<SkScalar> fScales;
1111 SkTArray<SkString> fSKPs;
Brian Salomon31fddc32021-04-30 13:08:55 -04001112 SkTArray<SkString> fMSKPs;
fmalita6519c212016-09-14 08:05:17 -07001113 SkTArray<SkString> fSVGs;
Hal Canary5dfefa22019-10-24 13:52:17 -04001114 SkTArray<SkString> fTextBlobTraces;
msarett95f192d2015-02-13 09:05:41 -08001115 SkTArray<SkString> fImages;
msarett74deb982015-10-20 16:45:56 -07001116 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001117 SkScalar fZoomMax;
1118 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001119
Mike Klein88d90712018-01-27 17:30:04 +00001120 double fSKPBytes, fSKPOps;
mtklein051e56d2014-12-04 08:46:51 -08001121
mtkleinfd731ce2014-09-10 12:19:30 -07001122 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1123 const char* fBenchType; // How we bench it: micro, recording, playback, ...
Hal Canary2362ae72019-10-24 13:40:40 -04001124 int fCurrentRecording = 0;
1125 int fCurrentDeserialPicture = 0;
Brian Salomon31fddc32021-04-30 13:08:55 -04001126 int fCurrentMSKP = 0;
Hal Canary2362ae72019-10-24 13:40:40 -04001127 int fCurrentScale = 0;
1128 int fCurrentSKP = 0;
1129 int fCurrentSVG = 0;
Hal Canary5dfefa22019-10-24 13:52:17 -04001130 int fCurrentTextBlobTrace = 0;
Hal Canary2362ae72019-10-24 13:40:40 -04001131 int fCurrentCodec = 0;
1132 int fCurrentAndroidCodec = 0;
Leon Scroggins III87caae62020-05-04 10:02:45 -04001133#ifdef SK_ENABLE_ANDROID_UTILS
Hal Canary2362ae72019-10-24 13:40:40 -04001134 int fCurrentBRDImage = 0;
Leon Scroggins III87caae62020-05-04 10:02:45 -04001135 int fCurrentSubsetType = 0;
1136#endif
Hal Canary2362ae72019-10-24 13:40:40 -04001137 int fCurrentColorType = 0;
1138 int fCurrentAlphaType = 0;
Hal Canary2362ae72019-10-24 13:40:40 -04001139 int fCurrentSampleSize = 0;
1140 int fCurrentAnimSKP = 0;
mtkleine714e752014-07-31 12:13:48 -07001141};
1142
msarettc149f0e2016-01-04 11:35:43 -08001143// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1144// This prints something every once in a while so that it knows we're still working.
1145static void start_keepalive() {
Mike Klein03141d22017-10-30 11:57:15 -04001146 static std::thread* intentionallyLeaked = new std::thread([]{
1147 for (;;) {
1148 static const int kSec = 1200;
1149 #if defined(SK_BUILD_FOR_WIN)
1150 Sleep(kSec * 1000);
1151 #else
1152 sleep(kSec);
1153 #endif
1154 SkDebugf("\nBenchmarks still running...\n");
msarettc149f0e2016-01-04 11:35:43 -08001155 }
Mike Klein03141d22017-10-30 11:57:15 -04001156 });
1157 (void)intentionallyLeaked;
msarettc149f0e2016-01-04 11:35:43 -08001158}
1159
John Stiles2ef627a2021-03-23 10:25:02 -04001160class NanobenchShaderErrorHandler : public GrContextOptions::ShaderErrorHandler {
1161 void compileError(const char* shader, const char* errors) override {
1162 // Nanobench should abort if any shader can't compile. Failure is much better than
1163 // reporting meaningless performance metrics.
1164 SkSL::String message = GrShaderUtils::BuildShaderErrorMessage(shader, errors);
John Stiles345f1402021-03-23 10:25:15 -04001165 SK_ABORT("\n%s", message.c_str());
John Stiles2ef627a2021-03-23 10:25:02 -04001166 }
1167};
1168
Mike Kleinbe28ee22017-02-06 12:46:20 -05001169int main(int argc, char** argv) {
Mike Klein88544fb2019-03-20 10:50:33 -05001170 CommandLineFlags::Parse(argc, argv);
Brian Osman53136aa2017-07-20 15:43:35 -04001171
Brian Osmanbc8150f2017-07-24 11:38:01 -04001172 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -04001173
Mike Kleinadacaef2017-02-06 09:26:14 -05001174#if defined(SK_BUILD_FOR_IOS)
1175 cd_Documents();
1176#endif
jcgregorio3b27ade2014-11-13 08:06:40 -08001177 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001178 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001179 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001180
Chris Dalton040238b2017-12-18 14:22:34 -07001181 SetCtxOptionsFromCommonFlags(&grContextOpts);
krajcevski69a55602014-08-13 10:46:31 -07001182
John Stiles2ef627a2021-03-23 10:25:02 -04001183 NanobenchShaderErrorHandler errorHandler;
1184 grContextOpts.fShaderErrorHandler = &errorHandler;
1185
bsalomon6eb03cc2014-08-07 14:28:50 -07001186 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001187 FLAGS_samples = 1;
1188 FLAGS_gpuFrameLag = 0;
1189 }
1190
bsalomon6eb03cc2014-08-07 14:28:50 -07001191 if (!FLAGS_writePath.isEmpty()) {
1192 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1193 if (!sk_mkdir(FLAGS_writePath[0])) {
1194 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001195 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001196 }
1197 }
1198
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001199 std::unique_ptr<SkWStream> logStream(new SkNullWStream);
mtklein60317d0f2014-07-14 11:30:37 -07001200 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001201#if defined(SK_RELEASE)
Herb Derbyefe9df32021-06-18 20:31:43 +00001202 logStream.reset(new SkFILEWStream(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001203#else
1204 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1205 return 1;
1206#endif
mtklein60317d0f2014-07-14 11:30:37 -07001207 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001208 NanoJSONResultsWriter log(logStream.get(), SkJSONWriter::Mode::kPretty);
1209 log.beginObject(); // root
mtklein1915b622014-08-20 11:45:00 -07001210
1211 if (1 == FLAGS_properties.count() % 2) {
1212 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1213 return 1;
1214 }
1215 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001216 log.appendString(FLAGS_properties[i-1], FLAGS_properties[i]);
mtklein1915b622014-08-20 11:45:00 -07001217 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001218
1219 if (1 == FLAGS_key.count() % 2) {
1220 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1221 return 1;
1222 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001223 if (FLAGS_key.count()) {
1224 log.beginObject("key");
1225 for (int i = 1; i < FLAGS_key.count(); i += 2) {
1226 log.appendString(FLAGS_key[i - 1], FLAGS_key[i]);
1227 }
1228 log.endObject(); // key
mtklein94e51562014-08-19 12:41:53 -07001229 }
mtklein60317d0f2014-07-14 11:30:37 -07001230
mtkleinf3723212014-06-25 14:08:00 -07001231 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001232 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001233
cdaltone1b89582015-06-25 19:17:08 -07001234 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001235
bsalomon6eb03cc2014-08-07 14:28:50 -07001236 if (kAutoTuneLoops != FLAGS_loops) {
1237 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001238 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001239 SkDebugf("! -> high variance, ? -> moderate variance\n");
1240 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001241 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001242 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001243 } else {
mtkleind75c4662015-04-30 07:11:22 -07001244 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001245 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001246 }
1247
Chris Dalton5dfb3f42021-04-30 17:16:12 -06001248 GrRecordingContextPriv::DMSAAStats combinedDMSAAStats;
1249
svaisanenc47635e2016-01-28 06:05:43 -08001250 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001251 create_configs(&configs);
1252
msarettc149f0e2016-01-04 11:35:43 -08001253 if (FLAGS_keepAlive) {
1254 start_keepalive();
1255 }
1256
Mike Klein03fa5d42019-03-22 11:39:09 -05001257 SetAnalyticAAFromCommonFlags();
liyuqian38911a72016-10-04 11:23:22 -07001258
Mike Reed0e2fb662021-07-23 11:00:40 -04001259 gSkForceRasterPipelineBlitter = FLAGS_forceRasterPipelineHP || FLAGS_forceRasterPipeline;
1260 gForceHighPrecisionRasterPipeline = FLAGS_forceRasterPipelineHP;
Mike Klein813e8cc2020-08-05 09:33:38 -05001261 gUseSkVMBlitter = FLAGS_skvm;
Mike Kleinf2b3f912021-03-23 14:32:45 -05001262 gSkVMAllowJIT = FLAGS_jit;
1263 gSkVMJITViaDylib = FLAGS_dylib;
Yuqian Li550148b2017-01-13 10:13:13 -05001264
mtkleine070c2b2014-10-14 08:40:43 -07001265 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001266 BenchmarkStream benchStream;
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001267 log.beginObject("results");
Jim Van Verth8a9a3712019-05-31 10:49:12 -04001268 AutoreleasePool pool;
mtklein92007582014-08-01 07:46:52 -07001269 while (Benchmark* b = benchStream.next()) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001270 std::unique_ptr<Benchmark> bench(b);
Mike Klein88544fb2019-03-20 10:50:33 -05001271 if (CommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001272 continue;
1273 }
1274
svaisanenc47635e2016-01-28 06:05:43 -08001275 if (!configs.empty()) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001276 log.beginBench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001277 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001278 }
egdaniel3bf92062015-06-26 08:12:46 -07001279 for (int i = 0; i < configs.count(); ++i) {
1280 Target* target = is_enabled(b, configs[i]);
1281 if (!target) {
1282 continue;
1283 }
mtkleinf3723212014-06-25 14:08:00 -07001284
halcanary96fcdcc2015-08-27 07:41:13 -07001285 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001286 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001287 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001288
brianosman7a5ada82016-02-08 13:49:12 -08001289 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001290 SkDebugf("Running %s\t%s\n"
1291 , bench->getUniqueName()
1292 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001293 if (FLAGS_dryRun) {
1294 continue;
1295 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001296 }
1297
Mike Kleinee254232019-03-28 08:32:14 -05001298 if (FLAGS_purgeBetweenBenches) {
1299 SkGraphics::PurgeAllCaches();
1300 }
1301
Brian Osman5ad87aa2017-09-08 16:05:23 -04001302 TRACE_EVENT2("skia", "Benchmark", "name", TRACE_STR_COPY(bench->getUniqueName()),
1303 "config", TRACE_STR_COPY(config));
Brian Osmanc3cdef52017-08-31 14:09:22 -04001304
egdaniel3bf92062015-06-26 08:12:46 -07001305 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001306 bench->perCanvasPreDraw(canvas);
1307
cdaltone1b89582015-06-25 19:17:08 -07001308 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001309 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001310 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1311 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001312
Leon Scroggins IIIb30d1132017-10-02 09:48:15 -04001313 if (kFailedLoops == loops) {
1314 // Can't be timed. A warning note has already been printed.
1315 cleanup_run(target);
1316 continue;
1317 }
1318
Yuqian Li3528eb32017-09-21 13:23:49 -04001319 if (runs == 0 && FLAGS_ms < 1000) {
1320 // Run the first bench for 1000ms to warm up the nanobench if FLAGS_ms < 1000.
1321 // Otherwise, the first few benches' measurements will be inaccurate.
1322 auto stop = now_ms() + 1000;
1323 do {
1324 time(loops, bench.get(), target);
Jim Van Verth49d6bca2020-02-28 11:05:44 -05001325 pool.drain();
Yuqian Li3528eb32017-09-21 13:23:49 -04001326 } while (now_ms() < stop);
1327 }
1328
mtkleinbbba1682015-10-28 11:36:30 -07001329 if (FLAGS_ms) {
1330 samples.reset();
1331 auto stop = now_ms() + FLAGS_ms;
1332 do {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001333 samples.push_back(time(loops, bench.get(), target) / loops);
Jim Van Verth49d6bca2020-02-28 11:05:44 -05001334 pool.drain();
mtkleinbbba1682015-10-28 11:36:30 -07001335 } while (now_ms() < stop);
1336 } else {
cdaltone1b89582015-06-25 19:17:08 -07001337 samples.reset(FLAGS_samples);
1338 for (int s = 0; s < FLAGS_samples; s++) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001339 samples[s] = time(loops, bench.get(), target) / loops;
Jim Van Verth49d6bca2020-02-28 11:05:44 -05001340 pool.drain();
cdaltone1b89582015-06-25 19:17:08 -07001341 }
cdaltone1b89582015-06-25 19:17:08 -07001342 }
mtkleinf3723212014-06-25 14:08:00 -07001343
Mike Kleinfec9b902019-05-31 10:03:07 -05001344 // Scale each result to the benchmark's own units, time/unit.
1345 for (double& sample : samples) {
1346 sample *= (1.0 / bench->getUnits());
1347 }
1348
joshualitte45c81c2015-12-02 09:05:37 -08001349 SkTArray<SkString> keys;
1350 SkTArray<double> values;
Chris Dalton5dfb3f42021-04-30 17:16:12 -06001351 if (configs[i].backend == Benchmark::kGPU_Backend) {
1352 if (FLAGS_gpuStatsDump) {
1353 // TODO cache stats
1354 bench->getGpuStats(canvas, &keys, &values);
1355 }
1356 if (FLAGS_dmsaaStatsDump && bench->getDMSAAStats(canvas->recordingContext())) {
1357 const auto& dmsaaStats = canvas->recordingContext()->priv().dmsaaStats();
1358 dmsaaStats.dumpKeyValuePairs(&keys, &values);
1359 dmsaaStats.dump();
1360 combinedDMSAAStats.merge(dmsaaStats);
1361 }
joshualitte45c81c2015-12-02 09:05:37 -08001362 }
joshualitte45c81c2015-12-02 09:05:37 -08001363
robertphillips5b693772014-11-21 06:19:36 -08001364 bench->perCanvasPostDraw(canvas);
1365
egdaniel3bf92062015-06-26 08:12:46 -07001366 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001367 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001368 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001369 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001370 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001371 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001372 }
1373
Mike Klein512a2442018-09-27 10:44:56 -04001374 // Building stats.plot often shows up in profiles,
1375 // so skip building it when we're not going to print it anyway.
Brian Osman7ad42c02021-09-03 16:42:51 -04001376 const bool want_plot = !FLAGS_quiet && !FLAGS_ms;
Mike Klein512a2442018-09-27 10:44:56 -04001377
1378 Stats stats(samples, want_plot);
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001379 log.beginObject(config);
1380
1381 log.beginObject("options");
1382 log.appendString("name", bench->getName());
1383 benchStream.fillCurrentOptions(log);
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001384 log.endObject(); // options
1385
1386 // Metrics
1387 log.appendMetric("min_ms", stats.min);
Joe Gregoriof4f9c3b2021-06-01 09:39:11 -04001388 log.appendMetric("min_ratio", sk_ieee_double_divide(stats.median, stats.min));
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001389 log.beginArray("samples");
1390 for (double sample : samples) {
1391 log.appendDoubleDigits(sample, 16);
1392 }
1393 log.endArray(); // samples
1394 benchStream.fillCurrentMetrics(log);
Chris Dalton5dfb3f42021-04-30 17:16:12 -06001395 if (!keys.empty()) {
joshualitte45c81c2015-12-02 09:05:37 -08001396 // dump to json, only SKPBench currently returns valid keys / values
1397 SkASSERT(keys.count() == values.count());
John Stiles73339ad2021-08-12 22:27:39 -04001398 for (int j = 0; j < keys.count(); j++) {
1399 log.appendMetric(keys[j].c_str(), values[j]);
joshualitte45c81c2015-12-02 09:05:37 -08001400 }
1401 }
joshualitte45c81c2015-12-02 09:05:37 -08001402
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001403 log.endObject(); // config
1404
mtkleine070c2b2014-10-14 08:40:43 -07001405 if (runs++ % FLAGS_flushEvery == 0) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001406 log.flush();
mtkleine070c2b2014-10-14 08:40:43 -07001407 }
mtklein60317d0f2014-07-14 11:30:37 -07001408
bsalomon6eb03cc2014-08-07 14:28:50 -07001409 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001410 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001411 config = ""; // Only print the config if we run the same bench on more than one.
1412 }
mtkleind75c4662015-04-30 07:11:22 -07001413 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1414 , sk_tools::getCurrResidentSetSizeMB()
1415 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001416 , bench->getUniqueName()
1417 , config);
mtkleinf3723212014-06-25 14:08:00 -07001418 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001419 const char* mark = " ";
Hal Canarye99e8da2019-01-14 16:34:50 -05001420 const double stddev_percent =
1421 sk_ieee_double_divide(100 * sqrt(stats.var), stats.mean);
mtklein66cfcff2015-12-04 06:35:30 -08001422 if (stddev_percent > 5) mark = "?";
1423 if (stddev_percent > 10) mark = "!";
1424
1425 SkDebugf("%10.2f %s\t%s\t%s\n",
1426 stats.median*1e3, mark, bench->getUniqueName(), config);
Mike Reedc665e5b2017-07-04 22:56:06 -04001427 } else if (FLAGS_csv) {
Hal Canarye99e8da2019-01-14 16:34:50 -05001428 const double stddev_percent =
1429 sk_ieee_double_divide(100 * sqrt(stats.var), stats.mean);
Mike Reedc665e5b2017-07-04 22:56:06 -04001430 SkDebugf("%g,%g,%g,%g,%g,%s,%s\n"
1431 , stats.min
1432 , stats.median
1433 , stats.mean
1434 , stats.max
1435 , stddev_percent
1436 , config
1437 , bench->getUniqueName()
1438 );
1439 } else {
1440 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 -05001441 const double stddev_percent =
1442 sk_ieee_double_divide(100 * sqrt(stats.var), stats.mean);
Mike Reedc665e5b2017-07-04 22:56:06 -04001443 SkDebugf(format
mtkleind75c4662015-04-30 07:11:22 -07001444 , sk_tools::getCurrResidentSetSizeMB()
1445 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001446 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001447 , HUMANIZE(stats.min)
1448 , HUMANIZE(stats.median)
1449 , HUMANIZE(stats.mean)
1450 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001451 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001452 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001453 , config
mtklein96289052014-09-10 12:05:59 -07001454 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001455 );
1456 }
joshualitte45c81c2015-12-02 09:05:37 -08001457
joshualitte45c81c2015-12-02 09:05:37 -08001458 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -04001459 target->dumpStats();
bsalomon06cddec2014-10-24 10:40:50 -07001460 }
joshualitte45c81c2015-12-02 09:05:37 -08001461
cdalton2c56ba52015-06-26 13:32:53 -07001462 if (FLAGS_verbose) {
1463 SkDebugf("Samples: ");
John Stiles73339ad2021-08-12 22:27:39 -04001464 for (int j = 0; j < samples.count(); j++) {
1465 SkDebugf("%s ", HUMANIZE(samples[j]));
cdalton2c56ba52015-06-26 13:32:53 -07001466 }
1467 SkDebugf("%s\n", bench->getUniqueName());
1468 }
egdaniel3bf92062015-06-26 08:12:46 -07001469 cleanup_run(target);
Jim Van Verth8a9a3712019-05-31 10:49:12 -04001470 pool.drain();
mtkleinf3723212014-06-25 14:08:00 -07001471 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001472 if (!configs.empty()) {
1473 log.endBench();
1474 }
mtkleinf3723212014-06-25 14:08:00 -07001475 }
1476
Chris Dalton5dfb3f42021-04-30 17:16:12 -06001477 if (FLAGS_dmsaaStatsDump) {
1478 SkDebugf("<<Total Combined DMSAA Stats>>\n");
1479 combinedDMSAAStats.dump();
1480 }
1481
Herb Derby5a523fe2017-01-26 16:48:28 -05001482 SkGraphics::PurgeAllCaches();
1483
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001484 log.beginBench("memory_usage", 0, 0);
1485 log.beginObject("meta"); // config
1486 log.appendS32("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1487 log.endObject(); // config
1488 log.endBench();
1489
Brian Osman68870aa2020-07-08 14:12:43 -04001490 RunSkSLMemoryBenchmarks(&log);
1491
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001492 log.endObject(); // results
1493 log.endObject(); // root
1494 log.flush();
mtkleine1091452014-12-04 10:47:02 -08001495
mtkleinf3723212014-06-25 14:08:00 -07001496 return 0;
1497}