1 /*
2 * Copyright (c) 2022 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 "display_group_info.h"
17
18 #include <cinttypes>
19 #include "window_manager_hilog.h"
20
21 namespace OHOS {
22 namespace Rosen {
23 namespace {
24 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "DisplayGroupInfo"};
25 }
26
DisplayGroupInfo(ScreenId displayGroupId,const sptr<DisplayInfo> & displayInfo)27 DisplayGroupInfo::DisplayGroupInfo(ScreenId displayGroupId, const sptr<DisplayInfo>& displayInfo)
28 {
29 displayGroupId_ = displayGroupId;
30 AddDisplayInfo(displayInfo);
31 }
32
AddDisplayInfo(const sptr<DisplayInfo> & displayInfo)33 void DisplayGroupInfo::AddDisplayInfo(const sptr<DisplayInfo>& displayInfo)
34 {
35 DisplayId displayId = displayInfo->GetDisplayId();
36 if (displayInfosMap_.find(displayId) != displayInfosMap_.end()) {
37 WLOGFE("current display is exits, displayId: %{public}" PRIu64"", displayId);
38 return;
39 }
40 displayInfosMap_.insert(std::make_pair(displayId, displayInfo));
41 }
42
RemoveDisplayInfo(DisplayId displayId)43 void DisplayGroupInfo::RemoveDisplayInfo(DisplayId displayId)
44 {
45 if (displayInfosMap_.find(displayId) == displayInfosMap_.end()) {
46 WLOGFE("current display is not exits, displayId: %{public}" PRIu64"", displayId);
47 return;
48 }
49 displayInfosMap_.erase(displayId);
50 }
51
UpdateLeftAndRightDisplayId()52 void DisplayGroupInfo::UpdateLeftAndRightDisplayId()
53 {
54 auto displayRectMap = GetAllDisplayRects();
55 leftDisplayId_ = displayRectMap.begin()->first;
56 rightDisplayId_ = displayRectMap.begin()->first;
57 for (auto& elem : displayRectMap) {
58 auto& curDisplayRect = elem.second;
59 if (curDisplayRect.posX_ < displayRectMap[leftDisplayId_].posX_) {
60 leftDisplayId_ = elem.first;
61 }
62 if ((curDisplayRect.posX_ + static_cast<int32_t>(curDisplayRect.width_)) >
63 (displayRectMap[rightDisplayId_].posX_ + static_cast<int32_t>(displayRectMap[rightDisplayId_].width_))) {
64 rightDisplayId_ = elem.first;
65 }
66 }
67 WLOGFI("max posX displayId: %{public}" PRIu64", min posX displayId: %{public}" PRIu64"",
68 rightDisplayId_, leftDisplayId_);
69 }
70
SetDisplayRotation(DisplayId displayId,Rotation rotation)71 void DisplayGroupInfo::SetDisplayRotation(DisplayId displayId, Rotation rotation)
72 {
73 if (displayInfosMap_.find(displayId) == displayInfosMap_.end()) {
74 WLOGFE("current display is not exits, displayId: %{public}" PRIu64"", displayId);
75 return;
76 }
77 displayInfosMap_[displayId]->SetRotation(rotation);
78 }
79
SetDisplayVirtualPixelRatio(DisplayId displayId,float vpr)80 void DisplayGroupInfo::SetDisplayVirtualPixelRatio(DisplayId displayId, float vpr)
81 {
82 if (displayInfosMap_.find(displayId) == displayInfosMap_.end()) {
83 WLOGFE("current display is not exits, displayId: %{public}" PRIu64"", displayId);
84 return;
85 }
86 displayInfosMap_[displayId]->SetVirtualPixelRatio(vpr);
87 }
88
SetDisplayRect(DisplayId displayId,Rect displayRect)89 void DisplayGroupInfo::SetDisplayRect(DisplayId displayId, Rect displayRect)
90 {
91 if (displayInfosMap_.find(displayId) == displayInfosMap_.end()) {
92 WLOGFE("current display is not exits, displayId: %{public}" PRIu64"", displayId);
93 return;
94 }
95 auto& displayInfo = displayInfosMap_[displayId];
96 displayInfo->SetOffsetX(displayRect.posX_);
97 displayInfo->SetOffsetY(displayRect.posY_);
98 displayInfo->SetWidth(displayRect.width_);
99 displayInfo->SetHeight(displayRect.height_);
100 }
101
GetDisplayRotation(DisplayId displayId) const102 Rotation DisplayGroupInfo::GetDisplayRotation(DisplayId displayId) const
103 {
104 Rotation rotation = Rotation::ROTATION_0;
105 if (displayInfosMap_.find(displayId) != displayInfosMap_.end()) {
106 auto& displayInfo = displayInfosMap_[displayId];
107 rotation = displayInfo->GetRotation();
108 }
109 return rotation;
110 }
111
GetDisplayVirtualPixelRatio(DisplayId displayId) const112 float DisplayGroupInfo::GetDisplayVirtualPixelRatio(DisplayId displayId) const
113 {
114 float vpr = 1.0;
115 if (displayInfosMap_.find(displayId) != displayInfosMap_.end()) {
116 auto& displayInfo = displayInfosMap_[displayId];
117 vpr = displayInfo->GetVirtualPixelRatio();
118 }
119 return vpr;
120 }
121
GetAllDisplayRects() const122 std::map<DisplayId, Rect> DisplayGroupInfo::GetAllDisplayRects() const
123 {
124 std::map<DisplayId, Rect> displayRectMap;
125 for (auto elem : displayInfosMap_) {
126 auto& displayInfo = elem.second;
127 Rect displayRect = { displayInfo->GetOffsetX(), displayInfo->GetOffsetY(),
128 displayInfo->GetWidth(), displayInfo->GetHeight() };
129 displayRectMap.insert(std::make_pair(elem.first, displayRect));
130 }
131 return displayRectMap;
132 }
133
GetDisplayRect(DisplayId displayId) const134 Rect DisplayGroupInfo::GetDisplayRect(DisplayId displayId) const
135 {
136 Rect rect;
137 if (displayInfosMap_.find(displayId) != displayInfosMap_.end()) {
138 auto& displayInfo = displayInfosMap_[displayId];
139 rect = { displayInfo->GetOffsetX(), displayInfo->GetOffsetY(),
140 displayInfo->GetWidth(), displayInfo->GetHeight() };
141 }
142 return rect;
143 }
144
GetDisplayInfo(DisplayId displayId) const145 sptr<DisplayInfo> DisplayGroupInfo::GetDisplayInfo(DisplayId displayId) const
146 {
147 if (displayInfosMap_.find(displayId) != displayInfosMap_.end()) {
148 return displayInfosMap_[displayId];
149 }
150 return nullptr;
151 }
152
UpdateDisplayInfo(sptr<DisplayInfo> displayInfo) const153 void DisplayGroupInfo::UpdateDisplayInfo(sptr<DisplayInfo> displayInfo) const
154 {
155 DisplayId displayId = displayInfo->GetDisplayId();
156 if (displayInfosMap_.find(displayId) != displayInfosMap_.end()) {
157 displayInfosMap_[displayId] = displayInfo;
158 WLOGFD("Update displayInfo");
159 }
160 }
161
GetAllDisplayInfo() const162 std::vector<sptr<DisplayInfo>> DisplayGroupInfo::GetAllDisplayInfo() const
163 {
164 std::vector<sptr<DisplayInfo>> displayInfos;
165 for (auto& iter : displayInfosMap_) {
166 displayInfos.push_back(iter.second);
167 }
168 return displayInfos;
169 }
170
GetLeftDisplayId() const171 DisplayId DisplayGroupInfo::GetLeftDisplayId() const
172 {
173 return leftDisplayId_;
174 }
175
GetRightDisplayId() const176 DisplayId DisplayGroupInfo::GetRightDisplayId() const
177 {
178 return rightDisplayId_;
179 }
180 } // namespace Rosen
181 } // namespace OHOS
182