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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkRefCnt.h" |
| 13 | #include "include/gpu/GrTypes.h" |
| 14 | #include "include/private/SkNoncopyable.h" |
| 15 | #include "include/private/SkTemplates.h" |
| 16 | #include "src/core/SkScopeExit.h" |
| 17 | #include "tools/gpu/FenceSync.h" |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 18 | |
Robert Phillips | 00f78de | 2020-07-01 16:09:43 -0400 | [diff] [blame] | 19 | class GrDirectContext; |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 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; |
Greg Daniel | 02497d4 | 2020-02-21 15:46:27 -0500 | [diff] [blame] | 25 | class FlushFinishTracker; |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 26 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 27 | /** |
| 28 | * An offscreen 3D context. This class is intended for Skia's internal testing needs and not |
| 29 | * for general use. |
| 30 | */ |
| 31 | class TestContext : public SkNoncopyable { |
| 32 | public: |
| 33 | virtual ~TestContext(); |
| 34 | |
Greg Daniel | 02497d4 | 2020-02-21 15:46:27 -0500 | [diff] [blame] | 35 | bool fenceSyncSupport() const { return fFenceSupport; } |
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 { |
Greg Daniel | 02497d4 | 2020-02-21 15:46:27 -0500 | [diff] [blame] | 41 | if (!this->fenceSyncSupport()) { |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 42 | return false; |
| 43 | } |
| 44 | *maxFrameLag = kMaxFrameLag; |
| 45 | return true; |
| 46 | } |
| 47 | |
Robert Phillips | edf3f38 | 2020-02-13 12:59:19 -0500 | [diff] [blame] | 48 | void makeNotCurrent() const; |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 49 | void makeCurrent() const; |
| 50 | |
Brian Salomon | 55ad774 | 2017-11-17 09:25:23 -0500 | [diff] [blame] | 51 | /** |
| 52 | * Like makeCurrent() but this returns an object that will restore the previous current |
| 53 | * context in its destructor. Useful to undo the effect making this current before returning to |
| 54 | * a caller that doesn't expect the current context to be changed underneath it. |
| 55 | * |
| 56 | * The returned object restores the current context of the same type (e.g. egl, glx, ...) in its |
| 57 | * destructor. It is undefined behavior if that context is destroyed before the destructor |
| 58 | * executes. If the concept of a current context doesn't make sense for this context type then |
| 59 | * the returned object's destructor is a no-op. |
| 60 | */ |
| 61 | SkScopeExit SK_WARN_UNUSED_RESULT makeCurrentAndAutoRestore() const; |
| 62 | |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 63 | virtual GrBackendApi backend() = 0; |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 64 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 65 | virtual sk_sp<GrDirectContext> makeContext(const GrContextOptions&); |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 66 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 67 | /** |
Greg Daniel | 02497d4 | 2020-02-21 15:46:27 -0500 | [diff] [blame] | 68 | * This will flush work to the GPU. Additionally, if the platform supports fence syncs, we will |
| 69 | * add a finished callback to our flush call. We allow ourselves to have kMaxFrameLag number of |
| 70 | * unfinished flushes active on the GPU at a time. If we have 2 outstanding flushes then we will |
| 71 | * wait on the CPU until one has finished. |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 72 | */ |
Robert Phillips | 00f78de | 2020-07-01 16:09:43 -0400 | [diff] [blame] | 73 | void flushAndWaitOnSync(GrDirectContext* context); |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * This notifies the context that we are deliberately testing abandoning |
| 77 | * the context. It is useful for debugging contexts that would otherwise |
| 78 | * test that GPU resources are properly deleted. It also allows a debugging |
| 79 | * context to test that further API calls are not made by Skia GPU code. |
| 80 | */ |
| 81 | virtual void testAbandon(); |
| 82 | |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 83 | /** Wait until all GPU work is finished. */ |
| 84 | virtual void finish() = 0; |
| 85 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 86 | protected: |
Greg Daniel | 02497d4 | 2020-02-21 15:46:27 -0500 | [diff] [blame] | 87 | bool fFenceSupport = false; |
| 88 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 89 | std::unique_ptr<GpuTimer> fGpuTimer; |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 90 | |
| 91 | TestContext(); |
| 92 | |
| 93 | /** This should destroy the 3D context. */ |
| 94 | virtual void teardown(); |
| 95 | |
Robert Phillips | edf3f38 | 2020-02-13 12:59:19 -0500 | [diff] [blame] | 96 | virtual void onPlatformMakeNotCurrent() const = 0; |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 97 | virtual void onPlatformMakeCurrent() const = 0; |
Brian Salomon | 55ad774 | 2017-11-17 09:25:23 -0500 | [diff] [blame] | 98 | /** |
| 99 | * Subclasses should implement such that the returned function will cause the current context |
| 100 | * of this type to be made current again when it is called. It should additionally be the |
| 101 | * case that if "this" is already current when this is called, then "this" is destroyed (thereby |
| 102 | * setting the null context as current), and then the std::function is called the null context |
| 103 | * should remain current. |
| 104 | */ |
| 105 | virtual std::function<void()> onPlatformGetAutoContextRestore() const = 0; |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 106 | |
| 107 | private: |
| 108 | enum { |
| 109 | kMaxFrameLag = 3 |
| 110 | }; |
| 111 | |
Greg Daniel | 02497d4 | 2020-02-21 15:46:27 -0500 | [diff] [blame] | 112 | sk_sp<FlushFinishTracker> fFinishTrackers[kMaxFrameLag - 1]; |
| 113 | int fCurrentFlushIdx = 0; |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 114 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 115 | using INHERITED = SkNoncopyable; |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 116 | }; |
| 117 | } // namespace sk_gpu_test |
| 118 | #endif |