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 kVulkan_ContextType, //! Vulkan 44 kMetal_ContextType, //! Metal 45 kMock_ContextType, //! Mock context that does not draw. 46 kLastContextType = kMock_ContextType 47 }; 48 49 static const int kContextTypeCnt = kLastContextType + 1; 50 51 /** 52 * Overrides for the initial GrContextOptions provided at construction time, and required 53 * features that will cause context creation to fail if not present. 54 */ 55 enum class ContextOverrides { 56 kNone = 0x0, 57 kDisableNVPR = 0x1, 58 kAvoidStencilBuffers = 0x2, 59 60 kRequireNVPRSupport = 0x4, 61 }; 62 IsRenderingContext(ContextType type)63 static bool IsRenderingContext(ContextType type) { 64 switch (type) { 65 case kNullGL_ContextType: 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 kMock_ContextType: 80 return GrBackendApi::kMock; 81 default: 82 return GrBackendApi::kOpenGL; 83 } 84 } 85 ContextTypeName(ContextType contextType)86 static const char* ContextTypeName(ContextType contextType) { 87 switch (contextType) { 88 case kGL_ContextType: 89 return "OpenGL"; 90 case kGLES_ContextType: 91 return "OpenGLES"; 92 case kANGLE_D3D9_ES2_ContextType: 93 return "ANGLE D3D9 ES2"; 94 case kANGLE_D3D11_ES2_ContextType: 95 return "ANGLE D3D11 ES2"; 96 case kANGLE_D3D11_ES3_ContextType: 97 return "ANGLE D3D11 ES3"; 98 case kANGLE_GL_ES2_ContextType: 99 return "ANGLE GL ES2"; 100 case kANGLE_GL_ES3_ContextType: 101 return "ANGLE GL ES3"; 102 case kCommandBuffer_ContextType: 103 return "Command Buffer"; 104 case kNullGL_ContextType: 105 return "Null GL"; 106 case kVulkan_ContextType: 107 return "Vulkan"; 108 case kMetal_ContextType: 109 return "Metal"; 110 case kMock_ContextType: 111 return "Mock"; 112 } 113 SK_ABORT("Unreachable"); 114 return "Unknown"; 115 } 116 117 explicit GrContextFactory(const GrContextOptions& opts); 118 GrContextFactory(); 119 120 ~GrContextFactory(); 121 122 void destroyContexts(); 123 void abandonContexts(); 124 void releaseResourcesAndAbandonContexts(); 125 126 /** 127 * Get a context initialized with a type of GL context. It also makes the GL context current. 128 */ 129 ContextInfo getContextInfo(ContextType type, 130 ContextOverrides overrides = ContextOverrides::kNone); 131 132 /** 133 * Get a context in the same share group as the passed in GrContext, with the same type and 134 * overrides. To get multiple contexts in a single share group, pass the same shareContext, 135 * with different values for shareIndex. 136 */ 137 ContextInfo getSharedContextInfo(GrContext* shareContext, uint32_t shareIndex = 0); 138 139 /** 140 * Get a GrContext initialized with a type of GL context. It also makes the GL context current. 141 */ 142 GrContext* get(ContextType type, ContextOverrides overrides = ContextOverrides::kNone); getGlobalOptions()143 const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; } 144 145 private: 146 ContextInfo getContextInfoInternal(ContextType type, ContextOverrides overrides, 147 GrContext* shareContext, uint32_t shareIndex); 148 149 struct Context { 150 ContextType fType; 151 ContextOverrides fOverrides; 152 GrContextOptions fOptions; 153 GrBackendApi fBackend; 154 TestContext* fTestContext; 155 GrContext* fGrContext; 156 GrContext* fShareContext; 157 uint32_t fShareIndex; 158 159 bool fAbandoned; 160 }; 161 SkTArray<Context, true> fContexts; 162 std::unique_ptr<GLTestContext> fSentinelGLContext; 163 const GrContextOptions fGlobalOptions; 164 }; 165 166 class ContextInfo { 167 public: 168 ContextInfo() = default; 169 ContextInfo& operator=(const ContextInfo&) = default; 170 type()171 GrContextFactory::ContextType type() const { return fType; } backend()172 GrBackendApi backend() const { return GrContextFactory::ContextTypeBackend(fType); } 173 grContext()174 GrContext* grContext() const { return fGrContext; } 175 testContext()176 TestContext* testContext() const { return fTestContext; } 177 glContext()178 GLTestContext* glContext() const { 179 SkASSERT(GrBackendApi::kOpenGL == this->backend()); 180 return static_cast<GLTestContext*>(fTestContext); 181 } 182 options()183 const GrContextOptions& options() const { return fOptions; } 184 185 private: ContextInfo(GrContextFactory::ContextType type,TestContext * testContext,GrContext * grContext,const GrContextOptions & options)186 ContextInfo(GrContextFactory::ContextType type, TestContext* testContext, GrContext* grContext, 187 const GrContextOptions& options) 188 : fType(type), fTestContext(testContext), fGrContext(grContext), fOptions(options) {} 189 190 GrContextFactory::ContextType fType = GrContextFactory::kGL_ContextType; 191 // Valid until the factory destroys it via abandonContexts() or destroyContexts(). 192 TestContext* fTestContext = nullptr; 193 GrContext* fGrContext = nullptr; 194 GrContextOptions fOptions; 195 196 friend class GrContextFactory; 197 }; 198 199 } // namespace sk_gpu_test 200 201 GR_MAKE_BITFIELD_CLASS_OPS(sk_gpu_test::GrContextFactory::ContextOverrides); 202 203 #endif 204