blob: cd15580afbbd14d7fd477188822b9c030d9c7ef3 [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"
bsalomon76228632015-05-29 08:02:10 -070046 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070047 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070048 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070049#endif
50
bsalomon682c2692015-05-22 14:01:46 -070051 struct GrContextOptions;
52
mtkleinf3723212014-06-25 14:08:00 -070053__SK_FORCE_IMAGE_DECODER_LINKING;
54
reed53249782014-10-10 09:09:52 -070055static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070056
mtkleinb5110422014-08-07 15:20:02 -070057static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070058#ifdef SK_DEBUG
59 1;
mtkleina189ccd2014-07-14 12:28:47 -070060#else
bsalomon6eb03cc2014-08-07 14:28:50 -070061 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070062#endif
63
bsalomon6eb03cc2014-08-07 14:28:50 -070064static SkString loops_help_txt() {
65 SkString help;
66 help.printf("Number of times to run each bench. Set this to %d to auto-"
67 "tune for each bench. Timings are only reported when auto-tuning.",
68 kAutoTuneLoops);
69 return help;
70}
71
72DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
73
mtkleinf3723212014-06-25 14:08:00 -070074DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
75DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
76DEFINE_double(overheadGoal, 0.0001,
77 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070078DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
79DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070080DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
81 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070082
mtklein60317d0f2014-07-14 11:30:37 -070083DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -070084DEFINE_int32(maxCalibrationAttempts, 3,
85 "Try up to this many times to guess loops for a bench, or skip the bench.");
86DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -070087DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
88DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
joshualitt261c3ad2015-04-27 09:16:57 -070089DEFINE_string(zoom, "1.0,1", "Comma-separated scale,step zoom factors for SKPs.");
mtklein20840502014-08-21 15:51:22 -070090DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
robertphillips5b693772014-11-21 06:19:36 -080091DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
mtkleine070c2b2014-10-14 08:40:43 -070092DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -080093DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -080094DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
mtklein92007582014-08-01 07:46:52 -070095
mtkleinf3723212014-06-25 14:08:00 -070096static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -070097 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -080098 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -070099}
mtklein55b0ffc2014-07-17 08:38:23 -0700100#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700101
tomhudsond968a6f2015-03-26 11:28:06 -0700102bool Target::init(SkImageInfo info, Benchmark* bench) {
103 if (Benchmark::kRaster_Backend == config.backend) {
104 this->surface.reset(SkSurface::NewRaster(info));
105 if (!this->surface.get()) {
106 return false;
107 }
108 }
109 return true;
110}
111bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700112 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700113 if (!canvas) {
114 return false;
115 }
116 bmp->setInfo(canvas->imageInfo());
117 if (!canvas->readPixels(bmp, 0, 0)) {
118 SkDebugf("Can't read canvas pixels.\n");
119 return false;
120 }
121 return true;
122}
123
124#if SK_SUPPORT_GPU
125struct GPUTarget : public Target {
126 explicit GPUTarget(const Config& c) : Target(c), gl(NULL) { }
127 SkGLContext* gl;
128
129 void setup() override {
130 this->gl->makeCurrent();
131 // Make sure we're done with whatever came before.
132 SK_GL(*this->gl, Finish());
133 }
134 void endTiming() override {
135 if (this->gl) {
136 SK_GL(*this->gl, Flush());
137 this->gl->swapBuffers();
138 }
139 }
140 void fence() override {
141 SK_GL(*this->gl, Finish());
142 }
mtkleind75c4662015-04-30 07:11:22 -0700143
tomhudsond968a6f2015-03-26 11:28:06 -0700144 bool needsFrameTiming() const override { return true; }
145 bool init(SkImageInfo info, Benchmark* bench) override {
146 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDistanceFieldFonts_Flag : 0;
147 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
148 this->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(this->config.ctxType),
149 SkSurface::kNo_Budgeted, info,
150 this->config.samples, &props));
151 this->gl = gGrFactory->getGLContext(this->config.ctxType);
152 if (!this->surface.get()) {
153 return false;
154 }
155 return true;
156 }
157 void fillOptions(ResultsWriter* log) override {
158 const GrGLubyte* version;
159 SK_GL_RET(*this->gl, version, GetString(GR_GL_VERSION));
160 log->configOption("GL_VERSION", (const char*)(version));
161
162 SK_GL_RET(*this->gl, version, GetString(GR_GL_RENDERER));
163 log->configOption("GL_RENDERER", (const char*) version);
164
165 SK_GL_RET(*this->gl, version, GetString(GR_GL_VENDOR));
166 log->configOption("GL_VENDOR", (const char*) version);
167
168 SK_GL_RET(*this->gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
169 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
170 }
171};
mtkleind75c4662015-04-30 07:11:22 -0700172
tomhudsond968a6f2015-03-26 11:28:06 -0700173#endif
174
tomhudson75a0ebb2015-03-27 12:11:44 -0700175static double time(int loops, Benchmark* bench, Target* target) {
176 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700177 if (canvas) {
178 canvas->clear(SK_ColorWHITE);
179 }
mtkleinbb6a0282014-07-01 08:43:42 -0700180 WallTimer timer;
181 timer.start();
tomhudson75a0ebb2015-03-27 12:11:44 -0700182 canvas = target->beginTiming(canvas);
183 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700184 if (canvas) {
185 canvas->flush();
186 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700187 target->endTiming();
mtkleinbb6a0282014-07-01 08:43:42 -0700188 timer.end();
189 return timer.fWall;
190}
191
mtkleinf3723212014-06-25 14:08:00 -0700192static double estimate_timer_overhead() {
193 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700194 for (int i = 0; i < FLAGS_overheadLoops; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700195 WallTimer timer;
196 timer.start();
197 timer.end();
198 overhead += timer.fWall;
mtkleinf3723212014-06-25 14:08:00 -0700199 }
200 return overhead / FLAGS_overheadLoops;
201}
202
reed53249782014-10-10 09:09:52 -0700203static int detect_forever_loops(int loops) {
204 // look for a magic run-forever value
205 if (loops < 0) {
206 loops = SK_MaxS32;
207 }
208 return loops;
209}
210
mtklein55b0ffc2014-07-17 08:38:23 -0700211static int clamp_loops(int loops) {
212 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800213 SkDebugf("ERROR: clamping loops from %d to 1. "
214 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700215 return 1;
216 }
217 if (loops > FLAGS_maxLoops) {
218 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
219 return FLAGS_maxLoops;
220 }
221 return loops;
222}
223
tomhudsond968a6f2015-03-26 11:28:06 -0700224static bool write_canvas_png(Target* target, const SkString& filename) {
225
bsalomon6eb03cc2014-08-07 14:28:50 -0700226 if (filename.isEmpty()) {
227 return false;
228 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700229 if (target->getCanvas() &&
230 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700231 return false;
232 }
tomhudsond968a6f2015-03-26 11:28:06 -0700233
bsalomon6eb03cc2014-08-07 14:28:50 -0700234 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700235
236 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700237 return false;
238 }
tomhudsond968a6f2015-03-26 11:28:06 -0700239
bsalomon6eb03cc2014-08-07 14:28:50 -0700240 SkString dir = SkOSPath::Dirname(filename.c_str());
241 if (!sk_mkdir(dir.c_str())) {
242 SkDebugf("Can't make dir %s.\n", dir.c_str());
243 return false;
244 }
245 SkFILEWStream stream(filename.c_str());
246 if (!stream.isValid()) {
247 SkDebugf("Can't write %s.\n", filename.c_str());
248 return false;
249 }
250 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
251 SkDebugf("Can't encode a PNG.\n");
252 return false;
253 }
254 return true;
255}
256
257static int kFailedLoops = -2;
tomhudson75a0ebb2015-03-27 12:11:44 -0700258static int cpu_bench(const double overhead, Target* target, Benchmark* bench, double* samples) {
mtkleinbb6a0282014-07-01 08:43:42 -0700259 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700260 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700261 int round = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -0700262 if (kAutoTuneLoops == FLAGS_loops) {
263 while (bench_plus_overhead < overhead) {
264 if (round++ == FLAGS_maxCalibrationAttempts) {
265 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700266 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700267 return kFailedLoops;
268 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700269 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700270 }
mtklein2069e222014-08-04 13:57:39 -0700271 }
mtkleinf3723212014-06-25 14:08:00 -0700272
mtkleinbb6a0282014-07-01 08:43:42 -0700273 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700274 // We'll pick N to make timer overhead negligible:
275 //
mtkleinbb6a0282014-07-01 08:43:42 -0700276 // overhead
277 // ------------------------- < FLAGS_overheadGoal
278 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700279 //
mtkleinbb6a0282014-07-01 08:43:42 -0700280 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700281 //
282 // Doing some math, we get:
283 //
mtkleinbb6a0282014-07-01 08:43:42 -0700284 // (overhead / FLAGS_overheadGoal) - overhead
285 // ------------------------------------------ < N
286 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700287 //
288 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700289 int loops = FLAGS_loops;
290 if (kAutoTuneLoops == loops) {
291 const double numer = overhead / FLAGS_overheadGoal - overhead;
292 const double denom = bench_plus_overhead - overhead;
293 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700294 loops = clamp_loops(loops);
295 } else {
296 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700297 }
mtkleinbb6a0282014-07-01 08:43:42 -0700298
299 for (int i = 0; i < FLAGS_samples; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700300 samples[i] = time(loops, bench, target) / loops;
mtkleinbb6a0282014-07-01 08:43:42 -0700301 }
302 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700303}
304
tomhudsond968a6f2015-03-26 11:28:06 -0700305static int gpu_bench(Target* target,
mtkleinbb6a0282014-07-01 08:43:42 -0700306 Benchmark* bench,
mtkleinbb6a0282014-07-01 08:43:42 -0700307 double* samples) {
mtkleinbb6a0282014-07-01 08:43:42 -0700308 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
bsalomon6eb03cc2014-08-07 14:28:50 -0700309 int loops = FLAGS_loops;
310 if (kAutoTuneLoops == loops) {
311 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700312 double elapsed = 0;
313 do {
mtklein527930f2014-11-06 08:04:34 -0800314 if (1<<30 == loops) {
315 // We're about to wrap. Something's wrong with the bench.
316 loops = 0;
317 break;
318 }
mtkleina189ccd2014-07-14 12:28:47 -0700319 loops *= 2;
320 // If the GPU lets frames lag at all, we need to make sure we're timing
321 // _this_ round, not still timing last round. We force this by looping
322 // more times than any reasonable GPU will allow frames to lag.
323 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700324 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700325 }
326 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700327
mtkleina189ccd2014-07-14 12:28:47 -0700328 // We've overshot at least a little. Scale back linearly.
329 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700330 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700331
tomhudsond968a6f2015-03-26 11:28:06 -0700332 // Make sure we're not still timing our calibration.
333 target->fence();
reed53249782014-10-10 09:09:52 -0700334 } else {
335 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700336 }
mtkleinbb6a0282014-07-01 08:43:42 -0700337
338 // Pretty much the same deal as the calibration: do some warmup to make
339 // sure we're timing steady-state pipelined frames.
340 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700341 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700342 }
mtkleinbb6a0282014-07-01 08:43:42 -0700343
344 // Now, actually do the timing!
345 for (int i = 0; i < FLAGS_samples; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700346 samples[i] = time(loops, bench, target) / loops;
mtkleinbb6a0282014-07-01 08:43:42 -0700347 }
tomhudsond968a6f2015-03-26 11:28:06 -0700348
mtkleinbb6a0282014-07-01 08:43:42 -0700349 return loops;
350}
mtkleinbb6a0282014-07-01 08:43:42 -0700351
352static SkString to_lower(const char* str) {
353 SkString lower(str);
354 for (size_t i = 0; i < lower.size(); i++) {
355 lower[i] = tolower(lower[i]);
356 }
357 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700358}
359
bsalomonc2553372014-07-22 13:09:05 -0700360static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700361 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700362 if (to_lower(FLAGS_config[i]).equals(name)) {
363 return true;
mtkleinf3723212014-06-25 14:08:00 -0700364 }
365 }
bsalomonc2553372014-07-22 13:09:05 -0700366 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700367}
368
bsalomonc2553372014-07-22 13:09:05 -0700369#if SK_SUPPORT_GPU
370static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
371 int sampleCnt) {
372 if (!is_cpu_config_allowed(name)) {
373 return false;
374 }
krajcevski69a55602014-08-13 10:46:31 -0700375 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomon76228632015-05-29 08:02:10 -0700376 return sampleCnt <= ctx->caps()->maxSampleCount();
bsalomonc2553372014-07-22 13:09:05 -0700377 }
378 return false;
379}
380#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700381
bsalomonc2553372014-07-22 13:09:05 -0700382#if SK_SUPPORT_GPU
383#define kBogusGLContextType GrContextFactory::kNative_GLContextType
384#else
385#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700386#endif
bsalomonc2553372014-07-22 13:09:05 -0700387
388// Append all configs that are enabled and supported.
389static void create_configs(SkTDArray<Config>* configs) {
jvanverth4736e142014-11-07 07:12:46 -0800390 #define CPU_CONFIG(name, backend, color, alpha) \
391 if (is_cpu_config_allowed(#name)) { \
392 Config config = { #name, Benchmark::backend, color, alpha, 0, \
393 kBogusGLContextType, false }; \
394 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700395 }
mtkleine714e752014-07-31 12:13:48 -0700396
mtklein40b32be2014-07-09 08:46:49 -0700397 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700398 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
399 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
400 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700401 }
mtkleinbb6a0282014-07-01 08:43:42 -0700402
403#if SK_SUPPORT_GPU
jvanverth4736e142014-11-07 07:12:46 -0800404 #define GPU_CONFIG(name, ctxType, samples, useDFText) \
bsalomonc2553372014-07-22 13:09:05 -0700405 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
406 Config config = { \
407 #name, \
408 Benchmark::kGPU_Backend, \
409 kN32_SkColorType, \
410 kPremul_SkAlphaType, \
411 samples, \
jvanverth4736e142014-11-07 07:12:46 -0800412 GrContextFactory::ctxType, \
413 useDFText }; \
bsalomonc2553372014-07-22 13:09:05 -0700414 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700415 }
mtkleine714e752014-07-31 12:13:48 -0700416
mtklein40b32be2014-07-09 08:46:49 -0700417 if (FLAGS_gpu) {
jvanverth4736e142014-11-07 07:12:46 -0800418 GPU_CONFIG(gpu, kNative_GLContextType, 0, false)
419 GPU_CONFIG(msaa4, kNative_GLContextType, 4, false)
420 GPU_CONFIG(msaa16, kNative_GLContextType, 16, false)
421 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4, false)
422 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16, false)
423 GPU_CONFIG(gpudft, kNative_GLContextType, 0, true)
424 GPU_CONFIG(debug, kDebug_GLContextType, 0, false)
425 GPU_CONFIG(nullgpu, kNull_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700426#ifdef SK_ANGLE
jvanverth4736e142014-11-07 07:12:46 -0800427 GPU_CONFIG(angle, kANGLE_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700428#endif
mtklein40b32be2014-07-09 08:46:49 -0700429 }
mtkleinbb6a0282014-07-01 08:43:42 -0700430#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700431
432#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
433 if (is_cpu_config_allowed("hwui")) {
434 Config config = { "hwui", Benchmark::kHWUI_Backend, kRGBA_8888_SkColorType,
435 kPremul_SkAlphaType, 0, kBogusGLContextType, false };
436 configs->push(config);
437 }
438#endif
mtkleinf3723212014-06-25 14:08:00 -0700439}
440
bsalomonc2553372014-07-22 13:09:05 -0700441// If bench is enabled for config, returns a Target* for it, otherwise NULL.
442static Target* is_enabled(Benchmark* bench, const Config& config) {
443 if (!bench->isSuitableFor(config.backend)) {
444 return NULL;
445 }
446
reede5ea5002014-09-03 11:54:58 -0700447 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
448 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700449
tomhudsond968a6f2015-03-26 11:28:06 -0700450 Target* target = NULL;
bsalomonc2553372014-07-22 13:09:05 -0700451
tomhudsond968a6f2015-03-26 11:28:06 -0700452 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700453#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700454 case Benchmark::kGPU_Backend:
455 target = new GPUTarget(config);
456 break;
bsalomonc2553372014-07-22 13:09:05 -0700457#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700458#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
459 case Benchmark::kHWUI_Backend:
460 target = new HWUITarget(config, bench);
461 break;
462#endif
463 default:
464 target = new Target(config);
465 break;
466 }
bsalomonc2553372014-07-22 13:09:05 -0700467
tomhudsond968a6f2015-03-26 11:28:06 -0700468 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700469 delete target;
470 return NULL;
471 }
472 return target;
473}
474
475// Creates targets for a benchmark and a set of configs.
476static void create_targets(SkTDArray<Target*>* targets, Benchmark* b,
477 const SkTDArray<Config>& configs) {
478 for (int i = 0; i < configs.count(); ++i) {
479 if (Target* t = is_enabled(b, configs[i])) {
480 targets->push(t);
481 }
mtkleine714e752014-07-31 12:13:48 -0700482
bsalomonc2553372014-07-22 13:09:05 -0700483 }
484}
485
jcgregoriobf5e5232014-07-17 13:14:16 -0700486
mtkleine714e752014-07-31 12:13:48 -0700487class BenchmarkStream {
488public:
mtklein92007582014-08-01 07:46:52 -0700489 BenchmarkStream() : fBenches(BenchRegistry::Head())
490 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700491 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700492 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800493 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800494 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700495 , fCurrentCodec(0)
msarett95f192d2015-02-13 09:05:41 -0800496 , fCurrentImage(0)
497 , fCurrentSubsetImage(0)
498 , fCurrentColorType(0)
joshualitt261c3ad2015-04-27 09:16:57 -0700499 , fCurrentAnimSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800500 , fDivisor(2) {
mtklein92007582014-08-01 07:46:52 -0700501 for (int i = 0; i < FLAGS_skps.count(); i++) {
502 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
503 fSKPs.push_back() = FLAGS_skps[i];
504 } else {
505 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
506 SkString path;
507 while (it.next(&path)) {
508 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
509 }
510 }
511 }
mtkleine714e752014-07-31 12:13:48 -0700512
mtklein92007582014-08-01 07:46:52 -0700513 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
514 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
515 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
516 exit(1);
517 }
518
519 for (int i = 0; i < FLAGS_scales.count(); i++) {
520 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
521 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
522 exit(1);
523 }
524 }
robertphillips5b693772014-11-21 06:19:36 -0800525
joshualitt261c3ad2015-04-27 09:16:57 -0700526 if (2 != sscanf(FLAGS_zoom[0], "%f,%d", &fZoomScale, &fZoomSteps)) {
527 SkDebugf("Can't parse %s from --zoom as a scale,step.\n", FLAGS_zoom[0]);
528 exit(1);
529 }
530
robertphillips5b693772014-11-21 06:19:36 -0800531 fUseMPDs.push_back() = false;
532 if (FLAGS_mpd) {
533 fUseMPDs.push_back() = true;
534 }
mtklein95553d92015-03-12 08:24:21 -0700535
msarett95f192d2015-02-13 09:05:41 -0800536 // Prepare the images for decoding
537 for (int i = 0; i < FLAGS_images.count(); i++) {
538 const char* flag = FLAGS_images[i];
539 if (sk_isdir(flag)) {
540 // If the value passed in is a directory, add all the images
541 SkOSFile::Iter it(flag);
542 SkString file;
543 while (it.next(&file)) {
544 fImages.push_back() = SkOSPath::Join(flag, file.c_str());
545 }
546 } else if (sk_exists(flag)) {
547 // Also add the value if it is a single image
548 fImages.push_back() = flag;
549 }
550 }
mtklein95553d92015-03-12 08:24:21 -0700551
msarett95f192d2015-02-13 09:05:41 -0800552 // Choose the candidate color types for image decoding
553 const SkColorType colorTypes[] =
scroggo21027992015-04-02 13:22:38 -0700554 { kN32_SkColorType, kRGB_565_SkColorType, kAlpha_8_SkColorType, kIndex_8_SkColorType };
msarett95f192d2015-02-13 09:05:41 -0800555 fColorTypes.push_back_n(SK_ARRAY_COUNT(colorTypes), colorTypes);
mtklein92007582014-08-01 07:46:52 -0700556 }
557
mtkleinfd731ce2014-09-10 12:19:30 -0700558 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
559 // Not strictly necessary, as it will be checked again later,
560 // but helps to avoid a lot of pointless work if we're going to skip it.
561 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
562 return false;
563 }
564
scroggoa1193e42015-01-21 12:09:53 -0800565 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
mtkleinfd731ce2014-09-10 12:19:30 -0700566 if (stream.get() == NULL) {
567 SkDebugf("Could not read %s.\n", path);
568 return false;
569 }
570
mtklein57f27bd2015-02-09 11:58:41 -0800571 pic->reset(SkPicture::CreateFromStream(stream.get()));
mtkleinfd731ce2014-09-10 12:19:30 -0700572 if (pic->get() == NULL) {
573 SkDebugf("Could not read %s as an SkPicture.\n", path);
574 return false;
575 }
576 return true;
577 }
578
mtklein92007582014-08-01 07:46:52 -0700579 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700580 if (fBenches) {
581 Benchmark* bench = fBenches->factory()(NULL);
582 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700583 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700584 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700585 return bench;
586 }
mtklein92007582014-08-01 07:46:52 -0700587
mtkleine714e752014-07-31 12:13:48 -0700588 while (fGMs) {
589 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
590 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800591 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700592 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700593 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700594 return SkNEW_ARGS(GMBench, (gm.detach()));
595 }
596 }
mtklein92007582014-08-01 07:46:52 -0700597
mtkleinfd731ce2014-09-10 12:19:30 -0700598 // First add all .skps as RecordingBenches.
599 while (fCurrentRecording < fSKPs.count()) {
600 const SkString& path = fSKPs[fCurrentRecording++];
601 SkAutoTUnref<SkPicture> pic;
602 if (!ReadPicture(path.c_str(), &pic)) {
603 continue;
604 }
605 SkString name = SkOSPath::Basename(path.c_str());
606 fSourceType = "skp";
607 fBenchType = "recording";
bsalomon0aa5cea2014-12-15 09:13:35 -0800608 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic));
mtklein051e56d2014-12-04 08:46:51 -0800609 fSKPOps = pic->approximateOpCount();
mtkleinfd731ce2014-09-10 12:19:30 -0700610 return SkNEW_ARGS(RecordingBench, (name.c_str(), pic.get(), FLAGS_bbh));
611 }
612
613 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700614 while (fCurrentScale < fScales.count()) {
615 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800616 const SkString& path = fSKPs[fCurrentSKP];
mtkleinfd731ce2014-09-10 12:19:30 -0700617 SkAutoTUnref<SkPicture> pic;
618 if (!ReadPicture(path.c_str(), &pic)) {
robertphillips5b693772014-11-21 06:19:36 -0800619 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700620 continue;
621 }
robertphillips5b693772014-11-21 06:19:36 -0800622
623 while (fCurrentUseMPD < fUseMPDs.count()) {
624 if (FLAGS_bbh) {
625 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
626 SkRTreeFactory factory;
627 SkPictureRecorder recorder;
628 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
629 pic->playback(recorder.beginRecording(pic->cullRect().width(),
630 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800631 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800632 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
robertphillips5b693772014-11-21 06:19:36 -0800633 pic.reset(recorder.endRecording());
634 }
635 SkString name = SkOSPath::Basename(path.c_str());
636 fSourceType = "skp";
637 fBenchType = "playback";
638 return SkNEW_ARGS(SKPBench,
joshualitt261c3ad2015-04-27 09:16:57 -0700639 (name.c_str(), pic.get(), fClip,
640 fScales[fCurrentScale], fUseMPDs[fCurrentUseMPD++]));
641
mtklein20840502014-08-21 15:51:22 -0700642 }
robertphillips5b693772014-11-21 06:19:36 -0800643 fCurrentUseMPD = 0;
644 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700645 }
646 fCurrentSKP = 0;
647 fCurrentScale++;
648 }
649
joshualitt261c3ad2015-04-27 09:16:57 -0700650 // Now loop over each skp again if we have an animation
651 if (fZoomScale != 1.0f && fZoomSteps != 1) {
652 while (fCurrentAnimSKP < fSKPs.count()) {
653 const SkString& path = fSKPs[fCurrentAnimSKP];
654 SkAutoTUnref<SkPicture> pic;
655 if (!ReadPicture(path.c_str(), &pic)) {
656 fCurrentAnimSKP++;
657 continue;
658 }
659
660 fCurrentAnimSKP++;
661 SkString name = SkOSPath::Basename(path.c_str());
662 SkMatrix anim = SkMatrix::I();
663 anim.setScale(fZoomScale, fZoomScale);
664 return SkNEW_ARGS(SKPAnimationBench, (name.c_str(), pic.get(), fClip, anim,
665 fZoomSteps));
666 }
667 }
668
669
scroggo60869a42015-04-01 12:09:17 -0700670 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
671 const SkString& path = fImages[fCurrentCodec];
672 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
673 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700674 if (!codec) {
675 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700676 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700677 continue;
678 }
scroggo21027992015-04-02 13:22:38 -0700679
scroggo60869a42015-04-01 12:09:17 -0700680 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700681 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo60869a42015-04-01 12:09:17 -0700682 fCurrentColorType++;
scroggo21027992015-04-02 13:22:38 -0700683
scroggo60869a42015-04-01 12:09:17 -0700684 // Make sure we can decode to this color type.
scroggo60869a42015-04-01 12:09:17 -0700685 SkImageInfo info = codec->getInfo().makeColorType(colorType);
scroggo21027992015-04-02 13:22:38 -0700686 SkAlphaType alphaType;
687 if (!SkColorTypeValidateAlphaType(colorType, info.alphaType(),
688 &alphaType)) {
689 continue;
690 }
691 if (alphaType != info.alphaType()) {
692 info = info.makeAlphaType(alphaType);
693 }
694
695 const size_t rowBytes = info.minRowBytes();
696 SkAutoMalloc storage(info.getSafeSize(rowBytes));
697
698 // Used if fCurrentColorType is kIndex_8_SkColorType
699 int colorCount = 256;
700 SkPMColor colors[256];
701
scroggo60869a42015-04-01 12:09:17 -0700702 const SkImageGenerator::Result result = codec->getPixels(
scroggo21027992015-04-02 13:22:38 -0700703 info, storage.get(), rowBytes, NULL, colors,
704 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700705 switch (result) {
706 case SkImageGenerator::kSuccess:
707 case SkImageGenerator::kIncompleteInput:
708 return new CodecBench(SkOSPath::Basename(path.c_str()),
709 encoded, colorType);
710 case SkImageGenerator::kInvalidConversion:
711 // This is okay. Not all conversions are valid.
712 break;
scroggo60869a42015-04-01 12:09:17 -0700713 default:
714 // This represents some sort of failure.
715 SkASSERT(false);
716 break;
717 }
718 }
719 fCurrentColorType = 0;
720 }
721
msarett95f192d2015-02-13 09:05:41 -0800722 // Run the DecodingBenches
723 while (fCurrentImage < fImages.count()) {
724 while (fCurrentColorType < fColorTypes.count()) {
725 const SkString& path = fImages[fCurrentImage];
726 SkColorType colorType = fColorTypes[fCurrentColorType];
727 fCurrentColorType++;
scroggo60869a42015-04-01 12:09:17 -0700728 // Check if the image decodes to the right color type
729 // before creating the benchmark
msarett95f192d2015-02-13 09:05:41 -0800730 SkBitmap bitmap;
731 if (SkImageDecoder::DecodeFile(path.c_str(), &bitmap,
scroggo60869a42015-04-01 12:09:17 -0700732 colorType, SkImageDecoder::kDecodePixels_Mode)
733 && bitmap.colorType() == colorType) {
msarett95f192d2015-02-13 09:05:41 -0800734 return new DecodingBench(path, colorType);
735 }
736 }
737 fCurrentColorType = 0;
738 fCurrentImage++;
739 }
740
741 // Run the DecodingSubsetBenches
742 while (fCurrentSubsetImage < fImages.count()) {
743 while (fCurrentColorType < fColorTypes.count()) {
744 const SkString& path = fImages[fCurrentSubsetImage];
745 SkColorType colorType = fColorTypes[fCurrentColorType];
746 fCurrentColorType++;
747 // Check if the image decodes before creating the benchmark
748 SkAutoTUnref<SkData> encoded(
749 SkData::NewFromFileName(path.c_str()));
750 SkAutoTDelete<SkMemoryStream> stream(
751 new SkMemoryStream(encoded));
752 SkAutoTDelete<SkImageDecoder>
753 decoder(SkImageDecoder::Factory(stream.get()));
754 if (!decoder) {
755 SkDebugf("Cannot find decoder for %s\n", path.c_str());
756 } else {
757 stream->rewind();
758 int w, h;
759 bool success;
760 if (!decoder->buildTileIndex(stream.detach(), &w, &h)
761 || w*h == 1) {
762 // This is not an error, but in this case we still
763 // do not want to run the benchmark.
764 success = false;
765 } else if (fDivisor > w || fDivisor > h) {
766 SkDebugf("Divisor %d is too big for %s %dx%d\n",
767 fDivisor, path.c_str(), w, h);
768 success = false;
769 } else {
770 const int sW = w / fDivisor;
771 const int sH = h / fDivisor;
772 SkBitmap bitmap;
773 success = true;
774 for (int y = 0; y < h; y += sH) {
775 for (int x = 0; x < w; x += sW) {
776 SkIRect rect = SkIRect::MakeXYWH(x, y, sW, sH);
777 success &= decoder->decodeSubset(&bitmap, rect,
778 colorType);
779 }
780 }
781 }
782 // Create the benchmark if successful
783 if (success) {
784 return new DecodingSubsetBench(path, colorType,
785 fDivisor);
786 }
787 }
788 }
789 fCurrentColorType = 0;
790 fCurrentSubsetImage++;
791 }
792
mtkleine714e752014-07-31 12:13:48 -0700793 return NULL;
794 }
mtklein92007582014-08-01 07:46:52 -0700795
796 void fillCurrentOptions(ResultsWriter* log) const {
797 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700798 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700799 if (0 == strcmp(fSourceType, "skp")) {
800 log->configOption("clip",
801 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
802 fClip.fRight, fClip.fBottom).c_str());
803 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -0800804 if (fCurrentUseMPD > 0) {
805 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
806 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
807 }
mtklein92007582014-08-01 07:46:52 -0700808 }
mtklein051e56d2014-12-04 08:46:51 -0800809 if (0 == strcmp(fBenchType, "recording")) {
810 log->metric("bytes", fSKPBytes);
811 log->metric("ops", fSKPOps);
812 }
mtklein92007582014-08-01 07:46:52 -0700813 }
814
mtkleine714e752014-07-31 12:13:48 -0700815private:
816 const BenchRegistry* fBenches;
817 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700818 SkIRect fClip;
819 SkTArray<SkScalar> fScales;
820 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -0800821 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -0800822 SkTArray<SkString> fImages;
823 SkTArray<SkColorType> fColorTypes;
joshualitt261c3ad2015-04-27 09:16:57 -0700824 SkScalar fZoomScale;
825 int fZoomSteps;
mtklein92007582014-08-01 07:46:52 -0700826
mtklein051e56d2014-12-04 08:46:51 -0800827 double fSKPBytes, fSKPOps;
828
mtkleinfd731ce2014-09-10 12:19:30 -0700829 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
830 const char* fBenchType; // How we bench it: micro, recording, playback, ...
831 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700832 int fCurrentScale;
833 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -0800834 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -0700835 int fCurrentCodec;
msarett95f192d2015-02-13 09:05:41 -0800836 int fCurrentImage;
837 int fCurrentSubsetImage;
838 int fCurrentColorType;
joshualitt261c3ad2015-04-27 09:16:57 -0700839 int fCurrentAnimSKP;
msarett95f192d2015-02-13 09:05:41 -0800840 const int fDivisor;
mtkleine714e752014-07-31 12:13:48 -0700841};
842
jcgregorio3b27ade2014-11-13 08:06:40 -0800843int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700844int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800845 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -0700846 SkAutoGraphics ag;
mtklein4f108442014-12-03 13:07:39 -0800847 SkTaskGroup::Enabler enabled;
mtkleinf3723212014-06-25 14:08:00 -0700848
krajcevski69a55602014-08-13 10:46:31 -0700849#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -0700850 GrContextOptions grContextOpts;
krajcevski12b35442014-08-13 12:06:26 -0700851 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
852 gGrFactory.reset(SkNEW_ARGS(GrContextFactory, (grContextOpts)));
krajcevski69a55602014-08-13 10:46:31 -0700853#endif
854
bsalomon06cddec2014-10-24 10:40:50 -0700855 if (FLAGS_veryVerbose) {
856 FLAGS_verbose = true;
857 }
858
bsalomon6eb03cc2014-08-07 14:28:50 -0700859 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700860 FLAGS_samples = 1;
861 FLAGS_gpuFrameLag = 0;
862 }
863
bsalomon6eb03cc2014-08-07 14:28:50 -0700864 if (!FLAGS_writePath.isEmpty()) {
865 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
866 if (!sk_mkdir(FLAGS_writePath[0])) {
867 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
868 FLAGS_writePath.set(0, NULL);
869 }
870 }
871
mtklein1915b622014-08-20 11:45:00 -0700872 SkAutoTDelete<ResultsWriter> log(SkNEW(ResultsWriter));
mtklein60317d0f2014-07-14 11:30:37 -0700873 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700874 log.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0])));
mtklein60317d0f2014-07-14 11:30:37 -0700875 }
mtklein1915b622014-08-20 11:45:00 -0700876
877 if (1 == FLAGS_properties.count() % 2) {
878 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
879 return 1;
880 }
881 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
882 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
883 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700884
885 if (1 == FLAGS_key.count() % 2) {
886 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
887 return 1;
888 }
889 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -0700890 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -0700891 }
mtklein60317d0f2014-07-14 11:30:37 -0700892
mtkleinf3723212014-06-25 14:08:00 -0700893 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -0700894 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -0400895
mtkleinbb6a0282014-07-01 08:43:42 -0700896 SkAutoTMalloc<double> samples(FLAGS_samples);
897
bsalomon6eb03cc2014-08-07 14:28:50 -0700898 if (kAutoTuneLoops != FLAGS_loops) {
899 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleina189ccd2014-07-14 12:28:47 -0700900 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700901 // No header.
902 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700903 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700904 } else {
mtkleind75c4662015-04-30 07:11:22 -0700905 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -0700906 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -0700907 }
908
bsalomonc2553372014-07-22 13:09:05 -0700909 SkTDArray<Config> configs;
910 create_configs(&configs);
911
mtkleine070c2b2014-10-14 08:40:43 -0700912 int runs = 0;
mtklein92007582014-08-01 07:46:52 -0700913 BenchmarkStream benchStream;
914 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -0700915 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -0700916 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -0700917 continue;
918 }
919
mtkleinbb6a0282014-07-01 08:43:42 -0700920 SkTDArray<Target*> targets;
bsalomonc2553372014-07-22 13:09:05 -0700921 create_targets(&targets, bench.get(), configs);
mtkleinf3723212014-06-25 14:08:00 -0700922
jcgregoriobf5e5232014-07-17 13:14:16 -0700923 if (!targets.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -0700924 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
jcgregoriobf5e5232014-07-17 13:14:16 -0700925 bench->preDraw();
926 }
mtkleinbb6a0282014-07-01 08:43:42 -0700927 for (int j = 0; j < targets.count(); j++) {
tomhudsond968a6f2015-03-26 11:28:06 -0700928 // During HWUI output this canvas may be NULL.
tomhudson75a0ebb2015-03-27 12:11:44 -0700929 SkCanvas* canvas = targets[j]->getCanvas();
bsalomonc2553372014-07-22 13:09:05 -0700930 const char* config = targets[j]->config.name;
mtkleinf3723212014-06-25 14:08:00 -0700931
tomhudsond968a6f2015-03-26 11:28:06 -0700932 targets[j]->setup();
robertphillips5b693772014-11-21 06:19:36 -0800933 bench->perCanvasPreDraw(canvas);
934
mtkleinbb6a0282014-07-01 08:43:42 -0700935 const int loops =
tomhudsond968a6f2015-03-26 11:28:06 -0700936 targets[j]->needsFrameTiming()
tomhudson75a0ebb2015-03-27 12:11:44 -0700937 ? gpu_bench(targets[j], bench.get(), samples.get())
938 : cpu_bench(overhead, targets[j], bench.get(), samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700939
robertphillips5b693772014-11-21 06:19:36 -0800940 bench->perCanvasPostDraw(canvas);
941
tomhudsond968a6f2015-03-26 11:28:06 -0700942 if (Benchmark::kNonRendering_Backend != targets[j]->config.backend &&
943 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700944 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -0700945 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -0700946 pngFilename.append(".png");
tomhudsond968a6f2015-03-26 11:28:06 -0700947 write_canvas_png(targets[j], pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -0700948 }
949
950 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -0700951 // Can't be timed. A warning note has already been printed.
Mike Kleine3631362014-07-15 17:56:37 -0400952 continue;
953 }
954
mtkleinf3723212014-06-25 14:08:00 -0700955 Stats stats(samples.get(), FLAGS_samples);
mtklein1915b622014-08-20 11:45:00 -0700956 log->config(config);
mtklein96289052014-09-10 12:05:59 -0700957 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -0700958 benchStream.fillCurrentOptions(log.get());
tomhudsond968a6f2015-03-26 11:28:06 -0700959 targets[j]->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -0800960 log->metric("min_ms", stats.min);
mtkleine070c2b2014-10-14 08:40:43 -0700961 if (runs++ % FLAGS_flushEvery == 0) {
962 log->flush();
963 }
mtklein60317d0f2014-07-14 11:30:37 -0700964
bsalomon6eb03cc2014-08-07 14:28:50 -0700965 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700966 if (targets.count() == 1) {
967 config = ""; // Only print the config if we run the same bench on more than one.
968 }
mtkleind75c4662015-04-30 07:11:22 -0700969 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
970 , sk_tools::getCurrResidentSetSizeMB()
971 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -0700972 , bench->getUniqueName()
973 , config);
mtkleina189ccd2014-07-14 12:28:47 -0700974 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700975 for (int i = 0; i < FLAGS_samples; i++) {
mtklein55b0ffc2014-07-17 08:38:23 -0700976 SkDebugf("%s ", HUMANIZE(samples[i]));
mtkleinf3723212014-06-25 14:08:00 -0700977 }
mtklein96289052014-09-10 12:05:59 -0700978 SkDebugf("%s\n", bench->getUniqueName());
mtkleinf3723212014-06-25 14:08:00 -0700979 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700980 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700981 config = ""; // Only print the config if we run the same bench on more than one.
982 }
mtklein96289052014-09-10 12:05:59 -0700983 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -0700984 } else {
985 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -0700986 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
987 , sk_tools::getCurrResidentSetSizeMB()
988 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -0700989 , loops
mtklein55b0ffc2014-07-17 08:38:23 -0700990 , HUMANIZE(stats.min)
991 , HUMANIZE(stats.median)
992 , HUMANIZE(stats.mean)
993 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -0700994 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -0700995 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -0700996 , config
mtklein96289052014-09-10 12:05:59 -0700997 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -0700998 );
999 }
bsalomonb12ea412015-02-02 21:19:50 -08001000#if SK_SUPPORT_GPU
1001 if (FLAGS_gpuStats &&
bsalomon06cddec2014-10-24 10:40:50 -07001002 Benchmark::kGPU_Backend == targets[j]->config.backend) {
mtklein6838d852014-10-29 14:15:10 -07001003 gGrFactory->get(targets[j]->config.ctxType)->printCacheStats();
bsalomonb12ea412015-02-02 21:19:50 -08001004 gGrFactory->get(targets[j]->config.ctxType)->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001005 }
1006#endif
mtkleinf3723212014-06-25 14:08:00 -07001007 }
mtkleinbb6a0282014-07-01 08:43:42 -07001008 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -04001009
bsalomon06cddec2014-10-24 10:40:50 -07001010#if SK_SUPPORT_GPU
bsalomon2354f842014-07-28 13:48:36 -07001011 if (FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -07001012 gGrFactory->abandonContexts();
bsalomon2354f842014-07-28 13:48:36 -07001013 }
1014 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -07001015 gGrFactory->destroyContexts();
Mike Klein3944a1d2014-07-15 13:40:19 -04001016 }
bsalomon06cddec2014-10-24 10:40:50 -07001017#endif
mtkleinf3723212014-06-25 14:08:00 -07001018 }
1019
mtkleine1091452014-12-04 10:47:02 -08001020 log->bench("memory_usage", 0,0);
1021 log->config("meta");
1022 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1023
joshualitte0b19d42015-03-26 10:41:02 -07001024#if SK_SUPPORT_GPU
1025 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1026 // SkEventTracer destructor
1027 gGrFactory.reset(NULL);
1028#endif
1029
mtkleinf3723212014-06-25 14:08:00 -07001030 return 0;
1031}
1032
jcgregorio3b27ade2014-11-13 08:06:40 -08001033#if !defined SK_BUILD_FOR_IOS
1034int main(int argc, char** argv) {
1035 SkCommandLineFlags::Parse(argc, argv);
1036 return nanobench_main();
1037}
1038#endif