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