RetroArch/gfx/drivers/caca_gfx.c

431 lines
10 KiB
C
Raw Normal View History

2016-12-01 12:13:36 -05:00
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2017-01-22 13:40:32 +01:00
* Copyright (C) 2011-2017 - Daniel De Matteis
2019-02-22 16:31:54 -05:00
* Copyright (C) 2016-2019 - Brad Parker
2016-12-01 12:13:36 -05: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 <caca.h>
#include <stdlib.h>
#include <retro_miscellaneous.h>
#include <string/stdstring.h>
#ifdef HAVE_CONFIG_H
#include "../../config.h"
#endif
#ifdef HAVE_MENU
2016-12-01 12:13:36 -05:00
#include "../../menu/menu_driver.h"
#endif
2023-05-31 22:46:34 +02:00
#include "../common/caca_defines.h"
2017-01-19 17:20:42 +01:00
#include "../font_driver.h"
#include "../../driver.h"
#include "../../verbosity.h"
/*
* FORWARD DECLARATIONS
*/
static void caca_free(void *data);
/*
* FONT DRIVER
*/
typedef struct
{
const font_renderer_driver_t *font_driver;
void *font_data;
caca_t *caca;
} caca_raster_t;
static void *caca_font_init(void *data,
const char *font_path, float font_size,
bool is_threaded)
{
caca_raster_t *font = (caca_raster_t*)calloc(1, sizeof(*font));
if (!font)
return NULL;
font->caca = (caca_t*)data;
if (!font_renderer_create_default(
&font->font_driver,
&font->font_data, font_path, font_size))
return NULL;
return font;
}
static void caca_font_free(void *data, bool is_threaded)
{
caca_raster_t *font = (caca_raster_t*)data;
if (!font)
return;
if (font->font_driver && font->font_data && font->font_driver->free)
font->font_driver->free(font->font_data);
free(font);
}
static int caca_font_get_message_width(void *data, const char *msg,
size_t msg_len, float scale) { return 0; }
static const struct font_glyph *caca_font_get_glyph(
void *data, uint32_t code) { return NULL; }
static void caca_font_render_msg(
void *userdata,
void *data, const char *msg,
const struct font_params *params)
{
float x, y, scale;
unsigned width, height;
unsigned newX, newY;
unsigned align;
size_t msg_len;
caca_raster_t *font = (caca_raster_t*)data;
settings_t *settings = config_get_ptr();
float video_msg_pos_x = settings->floats.video_msg_pos_x;
float video_msg_pos_y = settings->floats.video_msg_pos_y;
if (!font || string_is_empty(msg))
return;
if (params)
{
x = params->x;
y = params->y;
scale = params->scale;
align = params->text_align;
}
else
{
x = video_msg_pos_x;
y = video_msg_pos_y;
scale = 1.0f;
align = TEXT_ALIGN_LEFT;
}
if ( !font->caca
|| !font->caca->cv
|| !font->caca->display
|| !font->caca->cv
|| !font->caca->display)
return;
width = caca_get_canvas_width(font->caca->cv);
height = caca_get_canvas_height(font->caca->cv);
newY = height - (y * height * scale);
msg_len = strlen(msg);
switch (align)
{
case TEXT_ALIGN_RIGHT:
newX = (x * width * scale) - msg_len;
break;
case TEXT_ALIGN_CENTER:
newX = (x * width * scale) - (msg_len / 2);
break;
case TEXT_ALIGN_LEFT:
default:
newX = x * width * scale;
break;
}
caca_put_str(font->caca->cv, newX, newY, msg);
caca_refresh_display(font->caca->display);
}
font_renderer_t caca_font = {
caca_font_init,
caca_font_free,
caca_font_render_msg,
"caca",
caca_font_get_glyph,
NULL, /* bind_block */
NULL, /* flush */
caca_font_get_message_width,
NULL /* get_line_metrics */
};
2016-12-01 12:13:36 -05:00
/*
* VIDEO DRIVER
*/
static void caca_create(caca_t *caca)
2016-12-01 12:13:36 -05:00
{
2020-03-07 16:42:12 +01:00
caca->display = caca_create_display(NULL);
caca->cv = caca_get_canvas(caca->display);
2016-12-01 12:13:36 -05:00
2020-03-07 16:42:12 +01:00
if (!caca->video_width || !caca->video_height)
2016-12-01 12:13:36 -05:00
{
2020-03-07 16:42:12 +01:00
caca->video_width = caca_get_canvas_width(caca->cv);
caca->video_height = caca_get_canvas_height(caca->cv);
2016-12-01 12:13:36 -05:00
}
if (caca->rgb32)
2020-03-07 16:42:12 +01:00
caca->dither = caca_create_dither(32, caca->video_width,
caca->video_height, caca->video_pitch,
0x00FF0000, 0xFF00, 0xFF, 0x0);
2016-12-01 12:13:36 -05:00
else
2020-03-07 16:42:12 +01:00
caca->dither = caca_create_dither(16, caca->video_width,
caca->video_height, caca->video_pitch,
0xF800, 0x7E0, 0x1F, 0x0);
video_driver_set_size(caca->video_width, caca->video_height);
2016-12-01 12:13:36 -05:00
}
static void *caca_init(const video_info_t *video,
input_driver_t **input, void **input_data)
2016-12-01 12:13:36 -05:00
{
caca_t *caca = (caca_t*)calloc(1, sizeof(*caca));
2016-12-01 12:13:36 -05:00
if (!caca)
return NULL;
2016-12-01 12:13:36 -05:00
*input = NULL;
*input_data = NULL;
caca->video_width = video->width;
caca->video_height = video->height;
caca->rgb32 = video->rgb32;
2016-12-01 12:13:36 -05:00
if (video->rgb32)
caca->video_pitch = video->width * 4;
2016-12-01 12:13:36 -05:00
else
caca->video_pitch = video->width * 2;
2016-12-01 12:13:36 -05:00
caca_create(caca);
2016-12-01 12:13:36 -05:00
2020-03-07 16:42:12 +01:00
if (!caca->cv || !caca->dither || !caca->display)
2016-12-01 12:13:36 -05:00
{
free(caca);
return NULL;
2016-12-01 12:13:36 -05:00
}
if (video->font_enable)
font_driver_init_osd(caca, video,
false, video->is_threaded,
FONT_DRIVER_RENDER_CACA);
2016-12-01 12:13:36 -05:00
return caca;
}
static bool caca_frame(void *data, const void *frame,
2016-12-01 12:13:36 -05:00
unsigned frame_width, unsigned frame_height, uint64_t frame_count,
2017-01-18 17:41:27 +01:00
unsigned pitch, const char *msg, video_frame_info_t *video_info)
2016-12-01 12:13:36 -05:00
{
size_t len = 0;
void *buffer = NULL;
2016-12-01 12:13:36 -05:00
const void *frame_to_copy = frame;
unsigned width = 0;
unsigned height = 0;
bool draw = true;
caca_t *caca = (caca_t*)data;
2020-08-03 16:33:54 +02:00
#ifdef HAVE_MENU
2020-03-09 05:18:02 +01:00
bool menu_is_alive = video_info->menu_is_alive;
2020-08-03 16:33:54 +02:00
#endif
2016-12-01 12:13:36 -05:00
if (!frame || !frame_width || !frame_height)
return true;
if ( (caca->video_width != frame_width)
|| (caca->video_height != frame_height)
|| (caca->video_pitch != pitch))
2016-12-01 12:13:36 -05:00
{
if (frame_width > 4 && frame_height > 4)
{
caca->video_width = frame_width;
caca->video_height = frame_height;
caca->video_pitch = pitch;
caca_free(caca);
caca_create(caca);
2016-12-01 12:13:36 -05:00
}
}
2020-03-07 16:42:12 +01:00
if (!caca->cv)
2016-12-01 12:13:36 -05:00
return true;
2020-08-03 16:33:54 +02:00
#ifdef HAVE_MENU
2020-03-09 05:18:02 +01:00
if (caca->menu_frame && menu_is_alive)
2020-03-07 16:42:12 +01:00
frame_to_copy = caca->menu_frame;
2020-08-03 16:33:54 +02:00
#endif
2016-12-01 12:13:36 -05:00
2020-03-07 16:42:12 +01:00
width = caca_get_canvas_width(caca->cv);
height = caca_get_canvas_height(caca->cv);
2016-12-01 12:13:36 -05:00
if ( (frame_to_copy == frame)
&& (frame_width == 4)
&& (frame_height == 4)
&& (frame_width < width && frame_height < height))
2016-12-01 12:13:36 -05:00
draw = false;
2020-08-03 16:33:54 +02:00
#ifdef HAVE_MENU
2020-03-09 05:18:02 +01:00
if (menu_is_alive)
2017-01-21 15:39:29 -05:00
draw = false;
2020-08-03 16:33:54 +02:00
#endif
2017-01-21 15:39:29 -05:00
2020-03-07 16:42:12 +01:00
caca_clear_canvas(caca->cv);
2016-12-01 12:13:36 -05:00
#ifdef HAVE_MENU
2020-05-19 16:20:43 +02:00
menu_driver_frame(menu_is_alive, video_info);
2016-12-01 12:13:36 -05:00
#endif
if (msg)
font_driver_render_msg(data, msg, NULL, NULL);
2016-12-01 12:13:36 -05:00
if (draw)
{
2020-03-07 16:42:12 +01:00
caca_dither_bitmap(caca->cv, 0, 0,
width,
height,
2020-03-07 16:42:12 +01:00
caca->dither, frame_to_copy);
2016-12-01 12:13:36 -05:00
2020-03-07 16:42:12 +01:00
buffer = caca_export_canvas_to_memory(caca->cv, "caca", &len);
2016-12-01 12:13:36 -05:00
if (buffer)
{
if (len)
2020-03-07 16:42:12 +01:00
caca_refresh_display(caca->display);
2016-12-01 12:13:36 -05:00
free(buffer);
}
}
return true;
}
static bool caca_alive(void *data)
2016-12-01 12:13:36 -05:00
{
caca_t *caca = (caca_t*)data;
video_driver_set_size(caca->video_width, caca->video_height);
2016-12-01 12:13:36 -05:00
return true;
}
static void caca_set_nonblock_state(void *data, bool a,
2020-07-27 13:16:14 +02:00
bool b, unsigned c) { }
static bool caca_focus(void *data) { return true; }
static bool caca_suppress_screensaver(void *data, bool enable) { return false; }
static bool caca_has_windowed(void *data) { return true; }
2016-12-01 12:13:36 -05:00
static void caca_free(void *data)
2016-12-01 12:13:36 -05:00
{
2020-03-07 16:42:12 +01:00
caca_t *caca = (caca_t*)data;
if (caca->display)
caca_free_display(caca->display);
caca->display = NULL;
2016-12-01 12:13:36 -05:00
2020-03-07 16:42:12 +01:00
if (caca->dither)
caca_free_dither(caca->dither);
caca->dither = NULL;
2016-12-01 12:13:36 -05:00
2020-03-07 16:42:12 +01:00
if (caca->menu_frame)
free(caca->menu_frame);
caca->menu_frame = NULL;
2016-12-01 12:13:36 -05:00
}
static bool caca_set_shader(void *data,
enum rarch_shader_type type, const char *path) { return false; }
static void caca_set_rotation(void *a, unsigned b) { }
2016-12-01 12:13:36 -05:00
static void caca_set_texture_frame(void *data,
const void *frame, bool rgb32, unsigned width, unsigned height,
float alpha)
{
caca_t *caca = (caca_t*)data;
2016-12-01 12:13:36 -05:00
unsigned pitch = width * 2;
if (rgb32)
pitch = width * 4;
2020-03-07 16:42:12 +01:00
if (caca->menu_frame)
free(caca->menu_frame);
caca->menu_frame = NULL;
2016-12-01 12:13:36 -05:00
if ( (!caca->menu_frame)
|| (caca->menu_width != width)
|| (caca->menu_height != height)
|| (caca->menu_pitch != pitch))
2020-03-07 16:42:12 +01:00
{
2016-12-01 12:13:36 -05:00
if (pitch && height)
2020-03-07 16:42:12 +01:00
caca->menu_frame = (unsigned char*)malloc(pitch * height);
}
2016-12-01 12:13:36 -05:00
2020-03-07 16:42:12 +01:00
if (caca->menu_frame && frame && pitch && height)
memcpy(caca->menu_frame, frame, pitch * height);
2016-12-01 12:13:36 -05:00
}
static const video_poke_interface_t caca_poke_interface = {
2020-07-27 10:15:28 +02:00
NULL, /* get_flags */
2016-12-01 12:13:36 -05:00
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
2016-12-01 12:13:36 -05:00
caca_set_texture_frame,
NULL,
font_driver_render_msg,
2018-01-30 22:29:57 +01:00
NULL, /* show_mouse */
NULL, /* grab_mouse_toggle */
NULL, /* get_current_shader */
NULL, /* get_current_software_framebuffer */
NULL, /* get_hw_render_interface */
Add HDR support for D3D12 (rebased PR from MajorPainTheCactus) (#12917) * Add HDR support * Attempt to fix Mingw build and Metal builds * (D3D12) Fix relative header includes * Add missing hdr_sm5.hlsl.h * (d3d12_common.c) Some C89 build fixes * Fix MSVC build * - Attempt to fix build on mingw/msys unix with dirty hack - Fix shader compilation of hdr_sm5.hlsl.h on MSVC/Visual Studio - the define was seen as an error and was causing the first pipeline to error out - Make sure we manually set handle of backBuffer to NULL * Moving the release of the texture above the freeing of desc.srv_heap and desc.rtv_heap solves the hard crashes on teardown/setup in RA - it was crashing hard in d3d12_release_texture before * Add HAVE_D3D12_HDR ifdef - needs to be disabled for WinRT for now because of several things that are Windows desktop-specific right now (GetWindowRect) * Add dirty GUID hack - should work for both mingw/msys on Windows/Linux as well as MSVC/Visual Studio (hopefully) * Change HAVE_D3D12_HDR to HAVE_DXGI_HDR * Move away from camelcase named variables * Fix RARCH_ERR logs - they need a newline at the end * d3d12_check_display_hdr_support - make it return a bool on return and set d3d12->hdr.support and d3d12->hdr.enable outside of the function * (DXGI) Remove D3D12 dependencies from dxgi_check_display_hdr_support and move it to dxgi_common.c instead * (DXGI) move d3d12_swapchain_color_space over to dxgi_common.c and rename it dxgi_swapchain_color_space * (DXGI) move d3d12_set_hdr_metadata to dxgi_common.c and rename it dxgi_set_hdr_metadata * (DXGI) dxgi_check_display_hdr_support - better error handling? * Fix typo * Remove video_force_resolution * (D3D12) Address TODO/FIXME * (D3D12) Backport https://github.com/libretro/RetroArch/pull/12916/commits/c1b6c0bff2aa33cde035b43cb31ac7e78ff2a07a - Fixed resource transition for present when HDR is off Fixed cel shader displaying all black as blending was enabled when the hdr shader was being applied - turned off blending during this shader * Move d3d12_hdr_uniform_t to dxgi_common.h and rename it dxgi_hdr_uniform_t * (D3D11) Add HDR support * Add TODO/FIXME notes * Cache hdr_enable in video_frame_info_t * Update comment
2021-09-03 06:15:25 +02:00
NULL, /* set_hdr_max_nits */
NULL, /* set_hdr_paper_white_nits */
NULL, /* set_hdr_contrast */
NULL /* set_hdr_expand_gamut */
2016-12-01 12:13:36 -05:00
};
static void caca_get_poke_interface(void *data,
const video_poke_interface_t **iface) { *iface = &caca_poke_interface; }
static void caca_set_viewport(void *data, unsigned viewport_width,
unsigned viewport_height, bool force_full, bool allow_rotate) { }
2016-12-01 12:13:36 -05:00
video_driver_t video_caca = {
caca_init,
caca_frame,
caca_set_nonblock_state,
caca_alive,
caca_focus,
caca_suppress_screensaver,
caca_has_windowed,
caca_set_shader,
caca_free,
2016-12-01 12:13:36 -05:00
"caca",
caca_set_viewport,
caca_set_rotation,
NULL, /* viewport_info */
NULL, /* read_viewport */
2016-12-01 12:13:36 -05:00
NULL, /* read_frame_raw */
#ifdef HAVE_OVERLAY
NULL, /* overlay_interface */
#endif
caca_get_poke_interface,
2018-07-12 16:55:08 -04:00
NULL /* wrap_type_to_enum */
2016-12-01 12:13:36 -05:00
};