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