core: introduce DrmDevice

This commit is contained in:
Xaver Hugl 2024-03-20 16:28:05 +01:00
parent 831ca28f93
commit 3c82d5a919
3 changed files with 123 additions and 0 deletions

View file

@ -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

76
src/core/drmdevice.cpp Normal file
View file

@ -0,0 +1,76 @@
/*
KWin - the KDE window manager
This file is part of the KDE project.
SPDX-FileCopyrightText: 2024 Xaver Hugl <xaver.hugl@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "drmdevice.h"
#include "gbmgraphicsbufferallocator.h"
#include <fcntl.h>
#include <gbm.h>
#include <sys/stat.h>
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<GbmGraphicsBufferAllocator>(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> 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<DrmDevice>(new DrmDevice(path, buf.st_rdev, std::move(fd), device));
}
}

45
src/core/drmdevice.h Normal file
View file

@ -0,0 +1,45 @@
/*
KWin - the KDE window manager
This file is part of the KDE project.
SPDX-FileCopyrightText: 2024 Xaver Hugl <xaver.hugl@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include <QString>
#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<DrmDevice> 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<GraphicsBufferAllocator> m_allocator;
};
}