2024-01-20 13:21:35 +00:00
|
|
|
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
|
2024-09-01 12:08:31 +00:00
|
|
|
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
|
2022-12-04 11:03:45 +00:00
|
|
|
|
2024-02-25 09:22:25 +00:00
|
|
|
#include "opengl_context_egl.h"
|
2023-08-13 03:42:02 +00:00
|
|
|
|
|
|
|
#include "common/assert.h"
|
2024-02-25 05:32:26 +00:00
|
|
|
#include "common/dynamic_library.h"
|
2024-01-20 13:21:35 +00:00
|
|
|
#include "common/error.h"
|
2024-02-25 05:32:26 +00:00
|
|
|
#include "common/log.h"
|
2023-08-13 03:42:02 +00:00
|
|
|
|
2024-02-25 05:32:26 +00:00
|
|
|
#include <atomic>
|
2024-02-18 08:16:46 +00:00
|
|
|
#include <cstring>
|
2021-01-30 16:25:05 +00:00
|
|
|
#include <optional>
|
|
|
|
#include <vector>
|
2023-08-13 03:42:02 +00:00
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
LOG_CHANNEL(GPUDevice);
|
2020-05-07 12:48:13 +00:00
|
|
|
|
2024-02-25 05:32:26 +00:00
|
|
|
static DynamicLibrary s_egl_library;
|
|
|
|
static std::atomic_uint32_t s_egl_refcount = 0;
|
|
|
|
|
|
|
|
static bool LoadEGL()
|
|
|
|
{
|
|
|
|
// We're not going to be calling this from multiple threads concurrently.
|
|
|
|
// So, not wrapping this in a mutex should be fine.
|
|
|
|
if (s_egl_refcount.fetch_add(1, std::memory_order_acq_rel) == 0)
|
|
|
|
{
|
|
|
|
DebugAssert(!s_egl_library.IsOpen());
|
|
|
|
|
2024-03-10 09:35:42 +00:00
|
|
|
std::string egl_libname = DynamicLibrary::GetVersionedFilename("libEGL");
|
2024-05-23 10:55:28 +00:00
|
|
|
INFO_LOG("Loading EGL from {}...", egl_libname);
|
2024-02-25 05:32:26 +00:00
|
|
|
|
|
|
|
Error error;
|
|
|
|
if (!s_egl_library.Open(egl_libname.c_str(), &error))
|
2024-03-10 09:35:42 +00:00
|
|
|
{
|
|
|
|
// Try versioned.
|
|
|
|
egl_libname = DynamicLibrary::GetVersionedFilename("libEGL", 1);
|
2024-05-23 10:55:28 +00:00
|
|
|
INFO_LOG("Loading EGL from {}...", egl_libname);
|
2024-03-10 09:35:42 +00:00
|
|
|
if (!s_egl_library.Open(egl_libname.c_str(), &error))
|
2024-05-23 10:55:28 +00:00
|
|
|
ERROR_LOG("Failed to load EGL: {}", error.GetDescription());
|
2024-03-10 09:35:42 +00:00
|
|
|
}
|
2024-02-25 05:32:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s_egl_library.IsOpen();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void UnloadEGL()
|
|
|
|
{
|
|
|
|
DebugAssert(s_egl_refcount.load(std::memory_order_acquire) > 0);
|
|
|
|
if (s_egl_refcount.fetch_sub(1, std::memory_order_acq_rel) == 1)
|
|
|
|
{
|
2024-05-23 10:55:28 +00:00
|
|
|
INFO_LOG("Unloading EGL.");
|
2024-02-25 05:32:26 +00:00
|
|
|
s_egl_library.Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool LoadGLADEGL(EGLDisplay display, Error* error)
|
|
|
|
{
|
2024-02-18 08:16:46 +00:00
|
|
|
const int version =
|
|
|
|
gladLoadEGL(display, [](const char* name) { return (GLADapiproc)s_egl_library.GetSymbolAddress(name); });
|
|
|
|
if (version == 0)
|
2024-02-25 05:32:26 +00:00
|
|
|
{
|
|
|
|
Error::SetStringView(error, "Loading GLAD EGL functions failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-05-23 10:55:28 +00:00
|
|
|
DEV_LOG("GLAD EGL Version: {}.{}", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version));
|
2024-02-25 05:32:26 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
OpenGLContextEGL::OpenGLContextEGL() : OpenGLContext()
|
2023-08-13 03:42:02 +00:00
|
|
|
{
|
2024-02-25 05:32:26 +00:00
|
|
|
LoadEGL();
|
2023-08-13 03:42:02 +00:00
|
|
|
}
|
2020-05-07 12:48:13 +00:00
|
|
|
|
2024-02-25 09:22:25 +00:00
|
|
|
OpenGLContextEGL::~OpenGLContextEGL()
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
if (m_context != EGL_NO_CONTEXT && eglGetCurrentContext() == m_context)
|
|
|
|
eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
|
|
|
|
|
|
|
if (m_pbuffer_surface != EGL_NO_SURFACE)
|
|
|
|
eglDestroySurface(m_display, m_pbuffer_surface);
|
|
|
|
|
|
|
|
if (m_context != EGL_NO_CONTEXT)
|
|
|
|
eglDestroyContext(m_display, m_context);
|
|
|
|
|
2024-02-25 05:32:26 +00:00
|
|
|
UnloadEGL();
|
2020-05-07 12:48:13 +00:00
|
|
|
}
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
std::unique_ptr<OpenGLContext> OpenGLContextEGL::Create(WindowInfo& wi, SurfaceHandle* surface,
|
|
|
|
std::span<const Version> versions_to_try, Error* error)
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
std::unique_ptr<OpenGLContextEGL> context = std::make_unique<OpenGLContextEGL>();
|
|
|
|
if (!context->Initialize(wi, surface, versions_to_try, error))
|
2020-05-07 12:48:13 +00:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
bool OpenGLContextEGL::Initialize(WindowInfo& wi, SurfaceHandle* surface, std::span<const Version> versions_to_try,
|
|
|
|
Error* error)
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
2024-02-25 05:32:26 +00:00
|
|
|
if (!LoadGLADEGL(EGL_NO_DISPLAY, error))
|
2020-05-07 12:48:13 +00:00
|
|
|
return false;
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
m_display = GetPlatformDisplay(wi, error);
|
2024-02-18 08:16:46 +00:00
|
|
|
if (m_display == EGL_NO_DISPLAY)
|
2024-02-25 05:32:26 +00:00
|
|
|
return false;
|
|
|
|
|
2020-05-07 12:48:13 +00:00
|
|
|
int egl_major, egl_minor;
|
|
|
|
if (!eglInitialize(m_display, &egl_major, &egl_minor))
|
|
|
|
{
|
2024-01-20 13:21:35 +00:00
|
|
|
const int gerror = static_cast<int>(eglGetError());
|
|
|
|
Error::SetStringFmt(error, "eglInitialize() failed: {} (0x{:X})", gerror, gerror);
|
2020-05-07 12:48:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-05-23 10:55:28 +00:00
|
|
|
DEV_LOG("eglInitialize() version: {}.{}", egl_major, egl_minor);
|
2024-02-18 08:16:46 +00:00
|
|
|
|
|
|
|
// Re-initialize EGL/GLAD.
|
|
|
|
if (!LoadGLADEGL(m_display, error))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!GLAD_EGL_KHR_surfaceless_context)
|
2024-05-23 10:55:28 +00:00
|
|
|
WARNING_LOG("EGL implementation does not support surfaceless contexts, emulating with pbuffers");
|
2020-11-07 11:41:25 +00:00
|
|
|
|
2024-01-20 13:21:35 +00:00
|
|
|
for (const Version& cv : versions_to_try)
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
if (CreateContextAndSurface(wi, surface, cv, nullptr, true, error))
|
2020-05-07 12:48:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-01-20 13:21:35 +00:00
|
|
|
Error::SetStringView(error, "Failed to create any context versions");
|
2020-05-07 12:48:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
EGLDisplay OpenGLContextEGL::GetPlatformDisplay(const WindowInfo& wi, Error* error)
|
2021-01-30 16:25:05 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
EGLDisplay dpy =
|
|
|
|
TryGetPlatformDisplay(wi.display_connection, EGL_PLATFORM_SURFACELESS_MESA, "EGL_MESA_platform_surfaceless");
|
2024-02-18 08:16:46 +00:00
|
|
|
if (dpy == EGL_NO_DISPLAY)
|
2024-10-12 12:18:48 +00:00
|
|
|
dpy = GetFallbackDisplay(wi.display_connection, error);
|
2024-02-18 08:16:46 +00:00
|
|
|
|
|
|
|
return dpy;
|
|
|
|
}
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
EGLSurface OpenGLContextEGL::CreatePlatformSurface(EGLConfig config, const WindowInfo& wi, Error* error)
|
2024-02-29 10:03:40 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
EGLSurface surface = TryCreatePlatformSurface(config, wi.window_handle, error);
|
|
|
|
if (surface == EGL_NO_SURFACE)
|
|
|
|
surface = CreateFallbackSurface(config, wi.window_handle, error);
|
2024-02-29 10:03:40 +00:00
|
|
|
return surface;
|
|
|
|
}
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
bool OpenGLContextEGL::SupportsSurfaceless() const
|
|
|
|
{
|
|
|
|
return GLAD_EGL_KHR_surfaceless_context;
|
|
|
|
}
|
|
|
|
|
|
|
|
EGLDisplay OpenGLContextEGL::TryGetPlatformDisplay(void* display, EGLenum platform, const char* platform_ext)
|
2024-02-18 08:16:46 +00:00
|
|
|
{
|
|
|
|
const char* extensions_str = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
|
|
|
|
if (!extensions_str)
|
2021-01-30 16:25:05 +00:00
|
|
|
{
|
2024-05-23 10:55:28 +00:00
|
|
|
ERROR_LOG("No extensions supported.");
|
2024-02-18 08:16:46 +00:00
|
|
|
return EGL_NO_DISPLAY;
|
2021-01-30 16:25:05 +00:00
|
|
|
}
|
|
|
|
|
2024-02-18 08:16:46 +00:00
|
|
|
EGLDisplay dpy = EGL_NO_DISPLAY;
|
2024-02-29 10:03:40 +00:00
|
|
|
if (platform_ext && std::strstr(extensions_str, platform_ext))
|
2024-02-18 08:16:46 +00:00
|
|
|
{
|
2024-05-23 10:55:28 +00:00
|
|
|
DEV_LOG("Using EGL platform {}.", platform_ext);
|
2024-02-18 08:16:46 +00:00
|
|
|
|
2024-02-29 10:03:40 +00:00
|
|
|
PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display_ext =
|
|
|
|
(PFNEGLGETPLATFORMDISPLAYEXTPROC)eglGetProcAddress("eglGetPlatformDisplayEXT");
|
|
|
|
if (get_platform_display_ext)
|
2024-02-18 08:16:46 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
dpy = get_platform_display_ext(platform, display, nullptr);
|
2024-02-29 10:03:40 +00:00
|
|
|
m_use_ext_platform_base = (dpy != EGL_NO_DISPLAY);
|
|
|
|
if (!m_use_ext_platform_base)
|
2024-02-18 08:16:46 +00:00
|
|
|
{
|
|
|
|
const EGLint err = eglGetError();
|
2024-05-23 10:55:28 +00:00
|
|
|
ERROR_LOG("eglGetPlatformDisplayEXT() failed: {} (0x{:X})", err, err);
|
2024-02-18 08:16:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-05-23 10:55:28 +00:00
|
|
|
WARNING_LOG("eglGetPlatformDisplayEXT() was not found");
|
2024-02-18 08:16:46 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-29 10:03:40 +00:00
|
|
|
else
|
2024-02-18 08:16:46 +00:00
|
|
|
{
|
2024-05-23 10:55:28 +00:00
|
|
|
WARNING_LOG("{} is not supported.", platform_ext);
|
2024-02-29 10:03:40 +00:00
|
|
|
}
|
2024-02-18 08:16:46 +00:00
|
|
|
|
2024-02-29 10:03:40 +00:00
|
|
|
return dpy;
|
|
|
|
}
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
EGLSurface OpenGLContextEGL::TryCreatePlatformSurface(EGLConfig config, void* window, Error* error)
|
2024-02-29 10:03:40 +00:00
|
|
|
{
|
|
|
|
EGLSurface surface = EGL_NO_SURFACE;
|
|
|
|
if (m_use_ext_platform_base)
|
|
|
|
{
|
|
|
|
PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC create_platform_window_surface_ext =
|
|
|
|
(PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC)eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT");
|
|
|
|
if (create_platform_window_surface_ext)
|
2024-02-18 08:16:46 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
surface = create_platform_window_surface_ext(m_display, config, window, nullptr);
|
2024-02-29 10:03:40 +00:00
|
|
|
if (surface == EGL_NO_SURFACE)
|
2024-02-18 08:16:46 +00:00
|
|
|
{
|
|
|
|
const EGLint err = eglGetError();
|
2024-02-29 10:03:40 +00:00
|
|
|
Error::SetStringFmt(error, "eglCreatePlatformWindowSurfaceEXT() failed: {} (0x{:X})", err, err);
|
2024-02-18 08:16:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-05-23 10:55:28 +00:00
|
|
|
ERROR_LOG("eglCreatePlatformWindowSurfaceEXT() not found");
|
2024-02-18 08:16:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-29 10:03:40 +00:00
|
|
|
return surface;
|
2024-02-18 08:16:46 +00:00
|
|
|
}
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
EGLDisplay OpenGLContextEGL::GetFallbackDisplay(void* display, Error* error)
|
2024-02-18 08:16:46 +00:00
|
|
|
{
|
2024-05-23 10:55:28 +00:00
|
|
|
WARNING_LOG("Using fallback eglGetDisplay() path.");
|
2024-02-29 10:03:40 +00:00
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
EGLDisplay dpy = eglGetDisplay((EGLNativeDisplayType)display);
|
2024-02-18 08:16:46 +00:00
|
|
|
if (dpy == EGL_NO_DISPLAY)
|
|
|
|
{
|
|
|
|
const EGLint err = eglGetError();
|
|
|
|
Error::SetStringFmt(error, "eglGetDisplay() failed: {} (0x{:X})", err, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dpy;
|
|
|
|
}
|
|
|
|
|
2024-02-29 10:03:40 +00:00
|
|
|
EGLSurface OpenGLContextEGL::CreateFallbackSurface(EGLConfig config, void* win, Error* error)
|
2024-02-18 08:16:46 +00:00
|
|
|
{
|
2024-05-23 10:55:28 +00:00
|
|
|
WARNING_LOG("Using fallback eglCreateWindowSurface() path.");
|
2024-02-18 08:16:46 +00:00
|
|
|
|
2024-02-29 10:03:40 +00:00
|
|
|
EGLSurface surface = eglCreateWindowSurface(m_display, config, (EGLNativeWindowType)win, nullptr);
|
2024-02-18 08:16:46 +00:00
|
|
|
if (surface == EGL_NO_SURFACE)
|
|
|
|
{
|
|
|
|
const EGLint err = eglGetError();
|
|
|
|
Error::SetStringFmt(error, "eglCreateWindowSurface() failed: {} (0x{:X})", err, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return surface;
|
2021-01-30 16:25:05 +00:00
|
|
|
}
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
void OpenGLContextEGL::DestroyPlatformSurface(EGLSurface surface)
|
|
|
|
{
|
|
|
|
eglDestroySurface(m_display, surface);
|
|
|
|
}
|
|
|
|
|
2024-02-25 09:22:25 +00:00
|
|
|
void* OpenGLContextEGL::GetProcAddress(const char* name)
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
|
|
|
return reinterpret_cast<void*>(eglGetProcAddress(name));
|
|
|
|
}
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
OpenGLContext::SurfaceHandle OpenGLContextEGL::CreateSurface(WindowInfo& wi, Error* error /* = nullptr */)
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
if (wi.IsSurfaceless()) [[unlikely]]
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
Error::SetStringView(error, "Trying to create a surfaceless surface.");
|
|
|
|
return nullptr;
|
2020-05-07 12:48:13 +00:00
|
|
|
}
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
EGLSurface surface = CreatePlatformSurface(m_config, wi, error);
|
|
|
|
if (surface == EGL_NO_SURFACE)
|
|
|
|
return nullptr;
|
2020-05-07 12:48:13 +00:00
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
UpdateWindowInfoSize(wi, surface);
|
|
|
|
return (SurfaceHandle)surface;
|
|
|
|
}
|
2020-05-07 12:48:13 +00:00
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
void OpenGLContextEGL::DestroySurface(SurfaceHandle handle)
|
|
|
|
{
|
|
|
|
// pbuffer surface?
|
|
|
|
if (!handle)
|
|
|
|
return;
|
|
|
|
|
|
|
|
EGLSurface surface = (EGLSurface)handle;
|
|
|
|
if (eglGetCurrentSurface(EGL_DRAW) == surface)
|
|
|
|
eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
|
|
|
|
|
|
|
DestroyPlatformSurface(surface);
|
2020-05-07 12:48:13 +00:00
|
|
|
}
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
void OpenGLContextEGL::ResizeSurface(WindowInfo& wi, SurfaceHandle handle)
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
if (!handle)
|
|
|
|
return;
|
2020-09-19 03:04:32 +00:00
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
UpdateWindowInfoSize(wi, (EGLSurface)handle);
|
2020-05-07 12:48:13 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 09:22:25 +00:00
|
|
|
bool OpenGLContextEGL::SwapBuffers()
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
return eglSwapBuffers(m_display, m_current_surface);
|
2020-05-07 12:48:13 +00:00
|
|
|
}
|
|
|
|
|
2024-04-11 10:31:57 +00:00
|
|
|
bool OpenGLContextEGL::IsCurrent() const
|
2022-12-08 03:17:20 +00:00
|
|
|
{
|
|
|
|
return m_context && eglGetCurrentContext() == m_context;
|
|
|
|
}
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
bool OpenGLContextEGL::MakeCurrent(SurfaceHandle surface, Error* error /* = nullptr */)
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
EGLSurface esurface = surface ? (EGLSurface)surface : GetSurfacelessSurface();
|
|
|
|
if (esurface == m_current_surface)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!eglMakeCurrent(m_display, esurface, esurface, m_context)) [[unlikely]]
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
Error::SetStringFmt(error, "eglMakeCurrent() failed: 0x{:X}", eglGetError());
|
2020-05-07 12:48:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
m_current_surface = esurface;
|
2020-05-07 12:48:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-02-25 09:22:25 +00:00
|
|
|
bool OpenGLContextEGL::DoneCurrent()
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
if (!eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_current_surface = EGL_NO_SURFACE;
|
|
|
|
return true;
|
2020-05-07 12:48:13 +00:00
|
|
|
}
|
|
|
|
|
2024-04-11 10:31:57 +00:00
|
|
|
bool OpenGLContextEGL::SupportsNegativeSwapInterval() const
|
|
|
|
{
|
|
|
|
return m_supports_negative_swap_interval;
|
|
|
|
}
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
bool OpenGLContextEGL::SetSwapInterval(s32 interval, Error* error /* = nullptr */)
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
if (!eglSwapInterval(m_display, interval))
|
|
|
|
{
|
|
|
|
Error::SetStringFmt(error, "eglMakeCurrent() failed: 0x{:X}", eglGetError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2020-05-07 12:48:13 +00:00
|
|
|
}
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
std::unique_ptr<OpenGLContext> OpenGLContextEGL::CreateSharedContext(WindowInfo& wi, SurfaceHandle* surface,
|
|
|
|
Error* error)
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
std::unique_ptr<OpenGLContextEGL> context = std::make_unique<OpenGLContextEGL>();
|
2020-05-07 12:48:13 +00:00
|
|
|
context->m_display = m_display;
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
if (!context->CreateContextAndSurface(wi, surface, m_version, m_context, false, error))
|
2024-02-25 05:32:26 +00:00
|
|
|
{
|
|
|
|
Error::SetStringView(error, "Failed to create context/surface");
|
2020-05-07 12:48:13 +00:00
|
|
|
return nullptr;
|
2024-02-25 05:32:26 +00:00
|
|
|
}
|
2020-05-07 12:48:13 +00:00
|
|
|
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
EGLSurface OpenGLContextEGL::GetSurfacelessSurface()
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
return SupportsSurfaceless() ? EGL_NO_SURFACE : GetPBufferSurface(nullptr);
|
2020-05-07 12:48:13 +00:00
|
|
|
}
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
EGLSurface OpenGLContextEGL::GetPBufferSurface(Error* error)
|
2020-11-07 11:41:25 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
if (m_pbuffer_surface)
|
|
|
|
return m_pbuffer_surface;
|
2020-11-07 11:41:25 +00:00
|
|
|
|
|
|
|
EGLint attrib_list[] = {
|
2024-10-12 12:18:48 +00:00
|
|
|
EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE,
|
2020-11-07 11:41:25 +00:00
|
|
|
};
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
m_pbuffer_surface = eglCreatePbufferSurface(m_display, m_config, attrib_list);
|
|
|
|
if (!m_pbuffer_surface) [[unlikely]]
|
2020-11-07 11:41:25 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
if (error)
|
|
|
|
error->SetStringFmt("eglCreatePbufferSurface() failed: {}", eglGetError());
|
|
|
|
else
|
|
|
|
ERROR_LOG("eglCreatePbufferSurface() failed: {}", eglGetError());
|
2020-11-07 11:41:25 +00:00
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2023-10-31 15:52:28 +00:00
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
DEV_LOG("Created pbuffer surface");
|
|
|
|
return m_pbuffer_surface;
|
2020-11-07 11:41:25 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 09:22:25 +00:00
|
|
|
bool OpenGLContextEGL::CheckConfigSurfaceFormat(EGLConfig config, GPUTexture::Format format)
|
2021-01-30 16:25:05 +00:00
|
|
|
{
|
|
|
|
int red_size, green_size, blue_size, alpha_size;
|
|
|
|
if (!eglGetConfigAttrib(m_display, config, EGL_RED_SIZE, &red_size) ||
|
|
|
|
!eglGetConfigAttrib(m_display, config, EGL_GREEN_SIZE, &green_size) ||
|
|
|
|
!eglGetConfigAttrib(m_display, config, EGL_BLUE_SIZE, &blue_size) ||
|
|
|
|
!eglGetConfigAttrib(m_display, config, EGL_ALPHA_SIZE, &alpha_size))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (format)
|
|
|
|
{
|
2023-08-13 03:42:02 +00:00
|
|
|
case GPUTexture::Format::RGBA8:
|
2021-01-30 16:25:05 +00:00
|
|
|
return (red_size == 8 && green_size == 8 && blue_size == 8 && alpha_size == 8);
|
|
|
|
|
2023-08-13 03:42:02 +00:00
|
|
|
case GPUTexture::Format::RGB565:
|
2021-01-30 16:25:05 +00:00
|
|
|
return (red_size == 5 && green_size == 6 && blue_size == 5);
|
|
|
|
|
2023-08-13 03:42:02 +00:00
|
|
|
case GPUTexture::Format::RGBA5551:
|
|
|
|
return (red_size == 5 && green_size == 5 && blue_size == 5 && alpha_size == 1);
|
|
|
|
|
2023-10-31 15:52:28 +00:00
|
|
|
case GPUTexture::Format::Unknown:
|
|
|
|
return true;
|
|
|
|
|
2021-01-30 16:25:05 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
void OpenGLContextEGL::UpdateWindowInfoSize(WindowInfo& wi, EGLSurface surface) const
|
2023-10-31 15:52:28 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
// Some implementations may require the size to be queried at runtime.
|
|
|
|
EGLint surface_width, surface_height;
|
|
|
|
if (eglQuerySurface(m_display, surface, EGL_WIDTH, &surface_width) &&
|
|
|
|
eglQuerySurface(m_display, surface, EGL_HEIGHT, &surface_height))
|
|
|
|
{
|
|
|
|
wi.surface_width = static_cast<u16>(surface_width);
|
|
|
|
wi.surface_height = static_cast<u16>(surface_height);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ERROR_LOG("eglQuerySurface() failed: 0x{:X}", eglGetError());
|
|
|
|
}
|
|
|
|
|
2023-10-31 15:52:28 +00:00
|
|
|
int red_size = 0, green_size = 0, blue_size = 0, alpha_size = 0;
|
|
|
|
eglGetConfigAttrib(m_display, m_config, EGL_RED_SIZE, &red_size);
|
|
|
|
eglGetConfigAttrib(m_display, m_config, EGL_GREEN_SIZE, &green_size);
|
|
|
|
eglGetConfigAttrib(m_display, m_config, EGL_BLUE_SIZE, &blue_size);
|
|
|
|
eglGetConfigAttrib(m_display, m_config, EGL_ALPHA_SIZE, &alpha_size);
|
|
|
|
|
2024-06-30 14:28:05 +00:00
|
|
|
if (red_size == 5 && green_size == 6 && blue_size == 5)
|
2023-10-31 15:52:28 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
wi.surface_format = GPUTexture::Format::RGB565;
|
2023-10-31 15:52:28 +00:00
|
|
|
}
|
2024-06-30 14:28:05 +00:00
|
|
|
else if (red_size == 5 && green_size == 5 && blue_size == 5 && alpha_size == 1)
|
2023-10-31 15:52:28 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
wi.surface_format = GPUTexture::Format::RGBA5551;
|
2023-10-31 15:52:28 +00:00
|
|
|
}
|
|
|
|
else if (red_size == 8 && green_size == 8 && blue_size == 8 && alpha_size == 8)
|
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
wi.surface_format = GPUTexture::Format::RGBA8;
|
2023-10-31 15:52:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-05-23 10:55:28 +00:00
|
|
|
ERROR_LOG("Unknown surface format: R={}, G={}, B={}, A={}", red_size, green_size, blue_size, alpha_size);
|
2024-10-12 12:18:48 +00:00
|
|
|
wi.surface_format = GPUTexture::Format::RGBA8;
|
2021-01-31 15:27:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
bool OpenGLContextEGL::CreateContext(bool surfaceless, GPUTexture::Format surface_format, const Version& version,
|
|
|
|
EGLContext share_context, Error* error)
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
2024-05-23 10:55:28 +00:00
|
|
|
DEV_LOG("Trying version {}.{} ({})", version.major_version, version.minor_version,
|
|
|
|
version.profile == OpenGLContext::Profile::ES ?
|
|
|
|
"ES" :
|
|
|
|
(version.profile == OpenGLContext::Profile::Core ? "Core" : "None"));
|
2020-05-07 12:48:13 +00:00
|
|
|
int surface_attribs[16] = {
|
|
|
|
EGL_RENDERABLE_TYPE,
|
|
|
|
(version.profile == Profile::ES) ?
|
|
|
|
((version.major_version >= 3) ? EGL_OPENGL_ES3_BIT :
|
|
|
|
((version.major_version == 2) ? EGL_OPENGL_ES2_BIT : EGL_OPENGL_ES_BIT)) :
|
|
|
|
EGL_OPENGL_BIT,
|
|
|
|
EGL_SURFACE_TYPE,
|
2024-10-12 12:18:48 +00:00
|
|
|
surfaceless ? 0 : EGL_WINDOW_BIT,
|
2020-05-07 12:48:13 +00:00
|
|
|
};
|
|
|
|
int nsurface_attribs = 4;
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
if (surface_format == GPUTexture::Format::Unknown)
|
|
|
|
surface_format = GPUTexture::Format::RGBA8;
|
2020-05-07 12:48:13 +00:00
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
switch (surface_format)
|
2023-08-13 03:42:02 +00:00
|
|
|
{
|
|
|
|
case GPUTexture::Format::RGBA8:
|
2020-05-07 12:48:13 +00:00
|
|
|
surface_attribs[nsurface_attribs++] = EGL_RED_SIZE;
|
|
|
|
surface_attribs[nsurface_attribs++] = 8;
|
|
|
|
surface_attribs[nsurface_attribs++] = EGL_GREEN_SIZE;
|
|
|
|
surface_attribs[nsurface_attribs++] = 8;
|
|
|
|
surface_attribs[nsurface_attribs++] = EGL_BLUE_SIZE;
|
|
|
|
surface_attribs[nsurface_attribs++] = 8;
|
|
|
|
surface_attribs[nsurface_attribs++] = EGL_ALPHA_SIZE;
|
|
|
|
surface_attribs[nsurface_attribs++] = 8;
|
|
|
|
break;
|
|
|
|
|
2023-08-13 03:42:02 +00:00
|
|
|
case GPUTexture::Format::RGB565:
|
2020-05-07 12:48:13 +00:00
|
|
|
surface_attribs[nsurface_attribs++] = EGL_RED_SIZE;
|
|
|
|
surface_attribs[nsurface_attribs++] = 5;
|
|
|
|
surface_attribs[nsurface_attribs++] = EGL_GREEN_SIZE;
|
|
|
|
surface_attribs[nsurface_attribs++] = 6;
|
|
|
|
surface_attribs[nsurface_attribs++] = EGL_BLUE_SIZE;
|
|
|
|
surface_attribs[nsurface_attribs++] = 5;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2024-10-12 12:18:48 +00:00
|
|
|
Error::SetStringFmt(error, "Unsupported texture format {}", GPUTexture::GetFormatName(surface_format));
|
2020-05-07 12:48:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
surface_attribs[nsurface_attribs++] = EGL_NONE;
|
|
|
|
surface_attribs[nsurface_attribs++] = 0;
|
|
|
|
|
|
|
|
EGLint num_configs;
|
2021-01-30 16:25:05 +00:00
|
|
|
if (!eglChooseConfig(m_display, surface_attribs, nullptr, 0, &num_configs) || num_configs == 0)
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
Error::SetStringFmt(error, "eglChooseConfig() failed: 0x{:x}", static_cast<unsigned>(eglGetError()));
|
2020-05-07 12:48:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-30 16:25:05 +00:00
|
|
|
std::vector<EGLConfig> configs(static_cast<u32>(num_configs));
|
|
|
|
if (!eglChooseConfig(m_display, surface_attribs, configs.data(), num_configs, &num_configs))
|
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
Error::SetStringFmt(error, "eglChooseConfig() failed: 0x{:x}", static_cast<unsigned>(eglGetError()));
|
2021-01-30 16:25:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
configs.resize(static_cast<u32>(num_configs));
|
|
|
|
|
|
|
|
std::optional<EGLConfig> config;
|
|
|
|
for (EGLConfig check_config : configs)
|
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
if (CheckConfigSurfaceFormat(check_config, surface_format))
|
2021-01-30 16:25:05 +00:00
|
|
|
{
|
|
|
|
config = check_config;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!config.has_value())
|
|
|
|
{
|
2024-05-23 10:55:28 +00:00
|
|
|
WARNING_LOG("No EGL configs matched exactly, using first.");
|
2021-01-30 16:25:05 +00:00
|
|
|
config = configs.front();
|
|
|
|
}
|
|
|
|
|
2020-05-07 12:48:13 +00:00
|
|
|
int attribs[8];
|
|
|
|
int nattribs = 0;
|
|
|
|
if (version.profile != Profile::NoProfile)
|
|
|
|
{
|
|
|
|
attribs[nattribs++] = EGL_CONTEXT_MAJOR_VERSION;
|
|
|
|
attribs[nattribs++] = version.major_version;
|
|
|
|
attribs[nattribs++] = EGL_CONTEXT_MINOR_VERSION;
|
|
|
|
attribs[nattribs++] = version.minor_version;
|
|
|
|
}
|
|
|
|
attribs[nattribs++] = EGL_NONE;
|
|
|
|
attribs[nattribs++] = 0;
|
|
|
|
|
2020-11-26 12:13:09 +00:00
|
|
|
if (!eglBindAPI((version.profile == Profile::ES) ? EGL_OPENGL_ES_API : EGL_OPENGL_API))
|
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
Error::SetStringFmt(error, "eglBindAPI({}) failed",
|
|
|
|
(version.profile == Profile::ES) ? "EGL_OPENGL_ES_API" : "EGL_OPENGL_API");
|
2020-11-26 12:13:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-30 16:25:05 +00:00
|
|
|
m_context = eglCreateContext(m_display, config.value(), share_context, attribs);
|
2020-05-07 12:48:13 +00:00
|
|
|
if (!m_context)
|
2020-11-26 12:13:09 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
Error::SetStringFmt(error, "eglCreateContext() failed: 0x{:x}", static_cast<unsigned>(eglGetError()));
|
2020-05-07 12:48:13 +00:00
|
|
|
return false;
|
2020-11-26 12:13:09 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 10:55:28 +00:00
|
|
|
INFO_LOG("Got version {}.{} ({})", version.major_version, version.minor_version,
|
|
|
|
version.profile == OpenGLContext::Profile::ES ?
|
|
|
|
"ES" :
|
|
|
|
(version.profile == OpenGLContext::Profile::Core ? "Core" : "None"));
|
2024-04-11 10:31:57 +00:00
|
|
|
|
|
|
|
EGLint min_swap_interval, max_swap_interval;
|
|
|
|
m_supports_negative_swap_interval = false;
|
|
|
|
if (eglGetConfigAttrib(m_display, config.value(), EGL_MIN_SWAP_INTERVAL, &min_swap_interval) &&
|
|
|
|
eglGetConfigAttrib(m_display, config.value(), EGL_MAX_SWAP_INTERVAL, &max_swap_interval))
|
|
|
|
{
|
2024-05-23 10:55:28 +00:00
|
|
|
VERBOSE_LOG("EGL_MIN_SWAP_INTERVAL = {}", min_swap_interval);
|
|
|
|
VERBOSE_LOG("EGL_MAX_SWAP_INTERVAL = {}", max_swap_interval);
|
2024-04-11 10:31:57 +00:00
|
|
|
m_supports_negative_swap_interval = (min_swap_interval <= -1);
|
|
|
|
}
|
|
|
|
|
2024-05-23 10:55:28 +00:00
|
|
|
INFO_LOG("Negative swap interval/tear-control is {}supported", m_supports_negative_swap_interval ? "" : "NOT ");
|
2020-05-07 12:48:13 +00:00
|
|
|
|
2021-01-30 16:25:05 +00:00
|
|
|
m_config = config.value();
|
2020-05-07 12:48:13 +00:00
|
|
|
m_version = version;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
bool OpenGLContextEGL::CreateContextAndSurface(WindowInfo& wi, SurfaceHandle* surface, const Version& version,
|
|
|
|
EGLContext share_context, bool make_current, Error* error)
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
if (!CreateContext(wi.IsSurfaceless(), wi.surface_format, version, share_context, error))
|
2020-05-07 12:48:13 +00:00
|
|
|
return false;
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
// create actual surface, need to handle surfaceless here
|
|
|
|
EGLSurface esurface;
|
|
|
|
if (wi.IsSurfaceless())
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
if (!SupportsSurfaceless())
|
|
|
|
{
|
|
|
|
esurface = GetPBufferSurface(error);
|
|
|
|
if (esurface == EGL_NO_SURFACE)
|
|
|
|
{
|
|
|
|
ERROR_LOG("Failed to create pbuffer surface for context");
|
|
|
|
eglDestroyContext(m_display, m_context);
|
|
|
|
m_context = EGL_NO_SURFACE;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
esurface = EGL_NO_SURFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*surface = nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
esurface = CreatePlatformSurface(m_config, wi, error);
|
|
|
|
if (esurface == EGL_NO_SURFACE)
|
|
|
|
{
|
|
|
|
ERROR_LOG("Failed to create surface for context");
|
|
|
|
eglDestroyContext(m_display, m_context);
|
|
|
|
m_context = EGL_NO_SURFACE;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateWindowInfoSize(wi, esurface);
|
|
|
|
*surface = esurface;
|
2020-05-07 12:48:13 +00:00
|
|
|
}
|
|
|
|
|
2024-10-12 12:18:48 +00:00
|
|
|
if (make_current)
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
if (!eglMakeCurrent(m_display, esurface, esurface, m_context))
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
2024-10-12 12:18:48 +00:00
|
|
|
Error::SetStringFmt(error, "eglMakeCurrent() failed: 0x{:X}", eglGetError());
|
|
|
|
if (esurface != EGL_NO_SURFACE && esurface != m_pbuffer_surface)
|
|
|
|
DestroyPlatformSurface(esurface);
|
|
|
|
eglDestroyContext(m_display, m_context);
|
|
|
|
m_context = EGL_NO_CONTEXT;
|
|
|
|
return false;
|
2020-05-07 12:48:13 +00:00
|
|
|
}
|
2024-10-12 12:18:48 +00:00
|
|
|
|
|
|
|
m_current_surface = esurface;
|
2020-05-07 12:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|