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#include "surface_dev.h"
16#include <unistd.h>
17#include "drm_driver.h"
18#include "fbdev_driver.h"
19#include "log/log.h"
20#include "updater_ui_const.h"
21
22namespace Updater {
23std::vector<std::string> SurfaceDev::fbDeviceList_ {FB_DEV_PATH};
24
25std::unique_ptr<GraphicDrv> SurfaceDev::MakeDevDrv(DevType devType)
26{
27    std::unique_ptr<GraphicDrv> drv = nullptr;
28    switch (devType) {
29        case DevType::DRM_DEVICE:
30            drv = std::make_unique<DrmDriver>();
31            break;
32        case DevType::FB_DEVICE:
33            drv = std::make_unique<FbdevDriver>();
34            break;
35        default:
36            LOG(ERROR) << " not Support.";
37            break;
38    }
39    return drv;
40}
41
42void SurfaceDev::Flip(const uint8_t *buf) const
43{
44    if (buf == nullptr) {
45        LOG(ERROR) << "buf is null";
46        return;
47    }
48    if (drv_ != nullptr) {
49        drv_->Flip(buf);
50    }
51}
52
53DevType SurfaceDev::GetDevType() const
54{
55    bool isFbDevice = std::find_if(fbDeviceList_.begin(), fbDeviceList_.end(), [] (auto &devicePath) {
56        if (access(devicePath.c_str(), 0) != 0) {
57            LOG(WARNING) << devicePath << " not existed";
58            return false;
59        }
60        return true;
61    }) != fbDeviceList_.end();
62    if (access(DRM_DEV_PATH, 0) == 0) {
63        return DevType::DRM_DEVICE;
64    } else if (isFbDevice) {
65        return DevType::FB_DEVICE;
66    }
67    return DevType::UNKNOW_DEVICE;
68}
69
70bool SurfaceDev::Init()
71{
72    drv_ = MakeDevDrv(GetDevType());
73    if (drv_ != nullptr) {
74        return drv_->Init();
75    }
76    return false;
77}
78
79void SurfaceDev::Blank(bool blank)
80{
81    if (drv_ != nullptr) {
82        drv_->Blank(blank);
83    }
84}
85
86void SurfaceDev::Exit(void)
87{
88    if (drv_ != nullptr) {
89        drv_->Exit();
90    }
91}
92
93void SurfaceDev::AddFbDevice(const std::string &devicePath)
94{
95    fbDeviceList_.push_back(devicePath);
96}
97
98void SurfaceDev::GetScreenSize(uint16_t &w, uint16_t &h, GrSurface &surface) const
99{
100    if (drv_ != nullptr) {
101        drv_->GetGrSurface(surface);
102    }
103    w = surface.width;
104    h = surface.height;
105    LOG(INFO) << "weight=" << w << " " << "height=" << h;
106}
107
108SurfaceDev::~SurfaceDev()
109{
110    LOG(INFO) << "SurfaceDev end";
111}
112} // namespace Updater
113