From e8832bf552aa9d2c2b6503ab7d3af6bbc9031b7c Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sat, 13 Feb 2021 21:25:45 +1000 Subject: [PATCH] DRMDisplay: Use width/height/refresh rate to select mode --- src/common/drm_display.cpp | 40 ++++++++++++++++++++++--------- src/common/drm_display.h | 4 ++-- src/common/gl/context_egl_gbm.cpp | 2 +- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/common/drm_display.cpp b/src/common/drm_display.cpp index 3fd180d65..2cadeef93 100644 --- a/src/common/drm_display.cpp +++ b/src/common/drm_display.cpp @@ -3,6 +3,7 @@ #include "common/log.h" #include "common/string.h" #include "file_system.h" +#include #include #include #include @@ -67,23 +68,23 @@ static uint32_t find_crtc_for_connector(int card_fd, const drmModeRes* resources return -1; } -bool DRMDisplay::Initialize() +bool DRMDisplay::Initialize(u32 width, u32 height, float refresh_rate) { if (m_card_id < 0) { for (int i = 0; i < 10; i++) { - if (TryOpeningCard(i)) + if (TryOpeningCard(i, width, height, refresh_rate)) return true; } return false; } - return TryOpeningCard(m_card_id); + return TryOpeningCard(m_card_id, width, height, refresh_rate); } -bool DRMDisplay::TryOpeningCard(int card) +bool DRMDisplay::TryOpeningCard(int card, u32 width, u32 height, float refresh_rate) { if (m_card_fd >= 0) close(m_card_fd); @@ -126,15 +127,32 @@ bool DRMDisplay::TryOpeningCard(int card) for (int i = 0; i < m_connector->count_modes; i++) { drmModeModeInfo* next_mode = &m_connector->modes[i]; - if (next_mode->type & DRM_MODE_TYPE_PREFERRED) - { - m_mode = next_mode; - break; - } - if (!m_mode || (next_mode->hdisplay * next_mode->vdisplay) > (m_mode->hdisplay * m_mode->vdisplay)) + const float mode_refresh_rate = (static_cast(next_mode->clock) * 1000.0f) / + (static_cast(next_mode->htotal) * static_cast(next_mode->vtotal)); + Log_DevPrintf("Checking mode %u: %ux%u @ %f hz", i, next_mode->hdisplay, next_mode->vdisplay, mode_refresh_rate); + + if (width == 0 && height == 0) { - m_mode = next_mode; + // use preferred mode if we're auto selecting + if (next_mode->type & DRM_MODE_TYPE_PREFERRED) + { + m_mode = next_mode; + break; + } + else if (!m_mode) + { + m_mode = next_mode; + } + } + else + { + if (width == next_mode->hdisplay && height == next_mode->vdisplay && + (refresh_rate == 0.0f || std::abs(mode_refresh_rate - refresh_rate) < 0.1f)) + { + m_mode = next_mode; + break; + } } } diff --git a/src/common/drm_display.h b/src/common/drm_display.h index 04692405c..789b9be69 100644 --- a/src/common/drm_display.h +++ b/src/common/drm_display.h @@ -11,7 +11,7 @@ public: DRMDisplay(int card = -1); ~DRMDisplay(); - bool Initialize(); + bool Initialize(u32 width, u32 height, float refresh_rate); int GetCardID() const { return m_card_id; } int GetCardFD() const { return m_card_fd; } @@ -28,7 +28,7 @@ private: MAX_BUFFERS = 5 }; - bool TryOpeningCard(int card); + bool TryOpeningCard(int card, u32 width, u32 height, float refresh_rate); int m_card_id = 0; int m_card_fd = -1; diff --git a/src/common/gl/context_egl_gbm.cpp b/src/common/gl/context_egl_gbm.cpp index 1c12c4bac..3cc7d041c 100644 --- a/src/common/gl/context_egl_gbm.cpp +++ b/src/common/gl/context_egl_gbm.cpp @@ -83,7 +83,7 @@ bool ContextEGLGBM::CreateGBMDevice() bool ContextEGLGBM::CreateDisplay(const WindowInfo& wi) { - return m_drm_display.Initialize(); + return m_drm_display.Initialize(wi.surface_width, wi.surface_height, wi.surface_refresh_rate); } bool ContextEGLGBM::SetDisplay()