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