From be74f72b0c9b1645dbb4f7efc6ecc35e6fe70d86 Mon Sep 17 00:00:00 2001 From: yanansong Date: Tue, 20 Jun 2023 20:21:27 +0800 Subject: [PATCH] add hdi for gralloc --- .../hdi_service/gralloc/client/BUILD.gn | 54 ++++++ .../gralloc/client/allocator_adapter.cpp | 53 ++++++ .../gralloc/client/allocator_adapter.h | 43 +++++ .../gralloc/client/allocator_proxy.cpp | 113 +++++++++++++ .../gralloc/client/allocator_proxy.h | 46 ++++++ .../gralloc/client/display_gralloc_client.cpp | 141 ++++++++++++++++ .../gralloc/client/display_gralloc_client.h | 54 ++++++ .../gralloc/client/mapper_adapter.cpp | 80 +++++++++ .../gralloc/client/mapper_adapter.h | 46 ++++++ .../gralloc/include/gralloc_hdi_base.h | 36 ++++ .../gralloc/include/idisplay_allocator.h | 38 +++++ .../gralloc/include/idisplay_gralloc.h | 156 ++++++++++++++++++ .../gralloc/include/parcel_utils.h | 76 +++++++++ 13 files changed, 936 insertions(+) create mode 100755 display_server/drivers/hdi_service/gralloc/client/BUILD.gn create mode 100644 display_server/drivers/hdi_service/gralloc/client/allocator_adapter.cpp create mode 100644 display_server/drivers/hdi_service/gralloc/client/allocator_adapter.h create mode 100644 display_server/drivers/hdi_service/gralloc/client/allocator_proxy.cpp create mode 100644 display_server/drivers/hdi_service/gralloc/client/allocator_proxy.h create mode 100644 display_server/drivers/hdi_service/gralloc/client/display_gralloc_client.cpp create mode 100644 display_server/drivers/hdi_service/gralloc/client/display_gralloc_client.h create mode 100644 display_server/drivers/hdi_service/gralloc/client/mapper_adapter.cpp create mode 100644 display_server/drivers/hdi_service/gralloc/client/mapper_adapter.h create mode 100644 display_server/drivers/hdi_service/gralloc/include/gralloc_hdi_base.h create mode 100644 display_server/drivers/hdi_service/gralloc/include/idisplay_allocator.h create mode 100644 display_server/drivers/hdi_service/gralloc/include/idisplay_gralloc.h create mode 100644 display_server/drivers/hdi_service/gralloc/include/parcel_utils.h diff --git a/display_server/drivers/hdi_service/gralloc/client/BUILD.gn b/display_server/drivers/hdi_service/gralloc/client/BUILD.gn new file mode 100755 index 0000000..cb5a033 --- /dev/null +++ b/display_server/drivers/hdi_service/gralloc/client/BUILD.gn @@ -0,0 +1,54 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/gn/fangtian.gni") + +config("gralloc_client_public_config") { + include_dirs = [ + "//display_server/drivers/interfaces", + "//display_server/drivers/hdi_service/gralloc/include", + ] +} + +declare_args() { + drivers_peripheral_display_hdi_gralloc_client = + ":libhdi_display_gralloc_client" +} + +group("hdi_gralloc_client") { + deps = [ drivers_peripheral_display_hdi_gralloc_client ] + public_configs = [ ":gralloc_client_public_config" ] +} + +ft_shared_library("libhdi_display_gralloc_client") { + configs = [ + ":gralloc_client_public_config", + "//build/gn/configs/system_libs:hilog_config", + "//build/gn/configs/system_libs:ipc_core_config", + "//build/gn/configs/system_libs:c_utils_config", + ] + + sources = [ + "allocator_adapter.cpp", + "display_gralloc_client.cpp", + "mapper_adapter.cpp", + ] + + public_deps = [ + "//display_server/drivers/hal/drm_backend/display_gralloc:display_gralloc", + ] + + deps = [ + "//display_server/utils/buffer_handle/ft_build:buffer_handle" + ] +} diff --git a/display_server/drivers/hdi_service/gralloc/client/allocator_adapter.cpp b/display_server/drivers/hdi_service/gralloc/client/allocator_adapter.cpp new file mode 100644 index 0000000..5c6e6e3 --- /dev/null +++ b/display_server/drivers/hdi_service/gralloc/client/allocator_adapter.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "allocator_adapter.h" + +namespace OHOS { +namespace HDI { +namespace Display { +namespace V1_0 { +AllocatorAdapter::AllocatorAdapter() +{ + if (GrallocInitialize(&grallocFuncs_) != HDF_SUCCESS) { + LOG_ERROR("%{public}s: grallocFuncs_ init failed", __func__); + } +} + +AllocatorAdapter::~AllocatorAdapter() +{ + if (GrallocUninitialize(grallocFuncs_) != HDF_SUCCESS) { + LOG_ERROR("%{public}s: grallocFuncs_ uninit failed", __func__); + } +} + +int32_t AllocatorAdapter::AllocMem(const AllocInfo &info, BufferHandle *&handle) +{ + if (grallocFuncs_ == nullptr) { + LOG_ERROR("%{public}s: grallocFuncs_ is nullptr", __func__); + return HDF_FAILURE; + } + return grallocFuncs_->AllocMem(&info, &handle); +} + +sptr AllocatorAdapter::AsObject() +{ + return nullptr; +} + +} // namespace V1_0 +} // namespace Display +} // namespace HDI +} // namespace OHOS \ No newline at end of file diff --git a/display_server/drivers/hdi_service/gralloc/client/allocator_adapter.h b/display_server/drivers/hdi_service/gralloc/client/allocator_adapter.h new file mode 100644 index 0000000..5801c21 --- /dev/null +++ b/display_server/drivers/hdi_service/gralloc/client/allocator_adapter.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HDI_ALLOCATOR_ADAPTER_V1_0_H +#define HDI_ALLOCATOR_ADAPTER_V1_0_H + +#include "buffer_handle.h" +#include "display_gralloc.h" +#include "display_type.h" +#include "idisplay_allocator.h" + +namespace OHOS { +namespace HDI { +namespace Display { +namespace V1_0 { +class AllocatorAdapter : public IDisplayAllocator { +public: + AllocatorAdapter(); + virtual ~AllocatorAdapter(); + virtual int32_t AllocMem(const AllocInfo &info, BufferHandle *&handle) override; + virtual sptr AsObject() override; + +private: + GrallocFuncs *grallocFuncs_ = nullptr; +}; +} // namespace V1_0 +} // namespace Display +} // namespace HDI +} // namespace OHOS + +#endif // HDI_MAPPER_ADAPTER_V1_0_H \ No newline at end of file diff --git a/display_server/drivers/hdi_service/gralloc/client/allocator_proxy.cpp b/display_server/drivers/hdi_service/gralloc/client/allocator_proxy.cpp new file mode 100644 index 0000000..f88e0c5 --- /dev/null +++ b/display_server/drivers/hdi_service/gralloc/client/allocator_proxy.cpp @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "allocator_proxy.h" +#include +#include "buffer_handle_parcel.h" +#include "iremote_object.h" +#include "iservmgr_hdi.h" +#include "parcel_utils.h" +#include "unistd.h" +#include "refbase.h" + +#define HDF_LOG_TAG HDI_DISP_PROXY + +namespace OHOS { +namespace HDI { +namespace Display { +namespace V1_0 { +sptr IDisplayAllocator::Get(const char *serviceName) +{ + constexpr uint32_t sleepTime = 10000; + constexpr uint32_t waitSvcTimeout = 1000; + constexpr uint32_t waitSvcMgrTimeout = 10; + constexpr uint32_t printPeriod = 100; + using namespace OHOS::HDI::ServiceManager::V1_0; + + static sptr servMgr = IServiceManager::Get(); + uint32_t cnt = 0; + while (servMgr == nullptr) { + if (cnt > waitSvcMgrTimeout) { + LOG_ERROR("%{public}s: wait IServiceManager timeout cnt:%{public}u", __func__, cnt); + return nullptr; + } + usleep(sleepTime); // 10 ms + servMgr = IServiceManager::Get(); + cnt++; + LOG_INFO("%{public}s: IServiceManager cnt:%{public}u", __func__, cnt); + } + LOG_INFO("%{public}s: get IServiceManager success cnt:%{public}u", __func__, cnt); + + cnt = 0; + sptr remote = servMgr->GetService(serviceName); + while (remote == nullptr) { + if (cnt > waitSvcTimeout) { + LOG_ERROR("%{public}s: wait service:%{public}s timeout cnt:%{public}u", __func__, serviceName, cnt); + return nullptr; + } + usleep(sleepTime); // 10 ms + remote = servMgr->GetService(serviceName); + if (((cnt++) % printPeriod) == 0) { + LOG_INFO("%{public}s: get service:%{public}s cnt:%{public}u", __func__, serviceName, cnt); + } + } + LOG_INFO("%{public}s: get service:%{public}s success cnt:%{public}u", __func__, serviceName, cnt); + + sptr hostSptr = iface_cast(remote); + if (hostSptr == nullptr) { + LOG_ERROR("%{public}s: IServiceManager GetService null ptr", __func__); + return nullptr; + } + LOG_ERROR("%{public}s: GetService %{public}s ok", __func__, serviceName); + return hostSptr; +} + +int32_t AllocatorProxy::AllocMem(const AllocInfo &info, BufferHandle *&handle) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(AllocatorProxy::GetDescriptor())) { + return HDF_FAILURE; + } + auto ret = ParcelUtils::PackAllocInfo(data, &info); + if (ret != DISPLAY_SUCCESS) { + return ret; + } + int32_t retCode = Remote()->SendRequest(CMD_REMOTE_ALLOCATOR_ALLOCMEM, data, reply, option); + if (retCode != HDF_SUCCESS) { + LOG_ERROR("%{public}s: SendRequest failed, error code is %{public}x", __func__, retCode); + return retCode; + } + + retCode = reply.ReadInt32(); + if (retCode != HDF_SUCCESS) { + LOG_ERROR("%{public}s: Read return code failed, error code is %{public}x", __func__, retCode); + return retCode; + } + + auto retHandle = ReadBufferHandle(reply); + if (retHandle != nullptr) { + handle = retHandle; + retCode = DISPLAY_SUCCESS; + } else { + retCode = DISPLAY_NULL_PTR; + } + return retCode; +} +} // namespace V1_0 +} // namespace Display +} // namespace HDI +} // namespace OHOS diff --git a/display_server/drivers/hdi_service/gralloc/client/allocator_proxy.h b/display_server/drivers/hdi_service/gralloc/client/allocator_proxy.h new file mode 100644 index 0000000..2749453 --- /dev/null +++ b/display_server/drivers/hdi_service/gralloc/client/allocator_proxy.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HDI_ALLOCATOR_PROXY_V1_0_H +#define HDI_ALLOCATOR_PROXY_V1_0_H + +#include "idisplay_allocator.h" +#include "iremote_proxy.h" + +namespace OHOS { +namespace HDI { +namespace Display { +namespace V1_0 { +class AllocatorProxy : public IRemoteProxy { +public: + explicit AllocatorProxy(const sptr& impl) : IRemoteProxy(impl) {} + virtual ~AllocatorProxy() = default; + virtual int32_t AllocMem(const AllocInfo &info, BufferHandle *&handle) override; + +private: + static inline BrokerDelegator delegator_; + static constexpr int CMD_REMOTE_ALLOCATOR_ALLOCMEM = 0; + // static constexpr int CMD_REMOTE_ALLOCATOR_FREE = 1; + // static constexpr int CMD_REMOTE_ALLOCATOR_MMAP = 2; + // static constexpr int CMD_REMOTE_ALLOCATOR_UNMAP = 3; + // static constexpr int CMD_REMOTE_ALLOCATOR_INVALIDDATE = 4; +}; +} // namespace V1_0 +} // namespace Display +} // namespace HDI +} // namespace OHOS + +#endif // HDI_ALLOCATOR_PROXY_V1_0_H + diff --git a/display_server/drivers/hdi_service/gralloc/client/display_gralloc_client.cpp b/display_server/drivers/hdi_service/gralloc/client/display_gralloc_client.cpp new file mode 100644 index 0000000..83d0d5a --- /dev/null +++ b/display_server/drivers/hdi_service/gralloc/client/display_gralloc_client.cpp @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "display_gralloc_client.h" + +#define HDF_LOG_TAG HDI_DISP_CLIENT + +namespace OHOS { +namespace HDI { +namespace Display { +namespace V1_0 { +class GrallocDeathRecipient : public IRemoteObject::DeathRecipient { +public: + GrallocDeathRecipient(AllocatorDeathCallback func, void *data) : deathCbFun_(func), data_(data) {}; + void OnRemoteDied(const wptr &object) override + { + LOG_INFO("%{public}s: allocator service is dead", __func__); + if (deathCbFun_ != nullptr) { + LOG_INFO("%{public}s: notify the death event of allocator to RS", __func__); + deathCbFun_(data_); + } + } +private: + AllocatorDeathCallback deathCbFun_; + void *data_; +}; + +IDisplayGralloc *IDisplayGralloc::Get() +{ + IDisplayGralloc *instance = nullptr; + instance = new DisplayGrallocClient(); + if (instance == nullptr) { + LOG_ERROR("%{public}s: Can't new a DisplayGrallocClient instance", __func__); + return nullptr; + } + LOG_INFO("%{public}s: Get display gralloc client handle succ", __func__); + return instance; +} + +DisplayGrallocClient::DisplayGrallocClient() : mapperAdapter_(std::make_shared()) +{ + allocatorProxy_ = new AllocatorAdapter(); + if (allocatorProxy_ == nullptr) { + return; + } +} + +int32_t DisplayGrallocClient::RegAllocatorDeathCallback(AllocatorDeathCallback func, void *data) +{ + return DISPLAY_SUCCESS; +} + +int32_t DisplayGrallocClient::AllocMem(const AllocInfo &info, BufferHandle *&handle) const +{ + if (allocatorProxy_ == nullptr) { + LOG_ERROR("%{public}s: allocatorProxy_ is null", __func__); + return DISPLAY_FAILURE; + } + auto ret = allocatorProxy_->AllocMem(info, handle); + return ret; +} + +void DisplayGrallocClient::FreeMem(const BufferHandle &handle) const +{ + mapperAdapter_->FreeBuffer(handle); +} + +void* DisplayGrallocClient::Mmap(const BufferHandle &handle) const +{ + void* data = nullptr; + int32_t ret = mapperAdapter_->MapBuffer(handle, data); + if (ret != DISPLAY_SUCCESS) { + FreeMem(handle); + LOG_ERROR("%{public}s: DisplayGrallocClient::Mmap, mapBuffer failed", __func__); + return nullptr; + } + return data; +} + +int32_t DisplayGrallocClient::Unmap(const BufferHandle &handle) const +{ + auto ret = mapperAdapter_->UnmapBuffer(handle); + if (ret != DISPLAY_SUCCESS) { + LOG_ERROR("%{public}s: failed, ret %{public}d", __func__, ret); + } + return ret; +} + +int32_t DisplayGrallocClient::FlushCache(const BufferHandle &handle) const +{ + auto ret = mapperAdapter_->FlushCache(handle); + if (ret != DISPLAY_SUCCESS) { + LOG_ERROR("%{public}s: failed, ret %{public}d", __func__, ret); + } + return ret; +} + +int32_t DisplayGrallocClient::InvalidateCache(const BufferHandle &handle) const +{ + auto ret = mapperAdapter_->InvalidateCache(handle); + if (ret != DISPLAY_SUCCESS) { + LOG_INFO("%{public}s: failed, ret %{public}d", __func__, ret); + } + return ret; +} + +void* DisplayGrallocClient::MmapCache(const BufferHandle &handle) const +{ + (void)handle; + return nullptr; +} + +int32_t DisplayGrallocClient::FlushMCache(const BufferHandle &handle) const +{ + (void)handle; + return DISPLAY_NOT_SUPPORT; +} + +int32_t DisplayGrallocClient::IsSupportedAlloc(const std::vector &infos, + std::vector &supporteds) const +{ + (void)infos; + (void)supporteds; + return DISPLAY_NOT_SUPPORT; +} +} // namespace V1_0 +} // namespace Display +} // namespace HDI +} // namespace OHOS diff --git a/display_server/drivers/hdi_service/gralloc/client/display_gralloc_client.h b/display_server/drivers/hdi_service/gralloc/client/display_gralloc_client.h new file mode 100644 index 0000000..2f06df1 --- /dev/null +++ b/display_server/drivers/hdi_service/gralloc/client/display_gralloc_client.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HDI_DISPLAY_GRALLOC_CLIENT_V1_0_H +#define HDI_DISPLAY_GRALLOC_CLIENT_V1_0_H + +#include "buffer_handle.h" +#include "display_type.h" +#include "idisplay_gralloc.h" +#include "allocator_adapter.h" +#include "mapper_adapter.h" + +namespace OHOS { +namespace HDI { +namespace Display { +namespace V1_0 { +class DisplayGrallocClient : public IDisplayGralloc { +public: + DisplayGrallocClient(); + virtual ~DisplayGrallocClient() {} + int32_t AllocMem(const AllocInfo& info, BufferHandle*& handle) const override; + void FreeMem(const BufferHandle& handle) const override; + void *Mmap(const BufferHandle& handle) const override; + void *MmapCache(const BufferHandle &handle) const override; + int32_t Unmap(const BufferHandle& handle) const override; + int32_t FlushCache(const BufferHandle &handle) const override; + int32_t FlushMCache(const BufferHandle &handle) const override; + int32_t InvalidateCache(const BufferHandle& handle) const override; + int32_t RegAllocatorDeathCallback(AllocatorDeathCallback func, void *data) override; + int32_t IsSupportedAlloc(const std::vector &infos, + std::vector &supporteds) const override; + +private: + std::shared_ptr mapperAdapter_; + sptr allocatorProxy_; +}; +} // namespace V1_0 +} // namespace Display +} // namespace HDI +} // namespace OHOS + +#endif // HDI_DISPLAY_GRALLOC_CLIENT_V1_0_H diff --git a/display_server/drivers/hdi_service/gralloc/client/mapper_adapter.cpp b/display_server/drivers/hdi_service/gralloc/client/mapper_adapter.cpp new file mode 100644 index 0000000..960bc51 --- /dev/null +++ b/display_server/drivers/hdi_service/gralloc/client/mapper_adapter.cpp @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mapper_adapter.h" +#include "gralloc_hdi_base.h" + +#define HDF_LOG_TAG HDI_DISP_MAPPER + +namespace OHOS { +namespace HDI { +namespace Display { +namespace V1_0 { +MapperAdapter::MapperAdapter() +{ + if (GrallocInitialize(&mapperFuncs_) != HDF_SUCCESS) { + LOG_ERROR("%{public}s: mapperFuncs_ init failed", __func__); + } +} + +MapperAdapter::~MapperAdapter() +{ + if (GrallocUninitialize(mapperFuncs_) != HDF_SUCCESS) { + LOG_ERROR("%{public}s: mapperFuncs_ uninit failed", __func__); + } +} + +bool MapperAdapter::IsReady() const +{ + LOG_INFO("%{public}s: entry", __func__); + return mapperFuncs_ != nullptr; +} + +int32_t MapperAdapter::MapBuffer(const BufferHandle& handle, void*& outData) const +{ + int32_t ret = 0; + outData = mapperFuncs_->Mmap(const_cast(&handle)); + return ret; +} + +int32_t MapperAdapter::UnmapBuffer(const BufferHandle& handle) const +{ + int32_t ret = 0; + ret = mapperFuncs_->Unmap(const_cast(&handle)); + return ret; +} + +int32_t MapperAdapter::InvalidateCache(const BufferHandle& handle) const +{ + int32_t ret = 0; + ret = mapperFuncs_->InvalidateCache(const_cast(&handle)); + return ret; +} + +int32_t MapperAdapter::FlushCache(const BufferHandle& handle) const +{ + int32_t ret = 0; + ret = mapperFuncs_->FlushCache(const_cast(&handle)); + return ret; +} + +void MapperAdapter::FreeBuffer(const BufferHandle& handle) const +{ + mapperFuncs_->FreeMem(const_cast(&handle)); +} +} // namespace V1_0 +} // namespace Display +} // namespace HDI +} // namespace OHOS \ No newline at end of file diff --git a/display_server/drivers/hdi_service/gralloc/client/mapper_adapter.h b/display_server/drivers/hdi_service/gralloc/client/mapper_adapter.h new file mode 100644 index 0000000..4910ad6 --- /dev/null +++ b/display_server/drivers/hdi_service/gralloc/client/mapper_adapter.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HDI_MAPPER_ADAPTER_V1_0_H +#define HDI_MAPPER_ADAPTER_V1_0_H + +#include "buffer_handle.h" +#include "display_gralloc.h" +#include "display_type.h" + +namespace OHOS { +namespace HDI { +namespace Display { +namespace V1_0 { +class MapperAdapter { +public: + MapperAdapter(); + virtual ~MapperAdapter(); + bool IsReady() const; + int32_t MapBuffer(const BufferHandle& handle, void *&outData) const; + int32_t UnmapBuffer(const BufferHandle& handle) const; + int32_t InvalidateCache(const BufferHandle& handle) const; + int32_t FlushCache(const BufferHandle& handle) const; + void FreeBuffer(const BufferHandle& handle) const; + +private: + GrallocFuncs *mapperFuncs_ = nullptr; +}; +} // namespace V1_0 +} // namespace Display +} // namespace HDI +} // namespace OHOS + +#endif // HDI_MAPPER_ADAPTER_V1_0_H diff --git a/display_server/drivers/hdi_service/gralloc/include/gralloc_hdi_base.h b/display_server/drivers/hdi_service/gralloc/include/gralloc_hdi_base.h new file mode 100644 index 0000000..e02af50 --- /dev/null +++ b/display_server/drivers/hdi_service/gralloc/include/gralloc_hdi_base.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Technologies Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include +#include + +#include "hilog/log.h" + +using namespace OHOS::HiviewDFX; + +constexpr HiLogLabel LABEL = { LOG_CORE, 0xD001500, "GrallocHdi" }; + +#define LOG_FATAL(format, ...) HiLog::Fatal(LABEL, format, ##__VA_ARGS__) +#define LOG_ERROR(format, ...) HiLog::Error(LABEL, format, ##__VA_ARGS__) +#define LOG_WARN(format, ...) HiLog::Warn(LABEL, format, ##__VA_ARGS__) +#define LOG_INFO(format, ...) HiLog::Info(LABEL, format, ##__VA_ARGS__) +#define LOG_DEBUG(format, ...) HiLog::Debug(LABEL, format, ##__VA_ARGS__) +#define LOG_TRACE(format, ...) HiLog::Trace(LABEL, format, ##__VA_ARGS__) + +#define HDF_SUCCESS 0 +#define HDF_FAILURE -1 \ No newline at end of file diff --git a/display_server/drivers/hdi_service/gralloc/include/idisplay_allocator.h b/display_server/drivers/hdi_service/gralloc/include/idisplay_allocator.h new file mode 100644 index 0000000..96b125d --- /dev/null +++ b/display_server/drivers/hdi_service/gralloc/include/idisplay_allocator.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HDI_IDISPLAY_ALLOCATOR_V1_0_H +#define HDI_IDISPLAY_ALLOCATOR_V1_0_H + +#include "display_type.h" +#include "gralloc_hdi_base.h" + +namespace OHOS { +namespace HDI { +namespace Display { +namespace V1_0 { +class IDisplayAllocator : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.hdi.display.v1_0.IAllocator"); + virtual ~IDisplayAllocator() {} + virtual int32_t AllocMem(const AllocInfo &info, BufferHandle *&handle) = 0; + static sptr Get(const char *serviceName); +}; +} // namespace V1_0 +} // namespace Display +} // namespace HDI +} // namespace OHOS + +#endif // HDI_IDISPLAY_ALLOCATOR_V1_0_H diff --git a/display_server/drivers/hdi_service/gralloc/include/idisplay_gralloc.h b/display_server/drivers/hdi_service/gralloc/include/idisplay_gralloc.h new file mode 100644 index 0000000..7eb3965 --- /dev/null +++ b/display_server/drivers/hdi_service/gralloc/include/idisplay_gralloc.h @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HDI_IDISPLAY_GRALLOC_V1_0_H +#define HDI_IDISPLAY_GRALLOC_V1_0_H + +#include +#include "display_type.h" +#include "buffer_handle.h" + +namespace OHOS { +namespace HDI { +namespace Display { +namespace V1_0 { +using AllocatorDeathCallback = void (*)(void *); +class IDisplayGralloc { +public: + virtual ~IDisplayGralloc() = default; + + virtual int32_t RegAllocatorDeathCallback(AllocatorDeathCallback func, void* data) = 0; + + /** + * @brief Obtains all interfaces of IDisplayGralloc. + * + * @return Returns IDisplayGralloc* if the operation is successful; returns an null point otherwise. + * @since 1.0 + * @version 1.0 + */ + static IDisplayGralloc* Get(); + + /** + * @brief Allocates memory based on the parameters passed by the GUI. + * + * @param info Indicates the description of the memory to allocate. + * + * @param handle Indicates the pointer to the buffer of the memory to allocate. + * + * @return Returns 0 if the operation is successful; returns an error code defined in {@link DispErrCode} + * otherwise. + * @since 1.0 + * @version 1.0 + */ + virtual int32_t AllocMem(const AllocInfo &info, BufferHandle *&handle) const = 0; + + /** + * @brief Releases memory. + * + * @param handle Indicates the reference to the buffer of the memory to release. + * + * @since 1.0 + * @version 1.0 + */ + virtual void FreeMem(const BufferHandle &handle) const = 0; + + /** + * @brief Maps memory to memory without cache in the process's address space. + * + * @param handle Indicates the reference to the buffer of the memory to map. + * + * @return Returns the pointer to a valid address if the operation is successful; returns NULL otherwise. + * @since 1.0 + * @version 1.0 + */ + virtual void *Mmap(const BufferHandle &handle) const = 0; + + /** + * @brief Maps memory to memory with cache in the process's address space. + * + * @param handle Indicates the reference to the buffer of the memory to map. + * + * @return Returns the pointer to a valid address if the operation is successful; returns NULL otherwise. + * @since 1.0 + * @version 1.0 + */ + virtual void *MmapCache(const BufferHandle &buffer) const = 0; + + /** + * @brief Unmaps memory, that is, removes mappings from the process's address space. + * + * @param handle Indicates the reference to the buffer of the memory to unmap. + * + * @return Returns 0 if the operation is successful; returns an error code defined in {@link DispErrCode} + * otherwise. + * @since 1.0 + * @version 1.0 + */ + virtual int32_t Unmap(const BufferHandle &handle) const = 0; + + /** + * @brief Flushes data from the cache to memory and invalidates the data in the cache. + * + * @param handle Indicates the reference to the buffer of the cache to flush. + * + * @return Returns 0 if the operation is successful; returns an error code defined in {@link DispErrCode} + * otherwise. + * @since 1.0 + * @version 1.0 + */ + virtual int32_t FlushCache(const BufferHandle &handle) const = 0; + + /** + * @brief Flushes data from the cache mapped via {@link Mmap} to memory and invalidates the data in the cache. + * + * @param handle Indicates the reference to the buffer of the cache to flush. + * + * @return Returns 0 if the operation is successful; returns an error code defined in {@link DispErrCode} + * otherwise. + * @since 1.0 + * @version 1.0 + */ + virtual int32_t FlushMCache(const BufferHandle &buffer) const = 0; + + /** + * @brief Invalidates the cache to update it from memory. + * + * @param handle Indicates the reference to the buffer of the cache, which will be invalidated. + * + * @return Returns 0 if the operation is successful; returns an error code defined in {@link DispErrCode} + * otherwise. + * @since 1.0 + * @version 1.0 + */ + virtual int32_t InvalidateCache(const BufferHandle &handle) const = 0; + + /** + * @brief Checks whether the given VerifyAllocInfo array is allocatable. + * + * @param infos Indicates the VerifyAllocInfo array. + * @param supporteds Indicates whether the array is allocatable. + * + * @return Returns 0 if the operation is successful; returns an error code defined in {@link DispErrCode} + * otherwise. + * @since 1.0 + * @version 1.0 + */ + virtual int32_t IsSupportedAlloc(const std::vector &infos, + std::vector &supporteds) const = 0; +}; +} // namespace V1_0 +} // namespace Display +} // namespace HDI +} // namespace OHOS + +#endif // HDI_IDISPLAY_GRALLOC_V1_0_H diff --git a/display_server/drivers/hdi_service/gralloc/include/parcel_utils.h b/display_server/drivers/hdi_service/gralloc/include/parcel_utils.h new file mode 100644 index 0000000..a7c7ab7 --- /dev/null +++ b/display_server/drivers/hdi_service/gralloc/include/parcel_utils.h @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HDI_PARCEL_UTILS_V1_0_H +#define HDI_PARCEL_UTILS_V1_0_H + +#include +#include "hdf_sbuf_ipc.h" +#include "display_type.h" +#include "hdf_log.h" + +namespace OHOS { +namespace HDI { +namespace Display { +namespace V1_0 { +class ParcelUtils { +public: + static int32_t UnpackAllocInfo(MessageParcel &data, AllocInfo *pAllocInfo) + { + if (pAllocInfo == nullptr) { + return DISPLAY_NULL_PTR; + } + pAllocInfo->width = data.ReadUint32(); + pAllocInfo->height = data.ReadUint32(); + pAllocInfo->usage = data.ReadUint64(); + pAllocInfo->format = static_cast(data.ReadUint32()); + pAllocInfo->expectedSize = data.ReadUint32(); + return DISPLAY_SUCCESS; + } + + static int32_t PackAllocInfo(MessageParcel &data, const AllocInfo *pAllocInfo) + { + if (pAllocInfo == nullptr) { + return DISPLAY_NULL_PTR; + } + if (!data.WriteUint32(pAllocInfo->width)) { + HDF_LOGE("%{public}s: write AllocInfo width failed", __func__); + return DISPLAY_PARAM_ERR; + } + if (!data.WriteUint32(pAllocInfo->height)) { + HDF_LOGE("%{public}s: write AllocInfo height failed", __func__); + return DISPLAY_PARAM_ERR; + } + if (!data.WriteUint64(pAllocInfo->usage)) { + HDF_LOGE("%{public}s: write AllocInfo usage failed", __func__); + return DISPLAY_PARAM_ERR; + } + if (!data.WriteUint32(pAllocInfo->format)) { + HDF_LOGE("%{public}s: write AllocInfo format failed", __func__); + return DISPLAY_PARAM_ERR; + } + if (!data.WriteUint32(pAllocInfo->expectedSize)) { + HDF_LOGE("%{public}s: write AllocInfo type failed", __func__); + return DISPLAY_PARAM_ERR; + } + return DISPLAY_SUCCESS; + } +}; +} // namespace V1_0 +} // namespace Display +} // namespace HDI +} // namespace OHOS + +#endif // HDI_PARCEL_UTILS_V1_0_H -- Gitee