Coverage Report

Created: 2024-05-20 07:14

/src/skia/tools/gpu/GrContextFactory.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2012 Google Inc.
3
 *
4
 * Use of this source code is governed by a BSD-style license that can be
5
 * found in the LICENSE file.
6
 */
7
8
#ifndef GrContextFactory_DEFINED
9
#define GrContextFactory_DEFINED
10
11
#include "include/gpu/GrContextOptions.h"
12
#include "include/gpu/GrDirectContext.h"
13
14
#include "include/private/base/SkTArray.h"
15
#include "tools/gpu/ContextType.h"
16
#include "tools/gpu/TestContext.h"
17
18
#ifdef SK_GL
19
#include "tools/gpu/gl/GLTestContext.h"
20
#endif
21
22
struct GrVkBackendContext;
23
24
namespace sk_gpu_test {
25
class ContextInfo;
26
27
/**
28
 * This is a simple class that is useful in test apps that use different
29
 * GrContexts backed by different types of GL contexts. It manages creating the
30
 * GL context and a GrContext that uses it. The GL/Gr contexts persist until the
31
 * factory is destroyed (though the caller can always grab a ref on the returned
32
 * Gr and GL contexts to make them outlive the factory).
33
 */
34
class GrContextFactory : SkNoncopyable {
35
public:
36
    using ContextType = skgpu::ContextType;
37
38
    /**
39
     * Overrides for the initial GrContextOptions provided at construction time, and required
40
     * features that will cause context creation to fail if not present.
41
     */
42
    enum class ContextOverrides {
43
        kNone                          = 0x0,
44
        kAvoidStencilBuffers           = 0x1,
45
        kFakeGLESVersionAs2            = 0x2,
46
        kReducedShaders                = 0x4,
47
    };
48
49
    explicit GrContextFactory(const GrContextOptions& opts);
50
    GrContextFactory();
51
52
    ~GrContextFactory();
53
54
    void destroyContexts();
55
    void abandonContexts();
56
    void releaseResourcesAndAbandonContexts();
57
58
    /**
59
     * Get a context initialized with a type of GL context. It also makes the GL context current.
60
     */
61
    ContextInfo getContextInfo(ContextType type, ContextOverrides = ContextOverrides::kNone);
62
63
    /**
64
     * Get a context in the same share group as the passed in GrContext, with the same type and
65
     * overrides. To get multiple contexts in a single share group, pass the same shareContext,
66
     * with different values for shareIndex.
67
     */
68
    ContextInfo getSharedContextInfo(GrDirectContext* shareContext, uint32_t shareIndex = 0);
69
70
    /**
71
     * Get a GrContext initialized with a type of GL context. It also makes the GL context current.
72
     */
73
    GrDirectContext* get(ContextType type, ContextOverrides overrides = ContextOverrides::kNone);
74
0
    const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; }
75
76
private:
77
    ContextInfo getContextInfoInternal(ContextType type, ContextOverrides overrides,
78
                                       GrDirectContext* shareContext, uint32_t shareIndex);
79
80
    struct Context {
81
        ContextType       fType;
82
        ContextOverrides  fOverrides;
83
        GrContextOptions  fOptions;
84
        GrBackendApi      fBackend;
85
        TestContext*      fTestContext;
86
        GrDirectContext*  fGrContext;
87
        GrDirectContext*  fShareContext;
88
        uint32_t          fShareIndex;
89
90
        bool              fAbandoned;
91
    };
92
    skia_private::TArray<Context, true> fContexts;
93
#ifdef SK_GL
94
    std::unique_ptr<GLTestContext>      fSentinelGLContext;
95
#endif
96
97
    const GrContextOptions              fGlobalOptions;
98
};
99
100
class ContextInfo {
101
public:
102
184
    ContextInfo() = default;
103
    ContextInfo(const ContextInfo&) = default;
104
    ContextInfo& operator=(const ContextInfo&) = default;
105
106
0
    skgpu::ContextType type() const { return fType; }
107
0
    GrBackendApi backend() const { return skgpu::ganesh::ContextTypeBackend(fType); }
108
109
2.76k
    GrDirectContext* directContext() const { return fContext; }
110
28
    TestContext* testContext() const { return fTestContext; }
111
112
#ifdef SK_GL
113
    GLTestContext* glContext() const {
114
        SkASSERT(GrBackendApi::kOpenGL == this->backend());
115
        return static_cast<GLTestContext*>(fTestContext);
116
    }
117
#endif
118
119
0
    const GrContextOptions& options() const { return fOptions; }
120
121
private:
122
    ContextInfo(skgpu::ContextType type,
123
                TestContext* testContext,
124
                GrDirectContext* context,
125
                const GrContextOptions& options)
126
2.58k
            : fType(type), fTestContext(testContext), fContext(context), fOptions(options) {}
127
128
    skgpu::ContextType fType = skgpu::ContextType::kGL;
129
    // Valid until the factory destroys it via abandonContexts() or destroyContexts().
130
    TestContext* fTestContext = nullptr;
131
    GrDirectContext* fContext = nullptr;
132
    GrContextOptions fOptions;
133
134
    friend class GrContextFactory;
135
};
136
137
}  // namespace sk_gpu_test
138
139
GR_MAKE_BITFIELD_CLASS_OPS(sk_gpu_test::GrContextFactory::ContextOverrides)
140
141
#endif