ff126eb25ed7fdf5889b357b06aa5ab5291633c6
[gltest.git] / glxbackend.cpp
1 #include "glxbackend.h"
2
3 #include <stdio.h>
4 #include <assert.h>
5 #include <GL/glxext.h>
6 #include <string>
7
8 /* attributes for a double buffered visual in RGBA format with at least
9 * 4 bits per color */                       
10 static int attrList[] =                                             
11 {                                                                      
12         GLX_RGBA, GLX_DOUBLEBUFFER,
13         GLX_RED_SIZE, 4,                                                   
14         GLX_GREEN_SIZE, 4,                                                 
15         GLX_BLUE_SIZE, 4,
16         None                                                               
17 };
18
19 VisualID GLXBackend::initialize(Display *display)
20 {
21         if (this->display == NULL) { // this implies that the context is also unitialized
22                 this->display = display;
23                 // debugging: show version information
24                 int glxMajor, glxMinor;
25                 glXQueryVersion(display, &glxMajor, &glxMinor);
26                 printf("Using GLX version %d.%d\n", glxMajor, glxMinor);
27                 // check for swap control functions
28                 const char *extensions = glXQueryExtensionsString(display, DefaultScreen(display));
29                 if (std::string(extensions).find("GLX_MESA_swap_control") != std::string::npos) {
30                         funSwapIntervalMesa = (PFNGLXSWAPINTERVALMESAPROC)glXGetProcAddress((const GLubyte*)"glXSwapIntervalMESA");
31                         if (funSwapIntervalMesa) printf("glXSwapIntervalMESA is present\n");
32                 }
33                 else {
34                         funSwapIntervalMesa = 0;
35                 }
36                 if (std::string(extensions).find("GLX_EXT_swap_control") != std::string::npos) {
37                         funSwapIntervalExt = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress((const GLubyte*)"glXSwapIntervalEXT");
38                         if (funSwapIntervalExt) printf("glXSwapIntervalEXT is present\n");
39                 }
40                 else {
41                         funSwapIntervalExt = 0;
42                 }
43                 // get the visual
44                 vi = glXChooseVisual(display, DefaultScreen(display), attrList);
45         }
46         return vi->visualid;
47 }
48
49 void GLXBackend::createContext(Window window)
50 {
51         assert(display != NULL && context == None);
52         this->window = window;
53         // create context with that window
54         context = glXCreateContext(display, vi, 0, GL_TRUE);                             
55         glXMakeCurrent(display, window, context);                                      
56         assert(glXIsDirect(display, context));
57 }
58
59 GLXBackend::~GLXBackend()
60 {
61         if (display != NULL) {
62                 if (context != None) {
63                         glXMakeCurrent(display, None, NULL);
64                         glXDestroyContext(display, context);
65                 }
66                 XFree(vi);
67         }
68 }
69
70 void GLXBackend::swapBuffers() const
71 {
72         assert(context != None); // this implies the display is also initialized
73         glXSwapBuffers(display, window);
74 }
75
76 void GLXBackend::setSwapInterval(int i) const
77 {
78         assert(context != None);
79         if (funSwapIntervalExt)
80                 funSwapIntervalExt(display, window, i);
81         else if (funSwapIntervalMesa)
82                 funSwapIntervalMesa(i);
83         else
84                 assert(false && "At least one of glXSwapIntervalMESA, glXSwapIntervalEXT must be provided by the system");
85 }