blob: 7cc5383091d4d29723542605603c2cf8a8198850 [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"
scroggo60869a42015-04-01 12:09:17 -070013#include "CodecBench.h"
mtkleinf3723212014-06-25 14:08:00 -070014#include "CrashHandler.h"
msarett95f192d2015-02-13 09:05:41 -080015#include "DecodingBench.h"
mtkleine714e752014-07-31 12:13:48 -070016#include "GMBench.h"
mtkleinafb43792014-08-19 15:55:55 -070017#include "ProcStats.h"
mtklein60317d0f2014-07-14 11:30:37 -070018#include "ResultsWriter.h"
mtkleinfd731ce2014-09-10 12:19:30 -070019#include "RecordingBench.h"
joshualitt261c3ad2015-04-27 09:16:57 -070020#include "SKPAnimationBench.h"
mtklein92007582014-08-01 07:46:52 -070021#include "SKPBench.h"
msarettb23e6aa2015-06-09 13:56:10 -070022#include "SubsetBenchPriv.h"
msarettb23e6aa2015-06-09 13:56:10 -070023#include "SubsetSingleBench.h"
24#include "SubsetTranslateBench.h"
25#include "SubsetZoomBench.h"
mtkleinf3723212014-06-25 14:08:00 -070026#include "Stats.h"
27#include "Timer.h"
28
mtklein6838d852014-10-29 14:15:10 -070029#include "SkBBoxHierarchy.h"
mtkleinf3723212014-06-25 14:08:00 -070030#include "SkCanvas.h"
scroggo60869a42015-04-01 12:09:17 -070031#include "SkCodec.h"
caryclark17f0b6d2014-07-22 10:15:34 -070032#include "SkCommonFlags.h"
msarett95f192d2015-02-13 09:05:41 -080033#include "SkData.h"
mtkleinf3723212014-06-25 14:08:00 -070034#include "SkForceLinking.h"
35#include "SkGraphics.h"
mtklein20840502014-08-21 15:51:22 -070036#include "SkOSFile.h"
37#include "SkPictureRecorder.h"
mtklein051e56d2014-12-04 08:46:51 -080038#include "SkPictureUtils.h"
mtkleinf3723212014-06-25 14:08:00 -070039#include "SkString.h"
40#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080041#include "SkTaskGroup.h"
mtkleinf3723212014-06-25 14:08:00 -070042
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
cdaltone1b89582015-06-25 19:17:08 -070058static const int kTimedSampling = 0;
59
reed53249782014-10-10 09:09:52 -070060static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070061
mtkleinb5110422014-08-07 15:20:02 -070062static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070063#ifdef SK_DEBUG
64 1;
mtkleina189ccd2014-07-14 12:28:47 -070065#else
bsalomon6eb03cc2014-08-07 14:28:50 -070066 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070067#endif
68
bsalomon6eb03cc2014-08-07 14:28:50 -070069static SkString loops_help_txt() {
70 SkString help;
71 help.printf("Number of times to run each bench. Set this to %d to auto-"
72 "tune for each bench. Timings are only reported when auto-tuning.",
73 kAutoTuneLoops);
74 return help;
75}
76
cdaltone1b89582015-06-25 19:17:08 -070077static SkString to_string(int n) {
78 SkString str;
79 str.appendS32(n);
80 return str;
81}
82
bsalomon6eb03cc2014-08-07 14:28:50 -070083DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
84
mtkleinf3723212014-06-25 14:08:00 -070085DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
cdaltone1b89582015-06-25 19:17:08 -070086DEFINE_string(samplingTime, "0", "Amount of time to run each bench. Takes precedence over samples."
87 "Must be \"0\", \"%%lfs\", or \"%%lfms\"");
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.");
joshualitt261c3ad2015-04-27 09:16:57 -0700102DEFINE_string(zoom, "1.0,1", "Comma-separated scale,step zoom factors for SKPs.");
mtklein20840502014-08-21 15:51:22 -0700103DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
robertphillips5b693772014-11-21 06:19:36 -0800104DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700105DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700106DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800107DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800108DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
mtklein92007582014-08-01 07:46:52 -0700109
mtkleinf3723212014-06-25 14:08:00 -0700110static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700111 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800112 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700113}
mtklein55b0ffc2014-07-17 08:38:23 -0700114#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700115
tomhudsond968a6f2015-03-26 11:28:06 -0700116bool Target::init(SkImageInfo info, Benchmark* bench) {
117 if (Benchmark::kRaster_Backend == config.backend) {
118 this->surface.reset(SkSurface::NewRaster(info));
119 if (!this->surface.get()) {
120 return false;
121 }
122 }
123 return true;
124}
125bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700126 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700127 if (!canvas) {
128 return false;
129 }
130 bmp->setInfo(canvas->imageInfo());
131 if (!canvas->readPixels(bmp, 0, 0)) {
132 SkDebugf("Can't read canvas pixels.\n");
133 return false;
134 }
135 return true;
136}
137
138#if SK_SUPPORT_GPU
139struct GPUTarget : public Target {
140 explicit GPUTarget(const Config& c) : Target(c), gl(NULL) { }
141 SkGLContext* gl;
142
143 void setup() override {
144 this->gl->makeCurrent();
145 // Make sure we're done with whatever came before.
146 SK_GL(*this->gl, Finish());
147 }
148 void endTiming() override {
149 if (this->gl) {
150 SK_GL(*this->gl, Flush());
151 this->gl->swapBuffers();
152 }
153 }
154 void fence() override {
155 SK_GL(*this->gl, Finish());
156 }
mtkleind75c4662015-04-30 07:11:22 -0700157
cdaltond416a5b2015-06-23 13:23:44 -0700158 bool needsFrameTiming(int* maxFrameLag) const override {
159 if (!this->gl->getMaxGpuFrameLag(maxFrameLag)) {
160 // Frame lag is unknown.
161 *maxFrameLag = FLAGS_gpuFrameLag;
162 }
163 return true;
164 }
tomhudsond968a6f2015-03-26 11:28:06 -0700165 bool init(SkImageInfo info, Benchmark* bench) override {
166 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDistanceFieldFonts_Flag : 0;
167 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
168 this->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(this->config.ctxType),
169 SkSurface::kNo_Budgeted, info,
170 this->config.samples, &props));
171 this->gl = gGrFactory->getGLContext(this->config.ctxType);
172 if (!this->surface.get()) {
173 return false;
174 }
cdaltond416a5b2015-06-23 13:23:44 -0700175 if (!this->gl->fenceSyncSupport()) {
176 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
177 "Timings might not be accurate.\n", this->config.name);
178 }
tomhudsond968a6f2015-03-26 11:28:06 -0700179 return true;
180 }
181 void fillOptions(ResultsWriter* log) override {
182 const GrGLubyte* version;
183 SK_GL_RET(*this->gl, version, GetString(GR_GL_VERSION));
184 log->configOption("GL_VERSION", (const char*)(version));
185
186 SK_GL_RET(*this->gl, version, GetString(GR_GL_RENDERER));
187 log->configOption("GL_RENDERER", (const char*) version);
188
189 SK_GL_RET(*this->gl, version, GetString(GR_GL_VENDOR));
190 log->configOption("GL_VENDOR", (const char*) version);
191
192 SK_GL_RET(*this->gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
193 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
194 }
195};
mtkleind75c4662015-04-30 07:11:22 -0700196
tomhudsond968a6f2015-03-26 11:28:06 -0700197#endif
198
tomhudson75a0ebb2015-03-27 12:11:44 -0700199static double time(int loops, Benchmark* bench, Target* target) {
200 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700201 if (canvas) {
202 canvas->clear(SK_ColorWHITE);
203 }
mtkleinbb6a0282014-07-01 08:43:42 -0700204 WallTimer timer;
205 timer.start();
tomhudson75a0ebb2015-03-27 12:11:44 -0700206 canvas = target->beginTiming(canvas);
207 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700208 if (canvas) {
209 canvas->flush();
210 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700211 target->endTiming();
mtkleinbb6a0282014-07-01 08:43:42 -0700212 timer.end();
213 return timer.fWall;
214}
215
mtkleinf3723212014-06-25 14:08:00 -0700216static double estimate_timer_overhead() {
217 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700218 for (int i = 0; i < FLAGS_overheadLoops; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700219 WallTimer timer;
220 timer.start();
221 timer.end();
222 overhead += timer.fWall;
mtkleinf3723212014-06-25 14:08:00 -0700223 }
224 return overhead / FLAGS_overheadLoops;
225}
226
reed53249782014-10-10 09:09:52 -0700227static int detect_forever_loops(int loops) {
228 // look for a magic run-forever value
229 if (loops < 0) {
230 loops = SK_MaxS32;
231 }
232 return loops;
233}
234
mtklein55b0ffc2014-07-17 08:38:23 -0700235static int clamp_loops(int loops) {
236 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800237 SkDebugf("ERROR: clamping loops from %d to 1. "
238 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700239 return 1;
240 }
241 if (loops > FLAGS_maxLoops) {
242 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
243 return FLAGS_maxLoops;
244 }
245 return loops;
246}
247
tomhudsond968a6f2015-03-26 11:28:06 -0700248static bool write_canvas_png(Target* target, const SkString& filename) {
249
bsalomon6eb03cc2014-08-07 14:28:50 -0700250 if (filename.isEmpty()) {
251 return false;
252 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700253 if (target->getCanvas() &&
254 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700255 return false;
256 }
tomhudsond968a6f2015-03-26 11:28:06 -0700257
bsalomon6eb03cc2014-08-07 14:28:50 -0700258 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700259
260 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700261 return false;
262 }
tomhudsond968a6f2015-03-26 11:28:06 -0700263
bsalomon6eb03cc2014-08-07 14:28:50 -0700264 SkString dir = SkOSPath::Dirname(filename.c_str());
265 if (!sk_mkdir(dir.c_str())) {
266 SkDebugf("Can't make dir %s.\n", dir.c_str());
267 return false;
268 }
269 SkFILEWStream stream(filename.c_str());
270 if (!stream.isValid()) {
271 SkDebugf("Can't write %s.\n", filename.c_str());
272 return false;
273 }
274 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
275 SkDebugf("Can't encode a PNG.\n");
276 return false;
277 }
278 return true;
279}
280
281static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700282static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700283 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700284 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700285 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700286 int loops = bench->calculateLoops(FLAGS_loops);
287 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700288 while (bench_plus_overhead < overhead) {
289 if (round++ == FLAGS_maxCalibrationAttempts) {
290 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700291 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700292 return kFailedLoops;
293 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700294 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700295 }
mtklein2069e222014-08-04 13:57:39 -0700296 }
mtkleinf3723212014-06-25 14:08:00 -0700297
mtkleinbb6a0282014-07-01 08:43:42 -0700298 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700299 // We'll pick N to make timer overhead negligible:
300 //
mtkleinbb6a0282014-07-01 08:43:42 -0700301 // overhead
302 // ------------------------- < FLAGS_overheadGoal
303 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700304 //
mtkleinbb6a0282014-07-01 08:43:42 -0700305 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700306 //
307 // Doing some math, we get:
308 //
mtkleinbb6a0282014-07-01 08:43:42 -0700309 // (overhead / FLAGS_overheadGoal) - overhead
310 // ------------------------------------------ < N
311 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700312 //
313 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700314 if (kAutoTuneLoops == loops) {
315 const double numer = overhead / FLAGS_overheadGoal - overhead;
316 const double denom = bench_plus_overhead - overhead;
317 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700318 loops = clamp_loops(loops);
319 } else {
320 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700321 }
mtkleinbb6a0282014-07-01 08:43:42 -0700322
mtkleinbb6a0282014-07-01 08:43:42 -0700323 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700324}
325
cdaltone1b89582015-06-25 19:17:08 -0700326static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700327 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700328 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700329 if (kAutoTuneLoops == loops) {
330 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700331 double elapsed = 0;
332 do {
mtklein527930f2014-11-06 08:04:34 -0800333 if (1<<30 == loops) {
334 // We're about to wrap. Something's wrong with the bench.
335 loops = 0;
336 break;
337 }
mtkleina189ccd2014-07-14 12:28:47 -0700338 loops *= 2;
339 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700340 // _this_ round, not still timing last round.
341 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700342 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700343 }
344 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700345
mtkleina189ccd2014-07-14 12:28:47 -0700346 // We've overshot at least a little. Scale back linearly.
347 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700348 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700349
tomhudsond968a6f2015-03-26 11:28:06 -0700350 // Make sure we're not still timing our calibration.
351 target->fence();
reed53249782014-10-10 09:09:52 -0700352 } else {
353 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700354 }
mtkleinbb6a0282014-07-01 08:43:42 -0700355
356 // Pretty much the same deal as the calibration: do some warmup to make
357 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700358 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700359 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700360 }
mtkleinbb6a0282014-07-01 08:43:42 -0700361
mtkleinbb6a0282014-07-01 08:43:42 -0700362 return loops;
363}
mtkleinbb6a0282014-07-01 08:43:42 -0700364
365static SkString to_lower(const char* str) {
366 SkString lower(str);
367 for (size_t i = 0; i < lower.size(); i++) {
368 lower[i] = tolower(lower[i]);
369 }
370 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700371}
372
bsalomonc2553372014-07-22 13:09:05 -0700373static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700374 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700375 if (to_lower(FLAGS_config[i]).equals(name)) {
376 return true;
mtkleinf3723212014-06-25 14:08:00 -0700377 }
378 }
bsalomonc2553372014-07-22 13:09:05 -0700379 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700380}
381
bsalomonc2553372014-07-22 13:09:05 -0700382#if SK_SUPPORT_GPU
383static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
384 int sampleCnt) {
385 if (!is_cpu_config_allowed(name)) {
386 return false;
387 }
krajcevski69a55602014-08-13 10:46:31 -0700388 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomon76228632015-05-29 08:02:10 -0700389 return sampleCnt <= ctx->caps()->maxSampleCount();
bsalomonc2553372014-07-22 13:09:05 -0700390 }
391 return false;
392}
393#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700394
bsalomonc2553372014-07-22 13:09:05 -0700395#if SK_SUPPORT_GPU
396#define kBogusGLContextType GrContextFactory::kNative_GLContextType
397#else
398#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700399#endif
bsalomonc2553372014-07-22 13:09:05 -0700400
401// Append all configs that are enabled and supported.
402static void create_configs(SkTDArray<Config>* configs) {
jvanverth4736e142014-11-07 07:12:46 -0800403 #define CPU_CONFIG(name, backend, color, alpha) \
404 if (is_cpu_config_allowed(#name)) { \
405 Config config = { #name, Benchmark::backend, color, alpha, 0, \
406 kBogusGLContextType, false }; \
407 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700408 }
mtkleine714e752014-07-31 12:13:48 -0700409
mtklein40b32be2014-07-09 08:46:49 -0700410 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700411 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
412 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
413 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700414 }
mtkleinbb6a0282014-07-01 08:43:42 -0700415
416#if SK_SUPPORT_GPU
jvanverth4736e142014-11-07 07:12:46 -0800417 #define GPU_CONFIG(name, ctxType, samples, useDFText) \
bsalomonc2553372014-07-22 13:09:05 -0700418 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
419 Config config = { \
420 #name, \
421 Benchmark::kGPU_Backend, \
422 kN32_SkColorType, \
423 kPremul_SkAlphaType, \
424 samples, \
jvanverth4736e142014-11-07 07:12:46 -0800425 GrContextFactory::ctxType, \
426 useDFText }; \
bsalomonc2553372014-07-22 13:09:05 -0700427 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700428 }
mtkleine714e752014-07-31 12:13:48 -0700429
mtklein40b32be2014-07-09 08:46:49 -0700430 if (FLAGS_gpu) {
jvanverth4736e142014-11-07 07:12:46 -0800431 GPU_CONFIG(gpu, kNative_GLContextType, 0, false)
432 GPU_CONFIG(msaa4, kNative_GLContextType, 4, false)
433 GPU_CONFIG(msaa16, kNative_GLContextType, 16, false)
434 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4, false)
435 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16, false)
436 GPU_CONFIG(gpudft, kNative_GLContextType, 0, true)
437 GPU_CONFIG(debug, kDebug_GLContextType, 0, false)
438 GPU_CONFIG(nullgpu, kNull_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700439#ifdef SK_ANGLE
jvanverth4736e142014-11-07 07:12:46 -0800440 GPU_CONFIG(angle, kANGLE_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700441#endif
cdaltond416a5b2015-06-23 13:23:44 -0700442#if SK_MESA
443 GPU_CONFIG(mesa, kMESA_GLContextType, 0, false)
444#endif
mtklein40b32be2014-07-09 08:46:49 -0700445 }
mtkleinbb6a0282014-07-01 08:43:42 -0700446#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700447
448#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
449 if (is_cpu_config_allowed("hwui")) {
450 Config config = { "hwui", Benchmark::kHWUI_Backend, kRGBA_8888_SkColorType,
451 kPremul_SkAlphaType, 0, kBogusGLContextType, false };
452 configs->push(config);
453 }
454#endif
mtkleinf3723212014-06-25 14:08:00 -0700455}
456
bsalomonc2553372014-07-22 13:09:05 -0700457// If bench is enabled for config, returns a Target* for it, otherwise NULL.
458static Target* is_enabled(Benchmark* bench, const Config& config) {
459 if (!bench->isSuitableFor(config.backend)) {
460 return NULL;
461 }
462
reede5ea5002014-09-03 11:54:58 -0700463 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
464 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700465
tomhudsond968a6f2015-03-26 11:28:06 -0700466 Target* target = NULL;
bsalomonc2553372014-07-22 13:09:05 -0700467
tomhudsond968a6f2015-03-26 11:28:06 -0700468 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700469#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700470 case Benchmark::kGPU_Backend:
471 target = new GPUTarget(config);
472 break;
bsalomonc2553372014-07-22 13:09:05 -0700473#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700474#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
475 case Benchmark::kHWUI_Backend:
476 target = new HWUITarget(config, bench);
477 break;
478#endif
479 default:
480 target = new Target(config);
481 break;
482 }
bsalomonc2553372014-07-22 13:09:05 -0700483
tomhudsond968a6f2015-03-26 11:28:06 -0700484 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700485 delete target;
486 return NULL;
487 }
488 return target;
489}
490
msarettb23e6aa2015-06-09 13:56:10 -0700491/*
492 * Returns true if set up for a subset decode succeeds, false otherwise
493 * If the set-up succeeds, the width and height parameters will be set
494 */
495static bool valid_subset_bench(const SkString& path, SkColorType colorType, bool useCodec,
496 int* width, int* height) {
497 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
498 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded));
499
msarettab80e352015-06-17 10:28:22 -0700500 // Check that we can create a codec or image decoder.
msarettb23e6aa2015-06-09 13:56:10 -0700501 if (useCodec) {
502 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
503 if (NULL == codec) {
504 SkDebugf("Could not create codec for %s. Skipping bench.\n", path.c_str());
505 return false;
506 }
507
508 // These will be initialized by SkCodec if the color type is kIndex8 and
509 // unused otherwise.
510 SkPMColor colors[256];
511 int colorCount;
512 const SkImageInfo info = codec->getInfo().makeColorType(colorType);
513 SkAutoTDeleteArray<uint8_t> row(SkNEW_ARRAY(uint8_t, info.minRowBytes()));
514 SkScanlineDecoder* scanlineDecoder = codec->getScanlineDecoder(info, NULL,
515 colors, &colorCount);
516 if (NULL == scanlineDecoder) {
517 SkDebugf("Could not create scanline decoder for %s with color type %s. "
518 "Skipping bench.\n", path.c_str(), get_color_name(colorType));
519 return false;
520 }
521 *width = info.width();
522 *height = info.height();
523 } else {
524 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream));
525 if (NULL == decoder) {
526 SkDebugf("Could not create decoder for %s. Skipping bench.\n", path.c_str());
527 return false;
528 }
529 //FIXME: See skbug.com/3921
530 if (kIndex_8_SkColorType == colorType || kGray_8_SkColorType == colorType) {
531 SkDebugf("Cannot use image subset decoder for %s with color type %s. "
532 "Skipping bench.\n", path.c_str(), get_color_name(colorType));
533 return false;
534 }
535 if (!decoder->buildTileIndex(stream.detach(), width, height)) {
536 SkDebugf("Could not build tile index for %s. Skipping bench.\n", path.c_str());
537 return false;
538 }
539 }
msarettab80e352015-06-17 10:28:22 -0700540
541 // Check if the image is large enough for a meaningful subset benchmark.
542 if (*width <= 512 && *height <= 512) {
543 // This should not print a message since it is not an error.
544 return false;
545 }
546
msarettb23e6aa2015-06-09 13:56:10 -0700547 return true;
548}
jcgregoriobf5e5232014-07-17 13:14:16 -0700549
egdaniel3bf92062015-06-26 08:12:46 -0700550static void cleanup_run(Target* target) {
551 SkDELETE(target);
552#if SK_SUPPORT_GPU
553 if (FLAGS_abandonGpuContext) {
554 gGrFactory->abandonContexts();
555 }
556 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
557 gGrFactory->destroyContexts();
558 }
559#endif
560}
561
mtkleine714e752014-07-31 12:13:48 -0700562class BenchmarkStream {
563public:
mtklein92007582014-08-01 07:46:52 -0700564 BenchmarkStream() : fBenches(BenchRegistry::Head())
565 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700566 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700567 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800568 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800569 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700570 , fCurrentCodec(0)
msarett95f192d2015-02-13 09:05:41 -0800571 , fCurrentImage(0)
572 , fCurrentSubsetImage(0)
573 , fCurrentColorType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700574 , fCurrentSubsetType(0)
575 , fUseCodec(0)
576 , fCurrentAnimSKP(0) {
mtklein92007582014-08-01 07:46:52 -0700577 for (int i = 0; i < FLAGS_skps.count(); i++) {
578 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
579 fSKPs.push_back() = FLAGS_skps[i];
580 } else {
581 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
582 SkString path;
583 while (it.next(&path)) {
584 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
585 }
586 }
587 }
mtkleine714e752014-07-31 12:13:48 -0700588
mtklein92007582014-08-01 07:46:52 -0700589 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
590 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
591 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
592 exit(1);
593 }
594
595 for (int i = 0; i < FLAGS_scales.count(); i++) {
596 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
597 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
598 exit(1);
599 }
600 }
robertphillips5b693772014-11-21 06:19:36 -0800601
joshualitt261c3ad2015-04-27 09:16:57 -0700602 if (2 != sscanf(FLAGS_zoom[0], "%f,%d", &fZoomScale, &fZoomSteps)) {
603 SkDebugf("Can't parse %s from --zoom as a scale,step.\n", FLAGS_zoom[0]);
604 exit(1);
605 }
606
robertphillips5b693772014-11-21 06:19:36 -0800607 if (FLAGS_mpd) {
608 fUseMPDs.push_back() = true;
609 }
mtkleinc751ecb2015-06-15 08:56:38 -0700610 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700611
msarett95f192d2015-02-13 09:05:41 -0800612 // Prepare the images for decoding
613 for (int i = 0; i < FLAGS_images.count(); i++) {
614 const char* flag = FLAGS_images[i];
615 if (sk_isdir(flag)) {
616 // If the value passed in is a directory, add all the images
617 SkOSFile::Iter it(flag);
618 SkString file;
619 while (it.next(&file)) {
620 fImages.push_back() = SkOSPath::Join(flag, file.c_str());
621 }
622 } else if (sk_exists(flag)) {
623 // Also add the value if it is a single image
624 fImages.push_back() = flag;
625 }
626 }
mtklein95553d92015-03-12 08:24:21 -0700627
msarett95f192d2015-02-13 09:05:41 -0800628 // Choose the candidate color types for image decoding
629 const SkColorType colorTypes[] =
msarettb23e6aa2015-06-09 13:56:10 -0700630 { kN32_SkColorType,
631 kRGB_565_SkColorType,
632 kAlpha_8_SkColorType,
633 kIndex_8_SkColorType,
634 kGray_8_SkColorType };
msarett95f192d2015-02-13 09:05:41 -0800635 fColorTypes.push_back_n(SK_ARRAY_COUNT(colorTypes), colorTypes);
mtklein92007582014-08-01 07:46:52 -0700636 }
637
mtkleinfd731ce2014-09-10 12:19:30 -0700638 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
639 // Not strictly necessary, as it will be checked again later,
640 // but helps to avoid a lot of pointless work if we're going to skip it.
641 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
642 return false;
643 }
644
scroggoa1193e42015-01-21 12:09:53 -0800645 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
mtkleinfd731ce2014-09-10 12:19:30 -0700646 if (stream.get() == NULL) {
647 SkDebugf("Could not read %s.\n", path);
648 return false;
649 }
650
mtklein57f27bd2015-02-09 11:58:41 -0800651 pic->reset(SkPicture::CreateFromStream(stream.get()));
mtkleinfd731ce2014-09-10 12:19:30 -0700652 if (pic->get() == NULL) {
653 SkDebugf("Could not read %s as an SkPicture.\n", path);
654 return false;
655 }
656 return true;
657 }
658
mtklein92007582014-08-01 07:46:52 -0700659 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700660 if (fBenches) {
661 Benchmark* bench = fBenches->factory()(NULL);
662 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700663 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700664 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700665 return bench;
666 }
mtklein92007582014-08-01 07:46:52 -0700667
mtkleine714e752014-07-31 12:13:48 -0700668 while (fGMs) {
669 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
670 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800671 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700672 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700673 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700674 return SkNEW_ARGS(GMBench, (gm.detach()));
675 }
676 }
mtklein92007582014-08-01 07:46:52 -0700677
mtkleinfd731ce2014-09-10 12:19:30 -0700678 // First add all .skps as RecordingBenches.
679 while (fCurrentRecording < fSKPs.count()) {
680 const SkString& path = fSKPs[fCurrentRecording++];
681 SkAutoTUnref<SkPicture> pic;
682 if (!ReadPicture(path.c_str(), &pic)) {
683 continue;
684 }
685 SkString name = SkOSPath::Basename(path.c_str());
686 fSourceType = "skp";
687 fBenchType = "recording";
bsalomon0aa5cea2014-12-15 09:13:35 -0800688 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic));
mtklein051e56d2014-12-04 08:46:51 -0800689 fSKPOps = pic->approximateOpCount();
mtkleinfd731ce2014-09-10 12:19:30 -0700690 return SkNEW_ARGS(RecordingBench, (name.c_str(), pic.get(), FLAGS_bbh));
691 }
692
693 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700694 while (fCurrentScale < fScales.count()) {
695 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800696 const SkString& path = fSKPs[fCurrentSKP];
mtkleinfd731ce2014-09-10 12:19:30 -0700697 SkAutoTUnref<SkPicture> pic;
698 if (!ReadPicture(path.c_str(), &pic)) {
robertphillips5b693772014-11-21 06:19:36 -0800699 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700700 continue;
701 }
robertphillips5b693772014-11-21 06:19:36 -0800702
703 while (fCurrentUseMPD < fUseMPDs.count()) {
704 if (FLAGS_bbh) {
705 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
706 SkRTreeFactory factory;
707 SkPictureRecorder recorder;
708 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
709 pic->playback(recorder.beginRecording(pic->cullRect().width(),
710 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800711 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800712 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
robertphillips5b693772014-11-21 06:19:36 -0800713 pic.reset(recorder.endRecording());
714 }
715 SkString name = SkOSPath::Basename(path.c_str());
716 fSourceType = "skp";
717 fBenchType = "playback";
718 return SkNEW_ARGS(SKPBench,
cdaltonb4022962015-06-25 10:51:56 -0700719 (name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
720 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP));
joshualitt261c3ad2015-04-27 09:16:57 -0700721
mtklein20840502014-08-21 15:51:22 -0700722 }
robertphillips5b693772014-11-21 06:19:36 -0800723 fCurrentUseMPD = 0;
724 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700725 }
726 fCurrentSKP = 0;
727 fCurrentScale++;
728 }
729
joshualitt261c3ad2015-04-27 09:16:57 -0700730 // Now loop over each skp again if we have an animation
731 if (fZoomScale != 1.0f && fZoomSteps != 1) {
732 while (fCurrentAnimSKP < fSKPs.count()) {
733 const SkString& path = fSKPs[fCurrentAnimSKP];
734 SkAutoTUnref<SkPicture> pic;
735 if (!ReadPicture(path.c_str(), &pic)) {
736 fCurrentAnimSKP++;
737 continue;
738 }
739
740 fCurrentAnimSKP++;
741 SkString name = SkOSPath::Basename(path.c_str());
742 SkMatrix anim = SkMatrix::I();
743 anim.setScale(fZoomScale, fZoomScale);
744 return SkNEW_ARGS(SKPAnimationBench, (name.c_str(), pic.get(), fClip, anim,
cdaltonb4022962015-06-25 10:51:56 -0700745 fZoomSteps, FLAGS_loopSKP));
joshualitt261c3ad2015-04-27 09:16:57 -0700746 }
747 }
748
749
scroggo60869a42015-04-01 12:09:17 -0700750 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
751 const SkString& path = fImages[fCurrentCodec];
752 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
753 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700754 if (!codec) {
755 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700756 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700757 continue;
758 }
scroggo21027992015-04-02 13:22:38 -0700759
scroggo60869a42015-04-01 12:09:17 -0700760 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700761 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo60869a42015-04-01 12:09:17 -0700762 fCurrentColorType++;
scroggo21027992015-04-02 13:22:38 -0700763
scroggo60869a42015-04-01 12:09:17 -0700764 // Make sure we can decode to this color type.
scroggo60869a42015-04-01 12:09:17 -0700765 SkImageInfo info = codec->getInfo().makeColorType(colorType);
scroggo21027992015-04-02 13:22:38 -0700766 SkAlphaType alphaType;
767 if (!SkColorTypeValidateAlphaType(colorType, info.alphaType(),
768 &alphaType)) {
769 continue;
770 }
771 if (alphaType != info.alphaType()) {
772 info = info.makeAlphaType(alphaType);
773 }
774
775 const size_t rowBytes = info.minRowBytes();
776 SkAutoMalloc storage(info.getSafeSize(rowBytes));
777
778 // Used if fCurrentColorType is kIndex_8_SkColorType
779 int colorCount = 256;
780 SkPMColor colors[256];
781
scroggo60869a42015-04-01 12:09:17 -0700782 const SkImageGenerator::Result result = codec->getPixels(
scroggo21027992015-04-02 13:22:38 -0700783 info, storage.get(), rowBytes, NULL, colors,
784 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700785 switch (result) {
786 case SkImageGenerator::kSuccess:
787 case SkImageGenerator::kIncompleteInput:
788 return new CodecBench(SkOSPath::Basename(path.c_str()),
789 encoded, colorType);
790 case SkImageGenerator::kInvalidConversion:
791 // This is okay. Not all conversions are valid.
792 break;
scroggo60869a42015-04-01 12:09:17 -0700793 default:
794 // This represents some sort of failure.
795 SkASSERT(false);
796 break;
797 }
798 }
799 fCurrentColorType = 0;
800 }
801
msarett95f192d2015-02-13 09:05:41 -0800802 // Run the DecodingBenches
803 while (fCurrentImage < fImages.count()) {
804 while (fCurrentColorType < fColorTypes.count()) {
805 const SkString& path = fImages[fCurrentImage];
806 SkColorType colorType = fColorTypes[fCurrentColorType];
807 fCurrentColorType++;
scroggo60869a42015-04-01 12:09:17 -0700808 // Check if the image decodes to the right color type
809 // before creating the benchmark
msarett95f192d2015-02-13 09:05:41 -0800810 SkBitmap bitmap;
811 if (SkImageDecoder::DecodeFile(path.c_str(), &bitmap,
scroggo60869a42015-04-01 12:09:17 -0700812 colorType, SkImageDecoder::kDecodePixels_Mode)
813 && bitmap.colorType() == colorType) {
msarett95f192d2015-02-13 09:05:41 -0800814 return new DecodingBench(path, colorType);
815 }
816 }
817 fCurrentColorType = 0;
818 fCurrentImage++;
819 }
820
msarettb23e6aa2015-06-09 13:56:10 -0700821 // Run the SubsetBenches
822 bool useCodecOpts[] = { true, false };
823 while (fUseCodec < 2) {
824 bool useCodec = useCodecOpts[fUseCodec];
825 while (fCurrentSubsetImage < fImages.count()) {
826 while (fCurrentColorType < fColorTypes.count()) {
827 const SkString& path = fImages[fCurrentSubsetImage];
828 SkColorType colorType = fColorTypes[fCurrentColorType];
829 while (fCurrentSubsetType <= kLast_SubsetType) {
830 int width = 0;
831 int height = 0;
832 int currentSubsetType = fCurrentSubsetType++;
833 if (valid_subset_bench(path, colorType, useCodec, &width, &height)) {
834 switch (currentSubsetType) {
835 case kTopLeft_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700836 return new SubsetSingleBench(path, colorType, width/3,
837 height/3, 0, 0, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700838 case kTopRight_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700839 return new SubsetSingleBench(path, colorType, width/3,
840 height/3, 2*width/3, 0, useCodec);
841 case kMiddle_SubsetType:
842 return new SubsetSingleBench(path, colorType, width/3,
843 height/3, width/3, height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700844 case kBottomLeft_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700845 return new SubsetSingleBench(path, colorType, width/3,
846 height/3, 0, 2*height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700847 case kBottomRight_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700848 return new SubsetSingleBench(path, colorType, width/3,
849 height/3, 2*width/3, 2*height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700850 case kTranslate_SubsetType:
851 return new SubsetTranslateBench(path, colorType, 512, 512,
852 useCodec);
853 case kZoom_SubsetType:
854 return new SubsetZoomBench(path, colorType, 512, 512,
855 useCodec);
msarett95f192d2015-02-13 09:05:41 -0800856 }
msarettb23e6aa2015-06-09 13:56:10 -0700857 } else {
858 break;
msarett95f192d2015-02-13 09:05:41 -0800859 }
860 }
msarettb23e6aa2015-06-09 13:56:10 -0700861 fCurrentSubsetType = 0;
862 fCurrentColorType++;
msarett95f192d2015-02-13 09:05:41 -0800863 }
msarettb23e6aa2015-06-09 13:56:10 -0700864 fCurrentColorType = 0;
865 fCurrentSubsetImage++;
msarett95f192d2015-02-13 09:05:41 -0800866 }
msarettb23e6aa2015-06-09 13:56:10 -0700867 fCurrentSubsetImage = 0;
868 fUseCodec++;
msarett95f192d2015-02-13 09:05:41 -0800869 }
870
mtkleine714e752014-07-31 12:13:48 -0700871 return NULL;
872 }
mtklein92007582014-08-01 07:46:52 -0700873
874 void fillCurrentOptions(ResultsWriter* log) const {
875 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700876 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700877 if (0 == strcmp(fSourceType, "skp")) {
878 log->configOption("clip",
879 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
880 fClip.fRight, fClip.fBottom).c_str());
881 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -0800882 if (fCurrentUseMPD > 0) {
883 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
884 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
885 }
mtklein92007582014-08-01 07:46:52 -0700886 }
mtklein051e56d2014-12-04 08:46:51 -0800887 if (0 == strcmp(fBenchType, "recording")) {
888 log->metric("bytes", fSKPBytes);
889 log->metric("ops", fSKPOps);
890 }
mtklein92007582014-08-01 07:46:52 -0700891 }
892
mtkleine714e752014-07-31 12:13:48 -0700893private:
msarettb23e6aa2015-06-09 13:56:10 -0700894 enum SubsetType {
895 kTopLeft_SubsetType = 0,
896 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -0700897 kMiddle_SubsetType = 2,
898 kBottomLeft_SubsetType = 3,
899 kBottomRight_SubsetType = 4,
900 kTranslate_SubsetType = 5,
901 kZoom_SubsetType = 6,
msarettb23e6aa2015-06-09 13:56:10 -0700902 kLast_SubsetType = kZoom_SubsetType
903 };
904
mtkleine714e752014-07-31 12:13:48 -0700905 const BenchRegistry* fBenches;
906 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700907 SkIRect fClip;
908 SkTArray<SkScalar> fScales;
909 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -0800910 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -0800911 SkTArray<SkString> fImages;
912 SkTArray<SkColorType> fColorTypes;
joshualitt261c3ad2015-04-27 09:16:57 -0700913 SkScalar fZoomScale;
914 int fZoomSteps;
mtklein92007582014-08-01 07:46:52 -0700915
mtklein051e56d2014-12-04 08:46:51 -0800916 double fSKPBytes, fSKPOps;
917
mtkleinfd731ce2014-09-10 12:19:30 -0700918 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
919 const char* fBenchType; // How we bench it: micro, recording, playback, ...
920 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700921 int fCurrentScale;
922 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -0800923 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -0700924 int fCurrentCodec;
msarett95f192d2015-02-13 09:05:41 -0800925 int fCurrentImage;
926 int fCurrentSubsetImage;
927 int fCurrentColorType;
msarettb23e6aa2015-06-09 13:56:10 -0700928 int fCurrentSubsetType;
929 int fUseCodec;
joshualitt261c3ad2015-04-27 09:16:57 -0700930 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -0700931};
932
jcgregorio3b27ade2014-11-13 08:06:40 -0800933int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700934int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800935 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -0700936 SkAutoGraphics ag;
mtklein4f108442014-12-03 13:07:39 -0800937 SkTaskGroup::Enabler enabled;
mtkleinf3723212014-06-25 14:08:00 -0700938
krajcevski69a55602014-08-13 10:46:31 -0700939#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -0700940 GrContextOptions grContextOpts;
krajcevski12b35442014-08-13 12:06:26 -0700941 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
942 gGrFactory.reset(SkNEW_ARGS(GrContextFactory, (grContextOpts)));
krajcevski69a55602014-08-13 10:46:31 -0700943#endif
944
bsalomon06cddec2014-10-24 10:40:50 -0700945 if (FLAGS_veryVerbose) {
946 FLAGS_verbose = true;
947 }
948
cdaltone1b89582015-06-25 19:17:08 -0700949 double samplingTimeMs = 0;
950 if (0 != strcmp("0", FLAGS_samplingTime[0])) {
951 SkSTArray<8, char> timeUnit;
952 timeUnit.push_back_n(static_cast<int>(strlen(FLAGS_samplingTime[0])) + 1);
953 if (2 != sscanf(FLAGS_samplingTime[0], "%lf%s", &samplingTimeMs, timeUnit.begin()) ||
954 (0 != strcmp("s", timeUnit.begin()) && 0 != strcmp("ms", timeUnit.begin()))) {
955 SkDebugf("Invalid --samplingTime \"%s\". Must be \"0\", \"%%lfs\", or \"%%lfms\"\n",
956 FLAGS_samplingTime[0]);
957 exit(0);
958 }
959 if (0 == strcmp("s", timeUnit.begin())) {
960 samplingTimeMs *= 1000;
961 }
962 if (samplingTimeMs) {
963 FLAGS_samples = kTimedSampling;
964 }
965 }
966
bsalomon6eb03cc2014-08-07 14:28:50 -0700967 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700968 FLAGS_samples = 1;
969 FLAGS_gpuFrameLag = 0;
970 }
971
bsalomon6eb03cc2014-08-07 14:28:50 -0700972 if (!FLAGS_writePath.isEmpty()) {
973 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
974 if (!sk_mkdir(FLAGS_writePath[0])) {
975 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
976 FLAGS_writePath.set(0, NULL);
977 }
978 }
979
mtklein1915b622014-08-20 11:45:00 -0700980 SkAutoTDelete<ResultsWriter> log(SkNEW(ResultsWriter));
mtklein60317d0f2014-07-14 11:30:37 -0700981 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700982 log.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0])));
mtklein60317d0f2014-07-14 11:30:37 -0700983 }
mtklein1915b622014-08-20 11:45:00 -0700984
985 if (1 == FLAGS_properties.count() % 2) {
986 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
987 return 1;
988 }
989 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
990 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
991 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700992
993 if (1 == FLAGS_key.count() % 2) {
994 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
995 return 1;
996 }
997 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -0700998 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -0700999 }
mtklein60317d0f2014-07-14 11:30:37 -07001000
mtkleinf3723212014-06-25 14:08:00 -07001001 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001002 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001003
cdaltone1b89582015-06-25 19:17:08 -07001004 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001005
bsalomon6eb03cc2014-08-07 14:28:50 -07001006 if (kAutoTuneLoops != FLAGS_loops) {
1007 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001008 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -07001009 SkDebugf("median\tbench\tconfig\n");
cdaltone1b89582015-06-25 19:17:08 -07001010 } else if (kTimedSampling == FLAGS_samples) {
1011 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001012 } else {
mtkleind75c4662015-04-30 07:11:22 -07001013 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001014 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001015 }
1016
bsalomonc2553372014-07-22 13:09:05 -07001017 SkTDArray<Config> configs;
1018 create_configs(&configs);
1019
mtkleine070c2b2014-10-14 08:40:43 -07001020 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001021 BenchmarkStream benchStream;
1022 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001023 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001024 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001025 continue;
1026 }
1027
egdaniel3bf92062015-06-26 08:12:46 -07001028 if (!configs.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -07001029 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
jcgregoriobf5e5232014-07-17 13:14:16 -07001030 bench->preDraw();
1031 }
egdaniel3bf92062015-06-26 08:12:46 -07001032 for (int i = 0; i < configs.count(); ++i) {
1033 Target* target = is_enabled(b, configs[i]);
1034 if (!target) {
1035 continue;
1036 }
mtkleinf3723212014-06-25 14:08:00 -07001037
egdaniel3bf92062015-06-26 08:12:46 -07001038 // During HWUI output this canvas may be NULL.
1039 SkCanvas* canvas = target->getCanvas();
1040 const char* config = target->config.name;
1041
1042 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001043 bench->perCanvasPreDraw(canvas);
1044
cdaltone1b89582015-06-25 19:17:08 -07001045 int maxFrameLag;
egdaniel3bf92062015-06-26 08:12:46 -07001046 const int loops = target->needsFrameTiming(&maxFrameLag)
1047 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1048 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001049
1050 if (kTimedSampling != FLAGS_samples) {
1051 samples.reset(FLAGS_samples);
1052 for (int s = 0; s < FLAGS_samples; s++) {
egdaniel3bf92062015-06-26 08:12:46 -07001053 samples[s] = time(loops, bench, target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001054 }
1055 } else if (samplingTimeMs) {
1056 samples.reset();
1057 if (FLAGS_verbose) {
1058 SkDebugf("Begin sampling %s for %ims\n",
1059 bench->getUniqueName(), static_cast<int>(samplingTimeMs));
1060 }
1061 WallTimer timer;
1062 timer.start();
1063 do {
egdaniel3bf92062015-06-26 08:12:46 -07001064 samples.push_back(time(loops, bench, target) / loops);
cdaltone1b89582015-06-25 19:17:08 -07001065 timer.end();
1066 } while (timer.fWall < samplingTimeMs);
1067 }
mtkleinf3723212014-06-25 14:08:00 -07001068
robertphillips5b693772014-11-21 06:19:36 -08001069 bench->perCanvasPostDraw(canvas);
1070
egdaniel3bf92062015-06-26 08:12:46 -07001071 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001072 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001073 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001074 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001075 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001076 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001077 }
1078
1079 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001080 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001081 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001082 continue;
1083 }
1084
cdaltone1b89582015-06-25 19:17:08 -07001085 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001086 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001087 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001088 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001089 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001090 log->metric("min_ms", stats.min);
mtkleine070c2b2014-10-14 08:40:43 -07001091 if (runs++ % FLAGS_flushEvery == 0) {
1092 log->flush();
1093 }
mtklein60317d0f2014-07-14 11:30:37 -07001094
bsalomon6eb03cc2014-08-07 14:28:50 -07001095 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001096 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001097 config = ""; // Only print the config if we run the same bench on more than one.
1098 }
mtkleind75c4662015-04-30 07:11:22 -07001099 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1100 , sk_tools::getCurrResidentSetSizeMB()
1101 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001102 , bench->getUniqueName()
1103 , config);
mtkleinf3723212014-06-25 14:08:00 -07001104 } else if (FLAGS_quiet) {
egdaniel3bf92062015-06-26 08:12:46 -07001105 if (configs.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -07001106 config = ""; // Only print the config if we run the same bench on more than one.
1107 }
mtklein96289052014-09-10 12:05:59 -07001108 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001109 } else {
1110 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001111 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1112 , sk_tools::getCurrResidentSetSizeMB()
1113 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001114 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001115 , HUMANIZE(stats.min)
1116 , HUMANIZE(stats.median)
1117 , HUMANIZE(stats.mean)
1118 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001119 , stddev_percent
cdaltone1b89582015-06-25 19:17:08 -07001120 , kTimedSampling != FLAGS_samples ? stats.plot.c_str()
1121 : to_string(samples.count()).c_str()
mtkleinf3723212014-06-25 14:08:00 -07001122 , config
mtklein96289052014-09-10 12:05:59 -07001123 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001124 );
1125 }
bsalomonb12ea412015-02-02 21:19:50 -08001126#if SK_SUPPORT_GPU
1127 if (FLAGS_gpuStats &&
egdaniel3bf92062015-06-26 08:12:46 -07001128 Benchmark::kGPU_Backend == configs[i].backend) {
1129 gGrFactory->get(configs[i].ctxType)->printCacheStats();
1130 gGrFactory->get(configs[i].ctxType)->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001131 }
1132#endif
cdalton2c56ba52015-06-26 13:32:53 -07001133 if (FLAGS_verbose) {
1134 SkDebugf("Samples: ");
1135 for (int i = 0; i < samples.count(); i++) {
1136 SkDebugf("%s ", HUMANIZE(samples[i]));
1137 }
1138 SkDebugf("%s\n", bench->getUniqueName());
1139 }
egdaniel3bf92062015-06-26 08:12:46 -07001140 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001141 }
mtkleinf3723212014-06-25 14:08:00 -07001142 }
1143
mtkleine1091452014-12-04 10:47:02 -08001144 log->bench("memory_usage", 0,0);
1145 log->config("meta");
1146 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1147
joshualitte0b19d42015-03-26 10:41:02 -07001148#if SK_SUPPORT_GPU
1149 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1150 // SkEventTracer destructor
1151 gGrFactory.reset(NULL);
1152#endif
1153
mtkleinf3723212014-06-25 14:08:00 -07001154 return 0;
1155}
1156
jcgregorio3b27ade2014-11-13 08:06:40 -08001157#if !defined SK_BUILD_FOR_IOS
1158int main(int argc, char** argv) {
1159 SkCommandLineFlags::Parse(argc, argv);
1160 return nanobench_main();
1161}
1162#endif