Big wxGL classes refactoring/cleanup:
[wxWidgets.git] / src / gtk / glcanvas.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/glcanvas.cpp
3 // Purpose: wxGLCanvas, for using OpenGL/Mesa with wxWidgets and GTK
4 // Author: Robert Roebling
5 // Modified by:
6 // Created: 17/08/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if wxUSE_GLCANVAS
16
17 #include "wx/glcanvas.h"
18
19 #ifndef WX_PRECOMP
20 #include "wx/app.h"
21 #include "wx/frame.h"
22 #include "wx/colour.h"
23 #include "wx/module.h"
24 #endif // WX_PRECOMP
25
26 extern "C"
27 {
28 #include "gtk/gtk.h"
29 #include "gdk/gdk.h"
30 #include "gdk/gdkx.h"
31 }
32
33 #include "wx/gtk/win_gtk.h"
34 #include "wx/gtk/private.h"
35
36 //---------------------------------------------------------------------------
37 // global data
38 //---------------------------------------------------------------------------
39
40 XVisualInfo *g_vi = (XVisualInfo*) NULL;
41
42 // ----------------------------------------------------------------------------
43 // helper functions
44 // ----------------------------------------------------------------------------
45
46 // wrapper around glXMakeContextCurrent/glXMakeCurrent depending on GLX
47 // version
48 static void wxMakeContextCurrent(GLXDrawable drawable, GLXContext context)
49 {
50 if (wxGLCanvas::GetGLXVersion() >= 13)
51 glXMakeContextCurrent( GDK_DISPLAY(), drawable, drawable, context);
52 else // GLX <= 1.2 doesn't have glXMakeContextCurrent()
53 glXMakeCurrent( GDK_DISPLAY(), drawable, context);
54 }
55
56 //---------------------------------------------------------------------------
57 // wxGLContext
58 //---------------------------------------------------------------------------
59
60 IMPLEMENT_CLASS(wxGLContext,wxObject)
61
62 wxGLContext::wxGLContext(wxWindow* win, const wxGLContext* other)
63 {
64 wxGLCanvas *gc = (wxGLCanvas*) win;
65
66 if (wxGLCanvas::GetGLXVersion() >= 13)
67 {
68 GLXFBConfig *fbc = gc->m_fbc;
69 wxCHECK_RET( fbc, _T("invalid GLXFBConfig for OpenGl") );
70 m_glContext = glXCreateNewContext( GDK_DISPLAY(), fbc[0], GLX_RGBA_TYPE,
71 other ? other->m_glContext : None,
72 GL_TRUE );
73 }
74 else // GLX <= 1.2
75 {
76 XVisualInfo *vi = (XVisualInfo *) gc->m_vi;
77 wxCHECK_RET( vi, _T("invalid visual for OpenGl") );
78 m_glContext = glXCreateContext( GDK_DISPLAY(), vi,
79 other ? other->m_glContext : None,
80 GL_TRUE );
81 }
82
83 wxASSERT_MSG( m_glContext, _T("Couldn't create OpenGl context") );
84 }
85
86 wxGLContext::~wxGLContext()
87 {
88 if ( !m_glContext )
89 return;
90
91 if ( m_glContext == glXGetCurrentContext() )
92 wxMakeContextCurrent(None, NULL);
93
94 glXDestroyContext( GDK_DISPLAY(), m_glContext );
95 }
96
97 void wxGLContext::SetCurrent(const wxGLCanvas& win) const
98 {
99 if ( !m_glContext )
100 return;
101
102 GdkWindow *window = GTK_PIZZA(win.m_wxwindow)->bin_window;
103 wxCHECK_RET( window, _T("window must be shown") );
104
105 wxMakeContextCurrent(GDK_WINDOW_XWINDOW(window), m_glContext);
106 }
107
108
109 #if WXWIN_COMPATIBILITY_2_8
110
111 //-----------------------------------------------------------------------------
112 // "realize" from m_wxwindow: used to create m_glContext implicitly
113 //-----------------------------------------------------------------------------
114
115 extern "C" {
116 static gint
117 gtk_glwindow_realized_callback( GtkWidget *WXUNUSED(widget), wxGLCanvas *win )
118 {
119 win->GTKInitImplicitContext();
120
121 return FALSE;
122 }
123 }
124
125 #endif // WXWIN_COMPATIBILITY_2_8
126
127 //-----------------------------------------------------------------------------
128 // "map" from m_wxwindow
129 //-----------------------------------------------------------------------------
130
131 extern "C" {
132 static gint
133 gtk_glwindow_map_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win )
134 {
135 wxPaintEvent event( win->GetId() );
136 event.SetEventObject( win );
137 win->GetEventHandler()->ProcessEvent( event );
138
139 win->m_exposed = false;
140 win->GetUpdateRegion().Clear();
141
142 return FALSE;
143 }
144 }
145
146 //-----------------------------------------------------------------------------
147 // "expose_event" of m_wxwindow
148 //-----------------------------------------------------------------------------
149
150 extern "C" {
151 static gboolean
152 gtk_glwindow_expose_callback( GtkWidget *WXUNUSED(widget), GdkEventExpose *gdk_event, wxGLCanvas *win )
153 {
154 // don't need to install idle handler, its done from "event" signal
155
156 win->m_exposed = true;
157
158 win->GetUpdateRegion().Union( gdk_event->area.x,
159 gdk_event->area.y,
160 gdk_event->area.width,
161 gdk_event->area.height );
162 return false;
163 }
164 }
165
166 //-----------------------------------------------------------------------------
167 // "size_allocate" of m_wxwindow
168 //-----------------------------------------------------------------------------
169
170 extern "C" {
171 static void
172 gtk_glcanvas_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxGLCanvas *win )
173 {
174 if (g_isIdle)
175 wxapp_install_idle_handler();
176
177 if (!win->m_hasVMT)
178 return;
179
180 wxSizeEvent event( wxSize(win->m_width,win->m_height), win->GetId() );
181 event.SetEventObject( win );
182 win->GetEventHandler()->ProcessEvent( event );
183 }
184 }
185
186 //---------------------------------------------------------------------------
187 // wxGlCanvas
188 //---------------------------------------------------------------------------
189
190 IMPLEMENT_CLASS(wxGLCanvas, wxWindow)
191
192 wxGLCanvas::wxGLCanvas(wxWindow *parent,
193 wxWindowID id,
194 const int *attribList,
195 const wxPoint& pos,
196 const wxSize& size,
197 long style,
198 const wxString& name,
199 const wxPalette& palette)
200 #if WXWIN_COMPATIBILITY_2_8
201 : m_createImplicitContext(false)
202 #endif
203 {
204 Create(parent, id, pos, size, style, name, attribList, palette);
205 }
206
207 #if WXWIN_COMPATIBILITY_2_8
208
209 wxGLCanvas::wxGLCanvas(wxWindow *parent,
210 wxWindowID id,
211 const wxPoint& pos,
212 const wxSize& size,
213 long style,
214 const wxString& name,
215 const int *attribList,
216 const wxPalette& palette)
217 : m_createImplicitContext(true)
218 {
219 Create(parent, id, pos, size, style, name, attribList, palette);
220 }
221
222 wxGLCanvas::wxGLCanvas(wxWindow *parent,
223 const wxGLContext *shared,
224 wxWindowID id,
225 const wxPoint& pos,
226 const wxSize& size,
227 long style,
228 const wxString& name,
229 const int *attribList,
230 const wxPalette& palette)
231 : m_createImplicitContext(true)
232 {
233 m_sharedContext = wx_const_cast(wxGLContext *, shared);
234
235 Create(parent, id, pos, size, style, name, attribList, palette);
236 }
237
238 wxGLCanvas::wxGLCanvas(wxWindow *parent,
239 const wxGLCanvas *shared,
240 wxWindowID id,
241 const wxPoint& pos, const wxSize& size,
242 long style, const wxString& name,
243 const int *attribList,
244 const wxPalette& palette )
245 : m_createImplicitContext(true)
246 {
247 m_sharedContextOf = wx_const_cast(wxGLCanvas *, shared);
248
249 Create(parent, id, pos, size, style, name, attribList, palette);
250 }
251
252 #endif // WXWIN_COMPATIBILITY_2_8
253
254 bool wxGLCanvas::Create(wxWindow *parent,
255 wxWindowID id,
256 const wxPoint& pos,
257 const wxSize& size,
258 long style,
259 const wxString& name,
260 const int *attribList,
261 const wxPalette& palette)
262 {
263 m_exposed = false;
264 m_noExpose = true;
265 m_nativeSizeEvent = true;
266 m_fbc = NULL;
267 m_vi = NULL;
268
269 if (wxGLCanvas::GetGLXVersion() >= 13)
270 {
271 // GLX >= 1.3 uses a GLXFBConfig
272 GLXFBConfig * fbc = NULL;
273 if (wxTheApp->m_glFBCInfo != NULL)
274 {
275 fbc = (GLXFBConfig *) wxTheApp->m_glFBCInfo;
276 m_canFreeFBC = false; // owned by wxTheApp - don't free upon destruction
277 }
278 else
279 {
280 fbc = (GLXFBConfig *) wxGLCanvas::ChooseGLFBC(attribList);
281 m_canFreeFBC = true;
282 }
283 m_fbc = fbc; // save for later use
284 wxCHECK_MSG( m_fbc, false, _T("required FBConfig couldn't be found") );
285 }
286
287 XVisualInfo *vi = NULL;
288 if (wxTheApp->m_glVisualInfo != NULL)
289 {
290 vi = (XVisualInfo *)wxTheApp->m_glVisualInfo;
291 m_canFreeVi = false; // owned by wxTheApp - don't free upon destruction
292 }
293 else
294 {
295 if (wxGLCanvas::GetGLXVersion() >= 13)
296 // GLX >= 1.3
297 vi = glXGetVisualFromFBConfig(GDK_DISPLAY(), m_fbc[0]);
298 else
299 // GLX <= 1.2
300 vi = (XVisualInfo *) ChooseGLVisual(attribList);
301
302 m_canFreeVi = true;
303 }
304
305 m_vi = vi; // save for later use
306
307 wxCHECK_MSG( m_vi, false, _T("required visual couldn't be found") );
308 GdkVisual *visual;
309 GdkColormap *colormap;
310
311 // MR: This needs a fix for lower gtk+ versions too. Might need to rethink logic (FIXME)
312 #if defined(__WXGTK20__) && GTK_CHECK_VERSION(2,2,0)
313 if (!gtk_check_version(2,2,0))
314 {
315 wxWindow::Create( parent, id, pos, size, style, name );
316
317 m_glWidget = m_wxwindow;
318
319 GdkScreen *screen = gtk_widget_get_screen( m_glWidget );
320 colormap = gdk_screen_get_default_colormap(screen);
321 visual = gdk_colormap_get_visual(colormap);
322
323 if (GDK_VISUAL_XVISUAL(visual)->visualid != vi->visualid)
324 {
325 visual = gdk_x11_screen_lookup_visual( screen, vi->visualid );
326 colormap = gdk_colormap_new(visual, FALSE);
327 }
328
329 gtk_widget_set_colormap( m_glWidget, colormap );
330 }
331 else
332 #endif // GTK+ >= 2.2
333 {
334 visual = gdkx_visual_get( vi->visualid );
335 colormap = gdk_colormap_new( visual, TRUE );
336
337 gtk_widget_push_colormap( colormap );
338
339 wxWindow::Create( parent, id, pos, size, style, name );
340 m_glWidget = m_wxwindow;
341 }
342
343 gtk_widget_set_double_buffered( m_glWidget, FALSE );
344
345 #if WXWIN_COMPATIBILITY_2_8
346 g_signal_connect(m_wxwindow, "realize", G_CALLBACK(gtk_glwindow_realized_callback), this);
347 #endif // WXWIN_COMPATIBILITY_2_8
348 g_signal_connect(m_wxwindow, "map", G_CALLBACK(gtk_glwindow_map_callback), this);
349 g_signal_connect(m_wxwindow, "expose_event", G_CALLBACK(gtk_glwindow_expose_callback), this);
350 g_signal_connect(m_widget, "size_allocate", G_CALLBACK(gtk_glcanvas_size_callback), this);
351
352 if (gtk_check_version(2,2,0) != NULL)
353 {
354 gtk_widget_pop_colormap();
355 }
356
357 #if WXWIN_COMPATIBILITY_2_8
358 // if our parent window is already visible, we had been realized before we
359 // connected to the "realize" signal and hence our m_glContext hasn't been
360 // initialized yet and we have to do it now
361 if (GTK_WIDGET_REALIZED(m_wxwindow))
362 gtk_glwindow_realized_callback( m_wxwindow, this );
363 #endif // WXWIN_COMPATIBILITY_2_8
364
365 if (GTK_WIDGET_MAPPED(m_wxwindow))
366 gtk_glwindow_map_callback( m_wxwindow, this );
367
368 return true;
369 }
370
371 wxGLCanvas::~wxGLCanvas()
372 {
373 GLXFBConfig * fbc = (GLXFBConfig *) m_fbc;
374 if (fbc && m_canFreeFBC)
375 XFree( fbc );
376
377 XVisualInfo *vi = (XVisualInfo *) m_vi;
378 if (vi && m_canFreeVi)
379 XFree( vi );
380 }
381
382 void* wxGLCanvas::ChooseGLVisual(const int *attribList)
383 {
384 int data[512];
385 GetGLAttribListFromWX( attribList, data );
386
387 Display *dpy = GDK_DISPLAY();
388
389 return glXChooseVisual( dpy, DefaultScreen(dpy), data );
390 }
391
392 void* wxGLCanvas::ChooseGLFBC(const int *attribList)
393 {
394 int data[512];
395 GetGLAttribListFromWX( attribList, data );
396
397 int returned;
398 return glXChooseFBConfig( GDK_DISPLAY(), DefaultScreen(GDK_DISPLAY()),
399 data, &returned );
400 }
401
402
403 void
404 wxGLCanvas::GetGLAttribListFromWX(const int *wx_attribList, int *gl_attribList)
405 {
406 if ( !wx_attribList )
407 {
408 if (wxGLCanvas::GetGLXVersion() >= 13)
409 {
410 // leave GLX >= 1.3 choose the default attributes
411 gl_attribList[0] = 0;
412 }
413 else // GLX < 1.3
414 {
415 int i = 0;
416 // default settings if attriblist = 0
417 gl_attribList[i++] = GLX_RGBA;
418 gl_attribList[i++] = GLX_DOUBLEBUFFER;
419 gl_attribList[i++] = GLX_DEPTH_SIZE; gl_attribList[i++] = 1;
420 gl_attribList[i++] = GLX_RED_SIZE; gl_attribList[i++] = 1;
421 gl_attribList[i++] = GLX_GREEN_SIZE; gl_attribList[i++] = 1;
422 gl_attribList[i++] = GLX_BLUE_SIZE; gl_attribList[i++] = 1;
423 gl_attribList[i++] = GLX_ALPHA_SIZE; gl_attribList[i++] = 0;
424 gl_attribList[i++] = None;
425 }
426 }
427 else // have non-default attributes
428 {
429 int arg=0, p=0;
430 while( (wx_attribList[arg]!=0) && (p<510) )
431 {
432 switch( wx_attribList[arg++] )
433 {
434 case WX_GL_RGBA:
435 if (wxGLCanvas::GetGLXVersion() <= 12)
436 {
437 // for GLX >= 1.3, GLX_RGBA is useless (setting this flags will crash on most opengl implm)
438 gl_attribList[p++] = GLX_RGBA;
439 }
440 break;
441 case WX_GL_BUFFER_SIZE:
442 gl_attribList[p++] = GLX_BUFFER_SIZE;
443 gl_attribList[p++] = wx_attribList[arg++];
444 break;
445 case WX_GL_LEVEL:
446 gl_attribList[p++] = GLX_LEVEL;
447 gl_attribList[p++] = wx_attribList[arg++];
448 break;
449 case WX_GL_DOUBLEBUFFER:
450 gl_attribList[p++] = GLX_DOUBLEBUFFER;
451 gl_attribList[p++] = 1;
452 break;
453 case WX_GL_STEREO:
454 gl_attribList[p++] = GLX_STEREO;
455 break;
456 case WX_GL_AUX_BUFFERS:
457 gl_attribList[p++] = GLX_AUX_BUFFERS;
458 gl_attribList[p++] = wx_attribList[arg++];
459 break;
460 case WX_GL_MIN_RED:
461 gl_attribList[p++] = GLX_RED_SIZE;
462 gl_attribList[p++] = wx_attribList[arg++];
463 break;
464 case WX_GL_MIN_GREEN:
465 gl_attribList[p++] = GLX_GREEN_SIZE;
466 gl_attribList[p++] = wx_attribList[arg++];
467 break;
468 case WX_GL_MIN_BLUE:
469 gl_attribList[p++] = GLX_BLUE_SIZE;
470 gl_attribList[p++] = wx_attribList[arg++];
471 break;
472 case WX_GL_MIN_ALPHA:
473 gl_attribList[p++] = GLX_ALPHA_SIZE;
474 gl_attribList[p++] = wx_attribList[arg++];
475 break;
476 case WX_GL_DEPTH_SIZE:
477 gl_attribList[p++] = GLX_DEPTH_SIZE;
478 gl_attribList[p++] = wx_attribList[arg++];
479 break;
480 case WX_GL_STENCIL_SIZE:
481 gl_attribList[p++] = GLX_STENCIL_SIZE;
482 gl_attribList[p++] = wx_attribList[arg++];
483 break;
484 case WX_GL_MIN_ACCUM_RED:
485 gl_attribList[p++] = GLX_ACCUM_RED_SIZE;
486 gl_attribList[p++] = wx_attribList[arg++];
487 break;
488 case WX_GL_MIN_ACCUM_GREEN:
489 gl_attribList[p++] = GLX_ACCUM_GREEN_SIZE;
490 gl_attribList[p++] = wx_attribList[arg++];
491 break;
492 case WX_GL_MIN_ACCUM_BLUE:
493 gl_attribList[p++] = GLX_ACCUM_BLUE_SIZE;
494 gl_attribList[p++] = wx_attribList[arg++];
495 break;
496 case WX_GL_MIN_ACCUM_ALPHA:
497 gl_attribList[p++] = GLX_ACCUM_ALPHA_SIZE;
498 gl_attribList[p++] = wx_attribList[arg++];
499 break;
500 default:
501 break;
502 }
503 }
504
505 gl_attribList[p] = 0;
506 }
507 }
508
509 /* static */
510 int wxGLCanvas::GetGLXVersion()
511 {
512 static int s_glxVersion = 0;
513 if ( s_glxVersion == 0 )
514 {
515 // check the GLX version
516 int glxMajorVer, glxMinorVer;
517 bool ok = glXQueryVersion(GDK_DISPLAY(), &glxMajorVer, &glxMinorVer);
518 wxASSERT_MSG( ok, _T("GLX version not found") );
519 if (!ok)
520 s_glxVersion = 10; // 1.0 by default
521 else
522 s_glxVersion = glxMajorVer*10 + glxMinorVer;
523 }
524
525 return s_glxVersion;
526 }
527
528 void wxGLCanvas::SwapBuffers()
529 {
530 GdkWindow *window = GTK_PIZZA(m_wxwindow)->bin_window;
531 glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( window ) );
532 }
533
534 void wxGLCanvas::OnInternalIdle()
535 {
536 if (m_exposed)
537 {
538 wxPaintEvent event( GetId() );
539 event.SetEventObject( this );
540 GetEventHandler()->ProcessEvent( event );
541
542 m_exposed = false;
543 GetUpdateRegion().Clear();
544 }
545
546 wxWindow::OnInternalIdle();
547 }
548
549 #if WXWIN_COMPATIBILITY_2_8
550
551 void wxGLCanvas::GTKInitImplicitContext()
552 {
553 if ( !m_glContext && m_createImplicitContext )
554 {
555 wxGLContext *share = m_sharedContext;
556 if ( !share && m_sharedContextOf )
557 share = m_sharedContextOf->m_glContext;
558
559 m_glContext = new wxGLContext(this, share);
560 }
561 }
562
563 #endif // WXWIN_COMPATIBILITY_2_8
564
565 //---------------------------------------------------------------------------
566 // wxGLApp
567 //---------------------------------------------------------------------------
568
569 bool wxGLApp::InitGLVisual(const int *attribList)
570 {
571 if ( wxGLCanvas::GetGLXVersion() >= 13 )
572 {
573 if (m_glFBCInfo)
574 XFree(m_glFBCInfo);
575 m_glFBCInfo = wxGLCanvas::ChooseGLFBC(attribList);
576
577 if ( !m_glFBCInfo )
578 return false;
579
580 if (m_glVisualInfo)
581 XFree(m_glVisualInfo);
582 m_glVisualInfo = glXGetVisualFromFBConfig(GDK_DISPLAY(), ((GLXFBConfig *)m_glFBCInfo)[0]);
583 }
584 else // GLX <= 1.2
585 {
586 if (m_glVisualInfo)
587 XFree(m_glVisualInfo);
588 m_glVisualInfo = wxGLCanvas::ChooseGLVisual(attribList);
589 }
590
591 return m_glVisualInfo != NULL;
592 }
593
594 #endif // wxUSE_GLCANVAS