blob: acf3cf9a4978625ac8b67f6cacfa0d0a3f3cbdbd [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;
Robert Phillips00f78de2020-07-01 16:09:43 -040020class GrDirectContext;
Greg Danielb76a72a2017-07-13 15:07:54 -040021struct GrContextOptions;
22
bsalomon18a2f9d2016-05-11 10:09:18 -070023namespace sk_gpu_test {
csmartdaltonc6618dd2016-10-05 08:42:03 -070024
25class GpuTimer;
Greg Daniel02497d42020-02-21 15:46:27 -050026class FlushFinishTracker;
csmartdaltonc6618dd2016-10-05 08:42:03 -070027
bsalomon18a2f9d2016-05-11 10:09:18 -070028/**
29 * An offscreen 3D context. This class is intended for Skia's internal testing needs and not
30 * for general use.
31 */
32class TestContext : public SkNoncopyable {
33public:
34 virtual ~TestContext();
35
Greg Daniel02497d42020-02-21 15:46:27 -050036 bool fenceSyncSupport() const { return fFenceSupport; }
bsalomon18a2f9d2016-05-11 10:09:18 -070037
csmartdaltonc6618dd2016-10-05 08:42:03 -070038 bool gpuTimingSupport() const { return fGpuTimer != nullptr; }
Ben Wagner145dbcd2016-11-03 14:40:50 -040039 GpuTimer* gpuTimer() const { SkASSERT(fGpuTimer); return fGpuTimer.get(); }
csmartdaltonc6618dd2016-10-05 08:42:03 -070040
bsalomon18a2f9d2016-05-11 10:09:18 -070041 bool getMaxGpuFrameLag(int *maxFrameLag) const {
Greg Daniel02497d42020-02-21 15:46:27 -050042 if (!this->fenceSyncSupport()) {
bsalomon18a2f9d2016-05-11 10:09:18 -070043 return false;
44 }
45 *maxFrameLag = kMaxFrameLag;
46 return true;
47 }
48
Robert Phillipsedf3f382020-02-13 12:59:19 -050049 void makeNotCurrent() const;
bsalomon18a2f9d2016-05-11 10:09:18 -070050 void makeCurrent() const;
51
Brian Salomon55ad7742017-11-17 09:25:23 -050052 /**
53 * Like makeCurrent() but this returns an object that will restore the previous current
54 * context in its destructor. Useful to undo the effect making this current before returning to
55 * a caller that doesn't expect the current context to be changed underneath it.
56 *
57 * The returned object restores the current context of the same type (e.g. egl, glx, ...) in its
58 * destructor. It is undefined behavior if that context is destroyed before the destructor
59 * executes. If the concept of a current context doesn't make sense for this context type then
60 * the returned object's destructor is a no-op.
61 */
62 SkScopeExit SK_WARN_UNUSED_RESULT makeCurrentAndAutoRestore() const;
63
Greg Danielbdf12ad2018-10-12 09:31:11 -040064 virtual GrBackendApi backend() = 0;
bsalomon18a2f9d2016-05-11 10:09:18 -070065
Greg Danielb76a72a2017-07-13 15:07:54 -040066 virtual sk_sp<GrContext> makeGrContext(const GrContextOptions&);
67
bsalomon18a2f9d2016-05-11 10:09:18 -070068 /**
Greg Daniel02497d42020-02-21 15:46:27 -050069 * This will flush work to the GPU. Additionally, if the platform supports fence syncs, we will
70 * add a finished callback to our flush call. We allow ourselves to have kMaxFrameLag number of
71 * unfinished flushes active on the GPU at a time. If we have 2 outstanding flushes then we will
72 * wait on the CPU until one has finished.
bsalomon18a2f9d2016-05-11 10:09:18 -070073 */
Robert Phillips00f78de2020-07-01 16:09:43 -040074 void flushAndWaitOnSync(GrDirectContext* context);
bsalomon18a2f9d2016-05-11 10:09:18 -070075
76 /**
77 * This notifies the context that we are deliberately testing abandoning
78 * the context. It is useful for debugging contexts that would otherwise
79 * test that GPU resources are properly deleted. It also allows a debugging
80 * context to test that further API calls are not made by Skia GPU code.
81 */
82 virtual void testAbandon();
83
bsalomonc8699322016-05-11 11:55:36 -070084 /** Wait until all GPU work is finished. */
85 virtual void finish() = 0;
86
bsalomon18a2f9d2016-05-11 10:09:18 -070087protected:
Greg Daniel02497d42020-02-21 15:46:27 -050088 bool fFenceSupport = false;
89
Ben Wagner145dbcd2016-11-03 14:40:50 -040090 std::unique_ptr<GpuTimer> fGpuTimer;
bsalomon18a2f9d2016-05-11 10:09:18 -070091
92 TestContext();
93
94 /** This should destroy the 3D context. */
95 virtual void teardown();
96
Robert Phillipsedf3f382020-02-13 12:59:19 -050097 virtual void onPlatformMakeNotCurrent() const = 0;
bsalomon18a2f9d2016-05-11 10:09:18 -070098 virtual void onPlatformMakeCurrent() const = 0;
Brian Salomon55ad7742017-11-17 09:25:23 -050099 /**
100 * Subclasses should implement such that the returned function will cause the current context
101 * of this type to be made current again when it is called. It should additionally be the
102 * case that if "this" is already current when this is called, then "this" is destroyed (thereby
103 * setting the null context as current), and then the std::function is called the null context
104 * should remain current.
105 */
106 virtual std::function<void()> onPlatformGetAutoContextRestore() const = 0;
bsalomon18a2f9d2016-05-11 10:09:18 -0700107
108private:
109 enum {
110 kMaxFrameLag = 3
111 };
112
Greg Daniel02497d42020-02-21 15:46:27 -0500113 sk_sp<FlushFinishTracker> fFinishTrackers[kMaxFrameLag - 1];
114 int fCurrentFlushIdx = 0;
bsalomon18a2f9d2016-05-11 10:09:18 -0700115
116 typedef SkNoncopyable INHERITED;
117};
118} // namespace sk_gpu_test
119#endif