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 4 bits per color
28 static int attrList[] =
30 GLX_RGBA, GLX_DOUBLEBUFFER,
37 VisualID GLXBackend::initialize(Display *display)
39 if (this->display == NULL) { // this implies that the context is also unitialized
40 this->display = display;
41 // debugging: show version information
42 int glxMajor, glxMinor;
43 glXQueryVersion(display, &glxMajor, &glxMinor);
44 printf("Using GLX version %d.%d\n", glxMajor, glxMinor);
45 // check for swap control functions
46 const char *extensions = glXQueryExtensionsString(display, DefaultScreen(display));
47 if (std::string(extensions).find("GLX_MESA_swap_control") != std::string::npos) {
48 funSwapIntervalMesa = (PFNGLXSWAPINTERVALMESAPROC)glXGetProcAddress((const GLubyte*)"glXSwapIntervalMESA");
49 if (funSwapIntervalMesa) printf("glXSwapIntervalMESA is present\n");
52 funSwapIntervalMesa = 0;
54 if (std::string(extensions).find("GLX_EXT_swap_control") != std::string::npos) {
55 funSwapIntervalExt = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress((const GLubyte*)"glXSwapIntervalEXT");
56 if (funSwapIntervalExt) printf("glXSwapIntervalEXT is present\n");
59 funSwapIntervalExt = 0;
62 vi = glXChooseVisual(display, DefaultScreen(display), attrList);
67 void GLXBackend::createContext(Window window)
69 assert(display != NULL && context == None);
70 this->window = window;
71 // create context with that window
72 context = glXCreateContext(display, vi, 0, GL_TRUE);
73 glXMakeCurrent(display, window, context);
74 assert(glXIsDirect(display, context));
77 GLXBackend::~GLXBackend()
79 if (display != NULL) {
80 if (context != None) {
81 glXMakeCurrent(display, None, NULL);
82 glXDestroyContext(display, context);
88 void GLXBackend::swapBuffers() const
90 assert(context != None); // this implies the display is also initialized
91 glXSwapBuffers(display, window);
94 void GLXBackend::setSwapInterval(int i) const
96 assert(context != None);
97 if (funSwapIntervalExt)
98 funSwapIntervalExt(display, window, i);
99 else if (funSwapIntervalMesa)
100 funSwapIntervalMesa(i);
102 fprintf(stderr, "At least one of glXSwapIntervalMESA, glXSwapIntervalEXT must be provided by the system\n");