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     loader_ = DisplayModuleLoader::Create(LIB_NAME_ADAPTER);
42     if (loader_ == nullptr) {
43         DISPLAY_LOGE("can not create loader");
44         return DISPLAY_FAILURE;
45     }
46     initFunc_ = reinterpret_cast<AdapterInitFunc>(loader_->GetSymbol(INIT_FUNCTION_NAME));
47     if (initFunc_ == nullptr) {
48         DISPLAY_LOGE("failed to get function %{public}s", INIT_FUNCTION_NAME);
49         return DISPLAY_FAILURE;
50     }
51     deInitFunc_ = reinterpret_cast<AdapterDeInitFunc>(loader_->GetSymbol(DEINIT_FUNCTION_NAME));
52     if (deInitFunc_ == nullptr) {
53         DISPLAY_LOGE("failed to get function %{public}s", DEINIT_FUNCTION_NAME);
54         return DISPLAY_FAILURE;
55     }
56     int ret = initFunc_(&funcs_);
57     if ((ret != DISPLAY_SUCCESS) || (funcs_ == nullptr)) {
58         DISPLAY_LOGE("failed to get display adapter functions");
59         return DISPLAY_FAILURE;
60     }
61     return DISPLAY_SUCCESS;
62 }
63 
~DisplayAdapter()64 DisplayAdapter::~DisplayAdapter()
65 {
66     if ((funcs_ != nullptr) && (deInitFunc_ != nullptr)) {
67         deInitFunc_(funcs_);
68     }
69 }
70 
OpenDevice(const std::string & pathName,int32_t flags,mode_t mode)71 int32_t DisplayAdapter::OpenDevice(const std::string &pathName, int32_t flags, mode_t mode)
72 {
73     if (funcs_->OpenDevice != nullptr) {
74         DISPLAY_LOGD("use the adapter open function");
75         return funcs_->OpenDevice(pathName.c_str(), flags, mode);
76     } else {
77         return open(pathName.c_str(), flags, mode);
78     }
79 }
80 
CloseDevice(int32_t devFd)81 int32_t DisplayAdapter::CloseDevice(int32_t devFd)
82 {
83     if (funcs_->CloseDevice != nullptr) {
84         DISPLAY_LOGD("use the adapter close function");
85         return funcs_->CloseDevice(devFd);
86     } else {
87         return close(devFd);
88     }
89 }
90 
Ioctl(int32_t devFd,uint32_t cmd,void * args)91 int32_t DisplayAdapter::Ioctl(int32_t devFd, uint32_t cmd, void *args)
92 {
93     int32_t ret = -1;
94     if (funcs_->Ioctl != nullptr) {
95         DISPLAY_LOGD("use the adapter ioctl function");
96         ret = funcs_->Ioctl(devFd, cmd, args);
97     } else {
98         ret = ioctl(devFd, cmd, args);
99     }
100     return ret;
101 }
102 
FbGetDmaBuffer(int32_t devFd)103 int32_t DisplayAdapter::FbGetDmaBuffer(int32_t devFd)
104 {
105     if (funcs_->FbGetDmaBuffer == nullptr) {
106         DISPLAY_LOGE("has no function to get fb dmabuffer");
107         return -1;
108     }
109     return funcs_->FbGetDmaBuffer(devFd);
110 }
111 
FbFresh(int32_t devFd,DisplayFrameInfo & frame)112 int32_t DisplayAdapter::FbFresh(int32_t devFd, DisplayFrameInfo &frame)
113 {
114     if (funcs_->FbFresh == nullptr) {
115         DISPLAY_LOGE("has no function to fresh fb");
116         return -1;
117     }
118     return funcs_->FbFresh(devFd, &frame);
119 }
120 } // namespace DISPLAY
121 } // namespace HDI
122 } // namespace OHOS