blob: df4cc9edcd5aabd1efeff2a2360af919482ae08e [file] [log] [blame]
bsalomon18a2f9d2016-05-11 10:09:18 -07001
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 Kleinc0bd9f92019-04-23 12:05:21 -050012#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"
bsalomon18a2f9d2016-05-11 10:09:18 -070018
Greg Danielb76a72a2017-07-13 15:07:54 -040019class GrContext;
20struct GrContextOptions;
21
bsalomon18a2f9d2016-05-11 10:09:18 -070022namespace sk_gpu_test {
csmartdaltonc6618dd2016-10-05 08:42:03 -070023
24class GpuTimer;
25
bsalomon18a2f9d2016-05-11 10:09:18 -070026/**
27 * An offscreen 3D context. This class is intended for Skia's internal testing needs and not
28 * for general use.
29 */
30class TestContext : public SkNoncopyable {
31public:
32 virtual ~TestContext();
33
bsalomon18a2f9d2016-05-11 10:09:18 -070034 bool fenceSyncSupport() const { return fFenceSync != nullptr; }
Ben Wagner145dbcd2016-11-03 14:40:50 -040035 FenceSync* fenceSync() { SkASSERT(fFenceSync); return fFenceSync.get(); }
bsalomon18a2f9d2016-05-11 10:09:18 -070036
csmartdaltonc6618dd2016-10-05 08:42:03 -070037 bool gpuTimingSupport() const { return fGpuTimer != nullptr; }
Ben Wagner145dbcd2016-11-03 14:40:50 -040038 GpuTimer* gpuTimer() const { SkASSERT(fGpuTimer); return fGpuTimer.get(); }
csmartdaltonc6618dd2016-10-05 08:42:03 -070039
bsalomon18a2f9d2016-05-11 10:09:18 -070040 bool getMaxGpuFrameLag(int *maxFrameLag) const {
41 if (!fFenceSync) {
42 return false;
43 }
44 *maxFrameLag = kMaxFrameLag;
45 return true;
46 }
47
Robert Phillipsedf3f382020-02-13 12:59:19 -050048 void makeNotCurrent() const;
bsalomon18a2f9d2016-05-11 10:09:18 -070049 void makeCurrent() const;
50
Brian Salomon55ad7742017-11-17 09:25:23 -050051 /**
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 Danielbdf12ad2018-10-12 09:31:11 -040063 virtual GrBackendApi backend() = 0;
bsalomon18a2f9d2016-05-11 10:09:18 -070064
Greg Danielb76a72a2017-07-13 15:07:54 -040065 virtual sk_sp<GrContext> makeGrContext(const GrContextOptions&);
66
bsalomon18a2f9d2016-05-11 10:09:18 -070067 /** Swaps front and back buffer (if the context has such buffers) */
68 void swapBuffers();
69
70 /**
71 * The only purpose of this function it to provide a means of scheduling
72 * work on the GPU (since all of the subclasses create primary buffers for
73 * testing that are small and not meant to be rendered to the screen).
74 *
75 * If the platform supports fence syncs (OpenGL 3.2+ or EGL_KHR_fence_sync),
76 * this will not swap any buffers, but rather emulate triple buffer synchronization
77 * using fences.
78 *
79 * Otherwise it will call the platform SwapBuffers method. This may or may
80 * not perform some sort of synchronization, depending on whether the
81 * drawing surface provided by the platform is double buffered.
bsalomonc8699322016-05-11 11:55:36 -070082 *
83 * Implicitly performs a submit().
bsalomon18a2f9d2016-05-11 10:09:18 -070084 */
85 void waitOnSyncOrSwap();
86
87 /**
88 * This notifies the context that we are deliberately testing abandoning
89 * the context. It is useful for debugging contexts that would otherwise
90 * test that GPU resources are properly deleted. It also allows a debugging
91 * context to test that further API calls are not made by Skia GPU code.
92 */
93 virtual void testAbandon();
94
bsalomonc8699322016-05-11 11:55:36 -070095 /** Ensures all work is submitted to the GPU for execution. */
96 virtual void submit() = 0;
97
98 /** Wait until all GPU work is finished. */
99 virtual void finish() = 0;
100
bsalomon18a2f9d2016-05-11 10:09:18 -0700101protected:
Ben Wagner145dbcd2016-11-03 14:40:50 -0400102 std::unique_ptr<FenceSync> fFenceSync;
103 std::unique_ptr<GpuTimer> fGpuTimer;
bsalomon18a2f9d2016-05-11 10:09:18 -0700104
105 TestContext();
106
107 /** This should destroy the 3D context. */
108 virtual void teardown();
109
Robert Phillipsedf3f382020-02-13 12:59:19 -0500110 virtual void onPlatformMakeNotCurrent() const = 0;
bsalomon18a2f9d2016-05-11 10:09:18 -0700111 virtual void onPlatformMakeCurrent() const = 0;
Brian Salomon55ad7742017-11-17 09:25:23 -0500112 /**
113 * Subclasses should implement such that the returned function will cause the current context
114 * of this type to be made current again when it is called. It should additionally be the
115 * case that if "this" is already current when this is called, then "this" is destroyed (thereby
116 * setting the null context as current), and then the std::function is called the null context
117 * should remain current.
118 */
119 virtual std::function<void()> onPlatformGetAutoContextRestore() const = 0;
bsalomon18a2f9d2016-05-11 10:09:18 -0700120 virtual void onPlatformSwapBuffers() const = 0;
121
122private:
123 enum {
124 kMaxFrameLag = 3
125 };
126
csmartdalton421a3c12016-10-04 11:08:45 -0700127 PlatformFence fFrameFences[kMaxFrameLag - 1];
bsalomon18a2f9d2016-05-11 10:09:18 -0700128 int fCurrentFenceIdx;
129
130 typedef SkNoncopyable INHERITED;
131};
132} // namespace sk_gpu_test
133#endif