blob: 5755d4c4f476db7ef699045bceca95f7e5a7e1ea [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"
14#include "bench/BitmapRegionDecoderBench.h"
15#include "bench/CodecBench.h"
16#include "bench/CodecBenchPriv.h"
17#include "bench/GMBench.h"
18#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/android/SkBitmapRegionDecoder.h"
24#include "include/codec/SkAndroidCodec.h"
25#include "include/codec/SkCodec.h"
26#include "include/core/SkCanvas.h"
27#include "include/core/SkData.h"
28#include "include/core/SkGraphics.h"
29#include "include/core/SkPictureRecorder.h"
30#include "include/core/SkString.h"
31#include "include/core/SkSurface.h"
Ben Wagneracf98df2019-07-12 12:51:44 -040032#include "include/core/SkTime.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/core/SkAutoMalloc.h"
34#include "src/core/SkBBoxHierarchy.h"
35#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"
40#include "src/utils/SkJSONWriter.h"
41#include "src/utils/SkOSPath.h"
Jim Van Verth8a9a3712019-05-31 10:49:12 -040042#include "tools/AutoreleasePool.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "tools/CrashHandler.h"
44#include "tools/ProcStats.h"
45#include "tools/Stats.h"
46#include "tools/flags/CommonFlags.h"
47#include "tools/flags/CommonFlagsConfig.h"
48#include "tools/ios_utils.h"
49#include "tools/trace/EventTracingPriv.h"
50#include "tools/trace/SkDebugfTracer.h"
mtkleinf3723212014-06-25 14:08:00 -070051
Hal Canary0f666812018-03-22 15:21:12 -040052#ifdef SK_XML
Mike Kleinc0bd9f92019-04-23 12:05:21 -050053#include "experimental/svg/model/SkSVGDOM.h"
Hal Canary0f666812018-03-22 15:21:12 -040054#endif // SK_XML
55
bungeman60e0fee2015-08-26 05:15:46 -070056#include <stdlib.h>
Mike Klein03141d22017-10-30 11:57:15 -040057#include <thread>
bungeman60e0fee2015-08-26 05:15:46 -070058
Mike Reedc928fe22017-06-05 10:20:31 -040059extern bool gSkForceRasterPipelineBlitter;
Mike Klein2a57e792020-01-14 12:47:52 -060060extern bool gUseSkVMBlitter;
Mike Kleinea4459d2020-01-16 13:05:04 -060061extern bool gSkVMJITViaDylib;
Mike Reedc928fe22017-06-05 10:20:31 -040062
Mike Klein8f11d4d2018-01-24 12:42:55 -050063#ifndef SK_BUILD_FOR_WIN
scroggo38ce0a72016-01-07 07:28:47 -080064 #include <unistd.h>
Brian Salomon032aaae2018-03-23 16:10:36 -040065
scroggo38ce0a72016-01-07 07:28:47 -080066#endif
67
Mike Kleinc0bd9f92019-04-23 12:05:21 -050068#include "src/gpu/GrCaps.h"
69#include "src/gpu/GrContextPriv.h"
70#include "src/gpu/SkGr.h"
71#include "src/gpu/gl/GrGLDefines.h"
72#include "src/gpu/gl/GrGLGpu.h"
73#include "src/gpu/gl/GrGLUtil.h"
74#include "tools/gpu/GrContextFactory.h"
Brian Salomon032aaae2018-03-23 16:10:36 -040075
Brian Osmanc7ad40f2018-05-31 14:27:17 -040076using sk_gpu_test::ContextInfo;
77using sk_gpu_test::GrContextFactory;
78using sk_gpu_test::TestContext;
Brian Salomon032aaae2018-03-23 16:10:36 -040079
Brian Osmanc7ad40f2018-05-31 14:27:17 -040080GrContextOptions grContextOpts;
bsalomon682c2692015-05-22 14:01:46 -070081
reed53249782014-10-10 09:09:52 -070082static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070083
bsalomon6eb03cc2014-08-07 14:28:50 -070084static SkString loops_help_txt() {
85 SkString help;
86 help.printf("Number of times to run each bench. Set this to %d to auto-"
87 "tune for each bench. Timings are only reported when auto-tuning.",
88 kAutoTuneLoops);
89 return help;
90}
91
cdaltone1b89582015-06-25 19:17:08 -070092static SkString to_string(int n) {
93 SkString str;
94 str.appendS32(n);
95 return str;
96}
97
Mike Kleinac8f4432019-07-01 10:15:32 -050098static DEFINE_int(loops, kAutoTuneLoops, loops_help_txt().c_str());
bsalomon6eb03cc2014-08-07 14:28:50 -070099
Mike Klein5b3f3432019-03-21 11:42:21 -0500100static DEFINE_int(samples, 10, "Number of samples to measure for each bench.");
101static DEFINE_int(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
102static DEFINE_int(overheadLoops, 100000, "Loops to estimate timer overhead.");
Mike Klein84836b72019-03-21 11:31:36 -0500103static DEFINE_double(overheadGoal, 0.0001,
mtkleinf3723212014-06-25 14:08:00 -0700104 "Loop until timer overhead is at most this fraction of our measurments.");
Mike Klein84836b72019-03-21 11:31:36 -0500105static DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
Mike Klein5b3f3432019-03-21 11:42:21 -0500106static DEFINE_int(gpuFrameLag, 5,
Mike Klein84836b72019-03-21 11:31:36 -0500107 "If unknown, estimated maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -0700108
Mike Klein84836b72019-03-21 11:31:36 -0500109static DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
Mike Klein5b3f3432019-03-21 11:42:21 -0500110static DEFINE_int(maxCalibrationAttempts, 3,
mtklein55b0ffc2014-07-17 08:38:23 -0700111 "Try up to this many times to guess loops for a bench, or skip the bench.");
Mike Klein5b3f3432019-03-21 11:42:21 -0500112static DEFINE_int(maxLoops, 1000000, "Never run a bench more times than this.");
Mike Klein84836b72019-03-21 11:31:36 -0500113static DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
114static DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
115static DEFINE_string(zoom, "1.0,0",
116 "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
117 "function that ping-pongs between 1.0 and zoomMax.");
118static DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
Mike Klein84836b72019-03-21 11:31:36 -0500119static DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
120static DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
Mike Klein5b3f3432019-03-21 11:42:21 -0500121static DEFINE_int(flushEvery, 10, "Flush --outResultsFile every Nth run.");
Mike Klein84836b72019-03-21 11:31:36 -0500122static DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
123static DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
124static DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
125static DEFINE_bool(csv, false, "Print status in CSV format");
126static DEFINE_string(sourceType, "",
mtklein65dfd2f2016-02-03 10:40:54 -0800127 "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
Mike Klein84836b72019-03-21 11:31:36 -0500128static DEFINE_string(benchType, "",
129 "Apply usual --match rules to bench type: micro, recording, "
130 "piping, playback, skcodec, etc.");
mtklein65dfd2f2016-02-03 10:40:54 -0800131
Mike Klein84836b72019-03-21 11:31:36 -0500132static DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
Mike Kleinea4459d2020-01-16 13:05:04 -0600133static DEFINE_bool(skvm, false, "sets gUseSkVMBlitter and gSkVMJITViaDylib");
Mike Reedc928fe22017-06-05 10:20:31 -0400134
Mike Klein19fb3972019-03-21 13:08:08 -0500135static DEFINE_bool2(pre_log, p, false,
136 "Log before running each test. May be incomprehensible when threading");
137
Mike Klein629f5fc2019-03-22 14:55:19 -0500138static DEFINE_bool(cpu, true, "master switch for running CPU-bound work.");
139static DEFINE_bool(gpu, true, "master switch for running GPU-bound work.");
140static DEFINE_bool(dryRun, false,
141 "just print the tests that would be run, without actually running them.");
142static DEFINE_string(images, "",
143 "List of images and/or directories to decode. A directory with no images"
144 " is treated as a fatal error.");
145static DEFINE_bool(simpleCodec, false,
146 "Runs of a subset of the codec tests, always N32, Premul or Opaque");
147
Mike Kleinc6142d82019-03-25 10:54:59 -0500148static DEFINE_string2(match, m, nullptr,
149 "[~][^]substring[$] [...] of name to run.\n"
150 "Multiple matches may be separated by spaces.\n"
151 "~ causes a matching name to always be skipped\n"
152 "^ requires the start of the name to match\n"
153 "$ requires the end of the name to match\n"
154 "^ and $ requires an exact match\n"
155 "If a name does not match any list entry,\n"
156 "it is skipped unless some list entry starts with ~");
157
158static DEFINE_bool2(quiet, q, false, "if true, don't print status updates.");
159static DEFINE_bool2(verbose, v, false, "enable verbose output from the test driver.");
160
161
162static DEFINE_string(skps, "skps", "Directory to read skps from.");
163static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
Hal Canary5dfefa22019-10-24 13:52:17 -0400164static DEFINE_string(texttraces, "", "Directory to read TextBlobTrace files from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500165
166static DEFINE_int_2(threads, j, -1,
167 "Run threadsafe tests on a threadpool with this many extra threads, "
168 "defaulting to one extra thread per core.");
169
170static DEFINE_string2(writePath, w, "", "If set, write bitmaps here as .pngs.");
171
172static DEFINE_string(key, "",
173 "Space-separated key/value pairs to add to JSON identifying this builder.");
174static DEFINE_string(properties, "",
175 "Space-separated key/value pairs to add to JSON identifying this run.");
176
Mike Kleinee254232019-03-28 08:32:14 -0500177static DEFINE_bool(purgeBetweenBenches, false,
178 "Call SkGraphics::PurgeAllCaches() between each benchmark?");
179
mtkleinbbba1682015-10-28 11:36:30 -0700180static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
181
mtkleinf3723212014-06-25 14:08:00 -0700182static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700183 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800184 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700185}
mtklein55b0ffc2014-07-17 08:38:23 -0700186#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700187
tomhudsond968a6f2015-03-26 11:28:06 -0700188bool Target::init(SkImageInfo info, Benchmark* bench) {
189 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700190 this->surface = SkSurface::MakeRaster(info);
191 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700192 return false;
193 }
194 }
195 return true;
196}
197bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700198 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700199 if (!canvas) {
200 return false;
201 }
Mike Reed12e946b2017-04-17 10:53:29 -0400202 bmp->allocPixels(canvas->imageInfo());
203 if (!canvas->readPixels(*bmp, 0, 0)) {
tomhudsond968a6f2015-03-26 11:28:06 -0700204 SkDebugf("Can't read canvas pixels.\n");
205 return false;
206 }
207 return true;
208}
209
tomhudsond968a6f2015-03-26 11:28:06 -0700210struct GPUTarget : public Target {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400211 explicit GPUTarget(const Config& c) : Target(c) {}
212 ContextInfo contextInfo;
213 std::unique_ptr<GrContextFactory> factory;
tomhudsond968a6f2015-03-26 11:28:06 -0700214
215 void setup() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400216 this->contextInfo.testContext()->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700217 // Make sure we're done with whatever came before.
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400218 this->contextInfo.testContext()->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700219 }
220 void endTiming() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400221 if (this->contextInfo.testContext()) {
222 this->contextInfo.testContext()->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700223 }
224 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400225 void fence() override { this->contextInfo.testContext()->finish(); }
mtkleind75c4662015-04-30 07:11:22 -0700226
cdaltond416a5b2015-06-23 13:23:44 -0700227 bool needsFrameTiming(int* maxFrameLag) const override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400228 if (!this->contextInfo.testContext()->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700229 // Frame lag is unknown.
230 *maxFrameLag = FLAGS_gpuFrameLag;
231 }
232 return true;
233 }
tomhudsond968a6f2015-03-26 11:28:06 -0700234 bool init(SkImageInfo info, Benchmark* bench) override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400235 GrContextOptions options = grContextOpts;
236 bench->modifyGrContextOptions(&options);
237 this->factory.reset(new GrContextFactory(options));
bsalomonafcd7cd2015-08-31 12:39:41 -0700238 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
239 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700240 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400241 this->surface = SkSurface::MakeRenderTarget(
242 this->factory->get(this->config.ctxType, this->config.ctxOverrides),
243 SkBudgeted::kNo, info, this->config.samples, &props);
244 this->contextInfo =
245 this->factory->getContextInfo(this->config.ctxType, this->config.ctxOverrides);
tomhudsond968a6f2015-03-26 11:28:06 -0700246 if (!this->surface.get()) {
247 return false;
248 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400249 if (!this->contextInfo.testContext()->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700250 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800251 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700252 }
tomhudsond968a6f2015-03-26 11:28:06 -0700253 return true;
254 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500255 void fillOptions(NanoJSONResultsWriter& log) override {
tomhudsond968a6f2015-03-26 11:28:06 -0700256 const GrGLubyte* version;
Greg Danielbdf12ad2018-10-12 09:31:11 -0400257 if (this->contextInfo.backend() == GrBackendApi::kOpenGL) {
Brian Salomon032aaae2018-03-23 16:10:36 -0400258 const GrGLInterface* gl =
Robert Phillips9da87e02019-02-04 13:26:26 -0500259 static_cast<GrGLGpu*>(this->contextInfo.grContext()->priv().getGpu())
Brian Salomon032aaae2018-03-23 16:10:36 -0400260 ->glInterface();
bsalomonc8699322016-05-11 11:55:36 -0700261 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500262 log.appendString("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700263
bsalomonc8699322016-05-11 11:55:36 -0700264 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500265 log.appendString("GL_RENDERER", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700266
bsalomonc8699322016-05-11 11:55:36 -0700267 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VENDOR));
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500268 log.appendString("GL_VENDOR", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700269
bsalomonc8699322016-05-11 11:55:36 -0700270 GR_GL_CALL_RET(gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
Brian Osman8c0a1ca2019-01-28 14:24:29 -0500271 log.appendString("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
bsalomonc8699322016-05-11 11:55:36 -0700272 }
tomhudsond968a6f2015-03-26 11:28:06 -0700273 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400274
275 void dumpStats() override {
Robert Phillips9da87e02019-02-04 13:26:26 -0500276 this->contextInfo.grContext()->priv().printCacheStats();
277 this->contextInfo.grContext()->priv().printGpuStats();
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400278 }
tomhudsond968a6f2015-03-26 11:28:06 -0700279};
mtkleind75c4662015-04-30 07:11:22 -0700280
tomhudson75a0ebb2015-03-27 12:11:44 -0700281static double time(int loops, Benchmark* bench, Target* target) {
282 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700283 if (canvas) {
284 canvas->clear(SK_ColorWHITE);
285 }
joshualitt8a6697a2015-09-30 12:11:07 -0700286 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700287 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700288 canvas = target->beginTiming(canvas);
289 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700290 if (canvas) {
291 canvas->flush();
292 }
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
svaisanenc47635e2016-01-28 06:05:43 -0800448static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
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());
svaisanenc47635e2016-01-28 06:05:43 -0800452 return;
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();
459 auto colorSpace = gpuConfig->getColorSpace();
Brian Salomonf865b052018-03-09 09:01:53 -0500460 if (gpuConfig->getSurfType() != SkCommandLineConfigGpu::SurfType::kDefault) {
461 SkDebugf("This tool only supports the default surface type.");
462 return;
463 }
svaisanenc47635e2016-01-28 06:05:43 -0800464
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400465 GrContextFactory factory(grContextOpts);
466 if (const GrContext* ctx = factory.get(ctxType, ctxOverrides)) {
Greg Daniel6fa62e22019-08-07 15:52:37 -0400467 GrBackendFormat format = ctx->defaultBackendFormat(colorType, GrRenderable::kYes);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500468 int supportedSampleCount =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400469 ctx->priv().caps()->getRenderTargetSampleCount(sampleCount, format);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400470 if (sampleCount != supportedSampleCount) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400471 SkDebugf("Configuration '%s' sample count %d is not a supported sample count.\n",
472 config->getTag().c_str(), sampleCount);
svaisanenc47635e2016-01-28 06:05:43 -0800473 return;
474 }
475 } else {
Ben Wagner32fa5102017-08-10 21:25:55 -0400476 SkDebugf("No context was available matching config '%s'.\n",
477 config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800478 return;
479 }
480
481 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800482 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800483 Benchmark::kGPU_Backend,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400484 colorType,
svaisanenc47635e2016-01-28 06:05:43 -0800485 kPremul_SkAlphaType,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400486 sk_ref_sp(colorSpace),
svaisanenc47635e2016-01-28 06:05:43 -0800487 sampleCount,
488 ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700489 ctxOverrides,
kkinnunend4e1c352016-03-01 23:41:26 -0800490 gpuConfig->getUseDIText()
491 };
svaisanenc47635e2016-01-28 06:05:43 -0800492
493 configs->push_back(target);
494 return;
495 }
svaisanenc47635e2016-01-28 06:05:43 -0800496
brianosmanb109b8c2016-06-16 13:03:24 -0700497 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
498 if (config->getTag().equals(#name)) { \
Ben Wagner32fa5102017-08-10 21:25:55 -0400499 if (!FLAGS_cpu) { \
500 SkDebugf("Skipping config '%s' as requested.\n", \
501 config->getTag().c_str()); \
502 return; \
503 } \
brianosmanb109b8c2016-06-16 13:03:24 -0700504 Config config = { \
505 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
csmartdaltone812d492017-02-21 12:36:05 -0700506 0, kBogusContextType, kBogusContextOverrides, false \
brianosmanb109b8c2016-06-16 13:03:24 -0700507 }; \
508 configs->push_back(config); \
509 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700510 }
mtkleine714e752014-07-31 12:13:48 -0700511
Ben Wagner32fa5102017-08-10 21:25:55 -0400512 CPU_CONFIG(nonrendering, kNonRendering_Backend,
513 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800514
Mike Klein0e4041f2018-06-19 16:00:40 -0400515 CPU_CONFIG(a8, kRaster_Backend, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr)
516 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType, nullptr)
517 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
518
519 // 'narrow' has a gamut narrower than sRGB, and different transfer function.
Brian Osman82ebe042019-01-04 17:03:00 -0500520 auto narrow = SkColorSpace::MakeRGB(SkNamedTransferFn::k2Dot2, gNarrow_toXYZD50),
Mike Klein0e4041f2018-06-19 16:00:40 -0400521 srgb = SkColorSpace::MakeSRGB(),
522 srgbLinear = SkColorSpace::MakeSRGBLinear();
523
524 CPU_CONFIG( f16, kRaster_Backend, kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbLinear)
525 CPU_CONFIG( srgb, kRaster_Backend, kRGBA_8888_SkColorType, kPremul_SkAlphaType, srgb )
526 CPU_CONFIG( esrgb, kRaster_Backend, kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgb )
527 CPU_CONFIG( narrow, kRaster_Backend, kRGBA_8888_SkColorType, kPremul_SkAlphaType, narrow )
528 CPU_CONFIG(enarrow, kRaster_Backend, kRGBA_F16_SkColorType, kPremul_SkAlphaType, narrow )
mtkleinbb6a0282014-07-01 08:43:42 -0700529
svaisanenc47635e2016-01-28 06:05:43 -0800530 #undef CPU_CONFIG
Ben Wagner32fa5102017-08-10 21:25:55 -0400531
532 SkDebugf("Unknown config '%s'.\n", config->getTag().c_str());
mtkleinf3723212014-06-25 14:08:00 -0700533}
534
svaisanenc47635e2016-01-28 06:05:43 -0800535// Append all configs that are enabled and supported.
536void create_configs(SkTArray<Config>* configs) {
537 SkCommandLineConfigArray array;
538 ParseConfigs(FLAGS_config, &array);
539 for (int i = 0; i < array.count(); ++i) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400540 create_config(array[i].get(), configs);
svaisanenc47635e2016-01-28 06:05:43 -0800541 }
Ben Wagner32fa5102017-08-10 21:25:55 -0400542
543 // If no just default configs were requested, then we're okay.
544 if (array.count() == 0 || FLAGS_config.count() == 0 ||
Ben Wagner32fa5102017-08-10 21:25:55 -0400545 // Otherwise, make sure that all specified configs have been created.
546 array.count() == configs->count()) {
547 return;
548 }
Ben Wagner32fa5102017-08-10 21:25:55 -0400549 exit(1);
svaisanenc47635e2016-01-28 06:05:43 -0800550}
551
brianosman9f1f6e22016-09-15 08:33:02 -0700552// disable warning : switch statement contains default but no 'case' labels
553#if defined _WIN32
554#pragma warning ( push )
555#pragma warning ( disable : 4065 )
556#endif
557
halcanary96fcdcc2015-08-27 07:41:13 -0700558// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700559static Target* is_enabled(Benchmark* bench, const Config& config) {
560 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700561 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700562 }
563
reede5ea5002014-09-03 11:54:58 -0700564 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700565 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700566
halcanary96fcdcc2015-08-27 07:41:13 -0700567 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700568
tomhudsond968a6f2015-03-26 11:28:06 -0700569 switch (config.backend) {
tomhudsond968a6f2015-03-26 11:28:06 -0700570 case Benchmark::kGPU_Backend:
571 target = new GPUTarget(config);
572 break;
tomhudsond968a6f2015-03-26 11:28:06 -0700573 default:
574 target = new Target(config);
575 break;
576 }
bsalomonc2553372014-07-22 13:09:05 -0700577
tomhudsond968a6f2015-03-26 11:28:06 -0700578 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700579 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700580 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700581 }
582 return target;
583}
584
brianosman9f1f6e22016-09-15 08:33:02 -0700585#if defined _WIN32
586#pragma warning ( pop )
587#endif
588
reed42943c82016-09-12 12:01:44 -0700589static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700590 uint32_t minOutputSize, int* width, int* height) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400591 std::unique_ptr<SkBitmapRegionDecoder> brd(
msarettd1227a72016-05-18 06:23:57 -0700592 SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy));
msarett7f691442015-09-22 11:56:16 -0700593 if (nullptr == brd.get()) {
594 // This is indicates that subset decoding is not supported for a particular image format.
595 return false;
596 }
597
msarett7f691442015-09-22 11:56:16 -0700598 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
599 (uint32_t) brd->height()) {
600 // This indicates that the image is not large enough to decode a
601 // minOutputSize x minOutputSize subset at the given sampleSize.
602 return false;
603 }
604
605 // Set the image width and height. The calling code will use this to choose subsets to decode.
606 *width = brd->width();
607 *height = brd->height();
608 return true;
609}
610
egdaniel3bf92062015-06-26 08:12:46 -0700611static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700612 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700613}
614
Mike Klein88544fb2019-03-20 10:50:33 -0500615static void collect_files(const CommandLineFlags::StringArray& paths,
616 const char* ext,
617 SkTArray<SkString>* list) {
fmalita6519c212016-09-14 08:05:17 -0700618 for (int i = 0; i < paths.count(); ++i) {
619 if (SkStrEndsWith(paths[i], ext)) {
620 list->push_back(SkString(paths[i]));
621 } else {
622 SkOSFile::Iter it(paths[i], ext);
623 SkString path;
624 while (it.next(&path)) {
625 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
626 }
627 }
628 }
629}
630
mtkleine714e752014-07-31 12:13:48 -0700631class BenchmarkStream {
632public:
mtklein92007582014-08-01 07:46:52 -0700633 BenchmarkStream() : fBenches(BenchRegistry::Head())
Hal Canary2362ae72019-10-24 13:40:40 -0400634 , fGMs(skiagm::GMRegistry::Head()) {
fmalita6519c212016-09-14 08:05:17 -0700635 collect_files(FLAGS_skps, ".skp", &fSKPs);
636 collect_files(FLAGS_svgs, ".svg", &fSVGs);
Hal Canary5dfefa22019-10-24 13:52:17 -0400637 collect_files(FLAGS_texttraces, ".trace", &fTextBlobTraces);
mtkleine714e752014-07-31 12:13:48 -0700638
mtklein92007582014-08-01 07:46:52 -0700639 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
640 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
641 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
642 exit(1);
643 }
644
645 for (int i = 0; i < FLAGS_scales.count(); i++) {
646 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
647 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
648 exit(1);
649 }
650 }
robertphillips5b693772014-11-21 06:19:36 -0800651
cdalton63a82852015-06-29 14:06:10 -0700652 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
653 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700654 exit(1);
655 }
656
robertphillips5b693772014-11-21 06:19:36 -0800657 if (FLAGS_mpd) {
658 fUseMPDs.push_back() = true;
659 }
mtkleinc751ecb2015-06-15 08:56:38 -0700660 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700661
msarett95f192d2015-02-13 09:05:41 -0800662 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700663 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800664 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800665 }
mtklein95553d92015-03-12 08:24:21 -0700666
msarett95f192d2015-02-13 09:05:41 -0800667 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700668 fColorTypes.push_back(kN32_SkColorType);
669 if (!FLAGS_simpleCodec) {
670 fColorTypes.push_back(kRGB_565_SkColorType);
671 fColorTypes.push_back(kAlpha_8_SkColorType);
msarett67cb6662016-06-21 08:49:26 -0700672 fColorTypes.push_back(kGray_8_SkColorType);
673 }
mtklein92007582014-08-01 07:46:52 -0700674 }
675
reedca2622b2016-03-18 07:25:55 -0700676 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700677 // Not strictly necessary, as it will be checked again later,
678 // but helps to avoid a lot of pointless work if we're going to skip it.
Mike Klein88544fb2019-03-20 10:50:33 -0500679 if (CommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700680 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700681 }
682
bungemanf93d7112016-09-16 06:24:20 -0700683 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
684 if (!stream) {
mtkleinfd731ce2014-09-10 12:19:30 -0700685 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700686 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700687 }
688
reedca2622b2016-03-18 07:25:55 -0700689 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700690 }
691
fmalita6519c212016-09-14 08:05:17 -0700692 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
Brian Osman133823d2018-09-19 14:14:15 -0400693 sk_sp<SkData> data(SkData::MakeFromFileName(path));
694 if (!data) {
fmalita6519c212016-09-14 08:05:17 -0700695 SkDebugf("Could not read %s.\n", path);
696 return nullptr;
697 }
698
Hal Canary0f666812018-03-22 15:21:12 -0400699#ifdef SK_XML
Brian Osman133823d2018-09-19 14:14:15 -0400700 SkMemoryStream stream(std::move(data));
fmalitae1baa7c2016-09-14 12:04:30 -0700701 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
fmalita6519c212016-09-14 08:05:17 -0700702 if (!svgDom) {
703 SkDebugf("Could not parse %s.\n", path);
704 return nullptr;
705 }
706
fmalitae1baa7c2016-09-14 12:04:30 -0700707 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
708 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
709 if (svgDom->containerSize().isEmpty()) {
710 svgDom->setContainerSize(kDefaultContainerSize);
711 }
712
fmalita6519c212016-09-14 08:05:17 -0700713 SkPictureRecorder recorder;
fmalitae1baa7c2016-09-14 12:04:30 -0700714 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
715 svgDom->containerSize().height()));
fmalita6519c212016-09-14 08:05:17 -0700716 return recorder.finishRecordingAsPicture();
Hal Canary0f666812018-03-22 15:21:12 -0400717#else
718 return nullptr;
719#endif // SK_XML
fmalita6519c212016-09-14 08:05:17 -0700720 }
721
mtklein92007582014-08-01 07:46:52 -0700722 Benchmark* next() {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400723 std::unique_ptr<Benchmark> bench;
mtklein65dfd2f2016-02-03 10:40:54 -0800724 do {
725 bench.reset(this->rawNext());
726 if (!bench) {
727 return nullptr;
728 }
Mike Klein88544fb2019-03-20 10:50:33 -0500729 } while (CommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
730 CommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700731 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800732 }
733
734 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700735 if (fBenches) {
Hal Canary972eba32018-07-30 17:07:07 -0400736 Benchmark* bench = fBenches->get()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700737 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700738 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700739 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700740 return bench;
741 }
mtklein92007582014-08-01 07:46:52 -0700742
mtkleine714e752014-07-31 12:13:48 -0700743 while (fGMs) {
Ben Wagner406ff502019-08-12 16:39:24 -0400744 std::unique_ptr<skiagm::GM> gm = fGMs->get()();
mtkleine714e752014-07-31 12:13:48 -0700745 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800746 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700747 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700748 fBenchType = "micro";
Ben Wagner406ff502019-08-12 16:39:24 -0400749 return new GMBench(std::move(gm));
mtkleine714e752014-07-31 12:13:48 -0700750 }
751 }
mtklein92007582014-08-01 07:46:52 -0700752
Hal Canary5dfefa22019-10-24 13:52:17 -0400753 while (fCurrentTextBlobTrace < fTextBlobTraces.count()) {
754 SkString path = fTextBlobTraces[fCurrentTextBlobTrace++];
755 SkString basename = SkOSPath::Basename(path.c_str());
756 static constexpr char kEnding[] = ".trace";
757 if (basename.endsWith(kEnding)) {
758 basename.remove(basename.size() - strlen(kEnding), strlen(kEnding));
759 }
760 fSourceType = "texttrace";
761 fBenchType = "micro";
762 return CreateDiffCanvasBench(
763 SkStringPrintf("SkDiffBench-%s", basename.c_str()),
764 [path](){ return SkStream::MakeFromFile(path.c_str()); });
765 }
766
mtkleinfd731ce2014-09-10 12:19:30 -0700767 // First add all .skps as RecordingBenches.
768 while (fCurrentRecording < fSKPs.count()) {
769 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700770 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
771 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700772 continue;
773 }
774 SkString name = SkOSPath::Basename(path.c_str());
775 fSourceType = "skp";
776 fBenchType = "recording";
Mike Klein88d90712018-01-27 17:30:04 +0000777 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
778 fSKPOps = pic->approximateOpCount();
Mike Kleina705cb92019-05-14 12:33:40 -0500779 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700780 }
781
Mike Reede45ff462017-12-06 10:47:03 -0500782 // Add all .skps as DeserializePictureBenchs.
783 while (fCurrentDeserialPicture < fSKPs.count()) {
784 const SkString& path = fSKPs[fCurrentDeserialPicture++];
785 sk_sp<SkData> data = SkData::MakeFromFileName(path.c_str());
786 if (!data) {
787 continue;
788 }
789 SkString name = SkOSPath::Basename(path.c_str());
790 fSourceType = "skp";
791 fBenchType = "deserial";
Mike Reed7557bbb2017-12-24 19:50:57 -0500792 fSKPBytes = static_cast<double>(data->size());
Mike Klein88d90712018-01-27 17:30:04 +0000793 fSKPOps = 0;
Mike Reede45ff462017-12-06 10:47:03 -0500794 return new DeserializePictureBench(name.c_str(), std::move(data));
795 }
796
mtkleinfd731ce2014-09-10 12:19:30 -0700797 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700798 while (fCurrentScale < fScales.count()) {
799 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800800 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700801 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
802 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800803 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700804 continue;
805 }
robertphillips5b693772014-11-21 06:19:36 -0800806
807 while (fCurrentUseMPD < fUseMPDs.count()) {
808 if (FLAGS_bbh) {
809 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
810 SkRTreeFactory factory;
811 SkPictureRecorder recorder;
robertphillips5b693772014-11-21 06:19:36 -0800812 pic->playback(recorder.beginRecording(pic->cullRect().width(),
813 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800814 &factory,
robertphillipsdda54452016-07-13 13:27:16 -0700815 0));
reedca2622b2016-03-18 07:25:55 -0700816 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800817 }
818 SkString name = SkOSPath::Basename(path.c_str());
819 fSourceType = "skp";
820 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700821 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
822 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700823 }
robertphillips5b693772014-11-21 06:19:36 -0800824 fCurrentUseMPD = 0;
825 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700826 }
fmalita6519c212016-09-14 08:05:17 -0700827
828 while (fCurrentSVG++ < fSVGs.count()) {
829 const char* path = fSVGs[fCurrentSVG - 1].c_str();
830 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
831 fSourceType = "svg";
832 fBenchType = "playback";
833 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
834 fScales[fCurrentScale], false, FLAGS_loopSKP);
835 }
836 }
837
mtklein92007582014-08-01 07:46:52 -0700838 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700839 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700840 fCurrentScale++;
841 }
842
joshualitt261c3ad2015-04-27 09:16:57 -0700843 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700844 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700845 while (fCurrentAnimSKP < fSKPs.count()) {
846 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700847 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
848 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700849 fCurrentAnimSKP++;
850 continue;
851 }
852
853 fCurrentAnimSKP++;
854 SkString name = SkOSPath::Basename(path.c_str());
Ben Wagneracf98df2019-07-12 12:51:44 -0400855 sk_sp<SKPAnimationBench::Animation> animation =
856 SKPAnimationBench::MakeZoomAnimation(fZoomMax, fZoomPeriodMs);
857 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, std::move(animation),
halcanary385fe4d2015-08-26 13:07:48 -0700858 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700859 }
860 }
861
scroggo60869a42015-04-01 12:09:17 -0700862 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700863 fSourceType = "image";
864 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700865 const SkString& path = fImages[fCurrentCodec];
Mike Klein88544fb2019-03-20 10:50:33 -0500866 if (CommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
mtklein6f0ff912016-01-11 09:04:21 -0800867 continue;
868 }
bungeman38d909e2016-08-02 14:40:46 -0700869 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400870 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700871 if (!codec) {
872 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700873 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700874 continue;
875 }
scroggo21027992015-04-02 13:22:38 -0700876
scroggo60869a42015-04-01 12:09:17 -0700877 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700878 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700879
msarettc7796b92016-01-07 14:20:20 -0800880 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700881 if (FLAGS_simpleCodec) {
882 if (kUnpremul_SkAlphaType == alphaType) {
883 alphaType = kPremul_SkAlphaType;
884 }
885
886 fCurrentColorType++;
887 } else {
888 switch (alphaType) {
889 case kOpaque_SkAlphaType:
890 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800891 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700892 break;
893 case kUnpremul_SkAlphaType:
894 case kPremul_SkAlphaType:
895 if (0 == fCurrentAlphaType) {
896 // Test unpremul first.
897 alphaType = kUnpremul_SkAlphaType;
898 fCurrentAlphaType++;
899 } else {
900 // Test premul.
901 alphaType = kPremul_SkAlphaType;
902 fCurrentAlphaType = 0;
903 fCurrentColorType++;
904 }
905 break;
906 default:
907 SkASSERT(false);
908 fCurrentColorType++;
909 break;
910 }
scroggo21027992015-04-02 13:22:38 -0700911 }
912
msarettc7796b92016-01-07 14:20:20 -0800913 // Make sure we can decode to this color type and alpha type.
914 SkImageInfo info =
915 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700916 const size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400917 SkAutoMalloc storage(info.computeByteSize(rowBytes));
scroggo21027992015-04-02 13:22:38 -0700918
scroggoeb602a52015-07-09 08:16:03 -0700919 const SkCodec::Result result = codec->getPixels(
Leon Scroggins571b30f2017-07-11 17:35:31 +0000920 info, storage.get(), rowBytes);
scroggo60869a42015-04-01 12:09:17 -0700921 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700922 case SkCodec::kSuccess:
923 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700924 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700925 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700926 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700927 // This is okay. Not all conversions are valid.
928 break;
scroggo60869a42015-04-01 12:09:17 -0700929 default:
930 // This represents some sort of failure.
931 SkASSERT(false);
932 break;
933 }
934 }
935 fCurrentColorType = 0;
936 }
937
msarett84451022016-02-11 06:45:51 -0800938 // Run AndroidCodecBenches
939 const int sampleSizes[] = { 2, 4, 8 };
940 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
941 fSourceType = "image";
942 fBenchType = "skandroidcodec";
943
944 const SkString& path = fImages[fCurrentAndroidCodec];
Mike Klein88544fb2019-03-20 10:50:33 -0500945 if (CommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
msarett84451022016-02-11 06:45:51 -0800946 continue;
947 }
bungeman38d909e2016-08-02 14:40:46 -0700948 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400949 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800950 if (!codec) {
951 // Nothing to time.
952 SkDebugf("Cannot find codec for %s\n", path.c_str());
953 continue;
954 }
955
956 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
957 int sampleSize = sampleSizes[fCurrentSampleSize];
958 fCurrentSampleSize++;
959 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
960 // Avoid benchmarking scaled decodes of already small images.
961 break;
962 }
963
bungeman38d909e2016-08-02 14:40:46 -0700964 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
965 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -0800966 }
967 fCurrentSampleSize = 0;
968 }
969
msarett7f691442015-09-22 11:56:16 -0700970 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700971 // We intend to create benchmarks that model the use cases in
972 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
973 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
974 // the image. For that reason, we will benchmark decodes in five representative locations
975 // in the image. Additionally, this use case utilizes power of two scaling, so we will
976 // test on power of two sample sizes. The output tile is always 512x512, so, when a
977 // sampleSize is used, the size of the subset that is decoded is always
978 // (sampleSize*512)x(sampleSize*512).
979 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700980 // All use cases we are aware of only scale by powers of two.
981 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
982 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800983 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700984 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800985 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700986 fSourceType = "image";
987 fBenchType = "BRD";
988
mtklein6f0ff912016-01-11 09:04:21 -0800989 const SkString& path = fImages[fCurrentBRDImage];
Mike Klein88544fb2019-03-20 10:50:33 -0500990 if (CommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
mtklein6f0ff912016-01-11 09:04:21 -0800991 continue;
992 }
scroggo860e8a62015-10-15 07:51:28 -0700993
msarettd1227a72016-05-18 06:23:57 -0700994 while (fCurrentColorType < fColorTypes.count()) {
995 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
996 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700997
bungeman38d909e2016-08-02 14:40:46 -0700998 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -0700999 const SkColorType colorType = fColorTypes[fCurrentColorType];
1000 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
1001 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -07001002
msarettd1227a72016-05-18 06:23:57 -07001003 int width = 0;
1004 int height = 0;
reed42943c82016-09-12 12:01:44 -07001005 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -07001006 &width, &height)) {
1007 break;
msarett7f691442015-09-22 11:56:16 -07001008 }
msarettd1227a72016-05-18 06:23:57 -07001009
1010 SkString basename = SkOSPath::Basename(path.c_str());
1011 SkIRect subset;
1012 const uint32_t subsetSize = sampleSize * minOutputSize;
1013 switch (currentSubsetType) {
1014 case kTopLeft_SubsetType:
1015 basename.append("_TopLeft");
1016 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
1017 break;
1018 case kTopRight_SubsetType:
1019 basename.append("_TopRight");
1020 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
1021 subsetSize);
1022 break;
1023 case kMiddle_SubsetType:
1024 basename.append("_Middle");
1025 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
1026 (height - subsetSize) / 2, subsetSize, subsetSize);
1027 break;
1028 case kBottomLeft_SubsetType:
1029 basename.append("_BottomLeft");
1030 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
1031 subsetSize);
1032 break;
1033 case kBottomRight_SubsetType:
1034 basename.append("_BottomRight");
1035 subset = SkIRect::MakeXYWH(width - subsetSize,
1036 height - subsetSize, subsetSize, subsetSize);
1037 break;
1038 default:
1039 SkASSERT(false);
1040 }
1041
1042 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
1043 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -07001044 }
msarettd1227a72016-05-18 06:23:57 -07001045 fCurrentSubsetType = 0;
1046 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -07001047 }
msarettd1227a72016-05-18 06:23:57 -07001048 fCurrentSampleSize = 0;
1049 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -07001050 }
msarettd1227a72016-05-18 06:23:57 -07001051 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -07001052 }
1053
halcanary96fcdcc2015-08-27 07:41:13 -07001054 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001055 }
mtklein92007582014-08-01 07:46:52 -07001056
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001057 void fillCurrentOptions(NanoJSONResultsWriter& log) const {
1058 log.appendString("source_type", fSourceType);
1059 log.appendString("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001060 if (0 == strcmp(fSourceType, "skp")) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001061 log.appendString("clip",
mtklein92007582014-08-01 07:46:52 -07001062 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1063 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001064 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001065 log.appendString("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001066 if (fCurrentUseMPD > 0) {
1067 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001068 log.appendString("multi_picture_draw",
1069 fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
robertphillips5b693772014-11-21 06:19:36 -08001070 }
mtklein92007582014-08-01 07:46:52 -07001071 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001072 }
1073
1074 void fillCurrentMetrics(NanoJSONResultsWriter& log) const {
mtklein051e56d2014-12-04 08:46:51 -08001075 if (0 == strcmp(fBenchType, "recording")) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001076 log.appendMetric("bytes", fSKPBytes);
1077 log.appendMetric("ops", fSKPOps);
mtklein051e56d2014-12-04 08:46:51 -08001078 }
mtklein92007582014-08-01 07:46:52 -07001079 }
1080
mtkleine714e752014-07-31 12:13:48 -07001081private:
msarettb23e6aa2015-06-09 13:56:10 -07001082 enum SubsetType {
1083 kTopLeft_SubsetType = 0,
1084 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001085 kMiddle_SubsetType = 2,
1086 kBottomLeft_SubsetType = 3,
1087 kBottomRight_SubsetType = 4,
1088 kTranslate_SubsetType = 5,
1089 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001090 kLast_SubsetType = kZoom_SubsetType,
1091 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001092 };
1093
mtkleine714e752014-07-31 12:13:48 -07001094 const BenchRegistry* fBenches;
1095 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001096 SkIRect fClip;
1097 SkTArray<SkScalar> fScales;
1098 SkTArray<SkString> fSKPs;
fmalita6519c212016-09-14 08:05:17 -07001099 SkTArray<SkString> fSVGs;
Hal Canary5dfefa22019-10-24 13:52:17 -04001100 SkTArray<SkString> fTextBlobTraces;
robertphillips5b693772014-11-21 06:19:36 -08001101 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001102 SkTArray<SkString> fImages;
msarett74deb982015-10-20 16:45:56 -07001103 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001104 SkScalar fZoomMax;
1105 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001106
Mike Klein88d90712018-01-27 17:30:04 +00001107 double fSKPBytes, fSKPOps;
mtklein051e56d2014-12-04 08:46:51 -08001108
mtkleinfd731ce2014-09-10 12:19:30 -07001109 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1110 const char* fBenchType; // How we bench it: micro, recording, playback, ...
Hal Canary2362ae72019-10-24 13:40:40 -04001111 int fCurrentRecording = 0;
1112 int fCurrentDeserialPicture = 0;
1113 int fCurrentScale = 0;
1114 int fCurrentSKP = 0;
1115 int fCurrentSVG = 0;
Hal Canary5dfefa22019-10-24 13:52:17 -04001116 int fCurrentTextBlobTrace = 0;
Hal Canary2362ae72019-10-24 13:40:40 -04001117 int fCurrentUseMPD = 0;
1118 int fCurrentCodec = 0;
1119 int fCurrentAndroidCodec = 0;
1120 int fCurrentBRDImage = 0;
1121 int fCurrentColorType = 0;
1122 int fCurrentAlphaType = 0;
1123 int fCurrentSubsetType = 0;
1124 int fCurrentSampleSize = 0;
1125 int fCurrentAnimSKP = 0;
mtkleine714e752014-07-31 12:13:48 -07001126};
1127
msarettc149f0e2016-01-04 11:35:43 -08001128// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1129// This prints something every once in a while so that it knows we're still working.
1130static void start_keepalive() {
Mike Klein03141d22017-10-30 11:57:15 -04001131 static std::thread* intentionallyLeaked = new std::thread([]{
1132 for (;;) {
1133 static const int kSec = 1200;
1134 #if defined(SK_BUILD_FOR_WIN)
1135 Sleep(kSec * 1000);
1136 #else
1137 sleep(kSec);
1138 #endif
1139 SkDebugf("\nBenchmarks still running...\n");
msarettc149f0e2016-01-04 11:35:43 -08001140 }
Mike Klein03141d22017-10-30 11:57:15 -04001141 });
1142 (void)intentionallyLeaked;
msarettc149f0e2016-01-04 11:35:43 -08001143}
1144
Mike Kleinbe28ee22017-02-06 12:46:20 -05001145int main(int argc, char** argv) {
Mike Klein88544fb2019-03-20 10:50:33 -05001146 CommandLineFlags::Parse(argc, argv);
Brian Osman53136aa2017-07-20 15:43:35 -04001147
Brian Osmanbc8150f2017-07-24 11:38:01 -04001148 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -04001149
Mike Kleinadacaef2017-02-06 09:26:14 -05001150#if defined(SK_BUILD_FOR_IOS)
1151 cd_Documents();
1152#endif
jcgregorio3b27ade2014-11-13 08:06:40 -08001153 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001154 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001155 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001156
Chris Dalton040238b2017-12-18 14:22:34 -07001157 SetCtxOptionsFromCommonFlags(&grContextOpts);
krajcevski69a55602014-08-13 10:46:31 -07001158
bsalomon6eb03cc2014-08-07 14:28:50 -07001159 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001160 FLAGS_samples = 1;
1161 FLAGS_gpuFrameLag = 0;
1162 }
1163
bsalomon6eb03cc2014-08-07 14:28:50 -07001164 if (!FLAGS_writePath.isEmpty()) {
1165 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1166 if (!sk_mkdir(FLAGS_writePath[0])) {
1167 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001168 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001169 }
1170 }
1171
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001172 std::unique_ptr<SkWStream> logStream(new SkNullWStream);
mtklein60317d0f2014-07-14 11:30:37 -07001173 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001174#if defined(SK_RELEASE)
Chris Dalton2e1381e2019-10-23 14:35:19 -06001175 // SkJSONWriter uses a 32k in-memory cache, so it only flushes occasionally and is well
1176 // equipped for a stream that re-opens, appends, and closes the file on every write.
1177 logStream.reset(new NanoFILEAppendAndCloseStream(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001178#else
1179 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1180 return 1;
1181#endif
mtklein60317d0f2014-07-14 11:30:37 -07001182 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001183 NanoJSONResultsWriter log(logStream.get(), SkJSONWriter::Mode::kPretty);
1184 log.beginObject(); // root
mtklein1915b622014-08-20 11:45:00 -07001185
1186 if (1 == FLAGS_properties.count() % 2) {
1187 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1188 return 1;
1189 }
1190 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001191 log.appendString(FLAGS_properties[i-1], FLAGS_properties[i]);
mtklein1915b622014-08-20 11:45:00 -07001192 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001193
1194 if (1 == FLAGS_key.count() % 2) {
1195 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1196 return 1;
1197 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001198 if (FLAGS_key.count()) {
1199 log.beginObject("key");
1200 for (int i = 1; i < FLAGS_key.count(); i += 2) {
1201 log.appendString(FLAGS_key[i - 1], FLAGS_key[i]);
1202 }
1203 log.endObject(); // key
mtklein94e51562014-08-19 12:41:53 -07001204 }
mtklein60317d0f2014-07-14 11:30:37 -07001205
mtkleinf3723212014-06-25 14:08:00 -07001206 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001207 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001208
cdaltone1b89582015-06-25 19:17:08 -07001209 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001210
bsalomon6eb03cc2014-08-07 14:28:50 -07001211 if (kAutoTuneLoops != FLAGS_loops) {
1212 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001213 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001214 SkDebugf("! -> high variance, ? -> moderate variance\n");
1215 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001216 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001217 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001218 } else {
mtkleind75c4662015-04-30 07:11:22 -07001219 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001220 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001221 }
1222
svaisanenc47635e2016-01-28 06:05:43 -08001223 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001224 create_configs(&configs);
1225
msarettc149f0e2016-01-04 11:35:43 -08001226 if (FLAGS_keepAlive) {
1227 start_keepalive();
1228 }
1229
Mike Klein03fa5d42019-03-22 11:39:09 -05001230 SetAnalyticAAFromCommonFlags();
liyuqian38911a72016-10-04 11:23:22 -07001231
Mike Klein2a57e792020-01-14 12:47:52 -06001232 if (FLAGS_forceRasterPipeline) { gSkForceRasterPipelineBlitter = true; }
Mike Kleinea4459d2020-01-16 13:05:04 -06001233 if (FLAGS_skvm) { gUseSkVMBlitter = gSkVMJITViaDylib = true; }
Yuqian Li550148b2017-01-13 10:13:13 -05001234
mtkleine070c2b2014-10-14 08:40:43 -07001235 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001236 BenchmarkStream benchStream;
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001237 log.beginObject("results");
Jim Van Verth8a9a3712019-05-31 10:49:12 -04001238 AutoreleasePool pool;
mtklein92007582014-08-01 07:46:52 -07001239 while (Benchmark* b = benchStream.next()) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001240 std::unique_ptr<Benchmark> bench(b);
Mike Klein88544fb2019-03-20 10:50:33 -05001241 if (CommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001242 continue;
1243 }
1244
svaisanenc47635e2016-01-28 06:05:43 -08001245 if (!configs.empty()) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001246 log.beginBench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001247 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001248 }
egdaniel3bf92062015-06-26 08:12:46 -07001249 for (int i = 0; i < configs.count(); ++i) {
1250 Target* target = is_enabled(b, configs[i]);
1251 if (!target) {
1252 continue;
1253 }
mtkleinf3723212014-06-25 14:08:00 -07001254
halcanary96fcdcc2015-08-27 07:41:13 -07001255 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001256 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001257 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001258
brianosman7a5ada82016-02-08 13:49:12 -08001259 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001260 SkDebugf("Running %s\t%s\n"
1261 , bench->getUniqueName()
1262 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001263 if (FLAGS_dryRun) {
1264 continue;
1265 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001266 }
1267
Mike Kleinee254232019-03-28 08:32:14 -05001268 if (FLAGS_purgeBetweenBenches) {
1269 SkGraphics::PurgeAllCaches();
1270 }
1271
Brian Osman5ad87aa2017-09-08 16:05:23 -04001272 TRACE_EVENT2("skia", "Benchmark", "name", TRACE_STR_COPY(bench->getUniqueName()),
1273 "config", TRACE_STR_COPY(config));
Brian Osmanc3cdef52017-08-31 14:09:22 -04001274
egdaniel3bf92062015-06-26 08:12:46 -07001275 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001276 bench->perCanvasPreDraw(canvas);
1277
cdaltone1b89582015-06-25 19:17:08 -07001278 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001279 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001280 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1281 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001282
Leon Scroggins IIIb30d1132017-10-02 09:48:15 -04001283 if (kFailedLoops == loops) {
1284 // Can't be timed. A warning note has already been printed.
1285 cleanup_run(target);
1286 continue;
1287 }
1288
Yuqian Li3528eb32017-09-21 13:23:49 -04001289 if (runs == 0 && FLAGS_ms < 1000) {
1290 // Run the first bench for 1000ms to warm up the nanobench if FLAGS_ms < 1000.
1291 // Otherwise, the first few benches' measurements will be inaccurate.
1292 auto stop = now_ms() + 1000;
1293 do {
1294 time(loops, bench.get(), target);
1295 } while (now_ms() < stop);
1296 }
1297
mtkleinbbba1682015-10-28 11:36:30 -07001298 if (FLAGS_ms) {
1299 samples.reset();
1300 auto stop = now_ms() + FLAGS_ms;
1301 do {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001302 samples.push_back(time(loops, bench.get(), target) / loops);
mtkleinbbba1682015-10-28 11:36:30 -07001303 } while (now_ms() < stop);
1304 } else {
cdaltone1b89582015-06-25 19:17:08 -07001305 samples.reset(FLAGS_samples);
1306 for (int s = 0; s < FLAGS_samples; s++) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001307 samples[s] = time(loops, bench.get(), target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001308 }
cdaltone1b89582015-06-25 19:17:08 -07001309 }
mtkleinf3723212014-06-25 14:08:00 -07001310
Mike Kleinfec9b902019-05-31 10:03:07 -05001311 // Scale each result to the benchmark's own units, time/unit.
1312 for (double& sample : samples) {
1313 sample *= (1.0 / bench->getUnits());
1314 }
1315
joshualitte45c81c2015-12-02 09:05:37 -08001316 SkTArray<SkString> keys;
1317 SkTArray<double> values;
1318 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1319 if (gpuStatsDump) {
1320 // TODO cache stats
1321 bench->getGpuStats(canvas, &keys, &values);
1322 }
joshualitte45c81c2015-12-02 09:05:37 -08001323
robertphillips5b693772014-11-21 06:19:36 -08001324 bench->perCanvasPostDraw(canvas);
1325
egdaniel3bf92062015-06-26 08:12:46 -07001326 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001327 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001328 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001329 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001330 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001331 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001332 }
1333
Mike Klein512a2442018-09-27 10:44:56 -04001334 // Building stats.plot often shows up in profiles,
1335 // so skip building it when we're not going to print it anyway.
1336 const bool want_plot = !FLAGS_quiet;
1337
1338 Stats stats(samples, want_plot);
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001339 log.beginObject(config);
1340
1341 log.beginObject("options");
1342 log.appendString("name", bench->getName());
1343 benchStream.fillCurrentOptions(log);
1344 target->fillOptions(log);
1345 log.endObject(); // options
1346
1347 // Metrics
1348 log.appendMetric("min_ms", stats.min);
1349 log.beginArray("samples");
1350 for (double sample : samples) {
1351 log.appendDoubleDigits(sample, 16);
1352 }
1353 log.endArray(); // samples
1354 benchStream.fillCurrentMetrics(log);
joshualitte45c81c2015-12-02 09:05:37 -08001355 if (gpuStatsDump) {
1356 // dump to json, only SKPBench currently returns valid keys / values
1357 SkASSERT(keys.count() == values.count());
1358 for (int i = 0; i < keys.count(); i++) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001359 log.appendMetric(keys[i].c_str(), values[i]);
joshualitte45c81c2015-12-02 09:05:37 -08001360 }
1361 }
joshualitte45c81c2015-12-02 09:05:37 -08001362
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001363 log.endObject(); // config
1364
mtkleine070c2b2014-10-14 08:40:43 -07001365 if (runs++ % FLAGS_flushEvery == 0) {
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001366 log.flush();
mtkleine070c2b2014-10-14 08:40:43 -07001367 }
mtklein60317d0f2014-07-14 11:30:37 -07001368
bsalomon6eb03cc2014-08-07 14:28:50 -07001369 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001370 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001371 config = ""; // Only print the config if we run the same bench on more than one.
1372 }
mtkleind75c4662015-04-30 07:11:22 -07001373 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1374 , sk_tools::getCurrResidentSetSizeMB()
1375 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001376 , bench->getUniqueName()
1377 , config);
mtkleinf3723212014-06-25 14:08:00 -07001378 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001379 const char* mark = " ";
Hal Canarye99e8da2019-01-14 16:34:50 -05001380 const double stddev_percent =
1381 sk_ieee_double_divide(100 * sqrt(stats.var), stats.mean);
mtklein66cfcff2015-12-04 06:35:30 -08001382 if (stddev_percent > 5) mark = "?";
1383 if (stddev_percent > 10) mark = "!";
1384
1385 SkDebugf("%10.2f %s\t%s\t%s\n",
1386 stats.median*1e3, mark, bench->getUniqueName(), config);
Mike Reedc665e5b2017-07-04 22:56:06 -04001387 } else if (FLAGS_csv) {
Hal Canarye99e8da2019-01-14 16:34:50 -05001388 const double stddev_percent =
1389 sk_ieee_double_divide(100 * sqrt(stats.var), stats.mean);
Mike Reedc665e5b2017-07-04 22:56:06 -04001390 SkDebugf("%g,%g,%g,%g,%g,%s,%s\n"
1391 , stats.min
1392 , stats.median
1393 , stats.mean
1394 , stats.max
1395 , stddev_percent
1396 , config
1397 , bench->getUniqueName()
1398 );
1399 } else {
1400 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 -05001401 const double stddev_percent =
1402 sk_ieee_double_divide(100 * sqrt(stats.var), stats.mean);
Mike Reedc665e5b2017-07-04 22:56:06 -04001403 SkDebugf(format
mtkleind75c4662015-04-30 07:11:22 -07001404 , sk_tools::getCurrResidentSetSizeMB()
1405 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001406 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001407 , HUMANIZE(stats.min)
1408 , HUMANIZE(stats.median)
1409 , HUMANIZE(stats.mean)
1410 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001411 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001412 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001413 , config
mtklein96289052014-09-10 12:05:59 -07001414 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001415 );
1416 }
joshualitte45c81c2015-12-02 09:05:37 -08001417
joshualitte45c81c2015-12-02 09:05:37 -08001418 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -04001419 target->dumpStats();
bsalomon06cddec2014-10-24 10:40:50 -07001420 }
joshualitte45c81c2015-12-02 09:05:37 -08001421
cdalton2c56ba52015-06-26 13:32:53 -07001422 if (FLAGS_verbose) {
1423 SkDebugf("Samples: ");
1424 for (int i = 0; i < samples.count(); i++) {
1425 SkDebugf("%s ", HUMANIZE(samples[i]));
1426 }
1427 SkDebugf("%s\n", bench->getUniqueName());
1428 }
egdaniel3bf92062015-06-26 08:12:46 -07001429 cleanup_run(target);
Jim Van Verth8a9a3712019-05-31 10:49:12 -04001430 pool.drain();
mtkleinf3723212014-06-25 14:08:00 -07001431 }
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001432 if (!configs.empty()) {
1433 log.endBench();
1434 }
mtkleinf3723212014-06-25 14:08:00 -07001435 }
1436
Herb Derby5a523fe2017-01-26 16:48:28 -05001437 SkGraphics::PurgeAllCaches();
1438
Brian Osman8c0a1ca2019-01-28 14:24:29 -05001439 log.beginBench("memory_usage", 0, 0);
1440 log.beginObject("meta"); // config
1441 log.appendS32("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1442 log.endObject(); // config
1443 log.endBench();
1444
1445 log.endObject(); // results
1446 log.endObject(); // root
1447 log.flush();
mtkleine1091452014-12-04 10:47:02 -08001448
mtkleinf3723212014-06-25 14:08:00 -07001449 return 0;
1450}