client egl context and viewport in kotlin UI
This commit is contained in:
@@ -2,8 +2,10 @@
|
||||
#include "logger.h"
|
||||
#include <glad/gles2.h>
|
||||
#include <glad/egl.h>
|
||||
#include <android/native_window_jni.h>
|
||||
#include <vector>
|
||||
|
||||
bool egl::Context::create()
|
||||
bool egl::Context::create(ANativeWindow* window)
|
||||
{
|
||||
int version = gladLoaderLoadEGL(EGL_NO_DISPLAY);
|
||||
if (version == 0)
|
||||
@@ -12,7 +14,7 @@ bool egl::Context::create()
|
||||
return false;
|
||||
}
|
||||
if (m_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
||||
m_display == EGL_NO_DISPLAY)
|
||||
m_display == EGL_NO_DISPLAY)
|
||||
{
|
||||
Logger::Log("eglGetDisplay failed");
|
||||
return false;
|
||||
@@ -24,14 +26,22 @@ bool egl::Context::create()
|
||||
return false;
|
||||
}
|
||||
version = gladLoaderLoadEGL(EGL_DEFAULT_DISPLAY);
|
||||
const EGLint config_attribs[] =
|
||||
std::vector<EGLint> config_attribs =
|
||||
{
|
||||
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
|
||||
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT,
|
||||
EGL_NONE,
|
||||
};
|
||||
if (window)
|
||||
{
|
||||
config_attribs.append_range(std::to_array({
|
||||
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
|
||||
EGL_BLUE_SIZE, 8,
|
||||
EGL_GREEN_SIZE, 8,
|
||||
EGL_RED_SIZE, 8,
|
||||
}));
|
||||
}
|
||||
config_attribs.push_back(EGL_NONE);
|
||||
EGLint num_configs;
|
||||
if (!eglChooseConfig(m_display, config_attribs, &m_config, 1, &num_configs) || num_configs == 0)
|
||||
if (!eglChooseConfig(m_display, config_attribs.data(), &m_config, 1, &num_configs) || num_configs == 0)
|
||||
{
|
||||
Logger::Log("eglChooseConfig failed");
|
||||
return false;
|
||||
@@ -48,11 +58,22 @@ bool egl::Context::create()
|
||||
Logger::Log("eglCreateContext failed");
|
||||
return false;
|
||||
}
|
||||
if (!eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, m_context))
|
||||
if (window)
|
||||
{
|
||||
m_surface = eglCreateWindowSurface(m_display, m_config, window, nullptr);
|
||||
m_native_window = window;
|
||||
}
|
||||
if (!eglMakeCurrent(m_display, m_surface, m_surface, m_context))
|
||||
{
|
||||
Logger::Log("eglMakeCurrent failed");
|
||||
return false;
|
||||
}
|
||||
int gl_version = gladLoaderLoadGLES2();
|
||||
if (gl_version == 0)
|
||||
{
|
||||
Logger::Log("glad failed to load GLES2");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -61,18 +82,36 @@ void egl::Context::destroy()
|
||||
if (m_display != EGL_NO_DISPLAY)
|
||||
{
|
||||
eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
||||
if (m_surface != EGL_NO_SURFACE)
|
||||
{
|
||||
eglDestroySurface(m_display, m_surface);
|
||||
}
|
||||
if (m_context != EGL_NO_CONTEXT)
|
||||
{
|
||||
eglDestroyContext(m_display, m_context);
|
||||
}
|
||||
if (m_native_window)
|
||||
{
|
||||
ANativeWindow_release(m_native_window);
|
||||
}
|
||||
eglTerminate(m_display);
|
||||
}
|
||||
m_display = EGL_NO_DISPLAY;
|
||||
m_context = EGL_NO_CONTEXT;
|
||||
m_config = EGL_NO_CONFIG_KHR;
|
||||
m_surface = EGL_NO_SURFACE;
|
||||
m_native_window = nullptr;
|
||||
}
|
||||
|
||||
egl::Context::Context()
|
||||
: m_display(EGL_NO_DISPLAY), m_config(EGL_NO_CONFIG_KHR), m_context(EGL_NO_CONTEXT)
|
||||
egl::Context::Context() :
|
||||
m_display(EGL_NO_DISPLAY), m_config(EGL_NO_CONFIG_KHR),
|
||||
m_context(EGL_NO_CONTEXT), m_surface(EGL_NO_SURFACE),
|
||||
m_native_window(nullptr)
|
||||
{
|
||||
Logger::Log("egl::Context::Context");
|
||||
}
|
||||
|
||||
void egl::Context::swap()
|
||||
{
|
||||
eglSwapBuffers(m_display, m_surface);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user