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 #include "display_adapter.h"
16 #include <cstdarg>
17 #include <fcntl.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20 namespace OHOS {
21 namespace HDI {
22 namespace DISPLAY {
GetInstance()23 std::unique_ptr<DisplayAdapter> &DisplayAdapter::GetInstance()
24 {
25 static std::unique_ptr<DisplayAdapter> instance = nullptr;
26 static std::once_flag once;
27 std::call_once(once, [&]() {
28 auto adapter = std::make_unique<DisplayAdapter>();
29 if (adapter != nullptr) {
30 int ret = adapter->Init();
31 if (ret == DISPLAY_SUCCESS) {
32 instance = std::move(adapter);
33 }
34 }
35 });
36 return instance;
37 }
38
Init()39 int32_t DisplayAdapter::Init()
40 {
41 DISPLAY_LOGI();
42 loader_ = DisplayModuleLoader::Create(LIB_NAME_ADAPTER);
43 if (loader_ == nullptr) {
44 DISPLAY_LOGE("can not create loader");
45 return DISPLAY_FAILURE;
46 }
47 initFunc_ = reinterpret_cast<AdapterInitFunc>(loader_->GetSymbol(INIT_FUNCTION_NAME));
48 if (initFunc_ == nullptr) {
49 DISPLAY_LOGE("failed to get function %{public}s", INIT_FUNCTION_NAME);
50 return DISPLAY_FAILURE;
51 }
52 deInitFunc_ = reinterpret_cast<AdapterDeInitFunc>(loader_->GetSymbol(DEINIT_FUNCTION_NAME));
53 if (deInitFunc_ == nullptr) {
54 DISPLAY_LOGE("failed to get function %{public}s", DEINIT_FUNCTION_NAME);
55 return DISPLAY_FAILURE;
56 }
57 int ret = initFunc_(&funcs_);
58 if ((ret != DISPLAY_SUCCESS) || (funcs_ == nullptr)) {
59 DISPLAY_LOGE("failed to get display adapter functions");
60 return DISPLAY_FAILURE;
61 }
62 DISPLAY_LOGI();
63 return DISPLAY_SUCCESS;
64 }
65
~DisplayAdapter()66 DisplayAdapter::~DisplayAdapter()
67 {
68 if ((funcs_ != nullptr) && (deInitFunc_ != nullptr)) {
69 deInitFunc_(funcs_);
70 }
71 }
72
OpenDevice(const std::string & pathName,int32_t flags,mode_t mode)73 int32_t DisplayAdapter::OpenDevice(const std::string &pathName, int32_t flags, mode_t mode)
74 {
75 if (funcs_->OpenDevice != nullptr) {
76 DISPLAY_LOGI("use the adapter open function");
77 return funcs_->OpenDevice(pathName.c_str(), flags, mode);
78 } else {
79 DISPLAY_LOGI("use the open function");
80 return open(pathName.c_str(), flags, mode);
81 }
82 }
83
CloseDevice(int32_t devFd)84 int32_t DisplayAdapter::CloseDevice(int32_t devFd)
85 {
86 if (funcs_->CloseDevice != nullptr) {
87 DISPLAY_LOGD("use the adapter close function");
88 return funcs_->CloseDevice(devFd);
89 } else {
90 return close(devFd);
91 }
92 }
93
Ioctl(int32_t devFd,uint32_t cmd,void * args)94 int32_t DisplayAdapter::Ioctl(int32_t devFd, uint32_t cmd, void *args)
95 {
96 int32_t ret = -1;
97 if (funcs_->Ioctl != nullptr) {
98 DISPLAY_LOGD("use the adapter ioctl function");
99 ret = funcs_->Ioctl(devFd, cmd, args);
100 } else {
101 ret = ioctl(devFd, cmd, args);
102 }
103 return ret;
104 }
105
FbGetDmaBuffer(int32_t devFd)106 int32_t DisplayAdapter::FbGetDmaBuffer(int32_t devFd)
107 {
108 if (funcs_->FbGetDmaBuffer == nullptr) {
109 DISPLAY_LOGE("has no function to get fb dmabuffer");
110 return -1;
111 }
112 return funcs_->FbGetDmaBuffer(devFd);
113 }
114
FbFresh(int32_t devFd,DisplayFrameInfo & frame)115 int32_t DisplayAdapter::FbFresh(int32_t devFd, DisplayFrameInfo &frame)
116 {
117 if (funcs_->FbFresh == nullptr) {
118 DISPLAY_LOGE("has no function to fresh fb");
119 return -1;
120 }
121 return funcs_->FbFresh(devFd, &frame);
122 }
123 } // namespace DISPLAY
124 } // namespace HDI
125 } // namespace OHOS