• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "tools/gpu/TestContext.h"
10 
11 #include "tools/gpu/GpuTimer.h"
12 
13 #include "include/gpu/GrContext.h"
14 
15 namespace sk_gpu_test {
TestContext()16 TestContext::TestContext()
17     : fFenceSync(nullptr)
18     , fGpuTimer(nullptr)
19     , fCurrentFenceIdx(0) {
20     memset(fFrameFences, 0, sizeof(fFrameFences));
21 }
22 
~TestContext()23 TestContext::~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 
makeGrContext(const GrContextOptions &)34 sk_sp<GrContext> TestContext::makeGrContext(const GrContextOptions&) {
35     return nullptr;
36 }
37 
makeCurrent() const38 void TestContext::makeCurrent() const { this->onPlatformMakeCurrent(); }
39 
makeCurrentAndAutoRestore() const40 SkScopeExit TestContext::makeCurrentAndAutoRestore() const {
41     auto asr = SkScopeExit(this->onPlatformGetAutoContextRestore());
42     this->makeCurrent();
43     return asr;
44 }
45 
swapBuffers()46 void TestContext::swapBuffers() { this->onPlatformSwapBuffers(); }
47 
48 
waitOnSyncOrSwap()49 void 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 
testAbandon()68 void TestContext::testAbandon() {
69     if (fFenceSync) {
70         memset(fFrameFences, 0, sizeof(fFrameFences));
71     }
72 }
73 
teardown()74 void 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