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>
27 /* attributes for a double buffered visual in RGBA format with at least
29 static int attrList[] =
31 GLX_RGBA, GLX_DOUBLEBUFFER,
38 VisualID GLXBackend::initialize(Display *display)
40 if (this->display == NULL) { // this implies that the context is also unitialized
41 this->display = display;
42 // debugging: show version information
43 int glxMajor, glxMinor;
44 glXQueryVersion(display, &glxMajor, &glxMinor);
45 printf("Using GLX version %d.%d\n", glxMajor, glxMinor);
46 // check for swap control functions
47 const char *extensions = glXQueryExtensionsString(display, DefaultScreen(display));
48 if (std::string(extensions).find("GLX_MESA_swap_control") != std::string::npos) {
49 funSwapIntervalMesa = (PFNGLXSWAPINTERVALMESAPROC)glXGetProcAddress((const GLubyte*)"glXSwapIntervalMESA");
50 if (funSwapIntervalMesa) printf("glXSwapIntervalMESA is present\n");
53 funSwapIntervalMesa = 0;
55 if (std::string(extensions).find("GLX_EXT_swap_control") != std::string::npos) {
56 funSwapIntervalExt = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress((const GLubyte*)"glXSwapIntervalEXT");
57 if (funSwapIntervalExt) printf("glXSwapIntervalEXT is present\n");
60 funSwapIntervalExt = 0;
63 vi = glXChooseVisual(display, DefaultScreen(display), attrList);
68 void GLXBackend::createContext(Window window)
70 assert(display != NULL && context == None);
71 this->window = window;
72 // create context with that window
73 context = glXCreateContext(display, vi, 0, GL_TRUE);
74 glXMakeCurrent(display, window, context);
75 assert(glXIsDirect(display, context));
78 GLXBackend::~GLXBackend()
80 if (display != NULL) {
81 if (context != None) {
82 glXMakeCurrent(display, None, NULL);
83 glXDestroyContext(display, context);
89 void GLXBackend::swapBuffers() const
91 assert(context != None); // this implies the display is also initialized
92 glXSwapBuffers(display, window);
95 void GLXBackend::setSwapInterval(int i) const
97 assert(context != None);
98 if (funSwapIntervalExt)
99 funSwapIntervalExt(display, window, i);
100 else if (funSwapIntervalMesa)
101 funSwapIntervalMesa(i);
103 fprintf(stderr, "At least one of glXSwapIntervalMESA, glXSwapIntervalEXT must be provided by the system\n");