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