WindowInfo: Add surface_scale field

This commit is contained in:
Connor McLaughlin 2020-07-01 00:35:19 +10:00
parent e374853cf5
commit bf08385051
7 changed files with 38 additions and 35 deletions

View File

@ -29,6 +29,7 @@ struct WindowInfo
void* window_handle = nullptr;
u32 surface_width = 0;
u32 surface_height = 0;
float surface_scale = 1.0f;
SurfaceFormat surface_format = SurfaceFormat::RGB8;
// Needed for macOS.

View File

@ -767,6 +767,7 @@ void LibretroHostInterface::SwitchToHardwareRenderer()
wi.display_connection = &g_libretro_host_interface.m_hw_render_callback;
wi.surface_width = avi.geometry.base_width;
wi.surface_height = avi.geometry.base_height;
wi.surface_scale = 1.0f;
if (!display || !display->CreateRenderDevice(wi, {}, g_libretro_host_interface.m_settings.gpu_use_debug_device))
{
Log_ErrorPrintf("Failed to create hardware host display");

View File

@ -86,6 +86,7 @@ std::optional<WindowInfo> QtDisplayWidget::getWindowInfo() const
wi.surface_width = scaledWindowWidth();
wi.surface_height = scaledWindowHeight();
wi.surface_scale = devicePixelRatioFromScreen();
wi.surface_format = WindowInfo::SurfaceFormat::RGB8;
return wi;

View File

@ -47,36 +47,6 @@ ALWAYS_INLINE static TinyString GetWindowTitle()
return TinyString::FromFormat("DuckStation %s (%s)", g_scm_tag_str, g_scm_branch_str);
}
float SDLHostInterface::GetDPIScaleFactor(SDL_Window* window)
{
#ifdef __APPLE__
static constexpr float DEFAULT_DPI = 72.0f;
#else
static constexpr float DEFAULT_DPI = 96.0f;
#endif
if (!window)
{
SDL_Window* dummy_window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1, 1,
SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_HIDDEN);
if (!dummy_window)
return 1.0f;
const float scale = GetDPIScaleFactor(dummy_window);
SDL_DestroyWindow(dummy_window);
return scale;
}
int display_index = SDL_GetWindowDisplayIndex(window);
float display_dpi = DEFAULT_DPI;
if (SDL_GetDisplayDPI(display_index, &display_dpi, nullptr, nullptr) != 0)
return 1.0f;
return display_dpi / DEFAULT_DPI;
}
bool SDLHostInterface::CreateSDLWindow()
{
static constexpr u32 DEFAULT_WINDOW_WIDTH = 900;
@ -92,7 +62,7 @@ bool SDLHostInterface::CreateSDLWindow()
#ifndef __APPLE__
{
// scale by default monitor's DPI
float scale = GetDPIScaleFactor(nullptr);
float scale = SDLUtil::GetDPIScaleFactor(nullptr);
window_width = static_cast<u32>(std::round(static_cast<float>(window_width) * scale));
window_height = static_cast<u32>(std::round(static_cast<float>(window_height) * scale));
}
@ -214,7 +184,7 @@ void SDLHostInterface::DestroyDisplay()
void SDLHostInterface::CreateImGuiContext()
{
const float framebuffer_scale = GetDPIScaleFactor(m_window);
const float framebuffer_scale = SDLUtil::GetDPIScaleFactor(m_window);
ImGui::CreateContext();
ImGui::GetIO().IniFilename = nullptr;
@ -229,7 +199,7 @@ void SDLHostInterface::CreateImGuiContext()
void SDLHostInterface::UpdateFramebufferScale()
{
const float framebuffer_scale = GetDPIScaleFactor(m_window);
const float framebuffer_scale = SDLUtil::GetDPIScaleFactor(m_window);
ImGui::GetIO().DisplayFramebufferScale.x = framebuffer_scale;
ImGui::GetIO().DisplayFramebufferScale.y = framebuffer_scale;
}

View File

@ -60,8 +60,6 @@ protected:
private:
bool HasSystem() const { return static_cast<bool>(m_system); }
static float GetDPIScaleFactor(SDL_Window* window);
bool CreateSDLWindow();
void DestroySDLWindow();
bool CreateDisplay();

View File

@ -32,6 +32,7 @@ std::optional<WindowInfo> GetWindowInfoForSDLWindow(SDL_Window* window)
WindowInfo wi;
wi.surface_width = static_cast<u32>(window_width);
wi.surface_height = static_cast<u32>(window_height);
wi.surface_scale = GetDPIScaleFactor(window);
wi.surface_format = WindowInfo::SurfaceFormat::RGB8;
switch (syswm.subsystem)
@ -65,4 +66,34 @@ std::optional<WindowInfo> GetWindowInfoForSDLWindow(SDL_Window* window)
return wi;
}
float GetDPIScaleFactor(SDL_Window* window)
{
#ifdef __APPLE__
static constexpr float DEFAULT_DPI = 72.0f;
#else
static constexpr float DEFAULT_DPI = 96.0f;
#endif
if (!window)
{
SDL_Window* dummy_window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1, 1,
SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_HIDDEN);
if (!dummy_window)
return 1.0f;
const float scale = GetDPIScaleFactor(dummy_window);
SDL_DestroyWindow(dummy_window);
return scale;
}
int display_index = SDL_GetWindowDisplayIndex(window);
float display_dpi = DEFAULT_DPI;
if (SDL_GetDisplayDPI(display_index, &display_dpi, nullptr, nullptr) != 0)
return 1.0f;
return display_dpi / DEFAULT_DPI;
}
} // namespace SDLUtil

View File

@ -7,4 +7,5 @@ struct SDL_Window;
namespace SDLUtil {
std::optional<WindowInfo> GetWindowInfoForSDLWindow(SDL_Window* window);
float GetDPIScaleFactor(SDL_Window* window);
}