1 /*
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
8 #include "include/core/SkCanvas.h"
9 #include "include/core/SkGraphics.h"
10 #include "include/core/SkPicture.h"
11 #include "include/core/SkPictureRecorder.h"
12 #include "include/core/SkStream.h"
13 #include "include/core/SkSurface.h"
14 #include "include/core/SkSurfaceProps.h"
15 #include "include/effects/SkPerlinNoiseShader.h"
16 #include "include/private/SkDeferredDisplayList.h"
17 #include "src/core/SkOSFile.h"
18 #include "src/core/SkTaskGroup.h"
19 #include "src/gpu/GrCaps.h"
20 #include "src/gpu/GrContextPriv.h"
21 #include "src/gpu/SkGr.h"
22 #include "src/utils/SkMultiPictureDocument.h"
23 #include "src/utils/SkOSPath.h"
24 #include "tools/DDLPromiseImageHelper.h"
25 #include "tools/DDLTileHelper.h"
26 #include "tools/SkSharingProc.h"
27 #include "tools/ToolUtils.h"
28 #include "tools/flags/CommandLineFlags.h"
29 #include "tools/flags/CommonFlags.h"
30 #include "tools/flags/CommonFlagsConfig.h"
31 #include "tools/gpu/GpuTimer.h"
32 #include "tools/gpu/GrContextFactory.h"
33
34 #ifdef SK_XML
35 #include "experimental/svg/model/SkSVGDOM.h"
36 #include "src/xml/SkDOM.h"
37 #endif
38
39 #include <stdlib.h>
40 #include <algorithm>
41 #include <array>
42 #include <chrono>
43 #include <cmath>
44 #include <vector>
45
46 /**
47 * 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.
51 *
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 *
55 * Well, maybe a little fanciness, MSKP's can be loaded and played. The animation is played as many
56 * times as necessary to reach the target sample duration and FPS is reported.
57 *
58 * Currently, only GPU configs are supported.
59 */
60
61 static DEFINE_bool(ddl, false, "record the skp into DDLs before rendering");
62 static DEFINE_int(ddlNumAdditionalThreads, 0,
63 "number of DDL recording threads in addition to main one");
64 static DEFINE_int(ddlTilingWidthHeight, 0, "number of tiles along one edge when in DDL mode");
65 static DEFINE_bool(ddlRecordTime, false, "report just the cpu time spent recording DDLs");
66
67 static DEFINE_int(duration, 5000, "number of milliseconds to run the benchmark");
68 static DEFINE_int(sampleMs, 50, "minimum duration of a sample");
69 static DEFINE_bool(gpuClock, false, "time on the gpu clock (gpu work only)");
70 static DEFINE_bool(fps, false, "use fps instead of ms");
71 static DEFINE_string(src, "",
72 "path to a single .skp or .svg file, or 'warmup' for a builtin warmup run");
73 static DEFINE_string(png, "", "if set, save a .png proof to disk at this file location");
74 static DEFINE_int(verbosity, 4, "level of verbosity (0=none to 5=debug)");
75 static DEFINE_bool(suppressHeader, false, "don't print a header row before the results");
76
77 static const char* header =
78 " accum median max min stddev samples sample_ms clock metric config bench";
79
80 static const char* resultFormat =
81 "%8.4g %8.4g %8.4g %8.4g %6.3g%% %7li %9i %-5s %-6s %-9s %s";
82
83 static constexpr int kNumFlushesToPrimeCache = 3;
84
85 struct Sample {
86 using duration = std::chrono::nanoseconds;
87
SampleSample88 Sample() : fFrames(0), fDuration(0) {}
secondsSample89 double seconds() const { return std::chrono::duration<double>(fDuration).count(); }
msSample90 double ms() const { return std::chrono::duration<double, std::milli>(fDuration).count(); }
valueSample91 double value() const { return FLAGS_fps ? fFrames / this->seconds() : this->ms() / fFrames; }
metricSample92 static const char* metric() { return FLAGS_fps ? "fps" : "ms"; }
93
94 int fFrames;
95 duration fDuration;
96 };
97
98 class GpuSync {
99 public:
100 GpuSync(const sk_gpu_test::FenceSync* fenceSync);
101 ~GpuSync();
102
103 void syncToPreviousFrame();
104
105 private:
106 void updateFence();
107
108 const sk_gpu_test::FenceSync* const fFenceSync;
109 sk_gpu_test::PlatformFence fFence;
110 };
111
112 enum class ExitErr {
113 kOk = 0,
114 kUsage = 64,
115 kData = 65,
116 kUnavailable = 69,
117 kIO = 74,
118 kSoftware = 70
119 };
120
121 static void draw_skp_and_flush(SkSurface*, const SkPicture*);
122 static sk_sp<SkPicture> create_warmup_skp();
123 static sk_sp<SkPicture> create_skp_from_svg(SkStream*, const char* filename);
124 static bool mkdir_p(const SkString& name);
125 static SkString join(const CommandLineFlags::StringArray&);
126 static void exitf(ExitErr, const char* format, ...);
127
128 // An interface used by both static SKPs and animated SKPs
129 class SkpProducer {
130 public:
~SkpProducer()131 virtual ~SkpProducer() {}
132 // Draw an SkPicture to the provided surface, flush the surface, and sync the GPU.
133 // You may use the static draw_skp_and_flush declared above.
134 // returned int tells how many draw/flush/sync were done.
135 virtual int drawAndFlushAndSync(SkSurface* surface, GpuSync& gpuSync) = 0;
136 };
137
138 class StaticSkp : public SkpProducer {
139 public:
StaticSkp(sk_sp<SkPicture> skp)140 StaticSkp(sk_sp<SkPicture> skp) : fSkp(skp) {}
141
drawAndFlushAndSync(SkSurface * surface,GpuSync & gpuSync)142 int drawAndFlushAndSync(SkSurface* surface, GpuSync& gpuSync) override {
143 draw_skp_and_flush(surface, fSkp.get());
144 gpuSync.syncToPreviousFrame();
145 return 1;
146 }
147 private:
148 sk_sp<SkPicture> fSkp;
149 };
150
151 // A class for playing/benchmarking a multi frame SKP file.
152 // the recorded frames are looped over repeatedly.
153 // This type of benchmark may have a much higher std dev in frame times.
154 class MultiFrameSkp : public SkpProducer {
155 public:
MultiFrameSkp(const std::vector<SkDocumentPage> & frames)156 MultiFrameSkp(const std::vector<SkDocumentPage>& frames) : fFrames(frames){}
157
MakeFromFile(const SkString & path)158 static std::unique_ptr<MultiFrameSkp> MakeFromFile(const SkString& path) {
159 // Load the multi frame skp at the given filename.
160 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(path.c_str());
161 if (!stream) { return nullptr; }
162
163 // Attempt to deserialize with an image sharing serial proc.
164 auto deserialContext = std::make_unique<SkSharingDeserialContext>();
165 SkDeserialProcs procs;
166 procs.fImageProc = SkSharingDeserialContext::deserializeImage;
167 procs.fImageCtx = deserialContext.get();
168
169 // The outer format of multi-frame skps is the multi-picture document, which is a
170 // skp file containing subpictures separated by annotations.
171 int page_count = SkMultiPictureDocumentReadPageCount(stream.get());
172 if (!page_count) {
173 return nullptr;
174 }
175 std::vector<SkDocumentPage> frames(page_count); // can't call reserve, why?
176 if (!SkMultiPictureDocumentRead(stream.get(), frames.data(), page_count, &procs)) {
177 return nullptr;
178 }
179
180 return std::make_unique<MultiFrameSkp>(frames);
181 }
182
183 // Draw the whole animation once.
drawAndFlushAndSync(SkSurface * surface,GpuSync & gpuSync)184 int drawAndFlushAndSync(SkSurface* surface, GpuSync& gpuSync) override {
185 for (int i=0; i<this->count(); i++){
186 draw_skp_and_flush(surface, this->frame(i).get());
187 gpuSync.syncToPreviousFrame();
188 }
189 return this->count();
190 }
191 // Return the requested frame.
frame(int n) const192 sk_sp<SkPicture> frame(int n) const { return fFrames[n].fPicture; }
193 // Return the number of frames in the recording.
count() const194 int count() const { return fFrames.size(); }
195 private:
196 std::vector<SkDocumentPage> fFrames;
197 };
198
ddl_sample(GrContext * context,DDLTileHelper * tiles,GpuSync * gpuSync,Sample * sample,std::chrono::high_resolution_clock::time_point * startStopTime)199 static void ddl_sample(GrContext* context, DDLTileHelper* tiles, GpuSync* gpuSync, Sample* sample,
200 std::chrono::high_resolution_clock::time_point* startStopTime) {
201 using clock = std::chrono::high_resolution_clock;
202
203 clock::time_point start = *startStopTime;
204
205 tiles->createDDLsInParallel();
206
207 if (!FLAGS_ddlRecordTime) {
208 tiles->drawAllTilesAndFlush(context, true);
209 if (gpuSync) {
210 gpuSync->syncToPreviousFrame();
211 }
212 }
213
214 *startStopTime = clock::now();
215
216 tiles->resetAllTiles();
217
218 if (sample) {
219 SkASSERT(gpuSync);
220 sample->fDuration += *startStopTime - start;
221 sample->fFrames++;
222 }
223 }
224
run_ddl_benchmark(const sk_gpu_test::FenceSync * fenceSync,GrContext * context,SkCanvas * finalCanvas,SkPicture * inputPicture,std::vector<Sample> * samples)225 static void run_ddl_benchmark(const sk_gpu_test::FenceSync* fenceSync,
226 GrContext* context, SkCanvas* finalCanvas,
227 SkPicture* inputPicture, std::vector<Sample>* samples) {
228 using clock = std::chrono::high_resolution_clock;
229 const Sample::duration sampleDuration = std::chrono::milliseconds(FLAGS_sampleMs);
230 const clock::duration benchDuration = std::chrono::milliseconds(FLAGS_duration);
231
232 SkIRect viewport = finalCanvas->imageInfo().bounds();
233
234 DDLPromiseImageHelper promiseImageHelper;
235 sk_sp<SkData> compressedPictureData = promiseImageHelper.deflateSKP(inputPicture);
236 if (!compressedPictureData) {
237 exitf(ExitErr::kUnavailable, "DDL: conversion of skp failed");
238 }
239
240 promiseImageHelper.uploadAllToGPU(context);
241
242 DDLTileHelper tiles(finalCanvas, viewport, FLAGS_ddlTilingWidthHeight);
243
244 tiles.createSKPPerTile(compressedPictureData.get(), promiseImageHelper);
245
246 SkTaskGroup::Enabler enabled(FLAGS_ddlNumAdditionalThreads);
247
248 clock::time_point startStopTime = clock::now();
249
250 ddl_sample(context, &tiles, nullptr, nullptr, &startStopTime);
251 GpuSync gpuSync(fenceSync);
252 ddl_sample(context, &tiles, &gpuSync, nullptr, &startStopTime);
253
254 clock::duration cumulativeDuration = std::chrono::milliseconds(0);
255
256 do {
257 samples->emplace_back();
258 Sample& sample = samples->back();
259
260 do {
261 ddl_sample(context, &tiles, &gpuSync, &sample, &startStopTime);
262 } while (sample.fDuration < sampleDuration);
263
264 cumulativeDuration += sample.fDuration;
265 } while (cumulativeDuration < benchDuration || 0 == samples->size() % 2);
266
267 if (!FLAGS_png.isEmpty()) {
268 // The user wants to see the final result
269 tiles.composeAllTiles(finalCanvas);
270 }
271 }
272
run_benchmark(const sk_gpu_test::FenceSync * fenceSync,SkSurface * surface,SkpProducer * skpp,std::vector<Sample> * samples)273 static void run_benchmark(const sk_gpu_test::FenceSync* fenceSync, SkSurface* surface,
274 SkpProducer* skpp, std::vector<Sample>* samples) {
275 using clock = std::chrono::high_resolution_clock;
276 const Sample::duration sampleDuration = std::chrono::milliseconds(FLAGS_sampleMs);
277 const clock::duration benchDuration = std::chrono::milliseconds(FLAGS_duration);
278
279 GpuSync gpuSync(fenceSync);
280 int i = 0;
281 do {
282 i += skpp->drawAndFlushAndSync(surface, gpuSync);
283 } while(i < kNumFlushesToPrimeCache);
284
285 clock::time_point now = clock::now();
286 const clock::time_point endTime = now + benchDuration;
287
288 do {
289 clock::time_point sampleStart = now;
290 samples->emplace_back();
291 Sample& sample = samples->back();
292
293 do {
294 sample.fFrames += skpp->drawAndFlushAndSync(surface, gpuSync);
295 now = clock::now();
296 sample.fDuration = now - sampleStart;
297 } while (sample.fDuration < sampleDuration);
298 } while (now < endTime || 0 == samples->size() % 2);
299 }
300
run_gpu_time_benchmark(sk_gpu_test::GpuTimer * gpuTimer,const sk_gpu_test::FenceSync * fenceSync,SkSurface * surface,const SkPicture * skp,std::vector<Sample> * samples)301 static void run_gpu_time_benchmark(sk_gpu_test::GpuTimer* gpuTimer,
302 const sk_gpu_test::FenceSync* fenceSync, SkSurface* surface,
303 const SkPicture* skp, std::vector<Sample>* samples) {
304 using sk_gpu_test::PlatformTimerQuery;
305 using clock = std::chrono::steady_clock;
306 const clock::duration sampleDuration = std::chrono::milliseconds(FLAGS_sampleMs);
307 const clock::duration benchDuration = std::chrono::milliseconds(FLAGS_duration);
308
309 if (!gpuTimer->disjointSupport()) {
310 fprintf(stderr, "WARNING: GPU timer cannot detect disjoint operations; "
311 "results may be unreliable\n");
312 }
313
314 draw_skp_and_flush(surface, skp);
315 GpuSync gpuSync(fenceSync);
316
317 PlatformTimerQuery previousTime = 0;
318 for (int i = 1; i < kNumFlushesToPrimeCache; ++i) {
319 gpuTimer->queueStart();
320 draw_skp_and_flush(surface, skp);
321 previousTime = gpuTimer->queueStop();
322 gpuSync.syncToPreviousFrame();
323 }
324
325 clock::time_point now = clock::now();
326 const clock::time_point endTime = now + benchDuration;
327
328 do {
329 const clock::time_point sampleEndTime = now + sampleDuration;
330 samples->emplace_back();
331 Sample& sample = samples->back();
332
333 do {
334 gpuTimer->queueStart();
335 draw_skp_and_flush(surface, skp);
336 PlatformTimerQuery time = gpuTimer->queueStop();
337 gpuSync.syncToPreviousFrame();
338
339 switch (gpuTimer->checkQueryStatus(previousTime)) {
340 using QueryStatus = sk_gpu_test::GpuTimer::QueryStatus;
341 case QueryStatus::kInvalid:
342 exitf(ExitErr::kUnavailable, "GPU timer failed");
343 case QueryStatus::kPending:
344 exitf(ExitErr::kUnavailable, "timer query still not ready after fence sync");
345 case QueryStatus::kDisjoint:
346 if (FLAGS_verbosity >= 4) {
347 fprintf(stderr, "discarding timer query due to disjoint operations.\n");
348 }
349 break;
350 case QueryStatus::kAccurate:
351 sample.fDuration += gpuTimer->getTimeElapsed(previousTime);
352 ++sample.fFrames;
353 break;
354 }
355 gpuTimer->deleteQuery(previousTime);
356 previousTime = time;
357 now = clock::now();
358 } while (now < sampleEndTime || 0 == sample.fFrames);
359 } while (now < endTime || 0 == samples->size() % 2);
360
361 gpuTimer->deleteQuery(previousTime);
362 }
363
print_result(const std::vector<Sample> & samples,const char * config,const char * bench)364 void print_result(const std::vector<Sample>& samples, const char* config, const char* bench) {
365 if (0 == (samples.size() % 2)) {
366 exitf(ExitErr::kSoftware, "attempted to gather stats on even number of samples");
367 }
368
369 Sample accum = Sample();
370 std::vector<double> values;
371 values.reserve(samples.size());
372 for (const Sample& sample : samples) {
373 accum.fFrames += sample.fFrames;
374 accum.fDuration += sample.fDuration;
375 values.push_back(sample.value());
376 }
377 std::sort(values.begin(), values.end());
378
379 const double accumValue = accum.value();
380 double variance = 0;
381 for (double value : values) {
382 const double delta = value - accumValue;
383 variance += delta * delta;
384 }
385 variance /= values.size();
386 // Technically, this is the relative standard deviation.
387 const double stddev = 100/*%*/ * sqrt(variance) / accumValue;
388
389 printf(resultFormat, accumValue, values[values.size() / 2], values.back(), values.front(),
390 stddev, values.size(), FLAGS_sampleMs, FLAGS_gpuClock ? "gpu" : "cpu", Sample::metric(),
391 config, bench);
392 printf("\n");
393 fflush(stdout);
394 }
395
main(int argc,char ** argv)396 int main(int argc, char** argv) {
397 CommandLineFlags::SetUsage(
398 "Use skpbench.py instead. "
399 "You usually don't want to use this program directly.");
400 CommandLineFlags::Parse(argc, argv);
401
402 if (!FLAGS_suppressHeader) {
403 printf("%s\n", header);
404 }
405 if (FLAGS_duration <= 0) {
406 exit(0); // This can be used to print the header and quit.
407 }
408
409 // Parse the config.
410 const SkCommandLineConfigGpu* config = nullptr; // Initialize for spurious warning.
411 SkCommandLineConfigArray configs;
412 ParseConfigs(FLAGS_config, &configs);
413 if (configs.count() != 1 || !(config = configs[0]->asConfigGpu())) {
414 exitf(ExitErr::kUsage, "invalid config '%s': must specify one (and only one) GPU config",
415 join(FLAGS_config).c_str());
416 }
417
418 // Parse the skp.
419 if (FLAGS_src.count() != 1) {
420 exitf(ExitErr::kUsage,
421 "invalid input '%s': must specify a single .skp or .svg file, or 'warmup'",
422 join(FLAGS_src).c_str());
423 }
424
425 SkGraphics::Init();
426
427 sk_sp<SkPicture> skp;
428 std::unique_ptr<MultiFrameSkp> mskp; // populated if the file is multi frame.
429 SkString srcname;
430 if (0 == strcmp(FLAGS_src[0], "warmup")) {
431 skp = create_warmup_skp();
432 srcname = "warmup";
433 } else {
434 SkString srcfile(FLAGS_src[0]);
435 std::unique_ptr<SkStream> srcstream(SkStream::MakeFromFile(srcfile.c_str()));
436 if (!srcstream) {
437 exitf(ExitErr::kIO, "failed to open file %s", srcfile.c_str());
438 }
439 if (srcfile.endsWith(".svg")) {
440 skp = create_skp_from_svg(srcstream.get(), srcfile.c_str());
441 } else if (srcfile.endsWith(".mskp")) {
442 mskp = MultiFrameSkp::MakeFromFile(srcfile);
443 // populate skp with it's first frame, for width height determination.
444 skp = mskp->frame(0);
445 } else {
446 skp = SkPicture::MakeFromStream(srcstream.get());
447 }
448 if (!skp) {
449 exitf(ExitErr::kData, "failed to parse file %s", srcfile.c_str());
450 }
451 srcname = SkOSPath::Basename(srcfile.c_str());
452 }
453 int width = SkTMin(SkScalarCeilToInt(skp->cullRect().width()), 2048),
454 height = SkTMin(SkScalarCeilToInt(skp->cullRect().height()), 2048);
455 if (FLAGS_verbosity >= 3 &&
456 (width != skp->cullRect().width() || height != skp->cullRect().height())) {
457 fprintf(stderr, "%s is too large (%ix%i), cropping to %ix%i.\n",
458 srcname.c_str(), SkScalarCeilToInt(skp->cullRect().width()),
459 SkScalarCeilToInt(skp->cullRect().height()), width, height);
460 }
461
462 if (config->getSurfType() != SkCommandLineConfigGpu::SurfType::kDefault) {
463 exitf(ExitErr::kUnavailable, "This tool only supports the default surface type. (%s)",
464 config->getTag().c_str());
465 }
466
467 // Create a context.
468 GrContextOptions ctxOptions;
469 SetCtxOptionsFromCommonFlags(&ctxOptions);
470 sk_gpu_test::GrContextFactory factory(ctxOptions);
471 sk_gpu_test::ContextInfo ctxInfo =
472 factory.getContextInfo(config->getContextType(), config->getContextOverrides());
473 GrContext* ctx = ctxInfo.grContext();
474 if (!ctx) {
475 exitf(ExitErr::kUnavailable, "failed to create context for config %s",
476 config->getTag().c_str());
477 }
478 if (ctx->maxRenderTargetSize() < SkTMax(width, height)) {
479 exitf(ExitErr::kUnavailable, "render target size %ix%i not supported by platform (max: %i)",
480 width, height, ctx->maxRenderTargetSize());
481 }
482 GrBackendFormat format = ctx->defaultBackendFormat(config->getColorType(), GrRenderable::kYes);
483 if (!format.isValid()) {
484 exitf(ExitErr::kUnavailable, "failed to get GrBackendFormat from SkColorType: %d",
485 config->getColorType());
486 }
487 int supportedSampleCount = ctx->priv().caps()->getRenderTargetSampleCount(
488 config->getSamples(), format);
489 if (supportedSampleCount != config->getSamples()) {
490 exitf(ExitErr::kUnavailable, "sample count %i not supported by platform",
491 config->getSamples());
492 }
493 sk_gpu_test::TestContext* testCtx = ctxInfo.testContext();
494 if (!testCtx) {
495 exitf(ExitErr::kSoftware, "testContext is null");
496 }
497 if (!testCtx->fenceSyncSupport()) {
498 exitf(ExitErr::kUnavailable, "GPU does not support fence sync");
499 }
500
501 // Create a render target.
502 SkImageInfo info =
503 SkImageInfo::Make(width, height, config->getColorType(), config->getAlphaType(),
504 sk_ref_sp(config->getColorSpace()));
505 uint32_t flags = config->getUseDIText() ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag : 0;
506 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
507 sk_sp<SkSurface> surface =
508 SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, config->getSamples(), &props);
509 if (!surface) {
510 exitf(ExitErr::kUnavailable, "failed to create %ix%i render target for config %s",
511 width, height, config->getTag().c_str());
512 }
513
514 // Run the benchmark.
515 std::vector<Sample> samples;
516 if (FLAGS_sampleMs > 0) {
517 // +1 because we might take one more sample in order to have an odd number.
518 samples.reserve(1 + (FLAGS_duration + FLAGS_sampleMs - 1) / FLAGS_sampleMs);
519 } else {
520 samples.reserve(2 * FLAGS_duration);
521 }
522 SkCanvas* canvas = surface->getCanvas();
523 canvas->translate(-skp->cullRect().x(), -skp->cullRect().y());
524 if (!FLAGS_gpuClock) {
525 if (FLAGS_ddl) {
526 run_ddl_benchmark(testCtx->fenceSync(), ctx, canvas, skp.get(), &samples);
527 } else if (!mskp) {
528 auto s = std::make_unique<StaticSkp>(skp);
529 run_benchmark(testCtx->fenceSync(), surface.get(), s.get(), &samples);
530 } else {
531 run_benchmark(testCtx->fenceSync(), surface.get(), mskp.get(), &samples);
532 }
533 } else {
534 if (FLAGS_ddl) {
535 exitf(ExitErr::kUnavailable, "DDL: GPU-only timing not supported");
536 }
537 if (!testCtx->gpuTimingSupport()) {
538 exitf(ExitErr::kUnavailable, "GPU does not support timing");
539 }
540 run_gpu_time_benchmark(testCtx->gpuTimer(), testCtx->fenceSync(), surface.get(),
541 skp.get(), &samples);
542 }
543 print_result(samples, config->getTag().c_str(), srcname.c_str());
544
545 // Save a proof (if one was requested).
546 if (!FLAGS_png.isEmpty()) {
547 SkBitmap bmp;
548 bmp.allocPixels(info);
549 if (!surface->getCanvas()->readPixels(bmp, 0, 0)) {
550 exitf(ExitErr::kUnavailable, "failed to read canvas pixels for png");
551 }
552 if (!mkdir_p(SkOSPath::Dirname(FLAGS_png[0]))) {
553 exitf(ExitErr::kIO, "failed to create directory for png \"%s\"", FLAGS_png[0]);
554 }
555 if (!ToolUtils::EncodeImageToFile(FLAGS_png[0], bmp, SkEncodedImageFormat::kPNG, 100)) {
556 exitf(ExitErr::kIO, "failed to save png to \"%s\"", FLAGS_png[0]);
557 }
558 }
559
560 exit(0);
561 }
562
draw_skp_and_flush(SkSurface * surface,const SkPicture * skp)563 static void draw_skp_and_flush(SkSurface* surface, const SkPicture* skp) {
564 auto canvas = surface->getCanvas();
565 canvas->drawPicture(skp);
566 surface->flush();
567 }
568
create_warmup_skp()569 static sk_sp<SkPicture> create_warmup_skp() {
570 static constexpr SkRect bounds{0, 0, 500, 500};
571 SkPictureRecorder recorder;
572 SkCanvas* recording = recorder.beginRecording(bounds);
573
574 recording->clear(SK_ColorWHITE);
575
576 SkPaint stroke;
577 stroke.setStyle(SkPaint::kStroke_Style);
578 stroke.setStrokeWidth(2);
579
580 // Use a big path to (theoretically) warmup the CPU.
581 SkPath bigPath;
582 ToolUtils::make_big_path(bigPath);
583 recording->drawPath(bigPath, stroke);
584
585 // Use a perlin shader to warmup the GPU.
586 SkPaint perlin;
587 perlin.setShader(SkPerlinNoiseShader::MakeTurbulence(0.1f, 0.1f, 1, 0, nullptr));
588 recording->drawRect(bounds, perlin);
589
590 return recorder.finishRecordingAsPicture();
591 }
592
create_skp_from_svg(SkStream * stream,const char * filename)593 static sk_sp<SkPicture> create_skp_from_svg(SkStream* stream, const char* filename) {
594 #ifdef SK_XML
595 SkDOM xml;
596 if (!xml.build(*stream)) {
597 exitf(ExitErr::kData, "failed to parse xml in file %s", filename);
598 }
599 sk_sp<SkSVGDOM> svg = SkSVGDOM::MakeFromDOM(xml);
600 if (!svg) {
601 exitf(ExitErr::kData, "failed to build svg dom from file %s", filename);
602 }
603
604 static constexpr SkRect bounds{0, 0, 1200, 1200};
605 SkPictureRecorder recorder;
606 SkCanvas* recording = recorder.beginRecording(bounds);
607
608 svg->setContainerSize(SkSize::Make(recording->getBaseLayerSize()));
609 svg->render(recording);
610
611 return recorder.finishRecordingAsPicture();
612 #endif
613 exitf(ExitErr::kData, "SK_XML is disabled; cannot open svg file %s", filename);
614 return nullptr;
615 }
616
mkdir_p(const SkString & dirname)617 bool mkdir_p(const SkString& dirname) {
618 if (dirname.isEmpty() || dirname == SkString("/")) {
619 return true;
620 }
621 return mkdir_p(SkOSPath::Dirname(dirname.c_str())) && sk_mkdir(dirname.c_str());
622 }
623
join(const CommandLineFlags::StringArray & stringArray)624 static SkString join(const CommandLineFlags::StringArray& stringArray) {
625 SkString joined;
626 for (int i = 0; i < stringArray.count(); ++i) {
627 joined.appendf(i ? " %s" : "%s", stringArray[i]);
628 }
629 return joined;
630 }
631
exitf(ExitErr err,const char * format,...)632 static void exitf(ExitErr err, const char* format, ...) {
633 fprintf(stderr, ExitErr::kSoftware == err ? "INTERNAL ERROR: " : "ERROR: ");
634 va_list args;
635 va_start(args, format);
636 vfprintf(stderr, format, args);
637 va_end(args);
638 fprintf(stderr, ExitErr::kSoftware == err ? "; this should never happen.\n": ".\n");
639 exit((int)err);
640 }
641
GpuSync(const sk_gpu_test::FenceSync * fenceSync)642 GpuSync::GpuSync(const sk_gpu_test::FenceSync* fenceSync)
643 : fFenceSync(fenceSync) {
644 this->updateFence();
645 }
646
~GpuSync()647 GpuSync::~GpuSync() {
648 fFenceSync->deleteFence(fFence);
649 }
650
syncToPreviousFrame()651 void GpuSync::syncToPreviousFrame() {
652 if (sk_gpu_test::kInvalidFence == fFence) {
653 exitf(ExitErr::kSoftware, "attempted to sync with invalid fence");
654 }
655 if (!fFenceSync->waitFence(fFence)) {
656 exitf(ExitErr::kUnavailable, "failed to wait for fence");
657 }
658 fFenceSync->deleteFence(fFence);
659 this->updateFence();
660 }
661
updateFence()662 void GpuSync::updateFence() {
663 fFence = fFenceSync->insertFence();
664 if (sk_gpu_test::kInvalidFence == fFence) {
665 exitf(ExitErr::kUnavailable, "failed to insert fence");
666 }
667 }
668