blob: 264e4a1378f61f09b3565b4cd3f31543135d204a [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"
msarett7f691442015-09-22 11:56:16 -070013#include "BitmapRegionDecoderBench.h"
scroggo60869a42015-04-01 12:09:17 -070014#include "CodecBench.h"
msarett7f691442015-09-22 11:56:16 -070015#include "CodecBenchPriv.h"
mtkleinf3723212014-06-25 14:08:00 -070016#include "CrashHandler.h"
msarett95f192d2015-02-13 09:05:41 -080017#include "DecodingBench.h"
mtkleine714e752014-07-31 12:13:48 -070018#include "GMBench.h"
mtkleinafb43792014-08-19 15:55:55 -070019#include "ProcStats.h"
mtklein60317d0f2014-07-14 11:30:37 -070020#include "ResultsWriter.h"
mtkleinfd731ce2014-09-10 12:19:30 -070021#include "RecordingBench.h"
joshualitt261c3ad2015-04-27 09:16:57 -070022#include "SKPAnimationBench.h"
mtklein92007582014-08-01 07:46:52 -070023#include "SKPBench.h"
msarettb23e6aa2015-06-09 13:56:10 -070024#include "SubsetSingleBench.h"
25#include "SubsetTranslateBench.h"
26#include "SubsetZoomBench.h"
mtkleinf3723212014-06-25 14:08:00 -070027#include "Stats.h"
mtkleinf3723212014-06-25 14:08:00 -070028
msarett5cb48852015-11-06 08:56:32 -080029#include "SkBitmapRegionDecoder.h"
mtklein6838d852014-10-29 14:15:10 -070030#include "SkBBoxHierarchy.h"
mtkleinf3723212014-06-25 14:08:00 -070031#include "SkCanvas.h"
scroggo60869a42015-04-01 12:09:17 -070032#include "SkCodec.h"
caryclark17f0b6d2014-07-22 10:15:34 -070033#include "SkCommonFlags.h"
msarett95f192d2015-02-13 09:05:41 -080034#include "SkData.h"
mtkleinf3723212014-06-25 14:08:00 -070035#include "SkForceLinking.h"
36#include "SkGraphics.h"
mtklein20840502014-08-21 15:51:22 -070037#include "SkOSFile.h"
38#include "SkPictureRecorder.h"
mtklein051e56d2014-12-04 08:46:51 -080039#include "SkPictureUtils.h"
mtkleinf3723212014-06-25 14:08:00 -070040#include "SkString.h"
41#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080042#include "SkTaskGroup.h"
mtkleinf3723212014-06-25 14:08:00 -070043
bungeman60e0fee2015-08-26 05:15:46 -070044#include <stdlib.h>
45
tomhudsond968a6f2015-03-26 11:28:06 -070046#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
47 #include "nanobenchAndroid.h"
48#endif
49
mtkleinbb6a0282014-07-01 08:43:42 -070050#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070051 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070052 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070053 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070054 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070055#endif
56
bsalomon682c2692015-05-22 14:01:46 -070057 struct GrContextOptions;
58
mtkleinf3723212014-06-25 14:08:00 -070059__SK_FORCE_IMAGE_DECODER_LINKING;
60
reed53249782014-10-10 09:09:52 -070061static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070062
mtkleinb5110422014-08-07 15:20:02 -070063static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070064#ifdef SK_DEBUG
65 1;
mtkleina189ccd2014-07-14 12:28:47 -070066#else
bsalomon6eb03cc2014-08-07 14:28:50 -070067 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070068#endif
69
bsalomon6eb03cc2014-08-07 14:28:50 -070070static SkString loops_help_txt() {
71 SkString help;
72 help.printf("Number of times to run each bench. Set this to %d to auto-"
73 "tune for each bench. Timings are only reported when auto-tuning.",
74 kAutoTuneLoops);
75 return help;
76}
77
cdaltone1b89582015-06-25 19:17:08 -070078static SkString to_string(int n) {
79 SkString str;
80 str.appendS32(n);
81 return str;
82}
83
bsalomon6eb03cc2014-08-07 14:28:50 -070084DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
85
mtkleinf3723212014-06-25 14:08:00 -070086DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -070087DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -070088DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
89DEFINE_double(overheadGoal, 0.0001,
90 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070091DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -070092DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070093DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
94 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070095
mtklein60317d0f2014-07-14 11:30:37 -070096DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -070097DEFINE_int32(maxCalibrationAttempts, 3,
98 "Try up to this many times to guess loops for a bench, or skip the bench.");
99DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700100DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
101DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700102DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
103 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700104DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
robertphillips5b693772014-11-21 06:19:36 -0800105DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700106DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700107DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800108DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800109DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
scroggo2da1a852015-10-13 13:33:33 -0700110DEFINE_bool(pngBuildTileIndex, false, "If supported, use png buildTileIndex/decodeSubset.");
scroggo860e8a62015-10-15 07:51:28 -0700111DEFINE_bool(jpgBuildTileIndex, false, "If supported, use jpg buildTileIndex/decodeSubset.");
mtklein92007582014-08-01 07:46:52 -0700112
mtkleinbbba1682015-10-28 11:36:30 -0700113static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
114
mtkleinf3723212014-06-25 14:08:00 -0700115static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700116 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800117 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700118}
mtklein55b0ffc2014-07-17 08:38:23 -0700119#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700120
tomhudsond968a6f2015-03-26 11:28:06 -0700121bool Target::init(SkImageInfo info, Benchmark* bench) {
122 if (Benchmark::kRaster_Backend == config.backend) {
123 this->surface.reset(SkSurface::NewRaster(info));
124 if (!this->surface.get()) {
125 return false;
126 }
127 }
128 return true;
129}
130bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700131 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700132 if (!canvas) {
133 return false;
134 }
135 bmp->setInfo(canvas->imageInfo());
136 if (!canvas->readPixels(bmp, 0, 0)) {
137 SkDebugf("Can't read canvas pixels.\n");
138 return false;
139 }
140 return true;
141}
142
143#if SK_SUPPORT_GPU
144struct GPUTarget : public Target {
halcanary96fcdcc2015-08-27 07:41:13 -0700145 explicit GPUTarget(const Config& c) : Target(c), gl(nullptr) { }
tomhudsond968a6f2015-03-26 11:28:06 -0700146 SkGLContext* gl;
147
148 void setup() override {
149 this->gl->makeCurrent();
150 // Make sure we're done with whatever came before.
151 SK_GL(*this->gl, Finish());
152 }
153 void endTiming() override {
154 if (this->gl) {
155 SK_GL(*this->gl, Flush());
156 this->gl->swapBuffers();
157 }
158 }
159 void fence() override {
160 SK_GL(*this->gl, Finish());
161 }
mtkleind75c4662015-04-30 07:11:22 -0700162
cdaltond416a5b2015-06-23 13:23:44 -0700163 bool needsFrameTiming(int* maxFrameLag) const override {
164 if (!this->gl->getMaxGpuFrameLag(maxFrameLag)) {
165 // Frame lag is unknown.
166 *maxFrameLag = FLAGS_gpuFrameLag;
167 }
168 return true;
169 }
tomhudsond968a6f2015-03-26 11:28:06 -0700170 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700171 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
172 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700173 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
174 this->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(this->config.ctxType),
175 SkSurface::kNo_Budgeted, info,
176 this->config.samples, &props));
177 this->gl = gGrFactory->getGLContext(this->config.ctxType);
178 if (!this->surface.get()) {
179 return false;
180 }
cdaltond416a5b2015-06-23 13:23:44 -0700181 if (!this->gl->fenceSyncSupport()) {
182 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
183 "Timings might not be accurate.\n", this->config.name);
184 }
tomhudsond968a6f2015-03-26 11:28:06 -0700185 return true;
186 }
187 void fillOptions(ResultsWriter* log) override {
188 const GrGLubyte* version;
189 SK_GL_RET(*this->gl, version, GetString(GR_GL_VERSION));
190 log->configOption("GL_VERSION", (const char*)(version));
191
192 SK_GL_RET(*this->gl, version, GetString(GR_GL_RENDERER));
193 log->configOption("GL_RENDERER", (const char*) version);
194
195 SK_GL_RET(*this->gl, version, GetString(GR_GL_VENDOR));
196 log->configOption("GL_VENDOR", (const char*) version);
197
198 SK_GL_RET(*this->gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
199 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
200 }
201};
mtkleind75c4662015-04-30 07:11:22 -0700202
tomhudsond968a6f2015-03-26 11:28:06 -0700203#endif
204
tomhudson75a0ebb2015-03-27 12:11:44 -0700205static double time(int loops, Benchmark* bench, Target* target) {
206 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700207 if (canvas) {
208 canvas->clear(SK_ColorWHITE);
209 }
joshualitt8a6697a2015-09-30 12:11:07 -0700210 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700211 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700212 canvas = target->beginTiming(canvas);
213 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700214 if (canvas) {
215 canvas->flush();
216 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700217 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700218 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700219 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700220 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700221}
222
mtkleinf3723212014-06-25 14:08:00 -0700223static double estimate_timer_overhead() {
224 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700225 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700226 double start = now_ms();
227 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700228 }
229 return overhead / FLAGS_overheadLoops;
230}
231
reed53249782014-10-10 09:09:52 -0700232static int detect_forever_loops(int loops) {
233 // look for a magic run-forever value
234 if (loops < 0) {
235 loops = SK_MaxS32;
236 }
237 return loops;
238}
239
mtklein55b0ffc2014-07-17 08:38:23 -0700240static int clamp_loops(int loops) {
241 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800242 SkDebugf("ERROR: clamping loops from %d to 1. "
243 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700244 return 1;
245 }
246 if (loops > FLAGS_maxLoops) {
247 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
248 return FLAGS_maxLoops;
249 }
250 return loops;
251}
252
tomhudsond968a6f2015-03-26 11:28:06 -0700253static bool write_canvas_png(Target* target, const SkString& filename) {
254
bsalomon6eb03cc2014-08-07 14:28:50 -0700255 if (filename.isEmpty()) {
256 return false;
257 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700258 if (target->getCanvas() &&
259 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700260 return false;
261 }
tomhudsond968a6f2015-03-26 11:28:06 -0700262
bsalomon6eb03cc2014-08-07 14:28:50 -0700263 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700264
265 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700266 return false;
267 }
tomhudsond968a6f2015-03-26 11:28:06 -0700268
bsalomon6eb03cc2014-08-07 14:28:50 -0700269 SkString dir = SkOSPath::Dirname(filename.c_str());
270 if (!sk_mkdir(dir.c_str())) {
271 SkDebugf("Can't make dir %s.\n", dir.c_str());
272 return false;
273 }
274 SkFILEWStream stream(filename.c_str());
275 if (!stream.isValid()) {
276 SkDebugf("Can't write %s.\n", filename.c_str());
277 return false;
278 }
279 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
280 SkDebugf("Can't encode a PNG.\n");
281 return false;
282 }
283 return true;
284}
285
286static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700287static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700288 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700289 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700290 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700291 int loops = bench->calculateLoops(FLAGS_loops);
292 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700293 while (bench_plus_overhead < overhead) {
294 if (round++ == FLAGS_maxCalibrationAttempts) {
295 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700296 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700297 return kFailedLoops;
298 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700299 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700300 }
mtklein2069e222014-08-04 13:57:39 -0700301 }
mtkleinf3723212014-06-25 14:08:00 -0700302
mtkleinbb6a0282014-07-01 08:43:42 -0700303 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700304 // We'll pick N to make timer overhead negligible:
305 //
mtkleinbb6a0282014-07-01 08:43:42 -0700306 // overhead
307 // ------------------------- < FLAGS_overheadGoal
308 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700309 //
mtkleinbb6a0282014-07-01 08:43:42 -0700310 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700311 //
312 // Doing some math, we get:
313 //
mtkleinbb6a0282014-07-01 08:43:42 -0700314 // (overhead / FLAGS_overheadGoal) - overhead
315 // ------------------------------------------ < N
316 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700317 //
318 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700319 if (kAutoTuneLoops == loops) {
320 const double numer = overhead / FLAGS_overheadGoal - overhead;
321 const double denom = bench_plus_overhead - overhead;
322 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700323 loops = clamp_loops(loops);
324 } else {
325 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700326 }
mtkleinbb6a0282014-07-01 08:43:42 -0700327
mtkleinbb6a0282014-07-01 08:43:42 -0700328 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700329}
330
cdaltone1b89582015-06-25 19:17:08 -0700331static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700332 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700333 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700334 if (kAutoTuneLoops == loops) {
335 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700336 double elapsed = 0;
337 do {
mtklein527930f2014-11-06 08:04:34 -0800338 if (1<<30 == loops) {
339 // We're about to wrap. Something's wrong with the bench.
340 loops = 0;
341 break;
342 }
mtkleina189ccd2014-07-14 12:28:47 -0700343 loops *= 2;
344 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700345 // _this_ round, not still timing last round.
346 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700347 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700348 }
349 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700350
mtkleina189ccd2014-07-14 12:28:47 -0700351 // We've overshot at least a little. Scale back linearly.
352 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700353 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700354
tomhudsond968a6f2015-03-26 11:28:06 -0700355 // Make sure we're not still timing our calibration.
356 target->fence();
reed53249782014-10-10 09:09:52 -0700357 } else {
358 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700359 }
mtkleinbb6a0282014-07-01 08:43:42 -0700360
361 // Pretty much the same deal as the calibration: do some warmup to make
362 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700363 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700364 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700365 }
mtkleinbb6a0282014-07-01 08:43:42 -0700366
mtkleinbb6a0282014-07-01 08:43:42 -0700367 return loops;
368}
mtkleinbb6a0282014-07-01 08:43:42 -0700369
370static SkString to_lower(const char* str) {
371 SkString lower(str);
372 for (size_t i = 0; i < lower.size(); i++) {
373 lower[i] = tolower(lower[i]);
374 }
375 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700376}
377
bsalomonc2553372014-07-22 13:09:05 -0700378static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700379 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700380 if (to_lower(FLAGS_config[i]).equals(name)) {
381 return true;
mtkleinf3723212014-06-25 14:08:00 -0700382 }
383 }
bsalomonc2553372014-07-22 13:09:05 -0700384 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700385}
386
bsalomonc2553372014-07-22 13:09:05 -0700387#if SK_SUPPORT_GPU
388static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
389 int sampleCnt) {
390 if (!is_cpu_config_allowed(name)) {
391 return false;
392 }
krajcevski69a55602014-08-13 10:46:31 -0700393 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomon76228632015-05-29 08:02:10 -0700394 return sampleCnt <= ctx->caps()->maxSampleCount();
bsalomonc2553372014-07-22 13:09:05 -0700395 }
396 return false;
397}
398#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700399
bsalomonc2553372014-07-22 13:09:05 -0700400#if SK_SUPPORT_GPU
401#define kBogusGLContextType GrContextFactory::kNative_GLContextType
402#else
403#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700404#endif
bsalomonc2553372014-07-22 13:09:05 -0700405
406// Append all configs that are enabled and supported.
407static void create_configs(SkTDArray<Config>* configs) {
jvanverth4736e142014-11-07 07:12:46 -0800408 #define CPU_CONFIG(name, backend, color, alpha) \
409 if (is_cpu_config_allowed(#name)) { \
410 Config config = { #name, Benchmark::backend, color, alpha, 0, \
411 kBogusGLContextType, false }; \
412 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700413 }
mtkleine714e752014-07-31 12:13:48 -0700414
mtklein40b32be2014-07-09 08:46:49 -0700415 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700416 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
417 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
418 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700419 }
mtkleinbb6a0282014-07-01 08:43:42 -0700420
421#if SK_SUPPORT_GPU
jvanverth4736e142014-11-07 07:12:46 -0800422 #define GPU_CONFIG(name, ctxType, samples, useDFText) \
bsalomonc2553372014-07-22 13:09:05 -0700423 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
424 Config config = { \
425 #name, \
426 Benchmark::kGPU_Backend, \
427 kN32_SkColorType, \
428 kPremul_SkAlphaType, \
429 samples, \
jvanverth4736e142014-11-07 07:12:46 -0800430 GrContextFactory::ctxType, \
431 useDFText }; \
bsalomonc2553372014-07-22 13:09:05 -0700432 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700433 }
mtkleine714e752014-07-31 12:13:48 -0700434
mtklein40b32be2014-07-09 08:46:49 -0700435 if (FLAGS_gpu) {
jvanverth4736e142014-11-07 07:12:46 -0800436 GPU_CONFIG(gpu, kNative_GLContextType, 0, false)
437 GPU_CONFIG(msaa4, kNative_GLContextType, 4, false)
438 GPU_CONFIG(msaa16, kNative_GLContextType, 16, false)
439 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4, false)
440 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16, false)
441 GPU_CONFIG(gpudft, kNative_GLContextType, 0, true)
442 GPU_CONFIG(debug, kDebug_GLContextType, 0, false)
443 GPU_CONFIG(nullgpu, kNull_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700444#ifdef SK_ANGLE
jvanverth4736e142014-11-07 07:12:46 -0800445 GPU_CONFIG(angle, kANGLE_GLContextType, 0, false)
hendrikweddbefb2015-09-11 13:07:29 -0700446 GPU_CONFIG(angle-gl, kANGLE_GL_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700447#endif
hendrikw885bf092015-08-27 10:38:39 -0700448#ifdef SK_COMMAND_BUFFER
449 GPU_CONFIG(commandbuffer, kCommandBuffer_GLContextType, 0, false)
450#endif
cdaltond416a5b2015-06-23 13:23:44 -0700451#if SK_MESA
452 GPU_CONFIG(mesa, kMESA_GLContextType, 0, false)
453#endif
mtklein40b32be2014-07-09 08:46:49 -0700454 }
mtkleinbb6a0282014-07-01 08:43:42 -0700455#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700456
457#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
458 if (is_cpu_config_allowed("hwui")) {
459 Config config = { "hwui", Benchmark::kHWUI_Backend, kRGBA_8888_SkColorType,
460 kPremul_SkAlphaType, 0, kBogusGLContextType, false };
461 configs->push(config);
462 }
463#endif
mtkleinf3723212014-06-25 14:08:00 -0700464}
465
halcanary96fcdcc2015-08-27 07:41:13 -0700466// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700467static Target* is_enabled(Benchmark* bench, const Config& config) {
468 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700469 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700470 }
471
reede5ea5002014-09-03 11:54:58 -0700472 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
473 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700474
halcanary96fcdcc2015-08-27 07:41:13 -0700475 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700476
tomhudsond968a6f2015-03-26 11:28:06 -0700477 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700478#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700479 case Benchmark::kGPU_Backend:
480 target = new GPUTarget(config);
481 break;
bsalomonc2553372014-07-22 13:09:05 -0700482#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700483#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
484 case Benchmark::kHWUI_Backend:
485 target = new HWUITarget(config, bench);
486 break;
487#endif
488 default:
489 target = new Target(config);
490 break;
491 }
bsalomonc2553372014-07-22 13:09:05 -0700492
tomhudsond968a6f2015-03-26 11:28:06 -0700493 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700494 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700495 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700496 }
497 return target;
498}
499
msarettb23e6aa2015-06-09 13:56:10 -0700500/*
scroggoab12c272015-10-08 12:01:39 -0700501 * We only run our subset benches on files that are supported by BitmapRegionDecoder:
502 * i.e. PNG, JPEG, and WEBP. We do *not* test WEBP when using codec, since we do not
503 * have a scanline decoder for WEBP, which is necessary for running the subset bench.
504 * (Another bench must be used to test WEBP, which decodes subsets natively.)
505 */
506static bool run_subset_bench(const SkString& path, bool useCodec) {
507 static const char* const exts[] = {
scroggo2da1a852015-10-13 13:33:33 -0700508 "jpg", "jpeg",
509 "JPG", "JPEG",
scroggoab12c272015-10-08 12:01:39 -0700510 };
511
scroggo860e8a62015-10-15 07:51:28 -0700512 if (useCodec || FLAGS_jpgBuildTileIndex) {
513 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
514 if (path.endsWith(exts[i])) {
515 return true;
516 }
scroggoab12c272015-10-08 12:01:39 -0700517 }
518 }
519
scroggo2da1a852015-10-13 13:33:33 -0700520 // Test png in SkCodec, and optionally on SkImageDecoder. SkImageDecoder is
521 // disabled by default because it leaks memory.
522 // skbug.com/4360
523 if ((useCodec || FLAGS_pngBuildTileIndex) && (path.endsWith("png") || path.endsWith("PNG"))) {
524 return true;
525 }
526
527 if (!useCodec && (path.endsWith("webp") || path.endsWith("WEBP"))) {
528 return true;
529 }
530
531 return false;
scroggoab12c272015-10-08 12:01:39 -0700532}
533
534/*
msarettb23e6aa2015-06-09 13:56:10 -0700535 * Returns true if set up for a subset decode succeeds, false otherwise
536 * If the set-up succeeds, the width and height parameters will be set
537 */
538static bool valid_subset_bench(const SkString& path, SkColorType colorType, bool useCodec,
539 int* width, int* height) {
540 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
541 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded));
542
msarettab80e352015-06-17 10:28:22 -0700543 // Check that we can create a codec or image decoder.
msarettb23e6aa2015-06-09 13:56:10 -0700544 if (useCodec) {
545 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
halcanary96fcdcc2015-08-27 07:41:13 -0700546 if (nullptr == codec) {
msarettb23e6aa2015-06-09 13:56:10 -0700547 SkDebugf("Could not create codec for %s. Skipping bench.\n", path.c_str());
548 return false;
549 }
550
551 // These will be initialized by SkCodec if the color type is kIndex8 and
552 // unused otherwise.
553 SkPMColor colors[256];
554 int colorCount;
555 const SkImageInfo info = codec->getInfo().makeColorType(colorType);
scroggo46c57472015-09-30 08:57:13 -0700556 if (codec->startScanlineDecode(info, nullptr, colors, &colorCount) != SkCodec::kSuccess)
scroggo1c005e42015-08-04 09:24:45 -0700557 {
msarettb23e6aa2015-06-09 13:56:10 -0700558 SkDebugf("Could not create scanline decoder for %s with color type %s. "
msarett7f691442015-09-22 11:56:16 -0700559 "Skipping bench.\n", path.c_str(), color_type_to_str(colorType));
msarettb23e6aa2015-06-09 13:56:10 -0700560 return false;
561 }
562 *width = info.width();
563 *height = info.height();
564 } else {
565 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream));
halcanary96fcdcc2015-08-27 07:41:13 -0700566 if (nullptr == decoder) {
msarettb23e6aa2015-06-09 13:56:10 -0700567 SkDebugf("Could not create decoder for %s. Skipping bench.\n", path.c_str());
568 return false;
569 }
570 //FIXME: See skbug.com/3921
571 if (kIndex_8_SkColorType == colorType || kGray_8_SkColorType == colorType) {
572 SkDebugf("Cannot use image subset decoder for %s with color type %s. "
msarett7f691442015-09-22 11:56:16 -0700573 "Skipping bench.\n", path.c_str(), color_type_to_str(colorType));
msarettb23e6aa2015-06-09 13:56:10 -0700574 return false;
575 }
576 if (!decoder->buildTileIndex(stream.detach(), width, height)) {
577 SkDebugf("Could not build tile index for %s. Skipping bench.\n", path.c_str());
578 return false;
579 }
580 }
msarettab80e352015-06-17 10:28:22 -0700581
582 // Check if the image is large enough for a meaningful subset benchmark.
583 if (*width <= 512 && *height <= 512) {
584 // This should not print a message since it is not an error.
585 return false;
586 }
587
msarettb23e6aa2015-06-09 13:56:10 -0700588 return true;
589}
jcgregoriobf5e5232014-07-17 13:14:16 -0700590
msarett5cb48852015-11-06 08:56:32 -0800591static bool valid_brd_bench(SkData* encoded, SkBitmapRegionDecoder::Strategy strategy,
msarett7f691442015-09-22 11:56:16 -0700592 SkColorType colorType, uint32_t sampleSize, uint32_t minOutputSize, int* width,
593 int* height) {
msarett5cb48852015-11-06 08:56:32 -0800594 SkAutoTDelete<SkBitmapRegionDecoder> brd(
595 SkBitmapRegionDecoder::Create(encoded, strategy));
msarett7f691442015-09-22 11:56:16 -0700596 if (nullptr == brd.get()) {
597 // This is indicates that subset decoding is not supported for a particular image format.
598 return false;
599 }
600
msarett35e5d1b2015-10-27 12:50:25 -0700601 SkBitmap bitmap;
602 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, brd->width(), brd->height()),
603 1, colorType, false)) {
604 return false;
605 }
606 if (colorType != bitmap.colorType()) {
msarett7f691442015-09-22 11:56:16 -0700607 // This indicates that conversion to the requested color type is not supported for the
608 // particular image.
609 return false;
610 }
611
612 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
613 (uint32_t) brd->height()) {
614 // This indicates that the image is not large enough to decode a
615 // minOutputSize x minOutputSize subset at the given sampleSize.
616 return false;
617 }
618
619 // Set the image width and height. The calling code will use this to choose subsets to decode.
620 *width = brd->width();
621 *height = brd->height();
622 return true;
623}
624
egdaniel3bf92062015-06-26 08:12:46 -0700625static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700626 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700627#if SK_SUPPORT_GPU
628 if (FLAGS_abandonGpuContext) {
629 gGrFactory->abandonContexts();
630 }
631 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
632 gGrFactory->destroyContexts();
633 }
634#endif
635}
636
mtkleine714e752014-07-31 12:13:48 -0700637class BenchmarkStream {
638public:
mtklein92007582014-08-01 07:46:52 -0700639 BenchmarkStream() : fBenches(BenchRegistry::Head())
640 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700641 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700642 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800643 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800644 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700645 , fCurrentCodec(0)
msarett95f192d2015-02-13 09:05:41 -0800646 , fCurrentImage(0)
647 , fCurrentSubsetImage(0)
msarett7f691442015-09-22 11:56:16 -0700648 , fCurrentBRDImage(0)
msarett95f192d2015-02-13 09:05:41 -0800649 , fCurrentColorType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700650 , fCurrentSubsetType(0)
651 , fUseCodec(0)
msarett7f691442015-09-22 11:56:16 -0700652 , fCurrentBRDStrategy(0)
653 , fCurrentBRDSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700654 , fCurrentAnimSKP(0) {
mtklein92007582014-08-01 07:46:52 -0700655 for (int i = 0; i < FLAGS_skps.count(); i++) {
656 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
657 fSKPs.push_back() = FLAGS_skps[i];
658 } else {
659 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
660 SkString path;
661 while (it.next(&path)) {
662 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
663 }
664 }
665 }
mtkleine714e752014-07-31 12:13:48 -0700666
mtklein92007582014-08-01 07:46:52 -0700667 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
668 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
669 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
670 exit(1);
671 }
672
673 for (int i = 0; i < FLAGS_scales.count(); i++) {
674 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
675 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
676 exit(1);
677 }
678 }
robertphillips5b693772014-11-21 06:19:36 -0800679
cdalton63a82852015-06-29 14:06:10 -0700680 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
681 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700682 exit(1);
683 }
684
robertphillips5b693772014-11-21 06:19:36 -0800685 if (FLAGS_mpd) {
686 fUseMPDs.push_back() = true;
687 }
mtkleinc751ecb2015-06-15 08:56:38 -0700688 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700689
msarett95f192d2015-02-13 09:05:41 -0800690 // Prepare the images for decoding
691 for (int i = 0; i < FLAGS_images.count(); i++) {
692 const char* flag = FLAGS_images[i];
693 if (sk_isdir(flag)) {
694 // If the value passed in is a directory, add all the images
695 SkOSFile::Iter it(flag);
696 SkString file;
697 while (it.next(&file)) {
698 fImages.push_back() = SkOSPath::Join(flag, file.c_str());
699 }
700 } else if (sk_exists(flag)) {
701 // Also add the value if it is a single image
702 fImages.push_back() = flag;
703 }
704 }
mtklein95553d92015-03-12 08:24:21 -0700705
msarett95f192d2015-02-13 09:05:41 -0800706 // Choose the candidate color types for image decoding
707 const SkColorType colorTypes[] =
msarettb23e6aa2015-06-09 13:56:10 -0700708 { kN32_SkColorType,
709 kRGB_565_SkColorType,
710 kAlpha_8_SkColorType,
711 kIndex_8_SkColorType,
712 kGray_8_SkColorType };
msarett74deb982015-10-20 16:45:56 -0700713 fColorTypes.reset(colorTypes, SK_ARRAY_COUNT(colorTypes));
mtklein92007582014-08-01 07:46:52 -0700714 }
715
mtkleinfd731ce2014-09-10 12:19:30 -0700716 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
717 // Not strictly necessary, as it will be checked again later,
718 // but helps to avoid a lot of pointless work if we're going to skip it.
719 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
720 return false;
721 }
722
scroggoa1193e42015-01-21 12:09:53 -0800723 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -0700724 if (stream.get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700725 SkDebugf("Could not read %s.\n", path);
726 return false;
727 }
728
mtklein57f27bd2015-02-09 11:58:41 -0800729 pic->reset(SkPicture::CreateFromStream(stream.get()));
halcanary96fcdcc2015-08-27 07:41:13 -0700730 if (pic->get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700731 SkDebugf("Could not read %s as an SkPicture.\n", path);
732 return false;
733 }
734 return true;
735 }
736
mtklein92007582014-08-01 07:46:52 -0700737 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700738 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700739 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700740 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700741 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700742 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700743 return bench;
744 }
mtklein92007582014-08-01 07:46:52 -0700745
mtkleine714e752014-07-31 12:13:48 -0700746 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -0700747 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700748 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800749 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700750 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700751 fBenchType = "micro";
halcanary385fe4d2015-08-26 13:07:48 -0700752 return new GMBench(gm.detach());
mtkleine714e752014-07-31 12:13:48 -0700753 }
754 }
mtklein92007582014-08-01 07:46:52 -0700755
mtkleinfd731ce2014-09-10 12:19:30 -0700756 // First add all .skps as RecordingBenches.
757 while (fCurrentRecording < fSKPs.count()) {
758 const SkString& path = fSKPs[fCurrentRecording++];
759 SkAutoTUnref<SkPicture> pic;
760 if (!ReadPicture(path.c_str(), &pic)) {
761 continue;
762 }
763 SkString name = SkOSPath::Basename(path.c_str());
764 fSourceType = "skp";
765 fBenchType = "recording";
bsalomon0aa5cea2014-12-15 09:13:35 -0800766 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic));
mtklein051e56d2014-12-04 08:46:51 -0800767 fSKPOps = pic->approximateOpCount();
halcanary385fe4d2015-08-26 13:07:48 -0700768 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700769 }
770
771 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700772 while (fCurrentScale < fScales.count()) {
773 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800774 const SkString& path = fSKPs[fCurrentSKP];
mtkleinfd731ce2014-09-10 12:19:30 -0700775 SkAutoTUnref<SkPicture> pic;
776 if (!ReadPicture(path.c_str(), &pic)) {
robertphillips5b693772014-11-21 06:19:36 -0800777 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700778 continue;
779 }
robertphillips5b693772014-11-21 06:19:36 -0800780
781 while (fCurrentUseMPD < fUseMPDs.count()) {
782 if (FLAGS_bbh) {
783 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
784 SkRTreeFactory factory;
785 SkPictureRecorder recorder;
786 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
787 pic->playback(recorder.beginRecording(pic->cullRect().width(),
788 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800789 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800790 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
robertphillips5b693772014-11-21 06:19:36 -0800791 pic.reset(recorder.endRecording());
792 }
793 SkString name = SkOSPath::Basename(path.c_str());
794 fSourceType = "skp";
795 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700796 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
797 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700798 }
robertphillips5b693772014-11-21 06:19:36 -0800799 fCurrentUseMPD = 0;
800 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700801 }
802 fCurrentSKP = 0;
803 fCurrentScale++;
804 }
805
joshualitt261c3ad2015-04-27 09:16:57 -0700806 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700807 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700808 while (fCurrentAnimSKP < fSKPs.count()) {
809 const SkString& path = fSKPs[fCurrentAnimSKP];
810 SkAutoTUnref<SkPicture> pic;
811 if (!ReadPicture(path.c_str(), &pic)) {
812 fCurrentAnimSKP++;
813 continue;
814 }
815
816 fCurrentAnimSKP++;
817 SkString name = SkOSPath::Basename(path.c_str());
cdalton63a82852015-06-29 14:06:10 -0700818 SkAutoTUnref<SKPAnimationBench::Animation> animation(
819 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
halcanary385fe4d2015-08-26 13:07:48 -0700820 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation,
821 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700822 }
823 }
824
scroggo60869a42015-04-01 12:09:17 -0700825 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700826 fSourceType = "image";
827 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700828 const SkString& path = fImages[fCurrentCodec];
829 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
830 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700831 if (!codec) {
832 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700833 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700834 continue;
835 }
scroggo21027992015-04-02 13:22:38 -0700836
scroggo60869a42015-04-01 12:09:17 -0700837 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700838 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo60869a42015-04-01 12:09:17 -0700839 fCurrentColorType++;
scroggo21027992015-04-02 13:22:38 -0700840
scroggo60869a42015-04-01 12:09:17 -0700841 // Make sure we can decode to this color type.
scroggo60869a42015-04-01 12:09:17 -0700842 SkImageInfo info = codec->getInfo().makeColorType(colorType);
scroggo21027992015-04-02 13:22:38 -0700843 SkAlphaType alphaType;
844 if (!SkColorTypeValidateAlphaType(colorType, info.alphaType(),
845 &alphaType)) {
846 continue;
847 }
848 if (alphaType != info.alphaType()) {
849 info = info.makeAlphaType(alphaType);
850 }
851
852 const size_t rowBytes = info.minRowBytes();
853 SkAutoMalloc storage(info.getSafeSize(rowBytes));
854
855 // Used if fCurrentColorType is kIndex_8_SkColorType
856 int colorCount = 256;
857 SkPMColor colors[256];
858
scroggoeb602a52015-07-09 08:16:03 -0700859 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700860 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700861 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700862 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700863 case SkCodec::kSuccess:
864 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700865 return new CodecBench(SkOSPath::Basename(path.c_str()),
866 encoded, colorType);
scroggoeb602a52015-07-09 08:16:03 -0700867 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700868 // This is okay. Not all conversions are valid.
869 break;
scroggo60869a42015-04-01 12:09:17 -0700870 default:
871 // This represents some sort of failure.
872 SkASSERT(false);
873 break;
874 }
875 }
876 fCurrentColorType = 0;
877 }
878
msarett95f192d2015-02-13 09:05:41 -0800879 // Run the DecodingBenches
880 while (fCurrentImage < fImages.count()) {
scroggo303fa352015-10-05 11:03:34 -0700881 fSourceType = "image";
882 fBenchType = "skimagedecoder";
msarett95f192d2015-02-13 09:05:41 -0800883 while (fCurrentColorType < fColorTypes.count()) {
884 const SkString& path = fImages[fCurrentImage];
885 SkColorType colorType = fColorTypes[fCurrentColorType];
886 fCurrentColorType++;
scroggo60869a42015-04-01 12:09:17 -0700887 // Check if the image decodes to the right color type
888 // before creating the benchmark
msarett95f192d2015-02-13 09:05:41 -0800889 SkBitmap bitmap;
890 if (SkImageDecoder::DecodeFile(path.c_str(), &bitmap,
scroggo60869a42015-04-01 12:09:17 -0700891 colorType, SkImageDecoder::kDecodePixels_Mode)
892 && bitmap.colorType() == colorType) {
msarett95f192d2015-02-13 09:05:41 -0800893 return new DecodingBench(path, colorType);
894 }
895 }
896 fCurrentColorType = 0;
897 fCurrentImage++;
898 }
899
msarettb23e6aa2015-06-09 13:56:10 -0700900 // Run the SubsetBenches
901 bool useCodecOpts[] = { true, false };
902 while (fUseCodec < 2) {
903 bool useCodec = useCodecOpts[fUseCodec];
scroggo303fa352015-10-05 11:03:34 -0700904 fSourceType = "image";
905 fBenchType = useCodec ? "skcodec" : "skimagedecoder";
msarettb23e6aa2015-06-09 13:56:10 -0700906 while (fCurrentSubsetImage < fImages.count()) {
scroggoab12c272015-10-08 12:01:39 -0700907 const SkString& path = fImages[fCurrentSubsetImage];
908 if (!run_subset_bench(path, useCodec)) {
909 fCurrentSubsetImage++;
910 continue;
911 }
msarettb23e6aa2015-06-09 13:56:10 -0700912 while (fCurrentColorType < fColorTypes.count()) {
msarettb23e6aa2015-06-09 13:56:10 -0700913 SkColorType colorType = fColorTypes[fCurrentColorType];
914 while (fCurrentSubsetType <= kLast_SubsetType) {
915 int width = 0;
916 int height = 0;
917 int currentSubsetType = fCurrentSubsetType++;
918 if (valid_subset_bench(path, colorType, useCodec, &width, &height)) {
919 switch (currentSubsetType) {
920 case kTopLeft_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700921 return new SubsetSingleBench(path, colorType, width/3,
922 height/3, 0, 0, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700923 case kTopRight_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700924 return new SubsetSingleBench(path, colorType, width/3,
925 height/3, 2*width/3, 0, useCodec);
926 case kMiddle_SubsetType:
927 return new SubsetSingleBench(path, colorType, width/3,
928 height/3, width/3, height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700929 case kBottomLeft_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700930 return new SubsetSingleBench(path, colorType, width/3,
931 height/3, 0, 2*height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700932 case kBottomRight_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700933 return new SubsetSingleBench(path, colorType, width/3,
934 height/3, 2*width/3, 2*height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700935 case kTranslate_SubsetType:
936 return new SubsetTranslateBench(path, colorType, 512, 512,
937 useCodec);
938 case kZoom_SubsetType:
939 return new SubsetZoomBench(path, colorType, 512, 512,
940 useCodec);
msarett95f192d2015-02-13 09:05:41 -0800941 }
msarettb23e6aa2015-06-09 13:56:10 -0700942 } else {
943 break;
msarett95f192d2015-02-13 09:05:41 -0800944 }
945 }
msarettb23e6aa2015-06-09 13:56:10 -0700946 fCurrentSubsetType = 0;
947 fCurrentColorType++;
msarett95f192d2015-02-13 09:05:41 -0800948 }
msarettb23e6aa2015-06-09 13:56:10 -0700949 fCurrentColorType = 0;
950 fCurrentSubsetImage++;
msarett95f192d2015-02-13 09:05:41 -0800951 }
msarettb23e6aa2015-06-09 13:56:10 -0700952 fCurrentSubsetImage = 0;
953 fUseCodec++;
msarett95f192d2015-02-13 09:05:41 -0800954 }
955
msarett7f691442015-09-22 11:56:16 -0700956 // Run the BRDBenches
957 // We will benchmark multiple BRD strategies.
scroggo303fa352015-10-05 11:03:34 -0700958 static const struct {
msarett5cb48852015-11-06 08:56:32 -0800959 SkBitmapRegionDecoder::Strategy fStrategy;
scroggo303fa352015-10-05 11:03:34 -0700960 const char* fName;
961 } strategies[] = {
msarett5cb48852015-11-06 08:56:32 -0800962 { SkBitmapRegionDecoder::kOriginal_Strategy, "BRD" },
963 { SkBitmapRegionDecoder::kCanvas_Strategy, "BRD_canvas" },
964 { SkBitmapRegionDecoder::kAndroidCodec_Strategy, "BRD_android_codec" },
msarett7f691442015-09-22 11:56:16 -0700965 };
966
967 // We intend to create benchmarks that model the use cases in
968 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
969 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
970 // the image. For that reason, we will benchmark decodes in five representative locations
971 // in the image. Additionally, this use case utilizes power of two scaling, so we will
972 // test on power of two sample sizes. The output tile is always 512x512, so, when a
973 // sampleSize is used, the size of the subset that is decoded is always
974 // (sampleSize*512)x(sampleSize*512).
975 // There are a few good reasons to only test on power of two sample sizes at this time:
976 // JPEG decodes using kOriginal_Strategy are broken for non-powers of two.
977 // skbug.com/4319
978 // All use cases we are aware of only scale by powers of two.
979 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
980 // these tests are sufficient to provide good coverage of our scaling options.
scroggo501b7342015-11-03 07:55:11 -0800981 const uint32_t sampleSizes[] = { 1, 2, 4, 8, 16, 32, 64 };
msarett7f691442015-09-22 11:56:16 -0700982 const uint32_t minOutputSize = 512;
983 while (fCurrentBRDImage < fImages.count()) {
984 while (fCurrentBRDStrategy < (int) SK_ARRAY_COUNT(strategies)) {
scroggo303fa352015-10-05 11:03:34 -0700985 fSourceType = "image";
986 fBenchType = strategies[fCurrentBRDStrategy].fName;
scroggo860e8a62015-10-15 07:51:28 -0700987
988 const SkString& path = fImages[fCurrentBRDImage];
msarett5cb48852015-11-06 08:56:32 -0800989 const SkBitmapRegionDecoder::Strategy strategy =
scroggo860e8a62015-10-15 07:51:28 -0700990 strategies[fCurrentBRDStrategy].fStrategy;
991
msarett5cb48852015-11-06 08:56:32 -0800992 if (SkBitmapRegionDecoder::kOriginal_Strategy == strategy) {
scroggo860e8a62015-10-15 07:51:28 -0700993 // Disable png and jpeg for SkImageDecoder:
994 if (!FLAGS_jpgBuildTileIndex) {
995 if (path.endsWith("JPEG") || path.endsWith("JPG") ||
996 path.endsWith("jpeg") || path.endsWith("jpg"))
997 {
998 fCurrentBRDStrategy++;
999 continue;
1000 }
1001 }
1002 if (!FLAGS_pngBuildTileIndex) {
1003 if (path.endsWith("PNG") || path.endsWith("png")) {
1004 fCurrentBRDStrategy++;
1005 continue;
1006 }
1007 }
1008 }
1009
msarett7f691442015-09-22 11:56:16 -07001010 while (fCurrentColorType < fColorTypes.count()) {
1011 while (fCurrentBRDSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
1012 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -07001013
1014
msarett7f691442015-09-22 11:56:16 -07001015 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
1016 const SkColorType colorType = fColorTypes[fCurrentColorType];
1017 uint32_t sampleSize = sampleSizes[fCurrentBRDSampleSize];
1018 int currentSubsetType = fCurrentSubsetType++;
1019
1020 int width = 0;
1021 int height = 0;
1022 if (!valid_brd_bench(encoded.get(), strategy, colorType, sampleSize,
1023 minOutputSize, &width, &height)) {
1024 break;
1025 }
1026
1027 SkString basename = SkOSPath::Basename(path.c_str());
1028 SkIRect subset;
1029 const uint32_t subsetSize = sampleSize * minOutputSize;
1030 switch (currentSubsetType) {
1031 case kTopLeft_SubsetType:
1032 basename.append("_TopLeft");
1033 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
1034 break;
1035 case kTopRight_SubsetType:
1036 basename.append("_TopRight");
1037 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
1038 subsetSize);
1039 break;
1040 case kMiddle_SubsetType:
1041 basename.append("_Middle");
1042 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
1043 (height - subsetSize) / 2, subsetSize, subsetSize);
1044 break;
1045 case kBottomLeft_SubsetType:
1046 basename.append("_BottomLeft");
1047 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
1048 subsetSize);
1049 break;
1050 case kBottomRight_SubsetType:
1051 basename.append("_BottomRight");
1052 subset = SkIRect::MakeXYWH(width - subsetSize,
1053 height - subsetSize, subsetSize, subsetSize);
1054 break;
1055 default:
1056 SkASSERT(false);
1057 }
1058
1059 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
1060 strategy, colorType, sampleSize, subset);
1061 }
1062 fCurrentSubsetType = 0;
1063 fCurrentBRDSampleSize++;
1064 }
1065 fCurrentBRDSampleSize = 0;
1066 fCurrentColorType++;
1067 }
1068 fCurrentColorType = 0;
1069 fCurrentBRDStrategy++;
1070 }
1071 fCurrentBRDStrategy = 0;
1072 fCurrentBRDImage++;
1073 }
1074
halcanary96fcdcc2015-08-27 07:41:13 -07001075 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001076 }
mtklein92007582014-08-01 07:46:52 -07001077
1078 void fillCurrentOptions(ResultsWriter* log) const {
1079 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001080 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001081 if (0 == strcmp(fSourceType, "skp")) {
1082 log->configOption("clip",
1083 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1084 fClip.fRight, fClip.fBottom).c_str());
mtklein4dfdbb12015-10-20 07:45:29 -07001085 SK_ALWAYSBREAK(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -07001086 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001087 if (fCurrentUseMPD > 0) {
1088 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1089 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1090 }
mtklein92007582014-08-01 07:46:52 -07001091 }
mtklein051e56d2014-12-04 08:46:51 -08001092 if (0 == strcmp(fBenchType, "recording")) {
1093 log->metric("bytes", fSKPBytes);
1094 log->metric("ops", fSKPOps);
1095 }
mtklein92007582014-08-01 07:46:52 -07001096 }
1097
mtkleine714e752014-07-31 12:13:48 -07001098private:
msarettb23e6aa2015-06-09 13:56:10 -07001099 enum SubsetType {
1100 kTopLeft_SubsetType = 0,
1101 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001102 kMiddle_SubsetType = 2,
1103 kBottomLeft_SubsetType = 3,
1104 kBottomRight_SubsetType = 4,
1105 kTranslate_SubsetType = 5,
1106 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001107 kLast_SubsetType = kZoom_SubsetType,
1108 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001109 };
1110
mtkleine714e752014-07-31 12:13:48 -07001111 const BenchRegistry* fBenches;
1112 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001113 SkIRect fClip;
1114 SkTArray<SkScalar> fScales;
1115 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -08001116 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001117 SkTArray<SkString> fImages;
msarett74deb982015-10-20 16:45:56 -07001118 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001119 SkScalar fZoomMax;
1120 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001121
mtklein051e56d2014-12-04 08:46:51 -08001122 double fSKPBytes, fSKPOps;
1123
mtkleinfd731ce2014-09-10 12:19:30 -07001124 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1125 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1126 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -07001127 int fCurrentScale;
1128 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -08001129 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001130 int fCurrentCodec;
msarett95f192d2015-02-13 09:05:41 -08001131 int fCurrentImage;
1132 int fCurrentSubsetImage;
msarett7f691442015-09-22 11:56:16 -07001133 int fCurrentBRDImage;
msarett95f192d2015-02-13 09:05:41 -08001134 int fCurrentColorType;
msarettb23e6aa2015-06-09 13:56:10 -07001135 int fCurrentSubsetType;
1136 int fUseCodec;
msarett7f691442015-09-22 11:56:16 -07001137 int fCurrentBRDStrategy;
1138 int fCurrentBRDSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001139 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001140};
1141
jcgregorio3b27ade2014-11-13 08:06:40 -08001142int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001143int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001144 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001145 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001146 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001147
krajcevski69a55602014-08-13 10:46:31 -07001148#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001149 GrContextOptions grContextOpts;
krajcevski12b35442014-08-13 12:06:26 -07001150 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
halcanary385fe4d2015-08-26 13:07:48 -07001151 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001152#endif
1153
bsalomon06cddec2014-10-24 10:40:50 -07001154 if (FLAGS_veryVerbose) {
1155 FLAGS_verbose = true;
1156 }
1157
bsalomon6eb03cc2014-08-07 14:28:50 -07001158 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001159 FLAGS_samples = 1;
1160 FLAGS_gpuFrameLag = 0;
1161 }
1162
bsalomon6eb03cc2014-08-07 14:28:50 -07001163 if (!FLAGS_writePath.isEmpty()) {
1164 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1165 if (!sk_mkdir(FLAGS_writePath[0])) {
1166 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001167 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001168 }
1169 }
1170
halcanary385fe4d2015-08-26 13:07:48 -07001171 SkAutoTDelete<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001172 if (!FLAGS_outResultsFile.isEmpty()) {
halcanary385fe4d2015-08-26 13:07:48 -07001173 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein60317d0f2014-07-14 11:30:37 -07001174 }
mtklein1915b622014-08-20 11:45:00 -07001175
1176 if (1 == FLAGS_properties.count() % 2) {
1177 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1178 return 1;
1179 }
1180 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1181 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1182 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001183
1184 if (1 == FLAGS_key.count() % 2) {
1185 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1186 return 1;
1187 }
1188 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001189 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001190 }
mtklein60317d0f2014-07-14 11:30:37 -07001191
mtkleinf3723212014-06-25 14:08:00 -07001192 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001193 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001194
cdaltone1b89582015-06-25 19:17:08 -07001195 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001196
bsalomon6eb03cc2014-08-07 14:28:50 -07001197 if (kAutoTuneLoops != FLAGS_loops) {
1198 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001199 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -07001200 SkDebugf("median\tbench\tconfig\n");
mtkleinbbba1682015-10-28 11:36:30 -07001201 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001202 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001203 } else {
mtkleind75c4662015-04-30 07:11:22 -07001204 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001205 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001206 }
1207
bsalomonc2553372014-07-22 13:09:05 -07001208 SkTDArray<Config> configs;
1209 create_configs(&configs);
1210
mtkleine070c2b2014-10-14 08:40:43 -07001211 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001212 BenchmarkStream benchStream;
1213 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001214 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001215 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001216 continue;
1217 }
1218
egdaniel3bf92062015-06-26 08:12:46 -07001219 if (!configs.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -07001220 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001221 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001222 }
egdaniel3bf92062015-06-26 08:12:46 -07001223 for (int i = 0; i < configs.count(); ++i) {
1224 Target* target = is_enabled(b, configs[i]);
1225 if (!target) {
1226 continue;
1227 }
mtkleinf3723212014-06-25 14:08:00 -07001228
halcanary96fcdcc2015-08-27 07:41:13 -07001229 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001230 SkCanvas* canvas = target->getCanvas();
1231 const char* config = target->config.name;
1232
1233 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001234 bench->perCanvasPreDraw(canvas);
1235
cdaltone1b89582015-06-25 19:17:08 -07001236 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001237 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001238 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1239 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001240
mtkleinbbba1682015-10-28 11:36:30 -07001241 if (FLAGS_ms) {
1242 samples.reset();
1243 auto stop = now_ms() + FLAGS_ms;
1244 do {
1245 samples.push_back(time(loops, bench, target) / loops);
1246 } while (now_ms() < stop);
1247 } else {
cdaltone1b89582015-06-25 19:17:08 -07001248 samples.reset(FLAGS_samples);
1249 for (int s = 0; s < FLAGS_samples; s++) {
egdaniel3bf92062015-06-26 08:12:46 -07001250 samples[s] = time(loops, bench, target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001251 }
cdaltone1b89582015-06-25 19:17:08 -07001252 }
mtkleinf3723212014-06-25 14:08:00 -07001253
robertphillips5b693772014-11-21 06:19:36 -08001254 bench->perCanvasPostDraw(canvas);
1255
egdaniel3bf92062015-06-26 08:12:46 -07001256 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001257 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001258 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001259 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001260 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001261 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001262 }
1263
1264 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001265 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001266 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001267 continue;
1268 }
1269
cdaltone1b89582015-06-25 19:17:08 -07001270 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001271 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001272 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001273 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001274 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001275 log->metric("min_ms", stats.min);
mtkleine070c2b2014-10-14 08:40:43 -07001276 if (runs++ % FLAGS_flushEvery == 0) {
1277 log->flush();
1278 }
mtklein60317d0f2014-07-14 11:30:37 -07001279
bsalomon6eb03cc2014-08-07 14:28:50 -07001280 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001281 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001282 config = ""; // Only print the config if we run the same bench on more than one.
1283 }
mtkleind75c4662015-04-30 07:11:22 -07001284 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1285 , sk_tools::getCurrResidentSetSizeMB()
1286 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001287 , bench->getUniqueName()
1288 , config);
mtkleinf3723212014-06-25 14:08:00 -07001289 } else if (FLAGS_quiet) {
egdaniel3bf92062015-06-26 08:12:46 -07001290 if (configs.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -07001291 config = ""; // Only print the config if we run the same bench on more than one.
1292 }
mtklein96289052014-09-10 12:05:59 -07001293 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001294 } else {
1295 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001296 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1297 , sk_tools::getCurrResidentSetSizeMB()
1298 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001299 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001300 , HUMANIZE(stats.min)
1301 , HUMANIZE(stats.median)
1302 , HUMANIZE(stats.mean)
1303 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001304 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001305 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001306 , config
mtklein96289052014-09-10 12:05:59 -07001307 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001308 );
1309 }
bsalomonb12ea412015-02-02 21:19:50 -08001310#if SK_SUPPORT_GPU
1311 if (FLAGS_gpuStats &&
egdaniel3bf92062015-06-26 08:12:46 -07001312 Benchmark::kGPU_Backend == configs[i].backend) {
1313 gGrFactory->get(configs[i].ctxType)->printCacheStats();
1314 gGrFactory->get(configs[i].ctxType)->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001315 }
1316#endif
cdalton2c56ba52015-06-26 13:32:53 -07001317 if (FLAGS_verbose) {
1318 SkDebugf("Samples: ");
1319 for (int i = 0; i < samples.count(); i++) {
1320 SkDebugf("%s ", HUMANIZE(samples[i]));
1321 }
1322 SkDebugf("%s\n", bench->getUniqueName());
1323 }
egdaniel3bf92062015-06-26 08:12:46 -07001324 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001325 }
mtkleinf3723212014-06-25 14:08:00 -07001326 }
1327
mtkleine1091452014-12-04 10:47:02 -08001328 log->bench("memory_usage", 0,0);
1329 log->config("meta");
1330 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1331
joshualitte0b19d42015-03-26 10:41:02 -07001332#if SK_SUPPORT_GPU
1333 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1334 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001335 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001336#endif
1337
mtkleinf3723212014-06-25 14:08:00 -07001338 return 0;
1339}
1340
jcgregorio3b27ade2014-11-13 08:06:40 -08001341#if !defined SK_BUILD_FOR_IOS
1342int main(int argc, char** argv) {
1343 SkCommandLineFlags::Parse(argc, argv);
1344 return nanobench_main();
1345}
1346#endif