diff --git a/display/composer/cache_manager/BUILD.gn b/display/composer/cache_manager/BUILD.gn index 11e68573f1e127f9166ebdf956396840f18ec955..c4efa847a695978eedec793f37ec0e7342cafc48 100644 --- a/display/composer/cache_manager/BUILD.gn +++ b/display/composer/cache_manager/BUILD.gn @@ -25,6 +25,8 @@ ohos_shared_library("libcomposer_buffer_cache") { "../", ] + deps = [ "../../buffer/v1_0:libdisplay_buffer_stub_1.0" ] + external_deps = [ "c_utils:utils", "hdf_core:libhdi", diff --git a/display/composer/cache_manager/cache_manager.h b/display/composer/cache_manager/cache_manager.h index 1532299f90002ff9b33bbed6cc6d937f1aef1f90..c90c2cce52fe6ae84d2689d9640f956f67dcac2f 100644 --- a/display/composer/cache_manager/cache_manager.h +++ b/display/composer/cache_manager/cache_manager.h @@ -36,13 +36,21 @@ namespace Composer { template class CacheManager : public NoCopyable { public: - CacheManager() : cacheCountMax_(0) + CacheManager() + : cacheCountMax_ { 0 }, + cleanUpFunc_ { nullptr }, + initFunc_ { nullptr } { } virtual ~CacheManager() { std::lock_guard lock(mutex_); + if (cleanUpFunc_) { + for (auto& cache : caches_) { + cleanUpFunc_(cache.second); + } + } caches_.clear(); } @@ -71,32 +79,41 @@ public: bool InsertCache(IdType id, CacheType* cache) { - if (SearchCache(id) != nullptr) { + std::lock_guard lock(mutex_); + auto cacheItem = caches_.find(id); + if (cacheItem != caches_.end()) { HDF_LOGI("%{public}s: intend to insert a existing cache, SeqNo=%{public}d", __func__, id); + cacheItem->second.reset(cache); } else { if (cacheCountMax_ != 0 && Size() >= cacheCountMax_) { HDF_LOGE("%{public}s: Caches is full, new seqNo:%{public}d can't be inserted", __func__, id); return false; } + caches_[id] = std::move(std::unique_ptr(cache)); + } + + if (initFunc_) { + initFunc_(caches_[id]); } - std::lock_guard lock(mutex_); - caches_[id] = std::move(*(new std::unique_ptr(cache))); return true; } bool EraseCache(IdType id) { - bool ret = false; - if (SearchCache(id) != nullptr) { - std::lock_guard lock(mutex_); - caches_.erase(id); - ret = true; - } else { + std::lock_guard lock(mutex_); + auto cacheItem = caches_.find(id); + if (cacheItem == caches_.end() || cacheItem->second == nullptr) { HDF_LOGE("%{public}s: Cache %{public}d is not existing\n", __func__, id); + return false; } - return ret; + if (cleanUpFunc_) { + cleanUpFunc_(cacheItem->second); + } + + caches_.erase(cacheItem); + return true; } CacheType* SearchCache(IdType id) @@ -118,9 +135,21 @@ public: } } + void SetCleanUpFunc(void (*func)(std::unique_ptr&)) + { + cleanUpFunc_ = func; + } + + void SetInitFunc(void (*func)(std::unique_ptr&)) + { + initFunc_ = func; + } + private: uint32_t cacheCountMax_; std::unordered_map> caches_; + void (*cleanUpFunc_)(std::unique_ptr&); + void (*initFunc_)(std::unique_ptr&); std::mutex mutex_; }; } // namespace Composer diff --git a/display/composer/cache_manager/device_cache.cpp b/display/composer/cache_manager/device_cache.cpp index e5402760571645d1d43f8969f4b739b75b1ef3a0..79c35aca291f31e7eaecc123a397e222a302b6dd 100644 --- a/display/composer/cache_manager/device_cache.cpp +++ b/display/composer/cache_manager/device_cache.cpp @@ -58,10 +58,16 @@ int32_t DeviceCache::Init() DISPLAY_CHK_RETURN(clientBufferCaches_ == nullptr, HDF_FAILURE, HDF_LOGE("%{public}s: create client buffer caches failed", __func__)); + clientBufferCaches_->SetInitFunc(LayerCache::NativeBufferInit); + clientBufferCaches_->SetCleanUpFunc(LayerCache::NativeBufferCleanUp); + outputBufferCaches_.reset(new CacheManager()); DISPLAY_CHK_RETURN(outputBufferCaches_ == nullptr, HDF_FAILURE, HDF_LOGE("%{public}s: create output buffer caches failed", __func__)); + outputBufferCaches_->SetInitFunc(LayerCache::NativeBufferInit); + outputBufferCaches_->SetCleanUpFunc(LayerCache::NativeBufferCleanUp); + return HDF_SUCCESS; } diff --git a/display/composer/cache_manager/layer_cache.cpp b/display/composer/cache_manager/layer_cache.cpp index 834557cab4554726553de9ba8eda6e2ef7644dfb..56d2e1935a75faa33452c200f9df18bc0b6cfd96 100644 --- a/display/composer/cache_manager/layer_cache.cpp +++ b/display/composer/cache_manager/layer_cache.cpp @@ -54,6 +54,9 @@ int32_t LayerCache::Init() DISPLAY_CHK_RETURN(bufferCaches_ == nullptr, HDF_FAILURE, HDF_LOGE("%{public}s: create buffer caches failed", __func__)); + bufferCaches_->SetInitFunc(NativeBufferInit); + bufferCaches_->SetCleanUpFunc(NativeBufferCleanUp); + return HDF_SUCCESS; } @@ -90,6 +93,77 @@ void LayerCache::Dump() const HDF_LOGE("layerId-%{public}d, buffer[%{public}d]: %{public}s", layerId_, id, info.c_str()); }); } + +void LayerCache::NativeBufferInit(std::unique_ptr& buffer) +{ + int32_t ret = Mmap(buffer); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s: Mmap failed with %{public}d!", __func__, ret); + } +} + +void LayerCache::NativeBufferCleanUp(std::unique_ptr& buffer) +{ + int32_t ret = Unmap(buffer); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s: Unmap failed with %{public}d!", __func__, ret); + } +} + +sptr LayerCache::GetMapperService() +{ + static sptr mapperService; + if (mapperService == nullptr) { + mapperService = Buffer::V1_0::IMapper::Get(true); + } + return mapperService; +} + +int32_t LayerCache::Mmap(std::unique_ptr& buffer) +{ + if (buffer == nullptr || buffer.get() == nullptr) { + return HDF_SUCCESS; + } + + auto mapperService = GetMapperService(); + if (mapperService == nullptr) { + HDF_LOGE("GetMapperService failed!"); + return HDF_FAILURE; + } + + sptr nativeBuffer(new NativeBuffer); + if (nativeBuffer == nullptr) { + HDF_LOGE("new NativeBuffer failed!"); + return HDF_FAILURE; + } + + nativeBuffer->SetBufferHandle(buffer.get()->GetBufferHandle(), false); + + return mapperService->Mmap(nativeBuffer); +} + +int32_t LayerCache::Unmap(std::unique_ptr& buffer) +{ + if (buffer == nullptr || buffer.get() == nullptr) { + return HDF_SUCCESS; + } + + auto mapperService = GetMapperService(); + if (mapperService == nullptr) { + HDF_LOGE("GetMapperService failed!"); + return HDF_FAILURE; + } + + sptr nativeBuffer(new NativeBuffer); + if (nativeBuffer == nullptr) { + HDF_LOGE("new NativeBuffer failed!"); + return HDF_FAILURE; + } + + nativeBuffer->SetBufferHandle(buffer.get()->GetBufferHandle(), false); + + return mapperService->Unmap(nativeBuffer); +} } // namespace Composer } // namespace Display } // namespace HDI diff --git a/display/composer/cache_manager/layer_cache.h b/display/composer/cache_manager/layer_cache.h index 06169fa8d753bb9ebce8342fe830d44fc4682d2e..2dd18843d5528c9f37eb935ab40c8828f875b7e1 100644 --- a/display/composer/cache_manager/layer_cache.h +++ b/display/composer/cache_manager/layer_cache.h @@ -22,6 +22,7 @@ #include "base/native_buffer.h" #include "cache_manager.h" #include "nocopyable.h" +#include "v1_0/mapper_stub.h" namespace OHOS { namespace HDI { @@ -36,9 +37,14 @@ public: const std::vector& deletingList, std::function realFunc); int32_t SetBufferCacheMaxCount(uint32_t cacheCount); void Dump() const; + static void NativeBufferInit(std::unique_ptr& buffer); + static void NativeBufferCleanUp(std::unique_ptr& buffer); private: explicit LayerCache(uint32_t id); int32_t Init(); + static sptr GetMapperService(); + static int32_t Mmap(std::unique_ptr& buffer); + static int32_t Unmap(std::unique_ptr& buffer); uint32_t layerId_; std::shared_ptr> bufferCaches_; diff --git a/display/composer/v1_0/display_command/display_cmd_responser.h b/display/composer/v1_0/display_command/display_cmd_responser.h index 10f83b4e2b8b0ca988d9ae141faa257ac8e5870c..70f8a91e3a62a9a2fd8dfb33de2e8e0de0a24b32 100755 --- a/display/composer/v1_0/display_command/display_cmd_responser.h +++ b/display/composer/v1_0/display_command/display_cmd_responser.h @@ -386,6 +386,12 @@ EXIT: HDF_LOGE("%{public}s, FileDescriptorUnpack error", __func__); return HDF_FAILURE; } + + if (RegisterBuffer(data.buffer) != HDF_SUCCESS) { + HDF_LOGE("%{public}s, RegisterBuffer error", __func__); + return HDF_FAILURE; + } + if (data.buffer != nullptr) { int32_t ret = RegisterBuffer(data.buffer); DISPLAY_CHK_RETURN(ret != HDF_SUCCESS && ret != DISPLAY_NOT_SUPPORT, HDF_FAILURE,