blob: 550e0b77448835b6f79fb352b22b532071d7eb32 [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"
Brian Osmanf9810662017-08-30 10:02:10 -040033#include "SkCommonFlagsGpuThreads.h"
csmartdalton008b9d82017-02-22 12:00:42 -070034#include "SkCommonFlagsPathRenderer.h"
msarett95f192d2015-02-13 09:05:41 -080035#include "SkData.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040036#include "SkDebugfTracer.h"
Brian Osman53136aa2017-07-20 15:43:35 -040037#include "SkEventTracingPriv.h"
mtkleinf3723212014-06-25 14:08:00 -070038#include "SkGraphics.h"
halcanary4dbbd042016-06-07 17:21:10 -070039#include "SkLeanWindows.h"
mtklein20840502014-08-21 15:51:22 -070040#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050041#include "SkOSPath.h"
mtklein20840502014-08-21 15:51:22 -070042#include "SkPictureRecorder.h"
Hal Canary95e3c052017-01-11 12:44:43 -050043#include "SkSVGDOM.h"
44#include "SkScan.h"
mtkleinf3723212014-06-25 14:08:00 -070045#include "SkString.h"
46#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080047#include "SkTaskGroup.h"
Brian Osmanc3cdef52017-08-31 14:09:22 -040048#include "SkTraceEvent.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040049#include "Stats.h"
joshualitt3ebd0502016-02-09 07:18:08 -080050#include "ThermalManager.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040051#include "ios_utils.h"
mtkleinf3723212014-06-25 14:08:00 -070052
bungeman60e0fee2015-08-26 05:15:46 -070053#include <stdlib.h>
Mike Klein03141d22017-10-30 11:57:15 -040054#include <thread>
bungeman60e0fee2015-08-26 05:15:46 -070055
Mike Reedc928fe22017-06-05 10:20:31 -040056extern bool gSkForceRasterPipelineBlitter;
57
scroggo38ce0a72016-01-07 07:28:47 -080058#ifndef SK_BUILD_FOR_WIN32
59 #include <unistd.h>
60#endif
61
mtkleinbb6a0282014-07-01 08:43:42 -070062#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070063 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070064 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070065 #include "GrContextFactory.h"
bsalomon3724e572016-03-30 18:56:19 -070066 #include "gl/GrGLUtil.h"
Greg Daniel81e7bf82017-07-19 14:47:42 -040067 #include "SkGr.h"
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040068 using sk_gpu_test::ContextInfo;
bsalomon3724e572016-03-30 18:56:19 -070069 using sk_gpu_test::GrContextFactory;
bsalomonc8699322016-05-11 11:55:36 -070070 using sk_gpu_test::TestContext;
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040071 GrContextOptions grContextOpts;
mtkleinbb6a0282014-07-01 08:43:42 -070072#endif
73
bsalomon682c2692015-05-22 14:01:46 -070074 struct GrContextOptions;
75
reed53249782014-10-10 09:09:52 -070076static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070077
Mike Kleind8ee67c2017-01-19 12:14:50 -050078#if !defined(__has_feature)
79 #define __has_feature(x) 0
80#endif
81
mtkleinb5110422014-08-07 15:20:02 -070082static const int kDefaultLoops =
Mike Kleind8ee67c2017-01-19 12:14:50 -050083#if defined(SK_DEBUG) || __has_feature(address_sanitizer)
bsalomon6eb03cc2014-08-07 14:28:50 -070084 1;
mtkleina189ccd2014-07-14 12:28:47 -070085#else
bsalomon6eb03cc2014-08-07 14:28:50 -070086 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070087#endif
88
bsalomon6eb03cc2014-08-07 14:28:50 -070089static SkString loops_help_txt() {
90 SkString help;
91 help.printf("Number of times to run each bench. Set this to %d to auto-"
92 "tune for each bench. Timings are only reported when auto-tuning.",
93 kAutoTuneLoops);
94 return help;
95}
96
cdaltone1b89582015-06-25 19:17:08 -070097static SkString to_string(int n) {
98 SkString str;
99 str.appendS32(n);
100 return str;
101}
102
Ben Wagner32fa5102017-08-10 21:25:55 -0400103DECLARE_bool(undefok);
104
bsalomon6eb03cc2014-08-07 14:28:50 -0700105DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
106
mtkleinf3723212014-06-25 14:08:00 -0700107DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -0700108DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -0700109DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
110DEFINE_double(overheadGoal, 0.0001,
111 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -0700112DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -0700113DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -0700114
mtklein60317d0f2014-07-14 11:30:37 -0700115DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -0700116DEFINE_int32(maxCalibrationAttempts, 3,
117 "Try up to this many times to guess loops for a bench, or skip the bench.");
118DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700119DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
120DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700121DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
122 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700123DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
mtklein8c1a4f82016-08-08 06:56:22 -0700124DEFINE_bool(lite, false, "Use SkLiteRecorder in recording benchmarks?");
robertphillips5b693772014-11-21 06:19:36 -0800125DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700126DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700127DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
bsalomonb12ea412015-02-02 21:19:50 -0800128DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800129DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
msarettc149f0e2016-01-04 11:35:43 -0800130DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
joshualitt3ebd0502016-02-09 07:18:08 -0800131DEFINE_string(useThermalManager, "0,1,10,1000", "enabled,threshold,sleepTimeMs,TimeoutMs for "
132 "thermalManager\n");
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
csmartdalton008b9d82017-02-22 12:00:42 -0700141#if SK_SUPPORT_GPU
142DEFINE_pathrenderer_flag;
143#endif
144
mtkleinbbba1682015-10-28 11:36:30 -0700145static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
146
mtkleinf3723212014-06-25 14:08:00 -0700147static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700148 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800149 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700150}
mtklein55b0ffc2014-07-17 08:38:23 -0700151#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700152
tomhudsond968a6f2015-03-26 11:28:06 -0700153bool Target::init(SkImageInfo info, Benchmark* bench) {
154 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700155 this->surface = SkSurface::MakeRaster(info);
156 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700157 return false;
158 }
159 }
160 return true;
161}
162bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700163 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700164 if (!canvas) {
165 return false;
166 }
Mike Reed12e946b2017-04-17 10:53:29 -0400167 bmp->allocPixels(canvas->imageInfo());
168 if (!canvas->readPixels(*bmp, 0, 0)) {
tomhudsond968a6f2015-03-26 11:28:06 -0700169 SkDebugf("Can't read canvas pixels.\n");
170 return false;
171 }
172 return true;
173}
174
175#if SK_SUPPORT_GPU
176struct GPUTarget : public Target {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400177 explicit GPUTarget(const Config& c) : Target(c) {}
178 ContextInfo contextInfo;
179 std::unique_ptr<GrContextFactory> factory;
tomhudsond968a6f2015-03-26 11:28:06 -0700180
181 void setup() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400182 this->contextInfo.testContext()->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700183 // Make sure we're done with whatever came before.
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400184 this->contextInfo.testContext()->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700185 }
186 void endTiming() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400187 if (this->contextInfo.testContext()) {
188 this->contextInfo.testContext()->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700189 }
190 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400191 void fence() override { this->contextInfo.testContext()->finish(); }
mtkleind75c4662015-04-30 07:11:22 -0700192
cdaltond416a5b2015-06-23 13:23:44 -0700193 bool needsFrameTiming(int* maxFrameLag) const override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400194 if (!this->contextInfo.testContext()->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700195 // Frame lag is unknown.
196 *maxFrameLag = FLAGS_gpuFrameLag;
197 }
198 return true;
199 }
tomhudsond968a6f2015-03-26 11:28:06 -0700200 bool init(SkImageInfo info, Benchmark* bench) override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400201 GrContextOptions options = grContextOpts;
202 bench->modifyGrContextOptions(&options);
203 this->factory.reset(new GrContextFactory(options));
bsalomonafcd7cd2015-08-31 12:39:41 -0700204 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
205 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700206 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400207 this->surface = SkSurface::MakeRenderTarget(
208 this->factory->get(this->config.ctxType, this->config.ctxOverrides),
209 SkBudgeted::kNo, info, this->config.samples, &props);
210 this->contextInfo =
211 this->factory->getContextInfo(this->config.ctxType, this->config.ctxOverrides);
tomhudsond968a6f2015-03-26 11:28:06 -0700212 if (!this->surface.get()) {
213 return false;
214 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400215 if (!this->contextInfo.testContext()->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700216 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800217 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700218 }
tomhudsond968a6f2015-03-26 11:28:06 -0700219 return true;
220 }
221 void fillOptions(ResultsWriter* log) override {
222 const GrGLubyte* version;
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400223 if (this->contextInfo.backend() == kOpenGL_GrBackend) {
224 const GrGLInterface* gl = reinterpret_cast<const GrGLInterface*>(
225 this->contextInfo.testContext()->backendContext());
bsalomonc8699322016-05-11 11:55:36 -0700226 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
227 log->configOption("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700228
bsalomonc8699322016-05-11 11:55:36 -0700229 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
230 log->configOption("GL_RENDERER", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700231
bsalomonc8699322016-05-11 11:55:36 -0700232 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VENDOR));
233 log->configOption("GL_VENDOR", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700234
bsalomonc8699322016-05-11 11:55:36 -0700235 GR_GL_CALL_RET(gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
236 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
237 }
tomhudsond968a6f2015-03-26 11:28:06 -0700238 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400239
240 void dumpStats() override {
241 this->contextInfo.grContext()->printCacheStats();
242 this->contextInfo.grContext()->printGpuStats();
243 }
tomhudsond968a6f2015-03-26 11:28:06 -0700244};
mtkleind75c4662015-04-30 07:11:22 -0700245
tomhudsond968a6f2015-03-26 11:28:06 -0700246#endif
247
tomhudson75a0ebb2015-03-27 12:11:44 -0700248static double time(int loops, Benchmark* bench, Target* target) {
249 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700250 if (canvas) {
251 canvas->clear(SK_ColorWHITE);
252 }
joshualitt8a6697a2015-09-30 12:11:07 -0700253 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700254 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700255 canvas = target->beginTiming(canvas);
256 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700257 if (canvas) {
258 canvas->flush();
259 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700260 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700261 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700262 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700263 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700264}
265
mtkleinf3723212014-06-25 14:08:00 -0700266static double estimate_timer_overhead() {
267 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700268 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700269 double start = now_ms();
270 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700271 }
272 return overhead / FLAGS_overheadLoops;
273}
274
reed53249782014-10-10 09:09:52 -0700275static int detect_forever_loops(int loops) {
276 // look for a magic run-forever value
277 if (loops < 0) {
278 loops = SK_MaxS32;
279 }
280 return loops;
281}
282
mtklein55b0ffc2014-07-17 08:38:23 -0700283static int clamp_loops(int loops) {
284 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800285 SkDebugf("ERROR: clamping loops from %d to 1. "
286 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700287 return 1;
288 }
289 if (loops > FLAGS_maxLoops) {
290 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
291 return FLAGS_maxLoops;
292 }
293 return loops;
294}
295
tomhudsond968a6f2015-03-26 11:28:06 -0700296static bool write_canvas_png(Target* target, const SkString& filename) {
297
bsalomon6eb03cc2014-08-07 14:28:50 -0700298 if (filename.isEmpty()) {
299 return false;
300 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700301 if (target->getCanvas() &&
302 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700303 return false;
304 }
tomhudsond968a6f2015-03-26 11:28:06 -0700305
bsalomon6eb03cc2014-08-07 14:28:50 -0700306 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700307
308 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700309 return false;
310 }
tomhudsond968a6f2015-03-26 11:28:06 -0700311
bsalomon6eb03cc2014-08-07 14:28:50 -0700312 SkString dir = SkOSPath::Dirname(filename.c_str());
313 if (!sk_mkdir(dir.c_str())) {
314 SkDebugf("Can't make dir %s.\n", dir.c_str());
315 return false;
316 }
317 SkFILEWStream stream(filename.c_str());
318 if (!stream.isValid()) {
319 SkDebugf("Can't write %s.\n", filename.c_str());
320 return false;
321 }
Hal Canarydb683012016-11-23 08:55:18 -0700322 if (!SkEncodeImage(&stream, bmp, SkEncodedImageFormat::kPNG, 100)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700323 SkDebugf("Can't encode a PNG.\n");
324 return false;
325 }
326 return true;
327}
328
329static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700330static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700331 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700332 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700333 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700334 int loops = bench->calculateLoops(FLAGS_loops);
335 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700336 while (bench_plus_overhead < overhead) {
337 if (round++ == FLAGS_maxCalibrationAttempts) {
338 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700339 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700340 return kFailedLoops;
341 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700342 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700343 }
mtklein2069e222014-08-04 13:57:39 -0700344 }
mtkleinf3723212014-06-25 14:08:00 -0700345
mtkleinbb6a0282014-07-01 08:43:42 -0700346 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700347 // We'll pick N to make timer overhead negligible:
348 //
mtkleinbb6a0282014-07-01 08:43:42 -0700349 // overhead
350 // ------------------------- < FLAGS_overheadGoal
351 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700352 //
Hal Canary55325b72017-01-03 10:36:17 -0500353 // where bench_plus_overhead ~=~ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700354 //
355 // Doing some math, we get:
356 //
mtkleinbb6a0282014-07-01 08:43:42 -0700357 // (overhead / FLAGS_overheadGoal) - overhead
358 // ------------------------------------------ < N
359 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700360 //
361 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700362 if (kAutoTuneLoops == loops) {
363 const double numer = overhead / FLAGS_overheadGoal - overhead;
364 const double denom = bench_plus_overhead - overhead;
365 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700366 loops = clamp_loops(loops);
367 } else {
368 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700369 }
mtkleinbb6a0282014-07-01 08:43:42 -0700370
mtkleinbb6a0282014-07-01 08:43:42 -0700371 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700372}
373
cdaltone1b89582015-06-25 19:17:08 -0700374static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700375 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700376 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700377 if (kAutoTuneLoops == loops) {
378 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700379 double elapsed = 0;
380 do {
mtklein527930f2014-11-06 08:04:34 -0800381 if (1<<30 == loops) {
382 // We're about to wrap. Something's wrong with the bench.
383 loops = 0;
384 break;
385 }
mtkleina189ccd2014-07-14 12:28:47 -0700386 loops *= 2;
387 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700388 // _this_ round, not still timing last round.
389 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700390 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700391 }
392 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700393
mtkleina189ccd2014-07-14 12:28:47 -0700394 // We've overshot at least a little. Scale back linearly.
395 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700396 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700397
tomhudsond968a6f2015-03-26 11:28:06 -0700398 // Make sure we're not still timing our calibration.
399 target->fence();
reed53249782014-10-10 09:09:52 -0700400 } else {
401 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700402 }
mtkleinbb6a0282014-07-01 08:43:42 -0700403 // Pretty much the same deal as the calibration: do some warmup to make
404 // sure we're timing steady-state pipelined frames.
Brian Osman01776982017-10-10 15:41:03 -0400405 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700406 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700407 }
mtkleinbb6a0282014-07-01 08:43:42 -0700408
mtkleinbb6a0282014-07-01 08:43:42 -0700409 return loops;
410}
mtkleinbb6a0282014-07-01 08:43:42 -0700411
bsalomonc2553372014-07-22 13:09:05 -0700412#if SK_SUPPORT_GPU
Brian Salomon6405e712017-03-20 08:54:16 -0400413#define kBogusContextType GrContextFactory::kGL_ContextType
csmartdaltone812d492017-02-21 12:36:05 -0700414#define kBogusContextOverrides GrContextFactory::ContextOverrides::kNone
bsalomonc2553372014-07-22 13:09:05 -0700415#else
bsalomon85b4b532016-04-05 11:06:27 -0700416#define kBogusContextType 0
csmartdaltone812d492017-02-21 12:36:05 -0700417#define kBogusContextOverrides 0
mtkleine714e752014-07-31 12:13:48 -0700418#endif
bsalomonc2553372014-07-22 13:09:05 -0700419
svaisanenc47635e2016-01-28 06:05:43 -0800420static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
421
422#if SK_SUPPORT_GPU
423 if (const auto* gpuConfig = config->asConfigGpu()) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400424 if (!FLAGS_gpu) {
425 SkDebugf("Skipping config '%s' as requested.\n", config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800426 return;
Ben Wagner32fa5102017-08-10 21:25:55 -0400427 }
svaisanenc47635e2016-01-28 06:05:43 -0800428
svaisanenc47635e2016-01-28 06:05:43 -0800429 const auto ctxType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700430 const auto ctxOverrides = gpuConfig->getContextOverrides();
svaisanenc47635e2016-01-28 06:05:43 -0800431 const auto sampleCount = gpuConfig->getSamples();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400432 const auto colorType = gpuConfig->getColorType();
433 auto colorSpace = gpuConfig->getColorSpace();
svaisanenc47635e2016-01-28 06:05:43 -0800434
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400435 GrContextFactory factory(grContextOpts);
436 if (const GrContext* ctx = factory.get(ctxType, ctxOverrides)) {
Greg Daniel81e7bf82017-07-19 14:47:42 -0400437 GrPixelConfig grPixConfig = SkImageInfo2GrPixelConfig(colorType, colorSpace,
438 *ctx->caps());
439 int supportedSampleCount = ctx->caps()->getSampleCount(sampleCount, grPixConfig);
440 if (sampleCount != supportedSampleCount) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400441 SkDebugf("Configuration '%s' sample count %d is not a supported sample count.\n",
442 config->getTag().c_str(), sampleCount);
svaisanenc47635e2016-01-28 06:05:43 -0800443 return;
444 }
445 } else {
Ben Wagner32fa5102017-08-10 21:25:55 -0400446 SkDebugf("No context was available matching config '%s'.\n",
447 config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800448 return;
449 }
450
451 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800452 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800453 Benchmark::kGPU_Backend,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400454 colorType,
svaisanenc47635e2016-01-28 06:05:43 -0800455 kPremul_SkAlphaType,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400456 sk_ref_sp(colorSpace),
svaisanenc47635e2016-01-28 06:05:43 -0800457 sampleCount,
458 ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700459 ctxOverrides,
kkinnunend4e1c352016-03-01 23:41:26 -0800460 gpuConfig->getUseDIText()
461 };
svaisanenc47635e2016-01-28 06:05:43 -0800462
463 configs->push_back(target);
464 return;
465 }
466#endif
467
brianosmanb109b8c2016-06-16 13:03:24 -0700468 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
469 if (config->getTag().equals(#name)) { \
Ben Wagner32fa5102017-08-10 21:25:55 -0400470 if (!FLAGS_cpu) { \
471 SkDebugf("Skipping config '%s' as requested.\n", \
472 config->getTag().c_str()); \
473 return; \
474 } \
brianosmanb109b8c2016-06-16 13:03:24 -0700475 Config config = { \
476 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
csmartdaltone812d492017-02-21 12:36:05 -0700477 0, kBogusContextType, kBogusContextOverrides, false \
brianosmanb109b8c2016-06-16 13:03:24 -0700478 }; \
479 configs->push_back(config); \
480 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700481 }
mtkleine714e752014-07-31 12:13:48 -0700482
Ben Wagner32fa5102017-08-10 21:25:55 -0400483 CPU_CONFIG(nonrendering, kNonRendering_Backend,
484 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800485
Mike Reede6befa52017-08-28 11:30:48 -0400486 CPU_CONFIG(a8, kRaster_Backend,
487 kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr)
Ben Wagner32fa5102017-08-10 21:25:55 -0400488 CPU_CONFIG(8888, kRaster_Backend,
489 kN32_SkColorType, kPremul_SkAlphaType, nullptr)
490 CPU_CONFIG(565, kRaster_Backend,
491 kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
492 auto srgbColorSpace = SkColorSpace::MakeSRGB();
493 CPU_CONFIG(srgb, kRaster_Backend,
494 kN32_SkColorType, kPremul_SkAlphaType, srgbColorSpace)
495 auto srgbLinearColorSpace = SkColorSpace::MakeSRGBLinear();
496 CPU_CONFIG(f16, kRaster_Backend,
497 kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbLinearColorSpace)
mtkleinbb6a0282014-07-01 08:43:42 -0700498
svaisanenc47635e2016-01-28 06:05:43 -0800499 #undef CPU_CONFIG
Ben Wagner32fa5102017-08-10 21:25:55 -0400500
501 SkDebugf("Unknown config '%s'.\n", config->getTag().c_str());
mtkleinf3723212014-06-25 14:08:00 -0700502}
503
svaisanenc47635e2016-01-28 06:05:43 -0800504// Append all configs that are enabled and supported.
505void create_configs(SkTArray<Config>* configs) {
506 SkCommandLineConfigArray array;
507 ParseConfigs(FLAGS_config, &array);
508 for (int i = 0; i < array.count(); ++i) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400509 create_config(array[i].get(), configs);
svaisanenc47635e2016-01-28 06:05:43 -0800510 }
Ben Wagner32fa5102017-08-10 21:25:55 -0400511
512 // If no just default configs were requested, then we're okay.
513 if (array.count() == 0 || FLAGS_config.count() == 0 ||
514 // If we've been told to ignore undefined flags, we're okay.
515 FLAGS_undefok ||
516 // Otherwise, make sure that all specified configs have been created.
517 array.count() == configs->count()) {
518 return;
519 }
520 SkDebugf("Invalid --config. Use --undefok to bypass this warning.\n");
521 exit(1);
svaisanenc47635e2016-01-28 06:05:43 -0800522}
523
brianosman9f1f6e22016-09-15 08:33:02 -0700524// disable warning : switch statement contains default but no 'case' labels
525#if defined _WIN32
526#pragma warning ( push )
527#pragma warning ( disable : 4065 )
528#endif
529
halcanary96fcdcc2015-08-27 07:41:13 -0700530// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700531static Target* is_enabled(Benchmark* bench, const Config& config) {
532 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700533 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700534 }
535
reede5ea5002014-09-03 11:54:58 -0700536 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700537 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700538
halcanary96fcdcc2015-08-27 07:41:13 -0700539 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700540
tomhudsond968a6f2015-03-26 11:28:06 -0700541 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700542#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700543 case Benchmark::kGPU_Backend:
544 target = new GPUTarget(config);
545 break;
bsalomonc2553372014-07-22 13:09:05 -0700546#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700547 default:
548 target = new Target(config);
549 break;
550 }
bsalomonc2553372014-07-22 13:09:05 -0700551
tomhudsond968a6f2015-03-26 11:28:06 -0700552 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700553 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700554 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700555 }
556 return target;
557}
558
brianosman9f1f6e22016-09-15 08:33:02 -0700559#if defined _WIN32
560#pragma warning ( pop )
561#endif
562
reed42943c82016-09-12 12:01:44 -0700563static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700564 uint32_t minOutputSize, int* width, int* height) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400565 std::unique_ptr<SkBitmapRegionDecoder> brd(
msarettd1227a72016-05-18 06:23:57 -0700566 SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy));
msarett7f691442015-09-22 11:56:16 -0700567 if (nullptr == brd.get()) {
568 // This is indicates that subset decoding is not supported for a particular image format.
569 return false;
570 }
571
msarett7f691442015-09-22 11:56:16 -0700572 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
573 (uint32_t) brd->height()) {
574 // This indicates that the image is not large enough to decode a
575 // minOutputSize x minOutputSize subset at the given sampleSize.
576 return false;
577 }
578
579 // Set the image width and height. The calling code will use this to choose subsets to decode.
580 *width = brd->width();
581 *height = brd->height();
582 return true;
583}
584
egdaniel3bf92062015-06-26 08:12:46 -0700585static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700586 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700587}
588
fmalita6519c212016-09-14 08:05:17 -0700589static void collect_files(const SkCommandLineFlags::StringArray& paths, const char* ext,
590 SkTArray<SkString>* list) {
591 for (int i = 0; i < paths.count(); ++i) {
592 if (SkStrEndsWith(paths[i], ext)) {
593 list->push_back(SkString(paths[i]));
594 } else {
595 SkOSFile::Iter it(paths[i], ext);
596 SkString path;
597 while (it.next(&path)) {
598 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
599 }
600 }
601 }
602}
603
mtkleine714e752014-07-31 12:13:48 -0700604class BenchmarkStream {
605public:
mtklein92007582014-08-01 07:46:52 -0700606 BenchmarkStream() : fBenches(BenchRegistry::Head())
607 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700608 , fCurrentRecording(0)
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400609 , fCurrentPiping(0)
mtklein92007582014-08-01 07:46:52 -0700610 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800611 , fCurrentSKP(0)
fmalita6519c212016-09-14 08:05:17 -0700612 , fCurrentSVG(0)
msarett95f192d2015-02-13 09:05:41 -0800613 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700614 , fCurrentCodec(0)
msarett84451022016-02-11 06:45:51 -0800615 , fCurrentAndroidCodec(0)
msarett7f691442015-09-22 11:56:16 -0700616 , fCurrentBRDImage(0)
msarett2cee9022016-06-03 08:25:21 -0700617 , fCurrentColorImage(0)
msarett95f192d2015-02-13 09:05:41 -0800618 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800619 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700620 , fCurrentSubsetType(0)
msarett84451022016-02-11 06:45:51 -0800621 , fCurrentSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700622 , fCurrentAnimSKP(0) {
fmalita6519c212016-09-14 08:05:17 -0700623 collect_files(FLAGS_skps, ".skp", &fSKPs);
624 collect_files(FLAGS_svgs, ".svg", &fSVGs);
mtkleine714e752014-07-31 12:13:48 -0700625
mtklein92007582014-08-01 07:46:52 -0700626 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
627 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
628 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
629 exit(1);
630 }
631
632 for (int i = 0; i < FLAGS_scales.count(); i++) {
633 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
634 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
635 exit(1);
636 }
637 }
robertphillips5b693772014-11-21 06:19:36 -0800638
cdalton63a82852015-06-29 14:06:10 -0700639 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
640 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700641 exit(1);
642 }
643
robertphillips5b693772014-11-21 06:19:36 -0800644 if (FLAGS_mpd) {
645 fUseMPDs.push_back() = true;
646 }
mtkleinc751ecb2015-06-15 08:56:38 -0700647 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700648
msarett95f192d2015-02-13 09:05:41 -0800649 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700650 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800651 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800652 }
msarett2cee9022016-06-03 08:25:21 -0700653 if (!CollectImages(FLAGS_colorImages, &fColorImages)) {
654 exit(1);
655 }
mtklein95553d92015-03-12 08:24:21 -0700656
msarett95f192d2015-02-13 09:05:41 -0800657 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700658 fColorTypes.push_back(kN32_SkColorType);
659 if (!FLAGS_simpleCodec) {
660 fColorTypes.push_back(kRGB_565_SkColorType);
661 fColorTypes.push_back(kAlpha_8_SkColorType);
msarett67cb6662016-06-21 08:49:26 -0700662 fColorTypes.push_back(kGray_8_SkColorType);
663 }
mtklein92007582014-08-01 07:46:52 -0700664 }
665
reedca2622b2016-03-18 07:25:55 -0700666 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700667 // Not strictly necessary, as it will be checked again later,
668 // but helps to avoid a lot of pointless work if we're going to skip it.
cdalton91e457d2016-02-17 11:10:16 -0800669 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700670 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700671 }
672
bungemanf93d7112016-09-16 06:24:20 -0700673 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
674 if (!stream) {
mtkleinfd731ce2014-09-10 12:19:30 -0700675 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700676 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700677 }
678
reedca2622b2016-03-18 07:25:55 -0700679 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700680 }
681
fmalita6519c212016-09-14 08:05:17 -0700682 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
683 SkFILEStream stream(path);
684 if (!stream.isValid()) {
685 SkDebugf("Could not read %s.\n", path);
686 return nullptr;
687 }
688
fmalitae1baa7c2016-09-14 12:04:30 -0700689 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
fmalita6519c212016-09-14 08:05:17 -0700690 if (!svgDom) {
691 SkDebugf("Could not parse %s.\n", path);
692 return nullptr;
693 }
694
fmalitae1baa7c2016-09-14 12:04:30 -0700695 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
696 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
697 if (svgDom->containerSize().isEmpty()) {
698 svgDom->setContainerSize(kDefaultContainerSize);
699 }
700
fmalita6519c212016-09-14 08:05:17 -0700701 SkPictureRecorder recorder;
fmalitae1baa7c2016-09-14 12:04:30 -0700702 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
703 svgDom->containerSize().height()));
fmalita6519c212016-09-14 08:05:17 -0700704 return recorder.finishRecordingAsPicture();
705 }
706
mtklein92007582014-08-01 07:46:52 -0700707 Benchmark* next() {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400708 std::unique_ptr<Benchmark> bench;
mtklein65dfd2f2016-02-03 10:40:54 -0800709 do {
710 bench.reset(this->rawNext());
711 if (!bench) {
712 return nullptr;
713 }
714 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
715 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700716 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800717 }
718
719 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700720 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700721 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700722 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700723 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700724 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700725 return bench;
726 }
mtklein92007582014-08-01 07:46:52 -0700727
mtkleine714e752014-07-31 12:13:48 -0700728 while (fGMs) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400729 std::unique_ptr<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700730 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800731 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700732 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700733 fBenchType = "micro";
mtklein18300a32016-03-16 13:53:35 -0700734 return new GMBench(gm.release());
mtkleine714e752014-07-31 12:13:48 -0700735 }
736 }
mtklein92007582014-08-01 07:46:52 -0700737
mtkleinfd731ce2014-09-10 12:19:30 -0700738 // First add all .skps as RecordingBenches.
739 while (fCurrentRecording < fSKPs.count()) {
740 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700741 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
742 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700743 continue;
744 }
745 SkString name = SkOSPath::Basename(path.c_str());
746 fSourceType = "skp";
747 fBenchType = "recording";
Mike Reed90b20052017-03-08 10:39:02 -0500748 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
mtklein051e56d2014-12-04 08:46:51 -0800749 fSKPOps = pic->approximateOpCount();
mtklein8c1a4f82016-08-08 06:56:22 -0700750 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh, FLAGS_lite);
mtkleinfd731ce2014-09-10 12:19:30 -0700751 }
752
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400753 // Add all .skps as PipeBenches.
754 while (fCurrentPiping < fSKPs.count()) {
755 const SkString& path = fSKPs[fCurrentPiping++];
756 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
757 if (!pic) {
758 continue;
759 }
760 SkString name = SkOSPath::Basename(path.c_str());
761 fSourceType = "skp";
762 fBenchType = "piping";
Mike Reed90b20052017-03-08 10:39:02 -0500763 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400764 fSKPOps = pic->approximateOpCount();
765 return new PipingBench(name.c_str(), pic.get());
766 }
767
mtkleinfd731ce2014-09-10 12:19:30 -0700768 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700769 while (fCurrentScale < fScales.count()) {
770 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800771 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700772 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
773 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800774 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700775 continue;
776 }
robertphillips5b693772014-11-21 06:19:36 -0800777
778 while (fCurrentUseMPD < fUseMPDs.count()) {
779 if (FLAGS_bbh) {
780 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
781 SkRTreeFactory factory;
782 SkPictureRecorder recorder;
robertphillips5b693772014-11-21 06:19:36 -0800783 pic->playback(recorder.beginRecording(pic->cullRect().width(),
784 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800785 &factory,
robertphillipsdda54452016-07-13 13:27:16 -0700786 0));
reedca2622b2016-03-18 07:25:55 -0700787 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800788 }
789 SkString name = SkOSPath::Basename(path.c_str());
790 fSourceType = "skp";
791 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700792 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
793 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700794 }
robertphillips5b693772014-11-21 06:19:36 -0800795 fCurrentUseMPD = 0;
796 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700797 }
fmalita6519c212016-09-14 08:05:17 -0700798
799 while (fCurrentSVG++ < fSVGs.count()) {
800 const char* path = fSVGs[fCurrentSVG - 1].c_str();
801 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
802 fSourceType = "svg";
803 fBenchType = "playback";
804 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
805 fScales[fCurrentScale], false, FLAGS_loopSKP);
806 }
807 }
808
mtklein92007582014-08-01 07:46:52 -0700809 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700810 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700811 fCurrentScale++;
812 }
813
joshualitt261c3ad2015-04-27 09:16:57 -0700814 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700815 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700816 while (fCurrentAnimSKP < fSKPs.count()) {
817 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700818 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
819 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700820 fCurrentAnimSKP++;
821 continue;
822 }
823
824 fCurrentAnimSKP++;
825 SkString name = SkOSPath::Basename(path.c_str());
Hal Canary2db83612016-11-04 13:02:54 -0400826 sk_sp<SKPAnimationBench::Animation> animation(
cdalton63a82852015-06-29 14:06:10 -0700827 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
Hal Canary2db83612016-11-04 13:02:54 -0400828 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation.get(),
halcanary385fe4d2015-08-26 13:07:48 -0700829 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700830 }
831 }
832
scroggo60869a42015-04-01 12:09:17 -0700833 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700834 fSourceType = "image";
835 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700836 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800837 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
838 continue;
839 }
bungeman38d909e2016-08-02 14:40:46 -0700840 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400841 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700842 if (!codec) {
843 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700844 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700845 continue;
846 }
scroggo21027992015-04-02 13:22:38 -0700847
scroggo60869a42015-04-01 12:09:17 -0700848 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700849 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700850
msarettc7796b92016-01-07 14:20:20 -0800851 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700852 if (FLAGS_simpleCodec) {
853 if (kUnpremul_SkAlphaType == alphaType) {
854 alphaType = kPremul_SkAlphaType;
855 }
856
857 fCurrentColorType++;
858 } else {
859 switch (alphaType) {
860 case kOpaque_SkAlphaType:
861 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800862 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700863 break;
864 case kUnpremul_SkAlphaType:
865 case kPremul_SkAlphaType:
866 if (0 == fCurrentAlphaType) {
867 // Test unpremul first.
868 alphaType = kUnpremul_SkAlphaType;
869 fCurrentAlphaType++;
870 } else {
871 // Test premul.
872 alphaType = kPremul_SkAlphaType;
873 fCurrentAlphaType = 0;
874 fCurrentColorType++;
875 }
876 break;
877 default:
878 SkASSERT(false);
879 fCurrentColorType++;
880 break;
881 }
scroggo21027992015-04-02 13:22:38 -0700882 }
883
msarettc7796b92016-01-07 14:20:20 -0800884 // Make sure we can decode to this color type and alpha type.
885 SkImageInfo info =
886 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700887 const size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400888 SkAutoMalloc storage(info.computeByteSize(rowBytes));
scroggo21027992015-04-02 13:22:38 -0700889
scroggoeb602a52015-07-09 08:16:03 -0700890 const SkCodec::Result result = codec->getPixels(
Leon Scroggins571b30f2017-07-11 17:35:31 +0000891 info, storage.get(), rowBytes);
scroggo60869a42015-04-01 12:09:17 -0700892 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700893 case SkCodec::kSuccess:
894 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700895 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700896 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700897 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700898 // This is okay. Not all conversions are valid.
899 break;
scroggo60869a42015-04-01 12:09:17 -0700900 default:
901 // This represents some sort of failure.
902 SkASSERT(false);
903 break;
904 }
905 }
906 fCurrentColorType = 0;
907 }
908
msarett84451022016-02-11 06:45:51 -0800909 // Run AndroidCodecBenches
910 const int sampleSizes[] = { 2, 4, 8 };
911 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
912 fSourceType = "image";
913 fBenchType = "skandroidcodec";
914
915 const SkString& path = fImages[fCurrentAndroidCodec];
916 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
917 continue;
918 }
bungeman38d909e2016-08-02 14:40:46 -0700919 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400920 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800921 if (!codec) {
922 // Nothing to time.
923 SkDebugf("Cannot find codec for %s\n", path.c_str());
924 continue;
925 }
926
927 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
928 int sampleSize = sampleSizes[fCurrentSampleSize];
929 fCurrentSampleSize++;
930 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
931 // Avoid benchmarking scaled decodes of already small images.
932 break;
933 }
934
bungeman38d909e2016-08-02 14:40:46 -0700935 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
936 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -0800937 }
938 fCurrentSampleSize = 0;
939 }
940
msarett7f691442015-09-22 11:56:16 -0700941 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700942 // We intend to create benchmarks that model the use cases in
943 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
944 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
945 // the image. For that reason, we will benchmark decodes in five representative locations
946 // in the image. Additionally, this use case utilizes power of two scaling, so we will
947 // test on power of two sample sizes. The output tile is always 512x512, so, when a
948 // sampleSize is used, the size of the subset that is decoded is always
949 // (sampleSize*512)x(sampleSize*512).
950 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700951 // All use cases we are aware of only scale by powers of two.
952 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
953 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800954 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700955 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800956 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700957 fSourceType = "image";
958 fBenchType = "BRD";
959
mtklein6f0ff912016-01-11 09:04:21 -0800960 const SkString& path = fImages[fCurrentBRDImage];
961 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
962 continue;
963 }
scroggo860e8a62015-10-15 07:51:28 -0700964
msarettd1227a72016-05-18 06:23:57 -0700965 while (fCurrentColorType < fColorTypes.count()) {
966 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
967 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700968
bungeman38d909e2016-08-02 14:40:46 -0700969 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -0700970 const SkColorType colorType = fColorTypes[fCurrentColorType];
971 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
972 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -0700973
msarettd1227a72016-05-18 06:23:57 -0700974 int width = 0;
975 int height = 0;
reed42943c82016-09-12 12:01:44 -0700976 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -0700977 &width, &height)) {
978 break;
msarett7f691442015-09-22 11:56:16 -0700979 }
msarettd1227a72016-05-18 06:23:57 -0700980
981 SkString basename = SkOSPath::Basename(path.c_str());
982 SkIRect subset;
983 const uint32_t subsetSize = sampleSize * minOutputSize;
984 switch (currentSubsetType) {
985 case kTopLeft_SubsetType:
986 basename.append("_TopLeft");
987 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
988 break;
989 case kTopRight_SubsetType:
990 basename.append("_TopRight");
991 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
992 subsetSize);
993 break;
994 case kMiddle_SubsetType:
995 basename.append("_Middle");
996 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
997 (height - subsetSize) / 2, subsetSize, subsetSize);
998 break;
999 case kBottomLeft_SubsetType:
1000 basename.append("_BottomLeft");
1001 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
1002 subsetSize);
1003 break;
1004 case kBottomRight_SubsetType:
1005 basename.append("_BottomRight");
1006 subset = SkIRect::MakeXYWH(width - subsetSize,
1007 height - subsetSize, subsetSize, subsetSize);
1008 break;
1009 default:
1010 SkASSERT(false);
1011 }
1012
1013 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
1014 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -07001015 }
msarettd1227a72016-05-18 06:23:57 -07001016 fCurrentSubsetType = 0;
1017 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -07001018 }
msarettd1227a72016-05-18 06:23:57 -07001019 fCurrentSampleSize = 0;
1020 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -07001021 }
msarettd1227a72016-05-18 06:23:57 -07001022 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -07001023 }
1024
msarett2cee9022016-06-03 08:25:21 -07001025 while (fCurrentColorImage < fColorImages.count()) {
1026 fSourceType = "colorimage";
1027 fBenchType = "skcolorcodec";
1028 const SkString& path = fColorImages[fCurrentColorImage];
1029 fCurrentColorImage++;
1030 sk_sp<SkData> encoded = SkData::MakeFromFileName(path.c_str());
1031 if (encoded) {
1032 return new ColorCodecBench(SkOSPath::Basename(path.c_str()).c_str(),
1033 std::move(encoded));
1034 } else {
1035 SkDebugf("Could not read file %s.\n", path.c_str());
1036 }
1037 }
1038
halcanary96fcdcc2015-08-27 07:41:13 -07001039 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001040 }
mtklein92007582014-08-01 07:46:52 -07001041
1042 void fillCurrentOptions(ResultsWriter* log) const {
1043 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001044 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001045 if (0 == strcmp(fSourceType, "skp")) {
1046 log->configOption("clip",
1047 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1048 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001049 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -07001050 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001051 if (fCurrentUseMPD > 0) {
1052 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1053 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1054 }
mtklein92007582014-08-01 07:46:52 -07001055 }
mtklein051e56d2014-12-04 08:46:51 -08001056 if (0 == strcmp(fBenchType, "recording")) {
1057 log->metric("bytes", fSKPBytes);
1058 log->metric("ops", fSKPOps);
1059 }
mtklein92007582014-08-01 07:46:52 -07001060 }
1061
mtkleine714e752014-07-31 12:13:48 -07001062private:
msarettb23e6aa2015-06-09 13:56:10 -07001063 enum SubsetType {
1064 kTopLeft_SubsetType = 0,
1065 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001066 kMiddle_SubsetType = 2,
1067 kBottomLeft_SubsetType = 3,
1068 kBottomRight_SubsetType = 4,
1069 kTranslate_SubsetType = 5,
1070 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001071 kLast_SubsetType = kZoom_SubsetType,
1072 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001073 };
1074
mtkleine714e752014-07-31 12:13:48 -07001075 const BenchRegistry* fBenches;
1076 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001077 SkIRect fClip;
1078 SkTArray<SkScalar> fScales;
1079 SkTArray<SkString> fSKPs;
fmalita6519c212016-09-14 08:05:17 -07001080 SkTArray<SkString> fSVGs;
robertphillips5b693772014-11-21 06:19:36 -08001081 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001082 SkTArray<SkString> fImages;
msarett2cee9022016-06-03 08:25:21 -07001083 SkTArray<SkString> fColorImages;
msarett74deb982015-10-20 16:45:56 -07001084 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001085 SkScalar fZoomMax;
1086 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001087
mtklein051e56d2014-12-04 08:46:51 -08001088 double fSKPBytes, fSKPOps;
1089
mtkleinfd731ce2014-09-10 12:19:30 -07001090 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1091 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1092 int fCurrentRecording;
Mike Reed9cdd2ab2016-10-21 10:43:36 -04001093 int fCurrentPiping;
mtklein92007582014-08-01 07:46:52 -07001094 int fCurrentScale;
1095 int fCurrentSKP;
fmalita6519c212016-09-14 08:05:17 -07001096 int fCurrentSVG;
robertphillips5b693772014-11-21 06:19:36 -08001097 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001098 int fCurrentCodec;
msarett84451022016-02-11 06:45:51 -08001099 int fCurrentAndroidCodec;
msarett7f691442015-09-22 11:56:16 -07001100 int fCurrentBRDImage;
msarett2cee9022016-06-03 08:25:21 -07001101 int fCurrentColorImage;
msarett95f192d2015-02-13 09:05:41 -08001102 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -08001103 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -07001104 int fCurrentSubsetType;
msarett84451022016-02-11 06:45:51 -08001105 int fCurrentSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001106 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001107};
1108
msarettc149f0e2016-01-04 11:35:43 -08001109// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1110// This prints something every once in a while so that it knows we're still working.
1111static void start_keepalive() {
Mike Klein03141d22017-10-30 11:57:15 -04001112 static std::thread* intentionallyLeaked = new std::thread([]{
1113 for (;;) {
1114 static const int kSec = 1200;
1115 #if defined(SK_BUILD_FOR_WIN)
1116 Sleep(kSec * 1000);
1117 #else
1118 sleep(kSec);
1119 #endif
1120 SkDebugf("\nBenchmarks still running...\n");
msarettc149f0e2016-01-04 11:35:43 -08001121 }
Mike Klein03141d22017-10-30 11:57:15 -04001122 });
1123 (void)intentionallyLeaked;
msarettc149f0e2016-01-04 11:35:43 -08001124}
1125
Mike Kleinbe28ee22017-02-06 12:46:20 -05001126int main(int argc, char** argv) {
1127 SkCommandLineFlags::Parse(argc, argv);
Brian Osman53136aa2017-07-20 15:43:35 -04001128
Brian Osmanbc8150f2017-07-24 11:38:01 -04001129 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -04001130
Mike Kleinadacaef2017-02-06 09:26:14 -05001131#if defined(SK_BUILD_FOR_IOS)
1132 cd_Documents();
1133#endif
jcgregorio3b27ade2014-11-13 08:06:40 -08001134 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001135 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001136 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001137
krajcevski69a55602014-08-13 10:46:31 -07001138#if SK_SUPPORT_GPU
csmartdalton008b9d82017-02-22 12:00:42 -07001139 grContextOpts.fGpuPathRenderers = CollectGpuPathRenderersFromFlags();
Chris Dalton7a0ebfc2017-10-13 12:35:50 -06001140 grContextOpts.fAllowPathMaskCaching = FLAGS_cachePathMasks;
Brian Osmanf9810662017-08-30 10:02:10 -04001141 grContextOpts.fExecutor = GpuExecutorForTools();
krajcevski69a55602014-08-13 10:46:31 -07001142#endif
1143
bsalomon06cddec2014-10-24 10:40:50 -07001144 if (FLAGS_veryVerbose) {
1145 FLAGS_verbose = true;
1146 }
1147
bsalomon6eb03cc2014-08-07 14:28:50 -07001148 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001149 FLAGS_samples = 1;
1150 FLAGS_gpuFrameLag = 0;
1151 }
1152
bsalomon6eb03cc2014-08-07 14:28:50 -07001153 if (!FLAGS_writePath.isEmpty()) {
1154 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1155 if (!sk_mkdir(FLAGS_writePath[0])) {
1156 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001157 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001158 }
1159 }
1160
Ben Wagner145dbcd2016-11-03 14:40:50 -04001161 std::unique_ptr<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001162 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001163#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001164 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001165#else
1166 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1167 return 1;
1168#endif
mtklein60317d0f2014-07-14 11:30:37 -07001169 }
mtklein1915b622014-08-20 11:45:00 -07001170
1171 if (1 == FLAGS_properties.count() % 2) {
1172 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1173 return 1;
1174 }
1175 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1176 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1177 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001178
1179 if (1 == FLAGS_key.count() % 2) {
1180 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1181 return 1;
1182 }
1183 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001184 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001185 }
mtklein60317d0f2014-07-14 11:30:37 -07001186
mtkleinf3723212014-06-25 14:08:00 -07001187 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001188 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001189
cdaltone1b89582015-06-25 19:17:08 -07001190 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001191
bsalomon6eb03cc2014-08-07 14:28:50 -07001192 if (kAutoTuneLoops != FLAGS_loops) {
1193 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001194 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001195 SkDebugf("! -> high variance, ? -> moderate variance\n");
1196 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001197 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001198 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001199 } else {
mtkleind75c4662015-04-30 07:11:22 -07001200 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001201 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001202 }
1203
svaisanenc47635e2016-01-28 06:05:43 -08001204 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001205 create_configs(&configs);
1206
joshualitt3ebd0502016-02-09 07:18:08 -08001207#ifdef THERMAL_MANAGER_SUPPORTED
1208 int tmEnabled, tmThreshold, tmSleepTimeMs, tmTimeoutMs;
1209 if (4 != sscanf(FLAGS_useThermalManager[0], "%d,%d,%d,%d",
1210 &tmEnabled, &tmThreshold, &tmSleepTimeMs, &tmTimeoutMs)) {
1211 SkDebugf("Can't parse %s from --useThermalManager.\n", FLAGS_useThermalManager[0]);
1212 exit(1);
1213 }
1214 ThermalManager tm(tmThreshold, tmSleepTimeMs, tmTimeoutMs);
1215#endif
1216
msarettc149f0e2016-01-04 11:35:43 -08001217 if (FLAGS_keepAlive) {
1218 start_keepalive();
1219 }
1220
Yuqian Liba62b4a2016-12-14 13:46:53 -05001221 gSkUseAnalyticAA = FLAGS_analyticAA;
Yuqian Lidf60e362017-07-25 11:26:31 -04001222 gSkUseDeltaAA = FLAGS_deltaAA;
liyuqian38911a72016-10-04 11:23:22 -07001223
Yuqian Lidf60e362017-07-25 11:26:31 -04001224 if (FLAGS_forceDeltaAA) {
1225 gSkForceDeltaAA = true;
1226 }
Yuqian Li550148b2017-01-13 10:13:13 -05001227 if (FLAGS_forceAnalyticAA) {
1228 gSkForceAnalyticAA = true;
1229 }
Mike Reedc928fe22017-06-05 10:20:31 -04001230 if (FLAGS_forceRasterPipeline) {
1231 gSkForceRasterPipelineBlitter = true;
1232 }
Yuqian Li550148b2017-01-13 10:13:13 -05001233
mtkleine070c2b2014-10-14 08:40:43 -07001234 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001235 BenchmarkStream benchStream;
1236 while (Benchmark* b = benchStream.next()) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001237 std::unique_ptr<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001238 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001239 continue;
1240 }
1241
svaisanenc47635e2016-01-28 06:05:43 -08001242 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001243 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001244 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001245 }
egdaniel3bf92062015-06-26 08:12:46 -07001246 for (int i = 0; i < configs.count(); ++i) {
joshualitt3ebd0502016-02-09 07:18:08 -08001247#ifdef THERMAL_MANAGER_SUPPORTED
1248 if (tmEnabled && !tm.coolOffIfNecessary()) {
1249 SkDebugf("Could not cool off, timings will be throttled\n");
1250 }
1251#endif
egdaniel3bf92062015-06-26 08:12:46 -07001252 Target* target = is_enabled(b, configs[i]);
1253 if (!target) {
1254 continue;
1255 }
mtkleinf3723212014-06-25 14:08:00 -07001256
halcanary96fcdcc2015-08-27 07:41:13 -07001257 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001258 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001259 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001260
brianosman7a5ada82016-02-08 13:49:12 -08001261 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001262 SkDebugf("Running %s\t%s\n"
1263 , bench->getUniqueName()
1264 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001265 if (FLAGS_dryRun) {
1266 continue;
1267 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001268 }
1269
Brian Osman5ad87aa2017-09-08 16:05:23 -04001270 TRACE_EVENT2("skia", "Benchmark", "name", TRACE_STR_COPY(bench->getUniqueName()),
1271 "config", TRACE_STR_COPY(config));
Brian Osmanc3cdef52017-08-31 14:09:22 -04001272
egdaniel3bf92062015-06-26 08:12:46 -07001273 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001274 bench->perCanvasPreDraw(canvas);
1275
cdaltone1b89582015-06-25 19:17:08 -07001276 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001277 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001278 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1279 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001280
Leon Scroggins IIIb30d1132017-10-02 09:48:15 -04001281 if (kFailedLoops == loops) {
1282 // Can't be timed. A warning note has already been printed.
1283 cleanup_run(target);
1284 continue;
1285 }
1286
Yuqian Li3528eb32017-09-21 13:23:49 -04001287 if (runs == 0 && FLAGS_ms < 1000) {
1288 // Run the first bench for 1000ms to warm up the nanobench if FLAGS_ms < 1000.
1289 // Otherwise, the first few benches' measurements will be inaccurate.
1290 auto stop = now_ms() + 1000;
1291 do {
1292 time(loops, bench.get(), target);
1293 } while (now_ms() < stop);
1294 }
1295
mtkleinbbba1682015-10-28 11:36:30 -07001296 if (FLAGS_ms) {
1297 samples.reset();
1298 auto stop = now_ms() + FLAGS_ms;
1299 do {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001300 samples.push_back(time(loops, bench.get(), target) / loops);
mtkleinbbba1682015-10-28 11:36:30 -07001301 } while (now_ms() < stop);
1302 } else {
cdaltone1b89582015-06-25 19:17:08 -07001303 samples.reset(FLAGS_samples);
1304 for (int s = 0; s < FLAGS_samples; s++) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001305 samples[s] = time(loops, bench.get(), target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001306 }
cdaltone1b89582015-06-25 19:17:08 -07001307 }
mtkleinf3723212014-06-25 14:08:00 -07001308
joshualitte45c81c2015-12-02 09:05:37 -08001309#if SK_SUPPORT_GPU
1310 SkTArray<SkString> keys;
1311 SkTArray<double> values;
1312 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1313 if (gpuStatsDump) {
1314 // TODO cache stats
1315 bench->getGpuStats(canvas, &keys, &values);
1316 }
1317#endif
1318
robertphillips5b693772014-11-21 06:19:36 -08001319 bench->perCanvasPostDraw(canvas);
1320
egdaniel3bf92062015-06-26 08:12:46 -07001321 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001322 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001323 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001324 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001325 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001326 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001327 }
1328
cdaltone1b89582015-06-25 19:17:08 -07001329 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001330 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001331 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001332 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001333 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001334 log->metric("min_ms", stats.min);
George Burgess IV75b57182016-12-06 10:53:52 -08001335 log->metrics("samples", samples);
joshualitte45c81c2015-12-02 09:05:37 -08001336#if SK_SUPPORT_GPU
1337 if (gpuStatsDump) {
1338 // dump to json, only SKPBench currently returns valid keys / values
1339 SkASSERT(keys.count() == values.count());
1340 for (int i = 0; i < keys.count(); i++) {
1341 log->metric(keys[i].c_str(), values[i]);
1342 }
1343 }
1344#endif
1345
mtkleine070c2b2014-10-14 08:40:43 -07001346 if (runs++ % FLAGS_flushEvery == 0) {
1347 log->flush();
1348 }
mtklein60317d0f2014-07-14 11:30:37 -07001349
bsalomon6eb03cc2014-08-07 14:28:50 -07001350 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001351 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001352 config = ""; // Only print the config if we run the same bench on more than one.
1353 }
mtkleind75c4662015-04-30 07:11:22 -07001354 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1355 , sk_tools::getCurrResidentSetSizeMB()
1356 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001357 , bench->getUniqueName()
1358 , config);
mtkleinf3723212014-06-25 14:08:00 -07001359 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001360 const char* mark = " ";
1361 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1362 if (stddev_percent > 5) mark = "?";
1363 if (stddev_percent > 10) mark = "!";
1364
1365 SkDebugf("%10.2f %s\t%s\t%s\n",
1366 stats.median*1e3, mark, bench->getUniqueName(), config);
Mike Reedc665e5b2017-07-04 22:56:06 -04001367 } else if (FLAGS_csv) {
mtkleinf3723212014-06-25 14:08:00 -07001368 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
Mike Reedc665e5b2017-07-04 22:56:06 -04001369 SkDebugf("%g,%g,%g,%g,%g,%s,%s\n"
1370 , stats.min
1371 , stats.median
1372 , stats.mean
1373 , stats.max
1374 , stddev_percent
1375 , config
1376 , bench->getUniqueName()
1377 );
1378 } else {
1379 const char* format = "%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n";
1380 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1381 SkDebugf(format
mtkleind75c4662015-04-30 07:11:22 -07001382 , sk_tools::getCurrResidentSetSizeMB()
1383 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001384 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001385 , HUMANIZE(stats.min)
1386 , HUMANIZE(stats.median)
1387 , HUMANIZE(stats.mean)
1388 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001389 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001390 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001391 , config
mtklein96289052014-09-10 12:05:59 -07001392 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001393 );
1394 }
joshualitte45c81c2015-12-02 09:05:37 -08001395
bsalomonb12ea412015-02-02 21:19:50 -08001396#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001397 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -04001398 target->dumpStats();
bsalomon06cddec2014-10-24 10:40:50 -07001399 }
1400#endif
joshualitte45c81c2015-12-02 09:05:37 -08001401
cdalton2c56ba52015-06-26 13:32:53 -07001402 if (FLAGS_verbose) {
1403 SkDebugf("Samples: ");
1404 for (int i = 0; i < samples.count(); i++) {
1405 SkDebugf("%s ", HUMANIZE(samples[i]));
1406 }
1407 SkDebugf("%s\n", bench->getUniqueName());
1408 }
egdaniel3bf92062015-06-26 08:12:46 -07001409 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001410 }
mtkleinf3723212014-06-25 14:08:00 -07001411 }
1412
Herb Derby5a523fe2017-01-26 16:48:28 -05001413 SkGraphics::PurgeAllCaches();
1414
mtkleine1091452014-12-04 10:47:02 -08001415 log->bench("memory_usage", 0,0);
1416 log->config("meta");
1417 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1418
mtkleinf3723212014-06-25 14:08:00 -07001419 return 0;
1420}