fix build
[gltest.git] / glxbackend.cpp
1 /* gltest - small OpenGL tearing test program
2 * Copyright (C) 2012-2013 Ralf Jung <post@ralfj.de>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #include "glxbackend.h"
20 #include "glutil.h"
21
22 #include <stdio.h>
23 #include <assert.h>
24 #include <GL/glxext.h>
25 #include <string>
26
27 #if !defined(CON_GL1) && !defined(CON_GL3)
28 #error "Valid GL contexts for GLX are: GL1, GL3"
29 #endif
30
31 // attributes for a double buffered framebuffer in RGBA format with at least 4 bits per color
32 static int configAttribs[] =                                             
33 {
34     GLX_RENDER_TYPE, GLX_RGBA_BIT,
35     GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
36     GLX_DOUBLEBUFFER, True,
37     GLX_RED_SIZE, 4,
38     GLX_GREEN_SIZE, 4,
39     GLX_BLUE_SIZE, 4,
40     None
41 };
42
43 #ifdef CON_GL3
44 // attributes for a GL3 context
45 static int contextAttribs[] =
46 {
47     GLX_CONTEXT_MAJOR_VERSION_ARB,               3,
48     GLX_CONTEXT_MINOR_VERSION_ARB,               0,
49     GLX_CONTEXT_FLAGS_ARB,                       GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
50     None
51 };
52 #endif
53
54 VisualID GLXBackend::initialize(Display *display)
55 {
56     if (this->display == NULL) { // this implies that the context is also unitialized
57         this->display = display;
58         // debugging: show version information
59         int glxMajor, glxMinor;
60         glXQueryVersion(display, &glxMajor, &glxMinor);
61         printf("Using GLX version: %d.%d\n", glxMajor, glxMinor);
62         if (glxMajor == 1 && glxMinor < 4) {
63             // glXChooseFBConfig and glXCreateNewContext require GLX 1.3; GLX_ARB_create_context requires GLX 1.4
64             die("Need at least GLX 1.4 to function properly\n");
65         }
66         // check for extension-based functions
67         funSwapIntervalMesa = (PFNGLXSWAPINTERVALMESAPROC)resolveGLXFunction("GLX_MESA_swap_control", "glXSwapIntervalMESA");
68         funSwapIntervalExt = (PFNGLXSWAPINTERVALEXTPROC)resolveGLXFunction("GLX_EXT_swap_control", "glXSwapIntervalEXT");
69         funCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)resolveGLXFunction("GLX_ARB_create_context", "glXCreateContextAttribsARB");
70         // get the first usable framebuffer configuration
71         int count = 0;
72         GLXFBConfig *configs = glXChooseFBConfig(display, DefaultScreen(display), configAttribs, &count);
73         if (count < 1) {
74             die("Failed to choose framebuffer configuration\n");
75         }
76         config = configs[0];
77         XFree(configs);
78     }
79     // return visual ID
80     XVisualInfo *vi = glXGetVisualFromFBConfig(display, config);
81     if (vi== NULL) {
82         die("The GLXFBConfig I got is invalid\n");
83     }
84     VisualID visualid = vi->visualid;
85     XFree(vi);
86     return visualid;
87 }
88
89 bool GLXBackend::haveGLXExtension(const std::string &name)
90 {
91     assert(display != NULL);
92     std::string extensions = glXQueryExtensionsString(display, DefaultScreen(display));
93     return (std::string(" "+extensions+" ").find(" "+name+" ") != std::string::npos);
94 }
95
96 T_proc GLXBackend::resolveGLXFunction(const char *extension, const char *function)
97 {
98     if (!haveGLXExtension(extension)) return NULL;
99     T_proc f = glXGetProcAddress((const GLubyte*)function);
100     if (f)
101         printf("%s is supported, using it for %s\n", extension, function);
102     else
103         printf("%s is supported, but I could not find %s\n", extension, function);
104     return f;
105 }
106
107 void GLXBackend::createContext(Window window)
108 {
109     assert(display != NULL && context == None);
110     this->window = window;
111     // create context with that window
112 #ifdef CON_GL1
113     context = glXCreateNewContext(display, config, GLX_RGBA_TYPE, NULL, GL_TRUE);
114 #else
115     if (!funCreateContextAttribsARB) {
116         die("Cannot create GL3 context: GLX_ARB_create_context not supported\n");
117     }
118     context = funCreateContextAttribsARB(display, config, NULL, GL_TRUE, contextAttribs);
119 #endif
120     if (!context) {
121         die("Error creating context\n");
122     }
123     glXMakeCurrent(display, window, context);                                      
124     assert(glXIsDirect(display, context));
125     printf("Using GL version: %s\n", glGetString(GL_VERSION));
126     // initialise GL utilities
127     resolveFunctionPointers((T_glGetProcAddress)glXGetProcAddress);
128 }
129
130 GLXBackend::~GLXBackend()
131 {
132     if (display != NULL) {
133         if (context != None) {
134             glXMakeCurrent(display, None, NULL);
135             glXDestroyContext(display, context);
136         }
137     }
138 }
139
140 void GLXBackend::swapBuffers() const
141 {
142     assert(context != None); // this implies the display is also initialized
143     glXSwapBuffers(display, window);
144 }
145
146 void GLXBackend::setSwapInterval(int i) const
147 {
148     assert(context != None);
149     // check if swap interval value is supported
150     if (i < 0) {
151         die("Cannot set swap interval to %d, must not be negative\n", i);
152     }
153     // set it
154     if (funSwapIntervalExt)
155         funSwapIntervalExt(display, window, i);
156     else if (funSwapIntervalMesa)
157         funSwapIntervalMesa(i);
158     else {
159         die("At least one of GLX_EXT_swap_control, GLX_MESA_swap_control must be supported by the system\n");
160     }
161 }