From 3c82d5a919f43aaa314b459e721eed69a76bfdd2 Mon Sep 17 00:00:00 2001 From: Xaver Hugl Date: Wed, 20 Mar 2024 16:28:05 +0100 Subject: [PATCH] core: introduce DrmDevice --- src/CMakeLists.txt | 2 ++ src/core/drmdevice.cpp | 76 ++++++++++++++++++++++++++++++++++++++++++ src/core/drmdevice.h | 45 +++++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 src/core/drmdevice.cpp create mode 100644 src/core/drmdevice.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fee195f186..f25a88142b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -47,6 +47,7 @@ target_sources(kwin PRIVATE core/colorpipelinestage.cpp core/colorspace.cpp core/colortransformation.cpp + core/drmdevice.cpp core/gbmgraphicsbufferallocator.cpp core/graphicsbuffer.cpp core/graphicsbufferallocator.cpp @@ -549,6 +550,7 @@ install(FILES core/colorpipelinestage.h core/colorspace.h core/colortransformation.h + core/drmdevice.h core/gbmgraphicsbufferallocator.h core/graphicsbuffer.h core/graphicsbufferallocator.h diff --git a/src/core/drmdevice.cpp b/src/core/drmdevice.cpp new file mode 100644 index 0000000000..95519e1f18 --- /dev/null +++ b/src/core/drmdevice.cpp @@ -0,0 +1,76 @@ +/* + KWin - the KDE window manager + This file is part of the KDE project. + + SPDX-FileCopyrightText: 2024 Xaver Hugl + + SPDX-License-Identifier: GPL-2.0-or-later +*/ +#include "drmdevice.h" + +#include "gbmgraphicsbufferallocator.h" + +#include +#include +#include + +namespace KWin +{ + +DrmDevice::DrmDevice(const QString &path, dev_t id, FileDescriptor &&fd, gbm_device *gbmDevice) + : m_path(path) + , m_id(id) + , m_fd(std::move(fd)) + , m_gbmDevice(gbmDevice) + , m_allocator(std::make_unique(gbmDevice)) +{ +} + +DrmDevice::~DrmDevice() +{ + gbm_device_destroy(m_gbmDevice); +} + +QString DrmDevice::path() const +{ + return m_path; +} + +dev_t DrmDevice::deviceId() const +{ + return m_id; +} + +gbm_device *DrmDevice::gbmDevice() const +{ + return m_gbmDevice; +} + +GraphicsBufferAllocator *DrmDevice::allocator() const +{ + return m_allocator.get(); +} + +int DrmDevice::fileDescriptor() const +{ + return m_fd.get(); +} + +std::unique_ptr DrmDevice::open(const QString &path) +{ + FileDescriptor fd{::open(path.toLocal8Bit(), O_RDWR | O_CLOEXEC)}; + if (!fd.isValid()) { + return nullptr; + } + struct stat buf; + if (fstat(fd.get(), &buf) == -1) { + return nullptr; + } + gbm_device *device = gbm_create_device(fd.get()); + if (!device) { + return nullptr; + } + return std::unique_ptr(new DrmDevice(path, buf.st_rdev, std::move(fd), device)); +} + +} diff --git a/src/core/drmdevice.h b/src/core/drmdevice.h new file mode 100644 index 0000000000..9f11fd2a88 --- /dev/null +++ b/src/core/drmdevice.h @@ -0,0 +1,45 @@ +/* + KWin - the KDE window manager + This file is part of the KDE project. + + SPDX-FileCopyrightText: 2024 Xaver Hugl + + SPDX-License-Identifier: GPL-2.0-or-later +*/ +#pragma once + +#include + +#include "graphicsbufferallocator.h" +#include "kwin_export.h" +#include "utils/filedescriptor.h" + +struct gbm_device; + +namespace KWin +{ + +class KWIN_EXPORT DrmDevice +{ +public: + ~DrmDevice(); + + QString path() const; + dev_t deviceId() const; + gbm_device *gbmDevice() const; + GraphicsBufferAllocator *allocator() const; + int fileDescriptor() const; + + static std::unique_ptr open(const QString &path); + +private: + explicit DrmDevice(const QString &path, dev_t id, FileDescriptor &&fd, gbm_device *gbmDevice); + + const QString m_path; + const dev_t m_id; + const FileDescriptor m_fd; + gbm_device *const m_gbmDevice; + const std::unique_ptr m_allocator; +}; + +}