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 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 13 | namespace sk_gpu_test { |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 14 | TestContext::TestContext() |
| 15 | : fFenceSync(nullptr) |
| 16 | , fGpuTimer(nullptr) |
| 17 | , fCurrentFenceIdx(0) { |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 18 | memset(fFrameFences, 0, sizeof(fFrameFences)); |
| 19 | } |
| 20 | |
| 21 | TestContext::~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); |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 29 | SkASSERT(!fGpuTimer); |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | void TestContext::makeCurrent() const { this->onPlatformMakeCurrent(); } |
| 33 | |
| 34 | void TestContext::swapBuffers() { this->onPlatformSwapBuffers(); } |
| 35 | |
| 36 | void 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 | |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 43 | this->submit(); |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 44 | if (fFrameFences[fCurrentFenceIdx]) { |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 45 | if (!fFenceSync->waitFence(fFrameFences[fCurrentFenceIdx])) { |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 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 | |
| 55 | void TestContext::testAbandon() { |
| 56 | if (fFenceSync) { |
| 57 | memset(fFrameFences, 0, sizeof(fFrameFences)); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void 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 | } |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 69 | fFenceSync.reset(); |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 70 | } |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 71 | fGpuTimer.reset(); |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | } |