2020-06-08 16:44:42 +00:00
|
|
|
#include "libretro_host_display.h"
|
2020-10-21 15:25:33 +00:00
|
|
|
#include "common/align.h"
|
2020-06-08 16:44:42 +00:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/log.h"
|
|
|
|
#include "libretro.h"
|
2020-06-29 16:47:27 +00:00
|
|
|
#include "libretro_host_interface.h"
|
2020-06-08 16:44:42 +00:00
|
|
|
#include <array>
|
|
|
|
#include <tuple>
|
|
|
|
Log_SetChannel(LibretroHostDisplay);
|
|
|
|
|
2020-10-21 15:25:33 +00:00
|
|
|
static retro_pixel_format GetRetroPixelFormat(HostDisplayPixelFormat format)
|
2020-06-08 16:44:42 +00:00
|
|
|
{
|
2020-10-21 15:25:33 +00:00
|
|
|
switch (format)
|
2020-06-08 16:44:42 +00:00
|
|
|
{
|
2020-10-21 15:25:33 +00:00
|
|
|
case HostDisplayPixelFormat::BGRA8:
|
|
|
|
return RETRO_PIXEL_FORMAT_XRGB8888;
|
2020-06-08 16:44:42 +00:00
|
|
|
|
2020-10-21 15:25:33 +00:00
|
|
|
case HostDisplayPixelFormat::RGB565:
|
|
|
|
return RETRO_PIXEL_FORMAT_RGB565;
|
2020-06-08 16:44:42 +00:00
|
|
|
|
2020-10-21 15:25:33 +00:00
|
|
|
case HostDisplayPixelFormat::RGBA5551:
|
|
|
|
return RETRO_PIXEL_FORMAT_0RGB1555;
|
2020-06-08 16:44:42 +00:00
|
|
|
|
2020-10-21 15:25:33 +00:00
|
|
|
default:
|
|
|
|
return RETRO_PIXEL_FORMAT_UNKNOWN;
|
2020-06-08 16:44:42 +00:00
|
|
|
}
|
2020-10-21 15:25:33 +00:00
|
|
|
}
|
2020-06-08 16:44:42 +00:00
|
|
|
|
|
|
|
LibretroHostDisplay::LibretroHostDisplay()
|
|
|
|
{
|
2020-10-21 15:25:33 +00:00
|
|
|
retro_pixel_format pf = RETRO_PIXEL_FORMAT_RGB565;
|
2020-06-08 16:44:42 +00:00
|
|
|
if (!g_retro_environment_callback(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &pf))
|
2020-10-21 15:25:33 +00:00
|
|
|
Log_ErrorPrint("Failed to set pixel format to RGB565");
|
|
|
|
else
|
|
|
|
m_current_pixel_format = pf;
|
2020-06-08 16:44:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LibretroHostDisplay::~LibretroHostDisplay() = default;
|
|
|
|
|
2020-10-21 15:25:33 +00:00
|
|
|
bool LibretroHostDisplay::CheckPixelFormat(retro_pixel_format new_format)
|
|
|
|
{
|
|
|
|
if (new_format == RETRO_PIXEL_FORMAT_UNKNOWN || m_current_pixel_format == new_format)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!g_retro_environment_callback(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &new_format))
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("g_retro_environment_callback(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, %u) failed",
|
|
|
|
static_cast<unsigned>(new_format));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!g_libretro_host_interface.UpdateSystemAVInfo(false))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_current_pixel_format = new_format;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-08 16:44:42 +00:00
|
|
|
HostDisplay::RenderAPI LibretroHostDisplay::GetRenderAPI() const
|
|
|
|
{
|
|
|
|
return RenderAPI::None;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* LibretroHostDisplay::GetRenderDevice() const
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* LibretroHostDisplay::GetRenderContext() const
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-06-29 16:47:27 +00:00
|
|
|
bool LibretroHostDisplay::HasRenderDevice() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LibretroHostDisplay::HasRenderSurface() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LibretroHostDisplay::CreateRenderDevice(const WindowInfo& wi, std::string_view adapter_name, bool debug_device)
|
|
|
|
{
|
|
|
|
m_window_info = wi;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LibretroHostDisplay::InitializeRenderDevice(std::string_view shader_cache_directory, bool debug_device)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LibretroHostDisplay::MakeRenderContextCurrent()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LibretroHostDisplay::DoneRenderContextCurrent()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LibretroHostDisplay::DestroyRenderDevice() {}
|
|
|
|
|
|
|
|
void LibretroHostDisplay::DestroyRenderSurface() {}
|
|
|
|
|
2020-08-23 04:21:06 +00:00
|
|
|
bool LibretroHostDisplay::CreateResources()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LibretroHostDisplay::DestroyResources() {}
|
|
|
|
|
2020-06-29 16:47:27 +00:00
|
|
|
bool LibretroHostDisplay::ChangeRenderWindow(const WindowInfo& wi)
|
|
|
|
{
|
|
|
|
m_window_info = wi;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LibretroHostDisplay::ResizeRenderWindow(s32 new_window_width, s32 new_window_height)
|
|
|
|
{
|
|
|
|
m_window_info.surface_width = new_window_width;
|
|
|
|
m_window_info.surface_height = new_window_height;
|
|
|
|
}
|
2020-06-08 16:44:42 +00:00
|
|
|
|
2020-11-02 09:52:01 +00:00
|
|
|
bool LibretroHostDisplay::SupportsFullscreen() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-01 14:38:54 +00:00
|
|
|
bool LibretroHostDisplay::IsFullscreen()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LibretroHostDisplay::SetFullscreen(bool fullscreen, u32 width, u32 height, float refresh_rate)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-12 15:19:57 +00:00
|
|
|
bool LibretroHostDisplay::SetPostProcessingChain(const std::string_view& config)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-08 16:44:42 +00:00
|
|
|
std::unique_ptr<HostDisplayTexture> LibretroHostDisplay::CreateTexture(u32 width, u32 height, const void* data,
|
|
|
|
u32 data_stride, bool dynamic)
|
|
|
|
{
|
2020-10-21 15:25:33 +00:00
|
|
|
return nullptr;
|
2020-06-08 16:44:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LibretroHostDisplay::UpdateTexture(HostDisplayTexture* texture, u32 x, u32 y, u32 width, u32 height,
|
|
|
|
const void* data, u32 data_stride)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LibretroHostDisplay::DownloadTexture(const void* texture_handle, u32 x, u32 y, u32 width, u32 height,
|
|
|
|
void* out_data, u32 out_data_stride)
|
|
|
|
{
|
2020-10-21 15:25:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LibretroHostDisplay::SupportsDisplayPixelFormat(HostDisplayPixelFormat format) const
|
|
|
|
{
|
|
|
|
// For when we can change the pixel format.
|
|
|
|
// return (GetRetroPixelFormat(format) != RETRO_PIXEL_FORMAT_UNKNOWN);
|
|
|
|
return (GetRetroPixelFormat(format) == m_current_pixel_format);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LibretroHostDisplay::BeginSetDisplayPixels(HostDisplayPixelFormat format, u32 width, u32 height, void** out_buffer,
|
|
|
|
u32* out_pitch)
|
|
|
|
{
|
|
|
|
const retro_pixel_format retro_pf = GetRetroPixelFormat(format);
|
|
|
|
if (!CheckPixelFormat(retro_pf))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_software_fb.data = nullptr;
|
|
|
|
m_software_fb.width = width;
|
|
|
|
m_software_fb.height = height;
|
|
|
|
m_software_fb.pitch = 0;
|
|
|
|
m_software_fb.format = RETRO_PIXEL_FORMAT_UNKNOWN;
|
|
|
|
m_software_fb.access_flags = RETRO_MEMORY_ACCESS_WRITE;
|
|
|
|
m_software_fb.memory_flags = 0;
|
|
|
|
if (g_retro_environment_callback(RETRO_ENVIRONMENT_GET_CURRENT_SOFTWARE_FRAMEBUFFER, &m_software_fb) &&
|
|
|
|
m_software_fb.format == retro_pf)
|
|
|
|
{
|
|
|
|
SetDisplayTexture(m_software_fb.data, format, m_software_fb.width, m_software_fb.height, 0, 0, m_software_fb.width,
|
|
|
|
m_software_fb.height);
|
|
|
|
*out_buffer = m_software_fb.data;
|
|
|
|
*out_pitch = static_cast<u32>(m_software_fb.pitch);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const u32 pitch = Common::AlignUpPow2(width * GetDisplayPixelFormatSize(format), 4);
|
|
|
|
const u32 required_size = height * pitch;
|
|
|
|
if (m_frame_buffer.size() < (required_size / 4))
|
|
|
|
m_frame_buffer.resize(required_size / 4);
|
|
|
|
|
|
|
|
m_frame_buffer_pitch = pitch;
|
|
|
|
SetDisplayTexture(m_frame_buffer.data(), format, width, height, 0, 0, width, height);
|
|
|
|
*out_buffer = m_frame_buffer.data();
|
|
|
|
*out_pitch = pitch;
|
2020-06-08 16:44:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-21 15:25:33 +00:00
|
|
|
void LibretroHostDisplay::EndSetDisplayPixels()
|
|
|
|
{
|
|
|
|
// noop
|
|
|
|
}
|
|
|
|
|
2020-06-29 16:47:27 +00:00
|
|
|
void LibretroHostDisplay::SetVSync(bool enabled)
|
2020-06-08 16:44:42 +00:00
|
|
|
{
|
2020-06-29 16:47:27 +00:00
|
|
|
// The libretro frontend controls this.
|
|
|
|
Log_DevPrintf("Ignoring SetVSync(%u)", BoolToUInt32(enabled));
|
|
|
|
}
|
2020-06-08 16:44:42 +00:00
|
|
|
|
2020-06-29 16:47:27 +00:00
|
|
|
bool LibretroHostDisplay::Render()
|
|
|
|
{
|
|
|
|
if (HasDisplayTexture())
|
2020-06-08 16:44:42 +00:00
|
|
|
{
|
2020-10-21 15:25:33 +00:00
|
|
|
g_retro_video_refresh_callback(m_display_texture_handle, m_display_texture_view_width,
|
|
|
|
m_display_texture_view_height, m_frame_buffer_pitch);
|
|
|
|
|
|
|
|
if (m_display_texture_handle == m_software_fb.data)
|
|
|
|
ClearDisplayTexture();
|
2020-06-08 16:44:42 +00:00
|
|
|
}
|
2020-06-29 16:47:27 +00:00
|
|
|
|
|
|
|
return true;
|
2020-06-08 16:44:42 +00:00
|
|
|
}
|