• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "hdf_trace.h"
23 #ifndef DISPLAY_COMMUNITY
24 #include "../../../../../hdf_core/interfaces/inner_api/hdi/base/buffer_util.h"
25 #endif
26 
27 #undef LOG_TAG
28 #define LOG_TAG "MAPPER_SRV"
29 #undef LOG_DOMAIN
30 #define LOG_DOMAIN 0xD002515
31 #undef DISPLAY_TRACE
32 #define DISPLAY_TRACE HdfTrace trace(__func__, "HDI:DISP:")
33 
34 namespace OHOS {
35 namespace HDI {
36 namespace Display {
37 namespace Buffer {
38 namespace V1_0 {
MapperImplGetInstance(void)39 extern "C" Buffer::V1_2::IMapper* MapperImplGetInstance(void)
40 {
41     return new (std::nothrow) MapperService();
42 }
43 
MapperService()44 MapperService::MapperService()
45     : libHandle_(nullptr),
46     vdiImpl_(nullptr),
47     createVdi_(nullptr),
48     destroyVdi_(nullptr)
49 {
50     int32_t ret = LoadVdi();
51     if (ret == HDF_SUCCESS) {
52         vdiImpl_ = createVdi_();
53         CHECK_NULLPOINTER_RETURN(vdiImpl_);
54     } else {
55         HDF_LOGE("%{public}s: Load buffer VDI failed", __func__);
56     }
57 }
58 
~MapperService()59 MapperService::~MapperService()
60 {
61     std::lock_guard<std::mutex> lck(mutex_);
62     if (destroyVdi_ != nullptr && vdiImpl_ != nullptr) {
63         destroyVdi_(vdiImpl_);
64         vdiImpl_ = nullptr;
65         destroyVdi_ = nullptr;
66     }
67     if (libHandle_ != nullptr) {
68         dlclose(libHandle_);
69         libHandle_ = nullptr;
70     }
71 }
72 
LoadVdi()73 int32_t MapperService::LoadVdi()
74 {
75     const char* errStr = dlerror();
76     if (errStr != nullptr) {
77         HDF_LOGD("%{public}s: mapper load vdi, clear earlier dlerror: %{public}s", __func__, errStr);
78     }
79 #ifdef BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
80     libHandle_ = dlopen(DISPLAY_BUFFER_VDI_DEFAULT_LIBRARY, RTLD_LAZY);
81     if (libHandle_ == nullptr) {
82         DISPLAY_LOGE("display buffer load vendor vdi default library failed: %{public}s", DISPLAY_BUFFER_VDI_LIBRARY);
83 #endif // BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
84         libHandle_ = dlopen(DISPLAY_BUFFER_VDI_LIBRARY, RTLD_LAZY);
85         DISPLAY_LOGD("display buffer load vendor vdi library: %{public}s", DISPLAY_BUFFER_VDI_LIBRARY);
86 #ifdef BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
87     } else {
88         DISPLAY_LOGD("display buffer load vendor vdi default library: %{public}s", DISPLAY_BUFFER_VDI_LIBRARY);
89     }
90 #endif // BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
91     CHECK_NULLPOINTER_RETURN_VALUE(libHandle_, HDF_FAILURE);
92 
93     createVdi_ = reinterpret_cast<CreateDisplayBufferVdiFunc>(dlsym(libHandle_, "CreateDisplayBufferVdi"));
94     if (createVdi_ == nullptr) {
95         errStr = dlerror();
96         if (errStr != nullptr) {
97             HDF_LOGE("%{public}s: mapper CreateDisplayBufferVdi dlsym error: %{public}s", __func__, errStr);
98         }
99         dlclose(libHandle_);
100         return HDF_FAILURE;
101     }
102 
103     destroyVdi_ = reinterpret_cast<DestroyDisplayBufferVdiFunc>(dlsym(libHandle_, "DestroyDisplayBufferVdi"));
104     if (destroyVdi_ == nullptr) {
105         errStr = dlerror();
106         if (errStr != nullptr) {
107             HDF_LOGE("%{public}s: mapper DestroyDisplayBufferVdi dlsym error: %{public}s", __func__, errStr);
108         }
109         dlclose(libHandle_);
110         return HDF_FAILURE;
111     }
112 
113     return HDF_SUCCESS;
114 }
115 
FreeMem(const sptr<NativeBuffer> & handle)116 int32_t MapperService::FreeMem(const sptr<NativeBuffer>& handle)
117 {
118     DISPLAY_TRACE;
119     CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
120     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
121 
122     BufferHandle* buffer = handle->Move();
123     CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_FAILURE);
124     vdiImpl_->FreeMem(*buffer);
125 #ifndef DISPLAY_COMMUNITY
126     FreeNativeBufferHandle(buffer);
127     buffer = nullptr;
128 #endif
129     return HDF_SUCCESS;
130 }
131 
Mmap(const sptr<NativeBuffer> & handle)132 int32_t MapperService::Mmap(const sptr<NativeBuffer>& handle)
133 {
134     DISPLAY_TRACE;
135     CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
136     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
137 
138     BufferHandle* buffer = handle->GetBufferHandle();
139     CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_FAILURE);
140     void* retPtr = vdiImpl_->Mmap(*buffer);
141     CHECK_NULLPOINTER_RETURN_VALUE(retPtr, HDF_FAILURE);
142     return HDF_SUCCESS;
143 }
144 
Unmap(const sptr<NativeBuffer> & handle)145 int32_t MapperService::Unmap(const sptr<NativeBuffer>& handle)
146 {
147     DISPLAY_TRACE;
148     CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
149     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
150 
151     BufferHandle* buffer = handle->GetBufferHandle();
152     CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_FAILURE);
153     int32_t ret = vdiImpl_->Unmap(*buffer);
154     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
155     return ret;
156 }
157 
FlushCache(const sptr<NativeBuffer> & handle)158 int32_t MapperService::FlushCache(const sptr<NativeBuffer>& handle)
159 {
160     DISPLAY_TRACE;
161     CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
162     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
163 
164     BufferHandle* buffer = handle->GetBufferHandle();
165     CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_FAILURE);
166     int32_t ret = vdiImpl_->FlushCache(*buffer);
167     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
168     return ret;
169 }
170 
InvalidateCache(const sptr<NativeBuffer> & handle)171 int32_t MapperService::InvalidateCache(const sptr<NativeBuffer>& handle)
172 {
173     DISPLAY_TRACE;
174     CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
175     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
176 
177     BufferHandle* buffer = handle->GetBufferHandle();
178     CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_FAILURE);
179     int32_t ret = vdiImpl_->InvalidateCache(*buffer);
180     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
181     return ret;
182 }
183 
GetImageLayout(const sptr<NativeBuffer> & handle,V1_2::ImageLayout & layout)184 int32_t MapperService::GetImageLayout(const sptr<NativeBuffer>& handle, V1_2::ImageLayout& layout)
185 {
186     CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
187     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
188 
189     BufferHandle* buffer = handle->GetBufferHandle();
190     CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_FAILURE);
191     int32_t ret = vdiImpl_->GetImageLayout(*buffer, layout);
192     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
193     return ret;
194 }
195 } // namespace V1_2
196 } // namespace Buffer
197 } // namespace Display
198 } // namespace HDI
199 } // namespace OHOS
200