1 /* gltest - small OpenGL tearing test program
2 * Copyright (C) 2012-2013 Ralf Jung <post@ralfj.de>
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.
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.
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.
19 #include "glxbackend.h"
24 #include <GL/glxext.h>
28 #error "Valid GL contexts for GLX are: GL1"
31 // attributes for a double buffered visual in RGBA format with at least 4 bits per color
32 static int attrList[] =
34 GLX_RGBA, GLX_DOUBLEBUFFER,
41 VisualID GLXBackend::initialize(Display *display)
43 if (this->display == NULL) { // this implies that the context is also unitialized
44 this->display = display;
45 // debugging: show version information
46 int glxMajor, glxMinor;
47 glXQueryVersion(display, &glxMajor, &glxMinor);
48 printf("Using GLX version %d.%d\n", glxMajor, glxMinor);
49 // check for swap control functions
50 const char *extensions = glXQueryExtensionsString(display, DefaultScreen(display));
51 if (std::string(extensions).find("GLX_MESA_swap_control") != std::string::npos) {
52 funSwapIntervalMesa = (PFNGLXSWAPINTERVALMESAPROC)glXGetProcAddress((const GLubyte*)"glXSwapIntervalMESA");
53 if (funSwapIntervalMesa) printf("glXSwapIntervalMESA is present\n");
56 funSwapIntervalMesa = 0;
58 if (std::string(extensions).find("GLX_EXT_swap_control") != std::string::npos) {
59 funSwapIntervalExt = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress((const GLubyte*)"glXSwapIntervalEXT");
60 if (funSwapIntervalExt) printf("glXSwapIntervalEXT is present\n");
63 funSwapIntervalExt = 0;
66 vi = glXChooseVisual(display, DefaultScreen(display), attrList);
71 void GLXBackend::createContext(Window window)
73 assert(display != NULL && context == None);
74 this->window = window;
75 // create context with that window
76 context = glXCreateContext(display, vi, 0, GL_TRUE);
77 glXMakeCurrent(display, window, context);
78 assert(glXIsDirect(display, context));
81 GLXBackend::~GLXBackend()
83 if (display != NULL) {
84 if (context != None) {
85 glXMakeCurrent(display, None, NULL);
86 glXDestroyContext(display, context);
92 void GLXBackend::swapBuffers() const
94 assert(context != None); // this implies the display is also initialized
95 glXSwapBuffers(display, window);
98 void GLXBackend::setSwapInterval(int i) const
100 assert(context != None);
101 if (funSwapIntervalExt)
102 funSwapIntervalExt(display, window, i);
103 else if (funSwapIntervalMesa)
104 funSwapIntervalMesa(i);
106 fprintf(stderr, "At least one of glXSwapIntervalMESA, glXSwapIntervalEXT must be provided by the system\n");