RetroArch/gfx/drivers_context/drm_ctx.c

910 lines
20 KiB
C
Raw Normal View History

/* RetroArch - A frontend for libretro.
2014-01-01 00:50:59 +00:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2016-01-10 03:41:52 +00:00
* Copyright (C) 2011-2016 - Daniel De Matteis
*
* 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/>.
*/
/* KMS/DRM context, running without any window manager.
* Based on kmscube example by Rob Clark.
*/
#include <stdint.h>
2015-09-16 03:53:34 +00:00
#include <errno.h>
#include <unistd.h>
2015-09-16 03:53:34 +00:00
#include <math.h>
#include <sched.h>
#include <sys/time.h>
2015-09-16 03:53:34 +00:00
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/poll.h>
#include <libdrm/drm.h>
#include <gbm.h>
2015-09-16 03:53:34 +00:00
#include <lists/dir_list.h>
2016-03-20 15:29:14 +00:00
#include <streams/file_stream.h>
2015-09-16 03:53:34 +00:00
#include "../../verbosity.h"
2016-09-05 16:31:32 +00:00
#include "../../configuration.h"
2015-09-16 03:53:34 +00:00
#include "../../runloop.h"
#include "../../frontend/frontend_driver.h"
#include "../common/drm_common.h"
2016-02-22 10:37:37 +00:00
#ifdef HAVE_EGL
2015-11-19 12:24:51 +00:00
#include "../common/egl_common.h"
2016-02-22 10:37:37 +00:00
#endif
#if defined(HAVE_OPENGL) || defined(HAVE_OPENGLES)
2015-11-17 07:01:33 +00:00
#include "../common/gl_common.h"
2016-02-22 10:37:37 +00:00
#endif
2015-09-16 03:53:34 +00:00
#ifdef HAVE_CONFIG_H
#include "../../config.h"
#endif
2016-02-22 10:37:37 +00:00
#ifdef HAVE_OPENGLES
#ifndef EGL_OPENGL_ES3_BIT_KHR
#define EGL_OPENGL_ES3_BIT_KHR 0x0040
#endif
2016-02-22 10:37:37 +00:00
#endif
2016-02-22 10:47:18 +00:00
static enum gfx_ctx_api drm_api;
static struct gbm_bo *g_bo;
static struct gbm_bo *g_next_bo;
static struct gbm_surface *g_gbm_surface;
static struct gbm_device *g_gbm_dev;
static bool waiting_for_flip;
2016-02-22 11:59:13 +00:00
typedef struct gfx_ctx_drm_data
{
#ifdef HAVE_EGL
egl_ctx_data_t egl;
#endif
2016-02-22 12:25:48 +00:00
RFILE *drm;
unsigned interval;
unsigned fb_width;
unsigned fb_height;
2016-05-05 15:35:28 +00:00
bool core_hw_context_enable;
2016-02-22 11:59:13 +00:00
} gfx_ctx_drm_data_t;
struct drm_fb
{
struct gbm_bo *bo;
uint32_t fb_id;
};
static void drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
{
struct drm_fb *fb = (struct drm_fb*)data;
if (fb && fb->fb_id)
drmModeRmFB(g_drm_fd, fb->fb_id);
free(fb);
}
static struct drm_fb *drm_fb_get_from_bo(struct gbm_bo *bo)
{
int ret;
unsigned width, height, stride, handle;
struct drm_fb *fb = (struct drm_fb*)gbm_bo_get_user_data(bo);
if (fb)
return fb;
fb = (struct drm_fb*)calloc(1, sizeof(*fb));
fb->bo = bo;
width = gbm_bo_get_width(bo);
height = gbm_bo_get_height(bo);
stride = gbm_bo_get_stride(bo);
handle = gbm_bo_get_handle(bo).u32;
RARCH_LOG("[KMS]: New FB: %ux%u (stride: %u).\n",
2016-02-14 15:31:39 +00:00
width, height, stride);
2016-02-14 15:31:39 +00:00
ret = drmModeAddFB(g_drm_fd, width, height, 24, 32,
stride, handle, &fb->fb_id);
if (ret < 0)
2015-11-20 12:50:17 +00:00
goto error;
gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
return fb;
2015-11-20 12:50:17 +00:00
error:
RARCH_ERR("[KMS]: Failed to create FB: %s\n", strerror(errno));
2015-11-20 12:50:17 +00:00
free(fb);
return NULL;
}
static void gfx_ctx_drm_swap_interval(void *data, unsigned interval)
{
2016-02-22 11:59:13 +00:00
gfx_ctx_drm_data_t *drm = (gfx_ctx_drm_data_t*)data;
drm->interval = interval;
if (interval > 1)
RARCH_WARN("[KMS]: Swap intervals > 1 currently not supported. Will use swap interval of 1.\n");
}
static void gfx_ctx_drm_check_window(void *data, bool *quit,
bool *resize, unsigned *width, unsigned *height, unsigned frame_count)
{
(void)data;
(void)frame_count;
(void)width;
(void)height;
*resize = false;
*quit = (bool)frontend_driver_get_signal_handler_state();
}
2012-09-18 18:36:16 +00:00
2016-02-22 11:43:18 +00:00
static void drm_flip_handler(int fd, unsigned frame,
unsigned sec, unsigned usec, void *data)
{
(void)fd;
(void)sec;
(void)usec;
#if 0
static unsigned first_page_flip;
static unsigned last_page_flip;
if (!first_page_flip)
first_page_flip = frame;
if (last_page_flip)
{
unsigned missed = frame - last_page_flip - 1;
if (missed)
RARCH_LOG("[KMS]: Missed %u VBlank(s) (Frame: %u, DRM frame: %u).\n",
missed, frame - first_page_flip, frame);
}
last_page_flip = frame;
#endif
2012-09-18 18:36:16 +00:00
*(bool*)data = false;
}
2016-02-22 11:43:18 +00:00
static bool gfx_ctx_drm_wait_flip(bool block)
{
2015-01-10 02:25:44 +00:00
int timeout = 0;
2015-11-19 16:02:02 +00:00
if (!waiting_for_flip)
return false;
2015-01-10 02:25:44 +00:00
if (block)
timeout = -1;
while (waiting_for_flip)
{
2015-11-28 16:16:19 +00:00
if (!drm_wait_flip(timeout))
break;
}
2015-01-10 02:25:44 +00:00
if (waiting_for_flip)
2015-11-19 15:06:16 +00:00
return true;
2015-01-10 02:25:44 +00:00
/* Page flip has taken place. */
2015-01-10 02:25:44 +00:00
/* This buffer is not on-screen anymore. Release it to GBM. */
gbm_surface_release_buffer(g_gbm_surface, g_bo);
2015-06-30 12:37:11 +00:00
/* This buffer is being shown now. */
g_bo = g_next_bo;
2015-11-19 15:06:16 +00:00
return false;
}
2016-02-22 11:43:18 +00:00
static bool gfx_ctx_drm_queue_flip(void)
{
struct drm_fb *fb = NULL;
2016-02-22 11:43:18 +00:00
g_next_bo = gbm_surface_lock_front_buffer(g_gbm_surface);
fb = (struct drm_fb*)drm_fb_get_from_bo(g_next_bo);
2015-11-26 16:30:01 +00:00
if (drmModePageFlip(g_drm_fd, g_crtc_id, fb->fb_id,
2015-11-19 16:02:02 +00:00
DRM_MODE_PAGE_FLIP_EVENT, &waiting_for_flip) == 0)
return true;
/* Failed to queue page flip. */
return false;
}
2016-02-22 11:43:18 +00:00
static void gfx_ctx_drm_swap_buffers(void *data)
{
2016-02-22 11:59:13 +00:00
gfx_ctx_drm_data_t *drm = (gfx_ctx_drm_data_t*)data;
settings_t *settings = config_get_ptr();
2016-02-22 11:43:18 +00:00
switch (drm_api)
{
case GFX_CTX_OPENGL_API:
case GFX_CTX_OPENGL_ES_API:
case GFX_CTX_OPENVG_API:
2016-02-22 11:10:23 +00:00
#ifdef HAVE_EGL
2016-03-01 05:49:05 +00:00
egl_swap_buffers(&drm->egl);
2016-02-22 11:10:23 +00:00
#endif
2016-02-22 11:43:18 +00:00
break;
default:
break;
}
/* I guess we have to wait for flip to have taken
2015-11-19 16:02:02 +00:00
* place before another flip can be queued up.
*
* If true, we are still waiting for a flip
* (nonblocking mode, so just drop the frame). */
2016-02-22 11:43:18 +00:00
if (gfx_ctx_drm_wait_flip(drm->interval))
2015-11-19 16:02:02 +00:00
return;
2016-02-22 11:43:18 +00:00
waiting_for_flip = gfx_ctx_drm_queue_flip();
/* Triple-buffered page flips */
if (settings->video.max_swapchain_images >= 3 &&
gbm_surface_has_free_buffers(g_gbm_surface))
2015-11-19 15:06:16 +00:00
return;
2016-02-22 11:43:18 +00:00
gfx_ctx_drm_wait_flip(true);
}
static bool gfx_ctx_drm_set_resize(void *data,
unsigned width, unsigned height)
{
(void)data;
(void)width;
(void)height;
return false;
}
static void gfx_ctx_drm_update_window_title(void *data)
{
char buf[128];
char buf_fps[128];
2015-03-20 21:08:36 +00:00
settings_t *settings = config_get_ptr();
2016-11-25 17:25:59 +00:00
buf[0] = buf_fps[0] = '\0';
video_monitor_get_fps(buf, sizeof(buf),
2015-03-07 22:03:56 +00:00
buf_fps, sizeof(buf_fps));
2016-11-25 17:25:59 +00:00
2015-03-20 21:08:36 +00:00
if (settings->fps_show)
2015-12-07 14:32:14 +00:00
runloop_msg_queue_push( buf_fps, 1, 1, false);
}
static void gfx_ctx_drm_get_video_size(void *data,
unsigned *width, unsigned *height)
{
2016-02-22 11:59:13 +00:00
gfx_ctx_drm_data_t *drm = (gfx_ctx_drm_data_t*)data;
if (!drm)
return;
*width = drm->fb_width;
*height = drm->fb_height;
}
2016-02-22 11:59:13 +00:00
static void free_drm_resources(gfx_ctx_drm_data_t *drm)
2014-04-05 20:11:19 +00:00
{
if (!drm)
return;
2014-04-05 20:11:19 +00:00
2016-07-23 04:05:21 +00:00
/* Restore original CRTC. */
drm_restore_crtc();
if (g_gbm_surface)
gbm_surface_destroy(g_gbm_surface);
2014-04-05 20:11:19 +00:00
if (g_gbm_dev)
gbm_device_destroy(g_gbm_dev);
2014-04-05 20:11:19 +00:00
2015-11-26 16:34:35 +00:00
drm_free();
if (drm->drm)
if (g_drm_fd >= 0)
filestream_close(drm->drm);
g_gbm_surface = NULL;
g_gbm_dev = NULL;
g_drm_fd = -1;
}
2016-02-22 11:59:13 +00:00
static void gfx_ctx_drm_destroy_resources(gfx_ctx_drm_data_t *drm)
{
if (!drm)
return;
/* Make sure we acknowledge all page-flips. */
2016-02-22 11:43:18 +00:00
gfx_ctx_drm_wait_flip(true);
2016-02-22 11:43:18 +00:00
switch (drm_api)
{
case GFX_CTX_OPENGL_API:
case GFX_CTX_OPENGL_ES_API:
case GFX_CTX_OPENVG_API:
#ifdef HAVE_EGL
2016-03-01 05:49:05 +00:00
egl_destroy(&drm->egl);
#endif
2016-02-22 11:43:18 +00:00
break;
case GFX_CTX_NONE:
default:
break;
}
free_drm_resources(drm);
2014-04-05 20:11:19 +00:00
2015-11-26 16:30:01 +00:00
g_drm_mode = NULL;
g_crtc_id = 0;
g_connector_id = 0;
2014-04-05 20:11:19 +00:00
drm->fb_width = 0;
drm->fb_height = 0;
2014-04-05 20:11:19 +00:00
g_bo = NULL;
g_next_bo = NULL;
2014-04-05 20:11:19 +00:00
}
2016-02-22 11:56:16 +00:00
static void *gfx_ctx_drm_init(void *video_driver)
{
2015-11-26 17:18:44 +00:00
int fd, i;
2014-04-06 13:10:39 +00:00
unsigned monitor_index;
2015-06-13 00:10:06 +00:00
unsigned gpu_index = 0;
const char *gpu = NULL;
struct string_list *gpu_descriptors = NULL;
2016-02-22 11:59:13 +00:00
gfx_ctx_drm_data_t *drm = (gfx_ctx_drm_data_t*)
calloc(1, sizeof(gfx_ctx_drm_data_t));
if (!drm)
return NULL;
gpu_descriptors = dir_list_new("/dev/dri", NULL, false, true, false, false);
2014-04-05 20:11:19 +00:00
2014-04-05 13:57:33 +00:00
nextgpu:
free_drm_resources(drm);
2014-04-05 20:11:19 +00:00
if (!gpu_descriptors || gpu_index == gpu_descriptors->size)
{
2016-02-22 11:43:18 +00:00
RARCH_ERR("[KMS]: Couldn't find a suitable DRM device.\n");
goto error;
}
2014-04-05 17:51:28 +00:00
gpu = gpu_descriptors->elems[gpu_index++].data;
2014-04-05 13:57:33 +00:00
2016-03-24 03:23:17 +00:00
drm->drm = filestream_open(gpu, RFILE_MODE_READ_WRITE, -1);
2016-02-22 12:25:48 +00:00
if (!drm->drm)
2014-04-05 13:57:33 +00:00
{
2016-02-22 11:43:18 +00:00
RARCH_WARN("[KMS]: Couldn't open DRM device.\n");
2014-04-05 13:57:33 +00:00
goto nextgpu;
}
2016-03-24 03:09:25 +00:00
fd = filestream_get_fd(drm->drm);
2015-11-26 17:47:07 +00:00
if (!drm_get_resources(fd))
2014-04-05 13:57:33 +00:00
goto nextgpu;
2014-04-06 13:10:39 +00:00
2015-11-26 17:47:07 +00:00
if (!drm_get_connector(fd))
2014-04-05 13:57:33 +00:00
goto nextgpu;
2015-11-26 17:23:51 +00:00
if (!drm_get_encoder(fd))
2014-04-05 13:57:33 +00:00
goto nextgpu;
2015-11-26 17:47:07 +00:00
drm_setup(fd);
/* First mode is assumed to be the "optimal"
* one for get_video_size() purposes. */
drm->fb_width = g_drm_connector->modes[0].hdisplay;
drm->fb_height = g_drm_connector->modes[0].vdisplay;
g_gbm_dev = gbm_create_device(fd);
if (!g_gbm_dev)
{
2016-02-22 11:43:18 +00:00
RARCH_WARN("[KMS]: Couldn't create GBM device.\n");
2014-04-05 13:57:33 +00:00
goto nextgpu;
}
2014-04-05 17:59:31 +00:00
dir_list_free(gpu_descriptors);
/* Setup the flip handler. */
2016-02-22 11:50:45 +00:00
g_drm_fds.fd = fd;
g_drm_fds.events = POLLIN;
g_drm_evctx.version = DRM_EVENT_CONTEXT_VERSION;
g_drm_evctx.page_flip_handler = drm_flip_handler;
2016-02-22 11:56:16 +00:00
g_drm_fd = fd;
2015-11-26 17:18:44 +00:00
return drm;
error:
2014-04-05 17:32:51 +00:00
dir_list_free(gpu_descriptors);
gfx_ctx_drm_destroy_resources(drm);
if (drm)
free(drm);
return NULL;
}
2016-02-22 11:56:16 +00:00
static EGLint *gfx_ctx_drm_egl_fill_attribs(
2016-02-22 11:59:13 +00:00
gfx_ctx_drm_data_t *drm, EGLint *attr)
{
switch (drm_api)
{
#ifdef EGL_KHR_create_context
case GFX_CTX_OPENGL_API:
{
bool debug = false;
#ifdef HAVE_OPENGL
unsigned version = drm->egl.major * 1000 + drm->egl.minor;
2015-03-21 04:55:31 +00:00
bool core = version >= 3001;
#ifdef GL_DEBUG
debug = true;
#else
2016-05-08 12:00:51 +00:00
struct retro_hw_render_callback *hwr = video_driver_get_hw_context();
debug = hwr->debug_context;
#endif
if (core)
{
*attr++ = EGL_CONTEXT_MAJOR_VERSION_KHR;
*attr++ = drm->egl.major;
*attr++ = EGL_CONTEXT_MINOR_VERSION_KHR;
*attr++ = drm->egl.minor;
/* Technically, we don't have core/compat until 3.2.
* Version 3.1 is either compat or not depending
* on GL_ARB_compatibility. */
if (version >= 3002)
{
*attr++ = EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR;
*attr++ = EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR;
}
}
if (debug)
{
*attr++ = EGL_CONTEXT_FLAGS_KHR;
*attr++ = EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR;
}
break;
#endif
}
#endif
case GFX_CTX_OPENGL_ES_API:
#ifdef HAVE_OPENGLES
*attr++ = EGL_CONTEXT_CLIENT_VERSION;
2016-02-22 11:50:45 +00:00
*attr++ = drm->egl.major
? (EGLint)drm->egl.major : 2;
2014-05-03 13:21:14 +00:00
#ifdef EGL_KHR_create_context
if (drm->egl.minor > 0)
2014-05-03 13:21:14 +00:00
{
*attr++ = EGL_CONTEXT_MINOR_VERSION_KHR;
*attr++ = drm->egl.minor;
2014-05-03 13:21:14 +00:00
}
#endif
2014-05-03 13:21:14 +00:00
#endif
break;
2016-02-22 11:43:18 +00:00
case GFX_CTX_NONE:
default:
break;
}
*attr = EGL_NONE;
return attr;
}
#ifdef HAVE_EGL
2014-10-27 13:35:23 +00:00
#define DRM_EGL_ATTRIBS_BASE \
EGL_SURFACE_TYPE, EGL_WINDOW_BIT, \
EGL_RED_SIZE, 1, \
EGL_GREEN_SIZE, 1, \
EGL_BLUE_SIZE, 1, \
EGL_ALPHA_SIZE, 0, \
EGL_DEPTH_SIZE, 0
2016-02-22 11:59:13 +00:00
static bool gfx_ctx_drm_egl_set_video_mode(gfx_ctx_drm_data_t *drm)
{
2016-02-22 11:50:45 +00:00
const EGLint *attrib_ptr = NULL;
static const EGLint egl_attribs_gl[] = {
2014-10-27 13:35:23 +00:00
DRM_EGL_ATTRIBS_BASE,
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
EGL_NONE,
2012-09-24 23:26:22 +00:00
};
static const EGLint egl_attribs_gles[] = {
2014-10-27 13:35:23 +00:00
DRM_EGL_ATTRIBS_BASE,
2012-09-24 23:26:22 +00:00
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE,
};
#ifdef EGL_KHR_create_context
static const EGLint egl_attribs_gles3[] = {
2014-10-27 13:35:23 +00:00
DRM_EGL_ATTRIBS_BASE,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT_KHR,
EGL_NONE,
};
#endif
2016-02-22 10:37:37 +00:00
#ifdef HAVE_VG
static const EGLint egl_attribs_vg[] = {
2014-10-27 13:35:23 +00:00
DRM_EGL_ATTRIBS_BASE,
EGL_RENDERABLE_TYPE, EGL_OPENVG_BIT,
EGL_NONE,
2012-09-24 23:26:22 +00:00
};
2016-02-22 10:37:37 +00:00
#endif
EGLint major;
EGLint minor;
EGLint n;
EGLint egl_attribs[16];
EGLint *egl_attribs_ptr = NULL;
EGLint *attr = NULL;
2015-01-10 02:25:44 +00:00
2016-02-22 10:47:18 +00:00
switch (drm_api)
{
case GFX_CTX_OPENGL_API:
#ifdef HAVE_OPENGL
attrib_ptr = egl_attribs_gl;
break;
case GFX_CTX_OPENGL_ES_API:
#ifdef EGL_KHR_create_context
if (drm->egl.major >= 3)
attrib_ptr = egl_attribs_gles3;
else
#endif
attrib_ptr = egl_attribs_gles;
#endif
break;
case GFX_CTX_OPENVG_API:
#ifdef HAVE_VG
attrib_ptr = egl_attribs_vg;
#endif
break;
2016-02-22 11:43:18 +00:00
case GFX_CTX_NONE:
default:
break;
}
switch (drm_api)
{
case GFX_CTX_OPENGL_API:
case GFX_CTX_OPENGL_ES_API:
2016-02-23 00:34:28 +00:00
case GFX_CTX_OPENVG_API:
#ifdef HAVE_EGL
2016-03-01 05:49:05 +00:00
if (!egl_init_context(&drm->egl, (EGLNativeDisplayType)g_gbm_dev, &major,
2016-02-23 00:34:28 +00:00
&minor, &n, attrib_ptr))
goto error;
attr = gfx_ctx_drm_egl_fill_attribs(drm, egl_attribs);
egl_attribs_ptr = &egl_attribs[0];
2016-03-01 05:49:05 +00:00
if (!egl_create_context(&drm->egl, (attr != egl_attribs_ptr)
2016-02-23 00:34:28 +00:00
? egl_attribs_ptr : NULL))
goto error;
2016-03-01 05:49:05 +00:00
if (!egl_create_surface(&drm->egl, (EGLNativeWindowType)g_gbm_surface))
2016-02-23 00:34:28 +00:00
return false;
#if defined(HAVE_OPENGL) || defined(HAVE_OPENGLES)
glClear(GL_COLOR_BUFFER_BIT);
2016-02-23 00:34:28 +00:00
#endif
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
2016-02-22 11:50:45 +00:00
egl_swap_buffers(drm);
return true;
error:
egl_report_error();
return false;
}
#endif
static bool gfx_ctx_drm_set_video_mode(void *data,
unsigned width, unsigned height,
bool fullscreen)
{
float refresh_mod;
int i, ret = 0;
struct drm_fb *fb = NULL;
settings_t *settings = config_get_ptr();
2016-02-22 11:59:13 +00:00
gfx_ctx_drm_data_t *drm = (gfx_ctx_drm_data_t*)data;
2016-02-22 11:50:45 +00:00
if (!drm)
return false;
frontend_driver_install_signal_handler();
2016-02-22 11:50:45 +00:00
/* If we use black frame insertion,
2016-02-22 11:50:45 +00:00
* we fake a 60 Hz monitor for 120 Hz one,
* etc, so try to match that. */
refresh_mod = settings->video.black_frame_insertion
? 0.5f : 1.0f;
/* Find desired video mode, and use that.
* If not fullscreen, we get desired windowed size,
* which is not appropriate. */
2014-04-06 13:41:53 +00:00
if ((width == 0 && height == 0) || !fullscreen)
2015-11-26 16:30:01 +00:00
g_drm_mode = &g_drm_connector->modes[0];
else
{
2016-02-22 11:50:45 +00:00
/* Try to match settings->video.refresh_rate
* as closely as possible.
*
* Lower resolutions tend to have multiple supported
* refresh rates as well.
*/
float minimum_fps_diff = 0.0f;
/* Find best match. */
2015-11-26 16:30:01 +00:00
for (i = 0; i < g_drm_connector->count_modes; i++)
{
2015-06-29 19:52:55 +00:00
float diff;
2015-11-26 16:30:01 +00:00
if (width != g_drm_connector->modes[i].hdisplay ||
height != g_drm_connector->modes[i].vdisplay)
continue;
2015-11-26 16:30:01 +00:00
diff = fabsf(refresh_mod * g_drm_connector->modes[i].vrefresh
2015-06-29 19:52:55 +00:00
- settings->video.refresh_rate);
2015-11-26 16:30:01 +00:00
if (!g_drm_mode || diff < minimum_fps_diff)
{
2015-11-26 16:30:01 +00:00
g_drm_mode = &g_drm_connector->modes[i];
minimum_fps_diff = diff;
}
}
}
2015-11-26 16:30:01 +00:00
if (!g_drm_mode)
{
2016-02-14 15:31:39 +00:00
RARCH_ERR("[KMS/EGL]: Did not find suitable video mode for %u x %u.\n",
width, height);
goto error;
}
drm->fb_width = g_drm_mode->hdisplay;
drm->fb_height = g_drm_mode->vdisplay;
/* Create GBM surface. */
g_gbm_surface = gbm_surface_create(
g_gbm_dev,
drm->fb_width,
drm->fb_height,
GBM_FORMAT_XRGB8888,
GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING);
if (!g_gbm_surface)
{
RARCH_ERR("[KMS/EGL]: Couldn't create GBM surface.\n");
goto error;
}
2016-02-22 11:50:45 +00:00
switch (drm_api)
{
2016-02-22 11:50:45 +00:00
case GFX_CTX_OPENGL_API:
case GFX_CTX_OPENGL_ES_API:
case GFX_CTX_OPENVG_API:
#ifdef HAVE_EGL
if (!gfx_ctx_drm_egl_set_video_mode(drm))
goto error;
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
g_bo = gbm_surface_lock_front_buffer(g_gbm_surface);
fb = drm_fb_get_from_bo(g_bo);
ret = drmModeSetCrtc(g_drm_fd,
2015-11-26 16:30:01 +00:00
g_crtc_id, fb->fb_id, 0, 0, &g_connector_id, 1, g_drm_mode);
if (ret < 0)
goto error;
return true;
error:
gfx_ctx_drm_destroy_resources(drm);
if (drm)
free(drm);
return false;
}
static void gfx_ctx_drm_destroy(void *data)
{
2016-02-22 11:59:13 +00:00
gfx_ctx_drm_data_t *drm = (gfx_ctx_drm_data_t*)data;
if (!drm)
return;
gfx_ctx_drm_destroy_resources(drm);
free(drm);
}
static void gfx_ctx_drm_input_driver(void *data,
const input_driver_t **input, void **input_data)
{
(void)data;
*input = NULL;
*input_data = NULL;
}
static bool gfx_ctx_drm_has_focus(void *data)
{
2015-01-10 02:25:44 +00:00
return true;
}
static bool gfx_ctx_drm_suppress_screensaver(void *data, bool enable)
2015-01-18 21:32:14 +00:00
{
(void)data;
(void)enable;
return false;
}
static bool gfx_ctx_drm_has_windowed(void *data)
{
(void)data;
return false;
}
static bool gfx_ctx_drm_bind_api(void *video_driver,
enum gfx_ctx_api api, unsigned major, unsigned minor)
2012-09-24 23:26:22 +00:00
{
(void)video_driver;
2015-01-10 02:25:44 +00:00
2016-02-22 11:10:23 +00:00
#ifdef HAVE_EGL
g_egl_major = major;
g_egl_minor = minor;
2016-02-22 11:10:23 +00:00
#endif
2016-02-22 10:47:18 +00:00
drm_api = api;
2015-01-10 02:25:44 +00:00
2012-09-24 23:26:22 +00:00
switch (api)
{
case GFX_CTX_OPENGL_API:
2016-02-22 10:37:37 +00:00
#if defined(HAVE_EGL) && defined(HAVE_OPENGL)
#ifndef EGL_KHR_create_context
if ((major * 1000 + minor) >= 3001)
return false;
#endif
2012-09-24 23:26:22 +00:00
return eglBindAPI(EGL_OPENGL_API);
2016-02-22 10:37:37 +00:00
#else
break;
#endif
2012-09-24 23:26:22 +00:00
case GFX_CTX_OPENGL_ES_API:
2016-02-22 10:37:37 +00:00
#if defined(HAVE_EGL) && defined(HAVE_OPENGLES)
#ifndef EGL_KHR_create_context
if (major >= 3)
return false;
#endif
2012-09-24 23:26:22 +00:00
return eglBindAPI(EGL_OPENGL_ES_API);
2016-02-22 10:37:37 +00:00
#else
break;
#endif
2012-09-24 23:26:22 +00:00
case GFX_CTX_OPENVG_API:
2016-02-22 10:37:37 +00:00
#if defined(HAVE_EGL) && defined(HAVE_VG)
2012-09-24 23:26:22 +00:00
return eglBindAPI(EGL_OPENVG_API);
2016-02-22 10:37:37 +00:00
#endif
2016-02-22 11:43:18 +00:00
case GFX_CTX_NONE:
2012-09-24 23:26:22 +00:00
default:
2015-04-16 21:04:22 +00:00
break;
2012-09-24 23:26:22 +00:00
}
return false;
2012-09-24 23:26:22 +00:00
}
2016-02-22 11:56:16 +00:00
static gfx_ctx_proc_t gfx_ctx_drm_get_proc_address(const char *symbol)
{
switch (drm_api)
{
case GFX_CTX_OPENGL_API:
case GFX_CTX_OPENGL_ES_API:
case GFX_CTX_OPENVG_API:
#ifdef HAVE_EGL
return egl_get_proc_address(symbol);
#else
break;
#endif
case GFX_CTX_NONE:
default:
break;
}
return NULL;
}
static void gfx_ctx_drm_bind_hw_render(void *data, bool enable)
{
2016-03-01 05:49:05 +00:00
gfx_ctx_drm_data_t *drm = (gfx_ctx_drm_data_t*)data;
2016-02-22 11:56:16 +00:00
switch (drm_api)
{
case GFX_CTX_OPENGL_API:
case GFX_CTX_OPENGL_ES_API:
case GFX_CTX_OPENVG_API:
#ifdef HAVE_EGL
2016-03-01 05:49:05 +00:00
egl_bind_hw_render(&drm->egl, enable);
2016-02-22 11:56:16 +00:00
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
}
static uint32_t gfx_ctx_drm_get_flags(void *data)
2016-05-05 15:35:28 +00:00
{
uint32_t flags = 0;
gfx_ctx_drm_data_t *drm = (gfx_ctx_drm_data_t*)data;
BIT32_SET(flags, GFX_CTX_FLAGS_CUSTOMIZABLE_SWAPCHAIN_IMAGES);
2016-05-05 15:35:28 +00:00
if (drm->core_hw_context_enable)
BIT32_SET(flags, GFX_CTX_FLAGS_GL_CORE_CONTEXT);
return flags;
}
static void gfx_ctx_drm_set_flags(void *data, uint32_t flags)
{
gfx_ctx_drm_data_t *drm = (gfx_ctx_drm_data_t*)data;
2016-05-05 15:35:28 +00:00
if (BIT32_GET(flags, GFX_CTX_FLAGS_GL_CORE_CONTEXT))
drm->core_hw_context_enable = true;
}
2016-02-22 11:59:13 +00:00
const gfx_ctx_driver_t gfx_ctx_drm = {
2016-02-22 11:56:16 +00:00
gfx_ctx_drm_init,
gfx_ctx_drm_destroy,
gfx_ctx_drm_bind_api,
gfx_ctx_drm_swap_interval,
2016-02-22 11:50:45 +00:00
gfx_ctx_drm_set_video_mode,
gfx_ctx_drm_get_video_size,
NULL, /* get_video_output_size */
NULL, /* get_video_output_prev */
NULL, /* get_video_output_next */
NULL, /* get_metrics */
NULL,
gfx_ctx_drm_update_window_title,
gfx_ctx_drm_check_window,
gfx_ctx_drm_set_resize,
gfx_ctx_drm_has_focus,
gfx_ctx_drm_suppress_screensaver,
gfx_ctx_drm_has_windowed,
2016-02-22 11:43:18 +00:00
gfx_ctx_drm_swap_buffers,
gfx_ctx_drm_input_driver,
2016-02-22 11:56:16 +00:00
gfx_ctx_drm_get_proc_address,
NULL,
NULL,
2013-03-29 12:46:11 +00:00
NULL,
2016-02-22 11:59:13 +00:00
"kms",
gfx_ctx_drm_get_flags,
2016-05-05 15:35:28 +00:00
gfx_ctx_drm_set_flags,
2016-08-31 13:02:07 +00:00
gfx_ctx_drm_bind_hw_render,
NULL,
NULL
2012-09-24 23:26:22 +00:00
};