OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef GrContextFactory_DEFINED | 8 #ifndef GrContextFactory_DEFINED |
9 #define GrContextFactory_DEFINED | 9 #define GrContextFactory_DEFINED |
10 | 10 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 #endif | 91 #endif |
92 case kNull_GLContextType: | 92 case kNull_GLContextType: |
93 return "null"; | 93 return "null"; |
94 case kDebug_GLContextType: | 94 case kDebug_GLContextType: |
95 return "debug"; | 95 return "debug"; |
96 default: | 96 default: |
97 SkFAIL("Unknown GL Context type."); | 97 SkFAIL("Unknown GL Context type."); |
98 } | 98 } |
99 } | 99 } |
100 | 100 |
101 explicit GrContextFactory(const GrContextOptions& opts); | 101 explicit GrContextFactory(const GrContextOptions& opts) : fGlobalOptions(opt
s) { } |
102 GrContextFactory(); | 102 GrContextFactory() { } |
103 | 103 |
104 ~GrContextFactory(); | 104 ~GrContextFactory() { this->destroyContexts(); } |
105 | 105 |
106 void destroyContexts(); | 106 void destroyContexts() { |
107 void abandonContexts(); | 107 for (int i = 0; i < fContexts.count(); ++i) { |
| 108 if (fContexts[i]->fGLContext) { // could be abandoned. |
| 109 fContexts[i]->fGLContext->makeCurrent(); |
| 110 } |
| 111 fContexts[i]->fGrContext->unref(); |
| 112 SkSafeUnref(fContexts[i]->fGLContext); |
| 113 } |
| 114 fContexts.reset(); |
| 115 } |
| 116 |
| 117 void abandonContexts() { |
| 118 for (int i = 0; i < fContexts.count(); ++i) { |
| 119 if (fContexts[i]->fGLContext) { |
| 120 fContexts[i]->fGLContext->testAbandon(); |
| 121 SkSafeSetNull(fContexts[i]->fGLContext); |
| 122 } |
| 123 fContexts[i]->fGrContext->abandonContext(); |
| 124 } |
| 125 } |
108 | 126 |
109 struct ContextInfo { | 127 struct ContextInfo { |
110 ContextInfo() | 128 GLContextType fType; |
111 : fGrContext(nullptr), fGLContext(nullptr) { } | 129 GLContextOptions fOptions; |
112 ContextInfo(GrContext* grContext, SkGLContext* glContext) | 130 SkGLContext* fGLContext; |
113 : fGrContext(grContext), fGLContext(glContext) { } | 131 GrContext* fGrContext; |
114 GrContext* fGrContext; | |
115 SkGLContext* fGLContext; //! Valid until the factory destroys it via aba
ndonContexts() or | |
116 //! destroyContexts(). | |
117 }; | 132 }; |
118 | |
119 /** | 133 /** |
120 * Get a context initialized with a type of GL context. It also makes the GL
context current. | 134 * Get a context initialized with a type of GL context. It also makes the GL
context current. |
| 135 * Pointer is valid until destroyContexts() is called. |
121 */ | 136 */ |
122 ContextInfo getContextInfo(GLContextType type, | 137 ContextInfo* getContextInfo(GLContextType type, |
123 GLContextOptions options = kNone_GLContextOptions
); | 138 GLContextOptions options = kNone_GLContextOption
s); |
| 139 |
124 /** | 140 /** |
125 * Get a GrContext initialized with a type of GL context. It also makes the
GL context current. | 141 * Get a GrContext initialized with a type of GL context. It also makes the
GL context current. |
126 */ | 142 */ |
127 GrContext* get(GLContextType type, | 143 GrContext* get(GLContextType type, GLContextOptions options = kNone_GLContex
tOptions) { |
128 GLContextOptions options = kNone_GLContextOptions) { | 144 if (ContextInfo* info = this->getContextInfo(type, options)) { |
129 return this->getContextInfo(type, options).fGrContext; | 145 return info->fGrContext; |
| 146 } |
| 147 return nullptr; |
130 } | 148 } |
131 const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; } | 149 const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; } |
132 | 150 |
133 private: | 151 private: |
134 struct Context { | 152 SkTArray<SkAutoTDelete<ContextInfo>, true> fContexts; |
135 GLContextType fType; | 153 const GrContextOptions fGlobalOptions; |
136 GLContextOptions fOptions; | |
137 SkGLContext* fGLContext; | |
138 GrContext* fGrContext; | |
139 }; | |
140 SkTArray<Context, true> fContexts; | |
141 const GrContextOptions fGlobalOptions; | |
142 }; | 154 }; |
143 | 155 |
144 #endif | 156 #endif |
OLD | NEW |