File indexing completed on 2025-04-20 10:57:35

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2016 Roman Gilg <subdiff@gmail.com>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 #pragma once
0010 
0011 #include <QByteArray>
0012 #include <QMap>
0013 #include <QVector>
0014 
0015 #include <vector>
0016 
0017 // drm
0018 #include <xf86drmMode.h>
0019 
0020 #include "drm_pointer.h"
0021 #include "drm_property.h"
0022 
0023 namespace KWin
0024 {
0025 
0026 class DrmBackend;
0027 class DrmGpu;
0028 class DrmOutput;
0029 
0030 class DrmObject
0031 {
0032 public:
0033     virtual ~DrmObject() = default;
0034     DrmObject(const DrmObject &) = delete;
0035 
0036     /**
0037      * Must be called to query necessary data directly after creation.
0038      * @return true when initializing was successful
0039      */
0040     virtual bool init() = 0;
0041 
0042     /**
0043      * Set the properties in such a way that this resource won't be used anymore
0044      */
0045     virtual void disable() = 0;
0046 
0047     uint32_t id() const;
0048     DrmGpu *gpu() const;
0049     uint32_t type() const;
0050     QString typeName() const;
0051 
0052     void commit();
0053     void commitPending();
0054     void rollbackPending();
0055     bool atomicPopulate(drmModeAtomicReq *req) const;
0056     bool needsCommit() const;
0057     virtual bool updateProperties();
0058 
0059     template<typename T>
0060     bool setPending(T prop, uint64_t new_value)
0061     {
0062         if (auto &property = m_props.at(static_cast<uint32_t>(prop))) {
0063             property->setPending(new_value);
0064             return true;
0065         }
0066         return false;
0067     }
0068 
0069     template<typename T>
0070     DrmProperty *getProp(T propIndex) const
0071     {
0072         return m_props[static_cast<uint32_t>(propIndex)].get();
0073     }
0074 
0075     enum class PrintMode {
0076         OnlyChanged,
0077         All
0078     };
0079     void printProps(PrintMode mode);
0080 
0081 protected:
0082     enum class Requirement {
0083         Required,
0084         RequiredForLegacy,
0085         Optional,
0086     };
0087     struct PropertyDefinition
0088     {
0089         PropertyDefinition(const QByteArray &name, Requirement requirement, const QVector<QByteArray> &&enumNames = {})
0090             : name(name)
0091             , requirement(requirement)
0092             , enumNames(enumNames)
0093         {
0094         }
0095         QByteArray name;
0096         Requirement requirement;
0097         QVector<QByteArray> enumNames;
0098     };
0099 
0100     DrmObject(DrmGpu *gpu, uint32_t objectId, const QVector<PropertyDefinition> &&vector, uint32_t objectType);
0101 
0102     bool initProps();
0103 
0104     std::vector<std::unique_ptr<DrmProperty>> m_props;
0105 
0106 private:
0107     DrmGpu *m_gpu;
0108     const uint32_t m_id;
0109     const uint32_t m_objectType;
0110     const QVector<PropertyDefinition> m_propertyDefinitions;
0111 };
0112 
0113 }
0114 
0115 QDebug operator<<(QDebug stream, const KWin::DrmObject *);