2024-01-20 13:21:35 +00:00
|
|
|
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
|
2022-12-04 11:03:45 +00:00
|
|
|
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
|
|
|
|
2020-05-07 12:48:13 +00:00
|
|
|
#include "context_egl.h"
|
2023-08-13 03:42:02 +00:00
|
|
|
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/log.h"
|
2024-01-20 13:21:35 +00:00
|
|
|
#include "common/error.h"
|
2023-08-13 03:42:02 +00:00
|
|
|
|
2021-01-30 16:25:05 +00:00
|
|
|
#include <optional>
|
|
|
|
#include <vector>
|
2023-08-13 03:42:02 +00:00
|
|
|
|
2023-09-30 04:40:50 +00:00
|
|
|
Log_SetChannel(GL::Context);
|
2020-05-07 12:48:13 +00:00
|
|
|
|
|
|
|
namespace GL {
|
2023-08-13 03:42:02 +00:00
|
|
|
ContextEGL::ContextEGL(const WindowInfo& wi) : Context(wi)
|
|
|
|
{
|
|
|
|
}
|
2020-05-07 12:48:13 +00:00
|
|
|
|
|
|
|
ContextEGL::~ContextEGL()
|
|
|
|
{
|
2021-01-31 15:27:14 +00:00
|
|
|
DestroySurface();
|
|
|
|
DestroyContext();
|
2020-05-07 12:48:13 +00:00
|
|
|
}
|
|
|
|
|
2024-01-20 13:21:35 +00:00
|
|
|
std::unique_ptr<Context> ContextEGL::Create(const WindowInfo& wi, std::span<const Version> versions_to_try, Error* error)
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
|
|
|
std::unique_ptr<ContextEGL> context = std::make_unique<ContextEGL>(wi);
|
2024-01-20 13:21:35 +00:00
|
|
|
if (!context->Initialize(versions_to_try, error))
|
2020-05-07 12:48:13 +00:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2024-01-20 13:21:35 +00:00
|
|
|
bool ContextEGL::Initialize(std::span<const Version> versions_to_try, Error* error)
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
|
|
|
if (!gladLoadEGL())
|
|
|
|
{
|
2024-01-20 13:21:35 +00:00
|
|
|
Log_ErrorPrint("Loading GLAD EGL functions failed");
|
|
|
|
Error::SetStringView(error, "Loading GLAD EGL functions failed");
|
2020-05-07 12:48:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-30 16:25:05 +00:00
|
|
|
if (!SetDisplay())
|
2020-05-07 12:48:13 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
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());
|
|
|
|
Log_ErrorFmt("eglInitialize() failed: {} (0x{:X})", gerror, gerror);
|
|
|
|
Error::SetStringFmt(error, "eglInitialize() failed: {} (0x{:X})", gerror, gerror);
|
2020-05-07 12:48:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Log_InfoPrintf("EGL Version: %d.%d", egl_major, egl_minor);
|
|
|
|
|
2020-11-07 11:41:25 +00:00
|
|
|
const char* extensions = eglQueryString(m_display, EGL_EXTENSIONS);
|
|
|
|
if (extensions)
|
|
|
|
{
|
|
|
|
Log_InfoPrintf("EGL Extensions: %s", extensions);
|
|
|
|
m_supports_surfaceless = std::strstr(extensions, "EGL_KHR_surfaceless_context") != nullptr;
|
|
|
|
}
|
|
|
|
if (!m_supports_surfaceless)
|
|
|
|
Log_WarningPrint("EGL implementation does not support surfaceless contexts, emulating with pbuffers");
|
|
|
|
|
2024-01-20 13:21:35 +00:00
|
|
|
for (const Version& cv : versions_to_try)
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
2024-01-20 13:21:35 +00:00
|
|
|
if (CreateContextAndSurface(cv, nullptr, true))
|
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;
|
|
|
|
}
|
|
|
|
|
2021-01-30 16:25:05 +00:00
|
|
|
bool ContextEGL::SetDisplay()
|
|
|
|
{
|
|
|
|
m_display = eglGetDisplay(static_cast<EGLNativeDisplayType>(m_wi.display_connection));
|
|
|
|
if (!m_display)
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("eglGetDisplay() failed: %d", eglGetError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-07 12:48:13 +00:00
|
|
|
void* ContextEGL::GetProcAddress(const char* name)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<void*>(eglGetProcAddress(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContextEGL::ChangeSurface(const WindowInfo& new_wi)
|
|
|
|
{
|
|
|
|
const bool was_current = (eglGetCurrentContext() == m_context);
|
|
|
|
if (was_current)
|
|
|
|
eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
|
|
|
|
|
|
|
if (m_surface != EGL_NO_SURFACE)
|
|
|
|
{
|
|
|
|
eglDestroySurface(m_display, m_surface);
|
|
|
|
m_surface = EGL_NO_SURFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_wi = new_wi;
|
2020-11-07 11:41:25 +00:00
|
|
|
if (!CreateSurface())
|
2020-05-07 12:48:13 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (was_current && !eglMakeCurrent(m_display, m_surface, m_surface, m_context))
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Failed to make context current again after surface change");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContextEGL::ResizeSurface(u32 new_surface_width /*= 0*/, u32 new_surface_height /*= 0*/)
|
|
|
|
{
|
2021-03-27 06:14:33 +00:00
|
|
|
if (new_surface_width == 0 && new_surface_height == 0)
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
2021-03-27 06:14:33 +00:00
|
|
|
EGLint surface_width, surface_height;
|
|
|
|
if (eglQuerySurface(m_display, m_surface, EGL_WIDTH, &surface_width) &&
|
|
|
|
eglQuerySurface(m_display, m_surface, EGL_HEIGHT, &surface_height))
|
|
|
|
{
|
|
|
|
m_wi.surface_width = static_cast<u32>(surface_width);
|
|
|
|
m_wi.surface_height = static_cast<u32>(surface_height);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("eglQuerySurface() failed: %d", eglGetError());
|
|
|
|
}
|
2020-05-07 12:48:13 +00:00
|
|
|
}
|
2020-09-19 03:04:32 +00:00
|
|
|
|
|
|
|
m_wi.surface_width = new_surface_width;
|
|
|
|
m_wi.surface_height = new_surface_height;
|
2020-05-07 12:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ContextEGL::SwapBuffers()
|
|
|
|
{
|
|
|
|
return eglSwapBuffers(m_display, m_surface);
|
|
|
|
}
|
|
|
|
|
2022-12-08 03:17:20 +00:00
|
|
|
bool ContextEGL::IsCurrent()
|
|
|
|
{
|
|
|
|
return m_context && eglGetCurrentContext() == m_context;
|
|
|
|
}
|
|
|
|
|
2020-05-07 12:48:13 +00:00
|
|
|
bool ContextEGL::MakeCurrent()
|
|
|
|
{
|
|
|
|
if (!eglMakeCurrent(m_display, m_surface, m_surface, m_context))
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("eglMakeCurrent() failed: %d", eglGetError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContextEGL::DoneCurrent()
|
|
|
|
{
|
|
|
|
return eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContextEGL::SetSwapInterval(s32 interval)
|
|
|
|
{
|
|
|
|
return eglSwapInterval(m_display, interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Context> ContextEGL::CreateSharedContext(const WindowInfo& wi)
|
|
|
|
{
|
|
|
|
std::unique_ptr<ContextEGL> context = std::make_unique<ContextEGL>(wi);
|
|
|
|
context->m_display = m_display;
|
2020-11-07 11:41:25 +00:00
|
|
|
context->m_supports_surfaceless = m_supports_surfaceless;
|
2020-05-07 12:48:13 +00:00
|
|
|
|
|
|
|
if (!context->CreateContextAndSurface(m_version, m_context, false))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
EGLNativeWindowType ContextEGL::GetNativeWindow(EGLConfig config)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContextEGL::CreateSurface()
|
|
|
|
{
|
2020-11-07 11:41:25 +00:00
|
|
|
if (m_wi.type == WindowInfo::Type::Surfaceless)
|
|
|
|
{
|
|
|
|
if (m_supports_surfaceless)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return CreatePBufferSurface();
|
|
|
|
}
|
|
|
|
|
2020-05-07 12:48:13 +00:00
|
|
|
EGLNativeWindowType native_window = GetNativeWindow(m_config);
|
|
|
|
m_surface = eglCreateWindowSurface(m_display, m_config, native_window, nullptr);
|
|
|
|
if (!m_surface)
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("eglCreateWindowSurface() failed: %d", eglGetError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some implementations may require the size to be queried at runtime.
|
|
|
|
EGLint surface_width, surface_height;
|
|
|
|
if (eglQuerySurface(m_display, m_surface, EGL_WIDTH, &surface_width) &&
|
|
|
|
eglQuerySurface(m_display, m_surface, EGL_HEIGHT, &surface_height))
|
|
|
|
{
|
|
|
|
m_wi.surface_width = static_cast<u32>(surface_width);
|
|
|
|
m_wi.surface_height = static_cast<u32>(surface_height);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("eglQuerySurface() failed: %d", eglGetError());
|
|
|
|
}
|
|
|
|
|
2023-10-31 15:52:28 +00:00
|
|
|
m_wi.surface_format = GetSurfaceTextureFormat();
|
|
|
|
|
2020-05-07 12:48:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-07 11:41:25 +00:00
|
|
|
bool ContextEGL::CreatePBufferSurface()
|
|
|
|
{
|
|
|
|
const u32 width = std::max<u32>(m_wi.surface_width, 1);
|
|
|
|
const u32 height = std::max<u32>(m_wi.surface_height, 1);
|
|
|
|
|
|
|
|
// TODO: Format
|
|
|
|
EGLint attrib_list[] = {
|
|
|
|
EGL_WIDTH, static_cast<EGLint>(width), EGL_HEIGHT, static_cast<EGLint>(height), EGL_NONE,
|
|
|
|
};
|
|
|
|
|
|
|
|
m_surface = eglCreatePbufferSurface(m_display, m_config, attrib_list);
|
|
|
|
if (!m_surface)
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("eglCreatePbufferSurface() failed: %d", eglGetError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-31 15:52:28 +00:00
|
|
|
m_wi.surface_format = GetSurfaceTextureFormat();
|
|
|
|
|
2020-11-07 11:41:25 +00:00
|
|
|
Log_DevPrintf("Created %ux%u pbuffer surface", width, height);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-13 03:42:02 +00:00
|
|
|
bool ContextEGL::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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-31 15:52:28 +00:00
|
|
|
GPUTexture::Format ContextEGL::GetSurfaceTextureFormat() const
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (red_size == 5 && green_size == 6 && red_size == 5)
|
|
|
|
{
|
|
|
|
return GPUTexture::Format::RGB565;
|
|
|
|
}
|
|
|
|
else if (red_size == 5 && green_size == 5 && red_size == 5 && alpha_size == 1)
|
|
|
|
{
|
|
|
|
return GPUTexture::Format::RGBA5551;
|
|
|
|
}
|
|
|
|
else if (red_size == 8 && green_size == 8 && blue_size == 8 && alpha_size == 8)
|
|
|
|
{
|
|
|
|
return GPUTexture::Format::RGBA8;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Unknown surface format: R=%u, G=%u, B=%u, A=%u", red_size, green_size, blue_size, alpha_size);
|
|
|
|
return GPUTexture::Format::RGBA8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-31 15:27:14 +00:00
|
|
|
void ContextEGL::DestroyContext()
|
|
|
|
{
|
|
|
|
if (eglGetCurrentContext() == m_context)
|
|
|
|
eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
|
|
|
|
|
|
|
if (m_context != EGL_NO_CONTEXT)
|
|
|
|
{
|
|
|
|
eglDestroyContext(m_display, m_context);
|
|
|
|
m_context = EGL_NO_CONTEXT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContextEGL::DestroySurface()
|
|
|
|
{
|
|
|
|
if (eglGetCurrentSurface(EGL_DRAW) == m_surface)
|
|
|
|
eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
|
|
|
|
|
|
|
if (m_surface != EGL_NO_SURFACE)
|
|
|
|
{
|
|
|
|
eglDestroySurface(m_display, m_surface);
|
|
|
|
m_surface = EGL_NO_SURFACE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 12:48:13 +00:00
|
|
|
bool ContextEGL::CreateContext(const Version& version, EGLContext share_context)
|
|
|
|
{
|
2020-11-26 12:13:09 +00:00
|
|
|
Log_DevPrintf(
|
|
|
|
"Trying version %u.%u (%s)", version.major_version, version.minor_version,
|
|
|
|
version.profile == Context::Profile::ES ? "ES" : (version.profile == Context::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,
|
|
|
|
(m_wi.type != WindowInfo::Type::Surfaceless) ? EGL_WINDOW_BIT : 0,
|
|
|
|
};
|
|
|
|
int nsurface_attribs = 4;
|
|
|
|
|
2023-08-13 03:42:02 +00:00
|
|
|
const GPUTexture::Format format = m_wi.surface_format;
|
|
|
|
if (format == GPUTexture::Format::Unknown)
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
2023-08-13 03:42:02 +00:00
|
|
|
Log_WarningPrint("Surface format not specified, assuming RGBA8.");
|
|
|
|
m_wi.surface_format = GPUTexture::Format::RGBA8;
|
|
|
|
}
|
2020-05-07 12:48:13 +00:00
|
|
|
|
2023-08-13 03:42:02 +00:00
|
|
|
switch (m_wi.surface_format)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2023-08-13 03:42:02 +00:00
|
|
|
case GPUTexture::Format::Unknown:
|
2021-01-30 16:25:05 +00:00
|
|
|
break;
|
|
|
|
|
2020-05-07 12:48:13 +00:00
|
|
|
default:
|
|
|
|
UnreachableCode();
|
|
|
|
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
|
|
|
{
|
|
|
|
Log_ErrorPrintf("eglChooseConfig() failed: %d", eglGetError());
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("eglChooseConfig() failed: %d", eglGetError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
configs.resize(static_cast<u32>(num_configs));
|
|
|
|
|
|
|
|
std::optional<EGLConfig> config;
|
|
|
|
for (EGLConfig check_config : configs)
|
|
|
|
{
|
|
|
|
if (CheckConfigSurfaceFormat(check_config, m_wi.surface_format))
|
|
|
|
{
|
|
|
|
config = check_config;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!config.has_value())
|
|
|
|
{
|
|
|
|
Log_WarningPrintf("No EGL configs matched exactly, using first.");
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("eglBindAPI(%s) failed", (version.profile == Profile::ES) ? "EGL_OPENGL_ES_API" : "EGL_OPENGL_API");
|
|
|
|
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
|
|
|
{
|
|
|
|
Log_ErrorPrintf("eglCreateContext() failed: %d", eglGetError());
|
2020-05-07 12:48:13 +00:00
|
|
|
return false;
|
2020-11-26 12:13:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Log_InfoPrintf(
|
|
|
|
"Got version %u.%u (%s)", version.major_version, version.minor_version,
|
|
|
|
version.profile == Context::Profile::ES ? "ES" : (version.profile == Context::Profile::Core ? "Core" : "None"));
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContextEGL::CreateContextAndSurface(const Version& version, EGLContext share_context, bool make_current)
|
|
|
|
{
|
|
|
|
if (!CreateContext(version, share_context))
|
|
|
|
return false;
|
|
|
|
|
2020-11-07 11:41:25 +00:00
|
|
|
if (!CreateSurface())
|
2020-05-07 12:48:13 +00:00
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Failed to create surface for context");
|
|
|
|
eglDestroyContext(m_display, m_context);
|
|
|
|
m_context = EGL_NO_CONTEXT;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (make_current && !eglMakeCurrent(m_display, m_surface, m_surface, m_context))
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("eglMakeCurrent() failed: %d", eglGetError());
|
|
|
|
if (m_surface != EGL_NO_SURFACE)
|
|
|
|
{
|
|
|
|
eglDestroySurface(m_display, m_surface);
|
|
|
|
m_surface = EGL_NO_SURFACE;
|
|
|
|
}
|
|
|
|
eglDestroyContext(m_display, m_context);
|
|
|
|
m_context = EGL_NO_CONTEXT;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} // namespace GL
|