From 859c78fdc0f09045ffedf2345db486efe3bbecc5 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Mon, 21 Jun 2021 22:04:59 +1000 Subject: [PATCH] DRMDisplay: Restore previous framebuffer on shutdown --- src/common/drm_display.cpp | 17 +++++++++++++++++ src/common/drm_display.h | 5 +++++ src/common/gl/context_egl_gbm.cpp | 2 ++ 3 files changed, 24 insertions(+) diff --git a/src/common/drm_display.cpp b/src/common/drm_display.cpp index a667475ed..ef64d3d47 100644 --- a/src/common/drm_display.cpp +++ b/src/common/drm_display.cpp @@ -19,6 +19,10 @@ DRMDisplay::DRMDisplay(int card /*= 1*/) : m_card_id(card) {} DRMDisplay::~DRMDisplay() { + // restore original buffer + if (m_prev_crtc) + RestoreBuffer(); + if (m_connector) drmModeFreeConnector(m_connector); @@ -90,6 +94,18 @@ bool DRMDisplay::Initialize(u32 width, u32 height, float refresh_rate) return TryOpeningCard(m_card_id, width, height, refresh_rate); } +void DRMDisplay::RestoreBuffer() +{ + if (m_prev_crtc) + { + u32 connector_id = m_connector->connector_id; + drmModeSetCrtc(m_card_fd, m_prev_crtc->crtc_id, m_prev_crtc->buffer_id, m_prev_crtc->x, m_prev_crtc->y, + &connector_id, 1, &m_prev_crtc->mode); + drmModeFreeCrtc(m_prev_crtc); + m_prev_crtc = nullptr; + } +} + bool DRMDisplay::TryOpeningCard(int card, u32 width, u32 height, float refresh_rate) { if (m_card_fd >= 0) @@ -201,6 +217,7 @@ bool DRMDisplay::TryOpeningCard(int card, u32 width, u32 height, float refresh_r drmModeFreeResources(resources); m_card_id = card; + m_prev_crtc = drmModeGetCrtc(m_card_fd, m_crtc_id); return true; } diff --git a/src/common/drm_display.h b/src/common/drm_display.h index 1ef019c33..4125681ce 100644 --- a/src/common/drm_display.h +++ b/src/common/drm_display.h @@ -15,6 +15,9 @@ public: bool Initialize(u32 width, u32 height, float refresh_rate); + /// Restores the buffer saved at startup. + void RestoreBuffer(); + int GetCardID() const { return m_card_id; } int GetCardFD() const { return m_card_fd; } u32 GetWidth() const { return m_mode->hdisplay; } @@ -53,4 +56,6 @@ private: drmModeRes* m_resources = nullptr; drmModeConnector* m_connector = nullptr; drmModeModeInfo* m_mode = nullptr; + + drmModeCrtc* m_prev_crtc = nullptr; }; diff --git a/src/common/gl/context_egl_gbm.cpp b/src/common/gl/context_egl_gbm.cpp index 8a8163af2..2c67581c9 100644 --- a/src/common/gl/context_egl_gbm.cpp +++ b/src/common/gl/context_egl_gbm.cpp @@ -21,6 +21,8 @@ ContextEGLGBM::~ContextEGLGBM() Assert(!m_current_present_buffer); #endif + m_drm_display.RestoreBuffer(); + // We have to destroy the context before the surface/device. // Leaving it to the base class would be too late. DestroySurface();