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/SkNativeGLContext.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.");
94 ~GrContextFactory() { this->destroyContexts(); }
96 void destroyContexts() {
97 for (int i = 0; i < fContexts.count(); ++i) {
98 fContexts[i].fGLContext->makeCurrent();
99 fContexts[i].fGrContext->unref();
100 fContexts[i].fGLContext->unref();
106 * Get a GrContext initialized with a type of GL context. It also makes the GL context current.
108 GrContext* get(GLContextType type) {
110 for (int i = 0; i < fContexts.count(); ++i) {
111 if (fContexts[i].fType == type) {
112 fContexts[i].fGLContext->makeCurrent();
113 return fContexts[i].fGrContext;
116 SkAutoTUnref<SkGLContextHelper> glCtx;
117 SkAutoTUnref<GrContext> grCtx;
119 case kNVPR_GLContextType: // fallthru
120 case kNative_GLContextType:
121 glCtx.reset(SkNEW(SkNativeGLContext));
124 case kANGLE_GLContextType:
125 glCtx.reset(SkNEW(SkANGLEGLContext));
129 case kMESA_GLContextType:
130 glCtx.reset(SkNEW(SkMesaGLContext));
133 case kNull_GLContextType:
134 glCtx.reset(SkNEW(SkNullGLContext));
136 case kDebug_GLContextType:
137 glCtx.reset(SkNEW(SkDebugGLContext));
140 static const int kBogusSize = 1;
144 if (!glCtx.get()->init(kBogusSize, kBogusSize)) {
148 // Ensure NVPR is available for the NVPR type and block it from other types.
149 SkAutoTUnref<const GrGLInterface> glInterface(SkRef(glCtx.get()->gl()));
150 if (kNVPR_GLContextType == type) {
151 if (!glInterface->hasExtension("GL_NV_path_rendering")) {
155 glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface));
161 glCtx->makeCurrent();
162 GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glInterface.get());
163 grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx));
167 GPUContext& ctx = fContexts.push_back();
168 ctx.fGLContext = glCtx.get();
169 ctx.fGLContext->ref();
170 ctx.fGrContext = grCtx.get();
171 ctx.fGrContext->ref();
173 return ctx.fGrContext;
176 // Returns the GLContext of the given type. If it has not been created yet,
177 // NULL is returned instead.
178 SkGLContextHelper* getGLContext(GLContextType type) {
179 for (int i = 0; i < fContexts.count(); ++i) {
180 if (fContexts[i].fType == type) {
181 return fContexts[i].fGLContext;
191 SkGLContextHelper* fGLContext;
192 GrContext* fGrContext;
194 SkTArray<GPUContext, true> fContexts;