1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef DRM_DEVICE_H
17 #define DRM_DEVICE_H
18 #include <unordered_map>
19 #include <memory>
20 #include <xf86drm.h>
21 #include <xf86drmMode.h>
22 #include "drm_connector.h"
23 #include "drm_crtc.h"
24 #include "drm_encoder.h"
25 #include "drm_plane.h"
26 #include "hdi_device_common.h"
27 #include "hdi_device_interface.h"
28 #include "hdi_display.h"
29 #include "hdi_shared_fd.h"
30 
31 namespace OHOS {
32     namespace HDI {
33         namespace DISPLAY {
34 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
35 
36             class DrmPropertyEnum {
37             public:
DrmPropertyEnum(drm_mode_property_enum *e)38                 explicit DrmPropertyEnum(drm_mode_property_enum *e) : value(e->value), name(e->name)
39                 {
40                 }
~DrmPropertyEnum()41                 ~DrmPropertyEnum() {};
42 
43                 uint64_t value;
44                 std::string name;
45             };
46 
47             struct DrmProperty {
48                 uint32_t propId;
49                 uint64_t value;
50                 uint32_t type;
51                 uint32_t flags;
52                 std::string name;
53                 std::vector<uint64_t> values;
54                 std::vector<DrmPropertyEnum> enums;
55                 std::vector<uint32_t> blob_ids;
56             };
57 
58             class DrmDevice : public HdiDeviceInterface, std::enable_shared_from_this<DrmDevice> {
59             public:
60                 static std::shared_ptr<HdiDeviceInterface> Create();
61                 static uint32_t ConvertToDrmFormat(PixelFormat fmtIn);
62                 static int GetDrmFd();
63                 DrmDevice();
64                 ~DrmDevice() override
65                 {
66                 }
67 
68                 std::vector<std::shared_ptr<DrmPlane>> GetDrmPlane(uint32_t pipe, uint32_t type);
69 
70                 int32_t GetCrtcProperty(const DrmCrtc &crtc, const std::string &name, DrmProperty &prop);
71                 int32_t GetConnectorProperty(const DrmConnector &connector, const std::string &name, DrmProperty &prop);
72                 int32_t GetPlaneProperty(const DrmPlane &plane, const std::string &name, DrmProperty &prop);
73 
74                 int32_t GetProperty(uint32_t objId, uint32_t objType, const std::string &name, DrmProperty &prop);
75                 std::shared_ptr<DrmEncoder> GetDrmEncoderFromId(uint32_t id);
76                 std::shared_ptr<DrmConnector> GetDrmConnectorFromId(uint32_t id);
77                 std::shared_ptr<DrmCrtc> GetDrmCrtcFromId(uint32_t id);
78                 void CreateCrtc(drmModeCrtcPtr c);
79                 std::unordered_map<uint32_t, std::shared_ptr<HdiDisplay>> DiscoveryDisplay() override;
80                 int32_t Init() override;
81                 void DeInit() override;
82                 bool HandleHotplug(uint32_t dispId, bool plugIn) override;
83 
84             private:
85                 static FdPtr mDrmFd;
86                 static std::shared_ptr<DrmDevice> mInstance;
87                 void FindAllCrtc(const drmModeResPtr &drmRes);
88                 void FindAllEncoder(const drmModeResPtr &drmRes);
89                 void FindAllConnector(const drmModeResPtr &drmRes);
90                 void FindAllPlane();
91                 int InitNetLink();
92                 IdMapPtr<HdiDisplay> mDisplays;
93                 IdMapPtr<DrmCrtc> mCrtcs;
94                 IdMapPtr<DrmEncoder> mEncoders;
95                 IdMapPtr<DrmConnector> mConnectors;
96                 std::vector<std::shared_ptr<DrmPlane>> mPlanes;
97                 std::unordered_map<uint32_t, uint32_t> dispConnectorIdMaps_;
98             };
99         } // namespace OHOS
100     }     // namespace HDI
101 } // namespace DISPLAY
102 
103 #endif // DRM_DEVICE_H
104