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 | |
| 26 | GrContext* GrContextFactory::get(GLContextType type, GrGLStandard forcedGpuAPI) { |
| 27 | for (int i = 0; i < fContexts.count(); ++i) { |
| 28 | if (forcedGpuAPI != kNone_GrGLStandard && |
| 29 | forcedGpuAPI != fContexts[i].fGLContext->gl()->fStandard) |
| 30 | continue; |
| 31 | |
| 32 | if (fContexts[i].fType == type) { |
| 33 | fContexts[i].fGLContext->makeCurrent(); |
| 34 | return fContexts[i].fGrContext; |
| 35 | } |
| 36 | } |
| 37 | SkAutoTUnref<SkGLContext> glCtx; |
| 38 | SkAutoTUnref<GrContext> grCtx; |
| 39 | switch (type) { |
| 40 | case kNVPR_GLContextType: // fallthru |
| 41 | case kNative_GLContextType: |
| 42 | glCtx.reset(SkCreatePlatformGLContext(forcedGpuAPI)); |
| 43 | break; |
| 44 | #ifdef SK_ANGLE |
| 45 | case kANGLE_GLContextType: |
hendrikw | eddbefb | 2015-09-11 13:07:29 -0700 | [diff] [blame] | 46 | glCtx.reset(SkANGLEGLContext::Create(forcedGpuAPI, false)); |
| 47 | break; |
| 48 | case kANGLE_GL_GLContextType: |
| 49 | glCtx.reset(SkANGLEGLContext::Create(forcedGpuAPI, true)); |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 50 | break; |
| 51 | #endif |
hendrikw | 885bf09 | 2015-08-27 10:38:39 -0700 | [diff] [blame] | 52 | #ifdef SK_COMMAND_BUFFER |
| 53 | case kCommandBuffer_GLContextType: |
| 54 | glCtx.reset(SkCommandBufferGLContext::Create(forcedGpuAPI)); |
| 55 | break; |
| 56 | #endif |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 57 | #ifdef SK_MESA |
| 58 | case kMESA_GLContextType: |
| 59 | glCtx.reset(SkMesaGLContext::Create(forcedGpuAPI)); |
| 60 | break; |
| 61 | #endif |
| 62 | case kNull_GLContextType: |
| 63 | glCtx.reset(SkNullGLContext::Create(forcedGpuAPI)); |
| 64 | break; |
| 65 | case kDebug_GLContextType: |
| 66 | glCtx.reset(SkDebugGLContext::Create(forcedGpuAPI)); |
| 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 | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 77 | if (kNVPR_GLContextType != type) { |
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 | } |
kkinnunen | 8b2b380 | 2015-07-01 04:31:29 -0700 | [diff] [blame] | 82 | } else { |
| 83 | if (!glInterface->hasExtension("GL_NV_path_rendering")) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 84 | return nullptr; |
kkinnunen | 8b2b380 | 2015-07-01 04:31:29 -0700 | [diff] [blame] | 85 | } |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | glCtx->makeCurrent(); |
| 89 | GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glInterface.get()); |
jvanverth | a50e17a | 2015-08-12 12:19:36 -0700 | [diff] [blame] | 90 | #ifdef SK_VULKAN |
| 91 | grCtx.reset(GrContext::Create(kVulkan_GrBackend, p3dctx, fGlobalOptions)); |
| 92 | #else |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 93 | grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx, fGlobalOptions)); |
jvanverth | a50e17a | 2015-08-12 12:19:36 -0700 | [diff] [blame] | 94 | #endif |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 95 | if (!grCtx.get()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 96 | return nullptr; |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 97 | } |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 98 | // Warn if path rendering support is not available for the NVPR type. |
| 99 | if (kNVPR_GLContextType == type) { |
| 100 | if (!grCtx->caps()->shaderCaps()->pathRenderingSupport()) { |
jvanverth | 3e5f555 | 2015-07-16 07:46:07 -0700 | [diff] [blame] | 101 | GrGpu* gpu = grCtx->getGpu(); |
| 102 | const GrGLContext* ctx = gpu->glContextForTesting(); |
| 103 | if (ctx) { |
| 104 | const GrGLubyte* verUByte; |
| 105 | GR_GL_CALL_RET(ctx->interface(), verUByte, GetString(GR_GL_VERSION)); |
| 106 | const char* ver = reinterpret_cast<const char*>(verUByte); |
| 107 | SkDebugf("\nWARNING: nvprmsaa config requested, but driver path rendering " |
| 108 | "support not available. Maybe update the driver? Your driver version " |
| 109 | "string: \"%s\"\n", ver); |
| 110 | } else { |
| 111 | SkDebugf("\nWARNING: nvprmsaa config requested, but driver path rendering " |
| 112 | "support not available.\n"); |
| 113 | } |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 117 | GPUContext& ctx = fContexts.push_back(); |
| 118 | ctx.fGLContext = glCtx.get(); |
| 119 | ctx.fGLContext->ref(); |
| 120 | ctx.fGrContext = grCtx.get(); |
| 121 | ctx.fGrContext->ref(); |
| 122 | ctx.fType = type; |
| 123 | return ctx.fGrContext; |
| 124 | } |