blob: 9f3887bc2060ac84140e80d46e4cd538b0b10c00 [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
reed53249782014-10-10 09:09:52 -070058static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070059
mtkleinb5110422014-08-07 15:20:02 -070060static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070061#ifdef SK_DEBUG
62 1;
mtkleina189ccd2014-07-14 12:28:47 -070063#else
bsalomon6eb03cc2014-08-07 14:28:50 -070064 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070065#endif
66
bsalomon6eb03cc2014-08-07 14:28:50 -070067static SkString loops_help_txt() {
68 SkString help;
69 help.printf("Number of times to run each bench. Set this to %d to auto-"
70 "tune for each bench. Timings are only reported when auto-tuning.",
71 kAutoTuneLoops);
72 return help;
73}
74
75DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
76
mtkleinf3723212014-06-25 14:08:00 -070077DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
78DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
79DEFINE_double(overheadGoal, 0.0001,
80 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070081DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -070082DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070083DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
84 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070085
mtklein60317d0f2014-07-14 11:30:37 -070086DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -070087DEFINE_int32(maxCalibrationAttempts, 3,
88 "Try up to this many times to guess loops for a bench, or skip the bench.");
89DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -070090DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
91DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
joshualitt261c3ad2015-04-27 09:16:57 -070092DEFINE_string(zoom, "1.0,1", "Comma-separated scale,step zoom factors for SKPs.");
mtklein20840502014-08-21 15:51:22 -070093DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
robertphillips5b693772014-11-21 06:19:36 -080094DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -070095DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -070096DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -080097DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -080098DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
mtklein92007582014-08-01 07:46:52 -070099
mtkleinf3723212014-06-25 14:08:00 -0700100static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700101 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800102 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700103}
mtklein55b0ffc2014-07-17 08:38:23 -0700104#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700105
tomhudsond968a6f2015-03-26 11:28:06 -0700106bool Target::init(SkImageInfo info, Benchmark* bench) {
107 if (Benchmark::kRaster_Backend == config.backend) {
108 this->surface.reset(SkSurface::NewRaster(info));
109 if (!this->surface.get()) {
110 return false;
111 }
112 }
113 return true;
114}
115bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700116 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700117 if (!canvas) {
118 return false;
119 }
120 bmp->setInfo(canvas->imageInfo());
121 if (!canvas->readPixels(bmp, 0, 0)) {
122 SkDebugf("Can't read canvas pixels.\n");
123 return false;
124 }
125 return true;
126}
127
128#if SK_SUPPORT_GPU
129struct GPUTarget : public Target {
130 explicit GPUTarget(const Config& c) : Target(c), gl(NULL) { }
131 SkGLContext* gl;
132
133 void setup() override {
134 this->gl->makeCurrent();
135 // Make sure we're done with whatever came before.
136 SK_GL(*this->gl, Finish());
137 }
138 void endTiming() override {
139 if (this->gl) {
140 SK_GL(*this->gl, Flush());
141 this->gl->swapBuffers();
142 }
143 }
144 void fence() override {
145 SK_GL(*this->gl, Finish());
146 }
mtkleind75c4662015-04-30 07:11:22 -0700147
cdaltond416a5b2015-06-23 13:23:44 -0700148 bool needsFrameTiming(int* maxFrameLag) const override {
149 if (!this->gl->getMaxGpuFrameLag(maxFrameLag)) {
150 // Frame lag is unknown.
151 *maxFrameLag = FLAGS_gpuFrameLag;
152 }
153 return true;
154 }
tomhudsond968a6f2015-03-26 11:28:06 -0700155 bool init(SkImageInfo info, Benchmark* bench) override {
156 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDistanceFieldFonts_Flag : 0;
157 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
158 this->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(this->config.ctxType),
159 SkSurface::kNo_Budgeted, info,
160 this->config.samples, &props));
161 this->gl = gGrFactory->getGLContext(this->config.ctxType);
162 if (!this->surface.get()) {
163 return false;
164 }
cdaltond416a5b2015-06-23 13:23:44 -0700165 if (!this->gl->fenceSyncSupport()) {
166 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
167 "Timings might not be accurate.\n", this->config.name);
168 }
tomhudsond968a6f2015-03-26 11:28:06 -0700169 return true;
170 }
171 void fillOptions(ResultsWriter* log) override {
172 const GrGLubyte* version;
173 SK_GL_RET(*this->gl, version, GetString(GR_GL_VERSION));
174 log->configOption("GL_VERSION", (const char*)(version));
175
176 SK_GL_RET(*this->gl, version, GetString(GR_GL_RENDERER));
177 log->configOption("GL_RENDERER", (const char*) version);
178
179 SK_GL_RET(*this->gl, version, GetString(GR_GL_VENDOR));
180 log->configOption("GL_VENDOR", (const char*) version);
181
182 SK_GL_RET(*this->gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
183 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
184 }
185};
mtkleind75c4662015-04-30 07:11:22 -0700186
tomhudsond968a6f2015-03-26 11:28:06 -0700187#endif
188
tomhudson75a0ebb2015-03-27 12:11:44 -0700189static double time(int loops, Benchmark* bench, Target* target) {
190 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700191 if (canvas) {
192 canvas->clear(SK_ColorWHITE);
193 }
mtkleinbb6a0282014-07-01 08:43:42 -0700194 WallTimer timer;
195 timer.start();
tomhudson75a0ebb2015-03-27 12:11:44 -0700196 canvas = target->beginTiming(canvas);
197 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700198 if (canvas) {
199 canvas->flush();
200 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700201 target->endTiming();
mtkleinbb6a0282014-07-01 08:43:42 -0700202 timer.end();
203 return timer.fWall;
204}
205
mtkleinf3723212014-06-25 14:08:00 -0700206static double estimate_timer_overhead() {
207 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700208 for (int i = 0; i < FLAGS_overheadLoops; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700209 WallTimer timer;
210 timer.start();
211 timer.end();
212 overhead += timer.fWall;
mtkleinf3723212014-06-25 14:08:00 -0700213 }
214 return overhead / FLAGS_overheadLoops;
215}
216
reed53249782014-10-10 09:09:52 -0700217static int detect_forever_loops(int loops) {
218 // look for a magic run-forever value
219 if (loops < 0) {
220 loops = SK_MaxS32;
221 }
222 return loops;
223}
224
mtklein55b0ffc2014-07-17 08:38:23 -0700225static int clamp_loops(int loops) {
226 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800227 SkDebugf("ERROR: clamping loops from %d to 1. "
228 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700229 return 1;
230 }
231 if (loops > FLAGS_maxLoops) {
232 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
233 return FLAGS_maxLoops;
234 }
235 return loops;
236}
237
tomhudsond968a6f2015-03-26 11:28:06 -0700238static bool write_canvas_png(Target* target, const SkString& filename) {
239
bsalomon6eb03cc2014-08-07 14:28:50 -0700240 if (filename.isEmpty()) {
241 return false;
242 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700243 if (target->getCanvas() &&
244 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700245 return false;
246 }
tomhudsond968a6f2015-03-26 11:28:06 -0700247
bsalomon6eb03cc2014-08-07 14:28:50 -0700248 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700249
250 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700251 return false;
252 }
tomhudsond968a6f2015-03-26 11:28:06 -0700253
bsalomon6eb03cc2014-08-07 14:28:50 -0700254 SkString dir = SkOSPath::Dirname(filename.c_str());
255 if (!sk_mkdir(dir.c_str())) {
256 SkDebugf("Can't make dir %s.\n", dir.c_str());
257 return false;
258 }
259 SkFILEWStream stream(filename.c_str());
260 if (!stream.isValid()) {
261 SkDebugf("Can't write %s.\n", filename.c_str());
262 return false;
263 }
264 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
265 SkDebugf("Can't encode a PNG.\n");
266 return false;
267 }
268 return true;
269}
270
271static int kFailedLoops = -2;
tomhudson75a0ebb2015-03-27 12:11:44 -0700272static int cpu_bench(const double overhead, Target* target, Benchmark* bench, double* samples) {
mtkleinbb6a0282014-07-01 08:43:42 -0700273 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700274 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700275 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700276 int loops = bench->calculateLoops(FLAGS_loops);
277 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700278 while (bench_plus_overhead < overhead) {
279 if (round++ == FLAGS_maxCalibrationAttempts) {
280 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700281 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700282 return kFailedLoops;
283 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700284 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700285 }
mtklein2069e222014-08-04 13:57:39 -0700286 }
mtkleinf3723212014-06-25 14:08:00 -0700287
mtkleinbb6a0282014-07-01 08:43:42 -0700288 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700289 // We'll pick N to make timer overhead negligible:
290 //
mtkleinbb6a0282014-07-01 08:43:42 -0700291 // overhead
292 // ------------------------- < FLAGS_overheadGoal
293 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700294 //
mtkleinbb6a0282014-07-01 08:43:42 -0700295 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700296 //
297 // Doing some math, we get:
298 //
mtkleinbb6a0282014-07-01 08:43:42 -0700299 // (overhead / FLAGS_overheadGoal) - overhead
300 // ------------------------------------------ < N
301 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700302 //
303 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700304 if (kAutoTuneLoops == loops) {
305 const double numer = overhead / FLAGS_overheadGoal - overhead;
306 const double denom = bench_plus_overhead - overhead;
307 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700308 loops = clamp_loops(loops);
309 } else {
310 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700311 }
mtkleinbb6a0282014-07-01 08:43:42 -0700312
313 for (int i = 0; i < FLAGS_samples; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700314 samples[i] = time(loops, bench, target) / loops;
mtkleinbb6a0282014-07-01 08:43:42 -0700315 }
316 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700317}
318
tomhudsond968a6f2015-03-26 11:28:06 -0700319static int gpu_bench(Target* target,
mtkleinbb6a0282014-07-01 08:43:42 -0700320 Benchmark* bench,
cdaltond416a5b2015-06-23 13:23:44 -0700321 double* samples,
322 int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700323 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700324 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700325 if (kAutoTuneLoops == loops) {
326 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700327 double elapsed = 0;
328 do {
mtklein527930f2014-11-06 08:04:34 -0800329 if (1<<30 == loops) {
330 // We're about to wrap. Something's wrong with the bench.
331 loops = 0;
332 break;
333 }
mtkleina189ccd2014-07-14 12:28:47 -0700334 loops *= 2;
335 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700336 // _this_ round, not still timing last round.
337 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700338 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700339 }
340 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700341
mtkleina189ccd2014-07-14 12:28:47 -0700342 // We've overshot at least a little. Scale back linearly.
343 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700344 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700345
tomhudsond968a6f2015-03-26 11:28:06 -0700346 // Make sure we're not still timing our calibration.
347 target->fence();
reed53249782014-10-10 09:09:52 -0700348 } else {
349 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700350 }
mtkleinbb6a0282014-07-01 08:43:42 -0700351
352 // Pretty much the same deal as the calibration: do some warmup to make
353 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700354 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700355 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700356 }
mtkleinbb6a0282014-07-01 08:43:42 -0700357
358 // Now, actually do the timing!
359 for (int i = 0; i < FLAGS_samples; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700360 samples[i] = time(loops, bench, target) / loops;
mtkleinbb6a0282014-07-01 08:43:42 -0700361 }
tomhudsond968a6f2015-03-26 11:28:06 -0700362
mtkleinbb6a0282014-07-01 08:43:42 -0700363 return loops;
364}
mtkleinbb6a0282014-07-01 08:43:42 -0700365
366static SkString to_lower(const char* str) {
367 SkString lower(str);
368 for (size_t i = 0; i < lower.size(); i++) {
369 lower[i] = tolower(lower[i]);
370 }
371 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700372}
373
bsalomonc2553372014-07-22 13:09:05 -0700374static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700375 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700376 if (to_lower(FLAGS_config[i]).equals(name)) {
377 return true;
mtkleinf3723212014-06-25 14:08:00 -0700378 }
379 }
bsalomonc2553372014-07-22 13:09:05 -0700380 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700381}
382
bsalomonc2553372014-07-22 13:09:05 -0700383#if SK_SUPPORT_GPU
384static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
385 int sampleCnt) {
386 if (!is_cpu_config_allowed(name)) {
387 return false;
388 }
krajcevski69a55602014-08-13 10:46:31 -0700389 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomon76228632015-05-29 08:02:10 -0700390 return sampleCnt <= ctx->caps()->maxSampleCount();
bsalomonc2553372014-07-22 13:09:05 -0700391 }
392 return false;
393}
394#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700395
bsalomonc2553372014-07-22 13:09:05 -0700396#if SK_SUPPORT_GPU
397#define kBogusGLContextType GrContextFactory::kNative_GLContextType
398#else
399#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700400#endif
bsalomonc2553372014-07-22 13:09:05 -0700401
402// Append all configs that are enabled and supported.
403static void create_configs(SkTDArray<Config>* configs) {
jvanverth4736e142014-11-07 07:12:46 -0800404 #define CPU_CONFIG(name, backend, color, alpha) \
405 if (is_cpu_config_allowed(#name)) { \
406 Config config = { #name, Benchmark::backend, color, alpha, 0, \
407 kBogusGLContextType, false }; \
408 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700409 }
mtkleine714e752014-07-31 12:13:48 -0700410
mtklein40b32be2014-07-09 08:46:49 -0700411 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700412 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
413 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
414 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700415 }
mtkleinbb6a0282014-07-01 08:43:42 -0700416
417#if SK_SUPPORT_GPU
jvanverth4736e142014-11-07 07:12:46 -0800418 #define GPU_CONFIG(name, ctxType, samples, useDFText) \
bsalomonc2553372014-07-22 13:09:05 -0700419 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
420 Config config = { \
421 #name, \
422 Benchmark::kGPU_Backend, \
423 kN32_SkColorType, \
424 kPremul_SkAlphaType, \
425 samples, \
jvanverth4736e142014-11-07 07:12:46 -0800426 GrContextFactory::ctxType, \
427 useDFText }; \
bsalomonc2553372014-07-22 13:09:05 -0700428 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700429 }
mtkleine714e752014-07-31 12:13:48 -0700430
mtklein40b32be2014-07-09 08:46:49 -0700431 if (FLAGS_gpu) {
jvanverth4736e142014-11-07 07:12:46 -0800432 GPU_CONFIG(gpu, kNative_GLContextType, 0, false)
433 GPU_CONFIG(msaa4, kNative_GLContextType, 4, false)
434 GPU_CONFIG(msaa16, kNative_GLContextType, 16, false)
435 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4, false)
436 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16, false)
437 GPU_CONFIG(gpudft, kNative_GLContextType, 0, true)
438 GPU_CONFIG(debug, kDebug_GLContextType, 0, false)
439 GPU_CONFIG(nullgpu, kNull_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700440#ifdef SK_ANGLE
jvanverth4736e142014-11-07 07:12:46 -0800441 GPU_CONFIG(angle, kANGLE_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700442#endif
cdaltond416a5b2015-06-23 13:23:44 -0700443#if SK_MESA
444 GPU_CONFIG(mesa, kMESA_GLContextType, 0, false)
445#endif
mtklein40b32be2014-07-09 08:46:49 -0700446 }
mtkleinbb6a0282014-07-01 08:43:42 -0700447#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700448
449#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
450 if (is_cpu_config_allowed("hwui")) {
451 Config config = { "hwui", Benchmark::kHWUI_Backend, kRGBA_8888_SkColorType,
452 kPremul_SkAlphaType, 0, kBogusGLContextType, false };
453 configs->push(config);
454 }
455#endif
mtkleinf3723212014-06-25 14:08:00 -0700456}
457
bsalomonc2553372014-07-22 13:09:05 -0700458// If bench is enabled for config, returns a Target* for it, otherwise NULL.
459static Target* is_enabled(Benchmark* bench, const Config& config) {
460 if (!bench->isSuitableFor(config.backend)) {
461 return NULL;
462 }
463
reede5ea5002014-09-03 11:54:58 -0700464 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
465 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700466
tomhudsond968a6f2015-03-26 11:28:06 -0700467 Target* target = NULL;
bsalomonc2553372014-07-22 13:09:05 -0700468
tomhudsond968a6f2015-03-26 11:28:06 -0700469 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700470#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700471 case Benchmark::kGPU_Backend:
472 target = new GPUTarget(config);
473 break;
bsalomonc2553372014-07-22 13:09:05 -0700474#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700475#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
476 case Benchmark::kHWUI_Backend:
477 target = new HWUITarget(config, bench);
478 break;
479#endif
480 default:
481 target = new Target(config);
482 break;
483 }
bsalomonc2553372014-07-22 13:09:05 -0700484
tomhudsond968a6f2015-03-26 11:28:06 -0700485 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700486 delete target;
487 return NULL;
488 }
489 return target;
490}
491
492// Creates targets for a benchmark and a set of configs.
493static void create_targets(SkTDArray<Target*>* targets, Benchmark* b,
494 const SkTDArray<Config>& configs) {
495 for (int i = 0; i < configs.count(); ++i) {
496 if (Target* t = is_enabled(b, configs[i])) {
497 targets->push(t);
498 }
mtkleine714e752014-07-31 12:13:48 -0700499
bsalomonc2553372014-07-22 13:09:05 -0700500 }
501}
502
msarettb23e6aa2015-06-09 13:56:10 -0700503/*
504 * Returns true if set up for a subset decode succeeds, false otherwise
505 * If the set-up succeeds, the width and height parameters will be set
506 */
507static bool valid_subset_bench(const SkString& path, SkColorType colorType, bool useCodec,
508 int* width, int* height) {
509 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
510 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded));
511
msarettab80e352015-06-17 10:28:22 -0700512 // Check that we can create a codec or image decoder.
msarettb23e6aa2015-06-09 13:56:10 -0700513 if (useCodec) {
514 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
515 if (NULL == codec) {
516 SkDebugf("Could not create codec for %s. Skipping bench.\n", path.c_str());
517 return false;
518 }
519
520 // These will be initialized by SkCodec if the color type is kIndex8 and
521 // unused otherwise.
522 SkPMColor colors[256];
523 int colorCount;
524 const SkImageInfo info = codec->getInfo().makeColorType(colorType);
525 SkAutoTDeleteArray<uint8_t> row(SkNEW_ARRAY(uint8_t, info.minRowBytes()));
526 SkScanlineDecoder* scanlineDecoder = codec->getScanlineDecoder(info, NULL,
527 colors, &colorCount);
528 if (NULL == scanlineDecoder) {
529 SkDebugf("Could not create scanline decoder for %s with color type %s. "
530 "Skipping bench.\n", path.c_str(), get_color_name(colorType));
531 return false;
532 }
533 *width = info.width();
534 *height = info.height();
535 } else {
536 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream));
537 if (NULL == decoder) {
538 SkDebugf("Could not create decoder for %s. Skipping bench.\n", path.c_str());
539 return false;
540 }
541 //FIXME: See skbug.com/3921
542 if (kIndex_8_SkColorType == colorType || kGray_8_SkColorType == colorType) {
543 SkDebugf("Cannot use image subset decoder for %s with color type %s. "
544 "Skipping bench.\n", path.c_str(), get_color_name(colorType));
545 return false;
546 }
547 if (!decoder->buildTileIndex(stream.detach(), width, height)) {
548 SkDebugf("Could not build tile index for %s. Skipping bench.\n", path.c_str());
549 return false;
550 }
551 }
msarettab80e352015-06-17 10:28:22 -0700552
553 // Check if the image is large enough for a meaningful subset benchmark.
554 if (*width <= 512 && *height <= 512) {
555 // This should not print a message since it is not an error.
556 return false;
557 }
558
msarettb23e6aa2015-06-09 13:56:10 -0700559 return true;
560}
jcgregoriobf5e5232014-07-17 13:14:16 -0700561
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
bsalomon6eb03cc2014-08-07 14:28:50 -0700949 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700950 FLAGS_samples = 1;
951 FLAGS_gpuFrameLag = 0;
952 }
953
bsalomon6eb03cc2014-08-07 14:28:50 -0700954 if (!FLAGS_writePath.isEmpty()) {
955 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
956 if (!sk_mkdir(FLAGS_writePath[0])) {
957 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
958 FLAGS_writePath.set(0, NULL);
959 }
960 }
961
mtklein1915b622014-08-20 11:45:00 -0700962 SkAutoTDelete<ResultsWriter> log(SkNEW(ResultsWriter));
mtklein60317d0f2014-07-14 11:30:37 -0700963 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700964 log.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0])));
mtklein60317d0f2014-07-14 11:30:37 -0700965 }
mtklein1915b622014-08-20 11:45:00 -0700966
967 if (1 == FLAGS_properties.count() % 2) {
968 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
969 return 1;
970 }
971 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
972 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
973 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700974
975 if (1 == FLAGS_key.count() % 2) {
976 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
977 return 1;
978 }
979 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -0700980 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -0700981 }
mtklein60317d0f2014-07-14 11:30:37 -0700982
mtkleinf3723212014-06-25 14:08:00 -0700983 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -0700984 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -0400985
mtkleinbb6a0282014-07-01 08:43:42 -0700986 SkAutoTMalloc<double> samples(FLAGS_samples);
987
bsalomon6eb03cc2014-08-07 14:28:50 -0700988 if (kAutoTuneLoops != FLAGS_loops) {
989 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleina189ccd2014-07-14 12:28:47 -0700990 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700991 // No header.
992 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700993 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700994 } else {
mtkleind75c4662015-04-30 07:11:22 -0700995 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -0700996 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -0700997 }
998
bsalomonc2553372014-07-22 13:09:05 -0700999 SkTDArray<Config> configs;
1000 create_configs(&configs);
1001
mtkleine070c2b2014-10-14 08:40:43 -07001002 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001003 BenchmarkStream benchStream;
1004 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001005 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001006 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001007 continue;
1008 }
1009
mtkleinbb6a0282014-07-01 08:43:42 -07001010 SkTDArray<Target*> targets;
bsalomonc2553372014-07-22 13:09:05 -07001011 create_targets(&targets, bench.get(), configs);
mtkleinf3723212014-06-25 14:08:00 -07001012
jcgregoriobf5e5232014-07-17 13:14:16 -07001013 if (!targets.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -07001014 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
jcgregoriobf5e5232014-07-17 13:14:16 -07001015 bench->preDraw();
1016 }
mtkleinbb6a0282014-07-01 08:43:42 -07001017 for (int j = 0; j < targets.count(); j++) {
tomhudsond968a6f2015-03-26 11:28:06 -07001018 // During HWUI output this canvas may be NULL.
tomhudson75a0ebb2015-03-27 12:11:44 -07001019 SkCanvas* canvas = targets[j]->getCanvas();
bsalomonc2553372014-07-22 13:09:05 -07001020 const char* config = targets[j]->config.name;
mtkleinf3723212014-06-25 14:08:00 -07001021
tomhudsond968a6f2015-03-26 11:28:06 -07001022 targets[j]->setup();
robertphillips5b693772014-11-21 06:19:36 -08001023 bench->perCanvasPreDraw(canvas);
1024
cdaltond416a5b2015-06-23 13:23:44 -07001025 int frameLag;
mtkleinbb6a0282014-07-01 08:43:42 -07001026 const int loops =
cdaltond416a5b2015-06-23 13:23:44 -07001027 targets[j]->needsFrameTiming(&frameLag)
1028 ? gpu_bench(targets[j], bench.get(), samples.get(), frameLag)
tomhudson75a0ebb2015-03-27 12:11:44 -07001029 : cpu_bench(overhead, targets[j], bench.get(), samples.get());
mtkleinf3723212014-06-25 14:08:00 -07001030
robertphillips5b693772014-11-21 06:19:36 -08001031 bench->perCanvasPostDraw(canvas);
1032
tomhudsond968a6f2015-03-26 11:28:06 -07001033 if (Benchmark::kNonRendering_Backend != targets[j]->config.backend &&
1034 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001035 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001036 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001037 pngFilename.append(".png");
tomhudsond968a6f2015-03-26 11:28:06 -07001038 write_canvas_png(targets[j], pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001039 }
1040
1041 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001042 // Can't be timed. A warning note has already been printed.
Mike Kleine3631362014-07-15 17:56:37 -04001043 continue;
1044 }
1045
mtkleinf3723212014-06-25 14:08:00 -07001046 Stats stats(samples.get(), FLAGS_samples);
mtklein1915b622014-08-20 11:45:00 -07001047 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001048 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001049 benchStream.fillCurrentOptions(log.get());
tomhudsond968a6f2015-03-26 11:28:06 -07001050 targets[j]->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001051 log->metric("min_ms", stats.min);
mtkleine070c2b2014-10-14 08:40:43 -07001052 if (runs++ % FLAGS_flushEvery == 0) {
1053 log->flush();
1054 }
mtklein60317d0f2014-07-14 11:30:37 -07001055
bsalomon6eb03cc2014-08-07 14:28:50 -07001056 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001057 if (targets.count() == 1) {
1058 config = ""; // Only print the config if we run the same bench on more than one.
1059 }
mtkleind75c4662015-04-30 07:11:22 -07001060 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1061 , sk_tools::getCurrResidentSetSizeMB()
1062 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001063 , bench->getUniqueName()
1064 , config);
mtkleina189ccd2014-07-14 12:28:47 -07001065 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -07001066 for (int i = 0; i < FLAGS_samples; i++) {
mtklein55b0ffc2014-07-17 08:38:23 -07001067 SkDebugf("%s ", HUMANIZE(samples[i]));
mtkleinf3723212014-06-25 14:08:00 -07001068 }
mtklein96289052014-09-10 12:05:59 -07001069 SkDebugf("%s\n", bench->getUniqueName());
mtkleinf3723212014-06-25 14:08:00 -07001070 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -07001071 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -07001072 config = ""; // Only print the config if we run the same bench on more than one.
1073 }
mtklein96289052014-09-10 12:05:59 -07001074 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001075 } else {
1076 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001077 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1078 , sk_tools::getCurrResidentSetSizeMB()
1079 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001080 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001081 , HUMANIZE(stats.min)
1082 , HUMANIZE(stats.median)
1083 , HUMANIZE(stats.mean)
1084 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001085 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -07001086 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001087 , config
mtklein96289052014-09-10 12:05:59 -07001088 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001089 );
1090 }
bsalomonb12ea412015-02-02 21:19:50 -08001091#if SK_SUPPORT_GPU
1092 if (FLAGS_gpuStats &&
bsalomon06cddec2014-10-24 10:40:50 -07001093 Benchmark::kGPU_Backend == targets[j]->config.backend) {
mtklein6838d852014-10-29 14:15:10 -07001094 gGrFactory->get(targets[j]->config.ctxType)->printCacheStats();
bsalomonb12ea412015-02-02 21:19:50 -08001095 gGrFactory->get(targets[j]->config.ctxType)->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001096 }
1097#endif
mtkleinf3723212014-06-25 14:08:00 -07001098 }
mtkleinbb6a0282014-07-01 08:43:42 -07001099 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -04001100
bsalomon06cddec2014-10-24 10:40:50 -07001101#if SK_SUPPORT_GPU
bsalomon2354f842014-07-28 13:48:36 -07001102 if (FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -07001103 gGrFactory->abandonContexts();
bsalomon2354f842014-07-28 13:48:36 -07001104 }
1105 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -07001106 gGrFactory->destroyContexts();
Mike Klein3944a1d2014-07-15 13:40:19 -04001107 }
bsalomon06cddec2014-10-24 10:40:50 -07001108#endif
mtkleinf3723212014-06-25 14:08:00 -07001109 }
1110
mtkleine1091452014-12-04 10:47:02 -08001111 log->bench("memory_usage", 0,0);
1112 log->config("meta");
1113 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1114
joshualitte0b19d42015-03-26 10:41:02 -07001115#if SK_SUPPORT_GPU
1116 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1117 // SkEventTracer destructor
1118 gGrFactory.reset(NULL);
1119#endif
1120
mtkleinf3723212014-06-25 14:08:00 -07001121 return 0;
1122}
1123
jcgregorio3b27ade2014-11-13 08:06:40 -08001124#if !defined SK_BUILD_FOR_IOS
1125int main(int argc, char** argv) {
1126 SkCommandLineFlags::Parse(argc, argv);
1127 return nanobench_main();
1128}
1129#endif