From 1833d790f576572af108e6d42ea2883ba2ec1c12 Mon Sep 17 00:00:00 2001 From: Vlad Zahorodnii Date: Mon, 5 Jun 2023 13:19:13 +0300 Subject: [PATCH] core: Introduce GraphicsBufferOptions GraphicsBufferOptions describes the properties of the allocated graphics buffer. --- src/backends/virtual/virtual_egl_backend.cpp | 5 ++++- .../virtual/virtual_qpainter_backend.cpp | 5 ++++- src/backends/wayland/wayland_egl_backend.cpp | 6 ++++- .../wayland/wayland_qpainter_backend.cpp | 5 ++++- .../x11/windowed/x11_windowed_egl_backend.cpp | 6 ++++- .../x11_windowed_qpainter_backend.cpp | 5 ++++- src/core/gbmgraphicsbufferallocator.cpp | 22 +++++++++---------- src/core/gbmgraphicsbufferallocator.h | 2 +- src/core/graphicsbufferallocator.h | 17 +++++++++++++- src/core/shmgraphicsbufferallocator.cpp | 14 ++++++------ src/core/shmgraphicsbufferallocator.h | 2 +- 11 files changed, 62 insertions(+), 27 deletions(-) diff --git a/src/backends/virtual/virtual_egl_backend.cpp b/src/backends/virtual/virtual_egl_backend.cpp index d3c8b94bd4..a21d419607 100644 --- a/src/backends/virtual/virtual_egl_backend.cpp +++ b/src/backends/virtual/virtual_egl_backend.cpp @@ -71,7 +71,10 @@ std::shared_ptr VirtualEglSwapchain::acquire() } } - GbmGraphicsBuffer *graphicsBuffer = m_allocator->allocate(m_size, m_format); + GbmGraphicsBuffer *graphicsBuffer = m_allocator->allocate(GraphicsBufferOptions{ + .size = m_size, + .format = m_format, + }); if (!graphicsBuffer) { qCWarning(KWIN_VIRTUAL) << "Failed to allocate layer swapchain buffer"; return nullptr; diff --git a/src/backends/virtual/virtual_qpainter_backend.cpp b/src/backends/virtual/virtual_qpainter_backend.cpp index ffa9131fb9..ff8af606c3 100644 --- a/src/backends/virtual/virtual_qpainter_backend.cpp +++ b/src/backends/virtual/virtual_qpainter_backend.cpp @@ -77,7 +77,10 @@ std::shared_ptr VirtualQPainterSwapchain::acquire() } } - ShmGraphicsBuffer *buffer = m_allocator->allocate(m_size, m_format); + ShmGraphicsBuffer *buffer = m_allocator->allocate(GraphicsBufferOptions{ + .size = m_size, + .format = m_format, + }); if (!buffer) { qCDebug(KWIN_VIRTUAL) << "Did not get a new Buffer from Shm Pool"; return nullptr; diff --git a/src/backends/wayland/wayland_egl_backend.cpp b/src/backends/wayland/wayland_egl_backend.cpp index 8c127f411e..bef76243de 100644 --- a/src/backends/wayland/wayland_egl_backend.cpp +++ b/src/backends/wayland/wayland_egl_backend.cpp @@ -90,7 +90,11 @@ std::shared_ptr WaylandEglLayerSwapchain::acquire() } } - GbmGraphicsBuffer *graphicsBuffer = m_allocator->allocate(m_size, m_format, m_modifiers); + GbmGraphicsBuffer *graphicsBuffer = m_allocator->allocate(GraphicsBufferOptions{ + .size = m_size, + .format = m_format, + .modifiers = m_modifiers, + }); if (!graphicsBuffer) { qCWarning(KWIN_WAYLAND_BACKEND) << "Failed to allocate layer swapchain buffer"; return nullptr; diff --git a/src/backends/wayland/wayland_qpainter_backend.cpp b/src/backends/wayland/wayland_qpainter_backend.cpp index f4e1bcbcdd..d9c1ab1ae0 100644 --- a/src/backends/wayland/wayland_qpainter_backend.cpp +++ b/src/backends/wayland/wayland_qpainter_backend.cpp @@ -81,7 +81,10 @@ std::shared_ptr WaylandQPainterSwapchain::acquire() } } - ShmGraphicsBuffer *buffer = m_allocator->allocate(m_size, m_format); + ShmGraphicsBuffer *buffer = m_allocator->allocate(GraphicsBufferOptions{ + .size = m_size, + .format = m_format, + }); if (!buffer) { qCDebug(KWIN_WAYLAND_BACKEND) << "Did not get a new Buffer from Shm Pool"; return nullptr; diff --git a/src/backends/x11/windowed/x11_windowed_egl_backend.cpp b/src/backends/x11/windowed/x11_windowed_egl_backend.cpp index 9708ba4847..e48cc0bcaa 100644 --- a/src/backends/x11/windowed/x11_windowed_egl_backend.cpp +++ b/src/backends/x11/windowed/x11_windowed_egl_backend.cpp @@ -79,7 +79,11 @@ std::shared_ptr X11WindowedEglLayerSwapchain::acquire } } - GbmGraphicsBuffer *graphicsBuffer = m_allocator->allocate(m_size, m_format, m_modifiers); + GbmGraphicsBuffer *graphicsBuffer = m_allocator->allocate(GraphicsBufferOptions{ + .size = m_size, + .format = m_format, + .modifiers = m_modifiers, + }); if (!graphicsBuffer) { qCWarning(KWIN_X11WINDOWED) << "Failed to allocate layer swapchain buffer"; return nullptr; diff --git a/src/backends/x11/windowed/x11_windowed_qpainter_backend.cpp b/src/backends/x11/windowed/x11_windowed_qpainter_backend.cpp index 4305e3f81e..bf968260b0 100644 --- a/src/backends/x11/windowed/x11_windowed_qpainter_backend.cpp +++ b/src/backends/x11/windowed/x11_windowed_qpainter_backend.cpp @@ -88,7 +88,10 @@ std::shared_ptr X11WindowedQPainterLayerSwapchai } } - ShmGraphicsBuffer *graphicsBuffer = m_allocator->allocate(m_size, m_format); + ShmGraphicsBuffer *graphicsBuffer = m_allocator->allocate(GraphicsBufferOptions{ + .size = m_size, + .format = m_format, + }); if (!graphicsBuffer) { qCWarning(KWIN_X11WINDOWED) << "Failed to allocate a shared memory graphics buffer"; return nullptr; diff --git a/src/core/gbmgraphicsbufferallocator.cpp b/src/core/gbmgraphicsbufferallocator.cpp index 57145ce9ff..da49668286 100644 --- a/src/core/gbmgraphicsbufferallocator.cpp +++ b/src/core/gbmgraphicsbufferallocator.cpp @@ -22,15 +22,15 @@ GbmGraphicsBufferAllocator::~GbmGraphicsBufferAllocator() { } -GbmGraphicsBuffer *GbmGraphicsBufferAllocator::allocate(const QSize &size, uint32_t format, const QVector &modifiers) +GbmGraphicsBuffer *GbmGraphicsBufferAllocator::allocate(const GraphicsBufferOptions &options) { - if (!modifiers.isEmpty() && !(modifiers.size() == 1 && modifiers.first() == DRM_FORMAT_MOD_INVALID)) { + if (!options.modifiers.isEmpty() && !(options.modifiers.size() == 1 && options.modifiers.first() == DRM_FORMAT_MOD_INVALID)) { gbm_bo *bo = gbm_bo_create_with_modifiers(m_gbmDevice, - size.width(), - size.height(), - format, - modifiers.constData(), - modifiers.size()); + options.size.width(), + options.size.height(), + options.format, + options.modifiers.constData(), + options.modifiers.size()); if (bo) { std::optional attributes = dmaBufAttributesForBo(bo); if (!attributes.has_value()) { @@ -42,16 +42,16 @@ GbmGraphicsBuffer *GbmGraphicsBufferAllocator::allocate(const QSize &size, uint3 } uint32_t flags = GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING; - if (modifiers.size() == 1 && modifiers.first() == DRM_FORMAT_MOD_LINEAR) { + if (options.modifiers.size() == 1 && options.modifiers.first() == DRM_FORMAT_MOD_LINEAR) { flags |= GBM_BO_USE_LINEAR; } else if (!modifiers.contains(DRM_FORMAT_MOD_INVALID)) { return nullptr; } gbm_bo *bo = gbm_bo_create(m_gbmDevice, - size.width(), - size.height(), - format, + options.size.width(), + options.size.height(), + options.format, flags); if (bo) { std::optional attributes = dmaBufAttributesForBo(bo); diff --git a/src/core/gbmgraphicsbufferallocator.h b/src/core/gbmgraphicsbufferallocator.h index 1f191a1f87..ae9091538f 100644 --- a/src/core/gbmgraphicsbufferallocator.h +++ b/src/core/gbmgraphicsbufferallocator.h @@ -40,7 +40,7 @@ public: explicit GbmGraphicsBufferAllocator(gbm_device *device); ~GbmGraphicsBufferAllocator() override; - GbmGraphicsBuffer *allocate(const QSize &size, uint32_t format, const QVector &modifiers = {}) override; + GbmGraphicsBuffer *allocate(const GraphicsBufferOptions &options) override; private: gbm_device *m_gbmDevice; diff --git a/src/core/graphicsbufferallocator.h b/src/core/graphicsbufferallocator.h index 292e5e2b6d..89865ebb7b 100644 --- a/src/core/graphicsbufferallocator.h +++ b/src/core/graphicsbufferallocator.h @@ -16,13 +16,28 @@ namespace KWin class GraphicsBuffer; +/** + * The GraphicsBufferOptions describes the properties of an allocated graphics buffer. + */ +struct GraphicsBufferOptions +{ + /// The size of the buffer, in device pixels. + QSize size; + + /// The pixel format of the buffer, see DRM_FORMAT_*. + uint32_t format; + + /// An optional list of modifiers, see DRM_FORMAT_MOD_*. + QVector modifiers; +}; + class KWIN_EXPORT GraphicsBufferAllocator { public: GraphicsBufferAllocator(); virtual ~GraphicsBufferAllocator(); - virtual GraphicsBuffer *allocate(const QSize &size, uint32_t format, const QVector &modifiers = {}) = 0; + virtual GraphicsBuffer *allocate(const GraphicsBufferOptions &options) = 0; }; } // namespace KWin diff --git a/src/core/shmgraphicsbufferallocator.cpp b/src/core/shmgraphicsbufferallocator.cpp index 46e640f609..d710b25cd2 100644 --- a/src/core/shmgraphicsbufferallocator.cpp +++ b/src/core/shmgraphicsbufferallocator.cpp @@ -36,13 +36,13 @@ const ShmAttributes *ShmGraphicsBuffer::shmAttributes() const return &m_attributes; } -ShmGraphicsBuffer *ShmGraphicsBufferAllocator::allocate(const QSize &size, uint32_t format, const QVector &modifiers) +ShmGraphicsBuffer *ShmGraphicsBufferAllocator::allocate(const GraphicsBufferOptions &options) { - if (!modifiers.isEmpty()) { + if (!options.modifiers.isEmpty()) { return nullptr; } - switch (format) { + switch (options.format) { case DRM_FORMAT_ARGB8888: case DRM_FORMAT_XRGB8888: break; @@ -50,8 +50,8 @@ ShmGraphicsBuffer *ShmGraphicsBufferAllocator::allocate(const QSize &size, uint3 return nullptr; } - const int stride = size.width() * 4; - const int bufferSize = size.height() * stride; + const int stride = options.size.width() * 4; + const int bufferSize = options.size.height() * stride; #if HAVE_MEMFD FileDescriptor fd = FileDescriptor(memfd_create("shm", MFD_CLOEXEC | MFD_ALLOW_SEALING)); @@ -86,8 +86,8 @@ ShmGraphicsBuffer *ShmGraphicsBufferAllocator::allocate(const QSize &size, uint3 .fd = std::move(fd), .stride = stride, .offset = 0, - .size = size, - .format = format, + .size = options.size, + .format = options.format, }); } diff --git a/src/core/shmgraphicsbufferallocator.h b/src/core/shmgraphicsbufferallocator.h index 678163e142..74a94e0743 100644 --- a/src/core/shmgraphicsbufferallocator.h +++ b/src/core/shmgraphicsbufferallocator.h @@ -32,7 +32,7 @@ private: class KWIN_EXPORT ShmGraphicsBufferAllocator : public GraphicsBufferAllocator { public: - ShmGraphicsBuffer *allocate(const QSize &size, uint32_t format, const QVector &modifiers = {}) override; + ShmGraphicsBuffer *allocate(const GraphicsBufferOptions &options) override; }; } // namespace KWin