1 /*
2 * Copyright (c) 2023 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_service.h"
17
18 #include <dlfcn.h>
19 #include <hdf_base.h>
20 #include <hdf_log.h>
21 #include "display_log.h"
22
23 #undef LOG_TAG
24 #define LOG_TAG "MAPPER_SRV"
25 #undef LOG_DOMAIN
26 #define LOG_DOMAIN 0xD002515
27
28 namespace OHOS {
29 namespace HDI {
30 namespace Display {
31 namespace Buffer {
32 namespace V1_0 {
MapperImplGetInstance(void)33 extern "C" IMapper* MapperImplGetInstance(void)
34 {
35 return new (std::nothrow) MapperService();
36 }
37
MapperService()38 MapperService::MapperService()
39 : libHandle_(nullptr),
40 vdiImpl_(nullptr),
41 createVdi_(nullptr),
42 destroyVdi_(nullptr)
43 {
44 int32_t ret = LoadVdi();
45 if (ret == HDF_SUCCESS) {
46 vdiImpl_ = createVdi_();
47 CHECK_NULLPOINTER_RETURN(vdiImpl_);
48 } else {
49 HDF_LOGE("%{public}s: Load buffer VDI failed", __func__);
50 }
51 }
52
~MapperService()53 MapperService::~MapperService()
54 {
55 if (destroyVdi_ != nullptr && vdiImpl_ != nullptr) {
56 destroyVdi_(vdiImpl_);
57 }
58 if (libHandle_ != nullptr) {
59 dlclose(libHandle_);
60 }
61 }
62
LoadVdi()63 int32_t MapperService::LoadVdi()
64 {
65 const char* errStr = dlerror();
66 if (errStr != nullptr) {
67 HDF_LOGI("%{public}s: mapper load vdi, clear earlier dlerror: %{public}s", __func__, errStr);
68 }
69 #ifdef BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
70 libHandle_ = dlopen(DISPLAY_BUFFER_VDI_DEFAULT_LIBRARY, RTLD_LAZY);
71 if (libHandle_ == nullptr) {
72 DISPLAY_LOGE("display buffer load vendor vdi default library failed: %{public}s", DISPLAY_BUFFER_VDI_LIBRARY);
73 #endif // BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
74 libHandle_ = dlopen(DISPLAY_BUFFER_VDI_LIBRARY, RTLD_LAZY);
75 DISPLAY_LOGI("display buffer load vendor vdi library: %{public}s", DISPLAY_BUFFER_VDI_LIBRARY);
76 #ifdef BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
77 } else {
78 DISPLAY_LOGI("display buffer load vendor vdi default library: %{public}s", DISPLAY_BUFFER_VDI_LIBRARY);
79 }
80 #endif // BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
81 CHECK_NULLPOINTER_RETURN_VALUE(libHandle_, HDF_FAILURE);
82
83 createVdi_ = reinterpret_cast<CreateDisplayBufferVdiFunc>(dlsym(libHandle_, "CreateDisplayBufferVdi"));
84 if (createVdi_ == nullptr) {
85 errStr = dlerror();
86 if (errStr != nullptr) {
87 HDF_LOGE("%{public}s: mapper CreateDisplayBufferVdi dlsym error: %{public}s", __func__, errStr);
88 }
89 dlclose(libHandle_);
90 return HDF_FAILURE;
91 }
92
93 destroyVdi_ = reinterpret_cast<DestroyDisplayBufferVdiFunc>(dlsym(libHandle_, "DestroyDisplayBufferVdi"));
94 if (destroyVdi_ == nullptr) {
95 errStr = dlerror();
96 if (errStr != nullptr) {
97 HDF_LOGE("%{public}s: mapper DestroyDisplayBufferVdi dlsym error: %{public}s", __func__, errStr);
98 }
99 dlclose(libHandle_);
100 return HDF_FAILURE;
101 }
102
103 return HDF_SUCCESS;
104 }
105
FreeMem(const sptr<NativeBuffer> & handle)106 int32_t MapperService::FreeMem(const sptr<NativeBuffer>& handle)
107 {
108 CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
109 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
110 vdiImpl_->FreeMem(*handle->Move());
111 return HDF_SUCCESS;
112 }
113
Mmap(const sptr<NativeBuffer> & handle)114 int32_t MapperService::Mmap(const sptr<NativeBuffer>& handle)
115 {
116 CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
117 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
118 void* retPtr = vdiImpl_->Mmap(*handle->GetBufferHandle());
119 CHECK_NULLPOINTER_RETURN_VALUE(retPtr, HDF_FAILURE);
120 return HDF_SUCCESS;
121 }
122
Unmap(const sptr<NativeBuffer> & handle)123 int32_t MapperService::Unmap(const sptr<NativeBuffer>& handle)
124 {
125 CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
126 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
127 int32_t ret = vdiImpl_->Unmap(*handle->GetBufferHandle());
128 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
129 return ret;
130 }
131
FlushCache(const sptr<NativeBuffer> & handle)132 int32_t MapperService::FlushCache(const sptr<NativeBuffer>& handle)
133 {
134 CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
135 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
136 int32_t ret = vdiImpl_->FlushCache(*handle->GetBufferHandle());
137 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
138 return ret;
139 }
140
InvalidateCache(const sptr<NativeBuffer> & handle)141 int32_t MapperService::InvalidateCache(const sptr<NativeBuffer>& handle)
142 {
143 CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
144 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
145 int32_t ret = vdiImpl_->InvalidateCache(*handle->GetBufferHandle());
146 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
147 return ret;
148 }
149 } // namespace V1_0
150 } // namespace Buffer
151 } // namespace Display
152 } // namespace HDI
153 } // namespace OHOS
154