blob: 1f1f89df14d1995f5751f15b13c4508ce968539f [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
kkinnunen74fc7272014-06-22 22:56:53 -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) {
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000097 fContexts[i].fGLContext->makeCurrent();
bsalomon@google.com7361f542012-04-19 19:15:35 +000098 fContexts[i].fGrContext->unref();
99 fContexts[i].fGLContext->unref();
100 }
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000101 fContexts.reset();
bsalomon@google.com7361f542012-04-19 19:15:35 +0000102 }
103
104 /**
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000105 * 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 +0000106 */
kkinnunen74fc7272014-06-22 22:56:53 -0700107 GrContext* get(GLContextType type, GrGLStandard forcedGpuAPI = kNone_GrGLStandard) {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000108 for (int i = 0; i < fContexts.count(); ++i) {
kkinnunen74fc7272014-06-22 22:56:53 -0700109 if (forcedGpuAPI != kNone_GrGLStandard &&
110 forcedGpuAPI != fContexts[i].fGLContext->gl()->fStandard)
111 continue;
112
bsalomon@google.com7361f542012-04-19 19:15:35 +0000113 if (fContexts[i].fType == type) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000114 fContexts[i].fGLContext->makeCurrent();
bsalomon@google.com7361f542012-04-19 19:15:35 +0000115 return fContexts[i].fGrContext;
116 }
117 }
robertphillips@google.com6177e692013-02-28 20:16:25 +0000118 SkAutoTUnref<SkGLContextHelper> glCtx;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000119 SkAutoTUnref<GrContext> grCtx;
120 switch (type) {
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000121 case kNVPR_GLContextType: // fallthru
bsalomon@google.com7361f542012-04-19 19:15:35 +0000122 case kNative_GLContextType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000123 glCtx.reset(SkNEW(SkNativeGLContext));
bsalomon@google.com7361f542012-04-19 19:15:35 +0000124 break;
125#ifdef SK_ANGLE
126 case kANGLE_GLContextType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000127 glCtx.reset(SkNEW(SkANGLEGLContext));
bsalomon@google.com7361f542012-04-19 19:15:35 +0000128 break;
129#endif
130#ifdef SK_MESA
131 case kMESA_GLContextType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000132 glCtx.reset(SkNEW(SkMesaGLContext));
bsalomon@google.com7361f542012-04-19 19:15:35 +0000133 break;
134#endif
135 case kNull_GLContextType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000136 glCtx.reset(SkNEW(SkNullGLContext));
bsalomon@google.com7361f542012-04-19 19:15:35 +0000137 break;
138 case kDebug_GLContextType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000139 glCtx.reset(SkNEW(SkDebugGLContext));
bsalomon@google.com7361f542012-04-19 19:15:35 +0000140 break;
141 }
142 static const int kBogusSize = 1;
143 if (!glCtx.get()) {
144 return NULL;
145 }
kkinnunen74fc7272014-06-22 22:56:53 -0700146 if (!glCtx.get()->init(forcedGpuAPI, kBogusSize, kBogusSize)) {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000147 return NULL;
148 }
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000149
150 // Ensure NVPR is available for the NVPR type and block it from other types.
151 SkAutoTUnref<const GrGLInterface> glInterface(SkRef(glCtx.get()->gl()));
152 if (kNVPR_GLContextType == type) {
153 if (!glInterface->hasExtension("GL_NV_path_rendering")) {
154 return NULL;
155 }
156 } else {
157 glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface));
158 if (!glInterface) {
159 return NULL;
160 }
161 }
162
163 glCtx->makeCurrent();
164 GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glInterface.get());
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000165 grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx));
bsalomon@google.com7361f542012-04-19 19:15:35 +0000166 if (!grCtx.get()) {
167 return NULL;
168 }
169 GPUContext& ctx = fContexts.push_back();
170 ctx.fGLContext = glCtx.get();
171 ctx.fGLContext->ref();
172 ctx.fGrContext = grCtx.get();
173 ctx.fGrContext->ref();
174 ctx.fType = type;
175 return ctx.fGrContext;
176 }
keyar@chromium.org5bdef292012-08-14 22:02:48 +0000177
178 // Returns the GLContext of the given type. If it has not been created yet,
179 // NULL is returned instead.
robertphillips@google.com6177e692013-02-28 20:16:25 +0000180 SkGLContextHelper* getGLContext(GLContextType type) {
keyar@chromium.org5bdef292012-08-14 22:02:48 +0000181 for (int i = 0; i < fContexts.count(); ++i) {
182 if (fContexts[i].fType == type) {
183 return fContexts[i].fGLContext;
184 }
185 }
186
187 return NULL;
188 }
189
bsalomon@google.com7361f542012-04-19 19:15:35 +0000190private:
191 struct GPUContext {
192 GLContextType fType;
robertphillips@google.com6177e692013-02-28 20:16:25 +0000193 SkGLContextHelper* fGLContext;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000194 GrContext* fGrContext;
195 };
196 SkTArray<GPUContext, true> fContexts;
197};
198
199#endif