blob: c33f2c77d2624d6e59a0a2e14848fd35d1b5ad52 [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
mtkleinf3723212014-06-25 14:08:00 -070050__SK_FORCE_IMAGE_DECODER_LINKING;
51
reed53249782014-10-10 09:09:52 -070052static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070053
mtkleinb5110422014-08-07 15:20:02 -070054static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070055#ifdef SK_DEBUG
56 1;
mtkleina189ccd2014-07-14 12:28:47 -070057#else
bsalomon6eb03cc2014-08-07 14:28:50 -070058 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070059#endif
60
bsalomon6eb03cc2014-08-07 14:28:50 -070061static SkString loops_help_txt() {
62 SkString help;
63 help.printf("Number of times to run each bench. Set this to %d to auto-"
64 "tune for each bench. Timings are only reported when auto-tuning.",
65 kAutoTuneLoops);
66 return help;
67}
68
69DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
70
mtkleinf3723212014-06-25 14:08:00 -070071DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
72DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
73DEFINE_double(overheadGoal, 0.0001,
74 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070075DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
76DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070077DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
78 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070079
mtklein60317d0f2014-07-14 11:30:37 -070080DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -070081DEFINE_int32(maxCalibrationAttempts, 3,
82 "Try up to this many times to guess loops for a bench, or skip the bench.");
83DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -070084DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
85DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
joshualitt261c3ad2015-04-27 09:16:57 -070086DEFINE_string(zoom, "1.0,1", "Comma-separated scale,step zoom factors for SKPs.");
mtklein20840502014-08-21 15:51:22 -070087DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
robertphillips5b693772014-11-21 06:19:36 -080088DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
mtkleine070c2b2014-10-14 08:40:43 -070089DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -080090DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -080091DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
mtklein92007582014-08-01 07:46:52 -070092
mtkleinf3723212014-06-25 14:08:00 -070093static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -070094 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -080095 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -070096}
mtklein55b0ffc2014-07-17 08:38:23 -070097#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -070098
tomhudsond968a6f2015-03-26 11:28:06 -070099bool Target::init(SkImageInfo info, Benchmark* bench) {
100 if (Benchmark::kRaster_Backend == config.backend) {
101 this->surface.reset(SkSurface::NewRaster(info));
102 if (!this->surface.get()) {
103 return false;
104 }
105 }
106 return true;
107}
108bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700109 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700110 if (!canvas) {
111 return false;
112 }
113 bmp->setInfo(canvas->imageInfo());
114 if (!canvas->readPixels(bmp, 0, 0)) {
115 SkDebugf("Can't read canvas pixels.\n");
116 return false;
117 }
118 return true;
119}
120
121#if SK_SUPPORT_GPU
122struct GPUTarget : public Target {
123 explicit GPUTarget(const Config& c) : Target(c), gl(NULL) { }
124 SkGLContext* gl;
125
126 void setup() override {
127 this->gl->makeCurrent();
128 // Make sure we're done with whatever came before.
129 SK_GL(*this->gl, Finish());
130 }
131 void endTiming() override {
132 if (this->gl) {
133 SK_GL(*this->gl, Flush());
134 this->gl->swapBuffers();
135 }
136 }
137 void fence() override {
138 SK_GL(*this->gl, Finish());
139 }
140
141 bool needsFrameTiming() const override { return true; }
142 bool init(SkImageInfo info, Benchmark* bench) override {
143 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDistanceFieldFonts_Flag : 0;
144 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
145 this->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(this->config.ctxType),
146 SkSurface::kNo_Budgeted, info,
147 this->config.samples, &props));
148 this->gl = gGrFactory->getGLContext(this->config.ctxType);
149 if (!this->surface.get()) {
150 return false;
151 }
152 return true;
153 }
154 void fillOptions(ResultsWriter* log) override {
155 const GrGLubyte* version;
156 SK_GL_RET(*this->gl, version, GetString(GR_GL_VERSION));
157 log->configOption("GL_VERSION", (const char*)(version));
158
159 SK_GL_RET(*this->gl, version, GetString(GR_GL_RENDERER));
160 log->configOption("GL_RENDERER", (const char*) version);
161
162 SK_GL_RET(*this->gl, version, GetString(GR_GL_VENDOR));
163 log->configOption("GL_VENDOR", (const char*) version);
164
165 SK_GL_RET(*this->gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
166 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
167 }
168};
169
170#endif
171
tomhudson75a0ebb2015-03-27 12:11:44 -0700172static double time(int loops, Benchmark* bench, Target* target) {
173 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700174 if (canvas) {
175 canvas->clear(SK_ColorWHITE);
176 }
mtkleinbb6a0282014-07-01 08:43:42 -0700177 WallTimer timer;
178 timer.start();
tomhudson75a0ebb2015-03-27 12:11:44 -0700179 canvas = target->beginTiming(canvas);
180 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700181 if (canvas) {
182 canvas->flush();
183 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700184 target->endTiming();
mtkleinbb6a0282014-07-01 08:43:42 -0700185 timer.end();
186 return timer.fWall;
187}
188
mtkleinf3723212014-06-25 14:08:00 -0700189static double estimate_timer_overhead() {
190 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700191 for (int i = 0; i < FLAGS_overheadLoops; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700192 WallTimer timer;
193 timer.start();
194 timer.end();
195 overhead += timer.fWall;
mtkleinf3723212014-06-25 14:08:00 -0700196 }
197 return overhead / FLAGS_overheadLoops;
198}
199
reed53249782014-10-10 09:09:52 -0700200static int detect_forever_loops(int loops) {
201 // look for a magic run-forever value
202 if (loops < 0) {
203 loops = SK_MaxS32;
204 }
205 return loops;
206}
207
mtklein55b0ffc2014-07-17 08:38:23 -0700208static int clamp_loops(int loops) {
209 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800210 SkDebugf("ERROR: clamping loops from %d to 1. "
211 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700212 return 1;
213 }
214 if (loops > FLAGS_maxLoops) {
215 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
216 return FLAGS_maxLoops;
217 }
218 return loops;
219}
220
tomhudsond968a6f2015-03-26 11:28:06 -0700221static bool write_canvas_png(Target* target, const SkString& filename) {
222
bsalomon6eb03cc2014-08-07 14:28:50 -0700223 if (filename.isEmpty()) {
224 return false;
225 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700226 if (target->getCanvas() &&
227 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700228 return false;
229 }
tomhudsond968a6f2015-03-26 11:28:06 -0700230
bsalomon6eb03cc2014-08-07 14:28:50 -0700231 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700232
233 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700234 return false;
235 }
tomhudsond968a6f2015-03-26 11:28:06 -0700236
bsalomon6eb03cc2014-08-07 14:28:50 -0700237 SkString dir = SkOSPath::Dirname(filename.c_str());
238 if (!sk_mkdir(dir.c_str())) {
239 SkDebugf("Can't make dir %s.\n", dir.c_str());
240 return false;
241 }
242 SkFILEWStream stream(filename.c_str());
243 if (!stream.isValid()) {
244 SkDebugf("Can't write %s.\n", filename.c_str());
245 return false;
246 }
247 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
248 SkDebugf("Can't encode a PNG.\n");
249 return false;
250 }
251 return true;
252}
253
254static int kFailedLoops = -2;
tomhudson75a0ebb2015-03-27 12:11:44 -0700255static int cpu_bench(const double overhead, Target* target, Benchmark* bench, double* samples) {
mtkleinbb6a0282014-07-01 08:43:42 -0700256 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700257 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700258 int round = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -0700259 if (kAutoTuneLoops == FLAGS_loops) {
260 while (bench_plus_overhead < overhead) {
261 if (round++ == FLAGS_maxCalibrationAttempts) {
262 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700263 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700264 return kFailedLoops;
265 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700266 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700267 }
mtklein2069e222014-08-04 13:57:39 -0700268 }
mtkleinf3723212014-06-25 14:08:00 -0700269
mtkleinbb6a0282014-07-01 08:43:42 -0700270 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700271 // We'll pick N to make timer overhead negligible:
272 //
mtkleinbb6a0282014-07-01 08:43:42 -0700273 // overhead
274 // ------------------------- < FLAGS_overheadGoal
275 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700276 //
mtkleinbb6a0282014-07-01 08:43:42 -0700277 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700278 //
279 // Doing some math, we get:
280 //
mtkleinbb6a0282014-07-01 08:43:42 -0700281 // (overhead / FLAGS_overheadGoal) - overhead
282 // ------------------------------------------ < N
283 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700284 //
285 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700286 int loops = FLAGS_loops;
287 if (kAutoTuneLoops == loops) {
288 const double numer = overhead / FLAGS_overheadGoal - overhead;
289 const double denom = bench_plus_overhead - overhead;
290 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700291 loops = clamp_loops(loops);
292 } else {
293 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700294 }
mtkleinbb6a0282014-07-01 08:43:42 -0700295
296 for (int i = 0; i < FLAGS_samples; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700297 samples[i] = time(loops, bench, target) / loops;
mtkleinbb6a0282014-07-01 08:43:42 -0700298 }
299 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700300}
301
tomhudsond968a6f2015-03-26 11:28:06 -0700302static int gpu_bench(Target* target,
mtkleinbb6a0282014-07-01 08:43:42 -0700303 Benchmark* bench,
mtkleinbb6a0282014-07-01 08:43:42 -0700304 double* samples) {
mtkleinbb6a0282014-07-01 08:43:42 -0700305 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
bsalomon6eb03cc2014-08-07 14:28:50 -0700306 int loops = FLAGS_loops;
307 if (kAutoTuneLoops == loops) {
308 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700309 double elapsed = 0;
310 do {
mtklein527930f2014-11-06 08:04:34 -0800311 if (1<<30 == loops) {
312 // We're about to wrap. Something's wrong with the bench.
313 loops = 0;
314 break;
315 }
mtkleina189ccd2014-07-14 12:28:47 -0700316 loops *= 2;
317 // If the GPU lets frames lag at all, we need to make sure we're timing
318 // _this_ round, not still timing last round. We force this by looping
319 // more times than any reasonable GPU will allow frames to lag.
320 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700321 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700322 }
323 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700324
mtkleina189ccd2014-07-14 12:28:47 -0700325 // We've overshot at least a little. Scale back linearly.
326 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700327 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700328
tomhudsond968a6f2015-03-26 11:28:06 -0700329 // Make sure we're not still timing our calibration.
330 target->fence();
reed53249782014-10-10 09:09:52 -0700331 } else {
332 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700333 }
mtkleinbb6a0282014-07-01 08:43:42 -0700334
335 // Pretty much the same deal as the calibration: do some warmup to make
336 // sure we're timing steady-state pipelined frames.
337 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700338 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700339 }
mtkleinbb6a0282014-07-01 08:43:42 -0700340
341 // Now, actually do the timing!
342 for (int i = 0; i < FLAGS_samples; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700343 samples[i] = time(loops, bench, target) / loops;
mtkleinbb6a0282014-07-01 08:43:42 -0700344 }
tomhudsond968a6f2015-03-26 11:28:06 -0700345
mtkleinbb6a0282014-07-01 08:43:42 -0700346 return loops;
347}
mtkleinbb6a0282014-07-01 08:43:42 -0700348
349static SkString to_lower(const char* str) {
350 SkString lower(str);
351 for (size_t i = 0; i < lower.size(); i++) {
352 lower[i] = tolower(lower[i]);
353 }
354 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700355}
356
bsalomonc2553372014-07-22 13:09:05 -0700357static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700358 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700359 if (to_lower(FLAGS_config[i]).equals(name)) {
360 return true;
mtkleinf3723212014-06-25 14:08:00 -0700361 }
362 }
bsalomonc2553372014-07-22 13:09:05 -0700363 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700364}
365
bsalomonc2553372014-07-22 13:09:05 -0700366#if SK_SUPPORT_GPU
367static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
368 int sampleCnt) {
369 if (!is_cpu_config_allowed(name)) {
370 return false;
371 }
krajcevski69a55602014-08-13 10:46:31 -0700372 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomonc2553372014-07-22 13:09:05 -0700373 return sampleCnt <= ctx->getMaxSampleCount();
374 }
375 return false;
376}
377#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700378
bsalomonc2553372014-07-22 13:09:05 -0700379#if SK_SUPPORT_GPU
380#define kBogusGLContextType GrContextFactory::kNative_GLContextType
381#else
382#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700383#endif
bsalomonc2553372014-07-22 13:09:05 -0700384
385// Append all configs that are enabled and supported.
386static void create_configs(SkTDArray<Config>* configs) {
jvanverth4736e142014-11-07 07:12:46 -0800387 #define CPU_CONFIG(name, backend, color, alpha) \
388 if (is_cpu_config_allowed(#name)) { \
389 Config config = { #name, Benchmark::backend, color, alpha, 0, \
390 kBogusGLContextType, false }; \
391 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700392 }
mtkleine714e752014-07-31 12:13:48 -0700393
mtklein40b32be2014-07-09 08:46:49 -0700394 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700395 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
396 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
397 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700398 }
mtkleinbb6a0282014-07-01 08:43:42 -0700399
400#if SK_SUPPORT_GPU
jvanverth4736e142014-11-07 07:12:46 -0800401 #define GPU_CONFIG(name, ctxType, samples, useDFText) \
bsalomonc2553372014-07-22 13:09:05 -0700402 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
403 Config config = { \
404 #name, \
405 Benchmark::kGPU_Backend, \
406 kN32_SkColorType, \
407 kPremul_SkAlphaType, \
408 samples, \
jvanverth4736e142014-11-07 07:12:46 -0800409 GrContextFactory::ctxType, \
410 useDFText }; \
bsalomonc2553372014-07-22 13:09:05 -0700411 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700412 }
mtkleine714e752014-07-31 12:13:48 -0700413
mtklein40b32be2014-07-09 08:46:49 -0700414 if (FLAGS_gpu) {
jvanverth4736e142014-11-07 07:12:46 -0800415 GPU_CONFIG(gpu, kNative_GLContextType, 0, false)
416 GPU_CONFIG(msaa4, kNative_GLContextType, 4, false)
417 GPU_CONFIG(msaa16, kNative_GLContextType, 16, false)
418 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4, false)
419 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16, false)
420 GPU_CONFIG(gpudft, kNative_GLContextType, 0, true)
421 GPU_CONFIG(debug, kDebug_GLContextType, 0, false)
422 GPU_CONFIG(nullgpu, kNull_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700423#ifdef SK_ANGLE
jvanverth4736e142014-11-07 07:12:46 -0800424 GPU_CONFIG(angle, kANGLE_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700425#endif
mtklein40b32be2014-07-09 08:46:49 -0700426 }
mtkleinbb6a0282014-07-01 08:43:42 -0700427#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700428
429#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
430 if (is_cpu_config_allowed("hwui")) {
431 Config config = { "hwui", Benchmark::kHWUI_Backend, kRGBA_8888_SkColorType,
432 kPremul_SkAlphaType, 0, kBogusGLContextType, false };
433 configs->push(config);
434 }
435#endif
mtkleinf3723212014-06-25 14:08:00 -0700436}
437
bsalomonc2553372014-07-22 13:09:05 -0700438// If bench is enabled for config, returns a Target* for it, otherwise NULL.
439static Target* is_enabled(Benchmark* bench, const Config& config) {
440 if (!bench->isSuitableFor(config.backend)) {
441 return NULL;
442 }
443
reede5ea5002014-09-03 11:54:58 -0700444 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
445 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700446
tomhudsond968a6f2015-03-26 11:28:06 -0700447 Target* target = NULL;
bsalomonc2553372014-07-22 13:09:05 -0700448
tomhudsond968a6f2015-03-26 11:28:06 -0700449 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700450#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700451 case Benchmark::kGPU_Backend:
452 target = new GPUTarget(config);
453 break;
bsalomonc2553372014-07-22 13:09:05 -0700454#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700455#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
456 case Benchmark::kHWUI_Backend:
457 target = new HWUITarget(config, bench);
458 break;
459#endif
460 default:
461 target = new Target(config);
462 break;
463 }
bsalomonc2553372014-07-22 13:09:05 -0700464
tomhudsond968a6f2015-03-26 11:28:06 -0700465 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700466 delete target;
467 return NULL;
468 }
469 return target;
470}
471
472// Creates targets for a benchmark and a set of configs.
473static void create_targets(SkTDArray<Target*>* targets, Benchmark* b,
474 const SkTDArray<Config>& configs) {
475 for (int i = 0; i < configs.count(); ++i) {
476 if (Target* t = is_enabled(b, configs[i])) {
477 targets->push(t);
478 }
mtkleine714e752014-07-31 12:13:48 -0700479
bsalomonc2553372014-07-22 13:09:05 -0700480 }
481}
482
jcgregoriobf5e5232014-07-17 13:14:16 -0700483
mtkleine714e752014-07-31 12:13:48 -0700484class BenchmarkStream {
485public:
mtklein92007582014-08-01 07:46:52 -0700486 BenchmarkStream() : fBenches(BenchRegistry::Head())
487 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700488 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700489 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800490 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800491 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700492 , fCurrentCodec(0)
msarett95f192d2015-02-13 09:05:41 -0800493 , fCurrentImage(0)
494 , fCurrentSubsetImage(0)
495 , fCurrentColorType(0)
joshualitt261c3ad2015-04-27 09:16:57 -0700496 , fCurrentAnimSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800497 , fDivisor(2) {
mtklein92007582014-08-01 07:46:52 -0700498 for (int i = 0; i < FLAGS_skps.count(); i++) {
499 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
500 fSKPs.push_back() = FLAGS_skps[i];
501 } else {
502 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
503 SkString path;
504 while (it.next(&path)) {
505 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
506 }
507 }
508 }
mtkleine714e752014-07-31 12:13:48 -0700509
mtklein92007582014-08-01 07:46:52 -0700510 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
511 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
512 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
513 exit(1);
514 }
515
516 for (int i = 0; i < FLAGS_scales.count(); i++) {
517 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
518 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
519 exit(1);
520 }
521 }
robertphillips5b693772014-11-21 06:19:36 -0800522
joshualitt261c3ad2015-04-27 09:16:57 -0700523 if (2 != sscanf(FLAGS_zoom[0], "%f,%d", &fZoomScale, &fZoomSteps)) {
524 SkDebugf("Can't parse %s from --zoom as a scale,step.\n", FLAGS_zoom[0]);
525 exit(1);
526 }
527
robertphillips5b693772014-11-21 06:19:36 -0800528 fUseMPDs.push_back() = false;
529 if (FLAGS_mpd) {
530 fUseMPDs.push_back() = true;
531 }
mtklein95553d92015-03-12 08:24:21 -0700532
msarett95f192d2015-02-13 09:05:41 -0800533 // Prepare the images for decoding
534 for (int i = 0; i < FLAGS_images.count(); i++) {
535 const char* flag = FLAGS_images[i];
536 if (sk_isdir(flag)) {
537 // If the value passed in is a directory, add all the images
538 SkOSFile::Iter it(flag);
539 SkString file;
540 while (it.next(&file)) {
541 fImages.push_back() = SkOSPath::Join(flag, file.c_str());
542 }
543 } else if (sk_exists(flag)) {
544 // Also add the value if it is a single image
545 fImages.push_back() = flag;
546 }
547 }
mtklein95553d92015-03-12 08:24:21 -0700548
msarett95f192d2015-02-13 09:05:41 -0800549 // Choose the candidate color types for image decoding
550 const SkColorType colorTypes[] =
scroggo21027992015-04-02 13:22:38 -0700551 { kN32_SkColorType, kRGB_565_SkColorType, kAlpha_8_SkColorType, kIndex_8_SkColorType };
msarett95f192d2015-02-13 09:05:41 -0800552 fColorTypes.push_back_n(SK_ARRAY_COUNT(colorTypes), colorTypes);
mtklein92007582014-08-01 07:46:52 -0700553 }
554
mtkleinfd731ce2014-09-10 12:19:30 -0700555 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
556 // Not strictly necessary, as it will be checked again later,
557 // but helps to avoid a lot of pointless work if we're going to skip it.
558 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
559 return false;
560 }
561
scroggoa1193e42015-01-21 12:09:53 -0800562 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
mtkleinfd731ce2014-09-10 12:19:30 -0700563 if (stream.get() == NULL) {
564 SkDebugf("Could not read %s.\n", path);
565 return false;
566 }
567
mtklein57f27bd2015-02-09 11:58:41 -0800568 pic->reset(SkPicture::CreateFromStream(stream.get()));
mtkleinfd731ce2014-09-10 12:19:30 -0700569 if (pic->get() == NULL) {
570 SkDebugf("Could not read %s as an SkPicture.\n", path);
571 return false;
572 }
573 return true;
574 }
575
mtklein92007582014-08-01 07:46:52 -0700576 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700577 if (fBenches) {
578 Benchmark* bench = fBenches->factory()(NULL);
579 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700580 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700581 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700582 return bench;
583 }
mtklein92007582014-08-01 07:46:52 -0700584
mtkleine714e752014-07-31 12:13:48 -0700585 while (fGMs) {
586 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
587 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800588 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700589 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700590 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700591 return SkNEW_ARGS(GMBench, (gm.detach()));
592 }
593 }
mtklein92007582014-08-01 07:46:52 -0700594
mtkleinfd731ce2014-09-10 12:19:30 -0700595 // First add all .skps as RecordingBenches.
596 while (fCurrentRecording < fSKPs.count()) {
597 const SkString& path = fSKPs[fCurrentRecording++];
598 SkAutoTUnref<SkPicture> pic;
599 if (!ReadPicture(path.c_str(), &pic)) {
600 continue;
601 }
602 SkString name = SkOSPath::Basename(path.c_str());
603 fSourceType = "skp";
604 fBenchType = "recording";
bsalomon0aa5cea2014-12-15 09:13:35 -0800605 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic));
mtklein051e56d2014-12-04 08:46:51 -0800606 fSKPOps = pic->approximateOpCount();
mtkleinfd731ce2014-09-10 12:19:30 -0700607 return SkNEW_ARGS(RecordingBench, (name.c_str(), pic.get(), FLAGS_bbh));
608 }
609
610 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700611 while (fCurrentScale < fScales.count()) {
612 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800613 const SkString& path = fSKPs[fCurrentSKP];
mtkleinfd731ce2014-09-10 12:19:30 -0700614 SkAutoTUnref<SkPicture> pic;
615 if (!ReadPicture(path.c_str(), &pic)) {
robertphillips5b693772014-11-21 06:19:36 -0800616 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700617 continue;
618 }
robertphillips5b693772014-11-21 06:19:36 -0800619
620 while (fCurrentUseMPD < fUseMPDs.count()) {
621 if (FLAGS_bbh) {
622 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
623 SkRTreeFactory factory;
624 SkPictureRecorder recorder;
625 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
626 pic->playback(recorder.beginRecording(pic->cullRect().width(),
627 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800628 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800629 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
robertphillips5b693772014-11-21 06:19:36 -0800630 pic.reset(recorder.endRecording());
631 }
632 SkString name = SkOSPath::Basename(path.c_str());
633 fSourceType = "skp";
634 fBenchType = "playback";
635 return SkNEW_ARGS(SKPBench,
joshualitt261c3ad2015-04-27 09:16:57 -0700636 (name.c_str(), pic.get(), fClip,
637 fScales[fCurrentScale], fUseMPDs[fCurrentUseMPD++]));
638
mtklein20840502014-08-21 15:51:22 -0700639 }
robertphillips5b693772014-11-21 06:19:36 -0800640 fCurrentUseMPD = 0;
641 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700642 }
643 fCurrentSKP = 0;
644 fCurrentScale++;
645 }
646
joshualitt261c3ad2015-04-27 09:16:57 -0700647 // Now loop over each skp again if we have an animation
648 if (fZoomScale != 1.0f && fZoomSteps != 1) {
649 while (fCurrentAnimSKP < fSKPs.count()) {
650 const SkString& path = fSKPs[fCurrentAnimSKP];
651 SkAutoTUnref<SkPicture> pic;
652 if (!ReadPicture(path.c_str(), &pic)) {
653 fCurrentAnimSKP++;
654 continue;
655 }
656
657 fCurrentAnimSKP++;
658 SkString name = SkOSPath::Basename(path.c_str());
659 SkMatrix anim = SkMatrix::I();
660 anim.setScale(fZoomScale, fZoomScale);
661 return SkNEW_ARGS(SKPAnimationBench, (name.c_str(), pic.get(), fClip, anim,
662 fZoomSteps));
663 }
664 }
665
666
scroggo60869a42015-04-01 12:09:17 -0700667 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
668 const SkString& path = fImages[fCurrentCodec];
669 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
670 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700671 if (!codec) {
672 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700673 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700674 continue;
675 }
scroggo21027992015-04-02 13:22:38 -0700676
scroggo60869a42015-04-01 12:09:17 -0700677 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700678 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo60869a42015-04-01 12:09:17 -0700679 fCurrentColorType++;
scroggo21027992015-04-02 13:22:38 -0700680
scroggo60869a42015-04-01 12:09:17 -0700681 // Make sure we can decode to this color type.
scroggo60869a42015-04-01 12:09:17 -0700682 SkImageInfo info = codec->getInfo().makeColorType(colorType);
scroggo21027992015-04-02 13:22:38 -0700683 SkAlphaType alphaType;
684 if (!SkColorTypeValidateAlphaType(colorType, info.alphaType(),
685 &alphaType)) {
686 continue;
687 }
688 if (alphaType != info.alphaType()) {
689 info = info.makeAlphaType(alphaType);
690 }
691
692 const size_t rowBytes = info.minRowBytes();
693 SkAutoMalloc storage(info.getSafeSize(rowBytes));
694
695 // Used if fCurrentColorType is kIndex_8_SkColorType
696 int colorCount = 256;
697 SkPMColor colors[256];
698
scroggo60869a42015-04-01 12:09:17 -0700699 const SkImageGenerator::Result result = codec->getPixels(
scroggo21027992015-04-02 13:22:38 -0700700 info, storage.get(), rowBytes, NULL, colors,
701 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700702 switch (result) {
703 case SkImageGenerator::kSuccess:
704 case SkImageGenerator::kIncompleteInput:
705 return new CodecBench(SkOSPath::Basename(path.c_str()),
706 encoded, colorType);
707 case SkImageGenerator::kInvalidConversion:
708 // This is okay. Not all conversions are valid.
709 break;
scroggo60869a42015-04-01 12:09:17 -0700710 default:
711 // This represents some sort of failure.
712 SkASSERT(false);
713 break;
714 }
715 }
716 fCurrentColorType = 0;
717 }
718
msarett95f192d2015-02-13 09:05:41 -0800719 // Run the DecodingBenches
720 while (fCurrentImage < fImages.count()) {
721 while (fCurrentColorType < fColorTypes.count()) {
722 const SkString& path = fImages[fCurrentImage];
723 SkColorType colorType = fColorTypes[fCurrentColorType];
724 fCurrentColorType++;
scroggo60869a42015-04-01 12:09:17 -0700725 // Check if the image decodes to the right color type
726 // before creating the benchmark
msarett95f192d2015-02-13 09:05:41 -0800727 SkBitmap bitmap;
728 if (SkImageDecoder::DecodeFile(path.c_str(), &bitmap,
scroggo60869a42015-04-01 12:09:17 -0700729 colorType, SkImageDecoder::kDecodePixels_Mode)
730 && bitmap.colorType() == colorType) {
msarett95f192d2015-02-13 09:05:41 -0800731 return new DecodingBench(path, colorType);
732 }
733 }
734 fCurrentColorType = 0;
735 fCurrentImage++;
736 }
737
738 // Run the DecodingSubsetBenches
739 while (fCurrentSubsetImage < fImages.count()) {
740 while (fCurrentColorType < fColorTypes.count()) {
741 const SkString& path = fImages[fCurrentSubsetImage];
742 SkColorType colorType = fColorTypes[fCurrentColorType];
743 fCurrentColorType++;
744 // Check if the image decodes before creating the benchmark
745 SkAutoTUnref<SkData> encoded(
746 SkData::NewFromFileName(path.c_str()));
747 SkAutoTDelete<SkMemoryStream> stream(
748 new SkMemoryStream(encoded));
749 SkAutoTDelete<SkImageDecoder>
750 decoder(SkImageDecoder::Factory(stream.get()));
751 if (!decoder) {
752 SkDebugf("Cannot find decoder for %s\n", path.c_str());
753 } else {
754 stream->rewind();
755 int w, h;
756 bool success;
757 if (!decoder->buildTileIndex(stream.detach(), &w, &h)
758 || w*h == 1) {
759 // This is not an error, but in this case we still
760 // do not want to run the benchmark.
761 success = false;
762 } else if (fDivisor > w || fDivisor > h) {
763 SkDebugf("Divisor %d is too big for %s %dx%d\n",
764 fDivisor, path.c_str(), w, h);
765 success = false;
766 } else {
767 const int sW = w / fDivisor;
768 const int sH = h / fDivisor;
769 SkBitmap bitmap;
770 success = true;
771 for (int y = 0; y < h; y += sH) {
772 for (int x = 0; x < w; x += sW) {
773 SkIRect rect = SkIRect::MakeXYWH(x, y, sW, sH);
774 success &= decoder->decodeSubset(&bitmap, rect,
775 colorType);
776 }
777 }
778 }
779 // Create the benchmark if successful
780 if (success) {
781 return new DecodingSubsetBench(path, colorType,
782 fDivisor);
783 }
784 }
785 }
786 fCurrentColorType = 0;
787 fCurrentSubsetImage++;
788 }
789
mtkleine714e752014-07-31 12:13:48 -0700790 return NULL;
791 }
mtklein92007582014-08-01 07:46:52 -0700792
793 void fillCurrentOptions(ResultsWriter* log) const {
794 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700795 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700796 if (0 == strcmp(fSourceType, "skp")) {
797 log->configOption("clip",
798 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
799 fClip.fRight, fClip.fBottom).c_str());
800 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -0800801 if (fCurrentUseMPD > 0) {
802 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
803 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
804 }
mtklein92007582014-08-01 07:46:52 -0700805 }
mtklein051e56d2014-12-04 08:46:51 -0800806 if (0 == strcmp(fBenchType, "recording")) {
807 log->metric("bytes", fSKPBytes);
808 log->metric("ops", fSKPOps);
809 }
mtklein92007582014-08-01 07:46:52 -0700810 }
811
mtkleine714e752014-07-31 12:13:48 -0700812private:
813 const BenchRegistry* fBenches;
814 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700815 SkIRect fClip;
816 SkTArray<SkScalar> fScales;
817 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -0800818 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -0800819 SkTArray<SkString> fImages;
820 SkTArray<SkColorType> fColorTypes;
joshualitt261c3ad2015-04-27 09:16:57 -0700821 SkScalar fZoomScale;
822 int fZoomSteps;
mtklein92007582014-08-01 07:46:52 -0700823
mtklein051e56d2014-12-04 08:46:51 -0800824 double fSKPBytes, fSKPOps;
825
mtkleinfd731ce2014-09-10 12:19:30 -0700826 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
827 const char* fBenchType; // How we bench it: micro, recording, playback, ...
828 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700829 int fCurrentScale;
830 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -0800831 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -0700832 int fCurrentCodec;
msarett95f192d2015-02-13 09:05:41 -0800833 int fCurrentImage;
834 int fCurrentSubsetImage;
835 int fCurrentColorType;
joshualitt261c3ad2015-04-27 09:16:57 -0700836 int fCurrentAnimSKP;
msarett95f192d2015-02-13 09:05:41 -0800837 const int fDivisor;
mtkleine714e752014-07-31 12:13:48 -0700838};
839
jcgregorio3b27ade2014-11-13 08:06:40 -0800840int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700841int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800842 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -0700843 SkAutoGraphics ag;
mtklein4f108442014-12-03 13:07:39 -0800844 SkTaskGroup::Enabler enabled;
mtkleinf3723212014-06-25 14:08:00 -0700845
krajcevski69a55602014-08-13 10:46:31 -0700846#if SK_SUPPORT_GPU
krajcevski12b35442014-08-13 12:06:26 -0700847 GrContext::Options grContextOpts;
848 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
849 gGrFactory.reset(SkNEW_ARGS(GrContextFactory, (grContextOpts)));
krajcevski69a55602014-08-13 10:46:31 -0700850#endif
851
bsalomon06cddec2014-10-24 10:40:50 -0700852 if (FLAGS_veryVerbose) {
853 FLAGS_verbose = true;
854 }
855
bsalomon6eb03cc2014-08-07 14:28:50 -0700856 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700857 FLAGS_samples = 1;
858 FLAGS_gpuFrameLag = 0;
859 }
860
bsalomon6eb03cc2014-08-07 14:28:50 -0700861 if (!FLAGS_writePath.isEmpty()) {
862 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
863 if (!sk_mkdir(FLAGS_writePath[0])) {
864 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
865 FLAGS_writePath.set(0, NULL);
866 }
867 }
868
mtklein1915b622014-08-20 11:45:00 -0700869 SkAutoTDelete<ResultsWriter> log(SkNEW(ResultsWriter));
mtklein60317d0f2014-07-14 11:30:37 -0700870 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700871 log.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0])));
mtklein60317d0f2014-07-14 11:30:37 -0700872 }
mtklein1915b622014-08-20 11:45:00 -0700873
874 if (1 == FLAGS_properties.count() % 2) {
875 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
876 return 1;
877 }
878 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
879 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
880 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700881
882 if (1 == FLAGS_key.count() % 2) {
883 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
884 return 1;
885 }
886 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -0700887 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -0700888 }
mtklein60317d0f2014-07-14 11:30:37 -0700889
mtkleinf3723212014-06-25 14:08:00 -0700890 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -0700891 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -0400892
mtkleinbb6a0282014-07-01 08:43:42 -0700893 SkAutoTMalloc<double> samples(FLAGS_samples);
894
bsalomon6eb03cc2014-08-07 14:28:50 -0700895 if (kAutoTuneLoops != FLAGS_loops) {
896 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleina189ccd2014-07-14 12:28:47 -0700897 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700898 // No header.
899 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700900 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700901 } else {
qiankun.miao8247ec32014-09-09 19:24:36 -0700902 SkDebugf("maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
903 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -0700904 }
905
bsalomonc2553372014-07-22 13:09:05 -0700906 SkTDArray<Config> configs;
907 create_configs(&configs);
908
mtkleine070c2b2014-10-14 08:40:43 -0700909 int runs = 0;
mtklein92007582014-08-01 07:46:52 -0700910 BenchmarkStream benchStream;
911 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -0700912 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -0700913 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -0700914 continue;
915 }
916
mtkleinbb6a0282014-07-01 08:43:42 -0700917 SkTDArray<Target*> targets;
bsalomonc2553372014-07-22 13:09:05 -0700918 create_targets(&targets, bench.get(), configs);
mtkleinf3723212014-06-25 14:08:00 -0700919
jcgregoriobf5e5232014-07-17 13:14:16 -0700920 if (!targets.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -0700921 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
jcgregoriobf5e5232014-07-17 13:14:16 -0700922 bench->preDraw();
923 }
mtkleinbb6a0282014-07-01 08:43:42 -0700924 for (int j = 0; j < targets.count(); j++) {
tomhudsond968a6f2015-03-26 11:28:06 -0700925 // During HWUI output this canvas may be NULL.
tomhudson75a0ebb2015-03-27 12:11:44 -0700926 SkCanvas* canvas = targets[j]->getCanvas();
bsalomonc2553372014-07-22 13:09:05 -0700927 const char* config = targets[j]->config.name;
mtkleinf3723212014-06-25 14:08:00 -0700928
tomhudsond968a6f2015-03-26 11:28:06 -0700929 targets[j]->setup();
robertphillips5b693772014-11-21 06:19:36 -0800930 bench->perCanvasPreDraw(canvas);
931
mtkleinbb6a0282014-07-01 08:43:42 -0700932 const int loops =
tomhudsond968a6f2015-03-26 11:28:06 -0700933 targets[j]->needsFrameTiming()
tomhudson75a0ebb2015-03-27 12:11:44 -0700934 ? gpu_bench(targets[j], bench.get(), samples.get())
935 : cpu_bench(overhead, targets[j], bench.get(), samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700936
robertphillips5b693772014-11-21 06:19:36 -0800937 bench->perCanvasPostDraw(canvas);
938
tomhudsond968a6f2015-03-26 11:28:06 -0700939 if (Benchmark::kNonRendering_Backend != targets[j]->config.backend &&
940 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700941 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -0700942 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -0700943 pngFilename.append(".png");
tomhudsond968a6f2015-03-26 11:28:06 -0700944 write_canvas_png(targets[j], pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -0700945 }
946
947 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -0700948 // Can't be timed. A warning note has already been printed.
Mike Kleine3631362014-07-15 17:56:37 -0400949 continue;
950 }
951
mtkleinf3723212014-06-25 14:08:00 -0700952 Stats stats(samples.get(), FLAGS_samples);
mtklein1915b622014-08-20 11:45:00 -0700953 log->config(config);
mtklein96289052014-09-10 12:05:59 -0700954 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -0700955 benchStream.fillCurrentOptions(log.get());
tomhudsond968a6f2015-03-26 11:28:06 -0700956 targets[j]->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -0800957 log->metric("min_ms", stats.min);
mtkleine070c2b2014-10-14 08:40:43 -0700958 if (runs++ % FLAGS_flushEvery == 0) {
959 log->flush();
960 }
mtklein60317d0f2014-07-14 11:30:37 -0700961
bsalomon6eb03cc2014-08-07 14:28:50 -0700962 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700963 if (targets.count() == 1) {
964 config = ""; // Only print the config if we run the same bench on more than one.
965 }
mtklein53d25622014-09-18 07:39:42 -0700966 SkDebugf("%4dM\t%s\t%s\n"
mtklein95553d92015-03-12 08:24:21 -0700967 , sk_tools::getBestResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -0700968 , bench->getUniqueName()
969 , config);
mtkleina189ccd2014-07-14 12:28:47 -0700970 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700971 for (int i = 0; i < FLAGS_samples; i++) {
mtklein55b0ffc2014-07-17 08:38:23 -0700972 SkDebugf("%s ", HUMANIZE(samples[i]));
mtkleinf3723212014-06-25 14:08:00 -0700973 }
mtklein96289052014-09-10 12:05:59 -0700974 SkDebugf("%s\n", bench->getUniqueName());
mtkleinf3723212014-06-25 14:08:00 -0700975 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700976 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700977 config = ""; // Only print the config if we run the same bench on more than one.
978 }
mtklein96289052014-09-10 12:05:59 -0700979 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -0700980 } else {
981 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleinafb43792014-08-19 15:55:55 -0700982 SkDebugf("%4dM\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
mtklein95553d92015-03-12 08:24:21 -0700983 , sk_tools::getBestResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -0700984 , loops
mtklein55b0ffc2014-07-17 08:38:23 -0700985 , HUMANIZE(stats.min)
986 , HUMANIZE(stats.median)
987 , HUMANIZE(stats.mean)
988 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -0700989 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -0700990 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -0700991 , config
mtklein96289052014-09-10 12:05:59 -0700992 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -0700993 );
994 }
bsalomonb12ea412015-02-02 21:19:50 -0800995#if SK_SUPPORT_GPU
996 if (FLAGS_gpuStats &&
bsalomon06cddec2014-10-24 10:40:50 -0700997 Benchmark::kGPU_Backend == targets[j]->config.backend) {
mtklein6838d852014-10-29 14:15:10 -0700998 gGrFactory->get(targets[j]->config.ctxType)->printCacheStats();
bsalomonb12ea412015-02-02 21:19:50 -0800999 gGrFactory->get(targets[j]->config.ctxType)->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001000 }
1001#endif
mtkleinf3723212014-06-25 14:08:00 -07001002 }
mtkleinbb6a0282014-07-01 08:43:42 -07001003 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -04001004
bsalomon06cddec2014-10-24 10:40:50 -07001005#if SK_SUPPORT_GPU
bsalomon2354f842014-07-28 13:48:36 -07001006 if (FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -07001007 gGrFactory->abandonContexts();
bsalomon2354f842014-07-28 13:48:36 -07001008 }
1009 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -07001010 gGrFactory->destroyContexts();
Mike Klein3944a1d2014-07-15 13:40:19 -04001011 }
bsalomon06cddec2014-10-24 10:40:50 -07001012#endif
mtkleinf3723212014-06-25 14:08:00 -07001013 }
1014
mtkleine1091452014-12-04 10:47:02 -08001015 log->bench("memory_usage", 0,0);
1016 log->config("meta");
1017 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1018
joshualitte0b19d42015-03-26 10:41:02 -07001019#if SK_SUPPORT_GPU
1020 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1021 // SkEventTracer destructor
1022 gGrFactory.reset(NULL);
1023#endif
1024
mtkleinf3723212014-06-25 14:08:00 -07001025 return 0;
1026}
1027
jcgregorio3b27ade2014-11-13 08:06:40 -08001028#if !defined SK_BUILD_FOR_IOS
1029int main(int argc, char** argv) {
1030 SkCommandLineFlags::Parse(argc, argv);
1031 return nanobench_main();
1032}
1033#endif