blob: 25acb1c9f1f7577b609d26dd17f16861a41bab63 [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
Brian Salomonc7fe0f72018-05-11 10:14:21 -04008#include <stdlib.h>
9#include <algorithm>
10#include <array>
11#include <chrono>
12#include <cmath>
13#include <vector>
Robert Phillips96601082018-05-29 16:13:26 -040014#include "DDLPromiseImageHelper.h"
15#include "DDLTileHelper.h"
csmartdaltonc6618dd2016-10-05 08:42:03 -070016#include "GpuTimer.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040017#include "GrCaps.h"
csmartdalton4b5179b2016-09-19 11:03:58 -070018#include "GrContextFactory.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040019#include "GrContextPriv.h"
csmartdalton4b5179b2016-09-19 11:03:58 -070020#include "SkCanvas.h"
Chris Dalton7a0ebfc2017-10-13 12:35:50 -060021#include "SkCommonFlags.h"
Chris Dalton040238b2017-12-18 14:22:34 -070022#include "SkCommonFlagsGpu.h"
Robert Phillips96601082018-05-29 16:13:26 -040023#include "SkDeferredDisplayList.h"
24#include "SkGraphics.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040025#include "SkGr.h"
csmartdalton4b5179b2016-09-19 11:03:58 -070026#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050027#include "SkOSPath.h"
csmartdalton5772eaa2016-10-11 18:28:54 -070028#include "SkPerlinNoiseShader.h"
csmartdalton4b5179b2016-09-19 11:03:58 -070029#include "SkPicture.h"
csmartdalton5772eaa2016-10-11 18:28:54 -070030#include "SkPictureRecorder.h"
csmartdalton4b5179b2016-09-19 11:03:58 -070031#include "SkStream.h"
32#include "SkSurface.h"
33#include "SkSurfaceProps.h"
Robert Phillips96601082018-05-29 16:13:26 -040034#include "SkTaskGroup.h"
csmartdalton4b5179b2016-09-19 11:03:58 -070035#include "flags/SkCommandLineFlags.h"
36#include "flags/SkCommonFlagsConfig.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040037#include "picture_utils.h"
38#include "sk_tool_utils.h"
csmartdalton4b5179b2016-09-19 11:03:58 -070039
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 *
csmartdalton4b5179b2016-09-19 11:03:58 -070048 * Currently, only GPU configs are supported.
49 */
50
Robert Phillips96601082018-05-29 16:13:26 -040051DEFINE_bool(ddl, false, "record the skp into DDLs before rendering");
52DEFINE_int32(ddlNumAdditionalThreads, 0, "number of DDL recording threads in addition to main one");
53DEFINE_int32(ddlTilingWidthHeight, 0, "number of tiles along one edge when in DDL mode");
Robert Phillips65eb4fb2018-05-31 13:27:52 -040054DEFINE_bool(ddlRecordTime, false, "report just the cpu time spent recording DDLs");
Robert Phillips96601082018-05-29 16:13:26 -040055
csmartdalton037adf32016-09-28 13:56:01 -070056DEFINE_int32(duration, 5000, "number of milliseconds to run the benchmark");
57DEFINE_int32(sampleMs, 50, "minimum duration of a sample");
csmartdaltonc6618dd2016-10-05 08:42:03 -070058DEFINE_bool(gpuClock, false, "time on the gpu clock (gpu work only)");
csmartdalton4b5179b2016-09-19 11:03:58 -070059DEFINE_bool(fps, false, "use fps instead of ms");
csmartdalton5772eaa2016-10-11 18:28:54 -070060DEFINE_string(skp, "", "path to a single .skp file, or 'warmup' for a builtin warmup run");
csmartdalton4b5179b2016-09-19 11:03:58 -070061DEFINE_string(png, "", "if set, save a .png proof to disk at this file location");
62DEFINE_int32(verbosity, 4, "level of verbosity (0=none to 5=debug)");
63DEFINE_bool(suppressHeader, false, "don't print a header row before the results");
64
65static const char* header =
csmartdaltonc6618dd2016-10-05 08:42:03 -070066" accum median max min stddev samples sample_ms clock metric config bench";
csmartdalton4b5179b2016-09-19 11:03:58 -070067
68static const char* resultFormat =
csmartdaltonc6618dd2016-10-05 08:42:03 -070069"%8.4g %8.4g %8.4g %8.4g %6.3g%% %7li %9i %-5s %-6s %-9s %s";
csmartdalton4b5179b2016-09-19 11:03:58 -070070
71struct Sample {
csmartdaltonc6618dd2016-10-05 08:42:03 -070072 using duration = std::chrono::nanoseconds;
csmartdalton4b5179b2016-09-19 11:03:58 -070073
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
csmartdaltonc6618dd2016-10-05 08:42:03 -070080 int fFrames;
81 duration fDuration;
csmartdalton4b5179b2016-09-19 11:03:58 -070082};
83
csmartdaltone0384892016-09-28 14:53:07 -070084class GpuSync {
85public:
csmartdaltonc6618dd2016-10-05 08:42:03 -070086 GpuSync(const sk_gpu_test::FenceSync* fenceSync);
csmartdaltone0384892016-09-28 14:53:07 -070087 ~GpuSync();
88
89 void syncToPreviousFrame();
90
91private:
92 void updateFence();
93
csmartdaltonc6618dd2016-10-05 08:42:03 -070094 const sk_gpu_test::FenceSync* const fFenceSync;
95 sk_gpu_test::PlatformFence fFence;
csmartdaltone0384892016-09-28 14:53:07 -070096};
97
csmartdalton4b5179b2016-09-19 11:03:58 -070098enum class ExitErr {
99 kOk = 0,
100 kUsage = 64,
101 kData = 65,
102 kUnavailable = 69,
103 kIO = 74,
104 kSoftware = 70
105};
106
107static void draw_skp_and_flush(SkCanvas*, const SkPicture*);
csmartdalton5772eaa2016-10-11 18:28:54 -0700108static sk_sp<SkPicture> create_warmup_skp();
csmartdalton4b5179b2016-09-19 11:03:58 -0700109static bool mkdir_p(const SkString& name);
110static SkString join(const SkCommandLineFlags::StringArray&);
111static void exitf(ExitErr, const char* format, ...);
112
Robert Phillips96601082018-05-29 16:13:26 -0400113static 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 Phillips65eb4fb2018-05-31 13:27:52 -0400121 if (!FLAGS_ddlRecordTime) {
122 tiles->drawAllTilesAndFlush(context, true);
123 if (gpuSync) {
124 gpuSync->syncToPreviousFrame();
125 }
Robert Phillips96601082018-05-29 16:13:26 -0400126 }
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
139static 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 Phillipsf7dcdb02018-06-21 11:18:25 -0400160 SkTaskGroup::Enabler enabled(FLAGS_ddlNumAdditionalThreads);
161
Robert Phillips96601082018-05-29 16:13:26 -0400162 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
csmartdaltonc6618dd2016-10-05 08:42:03 -0700187static 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);
csmartdalton037adf32016-09-28 13:56:01 -0700191 const clock::duration benchDuration = std::chrono::milliseconds(FLAGS_duration);
csmartdalton4b5179b2016-09-19 11:03:58 -0700192
Robert Phillips96601082018-05-29 16:13:26 -0400193 draw_skp_and_flush(canvas, skp); // draw1
csmartdaltone0384892016-09-28 14:53:07 -0700194 GpuSync gpuSync(fenceSync);
csmartdalton4b5179b2016-09-19 11:03:58 -0700195
Robert Phillips96601082018-05-29 16:13:26 -0400196 draw_skp_and_flush(canvas, skp); // draw2
197 gpuSync.syncToPreviousFrame(); // waits for draw1 to finish (after draw2's cpu work is done).
csmartdalton4b5179b2016-09-19 11:03:58 -0700198
csmartdalton037adf32016-09-28 13:56:01 -0700199 clock::time_point now = clock::now();
200 const clock::time_point endTime = now + benchDuration;
csmartdalton4b5179b2016-09-19 11:03:58 -0700201
csmartdalton037adf32016-09-28 13:56:01 -0700202 do {
203 clock::time_point sampleStart = now;
204 samples->emplace_back();
205 Sample& sample = samples->back();
206
csmartdalton4b5179b2016-09-19 11:03:58 -0700207 do {
208 draw_skp_and_flush(canvas, skp);
csmartdaltone0384892016-09-28 14:53:07 -0700209 gpuSync.syncToPreviousFrame();
csmartdalton4b5179b2016-09-19 11:03:58 -0700210
csmartdalton037adf32016-09-28 13:56:01 -0700211 now = clock::now();
212 sample.fDuration = now - sampleStart;
csmartdalton4b5179b2016-09-19 11:03:58 -0700213 ++sample.fFrames;
csmartdalton037adf32016-09-28 13:56:01 -0700214 } while (sample.fDuration < sampleDuration);
215 } while (now < endTime || 0 == samples->size() % 2);
csmartdalton4b5179b2016-09-19 11:03:58 -0700216}
217
csmartdaltonc6618dd2016-10-05 08:42:03 -0700218static 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
csmartdalton4b5179b2016-09-19 11:03:58 -0700278void 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());
csmartdalton4b5179b2016-09-19 11:03:58 -0700292
csmartdalton6904b192016-09-29 06:23:23 -0700293 const double accumValue = accum.value();
csmartdalton4b5179b2016-09-19 11:03:58 -0700294 double variance = 0;
csmartdalton037adf32016-09-28 13:56:01 -0700295 for (double value : values) {
296 const double delta = value - accumValue;
csmartdalton4b5179b2016-09-19 11:03:58 -0700297 variance += delta * delta;
298 }
csmartdalton037adf32016-09-28 13:56:01 -0700299 variance /= values.size();
csmartdalton4b5179b2016-09-19 11:03:58 -0700300 // Technically, this is the relative standard deviation.
csmartdalton037adf32016-09-28 13:56:01 -0700301 const double stddev = 100/*%*/ * sqrt(variance) / accumValue;
csmartdalton4b5179b2016-09-19 11:03:58 -0700302
csmartdalton6904b192016-09-29 06:23:23 -0700303 printf(resultFormat, accumValue, values[values.size() / 2], values.back(), values.front(),
csmartdaltonc6618dd2016-10-05 08:42:03 -0700304 stddev, values.size(), FLAGS_sampleMs, FLAGS_gpuClock ? "gpu" : "cpu", Sample::metric(),
305 config, bench);
csmartdalton4b5179b2016-09-19 11:03:58 -0700306 printf("\n");
307 fflush(stdout);
308}
309
310int 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 }
csmartdalton037adf32016-09-28 13:56:01 -0700318 if (FLAGS_duration <= 0) {
csmartdalton4b5179b2016-09-19 11:03:58 -0700319 exit(0); // This can be used to print the header and quit.
320 }
csmartdalton4b5179b2016-09-19 11:03:58 -0700321
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())) {
csmartdalton5772eaa2016-10-11 18:28:54 -0700327 exitf(ExitErr::kUsage, "invalid config '%s': must specify one (and only one) GPU config",
csmartdalton4b5179b2016-09-19 11:03:58 -0700328 join(FLAGS_config).c_str());
329 }
330
331 // Parse the skp.
332 if (FLAGS_skp.count() != 1) {
csmartdalton5772eaa2016-10-11 18:28:54 -0700333 exitf(ExitErr::kUsage, "invalid skp '%s': must specify a single skp file, or 'warmup'",
csmartdalton4b5179b2016-09-19 11:03:58 -0700334 join(FLAGS_skp).c_str());
335 }
Robert Phillips96601082018-05-29 16:13:26 -0400336
337 SkGraphics::Init();
Robert Phillips96601082018-05-29 16:13:26 -0400338
csmartdalton5772eaa2016-10-11 18:28:54 -0700339 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);
csmartdalton4b5179b2016-09-19 11:03:58 -0700355 }
356 int width = SkTMin(SkScalarCeilToInt(skp->cullRect().width()), 2048),
357 height = SkTMin(SkScalarCeilToInt(skp->cullRect().height()), 2048);
csmartdaltond7a9db62016-09-22 05:10:02 -0700358 if (FLAGS_verbosity >= 3 &&
csmartdalton4b5179b2016-09-19 11:03:58 -0700359 (width != skp->cullRect().width() || height != skp->cullRect().height())) {
csmartdaltond7a9db62016-09-22 05:10:02 -0700360 fprintf(stderr, "%s is too large (%ix%i), cropping to %ix%i.\n",
csmartdalton5772eaa2016-10-11 18:28:54 -0700361 skpname.c_str(), SkScalarCeilToInt(skp->cullRect().width()),
csmartdalton4b5179b2016-09-19 11:03:58 -0700362 SkScalarCeilToInt(skp->cullRect().height()), width, height);
363 }
364
Brian Salomonf865b052018-03-09 09:01:53 -0500365 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
csmartdalton4b5179b2016-09-19 11:03:58 -0700370 // Create a context.
csmartdalton008b9d82017-02-22 12:00:42 -0700371 GrContextOptions ctxOptions;
Chris Dalton040238b2017-12-18 14:22:34 -0700372 SetCtxOptionsFromCommonFlags(&ctxOptions);
csmartdalton008b9d82017-02-22 12:00:42 -0700373 sk_gpu_test::GrContextFactory factory(ctxOptions);
csmartdalton4b5179b2016-09-19 11:03:58 -0700374 sk_gpu_test::ContextInfo ctxInfo =
csmartdaltone812d492017-02-21 12:36:05 -0700375 factory.getContextInfo(config->getContextType(), config->getContextOverrides());
csmartdalton4b5179b2016-09-19 11:03:58 -0700376 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 Salomonc7fe0f72018-05-11 10:14:21 -0400381 if (ctx->maxRenderTargetSize() < SkTMax(width, height)) {
csmartdalton4b5179b2016-09-19 11:03:58 -0700382 exitf(ExitErr::kUnavailable, "render target size %ix%i not supported by platform (max: %i)",
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400383 width, height, ctx->maxRenderTargetSize());
csmartdalton4b5179b2016-09-19 11:03:58 -0700384 }
Brian Osman2b23c4b2018-06-01 12:25:08 -0400385 GrPixelConfig grPixConfig = SkColorType2GrPixelConfig(config->getColorType());
Greg Daniel0a7aa142018-02-21 13:02:32 -0500386 if (kUnknown_GrPixelConfig == grPixConfig) {
387 exitf(ExitErr::kUnavailable, "failed to get GrPixelConfig from SkColorType: %d",
388 config->getColorType());
389 }
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400390 int supportedSampleCount = ctx->contextPriv().caps()->getRenderTargetSampleCount(
391 config->getSamples(), grPixConfig);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400392 if (supportedSampleCount != config->getSamples()) {
393 exitf(ExitErr::kUnavailable, "sample count %i not supported by platform",
394 config->getSamples());
csmartdalton4b5179b2016-09-19 11:03:58 -0700395 }
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 Salomonce5ee602017-07-17 11:31:31 -0400405 SkImageInfo info =
406 SkImageInfo::Make(width, height, config->getColorType(), config->getAlphaType(),
407 sk_ref_sp(config->getColorSpace()));
csmartdalton4b5179b2016-09-19 11:03:58 -0700408 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
csmartdalton5772eaa2016-10-11 18:28:54 -0700417 // Run the benchmark.
csmartdalton4b5179b2016-09-19 11:03:58 -0700418 std::vector<Sample> samples;
csmartdalton037adf32016-09-28 13:56:01 -0700419 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 }
csmartdalton4b5179b2016-09-19 11:03:58 -0700425 SkCanvas* canvas = surface->getCanvas();
426 canvas->translate(-skp->cullRect().x(), -skp->cullRect().y());
csmartdaltonc6618dd2016-10-05 08:42:03 -0700427 if (!FLAGS_gpuClock) {
Robert Phillips96601082018-05-29 16:13:26 -0400428 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 }
csmartdaltonc6618dd2016-10-05 08:42:03 -0700433 } else {
Robert Phillips96601082018-05-29 16:13:26 -0400434 if (FLAGS_ddl) {
435 exitf(ExitErr::kUnavailable, "DDL: GPU-only timing not supported");
436 }
csmartdaltonc6618dd2016-10-05 08:42:03 -0700437 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 }
csmartdalton5772eaa2016-10-11 18:28:54 -0700443 print_result(samples, config->getTag().c_str(), skpname.c_str());
csmartdalton4b5179b2016-09-19 11:03:58 -0700444
445 // Save a proof (if one was requested).
446 if (!FLAGS_png.isEmpty()) {
447 SkBitmap bmp;
Mike Reed12e946b2017-04-17 10:53:29 -0400448 bmp.allocPixels(info);
449 if (!surface->getCanvas()->readPixels(bmp, 0, 0)) {
csmartdalton4b5179b2016-09-19 11:03:58 -0700450 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
465static void draw_skp_and_flush(SkCanvas* canvas, const SkPicture* skp) {
466 canvas->drawPicture(skp);
467 canvas->flush();
468}
469
csmartdalton5772eaa2016-10-11 18:28:54 -0700470static 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
csmartdalton4b5179b2016-09-19 11:03:58 -0700494bool 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
501static SkString join(const SkCommandLineFlags::StringArray& stringArray) {
502 SkString joined;
csmartdalton5772eaa2016-10-11 18:28:54 -0700503 for (int i = 0; i < stringArray.count(); ++i) {
504 joined.appendf(i ? " %s" : "%s", stringArray[i]);
csmartdalton4b5179b2016-09-19 11:03:58 -0700505 }
506 return joined;
507}
508
509static 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}
csmartdaltone0384892016-09-28 14:53:07 -0700518
csmartdaltonc6618dd2016-10-05 08:42:03 -0700519GpuSync::GpuSync(const sk_gpu_test::FenceSync* fenceSync)
csmartdaltone0384892016-09-28 14:53:07 -0700520 : fFenceSync(fenceSync) {
521 this->updateFence();
522}
523
524GpuSync::~GpuSync() {
525 fFenceSync->deleteFence(fFence);
526}
527
528void GpuSync::syncToPreviousFrame() {
csmartdaltonc6618dd2016-10-05 08:42:03 -0700529 if (sk_gpu_test::kInvalidFence == fFence) {
csmartdaltone0384892016-09-28 14:53:07 -0700530 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
539void GpuSync::updateFence() {
540 fFence = fFenceSync->insertFence();
csmartdaltonc6618dd2016-10-05 08:42:03 -0700541 if (sk_gpu_test::kInvalidFence == fFence) {
csmartdaltone0384892016-09-28 14:53:07 -0700542 exitf(ExitErr::kUnavailable, "failed to insert fence");
543 }
544}