| 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 |
|
| 12 |
#include "include/core/SkRefCnt.h" |
| 13 |
#include "include/gpu/ganesh/GrTypes.h" |
| 14 |
#include "include/private/base/SkNoncopyable.h" |
| 15 |
#include "include/private/base/SkTemplates.h" |
| 16 |
#include "src/base/SkScopeExit.h" |
| 17 |
|
| 18 |
class GrDirectContext; |
| 19 |
struct GrContextOptions; |
| 20 |
|
| 21 |
namespace sk_gpu_test { |
| 22 |
|
| 23 |
class GpuTimer; |
| 24 |
class FlushFinishTracker; |
| 25 |
|
| 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 |
|
| 34 |
bool fenceSyncSupport() const { return fFenceSupport; } |
| 35 |
|
| 36 |
bool gpuTimingSupport() const { return fGpuTimer != nullptr; } |
| 37 |
GpuTimer* gpuTimer() const { SkASSERT(fGpuTimer); return fGpuTimer.get(); } |
| 38 |
|
| 39 |
bool getMaxGpuFrameLag(int *maxFrameLag) const { |
| 40 |
if (!this->fenceSyncSupport()) { |
| 41 |
return false; |
| 42 |
} |
| 43 |
*maxFrameLag = kMaxFrameLag; |
| 44 |
return true; |
| 45 |
} |
| 46 |
|
| 47 |
void makeNotCurrent() const; |
| 48 |
void makeCurrent() const; |
| 49 |
|
| 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 |
[[nodiscard]] SkScopeExit makeCurrentAndAutoRestore() const; |
| 61 |
|
| 62 |
virtual GrBackendApi backend() = 0; |
| 63 |
|
| 64 |
virtual sk_sp<GrDirectContext> makeContext(const GrContextOptions&); |
| 65 |
|
| 66 |
/** |
| 67 |
* This will flush work to the GPU. Additionally, if the platform supports fence syncs, we will |
| 68 |
* add a finished callback to our flush call. We allow ourselves to have kMaxFrameLag number of |
| 69 |
* unfinished flushes active on the GPU at a time. If we have 2 outstanding flushes then we will |
| 70 |
* wait on the CPU until one has finished. |
| 71 |
*/ |
| 72 |
void flushAndWaitOnSync(GrDirectContext* context); |
| 73 |
|
| 74 |
/** |
| 75 |
* This notifies the context that we are deliberately testing abandoning |
| 76 |
* the context. It is useful for debugging contexts that would otherwise |
| 77 |
* test that GPU resources are properly deleted. It also allows a debugging |
| 78 |
* context to test that further API calls are not made by Skia GPU code. |
| 79 |
*/ |
| 80 |
virtual void testAbandon(); |
| 81 |
|
| 82 |
/** Flush and wait until all GPU work is finished. */ |
| 83 |
void flushAndSyncCpu(GrDirectContext*); |
| 84 |
|
| 85 |
protected: |
| 86 |
bool fFenceSupport = false; |
| 87 |
|
| 88 |
std::unique_ptr<GpuTimer> fGpuTimer; |
| 89 |
|
| 90 |
TestContext(); |
| 91 |
|
| 92 |
/** This should destroy the 3D context. */ |
| 93 |
virtual void teardown(); |
| 94 |
|
| 95 |
virtual void onPlatformMakeNotCurrent() const = 0; |
| 96 |
virtual void onPlatformMakeCurrent() const = 0; |
| 97 |
/** |
| 98 |
* Subclasses should implement such that the returned function will cause the current context |
| 99 |
* of this type to be made current again when it is called. It should additionally be the |
| 100 |
* case that if "this" is already current when this is called, then "this" is destroyed (thereby |
| 101 |
* setting the null context as current), and then the std::function is called the null context |
| 102 |
* should remain current. |
| 103 |
*/ |
| 104 |
virtual std::function<void()> onPlatformGetAutoContextRestore() const = 0; |
| 105 |
|
| 106 |
private: |
| 107 |
enum { |
| 108 |
kMaxFrameLag = 3 |
| 109 |
}; |
| 110 |
|
| 111 |
sk_sp<FlushFinishTracker> fFinishTrackers[kMaxFrameLag - 1]; |
| 112 |
int fCurrentFlushIdx = 0; |
| 113 |
|
| 114 |
using INHERITED = SkNoncopyable; |
| 115 |
}; |
| 116 |
} // namespace sk_gpu_test |
| 117 |
#endif |