bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 1 | |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2014 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #include "GrContextFactory.h" |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 10 | #include "gl/GLTestContext.h" |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 11 | |
| 12 | #if SK_ANGLE |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 13 | #include "gl/angle/GLTestContext_angle.h" |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 14 | #endif |
hendrikw | 885bf09 | 2015-08-27 10:38:39 -0700 | [diff] [blame] | 15 | #if SK_COMMAND_BUFFER |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 16 | #include "gl/command_buffer/GLTestContext_command_buffer.h" |
hendrikw | 885bf09 | 2015-08-27 10:38:39 -0700 | [diff] [blame] | 17 | #endif |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 18 | #include "gl/debug/DebugGLTestContext.h" |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 19 | #if SK_MESA |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 20 | #include "gl/mesa/GLTestContext_mesa.h" |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 21 | #endif |
bsalomon | 7c62b47 | 2016-04-01 07:42:05 -0700 | [diff] [blame] | 22 | #if SK_VULKAN |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 23 | #include "vk/VkTestContext.h" |
bsalomon | 7c62b47 | 2016-04-01 07:42:05 -0700 | [diff] [blame] | 24 | #endif |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 25 | #include "gl/null/NullGLTestContext.h" |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 26 | #include "gl/GrGLGpu.h" |
| 27 | #include "GrCaps.h" |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 28 | |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 29 | namespace sk_gpu_test { |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 30 | GrContextFactory::GrContextFactory() { } |
| 31 | |
| 32 | GrContextFactory::GrContextFactory(const GrContextOptions& opts) |
| 33 | : fGlobalOptions(opts) { |
| 34 | } |
| 35 | |
| 36 | GrContextFactory::~GrContextFactory() { |
| 37 | this->destroyContexts(); |
| 38 | } |
| 39 | |
| 40 | void GrContextFactory::destroyContexts() { |
| 41 | for (Context& context : fContexts) { |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 42 | if (context.fTestContext) { |
| 43 | context.fTestContext->makeCurrent(); |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 44 | } |
| 45 | if (!context.fGrContext->unique()) { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 46 | context.fGrContext->releaseResourcesAndAbandonContext(); |
| 47 | context.fAbandoned = true; |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 48 | } |
| 49 | context.fGrContext->unref(); |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 50 | delete context.fTestContext; |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 51 | } |
| 52 | fContexts.reset(); |
| 53 | } |
| 54 | |
| 55 | void GrContextFactory::abandonContexts() { |
| 56 | for (Context& context : fContexts) { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 57 | if (!context.fAbandoned) { |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 58 | if (context.fTestContext) { |
| 59 | context.fTestContext->makeCurrent(); |
| 60 | context.fTestContext->testAbandon(); |
| 61 | delete(context.fTestContext); |
| 62 | context.fTestContext = nullptr; |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 63 | } |
| 64 | context.fGrContext->abandonContext(); |
| 65 | context.fAbandoned = true; |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 66 | } |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 70 | void GrContextFactory::releaseResourcesAndAbandonContexts() { |
| 71 | for (Context& context : fContexts) { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 72 | if (!context.fAbandoned) { |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 73 | if (context.fTestContext) { |
| 74 | context.fTestContext->makeCurrent(); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 75 | } |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 76 | context.fGrContext->releaseResourcesAndAbandonContext(); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 77 | context.fAbandoned = true; |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 78 | if (context.fTestContext) { |
| 79 | delete context.fTestContext; |
| 80 | context.fTestContext = nullptr; |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 81 | } |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 86 | #if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC) |
| 87 | const GrContextFactory::ContextType GrContextFactory::kNativeGL_ContextType = |
| 88 | GrContextFactory::kGL_ContextType; |
| 89 | #else |
| 90 | const GrContextFactory::ContextType GrContextFactory::kNativeGL_ContextType = |
| 91 | GrContextFactory::kGLES_ContextType; |
| 92 | #endif |
| 93 | |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 94 | ContextInfo GrContextFactory::getContextInfo(ContextType type, ContextOptions options) { |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 95 | for (int i = 0; i < fContexts.count(); ++i) { |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 96 | Context& context = fContexts[i]; |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 97 | if (context.fType == type && |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 98 | context.fOptions == options && |
| 99 | !context.fAbandoned) { |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 100 | context.fTestContext->makeCurrent(); |
| 101 | return ContextInfo(context.fBackend, context.fTestContext, context.fGrContext); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 102 | } |
| 103 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 104 | SkAutoTDelete<TestContext> testCtx; |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 105 | sk_sp<GrContext> grCtx; |
| 106 | GrBackendContext backendContext = 0; |
| 107 | sk_sp<const GrGLInterface> glInterface; |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 108 | GrBackend backend = ContextTypeBackend(type); |
| 109 | switch (backend) { |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 110 | case kOpenGL_GrBackend: { |
| 111 | GLTestContext* glCtx; |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 112 | switch (type) { |
| 113 | case kGL_ContextType: |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 114 | glCtx = CreatePlatformGLTestContext(kGL_GrGLStandard); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 115 | break; |
| 116 | case kGLES_ContextType: |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 117 | glCtx = CreatePlatformGLTestContext(kGLES_GrGLStandard); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 118 | break; |
| 119 | #if SK_ANGLE |
| 120 | # ifdef SK_BUILD_FOR_WIN |
| 121 | case kANGLE_ContextType: |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 122 | glCtx = CreateANGLEDirect3DGLTestContext(); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 123 | break; |
| 124 | # endif |
| 125 | case kANGLE_GL_ContextType: |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 126 | glCtx = CreateANGLEOpenGLGLTestContext(); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 127 | break; |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 128 | #endif |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 129 | #if SK_COMMAND_BUFFER |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 130 | case kCommandBuffer_ContextType: |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 131 | glCtx = CommandBufferGLTestContext::Create(); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 132 | break; |
hendrikw | 885bf09 | 2015-08-27 10:38:39 -0700 | [diff] [blame] | 133 | #endif |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 134 | #if SK_MESA |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 135 | case kMESA_ContextType: |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 136 | glCtx = CreateMesaGLTestContext(); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 137 | break; |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 138 | #endif |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 139 | case kNullGL_ContextType: |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 140 | glCtx = CreateNullGLTestContext(kEnableNVPR_ContextOptions & options); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 141 | break; |
| 142 | case kDebugGL_ContextType: |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 143 | glCtx = CreateDebugGLTestContext(); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 144 | break; |
| 145 | default: |
| 146 | return ContextInfo(); |
| 147 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 148 | if (!glCtx) { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 149 | return ContextInfo(); |
| 150 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 151 | testCtx.reset(glCtx); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 152 | glInterface.reset(SkRef(glCtx->gl())); |
| 153 | // Block NVPR from non-NVPR types. |
| 154 | if (!(kEnableNVPR_ContextOptions & options)) { |
| 155 | glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface.get())); |
| 156 | if (!glInterface) { |
| 157 | return ContextInfo(); |
| 158 | } |
| 159 | } |
| 160 | backendContext = reinterpret_cast<GrBackendContext>(glInterface.get()); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 161 | break; |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 162 | } |
jvanverth | a50e17a | 2015-08-12 12:19:36 -0700 | [diff] [blame] | 163 | #ifdef SK_VULKAN |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 164 | case kVulkan_GrBackend: |
| 165 | SkASSERT(kVulkan_ContextType == type); |
| 166 | if ((kEnableNVPR_ContextOptions & options) || |
| 167 | (kRequireSRGBSupport_ContextOptions & options)) { |
| 168 | return ContextInfo(); |
| 169 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 170 | testCtx.reset(CreatePlatformVkTestContext()); |
| 171 | if (!testCtx) { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 172 | return ContextInfo(); |
| 173 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 174 | |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 175 | // There is some bug (either in Skia or the NV Vulkan driver) where VkDevice |
| 176 | // destruction will hang occaisonally. For some reason having an existing GL |
| 177 | // context fixes this. |
| 178 | if (!fSentinelGLContext) { |
| 179 | fSentinelGLContext.reset(CreatePlatformGLTestContext(kGL_GrGLStandard)); |
| 180 | if (!fSentinelGLContext) { |
| 181 | fSentinelGLContext.reset(CreatePlatformGLTestContext(kGLES_GrGLStandard)); |
| 182 | } |
| 183 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 184 | backendContext = testCtx->backendContext(); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 185 | break; |
jvanverth | a50e17a | 2015-08-12 12:19:36 -0700 | [diff] [blame] | 186 | #endif |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 187 | default: |
| 188 | return ContextInfo(); |
| 189 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 190 | testCtx->makeCurrent(); |
| 191 | SkASSERT(testCtx && testCtx->backend() == backend); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 192 | grCtx.reset(GrContext::Create(backend, backendContext, fGlobalOptions)); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 193 | if (!grCtx.get()) { |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 194 | return ContextInfo(); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 195 | } |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 196 | if (kEnableNVPR_ContextOptions & options) { |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 197 | if (!grCtx->caps()->shaderCaps()->pathRenderingSupport()) { |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 198 | return ContextInfo(); |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 201 | if (kRequireSRGBSupport_ContextOptions & options) { |
brianosman | 61d3b08 | 2016-03-30 11:19:36 -0700 | [diff] [blame] | 202 | if (!grCtx->caps()->srgbSupport()) { |
| 203 | return ContextInfo(); |
| 204 | } |
| 205 | } |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 206 | |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 207 | Context& context = fContexts.push_back(); |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 208 | context.fBackend = backend; |
| 209 | context.fTestContext = testCtx.release(); |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 210 | context.fGrContext = SkRef(grCtx.get()); |
| 211 | context.fType = type; |
| 212 | context.fOptions = options; |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 213 | context.fAbandoned = false; |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame^] | 214 | return ContextInfo(context.fBackend, context.fTestContext, context.fGrContext); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 215 | } |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 216 | } // namespace sk_gpu_test |