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 | #ifndef TestContext_DEFINED |
| 10 | #define TestContext_DEFINED |
| 11 | |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame^] | 12 | #include "FenceSync.h" |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 13 | #include "GrTypes.h" |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 14 | #include "../private/SkTemplates.h" |
| 15 | |
| 16 | namespace sk_gpu_test { |
| 17 | /** |
| 18 | * An offscreen 3D context. This class is intended for Skia's internal testing needs and not |
| 19 | * for general use. |
| 20 | */ |
| 21 | class TestContext : public SkNoncopyable { |
| 22 | public: |
| 23 | virtual ~TestContext(); |
| 24 | |
| 25 | virtual bool isValid() const = 0; |
| 26 | |
| 27 | bool fenceSyncSupport() const { return fFenceSync != nullptr; } |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame^] | 28 | FenceSync* fenceSync() { SkASSERT(fFenceSync); return fFenceSync; } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 29 | |
| 30 | bool getMaxGpuFrameLag(int *maxFrameLag) const { |
| 31 | if (!fFenceSync) { |
| 32 | return false; |
| 33 | } |
| 34 | *maxFrameLag = kMaxFrameLag; |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | void makeCurrent() const; |
| 39 | |
| 40 | virtual GrBackend backend() = 0; |
| 41 | virtual GrBackendContext backendContext() = 0; |
| 42 | |
| 43 | /** Swaps front and back buffer (if the context has such buffers) */ |
| 44 | void swapBuffers(); |
| 45 | |
| 46 | /** |
| 47 | * The only purpose of this function it to provide a means of scheduling |
| 48 | * work on the GPU (since all of the subclasses create primary buffers for |
| 49 | * testing that are small and not meant to be rendered to the screen). |
| 50 | * |
| 51 | * If the platform supports fence syncs (OpenGL 3.2+ or EGL_KHR_fence_sync), |
| 52 | * this will not swap any buffers, but rather emulate triple buffer synchronization |
| 53 | * using fences. |
| 54 | * |
| 55 | * Otherwise it will call the platform SwapBuffers method. This may or may |
| 56 | * not perform some sort of synchronization, depending on whether the |
| 57 | * drawing surface provided by the platform is double buffered. |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 58 | * |
| 59 | * Implicitly performs a submit(). |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 60 | */ |
| 61 | void waitOnSyncOrSwap(); |
| 62 | |
| 63 | /** |
| 64 | * This notifies the context that we are deliberately testing abandoning |
| 65 | * the context. It is useful for debugging contexts that would otherwise |
| 66 | * test that GPU resources are properly deleted. It also allows a debugging |
| 67 | * context to test that further API calls are not made by Skia GPU code. |
| 68 | */ |
| 69 | virtual void testAbandon(); |
| 70 | |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 71 | /** Ensures all work is submitted to the GPU for execution. */ |
| 72 | virtual void submit() = 0; |
| 73 | |
| 74 | /** Wait until all GPU work is finished. */ |
| 75 | virtual void finish() = 0; |
| 76 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 77 | protected: |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame^] | 78 | FenceSync* fFenceSync; |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 79 | |
| 80 | TestContext(); |
| 81 | |
| 82 | /** This should destroy the 3D context. */ |
| 83 | virtual void teardown(); |
| 84 | |
| 85 | virtual void onPlatformMakeCurrent() const = 0; |
| 86 | virtual void onPlatformSwapBuffers() const = 0; |
| 87 | |
| 88 | private: |
| 89 | enum { |
| 90 | kMaxFrameLag = 3 |
| 91 | }; |
| 92 | |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame^] | 93 | PlatformFence fFrameFences[kMaxFrameLag - 1]; |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 94 | int fCurrentFenceIdx; |
| 95 | |
| 96 | typedef SkNoncopyable INHERITED; |
| 97 | }; |
| 98 | } // namespace sk_gpu_test |
| 99 | #endif |