fix fifo state and scheduling

This commit is contained in:
UjinT34 2026-01-28 02:36:27 +03:00
parent eee66c8c40
commit 572b46870d
6 changed files with 77 additions and 45 deletions

View file

@ -18,7 +18,8 @@ CFifoResource::CFifoResource(UP<CWpFifoV1>&& resource_, SP<CWLSurfaceResource> s
return;
}
m_pending.barrierSet = true;
m_surface->m_pending.barrierSet = true;
m_surface->m_pending.updated.bits.fifo = true;
});
m_resource->setWaitBarrier([this](CWpFifoV1* r) {
@ -27,54 +28,34 @@ CFifoResource::CFifoResource(UP<CWpFifoV1>&& resource_, SP<CWLSurfaceResource> s
return;
}
if (!m_pending.barrierSet)
return;
if (!m_surface->m_current.barrierSet) {
// that might mean an empty commit with a barrier_set alone
static const auto PPEND = CConfigValue<Hyprlang::INT>("debug:fifo_pending_workaround");
if (*PPEND && !m_surface->m_pending.fifoScheduled)
m_surface->m_pending.fifoScheduled = scheduleFrame();
m_pending.surfaceLocked = true;
return;
}
m_surface->m_pending.surfaceLocked = true;
});
m_listeners.surfaceStateCommit = m_surface->m_events.stateCommit.listen([this](auto state) {
static const auto PPEND = CConfigValue<Hyprlang::INT>("debug:fifo_pending_workaround");
if (!m_pending.surfaceLocked)
if (!state || !state->surfaceLocked)
return;
if (*PPEND) {
//#TODO:
// this feels wrong, but if we have no pending frames, presented might never come because
// we are waiting on the barrier to unlock and no damage is around.
// unlock on timeout instead?
if (m_surface->m_enteredOutputs.empty() && m_surface->m_hlSurface) {
for (auto& m : g_pCompositor->m_monitors) {
if (!m || !m->m_enabled)
continue;
static const auto PPEND = CConfigValue<Hyprlang::INT>("debug:fifo_pending_workaround");
auto box = m_surface->m_hlSurface->getSurfaceBoxGlobal();
if (box && !box->intersection({m->m_position, m->m_size}).empty()) {
if (m->m_tearingState.activelyTearing)
return; // dont fifo lock on tearing.
g_pCompositor->scheduleFrameForMonitor(m, Aquamarine::IOutput::AQ_SCHEDULE_NEEDS_FRAME);
}
}
} else {
for (auto& m : m_surface->m_enteredOutputs) {
if (!m)
continue;
if (m->m_tearingState.activelyTearing)
return; // dont fifo lock on tearing.
g_pCompositor->scheduleFrameForMonitor(m.lock(), Aquamarine::IOutput::AQ_SCHEDULE_NEEDS_FRAME);
}
}
}
//#TODO:
// this feels wrong, but if we have no pending frames, presented might never come because
// we are waiting on the barrier to unlock and no damage is around.
// unlock on timeout instead?
if (*PPEND && !state->fifoScheduled)
state->fifoScheduled = scheduleFrame();
// only lock once its mapped.
if (m_surface->m_mapped)
m_surface->m_stateQueue.lock(state, LOCK_REASON_FIFO);
m_pending = {};
});
}
@ -87,9 +68,41 @@ bool CFifoResource::good() {
}
void CFifoResource::presented() {
m_surface->m_current.barrierSet = false;
m_surface->m_stateQueue.unlockFirst(LOCK_REASON_FIFO);
}
bool CFifoResource::scheduleFrame() {
if (m_surface->m_enteredOutputs.empty() && m_surface->m_hlSurface) {
for (auto& m : g_pCompositor->m_monitors) {
if (!m || !m->m_enabled)
continue;
auto box = m_surface->m_hlSurface->getSurfaceBoxGlobal();
if (box && !box->intersection({m->m_position, m->m_size}).empty()) {
if (m->m_tearingState.activelyTearing)
return false; // dont fifo lock on tearing.
g_pCompositor->scheduleFrameForMonitor(m, Aquamarine::IOutput::AQ_SCHEDULE_NEEDS_FRAME);
return true;
}
}
} else {
for (auto& m : m_surface->m_enteredOutputs) {
if (!m)
continue;
if (m->m_tearingState.activelyTearing)
return false; // dont fifo lock on tearing.
g_pCompositor->scheduleFrameForMonitor(m.lock(), Aquamarine::IOutput::AQ_SCHEDULE_NEEDS_FRAME);
return true;
}
}
return false;
}
CFifoManagerResource::CFifoManagerResource(UP<CWpFifoManagerV1>&& resource_) : m_resource(std::move(resource_)) {
if UNLIKELY (!m_resource->resource())
return;

View file

@ -21,18 +21,12 @@ class CFifoResource {
WP<CWLSurfaceResource> m_surface;
struct SState {
bool barrierSet = false;
bool surfaceLocked = false;
};
SState m_pending;
struct {
CHyprSignalListener surfaceStateCommit;
} m_listeners;
void presented();
bool scheduleFrame();
friend class CFifoProtocol;
friend class CFifoManagerResource;

View file

@ -517,6 +517,15 @@ void CWLSurfaceResource::scheduleState(WP<SSurfaceState> state) {
}
void CWLSurfaceResource::commitState(SSurfaceState& state) {
// TODO might be incorrect. needed for VRR with FIFO to avoid same buffer extra frames for second commit when it's used in this way:
// wp_fifo_v1#43.set_barrier()
// wp_fifo_v1#43.wait_barrier()
// wl_surface#3.commit()
// wp_fifo_v1#43.wait_barrier()
// wl_surface#3.commit()
if (!state.updated.all && m_mapped)
return;
auto lastTexture = m_current.texture;
m_current.updateFrom(state);

View file

@ -63,6 +63,10 @@ void SSurfaceState::reset() {
callbacks.clear();
lockMask = LOCK_REASON_NONE;
barrierSet = false;
surfaceLocked = false;
fifoScheduled = false;
}
void SSurfaceState::updateFrom(SSurfaceState& ref) {
@ -112,4 +116,7 @@ void SSurfaceState::updateFrom(SSurfaceState& ref) {
callbacks.insert(callbacks.end(), std::make_move_iterator(ref.callbacks.begin()), std::make_move_iterator(ref.callbacks.end()));
ref.callbacks.clear();
}
if (ref.barrierSet)
barrierSet = ref.barrierSet;
}

View file

@ -48,6 +48,7 @@ struct SSurfaceState {
bool acquire : 1;
bool acked : 1;
bool frame : 1;
bool fifo : 1;
} bits;
} updated;
@ -88,6 +89,11 @@ struct SSurfaceState {
SP<CTexture> texture;
void updateSynchronousTexture(SP<CTexture> lastTexture);
// fifo
bool barrierSet = false;
bool surfaceLocked = false;
bool fifoScheduled = false;
// helpers
CRegion accumulateBufferDamage(); // transforms state.damage and merges it into state.bufferDamage
void updateFrom(SSurfaceState& ref); // updates this state based on a reference state.

View file

@ -68,6 +68,9 @@ auto CSurfaceStateQueue::find(const WP<SSurfaceState>& state) -> std::deque<UP<S
void CSurfaceStateQueue::tryProcess() {
while (!m_queue.empty()) {
auto& front = m_queue.front();
if (front->lockMask & LOCK_REASON_FIFO && !m_surface->m_current.barrierSet)
front->lockMask &= ~LOCK_REASON_FIFO;
if (front->lockMask != LOCK_REASON_NONE)
return;