blob: 11119be2e193b9900b84c7fb4c1981ea533c5388 [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"
msarett95f192d2015-02-13 09:05:41 -080031#include "SkData.h"
mtkleinf3723212014-06-25 14:08:00 -070032#include "SkForceLinking.h"
33#include "SkGraphics.h"
mtklein20840502014-08-21 15:51:22 -070034#include "SkOSFile.h"
35#include "SkPictureRecorder.h"
mtklein051e56d2014-12-04 08:46:51 -080036#include "SkPictureUtils.h"
mtkleinf3723212014-06-25 14:08:00 -070037#include "SkString.h"
38#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080039#include "SkTaskGroup.h"
mtkleinf3723212014-06-25 14:08:00 -070040
bungeman60e0fee2015-08-26 05:15:46 -070041#include <stdlib.h>
42
tomhudsond968a6f2015-03-26 11:28:06 -070043#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
44 #include "nanobenchAndroid.h"
45#endif
46
mtkleinbb6a0282014-07-01 08:43:42 -070047#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070048 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070049 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070050 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070051 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070052#endif
53
bsalomon682c2692015-05-22 14:01:46 -070054 struct GrContextOptions;
55
mtkleinf3723212014-06-25 14:08:00 -070056__SK_FORCE_IMAGE_DECODER_LINKING;
57
reed53249782014-10-10 09:09:52 -070058static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070059
mtkleinb5110422014-08-07 15:20:02 -070060static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070061#ifdef SK_DEBUG
62 1;
mtkleina189ccd2014-07-14 12:28:47 -070063#else
bsalomon6eb03cc2014-08-07 14:28:50 -070064 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070065#endif
66
bsalomon6eb03cc2014-08-07 14:28:50 -070067static SkString loops_help_txt() {
68 SkString help;
69 help.printf("Number of times to run each bench. Set this to %d to auto-"
70 "tune for each bench. Timings are only reported when auto-tuning.",
71 kAutoTuneLoops);
72 return help;
73}
74
cdaltone1b89582015-06-25 19:17:08 -070075static SkString to_string(int n) {
76 SkString str;
77 str.appendS32(n);
78 return str;
79}
80
bsalomon6eb03cc2014-08-07 14:28:50 -070081DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
82
mtkleinf3723212014-06-25 14:08:00 -070083DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -070084DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -070085DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
86DEFINE_double(overheadGoal, 0.0001,
87 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070088DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -070089DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070090DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
91 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070092
mtklein60317d0f2014-07-14 11:30:37 -070093DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -070094DEFINE_int32(maxCalibrationAttempts, 3,
95 "Try up to this many times to guess loops for a bench, or skip the bench.");
96DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -070097DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
98DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -070099DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
100 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700101DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
robertphillips5b693772014-11-21 06:19:36 -0800102DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700103DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700104DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800105DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800106DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800107DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
mtklein92007582014-08-01 07:46:52 -0700108
mtkleinbbba1682015-10-28 11:36:30 -0700109static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
110
mtkleinf3723212014-06-25 14:08:00 -0700111static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700112 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800113 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700114}
mtklein55b0ffc2014-07-17 08:38:23 -0700115#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700116
tomhudsond968a6f2015-03-26 11:28:06 -0700117bool Target::init(SkImageInfo info, Benchmark* bench) {
118 if (Benchmark::kRaster_Backend == config.backend) {
119 this->surface.reset(SkSurface::NewRaster(info));
120 if (!this->surface.get()) {
121 return false;
122 }
123 }
124 return true;
125}
126bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700127 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700128 if (!canvas) {
129 return false;
130 }
131 bmp->setInfo(canvas->imageInfo());
132 if (!canvas->readPixels(bmp, 0, 0)) {
133 SkDebugf("Can't read canvas pixels.\n");
134 return false;
135 }
136 return true;
137}
138
139#if SK_SUPPORT_GPU
140struct GPUTarget : public Target {
halcanary96fcdcc2015-08-27 07:41:13 -0700141 explicit GPUTarget(const Config& c) : Target(c), gl(nullptr) { }
tomhudsond968a6f2015-03-26 11:28:06 -0700142 SkGLContext* gl;
143
144 void setup() override {
145 this->gl->makeCurrent();
146 // Make sure we're done with whatever came before.
147 SK_GL(*this->gl, Finish());
148 }
149 void endTiming() override {
150 if (this->gl) {
151 SK_GL(*this->gl, Flush());
152 this->gl->swapBuffers();
153 }
154 }
155 void fence() override {
156 SK_GL(*this->gl, Finish());
157 }
mtkleind75c4662015-04-30 07:11:22 -0700158
cdaltond416a5b2015-06-23 13:23:44 -0700159 bool needsFrameTiming(int* maxFrameLag) const override {
160 if (!this->gl->getMaxGpuFrameLag(maxFrameLag)) {
161 // Frame lag is unknown.
162 *maxFrameLag = FLAGS_gpuFrameLag;
163 }
164 return true;
165 }
tomhudsond968a6f2015-03-26 11:28:06 -0700166 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700167 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
168 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700169 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
kkinnunen5219fd92015-12-10 06:28:13 -0800170 this->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(this->config.ctxType,
rmistry0f515bd2015-12-22 10:22:26 -0800171 kNone_GrGLStandard,
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));
rmistry0f515bd2015-12-22 10:22:26 -0800175 this->gl = gGrFactory->getContextInfo(this->config.ctxType, kNone_GrGLStandard,
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 }
rmistry0f515bd2015-12-22 10:22:26 -0800393 if (const GrContext* ctx = gGrFactory->get(ctxType, kNone_GrGLStandard, 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)
rmistry0f515bd2015-12-22 10:22:26 -0800449#ifdef SK_ANGLE
kkinnunen5219fd92015-12-10 06:28:13 -0800450 GPU_CONFIG(angle, kANGLE_GLContextType, kNone_GLContextOptions, 0, false)
451 GPU_CONFIG(angle-gl, kANGLE_GL_GLContextType, kNone_GLContextOptions, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700452#endif
rmistry0f515bd2015-12-22 10:22:26 -0800453#ifdef SK_COMMAND_BUFFER
kkinnunen5219fd92015-12-10 06:28:13 -0800454 GPU_CONFIG(commandbuffer, kCommandBuffer_GLContextType, kNone_GLContextOptions, 0, false)
hendrikw885bf092015-08-27 10:38:39 -0700455#endif
cdaltond416a5b2015-06-23 13:23:44 -0700456#if SK_MESA
kkinnunen5219fd92015-12-10 06:28:13 -0800457 GPU_CONFIG(mesa, kMESA_GLContextType, kNone_GLContextOptions, 0, false)
cdaltond416a5b2015-06-23 13:23:44 -0700458#endif
mtklein40b32be2014-07-09 08:46:49 -0700459 }
mtkleinbb6a0282014-07-01 08:43:42 -0700460#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700461
462#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
463 if (is_cpu_config_allowed("hwui")) {
464 Config config = { "hwui", Benchmark::kHWUI_Backend, kRGBA_8888_SkColorType,
kkinnunen5219fd92015-12-10 06:28:13 -0800465 kPremul_SkAlphaType, 0, kBogusGLContextType, kBogusGLContextOptions,
466 false };
tomhudsond968a6f2015-03-26 11:28:06 -0700467 configs->push(config);
468 }
469#endif
mtkleinf3723212014-06-25 14:08:00 -0700470}
471
halcanary96fcdcc2015-08-27 07:41:13 -0700472// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700473static Target* is_enabled(Benchmark* bench, const Config& config) {
474 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700475 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700476 }
477
reede5ea5002014-09-03 11:54:58 -0700478 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
479 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700480
halcanary96fcdcc2015-08-27 07:41:13 -0700481 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700482
tomhudsond968a6f2015-03-26 11:28:06 -0700483 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700484#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700485 case Benchmark::kGPU_Backend:
486 target = new GPUTarget(config);
487 break;
bsalomonc2553372014-07-22 13:09:05 -0700488#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700489#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
490 case Benchmark::kHWUI_Backend:
491 target = new HWUITarget(config, bench);
492 break;
493#endif
494 default:
495 target = new Target(config);
496 break;
497 }
bsalomonc2553372014-07-22 13:09:05 -0700498
tomhudsond968a6f2015-03-26 11:28:06 -0700499 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700500 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700501 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700502 }
503 return target;
504}
505
msarett5cb48852015-11-06 08:56:32 -0800506static bool valid_brd_bench(SkData* encoded, SkBitmapRegionDecoder::Strategy strategy,
msarett7f691442015-09-22 11:56:16 -0700507 SkColorType colorType, uint32_t sampleSize, uint32_t minOutputSize, int* width,
508 int* height) {
msarett5cb48852015-11-06 08:56:32 -0800509 SkAutoTDelete<SkBitmapRegionDecoder> brd(
510 SkBitmapRegionDecoder::Create(encoded, strategy));
msarett7f691442015-09-22 11:56:16 -0700511 if (nullptr == brd.get()) {
512 // This is indicates that subset decoding is not supported for a particular image format.
513 return false;
514 }
515
msarett35e5d1b2015-10-27 12:50:25 -0700516 SkBitmap bitmap;
517 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, brd->width(), brd->height()),
518 1, colorType, false)) {
519 return false;
520 }
msarett7f691442015-09-22 11:56:16 -0700521
522 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
523 (uint32_t) brd->height()) {
524 // This indicates that the image is not large enough to decode a
525 // minOutputSize x minOutputSize subset at the given sampleSize.
526 return false;
527 }
528
529 // Set the image width and height. The calling code will use this to choose subsets to decode.
530 *width = brd->width();
531 *height = brd->height();
532 return true;
533}
534
egdaniel3bf92062015-06-26 08:12:46 -0700535static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700536 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700537#if SK_SUPPORT_GPU
538 if (FLAGS_abandonGpuContext) {
539 gGrFactory->abandonContexts();
540 }
541 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
542 gGrFactory->destroyContexts();
543 }
544#endif
545}
546
mtkleine714e752014-07-31 12:13:48 -0700547class BenchmarkStream {
548public:
mtklein92007582014-08-01 07:46:52 -0700549 BenchmarkStream() : fBenches(BenchRegistry::Head())
550 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700551 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700552 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800553 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800554 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700555 , fCurrentCodec(0)
msarett95f192d2015-02-13 09:05:41 -0800556 , fCurrentImage(0)
msarett7f691442015-09-22 11:56:16 -0700557 , fCurrentBRDImage(0)
msarett95f192d2015-02-13 09:05:41 -0800558 , fCurrentColorType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700559 , fCurrentSubsetType(0)
msarett7f691442015-09-22 11:56:16 -0700560 , fCurrentBRDStrategy(0)
561 , fCurrentBRDSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700562 , fCurrentAnimSKP(0) {
mtklein92007582014-08-01 07:46:52 -0700563 for (int i = 0; i < FLAGS_skps.count(); i++) {
564 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
565 fSKPs.push_back() = FLAGS_skps[i];
566 } else {
567 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
568 SkString path;
569 while (it.next(&path)) {
570 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
571 }
572 }
573 }
mtkleine714e752014-07-31 12:13:48 -0700574
mtklein92007582014-08-01 07:46:52 -0700575 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
576 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
577 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
578 exit(1);
579 }
580
581 for (int i = 0; i < FLAGS_scales.count(); i++) {
582 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
583 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
584 exit(1);
585 }
586 }
robertphillips5b693772014-11-21 06:19:36 -0800587
cdalton63a82852015-06-29 14:06:10 -0700588 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
589 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700590 exit(1);
591 }
592
robertphillips5b693772014-11-21 06:19:36 -0800593 if (FLAGS_mpd) {
594 fUseMPDs.push_back() = true;
595 }
mtkleinc751ecb2015-06-15 08:56:38 -0700596 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700597
msarett95f192d2015-02-13 09:05:41 -0800598 // Prepare the images for decoding
599 for (int i = 0; i < FLAGS_images.count(); i++) {
600 const char* flag = FLAGS_images[i];
601 if (sk_isdir(flag)) {
602 // If the value passed in is a directory, add all the images
603 SkOSFile::Iter it(flag);
604 SkString file;
605 while (it.next(&file)) {
606 fImages.push_back() = SkOSPath::Join(flag, file.c_str());
607 }
608 } else if (sk_exists(flag)) {
609 // Also add the value if it is a single image
610 fImages.push_back() = flag;
611 }
612 }
mtklein95553d92015-03-12 08:24:21 -0700613
msarett95f192d2015-02-13 09:05:41 -0800614 // Choose the candidate color types for image decoding
615 const SkColorType colorTypes[] =
msarettb23e6aa2015-06-09 13:56:10 -0700616 { kN32_SkColorType,
617 kRGB_565_SkColorType,
618 kAlpha_8_SkColorType,
619 kIndex_8_SkColorType,
620 kGray_8_SkColorType };
msarett74deb982015-10-20 16:45:56 -0700621 fColorTypes.reset(colorTypes, SK_ARRAY_COUNT(colorTypes));
mtklein92007582014-08-01 07:46:52 -0700622 }
623
mtkleinfd731ce2014-09-10 12:19:30 -0700624 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
625 // Not strictly necessary, as it will be checked again later,
626 // but helps to avoid a lot of pointless work if we're going to skip it.
627 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
628 return false;
629 }
630
scroggoa1193e42015-01-21 12:09:53 -0800631 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -0700632 if (stream.get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700633 SkDebugf("Could not read %s.\n", path);
634 return false;
635 }
636
mtklein57f27bd2015-02-09 11:58:41 -0800637 pic->reset(SkPicture::CreateFromStream(stream.get()));
halcanary96fcdcc2015-08-27 07:41:13 -0700638 if (pic->get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700639 SkDebugf("Could not read %s as an SkPicture.\n", path);
640 return false;
641 }
642 return true;
643 }
644
mtklein92007582014-08-01 07:46:52 -0700645 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700646 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700647 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700648 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700649 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700650 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700651 return bench;
652 }
mtklein92007582014-08-01 07:46:52 -0700653
mtkleine714e752014-07-31 12:13:48 -0700654 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -0700655 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700656 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800657 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700658 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700659 fBenchType = "micro";
halcanary385fe4d2015-08-26 13:07:48 -0700660 return new GMBench(gm.detach());
mtkleine714e752014-07-31 12:13:48 -0700661 }
662 }
mtklein92007582014-08-01 07:46:52 -0700663
mtkleinfd731ce2014-09-10 12:19:30 -0700664 // First add all .skps as RecordingBenches.
665 while (fCurrentRecording < fSKPs.count()) {
666 const SkString& path = fSKPs[fCurrentRecording++];
667 SkAutoTUnref<SkPicture> pic;
668 if (!ReadPicture(path.c_str(), &pic)) {
669 continue;
670 }
671 SkString name = SkOSPath::Basename(path.c_str());
672 fSourceType = "skp";
673 fBenchType = "recording";
bsalomon0aa5cea2014-12-15 09:13:35 -0800674 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic));
mtklein051e56d2014-12-04 08:46:51 -0800675 fSKPOps = pic->approximateOpCount();
halcanary385fe4d2015-08-26 13:07:48 -0700676 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700677 }
678
679 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700680 while (fCurrentScale < fScales.count()) {
681 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800682 const SkString& path = fSKPs[fCurrentSKP];
mtkleinfd731ce2014-09-10 12:19:30 -0700683 SkAutoTUnref<SkPicture> pic;
684 if (!ReadPicture(path.c_str(), &pic)) {
robertphillips5b693772014-11-21 06:19:36 -0800685 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700686 continue;
687 }
robertphillips5b693772014-11-21 06:19:36 -0800688
689 while (fCurrentUseMPD < fUseMPDs.count()) {
690 if (FLAGS_bbh) {
691 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
692 SkRTreeFactory factory;
693 SkPictureRecorder recorder;
694 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
695 pic->playback(recorder.beginRecording(pic->cullRect().width(),
696 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800697 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800698 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
robertphillips5b693772014-11-21 06:19:36 -0800699 pic.reset(recorder.endRecording());
700 }
701 SkString name = SkOSPath::Basename(path.c_str());
702 fSourceType = "skp";
703 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700704 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
705 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700706 }
robertphillips5b693772014-11-21 06:19:36 -0800707 fCurrentUseMPD = 0;
708 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700709 }
710 fCurrentSKP = 0;
711 fCurrentScale++;
712 }
713
joshualitt261c3ad2015-04-27 09:16:57 -0700714 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700715 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700716 while (fCurrentAnimSKP < fSKPs.count()) {
717 const SkString& path = fSKPs[fCurrentAnimSKP];
718 SkAutoTUnref<SkPicture> pic;
719 if (!ReadPicture(path.c_str(), &pic)) {
720 fCurrentAnimSKP++;
721 continue;
722 }
723
724 fCurrentAnimSKP++;
725 SkString name = SkOSPath::Basename(path.c_str());
cdalton63a82852015-06-29 14:06:10 -0700726 SkAutoTUnref<SKPAnimationBench::Animation> animation(
727 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
halcanary385fe4d2015-08-26 13:07:48 -0700728 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation,
729 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700730 }
731 }
732
scroggo60869a42015-04-01 12:09:17 -0700733 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700734 fSourceType = "image";
735 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700736 const SkString& path = fImages[fCurrentCodec];
737 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
738 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700739 if (!codec) {
740 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700741 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700742 continue;
743 }
scroggo21027992015-04-02 13:22:38 -0700744
scroggo60869a42015-04-01 12:09:17 -0700745 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700746 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo60869a42015-04-01 12:09:17 -0700747 fCurrentColorType++;
scroggo21027992015-04-02 13:22:38 -0700748
scroggo60869a42015-04-01 12:09:17 -0700749 // Make sure we can decode to this color type.
scroggo60869a42015-04-01 12:09:17 -0700750 SkImageInfo info = codec->getInfo().makeColorType(colorType);
scroggo21027992015-04-02 13:22:38 -0700751 SkAlphaType alphaType;
752 if (!SkColorTypeValidateAlphaType(colorType, info.alphaType(),
753 &alphaType)) {
754 continue;
755 }
756 if (alphaType != info.alphaType()) {
757 info = info.makeAlphaType(alphaType);
758 }
759
760 const size_t rowBytes = info.minRowBytes();
761 SkAutoMalloc storage(info.getSafeSize(rowBytes));
762
763 // Used if fCurrentColorType is kIndex_8_SkColorType
764 int colorCount = 256;
765 SkPMColor colors[256];
766
scroggoeb602a52015-07-09 08:16:03 -0700767 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700768 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700769 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700770 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700771 case SkCodec::kSuccess:
772 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700773 return new CodecBench(SkOSPath::Basename(path.c_str()),
774 encoded, colorType);
scroggoeb602a52015-07-09 08:16:03 -0700775 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700776 // This is okay. Not all conversions are valid.
777 break;
scroggo60869a42015-04-01 12:09:17 -0700778 default:
779 // This represents some sort of failure.
780 SkASSERT(false);
781 break;
782 }
783 }
784 fCurrentColorType = 0;
785 }
786
msarett95f192d2015-02-13 09:05:41 -0800787 // Run the DecodingBenches
788 while (fCurrentImage < fImages.count()) {
scroggo303fa352015-10-05 11:03:34 -0700789 fSourceType = "image";
790 fBenchType = "skimagedecoder";
msarett95f192d2015-02-13 09:05:41 -0800791 while (fCurrentColorType < fColorTypes.count()) {
792 const SkString& path = fImages[fCurrentImage];
793 SkColorType colorType = fColorTypes[fCurrentColorType];
794 fCurrentColorType++;
scroggo60869a42015-04-01 12:09:17 -0700795 // Check if the image decodes to the right color type
796 // before creating the benchmark
msarett95f192d2015-02-13 09:05:41 -0800797 SkBitmap bitmap;
798 if (SkImageDecoder::DecodeFile(path.c_str(), &bitmap,
scroggo60869a42015-04-01 12:09:17 -0700799 colorType, SkImageDecoder::kDecodePixels_Mode)
800 && bitmap.colorType() == colorType) {
msarett95f192d2015-02-13 09:05:41 -0800801 return new DecodingBench(path, colorType);
802 }
803 }
804 fCurrentColorType = 0;
805 fCurrentImage++;
806 }
807
msarett7f691442015-09-22 11:56:16 -0700808 // Run the BRDBenches
809 // We will benchmark multiple BRD strategies.
scroggo303fa352015-10-05 11:03:34 -0700810 static const struct {
msarett5cb48852015-11-06 08:56:32 -0800811 SkBitmapRegionDecoder::Strategy fStrategy;
msarett0459c942015-11-10 14:52:13 -0800812 const char* fName;
scroggo303fa352015-10-05 11:03:34 -0700813 } strategies[] = {
msarett0459c942015-11-10 14:52:13 -0800814 { SkBitmapRegionDecoder::kCanvas_Strategy, "BRD_canvas" },
msarett5cb48852015-11-06 08:56:32 -0800815 { SkBitmapRegionDecoder::kAndroidCodec_Strategy, "BRD_android_codec" },
msarett7f691442015-09-22 11:56:16 -0700816 };
817
818 // We intend to create benchmarks that model the use cases in
819 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
820 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
821 // the image. For that reason, we will benchmark decodes in five representative locations
822 // in the image. Additionally, this use case utilizes power of two scaling, so we will
823 // test on power of two sample sizes. The output tile is always 512x512, so, when a
824 // sampleSize is used, the size of the subset that is decoded is always
825 // (sampleSize*512)x(sampleSize*512).
826 // There are a few good reasons to only test on power of two sample sizes at this time:
827 // JPEG decodes using kOriginal_Strategy are broken for non-powers of two.
halcanary6950de62015-11-07 05:29:00 -0800828 // https://bug.skia.org/4319
msarett7f691442015-09-22 11:56:16 -0700829 // All use cases we are aware of only scale by powers of two.
830 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
831 // these tests are sufficient to provide good coverage of our scaling options.
scroggo501b7342015-11-03 07:55:11 -0800832 const uint32_t sampleSizes[] = { 1, 2, 4, 8, 16, 32, 64 };
msarett7f691442015-09-22 11:56:16 -0700833 const uint32_t minOutputSize = 512;
834 while (fCurrentBRDImage < fImages.count()) {
835 while (fCurrentBRDStrategy < (int) SK_ARRAY_COUNT(strategies)) {
scroggo303fa352015-10-05 11:03:34 -0700836 fSourceType = "image";
837 fBenchType = strategies[fCurrentBRDStrategy].fName;
scroggo860e8a62015-10-15 07:51:28 -0700838
839 const SkString& path = fImages[fCurrentBRDImage];
msarett5cb48852015-11-06 08:56:32 -0800840 const SkBitmapRegionDecoder::Strategy strategy =
scroggo860e8a62015-10-15 07:51:28 -0700841 strategies[fCurrentBRDStrategy].fStrategy;
842
msarett7f691442015-09-22 11:56:16 -0700843 while (fCurrentColorType < fColorTypes.count()) {
844 while (fCurrentBRDSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
845 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700846
847
msarett7f691442015-09-22 11:56:16 -0700848 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
849 const SkColorType colorType = fColorTypes[fCurrentColorType];
850 uint32_t sampleSize = sampleSizes[fCurrentBRDSampleSize];
851 int currentSubsetType = fCurrentSubsetType++;
852
853 int width = 0;
854 int height = 0;
855 if (!valid_brd_bench(encoded.get(), strategy, colorType, sampleSize,
856 minOutputSize, &width, &height)) {
857 break;
858 }
859
860 SkString basename = SkOSPath::Basename(path.c_str());
861 SkIRect subset;
862 const uint32_t subsetSize = sampleSize * minOutputSize;
863 switch (currentSubsetType) {
864 case kTopLeft_SubsetType:
865 basename.append("_TopLeft");
866 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
867 break;
868 case kTopRight_SubsetType:
869 basename.append("_TopRight");
870 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
871 subsetSize);
872 break;
873 case kMiddle_SubsetType:
874 basename.append("_Middle");
875 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
876 (height - subsetSize) / 2, subsetSize, subsetSize);
877 break;
878 case kBottomLeft_SubsetType:
879 basename.append("_BottomLeft");
880 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
881 subsetSize);
882 break;
883 case kBottomRight_SubsetType:
884 basename.append("_BottomRight");
885 subset = SkIRect::MakeXYWH(width - subsetSize,
886 height - subsetSize, subsetSize, subsetSize);
887 break;
888 default:
889 SkASSERT(false);
890 }
891
892 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
893 strategy, colorType, sampleSize, subset);
894 }
895 fCurrentSubsetType = 0;
896 fCurrentBRDSampleSize++;
897 }
898 fCurrentBRDSampleSize = 0;
899 fCurrentColorType++;
900 }
901 fCurrentColorType = 0;
902 fCurrentBRDStrategy++;
903 }
904 fCurrentBRDStrategy = 0;
905 fCurrentBRDImage++;
906 }
907
halcanary96fcdcc2015-08-27 07:41:13 -0700908 return nullptr;
mtkleine714e752014-07-31 12:13:48 -0700909 }
mtklein92007582014-08-01 07:46:52 -0700910
911 void fillCurrentOptions(ResultsWriter* log) const {
912 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700913 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700914 if (0 == strcmp(fSourceType, "skp")) {
915 log->configOption("clip",
916 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
917 fClip.fRight, fClip.fBottom).c_str());
mtklein4dfdbb12015-10-20 07:45:29 -0700918 SK_ALWAYSBREAK(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -0700919 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -0800920 if (fCurrentUseMPD > 0) {
921 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
922 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
923 }
mtklein92007582014-08-01 07:46:52 -0700924 }
mtklein051e56d2014-12-04 08:46:51 -0800925 if (0 == strcmp(fBenchType, "recording")) {
926 log->metric("bytes", fSKPBytes);
927 log->metric("ops", fSKPOps);
928 }
mtklein92007582014-08-01 07:46:52 -0700929 }
930
mtkleine714e752014-07-31 12:13:48 -0700931private:
msarettb23e6aa2015-06-09 13:56:10 -0700932 enum SubsetType {
933 kTopLeft_SubsetType = 0,
934 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -0700935 kMiddle_SubsetType = 2,
936 kBottomLeft_SubsetType = 3,
937 kBottomRight_SubsetType = 4,
938 kTranslate_SubsetType = 5,
939 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -0700940 kLast_SubsetType = kZoom_SubsetType,
941 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -0700942 };
943
mtkleine714e752014-07-31 12:13:48 -0700944 const BenchRegistry* fBenches;
945 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700946 SkIRect fClip;
947 SkTArray<SkScalar> fScales;
948 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -0800949 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -0800950 SkTArray<SkString> fImages;
msarett74deb982015-10-20 16:45:56 -0700951 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -0700952 SkScalar fZoomMax;
953 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -0700954
mtklein051e56d2014-12-04 08:46:51 -0800955 double fSKPBytes, fSKPOps;
956
mtkleinfd731ce2014-09-10 12:19:30 -0700957 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
958 const char* fBenchType; // How we bench it: micro, recording, playback, ...
959 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700960 int fCurrentScale;
961 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -0800962 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -0700963 int fCurrentCodec;
msarett95f192d2015-02-13 09:05:41 -0800964 int fCurrentImage;
msarett7f691442015-09-22 11:56:16 -0700965 int fCurrentBRDImage;
msarett95f192d2015-02-13 09:05:41 -0800966 int fCurrentColorType;
msarettb23e6aa2015-06-09 13:56:10 -0700967 int fCurrentSubsetType;
msarett7f691442015-09-22 11:56:16 -0700968 int fCurrentBRDStrategy;
969 int fCurrentBRDSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -0700970 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -0700971};
972
jcgregorio3b27ade2014-11-13 08:06:40 -0800973int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700974int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800975 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -0700976 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -0700977 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -0700978
krajcevski69a55602014-08-13 10:46:31 -0700979#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -0700980 GrContextOptions grContextOpts;
krajcevski12b35442014-08-13 12:06:26 -0700981 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
halcanary385fe4d2015-08-26 13:07:48 -0700982 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -0700983#endif
984
bsalomon06cddec2014-10-24 10:40:50 -0700985 if (FLAGS_veryVerbose) {
986 FLAGS_verbose = true;
987 }
988
bsalomon6eb03cc2014-08-07 14:28:50 -0700989 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700990 FLAGS_samples = 1;
991 FLAGS_gpuFrameLag = 0;
992 }
993
bsalomon6eb03cc2014-08-07 14:28:50 -0700994 if (!FLAGS_writePath.isEmpty()) {
995 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
996 if (!sk_mkdir(FLAGS_writePath[0])) {
997 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -0700998 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -0700999 }
1000 }
1001
halcanary385fe4d2015-08-26 13:07:48 -07001002 SkAutoTDelete<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001003 if (!FLAGS_outResultsFile.isEmpty()) {
halcanary385fe4d2015-08-26 13:07:48 -07001004 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein60317d0f2014-07-14 11:30:37 -07001005 }
mtklein1915b622014-08-20 11:45:00 -07001006
1007 if (1 == FLAGS_properties.count() % 2) {
1008 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1009 return 1;
1010 }
1011 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1012 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1013 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001014
1015 if (1 == FLAGS_key.count() % 2) {
1016 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1017 return 1;
1018 }
1019 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001020 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001021 }
mtklein60317d0f2014-07-14 11:30:37 -07001022
mtkleinf3723212014-06-25 14:08:00 -07001023 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001024 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001025
cdaltone1b89582015-06-25 19:17:08 -07001026 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001027
bsalomon6eb03cc2014-08-07 14:28:50 -07001028 if (kAutoTuneLoops != FLAGS_loops) {
1029 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001030 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001031 SkDebugf("! -> high variance, ? -> moderate variance\n");
1032 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001033 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001034 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001035 } else {
mtkleind75c4662015-04-30 07:11:22 -07001036 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001037 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001038 }
1039
bsalomonc2553372014-07-22 13:09:05 -07001040 SkTDArray<Config> configs;
1041 create_configs(&configs);
1042
mtkleine070c2b2014-10-14 08:40:43 -07001043 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001044 BenchmarkStream benchStream;
1045 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001046 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001047 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001048 continue;
1049 }
1050
egdaniel3bf92062015-06-26 08:12:46 -07001051 if (!configs.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -07001052 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001053 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001054 }
egdaniel3bf92062015-06-26 08:12:46 -07001055 for (int i = 0; i < configs.count(); ++i) {
1056 Target* target = is_enabled(b, configs[i]);
1057 if (!target) {
1058 continue;
1059 }
mtkleinf3723212014-06-25 14:08:00 -07001060
halcanary96fcdcc2015-08-27 07:41:13 -07001061 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001062 SkCanvas* canvas = target->getCanvas();
1063 const char* config = target->config.name;
1064
1065 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001066 bench->perCanvasPreDraw(canvas);
1067
cdaltone1b89582015-06-25 19:17:08 -07001068 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001069 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001070 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1071 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001072
mtkleinbbba1682015-10-28 11:36:30 -07001073 if (FLAGS_ms) {
1074 samples.reset();
1075 auto stop = now_ms() + FLAGS_ms;
1076 do {
1077 samples.push_back(time(loops, bench, target) / loops);
1078 } while (now_ms() < stop);
1079 } else {
cdaltone1b89582015-06-25 19:17:08 -07001080 samples.reset(FLAGS_samples);
1081 for (int s = 0; s < FLAGS_samples; s++) {
egdaniel3bf92062015-06-26 08:12:46 -07001082 samples[s] = time(loops, bench, target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001083 }
cdaltone1b89582015-06-25 19:17:08 -07001084 }
mtkleinf3723212014-06-25 14:08:00 -07001085
joshualitte45c81c2015-12-02 09:05:37 -08001086#if SK_SUPPORT_GPU
1087 SkTArray<SkString> keys;
1088 SkTArray<double> values;
1089 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1090 if (gpuStatsDump) {
1091 // TODO cache stats
1092 bench->getGpuStats(canvas, &keys, &values);
1093 }
1094#endif
1095
robertphillips5b693772014-11-21 06:19:36 -08001096 bench->perCanvasPostDraw(canvas);
1097
egdaniel3bf92062015-06-26 08:12:46 -07001098 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001099 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001100 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001101 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001102 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001103 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001104 }
1105
1106 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001107 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001108 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001109 continue;
1110 }
1111
cdaltone1b89582015-06-25 19:17:08 -07001112 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001113 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001114 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001115 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001116 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001117 log->metric("min_ms", stats.min);
joshualitte45c81c2015-12-02 09:05:37 -08001118#if SK_SUPPORT_GPU
1119 if (gpuStatsDump) {
1120 // dump to json, only SKPBench currently returns valid keys / values
1121 SkASSERT(keys.count() == values.count());
1122 for (int i = 0; i < keys.count(); i++) {
1123 log->metric(keys[i].c_str(), values[i]);
1124 }
1125 }
1126#endif
1127
mtkleine070c2b2014-10-14 08:40:43 -07001128 if (runs++ % FLAGS_flushEvery == 0) {
1129 log->flush();
1130 }
mtklein60317d0f2014-07-14 11:30:37 -07001131
bsalomon6eb03cc2014-08-07 14:28:50 -07001132 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001133 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001134 config = ""; // Only print the config if we run the same bench on more than one.
1135 }
mtkleind75c4662015-04-30 07:11:22 -07001136 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1137 , sk_tools::getCurrResidentSetSizeMB()
1138 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001139 , bench->getUniqueName()
1140 , config);
mtkleinf3723212014-06-25 14:08:00 -07001141 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001142 const char* mark = " ";
1143 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1144 if (stddev_percent > 5) mark = "?";
1145 if (stddev_percent > 10) mark = "!";
1146
1147 SkDebugf("%10.2f %s\t%s\t%s\n",
1148 stats.median*1e3, mark, bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001149 } else {
1150 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001151 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1152 , sk_tools::getCurrResidentSetSizeMB()
1153 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001154 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001155 , HUMANIZE(stats.min)
1156 , HUMANIZE(stats.median)
1157 , HUMANIZE(stats.mean)
1158 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001159 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001160 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001161 , config
mtklein96289052014-09-10 12:05:59 -07001162 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001163 );
1164 }
joshualitte45c81c2015-12-02 09:05:37 -08001165
bsalomonb12ea412015-02-02 21:19:50 -08001166#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001167 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001168 GrContext* context = gGrFactory->get(configs[i].ctxType,
rmistry0f515bd2015-12-22 10:22:26 -08001169 kNone_GrGLStandard, configs[i].ctxOptions);
kkinnunen5219fd92015-12-10 06:28:13 -08001170 context->printCacheStats();
1171 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001172 }
1173#endif
joshualitte45c81c2015-12-02 09:05:37 -08001174
cdalton2c56ba52015-06-26 13:32:53 -07001175 if (FLAGS_verbose) {
1176 SkDebugf("Samples: ");
1177 for (int i = 0; i < samples.count(); i++) {
1178 SkDebugf("%s ", HUMANIZE(samples[i]));
1179 }
1180 SkDebugf("%s\n", bench->getUniqueName());
1181 }
egdaniel3bf92062015-06-26 08:12:46 -07001182 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001183 }
mtkleinf3723212014-06-25 14:08:00 -07001184 }
1185
mtkleine1091452014-12-04 10:47:02 -08001186 log->bench("memory_usage", 0,0);
1187 log->config("meta");
1188 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1189
joshualitte0b19d42015-03-26 10:41:02 -07001190#if SK_SUPPORT_GPU
1191 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1192 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001193 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001194#endif
1195
mtkleinf3723212014-06-25 14:08:00 -07001196 return 0;
1197}
1198
jcgregorio3b27ade2014-11-13 08:06:40 -08001199#if !defined SK_BUILD_FOR_IOS
1200int main(int argc, char** argv) {
1201 SkCommandLineFlags::Parse(argc, argv);
1202 return nanobench_main();
1203}
1204#endif