blob: d78120ccb0e265682d891bd07bdde5648c64c589 [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
bsalomon10805962014-10-08 04:45:09 -070018#include "gl/SkNativeGLContext.h"
bsalomon@google.com7361f542012-04-19 19:15:35 +000019#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
krajcevski69a55602014-08-13 10:46:31 -070091 explicit GrContextFactory(const GrContext::Options& opts) : fGlobalOptions(opts) { }
kkinnunen80549fc2014-06-30 06:36:31 -070092 GrContextFactory() { }
bsalomon@google.com7361f542012-04-19 19:15:35 +000093
bsalomon@google.com67b915d2013-02-04 16:13:32 +000094 ~GrContextFactory() { this->destroyContexts(); }
95
96 void destroyContexts() {
bsalomon@google.com7361f542012-04-19 19:15:35 +000097 for (int i = 0; i < fContexts.count(); ++i) {
bsalomon49f085d2014-09-05 13:34:00 -070098 if (fContexts[i].fGLContext) { // could be abandoned.
bsalomon2354f842014-07-28 13:48:36 -070099 fContexts[i].fGLContext->makeCurrent();
100 }
bsalomon@google.com7361f542012-04-19 19:15:35 +0000101 fContexts[i].fGrContext->unref();
bsalomon49f085d2014-09-05 13:34:00 -0700102 if (fContexts[i].fGLContext) {
bsalomon2354f842014-07-28 13:48:36 -0700103 fContexts[i].fGLContext->unref();
104 }
bsalomon@google.com7361f542012-04-19 19:15:35 +0000105 }
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000106 fContexts.reset();
bsalomon@google.com7361f542012-04-19 19:15:35 +0000107 }
108
bsalomon2354f842014-07-28 13:48:36 -0700109 void abandonContexts() {
110 for (int i = 0; i < fContexts.count(); ++i) {
bsalomon49f085d2014-09-05 13:34:00 -0700111 if (fContexts[i].fGLContext) {
bsalomon944bcf02014-07-29 08:01:52 -0700112 fContexts[i].fGLContext->testAbandon();
113 SkSafeSetNull(fContexts[i].fGLContext);
114 }
bsalomon2354f842014-07-28 13:48:36 -0700115 fContexts[i].fGrContext->abandonContext();
116 }
117 }
118
bsalomon@google.com7361f542012-04-19 19:15:35 +0000119 /**
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000120 * 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 +0000121 */
kkinnunen80549fc2014-06-30 06:36:31 -0700122 GrContext* get(GLContextType type, GrGLStandard forcedGpuAPI = kNone_GrGLStandard) {
rmistry05ead8a2014-06-23 06:13:46 -0700123 for (int i = 0; i < fContexts.count(); ++i) {
kkinnunen80549fc2014-06-30 06:36:31 -0700124 if (forcedGpuAPI != kNone_GrGLStandard &&
125 forcedGpuAPI != fContexts[i].fGLContext->gl()->fStandard)
126 continue;
127
bsalomon@google.com7361f542012-04-19 19:15:35 +0000128 if (fContexts[i].fType == type) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000129 fContexts[i].fGLContext->makeCurrent();
bsalomon@google.com7361f542012-04-19 19:15:35 +0000130 return fContexts[i].fGrContext;
131 }
132 }
bsalomon10805962014-10-08 04:45:09 -0700133 SkAutoTUnref<SkGLContextHelper> glCtx;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000134 SkAutoTUnref<GrContext> grCtx;
135 switch (type) {
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000136 case kNVPR_GLContextType: // fallthru
bsalomon@google.com7361f542012-04-19 19:15:35 +0000137 case kNative_GLContextType:
bsalomon10805962014-10-08 04:45:09 -0700138 glCtx.reset(SkNEW(SkNativeGLContext));
bsalomon@google.com7361f542012-04-19 19:15:35 +0000139 break;
140#ifdef SK_ANGLE
141 case kANGLE_GLContextType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000142 glCtx.reset(SkNEW(SkANGLEGLContext));
bsalomon@google.com7361f542012-04-19 19:15:35 +0000143 break;
144#endif
145#ifdef SK_MESA
146 case kMESA_GLContextType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000147 glCtx.reset(SkNEW(SkMesaGLContext));
bsalomon@google.com7361f542012-04-19 19:15:35 +0000148 break;
149#endif
150 case kNull_GLContextType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000151 glCtx.reset(SkNEW(SkNullGLContext));
bsalomon@google.com7361f542012-04-19 19:15:35 +0000152 break;
153 case kDebug_GLContextType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000154 glCtx.reset(SkNEW(SkDebugGLContext));
bsalomon@google.com7361f542012-04-19 19:15:35 +0000155 break;
156 }
157 static const int kBogusSize = 1;
158 if (!glCtx.get()) {
159 return NULL;
160 }
kkinnunen80549fc2014-06-30 06:36:31 -0700161 if (!glCtx.get()->init(forcedGpuAPI, kBogusSize, kBogusSize)) {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000162 return NULL;
163 }
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000164
165 // Ensure NVPR is available for the NVPR type and block it from other types.
166 SkAutoTUnref<const GrGLInterface> glInterface(SkRef(glCtx.get()->gl()));
167 if (kNVPR_GLContextType == type) {
168 if (!glInterface->hasExtension("GL_NV_path_rendering")) {
169 return NULL;
170 }
171 } else {
172 glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface));
173 if (!glInterface) {
174 return NULL;
175 }
176 }
177
178 glCtx->makeCurrent();
179 GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glInterface.get());
krajcevski69a55602014-08-13 10:46:31 -0700180 grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx, &fGlobalOptions));
bsalomon@google.com7361f542012-04-19 19:15:35 +0000181 if (!grCtx.get()) {
182 return NULL;
183 }
184 GPUContext& ctx = fContexts.push_back();
185 ctx.fGLContext = glCtx.get();
186 ctx.fGLContext->ref();
187 ctx.fGrContext = grCtx.get();
188 ctx.fGrContext->ref();
189 ctx.fType = type;
190 return ctx.fGrContext;
191 }
keyar@chromium.org5bdef292012-08-14 22:02:48 +0000192
193 // Returns the GLContext of the given type. If it has not been created yet,
194 // NULL is returned instead.
bsalomon10805962014-10-08 04:45:09 -0700195 SkGLContextHelper* getGLContext(GLContextType type) {
keyar@chromium.org5bdef292012-08-14 22:02:48 +0000196 for (int i = 0; i < fContexts.count(); ++i) {
197 if (fContexts[i].fType == type) {
198 return fContexts[i].fGLContext;
199 }
200 }
201
202 return NULL;
203 }
204
krajcevskib1aded82014-08-18 07:52:17 -0700205 const GrContext::Options& getGlobalOptions() const { return fGlobalOptions; }
206
bsalomon@google.com7361f542012-04-19 19:15:35 +0000207private:
208 struct GPUContext {
209 GLContextType fType;
bsalomon10805962014-10-08 04:45:09 -0700210 SkGLContextHelper* fGLContext;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000211 GrContext* fGrContext;
212 };
krajcevski69a55602014-08-13 10:46:31 -0700213 SkTArray<GPUContext, true> fContexts;
214 const GrContext::Options fGlobalOptions;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000215};
216
217#endif