offscreen rendering

This commit is contained in:
Raphael Robatsch 2021-10-20 21:09:19 +02:00
parent be9f2f9903
commit 9b72acd912
8 changed files with 63 additions and 11 deletions

View File

@ -12,6 +12,7 @@ executable('somebar',
'src/main.cpp',
'src/shm_buffer.cpp',
'src/bar.cpp',
'src/bar_widget.cpp',
wayland_sources,
#moc,
dependencies: [qt5_dep, wayland_dep])

View File

@ -1,8 +1,7 @@
// somebar - dwl bar
// See LICENSE file for copyright and license details.
#include <cstdio>
#include <cstring>
#include <QImage>
#include "bar.hpp"
#include "config.hpp"
@ -34,11 +33,25 @@ Bar::~Bar()
void Bar::layerSurfaceConfigure(uint32_t serial, uint32_t width, uint32_t height)
{
zwlr_layer_surface_v1_ack_configure(_layerSurface, serial);
printf("configure: %d x %d\n", width, height);
_bufs.emplace(width, height, WL_SHM_FORMAT_XRGB8888);
memset(_bufs->data(), 0xff, _bufs->stride*_bufs->height);
auto root = _widget.root();
root->setFixedSize(width, height);
render();
}
void Bar::render()
{
auto img = QImage {
_bufs->data(),
_bufs->width,
_bufs->height,
_bufs->stride,
QImage::Format_RGBX8888
};
auto root = _widget.root();
root->render(&img);
wl_surface_attach(_surface, _bufs->buffer(), 0, 0);
wl_surface_commit(_surface);
_bufs->flip();
waylandFlush();
}

View File

@ -7,6 +7,7 @@
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
#include "common.hpp"
#include "shm_buffer.hpp"
#include "bar_widget.hpp"
class Bar {
static const zwlr_layer_surface_v1_listener _layerSurfaceListener;
@ -14,8 +15,10 @@ class Bar {
wl_surface *_surface {nullptr};
zwlr_layer_surface_v1 *_layerSurface {nullptr};
std::optional<ShmBuffer> _bufs;
BarWidget _widget;
void layerSurfaceConfigure(uint32_t serial, uint32_t width, uint32_t height);
void render();
public:
explicit Bar(const wl_output *output);
~Bar();

18
src/bar_widget.cpp Normal file
View File

@ -0,0 +1,18 @@
// somebar - dwl bar
// See LICENSE file for copyright and license details.
#include "bar_widget.hpp"
BarWidget::BarWidget()
: _box {QBoxLayout::LeftToRight, &_root}
, _label {&_root}
{
_root.setAttribute(Qt::WA_DontShowOnScreen);
_label.setText("somebar");
}
BarWidget::~BarWidget()
{
}
QWidget* BarWidget::root() { return &_root; }

17
src/bar_widget.hpp Normal file
View File

@ -0,0 +1,17 @@
// somebar - dwl bar
// See LICENSE file for copyright and license details.
#pragma once
#include <QBoxLayout>
#include <QLabel>
#include <QWidget>
class BarWidget {
QWidget _root;
QBoxLayout _box;
QLabel _label;
public:
explicit BarWidget();
~BarWidget();
QWidget* root();
};

View File

@ -5,7 +5,7 @@
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#include <QCoreApplication>
#include <QApplication>
#include <QSocketNotifier>
#include <wayland-client.h>
#include "qnamespace.h"
@ -69,7 +69,7 @@ static const struct wl_registry_listener registry_listener = { registryHandleGlo
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
QApplication app(argc, argv);
QCoreApplication::setOrganizationName("tape software");
QCoreApplication::setOrganizationDomain("tapesoftware.net");
QCoreApplication::setApplicationName("somebar");

View File

@ -18,7 +18,7 @@ ShmBuffer::ShmBuffer(int w, int h, wl_shm_format format)
auto fd = memfd_create("wl_shm", MFD_CLOEXEC);
ftruncate(fd, _totalSize);
auto pool = wl_shm_create_pool(shm, fd, _totalSize);
auto ptr = reinterpret_cast<char*>(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));
close(fd);
for (auto i=0; i<n; i++) {
auto offset = oneSize*i;
@ -37,6 +37,6 @@ ShmBuffer::~ShmBuffer()
waylandFlush();
}
char* ShmBuffer::data() const { return _buffers[_current].data; }
uint8_t* ShmBuffer::data() const { return _buffers[_current].data; }
wl_buffer* ShmBuffer::buffer() const { return _buffers[_current].buffer; }
void ShmBuffer::flip() { _current = 1-_current; }

View File

@ -9,7 +9,7 @@
// format is must be 32-bit
class ShmBuffer {
struct Buf {
char *data {nullptr};
uint8_t *data {nullptr};
wl_buffer *buffer {nullptr};
};
std::array<Buf, 2> _buffers;
@ -18,7 +18,7 @@ class ShmBuffer {
public:
int width, height, stride;
explicit ShmBuffer(int width, int height, wl_shm_format format);
char* data() const;
uint8_t* data() const;
wl_buffer* buffer() const;
void flip();
~ShmBuffer();