diff --git a/src/config/ConfigDescriptions.hpp b/src/config/ConfigDescriptions.hpp index fa1ad35b..04991a96 100644 --- a/src/config/ConfigDescriptions.hpp +++ b/src/config/ConfigDescriptions.hpp @@ -1573,6 +1573,12 @@ inline static const std::vector CONFIG_OPTIONS = { .type = CONFIG_OPTION_CHOICE, .data = SConfigOptionDescription::SChoiceData{0, "default,gamma22,gamma22force,srgb"}, }, + SConfigOptionDescription{ + .value = "render:commit_timing_enabled", + .description = "Enable commit timing proto. Requires restart", + .type = CONFIG_OPTION_BOOL, + .data = SConfigOptionDescription::SBoolData{true}, + }, /* * cursor: diff --git a/src/config/ConfigManager.cpp b/src/config/ConfigManager.cpp index 61ba9dd3..046a2667 100644 --- a/src/config/ConfigManager.cpp +++ b/src/config/ConfigManager.cpp @@ -785,6 +785,7 @@ CConfigManager::CConfigManager() { registerConfigVar("render:new_render_scheduling", Hyprlang::INT{0}); registerConfigVar("render:non_shader_cm", Hyprlang::INT{3}); registerConfigVar("render:cm_sdr_eotf", Hyprlang::INT{0}); + registerConfigVar("render:commit_timing_enabled", Hyprlang::INT{1}); registerConfigVar("ecosystem:no_update_news", Hyprlang::INT{0}); registerConfigVar("ecosystem:no_donation_nag", Hyprlang::INT{0}); diff --git a/src/helpers/Monitor.cpp b/src/helpers/Monitor.cpp index f0077c63..be0b78fd 100644 --- a/src/helpers/Monitor.cpp +++ b/src/helpers/Monitor.cpp @@ -114,10 +114,12 @@ void CMonitor::onConnect(bool noRule) { ts = nullptr; } - if (!ts) - PROTO::presentation->onPresented(m_self.lock(), Time::steadyNow(), event.refresh, event.seq, event.flags); - else - PROTO::presentation->onPresented(m_self.lock(), Time::fromTimespec(event.when), event.refresh, event.seq, event.flags); + if (!ts) { + timespec mono{}; + clock_gettime(CLOCK_MONOTONIC, &mono); + PROTO::presentation->onPresented(m_self.lock(), mono, event.refresh, event.seq, event.flags & ~Aquamarine::IOutput::AQ_OUTPUT_PRESENT_HW_CLOCK); + } else + PROTO::presentation->onPresented(m_self.lock(), *ts, event.refresh, event.seq, event.flags); if (m_zoomAnimFrameCounter < 5) { m_zoomAnimFrameCounter++; diff --git a/src/helpers/time/Time.cpp b/src/helpers/time/Time.cpp index 7ef24e22..f454b784 100644 --- a/src/helpers/time/Time.cpp +++ b/src/helpers/time/Time.cpp @@ -103,7 +103,7 @@ Time::steady_tp Time::fromTimespec(const timespec* ts) { } struct timespec Time::toTimespec(const steady_tp& tp) { - struct timespec mono, real; + timespec mono{}, real{}; clock_gettime(CLOCK_MONOTONIC, &mono); clock_gettime(CLOCK_REALTIME, &real); Time::steady_tp now = Time::steadyNow(); @@ -136,3 +136,10 @@ struct timespec Time::toTimespec(const steady_tp& tp) { auto sum = timeadd(tpTime, diffFinal); return timespec{.tv_sec = sum.first, .tv_nsec = sum.second}; } + +Time::steady_dur Time::till(const timespec& ts) { + timespec mono{}; + clock_gettime(CLOCK_MONOTONIC, &mono); + const auto delay = (ts.tv_sec - mono.tv_sec) * 1000000000 + (ts.tv_nsec - mono.tv_nsec); + return std::chrono::nanoseconds(delay); +} diff --git a/src/helpers/time/Time.hpp b/src/helpers/time/Time.hpp index eb3b5771..ce99982b 100644 --- a/src/helpers/time/Time.hpp +++ b/src/helpers/time/Time.hpp @@ -17,6 +17,7 @@ namespace Time { steady_tp fromTimespec(const timespec*); struct timespec toTimespec(const steady_tp& tp); + steady_dur till(const timespec& ts); uint64_t millis(const steady_tp& tp); uint64_t millis(const system_tp& tp); diff --git a/src/managers/ProtocolManager.cpp b/src/managers/ProtocolManager.cpp index fe4e3c7f..6376f2a0 100644 --- a/src/managers/ProtocolManager.cpp +++ b/src/managers/ProtocolManager.cpp @@ -107,6 +107,8 @@ CProtocolManager::CProtocolManager() { static const auto PENABLECM = CConfigValue("render:cm_enabled"); static const auto PDEBUGCM = CConfigValue("debug:full_cm_proto"); + static const auto PENABLECT = CConfigValue("render:commit_timing_enabled"); + // Outputs are a bit dumb, we have to agree. static auto P = g_pHookSystem->hookDynamic("monitorAdded", [this](void* self, SCallbackInfo& info, std::any param) { auto M = std::any_cast(param); @@ -194,7 +196,9 @@ CProtocolManager::CProtocolManager() { PROTO::extDataDevice = makeUnique(&ext_data_control_manager_v1_interface, 1, "ExtDataDevice"); PROTO::pointerWarp = makeUnique(&wp_pointer_warp_v1_interface, 1, "PointerWarp"); PROTO::fifo = makeUnique(&wp_fifo_manager_v1_interface, 1, "Fifo"); - PROTO::commitTiming = makeUnique(&wp_commit_timing_manager_v1_interface, 1, "CommitTiming"); + + if (*PENABLECT) + PROTO::commitTiming = makeUnique(&wp_commit_timing_manager_v1_interface, 1, "CommitTiming"); if (*PENABLECM) PROTO::colorManagement = makeUnique(&wp_color_manager_v1_interface, 1, "ColorManagement", *PDEBUGCM); diff --git a/src/protocols/CommitTiming.cpp b/src/protocols/CommitTiming.cpp index bce14f6b..c1fca990 100644 --- a/src/protocols/CommitTiming.cpp +++ b/src/protocols/CommitTiming.cpp @@ -12,61 +12,48 @@ CCommitTimerResource::CCommitTimerResource(UP&& resource_, SP< m_resource->setOnDestroy([this](CWpCommitTimerV1* r) { PROTO::commitTiming->destroyResource(this); }); m_resource->setSetTimestamp([this](CWpCommitTimerV1* r, uint32_t tvHi, uint32_t tvLo, uint32_t tvNsec) { - return; - if (!m_surface) { r->error(WP_COMMIT_TIMER_V1_ERROR_SURFACE_DESTROYED, "Surface was gone"); return; } - if (m_pendingTimeout.has_value()) { + if (m_surface->m_pending.pendingTimeout.has_value()) { r->error(WP_COMMIT_TIMER_V1_ERROR_TIMESTAMP_EXISTS, "Timestamp is already set"); return; } - timespec ts; - ts.tv_sec = (((uint64_t)tvHi) << 32) | (uint64_t)tvLo; - ts.tv_nsec = tvNsec; + const auto delay = Time::till({.tv_sec = (((uint64_t)tvHi) << 32) | (uint64_t)tvLo, .tv_nsec = tvNsec}); - const auto TIME = Time::fromTimespec(&ts); - const auto TIME_NOW = Time::steadyNow(); - - if (TIME_NOW > TIME) { - m_pendingTimeout.reset(); + if (delay.count() <= 0) { + m_surface->m_pending.pendingTimeout.reset(); } else - m_pendingTimeout = TIME - TIME_NOW; + m_surface->m_pending.pendingTimeout = delay; }); m_listeners.surfaceStateCommit = m_surface->m_events.stateCommit2.listen([this](auto state) { - if (!m_pendingTimeout.has_value()) + if (!state || !state->pendingTimeout.has_value() || !m_surface || m_surface->isTearing()) return; m_surface->m_stateQueue.lock(state, LOCK_REASON_TIMER); - if (!m_timerPresent) { - m_timerPresent = true; - timer = makeShared( - m_pendingTimeout, - [this](SP self, void* data) { - if (!m_surface) + if (!state->timer) { + state->timer = makeShared( + state->pendingTimeout, + [this, state](SP self, void* data) { + if (!m_surface || !state) return; - m_surface->m_stateQueue.unlockFirst(LOCK_REASON_TIMER); + m_surface->m_stateQueue.unlock(state, LOCK_REASON_TIMER); }, nullptr); - g_pEventLoopManager->addTimer(timer); + g_pEventLoopManager->addTimer(state->timer); } else - timer->updateTimeout(m_pendingTimeout); + state->timer->updateTimeout(state->pendingTimeout); - m_pendingTimeout.reset(); + state->pendingTimeout.reset(); }); } -CCommitTimerResource::~CCommitTimerResource() { - if (m_timerPresent) - g_pEventLoopManager->removeTimer(timer); -} - bool CCommitTimerResource::good() { return m_resource->resource(); } diff --git a/src/protocols/CommitTiming.hpp b/src/protocols/CommitTiming.hpp index e79face8..b5a1de93 100644 --- a/src/protocols/CommitTiming.hpp +++ b/src/protocols/CommitTiming.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include #include "WaylandProtocol.hpp" #include "commit-timing-v1.hpp" @@ -14,16 +13,12 @@ class CEventLoopTimer; class CCommitTimerResource { public: CCommitTimerResource(UP&& resource_, SP surface); - ~CCommitTimerResource(); bool good(); private: - UP m_resource; - WP m_surface; - bool m_timerPresent = false; - std::optional m_pendingTimeout; - SP timer; + UP m_resource; + WP m_surface; struct { CHyprSignalListener surfaceStateCommit; diff --git a/src/protocols/PresentationTime.cpp b/src/protocols/PresentationTime.cpp index 82c4b1eb..f1cd42d2 100644 --- a/src/protocols/PresentationTime.cpp +++ b/src/protocols/PresentationTime.cpp @@ -40,7 +40,7 @@ bool CPresentationFeedback::good() { return m_resource->resource(); } -void CPresentationFeedback::sendQueued(WP data, const Time::steady_tp& when, uint32_t untilRefreshNs, uint64_t seq, uint32_t reportedFlags) { +void CPresentationFeedback::sendQueued(WP data, const timespec& when, uint32_t untilRefreshNs, uint64_t seq, uint32_t reportedFlags) { auto client = m_resource->client(); if LIKELY (PROTO::outputs.contains(data->m_monitor->m_name) && data->m_wasPresented) { @@ -48,28 +48,26 @@ void CPresentationFeedback::sendQueued(WP data, const T m_resource->sendSyncOutput(outputResource->getResource()->resource()); } - uint32_t flags = 0; - if (!data->m_monitor->m_tearingState.activelyTearing) - flags |= WP_PRESENTATION_FEEDBACK_KIND_VSYNC; - if (data->m_zeroCopy) - flags |= WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY; - if (reportedFlags & Aquamarine::IOutput::AQ_OUTPUT_PRESENT_HW_CLOCK) - flags |= WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK; - if (reportedFlags & Aquamarine::IOutput::AQ_OUTPUT_PRESENT_HW_COMPLETION) - flags |= WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION; + if (data->m_wasPresented) { + uint32_t flags = 0; + if (!data->m_monitor->m_tearingState.activelyTearing) + flags |= WP_PRESENTATION_FEEDBACK_KIND_VSYNC; + if (data->m_zeroCopy) + flags |= WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY; + if (reportedFlags & Aquamarine::IOutput::AQ_OUTPUT_PRESENT_HW_CLOCK) + flags |= WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK; + if (reportedFlags & Aquamarine::IOutput::AQ_OUTPUT_PRESENT_HW_COMPLETION) + flags |= WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION; - const auto TIMESPEC = Time::toTimespec(when); + time_t tv_sec = 0; + if (sizeof(time_t) > 4) + tv_sec = when.tv_sec >> 32; - time_t tv_sec = 0; - if (sizeof(time_t) > 4) - tv_sec = TIMESPEC.tv_sec >> 32; + uint32_t refreshNs = m_resource->version() == 1 && data->m_monitor->m_vrrActive && data->m_monitor->m_output->vrrCapable ? 0 : untilRefreshNs; - uint32_t refreshNs = m_resource->version() == 1 && data->m_monitor->m_vrrActive && data->m_monitor->m_output->vrrCapable ? 0 : untilRefreshNs; - - if (data->m_wasPresented) - m_resource->sendPresented(sc(tv_sec), sc(TIMESPEC.tv_sec & 0xFFFFFFFF), sc(TIMESPEC.tv_nsec), refreshNs, sc(seq >> 32), + m_resource->sendPresented(sc(tv_sec), sc(when.tv_sec & 0xFFFFFFFF), sc(when.tv_nsec), refreshNs, sc(seq >> 32), sc(seq & 0xFFFFFFFF), sc(flags)); - else + } else m_resource->sendDiscarded(); m_done = true; @@ -88,6 +86,7 @@ void CPresentationProtocol::bindManager(wl_client* client, void* data, uint32_t RESOURCE->setDestroy([this](CWpPresentation* pMgr) { this->onManagerResourceDestroy(pMgr->resource()); }); RESOURCE->setFeedback([this](CWpPresentation* pMgr, wl_resource* surf, uint32_t id) { this->onGetFeedback(pMgr, surf, id); }); + RESOURCE->sendClockId(CLOCK_MONOTONIC); } void CPresentationProtocol::onManagerResourceDestroy(wl_resource* res) { @@ -110,7 +109,7 @@ void CPresentationProtocol::onGetFeedback(CWpPresentation* pMgr, wl_resource* su } } -void CPresentationProtocol::onPresented(PHLMONITOR pMonitor, const Time::steady_tp& when, uint32_t untilRefreshNs, uint64_t seq, uint32_t reportedFlags) { +void CPresentationProtocol::onPresented(PHLMONITOR pMonitor, const timespec& when, uint32_t untilRefreshNs, uint64_t seq, uint32_t reportedFlags) { for (auto const& feedback : m_feedbacks) { if (!feedback->m_surface) continue; diff --git a/src/protocols/PresentationTime.hpp b/src/protocols/PresentationTime.hpp index c348c175..caf63ace 100644 --- a/src/protocols/PresentationTime.hpp +++ b/src/protocols/PresentationTime.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include "WaylandProtocol.hpp" @@ -37,7 +38,7 @@ class CPresentationFeedback { bool good(); - void sendQueued(WP data, const Time::steady_tp& when, uint32_t untilRefreshNs, uint64_t seq, uint32_t reportedFlags); + void sendQueued(WP data, const timespec& when, uint32_t untilRefreshNs, uint64_t seq, uint32_t reportedFlags); private: UP m_resource; @@ -53,7 +54,7 @@ class CPresentationProtocol : public IWaylandProtocol { virtual void bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id); - void onPresented(PHLMONITOR pMonitor, const Time::steady_tp& when, uint32_t untilRefreshNs, uint64_t seq, uint32_t reportedFlags); + void onPresented(PHLMONITOR pMonitor, const timespec& when, uint32_t untilRefreshNs, uint64_t seq, uint32_t reportedFlags); void queueData(UP&& data); private: diff --git a/src/protocols/core/Compositor.cpp b/src/protocols/core/Compositor.cpp index 2fd586a8..2aa72d97 100644 --- a/src/protocols/core/Compositor.cpp +++ b/src/protocols/core/Compositor.cpp @@ -626,6 +626,30 @@ bool CWLSurfaceResource::hasVisibleSubsurface() { return false; } +bool CWLSurfaceResource::isTearing() { + if (m_enteredOutputs.empty() && m_hlSurface) { + for (auto& m : g_pCompositor->m_monitors) { + if (!m || !m->m_enabled) + continue; + + auto box = m_hlSurface->getSurfaceBoxGlobal(); + if (box && !box->intersection({m->m_position, m->m_size}).empty()) { + if (m->m_tearingState.activelyTearing) + return true; + } + } + } else { + for (auto& m : m_enteredOutputs) { + if (!m) + continue; + + if (m->m_tearingState.activelyTearing) + return true; + } + } + return false; +} + void CWLSurfaceResource::updateCursorShm(CRegion damage) { if (damage.empty()) return; @@ -670,8 +694,14 @@ void CWLSurfaceResource::presentFeedback(const Time::steady_tp& when, PHLMONITOR FEEDBACK->attachMonitor(pMonitor); if (discarded) FEEDBACK->discarded(); - else + else { FEEDBACK->presented(); + if (!pMonitor->m_lastScanout.expired()) { + const auto WINDOW = m_hlSurface ? Desktop::View::CWindow::fromView(m_hlSurface->view()) : nullptr; + if (WINDOW == pMonitor->m_lastScanout) + FEEDBACK->setPresentationType(true); + } + } PROTO::presentation->queueData(std::move(FEEDBACK)); } diff --git a/src/protocols/core/Compositor.hpp b/src/protocols/core/Compositor.hpp index 89bfb31b..b5357520 100644 --- a/src/protocols/core/Compositor.hpp +++ b/src/protocols/core/Compositor.hpp @@ -128,6 +128,7 @@ class CWLSurfaceResource { NColorManagement::PImageDescription getPreferredImageDescription(); void sortSubsurfaces(); bool hasVisibleSubsurface(); + bool isTearing(); // returns a pair: found surface (null if not found) and surface local coords. // localCoords param is relative to 0,0 of this surface diff --git a/src/protocols/types/SurfaceState.cpp b/src/protocols/types/SurfaceState.cpp index a85a3c44..46f2a563 100644 --- a/src/protocols/types/SurfaceState.cpp +++ b/src/protocols/types/SurfaceState.cpp @@ -67,6 +67,9 @@ void SSurfaceState::reset() { barrierSet = false; surfaceLocked = false; fifoScheduled = false; + + pendingTimeout.reset(); + timer.reset(); // CEventLoopManager::nudgeTimers should handle it eventually } void SSurfaceState::updateFrom(SSurfaceState& ref) { diff --git a/src/protocols/types/SurfaceState.hpp b/src/protocols/types/SurfaceState.hpp index 315fa4fc..f6caa83c 100644 --- a/src/protocols/types/SurfaceState.hpp +++ b/src/protocols/types/SurfaceState.hpp @@ -1,6 +1,8 @@ #pragma once #include "../../helpers/math/Math.hpp" +#include "../../helpers/time/Time.hpp" +#include "../../managers/eventLoop/EventLoopTimer.hpp" #include "../WaylandProtocol.hpp" #include "./Buffer.hpp" @@ -94,6 +96,10 @@ struct SSurfaceState { bool surfaceLocked = false; bool fifoScheduled = false; + // commit timing + std::optional pendingTimeout; + SP timer; + // helpers CRegion accumulateBufferDamage(); // transforms state.damage and merges it into state.bufferDamage void updateFrom(SSurfaceState& ref); // updates this state based on a reference state.