2012-09-16 19:39:40 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2014-01-01 00:50:59 +00:00
|
|
|
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
|
|
|
|
* Copyright (C) 2011-2014 - Daniel De Matteis
|
2012-09-16 19:39:40 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// KMS/DRM context, running without any window manager.
|
|
|
|
// Based on kmscube example by Rob Clark.
|
|
|
|
|
|
|
|
#include "../../driver.h"
|
|
|
|
#include "../gfx_context.h"
|
|
|
|
#include "../gl_common.h"
|
|
|
|
#include "../gfx_common.h"
|
2014-04-05 17:32:51 +00:00
|
|
|
#include "../../file_path.h"
|
2012-09-16 19:39:40 +00:00
|
|
|
|
2012-09-18 10:16:27 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "../../config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-09-16 19:39:40 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <signal.h>
|
2012-09-18 19:51:50 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sched.h>
|
2012-09-18 22:44:50 +00:00
|
|
|
#include <sys/time.h>
|
2012-09-16 19:39:40 +00:00
|
|
|
|
|
|
|
#include <EGL/egl.h>
|
|
|
|
#include <EGL/eglext.h>
|
|
|
|
|
|
|
|
#include <libdrm/drm.h>
|
|
|
|
#include <xf86drm.h>
|
|
|
|
#include <xf86drmMode.h>
|
|
|
|
#include <gbm.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2014-04-19 14:06:12 +00:00
|
|
|
static bool g_use_hw_ctx;
|
|
|
|
static EGLContext g_egl_hw_ctx;
|
2012-09-16 19:39:40 +00:00
|
|
|
static EGLContext g_egl_ctx;
|
|
|
|
static EGLSurface g_egl_surf;
|
|
|
|
static EGLDisplay g_egl_dpy;
|
|
|
|
static EGLConfig g_config;
|
|
|
|
|
|
|
|
static volatile sig_atomic_t g_quit;
|
|
|
|
static bool g_inited;
|
|
|
|
static unsigned g_interval;
|
2012-09-24 23:26:22 +00:00
|
|
|
static enum gfx_ctx_api g_api;
|
2013-12-13 13:19:25 +00:00
|
|
|
static unsigned g_major;
|
|
|
|
static unsigned g_minor;
|
2012-09-16 19:39:40 +00:00
|
|
|
|
|
|
|
static struct gbm_device *g_gbm_dev;
|
|
|
|
static struct gbm_surface *g_gbm_surface;
|
|
|
|
|
|
|
|
static int g_drm_fd;
|
|
|
|
static drmModeModeInfo *g_drm_mode;
|
|
|
|
static uint32_t g_crtc_id;
|
|
|
|
static uint32_t g_connector_id;
|
|
|
|
|
2012-09-16 20:36:43 +00:00
|
|
|
static drmModeCrtcPtr g_orig_crtc;
|
|
|
|
|
2012-09-16 19:39:40 +00:00
|
|
|
static unsigned g_fb_width; // Just use something for now.
|
|
|
|
static unsigned g_fb_height;
|
|
|
|
|
2012-09-17 22:17:01 +00:00
|
|
|
static struct gbm_bo *g_bo, *g_next_bo;
|
2012-09-16 19:39:40 +00:00
|
|
|
|
2012-09-16 20:36:43 +00:00
|
|
|
static drmModeRes *g_resources;
|
|
|
|
static drmModeConnector *g_connector;
|
|
|
|
static drmModeEncoder *g_encoder;
|
|
|
|
|
2012-09-16 19:39:40 +00:00
|
|
|
struct drm_fb
|
|
|
|
{
|
|
|
|
struct gbm_bo *bo;
|
|
|
|
uint32_t fb_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct drm_fb *drm_fb_get_from_bo(struct gbm_bo *bo);
|
2014-03-09 15:50:18 +00:00
|
|
|
static void gfx_ctx_destroy(void *data);
|
2012-09-16 19:39:40 +00:00
|
|
|
|
|
|
|
static void sighandler(int sig)
|
|
|
|
{
|
|
|
|
(void)sig;
|
|
|
|
g_quit = 1;
|
|
|
|
}
|
|
|
|
|
2014-03-09 15:50:18 +00:00
|
|
|
static void gfx_ctx_swap_interval(void *data, unsigned interval)
|
2012-09-16 19:39:40 +00:00
|
|
|
{
|
2014-03-09 15:50:18 +00:00
|
|
|
(void)data;
|
2012-09-16 19:39:40 +00:00
|
|
|
g_interval = interval;
|
2013-09-01 11:20:10 +00:00
|
|
|
if (interval > 1)
|
|
|
|
RARCH_WARN("[KMS/EGL]: Swap intervals > 1 currently not supported. Will use swap interval of 1.\n");
|
2012-09-16 19:39:40 +00:00
|
|
|
}
|
|
|
|
|
2014-03-09 15:50:18 +00:00
|
|
|
static void gfx_ctx_check_window(void *data, bool *quit,
|
2012-09-16 19:39:40 +00:00
|
|
|
bool *resize, unsigned *width, unsigned *height, unsigned frame_count)
|
|
|
|
{
|
2014-03-09 15:50:18 +00:00
|
|
|
(void)data;
|
2012-09-16 19:39:40 +00:00
|
|
|
(void)frame_count;
|
|
|
|
(void)width;
|
|
|
|
(void)height;
|
|
|
|
|
|
|
|
*resize = false;
|
|
|
|
*quit = g_quit;
|
|
|
|
}
|
|
|
|
|
2012-09-18 18:36:16 +00:00
|
|
|
static unsigned first_page_flip;
|
|
|
|
static unsigned last_page_flip;
|
|
|
|
|
|
|
|
static void page_flip_handler(int fd, unsigned frame, unsigned sec, unsigned usec, void *data)
|
2012-09-16 19:39:40 +00:00
|
|
|
{
|
|
|
|
(void)fd;
|
2013-06-05 16:12:29 +00:00
|
|
|
(void)sec;
|
|
|
|
(void)usec;
|
2012-09-18 18:36:16 +00:00
|
|
|
|
|
|
|
if (!first_page_flip)
|
|
|
|
first_page_flip = frame;
|
|
|
|
|
|
|
|
if (last_page_flip)
|
|
|
|
{
|
|
|
|
unsigned missed = frame - last_page_flip - 1;
|
2013-06-05 16:12:29 +00:00
|
|
|
if (missed)
|
2012-09-20 17:54:46 +00:00
|
|
|
RARCH_LOG("[KMS/EGL]: Missed %u VBlank(s) (Frame: %u, DRM frame: %u).\n",
|
|
|
|
missed, frame - first_page_flip, frame);
|
2012-09-18 18:36:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
last_page_flip = frame;
|
2012-09-18 22:44:50 +00:00
|
|
|
*(bool*)data = false;
|
2012-09-16 19:39:40 +00:00
|
|
|
}
|
|
|
|
|
2012-09-17 22:17:01 +00:00
|
|
|
static bool waiting_for_flip;
|
2012-09-16 19:39:40 +00:00
|
|
|
|
2012-09-18 08:32:35 +00:00
|
|
|
static void wait_flip(bool block)
|
2012-09-17 22:17:01 +00:00
|
|
|
{
|
2012-09-16 19:39:40 +00:00
|
|
|
struct pollfd fds = {0};
|
|
|
|
fds.fd = g_drm_fd;
|
|
|
|
fds.events = POLLIN;
|
|
|
|
|
|
|
|
drmEventContext evctx = {0};
|
|
|
|
evctx.version = DRM_EVENT_CONTEXT_VERSION;
|
|
|
|
evctx.page_flip_handler = page_flip_handler;
|
2012-09-18 08:32:35 +00:00
|
|
|
|
|
|
|
int timeout = block ? -1 : 0;
|
2012-09-16 19:39:40 +00:00
|
|
|
|
|
|
|
while (waiting_for_flip)
|
|
|
|
{
|
|
|
|
fds.revents = 0;
|
2012-09-18 08:32:35 +00:00
|
|
|
if (poll(&fds, 1, timeout) < 0)
|
2012-09-16 19:39:40 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (fds.revents & (POLLHUP | POLLERR))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (fds.revents & POLLIN)
|
|
|
|
drmHandleEvent(g_drm_fd, &evctx);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-09-18 08:32:35 +00:00
|
|
|
if (!waiting_for_flip) // Page flip has taken place.
|
|
|
|
{
|
|
|
|
gbm_surface_release_buffer(g_gbm_surface, g_bo); // This buffer is not on-screen anymore. Release it to GBM.
|
|
|
|
g_bo = g_next_bo; // This buffer is being shown now.
|
|
|
|
}
|
2012-09-17 22:17:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void queue_flip(void)
|
|
|
|
{
|
|
|
|
g_next_bo = gbm_surface_lock_front_buffer(g_gbm_surface);
|
|
|
|
struct drm_fb *fb = drm_fb_get_from_bo(g_next_bo);
|
|
|
|
|
|
|
|
int ret = drmModePageFlip(g_drm_fd, g_crtc_id, fb->fb_id,
|
|
|
|
DRM_MODE_PAGE_FLIP_EVENT, &waiting_for_flip);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
RARCH_ERR("[KMS/EGL]: Failed to queue page flip.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
waiting_for_flip = true;
|
|
|
|
}
|
|
|
|
|
2014-03-09 15:50:18 +00:00
|
|
|
static void gfx_ctx_swap_buffers(void *data)
|
2012-09-17 22:17:01 +00:00
|
|
|
{
|
2014-03-09 15:50:18 +00:00
|
|
|
(void)data;
|
2012-09-18 08:32:35 +00:00
|
|
|
eglSwapBuffers(g_egl_dpy, g_egl_surf);
|
|
|
|
|
2012-09-17 22:17:01 +00:00
|
|
|
// I guess we have to wait for flip to have taken place before another flip can be queued up.
|
|
|
|
if (waiting_for_flip)
|
2012-09-18 08:32:35 +00:00
|
|
|
{
|
|
|
|
wait_flip(g_interval);
|
|
|
|
if (waiting_for_flip) // We are still waiting for a flip (nonblocking mode, just drop the frame).
|
|
|
|
return;
|
|
|
|
}
|
2012-09-17 22:17:01 +00:00
|
|
|
|
|
|
|
queue_flip();
|
|
|
|
|
2012-09-18 08:32:35 +00:00
|
|
|
// We have to wait for this flip to finish. This shouldn't happen as we have triple buffered page-flips.
|
|
|
|
if (!gbm_surface_has_free_buffers(g_gbm_surface))
|
|
|
|
{
|
|
|
|
RARCH_WARN("[KMS/EGL]: Triple buffering is not working correctly ...\n");
|
|
|
|
wait_flip(true);
|
|
|
|
}
|
2012-09-16 20:56:58 +00:00
|
|
|
}
|
|
|
|
|
2014-03-09 15:50:18 +00:00
|
|
|
static void gfx_ctx_set_resize(void *data, unsigned width, unsigned height)
|
2012-09-16 19:39:40 +00:00
|
|
|
{
|
2014-03-09 15:50:18 +00:00
|
|
|
(void)data;
|
2012-09-16 19:39:40 +00:00
|
|
|
(void)width;
|
|
|
|
(void)height;
|
|
|
|
}
|
|
|
|
|
2014-03-09 15:50:18 +00:00
|
|
|
static void gfx_ctx_update_window_title(void *data)
|
2013-06-05 16:12:29 +00:00
|
|
|
{
|
2014-03-09 15:50:18 +00:00
|
|
|
(void)data;
|
2013-10-07 18:17:07 +00:00
|
|
|
char buf[128], buf_fps[128];
|
2013-10-10 23:19:54 +00:00
|
|
|
bool fps_draw = g_settings.fps_show;
|
2013-10-07 19:54:29 +00:00
|
|
|
gfx_get_fps(buf, sizeof(buf), fps_draw ? buf_fps : NULL, sizeof(buf_fps));
|
2013-10-07 18:22:08 +00:00
|
|
|
|
2013-10-08 10:27:25 +00:00
|
|
|
if (fps_draw)
|
2013-10-07 18:22:08 +00:00
|
|
|
msg_queue_push(g_extern.msg_queue, buf_fps, 1, 1);
|
2013-06-05 16:12:29 +00:00
|
|
|
}
|
2012-09-16 19:39:40 +00:00
|
|
|
|
2014-03-09 15:50:18 +00:00
|
|
|
static void gfx_ctx_get_video_size(void *data, unsigned *width, unsigned *height)
|
2012-09-16 19:39:40 +00:00
|
|
|
{
|
2014-03-09 15:50:18 +00:00
|
|
|
(void)data;
|
2012-09-16 19:39:40 +00:00
|
|
|
*width = g_fb_width;
|
|
|
|
*height = g_fb_height;
|
|
|
|
}
|
|
|
|
|
2014-04-05 20:11:19 +00:00
|
|
|
static void free_drm_resources(void)
|
|
|
|
{
|
|
|
|
if (g_gbm_surface)
|
|
|
|
gbm_surface_destroy(g_gbm_surface);
|
|
|
|
|
|
|
|
if (g_gbm_dev)
|
|
|
|
gbm_device_destroy(g_gbm_dev);
|
|
|
|
|
|
|
|
if (g_encoder)
|
|
|
|
drmModeFreeEncoder(g_encoder);
|
|
|
|
|
|
|
|
if (g_connector)
|
|
|
|
drmModeFreeConnector(g_connector);
|
|
|
|
|
|
|
|
if (g_resources)
|
|
|
|
drmModeFreeResources(g_resources);
|
|
|
|
|
|
|
|
if (g_orig_crtc)
|
|
|
|
drmModeFreeCrtc(g_orig_crtc);
|
|
|
|
|
|
|
|
if (g_drm_fd >= 0)
|
|
|
|
close(g_drm_fd);
|
|
|
|
|
|
|
|
g_gbm_surface = NULL;
|
|
|
|
g_gbm_dev = NULL;
|
|
|
|
g_encoder = NULL;
|
|
|
|
g_connector = NULL;
|
|
|
|
g_resources = NULL;
|
|
|
|
g_orig_crtc = NULL;
|
|
|
|
g_drm_fd = -1;
|
|
|
|
}
|
|
|
|
|
2014-03-09 15:50:18 +00:00
|
|
|
static bool gfx_ctx_init(void *data)
|
2012-09-16 19:39:40 +00:00
|
|
|
{
|
2013-10-22 19:26:33 +00:00
|
|
|
int i;
|
2014-04-06 13:10:39 +00:00
|
|
|
unsigned monitor_index;
|
2014-04-06 13:29:59 +00:00
|
|
|
unsigned gpu_index = 0;
|
2014-04-06 13:10:39 +00:00
|
|
|
unsigned monitor = max(g_settings.video.monitor_index, 1);
|
|
|
|
|
2014-04-05 20:11:19 +00:00
|
|
|
const char *gpu;
|
2012-09-16 19:39:40 +00:00
|
|
|
if (g_inited)
|
|
|
|
return false;
|
|
|
|
|
2014-04-05 20:11:19 +00:00
|
|
|
g_drm_fd = -1;
|
2014-04-05 17:32:51 +00:00
|
|
|
struct string_list *gpu_descriptors = dir_list_new("/dev/dri", NULL, false);
|
2014-04-05 20:11:19 +00:00
|
|
|
|
2014-04-05 13:57:33 +00:00
|
|
|
nextgpu:
|
2014-04-05 20:11:19 +00:00
|
|
|
free_drm_resources();
|
|
|
|
|
|
|
|
if (!gpu_descriptors || gpu_index == gpu_descriptors->size)
|
2012-09-16 19:39:40 +00:00
|
|
|
{
|
2014-04-05 13:57:33 +00:00
|
|
|
RARCH_ERR("[KMS/EGL]: Couldn't find a suitable DRM device.\n");
|
2012-09-16 19:39:40 +00:00
|
|
|
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
|
|
|
|
|
|
|
g_drm_fd = open(gpu, O_RDWR);
|
|
|
|
if (g_drm_fd < 0)
|
|
|
|
{
|
|
|
|
RARCH_WARN("[KMS/EGL]: Couldn't open DRM device.\n");
|
|
|
|
goto nextgpu;
|
|
|
|
}
|
2012-09-16 19:39:40 +00:00
|
|
|
|
2012-09-16 20:36:43 +00:00
|
|
|
g_resources = drmModeGetResources(g_drm_fd);
|
|
|
|
if (!g_resources)
|
2012-09-16 19:39:40 +00:00
|
|
|
{
|
2014-04-05 13:57:33 +00:00
|
|
|
RARCH_WARN("[KMS/EGL]: Couldn't get device resources.\n");
|
|
|
|
goto nextgpu;
|
2012-09-16 19:39:40 +00:00
|
|
|
}
|
|
|
|
|
2014-04-06 13:10:39 +00:00
|
|
|
// Enumerate all connectors.
|
|
|
|
monitor_index = 0;
|
|
|
|
RARCH_LOG("[KMS/EGL]: Found %d connectors.\n", g_resources->count_connectors);
|
|
|
|
for (i = 0; i < g_resources->count_connectors; i++)
|
|
|
|
{
|
|
|
|
drmModeConnectorPtr conn = drmModeGetConnector(g_drm_fd, g_resources->connectors[i]);
|
|
|
|
if (conn)
|
|
|
|
{
|
|
|
|
bool connected = conn->connection == DRM_MODE_CONNECTED;
|
|
|
|
RARCH_LOG("[KMS/EGL]: Connector %d connected: %s\n", i, connected ? "yes" : "no");
|
|
|
|
RARCH_LOG("[KMS/EGL]: Connector %d has %d modes.\n", i, conn->count_modes);
|
|
|
|
if (connected && conn->count_modes > 0)
|
|
|
|
{
|
|
|
|
monitor_index++;
|
|
|
|
RARCH_LOG("[KMS/EGL]: Connector %d assigned to monitor index: #%u.\n", i, monitor_index);
|
|
|
|
}
|
|
|
|
drmModeFreeConnector(conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
monitor_index = 0;
|
2013-10-22 19:26:33 +00:00
|
|
|
for (i = 0; i < g_resources->count_connectors; i++)
|
2012-09-16 19:39:40 +00:00
|
|
|
{
|
2012-09-16 20:36:43 +00:00
|
|
|
g_connector = drmModeGetConnector(g_drm_fd, g_resources->connectors[i]);
|
2013-02-15 10:12:33 +00:00
|
|
|
|
|
|
|
if (!g_connector)
|
|
|
|
continue;
|
|
|
|
if (g_connector->connection == DRM_MODE_CONNECTED && g_connector->count_modes > 0)
|
2014-04-06 13:10:39 +00:00
|
|
|
{
|
|
|
|
monitor_index++;
|
|
|
|
if (monitor_index == monitor)
|
|
|
|
break;
|
|
|
|
}
|
2012-09-16 19:39:40 +00:00
|
|
|
|
2012-09-16 20:36:43 +00:00
|
|
|
drmModeFreeConnector(g_connector);
|
|
|
|
g_connector = NULL;
|
2012-09-16 19:39:40 +00:00
|
|
|
}
|
|
|
|
|
2012-09-16 20:36:43 +00:00
|
|
|
if (!g_connector)
|
2012-09-16 19:39:40 +00:00
|
|
|
{
|
2014-04-05 13:57:33 +00:00
|
|
|
RARCH_WARN("[KMS/EGL]: Couldn't get device connector.\n");
|
|
|
|
goto nextgpu;
|
2012-09-16 19:39:40 +00:00
|
|
|
}
|
|
|
|
|
2013-10-22 19:26:33 +00:00
|
|
|
for (i = 0; i < g_resources->count_encoders; i++)
|
2012-09-16 19:39:40 +00:00
|
|
|
{
|
2013-02-15 10:12:33 +00:00
|
|
|
g_encoder = drmModeGetEncoder(g_drm_fd, g_resources->encoders[i]);
|
2012-09-16 19:39:40 +00:00
|
|
|
|
2013-02-15 10:12:33 +00:00
|
|
|
if (!g_encoder)
|
|
|
|
continue;
|
|
|
|
if (g_encoder->encoder_id == g_connector->encoder_id)
|
|
|
|
break;
|
2013-02-15 09:26:45 +00:00
|
|
|
|
2013-02-15 10:12:33 +00:00
|
|
|
drmModeFreeEncoder(g_encoder);
|
|
|
|
g_encoder = NULL;
|
2012-09-16 19:39:40 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 10:12:33 +00:00
|
|
|
if (!g_encoder)
|
2012-09-16 19:39:40 +00:00
|
|
|
{
|
2014-04-05 13:57:33 +00:00
|
|
|
RARCH_WARN("[KMS/EGL]: Couldn't find DRM encoder.\n");
|
|
|
|
goto nextgpu;
|
2012-09-16 19:39:40 +00:00
|
|
|
}
|
|
|
|
|
2014-04-05 20:29:20 +00:00
|
|
|
for (i = 0; i < g_connector->count_modes; i++)
|
|
|
|
{
|
|
|
|
RARCH_LOG("[KMS/EGL]: Mode %d: (%s) %d x %d, %u Hz\n", i,
|
|
|
|
g_connector->modes[i].name,
|
|
|
|
g_connector->modes[i].hdisplay, g_connector->modes[i].vdisplay,
|
|
|
|
g_connector->modes[i].vrefresh);
|
|
|
|
}
|
|
|
|
|
2013-02-15 10:12:33 +00:00
|
|
|
g_crtc_id = g_encoder->crtc_id;
|
|
|
|
g_orig_crtc = drmModeGetCrtc(g_drm_fd, g_crtc_id);
|
|
|
|
if (!g_orig_crtc)
|
|
|
|
RARCH_WARN("[KMS/EGL]: Cannot find original CRTC.\n");
|
|
|
|
|
2012-09-24 23:26:22 +00:00
|
|
|
g_connector_id = g_connector->connector_id;
|
2012-09-16 19:39:40 +00:00
|
|
|
|
2014-04-06 13:20:31 +00:00
|
|
|
// First mode is assumed to be the "optimal" one for get_video_size() purposes.
|
|
|
|
g_fb_width = g_connector->modes[0].hdisplay;
|
|
|
|
g_fb_height = g_connector->modes[0].vdisplay;
|
2012-09-16 19:39:40 +00:00
|
|
|
|
2014-04-06 13:20:31 +00:00
|
|
|
g_gbm_dev = gbm_create_device(g_drm_fd);
|
|
|
|
if (!g_gbm_dev)
|
2012-09-16 19:39:40 +00:00
|
|
|
{
|
2014-04-06 13:20:31 +00:00
|
|
|
RARCH_WARN("[KMS/EGL]: Couldn't create GBM device.\n");
|
2014-04-05 13:57:33 +00:00
|
|
|
goto nextgpu;
|
2012-09-16 19:39:40 +00:00
|
|
|
}
|
|
|
|
|
2014-04-05 17:59:31 +00:00
|
|
|
dir_list_free(gpu_descriptors);
|
2012-09-16 19:39:40 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
error:
|
2014-04-05 17:32:51 +00:00
|
|
|
dir_list_free(gpu_descriptors);
|
2014-03-09 15:50:18 +00:00
|
|
|
gfx_ctx_destroy(data);
|
2012-09-16 19:39:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
|
|
|
|
{
|
2012-09-19 07:38:59 +00:00
|
|
|
struct drm_fb *fb = (struct drm_fb*)data;
|
2012-09-16 19:39:40 +00:00
|
|
|
|
|
|
|
if (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)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
unsigned width = gbm_bo_get_width(bo);
|
|
|
|
unsigned height = gbm_bo_get_height(bo);
|
|
|
|
unsigned stride = gbm_bo_get_stride(bo);
|
|
|
|
unsigned handle = gbm_bo_get_handle(bo).u32;
|
|
|
|
|
2013-02-15 09:26:45 +00:00
|
|
|
RARCH_LOG("[KMS/EGL]: New FB: %ux%u (stride: %u).\n", width, height, stride);
|
|
|
|
|
2012-09-16 19:39:40 +00:00
|
|
|
int ret = drmModeAddFB(g_drm_fd, width, height, 24, 32, stride, handle, &fb->fb_id);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
RARCH_ERR("[KMS/EGL]: Failed to create FB: %s\n", strerror(errno));
|
|
|
|
free(fb);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
|
|
|
|
return fb;
|
|
|
|
}
|
|
|
|
|
2014-03-09 15:50:18 +00:00
|
|
|
static bool gfx_ctx_set_video_mode(void *data,
|
2012-09-16 19:39:40 +00:00
|
|
|
unsigned width, unsigned height,
|
2012-11-20 22:23:18 +00:00
|
|
|
bool fullscreen)
|
2012-09-16 19:39:40 +00:00
|
|
|
{
|
|
|
|
if (g_inited)
|
|
|
|
return false;
|
|
|
|
|
2014-04-06 13:20:31 +00:00
|
|
|
int i;
|
2012-09-28 21:47:20 +00:00
|
|
|
int ret = 0;
|
|
|
|
struct drm_fb *fb = NULL;
|
|
|
|
|
2012-09-16 19:39:40 +00:00
|
|
|
struct sigaction sa = {{0}};
|
|
|
|
sa.sa_handler = sighandler;
|
|
|
|
sa.sa_flags = SA_RESTART;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sigaction(SIGINT, &sa, NULL);
|
|
|
|
sigaction(SIGTERM, &sa, NULL);
|
|
|
|
|
2012-09-25 11:25:03 +00:00
|
|
|
#define EGL_ATTRIBS_BASE \
|
|
|
|
EGL_SURFACE_TYPE, EGL_WINDOW_BIT, \
|
2013-02-15 10:30:59 +00:00
|
|
|
EGL_RED_SIZE, 1, \
|
|
|
|
EGL_GREEN_SIZE, 1, \
|
|
|
|
EGL_BLUE_SIZE, 1, \
|
|
|
|
EGL_ALPHA_SIZE, 0, \
|
|
|
|
EGL_DEPTH_SIZE, 0
|
2012-09-25 11:25:03 +00:00
|
|
|
|
|
|
|
static const EGLint egl_attribs_gl[] = {
|
|
|
|
EGL_ATTRIBS_BASE,
|
|
|
|
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
|
|
|
|
EGL_NONE,
|
2012-09-24 23:26:22 +00:00
|
|
|
};
|
|
|
|
|
2012-09-25 11:25:03 +00:00
|
|
|
static const EGLint egl_attribs_gles[] = {
|
|
|
|
EGL_ATTRIBS_BASE,
|
2012-09-24 23:26:22 +00:00
|
|
|
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
|
2012-09-25 11:25:03 +00:00
|
|
|
EGL_NONE,
|
|
|
|
};
|
|
|
|
|
2013-12-13 13:19:25 +00:00
|
|
|
#ifdef EGL_OPENGL_ES3_BIT_KHR
|
|
|
|
static const EGLint egl_attribs_gles3[] = {
|
|
|
|
EGL_ATTRIBS_BASE,
|
|
|
|
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT_KHR,
|
|
|
|
EGL_NONE,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2012-09-25 11:25:03 +00:00
|
|
|
static const EGLint egl_attribs_vg[] = {
|
|
|
|
EGL_ATTRIBS_BASE,
|
|
|
|
EGL_RENDERABLE_TYPE, EGL_OPENVG_BIT,
|
|
|
|
EGL_NONE,
|
2012-09-24 23:26:22 +00:00
|
|
|
};
|
|
|
|
|
2013-12-13 13:19:25 +00:00
|
|
|
// GLES 2.0+. Don't use for any other API.
|
|
|
|
const EGLint gles_context_attribs[] = {
|
2014-02-06 21:49:14 +00:00
|
|
|
EGL_CONTEXT_CLIENT_VERSION, g_major ? (EGLint)g_major : 2,
|
2012-09-28 21:47:20 +00:00
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
2012-09-25 11:25:03 +00:00
|
|
|
const EGLint *attrib_ptr;
|
|
|
|
switch (g_api)
|
|
|
|
{
|
|
|
|
case GFX_CTX_OPENGL_API:
|
|
|
|
attrib_ptr = egl_attribs_gl;
|
|
|
|
break;
|
|
|
|
case GFX_CTX_OPENGL_ES_API:
|
2013-12-13 13:19:25 +00:00
|
|
|
#ifdef EGL_OPENGL_ES3_BIT_KHR
|
|
|
|
if (g_major >= 3)
|
|
|
|
attrib_ptr = egl_attribs_gles3;
|
|
|
|
else
|
|
|
|
#endif
|
2012-09-25 11:25:03 +00:00
|
|
|
attrib_ptr = egl_attribs_gles;
|
|
|
|
break;
|
|
|
|
case GFX_CTX_OPENVG_API:
|
|
|
|
attrib_ptr = egl_attribs_vg;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
attrib_ptr = NULL;
|
|
|
|
}
|
|
|
|
|
2014-04-06 13:20:31 +00:00
|
|
|
// Find desired video mode, and use that.
|
2014-04-06 13:41:53 +00:00
|
|
|
// If not fullscreen, we get desired windowed size, which is not appropriate.
|
|
|
|
if ((width == 0 && height == 0) || !fullscreen)
|
2014-04-06 13:20:31 +00:00
|
|
|
g_drm_mode = &g_connector->modes[0];
|
|
|
|
else
|
|
|
|
{
|
2014-04-06 13:29:59 +00:00
|
|
|
// Try to match g_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.
|
2014-04-06 13:20:31 +00:00
|
|
|
for (i = 0; i < g_connector->count_modes; i++)
|
|
|
|
{
|
2014-04-06 13:29:59 +00:00
|
|
|
if (width != g_connector->modes[i].hdisplay || height != g_connector->modes[i].vdisplay)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!g_drm_mode)
|
2014-04-06 13:20:31 +00:00
|
|
|
{
|
|
|
|
g_drm_mode = &g_connector->modes[i];
|
2014-04-06 13:29:59 +00:00
|
|
|
minimum_fps_diff = g_drm_mode->vrefresh - g_settings.video.refresh_rate;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
float diff = g_connector->modes[i].vrefresh - g_settings.video.refresh_rate;
|
|
|
|
if (diff < minimum_fps_diff)
|
|
|
|
{
|
|
|
|
g_drm_mode = &g_connector->modes[i];
|
|
|
|
minimum_fps_diff = diff;
|
|
|
|
}
|
2014-04-06 13:20:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!g_drm_mode)
|
|
|
|
{
|
|
|
|
RARCH_ERR("[KMS/EGL]: Did not find suitable video mode for %u x %u.\n", width, height);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_fb_width = g_drm_mode->hdisplay;
|
|
|
|
g_fb_height = g_drm_mode->vdisplay;
|
|
|
|
|
|
|
|
// Create GBM surface.
|
|
|
|
g_gbm_surface = gbm_surface_create(g_gbm_dev,
|
|
|
|
g_fb_width, g_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;
|
|
|
|
}
|
|
|
|
|
2012-09-24 23:26:22 +00:00
|
|
|
g_egl_dpy = eglGetDisplay((EGLNativeDisplayType)g_gbm_dev);
|
|
|
|
if (!g_egl_dpy)
|
|
|
|
{
|
|
|
|
RARCH_ERR("[KMS/EGL]: Couldn't get EGL display.\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
EGLint major, minor;
|
|
|
|
if (!eglInitialize(g_egl_dpy, &major, &minor))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
EGLint n;
|
2012-09-25 11:25:03 +00:00
|
|
|
if (!eglChooseConfig(g_egl_dpy, attrib_ptr, &g_config, 1, &n) || n != 1)
|
2012-09-24 23:26:22 +00:00
|
|
|
goto error;
|
|
|
|
|
2012-09-25 17:28:21 +00:00
|
|
|
g_egl_ctx = eglCreateContext(g_egl_dpy, g_config, EGL_NO_CONTEXT, (g_api == GFX_CTX_OPENGL_ES_API) ? gles_context_attribs : NULL);
|
2014-04-19 14:06:12 +00:00
|
|
|
if (g_egl_ctx == EGL_NO_CONTEXT)
|
2012-09-24 23:26:22 +00:00
|
|
|
goto error;
|
|
|
|
|
2014-04-19 14:06:12 +00:00
|
|
|
if (g_use_hw_ctx)
|
|
|
|
{
|
|
|
|
g_egl_hw_ctx = eglCreateContext(g_egl_dpy, g_config, g_egl_ctx,
|
|
|
|
(g_api == GFX_CTX_OPENGL_ES_API) ? gles_context_attribs : NULL);
|
|
|
|
RARCH_LOG("[KMS/EGL]: Created shared context: %p.\n", (void*)g_egl_hw_ctx);
|
|
|
|
|
|
|
|
if (g_egl_hw_ctx == EGL_NO_CONTEXT)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2012-09-24 23:26:22 +00:00
|
|
|
g_egl_surf = eglCreateWindowSurface(g_egl_dpy, g_config, (EGLNativeWindowType)g_gbm_surface, NULL);
|
|
|
|
if (!g_egl_surf)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!eglMakeCurrent(g_egl_dpy, g_egl_surf, g_egl_surf, g_egl_ctx))
|
|
|
|
goto error;
|
|
|
|
|
2012-09-16 19:39:40 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
eglSwapBuffers(g_egl_dpy, g_egl_surf);
|
|
|
|
|
2012-09-17 22:17:01 +00:00
|
|
|
g_bo = gbm_surface_lock_front_buffer(g_gbm_surface);
|
2012-09-28 21:47:20 +00:00
|
|
|
fb = drm_fb_get_from_bo(g_bo);
|
2012-09-16 19:39:40 +00:00
|
|
|
|
2012-09-28 21:47:20 +00:00
|
|
|
ret = drmModeSetCrtc(g_drm_fd, g_crtc_id, fb->fb_id, 0, 0, &g_connector_id, 1, g_drm_mode);
|
2012-09-16 19:39:40 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
g_inited = true;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
error:
|
2014-03-09 15:50:18 +00:00
|
|
|
gfx_ctx_destroy(data);
|
2012-09-16 19:39:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-09 15:50:18 +00:00
|
|
|
void gfx_ctx_destroy(void *data)
|
2012-09-16 19:39:40 +00:00
|
|
|
{
|
2014-03-09 15:50:18 +00:00
|
|
|
(void)data;
|
2012-09-23 10:11:24 +00:00
|
|
|
// Make sure we acknowledge all page-flips.
|
|
|
|
if (waiting_for_flip)
|
|
|
|
wait_flip(true);
|
|
|
|
|
2012-09-16 19:39:40 +00:00
|
|
|
if (g_egl_dpy)
|
|
|
|
{
|
|
|
|
if (g_egl_ctx)
|
|
|
|
{
|
|
|
|
eglMakeCurrent(g_egl_dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
|
|
|
eglDestroyContext(g_egl_dpy, g_egl_ctx);
|
|
|
|
}
|
|
|
|
|
2014-04-19 14:06:12 +00:00
|
|
|
if (g_egl_hw_ctx)
|
|
|
|
eglDestroyContext(g_egl_dpy, g_egl_hw_ctx);
|
|
|
|
|
2012-09-16 19:39:40 +00:00
|
|
|
if (g_egl_surf)
|
|
|
|
eglDestroySurface(g_egl_dpy, g_egl_surf);
|
|
|
|
eglTerminate(g_egl_dpy);
|
|
|
|
}
|
|
|
|
|
2012-09-16 20:36:43 +00:00
|
|
|
// Be as careful as possible in deinit.
|
|
|
|
// If we screw up, the KMS tty will not restore.
|
|
|
|
|
2012-09-16 19:39:40 +00:00
|
|
|
g_egl_ctx = NULL;
|
|
|
|
g_egl_surf = NULL;
|
|
|
|
g_egl_dpy = NULL;
|
|
|
|
g_config = 0;
|
|
|
|
|
2012-09-16 20:36:43 +00:00
|
|
|
// Restore original CRTC.
|
|
|
|
if (g_orig_crtc)
|
|
|
|
{
|
|
|
|
drmModeSetCrtc(g_drm_fd, g_orig_crtc->crtc_id,
|
|
|
|
g_orig_crtc->buffer_id,
|
|
|
|
g_orig_crtc->x,
|
|
|
|
g_orig_crtc->y,
|
|
|
|
&g_connector_id, 1, &g_orig_crtc->mode);
|
|
|
|
}
|
|
|
|
|
2014-04-05 20:11:19 +00:00
|
|
|
free_drm_resources();
|
2012-09-16 20:36:43 +00:00
|
|
|
|
2014-04-05 20:11:19 +00:00
|
|
|
g_drm_mode = NULL;
|
2012-09-16 20:36:43 +00:00
|
|
|
|
|
|
|
g_quit = 0;
|
|
|
|
g_crtc_id = 0;
|
|
|
|
g_connector_id = 0;
|
|
|
|
|
|
|
|
g_fb_width = 0;
|
|
|
|
g_fb_height = 0;
|
|
|
|
|
2012-09-23 22:00:10 +00:00
|
|
|
g_bo = NULL;
|
|
|
|
g_next_bo = NULL;
|
2012-09-16 20:36:43 +00:00
|
|
|
|
2012-09-23 22:00:10 +00:00
|
|
|
g_inited = false;
|
2012-09-16 19:39:40 +00:00
|
|
|
}
|
|
|
|
|
2014-03-09 15:50:18 +00:00
|
|
|
static void gfx_ctx_input_driver(void *data, const input_driver_t **input, void **input_data)
|
2012-09-16 19:39:40 +00:00
|
|
|
{
|
2014-03-09 15:50:18 +00:00
|
|
|
(void)data;
|
2014-01-24 09:23:48 +00:00
|
|
|
*input = NULL;
|
|
|
|
*input_data = NULL;
|
2012-09-16 19:39:40 +00:00
|
|
|
}
|
|
|
|
|
2014-03-09 15:50:18 +00:00
|
|
|
static bool gfx_ctx_has_focus(void *data)
|
2012-09-16 19:39:40 +00:00
|
|
|
{
|
2014-03-09 15:50:18 +00:00
|
|
|
(void)data;
|
2012-09-16 19:39:40 +00:00
|
|
|
return g_inited;
|
|
|
|
}
|
|
|
|
|
2012-09-24 23:26:22 +00:00
|
|
|
static gfx_ctx_proc_t gfx_ctx_get_proc_address(const char *symbol)
|
2012-09-16 19:39:40 +00:00
|
|
|
{
|
|
|
|
return eglGetProcAddress(symbol);
|
|
|
|
}
|
|
|
|
|
2014-03-09 15:50:18 +00:00
|
|
|
static bool gfx_ctx_bind_api(void *data, enum gfx_ctx_api api, unsigned major, unsigned minor)
|
2012-09-24 23:26:22 +00:00
|
|
|
{
|
2014-03-09 15:50:18 +00:00
|
|
|
(void)data;
|
2013-12-13 13:19:25 +00:00
|
|
|
g_major = major;
|
|
|
|
g_minor = minor;
|
2012-09-24 23:26:22 +00:00
|
|
|
g_api = api;
|
|
|
|
switch (api)
|
|
|
|
{
|
|
|
|
case GFX_CTX_OPENGL_API:
|
|
|
|
return eglBindAPI(EGL_OPENGL_API);
|
|
|
|
case GFX_CTX_OPENGL_ES_API:
|
2013-12-13 13:19:25 +00:00
|
|
|
#ifndef EGL_OPENGL_ES3_BIT_KHR
|
|
|
|
if (major >= 3)
|
|
|
|
return false;
|
|
|
|
#endif
|
2012-09-24 23:26:22 +00:00
|
|
|
return eglBindAPI(EGL_OPENGL_ES_API);
|
|
|
|
case GFX_CTX_OPENVG_API:
|
|
|
|
return eglBindAPI(EGL_OPENVG_API);
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-19 14:06:12 +00:00
|
|
|
static void gfx_ctx_bind_hw_render(void *data, bool enable)
|
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
g_use_hw_ctx = enable;
|
|
|
|
if (g_egl_dpy)
|
|
|
|
eglMakeCurrent(g_egl_dpy, g_egl_surf, g_egl_surf, enable ? g_egl_hw_ctx : g_egl_ctx);
|
|
|
|
}
|
|
|
|
|
2012-09-24 23:26:22 +00:00
|
|
|
const gfx_ctx_driver_t gfx_ctx_drm_egl = {
|
|
|
|
gfx_ctx_init,
|
|
|
|
gfx_ctx_destroy,
|
|
|
|
gfx_ctx_bind_api,
|
|
|
|
gfx_ctx_swap_interval,
|
|
|
|
gfx_ctx_set_video_mode,
|
|
|
|
gfx_ctx_get_video_size,
|
2012-09-25 11:51:44 +00:00
|
|
|
NULL,
|
2012-09-24 23:26:22 +00:00
|
|
|
gfx_ctx_update_window_title,
|
|
|
|
gfx_ctx_check_window,
|
|
|
|
gfx_ctx_set_resize,
|
|
|
|
gfx_ctx_has_focus,
|
|
|
|
gfx_ctx_swap_buffers,
|
|
|
|
gfx_ctx_input_driver,
|
|
|
|
gfx_ctx_get_proc_address,
|
2014-03-09 15:50:18 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2013-03-29 12:46:11 +00:00
|
|
|
NULL,
|
2013-02-23 13:49:49 +00:00
|
|
|
"kms-egl",
|
2014-04-19 14:06:12 +00:00
|
|
|
gfx_ctx_bind_hw_render,
|
2012-09-24 23:26:22 +00:00
|
|
|
};
|
|
|
|
|