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
13#include "GrContext.h"
14
15namespace sk_gpu_test {
16TestContext::TestContext()
17    : fFenceSync(nullptr)
18    , fGpuTimer(nullptr)
19    , fCurrentFenceIdx(0) {
20    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);
31    SkASSERT(!fGpuTimer);
32}
33
34sk_sp<GrContext> TestContext::makeGrContext(const GrContextOptions&) {
35    return nullptr;
36}
37
38void TestContext::makeCurrent() const { this->onPlatformMakeCurrent(); }
39
40SkScopeExit TestContext::makeCurrentAndAutoRestore() const {
41    auto asr = SkScopeExit(this->onPlatformGetAutoContextRestore());
42    this->makeCurrent();
43    return asr;
44}
45
46void TestContext::swapBuffers() { this->onPlatformSwapBuffers(); }
47
48
49void 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
56    this->submit();
57    if (fFrameFences[fCurrentFenceIdx]) {
58        if (!fFenceSync->waitFence(fFrameFences[fCurrentFenceIdx])) {
59            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        }
82        fFenceSync.reset();
83    }
84    fGpuTimer.reset();
85}
86
87}
88