From 9b72acd912186471bdac5358ddb04b9e6e057a9b Mon Sep 17 00:00:00 2001 From: Raphael Robatsch Date: Wed, 20 Oct 2021 21:09:19 +0200 Subject: [PATCH] offscreen rendering --- meson.build | 1 + src/bar.cpp | 23 ++++++++++++++++++----- src/bar.hpp | 3 +++ src/bar_widget.cpp | 18 ++++++++++++++++++ src/bar_widget.hpp | 17 +++++++++++++++++ src/main.cpp | 4 ++-- src/shm_buffer.cpp | 4 ++-- src/shm_buffer.hpp | 4 ++-- 8 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 src/bar_widget.cpp create mode 100644 src/bar_widget.hpp diff --git a/meson.build b/meson.build index 5a7892d..4483abc 100644 --- a/meson.build +++ b/meson.build @@ -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]) diff --git a/src/bar.cpp b/src/bar.cpp index 3b86d7a..05751b9 100644 --- a/src/bar.cpp +++ b/src/bar.cpp @@ -1,8 +1,7 @@ // somebar - dwl bar // See LICENSE file for copyright and license details. -#include -#include +#include #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(); } diff --git a/src/bar.hpp b/src/bar.hpp index 86ea7b9..e79f975 100644 --- a/src/bar.hpp +++ b/src/bar.hpp @@ -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 _bufs; + BarWidget _widget; void layerSurfaceConfigure(uint32_t serial, uint32_t width, uint32_t height); + void render(); public: explicit Bar(const wl_output *output); ~Bar(); diff --git a/src/bar_widget.cpp b/src/bar_widget.cpp new file mode 100644 index 0000000..42e6597 --- /dev/null +++ b/src/bar_widget.cpp @@ -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; } diff --git a/src/bar_widget.hpp b/src/bar_widget.hpp new file mode 100644 index 0000000..e013714 --- /dev/null +++ b/src/bar_widget.hpp @@ -0,0 +1,17 @@ +// somebar - dwl bar +// See LICENSE file for copyright and license details. + +#pragma once +#include +#include +#include + +class BarWidget { + QWidget _root; + QBoxLayout _box; + QLabel _label; +public: + explicit BarWidget(); + ~BarWidget(); + QWidget* root(); +}; diff --git a/src/main.cpp b/src/main.cpp index 46b832c..0e3bafd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,7 +5,7 @@ #include #include #include -#include +#include #include #include #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"); diff --git a/src/shm_buffer.cpp b/src/shm_buffer.cpp index d81a96b..5d5a916 100644 --- a/src/shm_buffer.cpp +++ b/src/shm_buffer.cpp @@ -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(mmap(nullptr, _totalSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)); + auto ptr = reinterpret_cast(mmap(nullptr, _totalSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)); close(fd); for (auto i=0; i _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();