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 | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 26 | GrContextFactory::ContextInfo* GrContextFactory::getContextInfo(GLContextType type, |
bsalomon | a0a024e | 2015-12-01 07:58:44 -0800 | [diff] [blame] | 27 | GrGLStandard forcedGpuAPI) { |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 28 | for (int i = 0; i < fContexts.count(); ++i) { |
bsalomon | a0a024e | 2015-12-01 07:58:44 -0800 | [diff] [blame] | 29 | if (forcedGpuAPI != kNone_GrGLStandard && |
| 30 | forcedGpuAPI != fContexts[i]->fGLContext->gl()->fStandard) |
| 31 | continue; |
| 32 | |
| 33 | if (fContexts[i]->fType == type) { |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 34 | fContexts[i]->fGLContext->makeCurrent(); |
| 35 | return fContexts[i]; |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | SkAutoTUnref<SkGLContext> glCtx; |
| 39 | SkAutoTUnref<GrContext> grCtx; |
| 40 | switch (type) { |
bsalomon | a0a024e | 2015-12-01 07:58:44 -0800 | [diff] [blame] | 41 | case kNVPR_GLContextType: // fallthru |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 42 | case kNative_GLContextType: |
| 43 | glCtx.reset(SkCreatePlatformGLContext(forcedGpuAPI)); |
| 44 | break; |
| 45 | #ifdef SK_ANGLE |
| 46 | case kANGLE_GLContextType: |
hendrikw | eddbefb | 2015-09-11 13:07:29 -0700 | [diff] [blame] | 47 | glCtx.reset(SkANGLEGLContext::Create(forcedGpuAPI, false)); |
| 48 | break; |
| 49 | case kANGLE_GL_GLContextType: |
| 50 | glCtx.reset(SkANGLEGLContext::Create(forcedGpuAPI, true)); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 51 | break; |
| 52 | #endif |
hendrikw | 885bf09 | 2015-08-27 10:38:39 -0700 | [diff] [blame] | 53 | #ifdef SK_COMMAND_BUFFER |
| 54 | case kCommandBuffer_GLContextType: |
| 55 | glCtx.reset(SkCommandBufferGLContext::Create(forcedGpuAPI)); |
| 56 | break; |
| 57 | #endif |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 58 | #ifdef SK_MESA |
| 59 | case kMESA_GLContextType: |
| 60 | glCtx.reset(SkMesaGLContext::Create(forcedGpuAPI)); |
| 61 | break; |
| 62 | #endif |
| 63 | case kNull_GLContextType: |
| 64 | glCtx.reset(SkNullGLContext::Create(forcedGpuAPI)); |
| 65 | break; |
| 66 | case kDebug_GLContextType: |
| 67 | glCtx.reset(SkDebugGLContext::Create(forcedGpuAPI)); |
| 68 | break; |
| 69 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 70 | if (nullptr == glCtx.get()) { |
| 71 | return nullptr; |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | SkASSERT(glCtx->isValid()); |
| 75 | |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 76 | // Block NVPR from non-NVPR types. |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 77 | SkAutoTUnref<const GrGLInterface> glInterface(SkRef(glCtx->gl())); |
bsalomon | a0a024e | 2015-12-01 07:58:44 -0800 | [diff] [blame] | 78 | if (kNVPR_GLContextType != type) { |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 79 | glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface)); |
| 80 | if (!glInterface) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 81 | return nullptr; |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
| 85 | glCtx->makeCurrent(); |
| 86 | GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glInterface.get()); |
jvanverth | a50e17a | 2015-08-12 12:19:36 -0700 | [diff] [blame] | 87 | #ifdef SK_VULKAN |
| 88 | grCtx.reset(GrContext::Create(kVulkan_GrBackend, p3dctx, fGlobalOptions)); |
| 89 | #else |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 90 | grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx, fGlobalOptions)); |
jvanverth | a50e17a | 2015-08-12 12:19:36 -0700 | [diff] [blame] | 91 | #endif |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 92 | if (!grCtx.get()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 93 | return nullptr; |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 94 | } |
bsalomon | a0a024e | 2015-12-01 07:58:44 -0800 | [diff] [blame] | 95 | if (kNVPR_GLContextType == type) { |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 96 | if (!grCtx->caps()->shaderCaps()->pathRenderingSupport()) { |
kkinnunen | a18a8bc | 2015-12-03 23:04:50 -0800 | [diff] [blame^] | 97 | return nullptr; |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 101 | ContextInfo* ctx = fContexts.emplace_back(new ContextInfo); |
| 102 | ctx->fGLContext = SkRef(glCtx.get()); |
| 103 | ctx->fGrContext = SkRef(grCtx.get()); |
| 104 | ctx->fType = type; |
| 105 | return ctx; |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 106 | } |