blob: 48a6c1fe4d3fc1523bbcaa759ec3555ed786beff [file] [log] [blame]
mtkleinf3723212014-06-25 14:08:00 -07001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
mtkleinbb6a0282014-07-01 08:43:42 -07008#include <ctype.h>
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "bench/nanobench.h"
tomhudsond968a6f2015-03-26 11:28:06 -070011
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "bench/AndroidCodecBench.h"
13#include "bench/Benchmark.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "bench/CodecBench.h"
15#include "bench/CodecBenchPriv.h"
16#include "bench/GMBench.h"
17#include "bench/RecordingBench.h"
18#include "bench/ResultsWriter.h"
19#include "bench/SKPAnimationBench.h"
20#include "bench/SKPBench.h"
Hal Canary5dfefa22019-10-24 13:52:17 -040021#include "bench/SkGlyphCacheBench.h"
Brian Osman68870aa2020-07-08 14:12:43 -040022#include "bench/SkSLBench.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/codec/SkAndroidCodec.h"
24#include "include/codec/SkCodec.h"
25#include "include/core/SkCanvas.h"
26#include "include/core/SkData.h"
27#include "include/core/SkGraphics.h"
28#include "include/core/SkPictureRecorder.h"
29#include "include/core/SkString.h"
30#include "include/core/SkSurface.h"
Ben Wagneracf98df2019-07-12 12:51:44 -040031#include "include/core/SkTime.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/core/SkAutoMalloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/core/SkColorSpacePriv.h"
Ben Wagnerab6eefe2019-05-20 11:02:49 -040034#include "src/core/SkLeanWindows.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/core/SkOSFile.h"
36#include "src/core/SkTaskGroup.h"
37#include "src/core/SkTraceEvent.h"
38#include "src/utils/SkJSONWriter.h"
39#include "src/utils/SkOSPath.h"
Jim Van Verth8a9a3712019-05-31 10:49:12 -040040#include "tools/AutoreleasePool.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041#include "tools/CrashHandler.h"
42#include "tools/ProcStats.h"
43#include "tools/Stats.h"
44#include "tools/flags/CommonFlags.h"
45#include "tools/flags/CommonFlagsConfig.h"
46#include "tools/ios_utils.h"
47#include "tools/trace/EventTracingPriv.h"
48#include "tools/trace/SkDebugfTracer.h"
mtkleinf3723212014-06-25 14:08:00 -070049
Hal Canary0f666812018-03-22 15:21:12 -040050#ifdef SK_XML
Mike Kleinc0bd9f92019-04-23 12:05:21 -050051#include "experimental/svg/model/SkSVGDOM.h"
Hal Canary0f666812018-03-22 15:21:12 -040052#endif // SK_XML
53
Leon Scroggins III87caae62020-05-04 10:02:45 -040054#ifdef SK_ENABLE_ANDROID_UTILS
55#include "bench/BitmapRegionDecoderBench.h"
56#include "client_utils/android/BitmapRegionDecoder.h"
57#endif
58
Adlai Holler684838f2020-05-12 10:41:04 -040059#include <cinttypes>
bungeman60e0fee2015-08-26 05:15:46 -070060#include <stdlib.h>
John Stilesfbd050b2020-08-03 13:21:46 -040061#include <memory>
Mike Klein03141d22017-10-30 11:57:15 -040062#include <thread>
bungeman60e0fee2015-08-26 05:15:46 -070063
Mike Reedc928fe22017-06-05 10:20:31 -040064extern bool gSkForceRasterPipelineBlitter;
Mike Klein2a57e792020-01-14 12:47:52 -060065extern bool gUseSkVMBlitter;
Mike Kleinea4459d2020-01-16 13:05:04 -060066extern bool gSkVMJITViaDylib;
Mike Reedc928fe22017-06-05 10:20:31 -040067
Mike Klein8f11d4d2018-01-24 12:42:55 -050068#ifndef SK_BUILD_FOR_WIN
scroggo38ce0a72016-01-07 07:28:47 -080069 #include <unistd.h>
Brian Salomon032aaae2018-03-23 16:10:36 -040070
scroggo38ce0a72016-01-07 07:28:47 -080071#endif
72
Robert Phillips00f78de2020-07-01 16:09:43 -040073#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050074#include "src/gpu/GrCaps.h"
75#include "src/gpu/GrContextPriv.h"
76#include "src/gpu/SkGr.h"
77#include "src/gpu/gl/GrGLDefines.h"
78#include "src/gpu/gl/GrGLGpu.h"
79#include "src/gpu/gl/GrGLUtil.h"
80#include "tools/gpu/GrContextFactory.h"
Brian Salomon032aaae2018-03-23 16:10:36 -040081
Brian Osmanc7ad40f2018-05-31 14:27:17 -040082using sk_gpu_test::ContextInfo;
83using sk_gpu_test::GrContextFactory;
84using sk_gpu_test::TestContext;
Brian Salomon032aaae2018-03-23 16:10:36 -040085
Brian Osmanc7ad40f2018-05-31 14:27:17 -040086GrContextOptions grContextOpts;
bsalomon682c2692015-05-22 14:01:46 -070087
reed53249782014-10-10 09:09:52 -070088static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070089
bsalomon6eb03cc2014-08-07 14:28:50 -070090static SkString loops_help_txt() {
91 SkString help;
92 help.printf("Number of times to run each bench. Set this to %d to auto-"
93 "tune for each bench. Timings are only reported when auto-tuning.",
94 kAutoTuneLoops);
95 return help;
96}
97
cdaltone1b89582015-06-25 19:17:08 -070098static SkString to_string(int n) {
99 SkString str;
100 str.appendS32(n);
101 return str;
102}
103
Mike Kleinac8f4432019-07-01 10:15:32 -0500104static DEFINE_int(loops, kAutoTuneLoops, loops_help_txt().c_str());
bsalomon6eb03cc2014-08-07 14:28:50 -0700105
Mike Klein5b3f3432019-03-21 11:42:21 -0500106static DEFINE_int(samples, 10, "Number of samples to measure for each bench.");
107static DEFINE_int(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
108static DEFINE_int(overheadLoops, 100000, "Loops to estimate timer overhead.");
Mike Klein84836b72019-03-21 11:31:36 -0500109static DEFINE_double(overheadGoal, 0.0001,
mtkleinf3723212014-06-25 14:08:00 -0700110 "Loop until timer overhead is at most this fraction of our measurments.");
Mike Klein84836b72019-03-21 11:31:36 -0500111static DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
Mike Klein5b3f3432019-03-21 11:42:21 -0500112static DEFINE_int(gpuFrameLag, 5,
Mike Klein84836b72019-03-21 11:31:36 -0500113 "If unknown, estimated maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -0700114
Mike Klein84836b72019-03-21 11:31:36 -0500115static DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
Mike Klein5b3f3432019-03-21 11:42:21 -0500116static DEFINE_int(maxCalibrationAttempts, 3,
mtklein55b0ffc2014-07-17 08:38:23 -0700117 "Try up to this many times to guess loops for a bench, or skip the bench.");
Mike Klein5b3f3432019-03-21 11:42:21 -0500118static DEFINE_int(maxLoops, 1000000, "Never run a bench more times than this.");
Mike Klein84836b72019-03-21 11:31:36 -0500119static DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
120static DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
121static DEFINE_string(zoom, "1.0,0",
122 "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
123 "function that ping-pongs between 1.0 and zoomMax.");
124static DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
Mike Klein84836b72019-03-21 11:31:36 -0500125static DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
126static DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
Mike Klein5b3f3432019-03-21 11:42:21 -0500127static DEFINE_int(flushEvery, 10, "Flush --outResultsFile every Nth run.");
Mike Klein84836b72019-03-21 11:31:36 -0500128static DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
129static DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
130static DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
131static DEFINE_bool(csv, false, "Print status in CSV format");
132static DEFINE_string(sourceType, "",
mtklein65dfd2f2016-02-03 10:40:54 -0800133 "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
Mike Klein84836b72019-03-21 11:31:36 -0500134static DEFINE_string(benchType, "",
135 "Apply usual --match rules to bench type: micro, recording, "
136 "piping, playback, skcodec, etc.");
mtklein65dfd2f2016-02-03 10:40:54 -0800137
Mike Klein84836b72019-03-21 11:31:36 -0500138static DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
Mike Kleinea4459d2020-01-16 13:05:04 -0600139static DEFINE_bool(skvm, false, "sets gUseSkVMBlitter and gSkVMJITViaDylib");
Mike Reedc928fe22017-06-05 10:20:31 -0400140
Mike Klein19fb3972019-03-21 13:08:08 -0500141static DEFINE_bool2(pre_log, p, false,
142 "Log before running each test. May be incomprehensible when threading");
143
Leon Scroggins III1ff07062020-07-27 14:52:19 -0400144static DEFINE_bool(cpu, true, "Run CPU-bound work?");
145static DEFINE_bool(gpu, true, "Run GPU-bound work?");
Mike Klein629f5fc2019-03-22 14:55:19 -0500146static DEFINE_bool(dryRun, false,
147 "just print the tests that would be run, without actually running them.");
148static DEFINE_string(images, "",
149 "List of images and/or directories to decode. A directory with no images"
150 " is treated as a fatal error.");
151static DEFINE_bool(simpleCodec, false,
152 "Runs of a subset of the codec tests, always N32, Premul or Opaque");
153
Mike Kleinc6142d82019-03-25 10:54:59 -0500154static DEFINE_string2(match, m, nullptr,
155 "[~][^]substring[$] [...] of name to run.\n"
156 "Multiple matches may be separated by spaces.\n"
157 "~ causes a matching name to always be skipped\n"
158 "^ requires the start of the name to match\n"
159 "$ requires the end of the name to match\n"
160 "^ and $ requires an exact match\n"
161 "If a name does not match any list entry,\n"
162 "it is skipped unless some list entry starts with ~");
163
164static DEFINE_bool2(quiet, q, false, "if true, don't print status updates.");
165static DEFINE_bool2(verbose, v, false, "enable verbose output from the test driver.");
166
167
168static DEFINE_string(skps, "skps", "Directory to read skps from.");
169static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
Hal Canary5dfefa22019-10-24 13:52:17 -0400170static DEFINE_string(texttraces, "", "Directory to read TextBlobTrace files from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500171
172static DEFINE_int_2(threads, j, -1,
173 "Run threadsafe tests on a threadpool with this many extra threads, "
174 "defaulting to one extra thread per core.");
175
176static DEFINE_string2(writePath, w, "", "If set, write bitmaps here as .pngs.");
177
178static DEFINE_string(key, "",
179 "Space-separated key/value pairs to add to JSON identifying this builder.");
180static DEFINE_string(properties, "",
181 "Space-separated key/value pairs to add to JSON identifying this run.");
182
Mike Kleinee254232019-03-28 08:32:14 -0500183static DEFINE_bool(purgeBetweenBenches, false,
184 "Call SkGraphics::PurgeAllCaches() between each benchmark?");
185
mtkleinbbba1682015-10-28 11:36:30 -0700186static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
187
mtkleinf3723212014-06-25 14:08:00 -0700188static SkString humanize(double ms) {
Adlai Holler684838f2020-05-12 10:41:04 -0400189 if (FLAGS_verbose) return SkStringPrintf("%" PRIu64, (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800190 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700191}
mtklein55b0ffc2014-07-17 08:38:23 -0700192#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700193
tomhudsond968a6f2015-03-26 11:28:06 -0700194bool Target::init(SkImageInfo info, Benchmark* bench) {
195 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700196 this->surface = SkSurface::MakeRaster(info);
197 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700198 return false;
199 }
200 }
201 return true;
202}
203bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700204 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700205 if (!canvas) {
206 return false;
207 }
Mike Reed12e946b2017-04-17 10:53:29 -0400208 bmp->allocPixels(canvas->imageInfo());
209 if (!canvas->readPixels(*bmp, 0, 0)) {
tomhudsond968a6f2015-03-26 11:28:06 -0700210 SkDebugf("Can't read canvas pixels.\n");
211 return false;
212 }
213 return true;
214}
215
tomhudsond968a6f2015-03-26 11:28:06 -0700216struct GPUTarget : public Target {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400217 explicit GPUTarget(const Config& c) : Target(c) {}
218 ContextInfo contextInfo;
219 std::unique_ptr<GrContextFactory> factory;
tomhudsond968a6f2015-03-26 11:28:06 -0700220
Greg Daniel5ed3c112020-06-18 15:59:17 -0400221 ~GPUTarget() override {
222 // For Vulkan we need to release all our refs to the GrContext before destroy the vulkan
223 // context which happens at the end of this destructor. Thus we need to release the surface
224 // here which holds a ref to the GrContext.
225 surface.reset();
226 }
227
tomhudsond968a6f2015-03-26 11:28:06 -0700228 void setup() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400229 this->contextInfo.testContext()->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700230 // Make sure we're done with whatever came before.
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400231 this->contextInfo.testContext()->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700232 }
233 void endTiming() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400234 if (this->contextInfo.testContext()) {
Robert Phillips00f78de2020-07-01 16:09:43 -0400235 this->contextInfo.testContext()->flushAndWaitOnSync(contextInfo.directContext());
tomhudsond968a6f2015-03-26 11:28:06 -0700236 }
237 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400238 void fence() override { this->contextInfo.testContext()->finish(); }
mtkleind75c4662015-04-30 07:11:22 -0700239
cdaltond416a5b2015-06-23 13:23:44 -0700240 bool needsFrameTiming(int* maxFrameLag) const override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400241 if (!this->contextInfo.testContext()->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700242 // Frame lag is unknown.
243 *maxFrameLag = FLAGS_gpuFrameLag;
244 }
245 return true;
246 }
tomhudsond968a6f2015-03-26 11:28:06 -0700247 bool init(SkImageInfo info, Benchmark* bench) override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400248 GrContextOptions options = grContextOpts;
249 bench->modifyGrContextOptions(&options);
John Stilesfbd050b2020-08-03 13:21:46 -0400250 this->factory = std::make_unique<GrContextFactory>(options);
bsalomonafcd7cd2015-08-31 12:39:41 -0700251 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
252 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700253 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400254 this->surface = SkSurface::MakeRenderTarget(
255 this->factory->get(this->config.ctxType, this->config.ctxOverrides),
256 SkBudgeted::kNo, info, this->config.samples, &props);
257 this->contextInfo =
258 this->factory->getContextInfo(this->config.ctxType, this->config.ctxOverrides);
tomhudsond968a6f2015-03-26 11:28:06 -0700259 if (!this->surface.get()) {
260 return false;
261 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400262 if (!this->contextInfo.testContext()->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700263 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800264 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700265 }
tomhudsond968a6f2015-03-26 11:28:06 -0700266 return true;
267 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500268 void fillOptions(NanoJSONResultsWriter& log) override {
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400269#ifdef SK_GL
tomhudsond968a6f2015-03-26 11:28:06 -0700270 const GrGLubyte* version;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400271 if (this->contextInfo.backend() == GrBackendApi::kOpenGL) {
Brian Salomon032aaae2018-03-23 16:10:36 -0400272 const GrGLInterface* gl =
Robert Phillips00f78de2020-07-01 16:09:43 -0400273 static_cast<GrGLGpu*>(this->contextInfo.directContext()->priv().getGpu())
Brian Salomon032aaae2018-03-23 16:10:36 -0400274 ->glInterface();
bsalomonc8699322016-05-11 11:55:36 -0700275 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500276 log.appendString("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700277
bsalomonc8699322016-05-11 11:55:36 -0700278 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500279 log.appendString("GL_RENDERER", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700280
bsalomonc8699322016-05-11 11:55:36 -0700281 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VENDOR));
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500282 log.appendString("GL_VENDOR", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700283
bsalomonc8699322016-05-11 11:55:36 -0700284 GR_GL_CALL_RET(gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500285 log.appendString("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
bsalomonc8699322016-05-11 11:55:36 -0700286 }
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400287#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700288 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400289
290 void dumpStats() override {
Robert Phillips00f78de2020-07-01 16:09:43 -0400291 auto context = this->contextInfo.directContext();
292
293 context->priv().printCacheStats();
294 context->priv().printGpuStats();
295 context->priv().printContextStats();
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400296 }
tomhudsond968a6f2015-03-26 11:28:06 -0700297};
mtkleind75c4662015-04-30 07:11:22 -0700298
tomhudson75a0ebb2015-03-27 12:11:44 -0700299static double time(int loops, Benchmark* bench, Target* target) {
300 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700301 if (canvas) {
302 canvas->clear(SK_ColorWHITE);
303 }
joshualitt8a6697a2015-09-30 12:11:07 -0700304 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700305 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700306 canvas = target->beginTiming(canvas);
307 bench->draw(loops, canvas);
tomhudson75a0ebb2015-03-27 12:11:44 -0700308 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700309 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700310 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700311 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700312}
313
mtkleinf3723212014-06-25 14:08:00 -0700314static double estimate_timer_overhead() {
315 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700316 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700317 double start = now_ms();
318 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700319 }
320 return overhead / FLAGS_overheadLoops;
321}
322
reed53249782014-10-10 09:09:52 -0700323static int detect_forever_loops(int loops) {
324 // look for a magic run-forever value
325 if (loops < 0) {
326 loops = SK_MaxS32;
327 }
328 return loops;
329}
330
mtklein55b0ffc2014-07-17 08:38:23 -0700331static int clamp_loops(int loops) {
332 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800333 SkDebugf("ERROR: clamping loops from %d to 1. "
334 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700335 return 1;
336 }
337 if (loops > FLAGS_maxLoops) {
338 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
339 return FLAGS_maxLoops;
340 }
341 return loops;
342}
343
tomhudsond968a6f2015-03-26 11:28:06 -0700344static bool write_canvas_png(Target* target, const SkString& filename) {
345
bsalomon6eb03cc2014-08-07 14:28:50 -0700346 if (filename.isEmpty()) {
347 return false;
348 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700349 if (target->getCanvas() &&
350 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700351 return false;
352 }
tomhudsond968a6f2015-03-26 11:28:06 -0700353
bsalomon6eb03cc2014-08-07 14:28:50 -0700354 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700355
356 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700357 return false;
358 }
tomhudsond968a6f2015-03-26 11:28:06 -0700359
bsalomon6eb03cc2014-08-07 14:28:50 -0700360 SkString dir = SkOSPath::Dirname(filename.c_str());
361 if (!sk_mkdir(dir.c_str())) {
362 SkDebugf("Can't make dir %s.\n", dir.c_str());
363 return false;
364 }
365 SkFILEWStream stream(filename.c_str());
366 if (!stream.isValid()) {
367 SkDebugf("Can't write %s.\n", filename.c_str());
368 return false;
369 }
Hal Canarydb683012016-11-23 08:55:18 -0700370 if (!SkEncodeImage(&stream, bmp, SkEncodedImageFormat::kPNG, 100)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700371 SkDebugf("Can't encode a PNG.\n");
372 return false;
373 }
374 return true;
375}
376
377static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700378static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700379 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700380 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700381 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700382 int loops = bench->calculateLoops(FLAGS_loops);
383 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700384 while (bench_plus_overhead < overhead) {
385 if (round++ == FLAGS_maxCalibrationAttempts) {
386 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700387 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700388 return kFailedLoops;
389 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700390 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700391 }
mtklein2069e222014-08-04 13:57:39 -0700392 }
mtkleinf3723212014-06-25 14:08:00 -0700393
mtkleinbb6a0282014-07-01 08:43:42 -0700394 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700395 // We'll pick N to make timer overhead negligible:
396 //
mtkleinbb6a0282014-07-01 08:43:42 -0700397 // overhead
398 // ------------------------- < FLAGS_overheadGoal
399 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700400 //
Hal Canary55325b72017-01-03 10:36:17 -0500401 // where bench_plus_overhead ~=~ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700402 //
403 // Doing some math, we get:
404 //
mtkleinbb6a0282014-07-01 08:43:42 -0700405 // (overhead / FLAGS_overheadGoal) - overhead
406 // ------------------------------------------ < N
407 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700408 //
409 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700410 if (kAutoTuneLoops == loops) {
411 const double numer = overhead / FLAGS_overheadGoal - overhead;
412 const double denom = bench_plus_overhead - overhead;
413 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700414 loops = clamp_loops(loops);
415 } else {
416 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700417 }
mtkleinbb6a0282014-07-01 08:43:42 -0700418
mtkleinbb6a0282014-07-01 08:43:42 -0700419 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700420}
421
cdaltone1b89582015-06-25 19:17:08 -0700422static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700423 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700424 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700425 if (kAutoTuneLoops == loops) {
426 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700427 double elapsed = 0;
428 do {
mtklein527930f2014-11-06 08:04:34 -0800429 if (1<<30 == loops) {
430 // We're about to wrap. Something's wrong with the bench.
431 loops = 0;
432 break;
433 }
mtkleina189ccd2014-07-14 12:28:47 -0700434 loops *= 2;
435 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700436 // _this_ round, not still timing last round.
437 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700438 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700439 }
440 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700441
mtkleina189ccd2014-07-14 12:28:47 -0700442 // We've overshot at least a little. Scale back linearly.
443 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700444 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700445
tomhudsond968a6f2015-03-26 11:28:06 -0700446 // Make sure we're not still timing our calibration.
447 target->fence();
reed53249782014-10-10 09:09:52 -0700448 } else {
449 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700450 }
mtkleinbb6a0282014-07-01 08:43:42 -0700451 // Pretty much the same deal as the calibration: do some warmup to make
452 // sure we're timing steady-state pipelined frames.
Brian Osman01776982017-10-10 15:41:03 -0400453 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700454 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700455 }
mtkleinbb6a0282014-07-01 08:43:42 -0700456
mtkleinbb6a0282014-07-01 08:43:42 -0700457 return loops;
458}
mtkleinbb6a0282014-07-01 08:43:42 -0700459
Brian Salomon6405e712017-03-20 08:54:16 -0400460#define kBogusContextType GrContextFactory::kGL_ContextType
csmartdaltone812d492017-02-21 12:36:05 -0700461#define kBogusContextOverrides GrContextFactory::ContextOverrides::kNone
bsalomonc2553372014-07-22 13:09:05 -0700462
svaisanenc47635e2016-01-28 06:05:43 -0800463static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
svaisanenc47635e2016-01-28 06:05:43 -0800464 if (const auto* gpuConfig = config->asConfigGpu()) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400465 if (!FLAGS_gpu) {
466 SkDebugf("Skipping config '%s' as requested.\n", config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800467 return;
Ben Wagner32fa5102017-08-10 21:25:55 -0400468 }
svaisanenc47635e2016-01-28 06:05:43 -0800469
svaisanenc47635e2016-01-28 06:05:43 -0800470 const auto ctxType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700471 const auto ctxOverrides = gpuConfig->getContextOverrides();
svaisanenc47635e2016-01-28 06:05:43 -0800472 const auto sampleCount = gpuConfig->getSamples();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400473 const auto colorType = gpuConfig->getColorType();
474 auto colorSpace = gpuConfig->getColorSpace();
Brian Salomonf865b052018-03-09 09:01:53 -0500475 if (gpuConfig->getSurfType() != SkCommandLineConfigGpu::SurfType::kDefault) {
476 SkDebugf("This tool only supports the default surface type.");
477 return;
478 }
svaisanenc47635e2016-01-28 06:05:43 -0800479
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400480 GrContextFactory factory(grContextOpts);
Robert Phillipsf0288102020-07-06 13:45:34 -0400481 if (const auto ctx = factory.get(ctxType, ctxOverrides)) {
Greg Daniel6fa62e22019-08-07 15:52:37 -0400482 GrBackendFormat format = ctx->defaultBackendFormat(colorType, GrRenderable::kYes);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500483 int supportedSampleCount =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400484 ctx->priv().caps()->getRenderTargetSampleCount(sampleCount, format);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400485 if (sampleCount != supportedSampleCount) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400486 SkDebugf("Configuration '%s' sample count %d is not a supported sample count.\n",
487 config->getTag().c_str(), sampleCount);
svaisanenc47635e2016-01-28 06:05:43 -0800488 return;
489 }
490 } else {
Ben Wagner32fa5102017-08-10 21:25:55 -0400491 SkDebugf("No context was available matching config '%s'.\n",
492 config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800493 return;
494 }
495
496 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800497 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800498 Benchmark::kGPU_Backend,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400499 colorType,
svaisanenc47635e2016-01-28 06:05:43 -0800500 kPremul_SkAlphaType,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400501 sk_ref_sp(colorSpace),
svaisanenc47635e2016-01-28 06:05:43 -0800502 sampleCount,
503 ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700504 ctxOverrides,
kkinnunend4e1c352016-03-01 23:41:26 -0800505 gpuConfig->getUseDIText()
506 };
svaisanenc47635e2016-01-28 06:05:43 -0800507
508 configs->push_back(target);
509 return;
510 }
svaisanenc47635e2016-01-28 06:05:43 -0800511
brianosmanb109b8c2016-06-16 13:03:24 -0700512 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
513 if (config->getTag().equals(#name)) { \
Ben Wagner32fa5102017-08-10 21:25:55 -0400514 if (!FLAGS_cpu) { \
515 SkDebugf("Skipping config '%s' as requested.\n", \
516 config->getTag().c_str()); \
517 return; \
518 } \
brianosmanb109b8c2016-06-16 13:03:24 -0700519 Config config = { \
520 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
csmartdaltone812d492017-02-21 12:36:05 -0700521 0, kBogusContextType, kBogusContextOverrides, false \
brianosmanb109b8c2016-06-16 13:03:24 -0700522 }; \
523 configs->push_back(config); \
524 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700525 }
mtkleine714e752014-07-31 12:13:48 -0700526
Ben Wagner32fa5102017-08-10 21:25:55 -0400527 CPU_CONFIG(nonrendering, kNonRendering_Backend,
528 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800529
Mike Klein0e4041f2018-06-19 16:00:40 -0400530 CPU_CONFIG(a8, kRaster_Backend, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr)
531 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType, nullptr)
532 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
533
534 // 'narrow' has a gamut narrower than sRGB, and different transfer function.
Brian Osman82ebe042019-01-04 17:03:00 -0500535 auto narrow = SkColorSpace::MakeRGB(SkNamedTransferFn::k2Dot2, gNarrow_toXYZD50),
Mike Klein0e4041f2018-06-19 16:00:40 -0400536 srgb = SkColorSpace::MakeSRGB(),
537 srgbLinear = SkColorSpace::MakeSRGBLinear();
538
539 CPU_CONFIG( f16, kRaster_Backend, kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbLinear)
540 CPU_CONFIG( srgb, kRaster_Backend, kRGBA_8888_SkColorType, kPremul_SkAlphaType, srgb )
541 CPU_CONFIG( esrgb, kRaster_Backend, kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgb )
542 CPU_CONFIG( narrow, kRaster_Backend, kRGBA_8888_SkColorType, kPremul_SkAlphaType, narrow )
543 CPU_CONFIG(enarrow, kRaster_Backend, kRGBA_F16_SkColorType, kPremul_SkAlphaType, narrow )
mtkleinbb6a0282014-07-01 08:43:42 -0700544
svaisanenc47635e2016-01-28 06:05:43 -0800545 #undef CPU_CONFIG
Ben Wagner32fa5102017-08-10 21:25:55 -0400546
547 SkDebugf("Unknown config '%s'.\n", config->getTag().c_str());
mtkleinf3723212014-06-25 14:08:00 -0700548}
549
svaisanenc47635e2016-01-28 06:05:43 -0800550// Append all configs that are enabled and supported.
551void create_configs(SkTArray<Config>* configs) {
552 SkCommandLineConfigArray array;
553 ParseConfigs(FLAGS_config, &array);
554 for (int i = 0; i < array.count(); ++i) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400555 create_config(array[i].get(), configs);
svaisanenc47635e2016-01-28 06:05:43 -0800556 }
Ben Wagner32fa5102017-08-10 21:25:55 -0400557
558 // If no just default configs were requested, then we're okay.
559 if (array.count() == 0 || FLAGS_config.count() == 0 ||
Ben Wagner32fa5102017-08-10 21:25:55 -0400560 // Otherwise, make sure that all specified configs have been created.
561 array.count() == configs->count()) {
562 return;
563 }
Ben Wagner32fa5102017-08-10 21:25:55 -0400564 exit(1);
svaisanenc47635e2016-01-28 06:05:43 -0800565}
566
brianosman9f1f6e22016-09-15 08:33:02 -0700567// disable warning : switch statement contains default but no 'case' labels
568#if defined _WIN32
569#pragma warning ( push )
570#pragma warning ( disable : 4065 )
571#endif
572
halcanary96fcdcc2015-08-27 07:41:13 -0700573// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700574static Target* is_enabled(Benchmark* bench, const Config& config) {
575 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700576 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700577 }
578
reede5ea5002014-09-03 11:54:58 -0700579 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700580 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700581
halcanary96fcdcc2015-08-27 07:41:13 -0700582 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700583
tomhudsond968a6f2015-03-26 11:28:06 -0700584 switch (config.backend) {
tomhudsond968a6f2015-03-26 11:28:06 -0700585 case Benchmark::kGPU_Backend:
586 target = new GPUTarget(config);
587 break;
tomhudsond968a6f2015-03-26 11:28:06 -0700588 default:
589 target = new Target(config);
590 break;
591 }
bsalomonc2553372014-07-22 13:09:05 -0700592
tomhudsond968a6f2015-03-26 11:28:06 -0700593 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700594 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700595 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700596 }
597 return target;
598}
599
brianosman9f1f6e22016-09-15 08:33:02 -0700600#if defined _WIN32
601#pragma warning ( pop )
602#endif
603
Leon Scroggins III87caae62020-05-04 10:02:45 -0400604#ifdef SK_ENABLE_ANDROID_UTILS
reed42943c82016-09-12 12:01:44 -0700605static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700606 uint32_t minOutputSize, int* width, int* height) {
Leon Scroggins III87caae62020-05-04 10:02:45 -0400607 auto brd = android::skia::BitmapRegionDecoder::Make(encoded);
msarett7f691442015-09-22 11:56:16 -0700608 if (nullptr == brd.get()) {
609 // This is indicates that subset decoding is not supported for a particular image format.
610 return false;
611 }
612
msarett7f691442015-09-22 11:56:16 -0700613 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
614 (uint32_t) brd->height()) {
615 // This indicates that the image is not large enough to decode a
616 // minOutputSize x minOutputSize subset at the given sampleSize.
617 return false;
618 }
619
620 // Set the image width and height. The calling code will use this to choose subsets to decode.
621 *width = brd->width();
622 *height = brd->height();
623 return true;
624}
Leon Scroggins III87caae62020-05-04 10:02:45 -0400625#endif
msarett7f691442015-09-22 11:56:16 -0700626
egdaniel3bf92062015-06-26 08:12:46 -0700627static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700628 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700629}
630
Mike Klein88544fb2019-03-20 10:50:33 -0500631static void collect_files(const CommandLineFlags::StringArray& paths,
632 const char* ext,
633 SkTArray<SkString>* list) {
fmalita6519c212016-09-14 08:05:17 -0700634 for (int i = 0; i < paths.count(); ++i) {
635 if (SkStrEndsWith(paths[i], ext)) {
636 list->push_back(SkString(paths[i]));
637 } else {
638 SkOSFile::Iter it(paths[i], ext);
639 SkString path;
640 while (it.next(&path)) {
641 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
642 }
643 }
644 }
645}
646
mtkleine714e752014-07-31 12:13:48 -0700647class BenchmarkStream {
648public:
mtklein92007582014-08-01 07:46:52 -0700649 BenchmarkStream() : fBenches(BenchRegistry::Head())
Hal Canary2362ae72019-10-24 13:40:40 -0400650 , fGMs(skiagm::GMRegistry::Head()) {
fmalita6519c212016-09-14 08:05:17 -0700651 collect_files(FLAGS_skps, ".skp", &fSKPs);
652 collect_files(FLAGS_svgs, ".svg", &fSVGs);
Hal Canary5dfefa22019-10-24 13:52:17 -0400653 collect_files(FLAGS_texttraces, ".trace", &fTextBlobTraces);
mtkleine714e752014-07-31 12:13:48 -0700654
mtklein92007582014-08-01 07:46:52 -0700655 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
656 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
657 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
658 exit(1);
659 }
660
661 for (int i = 0; i < FLAGS_scales.count(); i++) {
662 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
663 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
664 exit(1);
665 }
666 }
robertphillips5b693772014-11-21 06:19:36 -0800667
cdalton63a82852015-06-29 14:06:10 -0700668 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
669 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700670 exit(1);
671 }
672
robertphillips5b693772014-11-21 06:19:36 -0800673 if (FLAGS_mpd) {
674 fUseMPDs.push_back() = true;
675 }
mtkleinc751ecb2015-06-15 08:56:38 -0700676 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700677
msarett95f192d2015-02-13 09:05:41 -0800678 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700679 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800680 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800681 }
mtklein95553d92015-03-12 08:24:21 -0700682
msarett95f192d2015-02-13 09:05:41 -0800683 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700684 fColorTypes.push_back(kN32_SkColorType);
685 if (!FLAGS_simpleCodec) {
686 fColorTypes.push_back(kRGB_565_SkColorType);
687 fColorTypes.push_back(kAlpha_8_SkColorType);
msarett67cb6662016-06-21 08:49:26 -0700688 fColorTypes.push_back(kGray_8_SkColorType);
689 }
mtklein92007582014-08-01 07:46:52 -0700690 }
691
reedca2622b2016-03-18 07:25:55 -0700692 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700693 // Not strictly necessary, as it will be checked again later,
694 // but helps to avoid a lot of pointless work if we're going to skip it.
Mike Klein88544fb2019-03-20 10:50:33 -0500695 if (CommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700696 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700697 }
698
bungemanf93d7112016-09-16 06:24:20 -0700699 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
700 if (!stream) {
mtkleinfd731ce2014-09-10 12:19:30 -0700701 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700702 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700703 }
704
reedca2622b2016-03-18 07:25:55 -0700705 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700706 }
707
fmalita6519c212016-09-14 08:05:17 -0700708 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
Brian Osman133823d2018-09-19 14:14:15 -0400709 sk_sp<SkData> data(SkData::MakeFromFileName(path));
710 if (!data) {
fmalita6519c212016-09-14 08:05:17 -0700711 SkDebugf("Could not read %s.\n", path);
712 return nullptr;
713 }
714
Hal Canary0f666812018-03-22 15:21:12 -0400715#ifdef SK_XML
Brian Osman133823d2018-09-19 14:14:15 -0400716 SkMemoryStream stream(std::move(data));
fmalitae1baa7c2016-09-14 12:04:30 -0700717 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
fmalita6519c212016-09-14 08:05:17 -0700718 if (!svgDom) {
719 SkDebugf("Could not parse %s.\n", path);
720 return nullptr;
721 }
722
fmalitae1baa7c2016-09-14 12:04:30 -0700723 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
724 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
725 if (svgDom->containerSize().isEmpty()) {
726 svgDom->setContainerSize(kDefaultContainerSize);
727 }
728
fmalita6519c212016-09-14 08:05:17 -0700729 SkPictureRecorder recorder;
fmalitae1baa7c2016-09-14 12:04:30 -0700730 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
731 svgDom->containerSize().height()));
fmalita6519c212016-09-14 08:05:17 -0700732 return recorder.finishRecordingAsPicture();
Hal Canary0f666812018-03-22 15:21:12 -0400733#else
734 return nullptr;
735#endif // SK_XML
fmalita6519c212016-09-14 08:05:17 -0700736 }
737
mtklein92007582014-08-01 07:46:52 -0700738 Benchmark* next() {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400739 std::unique_ptr<Benchmark> bench;
mtklein65dfd2f2016-02-03 10:40:54 -0800740 do {
741 bench.reset(this->rawNext());
742 if (!bench) {
743 return nullptr;
744 }
Mike Klein88544fb2019-03-20 10:50:33 -0500745 } while (CommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
746 CommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700747 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800748 }
749
750 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700751 if (fBenches) {
Hal Canary972eba32018-07-30 17:07:07 -0400752 Benchmark* bench = fBenches->get()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700753 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700754 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700755 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700756 return bench;
757 }
mtklein92007582014-08-01 07:46:52 -0700758
mtkleine714e752014-07-31 12:13:48 -0700759 while (fGMs) {
Ben Wagner406ff502019-08-12 16:39:24 -0400760 std::unique_ptr<skiagm::GM> gm = fGMs->get()();
mtkleine714e752014-07-31 12:13:48 -0700761 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800762 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700763 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700764 fBenchType = "micro";
Ben Wagner406ff502019-08-12 16:39:24 -0400765 return new GMBench(std::move(gm));
mtkleine714e752014-07-31 12:13:48 -0700766 }
767 }
mtklein92007582014-08-01 07:46:52 -0700768
Hal Canary5dfefa22019-10-24 13:52:17 -0400769 while (fCurrentTextBlobTrace < fTextBlobTraces.count()) {
770 SkString path = fTextBlobTraces[fCurrentTextBlobTrace++];
771 SkString basename = SkOSPath::Basename(path.c_str());
772 static constexpr char kEnding[] = ".trace";
773 if (basename.endsWith(kEnding)) {
774 basename.remove(basename.size() - strlen(kEnding), strlen(kEnding));
775 }
776 fSourceType = "texttrace";
777 fBenchType = "micro";
778 return CreateDiffCanvasBench(
779 SkStringPrintf("SkDiffBench-%s", basename.c_str()),
780 [path](){ return SkStream::MakeFromFile(path.c_str()); });
781 }
782
mtkleinfd731ce2014-09-10 12:19:30 -0700783 // First add all .skps as RecordingBenches.
784 while (fCurrentRecording < fSKPs.count()) {
785 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700786 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
787 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700788 continue;
789 }
790 SkString name = SkOSPath::Basename(path.c_str());
791 fSourceType = "skp";
792 fBenchType = "recording";
Mike Klein88d90712018-01-27 17:30:04 +0000793 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
794 fSKPOps = pic->approximateOpCount();
Mike Kleina705cb92019-05-14 12:33:40 -0500795 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700796 }
797
Mike Reede45ff462017-12-06 10:47:03 -0500798 // Add all .skps as DeserializePictureBenchs.
799 while (fCurrentDeserialPicture < fSKPs.count()) {
800 const SkString& path = fSKPs[fCurrentDeserialPicture++];
801 sk_sp<SkData> data = SkData::MakeFromFileName(path.c_str());
802 if (!data) {
803 continue;
804 }
805 SkString name = SkOSPath::Basename(path.c_str());
806 fSourceType = "skp";
807 fBenchType = "deserial";
Mike Reed7557bbb2017-12-24 19:50:57 -0500808 fSKPBytes = static_cast<double>(data->size());
Mike Klein88d90712018-01-27 17:30:04 +0000809 fSKPOps = 0;
Mike Reede45ff462017-12-06 10:47:03 -0500810 return new DeserializePictureBench(name.c_str(), std::move(data));
811 }
812
mtkleinfd731ce2014-09-10 12:19:30 -0700813 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700814 while (fCurrentScale < fScales.count()) {
815 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800816 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700817 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
818 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800819 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700820 continue;
821 }
robertphillips5b693772014-11-21 06:19:36 -0800822
823 while (fCurrentUseMPD < fUseMPDs.count()) {
824 if (FLAGS_bbh) {
825 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
826 SkRTreeFactory factory;
827 SkPictureRecorder recorder;
robertphillips5b693772014-11-21 06:19:36 -0800828 pic->playback(recorder.beginRecording(pic->cullRect().width(),
829 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800830 &factory,
robertphillipsdda54452016-07-13 13:27:16 -0700831 0));
reedca2622b2016-03-18 07:25:55 -0700832 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800833 }
834 SkString name = SkOSPath::Basename(path.c_str());
835 fSourceType = "skp";
836 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700837 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
838 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700839 }
robertphillips5b693772014-11-21 06:19:36 -0800840 fCurrentUseMPD = 0;
841 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700842 }
fmalita6519c212016-09-14 08:05:17 -0700843
844 while (fCurrentSVG++ < fSVGs.count()) {
845 const char* path = fSVGs[fCurrentSVG - 1].c_str();
846 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
847 fSourceType = "svg";
848 fBenchType = "playback";
849 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
850 fScales[fCurrentScale], false, FLAGS_loopSKP);
851 }
852 }
853
mtklein92007582014-08-01 07:46:52 -0700854 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700855 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700856 fCurrentScale++;
857 }
858
joshualitt261c3ad2015-04-27 09:16:57 -0700859 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700860 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700861 while (fCurrentAnimSKP < fSKPs.count()) {
862 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700863 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
864 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700865 fCurrentAnimSKP++;
866 continue;
867 }
868
869 fCurrentAnimSKP++;
870 SkString name = SkOSPath::Basename(path.c_str());
Ben Wagneracf98df2019-07-12 12:51:44 -0400871 sk_sp<SKPAnimationBench::Animation> animation =
872 SKPAnimationBench::MakeZoomAnimation(fZoomMax, fZoomPeriodMs);
873 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, std::move(animation),
halcanary385fe4d2015-08-26 13:07:48 -0700874 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700875 }
876 }
877
scroggo60869a42015-04-01 12:09:17 -0700878 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700879 fSourceType = "image";
880 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700881 const SkString& path = fImages[fCurrentCodec];
Mike Klein88544fb2019-03-20 10:50:33 -0500882 if (CommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
mtklein6f0ff912016-01-11 09:04:21 -0800883 continue;
884 }
bungeman38d909e2016-08-02 14:40:46 -0700885 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400886 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700887 if (!codec) {
888 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700889 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700890 continue;
891 }
scroggo21027992015-04-02 13:22:38 -0700892
scroggo60869a42015-04-01 12:09:17 -0700893 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700894 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700895
msarettc7796b92016-01-07 14:20:20 -0800896 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700897 if (FLAGS_simpleCodec) {
898 if (kUnpremul_SkAlphaType == alphaType) {
899 alphaType = kPremul_SkAlphaType;
900 }
901
902 fCurrentColorType++;
903 } else {
904 switch (alphaType) {
905 case kOpaque_SkAlphaType:
906 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800907 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700908 break;
909 case kUnpremul_SkAlphaType:
910 case kPremul_SkAlphaType:
911 if (0 == fCurrentAlphaType) {
912 // Test unpremul first.
913 alphaType = kUnpremul_SkAlphaType;
914 fCurrentAlphaType++;
915 } else {
916 // Test premul.
917 alphaType = kPremul_SkAlphaType;
918 fCurrentAlphaType = 0;
919 fCurrentColorType++;
920 }
921 break;
922 default:
923 SkASSERT(false);
924 fCurrentColorType++;
925 break;
926 }
scroggo21027992015-04-02 13:22:38 -0700927 }
928
msarettc7796b92016-01-07 14:20:20 -0800929 // Make sure we can decode to this color type and alpha type.
930 SkImageInfo info =
931 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700932 const size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400933 SkAutoMalloc storage(info.computeByteSize(rowBytes));
scroggo21027992015-04-02 13:22:38 -0700934
scroggoeb602a52015-07-09 08:16:03 -0700935 const SkCodec::Result result = codec->getPixels(
Leon Scroggins571b30f2017-07-11 17:35:31 +0000936 info, storage.get(), rowBytes);
scroggo60869a42015-04-01 12:09:17 -0700937 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700938 case SkCodec::kSuccess:
939 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700940 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700941 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700942 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700943 // This is okay. Not all conversions are valid.
944 break;
scroggo60869a42015-04-01 12:09:17 -0700945 default:
946 // This represents some sort of failure.
947 SkASSERT(false);
948 break;
949 }
950 }
951 fCurrentColorType = 0;
952 }
953
msarett84451022016-02-11 06:45:51 -0800954 // Run AndroidCodecBenches
955 const int sampleSizes[] = { 2, 4, 8 };
956 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
957 fSourceType = "image";
958 fBenchType = "skandroidcodec";
959
960 const SkString& path = fImages[fCurrentAndroidCodec];
Mike Klein88544fb2019-03-20 10:50:33 -0500961 if (CommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
msarett84451022016-02-11 06:45:51 -0800962 continue;
963 }
bungeman38d909e2016-08-02 14:40:46 -0700964 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400965 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800966 if (!codec) {
967 // Nothing to time.
968 SkDebugf("Cannot find codec for %s\n", path.c_str());
969 continue;
970 }
971
972 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
973 int sampleSize = sampleSizes[fCurrentSampleSize];
974 fCurrentSampleSize++;
Brian Osman788b9162020-02-07 10:36:46 -0500975 if (10 * sampleSize > std::min(codec->getInfo().width(), codec->getInfo().height())) {
msarett84451022016-02-11 06:45:51 -0800976 // Avoid benchmarking scaled decodes of already small images.
977 break;
978 }
979
bungeman38d909e2016-08-02 14:40:46 -0700980 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
981 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -0800982 }
983 fCurrentSampleSize = 0;
984 }
985
Leon Scroggins III87caae62020-05-04 10:02:45 -0400986#ifdef SK_ENABLE_ANDROID_UTILS
msarett7f691442015-09-22 11:56:16 -0700987 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700988 // We intend to create benchmarks that model the use cases in
989 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
990 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
991 // the image. For that reason, we will benchmark decodes in five representative locations
992 // in the image. Additionally, this use case utilizes power of two scaling, so we will
993 // test on power of two sample sizes. The output tile is always 512x512, so, when a
994 // sampleSize is used, the size of the subset that is decoded is always
995 // (sampleSize*512)x(sampleSize*512).
996 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700997 // All use cases we are aware of only scale by powers of two.
998 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
999 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -08001000 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -07001001 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -08001002 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -07001003 fSourceType = "image";
1004 fBenchType = "BRD";
1005
mtklein6f0ff912016-01-11 09:04:21 -08001006 const SkString& path = fImages[fCurrentBRDImage];
Mike Klein88544fb2019-03-20 10:50:33 -05001007 if (CommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
mtklein6f0ff912016-01-11 09:04:21 -08001008 continue;
1009 }
scroggo860e8a62015-10-15 07:51:28 -07001010
msarettd1227a72016-05-18 06:23:57 -07001011 while (fCurrentColorType < fColorTypes.count()) {
1012 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
1013 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -07001014
bungeman38d909e2016-08-02 14:40:46 -07001015 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -07001016 const SkColorType colorType = fColorTypes[fCurrentColorType];
1017 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
1018 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -07001019
msarettd1227a72016-05-18 06:23:57 -07001020 int width = 0;
1021 int height = 0;
reed42943c82016-09-12 12:01:44 -07001022 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -07001023 &width, &height)) {
1024 break;
msarett7f691442015-09-22 11:56:16 -07001025 }
msarettd1227a72016-05-18 06:23:57 -07001026
1027 SkString basename = SkOSPath::Basename(path.c_str());
1028 SkIRect subset;
1029 const uint32_t subsetSize = sampleSize * minOutputSize;
1030 switch (currentSubsetType) {
1031 case kTopLeft_SubsetType:
1032 basename.append("_TopLeft");
1033 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
1034 break;
1035 case kTopRight_SubsetType:
1036 basename.append("_TopRight");
1037 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
1038 subsetSize);
1039 break;
1040 case kMiddle_SubsetType:
1041 basename.append("_Middle");
1042 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
1043 (height - subsetSize) / 2, subsetSize, subsetSize);
1044 break;
1045 case kBottomLeft_SubsetType:
1046 basename.append("_BottomLeft");
1047 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
1048 subsetSize);
1049 break;
1050 case kBottomRight_SubsetType:
1051 basename.append("_BottomRight");
1052 subset = SkIRect::MakeXYWH(width - subsetSize,
1053 height - subsetSize, subsetSize, subsetSize);
1054 break;
1055 default:
1056 SkASSERT(false);
1057 }
1058
1059 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
1060 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -07001061 }
msarettd1227a72016-05-18 06:23:57 -07001062 fCurrentSubsetType = 0;
1063 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -07001064 }
msarettd1227a72016-05-18 06:23:57 -07001065 fCurrentSampleSize = 0;
1066 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -07001067 }
msarettd1227a72016-05-18 06:23:57 -07001068 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -07001069 }
Leon Scroggins III87caae62020-05-04 10:02:45 -04001070#endif // SK_ENABLE_ANDROID_UTILS
msarett7f691442015-09-22 11:56:16 -07001071
halcanary96fcdcc2015-08-27 07:41:13 -07001072 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001073 }
mtklein92007582014-08-01 07:46:52 -07001074
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001075 void fillCurrentOptions(NanoJSONResultsWriter& log) const {
1076 log.appendString("source_type", fSourceType);
1077 log.appendString("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001078 if (0 == strcmp(fSourceType, "skp")) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001079 log.appendString("clip",
mtklein92007582014-08-01 07:46:52 -07001080 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1081 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001082 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001083 log.appendString("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001084 if (fCurrentUseMPD > 0) {
1085 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001086 log.appendString("multi_picture_draw",
1087 fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
robertphillips5b693772014-11-21 06:19:36 -08001088 }
mtklein92007582014-08-01 07:46:52 -07001089 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001090 }
1091
1092 void fillCurrentMetrics(NanoJSONResultsWriter& log) const {
mtklein051e56d2014-12-04 08:46:51 -08001093 if (0 == strcmp(fBenchType, "recording")) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001094 log.appendMetric("bytes", fSKPBytes);
1095 log.appendMetric("ops", fSKPOps);
mtklein051e56d2014-12-04 08:46:51 -08001096 }
mtklein92007582014-08-01 07:46:52 -07001097 }
1098
mtkleine714e752014-07-31 12:13:48 -07001099private:
Leon Scroggins III87caae62020-05-04 10:02:45 -04001100#ifdef SK_ENABLE_ANDROID_UTILS
msarettb23e6aa2015-06-09 13:56:10 -07001101 enum SubsetType {
1102 kTopLeft_SubsetType = 0,
1103 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001104 kMiddle_SubsetType = 2,
1105 kBottomLeft_SubsetType = 3,
1106 kBottomRight_SubsetType = 4,
1107 kTranslate_SubsetType = 5,
1108 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001109 kLast_SubsetType = kZoom_SubsetType,
1110 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001111 };
Leon Scroggins III87caae62020-05-04 10:02:45 -04001112#endif
msarettb23e6aa2015-06-09 13:56:10 -07001113
mtkleine714e752014-07-31 12:13:48 -07001114 const BenchRegistry* fBenches;
1115 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001116 SkIRect fClip;
1117 SkTArray<SkScalar> fScales;
1118 SkTArray<SkString> fSKPs;
fmalita6519c212016-09-14 08:05:17 -07001119 SkTArray<SkString> fSVGs;
Hal Canary5dfefa22019-10-24 13:52:17 -04001120 SkTArray<SkString> fTextBlobTraces;
robertphillips5b693772014-11-21 06:19:36 -08001121 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001122 SkTArray<SkString> fImages;
msarett74deb982015-10-20 16:45:56 -07001123 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001124 SkScalar fZoomMax;
1125 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001126
Mike Klein88d90712018-01-27 17:30:04 +00001127 double fSKPBytes, fSKPOps;
mtklein051e56d2014-12-04 08:46:51 -08001128
mtkleinfd731ce2014-09-10 12:19:30 -07001129 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1130 const char* fBenchType; // How we bench it: micro, recording, playback, ...
Hal Canary2362ae72019-10-24 13:40:40 -04001131 int fCurrentRecording = 0;
1132 int fCurrentDeserialPicture = 0;
1133 int fCurrentScale = 0;
1134 int fCurrentSKP = 0;
1135 int fCurrentSVG = 0;
Hal Canary5dfefa22019-10-24 13:52:17 -04001136 int fCurrentTextBlobTrace = 0;
Hal Canary2362ae72019-10-24 13:40:40 -04001137 int fCurrentUseMPD = 0;
1138 int fCurrentCodec = 0;
1139 int fCurrentAndroidCodec = 0;
Leon Scroggins III87caae62020-05-04 10:02:45 -04001140#ifdef SK_ENABLE_ANDROID_UTILS
Hal Canary2362ae72019-10-24 13:40:40 -04001141 int fCurrentBRDImage = 0;
Leon Scroggins III87caae62020-05-04 10:02:45 -04001142 int fCurrentSubsetType = 0;
1143#endif
Hal Canary2362ae72019-10-24 13:40:40 -04001144 int fCurrentColorType = 0;
1145 int fCurrentAlphaType = 0;
Hal Canary2362ae72019-10-24 13:40:40 -04001146 int fCurrentSampleSize = 0;
1147 int fCurrentAnimSKP = 0;
mtkleine714e752014-07-31 12:13:48 -07001148};
1149
msarettc149f0e2016-01-04 11:35:43 -08001150// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1151// This prints something every once in a while so that it knows we're still working.
1152static void start_keepalive() {
Mike Klein03141d22017-10-30 11:57:15 -04001153 static std::thread* intentionallyLeaked = new std::thread([]{
1154 for (;;) {
1155 static const int kSec = 1200;
1156 #if defined(SK_BUILD_FOR_WIN)
1157 Sleep(kSec * 1000);
1158 #else
1159 sleep(kSec);
1160 #endif
1161 SkDebugf("\nBenchmarks still running...\n");
msarettc149f0e2016-01-04 11:35:43 -08001162 }
Mike Klein03141d22017-10-30 11:57:15 -04001163 });
1164 (void)intentionallyLeaked;
msarettc149f0e2016-01-04 11:35:43 -08001165}
1166
Mike Kleinbe28ee22017-02-06 12:46:20 -05001167int main(int argc, char** argv) {
Mike Klein88544fb2019-03-20 10:50:33 -05001168 CommandLineFlags::Parse(argc, argv);
Brian Osman53136aa2017-07-20 15:43:35 -04001169
Brian Osmanbc8150f2017-07-24 11:38:01 -04001170 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -04001171
Mike Kleinadacaef2017-02-06 09:26:14 -05001172#if defined(SK_BUILD_FOR_IOS)
1173 cd_Documents();
1174#endif
jcgregorio3b27ade2014-11-13 08:06:40 -08001175 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001176 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001177 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001178
Chris Dalton040238b2017-12-18 14:22:34 -07001179 SetCtxOptionsFromCommonFlags(&grContextOpts);
krajcevski69a55602014-08-13 10:46:31 -07001180
bsalomon6eb03cc2014-08-07 14:28:50 -07001181 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001182 FLAGS_samples = 1;
1183 FLAGS_gpuFrameLag = 0;
1184 }
1185
bsalomon6eb03cc2014-08-07 14:28:50 -07001186 if (!FLAGS_writePath.isEmpty()) {
1187 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1188 if (!sk_mkdir(FLAGS_writePath[0])) {
1189 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001190 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001191 }
1192 }
1193
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001194 std::unique_ptr<SkWStream> logStream(new SkNullWStream);
mtklein60317d0f2014-07-14 11:30:37 -07001195 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001196#if defined(SK_RELEASE)
Chris Dalton2e1381e2019-10-23 14:35:19 -06001197 // SkJSONWriter uses a 32k in-memory cache, so it only flushes occasionally and is well
1198 // equipped for a stream that re-opens, appends, and closes the file on every write.
1199 logStream.reset(new NanoFILEAppendAndCloseStream(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001200#else
1201 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1202 return 1;
1203#endif
mtklein60317d0f2014-07-14 11:30:37 -07001204 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001205 NanoJSONResultsWriter log(logStream.get(), SkJSONWriter::Mode::kPretty);
1206 log.beginObject(); // root
mtklein1915b622014-08-20 11:45:00 -07001207
1208 if (1 == FLAGS_properties.count() % 2) {
1209 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1210 return 1;
1211 }
1212 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001213 log.appendString(FLAGS_properties[i-1], FLAGS_properties[i]);
mtklein1915b622014-08-20 11:45:00 -07001214 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001215
1216 if (1 == FLAGS_key.count() % 2) {
1217 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1218 return 1;
1219 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001220 if (FLAGS_key.count()) {
1221 log.beginObject("key");
1222 for (int i = 1; i < FLAGS_key.count(); i += 2) {
1223 log.appendString(FLAGS_key[i - 1], FLAGS_key[i]);
1224 }
1225 log.endObject(); // key
mtklein94e51562014-08-19 12:41:53 -07001226 }
mtklein60317d0f2014-07-14 11:30:37 -07001227
mtkleinf3723212014-06-25 14:08:00 -07001228 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001229 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001230
cdaltone1b89582015-06-25 19:17:08 -07001231 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001232
bsalomon6eb03cc2014-08-07 14:28:50 -07001233 if (kAutoTuneLoops != FLAGS_loops) {
1234 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001235 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001236 SkDebugf("! -> high variance, ? -> moderate variance\n");
1237 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001238 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001239 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001240 } else {
mtkleind75c4662015-04-30 07:11:22 -07001241 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001242 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001243 }
1244
svaisanenc47635e2016-01-28 06:05:43 -08001245 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001246 create_configs(&configs);
1247
msarettc149f0e2016-01-04 11:35:43 -08001248 if (FLAGS_keepAlive) {
1249 start_keepalive();
1250 }
1251
Mike Klein03fa5d42019-03-22 11:39:09 -05001252 SetAnalyticAAFromCommonFlags();
liyuqian38911a72016-10-04 11:23:22 -07001253
Mike Klein2a57e792020-01-14 12:47:52 -06001254 if (FLAGS_forceRasterPipeline) { gSkForceRasterPipelineBlitter = true; }
Mike Kleinea4459d2020-01-16 13:05:04 -06001255 if (FLAGS_skvm) { gUseSkVMBlitter = gSkVMJITViaDylib = true; }
Yuqian Li550148b2017-01-13 10:13:13 -05001256
mtkleine070c2b2014-10-14 08:40:43 -07001257 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001258 BenchmarkStream benchStream;
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001259 log.beginObject("results");
Jim Van Verth8a9a3712019-05-31 10:49:12 -04001260 AutoreleasePool pool;
mtklein92007582014-08-01 07:46:52 -07001261 while (Benchmark* b = benchStream.next()) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001262 std::unique_ptr<Benchmark> bench(b);
Mike Klein88544fb2019-03-20 10:50:33 -05001263 if (CommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001264 continue;
1265 }
1266
svaisanenc47635e2016-01-28 06:05:43 -08001267 if (!configs.empty()) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001268 log.beginBench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001269 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001270 }
egdaniel3bf92062015-06-26 08:12:46 -07001271 for (int i = 0; i < configs.count(); ++i) {
1272 Target* target = is_enabled(b, configs[i]);
1273 if (!target) {
1274 continue;
1275 }
mtkleinf3723212014-06-25 14:08:00 -07001276
halcanary96fcdcc2015-08-27 07:41:13 -07001277 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001278 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001279 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001280
brianosman7a5ada82016-02-08 13:49:12 -08001281 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001282 SkDebugf("Running %s\t%s\n"
1283 , bench->getUniqueName()
1284 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001285 if (FLAGS_dryRun) {
1286 continue;
1287 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001288 }
1289
Mike Kleinee254232019-03-28 08:32:14 -05001290 if (FLAGS_purgeBetweenBenches) {
1291 SkGraphics::PurgeAllCaches();
1292 }
1293
Brian Osman5ad87aa2017-09-08 16:05:23 -04001294 TRACE_EVENT2("skia", "Benchmark", "name", TRACE_STR_COPY(bench->getUniqueName()),
1295 "config", TRACE_STR_COPY(config));
Brian Osmanc3cdef52017-08-31 14:09:22 -04001296
egdaniel3bf92062015-06-26 08:12:46 -07001297 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001298 bench->perCanvasPreDraw(canvas);
1299
cdaltone1b89582015-06-25 19:17:08 -07001300 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001301 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001302 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1303 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001304
Leon Scroggins IIIb30d1132017-10-02 09:48:15 -04001305 if (kFailedLoops == loops) {
1306 // Can't be timed. A warning note has already been printed.
1307 cleanup_run(target);
1308 continue;
1309 }
1310
Yuqian Li3528eb32017-09-21 13:23:49 -04001311 if (runs == 0 && FLAGS_ms < 1000) {
1312 // Run the first bench for 1000ms to warm up the nanobench if FLAGS_ms < 1000.
1313 // Otherwise, the first few benches' measurements will be inaccurate.
1314 auto stop = now_ms() + 1000;
1315 do {
1316 time(loops, bench.get(), target);
Jim Van Verth49d6bca2020-02-28 11:05:44 -05001317 pool.drain();
Yuqian Li3528eb32017-09-21 13:23:49 -04001318 } while (now_ms() < stop);
1319 }
1320
mtkleinbbba1682015-10-28 11:36:30 -07001321 if (FLAGS_ms) {
1322 samples.reset();
1323 auto stop = now_ms() + FLAGS_ms;
1324 do {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001325 samples.push_back(time(loops, bench.get(), target) / loops);
Jim Van Verth49d6bca2020-02-28 11:05:44 -05001326 pool.drain();
mtkleinbbba1682015-10-28 11:36:30 -07001327 } while (now_ms() < stop);
1328 } else {
cdaltone1b89582015-06-25 19:17:08 -07001329 samples.reset(FLAGS_samples);
1330 for (int s = 0; s < FLAGS_samples; s++) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001331 samples[s] = time(loops, bench.get(), target) / loops;
Jim Van Verth49d6bca2020-02-28 11:05:44 -05001332 pool.drain();
cdaltone1b89582015-06-25 19:17:08 -07001333 }
cdaltone1b89582015-06-25 19:17:08 -07001334 }
mtkleinf3723212014-06-25 14:08:00 -07001335
Mike Kleinfec9b902019-05-31 10:03:07 -05001336 // Scale each result to the benchmark's own units, time/unit.
1337 for (double& sample : samples) {
1338 sample *= (1.0 / bench->getUnits());
1339 }
1340
joshualitte45c81c2015-12-02 09:05:37 -08001341 SkTArray<SkString> keys;
1342 SkTArray<double> values;
1343 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1344 if (gpuStatsDump) {
1345 // TODO cache stats
1346 bench->getGpuStats(canvas, &keys, &values);
1347 }
joshualitte45c81c2015-12-02 09:05:37 -08001348
robertphillips5b693772014-11-21 06:19:36 -08001349 bench->perCanvasPostDraw(canvas);
1350
egdaniel3bf92062015-06-26 08:12:46 -07001351 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001352 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001353 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001354 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001355 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001356 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001357 }
1358
Mike Klein512a2442018-09-27 10:44:56 -04001359 // Building stats.plot often shows up in profiles,
1360 // so skip building it when we're not going to print it anyway.
1361 const bool want_plot = !FLAGS_quiet;
1362
1363 Stats stats(samples, want_plot);
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001364 log.beginObject(config);
1365
1366 log.beginObject("options");
1367 log.appendString("name", bench->getName());
1368 benchStream.fillCurrentOptions(log);
1369 target->fillOptions(log);
1370 log.endObject(); // options
1371
1372 // Metrics
1373 log.appendMetric("min_ms", stats.min);
1374 log.beginArray("samples");
1375 for (double sample : samples) {
1376 log.appendDoubleDigits(sample, 16);
1377 }
1378 log.endArray(); // samples
1379 benchStream.fillCurrentMetrics(log);
joshualitte45c81c2015-12-02 09:05:37 -08001380 if (gpuStatsDump) {
1381 // dump to json, only SKPBench currently returns valid keys / values
1382 SkASSERT(keys.count() == values.count());
1383 for (int i = 0; i < keys.count(); i++) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001384 log.appendMetric(keys[i].c_str(), values[i]);
joshualitte45c81c2015-12-02 09:05:37 -08001385 }
1386 }
joshualitte45c81c2015-12-02 09:05:37 -08001387
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001388 log.endObject(); // config
1389
mtkleine070c2b2014-10-14 08:40:43 -07001390 if (runs++ % FLAGS_flushEvery == 0) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001391 log.flush();
mtkleine070c2b2014-10-14 08:40:43 -07001392 }
mtklein60317d0f2014-07-14 11:30:37 -07001393
bsalomon6eb03cc2014-08-07 14:28:50 -07001394 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001395 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001396 config = ""; // Only print the config if we run the same bench on more than one.
1397 }
mtkleind75c4662015-04-30 07:11:22 -07001398 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1399 , sk_tools::getCurrResidentSetSizeMB()
1400 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001401 , bench->getUniqueName()
1402 , config);
mtkleinf3723212014-06-25 14:08:00 -07001403 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001404 const char* mark = " ";
Hal Canarye99e8da2019-01-14 16:34:50 -05001405 const double stddev_percent =
1406 sk_ieee_double_divide(100 * sqrt(stats.var), stats.mean);
mtklein66cfcff2015-12-04 06:35:30 -08001407 if (stddev_percent > 5) mark = "?";
1408 if (stddev_percent > 10) mark = "!";
1409
1410 SkDebugf("%10.2f %s\t%s\t%s\n",
1411 stats.median*1e3, mark, bench->getUniqueName(), config);
Mike Reedc665e5b2017-07-04 22:56:06 -04001412 } else if (FLAGS_csv) {
Hal Canarye99e8da2019-01-14 16:34:50 -05001413 const double stddev_percent =
1414 sk_ieee_double_divide(100 * sqrt(stats.var), stats.mean);
Mike Reedc665e5b2017-07-04 22:56:06 -04001415 SkDebugf("%g,%g,%g,%g,%g,%s,%s\n"
1416 , stats.min
1417 , stats.median
1418 , stats.mean
1419 , stats.max
1420 , stddev_percent
1421 , config
1422 , bench->getUniqueName()
1423 );
1424 } else {
1425 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 -05001426 const double stddev_percent =
1427 sk_ieee_double_divide(100 * sqrt(stats.var), stats.mean);
Mike Reedc665e5b2017-07-04 22:56:06 -04001428 SkDebugf(format
mtkleind75c4662015-04-30 07:11:22 -07001429 , sk_tools::getCurrResidentSetSizeMB()
1430 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001431 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001432 , HUMANIZE(stats.min)
1433 , HUMANIZE(stats.median)
1434 , HUMANIZE(stats.mean)
1435 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001436 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001437 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001438 , config
mtklein96289052014-09-10 12:05:59 -07001439 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001440 );
1441 }
joshualitte45c81c2015-12-02 09:05:37 -08001442
joshualitte45c81c2015-12-02 09:05:37 -08001443 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -04001444 target->dumpStats();
bsalomon06cddec2014-10-24 10:40:50 -07001445 }
joshualitte45c81c2015-12-02 09:05:37 -08001446
cdalton2c56ba52015-06-26 13:32:53 -07001447 if (FLAGS_verbose) {
1448 SkDebugf("Samples: ");
1449 for (int i = 0; i < samples.count(); i++) {
1450 SkDebugf("%s ", HUMANIZE(samples[i]));
1451 }
1452 SkDebugf("%s\n", bench->getUniqueName());
1453 }
egdaniel3bf92062015-06-26 08:12:46 -07001454 cleanup_run(target);
Jim Van Verth8a9a3712019-05-31 10:49:12 -04001455 pool.drain();
mtkleinf3723212014-06-25 14:08:00 -07001456 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001457 if (!configs.empty()) {
1458 log.endBench();
1459 }
mtkleinf3723212014-06-25 14:08:00 -07001460 }
1461
Herb Derby5a523fe2017-01-26 16:48:28 -05001462 SkGraphics::PurgeAllCaches();
1463
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001464 log.beginBench("memory_usage", 0, 0);
1465 log.beginObject("meta"); // config
1466 log.appendS32("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1467 log.endObject(); // config
1468 log.endBench();
1469
Brian Osman68870aa2020-07-08 14:12:43 -04001470 RunSkSLMemoryBenchmarks(&log);
1471
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001472 log.endObject(); // results
1473 log.endObject(); // root
1474 log.flush();
mtkleine1091452014-12-04 10:47:02 -08001475
mtkleinf3723212014-06-25 14:08:00 -07001476 return 0;
1477}