blob: 76d53718697115d829d411496030430ced9d65db [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"
Brian Salomondcbb9d92017-07-19 10:53:20 -040050#include "ios_utils.h"
mtkleinf3723212014-06-25 14:08:00 -070051
bungeman60e0fee2015-08-26 05:15:46 -070052#include <stdlib.h>
Mike Klein03141d22017-10-30 11:57:15 -040053#include <thread>
bungeman60e0fee2015-08-26 05:15:46 -070054
Mike Reedc928fe22017-06-05 10:20:31 -040055extern bool gSkForceRasterPipelineBlitter;
56
scroggo38ce0a72016-01-07 07:28:47 -080057#ifndef SK_BUILD_FOR_WIN32
58 #include <unistd.h>
59#endif
60
mtkleinbb6a0282014-07-01 08:43:42 -070061#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070062 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070063 #include "GrCaps.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
csmartdalton008b9d82017-02-22 12:00:42 -0700138#if SK_SUPPORT_GPU
139DEFINE_pathrenderer_flag;
140#endif
141
mtkleinbbba1682015-10-28 11:36:30 -0700142static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
143
mtkleinf3723212014-06-25 14:08:00 -0700144static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700145 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800146 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700147}
mtklein55b0ffc2014-07-17 08:38:23 -0700148#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700149
tomhudsond968a6f2015-03-26 11:28:06 -0700150bool Target::init(SkImageInfo info, Benchmark* bench) {
151 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700152 this->surface = SkSurface::MakeRaster(info);
153 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700154 return false;
155 }
156 }
157 return true;
158}
159bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700160 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700161 if (!canvas) {
162 return false;
163 }
Mike Reed12e946b2017-04-17 10:53:29 -0400164 bmp->allocPixels(canvas->imageInfo());
165 if (!canvas->readPixels(*bmp, 0, 0)) {
tomhudsond968a6f2015-03-26 11:28:06 -0700166 SkDebugf("Can't read canvas pixels.\n");
167 return false;
168 }
169 return true;
170}
171
172#if SK_SUPPORT_GPU
173struct GPUTarget : public Target {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400174 explicit GPUTarget(const Config& c) : Target(c) {}
175 ContextInfo contextInfo;
176 std::unique_ptr<GrContextFactory> factory;
tomhudsond968a6f2015-03-26 11:28:06 -0700177
178 void setup() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400179 this->contextInfo.testContext()->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700180 // Make sure we're done with whatever came before.
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400181 this->contextInfo.testContext()->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700182 }
183 void endTiming() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400184 if (this->contextInfo.testContext()) {
185 this->contextInfo.testContext()->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700186 }
187 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400188 void fence() override { this->contextInfo.testContext()->finish(); }
mtkleind75c4662015-04-30 07:11:22 -0700189
cdaltond416a5b2015-06-23 13:23:44 -0700190 bool needsFrameTiming(int* maxFrameLag) const override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400191 if (!this->contextInfo.testContext()->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700192 // Frame lag is unknown.
193 *maxFrameLag = FLAGS_gpuFrameLag;
194 }
195 return true;
196 }
tomhudsond968a6f2015-03-26 11:28:06 -0700197 bool init(SkImageInfo info, Benchmark* bench) override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400198 GrContextOptions options = grContextOpts;
199 bench->modifyGrContextOptions(&options);
200 this->factory.reset(new GrContextFactory(options));
bsalomonafcd7cd2015-08-31 12:39:41 -0700201 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
202 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700203 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400204 this->surface = SkSurface::MakeRenderTarget(
205 this->factory->get(this->config.ctxType, this->config.ctxOverrides),
206 SkBudgeted::kNo, info, this->config.samples, &props);
207 this->contextInfo =
208 this->factory->getContextInfo(this->config.ctxType, this->config.ctxOverrides);
tomhudsond968a6f2015-03-26 11:28:06 -0700209 if (!this->surface.get()) {
210 return false;
211 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400212 if (!this->contextInfo.testContext()->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700213 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800214 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700215 }
tomhudsond968a6f2015-03-26 11:28:06 -0700216 return true;
217 }
218 void fillOptions(ResultsWriter* log) override {
219 const GrGLubyte* version;
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400220 if (this->contextInfo.backend() == kOpenGL_GrBackend) {
221 const GrGLInterface* gl = reinterpret_cast<const GrGLInterface*>(
222 this->contextInfo.testContext()->backendContext());
bsalomonc8699322016-05-11 11:55:36 -0700223 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
224 log->configOption("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700225
bsalomonc8699322016-05-11 11:55:36 -0700226 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
227 log->configOption("GL_RENDERER", (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_VENDOR));
230 log->configOption("GL_VENDOR", (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_SHADING_LANGUAGE_VERSION));
233 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
234 }
tomhudsond968a6f2015-03-26 11:28:06 -0700235 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400236
237 void dumpStats() override {
238 this->contextInfo.grContext()->printCacheStats();
239 this->contextInfo.grContext()->printGpuStats();
240 }
tomhudsond968a6f2015-03-26 11:28:06 -0700241};
mtkleind75c4662015-04-30 07:11:22 -0700242
tomhudsond968a6f2015-03-26 11:28:06 -0700243#endif
244
tomhudson75a0ebb2015-03-27 12:11:44 -0700245static double time(int loops, Benchmark* bench, Target* target) {
246 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700247 if (canvas) {
248 canvas->clear(SK_ColorWHITE);
249 }
joshualitt8a6697a2015-09-30 12:11:07 -0700250 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700251 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700252 canvas = target->beginTiming(canvas);
253 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700254 if (canvas) {
255 canvas->flush();
256 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700257 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700258 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700259 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700260 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700261}
262
mtkleinf3723212014-06-25 14:08:00 -0700263static double estimate_timer_overhead() {
264 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700265 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700266 double start = now_ms();
267 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700268 }
269 return overhead / FLAGS_overheadLoops;
270}
271
reed53249782014-10-10 09:09:52 -0700272static int detect_forever_loops(int loops) {
273 // look for a magic run-forever value
274 if (loops < 0) {
275 loops = SK_MaxS32;
276 }
277 return loops;
278}
279
mtklein55b0ffc2014-07-17 08:38:23 -0700280static int clamp_loops(int loops) {
281 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800282 SkDebugf("ERROR: clamping loops from %d to 1. "
283 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700284 return 1;
285 }
286 if (loops > FLAGS_maxLoops) {
287 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
288 return FLAGS_maxLoops;
289 }
290 return loops;
291}
292
tomhudsond968a6f2015-03-26 11:28:06 -0700293static bool write_canvas_png(Target* target, const SkString& filename) {
294
bsalomon6eb03cc2014-08-07 14:28:50 -0700295 if (filename.isEmpty()) {
296 return false;
297 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700298 if (target->getCanvas() &&
299 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700300 return false;
301 }
tomhudsond968a6f2015-03-26 11:28:06 -0700302
bsalomon6eb03cc2014-08-07 14:28:50 -0700303 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700304
305 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700306 return false;
307 }
tomhudsond968a6f2015-03-26 11:28:06 -0700308
bsalomon6eb03cc2014-08-07 14:28:50 -0700309 SkString dir = SkOSPath::Dirname(filename.c_str());
310 if (!sk_mkdir(dir.c_str())) {
311 SkDebugf("Can't make dir %s.\n", dir.c_str());
312 return false;
313 }
314 SkFILEWStream stream(filename.c_str());
315 if (!stream.isValid()) {
316 SkDebugf("Can't write %s.\n", filename.c_str());
317 return false;
318 }
Hal Canarydb683012016-11-23 08:55:18 -0700319 if (!SkEncodeImage(&stream, bmp, SkEncodedImageFormat::kPNG, 100)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700320 SkDebugf("Can't encode a PNG.\n");
321 return false;
322 }
323 return true;
324}
325
326static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700327static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700328 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700329 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700330 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700331 int loops = bench->calculateLoops(FLAGS_loops);
332 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700333 while (bench_plus_overhead < overhead) {
334 if (round++ == FLAGS_maxCalibrationAttempts) {
335 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700336 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700337 return kFailedLoops;
338 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700339 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700340 }
mtklein2069e222014-08-04 13:57:39 -0700341 }
mtkleinf3723212014-06-25 14:08:00 -0700342
mtkleinbb6a0282014-07-01 08:43:42 -0700343 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700344 // We'll pick N to make timer overhead negligible:
345 //
mtkleinbb6a0282014-07-01 08:43:42 -0700346 // overhead
347 // ------------------------- < FLAGS_overheadGoal
348 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700349 //
Hal Canary55325b72017-01-03 10:36:17 -0500350 // where bench_plus_overhead ~=~ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700351 //
352 // Doing some math, we get:
353 //
mtkleinbb6a0282014-07-01 08:43:42 -0700354 // (overhead / FLAGS_overheadGoal) - overhead
355 // ------------------------------------------ < N
356 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700357 //
358 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700359 if (kAutoTuneLoops == loops) {
360 const double numer = overhead / FLAGS_overheadGoal - overhead;
361 const double denom = bench_plus_overhead - overhead;
362 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700363 loops = clamp_loops(loops);
364 } else {
365 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700366 }
mtkleinbb6a0282014-07-01 08:43:42 -0700367
mtkleinbb6a0282014-07-01 08:43:42 -0700368 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700369}
370
cdaltone1b89582015-06-25 19:17:08 -0700371static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700372 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700373 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700374 if (kAutoTuneLoops == loops) {
375 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700376 double elapsed = 0;
377 do {
mtklein527930f2014-11-06 08:04:34 -0800378 if (1<<30 == loops) {
379 // We're about to wrap. Something's wrong with the bench.
380 loops = 0;
381 break;
382 }
mtkleina189ccd2014-07-14 12:28:47 -0700383 loops *= 2;
384 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700385 // _this_ round, not still timing last round.
386 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700387 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700388 }
389 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700390
mtkleina189ccd2014-07-14 12:28:47 -0700391 // We've overshot at least a little. Scale back linearly.
392 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700393 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700394
tomhudsond968a6f2015-03-26 11:28:06 -0700395 // Make sure we're not still timing our calibration.
396 target->fence();
reed53249782014-10-10 09:09:52 -0700397 } else {
398 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700399 }
mtkleinbb6a0282014-07-01 08:43:42 -0700400 // Pretty much the same deal as the calibration: do some warmup to make
401 // sure we're timing steady-state pipelined frames.
Brian Osman01776982017-10-10 15:41:03 -0400402 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700403 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700404 }
mtkleinbb6a0282014-07-01 08:43:42 -0700405
mtkleinbb6a0282014-07-01 08:43:42 -0700406 return loops;
407}
mtkleinbb6a0282014-07-01 08:43:42 -0700408
bsalomonc2553372014-07-22 13:09:05 -0700409#if SK_SUPPORT_GPU
Brian Salomon6405e712017-03-20 08:54:16 -0400410#define kBogusContextType GrContextFactory::kGL_ContextType
csmartdaltone812d492017-02-21 12:36:05 -0700411#define kBogusContextOverrides GrContextFactory::ContextOverrides::kNone
bsalomonc2553372014-07-22 13:09:05 -0700412#else
bsalomon85b4b532016-04-05 11:06:27 -0700413#define kBogusContextType 0
csmartdaltone812d492017-02-21 12:36:05 -0700414#define kBogusContextOverrides 0
mtkleine714e752014-07-31 12:13:48 -0700415#endif
bsalomonc2553372014-07-22 13:09:05 -0700416
svaisanenc47635e2016-01-28 06:05:43 -0800417static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
418
419#if SK_SUPPORT_GPU
420 if (const auto* gpuConfig = config->asConfigGpu()) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400421 if (!FLAGS_gpu) {
422 SkDebugf("Skipping config '%s' as requested.\n", config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800423 return;
Ben Wagner32fa5102017-08-10 21:25:55 -0400424 }
svaisanenc47635e2016-01-28 06:05:43 -0800425
svaisanenc47635e2016-01-28 06:05:43 -0800426 const auto ctxType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700427 const auto ctxOverrides = gpuConfig->getContextOverrides();
svaisanenc47635e2016-01-28 06:05:43 -0800428 const auto sampleCount = gpuConfig->getSamples();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400429 const auto colorType = gpuConfig->getColorType();
430 auto colorSpace = gpuConfig->getColorSpace();
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());
436 int supportedSampleCount = ctx->caps()->getSampleCount(sampleCount, grPixConfig);
437 if (sampleCount != supportedSampleCount) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400438 SkDebugf("Configuration '%s' sample count %d is not a supported sample count.\n",
439 config->getTag().c_str(), sampleCount);
svaisanenc47635e2016-01-28 06:05:43 -0800440 return;
441 }
442 } else {
Ben Wagner32fa5102017-08-10 21:25:55 -0400443 SkDebugf("No context was available matching config '%s'.\n",
444 config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800445 return;
446 }
447
448 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800449 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800450 Benchmark::kGPU_Backend,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400451 colorType,
svaisanenc47635e2016-01-28 06:05:43 -0800452 kPremul_SkAlphaType,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400453 sk_ref_sp(colorSpace),
svaisanenc47635e2016-01-28 06:05:43 -0800454 sampleCount,
455 ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700456 ctxOverrides,
kkinnunend4e1c352016-03-01 23:41:26 -0800457 gpuConfig->getUseDIText()
458 };
svaisanenc47635e2016-01-28 06:05:43 -0800459
460 configs->push_back(target);
461 return;
462 }
463#endif
464
brianosmanb109b8c2016-06-16 13:03:24 -0700465 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
466 if (config->getTag().equals(#name)) { \
Ben Wagner32fa5102017-08-10 21:25:55 -0400467 if (!FLAGS_cpu) { \
468 SkDebugf("Skipping config '%s' as requested.\n", \
469 config->getTag().c_str()); \
470 return; \
471 } \
brianosmanb109b8c2016-06-16 13:03:24 -0700472 Config config = { \
473 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
csmartdaltone812d492017-02-21 12:36:05 -0700474 0, kBogusContextType, kBogusContextOverrides, false \
brianosmanb109b8c2016-06-16 13:03:24 -0700475 }; \
476 configs->push_back(config); \
477 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700478 }
mtkleine714e752014-07-31 12:13:48 -0700479
Ben Wagner32fa5102017-08-10 21:25:55 -0400480 CPU_CONFIG(nonrendering, kNonRendering_Backend,
481 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800482
Mike Reede6befa52017-08-28 11:30:48 -0400483 CPU_CONFIG(a8, kRaster_Backend,
484 kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr)
Ben Wagner32fa5102017-08-10 21:25:55 -0400485 CPU_CONFIG(8888, kRaster_Backend,
486 kN32_SkColorType, kPremul_SkAlphaType, nullptr)
487 CPU_CONFIG(565, kRaster_Backend,
488 kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
489 auto srgbColorSpace = SkColorSpace::MakeSRGB();
490 CPU_CONFIG(srgb, kRaster_Backend,
491 kN32_SkColorType, kPremul_SkAlphaType, srgbColorSpace)
492 auto srgbLinearColorSpace = SkColorSpace::MakeSRGBLinear();
493 CPU_CONFIG(f16, kRaster_Backend,
494 kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbLinearColorSpace)
mtkleinbb6a0282014-07-01 08:43:42 -0700495
svaisanenc47635e2016-01-28 06:05:43 -0800496 #undef CPU_CONFIG
Ben Wagner32fa5102017-08-10 21:25:55 -0400497
498 SkDebugf("Unknown config '%s'.\n", config->getTag().c_str());
mtkleinf3723212014-06-25 14:08:00 -0700499}
500
svaisanenc47635e2016-01-28 06:05:43 -0800501// Append all configs that are enabled and supported.
502void create_configs(SkTArray<Config>* configs) {
503 SkCommandLineConfigArray array;
504 ParseConfigs(FLAGS_config, &array);
505 for (int i = 0; i < array.count(); ++i) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400506 create_config(array[i].get(), configs);
svaisanenc47635e2016-01-28 06:05:43 -0800507 }
Ben Wagner32fa5102017-08-10 21:25:55 -0400508
509 // If no just default configs were requested, then we're okay.
510 if (array.count() == 0 || FLAGS_config.count() == 0 ||
511 // If we've been told to ignore undefined flags, we're okay.
512 FLAGS_undefok ||
513 // Otherwise, make sure that all specified configs have been created.
514 array.count() == configs->count()) {
515 return;
516 }
517 SkDebugf("Invalid --config. Use --undefok to bypass this warning.\n");
518 exit(1);
svaisanenc47635e2016-01-28 06:05:43 -0800519}
520
brianosman9f1f6e22016-09-15 08:33:02 -0700521// disable warning : switch statement contains default but no 'case' labels
522#if defined _WIN32
523#pragma warning ( push )
524#pragma warning ( disable : 4065 )
525#endif
526
halcanary96fcdcc2015-08-27 07:41:13 -0700527// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700528static Target* is_enabled(Benchmark* bench, const Config& config) {
529 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700530 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700531 }
532
reede5ea5002014-09-03 11:54:58 -0700533 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700534 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700535
halcanary96fcdcc2015-08-27 07:41:13 -0700536 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700537
tomhudsond968a6f2015-03-26 11:28:06 -0700538 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700539#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700540 case Benchmark::kGPU_Backend:
541 target = new GPUTarget(config);
542 break;
bsalomonc2553372014-07-22 13:09:05 -0700543#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700544 default:
545 target = new Target(config);
546 break;
547 }
bsalomonc2553372014-07-22 13:09:05 -0700548
tomhudsond968a6f2015-03-26 11:28:06 -0700549 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700550 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700551 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700552 }
553 return target;
554}
555
brianosman9f1f6e22016-09-15 08:33:02 -0700556#if defined _WIN32
557#pragma warning ( pop )
558#endif
559
reed42943c82016-09-12 12:01:44 -0700560static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700561 uint32_t minOutputSize, int* width, int* height) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400562 std::unique_ptr<SkBitmapRegionDecoder> brd(
msarettd1227a72016-05-18 06:23:57 -0700563 SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy));
msarett7f691442015-09-22 11:56:16 -0700564 if (nullptr == brd.get()) {
565 // This is indicates that subset decoding is not supported for a particular image format.
566 return false;
567 }
568
msarett7f691442015-09-22 11:56:16 -0700569 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
570 (uint32_t) brd->height()) {
571 // This indicates that the image is not large enough to decode a
572 // minOutputSize x minOutputSize subset at the given sampleSize.
573 return false;
574 }
575
576 // Set the image width and height. The calling code will use this to choose subsets to decode.
577 *width = brd->width();
578 *height = brd->height();
579 return true;
580}
581
egdaniel3bf92062015-06-26 08:12:46 -0700582static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700583 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700584}
585
fmalita6519c212016-09-14 08:05:17 -0700586static void collect_files(const SkCommandLineFlags::StringArray& paths, const char* ext,
587 SkTArray<SkString>* list) {
588 for (int i = 0; i < paths.count(); ++i) {
589 if (SkStrEndsWith(paths[i], ext)) {
590 list->push_back(SkString(paths[i]));
591 } else {
592 SkOSFile::Iter it(paths[i], ext);
593 SkString path;
594 while (it.next(&path)) {
595 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
596 }
597 }
598 }
599}
600
mtkleine714e752014-07-31 12:13:48 -0700601class BenchmarkStream {
602public:
mtklein92007582014-08-01 07:46:52 -0700603 BenchmarkStream() : fBenches(BenchRegistry::Head())
604 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700605 , fCurrentRecording(0)
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400606 , fCurrentPiping(0)
Mike Reede45ff462017-12-06 10:47:03 -0500607 , fCurrentDeserialPicture(0)
mtklein92007582014-08-01 07:46:52 -0700608 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800609 , fCurrentSKP(0)
fmalita6519c212016-09-14 08:05:17 -0700610 , fCurrentSVG(0)
msarett95f192d2015-02-13 09:05:41 -0800611 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700612 , fCurrentCodec(0)
msarett84451022016-02-11 06:45:51 -0800613 , fCurrentAndroidCodec(0)
msarett7f691442015-09-22 11:56:16 -0700614 , fCurrentBRDImage(0)
msarett2cee9022016-06-03 08:25:21 -0700615 , fCurrentColorImage(0)
msarett95f192d2015-02-13 09:05:41 -0800616 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800617 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700618 , fCurrentSubsetType(0)
msarett84451022016-02-11 06:45:51 -0800619 , fCurrentSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700620 , fCurrentAnimSKP(0) {
fmalita6519c212016-09-14 08:05:17 -0700621 collect_files(FLAGS_skps, ".skp", &fSKPs);
622 collect_files(FLAGS_svgs, ".svg", &fSVGs);
mtkleine714e752014-07-31 12:13:48 -0700623
mtklein92007582014-08-01 07:46:52 -0700624 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
625 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
626 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
627 exit(1);
628 }
629
630 for (int i = 0; i < FLAGS_scales.count(); i++) {
631 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
632 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
633 exit(1);
634 }
635 }
robertphillips5b693772014-11-21 06:19:36 -0800636
cdalton63a82852015-06-29 14:06:10 -0700637 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
638 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700639 exit(1);
640 }
641
robertphillips5b693772014-11-21 06:19:36 -0800642 if (FLAGS_mpd) {
643 fUseMPDs.push_back() = true;
644 }
mtkleinc751ecb2015-06-15 08:56:38 -0700645 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700646
msarett95f192d2015-02-13 09:05:41 -0800647 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700648 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800649 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800650 }
msarett2cee9022016-06-03 08:25:21 -0700651 if (!CollectImages(FLAGS_colorImages, &fColorImages)) {
652 exit(1);
653 }
mtklein95553d92015-03-12 08:24:21 -0700654
msarett95f192d2015-02-13 09:05:41 -0800655 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700656 fColorTypes.push_back(kN32_SkColorType);
657 if (!FLAGS_simpleCodec) {
658 fColorTypes.push_back(kRGB_565_SkColorType);
659 fColorTypes.push_back(kAlpha_8_SkColorType);
msarett67cb6662016-06-21 08:49:26 -0700660 fColorTypes.push_back(kGray_8_SkColorType);
661 }
mtklein92007582014-08-01 07:46:52 -0700662 }
663
reedca2622b2016-03-18 07:25:55 -0700664 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700665 // Not strictly necessary, as it will be checked again later,
666 // but helps to avoid a lot of pointless work if we're going to skip it.
cdalton91e457d2016-02-17 11:10:16 -0800667 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700668 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700669 }
670
bungemanf93d7112016-09-16 06:24:20 -0700671 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
672 if (!stream) {
mtkleinfd731ce2014-09-10 12:19:30 -0700673 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700674 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700675 }
676
reedca2622b2016-03-18 07:25:55 -0700677 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700678 }
679
fmalita6519c212016-09-14 08:05:17 -0700680 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
681 SkFILEStream stream(path);
682 if (!stream.isValid()) {
683 SkDebugf("Could not read %s.\n", path);
684 return nullptr;
685 }
686
fmalitae1baa7c2016-09-14 12:04:30 -0700687 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
fmalita6519c212016-09-14 08:05:17 -0700688 if (!svgDom) {
689 SkDebugf("Could not parse %s.\n", path);
690 return nullptr;
691 }
692
fmalitae1baa7c2016-09-14 12:04:30 -0700693 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
694 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
695 if (svgDom->containerSize().isEmpty()) {
696 svgDom->setContainerSize(kDefaultContainerSize);
697 }
698
fmalita6519c212016-09-14 08:05:17 -0700699 SkPictureRecorder recorder;
fmalitae1baa7c2016-09-14 12:04:30 -0700700 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
701 svgDom->containerSize().height()));
fmalita6519c212016-09-14 08:05:17 -0700702 return recorder.finishRecordingAsPicture();
703 }
704
mtklein92007582014-08-01 07:46:52 -0700705 Benchmark* next() {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400706 std::unique_ptr<Benchmark> bench;
mtklein65dfd2f2016-02-03 10:40:54 -0800707 do {
708 bench.reset(this->rawNext());
709 if (!bench) {
710 return nullptr;
711 }
712 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
713 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700714 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800715 }
716
717 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700718 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700719 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700720 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700721 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700722 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700723 return bench;
724 }
mtklein92007582014-08-01 07:46:52 -0700725
mtkleine714e752014-07-31 12:13:48 -0700726 while (fGMs) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400727 std::unique_ptr<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700728 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800729 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700730 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700731 fBenchType = "micro";
mtklein18300a32016-03-16 13:53:35 -0700732 return new GMBench(gm.release());
mtkleine714e752014-07-31 12:13:48 -0700733 }
734 }
mtklein92007582014-08-01 07:46:52 -0700735
mtkleinfd731ce2014-09-10 12:19:30 -0700736 // First add all .skps as RecordingBenches.
737 while (fCurrentRecording < fSKPs.count()) {
738 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700739 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
740 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700741 continue;
742 }
743 SkString name = SkOSPath::Basename(path.c_str());
744 fSourceType = "skp";
745 fBenchType = "recording";
Mike Reed90b20052017-03-08 10:39:02 -0500746 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
mtklein051e56d2014-12-04 08:46:51 -0800747 fSKPOps = pic->approximateOpCount();
mtklein8c1a4f82016-08-08 06:56:22 -0700748 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh, FLAGS_lite);
mtkleinfd731ce2014-09-10 12:19:30 -0700749 }
750
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400751 // Add all .skps as PipeBenches.
752 while (fCurrentPiping < fSKPs.count()) {
753 const SkString& path = fSKPs[fCurrentPiping++];
754 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
755 if (!pic) {
756 continue;
757 }
758 SkString name = SkOSPath::Basename(path.c_str());
759 fSourceType = "skp";
760 fBenchType = "piping";
Mike Reed90b20052017-03-08 10:39:02 -0500761 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400762 fSKPOps = pic->approximateOpCount();
763 return new PipingBench(name.c_str(), pic.get());
764 }
765
Mike Reede45ff462017-12-06 10:47:03 -0500766 // Add all .skps as DeserializePictureBenchs.
767 while (fCurrentDeserialPicture < fSKPs.count()) {
768 const SkString& path = fSKPs[fCurrentDeserialPicture++];
769 sk_sp<SkData> data = SkData::MakeFromFileName(path.c_str());
770 if (!data) {
771 continue;
772 }
773 SkString name = SkOSPath::Basename(path.c_str());
774 fSourceType = "skp";
775 fBenchType = "deserial";
776 fSKPBytes = static_cast<double>(data->size());
777 fSKPOps = 0;
778 return new DeserializePictureBench(name.c_str(), std::move(data));
779 }
780
mtkleinfd731ce2014-09-10 12:19:30 -0700781 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700782 while (fCurrentScale < fScales.count()) {
783 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800784 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700785 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
786 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800787 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700788 continue;
789 }
robertphillips5b693772014-11-21 06:19:36 -0800790
791 while (fCurrentUseMPD < fUseMPDs.count()) {
792 if (FLAGS_bbh) {
793 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
794 SkRTreeFactory factory;
795 SkPictureRecorder recorder;
robertphillips5b693772014-11-21 06:19:36 -0800796 pic->playback(recorder.beginRecording(pic->cullRect().width(),
797 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800798 &factory,
robertphillipsdda54452016-07-13 13:27:16 -0700799 0));
reedca2622b2016-03-18 07:25:55 -0700800 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800801 }
802 SkString name = SkOSPath::Basename(path.c_str());
803 fSourceType = "skp";
804 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700805 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
806 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700807 }
robertphillips5b693772014-11-21 06:19:36 -0800808 fCurrentUseMPD = 0;
809 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700810 }
fmalita6519c212016-09-14 08:05:17 -0700811
812 while (fCurrentSVG++ < fSVGs.count()) {
813 const char* path = fSVGs[fCurrentSVG - 1].c_str();
814 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
815 fSourceType = "svg";
816 fBenchType = "playback";
817 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
818 fScales[fCurrentScale], false, FLAGS_loopSKP);
819 }
820 }
821
mtklein92007582014-08-01 07:46:52 -0700822 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700823 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700824 fCurrentScale++;
825 }
826
joshualitt261c3ad2015-04-27 09:16:57 -0700827 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700828 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700829 while (fCurrentAnimSKP < fSKPs.count()) {
830 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700831 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
832 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700833 fCurrentAnimSKP++;
834 continue;
835 }
836
837 fCurrentAnimSKP++;
838 SkString name = SkOSPath::Basename(path.c_str());
Hal Canary2db83612016-11-04 13:02:54 -0400839 sk_sp<SKPAnimationBench::Animation> animation(
cdalton63a82852015-06-29 14:06:10 -0700840 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
Hal Canary2db83612016-11-04 13:02:54 -0400841 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation.get(),
halcanary385fe4d2015-08-26 13:07:48 -0700842 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700843 }
844 }
845
scroggo60869a42015-04-01 12:09:17 -0700846 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700847 fSourceType = "image";
848 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700849 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800850 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
851 continue;
852 }
bungeman38d909e2016-08-02 14:40:46 -0700853 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400854 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700855 if (!codec) {
856 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700857 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700858 continue;
859 }
scroggo21027992015-04-02 13:22:38 -0700860
scroggo60869a42015-04-01 12:09:17 -0700861 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700862 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700863
msarettc7796b92016-01-07 14:20:20 -0800864 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700865 if (FLAGS_simpleCodec) {
866 if (kUnpremul_SkAlphaType == alphaType) {
867 alphaType = kPremul_SkAlphaType;
868 }
869
870 fCurrentColorType++;
871 } else {
872 switch (alphaType) {
873 case kOpaque_SkAlphaType:
874 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800875 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700876 break;
877 case kUnpremul_SkAlphaType:
878 case kPremul_SkAlphaType:
879 if (0 == fCurrentAlphaType) {
880 // Test unpremul first.
881 alphaType = kUnpremul_SkAlphaType;
882 fCurrentAlphaType++;
883 } else {
884 // Test premul.
885 alphaType = kPremul_SkAlphaType;
886 fCurrentAlphaType = 0;
887 fCurrentColorType++;
888 }
889 break;
890 default:
891 SkASSERT(false);
892 fCurrentColorType++;
893 break;
894 }
scroggo21027992015-04-02 13:22:38 -0700895 }
896
msarettc7796b92016-01-07 14:20:20 -0800897 // Make sure we can decode to this color type and alpha type.
898 SkImageInfo info =
899 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700900 const size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400901 SkAutoMalloc storage(info.computeByteSize(rowBytes));
scroggo21027992015-04-02 13:22:38 -0700902
scroggoeb602a52015-07-09 08:16:03 -0700903 const SkCodec::Result result = codec->getPixels(
Leon Scroggins571b30f2017-07-11 17:35:31 +0000904 info, storage.get(), rowBytes);
scroggo60869a42015-04-01 12:09:17 -0700905 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700906 case SkCodec::kSuccess:
907 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700908 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700909 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700910 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700911 // This is okay. Not all conversions are valid.
912 break;
scroggo60869a42015-04-01 12:09:17 -0700913 default:
914 // This represents some sort of failure.
915 SkASSERT(false);
916 break;
917 }
918 }
919 fCurrentColorType = 0;
920 }
921
msarett84451022016-02-11 06:45:51 -0800922 // Run AndroidCodecBenches
923 const int sampleSizes[] = { 2, 4, 8 };
924 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
925 fSourceType = "image";
926 fBenchType = "skandroidcodec";
927
928 const SkString& path = fImages[fCurrentAndroidCodec];
929 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
930 continue;
931 }
bungeman38d909e2016-08-02 14:40:46 -0700932 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400933 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800934 if (!codec) {
935 // Nothing to time.
936 SkDebugf("Cannot find codec for %s\n", path.c_str());
937 continue;
938 }
939
940 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
941 int sampleSize = sampleSizes[fCurrentSampleSize];
942 fCurrentSampleSize++;
943 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
944 // Avoid benchmarking scaled decodes of already small images.
945 break;
946 }
947
bungeman38d909e2016-08-02 14:40:46 -0700948 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
949 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -0800950 }
951 fCurrentSampleSize = 0;
952 }
953
msarett7f691442015-09-22 11:56:16 -0700954 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700955 // We intend to create benchmarks that model the use cases in
956 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
957 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
958 // the image. For that reason, we will benchmark decodes in five representative locations
959 // in the image. Additionally, this use case utilizes power of two scaling, so we will
960 // test on power of two sample sizes. The output tile is always 512x512, so, when a
961 // sampleSize is used, the size of the subset that is decoded is always
962 // (sampleSize*512)x(sampleSize*512).
963 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700964 // All use cases we are aware of only scale by powers of two.
965 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
966 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800967 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700968 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800969 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700970 fSourceType = "image";
971 fBenchType = "BRD";
972
mtklein6f0ff912016-01-11 09:04:21 -0800973 const SkString& path = fImages[fCurrentBRDImage];
974 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
975 continue;
976 }
scroggo860e8a62015-10-15 07:51:28 -0700977
msarettd1227a72016-05-18 06:23:57 -0700978 while (fCurrentColorType < fColorTypes.count()) {
979 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
980 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700981
bungeman38d909e2016-08-02 14:40:46 -0700982 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -0700983 const SkColorType colorType = fColorTypes[fCurrentColorType];
984 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
985 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -0700986
msarettd1227a72016-05-18 06:23:57 -0700987 int width = 0;
988 int height = 0;
reed42943c82016-09-12 12:01:44 -0700989 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -0700990 &width, &height)) {
991 break;
msarett7f691442015-09-22 11:56:16 -0700992 }
msarettd1227a72016-05-18 06:23:57 -0700993
994 SkString basename = SkOSPath::Basename(path.c_str());
995 SkIRect subset;
996 const uint32_t subsetSize = sampleSize * minOutputSize;
997 switch (currentSubsetType) {
998 case kTopLeft_SubsetType:
999 basename.append("_TopLeft");
1000 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
1001 break;
1002 case kTopRight_SubsetType:
1003 basename.append("_TopRight");
1004 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
1005 subsetSize);
1006 break;
1007 case kMiddle_SubsetType:
1008 basename.append("_Middle");
1009 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
1010 (height - subsetSize) / 2, subsetSize, subsetSize);
1011 break;
1012 case kBottomLeft_SubsetType:
1013 basename.append("_BottomLeft");
1014 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
1015 subsetSize);
1016 break;
1017 case kBottomRight_SubsetType:
1018 basename.append("_BottomRight");
1019 subset = SkIRect::MakeXYWH(width - subsetSize,
1020 height - subsetSize, subsetSize, subsetSize);
1021 break;
1022 default:
1023 SkASSERT(false);
1024 }
1025
1026 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
1027 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -07001028 }
msarettd1227a72016-05-18 06:23:57 -07001029 fCurrentSubsetType = 0;
1030 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -07001031 }
msarettd1227a72016-05-18 06:23:57 -07001032 fCurrentSampleSize = 0;
1033 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -07001034 }
msarettd1227a72016-05-18 06:23:57 -07001035 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -07001036 }
1037
msarett2cee9022016-06-03 08:25:21 -07001038 while (fCurrentColorImage < fColorImages.count()) {
1039 fSourceType = "colorimage";
1040 fBenchType = "skcolorcodec";
1041 const SkString& path = fColorImages[fCurrentColorImage];
1042 fCurrentColorImage++;
1043 sk_sp<SkData> encoded = SkData::MakeFromFileName(path.c_str());
1044 if (encoded) {
1045 return new ColorCodecBench(SkOSPath::Basename(path.c_str()).c_str(),
1046 std::move(encoded));
1047 } else {
1048 SkDebugf("Could not read file %s.\n", path.c_str());
1049 }
1050 }
1051
halcanary96fcdcc2015-08-27 07:41:13 -07001052 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001053 }
mtklein92007582014-08-01 07:46:52 -07001054
1055 void fillCurrentOptions(ResultsWriter* log) const {
1056 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001057 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001058 if (0 == strcmp(fSourceType, "skp")) {
1059 log->configOption("clip",
1060 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1061 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001062 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -07001063 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001064 if (fCurrentUseMPD > 0) {
1065 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1066 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1067 }
mtklein92007582014-08-01 07:46:52 -07001068 }
mtklein051e56d2014-12-04 08:46:51 -08001069 if (0 == strcmp(fBenchType, "recording")) {
1070 log->metric("bytes", fSKPBytes);
1071 log->metric("ops", fSKPOps);
1072 }
mtklein92007582014-08-01 07:46:52 -07001073 }
1074
mtkleine714e752014-07-31 12:13:48 -07001075private:
msarettb23e6aa2015-06-09 13:56:10 -07001076 enum SubsetType {
1077 kTopLeft_SubsetType = 0,
1078 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001079 kMiddle_SubsetType = 2,
1080 kBottomLeft_SubsetType = 3,
1081 kBottomRight_SubsetType = 4,
1082 kTranslate_SubsetType = 5,
1083 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001084 kLast_SubsetType = kZoom_SubsetType,
1085 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001086 };
1087
mtkleine714e752014-07-31 12:13:48 -07001088 const BenchRegistry* fBenches;
1089 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001090 SkIRect fClip;
1091 SkTArray<SkScalar> fScales;
1092 SkTArray<SkString> fSKPs;
fmalita6519c212016-09-14 08:05:17 -07001093 SkTArray<SkString> fSVGs;
robertphillips5b693772014-11-21 06:19:36 -08001094 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001095 SkTArray<SkString> fImages;
msarett2cee9022016-06-03 08:25:21 -07001096 SkTArray<SkString> fColorImages;
msarett74deb982015-10-20 16:45:56 -07001097 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001098 SkScalar fZoomMax;
1099 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001100
mtklein051e56d2014-12-04 08:46:51 -08001101 double fSKPBytes, fSKPOps;
1102
mtkleinfd731ce2014-09-10 12:19:30 -07001103 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1104 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1105 int fCurrentRecording;
Mike Reed9cdd2ab2016-10-21 10:43:36 -04001106 int fCurrentPiping;
Mike Reede45ff462017-12-06 10:47:03 -05001107 int fCurrentDeserialPicture;
mtklein92007582014-08-01 07:46:52 -07001108 int fCurrentScale;
1109 int fCurrentSKP;
fmalita6519c212016-09-14 08:05:17 -07001110 int fCurrentSVG;
robertphillips5b693772014-11-21 06:19:36 -08001111 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001112 int fCurrentCodec;
msarett84451022016-02-11 06:45:51 -08001113 int fCurrentAndroidCodec;
msarett7f691442015-09-22 11:56:16 -07001114 int fCurrentBRDImage;
msarett2cee9022016-06-03 08:25:21 -07001115 int fCurrentColorImage;
msarett95f192d2015-02-13 09:05:41 -08001116 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -08001117 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -07001118 int fCurrentSubsetType;
msarett84451022016-02-11 06:45:51 -08001119 int fCurrentSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001120 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001121};
1122
msarettc149f0e2016-01-04 11:35:43 -08001123// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1124// This prints something every once in a while so that it knows we're still working.
1125static void start_keepalive() {
Mike Klein03141d22017-10-30 11:57:15 -04001126 static std::thread* intentionallyLeaked = new std::thread([]{
1127 for (;;) {
1128 static const int kSec = 1200;
1129 #if defined(SK_BUILD_FOR_WIN)
1130 Sleep(kSec * 1000);
1131 #else
1132 sleep(kSec);
1133 #endif
1134 SkDebugf("\nBenchmarks still running...\n");
msarettc149f0e2016-01-04 11:35:43 -08001135 }
Mike Klein03141d22017-10-30 11:57:15 -04001136 });
1137 (void)intentionallyLeaked;
msarettc149f0e2016-01-04 11:35:43 -08001138}
1139
Mike Kleinbe28ee22017-02-06 12:46:20 -05001140int main(int argc, char** argv) {
1141 SkCommandLineFlags::Parse(argc, argv);
Brian Osman53136aa2017-07-20 15:43:35 -04001142
Brian Osmanbc8150f2017-07-24 11:38:01 -04001143 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -04001144
Mike Kleinadacaef2017-02-06 09:26:14 -05001145#if defined(SK_BUILD_FOR_IOS)
1146 cd_Documents();
1147#endif
jcgregorio3b27ade2014-11-13 08:06:40 -08001148 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001149 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001150 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001151
krajcevski69a55602014-08-13 10:46:31 -07001152#if SK_SUPPORT_GPU
csmartdalton008b9d82017-02-22 12:00:42 -07001153 grContextOpts.fGpuPathRenderers = CollectGpuPathRenderersFromFlags();
Chris Dalton7a0ebfc2017-10-13 12:35:50 -06001154 grContextOpts.fAllowPathMaskCaching = FLAGS_cachePathMasks;
Brian Osmanf9810662017-08-30 10:02:10 -04001155 grContextOpts.fExecutor = GpuExecutorForTools();
krajcevski69a55602014-08-13 10:46:31 -07001156#endif
1157
bsalomon06cddec2014-10-24 10:40:50 -07001158 if (FLAGS_veryVerbose) {
1159 FLAGS_verbose = true;
1160 }
1161
bsalomon6eb03cc2014-08-07 14:28:50 -07001162 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001163 FLAGS_samples = 1;
1164 FLAGS_gpuFrameLag = 0;
1165 }
1166
bsalomon6eb03cc2014-08-07 14:28:50 -07001167 if (!FLAGS_writePath.isEmpty()) {
1168 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1169 if (!sk_mkdir(FLAGS_writePath[0])) {
1170 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001171 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001172 }
1173 }
1174
Ben Wagner145dbcd2016-11-03 14:40:50 -04001175 std::unique_ptr<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001176 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001177#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001178 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001179#else
1180 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1181 return 1;
1182#endif
mtklein60317d0f2014-07-14 11:30:37 -07001183 }
mtklein1915b622014-08-20 11:45:00 -07001184
1185 if (1 == FLAGS_properties.count() % 2) {
1186 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1187 return 1;
1188 }
1189 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1190 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1191 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001192
1193 if (1 == FLAGS_key.count() % 2) {
1194 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1195 return 1;
1196 }
1197 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001198 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001199 }
mtklein60317d0f2014-07-14 11:30:37 -07001200
mtkleinf3723212014-06-25 14:08:00 -07001201 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001202 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001203
cdaltone1b89582015-06-25 19:17:08 -07001204 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001205
bsalomon6eb03cc2014-08-07 14:28:50 -07001206 if (kAutoTuneLoops != FLAGS_loops) {
1207 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001208 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001209 SkDebugf("! -> high variance, ? -> moderate variance\n");
1210 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001211 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001212 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001213 } else {
mtkleind75c4662015-04-30 07:11:22 -07001214 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001215 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001216 }
1217
svaisanenc47635e2016-01-28 06:05:43 -08001218 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001219 create_configs(&configs);
1220
msarettc149f0e2016-01-04 11:35:43 -08001221 if (FLAGS_keepAlive) {
1222 start_keepalive();
1223 }
1224
Yuqian Liba62b4a2016-12-14 13:46:53 -05001225 gSkUseAnalyticAA = FLAGS_analyticAA;
Yuqian Lidf60e362017-07-25 11:26:31 -04001226 gSkUseDeltaAA = FLAGS_deltaAA;
liyuqian38911a72016-10-04 11:23:22 -07001227
Yuqian Lidf60e362017-07-25 11:26:31 -04001228 if (FLAGS_forceDeltaAA) {
1229 gSkForceDeltaAA = true;
1230 }
Yuqian Li550148b2017-01-13 10:13:13 -05001231 if (FLAGS_forceAnalyticAA) {
1232 gSkForceAnalyticAA = true;
1233 }
Mike Reedc928fe22017-06-05 10:20:31 -04001234 if (FLAGS_forceRasterPipeline) {
1235 gSkForceRasterPipelineBlitter = true;
1236 }
Yuqian Li550148b2017-01-13 10:13:13 -05001237
mtkleine070c2b2014-10-14 08:40:43 -07001238 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001239 BenchmarkStream benchStream;
1240 while (Benchmark* b = benchStream.next()) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001241 std::unique_ptr<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001242 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001243 continue;
1244 }
1245
svaisanenc47635e2016-01-28 06:05:43 -08001246 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001247 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001248 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001249 }
egdaniel3bf92062015-06-26 08:12:46 -07001250 for (int i = 0; i < configs.count(); ++i) {
1251 Target* target = is_enabled(b, configs[i]);
1252 if (!target) {
1253 continue;
1254 }
mtkleinf3723212014-06-25 14:08:00 -07001255
halcanary96fcdcc2015-08-27 07:41:13 -07001256 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001257 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001258 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001259
brianosman7a5ada82016-02-08 13:49:12 -08001260 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001261 SkDebugf("Running %s\t%s\n"
1262 , bench->getUniqueName()
1263 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001264 if (FLAGS_dryRun) {
1265 continue;
1266 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001267 }
1268
Brian Osman5ad87aa2017-09-08 16:05:23 -04001269 TRACE_EVENT2("skia", "Benchmark", "name", TRACE_STR_COPY(bench->getUniqueName()),
1270 "config", TRACE_STR_COPY(config));
Brian Osmanc3cdef52017-08-31 14:09:22 -04001271
egdaniel3bf92062015-06-26 08:12:46 -07001272 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001273 bench->perCanvasPreDraw(canvas);
1274
cdaltone1b89582015-06-25 19:17:08 -07001275 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001276 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001277 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1278 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001279
Leon Scroggins IIIb30d1132017-10-02 09:48:15 -04001280 if (kFailedLoops == loops) {
1281 // Can't be timed. A warning note has already been printed.
1282 cleanup_run(target);
1283 continue;
1284 }
1285
Yuqian Li3528eb32017-09-21 13:23:49 -04001286 if (runs == 0 && FLAGS_ms < 1000) {
1287 // Run the first bench for 1000ms to warm up the nanobench if FLAGS_ms < 1000.
1288 // Otherwise, the first few benches' measurements will be inaccurate.
1289 auto stop = now_ms() + 1000;
1290 do {
1291 time(loops, bench.get(), target);
1292 } while (now_ms() < stop);
1293 }
1294
mtkleinbbba1682015-10-28 11:36:30 -07001295 if (FLAGS_ms) {
1296 samples.reset();
1297 auto stop = now_ms() + FLAGS_ms;
1298 do {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001299 samples.push_back(time(loops, bench.get(), target) / loops);
mtkleinbbba1682015-10-28 11:36:30 -07001300 } while (now_ms() < stop);
1301 } else {
cdaltone1b89582015-06-25 19:17:08 -07001302 samples.reset(FLAGS_samples);
1303 for (int s = 0; s < FLAGS_samples; s++) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001304 samples[s] = time(loops, bench.get(), target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001305 }
cdaltone1b89582015-06-25 19:17:08 -07001306 }
mtkleinf3723212014-06-25 14:08:00 -07001307
joshualitte45c81c2015-12-02 09:05:37 -08001308#if SK_SUPPORT_GPU
1309 SkTArray<SkString> keys;
1310 SkTArray<double> values;
1311 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1312 if (gpuStatsDump) {
1313 // TODO cache stats
1314 bench->getGpuStats(canvas, &keys, &values);
1315 }
1316#endif
1317
robertphillips5b693772014-11-21 06:19:36 -08001318 bench->perCanvasPostDraw(canvas);
1319
egdaniel3bf92062015-06-26 08:12:46 -07001320 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001321 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001322 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001323 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001324 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001325 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001326 }
1327
cdaltone1b89582015-06-25 19:17:08 -07001328 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001329 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001330 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001331 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001332 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001333 log->metric("min_ms", stats.min);
George Burgess IV75b57182016-12-06 10:53:52 -08001334 log->metrics("samples", samples);
joshualitte45c81c2015-12-02 09:05:37 -08001335#if SK_SUPPORT_GPU
1336 if (gpuStatsDump) {
1337 // dump to json, only SKPBench currently returns valid keys / values
1338 SkASSERT(keys.count() == values.count());
1339 for (int i = 0; i < keys.count(); i++) {
1340 log->metric(keys[i].c_str(), values[i]);
1341 }
1342 }
1343#endif
1344
mtkleine070c2b2014-10-14 08:40:43 -07001345 if (runs++ % FLAGS_flushEvery == 0) {
1346 log->flush();
1347 }
mtklein60317d0f2014-07-14 11:30:37 -07001348
bsalomon6eb03cc2014-08-07 14:28:50 -07001349 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001350 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001351 config = ""; // Only print the config if we run the same bench on more than one.
1352 }
mtkleind75c4662015-04-30 07:11:22 -07001353 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1354 , sk_tools::getCurrResidentSetSizeMB()
1355 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001356 , bench->getUniqueName()
1357 , config);
mtkleinf3723212014-06-25 14:08:00 -07001358 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001359 const char* mark = " ";
1360 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1361 if (stddev_percent > 5) mark = "?";
1362 if (stddev_percent > 10) mark = "!";
1363
1364 SkDebugf("%10.2f %s\t%s\t%s\n",
1365 stats.median*1e3, mark, bench->getUniqueName(), config);
Mike Reedc665e5b2017-07-04 22:56:06 -04001366 } else if (FLAGS_csv) {
mtkleinf3723212014-06-25 14:08:00 -07001367 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
Mike Reedc665e5b2017-07-04 22:56:06 -04001368 SkDebugf("%g,%g,%g,%g,%g,%s,%s\n"
1369 , stats.min
1370 , stats.median
1371 , stats.mean
1372 , stats.max
1373 , stddev_percent
1374 , config
1375 , bench->getUniqueName()
1376 );
1377 } else {
1378 const char* format = "%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n";
1379 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1380 SkDebugf(format
mtkleind75c4662015-04-30 07:11:22 -07001381 , sk_tools::getCurrResidentSetSizeMB()
1382 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001383 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001384 , HUMANIZE(stats.min)
1385 , HUMANIZE(stats.median)
1386 , HUMANIZE(stats.mean)
1387 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001388 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001389 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001390 , config
mtklein96289052014-09-10 12:05:59 -07001391 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001392 );
1393 }
joshualitte45c81c2015-12-02 09:05:37 -08001394
bsalomonb12ea412015-02-02 21:19:50 -08001395#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001396 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -04001397 target->dumpStats();
bsalomon06cddec2014-10-24 10:40:50 -07001398 }
1399#endif
joshualitte45c81c2015-12-02 09:05:37 -08001400
cdalton2c56ba52015-06-26 13:32:53 -07001401 if (FLAGS_verbose) {
1402 SkDebugf("Samples: ");
1403 for (int i = 0; i < samples.count(); i++) {
1404 SkDebugf("%s ", HUMANIZE(samples[i]));
1405 }
1406 SkDebugf("%s\n", bench->getUniqueName());
1407 }
egdaniel3bf92062015-06-26 08:12:46 -07001408 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001409 }
mtkleinf3723212014-06-25 14:08:00 -07001410 }
1411
Herb Derby5a523fe2017-01-26 16:48:28 -05001412 SkGraphics::PurgeAllCaches();
1413
mtkleine1091452014-12-04 10:47:02 -08001414 log->bench("memory_usage", 0,0);
1415 log->config("meta");
1416 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1417
mtkleinf3723212014-06-25 14:08:00 -07001418 return 0;
1419}