blob: a1db0acd80b4e6616b87ae8261cb51a570618e0e [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"
28#include "Timer.h"
29
msarett7f691442015-09-22 11:56:16 -070030#include "SkBitmapRegionDecoderInterface.h"
mtklein6838d852014-10-29 14:15:10 -070031#include "SkBBoxHierarchy.h"
mtkleinf3723212014-06-25 14:08:00 -070032#include "SkCanvas.h"
scroggo60869a42015-04-01 12:09:17 -070033#include "SkCodec.h"
caryclark17f0b6d2014-07-22 10:15:34 -070034#include "SkCommonFlags.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"
scroggo9b2cdbf42015-07-10 12:07:02 -070041#include "SkScanlineDecoder.h"
mtkleinf3723212014-06-25 14:08:00 -070042#include "SkString.h"
43#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080044#include "SkTaskGroup.h"
mtkleinf3723212014-06-25 14:08:00 -070045
bungeman60e0fee2015-08-26 05:15:46 -070046#include <stdlib.h>
47
tomhudsond968a6f2015-03-26 11:28:06 -070048#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
49 #include "nanobenchAndroid.h"
50#endif
51
mtkleinbb6a0282014-07-01 08:43:42 -070052#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070053 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070054 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070055 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070056 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070057#endif
58
bsalomon682c2692015-05-22 14:01:46 -070059 struct GrContextOptions;
60
mtkleinf3723212014-06-25 14:08:00 -070061__SK_FORCE_IMAGE_DECODER_LINKING;
62
cdaltone1b89582015-06-25 19:17:08 -070063static const int kTimedSampling = 0;
64
reed53249782014-10-10 09:09:52 -070065static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070066
mtkleinb5110422014-08-07 15:20:02 -070067static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070068#ifdef SK_DEBUG
69 1;
mtkleina189ccd2014-07-14 12:28:47 -070070#else
bsalomon6eb03cc2014-08-07 14:28:50 -070071 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070072#endif
73
bsalomon6eb03cc2014-08-07 14:28:50 -070074static SkString loops_help_txt() {
75 SkString help;
76 help.printf("Number of times to run each bench. Set this to %d to auto-"
77 "tune for each bench. Timings are only reported when auto-tuning.",
78 kAutoTuneLoops);
79 return help;
80}
81
cdaltone1b89582015-06-25 19:17:08 -070082static SkString to_string(int n) {
83 SkString str;
84 str.appendS32(n);
85 return str;
86}
87
bsalomon6eb03cc2014-08-07 14:28:50 -070088DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
89
mtkleinf3723212014-06-25 14:08:00 -070090DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
cdaltone1b89582015-06-25 19:17:08 -070091DEFINE_string(samplingTime, "0", "Amount of time to run each bench. Takes precedence over samples."
92 "Must be \"0\", \"%%lfs\", or \"%%lfms\"");
mtkleinf3723212014-06-25 14:08:00 -070093DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
94DEFINE_double(overheadGoal, 0.0001,
95 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070096DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -070097DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070098DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
99 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -0700100
mtklein60317d0f2014-07-14 11:30:37 -0700101DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -0700102DEFINE_int32(maxCalibrationAttempts, 3,
103 "Try up to this many times to guess loops for a bench, or skip the bench.");
104DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700105DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
106DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700107DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
108 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700109DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
robertphillips5b693772014-11-21 06:19:36 -0800110DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700111DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700112DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800113DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800114DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
mtklein92007582014-08-01 07:46:52 -0700115
mtkleinf3723212014-06-25 14:08:00 -0700116static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700117 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800118 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700119}
mtklein55b0ffc2014-07-17 08:38:23 -0700120#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700121
tomhudsond968a6f2015-03-26 11:28:06 -0700122bool Target::init(SkImageInfo info, Benchmark* bench) {
123 if (Benchmark::kRaster_Backend == config.backend) {
124 this->surface.reset(SkSurface::NewRaster(info));
125 if (!this->surface.get()) {
126 return false;
127 }
128 }
129 return true;
130}
131bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700132 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700133 if (!canvas) {
134 return false;
135 }
136 bmp->setInfo(canvas->imageInfo());
137 if (!canvas->readPixels(bmp, 0, 0)) {
138 SkDebugf("Can't read canvas pixels.\n");
139 return false;
140 }
141 return true;
142}
143
144#if SK_SUPPORT_GPU
145struct GPUTarget : public Target {
halcanary96fcdcc2015-08-27 07:41:13 -0700146 explicit GPUTarget(const Config& c) : Target(c), gl(nullptr) { }
tomhudsond968a6f2015-03-26 11:28:06 -0700147 SkGLContext* gl;
148
149 void setup() override {
150 this->gl->makeCurrent();
151 // Make sure we're done with whatever came before.
152 SK_GL(*this->gl, Finish());
153 }
154 void endTiming() override {
155 if (this->gl) {
156 SK_GL(*this->gl, Flush());
157 this->gl->swapBuffers();
158 }
159 }
160 void fence() override {
161 SK_GL(*this->gl, Finish());
162 }
mtkleind75c4662015-04-30 07:11:22 -0700163
cdaltond416a5b2015-06-23 13:23:44 -0700164 bool needsFrameTiming(int* maxFrameLag) const override {
165 if (!this->gl->getMaxGpuFrameLag(maxFrameLag)) {
166 // Frame lag is unknown.
167 *maxFrameLag = FLAGS_gpuFrameLag;
168 }
169 return true;
170 }
tomhudsond968a6f2015-03-26 11:28:06 -0700171 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700172 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
173 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700174 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
175 this->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(this->config.ctxType),
176 SkSurface::kNo_Budgeted, info,
177 this->config.samples, &props));
178 this->gl = gGrFactory->getGLContext(this->config.ctxType);
179 if (!this->surface.get()) {
180 return false;
181 }
cdaltond416a5b2015-06-23 13:23:44 -0700182 if (!this->gl->fenceSyncSupport()) {
183 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
184 "Timings might not be accurate.\n", this->config.name);
185 }
tomhudsond968a6f2015-03-26 11:28:06 -0700186 return true;
187 }
188 void fillOptions(ResultsWriter* log) override {
189 const GrGLubyte* version;
190 SK_GL_RET(*this->gl, version, GetString(GR_GL_VERSION));
191 log->configOption("GL_VERSION", (const char*)(version));
192
193 SK_GL_RET(*this->gl, version, GetString(GR_GL_RENDERER));
194 log->configOption("GL_RENDERER", (const char*) version);
195
196 SK_GL_RET(*this->gl, version, GetString(GR_GL_VENDOR));
197 log->configOption("GL_VENDOR", (const char*) version);
198
199 SK_GL_RET(*this->gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
200 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
201 }
202};
mtkleind75c4662015-04-30 07:11:22 -0700203
tomhudsond968a6f2015-03-26 11:28:06 -0700204#endif
205
tomhudson75a0ebb2015-03-27 12:11:44 -0700206static double time(int loops, Benchmark* bench, Target* target) {
207 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700208 if (canvas) {
209 canvas->clear(SK_ColorWHITE);
210 }
mtkleinbb6a0282014-07-01 08:43:42 -0700211 WallTimer timer;
212 timer.start();
tomhudson75a0ebb2015-03-27 12:11:44 -0700213 canvas = target->beginTiming(canvas);
214 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700215 if (canvas) {
216 canvas->flush();
217 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700218 target->endTiming();
mtkleinbb6a0282014-07-01 08:43:42 -0700219 timer.end();
220 return timer.fWall;
221}
222
mtkleinf3723212014-06-25 14:08:00 -0700223static double estimate_timer_overhead() {
224 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700225 for (int i = 0; i < FLAGS_overheadLoops; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700226 WallTimer timer;
227 timer.start();
228 timer.end();
229 overhead += timer.fWall;
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,
391 int sampleCnt) {
392 if (!is_cpu_config_allowed(name)) {
393 return false;
394 }
krajcevski69a55602014-08-13 10:46:31 -0700395 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomon76228632015-05-29 08:02:10 -0700396 return sampleCnt <= ctx->caps()->maxSampleCount();
bsalomonc2553372014-07-22 13:09:05 -0700397 }
398 return false;
399}
400#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700401
bsalomonc2553372014-07-22 13:09:05 -0700402#if SK_SUPPORT_GPU
403#define kBogusGLContextType GrContextFactory::kNative_GLContextType
404#else
405#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700406#endif
bsalomonc2553372014-07-22 13:09:05 -0700407
408// Append all configs that are enabled and supported.
409static void create_configs(SkTDArray<Config>* configs) {
jvanverth4736e142014-11-07 07:12:46 -0800410 #define CPU_CONFIG(name, backend, color, alpha) \
411 if (is_cpu_config_allowed(#name)) { \
412 Config config = { #name, Benchmark::backend, color, alpha, 0, \
413 kBogusGLContextType, false }; \
414 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700415 }
mtkleine714e752014-07-31 12:13:48 -0700416
mtklein40b32be2014-07-09 08:46:49 -0700417 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700418 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
419 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
420 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700421 }
mtkleinbb6a0282014-07-01 08:43:42 -0700422
423#if SK_SUPPORT_GPU
jvanverth4736e142014-11-07 07:12:46 -0800424 #define GPU_CONFIG(name, ctxType, samples, useDFText) \
bsalomonc2553372014-07-22 13:09:05 -0700425 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
426 Config config = { \
427 #name, \
428 Benchmark::kGPU_Backend, \
429 kN32_SkColorType, \
430 kPremul_SkAlphaType, \
431 samples, \
jvanverth4736e142014-11-07 07:12:46 -0800432 GrContextFactory::ctxType, \
433 useDFText }; \
bsalomonc2553372014-07-22 13:09:05 -0700434 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700435 }
mtkleine714e752014-07-31 12:13:48 -0700436
mtklein40b32be2014-07-09 08:46:49 -0700437 if (FLAGS_gpu) {
jvanverth4736e142014-11-07 07:12:46 -0800438 GPU_CONFIG(gpu, kNative_GLContextType, 0, false)
439 GPU_CONFIG(msaa4, kNative_GLContextType, 4, false)
440 GPU_CONFIG(msaa16, kNative_GLContextType, 16, false)
441 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4, false)
442 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16, false)
443 GPU_CONFIG(gpudft, kNative_GLContextType, 0, true)
444 GPU_CONFIG(debug, kDebug_GLContextType, 0, false)
445 GPU_CONFIG(nullgpu, kNull_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700446#ifdef SK_ANGLE
jvanverth4736e142014-11-07 07:12:46 -0800447 GPU_CONFIG(angle, kANGLE_GLContextType, 0, false)
hendrikweddbefb2015-09-11 13:07:29 -0700448 GPU_CONFIG(angle-gl, kANGLE_GL_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700449#endif
hendrikw885bf092015-08-27 10:38:39 -0700450#ifdef SK_COMMAND_BUFFER
451 GPU_CONFIG(commandbuffer, kCommandBuffer_GLContextType, 0, false)
452#endif
cdaltond416a5b2015-06-23 13:23:44 -0700453#if SK_MESA
454 GPU_CONFIG(mesa, kMESA_GLContextType, 0, false)
455#endif
mtklein40b32be2014-07-09 08:46:49 -0700456 }
mtkleinbb6a0282014-07-01 08:43:42 -0700457#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700458
459#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
460 if (is_cpu_config_allowed("hwui")) {
461 Config config = { "hwui", Benchmark::kHWUI_Backend, kRGBA_8888_SkColorType,
462 kPremul_SkAlphaType, 0, kBogusGLContextType, false };
463 configs->push(config);
464 }
465#endif
mtkleinf3723212014-06-25 14:08:00 -0700466}
467
halcanary96fcdcc2015-08-27 07:41:13 -0700468// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700469static Target* is_enabled(Benchmark* bench, const Config& config) {
470 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700471 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700472 }
473
reede5ea5002014-09-03 11:54:58 -0700474 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
475 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700476
halcanary96fcdcc2015-08-27 07:41:13 -0700477 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700478
tomhudsond968a6f2015-03-26 11:28:06 -0700479 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700480#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700481 case Benchmark::kGPU_Backend:
482 target = new GPUTarget(config);
483 break;
bsalomonc2553372014-07-22 13:09:05 -0700484#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700485#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
486 case Benchmark::kHWUI_Backend:
487 target = new HWUITarget(config, bench);
488 break;
489#endif
490 default:
491 target = new Target(config);
492 break;
493 }
bsalomonc2553372014-07-22 13:09:05 -0700494
tomhudsond968a6f2015-03-26 11:28:06 -0700495 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700496 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700497 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700498 }
499 return target;
500}
501
msarettb23e6aa2015-06-09 13:56:10 -0700502/*
503 * Returns true if set up for a subset decode succeeds, false otherwise
504 * If the set-up succeeds, the width and height parameters will be set
505 */
506static bool valid_subset_bench(const SkString& path, SkColorType colorType, bool useCodec,
507 int* width, int* height) {
508 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
509 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded));
510
msarettab80e352015-06-17 10:28:22 -0700511 // Check that we can create a codec or image decoder.
msarettb23e6aa2015-06-09 13:56:10 -0700512 if (useCodec) {
513 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
halcanary96fcdcc2015-08-27 07:41:13 -0700514 if (nullptr == codec) {
msarettb23e6aa2015-06-09 13:56:10 -0700515 SkDebugf("Could not create codec for %s. Skipping bench.\n", path.c_str());
516 return false;
517 }
518
519 // These will be initialized by SkCodec if the color type is kIndex8 and
520 // unused otherwise.
521 SkPMColor colors[256];
522 int colorCount;
523 const SkImageInfo info = codec->getInfo().makeColorType(colorType);
halcanary385fe4d2015-08-26 13:07:48 -0700524 SkAutoTDeleteArray<uint8_t> row(new uint8_t[info.minRowBytes()]);
scroggo1c005e42015-08-04 09:24:45 -0700525 SkAutoTDelete<SkScanlineDecoder> scanlineDecoder(SkScanlineDecoder::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700526 if (nullptr == scanlineDecoder || scanlineDecoder->start(info, nullptr,
scroggo1c005e42015-08-04 09:24:45 -0700527 colors, &colorCount) != SkCodec::kSuccess)
528 {
msarettb23e6aa2015-06-09 13:56:10 -0700529 SkDebugf("Could not create scanline decoder for %s with color type %s. "
msarett7f691442015-09-22 11:56:16 -0700530 "Skipping bench.\n", path.c_str(), color_type_to_str(colorType));
msarettb23e6aa2015-06-09 13:56:10 -0700531 return false;
532 }
533 *width = info.width();
534 *height = info.height();
535 } else {
536 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream));
halcanary96fcdcc2015-08-27 07:41:13 -0700537 if (nullptr == decoder) {
msarettb23e6aa2015-06-09 13:56:10 -0700538 SkDebugf("Could not create decoder for %s. Skipping bench.\n", path.c_str());
539 return false;
540 }
541 //FIXME: See skbug.com/3921
542 if (kIndex_8_SkColorType == colorType || kGray_8_SkColorType == colorType) {
543 SkDebugf("Cannot use image subset decoder for %s with color type %s. "
msarett7f691442015-09-22 11:56:16 -0700544 "Skipping bench.\n", path.c_str(), color_type_to_str(colorType));
msarettb23e6aa2015-06-09 13:56:10 -0700545 return false;
546 }
547 if (!decoder->buildTileIndex(stream.detach(), width, height)) {
548 SkDebugf("Could not build tile index for %s. Skipping bench.\n", path.c_str());
549 return false;
550 }
551 }
msarettab80e352015-06-17 10:28:22 -0700552
553 // Check if the image is large enough for a meaningful subset benchmark.
554 if (*width <= 512 && *height <= 512) {
555 // This should not print a message since it is not an error.
556 return false;
557 }
558
msarettb23e6aa2015-06-09 13:56:10 -0700559 return true;
560}
jcgregoriobf5e5232014-07-17 13:14:16 -0700561
msarett7f691442015-09-22 11:56:16 -0700562static bool valid_brd_bench(SkData* encoded, SkBitmapRegionDecoderInterface::Strategy strategy,
563 SkColorType colorType, uint32_t sampleSize, uint32_t minOutputSize, int* width,
564 int* height) {
565 SkStreamRewindable* stream = new SkMemoryStream(encoded);
566 SkAutoTDelete<SkBitmapRegionDecoderInterface> brd(
567 SkBitmapRegionDecoderInterface::CreateBitmapRegionDecoder(stream, strategy));
568 if (nullptr == brd.get()) {
569 // This is indicates that subset decoding is not supported for a particular image format.
570 return false;
571 }
572
573 SkAutoTDelete<SkBitmap> bitmap(brd->decodeRegion(0, 0, brd->width(), brd->height(), 1,
574 colorType));
575 if (nullptr == bitmap.get() || colorType != bitmap->colorType()) {
576 // This indicates that conversion to the requested color type is not supported for the
577 // particular image.
578 return false;
579 }
580
581 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
582 (uint32_t) brd->height()) {
583 // This indicates that the image is not large enough to decode a
584 // minOutputSize x minOutputSize subset at the given sampleSize.
585 return false;
586 }
587
588 // Set the image width and height. The calling code will use this to choose subsets to decode.
589 *width = brd->width();
590 *height = brd->height();
591 return true;
592}
593
egdaniel3bf92062015-06-26 08:12:46 -0700594static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700595 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700596#if SK_SUPPORT_GPU
597 if (FLAGS_abandonGpuContext) {
598 gGrFactory->abandonContexts();
599 }
600 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
601 gGrFactory->destroyContexts();
602 }
603#endif
604}
605
mtkleine714e752014-07-31 12:13:48 -0700606class BenchmarkStream {
607public:
mtklein92007582014-08-01 07:46:52 -0700608 BenchmarkStream() : fBenches(BenchRegistry::Head())
609 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700610 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700611 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800612 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800613 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700614 , fCurrentCodec(0)
msarett95f192d2015-02-13 09:05:41 -0800615 , fCurrentImage(0)
616 , fCurrentSubsetImage(0)
msarett7f691442015-09-22 11:56:16 -0700617 , fCurrentBRDImage(0)
msarett95f192d2015-02-13 09:05:41 -0800618 , fCurrentColorType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700619 , fCurrentSubsetType(0)
620 , fUseCodec(0)
msarett7f691442015-09-22 11:56:16 -0700621 , fCurrentBRDStrategy(0)
622 , fCurrentBRDSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700623 , fCurrentAnimSKP(0) {
mtklein92007582014-08-01 07:46:52 -0700624 for (int i = 0; i < FLAGS_skps.count(); i++) {
625 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
626 fSKPs.push_back() = FLAGS_skps[i];
627 } else {
628 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
629 SkString path;
630 while (it.next(&path)) {
631 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
632 }
633 }
634 }
mtkleine714e752014-07-31 12:13:48 -0700635
mtklein92007582014-08-01 07:46:52 -0700636 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
637 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
638 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
639 exit(1);
640 }
641
642 for (int i = 0; i < FLAGS_scales.count(); i++) {
643 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
644 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
645 exit(1);
646 }
647 }
robertphillips5b693772014-11-21 06:19:36 -0800648
cdalton63a82852015-06-29 14:06:10 -0700649 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
650 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700651 exit(1);
652 }
653
robertphillips5b693772014-11-21 06:19:36 -0800654 if (FLAGS_mpd) {
655 fUseMPDs.push_back() = true;
656 }
mtkleinc751ecb2015-06-15 08:56:38 -0700657 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700658
msarett95f192d2015-02-13 09:05:41 -0800659 // Prepare the images for decoding
660 for (int i = 0; i < FLAGS_images.count(); i++) {
661 const char* flag = FLAGS_images[i];
662 if (sk_isdir(flag)) {
663 // If the value passed in is a directory, add all the images
664 SkOSFile::Iter it(flag);
665 SkString file;
666 while (it.next(&file)) {
667 fImages.push_back() = SkOSPath::Join(flag, file.c_str());
668 }
669 } else if (sk_exists(flag)) {
670 // Also add the value if it is a single image
671 fImages.push_back() = flag;
672 }
673 }
mtklein95553d92015-03-12 08:24:21 -0700674
msarett95f192d2015-02-13 09:05:41 -0800675 // Choose the candidate color types for image decoding
676 const SkColorType colorTypes[] =
msarettb23e6aa2015-06-09 13:56:10 -0700677 { kN32_SkColorType,
678 kRGB_565_SkColorType,
679 kAlpha_8_SkColorType,
680 kIndex_8_SkColorType,
681 kGray_8_SkColorType };
msarett95f192d2015-02-13 09:05:41 -0800682 fColorTypes.push_back_n(SK_ARRAY_COUNT(colorTypes), colorTypes);
mtklein92007582014-08-01 07:46:52 -0700683 }
684
mtkleinfd731ce2014-09-10 12:19:30 -0700685 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
686 // Not strictly necessary, as it will be checked again later,
687 // but helps to avoid a lot of pointless work if we're going to skip it.
688 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
689 return false;
690 }
691
scroggoa1193e42015-01-21 12:09:53 -0800692 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -0700693 if (stream.get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700694 SkDebugf("Could not read %s.\n", path);
695 return false;
696 }
697
mtklein57f27bd2015-02-09 11:58:41 -0800698 pic->reset(SkPicture::CreateFromStream(stream.get()));
halcanary96fcdcc2015-08-27 07:41:13 -0700699 if (pic->get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700700 SkDebugf("Could not read %s as an SkPicture.\n", path);
701 return false;
702 }
703 return true;
704 }
705
mtklein92007582014-08-01 07:46:52 -0700706 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700707 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700708 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700709 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700710 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700711 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700712 return bench;
713 }
mtklein92007582014-08-01 07:46:52 -0700714
mtkleine714e752014-07-31 12:13:48 -0700715 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -0700716 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700717 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800718 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700719 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700720 fBenchType = "micro";
halcanary385fe4d2015-08-26 13:07:48 -0700721 return new GMBench(gm.detach());
mtkleine714e752014-07-31 12:13:48 -0700722 }
723 }
mtklein92007582014-08-01 07:46:52 -0700724
mtkleinfd731ce2014-09-10 12:19:30 -0700725 // First add all .skps as RecordingBenches.
726 while (fCurrentRecording < fSKPs.count()) {
727 const SkString& path = fSKPs[fCurrentRecording++];
728 SkAutoTUnref<SkPicture> pic;
729 if (!ReadPicture(path.c_str(), &pic)) {
730 continue;
731 }
732 SkString name = SkOSPath::Basename(path.c_str());
733 fSourceType = "skp";
734 fBenchType = "recording";
bsalomon0aa5cea2014-12-15 09:13:35 -0800735 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic));
mtklein051e56d2014-12-04 08:46:51 -0800736 fSKPOps = pic->approximateOpCount();
halcanary385fe4d2015-08-26 13:07:48 -0700737 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700738 }
739
740 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700741 while (fCurrentScale < fScales.count()) {
742 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800743 const SkString& path = fSKPs[fCurrentSKP];
mtkleinfd731ce2014-09-10 12:19:30 -0700744 SkAutoTUnref<SkPicture> pic;
745 if (!ReadPicture(path.c_str(), &pic)) {
robertphillips5b693772014-11-21 06:19:36 -0800746 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700747 continue;
748 }
robertphillips5b693772014-11-21 06:19:36 -0800749
750 while (fCurrentUseMPD < fUseMPDs.count()) {
751 if (FLAGS_bbh) {
752 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
753 SkRTreeFactory factory;
754 SkPictureRecorder recorder;
755 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
756 pic->playback(recorder.beginRecording(pic->cullRect().width(),
757 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800758 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800759 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
robertphillips5b693772014-11-21 06:19:36 -0800760 pic.reset(recorder.endRecording());
761 }
762 SkString name = SkOSPath::Basename(path.c_str());
763 fSourceType = "skp";
764 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700765 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
766 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700767 }
robertphillips5b693772014-11-21 06:19:36 -0800768 fCurrentUseMPD = 0;
769 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700770 }
771 fCurrentSKP = 0;
772 fCurrentScale++;
773 }
774
joshualitt261c3ad2015-04-27 09:16:57 -0700775 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700776 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700777 while (fCurrentAnimSKP < fSKPs.count()) {
778 const SkString& path = fSKPs[fCurrentAnimSKP];
779 SkAutoTUnref<SkPicture> pic;
780 if (!ReadPicture(path.c_str(), &pic)) {
781 fCurrentAnimSKP++;
782 continue;
783 }
784
785 fCurrentAnimSKP++;
786 SkString name = SkOSPath::Basename(path.c_str());
cdalton63a82852015-06-29 14:06:10 -0700787 SkAutoTUnref<SKPAnimationBench::Animation> animation(
788 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
halcanary385fe4d2015-08-26 13:07:48 -0700789 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation,
790 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700791 }
792 }
793
794
scroggo60869a42015-04-01 12:09:17 -0700795 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
796 const SkString& path = fImages[fCurrentCodec];
797 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
798 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700799 if (!codec) {
800 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700801 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700802 continue;
803 }
scroggo21027992015-04-02 13:22:38 -0700804
scroggo60869a42015-04-01 12:09:17 -0700805 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700806 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo60869a42015-04-01 12:09:17 -0700807 fCurrentColorType++;
scroggo21027992015-04-02 13:22:38 -0700808
scroggo60869a42015-04-01 12:09:17 -0700809 // Make sure we can decode to this color type.
scroggo60869a42015-04-01 12:09:17 -0700810 SkImageInfo info = codec->getInfo().makeColorType(colorType);
scroggo21027992015-04-02 13:22:38 -0700811 SkAlphaType alphaType;
812 if (!SkColorTypeValidateAlphaType(colorType, info.alphaType(),
813 &alphaType)) {
814 continue;
815 }
816 if (alphaType != info.alphaType()) {
817 info = info.makeAlphaType(alphaType);
818 }
819
820 const size_t rowBytes = info.minRowBytes();
821 SkAutoMalloc storage(info.getSafeSize(rowBytes));
822
823 // Used if fCurrentColorType is kIndex_8_SkColorType
824 int colorCount = 256;
825 SkPMColor colors[256];
826
scroggoeb602a52015-07-09 08:16:03 -0700827 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700828 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700829 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700830 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700831 case SkCodec::kSuccess:
832 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700833 return new CodecBench(SkOSPath::Basename(path.c_str()),
834 encoded, colorType);
scroggoeb602a52015-07-09 08:16:03 -0700835 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700836 // This is okay. Not all conversions are valid.
837 break;
scroggo60869a42015-04-01 12:09:17 -0700838 default:
839 // This represents some sort of failure.
840 SkASSERT(false);
841 break;
842 }
843 }
844 fCurrentColorType = 0;
845 }
846
msarett95f192d2015-02-13 09:05:41 -0800847 // Run the DecodingBenches
848 while (fCurrentImage < fImages.count()) {
849 while (fCurrentColorType < fColorTypes.count()) {
850 const SkString& path = fImages[fCurrentImage];
851 SkColorType colorType = fColorTypes[fCurrentColorType];
852 fCurrentColorType++;
scroggo60869a42015-04-01 12:09:17 -0700853 // Check if the image decodes to the right color type
854 // before creating the benchmark
msarett95f192d2015-02-13 09:05:41 -0800855 SkBitmap bitmap;
856 if (SkImageDecoder::DecodeFile(path.c_str(), &bitmap,
scroggo60869a42015-04-01 12:09:17 -0700857 colorType, SkImageDecoder::kDecodePixels_Mode)
858 && bitmap.colorType() == colorType) {
msarett95f192d2015-02-13 09:05:41 -0800859 return new DecodingBench(path, colorType);
860 }
861 }
862 fCurrentColorType = 0;
863 fCurrentImage++;
864 }
865
msarettb23e6aa2015-06-09 13:56:10 -0700866 // Run the SubsetBenches
867 bool useCodecOpts[] = { true, false };
868 while (fUseCodec < 2) {
869 bool useCodec = useCodecOpts[fUseCodec];
870 while (fCurrentSubsetImage < fImages.count()) {
871 while (fCurrentColorType < fColorTypes.count()) {
872 const SkString& path = fImages[fCurrentSubsetImage];
873 SkColorType colorType = fColorTypes[fCurrentColorType];
874 while (fCurrentSubsetType <= kLast_SubsetType) {
875 int width = 0;
876 int height = 0;
877 int currentSubsetType = fCurrentSubsetType++;
878 if (valid_subset_bench(path, colorType, useCodec, &width, &height)) {
879 switch (currentSubsetType) {
880 case kTopLeft_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700881 return new SubsetSingleBench(path, colorType, width/3,
882 height/3, 0, 0, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700883 case kTopRight_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700884 return new SubsetSingleBench(path, colorType, width/3,
885 height/3, 2*width/3, 0, useCodec);
886 case kMiddle_SubsetType:
887 return new SubsetSingleBench(path, colorType, width/3,
888 height/3, width/3, height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700889 case kBottomLeft_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700890 return new SubsetSingleBench(path, colorType, width/3,
891 height/3, 0, 2*height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700892 case kBottomRight_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700893 return new SubsetSingleBench(path, colorType, width/3,
894 height/3, 2*width/3, 2*height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700895 case kTranslate_SubsetType:
896 return new SubsetTranslateBench(path, colorType, 512, 512,
897 useCodec);
898 case kZoom_SubsetType:
899 return new SubsetZoomBench(path, colorType, 512, 512,
900 useCodec);
msarett95f192d2015-02-13 09:05:41 -0800901 }
msarettb23e6aa2015-06-09 13:56:10 -0700902 } else {
903 break;
msarett95f192d2015-02-13 09:05:41 -0800904 }
905 }
msarettb23e6aa2015-06-09 13:56:10 -0700906 fCurrentSubsetType = 0;
907 fCurrentColorType++;
msarett95f192d2015-02-13 09:05:41 -0800908 }
msarettb23e6aa2015-06-09 13:56:10 -0700909 fCurrentColorType = 0;
910 fCurrentSubsetImage++;
msarett95f192d2015-02-13 09:05:41 -0800911 }
msarettb23e6aa2015-06-09 13:56:10 -0700912 fCurrentSubsetImage = 0;
913 fUseCodec++;
msarett95f192d2015-02-13 09:05:41 -0800914 }
915
msarett7f691442015-09-22 11:56:16 -0700916 // Run the BRDBenches
917 // We will benchmark multiple BRD strategies.
918 const SkBitmapRegionDecoderInterface::Strategy strategies[] = {
919 SkBitmapRegionDecoderInterface::kOriginal_Strategy,
920 SkBitmapRegionDecoderInterface::kCanvas_Strategy,
921 };
922
923 // We intend to create benchmarks that model the use cases in
924 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
925 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
926 // the image. For that reason, we will benchmark decodes in five representative locations
927 // in the image. Additionally, this use case utilizes power of two scaling, so we will
928 // test on power of two sample sizes. The output tile is always 512x512, so, when a
929 // sampleSize is used, the size of the subset that is decoded is always
930 // (sampleSize*512)x(sampleSize*512).
931 // There are a few good reasons to only test on power of two sample sizes at this time:
932 // JPEG decodes using kOriginal_Strategy are broken for non-powers of two.
933 // skbug.com/4319
934 // All use cases we are aware of only scale by powers of two.
935 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
936 // these tests are sufficient to provide good coverage of our scaling options.
937 const uint32_t sampleSizes[] = { 1, 2, 4, 8, 16 };
938 const uint32_t minOutputSize = 512;
939 while (fCurrentBRDImage < fImages.count()) {
940 while (fCurrentBRDStrategy < (int) SK_ARRAY_COUNT(strategies)) {
941 while (fCurrentColorType < fColorTypes.count()) {
942 while (fCurrentBRDSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
943 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
944 const SkString& path = fImages[fCurrentBRDImage];
945 const SkBitmapRegionDecoderInterface::Strategy strategy =
946 strategies[fCurrentBRDStrategy];
947 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
948 const SkColorType colorType = fColorTypes[fCurrentColorType];
949 uint32_t sampleSize = sampleSizes[fCurrentBRDSampleSize];
950 int currentSubsetType = fCurrentSubsetType++;
951
952 int width = 0;
953 int height = 0;
954 if (!valid_brd_bench(encoded.get(), strategy, colorType, sampleSize,
955 minOutputSize, &width, &height)) {
956 break;
957 }
958
959 SkString basename = SkOSPath::Basename(path.c_str());
960 SkIRect subset;
961 const uint32_t subsetSize = sampleSize * minOutputSize;
962 switch (currentSubsetType) {
963 case kTopLeft_SubsetType:
964 basename.append("_TopLeft");
965 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
966 break;
967 case kTopRight_SubsetType:
968 basename.append("_TopRight");
969 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
970 subsetSize);
971 break;
972 case kMiddle_SubsetType:
973 basename.append("_Middle");
974 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
975 (height - subsetSize) / 2, subsetSize, subsetSize);
976 break;
977 case kBottomLeft_SubsetType:
978 basename.append("_BottomLeft");
979 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
980 subsetSize);
981 break;
982 case kBottomRight_SubsetType:
983 basename.append("_BottomRight");
984 subset = SkIRect::MakeXYWH(width - subsetSize,
985 height - subsetSize, subsetSize, subsetSize);
986 break;
987 default:
988 SkASSERT(false);
989 }
990
991 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
992 strategy, colorType, sampleSize, subset);
993 }
994 fCurrentSubsetType = 0;
995 fCurrentBRDSampleSize++;
996 }
997 fCurrentBRDSampleSize = 0;
998 fCurrentColorType++;
999 }
1000 fCurrentColorType = 0;
1001 fCurrentBRDStrategy++;
1002 }
1003 fCurrentBRDStrategy = 0;
1004 fCurrentBRDImage++;
1005 }
1006
halcanary96fcdcc2015-08-27 07:41:13 -07001007 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001008 }
mtklein92007582014-08-01 07:46:52 -07001009
1010 void fillCurrentOptions(ResultsWriter* log) const {
1011 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001012 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001013 if (0 == strcmp(fSourceType, "skp")) {
1014 log->configOption("clip",
1015 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1016 fClip.fRight, fClip.fBottom).c_str());
1017 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001018 if (fCurrentUseMPD > 0) {
1019 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1020 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1021 }
mtklein92007582014-08-01 07:46:52 -07001022 }
mtklein051e56d2014-12-04 08:46:51 -08001023 if (0 == strcmp(fBenchType, "recording")) {
1024 log->metric("bytes", fSKPBytes);
1025 log->metric("ops", fSKPOps);
1026 }
mtklein92007582014-08-01 07:46:52 -07001027 }
1028
mtkleine714e752014-07-31 12:13:48 -07001029private:
msarettb23e6aa2015-06-09 13:56:10 -07001030 enum SubsetType {
1031 kTopLeft_SubsetType = 0,
1032 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001033 kMiddle_SubsetType = 2,
1034 kBottomLeft_SubsetType = 3,
1035 kBottomRight_SubsetType = 4,
1036 kTranslate_SubsetType = 5,
1037 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001038 kLast_SubsetType = kZoom_SubsetType,
1039 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001040 };
1041
mtkleine714e752014-07-31 12:13:48 -07001042 const BenchRegistry* fBenches;
1043 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001044 SkIRect fClip;
1045 SkTArray<SkScalar> fScales;
1046 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -08001047 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001048 SkTArray<SkString> fImages;
1049 SkTArray<SkColorType> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001050 SkScalar fZoomMax;
1051 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001052
mtklein051e56d2014-12-04 08:46:51 -08001053 double fSKPBytes, fSKPOps;
1054
mtkleinfd731ce2014-09-10 12:19:30 -07001055 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1056 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1057 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -07001058 int fCurrentScale;
1059 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -08001060 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001061 int fCurrentCodec;
msarett95f192d2015-02-13 09:05:41 -08001062 int fCurrentImage;
1063 int fCurrentSubsetImage;
msarett7f691442015-09-22 11:56:16 -07001064 int fCurrentBRDImage;
msarett95f192d2015-02-13 09:05:41 -08001065 int fCurrentColorType;
msarettb23e6aa2015-06-09 13:56:10 -07001066 int fCurrentSubsetType;
1067 int fUseCodec;
msarett7f691442015-09-22 11:56:16 -07001068 int fCurrentBRDStrategy;
1069 int fCurrentBRDSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001070 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001071};
1072
jcgregorio3b27ade2014-11-13 08:06:40 -08001073int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001074int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001075 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001076 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001077 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001078
krajcevski69a55602014-08-13 10:46:31 -07001079#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001080 GrContextOptions grContextOpts;
krajcevski12b35442014-08-13 12:06:26 -07001081 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
halcanary385fe4d2015-08-26 13:07:48 -07001082 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001083#endif
1084
bsalomon06cddec2014-10-24 10:40:50 -07001085 if (FLAGS_veryVerbose) {
1086 FLAGS_verbose = true;
1087 }
1088
cdaltone1b89582015-06-25 19:17:08 -07001089 double samplingTimeMs = 0;
1090 if (0 != strcmp("0", FLAGS_samplingTime[0])) {
1091 SkSTArray<8, char> timeUnit;
1092 timeUnit.push_back_n(static_cast<int>(strlen(FLAGS_samplingTime[0])) + 1);
1093 if (2 != sscanf(FLAGS_samplingTime[0], "%lf%s", &samplingTimeMs, timeUnit.begin()) ||
1094 (0 != strcmp("s", timeUnit.begin()) && 0 != strcmp("ms", timeUnit.begin()))) {
1095 SkDebugf("Invalid --samplingTime \"%s\". Must be \"0\", \"%%lfs\", or \"%%lfms\"\n",
1096 FLAGS_samplingTime[0]);
1097 exit(0);
1098 }
1099 if (0 == strcmp("s", timeUnit.begin())) {
1100 samplingTimeMs *= 1000;
1101 }
1102 if (samplingTimeMs) {
1103 FLAGS_samples = kTimedSampling;
1104 }
1105 }
1106
bsalomon6eb03cc2014-08-07 14:28:50 -07001107 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001108 FLAGS_samples = 1;
1109 FLAGS_gpuFrameLag = 0;
1110 }
1111
bsalomon6eb03cc2014-08-07 14:28:50 -07001112 if (!FLAGS_writePath.isEmpty()) {
1113 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1114 if (!sk_mkdir(FLAGS_writePath[0])) {
1115 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001116 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001117 }
1118 }
1119
halcanary385fe4d2015-08-26 13:07:48 -07001120 SkAutoTDelete<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001121 if (!FLAGS_outResultsFile.isEmpty()) {
halcanary385fe4d2015-08-26 13:07:48 -07001122 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein60317d0f2014-07-14 11:30:37 -07001123 }
mtklein1915b622014-08-20 11:45:00 -07001124
1125 if (1 == FLAGS_properties.count() % 2) {
1126 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1127 return 1;
1128 }
1129 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1130 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1131 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001132
1133 if (1 == FLAGS_key.count() % 2) {
1134 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1135 return 1;
1136 }
1137 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001138 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001139 }
mtklein60317d0f2014-07-14 11:30:37 -07001140
mtkleinf3723212014-06-25 14:08:00 -07001141 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001142 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001143
cdaltone1b89582015-06-25 19:17:08 -07001144 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001145
bsalomon6eb03cc2014-08-07 14:28:50 -07001146 if (kAutoTuneLoops != FLAGS_loops) {
1147 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001148 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -07001149 SkDebugf("median\tbench\tconfig\n");
cdaltone1b89582015-06-25 19:17:08 -07001150 } else if (kTimedSampling == FLAGS_samples) {
1151 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001152 } else {
mtkleind75c4662015-04-30 07:11:22 -07001153 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001154 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001155 }
1156
bsalomonc2553372014-07-22 13:09:05 -07001157 SkTDArray<Config> configs;
1158 create_configs(&configs);
1159
mtkleine070c2b2014-10-14 08:40:43 -07001160 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001161 BenchmarkStream benchStream;
1162 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001163 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001164 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001165 continue;
1166 }
1167
egdaniel3bf92062015-06-26 08:12:46 -07001168 if (!configs.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -07001169 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
jcgregoriobf5e5232014-07-17 13:14:16 -07001170 bench->preDraw();
1171 }
egdaniel3bf92062015-06-26 08:12:46 -07001172 for (int i = 0; i < configs.count(); ++i) {
1173 Target* target = is_enabled(b, configs[i]);
1174 if (!target) {
1175 continue;
1176 }
mtkleinf3723212014-06-25 14:08:00 -07001177
halcanary96fcdcc2015-08-27 07:41:13 -07001178 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001179 SkCanvas* canvas = target->getCanvas();
1180 const char* config = target->config.name;
1181
1182 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001183 bench->perCanvasPreDraw(canvas);
1184
cdaltone1b89582015-06-25 19:17:08 -07001185 int maxFrameLag;
egdaniel3bf92062015-06-26 08:12:46 -07001186 const int loops = target->needsFrameTiming(&maxFrameLag)
1187 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1188 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001189
1190 if (kTimedSampling != FLAGS_samples) {
1191 samples.reset(FLAGS_samples);
1192 for (int s = 0; s < FLAGS_samples; s++) {
egdaniel3bf92062015-06-26 08:12:46 -07001193 samples[s] = time(loops, bench, target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001194 }
1195 } else if (samplingTimeMs) {
1196 samples.reset();
1197 if (FLAGS_verbose) {
1198 SkDebugf("Begin sampling %s for %ims\n",
1199 bench->getUniqueName(), static_cast<int>(samplingTimeMs));
1200 }
1201 WallTimer timer;
1202 timer.start();
1203 do {
egdaniel3bf92062015-06-26 08:12:46 -07001204 samples.push_back(time(loops, bench, target) / loops);
cdaltone1b89582015-06-25 19:17:08 -07001205 timer.end();
1206 } while (timer.fWall < samplingTimeMs);
1207 }
mtkleinf3723212014-06-25 14:08:00 -07001208
robertphillips5b693772014-11-21 06:19:36 -08001209 bench->perCanvasPostDraw(canvas);
1210
egdaniel3bf92062015-06-26 08:12:46 -07001211 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001212 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001213 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001214 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001215 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001216 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001217 }
1218
1219 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001220 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001221 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001222 continue;
1223 }
1224
cdaltone1b89582015-06-25 19:17:08 -07001225 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001226 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001227 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001228 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001229 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001230 log->metric("min_ms", stats.min);
mtkleine070c2b2014-10-14 08:40:43 -07001231 if (runs++ % FLAGS_flushEvery == 0) {
1232 log->flush();
1233 }
mtklein60317d0f2014-07-14 11:30:37 -07001234
bsalomon6eb03cc2014-08-07 14:28:50 -07001235 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001236 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001237 config = ""; // Only print the config if we run the same bench on more than one.
1238 }
mtkleind75c4662015-04-30 07:11:22 -07001239 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1240 , sk_tools::getCurrResidentSetSizeMB()
1241 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001242 , bench->getUniqueName()
1243 , config);
mtkleinf3723212014-06-25 14:08:00 -07001244 } else if (FLAGS_quiet) {
egdaniel3bf92062015-06-26 08:12:46 -07001245 if (configs.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -07001246 config = ""; // Only print the config if we run the same bench on more than one.
1247 }
mtklein96289052014-09-10 12:05:59 -07001248 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001249 } else {
1250 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001251 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1252 , sk_tools::getCurrResidentSetSizeMB()
1253 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001254 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001255 , HUMANIZE(stats.min)
1256 , HUMANIZE(stats.median)
1257 , HUMANIZE(stats.mean)
1258 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001259 , stddev_percent
cdaltone1b89582015-06-25 19:17:08 -07001260 , kTimedSampling != FLAGS_samples ? stats.plot.c_str()
1261 : to_string(samples.count()).c_str()
mtkleinf3723212014-06-25 14:08:00 -07001262 , config
mtklein96289052014-09-10 12:05:59 -07001263 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001264 );
1265 }
bsalomonb12ea412015-02-02 21:19:50 -08001266#if SK_SUPPORT_GPU
1267 if (FLAGS_gpuStats &&
egdaniel3bf92062015-06-26 08:12:46 -07001268 Benchmark::kGPU_Backend == configs[i].backend) {
1269 gGrFactory->get(configs[i].ctxType)->printCacheStats();
1270 gGrFactory->get(configs[i].ctxType)->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001271 }
1272#endif
cdalton2c56ba52015-06-26 13:32:53 -07001273 if (FLAGS_verbose) {
1274 SkDebugf("Samples: ");
1275 for (int i = 0; i < samples.count(); i++) {
1276 SkDebugf("%s ", HUMANIZE(samples[i]));
1277 }
1278 SkDebugf("%s\n", bench->getUniqueName());
1279 }
egdaniel3bf92062015-06-26 08:12:46 -07001280 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001281 }
mtkleinf3723212014-06-25 14:08:00 -07001282 }
1283
mtkleine1091452014-12-04 10:47:02 -08001284 log->bench("memory_usage", 0,0);
1285 log->config("meta");
1286 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1287
joshualitte0b19d42015-03-26 10:41:02 -07001288#if SK_SUPPORT_GPU
1289 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1290 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001291 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001292#endif
1293
mtkleinf3723212014-06-25 14:08:00 -07001294 return 0;
1295}
1296
jcgregorio3b27ade2014-11-13 08:06:40 -08001297#if !defined SK_BUILD_FOR_IOS
1298int main(int argc, char** argv) {
1299 SkCommandLineFlags::Parse(argc, argv);
1300 return nanobench_main();
1301}
1302#endif