From dfae73b1c5a1d5b4e835c3a33fa45ce6c67c1db1 Mon Sep 17 00:00:00 2001 From: Raphael Robatsch Date: Wed, 27 Oct 2021 17:24:47 +0200 Subject: [PATCH] refactoring protocol build and shm_buffer --- protocols/meson.build | 19 ++++++++----------- src/shm_buffer.cpp | 22 ++++++++-------------- src/shm_buffer.hpp | 31 ++++++++++++++++++++++--------- 3 files changed, 38 insertions(+), 34 deletions(-) diff --git a/protocols/meson.build b/protocols/meson.build index ab0cfa2..cefdf64 100644 --- a/protocols/meson.build +++ b/protocols/meson.build @@ -11,15 +11,12 @@ wayland_scanner_client = generator( output: '@BASENAME@-client-protocol.h', arguments: ['client-header', '@INPUT@', '@OUTPUT@']) +wayland_xmls = [ + wl_protocol_dir + '/stable/xdg-shell/xdg-shell.xml', + 'wlr-layer-shell-unstable-v1.xml', + 'net-tapesoftware-dwl-wm-unstable-v1.xml', +] wayland_sources = [ - wayland_scanner_code.process( - wl_protocol_dir + '/stable/xdg-shell/xdg-shell.xml', - 'wlr-layer-shell-unstable-v1.xml', - 'net-tapesoftware-dwl-wm-unstable-v1.xml' - ), - wayland_scanner_client.process( - wl_protocol_dir + '/stable/xdg-shell/xdg-shell.xml', - 'wlr-layer-shell-unstable-v1.xml', - 'net-tapesoftware-dwl-wm-unstable-v1.xml' - ), - ] + wayland_scanner_code.process(wayland_xmls), + wayland_scanner_client.process(wayland_xmls), +] diff --git a/src/shm_buffer.cpp b/src/shm_buffer.cpp index fc16265..1dfb167 100644 --- a/src/shm_buffer.cpp +++ b/src/shm_buffer.cpp @@ -13,12 +13,13 @@ ShmBuffer::ShmBuffer(int w, int h, wl_shm_format format) , height(h) , stride(w*4) { - auto oneSize = stride*h; - _totalSize = oneSize * n; + auto oneSize = stride*size_t(h); + auto totalSize = oneSize * n; auto fd = memfd_create("wl_shm", MFD_CLOEXEC); - ftruncate(fd, _totalSize); - auto pool = wl_shm_create_pool(shm, fd, _totalSize); - auto ptr = reinterpret_cast(mmap(nullptr, _totalSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)); + ftruncate(fd, totalSize); + auto pool = wl_shm_create_pool(shm, fd, totalSize); + auto ptr = reinterpret_cast(mmap(nullptr, totalSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)); + _mapping = MemoryMapping {ptr, totalSize}; close(fd); for (auto i=0; i +#include #include #include "common.hpp" +class MemoryMapping { + void* _ptr {nullptr}; + size_t _size {0}; +public: + MemoryMapping() { } + explicit MemoryMapping(void *ptr, size_t size) : _ptr(ptr), _size(size) { } + MemoryMapping(const MemoryMapping&) = delete; + MemoryMapping(MemoryMapping &&other) { swap(other); } + MemoryMapping& operator=(const MemoryMapping &other) = delete; + MemoryMapping& operator=(MemoryMapping &&other) { swap(other); return *this; } + ~MemoryMapping() { if (_ptr) munmap(_ptr, _size); } + void swap(MemoryMapping &other) { + using std::swap; + swap(_ptr, other._ptr); + swap(_size, other._size); + } +}; + // double buffered shm // format is must be 32-bit class ShmBuffer { @@ -15,18 +34,12 @@ class ShmBuffer { }; std::array _buffers; int _current {0}; - size_t _totalSize {0}; + MemoryMapping _mapping; public: int width, height, stride; explicit ShmBuffer(int width, int height, wl_shm_format format); - ShmBuffer(const ShmBuffer&) = delete; - ShmBuffer(ShmBuffer&&) = default; - ShmBuffer& operator=(const ShmBuffer&) = delete; - ShmBuffer& operator=(ShmBuffer&&) = default; - ~ShmBuffer(); - - uint8_t* data() const; - wl_buffer* buffer() const; + uint8_t* data(); + wl_buffer* buffer(); void flip(); };