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 |
mtklein | 605d952 | 2016-09-21 14:01:32 -0700 | [diff] [blame] | 15 | #include "gl/command_buffer/GLTestContext_command_buffer.h" |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 16 | #include "gl/debug/DebugGLTestContext.h" |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 17 | #if SK_MESA |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 18 | #include "gl/mesa/GLTestContext_mesa.h" |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 19 | #endif |
djsollen | 7e73108 | 2016-06-09 13:07:13 -0700 | [diff] [blame] | 20 | #ifdef SK_VULKAN |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 21 | #include "vk/VkTestContext.h" |
bsalomon | 7c62b47 | 2016-04-01 07:42:05 -0700 | [diff] [blame] | 22 | #endif |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 23 | #include "gl/null/NullGLTestContext.h" |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 24 | #include "gl/GrGLGpu.h" |
| 25 | #include "GrCaps.h" |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 26 | |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 27 | namespace sk_gpu_test { |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 28 | GrContextFactory::GrContextFactory() { } |
| 29 | |
| 30 | GrContextFactory::GrContextFactory(const GrContextOptions& opts) |
| 31 | : fGlobalOptions(opts) { |
csmartdalton | 6270e55 | 2016-09-13 10:41:49 -0700 | [diff] [blame] | 32 | // In this factory, instanced rendering is specified with ContextOptions::kUseInstanced. |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame] | 33 | SkASSERT(!fGlobalOptions.fEnableInstancedRendering); |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 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 |
bsalomon | 11abd8d | 2016-10-14 08:13:48 -0700 | [diff] [blame^] | 120 | case kANGLE_D3D9_ES2_ContextType: |
| 121 | glCtx = CreateANGLETestContext(ANGLEBackend::kD3D9, ANGLEContextVersion::kES2); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 122 | break; |
bsalomon | 11abd8d | 2016-10-14 08:13:48 -0700 | [diff] [blame^] | 123 | case kANGLE_D3D11_ES2_ContextType: |
| 124 | glCtx = CreateANGLETestContext(ANGLEBackend::kD3D11, |
| 125 | ANGLEContextVersion::kES2); |
| 126 | break; |
| 127 | case kANGLE_D3D11_ES3_ContextType: |
| 128 | glCtx = CreateANGLETestContext(ANGLEBackend::kD3D11, |
| 129 | ANGLEContextVersion::kES3); |
| 130 | break; |
| 131 | case kANGLE_GL_ES2_ContextType: |
| 132 | glCtx = CreateANGLETestContext(ANGLEBackend::kOpenGL, |
| 133 | ANGLEContextVersion::kES2); |
| 134 | break; |
| 135 | case kANGLE_GL_ES3_ContextType: |
| 136 | glCtx = CreateANGLETestContext(ANGLEBackend::kOpenGL, |
| 137 | ANGLEContextVersion::kES3); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 138 | break; |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 139 | #endif |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 140 | case kCommandBuffer_ContextType: |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 141 | glCtx = CommandBufferGLTestContext::Create(); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 142 | break; |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 143 | #if SK_MESA |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 144 | case kMESA_ContextType: |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 145 | glCtx = CreateMesaGLTestContext(); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 146 | break; |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 147 | #endif |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 148 | case kNullGL_ContextType: |
csmartdalton | 6270e55 | 2016-09-13 10:41:49 -0700 | [diff] [blame] | 149 | glCtx = CreateNullGLTestContext(ContextOptions::kEnableNVPR & options); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 150 | break; |
| 151 | case kDebugGL_ContextType: |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 152 | glCtx = CreateDebugGLTestContext(); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 153 | break; |
| 154 | default: |
| 155 | return ContextInfo(); |
| 156 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 157 | if (!glCtx) { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 158 | return ContextInfo(); |
| 159 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 160 | testCtx.reset(glCtx); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 161 | glInterface.reset(SkRef(glCtx->gl())); |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame] | 162 | // Block NVPR from non-NVPR types. We don't block NVPR from contexts that will use |
| 163 | // instanced rendering because that would prevent us from testing mixed samples. |
csmartdalton | 6270e55 | 2016-09-13 10:41:49 -0700 | [diff] [blame] | 164 | if (!((ContextOptions::kEnableNVPR | ContextOptions::kUseInstanced) & options)) { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 165 | glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface.get())); |
| 166 | if (!glInterface) { |
| 167 | return ContextInfo(); |
| 168 | } |
| 169 | } |
| 170 | backendContext = reinterpret_cast<GrBackendContext>(glInterface.get()); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 171 | break; |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 172 | } |
jvanverth | a50e17a | 2015-08-12 12:19:36 -0700 | [diff] [blame] | 173 | #ifdef SK_VULKAN |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 174 | case kVulkan_GrBackend: |
| 175 | SkASSERT(kVulkan_ContextType == type); |
csmartdalton | 6270e55 | 2016-09-13 10:41:49 -0700 | [diff] [blame] | 176 | if (ContextOptions::kEnableNVPR & options) { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 177 | return ContextInfo(); |
| 178 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 179 | testCtx.reset(CreatePlatformVkTestContext()); |
| 180 | if (!testCtx) { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 181 | return ContextInfo(); |
| 182 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 183 | |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 184 | // There is some bug (either in Skia or the NV Vulkan driver) where VkDevice |
| 185 | // destruction will hang occaisonally. For some reason having an existing GL |
| 186 | // context fixes this. |
| 187 | if (!fSentinelGLContext) { |
| 188 | fSentinelGLContext.reset(CreatePlatformGLTestContext(kGL_GrGLStandard)); |
| 189 | if (!fSentinelGLContext) { |
| 190 | fSentinelGLContext.reset(CreatePlatformGLTestContext(kGLES_GrGLStandard)); |
| 191 | } |
| 192 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 193 | backendContext = testCtx->backendContext(); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 194 | break; |
jvanverth | a50e17a | 2015-08-12 12:19:36 -0700 | [diff] [blame] | 195 | #endif |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 196 | default: |
| 197 | return ContextInfo(); |
| 198 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 199 | testCtx->makeCurrent(); |
| 200 | SkASSERT(testCtx && testCtx->backend() == backend); |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame] | 201 | GrContextOptions grOptions = fGlobalOptions; |
csmartdalton | 6270e55 | 2016-09-13 10:41:49 -0700 | [diff] [blame] | 202 | if (ContextOptions::kUseInstanced & options) { |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame] | 203 | grOptions.fEnableInstancedRendering = true; |
| 204 | } |
| 205 | grCtx.reset(GrContext::Create(backend, backendContext, grOptions)); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 206 | if (!grCtx.get()) { |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 207 | return ContextInfo(); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 208 | } |
csmartdalton | 6270e55 | 2016-09-13 10:41:49 -0700 | [diff] [blame] | 209 | if (ContextOptions::kEnableNVPR & options) { |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 210 | if (!grCtx->caps()->shaderCaps()->pathRenderingSupport()) { |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 211 | return ContextInfo(); |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
csmartdalton | 6270e55 | 2016-09-13 10:41:49 -0700 | [diff] [blame] | 214 | if (ContextOptions::kUseInstanced & options) { |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame] | 215 | if (GrCaps::InstancedSupport::kNone == grCtx->caps()->instancedSupport()) { |
| 216 | return ContextInfo(); |
| 217 | } |
| 218 | } |
csmartdalton | 6270e55 | 2016-09-13 10:41:49 -0700 | [diff] [blame] | 219 | if (ContextOptions::kRequireSRGBSupport & options) { |
brianosman | 61d3b08 | 2016-03-30 11:19:36 -0700 | [diff] [blame] | 220 | if (!grCtx->caps()->srgbSupport()) { |
| 221 | return ContextInfo(); |
| 222 | } |
| 223 | } |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 224 | |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 225 | Context& context = fContexts.push_back(); |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 226 | context.fBackend = backend; |
| 227 | context.fTestContext = testCtx.release(); |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 228 | context.fGrContext = SkRef(grCtx.get()); |
| 229 | context.fType = type; |
| 230 | context.fOptions = options; |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 231 | context.fAbandoned = false; |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 232 | return ContextInfo(context.fBackend, context.fTestContext, context.fGrContext); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 233 | } |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 234 | } // namespace sk_gpu_test |