blob: 4ab6ca0b88c238aef127c644c3a3ec93e4ade075 [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
scroggo60869a42015-04-01 12:09:17 -0700793 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700794 fSourceType = "image";
795 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700796 const SkString& path = fImages[fCurrentCodec];
797 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
798 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700799 if (!codec) {
800 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700801 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700802 continue;
803 }
scroggo21027992015-04-02 13:22:38 -0700804
scroggo60869a42015-04-01 12:09:17 -0700805 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700806 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo60869a42015-04-01 12:09:17 -0700807 fCurrentColorType++;
scroggo21027992015-04-02 13:22:38 -0700808
scroggo60869a42015-04-01 12:09:17 -0700809 // Make sure we can decode to this color type.
scroggo60869a42015-04-01 12:09:17 -0700810 SkImageInfo info = codec->getInfo().makeColorType(colorType);
scroggo21027992015-04-02 13:22:38 -0700811 SkAlphaType alphaType;
812 if (!SkColorTypeValidateAlphaType(colorType, info.alphaType(),
813 &alphaType)) {
814 continue;
815 }
816 if (alphaType != info.alphaType()) {
817 info = info.makeAlphaType(alphaType);
818 }
819
820 const size_t rowBytes = info.minRowBytes();
821 SkAutoMalloc storage(info.getSafeSize(rowBytes));
822
823 // Used if fCurrentColorType is kIndex_8_SkColorType
824 int colorCount = 256;
825 SkPMColor colors[256];
826
scroggoeb602a52015-07-09 08:16:03 -0700827 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700828 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700829 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700830 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700831 case SkCodec::kSuccess:
832 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700833 return new CodecBench(SkOSPath::Basename(path.c_str()),
834 encoded, colorType);
scroggoeb602a52015-07-09 08:16:03 -0700835 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700836 // This is okay. Not all conversions are valid.
837 break;
scroggo60869a42015-04-01 12:09:17 -0700838 default:
839 // This represents some sort of failure.
840 SkASSERT(false);
841 break;
842 }
843 }
844 fCurrentColorType = 0;
845 }
846
msarett95f192d2015-02-13 09:05:41 -0800847 // Run the DecodingBenches
848 while (fCurrentImage < fImages.count()) {
scroggo303fa352015-10-05 11:03:34 -0700849 fSourceType = "image";
850 fBenchType = "skimagedecoder";
msarett95f192d2015-02-13 09:05:41 -0800851 while (fCurrentColorType < fColorTypes.count()) {
852 const SkString& path = fImages[fCurrentImage];
853 SkColorType colorType = fColorTypes[fCurrentColorType];
854 fCurrentColorType++;
scroggo60869a42015-04-01 12:09:17 -0700855 // Check if the image decodes to the right color type
856 // before creating the benchmark
msarett95f192d2015-02-13 09:05:41 -0800857 SkBitmap bitmap;
858 if (SkImageDecoder::DecodeFile(path.c_str(), &bitmap,
scroggo60869a42015-04-01 12:09:17 -0700859 colorType, SkImageDecoder::kDecodePixels_Mode)
860 && bitmap.colorType() == colorType) {
msarett95f192d2015-02-13 09:05:41 -0800861 return new DecodingBench(path, colorType);
862 }
863 }
864 fCurrentColorType = 0;
865 fCurrentImage++;
866 }
867
msarettb23e6aa2015-06-09 13:56:10 -0700868 // Run the SubsetBenches
869 bool useCodecOpts[] = { true, false };
870 while (fUseCodec < 2) {
871 bool useCodec = useCodecOpts[fUseCodec];
scroggo303fa352015-10-05 11:03:34 -0700872 fSourceType = "image";
873 fBenchType = useCodec ? "skcodec" : "skimagedecoder";
msarettb23e6aa2015-06-09 13:56:10 -0700874 while (fCurrentSubsetImage < fImages.count()) {
875 while (fCurrentColorType < fColorTypes.count()) {
876 const SkString& path = fImages[fCurrentSubsetImage];
877 SkColorType colorType = fColorTypes[fCurrentColorType];
878 while (fCurrentSubsetType <= kLast_SubsetType) {
879 int width = 0;
880 int height = 0;
881 int currentSubsetType = fCurrentSubsetType++;
882 if (valid_subset_bench(path, colorType, useCodec, &width, &height)) {
883 switch (currentSubsetType) {
884 case kTopLeft_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700885 return new SubsetSingleBench(path, colorType, width/3,
886 height/3, 0, 0, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700887 case kTopRight_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700888 return new SubsetSingleBench(path, colorType, width/3,
889 height/3, 2*width/3, 0, useCodec);
890 case kMiddle_SubsetType:
891 return new SubsetSingleBench(path, colorType, width/3,
892 height/3, width/3, height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700893 case kBottomLeft_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700894 return new SubsetSingleBench(path, colorType, width/3,
895 height/3, 0, 2*height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700896 case kBottomRight_SubsetType:
msarettab80e352015-06-17 10:28:22 -0700897 return new SubsetSingleBench(path, colorType, width/3,
898 height/3, 2*width/3, 2*height/3, useCodec);
msarettb23e6aa2015-06-09 13:56:10 -0700899 case kTranslate_SubsetType:
900 return new SubsetTranslateBench(path, colorType, 512, 512,
901 useCodec);
902 case kZoom_SubsetType:
903 return new SubsetZoomBench(path, colorType, 512, 512,
904 useCodec);
msarett95f192d2015-02-13 09:05:41 -0800905 }
msarettb23e6aa2015-06-09 13:56:10 -0700906 } else {
907 break;
msarett95f192d2015-02-13 09:05:41 -0800908 }
909 }
msarettb23e6aa2015-06-09 13:56:10 -0700910 fCurrentSubsetType = 0;
911 fCurrentColorType++;
msarett95f192d2015-02-13 09:05:41 -0800912 }
msarettb23e6aa2015-06-09 13:56:10 -0700913 fCurrentColorType = 0;
914 fCurrentSubsetImage++;
msarett95f192d2015-02-13 09:05:41 -0800915 }
msarettb23e6aa2015-06-09 13:56:10 -0700916 fCurrentSubsetImage = 0;
917 fUseCodec++;
msarett95f192d2015-02-13 09:05:41 -0800918 }
919
msarett7f691442015-09-22 11:56:16 -0700920 // Run the BRDBenches
921 // We will benchmark multiple BRD strategies.
scroggo303fa352015-10-05 11:03:34 -0700922 static const struct {
923 SkBitmapRegionDecoderInterface::Strategy fStrategy;
924 const char* fName;
925 } strategies[] = {
926 { SkBitmapRegionDecoderInterface::kOriginal_Strategy, "BRD" },
927 { SkBitmapRegionDecoderInterface::kCanvas_Strategy, "BRD_canvas" },
msarett7f691442015-09-22 11:56:16 -0700928 };
929
930 // We intend to create benchmarks that model the use cases in
931 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
932 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
933 // the image. For that reason, we will benchmark decodes in five representative locations
934 // in the image. Additionally, this use case utilizes power of two scaling, so we will
935 // test on power of two sample sizes. The output tile is always 512x512, so, when a
936 // sampleSize is used, the size of the subset that is decoded is always
937 // (sampleSize*512)x(sampleSize*512).
938 // There are a few good reasons to only test on power of two sample sizes at this time:
939 // JPEG decodes using kOriginal_Strategy are broken for non-powers of two.
940 // skbug.com/4319
941 // All use cases we are aware of only scale by powers of two.
942 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
943 // these tests are sufficient to provide good coverage of our scaling options.
944 const uint32_t sampleSizes[] = { 1, 2, 4, 8, 16 };
945 const uint32_t minOutputSize = 512;
946 while (fCurrentBRDImage < fImages.count()) {
947 while (fCurrentBRDStrategy < (int) SK_ARRAY_COUNT(strategies)) {
scroggo303fa352015-10-05 11:03:34 -0700948 fSourceType = "image";
949 fBenchType = strategies[fCurrentBRDStrategy].fName;
msarett7f691442015-09-22 11:56:16 -0700950 while (fCurrentColorType < fColorTypes.count()) {
951 while (fCurrentBRDSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
952 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
953 const SkString& path = fImages[fCurrentBRDImage];
954 const SkBitmapRegionDecoderInterface::Strategy strategy =
scroggo303fa352015-10-05 11:03:34 -0700955 strategies[fCurrentBRDStrategy].fStrategy;
msarett7f691442015-09-22 11:56:16 -0700956 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
957 const SkColorType colorType = fColorTypes[fCurrentColorType];
958 uint32_t sampleSize = sampleSizes[fCurrentBRDSampleSize];
959 int currentSubsetType = fCurrentSubsetType++;
960
961 int width = 0;
962 int height = 0;
963 if (!valid_brd_bench(encoded.get(), strategy, colorType, sampleSize,
964 minOutputSize, &width, &height)) {
965 break;
966 }
967
968 SkString basename = SkOSPath::Basename(path.c_str());
969 SkIRect subset;
970 const uint32_t subsetSize = sampleSize * minOutputSize;
971 switch (currentSubsetType) {
972 case kTopLeft_SubsetType:
973 basename.append("_TopLeft");
974 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
975 break;
976 case kTopRight_SubsetType:
977 basename.append("_TopRight");
978 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
979 subsetSize);
980 break;
981 case kMiddle_SubsetType:
982 basename.append("_Middle");
983 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
984 (height - subsetSize) / 2, subsetSize, subsetSize);
985 break;
986 case kBottomLeft_SubsetType:
987 basename.append("_BottomLeft");
988 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
989 subsetSize);
990 break;
991 case kBottomRight_SubsetType:
992 basename.append("_BottomRight");
993 subset = SkIRect::MakeXYWH(width - subsetSize,
994 height - subsetSize, subsetSize, subsetSize);
995 break;
996 default:
997 SkASSERT(false);
998 }
999
1000 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
1001 strategy, colorType, sampleSize, subset);
1002 }
1003 fCurrentSubsetType = 0;
1004 fCurrentBRDSampleSize++;
1005 }
1006 fCurrentBRDSampleSize = 0;
1007 fCurrentColorType++;
1008 }
1009 fCurrentColorType = 0;
1010 fCurrentBRDStrategy++;
1011 }
1012 fCurrentBRDStrategy = 0;
1013 fCurrentBRDImage++;
1014 }
1015
halcanary96fcdcc2015-08-27 07:41:13 -07001016 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001017 }
mtklein92007582014-08-01 07:46:52 -07001018
1019 void fillCurrentOptions(ResultsWriter* log) const {
1020 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001021 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001022 if (0 == strcmp(fSourceType, "skp")) {
1023 log->configOption("clip",
1024 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1025 fClip.fRight, fClip.fBottom).c_str());
1026 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001027 if (fCurrentUseMPD > 0) {
1028 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1029 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1030 }
mtklein92007582014-08-01 07:46:52 -07001031 }
mtklein051e56d2014-12-04 08:46:51 -08001032 if (0 == strcmp(fBenchType, "recording")) {
1033 log->metric("bytes", fSKPBytes);
1034 log->metric("ops", fSKPOps);
1035 }
mtklein92007582014-08-01 07:46:52 -07001036 }
1037
mtkleine714e752014-07-31 12:13:48 -07001038private:
msarettb23e6aa2015-06-09 13:56:10 -07001039 enum SubsetType {
1040 kTopLeft_SubsetType = 0,
1041 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001042 kMiddle_SubsetType = 2,
1043 kBottomLeft_SubsetType = 3,
1044 kBottomRight_SubsetType = 4,
1045 kTranslate_SubsetType = 5,
1046 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001047 kLast_SubsetType = kZoom_SubsetType,
1048 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001049 };
1050
mtkleine714e752014-07-31 12:13:48 -07001051 const BenchRegistry* fBenches;
1052 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001053 SkIRect fClip;
1054 SkTArray<SkScalar> fScales;
1055 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -08001056 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001057 SkTArray<SkString> fImages;
1058 SkTArray<SkColorType> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001059 SkScalar fZoomMax;
1060 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001061
mtklein051e56d2014-12-04 08:46:51 -08001062 double fSKPBytes, fSKPOps;
1063
mtkleinfd731ce2014-09-10 12:19:30 -07001064 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1065 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1066 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -07001067 int fCurrentScale;
1068 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -08001069 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001070 int fCurrentCodec;
msarett95f192d2015-02-13 09:05:41 -08001071 int fCurrentImage;
1072 int fCurrentSubsetImage;
msarett7f691442015-09-22 11:56:16 -07001073 int fCurrentBRDImage;
msarett95f192d2015-02-13 09:05:41 -08001074 int fCurrentColorType;
msarettb23e6aa2015-06-09 13:56:10 -07001075 int fCurrentSubsetType;
1076 int fUseCodec;
msarett7f691442015-09-22 11:56:16 -07001077 int fCurrentBRDStrategy;
1078 int fCurrentBRDSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001079 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001080};
1081
jcgregorio3b27ade2014-11-13 08:06:40 -08001082int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001083int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001084 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001085 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001086 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001087
krajcevski69a55602014-08-13 10:46:31 -07001088#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001089 GrContextOptions grContextOpts;
krajcevski12b35442014-08-13 12:06:26 -07001090 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
halcanary385fe4d2015-08-26 13:07:48 -07001091 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001092#endif
1093
bsalomon06cddec2014-10-24 10:40:50 -07001094 if (FLAGS_veryVerbose) {
1095 FLAGS_verbose = true;
1096 }
1097
cdaltone1b89582015-06-25 19:17:08 -07001098 double samplingTimeMs = 0;
1099 if (0 != strcmp("0", FLAGS_samplingTime[0])) {
1100 SkSTArray<8, char> timeUnit;
1101 timeUnit.push_back_n(static_cast<int>(strlen(FLAGS_samplingTime[0])) + 1);
1102 if (2 != sscanf(FLAGS_samplingTime[0], "%lf%s", &samplingTimeMs, timeUnit.begin()) ||
1103 (0 != strcmp("s", timeUnit.begin()) && 0 != strcmp("ms", timeUnit.begin()))) {
1104 SkDebugf("Invalid --samplingTime \"%s\". Must be \"0\", \"%%lfs\", or \"%%lfms\"\n",
1105 FLAGS_samplingTime[0]);
1106 exit(0);
1107 }
1108 if (0 == strcmp("s", timeUnit.begin())) {
1109 samplingTimeMs *= 1000;
1110 }
1111 if (samplingTimeMs) {
1112 FLAGS_samples = kTimedSampling;
1113 }
1114 }
1115
bsalomon6eb03cc2014-08-07 14:28:50 -07001116 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001117 FLAGS_samples = 1;
1118 FLAGS_gpuFrameLag = 0;
1119 }
1120
bsalomon6eb03cc2014-08-07 14:28:50 -07001121 if (!FLAGS_writePath.isEmpty()) {
1122 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1123 if (!sk_mkdir(FLAGS_writePath[0])) {
1124 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001125 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001126 }
1127 }
1128
halcanary385fe4d2015-08-26 13:07:48 -07001129 SkAutoTDelete<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001130 if (!FLAGS_outResultsFile.isEmpty()) {
halcanary385fe4d2015-08-26 13:07:48 -07001131 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein60317d0f2014-07-14 11:30:37 -07001132 }
mtklein1915b622014-08-20 11:45:00 -07001133
1134 if (1 == FLAGS_properties.count() % 2) {
1135 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1136 return 1;
1137 }
1138 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1139 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1140 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001141
1142 if (1 == FLAGS_key.count() % 2) {
1143 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1144 return 1;
1145 }
1146 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001147 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001148 }
mtklein60317d0f2014-07-14 11:30:37 -07001149
mtkleinf3723212014-06-25 14:08:00 -07001150 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001151 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001152
cdaltone1b89582015-06-25 19:17:08 -07001153 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001154
bsalomon6eb03cc2014-08-07 14:28:50 -07001155 if (kAutoTuneLoops != FLAGS_loops) {
1156 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001157 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -07001158 SkDebugf("median\tbench\tconfig\n");
cdaltone1b89582015-06-25 19:17:08 -07001159 } else if (kTimedSampling == FLAGS_samples) {
1160 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001161 } else {
mtkleind75c4662015-04-30 07:11:22 -07001162 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001163 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001164 }
1165
bsalomonc2553372014-07-22 13:09:05 -07001166 SkTDArray<Config> configs;
1167 create_configs(&configs);
1168
mtkleine070c2b2014-10-14 08:40:43 -07001169 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001170 BenchmarkStream benchStream;
1171 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001172 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001173 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001174 continue;
1175 }
1176
egdaniel3bf92062015-06-26 08:12:46 -07001177 if (!configs.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -07001178 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001179 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001180 }
egdaniel3bf92062015-06-26 08:12:46 -07001181 for (int i = 0; i < configs.count(); ++i) {
1182 Target* target = is_enabled(b, configs[i]);
1183 if (!target) {
1184 continue;
1185 }
mtkleinf3723212014-06-25 14:08:00 -07001186
halcanary96fcdcc2015-08-27 07:41:13 -07001187 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001188 SkCanvas* canvas = target->getCanvas();
1189 const char* config = target->config.name;
1190
1191 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001192 bench->perCanvasPreDraw(canvas);
1193
cdaltone1b89582015-06-25 19:17:08 -07001194 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001195 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001196 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1197 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001198
1199 if (kTimedSampling != FLAGS_samples) {
1200 samples.reset(FLAGS_samples);
1201 for (int s = 0; s < FLAGS_samples; s++) {
egdaniel3bf92062015-06-26 08:12:46 -07001202 samples[s] = time(loops, bench, target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001203 }
1204 } else if (samplingTimeMs) {
1205 samples.reset();
1206 if (FLAGS_verbose) {
1207 SkDebugf("Begin sampling %s for %ims\n",
1208 bench->getUniqueName(), static_cast<int>(samplingTimeMs));
1209 }
1210 WallTimer timer;
1211 timer.start();
1212 do {
egdaniel3bf92062015-06-26 08:12:46 -07001213 samples.push_back(time(loops, bench, target) / loops);
cdaltone1b89582015-06-25 19:17:08 -07001214 timer.end();
1215 } while (timer.fWall < samplingTimeMs);
1216 }
mtkleinf3723212014-06-25 14:08:00 -07001217
robertphillips5b693772014-11-21 06:19:36 -08001218 bench->perCanvasPostDraw(canvas);
1219
egdaniel3bf92062015-06-26 08:12:46 -07001220 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001221 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001222 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001223 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001224 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001225 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001226 }
1227
1228 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001229 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001230 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001231 continue;
1232 }
1233
cdaltone1b89582015-06-25 19:17:08 -07001234 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001235 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001236 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001237 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001238 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001239 log->metric("min_ms", stats.min);
mtkleine070c2b2014-10-14 08:40:43 -07001240 if (runs++ % FLAGS_flushEvery == 0) {
1241 log->flush();
1242 }
mtklein60317d0f2014-07-14 11:30:37 -07001243
bsalomon6eb03cc2014-08-07 14:28:50 -07001244 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001245 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001246 config = ""; // Only print the config if we run the same bench on more than one.
1247 }
mtkleind75c4662015-04-30 07:11:22 -07001248 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1249 , sk_tools::getCurrResidentSetSizeMB()
1250 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001251 , bench->getUniqueName()
1252 , config);
mtkleinf3723212014-06-25 14:08:00 -07001253 } else if (FLAGS_quiet) {
egdaniel3bf92062015-06-26 08:12:46 -07001254 if (configs.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -07001255 config = ""; // Only print the config if we run the same bench on more than one.
1256 }
mtklein96289052014-09-10 12:05:59 -07001257 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001258 } else {
1259 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001260 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1261 , sk_tools::getCurrResidentSetSizeMB()
1262 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001263 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001264 , HUMANIZE(stats.min)
1265 , HUMANIZE(stats.median)
1266 , HUMANIZE(stats.mean)
1267 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001268 , stddev_percent
cdaltone1b89582015-06-25 19:17:08 -07001269 , kTimedSampling != FLAGS_samples ? stats.plot.c_str()
1270 : to_string(samples.count()).c_str()
mtkleinf3723212014-06-25 14:08:00 -07001271 , config
mtklein96289052014-09-10 12:05:59 -07001272 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001273 );
1274 }
bsalomonb12ea412015-02-02 21:19:50 -08001275#if SK_SUPPORT_GPU
1276 if (FLAGS_gpuStats &&
egdaniel3bf92062015-06-26 08:12:46 -07001277 Benchmark::kGPU_Backend == configs[i].backend) {
1278 gGrFactory->get(configs[i].ctxType)->printCacheStats();
1279 gGrFactory->get(configs[i].ctxType)->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001280 }
1281#endif
cdalton2c56ba52015-06-26 13:32:53 -07001282 if (FLAGS_verbose) {
1283 SkDebugf("Samples: ");
1284 for (int i = 0; i < samples.count(); i++) {
1285 SkDebugf("%s ", HUMANIZE(samples[i]));
1286 }
1287 SkDebugf("%s\n", bench->getUniqueName());
1288 }
egdaniel3bf92062015-06-26 08:12:46 -07001289 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001290 }
mtkleinf3723212014-06-25 14:08:00 -07001291 }
1292
mtkleine1091452014-12-04 10:47:02 -08001293 log->bench("memory_usage", 0,0);
1294 log->config("meta");
1295 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1296
joshualitte0b19d42015-03-26 10:41:02 -07001297#if SK_SUPPORT_GPU
1298 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1299 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001300 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001301#endif
1302
mtkleinf3723212014-06-25 14:08:00 -07001303 return 0;
1304}
1305
jcgregorio3b27ade2014-11-13 08:06:40 -08001306#if !defined SK_BUILD_FOR_IOS
1307int main(int argc, char** argv) {
1308 SkCommandLineFlags::Parse(argc, argv);
1309 return nanobench_main();
1310}
1311#endif