blob: edef2f7db1cdc4802b96b25bb22cfd7bde94a582 [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
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"
kkinnunencfe62e32015-07-01 02:58:50 -070020#include "gl/GrGLGpu.h"
21#include "GrCaps.h"
djsollene4545212014-11-13 11:12:41 -080022
23GrContext* 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
kkinnunencfe62e32015-07-01 02:58:50 -070064 // Block NVPR from non-NVPR types.
djsollene4545212014-11-13 11:12:41 -080065 SkAutoTUnref<const GrGLInterface> glInterface(SkRef(glCtx->gl()));
kkinnunencfe62e32015-07-01 02:58:50 -070066 if (kNVPR_GLContextType != type) {
djsollene4545212014-11-13 11:12:41 -080067 glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface));
68 if (!glInterface) {
69 return NULL;
70 }
71 }
72
73 glCtx->makeCurrent();
74 GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glInterface.get());
bsalomon682c2692015-05-22 14:01:46 -070075 grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx, fGlobalOptions));
djsollene4545212014-11-13 11:12:41 -080076 if (!grCtx.get()) {
77 return NULL;
78 }
kkinnunencfe62e32015-07-01 02:58:50 -070079 // Warn if path rendering support is not available for the NVPR type.
80 if (kNVPR_GLContextType == type) {
81 if (!grCtx->caps()->shaderCaps()->pathRenderingSupport()) {
82 GrGLGpu* gpu = static_cast<GrGLGpu*>(grCtx->getGpu());
83 const GrGLubyte* verUByte;
84 GR_GL_CALL_RET(gpu->glInterface(), verUByte, GetString(GR_GL_VERSION));
85 const char* ver = reinterpret_cast<const char*>(verUByte);
86 SkDebugf("\nWARNING: nvprmsaa config requested, but driver path rendering support not"
87 " available. Maybe update the driver? Your driver version string: \"%s\"\n", ver);
88 }
89 }
90
djsollene4545212014-11-13 11:12:41 -080091 GPUContext& ctx = fContexts.push_back();
92 ctx.fGLContext = glCtx.get();
93 ctx.fGLContext->ref();
94 ctx.fGrContext = grCtx.get();
95 ctx.fGrContext->ref();
96 ctx.fType = type;
97 return ctx.fGrContext;
98}