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: |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 26 | enum GLContextType { |
rmistry | 0f515bd | 2015-12-22 10:22:26 -0800 | [diff] [blame^] | 27 | kNative_GLContextType, |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 28 | #if SK_ANGLE |
rmistry | 0f515bd | 2015-12-22 10:22:26 -0800 | [diff] [blame^] | 29 | kANGLE_GLContextType, |
| 30 | kANGLE_GL_GLContextType, |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 31 | #endif |
hendrikw | 885bf09 | 2015-08-27 10:38:39 -0700 | [diff] [blame] | 32 | #if SK_COMMAND_BUFFER |
rmistry | 0f515bd | 2015-12-22 10:22:26 -0800 | [diff] [blame^] | 33 | kCommandBuffer_GLContextType, |
hendrikw | 885bf09 | 2015-08-27 10:38:39 -0700 | [diff] [blame] | 34 | #endif |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 35 | #if SK_MESA |
rmistry | 0f515bd | 2015-12-22 10:22:26 -0800 | [diff] [blame^] | 36 | kMESA_GLContextType, |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 37 | #endif |
rmistry | 0f515bd | 2015-12-22 10:22:26 -0800 | [diff] [blame^] | 38 | kNull_GLContextType, |
| 39 | kDebug_GLContextType, |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 40 | kLastGLContextType = kDebug_GLContextType |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 43 | static const int kGLContextTypeCnt = kLastGLContextType + 1; |
| 44 | |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 45 | /** |
| 46 | * Options for GL context creation. For historical and testing reasons the options will default |
| 47 | * to not using GL_NV_path_rendering extension even when the driver supports it. |
| 48 | */ |
| 49 | enum GLContextOptions { |
| 50 | kNone_GLContextOptions = 0, |
| 51 | kEnableNVPR_GLContextOptions = 0x1, |
| 52 | }; |
| 53 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 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"; |
rmistry | 0f515bd | 2015-12-22 10:22:26 -0800 | [diff] [blame^] | 68 | case kNull_GLContextType: |
| 69 | return "null"; |
bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 70 | #if SK_ANGLE |
| 71 | case kANGLE_GLContextType: |
| 72 | return "angle"; |
hendrikw | eddbefb | 2015-09-11 13:07:29 -0700 | [diff] [blame] | 73 | case kANGLE_GL_GLContextType: |
| 74 | return "angle-gl"; |
bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 75 | #endif |
hendrikw | 885bf09 | 2015-08-27 10:38:39 -0700 | [diff] [blame] | 76 | #if SK_COMMAND_BUFFER |
| 77 | case kCommandBuffer_GLContextType: |
| 78 | return "commandbuffer"; |
| 79 | #endif |
bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 80 | #if SK_MESA |
| 81 | case kMESA_GLContextType: |
| 82 | return "mesa"; |
| 83 | #endif |
| 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) { |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 98 | if (fContexts[i]->fGLContext) { // could be abandoned. |
| 99 | fContexts[i]->fGLContext->makeCurrent(); |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 100 | } |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 101 | fContexts[i]->fGrContext->unref(); |
| 102 | SkSafeUnref(fContexts[i]->fGLContext); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 103 | } |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 104 | fContexts.reset(); |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 105 | } |
| 106 | |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 107 | void abandonContexts() { |
| 108 | for (int i = 0; i < fContexts.count(); ++i) { |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 109 | if (fContexts[i]->fGLContext) { |
| 110 | fContexts[i]->fGLContext->testAbandon(); |
| 111 | SkSafeSetNull(fContexts[i]->fGLContext); |
bsalomon | 944bcf0 | 2014-07-29 08:01:52 -0700 | [diff] [blame] | 112 | } |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 113 | fContexts[i]->fGrContext->abandonContext(); |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 117 | struct ContextInfo { |
| 118 | GLContextType fType; |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 119 | GLContextOptions fOptions; |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 120 | SkGLContext* fGLContext; |
| 121 | GrContext* fGrContext; |
| 122 | }; |
| 123 | /** |
| 124 | * Get a context initialized with a type of GL context. It also makes the GL context current. |
| 125 | * Pointer is valid until destroyContexts() is called. |
| 126 | */ |
rmistry | 0f515bd | 2015-12-22 10:22:26 -0800 | [diff] [blame^] | 127 | ContextInfo* getContextInfo(GLContextType type, GrGLStandard forcedGpuAPI = kNone_GrGLStandard, |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 128 | GLContextOptions options = kNone_GLContextOptions); |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 129 | |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 130 | /** |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 131 | * 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] | 132 | */ |
rmistry | 0f515bd | 2015-12-22 10:22:26 -0800 | [diff] [blame^] | 133 | GrContext* get(GLContextType type, GrGLStandard forcedGpuAPI = kNone_GrGLStandard, |
| 134 | GLContextOptions options = kNone_GLContextOptions) { |
| 135 | if (ContextInfo* info = this->getContextInfo(type, forcedGpuAPI, options)) { |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 136 | return info->fGrContext; |
| 137 | } |
| 138 | return nullptr; |
| 139 | } |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 140 | const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; } |
krajcevski | b1aded8 | 2014-08-18 07:52:17 -0700 | [diff] [blame] | 141 | |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 142 | private: |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 143 | SkTArray<SkAutoTDelete<ContextInfo>, true> fContexts; |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 144 | const GrContextOptions fGlobalOptions; |
bsalomon@google.com | 7361f54 | 2012-04-19 19:15:35 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | #endif |