• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "abstract_screen.h"
17 
18 #include <cmath>
19 #include "abstract_screen_controller.h"
20 #include "display_manager_service.h"
21 #include "dm_common.h"
22 #include "window_manager_hilog.h"
23 
24 namespace OHOS::Rosen {
25 namespace {
26     constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "AbstractScreenGroup"};
27     constexpr float MAX_ZORDER = 100000.0f;
28 }
29 
AbstractScreen(sptr<AbstractScreenController> screenController,const std::string & name,ScreenId dmsId,ScreenId rsId)30 AbstractScreen::AbstractScreen(sptr<AbstractScreenController> screenController, const std::string& name, ScreenId dmsId,
31     ScreenId rsId) : dmsId_(dmsId), rsId_(rsId), screenController_(screenController)
32 {
33     if (name != "") {
34         name_ = name;
35     }
36 }
37 
~AbstractScreen()38 AbstractScreen::~AbstractScreen()
39 {
40 }
41 
GetActiveScreenMode() const42 sptr<SupportedScreenModes> AbstractScreen::GetActiveScreenMode() const
43 {
44     if (activeIdx_ < 0 || activeIdx_ >= static_cast<int32_t>(modes_.size())) {
45         WLOGE("active mode index is wrong: %{public}d", activeIdx_);
46         return nullptr;
47     }
48     return modes_[activeIdx_];
49 }
50 
GetAbstractScreenModes() const51 std::vector<sptr<SupportedScreenModes>> AbstractScreen::GetAbstractScreenModes() const
52 {
53     return modes_;
54 }
55 
GetGroup() const56 sptr<AbstractScreenGroup> AbstractScreen::GetGroup() const
57 {
58     if (screenController_ == nullptr) {
59         return nullptr;
60     }
61     return screenController_->GetAbstractScreenGroup(groupDmsId_);
62 }
63 
ConvertToScreenInfo() const64 sptr<ScreenInfo> AbstractScreen::ConvertToScreenInfo() const
65 {
66     sptr<ScreenInfo> info = new(std::nothrow) ScreenInfo();
67     if (info == nullptr) {
68         return nullptr;
69     }
70     FillScreenInfo(info);
71     return info;
72 }
73 
UpdateRSTree(std::shared_ptr<RSSurfaceNode> & surfaceNode,bool isAdd,bool needToUpdate)74 void AbstractScreen::UpdateRSTree(std::shared_ptr<RSSurfaceNode>& surfaceNode, bool isAdd, bool needToUpdate)
75 {
76     if (rsDisplayNode_ == nullptr || surfaceNode == nullptr) {
77         WLOGFE("node is nullptr");
78         return;
79     }
80     WLOGFD("%{public}s surface: %{public}s, %{public}" PRIu64"", (isAdd ? "add" : "remove"),
81         surfaceNode->GetName().c_str(), surfaceNode->GetId());
82 
83     if (isAdd) {
84         surfaceNode->SetVisible(true);
85         rsDisplayNode_->AddChild(surfaceNode, -1);
86     } else {
87         rsDisplayNode_->RemoveChild(surfaceNode);
88     }
89 
90     if (needToUpdate) {
91         std::lock_guard<std::recursive_mutex> lock(mutex_);
92         if (isAdd) {
93             appSurfaceNodes_.push_back(surfaceNode);
94         } else {
95             auto iter = std::find_if(appSurfaceNodes_.begin(), appSurfaceNodes_.end(),
96                 [surfaceNode] (std::shared_ptr<RSSurfaceNode> node) {
97                     return surfaceNode->GetId() == node->GetId();
98                 });
99             if (iter != appSurfaceNodes_.end()) {
100                 appSurfaceNodes_.erase(iter);
101             }
102         }
103     }
104 }
105 
AddSurfaceNode(std::shared_ptr<RSSurfaceNode> & surfaceNode,bool onTop,bool needToRecord)106 DMError AbstractScreen::AddSurfaceNode(std::shared_ptr<RSSurfaceNode>& surfaceNode, bool onTop, bool needToRecord)
107 {
108     if (rsDisplayNode_ == nullptr || surfaceNode == nullptr) {
109         WLOGFE("node is nullptr");
110         return DMError::DM_ERROR_NULLPTR;
111     }
112     surfaceNode->SetVisible(true);
113     if (onTop) {
114         rsDisplayNode_->AddChild(surfaceNode, -1);
115         surfaceNode->SetPositionZ(MAX_ZORDER);
116     } else {
117         rsDisplayNode_->AddChild(surfaceNode, -1);
118     }
119     if (needToRecord) {
120         std::lock_guard<std::recursive_mutex> lock(mutex_);
121         nativeSurfaceNodes_.push_back(surfaceNode);
122     }
123     auto transactionProxy = RSTransactionProxy::GetInstance();
124     if (transactionProxy != nullptr) {
125         transactionProxy->FlushImplicitTransaction();
126     }
127     return DMError::DM_OK;
128 }
129 
RemoveSurfaceNode(std::shared_ptr<RSSurfaceNode> & surfaceNode)130 DMError AbstractScreen::RemoveSurfaceNode(std::shared_ptr<RSSurfaceNode>& surfaceNode)
131 {
132     if (rsDisplayNode_ == nullptr || surfaceNode == nullptr) {
133         WLOGFE("Node is nullptr");
134         return DMError::DM_ERROR_NULLPTR;
135     };
136     std::lock_guard<std::recursive_mutex> lock(mutex_);
137     auto iter = std::find_if(nativeSurfaceNodes_.begin(), nativeSurfaceNodes_.end(), [surfaceNode]
138         (std::shared_ptr<RSSurfaceNode> node) {
139         return surfaceNode->GetId() == node->GetId();
140     });
141     if (iter == nativeSurfaceNodes_.end()) {
142         WLOGFW("Child not found");
143         return DMError::DM_ERROR_INVALID_PARAM;
144     }
145     rsDisplayNode_->RemoveChild(*iter);
146     nativeSurfaceNodes_.erase(iter);
147     auto transactionProxy = RSTransactionProxy::GetInstance();
148     if (transactionProxy != nullptr) {
149         transactionProxy->FlushImplicitTransaction();
150     }
151     return DMError::DM_OK;
152 }
153 
UpdateDisplayGroupRSTree(std::shared_ptr<RSSurfaceNode> & surfaceNode,NodeId parentNodeId,bool isAdd)154 void AbstractScreen::UpdateDisplayGroupRSTree(std::shared_ptr<RSSurfaceNode>& surfaceNode, NodeId parentNodeId,
155     bool isAdd)
156 {
157     if (rsDisplayNode_ == nullptr || surfaceNode == nullptr) {
158         WLOGFE("node is nullptr");
159         return;
160     }
161     WLOGFI("%{public}s surface: %{public}s, %{public}" PRIu64"", (isAdd ? "add" : "remove"),
162         surfaceNode->GetName().c_str(), surfaceNode->GetId());
163 
164     if (isAdd) {
165         surfaceNode->SetVisible(true);
166         rsDisplayNode_->AddCrossParentChild(surfaceNode, -1);
167     } else {
168         rsDisplayNode_->RemoveCrossParentChild(surfaceNode, parentNodeId);
169     }
170 }
171 
SetPropertyForDisplayNode(const std::shared_ptr<RSDisplayNode> & rsDisplayNode,const RSDisplayNodeConfig & config,const Point & startPoint)172 void AbstractScreen::SetPropertyForDisplayNode(const std::shared_ptr<RSDisplayNode>& rsDisplayNode,
173     const RSDisplayNodeConfig& config, const Point& startPoint)
174 {
175     rSDisplayNodeConfig_ = config;
176     WLOGFI("SetDisplayOffset: posX:%{public}d, posY:%{public}d", startPoint.posX_, startPoint.posY_);
177     rsDisplayNode->SetDisplayOffset(startPoint.posX_, startPoint.posY_);
178     uint32_t width = 0;
179     uint32_t height = 0;
180     sptr<SupportedScreenModes> abstractScreenModes = GetActiveScreenMode();
181     if (abstractScreenModes != nullptr) {
182         height = abstractScreenModes->height_;
183         width = abstractScreenModes->width_;
184     }
185     RSScreenType screenType;
186     auto ret = RSInterfaces::GetInstance().GetScreenType(rsId_, screenType);
187     if (ret == StatusCode::SUCCESS && screenType == RSScreenType::VIRTUAL_TYPE_SCREEN) {
188         rsDisplayNode->SetSecurityDisplay(true);
189         WLOGFI("virtualScreen SetSecurityDisplay success");
190     }
191     // If setDisplayOffset is not valid for SetFrame/SetBounds
192     rsDisplayNode->SetFrame(0, 0, width, height);
193     rsDisplayNode->SetBounds(0, 0, width, height);
194 }
195 
InitRSDisplayNode(const RSDisplayNodeConfig & config,const Point & startPoint)196 void AbstractScreen::InitRSDisplayNode(const RSDisplayNodeConfig& config, const Point& startPoint)
197 {
198     if (rsDisplayNode_ != nullptr) {
199         rsDisplayNode_->SetDisplayNodeMirrorConfig(config);
200         WLOGFD("RSDisplayNode is not null");
201     } else {
202         WLOGFD("Create rsDisplayNode");
203         std::shared_ptr<RSDisplayNode> rsDisplayNode = RSDisplayNode::Create(config);
204         if (rsDisplayNode == nullptr) {
205             WLOGE("fail to add child. create rsDisplayNode fail!");
206             return;
207         }
208         rsDisplayNode_ = rsDisplayNode;
209     }
210     SetPropertyForDisplayNode(rsDisplayNode_, config, startPoint);
211 
212     // flush transaction
213     auto transactionProxy = RSTransactionProxy::GetInstance();
214     if (transactionProxy != nullptr) {
215         transactionProxy->FlushImplicitTransaction();
216     }
217     WLOGFD("InitRSDisplayNode success");
218 }
219 
InitRSDefaultDisplayNode(const RSDisplayNodeConfig & config,const Point & startPoint)220 void AbstractScreen::InitRSDefaultDisplayNode(const RSDisplayNodeConfig& config, const Point& startPoint)
221 {
222     if (rsDisplayNode_ == nullptr) {
223         WLOGFD("RSDisplayNode is nullptr");
224     }
225 
226     WLOGFD("Create defaultRSDisplayNode");
227     std::shared_ptr<RSDisplayNode> rsDisplayNode = RSDisplayNode::Create(config);
228     if (rsDisplayNode == nullptr) {
229         WLOGE("fail to add child. create rsDisplayNode fail!");
230         return;
231     }
232     rsDisplayNode_ = rsDisplayNode;
233     SetPropertyForDisplayNode(rsDisplayNode_, config, startPoint);
234 
235     std::lock_guard<std::recursive_mutex> lock(mutex_);
236     // update RSTree for default display
237     for (auto node: appSurfaceNodes_) {
238         UpdateRSTree(node, true, false);
239     }
240     for (auto node: nativeSurfaceNodes_) {
241         AddSurfaceNode(node, false, false);
242     }
243 
244     // flush transaction
245     auto transactionProxy = RSTransactionProxy::GetInstance();
246     if (transactionProxy != nullptr) {
247         transactionProxy->FlushImplicitTransaction();
248     }
249     WLOGFD("InitRSDefaultDisplayNode success");
250 }
251 
GetScreenGroupId() const252 ScreenId AbstractScreen::GetScreenGroupId() const
253 {
254     return groupDmsId_;
255 }
256 
GetScreenSupportedColorGamuts(std::vector<ScreenColorGamut> & colorGamuts)257 DMError AbstractScreen::GetScreenSupportedColorGamuts(std::vector<ScreenColorGamut>& colorGamuts)
258 {
259     auto ret = RSInterfaces::GetInstance().GetScreenSupportedColorGamuts(rsId_, colorGamuts);
260     if (ret != StatusCode::SUCCESS) {
261         WLOGE("GetScreenSupportedColorGamuts fail! rsId %{public}" PRIu64"", rsId_);
262         return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
263     }
264     WLOGI("GetScreenSupportedColorGamuts ok! rsId %{public}" PRIu64", size %{public}u",
265         rsId_, static_cast<uint32_t>(colorGamuts.size()));
266 
267     return DMError::DM_OK;
268 }
269 
GetScreenColorGamut(ScreenColorGamut & colorGamut)270 DMError AbstractScreen::GetScreenColorGamut(ScreenColorGamut& colorGamut)
271 {
272     auto ret = RSInterfaces::GetInstance().GetScreenColorGamut(rsId_, colorGamut);
273     if (ret != StatusCode::SUCCESS) {
274         WLOGE("GetScreenColorGamut fail! rsId %{public}" PRIu64"", rsId_);
275         return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
276     }
277     WLOGI("GetScreenColorGamut ok! rsId %{public}" PRIu64", colorGamut %{public}u",
278         rsId_, static_cast<uint32_t>(colorGamut));
279 
280     return DMError::DM_OK;
281 }
282 
SetScreenColorGamut(int32_t colorGamutIdx)283 DMError AbstractScreen::SetScreenColorGamut(int32_t colorGamutIdx)
284 {
285     std::vector<ScreenColorGamut> colorGamuts;
286     DMError res = GetScreenSupportedColorGamuts(colorGamuts);
287     if (res != DMError::DM_OK) {
288         WLOGE("SetScreenColorGamut fail! rsId %{public}" PRIu64"", rsId_);
289         return res;
290     }
291     if (colorGamutIdx < 0 || colorGamutIdx >= static_cast<int32_t>(colorGamuts.size())) {
292         WLOGE("SetScreenColorGamut fail! rsId %{public}" PRIu64" colorGamutIdx %{public}d invalid.",
293             rsId_, colorGamutIdx);
294         return DMError::DM_ERROR_INVALID_PARAM;
295     }
296     auto ret = RSInterfaces::GetInstance().SetScreenColorGamut(rsId_, colorGamutIdx);
297     if (ret != StatusCode::SUCCESS) {
298         WLOGE("SetScreenColorGamut fail! rsId %{public}" PRIu64"", rsId_);
299         return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
300     }
301     WLOGI("SetScreenColorGamut ok! rsId %{public}" PRIu64", colorGamutIdx %{public}u",
302         rsId_, colorGamutIdx);
303 
304     return DMError::DM_OK;
305 }
306 
GetScreenGamutMap(ScreenGamutMap & gamutMap)307 DMError AbstractScreen::GetScreenGamutMap(ScreenGamutMap& gamutMap)
308 {
309     auto ret = RSInterfaces::GetInstance().GetScreenGamutMap(rsId_, gamutMap);
310     if (ret != StatusCode::SUCCESS) {
311         WLOGE("GetScreenGamutMap fail! rsId %{public}" PRIu64"", rsId_);
312         return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
313     }
314     WLOGI("GetScreenGamutMap ok! rsId %{public}" PRIu64", gamutMap %{public}u",
315         rsId_, static_cast<uint32_t>(gamutMap));
316 
317     return DMError::DM_OK;
318 }
319 
SetScreenGamutMap(ScreenGamutMap gamutMap)320 DMError AbstractScreen::SetScreenGamutMap(ScreenGamutMap gamutMap)
321 {
322     if (gamutMap > GAMUT_MAP_HDR_EXTENSION) {
323         return DMError::DM_ERROR_INVALID_PARAM;
324     }
325     auto ret = RSInterfaces::GetInstance().SetScreenGamutMap(rsId_, gamutMap);
326     if (ret != StatusCode::SUCCESS) {
327         WLOGE("SetScreenGamutMap fail! rsId %{public}" PRIu64"", rsId_);
328         return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
329     }
330     WLOGI("SetScreenGamutMap ok! rsId %{public}" PRIu64", gamutMap %{public}u",
331         rsId_, static_cast<uint32_t>(gamutMap));
332 
333     return DMError::DM_OK;
334 }
335 
SetScreenColorTransform()336 DMError AbstractScreen::SetScreenColorTransform()
337 {
338     WLOGI("SetScreenColorTransform ok! rsId %{public}" PRIu64"", rsId_);
339 
340     return DMError::DM_OK;
341 }
342 
FillScreenInfo(sptr<ScreenInfo> info) const343 void AbstractScreen::FillScreenInfo(sptr<ScreenInfo> info) const
344 {
345     if (info == nullptr) {
346         WLOGE("FillScreenInfo failed! info is nullptr");
347         return;
348     }
349     info->id_ = dmsId_;
350     info->name_ = name_;
351     uint32_t width = 0;
352     uint32_t height = 0;
353     sptr<SupportedScreenModes> abstractScreenModes = GetActiveScreenMode();
354     if (abstractScreenModes != nullptr) {
355         height = abstractScreenModes->height_;
356         width = abstractScreenModes->width_;
357     }
358     float virtualPixelRatio = virtualPixelRatio_;
359     // "< 1e-6" means virtualPixelRatio is 0.
360     if (fabsf(virtualPixelRatio) < 1e-6) {
361         virtualPixelRatio = 1.0f;
362     }
363     ScreenSourceMode sourceMode = GetSourceMode();
364     info->virtualPixelRatio_ = virtualPixelRatio;
365     info->virtualHeight_ = height / virtualPixelRatio;
366     info->virtualWidth_ = width / virtualPixelRatio;
367     info->lastParent_ = lastGroupDmsId_;
368     info->parent_ = groupDmsId_;
369     info->isScreenGroup_ = isScreenGroup_;
370     info->rotation_ = rotation_;
371     info->orientation_ = orientation_;
372     info->sourceMode_ = sourceMode;
373     info->type_ = type_;
374     info->modeId_ = activeIdx_;
375     info->modes_ = modes_;
376 }
377 
SetOrientation(Orientation orientation)378 bool AbstractScreen::SetOrientation(Orientation orientation)
379 {
380     orientation_ = orientation;
381     return true;
382 }
383 
SetVirtualPixelRatio(float virtualPixelRatio)384 bool AbstractScreen::SetVirtualPixelRatio(float virtualPixelRatio)
385 {
386     virtualPixelRatio_ = virtualPixelRatio;
387     return true;
388 }
389 
GetVirtualPixelRatio() const390 float AbstractScreen::GetVirtualPixelRatio() const
391 {
392     return virtualPixelRatio_;
393 }
394 
GetSourceMode() const395 ScreenSourceMode AbstractScreen::GetSourceMode() const
396 {
397     sptr<AbstractScreenGroup> abstractScreenGroup = GetGroup();
398     if (abstractScreenGroup == nullptr || screenController_ == nullptr) {
399         return ScreenSourceMode::SCREEN_ALONE;
400     }
401     ScreenId defaultId = screenController_->GetDefaultAbstractScreenId();
402     if (dmsId_ == defaultId) {
403         return ScreenSourceMode::SCREEN_MAIN;
404     }
405     ScreenCombination combination = abstractScreenGroup->GetScreenCombination();
406     switch (combination) {
407         case ScreenCombination::SCREEN_MIRROR: {
408             return ScreenSourceMode::SCREEN_MIRROR;
409         }
410         case ScreenCombination::SCREEN_EXPAND: {
411             return ScreenSourceMode::SCREEN_EXTEND;
412         }
413         case ScreenCombination::SCREEN_ALONE: {
414             return ScreenSourceMode::SCREEN_ALONE;
415         }
416         default: {
417             return ScreenSourceMode::SCREEN_ALONE;
418         }
419     }
420 }
421 
CalcRotation(Orientation orientation) const422 Rotation AbstractScreen::CalcRotation(Orientation orientation) const
423 {
424     sptr<SupportedScreenModes> info = GetActiveScreenMode();
425     if (info == nullptr) {
426         return Rotation::ROTATION_0;
427     }
428     // vertical: phone(Plugin screen); horizontal: pad & external screen
429     bool isVerticalScreen = info->width_ < info->height_;
430     switch (orientation) {
431         case Orientation::UNSPECIFIED: {
432             return Rotation::ROTATION_0;
433         }
434         case Orientation::VERTICAL: {
435             return isVerticalScreen ? Rotation::ROTATION_0 : Rotation::ROTATION_90;
436         }
437         case Orientation::HORIZONTAL: {
438             return isVerticalScreen ? Rotation::ROTATION_90 : Rotation::ROTATION_0;
439         }
440         case Orientation::REVERSE_VERTICAL: {
441             return isVerticalScreen ? Rotation::ROTATION_180 : Rotation::ROTATION_270;
442         }
443         case Orientation::REVERSE_HORIZONTAL: {
444             return isVerticalScreen ? Rotation::ROTATION_270 : Rotation::ROTATION_180;
445         }
446         default: {
447             WLOGE("unknown orientation %{public}u", orientation);
448             return Rotation::ROTATION_0;
449         }
450     }
451 }
452 
GetScreenName() const453 const std::string& AbstractScreen::GetScreenName() const
454 {
455     return name_;
456 }
457 
SetPhyWidth(uint32_t phyWidth)458 void AbstractScreen::SetPhyWidth(uint32_t phyWidth)
459 {
460     phyWidth_ = phyWidth;
461 }
462 
SetPhyHeight(uint32_t phyHeight)463 void AbstractScreen::SetPhyHeight(uint32_t phyHeight)
464 {
465     phyHeight_ = phyHeight;
466 }
467 
GetPhyWidth() const468 uint32_t AbstractScreen::GetPhyWidth() const
469 {
470     return phyWidth_;
471 }
472 
GetPhyHeight() const473 uint32_t AbstractScreen::GetPhyHeight() const
474 {
475     return phyHeight_;
476 }
477 
AbstractScreenGroup(sptr<AbstractScreenController> screenController,ScreenId dmsId,ScreenId rsId,std::string name,ScreenCombination combination)478 AbstractScreenGroup::AbstractScreenGroup(sptr<AbstractScreenController> screenController, ScreenId dmsId, ScreenId rsId,
479     std::string name, ScreenCombination combination) : AbstractScreen(screenController, name, dmsId, rsId),
480     combination_(combination)
481 {
482     type_ = ScreenType::UNDEFINED;
483     isScreenGroup_ = true;
484 }
485 
~AbstractScreenGroup()486 AbstractScreenGroup::~AbstractScreenGroup()
487 {
488     rsDisplayNode_ = nullptr;
489     abstractScreenMap_.clear();
490 }
491 
ConvertToScreenGroupInfo() const492 sptr<ScreenGroupInfo> AbstractScreenGroup::ConvertToScreenGroupInfo() const
493 {
494     sptr<ScreenGroupInfo> screenGroupInfo = new(std::nothrow) ScreenGroupInfo();
495     if (screenGroupInfo == nullptr) {
496         return nullptr;
497     }
498     FillScreenInfo(screenGroupInfo);
499     screenGroupInfo->combination_ = combination_;
500     for (auto iter = abstractScreenMap_.begin(); iter != abstractScreenMap_.end(); iter++) {
501         screenGroupInfo->children_.push_back(iter->first);
502     }
503     auto positions = GetChildrenPosition();
504     screenGroupInfo->position_.insert(screenGroupInfo->position_.end(), positions.begin(), positions.end());
505     return screenGroupInfo;
506 }
507 
GetRSDisplayNodeConfig(sptr<AbstractScreen> & dmsScreen,struct RSDisplayNodeConfig & config)508 bool AbstractScreenGroup::GetRSDisplayNodeConfig(sptr<AbstractScreen>& dmsScreen, struct RSDisplayNodeConfig& config)
509 {
510     if (dmsScreen == nullptr) {
511         WLOGE("dmsScreen is nullptr.");
512         return false;
513     }
514     config = { dmsScreen->rsId_ };
515     switch (combination_) {
516         case ScreenCombination::SCREEN_ALONE:
517             [[fallthrough]];
518         case ScreenCombination::SCREEN_EXPAND:
519             break;
520         case ScreenCombination::SCREEN_MIRROR: {
521             if (GetChildCount() == 0 || mirrorScreenId_ == dmsScreen->dmsId_) {
522                 WLOGI("AddChild, SCREEN_MIRROR, config is not mirror");
523                 break;
524             }
525             if (screenController_ == nullptr) {
526                 return false;
527             }
528             if (mirrorScreenId_ == SCREEN_ID_INVALID || !HasChild(mirrorScreenId_)) {
529                 WLOGI("AddChild, mirrorScreenId_ is invalid, use default screen");
530                 mirrorScreenId_ = screenController_->GetDefaultAbstractScreenId();
531             }
532             // Todo displayNode is nullptr
533             std::shared_ptr<RSDisplayNode> displayNode = screenController_->GetRSDisplayNodeByScreenId(mirrorScreenId_);
534             if (displayNode == nullptr) {
535                 WLOGFE("AddChild fail, displayNode is nullptr, cannot get DisplayNode");
536                 break;
537             }
538             NodeId nodeId = displayNode->GetId();
539             WLOGI("AddChild, mirrorScreenId_:%{public}" PRIu64", rsId_:%{public}" PRIu64", nodeId:%{public}" PRIu64"",
540                 mirrorScreenId_, dmsScreen->rsId_, nodeId);
541             config = {dmsScreen->rsId_, true, nodeId};
542             break;
543         }
544         default:
545             WLOGE("fail to add child. invalid group combination:%{public}u", combination_);
546             return false;
547     }
548     return true;
549 }
550 
AddChild(sptr<AbstractScreen> & dmsScreen,Point & startPoint)551 bool AbstractScreenGroup::AddChild(sptr<AbstractScreen>& dmsScreen, Point& startPoint)
552 {
553     if (dmsScreen == nullptr) {
554         WLOGE("AddChild, dmsScreen is nullptr.");
555         return false;
556     }
557     ScreenId screenId = dmsScreen->dmsId_;
558     WLOGFD("AbstractScreenGroup AddChild dmsScreenId: %{public}" PRIu64"", screenId);
559     auto iter = abstractScreenMap_.find(screenId);
560     if (iter != abstractScreenMap_.end()) {
561         if (dmsScreen->rsDisplayNode_ != nullptr && dmsScreen->type_ == ScreenType::REAL &&
562             defaultScreenId_ == screenId) {
563             WLOGFD("Add default screen, id: %{public}" PRIu64"", screenId);
564         } else {
565             WLOGE("AddChild, abstractScreenMap_ has dmsScreen:%{public}" PRIu64"", screenId);
566             return false;
567         }
568     }
569     struct RSDisplayNodeConfig config;
570     if (!GetRSDisplayNodeConfig(dmsScreen, config)) {
571         return false;
572     }
573     if (dmsScreen->rsDisplayNode_ != nullptr && dmsScreen->type_ == ScreenType::REAL &&
574         defaultScreenId_ == screenId) {
575         WLOGFD("Reconnect default screen, screenId: %{public}" PRIu64"", screenId);
576         dmsScreen->InitRSDefaultDisplayNode(config, startPoint);
577     } else {
578         dmsScreen->InitRSDisplayNode(config, startPoint);
579         dmsScreen->lastGroupDmsId_ = dmsScreen->groupDmsId_;
580         dmsScreen->groupDmsId_ = dmsId_;
581         abstractScreenMap_.insert(std::make_pair(screenId, std::make_pair(dmsScreen, startPoint)));
582     }
583     return true;
584 }
585 
AddChildren(std::vector<sptr<AbstractScreen>> & dmsScreens,std::vector<Point> & startPoints)586 bool AbstractScreenGroup::AddChildren(std::vector<sptr<AbstractScreen>>& dmsScreens, std::vector<Point>& startPoints)
587 {
588     size_t size = dmsScreens.size();
589     if (size != startPoints.size()) {
590         WLOGE("AddChildren, unequal size.");
591         return false;
592     }
593     bool res = true;
594     for (size_t i = 0; i < size; i++) {
595         res = AddChild(dmsScreens[i], startPoints[i]) && res;
596     }
597     return res;
598 }
599 
RemoveChild(sptr<AbstractScreen> & dmsScreen)600 bool AbstractScreenGroup::RemoveChild(sptr<AbstractScreen>& dmsScreen)
601 {
602     if (dmsScreen == nullptr) {
603         WLOGE("RemoveChild, dmsScreen is nullptr.");
604         return false;
605     }
606     ScreenId screenId = dmsScreen->dmsId_;
607     dmsScreen->lastGroupDmsId_ = dmsScreen->groupDmsId_;
608     dmsScreen->groupDmsId_ = SCREEN_ID_INVALID;
609     if (dmsScreen->rsDisplayNode_ != nullptr) {
610         dmsScreen->rsDisplayNode_->SetDisplayOffset(0, 0);
611         dmsScreen->rsDisplayNode_->RemoveFromTree();
612         auto transactionProxy = RSTransactionProxy::GetInstance();
613         if (transactionProxy != nullptr) {
614             transactionProxy->FlushImplicitTransaction();
615         }
616         dmsScreen->rsDisplayNode_ = nullptr;
617     }
618     WLOGFD("groupDmsId:%{public}" PRIu64", screenId:%{public}" PRIu64"",
619         dmsScreen->groupDmsId_, screenId);
620     return abstractScreenMap_.erase(screenId);
621 }
622 
RemoveDefaultScreen(const sptr<AbstractScreen> & dmsScreen)623 bool AbstractScreenGroup::RemoveDefaultScreen(const sptr<AbstractScreen>& dmsScreen)
624 {
625     if (dmsScreen == nullptr) {
626         WLOGE("RemoveChild, dmsScreen is nullptr.");
627         return false;
628     }
629     ScreenId screenId = dmsScreen->dmsId_;
630     dmsScreen->lastGroupDmsId_ = dmsScreen->groupDmsId_;
631     if (dmsScreen->rsDisplayNode_ != nullptr) {
632         dmsScreen->rsDisplayNode_->SetDisplayOffset(0, 0);
633         dmsScreen->rsDisplayNode_->RemoveFromTree();
634         auto transactionProxy = RSTransactionProxy::GetInstance();
635         if (transactionProxy != nullptr) {
636             transactionProxy->FlushImplicitTransaction();
637         }
638     }
639     defaultScreenId_ = screenId;
640     WLOGFD("groupDmsId:%{public}" PRIu64", screenId:%{public}" PRIu64"",
641         dmsScreen->groupDmsId_, screenId);
642     return true;
643 }
644 
HasChild(ScreenId childScreen) const645 bool AbstractScreenGroup::HasChild(ScreenId childScreen) const
646 {
647     return abstractScreenMap_.find(childScreen) != abstractScreenMap_.end();
648 }
649 
GetChildren() const650 std::vector<sptr<AbstractScreen>> AbstractScreenGroup::GetChildren() const
651 {
652     std::vector<sptr<AbstractScreen>> res;
653     for (auto iter = abstractScreenMap_.begin(); iter != abstractScreenMap_.end(); iter++) {
654         res.push_back(iter->second.first);
655     }
656     return res;
657 }
658 
GetChildrenPosition() const659 std::vector<Point> AbstractScreenGroup::GetChildrenPosition() const
660 {
661     std::vector<Point> res;
662     for (auto iter = abstractScreenMap_.begin(); iter != abstractScreenMap_.end(); iter++) {
663         res.push_back(iter->second.second);
664     }
665     return res;
666 }
667 
GetChildPosition(ScreenId screenId) const668 Point AbstractScreenGroup::GetChildPosition(ScreenId screenId) const
669 {
670     Point point;
671     auto iter = abstractScreenMap_.find(screenId);
672     if (iter != abstractScreenMap_.end()) {
673         point = iter->second.second;
674     }
675     return point;
676 }
677 
GetChildCount() const678 size_t AbstractScreenGroup::GetChildCount() const
679 {
680     return abstractScreenMap_.size();
681 }
682 
GetScreenCombination() const683 ScreenCombination AbstractScreenGroup::GetScreenCombination() const
684 {
685     return combination_;
686 }
687 } // namespace OHOS::Rosen
688