2021-05-04 10:19:31 +00:00
|
|
|
/*
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
|
|
|
SPDX-FileCopyrightText: 2021 Xaver Hugl <xaver.hugl@gmail.com>
|
|
|
|
|
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "dumb_swapchain.h"
|
|
|
|
#include "drm_gpu.h"
|
2021-11-22 10:43:28 +00:00
|
|
|
#include "drm_buffer.h"
|
2021-05-04 10:19:31 +00:00
|
|
|
#include "logging.h"
|
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2021-12-02 16:51:08 +00:00
|
|
|
DumbSwapchain::DumbSwapchain(DrmGpu *gpu, const QSize &size, uint32_t drmFormat, QImage::Format imageFormat)
|
2021-05-04 10:19:31 +00:00
|
|
|
: m_size(size)
|
2021-12-02 16:51:08 +00:00
|
|
|
, m_format(drmFormat)
|
2021-05-04 10:19:31 +00:00
|
|
|
{
|
2021-06-08 19:11:39 +00:00
|
|
|
for (int i = 0; i < 2; i++) {
|
2021-12-02 16:51:08 +00:00
|
|
|
auto buffer = QSharedPointer<DrmDumbBuffer>::create(gpu, size, drmFormat);
|
2021-05-04 10:19:31 +00:00
|
|
|
if (!buffer->bufferId()) {
|
|
|
|
break;
|
|
|
|
}
|
2021-12-02 16:51:08 +00:00
|
|
|
if (!buffer->map(imageFormat)) {
|
2021-05-04 10:19:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
buffer->image()->fill(Qt::black);
|
2021-07-24 09:08:30 +00:00
|
|
|
m_slots.append(Slot{.buffer = buffer, .age = 0,});
|
2021-05-04 10:19:31 +00:00
|
|
|
}
|
2021-07-24 09:08:30 +00:00
|
|
|
if (m_slots.count() < 2) {
|
2021-05-23 17:45:05 +00:00
|
|
|
qCWarning(KWIN_DRM) << "Failed to create dumb buffers for swapchain!";
|
2021-07-24 09:08:30 +00:00
|
|
|
m_slots.clear();
|
2021-05-04 10:19:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-24 09:08:30 +00:00
|
|
|
QSharedPointer<DrmDumbBuffer> DumbSwapchain::acquireBuffer(int *age)
|
2021-05-04 10:19:31 +00:00
|
|
|
{
|
2021-07-24 09:08:30 +00:00
|
|
|
if (m_slots.isEmpty()) {
|
2021-05-04 10:19:31 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2021-07-24 09:08:30 +00:00
|
|
|
index = (index + 1) % m_slots.count();
|
|
|
|
if (age) {
|
|
|
|
*age = m_slots[index].age;
|
|
|
|
}
|
|
|
|
return m_slots[index].buffer;
|
2021-05-04 10:19:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QSharedPointer<DrmDumbBuffer> DumbSwapchain::currentBuffer() const
|
|
|
|
{
|
2021-07-24 09:08:30 +00:00
|
|
|
return m_slots[index].buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DumbSwapchain::releaseBuffer(QSharedPointer<DrmDumbBuffer> buffer)
|
|
|
|
{
|
|
|
|
Q_ASSERT(m_slots[index].buffer == buffer);
|
|
|
|
|
|
|
|
for (qsizetype i = 0; i < m_slots.count(); ++i) {
|
|
|
|
if (m_slots[i].buffer == buffer) {
|
|
|
|
m_slots[i].age = 1;
|
|
|
|
} else if (m_slots[i].age > 0) {
|
|
|
|
m_slots[i].age++;
|
|
|
|
}
|
|
|
|
}
|
2021-05-04 10:19:31 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 16:51:08 +00:00
|
|
|
uint32_t DumbSwapchain::drmFormat() const
|
|
|
|
{
|
|
|
|
return m_format;
|
|
|
|
}
|
|
|
|
|
2021-12-14 15:32:55 +00:00
|
|
|
qsizetype DumbSwapchain::slotCount() const
|
|
|
|
{
|
|
|
|
return m_slots.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize DumbSwapchain::size() const
|
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DumbSwapchain::isEmpty() const
|
|
|
|
{
|
|
|
|
return m_slots.isEmpty();
|
|
|
|
}
|
|
|
|
|
2021-05-04 10:19:31 +00:00
|
|
|
}
|