RetroArch/gfx/video_context_driver.c

452 lines
12 KiB
C
Raw Normal View History

2012-09-24 23:26:22 +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) 2011-2015 - Daniel De Matteis
2012-09-24 23:26:22 +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 "../general.h"
#include "video_context_driver.h"
#include <string.h>
2012-09-24 23:26:22 +00:00
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
static const gfx_ctx_driver_t *gfx_ctx_drivers[] = {
#if defined(__CELLOS_LV2__)
&gfx_ctx_ps3,
#endif
#if defined(HAVE_D3D)
2014-09-14 04:45:47 +00:00
&gfx_ctx_d3d,
#endif
2012-09-24 23:26:22 +00:00
#if defined(HAVE_VIDEOCORE)
&gfx_ctx_videocore,
#endif
2014-06-05 10:28:17 +00:00
#if defined(HAVE_MALI_FBDEV)
&gfx_ctx_mali_fbdev,
#endif
2014-07-27 20:19:11 +00:00
#if defined(HAVE_VIVANTE_FBDEV)
&gfx_ctx_vivante_fbdev,
#endif
2012-09-30 09:26:26 +00:00
#if defined(_WIN32) && defined(HAVE_OPENGL)
&gfx_ctx_wgl,
#endif
#if defined(HAVE_X11) && defined(HAVE_OPENGL) && !defined(HAVE_OPENGLES)
2012-09-29 09:59:08 +00:00
&gfx_ctx_glx,
2012-09-24 23:26:22 +00:00
#endif
#if defined(HAVE_WAYLAND) && defined(HAVE_OPENGL) && defined(HAVE_EGL)
&gfx_ctx_wayland,
#endif
2012-09-29 09:59:08 +00:00
#if defined(HAVE_X11) && defined(HAVE_OPENGL) && defined(HAVE_EGL)
2012-09-24 23:26:22 +00:00
&gfx_ctx_x_egl,
#endif
#if defined(HAVE_KMS)
&gfx_ctx_drm_egl,
#endif
2012-10-08 22:11:11 +00: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 01:59:55 +00:00
#if defined(__APPLE__) && !defined(TARGET_IPHONE_SIMULATOR) && !defined(TARGET_OS_IPHONE)
2015-04-26 01:48:35 +00:00
&gfx_ctx_cgl,
#endif
#if (defined(HAVE_SDL) || defined(HAVE_SDL2)) && defined(HAVE_OPENGL)
2014-08-21 01:09:30 +00:00
&gfx_ctx_sdl_gl,
#endif
#ifdef EMSCRIPTEN
&gfx_ctx_emscripten,
#endif
2014-10-07 12:31:10 +00:00
&gfx_ctx_null,
NULL
2012-09-24 23:26:22 +00:00
};
2015-04-10 07:15:25 +00:00
static const gfx_ctx_driver_t *gfx_ctx_get_ptr(void)
{
driver_t *driver = driver_get_ptr();
if (!driver)
return NULL;
return (const gfx_ctx_driver_t*)driver->video_context;
}
void gfx_ctx_free(void *data)
{
const gfx_ctx_driver_t *ctx = gfx_ctx_get_ptr();
if (ctx && ctx->destroy)
ctx->destroy(data);
ctx = NULL;
}
2015-04-16 18:00:10 +00:00
const char *gfx_ctx_get_ident(void)
{
const gfx_ctx_driver_t *ctx = gfx_ctx_get_ptr();
if (!ctx)
return NULL;
return ctx->ident;
}
void gfx_ctx_update_window_title(void *data)
{
const gfx_ctx_driver_t *ctx = gfx_ctx_get_ptr();
if (ctx->update_window_title)
ctx->update_window_title(data);
}
void gfx_ctx_get_video_output_size(void *data,
unsigned *width, unsigned *height)
{
const gfx_ctx_driver_t *ctx = gfx_ctx_get_ptr();
if (ctx->get_video_output_size)
ctx->get_video_output_size(data, width, height);
}
void gfx_ctx_get_video_output_prev(void *data)
{
const gfx_ctx_driver_t *ctx = gfx_ctx_get_ptr();
if (ctx->get_video_output_prev)
ctx->get_video_output_prev(data);
}
void gfx_ctx_get_video_output_next(void *data)
{
const gfx_ctx_driver_t *ctx = gfx_ctx_get_ptr();
if (ctx->get_video_output_next)
ctx->get_video_output_next(data);
}
void gfx_ctx_swap_buffers(void *data)
{
const gfx_ctx_driver_t *ctx = gfx_ctx_get_ptr();
if (ctx->swap_buffers)
ctx->swap_buffers(data);
}
2015-04-10 07:07:53 +00:00
void gfx_ctx_bind_hw_render(void *data, bool enable)
{
const gfx_ctx_driver_t *ctx = gfx_ctx_get_ptr();
if (ctx && ctx->bind_hw_render)
ctx->bind_hw_render(data, enable);
}
bool gfx_ctx_focus(void *data)
{
const gfx_ctx_driver_t *ctx = gfx_ctx_get_ptr();
if (data && ctx && ctx->has_focus)
return ctx->has_focus(data);
return false;
}
2015-04-10 07:05:07 +00:00
bool gfx_ctx_set_video_mode(void *data,
unsigned width, unsigned height,
bool fullscreen)
{
const gfx_ctx_driver_t *ctx = gfx_ctx_get_ptr();
if (ctx && ctx->set_video_mode)
2015-04-10 07:05:07 +00:00
return ctx->set_video_mode(data, width, height, fullscreen);
return false;
}
void gfx_ctx_translate_aspect(void *data, float *aspect,
unsigned width, unsigned height)
{
const gfx_ctx_driver_t *ctx = gfx_ctx_get_ptr();
if (ctx && ctx->translate_aspect)
*aspect = ctx->translate_aspect(data, width, height);
}
2015-04-09 16:55:22 +00:00
bool gfx_ctx_get_metrics(enum display_metric_types type, float *value)
{
driver_t *driver = driver_get_ptr();
const gfx_ctx_driver_t *ctx = gfx_ctx_get_ptr();
if (!ctx || !driver)
return false;
if (!ctx->get_metrics)
return false;
2015-04-09 16:55:22 +00:00
return ctx->get_metrics(driver->video_context_data, type,
value);
}
2015-04-10 07:02:24 +00:00
bool gfx_ctx_image_buffer_init(void *data, const video_info_t* info)
{
const gfx_ctx_driver_t *ctx = gfx_ctx_get_ptr();
if (ctx && ctx->image_buffer_init)
return ctx->image_buffer_init(data, info);
return false;
2015-04-10 07:02:24 +00:00
}
bool gfx_ctx_image_buffer_write(void *data, const void *frame, unsigned width,
2015-04-10 03:47:36 +00:00
unsigned height, unsigned pitch, bool rgb32,
unsigned index, void **image_handle)
{
const gfx_ctx_driver_t *ctx = gfx_ctx_get_ptr();
2015-04-10 07:02:24 +00:00
if (ctx && ctx->image_buffer_write)
return ctx->image_buffer_write(data, frame, width, height, pitch,
2015-04-10 03:47:36 +00:00
rgb32, index, image_handle);
return false;
}
retro_proc_address_t gfx_ctx_get_proc_address(const char *sym)
{
const gfx_ctx_driver_t *ctx = gfx_ctx_get_ptr();
return ctx->get_proc_address(sym);
}
void gfx_ctx_show_mouse(void *data, bool state)
{
const gfx_ctx_driver_t *ctx = gfx_ctx_get_ptr();
if (data && ctx->show_mouse)
ctx->show_mouse(data, state);
}
bool gfx_ctx_has_windowed(void *data)
{
const gfx_ctx_driver_t *ctx = gfx_ctx_get_ptr();
if (data && ctx)
return ctx->has_windowed(data);
return true;
}
2015-04-10 04:10:05 +00:00
bool gfx_ctx_check_window(void *data, bool *quit, bool *resize,
unsigned *width, unsigned *height)
{
const gfx_ctx_driver_t *ctx = gfx_ctx_get_ptr();
2015-08-03 21:01:07 +00:00
uint64_t *frame_count = video_driver_get_frame_count();
2015-04-10 04:10:05 +00:00
if (!data)
return false;
ctx->check_window(data, quit, resize, width, height,
2015-08-03 21:01:07 +00:00
(unsigned int)*frame_count);
2015-04-10 04:10:05 +00:00
return true;
}
2015-04-10 04:13:20 +00:00
bool gfx_ctx_suppress_screensaver(void *data, bool enable)
{
const gfx_ctx_driver_t *ctx = gfx_ctx_get_ptr();
if (data && ctx)
return ctx->suppress_screensaver(data, enable);
return false;
}
2015-04-10 05:12:15 +00:00
void gfx_ctx_get_video_size(void *data,
unsigned *width, unsigned *height)
{
const gfx_ctx_driver_t *ctx = gfx_ctx_get_ptr();
if (ctx)
ctx->get_video_size(data, width, height);
}
void gfx_ctx_swap_interval(void *data, unsigned interval)
{
const gfx_ctx_driver_t *ctx = gfx_ctx_get_ptr();
if (ctx)
ctx->swap_interval(data, interval);
}
void gfx_ctx_set_resize(void *data, unsigned width, unsigned height)
{
const gfx_ctx_driver_t *ctx = gfx_ctx_get_ptr();
if (ctx)
ctx->set_resize(data, width, height);
}
2015-04-10 06:53:45 +00:00
void gfx_ctx_input_driver(void *data,
const input_driver_t **input, void **input_data)
{
const gfx_ctx_driver_t *ctx = gfx_ctx_get_ptr();
if (ctx)
ctx->input_driver(data, input, input_data);
}
2015-01-09 22:32:32 +00: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-24 23:26:22 +00:00
{
unsigned i;
for (i = 0; gfx_ctx_drivers[i]; i++)
2015-01-09 22:32:32 +00:00
if (strcasecmp(ident, gfx_ctx_drivers[i]->ident) == 0)
return i;
return -1;
}
2012-09-24 23:26:22 +00:00
2015-01-09 22:32:32 +00:00
/**
* find_prev_context_driver:
*
* Finds previous driver in graphics context driver array.
**/
void find_prev_gfx_context_driver(void)
{
2015-03-20 19:43:22 +00:00
settings_t *settings = config_get_ptr();
int i = find_gfx_ctx_driver_index(settings->video.context_driver);
2015-01-09 22:32:32 +00:00
if (i > 0)
{
2015-03-20 19:43:22 +00:00
strlcpy(settings->video.context_driver, gfx_ctx_drivers[i - 1]->ident,
sizeof(settings->video.context_driver));
}
else
RARCH_WARN("Couldn't find any previous video context driver.\n");
}
2015-01-09 22:32:32 +00:00
/**
* find_next_context_driver:
*
* Finds next driver in graphics context driver array.
**/
void find_next_context_driver(void)
{
2015-03-20 19:43:22 +00:00
settings_t *settings = config_get_ptr();
int i = find_gfx_ctx_driver_index(settings->video.context_driver);
2015-01-09 22:32:32 +00:00
if (i >= 0 && gfx_ctx_drivers[i + 1])
{
2015-03-20 19:43:22 +00:00
strlcpy(settings->video.context_driver, gfx_ctx_drivers[i + 1]->ident,
sizeof(settings->video.context_driver));
}
else
RARCH_WARN("Couldn't find any next video context driver.\n");
2012-09-24 23:26:22 +00:00
}
2015-01-09 22:32:32 +00: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 21:09:37 +00:00
const gfx_ctx_driver_t *ctx,
2015-01-09 22:32:32 +00:00
const char *ident,
2014-10-23 21:09:37 +00:00
enum gfx_ctx_api api, unsigned major,
unsigned minor, bool hw_render_ctx)
{
2015-03-20 19:43:22 +00:00
settings_t *settings = config_get_ptr();
2014-10-23 21:09:37 +00:00
if (ctx->bind_api(data, api, major, minor))
{
bool initialized = ctx->init(data);
if (!initialized)
return NULL;
2014-10-23 21:09:37 +00:00
if (ctx->bind_hw_render)
ctx->bind_hw_render(data,
2015-03-20 19:43:22 +00:00
settings->video.shared_context && hw_render_ctx);
2014-10-23 21:09:37 +00:00
return ctx;
2014-10-23 21:09:37 +00:00
}
2015-01-09 22:32:32 +00:00
#ifndef _WIN32
RARCH_WARN("Failed to bind API (#%u, version %u.%u) on context driver \"%s\".\n",
2015-01-09 22:32:32 +00:00
(unsigned)api, major, minor, ctx->ident);
#endif
2014-10-23 21:09:37 +00:00
return NULL;
}
2015-01-09 22:32:32 +00: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 19:20:34 +00:00
* Finds graphics context driver and initializes.
2015-01-09 22:32:32 +00: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 15:57:18 +00:00
enum gfx_ctx_api api, unsigned major,
unsigned minor, bool hw_render_ctx)
2012-09-24 23:26:22 +00:00
{
int i = find_gfx_ctx_driver_index(ident);
if (i >= 0)
2015-01-09 22:32:32 +00:00
return gfx_ctx_init(data, gfx_ctx_drivers[i], ident,
api, major, minor, hw_render_ctx);
2015-01-09 22:32:32 +00:00
for (i = 0; gfx_ctx_drivers[i]; i++)
2012-09-24 23:26:22 +00:00
{
2015-09-29 15:35:28 +00:00
const gfx_ctx_driver_t *ctx =
gfx_ctx_init(data, gfx_ctx_drivers[i], ident,
2015-01-09 22:32:32 +00:00
api, major, minor, hw_render_ctx);
2015-09-29 15:35:28 +00:00
2015-01-09 22:32:32 +00:00
if (ctx)
return ctx;
}
2015-01-09 22:32:32 +00:00
return NULL;
2012-09-24 23:26:22 +00:00
}
2015-01-09 22:32:32 +00: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 22:32:32 +00:00
const char *ident, enum gfx_ctx_api api, unsigned major,
unsigned minor, bool hw_render_ctx)
{
2015-01-09 22:32:32 +00:00
return gfx_ctx_find_driver(data, ident, api, major, minor, hw_render_ctx);
}