blob: b0da85a5900dce8974ed6cea6cb7b89e5fc4077b [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 Klein88544fb2019-03-20 10:50:33 -050029#include "flags/CommandLineFlags.h"
30#include "flags/CommonFlagsConfig.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040031#include "sk_tool_utils.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
Robert Phillips96601082018-05-29 16:13:26 -040057DEFINE_bool(ddl, false, "record the skp into DDLs before rendering");
58DEFINE_int32(ddlNumAdditionalThreads, 0, "number of DDL recording threads in addition to main one");
59DEFINE_int32(ddlTilingWidthHeight, 0, "number of tiles along one edge when in DDL mode");
Robert Phillips65eb4fb2018-05-31 13:27:52 -040060DEFINE_bool(ddlRecordTime, false, "report just the cpu time spent recording DDLs");
Robert Phillips96601082018-05-29 16:13:26 -040061
csmartdalton037adf32016-09-28 13:56:01 -070062DEFINE_int32(duration, 5000, "number of milliseconds to run the benchmark");
63DEFINE_int32(sampleMs, 50, "minimum duration of a sample");
csmartdaltonc6618dd2016-10-05 08:42:03 -070064DEFINE_bool(gpuClock, false, "time on the gpu clock (gpu work only)");
csmartdalton4b5179b2016-09-19 11:03:58 -070065DEFINE_bool(fps, false, "use fps instead of ms");
Chris Daltona4f5ce02018-06-26 10:13:06 -060066DEFINE_string(src, "", "path to a single .skp or .svg file, or 'warmup' for a builtin warmup run");
csmartdalton4b5179b2016-09-19 11:03:58 -070067DEFINE_string(png, "", "if set, save a .png proof to disk at this file location");
68DEFINE_int32(verbosity, 4, "level of verbosity (0=none to 5=debug)");
69DEFINE_bool(suppressHeader, false, "don't print a header row before the results");
70
71static const char* header =
csmartdaltonc6618dd2016-10-05 08:42:03 -070072" accum median max min stddev samples sample_ms clock metric config bench";
csmartdalton4b5179b2016-09-19 11:03:58 -070073
74static const char* resultFormat =
csmartdaltonc6618dd2016-10-05 08:42:03 -070075"%8.4g %8.4g %8.4g %8.4g %6.3g%% %7li %9i %-5s %-6s %-9s %s";
csmartdalton4b5179b2016-09-19 11:03:58 -070076
Chris Daltona2b5b642018-06-24 13:08:57 -060077static constexpr int kNumFlushesToPrimeCache = 3;
78
csmartdalton4b5179b2016-09-19 11:03:58 -070079struct Sample {
csmartdaltonc6618dd2016-10-05 08:42:03 -070080 using duration = std::chrono::nanoseconds;
csmartdalton4b5179b2016-09-19 11:03:58 -070081
82 Sample() : fFrames(0), fDuration(0) {}
83 double seconds() const { return std::chrono::duration<double>(fDuration).count(); }
84 double ms() const { return std::chrono::duration<double, std::milli>(fDuration).count(); }
85 double value() const { return FLAGS_fps ? fFrames / this->seconds() : this->ms() / fFrames; }
86 static const char* metric() { return FLAGS_fps ? "fps" : "ms"; }
87
csmartdaltonc6618dd2016-10-05 08:42:03 -070088 int fFrames;
89 duration fDuration;
csmartdalton4b5179b2016-09-19 11:03:58 -070090};
91
csmartdaltone0384892016-09-28 14:53:07 -070092class GpuSync {
93public:
csmartdaltonc6618dd2016-10-05 08:42:03 -070094 GpuSync(const sk_gpu_test::FenceSync* fenceSync);
csmartdaltone0384892016-09-28 14:53:07 -070095 ~GpuSync();
96
97 void syncToPreviousFrame();
98
99private:
100 void updateFence();
101
csmartdaltonc6618dd2016-10-05 08:42:03 -0700102 const sk_gpu_test::FenceSync* const fFenceSync;
103 sk_gpu_test::PlatformFence fFence;
csmartdaltone0384892016-09-28 14:53:07 -0700104};
105
csmartdalton4b5179b2016-09-19 11:03:58 -0700106enum class ExitErr {
107 kOk = 0,
108 kUsage = 64,
109 kData = 65,
110 kUnavailable = 69,
111 kIO = 74,
112 kSoftware = 70
113};
114
Robert Phillips9882dae2019-03-04 11:00:10 -0500115static void draw_skp_and_flush(SkSurface*, const SkPicture*);
csmartdalton5772eaa2016-10-11 18:28:54 -0700116static sk_sp<SkPicture> create_warmup_skp();
Chris Daltona4f5ce02018-06-26 10:13:06 -0600117static sk_sp<SkPicture> create_skp_from_svg(SkStream*, const char* filename);
csmartdalton4b5179b2016-09-19 11:03:58 -0700118static bool mkdir_p(const SkString& name);
Mike Klein88544fb2019-03-20 10:50:33 -0500119static SkString join(const CommandLineFlags::StringArray&);
csmartdalton4b5179b2016-09-19 11:03:58 -0700120static void exitf(ExitErr, const char* format, ...);
121
Robert Phillips96601082018-05-29 16:13:26 -0400122static void ddl_sample(GrContext* context, DDLTileHelper* tiles, GpuSync* gpuSync, Sample* sample,
123 std::chrono::high_resolution_clock::time_point* startStopTime) {
124 using clock = std::chrono::high_resolution_clock;
125
126 clock::time_point start = *startStopTime;
127
128 tiles->createDDLsInParallel();
129
Robert Phillips65eb4fb2018-05-31 13:27:52 -0400130 if (!FLAGS_ddlRecordTime) {
131 tiles->drawAllTilesAndFlush(context, true);
132 if (gpuSync) {
133 gpuSync->syncToPreviousFrame();
134 }
Robert Phillips96601082018-05-29 16:13:26 -0400135 }
136
137 *startStopTime = clock::now();
138
139 tiles->resetAllTiles();
140
141 if (sample) {
142 SkASSERT(gpuSync);
143 sample->fDuration += *startStopTime - start;
144 sample->fFrames++;
145 }
146}
147
148static void run_ddl_benchmark(const sk_gpu_test::FenceSync* fenceSync,
149 GrContext* context, SkCanvas* finalCanvas,
150 SkPicture* inputPicture, std::vector<Sample>* samples) {
151 using clock = std::chrono::high_resolution_clock;
152 const Sample::duration sampleDuration = std::chrono::milliseconds(FLAGS_sampleMs);
153 const clock::duration benchDuration = std::chrono::milliseconds(FLAGS_duration);
154
155 SkIRect viewport = finalCanvas->imageInfo().bounds();
156
157 DDLPromiseImageHelper promiseImageHelper;
158 sk_sp<SkData> compressedPictureData = promiseImageHelper.deflateSKP(inputPicture);
159 if (!compressedPictureData) {
160 exitf(ExitErr::kUnavailable, "DDL: conversion of skp failed");
161 }
162
163 promiseImageHelper.uploadAllToGPU(context);
164
165 DDLTileHelper tiles(finalCanvas, viewport, FLAGS_ddlTilingWidthHeight);
166
167 tiles.createSKPPerTile(compressedPictureData.get(), promiseImageHelper);
168
Robert Phillipsf7dcdb02018-06-21 11:18:25 -0400169 SkTaskGroup::Enabler enabled(FLAGS_ddlNumAdditionalThreads);
170
Robert Phillips96601082018-05-29 16:13:26 -0400171 clock::time_point startStopTime = clock::now();
172
173 ddl_sample(context, &tiles, nullptr, nullptr, &startStopTime);
174 GpuSync gpuSync(fenceSync);
175 ddl_sample(context, &tiles, &gpuSync, nullptr, &startStopTime);
176
177 clock::duration cumulativeDuration = std::chrono::milliseconds(0);
178
179 do {
180 samples->emplace_back();
181 Sample& sample = samples->back();
182
183 do {
184 ddl_sample(context, &tiles, &gpuSync, &sample, &startStopTime);
185 } while (sample.fDuration < sampleDuration);
186
187 cumulativeDuration += sample.fDuration;
188 } while (cumulativeDuration < benchDuration || 0 == samples->size() % 2);
189
190 if (!FLAGS_png.isEmpty()) {
191 // The user wants to see the final result
192 tiles.composeAllTiles(finalCanvas);
193 }
194}
195
Robert Phillips9882dae2019-03-04 11:00:10 -0500196static void run_benchmark(const sk_gpu_test::FenceSync* fenceSync, SkSurface* surface,
csmartdaltonc6618dd2016-10-05 08:42:03 -0700197 const SkPicture* skp, std::vector<Sample>* samples) {
198 using clock = std::chrono::high_resolution_clock;
199 const Sample::duration sampleDuration = std::chrono::milliseconds(FLAGS_sampleMs);
csmartdalton037adf32016-09-28 13:56:01 -0700200 const clock::duration benchDuration = std::chrono::milliseconds(FLAGS_duration);
csmartdalton4b5179b2016-09-19 11:03:58 -0700201
Robert Phillips9882dae2019-03-04 11:00:10 -0500202 draw_skp_and_flush(surface, skp); // draw 1
csmartdaltone0384892016-09-28 14:53:07 -0700203 GpuSync gpuSync(fenceSync);
csmartdalton4b5179b2016-09-19 11:03:58 -0700204
Chris Daltona2b5b642018-06-24 13:08:57 -0600205 for (int i = 1; i < kNumFlushesToPrimeCache; ++i) {
Robert Phillips9882dae2019-03-04 11:00:10 -0500206 draw_skp_and_flush(surface, skp); // draw N
Chris Daltona2b5b642018-06-24 13:08:57 -0600207 // Waits for draw N-1 to finish (after draw N's cpu work is done).
208 gpuSync.syncToPreviousFrame();
209 }
csmartdalton4b5179b2016-09-19 11:03:58 -0700210
csmartdalton037adf32016-09-28 13:56:01 -0700211 clock::time_point now = clock::now();
212 const clock::time_point endTime = now + benchDuration;
csmartdalton4b5179b2016-09-19 11:03:58 -0700213
csmartdalton037adf32016-09-28 13:56:01 -0700214 do {
215 clock::time_point sampleStart = now;
216 samples->emplace_back();
217 Sample& sample = samples->back();
218
csmartdalton4b5179b2016-09-19 11:03:58 -0700219 do {
Robert Phillips9882dae2019-03-04 11:00:10 -0500220 draw_skp_and_flush(surface, skp);
csmartdaltone0384892016-09-28 14:53:07 -0700221 gpuSync.syncToPreviousFrame();
csmartdalton4b5179b2016-09-19 11:03:58 -0700222
csmartdalton037adf32016-09-28 13:56:01 -0700223 now = clock::now();
224 sample.fDuration = now - sampleStart;
csmartdalton4b5179b2016-09-19 11:03:58 -0700225 ++sample.fFrames;
csmartdalton037adf32016-09-28 13:56:01 -0700226 } while (sample.fDuration < sampleDuration);
227 } while (now < endTime || 0 == samples->size() % 2);
csmartdalton4b5179b2016-09-19 11:03:58 -0700228}
229
csmartdaltonc6618dd2016-10-05 08:42:03 -0700230static void run_gpu_time_benchmark(sk_gpu_test::GpuTimer* gpuTimer,
Robert Phillips9882dae2019-03-04 11:00:10 -0500231 const sk_gpu_test::FenceSync* fenceSync, SkSurface* surface,
csmartdaltonc6618dd2016-10-05 08:42:03 -0700232 const SkPicture* skp, std::vector<Sample>* samples) {
233 using sk_gpu_test::PlatformTimerQuery;
234 using clock = std::chrono::steady_clock;
235 const clock::duration sampleDuration = std::chrono::milliseconds(FLAGS_sampleMs);
236 const clock::duration benchDuration = std::chrono::milliseconds(FLAGS_duration);
237
238 if (!gpuTimer->disjointSupport()) {
239 fprintf(stderr, "WARNING: GPU timer cannot detect disjoint operations; "
240 "results may be unreliable\n");
241 }
242
Robert Phillips9882dae2019-03-04 11:00:10 -0500243 draw_skp_and_flush(surface, skp);
csmartdaltonc6618dd2016-10-05 08:42:03 -0700244 GpuSync gpuSync(fenceSync);
245
Chris Daltona2b5b642018-06-24 13:08:57 -0600246 PlatformTimerQuery previousTime = 0;
247 for (int i = 1; i < kNumFlushesToPrimeCache; ++i) {
248 gpuTimer->queueStart();
Robert Phillips9882dae2019-03-04 11:00:10 -0500249 draw_skp_and_flush(surface, skp);
Chris Daltona2b5b642018-06-24 13:08:57 -0600250 previousTime = gpuTimer->queueStop();
251 gpuSync.syncToPreviousFrame();
252 }
csmartdaltonc6618dd2016-10-05 08:42:03 -0700253
254 clock::time_point now = clock::now();
255 const clock::time_point endTime = now + benchDuration;
256
257 do {
258 const clock::time_point sampleEndTime = now + sampleDuration;
259 samples->emplace_back();
260 Sample& sample = samples->back();
261
262 do {
263 gpuTimer->queueStart();
Robert Phillips9882dae2019-03-04 11:00:10 -0500264 draw_skp_and_flush(surface, skp);
csmartdaltonc6618dd2016-10-05 08:42:03 -0700265 PlatformTimerQuery time = gpuTimer->queueStop();
266 gpuSync.syncToPreviousFrame();
267
268 switch (gpuTimer->checkQueryStatus(previousTime)) {
269 using QueryStatus = sk_gpu_test::GpuTimer::QueryStatus;
270 case QueryStatus::kInvalid:
271 exitf(ExitErr::kUnavailable, "GPU timer failed");
272 case QueryStatus::kPending:
273 exitf(ExitErr::kUnavailable, "timer query still not ready after fence sync");
274 case QueryStatus::kDisjoint:
275 if (FLAGS_verbosity >= 4) {
276 fprintf(stderr, "discarding timer query due to disjoint operations.\n");
277 }
278 break;
279 case QueryStatus::kAccurate:
280 sample.fDuration += gpuTimer->getTimeElapsed(previousTime);
281 ++sample.fFrames;
282 break;
283 }
284 gpuTimer->deleteQuery(previousTime);
285 previousTime = time;
286 now = clock::now();
287 } while (now < sampleEndTime || 0 == sample.fFrames);
288 } while (now < endTime || 0 == samples->size() % 2);
289
290 gpuTimer->deleteQuery(previousTime);
291}
292
csmartdalton4b5179b2016-09-19 11:03:58 -0700293void print_result(const std::vector<Sample>& samples, const char* config, const char* bench) {
294 if (0 == (samples.size() % 2)) {
295 exitf(ExitErr::kSoftware, "attempted to gather stats on even number of samples");
296 }
297
298 Sample accum = Sample();
299 std::vector<double> values;
300 values.reserve(samples.size());
301 for (const Sample& sample : samples) {
302 accum.fFrames += sample.fFrames;
303 accum.fDuration += sample.fDuration;
304 values.push_back(sample.value());
305 }
306 std::sort(values.begin(), values.end());
csmartdalton4b5179b2016-09-19 11:03:58 -0700307
csmartdalton6904b192016-09-29 06:23:23 -0700308 const double accumValue = accum.value();
csmartdalton4b5179b2016-09-19 11:03:58 -0700309 double variance = 0;
csmartdalton037adf32016-09-28 13:56:01 -0700310 for (double value : values) {
311 const double delta = value - accumValue;
csmartdalton4b5179b2016-09-19 11:03:58 -0700312 variance += delta * delta;
313 }
csmartdalton037adf32016-09-28 13:56:01 -0700314 variance /= values.size();
csmartdalton4b5179b2016-09-19 11:03:58 -0700315 // Technically, this is the relative standard deviation.
csmartdalton037adf32016-09-28 13:56:01 -0700316 const double stddev = 100/*%*/ * sqrt(variance) / accumValue;
csmartdalton4b5179b2016-09-19 11:03:58 -0700317
csmartdalton6904b192016-09-29 06:23:23 -0700318 printf(resultFormat, accumValue, values[values.size() / 2], values.back(), values.front(),
csmartdaltonc6618dd2016-10-05 08:42:03 -0700319 stddev, values.size(), FLAGS_sampleMs, FLAGS_gpuClock ? "gpu" : "cpu", Sample::metric(),
320 config, bench);
csmartdalton4b5179b2016-09-19 11:03:58 -0700321 printf("\n");
322 fflush(stdout);
323}
324
325int main(int argc, char** argv) {
Mike Klein88544fb2019-03-20 10:50:33 -0500326 CommandLineFlags::SetUsage(
327 "Use skpbench.py instead. "
328 "You usually don't want to use this program directly.");
329 CommandLineFlags::Parse(argc, argv);
csmartdalton4b5179b2016-09-19 11:03:58 -0700330
331 if (!FLAGS_suppressHeader) {
332 printf("%s\n", header);
333 }
csmartdalton037adf32016-09-28 13:56:01 -0700334 if (FLAGS_duration <= 0) {
csmartdalton4b5179b2016-09-19 11:03:58 -0700335 exit(0); // This can be used to print the header and quit.
336 }
csmartdalton4b5179b2016-09-19 11:03:58 -0700337
338 // Parse the config.
339 const SkCommandLineConfigGpu* config = nullptr; // Initialize for spurious warning.
340 SkCommandLineConfigArray configs;
341 ParseConfigs(FLAGS_config, &configs);
342 if (configs.count() != 1 || !(config = configs[0]->asConfigGpu())) {
csmartdalton5772eaa2016-10-11 18:28:54 -0700343 exitf(ExitErr::kUsage, "invalid config '%s': must specify one (and only one) GPU config",
csmartdalton4b5179b2016-09-19 11:03:58 -0700344 join(FLAGS_config).c_str());
345 }
346
347 // Parse the skp.
Chris Daltona4f5ce02018-06-26 10:13:06 -0600348 if (FLAGS_src.count() != 1) {
349 exitf(ExitErr::kUsage,
350 "invalid input '%s': must specify a single .skp or .svg file, or 'warmup'",
351 join(FLAGS_src).c_str());
csmartdalton4b5179b2016-09-19 11:03:58 -0700352 }
Robert Phillips96601082018-05-29 16:13:26 -0400353
354 SkGraphics::Init();
Robert Phillips96601082018-05-29 16:13:26 -0400355
csmartdalton5772eaa2016-10-11 18:28:54 -0700356 sk_sp<SkPicture> skp;
Chris Daltona4f5ce02018-06-26 10:13:06 -0600357 SkString srcname;
358 if (0 == strcmp(FLAGS_src[0], "warmup")) {
csmartdalton5772eaa2016-10-11 18:28:54 -0700359 skp = create_warmup_skp();
Chris Daltona4f5ce02018-06-26 10:13:06 -0600360 srcname = "warmup";
csmartdalton5772eaa2016-10-11 18:28:54 -0700361 } else {
Chris Daltona4f5ce02018-06-26 10:13:06 -0600362 SkString srcfile(FLAGS_src[0]);
363 std::unique_ptr<SkStream> srcstream(SkStream::MakeFromFile(srcfile.c_str()));
364 if (!srcstream) {
365 exitf(ExitErr::kIO, "failed to open file %s", srcfile.c_str());
csmartdalton5772eaa2016-10-11 18:28:54 -0700366 }
Chris Daltona4f5ce02018-06-26 10:13:06 -0600367 if (srcfile.endsWith(".svg")) {
368 skp = create_skp_from_svg(srcstream.get(), srcfile.c_str());
369 } else {
370 skp = SkPicture::MakeFromStream(srcstream.get());
371 }
csmartdalton5772eaa2016-10-11 18:28:54 -0700372 if (!skp) {
Chris Daltona4f5ce02018-06-26 10:13:06 -0600373 exitf(ExitErr::kData, "failed to parse file %s", srcfile.c_str());
csmartdalton5772eaa2016-10-11 18:28:54 -0700374 }
Chris Daltona4f5ce02018-06-26 10:13:06 -0600375 srcname = SkOSPath::Basename(srcfile.c_str());
csmartdalton4b5179b2016-09-19 11:03:58 -0700376 }
377 int width = SkTMin(SkScalarCeilToInt(skp->cullRect().width()), 2048),
378 height = SkTMin(SkScalarCeilToInt(skp->cullRect().height()), 2048);
csmartdaltond7a9db62016-09-22 05:10:02 -0700379 if (FLAGS_verbosity >= 3 &&
csmartdalton4b5179b2016-09-19 11:03:58 -0700380 (width != skp->cullRect().width() || height != skp->cullRect().height())) {
csmartdaltond7a9db62016-09-22 05:10:02 -0700381 fprintf(stderr, "%s is too large (%ix%i), cropping to %ix%i.\n",
Chris Daltona4f5ce02018-06-26 10:13:06 -0600382 srcname.c_str(), SkScalarCeilToInt(skp->cullRect().width()),
csmartdalton4b5179b2016-09-19 11:03:58 -0700383 SkScalarCeilToInt(skp->cullRect().height()), width, height);
384 }
385
Brian Salomonf865b052018-03-09 09:01:53 -0500386 if (config->getSurfType() != SkCommandLineConfigGpu::SurfType::kDefault) {
387 exitf(ExitErr::kUnavailable, "This tool only supports the default surface type. (%s)",
388 config->getTag().c_str());
389 }
390
csmartdalton4b5179b2016-09-19 11:03:58 -0700391 // Create a context.
csmartdalton008b9d82017-02-22 12:00:42 -0700392 GrContextOptions ctxOptions;
Chris Dalton040238b2017-12-18 14:22:34 -0700393 SetCtxOptionsFromCommonFlags(&ctxOptions);
csmartdalton008b9d82017-02-22 12:00:42 -0700394 sk_gpu_test::GrContextFactory factory(ctxOptions);
csmartdalton4b5179b2016-09-19 11:03:58 -0700395 sk_gpu_test::ContextInfo ctxInfo =
csmartdaltone812d492017-02-21 12:36:05 -0700396 factory.getContextInfo(config->getContextType(), config->getContextOverrides());
csmartdalton4b5179b2016-09-19 11:03:58 -0700397 GrContext* ctx = ctxInfo.grContext();
398 if (!ctx) {
399 exitf(ExitErr::kUnavailable, "failed to create context for config %s",
400 config->getTag().c_str());
401 }
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400402 if (ctx->maxRenderTargetSize() < SkTMax(width, height)) {
csmartdalton4b5179b2016-09-19 11:03:58 -0700403 exitf(ExitErr::kUnavailable, "render target size %ix%i not supported by platform (max: %i)",
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400404 width, height, ctx->maxRenderTargetSize());
csmartdalton4b5179b2016-09-19 11:03:58 -0700405 }
Brian Osman2b23c4b2018-06-01 12:25:08 -0400406 GrPixelConfig grPixConfig = SkColorType2GrPixelConfig(config->getColorType());
Greg Daniel0a7aa142018-02-21 13:02:32 -0500407 if (kUnknown_GrPixelConfig == grPixConfig) {
408 exitf(ExitErr::kUnavailable, "failed to get GrPixelConfig from SkColorType: %d",
409 config->getColorType());
410 }
Robert Phillips9da87e02019-02-04 13:26:26 -0500411 int supportedSampleCount = ctx->priv().caps()->getRenderTargetSampleCount(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400412 config->getSamples(), grPixConfig);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400413 if (supportedSampleCount != config->getSamples()) {
414 exitf(ExitErr::kUnavailable, "sample count %i not supported by platform",
415 config->getSamples());
csmartdalton4b5179b2016-09-19 11:03:58 -0700416 }
417 sk_gpu_test::TestContext* testCtx = ctxInfo.testContext();
418 if (!testCtx) {
419 exitf(ExitErr::kSoftware, "testContext is null");
420 }
421 if (!testCtx->fenceSyncSupport()) {
422 exitf(ExitErr::kUnavailable, "GPU does not support fence sync");
423 }
424
425 // Create a render target.
Brian Salomonce5ee602017-07-17 11:31:31 -0400426 SkImageInfo info =
427 SkImageInfo::Make(width, height, config->getColorType(), config->getAlphaType(),
428 sk_ref_sp(config->getColorSpace()));
csmartdalton4b5179b2016-09-19 11:03:58 -0700429 uint32_t flags = config->getUseDIText() ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag : 0;
430 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
431 sk_sp<SkSurface> surface =
432 SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, config->getSamples(), &props);
433 if (!surface) {
434 exitf(ExitErr::kUnavailable, "failed to create %ix%i render target for config %s",
435 width, height, config->getTag().c_str());
436 }
437
csmartdalton5772eaa2016-10-11 18:28:54 -0700438 // Run the benchmark.
csmartdalton4b5179b2016-09-19 11:03:58 -0700439 std::vector<Sample> samples;
csmartdalton037adf32016-09-28 13:56:01 -0700440 if (FLAGS_sampleMs > 0) {
441 // +1 because we might take one more sample in order to have an odd number.
442 samples.reserve(1 + (FLAGS_duration + FLAGS_sampleMs - 1) / FLAGS_sampleMs);
443 } else {
444 samples.reserve(2 * FLAGS_duration);
445 }
csmartdalton4b5179b2016-09-19 11:03:58 -0700446 SkCanvas* canvas = surface->getCanvas();
447 canvas->translate(-skp->cullRect().x(), -skp->cullRect().y());
csmartdaltonc6618dd2016-10-05 08:42:03 -0700448 if (!FLAGS_gpuClock) {
Robert Phillips96601082018-05-29 16:13:26 -0400449 if (FLAGS_ddl) {
450 run_ddl_benchmark(testCtx->fenceSync(), ctx, canvas, skp.get(), &samples);
451 } else {
Robert Phillips9882dae2019-03-04 11:00:10 -0500452 run_benchmark(testCtx->fenceSync(), surface.get(), skp.get(), &samples);
Robert Phillips96601082018-05-29 16:13:26 -0400453 }
csmartdaltonc6618dd2016-10-05 08:42:03 -0700454 } else {
Robert Phillips96601082018-05-29 16:13:26 -0400455 if (FLAGS_ddl) {
456 exitf(ExitErr::kUnavailable, "DDL: GPU-only timing not supported");
457 }
csmartdaltonc6618dd2016-10-05 08:42:03 -0700458 if (!testCtx->gpuTimingSupport()) {
459 exitf(ExitErr::kUnavailable, "GPU does not support timing");
460 }
Robert Phillips9882dae2019-03-04 11:00:10 -0500461 run_gpu_time_benchmark(testCtx->gpuTimer(), testCtx->fenceSync(), surface.get(), skp.get(),
csmartdaltonc6618dd2016-10-05 08:42:03 -0700462 &samples);
463 }
Chris Daltona4f5ce02018-06-26 10:13:06 -0600464 print_result(samples, config->getTag().c_str(), srcname.c_str());
csmartdalton4b5179b2016-09-19 11:03:58 -0700465
466 // Save a proof (if one was requested).
467 if (!FLAGS_png.isEmpty()) {
468 SkBitmap bmp;
Mike Reed12e946b2017-04-17 10:53:29 -0400469 bmp.allocPixels(info);
470 if (!surface->getCanvas()->readPixels(bmp, 0, 0)) {
csmartdalton4b5179b2016-09-19 11:03:58 -0700471 exitf(ExitErr::kUnavailable, "failed to read canvas pixels for png");
472 }
Brian Osmane581aaa2018-08-09 09:46:53 -0400473 if (!mkdir_p(SkOSPath::Dirname(FLAGS_png[0]))) {
474 exitf(ExitErr::kIO, "failed to create directory for png \"%s\"", FLAGS_png[0]);
csmartdalton4b5179b2016-09-19 11:03:58 -0700475 }
Brian Osmane581aaa2018-08-09 09:46:53 -0400476 if (!sk_tool_utils::EncodeImageToFile(FLAGS_png[0], bmp, SkEncodedImageFormat::kPNG, 100)) {
csmartdalton4b5179b2016-09-19 11:03:58 -0700477 exitf(ExitErr::kIO, "failed to save png to \"%s\"", FLAGS_png[0]);
478 }
479 }
480
481 exit(0);
482}
483
Robert Phillips9882dae2019-03-04 11:00:10 -0500484static void draw_skp_and_flush(SkSurface* surface, const SkPicture* skp) {
485 auto canvas = surface->getCanvas();
csmartdalton4b5179b2016-09-19 11:03:58 -0700486 canvas->drawPicture(skp);
Robert Phillips9882dae2019-03-04 11:00:10 -0500487 surface->flush();
csmartdalton4b5179b2016-09-19 11:03:58 -0700488}
489
csmartdalton5772eaa2016-10-11 18:28:54 -0700490static sk_sp<SkPicture> create_warmup_skp() {
491 static constexpr SkRect bounds{0, 0, 500, 500};
492 SkPictureRecorder recorder;
493 SkCanvas* recording = recorder.beginRecording(bounds);
494
495 recording->clear(SK_ColorWHITE);
496
497 SkPaint stroke;
498 stroke.setStyle(SkPaint::kStroke_Style);
499 stroke.setStrokeWidth(2);
500
501 // Use a big path to (theoretically) warmup the CPU.
502 SkPath bigPath;
503 sk_tool_utils::make_big_path(bigPath);
504 recording->drawPath(bigPath, stroke);
505
506 // Use a perlin shader to warmup the GPU.
507 SkPaint perlin;
508 perlin.setShader(SkPerlinNoiseShader::MakeTurbulence(0.1f, 0.1f, 1, 0, nullptr));
509 recording->drawRect(bounds, perlin);
510
511 return recorder.finishRecordingAsPicture();
512}
513
Chris Daltona4f5ce02018-06-26 10:13:06 -0600514static sk_sp<SkPicture> create_skp_from_svg(SkStream* stream, const char* filename) {
515#ifdef SK_XML
516 SkDOM xml;
517 if (!xml.build(*stream)) {
518 exitf(ExitErr::kData, "failed to parse xml in file %s", filename);
519 }
520 sk_sp<SkSVGDOM> svg = SkSVGDOM::MakeFromDOM(xml);
521 if (!svg) {
522 exitf(ExitErr::kData, "failed to build svg dom from file %s", filename);
523 }
524
525 static constexpr SkRect bounds{0, 0, 1200, 1200};
526 SkPictureRecorder recorder;
527 SkCanvas* recording = recorder.beginRecording(bounds);
528
529 svg->setContainerSize(SkSize::Make(recording->getBaseLayerSize()));
530 svg->render(recording);
531
532 return recorder.finishRecordingAsPicture();
533#endif
534 exitf(ExitErr::kData, "SK_XML is disabled; cannot open svg file %s", filename);
Florin Malita5d3ff432018-07-31 16:38:43 -0400535 return nullptr;
Chris Daltona4f5ce02018-06-26 10:13:06 -0600536}
537
csmartdalton4b5179b2016-09-19 11:03:58 -0700538bool mkdir_p(const SkString& dirname) {
539 if (dirname.isEmpty()) {
540 return true;
541 }
542 return mkdir_p(SkOSPath::Dirname(dirname.c_str())) && sk_mkdir(dirname.c_str());
543}
544
Mike Klein88544fb2019-03-20 10:50:33 -0500545static SkString join(const CommandLineFlags::StringArray& stringArray) {
csmartdalton4b5179b2016-09-19 11:03:58 -0700546 SkString joined;
csmartdalton5772eaa2016-10-11 18:28:54 -0700547 for (int i = 0; i < stringArray.count(); ++i) {
548 joined.appendf(i ? " %s" : "%s", stringArray[i]);
csmartdalton4b5179b2016-09-19 11:03:58 -0700549 }
550 return joined;
551}
552
553static void exitf(ExitErr err, const char* format, ...) {
554 fprintf(stderr, ExitErr::kSoftware == err ? "INTERNAL ERROR: " : "ERROR: ");
555 va_list args;
556 va_start(args, format);
557 vfprintf(stderr, format, args);
558 va_end(args);
559 fprintf(stderr, ExitErr::kSoftware == err ? "; this should never happen.\n": ".\n");
560 exit((int)err);
561}
csmartdaltone0384892016-09-28 14:53:07 -0700562
csmartdaltonc6618dd2016-10-05 08:42:03 -0700563GpuSync::GpuSync(const sk_gpu_test::FenceSync* fenceSync)
csmartdaltone0384892016-09-28 14:53:07 -0700564 : fFenceSync(fenceSync) {
565 this->updateFence();
566}
567
568GpuSync::~GpuSync() {
569 fFenceSync->deleteFence(fFence);
570}
571
572void GpuSync::syncToPreviousFrame() {
csmartdaltonc6618dd2016-10-05 08:42:03 -0700573 if (sk_gpu_test::kInvalidFence == fFence) {
csmartdaltone0384892016-09-28 14:53:07 -0700574 exitf(ExitErr::kSoftware, "attempted to sync with invalid fence");
575 }
576 if (!fFenceSync->waitFence(fFence)) {
577 exitf(ExitErr::kUnavailable, "failed to wait for fence");
578 }
579 fFenceSync->deleteFence(fFence);
580 this->updateFence();
581}
582
583void GpuSync::updateFence() {
584 fFence = fFenceSync->insertFence();
csmartdaltonc6618dd2016-10-05 08:42:03 -0700585 if (sk_gpu_test::kInvalidFence == fFence) {
csmartdaltone0384892016-09-28 14:53:07 -0700586 exitf(ExitErr::kUnavailable, "failed to insert fence");
587 }
588}