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