blob: 2c86adafb9fa0281f15979b2e2862be901dfb781 [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"
kkinnunene13ca322015-12-14 04:49:17 -080034#include "SkCommonFlagsConfig.h"
msarett95f192d2015-02-13 09:05:41 -080035#include "SkData.h"
mtkleinf3723212014-06-25 14:08:00 -070036#include "SkForceLinking.h"
37#include "SkGraphics.h"
mtklein20840502014-08-21 15:51:22 -070038#include "SkOSFile.h"
39#include "SkPictureRecorder.h"
mtklein051e56d2014-12-04 08:46:51 -080040#include "SkPictureUtils.h"
mtkleinf3723212014-06-25 14:08:00 -070041#include "SkString.h"
42#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080043#include "SkTaskGroup.h"
mtkleinf3723212014-06-25 14:08:00 -070044
bungeman60e0fee2015-08-26 05:15:46 -070045#include <stdlib.h>
46
tomhudsond968a6f2015-03-26 11:28:06 -070047#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
48 #include "nanobenchAndroid.h"
49#endif
50
mtkleinbb6a0282014-07-01 08:43:42 -070051#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070052 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070053 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070054 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070055 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070056#endif
57
bsalomon682c2692015-05-22 14:01:46 -070058 struct GrContextOptions;
59
mtkleinf3723212014-06-25 14:08:00 -070060__SK_FORCE_IMAGE_DECODER_LINKING;
61
reed53249782014-10-10 09:09:52 -070062static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070063
mtkleinb5110422014-08-07 15:20:02 -070064static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070065#ifdef SK_DEBUG
66 1;
mtkleina189ccd2014-07-14 12:28:47 -070067#else
bsalomon6eb03cc2014-08-07 14:28:50 -070068 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070069#endif
70
bsalomon6eb03cc2014-08-07 14:28:50 -070071static SkString loops_help_txt() {
72 SkString help;
73 help.printf("Number of times to run each bench. Set this to %d to auto-"
74 "tune for each bench. Timings are only reported when auto-tuning.",
75 kAutoTuneLoops);
76 return help;
77}
78
cdaltone1b89582015-06-25 19:17:08 -070079static SkString to_string(int n) {
80 SkString str;
81 str.appendS32(n);
82 return str;
83}
84
bsalomon6eb03cc2014-08-07 14:28:50 -070085DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
86
mtkleinf3723212014-06-25 14:08:00 -070087DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -070088DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -070089DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
90DEFINE_double(overheadGoal, 0.0001,
91 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070092DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -070093DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070094DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
95 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070096
mtklein60317d0f2014-07-14 11:30:37 -070097DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -070098DEFINE_int32(maxCalibrationAttempts, 3,
99 "Try up to this many times to guess loops for a bench, or skip the bench.");
100DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700101DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
102DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700103DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
104 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700105DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
robertphillips5b693772014-11-21 06:19:36 -0800106DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700107DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700108DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800109DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800110DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800111DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
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);
kkinnunen5219fd92015-12-10 06:28:13 -0800174 this->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(this->config.ctxType,
kkinnunen5219fd92015-12-10 06:28:13 -0800175 this->config.ctxOptions),
tomhudsond968a6f2015-03-26 11:28:06 -0700176 SkSurface::kNo_Budgeted, info,
177 this->config.samples, &props));
kkinnunene13ca322015-12-14 04:49:17 -0800178 this->gl = gGrFactory->getContextInfo(this->config.ctxType,
kkinnunen5219fd92015-12-10 06:28:13 -0800179 this->config.ctxOptions)->fGLContext;
tomhudsond968a6f2015-03-26 11:28:06 -0700180 if (!this->surface.get()) {
181 return false;
182 }
cdaltond416a5b2015-06-23 13:23:44 -0700183 if (!this->gl->fenceSyncSupport()) {
184 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
185 "Timings might not be accurate.\n", this->config.name);
186 }
tomhudsond968a6f2015-03-26 11:28:06 -0700187 return true;
188 }
189 void fillOptions(ResultsWriter* log) override {
190 const GrGLubyte* version;
191 SK_GL_RET(*this->gl, version, GetString(GR_GL_VERSION));
192 log->configOption("GL_VERSION", (const char*)(version));
193
194 SK_GL_RET(*this->gl, version, GetString(GR_GL_RENDERER));
195 log->configOption("GL_RENDERER", (const char*) version);
196
197 SK_GL_RET(*this->gl, version, GetString(GR_GL_VENDOR));
198 log->configOption("GL_VENDOR", (const char*) version);
199
200 SK_GL_RET(*this->gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
201 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
202 }
203};
mtkleind75c4662015-04-30 07:11:22 -0700204
tomhudsond968a6f2015-03-26 11:28:06 -0700205#endif
206
tomhudson75a0ebb2015-03-27 12:11:44 -0700207static double time(int loops, Benchmark* bench, Target* target) {
208 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700209 if (canvas) {
210 canvas->clear(SK_ColorWHITE);
211 }
joshualitt8a6697a2015-09-30 12:11:07 -0700212 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700213 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700214 canvas = target->beginTiming(canvas);
215 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700216 if (canvas) {
217 canvas->flush();
218 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700219 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700220 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700221 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700222 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700223}
224
mtkleinf3723212014-06-25 14:08:00 -0700225static double estimate_timer_overhead() {
226 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700227 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700228 double start = now_ms();
229 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700230 }
231 return overhead / FLAGS_overheadLoops;
232}
233
reed53249782014-10-10 09:09:52 -0700234static int detect_forever_loops(int loops) {
235 // look for a magic run-forever value
236 if (loops < 0) {
237 loops = SK_MaxS32;
238 }
239 return loops;
240}
241
mtklein55b0ffc2014-07-17 08:38:23 -0700242static int clamp_loops(int loops) {
243 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800244 SkDebugf("ERROR: clamping loops from %d to 1. "
245 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700246 return 1;
247 }
248 if (loops > FLAGS_maxLoops) {
249 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
250 return FLAGS_maxLoops;
251 }
252 return loops;
253}
254
tomhudsond968a6f2015-03-26 11:28:06 -0700255static bool write_canvas_png(Target* target, const SkString& filename) {
256
bsalomon6eb03cc2014-08-07 14:28:50 -0700257 if (filename.isEmpty()) {
258 return false;
259 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700260 if (target->getCanvas() &&
261 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700262 return false;
263 }
tomhudsond968a6f2015-03-26 11:28:06 -0700264
bsalomon6eb03cc2014-08-07 14:28:50 -0700265 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700266
267 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700268 return false;
269 }
tomhudsond968a6f2015-03-26 11:28:06 -0700270
bsalomon6eb03cc2014-08-07 14:28:50 -0700271 SkString dir = SkOSPath::Dirname(filename.c_str());
272 if (!sk_mkdir(dir.c_str())) {
273 SkDebugf("Can't make dir %s.\n", dir.c_str());
274 return false;
275 }
276 SkFILEWStream stream(filename.c_str());
277 if (!stream.isValid()) {
278 SkDebugf("Can't write %s.\n", filename.c_str());
279 return false;
280 }
281 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
282 SkDebugf("Can't encode a PNG.\n");
283 return false;
284 }
285 return true;
286}
287
288static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700289static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700290 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700291 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700292 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700293 int loops = bench->calculateLoops(FLAGS_loops);
294 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700295 while (bench_plus_overhead < overhead) {
296 if (round++ == FLAGS_maxCalibrationAttempts) {
297 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700298 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700299 return kFailedLoops;
300 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700301 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700302 }
mtklein2069e222014-08-04 13:57:39 -0700303 }
mtkleinf3723212014-06-25 14:08:00 -0700304
mtkleinbb6a0282014-07-01 08:43:42 -0700305 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700306 // We'll pick N to make timer overhead negligible:
307 //
mtkleinbb6a0282014-07-01 08:43:42 -0700308 // overhead
309 // ------------------------- < FLAGS_overheadGoal
310 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700311 //
mtkleinbb6a0282014-07-01 08:43:42 -0700312 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700313 //
314 // Doing some math, we get:
315 //
mtkleinbb6a0282014-07-01 08:43:42 -0700316 // (overhead / FLAGS_overheadGoal) - overhead
317 // ------------------------------------------ < N
318 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700319 //
320 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700321 if (kAutoTuneLoops == loops) {
322 const double numer = overhead / FLAGS_overheadGoal - overhead;
323 const double denom = bench_plus_overhead - overhead;
324 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700325 loops = clamp_loops(loops);
326 } else {
327 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700328 }
mtkleinbb6a0282014-07-01 08:43:42 -0700329
mtkleinbb6a0282014-07-01 08:43:42 -0700330 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700331}
332
cdaltone1b89582015-06-25 19:17:08 -0700333static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700334 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700335 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700336 if (kAutoTuneLoops == loops) {
337 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700338 double elapsed = 0;
339 do {
mtklein527930f2014-11-06 08:04:34 -0800340 if (1<<30 == loops) {
341 // We're about to wrap. Something's wrong with the bench.
342 loops = 0;
343 break;
344 }
mtkleina189ccd2014-07-14 12:28:47 -0700345 loops *= 2;
346 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700347 // _this_ round, not still timing last round.
348 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700349 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700350 }
351 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700352
mtkleina189ccd2014-07-14 12:28:47 -0700353 // We've overshot at least a little. Scale back linearly.
354 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700355 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700356
tomhudsond968a6f2015-03-26 11:28:06 -0700357 // Make sure we're not still timing our calibration.
358 target->fence();
reed53249782014-10-10 09:09:52 -0700359 } else {
360 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700361 }
mtkleinbb6a0282014-07-01 08:43:42 -0700362
363 // Pretty much the same deal as the calibration: do some warmup to make
364 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700365 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700366 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700367 }
mtkleinbb6a0282014-07-01 08:43:42 -0700368
mtkleinbb6a0282014-07-01 08:43:42 -0700369 return loops;
370}
mtkleinbb6a0282014-07-01 08:43:42 -0700371
372static SkString to_lower(const char* str) {
373 SkString lower(str);
374 for (size_t i = 0; i < lower.size(); i++) {
375 lower[i] = tolower(lower[i]);
376 }
377 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700378}
379
bsalomonc2553372014-07-22 13:09:05 -0700380static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700381 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700382 if (to_lower(FLAGS_config[i]).equals(name)) {
383 return true;
mtkleinf3723212014-06-25 14:08:00 -0700384 }
385 }
bsalomonc2553372014-07-22 13:09:05 -0700386 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700387}
388
bsalomonc2553372014-07-22 13:09:05 -0700389#if SK_SUPPORT_GPU
390static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
kkinnunen5219fd92015-12-10 06:28:13 -0800391 GrContextFactory::GLContextOptions ctxOptions,
bsalomonc2553372014-07-22 13:09:05 -0700392 int sampleCnt) {
393 if (!is_cpu_config_allowed(name)) {
394 return false;
395 }
kkinnunene13ca322015-12-14 04:49:17 -0800396 if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOptions)) {
bsalomon76228632015-05-29 08:02:10 -0700397 return sampleCnt <= ctx->caps()->maxSampleCount();
bsalomonc2553372014-07-22 13:09:05 -0700398 }
399 return false;
400}
401#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700402
bsalomonc2553372014-07-22 13:09:05 -0700403#if SK_SUPPORT_GPU
404#define kBogusGLContextType GrContextFactory::kNative_GLContextType
kkinnunen5219fd92015-12-10 06:28:13 -0800405#define kBogusGLContextOptions GrContextFactory::kNone_GLContextOptions
bsalomonc2553372014-07-22 13:09:05 -0700406#else
407#define kBogusGLContextType 0
kkinnunen5219fd92015-12-10 06:28:13 -0800408#define kBogusGLContextOptions 0
mtkleine714e752014-07-31 12:13:48 -0700409#endif
bsalomonc2553372014-07-22 13:09:05 -0700410
411// Append all configs that are enabled and supported.
412static void create_configs(SkTDArray<Config>* configs) {
kkinnunen5219fd92015-12-10 06:28:13 -0800413 #define CPU_CONFIG(name, backend, color, alpha) \
414 if (is_cpu_config_allowed(#name)) { \
415 Config config = { #name, Benchmark::backend, color, alpha, 0, \
416 kBogusGLContextType, kBogusGLContextOptions, \
417 false }; \
418 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700419 }
mtkleine714e752014-07-31 12:13:48 -0700420
mtklein40b32be2014-07-09 08:46:49 -0700421 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700422 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
423 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
424 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700425 }
mtkleinbb6a0282014-07-01 08:43:42 -0700426
427#if SK_SUPPORT_GPU
kkinnunen5219fd92015-12-10 06:28:13 -0800428 #define GPU_CONFIG(name, ctxType, ctxOptions, samples, useDFText) \
429 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, \
430 GrContextFactory::ctxOptions, samples)) { \
bsalomonc2553372014-07-22 13:09:05 -0700431 Config config = { \
432 #name, \
433 Benchmark::kGPU_Backend, \
434 kN32_SkColorType, \
435 kPremul_SkAlphaType, \
436 samples, \
jvanverth4736e142014-11-07 07:12:46 -0800437 GrContextFactory::ctxType, \
kkinnunen5219fd92015-12-10 06:28:13 -0800438 GrContextFactory::ctxOptions, \
jvanverth4736e142014-11-07 07:12:46 -0800439 useDFText }; \
bsalomonc2553372014-07-22 13:09:05 -0700440 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700441 }
mtkleine714e752014-07-31 12:13:48 -0700442
mtklein40b32be2014-07-09 08:46:49 -0700443 if (FLAGS_gpu) {
kkinnunen5219fd92015-12-10 06:28:13 -0800444 GPU_CONFIG(gpu, kNative_GLContextType, kNone_GLContextOptions, 0, false)
445 GPU_CONFIG(msaa4, kNative_GLContextType, kNone_GLContextOptions, 4, false)
446 GPU_CONFIG(msaa16, kNative_GLContextType, kNone_GLContextOptions, 16, false)
447 GPU_CONFIG(nvprmsaa4, kNative_GLContextType, kEnableNVPR_GLContextOptions, 4, false)
448 GPU_CONFIG(nvprmsaa16, kNative_GLContextType, kEnableNVPR_GLContextOptions, 16, false)
449 GPU_CONFIG(gpudft, kNative_GLContextType, kNone_GLContextOptions, 0, true)
450 GPU_CONFIG(debug, kDebug_GLContextType, kNone_GLContextOptions, 0, false)
451 GPU_CONFIG(nullgpu, kNull_GLContextType, kNone_GLContextOptions, 0, false)
kkinnunene13ca322015-12-14 04:49:17 -0800452#if SK_ANGLE
453#ifdef SK_BUILD_FOR_WIN
kkinnunen5219fd92015-12-10 06:28:13 -0800454 GPU_CONFIG(angle, kANGLE_GLContextType, kNone_GLContextOptions, 0, false)
kkinnunene13ca322015-12-14 04:49:17 -0800455#endif
kkinnunen5219fd92015-12-10 06:28:13 -0800456 GPU_CONFIG(angle-gl, kANGLE_GL_GLContextType, kNone_GLContextOptions, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700457#endif
kkinnunene13ca322015-12-14 04:49:17 -0800458#if SK_COMMAND_BUFFER
kkinnunen5219fd92015-12-10 06:28:13 -0800459 GPU_CONFIG(commandbuffer, kCommandBuffer_GLContextType, kNone_GLContextOptions, 0, false)
hendrikw885bf092015-08-27 10:38:39 -0700460#endif
cdaltond416a5b2015-06-23 13:23:44 -0700461#if SK_MESA
kkinnunen5219fd92015-12-10 06:28:13 -0800462 GPU_CONFIG(mesa, kMESA_GLContextType, kNone_GLContextOptions, 0, false)
cdaltond416a5b2015-06-23 13:23:44 -0700463#endif
mtklein40b32be2014-07-09 08:46:49 -0700464 }
mtkleinbb6a0282014-07-01 08:43:42 -0700465#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700466
467#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
468 if (is_cpu_config_allowed("hwui")) {
469 Config config = { "hwui", Benchmark::kHWUI_Backend, kRGBA_8888_SkColorType,
kkinnunen5219fd92015-12-10 06:28:13 -0800470 kPremul_SkAlphaType, 0, kBogusGLContextType, kBogusGLContextOptions,
471 false };
tomhudsond968a6f2015-03-26 11:28:06 -0700472 configs->push(config);
473 }
474#endif
mtkleinf3723212014-06-25 14:08:00 -0700475}
476
halcanary96fcdcc2015-08-27 07:41:13 -0700477// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700478static Target* is_enabled(Benchmark* bench, const Config& config) {
479 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700480 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700481 }
482
reede5ea5002014-09-03 11:54:58 -0700483 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
484 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700485
halcanary96fcdcc2015-08-27 07:41:13 -0700486 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700487
tomhudsond968a6f2015-03-26 11:28:06 -0700488 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700489#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700490 case Benchmark::kGPU_Backend:
491 target = new GPUTarget(config);
492 break;
bsalomonc2553372014-07-22 13:09:05 -0700493#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700494#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
495 case Benchmark::kHWUI_Backend:
496 target = new HWUITarget(config, bench);
497 break;
498#endif
499 default:
500 target = new Target(config);
501 break;
502 }
bsalomonc2553372014-07-22 13:09:05 -0700503
tomhudsond968a6f2015-03-26 11:28:06 -0700504 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700505 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700506 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700507 }
508 return target;
509}
510
msarettb23e6aa2015-06-09 13:56:10 -0700511/*
scroggoab12c272015-10-08 12:01:39 -0700512 * We only run our subset benches on files that are supported by BitmapRegionDecoder:
msarett0459c942015-11-10 14:52:13 -0800513 * i.e. PNG, JPEG, and WEBP. We do *not* test WEBP, since we do not have a scanline
514 * decoder for WEBP, which is necessary for running the subset bench. (Another bench
515 * must be used to test WEBP, which decodes subsets natively.)
scroggoab12c272015-10-08 12:01:39 -0700516 */
msarett0459c942015-11-10 14:52:13 -0800517static bool run_subset_bench(const SkString& path) {
scroggoab12c272015-10-08 12:01:39 -0700518 static const char* const exts[] = {
msarett0459c942015-11-10 14:52:13 -0800519 "jpg", "jpeg", "png",
520 "JPG", "JPEG", "PNG",
scroggoab12c272015-10-08 12:01:39 -0700521 };
522
msarett0459c942015-11-10 14:52:13 -0800523 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
524 if (path.endsWith(exts[i])) {
525 return true;
scroggoab12c272015-10-08 12:01:39 -0700526 }
527 }
528
scroggo2da1a852015-10-13 13:33:33 -0700529 return false;
scroggoab12c272015-10-08 12:01:39 -0700530}
531
532/*
msarettb23e6aa2015-06-09 13:56:10 -0700533 * Returns true if set up for a subset decode succeeds, false otherwise
534 * If the set-up succeeds, the width and height parameters will be set
535 */
msarett0459c942015-11-10 14:52:13 -0800536static bool valid_subset_bench(const SkString& path, SkColorType colorType,
msarettb23e6aa2015-06-09 13:56:10 -0700537 int* width, int* height) {
538 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
539 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded));
540
msarett0459c942015-11-10 14:52:13 -0800541 // Check that we can create a codec.
542 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
543 if (nullptr == codec) {
544 SkDebugf("Could not create codec for %s. Skipping bench.\n", path.c_str());
545 return false;
msarettb23e6aa2015-06-09 13:56:10 -0700546 }
msarettab80e352015-06-17 10:28:22 -0700547
msarett0459c942015-11-10 14:52:13 -0800548 // These will be initialized by SkCodec if the color type is kIndex8 and
549 // unused otherwise.
550 SkPMColor colors[256];
551 int colorCount;
552 const SkImageInfo info = codec->getInfo().makeColorType(colorType);
553 if (codec->startScanlineDecode(info, nullptr, colors, &colorCount) != SkCodec::kSuccess)
554 {
555 SkDebugf("Could not create scanline decoder for %s with color type %s. "
556 "Skipping bench.\n", path.c_str(), color_type_to_str(colorType));
557 return false;
558 }
559 *width = info.width();
560 *height = info.height();
561
msarettab80e352015-06-17 10:28:22 -0700562 // Check if the image is large enough for a meaningful subset benchmark.
563 if (*width <= 512 && *height <= 512) {
564 // This should not print a message since it is not an error.
565 return false;
566 }
567
msarettb23e6aa2015-06-09 13:56:10 -0700568 return true;
569}
jcgregoriobf5e5232014-07-17 13:14:16 -0700570
msarett5cb48852015-11-06 08:56:32 -0800571static bool valid_brd_bench(SkData* encoded, SkBitmapRegionDecoder::Strategy strategy,
msarett7f691442015-09-22 11:56:16 -0700572 SkColorType colorType, uint32_t sampleSize, uint32_t minOutputSize, int* width,
573 int* height) {
msarett5cb48852015-11-06 08:56:32 -0800574 SkAutoTDelete<SkBitmapRegionDecoder> brd(
575 SkBitmapRegionDecoder::Create(encoded, strategy));
msarett7f691442015-09-22 11:56:16 -0700576 if (nullptr == brd.get()) {
577 // This is indicates that subset decoding is not supported for a particular image format.
578 return false;
579 }
580
msarett35e5d1b2015-10-27 12:50:25 -0700581 SkBitmap bitmap;
582 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, brd->width(), brd->height()),
583 1, colorType, false)) {
584 return false;
585 }
msarett7f691442015-09-22 11:56:16 -0700586
587 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
588 (uint32_t) brd->height()) {
589 // This indicates that the image is not large enough to decode a
590 // minOutputSize x minOutputSize subset at the given sampleSize.
591 return false;
592 }
593
594 // Set the image width and height. The calling code will use this to choose subsets to decode.
595 *width = brd->width();
596 *height = brd->height();
597 return true;
598}
599
egdaniel3bf92062015-06-26 08:12:46 -0700600static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700601 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700602#if SK_SUPPORT_GPU
603 if (FLAGS_abandonGpuContext) {
604 gGrFactory->abandonContexts();
605 }
606 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
607 gGrFactory->destroyContexts();
608 }
609#endif
610}
611
mtkleine714e752014-07-31 12:13:48 -0700612class BenchmarkStream {
613public:
mtklein92007582014-08-01 07:46:52 -0700614 BenchmarkStream() : fBenches(BenchRegistry::Head())
615 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700616 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700617 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800618 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800619 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700620 , fCurrentCodec(0)
msarett95f192d2015-02-13 09:05:41 -0800621 , fCurrentImage(0)
622 , fCurrentSubsetImage(0)
msarett7f691442015-09-22 11:56:16 -0700623 , fCurrentBRDImage(0)
msarett95f192d2015-02-13 09:05:41 -0800624 , fCurrentColorType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700625 , fCurrentSubsetType(0)
msarett7f691442015-09-22 11:56:16 -0700626 , fCurrentBRDStrategy(0)
627 , fCurrentBRDSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700628 , fCurrentAnimSKP(0) {
mtklein92007582014-08-01 07:46:52 -0700629 for (int i = 0; i < FLAGS_skps.count(); i++) {
630 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
631 fSKPs.push_back() = FLAGS_skps[i];
632 } else {
633 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
634 SkString path;
635 while (it.next(&path)) {
636 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
637 }
638 }
639 }
mtkleine714e752014-07-31 12:13:48 -0700640
mtklein92007582014-08-01 07:46:52 -0700641 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
642 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
643 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
644 exit(1);
645 }
646
647 for (int i = 0; i < FLAGS_scales.count(); i++) {
648 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
649 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
650 exit(1);
651 }
652 }
robertphillips5b693772014-11-21 06:19:36 -0800653
cdalton63a82852015-06-29 14:06:10 -0700654 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
655 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700656 exit(1);
657 }
658
robertphillips5b693772014-11-21 06:19:36 -0800659 if (FLAGS_mpd) {
660 fUseMPDs.push_back() = true;
661 }
mtkleinc751ecb2015-06-15 08:56:38 -0700662 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700663
msarett95f192d2015-02-13 09:05:41 -0800664 // Prepare the images for decoding
665 for (int i = 0; i < FLAGS_images.count(); i++) {
666 const char* flag = FLAGS_images[i];
667 if (sk_isdir(flag)) {
668 // If the value passed in is a directory, add all the images
669 SkOSFile::Iter it(flag);
670 SkString file;
671 while (it.next(&file)) {
672 fImages.push_back() = SkOSPath::Join(flag, file.c_str());
673 }
674 } else if (sk_exists(flag)) {
675 // Also add the value if it is a single image
676 fImages.push_back() = flag;
677 }
678 }
mtklein95553d92015-03-12 08:24:21 -0700679
msarett95f192d2015-02-13 09:05:41 -0800680 // Choose the candidate color types for image decoding
681 const SkColorType colorTypes[] =
msarettb23e6aa2015-06-09 13:56:10 -0700682 { kN32_SkColorType,
683 kRGB_565_SkColorType,
684 kAlpha_8_SkColorType,
685 kIndex_8_SkColorType,
686 kGray_8_SkColorType };
msarett74deb982015-10-20 16:45:56 -0700687 fColorTypes.reset(colorTypes, SK_ARRAY_COUNT(colorTypes));
mtklein92007582014-08-01 07:46:52 -0700688 }
689
mtkleinfd731ce2014-09-10 12:19:30 -0700690 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
691 // Not strictly necessary, as it will be checked again later,
692 // but helps to avoid a lot of pointless work if we're going to skip it.
693 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
694 return false;
695 }
696
scroggoa1193e42015-01-21 12:09:53 -0800697 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -0700698 if (stream.get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700699 SkDebugf("Could not read %s.\n", path);
700 return false;
701 }
702
mtklein57f27bd2015-02-09 11:58:41 -0800703 pic->reset(SkPicture::CreateFromStream(stream.get()));
halcanary96fcdcc2015-08-27 07:41:13 -0700704 if (pic->get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700705 SkDebugf("Could not read %s as an SkPicture.\n", path);
706 return false;
707 }
708 return true;
709 }
710
mtklein92007582014-08-01 07:46:52 -0700711 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700712 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700713 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700714 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700715 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700716 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700717 return bench;
718 }
mtklein92007582014-08-01 07:46:52 -0700719
mtkleine714e752014-07-31 12:13:48 -0700720 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -0700721 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700722 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800723 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700724 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700725 fBenchType = "micro";
halcanary385fe4d2015-08-26 13:07:48 -0700726 return new GMBench(gm.detach());
mtkleine714e752014-07-31 12:13:48 -0700727 }
728 }
mtklein92007582014-08-01 07:46:52 -0700729
mtkleinfd731ce2014-09-10 12:19:30 -0700730 // First add all .skps as RecordingBenches.
731 while (fCurrentRecording < fSKPs.count()) {
732 const SkString& path = fSKPs[fCurrentRecording++];
733 SkAutoTUnref<SkPicture> pic;
734 if (!ReadPicture(path.c_str(), &pic)) {
735 continue;
736 }
737 SkString name = SkOSPath::Basename(path.c_str());
738 fSourceType = "skp";
739 fBenchType = "recording";
bsalomon0aa5cea2014-12-15 09:13:35 -0800740 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic));
mtklein051e56d2014-12-04 08:46:51 -0800741 fSKPOps = pic->approximateOpCount();
halcanary385fe4d2015-08-26 13:07:48 -0700742 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700743 }
744
745 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700746 while (fCurrentScale < fScales.count()) {
747 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800748 const SkString& path = fSKPs[fCurrentSKP];
mtkleinfd731ce2014-09-10 12:19:30 -0700749 SkAutoTUnref<SkPicture> pic;
750 if (!ReadPicture(path.c_str(), &pic)) {
robertphillips5b693772014-11-21 06:19:36 -0800751 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700752 continue;
753 }
robertphillips5b693772014-11-21 06:19:36 -0800754
755 while (fCurrentUseMPD < fUseMPDs.count()) {
756 if (FLAGS_bbh) {
757 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
758 SkRTreeFactory factory;
759 SkPictureRecorder recorder;
760 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
761 pic->playback(recorder.beginRecording(pic->cullRect().width(),
762 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800763 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800764 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
robertphillips5b693772014-11-21 06:19:36 -0800765 pic.reset(recorder.endRecording());
766 }
767 SkString name = SkOSPath::Basename(path.c_str());
768 fSourceType = "skp";
769 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700770 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
771 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700772 }
robertphillips5b693772014-11-21 06:19:36 -0800773 fCurrentUseMPD = 0;
774 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700775 }
776 fCurrentSKP = 0;
777 fCurrentScale++;
778 }
779
joshualitt261c3ad2015-04-27 09:16:57 -0700780 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700781 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700782 while (fCurrentAnimSKP < fSKPs.count()) {
783 const SkString& path = fSKPs[fCurrentAnimSKP];
784 SkAutoTUnref<SkPicture> pic;
785 if (!ReadPicture(path.c_str(), &pic)) {
786 fCurrentAnimSKP++;
787 continue;
788 }
789
790 fCurrentAnimSKP++;
791 SkString name = SkOSPath::Basename(path.c_str());
cdalton63a82852015-06-29 14:06:10 -0700792 SkAutoTUnref<SKPAnimationBench::Animation> animation(
793 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
halcanary385fe4d2015-08-26 13:07:48 -0700794 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation,
795 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700796 }
797 }
798
scroggo60869a42015-04-01 12:09:17 -0700799 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700800 fSourceType = "image";
801 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700802 const SkString& path = fImages[fCurrentCodec];
803 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
804 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700805 if (!codec) {
806 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700807 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700808 continue;
809 }
scroggo21027992015-04-02 13:22:38 -0700810
scroggo60869a42015-04-01 12:09:17 -0700811 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700812 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo60869a42015-04-01 12:09:17 -0700813 fCurrentColorType++;
scroggo21027992015-04-02 13:22:38 -0700814
scroggo60869a42015-04-01 12:09:17 -0700815 // Make sure we can decode to this color type.
scroggo60869a42015-04-01 12:09:17 -0700816 SkImageInfo info = codec->getInfo().makeColorType(colorType);
scroggo21027992015-04-02 13:22:38 -0700817 SkAlphaType alphaType;
818 if (!SkColorTypeValidateAlphaType(colorType, info.alphaType(),
819 &alphaType)) {
820 continue;
821 }
822 if (alphaType != info.alphaType()) {
823 info = info.makeAlphaType(alphaType);
824 }
825
826 const size_t rowBytes = info.minRowBytes();
827 SkAutoMalloc storage(info.getSafeSize(rowBytes));
828
829 // Used if fCurrentColorType is kIndex_8_SkColorType
830 int colorCount = 256;
831 SkPMColor colors[256];
832
scroggoeb602a52015-07-09 08:16:03 -0700833 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700834 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700835 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700836 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700837 case SkCodec::kSuccess:
838 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700839 return new CodecBench(SkOSPath::Basename(path.c_str()),
840 encoded, colorType);
scroggoeb602a52015-07-09 08:16:03 -0700841 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700842 // This is okay. Not all conversions are valid.
843 break;
scroggo60869a42015-04-01 12:09:17 -0700844 default:
845 // This represents some sort of failure.
846 SkASSERT(false);
847 break;
848 }
849 }
850 fCurrentColorType = 0;
851 }
852
msarett95f192d2015-02-13 09:05:41 -0800853 // Run the DecodingBenches
854 while (fCurrentImage < fImages.count()) {
scroggo303fa352015-10-05 11:03:34 -0700855 fSourceType = "image";
856 fBenchType = "skimagedecoder";
msarett95f192d2015-02-13 09:05:41 -0800857 while (fCurrentColorType < fColorTypes.count()) {
858 const SkString& path = fImages[fCurrentImage];
859 SkColorType colorType = fColorTypes[fCurrentColorType];
860 fCurrentColorType++;
scroggo60869a42015-04-01 12:09:17 -0700861 // Check if the image decodes to the right color type
862 // before creating the benchmark
msarett95f192d2015-02-13 09:05:41 -0800863 SkBitmap bitmap;
864 if (SkImageDecoder::DecodeFile(path.c_str(), &bitmap,
scroggo60869a42015-04-01 12:09:17 -0700865 colorType, SkImageDecoder::kDecodePixels_Mode)
866 && bitmap.colorType() == colorType) {
msarett95f192d2015-02-13 09:05:41 -0800867 return new DecodingBench(path, colorType);
868 }
869 }
870 fCurrentColorType = 0;
871 fCurrentImage++;
872 }
873
msarettb23e6aa2015-06-09 13:56:10 -0700874 // Run the SubsetBenches
msarett0459c942015-11-10 14:52:13 -0800875 while (fCurrentSubsetImage < fImages.count()) {
scroggo303fa352015-10-05 11:03:34 -0700876 fSourceType = "image";
msarett0459c942015-11-10 14:52:13 -0800877 fBenchType = "skcodec";
878 const SkString& path = fImages[fCurrentSubsetImage];
879 if (!run_subset_bench(path)) {
msarettb23e6aa2015-06-09 13:56:10 -0700880 fCurrentSubsetImage++;
msarett0459c942015-11-10 14:52:13 -0800881 continue;
msarett95f192d2015-02-13 09:05:41 -0800882 }
msarett0459c942015-11-10 14:52:13 -0800883 while (fCurrentColorType < fColorTypes.count()) {
884 SkColorType colorType = fColorTypes[fCurrentColorType];
885 while (fCurrentSubsetType <= kLast_SubsetType) {
886 int width = 0;
887 int height = 0;
888 int currentSubsetType = fCurrentSubsetType++;
889 if (valid_subset_bench(path, colorType, &width, &height)) {
890 switch (currentSubsetType) {
891 case kTopLeft_SubsetType:
892 return new SubsetSingleBench(path, colorType, width/3,
893 height/3, 0, 0);
894 case kTopRight_SubsetType:
895 return new SubsetSingleBench(path, colorType, width/3,
896 height/3, 2*width/3, 0);
897 case kMiddle_SubsetType:
898 return new SubsetSingleBench(path, colorType, width/3,
899 height/3, width/3, height/3);
900 case kBottomLeft_SubsetType:
901 return new SubsetSingleBench(path, colorType, width/3,
902 height/3, 0, 2*height/3);
903 case kBottomRight_SubsetType:
904 return new SubsetSingleBench(path, colorType, width/3,
905 height/3, 2*width/3, 2*height/3);
906 case kTranslate_SubsetType:
907 return new SubsetTranslateBench(path, colorType, 512, 512);
908 case kZoom_SubsetType:
909 return new SubsetZoomBench(path, colorType, 512, 512);
910 }
911 } else {
912 break;
913 }
914 }
915 fCurrentSubsetType = 0;
916 fCurrentColorType++;
917 }
918 fCurrentColorType = 0;
919 fCurrentSubsetImage++;
msarett95f192d2015-02-13 09:05:41 -0800920 }
921
msarett7f691442015-09-22 11:56:16 -0700922 // Run the BRDBenches
923 // We will benchmark multiple BRD strategies.
scroggo303fa352015-10-05 11:03:34 -0700924 static const struct {
msarett5cb48852015-11-06 08:56:32 -0800925 SkBitmapRegionDecoder::Strategy fStrategy;
msarett0459c942015-11-10 14:52:13 -0800926 const char* fName;
scroggo303fa352015-10-05 11:03:34 -0700927 } strategies[] = {
msarett0459c942015-11-10 14:52:13 -0800928 { SkBitmapRegionDecoder::kCanvas_Strategy, "BRD_canvas" },
msarett5cb48852015-11-06 08:56:32 -0800929 { SkBitmapRegionDecoder::kAndroidCodec_Strategy, "BRD_android_codec" },
msarett7f691442015-09-22 11:56:16 -0700930 };
931
932 // We intend to create benchmarks that model the use cases in
933 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
934 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
935 // the image. For that reason, we will benchmark decodes in five representative locations
936 // in the image. Additionally, this use case utilizes power of two scaling, so we will
937 // test on power of two sample sizes. The output tile is always 512x512, so, when a
938 // sampleSize is used, the size of the subset that is decoded is always
939 // (sampleSize*512)x(sampleSize*512).
940 // There are a few good reasons to only test on power of two sample sizes at this time:
941 // JPEG decodes using kOriginal_Strategy are broken for non-powers of two.
halcanary6950de62015-11-07 05:29:00 -0800942 // https://bug.skia.org/4319
msarett7f691442015-09-22 11:56:16 -0700943 // All use cases we are aware of only scale by powers of two.
944 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
945 // these tests are sufficient to provide good coverage of our scaling options.
scroggo501b7342015-11-03 07:55:11 -0800946 const uint32_t sampleSizes[] = { 1, 2, 4, 8, 16, 32, 64 };
msarett7f691442015-09-22 11:56:16 -0700947 const uint32_t minOutputSize = 512;
948 while (fCurrentBRDImage < fImages.count()) {
949 while (fCurrentBRDStrategy < (int) SK_ARRAY_COUNT(strategies)) {
scroggo303fa352015-10-05 11:03:34 -0700950 fSourceType = "image";
951 fBenchType = strategies[fCurrentBRDStrategy].fName;
scroggo860e8a62015-10-15 07:51:28 -0700952
953 const SkString& path = fImages[fCurrentBRDImage];
msarett5cb48852015-11-06 08:56:32 -0800954 const SkBitmapRegionDecoder::Strategy strategy =
scroggo860e8a62015-10-15 07:51:28 -0700955 strategies[fCurrentBRDStrategy].fStrategy;
956
msarett7f691442015-09-22 11:56:16 -0700957 while (fCurrentColorType < fColorTypes.count()) {
958 while (fCurrentBRDSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
959 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700960
961
msarett7f691442015-09-22 11:56:16 -0700962 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
963 const SkColorType colorType = fColorTypes[fCurrentColorType];
964 uint32_t sampleSize = sampleSizes[fCurrentBRDSampleSize];
965 int currentSubsetType = fCurrentSubsetType++;
966
967 int width = 0;
968 int height = 0;
969 if (!valid_brd_bench(encoded.get(), strategy, colorType, sampleSize,
970 minOutputSize, &width, &height)) {
971 break;
972 }
973
974 SkString basename = SkOSPath::Basename(path.c_str());
975 SkIRect subset;
976 const uint32_t subsetSize = sampleSize * minOutputSize;
977 switch (currentSubsetType) {
978 case kTopLeft_SubsetType:
979 basename.append("_TopLeft");
980 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
981 break;
982 case kTopRight_SubsetType:
983 basename.append("_TopRight");
984 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
985 subsetSize);
986 break;
987 case kMiddle_SubsetType:
988 basename.append("_Middle");
989 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
990 (height - subsetSize) / 2, subsetSize, subsetSize);
991 break;
992 case kBottomLeft_SubsetType:
993 basename.append("_BottomLeft");
994 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
995 subsetSize);
996 break;
997 case kBottomRight_SubsetType:
998 basename.append("_BottomRight");
999 subset = SkIRect::MakeXYWH(width - subsetSize,
1000 height - subsetSize, subsetSize, subsetSize);
1001 break;
1002 default:
1003 SkASSERT(false);
1004 }
1005
1006 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
1007 strategy, colorType, sampleSize, subset);
1008 }
1009 fCurrentSubsetType = 0;
1010 fCurrentBRDSampleSize++;
1011 }
1012 fCurrentBRDSampleSize = 0;
1013 fCurrentColorType++;
1014 }
1015 fCurrentColorType = 0;
1016 fCurrentBRDStrategy++;
1017 }
1018 fCurrentBRDStrategy = 0;
1019 fCurrentBRDImage++;
1020 }
1021
halcanary96fcdcc2015-08-27 07:41:13 -07001022 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001023 }
mtklein92007582014-08-01 07:46:52 -07001024
1025 void fillCurrentOptions(ResultsWriter* log) const {
1026 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001027 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001028 if (0 == strcmp(fSourceType, "skp")) {
1029 log->configOption("clip",
1030 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1031 fClip.fRight, fClip.fBottom).c_str());
mtklein4dfdbb12015-10-20 07:45:29 -07001032 SK_ALWAYSBREAK(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -07001033 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001034 if (fCurrentUseMPD > 0) {
1035 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1036 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1037 }
mtklein92007582014-08-01 07:46:52 -07001038 }
mtklein051e56d2014-12-04 08:46:51 -08001039 if (0 == strcmp(fBenchType, "recording")) {
1040 log->metric("bytes", fSKPBytes);
1041 log->metric("ops", fSKPOps);
1042 }
mtklein92007582014-08-01 07:46:52 -07001043 }
1044
mtkleine714e752014-07-31 12:13:48 -07001045private:
msarettb23e6aa2015-06-09 13:56:10 -07001046 enum SubsetType {
1047 kTopLeft_SubsetType = 0,
1048 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001049 kMiddle_SubsetType = 2,
1050 kBottomLeft_SubsetType = 3,
1051 kBottomRight_SubsetType = 4,
1052 kTranslate_SubsetType = 5,
1053 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001054 kLast_SubsetType = kZoom_SubsetType,
1055 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001056 };
1057
mtkleine714e752014-07-31 12:13:48 -07001058 const BenchRegistry* fBenches;
1059 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001060 SkIRect fClip;
1061 SkTArray<SkScalar> fScales;
1062 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -08001063 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001064 SkTArray<SkString> fImages;
msarett74deb982015-10-20 16:45:56 -07001065 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001066 SkScalar fZoomMax;
1067 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001068
mtklein051e56d2014-12-04 08:46:51 -08001069 double fSKPBytes, fSKPOps;
1070
mtkleinfd731ce2014-09-10 12:19:30 -07001071 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1072 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1073 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -07001074 int fCurrentScale;
1075 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -08001076 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001077 int fCurrentCodec;
msarett95f192d2015-02-13 09:05:41 -08001078 int fCurrentImage;
1079 int fCurrentSubsetImage;
msarett7f691442015-09-22 11:56:16 -07001080 int fCurrentBRDImage;
msarett95f192d2015-02-13 09:05:41 -08001081 int fCurrentColorType;
msarettb23e6aa2015-06-09 13:56:10 -07001082 int fCurrentSubsetType;
msarett7f691442015-09-22 11:56:16 -07001083 int fCurrentBRDStrategy;
1084 int fCurrentBRDSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001085 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001086};
1087
jcgregorio3b27ade2014-11-13 08:06:40 -08001088int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001089int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001090 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001091 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001092 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001093
krajcevski69a55602014-08-13 10:46:31 -07001094#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001095 GrContextOptions grContextOpts;
krajcevski12b35442014-08-13 12:06:26 -07001096 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
halcanary385fe4d2015-08-26 13:07:48 -07001097 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001098#endif
1099
bsalomon06cddec2014-10-24 10:40:50 -07001100 if (FLAGS_veryVerbose) {
1101 FLAGS_verbose = true;
1102 }
1103
bsalomon6eb03cc2014-08-07 14:28:50 -07001104 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001105 FLAGS_samples = 1;
1106 FLAGS_gpuFrameLag = 0;
1107 }
1108
bsalomon6eb03cc2014-08-07 14:28:50 -07001109 if (!FLAGS_writePath.isEmpty()) {
1110 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1111 if (!sk_mkdir(FLAGS_writePath[0])) {
1112 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001113 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001114 }
1115 }
1116
halcanary385fe4d2015-08-26 13:07:48 -07001117 SkAutoTDelete<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001118 if (!FLAGS_outResultsFile.isEmpty()) {
halcanary385fe4d2015-08-26 13:07:48 -07001119 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein60317d0f2014-07-14 11:30:37 -07001120 }
mtklein1915b622014-08-20 11:45:00 -07001121
1122 if (1 == FLAGS_properties.count() % 2) {
1123 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1124 return 1;
1125 }
1126 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1127 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1128 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001129
1130 if (1 == FLAGS_key.count() % 2) {
1131 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1132 return 1;
1133 }
1134 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001135 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001136 }
mtklein60317d0f2014-07-14 11:30:37 -07001137
mtkleinf3723212014-06-25 14:08:00 -07001138 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001139 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001140
cdaltone1b89582015-06-25 19:17:08 -07001141 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001142
bsalomon6eb03cc2014-08-07 14:28:50 -07001143 if (kAutoTuneLoops != FLAGS_loops) {
1144 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001145 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001146 SkDebugf("! -> high variance, ? -> moderate variance\n");
1147 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001148 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001149 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001150 } else {
mtkleind75c4662015-04-30 07:11:22 -07001151 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001152 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001153 }
1154
bsalomonc2553372014-07-22 13:09:05 -07001155 SkTDArray<Config> configs;
1156 create_configs(&configs);
1157
mtkleine070c2b2014-10-14 08:40:43 -07001158 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001159 BenchmarkStream benchStream;
1160 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001161 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001162 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001163 continue;
1164 }
1165
egdaniel3bf92062015-06-26 08:12:46 -07001166 if (!configs.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -07001167 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001168 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001169 }
egdaniel3bf92062015-06-26 08:12:46 -07001170 for (int i = 0; i < configs.count(); ++i) {
1171 Target* target = is_enabled(b, configs[i]);
1172 if (!target) {
1173 continue;
1174 }
mtkleinf3723212014-06-25 14:08:00 -07001175
halcanary96fcdcc2015-08-27 07:41:13 -07001176 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001177 SkCanvas* canvas = target->getCanvas();
1178 const char* config = target->config.name;
1179
1180 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001181 bench->perCanvasPreDraw(canvas);
1182
cdaltone1b89582015-06-25 19:17:08 -07001183 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001184 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001185 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1186 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001187
mtkleinbbba1682015-10-28 11:36:30 -07001188 if (FLAGS_ms) {
1189 samples.reset();
1190 auto stop = now_ms() + FLAGS_ms;
1191 do {
1192 samples.push_back(time(loops, bench, target) / loops);
1193 } while (now_ms() < stop);
1194 } else {
cdaltone1b89582015-06-25 19:17:08 -07001195 samples.reset(FLAGS_samples);
1196 for (int s = 0; s < FLAGS_samples; s++) {
egdaniel3bf92062015-06-26 08:12:46 -07001197 samples[s] = time(loops, bench, target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001198 }
cdaltone1b89582015-06-25 19:17:08 -07001199 }
mtkleinf3723212014-06-25 14:08:00 -07001200
joshualitte45c81c2015-12-02 09:05:37 -08001201#if SK_SUPPORT_GPU
1202 SkTArray<SkString> keys;
1203 SkTArray<double> values;
1204 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1205 if (gpuStatsDump) {
1206 // TODO cache stats
1207 bench->getGpuStats(canvas, &keys, &values);
1208 }
1209#endif
1210
robertphillips5b693772014-11-21 06:19:36 -08001211 bench->perCanvasPostDraw(canvas);
1212
egdaniel3bf92062015-06-26 08:12:46 -07001213 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001214 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001215 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001216 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001217 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001218 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001219 }
1220
1221 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001222 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001223 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001224 continue;
1225 }
1226
cdaltone1b89582015-06-25 19:17:08 -07001227 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001228 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001229 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001230 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001231 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001232 log->metric("min_ms", stats.min);
joshualitte45c81c2015-12-02 09:05:37 -08001233#if SK_SUPPORT_GPU
1234 if (gpuStatsDump) {
1235 // dump to json, only SKPBench currently returns valid keys / values
1236 SkASSERT(keys.count() == values.count());
1237 for (int i = 0; i < keys.count(); i++) {
1238 log->metric(keys[i].c_str(), values[i]);
1239 }
1240 }
1241#endif
1242
mtkleine070c2b2014-10-14 08:40:43 -07001243 if (runs++ % FLAGS_flushEvery == 0) {
1244 log->flush();
1245 }
mtklein60317d0f2014-07-14 11:30:37 -07001246
bsalomon6eb03cc2014-08-07 14:28:50 -07001247 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001248 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001249 config = ""; // Only print the config if we run the same bench on more than one.
1250 }
mtkleind75c4662015-04-30 07:11:22 -07001251 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1252 , sk_tools::getCurrResidentSetSizeMB()
1253 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001254 , bench->getUniqueName()
1255 , config);
mtkleinf3723212014-06-25 14:08:00 -07001256 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001257 const char* mark = " ";
1258 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1259 if (stddev_percent > 5) mark = "?";
1260 if (stddev_percent > 10) mark = "!";
1261
1262 SkDebugf("%10.2f %s\t%s\t%s\n",
1263 stats.median*1e3, mark, bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001264 } else {
1265 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001266 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1267 , sk_tools::getCurrResidentSetSizeMB()
1268 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001269 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001270 , HUMANIZE(stats.min)
1271 , HUMANIZE(stats.median)
1272 , HUMANIZE(stats.mean)
1273 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001274 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001275 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001276 , config
mtklein96289052014-09-10 12:05:59 -07001277 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001278 );
1279 }
joshualitte45c81c2015-12-02 09:05:37 -08001280
bsalomonb12ea412015-02-02 21:19:50 -08001281#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001282 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001283 GrContext* context = gGrFactory->get(configs[i].ctxType,
kkinnunene13ca322015-12-14 04:49:17 -08001284 configs[i].ctxOptions);
kkinnunen5219fd92015-12-10 06:28:13 -08001285 context->printCacheStats();
1286 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001287 }
1288#endif
joshualitte45c81c2015-12-02 09:05:37 -08001289
cdalton2c56ba52015-06-26 13:32:53 -07001290 if (FLAGS_verbose) {
1291 SkDebugf("Samples: ");
1292 for (int i = 0; i < samples.count(); i++) {
1293 SkDebugf("%s ", HUMANIZE(samples[i]));
1294 }
1295 SkDebugf("%s\n", bench->getUniqueName());
1296 }
egdaniel3bf92062015-06-26 08:12:46 -07001297 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001298 }
mtkleinf3723212014-06-25 14:08:00 -07001299 }
1300
mtkleine1091452014-12-04 10:47:02 -08001301 log->bench("memory_usage", 0,0);
1302 log->config("meta");
1303 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1304
joshualitte0b19d42015-03-26 10:41:02 -07001305#if SK_SUPPORT_GPU
1306 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1307 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001308 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001309#endif
1310
mtkleinf3723212014-06-25 14:08:00 -07001311 return 0;
1312}
1313
jcgregorio3b27ade2014-11-13 08:06:40 -08001314#if !defined SK_BUILD_FOR_IOS
1315int main(int argc, char** argv) {
1316 SkCommandLineFlags::Parse(argc, argv);
1317 return nanobench_main();
1318}
1319#endif