File indexing completed on 2024-05-19 16:35:21

0001 /*
0002     SPDX-FileCopyrightText: 2018 Fredrik Höglund <fredrik@kde.org>
0003     SPDX-FileCopyrightText: 2019 Roman Gilg <subdiff@gmail.com>
0004     SPDX-FileCopyrightText: 2021 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
0005     SPDX-FileCopyrightText: 2021 Xaver Hugl <xaver.hugl@gmail.com>
0006 
0007     Based on the libweston implementation,
0008     SPDX-FileCopyrightText: 2014, 2015 Collabora, Ltd.
0009 
0010     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0011 */
0012 #pragma once
0013 #include "clientbuffer_p.h"
0014 #include "display.h"
0015 #include "display_p.h"
0016 #include "linuxdmabufv1clientbuffer.h"
0017 #include "utils/ramfile.h"
0018 
0019 #include "qwayland-server-linux-dmabuf-unstable-v1.h"
0020 #include "qwayland-server-wayland.h"
0021 
0022 #include <QDebug>
0023 #include <QVector>
0024 
0025 #include <drm_fourcc.h>
0026 
0027 namespace KWaylandServer
0028 {
0029 
0030 class LinuxDmaBufV1FormatTable;
0031 
0032 class LinuxDmaBufV1ClientBufferIntegrationPrivate : public QtWaylandServer::zwp_linux_dmabuf_v1
0033 {
0034 public:
0035     LinuxDmaBufV1ClientBufferIntegrationPrivate(LinuxDmaBufV1ClientBufferIntegration *q, Display *display);
0036 
0037     LinuxDmaBufV1ClientBufferIntegration *q;
0038     LinuxDmaBufV1ClientBufferIntegration::RendererInterface *rendererInterface = nullptr;
0039     std::unique_ptr<LinuxDmaBufV1Feedback> defaultFeedback;
0040     std::unique_ptr<LinuxDmaBufV1FormatTable> table;
0041     dev_t mainDevice;
0042     QHash<uint32_t, QVector<uint64_t>> supportedModifiers;
0043 
0044 protected:
0045     void zwp_linux_dmabuf_v1_bind_resource(Resource *resource) override;
0046     void zwp_linux_dmabuf_v1_destroy(Resource *resource) override;
0047     void zwp_linux_dmabuf_v1_create_params(Resource *resource, uint32_t params_id) override;
0048     void zwp_linux_dmabuf_v1_get_default_feedback(Resource *resource, uint32_t id) override;
0049     void zwp_linux_dmabuf_v1_get_surface_feedback(Resource *resource, uint32_t id, wl_resource *surface) override;
0050 };
0051 
0052 class LinuxDmaBufV1ClientBufferPrivate : public ClientBufferPrivate, public QtWaylandServer::wl_buffer
0053 {
0054 public:
0055     KWin::DmaBufAttributes attrs;
0056     quint32 flags;
0057     bool hasAlphaChannel = false;
0058 
0059 protected:
0060     void buffer_destroy(Resource *resource) override;
0061 };
0062 
0063 class LinuxDmaBufParamsV1 : public QtWaylandServer::zwp_linux_buffer_params_v1
0064 {
0065 public:
0066     LinuxDmaBufParamsV1(LinuxDmaBufV1ClientBufferIntegration *integration, ::wl_resource *resource);
0067 
0068 protected:
0069     void zwp_linux_buffer_params_v1_destroy_resource(Resource *resource) override;
0070     void zwp_linux_buffer_params_v1_destroy(Resource *resource) override;
0071     void zwp_linux_buffer_params_v1_add(Resource *resource,
0072                                         int32_t fd,
0073                                         uint32_t plane_idx,
0074                                         uint32_t offset,
0075                                         uint32_t stride,
0076                                         uint32_t modifier_hi,
0077                                         uint32_t modifier_lo) override;
0078     void zwp_linux_buffer_params_v1_create(Resource *resource, int32_t width, int32_t height, uint32_t format, uint32_t flags) override;
0079     void
0080     zwp_linux_buffer_params_v1_create_immed(Resource *resource, uint32_t buffer_id, int32_t width, int32_t height, uint32_t format, uint32_t flags) override;
0081 
0082 private:
0083     bool test(Resource *resource, uint32_t width, uint32_t height);
0084 
0085     LinuxDmaBufV1ClientBufferIntegration *m_integration;
0086     KWin::DmaBufAttributes m_attrs;
0087     bool m_isUsed = false;
0088 };
0089 
0090 class LinuxDmaBufV1FormatTable
0091 {
0092 public:
0093     LinuxDmaBufV1FormatTable(const QHash<uint32_t, QVector<uint64_t>> &supportedModifiers);
0094 
0095     KWin::RamFile file;
0096     QMap<std::pair<uint32_t, uint64_t>, uint16_t> indices;
0097 };
0098 
0099 class LinuxDmaBufV1FeedbackPrivate : public QtWaylandServer::zwp_linux_dmabuf_feedback_v1
0100 {
0101 public:
0102     LinuxDmaBufV1FeedbackPrivate(LinuxDmaBufV1ClientBufferIntegrationPrivate *bufferintegration);
0103 
0104     static LinuxDmaBufV1FeedbackPrivate *get(LinuxDmaBufV1Feedback *q);
0105     void send(Resource *resource);
0106 
0107     QVector<LinuxDmaBufV1Feedback::Tranche> m_tranches;
0108     LinuxDmaBufV1ClientBufferIntegrationPrivate *m_bufferintegration;
0109 
0110 protected:
0111     void zwp_linux_dmabuf_feedback_v1_bind_resource(Resource *resource) override;
0112     void zwp_linux_dmabuf_feedback_v1_destroy(Resource *resource) override;
0113 };
0114 }