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/ganesh/GrContextOptions.h" 12 #include "include/gpu/ganesh/GrDirectContext.h" 13 14 #include "include/private/base/SkTArray.h" 15 #include "tools/gpu/ContextType.h" 16 #include "tools/gpu/TestContext.h" 17 18 #ifdef SK_GL 19 #include "tools/gpu/gl/GLTestContext.h" 20 #endif 21 22 namespace skgpu { 23 struct VulkanBackendContext; 24 } 25 26 namespace sk_gpu_test { 27 class ContextInfo; 28 29 /** 30 * This is a simple class that is useful in test apps that use different 31 * GrContexts backed by different types of GL contexts. It manages creating the 32 * GL context and a GrContext that uses it. The GL/Gr contexts persist until the 33 * factory is destroyed (though the caller can always grab a ref on the returned 34 * Gr and GL contexts to make them outlive the factory). 35 */ 36 class GrContextFactory : SkNoncopyable { 37 public: 38 using ContextType = skgpu::ContextType; 39 40 /** 41 * Overrides for the initial GrContextOptions provided at construction time, and required 42 * features that will cause context creation to fail if not present. 43 */ 44 enum class ContextOverrides { 45 kNone = 0x0, 46 kAvoidStencilBuffers = 0x1, 47 kFakeGLESVersionAs2 = 0x2, 48 kReducedShaders = 0x4, 49 }; 50 51 explicit GrContextFactory(const GrContextOptions& opts); 52 GrContextFactory(); 53 54 ~GrContextFactory(); 55 56 void destroyContexts(); 57 void abandonContexts(); 58 void releaseResourcesAndAbandonContexts(); 59 60 /** 61 * Get a context initialized with a type of GL context. It also makes the GL context current. 62 */ 63 ContextInfo getContextInfo(ContextType type, ContextOverrides = ContextOverrides::kNone); 64 65 /** 66 * Get a context in the same share group as the passed in GrContext, with the same type and 67 * overrides. To get multiple contexts in a single share group, pass the same shareContext, 68 * with different values for shareIndex. 69 */ 70 ContextInfo getSharedContextInfo(GrDirectContext* shareContext, uint32_t shareIndex = 0); 71 72 /** 73 * Get a GrContext initialized with a type of GL context. It also makes the GL context current. 74 */ 75 GrDirectContext* get(ContextType type, ContextOverrides overrides = ContextOverrides::kNone); getGlobalOptions()76 const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; } 77 78 private: 79 ContextInfo getContextInfoInternal(ContextType type, ContextOverrides overrides, 80 GrDirectContext* shareContext, uint32_t shareIndex); 81 82 struct Context { 83 ContextType fType; 84 ContextOverrides fOverrides; 85 GrContextOptions fOptions; 86 GrBackendApi fBackend; 87 TestContext* fTestContext; 88 GrDirectContext* fGrContext; 89 GrDirectContext* fShareContext; 90 uint32_t fShareIndex; 91 92 bool fAbandoned; 93 }; 94 skia_private::TArray<Context, true> fContexts; 95 #ifdef SK_GL 96 std::unique_ptr<GLTestContext> fSentinelGLContext; 97 #endif 98 99 const GrContextOptions fGlobalOptions; 100 }; 101 102 class ContextInfo { 103 public: 104 ContextInfo() = default; 105 ContextInfo(const ContextInfo&) = default; 106 ContextInfo& operator=(const ContextInfo&) = default; 107 type()108 skgpu::ContextType type() const { return fType; } backend()109 GrBackendApi backend() const { return skgpu::ganesh::ContextTypeBackend(fType); } 110 directContext()111 GrDirectContext* directContext() const { return fContext; } testContext()112 TestContext* testContext() const { return fTestContext; } 113 114 #ifdef SK_GL glContext()115 GLTestContext* glContext() const { 116 SkASSERT(GrBackendApi::kOpenGL == this->backend()); 117 return static_cast<GLTestContext*>(fTestContext); 118 } 119 #endif 120 options()121 const GrContextOptions& options() const { return fOptions; } 122 123 private: ContextInfo(skgpu::ContextType type,TestContext * testContext,GrDirectContext * context,const GrContextOptions & options)124 ContextInfo(skgpu::ContextType type, 125 TestContext* testContext, 126 GrDirectContext* context, 127 const GrContextOptions& options) 128 : fType(type), fTestContext(testContext), fContext(context), fOptions(options) {} 129 130 skgpu::ContextType fType = skgpu::ContextType::kGL; 131 // Valid until the factory destroys it via abandonContexts() or destroyContexts(). 132 TestContext* fTestContext = nullptr; 133 GrDirectContext* fContext = nullptr; 134 GrContextOptions fOptions; 135 136 friend class GrContextFactory; 137 }; 138 139 } // namespace sk_gpu_test 140 141 SK_MAKE_BITFIELD_CLASS_OPS(sk_gpu_test::GrContextFactory::ContextOverrides) 142 143 #endif 144