| 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 | */ |
| 31 | class GrContextFactory : GrNoncopyable { |
| 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 | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 62 | GrContextFactory() { |
| 63 | } |
| 64 | |
| bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 65 | ~GrContextFactory() { this->destroyContexts(); } |
| 66 | |
| 67 | void destroyContexts() { |
| bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 68 | for (int i = 0; i < fContexts.count(); ++i) { |
| 69 | fContexts[i].fGrContext->unref(); |
| 70 | fContexts[i].fGLContext->unref(); |
| 71 | } |
| bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 72 | fContexts.reset(); |
| bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
| bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 76 | * 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] | 77 | */ |
| 78 | GrContext* get(GLContextType type) { |
| 79 | |
| 80 | for (int i = 0; i < fContexts.count(); ++i) { |
| 81 | if (fContexts[i].fType == type) { |
| bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 82 | fContexts[i].fGLContext->makeCurrent(); |
| bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 83 | return fContexts[i].fGrContext; |
| 84 | } |
| 85 | } |
| 86 | SkAutoTUnref<SkGLContext> glCtx; |
| 87 | SkAutoTUnref<GrContext> grCtx; |
| 88 | switch (type) { |
| 89 | case kNative_GLContextType: |
| tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 90 | glCtx.reset(SkNEW(SkNativeGLContext)); |
| bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 91 | break; |
| 92 | #ifdef SK_ANGLE |
| 93 | case kANGLE_GLContextType: |
| tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 94 | glCtx.reset(SkNEW(SkANGLEGLContext)); |
| bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 95 | break; |
| 96 | #endif |
| 97 | #ifdef SK_MESA |
| 98 | case kMESA_GLContextType: |
| tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 99 | glCtx.reset(SkNEW(SkMesaGLContext)); |
| bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 100 | break; |
| 101 | #endif |
| 102 | case kNull_GLContextType: |
| tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 103 | glCtx.reset(SkNEW(SkNullGLContext)); |
| bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 104 | break; |
| 105 | case kDebug_GLContextType: |
| tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 106 | glCtx.reset(SkNEW(SkDebugGLContext)); |
| bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 107 | break; |
| 108 | } |
| 109 | static const int kBogusSize = 1; |
| 110 | if (!glCtx.get()) { |
| 111 | return NULL; |
| 112 | } |
| 113 | if (!glCtx.get()->init(kBogusSize, kBogusSize)) { |
| 114 | return NULL; |
| 115 | } |
| bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 116 | GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glCtx.get()->gl()); |
| 117 | grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx)); |
| bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 118 | if (!grCtx.get()) { |
| 119 | return NULL; |
| 120 | } |
| 121 | GPUContext& ctx = fContexts.push_back(); |
| 122 | ctx.fGLContext = glCtx.get(); |
| 123 | ctx.fGLContext->ref(); |
| 124 | ctx.fGrContext = grCtx.get(); |
| 125 | ctx.fGrContext->ref(); |
| 126 | ctx.fType = type; |
| 127 | return ctx.fGrContext; |
| 128 | } |
| keyar@chromium.org | 5bdef29 | 2012-08-14 22:02:48 +0000 | [diff] [blame] | 129 | |
| 130 | // Returns the GLContext of the given type. If it has not been created yet, |
| 131 | // NULL is returned instead. |
| 132 | SkGLContext* getGLContext(GLContextType type) { |
| 133 | for (int i = 0; i < fContexts.count(); ++i) { |
| 134 | if (fContexts[i].fType == type) { |
| 135 | return fContexts[i].fGLContext; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | return NULL; |
| 140 | } |
| 141 | |
| bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 142 | private: |
| 143 | struct GPUContext { |
| 144 | GLContextType fType; |
| 145 | SkGLContext* fGLContext; |
| 146 | GrContext* fGrContext; |
| 147 | }; |
| 148 | SkTArray<GPUContext, true> fContexts; |
| 149 | }; |
| 150 | |
| 151 | #endif |