core: add a destructor to CHyprOpenglImpl and avoid wl_container_of undefined behaviour (#7101)

* protocols: avoid undefined behaviour in C macro

to safely use wl_container_of with a class the class has to be no
virtual functions, no inheritance, and uniform access control (e.g all
public)

work around this by putting this into a destroywrapper struct.

* opengl: clean memory on destruction

add a destructor and free the allocated memory and close the fd
This commit is contained in:
Tom Englund 2024-07-29 19:19:47 +02:00 committed by Vaxry
parent 60b663e276
commit 9c38b0fdbe
6 changed files with 47 additions and 12 deletions

View file

@ -8,13 +8,14 @@ static void bindManagerInt(wl_client* client, void* data, uint32_t version, uint
} }
static void handleDisplayDestroy(struct wl_listener* listener, void* data) { static void handleDisplayDestroy(struct wl_listener* listener, void* data) {
CGlobalShortcutsProtocolManager* proto = wl_container_of(listener, proto, m_liDisplayDestroy); CGlobalShortcutsProtocolManagerDestroyWrapper* wrap = wl_container_of(listener, wrap, listener);
CGlobalShortcutsProtocolManager* proto = wrap->parent;
proto->displayDestroy(); proto->displayDestroy();
} }
void CGlobalShortcutsProtocolManager::displayDestroy() { void CGlobalShortcutsProtocolManager::displayDestroy() {
wl_list_remove(&m_liDisplayDestroy.link); wl_list_remove(&m_liDisplayDestroy.listener.link);
wl_list_init(&m_liDisplayDestroy.link); wl_list_init(&m_liDisplayDestroy.listener.link);
wl_global_destroy(m_pGlobal); wl_global_destroy(m_pGlobal);
} }
@ -30,8 +31,10 @@ CGlobalShortcutsProtocolManager::CGlobalShortcutsProtocolManager() {
return; return;
} }
m_liDisplayDestroy.notify = handleDisplayDestroy; wl_list_init(&m_liDisplayDestroy.listener.link);
wl_display_add_destroy_listener(g_pCompositor->m_sWLDisplay, &m_liDisplayDestroy); m_liDisplayDestroy.listener.notify = handleDisplayDestroy;
m_liDisplayDestroy.parent = this;
wl_display_add_destroy_listener(g_pCompositor->m_sWLDisplay, &m_liDisplayDestroy.listener);
Debug::log(LOG, "GlobalShortcutsManager started successfully!"); Debug::log(LOG, "GlobalShortcutsManager started successfully!");
} }

View file

@ -14,6 +14,12 @@ struct SShortcutClient {
std::vector<std::unique_ptr<SShortcut>> shortcuts; std::vector<std::unique_ptr<SShortcut>> shortcuts;
}; };
class CGlobalShortcutsProtocolManager;
struct CGlobalShortcutsProtocolManagerDestroyWrapper {
wl_listener listener;
CGlobalShortcutsProtocolManager* parent = nullptr;
};
class CGlobalShortcutsProtocolManager { class CGlobalShortcutsProtocolManager {
public: public:
CGlobalShortcutsProtocolManager(); CGlobalShortcutsProtocolManager();
@ -31,7 +37,7 @@ class CGlobalShortcutsProtocolManager {
std::vector<SShortcut> getAllShortcuts(); std::vector<SShortcut> getAllShortcuts();
wl_listener m_liDisplayDestroy; CGlobalShortcutsProtocolManagerDestroyWrapper m_liDisplayDestroy;
private: private:
std::vector<std::unique_ptr<SShortcutClient>> m_vClients; std::vector<std::unique_ptr<SShortcutClient>> m_vClients;

View file

@ -6,13 +6,14 @@ static void bindManagerInternal(wl_client* client, void* data, uint32_t ver, uin
} }
static void displayDestroyInternal(struct wl_listener* listener, void* data) { static void displayDestroyInternal(struct wl_listener* listener, void* data) {
IWaylandProtocol* proto = wl_container_of(listener, proto, m_liDisplayDestroy); IWaylandProtocolDestroyWrapper* wrap = wl_container_of(listener, wrap, listener);
IWaylandProtocol* proto = wrap->parent;
proto->onDisplayDestroy(); proto->onDisplayDestroy();
} }
void IWaylandProtocol::onDisplayDestroy() { void IWaylandProtocol::onDisplayDestroy() {
wl_list_remove(&m_liDisplayDestroy.link); wl_list_remove(&m_liDisplayDestroy.listener.link);
wl_list_init(&m_liDisplayDestroy.link); wl_list_init(&m_liDisplayDestroy.listener.link);
wl_global_destroy(m_pGlobal); wl_global_destroy(m_pGlobal);
} }
@ -24,8 +25,10 @@ IWaylandProtocol::IWaylandProtocol(const wl_interface* iface, const int& ver, co
return; return;
} }
m_liDisplayDestroy.notify = displayDestroyInternal; wl_list_init(&m_liDisplayDestroy.listener.link);
wl_display_add_destroy_listener(g_pCompositor->m_sWLDisplay, &m_liDisplayDestroy); m_liDisplayDestroy.listener.notify = displayDestroyInternal;
m_liDisplayDestroy.parent = this;
wl_display_add_destroy_listener(g_pCompositor->m_sWLDisplay, &m_liDisplayDestroy.listener);
protoLog(LOG, "Registered global"); protoLog(LOG, "Registered global");
} }

