1 /*
2  * Copyright (c) 2023 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_DRIVER_H
17 #define DRM_DRIVER_H
18 
19 #include "display_drv.h"
20 #include "nocopyable.h"
21 #include <cerrno>
22 #include <cstdio>
23 #include <cstdlib>
24 #include <cstring>
25 #include <drm_fourcc.h>
26 #include <fcntl.h>
27 #include <poll.h>
28 #include <sys/mman.h>
29 #include <xf86drm.h>
30 #include <xf86drmMode.h>
31 
32 namespace OHOS {
33 namespace PowerMgr {
34 struct BufferObject {
35     uint32_t width {};
36     uint32_t height {};
37     uint32_t pitch {};
38     uint32_t handle {};
39     uint32_t size {};
40     uint8_t* vaddr {};
41     uint32_t fbId {};
42 };
43 
44 class DrmDriver : public DisplayDrv {
45     DISALLOW_COPY_AND_MOVE(DrmDriver);
46 
47 public:
DrmDriver()48     DrmDriver() : conn_(nullptr), res_(nullptr), crtc_(nullptr) {}
49     ~DrmDriver() override;
50     bool Init() override;
51     void Flip(const uint8_t* buf) override;
52     void Blank(bool blank) override;
53     void Exit() override;
54     void GetDisplayInfo(DisplayInfo& dsInfo) override;
55 
56 private:
57     int ModesetCreateFb(struct BufferObject* bo);
58     void ModesetDestroyFb(struct BufferObject* bo);
59     int DrmInit();
60     drmModeCrtc* GetCrtc(const drmModeRes& res, const int fd, const drmModeConnector& conn) const;
61     drmModeConnector* GetFirstConnector(const drmModeRes& res, const int fd) const;
62     drmModeConnector* GetConnectorByType(const drmModeRes& res, const int fd, const uint32_t type) const;
63     drmModeConnector* GetConnector(const drmModeRes& res, const int fd, uint32_t& modeId) const;
64     drmModeRes* GetResources(int& fd) const;
65     drmModeRes* GetOneResources(const int devIndex, int& fd) const;
66     drmModeConnector* conn_;
67     drmModeRes* res_;
68     drmModeCrtc* crtc_;
69     struct BufferObject buff_ {};
70 };
71 } // namespace PowerMgr
72 } // namespace OHOS
73 #endif
74