blob: 99bea4f60f4e2f3c2e59162d3ee9ae78b4c8a68a [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
msarett84451022016-02-11 06:45:51 -080012#include "AndroidCodecBench.h"
mtkleinf3723212014-06-25 14:08:00 -070013#include "Benchmark.h"
msarett7f691442015-09-22 11:56:16 -070014#include "BitmapRegionDecoderBench.h"
scroggo60869a42015-04-01 12:09:17 -070015#include "CodecBench.h"
msarett7f691442015-09-22 11:56:16 -070016#include "CodecBenchPriv.h"
msarett2cee9022016-06-03 08:25:21 -070017#include "ColorCodecBench.h"
mtkleinf3723212014-06-25 14:08:00 -070018#include "CrashHandler.h"
mtkleine714e752014-07-31 12:13:48 -070019#include "GMBench.h"
mtkleinafb43792014-08-19 15:55:55 -070020#include "ProcStats.h"
mtkleinfd731ce2014-09-10 12:19:30 -070021#include "RecordingBench.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040022#include "ResultsWriter.h"
joshualitt261c3ad2015-04-27 09:16:57 -070023#include "SKPAnimationBench.h"
mtklein92007582014-08-01 07:46:52 -070024#include "SKPBench.h"
msarett84451022016-02-11 06:45:51 -080025#include "SkAndroidCodec.h"
Hal Canary95e3c052017-01-11 12:44:43 -050026#include "SkAutoMalloc.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000027#include "SkBBoxHierarchy.h"
Hal Canary95e3c052017-01-11 12:44:43 -050028#include "SkBitmapRegionDecoder.h"
mtkleinf3723212014-06-25 14:08:00 -070029#include "SkCanvas.h"
scroggo60869a42015-04-01 12:09:17 -070030#include "SkCodec.h"
caryclark17f0b6d2014-07-22 10:15:34 -070031#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080032#include "SkCommonFlagsConfig.h"
Chris Dalton040238b2017-12-18 14:22:34 -070033#include "SkCommonFlagsGpu.h"
msarett95f192d2015-02-13 09:05:41 -080034#include "SkData.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040035#include "SkDebugfTracer.h"
Brian Osman53136aa2017-07-20 15:43:35 -040036#include "SkEventTracingPriv.h"
mtkleinf3723212014-06-25 14:08:00 -070037#include "SkGraphics.h"
halcanary4dbbd042016-06-07 17:21:10 -070038#include "SkLeanWindows.h"
mtklein20840502014-08-21 15:51:22 -070039#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050040#include "SkOSPath.h"
mtklein20840502014-08-21 15:51:22 -070041#include "SkPictureRecorder.h"
Hal Canary95e3c052017-01-11 12:44:43 -050042#include "SkScan.h"
mtkleinf3723212014-06-25 14:08:00 -070043#include "SkString.h"
44#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080045#include "SkTaskGroup.h"
Brian Osmanc3cdef52017-08-31 14:09:22 -040046#include "SkTraceEvent.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040047#include "Stats.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040048#include "ios_utils.h"
mtkleinf3723212014-06-25 14:08:00 -070049
Hal Canary0f666812018-03-22 15:21:12 -040050#ifdef SK_XML
51#include "SkSVGDOM.h"
52#endif // SK_XML
53
bungeman60e0fee2015-08-26 05:15:46 -070054#include <stdlib.h>
Mike Klein03141d22017-10-30 11:57:15 -040055#include <thread>
bungeman60e0fee2015-08-26 05:15:46 -070056
Mike Reedc928fe22017-06-05 10:20:31 -040057extern bool gSkForceRasterPipelineBlitter;
58
Mike Klein8f11d4d2018-01-24 12:42:55 -050059#ifndef SK_BUILD_FOR_WIN
scroggo38ce0a72016-01-07 07:28:47 -080060 #include <unistd.h>
Brian Salomon032aaae2018-03-23 16:10:36 -040061
scroggo38ce0a72016-01-07 07:28:47 -080062#endif
63
mtkleinbb6a0282014-07-01 08:43:42 -070064#if SK_SUPPORT_GPU
bsalomon76228632015-05-29 08:02:10 -070065 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070066 #include "GrContextFactory.h"
Brian Salomon032aaae2018-03-23 16:10:36 -040067 #include "GrContextPriv.h"
Greg Daniel81e7bf82017-07-19 14:47:42 -040068 #include "SkGr.h"
Brian Salomon032aaae2018-03-23 16:10:36 -040069 #include "gl/GrGLDefines.h"
70 #include "gl/GrGLGpu.h"
71 #include "gl/GrGLUtil.h"
72
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040073 using sk_gpu_test::ContextInfo;
bsalomon3724e572016-03-30 18:56:19 -070074 using sk_gpu_test::GrContextFactory;
bsalomonc8699322016-05-11 11:55:36 -070075 using sk_gpu_test::TestContext;
Brian Salomon032aaae2018-03-23 16:10:36 -040076
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040077 GrContextOptions grContextOpts;
mtkleinbb6a0282014-07-01 08:43:42 -070078#endif
79
bsalomon682c2692015-05-22 14:01:46 -070080 struct GrContextOptions;
81
reed53249782014-10-10 09:09:52 -070082static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070083
Mike Kleind8ee67c2017-01-19 12:14:50 -050084#if !defined(__has_feature)
85 #define __has_feature(x) 0
86#endif
87
mtkleinb5110422014-08-07 15:20:02 -070088static const int kDefaultLoops =
Mike Klein7a002c32018-03-30 16:22:13 +000089#if defined(SK_DEBUG) || __has_feature(address_sanitizer)
bsalomon6eb03cc2014-08-07 14:28:50 -070090 1;
mtkleina189ccd2014-07-14 12:28:47 -070091#else
bsalomon6eb03cc2014-08-07 14:28:50 -070092 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070093#endif
94
bsalomon6eb03cc2014-08-07 14:28:50 -070095static SkString loops_help_txt() {
96 SkString help;
97 help.printf("Number of times to run each bench. Set this to %d to auto-"
98 "tune for each bench. Timings are only reported when auto-tuning.",
99 kAutoTuneLoops);
100 return help;
101}
102
cdaltone1b89582015-06-25 19:17:08 -0700103static SkString to_string(int n) {
104 SkString str;
105 str.appendS32(n);
106 return str;
107}
108
Ben Wagner32fa5102017-08-10 21:25:55 -0400109DECLARE_bool(undefok);
110
bsalomon6eb03cc2014-08-07 14:28:50 -0700111DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
112
mtkleinf3723212014-06-25 14:08:00 -0700113DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -0700114DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -0700115DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
116DEFINE_double(overheadGoal, 0.0001,
117 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -0700118DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -0700119DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -0700120
mtklein60317d0f2014-07-14 11:30:37 -0700121DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -0700122DEFINE_int32(maxCalibrationAttempts, 3,
123 "Try up to this many times to guess loops for a bench, or skip the bench.");
124DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -0700125DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
126DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700127DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
128 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700129DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
mtklein8c1a4f82016-08-08 06:56:22 -0700130DEFINE_bool(lite, false, "Use SkLiteRecorder in recording benchmarks?");
robertphillips5b693772014-11-21 06:19:36 -0800131DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700132DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700133DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
bsalomonb12ea412015-02-02 21:19:50 -0800134DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800135DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
msarettc149f0e2016-01-04 11:35:43 -0800136DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
Mike Reedc665e5b2017-07-04 22:56:06 -0400137DEFINE_bool(csv, false, "Print status in CSV format");
mtklein65dfd2f2016-02-03 10:40:54 -0800138DEFINE_string(sourceType, "",
139 "Apply usual --match rules to source type: bench, gm, skp, image, etc.");
140DEFINE_string(benchType, "",
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400141 "Apply usual --match rules to bench type: micro, recording, piping, playback, skcodec, etc.");
mtklein65dfd2f2016-02-03 10:40:54 -0800142
Mike Reedc928fe22017-06-05 10:20:31 -0400143DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
144
mtkleinbbba1682015-10-28 11:36:30 -0700145static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
146
mtkleinf3723212014-06-25 14:08:00 -0700147static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700148 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800149 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700150}
mtklein55b0ffc2014-07-17 08:38:23 -0700151#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700152
tomhudsond968a6f2015-03-26 11:28:06 -0700153bool Target::init(SkImageInfo info, Benchmark* bench) {
154 if (Benchmark::kRaster_Backend == config.backend) {
reede8f30622016-03-23 18:59:25 -0700155 this->surface = SkSurface::MakeRaster(info);
156 if (!this->surface) {
tomhudsond968a6f2015-03-26 11:28:06 -0700157 return false;
158 }
159 }
160 return true;
161}
162bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700163 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700164 if (!canvas) {
165 return false;
166 }
Mike Reed12e946b2017-04-17 10:53:29 -0400167 bmp->allocPixels(canvas->imageInfo());
168 if (!canvas->readPixels(*bmp, 0, 0)) {
tomhudsond968a6f2015-03-26 11:28:06 -0700169 SkDebugf("Can't read canvas pixels.\n");
170 return false;
171 }
172 return true;
173}
174
175#if SK_SUPPORT_GPU
176struct GPUTarget : public Target {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400177 explicit GPUTarget(const Config& c) : Target(c) {}
178 ContextInfo contextInfo;
179 std::unique_ptr<GrContextFactory> factory;
tomhudsond968a6f2015-03-26 11:28:06 -0700180
181 void setup() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400182 this->contextInfo.testContext()->makeCurrent();
tomhudsond968a6f2015-03-26 11:28:06 -0700183 // Make sure we're done with whatever came before.
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400184 this->contextInfo.testContext()->finish();
tomhudsond968a6f2015-03-26 11:28:06 -0700185 }
186 void endTiming() override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400187 if (this->contextInfo.testContext()) {
188 this->contextInfo.testContext()->waitOnSyncOrSwap();
tomhudsond968a6f2015-03-26 11:28:06 -0700189 }
190 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400191 void fence() override { this->contextInfo.testContext()->finish(); }
mtkleind75c4662015-04-30 07:11:22 -0700192
cdaltond416a5b2015-06-23 13:23:44 -0700193 bool needsFrameTiming(int* maxFrameLag) const override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400194 if (!this->contextInfo.testContext()->getMaxGpuFrameLag(maxFrameLag)) {
cdaltond416a5b2015-06-23 13:23:44 -0700195 // Frame lag is unknown.
196 *maxFrameLag = FLAGS_gpuFrameLag;
197 }
198 return true;
199 }
tomhudsond968a6f2015-03-26 11:28:06 -0700200 bool init(SkImageInfo info, Benchmark* bench) override {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400201 GrContextOptions options = grContextOpts;
202 bench->modifyGrContextOptions(&options);
203 this->factory.reset(new GrContextFactory(options));
bsalomonafcd7cd2015-08-31 12:39:41 -0700204 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
205 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700206 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400207 this->surface = SkSurface::MakeRenderTarget(
208 this->factory->get(this->config.ctxType, this->config.ctxOverrides),
209 SkBudgeted::kNo, info, this->config.samples, &props);
210 this->contextInfo =
211 this->factory->getContextInfo(this->config.ctxType, this->config.ctxOverrides);
tomhudsond968a6f2015-03-26 11:28:06 -0700212 if (!this->surface.get()) {
213 return false;
214 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400215 if (!this->contextInfo.testContext()->fenceSyncSupport()) {
cdaltond416a5b2015-06-23 13:23:44 -0700216 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
svaisanenc47635e2016-01-28 06:05:43 -0800217 "Timings might not be accurate.\n", this->config.name.c_str());
cdaltond416a5b2015-06-23 13:23:44 -0700218 }
tomhudsond968a6f2015-03-26 11:28:06 -0700219 return true;
220 }
221 void fillOptions(ResultsWriter* log) override {
222 const GrGLubyte* version;
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400223 if (this->contextInfo.backend() == kOpenGL_GrBackend) {
Brian Salomon032aaae2018-03-23 16:10:36 -0400224 const GrGLInterface* gl =
225 static_cast<GrGLGpu*>(this->contextInfo.grContext()->contextPriv().getGpu())
226 ->glInterface();
bsalomonc8699322016-05-11 11:55:36 -0700227 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION));
228 log->configOption("GL_VERSION", (const char*)(version));
tomhudsond968a6f2015-03-26 11:28:06 -0700229
bsalomonc8699322016-05-11 11:55:36 -0700230 GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER));
231 log->configOption("GL_RENDERER", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700232
bsalomonc8699322016-05-11 11:55:36 -0700233 GR_GL_CALL_RET(gl, version, GetString(GR_GL_VENDOR));
234 log->configOption("GL_VENDOR", (const char*) version);
tomhudsond968a6f2015-03-26 11:28:06 -0700235
bsalomonc8699322016-05-11 11:55:36 -0700236 GR_GL_CALL_RET(gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
237 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
238 }
tomhudsond968a6f2015-03-26 11:28:06 -0700239 }
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400240
241 void dumpStats() override {
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500242 this->contextInfo.grContext()->contextPriv().printCacheStats();
243 this->contextInfo.grContext()->contextPriv().printGpuStats();
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400244 }
tomhudsond968a6f2015-03-26 11:28:06 -0700245};
mtkleind75c4662015-04-30 07:11:22 -0700246
tomhudsond968a6f2015-03-26 11:28:06 -0700247#endif
248
tomhudson75a0ebb2015-03-27 12:11:44 -0700249static double time(int loops, Benchmark* bench, Target* target) {
250 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700251 if (canvas) {
252 canvas->clear(SK_ColorWHITE);
253 }
joshualitt8a6697a2015-09-30 12:11:07 -0700254 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700255 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700256 canvas = target->beginTiming(canvas);
257 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700258 if (canvas) {
259 canvas->flush();
260 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700261 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700262 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700263 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700264 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700265}
266
mtkleinf3723212014-06-25 14:08:00 -0700267static double estimate_timer_overhead() {
268 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700269 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700270 double start = now_ms();
271 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700272 }
273 return overhead / FLAGS_overheadLoops;
274}
275
reed53249782014-10-10 09:09:52 -0700276static int detect_forever_loops(int loops) {
277 // look for a magic run-forever value
278 if (loops < 0) {
279 loops = SK_MaxS32;
280 }
281 return loops;
282}
283
mtklein55b0ffc2014-07-17 08:38:23 -0700284static int clamp_loops(int loops) {
285 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800286 SkDebugf("ERROR: clamping loops from %d to 1. "
287 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700288 return 1;
289 }
290 if (loops > FLAGS_maxLoops) {
291 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
292 return FLAGS_maxLoops;
293 }
294 return loops;
295}
296
tomhudsond968a6f2015-03-26 11:28:06 -0700297static bool write_canvas_png(Target* target, const SkString& filename) {
298
bsalomon6eb03cc2014-08-07 14:28:50 -0700299 if (filename.isEmpty()) {
300 return false;
301 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700302 if (target->getCanvas() &&
303 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700304 return false;
305 }
tomhudsond968a6f2015-03-26 11:28:06 -0700306
bsalomon6eb03cc2014-08-07 14:28:50 -0700307 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700308
309 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700310 return false;
311 }
tomhudsond968a6f2015-03-26 11:28:06 -0700312
bsalomon6eb03cc2014-08-07 14:28:50 -0700313 SkString dir = SkOSPath::Dirname(filename.c_str());
314 if (!sk_mkdir(dir.c_str())) {
315 SkDebugf("Can't make dir %s.\n", dir.c_str());
316 return false;
317 }
318 SkFILEWStream stream(filename.c_str());
319 if (!stream.isValid()) {
320 SkDebugf("Can't write %s.\n", filename.c_str());
321 return false;
322 }
Hal Canarydb683012016-11-23 08:55:18 -0700323 if (!SkEncodeImage(&stream, bmp, SkEncodedImageFormat::kPNG, 100)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700324 SkDebugf("Can't encode a PNG.\n");
325 return false;
326 }
327 return true;
328}
329
330static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700331static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700332 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700333 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700334 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700335 int loops = bench->calculateLoops(FLAGS_loops);
336 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700337 while (bench_plus_overhead < overhead) {
338 if (round++ == FLAGS_maxCalibrationAttempts) {
339 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700340 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700341 return kFailedLoops;
342 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700343 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700344 }
mtklein2069e222014-08-04 13:57:39 -0700345 }
mtkleinf3723212014-06-25 14:08:00 -0700346
mtkleinbb6a0282014-07-01 08:43:42 -0700347 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700348 // We'll pick N to make timer overhead negligible:
349 //
mtkleinbb6a0282014-07-01 08:43:42 -0700350 // overhead
351 // ------------------------- < FLAGS_overheadGoal
352 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700353 //
Hal Canary55325b72017-01-03 10:36:17 -0500354 // where bench_plus_overhead ~=~ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700355 //
356 // Doing some math, we get:
357 //
mtkleinbb6a0282014-07-01 08:43:42 -0700358 // (overhead / FLAGS_overheadGoal) - overhead
359 // ------------------------------------------ < N
360 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700361 //
362 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700363 if (kAutoTuneLoops == loops) {
364 const double numer = overhead / FLAGS_overheadGoal - overhead;
365 const double denom = bench_plus_overhead - overhead;
366 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700367 loops = clamp_loops(loops);
368 } else {
369 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700370 }
mtkleinbb6a0282014-07-01 08:43:42 -0700371
mtkleinbb6a0282014-07-01 08:43:42 -0700372 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700373}
374
cdaltone1b89582015-06-25 19:17:08 -0700375static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700376 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700377 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700378 if (kAutoTuneLoops == loops) {
379 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700380 double elapsed = 0;
381 do {
mtklein527930f2014-11-06 08:04:34 -0800382 if (1<<30 == loops) {
383 // We're about to wrap. Something's wrong with the bench.
384 loops = 0;
385 break;
386 }
mtkleina189ccd2014-07-14 12:28:47 -0700387 loops *= 2;
388 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700389 // _this_ round, not still timing last round.
390 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700391 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700392 }
393 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700394
mtkleina189ccd2014-07-14 12:28:47 -0700395 // We've overshot at least a little. Scale back linearly.
396 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700397 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700398
tomhudsond968a6f2015-03-26 11:28:06 -0700399 // Make sure we're not still timing our calibration.
400 target->fence();
reed53249782014-10-10 09:09:52 -0700401 } else {
402 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700403 }
mtkleinbb6a0282014-07-01 08:43:42 -0700404 // Pretty much the same deal as the calibration: do some warmup to make
405 // sure we're timing steady-state pipelined frames.
Brian Osman01776982017-10-10 15:41:03 -0400406 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700407 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700408 }
mtkleinbb6a0282014-07-01 08:43:42 -0700409
mtkleinbb6a0282014-07-01 08:43:42 -0700410 return loops;
411}
mtkleinbb6a0282014-07-01 08:43:42 -0700412
bsalomonc2553372014-07-22 13:09:05 -0700413#if SK_SUPPORT_GPU
Brian Salomon6405e712017-03-20 08:54:16 -0400414#define kBogusContextType GrContextFactory::kGL_ContextType
csmartdaltone812d492017-02-21 12:36:05 -0700415#define kBogusContextOverrides GrContextFactory::ContextOverrides::kNone
bsalomonc2553372014-07-22 13:09:05 -0700416#else
bsalomon85b4b532016-04-05 11:06:27 -0700417#define kBogusContextType 0
csmartdaltone812d492017-02-21 12:36:05 -0700418#define kBogusContextOverrides 0
mtkleine714e752014-07-31 12:13:48 -0700419#endif
bsalomonc2553372014-07-22 13:09:05 -0700420
svaisanenc47635e2016-01-28 06:05:43 -0800421static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) {
422
423#if SK_SUPPORT_GPU
424 if (const auto* gpuConfig = config->asConfigGpu()) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400425 if (!FLAGS_gpu) {
426 SkDebugf("Skipping config '%s' as requested.\n", config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800427 return;
Ben Wagner32fa5102017-08-10 21:25:55 -0400428 }
svaisanenc47635e2016-01-28 06:05:43 -0800429
svaisanenc47635e2016-01-28 06:05:43 -0800430 const auto ctxType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700431 const auto ctxOverrides = gpuConfig->getContextOverrides();
svaisanenc47635e2016-01-28 06:05:43 -0800432 const auto sampleCount = gpuConfig->getSamples();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400433 const auto colorType = gpuConfig->getColorType();
434 auto colorSpace = gpuConfig->getColorSpace();
Brian Salomonf865b052018-03-09 09:01:53 -0500435 if (gpuConfig->getSurfType() != SkCommandLineConfigGpu::SurfType::kDefault) {
436 SkDebugf("This tool only supports the default surface type.");
437 return;
438 }
svaisanenc47635e2016-01-28 06:05:43 -0800439
Brian Salomon0b4d8aa2017-10-11 15:34:27 -0400440 GrContextFactory factory(grContextOpts);
441 if (const GrContext* ctx = factory.get(ctxType, ctxOverrides)) {
Greg Daniel81e7bf82017-07-19 14:47:42 -0400442 GrPixelConfig grPixConfig = SkImageInfo2GrPixelConfig(colorType, colorSpace,
443 *ctx->caps());
Brian Salomonbdecacf2018-02-02 20:32:49 -0500444 int supportedSampleCount =
445 ctx->caps()->getRenderTargetSampleCount(sampleCount, grPixConfig);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400446 if (sampleCount != supportedSampleCount) {
Ben Wagner32fa5102017-08-10 21:25:55 -0400447 SkDebugf("Configuration '%s' sample count %d is not a supported sample count.\n",
448 config->getTag().c_str(), sampleCount);
svaisanenc47635e2016-01-28 06:05:43 -0800449 return;
450 }
451 } else {
Ben Wagner32fa5102017-08-10 21:25:55 -0400452 SkDebugf("No context was available matching config '%s'.\n",
453 config->getTag().c_str());
svaisanenc47635e2016-01-28 06:05:43 -0800454 return;
455 }
456
457 Config target = {
kkinnunend4e1c352016-03-01 23:41:26 -0800458 gpuConfig->getTag(),
svaisanenc47635e2016-01-28 06:05:43 -0800459 Benchmark::kGPU_Backend,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400460 colorType,
svaisanenc47635e2016-01-28 06:05:43 -0800461 kPremul_SkAlphaType,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400462 sk_ref_sp(colorSpace),
svaisanenc47635e2016-01-28 06:05:43 -0800463 sampleCount,
464 ctxType,
csmartdaltone812d492017-02-21 12:36:05 -0700465 ctxOverrides,
kkinnunend4e1c352016-03-01 23:41:26 -0800466 gpuConfig->getUseDIText()
467 };
svaisanenc47635e2016-01-28 06:05:43 -0800468
469 configs->push_back(target);
470 return;
471 }
472#endif
473
brianosmanb109b8c2016-06-16 13:03:24 -0700474 #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \
475 if (config->getTag().equals(#name)) { \
Ben Wagner32fa5102017-08-10 21:25:55 -0400476 if (!FLAGS_cpu) { \
477 SkDebugf("Skipping config '%s' as requested.\n", \
478 config->getTag().c_str()); \
479 return; \
480 } \
brianosmanb109b8c2016-06-16 13:03:24 -0700481 Config config = { \
482 SkString(#name), Benchmark::backend, color, alpha, colorSpace, \
csmartdaltone812d492017-02-21 12:36:05 -0700483 0, kBogusContextType, kBogusContextOverrides, false \
brianosmanb109b8c2016-06-16 13:03:24 -0700484 }; \
485 configs->push_back(config); \
486 return; \
mtkleinbb6a0282014-07-01 08:43:42 -0700487 }
mtkleine714e752014-07-31 12:13:48 -0700488
Ben Wagner32fa5102017-08-10 21:25:55 -0400489 CPU_CONFIG(nonrendering, kNonRendering_Backend,
490 kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr)
mtkleinbb6c41b2016-03-08 11:31:11 -0800491
Mike Reede6befa52017-08-28 11:30:48 -0400492 CPU_CONFIG(a8, kRaster_Backend,
493 kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr)
Ben Wagner32fa5102017-08-10 21:25:55 -0400494 CPU_CONFIG(8888, kRaster_Backend,
495 kN32_SkColorType, kPremul_SkAlphaType, nullptr)
496 CPU_CONFIG(565, kRaster_Backend,
497 kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr)
498 auto srgbColorSpace = SkColorSpace::MakeSRGB();
499 CPU_CONFIG(srgb, kRaster_Backend,
500 kN32_SkColorType, kPremul_SkAlphaType, srgbColorSpace)
501 auto srgbLinearColorSpace = SkColorSpace::MakeSRGBLinear();
502 CPU_CONFIG(f16, kRaster_Backend,
503 kRGBA_F16_SkColorType, kPremul_SkAlphaType, srgbLinearColorSpace)
mtkleinbb6a0282014-07-01 08:43:42 -0700504
svaisanenc47635e2016-01-28 06:05:43 -0800505 #undef CPU_CONFIG
Ben Wagner32fa5102017-08-10 21:25:55 -0400506
507 SkDebugf("Unknown config '%s'.\n", config->getTag().c_str());
mtkleinf3723212014-06-25 14:08:00 -0700508}
509
svaisanenc47635e2016-01-28 06:05:43 -0800510// Append all configs that are enabled and supported.
511void create_configs(SkTArray<Config>* configs) {
512 SkCommandLineConfigArray array;
513 ParseConfigs(FLAGS_config, &array);
514 for (int i = 0; i < array.count(); ++i) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400515 create_config(array[i].get(), configs);
svaisanenc47635e2016-01-28 06:05:43 -0800516 }
Ben Wagner32fa5102017-08-10 21:25:55 -0400517
518 // If no just default configs were requested, then we're okay.
519 if (array.count() == 0 || FLAGS_config.count() == 0 ||
520 // If we've been told to ignore undefined flags, we're okay.
521 FLAGS_undefok ||
522 // Otherwise, make sure that all specified configs have been created.
523 array.count() == configs->count()) {
524 return;
525 }
526 SkDebugf("Invalid --config. Use --undefok to bypass this warning.\n");
527 exit(1);
svaisanenc47635e2016-01-28 06:05:43 -0800528}
529
brianosman9f1f6e22016-09-15 08:33:02 -0700530// disable warning : switch statement contains default but no 'case' labels
531#if defined _WIN32
532#pragma warning ( push )
533#pragma warning ( disable : 4065 )
534#endif
535
halcanary96fcdcc2015-08-27 07:41:13 -0700536// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700537static Target* is_enabled(Benchmark* bench, const Config& config) {
538 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700539 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700540 }
541
reede5ea5002014-09-03 11:54:58 -0700542 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
brianosmanb109b8c2016-06-16 13:03:24 -0700543 config.color, config.alpha, config.colorSpace);
bsalomonc2553372014-07-22 13:09:05 -0700544
halcanary96fcdcc2015-08-27 07:41:13 -0700545 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700546
tomhudsond968a6f2015-03-26 11:28:06 -0700547 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700548#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700549 case Benchmark::kGPU_Backend:
550 target = new GPUTarget(config);
551 break;
bsalomonc2553372014-07-22 13:09:05 -0700552#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700553 default:
554 target = new Target(config);
555 break;
556 }
bsalomonc2553372014-07-22 13:09:05 -0700557
tomhudsond968a6f2015-03-26 11:28:06 -0700558 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700559 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700560 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700561 }
562 return target;
563}
564
brianosman9f1f6e22016-09-15 08:33:02 -0700565#if defined _WIN32
566#pragma warning ( pop )
567#endif
568
reed42943c82016-09-12 12:01:44 -0700569static bool valid_brd_bench(sk_sp<SkData> encoded, SkColorType colorType, uint32_t sampleSize,
msarettd1227a72016-05-18 06:23:57 -0700570 uint32_t minOutputSize, int* width, int* height) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400571 std::unique_ptr<SkBitmapRegionDecoder> brd(
msarettd1227a72016-05-18 06:23:57 -0700572 SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy));
msarett7f691442015-09-22 11:56:16 -0700573 if (nullptr == brd.get()) {
574 // This is indicates that subset decoding is not supported for a particular image format.
575 return false;
576 }
577
msarett7f691442015-09-22 11:56:16 -0700578 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
579 (uint32_t) brd->height()) {
580 // This indicates that the image is not large enough to decode a
581 // minOutputSize x minOutputSize subset at the given sampleSize.
582 return false;
583 }
584
585 // Set the image width and height. The calling code will use this to choose subsets to decode.
586 *width = brd->width();
587 *height = brd->height();
588 return true;
589}
590
egdaniel3bf92062015-06-26 08:12:46 -0700591static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700592 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700593}
594
fmalita6519c212016-09-14 08:05:17 -0700595static void collect_files(const SkCommandLineFlags::StringArray& paths, const char* ext,
596 SkTArray<SkString>* list) {
597 for (int i = 0; i < paths.count(); ++i) {
598 if (SkStrEndsWith(paths[i], ext)) {
599 list->push_back(SkString(paths[i]));
600 } else {
601 SkOSFile::Iter it(paths[i], ext);
602 SkString path;
603 while (it.next(&path)) {
604 list->push_back(SkOSPath::Join(paths[i], path.c_str()));
605 }
606 }
607 }
608}
609
mtkleine714e752014-07-31 12:13:48 -0700610class BenchmarkStream {
611public:
mtklein92007582014-08-01 07:46:52 -0700612 BenchmarkStream() : fBenches(BenchRegistry::Head())
613 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700614 , fCurrentRecording(0)
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400615 , fCurrentPiping(0)
Mike Reede45ff462017-12-06 10:47:03 -0500616 , fCurrentDeserialPicture(0)
mtklein92007582014-08-01 07:46:52 -0700617 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800618 , fCurrentSKP(0)
fmalita6519c212016-09-14 08:05:17 -0700619 , fCurrentSVG(0)
msarett95f192d2015-02-13 09:05:41 -0800620 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700621 , fCurrentCodec(0)
msarett84451022016-02-11 06:45:51 -0800622 , fCurrentAndroidCodec(0)
msarett7f691442015-09-22 11:56:16 -0700623 , fCurrentBRDImage(0)
msarett2cee9022016-06-03 08:25:21 -0700624 , fCurrentColorImage(0)
msarett95f192d2015-02-13 09:05:41 -0800625 , fCurrentColorType(0)
msarettc7796b92016-01-07 14:20:20 -0800626 , fCurrentAlphaType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700627 , fCurrentSubsetType(0)
msarett84451022016-02-11 06:45:51 -0800628 , fCurrentSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700629 , fCurrentAnimSKP(0) {
fmalita6519c212016-09-14 08:05:17 -0700630 collect_files(FLAGS_skps, ".skp", &fSKPs);
631 collect_files(FLAGS_svgs, ".svg", &fSVGs);
mtkleine714e752014-07-31 12:13:48 -0700632
mtklein92007582014-08-01 07:46:52 -0700633 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
634 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
635 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
636 exit(1);
637 }
638
639 for (int i = 0; i < FLAGS_scales.count(); i++) {
640 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
641 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
642 exit(1);
643 }
644 }
robertphillips5b693772014-11-21 06:19:36 -0800645
cdalton63a82852015-06-29 14:06:10 -0700646 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
647 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700648 exit(1);
649 }
650
robertphillips5b693772014-11-21 06:19:36 -0800651 if (FLAGS_mpd) {
652 fUseMPDs.push_back() = true;
653 }
mtkleinc751ecb2015-06-15 08:56:38 -0700654 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700655
msarett95f192d2015-02-13 09:05:41 -0800656 // Prepare the images for decoding
msarett69deca82016-04-29 09:38:40 -0700657 if (!CollectImages(FLAGS_images, &fImages)) {
scroggo86737142016-02-03 12:19:11 -0800658 exit(1);
msarett95f192d2015-02-13 09:05:41 -0800659 }
msarett2cee9022016-06-03 08:25:21 -0700660 if (!CollectImages(FLAGS_colorImages, &fColorImages)) {
661 exit(1);
662 }
mtklein95553d92015-03-12 08:24:21 -0700663
msarett95f192d2015-02-13 09:05:41 -0800664 // Choose the candidate color types for image decoding
msarett67cb6662016-06-21 08:49:26 -0700665 fColorTypes.push_back(kN32_SkColorType);
666 if (!FLAGS_simpleCodec) {
667 fColorTypes.push_back(kRGB_565_SkColorType);
668 fColorTypes.push_back(kAlpha_8_SkColorType);
msarett67cb6662016-06-21 08:49:26 -0700669 fColorTypes.push_back(kGray_8_SkColorType);
670 }
mtklein92007582014-08-01 07:46:52 -0700671 }
672
reedca2622b2016-03-18 07:25:55 -0700673 static sk_sp<SkPicture> ReadPicture(const char* path) {
mtkleinfd731ce2014-09-10 12:19:30 -0700674 // Not strictly necessary, as it will be checked again later,
675 // but helps to avoid a lot of pointless work if we're going to skip it.
cdalton91e457d2016-02-17 11:10:16 -0800676 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) {
reedca2622b2016-03-18 07:25:55 -0700677 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700678 }
679
bungemanf93d7112016-09-16 06:24:20 -0700680 std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
681 if (!stream) {
mtkleinfd731ce2014-09-10 12:19:30 -0700682 SkDebugf("Could not read %s.\n", path);
reedca2622b2016-03-18 07:25:55 -0700683 return nullptr;
mtkleinfd731ce2014-09-10 12:19:30 -0700684 }
685
reedca2622b2016-03-18 07:25:55 -0700686 return SkPicture::MakeFromStream(stream.get());
mtkleinfd731ce2014-09-10 12:19:30 -0700687 }
688
fmalita6519c212016-09-14 08:05:17 -0700689 static sk_sp<SkPicture> ReadSVGPicture(const char* path) {
690 SkFILEStream stream(path);
691 if (!stream.isValid()) {
692 SkDebugf("Could not read %s.\n", path);
693 return nullptr;
694 }
695
Hal Canary0f666812018-03-22 15:21:12 -0400696#ifdef SK_XML
fmalitae1baa7c2016-09-14 12:04:30 -0700697 sk_sp<SkSVGDOM> svgDom = SkSVGDOM::MakeFromStream(stream);
fmalita6519c212016-09-14 08:05:17 -0700698 if (!svgDom) {
699 SkDebugf("Could not parse %s.\n", path);
700 return nullptr;
701 }
702
fmalitae1baa7c2016-09-14 12:04:30 -0700703 // Use the intrinsic SVG size if available, otherwise fall back to a default value.
704 static const SkSize kDefaultContainerSize = SkSize::Make(128, 128);
705 if (svgDom->containerSize().isEmpty()) {
706 svgDom->setContainerSize(kDefaultContainerSize);
707 }
708
fmalita6519c212016-09-14 08:05:17 -0700709 SkPictureRecorder recorder;
fmalitae1baa7c2016-09-14 12:04:30 -0700710 svgDom->render(recorder.beginRecording(svgDom->containerSize().width(),
711 svgDom->containerSize().height()));
fmalita6519c212016-09-14 08:05:17 -0700712 return recorder.finishRecordingAsPicture();
Hal Canary0f666812018-03-22 15:21:12 -0400713#else
714 return nullptr;
715#endif // SK_XML
fmalita6519c212016-09-14 08:05:17 -0700716 }
717
mtklein92007582014-08-01 07:46:52 -0700718 Benchmark* next() {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400719 std::unique_ptr<Benchmark> bench;
mtklein65dfd2f2016-02-03 10:40:54 -0800720 do {
721 bench.reset(this->rawNext());
722 if (!bench) {
723 return nullptr;
724 }
725 } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) ||
726 SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType));
mtklein18300a32016-03-16 13:53:35 -0700727 return bench.release();
mtklein65dfd2f2016-02-03 10:40:54 -0800728 }
729
730 Benchmark* rawNext() {
mtkleine714e752014-07-31 12:13:48 -0700731 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700732 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700733 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700734 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700735 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700736 return bench;
737 }
mtklein92007582014-08-01 07:46:52 -0700738
mtkleine714e752014-07-31 12:13:48 -0700739 while (fGMs) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400740 std::unique_ptr<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700741 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800742 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700743 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700744 fBenchType = "micro";
mtklein18300a32016-03-16 13:53:35 -0700745 return new GMBench(gm.release());
mtkleine714e752014-07-31 12:13:48 -0700746 }
747 }
mtklein92007582014-08-01 07:46:52 -0700748
mtkleinfd731ce2014-09-10 12:19:30 -0700749 // First add all .skps as RecordingBenches.
750 while (fCurrentRecording < fSKPs.count()) {
751 const SkString& path = fSKPs[fCurrentRecording++];
reedca2622b2016-03-18 07:25:55 -0700752 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
753 if (!pic) {
mtkleinfd731ce2014-09-10 12:19:30 -0700754 continue;
755 }
756 SkString name = SkOSPath::Basename(path.c_str());
757 fSourceType = "skp";
758 fBenchType = "recording";
Mike Klein88d90712018-01-27 17:30:04 +0000759 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
760 fSKPOps = pic->approximateOpCount();
mtklein8c1a4f82016-08-08 06:56:22 -0700761 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh, FLAGS_lite);
mtkleinfd731ce2014-09-10 12:19:30 -0700762 }
763
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400764 // Add all .skps as PipeBenches.
765 while (fCurrentPiping < fSKPs.count()) {
766 const SkString& path = fSKPs[fCurrentPiping++];
767 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
768 if (!pic) {
769 continue;
770 }
771 SkString name = SkOSPath::Basename(path.c_str());
772 fSourceType = "skp";
773 fBenchType = "piping";
Mike Klein88d90712018-01-27 17:30:04 +0000774 fSKPBytes = static_cast<double>(pic->approximateBytesUsed());
775 fSKPOps = pic->approximateOpCount();
Mike Reed9cdd2ab2016-10-21 10:43:36 -0400776 return new PipingBench(name.c_str(), pic.get());
777 }
778
Mike Reede45ff462017-12-06 10:47:03 -0500779 // Add all .skps as DeserializePictureBenchs.
780 while (fCurrentDeserialPicture < fSKPs.count()) {
781 const SkString& path = fSKPs[fCurrentDeserialPicture++];
782 sk_sp<SkData> data = SkData::MakeFromFileName(path.c_str());
783 if (!data) {
784 continue;
785 }
786 SkString name = SkOSPath::Basename(path.c_str());
787 fSourceType = "skp";
788 fBenchType = "deserial";
Mike Reed7557bbb2017-12-24 19:50:57 -0500789 fSKPBytes = static_cast<double>(data->size());
Mike Klein88d90712018-01-27 17:30:04 +0000790 fSKPOps = 0;
Mike Reede45ff462017-12-06 10:47:03 -0500791 return new DeserializePictureBench(name.c_str(), std::move(data));
792 }
793
mtkleinfd731ce2014-09-10 12:19:30 -0700794 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700795 while (fCurrentScale < fScales.count()) {
796 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800797 const SkString& path = fSKPs[fCurrentSKP];
reedca2622b2016-03-18 07:25:55 -0700798 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
799 if (!pic) {
robertphillips5b693772014-11-21 06:19:36 -0800800 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700801 continue;
802 }
robertphillips5b693772014-11-21 06:19:36 -0800803
804 while (fCurrentUseMPD < fUseMPDs.count()) {
805 if (FLAGS_bbh) {
806 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
807 SkRTreeFactory factory;
808 SkPictureRecorder recorder;
robertphillips5b693772014-11-21 06:19:36 -0800809 pic->playback(recorder.beginRecording(pic->cullRect().width(),
810 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800811 &factory,
robertphillipsdda54452016-07-13 13:27:16 -0700812 0));
reedca2622b2016-03-18 07:25:55 -0700813 pic = recorder.finishRecordingAsPicture();
robertphillips5b693772014-11-21 06:19:36 -0800814 }
815 SkString name = SkOSPath::Basename(path.c_str());
816 fSourceType = "skp";
817 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700818 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
819 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700820 }
robertphillips5b693772014-11-21 06:19:36 -0800821 fCurrentUseMPD = 0;
822 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700823 }
fmalita6519c212016-09-14 08:05:17 -0700824
825 while (fCurrentSVG++ < fSVGs.count()) {
826 const char* path = fSVGs[fCurrentSVG - 1].c_str();
827 if (sk_sp<SkPicture> pic = ReadSVGPicture(path)) {
828 fSourceType = "svg";
829 fBenchType = "playback";
830 return new SKPBench(SkOSPath::Basename(path).c_str(), pic.get(), fClip,
831 fScales[fCurrentScale], false, FLAGS_loopSKP);
832 }
833 }
834
mtklein92007582014-08-01 07:46:52 -0700835 fCurrentSKP = 0;
fmalita6519c212016-09-14 08:05:17 -0700836 fCurrentSVG = 0;
mtklein92007582014-08-01 07:46:52 -0700837 fCurrentScale++;
838 }
839
joshualitt261c3ad2015-04-27 09:16:57 -0700840 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700841 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700842 while (fCurrentAnimSKP < fSKPs.count()) {
843 const SkString& path = fSKPs[fCurrentAnimSKP];
reedca2622b2016-03-18 07:25:55 -0700844 sk_sp<SkPicture> pic = ReadPicture(path.c_str());
845 if (!pic) {
joshualitt261c3ad2015-04-27 09:16:57 -0700846 fCurrentAnimSKP++;
847 continue;
848 }
849
850 fCurrentAnimSKP++;
851 SkString name = SkOSPath::Basename(path.c_str());
Hal Canary2db83612016-11-04 13:02:54 -0400852 sk_sp<SKPAnimationBench::Animation> animation(
cdalton63a82852015-06-29 14:06:10 -0700853 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
Hal Canary2db83612016-11-04 13:02:54 -0400854 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation.get(),
halcanary385fe4d2015-08-26 13:07:48 -0700855 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700856 }
857 }
858
scroggo60869a42015-04-01 12:09:17 -0700859 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700860 fSourceType = "image";
861 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700862 const SkString& path = fImages[fCurrentCodec];
mtklein6f0ff912016-01-11 09:04:21 -0800863 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
864 continue;
865 }
bungeman38d909e2016-08-02 14:40:46 -0700866 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400867 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700868 if (!codec) {
869 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700870 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700871 continue;
872 }
scroggo21027992015-04-02 13:22:38 -0700873
scroggo60869a42015-04-01 12:09:17 -0700874 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700875 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo21027992015-04-02 13:22:38 -0700876
msarettc7796b92016-01-07 14:20:20 -0800877 SkAlphaType alphaType = codec->getInfo().alphaType();
msarett67cb6662016-06-21 08:49:26 -0700878 if (FLAGS_simpleCodec) {
879 if (kUnpremul_SkAlphaType == alphaType) {
880 alphaType = kPremul_SkAlphaType;
881 }
882
883 fCurrentColorType++;
884 } else {
885 switch (alphaType) {
886 case kOpaque_SkAlphaType:
887 // We only need to test one alpha type (opaque).
msarettc7796b92016-01-07 14:20:20 -0800888 fCurrentColorType++;
msarett67cb6662016-06-21 08:49:26 -0700889 break;
890 case kUnpremul_SkAlphaType:
891 case kPremul_SkAlphaType:
892 if (0 == fCurrentAlphaType) {
893 // Test unpremul first.
894 alphaType = kUnpremul_SkAlphaType;
895 fCurrentAlphaType++;
896 } else {
897 // Test premul.
898 alphaType = kPremul_SkAlphaType;
899 fCurrentAlphaType = 0;
900 fCurrentColorType++;
901 }
902 break;
903 default:
904 SkASSERT(false);
905 fCurrentColorType++;
906 break;
907 }
scroggo21027992015-04-02 13:22:38 -0700908 }
909
msarettc7796b92016-01-07 14:20:20 -0800910 // Make sure we can decode to this color type and alpha type.
911 SkImageInfo info =
912 codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType);
scroggo21027992015-04-02 13:22:38 -0700913 const size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400914 SkAutoMalloc storage(info.computeByteSize(rowBytes));
scroggo21027992015-04-02 13:22:38 -0700915
scroggoeb602a52015-07-09 08:16:03 -0700916 const SkCodec::Result result = codec->getPixels(
Leon Scroggins571b30f2017-07-11 17:35:31 +0000917 info, storage.get(), rowBytes);
scroggo60869a42015-04-01 12:09:17 -0700918 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700919 case SkCodec::kSuccess:
920 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700921 return new CodecBench(SkOSPath::Basename(path.c_str()),
bungeman38d909e2016-08-02 14:40:46 -0700922 encoded.get(), colorType, alphaType);
scroggoeb602a52015-07-09 08:16:03 -0700923 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700924 // This is okay. Not all conversions are valid.
925 break;
scroggo60869a42015-04-01 12:09:17 -0700926 default:
927 // This represents some sort of failure.
928 SkASSERT(false);
929 break;
930 }
931 }
932 fCurrentColorType = 0;
933 }
934
msarett84451022016-02-11 06:45:51 -0800935 // Run AndroidCodecBenches
936 const int sampleSizes[] = { 2, 4, 8 };
937 for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) {
938 fSourceType = "image";
939 fBenchType = "skandroidcodec";
940
941 const SkString& path = fImages[fCurrentAndroidCodec];
942 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
943 continue;
944 }
bungeman38d909e2016-08-02 14:40:46 -0700945 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
Mike Reedede7bac2017-07-23 15:30:02 -0400946 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(encoded));
msarett84451022016-02-11 06:45:51 -0800947 if (!codec) {
948 // Nothing to time.
949 SkDebugf("Cannot find codec for %s\n", path.c_str());
950 continue;
951 }
952
953 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
954 int sampleSize = sampleSizes[fCurrentSampleSize];
955 fCurrentSampleSize++;
956 if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) {
957 // Avoid benchmarking scaled decodes of already small images.
958 break;
959 }
960
bungeman38d909e2016-08-02 14:40:46 -0700961 return new AndroidCodecBench(SkOSPath::Basename(path.c_str()),
962 encoded.get(), sampleSize);
msarett84451022016-02-11 06:45:51 -0800963 }
964 fCurrentSampleSize = 0;
965 }
966
msarett7f691442015-09-22 11:56:16 -0700967 // Run the BRDBenches
msarett7f691442015-09-22 11:56:16 -0700968 // We intend to create benchmarks that model the use cases in
969 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
970 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
971 // the image. For that reason, we will benchmark decodes in five representative locations
972 // in the image. Additionally, this use case utilizes power of two scaling, so we will
973 // test on power of two sample sizes. The output tile is always 512x512, so, when a
974 // sampleSize is used, the size of the subset that is decoded is always
975 // (sampleSize*512)x(sampleSize*512).
976 // There are a few good reasons to only test on power of two sample sizes at this time:
msarett7f691442015-09-22 11:56:16 -0700977 // All use cases we are aware of only scale by powers of two.
978 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
979 // these tests are sufficient to provide good coverage of our scaling options.
msarett84451022016-02-11 06:45:51 -0800980 const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 };
msarett7f691442015-09-22 11:56:16 -0700981 const uint32_t minOutputSize = 512;
mtklein6f0ff912016-01-11 09:04:21 -0800982 for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) {
msarettd1227a72016-05-18 06:23:57 -0700983 fSourceType = "image";
984 fBenchType = "BRD";
985
mtklein6f0ff912016-01-11 09:04:21 -0800986 const SkString& path = fImages[fCurrentBRDImage];
987 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
988 continue;
989 }
scroggo860e8a62015-10-15 07:51:28 -0700990
msarettd1227a72016-05-18 06:23:57 -0700991 while (fCurrentColorType < fColorTypes.count()) {
992 while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) {
993 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700994
bungeman38d909e2016-08-02 14:40:46 -0700995 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarettd1227a72016-05-18 06:23:57 -0700996 const SkColorType colorType = fColorTypes[fCurrentColorType];
997 uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize];
998 int currentSubsetType = fCurrentSubsetType++;
scroggo860e8a62015-10-15 07:51:28 -0700999
msarettd1227a72016-05-18 06:23:57 -07001000 int width = 0;
1001 int height = 0;
reed42943c82016-09-12 12:01:44 -07001002 if (!valid_brd_bench(encoded, colorType, sampleSize, minOutputSize,
msarettd1227a72016-05-18 06:23:57 -07001003 &width, &height)) {
1004 break;
msarett7f691442015-09-22 11:56:16 -07001005 }
msarettd1227a72016-05-18 06:23:57 -07001006
1007 SkString basename = SkOSPath::Basename(path.c_str());
1008 SkIRect subset;
1009 const uint32_t subsetSize = sampleSize * minOutputSize;
1010 switch (currentSubsetType) {
1011 case kTopLeft_SubsetType:
1012 basename.append("_TopLeft");
1013 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
1014 break;
1015 case kTopRight_SubsetType:
1016 basename.append("_TopRight");
1017 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
1018 subsetSize);
1019 break;
1020 case kMiddle_SubsetType:
1021 basename.append("_Middle");
1022 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
1023 (height - subsetSize) / 2, subsetSize, subsetSize);
1024 break;
1025 case kBottomLeft_SubsetType:
1026 basename.append("_BottomLeft");
1027 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
1028 subsetSize);
1029 break;
1030 case kBottomRight_SubsetType:
1031 basename.append("_BottomRight");
1032 subset = SkIRect::MakeXYWH(width - subsetSize,
1033 height - subsetSize, subsetSize, subsetSize);
1034 break;
1035 default:
1036 SkASSERT(false);
1037 }
1038
1039 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
1040 colorType, sampleSize, subset);
msarett7f691442015-09-22 11:56:16 -07001041 }
msarettd1227a72016-05-18 06:23:57 -07001042 fCurrentSubsetType = 0;
1043 fCurrentSampleSize++;
msarett7f691442015-09-22 11:56:16 -07001044 }
msarettd1227a72016-05-18 06:23:57 -07001045 fCurrentSampleSize = 0;
1046 fCurrentColorType++;
msarett7f691442015-09-22 11:56:16 -07001047 }
msarettd1227a72016-05-18 06:23:57 -07001048 fCurrentColorType = 0;
msarett7f691442015-09-22 11:56:16 -07001049 }
1050
msarett2cee9022016-06-03 08:25:21 -07001051 while (fCurrentColorImage < fColorImages.count()) {
1052 fSourceType = "colorimage";
1053 fBenchType = "skcolorcodec";
1054 const SkString& path = fColorImages[fCurrentColorImage];
1055 fCurrentColorImage++;
1056 sk_sp<SkData> encoded = SkData::MakeFromFileName(path.c_str());
1057 if (encoded) {
1058 return new ColorCodecBench(SkOSPath::Basename(path.c_str()).c_str(),
1059 std::move(encoded));
1060 } else {
1061 SkDebugf("Could not read file %s.\n", path.c_str());
1062 }
1063 }
1064
halcanary96fcdcc2015-08-27 07:41:13 -07001065 return nullptr;
mtkleine714e752014-07-31 12:13:48 -07001066 }
mtklein92007582014-08-01 07:46:52 -07001067
1068 void fillCurrentOptions(ResultsWriter* log) const {
1069 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -07001070 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -07001071 if (0 == strcmp(fSourceType, "skp")) {
1072 log->configOption("clip",
1073 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
1074 fClip.fRight, fClip.fBottom).c_str());
djsollenf2b340f2016-01-29 08:51:04 -08001075 SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -07001076 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -08001077 if (fCurrentUseMPD > 0) {
1078 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
1079 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
1080 }
mtklein92007582014-08-01 07:46:52 -07001081 }
mtklein051e56d2014-12-04 08:46:51 -08001082 if (0 == strcmp(fBenchType, "recording")) {
Mike Reed7557bbb2017-12-24 19:50:57 -05001083 log->metric("bytes", fSKPBytes);
Mike Klein88d90712018-01-27 17:30:04 +00001084 log->metric("ops", fSKPOps);
mtklein051e56d2014-12-04 08:46:51 -08001085 }
mtklein92007582014-08-01 07:46:52 -07001086 }
1087
mtkleine714e752014-07-31 12:13:48 -07001088private:
msarettb23e6aa2015-06-09 13:56:10 -07001089 enum SubsetType {
1090 kTopLeft_SubsetType = 0,
1091 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -07001092 kMiddle_SubsetType = 2,
1093 kBottomLeft_SubsetType = 3,
1094 kBottomRight_SubsetType = 4,
1095 kTranslate_SubsetType = 5,
1096 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -07001097 kLast_SubsetType = kZoom_SubsetType,
1098 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -07001099 };
1100
mtkleine714e752014-07-31 12:13:48 -07001101 const BenchRegistry* fBenches;
1102 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -07001103 SkIRect fClip;
1104 SkTArray<SkScalar> fScales;
1105 SkTArray<SkString> fSKPs;
fmalita6519c212016-09-14 08:05:17 -07001106 SkTArray<SkString> fSVGs;
robertphillips5b693772014-11-21 06:19:36 -08001107 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -08001108 SkTArray<SkString> fImages;
msarett2cee9022016-06-03 08:25:21 -07001109 SkTArray<SkString> fColorImages;
msarett74deb982015-10-20 16:45:56 -07001110 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -07001111 SkScalar fZoomMax;
1112 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -07001113
Mike Klein88d90712018-01-27 17:30:04 +00001114 double fSKPBytes, fSKPOps;
mtklein051e56d2014-12-04 08:46:51 -08001115
mtkleinfd731ce2014-09-10 12:19:30 -07001116 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
1117 const char* fBenchType; // How we bench it: micro, recording, playback, ...
1118 int fCurrentRecording;
Mike Reed9cdd2ab2016-10-21 10:43:36 -04001119 int fCurrentPiping;
Mike Reede45ff462017-12-06 10:47:03 -05001120 int fCurrentDeserialPicture;
mtklein92007582014-08-01 07:46:52 -07001121 int fCurrentScale;
1122 int fCurrentSKP;
fmalita6519c212016-09-14 08:05:17 -07001123 int fCurrentSVG;
robertphillips5b693772014-11-21 06:19:36 -08001124 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -07001125 int fCurrentCodec;
msarett84451022016-02-11 06:45:51 -08001126 int fCurrentAndroidCodec;
msarett7f691442015-09-22 11:56:16 -07001127 int fCurrentBRDImage;
msarett2cee9022016-06-03 08:25:21 -07001128 int fCurrentColorImage;
msarett95f192d2015-02-13 09:05:41 -08001129 int fCurrentColorType;
msarettc7796b92016-01-07 14:20:20 -08001130 int fCurrentAlphaType;
msarettb23e6aa2015-06-09 13:56:10 -07001131 int fCurrentSubsetType;
msarett84451022016-02-11 06:45:51 -08001132 int fCurrentSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -07001133 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -07001134};
1135
msarettc149f0e2016-01-04 11:35:43 -08001136// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1137// This prints something every once in a while so that it knows we're still working.
1138static void start_keepalive() {
Mike Klein03141d22017-10-30 11:57:15 -04001139 static std::thread* intentionallyLeaked = new std::thread([]{
1140 for (;;) {
1141 static const int kSec = 1200;
1142 #if defined(SK_BUILD_FOR_WIN)
1143 Sleep(kSec * 1000);
1144 #else
1145 sleep(kSec);
1146 #endif
1147 SkDebugf("\nBenchmarks still running...\n");
msarettc149f0e2016-01-04 11:35:43 -08001148 }
Mike Klein03141d22017-10-30 11:57:15 -04001149 });
1150 (void)intentionallyLeaked;
msarettc149f0e2016-01-04 11:35:43 -08001151}
1152
Mike Kleinbe28ee22017-02-06 12:46:20 -05001153int main(int argc, char** argv) {
1154 SkCommandLineFlags::Parse(argc, argv);
Brian Osman53136aa2017-07-20 15:43:35 -04001155
Brian Osmanbc8150f2017-07-24 11:38:01 -04001156 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -04001157
Mike Kleinadacaef2017-02-06 09:26:14 -05001158#if defined(SK_BUILD_FOR_IOS)
1159 cd_Documents();
1160#endif
jcgregorio3b27ade2014-11-13 08:06:40 -08001161 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001162 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001163 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001164
krajcevski69a55602014-08-13 10:46:31 -07001165#if SK_SUPPORT_GPU
Chris Dalton040238b2017-12-18 14:22:34 -07001166 SetCtxOptionsFromCommonFlags(&grContextOpts);
krajcevski69a55602014-08-13 10:46:31 -07001167#endif
1168
bsalomon06cddec2014-10-24 10:40:50 -07001169 if (FLAGS_veryVerbose) {
1170 FLAGS_verbose = true;
1171 }
1172
bsalomon6eb03cc2014-08-07 14:28:50 -07001173 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001174 FLAGS_samples = 1;
1175 FLAGS_gpuFrameLag = 0;
1176 }
1177
bsalomon6eb03cc2014-08-07 14:28:50 -07001178 if (!FLAGS_writePath.isEmpty()) {
1179 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1180 if (!sk_mkdir(FLAGS_writePath[0])) {
1181 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001182 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001183 }
1184 }
1185
Ben Wagner145dbcd2016-11-03 14:40:50 -04001186 std::unique_ptr<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001187 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein53520152016-01-20 09:53:59 -08001188#if defined(SK_RELEASE)
halcanary385fe4d2015-08-26 13:07:48 -07001189 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein53520152016-01-20 09:53:59 -08001190#else
1191 SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
1192 return 1;
1193#endif
mtklein60317d0f2014-07-14 11:30:37 -07001194 }
mtklein1915b622014-08-20 11:45:00 -07001195
1196 if (1 == FLAGS_properties.count() % 2) {
1197 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1198 return 1;
1199 }
1200 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1201 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1202 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001203
1204 if (1 == FLAGS_key.count() % 2) {
1205 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1206 return 1;
1207 }
1208 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001209 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001210 }
mtklein60317d0f2014-07-14 11:30:37 -07001211
mtkleinf3723212014-06-25 14:08:00 -07001212 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001213 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001214
cdaltone1b89582015-06-25 19:17:08 -07001215 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001216
bsalomon6eb03cc2014-08-07 14:28:50 -07001217 if (kAutoTuneLoops != FLAGS_loops) {
1218 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001219 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001220 SkDebugf("! -> high variance, ? -> moderate variance\n");
1221 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001222 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001223 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001224 } else {
mtkleind75c4662015-04-30 07:11:22 -07001225 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001226 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001227 }
1228
svaisanenc47635e2016-01-28 06:05:43 -08001229 SkTArray<Config> configs;
bsalomonc2553372014-07-22 13:09:05 -07001230 create_configs(&configs);
1231
msarettc149f0e2016-01-04 11:35:43 -08001232 if (FLAGS_keepAlive) {
1233 start_keepalive();
1234 }
1235
Yuqian Liba62b4a2016-12-14 13:46:53 -05001236 gSkUseAnalyticAA = FLAGS_analyticAA;
Yuqian Lidf60e362017-07-25 11:26:31 -04001237 gSkUseDeltaAA = FLAGS_deltaAA;
liyuqian38911a72016-10-04 11:23:22 -07001238
Yuqian Lidf60e362017-07-25 11:26:31 -04001239 if (FLAGS_forceDeltaAA) {
1240 gSkForceDeltaAA = true;
1241 }
Yuqian Li550148b2017-01-13 10:13:13 -05001242 if (FLAGS_forceAnalyticAA) {
1243 gSkForceAnalyticAA = true;
1244 }
Mike Reedc928fe22017-06-05 10:20:31 -04001245 if (FLAGS_forceRasterPipeline) {
1246 gSkForceRasterPipelineBlitter = true;
1247 }
Yuqian Li550148b2017-01-13 10:13:13 -05001248
mtkleine070c2b2014-10-14 08:40:43 -07001249 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001250 BenchmarkStream benchStream;
1251 while (Benchmark* b = benchStream.next()) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001252 std::unique_ptr<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001253 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001254 continue;
1255 }
1256
svaisanenc47635e2016-01-28 06:05:43 -08001257 if (!configs.empty()) {
mtklein96289052014-09-10 12:05:59 -07001258 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001259 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001260 }
egdaniel3bf92062015-06-26 08:12:46 -07001261 for (int i = 0; i < configs.count(); ++i) {
1262 Target* target = is_enabled(b, configs[i]);
1263 if (!target) {
1264 continue;
1265 }
mtkleinf3723212014-06-25 14:08:00 -07001266
halcanary96fcdcc2015-08-27 07:41:13 -07001267 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001268 SkCanvas* canvas = target->getCanvas();
svaisanenc47635e2016-01-28 06:05:43 -08001269 const char* config = target->config.name.c_str();
egdaniel3bf92062015-06-26 08:12:46 -07001270
brianosman7a5ada82016-02-08 13:49:12 -08001271 if (FLAGS_pre_log || FLAGS_dryRun) {
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001272 SkDebugf("Running %s\t%s\n"
1273 , bench->getUniqueName()
1274 , config);
brianosman7a5ada82016-02-08 13:49:12 -08001275 if (FLAGS_dryRun) {
1276 continue;
1277 }
benjaminwagner8d61f0d2016-01-25 13:02:40 -08001278 }
1279
Brian Osman5ad87aa2017-09-08 16:05:23 -04001280 TRACE_EVENT2("skia", "Benchmark", "name", TRACE_STR_COPY(bench->getUniqueName()),
1281 "config", TRACE_STR_COPY(config));
Brian Osmanc3cdef52017-08-31 14:09:22 -04001282
egdaniel3bf92062015-06-26 08:12:46 -07001283 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001284 bench->perCanvasPreDraw(canvas);
1285
cdaltone1b89582015-06-25 19:17:08 -07001286 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001287 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001288 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1289 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001290
Leon Scroggins IIIb30d1132017-10-02 09:48:15 -04001291 if (kFailedLoops == loops) {
1292 // Can't be timed. A warning note has already been printed.
1293 cleanup_run(target);
1294 continue;
1295 }
1296
Yuqian Li3528eb32017-09-21 13:23:49 -04001297 if (runs == 0 && FLAGS_ms < 1000) {
1298 // Run the first bench for 1000ms to warm up the nanobench if FLAGS_ms < 1000.
1299 // Otherwise, the first few benches' measurements will be inaccurate.
1300 auto stop = now_ms() + 1000;
1301 do {
1302 time(loops, bench.get(), target);
1303 } while (now_ms() < stop);
1304 }
1305
mtkleinbbba1682015-10-28 11:36:30 -07001306 if (FLAGS_ms) {
1307 samples.reset();
1308 auto stop = now_ms() + FLAGS_ms;
1309 do {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001310 samples.push_back(time(loops, bench.get(), target) / loops);
mtkleinbbba1682015-10-28 11:36:30 -07001311 } while (now_ms() < stop);
1312 } else {
cdaltone1b89582015-06-25 19:17:08 -07001313 samples.reset(FLAGS_samples);
1314 for (int s = 0; s < FLAGS_samples; s++) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001315 samples[s] = time(loops, bench.get(), target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001316 }
cdaltone1b89582015-06-25 19:17:08 -07001317 }
mtkleinf3723212014-06-25 14:08:00 -07001318
joshualitte45c81c2015-12-02 09:05:37 -08001319#if SK_SUPPORT_GPU
1320 SkTArray<SkString> keys;
1321 SkTArray<double> values;
1322 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1323 if (gpuStatsDump) {
1324 // TODO cache stats
1325 bench->getGpuStats(canvas, &keys, &values);
1326 }
1327#endif
1328
robertphillips5b693772014-11-21 06:19:36 -08001329 bench->perCanvasPostDraw(canvas);
1330
egdaniel3bf92062015-06-26 08:12:46 -07001331 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001332 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001333 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001334 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001335 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001336 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001337 }
1338
cdaltone1b89582015-06-25 19:17:08 -07001339 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001340 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001341 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001342 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001343 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001344 log->metric("min_ms", stats.min);
George Burgess IV75b57182016-12-06 10:53:52 -08001345 log->metrics("samples", samples);
joshualitte45c81c2015-12-02 09:05:37 -08001346#if SK_SUPPORT_GPU
1347 if (gpuStatsDump) {
1348 // dump to json, only SKPBench currently returns valid keys / values
1349 SkASSERT(keys.count() == values.count());
1350 for (int i = 0; i < keys.count(); i++) {
1351 log->metric(keys[i].c_str(), values[i]);
1352 }
1353 }
1354#endif
1355
mtkleine070c2b2014-10-14 08:40:43 -07001356 if (runs++ % FLAGS_flushEvery == 0) {
1357 log->flush();
1358 }
mtklein60317d0f2014-07-14 11:30:37 -07001359
bsalomon6eb03cc2014-08-07 14:28:50 -07001360 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001361 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001362 config = ""; // Only print the config if we run the same bench on more than one.
1363 }
mtkleind75c4662015-04-30 07:11:22 -07001364 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1365 , sk_tools::getCurrResidentSetSizeMB()
1366 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001367 , bench->getUniqueName()
1368 , config);
mtkleinf3723212014-06-25 14:08:00 -07001369 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001370 const char* mark = " ";
1371 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1372 if (stddev_percent > 5) mark = "?";
1373 if (stddev_percent > 10) mark = "!";
1374
1375 SkDebugf("%10.2f %s\t%s\t%s\n",
1376 stats.median*1e3, mark, bench->getUniqueName(), config);
Mike Reedc665e5b2017-07-04 22:56:06 -04001377 } else if (FLAGS_csv) {
mtkleinf3723212014-06-25 14:08:00 -07001378 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
Mike Reedc665e5b2017-07-04 22:56:06 -04001379 SkDebugf("%g,%g,%g,%g,%g,%s,%s\n"
1380 , stats.min
1381 , stats.median
1382 , stats.mean
1383 , stats.max
1384 , stddev_percent
1385 , config
1386 , bench->getUniqueName()
1387 );
1388 } else {
1389 const char* format = "%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n";
1390 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1391 SkDebugf(format
mtkleind75c4662015-04-30 07:11:22 -07001392 , sk_tools::getCurrResidentSetSizeMB()
1393 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001394 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001395 , HUMANIZE(stats.min)
1396 , HUMANIZE(stats.median)
1397 , HUMANIZE(stats.mean)
1398 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001399 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001400 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001401 , config
mtklein96289052014-09-10 12:05:59 -07001402 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001403 );
1404 }
joshualitte45c81c2015-12-02 09:05:37 -08001405
bsalomonb12ea412015-02-02 21:19:50 -08001406#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001407 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
Brian Salomon0b4d8aa2017-10-11 15:34:27 -04001408 target->dumpStats();
bsalomon06cddec2014-10-24 10:40:50 -07001409 }
1410#endif
joshualitte45c81c2015-12-02 09:05:37 -08001411
cdalton2c56ba52015-06-26 13:32:53 -07001412 if (FLAGS_verbose) {
1413 SkDebugf("Samples: ");
1414 for (int i = 0; i < samples.count(); i++) {
1415 SkDebugf("%s ", HUMANIZE(samples[i]));
1416 }
1417 SkDebugf("%s\n", bench->getUniqueName());
1418 }
egdaniel3bf92062015-06-26 08:12:46 -07001419 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001420 }
mtkleinf3723212014-06-25 14:08:00 -07001421 }
1422
Herb Derby5a523fe2017-01-26 16:48:28 -05001423 SkGraphics::PurgeAllCaches();
1424
mtkleine1091452014-12-04 10:47:02 -08001425 log->bench("memory_usage", 0,0);
1426 log->config("meta");
1427 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1428
mtkleinf3723212014-06-25 14:08:00 -07001429 return 0;
1430}