Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/aquamarine/backend/DRM.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ namespace Aquamarine {
};

struct SDRMPageFlip {
uint64_t queuedSequence = 0;
std::optional<std::pair<uint64_t, timespec>> presentTime = std::nullopt;
Hyprutils::Memory::CWeakPointer<SDRMConnector> connector;
};

Expand Down Expand Up @@ -296,6 +298,7 @@ namespace Aquamarine {
Hyprutils::Memory::CSharedPointer<CDRMFB> pendingCursorFB;

bool isPageFlipPending = false;
bool isFrameRunning = false;
SDRMPageFlip pendingPageFlip;
bool frameEventScheduled = false;

Expand Down
70 changes: 53 additions & 17 deletions src/backend/drm/DRM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -921,45 +921,82 @@ int Aquamarine::CDRMBackend::drmRenderNodeFD() {
return gpu->renderNodeFd;
}

static void handlePF(int fd, unsigned seq, unsigned tv_sec, unsigned tv_usec, unsigned crtc_id, void* data) {
auto pageFlip = (SDRMPageFlip*)data;
static void present(SDRMPageFlip* pageFlip) {
pageFlip->connector->isPageFlipPending = false;
const auto& BACKEND = pageFlip->connector->backend;

if (!pageFlip || !pageFlip->connector)
if (!pageFlip->presentTime) {
TRACE(BACKEND->log(AQ_LOG_ERROR, "drm: present event missing presentTime"));
return;
}

pageFlip->connector->isPageFlipPending = false;

const auto& BACKEND = pageFlip->connector->backend;
auto& presentTime = pageFlip->presentTime.value();

TRACE(BACKEND->log(AQ_LOG_TRACE, std::format("drm: pf event seq {} sec {} usec {} crtc {}", seq, tv_sec, tv_usec, crtc_id)));
TRACE(BACKEND->log(
AQ_LOG_TRACE,
std::format("drm: present event seq {} sec {} usec {} crtc {}", presentTime.first, presentTime.second.tv_sec, presentTime.second.tv_nsec, pageFlip->connector->crtc->id)));

if (pageFlip->connector->status != DRM_MODE_CONNECTED || !pageFlip->connector->crtc) {
BACKEND->log(AQ_LOG_DEBUG, "drm: Ignoring a pf event from a disabled crtc / connector");
BACKEND->log(AQ_LOG_DEBUG, "drm: Ignoring a present event from a disabled crtc / connector");
return;
}

if (BACKEND->sessionActive() && pageFlip->connector->output->enabledState)
pageFlip->connector->isFrameRunning = true;

pageFlip->connector->onPresent();

uint32_t flags = IOutput::AQ_OUTPUT_PRESENT_VSYNC | IOutput::AQ_OUTPUT_PRESENT_HW_CLOCK | IOutput::AQ_OUTPUT_PRESENT_HW_COMPLETION | IOutput::AQ_OUTPUT_PRESENT_ZEROCOPY;

timespec presented = {.tv_sec = (time_t)tv_sec, .tv_nsec = (long)(tv_usec * 1000)};

pageFlip->connector->output->events.present.emit(IOutput::SPresentEvent{
.presented = BACKEND->sessionActive(),
.when = &presented,
.seq = seq,
.refresh = (int)(pageFlip->connector->refresh ? (1000000000000LL / pageFlip->connector->refresh) : 0),
.when = &presentTime.second,
.seq = sc<unsigned int>(presentTime.first),
.refresh = sc<int>(pageFlip->connector->refresh ? (1000000000000LL / pageFlip->connector->refresh) : 0),
.flags = flags,
});

if (BACKEND->sessionActive() && !pageFlip->connector->frameEventScheduled && pageFlip->connector->output->enabledState)
if (BACKEND->sessionActive() && pageFlip->connector->output->enabledState) {
pageFlip->connector->output->events.frame.emit();
pageFlip->connector->isFrameRunning = false;
}
}

static void handlePF(int fd, unsigned seq, unsigned tv_sec, unsigned tv_usec, unsigned crtc_id, void* data) {
auto pageFlip = rc<SDRMPageFlip*>(data);

if (!pageFlip || !pageFlip->connector)
return;

if (!pageFlip->presentTime || pageFlip->presentTime->first != seq) {
TRACE(pageFlip->connector->backend->log(AQ_LOG_TRACE, std::format("drm: handlePF event seq {} didnt have a timestamp from handleCRTCSequence", seq)));
pageFlip->presentTime = {seq, {.tv_sec = (time_t)tv_sec, .tv_nsec = (long)(tv_usec * 1000)}};
}

auto& presentTime = pageFlip->presentTime.value();
TRACE(pageFlip->connector->backend->log(
AQ_LOG_TRACE,
std::format("drm: handlePF event seq {} sec {} usec {} crtc {}", presentTime.first, presentTime.second.tv_sec, presentTime.second.tv_nsec, pageFlip->connector->crtc->id)));

present(pageFlip);
}

static void handleCRTCSequence(int fd, uint64_t seq, uint64_t ns, uint64_t data) {
auto* pageFlip = rc<SDRMPageFlip*>(data);

if (!pageFlip || !pageFlip->connector || pageFlip->queuedSequence != seq)
return;

TRACE(pageFlip->connector->backend->log(AQ_LOG_TRACE, std::format("drm: handleCRTCSequence event seq {} ns {}", seq, ns)));

pageFlip->presentTime = {seq, {.tv_sec = sc<time_t>(ns / 1'000'000'000), .tv_nsec = sc<long>(ns % 1'000'000'000)}};
}

bool Aquamarine::CDRMBackend::dispatchEvents() {
drmEventContext event = {
.version = 3,
.version = 4,
.page_flip_handler2 = ::handlePF,
.sequence_handler = ::handleCRTCSequence,
};

if (drmHandleEvent(gpu->fd, &event) != 0)
Expand Down Expand Up @@ -1954,11 +1991,10 @@ void Aquamarine::CDRMOutput::scheduleFrame(const scheduleFrameReason reason) {
connector->isPageFlipPending, connector->frameEventScheduled)));
needsFrame = true;

if (connector->isPageFlipPending || connector->frameEventScheduled || !enabledState)
if (connector->isPageFlipPending || connector->isFrameRunning || connector->frameEventScheduled || !enabledState)
return;

connector->frameEventScheduled = true;

backend->backend->addIdleEvent(frameIdle);
}

