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#include "dev/graphic_dev.h"
16#include "charger_log.h"
17#include "dev/drm_driver.h"
18#include "dev/fbdev_driver.h"
19#include <unistd.h>
20
21namespace OHOS {
22namespace PowerMgr {
23std::unique_ptr<DisplayDrv> GraphicDev::MakeDrv(DevType devType)
24{
25    std::unique_ptr<DisplayDrv> drv = nullptr;
26    switch (devType) {
27        case DevType::DRM_DEVICE:
28            drv = std::make_unique<DrmDriver>();
29            break;
30        case DevType::FB_DEVICE:
31            drv = std::make_unique<FbdevDriver>();
32            break;
33        default:
34            BATTERY_HILOGE(FEATURE_CHARGING, "not support device type");
35            break;
36    }
37    return drv;
38}
39
40void GraphicDev::Flip(const uint8_t* buf)
41{
42    if (buf == nullptr) {
43        BATTERY_HILOGE(FEATURE_CHARGING, "buf is null pointer");
44        return;
45    }
46    if (drv_ != nullptr) {
47        drv_->Flip(buf);
48    }
49}
50
51DevType GraphicDev::GetDevType()
52{
53    if (access(DRM_DEV_PATH, 0) == 0) {
54        return DevType::DRM_DEVICE;
55    } else if (access(FB_DEV_PATH, 0) == 0) {
56        return DevType::FB_DEVICE;
57    }
58    return DevType::UNKNOW_DEVICE;
59}
60
61bool GraphicDev::Init()
62{
63    drv_ = MakeDrv(GetDevType());
64    if (drv_ != nullptr) {
65        return drv_->Init();
66    }
67    return false;
68}
69
70void GraphicDev::Blank(bool blank)
71{
72    if (drv_ != nullptr) {
73        drv_->Blank(blank);
74    }
75}
76
77void GraphicDev::Exit()
78{
79    if (drv_ != nullptr) {
80        drv_->Exit();
81    }
82}
83
84void GraphicDev::GetScreenSize(uint16_t& w, uint16_t& h)
85{
86    DisplayInfo dsInfo {0};
87    if (drv_ != nullptr) {
88        drv_->GetDisplayInfo(dsInfo);
89    }
90    w = dsInfo.width;
91    h = dsInfo.height;
92}
93} // namespace PowerMgr
94} // namespace OHOS
95