• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021–2022 Beijing OSWare Technology Co., Ltd
3  * This file contains confidential and proprietary information of
4  * OSWare Technology Co., Ltd
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include "fb_device.h"
20 #include <cerrno>
21 #include <fcntl.h>
22 #include <memory>
23 #include <securec.h>
24 #include <string>
25 #include "display_common.h"
26 #include "fb_display.h"
27 #include "display_adapter.h"
28 namespace OHOS {
29 namespace HDI {
30 namespace DISPLAY {
Create()31 std::shared_ptr<HdiDeviceInterface> FbDevice::Create()
32 {
33     constexpr const char *FBDEV_PATH = "/dev/graphics/fb0";
34     std::unique_ptr<DisplayAdapter>& adapter = DisplayAdapter::GetInstance();
35     if (adapter == nullptr) {
36         DISPLAY_LOGE("can not get display adapter the fbdevice need it");
37         return nullptr;
38     }
39     int fd = adapter->OpenDevice(FBDEV_PATH, O_RDWR, 0);
40     if (fd < 0) {
41         DISPLAY_LOGE("failed to open fbdev %{public}s", FBDEV_PATH);
42         return nullptr;
43     }
44 
45     auto device = std::make_shared<FbDevice>(fd);
46     return device;
47 }
48 
FbDevice(int fd)49 FbDevice::FbDevice(int fd)
50 {
51     DISPLAY_LOGD();
52     deviceFd_ = fd;
53 }
54 
~FbDevice()55 FbDevice::~FbDevice()
56 {
57     DISPLAY_LOGD();
58     if (deviceFd_ > 0) {
59         close(deviceFd_);
60         deviceFd_ = -1;
61     }
62 }
63 
DiscoveryDisplay()64 std::unordered_map<uint32_t, std::shared_ptr<HdiDisplay>> FbDevice::DiscoveryDisplay()
65 {
66     DISPLAY_LOGD();
67     return displays_;
68 }
69 
Init()70 int32_t FbDevice::Init()
71 {
72     DISPLAY_LOGD();
73     std::vector<int> fds = {deviceFd_};
74     std::shared_ptr<HdiDisplay> display = std::make_shared<FbDisplay>(fds);
75     int ret = display->Init();
76     DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("display init failed"));
77     displays_.emplace(display->GetId(), std::move(display));
78     return DISPLAY_SUCCESS;
79 }
80 
DeInit()81 void FbDevice::DeInit()
82 {
83     DISPLAY_LOGD();
84     displays_.clear();
85 }
86 } // namespace DISPLAY
87 } // namespace HDI
88 } // namespace OHOS
89