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, |
| 27 | GrGLStandard forcedGpuAPI) { |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 28 | for (int i = 0; i < fContexts.count(); ++i) { |
| 29 | if (forcedGpuAPI != kNone_GrGLStandard && |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame^] | 30 | forcedGpuAPI != fContexts[i]->fGLContext->gl()->fStandard) |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 31 | continue; |
| 32 | |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame^] | 33 | if (fContexts[i]->fType == type) { |
| 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) { |
| 41 | case kNVPR_GLContextType: // fallthru |
| 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())); |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [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 | } |
kkinnunen | 8b2b380 | 2015-07-01 04:31:29 -0700 | [diff] [blame] | 83 | } else { |
| 84 | if (!glInterface->hasExtension("GL_NV_path_rendering")) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 85 | return nullptr; |
kkinnunen | 8b2b380 | 2015-07-01 04:31:29 -0700 | [diff] [blame] | 86 | } |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | glCtx->makeCurrent(); |
| 90 | GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glInterface.get()); |
jvanverth | a50e17a | 2015-08-12 12:19:36 -0700 | [diff] [blame] | 91 | #ifdef SK_VULKAN |
| 92 | grCtx.reset(GrContext::Create(kVulkan_GrBackend, p3dctx, fGlobalOptions)); |
| 93 | #else |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 94 | grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx, fGlobalOptions)); |
jvanverth | a50e17a | 2015-08-12 12:19:36 -0700 | [diff] [blame] | 95 | #endif |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 96 | if (!grCtx.get()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 97 | return nullptr; |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 98 | } |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 99 | // Warn if path rendering support is not available for the NVPR type. |
| 100 | if (kNVPR_GLContextType == type) { |
| 101 | if (!grCtx->caps()->shaderCaps()->pathRenderingSupport()) { |
jvanverth | 3e5f555 | 2015-07-16 07:46:07 -0700 | [diff] [blame] | 102 | GrGpu* gpu = grCtx->getGpu(); |
| 103 | const GrGLContext* ctx = gpu->glContextForTesting(); |
| 104 | if (ctx) { |
| 105 | const GrGLubyte* verUByte; |
| 106 | GR_GL_CALL_RET(ctx->interface(), verUByte, GetString(GR_GL_VERSION)); |
| 107 | const char* ver = reinterpret_cast<const char*>(verUByte); |
| 108 | SkDebugf("\nWARNING: nvprmsaa config requested, but driver path rendering " |
| 109 | "support not available. Maybe update the driver? Your driver version " |
| 110 | "string: \"%s\"\n", ver); |
| 111 | } else { |
| 112 | SkDebugf("\nWARNING: nvprmsaa config requested, but driver path rendering " |
| 113 | "support not available.\n"); |
| 114 | } |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame^] | 118 | ContextInfo* ctx = fContexts.emplace_back(new ContextInfo); |
| 119 | ctx->fGLContext = SkRef(glCtx.get()); |
| 120 | ctx->fGrContext = SkRef(grCtx.get()); |
| 121 | ctx->fType = type; |
| 122 | return ctx; |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 123 | } |