• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkTArray.h"
15 #include "tools/gpu/gl/GLTestContext.h"
16 
17 struct GrVkBackendContext;
18 
19 namespace sk_gpu_test {
20 class ContextInfo;
21 
22 /**
23  * This is a simple class that is useful in test apps that use different
24  * GrContexts backed by different types of GL contexts. It manages creating the
25  * GL context and a GrContext that uses it. The GL/Gr contexts persist until the
26  * factory is destroyed (though the caller can always grab a ref on the returned
27  * Gr and GL contexts to make them outlive the factory).
28  */
29 class GrContextFactory : SkNoncopyable {
30 public:
31     // The availability of context types is subject to platform and build configuration
32     // restrictions.
33     enum ContextType {
34         kGL_ContextType,                 //! OpenGL context.
35         kGLES_ContextType,               //! OpenGL ES context.
36         kANGLE_D3D9_ES2_ContextType,     //! ANGLE on Direct3D9 OpenGL ES 2 context.
37         kANGLE_D3D11_ES2_ContextType,    //! ANGLE on Direct3D11 OpenGL ES 2 context.
38         kANGLE_D3D11_ES3_ContextType,    //! ANGLE on Direct3D11 OpenGL ES 3 context.
39         kANGLE_GL_ES2_ContextType,       //! ANGLE on OpenGL OpenGL ES 2 context.
40         kANGLE_GL_ES3_ContextType,       //! ANGLE on OpenGL OpenGL ES 3 context.
41         kCommandBuffer_ES2_ContextType,  //! Chromium command buffer OpenGL ES 2 context.
42         kCommandBuffer_ES3_ContextType,  //! Chromium command buffer OpenGL ES 3 context.
43         kVulkan_ContextType,             //! Vulkan
44         kMetal_ContextType,              //! Metal
45         kDirect3D_ContextType,           //! Direct3D 12
46         kDawn_ContextType,               //! Dawn
47         kMock_ContextType,               //! Mock context that does not draw.
48         kLastContextType = kMock_ContextType
49     };
50 
51     static const int kContextTypeCnt = kLastContextType + 1;
52 
53     /**
54      * Overrides for the initial GrContextOptions provided at construction time, and required
55      * features that will cause context creation to fail if not present.
56      */
57     enum class ContextOverrides {
58         kNone                          = 0x0,
59         kAvoidStencilBuffers           = 0x1,
60         kFakeGLESVersionAs2            = 0x2,
61         kReducedShaders                = 0x4,
62     };
63 
IsRenderingContext(ContextType type)64     static bool IsRenderingContext(ContextType type) {
65         switch (type) {
66             case kMock_ContextType:
67                 return false;
68             default:
69                 return true;
70         }
71     }
72 
ContextTypeBackend(ContextType type)73     static GrBackendApi ContextTypeBackend(ContextType type) {
74         switch (type) {
75             case kVulkan_ContextType:
76                 return GrBackendApi::kVulkan;
77             case kMetal_ContextType:
78                 return GrBackendApi::kMetal;
79             case kDirect3D_ContextType:
80                 return GrBackendApi::kDirect3D;
81             case kDawn_ContextType:
82                 return GrBackendApi::kDawn;
83             case kMock_ContextType:
84                 return GrBackendApi::kMock;
85             default:
86                 return GrBackendApi::kOpenGL;
87         }
88     }
89 
ContextTypeName(ContextType contextType)90     static const char* ContextTypeName(ContextType contextType) {
91         switch (contextType) {
92             case kGL_ContextType:
93                 return "OpenGL";
94             case kGLES_ContextType:
95                 return "OpenGLES";
96             case kANGLE_D3D9_ES2_ContextType:
97                 return "ANGLE D3D9 ES2";
98             case kANGLE_D3D11_ES2_ContextType:
99                 return "ANGLE D3D11 ES2";
100             case kANGLE_D3D11_ES3_ContextType:
101                 return "ANGLE D3D11 ES3";
102             case kANGLE_GL_ES2_ContextType:
103                 return "ANGLE GL ES2";
104             case kANGLE_GL_ES3_ContextType:
105                 return "ANGLE GL ES3";
106             case kCommandBuffer_ES2_ContextType:
107                 return "Command Buffer ES2";
108             case kCommandBuffer_ES3_ContextType:
109                 return "Command Buffer ES3";
110             case kVulkan_ContextType:
111                 return "Vulkan";
112             case kMetal_ContextType:
113                 return "Metal";
114             case kDirect3D_ContextType:
115                 return "Direct3D";
116             case kDawn_ContextType:
117                 return "Dawn";
118             case kMock_ContextType:
119                 return "Mock";
120         }
121         SK_ABORT("Unreachable");
122     }
123 
124     explicit GrContextFactory(const GrContextOptions& opts);
125     GrContextFactory();
126 
127     ~GrContextFactory();
128 
129     void destroyContexts();
130     void abandonContexts();
131     void releaseResourcesAndAbandonContexts();
132 
133     /**
134      * Get a context initialized with a type of GL context. It also makes the GL context current.
135      */
136     ContextInfo getContextInfo(ContextType type, ContextOverrides = ContextOverrides::kNone);
137 
138     /**
139      * Get a context in the same share group as the passed in GrContext, with the same type and
140      * overrides. To get multiple contexts in a single share group, pass the same shareContext,
141      * with different values for shareIndex.
142      */
143     ContextInfo getSharedContextInfo(GrDirectContext* shareContext, uint32_t shareIndex = 0);
144 
145     /**
146      * Get a GrContext initialized with a type of GL context. It also makes the GL context current.
147      */
148     GrDirectContext* get(ContextType type, ContextOverrides overrides = ContextOverrides::kNone);
getGlobalOptions()149     const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; }
150 
151 private:
152     ContextInfo getContextInfoInternal(ContextType type, ContextOverrides overrides,
153                                        GrDirectContext* shareContext, uint32_t shareIndex);
154 
155     struct Context {
156         ContextType       fType;
157         ContextOverrides  fOverrides;
158         GrContextOptions  fOptions;
159         GrBackendApi      fBackend;
160         TestContext*      fTestContext;
161         GrDirectContext*  fGrContext;
162         GrDirectContext*  fShareContext;
163         uint32_t          fShareIndex;
164 
165         bool              fAbandoned;
166     };
167     SkTArray<Context, true>         fContexts;
168     std::unique_ptr<GLTestContext>  fSentinelGLContext;
169     const GrContextOptions          fGlobalOptions;
170 };
171 
172 class ContextInfo {
173 public:
174     ContextInfo() = default;
175     ContextInfo(const ContextInfo&) = default;
176     ContextInfo& operator=(const ContextInfo&) = default;
177 
type()178     GrContextFactory::ContextType type() const { return fType; }
backend()179     GrBackendApi backend() const { return GrContextFactory::ContextTypeBackend(fType); }
180 
directContext()181     GrDirectContext* directContext() const { return fContext; }
testContext()182     TestContext* testContext() const { return fTestContext; }
183 
184 #ifdef SK_GL
glContext()185     GLTestContext* glContext() const {
186         SkASSERT(GrBackendApi::kOpenGL == this->backend());
187         return static_cast<GLTestContext*>(fTestContext);
188     }
189 #endif
190 
options()191     const GrContextOptions& options() const { return fOptions; }
192 
193 private:
ContextInfo(GrContextFactory::ContextType type,TestContext * testContext,GrDirectContext * context,const GrContextOptions & options)194     ContextInfo(GrContextFactory::ContextType type,
195                 TestContext* testContext,
196                 GrDirectContext* context,
197                 const GrContextOptions& options)
198             : fType(type), fTestContext(testContext), fContext(context), fOptions(options) {}
199 
200     GrContextFactory::ContextType fType = GrContextFactory::kGL_ContextType;
201     // Valid until the factory destroys it via abandonContexts() or destroyContexts().
202     TestContext* fTestContext = nullptr;
203     GrDirectContext* fContext = nullptr;
204     GrContextOptions fOptions;
205 
206     friend class GrContextFactory;
207 };
208 
209 }  // namespace sk_gpu_test
210 
211 GR_MAKE_BITFIELD_CLASS_OPS(sk_gpu_test::GrContextFactory::ContextOverrides)
212 
213 #endif
214