1094332d3Sopenharmony_ci/* 2094332d3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License. 5094332d3Sopenharmony_ci * You may obtain a copy of the License at 6094332d3Sopenharmony_ci * 7094332d3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8094332d3Sopenharmony_ci * 9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and 13094332d3Sopenharmony_ci * limitations under the License. 14094332d3Sopenharmony_ci */ 15094332d3Sopenharmony_ci 16094332d3Sopenharmony_ci#include "mapper_service.h" 17094332d3Sopenharmony_ci 18094332d3Sopenharmony_ci#include <dlfcn.h> 19094332d3Sopenharmony_ci#include <hdf_base.h> 20094332d3Sopenharmony_ci#include <hdf_log.h> 21094332d3Sopenharmony_ci#include "display_log.h" 22094332d3Sopenharmony_ci#include "hdf_trace.h" 23094332d3Sopenharmony_ci 24094332d3Sopenharmony_ci#undef LOG_TAG 25094332d3Sopenharmony_ci#define LOG_TAG "MAPPER_SRV" 26094332d3Sopenharmony_ci#undef LOG_DOMAIN 27094332d3Sopenharmony_ci#define LOG_DOMAIN 0xD002515 28094332d3Sopenharmony_ci#undef DISPLAY_TRACE 29094332d3Sopenharmony_ci#define DISPLAY_TRACE HdfTrace trace(__func__, "HDI:DISP:") 30094332d3Sopenharmony_ci 31094332d3Sopenharmony_cinamespace OHOS { 32094332d3Sopenharmony_cinamespace HDI { 33094332d3Sopenharmony_cinamespace Display { 34094332d3Sopenharmony_cinamespace Buffer { 35094332d3Sopenharmony_cinamespace V1_0 { 36094332d3Sopenharmony_ciextern "C" Buffer::V1_2::IMapper* MapperImplGetInstance(void) 37094332d3Sopenharmony_ci{ 38094332d3Sopenharmony_ci return new (std::nothrow) MapperService(); 39094332d3Sopenharmony_ci} 40094332d3Sopenharmony_ci 41094332d3Sopenharmony_ciMapperService::MapperService() 42094332d3Sopenharmony_ci : libHandle_(nullptr), 43094332d3Sopenharmony_ci vdiImpl_(nullptr), 44094332d3Sopenharmony_ci createVdi_(nullptr), 45094332d3Sopenharmony_ci destroyVdi_(nullptr) 46094332d3Sopenharmony_ci{ 47094332d3Sopenharmony_ci int32_t ret = LoadVdi(); 48094332d3Sopenharmony_ci if (ret == HDF_SUCCESS) { 49094332d3Sopenharmony_ci vdiImpl_ = createVdi_(); 50094332d3Sopenharmony_ci CHECK_NULLPOINTER_RETURN(vdiImpl_); 51094332d3Sopenharmony_ci } else { 52094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Load buffer VDI failed", __func__); 53094332d3Sopenharmony_ci } 54094332d3Sopenharmony_ci} 55094332d3Sopenharmony_ci 56094332d3Sopenharmony_ciMapperService::~MapperService() 57094332d3Sopenharmony_ci{ 58094332d3Sopenharmony_ci std::lock_guard<std::mutex> lck(mutex_); 59094332d3Sopenharmony_ci if (destroyVdi_ != nullptr && vdiImpl_ != nullptr) { 60094332d3Sopenharmony_ci destroyVdi_(vdiImpl_); 61094332d3Sopenharmony_ci vdiImpl_ = nullptr; 62094332d3Sopenharmony_ci destroyVdi_ = nullptr; 63094332d3Sopenharmony_ci } 64094332d3Sopenharmony_ci if (libHandle_ != nullptr) { 65094332d3Sopenharmony_ci dlclose(libHandle_); 66094332d3Sopenharmony_ci libHandle_ = nullptr; 67094332d3Sopenharmony_ci } 68094332d3Sopenharmony_ci} 69094332d3Sopenharmony_ci 70094332d3Sopenharmony_ciint32_t MapperService::LoadVdi() 71094332d3Sopenharmony_ci{ 72094332d3Sopenharmony_ci const char* errStr = dlerror(); 73094332d3Sopenharmony_ci if (errStr != nullptr) { 74094332d3Sopenharmony_ci HDF_LOGD("%{public}s: mapper load vdi, clear earlier dlerror: %{public}s", __func__, errStr); 75094332d3Sopenharmony_ci } 76094332d3Sopenharmony_ci#ifdef BUFFER_VDI_DEFAULT_LIBRARY_ENABLE 77094332d3Sopenharmony_ci libHandle_ = dlopen(DISPLAY_BUFFER_VDI_DEFAULT_LIBRARY, RTLD_LAZY); 78094332d3Sopenharmony_ci if (libHandle_ == nullptr) { 79094332d3Sopenharmony_ci DISPLAY_LOGE("display buffer load vendor vdi default library failed: %{public}s", DISPLAY_BUFFER_VDI_LIBRARY); 80094332d3Sopenharmony_ci#endif // BUFFER_VDI_DEFAULT_LIBRARY_ENABLE 81094332d3Sopenharmony_ci libHandle_ = dlopen(DISPLAY_BUFFER_VDI_LIBRARY, RTLD_LAZY); 82094332d3Sopenharmony_ci DISPLAY_LOGD("display buffer load vendor vdi library: %{public}s", DISPLAY_BUFFER_VDI_LIBRARY); 83094332d3Sopenharmony_ci#ifdef BUFFER_VDI_DEFAULT_LIBRARY_ENABLE 84094332d3Sopenharmony_ci } else { 85094332d3Sopenharmony_ci DISPLAY_LOGD("display buffer load vendor vdi default library: %{public}s", DISPLAY_BUFFER_VDI_LIBRARY); 86094332d3Sopenharmony_ci } 87094332d3Sopenharmony_ci#endif // BUFFER_VDI_DEFAULT_LIBRARY_ENABLE 88094332d3Sopenharmony_ci CHECK_NULLPOINTER_RETURN_VALUE(libHandle_, HDF_FAILURE); 89094332d3Sopenharmony_ci 90094332d3Sopenharmony_ci createVdi_ = reinterpret_cast<CreateDisplayBufferVdiFunc>(dlsym(libHandle_, "CreateDisplayBufferVdi")); 91094332d3Sopenharmony_ci if (createVdi_ == nullptr) { 92094332d3Sopenharmony_ci errStr = dlerror(); 93094332d3Sopenharmony_ci if (errStr != nullptr) { 94094332d3Sopenharmony_ci HDF_LOGE("%{public}s: mapper CreateDisplayBufferVdi dlsym error: %{public}s", __func__, errStr); 95094332d3Sopenharmony_ci } 96094332d3Sopenharmony_ci dlclose(libHandle_); 97094332d3Sopenharmony_ci return HDF_FAILURE; 98094332d3Sopenharmony_ci } 99094332d3Sopenharmony_ci 100094332d3Sopenharmony_ci destroyVdi_ = reinterpret_cast<DestroyDisplayBufferVdiFunc>(dlsym(libHandle_, "DestroyDisplayBufferVdi")); 101094332d3Sopenharmony_ci if (destroyVdi_ == nullptr) { 102094332d3Sopenharmony_ci errStr = dlerror(); 103094332d3Sopenharmony_ci if (errStr != nullptr) { 104094332d3Sopenharmony_ci HDF_LOGE("%{public}s: mapper DestroyDisplayBufferVdi dlsym error: %{public}s", __func__, errStr); 105094332d3Sopenharmony_ci } 106094332d3Sopenharmony_ci dlclose(libHandle_); 107094332d3Sopenharmony_ci return HDF_FAILURE; 108094332d3Sopenharmony_ci } 109094332d3Sopenharmony_ci 110094332d3Sopenharmony_ci return HDF_SUCCESS; 111094332d3Sopenharmony_ci} 112094332d3Sopenharmony_ci 113094332d3Sopenharmony_ciint32_t MapperService::FreeMem(const sptr<NativeBuffer>& handle) 114094332d3Sopenharmony_ci{ 115094332d3Sopenharmony_ci DISPLAY_TRACE; 116094332d3Sopenharmony_ci CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE); 117094332d3Sopenharmony_ci CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE); 118094332d3Sopenharmony_ci 119094332d3Sopenharmony_ci BufferHandle* buffer = handle->Move(); 120094332d3Sopenharmony_ci CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_FAILURE); 121094332d3Sopenharmony_ci vdiImpl_->FreeMem(*buffer); 122094332d3Sopenharmony_ci return HDF_SUCCESS; 123094332d3Sopenharmony_ci} 124094332d3Sopenharmony_ci 125094332d3Sopenharmony_ciint32_t MapperService::Mmap(const sptr<NativeBuffer>& handle) 126094332d3Sopenharmony_ci{ 127094332d3Sopenharmony_ci DISPLAY_TRACE; 128094332d3Sopenharmony_ci CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE); 129094332d3Sopenharmony_ci CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE); 130094332d3Sopenharmony_ci 131094332d3Sopenharmony_ci BufferHandle* buffer = handle->GetBufferHandle(); 132094332d3Sopenharmony_ci CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_FAILURE); 133094332d3Sopenharmony_ci void* retPtr = vdiImpl_->Mmap(*buffer); 134094332d3Sopenharmony_ci CHECK_NULLPOINTER_RETURN_VALUE(retPtr, HDF_FAILURE); 135094332d3Sopenharmony_ci return HDF_SUCCESS; 136094332d3Sopenharmony_ci} 137094332d3Sopenharmony_ci 138094332d3Sopenharmony_ciint32_t MapperService::Unmap(const sptr<NativeBuffer>& handle) 139094332d3Sopenharmony_ci{ 140094332d3Sopenharmony_ci DISPLAY_TRACE; 141094332d3Sopenharmony_ci CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE); 142094332d3Sopenharmony_ci CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE); 143094332d3Sopenharmony_ci 144094332d3Sopenharmony_ci BufferHandle* buffer = handle->GetBufferHandle(); 145094332d3Sopenharmony_ci CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_FAILURE); 146094332d3Sopenharmony_ci int32_t ret = vdiImpl_->Unmap(*buffer); 147094332d3Sopenharmony_ci DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail")); 148094332d3Sopenharmony_ci return ret; 149094332d3Sopenharmony_ci} 150094332d3Sopenharmony_ci 151094332d3Sopenharmony_ciint32_t MapperService::FlushCache(const sptr<NativeBuffer>& handle) 152094332d3Sopenharmony_ci{ 153094332d3Sopenharmony_ci DISPLAY_TRACE; 154094332d3Sopenharmony_ci CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE); 155094332d3Sopenharmony_ci CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE); 156094332d3Sopenharmony_ci 157094332d3Sopenharmony_ci BufferHandle* buffer = handle->GetBufferHandle(); 158094332d3Sopenharmony_ci CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_FAILURE); 159094332d3Sopenharmony_ci int32_t ret = vdiImpl_->FlushCache(*buffer); 160094332d3Sopenharmony_ci DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail")); 161094332d3Sopenharmony_ci return ret; 162094332d3Sopenharmony_ci} 163094332d3Sopenharmony_ci 164094332d3Sopenharmony_ciint32_t MapperService::InvalidateCache(const sptr<NativeBuffer>& handle) 165094332d3Sopenharmony_ci{ 166094332d3Sopenharmony_ci DISPLAY_TRACE; 167094332d3Sopenharmony_ci CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE); 168094332d3Sopenharmony_ci CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE); 169094332d3Sopenharmony_ci 170094332d3Sopenharmony_ci BufferHandle* buffer = handle->GetBufferHandle(); 171094332d3Sopenharmony_ci CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_FAILURE); 172094332d3Sopenharmony_ci int32_t ret = vdiImpl_->InvalidateCache(*buffer); 173094332d3Sopenharmony_ci DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail")); 174094332d3Sopenharmony_ci return ret; 175094332d3Sopenharmony_ci} 176094332d3Sopenharmony_ci 177094332d3Sopenharmony_ciint32_t MapperService::GetImageLayout(const sptr<NativeBuffer>& handle, V1_2::ImageLayout& layout) 178094332d3Sopenharmony_ci{ 179094332d3Sopenharmony_ci CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE); 180094332d3Sopenharmony_ci CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE); 181094332d3Sopenharmony_ci 182094332d3Sopenharmony_ci BufferHandle* buffer = handle->GetBufferHandle(); 183094332d3Sopenharmony_ci CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_FAILURE); 184094332d3Sopenharmony_ci int32_t ret = vdiImpl_->GetImageLayout(*buffer, layout); 185094332d3Sopenharmony_ci DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail")); 186094332d3Sopenharmony_ci return ret; 187094332d3Sopenharmony_ci} 188094332d3Sopenharmony_ci} // namespace V1_2 189094332d3Sopenharmony_ci} // namespace Buffer 190094332d3Sopenharmony_ci} // namespace Display 191094332d3Sopenharmony_ci} // namespace HDI 192094332d3Sopenharmony_ci} // namespace OHOS 193