blob: f1841e5b6c24099ef18c0e0538fd221be4b94515 [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"
msarettb23e6aa2015-06-09 13:56:10 -070024#include "SubsetSingleBench.h"
25#include "SubsetTranslateBench.h"
26#include "SubsetZoomBench.h"
mtkleinf3723212014-06-25 14:08:00 -070027#include "Stats.h"
mtkleinf3723212014-06-25 14:08:00 -070028
msarett5cb48852015-11-06 08:56:32 -080029#include "SkBitmapRegionDecoder.h"
mtklein6838d852014-10-29 14:15:10 -070030#include "SkBBoxHierarchy.h"
mtkleinf3723212014-06-25 14:08:00 -070031#include "SkCanvas.h"
scroggo60869a42015-04-01 12:09:17 -070032#include "SkCodec.h"
caryclark17f0b6d2014-07-22 10:15:34 -070033#include "SkCommonFlags.h"
msarett95f192d2015-02-13 09:05:41 -080034#include "SkData.h"
mtkleinf3723212014-06-25 14:08:00 -070035#include "SkForceLinking.h"
36#include "SkGraphics.h"
mtklein20840502014-08-21 15:51:22 -070037#include "SkOSFile.h"
38#include "SkPictureRecorder.h"
mtklein051e56d2014-12-04 08:46:51 -080039#include "SkPictureUtils.h"
mtkleinf3723212014-06-25 14:08:00 -070040#include "SkString.h"
41#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080042#include "SkTaskGroup.h"
mtkleinf3723212014-06-25 14:08:00 -070043
bungeman60e0fee2015-08-26 05:15:46 -070044#include <stdlib.h>
45
tomhudsond968a6f2015-03-26 11:28:06 -070046#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
47 #include "nanobenchAndroid.h"
48#endif
49
mtkleinbb6a0282014-07-01 08:43:42 -070050#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070051 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070052 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070053 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070054 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070055#endif
56
bsalomon682c2692015-05-22 14:01:46 -070057 struct GrContextOptions;
58
mtkleinf3723212014-06-25 14:08:00 -070059__SK_FORCE_IMAGE_DECODER_LINKING;
60
reed53249782014-10-10 09:09:52 -070061static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070062
mtkleinb5110422014-08-07 15:20:02 -070063static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070064#ifdef SK_DEBUG
65 1;
mtkleina189ccd2014-07-14 12:28:47 -070066#else
bsalomon6eb03cc2014-08-07 14:28:50 -070067 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070068#endif
69
bsalomon6eb03cc2014-08-07 14:28:50 -070070static SkString loops_help_txt() {
71 SkString help;
72 help.printf("Number of times to run each bench. Set this to %d to auto-"
73 "tune for each bench. Timings are only reported when auto-tuning.",
74 kAutoTuneLoops);
75 return help;
76}
77
cdaltone1b89582015-06-25 19:17:08 -070078static SkString to_string(int n) {
79 SkString str;
80 str.appendS32(n);
81 return str;
82}
83
bsalomon6eb03cc2014-08-07 14:28:50 -070084DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
85
mtkleinf3723212014-06-25 14:08:00 -070086DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -070087DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -070088DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
89DEFINE_double(overheadGoal, 0.0001,
90 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070091DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -070092DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070093DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
94 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070095
mtklein60317d0f2014-07-14 11:30:37 -070096DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -070097DEFINE_int32(maxCalibrationAttempts, 3,
98 "Try up to this many times to guess loops for a bench, or skip the bench.");
99DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700100DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
101DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700102DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
103 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700104DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
robertphillips5b693772014-11-21 06:19:36 -0800105DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700106DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700107DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800108DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800109DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800110DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
mtklein92007582014-08-01 07:46:52 -0700111
mtkleinbbba1682015-10-28 11:36:30 -0700112static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
113
mtkleinf3723212014-06-25 14:08:00 -0700114static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700115 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800116 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700117}
mtklein55b0ffc2014-07-17 08:38:23 -0700118#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700119
tomhudsond968a6f2015-03-26 11:28:06 -0700120bool Target::init(SkImageInfo info, Benchmark* bench) {
121 if (Benchmark::kRaster_Backend == config.backend) {
122 this->surface.reset(SkSurface::NewRaster(info));
123 if (!this->surface.get()) {
124 return false;
125 }
126 }
127 return true;
128}
129bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700130 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700131 if (!canvas) {
132 return false;
133 }
134 bmp->setInfo(canvas->imageInfo());
135 if (!canvas->readPixels(bmp, 0, 0)) {
136 SkDebugf("Can't read canvas pixels.\n");
137 return false;
138 }
139 return true;
140}
141
142#if SK_SUPPORT_GPU
143struct GPUTarget : public Target {
halcanary96fcdcc2015-08-27 07:41:13 -0700144 explicit GPUTarget(const Config& c) : Target(c), gl(nullptr) { }
tomhudsond968a6f2015-03-26 11:28:06 -0700145 SkGLContext* gl;
146
147 void setup() override {
148 this->gl->makeCurrent();
149 // Make sure we're done with whatever came before.
150 SK_GL(*this->gl, Finish());
151 }
152 void endTiming() override {
153 if (this->gl) {
154 SK_GL(*this->gl, Flush());
155 this->gl->swapBuffers();
156 }
157 }
158 void fence() override {
159 SK_GL(*this->gl, Finish());
160 }
mtkleind75c4662015-04-30 07:11:22 -0700161
cdaltond416a5b2015-06-23 13:23:44 -0700162 bool needsFrameTiming(int* maxFrameLag) const override {
163 if (!this->gl->getMaxGpuFrameLag(maxFrameLag)) {
164 // Frame lag is unknown.
165 *maxFrameLag = FLAGS_gpuFrameLag;
166 }
167 return true;
168 }
tomhudsond968a6f2015-03-26 11:28:06 -0700169 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700170 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
171 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700172 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
kkinnunen5219fd92015-12-10 06:28:13 -0800173 this->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(this->config.ctxType,
174 kNone_GrGLStandard,
175 this->config.ctxOptions),
tomhudsond968a6f2015-03-26 11:28:06 -0700176 SkSurface::kNo_Budgeted, info,
177 this->config.samples, &props));
kkinnunen5219fd92015-12-10 06:28:13 -0800178 this->gl = gGrFactory->getContextInfo(this->config.ctxType, kNone_GrGLStandard,
179 this->config.ctxOptions)->fGLContext;
tomhudsond968a6f2015-03-26 11:28:06 -0700180 if (!this->surface.get()) {
181 return false;
182 }
cdaltond416a5b2015-06-23 13:23:44 -0700183 if (!this->gl->fenceSyncSupport()) {
184 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
185 "Timings might not be accurate.\n", this->config.name);
186 }
tomhudsond968a6f2015-03-26 11:28:06 -0700187 return true;
188 }
189 void fillOptions(ResultsWriter* log) override {
190 const GrGLubyte* version;
191 SK_GL_RET(*this->gl, version, GetString(GR_GL_VERSION));
192 log->configOption("GL_VERSION", (const char*)(version));
193
194 SK_GL_RET(*this->gl, version, GetString(GR_GL_RENDERER));
195 log->configOption("GL_RENDERER", (const char*) version);
196
197 SK_GL_RET(*this->gl, version, GetString(GR_GL_VENDOR));
198 log->configOption("GL_VENDOR", (const char*) version);
199
200 SK_GL_RET(*this->gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
201 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
202 }
203};
mtkleind75c4662015-04-30 07:11:22 -0700204
tomhudsond968a6f2015-03-26 11:28:06 -0700205#endif
206
tomhudson75a0ebb2015-03-27 12:11:44 -0700207static double time(int loops, Benchmark* bench, Target* target) {
208 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700209 if (canvas) {
210 canvas->clear(SK_ColorWHITE);
211 }
joshualitt8a6697a2015-09-30 12:11:07 -0700212 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700213 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700214 canvas = target->beginTiming(canvas);
215 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700216 if (canvas) {
217 canvas->flush();
218 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700219 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700220 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700221 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700222 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700223}
224
mtkleinf3723212014-06-25 14:08:00 -0700225static double estimate_timer_overhead() {
226 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700227 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700228 double start = now_ms();
229 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700230 }
231 return overhead / FLAGS_overheadLoops;
232}
233
reed53249782014-10-10 09:09:52 -0700234static int detect_forever_loops(int loops) {
235 // look for a magic run-forever value
236 if (loops < 0) {
237 loops = SK_MaxS32;
238 }
239 return loops;
240}
241
mtklein55b0ffc2014-07-17 08:38:23 -0700242static int clamp_loops(int loops) {
243 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800244 SkDebugf("ERROR: clamping loops from %d to 1. "
245 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700246 return 1;
247 }
248 if (loops > FLAGS_maxLoops) {
249 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
250 return FLAGS_maxLoops;
251 }
252 return loops;
253}
254
tomhudsond968a6f2015-03-26 11:28:06 -0700255static bool write_canvas_png(Target* target, const SkString& filename) {
256
bsalomon6eb03cc2014-08-07 14:28:50 -0700257 if (filename.isEmpty()) {
258 return false;
259 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700260 if (target->getCanvas() &&
261 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700262 return false;
263 }
tomhudsond968a6f2015-03-26 11:28:06 -0700264
bsalomon6eb03cc2014-08-07 14:28:50 -0700265 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700266
267 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700268 return false;
269 }
tomhudsond968a6f2015-03-26 11:28:06 -0700270
bsalomon6eb03cc2014-08-07 14:28:50 -0700271 SkString dir = SkOSPath::Dirname(filename.c_str());
272 if (!sk_mkdir(dir.c_str())) {
273 SkDebugf("Can't make dir %s.\n", dir.c_str());
274 return false;
275 }
276 SkFILEWStream stream(filename.c_str());
277 if (!stream.isValid()) {
278 SkDebugf("Can't write %s.\n", filename.c_str());
279 return false;
280 }
281 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
282 SkDebugf("Can't encode a PNG.\n");
283 return false;
284 }
285 return true;
286}
287
288static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700289static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700290 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700291 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700292 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700293 int loops = bench->calculateLoops(FLAGS_loops);
294 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700295 while (bench_plus_overhead < overhead) {
296 if (round++ == FLAGS_maxCalibrationAttempts) {
297 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700298 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700299 return kFailedLoops;
300 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700301 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700302 }
mtklein2069e222014-08-04 13:57:39 -0700303 }
mtkleinf3723212014-06-25 14:08:00 -0700304
mtkleinbb6a0282014-07-01 08:43:42 -0700305 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700306 // We'll pick N to make timer overhead negligible:
307 //
mtkleinbb6a0282014-07-01 08:43:42 -0700308 // overhead
309 // ------------------------- < FLAGS_overheadGoal
310 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700311 //
mtkleinbb6a0282014-07-01 08:43:42 -0700312 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700313 //
314 // Doing some math, we get:
315 //
mtkleinbb6a0282014-07-01 08:43:42 -0700316 // (overhead / FLAGS_overheadGoal) - overhead
317 // ------------------------------------------ < N
318 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700319 //
320 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700321 if (kAutoTuneLoops == loops) {
322 const double numer = overhead / FLAGS_overheadGoal - overhead;
323 const double denom = bench_plus_overhead - overhead;
324 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700325 loops = clamp_loops(loops);
326 } else {
327 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700328 }
mtkleinbb6a0282014-07-01 08:43:42 -0700329
mtkleinbb6a0282014-07-01 08:43:42 -0700330 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700331}
332
cdaltone1b89582015-06-25 19:17:08 -0700333static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700334 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700335 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700336 if (kAutoTuneLoops == loops) {
337 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700338 double elapsed = 0;
339 do {
mtklein527930f2014-11-06 08:04:34 -0800340 if (1<<30 == loops) {
341 // We're about to wrap. Something's wrong with the bench.
342 loops = 0;
343 break;
344 }
mtkleina189ccd2014-07-14 12:28:47 -0700345 loops *= 2;
346 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700347 // _this_ round, not still timing last round.
348 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700349 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700350 }
351 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700352
mtkleina189ccd2014-07-14 12:28:47 -0700353 // We've overshot at least a little. Scale back linearly.
354 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700355 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700356
tomhudsond968a6f2015-03-26 11:28:06 -0700357 // Make sure we're not still timing our calibration.
358 target->fence();
reed53249782014-10-10 09:09:52 -0700359 } else {
360 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700361 }
mtkleinbb6a0282014-07-01 08:43:42 -0700362
363 // Pretty much the same deal as the calibration: do some warmup to make
364 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700365 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700366 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700367 }
mtkleinbb6a0282014-07-01 08:43:42 -0700368
mtkleinbb6a0282014-07-01 08:43:42 -0700369 return loops;
370}
mtkleinbb6a0282014-07-01 08:43:42 -0700371
372static SkString to_lower(const char* str) {
373 SkString lower(str);
374 for (size_t i = 0; i < lower.size(); i++) {
375 lower[i] = tolower(lower[i]);
376 }
377 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700378}
379
bsalomonc2553372014-07-22 13:09:05 -0700380static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700381 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700382 if (to_lower(FLAGS_config[i]).equals(name)) {
383 return true;
mtkleinf3723212014-06-25 14:08:00 -0700384 }
385 }
bsalomonc2553372014-07-22 13:09:05 -0700386 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700387}
388
bsalomonc2553372014-07-22 13:09:05 -0700389#if SK_SUPPORT_GPU
390static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
kkinnunen5219fd92015-12-10 06:28:13 -0800391 GrContextFactory::GLContextOptions ctxOptions,
bsalomonc2553372014-07-22 13:09:05 -0700392 int sampleCnt) {
393 if (!is_cpu_config_allowed(name)) {
394 return false;
395 }
kkinnunen5219fd92015-12-10 06:28:13 -0800396 if (const GrContext* ctx = gGrFactory->get(ctxType, kNone_GrGLStandard, ctxOptions)) {
bsalomon76228632015-05-29 08:02:10 -0700397 return sampleCnt <= ctx->caps()->maxSampleCount();
bsalomonc2553372014-07-22 13:09:05 -0700398 }
399 return false;
400}
401#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700402
bsalomonc2553372014-07-22 13:09:05 -0700403#if SK_SUPPORT_GPU
404#define kBogusGLContextType GrContextFactory::kNative_GLContextType
kkinnunen5219fd92015-12-10 06:28:13 -0800405#define kBogusGLContextOptions GrContextFactory::kNone_GLContextOptions
bsalomonc2553372014-07-22 13:09:05 -0700406#else
407#define kBogusGLContextType 0
kkinnunen5219fd92015-12-10 06:28:13 -0800408#define kBogusGLContextOptions 0
mtkleine714e752014-07-31 12:13:48 -0700409#endif
bsalomonc2553372014-07-22 13:09:05 -0700410
411// Append all configs that are enabled and supported.
412static void create_configs(SkTDArray<Config>* configs) {
kkinnunen5219fd92015-12-10 06:28:13 -0800413 #define CPU_CONFIG(name, backend, color, alpha) \
414 if (is_cpu_config_allowed(#name)) { \
415 Config config = { #name, Benchmark::backend, color, alpha, 0, \
416 kBogusGLContextType, kBogusGLContextOptions, \
417 false }; \
418 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700419 }
mtkleine714e752014-07-31 12:13:48 -0700420
mtklein40b32be2014-07-09 08:46:49 -0700421 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700422 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
423 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
424 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700425 }
mtkleinbb6a0282014-07-01 08:43:42 -0700426
427#if SK_SUPPORT_GPU
kkinnunen5219fd92015-12-10 06:28:13 -0800428 #define GPU_CONFIG(name, ctxType, ctxOptions, samples, useDFText) \
429 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, \
430 GrContextFactory::ctxOptions, samples)) { \
bsalomonc2553372014-07-22 13:09:05 -0700431 Config config = { \
432 #name, \
433 Benchmark::kGPU_Backend, \
434 kN32_SkColorType, \
435 kPremul_SkAlphaType, \
436 samples, \
jvanverth4736e142014-11-07 07:12:46 -0800437 GrContextFactory::ctxType, \
kkinnunen5219fd92015-12-10 06:28:13 -0800438 GrContextFactory::ctxOptions, \
jvanverth4736e142014-11-07 07:12:46 -0800439 useDFText }; \
bsalomonc2553372014-07-22 13:09:05 -0700440 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700441 }
mtkleine714e752014-07-31 12:13:48 -0700442
mtklein40b32be2014-07-09 08:46:49 -0700443 if (FLAGS_gpu) {
kkinnunen5219fd92015-12-10 06:28:13 -0800444 GPU_CONFIG(gpu, kNative_GLContextType, kNone_GLContextOptions, 0, false)
445 GPU_CONFIG(msaa4, kNative_GLContextType, kNone_GLContextOptions, 4, false)
446 GPU_CONFIG(msaa16, kNative_GLContextType, kNone_GLContextOptions, 16, false)
447 GPU_CONFIG(nvprmsaa4, kNative_GLContextType, kEnableNVPR_GLContextOptions, 4, false)
448 GPU_CONFIG(nvprmsaa16, kNative_GLContextType, kEnableNVPR_GLContextOptions, 16, false)
449 GPU_CONFIG(gpudft, kNative_GLContextType, kNone_GLContextOptions, 0, true)
450 GPU_CONFIG(debug, kDebug_GLContextType, kNone_GLContextOptions, 0, false)
451 GPU_CONFIG(nullgpu, kNull_GLContextType, kNone_GLContextOptions, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700452#ifdef SK_ANGLE
kkinnunen5219fd92015-12-10 06:28:13 -0800453 GPU_CONFIG(angle, kANGLE_GLContextType, kNone_GLContextOptions, 0, false)
454 GPU_CONFIG(angle-gl, kANGLE_GL_GLContextType, kNone_GLContextOptions, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700455#endif
hendrikw885bf092015-08-27 10:38:39 -0700456#ifdef SK_COMMAND_BUFFER
kkinnunen5219fd92015-12-10 06:28:13 -0800457 GPU_CONFIG(commandbuffer, kCommandBuffer_GLContextType, kNone_GLContextOptions, 0, false)
hendrikw885bf092015-08-27 10:38:39 -0700458#endif
cdaltond416a5b2015-06-23 13:23:44 -0700459#if SK_MESA
kkinnunen5219fd92015-12-10 06:28:13 -0800460 GPU_CONFIG(mesa, kMESA_GLContextType, kNone_GLContextOptions, 0, false)
cdaltond416a5b2015-06-23 13:23:44 -0700461#endif
mtklein40b32be2014-07-09 08:46:49 -0700462 }
mtkleinbb6a0282014-07-01 08:43:42 -0700463#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700464
465#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
466 if (is_cpu_config_allowed("hwui")) {
467 Config config = { "hwui", Benchmark::kHWUI_Backend, kRGBA_8888_SkColorType,
kkinnunen5219fd92015-12-10 06:28:13 -0800468 kPremul_SkAlphaType, 0, kBogusGLContextType, kBogusGLContextOptions,
469 false };
tomhudsond968a6f2015-03-26 11:28:06 -0700470 configs->push(config);
471 }
472#endif
mtkleinf3723212014-06-25 14:08:00 -0700473}
474
halcanary96fcdcc2015-08-27 07:41:13 -0700475// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700476static Target* is_enabled(Benchmark* bench, const Config& config) {
477 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700478 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700479 }
480
reede5ea5002014-09-03 11:54:58 -0700481 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
482 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700483
halcanary96fcdcc2015-08-27 07:41:13 -0700484 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700485
tomhudsond968a6f2015-03-26 11:28:06 -0700486 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700487#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700488 case Benchmark::kGPU_Backend:
489 target = new GPUTarget(config);
490 break;
bsalomonc2553372014-07-22 13:09:05 -0700491#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700492#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
493 case Benchmark::kHWUI_Backend:
494 target = new HWUITarget(config, bench);
495 break;
496#endif
497 default:
498 target = new Target(config);
499 break;
500 }
bsalomonc2553372014-07-22 13:09:05 -0700501
tomhudsond968a6f2015-03-26 11:28:06 -0700502 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700503 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700504 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700505 }
506 return target;
507}
508
msarettb23e6aa2015-06-09 13:56:10 -0700509/*
scroggoab12c272015-10-08 12:01:39 -0700510 * We only run our subset benches on files that are supported by BitmapRegionDecoder:
msarett0459c942015-11-10 14:52:13 -0800511 * i.e. PNG, JPEG, and WEBP. We do *not* test WEBP, since we do not have a scanline
512 * decoder for WEBP, which is necessary for running the subset bench. (Another bench
513 * must be used to test WEBP, which decodes subsets natively.)
scroggoab12c272015-10-08 12:01:39 -0700514 */
msarett0459c942015-11-10 14:52:13 -0800515static bool run_subset_bench(const SkString& path) {
scroggoab12c272015-10-08 12:01:39 -0700516 static const char* const exts[] = {
msarett0459c942015-11-10 14:52:13 -0800517 "jpg", "jpeg", "png",
518 "JPG", "JPEG", "PNG",
scroggoab12c272015-10-08 12:01:39 -0700519 };
520
msarett0459c942015-11-10 14:52:13 -0800521 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
522 if (path.endsWith(exts[i])) {
523 return true;
scroggoab12c272015-10-08 12:01:39 -0700524 }
525 }
526
scroggo2da1a852015-10-13 13:33:33 -0700527 return false;
scroggoab12c272015-10-08 12:01:39 -0700528}
529
530/*
msarettb23e6aa2015-06-09 13:56:10 -0700531 * Returns true if set up for a subset decode succeeds, false otherwise
532 * If the set-up succeeds, the width and height parameters will be set
533 */
msarett0459c942015-11-10 14:52:13 -0800534static bool valid_subset_bench(const SkString& path, SkColorType colorType,
msarettb23e6aa2015-06-09 13:56:10 -0700535 int* width, int* height) {
536 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
537 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded));
538
msarett0459c942015-11-10 14:52:13 -0800539 // Check that we can create a codec.
540 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
541 if (nullptr == codec) {
542 SkDebugf("Could not create codec for %s. Skipping bench.\n", path.c_str());
543 return false;
msarettb23e6aa2015-06-09 13:56:10 -0700544 }
msarettab80e352015-06-17 10:28:22 -0700545
msarett0459c942015-11-10 14:52:13 -0800546 // These will be initialized by SkCodec if the color type is kIndex8 and
547 // unused otherwise.
548 SkPMColor colors[256];
549 int colorCount;
550 const SkImageInfo info = codec->getInfo().makeColorType(colorType);
551 if (codec->startScanlineDecode(info, nullptr, colors, &colorCount) != SkCodec::kSuccess)
552 {
553 SkDebugf("Could not create scanline decoder for %s with color type %s. "
554 "Skipping bench.\n", path.c_str(), color_type_to_str(colorType));
555 return false;
556 }
557 *width = info.width();
558 *height = info.height();
559
msarettab80e352015-06-17 10:28:22 -0700560 // Check if the image is large enough for a meaningful subset benchmark.
561 if (*width <= 512 && *height <= 512) {
562 // This should not print a message since it is not an error.
563 return false;
564 }
565
msarettb23e6aa2015-06-09 13:56:10 -0700566 return true;
567}
jcgregoriobf5e5232014-07-17 13:14:16 -0700568
msarett5cb48852015-11-06 08:56:32 -0800569static bool valid_brd_bench(SkData* encoded, SkBitmapRegionDecoder::Strategy strategy,
msarett7f691442015-09-22 11:56:16 -0700570 SkColorType colorType, uint32_t sampleSize, uint32_t minOutputSize, int* width,
571 int* height) {
msarett5cb48852015-11-06 08:56:32 -0800572 SkAutoTDelete<SkBitmapRegionDecoder> brd(
573 SkBitmapRegionDecoder::Create(encoded, strategy));
msarett7f691442015-09-22 11:56:16 -0700574 if (nullptr == brd.get()) {
575 // This is indicates that subset decoding is not supported for a particular image format.
576 return false;
577 }
578
msarett35e5d1b2015-10-27 12:50:25 -0700579 SkBitmap bitmap;
580 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, brd->width(), brd->height()),
581 1, colorType, false)) {
582 return false;
583 }
msarett7f691442015-09-22 11:56:16 -0700584
585 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
586 (uint32_t) brd->height()) {
587 // This indicates that the image is not large enough to decode a
588 // minOutputSize x minOutputSize subset at the given sampleSize.
589 return false;
590 }
591
592 // Set the image width and height. The calling code will use this to choose subsets to decode.
593 *width = brd->width();
594 *height = brd->height();
595 return true;
596}
597
egdaniel3bf92062015-06-26 08:12:46 -0700598static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700599 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700600#if SK_SUPPORT_GPU
601 if (FLAGS_abandonGpuContext) {
602 gGrFactory->abandonContexts();
603 }
604 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
605 gGrFactory->destroyContexts();
606 }
607#endif
608}
609
mtkleine714e752014-07-31 12:13:48 -0700610class BenchmarkStream {
611public:
mtklein92007582014-08-01 07:46:52 -0700612 BenchmarkStream() : fBenches(BenchRegistry::Head())
613 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700614 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700615 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800616 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800617 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700618 , fCurrentCodec(0)
msarett95f192d2015-02-13 09:05:41 -0800619 , fCurrentImage(0)
620 , fCurrentSubsetImage(0)
msarett7f691442015-09-22 11:56:16 -0700621 , fCurrentBRDImage(0)
msarett95f192d2015-02-13 09:05:41 -0800622 , fCurrentColorType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700623 , fCurrentSubsetType(0)
msarett7f691442015-09-22 11:56:16 -0700624 , fCurrentBRDStrategy(0)
625 , fCurrentBRDSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700626 , fCurrentAnimSKP(0) {
mtklein92007582014-08-01 07:46:52 -0700627 for (int i = 0; i < FLAGS_skps.count(); i++) {
628 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
629 fSKPs.push_back() = FLAGS_skps[i];
630 } else {
631 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
632 SkString path;
633 while (it.next(&path)) {
634 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
635 }
636 }
637 }
mtkleine714e752014-07-31 12:13:48 -0700638
mtklein92007582014-08-01 07:46:52 -0700639 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
640 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
641 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
642 exit(1);
643 }
644
645 for (int i = 0; i < FLAGS_scales.count(); i++) {
646 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
647 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
648 exit(1);
649 }
650 }
robertphillips5b693772014-11-21 06:19:36 -0800651
cdalton63a82852015-06-29 14:06:10 -0700652 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
653 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700654 exit(1);
655 }
656
robertphillips5b693772014-11-21 06:19:36 -0800657 if (FLAGS_mpd) {
658 fUseMPDs.push_back() = true;
659 }
mtkleinc751ecb2015-06-15 08:56:38 -0700660 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700661
msarett95f192d2015-02-13 09:05:41 -0800662 // Prepare the images for decoding
663 for (int i = 0; i < FLAGS_images.count(); i++) {
664 const char* flag = FLAGS_images[i];
665 if (sk_isdir(flag)) {
666 // If the value passed in is a directory, add all the images
667 SkOSFile::Iter it(flag);
668 SkString file;
669 while (it.next(&file)) {
670 fImages.push_back() = SkOSPath::Join(flag, file.c_str());
671 }
672 } else if (sk_exists(flag)) {
673 // Also add the value if it is a single image
674 fImages.push_back() = flag;
675 }
676 }
mtklein95553d92015-03-12 08:24:21 -0700677
msarett95f192d2015-02-13 09:05:41 -0800678 // Choose the candidate color types for image decoding
679 const SkColorType colorTypes[] =
msarettb23e6aa2015-06-09 13:56:10 -0700680 { kN32_SkColorType,
681 kRGB_565_SkColorType,
682 kAlpha_8_SkColorType,
683 kIndex_8_SkColorType,
684 kGray_8_SkColorType };
msarett74deb982015-10-20 16:45:56 -0700685 fColorTypes.reset(colorTypes, SK_ARRAY_COUNT(colorTypes));
mtklein92007582014-08-01 07:46:52 -0700686 }
687
mtkleinfd731ce2014-09-10 12:19:30 -0700688 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
689 // Not strictly necessary, as it will be checked again later,
690 // but helps to avoid a lot of pointless work if we're going to skip it.
691 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
692 return false;
693 }
694
scroggoa1193e42015-01-21 12:09:53 -0800695 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -0700696 if (stream.get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700697 SkDebugf("Could not read %s.\n", path);
698 return false;
699 }
700
mtklein57f27bd2015-02-09 11:58:41 -0800701 pic->reset(SkPicture::CreateFromStream(stream.get()));
halcanary96fcdcc2015-08-27 07:41:13 -0700702 if (pic->get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700703 SkDebugf("Could not read %s as an SkPicture.\n", path);
704 return false;
705 }
706 return true;
707 }
708
mtklein92007582014-08-01 07:46:52 -0700709 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700710 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700711 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700712 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700713 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700714 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700715 return bench;
716 }
mtklein92007582014-08-01 07:46:52 -0700717
mtkleine714e752014-07-31 12:13:48 -0700718 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -0700719 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700720 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800721 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700722 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700723 fBenchType = "micro";
halcanary385fe4d2015-08-26 13:07:48 -0700724 return new GMBench(gm.detach());
mtkleine714e752014-07-31 12:13:48 -0700725 }
726 }
mtklein92007582014-08-01 07:46:52 -0700727
mtkleinfd731ce2014-09-10 12:19:30 -0700728 // First add all .skps as RecordingBenches.
729 while (fCurrentRecording < fSKPs.count()) {
730 const SkString& path = fSKPs[fCurrentRecording++];
731 SkAutoTUnref<SkPicture> pic;
732 if (!ReadPicture(path.c_str(), &pic)) {
733 continue;
734 }
735 SkString name = SkOSPath::Basename(path.c_str());
736 fSourceType = "skp";
737 fBenchType = "recording";
bsalomon0aa5cea2014-12-15 09:13:35 -0800738 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic));
mtklein051e56d2014-12-04 08:46:51 -0800739 fSKPOps = pic->approximateOpCount();
halcanary385fe4d2015-08-26 13:07:48 -0700740 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700741 }
742
743 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700744 while (fCurrentScale < fScales.count()) {
745 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800746 const SkString& path = fSKPs[fCurrentSKP];
mtkleinfd731ce2014-09-10 12:19:30 -0700747 SkAutoTUnref<SkPicture> pic;
748 if (!ReadPicture(path.c_str(), &pic)) {
robertphillips5b693772014-11-21 06:19:36 -0800749 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700750 continue;
751 }
robertphillips5b693772014-11-21 06:19:36 -0800752
753 while (fCurrentUseMPD < fUseMPDs.count()) {
754 if (FLAGS_bbh) {
755 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
756 SkRTreeFactory factory;
757 SkPictureRecorder recorder;
758 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
759 pic->playback(recorder.beginRecording(pic->cullRect().width(),
760 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800761 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800762 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
robertphillips5b693772014-11-21 06:19:36 -0800763 pic.reset(recorder.endRecording());
764 }
765 SkString name = SkOSPath::Basename(path.c_str());
766 fSourceType = "skp";
767 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700768 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
769 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700770 }
robertphillips5b693772014-11-21 06:19:36 -0800771 fCurrentUseMPD = 0;
772 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700773 }
774 fCurrentSKP = 0;
775 fCurrentScale++;
776 }
777
joshualitt261c3ad2015-04-27 09:16:57 -0700778 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700779 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700780 while (fCurrentAnimSKP < fSKPs.count()) {
781 const SkString& path = fSKPs[fCurrentAnimSKP];
782 SkAutoTUnref<SkPicture> pic;
783 if (!ReadPicture(path.c_str(), &pic)) {
784 fCurrentAnimSKP++;
785 continue;
786 }
787
788 fCurrentAnimSKP++;
789 SkString name = SkOSPath::Basename(path.c_str());
cdalton63a82852015-06-29 14:06:10 -0700790 SkAutoTUnref<SKPAnimationBench::Animation> animation(
791 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
halcanary385fe4d2015-08-26 13:07:48 -0700792 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation,
793 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700794 }
795 }
796
scroggo60869a42015-04-01 12:09:17 -0700797 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700798 fSourceType = "image";
799 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700800 const SkString& path = fImages[fCurrentCodec];
801 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
802 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700803 if (!codec) {
804 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700805 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700806 continue;
807 }
scroggo21027992015-04-02 13:22:38 -0700808
scroggo60869a42015-04-01 12:09:17 -0700809 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700810 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo60869a42015-04-01 12:09:17 -0700811 fCurrentColorType++;
scroggo21027992015-04-02 13:22:38 -0700812
scroggo60869a42015-04-01 12:09:17 -0700813 // Make sure we can decode to this color type.
scroggo60869a42015-04-01 12:09:17 -0700814 SkImageInfo info = codec->getInfo().makeColorType(colorType);
scroggo21027992015-04-02 13:22:38 -0700815 SkAlphaType alphaType;
816 if (!SkColorTypeValidateAlphaType(colorType, info.alphaType(),
817 &alphaType)) {
818 continue;
819 }
820 if (alphaType != info.alphaType()) {
821 info = info.makeAlphaType(alphaType);
822 }
823
824 const size_t rowBytes = info.minRowBytes();
825 SkAutoMalloc storage(info.getSafeSize(rowBytes));
826
827 // Used if fCurrentColorType is kIndex_8_SkColorType
828 int colorCount = 256;
829 SkPMColor colors[256];
830
scroggoeb602a52015-07-09 08:16:03 -0700831 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700832 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700833 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700834 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700835 case SkCodec::kSuccess:
836 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700837 return new CodecBench(SkOSPath::Basename(path.c_str()),
838 encoded, colorType);
scroggoeb602a52015-07-09 08:16:03 -0700839 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700840 // This is okay. Not all conversions are valid.
841 break;
scroggo60869a42015-04-01 12:09:17 -0700842 default:
843 // This represents some sort of failure.
844 SkASSERT(false);
845 break;
846 }
847 }
848 fCurrentColorType = 0;
849 }
850
msarett95f192d2015-02-13 09:05:41 -0800851 // Run the DecodingBenches
852 while (fCurrentImage < fImages.count()) {
scroggo303fa352015-10-05 11:03:34 -0700853 fSourceType = "image";
854 fBenchType = "skimagedecoder";
msarett95f192d2015-02-13 09:05:41 -0800855 while (fCurrentColorType < fColorTypes.count()) {
856 const SkString& path = fImages[fCurrentImage];
857 SkColorType colorType = fColorTypes[fCurrentColorType];
858 fCurrentColorType++;
scroggo60869a42015-04-01 12:09:17 -0700859 // Check if the image decodes to the right color type
860 // before creating the benchmark
msarett95f192d2015-02-13 09:05:41 -0800861 SkBitmap bitmap;
862 if (SkImageDecoder::DecodeFile(path.c_str(), &bitmap,
scroggo60869a42015-04-01 12:09:17 -0700863 colorType, SkImageDecoder::kDecodePixels_Mode)
864 && bitmap.colorType() == colorType) {
msarett95f192d2015-02-13 09:05:41 -0800865 return new DecodingBench(path, colorType);
866 }
867 }
868 fCurrentColorType = 0;
869 fCurrentImage++;
870 }
871
msarettb23e6aa2015-06-09 13:56:10 -0700872 // Run the SubsetBenches
msarett0459c942015-11-10 14:52:13 -0800873 while (fCurrentSubsetImage < fImages.count()) {
scroggo303fa352015-10-05 11:03:34 -0700874 fSourceType = "image";
msarett0459c942015-11-10 14:52:13 -0800875 fBenchType = "skcodec";
876 const SkString& path = fImages[fCurrentSubsetImage];
877 if (!run_subset_bench(path)) {
msarettb23e6aa2015-06-09 13:56:10 -0700878 fCurrentSubsetImage++;
msarett0459c942015-11-10 14:52:13 -0800879 continue;
msarett95f192d2015-02-13 09:05:41 -0800880 }
msarett0459c942015-11-10 14:52:13 -0800881 while (fCurrentColorType < fColorTypes.count()) {
882 SkColorType colorType = fColorTypes[fCurrentColorType];
883 while (fCurrentSubsetType <= kLast_SubsetType) {
884 int width = 0;
885 int height = 0;
886 int currentSubsetType = fCurrentSubsetType++;
887 if (valid_subset_bench(path, colorType, &width, &height)) {
888 switch (currentSubsetType) {
889 case kTopLeft_SubsetType:
890 return new SubsetSingleBench(path, colorType, width/3,
891 height/3, 0, 0);
892 case kTopRight_SubsetType:
893 return new SubsetSingleBench(path, colorType, width/3,
894 height/3, 2*width/3, 0);
895 case kMiddle_SubsetType:
896 return new SubsetSingleBench(path, colorType, width/3,
897 height/3, width/3, height/3);
898 case kBottomLeft_SubsetType:
899 return new SubsetSingleBench(path, colorType, width/3,
900 height/3, 0, 2*height/3);
901 case kBottomRight_SubsetType:
902 return new SubsetSingleBench(path, colorType, width/3,
903 height/3, 2*width/3, 2*height/3);
904 case kTranslate_SubsetType:
905 return new SubsetTranslateBench(path, colorType, 512, 512);
906 case kZoom_SubsetType:
907 return new SubsetZoomBench(path, colorType, 512, 512);
908 }
909 } else {
910 break;
911 }
912 }
913 fCurrentSubsetType = 0;
914 fCurrentColorType++;
915 }
916 fCurrentColorType = 0;
917 fCurrentSubsetImage++;
msarett95f192d2015-02-13 09:05:41 -0800918 }
919
msarett7f691442015-09-22 11:56:16 -0700920 // Run the BRDBenches
921 // We will benchmark multiple BRD strategies.
scroggo303fa352015-10-05 11:03:34 -0700922 static const struct {
msarett5cb48852015-11-06 08:56:32 -0800923 SkBitmapRegionDecoder::Strategy fStrategy;
msarett0459c942015-11-10 14:52:13 -0800924 const char* fName;
scroggo303fa352015-10-05 11:03:34 -0700925 } strategies[] = {
msarett0459c942015-11-10 14:52:13 -0800926 { SkBitmapRegionDecoder::kCanvas_Strategy, "BRD_canvas" },
msarett5cb48852015-11-06 08:56:32 -0800927 { SkBitmapRegionDecoder::kAndroidCodec_Strategy, "BRD_android_codec" },
msarett7f691442015-09-22 11:56:16 -0700928 };
929
930 // We intend to create benchmarks that model the use cases in
931 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
932 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
933 // the image. For that reason, we will benchmark decodes in five representative locations
934 // in the image. Additionally, this use case utilizes power of two scaling, so we will
935 // test on power of two sample sizes. The output tile is always 512x512, so, when a
936 // sampleSize is used, the size of the subset that is decoded is always
937 // (sampleSize*512)x(sampleSize*512).
938 // There are a few good reasons to only test on power of two sample sizes at this time:
939 // JPEG decodes using kOriginal_Strategy are broken for non-powers of two.
halcanary6950de62015-11-07 05:29:00 -0800940 // https://bug.skia.org/4319
msarett7f691442015-09-22 11:56:16 -0700941 // All use cases we are aware of only scale by powers of two.
942 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
943 // these tests are sufficient to provide good coverage of our scaling options.
scroggo501b7342015-11-03 07:55:11 -0800944 const uint32_t sampleSizes[] = { 1, 2, 4, 8, 16, 32, 64 };
msarett7f691442015-09-22 11:56:16 -0700945 const uint32_t minOutputSize = 512;
946 while (fCurrentBRDImage < fImages.count()) {
947 while (fCurrentBRDStrategy < (int) SK_ARRAY_COUNT(strategies)) {
scroggo303fa352015-10-05 11:03:34 -0700948 fSourceType = "image";
949 fBenchType = strategies[fCurrentBRDStrategy].fName;
scroggo860e8a62015-10-15 07:51:28 -0700950
951 const SkString& path = fImages[fCurrentBRDImage];
msarett5cb48852015-11-06 08:56:32 -0800952 const SkBitmapRegionDecoder::Strategy strategy =
scroggo860e8a62015-10-15 07:51:28 -0700953 strategies[fCurrentBRDStrategy].fStrategy;
954
msarett7f691442015-09-22 11:56:16 -0700955 while (fCurrentColorType < fColorTypes.count()) {
956 while (fCurrentBRDSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
957 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700958
959
msarett7f691442015-09-22 11:56:16 -0700960 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
961 const SkColorType colorType = fColorTypes[fCurrentColorType];
962 uint32_t sampleSize = sampleSizes[fCurrentBRDSampleSize];
963 int currentSubsetType = fCurrentSubsetType++;
964
965 int width = 0;
966 int height = 0;
967 if (!valid_brd_bench(encoded.get(), strategy, colorType, sampleSize,
968 minOutputSize, &width, &height)) {
969 break;
970 }
971
972 SkString basename = SkOSPath::Basename(path.c_str());
973 SkIRect subset;
974 const uint32_t subsetSize = sampleSize * minOutputSize;
975 switch (currentSubsetType) {
976 case kTopLeft_SubsetType:
977 basename.append("_TopLeft");
978 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
979 break;
980 case kTopRight_SubsetType:
981 basename.append("_TopRight");
982 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
983 subsetSize);
984 break;
985 case kMiddle_SubsetType:
986 basename.append("_Middle");
987 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
988 (height - subsetSize) / 2, subsetSize, subsetSize);
989 break;
990 case kBottomLeft_SubsetType:
991 basename.append("_BottomLeft");
992 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
993 subsetSize);
994 break;
995 case kBottomRight_SubsetType:
996 basename.append("_BottomRight");
997 subset = SkIRect::MakeXYWH(width - subsetSize,
998 height - subsetSize, subsetSize, subsetSize);
999 break;
1000 default:
1001 SkASSERT(false);
1002 }
1003
1004 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
1005 strategy, colorType, sampleSize, subset);
1006 }
1007 fCurrentSubsetType = 0;
1008 fCurrentBRDSampleSize++;
1009 }
1010 fCurrentBRDSampleSize = 0;
1011 fCurrentColorType++;
1012 }
1013 fCurrentColorType = 0;
1014 fCurrentBRDStrategy++;
1015 }
1016 fCurrentBRDStrategy = 0;
1017 fCurrentBRDImage++;
1018 }
1019
halcanary96fcdcc2015-08-27 07:41:13 -07001020 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001021 }
mtklein92007582014-08-01 07:46:52 -07001022
1023 void fillCurrentOptions(ResultsWriter* log) const {
1024 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001025 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001026 if (0 == strcmp(fSourceType, "skp")) {
1027 log->configOption("clip",
1028 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1029 fClip.fRight, fClip.fBottom).c_str());
mtklein4dfdbb12015-10-20 07:45:29 -07001030 SK_ALWAYSBREAK(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -07001031 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001032 if (fCurrentUseMPD > 0) {
1033 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1034 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1035 }
mtklein92007582014-08-01 07:46:52 -07001036 }
mtklein051e56d2014-12-04 08:46:51 -08001037 if (0 == strcmp(fBenchType, "recording")) {
1038 log->metric("bytes", fSKPBytes);
1039 log->metric("ops", fSKPOps);
1040 }
mtklein92007582014-08-01 07:46:52 -07001041 }
1042
mtkleine714e752014-07-31 12:13:48 -07001043private:
msarettb23e6aa2015-06-09 13:56:10 -07001044 enum SubsetType {
1045 kTopLeft_SubsetType = 0,
1046 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001047 kMiddle_SubsetType = 2,
1048 kBottomLeft_SubsetType = 3,
1049 kBottomRight_SubsetType = 4,
1050 kTranslate_SubsetType = 5,
1051 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001052 kLast_SubsetType = kZoom_SubsetType,
1053 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001054 };
1055
mtkleine714e752014-07-31 12:13:48 -07001056 const BenchRegistry* fBenches;
1057 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001058 SkIRect fClip;
1059 SkTArray<SkScalar> fScales;
1060 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -08001061 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001062 SkTArray<SkString> fImages;
msarett74deb982015-10-20 16:45:56 -07001063 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001064 SkScalar fZoomMax;
1065 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001066
mtklein051e56d2014-12-04 08:46:51 -08001067 double fSKPBytes, fSKPOps;
1068
mtkleinfd731ce2014-09-10 12:19:30 -07001069 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1070 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1071 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -07001072 int fCurrentScale;
1073 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -08001074 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001075 int fCurrentCodec;
msarett95f192d2015-02-13 09:05:41 -08001076 int fCurrentImage;
1077 int fCurrentSubsetImage;
msarett7f691442015-09-22 11:56:16 -07001078 int fCurrentBRDImage;
msarett95f192d2015-02-13 09:05:41 -08001079 int fCurrentColorType;
msarettb23e6aa2015-06-09 13:56:10 -07001080 int fCurrentSubsetType;
msarett7f691442015-09-22 11:56:16 -07001081 int fCurrentBRDStrategy;
1082 int fCurrentBRDSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001083 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001084};
1085
jcgregorio3b27ade2014-11-13 08:06:40 -08001086int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001087int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001088 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001089 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001090 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001091
krajcevski69a55602014-08-13 10:46:31 -07001092#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001093 GrContextOptions grContextOpts;
krajcevski12b35442014-08-13 12:06:26 -07001094 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
halcanary385fe4d2015-08-26 13:07:48 -07001095 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001096#endif
1097
bsalomon06cddec2014-10-24 10:40:50 -07001098 if (FLAGS_veryVerbose) {
1099 FLAGS_verbose = true;
1100 }
1101
bsalomon6eb03cc2014-08-07 14:28:50 -07001102 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001103 FLAGS_samples = 1;
1104 FLAGS_gpuFrameLag = 0;
1105 }
1106
bsalomon6eb03cc2014-08-07 14:28:50 -07001107 if (!FLAGS_writePath.isEmpty()) {
1108 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1109 if (!sk_mkdir(FLAGS_writePath[0])) {
1110 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001111 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001112 }
1113 }
1114
halcanary385fe4d2015-08-26 13:07:48 -07001115 SkAutoTDelete<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001116 if (!FLAGS_outResultsFile.isEmpty()) {
halcanary385fe4d2015-08-26 13:07:48 -07001117 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein60317d0f2014-07-14 11:30:37 -07001118 }
mtklein1915b622014-08-20 11:45:00 -07001119
1120 if (1 == FLAGS_properties.count() % 2) {
1121 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1122 return 1;
1123 }
1124 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1125 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1126 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001127
1128 if (1 == FLAGS_key.count() % 2) {
1129 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1130 return 1;
1131 }
1132 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001133 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001134 }
mtklein60317d0f2014-07-14 11:30:37 -07001135
mtkleinf3723212014-06-25 14:08:00 -07001136 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001137 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001138
cdaltone1b89582015-06-25 19:17:08 -07001139 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001140
bsalomon6eb03cc2014-08-07 14:28:50 -07001141 if (kAutoTuneLoops != FLAGS_loops) {
1142 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001143 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001144 SkDebugf("! -> high variance, ? -> moderate variance\n");
1145 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001146 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001147 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001148 } else {
mtkleind75c4662015-04-30 07:11:22 -07001149 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001150 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001151 }
1152
bsalomonc2553372014-07-22 13:09:05 -07001153 SkTDArray<Config> configs;
1154 create_configs(&configs);
1155
mtkleine070c2b2014-10-14 08:40:43 -07001156 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001157 BenchmarkStream benchStream;
1158 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001159 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001160 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001161 continue;
1162 }
1163
egdaniel3bf92062015-06-26 08:12:46 -07001164 if (!configs.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -07001165 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001166 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001167 }
egdaniel3bf92062015-06-26 08:12:46 -07001168 for (int i = 0; i < configs.count(); ++i) {
1169 Target* target = is_enabled(b, configs[i]);
1170 if (!target) {
1171 continue;
1172 }
mtkleinf3723212014-06-25 14:08:00 -07001173
halcanary96fcdcc2015-08-27 07:41:13 -07001174 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001175 SkCanvas* canvas = target->getCanvas();
1176 const char* config = target->config.name;
1177
1178 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001179 bench->perCanvasPreDraw(canvas);
1180
cdaltone1b89582015-06-25 19:17:08 -07001181 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001182 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001183 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1184 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001185
mtkleinbbba1682015-10-28 11:36:30 -07001186 if (FLAGS_ms) {
1187 samples.reset();
1188 auto stop = now_ms() + FLAGS_ms;
1189 do {
1190 samples.push_back(time(loops, bench, target) / loops);
1191 } while (now_ms() < stop);
1192 } else {
cdaltone1b89582015-06-25 19:17:08 -07001193 samples.reset(FLAGS_samples);
1194 for (int s = 0; s < FLAGS_samples; s++) {
egdaniel3bf92062015-06-26 08:12:46 -07001195 samples[s] = time(loops, bench, target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001196 }
cdaltone1b89582015-06-25 19:17:08 -07001197 }
mtkleinf3723212014-06-25 14:08:00 -07001198
joshualitte45c81c2015-12-02 09:05:37 -08001199#if SK_SUPPORT_GPU
1200 SkTArray<SkString> keys;
1201 SkTArray<double> values;
1202 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1203 if (gpuStatsDump) {
1204 // TODO cache stats
1205 bench->getGpuStats(canvas, &keys, &values);
1206 }
1207#endif
1208
robertphillips5b693772014-11-21 06:19:36 -08001209 bench->perCanvasPostDraw(canvas);
1210
egdaniel3bf92062015-06-26 08:12:46 -07001211 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001212 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001213 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001214 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001215 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001216 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001217 }
1218
1219 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001220 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001221 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001222 continue;
1223 }
1224
cdaltone1b89582015-06-25 19:17:08 -07001225 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001226 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001227 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001228 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001229 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001230 log->metric("min_ms", stats.min);
joshualitte45c81c2015-12-02 09:05:37 -08001231#if SK_SUPPORT_GPU
1232 if (gpuStatsDump) {
1233 // dump to json, only SKPBench currently returns valid keys / values
1234 SkASSERT(keys.count() == values.count());
1235 for (int i = 0; i < keys.count(); i++) {
1236 log->metric(keys[i].c_str(), values[i]);
1237 }
1238 }
1239#endif
1240
mtkleine070c2b2014-10-14 08:40:43 -07001241 if (runs++ % FLAGS_flushEvery == 0) {
1242 log->flush();
1243 }
mtklein60317d0f2014-07-14 11:30:37 -07001244
bsalomon6eb03cc2014-08-07 14:28:50 -07001245 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001246 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001247 config = ""; // Only print the config if we run the same bench on more than one.
1248 }
mtkleind75c4662015-04-30 07:11:22 -07001249 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1250 , sk_tools::getCurrResidentSetSizeMB()
1251 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001252 , bench->getUniqueName()
1253 , config);
mtkleinf3723212014-06-25 14:08:00 -07001254 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001255 const char* mark = " ";
1256 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1257 if (stddev_percent > 5) mark = "?";
1258 if (stddev_percent > 10) mark = "!";
1259
1260 SkDebugf("%10.2f %s\t%s\t%s\n",
1261 stats.median*1e3, mark, bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001262 } else {
1263 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001264 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1265 , sk_tools::getCurrResidentSetSizeMB()
1266 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001267 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001268 , HUMANIZE(stats.min)
1269 , HUMANIZE(stats.median)
1270 , HUMANIZE(stats.mean)
1271 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001272 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001273 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001274 , config
mtklein96289052014-09-10 12:05:59 -07001275 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001276 );
1277 }
joshualitte45c81c2015-12-02 09:05:37 -08001278
bsalomonb12ea412015-02-02 21:19:50 -08001279#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001280 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001281 GrContext* context = gGrFactory->get(configs[i].ctxType,
1282 kNone_GrGLStandard, configs[i].ctxOptions);
1283 context->printCacheStats();
1284 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001285 }
1286#endif
joshualitte45c81c2015-12-02 09:05:37 -08001287
cdalton2c56ba52015-06-26 13:32:53 -07001288 if (FLAGS_verbose) {
1289 SkDebugf("Samples: ");
1290 for (int i = 0; i < samples.count(); i++) {
1291 SkDebugf("%s ", HUMANIZE(samples[i]));
1292 }
1293 SkDebugf("%s\n", bench->getUniqueName());
1294 }
egdaniel3bf92062015-06-26 08:12:46 -07001295 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001296 }
mtkleinf3723212014-06-25 14:08:00 -07001297 }
1298
mtkleine1091452014-12-04 10:47:02 -08001299 log->bench("memory_usage", 0,0);
1300 log->config("meta");
1301 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1302
joshualitte0b19d42015-03-26 10:41:02 -07001303#if SK_SUPPORT_GPU
1304 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1305 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001306 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001307#endif
1308
mtkleinf3723212014-06-25 14:08:00 -07001309 return 0;
1310}
1311
jcgregorio3b27ade2014-11-13 08:06:40 -08001312#if !defined SK_BUILD_FOR_IOS
1313int main(int argc, char** argv) {
1314 SkCommandLineFlags::Parse(argc, argv);
1315 return nanobench_main();
1316}
1317#endif