]> git.saurik.com Git - wxWidgets.git/blame - src/unix/glx11.cpp
added functions for setting the columns order in wxListCtrl (modified patch 1828074)
[wxWidgets.git] / src / unix / glx11.cpp
CommitLineData
498ace9e
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/univ/glx11.cpp
3// Purpose: code common to all X11-based wxGLCanvas implementations
4// Author: Vadim Zeitlin
5// Created: 2007-04-15
6// RCS-ID: $Id$
7// Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org>
8// Licence: wxWindows licence
9///////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19// for compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#if wxUSE_GLCANVAS
23
24#ifndef WX_PRECOMP
50c95f69 25 #include "wx/log.h"
498ace9e
VZ
26#endif //WX_PRECOMP
27
28#include "wx/glcanvas.h"
29
30// ============================================================================
31// wxGLContext implementation
32// ============================================================================
33
34IMPLEMENT_CLASS(wxGLContext, wxObject)
35
36wxGLContext::wxGLContext(wxGLCanvas *gc, const wxGLContext *other)
37{
38 if ( wxGLCanvas::GetGLXVersion() >= 13 )
39 {
40 GLXFBConfig *fbc = gc->GetGLXFBConfig();
41 wxCHECK_RET( fbc, _T("invalid GLXFBConfig for OpenGL") );
42
43 m_glContext = glXCreateNewContext( wxGetX11Display(), fbc[0], GLX_RGBA_TYPE,
44 other ? other->m_glContext : None,
45 GL_TRUE );
46 }
47 else // GLX <= 1.2
48 {
49 XVisualInfo *vi = gc->GetXVisualInfo();
50 wxCHECK_RET( vi, _T("invalid visual for OpenGL") );
51
52 m_glContext = glXCreateContext( wxGetX11Display(), vi,
53 other ? other->m_glContext : None,
54 GL_TRUE );
55 }
56
57 wxASSERT_MSG( m_glContext, _T("Couldn't create OpenGL context") );
58}
59
60wxGLContext::~wxGLContext()
61{
62 if ( !m_glContext )
63 return;
64
65 if ( m_glContext == glXGetCurrentContext() )
66 MakeCurrent(None, NULL);
67
68 glXDestroyContext( wxGetX11Display(), m_glContext );
69}
70
71void wxGLContext::SetCurrent(const wxGLCanvas& win) const
72{
73 if ( !m_glContext )
74 return;
75
76 const Window xid = win.GetXWindow();
77 wxCHECK_RET( xid, _T("window must be shown") );
78
79 MakeCurrent(xid, m_glContext);
80}
81
82// wrapper around glXMakeContextCurrent/glXMakeCurrent depending on GLX
83// version
84/* static */
85void wxGLContext::MakeCurrent(GLXDrawable drawable, GLXContext context)
86{
87 if (wxGLCanvas::GetGLXVersion() >= 13)
88 glXMakeContextCurrent( wxGetX11Display(), drawable, drawable, context);
89 else // GLX <= 1.2 doesn't have glXMakeContextCurrent()
90 glXMakeCurrent( wxGetX11Display(), drawable, context);
91}
92
93// ============================================================================
94// wxGLCanvasX11 implementation
95// ============================================================================
96
97// ----------------------------------------------------------------------------
98// initialization methods and dtor
99// ----------------------------------------------------------------------------
100
101wxGLCanvasX11::wxGLCanvasX11()
102{
103 m_fbc = NULL;
104 m_vi = NULL;
105}
106
107bool wxGLCanvasX11::InitVisual(const int *attribList)
108{
109 return InitXVisualInfo(attribList, &m_fbc, &m_vi);
110}
111
112wxGLCanvasX11::~wxGLCanvasX11()
113{
114 if ( m_fbc && m_fbc != ms_glFBCInfo )
115 XFree(m_fbc);
116
117 if ( m_vi && m_vi != ms_glVisualInfo )
118 XFree(m_vi);
119}
120
121// ----------------------------------------------------------------------------
122// working with GL attributes
123// ----------------------------------------------------------------------------
124
125bool
126wxGLCanvasX11::ConvertWXAttrsToGL(const int *wxattrs, int *glattrs, size_t n)
127{
128 wxCHECK_MSG( n >= 16, false, _T("GL attributes buffer too small") );
129
130 if ( !wxattrs )
131 {
132 if ( GetGLXVersion() >= 13 )
133 {
134 // leave GLX >= 1.3 choose the default attributes
135 glattrs[0] = None;
136 }
137 else // GLX < 1.3
138 {
139 // default settings if attriblist = 0
140 size_t i = 0;
141 glattrs[i++] = GLX_RGBA;
142 glattrs[i++] = GLX_DOUBLEBUFFER;
143 glattrs[i++] = GLX_DEPTH_SIZE; glattrs[i++] = 1;
144 glattrs[i++] = GLX_RED_SIZE; glattrs[i++] = 1;
145 glattrs[i++] = GLX_GREEN_SIZE; glattrs[i++] = 1;
146 glattrs[i++] = GLX_BLUE_SIZE; glattrs[i++] = 1;
147 glattrs[i++] = GLX_ALPHA_SIZE; glattrs[i++] = 0;
148 glattrs[i++] = None;
149
150 wxASSERT_MSG( i < n, _T("GL attributes buffer too small") );
151 }
152 }
153 else // have non-default attributes
154 {
155 size_t p = 0;
156 for ( int arg = 0; wxattrs[arg] != 0; )
157 {
158 // check if we have any space left, knowing that we may insert 2
159 // more elements during this loop iteration and we always need to
160 // terminate the list with None (hence -3)
161 if ( p >= n - 2 )
162 return false;
163
498ace9e
VZ
164 switch ( wxattrs[arg++] )
165 {
166 case WX_GL_RGBA:
167 // for GLX >= 1.3, GLX_RGBA is useless and apparently
168 // harmful for some implementations
169 //
170 // FIXME: is this true?
171 if ( GetGLXVersion() <= 12 )
172 {
173 glattrs[p++] = GLX_RGBA;
174 }
a15107c0
VZ
175
176 // use "continue" to skip the assignment of the attribute
177 // value at the end of the loop
498ace9e
VZ
178 continue;
179
180 case WX_GL_BUFFER_SIZE:
181 glattrs[p++] = GLX_BUFFER_SIZE;
182 break;
183
184 case WX_GL_LEVEL:
185 glattrs[p++] = GLX_LEVEL;
186 break;
187
85866f52
VZ
188 // the following boolean attributes don't have values in wx
189 // API (they're turned on if specified) but do have them in
190 // OpenGL, so do put them into glattrs and also skip the
191 // copy of wx value after switch by using "continue"
192 // instead of "break"
498ace9e
VZ
193 case WX_GL_DOUBLEBUFFER:
194 glattrs[p++] = GLX_DOUBLEBUFFER;
a15107c0 195 glattrs[p++] = True;
498ace9e
VZ
196 continue;
197
198 case WX_GL_STEREO:
199 glattrs[p++] = GLX_STEREO;
85866f52
VZ
200 glattrs[p++] = True;
201 continue;
202
498ace9e
VZ
203
204 case WX_GL_AUX_BUFFERS:
205 glattrs[p++] = GLX_AUX_BUFFERS;
206 break;
207
208 case WX_GL_MIN_RED:
209 glattrs[p++] = GLX_RED_SIZE;
210 break;
211
212 case WX_GL_MIN_GREEN:
213 glattrs[p++] = GLX_GREEN_SIZE;
214 break;
215
216 case WX_GL_MIN_BLUE:
217 glattrs[p++] = GLX_BLUE_SIZE;
218 break;
219
220 case WX_GL_MIN_ALPHA:
221 glattrs[p++] = GLX_ALPHA_SIZE;
222 break;
223
224 case WX_GL_DEPTH_SIZE:
225 glattrs[p++] = GLX_DEPTH_SIZE;
226 break;
227
228 case WX_GL_STENCIL_SIZE:
229 glattrs[p++] = GLX_STENCIL_SIZE;
230 break;
231
232 case WX_GL_MIN_ACCUM_RED:
233 glattrs[p++] = GLX_ACCUM_RED_SIZE;
234 break;
235
236 case WX_GL_MIN_ACCUM_GREEN:
237 glattrs[p++] = GLX_ACCUM_GREEN_SIZE;
238 break;
239
240 case WX_GL_MIN_ACCUM_BLUE:
241 glattrs[p++] = GLX_ACCUM_BLUE_SIZE;
242 break;
243
244 case WX_GL_MIN_ACCUM_ALPHA:
245 glattrs[p++] = GLX_ACCUM_ALPHA_SIZE;
246 break;
247
248 default:
249 wxLogDebug(_T("Unsupported OpenGL attribute %d"),
250 wxattrs[arg - 1]);
251 continue;
252 }
253
254 // copy attribute value as is
255 glattrs[p++] = wxattrs[arg++];
256 }
257
258 glattrs[p] = None;
259 }
260
261 return true;
262}
263
264/* static */
265bool
266wxGLCanvasX11::InitXVisualInfo(const int *attribList,
267 GLXFBConfig **pFBC,
268 XVisualInfo **pXVisual)
269{
270 int data[512];
271 if ( !ConvertWXAttrsToGL(attribList, data, WXSIZEOF(data)) )
272 return false;
273
274 Display * const dpy = wxGetX11Display();
275
276 if ( GetGLXVersion() >= 13 )
277 {
278 int returned;
279 *pFBC = glXChooseFBConfig(dpy, DefaultScreen(dpy), data, &returned);
280
281 if ( *pFBC )
282 {
283 *pXVisual = glXGetVisualFromFBConfig(wxGetX11Display(), **pFBC);
284 if ( !*pXVisual )
285 {
286 XFree(*pFBC);
287 *pFBC = NULL;
288 }
289 }
290 }
291 else // GLX <= 1.2
292 {
293 *pFBC = NULL;
294 *pXVisual = glXChooseVisual(dpy, DefaultScreen(dpy), data);
295 }
296
297 return *pXVisual != NULL;
298}
299
300// ----------------------------------------------------------------------------
301// default visual management
302// ----------------------------------------------------------------------------
303
304XVisualInfo *wxGLCanvasX11::ms_glVisualInfo = NULL;
305GLXFBConfig *wxGLCanvasX11::ms_glFBCInfo = NULL;
306
307/* static */
308bool wxGLCanvasX11::InitDefaultVisualInfo(const int *attribList)
309{
310 FreeDefaultVisualInfo();
311
312 return InitXVisualInfo(attribList, &ms_glFBCInfo, &ms_glVisualInfo);
313}
314
315/* static */
316void wxGLCanvasX11::FreeDefaultVisualInfo()
317{
318 if ( ms_glFBCInfo )
319 {
320 XFree(ms_glFBCInfo);
321 ms_glFBCInfo = NULL;
322 }
323
324 if ( ms_glVisualInfo )
325 {
326 XFree(ms_glVisualInfo);
327 ms_glVisualInfo = NULL;
328 }
329}
330
331// ----------------------------------------------------------------------------
332// other GL methods
333// ----------------------------------------------------------------------------
334
335/* static */
336int wxGLCanvasX11::GetGLXVersion()
337{
338 static int s_glxVersion = 0;
339 if ( s_glxVersion == 0 )
340 {
341 // check the GLX version
342 int glxMajorVer, glxMinorVer;
343 bool ok = glXQueryVersion(wxGetX11Display(), &glxMajorVer, &glxMinorVer);
344 wxASSERT_MSG( ok, _T("GLX version not found") );
345 if (!ok)
346 s_glxVersion = 10; // 1.0 by default
347 else
348 s_glxVersion = glxMajorVer*10 + glxMinorVer;
349 }
350
351 return s_glxVersion;
352}
353
354void wxGLCanvasX11::SwapBuffers()
355{
356 const Window xid = GetXWindow();
357 wxCHECK_RET( xid, _T("window must be shown") );
358
359 glXSwapBuffers(wxGetX11Display(), xid);
360}
361
362bool wxGLCanvasX11::IsShownOnScreen() const
363{
364 return GetXWindow() && wxGLCanvasBase::IsShownOnScreen();
365}
366
367#endif // wxUSE_GLCANVAS
368