GrContextFactory.h 7.0 KB

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