blob: ea0de3398e81b8a2516277152f99e97340e5320d [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"
msarett2cee9022016-06-03 08:25:21 -070017#include "ColorCodecBench.h"
mtkleinf3723212014-06-25 14:08:00 -070018#include "CrashHandler.h"
mtkleine714e752014-07-31 12:13:48 -070019#include "GMBench.h"
mtkleinafb43792014-08-19 15:55:55 -070020#include "ProcStats.h"
mtkleinfd731ce2014-09-10 12:19:30 -070021#include "RecordingBench.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040022#include "ResultsWriter.h"
joshualitt261c3ad2015-04-27 09:16:57 -070023#include "SKPAnimationBench.h"
mtklein92007582014-08-01 07:46:52 -070024#include "SKPBench.h"
msarett84451022016-02-11 06:45:51 -080025#include "SkAndroidCodec.h"
Hal Canary95e3c052017-01-11 12:44:43 -050026#include "SkAutoMalloc.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000027#include "SkBBoxHierarchy.h"
Hal Canary95e3c052017-01-11 12:44:43 -050028#include "SkBitmapRegionDecoder.h"
mtkleinf3723212014-06-25 14:08:00 -070029#include "SkCanvas.h"
scroggo60869a42015-04-01 12:09:17 -070030#include "SkCodec.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 "SkSVGDOM.h"
43#include "SkScan.h"
mtkleinf3723212014-06-25 14:08:00 -070044#include "SkString.h"
45#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080046#include "SkTaskGroup.h"
Brian Osmanc3cdef52017-08-31 14:09:22 -040047#include "SkTraceEvent.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040048#include "Stats.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040049#include "ios_utils.h"
mtkleinf3723212014-06-25 14:08:00 -070050
bungeman60e0fee2015-08-26 05:15:46 -070051#include <stdlib.h>
Mike Klein03141d22017-10-30 11:57:15 -040052#include <thread>
bungeman60e0fee2015-08-26 05:15:46 -070053
Mike Reedc928fe22017-06-05 10:20:31 -040054extern bool gSkForceRasterPipelineBlitter;
55
Mike Klein8f11d4d2018-01-24 12:42:55 -050056#ifndef SK_BUILD_FOR_WIN
scroggo38ce0a72016-01-07 07:28:47 -080057 #include <unistd.h>
58#endif
59
mtkleinbb6a0282014-07-01 08:43:42 -070060#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070061 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070062 #include "GrCaps.h"
Robert Phillips0c4b7b12018-03-06 08:20:37 -050063 #include "GrContextPriv.h"
mtkleinbb6a0282014-07-01 08:43:42 -070064 #include "GrContextFactory.h"
bsalomon3724e572016-03-30 18:56:19 -070065 #include "gl/GrGLUtil.h"
Greg Daniel81e7bf82017-07-19 14:47:42 -040066 #include "SkGr.h"
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040067 using sk_gpu_test::ContextInfo;
bsalomon3724e572016-03-30 18:56:19 -070068 using sk_gpu_test::GrContextFactory;
bsalomonc8699322016-05-11 11:55:36 -070069 using sk_gpu_test::TestContext;
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040070 GrContextOptions grContextOpts;
mtkleinbb6a0282014-07-01 08:43:42 -070071#endif
72
bsalomon682c2692015-05-22 14:01:46 -070073 struct GrContextOptions;
74
reed53249782014-10-10 09:09:52 -070075static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070076
Mike Kleind8ee67c2017-01-19 12:14:50 -050077#if !defined(__has_feature)
78 #define __has_feature(x) 0
79#endif
80
mtkleinb5110422014-08-07 15:20:02 -070081static const int kDefaultLoops =
Mike Kleind8ee67c2017-01-19 12:14:50 -050082#if defined(SK_DEBUG) || __has_feature(address_sanitizer)
bsalomon6eb03cc2014-08-07 14:28:50 -070083 1;
mtkleina189ccd2014-07-14 12:28:47 -070084#else
bsalomon6eb03cc2014-08-07 14:28:50 -070085 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070086#endif
87
bsalomon6eb03cc2014-08-07 14:28:50 -070088static SkString loops_help_txt() {
89 SkString help;
90 help.printf("Number of times to run each bench. Set this to %d to auto-"
91 "tune for each bench. Timings are only reported when auto-tuning.",
92 kAutoTuneLoops);
93 return help;
94}
95
cdaltone1b89582015-06-25 19:17:08 -070096static SkString to_string(int n) {
97 SkString str;
98 str.appendS32(n);
99 return str;
100}
101
Ben Wagner32fa5102017-08-10 21:25:55 -0400102DECLARE_bool(undefok);
103
bsalomon6eb03cc2014-08-07 14:28:50 -0700104DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
105
mtkleinf3723212014-06-25 14:08:00 -0700106DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -0700107DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -0700108DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
109DEFINE_double(overheadGoal, 0.0001,
110 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -0700111DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -0700112DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -0700113
mtklein60317d0f2014-07-14 11:30:37 -0700114DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -0700115DEFINE_int32(maxCalibrationAttempts, 3,
116 "Try up to this many times to guess loops for a bench, or skip the bench.");
117DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700118DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
119DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700120DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
121 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700122DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
mtklein8c1a4f82016-08-08 06:56:22 -0700123DEFINE_bool(lite, false, "Use SkLiteRecorder in recording benchmarks?");
robertphillips5b693772014-11-21 06:19:36 -0800124DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700125DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700126DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
bsalomonb12ea412015-02-02 21:19:50 -0800127DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800128DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
msarettc149f0e2016-01-04 11:35:43 -0800129DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
Mike Reedc665e5b2017-07-04 22:56:06 -0400130DEFINE_bool(csv, false, "Print status in CSV format");
mtklein65dfd2f2016-02-03 10:40:54 -0800131DEFINE_string(sourceType, "",
132 "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
133DEFINE_string(benchType, "",
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400134 "Apply usual --match rules to bench type: micro, recording, piping, playback, skcodec, etc.");
mtklein65dfd2f2016-02-03 10:40:54 -0800135
Mike Reedc928fe22017-06-05 10:20:31 -0400136DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
137
mtkleinbbba1682015-10-28 11:36:30 -0700138static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
139
mtkleinf3723212014-06-25 14:08:00 -0700140static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700141 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800142 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700143}
mtklein55b0ffc2014-07-17 08:38:23 -0700144#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700145
tomhudsond968a6f2015-03-26 11:28:06 -0700146bool Target::init(SkImageInfo info, Benchmark* bench) {
147 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700148 this->surface = SkSurface::MakeRaster(info);
149 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700150 return false;
151 }
152 }
153 return true;
154}
155bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700156 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700157 if (!canvas) {
158 return false;
159 }
Mike Reed12e946b2017-04-17 10:53:29 -0400160 bmp->allocPixels(canvas->imageInfo());
161 if (!canvas->readPixels(*bmp, 0, 0)) {
tomhudsond968a6f2015-03-26 11:28:06 -0700162 SkDebugf("Can't read canvas pixels.\n");
163 return false;
164 }
165 return true;
166}
167
168#if SK_SUPPORT_GPU
169struct GPUTarget : public Target {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400170 explicit GPUTarget(const Config& c) : Target(c) {}
171 ContextInfo contextInfo;
172 std::unique_ptr<GrContextFactory> factory;
tomhudsond968a6f2015-03-26 11:28:06 -0700173
174 void setup() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400175 this->contextInfo.testContext()->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700176 // Make sure we're done with whatever came before.
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400177 this->contextInfo.testContext()->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700178 }
179 void endTiming() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400180 if (this->contextInfo.testContext()) {
181 this->contextInfo.testContext()->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700182 }
183 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400184 void fence() override { this->contextInfo.testContext()->finish(); }
mtkleind75c4662015-04-30 07:11:22 -0700185
cdaltond416a5b2015-06-23 13:23:44 -0700186 bool needsFrameTiming(int* maxFrameLag) const override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400187 if (!this->contextInfo.testContext()->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700188 // Frame lag is unknown.
189 *maxFrameLag = FLAGS_gpuFrameLag;
190 }
191 return true;
192 }
tomhudsond968a6f2015-03-26 11:28:06 -0700193 bool init(SkImageInfo info, Benchmark* bench) override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400194 GrContextOptions options = grContextOpts;
195 bench->modifyGrContextOptions(&options);
196 this->factory.reset(new GrContextFactory(options));
bsalomonafcd7cd2015-08-31 12:39:41 -0700197 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
198 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700199 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400200 this->surface = SkSurface::MakeRenderTarget(
201 this->factory->get(this->config.ctxType, this->config.ctxOverrides),
202 SkBudgeted::kNo, info, this->config.samples, &props);
203 this->contextInfo =
204 this->factory->getContextInfo(this->config.ctxType, this->config.ctxOverrides);
tomhudsond968a6f2015-03-26 11:28:06 -0700205 if (!this->surface.get()) {
206 return false;
207 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400208 if (!this->contextInfo.testContext()->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700209 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800210 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700211 }
tomhudsond968a6f2015-03-26 11:28:06 -0700212 return true;
213 }
214 void fillOptions(ResultsWriter* log) override {
215 const GrGLubyte* version;
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400216 if (this->contextInfo.backend() == kOpenGL_GrBackend) {
217 const GrGLInterface* gl = reinterpret_cast<const GrGLInterface*>(
218 this->contextInfo.testContext()->backendContext());
bsalomonc8699322016-05-11 11:55:36 -0700219 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
220 log->configOption("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700221
bsalomonc8699322016-05-11 11:55:36 -0700222 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
223 log->configOption("GL_RENDERER", (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_VENDOR));
226 log->configOption("GL_VENDOR", (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_SHADING_LANGUAGE_VERSION));
229 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
230 }
tomhudsond968a6f2015-03-26 11:28:06 -0700231 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400232
233 void dumpStats() override {
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500234 this->contextInfo.grContext()->contextPriv().printCacheStats();
235 this->contextInfo.grContext()->contextPriv().printGpuStats();
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400236 }
tomhudsond968a6f2015-03-26 11:28:06 -0700237};
mtkleind75c4662015-04-30 07:11:22 -0700238
tomhudsond968a6f2015-03-26 11:28:06 -0700239#endif
240
tomhudson75a0ebb2015-03-27 12:11:44 -0700241static double time(int loops, Benchmark* bench, Target* target) {
242 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700243 if (canvas) {
244 canvas->clear(SK_ColorWHITE);
245 }
joshualitt8a6697a2015-09-30 12:11:07 -0700246 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700247 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700248 canvas = target->beginTiming(canvas);
249 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700250 if (canvas) {
251 canvas->flush();
252 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700253 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700254 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700255 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700256 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700257}
258
mtkleinf3723212014-06-25 14:08:00 -0700259static double estimate_timer_overhead() {
260 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700261 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700262 double start = now_ms();
263 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700264 }
265 return overhead / FLAGS_overheadLoops;
266}
267
reed53249782014-10-10 09:09:52 -0700268static int detect_forever_loops(int loops) {
269 // look for a magic run-forever value
270 if (loops < 0) {
271 loops = SK_MaxS32;
272 }
273 return loops;
274}
275
mtklein55b0ffc2014-07-17 08:38:23 -0700276static int clamp_loops(int loops) {
277 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800278 SkDebugf("ERROR: clamping loops from %d to 1. "
279 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700280 return 1;
281 }
282 if (loops > FLAGS_maxLoops) {
283 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
284 return FLAGS_maxLoops;
285 }
286 return loops;
287}
288
tomhudsond968a6f2015-03-26 11:28:06 -0700289static bool write_canvas_png(Target* target, const SkString& filename) {
290
bsalomon6eb03cc2014-08-07 14:28:50 -0700291 if (filename.isEmpty()) {
292 return false;
293 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700294 if (target->getCanvas() &&
295 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700296 return false;
297 }
tomhudsond968a6f2015-03-26 11:28:06 -0700298
bsalomon6eb03cc2014-08-07 14:28:50 -0700299 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700300
301 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700302 return false;
303 }
tomhudsond968a6f2015-03-26 11:28:06 -0700304
bsalomon6eb03cc2014-08-07 14:28:50 -0700305 SkString dir = SkOSPath::Dirname(filename.c_str());
306 if (!sk_mkdir(dir.c_str())) {
307 SkDebugf("Can't make dir %s.\n", dir.c_str());
308 return false;
309 }
310 SkFILEWStream stream(filename.c_str());
311 if (!stream.isValid()) {
312 SkDebugf("Can't write %s.\n", filename.c_str());
313 return false;
314 }
Hal Canarydb683012016-11-23 08:55:18 -0700315 if (!SkEncodeImage(&stream, bmp, SkEncodedImageFormat::kPNG, 100)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700316 SkDebugf("Can't encode a PNG.\n");
317 return false;
318 }
319 return true;
320}
321
322static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700323static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700324 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700325 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700326 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700327 int loops = bench->calculateLoops(FLAGS_loops);
328 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700329 while (bench_plus_overhead < overhead) {
330 if (round++ == FLAGS_maxCalibrationAttempts) {
331 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700332 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700333 return kFailedLoops;
334 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700335 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700336 }
mtklein2069e222014-08-04 13:57:39 -0700337 }
mtkleinf3723212014-06-25 14:08:00 -0700338
mtkleinbb6a0282014-07-01 08:43:42 -0700339 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700340 // We'll pick N to make timer overhead negligible:
341 //
mtkleinbb6a0282014-07-01 08:43:42 -0700342 // overhead
343 // ------------------------- < FLAGS_overheadGoal
344 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700345 //
Hal Canary55325b72017-01-03 10:36:17 -0500346 // where bench_plus_overhead ~=~ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700347 //
348 // Doing some math, we get:
349 //
mtkleinbb6a0282014-07-01 08:43:42 -0700350 // (overhead / FLAGS_overheadGoal) - overhead
351 // ------------------------------------------ < N
352 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700353 //
354 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700355 if (kAutoTuneLoops == loops) {
356 const double numer = overhead / FLAGS_overheadGoal - overhead;
357 const double denom = bench_plus_overhead - overhead;
358 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700359 loops = clamp_loops(loops);
360 } else {
361 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700362 }
mtkleinbb6a0282014-07-01 08:43:42 -0700363
mtkleinbb6a0282014-07-01 08:43:42 -0700364 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700365}
366
cdaltone1b89582015-06-25 19:17:08 -0700367static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700368 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700369 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700370 if (kAutoTuneLoops == loops) {
371 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700372 double elapsed = 0;
373 do {
mtklein527930f2014-11-06 08:04:34 -0800374 if (1<<30 == loops) {
375 // We're about to wrap. Something's wrong with the bench.
376 loops = 0;
377 break;
378 }
mtkleina189ccd2014-07-14 12:28:47 -0700379 loops *= 2;
380 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700381 // _this_ round, not still timing last round.
382 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700383 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700384 }
385 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700386
mtkleina189ccd2014-07-14 12:28:47 -0700387 // We've overshot at least a little. Scale back linearly.
388 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700389 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700390
tomhudsond968a6f2015-03-26 11:28:06 -0700391 // Make sure we're not still timing our calibration.
392 target->fence();
reed53249782014-10-10 09:09:52 -0700393 } else {
394 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700395 }
mtkleinbb6a0282014-07-01 08:43:42 -0700396 // Pretty much the same deal as the calibration: do some warmup to make
397 // sure we're timing steady-state pipelined frames.
Brian Osman01776982017-10-10 15:41:03 -0400398 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700399 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700400 }
mtkleinbb6a0282014-07-01 08:43:42 -0700401
mtkleinbb6a0282014-07-01 08:43:42 -0700402 return loops;
403}
mtkleinbb6a0282014-07-01 08:43:42 -0700404
bsalomonc2553372014-07-22 13:09:05 -0700405#if SK_SUPPORT_GPU
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#else
bsalomon85b4b532016-04-05 11:06:27 -0700409#define kBogusContextType 0
csmartdaltone812d492017-02-21 12:36:05 -0700410#define kBogusContextOverrides 0
mtkleine714e752014-07-31 12:13:48 -0700411#endif
bsalomonc2553372014-07-22 13:09:05 -0700412
svaisanenc47635e2016-01-28 06:05:43 -0800413static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
414
415#if SK_SUPPORT_GPU
416 if (const auto* gpuConfig = config->asConfigGpu()) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400417 if (!FLAGS_gpu) {
418 SkDebugf("Skipping config '%s' as requested.\n", config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800419 return;
Ben Wagner32fa5102017-08-10 21:25:55 -0400420 }
svaisanenc47635e2016-01-28 06:05:43 -0800421
svaisanenc47635e2016-01-28 06:05:43 -0800422 const auto ctxType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700423 const auto ctxOverrides = gpuConfig->getContextOverrides();
svaisanenc47635e2016-01-28 06:05:43 -0800424 const auto sampleCount = gpuConfig->getSamples();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400425 const auto colorType = gpuConfig->getColorType();
426 auto colorSpace = gpuConfig->getColorSpace();
Brian Salomonf865b052018-03-09 09:01:53 -0500427 if (gpuConfig->getSurfType() != SkCommandLineConfigGpu::SurfType::kDefault) {
428 SkDebugf("This tool only supports the default surface type.");
429 return;
430 }
svaisanenc47635e2016-01-28 06:05:43 -0800431
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400432 GrContextFactory factory(grContextOpts);
433 if (const GrContext* ctx = factory.get(ctxType, ctxOverrides)) {
Greg Daniel81e7bf82017-07-19 14:47:42 -0400434 GrPixelConfig grPixConfig = SkImageInfo2GrPixelConfig(colorType, colorSpace,
435 *ctx->caps());
Brian Salomonbdecacf2018-02-02 20:32:49 -0500436 int supportedSampleCount =
437 ctx->caps()->getRenderTargetSampleCount(sampleCount, grPixConfig);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400438 if (sampleCount != supportedSampleCount) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400439 SkDebugf("Configuration '%s' sample count %d is not a supported sample count.\n",
440 config->getTag().c_str(), sampleCount);
svaisanenc47635e2016-01-28 06:05:43 -0800441 return;
442 }
443 } else {
Ben Wagner32fa5102017-08-10 21:25:55 -0400444 SkDebugf("No context was available matching config '%s'.\n",
445 config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800446 return;
447 }
448
449 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800450 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800451 Benchmark::kGPU_Backend,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400452 colorType,
svaisanenc47635e2016-01-28 06:05:43 -0800453 kPremul_SkAlphaType,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400454 sk_ref_sp(colorSpace),
svaisanenc47635e2016-01-28 06:05:43 -0800455 sampleCount,
456 ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700457 ctxOverrides,
kkinnunend4e1c352016-03-01 23:41:26 -0800458 gpuConfig->getUseDIText()
459 };
svaisanenc47635e2016-01-28 06:05:43 -0800460
461 configs->push_back(target);
462 return;
463 }
464#endif
465
brianosmanb109b8c2016-06-16 13:03:24 -0700466 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
467 if (config->getTag().equals(#name)) { \
Ben Wagner32fa5102017-08-10 21:25:55 -0400468 if (!FLAGS_cpu) { \
469 SkDebugf("Skipping config '%s' as requested.\n", \
470 config->getTag().c_str()); \
471 return; \
472 } \
brianosmanb109b8c2016-06-16 13:03:24 -0700473 Config config = { \
474 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
csmartdaltone812d492017-02-21 12:36:05 -0700475 0, kBogusContextType, kBogusContextOverrides, false \
brianosmanb109b8c2016-06-16 13:03:24 -0700476 }; \
477 configs->push_back(config); \
478 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700479 }
mtkleine714e752014-07-31 12:13:48 -0700480
Ben Wagner32fa5102017-08-10 21:25:55 -0400481 CPU_CONFIG(nonrendering, kNonRendering_Backend,
482 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800483
Mike Reede6befa52017-08-28 11:30:48 -0400484 CPU_CONFIG(a8, kRaster_Backend,
485 kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr)
Ben Wagner32fa5102017-08-10 21:25:55 -0400486 CPU_CONFIG(8888, kRaster_Backend,
487 kN32_SkColorType, kPremul_SkAlphaType, nullptr)
488 CPU_CONFIG(565, kRaster_Backend,
489 kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
490 auto srgbColorSpace = SkColorSpace::MakeSRGB();
491 CPU_CONFIG(srgb, kRaster_Backend,
492 kN32_SkColorType, kPremul_SkAlphaType, srgbColorSpace)
493 auto srgbLinearColorSpace = SkColorSpace::MakeSRGBLinear();
494 CPU_CONFIG(f16, kRaster_Backend,
495 kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbLinearColorSpace)
mtkleinbb6a0282014-07-01 08:43:42 -0700496
svaisanenc47635e2016-01-28 06:05:43 -0800497 #undef CPU_CONFIG
Ben Wagner32fa5102017-08-10 21:25:55 -0400498
499 SkDebugf("Unknown config '%s'.\n", config->getTag().c_str());
mtkleinf3723212014-06-25 14:08:00 -0700500}
501
svaisanenc47635e2016-01-28 06:05:43 -0800502// Append all configs that are enabled and supported.
503void create_configs(SkTArray<Config>* configs) {
504 SkCommandLineConfigArray array;
505 ParseConfigs(FLAGS_config, &array);
506 for (int i = 0; i < array.count(); ++i) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400507 create_config(array[i].get(), configs);
svaisanenc47635e2016-01-28 06:05:43 -0800508 }
Ben Wagner32fa5102017-08-10 21:25:55 -0400509
510 // If no just default configs were requested, then we're okay.
511 if (array.count() == 0 || FLAGS_config.count() == 0 ||
512 // If we've been told to ignore undefined flags, we're okay.
513 FLAGS_undefok ||
514 // Otherwise, make sure that all specified configs have been created.
515 array.count() == configs->count()) {
516 return;
517 }
518 SkDebugf("Invalid --config. Use --undefok to bypass this warning.\n");
519 exit(1);
svaisanenc47635e2016-01-28 06:05:43 -0800520}
521
brianosman9f1f6e22016-09-15 08:33:02 -0700522// disable warning : switch statement contains default but no 'case' labels
523#if defined _WIN32
524#pragma warning ( push )
525#pragma warning ( disable : 4065 )
526#endif
527
halcanary96fcdcc2015-08-27 07:41:13 -0700528// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700529static Target* is_enabled(Benchmark* bench, const Config& config) {
530 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700531 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700532 }
533
reede5ea5002014-09-03 11:54:58 -0700534 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700535 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700536
halcanary96fcdcc2015-08-27 07:41:13 -0700537 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700538
tomhudsond968a6f2015-03-26 11:28:06 -0700539 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700540#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700541 case Benchmark::kGPU_Backend:
542 target = new GPUTarget(config);
543 break;
bsalomonc2553372014-07-22 13:09:05 -0700544#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700545 default:
546 target = new Target(config);
547 break;
548 }
bsalomonc2553372014-07-22 13:09:05 -0700549
tomhudsond968a6f2015-03-26 11:28:06 -0700550 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700551 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700552 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700553 }
554 return target;
555}
556
brianosman9f1f6e22016-09-15 08:33:02 -0700557#if defined _WIN32
558#pragma warning ( pop )
559#endif
560
reed42943c82016-09-12 12:01:44 -0700561static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700562 uint32_t minOutputSize, int* width, int* height) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400563 std::unique_ptr<SkBitmapRegionDecoder> brd(
msarettd1227a72016-05-18 06:23:57 -0700564 SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy));
msarett7f691442015-09-22 11:56:16 -0700565 if (nullptr == brd.get()) {
566 // This is indicates that subset decoding is not supported for a particular image format.
567 return false;
568 }
569
msarett7f691442015-09-22 11:56:16 -0700570 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
571 (uint32_t) brd->height()) {
572 // This indicates that the image is not large enough to decode a
573 // minOutputSize x minOutputSize subset at the given sampleSize.
574 return false;
575 }
576
577 // Set the image width and height. The calling code will use this to choose subsets to decode.
578 *width = brd->width();
579 *height = brd->height();
580 return true;
581}
582
egdaniel3bf92062015-06-26 08:12:46 -0700583static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700584 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700585}
586
fmalita6519c212016-09-14 08:05:17 -0700587static void collect_files(const SkCommandLineFlags::StringArray& paths, const char* ext,
588 SkTArray<SkString>* list) {
589 for (int i = 0; i < paths.count(); ++i) {
590 if (SkStrEndsWith(paths[i], ext)) {
591 list->push_back(SkString(paths[i]));
592 } else {
593 SkOSFile::Iter it(paths[i], ext);
594 SkString path;
595 while (it.next(&path)) {
596 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
597 }
598 }
599 }
600}
601
mtkleine714e752014-07-31 12:13:48 -0700602class BenchmarkStream {
603public:
mtklein92007582014-08-01 07:46:52 -0700604 BenchmarkStream() : fBenches(BenchRegistry::Head())
605 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700606 , fCurrentRecording(0)
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400607 , fCurrentPiping(0)
Mike Reede45ff462017-12-06 10:47:03 -0500608 , fCurrentDeserialPicture(0)
mtklein92007582014-08-01 07:46:52 -0700609 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800610 , fCurrentSKP(0)
fmalita6519c212016-09-14 08:05:17 -0700611 , fCurrentSVG(0)
msarett95f192d2015-02-13 09:05:41 -0800612 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700613 , fCurrentCodec(0)
msarett84451022016-02-11 06:45:51 -0800614 , fCurrentAndroidCodec(0)
msarett7f691442015-09-22 11:56:16 -0700615 , fCurrentBRDImage(0)
msarett2cee9022016-06-03 08:25:21 -0700616 , fCurrentColorImage(0)
msarett95f192d2015-02-13 09:05:41 -0800617 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800618 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700619 , fCurrentSubsetType(0)
msarett84451022016-02-11 06:45:51 -0800620 , fCurrentSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700621 , fCurrentAnimSKP(0) {
fmalita6519c212016-09-14 08:05:17 -0700622 collect_files(FLAGS_skps, ".skp", &fSKPs);
623 collect_files(FLAGS_svgs, ".svg", &fSVGs);
mtkleine714e752014-07-31 12:13:48 -0700624
mtklein92007582014-08-01 07:46:52 -0700625 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
626 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
627 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
628 exit(1);
629 }
630
631 for (int i = 0; i < FLAGS_scales.count(); i++) {
632 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
633 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
634 exit(1);
635 }
636 }
robertphillips5b693772014-11-21 06:19:36 -0800637
cdalton63a82852015-06-29 14:06:10 -0700638 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
639 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700640 exit(1);
641 }
642
robertphillips5b693772014-11-21 06:19:36 -0800643 if (FLAGS_mpd) {
644 fUseMPDs.push_back() = true;
645 }
mtkleinc751ecb2015-06-15 08:56:38 -0700646 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700647
msarett95f192d2015-02-13 09:05:41 -0800648 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700649 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800650 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800651 }
msarett2cee9022016-06-03 08:25:21 -0700652 if (!CollectImages(FLAGS_colorImages, &fColorImages)) {
653 exit(1);
654 }
mtklein95553d92015-03-12 08:24:21 -0700655
msarett95f192d2015-02-13 09:05:41 -0800656 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700657 fColorTypes.push_back(kN32_SkColorType);
658 if (!FLAGS_simpleCodec) {
659 fColorTypes.push_back(kRGB_565_SkColorType);
660 fColorTypes.push_back(kAlpha_8_SkColorType);
msarett67cb6662016-06-21 08:49:26 -0700661 fColorTypes.push_back(kGray_8_SkColorType);
662 }
mtklein92007582014-08-01 07:46:52 -0700663 }
664
reedca2622b2016-03-18 07:25:55 -0700665 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700666 // Not strictly necessary, as it will be checked again later,
667 // but helps to avoid a lot of pointless work if we're going to skip it.
cdalton91e457d2016-02-17 11:10:16 -0800668 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700669 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700670 }
671
bungemanf93d7112016-09-16 06:24:20 -0700672 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
673 if (!stream) {
mtkleinfd731ce2014-09-10 12:19:30 -0700674 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700675 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700676 }
677
reedca2622b2016-03-18 07:25:55 -0700678 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700679 }
680
fmalita6519c212016-09-14 08:05:17 -0700681 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
682 SkFILEStream stream(path);
683 if (!stream.isValid()) {
684 SkDebugf("Could not read %s.\n", path);
685 return nullptr;
686 }
687
fmalitae1baa7c2016-09-14 12:04:30 -0700688 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
fmalita6519c212016-09-14 08:05:17 -0700689 if (!svgDom) {
690 SkDebugf("Could not parse %s.\n", path);
691 return nullptr;
692 }
693
fmalitae1baa7c2016-09-14 12:04:30 -0700694 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
695 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
696 if (svgDom->containerSize().isEmpty()) {
697 svgDom->setContainerSize(kDefaultContainerSize);
698 }
699
fmalita6519c212016-09-14 08:05:17 -0700700 SkPictureRecorder recorder;
fmalitae1baa7c2016-09-14 12:04:30 -0700701 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
702 svgDom->containerSize().height()));
fmalita6519c212016-09-14 08:05:17 -0700703 return recorder.finishRecordingAsPicture();
704 }
705
mtklein92007582014-08-01 07:46:52 -0700706 Benchmark* next() {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400707 std::unique_ptr<Benchmark> bench;
mtklein65dfd2f2016-02-03 10:40:54 -0800708 do {
709 bench.reset(this->rawNext());
710 if (!bench) {
711 return nullptr;
712 }
713 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
714 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700715 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800716 }
717
718 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700719 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700720 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700721 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700722 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700723 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700724 return bench;
725 }
mtklein92007582014-08-01 07:46:52 -0700726
mtkleine714e752014-07-31 12:13:48 -0700727 while (fGMs) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400728 std::unique_ptr<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700729 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800730 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700731 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700732 fBenchType = "micro";
mtklein18300a32016-03-16 13:53:35 -0700733 return new GMBench(gm.release());
mtkleine714e752014-07-31 12:13:48 -0700734 }
735 }
mtklein92007582014-08-01 07:46:52 -0700736
mtkleinfd731ce2014-09-10 12:19:30 -0700737 // First add all .skps as RecordingBenches.
738 while (fCurrentRecording < fSKPs.count()) {
739 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700740 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
741 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700742 continue;
743 }
744 SkString name = SkOSPath::Basename(path.c_str());
745 fSourceType = "skp";
746 fBenchType = "recording";
Mike Klein88d90712018-01-27 17:30:04 +0000747 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
748 fSKPOps = pic->approximateOpCount();
mtklein8c1a4f82016-08-08 06:56:22 -0700749 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh, FLAGS_lite);
mtkleinfd731ce2014-09-10 12:19:30 -0700750 }
751
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400752 // Add all .skps as PipeBenches.
753 while (fCurrentPiping < fSKPs.count()) {
754 const SkString& path = fSKPs[fCurrentPiping++];
755 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
756 if (!pic) {
757 continue;
758 }
759 SkString name = SkOSPath::Basename(path.c_str());
760 fSourceType = "skp";
761 fBenchType = "piping";
Mike Klein88d90712018-01-27 17:30:04 +0000762 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
763 fSKPOps = pic->approximateOpCount();
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400764 return new PipingBench(name.c_str(), pic.get());
765 }
766
Mike Reede45ff462017-12-06 10:47:03 -0500767 // Add all .skps as DeserializePictureBenchs.
768 while (fCurrentDeserialPicture < fSKPs.count()) {
769 const SkString& path = fSKPs[fCurrentDeserialPicture++];
770 sk_sp<SkData> data = SkData::MakeFromFileName(path.c_str());
771 if (!data) {
772 continue;
773 }
774 SkString name = SkOSPath::Basename(path.c_str());
775 fSourceType = "skp";
776 fBenchType = "deserial";
Mike Reed7557bbb2017-12-24 19:50:57 -0500777 fSKPBytes = static_cast<double>(data->size());
Mike Klein88d90712018-01-27 17:30:04 +0000778 fSKPOps = 0;
Mike Reede45ff462017-12-06 10:47:03 -0500779 return new DeserializePictureBench(name.c_str(), std::move(data));
780 }
781
mtkleinfd731ce2014-09-10 12:19:30 -0700782 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700783 while (fCurrentScale < fScales.count()) {
784 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800785 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700786 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
787 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800788 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700789 continue;
790 }
robertphillips5b693772014-11-21 06:19:36 -0800791
792 while (fCurrentUseMPD < fUseMPDs.count()) {
793 if (FLAGS_bbh) {
794 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
795 SkRTreeFactory factory;
796 SkPictureRecorder recorder;
robertphillips5b693772014-11-21 06:19:36 -0800797 pic->playback(recorder.beginRecording(pic->cullRect().width(),
798 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800799 &factory,
robertphillipsdda54452016-07-13 13:27:16 -0700800 0));
reedca2622b2016-03-18 07:25:55 -0700801 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800802 }
803 SkString name = SkOSPath::Basename(path.c_str());
804 fSourceType = "skp";
805 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700806 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
807 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700808 }
robertphillips5b693772014-11-21 06:19:36 -0800809 fCurrentUseMPD = 0;
810 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700811 }
fmalita6519c212016-09-14 08:05:17 -0700812
813 while (fCurrentSVG++ < fSVGs.count()) {
814 const char* path = fSVGs[fCurrentSVG - 1].c_str();
815 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
816 fSourceType = "svg";
817 fBenchType = "playback";
818 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
819 fScales[fCurrentScale], false, FLAGS_loopSKP);
820 }
821 }
822
mtklein92007582014-08-01 07:46:52 -0700823 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700824 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700825 fCurrentScale++;
826 }
827
joshualitt261c3ad2015-04-27 09:16:57 -0700828 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700829 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700830 while (fCurrentAnimSKP < fSKPs.count()) {
831 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700832 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
833 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700834 fCurrentAnimSKP++;
835 continue;
836 }
837
838 fCurrentAnimSKP++;
839 SkString name = SkOSPath::Basename(path.c_str());
Hal Canary2db83612016-11-04 13:02:54 -0400840 sk_sp<SKPAnimationBench::Animation> animation(
cdalton63a82852015-06-29 14:06:10 -0700841 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
Hal Canary2db83612016-11-04 13:02:54 -0400842 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation.get(),
halcanary385fe4d2015-08-26 13:07:48 -0700843 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700844 }
845 }
846
scroggo60869a42015-04-01 12:09:17 -0700847 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700848 fSourceType = "image";
849 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700850 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800851 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
852 continue;
853 }
bungeman38d909e2016-08-02 14:40:46 -0700854 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400855 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700856 if (!codec) {
857 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700858 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700859 continue;
860 }
scroggo21027992015-04-02 13:22:38 -0700861
scroggo60869a42015-04-01 12:09:17 -0700862 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700863 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700864
msarettc7796b92016-01-07 14:20:20 -0800865 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700866 if (FLAGS_simpleCodec) {
867 if (kUnpremul_SkAlphaType == alphaType) {
868 alphaType = kPremul_SkAlphaType;
869 }
870
871 fCurrentColorType++;
872 } else {
873 switch (alphaType) {
874 case kOpaque_SkAlphaType:
875 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800876 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700877 break;
878 case kUnpremul_SkAlphaType:
879 case kPremul_SkAlphaType:
880 if (0 == fCurrentAlphaType) {
881 // Test unpremul first.
882 alphaType = kUnpremul_SkAlphaType;
883 fCurrentAlphaType++;
884 } else {
885 // Test premul.
886 alphaType = kPremul_SkAlphaType;
887 fCurrentAlphaType = 0;
888 fCurrentColorType++;
889 }
890 break;
891 default:
892 SkASSERT(false);
893 fCurrentColorType++;
894 break;
895 }
scroggo21027992015-04-02 13:22:38 -0700896 }
897
msarettc7796b92016-01-07 14:20:20 -0800898 // Make sure we can decode to this color type and alpha type.
899 SkImageInfo info =
900 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700901 const size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400902 SkAutoMalloc storage(info.computeByteSize(rowBytes));
scroggo21027992015-04-02 13:22:38 -0700903
scroggoeb602a52015-07-09 08:16:03 -0700904 const SkCodec::Result result = codec->getPixels(
Leon Scroggins571b30f2017-07-11 17:35:31 +0000905 info, storage.get(), rowBytes);
scroggo60869a42015-04-01 12:09:17 -0700906 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700907 case SkCodec::kSuccess:
908 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700909 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700910 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700911 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700912 // This is okay. Not all conversions are valid.
913 break;
scroggo60869a42015-04-01 12:09:17 -0700914 default:
915 // This represents some sort of failure.
916 SkASSERT(false);
917 break;
918 }
919 }
920 fCurrentColorType = 0;
921 }
922
msarett84451022016-02-11 06:45:51 -0800923 // Run AndroidCodecBenches
924 const int sampleSizes[] = { 2, 4, 8 };
925 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
926 fSourceType = "image";
927 fBenchType = "skandroidcodec";
928
929 const SkString& path = fImages[fCurrentAndroidCodec];
930 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
931 continue;
932 }
bungeman38d909e2016-08-02 14:40:46 -0700933 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400934 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800935 if (!codec) {
936 // Nothing to time.
937 SkDebugf("Cannot find codec for %s\n", path.c_str());
938 continue;
939 }
940
941 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
942 int sampleSize = sampleSizes[fCurrentSampleSize];
943 fCurrentSampleSize++;
944 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
945 // Avoid benchmarking scaled decodes of already small images.
946 break;
947 }
948
bungeman38d909e2016-08-02 14:40:46 -0700949 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
950 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -0800951 }
952 fCurrentSampleSize = 0;
953 }
954
msarett7f691442015-09-22 11:56:16 -0700955 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700956 // We intend to create benchmarks that model the use cases in
957 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
958 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
959 // the image. For that reason, we will benchmark decodes in five representative locations
960 // in the image. Additionally, this use case utilizes power of two scaling, so we will
961 // test on power of two sample sizes. The output tile is always 512x512, so, when a
962 // sampleSize is used, the size of the subset that is decoded is always
963 // (sampleSize*512)x(sampleSize*512).
964 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700965 // All use cases we are aware of only scale by powers of two.
966 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
967 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800968 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700969 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800970 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700971 fSourceType = "image";
972 fBenchType = "BRD";
973
mtklein6f0ff912016-01-11 09:04:21 -0800974 const SkString& path = fImages[fCurrentBRDImage];
975 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
976 continue;
977 }
scroggo860e8a62015-10-15 07:51:28 -0700978
msarettd1227a72016-05-18 06:23:57 -0700979 while (fCurrentColorType < fColorTypes.count()) {
980 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
981 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700982
bungeman38d909e2016-08-02 14:40:46 -0700983 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -0700984 const SkColorType colorType = fColorTypes[fCurrentColorType];
985 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
986 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -0700987
msarettd1227a72016-05-18 06:23:57 -0700988 int width = 0;
989 int height = 0;
reed42943c82016-09-12 12:01:44 -0700990 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -0700991 &width, &height)) {
992 break;
msarett7f691442015-09-22 11:56:16 -0700993 }
msarettd1227a72016-05-18 06:23:57 -0700994
995 SkString basename = SkOSPath::Basename(path.c_str());
996 SkIRect subset;
997 const uint32_t subsetSize = sampleSize * minOutputSize;
998 switch (currentSubsetType) {
999 case kTopLeft_SubsetType:
1000 basename.append("_TopLeft");
1001 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
1002 break;
1003 case kTopRight_SubsetType:
1004 basename.append("_TopRight");
1005 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
1006 subsetSize);
1007 break;
1008 case kMiddle_SubsetType:
1009 basename.append("_Middle");
1010 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
1011 (height - subsetSize) / 2, subsetSize, subsetSize);
1012 break;
1013 case kBottomLeft_SubsetType:
1014 basename.append("_BottomLeft");
1015 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
1016 subsetSize);
1017 break;
1018 case kBottomRight_SubsetType:
1019 basename.append("_BottomRight");
1020 subset = SkIRect::MakeXYWH(width - subsetSize,
1021 height - subsetSize, subsetSize, subsetSize);
1022 break;
1023 default:
1024 SkASSERT(false);
1025 }
1026
1027 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
1028 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -07001029 }
msarettd1227a72016-05-18 06:23:57 -07001030 fCurrentSubsetType = 0;
1031 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -07001032 }
msarettd1227a72016-05-18 06:23:57 -07001033 fCurrentSampleSize = 0;
1034 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -07001035 }
msarettd1227a72016-05-18 06:23:57 -07001036 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -07001037 }
1038
msarett2cee9022016-06-03 08:25:21 -07001039 while (fCurrentColorImage < fColorImages.count()) {
1040 fSourceType = "colorimage";
1041 fBenchType = "skcolorcodec";
1042 const SkString& path = fColorImages[fCurrentColorImage];
1043 fCurrentColorImage++;
1044 sk_sp<SkData> encoded = SkData::MakeFromFileName(path.c_str());
1045 if (encoded) {
1046 return new ColorCodecBench(SkOSPath::Basename(path.c_str()).c_str(),
1047 std::move(encoded));
1048 } else {
1049 SkDebugf("Could not read file %s.\n", path.c_str());
1050 }
1051 }
1052
halcanary96fcdcc2015-08-27 07:41:13 -07001053 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001054 }
mtklein92007582014-08-01 07:46:52 -07001055
1056 void fillCurrentOptions(ResultsWriter* log) const {
1057 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001058 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001059 if (0 == strcmp(fSourceType, "skp")) {
1060 log->configOption("clip",
1061 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1062 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001063 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -07001064 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001065 if (fCurrentUseMPD > 0) {
1066 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1067 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1068 }
mtklein92007582014-08-01 07:46:52 -07001069 }
mtklein051e56d2014-12-04 08:46:51 -08001070 if (0 == strcmp(fBenchType, "recording")) {
Mike Reed7557bbb2017-12-24 19:50:57 -05001071 log->metric("bytes", fSKPBytes);
Mike Klein88d90712018-01-27 17:30:04 +00001072 log->metric("ops", fSKPOps);
mtklein051e56d2014-12-04 08:46:51 -08001073 }
mtklein92007582014-08-01 07:46:52 -07001074 }
1075
mtkleine714e752014-07-31 12:13:48 -07001076private:
msarettb23e6aa2015-06-09 13:56:10 -07001077 enum SubsetType {
1078 kTopLeft_SubsetType = 0,
1079 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001080 kMiddle_SubsetType = 2,
1081 kBottomLeft_SubsetType = 3,
1082 kBottomRight_SubsetType = 4,
1083 kTranslate_SubsetType = 5,
1084 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001085 kLast_SubsetType = kZoom_SubsetType,
1086 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001087 };
1088
mtkleine714e752014-07-31 12:13:48 -07001089 const BenchRegistry* fBenches;
1090 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001091 SkIRect fClip;
1092 SkTArray<SkScalar> fScales;
1093 SkTArray<SkString> fSKPs;
fmalita6519c212016-09-14 08:05:17 -07001094 SkTArray<SkString> fSVGs;
robertphillips5b693772014-11-21 06:19:36 -08001095 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001096 SkTArray<SkString> fImages;
msarett2cee9022016-06-03 08:25:21 -07001097 SkTArray<SkString> fColorImages;
msarett74deb982015-10-20 16:45:56 -07001098 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001099 SkScalar fZoomMax;
1100 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001101
Mike Klein88d90712018-01-27 17:30:04 +00001102 double fSKPBytes, fSKPOps;
mtklein051e56d2014-12-04 08:46:51 -08001103
mtkleinfd731ce2014-09-10 12:19:30 -07001104 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1105 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1106 int fCurrentRecording;
Mike Reed9cdd2ab2016-10-21 10:43:36 -04001107 int fCurrentPiping;
Mike Reede45ff462017-12-06 10:47:03 -05001108 int fCurrentDeserialPicture;
mtklein92007582014-08-01 07:46:52 -07001109 int fCurrentScale;
1110 int fCurrentSKP;
fmalita6519c212016-09-14 08:05:17 -07001111 int fCurrentSVG;
robertphillips5b693772014-11-21 06:19:36 -08001112 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001113 int fCurrentCodec;
msarett84451022016-02-11 06:45:51 -08001114 int fCurrentAndroidCodec;
msarett7f691442015-09-22 11:56:16 -07001115 int fCurrentBRDImage;
msarett2cee9022016-06-03 08:25:21 -07001116 int fCurrentColorImage;
msarett95f192d2015-02-13 09:05:41 -08001117 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -08001118 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -07001119 int fCurrentSubsetType;
msarett84451022016-02-11 06:45:51 -08001120 int fCurrentSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001121 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001122};
1123
msarettc149f0e2016-01-04 11:35:43 -08001124// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1125// This prints something every once in a while so that it knows we're still working.
1126static void start_keepalive() {
Mike Klein03141d22017-10-30 11:57:15 -04001127 static std::thread* intentionallyLeaked = new std::thread([]{
1128 for (;;) {
1129 static const int kSec = 1200;
1130 #if defined(SK_BUILD_FOR_WIN)
1131 Sleep(kSec * 1000);
1132 #else
1133 sleep(kSec);
1134 #endif
1135 SkDebugf("\nBenchmarks still running...\n");
msarettc149f0e2016-01-04 11:35:43 -08001136 }
Mike Klein03141d22017-10-30 11:57:15 -04001137 });
1138 (void)intentionallyLeaked;
msarettc149f0e2016-01-04 11:35:43 -08001139}
1140
Mike Kleinbe28ee22017-02-06 12:46:20 -05001141int main(int argc, char** argv) {
1142 SkCommandLineFlags::Parse(argc, argv);
Brian Osman53136aa2017-07-20 15:43:35 -04001143
Brian Osmanbc8150f2017-07-24 11:38:01 -04001144 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -04001145
Mike Kleinadacaef2017-02-06 09:26:14 -05001146#if defined(SK_BUILD_FOR_IOS)
1147 cd_Documents();
1148#endif
jcgregorio3b27ade2014-11-13 08:06:40 -08001149 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001150 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001151 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001152
krajcevski69a55602014-08-13 10:46:31 -07001153#if SK_SUPPORT_GPU
Chris Dalton040238b2017-12-18 14:22:34 -07001154 SetCtxOptionsFromCommonFlags(&grContextOpts);
krajcevski69a55602014-08-13 10:46:31 -07001155#endif
1156
bsalomon06cddec2014-10-24 10:40:50 -07001157 if (FLAGS_veryVerbose) {
1158 FLAGS_verbose = true;
1159 }
1160
bsalomon6eb03cc2014-08-07 14:28:50 -07001161 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001162 FLAGS_samples = 1;
1163 FLAGS_gpuFrameLag = 0;
1164 }
1165
bsalomon6eb03cc2014-08-07 14:28:50 -07001166 if (!FLAGS_writePath.isEmpty()) {
1167 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1168 if (!sk_mkdir(FLAGS_writePath[0])) {
1169 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001170 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001171 }
1172 }
1173
Ben Wagner145dbcd2016-11-03 14:40:50 -04001174 std::unique_ptr<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001175 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001176#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001177 log.reset(new NanoJSONResultsWriter(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 }
mtklein1915b622014-08-20 11:45:00 -07001183
1184 if (1 == FLAGS_properties.count() % 2) {
1185 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1186 return 1;
1187 }
1188 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1189 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1190 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001191
1192 if (1 == FLAGS_key.count() % 2) {
1193 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1194 return 1;
1195 }
1196 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001197 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001198 }
mtklein60317d0f2014-07-14 11:30:37 -07001199
mtkleinf3723212014-06-25 14:08:00 -07001200 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001201 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001202
cdaltone1b89582015-06-25 19:17:08 -07001203 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001204
bsalomon6eb03cc2014-08-07 14:28:50 -07001205 if (kAutoTuneLoops != FLAGS_loops) {
1206 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001207 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001208 SkDebugf("! -> high variance, ? -> moderate variance\n");
1209 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001210 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001211 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001212 } else {
mtkleind75c4662015-04-30 07:11:22 -07001213 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001214 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001215 }
1216
svaisanenc47635e2016-01-28 06:05:43 -08001217 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001218 create_configs(&configs);
1219
msarettc149f0e2016-01-04 11:35:43 -08001220 if (FLAGS_keepAlive) {
1221 start_keepalive();
1222 }
1223
Yuqian Liba62b4a2016-12-14 13:46:53 -05001224 gSkUseAnalyticAA = FLAGS_analyticAA;
Yuqian Lidf60e362017-07-25 11:26:31 -04001225 gSkUseDeltaAA = FLAGS_deltaAA;
liyuqian38911a72016-10-04 11:23:22 -07001226
Yuqian Lidf60e362017-07-25 11:26:31 -04001227 if (FLAGS_forceDeltaAA) {
1228 gSkForceDeltaAA = true;
1229 }
Yuqian Li550148b2017-01-13 10:13:13 -05001230 if (FLAGS_forceAnalyticAA) {
1231 gSkForceAnalyticAA = true;
1232 }
Mike Reedc928fe22017-06-05 10:20:31 -04001233 if (FLAGS_forceRasterPipeline) {
1234 gSkForceRasterPipelineBlitter = true;
1235 }
Yuqian Li550148b2017-01-13 10:13:13 -05001236
mtkleine070c2b2014-10-14 08:40:43 -07001237 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001238 BenchmarkStream benchStream;
1239 while (Benchmark* b = benchStream.next()) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001240 std::unique_ptr<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001241 if (SkCommandLineFlags::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()) {
mtklein96289052014-09-10 12:05:59 -07001246 log->bench(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
Brian Osman5ad87aa2017-09-08 16:05:23 -04001268 TRACE_EVENT2("skia", "Benchmark", "name", TRACE_STR_COPY(bench->getUniqueName()),
1269 "config", TRACE_STR_COPY(config));
Brian Osmanc3cdef52017-08-31 14:09:22 -04001270
egdaniel3bf92062015-06-26 08:12:46 -07001271 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001272 bench->perCanvasPreDraw(canvas);
1273
cdaltone1b89582015-06-25 19:17:08 -07001274 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001275 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001276 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1277 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001278
Leon Scroggins IIIb30d1132017-10-02 09:48:15 -04001279 if (kFailedLoops == loops) {
1280 // Can't be timed. A warning note has already been printed.
1281 cleanup_run(target);
1282 continue;
1283 }
1284
Yuqian Li3528eb32017-09-21 13:23:49 -04001285 if (runs == 0 && FLAGS_ms < 1000) {
1286 // Run the first bench for 1000ms to warm up the nanobench if FLAGS_ms < 1000.
1287 // Otherwise, the first few benches' measurements will be inaccurate.
1288 auto stop = now_ms() + 1000;
1289 do {
1290 time(loops, bench.get(), target);
1291 } while (now_ms() < stop);
1292 }
1293
mtkleinbbba1682015-10-28 11:36:30 -07001294 if (FLAGS_ms) {
1295 samples.reset();
1296 auto stop = now_ms() + FLAGS_ms;
1297 do {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001298 samples.push_back(time(loops, bench.get(), target) / loops);
mtkleinbbba1682015-10-28 11:36:30 -07001299 } while (now_ms() < stop);
1300 } else {
cdaltone1b89582015-06-25 19:17:08 -07001301 samples.reset(FLAGS_samples);
1302 for (int s = 0; s < FLAGS_samples; s++) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001303 samples[s] = time(loops, bench.get(), target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001304 }
cdaltone1b89582015-06-25 19:17:08 -07001305 }
mtkleinf3723212014-06-25 14:08:00 -07001306
joshualitte45c81c2015-12-02 09:05:37 -08001307#if SK_SUPPORT_GPU
1308 SkTArray<SkString> keys;
1309 SkTArray<double> values;
1310 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1311 if (gpuStatsDump) {
1312 // TODO cache stats
1313 bench->getGpuStats(canvas, &keys, &values);
1314 }
1315#endif
1316
robertphillips5b693772014-11-21 06:19:36 -08001317 bench->perCanvasPostDraw(canvas);
1318
egdaniel3bf92062015-06-26 08:12:46 -07001319 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001320 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001321 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001322 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001323 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001324 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001325 }
1326
cdaltone1b89582015-06-25 19:17:08 -07001327 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001328 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001329 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001330 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001331 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001332 log->metric("min_ms", stats.min);
George Burgess IV75b57182016-12-06 10:53:52 -08001333 log->metrics("samples", samples);
joshualitte45c81c2015-12-02 09:05:37 -08001334#if SK_SUPPORT_GPU
1335 if (gpuStatsDump) {
1336 // dump to json, only SKPBench currently returns valid keys / values
1337 SkASSERT(keys.count() == values.count());
1338 for (int i = 0; i < keys.count(); i++) {
1339 log->metric(keys[i].c_str(), values[i]);
1340 }
1341 }
1342#endif
1343
mtkleine070c2b2014-10-14 08:40:43 -07001344 if (runs++ % FLAGS_flushEvery == 0) {
1345 log->flush();
1346 }
mtklein60317d0f2014-07-14 11:30:37 -07001347
bsalomon6eb03cc2014-08-07 14:28:50 -07001348 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001349 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001350 config = ""; // Only print the config if we run the same bench on more than one.
1351 }
mtkleind75c4662015-04-30 07:11:22 -07001352 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1353 , sk_tools::getCurrResidentSetSizeMB()
1354 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001355 , bench->getUniqueName()
1356 , config);
mtkleinf3723212014-06-25 14:08:00 -07001357 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001358 const char* mark = " ";
1359 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1360 if (stddev_percent > 5) mark = "?";
1361 if (stddev_percent > 10) mark = "!";
1362
1363 SkDebugf("%10.2f %s\t%s\t%s\n",
1364 stats.median*1e3, mark, bench->getUniqueName(), config);
Mike Reedc665e5b2017-07-04 22:56:06 -04001365 } else if (FLAGS_csv) {
mtkleinf3723212014-06-25 14:08:00 -07001366 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
Mike Reedc665e5b2017-07-04 22:56:06 -04001367 SkDebugf("%g,%g,%g,%g,%g,%s,%s\n"
1368 , stats.min
1369 , stats.median
1370 , stats.mean
1371 , stats.max
1372 , stddev_percent
1373 , config
1374 , bench->getUniqueName()
1375 );
1376 } else {
1377 const char* format = "%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n";
1378 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1379 SkDebugf(format
mtkleind75c4662015-04-30 07:11:22 -07001380 , sk_tools::getCurrResidentSetSizeMB()
1381 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001382 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001383 , HUMANIZE(stats.min)
1384 , HUMANIZE(stats.median)
1385 , HUMANIZE(stats.mean)
1386 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001387 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001388 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001389 , config
mtklein96289052014-09-10 12:05:59 -07001390 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001391 );
1392 }
joshualitte45c81c2015-12-02 09:05:37 -08001393
bsalomonb12ea412015-02-02 21:19:50 -08001394#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001395 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -04001396 target->dumpStats();
bsalomon06cddec2014-10-24 10:40:50 -07001397 }
1398#endif
joshualitte45c81c2015-12-02 09:05:37 -08001399
cdalton2c56ba52015-06-26 13:32:53 -07001400 if (FLAGS_verbose) {
1401 SkDebugf("Samples: ");
1402 for (int i = 0; i < samples.count(); i++) {
1403 SkDebugf("%s ", HUMANIZE(samples[i]));
1404 }
1405 SkDebugf("%s\n", bench->getUniqueName());
1406 }
egdaniel3bf92062015-06-26 08:12:46 -07001407 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001408 }
mtkleinf3723212014-06-25 14:08:00 -07001409 }
1410
Herb Derby5a523fe2017-01-26 16:48:28 -05001411 SkGraphics::PurgeAllCaches();
1412
mtkleine1091452014-12-04 10:47:02 -08001413 log->bench("memory_usage", 0,0);
1414 log->config("meta");
1415 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1416
mtkleinf3723212014-06-25 14:08:00 -07001417 return 0;
1418}