From b9e81175f174562297a2b9a339bbd8abfa96ecfd Mon Sep 17 00:00:00 2001 From: lu Date: Wed, 19 Jan 2022 19:25:16 +0800 Subject: [PATCH] Hide implement of the interface class Signed-off-by: lu Change-Id: I602afa0a650e35f0079d27df6b7d385aa9b00c53 --- dm/src/display.cpp | 37 +++-- dm/src/display_manager.cpp | 160 +++++++++++++--------- interfaces/innerkits/dm/display.h | 7 +- interfaces/innerkits/dm/display_manager.h | 19 +-- 4 files changed, 128 insertions(+), 95 deletions(-) diff --git a/dm/src/display.cpp b/dm/src/display.cpp index 9f9866ec6d..20ecf8b6d0 100644 --- a/dm/src/display.cpp +++ b/dm/src/display.cpp @@ -17,48 +17,59 @@ #include "display_info.h" namespace OHOS::Rosen { +class Display::Impl : public RefBase { +friend class Display; +private: + std::string name_; + DisplayId id_ { DISPLAY_ID_INVALD }; + int32_t width_ { 0 }; + int32_t height_ { 0 }; + uint32_t freshRate_ { 0 }; +}; + Display::Display(const std::string& name, DisplayInfo* info) - : name_(name), - id_(info->id_), - width_(info->width_), - height_(info->height_), - freshRate_(info->freshRate_) + : pImpl_(new Impl()) { + pImpl_->name_ = name; + pImpl_->id_ = info->id_; + pImpl_->width_ = info->width_; + pImpl_->height_ = info->height_; + pImpl_->freshRate_ = info->freshRate_; } DisplayId Display::GetId() const { - return id_; + return pImpl_->id_; } int32_t Display::GetWidth() const { - return width_; + return pImpl_->width_; } int32_t Display::GetHeight() const { - return height_; + return pImpl_->height_; } uint32_t Display::GetFreshRate() const { - return freshRate_; + return pImpl_->freshRate_; } void Display::SetWidth(int32_t width) { - width_ = width; + pImpl_->width_ = width; } void Display::SetHeight(int32_t height) { - height_ = height; + pImpl_->height_ = height; } void Display::SetFreshRate(uint32_t freshRate) { - freshRate_ = freshRate; + pImpl_->freshRate_ = freshRate; } float Display::GetVirtualPixelRatio() const @@ -73,6 +84,6 @@ float Display::GetVirtualPixelRatio() const void Display::SetId(DisplayId id) { - id_ = id; + pImpl_->id_ = id; } } // namespace OHOS::Rosen \ No newline at end of file diff --git a/dm/src/display_manager.cpp b/dm/src/display_manager.cpp index 108ef0bbef..345c6ecc82 100644 --- a/dm/src/display_manager.cpp +++ b/dm/src/display_manager.cpp @@ -30,7 +30,65 @@ namespace { } WM_IMPLEMENT_SINGLE_INSTANCE(DisplayManager) -DisplayManager::DisplayManager() +class DisplayManager::Impl : public RefBase{ +friend class DisplayManager; +private: + constexpr static int32_t MAX_RESOLUTION_SIZE_SCREENSHOT = 15360; // max resolution, 16K + std::mutex mutex_; + std::vector> powerEventListeners_; + sptr powerEventListenerAgent_; + sptr displayStateAgent_; + DisplayStateCallback displayStateCallback_; + + bool CheckRectValid(const Media::Rect& rect, int32_t oriHeight, int32_t oriWidth) const; + bool CheckSizeValid(const Media::Size& size, int32_t oriHeight, int32_t oriWidth) const; + void ClearDisplayStateCallback(); +}; + +bool DisplayManager::Impl::CheckRectValid(const Media::Rect& rect, int32_t oriHeight, int32_t oriWidth) const +{ + if (!((rect.left >= 0) and (rect.left < oriWidth) and (rect.top >= 0) and (rect.top < oriHeight))) { + WLOGFE("rect left or top invalid!"); + return false; + } + + if (!((rect.width > 0) and (rect.width <= (oriWidth - rect.left)) and + (rect.height > 0) and (rect.height <= (oriHeight - rect.top)))) { + if (!((rect.width == 0) and (rect.height == 0))) { + WLOGFE("rect height or width invalid!"); + return false; + } + } + return true; +} + +bool DisplayManager::Impl::CheckSizeValid(const Media::Size& size, int32_t oriHeight, int32_t oriWidth) const +{ + if (!((size.width > 0) and (size.height > 0))) { + if (!((size.width == 0) and (size.height == 0))) { + WLOGFE("width or height invalid!"); + return false; + } + } + + if ((size.width > MAX_RESOLUTION_SIZE_SCREENSHOT) or (size.height > MAX_RESOLUTION_SIZE_SCREENSHOT)) { + WLOGFE("width or height too big!"); + return false; + } + return true; +} + +void DisplayManager::Impl::ClearDisplayStateCallback() +{ + displayStateCallback_ = nullptr; + if (displayStateAgent_ != nullptr) { + SingletonContainer::Get().UnregisterDisplayManagerAgent(displayStateAgent_, + DisplayManagerAgentType::DISPLAY_STATE_LISTENER); + displayStateAgent_ = nullptr; + } +} + +DisplayManager::DisplayManager() : pImpl_(new Impl()) { } @@ -69,39 +127,6 @@ std::shared_ptr DisplayManager::GetScreenshot(DisplayId display return screenShot; } -bool DisplayManager::CheckRectValid(const Media::Rect &rect, int32_t oriHeight, int32_t oriWidth) const -{ - if (!((rect.left >= 0) and (rect.left < oriWidth) and (rect.top >= 0) and (rect.top < oriHeight))) { - WLOGFE("CheckRectValid rect left top invalid!"); - return false; - } - - if (!((rect.width > 0) and (rect.width <= (oriWidth - rect.left)) and - (rect.height > 0) and (rect.height <= (oriHeight - rect.top)))) { - if (!((rect.width == 0) and (rect.height == 0))) { - WLOGFE("CheckRectValid rect height width invalid!"); - return false; - } - } - return true; -} - -bool DisplayManager::CheckSizeValid(const Media::Size &size, int32_t oriHeight, int32_t oriWidth) const -{ - if (!((size.width > 0) and (size.height > 0))) { - if (!((size.width == 0) and (size.height == 0))) { - WLOGFE("CheckSizeValid width height invalid!"); - return false; - } - } - - if ((size.width > MAX_RESOLUTION_SIZE_SCREENSHOT) or (size.height > MAX_RESOLUTION_SIZE_SCREENSHOT)) { - WLOGFE("CheckSizeValid width and height too big!"); - return false; - } - return true; -} - std::shared_ptr DisplayManager::GetScreenshot(DisplayId displayId, const Media::Rect &rect, const Media::Size &size, int rotation) { @@ -120,12 +145,12 @@ std::shared_ptr DisplayManager::GetScreenshot(DisplayId display // check parameters int32_t oriHeight = screenShot->GetHeight(); int32_t oriWidth = screenShot->GetWidth(); - if (!CheckRectValid(rect, oriHeight, oriWidth)) { + if (!pImpl_->CheckRectValid(rect, oriHeight, oriWidth)) { WLOGFE("rect invalid! left %{public}d, top %{public}d, w %{public}d, h %{public}d", rect.left, rect.top, rect.width, rect.height); return nullptr; } - if (!CheckSizeValid(size, oriHeight, oriWidth)) { + if (!pImpl_->CheckSizeValid(size, oriHeight, oriWidth)) { WLOGFE("size invalid! w %{public}d, h %{public}d", rect.width, rect.height); return nullptr; } @@ -180,12 +205,13 @@ bool DisplayManager::RegisterDisplayPowerEventListener(sptr lock(mutex_); - powerEventListeners_.push_back(listener); + std::lock_guard lock(pImpl_->mutex_); + pImpl_->powerEventListeners_.push_back(listener); bool ret = true; - if (powerEventListenerAgent_ == nullptr) { - powerEventListenerAgent_ = new DisplayManagerAgent(); - ret = SingletonContainer::Get().RegisterDisplayManagerAgent(powerEventListenerAgent_, + if (pImpl_->powerEventListenerAgent_ == nullptr) { + pImpl_->powerEventListenerAgent_ = new DisplayManagerAgent(); + ret = SingletonContainer::Get().RegisterDisplayManagerAgent( + pImpl_->powerEventListenerAgent_, DisplayManagerAgentType::DISPLAY_POWER_EVENT_LISTENER); if (!ret) { WLOGFW("RegisterDisplayManagerAgent failed ! remove listener!"); @@ -203,18 +229,24 @@ bool DisplayManager::UnregisterDisplayPowerEventListener(sptr lock(mutex_); - auto iter = std::find(powerEventListeners_.begin(), powerEventListeners_.end(), listener); - if (iter == powerEventListeners_.end()) { + std::lock_guard lock(pImpl_->mutex_); + auto iter = std::find(pImpl_->powerEventListeners_.begin(), pImpl_->powerEventListeners_.end(), listener); + if (iter == pImpl_->powerEventListeners_.end()) { WLOGFE("could not find this listener"); return false; } +<<<<<<< Updated upstream powerEventListeners_.erase(iter); bool ret = true; if (powerEventListeners_.empty() && powerEventListenerAgent_ != nullptr) { ret = SingletonContainer::Get().UnregisterDisplayManagerAgent(powerEventListenerAgent_, +======= + pImpl_->powerEventListeners_.erase(iter); + if (pImpl_->powerEventListeners_.empty() && pImpl_->powerEventListenerAgent_ != nullptr) { + SingletonContainer::Get().UnregisterDisplayManagerAgent(pImpl_->powerEventListenerAgent_, +>>>>>>> Stashed changes DisplayManagerAgentType::DISPLAY_POWER_EVENT_LISTENER); - powerEventListenerAgent_ = nullptr; + pImpl_->powerEventListenerAgent_ = nullptr; } WLOGFI("UnregisterDisplayPowerEventListener end"); return ret; @@ -223,9 +255,9 @@ bool DisplayManager::UnregisterDisplayPowerEventListener(sptr lock(mutex_); - for (auto& listener : powerEventListeners_) { + pImpl_->powerEventListeners_.size()); + std::lock_guard lock(pImpl_->mutex_); + for (auto& listener : pImpl_->powerEventListeners_) { listener->OnDisplayPowerEvent(event, status); } } @@ -233,25 +265,15 @@ void DisplayManager::NotifyDisplayPowerEvent(DisplayPowerEvent event, EventStatu void DisplayManager::NotifyDisplayStateChanged(DisplayState state) { WLOGFI("state:%{public}u", state); - std::lock_guard lock(mutex_); - if (displayStateCallback_) { - displayStateCallback_(state); - ClearDisplayStateCallback(); + std::lock_guard lock(pImpl_->mutex_); + if (pImpl_->displayStateCallback_) { + pImpl_->displayStateCallback_(state); + pImpl_->ClearDisplayStateCallback(); return; } WLOGFW("callback_ target is not set!"); } -void DisplayManager::ClearDisplayStateCallback() -{ - displayStateCallback_ = nullptr; - if (displayStateAgent_ != nullptr) { - SingletonContainer::Get().UnregisterDisplayManagerAgent(displayStateAgent_, - DisplayManagerAgentType::DISPLAY_STATE_LISTENER); - displayStateAgent_ = nullptr; - } -} - bool DisplayManager::WakeUpBegin(PowerStateChangeReason reason) { return SingletonContainer::Get().WakeUpBegin(reason); @@ -310,6 +332,7 @@ DisplayPowerState DisplayManager::GetScreenPower(uint64_t screenId) bool DisplayManager::SetDisplayState(DisplayState state, DisplayStateCallback callback) { WLOGFI("state:%{public}u", state); +<<<<<<< Updated upstream std::lock_guard lock(mutex_); if (displayStateCallback_ != nullptr || callback == nullptr) { WLOGFI("previous callback not called or callback invalid"); @@ -320,12 +343,23 @@ bool DisplayManager::SetDisplayState(DisplayState state, DisplayStateCallback ca if (displayStateAgent_ == nullptr) { displayStateAgent_ = new DisplayManagerAgent(); ret = SingletonContainer::Get().RegisterDisplayManagerAgent(displayStateAgent_, +======= + std::lock_guard lock(pImpl_->mutex_); + if (pImpl_->displayStateCallback_ != nullptr) { + WLOGFI("previous callback not called, can't reset"); + return false; + } + pImpl_->displayStateCallback_ = callback; + if (pImpl_->displayStateAgent_ == nullptr) { + pImpl_->displayStateAgent_ = new DisplayManagerAgent(); + SingletonContainer::Get().RegisterDisplayManagerAgent(pImpl_->displayStateAgent_, +>>>>>>> Stashed changes DisplayManagerAgentType::DISPLAY_STATE_LISTENER); } ret &= SingletonContainer::Get().SetDisplayState(state); if (!ret) { - ClearDisplayStateCallback(); + pImpl_->ClearDisplayStateCallback(); } return ret; } diff --git a/interfaces/innerkits/dm/display.h b/interfaces/innerkits/dm/display.h index f45d1e3fee..c4e09fd78a 100644 --- a/interfaces/innerkits/dm/display.h +++ b/interfaces/innerkits/dm/display.h @@ -46,11 +46,8 @@ public: void SetFreshRate(uint32_t freshRate); private: - std::string name_; - DisplayId id_ {DISPLAY_ID_INVALD}; - int32_t width_ {0}; - int32_t height_ {0}; - uint32_t freshRate_ {0}; + class Impl; + sptr pImpl_; }; } // namespace OHOS::Rosen diff --git a/interfaces/innerkits/dm/display_manager.h b/interfaces/innerkits/dm/display_manager.h index aa09f768f0..45aaaf1cea 100644 --- a/interfaces/innerkits/dm/display_manager.h +++ b/interfaces/innerkits/dm/display_manager.h @@ -23,9 +23,7 @@ #include "display.h" #include "dm_common.h" -#include "singleton_delegator.h" -// #include "wm_common.h" - +#include "wm_single_instance.h" namespace OHOS::Rosen { class DisplayManagerAdapter; @@ -64,19 +62,12 @@ public: private: DisplayManager(); ~DisplayManager(); - bool CheckRectValid(const Media::Rect &rect, int32_t oriHeight, int32_t oriWidth) const; - bool CheckSizeValid(const Media::Size &size, int32_t oriHeight, int32_t oriWidth) const; + + class Impl; + sptr pImpl_; + void NotifyDisplayPowerEvent(DisplayPowerEvent event, EventStatus status); void NotifyDisplayStateChanged(DisplayState state); - void ClearDisplayStateCallback(); - - static inline SingletonDelegator delegator; - const int32_t MAX_RESOLUTION_SIZE_SCREENSHOT = 15360; // max resolution, 16K - std::mutex mutex_; - std::vector> powerEventListeners_; - sptr powerEventListenerAgent_; - sptr displayStateAgent_; - DisplayStateCallback displayStateCallback_; }; } // namespace OHOS::Rosen -- Gitee