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