diff --git a/display/buffer/v1_0/hdi_impl/display_buffer_hdi_impl.h b/display/buffer/v1_0/hdi_impl/display_buffer_hdi_impl.h index a7db5c20e921175ef6862009fe8d28e5409d42c2..2ea3ed36cd914322db4ad0eae32314c5006e00d3 100644 --- a/display/buffer/v1_0/hdi_impl/display_buffer_hdi_impl.h +++ b/display/buffer/v1_0/hdi_impl/display_buffer_hdi_impl.h @@ -77,10 +77,6 @@ public: explicit DisplayBufferHdiImpl(bool isAllocLocal = false) : allocator_(nullptr), mapper_(nullptr), recipient_(nullptr) { - while ((allocator_ = IAllocator::Get(isAllocLocal)) == nullptr) { - // Waiting for allocator service ready - usleep(WAIT_TIME_INTERVAL); - } while ((mapper_ = IMapper::Get(true)) == nullptr) { // Waiting for mapper IF ready usleep(WAIT_TIME_INTERVAL); @@ -89,44 +85,51 @@ public: virtual ~DisplayBufferHdiImpl() { if (recipient_ != nullptr) { - sptr remoteObj = OHOS::HDI::hdi_objcast(allocator_); - remoteObj->RemoveDeathRecipient(recipient_); - recipient_ = nullptr; + if (allocator_ != nullptr) { + sptr remoteObj = OHOS::HDI::hdi_objcast(allocator_); + remoteObj->RemoveDeathRecipient(recipient_); + recipient_ = nullptr; + } } } bool AddDeathRecipient(const sptr& recipient) override { - sptr remoteObj = OHOS::HDI::hdi_objcast(allocator_); - if (recipient_ != nullptr) { - HDF_LOGE("%{public}s: the existing recipient is removed, and add the new. %{public}d", - __func__, __LINE__); - remoteObj->RemoveDeathRecipient(recipient_); - } - bool ret = remoteObj->AddDeathRecipient(recipient); - if (ret) { - recipient_ = recipient; - } else { - recipient_ = nullptr; - HDF_LOGE("%{public}s: AddDeathRecipient failed %{public}d", __func__, __LINE__); - } - return ret; + + recipient_ = recipient; + return true; } bool RemoveDeathRecipient() override { if (recipient_ != nullptr) { - sptr remoteObj = OHOS::HDI::hdi_objcast(allocator_); - remoteObj->RemoveDeathRecipient(recipient_); - recipient_ = nullptr; + if (allocator_ != nullptr) { + sptr remoteObj = OHOS::HDI::hdi_objcast(allocator_); + remoteObj->RemoveDeathRecipient(recipient_); + recipient_ = nullptr; + } } return true; } - int32_t AllocMem(const AllocInfo& info, BufferHandle*& handle) const override + int32_t AllocMem(const AllocInfo& info, BufferHandle*& handle) override { + if (allocator_ == nullptr) { + HDF_LOGE("%{public}s: yktest allocator_ is nullptr", __func__); + std::lock_guard lock(mutex_); + while (allocator_ == nullptr) { + HDF_LOGE("%{public}s: yktest allocator_ is nullptr1", __func__); + allocator_ = IAllocator::Get(false); + } + } + DISPLAY_TRACE; CHECK_NULLPOINTER_RETURN_VALUE(allocator_, HDF_FAILURE); + sptr remoteObj = OHOS::HDI::hdi_objcast(allocator_); + HDF_LOGE("%{public}s: yktest allocator_ get complete", __func__); + remoteObj->AddDeathRecipient(recipient_); + HDF_LOGE("%{public}s: yktest AddDeathRecipient complete", __func__); + sptr hdiBuffer; int32_t ret = allocator_->AllocMem(info, hdiBuffer); if ((ret == HDF_SUCCESS) && (hdiBuffer != nullptr)) { @@ -204,6 +207,7 @@ protected: sptr allocator_; sptr mapper_; sptr recipient_; + std::mutex mutex_; }; using HdiDisplayBufferImpl = DisplayBufferHdiImpl; } // namespace V1_0 diff --git a/display/buffer/v1_0/include/idisplay_buffer.h b/display/buffer/v1_0/include/idisplay_buffer.h index b51a5f9bef785f8e6ccd10fdd02f43d8dc1bf436..f6c76db0d2d42d3aba3b12ea33a54d76b1c65070 100644 --- a/display/buffer/v1_0/include/idisplay_buffer.h +++ b/display/buffer/v1_0/include/idisplay_buffer.h @@ -53,7 +53,7 @@ public: * @since 4.0 * @version 1.0 */ - virtual int32_t AllocMem(const AllocInfo& info, BufferHandle*& handle) const = 0; + virtual int32_t AllocMem(const AllocInfo& info, BufferHandle*& handle) = 0; /** * @brief Releases memory.