bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 1 | /* |
| 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 |
kkinnunen | 9e61bb7 | 2014-10-09 05:24:15 -0700 | [diff] [blame] | 18 | #include "gl/SkGLContext.h" |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 19 | #include "gl/SkNullGLContext.h" |
| 20 | |
| 21 | #include "GrContext.h" |
robertphillips@google.com | a2d7148 | 2012-08-01 20:08:47 +0000 | [diff] [blame] | 22 | #include "SkTArray.h" |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 23 | |
| 24 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 25 | * This is a simple class that is useful in test apps that use different |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 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 |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 29 | * Gr and GL contexts to make them outlive the factory). |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 30 | */ |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 31 | class GrContextFactory : SkNoncopyable { |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 32 | public: |
| 33 | /** |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 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 |
| 37 | * the extension. |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 38 | */ |
| 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.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 47 | /** 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.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 50 | kNull_GLContextType, |
| 51 | kDebug_GLContextType, |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 52 | |
| 53 | kLastGLContextType = kDebug_GLContextType |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 56 | 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.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 68 | 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.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 82 | case kNVPR_GLContextType: |
| 83 | return "nvpr"; |
bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 84 | case kDebug_GLContextType: |
| 85 | return "debug"; |
| 86 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 87 | SkFAIL("Unknown GL Context type."); |
bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
krajcevski | 69a5560 | 2014-08-13 10:46:31 -0700 | [diff] [blame] | 91 | explicit GrContextFactory(const GrContext::Options& opts) : fGlobalOptions(opts) { } |
kkinnunen | 80549fc | 2014-06-30 06:36:31 -0700 | [diff] [blame] | 92 | GrContextFactory() { } |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 93 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 94 | ~GrContextFactory() { this->destroyContexts(); } |
| 95 | |
| 96 | void destroyContexts() { |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 97 | for (int i = 0; i < fContexts.count(); ++i) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 98 | if (fContexts[i].fGLContext) { // could be abandoned. |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 99 | fContexts[i].fGLContext->makeCurrent(); |
| 100 | } |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 101 | fContexts[i].fGrContext->unref(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 102 | if (fContexts[i].fGLContext) { |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 103 | fContexts[i].fGLContext->unref(); |
| 104 | } |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 105 | } |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 106 | fContexts.reset(); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 107 | } |
| 108 | |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 109 | void abandonContexts() { |
| 110 | for (int i = 0; i < fContexts.count(); ++i) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 111 | if (fContexts[i].fGLContext) { |
bsalomon | 944bcf0 | 2014-07-29 08:01:52 -0700 | [diff] [blame] | 112 | fContexts[i].fGLContext->testAbandon(); |
| 113 | SkSafeSetNull(fContexts[i].fGLContext); |
| 114 | } |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 115 | fContexts[i].fGrContext->abandonContext(); |
| 116 | } |
| 117 | } |
| 118 | |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 119 | /** |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 120 | * Get a GrContext initialized with a type of GL context. It also makes the GL context current. |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 121 | */ |
kkinnunen | 80549fc | 2014-06-30 06:36:31 -0700 | [diff] [blame] | 122 | GrContext* get(GLContextType type, GrGLStandard forcedGpuAPI = kNone_GrGLStandard) { |
rmistry | 05ead8a | 2014-06-23 06:13:46 -0700 | [diff] [blame] | 123 | for (int i = 0; i < fContexts.count(); ++i) { |
kkinnunen | 80549fc | 2014-06-30 06:36:31 -0700 | [diff] [blame] | 124 | if (forcedGpuAPI != kNone_GrGLStandard && |
| 125 | forcedGpuAPI != fContexts[i].fGLContext->gl()->fStandard) |
| 126 | continue; |
| 127 | |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 128 | if (fContexts[i].fType == type) { |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 129 | fContexts[i].fGLContext->makeCurrent(); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 130 | return fContexts[i].fGrContext; |
| 131 | } |
| 132 | } |
kkinnunen | 9e61bb7 | 2014-10-09 05:24:15 -0700 | [diff] [blame] | 133 | SkAutoTUnref<SkGLContext> glCtx; |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 134 | SkAutoTUnref<GrContext> grCtx; |
| 135 | switch (type) { |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 136 | case kNVPR_GLContextType: // fallthru |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 137 | case kNative_GLContextType: |
kkinnunen | 30bc88c | 2014-10-15 23:03:54 -0700 | [diff] [blame] | 138 | glCtx.reset(SkCreatePlatformGLContext(forcedGpuAPI)); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 139 | break; |
| 140 | #ifdef SK_ANGLE |
| 141 | case kANGLE_GLContextType: |
kkinnunen | 30bc88c | 2014-10-15 23:03:54 -0700 | [diff] [blame] | 142 | glCtx.reset(SkANGLEGLContext::Create(forcedGpuAPI)); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 143 | break; |
| 144 | #endif |
| 145 | #ifdef SK_MESA |
| 146 | case kMESA_GLContextType: |
kkinnunen | 30bc88c | 2014-10-15 23:03:54 -0700 | [diff] [blame] | 147 | glCtx.reset(SkMesaGLContext::Create(forcedGpuAPI)); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 148 | break; |
| 149 | #endif |
| 150 | case kNull_GLContextType: |
kkinnunen | 30bc88c | 2014-10-15 23:03:54 -0700 | [diff] [blame] | 151 | glCtx.reset(SkNullGLContext::Create(forcedGpuAPI)); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 152 | break; |
| 153 | case kDebug_GLContextType: |
kkinnunen | 30bc88c | 2014-10-15 23:03:54 -0700 | [diff] [blame] | 154 | glCtx.reset(SkDebugGLContext::Create(forcedGpuAPI)); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 155 | break; |
| 156 | } |
kkinnunen | 30bc88c | 2014-10-15 23:03:54 -0700 | [diff] [blame] | 157 | if (NULL == glCtx.get()) { |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 158 | return NULL; |
| 159 | } |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 160 | |
kkinnunen | 30bc88c | 2014-10-15 23:03:54 -0700 | [diff] [blame] | 161 | SkASSERT(glCtx->isValid()); |
| 162 | |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 163 | // Ensure NVPR is available for the NVPR type and block it from other types. |
kkinnunen | 30bc88c | 2014-10-15 23:03:54 -0700 | [diff] [blame] | 164 | SkAutoTUnref<const GrGLInterface> glInterface(SkRef(glCtx->gl())); |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 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()); |
krajcevski | 69a5560 | 2014-08-13 10:46:31 -0700 | [diff] [blame] | 178 | grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx, &fGlobalOptions)); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 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 | } |
keyar@chromium.org | 5bdef29 | 2012-08-14 22:02:48 +0000 | [diff] [blame] | 190 | |
| 191 | // Returns the GLContext of the given type. If it has not been created yet, |
| 192 | // NULL is returned instead. |
kkinnunen | 9e61bb7 | 2014-10-09 05:24:15 -0700 | [diff] [blame] | 193 | SkGLContext* getGLContext(GLContextType type) { |
keyar@chromium.org | 5bdef29 | 2012-08-14 22:02:48 +0000 | [diff] [blame] | 194 | for (int i = 0; i < fContexts.count(); ++i) { |
| 195 | if (fContexts[i].fType == type) { |
| 196 | return fContexts[i].fGLContext; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return NULL; |
| 201 | } |
| 202 | |
krajcevski | b1aded8 | 2014-08-18 07:52:17 -0700 | [diff] [blame] | 203 | const GrContext::Options& getGlobalOptions() const { return fGlobalOptions; } |
| 204 | |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 205 | private: |
| 206 | struct GPUContext { |
| 207 | GLContextType fType; |
kkinnunen | 9e61bb7 | 2014-10-09 05:24:15 -0700 | [diff] [blame] | 208 | SkGLContext* fGLContext; |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 209 | GrContext* fGrContext; |
| 210 | }; |
krajcevski | 69a5560 | 2014-08-13 10:46:31 -0700 | [diff] [blame] | 211 | SkTArray<GPUContext, true> fContexts; |
| 212 | const GrContext::Options fGlobalOptions; |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | #endif |