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 |
| 18 | #include "gl/SkNativeGLContext.h" |
| 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 |
| 29 | * GrContext to make it outlive the factory). |
| 30 | */ |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame^] | 31 | class GrContextFactory : public SkNoncopyable { |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 32 | public: |
| 33 | /** |
| 34 | * Types of GL contexts supported. |
| 35 | */ |
| 36 | enum GLContextType { |
| 37 | kNative_GLContextType, |
| 38 | #if SK_ANGLE |
| 39 | kANGLE_GLContextType, |
| 40 | #endif |
| 41 | #if SK_MESA |
| 42 | kMESA_GLContextType, |
| 43 | #endif |
| 44 | kNull_GLContextType, |
| 45 | kDebug_GLContextType, |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 46 | |
| 47 | kLastGLContextType = kDebug_GLContextType |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 50 | static const int kGLContextTypeCnt = kLastGLContextType + 1; |
| 51 | |
| 52 | static bool IsRenderingGLContext(GLContextType type) { |
| 53 | switch (type) { |
| 54 | case kNull_GLContextType: |
| 55 | case kDebug_GLContextType: |
| 56 | return false; |
| 57 | default: |
| 58 | return true; |
| 59 | } |
| 60 | } |
| 61 | |
bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 62 | static const char* GLContextTypeName(GLContextType type) { |
| 63 | switch (type) { |
| 64 | case kNative_GLContextType: |
| 65 | return "native"; |
| 66 | case kNull_GLContextType: |
| 67 | return "null"; |
| 68 | #if SK_ANGLE |
| 69 | case kANGLE_GLContextType: |
| 70 | return "angle"; |
| 71 | #endif |
| 72 | #if SK_MESA |
| 73 | case kMESA_GLContextType: |
| 74 | return "mesa"; |
| 75 | #endif |
| 76 | case kDebug_GLContextType: |
| 77 | return "debug"; |
| 78 | default: |
| 79 | GrCrash("Unknown GL Context type."); |
| 80 | } |
| 81 | } |
| 82 | |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 83 | GrContextFactory() { |
| 84 | } |
| 85 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 86 | ~GrContextFactory() { this->destroyContexts(); } |
| 87 | |
| 88 | void destroyContexts() { |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 89 | for (int i = 0; i < fContexts.count(); ++i) { |
| 90 | fContexts[i].fGrContext->unref(); |
| 91 | fContexts[i].fGLContext->unref(); |
| 92 | } |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 93 | fContexts.reset(); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 97 | * 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] | 98 | */ |
| 99 | GrContext* get(GLContextType type) { |
| 100 | |
| 101 | for (int i = 0; i < fContexts.count(); ++i) { |
| 102 | if (fContexts[i].fType == type) { |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 103 | fContexts[i].fGLContext->makeCurrent(); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 104 | return fContexts[i].fGrContext; |
| 105 | } |
| 106 | } |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 107 | SkAutoTUnref<SkGLContextHelper> glCtx; |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 108 | SkAutoTUnref<GrContext> grCtx; |
| 109 | switch (type) { |
| 110 | case kNative_GLContextType: |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 111 | glCtx.reset(SkNEW(SkNativeGLContext)); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 112 | break; |
| 113 | #ifdef SK_ANGLE |
| 114 | case kANGLE_GLContextType: |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 115 | glCtx.reset(SkNEW(SkANGLEGLContext)); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 116 | break; |
| 117 | #endif |
| 118 | #ifdef SK_MESA |
| 119 | case kMESA_GLContextType: |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 120 | glCtx.reset(SkNEW(SkMesaGLContext)); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 121 | break; |
| 122 | #endif |
| 123 | case kNull_GLContextType: |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 124 | glCtx.reset(SkNEW(SkNullGLContext)); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 125 | break; |
| 126 | case kDebug_GLContextType: |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 127 | glCtx.reset(SkNEW(SkDebugGLContext)); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 128 | break; |
| 129 | } |
| 130 | static const int kBogusSize = 1; |
| 131 | if (!glCtx.get()) { |
| 132 | return NULL; |
| 133 | } |
| 134 | if (!glCtx.get()->init(kBogusSize, kBogusSize)) { |
| 135 | return NULL; |
| 136 | } |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 137 | GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glCtx.get()->gl()); |
| 138 | grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx)); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 139 | if (!grCtx.get()) { |
| 140 | return NULL; |
| 141 | } |
| 142 | GPUContext& ctx = fContexts.push_back(); |
| 143 | ctx.fGLContext = glCtx.get(); |
| 144 | ctx.fGLContext->ref(); |
| 145 | ctx.fGrContext = grCtx.get(); |
| 146 | ctx.fGrContext->ref(); |
| 147 | ctx.fType = type; |
| 148 | return ctx.fGrContext; |
| 149 | } |
keyar@chromium.org | 5bdef29 | 2012-08-14 22:02:48 +0000 | [diff] [blame] | 150 | |
| 151 | // Returns the GLContext of the given type. If it has not been created yet, |
| 152 | // NULL is returned instead. |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 153 | SkGLContextHelper* getGLContext(GLContextType type) { |
keyar@chromium.org | 5bdef29 | 2012-08-14 22:02:48 +0000 | [diff] [blame] | 154 | for (int i = 0; i < fContexts.count(); ++i) { |
| 155 | if (fContexts[i].fType == type) { |
| 156 | return fContexts[i].fGLContext; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return NULL; |
| 161 | } |
| 162 | |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 163 | private: |
| 164 | struct GPUContext { |
| 165 | GLContextType fType; |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 166 | SkGLContextHelper* fGLContext; |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 167 | GrContext* fGrContext; |
| 168 | }; |
| 169 | SkTArray<GPUContext, true> fContexts; |
| 170 | }; |
| 171 | |
| 172 | #endif |