RetroArch/gfx/drivers_context/mali_fbdev_ctx.c

361 lines
8.4 KiB
C
Raw Normal View History

2014-06-05 12:28:17 +02:00
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
*
2014-06-05 12:28:17 +02: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 <stdlib.h>
2015-09-16 05:53:34 +02:00
#include <unistd.h>
2014-06-05 12:28:17 +02:00
2015-09-16 05:53:34 +02:00
#include <sys/ioctl.h>
2017-11-13 11:04:09 +01:00
#include <sys/stat.h>
#include <fcntl.h>
2015-09-16 05:53:34 +02:00
2015-01-07 18:06:50 +01:00
/* Includes and defines for framebuffer size retrieval */
#include <linux/fb.h>
#include <linux/vt.h>
2015-09-16 05:53:34 +02:00
#include <compat/strl.h>
2016-09-11 15:10:58 +02:00
#ifdef HAVE_CONFIG_H
#include "../../config.h"
#endif
2016-03-01 07:17:57 +01:00
#ifdef HAVE_EGL
2015-11-19 13:24:51 +01:00
#include "../common/egl_common.h"
2016-03-01 07:17:57 +01:00
#endif
#if defined(HAVE_OPENGL) || defined(HAVE_OPENGLES)
2015-11-17 08:01:33 +01:00
#include "../common/gl_common.h"
2016-03-01 07:17:57 +01:00
#endif
2016-09-11 15:10:58 +02:00
#include "../../frontend/frontend_driver.h"
2019-02-02 22:02:24 +01:00
#include "../../verbosity.h"
#include "../../configuration.h"
2016-09-11 15:10:58 +02:00
2016-03-01 06:49:05 +01:00
typedef struct
{
#ifdef HAVE_EGL
2015-12-08 17:32:08 -03:00
egl_ctx_data_t egl;
2016-03-01 06:49:05 +01:00
#endif
2015-12-08 17:32:08 -03:00
struct {
unsigned short width;
unsigned short height;
} native_window;
2015-12-08 17:32:08 -03:00
bool resize;
unsigned width, height;
float refresh_rate;
2015-12-08 17:32:08 -03:00
} mali_ctx_data_t;
2014-06-05 12:28:17 +02:00
2016-12-04 03:48:05 +01:00
static enum gfx_ctx_api mali_api = GFX_CTX_NONE;
2016-03-01 06:49:05 +01:00
static void gfx_ctx_mali_fbdev_destroy(void *data)
2014-06-05 12:28:17 +02:00
{
int fd;
2015-12-08 17:32:08 -03:00
mali_ctx_data_t *mali = (mali_ctx_data_t*)data;
2015-09-18 03:55:44 +02:00
2015-12-08 17:32:08 -03:00
if (mali)
{
2016-03-01 06:49:05 +01:00
#ifdef HAVE_EGL
egl_destroy(&mali->egl);
#endif
2014-06-05 12:28:17 +02:00
2015-12-08 17:32:08 -03:00
mali->resize = false;
free(mali);
}
2015-01-07 18:06:50 +01:00
/* Clear framebuffer and set cursor on again */
fd = open("/dev/tty", O_RDWR);
ioctl(fd, VT_ACTIVATE, 5);
ioctl(fd, VT_ACTIVATE, 1);
close(fd);
2015-09-18 03:55:44 +02:00
system("setterm -cursor on");
2014-06-05 12:28:17 +02:00
}
static void gfx_ctx_mali_fbdev_get_video_size(void *data,
unsigned *width, unsigned *height)
2014-06-05 12:28:17 +02:00
{
2015-12-08 17:32:08 -03:00
mali_ctx_data_t *mali = (mali_ctx_data_t*)data;
2015-12-08 17:32:08 -03:00
*width = mali->width;
*height = mali->height;
2014-06-05 12:28:17 +02:00
}
static void *gfx_ctx_mali_fbdev_init(video_frame_info_t *video_info,
void *video_driver)
2014-06-05 12:28:17 +02:00
{
2016-03-01 06:49:05 +01:00
#ifdef HAVE_EGL
EGLint n;
EGLint major, minor;
EGLint format;
2014-06-05 12:28:17 +02:00
static const EGLint attribs[] = {
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_NONE
};
2016-03-01 06:49:05 +01:00
#endif
2015-09-09 19:58:05 +02:00
2015-12-08 17:32:08 -03:00
mali_ctx_data_t *mali = (mali_ctx_data_t*)calloc(1, sizeof(*mali));
if (!mali)
return NULL;
2016-03-01 06:49:05 +01:00
#ifdef HAVE_EGL
frontend_driver_install_signal_handler();
2016-03-01 06:49:05 +01:00
#endif
2016-03-01 06:49:05 +01:00
#ifdef HAVE_EGL
if (!egl_init_context(&mali->egl, EGL_NONE, EGL_DEFAULT_DISPLAY,
&major, &minor, &n, attribs, NULL))
2014-06-05 12:28:17 +02:00
{
egl_report_error();
2014-06-05 12:28:17 +02:00
goto error;
}
2016-03-01 06:49:05 +01:00
#endif
2014-06-05 12:28:17 +02:00
2015-12-08 17:32:08 -03:00
return mali;
2014-06-05 12:28:17 +02:00
error:
RARCH_ERR("[Mali fbdev]: EGL error: %d.\n", eglGetError());
gfx_ctx_mali_fbdev_destroy(video_driver);
return NULL;
2014-06-05 12:28:17 +02:00
}
static void gfx_ctx_mali_fbdev_check_window(void *data, bool *quit,
bool *resize, unsigned *width, unsigned *height, bool is_shutdown)
2014-06-05 12:28:17 +02:00
{
unsigned new_width, new_height;
gfx_ctx_mali_fbdev_get_video_size(data, &new_width, &new_height);
2014-06-05 12:28:17 +02:00
if (new_width != *width || new_height != *height)
{
*width = new_width;
*height = new_height;
*resize = true;
}
*quit = (bool)frontend_driver_get_signal_handler_state();
2014-06-05 12:28:17 +02:00
}
static bool gfx_ctx_mali_fbdev_set_video_mode(void *data,
2017-01-18 17:41:27 +01:00
video_frame_info_t *video_info,
2014-06-05 12:28:17 +02:00
unsigned width, unsigned height,
bool fullscreen)
{
struct fb_var_screeninfo vinfo;
static const EGLint attribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2, /* Use version 2, even for GLES3. */
EGL_NONE
};
2015-12-08 17:32:08 -03:00
mali_ctx_data_t *mali = (mali_ctx_data_t*)data;
int fd = open("/dev/fb0", O_RDWR);
if (ioctl(fd, FBIOGET_VSCREENINFO, &vinfo) < 0)
{
2015-11-19 18:36:24 +01:00
RARCH_ERR("Error obtaining framebuffer info.\n");
goto error;
}
2016-03-24 04:09:25 +01:00
close(fd);
fd = -1;
2016-03-01 06:49:05 +01:00
width = vinfo.xres;
height = vinfo.yres;
2014-06-05 12:28:17 +02:00
2016-03-01 06:49:05 +01:00
mali->width = width;
mali->height = height;
2014-06-05 15:05:43 +02:00
2015-12-08 17:32:08 -03:00
mali->native_window.width = vinfo.xres;
mali->native_window.height = vinfo.yres;
2018-04-27 12:42:46 -05:00
mali->refresh_rate = 1000000.0f / vinfo.pixclock * 1000000.0f /
2018-04-27 22:44:53 -04:00
(vinfo.yres + vinfo.upper_margin + vinfo.lower_margin + vinfo.vsync_len) /
(vinfo.xres + vinfo.left_margin + vinfo.right_margin + vinfo.hsync_len);
2016-03-01 06:49:05 +01:00
#ifdef HAVE_EGL
if (!egl_create_context(&mali->egl, attribs))
2014-06-05 12:28:17 +02:00
{
2015-11-19 18:36:24 +01:00
egl_report_error();
2014-06-05 12:28:17 +02:00
goto error;
}
2016-07-08 14:29:16 +02:00
#endif
2014-06-05 12:28:17 +02:00
2016-07-08 14:29:16 +02:00
#ifdef HAVE_EGL
2016-03-01 06:49:05 +01:00
if (!egl_create_surface(&mali->egl, &mali->native_window))
2014-06-05 12:28:17 +02:00
goto error;
2016-03-01 06:49:05 +01:00
#endif
2014-06-05 12:28:17 +02:00
return true;
error:
if (fd >= 0)
close(fd);
2014-06-05 12:28:17 +02:00
RARCH_ERR("[Mali fbdev]: EGL error: %d.\n", eglGetError());
gfx_ctx_mali_fbdev_destroy(data);
2014-06-05 12:28:17 +02:00
return false;
}
static void gfx_ctx_mali_fbdev_input_driver(void *data,
const char *name,
const input_driver_t **input, void **input_data)
2014-06-05 12:28:17 +02:00
{
*input = NULL;
2014-06-05 12:28:17 +02:00
*input_data = NULL;
}
2018-03-01 21:16:54 +01:00
static enum gfx_ctx_api gfx_ctx_mali_fbdev_get_api(void *data)
{
return mali_api;
}
static bool gfx_ctx_mali_fbdev_bind_api(void *data,
enum gfx_ctx_api api, unsigned major, unsigned minor)
2014-06-05 12:28:17 +02:00
{
(void)data;
2016-03-01 06:49:05 +01:00
mali_api = api;
2018-03-01 21:16:54 +01:00
if (api == GFX_CTX_OPENGL_ES_API)
return true;
return false;
2014-06-05 12:28:17 +02:00
}
static bool gfx_ctx_mali_fbdev_has_focus(void *data)
2014-06-05 12:28:17 +02:00
{
(void)data;
return true;
}
2015-01-18 22:32:14 +01:00
static bool gfx_ctx_mali_fbdev_suppress_screensaver(void *data, bool enable)
{
(void)data;
(void)enable;
return false;
}
2018-09-12 00:07:43 +02:00
static void gfx_ctx_mali_fbdev_set_swap_interval(void *data,
int swap_interval)
2016-03-01 06:49:05 +01:00
{
mali_ctx_data_t *mali = (mali_ctx_data_t*)data;
#ifdef HAVE_EGL
egl_set_swap_interval(&mali->egl, swap_interval);
#endif
}
2017-05-19 03:34:53 +02:00
static void gfx_ctx_mali_fbdev_swap_buffers(void *data, void *data2)
2016-03-01 06:49:05 +01:00
{
mali_ctx_data_t *mali = (mali_ctx_data_t*)data;
#ifdef HAVE_EGL
egl_swap_buffers(&mali->egl);
#endif
}
static gfx_ctx_proc_t gfx_ctx_mali_fbdev_get_proc_address(const char *symbol)
{
#ifdef HAVE_EGL
return egl_get_proc_address(symbol);
2016-03-10 01:50:30 +07:00
#endif
2016-03-01 06:49:05 +01:00
}
static void gfx_ctx_mali_fbdev_bind_hw_render(void *data, bool enable)
{
mali_ctx_data_t *mali = (mali_ctx_data_t*)data;
#ifdef HAVE_EGL
egl_bind_hw_render(&mali->egl, enable);
#endif
}
static uint32_t gfx_ctx_mali_fbdev_get_flags(void *data)
2016-05-05 17:35:28 +02:00
{
uint32_t flags = 0;
BIT32_SET(flags, GFX_CTX_FLAGS_SHADERS_GLSL);
2016-05-05 17:35:28 +02:00
return flags;
}
static void gfx_ctx_mali_fbdev_set_flags(void *data, uint32_t flags)
{
(void)data;
}
static float gfx_ctx_mali_fbdev_get_refresh_rate(void *data)
{
mali_ctx_data_t *mali = (mali_ctx_data_t*)data;
return mali->refresh_rate;
}
static void gfx_ctx_mali_fbdev_update_window_title(void *data, void *data2)
{
const settings_t *settings = config_get_ptr();
video_frame_info_t* video_info = (video_frame_info_t*)data2;
if (settings->bools.video_memory_show)
{
uint64_t mem_bytes_used = frontend_driver_get_used_memory();
uint64_t mem_bytes_total = frontend_driver_get_total_memory();
char mem[128];
mem[0] = '\0';
snprintf(
mem, sizeof(mem), " || MEM: %.2f/%.2fMB", mem_bytes_used / (1024.0f * 1024.0f),
mem_bytes_total / (1024.0f * 1024.0f));
strlcat(video_info->fps_text, mem, sizeof(video_info->fps_text));
}
}
2014-06-05 12:28:17 +02:00
const gfx_ctx_driver_t gfx_ctx_mali_fbdev = {
gfx_ctx_mali_fbdev_init,
gfx_ctx_mali_fbdev_destroy,
2018-03-01 21:16:54 +01:00
gfx_ctx_mali_fbdev_get_api,
gfx_ctx_mali_fbdev_bind_api,
2016-03-01 06:49:05 +01:00
gfx_ctx_mali_fbdev_set_swap_interval,
gfx_ctx_mali_fbdev_set_video_mode,
gfx_ctx_mali_fbdev_get_video_size,
gfx_ctx_mali_fbdev_get_refresh_rate,
NULL, /* get_video_output_size */
NULL, /* get_video_output_prev */
NULL, /* get_video_output_next */
NULL, /* get_metrics */
NULL,
gfx_ctx_mali_fbdev_update_window_title,
gfx_ctx_mali_fbdev_check_window,
NULL, /* set_resize */
gfx_ctx_mali_fbdev_has_focus,
2015-01-18 22:32:14 +01:00
gfx_ctx_mali_fbdev_suppress_screensaver,
2017-01-19 02:50:56 +01:00
NULL, /* has_windowed */
2016-03-01 06:49:05 +01:00
gfx_ctx_mali_fbdev_swap_buffers,
gfx_ctx_mali_fbdev_input_driver,
2016-03-01 06:49:05 +01:00
gfx_ctx_mali_fbdev_get_proc_address,
2014-06-05 12:28:17 +02:00
NULL,
NULL,
NULL,
"mali-fbdev",
gfx_ctx_mali_fbdev_get_flags,
2016-05-05 17:35:28 +02:00
gfx_ctx_mali_fbdev_set_flags,
2016-08-31 15:02:07 +02:00
gfx_ctx_mali_fbdev_bind_hw_render,
NULL,
NULL
2014-06-05 12:28:17 +02:00
};