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 |
Jim Van Verth | b01e12b | 2020-02-18 14:34:38 -0500 | [diff] [blame] | 44 | kDirect3D_ContextType, //! Direct3D 12 |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 45 | kDawn_ContextType, //! Dawn |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 46 | kMock_ContextType, //! Mock context that does not draw. |
| 47 | kLastContextType = kMock_ContextType |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 50 | static const int kContextTypeCnt = kLastContextType + 1; |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 51 | |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 52 | /** |
csmartdalton | e812d49 | 2017-02-21 12:36:05 -0700 | [diff] [blame] | 53 | * Overrides for the initial GrContextOptions provided at construction time, and required |
| 54 | * features that will cause context creation to fail if not present. |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 55 | */ |
csmartdalton | e812d49 | 2017-02-21 12:36:05 -0700 | [diff] [blame] | 56 | enum class ContextOverrides { |
| 57 | kNone = 0x0, |
Chris Dalton | b3c9745 | 2019-06-25 20:07:56 -0600 | [diff] [blame] | 58 | kAvoidStencilBuffers = 0x1, |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 59 | }; |
| 60 | |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 61 | static bool IsRenderingContext(ContextType type) { |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 62 | switch (type) { |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 63 | case kMock_ContextType: |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 64 | return false; |
| 65 | default: |
| 66 | return true; |
| 67 | } |
| 68 | } |
| 69 | |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 70 | static GrBackendApi ContextTypeBackend(ContextType type) { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 71 | switch (type) { |
| 72 | case kVulkan_ContextType: |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 73 | return GrBackendApi::kVulkan; |
Greg Daniel | 2811aa2 | 2017-07-13 15:34:56 -0400 | [diff] [blame] | 74 | case kMetal_ContextType: |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 75 | return GrBackendApi::kMetal; |
Jim Van Verth | b01e12b | 2020-02-18 14:34:38 -0500 | [diff] [blame] | 76 | case kDirect3D_ContextType: |
| 77 | return GrBackendApi::kDirect3D; |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 78 | case kDawn_ContextType: |
| 79 | return GrBackendApi::kDawn; |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 80 | case kMock_ContextType: |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 81 | return GrBackendApi::kMock; |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 82 | default: |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 83 | return GrBackendApi::kOpenGL; |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 84 | } |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 87 | static const char* ContextTypeName(ContextType contextType) { |
| 88 | switch (contextType) { |
| 89 | case kGL_ContextType: |
| 90 | return "OpenGL"; |
| 91 | case kGLES_ContextType: |
| 92 | return "OpenGLES"; |
| 93 | case kANGLE_D3D9_ES2_ContextType: |
| 94 | return "ANGLE D3D9 ES2"; |
| 95 | case kANGLE_D3D11_ES2_ContextType: |
| 96 | return "ANGLE D3D11 ES2"; |
| 97 | case kANGLE_D3D11_ES3_ContextType: |
| 98 | return "ANGLE D3D11 ES3"; |
| 99 | case kANGLE_GL_ES2_ContextType: |
| 100 | return "ANGLE GL ES2"; |
| 101 | case kANGLE_GL_ES3_ContextType: |
| 102 | return "ANGLE GL ES3"; |
| 103 | case kCommandBuffer_ContextType: |
| 104 | return "Command Buffer"; |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 105 | case kVulkan_ContextType: |
| 106 | return "Vulkan"; |
Greg Daniel | 2811aa2 | 2017-07-13 15:34:56 -0400 | [diff] [blame] | 107 | case kMetal_ContextType: |
| 108 | return "Metal"; |
Jim Van Verth | b01e12b | 2020-02-18 14:34:38 -0500 | [diff] [blame] | 109 | case kDirect3D_ContextType: |
| 110 | return "Direct3D"; |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 111 | case kDawn_ContextType: |
| 112 | return "Dawn"; |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 113 | case kMock_ContextType: |
| 114 | return "Mock"; |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 115 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 116 | SK_ABORT("Unreachable"); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 117 | } |
| 118 | |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 119 | explicit GrContextFactory(const GrContextOptions& opts); |
| 120 | GrContextFactory(); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 121 | |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 122 | ~GrContextFactory(); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 123 | |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 124 | void destroyContexts(); |
| 125 | void abandonContexts(); |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 126 | void releaseResourcesAndAbandonContexts(); |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 127 | |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 128 | /** |
| 129 | * 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] | 130 | */ |
Chris Dalton | b3c9745 | 2019-06-25 20:07:56 -0600 | [diff] [blame] | 131 | ContextInfo getContextInfo(ContextType type, ContextOverrides = ContextOverrides::kNone); |
Brian Osman | 9eac2ea | 2017-02-24 14:51:44 -0500 | [diff] [blame] | 132 | |
| 133 | /** |
| 134 | * Get a context in the same share group as the passed in GrContext, with the same type and |
| 135 | * overrides. To get multiple contexts in a single share group, pass the same shareContext, |
| 136 | * with different values for shareIndex. |
| 137 | */ |
| 138 | ContextInfo getSharedContextInfo(GrContext* shareContext, uint32_t shareIndex = 0); |
| 139 | |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 140 | /** |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 141 | * 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] | 142 | */ |
Robert Phillips | cdabbcc | 2017-06-08 16:03:17 -0400 | [diff] [blame] | 143 | GrContext* get(ContextType type, ContextOverrides overrides = ContextOverrides::kNone); |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 144 | const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; } |
krajcevski | b1aded8 | 2014-08-18 07:52:17 -0700 | [diff] [blame] | 145 | |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 146 | private: |
Brian Osman | 9eac2ea | 2017-02-24 14:51:44 -0500 | [diff] [blame] | 147 | ContextInfo getContextInfoInternal(ContextType type, ContextOverrides overrides, |
| 148 | GrContext* shareContext, uint32_t shareIndex); |
| 149 | |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 150 | struct Context { |
Robert Phillips | 08ba085 | 2019-05-22 20:23:43 +0000 | [diff] [blame] | 151 | ContextType fType; |
| 152 | ContextOverrides fOverrides; |
| 153 | GrContextOptions fOptions; |
Robert Phillips | 27eb525 | 2019-06-03 12:59:40 -0400 | [diff] [blame] | 154 | GrBackendApi fBackend; |
Robert Phillips | 08ba085 | 2019-05-22 20:23:43 +0000 | [diff] [blame] | 155 | TestContext* fTestContext; |
| 156 | GrContext* fGrContext; |
| 157 | GrContext* fShareContext; |
| 158 | uint32_t fShareIndex; |
Brian Osman | 60c774d | 2017-02-21 16:58:08 -0500 | [diff] [blame] | 159 | |
Robert Phillips | 27eb525 | 2019-06-03 12:59:40 -0400 | [diff] [blame] | 160 | bool fAbandoned; |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 161 | }; |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 162 | SkTArray<Context, true> fContexts; |
Brian Salomon | 7f9c29a | 2017-01-24 22:22:05 +0000 | [diff] [blame] | 163 | std::unique_ptr<GLTestContext> fSentinelGLContext; |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 164 | const GrContextOptions fGlobalOptions; |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 165 | }; |
Robert Phillips | cdabbcc | 2017-06-08 16:03:17 -0400 | [diff] [blame] | 166 | |
| 167 | class ContextInfo { |
| 168 | public: |
| 169 | ContextInfo() = default; |
| 170 | ContextInfo& operator=(const ContextInfo&) = default; |
| 171 | |
| 172 | GrContextFactory::ContextType type() const { return fType; } |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 173 | GrBackendApi backend() const { return GrContextFactory::ContextTypeBackend(fType); } |
Robert Phillips | cdabbcc | 2017-06-08 16:03:17 -0400 | [diff] [blame] | 174 | |
| 175 | GrContext* grContext() const { return fGrContext; } |
| 176 | |
| 177 | TestContext* testContext() const { return fTestContext; } |
| 178 | |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame^] | 179 | #ifdef SK_GL |
Robert Phillips | cdabbcc | 2017-06-08 16:03:17 -0400 | [diff] [blame] | 180 | GLTestContext* glContext() const { |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 181 | SkASSERT(GrBackendApi::kOpenGL == this->backend()); |
Robert Phillips | cdabbcc | 2017-06-08 16:03:17 -0400 | [diff] [blame] | 182 | return static_cast<GLTestContext*>(fTestContext); |
| 183 | } |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame^] | 184 | #endif |
Robert Phillips | cdabbcc | 2017-06-08 16:03:17 -0400 | [diff] [blame] | 185 | |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 186 | const GrContextOptions& options() const { return fOptions; } |
| 187 | |
Robert Phillips | cdabbcc | 2017-06-08 16:03:17 -0400 | [diff] [blame] | 188 | private: |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 189 | ContextInfo(GrContextFactory::ContextType type, TestContext* testContext, GrContext* grContext, |
| 190 | const GrContextOptions& options) |
| 191 | : fType(type), fTestContext(testContext), fGrContext(grContext), fOptions(options) {} |
Robert Phillips | cdabbcc | 2017-06-08 16:03:17 -0400 | [diff] [blame] | 192 | |
| 193 | GrContextFactory::ContextType fType = GrContextFactory::kGL_ContextType; |
| 194 | // Valid until the factory destroys it via abandonContexts() or destroyContexts(). |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 195 | TestContext* fTestContext = nullptr; |
| 196 | GrContext* fGrContext = nullptr; |
| 197 | GrContextOptions fOptions; |
Robert Phillips | cdabbcc | 2017-06-08 16:03:17 -0400 | [diff] [blame] | 198 | |
| 199 | friend class GrContextFactory; |
| 200 | }; |
| 201 | |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 202 | } // namespace sk_gpu_test |
csmartdalton | 6270e55 | 2016-09-13 10:41:49 -0700 | [diff] [blame] | 203 | |
csmartdalton | e812d49 | 2017-02-21 12:36:05 -0700 | [diff] [blame] | 204 | GR_MAKE_BITFIELD_CLASS_OPS(sk_gpu_test::GrContextFactory::ContextOverrides); |
csmartdalton | 6270e55 | 2016-09-13 10:41:49 -0700 | [diff] [blame] | 205 | |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 206 | #endif |