1 /*
2 * Copyright (c) 2022-2023 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 #include "display_group_controller.h"
16
17 #include "window_helper.h"
18 #include "window_inner_manager.h"
19 #include "window_manager_hilog.h"
20 #include "window_node_container.h"
21
22 namespace OHOS {
23 namespace Rosen {
24 namespace {
25 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "DisplayGroupController"};
26 }
27
InitNewDisplay(DisplayId displayId)28 void DisplayGroupController::InitNewDisplay(DisplayId displayId)
29 {
30 // system bar map for display
31 SysBarNodeMap sysBarNodeMap {
32 { WindowType::WINDOW_TYPE_STATUS_BAR, nullptr },
33 { WindowType::WINDOW_TYPE_NAVIGATION_BAR, nullptr },
34 };
35 sysBarNodeMaps_.insert(std::make_pair(displayId, sysBarNodeMap));
36
37 SysBarTintMap sysBarTintMap {
38 { WindowType::WINDOW_TYPE_STATUS_BAR, SystemBarRegionTint() },
39 { WindowType::WINDOW_TYPE_NAVIGATION_BAR, SystemBarRegionTint() },
40 };
41 sysBarTintMaps_.insert(std::make_pair(displayId, sysBarTintMap));
42
43 // window node maps for display
44 std::map<WindowRootNodeType, std::unique_ptr<std::vector<sptr<WindowNode>>>> displayWindowTree;
45 displayWindowTree.insert(std::make_pair(WindowRootNodeType::APP_WINDOW_NODE,
46 std::make_unique<std::vector<sptr<WindowNode>>>()));
47 displayWindowTree.insert(std::make_pair(WindowRootNodeType::ABOVE_WINDOW_NODE,
48 std::make_unique<std::vector<sptr<WindowNode>>>()));
49 displayWindowTree.insert(std::make_pair(WindowRootNodeType::BELOW_WINDOW_NODE,
50 std::make_unique<std::vector<sptr<WindowNode>>>()));
51 displayGroupWindowTree_.insert(std::make_pair(displayId, std::move(displayWindowTree)));
52
53 // window pair for display
54 auto windowPair = new WindowPair(displayId);
55 windowPairMap_.insert(std::make_pair(displayId, windowPair));
56 }
57
GetWindowNodesByDisplayIdAndRootType(DisplayId displayId,WindowRootNodeType type)58 std::vector<sptr<WindowNode>>* DisplayGroupController::GetWindowNodesByDisplayIdAndRootType(DisplayId displayId,
59 WindowRootNodeType type)
60 {
61 if (displayGroupWindowTree_.find(displayId) != displayGroupWindowTree_.end()) {
62 auto& displayWindowTree = displayGroupWindowTree_[displayId];
63 if (displayWindowTree.find(type) != displayWindowTree.end()) {
64 return displayWindowTree[type].get();
65 }
66 }
67 return nullptr;
68 }
69
AddWindowNodeOnWindowTree(sptr<WindowNode> & node,WindowRootNodeType rootType)70 void DisplayGroupController::AddWindowNodeOnWindowTree(sptr<WindowNode>& node, WindowRootNodeType rootType)
71 {
72 std::vector<sptr<WindowNode>>* rootNodeVectorPtr = GetWindowNodesByDisplayIdAndRootType(node->GetDisplayId(),
73 rootType);
74 if (rootNodeVectorPtr != nullptr) {
75 rootNodeVectorPtr->push_back(node);
76 WLOGFD("add node in node vector of root, displayId: %{public}" PRIu64" windowId: %{public}d, "
77 "rootType: %{public}d", node->GetDisplayId(), node->GetWindowId(), rootType);
78 } else {
79 WLOGFE("add node failed, rootNode vector is empty, windowId: %{public}d, rootType: %{public}d",
80 node->GetWindowId(), rootType);
81 }
82 }
83
UpdateDisplayGroupWindowTree()84 void DisplayGroupController::UpdateDisplayGroupWindowTree()
85 {
86 // clear ori window tree of displayGroup
87 for (auto& elem : displayGroupWindowTree_) {
88 for (auto& nodeVec : elem.second) {
89 auto emptyVector = std::vector<sptr<WindowNode>>();
90 nodeVec.second->swap(emptyVector);
91 }
92 }
93 std::vector<WindowRootNodeType> rootNodeType = {
94 WindowRootNodeType::ABOVE_WINDOW_NODE,
95 WindowRootNodeType::APP_WINDOW_NODE,
96 WindowRootNodeType::BELOW_WINDOW_NODE
97 };
98 for (auto& rootType : rootNodeType) {
99 auto rootNode = windowNodeContainer_->GetRootNode(rootType);
100 if (rootNode == nullptr) {
101 WLOGFE("rootNode is nullptr, %{public}d", rootType);
102 continue;
103 }
104 for (auto& node : rootNode->children_) {
105 AddWindowNodeOnWindowTree(node, rootType);
106 }
107 }
108 }
109
ProcessCrossNodes(DisplayId defaultDisplayId,DisplayStateChangeType type)110 void DisplayGroupController::ProcessCrossNodes(DisplayId defaultDisplayId, DisplayStateChangeType type)
111 {
112 defaultDisplayId_ = defaultDisplayId;
113 for (auto& iter : displayGroupWindowTree_) {
114 auto nodeVec = *(iter.second[WindowRootNodeType::APP_WINDOW_NODE]);
115 for (auto node : nodeVec) {
116 if (node->isShowingOnMultiDisplays_) {
117 WLOGFD("process cross node, windowId: %{public}u, displayId: %{public}" PRIu64"",
118 node->GetWindowId(), node->GetDisplayId());
119 auto showingDisplays = node->GetShowingDisplays();
120
121 DisplayId newDisplayId;
122 if (type == DisplayStateChangeType::SIZE_CHANGE ||
123 type == DisplayStateChangeType::UPDATE_ROTATION ||
124 type == DisplayStateChangeType::DISPLAY_COMPRESS ||
125 type == DisplayStateChangeType::UPDATE_ROTATION_FROM_WINDOW) {
126 newDisplayId = node->GetDisplayId();
127 } else {
128 newDisplayId = defaultDisplayId;
129 }
130
131 for (auto& displayId : showingDisplays) {
132 if (displayId == newDisplayId) {
133 continue;
134 }
135 windowNodeContainer_->RemoveNodeFromRSTree(node, displayId, newDisplayId,
136 WindowUpdateType::WINDOW_UPDATE_ACTIVE);
137 }
138 // update shown displays and displayId
139 MoveCrossNodeToTargetDisplay(node, newDisplayId);
140 }
141 }
142 }
143 }
144
UpdateWindowShowingDisplays(const sptr<WindowNode> & node)145 void DisplayGroupController::UpdateWindowShowingDisplays(const sptr<WindowNode>& node)
146 {
147 auto& displayGroupInfo = DisplayGroupInfo::GetInstance();
148 auto leftDisplayId = displayGroupInfo.GetLeftDisplayId();
149 auto rightDisplayId = displayGroupInfo.GetRightDisplayId();
150 auto displayRectMap = displayGroupInfo.GetAllDisplayRects();
151 auto showingDisplays = std::vector<DisplayId>();
152 const auto& winRect = node->GetWindowRect();
153 for (auto& elem : displayRectMap) {
154 auto& curDisplayRect = elem.second;
155
156 // if window is showing in display region
157 if (((winRect.posX_ + static_cast<int32_t>(winRect.width_)) > curDisplayRect.posX_) &&
158 (winRect.posX_ < (curDisplayRect.posX_ + static_cast<int32_t>(curDisplayRect.width_)))) {
159 showingDisplays.push_back(elem.first);
160 }
161 }
162
163 // if window is not showing on any display, maybe in the left of minPosX display, or the right of maxPosX display
164 if (showingDisplays.empty()) {
165 if (((winRect.posX_ + static_cast<int32_t>(winRect.width_)) <=
166 displayRectMap[leftDisplayId].posX_)) {
167 showingDisplays.push_back(leftDisplayId);
168 }
169 if (winRect.posX_ >=
170 (displayRectMap[rightDisplayId].posX_ + static_cast<int32_t>(displayRectMap[rightDisplayId].width_))) {
171 showingDisplays.push_back(rightDisplayId);
172 }
173 }
174
175 // mean that this is cross-display window
176 if (showingDisplays.size() > 1) {
177 node->isShowingOnMultiDisplays_ = true;
178 } else {
179 node->isShowingOnMultiDisplays_ = false;
180 }
181 node->SetShowingDisplays(showingDisplays);
182 }
183
UpdateWindowDisplayIdIfNeeded(const sptr<WindowNode> & node)184 void DisplayGroupController::UpdateWindowDisplayIdIfNeeded(const sptr<WindowNode>& node)
185 {
186 // current multi-display is only support left-right combination, maxNum is two
187 DisplayId newDisplayId = node->GetDisplayId();
188 const auto& curShowingDisplays = node->GetShowingDisplays();
189 if (curShowingDisplays.empty()) {
190 WLOGFE("id:%{public}u not show on any display!", node->GetWindowId());
191 return;
192 }
193 const auto& winRect = node->GetWindowRect();
194 if (curShowingDisplays.size() == 1) {
195 newDisplayId = *(curShowingDisplays.begin());
196 } else {
197 // if more than half width of the window is showing on the display, means the window belongs to this display
198 int32_t halfWidth = static_cast<int32_t>(winRect.width_ * 0.5);
199 const auto& displayRectMap = DisplayGroupInfo::GetInstance().GetAllDisplayRects();
200 for (auto& elem : displayRectMap) {
201 auto& displayRect = elem.second;
202 if ((winRect.posX_ < displayRect.posX_) &&
203 (winRect.posX_ + static_cast<int32_t>(winRect.width_) >
204 displayRect.posX_ + static_cast<int32_t>(displayRect.width_))) { // window covers whole display region
205 newDisplayId = elem.first;
206 break;
207 }
208 if (winRect.posX_ >= displayRect.posX_) { // current display is default display
209 if ((displayRect.posX_ + static_cast<int32_t>(displayRect.width_) - winRect.posX_) >= halfWidth) {
210 newDisplayId = elem.first;
211 break;
212 }
213 } else { // current display is expand display
214 if ((winRect.posX_ + static_cast<int32_t>(winRect.width_) - displayRect.posX_) >= halfWidth) {
215 newDisplayId = elem.first;
216 break;
217 }
218 }
219 }
220 }
221
222 // update displayId if needed
223 if (node->GetDisplayId() != newDisplayId) {
224 UpdateWindowDisplayId(node, newDisplayId);
225 UpdateDisplayGroupWindowTree();
226 }
227 }
228
ChangeToRectInDisplayGroup(const sptr<WindowNode> & node,DisplayId displayId)229 void DisplayGroupController::ChangeToRectInDisplayGroup(const sptr<WindowNode>& node, DisplayId displayId)
230 {
231 Rect requestRect = node->GetRequestRect();
232 const Rect& displayRect = DisplayGroupInfo::GetInstance().GetDisplayRect(displayId);
233 requestRect.posX_ += displayRect.posX_;
234 requestRect.posY_ += displayRect.posY_;
235 node->SetRequestRect(requestRect);
236
237 std::vector<DisplayId> curShowingDisplays = { node->GetDisplayId() };
238 node->SetShowingDisplays(curShowingDisplays);
239 }
240
PreProcessWindowNode(const sptr<WindowNode> & node,WindowUpdateType type)241 void DisplayGroupController::PreProcessWindowNode(const sptr<WindowNode>& node, WindowUpdateType type)
242 {
243 if (!windowNodeContainer_->GetLayoutPolicy()->IsMultiDisplay()) {
244 if (type == WindowUpdateType::WINDOW_UPDATE_ADDED) {
245 std::vector<DisplayId> curShowingDisplays = { node->GetDisplayId() };
246 node->SetShowingDisplays(curShowingDisplays);
247 for (auto& childNode : node->children_) {
248 PreProcessWindowNode(childNode, type);
249 }
250 }
251 WLOGFD("Current mode is not multi-display");
252 return;
253 }
254
255 switch (type) {
256 case WindowUpdateType::WINDOW_UPDATE_ADDED: {
257 if (!node->isShowingOnMultiDisplays_) {
258 // change rect to rect in display group
259 ChangeToRectInDisplayGroup(node, node->GetDisplayId());
260 }
261 WLOGFD("preprocess node when add window");
262 break;
263 }
264 case WindowUpdateType::WINDOW_UPDATE_ACTIVE: {
265 // MoveTo can be called by user, calculate rect in display group if the reason is move
266 if (node->GetWindowSizeChangeReason() == WindowSizeChangeReason::MOVE) {
267 ChangeToRectInDisplayGroup(node, defaultDisplayId_);
268 }
269 WLOGFD("preprocess node when update window");
270 break;
271 }
272 default:
273 break;
274 }
275
276 for (auto& childNode : node->children_) {
277 PreProcessWindowNode(childNode, type);
278 }
279 }
280
PostProcessWindowNode(const sptr<WindowNode> & node)281 void DisplayGroupController::PostProcessWindowNode(const sptr<WindowNode>& node)
282 {
283 if (!windowNodeContainer_->GetLayoutPolicy()->IsMultiDisplay()) {
284 WLOGFD("Current mode is not multi-display");
285 return;
286 }
287
288 UpdateWindowShowingDisplays(node);
289 UpdateWindowDisplayIdIfNeeded(node);
290 }
291
UpdateWindowDisplayId(const sptr<WindowNode> & node,DisplayId newDisplayId)292 void DisplayGroupController::UpdateWindowDisplayId(const sptr<WindowNode>& node, DisplayId newDisplayId)
293 {
294 WLOGFD("update node displayId, srcDisplayId: %{public}" PRIu64", newDisplayId: %{public}" PRIu64"",
295 node->GetDisplayId(), newDisplayId);
296 if (node->GetWindowToken()) {
297 node->GetWindowToken()->UpdateDisplayId(node->GetDisplayId(), newDisplayId);
298 }
299 node->SetDisplayId(newDisplayId);
300 }
301
MoveCrossNodeToTargetDisplay(const sptr<WindowNode> & node,DisplayId targetDisplayId)302 void DisplayGroupController::MoveCrossNodeToTargetDisplay(const sptr<WindowNode>& node, DisplayId targetDisplayId)
303 {
304 node->isShowingOnMultiDisplays_ = false;
305 // update showing display
306 std::vector<DisplayId> newShowingDisplays = { targetDisplayId };
307 node->SetShowingDisplays(newShowingDisplays);
308 // update new displayId
309 if (node->GetDisplayId() != targetDisplayId) {
310 UpdateWindowDisplayId(node, targetDisplayId);
311 }
312
313 for (auto& childNode : node->children_) {
314 MoveCrossNodeToTargetDisplay(childNode, targetDisplayId);
315 }
316 }
317
MoveNotCrossNodeToDefaultDisplay(const sptr<WindowNode> & node,DisplayId displayId)318 void DisplayGroupController::MoveNotCrossNodeToDefaultDisplay(const sptr<WindowNode>& node, DisplayId displayId)
319 {
320 WLOGFD("windowId: %{public}d, displayId: %{public}" PRIu64"", node->GetWindowId(), displayId);
321 // update new rect in display group
322 const Rect& srcDisplayRect = DisplayGroupInfo::GetInstance().GetDisplayRect(displayId);
323 const Rect& dstDisplayRect = DisplayGroupInfo::GetInstance().GetDisplayRect(defaultDisplayId_);
324 Rect newRect = node->GetRequestRect();
325 if (node->GetWindowType() == WindowType::WINDOW_TYPE_POINTER) {
326 newRect.posX_ = static_cast<int32_t>(dstDisplayRect.width_ / 2); // default pointerX : displayRect.width / 2
327 newRect.posY_ = static_cast<int32_t>(dstDisplayRect.height_ / 2); // default pointerY : displayRect.height / 2
328 } else {
329 newRect.posX_ = newRect.posX_ - srcDisplayRect.posX_ + dstDisplayRect.posX_;
330 newRect.posY_ = newRect.posY_ - srcDisplayRect.posY_ + dstDisplayRect.posY_;
331 }
332
333 node->SetRequestRect(newRect);
334 // update showing display
335 std::vector<DisplayId> newShowingDisplays = { defaultDisplayId_ };
336 node->SetShowingDisplays(newShowingDisplays);
337 // update new displayId
338 UpdateWindowDisplayId(node, defaultDisplayId_);
339
340 for (auto& childNode : node->children_) {
341 MoveNotCrossNodeToDefaultDisplay(childNode, displayId);
342 }
343 }
344
ProcessNotCrossNodesOnDestroyedDisplay(DisplayId displayId,std::vector<uint32_t> & windowIds)345 void DisplayGroupController::ProcessNotCrossNodesOnDestroyedDisplay(DisplayId displayId,
346 std::vector<uint32_t>& windowIds)
347 {
348 if (displayId == defaultDisplayId_) {
349 WLOGFE("Move window nodes failed, displayId is the same as defaultDisplayId");
350 return;
351 }
352 if (displayGroupWindowTree_.find(displayId) == displayGroupWindowTree_.end()) {
353 WLOGFE("displayId: %{public}" PRIu64" not in display group window tree", displayId);
354 return;
355 }
356 WLOGI("move window nodes for display destroy, displayId: %{public}" PRIu64"", displayId);
357
358 std::vector<WindowRootNodeType> rootNodeType = {
359 WindowRootNodeType::ABOVE_WINDOW_NODE,
360 WindowRootNodeType::APP_WINDOW_NODE,
361 WindowRootNodeType::BELOW_WINDOW_NODE
362 };
363 for (const auto& type : rootNodeType) {
364 if (displayGroupWindowTree_[displayId].find(type) == displayGroupWindowTree_[displayId].end()) {
365 continue;
366 }
367 auto nodesVec = *(displayGroupWindowTree_[displayId][type]);
368 for (auto node : nodesVec) {
369 WLOGFD("node on destroied display, windowId: %{public}d, isShowingOnMulti: %{public}d",
370 node->GetWindowId(), node->isShowingOnMultiDisplays_);
371 if (node->GetDisplayId() != displayId || node->isShowingOnMultiDisplays_) {
372 continue;
373 }
374 // destroy status and navigation bar
375 if (node->GetWindowType() == WindowType::WINDOW_TYPE_STATUS_BAR ||
376 node->GetWindowType() == WindowType::WINDOW_TYPE_NAVIGATION_BAR) {
377 windowNodeContainer_->DestroyWindowNode(node, windowIds);
378 WLOGFW("destroy status or navigation bar on destroyed display, windowId: %{public}d",
379 node->GetWindowId());
380 continue;
381 }
382 // move not cross-display nodes to default display
383 MoveNotCrossNodeToDefaultDisplay(node, displayId);
384
385 // update RS tree
386 windowNodeContainer_->RemoveNodeFromRSTree(node, displayId, defaultDisplayId_,
387 WindowUpdateType::WINDOW_UPDATE_ACTIVE);
388 windowNodeContainer_->AddNodeOnRSTree(node, defaultDisplayId_, defaultDisplayId_,
389 WindowUpdateType::WINDOW_UPDATE_ACTIVE);
390 }
391 }
392 }
393
ProcessDisplayCreate(DisplayId defaultDisplayId,sptr<DisplayInfo> displayInfo,const std::map<DisplayId,Rect> & displayRectMap)394 void DisplayGroupController::ProcessDisplayCreate(DisplayId defaultDisplayId, sptr<DisplayInfo> displayInfo,
395 const std::map<DisplayId, Rect>& displayRectMap)
396 {
397 defaultDisplayId_ = defaultDisplayId;
398 WLOGI("defaultDisplay, displayId: %{public}" PRIu64"", defaultDisplayId);
399
400 DisplayId displayId = displayInfo->GetDisplayId();
401
402 InitNewDisplay(displayId);
403
404 // add displayInfo in displayGroupInfo
405 DisplayGroupInfo::GetInstance().AddDisplayInfo(displayInfo);
406
407 // modify RSTree and window tree of displayGroup for cross-display nodes
408 ProcessCrossNodes(defaultDisplayId, DisplayStateChangeType::CREATE);
409 UpdateDisplayGroupWindowTree();
410 const auto& layoutPolicy = windowNodeContainer_->GetLayoutPolicy();
411 if (layoutPolicy == nullptr) {
412 return;
413 }
414 layoutPolicy->ProcessDisplayCreate(displayId, displayRectMap);
415 ProcessWindowPairWhenDisplayChange();
416 }
417
ProcessDisplayDestroy(DisplayId defaultDisplayId,sptr<DisplayInfo> displayInfo,const std::map<DisplayId,Rect> & displayRectMap,std::vector<uint32_t> & windowIds)418 void DisplayGroupController::ProcessDisplayDestroy(DisplayId defaultDisplayId, sptr<DisplayInfo> displayInfo,
419 const std::map<DisplayId, Rect>& displayRectMap,
420 std::vector<uint32_t>& windowIds)
421 {
422 defaultDisplayId_ = defaultDisplayId;
423 DisplayGroupInfo::GetInstance().SetDefaultDisplayId(defaultDisplayId);
424 DisplayId displayId = displayInfo->GetDisplayId();
425 // delete nodes and map element of deleted display
426 ProcessNotCrossNodesOnDestroyedDisplay(displayId, windowIds);
427 // modify RSTree and window tree of displayGroup for cross-display nodes
428 ProcessCrossNodes(defaultDisplayId, DisplayStateChangeType::DESTROY);
429 UpdateDisplayGroupWindowTree();
430 ClearMapOfDestroyedDisplay(displayId);
431 windowNodeContainer_->GetLayoutPolicy()->ProcessDisplayDestroy(displayId, displayRectMap);
432 ProcessWindowPairWhenDisplayChange();
433 }
434
ProcessSystemBarRotation(const sptr<WindowNode> & node,const std::map<DisplayId,Rect> & displayRectMap)435 void DisplayGroupController::ProcessSystemBarRotation(const sptr<WindowNode>& node,
436 const std::map<DisplayId, Rect>& displayRectMap)
437 {
438 auto rect = node->GetWindowRect();
439 auto displayId = node->GetDisplayId();
440 auto iter = displayRectMap.find(displayId);
441 if (iter == displayRectMap.end()) {
442 return;
443 }
444 auto displayRect = iter->second;
445 if (node->GetWindowType() == WindowType::WINDOW_TYPE_STATUS_BAR) {
446 rect.width_ = displayRect.width_;
447 } else if (node->GetWindowType() == WindowType::WINDOW_TYPE_NAVIGATION_BAR) {
448 rect.posY_ = static_cast<int32_t>(displayRect.height_ - rect.height_) + displayRect.posY_;
449 rect.width_ = displayRect.width_;
450 }
451
452 node->SetRequestRect(rect);
453 }
454
UpdateNodeSizeChangeReasonWithRotation(DisplayId displayId,const std::map<DisplayId,Rect> & displayRectMap)455 void DisplayGroupController::UpdateNodeSizeChangeReasonWithRotation(DisplayId displayId,
456 const std::map<DisplayId, Rect>& displayRectMap)
457 {
458 std::vector<WindowRootNodeType> rootNodeType = {
459 WindowRootNodeType::ABOVE_WINDOW_NODE,
460 WindowRootNodeType::APP_WINDOW_NODE,
461 WindowRootNodeType::BELOW_WINDOW_NODE
462 };
463 for (auto& rootType : rootNodeType) {
464 std::vector<sptr<WindowNode>>* rootNodeVectorPtr = GetWindowNodesByDisplayIdAndRootType(displayId, rootType);
465 if (rootNodeVectorPtr == nullptr) {
466 WLOGFE("rootNodeVectorPtr is nullptr, %{public}d, displayId: %{public}" PRIu64, rootType, displayId);
467 return;
468 }
469 for (auto& node : (*rootNodeVectorPtr)) {
470 // DOCK_SLICE not need do rotation animation
471 if (node->GetWindowType() == WindowType::WINDOW_TYPE_DOCK_SLICE) {
472 continue;
473 }
474 if (WindowHelper::IsSystemBarWindow(node->GetWindowType())) {
475 ProcessSystemBarRotation(node, displayRectMap);
476 }
477 node->SetWindowSizeChangeReason(WindowSizeChangeReason::ROTATION);
478 }
479 }
480 }
481
ProcessDisplayChange(DisplayId defaultDisplayId,sptr<DisplayInfo> displayInfo,const std::map<DisplayId,Rect> & displayRectMap,DisplayStateChangeType type)482 void DisplayGroupController::ProcessDisplayChange(DisplayId defaultDisplayId, sptr<DisplayInfo> displayInfo,
483 const std::map<DisplayId, Rect>& displayRectMap,
484 DisplayStateChangeType type)
485 {
486 defaultDisplayId_ = defaultDisplayId;
487 auto& displayGroupInfo = DisplayGroupInfo::GetInstance();
488 displayGroupInfo.SetDefaultDisplayId(defaultDisplayId);
489 DisplayId displayId = displayInfo->GetDisplayId();
490 WLOGI("display change, displayId: %{public}" PRIu64", type: %{public}d", displayId, type);
491 switch (type) {
492 case DisplayStateChangeType::UPDATE_ROTATION:
493 case DisplayStateChangeType::UPDATE_ROTATION_FROM_WINDOW: {
494 displayGroupInfo.SetDisplayRotation(displayId, displayInfo->GetRotation());
495 displayGroupInfo.SetDisplayOrientation(displayId, displayInfo->GetDisplayOrientation());
496 displayGroupInfo.SetDisplayStateChangeType(displayId, type);
497 [[fallthrough]];
498 }
499 case DisplayStateChangeType::DISPLAY_COMPRESS:
500 case DisplayStateChangeType::SIZE_CHANGE: {
501 ProcessDisplaySizeChangeOrRotation(defaultDisplayId, displayId, displayRectMap, type);
502 break;
503 }
504 case DisplayStateChangeType::VIRTUAL_PIXEL_RATIO_CHANGE: {
505 displayGroupInfo.SetDisplayVirtualPixelRatio(displayId, displayInfo->GetVirtualPixelRatio());
506 windowNodeContainer_->GetLayoutPolicy()->ProcessDisplayVprChange(displayId);
507 break;
508 }
509 default: {
510 break;
511 }
512 }
513 }
514
ProcessDisplaySizeChangeOrRotation(DisplayId defaultDisplayId,DisplayId displayId,const std::map<DisplayId,Rect> & displayRectMap,DisplayStateChangeType type)515 void DisplayGroupController::ProcessDisplaySizeChangeOrRotation(DisplayId defaultDisplayId, DisplayId displayId,
516 const std::map<DisplayId, Rect>& displayRectMap, DisplayStateChangeType type)
517 {
518 // modify RSTree and window tree of displayGroup for cross-display nodes
519 ProcessCrossNodes(defaultDisplayId, type);
520 UpdateDisplayGroupWindowTree();
521 // update reason after process cross Nodes to get correct display attribution
522 UpdateNodeSizeChangeReasonWithRotation(displayId, displayRectMap);
523 const auto& layoutPolicy = windowNodeContainer_->GetLayoutPolicy();
524 if (layoutPolicy == nullptr) {
525 return;
526 }
527 layoutPolicy->ProcessDisplaySizeChangeOrRotation(displayId, displayRectMap);
528 ProcessWindowPairWhenDisplayChange(true);
529 }
530
ClearMapOfDestroyedDisplay(DisplayId displayId)531 void DisplayGroupController::ClearMapOfDestroyedDisplay(DisplayId displayId)
532 {
533 sysBarTintMaps_.erase(displayId);
534 sysBarNodeMaps_.erase(displayId);
535 displayGroupWindowTree_.erase(displayId);
536 DisplayGroupInfo::GetInstance().RemoveDisplayInfo(displayId);
537 windowPairMap_.erase(displayId);
538 }
539
GetWindowPairByDisplayId(DisplayId displayId)540 sptr<WindowPair> DisplayGroupController::GetWindowPairByDisplayId(DisplayId displayId)
541 {
542 if (windowPairMap_.find(displayId) != windowPairMap_.end()) {
543 return windowPairMap_[displayId];
544 }
545 return nullptr;
546 }
547
ProcessWindowPairWhenDisplayChange(bool rotateDisplay)548 void DisplayGroupController::ProcessWindowPairWhenDisplayChange(bool rotateDisplay)
549 {
550 for (auto& elem : DisplayGroupInfo::GetInstance().GetAllDisplayRects()) {
551 const auto& displayId = elem.first;
552 const auto& windowPair = GetWindowPairByDisplayId(displayId);
553 if (windowPair == nullptr) {
554 WLOGFE("WindowPair is nullptr, displayId: %{public}" PRIu64"", displayId);
555 return;
556 }
557 const auto& layoutPolicy = windowNodeContainer_->GetLayoutPolicy();
558 if (layoutPolicy == nullptr) {
559 WLOGFE("LayoutPolicy is nullptr, displayId: %{public}" PRIu64"", displayId);
560 return;
561 }
562 Rect divRect = layoutPolicy->GetDividerRect(displayId);
563 if (rotateDisplay) {
564 windowPair->RotateDividerWindow(divRect);
565 } else {
566 windowPair->SetDividerRect(divRect);
567 }
568 UpdateSplitRatioPoints(displayId);
569 }
570 }
571
SetSplitRatioConfig(const SplitRatioConfig & splitRatioConfig)572 void DisplayGroupController::SetSplitRatioConfig(const SplitRatioConfig& splitRatioConfig)
573 {
574 for (auto& elem : DisplayGroupInfo::GetInstance().GetAllDisplayRects()) {
575 const auto& displayId = elem.first;
576 const auto& windowPair = GetWindowPairByDisplayId(displayId);
577 if (windowPair == nullptr) {
578 WLOGFE("WindowPair is nullptr, displayId: %{public}" PRIu64"", displayId);
579 continue;
580 }
581 windowPair->SetSplitRatioConfig(splitRatioConfig);
582 UpdateSplitRatioPoints(displayId);
583 }
584 }
585
UpdateSplitRatioPoints(DisplayId displayId)586 void DisplayGroupController::UpdateSplitRatioPoints(DisplayId displayId)
587 {
588 const auto& windowPair = GetWindowPairByDisplayId(displayId);
589 if (windowPair == nullptr) {
590 WLOGFE("WindowPair is nullptr, displayId: %{public}" PRIu64"", displayId);
591 return;
592 }
593 auto displayRects = DisplayGroupInfo::GetInstance().GetAllDisplayRects();
594 if (displayRects.find(displayId) == displayRects.end()) {
595 return;
596 }
597 windowPair->CalculateSplitRatioPoints(displayRects[displayId]);
598 const auto& layoutPolicy = windowNodeContainer_->GetLayoutPolicy();
599 if (layoutPolicy == nullptr) {
600 WLOGFE("LayoutPolicy is nullptr, displayId: %{public}" PRIu64"", displayId);
601 return;
602 }
603 layoutPolicy->SetSplitRatioPoints(displayId, windowPair->GetSplitRatioPoints());
604 }
605 } // namespace Rosen
606 } // namespace OHOS
607