forked from funkey/gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenGl.cpp
250 lines (163 loc) · 5.59 KB
/
OpenGl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <util/Logger.h>
#include "ContextSettings.h"
#include "GlContext.h"
#include "GlContextCreator.h"
#include "OpenGl.h"
logger::LogChannel opengllog("opengllog", "[OpenGl] ");
namespace gui {
namespace priv {
void glCheckError(const char* file, const char* function, unsigned int line) {
GLenum error = glGetError();
if (error != GL_NO_ERROR)
BOOST_THROW_EXCEPTION(
boost::enable_error_info(OpenGlError())
<< boost::throw_function(function)
<< boost::throw_file(file)
<< boost::throw_line(line)
<< error_message(static_cast<const char*>((void*)gluErrorString(error)))
<< STACK_TRACE);
}
} // namespace priv
OpenGl* TheOpenGl = 0;
OpenGl::OpenGl() :
_globalContext(0) {
LOG_DEBUG(opengllog) << "creating global glxContext" << std::endl;
// use default context settings
ContextSettings defaultSettings;
// create offline context (has a dummy window)
_globalContext = new GlContext(defaultSettings);
// set active for this thread
if (!_globalContext->activate())
LOG_ERROR(opengllog) << "failed to activate global context" << std::endl;
// initialize GLEW for extension checks
if (glewInit() != GLEW_OK)
LOG_ERROR(opengllog) << "GLEW did not initialize" << std::endl;
// see if pixel buffer objects are supported
if (!glewIsSupported("GL_ARB_pixel_buffer_object"))
LOG_ERROR(opengllog) << "pixel buffer extension not supported" << std::endl;
LOG_ALL(opengllog) << "Initialized" << std::endl;
}
OpenGl::~OpenGl() {
LOG_ALL(opengllog) << "destructing..." << std::endl;
_globalContext->activate(false);
if (_globalContext)
delete _globalContext;
LOG_ALL(opengllog) << "destructed" << std::endl;
}
OpenGl*
OpenGl::getInstance() {
if (TheOpenGl == 0)
TheOpenGl = new OpenGl();
return TheOpenGl;
}
boost::mutex&
OpenGl::getMutex() {
return getInstance()->_mutex;
}
GlContext*
OpenGl::getGlobalContext() {
return getInstance()->_globalContext;
}
void
OpenGl::flush() {
LOG_ALL(opengllog) << "attempting to flush current context" << std::endl;
GlContext* context = getInstance()->_context.get();
if (context != 0)
context->flush();
else
LOG_ALL(opengllog) << "there is no current context in this thread" << std::endl;
}
OpenGl::Guard::Guard() :
_openGl(getInstance()) {
LOG_ALL(opengllog) << "[Guard] creating new guard" << std::endl;
// if no context was set for this thread
if (_openGl->_context.get() == 0) {
LOG_ALL(opengllog) << "[Guard] creating new context for this thread" << std::endl;
// create a new offline context
ContextSettings defaultSettings;
GlContext* context = new GlContext(defaultSettings, OpenGl::getGlobalContext());
// set it
_openGl->_context.reset(context);
}
// if current context is active already
if (_openGl->_context->isActive()) {
LOG_ALL(opengllog) << "[Guard] current context is active already -- nothing to do here" << std::endl;
_deactivateContext = false;
return;
}
LOG_ALL(opengllog) << "[Guard] activating context" << std::endl;
// activate the context for this thread
if (!_openGl->_context->activate())
LOG_ERROR(opengllog) << "[Guard] failed to activate context for this thread" << std::endl;
// remember to deactivate it on destruction
_deactivateContext = true;
}
OpenGl::Guard::Guard(GlContextCreator* contextCreator) :
_openGl(getInstance()) {
LOG_ALL(opengllog) << "[Guard] creating new factory guard" << std::endl;
if (contextCreator == 0) {
LOG_ALL(opengllog) << "[Guard] destructing current thread's context" << std::endl;
invalidateCurrentContext();
_deactivateContext = false;
return;
}
LOG_ALL(opengllog) << "[Guard] ensuring valid context" << std::endl;
// remember to deactivate the context on destruction
_deactivateContext = true;
if (getPreviousContextCreator() == contextCreator && reusePreviousContext()) {
LOG_ALL(opengllog) << "[Guard] could reuse previous context from the same creator" << std::endl;
return;
} else {
LOG_ALL(opengllog) << "[Guard] previous context not present or invalid -- create a new one" << std::endl;
createNewContex(contextCreator);
}
}
OpenGl::Guard::~Guard() {
LOG_ALL(opengllog) << "[Guard] destructing" << std::endl;
if (_deactivateContext) {
LOG_ALL(opengllog) << "[Guard] deactivating context" << std::endl;
// deactivate the context for this thread
_openGl->_context->activate(false);
}
}
GlContextCreator*
OpenGl::Guard::getPreviousContextCreator() {
if (_openGl->_contextCreator.get() != 0)
return (*_openGl->_contextCreator.get());
else
return 0;
}
bool
OpenGl::Guard::createNewContex(GlContextCreator* contextCreator) {
// set the current context to the one given by contextCreator
_openGl->_context.reset(contextCreator->createGlContext());
// remember who created this context
_openGl->_contextCreator.reset(new GlContextCreator*(contextCreator));
// activate the new context
if (!_openGl->_context->activate()) {
LOG_ERROR(opengllog) << "[Guard] failed to activate newly created context" << std::endl;
return false;
}
return true;
}
bool
OpenGl::Guard::reusePreviousContext() {
// is there a previous context and creator?
if (_openGl->_context.get() == 0 || _openGl->_contextCreator.get() == 0)
return false;
// activate the previous context for this thread and check whether it is
// still valid
if (_openGl->_context->activate()) {
LOG_ALL(opengllog) << "[Guard] previous context still working" << std::endl;
// the context was valid, everything is fine
return true;
}
// the previous context got invalid
return false;
}
void
OpenGl::Guard::invalidateCurrentContext() {
_openGl->_context.reset();
_openGl->_contextCreator.reset();
}
} // namespace gui