drm_object.h 4.18 KB
Newer Older
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
1
2
3
/*
    KWin - the KDE window manager
    This file is part of the KDE project.
4

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
5
    SPDX-FileCopyrightText: 2016 Roman Gilg <subdiff@gmail.com>
6

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
7
8
    SPDX-License-Identifier: GPL-2.0-or-later
*/
9
#pragma once
10
11
12
13
14
15
16

#include <QVector>
#include <QByteArray>

// drm
#include <xf86drmMode.h>

Xaver Hugl's avatar
Xaver Hugl committed
17
#include "drm_pointer.h"
18
19
20
21

namespace KWin
{

22
class DrmBackend;
23
class DrmGpu;
24
25
26
27
28
class DrmOutput;

class DrmObject
{
public:
29
30
31
32
33
    /**
     * Create DRM object representation.
     * @param object_id provided by the kernel
     * @param fd of the DRM device
     */
34
    DrmObject(DrmGpu *gpu, uint32_t objectId);
35
    virtual ~DrmObject();
36

37
38
39
40
    /**
     * Must be called to query necessary data directly after creation.
     * @return true when initializing was successful
     */
41
    virtual bool init() = 0;
42
43
44
45
46

    uint32_t id() const {
        return m_id;
    }

47
48
    DrmGpu *gpu() const {
        return m_gpu;
49
50
    }

51
52
53
54
55
    /**
     * Populate an atomic request with data of this object.
     * @param req the atomic request
     * @return true when the request was successfully populated
     */
Xaver Hugl's avatar
Xaver Hugl committed
56
    bool atomicPopulate(drmModeAtomicReq *req) const;
57

Xaver Hugl's avatar
Xaver Hugl committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    template <typename T>
    void setValue(T prop, uint64_t new_value)
    {
        if (auto &property = m_props.at(static_cast<uint32_t>(prop))) {
            property->setValue(new_value);
        }
    }

    template <typename T>
    bool propHasEnum(T prop, uint64_t value) const
    {
        const auto &property = m_props.at(static_cast<uint32_t>(prop));
        return property ? property->hasEnum(value) : false;
    }
72
73

protected:
Xaver Hugl's avatar
Xaver Hugl committed
74
75
76
77
78
79
80
81
82
    struct PropertyDefinition
    {
        PropertyDefinition(const QByteArray &name, const QVector<QByteArray> &&enumNames = {})
            : name(name), enumNames(enumNames)
        {
        }
        QByteArray name;
        QVector<QByteArray> enumNames;
    };
83
84
85
86
87
88
    /**
     * Initialize properties of object. Only derived classes know names and quantities of
     * properties.
     *
     * @return true when properties have been initialized successfully
     */
Xaver Hugl's avatar
Xaver Hugl committed
89
    bool initProps(const QVector<PropertyDefinition> &&vector, uint32_t objectType);
90

Xaver Hugl's avatar
Xaver Hugl committed
91
92
93
94
95
96
    template <typename T>
    void deleteProp(T prop)
    {
        delete m_props[static_cast<uint32_t>(prop)];
        m_props[static_cast<uint32_t>(prop)] = nullptr;
    }
97

98
    class Property;
99
    bool atomicAddProperty(drmModeAtomicReq *req, Property *property) const;
100

Yuri Chornoivan's avatar
Yuri Chornoivan committed
101
    // for comparison with received name of DRM object
102
    QVector<Property *> m_props;
103
104
105
106

    class Property
    {
    public:
Xaver Hugl's avatar
Xaver Hugl committed
107
        Property(drmModePropertyRes *prop, uint64_t val, const QVector<QByteArray> &enumNames, drmModePropertyBlobRes *blob);
108
109
110
111
        virtual ~Property();

        void initEnumMap(drmModePropertyRes *prop);

112
113
114
115
116
117
118
119
        /**
         * For properties of enum type the enum map identifies the kernel runtime values,
         * which must be queried beforehand.
         *
         * @param n the index to the enum
         * @return the runtime enum value corresponding with enum index @param n
         */
        uint64_t enumMap(int n) const {
120
121
            return m_enumMap[n];    // TODO: test on index out of bounds?
        }
122
123
124
        bool hasEnum(uint64_t value) const {
            return m_enumMap.contains(value);
        }
125

126
        uint32_t propId() const {
127
128
            return m_propId;
        }
129
        uint64_t value() const {
130
131
132
133
134
            return m_value;
        }
        void setValue(uint64_t new_value) {
            m_value = new_value;
        }
135
136
137
        const QByteArray &name() const {
            return m_propName;
        }
138
139
140
        bool isImmutable() const {
            return m_immutable;
        }
141
142
143
        bool isAtomic() const {
            return m_atomic;
        }
Xaver Hugl's avatar
Xaver Hugl committed
144
145
146
        drmModePropertyBlobRes *blob() const {
            return m_blob.data();
        }
147
148
149
150
151
152
153
154

    private:
        uint32_t m_propId = 0;
        QByteArray m_propName;

        uint64_t m_value = 0;
        QVector<uint64_t> m_enumMap;
        QVector<QByteArray> m_enumNames;
155
156
        const bool m_immutable;
        const bool m_atomic;
Xaver Hugl's avatar
Xaver Hugl committed
157
        DrmScopedPointer<drmModePropertyBlobRes> m_blob;
158
    };
159
160

private:
161
    DrmGpu *m_gpu;
162
    QVector<QByteArray> m_propsNames;
163
    const uint32_t m_id;
164
165
166
167
};

}

168
QDebug& operator<<(QDebug& stream, const KWin::DrmObject*);