blob: 399bf6cdb26b26c1b5a7c0fc24e2477ead1a8673 [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
Chris Daltona2b5b642018-06-24 13:08:57 -060071static constexpr int kNumFlushesToPrimeCache = 3;
72
csmartdalton4b5179b2016-09-19 11:03:58 -070073struct Sample {
csmartdaltonc6618dd2016-10-05 08:42:03 -070074 using duration = std::chrono::nanoseconds;
csmartdalton4b5179b2016-09-19 11:03:58 -070075
76 Sample() : fFrames(0), fDuration(0) {}
77 double seconds() const { return std::chrono::duration<double>(fDuration).count(); }
78 double ms() const { return std::chrono::duration<double, std::milli>(fDuration).count(); }
79 double value() const { return FLAGS_fps ? fFrames / this->seconds() : this->ms() / fFrames; }
80 static const char* metric() { return FLAGS_fps ? "fps" : "ms"; }
81
csmartdaltonc6618dd2016-10-05 08:42:03 -070082 int fFrames;
83 duration fDuration;
csmartdalton4b5179b2016-09-19 11:03:58 -070084};
85
csmartdaltone0384892016-09-28 14:53:07 -070086class GpuSync {
87public:
csmartdaltonc6618dd2016-10-05 08:42:03 -070088 GpuSync(const sk_gpu_test::FenceSync* fenceSync);
csmartdaltone0384892016-09-28 14:53:07 -070089 ~GpuSync();
90
91 void syncToPreviousFrame();
92
93private:
94 void updateFence();
95
csmartdaltonc6618dd2016-10-05 08:42:03 -070096 const sk_gpu_test::FenceSync* const fFenceSync;
97 sk_gpu_test::PlatformFence fFence;
csmartdaltone0384892016-09-28 14:53:07 -070098};
99
csmartdalton4b5179b2016-09-19 11:03:58 -0700100enum class ExitErr {
101 kOk = 0,
102 kUsage = 64,
103 kData = 65,
104 kUnavailable = 69,
105 kIO = 74,
106 kSoftware = 70
107};
108
109static void draw_skp_and_flush(SkCanvas*, const SkPicture*);
csmartdalton5772eaa2016-10-11 18:28:54 -0700110static sk_sp<SkPicture> create_warmup_skp();
csmartdalton4b5179b2016-09-19 11:03:58 -0700111static bool mkdir_p(const SkString& name);
112static SkString join(const SkCommandLineFlags::StringArray&);
113static void exitf(ExitErr, const char* format, ...);
114
Robert Phillips96601082018-05-29 16:13:26 -0400115static void ddl_sample(GrContext* context, DDLTileHelper* tiles, GpuSync* gpuSync, Sample* sample,
116 std::chrono::high_resolution_clock::time_point* startStopTime) {
117 using clock = std::chrono::high_resolution_clock;
118
119 clock::time_point start = *startStopTime;
120
121 tiles->createDDLsInParallel();
122
Robert Phillips65eb4fb2018-05-31 13:27:52 -0400123 if (!FLAGS_ddlRecordTime) {
124 tiles->drawAllTilesAndFlush(context, true);
125 if (gpuSync) {
126 gpuSync->syncToPreviousFrame();
127 }
Robert Phillips96601082018-05-29 16:13:26 -0400128 }
129
130 *startStopTime = clock::now();
131
132 tiles->resetAllTiles();
133
134 if (sample) {
135 SkASSERT(gpuSync);
136 sample->fDuration += *startStopTime - start;
137 sample->fFrames++;
138 }
139}
140
141static void run_ddl_benchmark(const sk_gpu_test::FenceSync* fenceSync,
142 GrContext* context, SkCanvas* finalCanvas,
143 SkPicture* inputPicture, std::vector<Sample>* samples) {
144 using clock = std::chrono::high_resolution_clock;
145 const Sample::duration sampleDuration = std::chrono::milliseconds(FLAGS_sampleMs);
146 const clock::duration benchDuration = std::chrono::milliseconds(FLAGS_duration);
147
148 SkIRect viewport = finalCanvas->imageInfo().bounds();
149
150 DDLPromiseImageHelper promiseImageHelper;
151 sk_sp<SkData> compressedPictureData = promiseImageHelper.deflateSKP(inputPicture);
152 if (!compressedPictureData) {
153 exitf(ExitErr::kUnavailable, "DDL: conversion of skp failed");
154 }
155
156 promiseImageHelper.uploadAllToGPU(context);
157
158 DDLTileHelper tiles(finalCanvas, viewport, FLAGS_ddlTilingWidthHeight);
159
160 tiles.createSKPPerTile(compressedPictureData.get(), promiseImageHelper);
161
Robert Phillipsf7dcdb02018-06-21 11:18:25 -0400162 SkTaskGroup::Enabler enabled(FLAGS_ddlNumAdditionalThreads);
163
Robert Phillips96601082018-05-29 16:13:26 -0400164 clock::time_point startStopTime = clock::now();
165
166 ddl_sample(context, &tiles, nullptr, nullptr, &startStopTime);
167 GpuSync gpuSync(fenceSync);
168 ddl_sample(context, &tiles, &gpuSync, nullptr, &startStopTime);
169
170 clock::duration cumulativeDuration = std::chrono::milliseconds(0);
171
172 do {
173 samples->emplace_back();
174 Sample& sample = samples->back();
175
176 do {
177 ddl_sample(context, &tiles, &gpuSync, &sample, &startStopTime);
178 } while (sample.fDuration < sampleDuration);
179
180 cumulativeDuration += sample.fDuration;
181 } while (cumulativeDuration < benchDuration || 0 == samples->size() % 2);
182
183 if (!FLAGS_png.isEmpty()) {
184 // The user wants to see the final result
185 tiles.composeAllTiles(finalCanvas);
186 }
187}
188
csmartdaltonc6618dd2016-10-05 08:42:03 -0700189static void run_benchmark(const sk_gpu_test::FenceSync* fenceSync, SkCanvas* canvas,
190 const SkPicture* skp, std::vector<Sample>* samples) {
191 using clock = std::chrono::high_resolution_clock;
192 const Sample::duration sampleDuration = std::chrono::milliseconds(FLAGS_sampleMs);
csmartdalton037adf32016-09-28 13:56:01 -0700193 const clock::duration benchDuration = std::chrono::milliseconds(FLAGS_duration);
csmartdalton4b5179b2016-09-19 11:03:58 -0700194
Chris Daltona2b5b642018-06-24 13:08:57 -0600195 draw_skp_and_flush(canvas, skp); // draw 1
csmartdaltone0384892016-09-28 14:53:07 -0700196 GpuSync gpuSync(fenceSync);
csmartdalton4b5179b2016-09-19 11:03:58 -0700197
Chris Daltona2b5b642018-06-24 13:08:57 -0600198 for (int i = 1; i < kNumFlushesToPrimeCache; ++i) {
199 draw_skp_and_flush(canvas, skp); // draw N
200 // Waits for draw N-1 to finish (after draw N's cpu work is done).
201 gpuSync.syncToPreviousFrame();
202 }
csmartdalton4b5179b2016-09-19 11:03:58 -0700203
csmartdalton037adf32016-09-28 13:56:01 -0700204 clock::time_point now = clock::now();
205 const clock::time_point endTime = now + benchDuration;
csmartdalton4b5179b2016-09-19 11:03:58 -0700206
csmartdalton037adf32016-09-28 13:56:01 -0700207 do {
208 clock::time_point sampleStart = now;
209 samples->emplace_back();
210 Sample& sample = samples->back();
211
csmartdalton4b5179b2016-09-19 11:03:58 -0700212 do {
213 draw_skp_and_flush(canvas, skp);
csmartdaltone0384892016-09-28 14:53:07 -0700214 gpuSync.syncToPreviousFrame();
csmartdalton4b5179b2016-09-19 11:03:58 -0700215
csmartdalton037adf32016-09-28 13:56:01 -0700216 now = clock::now();
217 sample.fDuration = now - sampleStart;
csmartdalton4b5179b2016-09-19 11:03:58 -0700218 ++sample.fFrames;
csmartdalton037adf32016-09-28 13:56:01 -0700219 } while (sample.fDuration < sampleDuration);
220 } while (now < endTime || 0 == samples->size() % 2);
csmartdalton4b5179b2016-09-19 11:03:58 -0700221}
222
csmartdaltonc6618dd2016-10-05 08:42:03 -0700223static void run_gpu_time_benchmark(sk_gpu_test::GpuTimer* gpuTimer,
224 const sk_gpu_test::FenceSync* fenceSync, SkCanvas* canvas,
225 const SkPicture* skp, std::vector<Sample>* samples) {
226 using sk_gpu_test::PlatformTimerQuery;
227 using clock = std::chrono::steady_clock;
228 const clock::duration sampleDuration = std::chrono::milliseconds(FLAGS_sampleMs);
229 const clock::duration benchDuration = std::chrono::milliseconds(FLAGS_duration);
230
231 if (!gpuTimer->disjointSupport()) {
232 fprintf(stderr, "WARNING: GPU timer cannot detect disjoint operations; "
233 "results may be unreliable\n");
234 }
235
236 draw_skp_and_flush(canvas, skp);
237 GpuSync gpuSync(fenceSync);
238
Chris Daltona2b5b642018-06-24 13:08:57 -0600239 PlatformTimerQuery previousTime = 0;
240 for (int i = 1; i < kNumFlushesToPrimeCache; ++i) {
241 gpuTimer->queueStart();
242 draw_skp_and_flush(canvas, skp);
243 previousTime = gpuTimer->queueStop();
244 gpuSync.syncToPreviousFrame();
245 }
csmartdaltonc6618dd2016-10-05 08:42:03 -0700246
247 clock::time_point now = clock::now();
248 const clock::time_point endTime = now + benchDuration;
249
250 do {
251 const clock::time_point sampleEndTime = now + sampleDuration;
252 samples->emplace_back();
253 Sample& sample = samples->back();
254
255 do {
256 gpuTimer->queueStart();
257 draw_skp_and_flush(canvas, skp);
258 PlatformTimerQuery time = gpuTimer->queueStop();
259 gpuSync.syncToPreviousFrame();
260
261 switch (gpuTimer->checkQueryStatus(previousTime)) {
262 using QueryStatus = sk_gpu_test::GpuTimer::QueryStatus;
263 case QueryStatus::kInvalid:
264 exitf(ExitErr::kUnavailable, "GPU timer failed");
265 case QueryStatus::kPending:
266 exitf(ExitErr::kUnavailable, "timer query still not ready after fence sync");
267 case QueryStatus::kDisjoint:
268 if (FLAGS_verbosity >= 4) {
269 fprintf(stderr, "discarding timer query due to disjoint operations.\n");
270 }
271 break;
272 case QueryStatus::kAccurate:
273 sample.fDuration += gpuTimer->getTimeElapsed(previousTime);
274 ++sample.fFrames;
275 break;
276 }
277 gpuTimer->deleteQuery(previousTime);
278 previousTime = time;
279 now = clock::now();
280 } while (now < sampleEndTime || 0 == sample.fFrames);
281 } while (now < endTime || 0 == samples->size() % 2);
282
283 gpuTimer->deleteQuery(previousTime);
284}
285
csmartdalton4b5179b2016-09-19 11:03:58 -0700286void print_result(const std::vector<Sample>& samples, const char* config, const char* bench) {
287 if (0 == (samples.size() % 2)) {
288 exitf(ExitErr::kSoftware, "attempted to gather stats on even number of samples");
289 }
290
291 Sample accum = Sample();
292 std::vector<double> values;
293 values.reserve(samples.size());
294 for (const Sample& sample : samples) {
295 accum.fFrames += sample.fFrames;
296 accum.fDuration += sample.fDuration;
297 values.push_back(sample.value());
298 }
299 std::sort(values.begin(), values.end());
csmartdalton4b5179b2016-09-19 11:03:58 -0700300
csmartdalton6904b192016-09-29 06:23:23 -0700301 const double accumValue = accum.value();
csmartdalton4b5179b2016-09-19 11:03:58 -0700302 double variance = 0;
csmartdalton037adf32016-09-28 13:56:01 -0700303 for (double value : values) {
304 const double delta = value - accumValue;
csmartdalton4b5179b2016-09-19 11:03:58 -0700305 variance += delta * delta;
306 }
csmartdalton037adf32016-09-28 13:56:01 -0700307 variance /= values.size();
csmartdalton4b5179b2016-09-19 11:03:58 -0700308 // Technically, this is the relative standard deviation.
csmartdalton037adf32016-09-28 13:56:01 -0700309 const double stddev = 100/*%*/ * sqrt(variance) / accumValue;
csmartdalton4b5179b2016-09-19 11:03:58 -0700310
csmartdalton6904b192016-09-29 06:23:23 -0700311 printf(resultFormat, accumValue, values[values.size() / 2], values.back(), values.front(),
csmartdaltonc6618dd2016-10-05 08:42:03 -0700312 stddev, values.size(), FLAGS_sampleMs, FLAGS_gpuClock ? "gpu" : "cpu", Sample::metric(),
313 config, bench);
csmartdalton4b5179b2016-09-19 11:03:58 -0700314 printf("\n");
315 fflush(stdout);
316}
317
318int main(int argc, char** argv) {
319 SkCommandLineFlags::SetUsage("Use skpbench.py instead. "
320 "You usually don't want to use this program directly.");
321 SkCommandLineFlags::Parse(argc, argv);
322
323 if (!FLAGS_suppressHeader) {
324 printf("%s\n", header);
325 }
csmartdalton037adf32016-09-28 13:56:01 -0700326 if (FLAGS_duration <= 0) {
csmartdalton4b5179b2016-09-19 11:03:58 -0700327 exit(0); // This can be used to print the header and quit.
328 }
csmartdalton4b5179b2016-09-19 11:03:58 -0700329
330 // Parse the config.
331 const SkCommandLineConfigGpu* config = nullptr; // Initialize for spurious warning.
332 SkCommandLineConfigArray configs;
333 ParseConfigs(FLAGS_config, &configs);
334 if (configs.count() != 1 || !(config = configs[0]->asConfigGpu())) {
csmartdalton5772eaa2016-10-11 18:28:54 -0700335 exitf(ExitErr::kUsage, "invalid config '%s': must specify one (and only one) GPU config",
csmartdalton4b5179b2016-09-19 11:03:58 -0700336 join(FLAGS_config).c_str());
337 }
338
339 // Parse the skp.
340 if (FLAGS_skp.count() != 1) {
csmartdalton5772eaa2016-10-11 18:28:54 -0700341 exitf(ExitErr::kUsage, "invalid skp '%s': must specify a single skp file, or 'warmup'",
csmartdalton4b5179b2016-09-19 11:03:58 -0700342 join(FLAGS_skp).c_str());
343 }
Robert Phillips96601082018-05-29 16:13:26 -0400344
345 SkGraphics::Init();
Robert Phillips96601082018-05-29 16:13:26 -0400346
csmartdalton5772eaa2016-10-11 18:28:54 -0700347 sk_sp<SkPicture> skp;
348 SkString skpname;
349 if (0 == strcmp(FLAGS_skp[0], "warmup")) {
350 skp = create_warmup_skp();
351 skpname = "warmup";
352 } else {
353 const char* skpfile = FLAGS_skp[0];
354 std::unique_ptr<SkStream> skpstream(SkStream::MakeFromFile(skpfile));
355 if (!skpstream) {
356 exitf(ExitErr::kIO, "failed to open skp file %s", skpfile);
357 }
358 skp = SkPicture::MakeFromStream(skpstream.get());
359 if (!skp) {
360 exitf(ExitErr::kData, "failed to parse skp file %s", skpfile);
361 }
362 skpname = SkOSPath::Basename(skpfile);
csmartdalton4b5179b2016-09-19 11:03:58 -0700363 }
364 int width = SkTMin(SkScalarCeilToInt(skp->cullRect().width()), 2048),
365 height = SkTMin(SkScalarCeilToInt(skp->cullRect().height()), 2048);
csmartdaltond7a9db62016-09-22 05:10:02 -0700366 if (FLAGS_verbosity >= 3 &&
csmartdalton4b5179b2016-09-19 11:03:58 -0700367 (width != skp->cullRect().width() || height != skp->cullRect().height())) {
csmartdaltond7a9db62016-09-22 05:10:02 -0700368 fprintf(stderr, "%s is too large (%ix%i), cropping to %ix%i.\n",
csmartdalton5772eaa2016-10-11 18:28:54 -0700369 skpname.c_str(), SkScalarCeilToInt(skp->cullRect().width()),
csmartdalton4b5179b2016-09-19 11:03:58 -0700370 SkScalarCeilToInt(skp->cullRect().height()), width, height);
371 }
372
Brian Salomonf865b052018-03-09 09:01:53 -0500373 if (config->getSurfType() != SkCommandLineConfigGpu::SurfType::kDefault) {
374 exitf(ExitErr::kUnavailable, "This tool only supports the default surface type. (%s)",
375 config->getTag().c_str());
376 }
377
csmartdalton4b5179b2016-09-19 11:03:58 -0700378 // Create a context.
csmartdalton008b9d82017-02-22 12:00:42 -0700379 GrContextOptions ctxOptions;
Chris Dalton040238b2017-12-18 14:22:34 -0700380 SetCtxOptionsFromCommonFlags(&ctxOptions);
csmartdalton008b9d82017-02-22 12:00:42 -0700381 sk_gpu_test::GrContextFactory factory(ctxOptions);
csmartdalton4b5179b2016-09-19 11:03:58 -0700382 sk_gpu_test::ContextInfo ctxInfo =
csmartdaltone812d492017-02-21 12:36:05 -0700383 factory.getContextInfo(config->getContextType(), config->getContextOverrides());
csmartdalton4b5179b2016-09-19 11:03:58 -0700384 GrContext* ctx = ctxInfo.grContext();
385 if (!ctx) {
386 exitf(ExitErr::kUnavailable, "failed to create context for config %s",
387 config->getTag().c_str());
388 }
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400389 if (ctx->maxRenderTargetSize() < SkTMax(width, height)) {
csmartdalton4b5179b2016-09-19 11:03:58 -0700390 exitf(ExitErr::kUnavailable, "render target size %ix%i not supported by platform (max: %i)",
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400391 width, height, ctx->maxRenderTargetSize());
csmartdalton4b5179b2016-09-19 11:03:58 -0700392 }
Brian Osman2b23c4b2018-06-01 12:25:08 -0400393 GrPixelConfig grPixConfig = SkColorType2GrPixelConfig(config->getColorType());
Greg Daniel0a7aa142018-02-21 13:02:32 -0500394 if (kUnknown_GrPixelConfig == grPixConfig) {
395 exitf(ExitErr::kUnavailable, "failed to get GrPixelConfig from SkColorType: %d",
396 config->getColorType());
397 }
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400398 int supportedSampleCount = ctx->contextPriv().caps()->getRenderTargetSampleCount(
399 config->getSamples(), grPixConfig);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400400 if (supportedSampleCount != config->getSamples()) {
401 exitf(ExitErr::kUnavailable, "sample count %i not supported by platform",
402 config->getSamples());
csmartdalton4b5179b2016-09-19 11:03:58 -0700403 }
404 sk_gpu_test::TestContext* testCtx = ctxInfo.testContext();
405 if (!testCtx) {
406 exitf(ExitErr::kSoftware, "testContext is null");
407 }
408 if (!testCtx->fenceSyncSupport()) {
409 exitf(ExitErr::kUnavailable, "GPU does not support fence sync");
410 }
411
412 // Create a render target.
Brian Salomonce5ee602017-07-17 11:31:31 -0400413 SkImageInfo info =
414 SkImageInfo::Make(width, height, config->getColorType(), config->getAlphaType(),
415 sk_ref_sp(config->getColorSpace()));
csmartdalton4b5179b2016-09-19 11:03:58 -0700416 uint32_t flags = config->getUseDIText() ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag : 0;
417 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
418 sk_sp<SkSurface> surface =
419 SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, config->getSamples(), &props);
420 if (!surface) {
421 exitf(ExitErr::kUnavailable, "failed to create %ix%i render target for config %s",
422 width, height, config->getTag().c_str());
423 }
424
csmartdalton5772eaa2016-10-11 18:28:54 -0700425 // Run the benchmark.
csmartdalton4b5179b2016-09-19 11:03:58 -0700426 std::vector<Sample> samples;
csmartdalton037adf32016-09-28 13:56:01 -0700427 if (FLAGS_sampleMs > 0) {
428 // +1 because we might take one more sample in order to have an odd number.
429 samples.reserve(1 + (FLAGS_duration + FLAGS_sampleMs - 1) / FLAGS_sampleMs);
430 } else {
431 samples.reserve(2 * FLAGS_duration);
432 }
csmartdalton4b5179b2016-09-19 11:03:58 -0700433 SkCanvas* canvas = surface->getCanvas();
434 canvas->translate(-skp->cullRect().x(), -skp->cullRect().y());
csmartdaltonc6618dd2016-10-05 08:42:03 -0700435 if (!FLAGS_gpuClock) {
Robert Phillips96601082018-05-29 16:13:26 -0400436 if (FLAGS_ddl) {
437 run_ddl_benchmark(testCtx->fenceSync(), ctx, canvas, skp.get(), &samples);
438 } else {
439 run_benchmark(testCtx->fenceSync(), canvas, skp.get(), &samples);
440 }
csmartdaltonc6618dd2016-10-05 08:42:03 -0700441 } else {
Robert Phillips96601082018-05-29 16:13:26 -0400442 if (FLAGS_ddl) {
443 exitf(ExitErr::kUnavailable, "DDL: GPU-only timing not supported");
444 }
csmartdaltonc6618dd2016-10-05 08:42:03 -0700445 if (!testCtx->gpuTimingSupport()) {
446 exitf(ExitErr::kUnavailable, "GPU does not support timing");
447 }
448 run_gpu_time_benchmark(testCtx->gpuTimer(), testCtx->fenceSync(), canvas, skp.get(),
449 &samples);
450 }
csmartdalton5772eaa2016-10-11 18:28:54 -0700451 print_result(samples, config->getTag().c_str(), skpname.c_str());
csmartdalton4b5179b2016-09-19 11:03:58 -0700452
453 // Save a proof (if one was requested).
454 if (!FLAGS_png.isEmpty()) {
455 SkBitmap bmp;
Mike Reed12e946b2017-04-17 10:53:29 -0400456 bmp.allocPixels(info);
457 if (!surface->getCanvas()->readPixels(bmp, 0, 0)) {
csmartdalton4b5179b2016-09-19 11:03:58 -0700458 exitf(ExitErr::kUnavailable, "failed to read canvas pixels for png");
459 }
460 const SkString &dirname = SkOSPath::Dirname(FLAGS_png[0]),
461 &basename = SkOSPath::Basename(FLAGS_png[0]);
462 if (!mkdir_p(dirname)) {
463 exitf(ExitErr::kIO, "failed to create directory \"%s\" for png", dirname.c_str());
464 }
465 if (!sk_tools::write_bitmap_to_disk(bmp, dirname, nullptr, basename)) {
466 exitf(ExitErr::kIO, "failed to save png to \"%s\"", FLAGS_png[0]);
467 }
468 }
469
470 exit(0);
471}
472
473static void draw_skp_and_flush(SkCanvas* canvas, const SkPicture* skp) {
474 canvas->drawPicture(skp);
475 canvas->flush();
476}
477
csmartdalton5772eaa2016-10-11 18:28:54 -0700478static sk_sp<SkPicture> create_warmup_skp() {
479 static constexpr SkRect bounds{0, 0, 500, 500};
480 SkPictureRecorder recorder;
481 SkCanvas* recording = recorder.beginRecording(bounds);
482
483 recording->clear(SK_ColorWHITE);
484
485 SkPaint stroke;
486 stroke.setStyle(SkPaint::kStroke_Style);
487 stroke.setStrokeWidth(2);
488
489 // Use a big path to (theoretically) warmup the CPU.
490 SkPath bigPath;
491 sk_tool_utils::make_big_path(bigPath);
492 recording->drawPath(bigPath, stroke);
493
494 // Use a perlin shader to warmup the GPU.
495 SkPaint perlin;
496 perlin.setShader(SkPerlinNoiseShader::MakeTurbulence(0.1f, 0.1f, 1, 0, nullptr));
497 recording->drawRect(bounds, perlin);
498
499 return recorder.finishRecordingAsPicture();
500}
501
csmartdalton4b5179b2016-09-19 11:03:58 -0700502bool mkdir_p(const SkString& dirname) {
503 if (dirname.isEmpty()) {
504 return true;
505 }
506 return mkdir_p(SkOSPath::Dirname(dirname.c_str())) && sk_mkdir(dirname.c_str());
507}
508
509static SkString join(const SkCommandLineFlags::StringArray& stringArray) {
510 SkString joined;
csmartdalton5772eaa2016-10-11 18:28:54 -0700511 for (int i = 0; i < stringArray.count(); ++i) {
512 joined.appendf(i ? " %s" : "%s", stringArray[i]);
csmartdalton4b5179b2016-09-19 11:03:58 -0700513 }
514 return joined;
515}
516
517static void exitf(ExitErr err, const char* format, ...) {
518 fprintf(stderr, ExitErr::kSoftware == err ? "INTERNAL ERROR: " : "ERROR: ");
519 va_list args;
520 va_start(args, format);
521 vfprintf(stderr, format, args);
522 va_end(args);
523 fprintf(stderr, ExitErr::kSoftware == err ? "; this should never happen.\n": ".\n");
524 exit((int)err);
525}
csmartdaltone0384892016-09-28 14:53:07 -0700526
csmartdaltonc6618dd2016-10-05 08:42:03 -0700527GpuSync::GpuSync(const sk_gpu_test::FenceSync* fenceSync)
csmartdaltone0384892016-09-28 14:53:07 -0700528 : fFenceSync(fenceSync) {
529 this->updateFence();
530}
531
532GpuSync::~GpuSync() {
533 fFenceSync->deleteFence(fFence);
534}
535
536void GpuSync::syncToPreviousFrame() {
csmartdaltonc6618dd2016-10-05 08:42:03 -0700537 if (sk_gpu_test::kInvalidFence == fFence) {
csmartdaltone0384892016-09-28 14:53:07 -0700538 exitf(ExitErr::kSoftware, "attempted to sync with invalid fence");
539 }
540 if (!fFenceSync->waitFence(fFence)) {
541 exitf(ExitErr::kUnavailable, "failed to wait for fence");
542 }
543 fFenceSync->deleteFence(fFence);
544 this->updateFence();
545}
546
547void GpuSync::updateFence() {
548 fFence = fFenceSync->insertFence();
csmartdaltonc6618dd2016-10-05 08:42:03 -0700549 if (sk_gpu_test::kInvalidFence == fFence) {
csmartdaltone0384892016-09-28 14:53:07 -0700550 exitf(ExitErr::kUnavailable, "failed to insert fence");
551 }
552}