csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 8 | #include <stdlib.h> |
| 9 | #include <algorithm> |
| 10 | #include <array> |
| 11 | #include <chrono> |
| 12 | #include <cmath> |
| 13 | #include <vector> |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 14 | #include "DDLPromiseImageHelper.h" |
| 15 | #include "DDLTileHelper.h" |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 16 | #include "GpuTimer.h" |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 17 | #include "GrCaps.h" |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 18 | #include "GrContextFactory.h" |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 19 | #include "GrContextPriv.h" |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 20 | #include "SkCanvas.h" |
Chris Dalton | 7a0ebfc | 2017-10-13 12:35:50 -0600 | [diff] [blame] | 21 | #include "SkCommonFlags.h" |
Chris Dalton | 040238b | 2017-12-18 14:22:34 -0700 | [diff] [blame] | 22 | #include "SkCommonFlagsGpu.h" |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 23 | #include "SkDeferredDisplayList.h" |
| 24 | #include "SkGraphics.h" |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 25 | #include "SkGr.h" |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 26 | #include "SkOSFile.h" |
Ben Wagner | bf111d7 | 2016-11-07 18:05:29 -0500 | [diff] [blame] | 27 | #include "SkOSPath.h" |
csmartdalton | 5772eaa | 2016-10-11 18:28:54 -0700 | [diff] [blame] | 28 | #include "SkPerlinNoiseShader.h" |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 29 | #include "SkPicture.h" |
csmartdalton | 5772eaa | 2016-10-11 18:28:54 -0700 | [diff] [blame] | 30 | #include "SkPictureRecorder.h" |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 31 | #include "SkStream.h" |
| 32 | #include "SkSurface.h" |
| 33 | #include "SkSurfaceProps.h" |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 34 | #include "SkTaskGroup.h" |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 35 | #include "flags/SkCommandLineFlags.h" |
| 36 | #include "flags/SkCommonFlagsConfig.h" |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 37 | #include "picture_utils.h" |
| 38 | #include "sk_tool_utils.h" |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * This is a minimalist program whose sole purpose is to open an skp file, benchmark it on a single |
| 42 | * config, and exit. It is intended to be used through skpbench.py rather than invoked directly. |
| 43 | * Limiting the entire process to a single config/skp pair helps to keep the results repeatable. |
| 44 | * |
| 45 | * No tiling, looping, or other fanciness is used; it just draws the skp whole into a size-matched |
| 46 | * render target and syncs the GPU after each draw. |
| 47 | * |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 48 | * Currently, only GPU configs are supported. |
| 49 | */ |
| 50 | |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 51 | DEFINE_bool(ddl, false, "record the skp into DDLs before rendering"); |
| 52 | DEFINE_int32(ddlNumAdditionalThreads, 0, "number of DDL recording threads in addition to main one"); |
| 53 | DEFINE_int32(ddlTilingWidthHeight, 0, "number of tiles along one edge when in DDL mode"); |
Robert Phillips | 65eb4fb | 2018-05-31 13:27:52 -0400 | [diff] [blame] | 54 | DEFINE_bool(ddlRecordTime, false, "report just the cpu time spent recording DDLs"); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 55 | |
csmartdalton | 037adf3 | 2016-09-28 13:56:01 -0700 | [diff] [blame] | 56 | DEFINE_int32(duration, 5000, "number of milliseconds to run the benchmark"); |
| 57 | DEFINE_int32(sampleMs, 50, "minimum duration of a sample"); |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 58 | DEFINE_bool(gpuClock, false, "time on the gpu clock (gpu work only)"); |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 59 | DEFINE_bool(fps, false, "use fps instead of ms"); |
csmartdalton | 5772eaa | 2016-10-11 18:28:54 -0700 | [diff] [blame] | 60 | DEFINE_string(skp, "", "path to a single .skp file, or 'warmup' for a builtin warmup run"); |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 61 | DEFINE_string(png, "", "if set, save a .png proof to disk at this file location"); |
| 62 | DEFINE_int32(verbosity, 4, "level of verbosity (0=none to 5=debug)"); |
| 63 | DEFINE_bool(suppressHeader, false, "don't print a header row before the results"); |
| 64 | |
| 65 | static const char* header = |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 66 | " accum median max min stddev samples sample_ms clock metric config bench"; |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 67 | |
| 68 | static const char* resultFormat = |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 69 | "%8.4g %8.4g %8.4g %8.4g %6.3g%% %7li %9i %-5s %-6s %-9s %s"; |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 70 | |
Chris Dalton | a2b5b64 | 2018-06-24 13:08:57 -0600 | [diff] [blame^] | 71 | static constexpr int kNumFlushesToPrimeCache = 3; |
| 72 | |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 73 | struct Sample { |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 74 | using duration = std::chrono::nanoseconds; |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 75 | |
| 76 | Sample() : fFrames(0), fDuration(0) {} |
| 77 | double seconds() const { return std::chrono::duration<double>(fDuration).count(); } |
| 78 | double ms() const { return std::chrono::duration<double, std::milli>(fDuration).count(); } |
| 79 | double value() const { return FLAGS_fps ? fFrames / this->seconds() : this->ms() / fFrames; } |
| 80 | static const char* metric() { return FLAGS_fps ? "fps" : "ms"; } |
| 81 | |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 82 | int fFrames; |
| 83 | duration fDuration; |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
csmartdalton | e038489 | 2016-09-28 14:53:07 -0700 | [diff] [blame] | 86 | class GpuSync { |
| 87 | public: |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 88 | GpuSync(const sk_gpu_test::FenceSync* fenceSync); |
csmartdalton | e038489 | 2016-09-28 14:53:07 -0700 | [diff] [blame] | 89 | ~GpuSync(); |
| 90 | |
| 91 | void syncToPreviousFrame(); |
| 92 | |
| 93 | private: |
| 94 | void updateFence(); |
| 95 | |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 96 | const sk_gpu_test::FenceSync* const fFenceSync; |
| 97 | sk_gpu_test::PlatformFence fFence; |
csmartdalton | e038489 | 2016-09-28 14:53:07 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 100 | enum class ExitErr { |
| 101 | kOk = 0, |
| 102 | kUsage = 64, |
| 103 | kData = 65, |
| 104 | kUnavailable = 69, |
| 105 | kIO = 74, |
| 106 | kSoftware = 70 |
| 107 | }; |
| 108 | |
| 109 | static void draw_skp_and_flush(SkCanvas*, const SkPicture*); |
csmartdalton | 5772eaa | 2016-10-11 18:28:54 -0700 | [diff] [blame] | 110 | static sk_sp<SkPicture> create_warmup_skp(); |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 111 | static bool mkdir_p(const SkString& name); |
| 112 | static SkString join(const SkCommandLineFlags::StringArray&); |
| 113 | static void exitf(ExitErr, const char* format, ...); |
| 114 | |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 115 | static void ddl_sample(GrContext* context, DDLTileHelper* tiles, GpuSync* gpuSync, Sample* sample, |
| 116 | std::chrono::high_resolution_clock::time_point* startStopTime) { |
| 117 | using clock = std::chrono::high_resolution_clock; |
| 118 | |
| 119 | clock::time_point start = *startStopTime; |
| 120 | |
| 121 | tiles->createDDLsInParallel(); |
| 122 | |
Robert Phillips | 65eb4fb | 2018-05-31 13:27:52 -0400 | [diff] [blame] | 123 | if (!FLAGS_ddlRecordTime) { |
| 124 | tiles->drawAllTilesAndFlush(context, true); |
| 125 | if (gpuSync) { |
| 126 | gpuSync->syncToPreviousFrame(); |
| 127 | } |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | *startStopTime = clock::now(); |
| 131 | |
| 132 | tiles->resetAllTiles(); |
| 133 | |
| 134 | if (sample) { |
| 135 | SkASSERT(gpuSync); |
| 136 | sample->fDuration += *startStopTime - start; |
| 137 | sample->fFrames++; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | static void run_ddl_benchmark(const sk_gpu_test::FenceSync* fenceSync, |
| 142 | GrContext* context, SkCanvas* finalCanvas, |
| 143 | SkPicture* inputPicture, std::vector<Sample>* samples) { |
| 144 | using clock = std::chrono::high_resolution_clock; |
| 145 | const Sample::duration sampleDuration = std::chrono::milliseconds(FLAGS_sampleMs); |
| 146 | const clock::duration benchDuration = std::chrono::milliseconds(FLAGS_duration); |
| 147 | |
| 148 | SkIRect viewport = finalCanvas->imageInfo().bounds(); |
| 149 | |
| 150 | DDLPromiseImageHelper promiseImageHelper; |
| 151 | sk_sp<SkData> compressedPictureData = promiseImageHelper.deflateSKP(inputPicture); |
| 152 | if (!compressedPictureData) { |
| 153 | exitf(ExitErr::kUnavailable, "DDL: conversion of skp failed"); |
| 154 | } |
| 155 | |
| 156 | promiseImageHelper.uploadAllToGPU(context); |
| 157 | |
| 158 | DDLTileHelper tiles(finalCanvas, viewport, FLAGS_ddlTilingWidthHeight); |
| 159 | |
| 160 | tiles.createSKPPerTile(compressedPictureData.get(), promiseImageHelper); |
| 161 | |
Robert Phillips | f7dcdb0 | 2018-06-21 11:18:25 -0400 | [diff] [blame] | 162 | SkTaskGroup::Enabler enabled(FLAGS_ddlNumAdditionalThreads); |
| 163 | |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 164 | clock::time_point startStopTime = clock::now(); |
| 165 | |
| 166 | ddl_sample(context, &tiles, nullptr, nullptr, &startStopTime); |
| 167 | GpuSync gpuSync(fenceSync); |
| 168 | ddl_sample(context, &tiles, &gpuSync, nullptr, &startStopTime); |
| 169 | |
| 170 | clock::duration cumulativeDuration = std::chrono::milliseconds(0); |
| 171 | |
| 172 | do { |
| 173 | samples->emplace_back(); |
| 174 | Sample& sample = samples->back(); |
| 175 | |
| 176 | do { |
| 177 | ddl_sample(context, &tiles, &gpuSync, &sample, &startStopTime); |
| 178 | } while (sample.fDuration < sampleDuration); |
| 179 | |
| 180 | cumulativeDuration += sample.fDuration; |
| 181 | } while (cumulativeDuration < benchDuration || 0 == samples->size() % 2); |
| 182 | |
| 183 | if (!FLAGS_png.isEmpty()) { |
| 184 | // The user wants to see the final result |
| 185 | tiles.composeAllTiles(finalCanvas); |
| 186 | } |
| 187 | } |
| 188 | |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 189 | static void run_benchmark(const sk_gpu_test::FenceSync* fenceSync, SkCanvas* canvas, |
| 190 | const SkPicture* skp, std::vector<Sample>* samples) { |
| 191 | using clock = std::chrono::high_resolution_clock; |
| 192 | const Sample::duration sampleDuration = std::chrono::milliseconds(FLAGS_sampleMs); |
csmartdalton | 037adf3 | 2016-09-28 13:56:01 -0700 | [diff] [blame] | 193 | const clock::duration benchDuration = std::chrono::milliseconds(FLAGS_duration); |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 194 | |
Chris Dalton | a2b5b64 | 2018-06-24 13:08:57 -0600 | [diff] [blame^] | 195 | draw_skp_and_flush(canvas, skp); // draw 1 |
csmartdalton | e038489 | 2016-09-28 14:53:07 -0700 | [diff] [blame] | 196 | GpuSync gpuSync(fenceSync); |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 197 | |
Chris Dalton | a2b5b64 | 2018-06-24 13:08:57 -0600 | [diff] [blame^] | 198 | for (int i = 1; i < kNumFlushesToPrimeCache; ++i) { |
| 199 | draw_skp_and_flush(canvas, skp); // draw N |
| 200 | // Waits for draw N-1 to finish (after draw N's cpu work is done). |
| 201 | gpuSync.syncToPreviousFrame(); |
| 202 | } |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 203 | |
csmartdalton | 037adf3 | 2016-09-28 13:56:01 -0700 | [diff] [blame] | 204 | clock::time_point now = clock::now(); |
| 205 | const clock::time_point endTime = now + benchDuration; |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 206 | |
csmartdalton | 037adf3 | 2016-09-28 13:56:01 -0700 | [diff] [blame] | 207 | do { |
| 208 | clock::time_point sampleStart = now; |
| 209 | samples->emplace_back(); |
| 210 | Sample& sample = samples->back(); |
| 211 | |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 212 | do { |
| 213 | draw_skp_and_flush(canvas, skp); |
csmartdalton | e038489 | 2016-09-28 14:53:07 -0700 | [diff] [blame] | 214 | gpuSync.syncToPreviousFrame(); |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 215 | |
csmartdalton | 037adf3 | 2016-09-28 13:56:01 -0700 | [diff] [blame] | 216 | now = clock::now(); |
| 217 | sample.fDuration = now - sampleStart; |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 218 | ++sample.fFrames; |
csmartdalton | 037adf3 | 2016-09-28 13:56:01 -0700 | [diff] [blame] | 219 | } while (sample.fDuration < sampleDuration); |
| 220 | } while (now < endTime || 0 == samples->size() % 2); |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 221 | } |
| 222 | |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 223 | static void run_gpu_time_benchmark(sk_gpu_test::GpuTimer* gpuTimer, |
| 224 | const sk_gpu_test::FenceSync* fenceSync, SkCanvas* canvas, |
| 225 | const SkPicture* skp, std::vector<Sample>* samples) { |
| 226 | using sk_gpu_test::PlatformTimerQuery; |
| 227 | using clock = std::chrono::steady_clock; |
| 228 | const clock::duration sampleDuration = std::chrono::milliseconds(FLAGS_sampleMs); |
| 229 | const clock::duration benchDuration = std::chrono::milliseconds(FLAGS_duration); |
| 230 | |
| 231 | if (!gpuTimer->disjointSupport()) { |
| 232 | fprintf(stderr, "WARNING: GPU timer cannot detect disjoint operations; " |
| 233 | "results may be unreliable\n"); |
| 234 | } |
| 235 | |
| 236 | draw_skp_and_flush(canvas, skp); |
| 237 | GpuSync gpuSync(fenceSync); |
| 238 | |
Chris Dalton | a2b5b64 | 2018-06-24 13:08:57 -0600 | [diff] [blame^] | 239 | PlatformTimerQuery previousTime = 0; |
| 240 | for (int i = 1; i < kNumFlushesToPrimeCache; ++i) { |
| 241 | gpuTimer->queueStart(); |
| 242 | draw_skp_and_flush(canvas, skp); |
| 243 | previousTime = gpuTimer->queueStop(); |
| 244 | gpuSync.syncToPreviousFrame(); |
| 245 | } |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 246 | |
| 247 | clock::time_point now = clock::now(); |
| 248 | const clock::time_point endTime = now + benchDuration; |
| 249 | |
| 250 | do { |
| 251 | const clock::time_point sampleEndTime = now + sampleDuration; |
| 252 | samples->emplace_back(); |
| 253 | Sample& sample = samples->back(); |
| 254 | |
| 255 | do { |
| 256 | gpuTimer->queueStart(); |
| 257 | draw_skp_and_flush(canvas, skp); |
| 258 | PlatformTimerQuery time = gpuTimer->queueStop(); |
| 259 | gpuSync.syncToPreviousFrame(); |
| 260 | |
| 261 | switch (gpuTimer->checkQueryStatus(previousTime)) { |
| 262 | using QueryStatus = sk_gpu_test::GpuTimer::QueryStatus; |
| 263 | case QueryStatus::kInvalid: |
| 264 | exitf(ExitErr::kUnavailable, "GPU timer failed"); |
| 265 | case QueryStatus::kPending: |
| 266 | exitf(ExitErr::kUnavailable, "timer query still not ready after fence sync"); |
| 267 | case QueryStatus::kDisjoint: |
| 268 | if (FLAGS_verbosity >= 4) { |
| 269 | fprintf(stderr, "discarding timer query due to disjoint operations.\n"); |
| 270 | } |
| 271 | break; |
| 272 | case QueryStatus::kAccurate: |
| 273 | sample.fDuration += gpuTimer->getTimeElapsed(previousTime); |
| 274 | ++sample.fFrames; |
| 275 | break; |
| 276 | } |
| 277 | gpuTimer->deleteQuery(previousTime); |
| 278 | previousTime = time; |
| 279 | now = clock::now(); |
| 280 | } while (now < sampleEndTime || 0 == sample.fFrames); |
| 281 | } while (now < endTime || 0 == samples->size() % 2); |
| 282 | |
| 283 | gpuTimer->deleteQuery(previousTime); |
| 284 | } |
| 285 | |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 286 | void print_result(const std::vector<Sample>& samples, const char* config, const char* bench) { |
| 287 | if (0 == (samples.size() % 2)) { |
| 288 | exitf(ExitErr::kSoftware, "attempted to gather stats on even number of samples"); |
| 289 | } |
| 290 | |
| 291 | Sample accum = Sample(); |
| 292 | std::vector<double> values; |
| 293 | values.reserve(samples.size()); |
| 294 | for (const Sample& sample : samples) { |
| 295 | accum.fFrames += sample.fFrames; |
| 296 | accum.fDuration += sample.fDuration; |
| 297 | values.push_back(sample.value()); |
| 298 | } |
| 299 | std::sort(values.begin(), values.end()); |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 300 | |
csmartdalton | 6904b19 | 2016-09-29 06:23:23 -0700 | [diff] [blame] | 301 | const double accumValue = accum.value(); |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 302 | double variance = 0; |
csmartdalton | 037adf3 | 2016-09-28 13:56:01 -0700 | [diff] [blame] | 303 | for (double value : values) { |
| 304 | const double delta = value - accumValue; |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 305 | variance += delta * delta; |
| 306 | } |
csmartdalton | 037adf3 | 2016-09-28 13:56:01 -0700 | [diff] [blame] | 307 | variance /= values.size(); |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 308 | // Technically, this is the relative standard deviation. |
csmartdalton | 037adf3 | 2016-09-28 13:56:01 -0700 | [diff] [blame] | 309 | const double stddev = 100/*%*/ * sqrt(variance) / accumValue; |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 310 | |
csmartdalton | 6904b19 | 2016-09-29 06:23:23 -0700 | [diff] [blame] | 311 | printf(resultFormat, accumValue, values[values.size() / 2], values.back(), values.front(), |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 312 | stddev, values.size(), FLAGS_sampleMs, FLAGS_gpuClock ? "gpu" : "cpu", Sample::metric(), |
| 313 | config, bench); |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 314 | printf("\n"); |
| 315 | fflush(stdout); |
| 316 | } |
| 317 | |
| 318 | int main(int argc, char** argv) { |
| 319 | SkCommandLineFlags::SetUsage("Use skpbench.py instead. " |
| 320 | "You usually don't want to use this program directly."); |
| 321 | SkCommandLineFlags::Parse(argc, argv); |
| 322 | |
| 323 | if (!FLAGS_suppressHeader) { |
| 324 | printf("%s\n", header); |
| 325 | } |
csmartdalton | 037adf3 | 2016-09-28 13:56:01 -0700 | [diff] [blame] | 326 | if (FLAGS_duration <= 0) { |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 327 | exit(0); // This can be used to print the header and quit. |
| 328 | } |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 329 | |
| 330 | // Parse the config. |
| 331 | const SkCommandLineConfigGpu* config = nullptr; // Initialize for spurious warning. |
| 332 | SkCommandLineConfigArray configs; |
| 333 | ParseConfigs(FLAGS_config, &configs); |
| 334 | if (configs.count() != 1 || !(config = configs[0]->asConfigGpu())) { |
csmartdalton | 5772eaa | 2016-10-11 18:28:54 -0700 | [diff] [blame] | 335 | exitf(ExitErr::kUsage, "invalid config '%s': must specify one (and only one) GPU config", |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 336 | join(FLAGS_config).c_str()); |
| 337 | } |
| 338 | |
| 339 | // Parse the skp. |
| 340 | if (FLAGS_skp.count() != 1) { |
csmartdalton | 5772eaa | 2016-10-11 18:28:54 -0700 | [diff] [blame] | 341 | exitf(ExitErr::kUsage, "invalid skp '%s': must specify a single skp file, or 'warmup'", |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 342 | join(FLAGS_skp).c_str()); |
| 343 | } |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 344 | |
| 345 | SkGraphics::Init(); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 346 | |
csmartdalton | 5772eaa | 2016-10-11 18:28:54 -0700 | [diff] [blame] | 347 | sk_sp<SkPicture> skp; |
| 348 | SkString skpname; |
| 349 | if (0 == strcmp(FLAGS_skp[0], "warmup")) { |
| 350 | skp = create_warmup_skp(); |
| 351 | skpname = "warmup"; |
| 352 | } else { |
| 353 | const char* skpfile = FLAGS_skp[0]; |
| 354 | std::unique_ptr<SkStream> skpstream(SkStream::MakeFromFile(skpfile)); |
| 355 | if (!skpstream) { |
| 356 | exitf(ExitErr::kIO, "failed to open skp file %s", skpfile); |
| 357 | } |
| 358 | skp = SkPicture::MakeFromStream(skpstream.get()); |
| 359 | if (!skp) { |
| 360 | exitf(ExitErr::kData, "failed to parse skp file %s", skpfile); |
| 361 | } |
| 362 | skpname = SkOSPath::Basename(skpfile); |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 363 | } |
| 364 | int width = SkTMin(SkScalarCeilToInt(skp->cullRect().width()), 2048), |
| 365 | height = SkTMin(SkScalarCeilToInt(skp->cullRect().height()), 2048); |
csmartdalton | d7a9db6 | 2016-09-22 05:10:02 -0700 | [diff] [blame] | 366 | if (FLAGS_verbosity >= 3 && |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 367 | (width != skp->cullRect().width() || height != skp->cullRect().height())) { |
csmartdalton | d7a9db6 | 2016-09-22 05:10:02 -0700 | [diff] [blame] | 368 | fprintf(stderr, "%s is too large (%ix%i), cropping to %ix%i.\n", |
csmartdalton | 5772eaa | 2016-10-11 18:28:54 -0700 | [diff] [blame] | 369 | skpname.c_str(), SkScalarCeilToInt(skp->cullRect().width()), |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 370 | SkScalarCeilToInt(skp->cullRect().height()), width, height); |
| 371 | } |
| 372 | |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 373 | if (config->getSurfType() != SkCommandLineConfigGpu::SurfType::kDefault) { |
| 374 | exitf(ExitErr::kUnavailable, "This tool only supports the default surface type. (%s)", |
| 375 | config->getTag().c_str()); |
| 376 | } |
| 377 | |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 378 | // Create a context. |
csmartdalton | 008b9d8 | 2017-02-22 12:00:42 -0700 | [diff] [blame] | 379 | GrContextOptions ctxOptions; |
Chris Dalton | 040238b | 2017-12-18 14:22:34 -0700 | [diff] [blame] | 380 | SetCtxOptionsFromCommonFlags(&ctxOptions); |
csmartdalton | 008b9d8 | 2017-02-22 12:00:42 -0700 | [diff] [blame] | 381 | sk_gpu_test::GrContextFactory factory(ctxOptions); |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 382 | sk_gpu_test::ContextInfo ctxInfo = |
csmartdalton | e812d49 | 2017-02-21 12:36:05 -0700 | [diff] [blame] | 383 | factory.getContextInfo(config->getContextType(), config->getContextOverrides()); |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 384 | GrContext* ctx = ctxInfo.grContext(); |
| 385 | if (!ctx) { |
| 386 | exitf(ExitErr::kUnavailable, "failed to create context for config %s", |
| 387 | config->getTag().c_str()); |
| 388 | } |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 389 | if (ctx->maxRenderTargetSize() < SkTMax(width, height)) { |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 390 | exitf(ExitErr::kUnavailable, "render target size %ix%i not supported by platform (max: %i)", |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 391 | width, height, ctx->maxRenderTargetSize()); |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 392 | } |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 393 | GrPixelConfig grPixConfig = SkColorType2GrPixelConfig(config->getColorType()); |
Greg Daniel | 0a7aa14 | 2018-02-21 13:02:32 -0500 | [diff] [blame] | 394 | if (kUnknown_GrPixelConfig == grPixConfig) { |
| 395 | exitf(ExitErr::kUnavailable, "failed to get GrPixelConfig from SkColorType: %d", |
| 396 | config->getColorType()); |
| 397 | } |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 398 | int supportedSampleCount = ctx->contextPriv().caps()->getRenderTargetSampleCount( |
| 399 | config->getSamples(), grPixConfig); |
Greg Daniel | 81e7bf8 | 2017-07-19 14:47:42 -0400 | [diff] [blame] | 400 | if (supportedSampleCount != config->getSamples()) { |
| 401 | exitf(ExitErr::kUnavailable, "sample count %i not supported by platform", |
| 402 | config->getSamples()); |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 403 | } |
| 404 | sk_gpu_test::TestContext* testCtx = ctxInfo.testContext(); |
| 405 | if (!testCtx) { |
| 406 | exitf(ExitErr::kSoftware, "testContext is null"); |
| 407 | } |
| 408 | if (!testCtx->fenceSyncSupport()) { |
| 409 | exitf(ExitErr::kUnavailable, "GPU does not support fence sync"); |
| 410 | } |
| 411 | |
| 412 | // Create a render target. |
Brian Salomon | ce5ee60 | 2017-07-17 11:31:31 -0400 | [diff] [blame] | 413 | SkImageInfo info = |
| 414 | SkImageInfo::Make(width, height, config->getColorType(), config->getAlphaType(), |
| 415 | sk_ref_sp(config->getColorSpace())); |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 416 | uint32_t flags = config->getUseDIText() ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag : 0; |
| 417 | SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); |
| 418 | sk_sp<SkSurface> surface = |
| 419 | SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, config->getSamples(), &props); |
| 420 | if (!surface) { |
| 421 | exitf(ExitErr::kUnavailable, "failed to create %ix%i render target for config %s", |
| 422 | width, height, config->getTag().c_str()); |
| 423 | } |
| 424 | |
csmartdalton | 5772eaa | 2016-10-11 18:28:54 -0700 | [diff] [blame] | 425 | // Run the benchmark. |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 426 | std::vector<Sample> samples; |
csmartdalton | 037adf3 | 2016-09-28 13:56:01 -0700 | [diff] [blame] | 427 | if (FLAGS_sampleMs > 0) { |
| 428 | // +1 because we might take one more sample in order to have an odd number. |
| 429 | samples.reserve(1 + (FLAGS_duration + FLAGS_sampleMs - 1) / FLAGS_sampleMs); |
| 430 | } else { |
| 431 | samples.reserve(2 * FLAGS_duration); |
| 432 | } |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 433 | SkCanvas* canvas = surface->getCanvas(); |
| 434 | canvas->translate(-skp->cullRect().x(), -skp->cullRect().y()); |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 435 | if (!FLAGS_gpuClock) { |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 436 | if (FLAGS_ddl) { |
| 437 | run_ddl_benchmark(testCtx->fenceSync(), ctx, canvas, skp.get(), &samples); |
| 438 | } else { |
| 439 | run_benchmark(testCtx->fenceSync(), canvas, skp.get(), &samples); |
| 440 | } |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 441 | } else { |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 442 | if (FLAGS_ddl) { |
| 443 | exitf(ExitErr::kUnavailable, "DDL: GPU-only timing not supported"); |
| 444 | } |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 445 | if (!testCtx->gpuTimingSupport()) { |
| 446 | exitf(ExitErr::kUnavailable, "GPU does not support timing"); |
| 447 | } |
| 448 | run_gpu_time_benchmark(testCtx->gpuTimer(), testCtx->fenceSync(), canvas, skp.get(), |
| 449 | &samples); |
| 450 | } |
csmartdalton | 5772eaa | 2016-10-11 18:28:54 -0700 | [diff] [blame] | 451 | print_result(samples, config->getTag().c_str(), skpname.c_str()); |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 452 | |
| 453 | // Save a proof (if one was requested). |
| 454 | if (!FLAGS_png.isEmpty()) { |
| 455 | SkBitmap bmp; |
Mike Reed | 12e946b | 2017-04-17 10:53:29 -0400 | [diff] [blame] | 456 | bmp.allocPixels(info); |
| 457 | if (!surface->getCanvas()->readPixels(bmp, 0, 0)) { |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 458 | exitf(ExitErr::kUnavailable, "failed to read canvas pixels for png"); |
| 459 | } |
| 460 | const SkString &dirname = SkOSPath::Dirname(FLAGS_png[0]), |
| 461 | &basename = SkOSPath::Basename(FLAGS_png[0]); |
| 462 | if (!mkdir_p(dirname)) { |
| 463 | exitf(ExitErr::kIO, "failed to create directory \"%s\" for png", dirname.c_str()); |
| 464 | } |
| 465 | if (!sk_tools::write_bitmap_to_disk(bmp, dirname, nullptr, basename)) { |
| 466 | exitf(ExitErr::kIO, "failed to save png to \"%s\"", FLAGS_png[0]); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | exit(0); |
| 471 | } |
| 472 | |
| 473 | static void draw_skp_and_flush(SkCanvas* canvas, const SkPicture* skp) { |
| 474 | canvas->drawPicture(skp); |
| 475 | canvas->flush(); |
| 476 | } |
| 477 | |
csmartdalton | 5772eaa | 2016-10-11 18:28:54 -0700 | [diff] [blame] | 478 | static sk_sp<SkPicture> create_warmup_skp() { |
| 479 | static constexpr SkRect bounds{0, 0, 500, 500}; |
| 480 | SkPictureRecorder recorder; |
| 481 | SkCanvas* recording = recorder.beginRecording(bounds); |
| 482 | |
| 483 | recording->clear(SK_ColorWHITE); |
| 484 | |
| 485 | SkPaint stroke; |
| 486 | stroke.setStyle(SkPaint::kStroke_Style); |
| 487 | stroke.setStrokeWidth(2); |
| 488 | |
| 489 | // Use a big path to (theoretically) warmup the CPU. |
| 490 | SkPath bigPath; |
| 491 | sk_tool_utils::make_big_path(bigPath); |
| 492 | recording->drawPath(bigPath, stroke); |
| 493 | |
| 494 | // Use a perlin shader to warmup the GPU. |
| 495 | SkPaint perlin; |
| 496 | perlin.setShader(SkPerlinNoiseShader::MakeTurbulence(0.1f, 0.1f, 1, 0, nullptr)); |
| 497 | recording->drawRect(bounds, perlin); |
| 498 | |
| 499 | return recorder.finishRecordingAsPicture(); |
| 500 | } |
| 501 | |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 502 | bool mkdir_p(const SkString& dirname) { |
| 503 | if (dirname.isEmpty()) { |
| 504 | return true; |
| 505 | } |
| 506 | return mkdir_p(SkOSPath::Dirname(dirname.c_str())) && sk_mkdir(dirname.c_str()); |
| 507 | } |
| 508 | |
| 509 | static SkString join(const SkCommandLineFlags::StringArray& stringArray) { |
| 510 | SkString joined; |
csmartdalton | 5772eaa | 2016-10-11 18:28:54 -0700 | [diff] [blame] | 511 | for (int i = 0; i < stringArray.count(); ++i) { |
| 512 | joined.appendf(i ? " %s" : "%s", stringArray[i]); |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 513 | } |
| 514 | return joined; |
| 515 | } |
| 516 | |
| 517 | static void exitf(ExitErr err, const char* format, ...) { |
| 518 | fprintf(stderr, ExitErr::kSoftware == err ? "INTERNAL ERROR: " : "ERROR: "); |
| 519 | va_list args; |
| 520 | va_start(args, format); |
| 521 | vfprintf(stderr, format, args); |
| 522 | va_end(args); |
| 523 | fprintf(stderr, ExitErr::kSoftware == err ? "; this should never happen.\n": ".\n"); |
| 524 | exit((int)err); |
| 525 | } |
csmartdalton | e038489 | 2016-09-28 14:53:07 -0700 | [diff] [blame] | 526 | |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 527 | GpuSync::GpuSync(const sk_gpu_test::FenceSync* fenceSync) |
csmartdalton | e038489 | 2016-09-28 14:53:07 -0700 | [diff] [blame] | 528 | : fFenceSync(fenceSync) { |
| 529 | this->updateFence(); |
| 530 | } |
| 531 | |
| 532 | GpuSync::~GpuSync() { |
| 533 | fFenceSync->deleteFence(fFence); |
| 534 | } |
| 535 | |
| 536 | void GpuSync::syncToPreviousFrame() { |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 537 | if (sk_gpu_test::kInvalidFence == fFence) { |
csmartdalton | e038489 | 2016-09-28 14:53:07 -0700 | [diff] [blame] | 538 | exitf(ExitErr::kSoftware, "attempted to sync with invalid fence"); |
| 539 | } |
| 540 | if (!fFenceSync->waitFence(fFence)) { |
| 541 | exitf(ExitErr::kUnavailable, "failed to wait for fence"); |
| 542 | } |
| 543 | fFenceSync->deleteFence(fFence); |
| 544 | this->updateFence(); |
| 545 | } |
| 546 | |
| 547 | void GpuSync::updateFence() { |
| 548 | fFence = fFenceSync->insertFence(); |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 549 | if (sk_gpu_test::kInvalidFence == fFence) { |
csmartdalton | e038489 | 2016-09-28 14:53:07 -0700 | [diff] [blame] | 550 | exitf(ExitErr::kUnavailable, "failed to insert fence"); |
| 551 | } |
| 552 | } |