blob: afe7b71bdbbbd230af87dc637bced58c14af2db7 [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
tomhudsond968a6f2015-03-26 11:28:06 -070010#include "nanobench.h"
11
msarett84451022016-02-11 06:45:51 -080012#include "AndroidCodecBench.h"
mtkleinf3723212014-06-25 14:08:00 -070013#include "Benchmark.h"
msarett7f691442015-09-22 11:56:16 -070014#include "BitmapRegionDecoderBench.h"
scroggo60869a42015-04-01 12:09:17 -070015#include "CodecBench.h"
msarett7f691442015-09-22 11:56:16 -070016#include "CodecBenchPriv.h"
mtkleinf3723212014-06-25 14:08:00 -070017#include "CrashHandler.h"
mtkleine714e752014-07-31 12:13:48 -070018#include "GMBench.h"
mtkleinafb43792014-08-19 15:55:55 -070019#include "ProcStats.h"
mtkleinfd731ce2014-09-10 12:19:30 -070020#include "RecordingBench.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040021#include "ResultsWriter.h"
joshualitt261c3ad2015-04-27 09:16:57 -070022#include "SKPAnimationBench.h"
mtklein92007582014-08-01 07:46:52 -070023#include "SKPBench.h"
msarett84451022016-02-11 06:45:51 -080024#include "SkAndroidCodec.h"
Hal Canary95e3c052017-01-11 12:44:43 -050025#include "SkAutoMalloc.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000026#include "SkBBoxHierarchy.h"
Hal Canary95e3c052017-01-11 12:44:43 -050027#include "SkBitmapRegionDecoder.h"
mtkleinf3723212014-06-25 14:08:00 -070028#include "SkCanvas.h"
scroggo60869a42015-04-01 12:09:17 -070029#include "SkCodec.h"
Mike Klein0e4041f2018-06-19 16:00:40 -040030#include "SkColorSpacePriv.h"
caryclark17f0b6d2014-07-22 10:15:34 -070031#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080032#include "SkCommonFlagsConfig.h"
Chris Dalton040238b2017-12-18 14:22:34 -070033#include "SkCommonFlagsGpu.h"
msarett95f192d2015-02-13 09:05:41 -080034#include "SkData.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040035#include "SkDebugfTracer.h"
Brian Osman53136aa2017-07-20 15:43:35 -040036#include "SkEventTracingPriv.h"
mtkleinf3723212014-06-25 14:08:00 -070037#include "SkGraphics.h"
halcanary4dbbd042016-06-07 17:21:10 -070038#include "SkLeanWindows.h"
mtklein20840502014-08-21 15:51:22 -070039#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050040#include "SkOSPath.h"
mtklein20840502014-08-21 15:51:22 -070041#include "SkPictureRecorder.h"
Hal Canary95e3c052017-01-11 12:44:43 -050042#include "SkScan.h"
mtkleinf3723212014-06-25 14:08:00 -070043#include "SkString.h"
44#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080045#include "SkTaskGroup.h"
Brian Osmanc3cdef52017-08-31 14:09:22 -040046#include "SkTraceEvent.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040047#include "Stats.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040048#include "ios_utils.h"
mtkleinf3723212014-06-25 14:08:00 -070049
Hal Canary0f666812018-03-22 15:21:12 -040050#ifdef SK_XML
51#include "SkSVGDOM.h"
52#endif // SK_XML
53
bungeman60e0fee2015-08-26 05:15:46 -070054#include <stdlib.h>
Mike Klein03141d22017-10-30 11:57:15 -040055#include <thread>
bungeman60e0fee2015-08-26 05:15:46 -070056
Mike Reedc928fe22017-06-05 10:20:31 -040057extern bool gSkForceRasterPipelineBlitter;
58
Mike Klein8f11d4d2018-01-24 12:42:55 -050059#ifndef SK_BUILD_FOR_WIN
scroggo38ce0a72016-01-07 07:28:47 -080060 #include <unistd.h>
Brian Salomon032aaae2018-03-23 16:10:36 -040061
scroggo38ce0a72016-01-07 07:28:47 -080062#endif
63
Brian Osmanc7ad40f2018-05-31 14:27:17 -040064#include "GrCaps.h"
65#include "GrContextFactory.h"
66#include "GrContextPriv.h"
67#include "SkGr.h"
68#include "gl/GrGLDefines.h"
69#include "gl/GrGLGpu.h"
70#include "gl/GrGLUtil.h"
Brian Salomon032aaae2018-03-23 16:10:36 -040071
Brian Osmanc7ad40f2018-05-31 14:27:17 -040072using sk_gpu_test::ContextInfo;
73using sk_gpu_test::GrContextFactory;
74using sk_gpu_test::TestContext;
Brian Salomon032aaae2018-03-23 16:10:36 -040075
Brian Osmanc7ad40f2018-05-31 14:27:17 -040076GrContextOptions grContextOpts;
bsalomon682c2692015-05-22 14:01:46 -070077
reed53249782014-10-10 09:09:52 -070078static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070079
Mike Kleind8ee67c2017-01-19 12:14:50 -050080#if !defined(__has_feature)
81 #define __has_feature(x) 0
82#endif
83
mtkleinb5110422014-08-07 15:20:02 -070084static const int kDefaultLoops =
Mike Klein7a002c32018-03-30 16:22:13 +000085#if defined(SK_DEBUG) || __has_feature(address_sanitizer)
bsalomon6eb03cc2014-08-07 14:28:50 -070086 1;
mtkleina189ccd2014-07-14 12:28:47 -070087#else
bsalomon6eb03cc2014-08-07 14:28:50 -070088 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070089#endif
90
bsalomon6eb03cc2014-08-07 14:28:50 -070091static SkString loops_help_txt() {
92 SkString help;
93 help.printf("Number of times to run each bench. Set this to %d to auto-"
94 "tune for each bench. Timings are only reported when auto-tuning.",
95 kAutoTuneLoops);
96 return help;
97}
98
cdaltone1b89582015-06-25 19:17:08 -070099static SkString to_string(int n) {
100 SkString str;
101 str.appendS32(n);
102 return str;
103}
104
Ben Wagner32fa5102017-08-10 21:25:55 -0400105DECLARE_bool(undefok);
106
bsalomon6eb03cc2014-08-07 14:28:50 -0700107DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
108
mtkleinf3723212014-06-25 14:08:00 -0700109DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -0700110DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -0700111DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
112DEFINE_double(overheadGoal, 0.0001,
113 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -0700114DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -0700115DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -0700116
mtklein60317d0f2014-07-14 11:30:37 -0700117DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -0700118DEFINE_int32(maxCalibrationAttempts, 3,
119 "Try up to this many times to guess loops for a bench, or skip the bench.");
120DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700121DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
122DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700123DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
124 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700125DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
mtklein8c1a4f82016-08-08 06:56:22 -0700126DEFINE_bool(lite, false, "Use SkLiteRecorder in recording benchmarks?");
robertphillips5b693772014-11-21 06:19:36 -0800127DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700128DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700129DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
bsalomonb12ea412015-02-02 21:19:50 -0800130DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800131DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
msarettc149f0e2016-01-04 11:35:43 -0800132DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
Mike Reedc665e5b2017-07-04 22:56:06 -0400133DEFINE_bool(csv, false, "Print status in CSV format");
mtklein65dfd2f2016-02-03 10:40:54 -0800134DEFINE_string(sourceType, "",
135 "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
136DEFINE_string(benchType, "",
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400137 "Apply usual --match rules to bench type: micro, recording, piping, playback, skcodec, etc.");
mtklein65dfd2f2016-02-03 10:40:54 -0800138
Mike Reedc928fe22017-06-05 10:20:31 -0400139DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
140
mtkleinbbba1682015-10-28 11:36:30 -0700141static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
142
mtkleinf3723212014-06-25 14:08:00 -0700143static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700144 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800145 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700146}
mtklein55b0ffc2014-07-17 08:38:23 -0700147#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700148
tomhudsond968a6f2015-03-26 11:28:06 -0700149bool Target::init(SkImageInfo info, Benchmark* bench) {
150 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700151 this->surface = SkSurface::MakeRaster(info);
152 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700153 return false;
154 }
155 }
156 return true;
157}
158bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700159 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700160 if (!canvas) {
161 return false;
162 }
Mike Reed12e946b2017-04-17 10:53:29 -0400163 bmp->allocPixels(canvas->imageInfo());
164 if (!canvas->readPixels(*bmp, 0, 0)) {
tomhudsond968a6f2015-03-26 11:28:06 -0700165 SkDebugf("Can't read canvas pixels.\n");
166 return false;
167 }
168 return true;
169}
170
tomhudsond968a6f2015-03-26 11:28:06 -0700171struct GPUTarget : public Target {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400172 explicit GPUTarget(const Config& c) : Target(c) {}
173 ContextInfo contextInfo;
174 std::unique_ptr<GrContextFactory> factory;
tomhudsond968a6f2015-03-26 11:28:06 -0700175
176 void setup() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400177 this->contextInfo.testContext()->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700178 // Make sure we're done with whatever came before.
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400179 this->contextInfo.testContext()->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700180 }
181 void endTiming() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400182 if (this->contextInfo.testContext()) {
183 this->contextInfo.testContext()->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700184 }
185 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400186 void fence() override { this->contextInfo.testContext()->finish(); }
mtkleind75c4662015-04-30 07:11:22 -0700187
cdaltond416a5b2015-06-23 13:23:44 -0700188 bool needsFrameTiming(int* maxFrameLag) const override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400189 if (!this->contextInfo.testContext()->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700190 // Frame lag is unknown.
191 *maxFrameLag = FLAGS_gpuFrameLag;
192 }
193 return true;
194 }
tomhudsond968a6f2015-03-26 11:28:06 -0700195 bool init(SkImageInfo info, Benchmark* bench) override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400196 GrContextOptions options = grContextOpts;
197 bench->modifyGrContextOptions(&options);
198 this->factory.reset(new GrContextFactory(options));
bsalomonafcd7cd2015-08-31 12:39:41 -0700199 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
200 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700201 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400202 this->surface = SkSurface::MakeRenderTarget(
203 this->factory->get(this->config.ctxType, this->config.ctxOverrides),
204 SkBudgeted::kNo, info, this->config.samples, &props);
205 this->contextInfo =
206 this->factory->getContextInfo(this->config.ctxType, this->config.ctxOverrides);
tomhudsond968a6f2015-03-26 11:28:06 -0700207 if (!this->surface.get()) {
208 return false;
209 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400210 if (!this->contextInfo.testContext()->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700211 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800212 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700213 }
tomhudsond968a6f2015-03-26 11:28:06 -0700214 return true;
215 }
216 void fillOptions(ResultsWriter* log) override {
217 const GrGLubyte* version;
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400218 if (this->contextInfo.backend() == kOpenGL_GrBackend) {
Brian Salomon032aaae2018-03-23 16:10:36 -0400219 const GrGLInterface* gl =
220 static_cast<GrGLGpu*>(this->contextInfo.grContext()->contextPriv().getGpu())
221 ->glInterface();
bsalomonc8699322016-05-11 11:55:36 -0700222 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
223 log->configOption("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700224
bsalomonc8699322016-05-11 11:55:36 -0700225 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
226 log->configOption("GL_RENDERER", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700227
bsalomonc8699322016-05-11 11:55:36 -0700228 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VENDOR));
229 log->configOption("GL_VENDOR", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700230
bsalomonc8699322016-05-11 11:55:36 -0700231 GR_GL_CALL_RET(gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
232 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
233 }
tomhudsond968a6f2015-03-26 11:28:06 -0700234 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400235
236 void dumpStats() override {
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500237 this->contextInfo.grContext()->contextPriv().printCacheStats();
238 this->contextInfo.grContext()->contextPriv().printGpuStats();
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400239 }
tomhudsond968a6f2015-03-26 11:28:06 -0700240};
mtkleind75c4662015-04-30 07:11:22 -0700241
tomhudson75a0ebb2015-03-27 12:11:44 -0700242static double time(int loops, Benchmark* bench, Target* target) {
243 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700244 if (canvas) {
245 canvas->clear(SK_ColorWHITE);
246 }
joshualitt8a6697a2015-09-30 12:11:07 -0700247 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700248 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700249 canvas = target->beginTiming(canvas);
250 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700251 if (canvas) {
252 canvas->flush();
253 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700254 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700255 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700256 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700257 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700258}
259
mtkleinf3723212014-06-25 14:08:00 -0700260static double estimate_timer_overhead() {
261 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700262 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700263 double start = now_ms();
264 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700265 }
266 return overhead / FLAGS_overheadLoops;
267}
268
reed53249782014-10-10 09:09:52 -0700269static int detect_forever_loops(int loops) {
270 // look for a magic run-forever value
271 if (loops < 0) {
272 loops = SK_MaxS32;
273 }
274 return loops;
275}
276
mtklein55b0ffc2014-07-17 08:38:23 -0700277static int clamp_loops(int loops) {
278 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800279 SkDebugf("ERROR: clamping loops from %d to 1. "
280 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700281 return 1;
282 }
283 if (loops > FLAGS_maxLoops) {
284 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
285 return FLAGS_maxLoops;
286 }
287 return loops;
288}
289
tomhudsond968a6f2015-03-26 11:28:06 -0700290static bool write_canvas_png(Target* target, const SkString& filename) {
291
bsalomon6eb03cc2014-08-07 14:28:50 -0700292 if (filename.isEmpty()) {
293 return false;
294 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700295 if (target->getCanvas() &&
296 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700297 return false;
298 }
tomhudsond968a6f2015-03-26 11:28:06 -0700299
bsalomon6eb03cc2014-08-07 14:28:50 -0700300 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700301
302 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700303 return false;
304 }
tomhudsond968a6f2015-03-26 11:28:06 -0700305
bsalomon6eb03cc2014-08-07 14:28:50 -0700306 SkString dir = SkOSPath::Dirname(filename.c_str());
307 if (!sk_mkdir(dir.c_str())) {
308 SkDebugf("Can't make dir %s.\n", dir.c_str());
309 return false;
310 }
311 SkFILEWStream stream(filename.c_str());
312 if (!stream.isValid()) {
313 SkDebugf("Can't write %s.\n", filename.c_str());
314 return false;
315 }
Hal Canarydb683012016-11-23 08:55:18 -0700316 if (!SkEncodeImage(&stream, bmp, SkEncodedImageFormat::kPNG, 100)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700317 SkDebugf("Can't encode a PNG.\n");
318 return false;
319 }
320 return true;
321}
322
323static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700324static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700325 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700326 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700327 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700328 int loops = bench->calculateLoops(FLAGS_loops);
329 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700330 while (bench_plus_overhead < overhead) {
331 if (round++ == FLAGS_maxCalibrationAttempts) {
332 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700333 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700334 return kFailedLoops;
335 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700336 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700337 }
mtklein2069e222014-08-04 13:57:39 -0700338 }
mtkleinf3723212014-06-25 14:08:00 -0700339
mtkleinbb6a0282014-07-01 08:43:42 -0700340 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700341 // We'll pick N to make timer overhead negligible:
342 //
mtkleinbb6a0282014-07-01 08:43:42 -0700343 // overhead
344 // ------------------------- < FLAGS_overheadGoal
345 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700346 //
Hal Canary55325b72017-01-03 10:36:17 -0500347 // where bench_plus_overhead ~=~ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700348 //
349 // Doing some math, we get:
350 //
mtkleinbb6a0282014-07-01 08:43:42 -0700351 // (overhead / FLAGS_overheadGoal) - overhead
352 // ------------------------------------------ < N
353 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700354 //
355 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700356 if (kAutoTuneLoops == loops) {
357 const double numer = overhead / FLAGS_overheadGoal - overhead;
358 const double denom = bench_plus_overhead - overhead;
359 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700360 loops = clamp_loops(loops);
361 } else {
362 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700363 }
mtkleinbb6a0282014-07-01 08:43:42 -0700364
mtkleinbb6a0282014-07-01 08:43:42 -0700365 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700366}
367
cdaltone1b89582015-06-25 19:17:08 -0700368static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700369 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700370 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700371 if (kAutoTuneLoops == loops) {
372 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700373 double elapsed = 0;
374 do {
mtklein527930f2014-11-06 08:04:34 -0800375 if (1<<30 == loops) {
376 // We're about to wrap. Something's wrong with the bench.
377 loops = 0;
378 break;
379 }
mtkleina189ccd2014-07-14 12:28:47 -0700380 loops *= 2;
381 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700382 // _this_ round, not still timing last round.
383 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700384 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700385 }
386 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700387
mtkleina189ccd2014-07-14 12:28:47 -0700388 // We've overshot at least a little. Scale back linearly.
389 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700390 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700391
tomhudsond968a6f2015-03-26 11:28:06 -0700392 // Make sure we're not still timing our calibration.
393 target->fence();
reed53249782014-10-10 09:09:52 -0700394 } else {
395 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700396 }
mtkleinbb6a0282014-07-01 08:43:42 -0700397 // Pretty much the same deal as the calibration: do some warmup to make
398 // sure we're timing steady-state pipelined frames.
Brian Osman01776982017-10-10 15:41:03 -0400399 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700400 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700401 }
mtkleinbb6a0282014-07-01 08:43:42 -0700402
mtkleinbb6a0282014-07-01 08:43:42 -0700403 return loops;
404}
mtkleinbb6a0282014-07-01 08:43:42 -0700405
Brian Salomon6405e712017-03-20 08:54:16 -0400406#define kBogusContextType GrContextFactory::kGL_ContextType
csmartdaltone812d492017-02-21 12:36:05 -0700407#define kBogusContextOverrides GrContextFactory::ContextOverrides::kNone
bsalomonc2553372014-07-22 13:09:05 -0700408
svaisanenc47635e2016-01-28 06:05:43 -0800409static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
svaisanenc47635e2016-01-28 06:05:43 -0800410 if (const auto* gpuConfig = config->asConfigGpu()) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400411 if (!FLAGS_gpu) {
412 SkDebugf("Skipping config '%s' as requested.\n", config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800413 return;
Ben Wagner32fa5102017-08-10 21:25:55 -0400414 }
svaisanenc47635e2016-01-28 06:05:43 -0800415
svaisanenc47635e2016-01-28 06:05:43 -0800416 const auto ctxType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700417 const auto ctxOverrides = gpuConfig->getContextOverrides();
svaisanenc47635e2016-01-28 06:05:43 -0800418 const auto sampleCount = gpuConfig->getSamples();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400419 const auto colorType = gpuConfig->getColorType();
420 auto colorSpace = gpuConfig->getColorSpace();
Brian Salomonf865b052018-03-09 09:01:53 -0500421 if (gpuConfig->getSurfType() != SkCommandLineConfigGpu::SurfType::kDefault) {
422 SkDebugf("This tool only supports the default surface type.");
423 return;
424 }
svaisanenc47635e2016-01-28 06:05:43 -0800425
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400426 GrContextFactory factory(grContextOpts);
427 if (const GrContext* ctx = factory.get(ctxType, ctxOverrides)) {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400428 GrPixelConfig grPixConfig = SkColorType2GrPixelConfig(colorType);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500429 int supportedSampleCount =
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400430 ctx->contextPriv().caps()->getRenderTargetSampleCount(sampleCount, grPixConfig);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400431 if (sampleCount != supportedSampleCount) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400432 SkDebugf("Configuration '%s' sample count %d is not a supported sample count.\n",
433 config->getTag().c_str(), sampleCount);
svaisanenc47635e2016-01-28 06:05:43 -0800434 return;
435 }
436 } else {
Ben Wagner32fa5102017-08-10 21:25:55 -0400437 SkDebugf("No context was available matching config '%s'.\n",
438 config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800439 return;
440 }
441
442 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800443 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800444 Benchmark::kGPU_Backend,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400445 colorType,
svaisanenc47635e2016-01-28 06:05:43 -0800446 kPremul_SkAlphaType,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400447 sk_ref_sp(colorSpace),
svaisanenc47635e2016-01-28 06:05:43 -0800448 sampleCount,
449 ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700450 ctxOverrides,
kkinnunend4e1c352016-03-01 23:41:26 -0800451 gpuConfig->getUseDIText()
452 };
svaisanenc47635e2016-01-28 06:05:43 -0800453
454 configs->push_back(target);
455 return;
456 }
svaisanenc47635e2016-01-28 06:05:43 -0800457
brianosmanb109b8c2016-06-16 13:03:24 -0700458 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
459 if (config->getTag().equals(#name)) { \
Ben Wagner32fa5102017-08-10 21:25:55 -0400460 if (!FLAGS_cpu) { \
461 SkDebugf("Skipping config '%s' as requested.\n", \
462 config->getTag().c_str()); \
463 return; \
464 } \
brianosmanb109b8c2016-06-16 13:03:24 -0700465 Config config = { \
466 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
csmartdaltone812d492017-02-21 12:36:05 -0700467 0, kBogusContextType, kBogusContextOverrides, false \
brianosmanb109b8c2016-06-16 13:03:24 -0700468 }; \
469 configs->push_back(config); \
470 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700471 }
mtkleine714e752014-07-31 12:13:48 -0700472
Ben Wagner32fa5102017-08-10 21:25:55 -0400473 CPU_CONFIG(nonrendering, kNonRendering_Backend,
474 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800475
Mike Klein0e4041f2018-06-19 16:00:40 -0400476 CPU_CONFIG(a8, kRaster_Backend, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr)
477 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType, nullptr)
478 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
479
480 // 'narrow' has a gamut narrower than sRGB, and different transfer function.
481 SkMatrix44 narrow_gamut(SkMatrix44::kUninitialized_Constructor);
482 narrow_gamut.set3x3RowMajorf(gNarrow_toXYZD50);
483
484 auto narrow = SkColorSpace::MakeRGB(k2Dot2Curve_SkGammaNamed, narrow_gamut),
485 srgb = SkColorSpace::MakeSRGB(),
486 srgbLinear = SkColorSpace::MakeSRGBLinear();
487
488 CPU_CONFIG( f16, kRaster_Backend, kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbLinear)
489 CPU_CONFIG( srgb, kRaster_Backend, kRGBA_8888_SkColorType, kPremul_SkAlphaType, srgb )
490 CPU_CONFIG( esrgb, kRaster_Backend, kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgb )
491 CPU_CONFIG( narrow, kRaster_Backend, kRGBA_8888_SkColorType, kPremul_SkAlphaType, narrow )
492 CPU_CONFIG(enarrow, kRaster_Backend, kRGBA_F16_SkColorType, kPremul_SkAlphaType, narrow )
mtkleinbb6a0282014-07-01 08:43:42 -0700493
svaisanenc47635e2016-01-28 06:05:43 -0800494 #undef CPU_CONFIG
Ben Wagner32fa5102017-08-10 21:25:55 -0400495
496 SkDebugf("Unknown config '%s'.\n", config->getTag().c_str());
mtkleinf3723212014-06-25 14:08:00 -0700497}
498
svaisanenc47635e2016-01-28 06:05:43 -0800499// Append all configs that are enabled and supported.
500void create_configs(SkTArray<Config>* configs) {
501 SkCommandLineConfigArray array;
502 ParseConfigs(FLAGS_config, &array);
503 for (int i = 0; i < array.count(); ++i) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400504 create_config(array[i].get(), configs);
svaisanenc47635e2016-01-28 06:05:43 -0800505 }
Ben Wagner32fa5102017-08-10 21:25:55 -0400506
507 // If no just default configs were requested, then we're okay.
508 if (array.count() == 0 || FLAGS_config.count() == 0 ||
509 // If we've been told to ignore undefined flags, we're okay.
510 FLAGS_undefok ||
511 // Otherwise, make sure that all specified configs have been created.
512 array.count() == configs->count()) {
513 return;
514 }
515 SkDebugf("Invalid --config. Use --undefok to bypass this warning.\n");
516 exit(1);
svaisanenc47635e2016-01-28 06:05:43 -0800517}
518
brianosman9f1f6e22016-09-15 08:33:02 -0700519// disable warning : switch statement contains default but no 'case' labels
520#if defined _WIN32
521#pragma warning ( push )
522#pragma warning ( disable : 4065 )
523#endif
524
halcanary96fcdcc2015-08-27 07:41:13 -0700525// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700526static Target* is_enabled(Benchmark* bench, const Config& config) {
527 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700528 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700529 }
530
reede5ea5002014-09-03 11:54:58 -0700531 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700532 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700533
halcanary96fcdcc2015-08-27 07:41:13 -0700534 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700535
tomhudsond968a6f2015-03-26 11:28:06 -0700536 switch (config.backend) {
tomhudsond968a6f2015-03-26 11:28:06 -0700537 case Benchmark::kGPU_Backend:
538 target = new GPUTarget(config);
539 break;
tomhudsond968a6f2015-03-26 11:28:06 -0700540 default:
541 target = new Target(config);
542 break;
543 }
bsalomonc2553372014-07-22 13:09:05 -0700544
tomhudsond968a6f2015-03-26 11:28:06 -0700545 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700546 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700547 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700548 }
549 return target;
550}
551
brianosman9f1f6e22016-09-15 08:33:02 -0700552#if defined _WIN32
553#pragma warning ( pop )
554#endif
555
reed42943c82016-09-12 12:01:44 -0700556static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700557 uint32_t minOutputSize, int* width, int* height) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400558 std::unique_ptr<SkBitmapRegionDecoder> brd(
msarettd1227a72016-05-18 06:23:57 -0700559 SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy));
msarett7f691442015-09-22 11:56:16 -0700560 if (nullptr == brd.get()) {
561 // This is indicates that subset decoding is not supported for a particular image format.
562 return false;
563 }
564
msarett7f691442015-09-22 11:56:16 -0700565 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
566 (uint32_t) brd->height()) {
567 // This indicates that the image is not large enough to decode a
568 // minOutputSize x minOutputSize subset at the given sampleSize.
569 return false;
570 }
571
572 // Set the image width and height. The calling code will use this to choose subsets to decode.
573 *width = brd->width();
574 *height = brd->height();
575 return true;
576}
577
egdaniel3bf92062015-06-26 08:12:46 -0700578static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700579 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700580}
581
fmalita6519c212016-09-14 08:05:17 -0700582static void collect_files(const SkCommandLineFlags::StringArray& paths, const char* ext,
583 SkTArray<SkString>* list) {
584 for (int i = 0; i < paths.count(); ++i) {
585 if (SkStrEndsWith(paths[i], ext)) {
586 list->push_back(SkString(paths[i]));
587 } else {
588 SkOSFile::Iter it(paths[i], ext);
589 SkString path;
590 while (it.next(&path)) {
591 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
592 }
593 }
594 }
595}
596
mtkleine714e752014-07-31 12:13:48 -0700597class BenchmarkStream {
598public:
mtklein92007582014-08-01 07:46:52 -0700599 BenchmarkStream() : fBenches(BenchRegistry::Head())
600 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700601 , fCurrentRecording(0)
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400602 , fCurrentPiping(0)
Mike Reede45ff462017-12-06 10:47:03 -0500603 , fCurrentDeserialPicture(0)
mtklein92007582014-08-01 07:46:52 -0700604 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800605 , fCurrentSKP(0)
fmalita6519c212016-09-14 08:05:17 -0700606 , fCurrentSVG(0)
msarett95f192d2015-02-13 09:05:41 -0800607 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700608 , fCurrentCodec(0)
msarett84451022016-02-11 06:45:51 -0800609 , fCurrentAndroidCodec(0)
msarett7f691442015-09-22 11:56:16 -0700610 , fCurrentBRDImage(0)
msarett95f192d2015-02-13 09:05:41 -0800611 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800612 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700613 , fCurrentSubsetType(0)
msarett84451022016-02-11 06:45:51 -0800614 , fCurrentSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700615 , fCurrentAnimSKP(0) {
fmalita6519c212016-09-14 08:05:17 -0700616 collect_files(FLAGS_skps, ".skp", &fSKPs);
617 collect_files(FLAGS_svgs, ".svg", &fSVGs);
mtkleine714e752014-07-31 12:13:48 -0700618
mtklein92007582014-08-01 07:46:52 -0700619 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
620 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
621 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
622 exit(1);
623 }
624
625 for (int i = 0; i < FLAGS_scales.count(); i++) {
626 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
627 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
628 exit(1);
629 }
630 }
robertphillips5b693772014-11-21 06:19:36 -0800631
cdalton63a82852015-06-29 14:06:10 -0700632 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
633 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700634 exit(1);
635 }
636
robertphillips5b693772014-11-21 06:19:36 -0800637 if (FLAGS_mpd) {
638 fUseMPDs.push_back() = true;
639 }
mtkleinc751ecb2015-06-15 08:56:38 -0700640 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700641
msarett95f192d2015-02-13 09:05:41 -0800642 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700643 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800644 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800645 }
mtklein95553d92015-03-12 08:24:21 -0700646
msarett95f192d2015-02-13 09:05:41 -0800647 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700648 fColorTypes.push_back(kN32_SkColorType);
649 if (!FLAGS_simpleCodec) {
650 fColorTypes.push_back(kRGB_565_SkColorType);
651 fColorTypes.push_back(kAlpha_8_SkColorType);
msarett67cb6662016-06-21 08:49:26 -0700652 fColorTypes.push_back(kGray_8_SkColorType);
653 }
mtklein92007582014-08-01 07:46:52 -0700654 }
655
reedca2622b2016-03-18 07:25:55 -0700656 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700657 // Not strictly necessary, as it will be checked again later,
658 // but helps to avoid a lot of pointless work if we're going to skip it.
cdalton91e457d2016-02-17 11:10:16 -0800659 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700660 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700661 }
662
bungemanf93d7112016-09-16 06:24:20 -0700663 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
664 if (!stream) {
mtkleinfd731ce2014-09-10 12:19:30 -0700665 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700666 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700667 }
668
reedca2622b2016-03-18 07:25:55 -0700669 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700670 }
671
fmalita6519c212016-09-14 08:05:17 -0700672 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
673 SkFILEStream stream(path);
674 if (!stream.isValid()) {
675 SkDebugf("Could not read %s.\n", path);
676 return nullptr;
677 }
678
Hal Canary0f666812018-03-22 15:21:12 -0400679#ifdef SK_XML
fmalitae1baa7c2016-09-14 12:04:30 -0700680 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
fmalita6519c212016-09-14 08:05:17 -0700681 if (!svgDom) {
682 SkDebugf("Could not parse %s.\n", path);
683 return nullptr;
684 }
685
fmalitae1baa7c2016-09-14 12:04:30 -0700686 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
687 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
688 if (svgDom->containerSize().isEmpty()) {
689 svgDom->setContainerSize(kDefaultContainerSize);
690 }
691
fmalita6519c212016-09-14 08:05:17 -0700692 SkPictureRecorder recorder;
fmalitae1baa7c2016-09-14 12:04:30 -0700693 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
694 svgDom->containerSize().height()));
fmalita6519c212016-09-14 08:05:17 -0700695 return recorder.finishRecordingAsPicture();
Hal Canary0f666812018-03-22 15:21:12 -0400696#else
697 return nullptr;
698#endif // SK_XML
fmalita6519c212016-09-14 08:05:17 -0700699 }
700
mtklein92007582014-08-01 07:46:52 -0700701 Benchmark* next() {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400702 std::unique_ptr<Benchmark> bench;
mtklein65dfd2f2016-02-03 10:40:54 -0800703 do {
704 bench.reset(this->rawNext());
705 if (!bench) {
706 return nullptr;
707 }
708 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
709 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700710 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800711 }
712
713 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700714 if (fBenches) {
Hal Canary972eba32018-07-30 17:07:07 -0400715 Benchmark* bench = fBenches->get()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700716 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700717 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700718 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700719 return bench;
720 }
mtklein92007582014-08-01 07:46:52 -0700721
mtkleine714e752014-07-31 12:13:48 -0700722 while (fGMs) {
Hal Canary972eba32018-07-30 17:07:07 -0400723 std::unique_ptr<skiagm::GM> gm(fGMs->get()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700724 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800725 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700726 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700727 fBenchType = "micro";
mtklein18300a32016-03-16 13:53:35 -0700728 return new GMBench(gm.release());
mtkleine714e752014-07-31 12:13:48 -0700729 }
730 }
mtklein92007582014-08-01 07:46:52 -0700731
mtkleinfd731ce2014-09-10 12:19:30 -0700732 // First add all .skps as RecordingBenches.
733 while (fCurrentRecording < fSKPs.count()) {
734 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700735 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
736 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700737 continue;
738 }
739 SkString name = SkOSPath::Basename(path.c_str());
740 fSourceType = "skp";
741 fBenchType = "recording";
Mike Klein88d90712018-01-27 17:30:04 +0000742 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
743 fSKPOps = pic->approximateOpCount();
mtklein8c1a4f82016-08-08 06:56:22 -0700744 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh, FLAGS_lite);
mtkleinfd731ce2014-09-10 12:19:30 -0700745 }
746
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400747 // Add all .skps as PipeBenches.
748 while (fCurrentPiping < fSKPs.count()) {
749 const SkString& path = fSKPs[fCurrentPiping++];
750 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
751 if (!pic) {
752 continue;
753 }
754 SkString name = SkOSPath::Basename(path.c_str());
755 fSourceType = "skp";
756 fBenchType = "piping";
Mike Klein88d90712018-01-27 17:30:04 +0000757 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
758 fSKPOps = pic->approximateOpCount();
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400759 return new PipingBench(name.c_str(), pic.get());
760 }
761
Mike Reede45ff462017-12-06 10:47:03 -0500762 // Add all .skps as DeserializePictureBenchs.
763 while (fCurrentDeserialPicture < fSKPs.count()) {
764 const SkString& path = fSKPs[fCurrentDeserialPicture++];
765 sk_sp<SkData> data = SkData::MakeFromFileName(path.c_str());
766 if (!data) {
767 continue;
768 }
769 SkString name = SkOSPath::Basename(path.c_str());
770 fSourceType = "skp";
771 fBenchType = "deserial";
Mike Reed7557bbb2017-12-24 19:50:57 -0500772 fSKPBytes = static_cast<double>(data->size());
Mike Klein88d90712018-01-27 17:30:04 +0000773 fSKPOps = 0;
Mike Reede45ff462017-12-06 10:47:03 -0500774 return new DeserializePictureBench(name.c_str(), std::move(data));
775 }
776
mtkleinfd731ce2014-09-10 12:19:30 -0700777 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700778 while (fCurrentScale < fScales.count()) {
779 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800780 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700781 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
782 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800783 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700784 continue;
785 }
robertphillips5b693772014-11-21 06:19:36 -0800786
787 while (fCurrentUseMPD < fUseMPDs.count()) {
788 if (FLAGS_bbh) {
789 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
790 SkRTreeFactory factory;
791 SkPictureRecorder recorder;
robertphillips5b693772014-11-21 06:19:36 -0800792 pic->playback(recorder.beginRecording(pic->cullRect().width(),
793 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800794 &factory,
robertphillipsdda54452016-07-13 13:27:16 -0700795 0));
reedca2622b2016-03-18 07:25:55 -0700796 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800797 }
798 SkString name = SkOSPath::Basename(path.c_str());
799 fSourceType = "skp";
800 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700801 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
802 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700803 }
robertphillips5b693772014-11-21 06:19:36 -0800804 fCurrentUseMPD = 0;
805 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700806 }
fmalita6519c212016-09-14 08:05:17 -0700807
808 while (fCurrentSVG++ < fSVGs.count()) {
809 const char* path = fSVGs[fCurrentSVG - 1].c_str();
810 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
811 fSourceType = "svg";
812 fBenchType = "playback";
813 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
814 fScales[fCurrentScale], false, FLAGS_loopSKP);
815 }
816 }
817
mtklein92007582014-08-01 07:46:52 -0700818 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700819 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700820 fCurrentScale++;
821 }
822
joshualitt261c3ad2015-04-27 09:16:57 -0700823 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700824 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700825 while (fCurrentAnimSKP < fSKPs.count()) {
826 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700827 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
828 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700829 fCurrentAnimSKP++;
830 continue;
831 }
832
833 fCurrentAnimSKP++;
834 SkString name = SkOSPath::Basename(path.c_str());
Hal Canary2db83612016-11-04 13:02:54 -0400835 sk_sp<SKPAnimationBench::Animation> animation(
cdalton63a82852015-06-29 14:06:10 -0700836 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
Hal Canary2db83612016-11-04 13:02:54 -0400837 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation.get(),
halcanary385fe4d2015-08-26 13:07:48 -0700838 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700839 }
840 }
841
scroggo60869a42015-04-01 12:09:17 -0700842 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700843 fSourceType = "image";
844 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700845 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800846 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
847 continue;
848 }
bungeman38d909e2016-08-02 14:40:46 -0700849 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400850 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700851 if (!codec) {
852 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700853 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700854 continue;
855 }
scroggo21027992015-04-02 13:22:38 -0700856
scroggo60869a42015-04-01 12:09:17 -0700857 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700858 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700859
msarettc7796b92016-01-07 14:20:20 -0800860 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700861 if (FLAGS_simpleCodec) {
862 if (kUnpremul_SkAlphaType == alphaType) {
863 alphaType = kPremul_SkAlphaType;
864 }
865
866 fCurrentColorType++;
867 } else {
868 switch (alphaType) {
869 case kOpaque_SkAlphaType:
870 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800871 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700872 break;
873 case kUnpremul_SkAlphaType:
874 case kPremul_SkAlphaType:
875 if (0 == fCurrentAlphaType) {
876 // Test unpremul first.
877 alphaType = kUnpremul_SkAlphaType;
878 fCurrentAlphaType++;
879 } else {
880 // Test premul.
881 alphaType = kPremul_SkAlphaType;
882 fCurrentAlphaType = 0;
883 fCurrentColorType++;
884 }
885 break;
886 default:
887 SkASSERT(false);
888 fCurrentColorType++;
889 break;
890 }
scroggo21027992015-04-02 13:22:38 -0700891 }
892
msarettc7796b92016-01-07 14:20:20 -0800893 // Make sure we can decode to this color type and alpha type.
894 SkImageInfo info =
895 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700896 const size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400897 SkAutoMalloc storage(info.computeByteSize(rowBytes));
scroggo21027992015-04-02 13:22:38 -0700898
scroggoeb602a52015-07-09 08:16:03 -0700899 const SkCodec::Result result = codec->getPixels(
Leon Scroggins571b30f2017-07-11 17:35:31 +0000900 info, storage.get(), rowBytes);
scroggo60869a42015-04-01 12:09:17 -0700901 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700902 case SkCodec::kSuccess:
903 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700904 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700905 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700906 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700907 // This is okay. Not all conversions are valid.
908 break;
scroggo60869a42015-04-01 12:09:17 -0700909 default:
910 // This represents some sort of failure.
911 SkASSERT(false);
912 break;
913 }
914 }
915 fCurrentColorType = 0;
916 }
917
msarett84451022016-02-11 06:45:51 -0800918 // Run AndroidCodecBenches
919 const int sampleSizes[] = { 2, 4, 8 };
920 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
921 fSourceType = "image";
922 fBenchType = "skandroidcodec";
923
924 const SkString& path = fImages[fCurrentAndroidCodec];
925 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
926 continue;
927 }
bungeman38d909e2016-08-02 14:40:46 -0700928 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400929 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800930 if (!codec) {
931 // Nothing to time.
932 SkDebugf("Cannot find codec for %s\n", path.c_str());
933 continue;
934 }
935
936 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
937 int sampleSize = sampleSizes[fCurrentSampleSize];
938 fCurrentSampleSize++;
939 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
940 // Avoid benchmarking scaled decodes of already small images.
941 break;
942 }
943
bungeman38d909e2016-08-02 14:40:46 -0700944 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
945 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -0800946 }
947 fCurrentSampleSize = 0;
948 }
949
msarett7f691442015-09-22 11:56:16 -0700950 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700951 // We intend to create benchmarks that model the use cases in
952 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
953 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
954 // the image. For that reason, we will benchmark decodes in five representative locations
955 // in the image. Additionally, this use case utilizes power of two scaling, so we will
956 // test on power of two sample sizes. The output tile is always 512x512, so, when a
957 // sampleSize is used, the size of the subset that is decoded is always
958 // (sampleSize*512)x(sampleSize*512).
959 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700960 // All use cases we are aware of only scale by powers of two.
961 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
962 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800963 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700964 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800965 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700966 fSourceType = "image";
967 fBenchType = "BRD";
968
mtklein6f0ff912016-01-11 09:04:21 -0800969 const SkString& path = fImages[fCurrentBRDImage];
970 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
971 continue;
972 }
scroggo860e8a62015-10-15 07:51:28 -0700973
msarettd1227a72016-05-18 06:23:57 -0700974 while (fCurrentColorType < fColorTypes.count()) {
975 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
976 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700977
bungeman38d909e2016-08-02 14:40:46 -0700978 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -0700979 const SkColorType colorType = fColorTypes[fCurrentColorType];
980 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
981 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -0700982
msarettd1227a72016-05-18 06:23:57 -0700983 int width = 0;
984 int height = 0;
reed42943c82016-09-12 12:01:44 -0700985 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -0700986 &width, &height)) {
987 break;
msarett7f691442015-09-22 11:56:16 -0700988 }
msarettd1227a72016-05-18 06:23:57 -0700989
990 SkString basename = SkOSPath::Basename(path.c_str());
991 SkIRect subset;
992 const uint32_t subsetSize = sampleSize * minOutputSize;
993 switch (currentSubsetType) {
994 case kTopLeft_SubsetType:
995 basename.append("_TopLeft");
996 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
997 break;
998 case kTopRight_SubsetType:
999 basename.append("_TopRight");
1000 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
1001 subsetSize);
1002 break;
1003 case kMiddle_SubsetType:
1004 basename.append("_Middle");
1005 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
1006 (height - subsetSize) / 2, subsetSize, subsetSize);
1007 break;
1008 case kBottomLeft_SubsetType:
1009 basename.append("_BottomLeft");
1010 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
1011 subsetSize);
1012 break;
1013 case kBottomRight_SubsetType:
1014 basename.append("_BottomRight");
1015 subset = SkIRect::MakeXYWH(width - subsetSize,
1016 height - subsetSize, subsetSize, subsetSize);
1017 break;
1018 default:
1019 SkASSERT(false);
1020 }
1021
1022 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
1023 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -07001024 }
msarettd1227a72016-05-18 06:23:57 -07001025 fCurrentSubsetType = 0;
1026 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -07001027 }
msarettd1227a72016-05-18 06:23:57 -07001028 fCurrentSampleSize = 0;
1029 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -07001030 }
msarettd1227a72016-05-18 06:23:57 -07001031 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -07001032 }
1033
halcanary96fcdcc2015-08-27 07:41:13 -07001034 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001035 }
mtklein92007582014-08-01 07:46:52 -07001036
1037 void fillCurrentOptions(ResultsWriter* log) const {
1038 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001039 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001040 if (0 == strcmp(fSourceType, "skp")) {
1041 log->configOption("clip",
1042 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1043 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001044 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -07001045 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001046 if (fCurrentUseMPD > 0) {
1047 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1048 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1049 }
mtklein92007582014-08-01 07:46:52 -07001050 }
mtklein051e56d2014-12-04 08:46:51 -08001051 if (0 == strcmp(fBenchType, "recording")) {
Mike Reed7557bbb2017-12-24 19:50:57 -05001052 log->metric("bytes", fSKPBytes);
Mike Klein88d90712018-01-27 17:30:04 +00001053 log->metric("ops", fSKPOps);
mtklein051e56d2014-12-04 08:46:51 -08001054 }
mtklein92007582014-08-01 07:46:52 -07001055 }
1056
mtkleine714e752014-07-31 12:13:48 -07001057private:
msarettb23e6aa2015-06-09 13:56:10 -07001058 enum SubsetType {
1059 kTopLeft_SubsetType = 0,
1060 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001061 kMiddle_SubsetType = 2,
1062 kBottomLeft_SubsetType = 3,
1063 kBottomRight_SubsetType = 4,
1064 kTranslate_SubsetType = 5,
1065 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001066 kLast_SubsetType = kZoom_SubsetType,
1067 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001068 };
1069
mtkleine714e752014-07-31 12:13:48 -07001070 const BenchRegistry* fBenches;
1071 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001072 SkIRect fClip;
1073 SkTArray<SkScalar> fScales;
1074 SkTArray<SkString> fSKPs;
fmalita6519c212016-09-14 08:05:17 -07001075 SkTArray<SkString> fSVGs;
robertphillips5b693772014-11-21 06:19:36 -08001076 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001077 SkTArray<SkString> fImages;
msarett74deb982015-10-20 16:45:56 -07001078 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001079 SkScalar fZoomMax;
1080 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001081
Mike Klein88d90712018-01-27 17:30:04 +00001082 double fSKPBytes, fSKPOps;
mtklein051e56d2014-12-04 08:46:51 -08001083
mtkleinfd731ce2014-09-10 12:19:30 -07001084 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1085 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1086 int fCurrentRecording;
Mike Reed9cdd2ab2016-10-21 10:43:36 -04001087 int fCurrentPiping;
Mike Reede45ff462017-12-06 10:47:03 -05001088 int fCurrentDeserialPicture;
mtklein92007582014-08-01 07:46:52 -07001089 int fCurrentScale;
1090 int fCurrentSKP;
fmalita6519c212016-09-14 08:05:17 -07001091 int fCurrentSVG;
robertphillips5b693772014-11-21 06:19:36 -08001092 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001093 int fCurrentCodec;
msarett84451022016-02-11 06:45:51 -08001094 int fCurrentAndroidCodec;
msarett7f691442015-09-22 11:56:16 -07001095 int fCurrentBRDImage;
msarett95f192d2015-02-13 09:05:41 -08001096 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -08001097 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -07001098 int fCurrentSubsetType;
msarett84451022016-02-11 06:45:51 -08001099 int fCurrentSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001100 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001101};
1102
msarettc149f0e2016-01-04 11:35:43 -08001103// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1104// This prints something every once in a while so that it knows we're still working.
1105static void start_keepalive() {
Mike Klein03141d22017-10-30 11:57:15 -04001106 static std::thread* intentionallyLeaked = new std::thread([]{
1107 for (;;) {
1108 static const int kSec = 1200;
1109 #if defined(SK_BUILD_FOR_WIN)
1110 Sleep(kSec * 1000);
1111 #else
1112 sleep(kSec);
1113 #endif
1114 SkDebugf("\nBenchmarks still running...\n");
msarettc149f0e2016-01-04 11:35:43 -08001115 }
Mike Klein03141d22017-10-30 11:57:15 -04001116 });
1117 (void)intentionallyLeaked;
msarettc149f0e2016-01-04 11:35:43 -08001118}
1119
Mike Kleinbe28ee22017-02-06 12:46:20 -05001120int main(int argc, char** argv) {
1121 SkCommandLineFlags::Parse(argc, argv);
Brian Osman53136aa2017-07-20 15:43:35 -04001122
Brian Osmanbc8150f2017-07-24 11:38:01 -04001123 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -04001124
Mike Kleinadacaef2017-02-06 09:26:14 -05001125#if defined(SK_BUILD_FOR_IOS)
1126 cd_Documents();
1127#endif
jcgregorio3b27ade2014-11-13 08:06:40 -08001128 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001129 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001130 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001131
Chris Dalton040238b2017-12-18 14:22:34 -07001132 SetCtxOptionsFromCommonFlags(&grContextOpts);
krajcevski69a55602014-08-13 10:46:31 -07001133
bsalomon06cddec2014-10-24 10:40:50 -07001134 if (FLAGS_veryVerbose) {
1135 FLAGS_verbose = true;
1136 }
1137
bsalomon6eb03cc2014-08-07 14:28:50 -07001138 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001139 FLAGS_samples = 1;
1140 FLAGS_gpuFrameLag = 0;
1141 }
1142
bsalomon6eb03cc2014-08-07 14:28:50 -07001143 if (!FLAGS_writePath.isEmpty()) {
1144 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1145 if (!sk_mkdir(FLAGS_writePath[0])) {
1146 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001147 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001148 }
1149 }
1150
Ben Wagner145dbcd2016-11-03 14:40:50 -04001151 std::unique_ptr<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001152 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001153#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001154 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001155#else
1156 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1157 return 1;
1158#endif
mtklein60317d0f2014-07-14 11:30:37 -07001159 }
mtklein1915b622014-08-20 11:45:00 -07001160
1161 if (1 == FLAGS_properties.count() % 2) {
1162 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1163 return 1;
1164 }
1165 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1166 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1167 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001168
1169 if (1 == FLAGS_key.count() % 2) {
1170 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1171 return 1;
1172 }
1173 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001174 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001175 }
mtklein60317d0f2014-07-14 11:30:37 -07001176
mtkleinf3723212014-06-25 14:08:00 -07001177 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001178 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001179
cdaltone1b89582015-06-25 19:17:08 -07001180 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001181
bsalomon6eb03cc2014-08-07 14:28:50 -07001182 if (kAutoTuneLoops != FLAGS_loops) {
1183 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001184 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001185 SkDebugf("! -> high variance, ? -> moderate variance\n");
1186 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001187 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001188 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001189 } else {
mtkleind75c4662015-04-30 07:11:22 -07001190 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001191 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001192 }
1193
svaisanenc47635e2016-01-28 06:05:43 -08001194 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001195 create_configs(&configs);
1196
msarettc149f0e2016-01-04 11:35:43 -08001197 if (FLAGS_keepAlive) {
1198 start_keepalive();
1199 }
1200
Yuqian Liba62b4a2016-12-14 13:46:53 -05001201 gSkUseAnalyticAA = FLAGS_analyticAA;
Yuqian Lidf60e362017-07-25 11:26:31 -04001202 gSkUseDeltaAA = FLAGS_deltaAA;
liyuqian38911a72016-10-04 11:23:22 -07001203
Yuqian Lidf60e362017-07-25 11:26:31 -04001204 if (FLAGS_forceDeltaAA) {
1205 gSkForceDeltaAA = true;
1206 }
Yuqian Li550148b2017-01-13 10:13:13 -05001207 if (FLAGS_forceAnalyticAA) {
1208 gSkForceAnalyticAA = true;
1209 }
Mike Reedc928fe22017-06-05 10:20:31 -04001210 if (FLAGS_forceRasterPipeline) {
1211 gSkForceRasterPipelineBlitter = true;
1212 }
Yuqian Li550148b2017-01-13 10:13:13 -05001213
mtkleine070c2b2014-10-14 08:40:43 -07001214 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001215 BenchmarkStream benchStream;
1216 while (Benchmark* b = benchStream.next()) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001217 std::unique_ptr<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001218 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001219 continue;
1220 }
1221
svaisanenc47635e2016-01-28 06:05:43 -08001222 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001223 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001224 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001225 }
egdaniel3bf92062015-06-26 08:12:46 -07001226 for (int i = 0; i < configs.count(); ++i) {
1227 Target* target = is_enabled(b, configs[i]);
1228 if (!target) {
1229 continue;
1230 }
mtkleinf3723212014-06-25 14:08:00 -07001231
halcanary96fcdcc2015-08-27 07:41:13 -07001232 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001233 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001234 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001235
brianosman7a5ada82016-02-08 13:49:12 -08001236 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001237 SkDebugf("Running %s\t%s\n"
1238 , bench->getUniqueName()
1239 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001240 if (FLAGS_dryRun) {
1241 continue;
1242 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001243 }
1244
Brian Osman5ad87aa2017-09-08 16:05:23 -04001245 TRACE_EVENT2("skia", "Benchmark", "name", TRACE_STR_COPY(bench->getUniqueName()),
1246 "config", TRACE_STR_COPY(config));
Brian Osmanc3cdef52017-08-31 14:09:22 -04001247
egdaniel3bf92062015-06-26 08:12:46 -07001248 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001249 bench->perCanvasPreDraw(canvas);
1250
cdaltone1b89582015-06-25 19:17:08 -07001251 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001252 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001253 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1254 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001255
Leon Scroggins IIIb30d1132017-10-02 09:48:15 -04001256 if (kFailedLoops == loops) {
1257 // Can't be timed. A warning note has already been printed.
1258 cleanup_run(target);
1259 continue;
1260 }
1261
Yuqian Li3528eb32017-09-21 13:23:49 -04001262 if (runs == 0 && FLAGS_ms < 1000) {
1263 // Run the first bench for 1000ms to warm up the nanobench if FLAGS_ms < 1000.
1264 // Otherwise, the first few benches' measurements will be inaccurate.
1265 auto stop = now_ms() + 1000;
1266 do {
1267 time(loops, bench.get(), target);
1268 } while (now_ms() < stop);
1269 }
1270
mtkleinbbba1682015-10-28 11:36:30 -07001271 if (FLAGS_ms) {
1272 samples.reset();
1273 auto stop = now_ms() + FLAGS_ms;
1274 do {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001275 samples.push_back(time(loops, bench.get(), target) / loops);
mtkleinbbba1682015-10-28 11:36:30 -07001276 } while (now_ms() < stop);
1277 } else {
cdaltone1b89582015-06-25 19:17:08 -07001278 samples.reset(FLAGS_samples);
1279 for (int s = 0; s < FLAGS_samples; s++) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001280 samples[s] = time(loops, bench.get(), target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001281 }
cdaltone1b89582015-06-25 19:17:08 -07001282 }
mtkleinf3723212014-06-25 14:08:00 -07001283
joshualitte45c81c2015-12-02 09:05:37 -08001284 SkTArray<SkString> keys;
1285 SkTArray<double> values;
1286 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1287 if (gpuStatsDump) {
1288 // TODO cache stats
1289 bench->getGpuStats(canvas, &keys, &values);
1290 }
joshualitte45c81c2015-12-02 09:05:37 -08001291
robertphillips5b693772014-11-21 06:19:36 -08001292 bench->perCanvasPostDraw(canvas);
1293
egdaniel3bf92062015-06-26 08:12:46 -07001294 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001295 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001296 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001297 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001298 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001299 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001300 }
1301
cdaltone1b89582015-06-25 19:17:08 -07001302 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001303 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001304 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001305 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001306 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001307 log->metric("min_ms", stats.min);
George Burgess IV75b57182016-12-06 10:53:52 -08001308 log->metrics("samples", samples);
joshualitte45c81c2015-12-02 09:05:37 -08001309 if (gpuStatsDump) {
1310 // dump to json, only SKPBench currently returns valid keys / values
1311 SkASSERT(keys.count() == values.count());
1312 for (int i = 0; i < keys.count(); i++) {
1313 log->metric(keys[i].c_str(), values[i]);
1314 }
1315 }
joshualitte45c81c2015-12-02 09:05:37 -08001316
mtkleine070c2b2014-10-14 08:40:43 -07001317 if (runs++ % FLAGS_flushEvery == 0) {
1318 log->flush();
1319 }
mtklein60317d0f2014-07-14 11:30:37 -07001320
bsalomon6eb03cc2014-08-07 14:28:50 -07001321 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001322 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001323 config = ""; // Only print the config if we run the same bench on more than one.
1324 }
mtkleind75c4662015-04-30 07:11:22 -07001325 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1326 , sk_tools::getCurrResidentSetSizeMB()
1327 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001328 , bench->getUniqueName()
1329 , config);
mtkleinf3723212014-06-25 14:08:00 -07001330 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001331 const char* mark = " ";
1332 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1333 if (stddev_percent > 5) mark = "?";
1334 if (stddev_percent > 10) mark = "!";
1335
1336 SkDebugf("%10.2f %s\t%s\t%s\n",
1337 stats.median*1e3, mark, bench->getUniqueName(), config);
Mike Reedc665e5b2017-07-04 22:56:06 -04001338 } else if (FLAGS_csv) {
mtkleinf3723212014-06-25 14:08:00 -07001339 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
Mike Reedc665e5b2017-07-04 22:56:06 -04001340 SkDebugf("%g,%g,%g,%g,%g,%s,%s\n"
1341 , stats.min
1342 , stats.median
1343 , stats.mean
1344 , stats.max
1345 , stddev_percent
1346 , config
1347 , bench->getUniqueName()
1348 );
1349 } else {
1350 const char* format = "%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n";
1351 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1352 SkDebugf(format
mtkleind75c4662015-04-30 07:11:22 -07001353 , sk_tools::getCurrResidentSetSizeMB()
1354 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001355 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001356 , HUMANIZE(stats.min)
1357 , HUMANIZE(stats.median)
1358 , HUMANIZE(stats.mean)
1359 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001360 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001361 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001362 , config
mtklein96289052014-09-10 12:05:59 -07001363 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001364 );
1365 }
joshualitte45c81c2015-12-02 09:05:37 -08001366
joshualitte45c81c2015-12-02 09:05:37 -08001367 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -04001368 target->dumpStats();
bsalomon06cddec2014-10-24 10:40:50 -07001369 }
joshualitte45c81c2015-12-02 09:05:37 -08001370
cdalton2c56ba52015-06-26 13:32:53 -07001371 if (FLAGS_verbose) {
1372 SkDebugf("Samples: ");
1373 for (int i = 0; i < samples.count(); i++) {
1374 SkDebugf("%s ", HUMANIZE(samples[i]));
1375 }
1376 SkDebugf("%s\n", bench->getUniqueName());
1377 }
egdaniel3bf92062015-06-26 08:12:46 -07001378 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001379 }
mtkleinf3723212014-06-25 14:08:00 -07001380 }
1381
Herb Derby5a523fe2017-01-26 16:48:28 -05001382 SkGraphics::PurgeAllCaches();
1383
mtkleine1091452014-12-04 10:47:02 -08001384 log->bench("memory_usage", 0,0);
1385 log->config("meta");
1386 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1387
mtkleinf3723212014-06-25 14:08:00 -07001388 return 0;
1389}