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