1 /*
2 * Copyright (c) 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 #include "fb_device.h"
17 #include <cerrno>
18 #include <fcntl.h>
19 #include <memory>
20 #include <securec.h>
21 #include <string>
22 #include "display_log.h"
23 #include "fb_display.h"
24 #include "display_adapter.h"
25 namespace OHOS {
26 namespace HDI {
27 namespace DISPLAY {
Create()28 std::shared_ptr<HdiDeviceInterface> FbDevice::Create()
29 {
30 constexpr const char *FBDEV_PATH = "/dev/graphics/fb0";
31 std::unique_ptr<DisplayAdapter>& adapter = DisplayAdapter::GetInstance();
32 if (adapter == nullptr) {
33 DISPLAY_LOGE("can not get display adapter the fbdevice need it");
34 return nullptr;
35 }
36 int fd = adapter->OpenDevice(FBDEV_PATH, O_RDWR, 0);
37 if (fd < 0) {
38 DISPLAY_LOGE("failed to open fbdev %{public}s", FBDEV_PATH);
39 return nullptr;
40 }
41
42 auto device = std::make_shared<FbDevice>(fd);
43 return device;
44 }
45
FbDevice(int fd)46 FbDevice::FbDevice(int fd)
47 {
48 DISPLAY_LOGD();
49 deviceFd_ = fd;
50 }
51
~FbDevice()52 FbDevice::~FbDevice()
53 {
54 DISPLAY_LOGD();
55 if (deviceFd_ > 0) {
56 close(deviceFd_);
57 deviceFd_ = -1;
58 }
59 }
60
DiscoveryDisplay()61 std::unordered_map<uint32_t, std::shared_ptr<HdiDisplay>> FbDevice::DiscoveryDisplay()
62 {
63 DISPLAY_LOGD();
64 return displays_;
65 }
66
Init()67 int32_t FbDevice::Init()
68 {
69 DISPLAY_LOGD();
70 std::vector<int> fds = {deviceFd_};
71 std::shared_ptr<HdiDisplay> display = std::make_shared<FbDisplay>(fds);
72 int ret = display->Init();
73 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("display init failed"));
74 displays_.emplace(display->GetId(), std::move(display));
75 return DISPLAY_SUCCESS;
76 }
77
DeInit()78 void FbDevice::DeInit()
79 {
80 DISPLAY_LOGD();
81 displays_.clear();
82 }
83 } // namespace DISPLAY
84 } // namespace HDI
85 } // namespace OHOS
86