(Emscripten context) Cleanups

This commit is contained in:
twinaphex 2016-03-01 07:07:56 +01:00
parent 32c4f13761
commit 47fddf28e5

View File

@ -22,15 +22,27 @@
#include "../../driver.h"
#include "../../runloop.h"
#include "../video_context_driver.h"
#ifdef HAVE_EGL
#include "../common/egl_common.h"
#endif
#if defined(HAVE_OPENGL) || defined(HAVE_OPENGLES)
#include "../common/gl_common.h"
#endif
#ifdef HAVE_CONFIG_H
#include "../../config.h"
#endif
static unsigned g_fb_width;
static unsigned g_fb_height;
typedef struct
{
#ifdef HAVE_EGL
egl_ctx_data_t egl;
#endif
unsigned fb_width;
unsigned fb_height;
} emscripten_ctx_data_t;
static void gfx_ctx_emscripten_swap_interval(void *data, unsigned interval)
{
@ -42,31 +54,31 @@ static void gfx_ctx_emscripten_swap_interval(void *data, unsigned interval)
static void gfx_ctx_emscripten_check_window(void *data, bool *quit,
bool *resize, unsigned *width, unsigned *height, unsigned frame_count)
{
int iWidth, iHeight, isFullscreen;
int input_width;
int input_height;
int is_fullscreen;
emscripten_ctx_data_t *emscripten = (emscripten_ctx_data_t*)data;
(void)data;
(void)frame_count;
emscripten_get_canvas_size(&iWidth, &iHeight, &isFullscreen);
*width = (unsigned) iWidth;
*height = (unsigned) iHeight;
*resize = false;
emscripten_get_canvas_size(&input_width, &input_height, &is_fullscreen);
*width = (unsigned)input_width;
*height = (unsigned)input_height;
*resize = false;
if (*width != g_fb_width || *height != g_fb_height)
*resize = true;
if (*width != emscripten->fb_width || *height != emscripten->fb_height)
*resize = true;
g_fb_width = (unsigned) iWidth;
g_fb_height = (unsigned) iHeight;
*quit = false;
emscripten->fb_width = (unsigned)input_width;
emscripten->fb_height = (unsigned)input_height;
*quit = false;
}
static void gfx_ctx_emscripten_swap_buffers(void *data)
{
(void)data;
/* no-op in emscripten, no way to force swap/wait for VSync in browsers */
#if 0
egl_swap_buffers(data);
#endif
}
static bool gfx_ctx_emscripten_set_resize(void *data,
@ -95,16 +107,27 @@ static void gfx_ctx_emscripten_update_window_title(void *data)
static void gfx_ctx_emscripten_get_video_size(void *data,
unsigned *width, unsigned *height)
{
(void)data;
*width = g_fb_width;
*height = g_fb_height;
emscripten_ctx_data_t *emscripten = (emscripten_ctx_data_t*)data;
*width = emscripten->fb_width;
*height = emscripten->fb_height;
}
static void gfx_ctx_emscripten_destroy(void *data);
static void gfx_ctx_emscripten_destroy(void *data)
{
emscripten_ctx_data_t *emscripten = (emscripten_ctx_data_t*)data;
egl_destroy(&emscripten->egl);
free(data);
}
static void *gfx_ctx_emscripten_init(void *video_driver)
{
EGLint width, height, n, major, minor;
#ifdef HAVE_EGL
EGLint width, height;
EGLint major, minor;
EGLint n;
static const EGLint attribute_list[] =
{
EGL_RED_SIZE, 8,
@ -119,38 +142,47 @@ static void *gfx_ctx_emscripten_init(void *video_driver)
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
#endif
emscripten_ctx_data_t *emscripten = (emscripten_ctx_data_t*)
calloc(1, sizeof(*emscripten));
if (!emscripten)
return NULL;
(void)video_driver;
#ifdef HAVE_EGL
if (g_egl_inited)
{
RARCH_LOG("[EMSCRIPTEN/EGL]: Attempted to re-initialize driver.\n");
return (void*)"emscripten";
}
if (!egl_init_context(EGL_DEFAULT_DISPLAY, &major, &minor,
&n, attribute_list))
if (!egl_init_context(&emscripten->egl, EGL_DEFAULT_DISPLAY,
&major, &minor, &n, attribute_list))
{
egl_report_error();
goto error;
}
if (!egl_create_context(context_attributes))
if (!egl_create_context(&emscripten->egl, context_attributes))
{
egl_report_error();
goto error;
}
if (!egl_create_surface(0))
if (!egl_create_surface(&emscripten->egl, 0))
goto error;
#endif
eglQuerySurface(g_egl_dpy, g_egl_surf, EGL_WIDTH, &width);
eglQuerySurface(g_egl_dpy, g_egl_surf, EGL_HEIGHT, &height);
g_fb_width = width;
g_fb_height = height;
emscripten->fb_width = width;
emscripten->fb_height = height;
RARCH_LOG("[EMSCRIPTEN/EGL]: Dimensions: %ux%u\n", width, height);
return (void*)"emscripten";
return emscripten;
error:
gfx_ctx_emscripten_destroy(video_driver);
@ -188,10 +220,6 @@ static bool gfx_ctx_emscripten_bind_api(void *data,
return false;
}
static void gfx_ctx_emscripten_destroy(void *data)
{
egl_destroy(data);
}
static void gfx_ctx_emscripten_input_driver(void *data,
const input_driver_t **input, void **input_data)
@ -258,6 +286,24 @@ static bool gfx_ctx_emscripten_write_egl_image(void *data,
return false;
}
static gfx_ctx_proc_t gfx_ctx_emscripten_get_proc_address(const char *symbol)
{
#ifdef HAVE_EGL
return egl_get_proc_address(symbol);
#else
return NULL;
#endif
}
static void gfx_ctx_emscripten_bind_hw_render(void *data, bool enable)
{
emscripten_ctx_data_t *emscripten = (emscripten_ctx_data_t*)data;
#ifdef HAVE_EGL
egl_bind_hw_render(&emscripten->egl, enable);
#endif
}
const gfx_ctx_driver_t gfx_ctx_emscripten = {
gfx_ctx_emscripten_init,
gfx_ctx_emscripten_destroy,
@ -278,10 +324,10 @@ const gfx_ctx_driver_t gfx_ctx_emscripten = {
gfx_ctx_emscripten_has_windowed,
gfx_ctx_emscripten_swap_buffers,
gfx_ctx_emscripten_input_driver,
egl_get_proc_address,
gfx_ctx_emscripten_get_proc_address,
gfx_ctx_emscripten_init_egl_image_buffer,
gfx_ctx_emscripten_write_egl_image,
NULL,
"emscripten",
egl_bind_hw_render,
gfx_ctx_emscripten_bind_hw_render,
};