RetroArch/gfx/video_context_driver.c

487 lines
16 KiB
C
Raw Normal View History

2012-09-25 01:26:22 +02:00
/* RetroArch - A frontend for libretro.
2014-01-01 01:50:59 +01:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2016-01-10 04:41:52 +01:00
* Copyright (C) 2011-2016 - Daniel De Matteis
2012-09-25 01:26:22 +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 <string.h>
2012-09-25 01:26:22 +02:00
#include <string/stdstring.h>
2015-11-23 12:03:38 +01:00
#include "video_context_driver.h"
2012-09-25 01:26:22 +02:00
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
2015-11-23 12:03:38 +01:00
#include "../general.h"
#include "../verbosity.h"
2012-09-25 01:26:22 +02:00
static const gfx_ctx_driver_t *gfx_ctx_drivers[] = {
#if defined(__CELLOS_LV2__)
&gfx_ctx_ps3,
#endif
#if defined(HAVE_D3D)
2014-09-14 06:45:47 +02:00
&gfx_ctx_d3d,
#endif
2012-09-25 01:26:22 +02:00
#if defined(HAVE_VIDEOCORE)
&gfx_ctx_videocore,
#endif
2014-06-05 12:28:17 +02:00
#if defined(HAVE_MALI_FBDEV)
&gfx_ctx_mali_fbdev,
#endif
2014-07-27 22:19:11 +02:00
#if defined(HAVE_VIVANTE_FBDEV)
&gfx_ctx_vivante_fbdev,
#endif
#if defined(HAVE_OPENDINGUX_FBDEV)
&gfx_ctx_opendingux_fbdev,
#endif
2012-09-30 11:26:26 +02:00
#if defined(_WIN32) && defined(HAVE_OPENGL)
&gfx_ctx_wgl,
#endif
#if defined(HAVE_WAYLAND)
&gfx_ctx_wayland,
#endif
#if defined(HAVE_X11) && !defined(HAVE_OPENGLES)
#if defined(HAVE_OPENGL) || defined(HAVE_VULKAN)
2016-02-20 05:48:45 +01:00
&gfx_ctx_x,
2012-09-25 01:26:22 +02:00
#endif
#endif
2012-09-29 11:59:08 +02:00
#if defined(HAVE_X11) && defined(HAVE_OPENGL) && defined(HAVE_EGL)
2012-09-25 01:26:22 +02:00
&gfx_ctx_x_egl,
#endif
#if defined(HAVE_KMS)
2016-02-22 12:59:13 +01:00
&gfx_ctx_drm,
2012-09-25 01:26:22 +02:00
#endif
2012-10-09 00:11:11 +02:00
#if defined(ANDROID)
&gfx_ctx_android,
#endif
#if defined(__QNX__)
&gfx_ctx_bbqnx,
#endif
#if defined(HAVE_COCOA) || defined(HAVE_COCOATOUCH)
&gfx_ctx_cocoagl,
#endif
2015-04-26 03:59:55 +02:00
#if defined(__APPLE__) && !defined(TARGET_IPHONE_SIMULATOR) && !defined(TARGET_OS_IPHONE)
2015-04-26 03:48:35 +02:00
&gfx_ctx_cgl,
#endif
#if (defined(HAVE_SDL) || defined(HAVE_SDL2)) && defined(HAVE_OPENGL)
2014-08-20 22:09:30 -03:00
&gfx_ctx_sdl_gl,
#endif
#ifdef EMSCRIPTEN
&gfx_ctx_emscripten,
#endif
2014-10-07 14:31:10 +02:00
&gfx_ctx_null,
NULL
2012-09-25 01:26:22 +02:00
};
2015-01-09 23:32:32 +01:00
/**
* find_gfx_ctx_driver_index:
* @ident : Identifier of resampler driver to find.
*
* Finds graphics context driver index by @ident name.
*
* Returns: graphics context driver index if driver was found, otherwise
* -1.
**/
static int find_gfx_ctx_driver_index(const char *ident)
2012-09-25 01:26:22 +02:00
{
unsigned i;
for (i = 0; gfx_ctx_drivers[i]; i++)
if (string_is_equal_noncase(ident, gfx_ctx_drivers[i]->ident))
return i;
return -1;
}
2012-09-25 01:26:22 +02:00
2015-01-09 23:32:32 +01:00
/**
* find_prev_context_driver:
*
* Finds previous driver in graphics context driver array.
**/
static bool gfx_ctl_find_prev_driver(void)
{
2015-03-20 20:43:22 +01:00
settings_t *settings = config_get_ptr();
2016-02-13 23:20:04 +01:00
int i = find_gfx_ctx_driver_index(
settings->video.context_driver);
2015-01-09 23:32:32 +01:00
if (i > 0)
{
2016-02-13 23:20:04 +01:00
strlcpy(settings->video.context_driver,
gfx_ctx_drivers[i - 1]->ident,
2015-03-20 20:43:22 +01:00
sizeof(settings->video.context_driver));
return true;
}
RARCH_WARN("Couldn't find any previous video context driver.\n");
return false;
}
2015-01-09 23:32:32 +01:00
/**
* find_next_context_driver:
*
* Finds next driver in graphics context driver array.
**/
static bool gfx_ctl_find_next_driver(void)
{
2015-03-20 20:43:22 +01:00
settings_t *settings = config_get_ptr();
int i = find_gfx_ctx_driver_index(settings->video.context_driver);
2015-01-09 23:32:32 +01:00
if (i >= 0 && gfx_ctx_drivers[i + 1])
{
2016-02-13 23:20:04 +01:00
strlcpy(settings->video.context_driver,
gfx_ctx_drivers[i + 1]->ident,
2015-03-20 20:43:22 +01:00
sizeof(settings->video.context_driver));
return true;
}
RARCH_WARN("Couldn't find any next video context driver.\n");
return false;
2012-09-25 01:26:22 +02:00
}
2015-01-09 23:32:32 +01:00
/**
* gfx_ctx_init:
* @data : Input data.
* @ctx : Graphics context driver to initialize.
* @ident : Identifier of graphics context driver to find.
* @api : API of higher-level graphics API.
* @major : Major version number of higher-level graphics API.
* @minor : Minor version number of higher-level graphics API.
* @hw_render_ctx : Request a graphics context driver capable of
* hardware rendering?
*
* Initialize graphics context driver.
*
* Returns: graphics context driver if successfully initialized, otherwise NULL.
**/
static const gfx_ctx_driver_t *gfx_ctx_init(void *data,
2014-10-23 23:09:37 +02:00
const gfx_ctx_driver_t *ctx,
2015-01-09 23:32:32 +01:00
const char *ident,
2014-10-23 23:09:37 +02:00
enum gfx_ctx_api api, unsigned major,
unsigned minor, bool hw_render_ctx)
{
2015-03-20 20:43:22 +01:00
settings_t *settings = config_get_ptr();
2014-10-23 23:09:37 +02:00
if (ctx->bind_api(data, api, major, minor))
{
void *ctx_data = ctx->init(data);
if (!ctx_data)
return NULL;
2014-10-23 23:09:37 +02:00
if (ctx->bind_hw_render)
ctx->bind_hw_render(ctx_data,
2015-03-20 20:43:22 +01:00
settings->video.shared_context && hw_render_ctx);
2014-10-23 23:09:37 +02:00
gfx_ctx_ctl(GFX_CTL_SET_VIDEO_CONTEXT_DATA, ctx_data);
return ctx;
2014-10-23 23:09:37 +02:00
}
2015-01-09 23:32:32 +01:00
#ifndef _WIN32
RARCH_WARN("Failed to bind API (#%u, version %u.%u) on context driver \"%s\".\n",
2015-01-09 23:32:32 +01:00
(unsigned)api, major, minor, ctx->ident);
#endif
2014-10-23 23:09:37 +02:00
return NULL;
}
2015-01-09 23:32:32 +01:00
/**
* gfx_ctx_find_driver:
* @data : Input data.
* @ident : Identifier of graphics context driver to find.
* @api : API of higher-level graphics API.
* @major : Major version number of higher-level graphics API.
* @minor : Minor version number of higher-level graphics API.
* @hw_render_ctx : Request a graphics context driver capable of
* hardware rendering?
*
2015-01-11 20:20:34 +01:00
* Finds graphics context driver and initializes.
2015-01-09 23:32:32 +01:00
*
* Returns: graphics context driver if found, otherwise NULL.
**/
static const gfx_ctx_driver_t *gfx_ctx_find_driver(void *data,
const char *ident,
2014-09-08 17:57:18 +02:00
enum gfx_ctx_api api, unsigned major,
unsigned minor, bool hw_render_ctx)
2012-09-25 01:26:22 +02:00
{
int i = find_gfx_ctx_driver_index(ident);
if (i >= 0)
2015-01-09 23:32:32 +01:00
return gfx_ctx_init(data, gfx_ctx_drivers[i], ident,
api, major, minor, hw_render_ctx);
2015-01-09 23:32:32 +01:00
for (i = 0; gfx_ctx_drivers[i]; i++)
2012-09-25 01:26:22 +02:00
{
2015-09-29 17:35:28 +02:00
const gfx_ctx_driver_t *ctx =
gfx_ctx_init(data, gfx_ctx_drivers[i], ident,
2015-01-09 23:32:32 +01:00
api, major, minor, hw_render_ctx);
2015-09-29 17:35:28 +02:00
2015-01-09 23:32:32 +01:00
if (ctx)
return ctx;
}
2015-01-09 23:32:32 +01:00
return NULL;
2012-09-25 01:26:22 +02:00
}
2015-01-09 23:32:32 +01:00
/**
* gfx_ctx_init_first:
* @data : Input data.
* @ident : Identifier of graphics context driver to find.
* @api : API of higher-level graphics API.
* @major : Major version number of higher-level graphics API.
* @minor : Minor version number of higher-level graphics API.
* @hw_render_ctx : Request a graphics context driver capable of
* hardware rendering?
*
* Finds first suitable graphics context driver and initializes.
*
* Returns: graphics context driver if found, otherwise NULL.
**/
const gfx_ctx_driver_t *gfx_ctx_init_first(void *data,
2015-01-09 23:32:32 +01:00
const char *ident, enum gfx_ctx_api api, unsigned major,
unsigned minor, bool hw_render_ctx)
{
2016-02-06 21:51:37 +01:00
return gfx_ctx_find_driver(data, ident, api,
major, minor, hw_render_ctx);
}
2016-02-13 08:21:35 +01:00
bool gfx_ctx_ctl(enum gfx_ctx_ctl_state state, void *data)
{
static const gfx_ctx_driver_t *current_video_context = NULL;
static void *video_context_data = NULL;
2016-02-13 08:21:35 +01:00
switch (state)
{
2016-02-13 19:53:14 +01:00
case GFX_CTL_CHECK_WINDOW:
{
uint64_t *frame_count = NULL;
gfx_ctx_size_t *size_data = (gfx_ctx_size_t*)data;
video_driver_ctl(RARCH_DISPLAY_CTL_GET_FRAME_COUNT, &frame_count);
if (!video_context_data || !size_data)
return false;
if (!current_video_context || !current_video_context->check_window)
2016-02-13 19:53:14 +01:00
return false;
current_video_context->check_window(video_context_data,
size_data->quit,
size_data->resize,
size_data->width,
size_data->height, (unsigned int)*frame_count);
}
break;
case GFX_CTL_FIND_PREV_DRIVER:
2016-03-20 17:41:28 +01:00
if (!gfx_ctl_find_prev_driver())
return false;
break;
case GFX_CTL_FIND_NEXT_DRIVER:
2016-03-20 17:41:28 +01:00
if (!gfx_ctl_find_next_driver())
return false;
break;
2016-02-13 18:51:16 +01:00
case GFX_CTL_IMAGE_BUFFER_INIT:
if (!current_video_context || !current_video_context->image_buffer_init)
return false;
2016-03-20 17:41:28 +01:00
if (!current_video_context->image_buffer_init(video_context_data,
(const video_info_t*)data))
return false;
break;
2016-02-13 23:35:47 +01:00
case GFX_CTL_IMAGE_BUFFER_WRITE:
{
gfx_ctx_image_t *img = (gfx_ctx_image_t*)data;
if (!current_video_context || !current_video_context->image_buffer_write)
return false;
2016-03-20 17:41:28 +01:00
if (!current_video_context->image_buffer_write(video_context_data,
2016-02-13 23:35:47 +01:00
img->frame, img->width, img->height, img->pitch,
2016-03-20 17:41:28 +01:00
img->rgb32, img->index, img->handle))
return false;
2016-02-13 23:35:47 +01:00
}
2016-03-20 17:41:28 +01:00
break;
case GFX_CTL_GET_VIDEO_OUTPUT_PREV:
if (!current_video_context
|| !current_video_context->get_video_output_prev)
return false;
current_video_context->get_video_output_prev(video_context_data);
break;
case GFX_CTL_GET_VIDEO_OUTPUT_NEXT:
if (!current_video_context ||
!current_video_context->get_video_output_next)
return false;
current_video_context->get_video_output_next(video_context_data);
break;
2016-02-13 18:36:23 +01:00
case GFX_CTL_BIND_HW_RENDER:
{
bool *enable = (bool*)data;
if (!current_video_context || !current_video_context->bind_hw_render)
return false;
current_video_context->bind_hw_render(video_context_data, *enable);
}
break;
2016-02-13 18:27:05 +01:00
case GFX_CTL_SET:
if (!data)
return false;
current_video_context = (const gfx_ctx_driver_t*)data;
break;
2016-02-13 16:33:38 +01:00
case GFX_CTL_DESTROY:
current_video_context = NULL;
break;
2016-02-13 08:50:22 +01:00
case GFX_CTL_UPDATE_WINDOW_TITLE:
if (!current_video_context || !current_video_context->update_window_title)
return false;
current_video_context->update_window_title(video_context_data);
break;
2016-02-13 08:33:33 +01:00
case GFX_CTL_SWAP_BUFFERS:
if (!current_video_context || !current_video_context->swap_buffers)
return false;
current_video_context->swap_buffers(video_context_data);
break;
2016-02-13 08:29:58 +01:00
case GFX_CTL_FOCUS:
if (!video_context_data || !current_video_context->has_focus)
return false;
2016-03-20 17:41:28 +01:00
if (!current_video_context->has_focus(video_context_data))
return false;
break;
2016-02-13 08:37:10 +01:00
case GFX_CTL_HAS_WINDOWED:
if (!video_context_data)
return false;
2016-03-20 17:41:28 +01:00
if (!current_video_context->has_windowed(video_context_data))
return false;
break;
2016-02-13 08:26:54 +01:00
case GFX_CTL_FREE:
if (current_video_context->destroy)
current_video_context->destroy(video_context_data);
current_video_context = NULL;
video_context_data = NULL;
break;
2016-02-13 19:36:02 +01:00
case GFX_CTL_GET_VIDEO_OUTPUT_SIZE:
{
gfx_ctx_size_t *size_data = (gfx_ctx_size_t*)data;
if (!size_data)
return false;
if (!current_video_context || !current_video_context->get_video_output_size)
return false;
current_video_context->get_video_output_size(video_context_data,
size_data->width, size_data->height);
}
break;
2016-02-13 20:45:45 +01:00
case GFX_CTL_SWAP_INTERVAL:
{
unsigned *interval = (unsigned*)data;
if (!current_video_context || !current_video_context->swap_interval)
return false;
current_video_context->swap_interval(video_context_data, *interval);
}
break;
2016-02-13 22:02:49 +01:00
case GFX_CTL_PROC_ADDRESS_GET:
{
gfx_ctx_proc_address_t *proc = (gfx_ctx_proc_address_t*)data;
if (!current_video_context || !current_video_context->get_proc_address)
return false;
proc->addr = current_video_context->get_proc_address(proc->sym);
}
break;
2016-02-13 22:14:33 +01:00
case GFX_CTL_GET_METRICS:
{
gfx_ctx_metrics_t *metrics = (gfx_ctx_metrics_t*)data;
if (!current_video_context || !current_video_context->get_metrics)
return false;
2016-03-20 17:41:28 +01:00
if (!current_video_context->get_metrics(video_context_data,
2016-02-13 22:14:33 +01:00
metrics->type,
2016-03-20 17:41:28 +01:00
metrics->value))
return false;
2016-02-13 22:14:33 +01:00
}
2016-03-20 17:41:28 +01:00
break;
2016-02-13 23:26:33 +01:00
case GFX_CTL_INPUT_DRIVER:
{
gfx_ctx_input_t *inp = (gfx_ctx_input_t*)data;
if (!current_video_context || !current_video_context->input_driver)
return false;
current_video_context->input_driver(
video_context_data, inp->input, inp->input_data);
}
break;
2016-02-13 23:39:12 +01:00
case GFX_CTL_SUPPRESS_SCREENSAVER:
{
bool *bool_data = (bool*)data;
if (!video_context_data || !current_video_context)
return false;
2016-03-20 17:41:28 +01:00
if (!current_video_context->suppress_screensaver(
video_context_data, *bool_data))
return false;
2016-02-13 23:39:12 +01:00
}
2016-03-20 17:41:28 +01:00
break;
2016-02-14 02:05:20 +01:00
case GFX_CTL_IDENT_GET:
{
gfx_ctx_ident_t *ident = (gfx_ctx_ident_t*)data;
ident->ident = NULL;
if (current_video_context)
ident->ident = current_video_context->ident;
}
break;
2016-02-14 02:12:18 +01:00
case GFX_CTL_SET_VIDEO_MODE:
{
gfx_ctx_mode_t *mode_info = (gfx_ctx_mode_t*)data;
if (!current_video_context || !current_video_context->set_video_mode)
return false;
2016-03-20 17:41:28 +01:00
if (!current_video_context->set_video_mode(
2016-02-14 02:12:18 +01:00
video_context_data, mode_info->width,
2016-03-20 17:41:28 +01:00
mode_info->height, mode_info->fullscreen))
return false;
2016-02-14 02:12:18 +01:00
}
2016-03-20 17:41:28 +01:00
break;
2016-02-14 02:15:43 +01:00
case GFX_CTL_SET_RESIZE:
{
gfx_ctx_mode_t *mode_info = (gfx_ctx_mode_t*)data;
if (!current_video_context)
return false;
2016-03-20 17:41:28 +01:00
if (!current_video_context->set_resize(
video_context_data, mode_info->width, mode_info->height))
return false;
2016-02-14 02:15:43 +01:00
}
2016-03-20 17:41:28 +01:00
break;
2016-02-14 02:26:20 +01:00
case GFX_CTL_GET_VIDEO_SIZE:
{
gfx_ctx_mode_t *mode_info = (gfx_ctx_mode_t*)data;
if (!current_video_context || !current_video_context->get_video_size)
return false;
current_video_context->get_video_size(video_context_data, &mode_info->width, &mode_info->height);
}
break;
2016-02-16 20:24:00 +01:00
case GFX_CTL_GET_CONTEXT_DATA:
{
if (!current_video_context || !current_video_context->get_context_data)
return false;
*(void**)data = current_video_context->get_context_data(video_context_data);
}
break;
2016-03-14 14:39:58 +01:00
case GFX_CTL_SHOW_MOUSE:
{
bool *bool_data = (bool*)data;
if (!current_video_context || !current_video_context->show_mouse)
return false;
current_video_context->show_mouse(video_context_data, *bool_data);
}
break;
case GFX_CTL_SET_VIDEO_CONTEXT_DATA:
video_context_data = data;
break;
2016-02-13 08:21:35 +01:00
case GFX_CTL_NONE:
default:
break;
}
return true;
}