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     }
~DrmPropertyEnum()40     ~DrmPropertyEnum() {};
41 
42     uint64_t value;
43     std::string name;
44 };
45 
46 struct DrmProperty {
47     uint32_t propId;
48     uint64_t value;
49     uint32_t type;
50     uint32_t flags;
51     std::string name;
52     std::vector<uint64_t> values;
53     std::vector<DrmPropertyEnum> enums;
54     std::vector<uint32_t> blob_ids;
55 };
56 
57 class DrmDevice : public HdiDeviceInterface, std::enable_shared_from_this<DrmDevice> {
58 public:
59     static std::shared_ptr<HdiDeviceInterface> Create();
60     static uint32_t ConvertToDrmFormat(PixelFormat fmtIn);
61     static int GetDrmFd();
62     DrmDevice();
~DrmDevice()63     ~DrmDevice() override {}
64 
65     std::vector<std::shared_ptr<DrmPlane>> GetDrmPlane(uint32_t pipe, uint32_t type);
66 
67     int32_t GetCrtcProperty(const DrmCrtc &crtc, const std::string &name, DrmProperty &prop);
68     int32_t GetConnectorProperty(const DrmConnector &connector, const std::string &name, DrmProperty &prop);
69     int32_t GetPlaneProperty(const DrmPlane &plane, const std::string &name, DrmProperty &prop);
70 
71     int32_t GetProperty(uint32_t objId, uint32_t objType, const std::string &name, DrmProperty &prop);
72     std::shared_ptr<DrmEncoder> GetDrmEncoderFromId(uint32_t id);
73     std::shared_ptr<DrmConnector> GetDrmConnectorFromId(uint32_t id);
74     std::shared_ptr<DrmCrtc> GetDrmCrtcFromId(uint32_t id);
75     void CreateCrtc(drmModeCrtcPtr c);
76     std::unordered_map<uint32_t, std::shared_ptr<HdiDisplay>> DiscoveryDisplay() override;
77     int32_t Init() override;
78     void DeInit() override;
79     bool HandleHotplug(uint32_t dispId, bool plugIn) override;
80 
81 private:
82     static FdPtr mDrmFd;
83     static std::shared_ptr<DrmDevice> mInstance;
84     void FindAllCrtc(const drmModeResPtr &drmRes);
85     void FindAllEncoder(const drmModeResPtr &drmRes);
86     void FindAllConnector(const drmModeResPtr &drmRes);
87     void FindAllPlane();
88     int InitNetLink();
89     IdMapPtr<HdiDisplay> mDisplays;
90     IdMapPtr<DrmCrtc> mCrtcs;
91     IdMapPtr<DrmEncoder> mEncoders;
92     IdMapPtr<DrmConnector> mConnectors;
93     std::vector<std::shared_ptr<DrmPlane>> mPlanes;
94     std::unordered_map<uint32_t, uint32_t> dispConnectorIdMaps_;
95 };
96 } // namespace OHOS
97 } // namespace HDI
98 } // namespace DISPLAY
99 
100 #endif // DRM_DEVICE_H
101