blob: e73eb784a9af7859dd6ce5776892098bb8a31c26 [file] [log] [blame]
bsalomon@google.com7361f542012-04-19 19:15:35 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef GrContextFactory_DEFINED
9#define GrContextFactory_DEFINED
10
11#if SK_ANGLE
12 #include "gl/SkANGLEGLContext.h"
13#endif
14#include "gl/SkDebugGLContext.h"
15#if SK_MESA
16 #include "gl/SkMesaGLContext.h"
17#endif
18#include "gl/SkNativeGLContext.h"
19#include "gl/SkNullGLContext.h"
20
21#include "GrContext.h"
robertphillips@google.coma2d71482012-08-01 20:08:47 +000022#include "SkTArray.h"
bsalomon@google.com7361f542012-04-19 19:15:35 +000023
24/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000025 * This is a simple class that is useful in test apps that use different
bsalomon@google.com7361f542012-04-19 19:15:35 +000026 * 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
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000029 * Gr and GL contexts to make them outlive the factory).
bsalomon@google.com7361f542012-04-19 19:15:35 +000030 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000031class GrContextFactory : SkNoncopyable {
bsalomon@google.com7361f542012-04-19 19:15:35 +000032public:
33 /**
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000034 * 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
37 * the extension.
bsalomon@google.com7361f542012-04-19 19:15:35 +000038 */
39 enum GLContextType {
40 kNative_GLContextType,
41#if SK_ANGLE
42 kANGLE_GLContextType,
43#endif
44#if SK_MESA
45 kMESA_GLContextType,
46#endif
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000047 /** Similar to kNative but does not filter NVPR. It will fail if the GL driver does not
48 support NVPR */
49 kNVPR_GLContextType,
bsalomon@google.com7361f542012-04-19 19:15:35 +000050 kNull_GLContextType,
51 kDebug_GLContextType,
bsalomon@google.com67b915d2013-02-04 16:13:32 +000052
53 kLastGLContextType = kDebug_GLContextType
bsalomon@google.com7361f542012-04-19 19:15:35 +000054 };
55
bsalomon@google.com67b915d2013-02-04 16:13:32 +000056 static const int kGLContextTypeCnt = kLastGLContextType + 1;
57
58 static bool IsRenderingGLContext(GLContextType type) {
59 switch (type) {
60 case kNull_GLContextType:
61 case kDebug_GLContextType:
62 return false;
63 default:
64 return true;
65 }
66 }
67
bsalomon@google.comcb265352013-02-22 16:13:16 +000068 static const char* GLContextTypeName(GLContextType type) {
69 switch (type) {
70 case kNative_GLContextType:
71 return "native";
72 case kNull_GLContextType:
73 return "null";
74#if SK_ANGLE
75 case kANGLE_GLContextType:
76 return "angle";
77#endif
78#if SK_MESA
79 case kMESA_GLContextType:
80 return "mesa";
81#endif
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000082 case kNVPR_GLContextType:
83 return "nvpr";
bsalomon@google.comcb265352013-02-22 16:13:16 +000084 case kDebug_GLContextType:
85 return "debug";
86 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +000087 SkFAIL("Unknown GL Context type.");
bsalomon@google.comcb265352013-02-22 16:13:16 +000088 }
89 }
90
kkinnunen80549fc2014-06-30 06:36:31 -070091 GrContextFactory() { }
bsalomon@google.com7361f542012-04-19 19:15:35 +000092
bsalomon@google.com67b915d2013-02-04 16:13:32 +000093 ~GrContextFactory() { this->destroyContexts(); }
94
95 void destroyContexts() {
bsalomon@google.com7361f542012-04-19 19:15:35 +000096 for (int i = 0; i < fContexts.count(); ++i) {
bsalomon2354f842014-07-28 13:48:36 -070097 if (NULL != fContexts[i].fGLContext) { // could be abandoned.
98 fContexts[i].fGLContext->makeCurrent();
99 }
bsalomon@google.com7361f542012-04-19 19:15:35 +0000100 fContexts[i].fGrContext->unref();
bsalomon2354f842014-07-28 13:48:36 -0700101 if (NULL != fContexts[i].fGLContext) {
102 fContexts[i].fGLContext->unref();
103 }
bsalomon@google.com7361f542012-04-19 19:15:35 +0000104 }
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000105 fContexts.reset();
bsalomon@google.com7361f542012-04-19 19:15:35 +0000106 }
107
bsalomon2354f842014-07-28 13:48:36 -0700108 void abandonContexts() {
109 for (int i = 0; i < fContexts.count(); ++i) {
bsalomon944bcf02014-07-29 08:01:52 -0700110 if (NULL != fContexts[i].fGLContext) {
111 fContexts[i].fGLContext->testAbandon();
112 SkSafeSetNull(fContexts[i].fGLContext);
113 }
bsalomon2354f842014-07-28 13:48:36 -0700114 fContexts[i].fGrContext->abandonContext();
115 }
116 }
117
bsalomon@google.com7361f542012-04-19 19:15:35 +0000118 /**
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000119 * Get a GrContext initialized with a type of GL context. It also makes the GL context current.
bsalomon@google.com7361f542012-04-19 19:15:35 +0000120 */
kkinnunen80549fc2014-06-30 06:36:31 -0700121 GrContext* get(GLContextType type, GrGLStandard forcedGpuAPI = kNone_GrGLStandard) {
rmistry05ead8a2014-06-23 06:13:46 -0700122 for (int i = 0; i < fContexts.count(); ++i) {
kkinnunen80549fc2014-06-30 06:36:31 -0700123 if (forcedGpuAPI != kNone_GrGLStandard &&
124 forcedGpuAPI != fContexts[i].fGLContext->gl()->fStandard)
125 continue;
126
bsalomon@google.com7361f542012-04-19 19:15:35 +0000127 if (fContexts[i].fType == type) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000128 fContexts[i].fGLContext->makeCurrent();
bsalomon@google.com7361f542012-04-19 19:15:35 +0000129 return fContexts[i].fGrContext;
130 }
131 }
robertphillips@google.com6177e692013-02-28 20:16:25 +0000132 SkAutoTUnref<SkGLContextHelper> glCtx;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000133 SkAutoTUnref<GrContext> grCtx;
134 switch (type) {
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000135 case kNVPR_GLContextType: // fallthru
bsalomon@google.com7361f542012-04-19 19:15:35 +0000136 case kNative_GLContextType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000137 glCtx.reset(SkNEW(SkNativeGLContext));
bsalomon@google.com7361f542012-04-19 19:15:35 +0000138 break;
139#ifdef SK_ANGLE
140 case kANGLE_GLContextType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000141 glCtx.reset(SkNEW(SkANGLEGLContext));
bsalomon@google.com7361f542012-04-19 19:15:35 +0000142 break;
143#endif
144#ifdef SK_MESA
145 case kMESA_GLContextType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000146 glCtx.reset(SkNEW(SkMesaGLContext));
bsalomon@google.com7361f542012-04-19 19:15:35 +0000147 break;
148#endif
149 case kNull_GLContextType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000150 glCtx.reset(SkNEW(SkNullGLContext));
bsalomon@google.com7361f542012-04-19 19:15:35 +0000151 break;
152 case kDebug_GLContextType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000153 glCtx.reset(SkNEW(SkDebugGLContext));
bsalomon@google.com7361f542012-04-19 19:15:35 +0000154 break;
155 }
156 static const int kBogusSize = 1;
157 if (!glCtx.get()) {
158 return NULL;
159 }
kkinnunen80549fc2014-06-30 06:36:31 -0700160 if (!glCtx.get()->init(forcedGpuAPI, kBogusSize, kBogusSize)) {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000161 return NULL;
162 }
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000163
164 // Ensure NVPR is available for the NVPR type and block it from other types.
165 SkAutoTUnref<const GrGLInterface> glInterface(SkRef(glCtx.get()->gl()));
166 if (kNVPR_GLContextType == type) {
167 if (!glInterface->hasExtension("GL_NV_path_rendering")) {
168 return NULL;
169 }
170 } else {
171 glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface));
172 if (!glInterface) {
173 return NULL;
174 }
175 }
176
177 glCtx->makeCurrent();
178 GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glInterface.get());
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000179 grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx));
bsalomon@google.com7361f542012-04-19 19:15:35 +0000180 if (!grCtx.get()) {
181 return NULL;
182 }
183 GPUContext& ctx = fContexts.push_back();
184 ctx.fGLContext = glCtx.get();
185 ctx.fGLContext->ref();
186 ctx.fGrContext = grCtx.get();
187 ctx.fGrContext->ref();
188 ctx.fType = type;
189 return ctx.fGrContext;
190 }
keyar@chromium.org5bdef292012-08-14 22:02:48 +0000191
192 // Returns the GLContext of the given type. If it has not been created yet,
193 // NULL is returned instead.
robertphillips@google.com6177e692013-02-28 20:16:25 +0000194 SkGLContextHelper* getGLContext(GLContextType type) {
keyar@chromium.org5bdef292012-08-14 22:02:48 +0000195 for (int i = 0; i < fContexts.count(); ++i) {
196 if (fContexts[i].fType == type) {
197 return fContexts[i].fGLContext;
198 }
199 }
200
201 return NULL;
202 }
203
bsalomon@google.com7361f542012-04-19 19:15:35 +0000204private:
205 struct GPUContext {
206 GLContextType fType;
robertphillips@google.com6177e692013-02-28 20:16:25 +0000207 SkGLContextHelper* fGLContext;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000208 GrContext* fGrContext;
209 };
210 SkTArray<GPUContext, true> fContexts;
211};
212
213#endif