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',
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),
]

View file

@ -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<uint8_t*>(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<uint8_t*>(mmap(nullptr, totalSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
_mapping = MemoryMapping {ptr, totalSize};
close(fd);
for (auto i=0; i<n; i++) {
auto offset = oneSize*i;
@ -30,13 +31,6 @@ ShmBuffer::ShmBuffer(int w, int h, wl_shm_format format)
wl_shm_pool_destroy(pool);
}
ShmBuffer::~ShmBuffer()
{
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(); }
uint8_t* ShmBuffer::data() { return _buffers[_current].data; }
wl_buffer* ShmBuffer::buffer() { return _buffers[_current].buffer.get(); }
void ShmBuffer::flip() { _current = 1-_current; }

View file

@ -3,9 +3,28 @@
#pragma once
#include <array>
#include <sys/mman.h>
#include <wayland-client.h>
#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<Buf, 2> _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();
};