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 | |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 11 | #include "GrContext.h" |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 12 | |
| 13 | #include "gl/SkGLContext.h" |
robertphillips@google.com | a2d7148 | 2012-08-01 20:08:47 +0000 | [diff] [blame] | 14 | #include "SkTArray.h" |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 15 | |
| 16 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 17 | * 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] | 18 | * GrContexts backed by different types of GL contexts. It manages creating the |
| 19 | * GL context and a GrContext that uses it. The GL/Gr contexts persist until the |
| 20 | * factory is destroyed (though the caller can always grab a ref on the returned |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 21 | * Gr and GL contexts to make them outlive the factory). |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 22 | */ |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 23 | class GrContextFactory : SkNoncopyable { |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 24 | public: |
| 25 | /** |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 26 | * Types of GL contexts supported. For historical and testing reasons the native GrContext will |
| 27 | * not use "GL_NV_path_rendering" even when the driver supports it. There is a separate context |
| 28 | * type that does not remove NVPR support and which will fail when the driver does not support |
| 29 | * the extension. |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 30 | */ |
| 31 | enum GLContextType { |
| 32 | kNative_GLContextType, |
| 33 | #if SK_ANGLE |
| 34 | kANGLE_GLContextType, |
| 35 | #endif |
| 36 | #if SK_MESA |
| 37 | kMESA_GLContextType, |
| 38 | #endif |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 39 | /** Similar to kNative but does not filter NVPR. It will fail if the GL driver does not |
| 40 | support NVPR */ |
| 41 | kNVPR_GLContextType, |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 42 | kNull_GLContextType, |
| 43 | kDebug_GLContextType, |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 44 | |
| 45 | kLastGLContextType = kDebug_GLContextType |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 48 | static const int kGLContextTypeCnt = kLastGLContextType + 1; |
| 49 | |
| 50 | static bool IsRenderingGLContext(GLContextType type) { |
| 51 | switch (type) { |
| 52 | case kNull_GLContextType: |
| 53 | case kDebug_GLContextType: |
| 54 | return false; |
| 55 | default: |
| 56 | return true; |
| 57 | } |
| 58 | } |
| 59 | |
bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 60 | static const char* GLContextTypeName(GLContextType type) { |
| 61 | switch (type) { |
| 62 | case kNative_GLContextType: |
| 63 | return "native"; |
| 64 | case kNull_GLContextType: |
| 65 | return "null"; |
| 66 | #if SK_ANGLE |
| 67 | case kANGLE_GLContextType: |
| 68 | return "angle"; |
| 69 | #endif |
| 70 | #if SK_MESA |
| 71 | case kMESA_GLContextType: |
| 72 | return "mesa"; |
| 73 | #endif |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 74 | case kNVPR_GLContextType: |
| 75 | return "nvpr"; |
bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 76 | case kDebug_GLContextType: |
| 77 | return "debug"; |
| 78 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 79 | SkFAIL("Unknown GL Context type."); |
bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
krajcevski | 69a5560 | 2014-08-13 10:46:31 -0700 | [diff] [blame] | 83 | explicit GrContextFactory(const GrContext::Options& opts) : fGlobalOptions(opts) { } |
kkinnunen | 80549fc | 2014-06-30 06:36:31 -0700 | [diff] [blame] | 84 | GrContextFactory() { } |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 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) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 90 | if (fContexts[i].fGLContext) { // could be abandoned. |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 91 | fContexts[i].fGLContext->makeCurrent(); |
| 92 | } |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 93 | fContexts[i].fGrContext->unref(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 94 | if (fContexts[i].fGLContext) { |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 95 | fContexts[i].fGLContext->unref(); |
| 96 | } |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 97 | } |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 98 | fContexts.reset(); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 99 | } |
| 100 | |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 101 | void abandonContexts() { |
| 102 | for (int i = 0; i < fContexts.count(); ++i) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 103 | if (fContexts[i].fGLContext) { |
bsalomon | 944bcf0 | 2014-07-29 08:01:52 -0700 | [diff] [blame] | 104 | fContexts[i].fGLContext->testAbandon(); |
| 105 | SkSafeSetNull(fContexts[i].fGLContext); |
| 106 | } |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 107 | fContexts[i].fGrContext->abandonContext(); |
| 108 | } |
| 109 | } |
| 110 | |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 111 | /** |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 112 | * 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] | 113 | */ |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 114 | GrContext* get(GLContextType type, GrGLStandard forcedGpuAPI = kNone_GrGLStandard); |
kkinnunen | 80549fc | 2014-06-30 06:36:31 -0700 | [diff] [blame] | 115 | |
keyar@chromium.org | 5bdef29 | 2012-08-14 22:02:48 +0000 | [diff] [blame] | 116 | |
| 117 | // Returns the GLContext of the given type. If it has not been created yet, |
| 118 | // NULL is returned instead. |
kkinnunen | 9e61bb7 | 2014-10-09 05:24:15 -0700 | [diff] [blame] | 119 | SkGLContext* getGLContext(GLContextType type) { |
keyar@chromium.org | 5bdef29 | 2012-08-14 22:02:48 +0000 | [diff] [blame] | 120 | for (int i = 0; i < fContexts.count(); ++i) { |
| 121 | if (fContexts[i].fType == type) { |
| 122 | return fContexts[i].fGLContext; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return NULL; |
| 127 | } |
| 128 | |
krajcevski | b1aded8 | 2014-08-18 07:52:17 -0700 | [diff] [blame] | 129 | const GrContext::Options& getGlobalOptions() const { return fGlobalOptions; } |
| 130 | |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 131 | private: |
| 132 | struct GPUContext { |
| 133 | GLContextType fType; |
kkinnunen | 9e61bb7 | 2014-10-09 05:24:15 -0700 | [diff] [blame] | 134 | SkGLContext* fGLContext; |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 135 | GrContext* fGrContext; |
| 136 | }; |
krajcevski | 69a5560 | 2014-08-13 10:46:31 -0700 | [diff] [blame] | 137 | SkTArray<GPUContext, true> fContexts; |
| 138 | const GrContext::Options fGlobalOptions; |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | #endif |