mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 8 | #include <ctype.h> |
| 9 | |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 10 | #include "nanobench.h" |
| 11 | |
msarett | 8445102 | 2016-02-11 06:45:51 -0800 | [diff] [blame] | 12 | #include "AndroidCodecBench.h" |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 13 | #include "Benchmark.h" |
msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 14 | #include "BitmapRegionDecoderBench.h" |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 15 | #include "CodecBench.h" |
msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 16 | #include "CodecBenchPriv.h" |
msarett | 2cee902 | 2016-06-03 08:25:21 -0700 | [diff] [blame] | 17 | #include "ColorCodecBench.h" |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 18 | #include "CrashHandler.h" |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 19 | #include "GMBench.h" |
mtklein | afb4379 | 2014-08-19 15:55:55 -0700 | [diff] [blame] | 20 | #include "ProcStats.h" |
mtklein | 60317d0f | 2014-07-14 11:30:37 -0700 | [diff] [blame] | 21 | #include "ResultsWriter.h" |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 22 | #include "RecordingBench.h" |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 23 | #include "SKPAnimationBench.h" |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 24 | #include "SKPBench.h" |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 25 | #include "Stats.h" |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 26 | |
msarett | 8445102 | 2016-02-11 06:45:51 -0800 | [diff] [blame] | 27 | #include "SkAndroidCodec.h" |
msarett | 5cb4885 | 2015-11-06 08:56:32 -0800 | [diff] [blame] | 28 | #include "SkBitmapRegionDecoder.h" |
mtklein | 6838d85 | 2014-10-29 14:15:10 -0700 | [diff] [blame] | 29 | #include "SkBBoxHierarchy.h" |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 30 | #include "SkCanvas.h" |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 31 | #include "SkCodec.h" |
caryclark | 17f0b6d | 2014-07-22 10:15:34 -0700 | [diff] [blame] | 32 | #include "SkCommonFlags.h" |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 33 | #include "SkCommonFlagsConfig.h" |
msarett | 95f192d | 2015-02-13 09:05:41 -0800 | [diff] [blame] | 34 | #include "SkData.h" |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 35 | #include "SkForceLinking.h" |
| 36 | #include "SkGraphics.h" |
halcanary | 4dbbd04 | 2016-06-07 17:21:10 -0700 | [diff] [blame] | 37 | #include "SkLeanWindows.h" |
mtklein | 2084050 | 2014-08-21 15:51:22 -0700 | [diff] [blame] | 38 | #include "SkOSFile.h" |
| 39 | #include "SkPictureRecorder.h" |
mtklein | 051e56d | 2014-12-04 08:46:51 -0800 | [diff] [blame] | 40 | #include "SkPictureUtils.h" |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 41 | #include "SkString.h" |
| 42 | #include "SkSurface.h" |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 43 | #include "SkTaskGroup.h" |
msarett | c149f0e | 2016-01-04 11:35:43 -0800 | [diff] [blame] | 44 | #include "SkThreadUtils.h" |
joshualitt | 3ebd050 | 2016-02-09 07:18:08 -0800 | [diff] [blame] | 45 | #include "ThermalManager.h" |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 46 | |
bungeman | 60e0fee | 2015-08-26 05:15:46 -0700 | [diff] [blame] | 47 | #include <stdlib.h> |
| 48 | |
scroggo | 38ce0a7 | 2016-01-07 07:28:47 -0800 | [diff] [blame] | 49 | #ifndef SK_BUILD_FOR_WIN32 |
| 50 | #include <unistd.h> |
| 51 | #endif |
| 52 | |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 53 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 54 | #include "nanobenchAndroid.h" |
| 55 | #endif |
| 56 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 57 | #if SK_SUPPORT_GPU |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 58 | #include "gl/GrGLDefines.h" |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 59 | #include "GrCaps.h" |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 60 | #include "GrContextFactory.h" |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 61 | #include "gl/GrGLUtil.h" |
| 62 | using sk_gpu_test::GrContextFactory; |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 63 | using sk_gpu_test::TestContext; |
krajcevski | 69a5560 | 2014-08-13 10:46:31 -0700 | [diff] [blame] | 64 | SkAutoTDelete<GrContextFactory> gGrFactory; |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 65 | #endif |
| 66 | |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 67 | struct GrContextOptions; |
| 68 | |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 69 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 70 | |
reed | 5324978 | 2014-10-10 09:09:52 -0700 | [diff] [blame] | 71 | static const int kAutoTuneLoops = 0; |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 72 | |
mtklein | b511042 | 2014-08-07 15:20:02 -0700 | [diff] [blame] | 73 | static const int kDefaultLoops = |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 74 | #ifdef SK_DEBUG |
| 75 | 1; |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 76 | #else |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 77 | kAutoTuneLoops; |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 78 | #endif |
| 79 | |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 80 | static SkString loops_help_txt() { |
| 81 | SkString help; |
| 82 | help.printf("Number of times to run each bench. Set this to %d to auto-" |
| 83 | "tune for each bench. Timings are only reported when auto-tuning.", |
| 84 | kAutoTuneLoops); |
| 85 | return help; |
| 86 | } |
| 87 | |
cdalton | e1b8958 | 2015-06-25 19:17:08 -0700 | [diff] [blame] | 88 | static SkString to_string(int n) { |
| 89 | SkString str; |
| 90 | str.appendS32(n); |
| 91 | return str; |
| 92 | } |
| 93 | |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 94 | DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str()); |
| 95 | |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 96 | DEFINE_int32(samples, 10, "Number of samples to measure for each bench."); |
mtklein | bbba168 | 2015-10-28 11:36:30 -0700 | [diff] [blame] | 97 | DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples."); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 98 | DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead."); |
| 99 | DEFINE_double(overheadGoal, 0.0001, |
| 100 | "Loop until timer overhead is at most this fraction of our measurments."); |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 101 | DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU."); |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 102 | DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag."); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 103 | |
mtklein | 60317d0f | 2014-07-14 11:30:37 -0700 | [diff] [blame] | 104 | DEFINE_string(outResultsFile, "", "If given, write results here as JSON."); |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 105 | DEFINE_int32(maxCalibrationAttempts, 3, |
| 106 | "Try up to this many times to guess loops for a bench, or skip the bench."); |
| 107 | DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this."); |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 108 | DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs."); |
| 109 | DEFINE_string(scales, "1.0", "Space-separated scales for SKPs."); |
cdalton | 63a8285 | 2015-06-29 14:06:10 -0700 | [diff] [blame] | 110 | DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom " |
| 111 | "function that ping-pongs between 1.0 and zoomMax."); |
mtklein | 2084050 | 2014-08-21 15:51:22 -0700 | [diff] [blame] | 112 | DEFINE_bool(bbh, true, "Build a BBH for SKPs?"); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 113 | DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?"); |
cdalton | b402296 | 2015-06-25 10:51:56 -0700 | [diff] [blame] | 114 | DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?"); |
mtklein | e070c2b | 2014-10-14 08:40:43 -0700 | [diff] [blame] | 115 | DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run."); |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 116 | DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test."); |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 117 | DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?"); |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 118 | DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json"); |
msarett | c149f0e | 2016-01-04 11:35:43 -0800 | [diff] [blame] | 119 | DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out"); |
joshualitt | 3ebd050 | 2016-02-09 07:18:08 -0800 | [diff] [blame] | 120 | DEFINE_string(useThermalManager, "0,1,10,1000", "enabled,threshold,sleepTimeMs,TimeoutMs for " |
| 121 | "thermalManager\n"); |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 122 | |
mtklein | 65dfd2f | 2016-02-03 10:40:54 -0800 | [diff] [blame] | 123 | DEFINE_string(sourceType, "", |
| 124 | "Apply usual --match rules to source type: bench, gm, skp, image, etc."); |
| 125 | DEFINE_string(benchType, "", |
| 126 | "Apply usual --match rules to bench type: micro, recording, playback, skcodec, etc."); |
| 127 | |
mtklein | bbba168 | 2015-10-28 11:36:30 -0700 | [diff] [blame] | 128 | static double now_ms() { return SkTime::GetNSecs() * 1e-6; } |
| 129 | |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 130 | static SkString humanize(double ms) { |
mtklein | dc5bbab | 2014-09-24 06:34:09 -0700 | [diff] [blame] | 131 | if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 132 | return HumanizeMs(ms); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 133 | } |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 134 | #define HUMANIZE(ms) humanize(ms).c_str() |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 135 | |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 136 | bool Target::init(SkImageInfo info, Benchmark* bench) { |
| 137 | if (Benchmark::kRaster_Backend == config.backend) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 138 | this->surface = SkSurface::MakeRaster(info); |
| 139 | if (!this->surface) { |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 140 | return false; |
| 141 | } |
| 142 | } |
| 143 | return true; |
| 144 | } |
| 145 | bool Target::capturePixels(SkBitmap* bmp) { |
tomhudson | 75a0ebb | 2015-03-27 12:11:44 -0700 | [diff] [blame] | 146 | SkCanvas* canvas = this->getCanvas(); |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 147 | if (!canvas) { |
| 148 | return false; |
| 149 | } |
| 150 | bmp->setInfo(canvas->imageInfo()); |
| 151 | if (!canvas->readPixels(bmp, 0, 0)) { |
| 152 | SkDebugf("Can't read canvas pixels.\n"); |
| 153 | return false; |
| 154 | } |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | #if SK_SUPPORT_GPU |
| 159 | struct GPUTarget : public Target { |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 160 | explicit GPUTarget(const Config& c) : Target(c), context(nullptr) { } |
| 161 | TestContext* context; |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 162 | |
| 163 | void setup() override { |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 164 | this->context->makeCurrent(); |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 165 | // Make sure we're done with whatever came before. |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 166 | this->context->finish(); |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 167 | } |
| 168 | void endTiming() override { |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 169 | if (this->context) { |
| 170 | this->context->waitOnSyncOrSwap(); |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | void fence() override { |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 174 | this->context->finish(); |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 175 | } |
mtklein | d75c466 | 2015-04-30 07:11:22 -0700 | [diff] [blame] | 176 | |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 177 | bool needsFrameTiming(int* maxFrameLag) const override { |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 178 | if (!this->context->getMaxGpuFrameLag(maxFrameLag)) { |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 179 | // Frame lag is unknown. |
| 180 | *maxFrameLag = FLAGS_gpuFrameLag; |
| 181 | } |
| 182 | return true; |
| 183 | } |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 184 | bool init(SkImageInfo info, Benchmark* bench) override { |
bsalomon | afcd7cd | 2015-08-31 12:39:41 -0700 | [diff] [blame] | 185 | uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag : |
| 186 | 0; |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 187 | SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 188 | this->surface = SkSurface::MakeRenderTarget(gGrFactory->get(this->config.ctxType, |
| 189 | this->config.ctxOptions), |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 190 | SkBudgeted::kNo, info, |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 191 | this->config.samples, &props); |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 192 | this->context = gGrFactory->getContextInfo(this->config.ctxType, |
| 193 | this->config.ctxOptions).testContext(); |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 194 | if (!this->surface.get()) { |
| 195 | return false; |
| 196 | } |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 197 | if (!this->context->fenceSyncSupport()) { |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 198 | SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. " |
svaisanen | c47635e | 2016-01-28 06:05:43 -0800 | [diff] [blame] | 199 | "Timings might not be accurate.\n", this->config.name.c_str()); |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 200 | } |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 201 | return true; |
| 202 | } |
| 203 | void fillOptions(ResultsWriter* log) override { |
| 204 | const GrGLubyte* version; |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 205 | if (this->context->backend() == kOpenGL_GrBackend) { |
| 206 | const GrGLInterface* gl = |
| 207 | reinterpret_cast<const GrGLInterface*>(this->context->backendContext()); |
| 208 | GR_GL_CALL_RET(gl, version, GetString(GR_GL_VERSION)); |
| 209 | log->configOption("GL_VERSION", (const char*)(version)); |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 210 | |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 211 | GR_GL_CALL_RET(gl, version, GetString(GR_GL_RENDERER)); |
| 212 | log->configOption("GL_RENDERER", (const char*) version); |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 213 | |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 214 | GR_GL_CALL_RET(gl, version, GetString(GR_GL_VENDOR)); |
| 215 | log->configOption("GL_VENDOR", (const char*) version); |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 216 | |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 217 | GR_GL_CALL_RET(gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION)); |
| 218 | log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version); |
| 219 | } |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 220 | } |
| 221 | }; |
mtklein | d75c466 | 2015-04-30 07:11:22 -0700 | [diff] [blame] | 222 | |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 223 | #endif |
| 224 | |
tomhudson | 75a0ebb | 2015-03-27 12:11:44 -0700 | [diff] [blame] | 225 | static double time(int loops, Benchmark* bench, Target* target) { |
| 226 | SkCanvas* canvas = target->getCanvas(); |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 227 | if (canvas) { |
| 228 | canvas->clear(SK_ColorWHITE); |
| 229 | } |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 230 | bench->preDraw(canvas); |
mtklein | bbba168 | 2015-10-28 11:36:30 -0700 | [diff] [blame] | 231 | double start = now_ms(); |
tomhudson | 75a0ebb | 2015-03-27 12:11:44 -0700 | [diff] [blame] | 232 | canvas = target->beginTiming(canvas); |
| 233 | bench->draw(loops, canvas); |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 234 | if (canvas) { |
| 235 | canvas->flush(); |
| 236 | } |
tomhudson | 75a0ebb | 2015-03-27 12:11:44 -0700 | [diff] [blame] | 237 | target->endTiming(); |
mtklein | bbba168 | 2015-10-28 11:36:30 -0700 | [diff] [blame] | 238 | double elapsed = now_ms() - start; |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 239 | bench->postDraw(canvas); |
mtklein | bbba168 | 2015-10-28 11:36:30 -0700 | [diff] [blame] | 240 | return elapsed; |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 241 | } |
| 242 | |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 243 | static double estimate_timer_overhead() { |
| 244 | double overhead = 0; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 245 | for (int i = 0; i < FLAGS_overheadLoops; i++) { |
mtklein | bbba168 | 2015-10-28 11:36:30 -0700 | [diff] [blame] | 246 | double start = now_ms(); |
| 247 | overhead += now_ms() - start; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 248 | } |
| 249 | return overhead / FLAGS_overheadLoops; |
| 250 | } |
| 251 | |
reed | 5324978 | 2014-10-10 09:09:52 -0700 | [diff] [blame] | 252 | static int detect_forever_loops(int loops) { |
| 253 | // look for a magic run-forever value |
| 254 | if (loops < 0) { |
| 255 | loops = SK_MaxS32; |
| 256 | } |
| 257 | return loops; |
| 258 | } |
| 259 | |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 260 | static int clamp_loops(int loops) { |
| 261 | if (loops < 1) { |
mtklein | 527930f | 2014-11-06 08:04:34 -0800 | [diff] [blame] | 262 | SkDebugf("ERROR: clamping loops from %d to 1. " |
| 263 | "There's probably something wrong with the bench.\n", loops); |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 264 | return 1; |
| 265 | } |
| 266 | if (loops > FLAGS_maxLoops) { |
| 267 | SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops); |
| 268 | return FLAGS_maxLoops; |
| 269 | } |
| 270 | return loops; |
| 271 | } |
| 272 | |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 273 | static bool write_canvas_png(Target* target, const SkString& filename) { |
| 274 | |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 275 | if (filename.isEmpty()) { |
| 276 | return false; |
| 277 | } |
tomhudson | 75a0ebb | 2015-03-27 12:11:44 -0700 | [diff] [blame] | 278 | if (target->getCanvas() && |
| 279 | kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) { |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 280 | return false; |
| 281 | } |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 282 | |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 283 | SkBitmap bmp; |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 284 | |
| 285 | if (!target->capturePixels(&bmp)) { |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 286 | return false; |
| 287 | } |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 288 | |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 289 | SkString dir = SkOSPath::Dirname(filename.c_str()); |
| 290 | if (!sk_mkdir(dir.c_str())) { |
| 291 | SkDebugf("Can't make dir %s.\n", dir.c_str()); |
| 292 | return false; |
| 293 | } |
| 294 | SkFILEWStream stream(filename.c_str()); |
| 295 | if (!stream.isValid()) { |
| 296 | SkDebugf("Can't write %s.\n", filename.c_str()); |
| 297 | return false; |
| 298 | } |
| 299 | if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) { |
| 300 | SkDebugf("Can't encode a PNG.\n"); |
| 301 | return false; |
| 302 | } |
| 303 | return true; |
| 304 | } |
| 305 | |
| 306 | static int kFailedLoops = -2; |
cdalton | e1b8958 | 2015-06-25 19:17:08 -0700 | [diff] [blame] | 307 | static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) { |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 308 | // First figure out approximately how many loops of bench it takes to make overhead negligible. |
mtklein | 2069e22 | 2014-08-04 13:57:39 -0700 | [diff] [blame] | 309 | double bench_plus_overhead = 0.0; |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 310 | int round = 0; |
cdalton | b402296 | 2015-06-25 10:51:56 -0700 | [diff] [blame] | 311 | int loops = bench->calculateLoops(FLAGS_loops); |
| 312 | if (kAutoTuneLoops == loops) { |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 313 | while (bench_plus_overhead < overhead) { |
| 314 | if (round++ == FLAGS_maxCalibrationAttempts) { |
| 315 | SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n", |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 316 | bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead)); |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 317 | return kFailedLoops; |
| 318 | } |
tomhudson | 75a0ebb | 2015-03-27 12:11:44 -0700 | [diff] [blame] | 319 | bench_plus_overhead = time(1, bench, target); |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 320 | } |
mtklein | 2069e22 | 2014-08-04 13:57:39 -0700 | [diff] [blame] | 321 | } |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 322 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 323 | // Later we'll just start and stop the timer once but loop N times. |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 324 | // We'll pick N to make timer overhead negligible: |
| 325 | // |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 326 | // overhead |
| 327 | // ------------------------- < FLAGS_overheadGoal |
| 328 | // overhead + N * Bench Time |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 329 | // |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 330 | // where bench_plus_overhead ≈ overhead + Bench Time. |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 331 | // |
| 332 | // Doing some math, we get: |
| 333 | // |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 334 | // (overhead / FLAGS_overheadGoal) - overhead |
| 335 | // ------------------------------------------ < N |
| 336 | // bench_plus_overhead - overhead) |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 337 | // |
| 338 | // Luckily, this also works well in practice. :) |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 339 | if (kAutoTuneLoops == loops) { |
| 340 | const double numer = overhead / FLAGS_overheadGoal - overhead; |
| 341 | const double denom = bench_plus_overhead - overhead; |
| 342 | loops = (int)ceil(numer / denom); |
reed | 5324978 | 2014-10-10 09:09:52 -0700 | [diff] [blame] | 343 | loops = clamp_loops(loops); |
| 344 | } else { |
| 345 | loops = detect_forever_loops(loops); |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 346 | } |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 347 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 348 | return loops; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 349 | } |
| 350 | |
cdalton | e1b8958 | 2015-06-25 19:17:08 -0700 | [diff] [blame] | 351 | static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) { |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 352 | // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs. |
cdalton | b402296 | 2015-06-25 10:51:56 -0700 | [diff] [blame] | 353 | int loops = bench->calculateLoops(FLAGS_loops); |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 354 | if (kAutoTuneLoops == loops) { |
| 355 | loops = 1; |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 356 | double elapsed = 0; |
| 357 | do { |
mtklein | 527930f | 2014-11-06 08:04:34 -0800 | [diff] [blame] | 358 | if (1<<30 == loops) { |
| 359 | // We're about to wrap. Something's wrong with the bench. |
| 360 | loops = 0; |
| 361 | break; |
| 362 | } |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 363 | loops *= 2; |
| 364 | // If the GPU lets frames lag at all, we need to make sure we're timing |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 365 | // _this_ round, not still timing last round. |
| 366 | for (int i = 0; i < maxGpuFrameLag; i++) { |
tomhudson | 75a0ebb | 2015-03-27 12:11:44 -0700 | [diff] [blame] | 367 | elapsed = time(loops, bench, target); |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 368 | } |
| 369 | } while (elapsed < FLAGS_gpuMs); |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 370 | |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 371 | // We've overshot at least a little. Scale back linearly. |
| 372 | loops = (int)ceil(loops * FLAGS_gpuMs / elapsed); |
reed | 5324978 | 2014-10-10 09:09:52 -0700 | [diff] [blame] | 373 | loops = clamp_loops(loops); |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 374 | |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 375 | // Make sure we're not still timing our calibration. |
| 376 | target->fence(); |
reed | 5324978 | 2014-10-10 09:09:52 -0700 | [diff] [blame] | 377 | } else { |
| 378 | loops = detect_forever_loops(loops); |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 379 | } |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 380 | |
| 381 | // Pretty much the same deal as the calibration: do some warmup to make |
| 382 | // sure we're timing steady-state pipelined frames. |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 383 | for (int i = 0; i < maxGpuFrameLag - 1; i++) { |
tomhudson | 75a0ebb | 2015-03-27 12:11:44 -0700 | [diff] [blame] | 384 | time(loops, bench, target); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 385 | } |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 386 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 387 | return loops; |
| 388 | } |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 389 | |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 390 | #if SK_SUPPORT_GPU |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 391 | #define kBogusContextType GrContextFactory::kNativeGL_ContextType |
| 392 | #define kBogusContextOptions GrContextFactory::kNone_ContextOptions |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 393 | #else |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 394 | #define kBogusContextType 0 |
| 395 | #define kBogusContextOptions 0 |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 396 | #endif |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 397 | |
svaisanen | c47635e | 2016-01-28 06:05:43 -0800 | [diff] [blame] | 398 | static void create_config(const SkCommandLineConfig* config, SkTArray<Config>* configs) { |
| 399 | |
| 400 | #if SK_SUPPORT_GPU |
| 401 | if (const auto* gpuConfig = config->asConfigGpu()) { |
| 402 | if (!FLAGS_gpu) |
| 403 | return; |
| 404 | |
brianosman | 1a48e13 | 2016-06-20 09:41:41 -0700 | [diff] [blame] | 405 | auto ctxOptions = GrContextFactory::kNone_ContextOptions; |
| 406 | if (gpuConfig->getUseNVPR()) { |
| 407 | ctxOptions = static_cast<GrContextFactory::ContextOptions>( |
| 408 | ctxOptions | GrContextFactory::kEnableNVPR_ContextOptions); |
| 409 | } |
| 410 | if (SkColorAndColorSpaceAreGammaCorrect(gpuConfig->getColorType(), |
| 411 | gpuConfig->getColorSpace())) { |
| 412 | ctxOptions = static_cast<GrContextFactory::ContextOptions>( |
| 413 | ctxOptions | GrContextFactory::kRequireSRGBSupport_ContextOptions); |
| 414 | } |
svaisanen | c47635e | 2016-01-28 06:05:43 -0800 | [diff] [blame] | 415 | const auto ctxType = gpuConfig->getContextType(); |
| 416 | const auto sampleCount = gpuConfig->getSamples(); |
| 417 | |
| 418 | if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOptions)) { |
| 419 | const auto maxSampleCount = ctx->caps()->maxSampleCount(); |
| 420 | if (sampleCount > ctx->caps()->maxSampleCount()) { |
| 421 | SkDebugf("Configuration sample count %d exceeds maximum %d.\n", |
| 422 | sampleCount, maxSampleCount); |
| 423 | return; |
| 424 | } |
| 425 | } else { |
| 426 | SkDebugf("No context was available matching config type and options.\n"); |
| 427 | return; |
| 428 | } |
| 429 | |
| 430 | Config target = { |
kkinnunen | d4e1c35 | 2016-03-01 23:41:26 -0800 | [diff] [blame] | 431 | gpuConfig->getTag(), |
svaisanen | c47635e | 2016-01-28 06:05:43 -0800 | [diff] [blame] | 432 | Benchmark::kGPU_Backend, |
brianosman | 1a48e13 | 2016-06-20 09:41:41 -0700 | [diff] [blame] | 433 | gpuConfig->getColorType(), |
svaisanen | c47635e | 2016-01-28 06:05:43 -0800 | [diff] [blame] | 434 | kPremul_SkAlphaType, |
brianosman | 1a48e13 | 2016-06-20 09:41:41 -0700 | [diff] [blame] | 435 | sk_ref_sp(gpuConfig->getColorSpace()), |
svaisanen | c47635e | 2016-01-28 06:05:43 -0800 | [diff] [blame] | 436 | sampleCount, |
| 437 | ctxType, |
kkinnunen | d4e1c35 | 2016-03-01 23:41:26 -0800 | [diff] [blame] | 438 | ctxOptions, |
| 439 | gpuConfig->getUseDIText() |
| 440 | }; |
svaisanen | c47635e | 2016-01-28 06:05:43 -0800 | [diff] [blame] | 441 | |
| 442 | configs->push_back(target); |
| 443 | return; |
| 444 | } |
| 445 | #endif |
| 446 | |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 447 | #define CPU_CONFIG(name, backend, color, alpha, colorSpace) \ |
| 448 | if (config->getTag().equals(#name)) { \ |
| 449 | Config config = { \ |
| 450 | SkString(#name), Benchmark::backend, color, alpha, colorSpace, \ |
| 451 | 0, kBogusContextType, kBogusContextOptions, false \ |
| 452 | }; \ |
| 453 | configs->push_back(config); \ |
| 454 | return; \ |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 455 | } |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 456 | |
mtklein | 40b32be | 2014-07-09 08:46:49 -0700 | [diff] [blame] | 457 | if (FLAGS_cpu) { |
mtklein | bb6c41b | 2016-03-08 11:31:11 -0800 | [diff] [blame] | 458 | CPU_CONFIG(nonrendering, kNonRendering_Backend, |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 459 | kUnknown_SkColorType, kUnpremul_SkAlphaType, nullptr) |
mtklein | bb6c41b | 2016-03-08 11:31:11 -0800 | [diff] [blame] | 460 | |
| 461 | CPU_CONFIG(8888, kRaster_Backend, |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 462 | kN32_SkColorType, kPremul_SkAlphaType, nullptr) |
mtklein | bb6c41b | 2016-03-08 11:31:11 -0800 | [diff] [blame] | 463 | CPU_CONFIG(565, kRaster_Backend, |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 464 | kRGB_565_SkColorType, kOpaque_SkAlphaType, nullptr) |
| 465 | auto srgbColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named); |
mtklein | bb6c41b | 2016-03-08 11:31:11 -0800 | [diff] [blame] | 466 | CPU_CONFIG(srgb, kRaster_Backend, |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 467 | kN32_SkColorType, kPremul_SkAlphaType, srgbColorSpace) |
mtklein | bb6c41b | 2016-03-08 11:31:11 -0800 | [diff] [blame] | 468 | CPU_CONFIG(f16, kRaster_Backend, |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 469 | kRGBA_F16_SkColorType, kPremul_SkAlphaType, nullptr) |
mtklein | 40b32be | 2014-07-09 08:46:49 -0700 | [diff] [blame] | 470 | } |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 471 | |
svaisanen | c47635e | 2016-01-28 06:05:43 -0800 | [diff] [blame] | 472 | #undef CPU_CONFIG |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 473 | |
| 474 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
djsollen | 817c72a | 2016-01-28 13:57:02 -0800 | [diff] [blame] | 475 | if (config->getTag().equals("hwui")) { |
mtklein | bb6c41b | 2016-03-08 11:31:11 -0800 | [diff] [blame] | 476 | Config config = { SkString("hwui"), Benchmark::kHWUI_Backend, |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 477 | kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr, |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 478 | 0, kBogusContextType, kBogusContextOptions, false }; |
svaisanen | c47635e | 2016-01-28 06:05:43 -0800 | [diff] [blame] | 479 | configs->push_back(config); |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 480 | } |
| 481 | #endif |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 482 | } |
| 483 | |
svaisanen | c47635e | 2016-01-28 06:05:43 -0800 | [diff] [blame] | 484 | // Append all configs that are enabled and supported. |
| 485 | void create_configs(SkTArray<Config>* configs) { |
| 486 | SkCommandLineConfigArray array; |
| 487 | ParseConfigs(FLAGS_config, &array); |
| 488 | for (int i = 0; i < array.count(); ++i) { |
| 489 | create_config(array[i], configs); |
| 490 | } |
| 491 | } |
| 492 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 493 | // If bench is enabled for config, returns a Target* for it, otherwise nullptr. |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 494 | static Target* is_enabled(Benchmark* bench, const Config& config) { |
| 495 | if (!bench->isSuitableFor(config.backend)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 496 | return nullptr; |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 497 | } |
| 498 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 499 | SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY, |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 500 | config.color, config.alpha, config.colorSpace); |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 501 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 502 | Target* target = nullptr; |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 503 | |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 504 | switch (config.backend) { |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 505 | #if SK_SUPPORT_GPU |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 506 | case Benchmark::kGPU_Backend: |
| 507 | target = new GPUTarget(config); |
| 508 | break; |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 509 | #endif |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 510 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 511 | case Benchmark::kHWUI_Backend: |
| 512 | target = new HWUITarget(config, bench); |
| 513 | break; |
| 514 | #endif |
| 515 | default: |
| 516 | target = new Target(config); |
| 517 | break; |
| 518 | } |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 519 | |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 520 | if (!target->init(info, bench)) { |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 521 | delete target; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 522 | return nullptr; |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 523 | } |
| 524 | return target; |
| 525 | } |
| 526 | |
msarett | d1227a7 | 2016-05-18 06:23:57 -0700 | [diff] [blame] | 527 | static bool valid_brd_bench(SkData* encoded, SkColorType colorType, uint32_t sampleSize, |
| 528 | uint32_t minOutputSize, int* width, int* height) { |
msarett | 5cb4885 | 2015-11-06 08:56:32 -0800 | [diff] [blame] | 529 | SkAutoTDelete<SkBitmapRegionDecoder> brd( |
msarett | d1227a7 | 2016-05-18 06:23:57 -0700 | [diff] [blame] | 530 | SkBitmapRegionDecoder::Create(encoded, SkBitmapRegionDecoder::kAndroidCodec_Strategy)); |
msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 531 | if (nullptr == brd.get()) { |
| 532 | // This is indicates that subset decoding is not supported for a particular image format. |
| 533 | return false; |
| 534 | } |
| 535 | |
msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 536 | if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize > |
| 537 | (uint32_t) brd->height()) { |
| 538 | // This indicates that the image is not large enough to decode a |
| 539 | // minOutputSize x minOutputSize subset at the given sampleSize. |
| 540 | return false; |
| 541 | } |
| 542 | |
| 543 | // Set the image width and height. The calling code will use this to choose subsets to decode. |
| 544 | *width = brd->width(); |
| 545 | *height = brd->height(); |
| 546 | return true; |
| 547 | } |
| 548 | |
egdaniel | 3bf9206 | 2015-06-26 08:12:46 -0700 | [diff] [blame] | 549 | static void cleanup_run(Target* target) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 550 | delete target; |
egdaniel | 3bf9206 | 2015-06-26 08:12:46 -0700 | [diff] [blame] | 551 | #if SK_SUPPORT_GPU |
| 552 | if (FLAGS_abandonGpuContext) { |
| 553 | gGrFactory->abandonContexts(); |
| 554 | } |
| 555 | if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) { |
| 556 | gGrFactory->destroyContexts(); |
| 557 | } |
| 558 | #endif |
| 559 | } |
| 560 | |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 561 | class BenchmarkStream { |
| 562 | public: |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 563 | BenchmarkStream() : fBenches(BenchRegistry::Head()) |
| 564 | , fGMs(skiagm::GMRegistry::Head()) |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 565 | , fCurrentRecording(0) |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 566 | , fCurrentScale(0) |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 567 | , fCurrentSKP(0) |
msarett | 95f192d | 2015-02-13 09:05:41 -0800 | [diff] [blame] | 568 | , fCurrentUseMPD(0) |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 569 | , fCurrentCodec(0) |
msarett | 8445102 | 2016-02-11 06:45:51 -0800 | [diff] [blame] | 570 | , fCurrentAndroidCodec(0) |
msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 571 | , fCurrentBRDImage(0) |
msarett | 2cee902 | 2016-06-03 08:25:21 -0700 | [diff] [blame] | 572 | , fCurrentColorImage(0) |
msarett | 95f192d | 2015-02-13 09:05:41 -0800 | [diff] [blame] | 573 | , fCurrentColorType(0) |
msarett | c7796b9 | 2016-01-07 14:20:20 -0800 | [diff] [blame] | 574 | , fCurrentAlphaType(0) |
msarett | b23e6aa | 2015-06-09 13:56:10 -0700 | [diff] [blame] | 575 | , fCurrentSubsetType(0) |
msarett | 8445102 | 2016-02-11 06:45:51 -0800 | [diff] [blame] | 576 | , fCurrentSampleSize(0) |
msarett | b23e6aa | 2015-06-09 13:56:10 -0700 | [diff] [blame] | 577 | , fCurrentAnimSKP(0) { |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 578 | for (int i = 0; i < FLAGS_skps.count(); i++) { |
| 579 | if (SkStrEndsWith(FLAGS_skps[i], ".skp")) { |
| 580 | fSKPs.push_back() = FLAGS_skps[i]; |
| 581 | } else { |
| 582 | SkOSFile::Iter it(FLAGS_skps[i], ".skp"); |
| 583 | SkString path; |
| 584 | while (it.next(&path)) { |
| 585 | fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str()); |
| 586 | } |
| 587 | } |
| 588 | } |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 589 | |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 590 | if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d", |
| 591 | &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) { |
| 592 | SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]); |
| 593 | exit(1); |
| 594 | } |
| 595 | |
| 596 | for (int i = 0; i < FLAGS_scales.count(); i++) { |
| 597 | if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) { |
| 598 | SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]); |
| 599 | exit(1); |
| 600 | } |
| 601 | } |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 602 | |
cdalton | 63a8285 | 2015-06-29 14:06:10 -0700 | [diff] [blame] | 603 | if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) { |
| 604 | SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]); |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 605 | exit(1); |
| 606 | } |
| 607 | |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 608 | if (FLAGS_mpd) { |
| 609 | fUseMPDs.push_back() = true; |
| 610 | } |
mtklein | c751ecb | 2015-06-15 08:56:38 -0700 | [diff] [blame] | 611 | fUseMPDs.push_back() = false; |
mtklein | 95553d9 | 2015-03-12 08:24:21 -0700 | [diff] [blame] | 612 | |
msarett | 95f192d | 2015-02-13 09:05:41 -0800 | [diff] [blame] | 613 | // Prepare the images for decoding |
msarett | 69deca8 | 2016-04-29 09:38:40 -0700 | [diff] [blame] | 614 | if (!CollectImages(FLAGS_images, &fImages)) { |
scroggo | 8673714 | 2016-02-03 12:19:11 -0800 | [diff] [blame] | 615 | exit(1); |
msarett | 95f192d | 2015-02-13 09:05:41 -0800 | [diff] [blame] | 616 | } |
msarett | 2cee902 | 2016-06-03 08:25:21 -0700 | [diff] [blame] | 617 | if (!CollectImages(FLAGS_colorImages, &fColorImages)) { |
| 618 | exit(1); |
| 619 | } |
mtklein | 95553d9 | 2015-03-12 08:24:21 -0700 | [diff] [blame] | 620 | |
msarett | 95f192d | 2015-02-13 09:05:41 -0800 | [diff] [blame] | 621 | // Choose the candidate color types for image decoding |
msarett | 67cb666 | 2016-06-21 08:49:26 -0700 | [diff] [blame] | 622 | fColorTypes.push_back(kN32_SkColorType); |
| 623 | if (!FLAGS_simpleCodec) { |
| 624 | fColorTypes.push_back(kRGB_565_SkColorType); |
| 625 | fColorTypes.push_back(kAlpha_8_SkColorType); |
| 626 | fColorTypes.push_back(kIndex_8_SkColorType); |
| 627 | fColorTypes.push_back(kGray_8_SkColorType); |
| 628 | } |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 629 | } |
| 630 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 631 | static sk_sp<SkPicture> ReadPicture(const char* path) { |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 632 | // Not strictly necessary, as it will be checked again later, |
| 633 | // but helps to avoid a lot of pointless work if we're going to skip it. |
cdalton | 91e457d | 2016-02-17 11:10:16 -0800 | [diff] [blame] | 634 | if (SkCommandLineFlags::ShouldSkip(FLAGS_match, SkOSPath::Basename(path).c_str())) { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 635 | return nullptr; |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 636 | } |
| 637 | |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 638 | SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 639 | if (stream.get() == nullptr) { |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 640 | SkDebugf("Could not read %s.\n", path); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 641 | return nullptr; |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 642 | } |
| 643 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 644 | return SkPicture::MakeFromStream(stream.get()); |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 645 | } |
| 646 | |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 647 | Benchmark* next() { |
mtklein | 65dfd2f | 2016-02-03 10:40:54 -0800 | [diff] [blame] | 648 | SkAutoTDelete<Benchmark> bench; |
| 649 | do { |
| 650 | bench.reset(this->rawNext()); |
| 651 | if (!bench) { |
| 652 | return nullptr; |
| 653 | } |
| 654 | } while(SkCommandLineFlags::ShouldSkip(FLAGS_sourceType, fSourceType) || |
| 655 | SkCommandLineFlags::ShouldSkip(FLAGS_benchType, fBenchType)); |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 656 | return bench.release(); |
mtklein | 65dfd2f | 2016-02-03 10:40:54 -0800 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | Benchmark* rawNext() { |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 660 | if (fBenches) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 661 | Benchmark* bench = fBenches->factory()(nullptr); |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 662 | fBenches = fBenches->next(); |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 663 | fSourceType = "bench"; |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 664 | fBenchType = "micro"; |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 665 | return bench; |
| 666 | } |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 667 | |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 668 | while (fGMs) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 669 | SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr)); |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 670 | fGMs = fGMs->next(); |
mtklein | cf5d9c9 | 2015-01-23 10:31:45 -0800 | [diff] [blame] | 671 | if (gm->runAsBench()) { |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 672 | fSourceType = "gm"; |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 673 | fBenchType = "micro"; |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 674 | return new GMBench(gm.release()); |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 675 | } |
| 676 | } |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 677 | |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 678 | // First add all .skps as RecordingBenches. |
| 679 | while (fCurrentRecording < fSKPs.count()) { |
| 680 | const SkString& path = fSKPs[fCurrentRecording++]; |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 681 | sk_sp<SkPicture> pic = ReadPicture(path.c_str()); |
| 682 | if (!pic) { |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 683 | continue; |
| 684 | } |
| 685 | SkString name = SkOSPath::Basename(path.c_str()); |
| 686 | fSourceType = "skp"; |
| 687 | fBenchType = "recording"; |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 688 | fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic.get())); |
mtklein | 051e56d | 2014-12-04 08:46:51 -0800 | [diff] [blame] | 689 | fSKPOps = pic->approximateOpCount(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 690 | return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh); |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | // Then once each for each scale as SKPBenches (playback). |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 694 | while (fCurrentScale < fScales.count()) { |
| 695 | while (fCurrentSKP < fSKPs.count()) { |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 696 | const SkString& path = fSKPs[fCurrentSKP]; |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 697 | sk_sp<SkPicture> pic = ReadPicture(path.c_str()); |
| 698 | if (!pic) { |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 699 | fCurrentSKP++; |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 700 | continue; |
| 701 | } |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 702 | |
| 703 | while (fCurrentUseMPD < fUseMPDs.count()) { |
| 704 | if (FLAGS_bbh) { |
| 705 | // The SKP we read off disk doesn't have a BBH. Re-record so it grows one. |
| 706 | SkRTreeFactory factory; |
| 707 | SkPictureRecorder recorder; |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 708 | pic->playback(recorder.beginRecording(pic->cullRect().width(), |
| 709 | pic->cullRect().height(), |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 710 | &factory, |
robertphillips | dda5445 | 2016-07-13 13:27:16 -0700 | [diff] [blame] | 711 | 0)); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 712 | pic = recorder.finishRecordingAsPicture(); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 713 | } |
| 714 | SkString name = SkOSPath::Basename(path.c_str()); |
| 715 | fSourceType = "skp"; |
| 716 | fBenchType = "playback"; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 717 | return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale], |
| 718 | fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP); |
mtklein | 2084050 | 2014-08-21 15:51:22 -0700 | [diff] [blame] | 719 | } |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 720 | fCurrentUseMPD = 0; |
| 721 | fCurrentSKP++; |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 722 | } |
| 723 | fCurrentSKP = 0; |
| 724 | fCurrentScale++; |
| 725 | } |
| 726 | |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 727 | // Now loop over each skp again if we have an animation |
cdalton | 63a8285 | 2015-06-29 14:06:10 -0700 | [diff] [blame] | 728 | if (fZoomMax != 1.0f && fZoomPeriodMs > 0) { |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 729 | while (fCurrentAnimSKP < fSKPs.count()) { |
| 730 | const SkString& path = fSKPs[fCurrentAnimSKP]; |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 731 | sk_sp<SkPicture> pic = ReadPicture(path.c_str()); |
| 732 | if (!pic) { |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 733 | fCurrentAnimSKP++; |
| 734 | continue; |
| 735 | } |
| 736 | |
| 737 | fCurrentAnimSKP++; |
| 738 | SkString name = SkOSPath::Basename(path.c_str()); |
cdalton | 63a8285 | 2015-06-29 14:06:10 -0700 | [diff] [blame] | 739 | SkAutoTUnref<SKPAnimationBench::Animation> animation( |
| 740 | SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs)); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 741 | return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation, |
| 742 | FLAGS_loopSKP); |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 746 | for (; fCurrentCodec < fImages.count(); fCurrentCodec++) { |
scroggo | 303fa35 | 2015-10-05 11:03:34 -0700 | [diff] [blame] | 747 | fSourceType = "image"; |
| 748 | fBenchType = "skcodec"; |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 749 | const SkString& path = fImages[fCurrentCodec]; |
mtklein | 6f0ff91 | 2016-01-11 09:04:21 -0800 | [diff] [blame] | 750 | if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) { |
| 751 | continue; |
| 752 | } |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 753 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str())); |
| 754 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 755 | if (!codec) { |
| 756 | // Nothing to time. |
msarett | 9d9725c | 2015-04-24 11:41:55 -0700 | [diff] [blame] | 757 | SkDebugf("Cannot find codec for %s\n", path.c_str()); |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 758 | continue; |
| 759 | } |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 760 | |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 761 | while (fCurrentColorType < fColorTypes.count()) { |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 762 | const SkColorType colorType = fColorTypes[fCurrentColorType]; |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 763 | |
msarett | c7796b9 | 2016-01-07 14:20:20 -0800 | [diff] [blame] | 764 | SkAlphaType alphaType = codec->getInfo().alphaType(); |
msarett | 67cb666 | 2016-06-21 08:49:26 -0700 | [diff] [blame] | 765 | if (FLAGS_simpleCodec) { |
| 766 | if (kUnpremul_SkAlphaType == alphaType) { |
| 767 | alphaType = kPremul_SkAlphaType; |
| 768 | } |
| 769 | |
| 770 | fCurrentColorType++; |
| 771 | } else { |
| 772 | switch (alphaType) { |
| 773 | case kOpaque_SkAlphaType: |
| 774 | // We only need to test one alpha type (opaque). |
msarett | c7796b9 | 2016-01-07 14:20:20 -0800 | [diff] [blame] | 775 | fCurrentColorType++; |
msarett | 67cb666 | 2016-06-21 08:49:26 -0700 | [diff] [blame] | 776 | break; |
| 777 | case kUnpremul_SkAlphaType: |
| 778 | case kPremul_SkAlphaType: |
| 779 | if (0 == fCurrentAlphaType) { |
| 780 | // Test unpremul first. |
| 781 | alphaType = kUnpremul_SkAlphaType; |
| 782 | fCurrentAlphaType++; |
| 783 | } else { |
| 784 | // Test premul. |
| 785 | alphaType = kPremul_SkAlphaType; |
| 786 | fCurrentAlphaType = 0; |
| 787 | fCurrentColorType++; |
| 788 | } |
| 789 | break; |
| 790 | default: |
| 791 | SkASSERT(false); |
| 792 | fCurrentColorType++; |
| 793 | break; |
| 794 | } |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 795 | } |
| 796 | |
msarett | c7796b9 | 2016-01-07 14:20:20 -0800 | [diff] [blame] | 797 | // Make sure we can decode to this color type and alpha type. |
| 798 | SkImageInfo info = |
| 799 | codec->getInfo().makeColorType(colorType).makeAlphaType(alphaType); |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 800 | const size_t rowBytes = info.minRowBytes(); |
| 801 | SkAutoMalloc storage(info.getSafeSize(rowBytes)); |
| 802 | |
| 803 | // Used if fCurrentColorType is kIndex_8_SkColorType |
| 804 | int colorCount = 256; |
| 805 | SkPMColor colors[256]; |
| 806 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 807 | const SkCodec::Result result = codec->getPixels( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 808 | info, storage.get(), rowBytes, nullptr, colors, |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 809 | &colorCount); |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 810 | switch (result) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 811 | case SkCodec::kSuccess: |
| 812 | case SkCodec::kIncompleteInput: |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 813 | return new CodecBench(SkOSPath::Basename(path.c_str()), |
msarett | c7796b9 | 2016-01-07 14:20:20 -0800 | [diff] [blame] | 814 | encoded, colorType, alphaType); |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 815 | case SkCodec::kInvalidConversion: |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 816 | // This is okay. Not all conversions are valid. |
| 817 | break; |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 818 | default: |
| 819 | // This represents some sort of failure. |
| 820 | SkASSERT(false); |
| 821 | break; |
| 822 | } |
| 823 | } |
| 824 | fCurrentColorType = 0; |
| 825 | } |
| 826 | |
msarett | 8445102 | 2016-02-11 06:45:51 -0800 | [diff] [blame] | 827 | // Run AndroidCodecBenches |
| 828 | const int sampleSizes[] = { 2, 4, 8 }; |
| 829 | for (; fCurrentAndroidCodec < fImages.count(); fCurrentAndroidCodec++) { |
| 830 | fSourceType = "image"; |
| 831 | fBenchType = "skandroidcodec"; |
| 832 | |
| 833 | const SkString& path = fImages[fCurrentAndroidCodec]; |
| 834 | if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) { |
| 835 | continue; |
| 836 | } |
| 837 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str())); |
| 838 | SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(encoded)); |
| 839 | if (!codec) { |
| 840 | // Nothing to time. |
| 841 | SkDebugf("Cannot find codec for %s\n", path.c_str()); |
| 842 | continue; |
| 843 | } |
| 844 | |
| 845 | while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) { |
| 846 | int sampleSize = sampleSizes[fCurrentSampleSize]; |
| 847 | fCurrentSampleSize++; |
| 848 | if (10 * sampleSize > SkTMin(codec->getInfo().width(), codec->getInfo().height())) { |
| 849 | // Avoid benchmarking scaled decodes of already small images. |
| 850 | break; |
| 851 | } |
| 852 | |
| 853 | return new AndroidCodecBench(SkOSPath::Basename(path.c_str()), encoded, sampleSize); |
| 854 | } |
| 855 | fCurrentSampleSize = 0; |
| 856 | } |
| 857 | |
msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 858 | // Run the BRDBenches |
msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 859 | // We intend to create benchmarks that model the use cases in |
| 860 | // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512 |
| 861 | // tiles. The image can be translated freely, so the location of a tile may be anywhere in |
| 862 | // the image. For that reason, we will benchmark decodes in five representative locations |
| 863 | // in the image. Additionally, this use case utilizes power of two scaling, so we will |
| 864 | // test on power of two sample sizes. The output tile is always 512x512, so, when a |
| 865 | // sampleSize is used, the size of the subset that is decoded is always |
| 866 | // (sampleSize*512)x(sampleSize*512). |
| 867 | // There are a few good reasons to only test on power of two sample sizes at this time: |
msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 868 | // All use cases we are aware of only scale by powers of two. |
| 869 | // PNG decodes use the indicated sampling strategy regardless of the sample size, so |
| 870 | // these tests are sufficient to provide good coverage of our scaling options. |
msarett | 8445102 | 2016-02-11 06:45:51 -0800 | [diff] [blame] | 871 | const uint32_t brdSampleSizes[] = { 1, 2, 4, 8, 16 }; |
msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 872 | const uint32_t minOutputSize = 512; |
mtklein | 6f0ff91 | 2016-01-11 09:04:21 -0800 | [diff] [blame] | 873 | for (; fCurrentBRDImage < fImages.count(); fCurrentBRDImage++) { |
msarett | d1227a7 | 2016-05-18 06:23:57 -0700 | [diff] [blame] | 874 | fSourceType = "image"; |
| 875 | fBenchType = "BRD"; |
| 876 | |
mtklein | 6f0ff91 | 2016-01-11 09:04:21 -0800 | [diff] [blame] | 877 | const SkString& path = fImages[fCurrentBRDImage]; |
| 878 | if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) { |
| 879 | continue; |
| 880 | } |
scroggo | 860e8a6 | 2015-10-15 07:51:28 -0700 | [diff] [blame] | 881 | |
msarett | d1227a7 | 2016-05-18 06:23:57 -0700 | [diff] [blame] | 882 | while (fCurrentColorType < fColorTypes.count()) { |
| 883 | while (fCurrentSampleSize < (int) SK_ARRAY_COUNT(brdSampleSizes)) { |
| 884 | while (fCurrentSubsetType <= kLastSingle_SubsetType) { |
scroggo | 860e8a6 | 2015-10-15 07:51:28 -0700 | [diff] [blame] | 885 | |
msarett | d1227a7 | 2016-05-18 06:23:57 -0700 | [diff] [blame] | 886 | SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str())); |
| 887 | const SkColorType colorType = fColorTypes[fCurrentColorType]; |
| 888 | uint32_t sampleSize = brdSampleSizes[fCurrentSampleSize]; |
| 889 | int currentSubsetType = fCurrentSubsetType++; |
scroggo | 860e8a6 | 2015-10-15 07:51:28 -0700 | [diff] [blame] | 890 | |
msarett | d1227a7 | 2016-05-18 06:23:57 -0700 | [diff] [blame] | 891 | int width = 0; |
| 892 | int height = 0; |
| 893 | if (!valid_brd_bench(encoded.get(), colorType, sampleSize, minOutputSize, |
| 894 | &width, &height)) { |
| 895 | break; |
msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 896 | } |
msarett | d1227a7 | 2016-05-18 06:23:57 -0700 | [diff] [blame] | 897 | |
| 898 | SkString basename = SkOSPath::Basename(path.c_str()); |
| 899 | SkIRect subset; |
| 900 | const uint32_t subsetSize = sampleSize * minOutputSize; |
| 901 | switch (currentSubsetType) { |
| 902 | case kTopLeft_SubsetType: |
| 903 | basename.append("_TopLeft"); |
| 904 | subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize); |
| 905 | break; |
| 906 | case kTopRight_SubsetType: |
| 907 | basename.append("_TopRight"); |
| 908 | subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize, |
| 909 | subsetSize); |
| 910 | break; |
| 911 | case kMiddle_SubsetType: |
| 912 | basename.append("_Middle"); |
| 913 | subset = SkIRect::MakeXYWH((width - subsetSize) / 2, |
| 914 | (height - subsetSize) / 2, subsetSize, subsetSize); |
| 915 | break; |
| 916 | case kBottomLeft_SubsetType: |
| 917 | basename.append("_BottomLeft"); |
| 918 | subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize, |
| 919 | subsetSize); |
| 920 | break; |
| 921 | case kBottomRight_SubsetType: |
| 922 | basename.append("_BottomRight"); |
| 923 | subset = SkIRect::MakeXYWH(width - subsetSize, |
| 924 | height - subsetSize, subsetSize, subsetSize); |
| 925 | break; |
| 926 | default: |
| 927 | SkASSERT(false); |
| 928 | } |
| 929 | |
| 930 | return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(), |
| 931 | colorType, sampleSize, subset); |
msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 932 | } |
msarett | d1227a7 | 2016-05-18 06:23:57 -0700 | [diff] [blame] | 933 | fCurrentSubsetType = 0; |
| 934 | fCurrentSampleSize++; |
msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 935 | } |
msarett | d1227a7 | 2016-05-18 06:23:57 -0700 | [diff] [blame] | 936 | fCurrentSampleSize = 0; |
| 937 | fCurrentColorType++; |
msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 938 | } |
msarett | d1227a7 | 2016-05-18 06:23:57 -0700 | [diff] [blame] | 939 | fCurrentColorType = 0; |
msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 940 | } |
| 941 | |
msarett | 2cee902 | 2016-06-03 08:25:21 -0700 | [diff] [blame] | 942 | while (fCurrentColorImage < fColorImages.count()) { |
| 943 | fSourceType = "colorimage"; |
| 944 | fBenchType = "skcolorcodec"; |
| 945 | const SkString& path = fColorImages[fCurrentColorImage]; |
| 946 | fCurrentColorImage++; |
| 947 | sk_sp<SkData> encoded = SkData::MakeFromFileName(path.c_str()); |
| 948 | if (encoded) { |
| 949 | return new ColorCodecBench(SkOSPath::Basename(path.c_str()).c_str(), |
| 950 | std::move(encoded)); |
| 951 | } else { |
| 952 | SkDebugf("Could not read file %s.\n", path.c_str()); |
| 953 | } |
| 954 | } |
| 955 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 956 | return nullptr; |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 957 | } |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 958 | |
| 959 | void fillCurrentOptions(ResultsWriter* log) const { |
| 960 | log->configOption("source_type", fSourceType); |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 961 | log->configOption("bench_type", fBenchType); |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 962 | if (0 == strcmp(fSourceType, "skp")) { |
| 963 | log->configOption("clip", |
| 964 | SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop, |
| 965 | fClip.fRight, fClip.fBottom).c_str()); |
djsollen | f2b340f | 2016-01-29 08:51:04 -0800 | [diff] [blame] | 966 | SkASSERT_RELEASE(fCurrentScale < fScales.count()); // debugging paranoia |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 967 | log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str()); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 968 | if (fCurrentUseMPD > 0) { |
| 969 | SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD); |
| 970 | log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false"); |
| 971 | } |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 972 | } |
mtklein | 051e56d | 2014-12-04 08:46:51 -0800 | [diff] [blame] | 973 | if (0 == strcmp(fBenchType, "recording")) { |
| 974 | log->metric("bytes", fSKPBytes); |
| 975 | log->metric("ops", fSKPOps); |
| 976 | } |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 977 | } |
| 978 | |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 979 | private: |
msarett | b23e6aa | 2015-06-09 13:56:10 -0700 | [diff] [blame] | 980 | enum SubsetType { |
| 981 | kTopLeft_SubsetType = 0, |
| 982 | kTopRight_SubsetType = 1, |
msarett | ab80e35 | 2015-06-17 10:28:22 -0700 | [diff] [blame] | 983 | kMiddle_SubsetType = 2, |
| 984 | kBottomLeft_SubsetType = 3, |
| 985 | kBottomRight_SubsetType = 4, |
| 986 | kTranslate_SubsetType = 5, |
| 987 | kZoom_SubsetType = 6, |
msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 988 | kLast_SubsetType = kZoom_SubsetType, |
| 989 | kLastSingle_SubsetType = kBottomRight_SubsetType, |
msarett | b23e6aa | 2015-06-09 13:56:10 -0700 | [diff] [blame] | 990 | }; |
| 991 | |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 992 | const BenchRegistry* fBenches; |
| 993 | const skiagm::GMRegistry* fGMs; |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 994 | SkIRect fClip; |
| 995 | SkTArray<SkScalar> fScales; |
| 996 | SkTArray<SkString> fSKPs; |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 997 | SkTArray<bool> fUseMPDs; |
msarett | 95f192d | 2015-02-13 09:05:41 -0800 | [diff] [blame] | 998 | SkTArray<SkString> fImages; |
msarett | 2cee902 | 2016-06-03 08:25:21 -0700 | [diff] [blame] | 999 | SkTArray<SkString> fColorImages; |
msarett | 74deb98 | 2015-10-20 16:45:56 -0700 | [diff] [blame] | 1000 | SkTArray<SkColorType, true> fColorTypes; |
cdalton | 63a8285 | 2015-06-29 14:06:10 -0700 | [diff] [blame] | 1001 | SkScalar fZoomMax; |
| 1002 | double fZoomPeriodMs; |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 1003 | |
mtklein | 051e56d | 2014-12-04 08:46:51 -0800 | [diff] [blame] | 1004 | double fSKPBytes, fSKPOps; |
| 1005 | |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 1006 | const char* fSourceType; // What we're benching: bench, GM, SKP, ... |
| 1007 | const char* fBenchType; // How we bench it: micro, recording, playback, ... |
| 1008 | int fCurrentRecording; |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 1009 | int fCurrentScale; |
| 1010 | int fCurrentSKP; |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 1011 | int fCurrentUseMPD; |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 1012 | int fCurrentCodec; |
msarett | 8445102 | 2016-02-11 06:45:51 -0800 | [diff] [blame] | 1013 | int fCurrentAndroidCodec; |
msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 1014 | int fCurrentBRDImage; |
msarett | 2cee902 | 2016-06-03 08:25:21 -0700 | [diff] [blame] | 1015 | int fCurrentColorImage; |
msarett | 95f192d | 2015-02-13 09:05:41 -0800 | [diff] [blame] | 1016 | int fCurrentColorType; |
msarett | c7796b9 | 2016-01-07 14:20:20 -0800 | [diff] [blame] | 1017 | int fCurrentAlphaType; |
msarett | b23e6aa | 2015-06-09 13:56:10 -0700 | [diff] [blame] | 1018 | int fCurrentSubsetType; |
msarett | 8445102 | 2016-02-11 06:45:51 -0800 | [diff] [blame] | 1019 | int fCurrentSampleSize; |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 1020 | int fCurrentAnimSKP; |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 1021 | }; |
| 1022 | |
msarett | c149f0e | 2016-01-04 11:35:43 -0800 | [diff] [blame] | 1023 | // Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung. |
| 1024 | // This prints something every once in a while so that it knows we're still working. |
| 1025 | static void start_keepalive() { |
| 1026 | struct Loop { |
| 1027 | static void forever(void*) { |
| 1028 | for (;;) { |
| 1029 | static const int kSec = 1200; |
| 1030 | #if defined(SK_BUILD_FOR_WIN) |
| 1031 | Sleep(kSec * 1000); |
| 1032 | #else |
| 1033 | sleep(kSec); |
| 1034 | #endif |
| 1035 | SkDebugf("\nBenchmarks still running...\n"); |
| 1036 | } |
| 1037 | } |
| 1038 | }; |
| 1039 | static SkThread* intentionallyLeaked = new SkThread(Loop::forever); |
| 1040 | intentionallyLeaked->start(); |
| 1041 | } |
| 1042 | |
jcgregorio | 3b27ade | 2014-11-13 08:06:40 -0800 | [diff] [blame] | 1043 | int nanobench_main(); |
caryclark | 17f0b6d | 2014-07-22 10:15:34 -0700 | [diff] [blame] | 1044 | int nanobench_main() { |
jcgregorio | 3b27ade | 2014-11-13 08:06:40 -0800 | [diff] [blame] | 1045 | SetupCrashHandler(); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 1046 | SkAutoGraphics ag; |
mtklein | cc29d26 | 2015-07-09 10:04:56 -0700 | [diff] [blame] | 1047 | SkTaskGroup::Enabler enabled(FLAGS_threads); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 1048 | |
krajcevski | 69a5560 | 2014-08-13 10:46:31 -0700 | [diff] [blame] | 1049 | #if SK_SUPPORT_GPU |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 1050 | GrContextOptions grContextOpts; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1051 | gGrFactory.reset(new GrContextFactory(grContextOpts)); |
krajcevski | 69a5560 | 2014-08-13 10:46:31 -0700 | [diff] [blame] | 1052 | #endif |
| 1053 | |
bsalomon | 06cddec | 2014-10-24 10:40:50 -0700 | [diff] [blame] | 1054 | if (FLAGS_veryVerbose) { |
| 1055 | FLAGS_verbose = true; |
| 1056 | } |
| 1057 | |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 1058 | if (kAutoTuneLoops != FLAGS_loops) { |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 1059 | FLAGS_samples = 1; |
| 1060 | FLAGS_gpuFrameLag = 0; |
| 1061 | } |
| 1062 | |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 1063 | if (!FLAGS_writePath.isEmpty()) { |
| 1064 | SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]); |
| 1065 | if (!sk_mkdir(FLAGS_writePath[0])) { |
| 1066 | SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1067 | FLAGS_writePath.set(0, nullptr); |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 1068 | } |
| 1069 | } |
| 1070 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1071 | SkAutoTDelete<ResultsWriter> log(new ResultsWriter); |
mtklein | 60317d0f | 2014-07-14 11:30:37 -0700 | [diff] [blame] | 1072 | if (!FLAGS_outResultsFile.isEmpty()) { |
mtklein | 5352015 | 2016-01-20 09:53:59 -0800 | [diff] [blame] | 1073 | #if defined(SK_RELEASE) |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1074 | log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0])); |
mtklein | 5352015 | 2016-01-20 09:53:59 -0800 | [diff] [blame] | 1075 | #else |
| 1076 | SkDebugf("I'm ignoring --outResultsFile because this is a Debug build."); |
| 1077 | return 1; |
| 1078 | #endif |
mtklein | 60317d0f | 2014-07-14 11:30:37 -0700 | [diff] [blame] | 1079 | } |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 1080 | |
| 1081 | if (1 == FLAGS_properties.count() % 2) { |
| 1082 | SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n"); |
| 1083 | return 1; |
| 1084 | } |
| 1085 | for (int i = 1; i < FLAGS_properties.count(); i += 2) { |
| 1086 | log->property(FLAGS_properties[i-1], FLAGS_properties[i]); |
| 1087 | } |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 1088 | |
| 1089 | if (1 == FLAGS_key.count() % 2) { |
| 1090 | SkDebugf("ERROR: --key must be passed with an even number of arguments.\n"); |
| 1091 | return 1; |
| 1092 | } |
| 1093 | for (int i = 1; i < FLAGS_key.count(); i += 2) { |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 1094 | log->key(FLAGS_key[i-1], FLAGS_key[i]); |
mtklein | 94e5156 | 2014-08-19 12:41:53 -0700 | [diff] [blame] | 1095 | } |
mtklein | 60317d0f | 2014-07-14 11:30:37 -0700 | [diff] [blame] | 1096 | |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 1097 | const double overhead = estimate_timer_overhead(); |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 1098 | SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead)); |
Mike Klein | 9129477 | 2014-07-16 19:59:32 -0400 | [diff] [blame] | 1099 | |
cdalton | e1b8958 | 2015-06-25 19:17:08 -0700 | [diff] [blame] | 1100 | SkTArray<double> samples; |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 1101 | |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 1102 | if (kAutoTuneLoops != FLAGS_loops) { |
| 1103 | SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n"); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 1104 | } else if (FLAGS_quiet) { |
mtklein | 66cfcff | 2015-12-04 06:35:30 -0800 | [diff] [blame] | 1105 | SkDebugf("! -> high variance, ? -> moderate variance\n"); |
| 1106 | SkDebugf(" micros \tbench\n"); |
mtklein | bbba168 | 2015-10-28 11:36:30 -0700 | [diff] [blame] | 1107 | } else if (FLAGS_ms) { |
cdalton | e1b8958 | 2015-06-25 19:17:08 -0700 | [diff] [blame] | 1108 | SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n"); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 1109 | } else { |
mtklein | d75c466 | 2015-04-30 07:11:22 -0700 | [diff] [blame] | 1110 | SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n", |
qiankun.miao | 8247ec3 | 2014-09-09 19:24:36 -0700 | [diff] [blame] | 1111 | FLAGS_samples, "samples"); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
svaisanen | c47635e | 2016-01-28 06:05:43 -0800 | [diff] [blame] | 1114 | SkTArray<Config> configs; |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 1115 | create_configs(&configs); |
| 1116 | |
joshualitt | 3ebd050 | 2016-02-09 07:18:08 -0800 | [diff] [blame] | 1117 | #ifdef THERMAL_MANAGER_SUPPORTED |
| 1118 | int tmEnabled, tmThreshold, tmSleepTimeMs, tmTimeoutMs; |
| 1119 | if (4 != sscanf(FLAGS_useThermalManager[0], "%d,%d,%d,%d", |
| 1120 | &tmEnabled, &tmThreshold, &tmSleepTimeMs, &tmTimeoutMs)) { |
| 1121 | SkDebugf("Can't parse %s from --useThermalManager.\n", FLAGS_useThermalManager[0]); |
| 1122 | exit(1); |
| 1123 | } |
| 1124 | ThermalManager tm(tmThreshold, tmSleepTimeMs, tmTimeoutMs); |
| 1125 | #endif |
| 1126 | |
msarett | c149f0e | 2016-01-04 11:35:43 -0800 | [diff] [blame] | 1127 | if (FLAGS_keepAlive) { |
| 1128 | start_keepalive(); |
| 1129 | } |
| 1130 | |
mtklein | e070c2b | 2014-10-14 08:40:43 -0700 | [diff] [blame] | 1131 | int runs = 0; |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 1132 | BenchmarkStream benchStream; |
| 1133 | while (Benchmark* b = benchStream.next()) { |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 1134 | SkAutoTDelete<Benchmark> bench(b); |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 1135 | if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) { |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 1136 | continue; |
| 1137 | } |
| 1138 | |
svaisanen | c47635e | 2016-01-28 06:05:43 -0800 | [diff] [blame] | 1139 | if (!configs.empty()) { |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 1140 | log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY); |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 1141 | bench->delayedSetup(); |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 1142 | } |
egdaniel | 3bf9206 | 2015-06-26 08:12:46 -0700 | [diff] [blame] | 1143 | for (int i = 0; i < configs.count(); ++i) { |
joshualitt | 3ebd050 | 2016-02-09 07:18:08 -0800 | [diff] [blame] | 1144 | #ifdef THERMAL_MANAGER_SUPPORTED |
| 1145 | if (tmEnabled && !tm.coolOffIfNecessary()) { |
| 1146 | SkDebugf("Could not cool off, timings will be throttled\n"); |
| 1147 | } |
| 1148 | #endif |
egdaniel | 3bf9206 | 2015-06-26 08:12:46 -0700 | [diff] [blame] | 1149 | Target* target = is_enabled(b, configs[i]); |
| 1150 | if (!target) { |
| 1151 | continue; |
| 1152 | } |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 1153 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1154 | // During HWUI output this canvas may be nullptr. |
egdaniel | 3bf9206 | 2015-06-26 08:12:46 -0700 | [diff] [blame] | 1155 | SkCanvas* canvas = target->getCanvas(); |
svaisanen | c47635e | 2016-01-28 06:05:43 -0800 | [diff] [blame] | 1156 | const char* config = target->config.name.c_str(); |
egdaniel | 3bf9206 | 2015-06-26 08:12:46 -0700 | [diff] [blame] | 1157 | |
brianosman | 7a5ada8 | 2016-02-08 13:49:12 -0800 | [diff] [blame] | 1158 | if (FLAGS_pre_log || FLAGS_dryRun) { |
benjaminwagner | 8d61f0d | 2016-01-25 13:02:40 -0800 | [diff] [blame] | 1159 | SkDebugf("Running %s\t%s\n" |
| 1160 | , bench->getUniqueName() |
| 1161 | , config); |
brianosman | 7a5ada8 | 2016-02-08 13:49:12 -0800 | [diff] [blame] | 1162 | if (FLAGS_dryRun) { |
| 1163 | continue; |
| 1164 | } |
benjaminwagner | 8d61f0d | 2016-01-25 13:02:40 -0800 | [diff] [blame] | 1165 | } |
| 1166 | |
egdaniel | 3bf9206 | 2015-06-26 08:12:46 -0700 | [diff] [blame] | 1167 | target->setup(); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 1168 | bench->perCanvasPreDraw(canvas); |
| 1169 | |
cdalton | e1b8958 | 2015-06-25 19:17:08 -0700 | [diff] [blame] | 1170 | int maxFrameLag; |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 1171 | int loops = target->needsFrameTiming(&maxFrameLag) |
egdaniel | 3bf9206 | 2015-06-26 08:12:46 -0700 | [diff] [blame] | 1172 | ? setup_gpu_bench(target, bench.get(), maxFrameLag) |
| 1173 | : setup_cpu_bench(overhead, target, bench.get()); |
cdalton | e1b8958 | 2015-06-25 19:17:08 -0700 | [diff] [blame] | 1174 | |
mtklein | bbba168 | 2015-10-28 11:36:30 -0700 | [diff] [blame] | 1175 | if (FLAGS_ms) { |
| 1176 | samples.reset(); |
| 1177 | auto stop = now_ms() + FLAGS_ms; |
| 1178 | do { |
| 1179 | samples.push_back(time(loops, bench, target) / loops); |
| 1180 | } while (now_ms() < stop); |
| 1181 | } else { |
cdalton | e1b8958 | 2015-06-25 19:17:08 -0700 | [diff] [blame] | 1182 | samples.reset(FLAGS_samples); |
| 1183 | for (int s = 0; s < FLAGS_samples; s++) { |
egdaniel | 3bf9206 | 2015-06-26 08:12:46 -0700 | [diff] [blame] | 1184 | samples[s] = time(loops, bench, target) / loops; |
cdalton | e1b8958 | 2015-06-25 19:17:08 -0700 | [diff] [blame] | 1185 | } |
cdalton | e1b8958 | 2015-06-25 19:17:08 -0700 | [diff] [blame] | 1186 | } |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 1187 | |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 1188 | #if SK_SUPPORT_GPU |
| 1189 | SkTArray<SkString> keys; |
| 1190 | SkTArray<double> values; |
| 1191 | bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend; |
| 1192 | if (gpuStatsDump) { |
| 1193 | // TODO cache stats |
| 1194 | bench->getGpuStats(canvas, &keys, &values); |
| 1195 | } |
| 1196 | #endif |
| 1197 | |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 1198 | bench->perCanvasPostDraw(canvas); |
| 1199 | |
egdaniel | 3bf9206 | 2015-06-26 08:12:46 -0700 | [diff] [blame] | 1200 | if (Benchmark::kNonRendering_Backend != target->config.backend && |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 1201 | !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) { |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 1202 | SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config); |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 1203 | pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName()); |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 1204 | pngFilename.append(".png"); |
egdaniel | 3bf9206 | 2015-06-26 08:12:46 -0700 | [diff] [blame] | 1205 | write_canvas_png(target, pngFilename); |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | if (kFailedLoops == loops) { |
mtklein | 2069e22 | 2014-08-04 13:57:39 -0700 | [diff] [blame] | 1209 | // Can't be timed. A warning note has already been printed. |
egdaniel | 3bf9206 | 2015-06-26 08:12:46 -0700 | [diff] [blame] | 1210 | cleanup_run(target); |
Mike Klein | e363136 | 2014-07-15 17:56:37 -0400 | [diff] [blame] | 1211 | continue; |
| 1212 | } |
| 1213 | |
cdalton | e1b8958 | 2015-06-25 19:17:08 -0700 | [diff] [blame] | 1214 | Stats stats(samples); |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 1215 | log->config(config); |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 1216 | log->configOption("name", bench->getName()); |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 1217 | benchStream.fillCurrentOptions(log.get()); |
egdaniel | 3bf9206 | 2015-06-26 08:12:46 -0700 | [diff] [blame] | 1218 | target->fillOptions(log.get()); |
mtklein | 051e56d | 2014-12-04 08:46:51 -0800 | [diff] [blame] | 1219 | log->metric("min_ms", stats.min); |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 1220 | #if SK_SUPPORT_GPU |
| 1221 | if (gpuStatsDump) { |
| 1222 | // dump to json, only SKPBench currently returns valid keys / values |
| 1223 | SkASSERT(keys.count() == values.count()); |
| 1224 | for (int i = 0; i < keys.count(); i++) { |
| 1225 | log->metric(keys[i].c_str(), values[i]); |
| 1226 | } |
| 1227 | } |
| 1228 | #endif |
| 1229 | |
mtklein | e070c2b | 2014-10-14 08:40:43 -0700 | [diff] [blame] | 1230 | if (runs++ % FLAGS_flushEvery == 0) { |
| 1231 | log->flush(); |
| 1232 | } |
mtklein | 60317d0f | 2014-07-14 11:30:37 -0700 | [diff] [blame] | 1233 | |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 1234 | if (kAutoTuneLoops != FLAGS_loops) { |
egdaniel | 3bf9206 | 2015-06-26 08:12:46 -0700 | [diff] [blame] | 1235 | if (configs.count() == 1) { |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 1236 | config = ""; // Only print the config if we run the same bench on more than one. |
| 1237 | } |
mtklein | d75c466 | 2015-04-30 07:11:22 -0700 | [diff] [blame] | 1238 | SkDebugf("%4d/%-4dMB\t%s\t%s\n" |
| 1239 | , sk_tools::getCurrResidentSetSizeMB() |
| 1240 | , sk_tools::getMaxResidentSetSizeMB() |
mtklein | 53d2562 | 2014-09-18 07:39:42 -0700 | [diff] [blame] | 1241 | , bench->getUniqueName() |
| 1242 | , config); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 1243 | } else if (FLAGS_quiet) { |
mtklein | 66cfcff | 2015-12-04 06:35:30 -0800 | [diff] [blame] | 1244 | const char* mark = " "; |
| 1245 | const double stddev_percent = 100 * sqrt(stats.var) / stats.mean; |
| 1246 | if (stddev_percent > 5) mark = "?"; |
| 1247 | if (stddev_percent > 10) mark = "!"; |
| 1248 | |
| 1249 | SkDebugf("%10.2f %s\t%s\t%s\n", |
| 1250 | stats.median*1e3, mark, bench->getUniqueName(), config); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 1251 | } else { |
| 1252 | const double stddev_percent = 100 * sqrt(stats.var) / stats.mean; |
mtklein | d75c466 | 2015-04-30 07:11:22 -0700 | [diff] [blame] | 1253 | SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n" |
| 1254 | , sk_tools::getCurrResidentSetSizeMB() |
| 1255 | , sk_tools::getMaxResidentSetSizeMB() |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 1256 | , loops |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 1257 | , HUMANIZE(stats.min) |
| 1258 | , HUMANIZE(stats.median) |
| 1259 | , HUMANIZE(stats.mean) |
| 1260 | , HUMANIZE(stats.max) |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 1261 | , stddev_percent |
mtklein | bbba168 | 2015-10-28 11:36:30 -0700 | [diff] [blame] | 1262 | , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str() |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 1263 | , config |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 1264 | , bench->getUniqueName() |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 1265 | ); |
| 1266 | } |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 1267 | |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 1268 | #if SK_SUPPORT_GPU |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 1269 | if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) { |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 1270 | GrContext* context = gGrFactory->get(configs[i].ctxType, |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 1271 | configs[i].ctxOptions); |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 1272 | context->printCacheStats(); |
| 1273 | context->printGpuStats(); |
bsalomon | 06cddec | 2014-10-24 10:40:50 -0700 | [diff] [blame] | 1274 | } |
| 1275 | #endif |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 1276 | |
cdalton | 2c56ba5 | 2015-06-26 13:32:53 -0700 | [diff] [blame] | 1277 | if (FLAGS_verbose) { |
| 1278 | SkDebugf("Samples: "); |
| 1279 | for (int i = 0; i < samples.count(); i++) { |
| 1280 | SkDebugf("%s ", HUMANIZE(samples[i])); |
| 1281 | } |
| 1282 | SkDebugf("%s\n", bench->getUniqueName()); |
| 1283 | } |
egdaniel | 3bf9206 | 2015-06-26 08:12:46 -0700 | [diff] [blame] | 1284 | cleanup_run(target); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 1285 | } |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 1286 | } |
| 1287 | |
mtklein | e109145 | 2014-12-04 10:47:02 -0800 | [diff] [blame] | 1288 | log->bench("memory_usage", 0,0); |
| 1289 | log->config("meta"); |
| 1290 | log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB()); |
| 1291 | |
joshualitt | e0b19d4 | 2015-03-26 10:41:02 -0700 | [diff] [blame] | 1292 | #if SK_SUPPORT_GPU |
| 1293 | // Make sure we clean up the global GrContextFactory here, otherwise we might race with the |
| 1294 | // SkEventTracer destructor |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1295 | gGrFactory.reset(nullptr); |
joshualitt | e0b19d4 | 2015-03-26 10:41:02 -0700 | [diff] [blame] | 1296 | #endif |
| 1297 | |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 1298 | return 0; |
| 1299 | } |
| 1300 | |
jcgregorio | 3b27ade | 2014-11-13 08:06:40 -0800 | [diff] [blame] | 1301 | #if !defined SK_BUILD_FOR_IOS |
| 1302 | int main(int argc, char** argv) { |
| 1303 | SkCommandLineFlags::Parse(argc, argv); |
| 1304 | return nanobench_main(); |
| 1305 | } |
| 1306 | #endif |