Michal Suchánek 0c36e16292 Replace glew with epoxy
Glew has the problem that it has to be selected at build time if GLX or
EGL is supported by the library, and this in not encoded in the library
name, nor ABI, nor anything.

Then it's easy to get into the situation that a binary is built but
cannot run because glew supports an API different from the one used by
wxWidgets, or the binary fails to link in the end after all objects are
compiled.

epoxy can support both with the same library avoiding this problem.

epoxy is not initialized explicitly, replaced initialization with
version check where one was not done already.

It seems to be available as vcpkg https://vcpkg.link/ports/libepoxy

There are problems related to GL context switching on Windows which does
not seem to be used in kicad
https://github.com/anholt/libepoxy#known-issues-when-running-on-windows
There is also a problem related to multithreaded rendering on Windows
https://github.com/anholt/libepoxy/pull/265 It's harder to tell if
threading is used for rendering but it does not look like kicad is doing
anything complex enough to warrant using multiple rendering threads.

Fixes https://gitlab.com/kicad/code/kicad/-/issues/20630
Fixes https://gitlab.com/kicad/code/kicad/-/issues/12543
2025-04-22 12:54:39 -07:00

173 lines
5.3 KiB
C++

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright The KiCad Developers, see AUTHORS.txt for contributors
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-3.0.html
* or you may search the http://www.gnu.org website for the version 3 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef GL_UTILS_H
#define GL_UTILS_H
#include <gal/opengl/kiepoxy.h> // Must be included first
#include <wx/glcanvas.h>
#include <wx/utils.h>
#ifdef _WIN32
#ifdef __MINGW32__
#pragma GCC push_options
#pragma GCC optimize( "O0" )
#else
#pragma optimize( "", off )
#endif
#endif
class GL_UTILS
{
public:
/**
* Attempt to set the OpenGL swap interval.
*
* @param aVal if -1 = try to set adaptive swapping, 0 = sync off, 1 = sync with VSYNC rate.
* @return actual value set
*/
static int SetSwapInterval( int aVal )
{
#if defined( __linux__ ) && !defined( KICAD_USE_EGL )
if( Display* dpy = glXGetCurrentDisplay() )
{
GLXDrawable drawable = glXGetCurrentDrawable();
std::string exts( glXQueryExtensionsString( dpy, DefaultScreen( dpy ) ) );
if( glXSwapIntervalEXT && glXQueryDrawable && drawable
&& exts.find( "GLX_EXT_swap_control" ) != std::string::npos )
{
if( aVal == -1 )
{
if( exts.find( "GLX_EXT_swap_control_tear" ) == std::string::npos )
{
aVal = 1;
}
else
{
// Even though the extensions might be available,
// we need to be sure that late/adaptive swaps are
// enabled on the drawable.
unsigned lateSwapsEnabled = 0;
glXQueryDrawable( dpy, drawable, GLX_LATE_SWAPS_TEAR_EXT,
&lateSwapsEnabled );
if( !lateSwapsEnabled )
{
aVal = 0;
}
}
}
unsigned clampedInterval;
glXSwapIntervalEXT( dpy, drawable, aVal );
glXQueryDrawable( dpy, drawable, GLX_SWAP_INTERVAL_EXT, &clampedInterval );
return clampedInterval;
}
if( glXSwapIntervalMESA && glXGetSwapIntervalMESA
&& exts.find( "GLX_MESA_swap_control" ) != std::string::npos )
{
if( aVal == -1 )
aVal = 1;
if( !glXSwapIntervalMESA( aVal ) )
return aVal;
}
if( glXSwapIntervalSGI && exts.find( "GLX_SGI_swap_control" ) != std::string::npos )
{
if( aVal == -1 )
aVal = 1;
if( !glXSwapIntervalSGI( aVal ) )
return aVal;
}
}
#elif defined( _WIN32 )
const GLubyte* vendor = glGetString( GL_VENDOR );
const GLubyte* version = glGetString( GL_VERSION );
if( wglSwapIntervalEXT && wxGLCanvas::IsExtensionSupported( "WGL_EXT_swap_control" ) )
{
wxString vendorStr = vendor;
wxString versionStr = version;
if( aVal == -1 && ( !wxGLCanvas::IsExtensionSupported( "WGL_EXT_swap_control_tear" ) ) )
aVal = 1;
// Trying to enable adaptive swapping on AMD drivers from 2017 or older leads to crash
if( aVal == -1 && vendorStr == wxS( "ATI Technologies Inc." ) )
{
wxArrayString parts = wxSplit( versionStr.AfterLast( ' ' ), '.', 0 );
if( parts.size() == 4 )
{
long majorVer = 0;
if( parts[0].ToLong( &majorVer ) )
{
if( majorVer <= 22 )
aVal = 1;
}
}
}
HDC hdc = wglGetCurrentDC();
HGLRC hglrc = wglGetCurrentContext();
if( hdc && hglrc )
{
int currentInterval = wglGetSwapIntervalEXT();
if( currentInterval != aVal )
{
wglSwapIntervalEXT( aVal );
currentInterval = wglGetSwapIntervalEXT();
}
return currentInterval;
}
}
#endif
return 0;
}
};
#ifdef _WIN32
#ifdef __MINGW32__
#pragma GCC pop_options
#else
#pragma optimize( "", on )
#endif
#endif
#endif /* GL_CONTEXT_MANAGER_H */