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 |
djsollen | 7e73108 | 2016-06-09 13:07:13 -0700 | [diff] [blame] | 22 | #ifdef 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) { |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame^] | 34 | // In this factory, instanced rendering is specified with kUseInstanced_ContextOptions. |
| 35 | SkASSERT(!fGlobalOptions.fEnableInstancedRendering); |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | GrContextFactory::~GrContextFactory() { |
| 39 | this->destroyContexts(); |
| 40 | } |
| 41 | |
| 42 | void GrContextFactory::destroyContexts() { |
| 43 | for (Context& context : fContexts) { |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 44 | if (context.fTestContext) { |
| 45 | context.fTestContext->makeCurrent(); |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 46 | } |
| 47 | if (!context.fGrContext->unique()) { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 48 | context.fGrContext->releaseResourcesAndAbandonContext(); |
| 49 | context.fAbandoned = true; |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 50 | } |
| 51 | context.fGrContext->unref(); |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 52 | delete context.fTestContext; |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 53 | } |
| 54 | fContexts.reset(); |
| 55 | } |
| 56 | |
| 57 | void GrContextFactory::abandonContexts() { |
| 58 | for (Context& context : fContexts) { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 59 | if (!context.fAbandoned) { |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 60 | if (context.fTestContext) { |
| 61 | context.fTestContext->makeCurrent(); |
| 62 | context.fTestContext->testAbandon(); |
| 63 | delete(context.fTestContext); |
| 64 | context.fTestContext = nullptr; |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 65 | } |
| 66 | context.fGrContext->abandonContext(); |
| 67 | context.fAbandoned = true; |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 68 | } |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 72 | void GrContextFactory::releaseResourcesAndAbandonContexts() { |
| 73 | for (Context& context : fContexts) { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 74 | if (!context.fAbandoned) { |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 75 | if (context.fTestContext) { |
| 76 | context.fTestContext->makeCurrent(); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 77 | } |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 78 | context.fGrContext->releaseResourcesAndAbandonContext(); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 79 | context.fAbandoned = true; |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 80 | if (context.fTestContext) { |
| 81 | delete context.fTestContext; |
| 82 | context.fTestContext = nullptr; |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 83 | } |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 88 | #if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC) |
| 89 | const GrContextFactory::ContextType GrContextFactory::kNativeGL_ContextType = |
| 90 | GrContextFactory::kGL_ContextType; |
| 91 | #else |
| 92 | const GrContextFactory::ContextType GrContextFactory::kNativeGL_ContextType = |
| 93 | GrContextFactory::kGLES_ContextType; |
| 94 | #endif |
| 95 | |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 96 | ContextInfo GrContextFactory::getContextInfo(ContextType type, ContextOptions options) { |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 97 | for (int i = 0; i < fContexts.count(); ++i) { |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 98 | Context& context = fContexts[i]; |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 99 | if (context.fType == type && |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 100 | context.fOptions == options && |
| 101 | !context.fAbandoned) { |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 102 | context.fTestContext->makeCurrent(); |
| 103 | return ContextInfo(context.fBackend, context.fTestContext, context.fGrContext); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 104 | } |
| 105 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 106 | SkAutoTDelete<TestContext> testCtx; |
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; |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 110 | GrBackend backend = ContextTypeBackend(type); |
| 111 | switch (backend) { |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 112 | case kOpenGL_GrBackend: { |
| 113 | GLTestContext* glCtx; |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 114 | switch (type) { |
| 115 | case kGL_ContextType: |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 116 | glCtx = CreatePlatformGLTestContext(kGL_GrGLStandard); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 117 | break; |
| 118 | case kGLES_ContextType: |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 119 | glCtx = CreatePlatformGLTestContext(kGLES_GrGLStandard); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 120 | break; |
| 121 | #if SK_ANGLE |
| 122 | # ifdef SK_BUILD_FOR_WIN |
| 123 | case kANGLE_ContextType: |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 124 | glCtx = CreateANGLEDirect3DGLTestContext(); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 125 | break; |
| 126 | # endif |
| 127 | case kANGLE_GL_ContextType: |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 128 | glCtx = CreateANGLEOpenGLGLTestContext(); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 129 | break; |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 130 | #endif |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 131 | #if SK_COMMAND_BUFFER |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 132 | case kCommandBuffer_ContextType: |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 133 | glCtx = CommandBufferGLTestContext::Create(); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 134 | break; |
hendrikw | 885bf09 | 2015-08-27 10:38:39 -0700 | [diff] [blame] | 135 | #endif |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 136 | #if SK_MESA |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 137 | case kMESA_ContextType: |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 138 | glCtx = CreateMesaGLTestContext(); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 139 | break; |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 140 | #endif |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 141 | case kNullGL_ContextType: |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 142 | glCtx = CreateNullGLTestContext(kEnableNVPR_ContextOptions & options); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 143 | break; |
| 144 | case kDebugGL_ContextType: |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 145 | glCtx = CreateDebugGLTestContext(); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 146 | break; |
| 147 | default: |
| 148 | return ContextInfo(); |
| 149 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 150 | if (!glCtx) { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 151 | return ContextInfo(); |
| 152 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 153 | testCtx.reset(glCtx); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 154 | glInterface.reset(SkRef(glCtx->gl())); |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame^] | 155 | // Block NVPR from non-NVPR types. We don't block NVPR from contexts that will use |
| 156 | // instanced rendering because that would prevent us from testing mixed samples. |
| 157 | if (!((kEnableNVPR_ContextOptions | kUseInstanced_ContextOptions) & options)) { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 158 | glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface.get())); |
| 159 | if (!glInterface) { |
| 160 | return ContextInfo(); |
| 161 | } |
| 162 | } |
| 163 | backendContext = reinterpret_cast<GrBackendContext>(glInterface.get()); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 164 | break; |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 165 | } |
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); |
brianosman | a56800a | 2016-05-23 10:11:07 -0700 | [diff] [blame] | 169 | if (kEnableNVPR_ContextOptions & options) { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 170 | return ContextInfo(); |
| 171 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 172 | testCtx.reset(CreatePlatformVkTestContext()); |
| 173 | if (!testCtx) { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 174 | return ContextInfo(); |
| 175 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 176 | |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 177 | // There is some bug (either in Skia or the NV Vulkan driver) where VkDevice |
| 178 | // destruction will hang occaisonally. For some reason having an existing GL |
| 179 | // context fixes this. |
| 180 | if (!fSentinelGLContext) { |
| 181 | fSentinelGLContext.reset(CreatePlatformGLTestContext(kGL_GrGLStandard)); |
| 182 | if (!fSentinelGLContext) { |
| 183 | fSentinelGLContext.reset(CreatePlatformGLTestContext(kGLES_GrGLStandard)); |
| 184 | } |
| 185 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 186 | backendContext = testCtx->backendContext(); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 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 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 192 | testCtx->makeCurrent(); |
| 193 | SkASSERT(testCtx && testCtx->backend() == backend); |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame^] | 194 | GrContextOptions grOptions = fGlobalOptions; |
| 195 | if (kUseInstanced_ContextOptions & options) { |
| 196 | grOptions.fEnableInstancedRendering = true; |
| 197 | } |
| 198 | grCtx.reset(GrContext::Create(backend, backendContext, grOptions)); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 199 | if (!grCtx.get()) { |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 200 | return ContextInfo(); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 201 | } |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 202 | if (kEnableNVPR_ContextOptions & options) { |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 203 | if (!grCtx->caps()->shaderCaps()->pathRenderingSupport()) { |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 204 | return ContextInfo(); |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame^] | 207 | if (kUseInstanced_ContextOptions & options) { |
| 208 | if (GrCaps::InstancedSupport::kNone == grCtx->caps()->instancedSupport()) { |
| 209 | return ContextInfo(); |
| 210 | } |
| 211 | } |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 212 | if (kRequireSRGBSupport_ContextOptions & options) { |
brianosman | 61d3b08 | 2016-03-30 11:19:36 -0700 | [diff] [blame] | 213 | if (!grCtx->caps()->srgbSupport()) { |
| 214 | return ContextInfo(); |
| 215 | } |
| 216 | } |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 217 | |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 218 | Context& context = fContexts.push_back(); |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 219 | context.fBackend = backend; |
| 220 | context.fTestContext = testCtx.release(); |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 221 | context.fGrContext = SkRef(grCtx.get()); |
| 222 | context.fType = type; |
| 223 | context.fOptions = options; |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 224 | context.fAbandoned = false; |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 225 | return ContextInfo(context.fBackend, context.fTestContext, context.fGrContext); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 226 | } |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 227 | } // namespace sk_gpu_test |