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