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 "mapper_interface_service.h"
17 #include <dlfcn.h>
18 #include <hdf_base.h>
19 #include "display_log.h"
20 #include "hdf_log.h"
21
22 namespace OHOS {
23 namespace HDI {
24 namespace Display {
25 namespace Buffer {
26 namespace V1_0 {
MapperInterfaceImplGetInstance(void)27 extern "C" IMapperInterface *MapperInterfaceImplGetInstance(void)
28 {
29 return new (std::nothrow) MapperInterfaceService();
30 }
31
MapperInterfaceService()32 MapperInterfaceService::MapperInterfaceService()
33 : libHandle_(nullptr),
34 hwiImpl_(nullptr),
35 createHwi_(nullptr),
36 destroyHwi_(nullptr)
37 {
38 int32_t ret = LoadHwi();
39 if (ret == HDF_SUCCESS) {
40 hwiImpl_ = createHwi_();
41 CHECK_NULLPOINTER_RETURN(hwiImpl_);
42 } else {
43 HDF_LOGE("error: LoadHwi failure, lib path:%{public}s", DISPLAY_BUFFER_HWI_LIBRARY_PATH);
44 }
45 }
46
~MapperInterfaceService()47 MapperInterfaceService::~MapperInterfaceService()
48 {
49 if (destroyHwi_ != nullptr && hwiImpl_ != nullptr) {
50 destroyHwi_(hwiImpl_);
51 }
52 if (libHandle_ != nullptr) {
53 dlclose(libHandle_);
54 }
55 }
56
LoadHwi()57 int32_t MapperInterfaceService::LoadHwi()
58 {
59 const char *errStr = dlerror();
60 if (errStr) {
61 HDF_LOGI("warning, existing dlerror: %{public}s", errStr);
62 }
63 libHandle_ = dlopen(DISPLAY_BUFFER_HWI_LIBRARY_PATH, RTLD_NOW);
64 CHECK_NULLPOINTER_RETURN_VALUE(libHandle_, HDF_FAILURE);
65
66 createHwi_ = reinterpret_cast<Create_DisplayBufferHwiFunc_t *>(dlsym(libHandle_, "Create_DisplayBufferHwi"));
67 errStr = dlerror();
68 if (errStr) {
69 HDF_LOGE("error: %{public}s", errStr);
70 return HDF_FAILURE;
71 }
72
73 destroyHwi_ = reinterpret_cast<Destroy_DisplayBufferHwiFunc_t *>(dlsym(libHandle_, "Destroy_DisplayBufferHwi"));
74 errStr = dlerror();
75 if (errStr) {
76 HDF_LOGE("error: %{public}s", errStr);
77 return HDF_FAILURE;
78 }
79 return HDF_SUCCESS;
80 }
81
FreeMem(const sptr<BufferHandleParcelable> & handle)82 int32_t MapperInterfaceService::FreeMem(const sptr<BufferHandleParcelable> &handle)
83 {
84 CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
85 hwiImpl_->FreeMem(*handle->Move());
86 return HDF_SUCCESS;
87 }
88
Mmap(const sptr<BufferHandleParcelable> & handle)89 int32_t MapperInterfaceService::Mmap(const sptr<BufferHandleParcelable> &handle)
90 {
91 CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
92 void *retPtr = hwiImpl_->Mmap(*handle->GetBufferHandle());
93 HDF_LOGD("%{public}s@%{public}d virAddr=%{public}p", __func__, __LINE__, handle->GetBufferHandle()->virAddr);
94 CHECK_NULLPOINTER_RETURN_VALUE(retPtr, HDF_FAILURE);
95 return HDF_SUCCESS;
96 }
97
MmapCache(const sptr<BufferHandleParcelable> & handle)98 int32_t MapperInterfaceService::MmapCache(const sptr<BufferHandleParcelable> &handle)
99 {
100 CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
101 void *retPtr = hwiImpl_->MmapCache(*handle->GetBufferHandle());
102 CHECK_NULLPOINTER_RETURN_VALUE(retPtr, HDF_FAILURE);
103 return HDF_SUCCESS;
104 }
105
Unmap(const sptr<BufferHandleParcelable> & handle)106 int32_t MapperInterfaceService::Unmap(const sptr<BufferHandleParcelable> &handle)
107 {
108 CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
109 int32_t ec = hwiImpl_->Unmap(*handle->GetBufferHandle());
110 return ec;
111 }
112
FlushCache(const sptr<BufferHandleParcelable> & handle)113 int32_t MapperInterfaceService::FlushCache(const sptr<BufferHandleParcelable> &handle)
114 {
115 CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
116 int32_t ec = hwiImpl_->FlushCache(*handle->GetBufferHandle());
117 return ec;
118 }
119
FlushMCache(const sptr<BufferHandleParcelable> & handle)120 int32_t MapperInterfaceService::FlushMCache(const sptr<BufferHandleParcelable> &handle)
121 {
122 CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
123 int32_t ec = hwiImpl_->FlushMCache(*handle->GetBufferHandle());
124 return ec;
125 }
126
InvalidateCache(const sptr<BufferHandleParcelable> & handle)127 int32_t MapperInterfaceService::InvalidateCache(const sptr<BufferHandleParcelable> &handle)
128 {
129 CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
130 int32_t ec = hwiImpl_->InvalidateCache(*handle->GetBufferHandle());
131 return ec;
132 }
133 } // namespace V1_0
134 } // namespace Buffer
135 } // namespace Display
136 } // namespace HDI
137 } // namespace OHOS
138