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 0xD002500
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, lib: %{public}s", __func__, DISPLAY_BUFFER_VDI_LIBRARY);
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 loadvid, clear earlier dlerror: %{public}s", __func__, errStr);
68 }
69 libHandle_ = dlopen(DISPLAY_BUFFER_VDI_LIBRARY, RTLD_LAZY);
70 CHECK_NULLPOINTER_RETURN_VALUE(libHandle_, HDF_FAILURE);
71
72 createVdi_ = reinterpret_cast<CreateDisplayBufferVdiFunc>(dlsym(libHandle_, "CreateDisplayBufferVdi"));
73 if (createVdi_ == nullptr) {
74 errStr = dlerror();
75 if (errStr != nullptr) {
76 HDF_LOGE("%{public}s: mapper CreateDisplayBufferVdi dlsym error: %{public}s", __func__, errStr);
77 }
78 dlclose(libHandle_);
79 return HDF_FAILURE;
80 }
81
82 destroyVdi_ = reinterpret_cast<DestroyDisplayBufferVdiFunc>(dlsym(libHandle_, "DestroyDisplayBufferVdi"));
83 if (destroyVdi_ == nullptr) {
84 errStr = dlerror();
85 if (errStr != nullptr) {
86 HDF_LOGE("%{public}s: mapper DestroyDisplayBufferVdi dlsym error: %{public}s", __func__, errStr);
87 }
88 dlclose(libHandle_);
89 return HDF_FAILURE;
90 }
91
92 return HDF_SUCCESS;
93 }
94
FreeMem(const sptr<NativeBuffer> & handle)95 int32_t MapperService::FreeMem(const sptr<NativeBuffer>& handle)
96 {
97 CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
98 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
99 vdiImpl_->FreeMem(*handle->Move());
100 return HDF_SUCCESS;
101 }
102
Mmap(const sptr<NativeBuffer> & handle)103 int32_t MapperService::Mmap(const sptr<NativeBuffer>& handle)
104 {
105 CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
106 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
107 void* retPtr = vdiImpl_->Mmap(*handle->GetBufferHandle());
108 CHECK_NULLPOINTER_RETURN_VALUE(retPtr, HDF_FAILURE);
109 return HDF_SUCCESS;
110 }
111
Unmap(const sptr<NativeBuffer> & handle)112 int32_t MapperService::Unmap(const sptr<NativeBuffer>& handle)
113 {
114 CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
115 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
116 int32_t ret = vdiImpl_->Unmap(*handle->GetBufferHandle());
117 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
118 return ret;
119 }
120
FlushCache(const sptr<NativeBuffer> & handle)121 int32_t MapperService::FlushCache(const sptr<NativeBuffer>& handle)
122 {
123 CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
124 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
125 int32_t ret = vdiImpl_->FlushCache(*handle->GetBufferHandle());
126 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
127 return ret;
128 }
129
InvalidateCache(const sptr<NativeBuffer> & handle)130 int32_t MapperService::InvalidateCache(const sptr<NativeBuffer>& handle)
131 {
132 CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
133 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
134 int32_t ret = vdiImpl_->InvalidateCache(*handle->GetBufferHandle());
135 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
136 return ret;
137 }
138 } // namespace V1_0
139 } // namespace Buffer
140 } // namespace Display
141 } // namespace HDI
142 } // namespace OHOS
143