blob: f760cc7738aafb24f1b2caf8769edd4dc86e86da [file] [log] [blame]
bsalomon18a2f9d2016-05-11 10:09:18 -07001
2/*
3 * Copyright 2016 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "TestContext.h"
10
csmartdaltonc6618dd2016-10-05 08:42:03 -070011#include "GpuTimer.h"
12
Greg Danielb76a72a2017-07-13 15:07:54 -040013#include "GrContext.h"
14
bsalomon18a2f9d2016-05-11 10:09:18 -070015namespace sk_gpu_test {
csmartdaltonc6618dd2016-10-05 08:42:03 -070016TestContext::TestContext()
17 : fFenceSync(nullptr)
18 , fGpuTimer(nullptr)
19 , fCurrentFenceIdx(0) {
bsalomon18a2f9d2016-05-11 10:09:18 -070020 memset(fFrameFences, 0, sizeof(fFrameFences));
21}
22
23TestContext::~TestContext() {
24 // Subclass should call teardown.
25#ifdef SK_DEBUG
26 for (size_t i = 0; i < SK_ARRAY_COUNT(fFrameFences); i++) {
27 SkASSERT(0 == fFrameFences[i]);
28 }
29#endif
30 SkASSERT(!fFenceSync);
csmartdaltonc6618dd2016-10-05 08:42:03 -070031 SkASSERT(!fGpuTimer);
bsalomon18a2f9d2016-05-11 10:09:18 -070032}
33
Greg Danielb76a72a2017-07-13 15:07:54 -040034sk_sp<GrContext> TestContext::makeGrContext(const GrContextOptions&) {
35 return nullptr;
36}
37
bsalomon18a2f9d2016-05-11 10:09:18 -070038void TestContext::makeCurrent() const { this->onPlatformMakeCurrent(); }
39
Brian Salomon55ad7742017-11-17 09:25:23 -050040SkScopeExit TestContext::makeCurrentAndAutoRestore() const {
41 auto asr = SkScopeExit(this->onPlatformGetAutoContextRestore());
42 this->makeCurrent();
43 return asr;
44}
45
bsalomon18a2f9d2016-05-11 10:09:18 -070046void TestContext::swapBuffers() { this->onPlatformSwapBuffers(); }
47
Brian Salomon55ad7742017-11-17 09:25:23 -050048
bsalomon18a2f9d2016-05-11 10:09:18 -070049void TestContext::waitOnSyncOrSwap() {
50 if (!fFenceSync) {
51 // Fallback on the platform SwapBuffers method for synchronization. This may have no effect.
52 this->swapBuffers();
53 return;
54 }
55
bsalomonc8699322016-05-11 11:55:36 -070056 this->submit();
bsalomon18a2f9d2016-05-11 10:09:18 -070057 if (fFrameFences[fCurrentFenceIdx]) {
bsalomonedea94c2016-05-16 14:09:56 -070058 if (!fFenceSync->waitFence(fFrameFences[fCurrentFenceIdx])) {
bsalomon18a2f9d2016-05-11 10:09:18 -070059 SkDebugf("WARNING: Wait failed for fence sync. Timings might not be accurate.\n");
60 }
61 fFenceSync->deleteFence(fFrameFences[fCurrentFenceIdx]);
62 }
63
64 fFrameFences[fCurrentFenceIdx] = fFenceSync->insertFence();
65 fCurrentFenceIdx = (fCurrentFenceIdx + 1) % SK_ARRAY_COUNT(fFrameFences);
66}
67
68void TestContext::testAbandon() {
69 if (fFenceSync) {
70 memset(fFrameFences, 0, sizeof(fFrameFences));
71 }
72}
73
74void TestContext::teardown() {
75 if (fFenceSync) {
76 for (size_t i = 0; i < SK_ARRAY_COUNT(fFrameFences); i++) {
77 if (fFrameFences[i]) {
78 fFenceSync->deleteFence(fFrameFences[i]);
79 fFrameFences[i] = 0;
80 }
81 }
csmartdaltonc6618dd2016-10-05 08:42:03 -070082 fFenceSync.reset();
bsalomon18a2f9d2016-05-11 10:09:18 -070083 }
csmartdaltonc6618dd2016-10-05 08:42:03 -070084 fGpuTimer.reset();
bsalomon18a2f9d2016-05-11 10:09:18 -070085}
86
87}