offscreen rendering
This commit is contained in:
		
							parent
							
								
									be9f2f9903
								
							
						
					
					
						commit
						9b72acd912
					
				| @ -12,6 +12,7 @@ executable('somebar', | |||||||
|   'src/main.cpp', |   'src/main.cpp', | ||||||
|   'src/shm_buffer.cpp', |   'src/shm_buffer.cpp', | ||||||
|   'src/bar.cpp', |   'src/bar.cpp', | ||||||
|  |   'src/bar_widget.cpp', | ||||||
|   wayland_sources, |   wayland_sources, | ||||||
|   #moc, |   #moc, | ||||||
|   dependencies: [qt5_dep, wayland_dep]) |   dependencies: [qt5_dep, wayland_dep]) | ||||||
|  | |||||||
							
								
								
									
										23
									
								
								src/bar.cpp
									
									
									
									
									
								
							
							
						
						
									
										23
									
								
								src/bar.cpp
									
									
									
									
									
								
							| @ -1,8 +1,7 @@ | |||||||
| // somebar - dwl bar
 | // somebar - dwl bar
 | ||||||
| // See LICENSE file for copyright and license details.
 | // See LICENSE file for copyright and license details.
 | ||||||
| 
 | 
 | ||||||
| #include <cstdio> | #include <QImage> | ||||||
| #include <cstring> |  | ||||||
| #include "bar.hpp" | #include "bar.hpp" | ||||||
| #include "config.hpp" | #include "config.hpp" | ||||||
| 
 | 
 | ||||||
| @ -34,11 +33,25 @@ Bar::~Bar() | |||||||
| void Bar::layerSurfaceConfigure(uint32_t serial, uint32_t width, uint32_t height) | void Bar::layerSurfaceConfigure(uint32_t serial, uint32_t width, uint32_t height) | ||||||
| { | { | ||||||
|     zwlr_layer_surface_v1_ack_configure(_layerSurface, serial); |     zwlr_layer_surface_v1_ack_configure(_layerSurface, serial); | ||||||
|     printf("configure: %d x %d\n", width, height); |  | ||||||
| 
 |  | ||||||
|     _bufs.emplace(width, height, WL_SHM_FORMAT_XRGB8888); |     _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_attach(_surface, _bufs->buffer(), 0, 0); | ||||||
|     wl_surface_commit(_surface); |     wl_surface_commit(_surface); | ||||||
|  |     _bufs->flip(); | ||||||
|     waylandFlush(); |     waylandFlush(); | ||||||
| } | } | ||||||
|  | |||||||
| @ -7,6 +7,7 @@ | |||||||
| #include "wlr-layer-shell-unstable-v1-client-protocol.h" | #include "wlr-layer-shell-unstable-v1-client-protocol.h" | ||||||
| #include "common.hpp" | #include "common.hpp" | ||||||
| #include "shm_buffer.hpp" | #include "shm_buffer.hpp" | ||||||
|  | #include "bar_widget.hpp" | ||||||
| 
 | 
 | ||||||
| class Bar { | class Bar { | ||||||
|     static const zwlr_layer_surface_v1_listener _layerSurfaceListener; |     static const zwlr_layer_surface_v1_listener _layerSurfaceListener; | ||||||
| @ -14,8 +15,10 @@ class Bar { | |||||||
|     wl_surface *_surface {nullptr}; |     wl_surface *_surface {nullptr}; | ||||||
|     zwlr_layer_surface_v1 *_layerSurface {nullptr}; |     zwlr_layer_surface_v1 *_layerSurface {nullptr}; | ||||||
|     std::optional<ShmBuffer> _bufs; |     std::optional<ShmBuffer> _bufs; | ||||||
|  |     BarWidget _widget; | ||||||
| 
 | 
 | ||||||
|     void layerSurfaceConfigure(uint32_t serial, uint32_t width, uint32_t height); |     void layerSurfaceConfigure(uint32_t serial, uint32_t width, uint32_t height); | ||||||
|  |     void render(); | ||||||
| public: | public: | ||||||
|     explicit Bar(const wl_output *output); |     explicit Bar(const wl_output *output); | ||||||
|     ~Bar(); |     ~Bar(); | ||||||
|  | |||||||
							
								
								
									
										18
									
								
								src/bar_widget.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								src/bar_widget.cpp
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										17
									
								
								src/bar_widget.hpp
									
									
									
									
									
										Normal 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(); | ||||||
|  | }; | ||||||
| @ -5,7 +5,7 @@ | |||||||
| #include <sys/mman.h> | #include <sys/mman.h> | ||||||
| #include <sys/types.h> | #include <sys/types.h> | ||||||
| #include <unistd.h> | #include <unistd.h> | ||||||
| #include <QCoreApplication> | #include <QApplication> | ||||||
| #include <QSocketNotifier> | #include <QSocketNotifier> | ||||||
| #include <wayland-client.h> | #include <wayland-client.h> | ||||||
| #include "qnamespace.h" | #include "qnamespace.h" | ||||||
| @ -69,7 +69,7 @@ static const struct wl_registry_listener registry_listener = { registryHandleGlo | |||||||
| 
 | 
 | ||||||
| int main(int argc, char **argv) | int main(int argc, char **argv) | ||||||
| { | { | ||||||
|     QCoreApplication app(argc, argv); |     QApplication app(argc, argv); | ||||||
|     QCoreApplication::setOrganizationName("tape software"); |     QCoreApplication::setOrganizationName("tape software"); | ||||||
|     QCoreApplication::setOrganizationDomain("tapesoftware.net"); |     QCoreApplication::setOrganizationDomain("tapesoftware.net"); | ||||||
|     QCoreApplication::setApplicationName("somebar"); |     QCoreApplication::setApplicationName("somebar"); | ||||||
|  | |||||||
| @ -18,7 +18,7 @@ ShmBuffer::ShmBuffer(int w, int h, wl_shm_format format) | |||||||
|     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<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); |     close(fd); | ||||||
|     for (auto i=0; i<n; i++) { |     for (auto i=0; i<n; i++) { | ||||||
|         auto offset = oneSize*i; |         auto offset = oneSize*i; | ||||||
| @ -37,6 +37,6 @@ ShmBuffer::~ShmBuffer() | |||||||
|     waylandFlush(); |     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; } | wl_buffer* ShmBuffer::buffer() const { return _buffers[_current].buffer; } | ||||||
| void ShmBuffer::flip() { _current = 1-_current; } | void ShmBuffer::flip() { _current = 1-_current; } | ||||||
|  | |||||||
| @ -9,7 +9,7 @@ | |||||||
| // format is must be 32-bit
 | // format is must be 32-bit
 | ||||||
| class ShmBuffer { | class ShmBuffer { | ||||||
|     struct Buf { |     struct Buf { | ||||||
|         char *data {nullptr}; |         uint8_t *data {nullptr}; | ||||||
|         wl_buffer *buffer {nullptr}; |         wl_buffer *buffer {nullptr}; | ||||||
|     }; |     }; | ||||||
|     std::array<Buf, 2> _buffers; |     std::array<Buf, 2> _buffers; | ||||||
| @ -18,7 +18,7 @@ class ShmBuffer { | |||||||
| 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); | ||||||
|     char* data() const; |     uint8_t* data() const; | ||||||
|     wl_buffer* buffer() const; |     wl_buffer* buffer() const; | ||||||
|     void flip(); |     void flip(); | ||||||
|     ~ShmBuffer(); |     ~ShmBuffer(); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user