blob: 90aba438804c80f9418e8e4a6eb52d1b3f53588b [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
bsalomon18a2f9d2016-05-11 10:09:18 -070013namespace sk_gpu_test {
csmartdaltonc6618dd2016-10-05 08:42:03 -070014TestContext::TestContext()
15 : fFenceSync(nullptr)
16 , fGpuTimer(nullptr)
17 , fCurrentFenceIdx(0) {
bsalomon18a2f9d2016-05-11 10:09:18 -070018 memset(fFrameFences, 0, sizeof(fFrameFences));
19}
20
21TestContext::~TestContext() {
22 // Subclass should call teardown.
23#ifdef SK_DEBUG
24 for (size_t i = 0; i < SK_ARRAY_COUNT(fFrameFences); i++) {
25 SkASSERT(0 == fFrameFences[i]);
26 }
27#endif
28 SkASSERT(!fFenceSync);
csmartdaltonc6618dd2016-10-05 08:42:03 -070029 SkASSERT(!fGpuTimer);
bsalomon18a2f9d2016-05-11 10:09:18 -070030}
31
32void TestContext::makeCurrent() const { this->onPlatformMakeCurrent(); }
33
34void TestContext::swapBuffers() { this->onPlatformSwapBuffers(); }
35
36void TestContext::waitOnSyncOrSwap() {
37 if (!fFenceSync) {
38 // Fallback on the platform SwapBuffers method for synchronization. This may have no effect.
39 this->swapBuffers();
40 return;
41 }
42
bsalomonc8699322016-05-11 11:55:36 -070043 this->submit();
bsalomon18a2f9d2016-05-11 10:09:18 -070044 if (fFrameFences[fCurrentFenceIdx]) {
bsalomonedea94c2016-05-16 14:09:56 -070045 if (!fFenceSync->waitFence(fFrameFences[fCurrentFenceIdx])) {
bsalomon18a2f9d2016-05-11 10:09:18 -070046 SkDebugf("WARNING: Wait failed for fence sync. Timings might not be accurate.\n");
47 }
48 fFenceSync->deleteFence(fFrameFences[fCurrentFenceIdx]);
49 }
50
51 fFrameFences[fCurrentFenceIdx] = fFenceSync->insertFence();
52 fCurrentFenceIdx = (fCurrentFenceIdx + 1) % SK_ARRAY_COUNT(fFrameFences);
53}
54
55void TestContext::testAbandon() {
56 if (fFenceSync) {
57 memset(fFrameFences, 0, sizeof(fFrameFences));
58 }
59}
60
61void TestContext::teardown() {
62 if (fFenceSync) {
63 for (size_t i = 0; i < SK_ARRAY_COUNT(fFrameFences); i++) {
64 if (fFrameFences[i]) {
65 fFenceSync->deleteFence(fFrameFences[i]);
66 fFrameFences[i] = 0;
67 }
68 }
csmartdaltonc6618dd2016-10-05 08:42:03 -070069 fFenceSync.reset();
bsalomon18a2f9d2016-05-11 10:09:18 -070070 }
csmartdaltonc6618dd2016-10-05 08:42:03 -070071 fGpuTimer.reset();
bsalomon18a2f9d2016-05-11 10:09:18 -070072}
73
74}