Skip to content

Commit

Permalink
core/renderer: Add GPU hotplug support (#8980)
Browse files Browse the repository at this point in the history
  • Loading branch information
maround95 authored Jan 23, 2025
1 parent fda5626 commit f1bd628
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
32 changes: 26 additions & 6 deletions src/managers/eventLoop/EventLoopManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <algorithm>
#include <limits>
#include <ranges>

#include <sys/timerfd.h>
#include <ctime>
Expand All @@ -20,8 +21,8 @@ CEventLoopManager::CEventLoopManager(wl_display* display, wl_event_loop* wlEvent
}

CEventLoopManager::~CEventLoopManager() {
for (auto const& eventSource : m_sWayland.aqEventSources) {
wl_event_source_remove(eventSource);
for (auto const& [_, eventSourceData] : aqEventSources) {
wl_event_source_remove(eventSourceData.eventSource);
}

if (m_sWayland.eventSource)
Expand Down Expand Up @@ -56,10 +57,8 @@ void CEventLoopManager::enterLoop() {
if (const auto FD = g_pConfigWatcher->getInotifyFD(); FD >= 0)
m_configWatcherInotifySource = wl_event_loop_add_fd(m_sWayland.loop, FD, WL_EVENT_READABLE, configWatcherWrite, nullptr);

aqPollFDs = g_pCompositor->m_pAqBackend->getPollFDs();
for (auto const& fd : aqPollFDs) {
m_sWayland.aqEventSources.emplace_back(wl_event_loop_add_fd(m_sWayland.loop, fd->fd, WL_EVENT_READABLE, aquamarineFDWrite, fd.get()));
}
syncPollFDs();
m_sListeners.pollFDsChanged = g_pCompositor->m_pAqBackend->events.pollFDsChanged.registerListener([this](std::any d) { syncPollFDs(); });

// if we have a session, dispatch it to get the pending input devices
if (g_pCompositor->m_pAqBackend->hasSession())
Expand Down Expand Up @@ -144,3 +143,24 @@ void CEventLoopManager::doLater(const std::function<void()>& fn) {
},
&m_sIdle);
}

void CEventLoopManager::syncPollFDs() {
auto aqPollFDs = g_pCompositor->m_pAqBackend->getPollFDs();

std::erase_if(aqEventSources, [&](const auto& item) {
auto const& [fd, eventSourceData] = item;

// If no pollFD has the same fd, remove this event source
const bool shouldRemove = std::ranges::none_of(aqPollFDs, [&](const auto& pollFD) { return pollFD->fd == fd; });

if (shouldRemove)
wl_event_source_remove(eventSourceData.eventSource);

return shouldRemove;
});

for (auto& fd : aqPollFDs | std::views::filter([&](SP<Aquamarine::SPollFD> fd) { return !aqEventSources.contains(fd->fd); })) {
auto eventSource = wl_event_loop_add_fd(m_sWayland.loop, fd->fd, WL_EVENT_READABLE, aquamarineFDWrite, fd.get());
aqEventSources[fd->fd] = {.pollFD = fd, .eventSource = eventSource};
}
}
29 changes: 21 additions & 8 deletions src/managers/eventLoop/EventLoopManager.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#pragma once

#include <condition_variable>
#include <map>
#include <mutex>
#include <thread>
#include <wayland-server.h>
#include "helpers/signal/Signal.hpp"

#include "EventLoopTimer.hpp"

Expand Down Expand Up @@ -36,24 +38,35 @@ class CEventLoopManager {
};

private:
// Manages the event sources after AQ pollFDs change.
void syncPollFDs();

struct SEventSourceData {
SP<Aquamarine::SPollFD> pollFD;
wl_event_source* eventSource = nullptr;
};

struct {
wl_event_loop* loop = nullptr;
wl_display* display = nullptr;
wl_event_source* eventSource = nullptr;
std::vector<wl_event_source*> aqEventSources;
wl_event_loop* loop = nullptr;
wl_display* display = nullptr;
wl_event_source* eventSource = nullptr;
} m_sWayland;

struct {
std::vector<SP<CEventLoopTimer>> timers;
int timerfd = -1;
} m_sTimers;

SIdleData m_sIdle;
std::vector<SP<Aquamarine::SPollFD>> aqPollFDs;
SIdleData m_sIdle;
std::map<int, SEventSourceData> aqEventSources;

struct {
CHyprSignalListener pollFDsChanged;
} m_sListeners;

wl_event_source* m_configWatcherInotifySource = nullptr;
wl_event_source* m_configWatcherInotifySource = nullptr;

friend class CSyncTimeline;
};

inline std::unique_ptr<CEventLoopManager> g_pEventLoopManager;
inline std::unique_ptr<CEventLoopManager> g_pEventLoopManager;

0 comments on commit f1bd628

Please sign in to comment.