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 |
| 23 | #include "vk/GrVkBackendContext.h" |
| 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) { |
| 42 | if (context.fGLContext) { |
| 43 | context.fGLContext->makeCurrent(); |
| 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 | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame^] | 50 | delete context.fGLContext; |
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) { |
| 58 | if (context.fGLContext) { |
| 59 | context.fGLContext->makeCurrent(); |
| 60 | context.fGLContext->testAbandon(); |
| 61 | delete(context.fGLContext); |
| 62 | context.fGLContext = nullptr; |
| 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) { |
| 73 | if (context.fGLContext) { |
| 74 | context.fGLContext->makeCurrent(); |
| 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; |
| 78 | if (context.fGLContext) { |
| 79 | delete context.fGLContext; |
| 80 | context.fGLContext = nullptr; |
| 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) { |
| 100 | if (context.fGLContext) { |
| 101 | context.fGLContext->makeCurrent(); |
| 102 | } |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 103 | return ContextInfo(context.fGrContext, context.fGLContext); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 104 | } |
| 105 | } |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 106 | SkAutoTDelete<GLTestContext> glCtx; |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame^] | 107 | sk_sp<GrContext> grCtx; |
| 108 | GrBackendContext backendContext = 0; |
| 109 | sk_sp<const GrGLInterface> glInterface; |
| 110 | #ifdef SK_VULKAN |
| 111 | sk_sp<const GrVkBackendContext> vkBackend; |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 112 | #endif |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame^] | 113 | GrBackend backend = ContextTypeBackend(type); |
| 114 | switch (backend) { |
| 115 | case kOpenGL_GrBackend: |
| 116 | switch (type) { |
| 117 | case kGL_ContextType: |
| 118 | glCtx.reset(CreatePlatformGLTestContext(kGL_GrGLStandard)); |
| 119 | break; |
| 120 | case kGLES_ContextType: |
| 121 | glCtx.reset(CreatePlatformGLTestContext(kGLES_GrGLStandard)); |
| 122 | break; |
| 123 | #if SK_ANGLE |
| 124 | # ifdef SK_BUILD_FOR_WIN |
| 125 | case kANGLE_ContextType: |
| 126 | glCtx.reset(CreateANGLEDirect3DGLTestContext()); |
| 127 | break; |
| 128 | # endif |
| 129 | case kANGLE_GL_ContextType: |
| 130 | glCtx.reset(CreateANGLEOpenGLGLTestContext()); |
| 131 | break; |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 132 | #endif |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 133 | #if SK_COMMAND_BUFFER |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame^] | 134 | case kCommandBuffer_ContextType: |
| 135 | glCtx.reset(CommandBufferGLTestContext::Create()); |
| 136 | break; |
hendrikw | 885bf09 | 2015-08-27 10:38:39 -0700 | [diff] [blame] | 137 | #endif |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 138 | #if SK_MESA |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame^] | 139 | case kMESA_ContextType: |
| 140 | glCtx.reset(CreateMesaGLTestContext()); |
| 141 | break; |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 142 | #endif |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame^] | 143 | case kNullGL_ContextType: |
| 144 | glCtx.reset(CreateNullGLTestContext()); |
| 145 | break; |
| 146 | case kDebugGL_ContextType: |
| 147 | glCtx.reset(CreateDebugGLTestContext()); |
| 148 | break; |
| 149 | default: |
| 150 | return ContextInfo(); |
| 151 | } |
| 152 | if (nullptr == glCtx.get()) { |
| 153 | return ContextInfo(); |
| 154 | } |
| 155 | glInterface.reset(SkRef(glCtx->gl())); |
| 156 | // Block NVPR from non-NVPR types. |
| 157 | if (!(kEnableNVPR_ContextOptions & options)) { |
| 158 | glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface.get())); |
| 159 | if (!glInterface) { |
| 160 | return ContextInfo(); |
| 161 | } |
| 162 | } |
| 163 | backendContext = reinterpret_cast<GrBackendContext>(glInterface.get()); |
| 164 | glCtx->makeCurrent(); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 165 | break; |
jvanverth | a50e17a | 2015-08-12 12:19:36 -0700 | [diff] [blame] | 166 | #ifdef SK_VULKAN |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame^] | 167 | case kVulkan_GrBackend: |
| 168 | SkASSERT(kVulkan_ContextType == type); |
| 169 | if ((kEnableNVPR_ContextOptions & options) || |
| 170 | (kRequireSRGBSupport_ContextOptions & options)) { |
| 171 | return ContextInfo(); |
| 172 | } |
| 173 | vkBackend.reset(GrVkBackendContext::Create()); |
| 174 | if (!vkBackend) { |
| 175 | return ContextInfo(); |
| 176 | } |
| 177 | backendContext = reinterpret_cast<GrBackendContext>(vkBackend.get()); |
| 178 | // There is some bug (either in Skia or the NV Vulkan driver) where VkDevice |
| 179 | // destruction will hang occaisonally. For some reason having an existing GL |
| 180 | // context fixes this. |
| 181 | if (!fSentinelGLContext) { |
| 182 | fSentinelGLContext.reset(CreatePlatformGLTestContext(kGL_GrGLStandard)); |
| 183 | if (!fSentinelGLContext) { |
| 184 | fSentinelGLContext.reset(CreatePlatformGLTestContext(kGLES_GrGLStandard)); |
| 185 | } |
| 186 | } |
| 187 | break; |
jvanverth | a50e17a | 2015-08-12 12:19:36 -0700 | [diff] [blame] | 188 | #endif |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame^] | 189 | default: |
| 190 | return ContextInfo(); |
| 191 | } |
| 192 | |
| 193 | grCtx.reset(GrContext::Create(backend, backendContext, fGlobalOptions)); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 194 | if (!grCtx.get()) { |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 195 | return ContextInfo(); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 196 | } |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 197 | if (kEnableNVPR_ContextOptions & options) { |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 198 | if (!grCtx->caps()->shaderCaps()->pathRenderingSupport()) { |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 199 | return ContextInfo(); |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 202 | if (kRequireSRGBSupport_ContextOptions & options) { |
brianosman | 61d3b08 | 2016-03-30 11:19:36 -0700 | [diff] [blame] | 203 | if (!grCtx->caps()->srgbSupport()) { |
| 204 | return ContextInfo(); |
| 205 | } |
| 206 | } |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 207 | |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 208 | Context& context = fContexts.push_back(); |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 209 | context.fGLContext = glCtx.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; |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 214 | return ContextInfo(context.fGrContext, context.fGLContext); |
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 |