#include "../../SDL_internal.h"
#include "SDL_qnx.h"
static EGLDisplay egl_disp;
static int chooseFormat(EGLConfig egl_conf)
{
EGLint buffer_bit_depth;
EGLint alpha_bit_depth;
eglGetConfigAttrib(egl_disp, egl_conf, EGL_BUFFER_SIZE, &buffer_bit_depth);
eglGetConfigAttrib(egl_disp, egl_conf, EGL_ALPHA_SIZE, &alpha_bit_depth);
switch (buffer_bit_depth) {
case 32:
return SCREEN_FORMAT_RGBX8888;
case 24:
return SDL_PIXELFORMAT_RGB24;
case 16:
switch (alpha_bit_depth) {
case 4:
return SCREEN_FORMAT_RGBX4444;
case 1:
return SCREEN_FORMAT_RGBA5551;
default:
return SCREEN_FORMAT_RGB565;
}
default:
return 0;
}
}
bool glGetConfig(EGLConfig *pconf, int *pformat)
{
EGLConfig egl_conf = (EGLConfig)0;
EGLConfig *egl_configs;
EGLint egl_num_configs;
EGLint val;
EGLBoolean rc;
EGLint i;
rc = eglGetConfigs(egl_disp, NULL, 0, &egl_num_configs);
if (rc != EGL_TRUE) {
return false;
}
if (egl_num_configs == 0) {
return false;
}
egl_configs = SDL_malloc(egl_num_configs * sizeof(*egl_configs));
if (!egl_configs) {
return false;
}
rc = eglGetConfigs(egl_disp, egl_configs, egl_num_configs,
&egl_num_configs);
if (rc != EGL_TRUE) {
SDL_free(egl_configs);
return false;
}
for (i = 0; i < egl_num_configs; i++) {
eglGetConfigAttrib(egl_disp, egl_configs[i], EGL_SURFACE_TYPE, &val);
if (!(val & EGL_WINDOW_BIT)) {
continue;
}
eglGetConfigAttrib(egl_disp, egl_configs[i], EGL_RENDERABLE_TYPE, &val);
if (!(val & EGL_OPENGL_ES2_BIT)) {
continue;
}
eglGetConfigAttrib(egl_disp, egl_configs[i], EGL_DEPTH_SIZE, &val);
if (val == 0) {
continue;
}
egl_conf = egl_configs[i];
break;
}
SDL_free(egl_configs);
*pconf = egl_conf;
*pformat = chooseFormat(egl_conf);
return true;
}
bool glLoadLibrary(SDL_VideoDevice *_this, const char *name)
{
EGLNativeDisplayType disp_id = EGL_DEFAULT_DISPLAY;
egl_disp = eglGetDisplay(disp_id);
if (egl_disp == EGL_NO_DISPLAY) {
return false;
}
if (eglInitialize(egl_disp, NULL, NULL) == EGL_FALSE) {
return false;
}
return true;
}
SDL_FunctionPointer glGetProcAddress(SDL_VideoDevice *_this, const char *proc)
{
return eglGetProcAddress(proc);
}
SDL_GLContext glCreateContext(SDL_VideoDevice *_this, SDL_Window *window)
{
window_impl_t *impl = (window_impl_t *)window->internal;
EGLContext context;
EGLSurface surface;
struct {
EGLint client_version[2];
EGLint none;
} egl_ctx_attr = {
.client_version = { EGL_CONTEXT_CLIENT_VERSION, 2 },
.none = EGL_NONE
};
struct {
EGLint render_buffer[2];
EGLint none;
} egl_surf_attr = {
.render_buffer = { EGL_RENDER_BUFFER, EGL_BACK_BUFFER },
.none = EGL_NONE
};
context = eglCreateContext(egl_disp, impl->conf, EGL_NO_CONTEXT,
(EGLint *)&egl_ctx_attr);
if (context == EGL_NO_CONTEXT) {
return NULL;
}
surface = eglCreateWindowSurface(egl_disp, impl->conf,
(EGLNativeWindowType)impl->window,
(EGLint *)&egl_surf_attr);
if (surface == EGL_NO_SURFACE) {
return NULL;
}
eglMakeCurrent(egl_disp, surface, surface, context);
impl->surface = surface;
return context;
}
bool glSetSwapInterval(SDL_VideoDevice *_this, int interval)
{
if (eglSwapInterval(egl_disp, interval) != EGL_TRUE) {
return false;
}
return true;
}
bool glSwapWindow(SDL_VideoDevice *_this, SDL_Window *window)
{
window_impl_t *impl = (window_impl_t *)window->internal;
return eglSwapBuffers(egl_disp, impl->surface) == EGL_TRUE ? 0 : -1;
}
bool glMakeCurrent(SDL_VideoDevice *_this, SDL_Window *window, SDL_GLContext context)
{
window_impl_t *impl;
EGLSurface surface = NULL;
if (window) {
impl = (window_impl_t *)window->internal;
surface = impl->surface;
}
if (eglMakeCurrent(egl_disp, surface, surface, context) != EGL_TRUE) {
return false;
}
return true;
}
bool glDeleteContext(SDL_VideoDevice *_this, SDL_GLContext context)
{
eglDestroyContext(egl_disp, context);
return true;
}
void glUnloadLibrary(SDL_VideoDevice *_this)
{
eglTerminate(egl_disp);
}