diff --git a/src/core/shmgraphicsbufferallocator.cpp b/src/core/shmgraphicsbufferallocator.cpp index fcc0bbfffc..12fb0bc8e1 100644 --- a/src/core/shmgraphicsbufferallocator.cpp +++ b/src/core/shmgraphicsbufferallocator.cpp @@ -21,6 +21,33 @@ ShmGraphicsBuffer::ShmGraphicsBuffer(ShmAttributes &&attributes) { } +void *ShmGraphicsBuffer::map(MapFlags flags) +{ + if (m_memoryMap.isValid()) { + return m_memoryMap.data(); + } + + int prot = 0; + if (flags & MapFlag::Read) { + prot |= PROT_READ; + } + if (flags & MapFlag::Write) { + prot |= PROT_WRITE; + } + + m_memoryMap = MemoryMap(m_attributes.stride * m_attributes.size.height(), prot, MAP_SHARED, m_attributes.fd.get(), m_attributes.offset); + if (m_memoryMap.isValid()) { + return m_memoryMap.data(); + } + + return nullptr; +} + +void ShmGraphicsBuffer::unmap() +{ + m_memoryMap = MemoryMap{}; +} + QSize ShmGraphicsBuffer::size() const { return m_attributes.size; diff --git a/src/core/shmgraphicsbufferallocator.h b/src/core/shmgraphicsbufferallocator.h index 41119abb22..65cbac89ce 100644 --- a/src/core/shmgraphicsbufferallocator.h +++ b/src/core/shmgraphicsbufferallocator.h @@ -8,7 +8,7 @@ #include "core/graphicsbuffer.h" #include "core/graphicsbufferallocator.h" -#include "utils/filedescriptor.h" +#include "utils/memorymap.h" namespace KWin { @@ -20,12 +20,16 @@ class KWIN_EXPORT ShmGraphicsBuffer : public GraphicsBuffer public: explicit ShmGraphicsBuffer(ShmAttributes &&attributes); + void *map(MapFlags flags) override; + void unmap() override; + QSize size() const override; bool hasAlphaChannel() const override; const ShmAttributes *shmAttributes() const override; private: ShmAttributes m_attributes; + MemoryMap m_memoryMap; bool m_hasAlphaChannel; };