bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 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 | |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 11 | #include "GpuTimer.h" |
| 12 | |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 13 | #include "GrContext.h" |
| 14 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 15 | namespace sk_gpu_test { |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 16 | TestContext::TestContext() |
| 17 | : fFenceSync(nullptr) |
| 18 | , fGpuTimer(nullptr) |
| 19 | , fCurrentFenceIdx(0) { |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 20 | memset(fFrameFences, 0, sizeof(fFrameFences)); |
| 21 | } |
| 22 | |
| 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); |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 31 | SkASSERT(!fGpuTimer); |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 34 | sk_sp<GrContext> TestContext::makeGrContext(const GrContextOptions&) { |
| 35 | return nullptr; |
| 36 | } |
| 37 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 38 | void TestContext::makeCurrent() const { this->onPlatformMakeCurrent(); } |
| 39 | |
Brian Salomon | 55ad774 | 2017-11-17 09:25:23 -0500 | [diff] [blame] | 40 | SkScopeExit TestContext::makeCurrentAndAutoRestore() const { |
| 41 | auto asr = SkScopeExit(this->onPlatformGetAutoContextRestore()); |
| 42 | this->makeCurrent(); |
| 43 | return asr; |
| 44 | } |
| 45 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 46 | void TestContext::swapBuffers() { this->onPlatformSwapBuffers(); } |
| 47 | |
Brian Salomon | 55ad774 | 2017-11-17 09:25:23 -0500 | [diff] [blame] | 48 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 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 | |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 56 | this->submit(); |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 57 | if (fFrameFences[fCurrentFenceIdx]) { |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 58 | if (!fFenceSync->waitFence(fFrameFences[fCurrentFenceIdx])) { |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 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 | |
| 68 | void TestContext::testAbandon() { |
| 69 | if (fFenceSync) { |
| 70 | memset(fFrameFences, 0, sizeof(fFrameFences)); |
| 71 | } |
| 72 | } |
| 73 | |
| 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 | } |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 82 | fFenceSync.reset(); |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 83 | } |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 84 | fGpuTimer.reset(); |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | } |