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" |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 15 | #include "vk/VkTestContext.h" |
robertphillips@google.com | a2d7148 | 2012-08-01 20:08:47 +0000 | [diff] [blame] | 16 | #include "SkTArray.h" |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 17 | |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 18 | struct GrVkBackendContext; |
| 19 | |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 20 | namespace sk_gpu_test { |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 21 | |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 22 | class ContextInfo { |
| 23 | public: |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 24 | ContextInfo() = default; |
| 25 | ContextInfo& operator=(const ContextInfo&) = default; |
| 26 | |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 27 | GrBackend backend() const { return fBackend; } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 28 | |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 29 | GrContext* grContext() const { return fGrContext; } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 30 | |
bsalomon | 0fd3c81 | 2016-05-11 10:38:05 -0700 | [diff] [blame] | 31 | TestContext* testContext() const { return fTestContext; } |
| 32 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 33 | GLTestContext* glContext() const { |
| 34 | SkASSERT(kOpenGL_GrBackend == fBackend); |
| 35 | return static_cast<GLTestContext*>(fTestContext); |
| 36 | } |
| 37 | |
| 38 | #ifdef SK_VULKAN |
| 39 | VkTestContext* vkContext() const { |
| 40 | SkASSERT(kVulkan_GrBackend == fBackend); |
| 41 | return static_cast<VkTestContext*>(fTestContext); |
| 42 | } |
| 43 | #endif |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 44 | |
| 45 | private: |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 46 | ContextInfo(GrBackend backend, TestContext* testContext, GrContext* grContext) |
| 47 | : fBackend(backend) |
| 48 | , fTestContext(testContext) |
| 49 | , fGrContext(grContext) {} |
| 50 | |
| 51 | GrBackend fBackend = kOpenGL_GrBackend; |
| 52 | // Valid until the factory destroys it via abandonContexts() or destroyContexts(). |
| 53 | TestContext* fTestContext = nullptr; |
| 54 | GrContext* fGrContext = nullptr; |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 55 | |
| 56 | friend class GrContextFactory; |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 59 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 60 | * 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] | 61 | * GrContexts backed by different types of GL contexts. It manages creating the |
| 62 | * GL context and a GrContext that uses it. The GL/Gr contexts persist until the |
| 63 | * 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] | 64 | * Gr and GL contexts to make them outlive the factory). |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 65 | */ |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 66 | class GrContextFactory : SkNoncopyable { |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 67 | public: |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 68 | // The availability of context types is subject to platform and build configuration |
| 69 | // restrictions. |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 70 | enum ContextType { |
bsalomon | 11abd8d | 2016-10-14 08:13:48 -0700 | [diff] [blame] | 71 | kGL_ContextType, //! OpenGL context. |
| 72 | kGLES_ContextType, //! OpenGL ES context. |
| 73 | kANGLE_D3D9_ES2_ContextType, //! ANGLE on Direct3D9 OpenGL ES 2 context. |
| 74 | kANGLE_D3D11_ES2_ContextType,//! ANGLE on Direct3D11 OpenGL ES 2 context. |
| 75 | kANGLE_D3D11_ES3_ContextType,//! ANGLE on Direct3D11 OpenGL ES 3 context. |
| 76 | kANGLE_GL_ES2_ContextType, //! ANGLE on OpenGL OpenGL ES 2 context. |
| 77 | kANGLE_GL_ES3_ContextType, //! ANGLE on OpenGL OpenGL ES 3 context. |
| 78 | kCommandBuffer_ContextType, //! Chromium command buffer OpenGL ES context. |
| 79 | kMESA_ContextType, //! MESA OpenGL context |
| 80 | kNullGL_ContextType, //! Non-rendering OpenGL mock context. |
| 81 | kDebugGL_ContextType, //! Non-rendering, state verifying OpenGL context. |
| 82 | kVulkan_ContextType, //! Vulkan |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 83 | kLastContextType = kVulkan_ContextType |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 86 | //! OpenGL or OpenGL ES context depending on the platform. To be removed. |
| 87 | static const ContextType kNativeGL_ContextType; |
| 88 | |
| 89 | static const int kContextTypeCnt = kLastContextType + 1; |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 90 | |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 91 | /** |
| 92 | * Options for GL context creation. For historical and testing reasons the options will default |
| 93 | * to not using GL_NV_path_rendering extension even when the driver supports it. |
| 94 | */ |
csmartdalton | 6270e55 | 2016-09-13 10:41:49 -0700 | [diff] [blame] | 95 | enum class ContextOptions { |
| 96 | kNone = 0x0, |
| 97 | kEnableNVPR = 0x1, |
| 98 | kUseInstanced = 0x2, |
| 99 | kRequireSRGBSupport = 0x4, |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 100 | }; |
| 101 | |
bsalomon | 0fd3c81 | 2016-05-11 10:38:05 -0700 | [diff] [blame] | 102 | static ContextType NativeContextTypeForBackend(GrBackend backend) { |
| 103 | switch (backend) { |
| 104 | case kOpenGL_GrBackend: |
| 105 | return kNativeGL_ContextType; |
| 106 | case kVulkan_GrBackend: |
| 107 | return kVulkan_ContextType; |
| 108 | default: |
| 109 | SkFAIL("Unknown backend"); |
| 110 | return kNullGL_ContextType; |
| 111 | } |
| 112 | } |
| 113 | |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 114 | static bool IsRenderingContext(ContextType type) { |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 115 | switch (type) { |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 116 | case kNullGL_ContextType: |
| 117 | case kDebugGL_ContextType: |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 118 | return false; |
| 119 | default: |
| 120 | return true; |
| 121 | } |
| 122 | } |
| 123 | |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 124 | static GrBackend ContextTypeBackend(ContextType type) { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 125 | switch (type) { |
| 126 | case kVulkan_ContextType: |
| 127 | return kVulkan_GrBackend; |
| 128 | default: |
| 129 | return kOpenGL_GrBackend; |
| 130 | } |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 131 | } |
| 132 | |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 133 | explicit GrContextFactory(const GrContextOptions& opts); |
| 134 | GrContextFactory(); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 135 | |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 136 | ~GrContextFactory(); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 137 | |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 138 | void destroyContexts(); |
| 139 | void abandonContexts(); |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 140 | void releaseResourcesAndAbandonContexts(); |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 141 | |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 142 | /** |
| 143 | * 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] | 144 | */ |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 145 | ContextInfo getContextInfo(ContextType type, |
csmartdalton | 6270e55 | 2016-09-13 10:41:49 -0700 | [diff] [blame] | 146 | ContextOptions options = ContextOptions::kNone); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 147 | /** |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 148 | * 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] | 149 | */ |
csmartdalton | 6270e55 | 2016-09-13 10:41:49 -0700 | [diff] [blame] | 150 | GrContext* get(ContextType type, ContextOptions options = ContextOptions::kNone) { |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 151 | return this->getContextInfo(type, options).grContext(); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 152 | } |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 153 | const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; } |
krajcevski | b1aded8 | 2014-08-18 07:52:17 -0700 | [diff] [blame] | 154 | |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 155 | private: |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 156 | struct Context { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 157 | ContextType fType; |
| 158 | ContextOptions fOptions; |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 159 | GrBackend fBackend; |
| 160 | TestContext* fTestContext; |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 161 | GrContext* fGrContext; |
| 162 | bool fAbandoned; |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 163 | }; |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 164 | SkTArray<Context, true> fContexts; |
| 165 | SkAutoTDelete<GLTestContext> fSentinelGLContext; |
| 166 | const GrContextOptions fGlobalOptions; |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 167 | }; |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 168 | } // namespace sk_gpu_test |
csmartdalton | 6270e55 | 2016-09-13 10:41:49 -0700 | [diff] [blame] | 169 | |
| 170 | GR_MAKE_BITFIELD_CLASS_OPS(sk_gpu_test::GrContextFactory::ContextOptions); |
| 171 | |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 172 | #endif |