blob: 15af8162b1e90bdbf0fa71d5da6c3ce358703eda [file] [log] [blame]
djsollene4545212014-11-13 11:12:41 -08001
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
hendrikw885bf092015-08-27 10:38:39 -070014#if SK_COMMAND_BUFFER
15 #include "gl/command_buffer/SkCommandBufferGLContext.h"
16#endif
djsollene4545212014-11-13 11:12:41 -080017#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"
kkinnunencfe62e32015-07-01 02:58:50 -070023#include "gl/GrGLGpu.h"
24#include "GrCaps.h"
djsollene4545212014-11-13 11:12:41 -080025
kkinnunen179a8f52015-11-20 13:32:24 -080026GrContextFactory::ContextInfo* GrContextFactory::getContextInfo(GLContextType type,
rmistry0f515bd2015-12-22 10:22:26 -080027 GrGLStandard forcedGpuAPI,
kkinnunen5219fd92015-12-10 06:28:13 -080028 GLContextOptions options) {
djsollene4545212014-11-13 11:12:41 -080029 for (int i = 0; i < fContexts.count(); ++i) {
kkinnunen5219fd92015-12-10 06:28:13 -080030 if (fContexts[i]->fType == type &&
rmistry0f515bd2015-12-22 10:22:26 -080031 fContexts[i]->fOptions == options &&
32 (forcedGpuAPI == kNone_GrGLStandard ||
33 forcedGpuAPI == fContexts[i]->fGLContext->gl()->fStandard)) {
kkinnunen179a8f52015-11-20 13:32:24 -080034 fContexts[i]->fGLContext->makeCurrent();
35 return fContexts[i];
djsollene4545212014-11-13 11:12:41 -080036 }
37 }
38 SkAutoTUnref<SkGLContext> glCtx;
39 SkAutoTUnref<GrContext> grCtx;
40 switch (type) {
djsollene4545212014-11-13 11:12:41 -080041 case kNative_GLContextType:
rmistry0f515bd2015-12-22 10:22:26 -080042 glCtx.reset(SkCreatePlatformGLContext(forcedGpuAPI));
djsollene4545212014-11-13 11:12:41 -080043 break;
rmistry0f515bd2015-12-22 10:22:26 -080044#ifdef SK_ANGLE
djsollene4545212014-11-13 11:12:41 -080045 case kANGLE_GLContextType:
rmistry0f515bd2015-12-22 10:22:26 -080046 glCtx.reset(SkANGLEGLContext::Create(forcedGpuAPI, false));
hendrikweddbefb2015-09-11 13:07:29 -070047 break;
48 case kANGLE_GL_GLContextType:
rmistry0f515bd2015-12-22 10:22:26 -080049 glCtx.reset(SkANGLEGLContext::Create(forcedGpuAPI, true));
djsollene4545212014-11-13 11:12:41 -080050 break;
51#endif
rmistry0f515bd2015-12-22 10:22:26 -080052#ifdef SK_COMMAND_BUFFER
hendrikw885bf092015-08-27 10:38:39 -070053 case kCommandBuffer_GLContextType:
rmistry0f515bd2015-12-22 10:22:26 -080054 glCtx.reset(SkCommandBufferGLContext::Create(forcedGpuAPI));
hendrikw885bf092015-08-27 10:38:39 -070055 break;
56#endif
rmistry0f515bd2015-12-22 10:22:26 -080057#ifdef SK_MESA
djsollene4545212014-11-13 11:12:41 -080058 case kMESA_GLContextType:
rmistry0f515bd2015-12-22 10:22:26 -080059 glCtx.reset(SkMesaGLContext::Create(forcedGpuAPI));
djsollene4545212014-11-13 11:12:41 -080060 break;
61#endif
62 case kNull_GLContextType:
rmistry0f515bd2015-12-22 10:22:26 -080063 glCtx.reset(SkNullGLContext::Create(forcedGpuAPI));
djsollene4545212014-11-13 11:12:41 -080064 break;
65 case kDebug_GLContextType:
rmistry0f515bd2015-12-22 10:22:26 -080066 glCtx.reset(SkDebugGLContext::Create(forcedGpuAPI));
djsollene4545212014-11-13 11:12:41 -080067 break;
68 }
halcanary96fcdcc2015-08-27 07:41:13 -070069 if (nullptr == glCtx.get()) {
70 return nullptr;
djsollene4545212014-11-13 11:12:41 -080071 }
72
73 SkASSERT(glCtx->isValid());
74
kkinnunencfe62e32015-07-01 02:58:50 -070075 // Block NVPR from non-NVPR types.
djsollene4545212014-11-13 11:12:41 -080076 SkAutoTUnref<const GrGLInterface> glInterface(SkRef(glCtx->gl()));
kkinnunen5219fd92015-12-10 06:28:13 -080077 if (!(kEnableNVPR_GLContextOptions & options)) {
djsollene4545212014-11-13 11:12:41 -080078 glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface));
79 if (!glInterface) {
halcanary96fcdcc2015-08-27 07:41:13 -070080 return nullptr;
djsollene4545212014-11-13 11:12:41 -080081 }
82 }
83
84 glCtx->makeCurrent();
85 GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glInterface.get());
jvanvertha50e17a2015-08-12 12:19:36 -070086#ifdef SK_VULKAN
87 grCtx.reset(GrContext::Create(kVulkan_GrBackend, p3dctx, fGlobalOptions));
88#else
bsalomon682c2692015-05-22 14:01:46 -070089 grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx, fGlobalOptions));
jvanvertha50e17a2015-08-12 12:19:36 -070090#endif
djsollene4545212014-11-13 11:12:41 -080091 if (!grCtx.get()) {
halcanary96fcdcc2015-08-27 07:41:13 -070092 return nullptr;
djsollene4545212014-11-13 11:12:41 -080093 }
kkinnunen5219fd92015-12-10 06:28:13 -080094 if (kEnableNVPR_GLContextOptions & options) {
kkinnunencfe62e32015-07-01 02:58:50 -070095 if (!grCtx->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunena18a8bc2015-12-03 23:04:50 -080096 return nullptr;
kkinnunencfe62e32015-07-01 02:58:50 -070097 }
98 }
99
kkinnunen179a8f52015-11-20 13:32:24 -0800100 ContextInfo* ctx = fContexts.emplace_back(new ContextInfo);
101 ctx->fGLContext = SkRef(glCtx.get());
102 ctx->fGrContext = SkRef(grCtx.get());
103 ctx->fType = type;
kkinnunen5219fd92015-12-10 06:28:13 -0800104 ctx->fOptions = options;
kkinnunen179a8f52015-11-20 13:32:24 -0800105 return ctx;
djsollene4545212014-11-13 11:12:41 -0800106}