• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "core/common/display_info_utils.h"
17 
18 #include "display_manager.h"
19 
20 namespace OHOS::Ace {
21 constexpr uint64_t DEFAULT_DISPLAY_ID = 0;
GetDisplayInfo(int32_t displayId)22 RefPtr<DisplayInfo> DisplayInfoUtils::GetDisplayInfo(int32_t displayId)
23 {
24     auto displayManager = Rosen::DisplayManager::GetInstance().GetDisplayById(displayId);
25     if (!displayManager) {
26         displayManager = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
27     }
28     CHECK_NULL_RETURN(displayManager, nullptr);
29     displayInfo_->SetWidth(displayManager->GetWidth());
30     displayInfo_->SetHeight(displayManager->GetHeight());
31     displayInfo_->SetDisplayId(displayManager->GetId());
32     auto dmRotation = displayManager->GetRotation();
33     displayInfo_->SetRotation(static_cast<Rotation>(static_cast<uint32_t>(dmRotation)));
34     GetIsFoldable();
35     GetCurrentFoldStatus();
36     GetCurrentFoldCreaseRegion();
37     return displayInfo_;
38 }
39 
InitIsFoldable()40 void DisplayInfoUtils::InitIsFoldable()
41 {
42     auto isFoldable = Rosen::DisplayManager::GetInstance().IsFoldable();
43     displayInfo_->SetIsFoldable(isFoldable);
44 }
45 
GetIsFoldable()46 bool DisplayInfoUtils::GetIsFoldable()
47 {
48     if (hasInitIsFoldable_) {
49         return displayInfo_->GetIsFoldable();
50     }
51     auto isFoldable = Rosen::DisplayManager::GetInstance().IsFoldable();
52     displayInfo_->SetIsFoldable(isFoldable);
53     hasInitIsFoldable_ = true;
54     return isFoldable;
55 }
56 
GetCurrentFoldStatus()57 FoldStatus DisplayInfoUtils::GetCurrentFoldStatus()
58 {
59     auto dmFoldStatus = Rosen::DisplayManager::GetInstance().GetFoldStatus();
60     displayInfo_->SetFoldStatus(static_cast<FoldStatus>(static_cast<uint32_t>(dmFoldStatus)));
61     return displayInfo_->GetFoldStatus();
62 }
63 
GetCurrentFoldCreaseRegion()64 std::vector<Rect> DisplayInfoUtils::GetCurrentFoldCreaseRegion()
65 {
66     if (hasInitFoldCreaseRegion_) {
67         return displayInfo_->GetCurrentFoldCreaseRegion();
68     }
69     std::vector<Rect> rects;
70     auto foldCreaseRegion = Rosen::DisplayManager::GetInstance().GetCurrentFoldCreaseRegion();
71     if (!foldCreaseRegion) {
72         TAG_LOGW(AceLogTag::ACE_OVERLAY, "failed to get foldCreaseRegion");
73         return rects;
74     }
75 
76     auto creaseRects = foldCreaseRegion->GetCreaseRects();
77     if (creaseRects.empty()) {
78         return rects;
79     }
80 
81     for (const auto& item : creaseRects) {
82         Rect rect;
83         rect.SetRect(item.posX_, item.posY_, item.width_, item.height_);
84         rects.insert(rects.end(), rect);
85     }
86     displayInfo_->SetCurrentFoldCreaseRegion(rects);
87     hasInitFoldCreaseRegion_ = true;
88     return rects;
89 }
90 
GetDisplayAvailableRect(int32_t displayId) const91 Rect DisplayInfoUtils::GetDisplayAvailableRect(int32_t displayId) const
92 {
93     auto display = Rosen::DisplayManager::GetInstance().GetDisplayById(displayId);
94     if (!display) {
95         TAG_LOGW(AceLogTag::ACE_WINDOW, "failed to get display by id: %{public}u", (uint32_t)displayId);
96         return Rect();
97     }
98 
99     Rosen::DMRect availableArea;
100     Rosen::DMError ret = display->GetAvailableArea(availableArea);
101     if (ret != Rosen::DMError::DM_OK) {
102         TAG_LOGW(AceLogTag::ACE_WINDOW, "failed to get availableArea of displayId: %{public}u", (uint32_t)displayId);
103         return Rect();
104     }
105 
106     return Rect(availableArea.posX_, availableArea.posY_, availableArea.width_, availableArea.height_);
107 }
108 
GetFoldExpandAvailableRect() const109 Rect DisplayInfoUtils::GetFoldExpandAvailableRect() const
110 {
111     Rosen::DMRect rect;
112     Rosen::DMError ret = Rosen::DisplayManager::GetInstance().GetExpandAvailableArea(DEFAULT_DISPLAY_ID, rect);
113     if (ret != Rosen::DMError::DM_OK) {
114         TAG_LOGW(AceLogTag::ACE_WINDOW, "failed to get expandAvailableArea");
115         return Rect();
116     }
117 
118     return Rect(rect.posX_, rect.posY_, rect.width_, rect.height_);
119 }
120 } // namespace OHOS::Ace::DisplayInfoUtils
121