• 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 }
28 
AbstractScreen(sptr<AbstractScreenController> screenController,const std::string & name,ScreenId dmsId,ScreenId rsId)29 AbstractScreen::AbstractScreen(sptr<AbstractScreenController> screenController, const std::string& name, ScreenId dmsId,
30     ScreenId rsId) : name_(name), dmsId_(dmsId), rsId_(rsId), screenController_(screenController)
31 {
32 }
33 
~AbstractScreen()34 AbstractScreen::~AbstractScreen()
35 {
36 }
37 
GetActiveScreenMode() const38 sptr<SupportedScreenModes> AbstractScreen::GetActiveScreenMode() const
39 {
40     if (activeIdx_ < 0 || activeIdx_ >= static_cast<int32_t>(modes_.size())) {
41         WLOGE("active mode index is wrong: %{public}d", activeIdx_);
42         return nullptr;
43     }
44     return modes_[activeIdx_];
45 }
46 
GetAbstractScreenModes() const47 std::vector<sptr<SupportedScreenModes>> AbstractScreen::GetAbstractScreenModes() const
48 {
49     return modes_;
50 }
51 
GetGroup() const52 sptr<AbstractScreenGroup> AbstractScreen::GetGroup() const
53 {
54     return screenController_->GetAbstractScreenGroup(groupDmsId_);
55 }
56 
ConvertToScreenInfo() const57 sptr<ScreenInfo> AbstractScreen::ConvertToScreenInfo() const
58 {
59     sptr<ScreenInfo> info = new(std::nothrow) ScreenInfo();
60     if (info == nullptr) {
61         return nullptr;
62     }
63     FillScreenInfo(info);
64     return info;
65 }
66 
UpdateRSTree(std::shared_ptr<RSSurfaceNode> & surfaceNode,bool isAdd)67 void AbstractScreen::UpdateRSTree(std::shared_ptr<RSSurfaceNode>& surfaceNode, bool isAdd)
68 {
69     if (rsDisplayNode_ == nullptr || surfaceNode == nullptr) {
70         WLOGFE("node is nullptr");
71         return;
72     }
73     WLOGFI("%{public}s surface: %{public}s, %{public}" PRIu64"", (isAdd ? "add" : "remove"),
74         surfaceNode->GetName().c_str(), surfaceNode->GetId());
75 
76     if (isAdd) {
77         surfaceNode->SetVisible(true);
78         rsDisplayNode_->AddChild(surfaceNode, -1);
79     } else {
80         rsDisplayNode_->RemoveChild(surfaceNode);
81     }
82 }
83 
UpdateDisplayGroupRSTree(std::shared_ptr<RSSurfaceNode> & surfaceNode,NodeId parentNodeId,bool isAdd)84 void AbstractScreen::UpdateDisplayGroupRSTree(std::shared_ptr<RSSurfaceNode>& surfaceNode, NodeId parentNodeId,
85     bool isAdd)
86 {
87     if (rsDisplayNode_ == nullptr || surfaceNode == nullptr) {
88         WLOGFE("node is nullptr");
89         return;
90     }
91     WLOGFI("%{public}s surface: %{public}s, %{public}" PRIu64"", (isAdd ? "add" : "remove"),
92         surfaceNode->GetName().c_str(), surfaceNode->GetId());
93 
94     if (isAdd) {
95         surfaceNode->SetVisible(true);
96         rsDisplayNode_->AddCrossParentChild(surfaceNode, -1);
97     } else {
98         rsDisplayNode_->RemoveCrossParentChild(surfaceNode, parentNodeId);
99     }
100 }
101 
InitRSDisplayNode(RSDisplayNodeConfig & config,Point & startPoint)102 void AbstractScreen::InitRSDisplayNode(RSDisplayNodeConfig& config, Point& startPoint)
103 {
104     if (rsDisplayNode_ != nullptr) {
105         rsDisplayNode_->SetDisplayNodeMirrorConfig(config);
106     } else {
107         std::shared_ptr<RSDisplayNode> rsDisplayNode = RSDisplayNode::Create(config);
108         if (rsDisplayNode == nullptr) {
109             WLOGE("fail to add child. create rsDisplayNode fail!");
110             return;
111         }
112         rsDisplayNode_ = rsDisplayNode;
113     }
114     rSDisplayNodeConfig_ = config;
115     WLOGFI("SetDisplayOffset: posX:%{public}d, posY:%{public}d", startPoint.posX_, startPoint.posY_);
116     rsDisplayNode_->SetDisplayOffset(startPoint.posX_, startPoint.posY_);
117     uint32_t width = 0;
118     uint32_t height = 0;
119     sptr<SupportedScreenModes> abstractScreenModes = GetActiveScreenMode();
120     if (abstractScreenModes != nullptr) {
121         height = abstractScreenModes->height_;
122         width = abstractScreenModes->width_;
123     }
124     RSScreenType screenType;
125     auto ret = RSInterfaces::GetInstance().GetScreenType(rsId_, screenType);
126     if (ret == StatusCode::SUCCESS && screenType == RSScreenType::VIRTUAL_TYPE_SCREEN) {
127         rsDisplayNode_->SetSecurityDisplay(true);
128         WLOGFI("virtualScreen SetSecurityDisplay success");
129     }
130     // If setDisplayOffset is not valid for SetFrame/SetBounds
131     rsDisplayNode_->SetFrame(0, 0, width, height);
132     rsDisplayNode_->SetBounds(0, 0, width, height);
133     auto transactionProxy = RSTransactionProxy::GetInstance();
134     if (transactionProxy != nullptr) {
135         transactionProxy->FlushImplicitTransaction();
136     }
137 }
138 
GetScreenGroupId() const139 ScreenId AbstractScreen::GetScreenGroupId() const
140 {
141     return groupDmsId_;
142 }
143 
GetScreenSupportedColorGamuts(std::vector<ScreenColorGamut> & colorGamuts)144 DMError AbstractScreen::GetScreenSupportedColorGamuts(std::vector<ScreenColorGamut>& colorGamuts)
145 {
146     auto ret = RSInterfaces::GetInstance().GetScreenSupportedColorGamuts(rsId_, colorGamuts);
147     if (ret != StatusCode::SUCCESS) {
148         WLOGE("GetScreenSupportedColorGamuts fail! rsId %{public}" PRIu64"", rsId_);
149         return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
150     }
151     WLOGI("GetScreenSupportedColorGamuts ok! rsId %{public}" PRIu64", size %{public}u",
152         rsId_, static_cast<uint32_t>(colorGamuts.size()));
153 
154     return DMError::DM_OK;
155 }
156 
GetScreenColorGamut(ScreenColorGamut & colorGamut)157 DMError AbstractScreen::GetScreenColorGamut(ScreenColorGamut& colorGamut)
158 {
159     auto ret = RSInterfaces::GetInstance().GetScreenColorGamut(rsId_, colorGamut);
160     if (ret != StatusCode::SUCCESS) {
161         WLOGE("GetScreenColorGamut fail! rsId %{public}" PRIu64"", rsId_);
162         return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
163     }
164     WLOGI("GetScreenColorGamut ok! rsId %{public}" PRIu64", colorGamut %{public}u",
165         rsId_, static_cast<uint32_t>(colorGamut));
166 
167     return DMError::DM_OK;
168 }
169 
SetScreenColorGamut(int32_t colorGamutIdx)170 DMError AbstractScreen::SetScreenColorGamut(int32_t colorGamutIdx)
171 {
172     std::vector<ScreenColorGamut> colorGamuts;
173     DMError res = GetScreenSupportedColorGamuts(colorGamuts);
174     if (res != DMError::DM_OK) {
175         WLOGE("SetScreenColorGamut fail! rsId %{public}" PRIu64"", rsId_);
176         return res;
177     }
178     if (colorGamutIdx < 0 || colorGamutIdx >= static_cast<int32_t>(colorGamuts.size())) {
179         WLOGE("SetScreenColorGamut fail! rsId %{public}" PRIu64" colorGamutIdx %{public}d invalid.",
180             rsId_, colorGamutIdx);
181         return DMError::DM_ERROR_INVALID_PARAM;
182     }
183     auto ret = RSInterfaces::GetInstance().SetScreenColorGamut(rsId_, colorGamutIdx);
184     if (ret != StatusCode::SUCCESS) {
185         WLOGE("SetScreenColorGamut fail! rsId %{public}" PRIu64"", rsId_);
186         return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
187     }
188     WLOGI("SetScreenColorGamut ok! rsId %{public}" PRIu64", colorGamutIdx %{public}u",
189         rsId_, colorGamutIdx);
190 
191     return DMError::DM_OK;
192 }
193 
GetScreenGamutMap(ScreenGamutMap & gamutMap)194 DMError AbstractScreen::GetScreenGamutMap(ScreenGamutMap& gamutMap)
195 {
196     auto ret = RSInterfaces::GetInstance().GetScreenGamutMap(rsId_, gamutMap);
197     if (ret != StatusCode::SUCCESS) {
198         WLOGE("GetScreenGamutMap fail! rsId %{public}" PRIu64"", rsId_);
199         return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
200     }
201     WLOGI("GetScreenGamutMap ok! rsId %{public}" PRIu64", gamutMap %{public}u",
202         rsId_, static_cast<uint32_t>(gamutMap));
203 
204     return DMError::DM_OK;
205 }
206 
SetScreenGamutMap(ScreenGamutMap gamutMap)207 DMError AbstractScreen::SetScreenGamutMap(ScreenGamutMap gamutMap)
208 {
209     if (gamutMap > GAMUT_MAP_HDR_EXTENSION) {
210         return DMError::DM_ERROR_INVALID_PARAM;
211     }
212     auto ret = RSInterfaces::GetInstance().SetScreenGamutMap(rsId_, gamutMap);
213     if (ret != StatusCode::SUCCESS) {
214         WLOGE("SetScreenGamutMap fail! rsId %{public}" PRIu64"", rsId_);
215         return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
216     }
217     WLOGI("SetScreenGamutMap ok! rsId %{public}" PRIu64", gamutMap %{public}u",
218         rsId_, static_cast<uint32_t>(gamutMap));
219 
220     return DMError::DM_OK;
221 }
222 
SetScreenColorTransform()223 DMError AbstractScreen::SetScreenColorTransform()
224 {
225     WLOGI("SetScreenColorTransform ok! rsId %{public}" PRIu64"", rsId_);
226 
227     return DMError::DM_OK;
228 }
229 
FillScreenInfo(sptr<ScreenInfo> info) const230 void AbstractScreen::FillScreenInfo(sptr<ScreenInfo> info) const
231 {
232     if (info == nullptr) {
233         WLOGE("FillScreenInfo failed! info is nullptr");
234         return;
235     }
236     info->id_ = dmsId_;
237     info->name_ = name_;
238     uint32_t width = 0;
239     uint32_t height = 0;
240     sptr<SupportedScreenModes> abstractScreenModes = GetActiveScreenMode();
241     if (abstractScreenModes != nullptr) {
242         height = abstractScreenModes->height_;
243         width = abstractScreenModes->width_;
244     }
245     float virtualPixelRatio = virtualPixelRatio_;
246     // "< 1e-6" means virtualPixelRatio is 0.
247     if (fabsf(virtualPixelRatio) < 1e-6) {
248         virtualPixelRatio = 1.0f;
249     }
250     info->virtualPixelRatio_ = virtualPixelRatio;
251     info->virtualHeight_ = height / virtualPixelRatio;
252     info->virtualWidth_ = width / virtualPixelRatio;
253     info->lastParent_ = lastGroupDmsId_;
254     info->parent_ = groupDmsId_;
255     info->isScreenGroup_ = isScreenGroup_;
256     info->rotation_ = rotation_;
257     info->orientation_ = orientation_;
258     info->type_ = type_;
259     info->modeId_ = activeIdx_;
260     info->modes_ = modes_;
261 }
262 
SetOrientation(Orientation orientation)263 bool AbstractScreen::SetOrientation(Orientation orientation)
264 {
265     orientation_ = orientation;
266     return true;
267 }
268 
SetVirtualPixelRatio(float virtualPixelRatio)269 bool AbstractScreen::SetVirtualPixelRatio(float virtualPixelRatio)
270 {
271     virtualPixelRatio_ = virtualPixelRatio;
272     return true;
273 }
274 
GetVirtualPixelRatio() const275 float AbstractScreen::GetVirtualPixelRatio() const
276 {
277     return virtualPixelRatio_;
278 }
279 
CalcRotation(Orientation orientation) const280 Rotation AbstractScreen::CalcRotation(Orientation orientation) const
281 {
282     sptr<SupportedScreenModes> info = GetActiveScreenMode();
283     if (info == nullptr) {
284         return Rotation::ROTATION_0;
285     }
286     // vertical: phone(Plugin screen); horizontal: pad & external screen
287     bool isVerticalScreen = info->width_ < info->height_;
288     switch (orientation) {
289         case Orientation::UNSPECIFIED: {
290             return Rotation::ROTATION_0;
291         }
292         case Orientation::VERTICAL: {
293             return isVerticalScreen ? Rotation::ROTATION_0 : Rotation::ROTATION_90;
294         }
295         case Orientation::HORIZONTAL: {
296             return isVerticalScreen ? Rotation::ROTATION_90 : Rotation::ROTATION_0;
297         }
298         case Orientation::REVERSE_VERTICAL: {
299             return isVerticalScreen ? Rotation::ROTATION_180 : Rotation::ROTATION_270;
300         }
301         case Orientation::REVERSE_HORIZONTAL: {
302             return isVerticalScreen ? Rotation::ROTATION_270 : Rotation::ROTATION_180;
303         }
304         default: {
305             WLOGE("unknown orientation %{public}u", orientation);
306             return Rotation::ROTATION_0;
307         }
308     }
309 }
310 
AbstractScreenGroup(sptr<AbstractScreenController> screenController,ScreenId dmsId,ScreenId rsId,std::string name,ScreenCombination combination)311 AbstractScreenGroup::AbstractScreenGroup(sptr<AbstractScreenController> screenController, ScreenId dmsId, ScreenId rsId,
312     std::string name, ScreenCombination combination) : AbstractScreen(screenController, name, dmsId, rsId),
313     combination_(combination)
314 {
315     type_ = ScreenType::UNDEFINED;
316     isScreenGroup_ = true;
317 }
318 
~AbstractScreenGroup()319 AbstractScreenGroup::~AbstractScreenGroup()
320 {
321     rsDisplayNode_ = nullptr;
322     abstractScreenMap_.clear();
323 }
324 
ConvertToScreenGroupInfo() const325 sptr<ScreenGroupInfo> AbstractScreenGroup::ConvertToScreenGroupInfo() const
326 {
327     sptr<ScreenGroupInfo> screenGroupInfo = new(std::nothrow) ScreenGroupInfo();
328     if (screenGroupInfo == nullptr) {
329         return nullptr;
330     }
331     FillScreenInfo(screenGroupInfo);
332     screenGroupInfo->combination_ = combination_;
333     for (auto iter = abstractScreenMap_.begin(); iter != abstractScreenMap_.end(); iter++) {
334         screenGroupInfo->children_.push_back(iter->first);
335     }
336     auto positions = GetChildrenPosition();
337     screenGroupInfo->position_.insert(screenGroupInfo->position_.end(), positions.begin(), positions.end());
338     return screenGroupInfo;
339 }
340 
GetRSDisplayNodeConfig(sptr<AbstractScreen> & dmsScreen,struct RSDisplayNodeConfig & config)341 bool AbstractScreenGroup::GetRSDisplayNodeConfig(sptr<AbstractScreen>& dmsScreen, struct RSDisplayNodeConfig& config)
342 {
343     if (dmsScreen == nullptr) {
344         WLOGE("dmsScreen is nullptr.");
345         return false;
346     }
347     config = { dmsScreen->rsId_ };
348     switch (combination_) {
349         case ScreenCombination::SCREEN_ALONE:
350             [[fallthrough]];
351         case ScreenCombination::SCREEN_EXPAND:
352             break;
353         case ScreenCombination::SCREEN_MIRROR: {
354             if (GetChildCount() == 0 || mirrorScreenId_ == dmsScreen->dmsId_) {
355                 WLOGI("AddChild, SCREEN_MIRROR, config is not mirror");
356                 break;
357             }
358             if (mirrorScreenId_ == SCREEN_ID_INVALID || !HasChild(mirrorScreenId_)) {
359                 WLOGI("AddChild, mirrorScreenId_ is invalid, use default screen");
360                 mirrorScreenId_ = screenController_->GetDefaultAbstractScreenId();
361             }
362             // Todo displayNode is nullptr
363             std::shared_ptr<RSDisplayNode> displayNode = screenController_->GetRSDisplayNodeByScreenId(mirrorScreenId_);
364             if (displayNode == nullptr) {
365                 WLOGFE("AddChild fail, displayNode is nullptr, cannot get DisplayNode");
366                 break;
367             }
368             NodeId nodeId = displayNode->GetId();
369             WLOGI("AddChild, mirrorScreenId_:%{public}" PRIu64", rsId_:%{public}" PRIu64", nodeId:%{public}" PRIu64"",
370                 mirrorScreenId_, dmsScreen->rsId_, nodeId);
371             config = {dmsScreen->rsId_, true, nodeId};
372             break;
373         }
374         default:
375             WLOGE("fail to add child. invalid group combination:%{public}u", combination_);
376             return false;
377     }
378     return true;
379 }
380 
AddChild(sptr<AbstractScreen> & dmsScreen,Point & startPoint)381 bool AbstractScreenGroup::AddChild(sptr<AbstractScreen>& dmsScreen, Point& startPoint)
382 {
383     if (dmsScreen == nullptr) {
384         WLOGE("AddChild, dmsScreen is nullptr.");
385         return false;
386     }
387     ScreenId screenId = dmsScreen->dmsId_;
388     auto iter = abstractScreenMap_.find(screenId);
389     if (iter != abstractScreenMap_.end()) {
390         WLOGE("AddChild, abstractScreenMap_ has dmsScreen:%{public}" PRIu64"", screenId);
391         return false;
392     }
393     struct RSDisplayNodeConfig config;
394     if (!GetRSDisplayNodeConfig(dmsScreen, config)) {
395         return false;
396     }
397     dmsScreen->InitRSDisplayNode(config, startPoint);
398     dmsScreen->lastGroupDmsId_ = dmsScreen->groupDmsId_;
399     dmsScreen->groupDmsId_ = dmsId_;
400     abstractScreenMap_.insert(std::make_pair(screenId, std::make_pair(dmsScreen, startPoint)));
401     return true;
402 }
403 
AddChildren(std::vector<sptr<AbstractScreen>> & dmsScreens,std::vector<Point> & startPoints)404 bool AbstractScreenGroup::AddChildren(std::vector<sptr<AbstractScreen>>& dmsScreens, std::vector<Point>& startPoints)
405 {
406     size_t size = dmsScreens.size();
407     if (size != startPoints.size()) {
408         WLOGE("AddChildren, unequal size.");
409         return false;
410     }
411     bool res = true;
412     for (size_t i = 0; i < size; i++) {
413         res = AddChild(dmsScreens[i], startPoints[i]) && res;
414     }
415     return res;
416 }
417 
RemoveChild(sptr<AbstractScreen> & dmsScreen)418 bool AbstractScreenGroup::RemoveChild(sptr<AbstractScreen>& dmsScreen)
419 {
420     if (dmsScreen == nullptr) {
421         WLOGE("RemoveChild, dmsScreen is nullptr.");
422         return false;
423     }
424     ScreenId screenId = dmsScreen->dmsId_;
425     dmsScreen->lastGroupDmsId_ = dmsScreen->groupDmsId_;
426     dmsScreen->groupDmsId_ = SCREEN_ID_INVALID;
427     if (dmsScreen->rsDisplayNode_ != nullptr) {
428         dmsScreen->rsDisplayNode_->SetDisplayOffset(0, 0);
429         dmsScreen->rsDisplayNode_->RemoveFromTree();
430         auto transactionProxy = RSTransactionProxy::GetInstance();
431         if (transactionProxy != nullptr) {
432             transactionProxy->FlushImplicitTransaction();
433         }
434         dmsScreen->rsDisplayNode_ = nullptr;
435     }
436     return abstractScreenMap_.erase(screenId);
437 }
438 
HasChild(ScreenId childScreen) const439 bool AbstractScreenGroup::HasChild(ScreenId childScreen) const
440 {
441     return abstractScreenMap_.find(childScreen) != abstractScreenMap_.end();
442 }
443 
GetChildren() const444 std::vector<sptr<AbstractScreen>> AbstractScreenGroup::GetChildren() const
445 {
446     std::vector<sptr<AbstractScreen>> res;
447     for (auto iter = abstractScreenMap_.begin(); iter != abstractScreenMap_.end(); iter++) {
448         res.push_back(iter->second.first);
449     }
450     return res;
451 }
452 
GetChildrenPosition() const453 std::vector<Point> AbstractScreenGroup::GetChildrenPosition() const
454 {
455     std::vector<Point> res;
456     for (auto iter = abstractScreenMap_.begin(); iter != abstractScreenMap_.end(); iter++) {
457         res.push_back(iter->second.second);
458     }
459     return res;
460 }
461 
GetChildPosition(ScreenId screenId) const462 Point AbstractScreenGroup::GetChildPosition(ScreenId screenId) const
463 {
464     Point point;
465     auto iter = abstractScreenMap_.find(screenId);
466     if (iter != abstractScreenMap_.end()) {
467         point = iter->second.second;
468     }
469     return point;
470 }
471 
GetChildCount() const472 size_t AbstractScreenGroup::GetChildCount() const
473 {
474     return abstractScreenMap_.size();
475 }
476 } // namespace OHOS::Rosen
477