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" |
Ben Wagner | d5148e3 | 2018-07-16 17:44:06 -0400 | [diff] [blame] | 14 | #include "SkNoncopyable.h" |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 15 | #include "SkRefCnt.h" |
Brian Salomon | 55ad774 | 2017-11-17 09:25:23 -0500 | [diff] [blame] | 16 | #include "SkScopeExit.h" |
Ben Wagner | d5148e3 | 2018-07-16 17:44:06 -0400 | [diff] [blame] | 17 | #include "SkTemplates.h" |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 18 | |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 19 | class GrContext; |
| 20 | struct GrContextOptions; |
| 21 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 22 | namespace sk_gpu_test { |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 23 | |
| 24 | class GpuTimer; |
| 25 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 26 | /** |
| 27 | * An offscreen 3D context. This class is intended for Skia's internal testing needs and not |
| 28 | * for general use. |
| 29 | */ |
| 30 | class TestContext : public SkNoncopyable { |
| 31 | public: |
| 32 | virtual ~TestContext(); |
| 33 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 34 | bool fenceSyncSupport() const { return fFenceSync != nullptr; } |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 35 | FenceSync* fenceSync() { SkASSERT(fFenceSync); return fFenceSync.get(); } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 36 | |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 37 | bool gpuTimingSupport() const { return fGpuTimer != nullptr; } |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 38 | GpuTimer* gpuTimer() const { SkASSERT(fGpuTimer); return fGpuTimer.get(); } |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 39 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 40 | bool getMaxGpuFrameLag(int *maxFrameLag) const { |
| 41 | if (!fFenceSync) { |
| 42 | return false; |
| 43 | } |
| 44 | *maxFrameLag = kMaxFrameLag; |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | void makeCurrent() const; |
| 49 | |
Brian Salomon | 55ad774 | 2017-11-17 09:25:23 -0500 | [diff] [blame] | 50 | /** |
| 51 | * Like makeCurrent() but this returns an object that will restore the previous current |
| 52 | * context in its destructor. Useful to undo the effect making this current before returning to |
| 53 | * a caller that doesn't expect the current context to be changed underneath it. |
| 54 | * |
| 55 | * The returned object restores the current context of the same type (e.g. egl, glx, ...) in its |
| 56 | * destructor. It is undefined behavior if that context is destroyed before the destructor |
| 57 | * executes. If the concept of a current context doesn't make sense for this context type then |
| 58 | * the returned object's destructor is a no-op. |
| 59 | */ |
| 60 | SkScopeExit SK_WARN_UNUSED_RESULT makeCurrentAndAutoRestore() const; |
| 61 | |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 62 | virtual GrBackendApi backend() = 0; |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 63 | |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 64 | virtual sk_sp<GrContext> makeGrContext(const GrContextOptions&); |
| 65 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 66 | /** Swaps front and back buffer (if the context has such buffers) */ |
| 67 | void swapBuffers(); |
| 68 | |
| 69 | /** |
| 70 | * The only purpose of this function it to provide a means of scheduling |
| 71 | * work on the GPU (since all of the subclasses create primary buffers for |
| 72 | * testing that are small and not meant to be rendered to the screen). |
| 73 | * |
| 74 | * If the platform supports fence syncs (OpenGL 3.2+ or EGL_KHR_fence_sync), |
| 75 | * this will not swap any buffers, but rather emulate triple buffer synchronization |
| 76 | * using fences. |
| 77 | * |
| 78 | * Otherwise it will call the platform SwapBuffers method. This may or may |
| 79 | * not perform some sort of synchronization, depending on whether the |
| 80 | * drawing surface provided by the platform is double buffered. |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 81 | * |
| 82 | * Implicitly performs a submit(). |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 83 | */ |
| 84 | void waitOnSyncOrSwap(); |
| 85 | |
| 86 | /** |
| 87 | * This notifies the context that we are deliberately testing abandoning |
| 88 | * the context. It is useful for debugging contexts that would otherwise |
| 89 | * test that GPU resources are properly deleted. It also allows a debugging |
| 90 | * context to test that further API calls are not made by Skia GPU code. |
| 91 | */ |
| 92 | virtual void testAbandon(); |
| 93 | |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 94 | /** Ensures all work is submitted to the GPU for execution. */ |
| 95 | virtual void submit() = 0; |
| 96 | |
| 97 | /** Wait until all GPU work is finished. */ |
| 98 | virtual void finish() = 0; |
| 99 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 100 | protected: |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 101 | std::unique_ptr<FenceSync> fFenceSync; |
| 102 | std::unique_ptr<GpuTimer> fGpuTimer; |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 103 | |
| 104 | TestContext(); |
| 105 | |
| 106 | /** This should destroy the 3D context. */ |
| 107 | virtual void teardown(); |
| 108 | |
| 109 | virtual void onPlatformMakeCurrent() const = 0; |
Brian Salomon | 55ad774 | 2017-11-17 09:25:23 -0500 | [diff] [blame] | 110 | /** |
| 111 | * Subclasses should implement such that the returned function will cause the current context |
| 112 | * of this type to be made current again when it is called. It should additionally be the |
| 113 | * case that if "this" is already current when this is called, then "this" is destroyed (thereby |
| 114 | * setting the null context as current), and then the std::function is called the null context |
| 115 | * should remain current. |
| 116 | */ |
| 117 | virtual std::function<void()> onPlatformGetAutoContextRestore() const = 0; |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 118 | virtual void onPlatformSwapBuffers() const = 0; |
| 119 | |
| 120 | private: |
| 121 | enum { |
| 122 | kMaxFrameLag = 3 |
| 123 | }; |
| 124 | |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 125 | PlatformFence fFrameFences[kMaxFrameLag - 1]; |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 126 | int fCurrentFenceIdx; |
| 127 | |
| 128 | typedef SkNoncopyable INHERITED; |
| 129 | }; |
| 130 | } // namespace sk_gpu_test |
| 131 | #endif |