Brian Salomon | 31fddc3 | 2021-04-30 13:08:55 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 14 | MSKPBench::MSKPBench(SkString name, std::unique_ptr<MSKPPlayer> player) |
| 15 | : fName(name), fPlayer(std::move(player)) {} |
| 16 | |
| 17 | MSKPBench::~MSKPBench() = default; |
| 18 | |
| 19 | void 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 | |
| 35 | const char* MSKPBench::onGetName() { return fName.c_str(); } |
| 36 | |
| 37 | SkIPoint MSKPBench::onGetSize() { |
| 38 | auto dims = fPlayer->maxDimensions(); |
| 39 | return {dims.width(), dims.height()}; |
| 40 | } |
| 41 | |
| 42 | void 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 | |
| 47 | void MSKPBench::onPostDraw(SkCanvas*) { |
| 48 | // nanobench can tear down the 3D API context/device before destroying the benchmarks. |
| 49 | fPlayer->resetLayers(); |
| 50 | } |