blob: 2fba1f053373c21e9aba473d65b79111f0464aaa [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"
16#include "DecodingSubsetBench.h"
mtkleine714e752014-07-31 12:13:48 -070017#include "GMBench.h"
mtkleinafb43792014-08-19 15:55:55 -070018#include "ProcStats.h"
mtklein60317d0f2014-07-14 11:30:37 -070019#include "ResultsWriter.h"
mtkleinfd731ce2014-09-10 12:19:30 -070020#include "RecordingBench.h"
joshualitt261c3ad2015-04-27 09:16:57 -070021#include "SKPAnimationBench.h"
mtklein92007582014-08-01 07:46:52 -070022#include "SKPBench.h"
mtkleinf3723212014-06-25 14:08:00 -070023#include "Stats.h"
24#include "Timer.h"
25
mtklein6838d852014-10-29 14:15:10 -070026#include "SkBBoxHierarchy.h"
mtkleinf3723212014-06-25 14:08:00 -070027#include "SkCanvas.h"
scroggo60869a42015-04-01 12:09:17 -070028#include "SkCodec.h"
caryclark17f0b6d2014-07-22 10:15:34 -070029#include "SkCommonFlags.h"
msarett95f192d2015-02-13 09:05:41 -080030#include "SkData.h"
mtkleinf3723212014-06-25 14:08:00 -070031#include "SkForceLinking.h"
32#include "SkGraphics.h"
mtklein20840502014-08-21 15:51:22 -070033#include "SkOSFile.h"
34#include "SkPictureRecorder.h"
mtklein051e56d2014-12-04 08:46:51 -080035#include "SkPictureUtils.h"
mtkleinf3723212014-06-25 14:08:00 -070036#include "SkString.h"
37#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080038#include "SkTaskGroup.h"
mtkleinf3723212014-06-25 14:08:00 -070039
tomhudsond968a6f2015-03-26 11:28:06 -070040#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
41 #include "nanobenchAndroid.h"
42#endif
43
mtkleinbb6a0282014-07-01 08:43:42 -070044#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070045 #include "gl/GrGLDefines.h"
mtkleinbb6a0282014-07-01 08:43:42 -070046 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070047 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070048#endif
49
bsalomon682c2692015-05-22 14:01:46 -070050 struct GrContextOptions;
51
mtkleinf3723212014-06-25 14:08:00 -070052__SK_FORCE_IMAGE_DECODER_LINKING;
53
reed53249782014-10-10 09:09:52 -070054static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070055
mtkleinb5110422014-08-07 15:20:02 -070056static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070057#ifdef SK_DEBUG
58 1;
mtkleina189ccd2014-07-14 12:28:47 -070059#else
bsalomon6eb03cc2014-08-07 14:28:50 -070060 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070061#endif
62
bsalomon6eb03cc2014-08-07 14:28:50 -070063static SkString loops_help_txt() {
64 SkString help;
65 help.printf("Number of times to run each bench. Set this to %d to auto-"
66 "tune for each bench. Timings are only reported when auto-tuning.",
67 kAutoTuneLoops);
68 return help;
69}
70
71DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
72
mtkleinf3723212014-06-25 14:08:00 -070073DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
74DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
75DEFINE_double(overheadGoal, 0.0001,
76 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070077DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
78DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070079DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
80 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070081
mtklein60317d0f2014-07-14 11:30:37 -070082DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -070083DEFINE_int32(maxCalibrationAttempts, 3,
84 "Try up to this many times to guess loops for a bench, or skip the bench.");
85DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -070086DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
87DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
joshualitt261c3ad2015-04-27 09:16:57 -070088DEFINE_string(zoom, "1.0,1", "Comma-separated scale,step zoom factors for SKPs.");
mtklein20840502014-08-21 15:51:22 -070089DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
robertphillips5b693772014-11-21 06:19:36 -080090DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
mtkleine070c2b2014-10-14 08:40:43 -070091DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -080092DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -080093DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
mtklein92007582014-08-01 07:46:52 -070094
mtkleinf3723212014-06-25 14:08:00 -070095static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -070096 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -080097 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -070098}
mtklein55b0ffc2014-07-17 08:38:23 -070099#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700100
tomhudsond968a6f2015-03-26 11:28:06 -0700101bool Target::init(SkImageInfo info, Benchmark* bench) {
102 if (Benchmark::kRaster_Backend == config.backend) {
103 this->surface.reset(SkSurface::NewRaster(info));
104 if (!this->surface.get()) {
105 return false;
106 }
107 }
108 return true;
109}
110bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700111 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700112 if (!canvas) {
113 return false;
114 }
115 bmp->setInfo(canvas->imageInfo());
116 if (!canvas->readPixels(bmp, 0, 0)) {
117 SkDebugf("Can't read canvas pixels.\n");
118 return false;
119 }
120 return true;
121}
122
123#if SK_SUPPORT_GPU
124struct GPUTarget : public Target {
125 explicit GPUTarget(const Config& c) : Target(c), gl(NULL) { }
126 SkGLContext* gl;
127
128 void setup() override {
129 this->gl->makeCurrent();
130 // Make sure we're done with whatever came before.
131 SK_GL(*this->gl, Finish());
132 }
133 void endTiming() override {
134 if (this->gl) {
135 SK_GL(*this->gl, Flush());
136 this->gl->swapBuffers();
137 }
138 }
139 void fence() override {
140 SK_GL(*this->gl, Finish());
141 }
mtkleind75c4662015-04-30 07:11:22 -0700142
tomhudsond968a6f2015-03-26 11:28:06 -0700143 bool needsFrameTiming() const override { return true; }
144 bool init(SkImageInfo info, Benchmark* bench) override {
145 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDistanceFieldFonts_Flag : 0;
146 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
147 this->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(this->config.ctxType),
148 SkSurface::kNo_Budgeted, info,
149 this->config.samples, &props));
150 this->gl = gGrFactory->getGLContext(this->config.ctxType);
151 if (!this->surface.get()) {
152 return false;
153 }
154 return true;
155 }
156 void fillOptions(ResultsWriter* log) override {
157 const GrGLubyte* version;
158 SK_GL_RET(*this->gl, version, GetString(GR_GL_VERSION));
159 log->configOption("GL_VERSION", (const char*)(version));
160
161 SK_GL_RET(*this->gl, version, GetString(GR_GL_RENDERER));
162 log->configOption("GL_RENDERER", (const char*) version);
163
164 SK_GL_RET(*this->gl, version, GetString(GR_GL_VENDOR));
165 log->configOption("GL_VENDOR", (const char*) version);
166
167 SK_GL_RET(*this->gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
168 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
169 }
170};
mtkleind75c4662015-04-30 07:11:22 -0700171
tomhudsond968a6f2015-03-26 11:28:06 -0700172#endif
173
tomhudson75a0ebb2015-03-27 12:11:44 -0700174static double time(int loops, Benchmark* bench, Target* target) {
175 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700176 if (canvas) {
177 canvas->clear(SK_ColorWHITE);
178 }
mtkleinbb6a0282014-07-01 08:43:42 -0700179 WallTimer timer;
180 timer.start();
tomhudson75a0ebb2015-03-27 12:11:44 -0700181 canvas = target->beginTiming(canvas);
182 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700183 if (canvas) {
184 canvas->flush();
185 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700186 target->endTiming();
mtkleinbb6a0282014-07-01 08:43:42 -0700187 timer.end();
188 return timer.fWall;
189}
190
mtkleinf3723212014-06-25 14:08:00 -0700191static double estimate_timer_overhead() {
192 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700193 for (int i = 0; i < FLAGS_overheadLoops; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700194 WallTimer timer;
195 timer.start();
196 timer.end();
197 overhead += timer.fWall;
mtkleinf3723212014-06-25 14:08:00 -0700198 }
199 return overhead / FLAGS_overheadLoops;
200}
201
reed53249782014-10-10 09:09:52 -0700202static int detect_forever_loops(int loops) {
203 // look for a magic run-forever value
204 if (loops < 0) {
205 loops = SK_MaxS32;
206 }
207 return loops;
208}
209
mtklein55b0ffc2014-07-17 08:38:23 -0700210static int clamp_loops(int loops) {
211 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800212 SkDebugf("ERROR: clamping loops from %d to 1. "
213 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700214 return 1;
215 }
216 if (loops > FLAGS_maxLoops) {
217 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
218 return FLAGS_maxLoops;
219 }
220 return loops;
221}
222
tomhudsond968a6f2015-03-26 11:28:06 -0700223static bool write_canvas_png(Target* target, const SkString& filename) {
224
bsalomon6eb03cc2014-08-07 14:28:50 -0700225 if (filename.isEmpty()) {
226 return false;
227 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700228 if (target->getCanvas() &&
229 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700230 return false;
231 }
tomhudsond968a6f2015-03-26 11:28:06 -0700232
bsalomon6eb03cc2014-08-07 14:28:50 -0700233 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700234
235 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700236 return false;
237 }
tomhudsond968a6f2015-03-26 11:28:06 -0700238
bsalomon6eb03cc2014-08-07 14:28:50 -0700239 SkString dir = SkOSPath::Dirname(filename.c_str());
240 if (!sk_mkdir(dir.c_str())) {
241 SkDebugf("Can't make dir %s.\n", dir.c_str());
242 return false;
243 }
244 SkFILEWStream stream(filename.c_str());
245 if (!stream.isValid()) {
246 SkDebugf("Can't write %s.\n", filename.c_str());
247 return false;
248 }
249 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
250 SkDebugf("Can't encode a PNG.\n");
251 return false;
252 }
253 return true;
254}
255
256static int kFailedLoops = -2;
tomhudson75a0ebb2015-03-27 12:11:44 -0700257static int cpu_bench(const double overhead, Target* target, Benchmark* bench, double* samples) {
mtkleinbb6a0282014-07-01 08:43:42 -0700258 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700259 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700260 int round = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -0700261 if (kAutoTuneLoops == FLAGS_loops) {
262 while (bench_plus_overhead < overhead) {
263 if (round++ == FLAGS_maxCalibrationAttempts) {
264 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700265 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700266 return kFailedLoops;
267 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700268 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700269 }
mtklein2069e222014-08-04 13:57:39 -0700270 }
mtkleinf3723212014-06-25 14:08:00 -0700271
mtkleinbb6a0282014-07-01 08:43:42 -0700272 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700273 // We'll pick N to make timer overhead negligible:
274 //
mtkleinbb6a0282014-07-01 08:43:42 -0700275 // overhead
276 // ------------------------- < FLAGS_overheadGoal
277 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700278 //
mtkleinbb6a0282014-07-01 08:43:42 -0700279 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700280 //
281 // Doing some math, we get:
282 //
mtkleinbb6a0282014-07-01 08:43:42 -0700283 // (overhead / FLAGS_overheadGoal) - overhead
284 // ------------------------------------------ < N
285 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700286 //
287 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700288 int loops = FLAGS_loops;
289 if (kAutoTuneLoops == loops) {
290 const double numer = overhead / FLAGS_overheadGoal - overhead;
291 const double denom = bench_plus_overhead - overhead;
292 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700293 loops = clamp_loops(loops);
294 } else {
295 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700296 }
mtkleinbb6a0282014-07-01 08:43:42 -0700297
298 for (int i = 0; i < FLAGS_samples; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700299 samples[i] = time(loops, bench, target) / loops;
mtkleinbb6a0282014-07-01 08:43:42 -0700300 }
301 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700302}
303
tomhudsond968a6f2015-03-26 11:28:06 -0700304static int gpu_bench(Target* target,
mtkleinbb6a0282014-07-01 08:43:42 -0700305 Benchmark* bench,
mtkleinbb6a0282014-07-01 08:43:42 -0700306 double* samples) {
mtkleinbb6a0282014-07-01 08:43:42 -0700307 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
bsalomon6eb03cc2014-08-07 14:28:50 -0700308 int loops = FLAGS_loops;
309 if (kAutoTuneLoops == loops) {
310 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700311 double elapsed = 0;
312 do {
mtklein527930f2014-11-06 08:04:34 -0800313 if (1<<30 == loops) {
314 // We're about to wrap. Something's wrong with the bench.
315 loops = 0;
316 break;
317 }
mtkleina189ccd2014-07-14 12:28:47 -0700318 loops *= 2;
319 // If the GPU lets frames lag at all, we need to make sure we're timing
320 // _this_ round, not still timing last round. We force this by looping
321 // more times than any reasonable GPU will allow frames to lag.
322 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700323 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700324 }
325 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700326
mtkleina189ccd2014-07-14 12:28:47 -0700327 // We've overshot at least a little. Scale back linearly.
328 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700329 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700330
tomhudsond968a6f2015-03-26 11:28:06 -0700331 // Make sure we're not still timing our calibration.
332 target->fence();
reed53249782014-10-10 09:09:52 -0700333 } else {
334 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700335 }
mtkleinbb6a0282014-07-01 08:43:42 -0700336
337 // Pretty much the same deal as the calibration: do some warmup to make
338 // sure we're timing steady-state pipelined frames.
339 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700340 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700341 }
mtkleinbb6a0282014-07-01 08:43:42 -0700342
343 // Now, actually do the timing!
344 for (int i = 0; i < FLAGS_samples; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700345 samples[i] = time(loops, bench, target) / loops;
mtkleinbb6a0282014-07-01 08:43:42 -0700346 }
tomhudsond968a6f2015-03-26 11:28:06 -0700347
mtkleinbb6a0282014-07-01 08:43:42 -0700348 return loops;
349}
mtkleinbb6a0282014-07-01 08:43:42 -0700350
351static SkString to_lower(const char* str) {
352 SkString lower(str);
353 for (size_t i = 0; i < lower.size(); i++) {
354 lower[i] = tolower(lower[i]);
355 }
356 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700357}
358
bsalomonc2553372014-07-22 13:09:05 -0700359static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700360 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700361 if (to_lower(FLAGS_config[i]).equals(name)) {
362 return true;
mtkleinf3723212014-06-25 14:08:00 -0700363 }
364 }
bsalomonc2553372014-07-22 13:09:05 -0700365 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700366}
367
bsalomonc2553372014-07-22 13:09:05 -0700368#if SK_SUPPORT_GPU
369static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
370 int sampleCnt) {
371 if (!is_cpu_config_allowed(name)) {
372 return false;
373 }
krajcevski69a55602014-08-13 10:46:31 -0700374 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomonc2553372014-07-22 13:09:05 -0700375 return sampleCnt <= ctx->getMaxSampleCount();
376 }
377 return false;
378}
379#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700380
bsalomonc2553372014-07-22 13:09:05 -0700381#if SK_SUPPORT_GPU
382#define kBogusGLContextType GrContextFactory::kNative_GLContextType
383#else
384#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700385#endif
bsalomonc2553372014-07-22 13:09:05 -0700386
387// Append all configs that are enabled and supported.
388static void create_configs(SkTDArray<Config>* configs) {
jvanverth4736e142014-11-07 07:12:46 -0800389 #define CPU_CONFIG(name, backend, color, alpha) \
390 if (is_cpu_config_allowed(#name)) { \
391 Config config = { #name, Benchmark::backend, color, alpha, 0, \
392 kBogusGLContextType, false }; \
393 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700394 }
mtkleine714e752014-07-31 12:13:48 -0700395
mtklein40b32be2014-07-09 08:46:49 -0700396 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700397 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
398 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
399 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700400 }
mtkleinbb6a0282014-07-01 08:43:42 -0700401
402#if SK_SUPPORT_GPU
jvanverth4736e142014-11-07 07:12:46 -0800403 #define GPU_CONFIG(name, ctxType, samples, useDFText) \
bsalomonc2553372014-07-22 13:09:05 -0700404 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
405 Config config = { \
406 #name, \
407 Benchmark::kGPU_Backend, \
408 kN32_SkColorType, \
409 kPremul_SkAlphaType, \
410 samples, \
jvanverth4736e142014-11-07 07:12:46 -0800411 GrContextFactory::ctxType, \
412 useDFText }; \
bsalomonc2553372014-07-22 13:09:05 -0700413 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700414 }
mtkleine714e752014-07-31 12:13:48 -0700415
mtklein40b32be2014-07-09 08:46:49 -0700416 if (FLAGS_gpu) {
jvanverth4736e142014-11-07 07:12:46 -0800417 GPU_CONFIG(gpu, kNative_GLContextType, 0, false)
418 GPU_CONFIG(msaa4, kNative_GLContextType, 4, false)
419 GPU_CONFIG(msaa16, kNative_GLContextType, 16, false)
420 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4, false)
421 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16, false)
422 GPU_CONFIG(gpudft, kNative_GLContextType, 0, true)
423 GPU_CONFIG(debug, kDebug_GLContextType, 0, false)
424 GPU_CONFIG(nullgpu, kNull_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700425#ifdef SK_ANGLE
jvanverth4736e142014-11-07 07:12:46 -0800426 GPU_CONFIG(angle, kANGLE_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700427#endif
mtklein40b32be2014-07-09 08:46:49 -0700428 }
mtkleinbb6a0282014-07-01 08:43:42 -0700429#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700430
431#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
432 if (is_cpu_config_allowed("hwui")) {
433 Config config = { "hwui", Benchmark::kHWUI_Backend, kRGBA_8888_SkColorType,
434 kPremul_SkAlphaType, 0, kBogusGLContextType, false };
435 configs->push(config);
436 }
437#endif
mtkleinf3723212014-06-25 14:08:00 -0700438}
439
bsalomonc2553372014-07-22 13:09:05 -0700440// If bench is enabled for config, returns a Target* for it, otherwise NULL.
441static Target* is_enabled(Benchmark* bench, const Config& config) {
442 if (!bench->isSuitableFor(config.backend)) {
443 return NULL;
444 }
445
reede5ea5002014-09-03 11:54:58 -0700446 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
447 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700448
tomhudsond968a6f2015-03-26 11:28:06 -0700449 Target* target = NULL;
bsalomonc2553372014-07-22 13:09:05 -0700450
tomhudsond968a6f2015-03-26 11:28:06 -0700451 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700452#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700453 case Benchmark::kGPU_Backend:
454 target = new GPUTarget(config);
455 break;
bsalomonc2553372014-07-22 13:09:05 -0700456#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700457#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
458 case Benchmark::kHWUI_Backend:
459 target = new HWUITarget(config, bench);
460 break;
461#endif
462 default:
463 target = new Target(config);
464 break;
465 }
bsalomonc2553372014-07-22 13:09:05 -0700466
tomhudsond968a6f2015-03-26 11:28:06 -0700467 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700468 delete target;
469 return NULL;
470 }
471 return target;
472}
473
474// Creates targets for a benchmark and a set of configs.
475static void create_targets(SkTDArray<Target*>* targets, Benchmark* b,
476 const SkTDArray<Config>& configs) {
477 for (int i = 0; i < configs.count(); ++i) {
478 if (Target* t = is_enabled(b, configs[i])) {
479 targets->push(t);
480 }
mtkleine714e752014-07-31 12:13:48 -0700481
bsalomonc2553372014-07-22 13:09:05 -0700482 }
483}
484
jcgregoriobf5e5232014-07-17 13:14:16 -0700485
mtkleine714e752014-07-31 12:13:48 -0700486class BenchmarkStream {
487public:
mtklein92007582014-08-01 07:46:52 -0700488 BenchmarkStream() : fBenches(BenchRegistry::Head())
489 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700490 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700491 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800492 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800493 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700494 , fCurrentCodec(0)
msarett95f192d2015-02-13 09:05:41 -0800495 , fCurrentImage(0)
496 , fCurrentSubsetImage(0)
497 , fCurrentColorType(0)
joshualitt261c3ad2015-04-27 09:16:57 -0700498 , fCurrentAnimSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800499 , fDivisor(2) {
mtklein92007582014-08-01 07:46:52 -0700500 for (int i = 0; i < FLAGS_skps.count(); i++) {
501 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
502 fSKPs.push_back() = FLAGS_skps[i];
503 } else {
504 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
505 SkString path;
506 while (it.next(&path)) {
507 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
508 }
509 }
510 }
mtkleine714e752014-07-31 12:13:48 -0700511
mtklein92007582014-08-01 07:46:52 -0700512 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
513 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
514 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
515 exit(1);
516 }
517
518 for (int i = 0; i < FLAGS_scales.count(); i++) {
519 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
520 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
521 exit(1);
522 }
523 }
robertphillips5b693772014-11-21 06:19:36 -0800524
joshualitt261c3ad2015-04-27 09:16:57 -0700525 if (2 != sscanf(FLAGS_zoom[0], "%f,%d", &fZoomScale, &fZoomSteps)) {
526 SkDebugf("Can't parse %s from --zoom as a scale,step.\n", FLAGS_zoom[0]);
527 exit(1);
528 }
529
robertphillips5b693772014-11-21 06:19:36 -0800530 fUseMPDs.push_back() = false;
531 if (FLAGS_mpd) {
532 fUseMPDs.push_back() = true;
533 }
mtklein95553d92015-03-12 08:24:21 -0700534
msarett95f192d2015-02-13 09:05:41 -0800535 // Prepare the images for decoding
536 for (int i = 0; i < FLAGS_images.count(); i++) {
537 const char* flag = FLAGS_images[i];
538 if (sk_isdir(flag)) {
539 // If the value passed in is a directory, add all the images
540 SkOSFile::Iter it(flag);
541 SkString file;
542 while (it.next(&file)) {
543 fImages.push_back() = SkOSPath::Join(flag, file.c_str());
544 }
545 } else if (sk_exists(flag)) {
546 // Also add the value if it is a single image
547 fImages.push_back() = flag;
548 }
549 }
mtklein95553d92015-03-12 08:24:21 -0700550
msarett95f192d2015-02-13 09:05:41 -0800551 // Choose the candidate color types for image decoding
552 const SkColorType colorTypes[] =
scroggo21027992015-04-02 13:22:38 -0700553 { kN32_SkColorType, kRGB_565_SkColorType, kAlpha_8_SkColorType, kIndex_8_SkColorType };
msarett95f192d2015-02-13 09:05:41 -0800554 fColorTypes.push_back_n(SK_ARRAY_COUNT(colorTypes), colorTypes);
mtklein92007582014-08-01 07:46:52 -0700555 }
556
mtkleinfd731ce2014-09-10 12:19:30 -0700557 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
558 // Not strictly necessary, as it will be checked again later,
559 // but helps to avoid a lot of pointless work if we're going to skip it.
560 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
561 return false;
562 }
563
scroggoa1193e42015-01-21 12:09:53 -0800564 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
mtkleinfd731ce2014-09-10 12:19:30 -0700565 if (stream.get() == NULL) {
566 SkDebugf("Could not read %s.\n", path);
567 return false;
568 }
569
mtklein57f27bd2015-02-09 11:58:41 -0800570 pic->reset(SkPicture::CreateFromStream(stream.get()));
mtkleinfd731ce2014-09-10 12:19:30 -0700571 if (pic->get() == NULL) {
572 SkDebugf("Could not read %s as an SkPicture.\n", path);
573 return false;
574 }
575 return true;
576 }
577
mtklein92007582014-08-01 07:46:52 -0700578 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700579 if (fBenches) {
580 Benchmark* bench = fBenches->factory()(NULL);
581 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700582 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700583 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700584 return bench;
585 }
mtklein92007582014-08-01 07:46:52 -0700586
mtkleine714e752014-07-31 12:13:48 -0700587 while (fGMs) {
588 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
589 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800590 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700591 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700592 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700593 return SkNEW_ARGS(GMBench, (gm.detach()));
594 }
595 }
mtklein92007582014-08-01 07:46:52 -0700596
mtkleinfd731ce2014-09-10 12:19:30 -0700597 // First add all .skps as RecordingBenches.
598 while (fCurrentRecording < fSKPs.count()) {
599 const SkString& path = fSKPs[fCurrentRecording++];
600 SkAutoTUnref<SkPicture> pic;
601 if (!ReadPicture(path.c_str(), &pic)) {
602 continue;
603 }
604 SkString name = SkOSPath::Basename(path.c_str());
605 fSourceType = "skp";
606 fBenchType = "recording";
bsalomon0aa5cea2014-12-15 09:13:35 -0800607 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic));
mtklein051e56d2014-12-04 08:46:51 -0800608 fSKPOps = pic->approximateOpCount();
mtkleinfd731ce2014-09-10 12:19:30 -0700609 return SkNEW_ARGS(RecordingBench, (name.c_str(), pic.get(), FLAGS_bbh));
610 }
611
612 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700613 while (fCurrentScale < fScales.count()) {
614 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800615 const SkString& path = fSKPs[fCurrentSKP];
mtkleinfd731ce2014-09-10 12:19:30 -0700616 SkAutoTUnref<SkPicture> pic;
617 if (!ReadPicture(path.c_str(), &pic)) {
robertphillips5b693772014-11-21 06:19:36 -0800618 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700619 continue;
620 }
robertphillips5b693772014-11-21 06:19:36 -0800621
622 while (fCurrentUseMPD < fUseMPDs.count()) {
623 if (FLAGS_bbh) {
624 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
625 SkRTreeFactory factory;
626 SkPictureRecorder recorder;
627 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
628 pic->playback(recorder.beginRecording(pic->cullRect().width(),
629 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800630 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800631 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
robertphillips5b693772014-11-21 06:19:36 -0800632 pic.reset(recorder.endRecording());
633 }
634 SkString name = SkOSPath::Basename(path.c_str());
635 fSourceType = "skp";
636 fBenchType = "playback";
637 return SkNEW_ARGS(SKPBench,
joshualitt261c3ad2015-04-27 09:16:57 -0700638 (name.c_str(), pic.get(), fClip,
639 fScales[fCurrentScale], fUseMPDs[fCurrentUseMPD++]));
640
mtklein20840502014-08-21 15:51:22 -0700641 }
robertphillips5b693772014-11-21 06:19:36 -0800642 fCurrentUseMPD = 0;
643 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700644 }
645 fCurrentSKP = 0;
646 fCurrentScale++;
647 }
648
joshualitt261c3ad2015-04-27 09:16:57 -0700649 // Now loop over each skp again if we have an animation
650 if (fZoomScale != 1.0f && fZoomSteps != 1) {
651 while (fCurrentAnimSKP < fSKPs.count()) {
652 const SkString& path = fSKPs[fCurrentAnimSKP];
653 SkAutoTUnref<SkPicture> pic;
654 if (!ReadPicture(path.c_str(), &pic)) {
655 fCurrentAnimSKP++;
656 continue;
657 }
658
659 fCurrentAnimSKP++;
660 SkString name = SkOSPath::Basename(path.c_str());
661 SkMatrix anim = SkMatrix::I();
662 anim.setScale(fZoomScale, fZoomScale);
663 return SkNEW_ARGS(SKPAnimationBench, (name.c_str(), pic.get(), fClip, anim,
664 fZoomSteps));
665 }
666 }
667
668
scroggo60869a42015-04-01 12:09:17 -0700669 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
670 const SkString& path = fImages[fCurrentCodec];
671 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
672 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700673 if (!codec) {
674 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700675 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700676 continue;
677 }
scroggo21027992015-04-02 13:22:38 -0700678
scroggo60869a42015-04-01 12:09:17 -0700679 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700680 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo60869a42015-04-01 12:09:17 -0700681 fCurrentColorType++;
scroggo21027992015-04-02 13:22:38 -0700682
scroggo60869a42015-04-01 12:09:17 -0700683 // Make sure we can decode to this color type.
scroggo60869a42015-04-01 12:09:17 -0700684 SkImageInfo info = codec->getInfo().makeColorType(colorType);
scroggo21027992015-04-02 13:22:38 -0700685 SkAlphaType alphaType;
686 if (!SkColorTypeValidateAlphaType(colorType, info.alphaType(),
687 &alphaType)) {
688 continue;
689 }
690 if (alphaType != info.alphaType()) {
691 info = info.makeAlphaType(alphaType);
692 }
693
694 const size_t rowBytes = info.minRowBytes();
695 SkAutoMalloc storage(info.getSafeSize(rowBytes));
696
697 // Used if fCurrentColorType is kIndex_8_SkColorType
698 int colorCount = 256;
699 SkPMColor colors[256];
700
scroggo60869a42015-04-01 12:09:17 -0700701 const SkImageGenerator::Result result = codec->getPixels(
scroggo21027992015-04-02 13:22:38 -0700702 info, storage.get(), rowBytes, NULL, colors,
703 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700704 switch (result) {
705 case SkImageGenerator::kSuccess:
706 case SkImageGenerator::kIncompleteInput:
707 return new CodecBench(SkOSPath::Basename(path.c_str()),
708 encoded, colorType);
709 case SkImageGenerator::kInvalidConversion:
710 // This is okay. Not all conversions are valid.
711 break;
scroggo60869a42015-04-01 12:09:17 -0700712 default:
713 // This represents some sort of failure.
714 SkASSERT(false);
715 break;
716 }
717 }
718 fCurrentColorType = 0;
719 }
720
msarett95f192d2015-02-13 09:05:41 -0800721 // Run the DecodingBenches
722 while (fCurrentImage < fImages.count()) {
723 while (fCurrentColorType < fColorTypes.count()) {
724 const SkString& path = fImages[fCurrentImage];
725 SkColorType colorType = fColorTypes[fCurrentColorType];
726 fCurrentColorType++;
scroggo60869a42015-04-01 12:09:17 -0700727 // Check if the image decodes to the right color type
728 // before creating the benchmark
msarett95f192d2015-02-13 09:05:41 -0800729 SkBitmap bitmap;
730 if (SkImageDecoder::DecodeFile(path.c_str(), &bitmap,
scroggo60869a42015-04-01 12:09:17 -0700731 colorType, SkImageDecoder::kDecodePixels_Mode)
732 && bitmap.colorType() == colorType) {
msarett95f192d2015-02-13 09:05:41 -0800733 return new DecodingBench(path, colorType);
734 }
735 }
736 fCurrentColorType = 0;
737 fCurrentImage++;
738 }
739
740 // Run the DecodingSubsetBenches
741 while (fCurrentSubsetImage < fImages.count()) {
742 while (fCurrentColorType < fColorTypes.count()) {
743 const SkString& path = fImages[fCurrentSubsetImage];
744 SkColorType colorType = fColorTypes[fCurrentColorType];
745 fCurrentColorType++;
746 // Check if the image decodes before creating the benchmark
747 SkAutoTUnref<SkData> encoded(
748 SkData::NewFromFileName(path.c_str()));
749 SkAutoTDelete<SkMemoryStream> stream(
750 new SkMemoryStream(encoded));
751 SkAutoTDelete<SkImageDecoder>
752 decoder(SkImageDecoder::Factory(stream.get()));
753 if (!decoder) {
754 SkDebugf("Cannot find decoder for %s\n", path.c_str());
755 } else {
756 stream->rewind();
757 int w, h;
758 bool success;
759 if (!decoder->buildTileIndex(stream.detach(), &w, &h)
760 || w*h == 1) {
761 // This is not an error, but in this case we still
762 // do not want to run the benchmark.
763 success = false;
764 } else if (fDivisor > w || fDivisor > h) {
765 SkDebugf("Divisor %d is too big for %s %dx%d\n",
766 fDivisor, path.c_str(), w, h);
767 success = false;
768 } else {
769 const int sW = w / fDivisor;
770 const int sH = h / fDivisor;
771 SkBitmap bitmap;
772 success = true;
773 for (int y = 0; y < h; y += sH) {
774 for (int x = 0; x < w; x += sW) {
775 SkIRect rect = SkIRect::MakeXYWH(x, y, sW, sH);
776 success &= decoder->decodeSubset(&bitmap, rect,
777 colorType);
778 }
779 }
780 }
781 // Create the benchmark if successful
782 if (success) {
783 return new DecodingSubsetBench(path, colorType,
784 fDivisor);
785 }
786 }
787 }
788 fCurrentColorType = 0;
789 fCurrentSubsetImage++;
790 }
791
mtkleine714e752014-07-31 12:13:48 -0700792 return NULL;
793 }
mtklein92007582014-08-01 07:46:52 -0700794
795 void fillCurrentOptions(ResultsWriter* log) const {
796 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700797 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700798 if (0 == strcmp(fSourceType, "skp")) {
799 log->configOption("clip",
800 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
801 fClip.fRight, fClip.fBottom).c_str());
802 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -0800803 if (fCurrentUseMPD > 0) {
804 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
805 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
806 }
mtklein92007582014-08-01 07:46:52 -0700807 }
mtklein051e56d2014-12-04 08:46:51 -0800808 if (0 == strcmp(fBenchType, "recording")) {
809 log->metric("bytes", fSKPBytes);
810 log->metric("ops", fSKPOps);
811 }
mtklein92007582014-08-01 07:46:52 -0700812 }
813
mtkleine714e752014-07-31 12:13:48 -0700814private:
815 const BenchRegistry* fBenches;
816 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700817 SkIRect fClip;
818 SkTArray<SkScalar> fScales;
819 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -0800820 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -0800821 SkTArray<SkString> fImages;
822 SkTArray<SkColorType> fColorTypes;
joshualitt261c3ad2015-04-27 09:16:57 -0700823 SkScalar fZoomScale;
824 int fZoomSteps;
mtklein92007582014-08-01 07:46:52 -0700825
mtklein051e56d2014-12-04 08:46:51 -0800826 double fSKPBytes, fSKPOps;
827
mtkleinfd731ce2014-09-10 12:19:30 -0700828 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
829 const char* fBenchType; // How we bench it: micro, recording, playback, ...
830 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700831 int fCurrentScale;
832 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -0800833 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -0700834 int fCurrentCodec;
msarett95f192d2015-02-13 09:05:41 -0800835 int fCurrentImage;
836 int fCurrentSubsetImage;
837 int fCurrentColorType;
joshualitt261c3ad2015-04-27 09:16:57 -0700838 int fCurrentAnimSKP;
msarett95f192d2015-02-13 09:05:41 -0800839 const int fDivisor;
mtkleine714e752014-07-31 12:13:48 -0700840};
841
jcgregorio3b27ade2014-11-13 08:06:40 -0800842int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700843int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800844 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -0700845 SkAutoGraphics ag;
mtklein4f108442014-12-03 13:07:39 -0800846 SkTaskGroup::Enabler enabled;
mtkleinf3723212014-06-25 14:08:00 -0700847
krajcevski69a55602014-08-13 10:46:31 -0700848#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -0700849 GrContextOptions grContextOpts;
krajcevski12b35442014-08-13 12:06:26 -0700850 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
851 gGrFactory.reset(SkNEW_ARGS(GrContextFactory, (grContextOpts)));
krajcevski69a55602014-08-13 10:46:31 -0700852#endif
853
bsalomon06cddec2014-10-24 10:40:50 -0700854 if (FLAGS_veryVerbose) {
855 FLAGS_verbose = true;
856 }
857
bsalomon6eb03cc2014-08-07 14:28:50 -0700858 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700859 FLAGS_samples = 1;
860 FLAGS_gpuFrameLag = 0;
861 }
862
bsalomon6eb03cc2014-08-07 14:28:50 -0700863 if (!FLAGS_writePath.isEmpty()) {
864 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
865 if (!sk_mkdir(FLAGS_writePath[0])) {
866 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
867 FLAGS_writePath.set(0, NULL);
868 }
869 }
870
mtklein1915b622014-08-20 11:45:00 -0700871 SkAutoTDelete<ResultsWriter> log(SkNEW(ResultsWriter));
mtklein60317d0f2014-07-14 11:30:37 -0700872 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700873 log.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0])));
mtklein60317d0f2014-07-14 11:30:37 -0700874 }
mtklein1915b622014-08-20 11:45:00 -0700875
876 if (1 == FLAGS_properties.count() % 2) {
877 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
878 return 1;
879 }
880 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
881 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
882 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700883
884 if (1 == FLAGS_key.count() % 2) {
885 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
886 return 1;
887 }
888 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -0700889 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -0700890 }
mtklein60317d0f2014-07-14 11:30:37 -0700891
mtkleinf3723212014-06-25 14:08:00 -0700892 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -0700893 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -0400894
mtkleinbb6a0282014-07-01 08:43:42 -0700895 SkAutoTMalloc<double> samples(FLAGS_samples);
896
bsalomon6eb03cc2014-08-07 14:28:50 -0700897 if (kAutoTuneLoops != FLAGS_loops) {
898 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleina189ccd2014-07-14 12:28:47 -0700899 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700900 // No header.
901 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700902 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700903 } else {
mtkleind75c4662015-04-30 07:11:22 -0700904 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -0700905 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -0700906 }
907
bsalomonc2553372014-07-22 13:09:05 -0700908 SkTDArray<Config> configs;
909 create_configs(&configs);
910
mtkleine070c2b2014-10-14 08:40:43 -0700911 int runs = 0;
mtklein92007582014-08-01 07:46:52 -0700912 BenchmarkStream benchStream;
913 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -0700914 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -0700915 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -0700916 continue;
917 }
918
mtkleinbb6a0282014-07-01 08:43:42 -0700919 SkTDArray<Target*> targets;
bsalomonc2553372014-07-22 13:09:05 -0700920 create_targets(&targets, bench.get(), configs);
mtkleinf3723212014-06-25 14:08:00 -0700921
jcgregoriobf5e5232014-07-17 13:14:16 -0700922 if (!targets.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -0700923 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
jcgregoriobf5e5232014-07-17 13:14:16 -0700924 bench->preDraw();
925 }
mtkleinbb6a0282014-07-01 08:43:42 -0700926 for (int j = 0; j < targets.count(); j++) {
tomhudsond968a6f2015-03-26 11:28:06 -0700927 // During HWUI output this canvas may be NULL.
tomhudson75a0ebb2015-03-27 12:11:44 -0700928 SkCanvas* canvas = targets[j]->getCanvas();
bsalomonc2553372014-07-22 13:09:05 -0700929 const char* config = targets[j]->config.name;
mtkleinf3723212014-06-25 14:08:00 -0700930
tomhudsond968a6f2015-03-26 11:28:06 -0700931 targets[j]->setup();
robertphillips5b693772014-11-21 06:19:36 -0800932 bench->perCanvasPreDraw(canvas);
933
mtkleinbb6a0282014-07-01 08:43:42 -0700934 const int loops =
tomhudsond968a6f2015-03-26 11:28:06 -0700935 targets[j]->needsFrameTiming()
tomhudson75a0ebb2015-03-27 12:11:44 -0700936 ? gpu_bench(targets[j], bench.get(), samples.get())
937 : cpu_bench(overhead, targets[j], bench.get(), samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700938
robertphillips5b693772014-11-21 06:19:36 -0800939 bench->perCanvasPostDraw(canvas);
940
tomhudsond968a6f2015-03-26 11:28:06 -0700941 if (Benchmark::kNonRendering_Backend != targets[j]->config.backend &&
942 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700943 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -0700944 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -0700945 pngFilename.append(".png");
tomhudsond968a6f2015-03-26 11:28:06 -0700946 write_canvas_png(targets[j], pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -0700947 }
948
949 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -0700950 // Can't be timed. A warning note has already been printed.
Mike Kleine3631362014-07-15 17:56:37 -0400951 continue;
952 }
953
mtkleinf3723212014-06-25 14:08:00 -0700954 Stats stats(samples.get(), FLAGS_samples);
mtklein1915b622014-08-20 11:45:00 -0700955 log->config(config);
mtklein96289052014-09-10 12:05:59 -0700956 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -0700957 benchStream.fillCurrentOptions(log.get());
tomhudsond968a6f2015-03-26 11:28:06 -0700958 targets[j]->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -0800959 log->metric("min_ms", stats.min);
mtkleine070c2b2014-10-14 08:40:43 -0700960 if (runs++ % FLAGS_flushEvery == 0) {
961 log->flush();
962 }
mtklein60317d0f2014-07-14 11:30:37 -0700963
bsalomon6eb03cc2014-08-07 14:28:50 -0700964 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700965 if (targets.count() == 1) {
966 config = ""; // Only print the config if we run the same bench on more than one.
967 }
mtkleind75c4662015-04-30 07:11:22 -0700968 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
969 , sk_tools::getCurrResidentSetSizeMB()
970 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -0700971 , bench->getUniqueName()
972 , config);
mtkleina189ccd2014-07-14 12:28:47 -0700973 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700974 for (int i = 0; i < FLAGS_samples; i++) {
mtklein55b0ffc2014-07-17 08:38:23 -0700975 SkDebugf("%s ", HUMANIZE(samples[i]));
mtkleinf3723212014-06-25 14:08:00 -0700976 }
mtklein96289052014-09-10 12:05:59 -0700977 SkDebugf("%s\n", bench->getUniqueName());
mtkleinf3723212014-06-25 14:08:00 -0700978 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700979 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700980 config = ""; // Only print the config if we run the same bench on more than one.
981 }
mtklein96289052014-09-10 12:05:59 -0700982 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -0700983 } else {
984 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -0700985 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
986 , sk_tools::getCurrResidentSetSizeMB()
987 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -0700988 , loops
mtklein55b0ffc2014-07-17 08:38:23 -0700989 , HUMANIZE(stats.min)
990 , HUMANIZE(stats.median)
991 , HUMANIZE(stats.mean)
992 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -0700993 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -0700994 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -0700995 , config
mtklein96289052014-09-10 12:05:59 -0700996 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -0700997 );
998 }
bsalomonb12ea412015-02-02 21:19:50 -0800999#if SK_SUPPORT_GPU
1000 if (FLAGS_gpuStats &&
bsalomon06cddec2014-10-24 10:40:50 -07001001 Benchmark::kGPU_Backend == targets[j]->config.backend) {
mtklein6838d852014-10-29 14:15:10 -07001002 gGrFactory->get(targets[j]->config.ctxType)->printCacheStats();
bsalomonb12ea412015-02-02 21:19:50 -08001003 gGrFactory->get(targets[j]->config.ctxType)->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001004 }
1005#endif
mtkleinf3723212014-06-25 14:08:00 -07001006 }
mtkleinbb6a0282014-07-01 08:43:42 -07001007 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -04001008
bsalomon06cddec2014-10-24 10:40:50 -07001009#if SK_SUPPORT_GPU
bsalomon2354f842014-07-28 13:48:36 -07001010 if (FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -07001011 gGrFactory->abandonContexts();
bsalomon2354f842014-07-28 13:48:36 -07001012 }
1013 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -07001014 gGrFactory->destroyContexts();
Mike Klein3944a1d2014-07-15 13:40:19 -04001015 }
bsalomon06cddec2014-10-24 10:40:50 -07001016#endif
mtkleinf3723212014-06-25 14:08:00 -07001017 }
1018
mtkleine1091452014-12-04 10:47:02 -08001019 log->bench("memory_usage", 0,0);
1020 log->config("meta");
1021 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1022
joshualitte0b19d42015-03-26 10:41:02 -07001023#if SK_SUPPORT_GPU
1024 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1025 // SkEventTracer destructor
1026 gGrFactory.reset(NULL);
1027#endif
1028
mtkleinf3723212014-06-25 14:08:00 -07001029 return 0;
1030}
1031
jcgregorio3b27ade2014-11-13 08:06:40 -08001032#if !defined SK_BUILD_FOR_IOS
1033int main(int argc, char** argv) {
1034 SkCommandLineFlags::Parse(argc, argv);
1035 return nanobench_main();
1036}
1037#endif