2013-07-18 00:26:01 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2014-01-01 00:50:59 +00:00
|
|
|
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
|
2015-01-07 17:06:50 +00:00
|
|
|
* Copyright (C) 2012-2015 - Michael Lelli
|
2013-07-18 00:26:01 +00:00
|
|
|
*
|
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../../driver.h"
|
2015-01-12 20:53:04 +00:00
|
|
|
#include "../video_context_driver.h"
|
2013-07-18 00:26:01 +00:00
|
|
|
#include "../gl_common.h"
|
2015-01-18 19:03:33 +00:00
|
|
|
#include "../video_monitor.h"
|
2013-07-18 00:26:01 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "../../config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2013-08-25 19:39:50 +00:00
|
|
|
#include <emscripten/emscripten.h>
|
2013-07-18 00:26:01 +00:00
|
|
|
#include <EGL/egl.h>
|
|
|
|
#include <EGL/eglext.h>
|
|
|
|
|
|
|
|
static EGLContext g_egl_ctx;
|
|
|
|
static EGLSurface g_egl_surf;
|
|
|
|
static EGLDisplay g_egl_dpy;
|
|
|
|
static EGLConfig g_config;
|
|
|
|
|
|
|
|
static bool g_inited;
|
|
|
|
|
|
|
|
static unsigned g_fb_width;
|
|
|
|
static unsigned g_fb_height;
|
|
|
|
|
2014-10-08 16:09:01 +00:00
|
|
|
static void gfx_ctx_emscripten_swap_interval(void *data, unsigned interval)
|
2013-07-18 00:26:01 +00:00
|
|
|
{
|
2014-06-19 00:31:41 +00:00
|
|
|
(void)data;
|
2014-10-08 16:09:01 +00:00
|
|
|
/* no way to control VSync in WebGL. */
|
2013-07-18 00:26:01 +00:00
|
|
|
(void)interval;
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:09:01 +00:00
|
|
|
static void gfx_ctx_emscripten_check_window(void *data, bool *quit,
|
2013-07-18 00:26:01 +00:00
|
|
|
bool *resize, unsigned *width, unsigned *height, unsigned frame_count)
|
|
|
|
{
|
2015-01-10 16:51:00 +00:00
|
|
|
int iWidth, iHeight, isFullscreen;
|
|
|
|
|
2014-06-19 00:31:41 +00:00
|
|
|
(void)data;
|
2013-07-18 00:26:01 +00:00
|
|
|
(void)frame_count;
|
2013-08-25 19:39:50 +00:00
|
|
|
|
|
|
|
emscripten_get_canvas_size(&iWidth, &iHeight, &isFullscreen);
|
|
|
|
*width = (unsigned) iWidth;
|
|
|
|
*height = (unsigned) iHeight;
|
2014-10-08 16:09:01 +00:00
|
|
|
*resize = false;
|
2013-08-25 19:39:50 +00:00
|
|
|
|
|
|
|
if (*width != g_fb_width || *height != g_fb_height)
|
|
|
|
*resize = true;
|
2013-07-18 00:26:01 +00:00
|
|
|
|
2013-08-25 19:39:50 +00:00
|
|
|
g_fb_width = (unsigned) iWidth;
|
|
|
|
g_fb_height = (unsigned) iHeight;
|
2013-08-18 06:50:10 +00:00
|
|
|
*quit = false;
|
2013-07-18 00:26:01 +00:00
|
|
|
}
|
|
|
|
|
2014-10-08 16:09:01 +00:00
|
|
|
static void gfx_ctx_emscripten_swap_buffers(void *data)
|
2013-07-18 00:26:01 +00:00
|
|
|
{
|
2014-06-19 00:31:41 +00:00
|
|
|
(void)data;
|
2014-10-08 16:09:01 +00:00
|
|
|
// no-op in emscripten, no way to force swap/wait for VSync in browsers
|
2013-08-18 06:50:10 +00:00
|
|
|
//eglSwapBuffers(g_egl_dpy, g_egl_surf);
|
2013-07-18 00:26:01 +00:00
|
|
|
}
|
|
|
|
|
2014-10-08 16:09:01 +00:00
|
|
|
static void gfx_ctx_emscripten_set_resize(void *data,
|
|
|
|
unsigned width, unsigned height)
|
2013-07-18 00:26:01 +00:00
|
|
|
{
|
2014-06-19 00:31:41 +00:00
|
|
|
(void)data;
|
2013-07-18 00:26:01 +00:00
|
|
|
(void)width;
|
|
|
|
(void)height;
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:09:01 +00:00
|
|
|
static void gfx_ctx_emscripten_update_window_title(void *data)
|
2013-07-18 00:26:01 +00:00
|
|
|
{
|
2013-10-07 18:17:07 +00:00
|
|
|
char buf[128], buf_fps[128];
|
2013-10-07 18:22:08 +00:00
|
|
|
|
2015-01-10 16:51:00 +00:00
|
|
|
(void)data;
|
|
|
|
|
2015-01-18 19:03:33 +00:00
|
|
|
video_monitor_get_fps(buf, sizeof(buf),
|
2015-03-07 22:03:56 +00:00
|
|
|
buf_fps, sizeof(buf_fps));
|
2015-01-12 04:59:11 +00:00
|
|
|
if (g_settings.fps_show)
|
2015-03-07 12:01:41 +00:00
|
|
|
msg_queue_push(g_runloop.msg_queue, buf_fps, 1, 1);
|
2013-07-18 00:26:01 +00:00
|
|
|
}
|
|
|
|
|
2014-10-08 16:09:01 +00:00
|
|
|
static void gfx_ctx_emscripten_get_video_size(void *data,
|
|
|
|
unsigned *width, unsigned *height)
|
2013-07-18 00:26:01 +00:00
|
|
|
{
|
2014-06-19 00:31:41 +00:00
|
|
|
(void)data;
|
2013-07-18 00:26:01 +00:00
|
|
|
*width = g_fb_width;
|
|
|
|
*height = g_fb_height;
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:09:01 +00:00
|
|
|
static void gfx_ctx_emscripten_destroy(void *data);
|
2013-07-18 00:26:01 +00:00
|
|
|
|
2014-10-08 16:09:01 +00:00
|
|
|
static bool gfx_ctx_emscripten_init(void *data)
|
2013-07-18 00:26:01 +00:00
|
|
|
{
|
2015-01-10 16:51:00 +00:00
|
|
|
EGLint width, height, num_config;
|
2013-07-18 00:26:01 +00:00
|
|
|
static const EGLint attribute_list[] =
|
|
|
|
{
|
|
|
|
EGL_RED_SIZE, 8,
|
|
|
|
EGL_GREEN_SIZE, 8,
|
|
|
|
EGL_BLUE_SIZE, 8,
|
|
|
|
EGL_ALPHA_SIZE, 8,
|
|
|
|
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
static const EGLint context_attributes[] =
|
|
|
|
{
|
|
|
|
EGL_CONTEXT_CLIENT_VERSION, 2,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
2015-01-10 16:51:00 +00:00
|
|
|
(void)data;
|
|
|
|
|
|
|
|
RARCH_LOG("[EMSCRIPTEN/EGL]: Initializing...\n");
|
|
|
|
|
|
|
|
if (g_inited)
|
|
|
|
{
|
|
|
|
RARCH_LOG("[EMSCRIPTEN/EGL]: Attempted to re-initialize driver.\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-27 13:35:23 +00:00
|
|
|
/* Get an EGL display connection. */
|
2013-07-18 00:26:01 +00:00
|
|
|
g_egl_dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
|
|
|
if (!g_egl_dpy)
|
|
|
|
goto error;
|
|
|
|
|
2014-10-27 13:35:23 +00:00
|
|
|
/* Initialize the EGL display connection. */
|
2013-07-18 00:26:01 +00:00
|
|
|
if (!eglInitialize(g_egl_dpy, NULL, NULL))
|
|
|
|
goto error;
|
|
|
|
|
2014-10-27 13:35:23 +00:00
|
|
|
/* Get an appropriate EGL frame buffer configuration. */
|
2013-07-18 00:26:01 +00:00
|
|
|
if (!eglChooseConfig(g_egl_dpy, attribute_list, &g_config, 1, &num_config))
|
|
|
|
goto error;
|
|
|
|
|
2014-10-27 13:35:23 +00:00
|
|
|
/* Create an EGL rendering context. */
|
2013-07-18 00:26:01 +00:00
|
|
|
g_egl_ctx = eglCreateContext(g_egl_dpy, g_config, EGL_NO_CONTEXT, context_attributes);
|
|
|
|
if (!g_egl_ctx)
|
|
|
|
goto error;
|
|
|
|
|
2014-10-27 13:35:23 +00:00
|
|
|
/* create an EGL window surface. */
|
2013-07-18 00:26:01 +00:00
|
|
|
g_egl_surf = eglCreateWindowSurface(g_egl_dpy, g_config, 0, NULL);
|
|
|
|
if (!g_egl_surf)
|
|
|
|
goto error;
|
|
|
|
|
2014-10-27 13:35:23 +00:00
|
|
|
/* Connect the context to the surface. */
|
2013-07-18 00:26:01 +00:00
|
|
|
if (!eglMakeCurrent(g_egl_dpy, g_egl_surf, g_egl_surf, g_egl_ctx))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
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;
|
2013-07-19 03:58:35 +00:00
|
|
|
RARCH_LOG("[EMSCRIPTEN/EGL]: Dimensions: %ux%u\n", width, height);
|
2013-07-18 00:26:01 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
error:
|
2014-10-08 16:09:01 +00:00
|
|
|
gfx_ctx_emscripten_destroy(data);
|
2013-07-18 00:26:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:09:01 +00:00
|
|
|
static bool gfx_ctx_emscripten_set_video_mode(void *data,
|
2013-07-18 00:26:01 +00:00
|
|
|
unsigned width, unsigned height,
|
|
|
|
bool fullscreen)
|
|
|
|
{
|
2014-06-19 00:31:41 +00:00
|
|
|
(void)data;
|
2015-01-10 16:51:00 +00:00
|
|
|
|
2013-07-18 00:26:01 +00:00
|
|
|
if (g_inited)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
g_inited = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:09:01 +00:00
|
|
|
static bool gfx_ctx_emscripten_bind_api(void *data,
|
|
|
|
enum gfx_ctx_api api, unsigned major, unsigned minor)
|
2013-07-18 00:26:01 +00:00
|
|
|
{
|
2014-06-19 00:31:41 +00:00
|
|
|
(void)data;
|
2013-08-25 19:39:50 +00:00
|
|
|
(void)major;
|
|
|
|
(void)minor;
|
2015-01-10 16:51:00 +00:00
|
|
|
|
2013-07-18 00:26:01 +00:00
|
|
|
switch (api)
|
|
|
|
{
|
|
|
|
case GFX_CTX_OPENGL_ES_API:
|
|
|
|
return eglBindAPI(EGL_OPENGL_ES_API);
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:09:01 +00:00
|
|
|
static void gfx_ctx_emscripten_destroy(void *data)
|
2013-07-18 00:26:01 +00:00
|
|
|
{
|
2014-06-19 00:31:41 +00:00
|
|
|
(void)data;
|
2015-01-10 16:51:00 +00:00
|
|
|
|
2013-07-18 00:26:01 +00:00
|
|
|
if (g_egl_dpy)
|
|
|
|
{
|
2014-10-27 13:35:23 +00:00
|
|
|
eglMakeCurrent(g_egl_dpy, EGL_NO_SURFACE,
|
|
|
|
EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
2013-07-19 03:58:35 +00:00
|
|
|
|
2013-07-18 00:26:01 +00:00
|
|
|
if (g_egl_ctx)
|
|
|
|
eglDestroyContext(g_egl_dpy, g_egl_ctx);
|
|
|
|
|
|
|
|
if (g_egl_surf)
|
|
|
|
eglDestroySurface(g_egl_dpy, g_egl_surf);
|
|
|
|
|
|
|
|
eglTerminate(g_egl_dpy);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_egl_ctx = NULL;
|
|
|
|
g_egl_surf = NULL;
|
|
|
|
g_egl_dpy = NULL;
|
|
|
|
g_config = 0;
|
|
|
|
g_inited = false;
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:09:01 +00:00
|
|
|
static void gfx_ctx_emscripten_input_driver(void *data,
|
|
|
|
const input_driver_t **input, void **input_data)
|
2013-07-18 00:26:01 +00:00
|
|
|
{
|
2015-01-10 16:51:00 +00:00
|
|
|
void *rwebinput = NULL;
|
|
|
|
|
2014-06-19 00:31:41 +00:00
|
|
|
(void)data;
|
2015-01-10 16:51:00 +00:00
|
|
|
|
2013-07-18 00:26:01 +00:00
|
|
|
*input = NULL;
|
|
|
|
|
2015-01-10 16:51:00 +00:00
|
|
|
rwebinput = input_rwebinput.init();
|
2013-07-18 00:26:01 +00:00
|
|
|
|
2015-01-10 16:51:00 +00:00
|
|
|
if (!rwebinput)
|
|
|
|
return;
|
|
|
|
|
|
|
|
*input = &input_rwebinput;
|
|
|
|
*input_data = rwebinput;
|
2013-07-18 00:26:01 +00:00
|
|
|
}
|
|
|
|
|
2014-10-08 16:09:01 +00:00
|
|
|
static bool gfx_ctx_emscripten_has_focus(void *data)
|
2013-07-18 00:26:01 +00:00
|
|
|
{
|
2014-06-19 00:31:41 +00:00
|
|
|
(void)data;
|
2015-01-10 16:51:00 +00:00
|
|
|
|
2013-07-18 00:26:01 +00:00
|
|
|
return g_inited;
|
|
|
|
}
|
|
|
|
|
2015-01-18 21:32:14 +00:00
|
|
|
static bool gfx_ctx_emscripten_suppress_screensaver(void *data, bool enable)
|
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
(void)enable;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:09:01 +00:00
|
|
|
static bool gfx_ctx_emscripten_has_windowed(void *data)
|
|
|
|
{
|
|
|
|
(void)data;
|
2015-01-10 16:51:00 +00:00
|
|
|
|
2014-10-08 16:09:01 +00:00
|
|
|
/* TODO -verify. */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gfx_ctx_proc_t gfx_ctx_emscripten_get_proc_address(const char *symbol)
|
2013-07-18 00:26:01 +00:00
|
|
|
{
|
2013-08-18 06:50:10 +00:00
|
|
|
return eglGetProcAddress(symbol);
|
2013-07-18 00:26:01 +00:00
|
|
|
}
|
|
|
|
|
2014-10-08 16:09:01 +00:00
|
|
|
static float gfx_ctx_emscripten_translate_aspect(void *data,
|
|
|
|
unsigned width, unsigned height)
|
2013-07-18 00:26:01 +00:00
|
|
|
{
|
2014-06-19 00:31:41 +00:00
|
|
|
(void)data;
|
2015-01-10 16:51:00 +00:00
|
|
|
|
2013-07-18 00:26:01 +00:00
|
|
|
return (float)width / height;
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:09:01 +00:00
|
|
|
static bool gfx_ctx_emscripten_init_egl_image_buffer(void *data,
|
|
|
|
const video_info_t *video)
|
2013-07-18 00:26:01 +00:00
|
|
|
{
|
2014-06-19 00:31:41 +00:00
|
|
|
(void)data;
|
2015-01-10 16:51:00 +00:00
|
|
|
|
2013-07-18 00:26:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:09:01 +00:00
|
|
|
static bool gfx_ctx_emscripten_write_egl_image(void *data,
|
|
|
|
const void *frame, unsigned width, unsigned height, unsigned pitch,
|
|
|
|
bool rgb32, unsigned index, void **image_handle)
|
2013-07-18 00:26:01 +00:00
|
|
|
{
|
2014-06-19 00:31:41 +00:00
|
|
|
(void)data;
|
2013-07-18 00:26:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const gfx_ctx_driver_t gfx_ctx_emscripten = {
|
2014-10-08 16:09:01 +00:00
|
|
|
gfx_ctx_emscripten_init,
|
|
|
|
gfx_ctx_emscripten_destroy,
|
|
|
|
gfx_ctx_emscripten_bind_api,
|
|
|
|
gfx_ctx_emscripten_swap_interval,
|
|
|
|
gfx_ctx_emscripten_set_video_mode,
|
|
|
|
gfx_ctx_emscripten_get_video_size,
|
2015-02-24 20:36:23 +00:00
|
|
|
NULL, /* get_video_output_size */
|
|
|
|
NULL, /* get_video_output_prev */
|
|
|
|
NULL, /* get_video_output_next */
|
|
|
|
NULL,
|
2014-10-08 16:09:01 +00:00
|
|
|
gfx_ctx_emscripten_translate_aspect,
|
|
|
|
gfx_ctx_emscripten_update_window_title,
|
|
|
|
gfx_ctx_emscripten_check_window,
|
|
|
|
gfx_ctx_emscripten_set_resize,
|
|
|
|
gfx_ctx_emscripten_has_focus,
|
2015-01-18 21:32:14 +00:00
|
|
|
gfx_ctx_emscripten_suppress_screensaver,
|
2014-10-08 16:09:01 +00:00
|
|
|
gfx_ctx_emscripten_has_windowed,
|
|
|
|
gfx_ctx_emscripten_swap_buffers,
|
|
|
|
gfx_ctx_emscripten_input_driver,
|
|
|
|
gfx_ctx_emscripten_get_proc_address,
|
|
|
|
gfx_ctx_emscripten_init_egl_image_buffer,
|
|
|
|
gfx_ctx_emscripten_write_egl_image,
|
2013-07-18 00:26:01 +00:00
|
|
|
NULL,
|
|
|
|
"emscripten",
|
|
|
|
};
|