blob: 04583309ce79ddea5d5c938a9cd1f006fd6c7501 [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"
msarettc149f0e2016-01-04 11:35:43 -080048#include "SkThreadUtils.h"
Brian Osmanc3cdef52017-08-31 14:09:22 -040049#include "SkTraceEvent.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040050#include "Stats.h"
joshualitt3ebd0502016-02-09 07:18:08 -080051#include "ThermalManager.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040052#include "ios_utils.h"
mtkleinf3723212014-06-25 14:08:00 -070053
bungeman60e0fee2015-08-26 05:15:46 -070054#include <stdlib.h>
55
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"
bsalomon3724e572016-03-30 18:56:19 -070068 using sk_gpu_test::GrContextFactory;
bsalomonc8699322016-05-11 11:55:36 -070069 using sk_gpu_test::TestContext;
Ben Wagner145dbcd2016-11-03 14:40:50 -040070 std::unique_ptr<GrContextFactory> gGrFactory;
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.");
mtklein55e88b22015-01-21 15:50:13 -0800127DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
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 {
bsalomonc8699322016-05-11 11:55:36 -0700177 explicit GPUTarget(const Config& c) : Target(c), context(nullptr) { }
178 TestContext* context;
tomhudsond968a6f2015-03-26 11:28:06 -0700179
180 void setup() override {
bsalomonc8699322016-05-11 11:55:36 -0700181 this->context->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700182 // Make sure we're done with whatever came before.
bsalomonc8699322016-05-11 11:55:36 -0700183 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700184 }
185 void endTiming() override {
bsalomonc8699322016-05-11 11:55:36 -0700186 if (this->context) {
187 this->context->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700188 }
189 }
190 void fence() override {
bsalomonc8699322016-05-11 11:55:36 -0700191 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700192 }
mtkleind75c4662015-04-30 07:11:22 -0700193
cdaltond416a5b2015-06-23 13:23:44 -0700194 bool needsFrameTiming(int* maxFrameLag) const override {
bsalomonc8699322016-05-11 11:55:36 -0700195 if (!this->context->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700196 // Frame lag is unknown.
197 *maxFrameLag = FLAGS_gpuFrameLag;
198 }
199 return true;
200 }
tomhudsond968a6f2015-03-26 11:28:06 -0700201 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700202 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
203 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700204 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700205 this->surface = SkSurface::MakeRenderTarget(gGrFactory->get(this->config.ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700206 this->config.ctxOverrides),
bsalomon5ec26ae2016-02-25 08:33:02 -0800207 SkBudgeted::kNo, info,
reede8f30622016-03-23 18:59:25 -0700208 this->config.samples, &props);
bsalomonc8699322016-05-11 11:55:36 -0700209 this->context = gGrFactory->getContextInfo(this->config.ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700210 this->config.ctxOverrides).testContext();
tomhudsond968a6f2015-03-26 11:28:06 -0700211 if (!this->surface.get()) {
212 return false;
213 }
bsalomonc8699322016-05-11 11:55:36 -0700214 if (!this->context->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700215 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800216 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700217 }
tomhudsond968a6f2015-03-26 11:28:06 -0700218 return true;
219 }
220 void fillOptions(ResultsWriter* log) override {
221 const GrGLubyte* version;
bsalomonc8699322016-05-11 11:55:36 -0700222 if (this->context->backend() == kOpenGL_GrBackend) {
223 const GrGLInterface* gl =
224 reinterpret_cast<const GrGLInterface*>(this->context->backendContext());
225 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
226 log->configOption("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700227
bsalomonc8699322016-05-11 11:55:36 -0700228 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
229 log->configOption("GL_RENDERER", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700230
bsalomonc8699322016-05-11 11:55:36 -0700231 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VENDOR));
232 log->configOption("GL_VENDOR", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700233
bsalomonc8699322016-05-11 11:55:36 -0700234 GR_GL_CALL_RET(gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
235 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
236 }
tomhudsond968a6f2015-03-26 11:28:06 -0700237 }
238};
mtkleind75c4662015-04-30 07:11:22 -0700239
tomhudsond968a6f2015-03-26 11:28:06 -0700240#endif
241
tomhudson75a0ebb2015-03-27 12:11:44 -0700242static double time(int loops, Benchmark* bench, Target* target) {
243 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700244 if (canvas) {
245 canvas->clear(SK_ColorWHITE);
246 }
joshualitt8a6697a2015-09-30 12:11:07 -0700247 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700248 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700249 canvas = target->beginTiming(canvas);
250 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700251 if (canvas) {
252 canvas->flush();
253 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700254 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700255 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700256 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700257 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700258}
259
mtkleinf3723212014-06-25 14:08:00 -0700260static double estimate_timer_overhead() {
261 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700262 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700263 double start = now_ms();
264 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700265 }
266 return overhead / FLAGS_overheadLoops;
267}
268
reed53249782014-10-10 09:09:52 -0700269static int detect_forever_loops(int loops) {
270 // look for a magic run-forever value
271 if (loops < 0) {
272 loops = SK_MaxS32;
273 }
274 return loops;
275}
276
mtklein55b0ffc2014-07-17 08:38:23 -0700277static int clamp_loops(int loops) {
278 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800279 SkDebugf("ERROR: clamping loops from %d to 1. "
280 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700281 return 1;
282 }
283 if (loops > FLAGS_maxLoops) {
284 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
285 return FLAGS_maxLoops;
286 }
287 return loops;
288}
289
tomhudsond968a6f2015-03-26 11:28:06 -0700290static bool write_canvas_png(Target* target, const SkString& filename) {
291
bsalomon6eb03cc2014-08-07 14:28:50 -0700292 if (filename.isEmpty()) {
293 return false;
294 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700295 if (target->getCanvas() &&
296 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700297 return false;
298 }
tomhudsond968a6f2015-03-26 11:28:06 -0700299
bsalomon6eb03cc2014-08-07 14:28:50 -0700300 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700301
302 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700303 return false;
304 }
tomhudsond968a6f2015-03-26 11:28:06 -0700305
bsalomon6eb03cc2014-08-07 14:28:50 -0700306 SkString dir = SkOSPath::Dirname(filename.c_str());
307 if (!sk_mkdir(dir.c_str())) {
308 SkDebugf("Can't make dir %s.\n", dir.c_str());
309 return false;
310 }
311 SkFILEWStream stream(filename.c_str());
312 if (!stream.isValid()) {
313 SkDebugf("Can't write %s.\n", filename.c_str());
314 return false;
315 }
Hal Canarydb683012016-11-23 08:55:18 -0700316 if (!SkEncodeImage(&stream, bmp, SkEncodedImageFormat::kPNG, 100)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700317 SkDebugf("Can't encode a PNG.\n");
318 return false;
319 }
320 return true;
321}
322
323static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700324static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700325 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700326 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700327 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700328 int loops = bench->calculateLoops(FLAGS_loops);
329 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700330 while (bench_plus_overhead < overhead) {
331 if (round++ == FLAGS_maxCalibrationAttempts) {
332 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700333 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700334 return kFailedLoops;
335 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700336 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700337 }
mtklein2069e222014-08-04 13:57:39 -0700338 }
mtkleinf3723212014-06-25 14:08:00 -0700339
mtkleinbb6a0282014-07-01 08:43:42 -0700340 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700341 // We'll pick N to make timer overhead negligible:
342 //
mtkleinbb6a0282014-07-01 08:43:42 -0700343 // overhead
344 // ------------------------- < FLAGS_overheadGoal
345 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700346 //
Hal Canary55325b72017-01-03 10:36:17 -0500347 // where bench_plus_overhead ~=~ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700348 //
349 // Doing some math, we get:
350 //
mtkleinbb6a0282014-07-01 08:43:42 -0700351 // (overhead / FLAGS_overheadGoal) - overhead
352 // ------------------------------------------ < N
353 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700354 //
355 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700356 if (kAutoTuneLoops == loops) {
357 const double numer = overhead / FLAGS_overheadGoal - overhead;
358 const double denom = bench_plus_overhead - overhead;
359 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700360 loops = clamp_loops(loops);
361 } else {
362 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700363 }
mtkleinbb6a0282014-07-01 08:43:42 -0700364
mtkleinbb6a0282014-07-01 08:43:42 -0700365 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700366}
367
cdaltone1b89582015-06-25 19:17:08 -0700368static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700369 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700370 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700371 if (kAutoTuneLoops == loops) {
372 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700373 double elapsed = 0;
374 do {
mtklein527930f2014-11-06 08:04:34 -0800375 if (1<<30 == loops) {
376 // We're about to wrap. Something's wrong with the bench.
377 loops = 0;
378 break;
379 }
mtkleina189ccd2014-07-14 12:28:47 -0700380 loops *= 2;
381 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700382 // _this_ round, not still timing last round.
383 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700384 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700385 }
386 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700387
mtkleina189ccd2014-07-14 12:28:47 -0700388 // We've overshot at least a little. Scale back linearly.
389 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700390 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700391
tomhudsond968a6f2015-03-26 11:28:06 -0700392 // Make sure we're not still timing our calibration.
393 target->fence();
reed53249782014-10-10 09:09:52 -0700394 } else {
395 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700396 }
mtkleinbb6a0282014-07-01 08:43:42 -0700397
398 // Pretty much the same deal as the calibration: do some warmup to make
399 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700400 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700401 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700402 }
mtkleinbb6a0282014-07-01 08:43:42 -0700403
mtkleinbb6a0282014-07-01 08:43:42 -0700404 return loops;
405}
mtkleinbb6a0282014-07-01 08:43:42 -0700406
bsalomonc2553372014-07-22 13:09:05 -0700407#if SK_SUPPORT_GPU
Brian Salomon6405e712017-03-20 08:54:16 -0400408#define kBogusContextType GrContextFactory::kGL_ContextType
csmartdaltone812d492017-02-21 12:36:05 -0700409#define kBogusContextOverrides GrContextFactory::ContextOverrides::kNone
bsalomonc2553372014-07-22 13:09:05 -0700410#else
bsalomon85b4b532016-04-05 11:06:27 -0700411#define kBogusContextType 0
csmartdaltone812d492017-02-21 12:36:05 -0700412#define kBogusContextOverrides 0
mtkleine714e752014-07-31 12:13:48 -0700413#endif
bsalomonc2553372014-07-22 13:09:05 -0700414
svaisanenc47635e2016-01-28 06:05:43 -0800415static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
416
417#if SK_SUPPORT_GPU
418 if (const auto* gpuConfig = config->asConfigGpu()) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400419 if (!FLAGS_gpu) {
420 SkDebugf("Skipping config '%s' as requested.\n", config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800421 return;
Ben Wagner32fa5102017-08-10 21:25:55 -0400422 }
svaisanenc47635e2016-01-28 06:05:43 -0800423
svaisanenc47635e2016-01-28 06:05:43 -0800424 const auto ctxType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700425 const auto ctxOverrides = gpuConfig->getContextOverrides();
svaisanenc47635e2016-01-28 06:05:43 -0800426 const auto sampleCount = gpuConfig->getSamples();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400427 const auto colorType = gpuConfig->getColorType();
428 auto colorSpace = gpuConfig->getColorSpace();
svaisanenc47635e2016-01-28 06:05:43 -0800429
csmartdaltone812d492017-02-21 12:36:05 -0700430 if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOverrides)) {
Greg Daniel81e7bf82017-07-19 14:47:42 -0400431 GrPixelConfig grPixConfig = SkImageInfo2GrPixelConfig(colorType, colorSpace,
432 *ctx->caps());
433 int supportedSampleCount = ctx->caps()->getSampleCount(sampleCount, grPixConfig);
434 if (sampleCount != supportedSampleCount) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400435 SkDebugf("Configuration '%s' sample count %d is not a supported sample count.\n",
436 config->getTag().c_str(), sampleCount);
svaisanenc47635e2016-01-28 06:05:43 -0800437 return;
438 }
439 } else {
Ben Wagner32fa5102017-08-10 21:25:55 -0400440 SkDebugf("No context was available matching config '%s'.\n",
441 config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800442 return;
443 }
444
445 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800446 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800447 Benchmark::kGPU_Backend,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400448 colorType,
svaisanenc47635e2016-01-28 06:05:43 -0800449 kPremul_SkAlphaType,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400450 sk_ref_sp(colorSpace),
svaisanenc47635e2016-01-28 06:05:43 -0800451 sampleCount,
452 ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700453 ctxOverrides,
kkinnunend4e1c352016-03-01 23:41:26 -0800454 gpuConfig->getUseDIText()
455 };
svaisanenc47635e2016-01-28 06:05:43 -0800456
457 configs->push_back(target);
458 return;
459 }
460#endif
461
brianosmanb109b8c2016-06-16 13:03:24 -0700462 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
463 if (config->getTag().equals(#name)) { \
Ben Wagner32fa5102017-08-10 21:25:55 -0400464 if (!FLAGS_cpu) { \
465 SkDebugf("Skipping config '%s' as requested.\n", \
466 config->getTag().c_str()); \
467 return; \
468 } \
brianosmanb109b8c2016-06-16 13:03:24 -0700469 Config config = { \
470 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
csmartdaltone812d492017-02-21 12:36:05 -0700471 0, kBogusContextType, kBogusContextOverrides, false \
brianosmanb109b8c2016-06-16 13:03:24 -0700472 }; \
473 configs->push_back(config); \
474 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700475 }
mtkleine714e752014-07-31 12:13:48 -0700476
Ben Wagner32fa5102017-08-10 21:25:55 -0400477 CPU_CONFIG(nonrendering, kNonRendering_Backend,
478 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800479
Mike Reede6befa52017-08-28 11:30:48 -0400480 CPU_CONFIG(a8, kRaster_Backend,
481 kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr)
Ben Wagner32fa5102017-08-10 21:25:55 -0400482 CPU_CONFIG(8888, kRaster_Backend,
483 kN32_SkColorType, kPremul_SkAlphaType, nullptr)
484 CPU_CONFIG(565, kRaster_Backend,
485 kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
486 auto srgbColorSpace = SkColorSpace::MakeSRGB();
487 CPU_CONFIG(srgb, kRaster_Backend,
488 kN32_SkColorType, kPremul_SkAlphaType, srgbColorSpace)
489 auto srgbLinearColorSpace = SkColorSpace::MakeSRGBLinear();
490 CPU_CONFIG(f16, kRaster_Backend,
491 kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbLinearColorSpace)
mtkleinbb6a0282014-07-01 08:43:42 -0700492
svaisanenc47635e2016-01-28 06:05:43 -0800493 #undef CPU_CONFIG
Ben Wagner32fa5102017-08-10 21:25:55 -0400494
495 SkDebugf("Unknown config '%s'.\n", config->getTag().c_str());
mtkleinf3723212014-06-25 14:08:00 -0700496}
497
svaisanenc47635e2016-01-28 06:05:43 -0800498// Append all configs that are enabled and supported.
499void create_configs(SkTArray<Config>* configs) {
500 SkCommandLineConfigArray array;
501 ParseConfigs(FLAGS_config, &array);
502 for (int i = 0; i < array.count(); ++i) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400503 create_config(array[i].get(), configs);
svaisanenc47635e2016-01-28 06:05:43 -0800504 }
Ben Wagner32fa5102017-08-10 21:25:55 -0400505
506 // If no just default configs were requested, then we're okay.
507 if (array.count() == 0 || FLAGS_config.count() == 0 ||
508 // If we've been told to ignore undefined flags, we're okay.
509 FLAGS_undefok ||
510 // Otherwise, make sure that all specified configs have been created.
511 array.count() == configs->count()) {
512 return;
513 }
514 SkDebugf("Invalid --config. Use --undefok to bypass this warning.\n");
515 exit(1);
svaisanenc47635e2016-01-28 06:05:43 -0800516}
517
brianosman9f1f6e22016-09-15 08:33:02 -0700518// disable warning : switch statement contains default but no 'case' labels
519#if defined _WIN32
520#pragma warning ( push )
521#pragma warning ( disable : 4065 )
522#endif
523
halcanary96fcdcc2015-08-27 07:41:13 -0700524// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700525static Target* is_enabled(Benchmark* bench, const Config& config) {
526 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700527 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700528 }
529
reede5ea5002014-09-03 11:54:58 -0700530 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700531 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700532
halcanary96fcdcc2015-08-27 07:41:13 -0700533 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700534
tomhudsond968a6f2015-03-26 11:28:06 -0700535 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700536#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700537 case Benchmark::kGPU_Backend:
538 target = new GPUTarget(config);
539 break;
bsalomonc2553372014-07-22 13:09:05 -0700540#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700541 default:
542 target = new Target(config);
543 break;
544 }
bsalomonc2553372014-07-22 13:09:05 -0700545
tomhudsond968a6f2015-03-26 11:28:06 -0700546 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700547 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700548 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700549 }
550 return target;
551}
552
brianosman9f1f6e22016-09-15 08:33:02 -0700553#if defined _WIN32
554#pragma warning ( pop )
555#endif
556
reed42943c82016-09-12 12:01:44 -0700557static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700558 uint32_t minOutputSize, int* width, int* height) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400559 std::unique_ptr<SkBitmapRegionDecoder> brd(
msarettd1227a72016-05-18 06:23:57 -0700560 SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy));
msarett7f691442015-09-22 11:56:16 -0700561 if (nullptr == brd.get()) {
562 // This is indicates that subset decoding is not supported for a particular image format.
563 return false;
564 }
565
msarett7f691442015-09-22 11:56:16 -0700566 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
567 (uint32_t) brd->height()) {
568 // This indicates that the image is not large enough to decode a
569 // minOutputSize x minOutputSize subset at the given sampleSize.
570 return false;
571 }
572
573 // Set the image width and height. The calling code will use this to choose subsets to decode.
574 *width = brd->width();
575 *height = brd->height();
576 return true;
577}
578
egdaniel3bf92062015-06-26 08:12:46 -0700579static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700580 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700581#if SK_SUPPORT_GPU
582 if (FLAGS_abandonGpuContext) {
583 gGrFactory->abandonContexts();
584 }
585 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
586 gGrFactory->destroyContexts();
587 }
588#endif
589}
590
fmalita6519c212016-09-14 08:05:17 -0700591static void collect_files(const SkCommandLineFlags::StringArray& paths, const char* ext,
592 SkTArray<SkString>* list) {
593 for (int i = 0; i < paths.count(); ++i) {
594 if (SkStrEndsWith(paths[i], ext)) {
595 list->push_back(SkString(paths[i]));
596 } else {
597 SkOSFile::Iter it(paths[i], ext);
598 SkString path;
599 while (it.next(&path)) {
600 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
601 }
602 }
603 }
604}
605
mtkleine714e752014-07-31 12:13:48 -0700606class BenchmarkStream {
607public:
mtklein92007582014-08-01 07:46:52 -0700608 BenchmarkStream() : fBenches(BenchRegistry::Head())
609 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700610 , fCurrentRecording(0)
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400611 , fCurrentPiping(0)
mtklein92007582014-08-01 07:46:52 -0700612 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800613 , fCurrentSKP(0)
fmalita6519c212016-09-14 08:05:17 -0700614 , fCurrentSVG(0)
msarett95f192d2015-02-13 09:05:41 -0800615 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700616 , fCurrentCodec(0)
msarett84451022016-02-11 06:45:51 -0800617 , fCurrentAndroidCodec(0)
msarett7f691442015-09-22 11:56:16 -0700618 , fCurrentBRDImage(0)
msarett2cee9022016-06-03 08:25:21 -0700619 , fCurrentColorImage(0)
msarett95f192d2015-02-13 09:05:41 -0800620 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800621 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700622 , fCurrentSubsetType(0)
msarett84451022016-02-11 06:45:51 -0800623 , fCurrentSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700624 , fCurrentAnimSKP(0) {
fmalita6519c212016-09-14 08:05:17 -0700625 collect_files(FLAGS_skps, ".skp", &fSKPs);
626 collect_files(FLAGS_svgs, ".svg", &fSVGs);
mtkleine714e752014-07-31 12:13:48 -0700627
mtklein92007582014-08-01 07:46:52 -0700628 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
629 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
630 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
631 exit(1);
632 }
633
634 for (int i = 0; i < FLAGS_scales.count(); i++) {
635 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
636 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
637 exit(1);
638 }
639 }
robertphillips5b693772014-11-21 06:19:36 -0800640
cdalton63a82852015-06-29 14:06:10 -0700641 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
642 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700643 exit(1);
644 }
645
robertphillips5b693772014-11-21 06:19:36 -0800646 if (FLAGS_mpd) {
647 fUseMPDs.push_back() = true;
648 }
mtkleinc751ecb2015-06-15 08:56:38 -0700649 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700650
msarett95f192d2015-02-13 09:05:41 -0800651 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700652 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800653 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800654 }
msarett2cee9022016-06-03 08:25:21 -0700655 if (!CollectImages(FLAGS_colorImages, &fColorImages)) {
656 exit(1);
657 }
mtklein95553d92015-03-12 08:24:21 -0700658
msarett95f192d2015-02-13 09:05:41 -0800659 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700660 fColorTypes.push_back(kN32_SkColorType);
661 if (!FLAGS_simpleCodec) {
662 fColorTypes.push_back(kRGB_565_SkColorType);
663 fColorTypes.push_back(kAlpha_8_SkColorType);
msarett67cb6662016-06-21 08:49:26 -0700664 fColorTypes.push_back(kGray_8_SkColorType);
665 }
mtklein92007582014-08-01 07:46:52 -0700666 }
667
reedca2622b2016-03-18 07:25:55 -0700668 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700669 // Not strictly necessary, as it will be checked again later,
670 // but helps to avoid a lot of pointless work if we're going to skip it.
cdalton91e457d2016-02-17 11:10:16 -0800671 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700672 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700673 }
674
bungemanf93d7112016-09-16 06:24:20 -0700675 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
676 if (!stream) {
mtkleinfd731ce2014-09-10 12:19:30 -0700677 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700678 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700679 }
680
reedca2622b2016-03-18 07:25:55 -0700681 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700682 }
683
fmalita6519c212016-09-14 08:05:17 -0700684 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
685 SkFILEStream stream(path);
686 if (!stream.isValid()) {
687 SkDebugf("Could not read %s.\n", path);
688 return nullptr;
689 }
690
fmalitae1baa7c2016-09-14 12:04:30 -0700691 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
fmalita6519c212016-09-14 08:05:17 -0700692 if (!svgDom) {
693 SkDebugf("Could not parse %s.\n", path);
694 return nullptr;
695 }
696
fmalitae1baa7c2016-09-14 12:04:30 -0700697 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
698 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
699 if (svgDom->containerSize().isEmpty()) {
700 svgDom->setContainerSize(kDefaultContainerSize);
701 }
702
fmalita6519c212016-09-14 08:05:17 -0700703 SkPictureRecorder recorder;
fmalitae1baa7c2016-09-14 12:04:30 -0700704 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
705 svgDom->containerSize().height()));
fmalita6519c212016-09-14 08:05:17 -0700706 return recorder.finishRecordingAsPicture();
707 }
708
mtklein92007582014-08-01 07:46:52 -0700709 Benchmark* next() {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400710 std::unique_ptr<Benchmark> bench;
mtklein65dfd2f2016-02-03 10:40:54 -0800711 do {
712 bench.reset(this->rawNext());
713 if (!bench) {
714 return nullptr;
715 }
716 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
717 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700718 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800719 }
720
721 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700722 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700723 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700724 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700725 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700726 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700727 return bench;
728 }
mtklein92007582014-08-01 07:46:52 -0700729
mtkleine714e752014-07-31 12:13:48 -0700730 while (fGMs) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400731 std::unique_ptr<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700732 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800733 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700734 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700735 fBenchType = "micro";
mtklein18300a32016-03-16 13:53:35 -0700736 return new GMBench(gm.release());
mtkleine714e752014-07-31 12:13:48 -0700737 }
738 }
mtklein92007582014-08-01 07:46:52 -0700739
mtkleinfd731ce2014-09-10 12:19:30 -0700740 // First add all .skps as RecordingBenches.
741 while (fCurrentRecording < fSKPs.count()) {
742 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700743 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
744 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700745 continue;
746 }
747 SkString name = SkOSPath::Basename(path.c_str());
748 fSourceType = "skp";
749 fBenchType = "recording";
Mike Reed90b20052017-03-08 10:39:02 -0500750 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
mtklein051e56d2014-12-04 08:46:51 -0800751 fSKPOps = pic->approximateOpCount();
mtklein8c1a4f82016-08-08 06:56:22 -0700752 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh, FLAGS_lite);
mtkleinfd731ce2014-09-10 12:19:30 -0700753 }
754
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400755 // Add all .skps as PipeBenches.
756 while (fCurrentPiping < fSKPs.count()) {
757 const SkString& path = fSKPs[fCurrentPiping++];
758 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
759 if (!pic) {
760 continue;
761 }
762 SkString name = SkOSPath::Basename(path.c_str());
763 fSourceType = "skp";
764 fBenchType = "piping";
Mike Reed90b20052017-03-08 10:39:02 -0500765 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400766 fSKPOps = pic->approximateOpCount();
767 return new PipingBench(name.c_str(), pic.get());
768 }
769
mtkleinfd731ce2014-09-10 12:19:30 -0700770 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700771 while (fCurrentScale < fScales.count()) {
772 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800773 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700774 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
775 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800776 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700777 continue;
778 }
robertphillips5b693772014-11-21 06:19:36 -0800779
780 while (fCurrentUseMPD < fUseMPDs.count()) {
781 if (FLAGS_bbh) {
782 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
783 SkRTreeFactory factory;
784 SkPictureRecorder recorder;
robertphillips5b693772014-11-21 06:19:36 -0800785 pic->playback(recorder.beginRecording(pic->cullRect().width(),
786 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800787 &factory,
robertphillipsdda54452016-07-13 13:27:16 -0700788 0));
reedca2622b2016-03-18 07:25:55 -0700789 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800790 }
791 SkString name = SkOSPath::Basename(path.c_str());
792 fSourceType = "skp";
793 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700794 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
795 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700796 }
robertphillips5b693772014-11-21 06:19:36 -0800797 fCurrentUseMPD = 0;
798 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700799 }
fmalita6519c212016-09-14 08:05:17 -0700800
801 while (fCurrentSVG++ < fSVGs.count()) {
802 const char* path = fSVGs[fCurrentSVG - 1].c_str();
803 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
804 fSourceType = "svg";
805 fBenchType = "playback";
806 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
807 fScales[fCurrentScale], false, FLAGS_loopSKP);
808 }
809 }
810
mtklein92007582014-08-01 07:46:52 -0700811 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700812 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700813 fCurrentScale++;
814 }
815
joshualitt261c3ad2015-04-27 09:16:57 -0700816 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700817 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700818 while (fCurrentAnimSKP < fSKPs.count()) {
819 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700820 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
821 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700822 fCurrentAnimSKP++;
823 continue;
824 }
825
826 fCurrentAnimSKP++;
827 SkString name = SkOSPath::Basename(path.c_str());
Hal Canary2db83612016-11-04 13:02:54 -0400828 sk_sp<SKPAnimationBench::Animation> animation(
cdalton63a82852015-06-29 14:06:10 -0700829 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
Hal Canary2db83612016-11-04 13:02:54 -0400830 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation.get(),
halcanary385fe4d2015-08-26 13:07:48 -0700831 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700832 }
833 }
834
scroggo60869a42015-04-01 12:09:17 -0700835 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700836 fSourceType = "image";
837 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700838 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800839 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
840 continue;
841 }
bungeman38d909e2016-08-02 14:40:46 -0700842 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400843 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700844 if (!codec) {
845 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700846 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700847 continue;
848 }
scroggo21027992015-04-02 13:22:38 -0700849
scroggo60869a42015-04-01 12:09:17 -0700850 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700851 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700852
msarettc7796b92016-01-07 14:20:20 -0800853 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700854 if (FLAGS_simpleCodec) {
855 if (kUnpremul_SkAlphaType == alphaType) {
856 alphaType = kPremul_SkAlphaType;
857 }
858
859 fCurrentColorType++;
860 } else {
861 switch (alphaType) {
862 case kOpaque_SkAlphaType:
863 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800864 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700865 break;
866 case kUnpremul_SkAlphaType:
867 case kPremul_SkAlphaType:
868 if (0 == fCurrentAlphaType) {
869 // Test unpremul first.
870 alphaType = kUnpremul_SkAlphaType;
871 fCurrentAlphaType++;
872 } else {
873 // Test premul.
874 alphaType = kPremul_SkAlphaType;
875 fCurrentAlphaType = 0;
876 fCurrentColorType++;
877 }
878 break;
879 default:
880 SkASSERT(false);
881 fCurrentColorType++;
882 break;
883 }
scroggo21027992015-04-02 13:22:38 -0700884 }
885
msarettc7796b92016-01-07 14:20:20 -0800886 // Make sure we can decode to this color type and alpha type.
887 SkImageInfo info =
888 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700889 const size_t rowBytes = info.minRowBytes();
Jim Van Verth809cbed2017-09-27 22:13:44 +0000890 SkAutoMalloc storage(info.getSafeSize(rowBytes));
scroggo21027992015-04-02 13:22:38 -0700891
scroggoeb602a52015-07-09 08:16:03 -0700892 const SkCodec::Result result = codec->getPixels(
Leon Scroggins571b30f2017-07-11 17:35:31 +0000893 info, storage.get(), rowBytes);
scroggo60869a42015-04-01 12:09:17 -0700894 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700895 case SkCodec::kSuccess:
896 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700897 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700898 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700899 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700900 // This is okay. Not all conversions are valid.
901 break;
scroggo60869a42015-04-01 12:09:17 -0700902 default:
903 // This represents some sort of failure.
904 SkASSERT(false);
905 break;
906 }
907 }
908 fCurrentColorType = 0;
909 }
910
msarett84451022016-02-11 06:45:51 -0800911 // Run AndroidCodecBenches
912 const int sampleSizes[] = { 2, 4, 8 };
913 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
914 fSourceType = "image";
915 fBenchType = "skandroidcodec";
916
917 const SkString& path = fImages[fCurrentAndroidCodec];
918 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
919 continue;
920 }
bungeman38d909e2016-08-02 14:40:46 -0700921 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400922 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800923 if (!codec) {
924 // Nothing to time.
925 SkDebugf("Cannot find codec for %s\n", path.c_str());
926 continue;
927 }
928
929 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
930 int sampleSize = sampleSizes[fCurrentSampleSize];
931 fCurrentSampleSize++;
932 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
933 // Avoid benchmarking scaled decodes of already small images.
934 break;
935 }
936
bungeman38d909e2016-08-02 14:40:46 -0700937 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
938 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -0800939 }
940 fCurrentSampleSize = 0;
941 }
942
msarett7f691442015-09-22 11:56:16 -0700943 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700944 // We intend to create benchmarks that model the use cases in
945 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
946 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
947 // the image. For that reason, we will benchmark decodes in five representative locations
948 // in the image. Additionally, this use case utilizes power of two scaling, so we will
949 // test on power of two sample sizes. The output tile is always 512x512, so, when a
950 // sampleSize is used, the size of the subset that is decoded is always
951 // (sampleSize*512)x(sampleSize*512).
952 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700953 // All use cases we are aware of only scale by powers of two.
954 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
955 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800956 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700957 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800958 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700959 fSourceType = "image";
960 fBenchType = "BRD";
961
mtklein6f0ff912016-01-11 09:04:21 -0800962 const SkString& path = fImages[fCurrentBRDImage];
963 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
964 continue;
965 }
scroggo860e8a62015-10-15 07:51:28 -0700966
msarettd1227a72016-05-18 06:23:57 -0700967 while (fCurrentColorType < fColorTypes.count()) {
968 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
969 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700970
bungeman38d909e2016-08-02 14:40:46 -0700971 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -0700972 const SkColorType colorType = fColorTypes[fCurrentColorType];
973 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
974 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -0700975
msarettd1227a72016-05-18 06:23:57 -0700976 int width = 0;
977 int height = 0;
reed42943c82016-09-12 12:01:44 -0700978 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -0700979 &width, &height)) {
980 break;
msarett7f691442015-09-22 11:56:16 -0700981 }
msarettd1227a72016-05-18 06:23:57 -0700982
983 SkString basename = SkOSPath::Basename(path.c_str());
984 SkIRect subset;
985 const uint32_t subsetSize = sampleSize * minOutputSize;
986 switch (currentSubsetType) {
987 case kTopLeft_SubsetType:
988 basename.append("_TopLeft");
989 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
990 break;
991 case kTopRight_SubsetType:
992 basename.append("_TopRight");
993 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
994 subsetSize);
995 break;
996 case kMiddle_SubsetType:
997 basename.append("_Middle");
998 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
999 (height - subsetSize) / 2, subsetSize, subsetSize);
1000 break;
1001 case kBottomLeft_SubsetType:
1002 basename.append("_BottomLeft");
1003 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
1004 subsetSize);
1005 break;
1006 case kBottomRight_SubsetType:
1007 basename.append("_BottomRight");
1008 subset = SkIRect::MakeXYWH(width - subsetSize,
1009 height - subsetSize, subsetSize, subsetSize);
1010 break;
1011 default:
1012 SkASSERT(false);
1013 }
1014
1015 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
1016 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -07001017 }
msarettd1227a72016-05-18 06:23:57 -07001018 fCurrentSubsetType = 0;
1019 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -07001020 }
msarettd1227a72016-05-18 06:23:57 -07001021 fCurrentSampleSize = 0;
1022 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -07001023 }
msarettd1227a72016-05-18 06:23:57 -07001024 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -07001025 }
1026
msarett2cee9022016-06-03 08:25:21 -07001027 while (fCurrentColorImage < fColorImages.count()) {
1028 fSourceType = "colorimage";
1029 fBenchType = "skcolorcodec";
1030 const SkString& path = fColorImages[fCurrentColorImage];
1031 fCurrentColorImage++;
1032 sk_sp<SkData> encoded = SkData::MakeFromFileName(path.c_str());
1033 if (encoded) {
1034 return new ColorCodecBench(SkOSPath::Basename(path.c_str()).c_str(),
1035 std::move(encoded));
1036 } else {
1037 SkDebugf("Could not read file %s.\n", path.c_str());
1038 }
1039 }
1040
halcanary96fcdcc2015-08-27 07:41:13 -07001041 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001042 }
mtklein92007582014-08-01 07:46:52 -07001043
1044 void fillCurrentOptions(ResultsWriter* log) const {
1045 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001046 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001047 if (0 == strcmp(fSourceType, "skp")) {
1048 log->configOption("clip",
1049 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1050 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001051 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -07001052 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001053 if (fCurrentUseMPD > 0) {
1054 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1055 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1056 }
mtklein92007582014-08-01 07:46:52 -07001057 }
mtklein051e56d2014-12-04 08:46:51 -08001058 if (0 == strcmp(fBenchType, "recording")) {
1059 log->metric("bytes", fSKPBytes);
1060 log->metric("ops", fSKPOps);
1061 }
mtklein92007582014-08-01 07:46:52 -07001062 }
1063
mtkleine714e752014-07-31 12:13:48 -07001064private:
msarettb23e6aa2015-06-09 13:56:10 -07001065 enum SubsetType {
1066 kTopLeft_SubsetType = 0,
1067 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001068 kMiddle_SubsetType = 2,
1069 kBottomLeft_SubsetType = 3,
1070 kBottomRight_SubsetType = 4,
1071 kTranslate_SubsetType = 5,
1072 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001073 kLast_SubsetType = kZoom_SubsetType,
1074 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001075 };
1076
mtkleine714e752014-07-31 12:13:48 -07001077 const BenchRegistry* fBenches;
1078 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001079 SkIRect fClip;
1080 SkTArray<SkScalar> fScales;
1081 SkTArray<SkString> fSKPs;
fmalita6519c212016-09-14 08:05:17 -07001082 SkTArray<SkString> fSVGs;
robertphillips5b693772014-11-21 06:19:36 -08001083 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001084 SkTArray<SkString> fImages;
msarett2cee9022016-06-03 08:25:21 -07001085 SkTArray<SkString> fColorImages;
msarett74deb982015-10-20 16:45:56 -07001086 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001087 SkScalar fZoomMax;
1088 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001089
mtklein051e56d2014-12-04 08:46:51 -08001090 double fSKPBytes, fSKPOps;
1091
mtkleinfd731ce2014-09-10 12:19:30 -07001092 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1093 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1094 int fCurrentRecording;
Mike Reed9cdd2ab2016-10-21 10:43:36 -04001095 int fCurrentPiping;
mtklein92007582014-08-01 07:46:52 -07001096 int fCurrentScale;
1097 int fCurrentSKP;
fmalita6519c212016-09-14 08:05:17 -07001098 int fCurrentSVG;
robertphillips5b693772014-11-21 06:19:36 -08001099 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001100 int fCurrentCodec;
msarett84451022016-02-11 06:45:51 -08001101 int fCurrentAndroidCodec;
msarett7f691442015-09-22 11:56:16 -07001102 int fCurrentBRDImage;
msarett2cee9022016-06-03 08:25:21 -07001103 int fCurrentColorImage;
msarett95f192d2015-02-13 09:05:41 -08001104 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -08001105 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -07001106 int fCurrentSubsetType;
msarett84451022016-02-11 06:45:51 -08001107 int fCurrentSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001108 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001109};
1110
msarettc149f0e2016-01-04 11:35:43 -08001111// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1112// This prints something every once in a while so that it knows we're still working.
1113static void start_keepalive() {
1114 struct Loop {
1115 static void forever(void*) {
1116 for (;;) {
1117 static const int kSec = 1200;
1118 #if defined(SK_BUILD_FOR_WIN)
1119 Sleep(kSec * 1000);
1120 #else
1121 sleep(kSec);
1122 #endif
1123 SkDebugf("\nBenchmarks still running...\n");
1124 }
1125 }
1126 };
1127 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1128 intentionallyLeaked->start();
1129}
1130
Mike Kleinbe28ee22017-02-06 12:46:20 -05001131int main(int argc, char** argv) {
1132 SkCommandLineFlags::Parse(argc, argv);
Brian Osman53136aa2017-07-20 15:43:35 -04001133
Brian Osmanbc8150f2017-07-24 11:38:01 -04001134 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -04001135
Mike Kleinadacaef2017-02-06 09:26:14 -05001136#if defined(SK_BUILD_FOR_IOS)
1137 cd_Documents();
1138#endif
jcgregorio3b27ade2014-11-13 08:06:40 -08001139 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001140 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001141 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001142
krajcevski69a55602014-08-13 10:46:31 -07001143#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001144 GrContextOptions grContextOpts;
csmartdalton008b9d82017-02-22 12:00:42 -07001145 grContextOpts.fGpuPathRenderers = CollectGpuPathRenderersFromFlags();
Brian Osmanf9810662017-08-30 10:02:10 -04001146 grContextOpts.fExecutor = GpuExecutorForTools();
halcanary385fe4d2015-08-26 13:07:48 -07001147 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001148#endif
1149
bsalomon06cddec2014-10-24 10:40:50 -07001150 if (FLAGS_veryVerbose) {
1151 FLAGS_verbose = true;
1152 }
1153
bsalomon6eb03cc2014-08-07 14:28:50 -07001154 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001155 FLAGS_samples = 1;
1156 FLAGS_gpuFrameLag = 0;
1157 }
1158
bsalomon6eb03cc2014-08-07 14:28:50 -07001159 if (!FLAGS_writePath.isEmpty()) {
1160 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1161 if (!sk_mkdir(FLAGS_writePath[0])) {
1162 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001163 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001164 }
1165 }
1166
Ben Wagner145dbcd2016-11-03 14:40:50 -04001167 std::unique_ptr<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001168 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001169#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001170 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001171#else
1172 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1173 return 1;
1174#endif
mtklein60317d0f2014-07-14 11:30:37 -07001175 }
mtklein1915b622014-08-20 11:45:00 -07001176
1177 if (1 == FLAGS_properties.count() % 2) {
1178 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1179 return 1;
1180 }
1181 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1182 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1183 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001184
1185 if (1 == FLAGS_key.count() % 2) {
1186 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1187 return 1;
1188 }
1189 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001190 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001191 }
mtklein60317d0f2014-07-14 11:30:37 -07001192
mtkleinf3723212014-06-25 14:08:00 -07001193 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001194 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001195
cdaltone1b89582015-06-25 19:17:08 -07001196 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001197
bsalomon6eb03cc2014-08-07 14:28:50 -07001198 if (kAutoTuneLoops != FLAGS_loops) {
1199 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001200 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001201 SkDebugf("! -> high variance, ? -> moderate variance\n");
1202 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001203 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001204 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001205 } else {
mtkleind75c4662015-04-30 07:11:22 -07001206 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001207 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001208 }
1209
svaisanenc47635e2016-01-28 06:05:43 -08001210 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001211 create_configs(&configs);
1212
joshualitt3ebd0502016-02-09 07:18:08 -08001213#ifdef THERMAL_MANAGER_SUPPORTED
1214 int tmEnabled, tmThreshold, tmSleepTimeMs, tmTimeoutMs;
1215 if (4 != sscanf(FLAGS_useThermalManager[0], "%d,%d,%d,%d",
1216 &tmEnabled, &tmThreshold, &tmSleepTimeMs, &tmTimeoutMs)) {
1217 SkDebugf("Can't parse %s from --useThermalManager.\n", FLAGS_useThermalManager[0]);
1218 exit(1);
1219 }
1220 ThermalManager tm(tmThreshold, tmSleepTimeMs, tmTimeoutMs);
1221#endif
1222
msarettc149f0e2016-01-04 11:35:43 -08001223 if (FLAGS_keepAlive) {
1224 start_keepalive();
1225 }
1226
Yuqian Liba62b4a2016-12-14 13:46:53 -05001227 gSkUseAnalyticAA = FLAGS_analyticAA;
Yuqian Lidf60e362017-07-25 11:26:31 -04001228 gSkUseDeltaAA = FLAGS_deltaAA;
liyuqian38911a72016-10-04 11:23:22 -07001229
Yuqian Lidf60e362017-07-25 11:26:31 -04001230 if (FLAGS_forceDeltaAA) {
1231 gSkForceDeltaAA = true;
1232 }
Yuqian Li550148b2017-01-13 10:13:13 -05001233 if (FLAGS_forceAnalyticAA) {
1234 gSkForceAnalyticAA = true;
1235 }
Mike Reedc928fe22017-06-05 10:20:31 -04001236 if (FLAGS_forceRasterPipeline) {
1237 gSkForceRasterPipelineBlitter = true;
1238 }
Yuqian Li550148b2017-01-13 10:13:13 -05001239
mtkleine070c2b2014-10-14 08:40:43 -07001240 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001241 BenchmarkStream benchStream;
1242 while (Benchmark* b = benchStream.next()) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001243 std::unique_ptr<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001244 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001245 continue;
1246 }
1247
svaisanenc47635e2016-01-28 06:05:43 -08001248 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001249 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001250 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001251 }
egdaniel3bf92062015-06-26 08:12:46 -07001252 for (int i = 0; i < configs.count(); ++i) {
joshualitt3ebd0502016-02-09 07:18:08 -08001253#ifdef THERMAL_MANAGER_SUPPORTED
1254 if (tmEnabled && !tm.coolOffIfNecessary()) {
1255 SkDebugf("Could not cool off, timings will be throttled\n");
1256 }
1257#endif
egdaniel3bf92062015-06-26 08:12:46 -07001258 Target* target = is_enabled(b, configs[i]);
1259 if (!target) {
1260 continue;
1261 }
mtkleinf3723212014-06-25 14:08:00 -07001262
halcanary96fcdcc2015-08-27 07:41:13 -07001263 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001264 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001265 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001266
brianosman7a5ada82016-02-08 13:49:12 -08001267 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001268 SkDebugf("Running %s\t%s\n"
1269 , bench->getUniqueName()
1270 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001271 if (FLAGS_dryRun) {
1272 continue;
1273 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001274 }
1275
Brian Osman5ad87aa2017-09-08 16:05:23 -04001276 TRACE_EVENT2("skia", "Benchmark", "name", TRACE_STR_COPY(bench->getUniqueName()),
1277 "config", TRACE_STR_COPY(config));
Brian Osmanc3cdef52017-08-31 14:09:22 -04001278
egdaniel3bf92062015-06-26 08:12:46 -07001279 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001280 bench->perCanvasPreDraw(canvas);
1281
cdaltone1b89582015-06-25 19:17:08 -07001282 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001283 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001284 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1285 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001286
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
1329 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001330 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001331 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001332 continue;
1333 }
1334
cdaltone1b89582015-06-25 19:17:08 -07001335 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001336 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001337 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001338 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001339 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001340 log->metric("min_ms", stats.min);
George Burgess IV75b57182016-12-06 10:53:52 -08001341 log->metrics("samples", samples);
joshualitte45c81c2015-12-02 09:05:37 -08001342#if SK_SUPPORT_GPU
1343 if (gpuStatsDump) {
1344 // dump to json, only SKPBench currently returns valid keys / values
1345 SkASSERT(keys.count() == values.count());
1346 for (int i = 0; i < keys.count(); i++) {
1347 log->metric(keys[i].c_str(), values[i]);
1348 }
1349 }
1350#endif
1351
mtkleine070c2b2014-10-14 08:40:43 -07001352 if (runs++ % FLAGS_flushEvery == 0) {
1353 log->flush();
1354 }
mtklein60317d0f2014-07-14 11:30:37 -07001355
bsalomon6eb03cc2014-08-07 14:28:50 -07001356 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001357 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001358 config = ""; // Only print the config if we run the same bench on more than one.
1359 }
mtkleind75c4662015-04-30 07:11:22 -07001360 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1361 , sk_tools::getCurrResidentSetSizeMB()
1362 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001363 , bench->getUniqueName()
1364 , config);
mtkleinf3723212014-06-25 14:08:00 -07001365 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001366 const char* mark = " ";
1367 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1368 if (stddev_percent > 5) mark = "?";
1369 if (stddev_percent > 10) mark = "!";
1370
1371 SkDebugf("%10.2f %s\t%s\t%s\n",
1372 stats.median*1e3, mark, bench->getUniqueName(), config);
Mike Reedc665e5b2017-07-04 22:56:06 -04001373 } else if (FLAGS_csv) {
mtkleinf3723212014-06-25 14:08:00 -07001374 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
Mike Reedc665e5b2017-07-04 22:56:06 -04001375 SkDebugf("%g,%g,%g,%g,%g,%s,%s\n"
1376 , stats.min
1377 , stats.median
1378 , stats.mean
1379 , stats.max
1380 , stddev_percent
1381 , config
1382 , bench->getUniqueName()
1383 );
1384 } else {
1385 const char* format = "%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n";
1386 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1387 SkDebugf(format
mtkleind75c4662015-04-30 07:11:22 -07001388 , sk_tools::getCurrResidentSetSizeMB()
1389 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001390 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001391 , HUMANIZE(stats.min)
1392 , HUMANIZE(stats.median)
1393 , HUMANIZE(stats.mean)
1394 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001395 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001396 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001397 , config
mtklein96289052014-09-10 12:05:59 -07001398 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001399 );
1400 }
joshualitte45c81c2015-12-02 09:05:37 -08001401
bsalomonb12ea412015-02-02 21:19:50 -08001402#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001403 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001404 GrContext* context = gGrFactory->get(configs[i].ctxType,
csmartdaltone812d492017-02-21 12:36:05 -07001405 configs[i].ctxOverrides);
kkinnunen5219fd92015-12-10 06:28:13 -08001406 context->printCacheStats();
1407 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001408 }
1409#endif
joshualitte45c81c2015-12-02 09:05:37 -08001410
cdalton2c56ba52015-06-26 13:32:53 -07001411 if (FLAGS_verbose) {
1412 SkDebugf("Samples: ");
1413 for (int i = 0; i < samples.count(); i++) {
1414 SkDebugf("%s ", HUMANIZE(samples[i]));
1415 }
1416 SkDebugf("%s\n", bench->getUniqueName());
1417 }
egdaniel3bf92062015-06-26 08:12:46 -07001418 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001419 }
mtkleinf3723212014-06-25 14:08:00 -07001420 }
1421
Herb Derby5a523fe2017-01-26 16:48:28 -05001422 SkGraphics::PurgeAllCaches();
1423
mtkleine1091452014-12-04 10:47:02 -08001424 log->bench("memory_usage", 0,0);
1425 log->config("meta");
1426 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1427
joshualitte0b19d42015-03-26 10:41:02 -07001428#if SK_SUPPORT_GPU
1429 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1430 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001431 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001432#endif
1433
mtkleinf3723212014-06-25 14:08:00 -07001434 return 0;
1435}