xref: /third_party/skia/tools/gpu/TestContext.h (revision cb93a386)
1cb93a386Sopenharmony_ci
2cb93a386Sopenharmony_ci/*
3cb93a386Sopenharmony_ci * Copyright 2016 Google Inc.
4cb93a386Sopenharmony_ci *
5cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
6cb93a386Sopenharmony_ci * found in the LICENSE file.
7cb93a386Sopenharmony_ci */
8cb93a386Sopenharmony_ci
9cb93a386Sopenharmony_ci#ifndef TestContext_DEFINED
10cb93a386Sopenharmony_ci#define TestContext_DEFINED
11cb93a386Sopenharmony_ci
12cb93a386Sopenharmony_ci#include "include/core/SkRefCnt.h"
13cb93a386Sopenharmony_ci#include "include/gpu/GrTypes.h"
14cb93a386Sopenharmony_ci#include "include/private/SkNoncopyable.h"
15cb93a386Sopenharmony_ci#include "include/private/SkTemplates.h"
16cb93a386Sopenharmony_ci#include "src/core/SkScopeExit.h"
17cb93a386Sopenharmony_ci#include "tools/gpu/FenceSync.h"
18cb93a386Sopenharmony_ci
19cb93a386Sopenharmony_ciclass GrDirectContext;
20cb93a386Sopenharmony_cistruct GrContextOptions;
21cb93a386Sopenharmony_ci
22cb93a386Sopenharmony_cinamespace sk_gpu_test {
23cb93a386Sopenharmony_ci
24cb93a386Sopenharmony_ciclass GpuTimer;
25cb93a386Sopenharmony_ciclass FlushFinishTracker;
26cb93a386Sopenharmony_ci
27cb93a386Sopenharmony_ci/**
28cb93a386Sopenharmony_ci * An offscreen 3D context. This class is intended for Skia's internal testing needs and not
29cb93a386Sopenharmony_ci * for general use.
30cb93a386Sopenharmony_ci */
31cb93a386Sopenharmony_ciclass TestContext : public SkNoncopyable {
32cb93a386Sopenharmony_cipublic:
33cb93a386Sopenharmony_ci    virtual ~TestContext();
34cb93a386Sopenharmony_ci
35cb93a386Sopenharmony_ci    bool fenceSyncSupport() const { return fFenceSupport; }
36cb93a386Sopenharmony_ci
37cb93a386Sopenharmony_ci    bool gpuTimingSupport() const { return fGpuTimer != nullptr; }
38cb93a386Sopenharmony_ci    GpuTimer* gpuTimer() const { SkASSERT(fGpuTimer); return fGpuTimer.get(); }
39cb93a386Sopenharmony_ci
40cb93a386Sopenharmony_ci    bool getMaxGpuFrameLag(int *maxFrameLag) const {
41cb93a386Sopenharmony_ci        if (!this->fenceSyncSupport()) {
42cb93a386Sopenharmony_ci            return false;
43cb93a386Sopenharmony_ci        }
44cb93a386Sopenharmony_ci        *maxFrameLag = kMaxFrameLag;
45cb93a386Sopenharmony_ci        return true;
46cb93a386Sopenharmony_ci    }
47cb93a386Sopenharmony_ci
48cb93a386Sopenharmony_ci    void makeNotCurrent() const;
49cb93a386Sopenharmony_ci    void makeCurrent() const;
50cb93a386Sopenharmony_ci
51cb93a386Sopenharmony_ci    /**
52cb93a386Sopenharmony_ci     * Like makeCurrent() but this returns an object that will restore the previous current
53cb93a386Sopenharmony_ci     * context in its destructor. Useful to undo the effect making this current before returning to
54cb93a386Sopenharmony_ci     * a caller that doesn't expect the current context to be changed underneath it.
55cb93a386Sopenharmony_ci     *
56cb93a386Sopenharmony_ci     * The returned object restores the current context of the same type (e.g. egl, glx, ...) in its
57cb93a386Sopenharmony_ci     * destructor. It is undefined behavior if that context is destroyed before the destructor
58cb93a386Sopenharmony_ci     * executes. If the concept of a current context doesn't make sense for this context type then
59cb93a386Sopenharmony_ci     * the returned object's destructor is a no-op.
60cb93a386Sopenharmony_ci     */
61cb93a386Sopenharmony_ci    SkScopeExit SK_WARN_UNUSED_RESULT makeCurrentAndAutoRestore() const;
62cb93a386Sopenharmony_ci
63cb93a386Sopenharmony_ci    virtual GrBackendApi backend() = 0;
64cb93a386Sopenharmony_ci
65cb93a386Sopenharmony_ci    virtual sk_sp<GrDirectContext> makeContext(const GrContextOptions&);
66cb93a386Sopenharmony_ci
67cb93a386Sopenharmony_ci    /**
68cb93a386Sopenharmony_ci     * This will flush work to the GPU. Additionally, if the platform supports fence syncs, we will
69cb93a386Sopenharmony_ci     * add a finished callback to our flush call. We allow ourselves to have kMaxFrameLag number of
70cb93a386Sopenharmony_ci     * unfinished flushes active on the GPU at a time. If we have 2 outstanding flushes then we will
71cb93a386Sopenharmony_ci     * wait on the CPU until one has finished.
72cb93a386Sopenharmony_ci     */
73cb93a386Sopenharmony_ci    void flushAndWaitOnSync(GrDirectContext* context);
74cb93a386Sopenharmony_ci
75cb93a386Sopenharmony_ci    /**
76cb93a386Sopenharmony_ci     * This notifies the context that we are deliberately testing abandoning
77cb93a386Sopenharmony_ci     * the context. It is useful for debugging contexts that would otherwise
78cb93a386Sopenharmony_ci     * test that GPU resources are properly deleted. It also allows a debugging
79cb93a386Sopenharmony_ci     * context to test that further API calls are not made by Skia GPU code.
80cb93a386Sopenharmony_ci     */
81cb93a386Sopenharmony_ci    virtual void testAbandon();
82cb93a386Sopenharmony_ci
83cb93a386Sopenharmony_ci    /** Wait until all GPU work is finished. */
84cb93a386Sopenharmony_ci    virtual void finish() = 0;
85cb93a386Sopenharmony_ci
86cb93a386Sopenharmony_ciprotected:
87cb93a386Sopenharmony_ci    bool fFenceSupport = false;
88cb93a386Sopenharmony_ci
89cb93a386Sopenharmony_ci    std::unique_ptr<GpuTimer>  fGpuTimer;
90cb93a386Sopenharmony_ci
91cb93a386Sopenharmony_ci    TestContext();
92cb93a386Sopenharmony_ci
93cb93a386Sopenharmony_ci    /** This should destroy the 3D context. */
94cb93a386Sopenharmony_ci    virtual void teardown();
95cb93a386Sopenharmony_ci
96cb93a386Sopenharmony_ci    virtual void onPlatformMakeNotCurrent() const = 0;
97cb93a386Sopenharmony_ci    virtual void onPlatformMakeCurrent() const = 0;
98cb93a386Sopenharmony_ci    /**
99cb93a386Sopenharmony_ci     * Subclasses should implement such that the returned function will cause the current context
100cb93a386Sopenharmony_ci     * of this type to be made current again when it is called. It should additionally be the
101cb93a386Sopenharmony_ci     * case that if "this" is already current when this is called, then "this" is destroyed (thereby
102cb93a386Sopenharmony_ci     * setting the null context as current), and then the std::function is called the null context
103cb93a386Sopenharmony_ci     * should remain current.
104cb93a386Sopenharmony_ci     */
105cb93a386Sopenharmony_ci    virtual std::function<void()> onPlatformGetAutoContextRestore() const = 0;
106cb93a386Sopenharmony_ci
107cb93a386Sopenharmony_ciprivate:
108cb93a386Sopenharmony_ci    enum {
109cb93a386Sopenharmony_ci        kMaxFrameLag = 3
110cb93a386Sopenharmony_ci    };
111cb93a386Sopenharmony_ci
112cb93a386Sopenharmony_ci    sk_sp<FlushFinishTracker> fFinishTrackers[kMaxFrameLag - 1];
113cb93a386Sopenharmony_ci    int fCurrentFlushIdx = 0;
114cb93a386Sopenharmony_ci
115cb93a386Sopenharmony_ci    using INHERITED = SkNoncopyable;
116cb93a386Sopenharmony_ci};
117cb93a386Sopenharmony_ci}  // namespace sk_gpu_test
118cb93a386Sopenharmony_ci#endif
119