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()) { |
| 46 | context.fGrContext->abandonContext(); |
| 47 | } |
| 48 | context.fGrContext->unref(); |
| 49 | delete(context.fGLContext); |
| 50 | } |
| 51 | fContexts.reset(); |
| 52 | } |
| 53 | |
| 54 | void GrContextFactory::abandonContexts() { |
| 55 | for (Context& context : fContexts) { |
| 56 | if (context.fGLContext) { |
| 57 | context.fGLContext->makeCurrent(); |
| 58 | context.fGLContext->testAbandon(); |
| 59 | delete(context.fGLContext); |
| 60 | context.fGLContext = nullptr; |
| 61 | } |
| 62 | context.fGrContext->abandonContext(); |
| 63 | } |
| 64 | } |
| 65 | |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 66 | void GrContextFactory::releaseResourcesAndAbandonContexts() { |
| 67 | for (Context& context : fContexts) { |
| 68 | if (context.fGLContext) { |
| 69 | context.fGLContext->makeCurrent(); |
| 70 | context.fGrContext->releaseResourcesAndAbandonContext(); |
| 71 | delete(context.fGLContext); |
| 72 | context.fGLContext = nullptr; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
bsalomon | b4b4cf3 | 2016-04-04 05:56:59 -0700 | [diff] [blame^] | 77 | GrContextFactory::ContextInfo GrContextFactory::getContextInfo(GLContextType type, |
| 78 | GLContextOptions options) { |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 79 | for (int i = 0; i < fContexts.count(); ++i) { |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 80 | Context& context = fContexts[i]; |
| 81 | if (!context.fGLContext) { |
| 82 | continue; |
| 83 | } |
| 84 | if (context.fType == type && |
| 85 | context.fOptions == options) { |
| 86 | context.fGLContext->makeCurrent(); |
| 87 | return ContextInfo(context.fGrContext, context.fGLContext); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 88 | } |
| 89 | } |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 90 | SkAutoTDelete<GLTestContext> glCtx; |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 91 | SkAutoTUnref<GrContext> grCtx; |
| 92 | switch (type) { |
bsalomon | b4b4cf3 | 2016-04-04 05:56:59 -0700 | [diff] [blame^] | 93 | case kNative_GLContextType: |
| 94 | glCtx.reset(CreatePlatformGLTestContext(kNone_GrGLStandard)); |
| 95 | break; |
| 96 | case kGL_GLContextType: |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 97 | glCtx.reset(CreatePlatformGLTestContext(kGL_GrGLStandard)); |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 98 | break; |
bsalomon | b4b4cf3 | 2016-04-04 05:56:59 -0700 | [diff] [blame^] | 99 | case kGLES_GLContextType: |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 100 | glCtx.reset(CreatePlatformGLTestContext(kGLES_GrGLStandard)); |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 101 | break; |
| 102 | #if SK_ANGLE |
| 103 | #ifdef SK_BUILD_FOR_WIN |
bsalomon | b4b4cf3 | 2016-04-04 05:56:59 -0700 | [diff] [blame^] | 104 | case kANGLE_GLContextType: |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 105 | glCtx.reset(CreateANGLEDirect3DGLTestContext()); |
hendrikw | eddbefb | 2015-09-11 13:07:29 -0700 | [diff] [blame] | 106 | break; |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 107 | #endif |
bsalomon | b4b4cf3 | 2016-04-04 05:56:59 -0700 | [diff] [blame^] | 108 | case kANGLE_GL_GLContextType: |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 109 | glCtx.reset(CreateANGLEOpenGLGLTestContext()); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 110 | break; |
| 111 | #endif |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 112 | #if SK_COMMAND_BUFFER |
bsalomon | b4b4cf3 | 2016-04-04 05:56:59 -0700 | [diff] [blame^] | 113 | case kCommandBuffer_GLContextType: |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 114 | glCtx.reset(CommandBufferGLTestContext::Create()); |
hendrikw | 885bf09 | 2015-08-27 10:38:39 -0700 | [diff] [blame] | 115 | break; |
| 116 | #endif |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 117 | #if SK_MESA |
bsalomon | b4b4cf3 | 2016-04-04 05:56:59 -0700 | [diff] [blame^] | 118 | case kMESA_GLContextType: |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 119 | glCtx.reset(CreateMesaGLTestContext()); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 120 | break; |
| 121 | #endif |
bsalomon | b4b4cf3 | 2016-04-04 05:56:59 -0700 | [diff] [blame^] | 122 | case kNull_GLContextType: |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 123 | glCtx.reset(CreateNullGLTestContext()); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 124 | break; |
bsalomon | b4b4cf3 | 2016-04-04 05:56:59 -0700 | [diff] [blame^] | 125 | case kDebug_GLContextType: |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 126 | glCtx.reset(CreateDebugGLTestContext()); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 127 | break; |
| 128 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 129 | if (nullptr == glCtx.get()) { |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 130 | return ContextInfo(); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | SkASSERT(glCtx->isValid()); |
| 134 | |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 135 | // Block NVPR from non-NVPR types. |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 136 | SkAutoTUnref<const GrGLInterface> glInterface(SkRef(glCtx->gl())); |
bsalomon | b4b4cf3 | 2016-04-04 05:56:59 -0700 | [diff] [blame^] | 137 | if (!(kEnableNVPR_GLContextOptions & options)) { |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 138 | glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface)); |
| 139 | if (!glInterface) { |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 140 | return ContextInfo(); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | glCtx->makeCurrent(); |
jvanverth | a50e17a | 2015-08-12 12:19:36 -0700 | [diff] [blame] | 145 | #ifdef SK_VULKAN |
bsalomon | b4b4cf3 | 2016-04-04 05:56:59 -0700 | [diff] [blame^] | 146 | if (kEnableNVPR_GLContextOptions & options) { |
jvanverth | ddf9835 | 2016-03-21 11:46:00 -0700 | [diff] [blame] | 147 | return ContextInfo(); |
| 148 | } else { |
bsalomon | 7c62b47 | 2016-04-01 07:42:05 -0700 | [diff] [blame] | 149 | GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(GrVkBackendContext::Create()); |
jvanverth | ddf9835 | 2016-03-21 11:46:00 -0700 | [diff] [blame] | 150 | grCtx.reset(GrContext::Create(kVulkan_GrBackend, p3dctx, fGlobalOptions)); |
| 151 | } |
jvanverth | a50e17a | 2015-08-12 12:19:36 -0700 | [diff] [blame] | 152 | #else |
bsalomon | 7c62b47 | 2016-04-01 07:42:05 -0700 | [diff] [blame] | 153 | GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glInterface.get()); |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 154 | grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx, fGlobalOptions)); |
jvanverth | a50e17a | 2015-08-12 12:19:36 -0700 | [diff] [blame] | 155 | #endif |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 156 | if (!grCtx.get()) { |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 157 | return ContextInfo(); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 158 | } |
bsalomon | b4b4cf3 | 2016-04-04 05:56:59 -0700 | [diff] [blame^] | 159 | if (kEnableNVPR_GLContextOptions & options) { |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 160 | if (!grCtx->caps()->shaderCaps()->pathRenderingSupport()) { |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 161 | return ContextInfo(); |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
bsalomon | b4b4cf3 | 2016-04-04 05:56:59 -0700 | [diff] [blame^] | 164 | if (kRequireSRGBSupport_GLContextOptions & options) { |
brianosman | 61d3b08 | 2016-03-30 11:19:36 -0700 | [diff] [blame] | 165 | if (!grCtx->caps()->srgbSupport()) { |
| 166 | return ContextInfo(); |
| 167 | } |
| 168 | } |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 169 | |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 170 | Context& context = fContexts.push_back(); |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 171 | context.fGLContext = glCtx.release(); |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 172 | context.fGrContext = SkRef(grCtx.get()); |
| 173 | context.fType = type; |
| 174 | context.fOptions = options; |
| 175 | return ContextInfo(context.fGrContext, context.fGLContext); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 176 | } |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 177 | } // namespace sk_gpu_test |