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/GrContext.h" 12 #include "include/gpu/GrContextOptions.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_ContextType, //! Chromium command buffer OpenGL ES context. 42 kVulkan_ContextType, //! Vulkan 43 kMetal_ContextType, //! Metal 44 kDawn_ContextType, //! Dawn 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 kAvoidStencilBuffers = 0x1, 58 }; 59 IsRenderingContext(ContextType type)60 static bool IsRenderingContext(ContextType type) { 61 switch (type) { 62 case kMock_ContextType: 63 return false; 64 default: 65 return true; 66 } 67 } 68 ContextTypeBackend(ContextType type)69 static GrBackendApi ContextTypeBackend(ContextType type) { 70 switch (type) { 71 case kVulkan_ContextType: 72 return GrBackendApi::kVulkan; 73 case kMetal_ContextType: 74 return GrBackendApi::kMetal; 75 case kDawn_ContextType: 76 return GrBackendApi::kDawn; 77 case kMock_ContextType: 78 return GrBackendApi::kMock; 79 default: 80 return GrBackendApi::kOpenGL; 81 } 82 } 83 ContextTypeName(ContextType contextType)84 static const char* ContextTypeName(ContextType contextType) { 85 switch (contextType) { 86 case kGL_ContextType: 87 return "OpenGL"; 88 case kGLES_ContextType: 89 return "OpenGLES"; 90 case kANGLE_D3D9_ES2_ContextType: 91 return "ANGLE D3D9 ES2"; 92 case kANGLE_D3D11_ES2_ContextType: 93 return "ANGLE D3D11 ES2"; 94 case kANGLE_D3D11_ES3_ContextType: 95 return "ANGLE D3D11 ES3"; 96 case kANGLE_GL_ES2_ContextType: 97 return "ANGLE GL ES2"; 98 case kANGLE_GL_ES3_ContextType: 99 return "ANGLE GL ES3"; 100 case kCommandBuffer_ContextType: 101 return "Command Buffer"; 102 case kVulkan_ContextType: 103 return "Vulkan"; 104 case kMetal_ContextType: 105 return "Metal"; 106 case kDawn_ContextType: 107 return "Dawn"; 108 case kMock_ContextType: 109 return "Mock"; 110 } 111 SK_ABORT("Unreachable"); 112 } 113 114 explicit GrContextFactory(const GrContextOptions& opts); 115 GrContextFactory(); 116 117 ~GrContextFactory(); 118 119 void destroyContexts(); 120 void abandonContexts(); 121 void releaseResourcesAndAbandonContexts(); 122 123 /** 124 * Get a context initialized with a type of GL context. It also makes the GL context current. 125 */ 126 ContextInfo getContextInfo(ContextType type, ContextOverrides = ContextOverrides::kNone); 127 128 /** 129 * Get a context in the same share group as the passed in GrContext, with the same type and 130 * overrides. To get multiple contexts in a single share group, pass the same shareContext, 131 * with different values for shareIndex. 132 */ 133 ContextInfo getSharedContextInfo(GrContext* shareContext, uint32_t shareIndex = 0); 134 135 /** 136 * Get a GrContext initialized with a type of GL context. It also makes the GL context current. 137 */ 138 GrContext* get(ContextType type, ContextOverrides overrides = ContextOverrides::kNone); getGlobalOptions()139 const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; } 140 141 private: 142 ContextInfo getContextInfoInternal(ContextType type, ContextOverrides overrides, 143 GrContext* shareContext, uint32_t shareIndex); 144 145 struct Context { 146 ContextType fType; 147 ContextOverrides fOverrides; 148 GrContextOptions fOptions; 149 GrBackendApi fBackend; 150 TestContext* fTestContext; 151 GrContext* fGrContext; 152 GrContext* fShareContext; 153 uint32_t fShareIndex; 154 155 bool fAbandoned; 156 }; 157 SkTArray<Context, true> fContexts; 158 std::unique_ptr<GLTestContext> fSentinelGLContext; 159 const GrContextOptions fGlobalOptions; 160 }; 161 162 class ContextInfo { 163 public: 164 ContextInfo() = default; 165 ContextInfo& operator=(const ContextInfo&) = default; 166 type()167 GrContextFactory::ContextType type() const { return fType; } backend()168 GrBackendApi backend() const { return GrContextFactory::ContextTypeBackend(fType); } 169 grContext()170 GrContext* grContext() const { return fGrContext; } 171 testContext()172 TestContext* testContext() const { return fTestContext; } 173 glContext()174 GLTestContext* glContext() const { 175 SkASSERT(GrBackendApi::kOpenGL == this->backend()); 176 return static_cast<GLTestContext*>(fTestContext); 177 } 178 options()179 const GrContextOptions& options() const { return fOptions; } 180 181 private: ContextInfo(GrContextFactory::ContextType type,TestContext * testContext,GrContext * grContext,const GrContextOptions & options)182 ContextInfo(GrContextFactory::ContextType type, TestContext* testContext, GrContext* grContext, 183 const GrContextOptions& options) 184 : fType(type), fTestContext(testContext), fGrContext(grContext), fOptions(options) {} 185 186 GrContextFactory::ContextType fType = GrContextFactory::kGL_ContextType; 187 // Valid until the factory destroys it via abandonContexts() or destroyContexts(). 188 TestContext* fTestContext = nullptr; 189 GrContext* fGrContext = nullptr; 190 GrContextOptions fOptions; 191 192 friend class GrContextFactory; 193 }; 194 195 } // namespace sk_gpu_test 196 197 GR_MAKE_BITFIELD_CLASS_OPS(sk_gpu_test::GrContextFactory::ContextOverrides); 198 199 #endif 200