blob: 58c31862419c149dddbf086245a96175e6cb617a [file] [log] [blame]
commit-bot@chromium.org6adce672014-02-03 14:48:17 +00001/*
2 * Copyright 2014 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "bench/GMBench.h"
commit-bot@chromium.org6adce672014-02-03 14:48:17 +00009
Robert Phillipsb87b39b2020-07-01 14:45:24 -040010#include "include/gpu/GrRecordingContext.h"
11#include "src/gpu/GrRecordingContextPriv.h"
12
Ben Wagner406ff502019-08-12 16:39:24 -040013GMBench::GMBench(std::unique_ptr<skiagm::GM> gm) : fGM(std::move(gm)) {
Robert Phillips83b749a2020-06-25 11:41:19 -040014 fGM->setMode(skiagm::GM::kBench_Mode);
15
Ben Wagner406ff502019-08-12 16:39:24 -040016 fName.printf("GM_%s", fGM->getName());
commit-bot@chromium.org6adce672014-02-03 14:48:17 +000017}
18
tfarinaf168b862014-06-19 12:32:29 -070019const char* GMBench::onGetName() {
commit-bot@chromium.org6adce672014-02-03 14:48:17 +000020 return fName.c_str();
21}
22
tfarinaf168b862014-06-19 12:32:29 -070023bool GMBench::isSuitableFor(Backend backend) {
mtklein1c402922015-01-23 11:07:07 -080024 return kNonRendering_Backend != backend;
commit-bot@chromium.org6adce672014-02-03 14:48:17 +000025}
26
Robert Phillips83b749a2020-06-25 11:41:19 -040027void GMBench::onPerCanvasPreDraw(SkCanvas* canvas) {
Adlai Hollere3ad5272020-07-07 10:27:55 -040028 auto direct = GrAsDirectContext(canvas->recordingContext());
Robert Phillipsb87b39b2020-07-01 14:45:24 -040029
30 if (fGM->gpuSetup(direct, canvas) != skiagm::DrawResult::kOk) {
Robert Phillipse9229532020-06-26 10:10:49 -040031 fGpuSetupFailed = true;
32 }
33
Robert Phillips83b749a2020-06-25 11:41:19 -040034 fGM->onceBeforeDraw();
35}
36
Robert Phillipse9229532020-06-26 10:10:49 -040037void GMBench::onPerCanvasPostDraw(SkCanvas*) {
38 fGM->gpuTeardown();
39
40 // The same GM will be reused with multiple GrContexts. Let the next GrContext start
41 // afresh.
42 fGpuSetupFailed = false;
43}
Robert Phillips83b749a2020-06-25 11:41:19 -040044
mtkleina1ebeb22015-10-01 09:43:39 -070045void GMBench::onDraw(int loops, SkCanvas* canvas) {
Robert Phillipse9229532020-06-26 10:10:49 -040046 if (fGpuSetupFailed) {
47 return;
48 }
49
commit-bot@chromium.org6adce672014-02-03 14:48:17 +000050 fGM->drawBackground(canvas);
51 for (int i = 0; i < loops; ++i) {
52 fGM->drawContent(canvas);
53 }
54}
55
tfarinaf168b862014-06-19 12:32:29 -070056SkIPoint GMBench::onGetSize() {
commit-bot@chromium.org6adce672014-02-03 14:48:17 +000057 SkISize size = fGM->getISize();
58 return SkIPoint::Make(size.fWidth, size.fHeight);
59}