blob: 3817b8b0eca3a08f011fe188ea652074e5fc28f2 [file] [log] [blame]
csmartdalton4b5179b2016-09-19 11:03:58 -07001/*
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
Mike Klein88544fb2019-03-20 10:50:33 -05008#include "CommonFlags.h"
9#include "CommonFlagsGpu.h"
Robert Phillips96601082018-05-29 16:13:26 -040010#include "DDLPromiseImageHelper.h"
11#include "DDLTileHelper.h"
csmartdaltonc6618dd2016-10-05 08:42:03 -070012#include "GpuTimer.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040013#include "GrCaps.h"
csmartdalton4b5179b2016-09-19 11:03:58 -070014#include "GrContextFactory.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040015#include "GrContextPriv.h"
csmartdalton4b5179b2016-09-19 11:03:58 -070016#include "SkCanvas.h"
Robert Phillips96601082018-05-29 16:13:26 -040017#include "SkDeferredDisplayList.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040018#include "SkGr.h"
Mike Klein88544fb2019-03-20 10:50:33 -050019#include "SkGraphics.h"
csmartdalton4b5179b2016-09-19 11:03:58 -070020#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050021#include "SkOSPath.h"
csmartdalton5772eaa2016-10-11 18:28:54 -070022#include "SkPerlinNoiseShader.h"
csmartdalton4b5179b2016-09-19 11:03:58 -070023#include "SkPicture.h"
csmartdalton5772eaa2016-10-11 18:28:54 -070024#include "SkPictureRecorder.h"
csmartdalton4b5179b2016-09-19 11:03:58 -070025#include "SkStream.h"
26#include "SkSurface.h"
27#include "SkSurfaceProps.h"
Robert Phillips96601082018-05-29 16:13:26 -040028#include "SkTaskGroup.h"
Mike Kleinea3f0142019-03-20 11:12:10 -050029#include "ToolUtils.h"
Mike Klein88544fb2019-03-20 10:50:33 -050030#include "flags/CommandLineFlags.h"
31#include "flags/CommonFlagsConfig.h"
csmartdalton4b5179b2016-09-19 11:03:58 -070032
Chris Daltona4f5ce02018-06-26 10:13:06 -060033#ifdef SK_XML
34#include "SkDOM.h"
35#include "../experimental/svg/model/SkSVGDOM.h"
36#endif
37
Hal Canary8a001442018-09-19 11:31:27 -040038#include <stdlib.h>
39#include <algorithm>
40#include <array>
41#include <chrono>
42#include <cmath>
43#include <vector>
Chris Daltona4f5ce02018-06-26 10:13:06 -060044
csmartdalton4b5179b2016-09-19 11:03:58 -070045/**
Chris Daltona4f5ce02018-06-26 10:13:06 -060046 * This is a minimalist program whose sole purpose is to open a .skp or .svg file, benchmark it on a
47 * single config, and exit. It is intended to be used through skpbench.py rather than invoked
48 * directly. Limiting the entire process to a single config/skp pair helps to keep the results
49 * repeatable.
csmartdalton4b5179b2016-09-19 11:03:58 -070050 *
51 * No tiling, looping, or other fanciness is used; it just draws the skp whole into a size-matched
52 * render target and syncs the GPU after each draw.
53 *
csmartdalton4b5179b2016-09-19 11:03:58 -070054 * Currently, only GPU configs are supported.
55 */
56
Mike Klein84836b72019-03-21 11:31:36 -050057static DEFINE_bool(ddl, false, "record the skp into DDLs before rendering");
Mike Klein5b3f3432019-03-21 11:42:21 -050058static DEFINE_int(ddlNumAdditionalThreads, 0,
Mike Klein84836b72019-03-21 11:31:36 -050059 "number of DDL recording threads in addition to main one");
Mike Klein5b3f3432019-03-21 11:42:21 -050060static DEFINE_int(ddlTilingWidthHeight, 0, "number of tiles along one edge when in DDL mode");
Mike Klein84836b72019-03-21 11:31:36 -050061static DEFINE_bool(ddlRecordTime, false, "report just the cpu time spent recording DDLs");
Robert Phillips96601082018-05-29 16:13:26 -040062
Mike Klein5b3f3432019-03-21 11:42:21 -050063static DEFINE_int(duration, 5000, "number of milliseconds to run the benchmark");
64static DEFINE_int(sampleMs, 50, "minimum duration of a sample");
Mike Klein84836b72019-03-21 11:31:36 -050065static DEFINE_bool(gpuClock, false, "time on the gpu clock (gpu work only)");
66static DEFINE_bool(fps, false, "use fps instead of ms");
67static DEFINE_string(src, "",
68 "path to a single .skp or .svg file, or 'warmup' for a builtin warmup run");
69static DEFINE_string(png, "", "if set, save a .png proof to disk at this file location");
Mike Klein5b3f3432019-03-21 11:42:21 -050070static DEFINE_int(verbosity, 4, "level of verbosity (0=none to 5=debug)");
Mike Klein84836b72019-03-21 11:31:36 -050071static DEFINE_bool(suppressHeader, false, "don't print a header row before the results");
csmartdalton4b5179b2016-09-19 11:03:58 -070072
73static const char* header =
csmartdaltonc6618dd2016-10-05 08:42:03 -070074" accum median max min stddev samples sample_ms clock metric config bench";
csmartdalton4b5179b2016-09-19 11:03:58 -070075
76static const char* resultFormat =
csmartdaltonc6618dd2016-10-05 08:42:03 -070077"%8.4g %8.4g %8.4g %8.4g %6.3g%% %7li %9i %-5s %-6s %-9s %s";
csmartdalton4b5179b2016-09-19 11:03:58 -070078
Chris Daltona2b5b642018-06-24 13:08:57 -060079static constexpr int kNumFlushesToPrimeCache = 3;
80
csmartdalton4b5179b2016-09-19 11:03:58 -070081struct Sample {
csmartdaltonc6618dd2016-10-05 08:42:03 -070082 using duration = std::chrono::nanoseconds;
csmartdalton4b5179b2016-09-19 11:03:58 -070083
84 Sample() : fFrames(0), fDuration(0) {}
85 double seconds() const { return std::chrono::duration<double>(fDuration).count(); }
86 double ms() const { return std::chrono::duration<double, std::milli>(fDuration).count(); }
87 double value() const { return FLAGS_fps ? fFrames / this->seconds() : this->ms() / fFrames; }
88 static const char* metric() { return FLAGS_fps ? "fps" : "ms"; }
89
csmartdaltonc6618dd2016-10-05 08:42:03 -070090 int fFrames;
91 duration fDuration;
csmartdalton4b5179b2016-09-19 11:03:58 -070092};
93
csmartdaltone0384892016-09-28 14:53:07 -070094class GpuSync {
95public:
csmartdaltonc6618dd2016-10-05 08:42:03 -070096 GpuSync(const sk_gpu_test::FenceSync* fenceSync);
csmartdaltone0384892016-09-28 14:53:07 -070097 ~GpuSync();
98
99 void syncToPreviousFrame();
100
101private:
102 void updateFence();
103
csmartdaltonc6618dd2016-10-05 08:42:03 -0700104 const sk_gpu_test::FenceSync* const fFenceSync;
105 sk_gpu_test::PlatformFence fFence;
csmartdaltone0384892016-09-28 14:53:07 -0700106};
107
csmartdalton4b5179b2016-09-19 11:03:58 -0700108enum class ExitErr {
109 kOk = 0,
110 kUsage = 64,
111 kData = 65,
112 kUnavailable = 69,
113 kIO = 74,
114 kSoftware = 70
115};
116
Robert Phillips9882dae2019-03-04 11:00:10 -0500117static void draw_skp_and_flush(SkSurface*, const SkPicture*);
csmartdalton5772eaa2016-10-11 18:28:54 -0700118static sk_sp<SkPicture> create_warmup_skp();
Chris Daltona4f5ce02018-06-26 10:13:06 -0600119static sk_sp<SkPicture> create_skp_from_svg(SkStream*, const char* filename);
csmartdalton4b5179b2016-09-19 11:03:58 -0700120static bool mkdir_p(const SkString& name);
Mike Klein88544fb2019-03-20 10:50:33 -0500121static SkString join(const CommandLineFlags::StringArray&);
csmartdalton4b5179b2016-09-19 11:03:58 -0700122static void exitf(ExitErr, const char* format, ...);
123
Robert Phillips96601082018-05-29 16:13:26 -0400124static void ddl_sample(GrContext* context, DDLTileHelper* tiles, GpuSync* gpuSync, Sample* sample,
125 std::chrono::high_resolution_clock::time_point* startStopTime) {
126 using clock = std::chrono::high_resolution_clock;
127
128 clock::time_point start = *startStopTime;
129
130 tiles->createDDLsInParallel();
131
Robert Phillips65eb4fb2018-05-31 13:27:52 -0400132 if (!FLAGS_ddlRecordTime) {
133 tiles->drawAllTilesAndFlush(context, true);
134 if (gpuSync) {
135 gpuSync->syncToPreviousFrame();
136 }
Robert Phillips96601082018-05-29 16:13:26 -0400137 }
138
139 *startStopTime = clock::now();
140
141 tiles->resetAllTiles();
142
143 if (sample) {
144 SkASSERT(gpuSync);
145 sample->fDuration += *startStopTime - start;
146 sample->fFrames++;
147 }
148}
149
150static void run_ddl_benchmark(const sk_gpu_test::FenceSync* fenceSync,
151 GrContext* context, SkCanvas* finalCanvas,
152 SkPicture* inputPicture, std::vector<Sample>* samples) {
153 using clock = std::chrono::high_resolution_clock;
154 const Sample::duration sampleDuration = std::chrono::milliseconds(FLAGS_sampleMs);
155 const clock::duration benchDuration = std::chrono::milliseconds(FLAGS_duration);
156
157 SkIRect viewport = finalCanvas->imageInfo().bounds();
158
159 DDLPromiseImageHelper promiseImageHelper;
160 sk_sp<SkData> compressedPictureData = promiseImageHelper.deflateSKP(inputPicture);
161 if (!compressedPictureData) {
162 exitf(ExitErr::kUnavailable, "DDL: conversion of skp failed");
163 }
164
165 promiseImageHelper.uploadAllToGPU(context);
166
167 DDLTileHelper tiles(finalCanvas, viewport, FLAGS_ddlTilingWidthHeight);
168
169 tiles.createSKPPerTile(compressedPictureData.get(), promiseImageHelper);
170
Robert Phillipsf7dcdb02018-06-21 11:18:25 -0400171 SkTaskGroup::Enabler enabled(FLAGS_ddlNumAdditionalThreads);
172
Robert Phillips96601082018-05-29 16:13:26 -0400173 clock::time_point startStopTime = clock::now();
174
175 ddl_sample(context, &tiles, nullptr, nullptr, &startStopTime);
176 GpuSync gpuSync(fenceSync);
177 ddl_sample(context, &tiles, &gpuSync, nullptr, &startStopTime);
178
179 clock::duration cumulativeDuration = std::chrono::milliseconds(0);
180
181 do {
182 samples->emplace_back();
183 Sample& sample = samples->back();
184
185 do {
186 ddl_sample(context, &tiles, &gpuSync, &sample, &startStopTime);
187 } while (sample.fDuration < sampleDuration);
188
189 cumulativeDuration += sample.fDuration;
190 } while (cumulativeDuration < benchDuration || 0 == samples->size() % 2);
191
192 if (!FLAGS_png.isEmpty()) {
193 // The user wants to see the final result
194 tiles.composeAllTiles(finalCanvas);
195 }
196}
197
Robert Phillips9882dae2019-03-04 11:00:10 -0500198static void run_benchmark(const sk_gpu_test::FenceSync* fenceSync, SkSurface* surface,
csmartdaltonc6618dd2016-10-05 08:42:03 -0700199 const SkPicture* skp, std::vector<Sample>* samples) {
200 using clock = std::chrono::high_resolution_clock;
201 const Sample::duration sampleDuration = std::chrono::milliseconds(FLAGS_sampleMs);
csmartdalton037adf32016-09-28 13:56:01 -0700202 const clock::duration benchDuration = std::chrono::milliseconds(FLAGS_duration);
csmartdalton4b5179b2016-09-19 11:03:58 -0700203
Robert Phillips9882dae2019-03-04 11:00:10 -0500204 draw_skp_and_flush(surface, skp); // draw 1
csmartdaltone0384892016-09-28 14:53:07 -0700205 GpuSync gpuSync(fenceSync);
csmartdalton4b5179b2016-09-19 11:03:58 -0700206
Chris Daltona2b5b642018-06-24 13:08:57 -0600207 for (int i = 1; i < kNumFlushesToPrimeCache; ++i) {
Robert Phillips9882dae2019-03-04 11:00:10 -0500208 draw_skp_and_flush(surface, skp); // draw N
Chris Daltona2b5b642018-06-24 13:08:57 -0600209 // Waits for draw N-1 to finish (after draw N's cpu work is done).
210 gpuSync.syncToPreviousFrame();
211 }
csmartdalton4b5179b2016-09-19 11:03:58 -0700212
csmartdalton037adf32016-09-28 13:56:01 -0700213 clock::time_point now = clock::now();
214 const clock::time_point endTime = now + benchDuration;
csmartdalton4b5179b2016-09-19 11:03:58 -0700215
csmartdalton037adf32016-09-28 13:56:01 -0700216 do {
217 clock::time_point sampleStart = now;
218 samples->emplace_back();
219 Sample& sample = samples->back();
220
csmartdalton4b5179b2016-09-19 11:03:58 -0700221 do {
Robert Phillips9882dae2019-03-04 11:00:10 -0500222 draw_skp_and_flush(surface, skp);
csmartdaltone0384892016-09-28 14:53:07 -0700223 gpuSync.syncToPreviousFrame();
csmartdalton4b5179b2016-09-19 11:03:58 -0700224
csmartdalton037adf32016-09-28 13:56:01 -0700225 now = clock::now();
226 sample.fDuration = now - sampleStart;
csmartdalton4b5179b2016-09-19 11:03:58 -0700227 ++sample.fFrames;
csmartdalton037adf32016-09-28 13:56:01 -0700228 } while (sample.fDuration < sampleDuration);
229 } while (now < endTime || 0 == samples->size() % 2);
csmartdalton4b5179b2016-09-19 11:03:58 -0700230}
231
csmartdaltonc6618dd2016-10-05 08:42:03 -0700232static void run_gpu_time_benchmark(sk_gpu_test::GpuTimer* gpuTimer,
Robert Phillips9882dae2019-03-04 11:00:10 -0500233 const sk_gpu_test::FenceSync* fenceSync, SkSurface* surface,
csmartdaltonc6618dd2016-10-05 08:42:03 -0700234 const SkPicture* skp, std::vector<Sample>* samples) {
235 using sk_gpu_test::PlatformTimerQuery;
236 using clock = std::chrono::steady_clock;
237 const clock::duration sampleDuration = std::chrono::milliseconds(FLAGS_sampleMs);
238 const clock::duration benchDuration = std::chrono::milliseconds(FLAGS_duration);
239
240 if (!gpuTimer->disjointSupport()) {
241 fprintf(stderr, "WARNING: GPU timer cannot detect disjoint operations; "
242 "results may be unreliable\n");
243 }
244
Robert Phillips9882dae2019-03-04 11:00:10 -0500245 draw_skp_and_flush(surface, skp);
csmartdaltonc6618dd2016-10-05 08:42:03 -0700246 GpuSync gpuSync(fenceSync);
247
Chris Daltona2b5b642018-06-24 13:08:57 -0600248 PlatformTimerQuery previousTime = 0;
249 for (int i = 1; i < kNumFlushesToPrimeCache; ++i) {
250 gpuTimer->queueStart();
Robert Phillips9882dae2019-03-04 11:00:10 -0500251 draw_skp_and_flush(surface, skp);
Chris Daltona2b5b642018-06-24 13:08:57 -0600252 previousTime = gpuTimer->queueStop();
253 gpuSync.syncToPreviousFrame();
254 }
csmartdaltonc6618dd2016-10-05 08:42:03 -0700255
256 clock::time_point now = clock::now();
257 const clock::time_point endTime = now + benchDuration;
258
259 do {
260 const clock::time_point sampleEndTime = now + sampleDuration;
261 samples->emplace_back();
262 Sample& sample = samples->back();
263
264 do {
265 gpuTimer->queueStart();
Robert Phillips9882dae2019-03-04 11:00:10 -0500266 draw_skp_and_flush(surface, skp);
csmartdaltonc6618dd2016-10-05 08:42:03 -0700267 PlatformTimerQuery time = gpuTimer->queueStop();
268 gpuSync.syncToPreviousFrame();
269
270 switch (gpuTimer->checkQueryStatus(previousTime)) {
271 using QueryStatus = sk_gpu_test::GpuTimer::QueryStatus;
272 case QueryStatus::kInvalid:
273 exitf(ExitErr::kUnavailable, "GPU timer failed");
274 case QueryStatus::kPending:
275 exitf(ExitErr::kUnavailable, "timer query still not ready after fence sync");
276 case QueryStatus::kDisjoint:
277 if (FLAGS_verbosity >= 4) {
278 fprintf(stderr, "discarding timer query due to disjoint operations.\n");
279 }
280 break;
281 case QueryStatus::kAccurate:
282 sample.fDuration += gpuTimer->getTimeElapsed(previousTime);
283 ++sample.fFrames;
284 break;
285 }
286 gpuTimer->deleteQuery(previousTime);
287 previousTime = time;
288 now = clock::now();
289 } while (now < sampleEndTime || 0 == sample.fFrames);
290 } while (now < endTime || 0 == samples->size() % 2);
291
292 gpuTimer->deleteQuery(previousTime);
293}
294
csmartdalton4b5179b2016-09-19 11:03:58 -0700295void print_result(const std::vector<Sample>& samples, const char* config, const char* bench) {
296 if (0 == (samples.size() % 2)) {
297 exitf(ExitErr::kSoftware, "attempted to gather stats on even number of samples");
298 }
299
300 Sample accum = Sample();
301 std::vector<double> values;
302 values.reserve(samples.size());
303 for (const Sample& sample : samples) {
304 accum.fFrames += sample.fFrames;
305 accum.fDuration += sample.fDuration;
306 values.push_back(sample.value());
307 }
308 std::sort(values.begin(), values.end());
csmartdalton4b5179b2016-09-19 11:03:58 -0700309
csmartdalton6904b192016-09-29 06:23:23 -0700310 const double accumValue = accum.value();
csmartdalton4b5179b2016-09-19 11:03:58 -0700311 double variance = 0;
csmartdalton037adf32016-09-28 13:56:01 -0700312 for (double value : values) {
313 const double delta = value - accumValue;
csmartdalton4b5179b2016-09-19 11:03:58 -0700314 variance += delta * delta;
315 }
csmartdalton037adf32016-09-28 13:56:01 -0700316 variance /= values.size();
csmartdalton4b5179b2016-09-19 11:03:58 -0700317 // Technically, this is the relative standard deviation.
csmartdalton037adf32016-09-28 13:56:01 -0700318 const double stddev = 100/*%*/ * sqrt(variance) / accumValue;
csmartdalton4b5179b2016-09-19 11:03:58 -0700319
csmartdalton6904b192016-09-29 06:23:23 -0700320 printf(resultFormat, accumValue, values[values.size() / 2], values.back(), values.front(),
csmartdaltonc6618dd2016-10-05 08:42:03 -0700321 stddev, values.size(), FLAGS_sampleMs, FLAGS_gpuClock ? "gpu" : "cpu", Sample::metric(),
322 config, bench);
csmartdalton4b5179b2016-09-19 11:03:58 -0700323 printf("\n");
324 fflush(stdout);
325}
326
327int main(int argc, char** argv) {
Mike Klein88544fb2019-03-20 10:50:33 -0500328 CommandLineFlags::SetUsage(
329 "Use skpbench.py instead. "
330 "You usually don't want to use this program directly.");
331 CommandLineFlags::Parse(argc, argv);
csmartdalton4b5179b2016-09-19 11:03:58 -0700332
333 if (!FLAGS_suppressHeader) {
334 printf("%s\n", header);
335 }
csmartdalton037adf32016-09-28 13:56:01 -0700336 if (FLAGS_duration <= 0) {
csmartdalton4b5179b2016-09-19 11:03:58 -0700337 exit(0); // This can be used to print the header and quit.
338 }
csmartdalton4b5179b2016-09-19 11:03:58 -0700339
340 // Parse the config.
341 const SkCommandLineConfigGpu* config = nullptr; // Initialize for spurious warning.
342 SkCommandLineConfigArray configs;
343 ParseConfigs(FLAGS_config, &configs);
344 if (configs.count() != 1 || !(config = configs[0]->asConfigGpu())) {
csmartdalton5772eaa2016-10-11 18:28:54 -0700345 exitf(ExitErr::kUsage, "invalid config '%s': must specify one (and only one) GPU config",
csmartdalton4b5179b2016-09-19 11:03:58 -0700346 join(FLAGS_config).c_str());
347 }
348
349 // Parse the skp.
Chris Daltona4f5ce02018-06-26 10:13:06 -0600350 if (FLAGS_src.count() != 1) {
351 exitf(ExitErr::kUsage,
352 "invalid input '%s': must specify a single .skp or .svg file, or 'warmup'",
353 join(FLAGS_src).c_str());
csmartdalton4b5179b2016-09-19 11:03:58 -0700354 }
Robert Phillips96601082018-05-29 16:13:26 -0400355
356 SkGraphics::Init();
Robert Phillips96601082018-05-29 16:13:26 -0400357
csmartdalton5772eaa2016-10-11 18:28:54 -0700358 sk_sp<SkPicture> skp;
Chris Daltona4f5ce02018-06-26 10:13:06 -0600359 SkString srcname;
360 if (0 == strcmp(FLAGS_src[0], "warmup")) {
csmartdalton5772eaa2016-10-11 18:28:54 -0700361 skp = create_warmup_skp();
Chris Daltona4f5ce02018-06-26 10:13:06 -0600362 srcname = "warmup";
csmartdalton5772eaa2016-10-11 18:28:54 -0700363 } else {
Chris Daltona4f5ce02018-06-26 10:13:06 -0600364 SkString srcfile(FLAGS_src[0]);
365 std::unique_ptr<SkStream> srcstream(SkStream::MakeFromFile(srcfile.c_str()));
366 if (!srcstream) {
367 exitf(ExitErr::kIO, "failed to open file %s", srcfile.c_str());
csmartdalton5772eaa2016-10-11 18:28:54 -0700368 }
Chris Daltona4f5ce02018-06-26 10:13:06 -0600369 if (srcfile.endsWith(".svg")) {
370 skp = create_skp_from_svg(srcstream.get(), srcfile.c_str());
371 } else {
372 skp = SkPicture::MakeFromStream(srcstream.get());
373 }
csmartdalton5772eaa2016-10-11 18:28:54 -0700374 if (!skp) {
Chris Daltona4f5ce02018-06-26 10:13:06 -0600375 exitf(ExitErr::kData, "failed to parse file %s", srcfile.c_str());
csmartdalton5772eaa2016-10-11 18:28:54 -0700376 }
Chris Daltona4f5ce02018-06-26 10:13:06 -0600377 srcname = SkOSPath::Basename(srcfile.c_str());
csmartdalton4b5179b2016-09-19 11:03:58 -0700378 }
379 int width = SkTMin(SkScalarCeilToInt(skp->cullRect().width()), 2048),
380 height = SkTMin(SkScalarCeilToInt(skp->cullRect().height()), 2048);
csmartdaltond7a9db62016-09-22 05:10:02 -0700381 if (FLAGS_verbosity >= 3 &&
csmartdalton4b5179b2016-09-19 11:03:58 -0700382 (width != skp->cullRect().width() || height != skp->cullRect().height())) {
csmartdaltond7a9db62016-09-22 05:10:02 -0700383 fprintf(stderr, "%s is too large (%ix%i), cropping to %ix%i.\n",
Chris Daltona4f5ce02018-06-26 10:13:06 -0600384 srcname.c_str(), SkScalarCeilToInt(skp->cullRect().width()),
csmartdalton4b5179b2016-09-19 11:03:58 -0700385 SkScalarCeilToInt(skp->cullRect().height()), width, height);
386 }
387
Brian Salomonf865b052018-03-09 09:01:53 -0500388 if (config->getSurfType() != SkCommandLineConfigGpu::SurfType::kDefault) {
389 exitf(ExitErr::kUnavailable, "This tool only supports the default surface type. (%s)",
390 config->getTag().c_str());
391 }
392
csmartdalton4b5179b2016-09-19 11:03:58 -0700393 // Create a context.
csmartdalton008b9d82017-02-22 12:00:42 -0700394 GrContextOptions ctxOptions;
Chris Dalton040238b2017-12-18 14:22:34 -0700395 SetCtxOptionsFromCommonFlags(&ctxOptions);
csmartdalton008b9d82017-02-22 12:00:42 -0700396 sk_gpu_test::GrContextFactory factory(ctxOptions);
csmartdalton4b5179b2016-09-19 11:03:58 -0700397 sk_gpu_test::ContextInfo ctxInfo =
csmartdaltone812d492017-02-21 12:36:05 -0700398 factory.getContextInfo(config->getContextType(), config->getContextOverrides());
csmartdalton4b5179b2016-09-19 11:03:58 -0700399 GrContext* ctx = ctxInfo.grContext();
400 if (!ctx) {
401 exitf(ExitErr::kUnavailable, "failed to create context for config %s",
402 config->getTag().c_str());
403 }
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400404 if (ctx->maxRenderTargetSize() < SkTMax(width, height)) {
csmartdalton4b5179b2016-09-19 11:03:58 -0700405 exitf(ExitErr::kUnavailable, "render target size %ix%i not supported by platform (max: %i)",
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400406 width, height, ctx->maxRenderTargetSize());
csmartdalton4b5179b2016-09-19 11:03:58 -0700407 }
Brian Osman2b23c4b2018-06-01 12:25:08 -0400408 GrPixelConfig grPixConfig = SkColorType2GrPixelConfig(config->getColorType());
Greg Daniel0a7aa142018-02-21 13:02:32 -0500409 if (kUnknown_GrPixelConfig == grPixConfig) {
410 exitf(ExitErr::kUnavailable, "failed to get GrPixelConfig from SkColorType: %d",
411 config->getColorType());
412 }
Robert Phillips9da87e02019-02-04 13:26:26 -0500413 int supportedSampleCount = ctx->priv().caps()->getRenderTargetSampleCount(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400414 config->getSamples(), grPixConfig);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400415 if (supportedSampleCount != config->getSamples()) {
416 exitf(ExitErr::kUnavailable, "sample count %i not supported by platform",
417 config->getSamples());
csmartdalton4b5179b2016-09-19 11:03:58 -0700418 }
419 sk_gpu_test::TestContext* testCtx = ctxInfo.testContext();
420 if (!testCtx) {
421 exitf(ExitErr::kSoftware, "testContext is null");
422 }
423 if (!testCtx->fenceSyncSupport()) {
424 exitf(ExitErr::kUnavailable, "GPU does not support fence sync");
425 }
426
427 // Create a render target.
Brian Salomonce5ee602017-07-17 11:31:31 -0400428 SkImageInfo info =
429 SkImageInfo::Make(width, height, config->getColorType(), config->getAlphaType(),
430 sk_ref_sp(config->getColorSpace()));
csmartdalton4b5179b2016-09-19 11:03:58 -0700431 uint32_t flags = config->getUseDIText() ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag : 0;
432 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
433 sk_sp<SkSurface> surface =
434 SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, config->getSamples(), &props);
435 if (!surface) {
436 exitf(ExitErr::kUnavailable, "failed to create %ix%i render target for config %s",
437 width, height, config->getTag().c_str());
438 }
439
csmartdalton5772eaa2016-10-11 18:28:54 -0700440 // Run the benchmark.
csmartdalton4b5179b2016-09-19 11:03:58 -0700441 std::vector<Sample> samples;
csmartdalton037adf32016-09-28 13:56:01 -0700442 if (FLAGS_sampleMs > 0) {
443 // +1 because we might take one more sample in order to have an odd number.
444 samples.reserve(1 + (FLAGS_duration + FLAGS_sampleMs - 1) / FLAGS_sampleMs);
445 } else {
446 samples.reserve(2 * FLAGS_duration);
447 }
csmartdalton4b5179b2016-09-19 11:03:58 -0700448 SkCanvas* canvas = surface->getCanvas();
449 canvas->translate(-skp->cullRect().x(), -skp->cullRect().y());
csmartdaltonc6618dd2016-10-05 08:42:03 -0700450 if (!FLAGS_gpuClock) {
Robert Phillips96601082018-05-29 16:13:26 -0400451 if (FLAGS_ddl) {
452 run_ddl_benchmark(testCtx->fenceSync(), ctx, canvas, skp.get(), &samples);
453 } else {
Robert Phillips9882dae2019-03-04 11:00:10 -0500454 run_benchmark(testCtx->fenceSync(), surface.get(), skp.get(), &samples);
Robert Phillips96601082018-05-29 16:13:26 -0400455 }
csmartdaltonc6618dd2016-10-05 08:42:03 -0700456 } else {
Robert Phillips96601082018-05-29 16:13:26 -0400457 if (FLAGS_ddl) {
458 exitf(ExitErr::kUnavailable, "DDL: GPU-only timing not supported");
459 }
csmartdaltonc6618dd2016-10-05 08:42:03 -0700460 if (!testCtx->gpuTimingSupport()) {
461 exitf(ExitErr::kUnavailable, "GPU does not support timing");
462 }
Robert Phillips9882dae2019-03-04 11:00:10 -0500463 run_gpu_time_benchmark(testCtx->gpuTimer(), testCtx->fenceSync(), surface.get(), skp.get(),
csmartdaltonc6618dd2016-10-05 08:42:03 -0700464 &samples);
465 }
Chris Daltona4f5ce02018-06-26 10:13:06 -0600466 print_result(samples, config->getTag().c_str(), srcname.c_str());
csmartdalton4b5179b2016-09-19 11:03:58 -0700467
468 // Save a proof (if one was requested).
469 if (!FLAGS_png.isEmpty()) {
470 SkBitmap bmp;
Mike Reed12e946b2017-04-17 10:53:29 -0400471 bmp.allocPixels(info);
472 if (!surface->getCanvas()->readPixels(bmp, 0, 0)) {
csmartdalton4b5179b2016-09-19 11:03:58 -0700473 exitf(ExitErr::kUnavailable, "failed to read canvas pixels for png");
474 }
Brian Osmane581aaa2018-08-09 09:46:53 -0400475 if (!mkdir_p(SkOSPath::Dirname(FLAGS_png[0]))) {
476 exitf(ExitErr::kIO, "failed to create directory for png \"%s\"", FLAGS_png[0]);
csmartdalton4b5179b2016-09-19 11:03:58 -0700477 }
Mike Kleinea3f0142019-03-20 11:12:10 -0500478 if (!ToolUtils::EncodeImageToFile(FLAGS_png[0], bmp, SkEncodedImageFormat::kPNG, 100)) {
csmartdalton4b5179b2016-09-19 11:03:58 -0700479 exitf(ExitErr::kIO, "failed to save png to \"%s\"", FLAGS_png[0]);
480 }
481 }
482
483 exit(0);
484}
485
Robert Phillips9882dae2019-03-04 11:00:10 -0500486static void draw_skp_and_flush(SkSurface* surface, const SkPicture* skp) {
487 auto canvas = surface->getCanvas();
csmartdalton4b5179b2016-09-19 11:03:58 -0700488 canvas->drawPicture(skp);
Robert Phillips9882dae2019-03-04 11:00:10 -0500489 surface->flush();
csmartdalton4b5179b2016-09-19 11:03:58 -0700490}
491
csmartdalton5772eaa2016-10-11 18:28:54 -0700492static sk_sp<SkPicture> create_warmup_skp() {
493 static constexpr SkRect bounds{0, 0, 500, 500};
494 SkPictureRecorder recorder;
495 SkCanvas* recording = recorder.beginRecording(bounds);
496
497 recording->clear(SK_ColorWHITE);
498
499 SkPaint stroke;
500 stroke.setStyle(SkPaint::kStroke_Style);
501 stroke.setStrokeWidth(2);
502
503 // Use a big path to (theoretically) warmup the CPU.
504 SkPath bigPath;
Mike Kleinea3f0142019-03-20 11:12:10 -0500505 ToolUtils::make_big_path(bigPath);
csmartdalton5772eaa2016-10-11 18:28:54 -0700506 recording->drawPath(bigPath, stroke);
507
508 // Use a perlin shader to warmup the GPU.
509 SkPaint perlin;
510 perlin.setShader(SkPerlinNoiseShader::MakeTurbulence(0.1f, 0.1f, 1, 0, nullptr));
511 recording->drawRect(bounds, perlin);
512
513 return recorder.finishRecordingAsPicture();
514}
515
Chris Daltona4f5ce02018-06-26 10:13:06 -0600516static sk_sp<SkPicture> create_skp_from_svg(SkStream* stream, const char* filename) {
517#ifdef SK_XML
518 SkDOM xml;
519 if (!xml.build(*stream)) {
520 exitf(ExitErr::kData, "failed to parse xml in file %s", filename);
521 }
522 sk_sp<SkSVGDOM> svg = SkSVGDOM::MakeFromDOM(xml);
523 if (!svg) {
524 exitf(ExitErr::kData, "failed to build svg dom from file %s", filename);
525 }
526
527 static constexpr SkRect bounds{0, 0, 1200, 1200};
528 SkPictureRecorder recorder;
529 SkCanvas* recording = recorder.beginRecording(bounds);
530
531 svg->setContainerSize(SkSize::Make(recording->getBaseLayerSize()));
532 svg->render(recording);
533
534 return recorder.finishRecordingAsPicture();
535#endif
536 exitf(ExitErr::kData, "SK_XML is disabled; cannot open svg file %s", filename);
Florin Malita5d3ff432018-07-31 16:38:43 -0400537 return nullptr;
Chris Daltona4f5ce02018-06-26 10:13:06 -0600538}
539
csmartdalton4b5179b2016-09-19 11:03:58 -0700540bool mkdir_p(const SkString& dirname) {
541 if (dirname.isEmpty()) {
542 return true;
543 }
544 return mkdir_p(SkOSPath::Dirname(dirname.c_str())) && sk_mkdir(dirname.c_str());
545}
546
Mike Klein88544fb2019-03-20 10:50:33 -0500547static SkString join(const CommandLineFlags::StringArray& stringArray) {
csmartdalton4b5179b2016-09-19 11:03:58 -0700548 SkString joined;
csmartdalton5772eaa2016-10-11 18:28:54 -0700549 for (int i = 0; i < stringArray.count(); ++i) {
550 joined.appendf(i ? " %s" : "%s", stringArray[i]);
csmartdalton4b5179b2016-09-19 11:03:58 -0700551 }
552 return joined;
553}
554
555static void exitf(ExitErr err, const char* format, ...) {
556 fprintf(stderr, ExitErr::kSoftware == err ? "INTERNAL ERROR: " : "ERROR: ");
557 va_list args;
558 va_start(args, format);
559 vfprintf(stderr, format, args);
560 va_end(args);
561 fprintf(stderr, ExitErr::kSoftware == err ? "; this should never happen.\n": ".\n");
562 exit((int)err);
563}
csmartdaltone0384892016-09-28 14:53:07 -0700564
csmartdaltonc6618dd2016-10-05 08:42:03 -0700565GpuSync::GpuSync(const sk_gpu_test::FenceSync* fenceSync)
csmartdaltone0384892016-09-28 14:53:07 -0700566 : fFenceSync(fenceSync) {
567 this->updateFence();
568}
569
570GpuSync::~GpuSync() {
571 fFenceSync->deleteFence(fFence);
572}
573
574void GpuSync::syncToPreviousFrame() {
csmartdaltonc6618dd2016-10-05 08:42:03 -0700575 if (sk_gpu_test::kInvalidFence == fFence) {
csmartdaltone0384892016-09-28 14:53:07 -0700576 exitf(ExitErr::kSoftware, "attempted to sync with invalid fence");
577 }
578 if (!fFenceSync->waitFence(fFence)) {
579 exitf(ExitErr::kUnavailable, "failed to wait for fence");
580 }
581 fFenceSync->deleteFence(fFence);
582 this->updateFence();
583}
584
585void GpuSync::updateFence() {
586 fFence = fFenceSync->insertFence();
csmartdaltonc6618dd2016-10-05 08:42:03 -0700587 if (sk_gpu_test::kInvalidFence == fFence) {
csmartdaltone0384892016-09-28 14:53:07 -0700588 exitf(ExitErr::kUnavailable, "failed to insert fence");
589 }
590}