blob: 1e7fcda10749af632fd7539ad7cc0543334e5962 [file] [log] [blame]
Brian Salomon31fddc32021-04-30 13:08:55 -04001/*
2 * Copyright 2021 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 "bench/MSKPBench.h"
9#include "include/core/SkCanvas.h"
10#include "include/gpu/GrDirectContext.h"
11#include "include/gpu/GrRecordingContext.h"
12#include "tools/MSKPPlayer.h"
13
14MSKPBench::MSKPBench(SkString name, std::unique_ptr<MSKPPlayer> player)
15 : fName(name), fPlayer(std::move(player)) {}
16
17MSKPBench::~MSKPBench() = default;
18
19void MSKPBench::onDraw(int loops, SkCanvas* canvas) {
20 for (int i = 0; i < loops; ++i) {
21 for (int f = 0; f < fPlayer->numFrames(); ++f) {
22 canvas->save();
23 canvas->clipIRect(SkIRect::MakeSize(fPlayer->frameDimensions(f)));
24 fPlayer->playFrame(canvas, f);
25 canvas->restore();
26 if (auto dContext = GrAsDirectContext(canvas->recordingContext())) {
27 dContext->flushAndSubmit();
28 }
29 }
30 // Ensure each loop replays all offscreen layer draws from scratch.
31 fPlayer->rewindLayers();
32 }
33}
34
35const char* MSKPBench::onGetName() { return fName.c_str(); }
36
37SkIPoint MSKPBench::onGetSize() {
38 auto dims = fPlayer->maxDimensions();
39 return {dims.width(), dims.height()};
40}
41
42void MSKPBench::onPreDraw(SkCanvas* canvas) {
43 // We don't benchmark creation of the backing stores for layers so ensure they're all created.
44 fPlayer->allocateLayers(canvas);
45}
46
47void MSKPBench::onPostDraw(SkCanvas*) {
48 // nanobench can tear down the 3D API context/device before destroying the benchmarks.
49 fPlayer->resetLayers();
50}