refactoring protocol build and shm_buffer

This commit is contained in:
Raphael Robatsch 2021-10-27 17:24:47 +02:00
parent d5c4349a3f
commit dfae73b1c5
3 changed files with 38 additions and 34 deletions

View File

@ -11,15 +11,12 @@ wayland_scanner_client = generator(
output: '@BASENAME@-client-protocol.h', output: '@BASENAME@-client-protocol.h',
arguments: ['client-header', '@INPUT@', '@OUTPUT@']) 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_sources = [
wayland_scanner_code.process( wayland_scanner_code.process(wayland_xmls),
wl_protocol_dir + '/stable/xdg-shell/xdg-shell.xml', wayland_scanner_client.process(wayland_xmls),
'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'
),
]

View File

@ -13,12 +13,13 @@ ShmBuffer::ShmBuffer(int w, int h, wl_shm_format format)
, height(h) , height(h)
, stride(w*4) , stride(w*4)
{ {
auto oneSize = stride*h; auto oneSize = stride*size_t(h);
_totalSize = oneSize * n; auto totalSize = oneSize * n;
auto fd = memfd_create("wl_shm", MFD_CLOEXEC); auto fd = memfd_create("wl_shm", MFD_CLOEXEC);
ftruncate(fd, _totalSize); ftruncate(fd, totalSize);
auto pool = wl_shm_create_pool(shm, fd, _totalSize); auto pool = wl_shm_create_pool(shm, fd, totalSize);
auto ptr = reinterpret_cast<uint8_t*>(mmap(nullptr, _totalSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)); auto ptr = reinterpret_cast<uint8_t*>(mmap(nullptr, totalSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
_mapping = MemoryMapping {ptr, totalSize};
close(fd); close(fd);
for (auto i=0; i<n; i++) { for (auto i=0; i<n; i++) {
auto offset = oneSize*i; auto offset = oneSize*i;
@ -30,13 +31,6 @@ ShmBuffer::ShmBuffer(int w, int h, wl_shm_format format)
wl_shm_pool_destroy(pool); wl_shm_pool_destroy(pool);
} }
ShmBuffer::~ShmBuffer() uint8_t* ShmBuffer::data() { return _buffers[_current].data; }
{ wl_buffer* ShmBuffer::buffer() { return _buffers[_current].buffer.get(); }
if (_buffers[0].data) {
munmap(_buffers[0].data, _totalSize);
}
}
uint8_t* ShmBuffer::data() const { return _buffers[_current].data; }
wl_buffer* ShmBuffer::buffer() const { return _buffers[_current].buffer.get(); }
void ShmBuffer::flip() { _current = 1-_current; } void ShmBuffer::flip() { _current = 1-_current; }

View File

@ -3,9 +3,28 @@
#pragma once #pragma once
#include <array> #include <array>
#include <sys/mman.h>
#include <wayland-client.h> #include <wayland-client.h>
#include "common.hpp" #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 // double buffered shm
// format is must be 32-bit // format is must be 32-bit
class ShmBuffer { class ShmBuffer {
@ -15,18 +34,12 @@ class ShmBuffer {
}; };
std::array<Buf, 2> _buffers; std::array<Buf, 2> _buffers;
int _current {0}; int _current {0};
size_t _totalSize {0}; MemoryMapping _mapping;
public: public:
int width, height, stride; int width, height, stride;
explicit ShmBuffer(int width, int height, wl_shm_format format); explicit ShmBuffer(int width, int height, wl_shm_format format);
ShmBuffer(const ShmBuffer&) = delete; uint8_t* data();
ShmBuffer(ShmBuffer&&) = default; wl_buffer* buffer();
ShmBuffer& operator=(const ShmBuffer&) = delete;
ShmBuffer& operator=(ShmBuffer&&) = default;
~ShmBuffer();
uint8_t* data() const;
wl_buffer* buffer() const;
void flip(); void flip();
}; };