bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/gpu/GrContext.h" |
| 12 | #include "include/gpu/GrContextOptions.h" |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 13 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/private/SkTArray.h" |
| 15 | #include "tools/gpu/gl/GLTestContext.h" |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 16 | |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 17 | struct GrVkBackendContext; |
| 18 | |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 19 | namespace sk_gpu_test { |
Robert Phillips | cdabbcc | 2017-06-08 16:03:17 -0400 | [diff] [blame] | 20 | class ContextInfo; |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 21 | |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 22 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 23 | * This is a simple class that is useful in test apps that use different |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 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 |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 27 | * Gr and GL contexts to make them outlive the factory). |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 28 | */ |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 29 | class GrContextFactory : SkNoncopyable { |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 30 | public: |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 31 | // The availability of context types is subject to platform and build configuration |
| 32 | // restrictions. |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 33 | enum ContextType { |
bsalomon | 11abd8d | 2016-10-14 08:13:48 -0700 | [diff] [blame] | 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. |
bsalomon | 11abd8d | 2016-10-14 08:13:48 -0700 | [diff] [blame] | 42 | kVulkan_ContextType, //! Vulkan |
Greg Daniel | 2811aa2 | 2017-07-13 15:34:56 -0400 | [diff] [blame] | 43 | kMetal_ContextType, //! Metal |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 44 | kMock_ContextType, //! Mock context that does not draw. |
| 45 | kLastContextType = kMock_ContextType |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 48 | static const int kContextTypeCnt = kLastContextType + 1; |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 49 | |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 50 | /** |
csmartdalton | e812d49 | 2017-02-21 12:36:05 -0700 | [diff] [blame] | 51 | * Overrides for the initial GrContextOptions provided at construction time, and required |
| 52 | * features that will cause context creation to fail if not present. |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 53 | */ |
csmartdalton | e812d49 | 2017-02-21 12:36:05 -0700 | [diff] [blame] | 54 | enum class ContextOverrides { |
| 55 | kNone = 0x0, |
| 56 | kDisableNVPR = 0x1, |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 57 | kAvoidStencilBuffers = 0x2, |
csmartdalton | e812d49 | 2017-02-21 12:36:05 -0700 | [diff] [blame] | 58 | |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 59 | kRequireNVPRSupport = 0x4, |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 60 | }; |
| 61 | |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 62 | static bool IsRenderingContext(ContextType type) { |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 63 | switch (type) { |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 64 | case kMock_ContextType: |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 65 | return false; |
| 66 | default: |
| 67 | return true; |
| 68 | } |
| 69 | } |
| 70 | |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 71 | static GrBackendApi ContextTypeBackend(ContextType type) { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 72 | switch (type) { |
| 73 | case kVulkan_ContextType: |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 74 | return GrBackendApi::kVulkan; |
Greg Daniel | 2811aa2 | 2017-07-13 15:34:56 -0400 | [diff] [blame] | 75 | case kMetal_ContextType: |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 76 | return GrBackendApi::kMetal; |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 77 | case kMock_ContextType: |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 78 | return GrBackendApi::kMock; |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 79 | default: |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 80 | return GrBackendApi::kOpenGL; |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 81 | } |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 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"; |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 102 | case kVulkan_ContextType: |
| 103 | return "Vulkan"; |
Greg Daniel | 2811aa2 | 2017-07-13 15:34:56 -0400 | [diff] [blame] | 104 | case kMetal_ContextType: |
| 105 | return "Metal"; |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 106 | case kMock_ContextType: |
| 107 | return "Mock"; |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 108 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 109 | SK_ABORT("Unreachable"); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 110 | return "Unknown"; |
| 111 | } |
| 112 | |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 113 | explicit GrContextFactory(const GrContextOptions& opts); |
| 114 | GrContextFactory(); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 115 | |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 116 | ~GrContextFactory(); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 117 | |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 118 | void destroyContexts(); |
| 119 | void abandonContexts(); |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 120 | void releaseResourcesAndAbandonContexts(); |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 121 | |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 122 | /** |
| 123 | * Get a context initialized with a type of GL context. It also makes the GL context current. |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 124 | */ |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 125 | ContextInfo getContextInfo(ContextType type, |
Brian Osman | 9eac2ea | 2017-02-24 14:51:44 -0500 | [diff] [blame] | 126 | ContextOverrides overrides = 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 | |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 135 | /** |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 136 | * Get a GrContext initialized with a type of GL context. It also makes the GL context current. |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 137 | */ |
Robert Phillips | cdabbcc | 2017-06-08 16:03:17 -0400 | [diff] [blame] | 138 | GrContext* get(ContextType type, ContextOverrides overrides = ContextOverrides::kNone); |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 139 | const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; } |
krajcevski | b1aded8 | 2014-08-18 07:52:17 -0700 | [diff] [blame] | 140 | |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 141 | private: |
Brian Osman | 9eac2ea | 2017-02-24 14:51:44 -0500 | [diff] [blame] | 142 | ContextInfo getContextInfoInternal(ContextType type, ContextOverrides overrides, |
| 143 | GrContext* shareContext, uint32_t shareIndex); |
| 144 | |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 145 | struct Context { |
Robert Phillips | 08ba085 | 2019-05-22 20:23:43 +0000 | [diff] [blame] | 146 | ContextType fType; |
| 147 | ContextOverrides fOverrides; |
| 148 | GrContextOptions fOptions; |
Robert Phillips | 27eb525 | 2019-06-03 12:59:40 -0400 | [diff] [blame] | 149 | GrBackendApi fBackend; |
Robert Phillips | 08ba085 | 2019-05-22 20:23:43 +0000 | [diff] [blame] | 150 | TestContext* fTestContext; |
| 151 | GrContext* fGrContext; |
| 152 | GrContext* fShareContext; |
| 153 | uint32_t fShareIndex; |
Brian Osman | 60c774d | 2017-02-21 16:58:08 -0500 | [diff] [blame] | 154 | |
Robert Phillips | 27eb525 | 2019-06-03 12:59:40 -0400 | [diff] [blame] | 155 | bool fAbandoned; |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 156 | }; |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 157 | SkTArray<Context, true> fContexts; |
Brian Salomon | 7f9c29a | 2017-01-24 22:22:05 +0000 | [diff] [blame] | 158 | std::unique_ptr<GLTestContext> fSentinelGLContext; |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 159 | const GrContextOptions fGlobalOptions; |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 160 | }; |
Robert Phillips | cdabbcc | 2017-06-08 16:03:17 -0400 | [diff] [blame] | 161 | |
| 162 | class ContextInfo { |
| 163 | public: |
| 164 | ContextInfo() = default; |
| 165 | ContextInfo& operator=(const ContextInfo&) = default; |
| 166 | |
| 167 | GrContextFactory::ContextType type() const { return fType; } |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 168 | GrBackendApi backend() const { return GrContextFactory::ContextTypeBackend(fType); } |
Robert Phillips | cdabbcc | 2017-06-08 16:03:17 -0400 | [diff] [blame] | 169 | |
| 170 | GrContext* grContext() const { return fGrContext; } |
| 171 | |
| 172 | TestContext* testContext() const { return fTestContext; } |
| 173 | |
| 174 | GLTestContext* glContext() const { |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 175 | SkASSERT(GrBackendApi::kOpenGL == this->backend()); |
Robert Phillips | cdabbcc | 2017-06-08 16:03:17 -0400 | [diff] [blame] | 176 | return static_cast<GLTestContext*>(fTestContext); |
| 177 | } |
| 178 | |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 179 | const GrContextOptions& options() const { return fOptions; } |
| 180 | |
Robert Phillips | cdabbcc | 2017-06-08 16:03:17 -0400 | [diff] [blame] | 181 | private: |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 182 | ContextInfo(GrContextFactory::ContextType type, TestContext* testContext, GrContext* grContext, |
| 183 | const GrContextOptions& options) |
| 184 | : fType(type), fTestContext(testContext), fGrContext(grContext), fOptions(options) {} |
Robert Phillips | cdabbcc | 2017-06-08 16:03:17 -0400 | [diff] [blame] | 185 | |
| 186 | GrContextFactory::ContextType fType = GrContextFactory::kGL_ContextType; |
| 187 | // Valid until the factory destroys it via abandonContexts() or destroyContexts(). |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 188 | TestContext* fTestContext = nullptr; |
| 189 | GrContext* fGrContext = nullptr; |
| 190 | GrContextOptions fOptions; |
Robert Phillips | cdabbcc | 2017-06-08 16:03:17 -0400 | [diff] [blame] | 191 | |
| 192 | friend class GrContextFactory; |
| 193 | }; |
| 194 | |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 195 | } // namespace sk_gpu_test |
csmartdalton | 6270e55 | 2016-09-13 10:41:49 -0700 | [diff] [blame] | 196 | |
csmartdalton | e812d49 | 2017-02-21 12:36:05 -0700 | [diff] [blame] | 197 | GR_MAKE_BITFIELD_CLASS_OPS(sk_gpu_test::GrContextFactory::ContextOverrides); |
csmartdalton | 6270e55 | 2016-09-13 10:41:49 -0700 | [diff] [blame] | 198 | |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 199 | #endif |