Expand Down
12 changes: 12 additions & 0 deletions src/backend/drm/impl/Atomic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,18 @@ bool Aquamarine::CDRMAtomicRequest::commit(uint32_t flagssss) {
return false;
}

if (conn && flagssss & DRM_MODE_PAGE_FLIP_EVENT) {
uint64_t prevSeq, prevNs;
if (drmCrtcGetSequence(backend->gpu->fd, conn->crtc->id, &prevSeq, &prevNs) == 0) {
if (drmCrtcQueueSequence(backend->gpu->fd, conn->crtc->id, DRM_CRTC_SEQUENCE_NEXT_ON_MISS, prevSeq + 1, &conn->pendingPageFlip.queuedSequence,
rc<uint64_t>(&conn->pendingPageFlip)) != 0) {
backend->log(AQ_LOG_TRACE, "atomic drm request: failed to queue crtc sequence");
conn->pendingPageFlip.queuedSequence = 0; // fallback to page_flip_handler2
}
} else
backend->log(AQ_LOG_TRACE, "atomic drm request: failed to get crtc sequence");
}

if (auto ret = drmModeAtomicCommit(backend->gpu->fd, req, flagssss, conn ? &conn->pendingPageFlip : nullptr); ret) {
backend->log((flagssss & DRM_MODE_ATOMIC_TEST_ONLY) ? AQ_LOG_DEBUG : AQ_LOG_ERROR,
std::format("atomic drm request: failed to commit: {}, flags: {}", strerror(ret == -1 ? errno : -ret), flagsToStr(flagssss)));
Expand Down