KiCad PCB EDA Suite
gl_utils.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2020 Kicad Developers, see AUTHORS.txt for contributors
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#ifndef GL_UTILS_H
25#define GL_UTILS_H
26
27#include <gal/opengl/kiglew.h> // Must be included first
28#include <wx/glcanvas.h>
29#include <wx/utils.h>
30
31#include <limits>
32
34{
35public:
42 static int SetSwapInterval( int aVal )
43 {
46#if defined( __linux__ ) && !defined( KICAD_USE_EGL )
47
48 if( Display* dpy = glXGetCurrentDisplay() )
49 {
50 GLXDrawable drawable = glXGetCurrentDrawable();
51
52 std::string exts( glXQueryExtensionsString( dpy, DefaultScreen( dpy ) ) );
53
54 if( glXSwapIntervalEXT && glXQueryDrawable && drawable
55 && exts.find( "GLX_EXT_swap_control" ) != std::string::npos )
56 {
57 if( aVal < 0 )
58 {
59 if( exts.find( "GLX_EXT_swap_control_tear" ) == std::string::npos )
60 {
61 aVal = 0;
62 }
63 else
64 {
65 // Even though the extensions might be available,
66 // we need to be sure that late/adaptive swaps are
67 // enabled on the drawable.
68
69 unsigned lateSwapsEnabled = 0;
70 glXQueryDrawable( dpy, drawable, GLX_LATE_SWAPS_TEAR_EXT,
71 &lateSwapsEnabled );
72
73 if( !lateSwapsEnabled )
74 {
75 aVal = 0;
76 }
77 }
78 }
79
80 unsigned clampedInterval;
81 glXSwapIntervalEXT( dpy, drawable, aVal );
82 glXQueryDrawable( dpy, drawable, GLX_SWAP_INTERVAL_EXT, &clampedInterval );
83
84 return clampedInterval;
85 }
86
87 if( glXSwapIntervalMESA && glXGetSwapIntervalMESA
88 && exts.find( "GLX_MESA_swap_control" ) != std::string::npos )
89 {
90 if( aVal < 0 )
91 aVal = 0;
92
93 glXSwapIntervalMESA( aVal );
94 return glXGetSwapIntervalMESA();
95 }
96
97 if( aVal > 0 && glXSwapIntervalSGI
98 && exts.find( "GLX_SGI_swap_control" ) != std::string::npos )
99 {
100 if( glXSwapIntervalSGI( aVal ) )
101 glXSwapIntervalSGI( 1 );
102
103 return 1;
104 }
105 }
106
107 return std::numeric_limits<int>::max();
108#else
109 return 0;
110#endif
111 }
112};
113
114#endif /* GL_CONTEXT_MANAGER_H */
static int SetSwapInterval(int aVal)
Attempts to set the OpenGL swap interval.
Definition: gl_utils.h:42