blob: 38f554dea447103975dc01ee32fededd0f3ef1fa [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?");
scroggo2da1a852015-10-13 13:33:33 -0700114DEFINE_bool(pngBuildTileIndex, false, "If supported, use png buildTileIndex/decodeSubset.");
scroggo860e8a62015-10-15 07:51:28 -0700115DEFINE_bool(jpgBuildTileIndex, false, "If supported, use jpg buildTileIndex/decodeSubset.");
mtklein92007582014-08-01 07:46:52 -0700116
mtkleinf3723212014-06-25 14:08:00 -0700117static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700118 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800119 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700120}
mtklein55b0ffc2014-07-17 08:38:23 -0700121#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700122
tomhudsond968a6f2015-03-26 11:28:06 -0700123bool Target::init(SkImageInfo info, Benchmark* bench) {
124 if (Benchmark::kRaster_Backend == config.backend) {
125 this->surface.reset(SkSurface::NewRaster(info));
126 if (!this->surface.get()) {
127 return false;
128 }
129 }
130 return true;
131}
132bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700133 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700134 if (!canvas) {
135 return false;
136 }
137 bmp->setInfo(canvas->imageInfo());
138 if (!canvas->readPixels(bmp, 0, 0)) {
139 SkDebugf("Can't read canvas pixels.\n");
140 return false;
141 }
142 return true;
143}
144
145#if SK_SUPPORT_GPU
146struct GPUTarget : public Target {
halcanary96fcdcc2015-08-27 07:41:13 -0700147 explicit GPUTarget(const Config& c) : Target(c), gl(nullptr) { }
tomhudsond968a6f2015-03-26 11:28:06 -0700148 SkGLContext* gl;
149
150 void setup() override {
151 this->gl->makeCurrent();
152 // Make sure we're done with whatever came before.
153 SK_GL(*this->gl, Finish());
154 }
155 void endTiming() override {
156 if (this->gl) {
157 SK_GL(*this->gl, Flush());
158 this->gl->swapBuffers();
159 }
160 }
161 void fence() override {
162 SK_GL(*this->gl, Finish());
163 }
mtkleind75c4662015-04-30 07:11:22 -0700164
cdaltond416a5b2015-06-23 13:23:44 -0700165 bool needsFrameTiming(int* maxFrameLag) const override {
166 if (!this->gl->getMaxGpuFrameLag(maxFrameLag)) {
167 // Frame lag is unknown.
168 *maxFrameLag = FLAGS_gpuFrameLag;
169 }
170 return true;
171 }
tomhudsond968a6f2015-03-26 11:28:06 -0700172 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700173 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
174 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700175 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
176 this->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(this->config.ctxType),
177 SkSurface::kNo_Budgeted, info,
178 this->config.samples, &props));
179 this->gl = gGrFactory->getGLContext(this->config.ctxType);
180 if (!this->surface.get()) {
181 return false;
182 }
cdaltond416a5b2015-06-23 13:23:44 -0700183 if (!this->gl->fenceSyncSupport()) {
184 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
185 "Timings might not be accurate.\n", this->config.name);
186 }
tomhudsond968a6f2015-03-26 11:28:06 -0700187 return true;
188 }
189 void fillOptions(ResultsWriter* log) override {
190 const GrGLubyte* version;
191 SK_GL_RET(*this->gl, version, GetString(GR_GL_VERSION));
192 log->configOption("GL_VERSION", (const char*)(version));
193
194 SK_GL_RET(*this->gl, version, GetString(GR_GL_RENDERER));
195 log->configOption("GL_RENDERER", (const char*) version);
196
197 SK_GL_RET(*this->gl, version, GetString(GR_GL_VENDOR));
198 log->configOption("GL_VENDOR", (const char*) version);
199
200 SK_GL_RET(*this->gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
201 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
202 }
203};
mtkleind75c4662015-04-30 07:11:22 -0700204
tomhudsond968a6f2015-03-26 11:28:06 -0700205#endif
206
tomhudson75a0ebb2015-03-27 12:11:44 -0700207static double time(int loops, Benchmark* bench, Target* target) {
208 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700209 if (canvas) {
210 canvas->clear(SK_ColorWHITE);
211 }
joshualitt8a6697a2015-09-30 12:11:07 -0700212 bench->preDraw(canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700213 WallTimer timer;
214 timer.start();
tomhudson75a0ebb2015-03-27 12:11:44 -0700215 canvas = target->beginTiming(canvas);
216 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700217 if (canvas) {
218 canvas->flush();
219 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700220 target->endTiming();
mtkleinbb6a0282014-07-01 08:43:42 -0700221 timer.end();
joshualitt8a6697a2015-09-30 12:11:07 -0700222 bench->postDraw(canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700223 return timer.fWall;
224}
225
mtkleinf3723212014-06-25 14:08:00 -0700226static double estimate_timer_overhead() {
227 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700228 for (int i = 0; i < FLAGS_overheadLoops; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700229 WallTimer timer;
230 timer.start();
231 timer.end();
232 overhead += timer.fWall;
mtkleinf3723212014-06-25 14:08:00 -0700233 }
234 return overhead / FLAGS_overheadLoops;
235}
236
reed53249782014-10-10 09:09:52 -0700237static int detect_forever_loops(int loops) {
238 // look for a magic run-forever value
239 if (loops < 0) {
240 loops = SK_MaxS32;
241 }
242 return loops;
243}
244
mtklein55b0ffc2014-07-17 08:38:23 -0700245static int clamp_loops(int loops) {
246 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800247 SkDebugf("ERROR: clamping loops from %d to 1. "
248 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700249 return 1;
250 }
251 if (loops > FLAGS_maxLoops) {
252 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
253 return FLAGS_maxLoops;
254 }
255 return loops;
256}
257
tomhudsond968a6f2015-03-26 11:28:06 -0700258static bool write_canvas_png(Target* target, const SkString& filename) {
259
bsalomon6eb03cc2014-08-07 14:28:50 -0700260 if (filename.isEmpty()) {
261 return false;
262 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700263 if (target->getCanvas() &&
264 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700265 return false;
266 }
tomhudsond968a6f2015-03-26 11:28:06 -0700267
bsalomon6eb03cc2014-08-07 14:28:50 -0700268 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700269
270 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700271 return false;
272 }
tomhudsond968a6f2015-03-26 11:28:06 -0700273
bsalomon6eb03cc2014-08-07 14:28:50 -0700274 SkString dir = SkOSPath::Dirname(filename.c_str());
275 if (!sk_mkdir(dir.c_str())) {
276 SkDebugf("Can't make dir %s.\n", dir.c_str());
277 return false;
278 }
279 SkFILEWStream stream(filename.c_str());
280 if (!stream.isValid()) {
281 SkDebugf("Can't write %s.\n", filename.c_str());
282 return false;
283 }
284 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
285 SkDebugf("Can't encode a PNG.\n");
286 return false;
287 }
288 return true;
289}
290
291static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700292static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700293 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700294 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700295 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700296 int loops = bench->calculateLoops(FLAGS_loops);
297 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700298 while (bench_plus_overhead < overhead) {
299 if (round++ == FLAGS_maxCalibrationAttempts) {
300 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700301 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700302 return kFailedLoops;
303 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700304 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700305 }
mtklein2069e222014-08-04 13:57:39 -0700306 }
mtkleinf3723212014-06-25 14:08:00 -0700307
mtkleinbb6a0282014-07-01 08:43:42 -0700308 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700309 // We'll pick N to make timer overhead negligible:
310 //
mtkleinbb6a0282014-07-01 08:43:42 -0700311 // overhead
312 // ------------------------- < FLAGS_overheadGoal
313 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700314 //
mtkleinbb6a0282014-07-01 08:43:42 -0700315 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700316 //
317 // Doing some math, we get:
318 //
mtkleinbb6a0282014-07-01 08:43:42 -0700319 // (overhead / FLAGS_overheadGoal) - overhead
320 // ------------------------------------------ < N
321 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700322 //
323 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700324 if (kAutoTuneLoops == loops) {
325 const double numer = overhead / FLAGS_overheadGoal - overhead;
326 const double denom = bench_plus_overhead - overhead;
327 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700328 loops = clamp_loops(loops);
329 } else {
330 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700331 }
mtkleinbb6a0282014-07-01 08:43:42 -0700332
mtkleinbb6a0282014-07-01 08:43:42 -0700333 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700334}
335
cdaltone1b89582015-06-25 19:17:08 -0700336static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700337 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700338 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700339 if (kAutoTuneLoops == loops) {
340 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700341 double elapsed = 0;
342 do {
mtklein527930f2014-11-06 08:04:34 -0800343 if (1<<30 == loops) {
344 // We're about to wrap. Something's wrong with the bench.
345 loops = 0;
346 break;
347 }
mtkleina189ccd2014-07-14 12:28:47 -0700348 loops *= 2;
349 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700350 // _this_ round, not still timing last round.
351 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700352 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700353 }
354 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700355
mtkleina189ccd2014-07-14 12:28:47 -0700356 // We've overshot at least a little. Scale back linearly.
357 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700358 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700359
tomhudsond968a6f2015-03-26 11:28:06 -0700360 // Make sure we're not still timing our calibration.
361 target->fence();
reed53249782014-10-10 09:09:52 -0700362 } else {
363 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700364 }
mtkleinbb6a0282014-07-01 08:43:42 -0700365
366 // Pretty much the same deal as the calibration: do some warmup to make
367 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700368 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700369 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700370 }
mtkleinbb6a0282014-07-01 08:43:42 -0700371
mtkleinbb6a0282014-07-01 08:43:42 -0700372 return loops;
373}
mtkleinbb6a0282014-07-01 08:43:42 -0700374
375static SkString to_lower(const char* str) {
376 SkString lower(str);
377 for (size_t i = 0; i < lower.size(); i++) {
378 lower[i] = tolower(lower[i]);
379 }
380 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700381}
382
bsalomonc2553372014-07-22 13:09:05 -0700383static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700384 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700385 if (to_lower(FLAGS_config[i]).equals(name)) {
386 return true;
mtkleinf3723212014-06-25 14:08:00 -0700387 }
388 }
bsalomonc2553372014-07-22 13:09:05 -0700389 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700390}
391
bsalomonc2553372014-07-22 13:09:05 -0700392#if SK_SUPPORT_GPU
393static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
394 int sampleCnt) {
395 if (!is_cpu_config_allowed(name)) {
396 return false;
397 }
krajcevski69a55602014-08-13 10:46:31 -0700398 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomon76228632015-05-29 08:02:10 -0700399 return sampleCnt <= ctx->caps()->maxSampleCount();
bsalomonc2553372014-07-22 13:09:05 -0700400 }
401 return false;
402}
403#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700404
bsalomonc2553372014-07-22 13:09:05 -0700405#if SK_SUPPORT_GPU
406#define kBogusGLContextType GrContextFactory::kNative_GLContextType
407#else
408#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700409#endif
bsalomonc2553372014-07-22 13:09:05 -0700410
411// Append all configs that are enabled and supported.
412static void create_configs(SkTDArray<Config>* configs) {
jvanverth4736e142014-11-07 07:12:46 -0800413 #define CPU_CONFIG(name, backend, color, alpha) \
414 if (is_cpu_config_allowed(#name)) { \
415 Config config = { #name, Benchmark::backend, color, alpha, 0, \
416 kBogusGLContextType, false }; \
417 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700418 }
mtkleine714e752014-07-31 12:13:48 -0700419
mtklein40b32be2014-07-09 08:46:49 -0700420 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700421 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
422 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
423 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700424 }
mtkleinbb6a0282014-07-01 08:43:42 -0700425
426#if SK_SUPPORT_GPU
jvanverth4736e142014-11-07 07:12:46 -0800427 #define GPU_CONFIG(name, ctxType, samples, useDFText) \
bsalomonc2553372014-07-22 13:09:05 -0700428 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
429 Config config = { \
430 #name, \
431 Benchmark::kGPU_Backend, \
432 kN32_SkColorType, \
433 kPremul_SkAlphaType, \
434 samples, \
jvanverth4736e142014-11-07 07:12:46 -0800435 GrContextFactory::ctxType, \
436 useDFText }; \
bsalomonc2553372014-07-22 13:09:05 -0700437 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700438 }
mtkleine714e752014-07-31 12:13:48 -0700439
mtklein40b32be2014-07-09 08:46:49 -0700440 if (FLAGS_gpu) {
jvanverth4736e142014-11-07 07:12:46 -0800441 GPU_CONFIG(gpu, kNative_GLContextType, 0, false)
442 GPU_CONFIG(msaa4, kNative_GLContextType, 4, false)
443 GPU_CONFIG(msaa16, kNative_GLContextType, 16, false)
444 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4, false)
445 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16, false)
446 GPU_CONFIG(gpudft, kNative_GLContextType, 0, true)
447 GPU_CONFIG(debug, kDebug_GLContextType, 0, false)
448 GPU_CONFIG(nullgpu, kNull_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700449#ifdef SK_ANGLE
jvanverth4736e142014-11-07 07:12:46 -0800450 GPU_CONFIG(angle, kANGLE_GLContextType, 0, false)
hendrikweddbefb2015-09-11 13:07:29 -0700451 GPU_CONFIG(angle-gl, kANGLE_GL_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700452#endif
hendrikw885bf092015-08-27 10:38:39 -0700453#ifdef SK_COMMAND_BUFFER
454 GPU_CONFIG(commandbuffer, kCommandBuffer_GLContextType, 0, false)
455#endif
cdaltond416a5b2015-06-23 13:23:44 -0700456#if SK_MESA
457 GPU_CONFIG(mesa, kMESA_GLContextType, 0, false)
458#endif
mtklein40b32be2014-07-09 08:46:49 -0700459 }
mtkleinbb6a0282014-07-01 08:43:42 -0700460#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700461
462#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
463 if (is_cpu_config_allowed("hwui")) {
464 Config config = { "hwui", Benchmark::kHWUI_Backend, kRGBA_8888_SkColorType,
465 kPremul_SkAlphaType, 0, kBogusGLContextType, false };
466 configs->push(config);
467 }
468#endif
mtkleinf3723212014-06-25 14:08:00 -0700469}
470
halcanary96fcdcc2015-08-27 07:41:13 -0700471// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700472static Target* is_enabled(Benchmark* bench, const Config& config) {
473 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700474 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700475 }
476
reede5ea5002014-09-03 11:54:58 -0700477 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
478 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700479
halcanary96fcdcc2015-08-27 07:41:13 -0700480 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700481
tomhudsond968a6f2015-03-26 11:28:06 -0700482 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700483#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700484 case Benchmark::kGPU_Backend:
485 target = new GPUTarget(config);
486 break;
bsalomonc2553372014-07-22 13:09:05 -0700487#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700488#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
489 case Benchmark::kHWUI_Backend:
490 target = new HWUITarget(config, bench);
491 break;
492#endif
493 default:
494 target = new Target(config);
495 break;
496 }
bsalomonc2553372014-07-22 13:09:05 -0700497
tomhudsond968a6f2015-03-26 11:28:06 -0700498 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700499 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700500 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700501 }
502 return target;
503}
504
msarettb23e6aa2015-06-09 13:56:10 -0700505/*
scroggoab12c272015-10-08 12:01:39 -0700506 * We only run our subset benches on files that are supported by BitmapRegionDecoder:
507 * i.e. PNG, JPEG, and WEBP. We do *not* test WEBP when using codec, since we do not
508 * have a scanline decoder for WEBP, which is necessary for running the subset bench.
509 * (Another bench must be used to test WEBP, which decodes subsets natively.)
510 */
511static bool run_subset_bench(const SkString& path, bool useCodec) {
512 static const char* const exts[] = {
scroggo2da1a852015-10-13 13:33:33 -0700513 "jpg", "jpeg",
514 "JPG", "JPEG",
scroggoab12c272015-10-08 12:01:39 -0700515 };
516
scroggo860e8a62015-10-15 07:51:28 -0700517 if (useCodec || FLAGS_jpgBuildTileIndex) {
518 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
519 if (path.endsWith(exts[i])) {
520 return true;
521 }
scroggoab12c272015-10-08 12:01:39 -0700522 }
523 }
524
scroggo2da1a852015-10-13 13:33:33 -0700525 // Test png in SkCodec, and optionally on SkImageDecoder. SkImageDecoder is
526 // disabled by default because it leaks memory.
527 // skbug.com/4360
528 if ((useCodec || FLAGS_pngBuildTileIndex) && (path.endsWith("png") || path.endsWith("PNG"))) {
529 return true;
530 }
531
532 if (!useCodec && (path.endsWith("webp") || path.endsWith("WEBP"))) {
533 return true;
534 }
535
536 return false;
scroggoab12c272015-10-08 12:01:39 -0700537}
538
539/*
msarettb23e6aa2015-06-09 13:56:10 -0700540 * Returns true if set up for a subset decode succeeds, false otherwise
541 * If the set-up succeeds, the width and height parameters will be set
542 */
543static bool valid_subset_bench(const SkString& path, SkColorType colorType, bool useCodec,
544 int* width, int* height) {
545 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
546 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded));
547
msarettab80e352015-06-17 10:28:22 -0700548 // Check that we can create a codec or image decoder.
msarettb23e6aa2015-06-09 13:56:10 -0700549 if (useCodec) {
550 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
halcanary96fcdcc2015-08-27 07:41:13 -0700551 if (nullptr == codec) {
msarettb23e6aa2015-06-09 13:56:10 -0700552 SkDebugf("Could not create codec for %s. Skipping bench.\n", path.c_str());
553 return false;
554 }
555
556 // These will be initialized by SkCodec if the color type is kIndex8 and
557 // unused otherwise.
558 SkPMColor colors[256];
559 int colorCount;
560 const SkImageInfo info = codec->getInfo().makeColorType(colorType);
scroggo46c57472015-09-30 08:57:13 -0700561 if (codec->startScanlineDecode(info, nullptr, colors, &colorCount) != SkCodec::kSuccess)
scroggo1c005e42015-08-04 09:24:45 -0700562 {
msarettb23e6aa2015-06-09 13:56:10 -0700563 SkDebugf("Could not create scanline decoder for %s with color type %s. "
msarett7f691442015-09-22 11:56:16 -0700564 "Skipping bench.\n", path.c_str(), color_type_to_str(colorType));
msarettb23e6aa2015-06-09 13:56:10 -0700565 return false;
566 }
567 *width = info.width();
568 *height = info.height();
569 } else {
570 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream));
halcanary96fcdcc2015-08-27 07:41:13 -0700571 if (nullptr == decoder) {
msarettb23e6aa2015-06-09 13:56:10 -0700572 SkDebugf("Could not create decoder for %s. Skipping bench.\n", path.c_str());
573 return false;
574 }
575 //FIXME: See skbug.com/3921
576 if (kIndex_8_SkColorType == colorType || kGray_8_SkColorType == colorType) {
577 SkDebugf("Cannot use image subset decoder for %s with color type %s. "
msarett7f691442015-09-22 11:56:16 -0700578 "Skipping bench.\n", path.c_str(), color_type_to_str(colorType));
msarettb23e6aa2015-06-09 13:56:10 -0700579 return false;
580 }
581 if (!decoder->buildTileIndex(stream.detach(), width, height)) {
582 SkDebugf("Could not build tile index for %s. Skipping bench.\n", path.c_str());
583 return false;
584 }
585 }
msarettab80e352015-06-17 10:28:22 -0700586
587 // Check if the image is large enough for a meaningful subset benchmark.
588 if (*width <= 512 && *height <= 512) {
589 // This should not print a message since it is not an error.
590 return false;
591 }
592
msarettb23e6aa2015-06-09 13:56:10 -0700593 return true;
594}
jcgregoriobf5e5232014-07-17 13:14:16 -0700595
msarett7f691442015-09-22 11:56:16 -0700596static bool valid_brd_bench(SkData* encoded, SkBitmapRegionDecoderInterface::Strategy strategy,
597 SkColorType colorType, uint32_t sampleSize, uint32_t minOutputSize, int* width,
598 int* height) {
599 SkStreamRewindable* stream = new SkMemoryStream(encoded);
600 SkAutoTDelete<SkBitmapRegionDecoderInterface> brd(
601 SkBitmapRegionDecoderInterface::CreateBitmapRegionDecoder(stream, strategy));
602 if (nullptr == brd.get()) {
603 // This is indicates that subset decoding is not supported for a particular image format.
604 return false;
605 }
606
607 SkAutoTDelete<SkBitmap> bitmap(brd->decodeRegion(0, 0, brd->width(), brd->height(), 1,
608 colorType));
609 if (nullptr == bitmap.get() || colorType != bitmap->colorType()) {
610 // This indicates that conversion to the requested color type is not supported for the
611 // particular image.
612 return false;
613 }
614
615 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
616 (uint32_t) brd->height()) {
617 // This indicates that the image is not large enough to decode a
618 // minOutputSize x minOutputSize subset at the given sampleSize.
619 return false;
620 }
621
622 // Set the image width and height. The calling code will use this to choose subsets to decode.
623 *width = brd->width();
624 *height = brd->height();
625 return true;
626}
627
egdaniel3bf92062015-06-26 08:12:46 -0700628static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700629 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700630#if SK_SUPPORT_GPU
631 if (FLAGS_abandonGpuContext) {
632 gGrFactory->abandonContexts();
633 }
634 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
635 gGrFactory->destroyContexts();
636 }
637#endif
638}
639
mtkleine714e752014-07-31 12:13:48 -0700640class BenchmarkStream {
641public:
mtklein92007582014-08-01 07:46:52 -0700642 BenchmarkStream() : fBenches(BenchRegistry::Head())
643 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700644 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700645 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800646 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800647 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700648 , fCurrentCodec(0)
msarett95f192d2015-02-13 09:05:41 -0800649 , fCurrentImage(0)
650 , fCurrentSubsetImage(0)
msarett7f691442015-09-22 11:56:16 -0700651 , fCurrentBRDImage(0)
msarett95f192d2015-02-13 09:05:41 -0800652 , fCurrentColorType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700653 , fCurrentSubsetType(0)
654 , fUseCodec(0)
msarett7f691442015-09-22 11:56:16 -0700655 , fCurrentBRDStrategy(0)
656 , fCurrentBRDSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700657 , fCurrentAnimSKP(0) {
mtklein92007582014-08-01 07:46:52 -0700658 for (int i = 0; i < FLAGS_skps.count(); i++) {
659 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
660 fSKPs.push_back() = FLAGS_skps[i];
661 } else {
662 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
663 SkString path;
664 while (it.next(&path)) {
665 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
666 }
667 }
668 }
mtkleine714e752014-07-31 12:13:48 -0700669
mtklein92007582014-08-01 07:46:52 -0700670 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
671 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
672 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
673 exit(1);
674 }
675
676 for (int i = 0; i < FLAGS_scales.count(); i++) {
677 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
678 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
679 exit(1);
680 }
681 }
robertphillips5b693772014-11-21 06:19:36 -0800682
cdalton63a82852015-06-29 14:06:10 -0700683 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
684 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700685 exit(1);
686 }
687
robertphillips5b693772014-11-21 06:19:36 -0800688 if (FLAGS_mpd) {
689 fUseMPDs.push_back() = true;
690 }
mtkleinc751ecb2015-06-15 08:56:38 -0700691 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700692
msarett95f192d2015-02-13 09:05:41 -0800693 // Prepare the images for decoding
694 for (int i = 0; i < FLAGS_images.count(); i++) {
695 const char* flag = FLAGS_images[i];
696 if (sk_isdir(flag)) {
697 // If the value passed in is a directory, add all the images
698 SkOSFile::Iter it(flag);
699 SkString file;
700 while (it.next(&file)) {
701 fImages.push_back() = SkOSPath::Join(flag, file.c_str());
702 }
703 } else if (sk_exists(flag)) {
704 // Also add the value if it is a single image
705 fImages.push_back() = flag;
706 }
707 }
mtklein95553d92015-03-12 08:24:21 -0700708
msarett95f192d2015-02-13 09:05:41 -0800709 // Choose the candidate color types for image decoding
710 const SkColorType colorTypes[] =
msarettb23e6aa2015-06-09 13:56:10 -0700711 { kN32_SkColorType,
712 kRGB_565_SkColorType,
713 kAlpha_8_SkColorType,
714 kIndex_8_SkColorType,
715 kGray_8_SkColorType };
msarett95f192d2015-02-13 09:05:41 -0800716 fColorTypes.push_back_n(SK_ARRAY_COUNT(colorTypes), colorTypes);
mtklein92007582014-08-01 07:46:52 -0700717 }
718
mtkleinfd731ce2014-09-10 12:19:30 -0700719 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
720 // Not strictly necessary, as it will be checked again later,
721 // but helps to avoid a lot of pointless work if we're going to skip it.
722 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
723 return false;
724 }
725
scroggoa1193e42015-01-21 12:09:53 -0800726 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -0700727 if (stream.get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700728 SkDebugf("Could not read %s.\n", path);
729 return false;
730 }
731
mtklein57f27bd2015-02-09 11:58:41 -0800732 pic->reset(SkPicture::CreateFromStream(stream.get()));
halcanary96fcdcc2015-08-27 07:41:13 -0700733 if (pic->get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700734 SkDebugf("Could not read %s as an SkPicture.\n", path);
735 return false;
736 }
737 return true;
738 }
739
mtklein92007582014-08-01 07:46:52 -0700740 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700741 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700742 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700743 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700744 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700745 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700746 return bench;
747 }
mtklein92007582014-08-01 07:46:52 -0700748
mtkleine714e752014-07-31 12:13:48 -0700749 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -0700750 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700751 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800752 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700753 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700754 fBenchType = "micro";
halcanary385fe4d2015-08-26 13:07:48 -0700755 return new GMBench(gm.detach());
mtkleine714e752014-07-31 12:13:48 -0700756 }
757 }
mtklein92007582014-08-01 07:46:52 -0700758
mtkleinfd731ce2014-09-10 12:19:30 -0700759 // First add all .skps as RecordingBenches.
760 while (fCurrentRecording < fSKPs.count()) {
761 const SkString& path = fSKPs[fCurrentRecording++];
762 SkAutoTUnref<SkPicture> pic;
763 if (!ReadPicture(path.c_str(), &pic)) {
764 continue;
765 }
766 SkString name = SkOSPath::Basename(path.c_str());
767 fSourceType = "skp";
768 fBenchType = "recording";
bsalomon0aa5cea2014-12-15 09:13:35 -0800769 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic));
mtklein051e56d2014-12-04 08:46:51 -0800770 fSKPOps = pic->approximateOpCount();
halcanary385fe4d2015-08-26 13:07:48 -0700771 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700772 }
773
774 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700775 while (fCurrentScale < fScales.count()) {
776 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800777 const SkString& path = fSKPs[fCurrentSKP];
mtkleinfd731ce2014-09-10 12:19:30 -0700778 SkAutoTUnref<SkPicture> pic;
779 if (!ReadPicture(path.c_str(), &pic)) {
robertphillips5b693772014-11-21 06:19:36 -0800780 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700781 continue;
782 }
robertphillips5b693772014-11-21 06:19:36 -0800783
784 while (fCurrentUseMPD < fUseMPDs.count()) {
785 if (FLAGS_bbh) {
786 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
787 SkRTreeFactory factory;
788 SkPictureRecorder recorder;
789 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
790 pic->playback(recorder.beginRecording(pic->cullRect().width(),
791 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800792 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800793 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
robertphillips5b693772014-11-21 06:19:36 -0800794 pic.reset(recorder.endRecording());
795 }
796 SkString name = SkOSPath::Basename(path.c_str());
797 fSourceType = "skp";
798 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700799 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
800 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700801 }
robertphillips5b693772014-11-21 06:19:36 -0800802 fCurrentUseMPD = 0;
803 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700804 }
805 fCurrentSKP = 0;
806 fCurrentScale++;
807 }
808
joshualitt261c3ad2015-04-27 09:16:57 -0700809 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700810 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700811 while (fCurrentAnimSKP < fSKPs.count()) {
812 const SkString& path = fSKPs[fCurrentAnimSKP];
813 SkAutoTUnref<SkPicture> pic;
814 if (!ReadPicture(path.c_str(), &pic)) {
815 fCurrentAnimSKP++;
816 continue;
817 }
818
819 fCurrentAnimSKP++;
820 SkString name = SkOSPath::Basename(path.c_str());
cdalton63a82852015-06-29 14:06:10 -0700821 SkAutoTUnref<SKPAnimationBench::Animation> animation(
822 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
halcanary385fe4d2015-08-26 13:07:48 -0700823 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation,
824 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700825 }
826 }
827
scroggo60869a42015-04-01 12:09:17 -0700828 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700829 fSourceType = "image";
830 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700831 const SkString& path = fImages[fCurrentCodec];
832 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
833 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700834 if (!codec) {
835 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700836 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700837 continue;
838 }
scroggo21027992015-04-02 13:22:38 -0700839
scroggo60869a42015-04-01 12:09:17 -0700840 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700841 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo60869a42015-04-01 12:09:17 -0700842 fCurrentColorType++;
scroggo21027992015-04-02 13:22:38 -0700843
scroggo60869a42015-04-01 12:09:17 -0700844 // Make sure we can decode to this color type.
scroggo60869a42015-04-01 12:09:17 -0700845 SkImageInfo info = codec->getInfo().makeColorType(colorType);
scroggo21027992015-04-02 13:22:38 -0700846 SkAlphaType alphaType;
847 if (!SkColorTypeValidateAlphaType(colorType, info.alphaType(),
848 &alphaType)) {
849 continue;
850 }
851 if (alphaType != info.alphaType()) {
852 info = info.makeAlphaType(alphaType);
853 }
854
855 const size_t rowBytes = info.minRowBytes();
856 SkAutoMalloc storage(info.getSafeSize(rowBytes));
857
858 // Used if fCurrentColorType is kIndex_8_SkColorType
859 int colorCount = 256;
860 SkPMColor colors[256];
861
scroggoeb602a52015-07-09 08:16:03 -0700862 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700863 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700864 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700865 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700866 case SkCodec::kSuccess:
867 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700868 return new CodecBench(SkOSPath::Basename(path.c_str()),
869 encoded, colorType);
scroggoeb602a52015-07-09 08:16:03 -0700870 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700871 // This is okay. Not all conversions are valid.
872 break;
scroggo60869a42015-04-01 12:09:17 -0700873 default:
874 // This represents some sort of failure.
875 SkASSERT(false);
876 break;
877 }
878 }
879 fCurrentColorType = 0;
880 }
881
msarett95f192d2015-02-13 09:05:41 -0800882 // Run the DecodingBenches
883 while (fCurrentImage < fImages.count()) {
scroggo303fa352015-10-05 11:03:34 -0700884 fSourceType = "image";
885 fBenchType = "skimagedecoder";
msarett95f192d2015-02-13 09:05:41 -0800886 while (fCurrentColorType < fColorTypes.count()) {
887 const SkString& path = fImages[fCurrentImage];
888 SkColorType colorType = fColorTypes[fCurrentColorType];
889 fCurrentColorType++;
scroggo60869a42015-04-01 12:09:17 -0700890 // Check if the image decodes to the right color type
891 // before creating the benchmark
msarett95f192d2015-02-13 09:05:41 -0800892 SkBitmap bitmap;
893 if (SkImageDecoder::DecodeFile(path.c_str(), &bitmap,
scroggo60869a42015-04-01 12:09:17 -0700894 colorType, SkImageDecoder::kDecodePixels_Mode)
895 && bitmap.colorType() == colorType) {
msarett95f192d2015-02-13 09:05:41 -0800896 return new DecodingBench(path, colorType);
897 }
898 }
899 fCurrentColorType = 0;
900 fCurrentImage++;
901 }
902
msarettb23e6aa2015-06-09 13:56:10 -0700903 // Run the SubsetBenches
904 bool useCodecOpts[] = { true, false };
905 while (fUseCodec < 2) {
906 bool useCodec = useCodecOpts[fUseCodec];
scroggo303fa352015-10-05 11:03:34 -0700907 fSourceType = "image";
908 fBenchType = useCodec ? "skcodec" : "skimagedecoder";
msarettb23e6aa2015-06-09 13:56:10 -0700909 while (fCurrentSubsetImage < fImages.count()) {
scroggoab12c272015-10-08 12:01:39 -0700910 const SkString& path = fImages[fCurrentSubsetImage];
911 if (!run_subset_bench(path, useCodec)) {
912 fCurrentSubsetImage++;
913 continue;
914 }
msarettb23e6aa2015-06-09 13:56:10 -0700915 while (fCurrentColorType < fColorTypes.count()) {
msarettb23e6aa2015-06-09 13:56:10 -0700916 SkColorType colorType = fColorTypes[fCurrentColorType];
917 while (fCurrentSubsetType <= kLast_SubsetType) {
918 int width = 0;
919 int height = 0;
920 int currentSubsetType = fCurrentSubsetType++;
921 if (valid_subset_bench(path, colorType, useCodec, &width, &height)) {
922 switch (currentSubsetType) {
923 case kTopLeft_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700924 return new SubsetSingleBench(path, colorType, width/3,
925 height/3, 0, 0, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700926 case kTopRight_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700927 return new SubsetSingleBench(path, colorType, width/3,
928 height/3, 2*width/3, 0, useCodec);
929 case kMiddle_SubsetType:
930 return new SubsetSingleBench(path, colorType, width/3,
931 height/3, width/3, height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700932 case kBottomLeft_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700933 return new SubsetSingleBench(path, colorType, width/3,
934 height/3, 0, 2*height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700935 case kBottomRight_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700936 return new SubsetSingleBench(path, colorType, width/3,
937 height/3, 2*width/3, 2*height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700938 case kTranslate_SubsetType:
939 return new SubsetTranslateBench(path, colorType, 512, 512,
940 useCodec);
941 case kZoom_SubsetType:
942 return new SubsetZoomBench(path, colorType, 512, 512,
943 useCodec);
msarett95f192d2015-02-13 09:05:41 -0800944 }
msarettb23e6aa2015-06-09 13:56:10 -0700945 } else {
946 break;
msarett95f192d2015-02-13 09:05:41 -0800947 }
948 }
msarettb23e6aa2015-06-09 13:56:10 -0700949 fCurrentSubsetType = 0;
950 fCurrentColorType++;
msarett95f192d2015-02-13 09:05:41 -0800951 }
msarettb23e6aa2015-06-09 13:56:10 -0700952 fCurrentColorType = 0;
953 fCurrentSubsetImage++;
msarett95f192d2015-02-13 09:05:41 -0800954 }
msarettb23e6aa2015-06-09 13:56:10 -0700955 fCurrentSubsetImage = 0;
956 fUseCodec++;
msarett95f192d2015-02-13 09:05:41 -0800957 }
958
msarett7f691442015-09-22 11:56:16 -0700959 // Run the BRDBenches
960 // We will benchmark multiple BRD strategies.
scroggo303fa352015-10-05 11:03:34 -0700961 static const struct {
962 SkBitmapRegionDecoderInterface::Strategy fStrategy;
963 const char* fName;
964 } strategies[] = {
965 { SkBitmapRegionDecoderInterface::kOriginal_Strategy, "BRD" },
966 { SkBitmapRegionDecoderInterface::kCanvas_Strategy, "BRD_canvas" },
msarett7f691442015-09-22 11:56:16 -0700967 };
968
969 // We intend to create benchmarks that model the use cases in
970 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
971 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
972 // the image. For that reason, we will benchmark decodes in five representative locations
973 // in the image. Additionally, this use case utilizes power of two scaling, so we will
974 // test on power of two sample sizes. The output tile is always 512x512, so, when a
975 // sampleSize is used, the size of the subset that is decoded is always
976 // (sampleSize*512)x(sampleSize*512).
977 // There are a few good reasons to only test on power of two sample sizes at this time:
978 // JPEG decodes using kOriginal_Strategy are broken for non-powers of two.
979 // skbug.com/4319
980 // All use cases we are aware of only scale by powers of two.
981 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
982 // these tests are sufficient to provide good coverage of our scaling options.
983 const uint32_t sampleSizes[] = { 1, 2, 4, 8, 16 };
984 const uint32_t minOutputSize = 512;
985 while (fCurrentBRDImage < fImages.count()) {
986 while (fCurrentBRDStrategy < (int) SK_ARRAY_COUNT(strategies)) {
scroggo303fa352015-10-05 11:03:34 -0700987 fSourceType = "image";
988 fBenchType = strategies[fCurrentBRDStrategy].fName;
scroggo860e8a62015-10-15 07:51:28 -0700989
990 const SkString& path = fImages[fCurrentBRDImage];
991 const SkBitmapRegionDecoderInterface::Strategy strategy =
992 strategies[fCurrentBRDStrategy].fStrategy;
993
994 if (SkBitmapRegionDecoderInterface::kOriginal_Strategy == strategy) {
995 // Disable png and jpeg for SkImageDecoder:
996 if (!FLAGS_jpgBuildTileIndex) {
997 if (path.endsWith("JPEG") || path.endsWith("JPG") ||
998 path.endsWith("jpeg") || path.endsWith("jpg"))
999 {
1000 fCurrentBRDStrategy++;
1001 continue;
1002 }
1003 }
1004 if (!FLAGS_pngBuildTileIndex) {
1005 if (path.endsWith("PNG") || path.endsWith("png")) {
1006 fCurrentBRDStrategy++;
1007 continue;
1008 }
1009 }
1010 }
1011
msarett7f691442015-09-22 11:56:16 -07001012 while (fCurrentColorType < fColorTypes.count()) {
1013 while (fCurrentBRDSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
1014 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -07001015
1016
msarett7f691442015-09-22 11:56:16 -07001017 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
1018 const SkColorType colorType = fColorTypes[fCurrentColorType];
1019 uint32_t sampleSize = sampleSizes[fCurrentBRDSampleSize];
1020 int currentSubsetType = fCurrentSubsetType++;
1021
1022 int width = 0;
1023 int height = 0;
1024 if (!valid_brd_bench(encoded.get(), strategy, colorType, sampleSize,
1025 minOutputSize, &width, &height)) {
1026 break;
1027 }
1028
1029 SkString basename = SkOSPath::Basename(path.c_str());
1030 SkIRect subset;
1031 const uint32_t subsetSize = sampleSize * minOutputSize;
1032 switch (currentSubsetType) {
1033 case kTopLeft_SubsetType:
1034 basename.append("_TopLeft");
1035 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
1036 break;
1037 case kTopRight_SubsetType:
1038 basename.append("_TopRight");
1039 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
1040 subsetSize);
1041 break;
1042 case kMiddle_SubsetType:
1043 basename.append("_Middle");
1044 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
1045 (height - subsetSize) / 2, subsetSize, subsetSize);
1046 break;
1047 case kBottomLeft_SubsetType:
1048 basename.append("_BottomLeft");
1049 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
1050 subsetSize);
1051 break;
1052 case kBottomRight_SubsetType:
1053 basename.append("_BottomRight");
1054 subset = SkIRect::MakeXYWH(width - subsetSize,
1055 height - subsetSize, subsetSize, subsetSize);
1056 break;
1057 default:
1058 SkASSERT(false);
1059 }
1060
1061 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
1062 strategy, colorType, sampleSize, subset);
1063 }
1064 fCurrentSubsetType = 0;
1065 fCurrentBRDSampleSize++;
1066 }
1067 fCurrentBRDSampleSize = 0;
1068 fCurrentColorType++;
1069 }
1070 fCurrentColorType = 0;
1071 fCurrentBRDStrategy++;
1072 }
1073 fCurrentBRDStrategy = 0;
1074 fCurrentBRDImage++;
1075 }
1076
halcanary96fcdcc2015-08-27 07:41:13 -07001077 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001078 }
mtklein92007582014-08-01 07:46:52 -07001079
1080 void fillCurrentOptions(ResultsWriter* log) const {
1081 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001082 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001083 if (0 == strcmp(fSourceType, "skp")) {
1084 log->configOption("clip",
1085 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1086 fClip.fRight, fClip.fBottom).c_str());
1087 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001088 if (fCurrentUseMPD > 0) {
1089 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1090 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1091 }
mtklein92007582014-08-01 07:46:52 -07001092 }
mtklein051e56d2014-12-04 08:46:51 -08001093 if (0 == strcmp(fBenchType, "recording")) {
1094 log->metric("bytes", fSKPBytes);
1095 log->metric("ops", fSKPOps);
1096 }
mtklein92007582014-08-01 07:46:52 -07001097 }
1098
mtkleine714e752014-07-31 12:13:48 -07001099private:
msarettb23e6aa2015-06-09 13:56:10 -07001100 enum SubsetType {
1101 kTopLeft_SubsetType = 0,
1102 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001103 kMiddle_SubsetType = 2,
1104 kBottomLeft_SubsetType = 3,
1105 kBottomRight_SubsetType = 4,
1106 kTranslate_SubsetType = 5,
1107 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001108 kLast_SubsetType = kZoom_SubsetType,
1109 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001110 };
1111
mtkleine714e752014-07-31 12:13:48 -07001112 const BenchRegistry* fBenches;
1113 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001114 SkIRect fClip;
1115 SkTArray<SkScalar> fScales;
1116 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -08001117 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001118 SkTArray<SkString> fImages;
1119 SkTArray<SkColorType> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001120 SkScalar fZoomMax;
1121 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001122
mtklein051e56d2014-12-04 08:46:51 -08001123 double fSKPBytes, fSKPOps;
1124
mtkleinfd731ce2014-09-10 12:19:30 -07001125 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1126 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1127 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -07001128 int fCurrentScale;
1129 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -08001130 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001131 int fCurrentCodec;
msarett95f192d2015-02-13 09:05:41 -08001132 int fCurrentImage;
1133 int fCurrentSubsetImage;
msarett7f691442015-09-22 11:56:16 -07001134 int fCurrentBRDImage;
msarett95f192d2015-02-13 09:05:41 -08001135 int fCurrentColorType;
msarettb23e6aa2015-06-09 13:56:10 -07001136 int fCurrentSubsetType;
1137 int fUseCodec;
msarett7f691442015-09-22 11:56:16 -07001138 int fCurrentBRDStrategy;
1139 int fCurrentBRDSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001140 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001141};
1142
jcgregorio3b27ade2014-11-13 08:06:40 -08001143int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001144int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001145 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001146 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001147 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001148
krajcevski69a55602014-08-13 10:46:31 -07001149#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001150 GrContextOptions grContextOpts;
krajcevski12b35442014-08-13 12:06:26 -07001151 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
halcanary385fe4d2015-08-26 13:07:48 -07001152 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001153#endif
1154
bsalomon06cddec2014-10-24 10:40:50 -07001155 if (FLAGS_veryVerbose) {
1156 FLAGS_verbose = true;
1157 }
1158
cdaltone1b89582015-06-25 19:17:08 -07001159 double samplingTimeMs = 0;
1160 if (0 != strcmp("0", FLAGS_samplingTime[0])) {
1161 SkSTArray<8, char> timeUnit;
1162 timeUnit.push_back_n(static_cast<int>(strlen(FLAGS_samplingTime[0])) + 1);
1163 if (2 != sscanf(FLAGS_samplingTime[0], "%lf%s", &samplingTimeMs, timeUnit.begin()) ||
1164 (0 != strcmp("s", timeUnit.begin()) && 0 != strcmp("ms", timeUnit.begin()))) {
1165 SkDebugf("Invalid --samplingTime \"%s\". Must be \"0\", \"%%lfs\", or \"%%lfms\"\n",
1166 FLAGS_samplingTime[0]);
1167 exit(0);
1168 }
1169 if (0 == strcmp("s", timeUnit.begin())) {
1170 samplingTimeMs *= 1000;
1171 }
1172 if (samplingTimeMs) {
1173 FLAGS_samples = kTimedSampling;
1174 }
1175 }
1176
bsalomon6eb03cc2014-08-07 14:28:50 -07001177 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001178 FLAGS_samples = 1;
1179 FLAGS_gpuFrameLag = 0;
1180 }
1181
bsalomon6eb03cc2014-08-07 14:28:50 -07001182 if (!FLAGS_writePath.isEmpty()) {
1183 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1184 if (!sk_mkdir(FLAGS_writePath[0])) {
1185 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001186 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001187 }
1188 }
1189
halcanary385fe4d2015-08-26 13:07:48 -07001190 SkAutoTDelete<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001191 if (!FLAGS_outResultsFile.isEmpty()) {
halcanary385fe4d2015-08-26 13:07:48 -07001192 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein60317d0f2014-07-14 11:30:37 -07001193 }
mtklein1915b622014-08-20 11:45:00 -07001194
1195 if (1 == FLAGS_properties.count() % 2) {
1196 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1197 return 1;
1198 }
1199 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1200 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1201 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001202
1203 if (1 == FLAGS_key.count() % 2) {
1204 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1205 return 1;
1206 }
1207 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001208 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001209 }
mtklein60317d0f2014-07-14 11:30:37 -07001210
mtkleinf3723212014-06-25 14:08:00 -07001211 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001212 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001213
cdaltone1b89582015-06-25 19:17:08 -07001214 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001215
bsalomon6eb03cc2014-08-07 14:28:50 -07001216 if (kAutoTuneLoops != FLAGS_loops) {
1217 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001218 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -07001219 SkDebugf("median\tbench\tconfig\n");
cdaltone1b89582015-06-25 19:17:08 -07001220 } else if (kTimedSampling == FLAGS_samples) {
1221 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001222 } else {
mtkleind75c4662015-04-30 07:11:22 -07001223 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001224 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001225 }
1226
bsalomonc2553372014-07-22 13:09:05 -07001227 SkTDArray<Config> configs;
1228 create_configs(&configs);
1229
mtkleine070c2b2014-10-14 08:40:43 -07001230 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001231 BenchmarkStream benchStream;
1232 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001233 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001234 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001235 continue;
1236 }
1237
egdaniel3bf92062015-06-26 08:12:46 -07001238 if (!configs.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -07001239 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001240 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001241 }
egdaniel3bf92062015-06-26 08:12:46 -07001242 for (int i = 0; i < configs.count(); ++i) {
1243 Target* target = is_enabled(b, configs[i]);
1244 if (!target) {
1245 continue;
1246 }
mtkleinf3723212014-06-25 14:08:00 -07001247
halcanary96fcdcc2015-08-27 07:41:13 -07001248 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001249 SkCanvas* canvas = target->getCanvas();
1250 const char* config = target->config.name;
1251
1252 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001253 bench->perCanvasPreDraw(canvas);
1254
cdaltone1b89582015-06-25 19:17:08 -07001255 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001256 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001257 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1258 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001259
1260 if (kTimedSampling != FLAGS_samples) {
1261 samples.reset(FLAGS_samples);
1262 for (int s = 0; s < FLAGS_samples; s++) {
egdaniel3bf92062015-06-26 08:12:46 -07001263 samples[s] = time(loops, bench, target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001264 }
1265 } else if (samplingTimeMs) {
1266 samples.reset();
1267 if (FLAGS_verbose) {
1268 SkDebugf("Begin sampling %s for %ims\n",
1269 bench->getUniqueName(), static_cast<int>(samplingTimeMs));
1270 }
1271 WallTimer timer;
1272 timer.start();
1273 do {
egdaniel3bf92062015-06-26 08:12:46 -07001274 samples.push_back(time(loops, bench, target) / loops);
cdaltone1b89582015-06-25 19:17:08 -07001275 timer.end();
1276 } while (timer.fWall < samplingTimeMs);
1277 }
mtkleinf3723212014-06-25 14:08:00 -07001278
robertphillips5b693772014-11-21 06:19:36 -08001279 bench->perCanvasPostDraw(canvas);
1280
egdaniel3bf92062015-06-26 08:12:46 -07001281 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001282 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001283 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001284 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001285 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001286 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001287 }
1288
1289 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001290 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001291 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001292 continue;
1293 }
1294
cdaltone1b89582015-06-25 19:17:08 -07001295 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001296 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001297 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001298 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001299 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001300 log->metric("min_ms", stats.min);
mtkleine070c2b2014-10-14 08:40:43 -07001301 if (runs++ % FLAGS_flushEvery == 0) {
1302 log->flush();
1303 }
mtklein60317d0f2014-07-14 11:30:37 -07001304
bsalomon6eb03cc2014-08-07 14:28:50 -07001305 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001306 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001307 config = ""; // Only print the config if we run the same bench on more than one.
1308 }
mtkleind75c4662015-04-30 07:11:22 -07001309 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1310 , sk_tools::getCurrResidentSetSizeMB()
1311 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001312 , bench->getUniqueName()
1313 , config);
mtkleinf3723212014-06-25 14:08:00 -07001314 } else if (FLAGS_quiet) {
egdaniel3bf92062015-06-26 08:12:46 -07001315 if (configs.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -07001316 config = ""; // Only print the config if we run the same bench on more than one.
1317 }
mtklein96289052014-09-10 12:05:59 -07001318 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001319 } else {
1320 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001321 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1322 , sk_tools::getCurrResidentSetSizeMB()
1323 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001324 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001325 , HUMANIZE(stats.min)
1326 , HUMANIZE(stats.median)
1327 , HUMANIZE(stats.mean)
1328 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001329 , stddev_percent
cdaltone1b89582015-06-25 19:17:08 -07001330 , kTimedSampling != FLAGS_samples ? stats.plot.c_str()
1331 : to_string(samples.count()).c_str()
mtkleinf3723212014-06-25 14:08:00 -07001332 , config
mtklein96289052014-09-10 12:05:59 -07001333 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001334 );
1335 }
bsalomonb12ea412015-02-02 21:19:50 -08001336#if SK_SUPPORT_GPU
1337 if (FLAGS_gpuStats &&
egdaniel3bf92062015-06-26 08:12:46 -07001338 Benchmark::kGPU_Backend == configs[i].backend) {
1339 gGrFactory->get(configs[i].ctxType)->printCacheStats();
1340 gGrFactory->get(configs[i].ctxType)->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001341 }
1342#endif
cdalton2c56ba52015-06-26 13:32:53 -07001343 if (FLAGS_verbose) {
1344 SkDebugf("Samples: ");
1345 for (int i = 0; i < samples.count(); i++) {
1346 SkDebugf("%s ", HUMANIZE(samples[i]));
1347 }
1348 SkDebugf("%s\n", bench->getUniqueName());
1349 }
egdaniel3bf92062015-06-26 08:12:46 -07001350 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001351 }
mtkleinf3723212014-06-25 14:08:00 -07001352 }
1353
mtkleine1091452014-12-04 10:47:02 -08001354 log->bench("memory_usage", 0,0);
1355 log->config("meta");
1356 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1357
joshualitte0b19d42015-03-26 10:41:02 -07001358#if SK_SUPPORT_GPU
1359 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1360 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001361 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001362#endif
1363
mtkleinf3723212014-06-25 14:08:00 -07001364 return 0;
1365}
1366
jcgregorio3b27ade2014-11-13 08:06:40 -08001367#if !defined SK_BUILD_FOR_IOS
1368int main(int argc, char** argv) {
1369 SkCommandLineFlags::Parse(argc, argv);
1370 return nanobench_main();
1371}
1372#endif