blob: cfac6be7ba4f77d7739446a11e185b9267e8f683 [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
mtkleinf3723212014-06-25 14:08:00 -070012#include "Benchmark.h"
msarett7f691442015-09-22 11:56:16 -070013#include "BitmapRegionDecoderBench.h"
scroggo60869a42015-04-01 12:09:17 -070014#include "CodecBench.h"
msarett7f691442015-09-22 11:56:16 -070015#include "CodecBenchPriv.h"
mtkleinf3723212014-06-25 14:08:00 -070016#include "CrashHandler.h"
msarett95f192d2015-02-13 09:05:41 -080017#include "DecodingBench.h"
mtkleine714e752014-07-31 12:13:48 -070018#include "GMBench.h"
mtkleinafb43792014-08-19 15:55:55 -070019#include "ProcStats.h"
mtklein60317d0f2014-07-14 11:30:37 -070020#include "ResultsWriter.h"
mtkleinfd731ce2014-09-10 12:19:30 -070021#include "RecordingBench.h"
joshualitt261c3ad2015-04-27 09:16:57 -070022#include "SKPAnimationBench.h"
mtklein92007582014-08-01 07:46:52 -070023#include "SKPBench.h"
mtkleinf3723212014-06-25 14:08:00 -070024#include "Stats.h"
mtkleinf3723212014-06-25 14:08:00 -070025
msarett5cb48852015-11-06 08:56:32 -080026#include "SkBitmapRegionDecoder.h"
mtklein6838d852014-10-29 14:15:10 -070027#include "SkBBoxHierarchy.h"
mtkleinf3723212014-06-25 14:08:00 -070028#include "SkCanvas.h"
scroggo60869a42015-04-01 12:09:17 -070029#include "SkCodec.h"
caryclark17f0b6d2014-07-22 10:15:34 -070030#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080031#include "SkCommonFlagsConfig.h"
msarett95f192d2015-02-13 09:05:41 -080032#include "SkData.h"
mtkleinf3723212014-06-25 14:08:00 -070033#include "SkForceLinking.h"
34#include "SkGraphics.h"
mtklein20840502014-08-21 15:51:22 -070035#include "SkOSFile.h"
36#include "SkPictureRecorder.h"
mtklein051e56d2014-12-04 08:46:51 -080037#include "SkPictureUtils.h"
mtkleinf3723212014-06-25 14:08:00 -070038#include "SkString.h"
39#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080040#include "SkTaskGroup.h"
msarettc149f0e2016-01-04 11:35:43 -080041#include "SkThreadUtils.h"
mtkleinf3723212014-06-25 14:08:00 -070042
bungeman60e0fee2015-08-26 05:15:46 -070043#include <stdlib.h>
44
scroggo38ce0a72016-01-07 07:28:47 -080045#ifndef SK_BUILD_FOR_WIN32
46 #include <unistd.h>
47#endif
48
tomhudsond968a6f2015-03-26 11:28:06 -070049#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
50 #include "nanobenchAndroid.h"
51#endif
52
mtkleinbb6a0282014-07-01 08:43:42 -070053#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070054 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070055 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070056 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070057 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070058#endif
59
bsalomon682c2692015-05-22 14:01:46 -070060 struct GrContextOptions;
61
mtkleinf3723212014-06-25 14:08:00 -070062__SK_FORCE_IMAGE_DECODER_LINKING;
63
reed53249782014-10-10 09:09:52 -070064static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070065
mtkleinb5110422014-08-07 15:20:02 -070066static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070067#ifdef SK_DEBUG
68 1;
mtkleina189ccd2014-07-14 12:28:47 -070069#else
bsalomon6eb03cc2014-08-07 14:28:50 -070070 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070071#endif
72
bsalomon6eb03cc2014-08-07 14:28:50 -070073static SkString loops_help_txt() {
74 SkString help;
75 help.printf("Number of times to run each bench. Set this to %d to auto-"
76 "tune for each bench. Timings are only reported when auto-tuning.",
77 kAutoTuneLoops);
78 return help;
79}
80
cdaltone1b89582015-06-25 19:17:08 -070081static SkString to_string(int n) {
82 SkString str;
83 str.appendS32(n);
84 return str;
85}
86
bsalomon6eb03cc2014-08-07 14:28:50 -070087DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
88
mtkleinf3723212014-06-25 14:08:00 -070089DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -070090DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -070091DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
92DEFINE_double(overheadGoal, 0.0001,
93 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070094DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -070095DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070096DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
97 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070098
mtklein60317d0f2014-07-14 11:30:37 -070099DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -0700100DEFINE_int32(maxCalibrationAttempts, 3,
101 "Try up to this many times to guess loops for a bench, or skip the bench.");
102DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700103DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
104DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700105DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
106 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700107DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
robertphillips5b693772014-11-21 06:19:36 -0800108DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700109DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700110DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800111DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800112DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800113DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
msarettc149f0e2016-01-04 11:35:43 -0800114DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
mtklein92007582014-08-01 07:46:52 -0700115
mtkleinbbba1682015-10-28 11:36:30 -0700116static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
117
mtkleinf3723212014-06-25 14:08:00 -0700118static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700119 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800120 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700121}
mtklein55b0ffc2014-07-17 08:38:23 -0700122#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700123
tomhudsond968a6f2015-03-26 11:28:06 -0700124bool Target::init(SkImageInfo info, Benchmark* bench) {
125 if (Benchmark::kRaster_Backend == config.backend) {
126 this->surface.reset(SkSurface::NewRaster(info));
127 if (!this->surface.get()) {
128 return false;
129 }
130 }
131 return true;
132}
133bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700134 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700135 if (!canvas) {
136 return false;
137 }
138 bmp->setInfo(canvas->imageInfo());
139 if (!canvas->readPixels(bmp, 0, 0)) {
140 SkDebugf("Can't read canvas pixels.\n");
141 return false;
142 }
143 return true;
144}
145
146#if SK_SUPPORT_GPU
147struct GPUTarget : public Target {
halcanary96fcdcc2015-08-27 07:41:13 -0700148 explicit GPUTarget(const Config& c) : Target(c), gl(nullptr) { }
tomhudsond968a6f2015-03-26 11:28:06 -0700149 SkGLContext* gl;
150
151 void setup() override {
152 this->gl->makeCurrent();
153 // Make sure we're done with whatever came before.
154 SK_GL(*this->gl, Finish());
155 }
156 void endTiming() override {
157 if (this->gl) {
158 SK_GL(*this->gl, Flush());
159 this->gl->swapBuffers();
160 }
161 }
162 void fence() override {
163 SK_GL(*this->gl, Finish());
164 }
mtkleind75c4662015-04-30 07:11:22 -0700165
cdaltond416a5b2015-06-23 13:23:44 -0700166 bool needsFrameTiming(int* maxFrameLag) const override {
167 if (!this->gl->getMaxGpuFrameLag(maxFrameLag)) {
168 // Frame lag is unknown.
169 *maxFrameLag = FLAGS_gpuFrameLag;
170 }
171 return true;
172 }
tomhudsond968a6f2015-03-26 11:28:06 -0700173 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700174 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
175 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700176 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
kkinnunen5219fd92015-12-10 06:28:13 -0800177 this->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(this->config.ctxType,
kkinnunen5219fd92015-12-10 06:28:13 -0800178 this->config.ctxOptions),
tomhudsond968a6f2015-03-26 11:28:06 -0700179 SkSurface::kNo_Budgeted, info,
180 this->config.samples, &props));
kkinnunen3e980c32015-12-23 01:33:00 -0800181 this->gl = gGrFactory->getContextInfo(this->config.ctxType,
kkinnunen34058002016-01-06 23:49:30 -0800182 this->config.ctxOptions).fGLContext;
tomhudsond968a6f2015-03-26 11:28:06 -0700183 if (!this->surface.get()) {
184 return false;
185 }
cdaltond416a5b2015-06-23 13:23:44 -0700186 if (!this->gl->fenceSyncSupport()) {
187 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
188 "Timings might not be accurate.\n", this->config.name);
189 }
tomhudsond968a6f2015-03-26 11:28:06 -0700190 return true;
191 }
192 void fillOptions(ResultsWriter* log) override {
193 const GrGLubyte* version;
194 SK_GL_RET(*this->gl, version, GetString(GR_GL_VERSION));
195 log->configOption("GL_VERSION", (const char*)(version));
196
197 SK_GL_RET(*this->gl, version, GetString(GR_GL_RENDERER));
198 log->configOption("GL_RENDERER", (const char*) version);
199
200 SK_GL_RET(*this->gl, version, GetString(GR_GL_VENDOR));
201 log->configOption("GL_VENDOR", (const char*) version);
202
203 SK_GL_RET(*this->gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
204 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
205 }
206};
mtkleind75c4662015-04-30 07:11:22 -0700207
tomhudsond968a6f2015-03-26 11:28:06 -0700208#endif
209
tomhudson75a0ebb2015-03-27 12:11:44 -0700210static double time(int loops, Benchmark* bench, Target* target) {
211 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700212 if (canvas) {
213 canvas->clear(SK_ColorWHITE);
214 }
joshualitt8a6697a2015-09-30 12:11:07 -0700215 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700216 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700217 canvas = target->beginTiming(canvas);
218 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700219 if (canvas) {
220 canvas->flush();
221 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700222 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700223 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700224 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700225 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700226}
227
mtkleinf3723212014-06-25 14:08:00 -0700228static double estimate_timer_overhead() {
229 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700230 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700231 double start = now_ms();
232 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700233 }
234 return overhead / FLAGS_overheadLoops;
235}
236
reed53249782014-10-10 09:09:52 -0700237static int detect_forever_loops(int loops) {
238 // look for a magic run-forever value
239 if (loops < 0) {
240 loops = SK_MaxS32;
241 }
242 return loops;
243}
244
mtklein55b0ffc2014-07-17 08:38:23 -0700245static int clamp_loops(int loops) {
246 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800247 SkDebugf("ERROR: clamping loops from %d to 1. "
248 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700249 return 1;
250 }
251 if (loops > FLAGS_maxLoops) {
252 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
253 return FLAGS_maxLoops;
254 }
255 return loops;
256}
257
tomhudsond968a6f2015-03-26 11:28:06 -0700258static bool write_canvas_png(Target* target, const SkString& filename) {
259
bsalomon6eb03cc2014-08-07 14:28:50 -0700260 if (filename.isEmpty()) {
261 return false;
262 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700263 if (target->getCanvas() &&
264 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700265 return false;
266 }
tomhudsond968a6f2015-03-26 11:28:06 -0700267
bsalomon6eb03cc2014-08-07 14:28:50 -0700268 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700269
270 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700271 return false;
272 }
tomhudsond968a6f2015-03-26 11:28:06 -0700273
bsalomon6eb03cc2014-08-07 14:28:50 -0700274 SkString dir = SkOSPath::Dirname(filename.c_str());
275 if (!sk_mkdir(dir.c_str())) {
276 SkDebugf("Can't make dir %s.\n", dir.c_str());
277 return false;
278 }
279 SkFILEWStream stream(filename.c_str());
280 if (!stream.isValid()) {
281 SkDebugf("Can't write %s.\n", filename.c_str());
282 return false;
283 }
284 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
285 SkDebugf("Can't encode a PNG.\n");
286 return false;
287 }
288 return true;
289}
290
291static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700292static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700293 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700294 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700295 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700296 int loops = bench->calculateLoops(FLAGS_loops);
297 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700298 while (bench_plus_overhead < overhead) {
299 if (round++ == FLAGS_maxCalibrationAttempts) {
300 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700301 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700302 return kFailedLoops;
303 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700304 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700305 }
mtklein2069e222014-08-04 13:57:39 -0700306 }
mtkleinf3723212014-06-25 14:08:00 -0700307
mtkleinbb6a0282014-07-01 08:43:42 -0700308 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700309 // We'll pick N to make timer overhead negligible:
310 //
mtkleinbb6a0282014-07-01 08:43:42 -0700311 // overhead
312 // ------------------------- < FLAGS_overheadGoal
313 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700314 //
mtkleinbb6a0282014-07-01 08:43:42 -0700315 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700316 //
317 // Doing some math, we get:
318 //
mtkleinbb6a0282014-07-01 08:43:42 -0700319 // (overhead / FLAGS_overheadGoal) - overhead
320 // ------------------------------------------ < N
321 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700322 //
323 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700324 if (kAutoTuneLoops == loops) {
325 const double numer = overhead / FLAGS_overheadGoal - overhead;
326 const double denom = bench_plus_overhead - overhead;
327 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700328 loops = clamp_loops(loops);
329 } else {
330 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700331 }
mtkleinbb6a0282014-07-01 08:43:42 -0700332
mtkleinbb6a0282014-07-01 08:43:42 -0700333 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700334}
335
cdaltone1b89582015-06-25 19:17:08 -0700336static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700337 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700338 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700339 if (kAutoTuneLoops == loops) {
340 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700341 double elapsed = 0;
342 do {
mtklein527930f2014-11-06 08:04:34 -0800343 if (1<<30 == loops) {
344 // We're about to wrap. Something's wrong with the bench.
345 loops = 0;
346 break;
347 }
mtkleina189ccd2014-07-14 12:28:47 -0700348 loops *= 2;
349 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700350 // _this_ round, not still timing last round.
351 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700352 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700353 }
354 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700355
mtkleina189ccd2014-07-14 12:28:47 -0700356 // We've overshot at least a little. Scale back linearly.
357 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700358 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700359
tomhudsond968a6f2015-03-26 11:28:06 -0700360 // Make sure we're not still timing our calibration.
361 target->fence();
reed53249782014-10-10 09:09:52 -0700362 } else {
363 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700364 }
mtkleinbb6a0282014-07-01 08:43:42 -0700365
366 // Pretty much the same deal as the calibration: do some warmup to make
367 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700368 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700369 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700370 }
mtkleinbb6a0282014-07-01 08:43:42 -0700371
mtkleinbb6a0282014-07-01 08:43:42 -0700372 return loops;
373}
mtkleinbb6a0282014-07-01 08:43:42 -0700374
375static SkString to_lower(const char* str) {
376 SkString lower(str);
377 for (size_t i = 0; i < lower.size(); i++) {
378 lower[i] = tolower(lower[i]);
379 }
380 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700381}
382
bsalomonc2553372014-07-22 13:09:05 -0700383static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700384 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700385 if (to_lower(FLAGS_config[i]).equals(name)) {
386 return true;
mtkleinf3723212014-06-25 14:08:00 -0700387 }
388 }
bsalomonc2553372014-07-22 13:09:05 -0700389 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700390}
391
bsalomonc2553372014-07-22 13:09:05 -0700392#if SK_SUPPORT_GPU
393static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
kkinnunen5219fd92015-12-10 06:28:13 -0800394 GrContextFactory::GLContextOptions ctxOptions,
bsalomonc2553372014-07-22 13:09:05 -0700395 int sampleCnt) {
396 if (!is_cpu_config_allowed(name)) {
397 return false;
398 }
kkinnunen3e980c32015-12-23 01:33:00 -0800399 if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOptions)) {
bsalomon76228632015-05-29 08:02:10 -0700400 return sampleCnt <= ctx->caps()->maxSampleCount();
bsalomonc2553372014-07-22 13:09:05 -0700401 }
402 return false;
403}
404#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700405
bsalomonc2553372014-07-22 13:09:05 -0700406#if SK_SUPPORT_GPU
407#define kBogusGLContextType GrContextFactory::kNative_GLContextType
kkinnunen5219fd92015-12-10 06:28:13 -0800408#define kBogusGLContextOptions GrContextFactory::kNone_GLContextOptions
bsalomonc2553372014-07-22 13:09:05 -0700409#else
410#define kBogusGLContextType 0
kkinnunen5219fd92015-12-10 06:28:13 -0800411#define kBogusGLContextOptions 0
mtkleine714e752014-07-31 12:13:48 -0700412#endif
bsalomonc2553372014-07-22 13:09:05 -0700413
414// Append all configs that are enabled and supported.
415static void create_configs(SkTDArray<Config>* configs) {
kkinnunen5219fd92015-12-10 06:28:13 -0800416 #define CPU_CONFIG(name, backend, color, alpha) \
417 if (is_cpu_config_allowed(#name)) { \
418 Config config = { #name, Benchmark::backend, color, alpha, 0, \
419 kBogusGLContextType, kBogusGLContextOptions, \
420 false }; \
421 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700422 }
mtkleine714e752014-07-31 12:13:48 -0700423
mtklein40b32be2014-07-09 08:46:49 -0700424 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700425 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
426 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
427 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700428 }
mtkleinbb6a0282014-07-01 08:43:42 -0700429
430#if SK_SUPPORT_GPU
kkinnunen5219fd92015-12-10 06:28:13 -0800431 #define GPU_CONFIG(name, ctxType, ctxOptions, samples, useDFText) \
432 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, \
433 GrContextFactory::ctxOptions, samples)) { \
bsalomonc2553372014-07-22 13:09:05 -0700434 Config config = { \
435 #name, \
436 Benchmark::kGPU_Backend, \
437 kN32_SkColorType, \
438 kPremul_SkAlphaType, \
439 samples, \
jvanverth4736e142014-11-07 07:12:46 -0800440 GrContextFactory::ctxType, \
kkinnunen5219fd92015-12-10 06:28:13 -0800441 GrContextFactory::ctxOptions, \
jvanverth4736e142014-11-07 07:12:46 -0800442 useDFText }; \
bsalomonc2553372014-07-22 13:09:05 -0700443 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700444 }
mtkleine714e752014-07-31 12:13:48 -0700445
mtklein40b32be2014-07-09 08:46:49 -0700446 if (FLAGS_gpu) {
kkinnunen5219fd92015-12-10 06:28:13 -0800447 GPU_CONFIG(gpu, kNative_GLContextType, kNone_GLContextOptions, 0, false)
448 GPU_CONFIG(msaa4, kNative_GLContextType, kNone_GLContextOptions, 4, false)
449 GPU_CONFIG(msaa16, kNative_GLContextType, kNone_GLContextOptions, 16, false)
450 GPU_CONFIG(nvprmsaa4, kNative_GLContextType, kEnableNVPR_GLContextOptions, 4, false)
451 GPU_CONFIG(nvprmsaa16, kNative_GLContextType, kEnableNVPR_GLContextOptions, 16, false)
452 GPU_CONFIG(gpudft, kNative_GLContextType, kNone_GLContextOptions, 0, true)
453 GPU_CONFIG(debug, kDebug_GLContextType, kNone_GLContextOptions, 0, false)
454 GPU_CONFIG(nullgpu, kNull_GLContextType, kNone_GLContextOptions, 0, false)
kkinnunen3e980c32015-12-23 01:33:00 -0800455#if SK_ANGLE
456#ifdef SK_BUILD_FOR_WIN
kkinnunen5219fd92015-12-10 06:28:13 -0800457 GPU_CONFIG(angle, kANGLE_GLContextType, kNone_GLContextOptions, 0, false)
kkinnunen3e980c32015-12-23 01:33:00 -0800458#endif
kkinnunen5219fd92015-12-10 06:28:13 -0800459 GPU_CONFIG(angle-gl, kANGLE_GL_GLContextType, kNone_GLContextOptions, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700460#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800461#if SK_COMMAND_BUFFER
kkinnunen5219fd92015-12-10 06:28:13 -0800462 GPU_CONFIG(commandbuffer, kCommandBuffer_GLContextType, kNone_GLContextOptions, 0, false)
hendrikw885bf092015-08-27 10:38:39 -0700463#endif
cdaltond416a5b2015-06-23 13:23:44 -0700464#if SK_MESA
kkinnunen5219fd92015-12-10 06:28:13 -0800465 GPU_CONFIG(mesa, kMESA_GLContextType, kNone_GLContextOptions, 0, false)
cdaltond416a5b2015-06-23 13:23:44 -0700466#endif
mtklein40b32be2014-07-09 08:46:49 -0700467 }
mtkleinbb6a0282014-07-01 08:43:42 -0700468#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700469
470#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
471 if (is_cpu_config_allowed("hwui")) {
472 Config config = { "hwui", Benchmark::kHWUI_Backend, kRGBA_8888_SkColorType,
kkinnunen5219fd92015-12-10 06:28:13 -0800473 kPremul_SkAlphaType, 0, kBogusGLContextType, kBogusGLContextOptions,
474 false };
tomhudsond968a6f2015-03-26 11:28:06 -0700475 configs->push(config);
476 }
477#endif
mtkleinf3723212014-06-25 14:08:00 -0700478}
479
halcanary96fcdcc2015-08-27 07:41:13 -0700480// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700481static Target* is_enabled(Benchmark* bench, const Config& config) {
482 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700483 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700484 }
485
reede5ea5002014-09-03 11:54:58 -0700486 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
487 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700488
halcanary96fcdcc2015-08-27 07:41:13 -0700489 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700490
tomhudsond968a6f2015-03-26 11:28:06 -0700491 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700492#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700493 case Benchmark::kGPU_Backend:
494 target = new GPUTarget(config);
495 break;
bsalomonc2553372014-07-22 13:09:05 -0700496#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700497#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
498 case Benchmark::kHWUI_Backend:
499 target = new HWUITarget(config, bench);
500 break;
501#endif
502 default:
503 target = new Target(config);
504 break;
505 }
bsalomonc2553372014-07-22 13:09:05 -0700506
tomhudsond968a6f2015-03-26 11:28:06 -0700507 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700508 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700509 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700510 }
511 return target;
512}
513
msarett5cb48852015-11-06 08:56:32 -0800514static bool valid_brd_bench(SkData* encoded, SkBitmapRegionDecoder::Strategy strategy,
msarett7f691442015-09-22 11:56:16 -0700515 SkColorType colorType, uint32_t sampleSize, uint32_t minOutputSize, int* width,
516 int* height) {
msarett5cb48852015-11-06 08:56:32 -0800517 SkAutoTDelete<SkBitmapRegionDecoder> brd(
518 SkBitmapRegionDecoder::Create(encoded, strategy));
msarett7f691442015-09-22 11:56:16 -0700519 if (nullptr == brd.get()) {
520 // This is indicates that subset decoding is not supported for a particular image format.
521 return false;
522 }
523
msarett35e5d1b2015-10-27 12:50:25 -0700524 SkBitmap bitmap;
525 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, brd->width(), brd->height()),
526 1, colorType, false)) {
527 return false;
528 }
msarett7f691442015-09-22 11:56:16 -0700529
530 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
531 (uint32_t) brd->height()) {
532 // This indicates that the image is not large enough to decode a
533 // minOutputSize x minOutputSize subset at the given sampleSize.
534 return false;
535 }
536
537 // Set the image width and height. The calling code will use this to choose subsets to decode.
538 *width = brd->width();
539 *height = brd->height();
540 return true;
541}
542
egdaniel3bf92062015-06-26 08:12:46 -0700543static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700544 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700545#if SK_SUPPORT_GPU
546 if (FLAGS_abandonGpuContext) {
547 gGrFactory->abandonContexts();
548 }
549 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
550 gGrFactory->destroyContexts();
551 }
552#endif
553}
554
mtkleine714e752014-07-31 12:13:48 -0700555class BenchmarkStream {
556public:
mtklein92007582014-08-01 07:46:52 -0700557 BenchmarkStream() : fBenches(BenchRegistry::Head())
558 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700559 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700560 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800561 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800562 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700563 , fCurrentCodec(0)
msarett95f192d2015-02-13 09:05:41 -0800564 , fCurrentImage(0)
msarett7f691442015-09-22 11:56:16 -0700565 , fCurrentBRDImage(0)
msarett95f192d2015-02-13 09:05:41 -0800566 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800567 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700568 , fCurrentSubsetType(0)
msarett7f691442015-09-22 11:56:16 -0700569 , fCurrentBRDStrategy(0)
570 , fCurrentBRDSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700571 , fCurrentAnimSKP(0) {
mtklein92007582014-08-01 07:46:52 -0700572 for (int i = 0; i < FLAGS_skps.count(); i++) {
573 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
574 fSKPs.push_back() = FLAGS_skps[i];
575 } else {
576 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
577 SkString path;
578 while (it.next(&path)) {
579 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
580 }
581 }
582 }
mtkleine714e752014-07-31 12:13:48 -0700583
mtklein92007582014-08-01 07:46:52 -0700584 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
585 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
586 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
587 exit(1);
588 }
589
590 for (int i = 0; i < FLAGS_scales.count(); i++) {
591 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
592 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
593 exit(1);
594 }
595 }
robertphillips5b693772014-11-21 06:19:36 -0800596
cdalton63a82852015-06-29 14:06:10 -0700597 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
598 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700599 exit(1);
600 }
601
robertphillips5b693772014-11-21 06:19:36 -0800602 if (FLAGS_mpd) {
603 fUseMPDs.push_back() = true;
604 }
mtkleinc751ecb2015-06-15 08:56:38 -0700605 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700606
msarett95f192d2015-02-13 09:05:41 -0800607 // Prepare the images for decoding
608 for (int i = 0; i < FLAGS_images.count(); i++) {
609 const char* flag = FLAGS_images[i];
610 if (sk_isdir(flag)) {
611 // If the value passed in is a directory, add all the images
612 SkOSFile::Iter it(flag);
613 SkString file;
614 while (it.next(&file)) {
615 fImages.push_back() = SkOSPath::Join(flag, file.c_str());
616 }
617 } else if (sk_exists(flag)) {
618 // Also add the value if it is a single image
619 fImages.push_back() = flag;
620 }
621 }
mtklein95553d92015-03-12 08:24:21 -0700622
msarett95f192d2015-02-13 09:05:41 -0800623 // Choose the candidate color types for image decoding
624 const SkColorType colorTypes[] =
msarettb23e6aa2015-06-09 13:56:10 -0700625 { kN32_SkColorType,
626 kRGB_565_SkColorType,
627 kAlpha_8_SkColorType,
628 kIndex_8_SkColorType,
629 kGray_8_SkColorType };
msarett74deb982015-10-20 16:45:56 -0700630 fColorTypes.reset(colorTypes, SK_ARRAY_COUNT(colorTypes));
mtklein92007582014-08-01 07:46:52 -0700631 }
632
mtkleinfd731ce2014-09-10 12:19:30 -0700633 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
634 // Not strictly necessary, as it will be checked again later,
635 // but helps to avoid a lot of pointless work if we're going to skip it.
636 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
637 return false;
638 }
639
scroggoa1193e42015-01-21 12:09:53 -0800640 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -0700641 if (stream.get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700642 SkDebugf("Could not read %s.\n", path);
643 return false;
644 }
645
mtklein57f27bd2015-02-09 11:58:41 -0800646 pic->reset(SkPicture::CreateFromStream(stream.get()));
halcanary96fcdcc2015-08-27 07:41:13 -0700647 if (pic->get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700648 SkDebugf("Could not read %s as an SkPicture.\n", path);
649 return false;
650 }
651 return true;
652 }
653
mtklein92007582014-08-01 07:46:52 -0700654 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700655 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700656 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700657 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700658 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700659 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700660 return bench;
661 }
mtklein92007582014-08-01 07:46:52 -0700662
mtkleine714e752014-07-31 12:13:48 -0700663 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -0700664 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700665 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800666 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700667 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700668 fBenchType = "micro";
halcanary385fe4d2015-08-26 13:07:48 -0700669 return new GMBench(gm.detach());
mtkleine714e752014-07-31 12:13:48 -0700670 }
671 }
mtklein92007582014-08-01 07:46:52 -0700672
mtkleinfd731ce2014-09-10 12:19:30 -0700673 // First add all .skps as RecordingBenches.
674 while (fCurrentRecording < fSKPs.count()) {
675 const SkString& path = fSKPs[fCurrentRecording++];
676 SkAutoTUnref<SkPicture> pic;
677 if (!ReadPicture(path.c_str(), &pic)) {
678 continue;
679 }
680 SkString name = SkOSPath::Basename(path.c_str());
681 fSourceType = "skp";
682 fBenchType = "recording";
bsalomon0aa5cea2014-12-15 09:13:35 -0800683 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic));
mtklein051e56d2014-12-04 08:46:51 -0800684 fSKPOps = pic->approximateOpCount();
halcanary385fe4d2015-08-26 13:07:48 -0700685 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700686 }
687
688 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700689 while (fCurrentScale < fScales.count()) {
690 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800691 const SkString& path = fSKPs[fCurrentSKP];
mtkleinfd731ce2014-09-10 12:19:30 -0700692 SkAutoTUnref<SkPicture> pic;
693 if (!ReadPicture(path.c_str(), &pic)) {
robertphillips5b693772014-11-21 06:19:36 -0800694 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700695 continue;
696 }
robertphillips5b693772014-11-21 06:19:36 -0800697
698 while (fCurrentUseMPD < fUseMPDs.count()) {
699 if (FLAGS_bbh) {
700 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
701 SkRTreeFactory factory;
702 SkPictureRecorder recorder;
703 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
704 pic->playback(recorder.beginRecording(pic->cullRect().width(),
705 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800706 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800707 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
robertphillips5b693772014-11-21 06:19:36 -0800708 pic.reset(recorder.endRecording());
709 }
710 SkString name = SkOSPath::Basename(path.c_str());
711 fSourceType = "skp";
712 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700713 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
714 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700715 }
robertphillips5b693772014-11-21 06:19:36 -0800716 fCurrentUseMPD = 0;
717 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700718 }
719 fCurrentSKP = 0;
720 fCurrentScale++;
721 }
722
joshualitt261c3ad2015-04-27 09:16:57 -0700723 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700724 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700725 while (fCurrentAnimSKP < fSKPs.count()) {
726 const SkString& path = fSKPs[fCurrentAnimSKP];
727 SkAutoTUnref<SkPicture> pic;
728 if (!ReadPicture(path.c_str(), &pic)) {
729 fCurrentAnimSKP++;
730 continue;
731 }
732
733 fCurrentAnimSKP++;
734 SkString name = SkOSPath::Basename(path.c_str());
cdalton63a82852015-06-29 14:06:10 -0700735 SkAutoTUnref<SKPAnimationBench::Animation> animation(
736 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
halcanary385fe4d2015-08-26 13:07:48 -0700737 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation,
738 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700739 }
740 }
741
scroggo60869a42015-04-01 12:09:17 -0700742 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700743 fSourceType = "image";
744 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700745 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800746 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
747 continue;
748 }
scroggo60869a42015-04-01 12:09:17 -0700749 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
750 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700751 if (!codec) {
752 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700753 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700754 continue;
755 }
scroggo21027992015-04-02 13:22:38 -0700756
scroggo60869a42015-04-01 12:09:17 -0700757 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700758 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700759
msarettc7796b92016-01-07 14:20:20 -0800760 SkAlphaType alphaType = codec->getInfo().alphaType();
761 switch (alphaType) {
762 case kOpaque_SkAlphaType:
763 // We only need to test one alpha type (opaque).
764 fCurrentColorType++;
765 break;
766 case kUnpremul_SkAlphaType:
767 case kPremul_SkAlphaType:
768 if (0 == fCurrentAlphaType) {
769 // Test unpremul first.
770 alphaType = kUnpremul_SkAlphaType;
771 fCurrentAlphaType++;
772 } else {
773 // Test premul.
774 alphaType = kPremul_SkAlphaType;
775 fCurrentAlphaType = 0;
776 fCurrentColorType++;
777 }
778 break;
779 default:
780 SkASSERT(false);
781 fCurrentColorType++;
782 break;
scroggo21027992015-04-02 13:22:38 -0700783 }
784
msarettc7796b92016-01-07 14:20:20 -0800785 // Make sure we can decode to this color type and alpha type.
786 SkImageInfo info =
787 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700788 const size_t rowBytes = info.minRowBytes();
789 SkAutoMalloc storage(info.getSafeSize(rowBytes));
790
791 // Used if fCurrentColorType is kIndex_8_SkColorType
792 int colorCount = 256;
793 SkPMColor colors[256];
794
scroggoeb602a52015-07-09 08:16:03 -0700795 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700796 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700797 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700798 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700799 case SkCodec::kSuccess:
800 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700801 return new CodecBench(SkOSPath::Basename(path.c_str()),
msarettc7796b92016-01-07 14:20:20 -0800802 encoded, colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700803 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700804 // This is okay. Not all conversions are valid.
805 break;
scroggo60869a42015-04-01 12:09:17 -0700806 default:
807 // This represents some sort of failure.
808 SkASSERT(false);
809 break;
810 }
811 }
812 fCurrentColorType = 0;
813 }
814
msarett95f192d2015-02-13 09:05:41 -0800815 // Run the DecodingBenches
mtklein6f0ff912016-01-11 09:04:21 -0800816 for (; fCurrentImage < fImages.count(); fCurrentImage++) {
scroggo303fa352015-10-05 11:03:34 -0700817 fSourceType = "image";
818 fBenchType = "skimagedecoder";
mtklein6f0ff912016-01-11 09:04:21 -0800819 const SkString& path = fImages[fCurrentImage];
820 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
821 continue;
822 }
msarett95f192d2015-02-13 09:05:41 -0800823 while (fCurrentColorType < fColorTypes.count()) {
msarett95f192d2015-02-13 09:05:41 -0800824 SkColorType colorType = fColorTypes[fCurrentColorType];
825 fCurrentColorType++;
scroggo60869a42015-04-01 12:09:17 -0700826 // Check if the image decodes to the right color type
827 // before creating the benchmark
msarett95f192d2015-02-13 09:05:41 -0800828 SkBitmap bitmap;
829 if (SkImageDecoder::DecodeFile(path.c_str(), &bitmap,
scroggo60869a42015-04-01 12:09:17 -0700830 colorType, SkImageDecoder::kDecodePixels_Mode)
831 && bitmap.colorType() == colorType) {
msarett95f192d2015-02-13 09:05:41 -0800832 return new DecodingBench(path, colorType);
833 }
834 }
835 fCurrentColorType = 0;
msarett95f192d2015-02-13 09:05:41 -0800836 }
837
msarett7f691442015-09-22 11:56:16 -0700838 // Run the BRDBenches
839 // We will benchmark multiple BRD strategies.
scroggo303fa352015-10-05 11:03:34 -0700840 static const struct {
msarett5cb48852015-11-06 08:56:32 -0800841 SkBitmapRegionDecoder::Strategy fStrategy;
msarett0459c942015-11-10 14:52:13 -0800842 const char* fName;
scroggo303fa352015-10-05 11:03:34 -0700843 } strategies[] = {
msarett0459c942015-11-10 14:52:13 -0800844 { SkBitmapRegionDecoder::kCanvas_Strategy, "BRD_canvas" },
msarett5cb48852015-11-06 08:56:32 -0800845 { SkBitmapRegionDecoder::kAndroidCodec_Strategy, "BRD_android_codec" },
msarett7f691442015-09-22 11:56:16 -0700846 };
847
848 // We intend to create benchmarks that model the use cases in
849 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
850 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
851 // the image. For that reason, we will benchmark decodes in five representative locations
852 // in the image. Additionally, this use case utilizes power of two scaling, so we will
853 // test on power of two sample sizes. The output tile is always 512x512, so, when a
854 // sampleSize is used, the size of the subset that is decoded is always
855 // (sampleSize*512)x(sampleSize*512).
856 // There are a few good reasons to only test on power of two sample sizes at this time:
857 // JPEG decodes using kOriginal_Strategy are broken for non-powers of two.
halcanary6950de62015-11-07 05:29:00 -0800858 // https://bug.skia.org/4319
msarett7f691442015-09-22 11:56:16 -0700859 // All use cases we are aware of only scale by powers of two.
860 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
861 // these tests are sufficient to provide good coverage of our scaling options.
scroggo501b7342015-11-03 07:55:11 -0800862 const uint32_t sampleSizes[] = { 1, 2, 4, 8, 16, 32, 64 };
msarett7f691442015-09-22 11:56:16 -0700863 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800864 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
865 const SkString& path = fImages[fCurrentBRDImage];
866 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
867 continue;
868 }
msarett7f691442015-09-22 11:56:16 -0700869 while (fCurrentBRDStrategy < (int) SK_ARRAY_COUNT(strategies)) {
scroggo303fa352015-10-05 11:03:34 -0700870 fSourceType = "image";
871 fBenchType = strategies[fCurrentBRDStrategy].fName;
scroggo860e8a62015-10-15 07:51:28 -0700872
msarett5cb48852015-11-06 08:56:32 -0800873 const SkBitmapRegionDecoder::Strategy strategy =
scroggo860e8a62015-10-15 07:51:28 -0700874 strategies[fCurrentBRDStrategy].fStrategy;
875
msarett7f691442015-09-22 11:56:16 -0700876 while (fCurrentColorType < fColorTypes.count()) {
877 while (fCurrentBRDSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
878 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700879
880
msarett7f691442015-09-22 11:56:16 -0700881 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
882 const SkColorType colorType = fColorTypes[fCurrentColorType];
883 uint32_t sampleSize = sampleSizes[fCurrentBRDSampleSize];
884 int currentSubsetType = fCurrentSubsetType++;
885
886 int width = 0;
887 int height = 0;
888 if (!valid_brd_bench(encoded.get(), strategy, colorType, sampleSize,
889 minOutputSize, &width, &height)) {
890 break;
891 }
892
893 SkString basename = SkOSPath::Basename(path.c_str());
894 SkIRect subset;
895 const uint32_t subsetSize = sampleSize * minOutputSize;
896 switch (currentSubsetType) {
897 case kTopLeft_SubsetType:
898 basename.append("_TopLeft");
899 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
900 break;
901 case kTopRight_SubsetType:
902 basename.append("_TopRight");
903 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
904 subsetSize);
905 break;
906 case kMiddle_SubsetType:
907 basename.append("_Middle");
908 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
909 (height - subsetSize) / 2, subsetSize, subsetSize);
910 break;
911 case kBottomLeft_SubsetType:
912 basename.append("_BottomLeft");
913 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
914 subsetSize);
915 break;
916 case kBottomRight_SubsetType:
917 basename.append("_BottomRight");
918 subset = SkIRect::MakeXYWH(width - subsetSize,
919 height - subsetSize, subsetSize, subsetSize);
920 break;
921 default:
922 SkASSERT(false);
923 }
924
925 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
926 strategy, colorType, sampleSize, subset);
927 }
928 fCurrentSubsetType = 0;
929 fCurrentBRDSampleSize++;
930 }
931 fCurrentBRDSampleSize = 0;
932 fCurrentColorType++;
933 }
934 fCurrentColorType = 0;
935 fCurrentBRDStrategy++;
936 }
937 fCurrentBRDStrategy = 0;
msarett7f691442015-09-22 11:56:16 -0700938 }
939
halcanary96fcdcc2015-08-27 07:41:13 -0700940 return nullptr;
mtkleine714e752014-07-31 12:13:48 -0700941 }
mtklein92007582014-08-01 07:46:52 -0700942
943 void fillCurrentOptions(ResultsWriter* log) const {
944 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700945 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700946 if (0 == strcmp(fSourceType, "skp")) {
947 log->configOption("clip",
948 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
949 fClip.fRight, fClip.fBottom).c_str());
mtklein4dfdbb12015-10-20 07:45:29 -0700950 SK_ALWAYSBREAK(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -0700951 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -0800952 if (fCurrentUseMPD > 0) {
953 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
954 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
955 }
mtklein92007582014-08-01 07:46:52 -0700956 }
mtklein051e56d2014-12-04 08:46:51 -0800957 if (0 == strcmp(fBenchType, "recording")) {
958 log->metric("bytes", fSKPBytes);
959 log->metric("ops", fSKPOps);
960 }
mtklein92007582014-08-01 07:46:52 -0700961 }
962
mtkleine714e752014-07-31 12:13:48 -0700963private:
msarettb23e6aa2015-06-09 13:56:10 -0700964 enum SubsetType {
965 kTopLeft_SubsetType = 0,
966 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -0700967 kMiddle_SubsetType = 2,
968 kBottomLeft_SubsetType = 3,
969 kBottomRight_SubsetType = 4,
970 kTranslate_SubsetType = 5,
971 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -0700972 kLast_SubsetType = kZoom_SubsetType,
973 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -0700974 };
975
mtkleine714e752014-07-31 12:13:48 -0700976 const BenchRegistry* fBenches;
977 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700978 SkIRect fClip;
979 SkTArray<SkScalar> fScales;
980 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -0800981 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -0800982 SkTArray<SkString> fImages;
msarett74deb982015-10-20 16:45:56 -0700983 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -0700984 SkScalar fZoomMax;
985 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -0700986
mtklein051e56d2014-12-04 08:46:51 -0800987 double fSKPBytes, fSKPOps;
988
mtkleinfd731ce2014-09-10 12:19:30 -0700989 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
990 const char* fBenchType; // How we bench it: micro, recording, playback, ...
991 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700992 int fCurrentScale;
993 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -0800994 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -0700995 int fCurrentCodec;
msarett95f192d2015-02-13 09:05:41 -0800996 int fCurrentImage;
msarett7f691442015-09-22 11:56:16 -0700997 int fCurrentBRDImage;
msarett95f192d2015-02-13 09:05:41 -0800998 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -0800999 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -07001000 int fCurrentSubsetType;
msarett7f691442015-09-22 11:56:16 -07001001 int fCurrentBRDStrategy;
1002 int fCurrentBRDSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001003 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001004};
1005
msarettc149f0e2016-01-04 11:35:43 -08001006// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1007// This prints something every once in a while so that it knows we're still working.
1008static void start_keepalive() {
1009 struct Loop {
1010 static void forever(void*) {
1011 for (;;) {
1012 static const int kSec = 1200;
1013 #if defined(SK_BUILD_FOR_WIN)
1014 Sleep(kSec * 1000);
1015 #else
1016 sleep(kSec);
1017 #endif
1018 SkDebugf("\nBenchmarks still running...\n");
1019 }
1020 }
1021 };
1022 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1023 intentionallyLeaked->start();
1024}
1025
jcgregorio3b27ade2014-11-13 08:06:40 -08001026int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001027int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001028 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001029 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001030 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001031
krajcevski69a55602014-08-13 10:46:31 -07001032#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001033 GrContextOptions grContextOpts;
krajcevski12b35442014-08-13 12:06:26 -07001034 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
halcanary385fe4d2015-08-26 13:07:48 -07001035 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001036#endif
1037
bsalomon06cddec2014-10-24 10:40:50 -07001038 if (FLAGS_veryVerbose) {
1039 FLAGS_verbose = true;
1040 }
1041
bsalomon6eb03cc2014-08-07 14:28:50 -07001042 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001043 FLAGS_samples = 1;
1044 FLAGS_gpuFrameLag = 0;
1045 }
1046
bsalomon6eb03cc2014-08-07 14:28:50 -07001047 if (!FLAGS_writePath.isEmpty()) {
1048 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1049 if (!sk_mkdir(FLAGS_writePath[0])) {
1050 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001051 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001052 }
1053 }
1054
halcanary385fe4d2015-08-26 13:07:48 -07001055 SkAutoTDelete<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001056 if (!FLAGS_outResultsFile.isEmpty()) {
halcanary385fe4d2015-08-26 13:07:48 -07001057 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein60317d0f2014-07-14 11:30:37 -07001058 }
mtklein1915b622014-08-20 11:45:00 -07001059
1060 if (1 == FLAGS_properties.count() % 2) {
1061 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1062 return 1;
1063 }
1064 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1065 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1066 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001067
1068 if (1 == FLAGS_key.count() % 2) {
1069 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1070 return 1;
1071 }
1072 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001073 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001074 }
mtklein60317d0f2014-07-14 11:30:37 -07001075
mtkleinf3723212014-06-25 14:08:00 -07001076 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001077 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001078
cdaltone1b89582015-06-25 19:17:08 -07001079 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001080
bsalomon6eb03cc2014-08-07 14:28:50 -07001081 if (kAutoTuneLoops != FLAGS_loops) {
1082 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001083 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001084 SkDebugf("! -> high variance, ? -> moderate variance\n");
1085 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001086 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001087 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001088 } else {
mtkleind75c4662015-04-30 07:11:22 -07001089 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001090 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001091 }
1092
bsalomonc2553372014-07-22 13:09:05 -07001093 SkTDArray<Config> configs;
1094 create_configs(&configs);
1095
msarettc149f0e2016-01-04 11:35:43 -08001096 if (FLAGS_keepAlive) {
1097 start_keepalive();
1098 }
1099
mtkleine070c2b2014-10-14 08:40:43 -07001100 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001101 BenchmarkStream benchStream;
1102 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001103 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001104 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001105 continue;
1106 }
1107
egdaniel3bf92062015-06-26 08:12:46 -07001108 if (!configs.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -07001109 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001110 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001111 }
egdaniel3bf92062015-06-26 08:12:46 -07001112 for (int i = 0; i < configs.count(); ++i) {
1113 Target* target = is_enabled(b, configs[i]);
1114 if (!target) {
1115 continue;
1116 }
mtkleinf3723212014-06-25 14:08:00 -07001117
halcanary96fcdcc2015-08-27 07:41:13 -07001118 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001119 SkCanvas* canvas = target->getCanvas();
1120 const char* config = target->config.name;
1121
1122 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001123 bench->perCanvasPreDraw(canvas);
1124
cdaltone1b89582015-06-25 19:17:08 -07001125 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001126 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001127 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1128 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001129
mtkleinbbba1682015-10-28 11:36:30 -07001130 if (FLAGS_ms) {
1131 samples.reset();
1132 auto stop = now_ms() + FLAGS_ms;
1133 do {
1134 samples.push_back(time(loops, bench, target) / loops);
1135 } while (now_ms() < stop);
1136 } else {
cdaltone1b89582015-06-25 19:17:08 -07001137 samples.reset(FLAGS_samples);
1138 for (int s = 0; s < FLAGS_samples; s++) {
egdaniel3bf92062015-06-26 08:12:46 -07001139 samples[s] = time(loops, bench, target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001140 }
cdaltone1b89582015-06-25 19:17:08 -07001141 }
mtkleinf3723212014-06-25 14:08:00 -07001142
joshualitte45c81c2015-12-02 09:05:37 -08001143#if SK_SUPPORT_GPU
1144 SkTArray<SkString> keys;
1145 SkTArray<double> values;
1146 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1147 if (gpuStatsDump) {
1148 // TODO cache stats
1149 bench->getGpuStats(canvas, &keys, &values);
1150 }
1151#endif
1152
robertphillips5b693772014-11-21 06:19:36 -08001153 bench->perCanvasPostDraw(canvas);
1154
egdaniel3bf92062015-06-26 08:12:46 -07001155 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001156 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001157 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001158 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001159 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001160 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001161 }
1162
1163 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001164 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001165 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001166 continue;
1167 }
1168
cdaltone1b89582015-06-25 19:17:08 -07001169 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001170 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001171 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001172 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001173 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001174 log->metric("min_ms", stats.min);
joshualitte45c81c2015-12-02 09:05:37 -08001175#if SK_SUPPORT_GPU
1176 if (gpuStatsDump) {
1177 // dump to json, only SKPBench currently returns valid keys / values
1178 SkASSERT(keys.count() == values.count());
1179 for (int i = 0; i < keys.count(); i++) {
1180 log->metric(keys[i].c_str(), values[i]);
1181 }
1182 }
1183#endif
1184
mtkleine070c2b2014-10-14 08:40:43 -07001185 if (runs++ % FLAGS_flushEvery == 0) {
1186 log->flush();
1187 }
mtklein60317d0f2014-07-14 11:30:37 -07001188
bsalomon6eb03cc2014-08-07 14:28:50 -07001189 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001190 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001191 config = ""; // Only print the config if we run the same bench on more than one.
1192 }
mtkleind75c4662015-04-30 07:11:22 -07001193 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1194 , sk_tools::getCurrResidentSetSizeMB()
1195 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001196 , bench->getUniqueName()
1197 , config);
mtkleinf3723212014-06-25 14:08:00 -07001198 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001199 const char* mark = " ";
1200 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1201 if (stddev_percent > 5) mark = "?";
1202 if (stddev_percent > 10) mark = "!";
1203
1204 SkDebugf("%10.2f %s\t%s\t%s\n",
1205 stats.median*1e3, mark, bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001206 } else {
1207 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001208 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1209 , sk_tools::getCurrResidentSetSizeMB()
1210 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001211 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001212 , HUMANIZE(stats.min)
1213 , HUMANIZE(stats.median)
1214 , HUMANIZE(stats.mean)
1215 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001216 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001217 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001218 , config
mtklein96289052014-09-10 12:05:59 -07001219 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001220 );
1221 }
joshualitte45c81c2015-12-02 09:05:37 -08001222
bsalomonb12ea412015-02-02 21:19:50 -08001223#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001224 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001225 GrContext* context = gGrFactory->get(configs[i].ctxType,
kkinnunen3e980c32015-12-23 01:33:00 -08001226 configs[i].ctxOptions);
kkinnunen5219fd92015-12-10 06:28:13 -08001227 context->printCacheStats();
1228 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001229 }
1230#endif
joshualitte45c81c2015-12-02 09:05:37 -08001231
cdalton2c56ba52015-06-26 13:32:53 -07001232 if (FLAGS_verbose) {
1233 SkDebugf("Samples: ");
1234 for (int i = 0; i < samples.count(); i++) {
1235 SkDebugf("%s ", HUMANIZE(samples[i]));
1236 }
1237 SkDebugf("%s\n", bench->getUniqueName());
1238 }
egdaniel3bf92062015-06-26 08:12:46 -07001239 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001240 }
mtkleinf3723212014-06-25 14:08:00 -07001241 }
1242
mtkleine1091452014-12-04 10:47:02 -08001243 log->bench("memory_usage", 0,0);
1244 log->config("meta");
1245 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1246
joshualitte0b19d42015-03-26 10:41:02 -07001247#if SK_SUPPORT_GPU
1248 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1249 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001250 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001251#endif
1252
mtkleinf3723212014-06-25 14:08:00 -07001253 return 0;
1254}
1255
jcgregorio3b27ade2014-11-13 08:06:40 -08001256#if !defined SK_BUILD_FOR_IOS
1257int main(int argc, char** argv) {
1258 SkCommandLineFlags::Parse(argc, argv);
1259 return nanobench_main();
1260}
1261#endif