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" |
| 22 | |
| 23 | /** |
| 24 | * This is a simple class that is useful in test apps that use different |
| 25 | * GrContexts backed by different types of GL contexts. It manages creating the |
| 26 | * GL context and a GrContext that uses it. The GL/Gr contexts persist until the |
| 27 | * factory is destroyed (though the caller can always grab a ref on the returned |
| 28 | * GrContext to make it outlive the factory). |
| 29 | */ |
| 30 | class GrContextFactory : GrNoncopyable { |
| 31 | public: |
| 32 | /** |
| 33 | * Types of GL contexts supported. |
| 34 | */ |
| 35 | enum GLContextType { |
| 36 | kNative_GLContextType, |
| 37 | #if SK_ANGLE |
| 38 | kANGLE_GLContextType, |
| 39 | #endif |
| 40 | #if SK_MESA |
| 41 | kMESA_GLContextType, |
| 42 | #endif |
| 43 | kNull_GLContextType, |
| 44 | kDebug_GLContextType, |
| 45 | }; |
| 46 | |
| 47 | GrContextFactory() { |
| 48 | } |
| 49 | |
| 50 | ~GrContextFactory() { |
| 51 | for (int i = 0; i < fContexts.count(); ++i) { |
| 52 | fContexts[i].fGrContext->unref(); |
| 53 | fContexts[i].fGLContext->unref(); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Get a GrContext initalized with a type of GL context. |
| 59 | */ |
| 60 | GrContext* get(GLContextType type) { |
| 61 | |
| 62 | for (int i = 0; i < fContexts.count(); ++i) { |
| 63 | if (fContexts[i].fType == type) { |
| 64 | return fContexts[i].fGrContext; |
| 65 | } |
| 66 | } |
| 67 | SkAutoTUnref<SkGLContext> glCtx; |
| 68 | SkAutoTUnref<GrContext> grCtx; |
| 69 | switch (type) { |
| 70 | case kNative_GLContextType: |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame^] | 71 | glCtx.reset(SkNEW(SkNativeGLContext)); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 72 | break; |
| 73 | #ifdef SK_ANGLE |
| 74 | case kANGLE_GLContextType: |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame^] | 75 | glCtx.reset(SkNEW(SkANGLEGLContext)); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 76 | break; |
| 77 | #endif |
| 78 | #ifdef SK_MESA |
| 79 | case kMESA_GLContextType: |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame^] | 80 | glCtx.reset(SkNEW(SkMesaGLContext)); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 81 | break; |
| 82 | #endif |
| 83 | case kNull_GLContextType: |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame^] | 84 | glCtx.reset(SkNEW(SkNullGLContext)); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 85 | break; |
| 86 | case kDebug_GLContextType: |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame^] | 87 | glCtx.reset(SkNEW(SkDebugGLContext)); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 88 | break; |
| 89 | } |
| 90 | static const int kBogusSize = 1; |
| 91 | if (!glCtx.get()) { |
| 92 | return NULL; |
| 93 | } |
| 94 | if (!glCtx.get()->init(kBogusSize, kBogusSize)) { |
| 95 | return NULL; |
| 96 | } |
| 97 | GrPlatform3DContext p3dctx = |
| 98 | reinterpret_cast<GrPlatform3DContext>(glCtx.get()->gl()); |
| 99 | grCtx.reset(GrContext::Create(kOpenGL_Shaders_GrEngine, p3dctx)); |
| 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 | } |
| 111 | private: |
| 112 | struct GPUContext { |
| 113 | GLContextType fType; |
| 114 | SkGLContext* fGLContext; |
| 115 | GrContext* fGrContext; |
| 116 | }; |
| 117 | SkTArray<GPUContext, true> fContexts; |
| 118 | }; |
| 119 | |
| 120 | #endif |