blob: 638477ecf6fc8720af438cf07dde48b162fcb19f [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"
mtkleinf3723212014-06-25 14:08:00 -070041
bungeman60e0fee2015-08-26 05:15:46 -070042#include <stdlib.h>
43
tomhudsond968a6f2015-03-26 11:28:06 -070044#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
45 #include "nanobenchAndroid.h"
46#endif
47
mtkleinbb6a0282014-07-01 08:43:42 -070048#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070049 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070050 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070051 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070052 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070053#endif
54
bsalomon682c2692015-05-22 14:01:46 -070055 struct GrContextOptions;
56
mtkleinf3723212014-06-25 14:08:00 -070057__SK_FORCE_IMAGE_DECODER_LINKING;
58
reed53249782014-10-10 09:09:52 -070059static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070060
mtkleinb5110422014-08-07 15:20:02 -070061static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070062#ifdef SK_DEBUG
63 1;
mtkleina189ccd2014-07-14 12:28:47 -070064#else
bsalomon6eb03cc2014-08-07 14:28:50 -070065 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070066#endif
67
bsalomon6eb03cc2014-08-07 14:28:50 -070068static SkString loops_help_txt() {
69 SkString help;
70 help.printf("Number of times to run each bench. Set this to %d to auto-"
71 "tune for each bench. Timings are only reported when auto-tuning.",
72 kAutoTuneLoops);
73 return help;
74}
75
cdaltone1b89582015-06-25 19:17:08 -070076static SkString to_string(int n) {
77 SkString str;
78 str.appendS32(n);
79 return str;
80}
81
bsalomon6eb03cc2014-08-07 14:28:50 -070082DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
83
mtkleinf3723212014-06-25 14:08:00 -070084DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -070085DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -070086DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
87DEFINE_double(overheadGoal, 0.0001,
88 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070089DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -070090DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070091DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
92 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070093
mtklein60317d0f2014-07-14 11:30:37 -070094DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -070095DEFINE_int32(maxCalibrationAttempts, 3,
96 "Try up to this many times to guess loops for a bench, or skip the bench.");
97DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -070098DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
99DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700100DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
101 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700102DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
robertphillips5b693772014-11-21 06:19:36 -0800103DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700104DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700105DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800106DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800107DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800108DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
mtklein92007582014-08-01 07:46:52 -0700109
mtkleinbbba1682015-10-28 11:36:30 -0700110static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
111
mtkleinf3723212014-06-25 14:08:00 -0700112static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700113 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800114 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700115}
mtklein55b0ffc2014-07-17 08:38:23 -0700116#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700117
tomhudsond968a6f2015-03-26 11:28:06 -0700118bool Target::init(SkImageInfo info, Benchmark* bench) {
119 if (Benchmark::kRaster_Backend == config.backend) {
120 this->surface.reset(SkSurface::NewRaster(info));
121 if (!this->surface.get()) {
122 return false;
123 }
124 }
125 return true;
126}
127bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700128 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700129 if (!canvas) {
130 return false;
131 }
132 bmp->setInfo(canvas->imageInfo());
133 if (!canvas->readPixels(bmp, 0, 0)) {
134 SkDebugf("Can't read canvas pixels.\n");
135 return false;
136 }
137 return true;
138}
139
140#if SK_SUPPORT_GPU
141struct GPUTarget : public Target {
halcanary96fcdcc2015-08-27 07:41:13 -0700142 explicit GPUTarget(const Config& c) : Target(c), gl(nullptr) { }
tomhudsond968a6f2015-03-26 11:28:06 -0700143 SkGLContext* gl;
144
145 void setup() override {
146 this->gl->makeCurrent();
147 // Make sure we're done with whatever came before.
148 SK_GL(*this->gl, Finish());
149 }
150 void endTiming() override {
151 if (this->gl) {
152 SK_GL(*this->gl, Flush());
153 this->gl->swapBuffers();
154 }
155 }
156 void fence() override {
157 SK_GL(*this->gl, Finish());
158 }
mtkleind75c4662015-04-30 07:11:22 -0700159
cdaltond416a5b2015-06-23 13:23:44 -0700160 bool needsFrameTiming(int* maxFrameLag) const override {
161 if (!this->gl->getMaxGpuFrameLag(maxFrameLag)) {
162 // Frame lag is unknown.
163 *maxFrameLag = FLAGS_gpuFrameLag;
164 }
165 return true;
166 }
tomhudsond968a6f2015-03-26 11:28:06 -0700167 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700168 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
169 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700170 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
kkinnunen5219fd92015-12-10 06:28:13 -0800171 this->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(this->config.ctxType,
kkinnunen5219fd92015-12-10 06:28:13 -0800172 this->config.ctxOptions),
tomhudsond968a6f2015-03-26 11:28:06 -0700173 SkSurface::kNo_Budgeted, info,
174 this->config.samples, &props));
kkinnunen3e980c32015-12-23 01:33:00 -0800175 this->gl = gGrFactory->getContextInfo(this->config.ctxType,
kkinnunen5219fd92015-12-10 06:28:13 -0800176 this->config.ctxOptions)->fGLContext;
tomhudsond968a6f2015-03-26 11:28:06 -0700177 if (!this->surface.get()) {
178 return false;
179 }
cdaltond416a5b2015-06-23 13:23:44 -0700180 if (!this->gl->fenceSyncSupport()) {
181 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
182 "Timings might not be accurate.\n", this->config.name);
183 }
tomhudsond968a6f2015-03-26 11:28:06 -0700184 return true;
185 }
186 void fillOptions(ResultsWriter* log) override {
187 const GrGLubyte* version;
188 SK_GL_RET(*this->gl, version, GetString(GR_GL_VERSION));
189 log->configOption("GL_VERSION", (const char*)(version));
190
191 SK_GL_RET(*this->gl, version, GetString(GR_GL_RENDERER));
192 log->configOption("GL_RENDERER", (const char*) version);
193
194 SK_GL_RET(*this->gl, version, GetString(GR_GL_VENDOR));
195 log->configOption("GL_VENDOR", (const char*) version);
196
197 SK_GL_RET(*this->gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
198 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
199 }
200};
mtkleind75c4662015-04-30 07:11:22 -0700201
tomhudsond968a6f2015-03-26 11:28:06 -0700202#endif
203
tomhudson75a0ebb2015-03-27 12:11:44 -0700204static double time(int loops, Benchmark* bench, Target* target) {
205 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700206 if (canvas) {
207 canvas->clear(SK_ColorWHITE);
208 }
joshualitt8a6697a2015-09-30 12:11:07 -0700209 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700210 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700211 canvas = target->beginTiming(canvas);
212 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700213 if (canvas) {
214 canvas->flush();
215 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700216 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700217 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700218 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700219 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700220}
221
mtkleinf3723212014-06-25 14:08:00 -0700222static double estimate_timer_overhead() {
223 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700224 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700225 double start = now_ms();
226 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700227 }
228 return overhead / FLAGS_overheadLoops;
229}
230
reed53249782014-10-10 09:09:52 -0700231static int detect_forever_loops(int loops) {
232 // look for a magic run-forever value
233 if (loops < 0) {
234 loops = SK_MaxS32;
235 }
236 return loops;
237}
238
mtklein55b0ffc2014-07-17 08:38:23 -0700239static int clamp_loops(int loops) {
240 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800241 SkDebugf("ERROR: clamping loops from %d to 1. "
242 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700243 return 1;
244 }
245 if (loops > FLAGS_maxLoops) {
246 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
247 return FLAGS_maxLoops;
248 }
249 return loops;
250}
251
tomhudsond968a6f2015-03-26 11:28:06 -0700252static bool write_canvas_png(Target* target, const SkString& filename) {
253
bsalomon6eb03cc2014-08-07 14:28:50 -0700254 if (filename.isEmpty()) {
255 return false;
256 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700257 if (target->getCanvas() &&
258 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700259 return false;
260 }
tomhudsond968a6f2015-03-26 11:28:06 -0700261
bsalomon6eb03cc2014-08-07 14:28:50 -0700262 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700263
264 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700265 return false;
266 }
tomhudsond968a6f2015-03-26 11:28:06 -0700267
bsalomon6eb03cc2014-08-07 14:28:50 -0700268 SkString dir = SkOSPath::Dirname(filename.c_str());
269 if (!sk_mkdir(dir.c_str())) {
270 SkDebugf("Can't make dir %s.\n", dir.c_str());
271 return false;
272 }
273 SkFILEWStream stream(filename.c_str());
274 if (!stream.isValid()) {
275 SkDebugf("Can't write %s.\n", filename.c_str());
276 return false;
277 }
278 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
279 SkDebugf("Can't encode a PNG.\n");
280 return false;
281 }
282 return true;
283}
284
285static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700286static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700287 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700288 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700289 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700290 int loops = bench->calculateLoops(FLAGS_loops);
291 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700292 while (bench_plus_overhead < overhead) {
293 if (round++ == FLAGS_maxCalibrationAttempts) {
294 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700295 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700296 return kFailedLoops;
297 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700298 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700299 }
mtklein2069e222014-08-04 13:57:39 -0700300 }
mtkleinf3723212014-06-25 14:08:00 -0700301
mtkleinbb6a0282014-07-01 08:43:42 -0700302 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700303 // We'll pick N to make timer overhead negligible:
304 //
mtkleinbb6a0282014-07-01 08:43:42 -0700305 // overhead
306 // ------------------------- < FLAGS_overheadGoal
307 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700308 //
mtkleinbb6a0282014-07-01 08:43:42 -0700309 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700310 //
311 // Doing some math, we get:
312 //
mtkleinbb6a0282014-07-01 08:43:42 -0700313 // (overhead / FLAGS_overheadGoal) - overhead
314 // ------------------------------------------ < N
315 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700316 //
317 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700318 if (kAutoTuneLoops == loops) {
319 const double numer = overhead / FLAGS_overheadGoal - overhead;
320 const double denom = bench_plus_overhead - overhead;
321 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700322 loops = clamp_loops(loops);
323 } else {
324 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700325 }
mtkleinbb6a0282014-07-01 08:43:42 -0700326
mtkleinbb6a0282014-07-01 08:43:42 -0700327 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700328}
329
cdaltone1b89582015-06-25 19:17:08 -0700330static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700331 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700332 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700333 if (kAutoTuneLoops == loops) {
334 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700335 double elapsed = 0;
336 do {
mtklein527930f2014-11-06 08:04:34 -0800337 if (1<<30 == loops) {
338 // We're about to wrap. Something's wrong with the bench.
339 loops = 0;
340 break;
341 }
mtkleina189ccd2014-07-14 12:28:47 -0700342 loops *= 2;
343 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700344 // _this_ round, not still timing last round.
345 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700346 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700347 }
348 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700349
mtkleina189ccd2014-07-14 12:28:47 -0700350 // We've overshot at least a little. Scale back linearly.
351 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700352 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700353
tomhudsond968a6f2015-03-26 11:28:06 -0700354 // Make sure we're not still timing our calibration.
355 target->fence();
reed53249782014-10-10 09:09:52 -0700356 } else {
357 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700358 }
mtkleinbb6a0282014-07-01 08:43:42 -0700359
360 // Pretty much the same deal as the calibration: do some warmup to make
361 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700362 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700363 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700364 }
mtkleinbb6a0282014-07-01 08:43:42 -0700365
mtkleinbb6a0282014-07-01 08:43:42 -0700366 return loops;
367}
mtkleinbb6a0282014-07-01 08:43:42 -0700368
369static SkString to_lower(const char* str) {
370 SkString lower(str);
371 for (size_t i = 0; i < lower.size(); i++) {
372 lower[i] = tolower(lower[i]);
373 }
374 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700375}
376
bsalomonc2553372014-07-22 13:09:05 -0700377static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700378 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700379 if (to_lower(FLAGS_config[i]).equals(name)) {
380 return true;
mtkleinf3723212014-06-25 14:08:00 -0700381 }
382 }
bsalomonc2553372014-07-22 13:09:05 -0700383 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700384}
385
bsalomonc2553372014-07-22 13:09:05 -0700386#if SK_SUPPORT_GPU
387static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
kkinnunen5219fd92015-12-10 06:28:13 -0800388 GrContextFactory::GLContextOptions ctxOptions,
bsalomonc2553372014-07-22 13:09:05 -0700389 int sampleCnt) {
390 if (!is_cpu_config_allowed(name)) {
391 return false;
392 }
kkinnunen3e980c32015-12-23 01:33:00 -0800393 if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOptions)) {
bsalomon76228632015-05-29 08:02:10 -0700394 return sampleCnt <= ctx->caps()->maxSampleCount();
bsalomonc2553372014-07-22 13:09:05 -0700395 }
396 return false;
397}
398#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700399
bsalomonc2553372014-07-22 13:09:05 -0700400#if SK_SUPPORT_GPU
401#define kBogusGLContextType GrContextFactory::kNative_GLContextType
kkinnunen5219fd92015-12-10 06:28:13 -0800402#define kBogusGLContextOptions GrContextFactory::kNone_GLContextOptions
bsalomonc2553372014-07-22 13:09:05 -0700403#else
404#define kBogusGLContextType 0
kkinnunen5219fd92015-12-10 06:28:13 -0800405#define kBogusGLContextOptions 0
mtkleine714e752014-07-31 12:13:48 -0700406#endif
bsalomonc2553372014-07-22 13:09:05 -0700407
408// Append all configs that are enabled and supported.
409static void create_configs(SkTDArray<Config>* configs) {
kkinnunen5219fd92015-12-10 06:28:13 -0800410 #define CPU_CONFIG(name, backend, color, alpha) \
411 if (is_cpu_config_allowed(#name)) { \
412 Config config = { #name, Benchmark::backend, color, alpha, 0, \
413 kBogusGLContextType, kBogusGLContextOptions, \
414 false }; \
415 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700416 }
mtkleine714e752014-07-31 12:13:48 -0700417
mtklein40b32be2014-07-09 08:46:49 -0700418 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700419 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
420 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
421 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700422 }
mtkleinbb6a0282014-07-01 08:43:42 -0700423
424#if SK_SUPPORT_GPU
kkinnunen5219fd92015-12-10 06:28:13 -0800425 #define GPU_CONFIG(name, ctxType, ctxOptions, samples, useDFText) \
426 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, \
427 GrContextFactory::ctxOptions, samples)) { \
bsalomonc2553372014-07-22 13:09:05 -0700428 Config config = { \
429 #name, \
430 Benchmark::kGPU_Backend, \
431 kN32_SkColorType, \
432 kPremul_SkAlphaType, \
433 samples, \
jvanverth4736e142014-11-07 07:12:46 -0800434 GrContextFactory::ctxType, \
kkinnunen5219fd92015-12-10 06:28:13 -0800435 GrContextFactory::ctxOptions, \
jvanverth4736e142014-11-07 07:12:46 -0800436 useDFText }; \
bsalomonc2553372014-07-22 13:09:05 -0700437 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700438 }
mtkleine714e752014-07-31 12:13:48 -0700439
mtklein40b32be2014-07-09 08:46:49 -0700440 if (FLAGS_gpu) {
kkinnunen5219fd92015-12-10 06:28:13 -0800441 GPU_CONFIG(gpu, kNative_GLContextType, kNone_GLContextOptions, 0, false)
442 GPU_CONFIG(msaa4, kNative_GLContextType, kNone_GLContextOptions, 4, false)
443 GPU_CONFIG(msaa16, kNative_GLContextType, kNone_GLContextOptions, 16, false)
444 GPU_CONFIG(nvprmsaa4, kNative_GLContextType, kEnableNVPR_GLContextOptions, 4, false)
445 GPU_CONFIG(nvprmsaa16, kNative_GLContextType, kEnableNVPR_GLContextOptions, 16, false)
446 GPU_CONFIG(gpudft, kNative_GLContextType, kNone_GLContextOptions, 0, true)
447 GPU_CONFIG(debug, kDebug_GLContextType, kNone_GLContextOptions, 0, false)
448 GPU_CONFIG(nullgpu, kNull_GLContextType, kNone_GLContextOptions, 0, false)
kkinnunen3e980c32015-12-23 01:33:00 -0800449#if SK_ANGLE
450#ifdef SK_BUILD_FOR_WIN
kkinnunen5219fd92015-12-10 06:28:13 -0800451 GPU_CONFIG(angle, kANGLE_GLContextType, kNone_GLContextOptions, 0, false)
kkinnunen3e980c32015-12-23 01:33:00 -0800452#endif
kkinnunen5219fd92015-12-10 06:28:13 -0800453 GPU_CONFIG(angle-gl, kANGLE_GL_GLContextType, kNone_GLContextOptions, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700454#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800455#if SK_COMMAND_BUFFER
kkinnunen5219fd92015-12-10 06:28:13 -0800456 GPU_CONFIG(commandbuffer, kCommandBuffer_GLContextType, kNone_GLContextOptions, 0, false)
hendrikw885bf092015-08-27 10:38:39 -0700457#endif
cdaltond416a5b2015-06-23 13:23:44 -0700458#if SK_MESA
kkinnunen5219fd92015-12-10 06:28:13 -0800459 GPU_CONFIG(mesa, kMESA_GLContextType, kNone_GLContextOptions, 0, false)
cdaltond416a5b2015-06-23 13:23:44 -0700460#endif
mtklein40b32be2014-07-09 08:46:49 -0700461 }
mtkleinbb6a0282014-07-01 08:43:42 -0700462#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700463
464#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
465 if (is_cpu_config_allowed("hwui")) {
466 Config config = { "hwui", Benchmark::kHWUI_Backend, kRGBA_8888_SkColorType,
kkinnunen5219fd92015-12-10 06:28:13 -0800467 kPremul_SkAlphaType, 0, kBogusGLContextType, kBogusGLContextOptions,
468 false };
tomhudsond968a6f2015-03-26 11:28:06 -0700469 configs->push(config);
470 }
471#endif
mtkleinf3723212014-06-25 14:08:00 -0700472}
473
halcanary96fcdcc2015-08-27 07:41:13 -0700474// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700475static Target* is_enabled(Benchmark* bench, const Config& config) {
476 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700477 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700478 }
479
reede5ea5002014-09-03 11:54:58 -0700480 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
481 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700482
halcanary96fcdcc2015-08-27 07:41:13 -0700483 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700484
tomhudsond968a6f2015-03-26 11:28:06 -0700485 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700486#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700487 case Benchmark::kGPU_Backend:
488 target = new GPUTarget(config);
489 break;
bsalomonc2553372014-07-22 13:09:05 -0700490#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700491#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
492 case Benchmark::kHWUI_Backend:
493 target = new HWUITarget(config, bench);
494 break;
495#endif
496 default:
497 target = new Target(config);
498 break;
499 }
bsalomonc2553372014-07-22 13:09:05 -0700500
tomhudsond968a6f2015-03-26 11:28:06 -0700501 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700502 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700503 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700504 }
505 return target;
506}
507
msarett5cb48852015-11-06 08:56:32 -0800508static bool valid_brd_bench(SkData* encoded, SkBitmapRegionDecoder::Strategy strategy,
msarett7f691442015-09-22 11:56:16 -0700509 SkColorType colorType, uint32_t sampleSize, uint32_t minOutputSize, int* width,
510 int* height) {
msarett5cb48852015-11-06 08:56:32 -0800511 SkAutoTDelete<SkBitmapRegionDecoder> brd(
512 SkBitmapRegionDecoder::Create(encoded, strategy));
msarett7f691442015-09-22 11:56:16 -0700513 if (nullptr == brd.get()) {
514 // This is indicates that subset decoding is not supported for a particular image format.
515 return false;
516 }
517
msarett35e5d1b2015-10-27 12:50:25 -0700518 SkBitmap bitmap;
519 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, brd->width(), brd->height()),
520 1, colorType, false)) {
521 return false;
522 }
msarett7f691442015-09-22 11:56:16 -0700523
524 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
525 (uint32_t) brd->height()) {
526 // This indicates that the image is not large enough to decode a
527 // minOutputSize x minOutputSize subset at the given sampleSize.
528 return false;
529 }
530
531 // Set the image width and height. The calling code will use this to choose subsets to decode.
532 *width = brd->width();
533 *height = brd->height();
534 return true;
535}
536
egdaniel3bf92062015-06-26 08:12:46 -0700537static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700538 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700539#if SK_SUPPORT_GPU
540 if (FLAGS_abandonGpuContext) {
541 gGrFactory->abandonContexts();
542 }
543 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
544 gGrFactory->destroyContexts();
545 }
546#endif
547}
548
mtkleine714e752014-07-31 12:13:48 -0700549class BenchmarkStream {
550public:
mtklein92007582014-08-01 07:46:52 -0700551 BenchmarkStream() : fBenches(BenchRegistry::Head())
552 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700553 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700554 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800555 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800556 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700557 , fCurrentCodec(0)
msarett95f192d2015-02-13 09:05:41 -0800558 , fCurrentImage(0)
msarett7f691442015-09-22 11:56:16 -0700559 , fCurrentBRDImage(0)
msarett95f192d2015-02-13 09:05:41 -0800560 , fCurrentColorType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700561 , fCurrentSubsetType(0)
msarett7f691442015-09-22 11:56:16 -0700562 , fCurrentBRDStrategy(0)
563 , fCurrentBRDSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700564 , fCurrentAnimSKP(0) {
mtklein92007582014-08-01 07:46:52 -0700565 for (int i = 0; i < FLAGS_skps.count(); i++) {
566 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
567 fSKPs.push_back() = FLAGS_skps[i];
568 } else {
569 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
570 SkString path;
571 while (it.next(&path)) {
572 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
573 }
574 }
575 }
mtkleine714e752014-07-31 12:13:48 -0700576
mtklein92007582014-08-01 07:46:52 -0700577 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
578 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
579 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
580 exit(1);
581 }
582
583 for (int i = 0; i < FLAGS_scales.count(); i++) {
584 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
585 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
586 exit(1);
587 }
588 }
robertphillips5b693772014-11-21 06:19:36 -0800589
cdalton63a82852015-06-29 14:06:10 -0700590 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
591 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700592 exit(1);
593 }
594
robertphillips5b693772014-11-21 06:19:36 -0800595 if (FLAGS_mpd) {
596 fUseMPDs.push_back() = true;
597 }
mtkleinc751ecb2015-06-15 08:56:38 -0700598 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700599
msarett95f192d2015-02-13 09:05:41 -0800600 // Prepare the images for decoding
601 for (int i = 0; i < FLAGS_images.count(); i++) {
602 const char* flag = FLAGS_images[i];
603 if (sk_isdir(flag)) {
604 // If the value passed in is a directory, add all the images
605 SkOSFile::Iter it(flag);
606 SkString file;
607 while (it.next(&file)) {
608 fImages.push_back() = SkOSPath::Join(flag, file.c_str());
609 }
610 } else if (sk_exists(flag)) {
611 // Also add the value if it is a single image
612 fImages.push_back() = flag;
613 }
614 }
mtklein95553d92015-03-12 08:24:21 -0700615
msarett95f192d2015-02-13 09:05:41 -0800616 // Choose the candidate color types for image decoding
617 const SkColorType colorTypes[] =
msarettb23e6aa2015-06-09 13:56:10 -0700618 { kN32_SkColorType,
619 kRGB_565_SkColorType,
620 kAlpha_8_SkColorType,
621 kIndex_8_SkColorType,
622 kGray_8_SkColorType };
msarett74deb982015-10-20 16:45:56 -0700623 fColorTypes.reset(colorTypes, SK_ARRAY_COUNT(colorTypes));
mtklein92007582014-08-01 07:46:52 -0700624 }
625
mtkleinfd731ce2014-09-10 12:19:30 -0700626 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
627 // Not strictly necessary, as it will be checked again later,
628 // but helps to avoid a lot of pointless work if we're going to skip it.
629 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
630 return false;
631 }
632
scroggoa1193e42015-01-21 12:09:53 -0800633 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -0700634 if (stream.get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700635 SkDebugf("Could not read %s.\n", path);
636 return false;
637 }
638
mtklein57f27bd2015-02-09 11:58:41 -0800639 pic->reset(SkPicture::CreateFromStream(stream.get()));
halcanary96fcdcc2015-08-27 07:41:13 -0700640 if (pic->get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700641 SkDebugf("Could not read %s as an SkPicture.\n", path);
642 return false;
643 }
644 return true;
645 }
646
mtklein92007582014-08-01 07:46:52 -0700647 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700648 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700649 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700650 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700651 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700652 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700653 return bench;
654 }
mtklein92007582014-08-01 07:46:52 -0700655
mtkleine714e752014-07-31 12:13:48 -0700656 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -0700657 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700658 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800659 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700660 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700661 fBenchType = "micro";
halcanary385fe4d2015-08-26 13:07:48 -0700662 return new GMBench(gm.detach());
mtkleine714e752014-07-31 12:13:48 -0700663 }
664 }
mtklein92007582014-08-01 07:46:52 -0700665
mtkleinfd731ce2014-09-10 12:19:30 -0700666 // First add all .skps as RecordingBenches.
667 while (fCurrentRecording < fSKPs.count()) {
668 const SkString& path = fSKPs[fCurrentRecording++];
669 SkAutoTUnref<SkPicture> pic;
670 if (!ReadPicture(path.c_str(), &pic)) {
671 continue;
672 }
673 SkString name = SkOSPath::Basename(path.c_str());
674 fSourceType = "skp";
675 fBenchType = "recording";
bsalomon0aa5cea2014-12-15 09:13:35 -0800676 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic));
mtklein051e56d2014-12-04 08:46:51 -0800677 fSKPOps = pic->approximateOpCount();
halcanary385fe4d2015-08-26 13:07:48 -0700678 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700679 }
680
681 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700682 while (fCurrentScale < fScales.count()) {
683 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800684 const SkString& path = fSKPs[fCurrentSKP];
mtkleinfd731ce2014-09-10 12:19:30 -0700685 SkAutoTUnref<SkPicture> pic;
686 if (!ReadPicture(path.c_str(), &pic)) {
robertphillips5b693772014-11-21 06:19:36 -0800687 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700688 continue;
689 }
robertphillips5b693772014-11-21 06:19:36 -0800690
691 while (fCurrentUseMPD < fUseMPDs.count()) {
692 if (FLAGS_bbh) {
693 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
694 SkRTreeFactory factory;
695 SkPictureRecorder recorder;
696 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
697 pic->playback(recorder.beginRecording(pic->cullRect().width(),
698 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800699 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800700 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
robertphillips5b693772014-11-21 06:19:36 -0800701 pic.reset(recorder.endRecording());
702 }
703 SkString name = SkOSPath::Basename(path.c_str());
704 fSourceType = "skp";
705 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700706 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
707 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700708 }
robertphillips5b693772014-11-21 06:19:36 -0800709 fCurrentUseMPD = 0;
710 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700711 }
712 fCurrentSKP = 0;
713 fCurrentScale++;
714 }
715
joshualitt261c3ad2015-04-27 09:16:57 -0700716 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700717 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700718 while (fCurrentAnimSKP < fSKPs.count()) {
719 const SkString& path = fSKPs[fCurrentAnimSKP];
720 SkAutoTUnref<SkPicture> pic;
721 if (!ReadPicture(path.c_str(), &pic)) {
722 fCurrentAnimSKP++;
723 continue;
724 }
725
726 fCurrentAnimSKP++;
727 SkString name = SkOSPath::Basename(path.c_str());
cdalton63a82852015-06-29 14:06:10 -0700728 SkAutoTUnref<SKPAnimationBench::Animation> animation(
729 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
halcanary385fe4d2015-08-26 13:07:48 -0700730 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation,
731 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700732 }
733 }
734
scroggo60869a42015-04-01 12:09:17 -0700735 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700736 fSourceType = "image";
737 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700738 const SkString& path = fImages[fCurrentCodec];
739 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
740 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700741 if (!codec) {
742 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700743 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700744 continue;
745 }
scroggo21027992015-04-02 13:22:38 -0700746
scroggo60869a42015-04-01 12:09:17 -0700747 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700748 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo60869a42015-04-01 12:09:17 -0700749 fCurrentColorType++;
scroggo21027992015-04-02 13:22:38 -0700750
scroggo60869a42015-04-01 12:09:17 -0700751 // Make sure we can decode to this color type.
scroggo60869a42015-04-01 12:09:17 -0700752 SkImageInfo info = codec->getInfo().makeColorType(colorType);
scroggo21027992015-04-02 13:22:38 -0700753 SkAlphaType alphaType;
754 if (!SkColorTypeValidateAlphaType(colorType, info.alphaType(),
755 &alphaType)) {
756 continue;
757 }
758 if (alphaType != info.alphaType()) {
759 info = info.makeAlphaType(alphaType);
760 }
761
762 const size_t rowBytes = info.minRowBytes();
763 SkAutoMalloc storage(info.getSafeSize(rowBytes));
764
765 // Used if fCurrentColorType is kIndex_8_SkColorType
766 int colorCount = 256;
767 SkPMColor colors[256];
768
scroggoeb602a52015-07-09 08:16:03 -0700769 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700770 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700771 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700772 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700773 case SkCodec::kSuccess:
774 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700775 return new CodecBench(SkOSPath::Basename(path.c_str()),
776 encoded, colorType);
scroggoeb602a52015-07-09 08:16:03 -0700777 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700778 // This is okay. Not all conversions are valid.
779 break;
scroggo60869a42015-04-01 12:09:17 -0700780 default:
781 // This represents some sort of failure.
782 SkASSERT(false);
783 break;
784 }
785 }
786 fCurrentColorType = 0;
787 }
788
msarett95f192d2015-02-13 09:05:41 -0800789 // Run the DecodingBenches
790 while (fCurrentImage < fImages.count()) {
scroggo303fa352015-10-05 11:03:34 -0700791 fSourceType = "image";
792 fBenchType = "skimagedecoder";
msarett95f192d2015-02-13 09:05:41 -0800793 while (fCurrentColorType < fColorTypes.count()) {
794 const SkString& path = fImages[fCurrentImage];
795 SkColorType colorType = fColorTypes[fCurrentColorType];
796 fCurrentColorType++;
scroggo60869a42015-04-01 12:09:17 -0700797 // Check if the image decodes to the right color type
798 // before creating the benchmark
msarett95f192d2015-02-13 09:05:41 -0800799 SkBitmap bitmap;
800 if (SkImageDecoder::DecodeFile(path.c_str(), &bitmap,
scroggo60869a42015-04-01 12:09:17 -0700801 colorType, SkImageDecoder::kDecodePixels_Mode)
802 && bitmap.colorType() == colorType) {
msarett95f192d2015-02-13 09:05:41 -0800803 return new DecodingBench(path, colorType);
804 }
805 }
806 fCurrentColorType = 0;
807 fCurrentImage++;
808 }
809
msarett7f691442015-09-22 11:56:16 -0700810 // Run the BRDBenches
811 // We will benchmark multiple BRD strategies.
scroggo303fa352015-10-05 11:03:34 -0700812 static const struct {
msarett5cb48852015-11-06 08:56:32 -0800813 SkBitmapRegionDecoder::Strategy fStrategy;
msarett0459c942015-11-10 14:52:13 -0800814 const char* fName;
scroggo303fa352015-10-05 11:03:34 -0700815 } strategies[] = {
msarett0459c942015-11-10 14:52:13 -0800816 { SkBitmapRegionDecoder::kCanvas_Strategy, "BRD_canvas" },
msarett5cb48852015-11-06 08:56:32 -0800817 { SkBitmapRegionDecoder::kAndroidCodec_Strategy, "BRD_android_codec" },
msarett7f691442015-09-22 11:56:16 -0700818 };
819
820 // We intend to create benchmarks that model the use cases in
821 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
822 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
823 // the image. For that reason, we will benchmark decodes in five representative locations
824 // in the image. Additionally, this use case utilizes power of two scaling, so we will
825 // test on power of two sample sizes. The output tile is always 512x512, so, when a
826 // sampleSize is used, the size of the subset that is decoded is always
827 // (sampleSize*512)x(sampleSize*512).
828 // There are a few good reasons to only test on power of two sample sizes at this time:
829 // JPEG decodes using kOriginal_Strategy are broken for non-powers of two.
halcanary6950de62015-11-07 05:29:00 -0800830 // https://bug.skia.org/4319
msarett7f691442015-09-22 11:56:16 -0700831 // All use cases we are aware of only scale by powers of two.
832 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
833 // these tests are sufficient to provide good coverage of our scaling options.
scroggo501b7342015-11-03 07:55:11 -0800834 const uint32_t sampleSizes[] = { 1, 2, 4, 8, 16, 32, 64 };
msarett7f691442015-09-22 11:56:16 -0700835 const uint32_t minOutputSize = 512;
836 while (fCurrentBRDImage < fImages.count()) {
837 while (fCurrentBRDStrategy < (int) SK_ARRAY_COUNT(strategies)) {
scroggo303fa352015-10-05 11:03:34 -0700838 fSourceType = "image";
839 fBenchType = strategies[fCurrentBRDStrategy].fName;
scroggo860e8a62015-10-15 07:51:28 -0700840
841 const SkString& path = fImages[fCurrentBRDImage];
msarett5cb48852015-11-06 08:56:32 -0800842 const SkBitmapRegionDecoder::Strategy strategy =
scroggo860e8a62015-10-15 07:51:28 -0700843 strategies[fCurrentBRDStrategy].fStrategy;
844
msarett7f691442015-09-22 11:56:16 -0700845 while (fCurrentColorType < fColorTypes.count()) {
846 while (fCurrentBRDSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
847 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700848
849
msarett7f691442015-09-22 11:56:16 -0700850 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
851 const SkColorType colorType = fColorTypes[fCurrentColorType];
852 uint32_t sampleSize = sampleSizes[fCurrentBRDSampleSize];
853 int currentSubsetType = fCurrentSubsetType++;
854
855 int width = 0;
856 int height = 0;
857 if (!valid_brd_bench(encoded.get(), strategy, colorType, sampleSize,
858 minOutputSize, &width, &height)) {
859 break;
860 }
861
862 SkString basename = SkOSPath::Basename(path.c_str());
863 SkIRect subset;
864 const uint32_t subsetSize = sampleSize * minOutputSize;
865 switch (currentSubsetType) {
866 case kTopLeft_SubsetType:
867 basename.append("_TopLeft");
868 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
869 break;
870 case kTopRight_SubsetType:
871 basename.append("_TopRight");
872 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
873 subsetSize);
874 break;
875 case kMiddle_SubsetType:
876 basename.append("_Middle");
877 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
878 (height - subsetSize) / 2, subsetSize, subsetSize);
879 break;
880 case kBottomLeft_SubsetType:
881 basename.append("_BottomLeft");
882 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
883 subsetSize);
884 break;
885 case kBottomRight_SubsetType:
886 basename.append("_BottomRight");
887 subset = SkIRect::MakeXYWH(width - subsetSize,
888 height - subsetSize, subsetSize, subsetSize);
889 break;
890 default:
891 SkASSERT(false);
892 }
893
894 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
895 strategy, colorType, sampleSize, subset);
896 }
897 fCurrentSubsetType = 0;
898 fCurrentBRDSampleSize++;
899 }
900 fCurrentBRDSampleSize = 0;
901 fCurrentColorType++;
902 }
903 fCurrentColorType = 0;
904 fCurrentBRDStrategy++;
905 }
906 fCurrentBRDStrategy = 0;
907 fCurrentBRDImage++;
908 }
909
halcanary96fcdcc2015-08-27 07:41:13 -0700910 return nullptr;
mtkleine714e752014-07-31 12:13:48 -0700911 }
mtklein92007582014-08-01 07:46:52 -0700912
913 void fillCurrentOptions(ResultsWriter* log) const {
914 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700915 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700916 if (0 == strcmp(fSourceType, "skp")) {
917 log->configOption("clip",
918 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
919 fClip.fRight, fClip.fBottom).c_str());
mtklein4dfdbb12015-10-20 07:45:29 -0700920 SK_ALWAYSBREAK(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -0700921 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -0800922 if (fCurrentUseMPD > 0) {
923 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
924 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
925 }
mtklein92007582014-08-01 07:46:52 -0700926 }
mtklein051e56d2014-12-04 08:46:51 -0800927 if (0 == strcmp(fBenchType, "recording")) {
928 log->metric("bytes", fSKPBytes);
929 log->metric("ops", fSKPOps);
930 }
mtklein92007582014-08-01 07:46:52 -0700931 }
932
mtkleine714e752014-07-31 12:13:48 -0700933private:
msarettb23e6aa2015-06-09 13:56:10 -0700934 enum SubsetType {
935 kTopLeft_SubsetType = 0,
936 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -0700937 kMiddle_SubsetType = 2,
938 kBottomLeft_SubsetType = 3,
939 kBottomRight_SubsetType = 4,
940 kTranslate_SubsetType = 5,
941 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -0700942 kLast_SubsetType = kZoom_SubsetType,
943 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -0700944 };
945
mtkleine714e752014-07-31 12:13:48 -0700946 const BenchRegistry* fBenches;
947 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700948 SkIRect fClip;
949 SkTArray<SkScalar> fScales;
950 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -0800951 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -0800952 SkTArray<SkString> fImages;
msarett74deb982015-10-20 16:45:56 -0700953 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -0700954 SkScalar fZoomMax;
955 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -0700956
mtklein051e56d2014-12-04 08:46:51 -0800957 double fSKPBytes, fSKPOps;
958
mtkleinfd731ce2014-09-10 12:19:30 -0700959 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
960 const char* fBenchType; // How we bench it: micro, recording, playback, ...
961 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700962 int fCurrentScale;
963 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -0800964 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -0700965 int fCurrentCodec;
msarett95f192d2015-02-13 09:05:41 -0800966 int fCurrentImage;
msarett7f691442015-09-22 11:56:16 -0700967 int fCurrentBRDImage;
msarett95f192d2015-02-13 09:05:41 -0800968 int fCurrentColorType;
msarettb23e6aa2015-06-09 13:56:10 -0700969 int fCurrentSubsetType;
msarett7f691442015-09-22 11:56:16 -0700970 int fCurrentBRDStrategy;
971 int fCurrentBRDSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -0700972 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -0700973};
974
jcgregorio3b27ade2014-11-13 08:06:40 -0800975int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700976int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800977 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -0700978 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -0700979 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -0700980
krajcevski69a55602014-08-13 10:46:31 -0700981#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -0700982 GrContextOptions grContextOpts;
krajcevski12b35442014-08-13 12:06:26 -0700983 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
halcanary385fe4d2015-08-26 13:07:48 -0700984 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -0700985#endif
986
bsalomon06cddec2014-10-24 10:40:50 -0700987 if (FLAGS_veryVerbose) {
988 FLAGS_verbose = true;
989 }
990
bsalomon6eb03cc2014-08-07 14:28:50 -0700991 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700992 FLAGS_samples = 1;
993 FLAGS_gpuFrameLag = 0;
994 }
995
bsalomon6eb03cc2014-08-07 14:28:50 -0700996 if (!FLAGS_writePath.isEmpty()) {
997 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
998 if (!sk_mkdir(FLAGS_writePath[0])) {
999 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001000 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001001 }
1002 }
1003
halcanary385fe4d2015-08-26 13:07:48 -07001004 SkAutoTDelete<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001005 if (!FLAGS_outResultsFile.isEmpty()) {
halcanary385fe4d2015-08-26 13:07:48 -07001006 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein60317d0f2014-07-14 11:30:37 -07001007 }
mtklein1915b622014-08-20 11:45:00 -07001008
1009 if (1 == FLAGS_properties.count() % 2) {
1010 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1011 return 1;
1012 }
1013 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1014 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1015 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001016
1017 if (1 == FLAGS_key.count() % 2) {
1018 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1019 return 1;
1020 }
1021 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001022 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001023 }
mtklein60317d0f2014-07-14 11:30:37 -07001024
mtkleinf3723212014-06-25 14:08:00 -07001025 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001026 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001027
cdaltone1b89582015-06-25 19:17:08 -07001028 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001029
bsalomon6eb03cc2014-08-07 14:28:50 -07001030 if (kAutoTuneLoops != FLAGS_loops) {
1031 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001032 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001033 SkDebugf("! -> high variance, ? -> moderate variance\n");
1034 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001035 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001036 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001037 } else {
mtkleind75c4662015-04-30 07:11:22 -07001038 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001039 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001040 }
1041
bsalomonc2553372014-07-22 13:09:05 -07001042 SkTDArray<Config> configs;
1043 create_configs(&configs);
1044
mtkleine070c2b2014-10-14 08:40:43 -07001045 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001046 BenchmarkStream benchStream;
1047 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001048 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001049 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001050 continue;
1051 }
1052
egdaniel3bf92062015-06-26 08:12:46 -07001053 if (!configs.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -07001054 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001055 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001056 }
egdaniel3bf92062015-06-26 08:12:46 -07001057 for (int i = 0; i < configs.count(); ++i) {
1058 Target* target = is_enabled(b, configs[i]);
1059 if (!target) {
1060 continue;
1061 }
mtkleinf3723212014-06-25 14:08:00 -07001062
halcanary96fcdcc2015-08-27 07:41:13 -07001063 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001064 SkCanvas* canvas = target->getCanvas();
1065 const char* config = target->config.name;
1066
1067 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001068 bench->perCanvasPreDraw(canvas);
1069
cdaltone1b89582015-06-25 19:17:08 -07001070 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001071 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001072 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1073 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001074
mtkleinbbba1682015-10-28 11:36:30 -07001075 if (FLAGS_ms) {
1076 samples.reset();
1077 auto stop = now_ms() + FLAGS_ms;
1078 do {
1079 samples.push_back(time(loops, bench, target) / loops);
1080 } while (now_ms() < stop);
1081 } else {
cdaltone1b89582015-06-25 19:17:08 -07001082 samples.reset(FLAGS_samples);
1083 for (int s = 0; s < FLAGS_samples; s++) {
egdaniel3bf92062015-06-26 08:12:46 -07001084 samples[s] = time(loops, bench, target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001085 }
cdaltone1b89582015-06-25 19:17:08 -07001086 }
mtkleinf3723212014-06-25 14:08:00 -07001087
joshualitte45c81c2015-12-02 09:05:37 -08001088#if SK_SUPPORT_GPU
1089 SkTArray<SkString> keys;
1090 SkTArray<double> values;
1091 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1092 if (gpuStatsDump) {
1093 // TODO cache stats
1094 bench->getGpuStats(canvas, &keys, &values);
1095 }
1096#endif
1097
robertphillips5b693772014-11-21 06:19:36 -08001098 bench->perCanvasPostDraw(canvas);
1099
egdaniel3bf92062015-06-26 08:12:46 -07001100 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001101 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001102 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001103 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001104 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001105 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001106 }
1107
1108 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001109 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001110 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001111 continue;
1112 }
1113
cdaltone1b89582015-06-25 19:17:08 -07001114 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001115 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001116 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001117 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001118 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001119 log->metric("min_ms", stats.min);
joshualitte45c81c2015-12-02 09:05:37 -08001120#if SK_SUPPORT_GPU
1121 if (gpuStatsDump) {
1122 // dump to json, only SKPBench currently returns valid keys / values
1123 SkASSERT(keys.count() == values.count());
1124 for (int i = 0; i < keys.count(); i++) {
1125 log->metric(keys[i].c_str(), values[i]);
1126 }
1127 }
1128#endif
1129
mtkleine070c2b2014-10-14 08:40:43 -07001130 if (runs++ % FLAGS_flushEvery == 0) {
1131 log->flush();
1132 }
mtklein60317d0f2014-07-14 11:30:37 -07001133
bsalomon6eb03cc2014-08-07 14:28:50 -07001134 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001135 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001136 config = ""; // Only print the config if we run the same bench on more than one.
1137 }
mtkleind75c4662015-04-30 07:11:22 -07001138 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1139 , sk_tools::getCurrResidentSetSizeMB()
1140 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001141 , bench->getUniqueName()
1142 , config);
mtkleinf3723212014-06-25 14:08:00 -07001143 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001144 const char* mark = " ";
1145 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1146 if (stddev_percent > 5) mark = "?";
1147 if (stddev_percent > 10) mark = "!";
1148
1149 SkDebugf("%10.2f %s\t%s\t%s\n",
1150 stats.median*1e3, mark, bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001151 } else {
1152 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001153 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1154 , sk_tools::getCurrResidentSetSizeMB()
1155 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001156 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001157 , HUMANIZE(stats.min)
1158 , HUMANIZE(stats.median)
1159 , HUMANIZE(stats.mean)
1160 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001161 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001162 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001163 , config
mtklein96289052014-09-10 12:05:59 -07001164 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001165 );
1166 }
joshualitte45c81c2015-12-02 09:05:37 -08001167
bsalomonb12ea412015-02-02 21:19:50 -08001168#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001169 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001170 GrContext* context = gGrFactory->get(configs[i].ctxType,
kkinnunen3e980c32015-12-23 01:33:00 -08001171 configs[i].ctxOptions);
kkinnunen5219fd92015-12-10 06:28:13 -08001172 context->printCacheStats();
1173 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001174 }
1175#endif
joshualitte45c81c2015-12-02 09:05:37 -08001176
cdalton2c56ba52015-06-26 13:32:53 -07001177 if (FLAGS_verbose) {
1178 SkDebugf("Samples: ");
1179 for (int i = 0; i < samples.count(); i++) {
1180 SkDebugf("%s ", HUMANIZE(samples[i]));
1181 }
1182 SkDebugf("%s\n", bench->getUniqueName());
1183 }
egdaniel3bf92062015-06-26 08:12:46 -07001184 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001185 }
mtkleinf3723212014-06-25 14:08:00 -07001186 }
1187
mtkleine1091452014-12-04 10:47:02 -08001188 log->bench("memory_usage", 0,0);
1189 log->config("meta");
1190 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1191
joshualitte0b19d42015-03-26 10:41:02 -07001192#if SK_SUPPORT_GPU
1193 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1194 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001195 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001196#endif
1197
mtkleinf3723212014-06-25 14:08:00 -07001198 return 0;
1199}
1200
jcgregorio3b27ade2014-11-13 08:06:40 -08001201#if !defined SK_BUILD_FOR_IOS
1202int main(int argc, char** argv) {
1203 SkCommandLineFlags::Parse(argc, argv);
1204 return nanobench_main();
1205}
1206#endif