• 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 #include "display_adapter.h"
19 #include <cstdarg>
20 #include <fcntl.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 namespace OHOS {
24 namespace HDI {
25 namespace DISPLAY {
GetInstance()26 std::unique_ptr<DisplayAdapter> &DisplayAdapter::GetInstance()
27 {
28     static std::unique_ptr<DisplayAdapter> instance = nullptr;
29     static std::once_flag once;
30     std::call_once(once, [&]() {
31         auto adapter = std::make_unique<DisplayAdapter>();
32         if (adapter != nullptr) {
33             int ret = adapter->Init();
34             if (ret == DISPLAY_SUCCESS) {
35                 instance = std::move(adapter);
36             }
37         }
38     });
39     return instance;
40 }
41 
Init()42 int32_t DisplayAdapter::Init()
43 {
44     loader_ = DisplayModuleLoader::Create(LIB_NAME_ADAPTER);
45     if (loader_ == nullptr) {
46         DISPLAY_LOGE("can not create loader");
47         return DISPLAY_FAILURE;
48     }
49     initFunc_ = reinterpret_cast<AdapterInitFunc>(loader_->GetSymbol(INIT_FUNCTION_NAME));
50     if (initFunc_ == nullptr) {
51         DISPLAY_LOGE("failed to get function %{public}s", INIT_FUNCTION_NAME);
52         return DISPLAY_FAILURE;
53     }
54     deInitFunc_ = reinterpret_cast<AdapterDeInitFunc>(loader_->GetSymbol(DEINIT_FUNCTION_NAME));
55     if (deInitFunc_ == nullptr) {
56         DISPLAY_LOGE("failed to get function %{public}s", DEINIT_FUNCTION_NAME);
57         return DISPLAY_FAILURE;
58     }
59     int ret = initFunc_(&funcs_);
60     if ((ret != DISPLAY_SUCCESS) || (funcs_ == nullptr)) {
61         DISPLAY_LOGE("failed to get display adapter functions");
62         return DISPLAY_FAILURE;
63     }
64     return DISPLAY_SUCCESS;
65 }
66 
~DisplayAdapter()67 DisplayAdapter::~DisplayAdapter()
68 {
69     if ((funcs_ != nullptr) && (deInitFunc_ != nullptr)) {
70         deInitFunc_(funcs_);
71     }
72 }
73 
OpenDevice(const std::string & pathName,int32_t flags,mode_t mode)74 int32_t DisplayAdapter::OpenDevice(const std::string &pathName, int32_t flags, mode_t mode)
75 {
76     if (funcs_->OpenDevice != nullptr) {
77         DISPLAY_LOGD("use the adapter open function");
78         return funcs_->OpenDevice(pathName.c_str(), flags, mode);
79     } else {
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