File indexing completed on 2024-05-12 05:32:28

0001 /*
0002     SPDX-FileCopyrightText: 2023 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #pragma once
0008 
0009 #include "core/graphicsbuffer.h"
0010 #include "wayland/shmclientbuffer.h"
0011 #include "utils/filedescriptor.h"
0012 #include "utils/memorymap.h"
0013 
0014 #include "qwayland-server-wayland.h"
0015 
0016 namespace KWin
0017 {
0018 
0019 class ShmClientBufferIntegrationPrivate : public QtWaylandServer::wl_shm
0020 {
0021 public:
0022     ShmClientBufferIntegrationPrivate(Display *display, ShmClientBufferIntegration *q);
0023 
0024     ShmClientBufferIntegration *q;
0025 
0026 protected:
0027     void shm_bind_resource(Resource *resource) override;
0028     void shm_create_pool(Resource *resource, uint32_t id, int32_t fd, int32_t size) override;
0029 };
0030 
0031 class ShmPool : public QtWaylandServer::wl_shm_pool
0032 {
0033 public:
0034     ShmPool(ShmClientBufferIntegration *integration, wl_client *client, int id, uint32_t version, FileDescriptor &&fd, MemoryMap &&mapping);
0035 
0036     void ref();
0037     void unref();
0038 
0039     ShmClientBufferIntegration *integration;
0040     MemoryMap mapping;
0041     FileDescriptor fd;
0042     int refCount = 1;
0043     bool sigbusImpossible = false;
0044 
0045 protected:
0046     void shm_pool_destroy_resource(Resource *resource) override;
0047     void shm_pool_create_buffer(Resource *resource, uint32_t id, int32_t offset, int32_t width, int32_t height, int32_t stride, uint32_t format) override;
0048     void shm_pool_destroy(Resource *resource) override;
0049     void shm_pool_resize(Resource *resource, int32_t size) override;
0050 };
0051 
0052 class KWIN_EXPORT ShmClientBuffer : public GraphicsBuffer
0053 {
0054     Q_OBJECT
0055 
0056 public:
0057     ShmClientBuffer(ShmPool *pool, ShmAttributes attributes, wl_client *client, uint32_t id);
0058     ~ShmClientBuffer() override;
0059 
0060     Map map(MapFlags flags) override;
0061     void unmap() override;
0062 
0063     QSize size() const override;
0064     bool hasAlphaChannel() const override;
0065     const ShmAttributes *shmAttributes() const override;
0066 
0067     static ShmClientBuffer *get(wl_resource *resource);
0068 
0069 private:
0070     static void buffer_destroy_resource(wl_resource *resource);
0071     static void buffer_destroy(wl_client *client, wl_resource *resource);
0072     static const struct wl_buffer_interface implementation;
0073 
0074     wl_resource *m_resource = nullptr;
0075     ShmPool *m_shmPool;
0076     ShmAttributes m_shmAttributes;
0077 };
0078 
0079 } // namespace KWin