blob: 55fbc009805d82c612fa5f33b1450553ba83ab89 [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
Chris Daltona4f5ce02018-06-26 10:13:06 -060040#ifdef SK_XML
41#include "SkDOM.h"
42#include "../experimental/svg/model/SkSVGDOM.h"
43#endif
44
45
csmartdalton4b5179b2016-09-19 11:03:58 -070046/**
Chris Daltona4f5ce02018-06-26 10:13:06 -060047 * This is a minimalist program whose sole purpose is to open a .skp or .svg file, benchmark it on a
48 * single config, and exit. It is intended to be used through skpbench.py rather than invoked
49 * directly. Limiting the entire process to a single config/skp pair helps to keep the results
50 * repeatable.
csmartdalton4b5179b2016-09-19 11:03:58 -070051 *
52 * No tiling, looping, or other fanciness is used; it just draws the skp whole into a size-matched
53 * render target and syncs the GPU after each draw.
54 *
csmartdalton4b5179b2016-09-19 11:03:58 -070055 * Currently, only GPU configs are supported.
56 */
57
Robert Phillips96601082018-05-29 16:13:26 -040058DEFINE_bool(ddl, false, "record the skp into DDLs before rendering");
59DEFINE_int32(ddlNumAdditionalThreads, 0, "number of DDL recording threads in addition to main one");
60DEFINE_int32(ddlTilingWidthHeight, 0, "number of tiles along one edge when in DDL mode");
Robert Phillips65eb4fb2018-05-31 13:27:52 -040061DEFINE_bool(ddlRecordTime, false, "report just the cpu time spent recording DDLs");
Robert Phillips96601082018-05-29 16:13:26 -040062
csmartdalton037adf32016-09-28 13:56:01 -070063DEFINE_int32(duration, 5000, "number of milliseconds to run the benchmark");
64DEFINE_int32(sampleMs, 50, "minimum duration of a sample");
csmartdaltonc6618dd2016-10-05 08:42:03 -070065DEFINE_bool(gpuClock, false, "time on the gpu clock (gpu work only)");
csmartdalton4b5179b2016-09-19 11:03:58 -070066DEFINE_bool(fps, false, "use fps instead of ms");
Chris Daltona4f5ce02018-06-26 10:13:06 -060067DEFINE_string(src, "", "path to a single .skp or .svg file, or 'warmup' for a builtin warmup run");
csmartdalton4b5179b2016-09-19 11:03:58 -070068DEFINE_string(png, "", "if set, save a .png proof to disk at this file location");
69DEFINE_int32(verbosity, 4, "level of verbosity (0=none to 5=debug)");
70DEFINE_bool(suppressHeader, false, "don't print a header row before the results");
71
72static const char* header =
csmartdaltonc6618dd2016-10-05 08:42:03 -070073" accum median max min stddev samples sample_ms clock metric config bench";
csmartdalton4b5179b2016-09-19 11:03:58 -070074
75static const char* resultFormat =
csmartdaltonc6618dd2016-10-05 08:42:03 -070076"%8.4g %8.4g %8.4g %8.4g %6.3g%% %7li %9i %-5s %-6s %-9s %s";
csmartdalton4b5179b2016-09-19 11:03:58 -070077
Chris Daltona2b5b642018-06-24 13:08:57 -060078static constexpr int kNumFlushesToPrimeCache = 3;
79
csmartdalton4b5179b2016-09-19 11:03:58 -070080struct Sample {
csmartdaltonc6618dd2016-10-05 08:42:03 -070081 using duration = std::chrono::nanoseconds;
csmartdalton4b5179b2016-09-19 11:03:58 -070082
83 Sample() : fFrames(0), fDuration(0) {}
84 double seconds() const { return std::chrono::duration<double>(fDuration).count(); }
85 double ms() const { return std::chrono::duration<double, std::milli>(fDuration).count(); }
86 double value() const { return FLAGS_fps ? fFrames / this->seconds() : this->ms() / fFrames; }
87 static const char* metric() { return FLAGS_fps ? "fps" : "ms"; }
88
csmartdaltonc6618dd2016-10-05 08:42:03 -070089 int fFrames;
90 duration fDuration;
csmartdalton4b5179b2016-09-19 11:03:58 -070091};
92
csmartdaltone0384892016-09-28 14:53:07 -070093class GpuSync {
94public:
csmartdaltonc6618dd2016-10-05 08:42:03 -070095 GpuSync(const sk_gpu_test::FenceSync* fenceSync);
csmartdaltone0384892016-09-28 14:53:07 -070096 ~GpuSync();
97
98 void syncToPreviousFrame();
99
100private:
101 void updateFence();
102
csmartdaltonc6618dd2016-10-05 08:42:03 -0700103 const sk_gpu_test::FenceSync* const fFenceSync;
104 sk_gpu_test::PlatformFence fFence;
csmartdaltone0384892016-09-28 14:53:07 -0700105};
106
csmartdalton4b5179b2016-09-19 11:03:58 -0700107enum class ExitErr {
108 kOk = 0,
109 kUsage = 64,
110 kData = 65,
111 kUnavailable = 69,
112 kIO = 74,
113 kSoftware = 70
114};
115
116static void draw_skp_and_flush(SkCanvas*, const SkPicture*);
csmartdalton5772eaa2016-10-11 18:28:54 -0700117static sk_sp<SkPicture> create_warmup_skp();
Chris Daltona4f5ce02018-06-26 10:13:06 -0600118static sk_sp<SkPicture> create_skp_from_svg(SkStream*, const char* filename);
csmartdalton4b5179b2016-09-19 11:03:58 -0700119static bool mkdir_p(const SkString& name);
120static SkString join(const SkCommandLineFlags::StringArray&);
121static void exitf(ExitErr, const char* format, ...);
122
Robert Phillips96601082018-05-29 16:13:26 -0400123static void ddl_sample(GrContext* context, DDLTileHelper* tiles, GpuSync* gpuSync, Sample* sample,
124 std::chrono::high_resolution_clock::time_point* startStopTime) {
125 using clock = std::chrono::high_resolution_clock;
126
127 clock::time_point start = *startStopTime;
128
129 tiles->createDDLsInParallel();
130
Robert Phillips65eb4fb2018-05-31 13:27:52 -0400131 if (!FLAGS_ddlRecordTime) {
132 tiles->drawAllTilesAndFlush(context, true);
133 if (gpuSync) {
134 gpuSync->syncToPreviousFrame();
135 }
Robert Phillips96601082018-05-29 16:13:26 -0400136 }
137
138 *startStopTime = clock::now();
139
140 tiles->resetAllTiles();
141
142 if (sample) {
143 SkASSERT(gpuSync);
144 sample->fDuration += *startStopTime - start;
145 sample->fFrames++;
146 }
147}
148
149static void run_ddl_benchmark(const sk_gpu_test::FenceSync* fenceSync,
150 GrContext* context, SkCanvas* finalCanvas,
151 SkPicture* inputPicture, std::vector<Sample>* samples) {
152 using clock = std::chrono::high_resolution_clock;
153 const Sample::duration sampleDuration = std::chrono::milliseconds(FLAGS_sampleMs);
154 const clock::duration benchDuration = std::chrono::milliseconds(FLAGS_duration);
155
156 SkIRect viewport = finalCanvas->imageInfo().bounds();
157
158 DDLPromiseImageHelper promiseImageHelper;
159 sk_sp<SkData> compressedPictureData = promiseImageHelper.deflateSKP(inputPicture);
160 if (!compressedPictureData) {
161 exitf(ExitErr::kUnavailable, "DDL: conversion of skp failed");
162 }
163
164 promiseImageHelper.uploadAllToGPU(context);
165
166 DDLTileHelper tiles(finalCanvas, viewport, FLAGS_ddlTilingWidthHeight);
167
168 tiles.createSKPPerTile(compressedPictureData.get(), promiseImageHelper);
169
Robert Phillipsf7dcdb02018-06-21 11:18:25 -0400170 SkTaskGroup::Enabler enabled(FLAGS_ddlNumAdditionalThreads);
171
Robert Phillips96601082018-05-29 16:13:26 -0400172 clock::time_point startStopTime = clock::now();
173
174 ddl_sample(context, &tiles, nullptr, nullptr, &startStopTime);
175 GpuSync gpuSync(fenceSync);
176 ddl_sample(context, &tiles, &gpuSync, nullptr, &startStopTime);
177
178 clock::duration cumulativeDuration = std::chrono::milliseconds(0);
179
180 do {
181 samples->emplace_back();
182 Sample& sample = samples->back();
183
184 do {
185 ddl_sample(context, &tiles, &gpuSync, &sample, &startStopTime);
186 } while (sample.fDuration < sampleDuration);
187
188 cumulativeDuration += sample.fDuration;
189 } while (cumulativeDuration < benchDuration || 0 == samples->size() % 2);
190
191 if (!FLAGS_png.isEmpty()) {
192 // The user wants to see the final result
193 tiles.composeAllTiles(finalCanvas);
194 }
195}
196
csmartdaltonc6618dd2016-10-05 08:42:03 -0700197static void run_benchmark(const sk_gpu_test::FenceSync* fenceSync, SkCanvas* canvas,
198 const SkPicture* skp, std::vector<Sample>* samples) {
199 using clock = std::chrono::high_resolution_clock;
200 const Sample::duration sampleDuration = std::chrono::milliseconds(FLAGS_sampleMs);
csmartdalton037adf32016-09-28 13:56:01 -0700201 const clock::duration benchDuration = std::chrono::milliseconds(FLAGS_duration);
csmartdalton4b5179b2016-09-19 11:03:58 -0700202
Chris Daltona2b5b642018-06-24 13:08:57 -0600203 draw_skp_and_flush(canvas, skp); // draw 1
csmartdaltone0384892016-09-28 14:53:07 -0700204 GpuSync gpuSync(fenceSync);
csmartdalton4b5179b2016-09-19 11:03:58 -0700205
Chris Daltona2b5b642018-06-24 13:08:57 -0600206 for (int i = 1; i < kNumFlushesToPrimeCache; ++i) {
207 draw_skp_and_flush(canvas, skp); // draw N
208 // Waits for draw N-1 to finish (after draw N's cpu work is done).
209 gpuSync.syncToPreviousFrame();
210 }
csmartdalton4b5179b2016-09-19 11:03:58 -0700211
csmartdalton037adf32016-09-28 13:56:01 -0700212 clock::time_point now = clock::now();
213 const clock::time_point endTime = now + benchDuration;
csmartdalton4b5179b2016-09-19 11:03:58 -0700214
csmartdalton037adf32016-09-28 13:56:01 -0700215 do {
216 clock::time_point sampleStart = now;
217 samples->emplace_back();
218 Sample& sample = samples->back();
219
csmartdalton4b5179b2016-09-19 11:03:58 -0700220 do {
221 draw_skp_and_flush(canvas, skp);
csmartdaltone0384892016-09-28 14:53:07 -0700222 gpuSync.syncToPreviousFrame();
csmartdalton4b5179b2016-09-19 11:03:58 -0700223
csmartdalton037adf32016-09-28 13:56:01 -0700224 now = clock::now();
225 sample.fDuration = now - sampleStart;
csmartdalton4b5179b2016-09-19 11:03:58 -0700226 ++sample.fFrames;
csmartdalton037adf32016-09-28 13:56:01 -0700227 } while (sample.fDuration < sampleDuration);
228 } while (now < endTime || 0 == samples->size() % 2);
csmartdalton4b5179b2016-09-19 11:03:58 -0700229}
230
csmartdaltonc6618dd2016-10-05 08:42:03 -0700231static void run_gpu_time_benchmark(sk_gpu_test::GpuTimer* gpuTimer,
232 const sk_gpu_test::FenceSync* fenceSync, SkCanvas* canvas,
233 const SkPicture* skp, std::vector<Sample>* samples) {
234 using sk_gpu_test::PlatformTimerQuery;
235 using clock = std::chrono::steady_clock;
236 const clock::duration sampleDuration = std::chrono::milliseconds(FLAGS_sampleMs);
237 const clock::duration benchDuration = std::chrono::milliseconds(FLAGS_duration);
238
239 if (!gpuTimer->disjointSupport()) {
240 fprintf(stderr, "WARNING: GPU timer cannot detect disjoint operations; "
241 "results may be unreliable\n");
242 }
243
244 draw_skp_and_flush(canvas, skp);
245 GpuSync gpuSync(fenceSync);
246
Chris Daltona2b5b642018-06-24 13:08:57 -0600247 PlatformTimerQuery previousTime = 0;
248 for (int i = 1; i < kNumFlushesToPrimeCache; ++i) {
249 gpuTimer->queueStart();
250 draw_skp_and_flush(canvas, skp);
251 previousTime = gpuTimer->queueStop();
252 gpuSync.syncToPreviousFrame();
253 }
csmartdaltonc6618dd2016-10-05 08:42:03 -0700254
255 clock::time_point now = clock::now();
256 const clock::time_point endTime = now + benchDuration;
257
258 do {
259 const clock::time_point sampleEndTime = now + sampleDuration;
260 samples->emplace_back();
261 Sample& sample = samples->back();
262
263 do {
264 gpuTimer->queueStart();
265 draw_skp_and_flush(canvas, skp);
266 PlatformTimerQuery time = gpuTimer->queueStop();
267 gpuSync.syncToPreviousFrame();
268
269 switch (gpuTimer->checkQueryStatus(previousTime)) {
270 using QueryStatus = sk_gpu_test::GpuTimer::QueryStatus;
271 case QueryStatus::kInvalid:
272 exitf(ExitErr::kUnavailable, "GPU timer failed");
273 case QueryStatus::kPending:
274 exitf(ExitErr::kUnavailable, "timer query still not ready after fence sync");
275 case QueryStatus::kDisjoint:
276 if (FLAGS_verbosity >= 4) {
277 fprintf(stderr, "discarding timer query due to disjoint operations.\n");
278 }
279 break;
280 case QueryStatus::kAccurate:
281 sample.fDuration += gpuTimer->getTimeElapsed(previousTime);
282 ++sample.fFrames;
283 break;
284 }
285 gpuTimer->deleteQuery(previousTime);
286 previousTime = time;
287 now = clock::now();
288 } while (now < sampleEndTime || 0 == sample.fFrames);
289 } while (now < endTime || 0 == samples->size() % 2);
290
291 gpuTimer->deleteQuery(previousTime);
292}
293
csmartdalton4b5179b2016-09-19 11:03:58 -0700294void print_result(const std::vector<Sample>& samples, const char* config, const char* bench) {
295 if (0 == (samples.size() % 2)) {
296 exitf(ExitErr::kSoftware, "attempted to gather stats on even number of samples");
297 }
298
299 Sample accum = Sample();
300 std::vector<double> values;
301 values.reserve(samples.size());
302 for (const Sample& sample : samples) {
303 accum.fFrames += sample.fFrames;
304 accum.fDuration += sample.fDuration;
305 values.push_back(sample.value());
306 }
307 std::sort(values.begin(), values.end());
csmartdalton4b5179b2016-09-19 11:03:58 -0700308
csmartdalton6904b192016-09-29 06:23:23 -0700309 const double accumValue = accum.value();
csmartdalton4b5179b2016-09-19 11:03:58 -0700310 double variance = 0;
csmartdalton037adf32016-09-28 13:56:01 -0700311 for (double value : values) {
312 const double delta = value - accumValue;
csmartdalton4b5179b2016-09-19 11:03:58 -0700313 variance += delta * delta;
314 }
csmartdalton037adf32016-09-28 13:56:01 -0700315 variance /= values.size();
csmartdalton4b5179b2016-09-19 11:03:58 -0700316 // Technically, this is the relative standard deviation.
csmartdalton037adf32016-09-28 13:56:01 -0700317 const double stddev = 100/*%*/ * sqrt(variance) / accumValue;
csmartdalton4b5179b2016-09-19 11:03:58 -0700318
csmartdalton6904b192016-09-29 06:23:23 -0700319 printf(resultFormat, accumValue, values[values.size() / 2], values.back(), values.front(),
csmartdaltonc6618dd2016-10-05 08:42:03 -0700320 stddev, values.size(), FLAGS_sampleMs, FLAGS_gpuClock ? "gpu" : "cpu", Sample::metric(),
321 config, bench);
csmartdalton4b5179b2016-09-19 11:03:58 -0700322 printf("\n");
323 fflush(stdout);
324}
325
326int main(int argc, char** argv) {
327 SkCommandLineFlags::SetUsage("Use skpbench.py instead. "
328 "You usually don't want to use this program directly.");
329 SkCommandLineFlags::Parse(argc, argv);
330
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 }
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400411 int supportedSampleCount = ctx->contextPriv().caps()->getRenderTargetSampleCount(
412 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 {
452 run_benchmark(testCtx->fenceSync(), canvas, skp.get(), &samples);
453 }
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 }
461 run_gpu_time_benchmark(testCtx->gpuTimer(), testCtx->fenceSync(), canvas, skp.get(),
462 &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 }
473 const SkString &dirname = SkOSPath::Dirname(FLAGS_png[0]),
474 &basename = SkOSPath::Basename(FLAGS_png[0]);
475 if (!mkdir_p(dirname)) {
476 exitf(ExitErr::kIO, "failed to create directory \"%s\" for png", dirname.c_str());
477 }
478 if (!sk_tools::write_bitmap_to_disk(bmp, dirname, nullptr, basename)) {
479 exitf(ExitErr::kIO, "failed to save png to \"%s\"", FLAGS_png[0]);
480 }
481 }
482
483 exit(0);
484}
485
486static void draw_skp_and_flush(SkCanvas* canvas, const SkPicture* skp) {
487 canvas->drawPicture(skp);
488 canvas->flush();
489}
490
csmartdalton5772eaa2016-10-11 18:28:54 -0700491static sk_sp<SkPicture> create_warmup_skp() {
492 static constexpr SkRect bounds{0, 0, 500, 500};
493 SkPictureRecorder recorder;
494 SkCanvas* recording = recorder.beginRecording(bounds);
495
496 recording->clear(SK_ColorWHITE);
497
498 SkPaint stroke;
499 stroke.setStyle(SkPaint::kStroke_Style);
500 stroke.setStrokeWidth(2);
501
502 // Use a big path to (theoretically) warmup the CPU.
503 SkPath bigPath;
504 sk_tool_utils::make_big_path(bigPath);
505 recording->drawPath(bigPath, stroke);
506
507 // Use a perlin shader to warmup the GPU.
508 SkPaint perlin;
509 perlin.setShader(SkPerlinNoiseShader::MakeTurbulence(0.1f, 0.1f, 1, 0, nullptr));
510 recording->drawRect(bounds, perlin);
511
512 return recorder.finishRecordingAsPicture();
513}
514
Chris Daltona4f5ce02018-06-26 10:13:06 -0600515static sk_sp<SkPicture> create_skp_from_svg(SkStream* stream, const char* filename) {
516#ifdef SK_XML
517 SkDOM xml;
518 if (!xml.build(*stream)) {
519 exitf(ExitErr::kData, "failed to parse xml in file %s", filename);
520 }
521 sk_sp<SkSVGDOM> svg = SkSVGDOM::MakeFromDOM(xml);
522 if (!svg) {
523 exitf(ExitErr::kData, "failed to build svg dom from file %s", filename);
524 }
525
526 static constexpr SkRect bounds{0, 0, 1200, 1200};
527 SkPictureRecorder recorder;
528 SkCanvas* recording = recorder.beginRecording(bounds);
529
530 svg->setContainerSize(SkSize::Make(recording->getBaseLayerSize()));
531 svg->render(recording);
532
533 return recorder.finishRecordingAsPicture();
534#endif
535 exitf(ExitErr::kData, "SK_XML is disabled; cannot open svg file %s", filename);
536}
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
545static SkString join(const SkCommandLineFlags::StringArray& stringArray) {
546 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}