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 | |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 10 | #include "Benchmark.h" |
| 11 | #include "CrashHandler.h" |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 12 | #include "GMBench.h" |
mtklein | afb4379 | 2014-08-19 15:55:55 -0700 | [diff] [blame] | 13 | #include "ProcStats.h" |
mtklein | 60317d0f | 2014-07-14 11:30:37 -0700 | [diff] [blame] | 14 | #include "ResultsWriter.h" |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 15 | #include "RecordingBench.h" |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 16 | #include "SKPBench.h" |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 17 | #include "Stats.h" |
| 18 | #include "Timer.h" |
| 19 | |
mtklein | 6838d85 | 2014-10-29 14:15:10 -0700 | [diff] [blame] | 20 | #include "SkBBoxHierarchy.h" |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 21 | #include "SkCanvas.h" |
caryclark | 17f0b6d | 2014-07-22 10:15:34 -0700 | [diff] [blame] | 22 | #include "SkCommonFlags.h" |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 23 | #include "SkForceLinking.h" |
| 24 | #include "SkGraphics.h" |
mtklein | 2084050 | 2014-08-21 15:51:22 -0700 | [diff] [blame] | 25 | #include "SkOSFile.h" |
| 26 | #include "SkPictureRecorder.h" |
mtklein | 051e56d | 2014-12-04 08:46:51 -0800 | [diff] [blame] | 27 | #include "SkPictureUtils.h" |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 28 | #include "SkString.h" |
| 29 | #include "SkSurface.h" |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 30 | #include "SkTaskGroup.h" |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 31 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 32 | #if SK_SUPPORT_GPU |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 33 | #include "gl/GrGLDefines.h" |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 34 | #include "GrContextFactory.h" |
krajcevski | 69a5560 | 2014-08-13 10:46:31 -0700 | [diff] [blame] | 35 | SkAutoTDelete<GrContextFactory> gGrFactory; |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 36 | #endif |
| 37 | |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 38 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 39 | |
reed | 5324978 | 2014-10-10 09:09:52 -0700 | [diff] [blame] | 40 | static const int kAutoTuneLoops = 0; |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 41 | |
mtklein | b511042 | 2014-08-07 15:20:02 -0700 | [diff] [blame] | 42 | static const int kDefaultLoops = |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 43 | #ifdef SK_DEBUG |
| 44 | 1; |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 45 | #else |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 46 | kAutoTuneLoops; |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 47 | #endif |
| 48 | |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 49 | static SkString loops_help_txt() { |
| 50 | SkString help; |
| 51 | help.printf("Number of times to run each bench. Set this to %d to auto-" |
| 52 | "tune for each bench. Timings are only reported when auto-tuning.", |
| 53 | kAutoTuneLoops); |
| 54 | return help; |
| 55 | } |
| 56 | |
| 57 | DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str()); |
| 58 | |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 59 | DEFINE_int32(samples, 10, "Number of samples to measure for each bench."); |
| 60 | DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead."); |
| 61 | DEFINE_double(overheadGoal, 0.0001, |
| 62 | "Loop until timer overhead is at most this fraction of our measurments."); |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 63 | DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU."); |
| 64 | DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag."); |
krajcevski | 12b3544 | 2014-08-13 12:06:26 -0700 | [diff] [blame] | 65 | DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to " |
| 66 | "software path rendering."); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 67 | |
mtklein | 60317d0f | 2014-07-14 11:30:37 -0700 | [diff] [blame] | 68 | DEFINE_string(outResultsFile, "", "If given, write results here as JSON."); |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 69 | DEFINE_int32(maxCalibrationAttempts, 3, |
| 70 | "Try up to this many times to guess loops for a bench, or skip the bench."); |
| 71 | DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this."); |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 72 | DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs."); |
| 73 | DEFINE_string(scales, "1.0", "Space-separated scales for SKPs."); |
mtklein | 2084050 | 2014-08-21 15:51:22 -0700 | [diff] [blame] | 74 | DEFINE_bool(bbh, true, "Build a BBH for SKPs?"); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 75 | DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?"); |
mtklein | e070c2b | 2014-10-14 08:40:43 -0700 | [diff] [blame] | 76 | DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run."); |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 77 | DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test."); |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 78 | |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 79 | static SkString humanize(double ms) { |
mtklein | dc5bbab | 2014-09-24 06:34:09 -0700 | [diff] [blame] | 80 | if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6)); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 81 | return HumanizeMs(ms); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 82 | } |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 83 | #define HUMANIZE(ms) humanize(ms).c_str() |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 84 | |
kkinnunen | 9e61bb7 | 2014-10-09 05:24:15 -0700 | [diff] [blame] | 85 | static double time(int loops, Benchmark* bench, SkCanvas* canvas, SkGLContext* gl) { |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 86 | if (canvas) { |
| 87 | canvas->clear(SK_ColorWHITE); |
| 88 | } |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 89 | WallTimer timer; |
| 90 | timer.start(); |
| 91 | if (bench) { |
| 92 | bench->draw(loops, canvas); |
| 93 | } |
| 94 | if (canvas) { |
| 95 | canvas->flush(); |
| 96 | } |
| 97 | #if SK_SUPPORT_GPU |
| 98 | if (gl) { |
| 99 | SK_GL(*gl, Flush()); |
| 100 | gl->swapBuffers(); |
| 101 | } |
| 102 | #endif |
| 103 | timer.end(); |
| 104 | return timer.fWall; |
| 105 | } |
| 106 | |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 107 | static double estimate_timer_overhead() { |
| 108 | double overhead = 0; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 109 | for (int i = 0; i < FLAGS_overheadLoops; i++) { |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 110 | overhead += time(1, NULL, NULL, NULL); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 111 | } |
| 112 | return overhead / FLAGS_overheadLoops; |
| 113 | } |
| 114 | |
reed | 5324978 | 2014-10-10 09:09:52 -0700 | [diff] [blame] | 115 | static int detect_forever_loops(int loops) { |
| 116 | // look for a magic run-forever value |
| 117 | if (loops < 0) { |
| 118 | loops = SK_MaxS32; |
| 119 | } |
| 120 | return loops; |
| 121 | } |
| 122 | |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 123 | static int clamp_loops(int loops) { |
| 124 | if (loops < 1) { |
mtklein | 527930f | 2014-11-06 08:04:34 -0800 | [diff] [blame] | 125 | SkDebugf("ERROR: clamping loops from %d to 1. " |
| 126 | "There's probably something wrong with the bench.\n", loops); |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 127 | return 1; |
| 128 | } |
| 129 | if (loops > FLAGS_maxLoops) { |
| 130 | SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops); |
| 131 | return FLAGS_maxLoops; |
| 132 | } |
| 133 | return loops; |
| 134 | } |
| 135 | |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 136 | static bool write_canvas_png(SkCanvas* canvas, const SkString& filename) { |
| 137 | if (filename.isEmpty()) { |
| 138 | return false; |
| 139 | } |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 140 | if (kUnknown_SkColorType == canvas->imageInfo().colorType()) { |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 141 | return false; |
| 142 | } |
| 143 | SkBitmap bmp; |
| 144 | bmp.setInfo(canvas->imageInfo()); |
| 145 | if (!canvas->readPixels(&bmp, 0, 0)) { |
| 146 | SkDebugf("Can't read canvas pixels.\n"); |
| 147 | return false; |
| 148 | } |
| 149 | SkString dir = SkOSPath::Dirname(filename.c_str()); |
| 150 | if (!sk_mkdir(dir.c_str())) { |
| 151 | SkDebugf("Can't make dir %s.\n", dir.c_str()); |
| 152 | return false; |
| 153 | } |
| 154 | SkFILEWStream stream(filename.c_str()); |
| 155 | if (!stream.isValid()) { |
| 156 | SkDebugf("Can't write %s.\n", filename.c_str()); |
| 157 | return false; |
| 158 | } |
| 159 | if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) { |
| 160 | SkDebugf("Can't encode a PNG.\n"); |
| 161 | return false; |
| 162 | } |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | static int kFailedLoops = -2; |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 167 | static int cpu_bench(const double overhead, Benchmark* bench, SkCanvas* canvas, double* samples) { |
| 168 | // 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] | 169 | double bench_plus_overhead = 0.0; |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 170 | int round = 0; |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 171 | if (kAutoTuneLoops == FLAGS_loops) { |
| 172 | while (bench_plus_overhead < overhead) { |
| 173 | if (round++ == FLAGS_maxCalibrationAttempts) { |
| 174 | SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n", |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 175 | bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead)); |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 176 | return kFailedLoops; |
| 177 | } |
| 178 | bench_plus_overhead = time(1, bench, canvas, NULL); |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 179 | } |
mtklein | 2069e22 | 2014-08-04 13:57:39 -0700 | [diff] [blame] | 180 | } |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 181 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 182 | // 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] | 183 | // We'll pick N to make timer overhead negligible: |
| 184 | // |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 185 | // overhead |
| 186 | // ------------------------- < FLAGS_overheadGoal |
| 187 | // overhead + N * Bench Time |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 188 | // |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 189 | // where bench_plus_overhead ≈ overhead + Bench Time. |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 190 | // |
| 191 | // Doing some math, we get: |
| 192 | // |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 193 | // (overhead / FLAGS_overheadGoal) - overhead |
| 194 | // ------------------------------------------ < N |
| 195 | // bench_plus_overhead - overhead) |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 196 | // |
| 197 | // Luckily, this also works well in practice. :) |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 198 | int loops = FLAGS_loops; |
| 199 | if (kAutoTuneLoops == loops) { |
| 200 | const double numer = overhead / FLAGS_overheadGoal - overhead; |
| 201 | const double denom = bench_plus_overhead - overhead; |
| 202 | loops = (int)ceil(numer / denom); |
reed | 5324978 | 2014-10-10 09:09:52 -0700 | [diff] [blame] | 203 | loops = clamp_loops(loops); |
| 204 | } else { |
| 205 | loops = detect_forever_loops(loops); |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 206 | } |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 207 | |
| 208 | for (int i = 0; i < FLAGS_samples; i++) { |
| 209 | samples[i] = time(loops, bench, canvas, NULL) / loops; |
| 210 | } |
| 211 | return loops; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 212 | } |
| 213 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 214 | #if SK_SUPPORT_GPU |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 215 | static void setup_gl(SkGLContext* gl) { |
| 216 | gl->makeCurrent(); |
| 217 | // Make sure we're done with whatever came before. |
| 218 | SK_GL(*gl, Finish()); |
| 219 | } |
| 220 | |
kkinnunen | 9e61bb7 | 2014-10-09 05:24:15 -0700 | [diff] [blame] | 221 | static int gpu_bench(SkGLContext* gl, |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 222 | Benchmark* bench, |
| 223 | SkCanvas* canvas, |
| 224 | double* samples) { |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 225 | // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs. |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 226 | int loops = FLAGS_loops; |
| 227 | if (kAutoTuneLoops == loops) { |
| 228 | loops = 1; |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 229 | double elapsed = 0; |
| 230 | do { |
mtklein | 527930f | 2014-11-06 08:04:34 -0800 | [diff] [blame] | 231 | if (1<<30 == loops) { |
| 232 | // We're about to wrap. Something's wrong with the bench. |
| 233 | loops = 0; |
| 234 | break; |
| 235 | } |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 236 | loops *= 2; |
| 237 | // If the GPU lets frames lag at all, we need to make sure we're timing |
| 238 | // _this_ round, not still timing last round. We force this by looping |
| 239 | // more times than any reasonable GPU will allow frames to lag. |
| 240 | for (int i = 0; i < FLAGS_gpuFrameLag; i++) { |
| 241 | elapsed = time(loops, bench, canvas, gl); |
| 242 | } |
| 243 | } while (elapsed < FLAGS_gpuMs); |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 244 | |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 245 | // We've overshot at least a little. Scale back linearly. |
| 246 | loops = (int)ceil(loops * FLAGS_gpuMs / elapsed); |
reed | 5324978 | 2014-10-10 09:09:52 -0700 | [diff] [blame] | 247 | loops = clamp_loops(loops); |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 248 | |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 249 | // Might as well make sure we're not still timing our calibration. |
| 250 | SK_GL(*gl, Finish()); |
reed | 5324978 | 2014-10-10 09:09:52 -0700 | [diff] [blame] | 251 | } else { |
| 252 | loops = detect_forever_loops(loops); |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 253 | } |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 254 | |
| 255 | // Pretty much the same deal as the calibration: do some warmup to make |
| 256 | // sure we're timing steady-state pipelined frames. |
| 257 | for (int i = 0; i < FLAGS_gpuFrameLag; i++) { |
| 258 | time(loops, bench, canvas, gl); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 259 | } |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 260 | |
| 261 | // Now, actually do the timing! |
| 262 | for (int i = 0; i < FLAGS_samples; i++) { |
| 263 | samples[i] = time(loops, bench, canvas, gl) / loops; |
| 264 | } |
| 265 | return loops; |
| 266 | } |
| 267 | #endif |
| 268 | |
| 269 | static SkString to_lower(const char* str) { |
| 270 | SkString lower(str); |
| 271 | for (size_t i = 0; i < lower.size(); i++) { |
| 272 | lower[i] = tolower(lower[i]); |
| 273 | } |
| 274 | return lower; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 275 | } |
| 276 | |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 277 | struct Config { |
| 278 | const char* name; |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 279 | Benchmark::Backend backend; |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 280 | SkColorType color; |
| 281 | SkAlphaType alpha; |
| 282 | int samples; |
| 283 | #if SK_SUPPORT_GPU |
| 284 | GrContextFactory::GLContextType ctxType; |
jvanverth | 4736e14 | 2014-11-07 07:12:46 -0800 | [diff] [blame] | 285 | bool useDFText; |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 286 | #else |
| 287 | int bogusInt; |
jvanverth | 4736e14 | 2014-11-07 07:12:46 -0800 | [diff] [blame] | 288 | bool bogusBool; |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 289 | #endif |
| 290 | }; |
| 291 | |
| 292 | struct Target { |
| 293 | explicit Target(const Config& c) : config(c) {} |
| 294 | const Config config; |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 295 | SkAutoTDelete<SkSurface> surface; |
| 296 | #if SK_SUPPORT_GPU |
kkinnunen | 9e61bb7 | 2014-10-09 05:24:15 -0700 | [diff] [blame] | 297 | SkGLContext* gl; |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 298 | #endif |
| 299 | }; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 300 | |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 301 | static bool is_cpu_config_allowed(const char* name) { |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 302 | for (int i = 0; i < FLAGS_config.count(); i++) { |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 303 | if (to_lower(FLAGS_config[i]).equals(name)) { |
| 304 | return true; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 305 | } |
| 306 | } |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 307 | return false; |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 308 | } |
| 309 | |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 310 | #if SK_SUPPORT_GPU |
| 311 | static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType, |
| 312 | int sampleCnt) { |
| 313 | if (!is_cpu_config_allowed(name)) { |
| 314 | return false; |
| 315 | } |
krajcevski | 69a5560 | 2014-08-13 10:46:31 -0700 | [diff] [blame] | 316 | if (const GrContext* ctx = gGrFactory->get(ctxType)) { |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 317 | return sampleCnt <= ctx->getMaxSampleCount(); |
| 318 | } |
| 319 | return false; |
| 320 | } |
| 321 | #endif |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 322 | |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 323 | #if SK_SUPPORT_GPU |
| 324 | #define kBogusGLContextType GrContextFactory::kNative_GLContextType |
| 325 | #else |
| 326 | #define kBogusGLContextType 0 |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 327 | #endif |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 328 | |
| 329 | // Append all configs that are enabled and supported. |
| 330 | static void create_configs(SkTDArray<Config>* configs) { |
jvanverth | 4736e14 | 2014-11-07 07:12:46 -0800 | [diff] [blame] | 331 | #define CPU_CONFIG(name, backend, color, alpha) \ |
| 332 | if (is_cpu_config_allowed(#name)) { \ |
| 333 | Config config = { #name, Benchmark::backend, color, alpha, 0, \ |
| 334 | kBogusGLContextType, false }; \ |
| 335 | configs->push(config); \ |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 336 | } |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 337 | |
mtklein | 40b32be | 2014-07-09 08:46:49 -0700 | [diff] [blame] | 338 | if (FLAGS_cpu) { |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 339 | CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType) |
| 340 | CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType) |
| 341 | CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType) |
mtklein | 40b32be | 2014-07-09 08:46:49 -0700 | [diff] [blame] | 342 | } |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 343 | |
| 344 | #if SK_SUPPORT_GPU |
jvanverth | 4736e14 | 2014-11-07 07:12:46 -0800 | [diff] [blame] | 345 | #define GPU_CONFIG(name, ctxType, samples, useDFText) \ |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 346 | if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \ |
| 347 | Config config = { \ |
| 348 | #name, \ |
| 349 | Benchmark::kGPU_Backend, \ |
| 350 | kN32_SkColorType, \ |
| 351 | kPremul_SkAlphaType, \ |
| 352 | samples, \ |
jvanverth | 4736e14 | 2014-11-07 07:12:46 -0800 | [diff] [blame] | 353 | GrContextFactory::ctxType, \ |
| 354 | useDFText }; \ |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 355 | configs->push(config); \ |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 356 | } |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 357 | |
mtklein | 40b32be | 2014-07-09 08:46:49 -0700 | [diff] [blame] | 358 | if (FLAGS_gpu) { |
jvanverth | 4736e14 | 2014-11-07 07:12:46 -0800 | [diff] [blame] | 359 | GPU_CONFIG(gpu, kNative_GLContextType, 0, false) |
| 360 | GPU_CONFIG(msaa4, kNative_GLContextType, 4, false) |
| 361 | GPU_CONFIG(msaa16, kNative_GLContextType, 16, false) |
| 362 | GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4, false) |
| 363 | GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16, false) |
| 364 | GPU_CONFIG(gpudft, kNative_GLContextType, 0, true) |
| 365 | GPU_CONFIG(debug, kDebug_GLContextType, 0, false) |
| 366 | GPU_CONFIG(nullgpu, kNull_GLContextType, 0, false) |
bsalomon | 3b4d077 | 2014-08-06 10:52:33 -0700 | [diff] [blame] | 367 | #ifdef SK_ANGLE |
jvanverth | 4736e14 | 2014-11-07 07:12:46 -0800 | [diff] [blame] | 368 | GPU_CONFIG(angle, kANGLE_GLContextType, 0, false) |
bsalomon | 3b4d077 | 2014-08-06 10:52:33 -0700 | [diff] [blame] | 369 | #endif |
mtklein | 40b32be | 2014-07-09 08:46:49 -0700 | [diff] [blame] | 370 | } |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 371 | #endif |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 372 | } |
| 373 | |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 374 | // If bench is enabled for config, returns a Target* for it, otherwise NULL. |
| 375 | static Target* is_enabled(Benchmark* bench, const Config& config) { |
| 376 | if (!bench->isSuitableFor(config.backend)) { |
| 377 | return NULL; |
| 378 | } |
| 379 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 380 | SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY, |
| 381 | config.color, config.alpha); |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 382 | |
| 383 | Target* target = new Target(config); |
| 384 | |
| 385 | if (Benchmark::kRaster_Backend == config.backend) { |
| 386 | target->surface.reset(SkSurface::NewRaster(info)); |
| 387 | } |
| 388 | #if SK_SUPPORT_GPU |
| 389 | else if (Benchmark::kGPU_Backend == config.backend) { |
jvanverth | 4736e14 | 2014-11-07 07:12:46 -0800 | [diff] [blame] | 390 | uint32_t flags = config.useDFText ? SkSurfaceProps::kUseDistanceFieldFonts_Flag : 0; |
| 391 | SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 392 | target->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(config.ctxType), |
| 393 | SkSurface::kNo_Budgeted, info, |
jvanverth | 4736e14 | 2014-11-07 07:12:46 -0800 | [diff] [blame] | 394 | config.samples, &props)); |
krajcevski | 69a5560 | 2014-08-13 10:46:31 -0700 | [diff] [blame] | 395 | target->gl = gGrFactory->getGLContext(config.ctxType); |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 396 | } |
| 397 | #endif |
| 398 | |
| 399 | if (Benchmark::kNonRendering_Backend != config.backend && !target->surface.get()) { |
| 400 | delete target; |
| 401 | return NULL; |
| 402 | } |
| 403 | return target; |
| 404 | } |
| 405 | |
| 406 | // Creates targets for a benchmark and a set of configs. |
| 407 | static void create_targets(SkTDArray<Target*>* targets, Benchmark* b, |
| 408 | const SkTDArray<Config>& configs) { |
| 409 | for (int i = 0; i < configs.count(); ++i) { |
| 410 | if (Target* t = is_enabled(b, configs[i])) { |
| 411 | targets->push(t); |
| 412 | } |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 413 | |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 417 | #if SK_SUPPORT_GPU |
kkinnunen | 9e61bb7 | 2014-10-09 05:24:15 -0700 | [diff] [blame] | 418 | static void fill_gpu_options(ResultsWriter* log, SkGLContext* ctx) { |
jcgregorio | 05c4560 | 2014-07-17 13:54:36 -0700 | [diff] [blame] | 419 | const GrGLubyte* version; |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 420 | SK_GL_RET(*ctx, version, GetString(GR_GL_VERSION)); |
| 421 | log->configOption("GL_VERSION", (const char*)(version)); |
| 422 | |
| 423 | SK_GL_RET(*ctx, version, GetString(GR_GL_RENDERER)); |
| 424 | log->configOption("GL_RENDERER", (const char*) version); |
| 425 | |
| 426 | SK_GL_RET(*ctx, version, GetString(GR_GL_VENDOR)); |
| 427 | log->configOption("GL_VENDOR", (const char*) version); |
| 428 | |
| 429 | SK_GL_RET(*ctx, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION)); |
| 430 | log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version); |
| 431 | } |
| 432 | #endif |
| 433 | |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 434 | class BenchmarkStream { |
| 435 | public: |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 436 | BenchmarkStream() : fBenches(BenchRegistry::Head()) |
| 437 | , fGMs(skiagm::GMRegistry::Head()) |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 438 | , fCurrentRecording(0) |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 439 | , fCurrentScale(0) |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 440 | , fCurrentSKP(0) |
| 441 | , fCurrentUseMPD(0) { |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 442 | for (int i = 0; i < FLAGS_skps.count(); i++) { |
| 443 | if (SkStrEndsWith(FLAGS_skps[i], ".skp")) { |
| 444 | fSKPs.push_back() = FLAGS_skps[i]; |
| 445 | } else { |
| 446 | SkOSFile::Iter it(FLAGS_skps[i], ".skp"); |
| 447 | SkString path; |
| 448 | while (it.next(&path)) { |
| 449 | fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str()); |
| 450 | } |
| 451 | } |
| 452 | } |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 453 | |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 454 | if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d", |
| 455 | &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) { |
| 456 | SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]); |
| 457 | exit(1); |
| 458 | } |
| 459 | |
| 460 | for (int i = 0; i < FLAGS_scales.count(); i++) { |
| 461 | if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) { |
| 462 | SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]); |
| 463 | exit(1); |
| 464 | } |
| 465 | } |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 466 | |
| 467 | fUseMPDs.push_back() = false; |
| 468 | if (FLAGS_mpd) { |
| 469 | fUseMPDs.push_back() = true; |
| 470 | } |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 471 | } |
| 472 | |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 473 | static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) { |
| 474 | // Not strictly necessary, as it will be checked again later, |
| 475 | // but helps to avoid a lot of pointless work if we're going to skip it. |
| 476 | if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) { |
| 477 | return false; |
| 478 | } |
| 479 | |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 480 | SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path)); |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 481 | if (stream.get() == NULL) { |
| 482 | SkDebugf("Could not read %s.\n", path); |
| 483 | return false; |
| 484 | } |
| 485 | |
mtklein | 535776e | 2014-11-25 14:57:26 -0800 | [diff] [blame] | 486 | pic->reset(SkPicture::CreateFromStream(stream.get())); |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 487 | if (pic->get() == NULL) { |
| 488 | SkDebugf("Could not read %s as an SkPicture.\n", path); |
| 489 | return false; |
| 490 | } |
| 491 | return true; |
| 492 | } |
| 493 | |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 494 | Benchmark* next() { |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 495 | if (fBenches) { |
| 496 | Benchmark* bench = fBenches->factory()(NULL); |
| 497 | fBenches = fBenches->next(); |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 498 | fSourceType = "bench"; |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 499 | fBenchType = "micro"; |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 500 | return bench; |
| 501 | } |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 502 | |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 503 | while (fGMs) { |
| 504 | SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL)); |
| 505 | fGMs = fGMs->next(); |
mtklein | cf5d9c9 | 2015-01-23 10:31:45 -0800 | [diff] [blame^] | 506 | if (gm->runAsBench()) { |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 507 | fSourceType = "gm"; |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 508 | fBenchType = "micro"; |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 509 | return SkNEW_ARGS(GMBench, (gm.detach())); |
| 510 | } |
| 511 | } |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 512 | |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 513 | // First add all .skps as RecordingBenches. |
| 514 | while (fCurrentRecording < fSKPs.count()) { |
| 515 | const SkString& path = fSKPs[fCurrentRecording++]; |
| 516 | SkAutoTUnref<SkPicture> pic; |
| 517 | if (!ReadPicture(path.c_str(), &pic)) { |
| 518 | continue; |
| 519 | } |
| 520 | SkString name = SkOSPath::Basename(path.c_str()); |
| 521 | fSourceType = "skp"; |
| 522 | fBenchType = "recording"; |
bsalomon | 0aa5cea | 2014-12-15 09:13:35 -0800 | [diff] [blame] | 523 | fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic)); |
mtklein | 051e56d | 2014-12-04 08:46:51 -0800 | [diff] [blame] | 524 | fSKPOps = pic->approximateOpCount(); |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 525 | return SkNEW_ARGS(RecordingBench, (name.c_str(), pic.get(), FLAGS_bbh)); |
| 526 | } |
| 527 | |
| 528 | // Then once each for each scale as SKPBenches (playback). |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 529 | while (fCurrentScale < fScales.count()) { |
| 530 | while (fCurrentSKP < fSKPs.count()) { |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 531 | const SkString& path = fSKPs[fCurrentSKP]; |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 532 | SkAutoTUnref<SkPicture> pic; |
| 533 | if (!ReadPicture(path.c_str(), &pic)) { |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 534 | fCurrentSKP++; |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 535 | continue; |
| 536 | } |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 537 | |
| 538 | while (fCurrentUseMPD < fUseMPDs.count()) { |
| 539 | if (FLAGS_bbh) { |
| 540 | // The SKP we read off disk doesn't have a BBH. Re-record so it grows one. |
| 541 | SkRTreeFactory factory; |
| 542 | SkPictureRecorder recorder; |
| 543 | static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag; |
| 544 | pic->playback(recorder.beginRecording(pic->cullRect().width(), |
| 545 | pic->cullRect().height(), |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 546 | &factory, |
robertphillips | e451c4d | 2014-12-09 10:28:00 -0800 | [diff] [blame] | 547 | fUseMPDs[fCurrentUseMPD] ? kFlags : 0)); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 548 | pic.reset(recorder.endRecording()); |
| 549 | } |
| 550 | SkString name = SkOSPath::Basename(path.c_str()); |
| 551 | fSourceType = "skp"; |
| 552 | fBenchType = "playback"; |
| 553 | return SkNEW_ARGS(SKPBench, |
| 554 | (name.c_str(), pic.get(), fClip, |
| 555 | fScales[fCurrentScale], fUseMPDs[fCurrentUseMPD++])); |
mtklein | 2084050 | 2014-08-21 15:51:22 -0700 | [diff] [blame] | 556 | } |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 557 | fCurrentUseMPD = 0; |
| 558 | fCurrentSKP++; |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 559 | } |
| 560 | fCurrentSKP = 0; |
| 561 | fCurrentScale++; |
| 562 | } |
| 563 | |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 564 | return NULL; |
| 565 | } |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 566 | |
| 567 | void fillCurrentOptions(ResultsWriter* log) const { |
| 568 | log->configOption("source_type", fSourceType); |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 569 | log->configOption("bench_type", fBenchType); |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 570 | if (0 == strcmp(fSourceType, "skp")) { |
| 571 | log->configOption("clip", |
| 572 | SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop, |
| 573 | fClip.fRight, fClip.fBottom).c_str()); |
| 574 | log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str()); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 575 | if (fCurrentUseMPD > 0) { |
| 576 | SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD); |
| 577 | log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false"); |
| 578 | } |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 579 | } |
mtklein | 051e56d | 2014-12-04 08:46:51 -0800 | [diff] [blame] | 580 | if (0 == strcmp(fBenchType, "recording")) { |
| 581 | log->metric("bytes", fSKPBytes); |
| 582 | log->metric("ops", fSKPOps); |
| 583 | } |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 584 | } |
| 585 | |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 586 | private: |
| 587 | const BenchRegistry* fBenches; |
| 588 | const skiagm::GMRegistry* fGMs; |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 589 | SkIRect fClip; |
| 590 | SkTArray<SkScalar> fScales; |
| 591 | SkTArray<SkString> fSKPs; |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 592 | SkTArray<bool> fUseMPDs; |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 593 | |
mtklein | 051e56d | 2014-12-04 08:46:51 -0800 | [diff] [blame] | 594 | double fSKPBytes, fSKPOps; |
| 595 | |
mtklein | fd731ce | 2014-09-10 12:19:30 -0700 | [diff] [blame] | 596 | const char* fSourceType; // What we're benching: bench, GM, SKP, ... |
| 597 | const char* fBenchType; // How we bench it: micro, recording, playback, ... |
| 598 | int fCurrentRecording; |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 599 | int fCurrentScale; |
| 600 | int fCurrentSKP; |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 601 | int fCurrentUseMPD; |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 602 | }; |
| 603 | |
jcgregorio | 3b27ade | 2014-11-13 08:06:40 -0800 | [diff] [blame] | 604 | int nanobench_main(); |
caryclark | 17f0b6d | 2014-07-22 10:15:34 -0700 | [diff] [blame] | 605 | int nanobench_main() { |
jcgregorio | 3b27ade | 2014-11-13 08:06:40 -0800 | [diff] [blame] | 606 | SetupCrashHandler(); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 607 | SkAutoGraphics ag; |
mtklein | 4f10844 | 2014-12-03 13:07:39 -0800 | [diff] [blame] | 608 | SkTaskGroup::Enabler enabled; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 609 | |
krajcevski | 69a5560 | 2014-08-13 10:46:31 -0700 | [diff] [blame] | 610 | #if SK_SUPPORT_GPU |
krajcevski | 12b3544 | 2014-08-13 12:06:26 -0700 | [diff] [blame] | 611 | GrContext::Options grContextOpts; |
| 612 | grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks; |
| 613 | gGrFactory.reset(SkNEW_ARGS(GrContextFactory, (grContextOpts))); |
krajcevski | 69a5560 | 2014-08-13 10:46:31 -0700 | [diff] [blame] | 614 | #endif |
| 615 | |
bsalomon | 06cddec | 2014-10-24 10:40:50 -0700 | [diff] [blame] | 616 | if (FLAGS_veryVerbose) { |
| 617 | FLAGS_verbose = true; |
| 618 | } |
| 619 | |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 620 | if (kAutoTuneLoops != FLAGS_loops) { |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 621 | FLAGS_samples = 1; |
| 622 | FLAGS_gpuFrameLag = 0; |
| 623 | } |
| 624 | |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 625 | if (!FLAGS_writePath.isEmpty()) { |
| 626 | SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]); |
| 627 | if (!sk_mkdir(FLAGS_writePath[0])) { |
| 628 | SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]); |
| 629 | FLAGS_writePath.set(0, NULL); |
| 630 | } |
| 631 | } |
| 632 | |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 633 | SkAutoTDelete<ResultsWriter> log(SkNEW(ResultsWriter)); |
mtklein | 60317d0f | 2014-07-14 11:30:37 -0700 | [diff] [blame] | 634 | if (!FLAGS_outResultsFile.isEmpty()) { |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 635 | log.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0]))); |
mtklein | 60317d0f | 2014-07-14 11:30:37 -0700 | [diff] [blame] | 636 | } |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 637 | |
| 638 | if (1 == FLAGS_properties.count() % 2) { |
| 639 | SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n"); |
| 640 | return 1; |
| 641 | } |
| 642 | for (int i = 1; i < FLAGS_properties.count(); i += 2) { |
| 643 | log->property(FLAGS_properties[i-1], FLAGS_properties[i]); |
| 644 | } |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 645 | |
| 646 | if (1 == FLAGS_key.count() % 2) { |
| 647 | SkDebugf("ERROR: --key must be passed with an even number of arguments.\n"); |
| 648 | return 1; |
| 649 | } |
| 650 | for (int i = 1; i < FLAGS_key.count(); i += 2) { |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 651 | log->key(FLAGS_key[i-1], FLAGS_key[i]); |
mtklein | 94e5156 | 2014-08-19 12:41:53 -0700 | [diff] [blame] | 652 | } |
mtklein | 60317d0f | 2014-07-14 11:30:37 -0700 | [diff] [blame] | 653 | |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 654 | const double overhead = estimate_timer_overhead(); |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 655 | SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead)); |
Mike Klein | 9129477 | 2014-07-16 19:59:32 -0400 | [diff] [blame] | 656 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 657 | SkAutoTMalloc<double> samples(FLAGS_samples); |
| 658 | |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 659 | if (kAutoTuneLoops != FLAGS_loops) { |
| 660 | SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n"); |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 661 | } else if (FLAGS_verbose) { |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 662 | // No header. |
| 663 | } else if (FLAGS_quiet) { |
mtklein | 40b32be | 2014-07-09 08:46:49 -0700 | [diff] [blame] | 664 | SkDebugf("median\tbench\tconfig\n"); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 665 | } else { |
qiankun.miao | 8247ec3 | 2014-09-09 19:24:36 -0700 | [diff] [blame] | 666 | SkDebugf("maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n", |
| 667 | FLAGS_samples, "samples"); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 668 | } |
| 669 | |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 670 | SkTDArray<Config> configs; |
| 671 | create_configs(&configs); |
| 672 | |
mtklein | e070c2b | 2014-10-14 08:40:43 -0700 | [diff] [blame] | 673 | int runs = 0; |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 674 | BenchmarkStream benchStream; |
| 675 | while (Benchmark* b = benchStream.next()) { |
mtklein | e714e75 | 2014-07-31 12:13:48 -0700 | [diff] [blame] | 676 | SkAutoTDelete<Benchmark> bench(b); |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 677 | if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) { |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 678 | continue; |
| 679 | } |
| 680 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 681 | SkTDArray<Target*> targets; |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 682 | create_targets(&targets, bench.get(), configs); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 683 | |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 684 | if (!targets.isEmpty()) { |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 685 | log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY); |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 686 | bench->preDraw(); |
| 687 | } |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 688 | for (int j = 0; j < targets.count(); j++) { |
| 689 | SkCanvas* canvas = targets[j]->surface.get() ? targets[j]->surface->getCanvas() : NULL; |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 690 | const char* config = targets[j]->config.name; |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 691 | |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 692 | #if SK_SUPPORT_GPU |
| 693 | if (Benchmark::kGPU_Backend == targets[j]->config.backend) { |
| 694 | setup_gl(targets[j]->gl); |
| 695 | } |
| 696 | #endif |
| 697 | |
| 698 | bench->perCanvasPreDraw(canvas); |
| 699 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 700 | const int loops = |
| 701 | #if SK_SUPPORT_GPU |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 702 | Benchmark::kGPU_Backend == targets[j]->config.backend |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 703 | ? gpu_bench(targets[j]->gl, bench.get(), canvas, samples.get()) |
| 704 | : |
| 705 | #endif |
| 706 | cpu_bench( overhead, bench.get(), canvas, samples.get()); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 707 | |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 708 | bench->perCanvasPostDraw(canvas); |
| 709 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 710 | if (canvas && !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) { |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 711 | SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config); |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 712 | pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName()); |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 713 | pngFilename.append(".png"); |
| 714 | write_canvas_png(canvas, pngFilename); |
| 715 | } |
| 716 | |
| 717 | if (kFailedLoops == loops) { |
mtklein | 2069e22 | 2014-08-04 13:57:39 -0700 | [diff] [blame] | 718 | // Can't be timed. A warning note has already been printed. |
Mike Klein | e363136 | 2014-07-15 17:56:37 -0400 | [diff] [blame] | 719 | continue; |
| 720 | } |
| 721 | |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 722 | Stats stats(samples.get(), FLAGS_samples); |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 723 | log->config(config); |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 724 | log->configOption("name", bench->getName()); |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 725 | benchStream.fillCurrentOptions(log.get()); |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 726 | #if SK_SUPPORT_GPU |
bsalomon | c255337 | 2014-07-22 13:09:05 -0700 | [diff] [blame] | 727 | if (Benchmark::kGPU_Backend == targets[j]->config.backend) { |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 728 | fill_gpu_options(log.get(), targets[j]->gl); |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 729 | } |
| 730 | #endif |
mtklein | 051e56d | 2014-12-04 08:46:51 -0800 | [diff] [blame] | 731 | log->metric("min_ms", stats.min); |
mtklein | e070c2b | 2014-10-14 08:40:43 -0700 | [diff] [blame] | 732 | if (runs++ % FLAGS_flushEvery == 0) { |
| 733 | log->flush(); |
| 734 | } |
mtklein | 60317d0f | 2014-07-14 11:30:37 -0700 | [diff] [blame] | 735 | |
bsalomon | 6eb03cc | 2014-08-07 14:28:50 -0700 | [diff] [blame] | 736 | if (kAutoTuneLoops != FLAGS_loops) { |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 737 | if (targets.count() == 1) { |
| 738 | config = ""; // Only print the config if we run the same bench on more than one. |
| 739 | } |
mtklein | 53d2562 | 2014-09-18 07:39:42 -0700 | [diff] [blame] | 740 | SkDebugf("%4dM\t%s\t%s\n" |
| 741 | , sk_tools::getMaxResidentSetSizeMB() |
| 742 | , bench->getUniqueName() |
| 743 | , config); |
mtklein | a189ccd | 2014-07-14 12:28:47 -0700 | [diff] [blame] | 744 | } else if (FLAGS_verbose) { |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 745 | for (int i = 0; i < FLAGS_samples; i++) { |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 746 | SkDebugf("%s ", HUMANIZE(samples[i])); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 747 | } |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 748 | SkDebugf("%s\n", bench->getUniqueName()); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 749 | } else if (FLAGS_quiet) { |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 750 | if (targets.count() == 1) { |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 751 | config = ""; // Only print the config if we run the same bench on more than one. |
| 752 | } |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 753 | SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config); |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 754 | } else { |
| 755 | const double stddev_percent = 100 * sqrt(stats.var) / stats.mean; |
mtklein | afb4379 | 2014-08-19 15:55:55 -0700 | [diff] [blame] | 756 | SkDebugf("%4dM\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n" |
| 757 | , sk_tools::getMaxResidentSetSizeMB() |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 758 | , loops |
mtklein | 55b0ffc | 2014-07-17 08:38:23 -0700 | [diff] [blame] | 759 | , HUMANIZE(stats.min) |
| 760 | , HUMANIZE(stats.median) |
| 761 | , HUMANIZE(stats.mean) |
| 762 | , HUMANIZE(stats.max) |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 763 | , stddev_percent |
mtklein | 5d9d10e | 2014-07-11 11:57:07 -0700 | [diff] [blame] | 764 | , stats.plot.c_str() |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 765 | , config |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 766 | , bench->getUniqueName() |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 767 | ); |
| 768 | } |
bsalomon | 06cddec | 2014-10-24 10:40:50 -0700 | [diff] [blame] | 769 | #if SK_SUPPORT_GPU && GR_CACHE_STATS |
| 770 | if (FLAGS_veryVerbose && |
| 771 | Benchmark::kGPU_Backend == targets[j]->config.backend) { |
mtklein | 6838d85 | 2014-10-29 14:15:10 -0700 | [diff] [blame] | 772 | gGrFactory->get(targets[j]->config.ctxType)->printCacheStats(); |
bsalomon | 06cddec | 2014-10-24 10:40:50 -0700 | [diff] [blame] | 773 | } |
| 774 | #endif |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 775 | } |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 776 | targets.deleteAll(); |
Mike Klein | 3944a1d | 2014-07-15 13:40:19 -0400 | [diff] [blame] | 777 | |
bsalomon | 06cddec | 2014-10-24 10:40:50 -0700 | [diff] [blame] | 778 | #if SK_SUPPORT_GPU |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 779 | if (FLAGS_abandonGpuContext) { |
krajcevski | 69a5560 | 2014-08-13 10:46:31 -0700 | [diff] [blame] | 780 | gGrFactory->abandonContexts(); |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 781 | } |
| 782 | if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) { |
krajcevski | 69a5560 | 2014-08-13 10:46:31 -0700 | [diff] [blame] | 783 | gGrFactory->destroyContexts(); |
Mike Klein | 3944a1d | 2014-07-15 13:40:19 -0400 | [diff] [blame] | 784 | } |
bsalomon | 06cddec | 2014-10-24 10:40:50 -0700 | [diff] [blame] | 785 | #endif |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 786 | } |
| 787 | |
mtklein | e109145 | 2014-12-04 10:47:02 -0800 | [diff] [blame] | 788 | log->bench("memory_usage", 0,0); |
| 789 | log->config("meta"); |
| 790 | log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB()); |
| 791 | |
mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame] | 792 | return 0; |
| 793 | } |
| 794 | |
jcgregorio | 3b27ade | 2014-11-13 08:06:40 -0800 | [diff] [blame] | 795 | #if !defined SK_BUILD_FOR_IOS |
| 796 | int main(int argc, char** argv) { |
| 797 | SkCommandLineFlags::Parse(argc, argv); |
| 798 | return nanobench_main(); |
| 799 | } |
| 800 | #endif |