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_ >= 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) {
70 WLOGFE("rsDisplayNode_ is nullptr");
71 return;
72 }
73 WLOGFI("AbstractScreen::UpdateRSTree");
74
75 if (isAdd) {
76 rsDisplayNode_->AddChild(surfaceNode, -1);
77 } else {
78 rsDisplayNode_->RemoveChild(surfaceNode);
79 }
80 }
81
InitRSDisplayNode(RSDisplayNodeConfig & config)82 void AbstractScreen::InitRSDisplayNode(RSDisplayNodeConfig& config)
83 {
84 std::shared_ptr<RSDisplayNode> rsDisplayNode = RSDisplayNode::Create(config);
85 if (rsDisplayNode == nullptr) {
86 WLOGE("fail to add child. create rsDisplayNode fail!");
87 return;
88 }
89 rsDisplayNode_ = rsDisplayNode;
90 rSDisplayNodeConfig_ = config;
91 auto transactionProxy = RSTransactionProxy::GetInstance();
92 if (transactionProxy != nullptr) {
93 transactionProxy->FlushImplicitTransaction();
94 }
95 }
96
GetScreenSupportedColorGamuts(std::vector<ScreenColorGamut> & colorGamuts)97 DMError AbstractScreen::GetScreenSupportedColorGamuts(std::vector<ScreenColorGamut>& colorGamuts)
98 {
99 auto ret = RSInterfaces::GetInstance().GetScreenSupportedColorGamuts(rsId_, colorGamuts);
100 if (ret != StatusCode::SUCCESS) {
101 WLOGE("GetScreenSupportedColorGamuts fail! rsId %{public}" PRIu64"", rsId_);
102 return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
103 }
104 WLOGI("GetScreenSupportedColorGamuts ok! rsId %{public}" PRIu64", size %{public}u",
105 rsId_, static_cast<uint32_t>(colorGamuts.size()));
106
107 return DMError::DM_OK;
108 }
109
GetScreenColorGamut(ScreenColorGamut & colorGamut)110 DMError AbstractScreen::GetScreenColorGamut(ScreenColorGamut& colorGamut)
111 {
112 auto ret = RSInterfaces::GetInstance().GetScreenColorGamut(rsId_, colorGamut);
113 if (ret != StatusCode::SUCCESS) {
114 WLOGE("GetScreenColorGamut fail! rsId %{public}" PRIu64"", rsId_);
115 return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
116 }
117 WLOGI("GetScreenColorGamut ok! rsId %{public}" PRIu64", colorGamut %{public}u",
118 rsId_, static_cast<uint32_t>(colorGamut));
119
120 return DMError::DM_OK;
121 }
122
SetScreenColorGamut(int32_t colorGamutIdx)123 DMError AbstractScreen::SetScreenColorGamut(int32_t colorGamutIdx)
124 {
125 std::vector<ScreenColorGamut> colorGamuts;
126 DMError res = GetScreenSupportedColorGamuts(colorGamuts);
127 if (res != DMError::DM_OK) {
128 WLOGE("SetScreenColorGamut fail! rsId %{public}" PRIu64"", rsId_);
129 return res;
130 }
131 if (colorGamutIdx < 0 || colorGamutIdx >= static_cast<int32_t>(colorGamuts.size())) {
132 WLOGE("SetScreenColorGamut fail! rsId %{public}" PRIu64" colorGamutIdx %{public}d invalid.",
133 rsId_, colorGamutIdx);
134 return DMError::DM_ERROR_INVALID_PARAM;
135 }
136 auto ret = RSInterfaces::GetInstance().SetScreenColorGamut(rsId_, colorGamutIdx);
137 if (ret != StatusCode::SUCCESS) {
138 WLOGE("SetScreenColorGamut fail! rsId %{public}" PRIu64"", rsId_);
139 return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
140 }
141 WLOGI("SetScreenColorGamut ok! rsId %{public}" PRIu64", colorGamutIdx %{public}u",
142 rsId_, colorGamutIdx);
143
144 return DMError::DM_OK;
145 }
146
GetScreenGamutMap(ScreenGamutMap & gamutMap)147 DMError AbstractScreen::GetScreenGamutMap(ScreenGamutMap& gamutMap)
148 {
149 auto ret = RSInterfaces::GetInstance().GetScreenGamutMap(rsId_, gamutMap);
150 if (ret != StatusCode::SUCCESS) {
151 WLOGE("GetScreenGamutMap fail! rsId %{public}" PRIu64"", rsId_);
152 return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
153 }
154 WLOGI("GetScreenGamutMap ok! rsId %{public}" PRIu64", gamutMap %{public}u",
155 rsId_, static_cast<uint32_t>(gamutMap));
156
157 return DMError::DM_OK;
158 }
159
SetScreenGamutMap(ScreenGamutMap gamutMap)160 DMError AbstractScreen::SetScreenGamutMap(ScreenGamutMap gamutMap)
161 {
162 if (gamutMap > GAMUT_MAP_HDR_EXTENSION) {
163 return DMError::DM_ERROR_INVALID_PARAM;
164 }
165 auto ret = RSInterfaces::GetInstance().SetScreenGamutMap(rsId_, gamutMap);
166 if (ret != StatusCode::SUCCESS) {
167 WLOGE("SetScreenGamutMap fail! rsId %{public}" PRIu64"", rsId_);
168 return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
169 }
170 WLOGI("SetScreenGamutMap ok! rsId %{public}" PRIu64", gamutMap %{public}u",
171 rsId_, static_cast<uint32_t>(gamutMap));
172
173 return DMError::DM_OK;
174 }
175
SetScreenColorTransform()176 DMError AbstractScreen::SetScreenColorTransform()
177 {
178 WLOGI("SetScreenColorTransform ok! rsId %{public}" PRIu64"", rsId_);
179
180 return DMError::DM_OK;
181 }
182
FillScreenInfo(sptr<ScreenInfo> info) const183 void AbstractScreen::FillScreenInfo(sptr<ScreenInfo> info) const
184 {
185 info->id_ = dmsId_;
186 uint32_t width = 0;
187 uint32_t height = 0;
188 if (activeIdx_ >= 0 && activeIdx_ < modes_.size()) {
189 sptr<SupportedScreenModes> abstractScreenModes = modes_[activeIdx_];
190 height = abstractScreenModes->height_;
191 width = abstractScreenModes->width_;
192 }
193 float virtualPixelRatio = virtualPixelRatio_;
194 // "< 1e-6" means virtualPixelRatio is 0.
195 if (fabsf(virtualPixelRatio) < 1e-6) {
196 virtualPixelRatio = 1.0f;
197 }
198 info->name_ = name_;
199 info->virtualPixelRatio_ = virtualPixelRatio;
200 info->virtualHeight_ = height / virtualPixelRatio;
201 info->virtualWidth_ = width / virtualPixelRatio;
202 info->parent_ = groupDmsId_;
203 info->isScreenGroup_ = isScreenGroup_;
204 info->rotation_ = rotation_;
205 info->orientation_ = orientation_;
206 info->type_ = type_;
207 info->modeId_ = activeIdx_;
208 info->modes_ = modes_;
209 }
210
SetOrientation(Orientation orientation)211 bool AbstractScreen::SetOrientation(Orientation orientation)
212 {
213 orientation_ = orientation;
214 return true;
215 }
216
CalcRotation(Orientation orientation) const217 Rotation AbstractScreen::CalcRotation(Orientation orientation) const
218 {
219 if (activeIdx_ < 0 || activeIdx_ >= modes_.size()) {
220 WLOGE("active mode index is wrong: %{public}d", activeIdx_);
221 return Rotation::ROTATION_0;
222 }
223 sptr<SupportedScreenModes> info = modes_[activeIdx_];
224 // virtical: phone(Plugin screen); horizontal: pad & external screen
225 bool isVerticalScreen = info->width_ < info->height_;
226 switch (orientation) {
227 case Orientation::UNSPECIFIED: {
228 return Rotation::ROTATION_0;
229 }
230 case Orientation::VERTICAL: {
231 return isVerticalScreen ? Rotation::ROTATION_0 : Rotation::ROTATION_90;
232 }
233 case Orientation::HORIZONTAL: {
234 return isVerticalScreen ? Rotation::ROTATION_90 : Rotation::ROTATION_0;
235 }
236 case Orientation::REVERSE_VERTICAL: {
237 return isVerticalScreen ? Rotation::ROTATION_180 : Rotation::ROTATION_270;
238 }
239 case Orientation::REVERSE_HORIZONTAL: {
240 return isVerticalScreen ? Rotation::ROTATION_270 : Rotation::ROTATION_180;
241 }
242 default: {
243 WLOGE("unknown orientation %{public}u", orientation);
244 return Rotation::ROTATION_0;
245 }
246 }
247 }
248
AbstractScreenGroup(sptr<AbstractScreenController> screenController,ScreenId dmsId,ScreenId rsId,std::string name,ScreenCombination combination)249 AbstractScreenGroup::AbstractScreenGroup(sptr<AbstractScreenController> screenController, ScreenId dmsId, ScreenId rsId,
250 std::string name, ScreenCombination combination) : AbstractScreen(screenController, name, dmsId, rsId),
251 combination_(combination)
252 {
253 type_ = ScreenType::UNDEFINE;
254 isScreenGroup_ = true;
255 }
256
~AbstractScreenGroup()257 AbstractScreenGroup::~AbstractScreenGroup()
258 {
259 rsDisplayNode_ = nullptr;
260 abstractScreenMap_.clear();
261 }
262
ConvertToScreenGroupInfo() const263 sptr<ScreenGroupInfo> AbstractScreenGroup::ConvertToScreenGroupInfo() const
264 {
265 sptr<ScreenGroupInfo> screenGroupInfo = new(std::nothrow) ScreenGroupInfo();
266 if (screenGroupInfo == nullptr) {
267 return nullptr;
268 }
269 FillScreenInfo(screenGroupInfo);
270 screenGroupInfo->combination_ = combination_;
271 for (auto iter = abstractScreenMap_.begin(); iter != abstractScreenMap_.end(); iter++) {
272 screenGroupInfo->children_.push_back(iter->first);
273 }
274 auto positions = GetChildrenPosition();
275 screenGroupInfo->position_.insert(screenGroupInfo->position_.end(), positions.begin(), positions.end());
276 return screenGroupInfo;
277 }
278
GetRSDisplayNodeConfig(sptr<AbstractScreen> & dmsScreen,struct RSDisplayNodeConfig & config)279 bool AbstractScreenGroup::GetRSDisplayNodeConfig(sptr<AbstractScreen>& dmsScreen, struct RSDisplayNodeConfig& config)
280 {
281 if (dmsScreen == nullptr) {
282 WLOGE("dmsScreen is nullptr.");
283 return false;
284 }
285 switch (combination_) {
286 case ScreenCombination::SCREEN_ALONE:
287 case ScreenCombination::SCREEN_EXPAND:
288 config = { dmsScreen->rsId_ };
289 break;
290 case ScreenCombination::SCREEN_MIRROR: {
291 if (GetChildCount() == 0 || mirrorScreenId_ == dmsScreen->dmsId_) {
292 WLOGI("AddChild, SCREEN_MIRROR, config is not mirror");
293 config = { dmsScreen->rsId_ };
294 break;
295 }
296 if (mirrorScreenId_ == INVALID_SCREEN_ID || !HasChild(mirrorScreenId_)) {
297 WLOGI("AddChild, mirrorScreenId_ is invalid, use default screen");
298 mirrorScreenId_ = screenController_->GetDefaultAbstractScreenId();
299 }
300 std::shared_ptr<RSDisplayNode> displayNode = screenController_->GetRSDisplayNodeByScreenId(mirrorScreenId_);
301 if (displayNode == nullptr) {
302 WLOGFE("AddChild fail, displayNode is nullptr, cannot get DisplayNode");
303 return false;
304 }
305 NodeId nodeId = displayNode->GetId();
306 WLOGI("AddChild, mirrorScreenId_:%{public}" PRIu64", rsId_:%{public}" PRIu64", nodeId:%{public}" PRIu64"",
307 mirrorScreenId_, dmsScreen->rsId_, nodeId);
308 config = {dmsScreen->rsId_, true, nodeId};
309 break;
310 }
311 default:
312 WLOGE("fail to add child. invalid group combination:%{public}u", combination_);
313 return false;
314 }
315 return true;
316 }
317
AddChild(sptr<AbstractScreen> & dmsScreen,Point & startPoint)318 bool AbstractScreenGroup::AddChild(sptr<AbstractScreen>& dmsScreen, Point& startPoint)
319 {
320 if (dmsScreen == nullptr) {
321 WLOGE("AddChild, dmsScreen is nullptr.");
322 return false;
323 }
324 ScreenId screenId = dmsScreen->dmsId_;
325 auto iter = abstractScreenMap_.find(screenId);
326 if (iter != abstractScreenMap_.end()) {
327 WLOGE("AddChild, abstractScreenMap_ has dmsScreen:%{public}" PRIu64"", screenId);
328 return false;
329 }
330 struct RSDisplayNodeConfig config;
331 if (!GetRSDisplayNodeConfig(dmsScreen, config)) {
332 return false;
333 }
334 dmsScreen->InitRSDisplayNode(config);
335 dmsScreen->groupDmsId_ = dmsId_;
336 abstractScreenMap_.insert(std::make_pair(screenId, std::make_pair(dmsScreen, startPoint)));
337 return true;
338 }
339
AddChildren(std::vector<sptr<AbstractScreen>> & dmsScreens,std::vector<Point> & startPoints)340 bool AbstractScreenGroup::AddChildren(std::vector<sptr<AbstractScreen>>& dmsScreens, std::vector<Point>& startPoints)
341 {
342 size_t size = dmsScreens.size();
343 if (size != startPoints.size()) {
344 WLOGE("AddChildren, unequal size.");
345 return false;
346 }
347 bool res = true;
348 for (size_t i = 0; i < size; i++) {
349 res = AddChild(dmsScreens[i], startPoints[i]) && res;
350 }
351 return res;
352 }
353
RemoveChild(sptr<AbstractScreen> & dmsScreen)354 bool AbstractScreenGroup::RemoveChild(sptr<AbstractScreen>& dmsScreen)
355 {
356 if (dmsScreen == nullptr) {
357 WLOGE("RemoveChild, dmsScreen is nullptr.");
358 return false;
359 }
360 ScreenId screenId = dmsScreen->dmsId_;
361 dmsScreen->groupDmsId_ = SCREEN_ID_INVALID;
362 if (rsDisplayNode_ != nullptr) {
363 rsDisplayNode_->RemoveFromTree();
364 auto transactionProxy = RSTransactionProxy::GetInstance();
365 if (transactionProxy != nullptr) {
366 transactionProxy->FlushImplicitTransaction();
367 }
368 }
369 return abstractScreenMap_.erase(screenId);
370 }
371
HasChild(ScreenId childScreen) const372 bool AbstractScreenGroup::HasChild(ScreenId childScreen) const
373 {
374 return abstractScreenMap_.find(childScreen) != abstractScreenMap_.end();
375 }
376
GetChildren() const377 std::vector<sptr<AbstractScreen>> AbstractScreenGroup::GetChildren() const
378 {
379 std::vector<sptr<AbstractScreen>> res;
380 for (auto iter = abstractScreenMap_.begin(); iter != abstractScreenMap_.end(); iter++) {
381 res.push_back(iter->second.first);
382 }
383 return res;
384 }
385
GetChildrenPosition() const386 std::vector<Point> AbstractScreenGroup::GetChildrenPosition() const
387 {
388 std::vector<Point> res;
389 for (auto iter = abstractScreenMap_.begin(); iter != abstractScreenMap_.end(); iter++) {
390 res.push_back(iter->second.second);
391 }
392 return res;
393 }
394
GetChildCount() const395 size_t AbstractScreenGroup::GetChildCount() const
396 {
397 return abstractScreenMap_.size();
398 }
399 } // namespace OHOS::Rosen
400