OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef GrContextFactory_DEFINED | 8 #ifndef GrContextFactory_DEFINED |
9 #define GrContextFactory_DEFINED | 9 #define GrContextFactory_DEFINED |
10 | 10 |
11 #if SK_ANGLE | 11 #include "GrContext.h" |
12 #include "gl/SkANGLEGLContext.h" | 12 |
13 #endif | |
14 #include "gl/SkDebugGLContext.h" | |
15 #if SK_MESA | |
16 #include "gl/SkMesaGLContext.h" | |
17 #endif | |
18 #include "gl/SkGLContext.h" | 13 #include "gl/SkGLContext.h" |
19 #include "gl/SkNullGLContext.h" | |
20 | |
21 #include "GrContext.h" | |
22 #include "SkTArray.h" | 14 #include "SkTArray.h" |
23 | 15 |
24 /** | 16 /** |
25 * This is a simple class that is useful in test apps that use different | 17 * 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 | 18 * 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 | 19 * 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 | 20 * 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). | 21 * Gr and GL contexts to make them outlive the factory). |
30 */ | 22 */ |
31 class GrContextFactory : SkNoncopyable { | 23 class GrContextFactory : SkNoncopyable { |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 fContexts[i].fGLContext->testAbandon(); | 104 fContexts[i].fGLContext->testAbandon(); |
113 SkSafeSetNull(fContexts[i].fGLContext); | 105 SkSafeSetNull(fContexts[i].fGLContext); |
114 } | 106 } |
115 fContexts[i].fGrContext->abandonContext(); | 107 fContexts[i].fGrContext->abandonContext(); |
116 } | 108 } |
117 } | 109 } |
118 | 110 |
119 /** | 111 /** |
120 * Get a GrContext initialized with a type of GL context. It also makes the
GL context current. | 112 * Get a GrContext initialized with a type of GL context. It also makes the
GL context current. |
121 */ | 113 */ |
122 GrContext* get(GLContextType type, GrGLStandard forcedGpuAPI = kNone_GrGLSta
ndard) { | 114 GrContext* get(GLContextType type, GrGLStandard forcedGpuAPI = kNone_GrGLSta
ndard); |
123 for (int i = 0; i < fContexts.count(); ++i) { | |
124 if (forcedGpuAPI != kNone_GrGLStandard && | |
125 forcedGpuAPI != fContexts[i].fGLContext->gl()->fStandard) | |
126 continue; | |
127 | 115 |
128 if (fContexts[i].fType == type) { | |
129 fContexts[i].fGLContext->makeCurrent(); | |
130 return fContexts[i].fGrContext; | |
131 } | |
132 } | |
133 SkAutoTUnref<SkGLContext> glCtx; | |
134 SkAutoTUnref<GrContext> grCtx; | |
135 switch (type) { | |
136 case kNVPR_GLContextType: // fallthru | |
137 case kNative_GLContextType: | |
138 glCtx.reset(SkCreatePlatformGLContext(forcedGpuAPI)); | |
139 break; | |
140 #ifdef SK_ANGLE | |
141 case kANGLE_GLContextType: | |
142 glCtx.reset(SkANGLEGLContext::Create(forcedGpuAPI)); | |
143 break; | |
144 #endif | |
145 #ifdef SK_MESA | |
146 case kMESA_GLContextType: | |
147 glCtx.reset(SkMesaGLContext::Create(forcedGpuAPI)); | |
148 break; | |
149 #endif | |
150 case kNull_GLContextType: | |
151 glCtx.reset(SkNullGLContext::Create(forcedGpuAPI)); | |
152 break; | |
153 case kDebug_GLContextType: | |
154 glCtx.reset(SkDebugGLContext::Create(forcedGpuAPI)); | |
155 break; | |
156 } | |
157 if (NULL == glCtx.get()) { | |
158 return NULL; | |
159 } | |
160 | |
161 SkASSERT(glCtx->isValid()); | |
162 | |
163 // Ensure NVPR is available for the NVPR type and block it from other ty
pes. | |
164 SkAutoTUnref<const GrGLInterface> glInterface(SkRef(glCtx->gl())); | |
165 if (kNVPR_GLContextType == type) { | |
166 if (!glInterface->hasExtension("GL_NV_path_rendering")) { | |
167 return NULL; | |
168 } | |
169 } else { | |
170 glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface)); | |
171 if (!glInterface) { | |
172 return NULL; | |
173 } | |
174 } | |
175 | |
176 glCtx->makeCurrent(); | |
177 GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glInterface
.get()); | |
178 grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx, &fGlobalOptions
)); | |
179 if (!grCtx.get()) { | |
180 return NULL; | |
181 } | |
182 GPUContext& ctx = fContexts.push_back(); | |
183 ctx.fGLContext = glCtx.get(); | |
184 ctx.fGLContext->ref(); | |
185 ctx.fGrContext = grCtx.get(); | |
186 ctx.fGrContext->ref(); | |
187 ctx.fType = type; | |
188 return ctx.fGrContext; | |
189 } | |
190 | 116 |
191 // Returns the GLContext of the given type. If it has not been created yet, | 117 // Returns the GLContext of the given type. If it has not been created yet, |
192 // NULL is returned instead. | 118 // NULL is returned instead. |
193 SkGLContext* getGLContext(GLContextType type) { | 119 SkGLContext* getGLContext(GLContextType type) { |
194 for (int i = 0; i < fContexts.count(); ++i) { | 120 for (int i = 0; i < fContexts.count(); ++i) { |
195 if (fContexts[i].fType == type) { | 121 if (fContexts[i].fType == type) { |
196 return fContexts[i].fGLContext; | 122 return fContexts[i].fGLContext; |
197 } | 123 } |
198 } | 124 } |
199 | 125 |
200 return NULL; | 126 return NULL; |
201 } | 127 } |
202 | 128 |
203 const GrContext::Options& getGlobalOptions() const { return fGlobalOptions;
} | 129 const GrContext::Options& getGlobalOptions() const { return fGlobalOptions;
} |
204 | 130 |
205 private: | 131 private: |
206 struct GPUContext { | 132 struct GPUContext { |
207 GLContextType fType; | 133 GLContextType fType; |
208 SkGLContext* fGLContext; | 134 SkGLContext* fGLContext; |
209 GrContext* fGrContext; | 135 GrContext* fGrContext; |
210 }; | 136 }; |
211 SkTArray<GPUContext, true> fContexts; | 137 SkTArray<GPUContext, true> fContexts; |
212 const GrContext::Options fGlobalOptions; | 138 const GrContext::Options fGlobalOptions; |
213 }; | 139 }; |
214 | 140 |
215 #endif | 141 #endif |
OLD | NEW |