blob: 13ae9f6239aff951d5a5ca4a4aded236e9b4020f [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?");
mtkleine070c2b2014-10-14 08:40:43 -070095DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -080096DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -080097DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
mtklein92007582014-08-01 07:46:52 -070098
mtkleinf3723212014-06-25 14:08:00 -070099static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700100 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800101 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700102}
mtklein55b0ffc2014-07-17 08:38:23 -0700103#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700104
tomhudsond968a6f2015-03-26 11:28:06 -0700105bool Target::init(SkImageInfo info, Benchmark* bench) {
106 if (Benchmark::kRaster_Backend == config.backend) {
107 this->surface.reset(SkSurface::NewRaster(info));
108 if (!this->surface.get()) {
109 return false;
110 }
111 }
112 return true;
113}
114bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700115 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700116 if (!canvas) {
117 return false;
118 }
119 bmp->setInfo(canvas->imageInfo());
120 if (!canvas->readPixels(bmp, 0, 0)) {
121 SkDebugf("Can't read canvas pixels.\n");
122 return false;
123 }
124 return true;
125}
126
127#if SK_SUPPORT_GPU
128struct GPUTarget : public Target {
129 explicit GPUTarget(const Config& c) : Target(c), gl(NULL) { }
130 SkGLContext* gl;
131
132 void setup() override {
133 this->gl->makeCurrent();
134 // Make sure we're done with whatever came before.
135 SK_GL(*this->gl, Finish());
136 }
137 void endTiming() override {
138 if (this->gl) {
139 SK_GL(*this->gl, Flush());
140 this->gl->swapBuffers();
141 }
142 }
143 void fence() override {
144 SK_GL(*this->gl, Finish());
145 }
mtkleind75c4662015-04-30 07:11:22 -0700146
cdaltond416a5b2015-06-23 13:23:44 -0700147 bool needsFrameTiming(int* maxFrameLag) const override {
148 if (!this->gl->getMaxGpuFrameLag(maxFrameLag)) {
149 // Frame lag is unknown.
150 *maxFrameLag = FLAGS_gpuFrameLag;
151 }
152 return true;
153 }
tomhudsond968a6f2015-03-26 11:28:06 -0700154 bool init(SkImageInfo info, Benchmark* bench) override {
155 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDistanceFieldFonts_Flag : 0;
156 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
157 this->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(this->config.ctxType),
158 SkSurface::kNo_Budgeted, info,
159 this->config.samples, &props));
160 this->gl = gGrFactory->getGLContext(this->config.ctxType);
161 if (!this->surface.get()) {
162 return false;
163 }
cdaltond416a5b2015-06-23 13:23:44 -0700164 if (!this->gl->fenceSyncSupport()) {
165 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
166 "Timings might not be accurate.\n", this->config.name);
167 }
tomhudsond968a6f2015-03-26 11:28:06 -0700168 return true;
169 }
170 void fillOptions(ResultsWriter* log) override {
171 const GrGLubyte* version;
172 SK_GL_RET(*this->gl, version, GetString(GR_GL_VERSION));
173 log->configOption("GL_VERSION", (const char*)(version));
174
175 SK_GL_RET(*this->gl, version, GetString(GR_GL_RENDERER));
176 log->configOption("GL_RENDERER", (const char*) version);
177
178 SK_GL_RET(*this->gl, version, GetString(GR_GL_VENDOR));
179 log->configOption("GL_VENDOR", (const char*) version);
180
181 SK_GL_RET(*this->gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
182 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
183 }
184};
mtkleind75c4662015-04-30 07:11:22 -0700185
tomhudsond968a6f2015-03-26 11:28:06 -0700186#endif
187
tomhudson75a0ebb2015-03-27 12:11:44 -0700188static double time(int loops, Benchmark* bench, Target* target) {
189 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700190 if (canvas) {
191 canvas->clear(SK_ColorWHITE);
192 }
mtkleinbb6a0282014-07-01 08:43:42 -0700193 WallTimer timer;
194 timer.start();
tomhudson75a0ebb2015-03-27 12:11:44 -0700195 canvas = target->beginTiming(canvas);
196 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700197 if (canvas) {
198 canvas->flush();
199 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700200 target->endTiming();
mtkleinbb6a0282014-07-01 08:43:42 -0700201 timer.end();
202 return timer.fWall;
203}
204
mtkleinf3723212014-06-25 14:08:00 -0700205static double estimate_timer_overhead() {
206 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700207 for (int i = 0; i < FLAGS_overheadLoops; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700208 WallTimer timer;
209 timer.start();
210 timer.end();
211 overhead += timer.fWall;
mtkleinf3723212014-06-25 14:08:00 -0700212 }
213 return overhead / FLAGS_overheadLoops;
214}
215
reed53249782014-10-10 09:09:52 -0700216static int detect_forever_loops(int loops) {
217 // look for a magic run-forever value
218 if (loops < 0) {
219 loops = SK_MaxS32;
220 }
221 return loops;
222}
223
mtklein55b0ffc2014-07-17 08:38:23 -0700224static int clamp_loops(int loops) {
225 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800226 SkDebugf("ERROR: clamping loops from %d to 1. "
227 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700228 return 1;
229 }
230 if (loops > FLAGS_maxLoops) {
231 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
232 return FLAGS_maxLoops;
233 }
234 return loops;
235}
236
tomhudsond968a6f2015-03-26 11:28:06 -0700237static bool write_canvas_png(Target* target, const SkString& filename) {
238
bsalomon6eb03cc2014-08-07 14:28:50 -0700239 if (filename.isEmpty()) {
240 return false;
241 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700242 if (target->getCanvas() &&
243 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700244 return false;
245 }
tomhudsond968a6f2015-03-26 11:28:06 -0700246
bsalomon6eb03cc2014-08-07 14:28:50 -0700247 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700248
249 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700250 return false;
251 }
tomhudsond968a6f2015-03-26 11:28:06 -0700252
bsalomon6eb03cc2014-08-07 14:28:50 -0700253 SkString dir = SkOSPath::Dirname(filename.c_str());
254 if (!sk_mkdir(dir.c_str())) {
255 SkDebugf("Can't make dir %s.\n", dir.c_str());
256 return false;
257 }
258 SkFILEWStream stream(filename.c_str());
259 if (!stream.isValid()) {
260 SkDebugf("Can't write %s.\n", filename.c_str());
261 return false;
262 }
263 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
264 SkDebugf("Can't encode a PNG.\n");
265 return false;
266 }
267 return true;
268}
269
270static int kFailedLoops = -2;
tomhudson75a0ebb2015-03-27 12:11:44 -0700271static int cpu_bench(const double overhead, Target* target, Benchmark* bench, double* samples) {
mtkleinbb6a0282014-07-01 08:43:42 -0700272 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700273 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700274 int round = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -0700275 if (kAutoTuneLoops == FLAGS_loops) {
276 while (bench_plus_overhead < overhead) {
277 if (round++ == FLAGS_maxCalibrationAttempts) {
278 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700279 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700280 return kFailedLoops;
281 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700282 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700283 }
mtklein2069e222014-08-04 13:57:39 -0700284 }
mtkleinf3723212014-06-25 14:08:00 -0700285
mtkleinbb6a0282014-07-01 08:43:42 -0700286 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700287 // We'll pick N to make timer overhead negligible:
288 //
mtkleinbb6a0282014-07-01 08:43:42 -0700289 // overhead
290 // ------------------------- < FLAGS_overheadGoal
291 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700292 //
mtkleinbb6a0282014-07-01 08:43:42 -0700293 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700294 //
295 // Doing some math, we get:
296 //
mtkleinbb6a0282014-07-01 08:43:42 -0700297 // (overhead / FLAGS_overheadGoal) - overhead
298 // ------------------------------------------ < N
299 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700300 //
301 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700302 int loops = FLAGS_loops;
303 if (kAutoTuneLoops == loops) {
304 const double numer = overhead / FLAGS_overheadGoal - overhead;
305 const double denom = bench_plus_overhead - overhead;
306 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700307 loops = clamp_loops(loops);
308 } else {
309 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700310 }
mtkleinbb6a0282014-07-01 08:43:42 -0700311
312 for (int i = 0; i < FLAGS_samples; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700313 samples[i] = time(loops, bench, target) / loops;
mtkleinbb6a0282014-07-01 08:43:42 -0700314 }
315 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700316}
317
tomhudsond968a6f2015-03-26 11:28:06 -0700318static int gpu_bench(Target* target,
mtkleinbb6a0282014-07-01 08:43:42 -0700319 Benchmark* bench,
cdaltond416a5b2015-06-23 13:23:44 -0700320 double* samples,
321 int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700322 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
bsalomon6eb03cc2014-08-07 14:28:50 -0700323 int loops = FLAGS_loops;
324 if (kAutoTuneLoops == loops) {
325 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700326 double elapsed = 0;
327 do {
mtklein527930f2014-11-06 08:04:34 -0800328 if (1<<30 == loops) {
329 // We're about to wrap. Something's wrong with the bench.
330 loops = 0;
331 break;
332 }
mtkleina189ccd2014-07-14 12:28:47 -0700333 loops *= 2;
334 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700335 // _this_ round, not still timing last round.
336 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700337 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700338 }
339 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700340
mtkleina189ccd2014-07-14 12:28:47 -0700341 // We've overshot at least a little. Scale back linearly.
342 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700343 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700344
tomhudsond968a6f2015-03-26 11:28:06 -0700345 // Make sure we're not still timing our calibration.
346 target->fence();
reed53249782014-10-10 09:09:52 -0700347 } else {
348 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700349 }
mtkleinbb6a0282014-07-01 08:43:42 -0700350
351 // Pretty much the same deal as the calibration: do some warmup to make
352 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700353 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700354 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700355 }
mtkleinbb6a0282014-07-01 08:43:42 -0700356
357 // Now, actually do the timing!
358 for (int i = 0; i < FLAGS_samples; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700359 samples[i] = time(loops, bench, target) / loops;
mtkleinbb6a0282014-07-01 08:43:42 -0700360 }
tomhudsond968a6f2015-03-26 11:28:06 -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,
joshualitt261c3ad2015-04-27 09:16:57 -0700718 (name.c_str(), pic.get(), fClip,
719 fScales[fCurrentScale], fUseMPDs[fCurrentUseMPD++]));
720
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,
744 fZoomSteps));
745 }
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
bsalomon6eb03cc2014-08-07 14:28:50 -0700948 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700949 FLAGS_samples = 1;
950 FLAGS_gpuFrameLag = 0;
951 }
952
bsalomon6eb03cc2014-08-07 14:28:50 -0700953 if (!FLAGS_writePath.isEmpty()) {
954 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
955 if (!sk_mkdir(FLAGS_writePath[0])) {
956 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
957 FLAGS_writePath.set(0, NULL);
958 }
959 }
960
mtklein1915b622014-08-20 11:45:00 -0700961 SkAutoTDelete<ResultsWriter> log(SkNEW(ResultsWriter));
mtklein60317d0f2014-07-14 11:30:37 -0700962 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700963 log.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0])));
mtklein60317d0f2014-07-14 11:30:37 -0700964 }
mtklein1915b622014-08-20 11:45:00 -0700965
966 if (1 == FLAGS_properties.count() % 2) {
967 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
968 return 1;
969 }
970 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
971 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
972 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700973
974 if (1 == FLAGS_key.count() % 2) {
975 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
976 return 1;
977 }
978 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -0700979 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -0700980 }
mtklein60317d0f2014-07-14 11:30:37 -0700981
mtkleinf3723212014-06-25 14:08:00 -0700982 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -0700983 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -0400984
mtkleinbb6a0282014-07-01 08:43:42 -0700985 SkAutoTMalloc<double> samples(FLAGS_samples);
986
bsalomon6eb03cc2014-08-07 14:28:50 -0700987 if (kAutoTuneLoops != FLAGS_loops) {
988 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleina189ccd2014-07-14 12:28:47 -0700989 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700990 // No header.
991 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700992 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700993 } else {
mtkleind75c4662015-04-30 07:11:22 -0700994 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -0700995 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -0700996 }
997
bsalomonc2553372014-07-22 13:09:05 -0700998 SkTDArray<Config> configs;
999 create_configs(&configs);
1000
mtkleine070c2b2014-10-14 08:40:43 -07001001 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001002 BenchmarkStream benchStream;
1003 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001004 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001005 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001006 continue;
1007 }
1008
mtkleinbb6a0282014-07-01 08:43:42 -07001009 SkTDArray<Target*> targets;
bsalomonc2553372014-07-22 13:09:05 -07001010 create_targets(&targets, bench.get(), configs);
mtkleinf3723212014-06-25 14:08:00 -07001011
jcgregoriobf5e5232014-07-17 13:14:16 -07001012 if (!targets.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -07001013 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
jcgregoriobf5e5232014-07-17 13:14:16 -07001014 bench->preDraw();
1015 }
mtkleinbb6a0282014-07-01 08:43:42 -07001016 for (int j = 0; j < targets.count(); j++) {
tomhudsond968a6f2015-03-26 11:28:06 -07001017 // During HWUI output this canvas may be NULL.
tomhudson75a0ebb2015-03-27 12:11:44 -07001018 SkCanvas* canvas = targets[j]->getCanvas();
bsalomonc2553372014-07-22 13:09:05 -07001019 const char* config = targets[j]->config.name;
mtkleinf3723212014-06-25 14:08:00 -07001020
tomhudsond968a6f2015-03-26 11:28:06 -07001021 targets[j]->setup();
robertphillips5b693772014-11-21 06:19:36 -08001022 bench->perCanvasPreDraw(canvas);
1023
cdaltond416a5b2015-06-23 13:23:44 -07001024 int frameLag;
mtkleinbb6a0282014-07-01 08:43:42 -07001025 const int loops =
cdaltond416a5b2015-06-23 13:23:44 -07001026 targets[j]->needsFrameTiming(&frameLag)
1027 ? gpu_bench(targets[j], bench.get(), samples.get(), frameLag)
tomhudson75a0ebb2015-03-27 12:11:44 -07001028 : cpu_bench(overhead, targets[j], bench.get(), samples.get());
mtkleinf3723212014-06-25 14:08:00 -07001029
robertphillips5b693772014-11-21 06:19:36 -08001030 bench->perCanvasPostDraw(canvas);
1031
tomhudsond968a6f2015-03-26 11:28:06 -07001032 if (Benchmark::kNonRendering_Backend != targets[j]->config.backend &&
1033 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001034 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001035 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001036 pngFilename.append(".png");
tomhudsond968a6f2015-03-26 11:28:06 -07001037 write_canvas_png(targets[j], pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001038 }
1039
1040 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001041 // Can't be timed. A warning note has already been printed.
Mike Kleine3631362014-07-15 17:56:37 -04001042 continue;
1043 }
1044
mtkleinf3723212014-06-25 14:08:00 -07001045 Stats stats(samples.get(), FLAGS_samples);
mtklein1915b622014-08-20 11:45:00 -07001046 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001047 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001048 benchStream.fillCurrentOptions(log.get());
tomhudsond968a6f2015-03-26 11:28:06 -07001049 targets[j]->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001050 log->metric("min_ms", stats.min);
mtkleine070c2b2014-10-14 08:40:43 -07001051 if (runs++ % FLAGS_flushEvery == 0) {
1052 log->flush();
1053 }
mtklein60317d0f2014-07-14 11:30:37 -07001054
bsalomon6eb03cc2014-08-07 14:28:50 -07001055 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001056 if (targets.count() == 1) {
1057 config = ""; // Only print the config if we run the same bench on more than one.
1058 }
mtkleind75c4662015-04-30 07:11:22 -07001059 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1060 , sk_tools::getCurrResidentSetSizeMB()
1061 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001062 , bench->getUniqueName()
1063 , config);
mtkleina189ccd2014-07-14 12:28:47 -07001064 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -07001065 for (int i = 0; i < FLAGS_samples; i++) {
mtklein55b0ffc2014-07-17 08:38:23 -07001066 SkDebugf("%s ", HUMANIZE(samples[i]));
mtkleinf3723212014-06-25 14:08:00 -07001067 }
mtklein96289052014-09-10 12:05:59 -07001068 SkDebugf("%s\n", bench->getUniqueName());
mtkleinf3723212014-06-25 14:08:00 -07001069 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -07001070 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -07001071 config = ""; // Only print the config if we run the same bench on more than one.
1072 }
mtklein96289052014-09-10 12:05:59 -07001073 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001074 } else {
1075 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001076 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1077 , sk_tools::getCurrResidentSetSizeMB()
1078 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001079 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001080 , HUMANIZE(stats.min)
1081 , HUMANIZE(stats.median)
1082 , HUMANIZE(stats.mean)
1083 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001084 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -07001085 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001086 , config
mtklein96289052014-09-10 12:05:59 -07001087 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001088 );
1089 }
bsalomonb12ea412015-02-02 21:19:50 -08001090#if SK_SUPPORT_GPU
1091 if (FLAGS_gpuStats &&
bsalomon06cddec2014-10-24 10:40:50 -07001092 Benchmark::kGPU_Backend == targets[j]->config.backend) {
mtklein6838d852014-10-29 14:15:10 -07001093 gGrFactory->get(targets[j]->config.ctxType)->printCacheStats();
bsalomonb12ea412015-02-02 21:19:50 -08001094 gGrFactory->get(targets[j]->config.ctxType)->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001095 }
1096#endif
mtkleinf3723212014-06-25 14:08:00 -07001097 }
mtkleinbb6a0282014-07-01 08:43:42 -07001098 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -04001099
bsalomon06cddec2014-10-24 10:40:50 -07001100#if SK_SUPPORT_GPU
bsalomon2354f842014-07-28 13:48:36 -07001101 if (FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -07001102 gGrFactory->abandonContexts();
bsalomon2354f842014-07-28 13:48:36 -07001103 }
1104 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -07001105 gGrFactory->destroyContexts();
Mike Klein3944a1d2014-07-15 13:40:19 -04001106 }
bsalomon06cddec2014-10-24 10:40:50 -07001107#endif
mtkleinf3723212014-06-25 14:08:00 -07001108 }
1109
mtkleine1091452014-12-04 10:47:02 -08001110 log->bench("memory_usage", 0,0);
1111 log->config("meta");
1112 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1113
joshualitte0b19d42015-03-26 10:41:02 -07001114#if SK_SUPPORT_GPU
1115 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1116 // SkEventTracer destructor
1117 gGrFactory.reset(NULL);
1118#endif
1119
mtkleinf3723212014-06-25 14:08:00 -07001120 return 0;
1121}
1122
jcgregorio3b27ade2014-11-13 08:06:40 -08001123#if !defined SK_BUILD_FOR_IOS
1124int main(int argc, char** argv) {
1125 SkCommandLineFlags::Parse(argc, argv);
1126 return nanobench_main();
1127}
1128#endif