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()16TestContext::TestContext() 17 : fFenceSync(nullptr) 18 , fGpuTimer(nullptr) 19 , fCurrentFenceIdx(0) { 20 memset(fFrameFences, 0, sizeof(fFrameFences)); 21 } 22 ~TestContext()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 makeGrContext(const GrContextOptions &)34sk_sp<GrContext> TestContext::makeGrContext(const GrContextOptions&) { 35 return nullptr; 36 } 37 makeNotCurrent() const38void TestContext::makeNotCurrent() const { this->onPlatformMakeNotCurrent(); } makeCurrent() const39void TestContext::makeCurrent() const { this->onPlatformMakeCurrent(); } 40 makeCurrentAndAutoRestore() const41SkScopeExit TestContext::makeCurrentAndAutoRestore() const { 42 auto asr = SkScopeExit(this->onPlatformGetAutoContextRestore()); 43 this->makeCurrent(); 44 return asr; 45 } 46 swapBuffers()47void TestContext::swapBuffers() { this->onPlatformSwapBuffers(); } 48 49 waitOnSyncOrSwap()50void TestContext::waitOnSyncOrSwap() { 51 if (!fFenceSync) { 52 // Fallback on the platform SwapBuffers method for synchronization. This may have no effect. 53 this->swapBuffers(); 54 return; 55 } 56 57 this->submit(); 58 if (fFrameFences[fCurrentFenceIdx]) { 59 if (!fFenceSync->waitFence(fFrameFences[fCurrentFenceIdx])) { 60 SkDebugf("WARNING: Wait failed for fence sync. Timings might not be accurate.\n"); 61 } 62 fFenceSync->deleteFence(fFrameFences[fCurrentFenceIdx]); 63 } 64 65 fFrameFences[fCurrentFenceIdx] = fFenceSync->insertFence(); 66 fCurrentFenceIdx = (fCurrentFenceIdx + 1) % SK_ARRAY_COUNT(fFrameFences); 67 } 68 testAbandon()69void TestContext::testAbandon() { 70 if (fFenceSync) { 71 memset(fFrameFences, 0, sizeof(fFrameFences)); 72 } 73 } 74 teardown()75void TestContext::teardown() { 76 if (fFenceSync) { 77 for (size_t i = 0; i < SK_ARRAY_COUNT(fFrameFences); i++) { 78 if (fFrameFences[i]) { 79 fFenceSync->deleteFence(fFrameFences[i]); 80 fFrameFences[i] = 0; 81 } 82 } 83 fFenceSync.reset(); 84 } 85 fGpuTimer.reset(); 86 } 87 88 } 89