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