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 |
| 14 | #include "gl/debug/SkDebugGLContext.h" |
| 15 | #if SK_MESA |
| 16 | #include "gl/mesa/SkMesaGLContext.h" |
| 17 | #endif |
| 18 | #include "gl/SkGLContext.h" |
| 19 | #include "gl/SkNullGLContext.h" |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 20 | #include "gl/GrGLGpu.h" |
| 21 | #include "GrCaps.h" |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 22 | |
| 23 | GrContext* GrContextFactory::get(GLContextType type, GrGLStandard forcedGpuAPI) { |
| 24 | for (int i = 0; i < fContexts.count(); ++i) { |
| 25 | if (forcedGpuAPI != kNone_GrGLStandard && |
| 26 | forcedGpuAPI != fContexts[i].fGLContext->gl()->fStandard) |
| 27 | continue; |
| 28 | |
| 29 | if (fContexts[i].fType == type) { |
| 30 | fContexts[i].fGLContext->makeCurrent(); |
| 31 | return fContexts[i].fGrContext; |
| 32 | } |
| 33 | } |
| 34 | SkAutoTUnref<SkGLContext> glCtx; |
| 35 | SkAutoTUnref<GrContext> grCtx; |
| 36 | switch (type) { |
| 37 | case kNVPR_GLContextType: // fallthru |
| 38 | case kNative_GLContextType: |
| 39 | glCtx.reset(SkCreatePlatformGLContext(forcedGpuAPI)); |
| 40 | break; |
| 41 | #ifdef SK_ANGLE |
| 42 | case kANGLE_GLContextType: |
| 43 | glCtx.reset(SkANGLEGLContext::Create(forcedGpuAPI)); |
| 44 | break; |
| 45 | #endif |
| 46 | #ifdef SK_MESA |
| 47 | case kMESA_GLContextType: |
| 48 | glCtx.reset(SkMesaGLContext::Create(forcedGpuAPI)); |
| 49 | break; |
| 50 | #endif |
| 51 | case kNull_GLContextType: |
| 52 | glCtx.reset(SkNullGLContext::Create(forcedGpuAPI)); |
| 53 | break; |
| 54 | case kDebug_GLContextType: |
| 55 | glCtx.reset(SkDebugGLContext::Create(forcedGpuAPI)); |
| 56 | break; |
| 57 | } |
| 58 | if (NULL == glCtx.get()) { |
| 59 | return NULL; |
| 60 | } |
| 61 | |
| 62 | SkASSERT(glCtx->isValid()); |
| 63 | |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 64 | // Block NVPR from non-NVPR types. |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 65 | SkAutoTUnref<const GrGLInterface> glInterface(SkRef(glCtx->gl())); |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 66 | if (kNVPR_GLContextType != type) { |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 67 | glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface)); |
| 68 | if (!glInterface) { |
| 69 | return NULL; |
| 70 | } |
kkinnunen | 8b2b380 | 2015-07-01 04:31:29 -0700 | [diff] [blame] | 71 | } else { |
| 72 | if (!glInterface->hasExtension("GL_NV_path_rendering")) { |
| 73 | return NULL; |
| 74 | } |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | glCtx->makeCurrent(); |
| 78 | GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glInterface.get()); |
jvanverth | a50e17a | 2015-08-12 12:19:36 -0700 | [diff] [blame^] | 79 | #ifdef SK_VULKAN |
| 80 | grCtx.reset(GrContext::Create(kVulkan_GrBackend, p3dctx, fGlobalOptions)); |
| 81 | #else |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 82 | grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx, fGlobalOptions)); |
jvanverth | a50e17a | 2015-08-12 12:19:36 -0700 | [diff] [blame^] | 83 | #endif |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 84 | if (!grCtx.get()) { |
| 85 | return NULL; |
| 86 | } |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 87 | // Warn if path rendering support is not available for the NVPR type. |
| 88 | if (kNVPR_GLContextType == type) { |
| 89 | if (!grCtx->caps()->shaderCaps()->pathRenderingSupport()) { |
jvanverth | 3e5f555 | 2015-07-16 07:46:07 -0700 | [diff] [blame] | 90 | GrGpu* gpu = grCtx->getGpu(); |
| 91 | const GrGLContext* ctx = gpu->glContextForTesting(); |
| 92 | if (ctx) { |
| 93 | const GrGLubyte* verUByte; |
| 94 | GR_GL_CALL_RET(ctx->interface(), verUByte, GetString(GR_GL_VERSION)); |
| 95 | const char* ver = reinterpret_cast<const char*>(verUByte); |
| 96 | SkDebugf("\nWARNING: nvprmsaa config requested, but driver path rendering " |
| 97 | "support not available. Maybe update the driver? Your driver version " |
| 98 | "string: \"%s\"\n", ver); |
| 99 | } else { |
| 100 | SkDebugf("\nWARNING: nvprmsaa config requested, but driver path rendering " |
| 101 | "support not available.\n"); |
| 102 | } |
kkinnunen | cfe62e3 | 2015-07-01 02:58:50 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 106 | GPUContext& ctx = fContexts.push_back(); |
| 107 | ctx.fGLContext = glCtx.get(); |
| 108 | ctx.fGLContext->ref(); |
| 109 | ctx.fGrContext = grCtx.get(); |
| 110 | ctx.fGrContext->ref(); |
| 111 | ctx.fType = type; |
| 112 | return ctx.fGrContext; |
| 113 | } |