2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef GrContextFactory_DEFINED
9 #define GrContextFactory_DEFINED
12 #include "gl/SkANGLEGLContext.h"
14 #include "gl/SkDebugGLContext.h"
16 #include "gl/SkMesaGLContext.h"
18 #include "gl/SkGLContext.h"
19 #include "gl/SkNullGLContext.h"
21 #include "GrContext.h"
25 * This is a simple class that is useful in test apps that use different
26 * GrContexts backed by different types of GL contexts. It manages creating the
27 * GL context and a GrContext that uses it. The GL/Gr contexts persist until the
28 * factory is destroyed (though the caller can always grab a ref on the returned
29 * Gr and GL contexts to make them outlive the factory).
31 class GrContextFactory : SkNoncopyable {
34 * Types of GL contexts supported. For historical and testing reasons the native GrContext will
35 * not use "GL_NV_path_rendering" even when the driver supports it. There is a separate context
36 * type that does not remove NVPR support and which will fail when the driver does not support
40 kNative_GLContextType,
47 /** Similar to kNative but does not filter NVPR. It will fail if the GL driver does not
53 kLastGLContextType = kDebug_GLContextType
56 static const int kGLContextTypeCnt = kLastGLContextType + 1;
58 static bool IsRenderingGLContext(GLContextType type) {
60 case kNull_GLContextType:
61 case kDebug_GLContextType:
68 static const char* GLContextTypeName(GLContextType type) {
70 case kNative_GLContextType:
72 case kNull_GLContextType:
75 case kANGLE_GLContextType:
79 case kMESA_GLContextType:
82 case kNVPR_GLContextType:
84 case kDebug_GLContextType:
87 SkFAIL("Unknown GL Context type.");
91 explicit GrContextFactory(const GrContext::Options& opts) : fGlobalOptions(opts) { }
92 GrContextFactory() { }
94 ~GrContextFactory() { this->destroyContexts(); }
96 void destroyContexts() {
97 for (int i = 0; i < fContexts.count(); ++i) {
98 if (fContexts[i].fGLContext) { // could be abandoned.
99 fContexts[i].fGLContext->makeCurrent();
101 fContexts[i].fGrContext->unref();
102 if (fContexts[i].fGLContext) {
103 fContexts[i].fGLContext->unref();
109 void abandonContexts() {
110 for (int i = 0; i < fContexts.count(); ++i) {
111 if (fContexts[i].fGLContext) {
112 fContexts[i].fGLContext->testAbandon();
113 SkSafeSetNull(fContexts[i].fGLContext);
115 fContexts[i].fGrContext->abandonContext();
120 * Get a GrContext initialized with a type of GL context. It also makes the GL context current.
122 GrContext* get(GLContextType type, GrGLStandard forcedGpuAPI = kNone_GrGLStandard) {
123 for (int i = 0; i < fContexts.count(); ++i) {
124 if (forcedGpuAPI != kNone_GrGLStandard &&
125 forcedGpuAPI != fContexts[i].fGLContext->gl()->fStandard)
128 if (fContexts[i].fType == type) {
129 fContexts[i].fGLContext->makeCurrent();
130 return fContexts[i].fGrContext;
133 SkAutoTUnref<SkGLContext> glCtx;
134 SkAutoTUnref<GrContext> grCtx;
136 case kNVPR_GLContextType: // fallthru
137 case kNative_GLContextType:
138 glCtx.reset(SkCreatePlatformGLContext(forcedGpuAPI));
141 case kANGLE_GLContextType:
142 glCtx.reset(SkANGLEGLContext::Create(forcedGpuAPI));
146 case kMESA_GLContextType:
147 glCtx.reset(SkMesaGLContext::Create(forcedGpuAPI));
150 case kNull_GLContextType:
151 glCtx.reset(SkNullGLContext::Create(forcedGpuAPI));
153 case kDebug_GLContextType:
154 glCtx.reset(SkDebugGLContext::Create(forcedGpuAPI));
157 if (NULL == glCtx.get()) {
161 SkASSERT(glCtx->isValid());
163 // Ensure NVPR is available for the NVPR type and block it from other types.
164 SkAutoTUnref<const GrGLInterface> glInterface(SkRef(glCtx->gl()));
165 if (kNVPR_GLContextType == type) {
166 if (!glInterface->hasExtension("GL_NV_path_rendering")) {
170 glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface));
176 glCtx->makeCurrent();
177 GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glInterface.get());
178 grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx, &fGlobalOptions));
182 GPUContext& ctx = fContexts.push_back();
183 ctx.fGLContext = glCtx.get();
184 ctx.fGLContext->ref();
185 ctx.fGrContext = grCtx.get();
186 ctx.fGrContext->ref();
188 return ctx.fGrContext;
191 // Returns the GLContext of the given type. If it has not been created yet,
192 // NULL is returned instead.
193 SkGLContext* getGLContext(GLContextType type) {
194 for (int i = 0; i < fContexts.count(); ++i) {
195 if (fContexts[i].fType == type) {
196 return fContexts[i].fGLContext;
203 const GrContext::Options& getGlobalOptions() const { return fGlobalOptions; }
208 SkGLContext* fGLContext;
209 GrContext* fGrContext;
211 SkTArray<GPUContext, true> fContexts;
212 const GrContext::Options fGlobalOptions;