Skip to content

Commit

Permalink
syncobj: lets optimize it further
Browse files Browse the repository at this point in the history
lets use unique_pointers all over, add defaulted destructors and make
fromResource directly check existing timelines instead of C style
casting and return a WP from that. instead of moving the releasepoint
vector lets swap it.
  • Loading branch information
gulafaran committed Feb 22, 2025
1 parent 46dbd22 commit 88d328e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
31 changes: 18 additions & 13 deletions src/protocols/DRMSyncobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
#include <fcntl.h>
using namespace Hyprutils::OS;

CDRMSyncobjSurfaceResource::CDRMSyncobjSurfaceResource(SP<CWpLinuxDrmSyncobjSurfaceV1> resource_, SP<CWLSurfaceResource> surface_) : surface(surface_), resource(resource_) {
CDRMSyncobjSurfaceResource::CDRMSyncobjSurfaceResource(UP<CWpLinuxDrmSyncobjSurfaceV1>&& resource_, SP<CWLSurfaceResource> surface_) :
surface(surface_), resource(std::move(resource_)) {
if UNLIKELY (!good())
return;

Expand Down Expand Up @@ -107,7 +108,7 @@ bool CDRMSyncobjSurfaceResource::good() {
return resource->resource();
}

CDRMSyncobjTimelineResource::CDRMSyncobjTimelineResource(SP<CWpLinuxDrmSyncobjTimelineV1> resource_, CFileDescriptor&& fd_) : fd(std::move(fd_)), resource(resource_) {
CDRMSyncobjTimelineResource::CDRMSyncobjTimelineResource(UP<CWpLinuxDrmSyncobjTimelineV1>&& resource_, CFileDescriptor&& fd_) : fd(std::move(fd_)), resource(std::move(resource_)) {
if UNLIKELY (!good())
return;

Expand All @@ -124,16 +125,20 @@ CDRMSyncobjTimelineResource::CDRMSyncobjTimelineResource(SP<CWpLinuxDrmSyncobjTi
}
}

SP<CDRMSyncobjTimelineResource> CDRMSyncobjTimelineResource::fromResource(wl_resource* res) {
auto data = (CDRMSyncobjTimelineResource*)(((CWpLinuxDrmSyncobjTimelineV1*)wl_resource_get_user_data(res))->data());
return data ? data->self.lock() : nullptr;
WP<CDRMSyncobjTimelineResource> CDRMSyncobjTimelineResource::fromResource(wl_resource* res) {
for (auto& t : PROTO::sync->m_vTimelines) {
if (t && t->resource && t->resource->resource() == res)
return t;
}

return {};
}

bool CDRMSyncobjTimelineResource::good() {
return resource->resource();
}

CDRMSyncobjManagerResource::CDRMSyncobjManagerResource(SP<CWpLinuxDrmSyncobjManagerV1> resource_) : resource(resource_) {
CDRMSyncobjManagerResource::CDRMSyncobjManagerResource(UP<CWpLinuxDrmSyncobjManagerV1>&& resource_) : resource(std::move(resource_)) {
if UNLIKELY (!good())
return;

Expand All @@ -157,28 +162,28 @@ CDRMSyncobjManagerResource::CDRMSyncobjManagerResource(SP<CWpLinuxDrmSyncobjMana
return;
}

auto RESOURCE = makeShared<CDRMSyncobjSurfaceResource>(makeShared<CWpLinuxDrmSyncobjSurfaceV1>(resource->client(), resource->version(), id), SURF);
const auto& RESOURCE = PROTO::sync->m_vSurfaces.emplace_back(
makeUnique<CDRMSyncobjSurfaceResource>(makeUnique<CWpLinuxDrmSyncobjSurfaceV1>(resource->client(), resource->version(), id), SURF));
if UNLIKELY (!RESOURCE->good()) {
resource->noMemory();
PROTO::sync->m_vSurfaces.pop_back();
return;
}

PROTO::sync->m_vSurfaces.emplace_back(RESOURCE);
SURF->syncobj = RESOURCE;

LOGM(LOG, "New linux_syncobj at {:x} for surface {:x}", (uintptr_t)RESOURCE.get(), (uintptr_t)SURF.get());
});

resource->setImportTimeline([this](CWpLinuxDrmSyncobjManagerV1* r, uint32_t id, int32_t fd) {
auto RESOURCE = makeShared<CDRMSyncobjTimelineResource>(makeShared<CWpLinuxDrmSyncobjTimelineV1>(resource->client(), resource->version(), id), CFileDescriptor{fd});
const auto& RESOURCE = PROTO::sync->m_vTimelines.emplace_back(
makeUnique<CDRMSyncobjTimelineResource>(makeUnique<CWpLinuxDrmSyncobjTimelineV1>(resource->client(), resource->version(), id), CFileDescriptor{fd}));
if UNLIKELY (!RESOURCE->good()) {
resource->noMemory();
PROTO::sync->m_vTimelines.pop_back();
return;
}

PROTO::sync->m_vTimelines.emplace_back(RESOURCE);
RESOURCE->self = RESOURCE;

LOGM(LOG, "New linux_drm_timeline at {:x}", (uintptr_t)RESOURCE.get());
});
}
Expand All @@ -192,7 +197,7 @@ CDRMSyncobjProtocol::CDRMSyncobjProtocol(const wl_interface* iface, const int& v
}

void CDRMSyncobjProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) {
const auto RESOURCE = m_vManagers.emplace_back(makeShared<CDRMSyncobjManagerResource>(makeShared<CWpLinuxDrmSyncobjManagerV1>(client, ver, id)));
const auto& RESOURCE = m_vManagers.emplace_back(makeUnique<CDRMSyncobjManagerResource>(makeUnique<CWpLinuxDrmSyncobjManagerV1>(client, ver, id)));

if UNLIKELY (!RESOURCE->good()) {
wl_client_post_no_memory(client);
Expand Down
22 changes: 11 additions & 11 deletions src/protocols/DRMSyncobj.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CSyncTimeline;

class CDRMSyncobjSurfaceResource {
public:
CDRMSyncobjSurfaceResource(SP<CWpLinuxDrmSyncobjSurfaceV1> resource_, SP<CWLSurfaceResource> surface_);
CDRMSyncobjSurfaceResource(UP<CWpLinuxDrmSyncobjSurfaceV1>&& resource_, SP<CWLSurfaceResource> surface_);
~CDRMSyncobjSurfaceResource();

bool good();
Expand All @@ -34,7 +34,7 @@ class CDRMSyncobjSurfaceResource {
std::vector<UP<CSyncReleaser>> releasePoints;

private:
SP<CWpLinuxDrmSyncobjSurfaceV1> resource;
UP<CWpLinuxDrmSyncobjSurfaceV1> resource;
bool acquireWaiting = false;

struct {
Expand All @@ -45,33 +45,33 @@ class CDRMSyncobjSurfaceResource {

class CDRMSyncobjTimelineResource {
public:
CDRMSyncobjTimelineResource(SP<CWpLinuxDrmSyncobjTimelineV1> resource_, Hyprutils::OS::CFileDescriptor&& fd_);
CDRMSyncobjTimelineResource(UP<CWpLinuxDrmSyncobjTimelineV1>&& resource_, Hyprutils::OS::CFileDescriptor&& fd_);
~CDRMSyncobjTimelineResource() = default;
static SP<CDRMSyncobjTimelineResource> fromResource(wl_resource*);
static WP<CDRMSyncobjTimelineResource> fromResource(wl_resource*);

bool good();

WP<CDRMSyncobjTimelineResource> self;
Hyprutils::OS::CFileDescriptor fd;
SP<CSyncTimeline> timeline;

private:
SP<CWpLinuxDrmSyncobjTimelineV1> resource;
UP<CWpLinuxDrmSyncobjTimelineV1> resource;
};

class CDRMSyncobjManagerResource {
public:
CDRMSyncobjManagerResource(SP<CWpLinuxDrmSyncobjManagerV1> resource_);
CDRMSyncobjManagerResource(UP<CWpLinuxDrmSyncobjManagerV1>&& resource_);

bool good();

private:
SP<CWpLinuxDrmSyncobjManagerV1> resource;
UP<CWpLinuxDrmSyncobjManagerV1> resource;
};

class CDRMSyncobjProtocol : public IWaylandProtocol {
public:
CDRMSyncobjProtocol(const wl_interface* iface, const int& ver, const std::string& name);
~CDRMSyncobjProtocol() = default;

virtual void bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id);

Expand All @@ -81,9 +81,9 @@ class CDRMSyncobjProtocol : public IWaylandProtocol {
void destroyResource(CDRMSyncobjSurfaceResource* resource);

//
std::vector<SP<CDRMSyncobjManagerResource>> m_vManagers;
std::vector<SP<CDRMSyncobjTimelineResource>> m_vTimelines;
std::vector<SP<CDRMSyncobjSurfaceResource>> m_vSurfaces;
std::vector<UP<CDRMSyncobjManagerResource>> m_vManagers;
std::vector<UP<CDRMSyncobjTimelineResource>> m_vTimelines;
std::vector<UP<CDRMSyncobjSurfaceResource>> m_vSurfaces;

//
int drmFD = -1;
Expand Down
2 changes: 1 addition & 1 deletion src/protocols/core/Compositor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ void CWLSurfaceResource::commitPendingState() {
events.roleCommit.emit();

if (syncobj && syncobj->release.resource && syncobj->release.resource->timeline && current.buffer && current.buffer->buffer)
current.buffer->releaser = std::move(syncobj->releasePoints);
current.buffer->releaser.swap(syncobj->releasePoints);

if (current.texture)
current.texture->m_eTransform = wlTransformToHyprutils(current.transform);
Expand Down

0 comments on commit 88d328e

Please sign in to comment.