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 {
GetDisplayInfo(int32_t displayId)21 RefPtr<DisplayInfo> DisplayInfoUtils::GetDisplayInfo(int32_t displayId)
22 {
23 auto displayManager = Rosen::DisplayManager::GetInstance().GetDisplayById(displayId);
24 if (!displayManager) {
25 displayManager = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
26 }
27 CHECK_NULL_RETURN(displayManager, nullptr);
28 displayInfo_->SetWidth(displayManager->GetWidth());
29 displayInfo_->SetHeight(displayManager->GetHeight());
30 displayInfo_->SetDisplayId(displayManager->GetId());
31 auto dmRotation = displayManager->GetRotation();
32 displayInfo_->SetRotation(static_cast<Rotation>(static_cast<uint32_t>(dmRotation)));
33 GetIsFoldable();
34 GetCurrentFoldStatus();
35 GetCurrentFoldCreaseRegion();
36 return displayInfo_;
37 }
38
InitIsFoldable()39 void DisplayInfoUtils::InitIsFoldable()
40 {
41 auto isFoldable = Rosen::DisplayManager::GetInstance().IsFoldable();
42 displayInfo_->SetIsFoldable(isFoldable);
43 }
44
GetIsFoldable()45 bool DisplayInfoUtils::GetIsFoldable()
46 {
47 if (hasInitIsFoldable_) {
48 return displayInfo_->GetIsFoldable();
49 }
50 auto isFoldable = Rosen::DisplayManager::GetInstance().IsFoldable();
51 displayInfo_->SetIsFoldable(isFoldable);
52 hasInitIsFoldable_ = true;
53 return isFoldable;
54 }
55
GetCurrentFoldStatus()56 FoldStatus DisplayInfoUtils::GetCurrentFoldStatus()
57 {
58 auto dmFoldStatus = Rosen::DisplayManager::GetInstance().GetFoldStatus();
59 displayInfo_->SetFoldStatus(static_cast<FoldStatus>(static_cast<uint32_t>(dmFoldStatus)));
60 return displayInfo_->GetFoldStatus();
61 }
62
GetCurrentFoldCreaseRegion()63 std::vector<Rect> DisplayInfoUtils::GetCurrentFoldCreaseRegion()
64 {
65 if (hasInitFoldCreaseRegion_) {
66 return displayInfo_->GetCurrentFoldCreaseRegion();
67 }
68 std::vector<Rect> rects;
69 auto foldCreaseRegion = Rosen::DisplayManager::GetInstance().GetCurrentFoldCreaseRegion();
70 if (!foldCreaseRegion) {
71 TAG_LOGW(AceLogTag::ACE_OVERLAY, "failed to get foldCreaseRegion");
72 return rects;
73 }
74
75 auto creaseRects = foldCreaseRegion->GetCreaseRects();
76 if (creaseRects.empty()) {
77 return rects;
78 }
79
80 for (const auto& item : creaseRects) {
81 Rect rect;
82 rect.SetRect(item.posX_, item.posY_, item.width_, item.height_);
83 rects.insert(rects.end(), rect);
84 }
85 displayInfo_->SetCurrentFoldCreaseRegion(rects);
86 hasInitFoldCreaseRegion_ = true;
87 return rects;
88 }
89
GetDisplayAvailableRect(int32_t displayId) const90 Rect DisplayInfoUtils::GetDisplayAvailableRect(int32_t displayId) const
91 {
92 auto display = Rosen::DisplayManager::GetInstance().GetDisplayById(displayId);
93 if (!display) {
94 TAG_LOGW(AceLogTag::ACE_WINDOW, "failed to get display by id: %{public}u", (uint32_t)displayId);
95 return Rect();
96 }
97
98 Rosen::DMRect availableArea;
99 Rosen::DMError ret = display->GetAvailableArea(availableArea);
100 if (ret != Rosen::DMError::DM_OK) {
101 TAG_LOGW(AceLogTag::ACE_WINDOW, "failed to get availableArea of displayId: %{public}u", (uint32_t)displayId);
102 return Rect();
103 }
104
105 return Rect(availableArea.posX_, availableArea.posY_, availableArea.width_, availableArea.height_);
106 }
107 } // namespace OHOS::Ace::DisplayInfoUtils
108