blob: 6328aa15ab4a9f5d74860c69721ae0eb07f8c376 [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"
mtkleinf3723212014-06-25 14:08:00 -070041#include "SkString.h"
42#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080043#include "SkTaskGroup.h"
mtkleinf3723212014-06-25 14:08:00 -070044
bungeman60e0fee2015-08-26 05:15:46 -070045#include <stdlib.h>
46
tomhudsond968a6f2015-03-26 11:28:06 -070047#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
48 #include "nanobenchAndroid.h"
49#endif
50
mtkleinbb6a0282014-07-01 08:43:42 -070051#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070052 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070053 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070054 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070055 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070056#endif
57
bsalomon682c2692015-05-22 14:01:46 -070058 struct GrContextOptions;
59
mtkleinf3723212014-06-25 14:08:00 -070060__SK_FORCE_IMAGE_DECODER_LINKING;
61
cdaltone1b89582015-06-25 19:17:08 -070062static const int kTimedSampling = 0;
63
reed53249782014-10-10 09:09:52 -070064static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070065
mtkleinb5110422014-08-07 15:20:02 -070066static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070067#ifdef SK_DEBUG
68 1;
mtkleina189ccd2014-07-14 12:28:47 -070069#else
bsalomon6eb03cc2014-08-07 14:28:50 -070070 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070071#endif
72
bsalomon6eb03cc2014-08-07 14:28:50 -070073static SkString loops_help_txt() {
74 SkString help;
75 help.printf("Number of times to run each bench. Set this to %d to auto-"
76 "tune for each bench. Timings are only reported when auto-tuning.",
77 kAutoTuneLoops);
78 return help;
79}
80
cdaltone1b89582015-06-25 19:17:08 -070081static SkString to_string(int n) {
82 SkString str;
83 str.appendS32(n);
84 return str;
85}
86
bsalomon6eb03cc2014-08-07 14:28:50 -070087DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
88
mtkleinf3723212014-06-25 14:08:00 -070089DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
cdaltone1b89582015-06-25 19:17:08 -070090DEFINE_string(samplingTime, "0", "Amount of time to run each bench. Takes precedence over samples."
91 "Must be \"0\", \"%%lfs\", or \"%%lfms\"");
mtkleinf3723212014-06-25 14:08:00 -070092DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
93DEFINE_double(overheadGoal, 0.0001,
94 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070095DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -070096DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070097DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
98 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070099
mtklein60317d0f2014-07-14 11:30:37 -0700100DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -0700101DEFINE_int32(maxCalibrationAttempts, 3,
102 "Try up to this many times to guess loops for a bench, or skip the bench.");
103DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700104DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
105DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700106DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
107 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700108DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
robertphillips5b693772014-11-21 06:19:36 -0800109DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700110DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700111DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800112DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800113DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
mtklein92007582014-08-01 07:46:52 -0700114
mtkleinf3723212014-06-25 14:08:00 -0700115static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700116 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800117 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700118}
mtklein55b0ffc2014-07-17 08:38:23 -0700119#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700120
tomhudsond968a6f2015-03-26 11:28:06 -0700121bool Target::init(SkImageInfo info, Benchmark* bench) {
122 if (Benchmark::kRaster_Backend == config.backend) {
123 this->surface.reset(SkSurface::NewRaster(info));
124 if (!this->surface.get()) {
125 return false;
126 }
127 }
128 return true;
129}
130bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700131 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700132 if (!canvas) {
133 return false;
134 }
135 bmp->setInfo(canvas->imageInfo());
136 if (!canvas->readPixels(bmp, 0, 0)) {
137 SkDebugf("Can't read canvas pixels.\n");
138 return false;
139 }
140 return true;
141}
142
143#if SK_SUPPORT_GPU
144struct GPUTarget : public Target {
halcanary96fcdcc2015-08-27 07:41:13 -0700145 explicit GPUTarget(const Config& c) : Target(c), gl(nullptr) { }
tomhudsond968a6f2015-03-26 11:28:06 -0700146 SkGLContext* gl;
147
148 void setup() override {
149 this->gl->makeCurrent();
150 // Make sure we're done with whatever came before.
151 SK_GL(*this->gl, Finish());
152 }
153 void endTiming() override {
154 if (this->gl) {
155 SK_GL(*this->gl, Flush());
156 this->gl->swapBuffers();
157 }
158 }
159 void fence() override {
160 SK_GL(*this->gl, Finish());
161 }
mtkleind75c4662015-04-30 07:11:22 -0700162
cdaltond416a5b2015-06-23 13:23:44 -0700163 bool needsFrameTiming(int* maxFrameLag) const override {
164 if (!this->gl->getMaxGpuFrameLag(maxFrameLag)) {
165 // Frame lag is unknown.
166 *maxFrameLag = FLAGS_gpuFrameLag;
167 }
168 return true;
169 }
tomhudsond968a6f2015-03-26 11:28:06 -0700170 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700171 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
172 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700173 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
174 this->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(this->config.ctxType),
175 SkSurface::kNo_Budgeted, info,
176 this->config.samples, &props));
177 this->gl = gGrFactory->getGLContext(this->config.ctxType);
178 if (!this->surface.get()) {
179 return false;
180 }
cdaltond416a5b2015-06-23 13:23:44 -0700181 if (!this->gl->fenceSyncSupport()) {
182 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
183 "Timings might not be accurate.\n", this->config.name);
184 }
tomhudsond968a6f2015-03-26 11:28:06 -0700185 return true;
186 }
187 void fillOptions(ResultsWriter* log) override {
188 const GrGLubyte* version;
189 SK_GL_RET(*this->gl, version, GetString(GR_GL_VERSION));
190 log->configOption("GL_VERSION", (const char*)(version));
191
192 SK_GL_RET(*this->gl, version, GetString(GR_GL_RENDERER));
193 log->configOption("GL_RENDERER", (const char*) version);
194
195 SK_GL_RET(*this->gl, version, GetString(GR_GL_VENDOR));
196 log->configOption("GL_VENDOR", (const char*) version);
197
198 SK_GL_RET(*this->gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
199 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
200 }
201};
mtkleind75c4662015-04-30 07:11:22 -0700202
tomhudsond968a6f2015-03-26 11:28:06 -0700203#endif
204
tomhudson75a0ebb2015-03-27 12:11:44 -0700205static double time(int loops, Benchmark* bench, Target* target) {
206 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700207 if (canvas) {
208 canvas->clear(SK_ColorWHITE);
209 }
joshualitt8a6697a2015-09-30 12:11:07 -0700210 bench->preDraw(canvas);
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();
joshualitt8a6697a2015-09-30 12:11:07 -0700220 bench->postDraw(canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700221 return timer.fWall;
222}
223
mtkleinf3723212014-06-25 14:08:00 -0700224static double estimate_timer_overhead() {
225 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700226 for (int i = 0; i < FLAGS_overheadLoops; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700227 WallTimer timer;
228 timer.start();
229 timer.end();
230 overhead += timer.fWall;
mtkleinf3723212014-06-25 14:08:00 -0700231 }
232 return overhead / FLAGS_overheadLoops;
233}
234
reed53249782014-10-10 09:09:52 -0700235static int detect_forever_loops(int loops) {
236 // look for a magic run-forever value
237 if (loops < 0) {
238 loops = SK_MaxS32;
239 }
240 return loops;
241}
242
mtklein55b0ffc2014-07-17 08:38:23 -0700243static int clamp_loops(int loops) {
244 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800245 SkDebugf("ERROR: clamping loops from %d to 1. "
246 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700247 return 1;
248 }
249 if (loops > FLAGS_maxLoops) {
250 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
251 return FLAGS_maxLoops;
252 }
253 return loops;
254}
255
tomhudsond968a6f2015-03-26 11:28:06 -0700256static bool write_canvas_png(Target* target, const SkString& filename) {
257
bsalomon6eb03cc2014-08-07 14:28:50 -0700258 if (filename.isEmpty()) {
259 return false;
260 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700261 if (target->getCanvas() &&
262 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700263 return false;
264 }
tomhudsond968a6f2015-03-26 11:28:06 -0700265
bsalomon6eb03cc2014-08-07 14:28:50 -0700266 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700267
268 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700269 return false;
270 }
tomhudsond968a6f2015-03-26 11:28:06 -0700271
bsalomon6eb03cc2014-08-07 14:28:50 -0700272 SkString dir = SkOSPath::Dirname(filename.c_str());
273 if (!sk_mkdir(dir.c_str())) {
274 SkDebugf("Can't make dir %s.\n", dir.c_str());
275 return false;
276 }
277 SkFILEWStream stream(filename.c_str());
278 if (!stream.isValid()) {
279 SkDebugf("Can't write %s.\n", filename.c_str());
280 return false;
281 }
282 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
283 SkDebugf("Can't encode a PNG.\n");
284 return false;
285 }
286 return true;
287}
288
289static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700290static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700291 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700292 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700293 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700294 int loops = bench->calculateLoops(FLAGS_loops);
295 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700296 while (bench_plus_overhead < overhead) {
297 if (round++ == FLAGS_maxCalibrationAttempts) {
298 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700299 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700300 return kFailedLoops;
301 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700302 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700303 }
mtklein2069e222014-08-04 13:57:39 -0700304 }
mtkleinf3723212014-06-25 14:08:00 -0700305
mtkleinbb6a0282014-07-01 08:43:42 -0700306 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700307 // We'll pick N to make timer overhead negligible:
308 //
mtkleinbb6a0282014-07-01 08:43:42 -0700309 // overhead
310 // ------------------------- < FLAGS_overheadGoal
311 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700312 //
mtkleinbb6a0282014-07-01 08:43:42 -0700313 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700314 //
315 // Doing some math, we get:
316 //
mtkleinbb6a0282014-07-01 08:43:42 -0700317 // (overhead / FLAGS_overheadGoal) - overhead
318 // ------------------------------------------ < N
319 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700320 //
321 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700322 if (kAutoTuneLoops == loops) {
323 const double numer = overhead / FLAGS_overheadGoal - overhead;
324 const double denom = bench_plus_overhead - overhead;
325 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700326 loops = clamp_loops(loops);
327 } else {
328 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700329 }
mtkleinbb6a0282014-07-01 08:43:42 -0700330
mtkleinbb6a0282014-07-01 08:43:42 -0700331 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700332}
333
cdaltone1b89582015-06-25 19:17:08 -0700334static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700335 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700336 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700337 if (kAutoTuneLoops == loops) {
338 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700339 double elapsed = 0;
340 do {
mtklein527930f2014-11-06 08:04:34 -0800341 if (1<<30 == loops) {
342 // We're about to wrap. Something's wrong with the bench.
343 loops = 0;
344 break;
345 }
mtkleina189ccd2014-07-14 12:28:47 -0700346 loops *= 2;
347 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700348 // _this_ round, not still timing last round.
349 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700350 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700351 }
352 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700353
mtkleina189ccd2014-07-14 12:28:47 -0700354 // We've overshot at least a little. Scale back linearly.
355 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700356 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700357
tomhudsond968a6f2015-03-26 11:28:06 -0700358 // Make sure we're not still timing our calibration.
359 target->fence();
reed53249782014-10-10 09:09:52 -0700360 } else {
361 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700362 }
mtkleinbb6a0282014-07-01 08:43:42 -0700363
364 // Pretty much the same deal as the calibration: do some warmup to make
365 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700366 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700367 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700368 }
mtkleinbb6a0282014-07-01 08:43:42 -0700369
mtkleinbb6a0282014-07-01 08:43:42 -0700370 return loops;
371}
mtkleinbb6a0282014-07-01 08:43:42 -0700372
373static SkString to_lower(const char* str) {
374 SkString lower(str);
375 for (size_t i = 0; i < lower.size(); i++) {
376 lower[i] = tolower(lower[i]);
377 }
378 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700379}
380
bsalomonc2553372014-07-22 13:09:05 -0700381static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700382 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700383 if (to_lower(FLAGS_config[i]).equals(name)) {
384 return true;
mtkleinf3723212014-06-25 14:08:00 -0700385 }
386 }
bsalomonc2553372014-07-22 13:09:05 -0700387 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700388}
389
bsalomonc2553372014-07-22 13:09:05 -0700390#if SK_SUPPORT_GPU
391static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
392 int sampleCnt) {
393 if (!is_cpu_config_allowed(name)) {
394 return false;
395 }
krajcevski69a55602014-08-13 10:46:31 -0700396 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomon76228632015-05-29 08:02:10 -0700397 return sampleCnt <= ctx->caps()->maxSampleCount();
bsalomonc2553372014-07-22 13:09:05 -0700398 }
399 return false;
400}
401#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700402
bsalomonc2553372014-07-22 13:09:05 -0700403#if SK_SUPPORT_GPU
404#define kBogusGLContextType GrContextFactory::kNative_GLContextType
405#else
406#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700407#endif
bsalomonc2553372014-07-22 13:09:05 -0700408
409// Append all configs that are enabled and supported.
410static void create_configs(SkTDArray<Config>* configs) {
jvanverth4736e142014-11-07 07:12:46 -0800411 #define CPU_CONFIG(name, backend, color, alpha) \
412 if (is_cpu_config_allowed(#name)) { \
413 Config config = { #name, Benchmark::backend, color, alpha, 0, \
414 kBogusGLContextType, false }; \
415 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700416 }
mtkleine714e752014-07-31 12:13:48 -0700417
mtklein40b32be2014-07-09 08:46:49 -0700418 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700419 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
420 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
421 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700422 }
mtkleinbb6a0282014-07-01 08:43:42 -0700423
424#if SK_SUPPORT_GPU
jvanverth4736e142014-11-07 07:12:46 -0800425 #define GPU_CONFIG(name, ctxType, samples, useDFText) \
bsalomonc2553372014-07-22 13:09:05 -0700426 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
427 Config config = { \
428 #name, \
429 Benchmark::kGPU_Backend, \
430 kN32_SkColorType, \
431 kPremul_SkAlphaType, \
432 samples, \
jvanverth4736e142014-11-07 07:12:46 -0800433 GrContextFactory::ctxType, \
434 useDFText }; \
bsalomonc2553372014-07-22 13:09:05 -0700435 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700436 }
mtkleine714e752014-07-31 12:13:48 -0700437
mtklein40b32be2014-07-09 08:46:49 -0700438 if (FLAGS_gpu) {
jvanverth4736e142014-11-07 07:12:46 -0800439 GPU_CONFIG(gpu, kNative_GLContextType, 0, false)
440 GPU_CONFIG(msaa4, kNative_GLContextType, 4, false)
441 GPU_CONFIG(msaa16, kNative_GLContextType, 16, false)
442 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4, false)
443 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16, false)
444 GPU_CONFIG(gpudft, kNative_GLContextType, 0, true)
445 GPU_CONFIG(debug, kDebug_GLContextType, 0, false)
446 GPU_CONFIG(nullgpu, kNull_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700447#ifdef SK_ANGLE
jvanverth4736e142014-11-07 07:12:46 -0800448 GPU_CONFIG(angle, kANGLE_GLContextType, 0, false)
hendrikweddbefb2015-09-11 13:07:29 -0700449 GPU_CONFIG(angle-gl, kANGLE_GL_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700450#endif
hendrikw885bf092015-08-27 10:38:39 -0700451#ifdef SK_COMMAND_BUFFER
452 GPU_CONFIG(commandbuffer, kCommandBuffer_GLContextType, 0, false)
453#endif
cdaltond416a5b2015-06-23 13:23:44 -0700454#if SK_MESA
455 GPU_CONFIG(mesa, kMESA_GLContextType, 0, false)
456#endif
mtklein40b32be2014-07-09 08:46:49 -0700457 }
mtkleinbb6a0282014-07-01 08:43:42 -0700458#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700459
460#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
461 if (is_cpu_config_allowed("hwui")) {
462 Config config = { "hwui", Benchmark::kHWUI_Backend, kRGBA_8888_SkColorType,
463 kPremul_SkAlphaType, 0, kBogusGLContextType, false };
464 configs->push(config);
465 }
466#endif
mtkleinf3723212014-06-25 14:08:00 -0700467}
468
halcanary96fcdcc2015-08-27 07:41:13 -0700469// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700470static Target* is_enabled(Benchmark* bench, const Config& config) {
471 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700472 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700473 }
474
reede5ea5002014-09-03 11:54:58 -0700475 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
476 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700477
halcanary96fcdcc2015-08-27 07:41:13 -0700478 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700479
tomhudsond968a6f2015-03-26 11:28:06 -0700480 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700481#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700482 case Benchmark::kGPU_Backend:
483 target = new GPUTarget(config);
484 break;
bsalomonc2553372014-07-22 13:09:05 -0700485#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700486#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
487 case Benchmark::kHWUI_Backend:
488 target = new HWUITarget(config, bench);
489 break;
490#endif
491 default:
492 target = new Target(config);
493 break;
494 }
bsalomonc2553372014-07-22 13:09:05 -0700495
tomhudsond968a6f2015-03-26 11:28:06 -0700496 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700497 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700498 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700499 }
500 return target;
501}
502
msarettb23e6aa2015-06-09 13:56:10 -0700503/*
504 * Returns true if set up for a subset decode succeeds, false otherwise
505 * If the set-up succeeds, the width and height parameters will be set
506 */
507static bool valid_subset_bench(const SkString& path, SkColorType colorType, bool useCodec,
508 int* width, int* height) {
509 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
510 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded));
511
msarettab80e352015-06-17 10:28:22 -0700512 // Check that we can create a codec or image decoder.
msarettb23e6aa2015-06-09 13:56:10 -0700513 if (useCodec) {
514 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
halcanary96fcdcc2015-08-27 07:41:13 -0700515 if (nullptr == codec) {
msarettb23e6aa2015-06-09 13:56:10 -0700516 SkDebugf("Could not create codec for %s. Skipping bench.\n", path.c_str());
517 return false;
518 }
519
520 // These will be initialized by SkCodec if the color type is kIndex8 and
521 // unused otherwise.
522 SkPMColor colors[256];
523 int colorCount;
524 const SkImageInfo info = codec->getInfo().makeColorType(colorType);
halcanary385fe4d2015-08-26 13:07:48 -0700525 SkAutoTDeleteArray<uint8_t> row(new uint8_t[info.minRowBytes()]);
scroggo46c57472015-09-30 08:57:13 -0700526 if (codec->startScanlineDecode(info, nullptr, colors, &colorCount) != SkCodec::kSuccess)
scroggo1c005e42015-08-04 09:24:45 -0700527 {
msarettb23e6aa2015-06-09 13:56:10 -0700528 SkDebugf("Could not create scanline decoder for %s with color type %s. "
msarett7f691442015-09-22 11:56:16 -0700529 "Skipping bench.\n", path.c_str(), color_type_to_str(colorType));
msarettb23e6aa2015-06-09 13:56:10 -0700530 return false;
531 }
532 *width = info.width();
533 *height = info.height();
534 } else {
535 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream));
halcanary96fcdcc2015-08-27 07:41:13 -0700536 if (nullptr == decoder) {
msarettb23e6aa2015-06-09 13:56:10 -0700537 SkDebugf("Could not create decoder for %s. Skipping bench.\n", path.c_str());
538 return false;
539 }
540 //FIXME: See skbug.com/3921
541 if (kIndex_8_SkColorType == colorType || kGray_8_SkColorType == colorType) {
542 SkDebugf("Cannot use image subset decoder for %s with color type %s. "
msarett7f691442015-09-22 11:56:16 -0700543 "Skipping bench.\n", path.c_str(), color_type_to_str(colorType));
msarettb23e6aa2015-06-09 13:56:10 -0700544 return false;
545 }
546 if (!decoder->buildTileIndex(stream.detach(), width, height)) {
547 SkDebugf("Could not build tile index for %s. Skipping bench.\n", path.c_str());
548 return false;
549 }
550 }
msarettab80e352015-06-17 10:28:22 -0700551
552 // Check if the image is large enough for a meaningful subset benchmark.
553 if (*width <= 512 && *height <= 512) {
554 // This should not print a message since it is not an error.
555 return false;
556 }
557
msarettb23e6aa2015-06-09 13:56:10 -0700558 return true;
559}
jcgregoriobf5e5232014-07-17 13:14:16 -0700560
msarett7f691442015-09-22 11:56:16 -0700561static bool valid_brd_bench(SkData* encoded, SkBitmapRegionDecoderInterface::Strategy strategy,
562 SkColorType colorType, uint32_t sampleSize, uint32_t minOutputSize, int* width,
563 int* height) {
564 SkStreamRewindable* stream = new SkMemoryStream(encoded);
565 SkAutoTDelete<SkBitmapRegionDecoderInterface> brd(
566 SkBitmapRegionDecoderInterface::CreateBitmapRegionDecoder(stream, strategy));
567 if (nullptr == brd.get()) {
568 // This is indicates that subset decoding is not supported for a particular image format.
569 return false;
570 }
571
572 SkAutoTDelete<SkBitmap> bitmap(brd->decodeRegion(0, 0, brd->width(), brd->height(), 1,
573 colorType));
574 if (nullptr == bitmap.get() || colorType != bitmap->colorType()) {
575 // This indicates that conversion to the requested color type is not supported for the
576 // particular image.
577 return false;
578 }
579
580 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
581 (uint32_t) brd->height()) {
582 // This indicates that the image is not large enough to decode a
583 // minOutputSize x minOutputSize subset at the given sampleSize.
584 return false;
585 }
586
587 // Set the image width and height. The calling code will use this to choose subsets to decode.
588 *width = brd->width();
589 *height = brd->height();
590 return true;
591}
592
egdaniel3bf92062015-06-26 08:12:46 -0700593static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700594 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700595#if SK_SUPPORT_GPU
596 if (FLAGS_abandonGpuContext) {
597 gGrFactory->abandonContexts();
598 }
599 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
600 gGrFactory->destroyContexts();
601 }
602#endif
603}
604
mtkleine714e752014-07-31 12:13:48 -0700605class BenchmarkStream {
606public:
mtklein92007582014-08-01 07:46:52 -0700607 BenchmarkStream() : fBenches(BenchRegistry::Head())
608 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700609 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700610 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800611 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800612 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700613 , fCurrentCodec(0)
msarett95f192d2015-02-13 09:05:41 -0800614 , fCurrentImage(0)
615 , fCurrentSubsetImage(0)
msarett7f691442015-09-22 11:56:16 -0700616 , fCurrentBRDImage(0)
msarett95f192d2015-02-13 09:05:41 -0800617 , fCurrentColorType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700618 , fCurrentSubsetType(0)
619 , fUseCodec(0)
msarett7f691442015-09-22 11:56:16 -0700620 , fCurrentBRDStrategy(0)
621 , fCurrentBRDSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700622 , fCurrentAnimSKP(0) {
mtklein92007582014-08-01 07:46:52 -0700623 for (int i = 0; i < FLAGS_skps.count(); i++) {
624 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
625 fSKPs.push_back() = FLAGS_skps[i];
626 } else {
627 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
628 SkString path;
629 while (it.next(&path)) {
630 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
631 }
632 }
633 }
mtkleine714e752014-07-31 12:13:48 -0700634
mtklein92007582014-08-01 07:46:52 -0700635 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
636 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
637 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
638 exit(1);
639 }
640
641 for (int i = 0; i < FLAGS_scales.count(); i++) {
642 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
643 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
644 exit(1);
645 }
646 }
robertphillips5b693772014-11-21 06:19:36 -0800647
cdalton63a82852015-06-29 14:06:10 -0700648 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
649 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700650 exit(1);
651 }
652
robertphillips5b693772014-11-21 06:19:36 -0800653 if (FLAGS_mpd) {
654 fUseMPDs.push_back() = true;
655 }
mtkleinc751ecb2015-06-15 08:56:38 -0700656 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700657
msarett95f192d2015-02-13 09:05:41 -0800658 // Prepare the images for decoding
659 for (int i = 0; i < FLAGS_images.count(); i++) {
660 const char* flag = FLAGS_images[i];
661 if (sk_isdir(flag)) {
662 // If the value passed in is a directory, add all the images
663 SkOSFile::Iter it(flag);
664 SkString file;
665 while (it.next(&file)) {
666 fImages.push_back() = SkOSPath::Join(flag, file.c_str());
667 }
668 } else if (sk_exists(flag)) {
669 // Also add the value if it is a single image
670 fImages.push_back() = flag;
671 }
672 }
mtklein95553d92015-03-12 08:24:21 -0700673
msarett95f192d2015-02-13 09:05:41 -0800674 // Choose the candidate color types for image decoding
675 const SkColorType colorTypes[] =
msarettb23e6aa2015-06-09 13:56:10 -0700676 { kN32_SkColorType,
677 kRGB_565_SkColorType,
678 kAlpha_8_SkColorType,
679 kIndex_8_SkColorType,
680 kGray_8_SkColorType };
msarett95f192d2015-02-13 09:05:41 -0800681 fColorTypes.push_back_n(SK_ARRAY_COUNT(colorTypes), colorTypes);
mtklein92007582014-08-01 07:46:52 -0700682 }
683
mtkleinfd731ce2014-09-10 12:19:30 -0700684 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
685 // Not strictly necessary, as it will be checked again later,
686 // but helps to avoid a lot of pointless work if we're going to skip it.
687 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
688 return false;
689 }
690
scroggoa1193e42015-01-21 12:09:53 -0800691 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -0700692 if (stream.get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700693 SkDebugf("Could not read %s.\n", path);
694 return false;
695 }
696
mtklein57f27bd2015-02-09 11:58:41 -0800697 pic->reset(SkPicture::CreateFromStream(stream.get()));
halcanary96fcdcc2015-08-27 07:41:13 -0700698 if (pic->get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700699 SkDebugf("Could not read %s as an SkPicture.\n", path);
700 return false;
701 }
702 return true;
703 }
704
mtklein92007582014-08-01 07:46:52 -0700705 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700706 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700707 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700708 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700709 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700710 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700711 return bench;
712 }
mtklein92007582014-08-01 07:46:52 -0700713
mtkleine714e752014-07-31 12:13:48 -0700714 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -0700715 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700716 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800717 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700718 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700719 fBenchType = "micro";
halcanary385fe4d2015-08-26 13:07:48 -0700720 return new GMBench(gm.detach());
mtkleine714e752014-07-31 12:13:48 -0700721 }
722 }
mtklein92007582014-08-01 07:46:52 -0700723
mtkleinfd731ce2014-09-10 12:19:30 -0700724 // First add all .skps as RecordingBenches.
725 while (fCurrentRecording < fSKPs.count()) {
726 const SkString& path = fSKPs[fCurrentRecording++];
727 SkAutoTUnref<SkPicture> pic;
728 if (!ReadPicture(path.c_str(), &pic)) {
729 continue;
730 }
731 SkString name = SkOSPath::Basename(path.c_str());
732 fSourceType = "skp";
733 fBenchType = "recording";
bsalomon0aa5cea2014-12-15 09:13:35 -0800734 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic));
mtklein051e56d2014-12-04 08:46:51 -0800735 fSKPOps = pic->approximateOpCount();
halcanary385fe4d2015-08-26 13:07:48 -0700736 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700737 }
738
739 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700740 while (fCurrentScale < fScales.count()) {
741 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800742 const SkString& path = fSKPs[fCurrentSKP];
mtkleinfd731ce2014-09-10 12:19:30 -0700743 SkAutoTUnref<SkPicture> pic;
744 if (!ReadPicture(path.c_str(), &pic)) {
robertphillips5b693772014-11-21 06:19:36 -0800745 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700746 continue;
747 }
robertphillips5b693772014-11-21 06:19:36 -0800748
749 while (fCurrentUseMPD < fUseMPDs.count()) {
750 if (FLAGS_bbh) {
751 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
752 SkRTreeFactory factory;
753 SkPictureRecorder recorder;
754 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
755 pic->playback(recorder.beginRecording(pic->cullRect().width(),
756 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800757 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800758 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
robertphillips5b693772014-11-21 06:19:36 -0800759 pic.reset(recorder.endRecording());
760 }
761 SkString name = SkOSPath::Basename(path.c_str());
762 fSourceType = "skp";
763 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700764 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
765 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700766 }
robertphillips5b693772014-11-21 06:19:36 -0800767 fCurrentUseMPD = 0;
768 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700769 }
770 fCurrentSKP = 0;
771 fCurrentScale++;
772 }
773
joshualitt261c3ad2015-04-27 09:16:57 -0700774 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700775 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700776 while (fCurrentAnimSKP < fSKPs.count()) {
777 const SkString& path = fSKPs[fCurrentAnimSKP];
778 SkAutoTUnref<SkPicture> pic;
779 if (!ReadPicture(path.c_str(), &pic)) {
780 fCurrentAnimSKP++;
781 continue;
782 }
783
784 fCurrentAnimSKP++;
785 SkString name = SkOSPath::Basename(path.c_str());
cdalton63a82852015-06-29 14:06:10 -0700786 SkAutoTUnref<SKPAnimationBench::Animation> animation(
787 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
halcanary385fe4d2015-08-26 13:07:48 -0700788 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation,
789 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700790 }
791 }
792
793
scroggo60869a42015-04-01 12:09:17 -0700794 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
795 const SkString& path = fImages[fCurrentCodec];
796 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
797 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700798 if (!codec) {
799 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700800 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700801 continue;
802 }
scroggo21027992015-04-02 13:22:38 -0700803
scroggo60869a42015-04-01 12:09:17 -0700804 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700805 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo60869a42015-04-01 12:09:17 -0700806 fCurrentColorType++;
scroggo21027992015-04-02 13:22:38 -0700807
scroggo60869a42015-04-01 12:09:17 -0700808 // Make sure we can decode to this color type.
scroggo60869a42015-04-01 12:09:17 -0700809 SkImageInfo info = codec->getInfo().makeColorType(colorType);
scroggo21027992015-04-02 13:22:38 -0700810 SkAlphaType alphaType;
811 if (!SkColorTypeValidateAlphaType(colorType, info.alphaType(),
812 &alphaType)) {
813 continue;
814 }
815 if (alphaType != info.alphaType()) {
816 info = info.makeAlphaType(alphaType);
817 }
818
819 const size_t rowBytes = info.minRowBytes();
820 SkAutoMalloc storage(info.getSafeSize(rowBytes));
821
822 // Used if fCurrentColorType is kIndex_8_SkColorType
823 int colorCount = 256;
824 SkPMColor colors[256];
825
scroggoeb602a52015-07-09 08:16:03 -0700826 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700827 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700828 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700829 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700830 case SkCodec::kSuccess:
831 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700832 return new CodecBench(SkOSPath::Basename(path.c_str()),
833 encoded, colorType);
scroggoeb602a52015-07-09 08:16:03 -0700834 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700835 // This is okay. Not all conversions are valid.
836 break;
scroggo60869a42015-04-01 12:09:17 -0700837 default:
838 // This represents some sort of failure.
839 SkASSERT(false);
840 break;
841 }
842 }
843 fCurrentColorType = 0;
844 }
845
msarett95f192d2015-02-13 09:05:41 -0800846 // Run the DecodingBenches
847 while (fCurrentImage < fImages.count()) {
848 while (fCurrentColorType < fColorTypes.count()) {
849 const SkString& path = fImages[fCurrentImage];
850 SkColorType colorType = fColorTypes[fCurrentColorType];
851 fCurrentColorType++;
scroggo60869a42015-04-01 12:09:17 -0700852 // Check if the image decodes to the right color type
853 // before creating the benchmark
msarett95f192d2015-02-13 09:05:41 -0800854 SkBitmap bitmap;
855 if (SkImageDecoder::DecodeFile(path.c_str(), &bitmap,
scroggo60869a42015-04-01 12:09:17 -0700856 colorType, SkImageDecoder::kDecodePixels_Mode)
857 && bitmap.colorType() == colorType) {
msarett95f192d2015-02-13 09:05:41 -0800858 return new DecodingBench(path, colorType);
859 }
860 }
861 fCurrentColorType = 0;
862 fCurrentImage++;
863 }
864
msarettb23e6aa2015-06-09 13:56:10 -0700865 // Run the SubsetBenches
866 bool useCodecOpts[] = { true, false };
867 while (fUseCodec < 2) {
868 bool useCodec = useCodecOpts[fUseCodec];
869 while (fCurrentSubsetImage < fImages.count()) {
870 while (fCurrentColorType < fColorTypes.count()) {
871 const SkString& path = fImages[fCurrentSubsetImage];
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, useCodec, &width, &height)) {
878 switch (currentSubsetType) {
879 case kTopLeft_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700880 return new SubsetSingleBench(path, colorType, width/3,
881 height/3, 0, 0, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700882 case kTopRight_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700883 return new SubsetSingleBench(path, colorType, width/3,
884 height/3, 2*width/3, 0, useCodec);
885 case kMiddle_SubsetType:
886 return new SubsetSingleBench(path, colorType, width/3,
887 height/3, width/3, height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700888 case kBottomLeft_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700889 return new SubsetSingleBench(path, colorType, width/3,
890 height/3, 0, 2*height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700891 case kBottomRight_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700892 return new SubsetSingleBench(path, colorType, width/3,
893 height/3, 2*width/3, 2*height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700894 case kTranslate_SubsetType:
895 return new SubsetTranslateBench(path, colorType, 512, 512,
896 useCodec);
897 case kZoom_SubsetType:
898 return new SubsetZoomBench(path, colorType, 512, 512,
899 useCodec);
msarett95f192d2015-02-13 09:05:41 -0800900 }
msarettb23e6aa2015-06-09 13:56:10 -0700901 } else {
902 break;
msarett95f192d2015-02-13 09:05:41 -0800903 }
904 }
msarettb23e6aa2015-06-09 13:56:10 -0700905 fCurrentSubsetType = 0;
906 fCurrentColorType++;
msarett95f192d2015-02-13 09:05:41 -0800907 }
msarettb23e6aa2015-06-09 13:56:10 -0700908 fCurrentColorType = 0;
909 fCurrentSubsetImage++;
msarett95f192d2015-02-13 09:05:41 -0800910 }
msarettb23e6aa2015-06-09 13:56:10 -0700911 fCurrentSubsetImage = 0;
912 fUseCodec++;
msarett95f192d2015-02-13 09:05:41 -0800913 }
914
msarett7f691442015-09-22 11:56:16 -0700915 // Run the BRDBenches
916 // We will benchmark multiple BRD strategies.
917 const SkBitmapRegionDecoderInterface::Strategy strategies[] = {
918 SkBitmapRegionDecoderInterface::kOriginal_Strategy,
919 SkBitmapRegionDecoderInterface::kCanvas_Strategy,
920 };
921
922 // We intend to create benchmarks that model the use cases in
923 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
924 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
925 // the image. For that reason, we will benchmark decodes in five representative locations
926 // in the image. Additionally, this use case utilizes power of two scaling, so we will
927 // test on power of two sample sizes. The output tile is always 512x512, so, when a
928 // sampleSize is used, the size of the subset that is decoded is always
929 // (sampleSize*512)x(sampleSize*512).
930 // There are a few good reasons to only test on power of two sample sizes at this time:
931 // JPEG decodes using kOriginal_Strategy are broken for non-powers of two.
932 // skbug.com/4319
933 // All use cases we are aware of only scale by powers of two.
934 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
935 // these tests are sufficient to provide good coverage of our scaling options.
936 const uint32_t sampleSizes[] = { 1, 2, 4, 8, 16 };
937 const uint32_t minOutputSize = 512;
938 while (fCurrentBRDImage < fImages.count()) {
939 while (fCurrentBRDStrategy < (int) SK_ARRAY_COUNT(strategies)) {
940 while (fCurrentColorType < fColorTypes.count()) {
941 while (fCurrentBRDSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
942 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
943 const SkString& path = fImages[fCurrentBRDImage];
944 const SkBitmapRegionDecoderInterface::Strategy strategy =
945 strategies[fCurrentBRDStrategy];
946 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
947 const SkColorType colorType = fColorTypes[fCurrentColorType];
948 uint32_t sampleSize = sampleSizes[fCurrentBRDSampleSize];
949 int currentSubsetType = fCurrentSubsetType++;
950
951 int width = 0;
952 int height = 0;
953 if (!valid_brd_bench(encoded.get(), strategy, colorType, sampleSize,
954 minOutputSize, &width, &height)) {
955 break;
956 }
957
958 SkString basename = SkOSPath::Basename(path.c_str());
959 SkIRect subset;
960 const uint32_t subsetSize = sampleSize * minOutputSize;
961 switch (currentSubsetType) {
962 case kTopLeft_SubsetType:
963 basename.append("_TopLeft");
964 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
965 break;
966 case kTopRight_SubsetType:
967 basename.append("_TopRight");
968 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
969 subsetSize);
970 break;
971 case kMiddle_SubsetType:
972 basename.append("_Middle");
973 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
974 (height - subsetSize) / 2, subsetSize, subsetSize);
975 break;
976 case kBottomLeft_SubsetType:
977 basename.append("_BottomLeft");
978 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
979 subsetSize);
980 break;
981 case kBottomRight_SubsetType:
982 basename.append("_BottomRight");
983 subset = SkIRect::MakeXYWH(width - subsetSize,
984 height - subsetSize, subsetSize, subsetSize);
985 break;
986 default:
987 SkASSERT(false);
988 }
989
990 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
991 strategy, colorType, sampleSize, subset);
992 }
993 fCurrentSubsetType = 0;
994 fCurrentBRDSampleSize++;
995 }
996 fCurrentBRDSampleSize = 0;
997 fCurrentColorType++;
998 }
999 fCurrentColorType = 0;
1000 fCurrentBRDStrategy++;
1001 }
1002 fCurrentBRDStrategy = 0;
1003 fCurrentBRDImage++;
1004 }
1005
halcanary96fcdcc2015-08-27 07:41:13 -07001006 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001007 }
mtklein92007582014-08-01 07:46:52 -07001008
1009 void fillCurrentOptions(ResultsWriter* log) const {
1010 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001011 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001012 if (0 == strcmp(fSourceType, "skp")) {
1013 log->configOption("clip",
1014 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1015 fClip.fRight, fClip.fBottom).c_str());
1016 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001017 if (fCurrentUseMPD > 0) {
1018 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1019 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1020 }
mtklein92007582014-08-01 07:46:52 -07001021 }
mtklein051e56d2014-12-04 08:46:51 -08001022 if (0 == strcmp(fBenchType, "recording")) {
1023 log->metric("bytes", fSKPBytes);
1024 log->metric("ops", fSKPOps);
1025 }
mtklein92007582014-08-01 07:46:52 -07001026 }
1027
mtkleine714e752014-07-31 12:13:48 -07001028private:
msarettb23e6aa2015-06-09 13:56:10 -07001029 enum SubsetType {
1030 kTopLeft_SubsetType = 0,
1031 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001032 kMiddle_SubsetType = 2,
1033 kBottomLeft_SubsetType = 3,
1034 kBottomRight_SubsetType = 4,
1035 kTranslate_SubsetType = 5,
1036 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001037 kLast_SubsetType = kZoom_SubsetType,
1038 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001039 };
1040
mtkleine714e752014-07-31 12:13:48 -07001041 const BenchRegistry* fBenches;
1042 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001043 SkIRect fClip;
1044 SkTArray<SkScalar> fScales;
1045 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -08001046 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001047 SkTArray<SkString> fImages;
1048 SkTArray<SkColorType> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001049 SkScalar fZoomMax;
1050 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001051
mtklein051e56d2014-12-04 08:46:51 -08001052 double fSKPBytes, fSKPOps;
1053
mtkleinfd731ce2014-09-10 12:19:30 -07001054 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1055 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1056 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -07001057 int fCurrentScale;
1058 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -08001059 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001060 int fCurrentCodec;
msarett95f192d2015-02-13 09:05:41 -08001061 int fCurrentImage;
1062 int fCurrentSubsetImage;
msarett7f691442015-09-22 11:56:16 -07001063 int fCurrentBRDImage;
msarett95f192d2015-02-13 09:05:41 -08001064 int fCurrentColorType;
msarettb23e6aa2015-06-09 13:56:10 -07001065 int fCurrentSubsetType;
1066 int fUseCodec;
msarett7f691442015-09-22 11:56:16 -07001067 int fCurrentBRDStrategy;
1068 int fCurrentBRDSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001069 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001070};
1071
jcgregorio3b27ade2014-11-13 08:06:40 -08001072int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001073int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001074 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001075 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001076 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001077
krajcevski69a55602014-08-13 10:46:31 -07001078#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001079 GrContextOptions grContextOpts;
krajcevski12b35442014-08-13 12:06:26 -07001080 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
halcanary385fe4d2015-08-26 13:07:48 -07001081 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001082#endif
1083
bsalomon06cddec2014-10-24 10:40:50 -07001084 if (FLAGS_veryVerbose) {
1085 FLAGS_verbose = true;
1086 }
1087
cdaltone1b89582015-06-25 19:17:08 -07001088 double samplingTimeMs = 0;
1089 if (0 != strcmp("0", FLAGS_samplingTime[0])) {
1090 SkSTArray<8, char> timeUnit;
1091 timeUnit.push_back_n(static_cast<int>(strlen(FLAGS_samplingTime[0])) + 1);
1092 if (2 != sscanf(FLAGS_samplingTime[0], "%lf%s", &samplingTimeMs, timeUnit.begin()) ||
1093 (0 != strcmp("s", timeUnit.begin()) && 0 != strcmp("ms", timeUnit.begin()))) {
1094 SkDebugf("Invalid --samplingTime \"%s\". Must be \"0\", \"%%lfs\", or \"%%lfms\"\n",
1095 FLAGS_samplingTime[0]);
1096 exit(0);
1097 }
1098 if (0 == strcmp("s", timeUnit.begin())) {
1099 samplingTimeMs *= 1000;
1100 }
1101 if (samplingTimeMs) {
1102 FLAGS_samples = kTimedSampling;
1103 }
1104 }
1105
bsalomon6eb03cc2014-08-07 14:28:50 -07001106 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001107 FLAGS_samples = 1;
1108 FLAGS_gpuFrameLag = 0;
1109 }
1110
bsalomon6eb03cc2014-08-07 14:28:50 -07001111 if (!FLAGS_writePath.isEmpty()) {
1112 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1113 if (!sk_mkdir(FLAGS_writePath[0])) {
1114 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001115 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001116 }
1117 }
1118
halcanary385fe4d2015-08-26 13:07:48 -07001119 SkAutoTDelete<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001120 if (!FLAGS_outResultsFile.isEmpty()) {
halcanary385fe4d2015-08-26 13:07:48 -07001121 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein60317d0f2014-07-14 11:30:37 -07001122 }
mtklein1915b622014-08-20 11:45:00 -07001123
1124 if (1 == FLAGS_properties.count() % 2) {
1125 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1126 return 1;
1127 }
1128 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1129 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1130 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001131
1132 if (1 == FLAGS_key.count() % 2) {
1133 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1134 return 1;
1135 }
1136 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001137 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001138 }
mtklein60317d0f2014-07-14 11:30:37 -07001139
mtkleinf3723212014-06-25 14:08:00 -07001140 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001141 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001142
cdaltone1b89582015-06-25 19:17:08 -07001143 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001144
bsalomon6eb03cc2014-08-07 14:28:50 -07001145 if (kAutoTuneLoops != FLAGS_loops) {
1146 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001147 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -07001148 SkDebugf("median\tbench\tconfig\n");
cdaltone1b89582015-06-25 19:17:08 -07001149 } else if (kTimedSampling == FLAGS_samples) {
1150 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001151 } else {
mtkleind75c4662015-04-30 07:11:22 -07001152 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001153 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001154 }
1155
bsalomonc2553372014-07-22 13:09:05 -07001156 SkTDArray<Config> configs;
1157 create_configs(&configs);
1158
mtkleine070c2b2014-10-14 08:40:43 -07001159 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001160 BenchmarkStream benchStream;
1161 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001162 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001163 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001164 continue;
1165 }
1166
egdaniel3bf92062015-06-26 08:12:46 -07001167 if (!configs.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -07001168 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001169 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001170 }
egdaniel3bf92062015-06-26 08:12:46 -07001171 for (int i = 0; i < configs.count(); ++i) {
1172 Target* target = is_enabled(b, configs[i]);
1173 if (!target) {
1174 continue;
1175 }
mtkleinf3723212014-06-25 14:08:00 -07001176
halcanary96fcdcc2015-08-27 07:41:13 -07001177 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001178 SkCanvas* canvas = target->getCanvas();
1179 const char* config = target->config.name;
1180
1181 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001182 bench->perCanvasPreDraw(canvas);
1183
cdaltone1b89582015-06-25 19:17:08 -07001184 int maxFrameLag;
egdaniel3bf92062015-06-26 08:12:46 -07001185 const int loops = target->needsFrameTiming(&maxFrameLag)
1186 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1187 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001188
1189 if (kTimedSampling != FLAGS_samples) {
1190 samples.reset(FLAGS_samples);
1191 for (int s = 0; s < FLAGS_samples; s++) {
egdaniel3bf92062015-06-26 08:12:46 -07001192 samples[s] = time(loops, bench, target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001193 }
1194 } else if (samplingTimeMs) {
1195 samples.reset();
1196 if (FLAGS_verbose) {
1197 SkDebugf("Begin sampling %s for %ims\n",
1198 bench->getUniqueName(), static_cast<int>(samplingTimeMs));
1199 }
1200 WallTimer timer;
1201 timer.start();
1202 do {
egdaniel3bf92062015-06-26 08:12:46 -07001203 samples.push_back(time(loops, bench, target) / loops);
cdaltone1b89582015-06-25 19:17:08 -07001204 timer.end();
1205 } while (timer.fWall < samplingTimeMs);
1206 }
mtkleinf3723212014-06-25 14:08:00 -07001207
robertphillips5b693772014-11-21 06:19:36 -08001208 bench->perCanvasPostDraw(canvas);
1209
egdaniel3bf92062015-06-26 08:12:46 -07001210 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001211 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001212 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001213 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001214 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001215 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001216 }
1217
1218 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001219 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001220 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001221 continue;
1222 }
1223
cdaltone1b89582015-06-25 19:17:08 -07001224 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001225 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001226 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001227 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001228 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001229 log->metric("min_ms", stats.min);
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
cdaltone1b89582015-06-25 19:17:08 -07001259 , kTimedSampling != FLAGS_samples ? stats.plot.c_str()
1260 : to_string(samples.count()).c_str()
mtkleinf3723212014-06-25 14:08:00 -07001261 , config
mtklein96289052014-09-10 12:05:59 -07001262 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001263 );
1264 }
bsalomonb12ea412015-02-02 21:19:50 -08001265#if SK_SUPPORT_GPU
1266 if (FLAGS_gpuStats &&
egdaniel3bf92062015-06-26 08:12:46 -07001267 Benchmark::kGPU_Backend == configs[i].backend) {
1268 gGrFactory->get(configs[i].ctxType)->printCacheStats();
1269 gGrFactory->get(configs[i].ctxType)->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001270 }
1271#endif
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