blob: d63ba7e9a733d41b9061693f0ef8297060a071a9 [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
Robert Phillips96601082018-05-29 16:13:26 -04008#include "DDLPromiseImageHelper.h"
9#include "DDLTileHelper.h"
csmartdaltonc6618dd2016-10-05 08:42:03 -070010#include "GpuTimer.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040011#include "GrCaps.h"
csmartdalton4b5179b2016-09-19 11:03:58 -070012#include "GrContextFactory.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040013#include "GrContextPriv.h"
csmartdalton4b5179b2016-09-19 11:03:58 -070014#include "SkCanvas.h"
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060015#include "SkCommonFlags.h"
Chris Dalton040238b2017-12-18 14:22:34 -070016#include "SkCommonFlagsGpu.h"
Robert Phillips96601082018-05-29 16:13:26 -040017#include "SkDeferredDisplayList.h"
18#include "SkGraphics.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040019#include "SkGr.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"
csmartdalton4b5179b2016-09-19 11:03:58 -070029#include "flags/SkCommandLineFlags.h"
30#include "flags/SkCommonFlagsConfig.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
115static void draw_skp_and_flush(SkCanvas*, 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);
119static SkString join(const SkCommandLineFlags::StringArray&);
120static 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
csmartdaltonc6618dd2016-10-05 08:42:03 -0700196static void run_benchmark(const sk_gpu_test::FenceSync* fenceSync, SkCanvas* canvas,
197 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
Chris Daltona2b5b642018-06-24 13:08:57 -0600202 draw_skp_and_flush(canvas, 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) {
206 draw_skp_and_flush(canvas, skp); // draw N
207 // 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 {
220 draw_skp_and_flush(canvas, 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,
231 const sk_gpu_test::FenceSync* fenceSync, SkCanvas* canvas,
232 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
243 draw_skp_and_flush(canvas, skp);
244 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();
249 draw_skp_and_flush(canvas, skp);
250 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();
264 draw_skp_and_flush(canvas, skp);
265 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) {
326 SkCommandLineFlags::SetUsage("Use skpbench.py instead. "
327 "You usually don't want to use this program directly.");
328 SkCommandLineFlags::Parse(argc, argv);
329
330 if (!FLAGS_suppressHeader) {
331 printf("%s\n", header);
332 }
csmartdalton037adf32016-09-28 13:56:01 -0700333 if (FLAGS_duration <= 0) {
csmartdalton4b5179b2016-09-19 11:03:58 -0700334 exit(0); // This can be used to print the header and quit.
335 }
csmartdalton4b5179b2016-09-19 11:03:58 -0700336
337 // Parse the config.
338 const SkCommandLineConfigGpu* config = nullptr; // Initialize for spurious warning.
339 SkCommandLineConfigArray configs;
340 ParseConfigs(FLAGS_config, &configs);
341 if (configs.count() != 1 || !(config = configs[0]->asConfigGpu())) {
csmartdalton5772eaa2016-10-11 18:28:54 -0700342 exitf(ExitErr::kUsage, "invalid config '%s': must specify one (and only one) GPU config",
csmartdalton4b5179b2016-09-19 11:03:58 -0700343 join(FLAGS_config).c_str());
344 }
345
346 // Parse the skp.
Chris Daltona4f5ce02018-06-26 10:13:06 -0600347 if (FLAGS_src.count() != 1) {
348 exitf(ExitErr::kUsage,
349 "invalid input '%s': must specify a single .skp or .svg file, or 'warmup'",
350 join(FLAGS_src).c_str());
csmartdalton4b5179b2016-09-19 11:03:58 -0700351 }
Robert Phillips96601082018-05-29 16:13:26 -0400352
353 SkGraphics::Init();
Robert Phillips96601082018-05-29 16:13:26 -0400354
csmartdalton5772eaa2016-10-11 18:28:54 -0700355 sk_sp<SkPicture> skp;
Chris Daltona4f5ce02018-06-26 10:13:06 -0600356 SkString srcname;
357 if (0 == strcmp(FLAGS_src[0], "warmup")) {
csmartdalton5772eaa2016-10-11 18:28:54 -0700358 skp = create_warmup_skp();
Chris Daltona4f5ce02018-06-26 10:13:06 -0600359 srcname = "warmup";
csmartdalton5772eaa2016-10-11 18:28:54 -0700360 } else {
Chris Daltona4f5ce02018-06-26 10:13:06 -0600361 SkString srcfile(FLAGS_src[0]);
362 std::unique_ptr<SkStream> srcstream(SkStream::MakeFromFile(srcfile.c_str()));
363 if (!srcstream) {
364 exitf(ExitErr::kIO, "failed to open file %s", srcfile.c_str());
csmartdalton5772eaa2016-10-11 18:28:54 -0700365 }
Chris Daltona4f5ce02018-06-26 10:13:06 -0600366 if (srcfile.endsWith(".svg")) {
367 skp = create_skp_from_svg(srcstream.get(), srcfile.c_str());
368 } else {
369 skp = SkPicture::MakeFromStream(srcstream.get());
370 }
csmartdalton5772eaa2016-10-11 18:28:54 -0700371 if (!skp) {
Chris Daltona4f5ce02018-06-26 10:13:06 -0600372 exitf(ExitErr::kData, "failed to parse file %s", srcfile.c_str());
csmartdalton5772eaa2016-10-11 18:28:54 -0700373 }
Chris Daltona4f5ce02018-06-26 10:13:06 -0600374 srcname = SkOSPath::Basename(srcfile.c_str());
csmartdalton4b5179b2016-09-19 11:03:58 -0700375 }
376 int width = SkTMin(SkScalarCeilToInt(skp->cullRect().width()), 2048),
377 height = SkTMin(SkScalarCeilToInt(skp->cullRect().height()), 2048);
csmartdaltond7a9db62016-09-22 05:10:02 -0700378 if (FLAGS_verbosity >= 3 &&
csmartdalton4b5179b2016-09-19 11:03:58 -0700379 (width != skp->cullRect().width() || height != skp->cullRect().height())) {
csmartdaltond7a9db62016-09-22 05:10:02 -0700380 fprintf(stderr, "%s is too large (%ix%i), cropping to %ix%i.\n",
Chris Daltona4f5ce02018-06-26 10:13:06 -0600381 srcname.c_str(), SkScalarCeilToInt(skp->cullRect().width()),
csmartdalton4b5179b2016-09-19 11:03:58 -0700382 SkScalarCeilToInt(skp->cullRect().height()), width, height);
383 }
384
Brian Salomonf865b052018-03-09 09:01:53 -0500385 if (config->getSurfType() != SkCommandLineConfigGpu::SurfType::kDefault) {
386 exitf(ExitErr::kUnavailable, "This tool only supports the default surface type. (%s)",
387 config->getTag().c_str());
388 }
389
csmartdalton4b5179b2016-09-19 11:03:58 -0700390 // Create a context.
csmartdalton008b9d82017-02-22 12:00:42 -0700391 GrContextOptions ctxOptions;
Chris Dalton040238b2017-12-18 14:22:34 -0700392 SetCtxOptionsFromCommonFlags(&ctxOptions);
csmartdalton008b9d82017-02-22 12:00:42 -0700393 sk_gpu_test::GrContextFactory factory(ctxOptions);
csmartdalton4b5179b2016-09-19 11:03:58 -0700394 sk_gpu_test::ContextInfo ctxInfo =
csmartdaltone812d492017-02-21 12:36:05 -0700395 factory.getContextInfo(config->getContextType(), config->getContextOverrides());
csmartdalton4b5179b2016-09-19 11:03:58 -0700396 GrContext* ctx = ctxInfo.grContext();
397 if (!ctx) {
398 exitf(ExitErr::kUnavailable, "failed to create context for config %s",
399 config->getTag().c_str());
400 }
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400401 if (ctx->maxRenderTargetSize() < SkTMax(width, height)) {
csmartdalton4b5179b2016-09-19 11:03:58 -0700402 exitf(ExitErr::kUnavailable, "render target size %ix%i not supported by platform (max: %i)",
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400403 width, height, ctx->maxRenderTargetSize());
csmartdalton4b5179b2016-09-19 11:03:58 -0700404 }
Brian Osman2b23c4b2018-06-01 12:25:08 -0400405 GrPixelConfig grPixConfig = SkColorType2GrPixelConfig(config->getColorType());
Greg Daniel0a7aa142018-02-21 13:02:32 -0500406 if (kUnknown_GrPixelConfig == grPixConfig) {
407 exitf(ExitErr::kUnavailable, "failed to get GrPixelConfig from SkColorType: %d",
408 config->getColorType());
409 }
Robert Phillips9da87e02019-02-04 13:26:26 -0500410 int supportedSampleCount = ctx->priv().caps()->getRenderTargetSampleCount(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400411 config->getSamples(), grPixConfig);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400412 if (supportedSampleCount != config->getSamples()) {
413 exitf(ExitErr::kUnavailable, "sample count %i not supported by platform",
414 config->getSamples());
csmartdalton4b5179b2016-09-19 11:03:58 -0700415 }
416 sk_gpu_test::TestContext* testCtx = ctxInfo.testContext();
417 if (!testCtx) {
418 exitf(ExitErr::kSoftware, "testContext is null");
419 }
420 if (!testCtx->fenceSyncSupport()) {
421 exitf(ExitErr::kUnavailable, "GPU does not support fence sync");
422 }
423
424 // Create a render target.
Brian Salomonce5ee602017-07-17 11:31:31 -0400425 SkImageInfo info =
426 SkImageInfo::Make(width, height, config->getColorType(), config->getAlphaType(),
427 sk_ref_sp(config->getColorSpace()));
csmartdalton4b5179b2016-09-19 11:03:58 -0700428 uint32_t flags = config->getUseDIText() ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag : 0;
429 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
430 sk_sp<SkSurface> surface =
431 SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, config->getSamples(), &props);
432 if (!surface) {
433 exitf(ExitErr::kUnavailable, "failed to create %ix%i render target for config %s",
434 width, height, config->getTag().c_str());
435 }
436
csmartdalton5772eaa2016-10-11 18:28:54 -0700437 // Run the benchmark.
csmartdalton4b5179b2016-09-19 11:03:58 -0700438 std::vector<Sample> samples;
csmartdalton037adf32016-09-28 13:56:01 -0700439 if (FLAGS_sampleMs > 0) {
440 // +1 because we might take one more sample in order to have an odd number.
441 samples.reserve(1 + (FLAGS_duration + FLAGS_sampleMs - 1) / FLAGS_sampleMs);
442 } else {
443 samples.reserve(2 * FLAGS_duration);
444 }
csmartdalton4b5179b2016-09-19 11:03:58 -0700445 SkCanvas* canvas = surface->getCanvas();
446 canvas->translate(-skp->cullRect().x(), -skp->cullRect().y());
csmartdaltonc6618dd2016-10-05 08:42:03 -0700447 if (!FLAGS_gpuClock) {
Robert Phillips96601082018-05-29 16:13:26 -0400448 if (FLAGS_ddl) {
449 run_ddl_benchmark(testCtx->fenceSync(), ctx, canvas, skp.get(), &samples);
450 } else {
451 run_benchmark(testCtx->fenceSync(), canvas, skp.get(), &samples);
452 }
csmartdaltonc6618dd2016-10-05 08:42:03 -0700453 } else {
Robert Phillips96601082018-05-29 16:13:26 -0400454 if (FLAGS_ddl) {
455 exitf(ExitErr::kUnavailable, "DDL: GPU-only timing not supported");
456 }
csmartdaltonc6618dd2016-10-05 08:42:03 -0700457 if (!testCtx->gpuTimingSupport()) {
458 exitf(ExitErr::kUnavailable, "GPU does not support timing");
459 }
460 run_gpu_time_benchmark(testCtx->gpuTimer(), testCtx->fenceSync(), canvas, skp.get(),
461 &samples);
462 }
Chris Daltona4f5ce02018-06-26 10:13:06 -0600463 print_result(samples, config->getTag().c_str(), srcname.c_str());
csmartdalton4b5179b2016-09-19 11:03:58 -0700464
465 // Save a proof (if one was requested).
466 if (!FLAGS_png.isEmpty()) {
467 SkBitmap bmp;
Mike Reed12e946b2017-04-17 10:53:29 -0400468 bmp.allocPixels(info);
469 if (!surface->getCanvas()->readPixels(bmp, 0, 0)) {
csmartdalton4b5179b2016-09-19 11:03:58 -0700470 exitf(ExitErr::kUnavailable, "failed to read canvas pixels for png");
471 }
Brian Osmane581aaa2018-08-09 09:46:53 -0400472 if (!mkdir_p(SkOSPath::Dirname(FLAGS_png[0]))) {
473 exitf(ExitErr::kIO, "failed to create directory for png \"%s\"", FLAGS_png[0]);
csmartdalton4b5179b2016-09-19 11:03:58 -0700474 }
Brian Osmane581aaa2018-08-09 09:46:53 -0400475 if (!sk_tool_utils::EncodeImageToFile(FLAGS_png[0], bmp, SkEncodedImageFormat::kPNG, 100)) {
csmartdalton4b5179b2016-09-19 11:03:58 -0700476 exitf(ExitErr::kIO, "failed to save png to \"%s\"", FLAGS_png[0]);
477 }
478 }
479
480 exit(0);
481}
482
483static void draw_skp_and_flush(SkCanvas* canvas, const SkPicture* skp) {
484 canvas->drawPicture(skp);
485 canvas->flush();
486}
487
csmartdalton5772eaa2016-10-11 18:28:54 -0700488static sk_sp<SkPicture> create_warmup_skp() {
489 static constexpr SkRect bounds{0, 0, 500, 500};
490 SkPictureRecorder recorder;
491 SkCanvas* recording = recorder.beginRecording(bounds);
492
493 recording->clear(SK_ColorWHITE);
494
495 SkPaint stroke;
496 stroke.setStyle(SkPaint::kStroke_Style);
497 stroke.setStrokeWidth(2);
498
499 // Use a big path to (theoretically) warmup the CPU.
500 SkPath bigPath;
501 sk_tool_utils::make_big_path(bigPath);
502 recording->drawPath(bigPath, stroke);
503
504 // Use a perlin shader to warmup the GPU.
505 SkPaint perlin;
506 perlin.setShader(SkPerlinNoiseShader::MakeTurbulence(0.1f, 0.1f, 1, 0, nullptr));
507 recording->drawRect(bounds, perlin);
508
509 return recorder.finishRecordingAsPicture();
510}
511
Chris Daltona4f5ce02018-06-26 10:13:06 -0600512static sk_sp<SkPicture> create_skp_from_svg(SkStream* stream, const char* filename) {
513#ifdef SK_XML
514 SkDOM xml;
515 if (!xml.build(*stream)) {
516 exitf(ExitErr::kData, "failed to parse xml in file %s", filename);
517 }
518 sk_sp<SkSVGDOM> svg = SkSVGDOM::MakeFromDOM(xml);
519 if (!svg) {
520 exitf(ExitErr::kData, "failed to build svg dom from file %s", filename);
521 }
522
523 static constexpr SkRect bounds{0, 0, 1200, 1200};
524 SkPictureRecorder recorder;
525 SkCanvas* recording = recorder.beginRecording(bounds);
526
527 svg->setContainerSize(SkSize::Make(recording->getBaseLayerSize()));
528 svg->render(recording);
529
530 return recorder.finishRecordingAsPicture();
531#endif
532 exitf(ExitErr::kData, "SK_XML is disabled; cannot open svg file %s", filename);
Florin Malita5d3ff432018-07-31 16:38:43 -0400533 return nullptr;
Chris Daltona4f5ce02018-06-26 10:13:06 -0600534}
535
csmartdalton4b5179b2016-09-19 11:03:58 -0700536bool mkdir_p(const SkString& dirname) {
537 if (dirname.isEmpty()) {
538 return true;
539 }
540 return mkdir_p(SkOSPath::Dirname(dirname.c_str())) && sk_mkdir(dirname.c_str());
541}
542
543static SkString join(const SkCommandLineFlags::StringArray& stringArray) {
544 SkString joined;
csmartdalton5772eaa2016-10-11 18:28:54 -0700545 for (int i = 0; i < stringArray.count(); ++i) {
546 joined.appendf(i ? " %s" : "%s", stringArray[i]);
csmartdalton4b5179b2016-09-19 11:03:58 -0700547 }
548 return joined;
549}
550
551static void exitf(ExitErr err, const char* format, ...) {
552 fprintf(stderr, ExitErr::kSoftware == err ? "INTERNAL ERROR: " : "ERROR: ");
553 va_list args;
554 va_start(args, format);
555 vfprintf(stderr, format, args);
556 va_end(args);
557 fprintf(stderr, ExitErr::kSoftware == err ? "; this should never happen.\n": ".\n");
558 exit((int)err);
559}
csmartdaltone0384892016-09-28 14:53:07 -0700560
csmartdaltonc6618dd2016-10-05 08:42:03 -0700561GpuSync::GpuSync(const sk_gpu_test::FenceSync* fenceSync)
csmartdaltone0384892016-09-28 14:53:07 -0700562 : fFenceSync(fenceSync) {
563 this->updateFence();
564}
565
566GpuSync::~GpuSync() {
567 fFenceSync->deleteFence(fFence);
568}
569
570void GpuSync::syncToPreviousFrame() {
csmartdaltonc6618dd2016-10-05 08:42:03 -0700571 if (sk_gpu_test::kInvalidFence == fFence) {
csmartdaltone0384892016-09-28 14:53:07 -0700572 exitf(ExitErr::kSoftware, "attempted to sync with invalid fence");
573 }
574 if (!fFenceSync->waitFence(fFence)) {
575 exitf(ExitErr::kUnavailable, "failed to wait for fence");
576 }
577 fFenceSync->deleteFence(fFence);
578 this->updateFence();
579}
580
581void GpuSync::updateFence() {
582 fFence = fFenceSync->insertFence();
csmartdaltonc6618dd2016-10-05 08:42:03 -0700583 if (sk_gpu_test::kInvalidFence == fFence) {
csmartdaltone0384892016-09-28 14:53:07 -0700584 exitf(ExitErr::kUnavailable, "failed to insert fence");
585 }
586}