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 "GrContext.h"
12 #include "GrContextOptions.h"
13 
14 #include "gl/GLTestContext.h"
15 #include "SkTArray.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_ContextType,  //! Chromium command buffer OpenGL ES context.
42         kNullGL_ContextType,         //! Non-rendering OpenGL mock context.
43         kDebugGL_ContextType,        //! Non-rendering, state verifying OpenGL context.
44         kVulkan_ContextType,         //! Vulkan
45         kMetal_ContextType,          //! Metal
46         kMock_ContextType,           //! Mock context that does not draw.
47         kLastContextType = kMock_ContextType
48     };
49 
50     static const int kContextTypeCnt = kLastContextType + 1;
51 
52     /**
53      * Overrides for the initial GrContextOptions provided at construction time, and required
54      * features that will cause context creation to fail if not present.
55      */
56     enum class ContextOverrides {
57         kNone                          = 0x0,
58         kDisableNVPR                   = 0x1,
59         kAllowSRGBWithoutDecodeControl = 0x2,
60         kAvoidStencilBuffers           = 0x4,
61 
62         kRequireNVPRSupport            = 0x8,
63         kRequireSRGBSupport            = 0x10,
64     };
65 
IsRenderingContext(ContextType type)66     static bool IsRenderingContext(ContextType type) {
67         switch (type) {
68             case kNullGL_ContextType:
69             case kDebugGL_ContextType:
70             case kMock_ContextType:
71                 return false;
72             default:
73                 return true;
74         }
75     }
76 
ContextTypeBackend(ContextType type)77     static GrBackend ContextTypeBackend(ContextType type) {
78         switch (type) {
79             case kVulkan_ContextType:
80                 return kVulkan_GrBackend;
81             case kMetal_ContextType:
82                 return kMetal_GrBackend;
83             case kMock_ContextType:
84                 return kMock_GrBackend;
85             default:
86                 return kOpenGL_GrBackend;
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_ContextType:
107                 return "Command Buffer";
108             case kNullGL_ContextType:
109                 return "Null GL";
110             case kDebugGL_ContextType:
111                 return "Debug GL";
112             case kVulkan_ContextType:
113                 return "Vulkan";
114             case kMetal_ContextType:
115                 return "Metal";
116             case kMock_ContextType:
117                 return "Mock";
118         }
119         SK_ABORT("Unreachable");
120         return "Unknown";
121     }
122 
123     explicit GrContextFactory(const GrContextOptions& opts);
124     GrContextFactory();
125 
126     ~GrContextFactory();
127 
128     void destroyContexts();
129     void abandonContexts();
130     void releaseResourcesAndAbandonContexts();
131 
132     /**
133      * Get a context initialized with a type of GL context. It also makes the GL context current.
134      */
135     ContextInfo getContextInfo(ContextType type,
136                                ContextOverrides overrides = 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(GrContext* 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     GrContext* 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                                        GrContext* shareContext, uint32_t shareIndex);
154 
155     struct Context {
156         ContextType       fType;
157         ContextOverrides  fOverrides;
158         GrContextOptions  fOptions;
159         GrBackend         fBackend;
160         TestContext*      fTestContext;
161         GrContext*        fGrContext;
162         GrContext*        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& operator=(const ContextInfo&) = default;
176 
type()177     GrContextFactory::ContextType type() const { return fType; }
backend()178     GrBackend backend() const { return GrContextFactory::ContextTypeBackend(fType); }
179 
grContext()180     GrContext* grContext() const { return fGrContext; }
181 
testContext()182     TestContext* testContext() const { return fTestContext; }
183 
glContext()184     GLTestContext* glContext() const {
185         SkASSERT(kOpenGL_GrBackend == this->backend());
186         return static_cast<GLTestContext*>(fTestContext);
187     }
188 
options()189     const GrContextOptions& options() const { return fOptions; }
190 
191 private:
ContextInfo(GrContextFactory::ContextType type,TestContext * testContext,GrContext * grContext,const GrContextOptions & options)192     ContextInfo(GrContextFactory::ContextType type, TestContext* testContext, GrContext* grContext,
193                 const GrContextOptions& options)
194             : fType(type), fTestContext(testContext), fGrContext(grContext), fOptions(options) {}
195 
196     GrContextFactory::ContextType fType = GrContextFactory::kGL_ContextType;
197     // Valid until the factory destroys it via abandonContexts() or destroyContexts().
198     TestContext* fTestContext = nullptr;
199     GrContext* fGrContext = nullptr;
200     GrContextOptions fOptions;
201 
202     friend class GrContextFactory;
203 };
204 
205 }  // namespace sk_gpu_test
206 
207 GR_MAKE_BITFIELD_CLASS_OPS(sk_gpu_test::GrContextFactory::ContextOverrides);
208 
209 #endif
210