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