blob: 65d9216e35090a21bf578f71c1574e269649024b [file] [log] [blame]
mtkleinf3723212014-06-25 14:08:00 -07001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
mtkleinbb6a0282014-07-01 08:43:42 -07008#include <ctype.h>
9
tomhudsond968a6f2015-03-26 11:28:06 -070010#include "nanobench.h"
11
mtkleinf3723212014-06-25 14:08:00 -070012#include "Benchmark.h"
scroggo60869a42015-04-01 12:09:17 -070013#include "CodecBench.h"
mtkleinf3723212014-06-25 14:08:00 -070014#include "CrashHandler.h"
msarett95f192d2015-02-13 09:05:41 -080015#include "DecodingBench.h"
mtkleine714e752014-07-31 12:13:48 -070016#include "GMBench.h"
mtkleinafb43792014-08-19 15:55:55 -070017#include "ProcStats.h"
mtklein60317d0f2014-07-14 11:30:37 -070018#include "ResultsWriter.h"
mtkleinfd731ce2014-09-10 12:19:30 -070019#include "RecordingBench.h"
joshualitt261c3ad2015-04-27 09:16:57 -070020#include "SKPAnimationBench.h"
mtklein92007582014-08-01 07:46:52 -070021#include "SKPBench.h"
msarettb23e6aa2015-06-09 13:56:10 -070022#include "SubsetBenchPriv.h"
msarettb23e6aa2015-06-09 13:56:10 -070023#include "SubsetSingleBench.h"
24#include "SubsetTranslateBench.h"
25#include "SubsetZoomBench.h"
mtkleinf3723212014-06-25 14:08:00 -070026#include "Stats.h"
27#include "Timer.h"
28
mtklein6838d852014-10-29 14:15:10 -070029#include "SkBBoxHierarchy.h"
mtkleinf3723212014-06-25 14:08:00 -070030#include "SkCanvas.h"
scroggo60869a42015-04-01 12:09:17 -070031#include "SkCodec.h"
caryclark17f0b6d2014-07-22 10:15:34 -070032#include "SkCommonFlags.h"
msarett95f192d2015-02-13 09:05:41 -080033#include "SkData.h"
mtkleinf3723212014-06-25 14:08:00 -070034#include "SkForceLinking.h"
35#include "SkGraphics.h"
mtklein20840502014-08-21 15:51:22 -070036#include "SkOSFile.h"
37#include "SkPictureRecorder.h"
mtklein051e56d2014-12-04 08:46:51 -080038#include "SkPictureUtils.h"
mtkleinf3723212014-06-25 14:08:00 -070039#include "SkString.h"
40#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080041#include "SkTaskGroup.h"
mtkleinf3723212014-06-25 14:08:00 -070042
tomhudsond968a6f2015-03-26 11:28:06 -070043#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
44 #include "nanobenchAndroid.h"
45#endif
46
mtkleinbb6a0282014-07-01 08:43:42 -070047#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070048 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070049 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070050 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070051 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070052#endif
53
bsalomon682c2692015-05-22 14:01:46 -070054 struct GrContextOptions;
55
mtkleinf3723212014-06-25 14:08:00 -070056__SK_FORCE_IMAGE_DECODER_LINKING;
57
reed53249782014-10-10 09:09:52 -070058static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070059
mtkleinb5110422014-08-07 15:20:02 -070060static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070061#ifdef SK_DEBUG
62 1;
mtkleina189ccd2014-07-14 12:28:47 -070063#else
bsalomon6eb03cc2014-08-07 14:28:50 -070064 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070065#endif
66
bsalomon6eb03cc2014-08-07 14:28:50 -070067static SkString loops_help_txt() {
68 SkString help;
69 help.printf("Number of times to run each bench. Set this to %d to auto-"
70 "tune for each bench. Timings are only reported when auto-tuning.",
71 kAutoTuneLoops);
72 return help;
73}
74
75DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
76
mtkleinf3723212014-06-25 14:08:00 -070077DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
78DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
79DEFINE_double(overheadGoal, 0.0001,
80 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070081DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
82DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070083DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
84 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070085
mtklein60317d0f2014-07-14 11:30:37 -070086DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -070087DEFINE_int32(maxCalibrationAttempts, 3,
88 "Try up to this many times to guess loops for a bench, or skip the bench.");
89DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -070090DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
91DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
joshualitt261c3ad2015-04-27 09:16:57 -070092DEFINE_string(zoom, "1.0,1", "Comma-separated scale,step zoom factors for SKPs.");
mtklein20840502014-08-21 15:51:22 -070093DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
robertphillips5b693772014-11-21 06:19:36 -080094DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
mtkleine070c2b2014-10-14 08:40:43 -070095DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -080096DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -080097DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
mtklein92007582014-08-01 07:46:52 -070098
mtkleinf3723212014-06-25 14:08:00 -070099static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700100 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800101 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700102}
mtklein55b0ffc2014-07-17 08:38:23 -0700103#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700104
tomhudsond968a6f2015-03-26 11:28:06 -0700105bool Target::init(SkImageInfo info, Benchmark* bench) {
106 if (Benchmark::kRaster_Backend == config.backend) {
107 this->surface.reset(SkSurface::NewRaster(info));
108 if (!this->surface.get()) {
109 return false;
110 }
111 }
112 return true;
113}
114bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700115 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700116 if (!canvas) {
117 return false;
118 }
119 bmp->setInfo(canvas->imageInfo());
120 if (!canvas->readPixels(bmp, 0, 0)) {
121 SkDebugf("Can't read canvas pixels.\n");
122 return false;
123 }
124 return true;
125}
126
127#if SK_SUPPORT_GPU
128struct GPUTarget : public Target {
129 explicit GPUTarget(const Config& c) : Target(c), gl(NULL) { }
130 SkGLContext* gl;
131
132 void setup() override {
133 this->gl->makeCurrent();
134 // Make sure we're done with whatever came before.
135 SK_GL(*this->gl, Finish());
136 }
137 void endTiming() override {
138 if (this->gl) {
139 SK_GL(*this->gl, Flush());
140 this->gl->swapBuffers();
141 }
142 }
143 void fence() override {
144 SK_GL(*this->gl, Finish());
145 }
mtkleind75c4662015-04-30 07:11:22 -0700146
tomhudsond968a6f2015-03-26 11:28:06 -0700147 bool needsFrameTiming() const override { return true; }
148 bool init(SkImageInfo info, Benchmark* bench) override {
149 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDistanceFieldFonts_Flag : 0;
150 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
151 this->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(this->config.ctxType),
152 SkSurface::kNo_Budgeted, info,
153 this->config.samples, &props));
154 this->gl = gGrFactory->getGLContext(this->config.ctxType);
155 if (!this->surface.get()) {
156 return false;
157 }
158 return true;
159 }
160 void fillOptions(ResultsWriter* log) override {
161 const GrGLubyte* version;
162 SK_GL_RET(*this->gl, version, GetString(GR_GL_VERSION));
163 log->configOption("GL_VERSION", (const char*)(version));
164
165 SK_GL_RET(*this->gl, version, GetString(GR_GL_RENDERER));
166 log->configOption("GL_RENDERER", (const char*) version);
167
168 SK_GL_RET(*this->gl, version, GetString(GR_GL_VENDOR));
169 log->configOption("GL_VENDOR", (const char*) version);
170
171 SK_GL_RET(*this->gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
172 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
173 }
174};
mtkleind75c4662015-04-30 07:11:22 -0700175
tomhudsond968a6f2015-03-26 11:28:06 -0700176#endif
177
tomhudson75a0ebb2015-03-27 12:11:44 -0700178static double time(int loops, Benchmark* bench, Target* target) {
179 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700180 if (canvas) {
181 canvas->clear(SK_ColorWHITE);
182 }
mtkleinbb6a0282014-07-01 08:43:42 -0700183 WallTimer timer;
184 timer.start();
tomhudson75a0ebb2015-03-27 12:11:44 -0700185 canvas = target->beginTiming(canvas);
186 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700187 if (canvas) {
188 canvas->flush();
189 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700190 target->endTiming();
mtkleinbb6a0282014-07-01 08:43:42 -0700191 timer.end();
192 return timer.fWall;
193}
194
mtkleinf3723212014-06-25 14:08:00 -0700195static double estimate_timer_overhead() {
196 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700197 for (int i = 0; i < FLAGS_overheadLoops; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700198 WallTimer timer;
199 timer.start();
200 timer.end();
201 overhead += timer.fWall;
mtkleinf3723212014-06-25 14:08:00 -0700202 }
203 return overhead / FLAGS_overheadLoops;
204}
205
reed53249782014-10-10 09:09:52 -0700206static int detect_forever_loops(int loops) {
207 // look for a magic run-forever value
208 if (loops < 0) {
209 loops = SK_MaxS32;
210 }
211 return loops;
212}
213
mtklein55b0ffc2014-07-17 08:38:23 -0700214static int clamp_loops(int loops) {
215 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800216 SkDebugf("ERROR: clamping loops from %d to 1. "
217 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700218 return 1;
219 }
220 if (loops > FLAGS_maxLoops) {
221 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
222 return FLAGS_maxLoops;
223 }
224 return loops;
225}
226
tomhudsond968a6f2015-03-26 11:28:06 -0700227static bool write_canvas_png(Target* target, const SkString& filename) {
228
bsalomon6eb03cc2014-08-07 14:28:50 -0700229 if (filename.isEmpty()) {
230 return false;
231 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700232 if (target->getCanvas() &&
233 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700234 return false;
235 }
tomhudsond968a6f2015-03-26 11:28:06 -0700236
bsalomon6eb03cc2014-08-07 14:28:50 -0700237 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700238
239 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700240 return false;
241 }
tomhudsond968a6f2015-03-26 11:28:06 -0700242
bsalomon6eb03cc2014-08-07 14:28:50 -0700243 SkString dir = SkOSPath::Dirname(filename.c_str());
244 if (!sk_mkdir(dir.c_str())) {
245 SkDebugf("Can't make dir %s.\n", dir.c_str());
246 return false;
247 }
248 SkFILEWStream stream(filename.c_str());
249 if (!stream.isValid()) {
250 SkDebugf("Can't write %s.\n", filename.c_str());
251 return false;
252 }
253 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
254 SkDebugf("Can't encode a PNG.\n");
255 return false;
256 }
257 return true;
258}
259
260static int kFailedLoops = -2;
tomhudson75a0ebb2015-03-27 12:11:44 -0700261static int cpu_bench(const double overhead, Target* target, Benchmark* bench, double* samples) {
mtkleinbb6a0282014-07-01 08:43:42 -0700262 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700263 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700264 int round = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -0700265 if (kAutoTuneLoops == FLAGS_loops) {
266 while (bench_plus_overhead < overhead) {
267 if (round++ == FLAGS_maxCalibrationAttempts) {
268 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700269 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700270 return kFailedLoops;
271 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700272 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700273 }
mtklein2069e222014-08-04 13:57:39 -0700274 }
mtkleinf3723212014-06-25 14:08:00 -0700275
mtkleinbb6a0282014-07-01 08:43:42 -0700276 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700277 // We'll pick N to make timer overhead negligible:
278 //
mtkleinbb6a0282014-07-01 08:43:42 -0700279 // overhead
280 // ------------------------- < FLAGS_overheadGoal
281 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700282 //
mtkleinbb6a0282014-07-01 08:43:42 -0700283 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700284 //
285 // Doing some math, we get:
286 //
mtkleinbb6a0282014-07-01 08:43:42 -0700287 // (overhead / FLAGS_overheadGoal) - overhead
288 // ------------------------------------------ < N
289 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700290 //
291 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700292 int loops = FLAGS_loops;
293 if (kAutoTuneLoops == loops) {
294 const double numer = overhead / FLAGS_overheadGoal - overhead;
295 const double denom = bench_plus_overhead - overhead;
296 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700297 loops = clamp_loops(loops);
298 } else {
299 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700300 }
mtkleinbb6a0282014-07-01 08:43:42 -0700301
302 for (int i = 0; i < FLAGS_samples; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700303 samples[i] = time(loops, bench, target) / loops;
mtkleinbb6a0282014-07-01 08:43:42 -0700304 }
305 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700306}
307
tomhudsond968a6f2015-03-26 11:28:06 -0700308static int gpu_bench(Target* target,
mtkleinbb6a0282014-07-01 08:43:42 -0700309 Benchmark* bench,
mtkleinbb6a0282014-07-01 08:43:42 -0700310 double* samples) {
mtkleinbb6a0282014-07-01 08:43:42 -0700311 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
bsalomon6eb03cc2014-08-07 14:28:50 -0700312 int loops = FLAGS_loops;
313 if (kAutoTuneLoops == loops) {
314 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700315 double elapsed = 0;
316 do {
mtklein527930f2014-11-06 08:04:34 -0800317 if (1<<30 == loops) {
318 // We're about to wrap. Something's wrong with the bench.
319 loops = 0;
320 break;
321 }
mtkleina189ccd2014-07-14 12:28:47 -0700322 loops *= 2;
323 // If the GPU lets frames lag at all, we need to make sure we're timing
324 // _this_ round, not still timing last round. We force this by looping
325 // more times than any reasonable GPU will allow frames to lag.
326 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700327 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700328 }
329 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700330
mtkleina189ccd2014-07-14 12:28:47 -0700331 // We've overshot at least a little. Scale back linearly.
332 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700333 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700334
tomhudsond968a6f2015-03-26 11:28:06 -0700335 // Make sure we're not still timing our calibration.
336 target->fence();
reed53249782014-10-10 09:09:52 -0700337 } else {
338 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700339 }
mtkleinbb6a0282014-07-01 08:43:42 -0700340
341 // Pretty much the same deal as the calibration: do some warmup to make
342 // sure we're timing steady-state pipelined frames.
343 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700344 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700345 }
mtkleinbb6a0282014-07-01 08:43:42 -0700346
347 // Now, actually do the timing!
348 for (int i = 0; i < FLAGS_samples; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700349 samples[i] = time(loops, bench, target) / loops;
mtkleinbb6a0282014-07-01 08:43:42 -0700350 }
tomhudsond968a6f2015-03-26 11:28:06 -0700351
mtkleinbb6a0282014-07-01 08:43:42 -0700352 return loops;
353}
mtkleinbb6a0282014-07-01 08:43:42 -0700354
355static SkString to_lower(const char* str) {
356 SkString lower(str);
357 for (size_t i = 0; i < lower.size(); i++) {
358 lower[i] = tolower(lower[i]);
359 }
360 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700361}
362
bsalomonc2553372014-07-22 13:09:05 -0700363static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700364 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700365 if (to_lower(FLAGS_config[i]).equals(name)) {
366 return true;
mtkleinf3723212014-06-25 14:08:00 -0700367 }
368 }
bsalomonc2553372014-07-22 13:09:05 -0700369 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700370}
371
bsalomonc2553372014-07-22 13:09:05 -0700372#if SK_SUPPORT_GPU
373static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
374 int sampleCnt) {
375 if (!is_cpu_config_allowed(name)) {
376 return false;
377 }
krajcevski69a55602014-08-13 10:46:31 -0700378 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomon76228632015-05-29 08:02:10 -0700379 return sampleCnt <= ctx->caps()->maxSampleCount();
bsalomonc2553372014-07-22 13:09:05 -0700380 }
381 return false;
382}
383#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700384
bsalomonc2553372014-07-22 13:09:05 -0700385#if SK_SUPPORT_GPU
386#define kBogusGLContextType GrContextFactory::kNative_GLContextType
387#else
388#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700389#endif
bsalomonc2553372014-07-22 13:09:05 -0700390
391// Append all configs that are enabled and supported.
392static void create_configs(SkTDArray<Config>* configs) {
jvanverth4736e142014-11-07 07:12:46 -0800393 #define CPU_CONFIG(name, backend, color, alpha) \
394 if (is_cpu_config_allowed(#name)) { \
395 Config config = { #name, Benchmark::backend, color, alpha, 0, \
396 kBogusGLContextType, false }; \
397 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700398 }
mtkleine714e752014-07-31 12:13:48 -0700399
mtklein40b32be2014-07-09 08:46:49 -0700400 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700401 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
402 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
403 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700404 }
mtkleinbb6a0282014-07-01 08:43:42 -0700405
406#if SK_SUPPORT_GPU
jvanverth4736e142014-11-07 07:12:46 -0800407 #define GPU_CONFIG(name, ctxType, samples, useDFText) \
bsalomonc2553372014-07-22 13:09:05 -0700408 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
409 Config config = { \
410 #name, \
411 Benchmark::kGPU_Backend, \
412 kN32_SkColorType, \
413 kPremul_SkAlphaType, \
414 samples, \
jvanverth4736e142014-11-07 07:12:46 -0800415 GrContextFactory::ctxType, \
416 useDFText }; \
bsalomonc2553372014-07-22 13:09:05 -0700417 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700418 }
mtkleine714e752014-07-31 12:13:48 -0700419
mtklein40b32be2014-07-09 08:46:49 -0700420 if (FLAGS_gpu) {
jvanverth4736e142014-11-07 07:12:46 -0800421 GPU_CONFIG(gpu, kNative_GLContextType, 0, false)
422 GPU_CONFIG(msaa4, kNative_GLContextType, 4, false)
423 GPU_CONFIG(msaa16, kNative_GLContextType, 16, false)
424 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4, false)
425 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16, false)
426 GPU_CONFIG(gpudft, kNative_GLContextType, 0, true)
427 GPU_CONFIG(debug, kDebug_GLContextType, 0, false)
428 GPU_CONFIG(nullgpu, kNull_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700429#ifdef SK_ANGLE
jvanverth4736e142014-11-07 07:12:46 -0800430 GPU_CONFIG(angle, kANGLE_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700431#endif
mtklein40b32be2014-07-09 08:46:49 -0700432 }
mtkleinbb6a0282014-07-01 08:43:42 -0700433#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700434
435#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
436 if (is_cpu_config_allowed("hwui")) {
437 Config config = { "hwui", Benchmark::kHWUI_Backend, kRGBA_8888_SkColorType,
438 kPremul_SkAlphaType, 0, kBogusGLContextType, false };
439 configs->push(config);
440 }
441#endif
mtkleinf3723212014-06-25 14:08:00 -0700442}
443
bsalomonc2553372014-07-22 13:09:05 -0700444// If bench is enabled for config, returns a Target* for it, otherwise NULL.
445static Target* is_enabled(Benchmark* bench, const Config& config) {
446 if (!bench->isSuitableFor(config.backend)) {
447 return NULL;
448 }
449
reede5ea5002014-09-03 11:54:58 -0700450 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
451 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700452
tomhudsond968a6f2015-03-26 11:28:06 -0700453 Target* target = NULL;
bsalomonc2553372014-07-22 13:09:05 -0700454
tomhudsond968a6f2015-03-26 11:28:06 -0700455 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700456#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700457 case Benchmark::kGPU_Backend:
458 target = new GPUTarget(config);
459 break;
bsalomonc2553372014-07-22 13:09:05 -0700460#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700461#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
462 case Benchmark::kHWUI_Backend:
463 target = new HWUITarget(config, bench);
464 break;
465#endif
466 default:
467 target = new Target(config);
468 break;
469 }
bsalomonc2553372014-07-22 13:09:05 -0700470
tomhudsond968a6f2015-03-26 11:28:06 -0700471 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700472 delete target;
473 return NULL;
474 }
475 return target;
476}
477
478// Creates targets for a benchmark and a set of configs.
479static void create_targets(SkTDArray<Target*>* targets, Benchmark* b,
480 const SkTDArray<Config>& configs) {
481 for (int i = 0; i < configs.count(); ++i) {
482 if (Target* t = is_enabled(b, configs[i])) {
483 targets->push(t);
484 }
mtkleine714e752014-07-31 12:13:48 -0700485
bsalomonc2553372014-07-22 13:09:05 -0700486 }
487}
488
msarettb23e6aa2015-06-09 13:56:10 -0700489/*
490 * Returns true if set up for a subset decode succeeds, false otherwise
491 * If the set-up succeeds, the width and height parameters will be set
492 */
493static bool valid_subset_bench(const SkString& path, SkColorType colorType, bool useCodec,
494 int* width, int* height) {
495 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
496 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded));
497
msarettab80e352015-06-17 10:28:22 -0700498 // Check that we can create a codec or image decoder.
msarettb23e6aa2015-06-09 13:56:10 -0700499 if (useCodec) {
500 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
501 if (NULL == codec) {
502 SkDebugf("Could not create codec for %s. Skipping bench.\n", path.c_str());
503 return false;
504 }
505
506 // These will be initialized by SkCodec if the color type is kIndex8 and
507 // unused otherwise.
508 SkPMColor colors[256];
509 int colorCount;
510 const SkImageInfo info = codec->getInfo().makeColorType(colorType);
511 SkAutoTDeleteArray<uint8_t> row(SkNEW_ARRAY(uint8_t, info.minRowBytes()));
512 SkScanlineDecoder* scanlineDecoder = codec->getScanlineDecoder(info, NULL,
513 colors, &colorCount);
514 if (NULL == scanlineDecoder) {
515 SkDebugf("Could not create scanline decoder for %s with color type %s. "
516 "Skipping bench.\n", path.c_str(), get_color_name(colorType));
517 return false;
518 }
519 *width = info.width();
520 *height = info.height();
521 } else {
522 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream));
523 if (NULL == decoder) {
524 SkDebugf("Could not create decoder for %s. Skipping bench.\n", path.c_str());
525 return false;
526 }
527 //FIXME: See skbug.com/3921
528 if (kIndex_8_SkColorType == colorType || kGray_8_SkColorType == colorType) {
529 SkDebugf("Cannot use image subset decoder for %s with color type %s. "
530 "Skipping bench.\n", path.c_str(), get_color_name(colorType));
531 return false;
532 }
533 if (!decoder->buildTileIndex(stream.detach(), width, height)) {
534 SkDebugf("Could not build tile index for %s. Skipping bench.\n", path.c_str());
535 return false;
536 }
537 }
msarettab80e352015-06-17 10:28:22 -0700538
539 // Check if the image is large enough for a meaningful subset benchmark.
540 if (*width <= 512 && *height <= 512) {
541 // This should not print a message since it is not an error.
542 return false;
543 }
544
msarettb23e6aa2015-06-09 13:56:10 -0700545 return true;
546}
jcgregoriobf5e5232014-07-17 13:14:16 -0700547
mtkleine714e752014-07-31 12:13:48 -0700548class BenchmarkStream {
549public:
mtklein92007582014-08-01 07:46:52 -0700550 BenchmarkStream() : fBenches(BenchRegistry::Head())
551 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700552 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700553 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800554 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800555 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700556 , fCurrentCodec(0)
msarett95f192d2015-02-13 09:05:41 -0800557 , fCurrentImage(0)
558 , fCurrentSubsetImage(0)
559 , fCurrentColorType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700560 , fCurrentSubsetType(0)
561 , fUseCodec(0)
562 , fCurrentAnimSKP(0) {
mtklein92007582014-08-01 07:46:52 -0700563 for (int i = 0; i < FLAGS_skps.count(); i++) {
564 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
565 fSKPs.push_back() = FLAGS_skps[i];
566 } else {
567 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
568 SkString path;
569 while (it.next(&path)) {
570 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
571 }
572 }
573 }
mtkleine714e752014-07-31 12:13:48 -0700574
mtklein92007582014-08-01 07:46:52 -0700575 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
576 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
577 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
578 exit(1);
579 }
580
581 for (int i = 0; i < FLAGS_scales.count(); i++) {
582 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
583 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
584 exit(1);
585 }
586 }
robertphillips5b693772014-11-21 06:19:36 -0800587
joshualitt261c3ad2015-04-27 09:16:57 -0700588 if (2 != sscanf(FLAGS_zoom[0], "%f,%d", &fZoomScale, &fZoomSteps)) {
589 SkDebugf("Can't parse %s from --zoom as a scale,step.\n", FLAGS_zoom[0]);
590 exit(1);
591 }
592
robertphillips5b693772014-11-21 06:19:36 -0800593 if (FLAGS_mpd) {
594 fUseMPDs.push_back() = true;
595 }
mtkleinc751ecb2015-06-15 08:56:38 -0700596 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700597
msarett95f192d2015-02-13 09:05:41 -0800598 // Prepare the images for decoding
599 for (int i = 0; i < FLAGS_images.count(); i++) {
600 const char* flag = FLAGS_images[i];
601 if (sk_isdir(flag)) {
602 // If the value passed in is a directory, add all the images
603 SkOSFile::Iter it(flag);
604 SkString file;
605 while (it.next(&file)) {
606 fImages.push_back() = SkOSPath::Join(flag, file.c_str());
607 }
608 } else if (sk_exists(flag)) {
609 // Also add the value if it is a single image
610 fImages.push_back() = flag;
611 }
612 }
mtklein95553d92015-03-12 08:24:21 -0700613
msarett95f192d2015-02-13 09:05:41 -0800614 // Choose the candidate color types for image decoding
615 const SkColorType colorTypes[] =
msarettb23e6aa2015-06-09 13:56:10 -0700616 { kN32_SkColorType,
617 kRGB_565_SkColorType,
618 kAlpha_8_SkColorType,
619 kIndex_8_SkColorType,
620 kGray_8_SkColorType };
msarett95f192d2015-02-13 09:05:41 -0800621 fColorTypes.push_back_n(SK_ARRAY_COUNT(colorTypes), colorTypes);
mtklein92007582014-08-01 07:46:52 -0700622 }
623
mtkleinfd731ce2014-09-10 12:19:30 -0700624 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
625 // Not strictly necessary, as it will be checked again later,
626 // but helps to avoid a lot of pointless work if we're going to skip it.
627 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
628 return false;
629 }
630
scroggoa1193e42015-01-21 12:09:53 -0800631 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
mtkleinfd731ce2014-09-10 12:19:30 -0700632 if (stream.get() == NULL) {
633 SkDebugf("Could not read %s.\n", path);
634 return false;
635 }
636
mtklein57f27bd2015-02-09 11:58:41 -0800637 pic->reset(SkPicture::CreateFromStream(stream.get()));
mtkleinfd731ce2014-09-10 12:19:30 -0700638 if (pic->get() == NULL) {
639 SkDebugf("Could not read %s as an SkPicture.\n", path);
640 return false;
641 }
642 return true;
643 }
644
mtklein92007582014-08-01 07:46:52 -0700645 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700646 if (fBenches) {
647 Benchmark* bench = fBenches->factory()(NULL);
648 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700649 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700650 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700651 return bench;
652 }
mtklein92007582014-08-01 07:46:52 -0700653
mtkleine714e752014-07-31 12:13:48 -0700654 while (fGMs) {
655 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
656 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800657 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700658 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700659 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700660 return SkNEW_ARGS(GMBench, (gm.detach()));
661 }
662 }
mtklein92007582014-08-01 07:46:52 -0700663
mtkleinfd731ce2014-09-10 12:19:30 -0700664 // First add all .skps as RecordingBenches.
665 while (fCurrentRecording < fSKPs.count()) {
666 const SkString& path = fSKPs[fCurrentRecording++];
667 SkAutoTUnref<SkPicture> pic;
668 if (!ReadPicture(path.c_str(), &pic)) {
669 continue;
670 }
671 SkString name = SkOSPath::Basename(path.c_str());
672 fSourceType = "skp";
673 fBenchType = "recording";
bsalomon0aa5cea2014-12-15 09:13:35 -0800674 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic));
mtklein051e56d2014-12-04 08:46:51 -0800675 fSKPOps = pic->approximateOpCount();
mtkleinfd731ce2014-09-10 12:19:30 -0700676 return SkNEW_ARGS(RecordingBench, (name.c_str(), pic.get(), FLAGS_bbh));
677 }
678
679 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700680 while (fCurrentScale < fScales.count()) {
681 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800682 const SkString& path = fSKPs[fCurrentSKP];
mtkleinfd731ce2014-09-10 12:19:30 -0700683 SkAutoTUnref<SkPicture> pic;
684 if (!ReadPicture(path.c_str(), &pic)) {
robertphillips5b693772014-11-21 06:19:36 -0800685 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700686 continue;
687 }
robertphillips5b693772014-11-21 06:19:36 -0800688
689 while (fCurrentUseMPD < fUseMPDs.count()) {
690 if (FLAGS_bbh) {
691 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
692 SkRTreeFactory factory;
693 SkPictureRecorder recorder;
694 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
695 pic->playback(recorder.beginRecording(pic->cullRect().width(),
696 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800697 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800698 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
robertphillips5b693772014-11-21 06:19:36 -0800699 pic.reset(recorder.endRecording());
700 }
701 SkString name = SkOSPath::Basename(path.c_str());
702 fSourceType = "skp";
703 fBenchType = "playback";
704 return SkNEW_ARGS(SKPBench,
joshualitt261c3ad2015-04-27 09:16:57 -0700705 (name.c_str(), pic.get(), fClip,
706 fScales[fCurrentScale], fUseMPDs[fCurrentUseMPD++]));
707
mtklein20840502014-08-21 15:51:22 -0700708 }
robertphillips5b693772014-11-21 06:19:36 -0800709 fCurrentUseMPD = 0;
710 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700711 }
712 fCurrentSKP = 0;
713 fCurrentScale++;
714 }
715
joshualitt261c3ad2015-04-27 09:16:57 -0700716 // Now loop over each skp again if we have an animation
717 if (fZoomScale != 1.0f && fZoomSteps != 1) {
718 while (fCurrentAnimSKP < fSKPs.count()) {
719 const SkString& path = fSKPs[fCurrentAnimSKP];
720 SkAutoTUnref<SkPicture> pic;
721 if (!ReadPicture(path.c_str(), &pic)) {
722 fCurrentAnimSKP++;
723 continue;
724 }
725
726 fCurrentAnimSKP++;
727 SkString name = SkOSPath::Basename(path.c_str());
728 SkMatrix anim = SkMatrix::I();
729 anim.setScale(fZoomScale, fZoomScale);
730 return SkNEW_ARGS(SKPAnimationBench, (name.c_str(), pic.get(), fClip, anim,
731 fZoomSteps));
732 }
733 }
734
735
scroggo60869a42015-04-01 12:09:17 -0700736 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
737 const SkString& path = fImages[fCurrentCodec];
738 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
739 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700740 if (!codec) {
741 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700742 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700743 continue;
744 }
scroggo21027992015-04-02 13:22:38 -0700745
scroggo60869a42015-04-01 12:09:17 -0700746 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700747 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo60869a42015-04-01 12:09:17 -0700748 fCurrentColorType++;
scroggo21027992015-04-02 13:22:38 -0700749
scroggo60869a42015-04-01 12:09:17 -0700750 // Make sure we can decode to this color type.
scroggo60869a42015-04-01 12:09:17 -0700751 SkImageInfo info = codec->getInfo().makeColorType(colorType);
scroggo21027992015-04-02 13:22:38 -0700752 SkAlphaType alphaType;
753 if (!SkColorTypeValidateAlphaType(colorType, info.alphaType(),
754 &alphaType)) {
755 continue;
756 }
757 if (alphaType != info.alphaType()) {
758 info = info.makeAlphaType(alphaType);
759 }
760
761 const size_t rowBytes = info.minRowBytes();
762 SkAutoMalloc storage(info.getSafeSize(rowBytes));
763
764 // Used if fCurrentColorType is kIndex_8_SkColorType
765 int colorCount = 256;
766 SkPMColor colors[256];
767
scroggo60869a42015-04-01 12:09:17 -0700768 const SkImageGenerator::Result result = codec->getPixels(
scroggo21027992015-04-02 13:22:38 -0700769 info, storage.get(), rowBytes, NULL, colors,
770 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700771 switch (result) {
772 case SkImageGenerator::kSuccess:
773 case SkImageGenerator::kIncompleteInput:
774 return new CodecBench(SkOSPath::Basename(path.c_str()),
775 encoded, colorType);
776 case SkImageGenerator::kInvalidConversion:
777 // This is okay. Not all conversions are valid.
778 break;
scroggo60869a42015-04-01 12:09:17 -0700779 default:
780 // This represents some sort of failure.
781 SkASSERT(false);
782 break;
783 }
784 }
785 fCurrentColorType = 0;
786 }
787
msarett95f192d2015-02-13 09:05:41 -0800788 // Run the DecodingBenches
789 while (fCurrentImage < fImages.count()) {
790 while (fCurrentColorType < fColorTypes.count()) {
791 const SkString& path = fImages[fCurrentImage];
792 SkColorType colorType = fColorTypes[fCurrentColorType];
793 fCurrentColorType++;
scroggo60869a42015-04-01 12:09:17 -0700794 // Check if the image decodes to the right color type
795 // before creating the benchmark
msarett95f192d2015-02-13 09:05:41 -0800796 SkBitmap bitmap;
797 if (SkImageDecoder::DecodeFile(path.c_str(), &bitmap,
scroggo60869a42015-04-01 12:09:17 -0700798 colorType, SkImageDecoder::kDecodePixels_Mode)
799 && bitmap.colorType() == colorType) {
msarett95f192d2015-02-13 09:05:41 -0800800 return new DecodingBench(path, colorType);
801 }
802 }
803 fCurrentColorType = 0;
804 fCurrentImage++;
805 }
806
msarettb23e6aa2015-06-09 13:56:10 -0700807 // Run the SubsetBenches
808 bool useCodecOpts[] = { true, false };
809 while (fUseCodec < 2) {
810 bool useCodec = useCodecOpts[fUseCodec];
811 while (fCurrentSubsetImage < fImages.count()) {
812 while (fCurrentColorType < fColorTypes.count()) {
813 const SkString& path = fImages[fCurrentSubsetImage];
814 SkColorType colorType = fColorTypes[fCurrentColorType];
815 while (fCurrentSubsetType <= kLast_SubsetType) {
816 int width = 0;
817 int height = 0;
818 int currentSubsetType = fCurrentSubsetType++;
819 if (valid_subset_bench(path, colorType, useCodec, &width, &height)) {
820 switch (currentSubsetType) {
821 case kTopLeft_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700822 return new SubsetSingleBench(path, colorType, width/3,
823 height/3, 0, 0, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700824 case kTopRight_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700825 return new SubsetSingleBench(path, colorType, width/3,
826 height/3, 2*width/3, 0, useCodec);
827 case kMiddle_SubsetType:
828 return new SubsetSingleBench(path, colorType, width/3,
829 height/3, width/3, height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700830 case kBottomLeft_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700831 return new SubsetSingleBench(path, colorType, width/3,
832 height/3, 0, 2*height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700833 case kBottomRight_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700834 return new SubsetSingleBench(path, colorType, width/3,
835 height/3, 2*width/3, 2*height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700836 case kTranslate_SubsetType:
837 return new SubsetTranslateBench(path, colorType, 512, 512,
838 useCodec);
839 case kZoom_SubsetType:
840 return new SubsetZoomBench(path, colorType, 512, 512,
841 useCodec);
msarett95f192d2015-02-13 09:05:41 -0800842 }
msarettb23e6aa2015-06-09 13:56:10 -0700843 } else {
844 break;
msarett95f192d2015-02-13 09:05:41 -0800845 }
846 }
msarettb23e6aa2015-06-09 13:56:10 -0700847 fCurrentSubsetType = 0;
848 fCurrentColorType++;
msarett95f192d2015-02-13 09:05:41 -0800849 }
msarettb23e6aa2015-06-09 13:56:10 -0700850 fCurrentColorType = 0;
851 fCurrentSubsetImage++;
msarett95f192d2015-02-13 09:05:41 -0800852 }
msarettb23e6aa2015-06-09 13:56:10 -0700853 fCurrentSubsetImage = 0;
854 fUseCodec++;
msarett95f192d2015-02-13 09:05:41 -0800855 }
856
mtkleine714e752014-07-31 12:13:48 -0700857 return NULL;
858 }
mtklein92007582014-08-01 07:46:52 -0700859
860 void fillCurrentOptions(ResultsWriter* log) const {
861 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700862 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700863 if (0 == strcmp(fSourceType, "skp")) {
864 log->configOption("clip",
865 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
866 fClip.fRight, fClip.fBottom).c_str());
867 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -0800868 if (fCurrentUseMPD > 0) {
869 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
870 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
871 }
mtklein92007582014-08-01 07:46:52 -0700872 }
mtklein051e56d2014-12-04 08:46:51 -0800873 if (0 == strcmp(fBenchType, "recording")) {
874 log->metric("bytes", fSKPBytes);
875 log->metric("ops", fSKPOps);
876 }
mtklein92007582014-08-01 07:46:52 -0700877 }
878
mtkleine714e752014-07-31 12:13:48 -0700879private:
msarettb23e6aa2015-06-09 13:56:10 -0700880 enum SubsetType {
881 kTopLeft_SubsetType = 0,
882 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -0700883 kMiddle_SubsetType = 2,
884 kBottomLeft_SubsetType = 3,
885 kBottomRight_SubsetType = 4,
886 kTranslate_SubsetType = 5,
887 kZoom_SubsetType = 6,
msarettb23e6aa2015-06-09 13:56:10 -0700888 kLast_SubsetType = kZoom_SubsetType
889 };
890
mtkleine714e752014-07-31 12:13:48 -0700891 const BenchRegistry* fBenches;
892 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700893 SkIRect fClip;
894 SkTArray<SkScalar> fScales;
895 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -0800896 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -0800897 SkTArray<SkString> fImages;
898 SkTArray<SkColorType> fColorTypes;
joshualitt261c3ad2015-04-27 09:16:57 -0700899 SkScalar fZoomScale;
900 int fZoomSteps;
mtklein92007582014-08-01 07:46:52 -0700901
mtklein051e56d2014-12-04 08:46:51 -0800902 double fSKPBytes, fSKPOps;
903
mtkleinfd731ce2014-09-10 12:19:30 -0700904 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
905 const char* fBenchType; // How we bench it: micro, recording, playback, ...
906 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700907 int fCurrentScale;
908 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -0800909 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -0700910 int fCurrentCodec;
msarett95f192d2015-02-13 09:05:41 -0800911 int fCurrentImage;
912 int fCurrentSubsetImage;
913 int fCurrentColorType;
msarettb23e6aa2015-06-09 13:56:10 -0700914 int fCurrentSubsetType;
915 int fUseCodec;
joshualitt261c3ad2015-04-27 09:16:57 -0700916 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -0700917};
918
jcgregorio3b27ade2014-11-13 08:06:40 -0800919int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700920int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800921 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -0700922 SkAutoGraphics ag;
mtklein4f108442014-12-03 13:07:39 -0800923 SkTaskGroup::Enabler enabled;
mtkleinf3723212014-06-25 14:08:00 -0700924
krajcevski69a55602014-08-13 10:46:31 -0700925#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -0700926 GrContextOptions grContextOpts;
krajcevski12b35442014-08-13 12:06:26 -0700927 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
928 gGrFactory.reset(SkNEW_ARGS(GrContextFactory, (grContextOpts)));
krajcevski69a55602014-08-13 10:46:31 -0700929#endif
930
bsalomon06cddec2014-10-24 10:40:50 -0700931 if (FLAGS_veryVerbose) {
932 FLAGS_verbose = true;
933 }
934
bsalomon6eb03cc2014-08-07 14:28:50 -0700935 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700936 FLAGS_samples = 1;
937 FLAGS_gpuFrameLag = 0;
938 }
939
bsalomon6eb03cc2014-08-07 14:28:50 -0700940 if (!FLAGS_writePath.isEmpty()) {
941 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
942 if (!sk_mkdir(FLAGS_writePath[0])) {
943 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
944 FLAGS_writePath.set(0, NULL);
945 }
946 }
947
mtklein1915b622014-08-20 11:45:00 -0700948 SkAutoTDelete<ResultsWriter> log(SkNEW(ResultsWriter));
mtklein60317d0f2014-07-14 11:30:37 -0700949 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700950 log.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0])));
mtklein60317d0f2014-07-14 11:30:37 -0700951 }
mtklein1915b622014-08-20 11:45:00 -0700952
953 if (1 == FLAGS_properties.count() % 2) {
954 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
955 return 1;
956 }
957 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
958 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
959 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700960
961 if (1 == FLAGS_key.count() % 2) {
962 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
963 return 1;
964 }
965 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -0700966 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -0700967 }
mtklein60317d0f2014-07-14 11:30:37 -0700968
mtkleinf3723212014-06-25 14:08:00 -0700969 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -0700970 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -0400971
mtkleinbb6a0282014-07-01 08:43:42 -0700972 SkAutoTMalloc<double> samples(FLAGS_samples);
973
bsalomon6eb03cc2014-08-07 14:28:50 -0700974 if (kAutoTuneLoops != FLAGS_loops) {
975 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleina189ccd2014-07-14 12:28:47 -0700976 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700977 // No header.
978 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700979 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700980 } else {
mtkleind75c4662015-04-30 07:11:22 -0700981 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -0700982 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -0700983 }
984
bsalomonc2553372014-07-22 13:09:05 -0700985 SkTDArray<Config> configs;
986 create_configs(&configs);
987
mtkleine070c2b2014-10-14 08:40:43 -0700988 int runs = 0;
mtklein92007582014-08-01 07:46:52 -0700989 BenchmarkStream benchStream;
990 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -0700991 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -0700992 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -0700993 continue;
994 }
995
mtkleinbb6a0282014-07-01 08:43:42 -0700996 SkTDArray<Target*> targets;
bsalomonc2553372014-07-22 13:09:05 -0700997 create_targets(&targets, bench.get(), configs);
mtkleinf3723212014-06-25 14:08:00 -0700998
jcgregoriobf5e5232014-07-17 13:14:16 -0700999 if (!targets.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -07001000 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
jcgregoriobf5e5232014-07-17 13:14:16 -07001001 bench->preDraw();
1002 }
mtkleinbb6a0282014-07-01 08:43:42 -07001003 for (int j = 0; j < targets.count(); j++) {
tomhudsond968a6f2015-03-26 11:28:06 -07001004 // During HWUI output this canvas may be NULL.
tomhudson75a0ebb2015-03-27 12:11:44 -07001005 SkCanvas* canvas = targets[j]->getCanvas();
bsalomonc2553372014-07-22 13:09:05 -07001006 const char* config = targets[j]->config.name;
mtkleinf3723212014-06-25 14:08:00 -07001007
tomhudsond968a6f2015-03-26 11:28:06 -07001008 targets[j]->setup();
robertphillips5b693772014-11-21 06:19:36 -08001009 bench->perCanvasPreDraw(canvas);
1010
mtkleinbb6a0282014-07-01 08:43:42 -07001011 const int loops =
tomhudsond968a6f2015-03-26 11:28:06 -07001012 targets[j]->needsFrameTiming()
tomhudson75a0ebb2015-03-27 12:11:44 -07001013 ? gpu_bench(targets[j], bench.get(), samples.get())
1014 : cpu_bench(overhead, targets[j], bench.get(), samples.get());
mtkleinf3723212014-06-25 14:08:00 -07001015
robertphillips5b693772014-11-21 06:19:36 -08001016 bench->perCanvasPostDraw(canvas);
1017
tomhudsond968a6f2015-03-26 11:28:06 -07001018 if (Benchmark::kNonRendering_Backend != targets[j]->config.backend &&
1019 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001020 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001021 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001022 pngFilename.append(".png");
tomhudsond968a6f2015-03-26 11:28:06 -07001023 write_canvas_png(targets[j], pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001024 }
1025
1026 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001027 // Can't be timed. A warning note has already been printed.
Mike Kleine3631362014-07-15 17:56:37 -04001028 continue;
1029 }
1030
mtkleinf3723212014-06-25 14:08:00 -07001031 Stats stats(samples.get(), FLAGS_samples);
mtklein1915b622014-08-20 11:45:00 -07001032 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001033 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001034 benchStream.fillCurrentOptions(log.get());
tomhudsond968a6f2015-03-26 11:28:06 -07001035 targets[j]->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001036 log->metric("min_ms", stats.min);
mtkleine070c2b2014-10-14 08:40:43 -07001037 if (runs++ % FLAGS_flushEvery == 0) {
1038 log->flush();
1039 }
mtklein60317d0f2014-07-14 11:30:37 -07001040
bsalomon6eb03cc2014-08-07 14:28:50 -07001041 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001042 if (targets.count() == 1) {
1043 config = ""; // Only print the config if we run the same bench on more than one.
1044 }
mtkleind75c4662015-04-30 07:11:22 -07001045 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1046 , sk_tools::getCurrResidentSetSizeMB()
1047 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001048 , bench->getUniqueName()
1049 , config);
mtkleina189ccd2014-07-14 12:28:47 -07001050 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -07001051 for (int i = 0; i < FLAGS_samples; i++) {
mtklein55b0ffc2014-07-17 08:38:23 -07001052 SkDebugf("%s ", HUMANIZE(samples[i]));
mtkleinf3723212014-06-25 14:08:00 -07001053 }
mtklein96289052014-09-10 12:05:59 -07001054 SkDebugf("%s\n", bench->getUniqueName());
mtkleinf3723212014-06-25 14:08:00 -07001055 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -07001056 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -07001057 config = ""; // Only print the config if we run the same bench on more than one.
1058 }
mtklein96289052014-09-10 12:05:59 -07001059 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001060 } else {
1061 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001062 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1063 , sk_tools::getCurrResidentSetSizeMB()
1064 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001065 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001066 , HUMANIZE(stats.min)
1067 , HUMANIZE(stats.median)
1068 , HUMANIZE(stats.mean)
1069 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001070 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -07001071 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001072 , config
mtklein96289052014-09-10 12:05:59 -07001073 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001074 );
1075 }
bsalomonb12ea412015-02-02 21:19:50 -08001076#if SK_SUPPORT_GPU
1077 if (FLAGS_gpuStats &&
bsalomon06cddec2014-10-24 10:40:50 -07001078 Benchmark::kGPU_Backend == targets[j]->config.backend) {
mtklein6838d852014-10-29 14:15:10 -07001079 gGrFactory->get(targets[j]->config.ctxType)->printCacheStats();
bsalomonb12ea412015-02-02 21:19:50 -08001080 gGrFactory->get(targets[j]->config.ctxType)->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001081 }
1082#endif
mtkleinf3723212014-06-25 14:08:00 -07001083 }
mtkleinbb6a0282014-07-01 08:43:42 -07001084 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -04001085
bsalomon06cddec2014-10-24 10:40:50 -07001086#if SK_SUPPORT_GPU
bsalomon2354f842014-07-28 13:48:36 -07001087 if (FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -07001088 gGrFactory->abandonContexts();
bsalomon2354f842014-07-28 13:48:36 -07001089 }
1090 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -07001091 gGrFactory->destroyContexts();
Mike Klein3944a1d2014-07-15 13:40:19 -04001092 }
bsalomon06cddec2014-10-24 10:40:50 -07001093#endif
mtkleinf3723212014-06-25 14:08:00 -07001094 }
1095
mtkleine1091452014-12-04 10:47:02 -08001096 log->bench("memory_usage", 0,0);
1097 log->config("meta");
1098 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1099
joshualitte0b19d42015-03-26 10:41:02 -07001100#if SK_SUPPORT_GPU
1101 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1102 // SkEventTracer destructor
1103 gGrFactory.reset(NULL);
1104#endif
1105
mtkleinf3723212014-06-25 14:08:00 -07001106 return 0;
1107}
1108
jcgregorio3b27ade2014-11-13 08:06:40 -08001109#if !defined SK_BUILD_FOR_IOS
1110int main(int argc, char** argv) {
1111 SkCommandLineFlags::Parse(argc, argv);
1112 return nanobench_main();
1113}
1114#endif