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"
20 
21 
get(GLContextType type,GrGLStandard forcedGpuAPI)22 GrContext* GrContextFactory::get(GLContextType type, GrGLStandard forcedGpuAPI) {
23     for (int i = 0; i < fContexts.count(); ++i) {
24         if (forcedGpuAPI != kNone_GrGLStandard &&
25             forcedGpuAPI != fContexts[i].fGLContext->gl()->fStandard)
26             continue;
27 
28         if (fContexts[i].fType == type) {
29             fContexts[i].fGLContext->makeCurrent();
30             return fContexts[i].fGrContext;
31         }
32     }
33     SkAutoTUnref<SkGLContext> glCtx;
34     SkAutoTUnref<GrContext> grCtx;
35     switch (type) {
36         case kNVPR_GLContextType: // fallthru
37         case kNative_GLContextType:
38             glCtx.reset(SkCreatePlatformGLContext(forcedGpuAPI));
39             break;
40 #ifdef SK_ANGLE
41         case kANGLE_GLContextType:
42             glCtx.reset(SkANGLEGLContext::Create(forcedGpuAPI));
43             break;
44 #endif
45 #ifdef SK_MESA
46         case kMESA_GLContextType:
47             glCtx.reset(SkMesaGLContext::Create(forcedGpuAPI));
48             break;
49 #endif
50         case kNull_GLContextType:
51             glCtx.reset(SkNullGLContext::Create(forcedGpuAPI));
52             break;
53         case kDebug_GLContextType:
54             glCtx.reset(SkDebugGLContext::Create(forcedGpuAPI));
55             break;
56     }
57     if (NULL == glCtx.get()) {
58         return NULL;
59     }
60 
61     SkASSERT(glCtx->isValid());
62 
63     // Ensure NVPR is available for the NVPR type and block it from other types.
64     SkAutoTUnref<const GrGLInterface> glInterface(SkRef(glCtx->gl()));
65     if (kNVPR_GLContextType == type) {
66         if (!glInterface->hasExtension("GL_NV_path_rendering")) {
67             return NULL;
68         }
69     } else {
70         glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface));
71         if (!glInterface) {
72             return NULL;
73         }
74     }
75 
76     glCtx->makeCurrent();
77     GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glInterface.get());
78     grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx, &fGlobalOptions));
79     if (!grCtx.get()) {
80         return NULL;
81     }
82     GPUContext& ctx = fContexts.push_back();
83     ctx.fGLContext = glCtx.get();
84     ctx.fGLContext->ref();
85     ctx.fGrContext = grCtx.get();
86     ctx.fGrContext->ref();
87     ctx.fType = type;
88     return ctx.fGrContext;
89 }
90