blob: 4aad61da72aece64376e4d378dfe1ea7d8e93857 [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
491// Creates targets for a benchmark and a set of configs.
492static void create_targets(SkTDArray<Target*>* targets, Benchmark* b,
493 const SkTDArray<Config>& configs) {
494 for (int i = 0; i < configs.count(); ++i) {
495 if (Target* t = is_enabled(b, configs[i])) {
496 targets->push(t);
497 }
mtkleine714e752014-07-31 12:13:48 -0700498
bsalomonc2553372014-07-22 13:09:05 -0700499 }
500}
501
msarettb23e6aa2015-06-09 13:56:10 -0700502/*
503 * Returns true if set up for a subset decode succeeds, false otherwise
504 * If the set-up succeeds, the width and height parameters will be set
505 */
506static bool valid_subset_bench(const SkString& path, SkColorType colorType, bool useCodec,
507 int* width, int* height) {
508 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
509 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded));
510
msarettab80e352015-06-17 10:28:22 -0700511 // Check that we can create a codec or image decoder.
msarettb23e6aa2015-06-09 13:56:10 -0700512 if (useCodec) {
513 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
514 if (NULL == codec) {
515 SkDebugf("Could not create codec for %s. Skipping bench.\n", path.c_str());
516 return false;
517 }
518
519 // These will be initialized by SkCodec if the color type is kIndex8 and
520 // unused otherwise.
521 SkPMColor colors[256];
522 int colorCount;
523 const SkImageInfo info = codec->getInfo().makeColorType(colorType);
524 SkAutoTDeleteArray<uint8_t> row(SkNEW_ARRAY(uint8_t, info.minRowBytes()));
525 SkScanlineDecoder* scanlineDecoder = codec->getScanlineDecoder(info, NULL,
526 colors, &colorCount);
527 if (NULL == scanlineDecoder) {
528 SkDebugf("Could not create scanline decoder for %s with color type %s. "
529 "Skipping bench.\n", path.c_str(), get_color_name(colorType));
530 return false;
531 }
532 *width = info.width();
533 *height = info.height();
534 } else {
535 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream));
536 if (NULL == decoder) {
537 SkDebugf("Could not create decoder for %s. Skipping bench.\n", path.c_str());
538 return false;
539 }
540 //FIXME: See skbug.com/3921
541 if (kIndex_8_SkColorType == colorType || kGray_8_SkColorType == colorType) {
542 SkDebugf("Cannot use image subset decoder for %s with color type %s. "
543 "Skipping bench.\n", path.c_str(), get_color_name(colorType));
544 return false;
545 }
546 if (!decoder->buildTileIndex(stream.detach(), width, height)) {
547 SkDebugf("Could not build tile index for %s. Skipping bench.\n", path.c_str());
548 return false;
549 }
550 }
msarettab80e352015-06-17 10:28:22 -0700551
552 // Check if the image is large enough for a meaningful subset benchmark.
553 if (*width <= 512 && *height <= 512) {
554 // This should not print a message since it is not an error.
555 return false;
556 }
557
msarettb23e6aa2015-06-09 13:56:10 -0700558 return true;
559}
jcgregoriobf5e5232014-07-17 13:14:16 -0700560
mtkleine714e752014-07-31 12:13:48 -0700561class BenchmarkStream {
562public:
mtklein92007582014-08-01 07:46:52 -0700563 BenchmarkStream() : fBenches(BenchRegistry::Head())
564 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700565 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700566 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800567 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800568 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700569 , fCurrentCodec(0)
msarett95f192d2015-02-13 09:05:41 -0800570 , fCurrentImage(0)
571 , fCurrentSubsetImage(0)
572 , fCurrentColorType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700573 , fCurrentSubsetType(0)
574 , fUseCodec(0)
575 , fCurrentAnimSKP(0) {
mtklein92007582014-08-01 07:46:52 -0700576 for (int i = 0; i < FLAGS_skps.count(); i++) {
577 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
578 fSKPs.push_back() = FLAGS_skps[i];
579 } else {
580 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
581 SkString path;
582 while (it.next(&path)) {
583 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
584 }
585 }
586 }
mtkleine714e752014-07-31 12:13:48 -0700587
mtklein92007582014-08-01 07:46:52 -0700588 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
589 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
590 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
591 exit(1);
592 }
593
594 for (int i = 0; i < FLAGS_scales.count(); i++) {
595 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
596 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
597 exit(1);
598 }
599 }
robertphillips5b693772014-11-21 06:19:36 -0800600
joshualitt261c3ad2015-04-27 09:16:57 -0700601 if (2 != sscanf(FLAGS_zoom[0], "%f,%d", &fZoomScale, &fZoomSteps)) {
602 SkDebugf("Can't parse %s from --zoom as a scale,step.\n", FLAGS_zoom[0]);
603 exit(1);
604 }
605
robertphillips5b693772014-11-21 06:19:36 -0800606 if (FLAGS_mpd) {
607 fUseMPDs.push_back() = true;
608 }
mtkleinc751ecb2015-06-15 08:56:38 -0700609 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700610
msarett95f192d2015-02-13 09:05:41 -0800611 // Prepare the images for decoding
612 for (int i = 0; i < FLAGS_images.count(); i++) {
613 const char* flag = FLAGS_images[i];
614 if (sk_isdir(flag)) {
615 // If the value passed in is a directory, add all the images
616 SkOSFile::Iter it(flag);
617 SkString file;
618 while (it.next(&file)) {
619 fImages.push_back() = SkOSPath::Join(flag, file.c_str());
620 }
621 } else if (sk_exists(flag)) {
622 // Also add the value if it is a single image
623 fImages.push_back() = flag;
624 }
625 }
mtklein95553d92015-03-12 08:24:21 -0700626
msarett95f192d2015-02-13 09:05:41 -0800627 // Choose the candidate color types for image decoding
628 const SkColorType colorTypes[] =
msarettb23e6aa2015-06-09 13:56:10 -0700629 { kN32_SkColorType,
630 kRGB_565_SkColorType,
631 kAlpha_8_SkColorType,
632 kIndex_8_SkColorType,
633 kGray_8_SkColorType };
msarett95f192d2015-02-13 09:05:41 -0800634 fColorTypes.push_back_n(SK_ARRAY_COUNT(colorTypes), colorTypes);
mtklein92007582014-08-01 07:46:52 -0700635 }
636
mtkleinfd731ce2014-09-10 12:19:30 -0700637 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
638 // Not strictly necessary, as it will be checked again later,
639 // but helps to avoid a lot of pointless work if we're going to skip it.
640 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
641 return false;
642 }
643
scroggoa1193e42015-01-21 12:09:53 -0800644 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
mtkleinfd731ce2014-09-10 12:19:30 -0700645 if (stream.get() == NULL) {
646 SkDebugf("Could not read %s.\n", path);
647 return false;
648 }
649
mtklein57f27bd2015-02-09 11:58:41 -0800650 pic->reset(SkPicture::CreateFromStream(stream.get()));
mtkleinfd731ce2014-09-10 12:19:30 -0700651 if (pic->get() == NULL) {
652 SkDebugf("Could not read %s as an SkPicture.\n", path);
653 return false;
654 }
655 return true;
656 }
657
mtklein92007582014-08-01 07:46:52 -0700658 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700659 if (fBenches) {
660 Benchmark* bench = fBenches->factory()(NULL);
661 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700662 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700663 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700664 return bench;
665 }
mtklein92007582014-08-01 07:46:52 -0700666
mtkleine714e752014-07-31 12:13:48 -0700667 while (fGMs) {
668 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
669 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800670 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700671 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700672 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700673 return SkNEW_ARGS(GMBench, (gm.detach()));
674 }
675 }
mtklein92007582014-08-01 07:46:52 -0700676
mtkleinfd731ce2014-09-10 12:19:30 -0700677 // First add all .skps as RecordingBenches.
678 while (fCurrentRecording < fSKPs.count()) {
679 const SkString& path = fSKPs[fCurrentRecording++];
680 SkAutoTUnref<SkPicture> pic;
681 if (!ReadPicture(path.c_str(), &pic)) {
682 continue;
683 }
684 SkString name = SkOSPath::Basename(path.c_str());
685 fSourceType = "skp";
686 fBenchType = "recording";
bsalomon0aa5cea2014-12-15 09:13:35 -0800687 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic));
mtklein051e56d2014-12-04 08:46:51 -0800688 fSKPOps = pic->approximateOpCount();
mtkleinfd731ce2014-09-10 12:19:30 -0700689 return SkNEW_ARGS(RecordingBench, (name.c_str(), pic.get(), FLAGS_bbh));
690 }
691
692 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700693 while (fCurrentScale < fScales.count()) {
694 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800695 const SkString& path = fSKPs[fCurrentSKP];
mtkleinfd731ce2014-09-10 12:19:30 -0700696 SkAutoTUnref<SkPicture> pic;
697 if (!ReadPicture(path.c_str(), &pic)) {
robertphillips5b693772014-11-21 06:19:36 -0800698 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700699 continue;
700 }
robertphillips5b693772014-11-21 06:19:36 -0800701
702 while (fCurrentUseMPD < fUseMPDs.count()) {
703 if (FLAGS_bbh) {
704 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
705 SkRTreeFactory factory;
706 SkPictureRecorder recorder;
707 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
708 pic->playback(recorder.beginRecording(pic->cullRect().width(),
709 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800710 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800711 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
robertphillips5b693772014-11-21 06:19:36 -0800712 pic.reset(recorder.endRecording());
713 }
714 SkString name = SkOSPath::Basename(path.c_str());
715 fSourceType = "skp";
716 fBenchType = "playback";
717 return SkNEW_ARGS(SKPBench,
cdaltonb4022962015-06-25 10:51:56 -0700718 (name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
719 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP));
joshualitt261c3ad2015-04-27 09:16:57 -0700720
mtklein20840502014-08-21 15:51:22 -0700721 }
robertphillips5b693772014-11-21 06:19:36 -0800722 fCurrentUseMPD = 0;
723 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700724 }
725 fCurrentSKP = 0;
726 fCurrentScale++;
727 }
728
joshualitt261c3ad2015-04-27 09:16:57 -0700729 // Now loop over each skp again if we have an animation
730 if (fZoomScale != 1.0f && fZoomSteps != 1) {
731 while (fCurrentAnimSKP < fSKPs.count()) {
732 const SkString& path = fSKPs[fCurrentAnimSKP];
733 SkAutoTUnref<SkPicture> pic;
734 if (!ReadPicture(path.c_str(), &pic)) {
735 fCurrentAnimSKP++;
736 continue;
737 }
738
739 fCurrentAnimSKP++;
740 SkString name = SkOSPath::Basename(path.c_str());
741 SkMatrix anim = SkMatrix::I();
742 anim.setScale(fZoomScale, fZoomScale);
743 return SkNEW_ARGS(SKPAnimationBench, (name.c_str(), pic.get(), fClip, anim,
cdaltonb4022962015-06-25 10:51:56 -0700744 fZoomSteps, FLAGS_loopSKP));
joshualitt261c3ad2015-04-27 09:16:57 -0700745 }
746 }
747
748
scroggo60869a42015-04-01 12:09:17 -0700749 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
750 const SkString& path = fImages[fCurrentCodec];
751 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
752 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700753 if (!codec) {
754 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700755 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700756 continue;
757 }
scroggo21027992015-04-02 13:22:38 -0700758
scroggo60869a42015-04-01 12:09:17 -0700759 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700760 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo60869a42015-04-01 12:09:17 -0700761 fCurrentColorType++;
scroggo21027992015-04-02 13:22:38 -0700762
scroggo60869a42015-04-01 12:09:17 -0700763 // Make sure we can decode to this color type.
scroggo60869a42015-04-01 12:09:17 -0700764 SkImageInfo info = codec->getInfo().makeColorType(colorType);
scroggo21027992015-04-02 13:22:38 -0700765 SkAlphaType alphaType;
766 if (!SkColorTypeValidateAlphaType(colorType, info.alphaType(),
767 &alphaType)) {
768 continue;
769 }
770 if (alphaType != info.alphaType()) {
771 info = info.makeAlphaType(alphaType);
772 }
773
774 const size_t rowBytes = info.minRowBytes();
775 SkAutoMalloc storage(info.getSafeSize(rowBytes));
776
777 // Used if fCurrentColorType is kIndex_8_SkColorType
778 int colorCount = 256;
779 SkPMColor colors[256];
780
scroggo60869a42015-04-01 12:09:17 -0700781 const SkImageGenerator::Result result = codec->getPixels(
scroggo21027992015-04-02 13:22:38 -0700782 info, storage.get(), rowBytes, NULL, colors,
783 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700784 switch (result) {
785 case SkImageGenerator::kSuccess:
786 case SkImageGenerator::kIncompleteInput:
787 return new CodecBench(SkOSPath::Basename(path.c_str()),
788 encoded, colorType);
789 case SkImageGenerator::kInvalidConversion:
790 // This is okay. Not all conversions are valid.
791 break;
scroggo60869a42015-04-01 12:09:17 -0700792 default:
793 // This represents some sort of failure.
794 SkASSERT(false);
795 break;
796 }
797 }
798 fCurrentColorType = 0;
799 }
800
msarett95f192d2015-02-13 09:05:41 -0800801 // Run the DecodingBenches
802 while (fCurrentImage < fImages.count()) {
803 while (fCurrentColorType < fColorTypes.count()) {
804 const SkString& path = fImages[fCurrentImage];
805 SkColorType colorType = fColorTypes[fCurrentColorType];
806 fCurrentColorType++;
scroggo60869a42015-04-01 12:09:17 -0700807 // Check if the image decodes to the right color type
808 // before creating the benchmark
msarett95f192d2015-02-13 09:05:41 -0800809 SkBitmap bitmap;
810 if (SkImageDecoder::DecodeFile(path.c_str(), &bitmap,
scroggo60869a42015-04-01 12:09:17 -0700811 colorType, SkImageDecoder::kDecodePixels_Mode)
812 && bitmap.colorType() == colorType) {
msarett95f192d2015-02-13 09:05:41 -0800813 return new DecodingBench(path, colorType);
814 }
815 }
816 fCurrentColorType = 0;
817 fCurrentImage++;
818 }
819
msarettb23e6aa2015-06-09 13:56:10 -0700820 // Run the SubsetBenches
821 bool useCodecOpts[] = { true, false };
822 while (fUseCodec < 2) {
823 bool useCodec = useCodecOpts[fUseCodec];
824 while (fCurrentSubsetImage < fImages.count()) {
825 while (fCurrentColorType < fColorTypes.count()) {
826 const SkString& path = fImages[fCurrentSubsetImage];
827 SkColorType colorType = fColorTypes[fCurrentColorType];
828 while (fCurrentSubsetType <= kLast_SubsetType) {
829 int width = 0;
830 int height = 0;
831 int currentSubsetType = fCurrentSubsetType++;
832 if (valid_subset_bench(path, colorType, useCodec, &width, &height)) {
833 switch (currentSubsetType) {
834 case kTopLeft_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700835 return new SubsetSingleBench(path, colorType, width/3,
836 height/3, 0, 0, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700837 case kTopRight_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700838 return new SubsetSingleBench(path, colorType, width/3,
839 height/3, 2*width/3, 0, useCodec);
840 case kMiddle_SubsetType:
841 return new SubsetSingleBench(path, colorType, width/3,
842 height/3, width/3, height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700843 case kBottomLeft_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700844 return new SubsetSingleBench(path, colorType, width/3,
845 height/3, 0, 2*height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700846 case kBottomRight_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700847 return new SubsetSingleBench(path, colorType, width/3,
848 height/3, 2*width/3, 2*height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700849 case kTranslate_SubsetType:
850 return new SubsetTranslateBench(path, colorType, 512, 512,
851 useCodec);
852 case kZoom_SubsetType:
853 return new SubsetZoomBench(path, colorType, 512, 512,
854 useCodec);
msarett95f192d2015-02-13 09:05:41 -0800855 }
msarettb23e6aa2015-06-09 13:56:10 -0700856 } else {
857 break;
msarett95f192d2015-02-13 09:05:41 -0800858 }
859 }
msarettb23e6aa2015-06-09 13:56:10 -0700860 fCurrentSubsetType = 0;
861 fCurrentColorType++;
msarett95f192d2015-02-13 09:05:41 -0800862 }
msarettb23e6aa2015-06-09 13:56:10 -0700863 fCurrentColorType = 0;
864 fCurrentSubsetImage++;
msarett95f192d2015-02-13 09:05:41 -0800865 }
msarettb23e6aa2015-06-09 13:56:10 -0700866 fCurrentSubsetImage = 0;
867 fUseCodec++;
msarett95f192d2015-02-13 09:05:41 -0800868 }
869
mtkleine714e752014-07-31 12:13:48 -0700870 return NULL;
871 }
mtklein92007582014-08-01 07:46:52 -0700872
873 void fillCurrentOptions(ResultsWriter* log) const {
874 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700875 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700876 if (0 == strcmp(fSourceType, "skp")) {
877 log->configOption("clip",
878 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
879 fClip.fRight, fClip.fBottom).c_str());
880 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -0800881 if (fCurrentUseMPD > 0) {
882 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
883 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
884 }
mtklein92007582014-08-01 07:46:52 -0700885 }
mtklein051e56d2014-12-04 08:46:51 -0800886 if (0 == strcmp(fBenchType, "recording")) {
887 log->metric("bytes", fSKPBytes);
888 log->metric("ops", fSKPOps);
889 }
mtklein92007582014-08-01 07:46:52 -0700890 }
891
mtkleine714e752014-07-31 12:13:48 -0700892private:
msarettb23e6aa2015-06-09 13:56:10 -0700893 enum SubsetType {
894 kTopLeft_SubsetType = 0,
895 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -0700896 kMiddle_SubsetType = 2,
897 kBottomLeft_SubsetType = 3,
898 kBottomRight_SubsetType = 4,
899 kTranslate_SubsetType = 5,
900 kZoom_SubsetType = 6,
msarettb23e6aa2015-06-09 13:56:10 -0700901 kLast_SubsetType = kZoom_SubsetType
902 };
903
mtkleine714e752014-07-31 12:13:48 -0700904 const BenchRegistry* fBenches;
905 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700906 SkIRect fClip;
907 SkTArray<SkScalar> fScales;
908 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -0800909 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -0800910 SkTArray<SkString> fImages;
911 SkTArray<SkColorType> fColorTypes;
joshualitt261c3ad2015-04-27 09:16:57 -0700912 SkScalar fZoomScale;
913 int fZoomSteps;
mtklein92007582014-08-01 07:46:52 -0700914
mtklein051e56d2014-12-04 08:46:51 -0800915 double fSKPBytes, fSKPOps;
916
mtkleinfd731ce2014-09-10 12:19:30 -0700917 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
918 const char* fBenchType; // How we bench it: micro, recording, playback, ...
919 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700920 int fCurrentScale;
921 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -0800922 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -0700923 int fCurrentCodec;
msarett95f192d2015-02-13 09:05:41 -0800924 int fCurrentImage;
925 int fCurrentSubsetImage;
926 int fCurrentColorType;
msarettb23e6aa2015-06-09 13:56:10 -0700927 int fCurrentSubsetType;
928 int fUseCodec;
joshualitt261c3ad2015-04-27 09:16:57 -0700929 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -0700930};
931
jcgregorio3b27ade2014-11-13 08:06:40 -0800932int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700933int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800934 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -0700935 SkAutoGraphics ag;
mtklein4f108442014-12-03 13:07:39 -0800936 SkTaskGroup::Enabler enabled;
mtkleinf3723212014-06-25 14:08:00 -0700937
krajcevski69a55602014-08-13 10:46:31 -0700938#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -0700939 GrContextOptions grContextOpts;
krajcevski12b35442014-08-13 12:06:26 -0700940 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
941 gGrFactory.reset(SkNEW_ARGS(GrContextFactory, (grContextOpts)));
krajcevski69a55602014-08-13 10:46:31 -0700942#endif
943
bsalomon06cddec2014-10-24 10:40:50 -0700944 if (FLAGS_veryVerbose) {
945 FLAGS_verbose = true;
946 }
947
cdaltone1b89582015-06-25 19:17:08 -0700948 double samplingTimeMs = 0;
949 if (0 != strcmp("0", FLAGS_samplingTime[0])) {
950 SkSTArray<8, char> timeUnit;
951 timeUnit.push_back_n(static_cast<int>(strlen(FLAGS_samplingTime[0])) + 1);
952 if (2 != sscanf(FLAGS_samplingTime[0], "%lf%s", &samplingTimeMs, timeUnit.begin()) ||
953 (0 != strcmp("s", timeUnit.begin()) && 0 != strcmp("ms", timeUnit.begin()))) {
954 SkDebugf("Invalid --samplingTime \"%s\". Must be \"0\", \"%%lfs\", or \"%%lfms\"\n",
955 FLAGS_samplingTime[0]);
956 exit(0);
957 }
958 if (0 == strcmp("s", timeUnit.begin())) {
959 samplingTimeMs *= 1000;
960 }
961 if (samplingTimeMs) {
962 FLAGS_samples = kTimedSampling;
963 }
964 }
965
bsalomon6eb03cc2014-08-07 14:28:50 -0700966 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700967 FLAGS_samples = 1;
968 FLAGS_gpuFrameLag = 0;
969 }
970
bsalomon6eb03cc2014-08-07 14:28:50 -0700971 if (!FLAGS_writePath.isEmpty()) {
972 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
973 if (!sk_mkdir(FLAGS_writePath[0])) {
974 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
975 FLAGS_writePath.set(0, NULL);
976 }
977 }
978
mtklein1915b622014-08-20 11:45:00 -0700979 SkAutoTDelete<ResultsWriter> log(SkNEW(ResultsWriter));
mtklein60317d0f2014-07-14 11:30:37 -0700980 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700981 log.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0])));
mtklein60317d0f2014-07-14 11:30:37 -0700982 }
mtklein1915b622014-08-20 11:45:00 -0700983
984 if (1 == FLAGS_properties.count() % 2) {
985 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
986 return 1;
987 }
988 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
989 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
990 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700991
992 if (1 == FLAGS_key.count() % 2) {
993 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
994 return 1;
995 }
996 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -0700997 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -0700998 }
mtklein60317d0f2014-07-14 11:30:37 -0700999
mtkleinf3723212014-06-25 14:08:00 -07001000 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001001 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001002
cdaltone1b89582015-06-25 19:17:08 -07001003 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001004
bsalomon6eb03cc2014-08-07 14:28:50 -07001005 if (kAutoTuneLoops != FLAGS_loops) {
1006 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleina189ccd2014-07-14 12:28:47 -07001007 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -07001008 // No header.
1009 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -07001010 SkDebugf("median\tbench\tconfig\n");
cdaltone1b89582015-06-25 19:17:08 -07001011 } else if (kTimedSampling == FLAGS_samples) {
1012 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001013 } else {
mtkleind75c4662015-04-30 07:11:22 -07001014 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001015 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001016 }
1017
bsalomonc2553372014-07-22 13:09:05 -07001018 SkTDArray<Config> configs;
1019 create_configs(&configs);
1020
mtkleine070c2b2014-10-14 08:40:43 -07001021 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001022 BenchmarkStream benchStream;
1023 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001024 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001025 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001026 continue;
1027 }
1028
mtkleinbb6a0282014-07-01 08:43:42 -07001029 SkTDArray<Target*> targets;
bsalomonc2553372014-07-22 13:09:05 -07001030 create_targets(&targets, bench.get(), configs);
mtkleinf3723212014-06-25 14:08:00 -07001031
jcgregoriobf5e5232014-07-17 13:14:16 -07001032 if (!targets.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -07001033 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
jcgregoriobf5e5232014-07-17 13:14:16 -07001034 bench->preDraw();
1035 }
mtkleinbb6a0282014-07-01 08:43:42 -07001036 for (int j = 0; j < targets.count(); j++) {
tomhudsond968a6f2015-03-26 11:28:06 -07001037 // During HWUI output this canvas may be NULL.
tomhudson75a0ebb2015-03-27 12:11:44 -07001038 SkCanvas* canvas = targets[j]->getCanvas();
bsalomonc2553372014-07-22 13:09:05 -07001039 const char* config = targets[j]->config.name;
mtkleinf3723212014-06-25 14:08:00 -07001040
tomhudsond968a6f2015-03-26 11:28:06 -07001041 targets[j]->setup();
robertphillips5b693772014-11-21 06:19:36 -08001042 bench->perCanvasPreDraw(canvas);
1043
cdaltone1b89582015-06-25 19:17:08 -07001044 int maxFrameLag;
1045 const int loops = targets[j]->needsFrameTiming(&maxFrameLag)
1046 ? setup_gpu_bench(targets[j], bench.get(), maxFrameLag)
1047 : setup_cpu_bench(overhead, targets[j], bench.get());
1048
1049 if (kTimedSampling != FLAGS_samples) {
1050 samples.reset(FLAGS_samples);
1051 for (int s = 0; s < FLAGS_samples; s++) {
1052 samples[s] = time(loops, bench, targets[j]) / loops;
1053 }
1054 } else if (samplingTimeMs) {
1055 samples.reset();
1056 if (FLAGS_verbose) {
1057 SkDebugf("Begin sampling %s for %ims\n",
1058 bench->getUniqueName(), static_cast<int>(samplingTimeMs));
1059 }
1060 WallTimer timer;
1061 timer.start();
1062 do {
1063 samples.push_back(time(loops, bench, targets[j]) / loops);
1064 timer.end();
1065 } while (timer.fWall < samplingTimeMs);
1066 }
mtkleinf3723212014-06-25 14:08:00 -07001067
robertphillips5b693772014-11-21 06:19:36 -08001068 bench->perCanvasPostDraw(canvas);
1069
tomhudsond968a6f2015-03-26 11:28:06 -07001070 if (Benchmark::kNonRendering_Backend != targets[j]->config.backend &&
1071 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001072 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001073 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001074 pngFilename.append(".png");
tomhudsond968a6f2015-03-26 11:28:06 -07001075 write_canvas_png(targets[j], pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001076 }
1077
1078 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001079 // Can't be timed. A warning note has already been printed.
Mike Kleine3631362014-07-15 17:56:37 -04001080 continue;
1081 }
1082
cdaltone1b89582015-06-25 19:17:08 -07001083 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001084 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001085 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001086 benchStream.fillCurrentOptions(log.get());
tomhudsond968a6f2015-03-26 11:28:06 -07001087 targets[j]->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001088 log->metric("min_ms", stats.min);
mtkleine070c2b2014-10-14 08:40:43 -07001089 if (runs++ % FLAGS_flushEvery == 0) {
1090 log->flush();
1091 }
mtklein60317d0f2014-07-14 11:30:37 -07001092
bsalomon6eb03cc2014-08-07 14:28:50 -07001093 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001094 if (targets.count() == 1) {
1095 config = ""; // Only print the config if we run the same bench on more than one.
1096 }
mtkleind75c4662015-04-30 07:11:22 -07001097 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1098 , sk_tools::getCurrResidentSetSizeMB()
1099 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001100 , bench->getUniqueName()
1101 , config);
mtkleina189ccd2014-07-14 12:28:47 -07001102 } else if (FLAGS_verbose) {
cdaltone1b89582015-06-25 19:17:08 -07001103 for (int i = 0; i < samples.count(); i++) {
mtklein55b0ffc2014-07-17 08:38:23 -07001104 SkDebugf("%s ", HUMANIZE(samples[i]));
mtkleinf3723212014-06-25 14:08:00 -07001105 }
mtklein96289052014-09-10 12:05:59 -07001106 SkDebugf("%s\n", bench->getUniqueName());
mtkleinf3723212014-06-25 14:08:00 -07001107 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -07001108 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -07001109 config = ""; // Only print the config if we run the same bench on more than one.
1110 }
mtklein96289052014-09-10 12:05:59 -07001111 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001112 } else {
1113 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001114 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1115 , sk_tools::getCurrResidentSetSizeMB()
1116 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001117 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001118 , HUMANIZE(stats.min)
1119 , HUMANIZE(stats.median)
1120 , HUMANIZE(stats.mean)
1121 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001122 , stddev_percent
cdaltone1b89582015-06-25 19:17:08 -07001123 , kTimedSampling != FLAGS_samples ? stats.plot.c_str()
1124 : to_string(samples.count()).c_str()
mtkleinf3723212014-06-25 14:08:00 -07001125 , config
mtklein96289052014-09-10 12:05:59 -07001126 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001127 );
1128 }
bsalomonb12ea412015-02-02 21:19:50 -08001129#if SK_SUPPORT_GPU
1130 if (FLAGS_gpuStats &&
bsalomon06cddec2014-10-24 10:40:50 -07001131 Benchmark::kGPU_Backend == targets[j]->config.backend) {
mtklein6838d852014-10-29 14:15:10 -07001132 gGrFactory->get(targets[j]->config.ctxType)->printCacheStats();
bsalomonb12ea412015-02-02 21:19:50 -08001133 gGrFactory->get(targets[j]->config.ctxType)->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001134 }
1135#endif
mtkleinf3723212014-06-25 14:08:00 -07001136 }
mtkleinbb6a0282014-07-01 08:43:42 -07001137 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -04001138
bsalomon06cddec2014-10-24 10:40:50 -07001139#if SK_SUPPORT_GPU
bsalomon2354f842014-07-28 13:48:36 -07001140 if (FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -07001141 gGrFactory->abandonContexts();
bsalomon2354f842014-07-28 13:48:36 -07001142 }
1143 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -07001144 gGrFactory->destroyContexts();
Mike Klein3944a1d2014-07-15 13:40:19 -04001145 }
bsalomon06cddec2014-10-24 10:40:50 -07001146#endif
mtkleinf3723212014-06-25 14:08:00 -07001147 }
1148
mtkleine1091452014-12-04 10:47:02 -08001149 log->bench("memory_usage", 0,0);
1150 log->config("meta");
1151 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1152
joshualitte0b19d42015-03-26 10:41:02 -07001153#if SK_SUPPORT_GPU
1154 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1155 // SkEventTracer destructor
1156 gGrFactory.reset(NULL);
1157#endif
1158
mtkleinf3723212014-06-25 14:08:00 -07001159 return 0;
1160}
1161
jcgregorio3b27ade2014-11-13 08:06:40 -08001162#if !defined SK_BUILD_FOR_IOS
1163int main(int argc, char** argv) {
1164 SkCommandLineFlags::Parse(argc, argv);
1165 return nanobench_main();
1166}
1167#endif