DRMDisplay: Use width/height/refresh rate to select mode

This commit is contained in:
Connor McLaughlin
2021-02-13 21:25:45 +10:00
parent b811b78c09
commit e8832bf552
3 changed files with 32 additions and 14 deletions

View File

@ -3,6 +3,7 @@
#include "common/log.h" #include "common/log.h"
#include "common/string.h" #include "common/string.h"
#include "file_system.h" #include "file_system.h"
#include <cmath>
#include <fcntl.h> #include <fcntl.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
@ -67,23 +68,23 @@ static uint32_t find_crtc_for_connector(int card_fd, const drmModeRes* resources
return -1; return -1;
} }
bool DRMDisplay::Initialize() bool DRMDisplay::Initialize(u32 width, u32 height, float refresh_rate)
{ {
if (m_card_id < 0) if (m_card_id < 0)
{ {
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
if (TryOpeningCard(i)) if (TryOpeningCard(i, width, height, refresh_rate))
return true; return true;
} }
return false; 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) if (m_card_fd >= 0)
close(m_card_fd); close(m_card_fd);
@ -126,17 +127,34 @@ bool DRMDisplay::TryOpeningCard(int card)
for (int i = 0; i < m_connector->count_modes; i++) for (int i = 0; i < m_connector->count_modes; i++)
{ {
drmModeModeInfo* next_mode = &m_connector->modes[i]; drmModeModeInfo* next_mode = &m_connector->modes[i];
const float mode_refresh_rate = (static_cast<float>(next_mode->clock) * 1000.0f) /
(static_cast<float>(next_mode->htotal) * static_cast<float>(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)
{
// use preferred mode if we're auto selecting
if (next_mode->type & DRM_MODE_TYPE_PREFERRED) if (next_mode->type & DRM_MODE_TYPE_PREFERRED)
{ {
m_mode = next_mode; m_mode = next_mode;
break; break;
} }
else if (!m_mode)
if (!m_mode || (next_mode->hdisplay * next_mode->vdisplay) > (m_mode->hdisplay * m_mode->vdisplay))
{ {
m_mode = next_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;
}
}
}
if (!m_mode) if (!m_mode)
{ {

View File

@ -11,7 +11,7 @@ public:
DRMDisplay(int card = -1); DRMDisplay(int card = -1);
~DRMDisplay(); ~DRMDisplay();
bool Initialize(); bool Initialize(u32 width, u32 height, float refresh_rate);
int GetCardID() const { return m_card_id; } int GetCardID() const { return m_card_id; }
int GetCardFD() const { return m_card_fd; } int GetCardFD() const { return m_card_fd; }
@ -28,7 +28,7 @@ private:
MAX_BUFFERS = 5 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_id = 0;
int m_card_fd = -1; int m_card_fd = -1;

View File

@ -83,7 +83,7 @@ bool ContextEGLGBM::CreateGBMDevice()
bool ContextEGLGBM::CreateDisplay(const WindowInfo& wi) 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() bool ContextEGLGBM::SetDisplay()