blob: 602d6137ca77c1332faeebbb1a4665d57e424615 [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?");
joshualitte45c81c2015-12-02 09:05:37 -0800110DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
mtklein92007582014-08-01 07:46:52 -0700111
mtkleinbbba1682015-10-28 11:36:30 -0700112static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
113
mtkleinf3723212014-06-25 14:08:00 -0700114static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700115 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800116 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700117}
mtklein55b0ffc2014-07-17 08:38:23 -0700118#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700119
tomhudsond968a6f2015-03-26 11:28:06 -0700120bool Target::init(SkImageInfo info, Benchmark* bench) {
121 if (Benchmark::kRaster_Backend == config.backend) {
122 this->surface.reset(SkSurface::NewRaster(info));
123 if (!this->surface.get()) {
124 return false;
125 }
126 }
127 return true;
128}
129bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700130 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700131 if (!canvas) {
132 return false;
133 }
134 bmp->setInfo(canvas->imageInfo());
135 if (!canvas->readPixels(bmp, 0, 0)) {
136 SkDebugf("Can't read canvas pixels.\n");
137 return false;
138 }
139 return true;
140}
141
142#if SK_SUPPORT_GPU
143struct GPUTarget : public Target {
halcanary96fcdcc2015-08-27 07:41:13 -0700144 explicit GPUTarget(const Config& c) : Target(c), gl(nullptr) { }
tomhudsond968a6f2015-03-26 11:28:06 -0700145 SkGLContext* gl;
146
147 void setup() override {
148 this->gl->makeCurrent();
149 // Make sure we're done with whatever came before.
150 SK_GL(*this->gl, Finish());
151 }
152 void endTiming() override {
153 if (this->gl) {
154 SK_GL(*this->gl, Flush());
155 this->gl->swapBuffers();
156 }
157 }
158 void fence() override {
159 SK_GL(*this->gl, Finish());
160 }
mtkleind75c4662015-04-30 07:11:22 -0700161
cdaltond416a5b2015-06-23 13:23:44 -0700162 bool needsFrameTiming(int* maxFrameLag) const override {
163 if (!this->gl->getMaxGpuFrameLag(maxFrameLag)) {
164 // Frame lag is unknown.
165 *maxFrameLag = FLAGS_gpuFrameLag;
166 }
167 return true;
168 }
tomhudsond968a6f2015-03-26 11:28:06 -0700169 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700170 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
171 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700172 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
bsalomona0a024e2015-12-01 07:58:44 -0800173 this->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(this->config.ctxType),
tomhudsond968a6f2015-03-26 11:28:06 -0700174 SkSurface::kNo_Budgeted, info,
175 this->config.samples, &props));
bsalomona0a024e2015-12-01 07:58:44 -0800176 this->gl = gGrFactory->getContextInfo(this->config.ctxType)->fGLContext;
tomhudsond968a6f2015-03-26 11:28:06 -0700177 if (!this->surface.get()) {
178 return false;
179 }
cdaltond416a5b2015-06-23 13:23:44 -0700180 if (!this->gl->fenceSyncSupport()) {
181 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
182 "Timings might not be accurate.\n", this->config.name);
183 }
tomhudsond968a6f2015-03-26 11:28:06 -0700184 return true;
185 }
186 void fillOptions(ResultsWriter* log) override {
187 const GrGLubyte* version;
188 SK_GL_RET(*this->gl, version, GetString(GR_GL_VERSION));
189 log->configOption("GL_VERSION", (const char*)(version));
190
191 SK_GL_RET(*this->gl, version, GetString(GR_GL_RENDERER));
192 log->configOption("GL_RENDERER", (const char*) version);
193
194 SK_GL_RET(*this->gl, version, GetString(GR_GL_VENDOR));
195 log->configOption("GL_VENDOR", (const char*) version);
196
197 SK_GL_RET(*this->gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
198 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
199 }
200};
mtkleind75c4662015-04-30 07:11:22 -0700201
tomhudsond968a6f2015-03-26 11:28:06 -0700202#endif
203
tomhudson75a0ebb2015-03-27 12:11:44 -0700204static double time(int loops, Benchmark* bench, Target* target) {
205 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700206 if (canvas) {
207 canvas->clear(SK_ColorWHITE);
208 }
joshualitt8a6697a2015-09-30 12:11:07 -0700209 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700210 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700211 canvas = target->beginTiming(canvas);
212 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700213 if (canvas) {
214 canvas->flush();
215 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700216 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700217 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700218 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700219 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700220}
221
mtkleinf3723212014-06-25 14:08:00 -0700222static double estimate_timer_overhead() {
223 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700224 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700225 double start = now_ms();
226 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700227 }
228 return overhead / FLAGS_overheadLoops;
229}
230
reed53249782014-10-10 09:09:52 -0700231static int detect_forever_loops(int loops) {
232 // look for a magic run-forever value
233 if (loops < 0) {
234 loops = SK_MaxS32;
235 }
236 return loops;
237}
238
mtklein55b0ffc2014-07-17 08:38:23 -0700239static int clamp_loops(int loops) {
240 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800241 SkDebugf("ERROR: clamping loops from %d to 1. "
242 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700243 return 1;
244 }
245 if (loops > FLAGS_maxLoops) {
246 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
247 return FLAGS_maxLoops;
248 }
249 return loops;
250}
251
tomhudsond968a6f2015-03-26 11:28:06 -0700252static bool write_canvas_png(Target* target, const SkString& filename) {
253
bsalomon6eb03cc2014-08-07 14:28:50 -0700254 if (filename.isEmpty()) {
255 return false;
256 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700257 if (target->getCanvas() &&
258 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700259 return false;
260 }
tomhudsond968a6f2015-03-26 11:28:06 -0700261
bsalomon6eb03cc2014-08-07 14:28:50 -0700262 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700263
264 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700265 return false;
266 }
tomhudsond968a6f2015-03-26 11:28:06 -0700267
bsalomon6eb03cc2014-08-07 14:28:50 -0700268 SkString dir = SkOSPath::Dirname(filename.c_str());
269 if (!sk_mkdir(dir.c_str())) {
270 SkDebugf("Can't make dir %s.\n", dir.c_str());
271 return false;
272 }
273 SkFILEWStream stream(filename.c_str());
274 if (!stream.isValid()) {
275 SkDebugf("Can't write %s.\n", filename.c_str());
276 return false;
277 }
278 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
279 SkDebugf("Can't encode a PNG.\n");
280 return false;
281 }
282 return true;
283}
284
285static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700286static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700287 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700288 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700289 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700290 int loops = bench->calculateLoops(FLAGS_loops);
291 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700292 while (bench_plus_overhead < overhead) {
293 if (round++ == FLAGS_maxCalibrationAttempts) {
294 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700295 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700296 return kFailedLoops;
297 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700298 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700299 }
mtklein2069e222014-08-04 13:57:39 -0700300 }
mtkleinf3723212014-06-25 14:08:00 -0700301
mtkleinbb6a0282014-07-01 08:43:42 -0700302 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700303 // We'll pick N to make timer overhead negligible:
304 //
mtkleinbb6a0282014-07-01 08:43:42 -0700305 // overhead
306 // ------------------------- < FLAGS_overheadGoal
307 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700308 //
mtkleinbb6a0282014-07-01 08:43:42 -0700309 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700310 //
311 // Doing some math, we get:
312 //
mtkleinbb6a0282014-07-01 08:43:42 -0700313 // (overhead / FLAGS_overheadGoal) - overhead
314 // ------------------------------------------ < N
315 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700316 //
317 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700318 if (kAutoTuneLoops == loops) {
319 const double numer = overhead / FLAGS_overheadGoal - overhead;
320 const double denom = bench_plus_overhead - overhead;
321 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700322 loops = clamp_loops(loops);
323 } else {
324 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700325 }
mtkleinbb6a0282014-07-01 08:43:42 -0700326
mtkleinbb6a0282014-07-01 08:43:42 -0700327 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700328}
329
cdaltone1b89582015-06-25 19:17:08 -0700330static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700331 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700332 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700333 if (kAutoTuneLoops == loops) {
334 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700335 double elapsed = 0;
336 do {
mtklein527930f2014-11-06 08:04:34 -0800337 if (1<<30 == loops) {
338 // We're about to wrap. Something's wrong with the bench.
339 loops = 0;
340 break;
341 }
mtkleina189ccd2014-07-14 12:28:47 -0700342 loops *= 2;
343 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700344 // _this_ round, not still timing last round.
345 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700346 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700347 }
348 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700349
mtkleina189ccd2014-07-14 12:28:47 -0700350 // We've overshot at least a little. Scale back linearly.
351 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700352 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700353
tomhudsond968a6f2015-03-26 11:28:06 -0700354 // Make sure we're not still timing our calibration.
355 target->fence();
reed53249782014-10-10 09:09:52 -0700356 } else {
357 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700358 }
mtkleinbb6a0282014-07-01 08:43:42 -0700359
360 // Pretty much the same deal as the calibration: do some warmup to make
361 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700362 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700363 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700364 }
mtkleinbb6a0282014-07-01 08:43:42 -0700365
mtkleinbb6a0282014-07-01 08:43:42 -0700366 return loops;
367}
mtkleinbb6a0282014-07-01 08:43:42 -0700368
369static SkString to_lower(const char* str) {
370 SkString lower(str);
371 for (size_t i = 0; i < lower.size(); i++) {
372 lower[i] = tolower(lower[i]);
373 }
374 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700375}
376
bsalomonc2553372014-07-22 13:09:05 -0700377static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700378 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700379 if (to_lower(FLAGS_config[i]).equals(name)) {
380 return true;
mtkleinf3723212014-06-25 14:08:00 -0700381 }
382 }
bsalomonc2553372014-07-22 13:09:05 -0700383 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700384}
385
bsalomonc2553372014-07-22 13:09:05 -0700386#if SK_SUPPORT_GPU
387static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
388 int sampleCnt) {
389 if (!is_cpu_config_allowed(name)) {
390 return false;
391 }
bsalomona0a024e2015-12-01 07:58:44 -0800392 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomon76228632015-05-29 08:02:10 -0700393 return sampleCnt <= ctx->caps()->maxSampleCount();
bsalomonc2553372014-07-22 13:09:05 -0700394 }
395 return false;
396}
397#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700398
bsalomonc2553372014-07-22 13:09:05 -0700399#if SK_SUPPORT_GPU
400#define kBogusGLContextType GrContextFactory::kNative_GLContextType
401#else
402#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700403#endif
bsalomonc2553372014-07-22 13:09:05 -0700404
405// Append all configs that are enabled and supported.
406static void create_configs(SkTDArray<Config>* configs) {
bsalomona0a024e2015-12-01 07:58:44 -0800407 #define CPU_CONFIG(name, backend, color, alpha) \
408 if (is_cpu_config_allowed(#name)) { \
409 Config config = { #name, Benchmark::backend, color, alpha, 0, \
410 kBogusGLContextType, false }; \
411 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700412 }
mtkleine714e752014-07-31 12:13:48 -0700413
mtklein40b32be2014-07-09 08:46:49 -0700414 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700415 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
416 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
417 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700418 }
mtkleinbb6a0282014-07-01 08:43:42 -0700419
420#if SK_SUPPORT_GPU
bsalomona0a024e2015-12-01 07:58:44 -0800421 #define GPU_CONFIG(name, ctxType, samples, useDFText) \
422 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
bsalomonc2553372014-07-22 13:09:05 -0700423 Config config = { \
424 #name, \
425 Benchmark::kGPU_Backend, \
426 kN32_SkColorType, \
427 kPremul_SkAlphaType, \
428 samples, \
jvanverth4736e142014-11-07 07:12:46 -0800429 GrContextFactory::ctxType, \
430 useDFText }; \
bsalomonc2553372014-07-22 13:09:05 -0700431 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700432 }
mtkleine714e752014-07-31 12:13:48 -0700433
mtklein40b32be2014-07-09 08:46:49 -0700434 if (FLAGS_gpu) {
bsalomona0a024e2015-12-01 07:58:44 -0800435 GPU_CONFIG(gpu, kNative_GLContextType, 0, false)
436 GPU_CONFIG(msaa4, kNative_GLContextType, 4, false)
437 GPU_CONFIG(msaa16, kNative_GLContextType, 16, false)
438 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4, false)
439 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16, false)
440 GPU_CONFIG(gpudft, kNative_GLContextType, 0, true)
441 GPU_CONFIG(debug, kDebug_GLContextType, 0, false)
442 GPU_CONFIG(nullgpu, kNull_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700443#ifdef SK_ANGLE
bsalomona0a024e2015-12-01 07:58:44 -0800444 GPU_CONFIG(angle, kANGLE_GLContextType, 0, false)
445 GPU_CONFIG(angle-gl, kANGLE_GL_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700446#endif
hendrikw885bf092015-08-27 10:38:39 -0700447#ifdef SK_COMMAND_BUFFER
bsalomona0a024e2015-12-01 07:58:44 -0800448 GPU_CONFIG(commandbuffer, kCommandBuffer_GLContextType, 0, false)
hendrikw885bf092015-08-27 10:38:39 -0700449#endif
cdaltond416a5b2015-06-23 13:23:44 -0700450#if SK_MESA
bsalomona0a024e2015-12-01 07:58:44 -0800451 GPU_CONFIG(mesa, kMESA_GLContextType, 0, false)
cdaltond416a5b2015-06-23 13:23:44 -0700452#endif
mtklein40b32be2014-07-09 08:46:49 -0700453 }
mtkleinbb6a0282014-07-01 08:43:42 -0700454#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700455
456#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
457 if (is_cpu_config_allowed("hwui")) {
458 Config config = { "hwui", Benchmark::kHWUI_Backend, kRGBA_8888_SkColorType,
scroggo9323dc62015-12-01 08:08:18 -0800459 kPremul_SkAlphaType, 0, kBogusGLContextType, false };
tomhudsond968a6f2015-03-26 11:28:06 -0700460 configs->push(config);
461 }
462#endif
mtkleinf3723212014-06-25 14:08:00 -0700463}
464
halcanary96fcdcc2015-08-27 07:41:13 -0700465// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700466static Target* is_enabled(Benchmark* bench, const Config& config) {
467 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700468 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700469 }
470
reede5ea5002014-09-03 11:54:58 -0700471 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
472 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700473
halcanary96fcdcc2015-08-27 07:41:13 -0700474 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700475
tomhudsond968a6f2015-03-26 11:28:06 -0700476 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700477#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700478 case Benchmark::kGPU_Backend:
479 target = new GPUTarget(config);
480 break;
bsalomonc2553372014-07-22 13:09:05 -0700481#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700482#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
483 case Benchmark::kHWUI_Backend:
484 target = new HWUITarget(config, bench);
485 break;
486#endif
487 default:
488 target = new Target(config);
489 break;
490 }
bsalomonc2553372014-07-22 13:09:05 -0700491
tomhudsond968a6f2015-03-26 11:28:06 -0700492 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700493 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700494 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700495 }
496 return target;
497}
498
msarettb23e6aa2015-06-09 13:56:10 -0700499/*
scroggoab12c272015-10-08 12:01:39 -0700500 * We only run our subset benches on files that are supported by BitmapRegionDecoder:
msarett0459c942015-11-10 14:52:13 -0800501 * i.e. PNG, JPEG, and WEBP. We do *not* test WEBP, since we do not have a scanline
502 * decoder for WEBP, which is necessary for running the subset bench. (Another bench
503 * must be used to test WEBP, which decodes subsets natively.)
scroggoab12c272015-10-08 12:01:39 -0700504 */
msarett0459c942015-11-10 14:52:13 -0800505static bool run_subset_bench(const SkString& path) {
scroggoab12c272015-10-08 12:01:39 -0700506 static const char* const exts[] = {
msarett0459c942015-11-10 14:52:13 -0800507 "jpg", "jpeg", "png",
508 "JPG", "JPEG", "PNG",
scroggoab12c272015-10-08 12:01:39 -0700509 };
510
msarett0459c942015-11-10 14:52:13 -0800511 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
512 if (path.endsWith(exts[i])) {
513 return true;
scroggoab12c272015-10-08 12:01:39 -0700514 }
515 }
516
scroggo2da1a852015-10-13 13:33:33 -0700517 return false;
scroggoab12c272015-10-08 12:01:39 -0700518}
519
520/*
msarettb23e6aa2015-06-09 13:56:10 -0700521 * Returns true if set up for a subset decode succeeds, false otherwise
522 * If the set-up succeeds, the width and height parameters will be set
523 */
msarett0459c942015-11-10 14:52:13 -0800524static bool valid_subset_bench(const SkString& path, SkColorType colorType,
msarettb23e6aa2015-06-09 13:56:10 -0700525 int* width, int* height) {
526 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
527 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded));
528
msarett0459c942015-11-10 14:52:13 -0800529 // Check that we can create a codec.
530 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
531 if (nullptr == codec) {
532 SkDebugf("Could not create codec for %s. Skipping bench.\n", path.c_str());
533 return false;
msarettb23e6aa2015-06-09 13:56:10 -0700534 }
msarettab80e352015-06-17 10:28:22 -0700535
msarett0459c942015-11-10 14:52:13 -0800536 // These will be initialized by SkCodec if the color type is kIndex8 and
537 // unused otherwise.
538 SkPMColor colors[256];
539 int colorCount;
540 const SkImageInfo info = codec->getInfo().makeColorType(colorType);
541 if (codec->startScanlineDecode(info, nullptr, colors, &colorCount) != SkCodec::kSuccess)
542 {
543 SkDebugf("Could not create scanline decoder for %s with color type %s. "
544 "Skipping bench.\n", path.c_str(), color_type_to_str(colorType));
545 return false;
546 }
547 *width = info.width();
548 *height = info.height();
549
msarettab80e352015-06-17 10:28:22 -0700550 // Check if the image is large enough for a meaningful subset benchmark.
551 if (*width <= 512 && *height <= 512) {
552 // This should not print a message since it is not an error.
553 return false;
554 }
555
msarettb23e6aa2015-06-09 13:56:10 -0700556 return true;
557}
jcgregoriobf5e5232014-07-17 13:14:16 -0700558
msarett5cb48852015-11-06 08:56:32 -0800559static bool valid_brd_bench(SkData* encoded, SkBitmapRegionDecoder::Strategy strategy,
msarett7f691442015-09-22 11:56:16 -0700560 SkColorType colorType, uint32_t sampleSize, uint32_t minOutputSize, int* width,
561 int* height) {
msarett5cb48852015-11-06 08:56:32 -0800562 SkAutoTDelete<SkBitmapRegionDecoder> brd(
563 SkBitmapRegionDecoder::Create(encoded, strategy));
msarett7f691442015-09-22 11:56:16 -0700564 if (nullptr == brd.get()) {
565 // This is indicates that subset decoding is not supported for a particular image format.
566 return false;
567 }
568
msarett35e5d1b2015-10-27 12:50:25 -0700569 SkBitmap bitmap;
570 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, brd->width(), brd->height()),
571 1, colorType, false)) {
572 return false;
573 }
msarett7f691442015-09-22 11:56:16 -0700574
575 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
576 (uint32_t) brd->height()) {
577 // This indicates that the image is not large enough to decode a
578 // minOutputSize x minOutputSize subset at the given sampleSize.
579 return false;
580 }
581
582 // Set the image width and height. The calling code will use this to choose subsets to decode.
583 *width = brd->width();
584 *height = brd->height();
585 return true;
586}
587
egdaniel3bf92062015-06-26 08:12:46 -0700588static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700589 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700590#if SK_SUPPORT_GPU
591 if (FLAGS_abandonGpuContext) {
592 gGrFactory->abandonContexts();
593 }
594 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
595 gGrFactory->destroyContexts();
596 }
597#endif
598}
599
mtkleine714e752014-07-31 12:13:48 -0700600class BenchmarkStream {
601public:
mtklein92007582014-08-01 07:46:52 -0700602 BenchmarkStream() : fBenches(BenchRegistry::Head())
603 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700604 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700605 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800606 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800607 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700608 , fCurrentCodec(0)
msarett95f192d2015-02-13 09:05:41 -0800609 , fCurrentImage(0)
610 , fCurrentSubsetImage(0)
msarett7f691442015-09-22 11:56:16 -0700611 , fCurrentBRDImage(0)
msarett95f192d2015-02-13 09:05:41 -0800612 , fCurrentColorType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700613 , fCurrentSubsetType(0)
msarett7f691442015-09-22 11:56:16 -0700614 , fCurrentBRDStrategy(0)
615 , fCurrentBRDSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700616 , fCurrentAnimSKP(0) {
mtklein92007582014-08-01 07:46:52 -0700617 for (int i = 0; i < FLAGS_skps.count(); i++) {
618 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
619 fSKPs.push_back() = FLAGS_skps[i];
620 } else {
621 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
622 SkString path;
623 while (it.next(&path)) {
624 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
625 }
626 }
627 }
mtkleine714e752014-07-31 12:13:48 -0700628
mtklein92007582014-08-01 07:46:52 -0700629 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
630 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
631 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
632 exit(1);
633 }
634
635 for (int i = 0; i < FLAGS_scales.count(); i++) {
636 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
637 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
638 exit(1);
639 }
640 }
robertphillips5b693772014-11-21 06:19:36 -0800641
cdalton63a82852015-06-29 14:06:10 -0700642 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
643 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700644 exit(1);
645 }
646
robertphillips5b693772014-11-21 06:19:36 -0800647 if (FLAGS_mpd) {
648 fUseMPDs.push_back() = true;
649 }
mtkleinc751ecb2015-06-15 08:56:38 -0700650 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700651
msarett95f192d2015-02-13 09:05:41 -0800652 // Prepare the images for decoding
653 for (int i = 0; i < FLAGS_images.count(); i++) {
654 const char* flag = FLAGS_images[i];
655 if (sk_isdir(flag)) {
656 // If the value passed in is a directory, add all the images
657 SkOSFile::Iter it(flag);
658 SkString file;
659 while (it.next(&file)) {
660 fImages.push_back() = SkOSPath::Join(flag, file.c_str());
661 }
662 } else if (sk_exists(flag)) {
663 // Also add the value if it is a single image
664 fImages.push_back() = flag;
665 }
666 }
mtklein95553d92015-03-12 08:24:21 -0700667
msarett95f192d2015-02-13 09:05:41 -0800668 // Choose the candidate color types for image decoding
669 const SkColorType colorTypes[] =
msarettb23e6aa2015-06-09 13:56:10 -0700670 { kN32_SkColorType,
671 kRGB_565_SkColorType,
672 kAlpha_8_SkColorType,
673 kIndex_8_SkColorType,
674 kGray_8_SkColorType };
msarett74deb982015-10-20 16:45:56 -0700675 fColorTypes.reset(colorTypes, SK_ARRAY_COUNT(colorTypes));
mtklein92007582014-08-01 07:46:52 -0700676 }
677
mtkleinfd731ce2014-09-10 12:19:30 -0700678 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
679 // Not strictly necessary, as it will be checked again later,
680 // but helps to avoid a lot of pointless work if we're going to skip it.
681 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
682 return false;
683 }
684
scroggoa1193e42015-01-21 12:09:53 -0800685 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -0700686 if (stream.get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700687 SkDebugf("Could not read %s.\n", path);
688 return false;
689 }
690
mtklein57f27bd2015-02-09 11:58:41 -0800691 pic->reset(SkPicture::CreateFromStream(stream.get()));
halcanary96fcdcc2015-08-27 07:41:13 -0700692 if (pic->get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700693 SkDebugf("Could not read %s as an SkPicture.\n", path);
694 return false;
695 }
696 return true;
697 }
698
mtklein92007582014-08-01 07:46:52 -0700699 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700700 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700701 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700702 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700703 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700704 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700705 return bench;
706 }
mtklein92007582014-08-01 07:46:52 -0700707
mtkleine714e752014-07-31 12:13:48 -0700708 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -0700709 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700710 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800711 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700712 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700713 fBenchType = "micro";
halcanary385fe4d2015-08-26 13:07:48 -0700714 return new GMBench(gm.detach());
mtkleine714e752014-07-31 12:13:48 -0700715 }
716 }
mtklein92007582014-08-01 07:46:52 -0700717
mtkleinfd731ce2014-09-10 12:19:30 -0700718 // First add all .skps as RecordingBenches.
719 while (fCurrentRecording < fSKPs.count()) {
720 const SkString& path = fSKPs[fCurrentRecording++];
721 SkAutoTUnref<SkPicture> pic;
722 if (!ReadPicture(path.c_str(), &pic)) {
723 continue;
724 }
725 SkString name = SkOSPath::Basename(path.c_str());
726 fSourceType = "skp";
727 fBenchType = "recording";
bsalomon0aa5cea2014-12-15 09:13:35 -0800728 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic));
mtklein051e56d2014-12-04 08:46:51 -0800729 fSKPOps = pic->approximateOpCount();
halcanary385fe4d2015-08-26 13:07:48 -0700730 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700731 }
732
733 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700734 while (fCurrentScale < fScales.count()) {
735 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800736 const SkString& path = fSKPs[fCurrentSKP];
mtkleinfd731ce2014-09-10 12:19:30 -0700737 SkAutoTUnref<SkPicture> pic;
738 if (!ReadPicture(path.c_str(), &pic)) {
robertphillips5b693772014-11-21 06:19:36 -0800739 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700740 continue;
741 }
robertphillips5b693772014-11-21 06:19:36 -0800742
743 while (fCurrentUseMPD < fUseMPDs.count()) {
744 if (FLAGS_bbh) {
745 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
746 SkRTreeFactory factory;
747 SkPictureRecorder recorder;
748 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
749 pic->playback(recorder.beginRecording(pic->cullRect().width(),
750 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800751 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800752 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
robertphillips5b693772014-11-21 06:19:36 -0800753 pic.reset(recorder.endRecording());
754 }
755 SkString name = SkOSPath::Basename(path.c_str());
756 fSourceType = "skp";
757 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700758 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
759 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700760 }
robertphillips5b693772014-11-21 06:19:36 -0800761 fCurrentUseMPD = 0;
762 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700763 }
764 fCurrentSKP = 0;
765 fCurrentScale++;
766 }
767
joshualitt261c3ad2015-04-27 09:16:57 -0700768 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700769 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700770 while (fCurrentAnimSKP < fSKPs.count()) {
771 const SkString& path = fSKPs[fCurrentAnimSKP];
772 SkAutoTUnref<SkPicture> pic;
773 if (!ReadPicture(path.c_str(), &pic)) {
774 fCurrentAnimSKP++;
775 continue;
776 }
777
778 fCurrentAnimSKP++;
779 SkString name = SkOSPath::Basename(path.c_str());
cdalton63a82852015-06-29 14:06:10 -0700780 SkAutoTUnref<SKPAnimationBench::Animation> animation(
781 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
halcanary385fe4d2015-08-26 13:07:48 -0700782 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation,
783 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700784 }
785 }
786
scroggo60869a42015-04-01 12:09:17 -0700787 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700788 fSourceType = "image";
789 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700790 const SkString& path = fImages[fCurrentCodec];
791 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
792 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700793 if (!codec) {
794 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700795 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700796 continue;
797 }
scroggo21027992015-04-02 13:22:38 -0700798
scroggo60869a42015-04-01 12:09:17 -0700799 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700800 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo60869a42015-04-01 12:09:17 -0700801 fCurrentColorType++;
scroggo21027992015-04-02 13:22:38 -0700802
scroggo60869a42015-04-01 12:09:17 -0700803 // Make sure we can decode to this color type.
scroggo60869a42015-04-01 12:09:17 -0700804 SkImageInfo info = codec->getInfo().makeColorType(colorType);
scroggo21027992015-04-02 13:22:38 -0700805 SkAlphaType alphaType;
806 if (!SkColorTypeValidateAlphaType(colorType, info.alphaType(),
807 &alphaType)) {
808 continue;
809 }
810 if (alphaType != info.alphaType()) {
811 info = info.makeAlphaType(alphaType);
812 }
813
814 const size_t rowBytes = info.minRowBytes();
815 SkAutoMalloc storage(info.getSafeSize(rowBytes));
816
817 // Used if fCurrentColorType is kIndex_8_SkColorType
818 int colorCount = 256;
819 SkPMColor colors[256];
820
scroggoeb602a52015-07-09 08:16:03 -0700821 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700822 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700823 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700824 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700825 case SkCodec::kSuccess:
826 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700827 return new CodecBench(SkOSPath::Basename(path.c_str()),
828 encoded, colorType);
scroggoeb602a52015-07-09 08:16:03 -0700829 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700830 // This is okay. Not all conversions are valid.
831 break;
scroggo60869a42015-04-01 12:09:17 -0700832 default:
833 // This represents some sort of failure.
834 SkASSERT(false);
835 break;
836 }
837 }
838 fCurrentColorType = 0;
839 }
840
msarett95f192d2015-02-13 09:05:41 -0800841 // Run the DecodingBenches
842 while (fCurrentImage < fImages.count()) {
scroggo303fa352015-10-05 11:03:34 -0700843 fSourceType = "image";
844 fBenchType = "skimagedecoder";
msarett95f192d2015-02-13 09:05:41 -0800845 while (fCurrentColorType < fColorTypes.count()) {
846 const SkString& path = fImages[fCurrentImage];
847 SkColorType colorType = fColorTypes[fCurrentColorType];
848 fCurrentColorType++;
scroggo60869a42015-04-01 12:09:17 -0700849 // Check if the image decodes to the right color type
850 // before creating the benchmark
msarett95f192d2015-02-13 09:05:41 -0800851 SkBitmap bitmap;
852 if (SkImageDecoder::DecodeFile(path.c_str(), &bitmap,
scroggo60869a42015-04-01 12:09:17 -0700853 colorType, SkImageDecoder::kDecodePixels_Mode)
854 && bitmap.colorType() == colorType) {
msarett95f192d2015-02-13 09:05:41 -0800855 return new DecodingBench(path, colorType);
856 }
857 }
858 fCurrentColorType = 0;
859 fCurrentImage++;
860 }
861
msarettb23e6aa2015-06-09 13:56:10 -0700862 // Run the SubsetBenches
msarett0459c942015-11-10 14:52:13 -0800863 while (fCurrentSubsetImage < fImages.count()) {
scroggo303fa352015-10-05 11:03:34 -0700864 fSourceType = "image";
msarett0459c942015-11-10 14:52:13 -0800865 fBenchType = "skcodec";
866 const SkString& path = fImages[fCurrentSubsetImage];
867 if (!run_subset_bench(path)) {
msarettb23e6aa2015-06-09 13:56:10 -0700868 fCurrentSubsetImage++;
msarett0459c942015-11-10 14:52:13 -0800869 continue;
msarett95f192d2015-02-13 09:05:41 -0800870 }
msarett0459c942015-11-10 14:52:13 -0800871 while (fCurrentColorType < fColorTypes.count()) {
872 SkColorType colorType = fColorTypes[fCurrentColorType];
873 while (fCurrentSubsetType <= kLast_SubsetType) {
874 int width = 0;
875 int height = 0;
876 int currentSubsetType = fCurrentSubsetType++;
877 if (valid_subset_bench(path, colorType, &width, &height)) {
878 switch (currentSubsetType) {
879 case kTopLeft_SubsetType:
880 return new SubsetSingleBench(path, colorType, width/3,
881 height/3, 0, 0);
882 case kTopRight_SubsetType:
883 return new SubsetSingleBench(path, colorType, width/3,
884 height/3, 2*width/3, 0);
885 case kMiddle_SubsetType:
886 return new SubsetSingleBench(path, colorType, width/3,
887 height/3, width/3, height/3);
888 case kBottomLeft_SubsetType:
889 return new SubsetSingleBench(path, colorType, width/3,
890 height/3, 0, 2*height/3);
891 case kBottomRight_SubsetType:
892 return new SubsetSingleBench(path, colorType, width/3,
893 height/3, 2*width/3, 2*height/3);
894 case kTranslate_SubsetType:
895 return new SubsetTranslateBench(path, colorType, 512, 512);
896 case kZoom_SubsetType:
897 return new SubsetZoomBench(path, colorType, 512, 512);
898 }
899 } else {
900 break;
901 }
902 }
903 fCurrentSubsetType = 0;
904 fCurrentColorType++;
905 }
906 fCurrentColorType = 0;
907 fCurrentSubsetImage++;
msarett95f192d2015-02-13 09:05:41 -0800908 }
909
msarett7f691442015-09-22 11:56:16 -0700910 // Run the BRDBenches
911 // We will benchmark multiple BRD strategies.
scroggo303fa352015-10-05 11:03:34 -0700912 static const struct {
msarett5cb48852015-11-06 08:56:32 -0800913 SkBitmapRegionDecoder::Strategy fStrategy;
msarett0459c942015-11-10 14:52:13 -0800914 const char* fName;
scroggo303fa352015-10-05 11:03:34 -0700915 } strategies[] = {
msarett0459c942015-11-10 14:52:13 -0800916 { SkBitmapRegionDecoder::kCanvas_Strategy, "BRD_canvas" },
msarett5cb48852015-11-06 08:56:32 -0800917 { SkBitmapRegionDecoder::kAndroidCodec_Strategy, "BRD_android_codec" },
msarett7f691442015-09-22 11:56:16 -0700918 };
919
920 // We intend to create benchmarks that model the use cases in
921 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
922 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
923 // the image. For that reason, we will benchmark decodes in five representative locations
924 // in the image. Additionally, this use case utilizes power of two scaling, so we will
925 // test on power of two sample sizes. The output tile is always 512x512, so, when a
926 // sampleSize is used, the size of the subset that is decoded is always
927 // (sampleSize*512)x(sampleSize*512).
928 // There are a few good reasons to only test on power of two sample sizes at this time:
929 // JPEG decodes using kOriginal_Strategy are broken for non-powers of two.
halcanary6950de62015-11-07 05:29:00 -0800930 // https://bug.skia.org/4319
msarett7f691442015-09-22 11:56:16 -0700931 // All use cases we are aware of only scale by powers of two.
932 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
933 // these tests are sufficient to provide good coverage of our scaling options.
scroggo501b7342015-11-03 07:55:11 -0800934 const uint32_t sampleSizes[] = { 1, 2, 4, 8, 16, 32, 64 };
msarett7f691442015-09-22 11:56:16 -0700935 const uint32_t minOutputSize = 512;
936 while (fCurrentBRDImage < fImages.count()) {
937 while (fCurrentBRDStrategy < (int) SK_ARRAY_COUNT(strategies)) {
scroggo303fa352015-10-05 11:03:34 -0700938 fSourceType = "image";
939 fBenchType = strategies[fCurrentBRDStrategy].fName;
scroggo860e8a62015-10-15 07:51:28 -0700940
941 const SkString& path = fImages[fCurrentBRDImage];
msarett5cb48852015-11-06 08:56:32 -0800942 const SkBitmapRegionDecoder::Strategy strategy =
scroggo860e8a62015-10-15 07:51:28 -0700943 strategies[fCurrentBRDStrategy].fStrategy;
944
msarett7f691442015-09-22 11:56:16 -0700945 while (fCurrentColorType < fColorTypes.count()) {
946 while (fCurrentBRDSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
947 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700948
949
msarett7f691442015-09-22 11:56:16 -0700950 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
951 const SkColorType colorType = fColorTypes[fCurrentColorType];
952 uint32_t sampleSize = sampleSizes[fCurrentBRDSampleSize];
953 int currentSubsetType = fCurrentSubsetType++;
954
955 int width = 0;
956 int height = 0;
957 if (!valid_brd_bench(encoded.get(), strategy, colorType, sampleSize,
958 minOutputSize, &width, &height)) {
959 break;
960 }
961
962 SkString basename = SkOSPath::Basename(path.c_str());
963 SkIRect subset;
964 const uint32_t subsetSize = sampleSize * minOutputSize;
965 switch (currentSubsetType) {
966 case kTopLeft_SubsetType:
967 basename.append("_TopLeft");
968 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
969 break;
970 case kTopRight_SubsetType:
971 basename.append("_TopRight");
972 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
973 subsetSize);
974 break;
975 case kMiddle_SubsetType:
976 basename.append("_Middle");
977 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
978 (height - subsetSize) / 2, subsetSize, subsetSize);
979 break;
980 case kBottomLeft_SubsetType:
981 basename.append("_BottomLeft");
982 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
983 subsetSize);
984 break;
985 case kBottomRight_SubsetType:
986 basename.append("_BottomRight");
987 subset = SkIRect::MakeXYWH(width - subsetSize,
988 height - subsetSize, subsetSize, subsetSize);
989 break;
990 default:
991 SkASSERT(false);
992 }
993
994 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
995 strategy, colorType, sampleSize, subset);
996 }
997 fCurrentSubsetType = 0;
998 fCurrentBRDSampleSize++;
999 }
1000 fCurrentBRDSampleSize = 0;
1001 fCurrentColorType++;
1002 }
1003 fCurrentColorType = 0;
1004 fCurrentBRDStrategy++;
1005 }
1006 fCurrentBRDStrategy = 0;
1007 fCurrentBRDImage++;
1008 }
1009
halcanary96fcdcc2015-08-27 07:41:13 -07001010 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001011 }
mtklein92007582014-08-01 07:46:52 -07001012
1013 void fillCurrentOptions(ResultsWriter* log) const {
1014 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001015 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001016 if (0 == strcmp(fSourceType, "skp")) {
1017 log->configOption("clip",
1018 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1019 fClip.fRight, fClip.fBottom).c_str());
mtklein4dfdbb12015-10-20 07:45:29 -07001020 SK_ALWAYSBREAK(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -07001021 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001022 if (fCurrentUseMPD > 0) {
1023 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1024 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1025 }
mtklein92007582014-08-01 07:46:52 -07001026 }
mtklein051e56d2014-12-04 08:46:51 -08001027 if (0 == strcmp(fBenchType, "recording")) {
1028 log->metric("bytes", fSKPBytes);
1029 log->metric("ops", fSKPOps);
1030 }
mtklein92007582014-08-01 07:46:52 -07001031 }
1032
mtkleine714e752014-07-31 12:13:48 -07001033private:
msarettb23e6aa2015-06-09 13:56:10 -07001034 enum SubsetType {
1035 kTopLeft_SubsetType = 0,
1036 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001037 kMiddle_SubsetType = 2,
1038 kBottomLeft_SubsetType = 3,
1039 kBottomRight_SubsetType = 4,
1040 kTranslate_SubsetType = 5,
1041 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001042 kLast_SubsetType = kZoom_SubsetType,
1043 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001044 };
1045
mtkleine714e752014-07-31 12:13:48 -07001046 const BenchRegistry* fBenches;
1047 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001048 SkIRect fClip;
1049 SkTArray<SkScalar> fScales;
1050 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -08001051 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001052 SkTArray<SkString> fImages;
msarett74deb982015-10-20 16:45:56 -07001053 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001054 SkScalar fZoomMax;
1055 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001056
mtklein051e56d2014-12-04 08:46:51 -08001057 double fSKPBytes, fSKPOps;
1058
mtkleinfd731ce2014-09-10 12:19:30 -07001059 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1060 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1061 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -07001062 int fCurrentScale;
1063 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -08001064 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001065 int fCurrentCodec;
msarett95f192d2015-02-13 09:05:41 -08001066 int fCurrentImage;
1067 int fCurrentSubsetImage;
msarett7f691442015-09-22 11:56:16 -07001068 int fCurrentBRDImage;
msarett95f192d2015-02-13 09:05:41 -08001069 int fCurrentColorType;
msarettb23e6aa2015-06-09 13:56:10 -07001070 int fCurrentSubsetType;
msarett7f691442015-09-22 11:56:16 -07001071 int fCurrentBRDStrategy;
1072 int fCurrentBRDSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001073 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001074};
1075
jcgregorio3b27ade2014-11-13 08:06:40 -08001076int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001077int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001078 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001079 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001080 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001081
krajcevski69a55602014-08-13 10:46:31 -07001082#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001083 GrContextOptions grContextOpts;
krajcevski12b35442014-08-13 12:06:26 -07001084 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
halcanary385fe4d2015-08-26 13:07:48 -07001085 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001086#endif
1087
bsalomon06cddec2014-10-24 10:40:50 -07001088 if (FLAGS_veryVerbose) {
1089 FLAGS_verbose = true;
1090 }
1091
bsalomon6eb03cc2014-08-07 14:28:50 -07001092 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001093 FLAGS_samples = 1;
1094 FLAGS_gpuFrameLag = 0;
1095 }
1096
bsalomon6eb03cc2014-08-07 14:28:50 -07001097 if (!FLAGS_writePath.isEmpty()) {
1098 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1099 if (!sk_mkdir(FLAGS_writePath[0])) {
1100 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001101 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001102 }
1103 }
1104
halcanary385fe4d2015-08-26 13:07:48 -07001105 SkAutoTDelete<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001106 if (!FLAGS_outResultsFile.isEmpty()) {
halcanary385fe4d2015-08-26 13:07:48 -07001107 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein60317d0f2014-07-14 11:30:37 -07001108 }
mtklein1915b622014-08-20 11:45:00 -07001109
1110 if (1 == FLAGS_properties.count() % 2) {
1111 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1112 return 1;
1113 }
1114 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1115 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1116 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001117
1118 if (1 == FLAGS_key.count() % 2) {
1119 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1120 return 1;
1121 }
1122 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001123 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001124 }
mtklein60317d0f2014-07-14 11:30:37 -07001125
mtkleinf3723212014-06-25 14:08:00 -07001126 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001127 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001128
cdaltone1b89582015-06-25 19:17:08 -07001129 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001130
bsalomon6eb03cc2014-08-07 14:28:50 -07001131 if (kAutoTuneLoops != FLAGS_loops) {
1132 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001133 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -07001134 SkDebugf("median\tbench\tconfig\n");
mtkleinbbba1682015-10-28 11:36:30 -07001135 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001136 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001137 } else {
mtkleind75c4662015-04-30 07:11:22 -07001138 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001139 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001140 }
1141
bsalomonc2553372014-07-22 13:09:05 -07001142 SkTDArray<Config> configs;
1143 create_configs(&configs);
1144
mtkleine070c2b2014-10-14 08:40:43 -07001145 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001146 BenchmarkStream benchStream;
1147 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001148 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001149 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001150 continue;
1151 }
1152
egdaniel3bf92062015-06-26 08:12:46 -07001153 if (!configs.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -07001154 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001155 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001156 }
egdaniel3bf92062015-06-26 08:12:46 -07001157 for (int i = 0; i < configs.count(); ++i) {
1158 Target* target = is_enabled(b, configs[i]);
1159 if (!target) {
1160 continue;
1161 }
mtkleinf3723212014-06-25 14:08:00 -07001162
halcanary96fcdcc2015-08-27 07:41:13 -07001163 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001164 SkCanvas* canvas = target->getCanvas();
1165 const char* config = target->config.name;
1166
1167 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001168 bench->perCanvasPreDraw(canvas);
1169
cdaltone1b89582015-06-25 19:17:08 -07001170 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001171 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001172 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1173 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001174
mtkleinbbba1682015-10-28 11:36:30 -07001175 if (FLAGS_ms) {
1176 samples.reset();
1177 auto stop = now_ms() + FLAGS_ms;
1178 do {
1179 samples.push_back(time(loops, bench, target) / loops);
1180 } while (now_ms() < stop);
1181 } else {
cdaltone1b89582015-06-25 19:17:08 -07001182 samples.reset(FLAGS_samples);
1183 for (int s = 0; s < FLAGS_samples; s++) {
egdaniel3bf92062015-06-26 08:12:46 -07001184 samples[s] = time(loops, bench, target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001185 }
cdaltone1b89582015-06-25 19:17:08 -07001186 }
mtkleinf3723212014-06-25 14:08:00 -07001187
joshualitte45c81c2015-12-02 09:05:37 -08001188#if SK_SUPPORT_GPU
1189 SkTArray<SkString> keys;
1190 SkTArray<double> values;
1191 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1192 if (gpuStatsDump) {
1193 // TODO cache stats
1194 bench->getGpuStats(canvas, &keys, &values);
1195 }
1196#endif
1197
robertphillips5b693772014-11-21 06:19:36 -08001198 bench->perCanvasPostDraw(canvas);
1199
egdaniel3bf92062015-06-26 08:12:46 -07001200 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001201 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001202 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001203 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001204 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001205 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001206 }
1207
1208 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001209 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001210 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001211 continue;
1212 }
1213
cdaltone1b89582015-06-25 19:17:08 -07001214 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001215 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001216 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001217 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001218 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001219 log->metric("min_ms", stats.min);
joshualitte45c81c2015-12-02 09:05:37 -08001220#if SK_SUPPORT_GPU
1221 if (gpuStatsDump) {
1222 // dump to json, only SKPBench currently returns valid keys / values
1223 SkASSERT(keys.count() == values.count());
1224 for (int i = 0; i < keys.count(); i++) {
1225 log->metric(keys[i].c_str(), values[i]);
1226 }
1227 }
1228#endif
1229
mtkleine070c2b2014-10-14 08:40:43 -07001230 if (runs++ % FLAGS_flushEvery == 0) {
1231 log->flush();
1232 }
mtklein60317d0f2014-07-14 11:30:37 -07001233
bsalomon6eb03cc2014-08-07 14:28:50 -07001234 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001235 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001236 config = ""; // Only print the config if we run the same bench on more than one.
1237 }
mtkleind75c4662015-04-30 07:11:22 -07001238 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1239 , sk_tools::getCurrResidentSetSizeMB()
1240 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001241 , bench->getUniqueName()
1242 , config);
mtkleinf3723212014-06-25 14:08:00 -07001243 } else if (FLAGS_quiet) {
egdaniel3bf92062015-06-26 08:12:46 -07001244 if (configs.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -07001245 config = ""; // Only print the config if we run the same bench on more than one.
1246 }
mtklein96289052014-09-10 12:05:59 -07001247 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001248 } else {
1249 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001250 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1251 , sk_tools::getCurrResidentSetSizeMB()
1252 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001253 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001254 , HUMANIZE(stats.min)
1255 , HUMANIZE(stats.median)
1256 , HUMANIZE(stats.mean)
1257 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001258 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001259 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001260 , config
mtklein96289052014-09-10 12:05:59 -07001261 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001262 );
1263 }
joshualitte45c81c2015-12-02 09:05:37 -08001264
bsalomonb12ea412015-02-02 21:19:50 -08001265#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001266 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
bsalomona0a024e2015-12-01 07:58:44 -08001267 gGrFactory->get(configs[i].ctxType)->printCacheStats();
1268 gGrFactory->get(configs[i].ctxType)->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001269 }
1270#endif
joshualitte45c81c2015-12-02 09:05:37 -08001271
cdalton2c56ba52015-06-26 13:32:53 -07001272 if (FLAGS_verbose) {
1273 SkDebugf("Samples: ");
1274 for (int i = 0; i < samples.count(); i++) {
1275 SkDebugf("%s ", HUMANIZE(samples[i]));
1276 }
1277 SkDebugf("%s\n", bench->getUniqueName());
1278 }
egdaniel3bf92062015-06-26 08:12:46 -07001279 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001280 }
mtkleinf3723212014-06-25 14:08:00 -07001281 }
1282
mtkleine1091452014-12-04 10:47:02 -08001283 log->bench("memory_usage", 0,0);
1284 log->config("meta");
1285 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1286
joshualitte0b19d42015-03-26 10:41:02 -07001287#if SK_SUPPORT_GPU
1288 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1289 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001290 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001291#endif
1292
mtkleinf3723212014-06-25 14:08:00 -07001293 return 0;
1294}
1295
jcgregorio3b27ade2014-11-13 08:06:40 -08001296#if !defined SK_BUILD_FOR_IOS
1297int main(int argc, char** argv) {
1298 SkCommandLineFlags::Parse(argc, argv);
1299 return nanobench_main();
1300}
1301#endif