| 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 #if SK_ANGLE |
| 12 #include "gl/SkANGLEGLContext.h" | 12 #include "gl/SkANGLEGLContext.h" |
| 13 #endif | 13 #endif |
| 14 #include "gl/SkDebugGLContext.h" | 14 #include "gl/SkDebugGLContext.h" |
| 15 #if SK_MESA | 15 #if SK_MESA |
| 16 #include "gl/SkMesaGLContext.h" | 16 #include "gl/SkMesaGLContext.h" |
| 17 #endif | 17 #endif |
| 18 #include "gl/SkNativeGLContext.h" | 18 #include "gl/SkNativeGLContext.h" |
| 19 #include "gl/SkNullGLContext.h" | 19 #include "gl/SkNullGLContext.h" |
| 20 | 20 |
| 21 #include "GrContext.h" | 21 #include "GrContext.h" |
| 22 #include "SkTArray.h" | 22 #include "SkTArray.h" |
| 23 | 23 |
| 24 /** | 24 /** |
| 25 * This is a simple class that is useful in test apps that use different | 25 * 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 | 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 | 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 | 28 * factory is destroyed (though the caller can always grab a ref on the returned |
| 29 * GrContext to make it outlive the factory). | 29 * Gr and GL contexts to make them outlive the factory). |
| 30 */ | 30 */ |
| 31 class GrContextFactory : public SkNoncopyable { | 31 class GrContextFactory : public SkNoncopyable { |
| 32 public: | 32 public: |
| 33 /** | 33 /** |
| 34 * Types of GL contexts supported. | 34 * Types of GL contexts supported. For historical and testing reasons the na
tive 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 drive
r does not support |
| 37 * the extension. |
| 35 */ | 38 */ |
| 36 enum GLContextType { | 39 enum GLContextType { |
| 37 kNative_GLContextType, | 40 kNative_GLContextType, |
| 38 #if SK_ANGLE | 41 #if SK_ANGLE |
| 39 kANGLE_GLContextType, | 42 kANGLE_GLContextType, |
| 40 #endif | 43 #endif |
| 41 #if SK_MESA | 44 #if SK_MESA |
| 42 kMESA_GLContextType, | 45 kMESA_GLContextType, |
| 43 #endif | 46 #endif |
| 47 /** Similar to kNative but does not filter NVPR. It will fail if the GL dr
iver does not |
| 48 support NVPR */ |
| 49 kNVPR_GLContextType, |
| 44 kNull_GLContextType, | 50 kNull_GLContextType, |
| 45 kDebug_GLContextType, | 51 kDebug_GLContextType, |
| 46 | 52 |
| 47 kLastGLContextType = kDebug_GLContextType | 53 kLastGLContextType = kDebug_GLContextType |
| 48 }; | 54 }; |
| 49 | 55 |
| 50 static const int kGLContextTypeCnt = kLastGLContextType + 1; | 56 static const int kGLContextTypeCnt = kLastGLContextType + 1; |
| 51 | 57 |
| 52 static bool IsRenderingGLContext(GLContextType type) { | 58 static bool IsRenderingGLContext(GLContextType type) { |
| 53 switch (type) { | 59 switch (type) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 66 case kNull_GLContextType: | 72 case kNull_GLContextType: |
| 67 return "null"; | 73 return "null"; |
| 68 #if SK_ANGLE | 74 #if SK_ANGLE |
| 69 case kANGLE_GLContextType: | 75 case kANGLE_GLContextType: |
| 70 return "angle"; | 76 return "angle"; |
| 71 #endif | 77 #endif |
| 72 #if SK_MESA | 78 #if SK_MESA |
| 73 case kMESA_GLContextType: | 79 case kMESA_GLContextType: |
| 74 return "mesa"; | 80 return "mesa"; |
| 75 #endif | 81 #endif |
| 82 case kNVPR_GLContextType: |
| 83 return "nvpr"; |
| 76 case kDebug_GLContextType: | 84 case kDebug_GLContextType: |
| 77 return "debug"; | 85 return "debug"; |
| 78 default: | 86 default: |
| 79 GrCrash("Unknown GL Context type."); | 87 GrCrash("Unknown GL Context type."); |
| 80 } | 88 } |
| 81 } | 89 } |
| 82 | 90 |
| 83 GrContextFactory() { | 91 GrContextFactory() { |
| 84 } | 92 } |
| 85 | 93 |
| 86 ~GrContextFactory() { this->destroyContexts(); } | 94 ~GrContextFactory() { this->destroyContexts(); } |
| 87 | 95 |
| 88 void destroyContexts() { | 96 void destroyContexts() { |
| 89 for (int i = 0; i < fContexts.count(); ++i) { | 97 for (int i = 0; i < fContexts.count(); ++i) { |
| 98 fContexts[i].fGLContext->makeCurrent(); |
| 90 fContexts[i].fGrContext->unref(); | 99 fContexts[i].fGrContext->unref(); |
| 91 fContexts[i].fGLContext->unref(); | 100 fContexts[i].fGLContext->unref(); |
| 92 } | 101 } |
| 93 fContexts.reset(); | 102 fContexts.reset(); |
| 94 } | 103 } |
| 95 | 104 |
| 96 /** | 105 /** |
| 97 * Get a GrContext initialized with a type of GL context. It also makes the
GL context current. | 106 * Get a GrContext initialized with a type of GL context. It also makes the
GL context current. |
| 98 */ | 107 */ |
| 99 GrContext* get(GLContextType type) { | 108 GrContext* get(GLContextType type) { |
| 100 | 109 |
| 101 for (int i = 0; i < fContexts.count(); ++i) { | 110 for (int i = 0; i < fContexts.count(); ++i) { |
| 102 if (fContexts[i].fType == type) { | 111 if (fContexts[i].fType == type) { |
| 103 fContexts[i].fGLContext->makeCurrent(); | 112 fContexts[i].fGLContext->makeCurrent(); |
| 104 return fContexts[i].fGrContext; | 113 return fContexts[i].fGrContext; |
| 105 } | 114 } |
| 106 } | 115 } |
| 107 SkAutoTUnref<SkGLContextHelper> glCtx; | 116 SkAutoTUnref<SkGLContextHelper> glCtx; |
| 108 SkAutoTUnref<GrContext> grCtx; | 117 SkAutoTUnref<GrContext> grCtx; |
| 109 switch (type) { | 118 switch (type) { |
| 119 case kNVPR_GLContextType: // fallthru |
| 110 case kNative_GLContextType: | 120 case kNative_GLContextType: |
| 111 glCtx.reset(SkNEW(SkNativeGLContext)); | 121 glCtx.reset(SkNEW(SkNativeGLContext)); |
| 112 break; | 122 break; |
| 113 #ifdef SK_ANGLE | 123 #ifdef SK_ANGLE |
| 114 case kANGLE_GLContextType: | 124 case kANGLE_GLContextType: |
| 115 glCtx.reset(SkNEW(SkANGLEGLContext)); | 125 glCtx.reset(SkNEW(SkANGLEGLContext)); |
| 116 break; | 126 break; |
| 117 #endif | 127 #endif |
| 118 #ifdef SK_MESA | 128 #ifdef SK_MESA |
| 119 case kMESA_GLContextType: | 129 case kMESA_GLContextType: |
| 120 glCtx.reset(SkNEW(SkMesaGLContext)); | 130 glCtx.reset(SkNEW(SkMesaGLContext)); |
| 121 break; | 131 break; |
| 122 #endif | 132 #endif |
| 123 case kNull_GLContextType: | 133 case kNull_GLContextType: |
| 124 glCtx.reset(SkNEW(SkNullGLContext)); | 134 glCtx.reset(SkNEW(SkNullGLContext)); |
| 125 break; | 135 break; |
| 126 case kDebug_GLContextType: | 136 case kDebug_GLContextType: |
| 127 glCtx.reset(SkNEW(SkDebugGLContext)); | 137 glCtx.reset(SkNEW(SkDebugGLContext)); |
| 128 break; | 138 break; |
| 129 } | 139 } |
| 130 static const int kBogusSize = 1; | 140 static const int kBogusSize = 1; |
| 131 if (!glCtx.get()) { | 141 if (!glCtx.get()) { |
| 132 return NULL; | 142 return NULL; |
| 133 } | 143 } |
| 134 if (!glCtx.get()->init(kBogusSize, kBogusSize)) { | 144 if (!glCtx.get()->init(kBogusSize, kBogusSize)) { |
| 135 return NULL; | 145 return NULL; |
| 136 } | 146 } |
| 137 GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glCtx.get()
->gl()); | 147 |
| 148 // Ensure NVPR is available for the NVPR type and block it from other ty
pes. |
| 149 SkAutoTUnref<const GrGLInterface> glInterface(SkRef(glCtx.get()->gl())); |
| 150 if (kNVPR_GLContextType == type) { |
| 151 if (!glInterface->hasExtension("GL_NV_path_rendering")) { |
| 152 return NULL; |
| 153 } |
| 154 } else { |
| 155 glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface)); |
| 156 if (!glInterface) { |
| 157 return NULL; |
| 158 } |
| 159 } |
| 160 |
| 161 glCtx->makeCurrent(); |
| 162 GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glInterface
.get()); |
| 138 grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx)); | 163 grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx)); |
| 139 if (!grCtx.get()) { | 164 if (!grCtx.get()) { |
| 140 return NULL; | 165 return NULL; |
| 141 } | 166 } |
| 142 GPUContext& ctx = fContexts.push_back(); | 167 GPUContext& ctx = fContexts.push_back(); |
| 143 ctx.fGLContext = glCtx.get(); | 168 ctx.fGLContext = glCtx.get(); |
| 144 ctx.fGLContext->ref(); | 169 ctx.fGLContext->ref(); |
| 145 ctx.fGrContext = grCtx.get(); | 170 ctx.fGrContext = grCtx.get(); |
| 146 ctx.fGrContext->ref(); | 171 ctx.fGrContext->ref(); |
| 147 ctx.fType = type; | 172 ctx.fType = type; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 163 private: | 188 private: |
| 164 struct GPUContext { | 189 struct GPUContext { |
| 165 GLContextType fType; | 190 GLContextType fType; |
| 166 SkGLContextHelper* fGLContext; | 191 SkGLContextHelper* fGLContext; |
| 167 GrContext* fGrContext; | 192 GrContext* fGrContext; |
| 168 }; | 193 }; |
| 169 SkTArray<GPUContext, true> fContexts; | 194 SkTArray<GPUContext, true> fContexts; |
| 170 }; | 195 }; |
| 171 | 196 |
| 172 #endif | 197 #endif |
| OLD | NEW |