djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 1 | |
| 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" |
| 10 | |
| 11 | #if SK_ANGLE |
| 12 | #include "gl/angle/SkANGLEGLContext.h" |
| 13 | #endif |
hendrikw | 885bf09 | 2015-08-27 10:38:39 -0700 | [diff] [blame] | 14 | #if SK_COMMAND_BUFFER |
| 15 | #include "gl/command_buffer/SkCommandBufferGLContext.h" |
| 16 | #endif |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 17 | #include "gl/debug/SkDebugGLContext.h" |
| 18 | #if SK_MESA |
| 19 | #include "gl/mesa/SkMesaGLContext.h" |
| 20 | #endif |
| 21 | #include "gl/SkGLContext.h" |
| 22 | #include "gl/SkNullGLContext.h" |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 23 | #include "gl/GrGLGpu.h" |
| 24 | #include "GrCaps.h" |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 25 | |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 26 | GrContextFactory::GrContextFactory() { } |
| 27 | |
| 28 | GrContextFactory::GrContextFactory(const GrContextOptions& opts) |
| 29 | : fGlobalOptions(opts) { |
| 30 | } |
| 31 | |
| 32 | GrContextFactory::~GrContextFactory() { |
| 33 | this->destroyContexts(); |
| 34 | } |
| 35 | |
| 36 | void GrContextFactory::destroyContexts() { |
| 37 | for (Context& context : fContexts) { |
| 38 | if (context.fGLContext) { |
| 39 | context.fGLContext->makeCurrent(); |
| 40 | } |
| 41 | if (!context.fGrContext->unique()) { |
| 42 | context.fGrContext->abandonContext(); |
| 43 | } |
| 44 | context.fGrContext->unref(); |
| 45 | delete(context.fGLContext); |
| 46 | } |
| 47 | fContexts.reset(); |
| 48 | } |
| 49 | |
| 50 | void GrContextFactory::abandonContexts() { |
| 51 | for (Context& context : fContexts) { |
| 52 | if (context.fGLContext) { |
| 53 | context.fGLContext->makeCurrent(); |
| 54 | context.fGLContext->testAbandon(); |
| 55 | delete(context.fGLContext); |
| 56 | context.fGLContext = nullptr; |
| 57 | } |
| 58 | context.fGrContext->abandonContext(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | GrContextFactory::ContextInfo GrContextFactory::getContextInfo(GLContextType type, |
| 63 | GLContextOptions options) { |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 64 | for (int i = 0; i < fContexts.count(); ++i) { |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 65 | Context& context = fContexts[i]; |
| 66 | if (!context.fGLContext) { |
| 67 | continue; |
| 68 | } |
| 69 | if (context.fType == type && |
| 70 | context.fOptions == options) { |
| 71 | context.fGLContext->makeCurrent(); |
| 72 | return ContextInfo(context.fGrContext, context.fGLContext); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 73 | } |
| 74 | } |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 75 | SkAutoTDelete<SkGLContext> glCtx; |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 76 | SkAutoTUnref<GrContext> grCtx; |
| 77 | switch (type) { |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 78 | case kNative_GLContextType: |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 79 | glCtx.reset(SkCreatePlatformGLContext(kNone_GrGLStandard)); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 80 | break; |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 81 | case kGL_GLContextType: |
| 82 | glCtx.reset(SkCreatePlatformGLContext(kGL_GrGLStandard)); |
| 83 | break; |
| 84 | case kGLES_GLContextType: |
| 85 | glCtx.reset(SkCreatePlatformGLContext(kGLES_GrGLStandard)); |
| 86 | break; |
| 87 | #if SK_ANGLE |
| 88 | #ifdef SK_BUILD_FOR_WIN |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 89 | case kANGLE_GLContextType: |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 90 | glCtx.reset(SkANGLEGLContext::CreateDirectX()); |
hendrikw | eddbefb | 2015-09-11 13:07:29 -0700 | [diff] [blame] | 91 | break; |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 92 | #endif |
hendrikw | eddbefb | 2015-09-11 13:07:29 -0700 | [diff] [blame] | 93 | case kANGLE_GL_GLContextType: |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 94 | glCtx.reset(SkANGLEGLContext::CreateOpenGL()); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 95 | break; |
| 96 | #endif |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 97 | #if SK_COMMAND_BUFFER |
kkinnunen | f655e93 | 2016-03-03 07:39:48 -0800 | [diff] [blame] | 98 | case kCommandBuffer_GLContextType: |
| 99 | glCtx.reset(SkCommandBufferGLContext::Create()); |
hendrikw | 885bf09 | 2015-08-27 10:38:39 -0700 | [diff] [blame] | 100 | break; |
| 101 | #endif |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 102 | #if SK_MESA |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 103 | case kMESA_GLContextType: |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 104 | glCtx.reset(SkMesaGLContext::Create()); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 105 | break; |
| 106 | #endif |
| 107 | case kNull_GLContextType: |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 108 | glCtx.reset(SkNullGLContext::Create()); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 109 | break; |
| 110 | case kDebug_GLContextType: |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 111 | glCtx.reset(SkDebugGLContext::Create()); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 112 | break; |
| 113 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 114 | if (nullptr == glCtx.get()) { |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 115 | return ContextInfo(); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | SkASSERT(glCtx->isValid()); |
| 119 | |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 120 | // Block NVPR from non-NVPR types. |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 121 | SkAutoTUnref<const GrGLInterface> glInterface(SkRef(glCtx->gl())); |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 122 | if (!(kEnableNVPR_GLContextOptions & options)) { |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 123 | glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface)); |
| 124 | if (!glInterface) { |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 125 | return ContextInfo(); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | glCtx->makeCurrent(); |
| 130 | GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glInterface.get()); |
jvanverth | a50e17a | 2015-08-12 12:19:36 -0700 | [diff] [blame] | 131 | #ifdef SK_VULKAN |
| 132 | grCtx.reset(GrContext::Create(kVulkan_GrBackend, p3dctx, fGlobalOptions)); |
| 133 | #else |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 134 | grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx, fGlobalOptions)); |
jvanverth | a50e17a | 2015-08-12 12:19:36 -0700 | [diff] [blame] | 135 | #endif |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 136 | if (!grCtx.get()) { |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 137 | return ContextInfo(); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 138 | } |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 139 | if (kEnableNVPR_GLContextOptions & options) { |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 140 | if (!grCtx->caps()->shaderCaps()->pathRenderingSupport()) { |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 141 | return ContextInfo(); |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 145 | Context& context = fContexts.push_back(); |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame^] | 146 | context.fGLContext = glCtx.release(); |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 147 | context.fGrContext = SkRef(grCtx.get()); |
| 148 | context.fType = type; |
| 149 | context.fOptions = options; |
| 150 | return ContextInfo(context.fGrContext, context.fGLContext); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 151 | } |