View file

@ -11,6 +11,12 @@
#define PROTO NProtocols #define PROTO NProtocols
class IWaylandProtocol;
struct IWaylandProtocolDestroyWrapper {
wl_listener listener;
IWaylandProtocol* parent = nullptr;
};
class IWaylandProtocol { class IWaylandProtocol {
public: public:
IWaylandProtocol(const wl_interface* iface, const int& ver, const std::string& name); IWaylandProtocol(const wl_interface* iface, const int& ver, const std::string& name);
@ -26,7 +32,7 @@ class IWaylandProtocol {
Debug::log(level, std::format("[{}] ", m_szName) + std::vformat(fmt.get(), std::make_format_args(args...))); Debug::log(level, std::format("[{}] ", m_szName) + std::vformat(fmt.get(), std::make_format_args(args...)));
}; };
wl_listener m_liDisplayDestroy; IWaylandProtocolDestroyWrapper m_liDisplayDestroy;
private: private:
std::string m_szName; std::string m_szName;

View file

@ -345,6 +345,22 @@ CHyprOpenGLImpl::CHyprOpenGLImpl() {
m_tGlobalTimer.reset(); m_tGlobalTimer.reset();
} }
CHyprOpenGLImpl::~CHyprOpenGLImpl() {
if (m_pEglDisplay && m_pEglContext != EGL_NO_CONTEXT)
eglDestroyContext(m_pEglDisplay, m_pEglContext);
if (m_pEglDisplay)
eglTerminate(m_pEglDisplay);
eglReleaseThread();
if (m_pGbmDevice)
gbm_device_destroy(m_pGbmDevice);
if (m_iGBMFD >= 0)
close(m_iGBMFD);
}
std::optional<std::vector<uint64_t>> CHyprOpenGLImpl::getModsForFormat(EGLint format) { std::optional<std::vector<uint64_t>> CHyprOpenGLImpl::getModsForFormat(EGLint format) {
// TODO: return std::expected when clang supports it // TODO: return std::expected when clang supports it

View file

@ -145,6 +145,7 @@ class CGradientValueData;
class CHyprOpenGLImpl { class CHyprOpenGLImpl {
public: public:
CHyprOpenGLImpl(); CHyprOpenGLImpl();
~CHyprOpenGLImpl();
void begin(CMonitor*, const CRegion& damage, CFramebuffer* fb = nullptr, std::optional<CRegion> finalDamage = {}); void begin(CMonitor*, const CRegion& damage, CFramebuffer* fb = nullptr, std::optional<CRegion> finalDamage = {});
void beginSimple(CMonitor*, const CRegion& damage, SP<CRenderbuffer> rb = nullptr, CFramebuffer* fb = nullptr); void beginSimple(CMonitor*, const CRegion& damage, SP<CRenderbuffer> rb = nullptr, CFramebuffer* fb = nullptr);