From ea639ad1709f49aa88658b8c7e572d5c972005f9 Mon Sep 17 00:00:00 2001 From: Vlad Zahorodnii Date: Mon, 5 Jun 2023 13:44:48 +0300 Subject: [PATCH] core: Add GraphicsBuffer map flags This allows to specify whether the graphics buffer will be read or written. It's mainly needed to map our APIs to gbm APIs. --- src/core/gbmgraphicsbufferallocator.cpp | 12 ++++++++++-- src/core/gbmgraphicsbufferallocator.h | 6 +++--- src/core/graphicsbuffer.cpp | 2 +- src/core/graphicsbuffer.h | 10 +++++++++- src/core/graphicsbufferview.cpp | 4 ++-- src/core/graphicsbufferview.h | 6 ++---- src/wayland/shmclientbuffer.cpp | 2 +- src/wayland/shmclientbuffer_p.h | 2 +- 8 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/core/gbmgraphicsbufferallocator.cpp b/src/core/gbmgraphicsbufferallocator.cpp index f2aaed852b..071292d0ae 100644 --- a/src/core/gbmgraphicsbufferallocator.cpp +++ b/src/core/gbmgraphicsbufferallocator.cpp @@ -117,14 +117,22 @@ const DmaBufAttributes *GbmGraphicsBuffer::dmabufAttributes() const return &m_dmabufAttributes; } -void *GbmGraphicsBuffer::map() +void *GbmGraphicsBuffer::map(MapFlags flags) { if (m_mapPtr) { return m_mapPtr; } + uint32_t access = 0; + if (flags & MapFlag::Read) { + access |= GBM_BO_TRANSFER_READ; + } + if (flags & MapFlag::Write) { + access |= GBM_BO_TRANSFER_WRITE; + } + uint32_t stride = 0; - m_mapPtr = gbm_bo_map(m_bo, 0, 0, m_dmabufAttributes.width, m_dmabufAttributes.height, GBM_BO_TRANSFER_READ_WRITE, &stride, &m_mapData); + m_mapPtr = gbm_bo_map(m_bo, 0, 0, m_dmabufAttributes.width, m_dmabufAttributes.height, access, &stride, &m_mapData); return m_mapPtr; } diff --git a/src/core/gbmgraphicsbufferallocator.h b/src/core/gbmgraphicsbufferallocator.h index 0a37d58f80..5c62e390b0 100644 --- a/src/core/gbmgraphicsbufferallocator.h +++ b/src/core/gbmgraphicsbufferallocator.h @@ -23,13 +23,13 @@ public: GbmGraphicsBuffer(DmaBufAttributes attributes, gbm_bo *handle); ~GbmGraphicsBuffer() override; + void *map(MapFlags flags) override; + void unmap() override; + QSize size() const override; bool hasAlphaChannel() const override; const DmaBufAttributes *dmabufAttributes() const override; - void *map() override; - void unmap() override; - private: gbm_bo *m_bo; void *m_mapPtr = nullptr; diff --git a/src/core/graphicsbuffer.cpp b/src/core/graphicsbuffer.cpp index be7a9b5dd3..f06cb23af5 100644 --- a/src/core/graphicsbuffer.cpp +++ b/src/core/graphicsbuffer.cpp @@ -53,7 +53,7 @@ void GraphicsBuffer::drop() } } -void *GraphicsBuffer::map() +void *GraphicsBuffer::map(MapFlags flags) { return nullptr; } diff --git a/src/core/graphicsbuffer.h b/src/core/graphicsbuffer.h index bcd536d91a..daf27ca86f 100644 --- a/src/core/graphicsbuffer.h +++ b/src/core/graphicsbuffer.h @@ -58,7 +58,13 @@ public: void unref(); void drop(); - virtual void *map(); + enum MapFlag { + Read = 0x1, + Write = 0x2, + }; + Q_DECLARE_FLAGS(MapFlags, MapFlag) + + virtual void *map(MapFlags flags); virtual void unmap(); virtual QSize size() const = 0; @@ -78,3 +84,5 @@ protected: }; } // namespace KWin + +Q_DECLARE_OPERATORS_FOR_FLAGS(KWin::GraphicsBuffer::MapFlags) diff --git a/src/core/graphicsbufferview.cpp b/src/core/graphicsbufferview.cpp index e78e098ecf..55c8d51019 100644 --- a/src/core/graphicsbufferview.cpp +++ b/src/core/graphicsbufferview.cpp @@ -39,7 +39,7 @@ static QImage::Format drmFormatToQImageFormat(uint32_t drmFormat) } } -GraphicsBufferView::GraphicsBufferView(GraphicsBuffer *buffer) +GraphicsBufferView::GraphicsBufferView(GraphicsBuffer *buffer, GraphicsBuffer::MapFlags accessFlags) : m_buffer(buffer) { int width; @@ -65,7 +65,7 @@ GraphicsBufferView::GraphicsBufferView(GraphicsBuffer *buffer) return; } - void *data = buffer->map(); + void *data = buffer->map(accessFlags); if (data) { m_image = QImage(static_cast(data), width, height, stride, drmFormatToQImageFormat(format)); } diff --git a/src/core/graphicsbufferview.h b/src/core/graphicsbufferview.h index a12f1b24f6..49afa8a191 100644 --- a/src/core/graphicsbufferview.h +++ b/src/core/graphicsbufferview.h @@ -6,19 +6,17 @@ #pragma once -#include "kwin_export.h" +#include "core/graphicsbuffer.h" #include namespace KWin { -class GraphicsBuffer; - class KWIN_EXPORT GraphicsBufferView { public: - explicit GraphicsBufferView(GraphicsBuffer *buffer); + explicit GraphicsBufferView(GraphicsBuffer *buffer, GraphicsBuffer::MapFlags accessFlags = GraphicsBuffer::Read); ~GraphicsBufferView(); QImage *image(); diff --git a/src/wayland/shmclientbuffer.cpp b/src/wayland/shmclientbuffer.cpp index b64486ce19..291beb9a4e 100644 --- a/src/wayland/shmclientbuffer.cpp +++ b/src/wayland/shmclientbuffer.cpp @@ -233,7 +233,7 @@ static void sigbusHandler(int signum, siginfo_t *info, void *context) } } -void *ShmClientBuffer::map() +void *ShmClientBuffer::map(MapFlags flags) { if (!m_shmPool->sigbusImpossible) { // A SIGBUS signal may be emitted if the backing file is shrinked and we access now diff --git a/src/wayland/shmclientbuffer_p.h b/src/wayland/shmclientbuffer_p.h index da941398a3..e1e824e65a 100644 --- a/src/wayland/shmclientbuffer_p.h +++ b/src/wayland/shmclientbuffer_p.h @@ -57,7 +57,7 @@ public: ShmClientBuffer(ShmPool *pool, KWin::ShmAttributes attributes, wl_client *client, uint32_t id); ~ShmClientBuffer() override; - void *map() override; + void *map(MapFlags flags) override; void unmap() override; QSize size() const override;