blob: 8cbaa2339ae9f5ece9e0da3e5fcf9da1754163ca [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"
csmartdalton008b9d82017-02-22 12:00:42 -070033#include "SkCommonFlagsPathRenderer.h"
msarett95f192d2015-02-13 09:05:41 -080034#include "SkData.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040035#include "SkDebugfTracer.h"
mtkleinf3723212014-06-25 14:08:00 -070036#include "SkGraphics.h"
halcanary4dbbd042016-06-07 17:21:10 -070037#include "SkLeanWindows.h"
mtklein20840502014-08-21 15:51:22 -070038#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050039#include "SkOSPath.h"
mtklein20840502014-08-21 15:51:22 -070040#include "SkPictureRecorder.h"
Hal Canary95e3c052017-01-11 12:44:43 -050041#include "SkSVGDOM.h"
42#include "SkScan.h"
mtkleinf3723212014-06-25 14:08:00 -070043#include "SkString.h"
44#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080045#include "SkTaskGroup.h"
msarettc149f0e2016-01-04 11:35:43 -080046#include "SkThreadUtils.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040047#include "Stats.h"
joshualitt3ebd0502016-02-09 07:18:08 -080048#include "ThermalManager.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040049#include "ios_utils.h"
mtkleinf3723212014-06-25 14:08:00 -070050
bungeman60e0fee2015-08-26 05:15:46 -070051#include <stdlib.h>
52
Mike Reedc928fe22017-06-05 10:20:31 -040053extern bool gSkForceRasterPipelineBlitter;
54
scroggo38ce0a72016-01-07 07:28:47 -080055#ifndef SK_BUILD_FOR_WIN32
56 #include <unistd.h>
57#endif
58
mtkleinbb6a0282014-07-01 08:43:42 -070059#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070060 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070061 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070062 #include "GrContextFactory.h"
bsalomon3724e572016-03-30 18:56:19 -070063 #include "gl/GrGLUtil.h"
64 using sk_gpu_test::GrContextFactory;
bsalomonc8699322016-05-11 11:55:36 -070065 using sk_gpu_test::TestContext;
Ben Wagner145dbcd2016-11-03 14:40:50 -040066 std::unique_ptr<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070067#endif
68
bsalomon682c2692015-05-22 14:01:46 -070069 struct GrContextOptions;
70
reed53249782014-10-10 09:09:52 -070071static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070072
Mike Kleind8ee67c2017-01-19 12:14:50 -050073#if !defined(__has_feature)
74 #define __has_feature(x) 0
75#endif
76
mtkleinb5110422014-08-07 15:20:02 -070077static const int kDefaultLoops =
Mike Kleind8ee67c2017-01-19 12:14:50 -050078#if defined(SK_DEBUG) || __has_feature(address_sanitizer)
bsalomon6eb03cc2014-08-07 14:28:50 -070079 1;
mtkleina189ccd2014-07-14 12:28:47 -070080#else
bsalomon6eb03cc2014-08-07 14:28:50 -070081 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070082#endif
83
bsalomon6eb03cc2014-08-07 14:28:50 -070084static SkString loops_help_txt() {
85 SkString help;
86 help.printf("Number of times to run each bench. Set this to %d to auto-"
87 "tune for each bench. Timings are only reported when auto-tuning.",
88 kAutoTuneLoops);
89 return help;
90}
91
cdaltone1b89582015-06-25 19:17:08 -070092static SkString to_string(int n) {
93 SkString str;
94 str.appendS32(n);
95 return str;
96}
97
bsalomon6eb03cc2014-08-07 14:28:50 -070098DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
99
mtkleinf3723212014-06-25 14:08:00 -0700100DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -0700101DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -0700102DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
103DEFINE_double(overheadGoal, 0.0001,
104 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -0700105DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -0700106DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -0700107
mtklein60317d0f2014-07-14 11:30:37 -0700108DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -0700109DEFINE_int32(maxCalibrationAttempts, 3,
110 "Try up to this many times to guess loops for a bench, or skip the bench.");
111DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700112DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
113DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700114DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
115 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700116DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
mtklein8c1a4f82016-08-08 06:56:22 -0700117DEFINE_bool(lite, false, "Use SkLiteRecorder in recording benchmarks?");
robertphillips5b693772014-11-21 06:19:36 -0800118DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700119DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700120DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800121DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800122DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800123DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
msarettc149f0e2016-01-04 11:35:43 -0800124DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
joshualitt3ebd0502016-02-09 07:18:08 -0800125DEFINE_string(useThermalManager, "0,1,10,1000", "enabled,threshold,sleepTimeMs,TimeoutMs for "
126 "thermalManager\n");
Mike Reedc665e5b2017-07-04 22:56:06 -0400127DEFINE_bool(csv, false, "Print status in CSV format");
mtklein65dfd2f2016-02-03 10:40:54 -0800128DEFINE_string(sourceType, "",
129 "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
130DEFINE_string(benchType, "",
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400131 "Apply usual --match rules to bench type: micro, recording, piping, playback, skcodec, etc.");
mtklein65dfd2f2016-02-03 10:40:54 -0800132
Mike Reedc928fe22017-06-05 10:20:31 -0400133DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
134
csmartdalton008b9d82017-02-22 12:00:42 -0700135#if SK_SUPPORT_GPU
136DEFINE_pathrenderer_flag;
137#endif
138
mtkleinbbba1682015-10-28 11:36:30 -0700139static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
140
mtkleinf3723212014-06-25 14:08:00 -0700141static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700142 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800143 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700144}
mtklein55b0ffc2014-07-17 08:38:23 -0700145#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700146
tomhudsond968a6f2015-03-26 11:28:06 -0700147bool Target::init(SkImageInfo info, Benchmark* bench) {
148 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700149 this->surface = SkSurface::MakeRaster(info);
150 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700151 return false;
152 }
153 }
154 return true;
155}
156bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700157 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700158 if (!canvas) {
159 return false;
160 }
Mike Reed12e946b2017-04-17 10:53:29 -0400161 bmp->allocPixels(canvas->imageInfo());
162 if (!canvas->readPixels(*bmp, 0, 0)) {
tomhudsond968a6f2015-03-26 11:28:06 -0700163 SkDebugf("Can't read canvas pixels.\n");
164 return false;
165 }
166 return true;
167}
168
169#if SK_SUPPORT_GPU
170struct GPUTarget : public Target {
bsalomonc8699322016-05-11 11:55:36 -0700171 explicit GPUTarget(const Config& c) : Target(c), context(nullptr) { }
172 TestContext* context;
tomhudsond968a6f2015-03-26 11:28:06 -0700173
174 void setup() override {
bsalomonc8699322016-05-11 11:55:36 -0700175 this->context->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700176 // Make sure we're done with whatever came before.
bsalomonc8699322016-05-11 11:55:36 -0700177 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700178 }
179 void endTiming() override {
bsalomonc8699322016-05-11 11:55:36 -0700180 if (this->context) {
181 this->context->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700182 }
183 }
184 void fence() override {
bsalomonc8699322016-05-11 11:55:36 -0700185 this->context->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700186 }
mtkleind75c4662015-04-30 07:11:22 -0700187
cdaltond416a5b2015-06-23 13:23:44 -0700188 bool needsFrameTiming(int* maxFrameLag) const override {
bsalomonc8699322016-05-11 11:55:36 -0700189 if (!this->context->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700190 // Frame lag is unknown.
191 *maxFrameLag = FLAGS_gpuFrameLag;
192 }
193 return true;
194 }
tomhudsond968a6f2015-03-26 11:28:06 -0700195 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700196 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
197 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700198 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700199 this->surface = SkSurface::MakeRenderTarget(gGrFactory->get(this->config.ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700200 this->config.ctxOverrides),
bsalomon5ec26ae2016-02-25 08:33:02 -0800201 SkBudgeted::kNo, info,
reede8f30622016-03-23 18:59:25 -0700202 this->config.samples, &props);
bsalomonc8699322016-05-11 11:55:36 -0700203 this->context = gGrFactory->getContextInfo(this->config.ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700204 this->config.ctxOverrides).testContext();
tomhudsond968a6f2015-03-26 11:28:06 -0700205 if (!this->surface.get()) {
206 return false;
207 }
bsalomonc8699322016-05-11 11:55:36 -0700208 if (!this->context->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700209 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800210 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700211 }
tomhudsond968a6f2015-03-26 11:28:06 -0700212 return true;
213 }
214 void fillOptions(ResultsWriter* log) override {
215 const GrGLubyte* version;
bsalomonc8699322016-05-11 11:55:36 -0700216 if (this->context->backend() == kOpenGL_GrBackend) {
217 const GrGLInterface* gl =
218 reinterpret_cast<const GrGLInterface*>(this->context->backendContext());
219 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
220 log->configOption("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700221
bsalomonc8699322016-05-11 11:55:36 -0700222 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
223 log->configOption("GL_RENDERER", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700224
bsalomonc8699322016-05-11 11:55:36 -0700225 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VENDOR));
226 log->configOption("GL_VENDOR", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700227
bsalomonc8699322016-05-11 11:55:36 -0700228 GR_GL_CALL_RET(gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
229 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
230 }
tomhudsond968a6f2015-03-26 11:28:06 -0700231 }
232};
mtkleind75c4662015-04-30 07:11:22 -0700233
tomhudsond968a6f2015-03-26 11:28:06 -0700234#endif
235
tomhudson75a0ebb2015-03-27 12:11:44 -0700236static double time(int loops, Benchmark* bench, Target* target) {
237 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700238 if (canvas) {
239 canvas->clear(SK_ColorWHITE);
240 }
joshualitt8a6697a2015-09-30 12:11:07 -0700241 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700242 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700243 canvas = target->beginTiming(canvas);
244 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700245 if (canvas) {
246 canvas->flush();
247 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700248 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700249 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700250 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700251 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700252}
253
mtkleinf3723212014-06-25 14:08:00 -0700254static double estimate_timer_overhead() {
255 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700256 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700257 double start = now_ms();
258 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700259 }
260 return overhead / FLAGS_overheadLoops;
261}
262
reed53249782014-10-10 09:09:52 -0700263static int detect_forever_loops(int loops) {
264 // look for a magic run-forever value
265 if (loops < 0) {
266 loops = SK_MaxS32;
267 }
268 return loops;
269}
270
mtklein55b0ffc2014-07-17 08:38:23 -0700271static int clamp_loops(int loops) {
272 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800273 SkDebugf("ERROR: clamping loops from %d to 1. "
274 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700275 return 1;
276 }
277 if (loops > FLAGS_maxLoops) {
278 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
279 return FLAGS_maxLoops;
280 }
281 return loops;
282}
283
tomhudsond968a6f2015-03-26 11:28:06 -0700284static bool write_canvas_png(Target* target, const SkString& filename) {
285
bsalomon6eb03cc2014-08-07 14:28:50 -0700286 if (filename.isEmpty()) {
287 return false;
288 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700289 if (target->getCanvas() &&
290 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700291 return false;
292 }
tomhudsond968a6f2015-03-26 11:28:06 -0700293
bsalomon6eb03cc2014-08-07 14:28:50 -0700294 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700295
296 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700297 return false;
298 }
tomhudsond968a6f2015-03-26 11:28:06 -0700299
bsalomon6eb03cc2014-08-07 14:28:50 -0700300 SkString dir = SkOSPath::Dirname(filename.c_str());
301 if (!sk_mkdir(dir.c_str())) {
302 SkDebugf("Can't make dir %s.\n", dir.c_str());
303 return false;
304 }
305 SkFILEWStream stream(filename.c_str());
306 if (!stream.isValid()) {
307 SkDebugf("Can't write %s.\n", filename.c_str());
308 return false;
309 }
Hal Canarydb683012016-11-23 08:55:18 -0700310 if (!SkEncodeImage(&stream, bmp, SkEncodedImageFormat::kPNG, 100)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700311 SkDebugf("Can't encode a PNG.\n");
312 return false;
313 }
314 return true;
315}
316
317static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700318static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700319 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700320 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700321 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700322 int loops = bench->calculateLoops(FLAGS_loops);
323 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700324 while (bench_plus_overhead < overhead) {
325 if (round++ == FLAGS_maxCalibrationAttempts) {
326 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700327 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700328 return kFailedLoops;
329 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700330 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700331 }
mtklein2069e222014-08-04 13:57:39 -0700332 }
mtkleinf3723212014-06-25 14:08:00 -0700333
mtkleinbb6a0282014-07-01 08:43:42 -0700334 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700335 // We'll pick N to make timer overhead negligible:
336 //
mtkleinbb6a0282014-07-01 08:43:42 -0700337 // overhead
338 // ------------------------- < FLAGS_overheadGoal
339 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700340 //
Hal Canary55325b72017-01-03 10:36:17 -0500341 // where bench_plus_overhead ~=~ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700342 //
343 // Doing some math, we get:
344 //
mtkleinbb6a0282014-07-01 08:43:42 -0700345 // (overhead / FLAGS_overheadGoal) - overhead
346 // ------------------------------------------ < N
347 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700348 //
349 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700350 if (kAutoTuneLoops == loops) {
351 const double numer = overhead / FLAGS_overheadGoal - overhead;
352 const double denom = bench_plus_overhead - overhead;
353 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700354 loops = clamp_loops(loops);
355 } else {
356 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700357 }
mtkleinbb6a0282014-07-01 08:43:42 -0700358
mtkleinbb6a0282014-07-01 08:43:42 -0700359 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700360}
361
cdaltone1b89582015-06-25 19:17:08 -0700362static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700363 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700364 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700365 if (kAutoTuneLoops == loops) {
366 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700367 double elapsed = 0;
368 do {
mtklein527930f2014-11-06 08:04:34 -0800369 if (1<<30 == loops) {
370 // We're about to wrap. Something's wrong with the bench.
371 loops = 0;
372 break;
373 }
mtkleina189ccd2014-07-14 12:28:47 -0700374 loops *= 2;
375 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700376 // _this_ round, not still timing last round.
377 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700378 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700379 }
380 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700381
mtkleina189ccd2014-07-14 12:28:47 -0700382 // We've overshot at least a little. Scale back linearly.
383 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700384 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700385
tomhudsond968a6f2015-03-26 11:28:06 -0700386 // Make sure we're not still timing our calibration.
387 target->fence();
reed53249782014-10-10 09:09:52 -0700388 } else {
389 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700390 }
mtkleinbb6a0282014-07-01 08:43:42 -0700391
392 // Pretty much the same deal as the calibration: do some warmup to make
393 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700394 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700395 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700396 }
mtkleinbb6a0282014-07-01 08:43:42 -0700397
mtkleinbb6a0282014-07-01 08:43:42 -0700398 return loops;
399}
mtkleinbb6a0282014-07-01 08:43:42 -0700400
bsalomonc2553372014-07-22 13:09:05 -0700401#if SK_SUPPORT_GPU
Brian Salomon6405e712017-03-20 08:54:16 -0400402#define kBogusContextType GrContextFactory::kGL_ContextType
csmartdaltone812d492017-02-21 12:36:05 -0700403#define kBogusContextOverrides GrContextFactory::ContextOverrides::kNone
bsalomonc2553372014-07-22 13:09:05 -0700404#else
bsalomon85b4b532016-04-05 11:06:27 -0700405#define kBogusContextType 0
csmartdaltone812d492017-02-21 12:36:05 -0700406#define kBogusContextOverrides 0
mtkleine714e752014-07-31 12:13:48 -0700407#endif
bsalomonc2553372014-07-22 13:09:05 -0700408
svaisanenc47635e2016-01-28 06:05:43 -0800409static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
410
411#if SK_SUPPORT_GPU
412 if (const auto* gpuConfig = config->asConfigGpu()) {
413 if (!FLAGS_gpu)
414 return;
415
svaisanenc47635e2016-01-28 06:05:43 -0800416 const auto ctxType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700417 const auto ctxOverrides = gpuConfig->getContextOverrides();
svaisanenc47635e2016-01-28 06:05:43 -0800418 const auto sampleCount = gpuConfig->getSamples();
419
csmartdaltone812d492017-02-21 12:36:05 -0700420 if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOverrides)) {
svaisanenc47635e2016-01-28 06:05:43 -0800421 const auto maxSampleCount = ctx->caps()->maxSampleCount();
422 if (sampleCount > ctx->caps()->maxSampleCount()) {
423 SkDebugf("Configuration sample count %d exceeds maximum %d.\n",
424 sampleCount, maxSampleCount);
425 return;
426 }
427 } else {
428 SkDebugf("No context was available matching config type and options.\n");
429 return;
430 }
431
432 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800433 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800434 Benchmark::kGPU_Backend,
brianosman1a48e132016-06-20 09:41:41 -0700435 gpuConfig->getColorType(),
svaisanenc47635e2016-01-28 06:05:43 -0800436 kPremul_SkAlphaType,
brianosman1a48e132016-06-20 09:41:41 -0700437 sk_ref_sp(gpuConfig->getColorSpace()),
svaisanenc47635e2016-01-28 06:05:43 -0800438 sampleCount,
439 ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700440 ctxOverrides,
kkinnunend4e1c352016-03-01 23:41:26 -0800441 gpuConfig->getUseDIText()
442 };
svaisanenc47635e2016-01-28 06:05:43 -0800443
444 configs->push_back(target);
445 return;
446 }
447#endif
448
brianosmanb109b8c2016-06-16 13:03:24 -0700449 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
450 if (config->getTag().equals(#name)) { \
451 Config config = { \
452 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
csmartdaltone812d492017-02-21 12:36:05 -0700453 0, kBogusContextType, kBogusContextOverrides, false \
brianosmanb109b8c2016-06-16 13:03:24 -0700454 }; \
455 configs->push_back(config); \
456 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700457 }
mtkleine714e752014-07-31 12:13:48 -0700458
mtklein40b32be2014-07-09 08:46:49 -0700459 if (FLAGS_cpu) {
mtkleinbb6c41b2016-03-08 11:31:11 -0800460 CPU_CONFIG(nonrendering, kNonRendering_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700461 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800462
463 CPU_CONFIG(8888, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700464 kN32_SkColorType, kPremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800465 CPU_CONFIG(565, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700466 kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500467 auto srgbColorSpace = SkColorSpace::MakeSRGB();
mtkleinbb6c41b2016-03-08 11:31:11 -0800468 CPU_CONFIG(srgb, kRaster_Backend,
brianosmanb109b8c2016-06-16 13:03:24 -0700469 kN32_SkColorType, kPremul_SkAlphaType, srgbColorSpace)
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500470 auto srgbLinearColorSpace = SkColorSpace::MakeSRGBLinear();
mtkleinbb6c41b2016-03-08 11:31:11 -0800471 CPU_CONFIG(f16, kRaster_Backend,
raftias7c602de2016-10-13 10:45:44 -0700472 kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbLinearColorSpace)
mtklein40b32be2014-07-09 08:46:49 -0700473 }
mtkleinbb6a0282014-07-01 08:43:42 -0700474
svaisanenc47635e2016-01-28 06:05:43 -0800475 #undef CPU_CONFIG
mtkleinf3723212014-06-25 14:08:00 -0700476}
477
svaisanenc47635e2016-01-28 06:05:43 -0800478// Append all configs that are enabled and supported.
479void create_configs(SkTArray<Config>* configs) {
480 SkCommandLineConfigArray array;
481 ParseConfigs(FLAGS_config, &array);
482 for (int i = 0; i < array.count(); ++i) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400483 create_config(array[i].get(), configs);
svaisanenc47635e2016-01-28 06:05:43 -0800484 }
485}
486
brianosman9f1f6e22016-09-15 08:33:02 -0700487// disable warning : switch statement contains default but no 'case' labels
488#if defined _WIN32
489#pragma warning ( push )
490#pragma warning ( disable : 4065 )
491#endif
492
halcanary96fcdcc2015-08-27 07:41:13 -0700493// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700494static Target* is_enabled(Benchmark* bench, const Config& config) {
495 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700496 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700497 }
498
reede5ea5002014-09-03 11:54:58 -0700499 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700500 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700501
halcanary96fcdcc2015-08-27 07:41:13 -0700502 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700503
tomhudsond968a6f2015-03-26 11:28:06 -0700504 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700505#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700506 case Benchmark::kGPU_Backend:
507 target = new GPUTarget(config);
508 break;
bsalomonc2553372014-07-22 13:09:05 -0700509#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700510 default:
511 target = new Target(config);
512 break;
513 }
bsalomonc2553372014-07-22 13:09:05 -0700514
tomhudsond968a6f2015-03-26 11:28:06 -0700515 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700516 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700517 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700518 }
519 return target;
520}
521
brianosman9f1f6e22016-09-15 08:33:02 -0700522#if defined _WIN32
523#pragma warning ( pop )
524#endif
525
reed42943c82016-09-12 12:01:44 -0700526static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700527 uint32_t minOutputSize, int* width, int* height) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400528 std::unique_ptr<SkBitmapRegionDecoder> brd(
msarettd1227a72016-05-18 06:23:57 -0700529 SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy));
msarett7f691442015-09-22 11:56:16 -0700530 if (nullptr == brd.get()) {
531 // This is indicates that subset decoding is not supported for a particular image format.
532 return false;
533 }
534
msarett7f691442015-09-22 11:56:16 -0700535 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
536 (uint32_t) brd->height()) {
537 // This indicates that the image is not large enough to decode a
538 // minOutputSize x minOutputSize subset at the given sampleSize.
539 return false;
540 }
541
542 // Set the image width and height. The calling code will use this to choose subsets to decode.
543 *width = brd->width();
544 *height = brd->height();
545 return true;
546}
547
egdaniel3bf92062015-06-26 08:12:46 -0700548static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700549 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700550#if SK_SUPPORT_GPU
551 if (FLAGS_abandonGpuContext) {
552 gGrFactory->abandonContexts();
553 }
554 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
555 gGrFactory->destroyContexts();
556 }
557#endif
558}
559
fmalita6519c212016-09-14 08:05:17 -0700560static void collect_files(const SkCommandLineFlags::StringArray& paths, const char* ext,
561 SkTArray<SkString>* list) {
562 for (int i = 0; i < paths.count(); ++i) {
563 if (SkStrEndsWith(paths[i], ext)) {
564 list->push_back(SkString(paths[i]));
565 } else {
566 SkOSFile::Iter it(paths[i], ext);
567 SkString path;
568 while (it.next(&path)) {
569 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
570 }
571 }
572 }
573}
574
mtkleine714e752014-07-31 12:13:48 -0700575class BenchmarkStream {
576public:
mtklein92007582014-08-01 07:46:52 -0700577 BenchmarkStream() : fBenches(BenchRegistry::Head())
578 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700579 , fCurrentRecording(0)
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400580 , fCurrentPiping(0)
mtklein92007582014-08-01 07:46:52 -0700581 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800582 , fCurrentSKP(0)
fmalita6519c212016-09-14 08:05:17 -0700583 , fCurrentSVG(0)
msarett95f192d2015-02-13 09:05:41 -0800584 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700585 , fCurrentCodec(0)
msarett84451022016-02-11 06:45:51 -0800586 , fCurrentAndroidCodec(0)
msarett7f691442015-09-22 11:56:16 -0700587 , fCurrentBRDImage(0)
msarett2cee9022016-06-03 08:25:21 -0700588 , fCurrentColorImage(0)
msarett95f192d2015-02-13 09:05:41 -0800589 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800590 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700591 , fCurrentSubsetType(0)
msarett84451022016-02-11 06:45:51 -0800592 , fCurrentSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700593 , fCurrentAnimSKP(0) {
fmalita6519c212016-09-14 08:05:17 -0700594 collect_files(FLAGS_skps, ".skp", &fSKPs);
595 collect_files(FLAGS_svgs, ".svg", &fSVGs);
mtkleine714e752014-07-31 12:13:48 -0700596
mtklein92007582014-08-01 07:46:52 -0700597 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
598 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
599 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
600 exit(1);
601 }
602
603 for (int i = 0; i < FLAGS_scales.count(); i++) {
604 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
605 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
606 exit(1);
607 }
608 }
robertphillips5b693772014-11-21 06:19:36 -0800609
cdalton63a82852015-06-29 14:06:10 -0700610 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
611 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700612 exit(1);
613 }
614
robertphillips5b693772014-11-21 06:19:36 -0800615 if (FLAGS_mpd) {
616 fUseMPDs.push_back() = true;
617 }
mtkleinc751ecb2015-06-15 08:56:38 -0700618 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700619
msarett95f192d2015-02-13 09:05:41 -0800620 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700621 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800622 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800623 }
msarett2cee9022016-06-03 08:25:21 -0700624 if (!CollectImages(FLAGS_colorImages, &fColorImages)) {
625 exit(1);
626 }
mtklein95553d92015-03-12 08:24:21 -0700627
msarett95f192d2015-02-13 09:05:41 -0800628 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700629 fColorTypes.push_back(kN32_SkColorType);
630 if (!FLAGS_simpleCodec) {
631 fColorTypes.push_back(kRGB_565_SkColorType);
632 fColorTypes.push_back(kAlpha_8_SkColorType);
msarett67cb6662016-06-21 08:49:26 -0700633 fColorTypes.push_back(kGray_8_SkColorType);
634 }
mtklein92007582014-08-01 07:46:52 -0700635 }
636
reedca2622b2016-03-18 07:25:55 -0700637 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700638 // Not strictly necessary, as it will be checked again later,
639 // but helps to avoid a lot of pointless work if we're going to skip it.
cdalton91e457d2016-02-17 11:10:16 -0800640 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700641 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700642 }
643
bungemanf93d7112016-09-16 06:24:20 -0700644 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
645 if (!stream) {
mtkleinfd731ce2014-09-10 12:19:30 -0700646 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700647 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700648 }
649
reedca2622b2016-03-18 07:25:55 -0700650 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700651 }
652
fmalita6519c212016-09-14 08:05:17 -0700653 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
654 SkFILEStream stream(path);
655 if (!stream.isValid()) {
656 SkDebugf("Could not read %s.\n", path);
657 return nullptr;
658 }
659
fmalitae1baa7c2016-09-14 12:04:30 -0700660 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
fmalita6519c212016-09-14 08:05:17 -0700661 if (!svgDom) {
662 SkDebugf("Could not parse %s.\n", path);
663 return nullptr;
664 }
665
fmalitae1baa7c2016-09-14 12:04:30 -0700666 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
667 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
668 if (svgDom->containerSize().isEmpty()) {
669 svgDom->setContainerSize(kDefaultContainerSize);
670 }
671
fmalita6519c212016-09-14 08:05:17 -0700672 SkPictureRecorder recorder;
fmalitae1baa7c2016-09-14 12:04:30 -0700673 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
674 svgDom->containerSize().height()));
fmalita6519c212016-09-14 08:05:17 -0700675 return recorder.finishRecordingAsPicture();
676 }
677
mtklein92007582014-08-01 07:46:52 -0700678 Benchmark* next() {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400679 std::unique_ptr<Benchmark> bench;
mtklein65dfd2f2016-02-03 10:40:54 -0800680 do {
681 bench.reset(this->rawNext());
682 if (!bench) {
683 return nullptr;
684 }
685 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
686 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700687 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800688 }
689
690 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700691 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700692 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700693 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700694 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700695 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700696 return bench;
697 }
mtklein92007582014-08-01 07:46:52 -0700698
mtkleine714e752014-07-31 12:13:48 -0700699 while (fGMs) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400700 std::unique_ptr<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700701 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800702 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700703 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700704 fBenchType = "micro";
mtklein18300a32016-03-16 13:53:35 -0700705 return new GMBench(gm.release());
mtkleine714e752014-07-31 12:13:48 -0700706 }
707 }
mtklein92007582014-08-01 07:46:52 -0700708
mtkleinfd731ce2014-09-10 12:19:30 -0700709 // First add all .skps as RecordingBenches.
710 while (fCurrentRecording < fSKPs.count()) {
711 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700712 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
713 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700714 continue;
715 }
716 SkString name = SkOSPath::Basename(path.c_str());
717 fSourceType = "skp";
718 fBenchType = "recording";
Mike Reed90b20052017-03-08 10:39:02 -0500719 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
mtklein051e56d2014-12-04 08:46:51 -0800720 fSKPOps = pic->approximateOpCount();
mtklein8c1a4f82016-08-08 06:56:22 -0700721 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh, FLAGS_lite);
mtkleinfd731ce2014-09-10 12:19:30 -0700722 }
723
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400724 // Add all .skps as PipeBenches.
725 while (fCurrentPiping < fSKPs.count()) {
726 const SkString& path = fSKPs[fCurrentPiping++];
727 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
728 if (!pic) {
729 continue;
730 }
731 SkString name = SkOSPath::Basename(path.c_str());
732 fSourceType = "skp";
733 fBenchType = "piping";
Mike Reed90b20052017-03-08 10:39:02 -0500734 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400735 fSKPOps = pic->approximateOpCount();
736 return new PipingBench(name.c_str(), pic.get());
737 }
738
mtkleinfd731ce2014-09-10 12:19:30 -0700739 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700740 while (fCurrentScale < fScales.count()) {
741 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800742 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700743 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
744 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800745 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700746 continue;
747 }
robertphillips5b693772014-11-21 06:19:36 -0800748
749 while (fCurrentUseMPD < fUseMPDs.count()) {
750 if (FLAGS_bbh) {
751 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
752 SkRTreeFactory factory;
753 SkPictureRecorder recorder;
robertphillips5b693772014-11-21 06:19:36 -0800754 pic->playback(recorder.beginRecording(pic->cullRect().width(),
755 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800756 &factory,
robertphillipsdda54452016-07-13 13:27:16 -0700757 0));
reedca2622b2016-03-18 07:25:55 -0700758 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800759 }
760 SkString name = SkOSPath::Basename(path.c_str());
761 fSourceType = "skp";
762 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700763 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
764 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700765 }
robertphillips5b693772014-11-21 06:19:36 -0800766 fCurrentUseMPD = 0;
767 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700768 }
fmalita6519c212016-09-14 08:05:17 -0700769
770 while (fCurrentSVG++ < fSVGs.count()) {
771 const char* path = fSVGs[fCurrentSVG - 1].c_str();
772 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
773 fSourceType = "svg";
774 fBenchType = "playback";
775 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
776 fScales[fCurrentScale], false, FLAGS_loopSKP);
777 }
778 }
779
mtklein92007582014-08-01 07:46:52 -0700780 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700781 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700782 fCurrentScale++;
783 }
784
joshualitt261c3ad2015-04-27 09:16:57 -0700785 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700786 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700787 while (fCurrentAnimSKP < fSKPs.count()) {
788 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700789 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
790 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700791 fCurrentAnimSKP++;
792 continue;
793 }
794
795 fCurrentAnimSKP++;
796 SkString name = SkOSPath::Basename(path.c_str());
Hal Canary2db83612016-11-04 13:02:54 -0400797 sk_sp<SKPAnimationBench::Animation> animation(
cdalton63a82852015-06-29 14:06:10 -0700798 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
Hal Canary2db83612016-11-04 13:02:54 -0400799 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation.get(),
halcanary385fe4d2015-08-26 13:07:48 -0700800 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700801 }
802 }
803
scroggo60869a42015-04-01 12:09:17 -0700804 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700805 fSourceType = "image";
806 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700807 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800808 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
809 continue;
810 }
bungeman38d909e2016-08-02 14:40:46 -0700811 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Ben Wagner145dbcd2016-11-03 14:40:50 -0400812 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700813 if (!codec) {
814 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700815 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700816 continue;
817 }
scroggo21027992015-04-02 13:22:38 -0700818
scroggo60869a42015-04-01 12:09:17 -0700819 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700820 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700821
msarettc7796b92016-01-07 14:20:20 -0800822 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700823 if (FLAGS_simpleCodec) {
824 if (kUnpremul_SkAlphaType == alphaType) {
825 alphaType = kPremul_SkAlphaType;
826 }
827
828 fCurrentColorType++;
829 } else {
830 switch (alphaType) {
831 case kOpaque_SkAlphaType:
832 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800833 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700834 break;
835 case kUnpremul_SkAlphaType:
836 case kPremul_SkAlphaType:
837 if (0 == fCurrentAlphaType) {
838 // Test unpremul first.
839 alphaType = kUnpremul_SkAlphaType;
840 fCurrentAlphaType++;
841 } else {
842 // Test premul.
843 alphaType = kPremul_SkAlphaType;
844 fCurrentAlphaType = 0;
845 fCurrentColorType++;
846 }
847 break;
848 default:
849 SkASSERT(false);
850 fCurrentColorType++;
851 break;
852 }
scroggo21027992015-04-02 13:22:38 -0700853 }
854
msarettc7796b92016-01-07 14:20:20 -0800855 // Make sure we can decode to this color type and alpha type.
856 SkImageInfo info =
857 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700858 const size_t rowBytes = info.minRowBytes();
859 SkAutoMalloc storage(info.getSafeSize(rowBytes));
860
scroggoeb602a52015-07-09 08:16:03 -0700861 const SkCodec::Result result = codec->getPixels(
Leon Scroggins571b30f2017-07-11 17:35:31 +0000862 info, storage.get(), rowBytes);
scroggo60869a42015-04-01 12:09:17 -0700863 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700864 case SkCodec::kSuccess:
865 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700866 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700867 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700868 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700869 // This is okay. Not all conversions are valid.
870 break;
scroggo60869a42015-04-01 12:09:17 -0700871 default:
872 // This represents some sort of failure.
873 SkASSERT(false);
874 break;
875 }
876 }
877 fCurrentColorType = 0;
878 }
879
msarett84451022016-02-11 06:45:51 -0800880 // Run AndroidCodecBenches
881 const int sampleSizes[] = { 2, 4, 8 };
882 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
883 fSourceType = "image";
884 fBenchType = "skandroidcodec";
885
886 const SkString& path = fImages[fCurrentAndroidCodec];
887 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
888 continue;
889 }
bungeman38d909e2016-08-02 14:40:46 -0700890 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Ben Wagner145dbcd2016-11-03 14:40:50 -0400891 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800892 if (!codec) {
893 // Nothing to time.
894 SkDebugf("Cannot find codec for %s\n", path.c_str());
895 continue;
896 }
897
898 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
899 int sampleSize = sampleSizes[fCurrentSampleSize];
900 fCurrentSampleSize++;
901 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
902 // Avoid benchmarking scaled decodes of already small images.
903 break;
904 }
905
bungeman38d909e2016-08-02 14:40:46 -0700906 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
907 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -0800908 }
909 fCurrentSampleSize = 0;
910 }
911
msarett7f691442015-09-22 11:56:16 -0700912 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700913 // We intend to create benchmarks that model the use cases in
914 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
915 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
916 // the image. For that reason, we will benchmark decodes in five representative locations
917 // in the image. Additionally, this use case utilizes power of two scaling, so we will
918 // test on power of two sample sizes. The output tile is always 512x512, so, when a
919 // sampleSize is used, the size of the subset that is decoded is always
920 // (sampleSize*512)x(sampleSize*512).
921 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700922 // All use cases we are aware of only scale by powers of two.
923 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
924 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800925 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700926 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800927 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700928 fSourceType = "image";
929 fBenchType = "BRD";
930
mtklein6f0ff912016-01-11 09:04:21 -0800931 const SkString& path = fImages[fCurrentBRDImage];
932 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
933 continue;
934 }
scroggo860e8a62015-10-15 07:51:28 -0700935
msarettd1227a72016-05-18 06:23:57 -0700936 while (fCurrentColorType < fColorTypes.count()) {
937 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
938 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700939
bungeman38d909e2016-08-02 14:40:46 -0700940 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -0700941 const SkColorType colorType = fColorTypes[fCurrentColorType];
942 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
943 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -0700944
msarettd1227a72016-05-18 06:23:57 -0700945 int width = 0;
946 int height = 0;
reed42943c82016-09-12 12:01:44 -0700947 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -0700948 &width, &height)) {
949 break;
msarett7f691442015-09-22 11:56:16 -0700950 }
msarettd1227a72016-05-18 06:23:57 -0700951
952 SkString basename = SkOSPath::Basename(path.c_str());
953 SkIRect subset;
954 const uint32_t subsetSize = sampleSize * minOutputSize;
955 switch (currentSubsetType) {
956 case kTopLeft_SubsetType:
957 basename.append("_TopLeft");
958 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
959 break;
960 case kTopRight_SubsetType:
961 basename.append("_TopRight");
962 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
963 subsetSize);
964 break;
965 case kMiddle_SubsetType:
966 basename.append("_Middle");
967 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
968 (height - subsetSize) / 2, subsetSize, subsetSize);
969 break;
970 case kBottomLeft_SubsetType:
971 basename.append("_BottomLeft");
972 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
973 subsetSize);
974 break;
975 case kBottomRight_SubsetType:
976 basename.append("_BottomRight");
977 subset = SkIRect::MakeXYWH(width - subsetSize,
978 height - subsetSize, subsetSize, subsetSize);
979 break;
980 default:
981 SkASSERT(false);
982 }
983
984 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
985 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -0700986 }
msarettd1227a72016-05-18 06:23:57 -0700987 fCurrentSubsetType = 0;
988 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -0700989 }
msarettd1227a72016-05-18 06:23:57 -0700990 fCurrentSampleSize = 0;
991 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -0700992 }
msarettd1227a72016-05-18 06:23:57 -0700993 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -0700994 }
995
msarett2cee9022016-06-03 08:25:21 -0700996 while (fCurrentColorImage < fColorImages.count()) {
997 fSourceType = "colorimage";
998 fBenchType = "skcolorcodec";
999 const SkString& path = fColorImages[fCurrentColorImage];
1000 fCurrentColorImage++;
1001 sk_sp<SkData> encoded = SkData::MakeFromFileName(path.c_str());
1002 if (encoded) {
1003 return new ColorCodecBench(SkOSPath::Basename(path.c_str()).c_str(),
1004 std::move(encoded));
1005 } else {
1006 SkDebugf("Could not read file %s.\n", path.c_str());
1007 }
1008 }
1009
halcanary96fcdcc2015-08-27 07:41:13 -07001010 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001011 }
mtklein92007582014-08-01 07:46:52 -07001012
1013 void fillCurrentOptions(ResultsWriter* log) const {
1014 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001015 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001016 if (0 == strcmp(fSourceType, "skp")) {
1017 log->configOption("clip",
1018 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1019 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001020 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -07001021 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001022 if (fCurrentUseMPD > 0) {
1023 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1024 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1025 }
mtklein92007582014-08-01 07:46:52 -07001026 }
mtklein051e56d2014-12-04 08:46:51 -08001027 if (0 == strcmp(fBenchType, "recording")) {
1028 log->metric("bytes", fSKPBytes);
1029 log->metric("ops", fSKPOps);
1030 }
mtklein92007582014-08-01 07:46:52 -07001031 }
1032
mtkleine714e752014-07-31 12:13:48 -07001033private:
msarettb23e6aa2015-06-09 13:56:10 -07001034 enum SubsetType {
1035 kTopLeft_SubsetType = 0,
1036 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001037 kMiddle_SubsetType = 2,
1038 kBottomLeft_SubsetType = 3,
1039 kBottomRight_SubsetType = 4,
1040 kTranslate_SubsetType = 5,
1041 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001042 kLast_SubsetType = kZoom_SubsetType,
1043 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001044 };
1045
mtkleine714e752014-07-31 12:13:48 -07001046 const BenchRegistry* fBenches;
1047 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001048 SkIRect fClip;
1049 SkTArray<SkScalar> fScales;
1050 SkTArray<SkString> fSKPs;
fmalita6519c212016-09-14 08:05:17 -07001051 SkTArray<SkString> fSVGs;
robertphillips5b693772014-11-21 06:19:36 -08001052 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001053 SkTArray<SkString> fImages;
msarett2cee9022016-06-03 08:25:21 -07001054 SkTArray<SkString> fColorImages;
msarett74deb982015-10-20 16:45:56 -07001055 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001056 SkScalar fZoomMax;
1057 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001058
mtklein051e56d2014-12-04 08:46:51 -08001059 double fSKPBytes, fSKPOps;
1060
mtkleinfd731ce2014-09-10 12:19:30 -07001061 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1062 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1063 int fCurrentRecording;
Mike Reed9cdd2ab2016-10-21 10:43:36 -04001064 int fCurrentPiping;
mtklein92007582014-08-01 07:46:52 -07001065 int fCurrentScale;
1066 int fCurrentSKP;
fmalita6519c212016-09-14 08:05:17 -07001067 int fCurrentSVG;
robertphillips5b693772014-11-21 06:19:36 -08001068 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001069 int fCurrentCodec;
msarett84451022016-02-11 06:45:51 -08001070 int fCurrentAndroidCodec;
msarett7f691442015-09-22 11:56:16 -07001071 int fCurrentBRDImage;
msarett2cee9022016-06-03 08:25:21 -07001072 int fCurrentColorImage;
msarett95f192d2015-02-13 09:05:41 -08001073 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -08001074 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -07001075 int fCurrentSubsetType;
msarett84451022016-02-11 06:45:51 -08001076 int fCurrentSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001077 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001078};
1079
msarettc149f0e2016-01-04 11:35:43 -08001080// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1081// This prints something every once in a while so that it knows we're still working.
1082static void start_keepalive() {
1083 struct Loop {
1084 static void forever(void*) {
1085 for (;;) {
1086 static const int kSec = 1200;
1087 #if defined(SK_BUILD_FOR_WIN)
1088 Sleep(kSec * 1000);
1089 #else
1090 sleep(kSec);
1091 #endif
1092 SkDebugf("\nBenchmarks still running...\n");
1093 }
1094 }
1095 };
1096 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1097 intentionallyLeaked->start();
1098}
1099
Mike Kleinbe28ee22017-02-06 12:46:20 -05001100int main(int argc, char** argv) {
1101 SkCommandLineFlags::Parse(argc, argv);
Brian Salomondcbb9d92017-07-19 10:53:20 -04001102 if (FLAGS_trace) {
1103 SkEventTracer::SetInstance(new SkDebugfTracer);
1104 }
Mike Kleinadacaef2017-02-06 09:26:14 -05001105#if defined(SK_BUILD_FOR_IOS)
1106 cd_Documents();
1107#endif
jcgregorio3b27ade2014-11-13 08:06:40 -08001108 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001109 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001110 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001111
krajcevski69a55602014-08-13 10:46:31 -07001112#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001113 GrContextOptions grContextOpts;
csmartdalton008b9d82017-02-22 12:00:42 -07001114 grContextOpts.fGpuPathRenderers = CollectGpuPathRenderersFromFlags();
halcanary385fe4d2015-08-26 13:07:48 -07001115 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001116#endif
1117
bsalomon06cddec2014-10-24 10:40:50 -07001118 if (FLAGS_veryVerbose) {
1119 FLAGS_verbose = true;
1120 }
1121
bsalomon6eb03cc2014-08-07 14:28:50 -07001122 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001123 FLAGS_samples = 1;
1124 FLAGS_gpuFrameLag = 0;
1125 }
1126
bsalomon6eb03cc2014-08-07 14:28:50 -07001127 if (!FLAGS_writePath.isEmpty()) {
1128 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1129 if (!sk_mkdir(FLAGS_writePath[0])) {
1130 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001131 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001132 }
1133 }
1134
Ben Wagner145dbcd2016-11-03 14:40:50 -04001135 std::unique_ptr<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001136 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001137#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001138 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001139#else
1140 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1141 return 1;
1142#endif
mtklein60317d0f2014-07-14 11:30:37 -07001143 }
mtklein1915b622014-08-20 11:45:00 -07001144
1145 if (1 == FLAGS_properties.count() % 2) {
1146 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1147 return 1;
1148 }
1149 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1150 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1151 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001152
1153 if (1 == FLAGS_key.count() % 2) {
1154 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1155 return 1;
1156 }
1157 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001158 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001159 }
mtklein60317d0f2014-07-14 11:30:37 -07001160
mtkleinf3723212014-06-25 14:08:00 -07001161 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001162 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001163
cdaltone1b89582015-06-25 19:17:08 -07001164 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001165
bsalomon6eb03cc2014-08-07 14:28:50 -07001166 if (kAutoTuneLoops != FLAGS_loops) {
1167 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001168 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001169 SkDebugf("! -> high variance, ? -> moderate variance\n");
1170 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001171 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001172 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001173 } else {
mtkleind75c4662015-04-30 07:11:22 -07001174 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001175 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001176 }
1177
svaisanenc47635e2016-01-28 06:05:43 -08001178 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001179 create_configs(&configs);
1180
joshualitt3ebd0502016-02-09 07:18:08 -08001181#ifdef THERMAL_MANAGER_SUPPORTED
1182 int tmEnabled, tmThreshold, tmSleepTimeMs, tmTimeoutMs;
1183 if (4 != sscanf(FLAGS_useThermalManager[0], "%d,%d,%d,%d",
1184 &tmEnabled, &tmThreshold, &tmSleepTimeMs, &tmTimeoutMs)) {
1185 SkDebugf("Can't parse %s from --useThermalManager.\n", FLAGS_useThermalManager[0]);
1186 exit(1);
1187 }
1188 ThermalManager tm(tmThreshold, tmSleepTimeMs, tmTimeoutMs);
1189#endif
1190
msarettc149f0e2016-01-04 11:35:43 -08001191 if (FLAGS_keepAlive) {
1192 start_keepalive();
1193 }
1194
Yuqian Liba62b4a2016-12-14 13:46:53 -05001195 gSkUseAnalyticAA = FLAGS_analyticAA;
liyuqian38911a72016-10-04 11:23:22 -07001196
Yuqian Li550148b2017-01-13 10:13:13 -05001197 if (FLAGS_forceAnalyticAA) {
1198 gSkForceAnalyticAA = true;
1199 }
Mike Reedc928fe22017-06-05 10:20:31 -04001200 if (FLAGS_forceRasterPipeline) {
1201 gSkForceRasterPipelineBlitter = true;
1202 }
Yuqian Li550148b2017-01-13 10:13:13 -05001203
mtkleine070c2b2014-10-14 08:40:43 -07001204 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001205 BenchmarkStream benchStream;
1206 while (Benchmark* b = benchStream.next()) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001207 std::unique_ptr<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001208 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001209 continue;
1210 }
1211
svaisanenc47635e2016-01-28 06:05:43 -08001212 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001213 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001214 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001215 }
egdaniel3bf92062015-06-26 08:12:46 -07001216 for (int i = 0; i < configs.count(); ++i) {
joshualitt3ebd0502016-02-09 07:18:08 -08001217#ifdef THERMAL_MANAGER_SUPPORTED
1218 if (tmEnabled && !tm.coolOffIfNecessary()) {
1219 SkDebugf("Could not cool off, timings will be throttled\n");
1220 }
1221#endif
egdaniel3bf92062015-06-26 08:12:46 -07001222 Target* target = is_enabled(b, configs[i]);
1223 if (!target) {
1224 continue;
1225 }
mtkleinf3723212014-06-25 14:08:00 -07001226
halcanary96fcdcc2015-08-27 07:41:13 -07001227 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001228 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001229 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001230
brianosman7a5ada82016-02-08 13:49:12 -08001231 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001232 SkDebugf("Running %s\t%s\n"
1233 , bench->getUniqueName()
1234 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001235 if (FLAGS_dryRun) {
1236 continue;
1237 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001238 }
1239
egdaniel3bf92062015-06-26 08:12:46 -07001240 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001241 bench->perCanvasPreDraw(canvas);
1242
cdaltone1b89582015-06-25 19:17:08 -07001243 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001244 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001245 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1246 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001247
mtkleinbbba1682015-10-28 11:36:30 -07001248 if (FLAGS_ms) {
1249 samples.reset();
1250 auto stop = now_ms() + FLAGS_ms;
1251 do {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001252 samples.push_back(time(loops, bench.get(), target) / loops);
mtkleinbbba1682015-10-28 11:36:30 -07001253 } while (now_ms() < stop);
1254 } else {
cdaltone1b89582015-06-25 19:17:08 -07001255 samples.reset(FLAGS_samples);
1256 for (int s = 0; s < FLAGS_samples; s++) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001257 samples[s] = time(loops, bench.get(), target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001258 }
cdaltone1b89582015-06-25 19:17:08 -07001259 }
mtkleinf3723212014-06-25 14:08:00 -07001260
joshualitte45c81c2015-12-02 09:05:37 -08001261#if SK_SUPPORT_GPU
1262 SkTArray<SkString> keys;
1263 SkTArray<double> values;
1264 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1265 if (gpuStatsDump) {
1266 // TODO cache stats
1267 bench->getGpuStats(canvas, &keys, &values);
1268 }
1269#endif
1270
robertphillips5b693772014-11-21 06:19:36 -08001271 bench->perCanvasPostDraw(canvas);
1272
egdaniel3bf92062015-06-26 08:12:46 -07001273 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001274 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001275 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001276 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001277 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001278 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001279 }
1280
1281 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001282 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001283 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001284 continue;
1285 }
1286
cdaltone1b89582015-06-25 19:17:08 -07001287 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001288 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001289 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001290 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001291 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001292 log->metric("min_ms", stats.min);
George Burgess IV75b57182016-12-06 10:53:52 -08001293 log->metrics("samples", samples);
joshualitte45c81c2015-12-02 09:05:37 -08001294#if SK_SUPPORT_GPU
1295 if (gpuStatsDump) {
1296 // dump to json, only SKPBench currently returns valid keys / values
1297 SkASSERT(keys.count() == values.count());
1298 for (int i = 0; i < keys.count(); i++) {
1299 log->metric(keys[i].c_str(), values[i]);
1300 }
1301 }
1302#endif
1303
mtkleine070c2b2014-10-14 08:40:43 -07001304 if (runs++ % FLAGS_flushEvery == 0) {
1305 log->flush();
1306 }
mtklein60317d0f2014-07-14 11:30:37 -07001307
bsalomon6eb03cc2014-08-07 14:28:50 -07001308 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001309 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001310 config = ""; // Only print the config if we run the same bench on more than one.
1311 }
mtkleind75c4662015-04-30 07:11:22 -07001312 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1313 , sk_tools::getCurrResidentSetSizeMB()
1314 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001315 , bench->getUniqueName()
1316 , config);
mtkleinf3723212014-06-25 14:08:00 -07001317 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001318 const char* mark = " ";
1319 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1320 if (stddev_percent > 5) mark = "?";
1321 if (stddev_percent > 10) mark = "!";
1322
1323 SkDebugf("%10.2f %s\t%s\t%s\n",
1324 stats.median*1e3, mark, bench->getUniqueName(), config);
Mike Reedc665e5b2017-07-04 22:56:06 -04001325 } else if (FLAGS_csv) {
mtkleinf3723212014-06-25 14:08:00 -07001326 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
Mike Reedc665e5b2017-07-04 22:56:06 -04001327 SkDebugf("%g,%g,%g,%g,%g,%s,%s\n"
1328 , stats.min
1329 , stats.median
1330 , stats.mean
1331 , stats.max
1332 , stddev_percent
1333 , config
1334 , bench->getUniqueName()
1335 );
1336 } else {
1337 const char* format = "%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n";
1338 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1339 SkDebugf(format
mtkleind75c4662015-04-30 07:11:22 -07001340 , sk_tools::getCurrResidentSetSizeMB()
1341 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001342 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001343 , HUMANIZE(stats.min)
1344 , HUMANIZE(stats.median)
1345 , HUMANIZE(stats.mean)
1346 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001347 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001348 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001349 , config
mtklein96289052014-09-10 12:05:59 -07001350 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001351 );
1352 }
joshualitte45c81c2015-12-02 09:05:37 -08001353
bsalomonb12ea412015-02-02 21:19:50 -08001354#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001355 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001356 GrContext* context = gGrFactory->get(configs[i].ctxType,
csmartdaltone812d492017-02-21 12:36:05 -07001357 configs[i].ctxOverrides);
kkinnunen5219fd92015-12-10 06:28:13 -08001358 context->printCacheStats();
1359 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001360 }
1361#endif
joshualitte45c81c2015-12-02 09:05:37 -08001362
cdalton2c56ba52015-06-26 13:32:53 -07001363 if (FLAGS_verbose) {
1364 SkDebugf("Samples: ");
1365 for (int i = 0; i < samples.count(); i++) {
1366 SkDebugf("%s ", HUMANIZE(samples[i]));
1367 }
1368 SkDebugf("%s\n", bench->getUniqueName());
1369 }
egdaniel3bf92062015-06-26 08:12:46 -07001370 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001371 }
mtkleinf3723212014-06-25 14:08:00 -07001372 }
1373
Herb Derby5a523fe2017-01-26 16:48:28 -05001374 SkGraphics::PurgeAllCaches();
1375
mtkleine1091452014-12-04 10:47:02 -08001376 log->bench("memory_usage", 0,0);
1377 log->config("meta");
1378 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1379
joshualitte0b19d42015-03-26 10:41:02 -07001380#if SK_SUPPORT_GPU
1381 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1382 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001383 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001384#endif
1385
mtkleinf3723212014-06-25 14:08:00 -07001386 return 0;
1387}