1
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
11#include "GpuTimer.h"
12
13namespace sk_gpu_test {
14TestContext::TestContext()
15    : fFenceSync(nullptr)
16    , fGpuTimer(nullptr)
17    , fCurrentFenceIdx(0) {
18    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);
29    SkASSERT(!fGpuTimer);
30}
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
43    this->submit();
44    if (fFrameFences[fCurrentFenceIdx]) {
45        if (!fFenceSync->waitFence(fFrameFences[fCurrentFenceIdx])) {
46            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        }
69        fFenceSync.reset();
70    }
71    fGpuTimer.reset();
72}
73
74}
75