Flutter Engine
The Flutter Engine
TestContext.h
Go to the documentation of this file.
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
13#include "include/gpu/GrTypes.h"
17#include "tools/gpu/FenceSync.h"
18
19class GrDirectContext;
20struct GrContextOptions;
21
22namespace sk_gpu_test {
23
24class GpuTimer;
25class FlushFinishTracker;
26
27/**
28 * An offscreen 3D context. This class is intended for Skia's internal testing needs and not
29 * for general use.
30 */
31class TestContext : public SkNoncopyable {
32public:
33 virtual ~TestContext();
34
35 bool fenceSyncSupport() const { return fFenceSupport; }
36
37 bool gpuTimingSupport() const { return fGpuTimer != nullptr; }
38 GpuTimer* gpuTimer() const { SkASSERT(fGpuTimer); return fGpuTimer.get(); }
39
40 bool getMaxGpuFrameLag(int *maxFrameLag) const {
41 if (!this->fenceSyncSupport()) {
42 return false;
43 }
44 *maxFrameLag = kMaxFrameLag;
45 return true;
46 }
47
48 void makeNotCurrent() const;
49 void makeCurrent() const;
50
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 [[nodiscard]] SkScopeExit makeCurrentAndAutoRestore() const;
62
63 virtual GrBackendApi backend() = 0;
64
66
67 /**
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.
72 */
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
83 /** Flush and wait until all GPU work is finished. */
85
86protected:
87 bool fFenceSupport = false;
88
89 std::unique_ptr<GpuTimer> fGpuTimer;
90
92
93 /** This should destroy the 3D context. */
94 virtual void teardown();
95
96 virtual void onPlatformMakeNotCurrent() const = 0;
97 virtual void onPlatformMakeCurrent() const = 0;
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 */
106
107private:
108 enum {
109 kMaxFrameLag = 3
110 };
111
112 sk_sp<FlushFinishTracker> fFinishTrackers[kMaxFrameLag - 1];
113 int fCurrentFlushIdx = 0;
114
115 using INHERITED = SkNoncopyable;
116};
117} // namespace sk_gpu_test
118#endif
GrBackendApi
Definition: GrTypes.h:95
#define SkASSERT(cond)
Definition: SkAssert.h:116
SkNoncopyable()=default
virtual sk_sp< GrDirectContext > makeContext(const GrContextOptions &)
Definition: TestContext.cpp:24
bool getMaxGpuFrameLag(int *maxFrameLag) const
Definition: TestContext.h:40
virtual GrBackendApi backend()=0
std::unique_ptr< GpuTimer > fGpuTimer
Definition: TestContext.h:89
void flushAndWaitOnSync(GrDirectContext *context)
Definition: TestContext.cpp:37
bool gpuTimingSupport() const
Definition: TestContext.h:37
GpuTimer * gpuTimer() const
Definition: TestContext.h:38
virtual void teardown()
Definition: TestContext.cpp:70
virtual std::function< void()> onPlatformGetAutoContextRestore() const =0
virtual void onPlatformMakeNotCurrent() const =0
void flushAndSyncCpu(GrDirectContext *)
Definition: TestContext.cpp:61
void makeNotCurrent() const
Definition: TestContext.cpp:28
virtual void testAbandon()
Definition: TestContext.cpp:67
virtual void onPlatformMakeCurrent() const =0
bool fenceSyncSupport() const
Definition: TestContext.h:35
SkScopeExit makeCurrentAndAutoRestore() const
Definition: TestContext.cpp:31
Dart_NativeFunction function
Definition: fuchsia.cc:51