1 /*
2 * Copyright (c) 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
16 #include "session/screen/include/screen_session.h"
17 #include <hisysevent.h>
18
19 #include "screen_cache.h"
20 #include <hitrace_meter.h>
21 #include <surface_capture_future.h>
22 #include <transaction/rs_interfaces.h>
23 #include <transaction/rs_transaction.h>
24 #include "window_manager_hilog.h"
25 #include "dm_common.h"
26 #include "dms_xcollie.h"
27 #include "fold_screen_state_internel.h"
28 #include <parameters.h>
29 #include "sys_cap_util.h"
30 #include <ipc_skeleton.h>
31
32 namespace OHOS::Rosen {
33 namespace {
34 constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_DISPLAY, "ScreenSession" };
35 static const int32_t g_screenRotationOffSet = system::GetIntParameter<int32_t>("const.fold.screen_rotation.offset", 0);
36 // 0: 横扫屏; 1: 竖扫屏. 默认为0
37 static const int32_t g_screenScanType = system::GetIntParameter<int32_t>("const.window.screen.scan_type", 0);
38 static const int32_t SCAN_TYPE_VERTICAL = 1;
39 static const int32_t ROTATION_90 = 1;
40 static const int32_t ROTATION_270 = 3;
41 const unsigned int XCOLLIE_TIMEOUT_5S = 5;
42 const static uint32_t MAX_INTERVAL_US = 1800000000; //30分钟
43 const int32_t MAP_SIZE = 300;
44 const int32_t NO_EXIST_UID_VERSION = -1;
45 const float FULL_STATUS_WIDTH = 2048;
46 const float GLOBAL_FULL_STATUS_WIDTH = 3184;
47 const float MAIN_STATUS_WIDTH = 1008;
48 const float FULL_STATUS_OFFSET_X = 1136;
49 const float SCREEN_HEIGHT = 2232;
50 constexpr uint32_t SECONDARY_ROTATION_90 = 1;
51 constexpr uint32_t SECONDARY_ROTATION_270 = 3;
52 constexpr uint32_t SECONDARY_ROTATION_MOD = 4;
53 ScreenCache g_uidVersionMap(MAP_SIZE, NO_EXIST_UID_VERSION);
54 }
55
ScreenSession(const ScreenSessionConfig & config,ScreenSessionReason reason)56 ScreenSession::ScreenSession(const ScreenSessionConfig& config, ScreenSessionReason reason)
57 : name_(config.name), screenId_(config.screenId), rsId_(config.rsId), defaultScreenId_(config.defaultScreenId),
58 property_(config.property), displayNode_(config.displayNode), innerName_(config.innerName)
59 {
60 TLOGI(WmsLogTag::DMS,
61 "[DPNODE]Create Session, reason: %{public}d, screenId: %{public}" PRIu64", rsId: %{public}" PRIu64"",
62 reason, screenId_, rsId_);
63 TLOGI(WmsLogTag::DMS,
64 "[DPNODE]Config name: %{public}s, defaultId: %{public}" PRIu64", mirrorNodeId: %{public}" PRIu64"",
65 name_.c_str(), defaultScreenId_, config.mirrorNodeId);
66 Rosen::RSDisplayNodeConfig rsConfig;
67 bool isNeedCreateDisplayNode = true;
68 switch (reason) {
69 case ScreenSessionReason::CREATE_SESSION_FOR_CLIENT: {
70 TLOGI(WmsLogTag::DMS, "create screen session for client. noting to do.");
71 return;
72 }
73 case ScreenSessionReason::CREATE_SESSION_FOR_VIRTUAL: {
74 // create virtual screen should use rsid
75 rsConfig.screenId = rsId_;
76 break;
77 }
78 case ScreenSessionReason::CREATE_SESSION_FOR_MIRROR: {
79 rsConfig.screenId = screenId_;
80 rsConfig.isMirrored = true;
81 rsConfig.mirrorNodeId = config.mirrorNodeId;
82 rsConfig.isSync = true;
83 break;
84 }
85 case ScreenSessionReason::CREATE_SESSION_FOR_REAL: {
86 rsConfig.screenId = screenId_;
87 break;
88 }
89 case ScreenSessionReason::CREATE_SESSION_WITHOUT_DISPLAY_NODE: {
90 TLOGI(WmsLogTag::DMS, "screen session no need create displayNode.");
91 isNeedCreateDisplayNode = false;
92 break;
93 }
94 default : {
95 TLOGE(WmsLogTag::DMS, "invalid screen session config.");
96 break;
97 }
98 }
99 if (isNeedCreateDisplayNode) {
100 CreateDisplayNode(rsConfig);
101 }
102 }
103
CreateDisplayNode(const Rosen::RSDisplayNodeConfig & config)104 void ScreenSession::CreateDisplayNode(const Rosen::RSDisplayNodeConfig& config)
105 {
106 TLOGI(WmsLogTag::DMS,
107 "[DPNODE]config screenId: %{public}" PRIu64", mirrorNodeId: %{public}" PRIu64", isMirrored: %{public}d",
108 config.screenId, config.mirrorNodeId, static_cast<int32_t>(config.isMirrored));
109 {
110 std::unique_lock<std::shared_mutex> displayNodeLock(displayNodeMutex_);
111 displayNode_ = Rosen::RSDisplayNode::Create(config);
112 if (displayNode_) {
113 displayNode_->SetFrame(property_.GetBounds().rect_.left_, property_.GetBounds().rect_.top_,
114 property_.GetBounds().rect_.width_, property_.GetBounds().rect_.height_);
115 displayNode_->SetBounds(property_.GetBounds().rect_.left_, property_.GetBounds().rect_.top_,
116 property_.GetBounds().rect_.width_, property_.GetBounds().rect_.height_);
117 if (config.isMirrored) {
118 EnableMirrorScreenRegion();
119 }
120 } else {
121 TLOGE(WmsLogTag::DMS, "Failed to create displayNode, displayNode is null!");
122 }
123 }
124 RSTransaction::FlushImplicitTransaction();
125 }
126
ReuseDisplayNode(const RSDisplayNodeConfig & config)127 void ScreenSession::ReuseDisplayNode(const RSDisplayNodeConfig& config)
128 {
129 if (displayNode_) {
130 displayNode_->SetDisplayNodeMirrorConfig(config);
131 RSTransaction::FlushImplicitTransaction();
132 } else {
133 CreateDisplayNode(config);
134 }
135 }
136
~ScreenSession()137 ScreenSession::~ScreenSession()
138 {
139 TLOGI(WmsLogTag::DMS, "~ScreenSession");
140 }
141
ScreenSession(ScreenId screenId,ScreenId rsId,const std::string & name,const ScreenProperty & property,const std::shared_ptr<RSDisplayNode> & displayNode)142 ScreenSession::ScreenSession(ScreenId screenId, ScreenId rsId, const std::string& name,
143 const ScreenProperty& property, const std::shared_ptr<RSDisplayNode>& displayNode)
144 : name_(name), screenId_(screenId), rsId_(rsId), property_(property), displayNode_(displayNode)
145 {
146 TLOGI(WmsLogTag::DMS, "Success to create screenSession in constructor_0, screenid is %{public}" PRIu64"",
147 screenId_);
148 }
149
ScreenSession(ScreenId screenId,const ScreenProperty & property,ScreenId defaultScreenId)150 ScreenSession::ScreenSession(ScreenId screenId, const ScreenProperty& property, ScreenId defaultScreenId)
151 : screenId_(screenId), defaultScreenId_(defaultScreenId), property_(property)
152 {
153 Rosen::RSDisplayNodeConfig config = { .screenId = screenId_ };
154 displayNode_ = Rosen::RSDisplayNode::Create(config);
155 if (displayNode_) {
156 TLOGI(WmsLogTag::DMS, "Success to create displayNode in constructor_1, screenid is %{public}" PRIu64"",
157 screenId_);
158 displayNode_->SetFrame(property_.GetBounds().rect_.left_, property_.GetBounds().rect_.top_,
159 property_.GetBounds().rect_.width_, property_.GetBounds().rect_.height_);
160 displayNode_->SetBounds(property_.GetBounds().rect_.left_, property_.GetBounds().rect_.top_,
161 property_.GetBounds().rect_.width_, property_.GetBounds().rect_.height_);
162 } else {
163 TLOGE(WmsLogTag::DMS, "Failed to create displayNode, displayNode is null!");
164 }
165 RSTransaction::FlushImplicitTransaction();
166 }
167
ScreenSession(ScreenId screenId,const ScreenProperty & property,NodeId nodeId,ScreenId defaultScreenId)168 ScreenSession::ScreenSession(ScreenId screenId, const ScreenProperty& property,
169 NodeId nodeId, ScreenId defaultScreenId)
170 : screenId_(screenId), defaultScreenId_(defaultScreenId), property_(property)
171 {
172 rsId_ = screenId;
173 Rosen::RSDisplayNodeConfig config = { .screenId = screenId_, .isMirrored = true, .mirrorNodeId = nodeId,
174 .isSync = true};
175 displayNode_ = Rosen::RSDisplayNode::Create(config);
176 if (displayNode_) {
177 TLOGI(WmsLogTag::DMS, "Success to create displayNode in constructor_2, screenid is %{public}" PRIu64"",
178 screenId_);
179 displayNode_->SetFrame(property_.GetBounds().rect_.left_, property_.GetBounds().rect_.top_,
180 property_.GetBounds().rect_.width_, property_.GetBounds().rect_.height_);
181 displayNode_->SetBounds(property_.GetBounds().rect_.left_, property_.GetBounds().rect_.top_,
182 property_.GetBounds().rect_.width_, property_.GetBounds().rect_.height_);
183 } else {
184 TLOGE(WmsLogTag::DMS, "Failed to create displayNode, displayNode is null!");
185 }
186 RSTransaction::FlushImplicitTransaction();
187 }
188
ScreenSession(const std::string & name,ScreenId smsId,ScreenId rsId,ScreenId defaultScreenId)189 ScreenSession::ScreenSession(const std::string& name, ScreenId smsId, ScreenId rsId, ScreenId defaultScreenId)
190 : name_(name), screenId_(smsId), rsId_(rsId), defaultScreenId_(defaultScreenId)
191 {
192 (void)rsId_;
193 // 虚拟屏的screen id和rs id不一致,displayNode的创建应使用rs id
194 Rosen::RSDisplayNodeConfig config = { .screenId = rsId_ };
195 displayNode_ = Rosen::RSDisplayNode::Create(config);
196 if (displayNode_) {
197 TLOGI(WmsLogTag::DMS, "Success to create displayNode in constructor_3, rs id is %{public}" PRIu64"", rsId_);
198 displayNode_->SetFrame(property_.GetBounds().rect_.left_, property_.GetBounds().rect_.top_,
199 property_.GetBounds().rect_.width_, property_.GetBounds().rect_.height_);
200 displayNode_->SetBounds(property_.GetBounds().rect_.left_, property_.GetBounds().rect_.top_,
201 property_.GetBounds().rect_.width_, property_.GetBounds().rect_.height_);
202 } else {
203 TLOGE(WmsLogTag::DMS, "Failed to create displayNode, displayNode is null!");
204 }
205 RSTransaction::FlushImplicitTransaction();
206 }
207
SetDisplayNodeScreenId(ScreenId screenId)208 void ScreenSession::SetDisplayNodeScreenId(ScreenId screenId)
209 {
210 std::shared_lock<std::shared_mutex> displayNodeLock(displayNodeMutex_);
211 if (displayNode_ != nullptr) {
212 TLOGI(WmsLogTag::DMS, "SetDisplayNodeScreenId %{public}" PRIu64"", screenId);
213 displayNode_->SetScreenId(screenId);
214 }
215 }
216
RegisterScreenChangeListener(IScreenChangeListener * screenChangeListener)217 void ScreenSession::RegisterScreenChangeListener(IScreenChangeListener* screenChangeListener)
218 {
219 if (screenChangeListener == nullptr) {
220 TLOGE(WmsLogTag::DMS, "Failed to register screen change listener, listener is null!");
221 return;
222 }
223 std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
224 if (std::find(screenChangeListenerList_.begin(), screenChangeListenerList_.end(), screenChangeListener) !=
225 screenChangeListenerList_.end()) {
226 TLOGI(WmsLogTag::DMS, "Repeat to register screen change listener!");
227 return;
228 }
229
230 screenChangeListenerList_.emplace_back(screenChangeListener);
231 if (screenState_ == ScreenState::CONNECTION) {
232 screenChangeListener->OnConnect(screenId_);
233 TLOGI(WmsLogTag::DMS, "Success to call onconnect callback.");
234 }
235 TLOGI(WmsLogTag::DMS, "Success to register screen change listener.");
236 }
237
UnregisterScreenChangeListener(IScreenChangeListener * screenChangeListener)238 void ScreenSession::UnregisterScreenChangeListener(IScreenChangeListener* screenChangeListener)
239 {
240 if (screenChangeListener == nullptr) {
241 TLOGE(WmsLogTag::DMS, "Failed to unregister screen change listener, listener is null!");
242 return;
243 }
244 std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
245 screenChangeListenerList_.erase(
246 std::remove_if(screenChangeListenerList_.begin(), screenChangeListenerList_.end(),
247 [screenChangeListener](IScreenChangeListener* listener) { return screenChangeListener == listener; }),
248 screenChangeListenerList_.end());
249 }
250
SetMirrorScreenRegion(ScreenId screenId,DMRect screenRegion)251 void ScreenSession::SetMirrorScreenRegion(ScreenId screenId, DMRect screenRegion)
252 {
253 std::lock_guard<std::mutex> lock(mirrorScreenRegionMutex_);
254 mirrorScreenRegion_ = std::make_pair(screenId, screenRegion);
255 }
256
GetMirrorScreenRegion()257 std::pair<ScreenId, DMRect> ScreenSession::GetMirrorScreenRegion()
258 {
259 std::lock_guard<std::mutex> lock(mirrorScreenRegionMutex_);
260 return mirrorScreenRegion_;
261 }
262
EnableMirrorScreenRegion()263 void ScreenSession::EnableMirrorScreenRegion()
264 {
265 const auto& mirrorScreenRegionPair = GetMirrorScreenRegion();
266 const auto& rect = mirrorScreenRegionPair.second;
267 ScreenId screenId = INVALID_SCREEN_ID;
268 if (isPhysicalMirrorSwitch_) {
269 screenId = screenId_;
270 } else {
271 screenId = rsId_;
272 }
273 auto ret = RSInterfaces::GetInstance().SetMirrorScreenVisibleRect(screenId,
274 { rect.posX_, rect.posY_, rect.width_, rect.height_ });
275 if (ret != StatusCode::SUCCESS) {
276 TLOGE(WmsLogTag::DMS, "Fail! rsId %{public}" PRIu64", ret:%{public}d," PRIu64
277 ", x:%{public}d y:%{public}d w:%{public}u h:%{public}u", screenId, ret,
278 rect.posX_, rect.posY_, rect.width_, rect.height_);
279 } else {
280 TLOGE(WmsLogTag::DMS, "Success! rsId %{public}" PRIu64", ret:%{public}d," PRIu64
281 ", x:%{public}d y:%{public}d w:%{public}u h:%{public}u", screenId, ret,
282 rect.posX_, rect.posY_, rect.width_, rect.height_);
283 }
284 }
285
ConvertToDisplayInfo()286 sptr<DisplayInfo> ScreenSession::ConvertToDisplayInfo()
287 {
288 sptr<DisplayInfo> displayInfo = new(std::nothrow) DisplayInfo();
289 if (displayInfo == nullptr) {
290 return displayInfo;
291 }
292 RRect bounds = property_.GetBounds();
293 RRect phyBounds = property_.GetPhyBounds();
294 displayInfo->name_ = name_;
295 displayInfo->SetWidth(bounds.rect_.GetWidth());
296 if (isBScreenHalf_) {
297 DMRect creaseRect = property_.GetCreaseRect();
298 if (creaseRect.posY_ > 0) {
299 displayInfo->SetHeight(creaseRect.posY_);
300 } else {
301 displayInfo->SetHeight(bounds.rect_.GetHeight() / HALF_SCREEN_PARAM);
302 }
303 } else {
304 displayInfo->SetHeight(bounds.rect_.GetHeight());
305 }
306 displayInfo->SetPhysicalWidth(phyBounds.rect_.GetWidth());
307 displayInfo->SetPhysicalHeight(phyBounds.rect_.GetHeight());
308 displayInfo->SetScreenId(screenId_);
309 displayInfo->SetDisplayId(screenId_);
310 displayInfo->SetRefreshRate(property_.GetRefreshRate());
311 displayInfo->SetVirtualPixelRatio(property_.GetVirtualPixelRatio());
312 displayInfo->SetDensityInCurResolution(property_.GetDensityInCurResolution());
313 displayInfo->SetDefaultVirtualPixelRatio(property_.GetDefaultDensity());
314 displayInfo->SetXDpi(property_.GetXDpi());
315 displayInfo->SetYDpi(property_.GetYDpi());
316 displayInfo->SetDpi(property_.GetVirtualPixelRatio() * DOT_PER_INCH);
317 int32_t apiVersion = GetApiVersion();
318 if (apiVersion >= 14 || apiVersion == 0) { // 14 is API version
319 displayInfo->SetRotation(property_.GetDeviceRotation());
320 displayInfo->SetDisplayOrientation(property_.GetDeviceOrientation());
321 } else {
322 displayInfo->SetRotation(property_.GetScreenRotation());
323 displayInfo->SetDisplayOrientation(property_.GetDisplayOrientation());
324 }
325 displayInfo->SetOrientation(property_.GetOrientation());
326 displayInfo->SetOffsetX(property_.GetOffsetX());
327 displayInfo->SetOffsetY(property_.GetOffsetY());
328 displayInfo->SetHdrFormats(hdrFormats_);
329 displayInfo->SetColorSpaces(colorSpaces_);
330 displayInfo->SetDisplayState(property_.GetDisplayState());
331 displayInfo->SetDefaultDeviceRotationOffset(property_.GetDefaultDeviceRotationOffset());
332 displayInfo->SetAvailableWidth(property_.GetAvailableArea().width_);
333 displayInfo->SetAvailableHeight(property_.GetAvailableArea().height_);
334 displayInfo->SetScaleX(property_.GetScaleX());
335 displayInfo->SetScaleY(property_.GetScaleY());
336 displayInfo->SetPivotX(property_.GetPivotX());
337 displayInfo->SetPivotY(property_.GetPivotY());
338 displayInfo->SetTranslateX(property_.GetTranslateX());
339 displayInfo->SetTranslateY(property_.GetTranslateY());
340 displayInfo->SetScreenShape(property_.GetScreenShape());
341 displayInfo->SetOriginRotation(property_.GetScreenRotation());
342 return displayInfo;
343 }
344
ConvertToRealDisplayInfo()345 sptr<DisplayInfo> ScreenSession::ConvertToRealDisplayInfo()
346 {
347 sptr<DisplayInfo> displayInfo = new(std::nothrow) DisplayInfo();
348 if (displayInfo == nullptr) {
349 return displayInfo;
350 }
351 RRect bounds = property_.GetBounds();
352 RRect phyBounds = property_.GetPhyBounds();
353 displayInfo->name_ = name_;
354 displayInfo->SetWidth(bounds.rect_.GetWidth());
355 displayInfo->SetHeight(bounds.rect_.GetHeight());
356 displayInfo->SetPhysicalWidth(phyBounds.rect_.GetWidth());
357 displayInfo->SetPhysicalHeight(phyBounds.rect_.GetHeight());
358 displayInfo->SetScreenId(screenId_);
359 displayInfo->SetDisplayId(screenId_);
360 displayInfo->SetRefreshRate(property_.GetRefreshRate());
361 displayInfo->SetVirtualPixelRatio(property_.GetVirtualPixelRatio());
362 displayInfo->SetDensityInCurResolution(property_.GetDensityInCurResolution());
363 displayInfo->SetDefaultVirtualPixelRatio(property_.GetDefaultDensity());
364 displayInfo->SetXDpi(property_.GetXDpi());
365 displayInfo->SetYDpi(property_.GetYDpi());
366 displayInfo->SetDpi(property_.GetVirtualPixelRatio() * DOT_PER_INCH);
367 int32_t apiVersion = GetApiVersion();
368 if (apiVersion >= 14 || apiVersion == 0) { // 14 is API version
369 displayInfo->SetRotation(property_.GetDeviceRotation());
370 displayInfo->SetDisplayOrientation(property_.GetDeviceOrientation());
371 } else {
372 displayInfo->SetRotation(property_.GetScreenRotation());
373 displayInfo->SetDisplayOrientation(property_.GetDisplayOrientation());
374 }
375 displayInfo->SetOrientation(property_.GetOrientation());
376 displayInfo->SetOffsetX(property_.GetOffsetX());
377 displayInfo->SetOffsetY(property_.GetOffsetY());
378 displayInfo->SetHdrFormats(hdrFormats_);
379 displayInfo->SetColorSpaces(colorSpaces_);
380 displayInfo->SetDisplayState(property_.GetDisplayState());
381 displayInfo->SetDefaultDeviceRotationOffset(property_.GetDefaultDeviceRotationOffset());
382 displayInfo->SetAvailableWidth(property_.GetAvailableArea().width_);
383 displayInfo->SetAvailableHeight(property_.GetAvailableArea().height_);
384 displayInfo->SetScaleX(property_.GetScaleX());
385 displayInfo->SetScaleY(property_.GetScaleY());
386 displayInfo->SetPivotX(property_.GetPivotX());
387 displayInfo->SetPivotY(property_.GetPivotY());
388 displayInfo->SetTranslateX(property_.GetTranslateX());
389 displayInfo->SetTranslateY(property_.GetTranslateY());
390 return displayInfo;
391 }
392
GetScreenSupportedColorGamuts(std::vector<ScreenColorGamut> & colorGamuts)393 DMError ScreenSession::GetScreenSupportedColorGamuts(std::vector<ScreenColorGamut>& colorGamuts)
394 {
395 auto ret = RSInterfaces::GetInstance().GetScreenSupportedColorGamuts(rsId_, colorGamuts);
396 if (ret != StatusCode::SUCCESS) {
397 TLOGE(WmsLogTag::DMS, "SCB: fail! rsId %{public}" PRIu64", ret:%{public}d",
398 rsId_, ret);
399 return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
400 }
401 TLOGI(WmsLogTag::DMS, "SCB: ok! rsId %{public}" PRIu64", size %{public}u",
402 rsId_, static_cast<uint32_t>(colorGamuts.size()));
403
404 return DMError::DM_OK;
405 }
406
SetIsExtend(bool isExtend)407 void ScreenSession::SetIsExtend(bool isExtend)
408 {
409 isExtended_ = isExtend;
410 }
411
GetIsExtend() const412 bool ScreenSession::GetIsExtend() const
413 {
414 return isExtended_;
415 }
416
SetIsInternal(bool isInternal)417 void ScreenSession::SetIsInternal(bool isInternal)
418 {
419 isInternal_ = isInternal;
420 }
421
GetIsInternal() const422 bool ScreenSession::GetIsInternal() const
423 {
424 return isInternal_;
425 }
426
SetIsCurrentInUse(bool isInUse)427 void ScreenSession::SetIsCurrentInUse(bool isInUse)
428 {
429 isInUse_ = isInUse;
430 }
431
GetIsCurrentInUse() const432 bool ScreenSession::GetIsCurrentInUse() const
433 {
434 return isInUse_;
435 }
436
SetIsFakeInUse(bool isFakeInUse)437 void ScreenSession::SetIsFakeInUse(bool isFakeInUse)
438 {
439 isFakeInUse_ = isFakeInUse;
440 }
441
GetIsFakeInUse() const442 bool ScreenSession::GetIsFakeInUse() const
443 {
444 return isFakeInUse_;
445 }
446
SetIsRealScreen(bool isReal)447 void ScreenSession::SetIsRealScreen(bool isReal)
448 {
449 isReal_ = isReal;
450 }
451
GetIsRealScreen()452 bool ScreenSession::GetIsRealScreen()
453 {
454 return isReal_;
455 }
456
SetIsPcUse(bool isPcUse)457 void ScreenSession::SetIsPcUse(bool isPcUse)
458 {
459 isPcUse_ = isPcUse;
460 }
461
GetIsPcUse()462 bool ScreenSession::GetIsPcUse()
463 {
464 return isPcUse_;
465 }
466
SetValidHeight(uint32_t validHeight)467 void ScreenSession::SetValidHeight(uint32_t validHeight)
468 {
469 property_.SetValidHeight(validHeight);
470 }
471
SetValidWidth(uint32_t validWidth)472 void ScreenSession::SetValidWidth(uint32_t validWidth)
473 {
474 property_.SetValidWidth(validWidth);
475 }
476
GetValidHeight() const477 int32_t ScreenSession::GetValidHeight() const
478 {
479 return property_.GetValidHeight();
480 }
481
GetValidWidth() const482 int32_t ScreenSession::GetValidWidth() const
483 {
484 return property_.GetValidWidth();
485 }
486
SetPointerActiveWidth(uint32_t pointerActiveWidth)487 void ScreenSession::SetPointerActiveWidth(uint32_t pointerActiveWidth)
488 {
489 property_.SetPointerActiveWidth(pointerActiveWidth);
490 }
491
GetPointerActiveWidth()492 uint32_t ScreenSession::GetPointerActiveWidth()
493 {
494 return property_.GetPointerActiveWidth();
495 }
496
SetPointerActiveHeight(uint32_t pointerActiveHeight)497 void ScreenSession::SetPointerActiveHeight(uint32_t pointerActiveHeight)
498 {
499 property_.SetPointerActiveHeight(pointerActiveHeight);
500 }
501
GetPointerActiveHeight()502 uint32_t ScreenSession::GetPointerActiveHeight()
503 {
504 return property_.GetPointerActiveHeight();
505 }
506
SetIsBScreenHalf(bool isBScreenHalf)507 void ScreenSession::SetIsBScreenHalf(bool isBScreenHalf)
508 {
509 isBScreenHalf_ = isBScreenHalf;
510 }
511
GetIsBScreenHalf() const512 bool ScreenSession::GetIsBScreenHalf() const
513 {
514 return isBScreenHalf_;
515 }
516
SetFakeScreenSession(sptr<ScreenSession> fakeScreenSession)517 void ScreenSession::SetFakeScreenSession(sptr<ScreenSession> fakeScreenSession)
518 {
519 fakeScreenSession_ = fakeScreenSession;
520 }
521
GetFakeScreenSession() const522 sptr<ScreenSession> ScreenSession::GetFakeScreenSession() const
523 {
524 return fakeScreenSession_;
525 }
526
GetName()527 std::string ScreenSession::GetName()
528 {
529 return name_;
530 }
531
SetName(std::string name)532 void ScreenSession::SetName(std::string name)
533 {
534 name_ = name;
535 }
536
GetInnerName()537 std::string ScreenSession::GetInnerName()
538 {
539 return innerName_;
540 }
541
SetInnerName(std::string innerName)542 void ScreenSession::SetInnerName(std::string innerName)
543 {
544 innerName_ = innerName;
545 }
546
SetMirrorScreenType(MirrorScreenType mirrorType)547 void ScreenSession::SetMirrorScreenType(MirrorScreenType mirrorType)
548 {
549 mirrorScreenType_ = mirrorType;
550 }
551
GetMirrorScreenType()552 MirrorScreenType ScreenSession::GetMirrorScreenType()
553 {
554 return mirrorScreenType_;
555 }
556
GetScreenId()557 ScreenId ScreenSession::GetScreenId()
558 {
559 return screenId_;
560 }
561
GetRSScreenId()562 ScreenId ScreenSession::GetRSScreenId()
563 {
564 return rsId_;
565 }
566
GetScreenShape() const567 ScreenShape ScreenSession::GetScreenShape() const
568 {
569 return property_.GetScreenShape();
570 }
571
GetScreenProperty() const572 ScreenProperty ScreenSession::GetScreenProperty() const
573 {
574 return property_;
575 }
576
SetSerialNumber(std::string serialNumber)577 void ScreenSession::SetSerialNumber(std::string serialNumber)
578 {
579 serialNumber_ = serialNumber;
580 }
581
GetSerialNumber() const582 std::string ScreenSession::GetSerialNumber() const
583 {
584 return serialNumber_;
585 }
586
SetScreenScale(float scaleX,float scaleY,float pivotX,float pivotY,float translateX,float translateY)587 void ScreenSession::SetScreenScale(float scaleX, float scaleY, float pivotX, float pivotY, float translateX,
588 float translateY)
589 {
590 property_.SetScaleX(scaleX);
591 property_.SetScaleY(scaleY);
592 property_.SetPivotX(pivotX);
593 property_.SetPivotY(pivotY);
594 property_.SetTranslateX(translateX);
595 property_.SetTranslateY(translateY);
596 }
597
SetDefaultDeviceRotationOffset(uint32_t defaultRotationOffset)598 void ScreenSession::SetDefaultDeviceRotationOffset(uint32_t defaultRotationOffset)
599 {
600 TLOGI(WmsLogTag::DMS, "set device default rotation offset: %{public}d", defaultRotationOffset);
601 property_.SetDefaultDeviceRotationOffset(defaultRotationOffset);
602 }
603
UpdatePropertyByActiveMode()604 void ScreenSession::UpdatePropertyByActiveMode()
605 {
606 sptr<SupportedScreenModes> mode = GetActiveScreenMode();
607 if (mode != nullptr) {
608 auto screeBounds = property_.GetBounds();
609 screeBounds.rect_.width_ = mode->width_;
610 screeBounds.rect_.height_ = mode->height_;
611 property_.SetBounds(screeBounds);
612 }
613 }
614
UpdatePropertyByFoldControl(const ScreenProperty & updatedProperty,FoldDisplayMode foldDisplayMode)615 ScreenProperty ScreenSession::UpdatePropertyByFoldControl(const ScreenProperty& updatedProperty,
616 FoldDisplayMode foldDisplayMode)
617 {
618 property_.SetDpiPhyBounds(updatedProperty.GetPhyWidth(), updatedProperty.GetPhyHeight());
619 property_.SetPhyBounds(updatedProperty.GetPhyBounds());
620 property_.SetBounds(updatedProperty.GetBounds());
621 OptimizeSecondaryDisplayMode(updatedProperty.GetBounds(), foldDisplayMode);
622 if (FoldScreenStateInternel::IsSecondaryDisplayFoldDevice()) {
623 DisplayOrientation deviceOrientation =
624 CalcDeviceOrientation(property_.GetScreenRotation(), foldDisplayMode, true);
625 property_.SetDisplayOrientation(deviceOrientation);
626 property_.SetDeviceOrientation(deviceOrientation);
627 }
628 UpdateTouchBoundsAndOffset();
629 return property_;
630 }
631
UpdateDisplayState(DisplayState displayState)632 void ScreenSession::UpdateDisplayState(DisplayState displayState)
633 {
634 property_.SetDisplayState(displayState);
635 }
636
UpdateRefreshRate(uint32_t refreshRate)637 void ScreenSession::UpdateRefreshRate(uint32_t refreshRate)
638 {
639 property_.SetRefreshRate(refreshRate);
640 }
641
GetRefreshRate()642 uint32_t ScreenSession::GetRefreshRate()
643 {
644 return property_.GetRefreshRate();
645 }
646
UpdatePropertyByResolution(uint32_t width,uint32_t height)647 void ScreenSession::UpdatePropertyByResolution(uint32_t width, uint32_t height)
648 {
649 auto screenBounds = property_.GetBounds();
650 screenBounds.rect_.width_ = width;
651 screenBounds.rect_.height_ = height;
652 property_.SetBounds(screenBounds);
653 }
654
UpdatePropertyByFakeBounds(uint32_t width,uint32_t height)655 void ScreenSession::UpdatePropertyByFakeBounds(uint32_t width, uint32_t height)
656 {
657 auto screenFakeBounds = property_.GetFakeBounds();
658 screenFakeBounds.rect_.width_ = width;
659 screenFakeBounds.rect_.height_ = height;
660 property_.SetFakeBounds(screenFakeBounds);
661 }
662
GetDisplayNode() const663 std::shared_ptr<RSDisplayNode> ScreenSession::GetDisplayNode() const
664 {
665 std::shared_lock<std::shared_mutex> displayNodeLock(displayNodeMutex_);
666 return displayNode_;
667 }
668
ReleaseDisplayNode()669 void ScreenSession::ReleaseDisplayNode()
670 {
671 std::unique_lock<std::shared_mutex> displayNodeLock(displayNodeMutex_);
672 displayNode_ = nullptr;
673 TLOGI(WmsLogTag::DMS, "displayNode_ is released.");
674 }
675
Connect()676 void ScreenSession::Connect()
677 {
678 std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
679 screenState_ = ScreenState::CONNECTION;
680 if (screenChangeListenerList_.empty()) {
681 TLOGE(WmsLogTag::DMS, "screenChangeListenerList is empty.");
682 return;
683 }
684 for (auto& listener : screenChangeListenerList_) {
685 if (!listener) {
686 TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
687 continue;
688 }
689 listener->OnConnect(screenId_);
690 }
691 }
692
Disconnect()693 void ScreenSession::Disconnect()
694 {
695 std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
696 screenState_ = ScreenState::DISCONNECTION;
697 if (screenChangeListenerList_.empty()) {
698 TLOGE(WmsLogTag::DMS, "screenChangeListenerList is empty.");
699 return;
700 }
701 for (auto& listener : screenChangeListenerList_) {
702 if (!listener) {
703 TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
704 continue;
705 }
706 listener->OnDisconnect(screenId_);
707 }
708 }
709
PropertyChange(const ScreenProperty & newProperty,ScreenPropertyChangeReason reason)710 void ScreenSession::PropertyChange(const ScreenProperty& newProperty, ScreenPropertyChangeReason reason)
711 {
712 std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
713 property_ = newProperty;
714 if (reason == ScreenPropertyChangeReason::VIRTUAL_PIXEL_RATIO_CHANGE) {
715 return;
716 }
717 if (screenChangeListenerList_.empty()) {
718 TLOGE(WmsLogTag::DMS, "screenChangeListenerList is empty.");
719 return;
720 }
721 for (auto& listener : screenChangeListenerList_) {
722 if (!listener) {
723 TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
724 continue;
725 }
726 listener->OnPropertyChange(newProperty, reason, screenId_);
727 }
728 }
729
PowerStatusChange(DisplayPowerEvent event,EventStatus status,PowerStateChangeReason reason)730 void ScreenSession::PowerStatusChange(DisplayPowerEvent event, EventStatus status, PowerStateChangeReason reason)
731 {
732 std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
733 if (screenChangeListenerList_.empty()) {
734 TLOGE(WmsLogTag::DMS, "screenChangeListenerList is empty.");
735 return;
736 }
737 for (auto& listener : screenChangeListenerList_) {
738 if (!listener) {
739 TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
740 continue;
741 }
742 listener->OnPowerStatusChange(event, status, reason);
743 }
744 }
745
ConvertRotationToFloat(Rotation sensorRotation)746 float ScreenSession::ConvertRotationToFloat(Rotation sensorRotation)
747 {
748 float rotation = 0.f;
749 switch (sensorRotation) {
750 case Rotation::ROTATION_90:
751 rotation = 90.f; // degree 90
752 break;
753 case Rotation::ROTATION_180:
754 rotation = 180.f; // degree 180
755 break;
756 case Rotation::ROTATION_270:
757 rotation = 270.f; // degree 270
758 break;
759 default:
760 rotation = 0.f;
761 break;
762 }
763 return rotation;
764 }
765
HandleSensorRotation(float sensorRotation)766 void ScreenSession::HandleSensorRotation(float sensorRotation)
767 {
768 SensorRotationChange(sensorRotation);
769 }
770
SensorRotationChange(Rotation sensorRotation)771 void ScreenSession::SensorRotationChange(Rotation sensorRotation)
772 {
773 float rotation = ConvertRotationToFloat(sensorRotation);
774 SensorRotationChange(rotation);
775 }
776
SensorRotationChange(float sensorRotation)777 void ScreenSession::SensorRotationChange(float sensorRotation)
778 {
779 std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
780 if (sensorRotation >= 0.0f) {
781 currentValidSensorRotation_ = sensorRotation;
782 }
783 currentSensorRotation_ = sensorRotation;
784 for (auto& listener : screenChangeListenerList_) {
785 if (!listener) {
786 TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
787 continue;
788 }
789 listener->OnSensorRotationChange(sensorRotation, screenId_);
790 }
791 }
792
GetValidSensorRotation()793 float ScreenSession::GetValidSensorRotation()
794 {
795 return currentValidSensorRotation_;
796 }
797
HandleHoverStatusChange(int32_t hoverStatus,bool needRotate)798 void ScreenSession::HandleHoverStatusChange(int32_t hoverStatus, bool needRotate)
799 {
800 HoverStatusChange(hoverStatus, needRotate);
801 }
802
HoverStatusChange(int32_t hoverStatus,bool needRotate)803 void ScreenSession::HoverStatusChange(int32_t hoverStatus, bool needRotate)
804 {
805 std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
806 for (auto& listener : screenChangeListenerList_) {
807 if (!listener) {
808 TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
809 continue;
810 }
811 listener->OnHoverStatusChange(hoverStatus, needRotate, screenId_);
812 }
813 }
814
HandleCameraBackSelfieChange(bool isCameraBackSelfie)815 void ScreenSession::HandleCameraBackSelfieChange(bool isCameraBackSelfie)
816 {
817 CameraBackSelfieChange(isCameraBackSelfie);
818 }
819
CameraBackSelfieChange(bool isCameraBackSelfie)820 void ScreenSession::CameraBackSelfieChange(bool isCameraBackSelfie)
821 {
822 std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
823 for (auto& listener : screenChangeListenerList_) {
824 if (!listener) {
825 WLOGFE("screenChangeListener is null.");
826 continue;
827 }
828 listener->OnCameraBackSelfieChange(isCameraBackSelfie, screenId_);
829 }
830 }
831
ScreenExtendChange(ScreenId mainScreenId,ScreenId extendScreenId)832 void ScreenSession::ScreenExtendChange(ScreenId mainScreenId, ScreenId extendScreenId)
833 {
834 std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
835 for (auto& listener : screenChangeListenerList_) {
836 if (!listener) {
837 TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
838 continue;
839 }
840 listener->OnScreenExtendChange(mainScreenId, extendScreenId);
841 }
842 }
843
ScreenOrientationChange(Orientation orientation,FoldDisplayMode foldDisplayMode)844 void ScreenSession::ScreenOrientationChange(Orientation orientation, FoldDisplayMode foldDisplayMode)
845 {
846 Rotation rotationAfter = CalcRotation(orientation, foldDisplayMode);
847 float screenRotation = ConvertRotationToFloat(rotationAfter);
848 ScreenOrientationChange(screenRotation);
849 }
850
ScreenOrientationChange(float orientation)851 void ScreenSession::ScreenOrientationChange(float orientation)
852 {
853 std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
854 for (auto& listener : screenChangeListenerList_) {
855 if (!listener) {
856 TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
857 continue;
858 }
859 listener->OnScreenOrientationChange(orientation, screenId_);
860 }
861 }
862
ConvertIntToRotation(int rotation)863 Rotation ScreenSession::ConvertIntToRotation(int rotation)
864 {
865 Rotation targetRotation = Rotation::ROTATION_0;
866 switch (rotation) {
867 case 90: // Rotation 90 degree
868 targetRotation = Rotation::ROTATION_90;
869 break;
870 case 180: // Rotation 180 degree
871 targetRotation = Rotation::ROTATION_180;
872 break;
873 case 270: // Rotation 270 degree
874 targetRotation = Rotation::ROTATION_270;
875 break;
876 default:
877 targetRotation = Rotation::ROTATION_0;
878 break;
879 }
880 return targetRotation;
881 }
882
SetUpdateToInputManagerCallback(std::function<void (float)> updateToInputManagerCallback)883 void ScreenSession::SetUpdateToInputManagerCallback(std::function<void(float)> updateToInputManagerCallback)
884 {
885 updateToInputManagerCallback_ = updateToInputManagerCallback;
886 }
887
SetUpdateScreenPivotCallback(std::function<void (float,float)> && updateScreenPivotCallback)888 void ScreenSession::SetUpdateScreenPivotCallback(std::function<void(float, float)>&& updateScreenPivotCallback)
889 {
890 updateScreenPivotCallback_ = std::move(updateScreenPivotCallback);
891 }
892
GetVirtualScreenFlag()893 VirtualScreenFlag ScreenSession::GetVirtualScreenFlag()
894 {
895 return screenFlag_;
896 }
897
SetVirtualScreenFlag(VirtualScreenFlag screenFlag)898 void ScreenSession::SetVirtualScreenFlag(VirtualScreenFlag screenFlag)
899 {
900 screenFlag_ = screenFlag;
901 }
902
UpdateTouchBoundsAndOffset()903 void ScreenSession::UpdateTouchBoundsAndOffset()
904 {
905 property_.SetPhysicalTouchBounds();
906 property_.SetInputOffsetY();
907 if (FoldScreenStateInternel::IsSecondaryDisplayFoldDevice()) {
908 property_.SetValidHeight(property_.GetBounds().rect_.GetHeight());
909 property_.SetValidWidth(property_.GetBounds().rect_.GetWidth());
910 }
911 }
912
UpdateToInputManager(RRect bounds,int rotation,int deviceRotation,FoldDisplayMode foldDisplayMode,bool isChanged)913 void ScreenSession::UpdateToInputManager(RRect bounds, int rotation, int deviceRotation,
914 FoldDisplayMode foldDisplayMode, bool isChanged)
915 {
916 OptimizeSecondaryDisplayMode(bounds, foldDisplayMode);
917 bool needUpdateToInputManager = false;
918 if (foldDisplayMode == FoldDisplayMode::FULL &&
919 property_.GetBounds() == bounds && property_.GetRotation() != static_cast<float>(rotation)) {
920 needUpdateToInputManager = true;
921 }
922 Rotation targetRotation = ConvertIntToRotation(rotation);
923 DisplayOrientation displayOrientation = CalcDisplayOrientation(targetRotation, foldDisplayMode, isChanged);
924 property_.SetBounds(bounds);
925 property_.SetRotation(static_cast<float>(rotation));
926 property_.UpdateScreenRotation(targetRotation);
927 property_.SetDisplayOrientation(displayOrientation);
928 UpdateTouchBoundsAndOffset();
929 Rotation targetDeviceRotation = ConvertIntToRotation(deviceRotation);
930 DisplayOrientation deviceOrientation = CalcDeviceOrientation(targetDeviceRotation, foldDisplayMode, isChanged);
931 property_.UpdateDeviceRotation(targetDeviceRotation);
932 property_.SetDeviceOrientation(deviceOrientation);
933 if (needUpdateToInputManager && updateToInputManagerCallback_ != nullptr
934 && g_screenRotationOffSet == ROTATION_270) {
935 // fold phone need fix 90 degree by remainder 360 degree
936 int foldRotation = (rotation + 90) % 360;
937 updateToInputManagerCallback_(static_cast<float>(foldRotation));
938 TLOGI(WmsLogTag::DMS, "updateToInputManagerCallback_:%{public}d", foldRotation);
939 }
940 }
941
SetPhysicalRotation(int rotation)942 void ScreenSession::SetPhysicalRotation(int rotation)
943 {
944 property_.SetPhysicalRotation(static_cast<float>(rotation));
945 TLOGI(WmsLogTag::DMS, "physicalrotation :%{public}f", property_.GetPhysicalRotation());
946 }
947
SetScreenComponentRotation(int rotation)948 void ScreenSession::SetScreenComponentRotation(int rotation)
949 {
950 property_.SetScreenComponentRotation(static_cast<float>(rotation));
951 TLOGI(WmsLogTag::DMS, "screenComponentRotation :%{public}f ", property_.GetScreenComponentRotation());
952 }
953
OptimizeSecondaryDisplayMode(const RRect & bounds,FoldDisplayMode & foldDisplayMode)954 void ScreenSession::OptimizeSecondaryDisplayMode(const RRect &bounds, FoldDisplayMode &foldDisplayMode)
955 {
956 if (!FoldScreenStateInternel::IsSecondaryDisplayFoldDevice()) {
957 return;
958 }
959 if (IsWidthHeightMatch(bounds.rect_.GetWidth(), bounds.rect_.GetHeight(),
960 MAIN_STATUS_WIDTH, SCREEN_HEIGHT)) {
961 foldDisplayMode = FoldDisplayMode::MAIN;
962 } else if (IsWidthHeightMatch(bounds.rect_.GetWidth(), bounds.rect_.GetHeight(),
963 FULL_STATUS_WIDTH, SCREEN_HEIGHT)) {
964 foldDisplayMode = FoldDisplayMode::FULL;
965 } else if (IsWidthHeightMatch(bounds.rect_.GetWidth(), bounds.rect_.GetHeight(),
966 GLOBAL_FULL_STATUS_WIDTH, SCREEN_HEIGHT)) {
967 foldDisplayMode = FoldDisplayMode::GLOBAL_FULL;
968 }
969 }
970
UpdatePropertyAfterRotation(RRect bounds,int rotation,FoldDisplayMode foldDisplayMode,bool isChanged)971 void ScreenSession::UpdatePropertyAfterRotation(RRect bounds, int rotation,
972 FoldDisplayMode foldDisplayMode, bool isChanged)
973 {
974 OptimizeSecondaryDisplayMode(bounds, foldDisplayMode);
975 Rotation targetRotation = ConvertIntToRotation(rotation);
976 DisplayOrientation displayOrientation = CalcDisplayOrientation(targetRotation, foldDisplayMode, isChanged);
977 property_.SetBounds(bounds);
978 property_.SetRotation(static_cast<float>(rotation));
979 property_.UpdateScreenRotation(targetRotation);
980 property_.SetDisplayOrientation(displayOrientation);
981 if (!isBScreenHalf_ || property_.GetIsFakeInUse()) {
982 property_.SetValidHeight(bounds.rect_.GetHeight());
983 property_.SetValidWidth(bounds.rect_.GetWidth());
984 }
985 UpdateTouchBoundsAndOffset();
986 {
987 std::shared_lock<std::shared_mutex> displayNodeLock(displayNodeMutex_);
988 if (!displayNode_) {
989 TLOGI(WmsLogTag::DMS, "update failed since null display node with \
990 rotation:%{public}d displayOrientation:%{public}u", rotation, displayOrientation);
991 return;
992 }
993 }
994 auto transactionProxy = RSTransactionProxy::GetInstance();
995 if (transactionProxy != nullptr) {
996 transactionProxy->Begin();
997 {
998 std::shared_lock<std::shared_mutex> displayNodeLock(displayNodeMutex_);
999 displayNode_->SetScreenRotation(static_cast<uint32_t>(property_.GetDeviceRotation()));
1000 }
1001 transactionProxy->Commit();
1002 } else {
1003 {
1004 std::shared_lock<std::shared_mutex> displayNodeLock(displayNodeMutex_);
1005 displayNode_->SetScreenRotation(static_cast<uint32_t>(property_.GetDeviceRotation()));
1006 }
1007 }
1008 TLOGI(WmsLogTag::DMS, "bounds:[%{public}f %{public}f %{public}f %{public}f],rotation:%{public}d,\
1009 displayOrientation:%{public}u, foldDisplayMode:%{public}u",
1010 property_.GetBounds().rect_.GetLeft(), property_.GetBounds().rect_.GetTop(),
1011 property_.GetBounds().rect_.GetWidth(), property_.GetBounds().rect_.GetHeight(),
1012 rotation, displayOrientation, foldDisplayMode);
1013 ReportNotifyModeChange(displayOrientation);
1014 }
1015
UpdatePropertyOnly(RRect bounds,int rotation,FoldDisplayMode foldDisplayMode,bool isChanged)1016 void ScreenSession::UpdatePropertyOnly(RRect bounds, int rotation, FoldDisplayMode foldDisplayMode, bool isChanged)
1017 {
1018 OptimizeSecondaryDisplayMode(bounds, foldDisplayMode);
1019 Rotation targetRotation = ConvertIntToRotation(rotation);
1020 DisplayOrientation displayOrientation = CalcDisplayOrientation(targetRotation, foldDisplayMode, isChanged);
1021 property_.SetBounds(bounds);
1022 property_.SetRotation(static_cast<float>(rotation));
1023 property_.UpdateScreenRotation(targetRotation);
1024 property_.SetDisplayOrientation(displayOrientation);
1025 if (!isBScreenHalf_ || property_.GetIsFakeInUse()) {
1026 property_.SetValidHeight(bounds.rect_.GetHeight());
1027 property_.SetValidWidth(bounds.rect_.GetWidth());
1028 }
1029 UpdateTouchBoundsAndOffset();
1030 TLOGI(WmsLogTag::DMS, "bounds:[%{public}f %{public}f %{public}f %{public}f],\
1031 rotation:%{public}d, displayOrientation:%{public}u",
1032 property_.GetBounds().rect_.GetLeft(), property_.GetBounds().rect_.GetTop(),
1033 property_.GetBounds().rect_.GetWidth(), property_.GetBounds().rect_.GetHeight(),
1034 rotation, displayOrientation);
1035 }
1036
UpdateRotationOrientation(int rotation,FoldDisplayMode foldDisplayMode,bool isChanged)1037 void ScreenSession::UpdateRotationOrientation(int rotation, FoldDisplayMode foldDisplayMode, bool isChanged)
1038 {
1039 OptimizeSecondaryDisplayMode(property_.GetBounds(), foldDisplayMode);
1040 Rotation targetRotation = ConvertIntToRotation(rotation);
1041 DisplayOrientation deviceOrientation = CalcDeviceOrientation(targetRotation, foldDisplayMode, isChanged);
1042 property_.UpdateDeviceRotation(targetRotation);
1043 property_.SetDeviceOrientation(deviceOrientation);
1044 TLOGI(WmsLogTag::DMS, "rotation:%{public}d, orientation:%{public}u", rotation, deviceOrientation);
1045 }
1046
UpdatePropertyByFakeInUse(bool isFakeInUse)1047 void ScreenSession::UpdatePropertyByFakeInUse(bool isFakeInUse)
1048 {
1049 property_.SetIsFakeInUse(isFakeInUse);
1050 }
1051
ReportNotifyModeChange(DisplayOrientation displayOrientation)1052 void ScreenSession::ReportNotifyModeChange(DisplayOrientation displayOrientation)
1053 {
1054 int32_t vhMode = 1;
1055 if (displayOrientation == DisplayOrientation::PORTRAIT_INVERTED ||
1056 displayOrientation == DisplayOrientation::PORTRAIT) {
1057 vhMode = 0;
1058 }
1059 int32_t ret = HiSysEventWrite(
1060 OHOS::HiviewDFX::HiSysEvent::Domain::WINDOW_MANAGER,
1061 "VH_MODE",
1062 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
1063 "MODE", vhMode);
1064 if (ret != 0) {
1065 TLOGE(WmsLogTag::DMS, "ReportNotifyModeChange Write HiSysEvent error, ret: %{public}d", ret);
1066 }
1067 }
1068
UpdateRotationAfterBoot(bool foldToExpand)1069 void ScreenSession::UpdateRotationAfterBoot(bool foldToExpand)
1070 {
1071 TLOGI(WmsLogTag::DMS, "foldToExpand: %{public}d, Rotation: %{public}f",
1072 static_cast<int32_t>(foldToExpand), currentSensorRotation_);
1073 if (foldToExpand) {
1074 SensorRotationChange(currentSensorRotation_);
1075 }
1076 }
1077
UpdateValidRotationToScb()1078 void ScreenSession::UpdateValidRotationToScb()
1079 {
1080 TLOGI(WmsLogTag::DMS, "Rotation: %{public}f", currentValidSensorRotation_);
1081 SensorRotationChange(currentValidSensorRotation_);
1082 }
1083
GetActiveScreenMode() const1084 sptr<SupportedScreenModes> ScreenSession::GetActiveScreenMode() const
1085 {
1086 if (activeIdx_ < 0 || activeIdx_ >= static_cast<int32_t>(modes_.size())) {
1087 TLOGW(WmsLogTag::DMS, "SCB: active mode index is wrong: %{public}d", activeIdx_);
1088 return nullptr;
1089 }
1090 return modes_[activeIdx_];
1091 }
1092
GetOrientation() const1093 Orientation ScreenSession::GetOrientation() const
1094 {
1095 return property_.GetOrientation();
1096 }
1097
SetOrientation(Orientation orientation)1098 void ScreenSession::SetOrientation(Orientation orientation)
1099 {
1100 property_.SetOrientation(orientation);
1101 }
1102
GetRotation() const1103 Rotation ScreenSession::GetRotation() const
1104 {
1105 return property_.GetScreenRotation();
1106 }
1107
SetRotation(Rotation rotation)1108 void ScreenSession::SetRotation(Rotation rotation)
1109 {
1110 property_.SetScreenRotation(rotation);
1111 }
1112
SetRotationAndScreenRotationOnly(Rotation rotation)1113 void ScreenSession::SetRotationAndScreenRotationOnly(Rotation rotation)
1114 {
1115 property_.SetRotationAndScreenRotationOnly(rotation);
1116 }
1117
SetScreenRequestedOrientation(Orientation orientation)1118 void ScreenSession::SetScreenRequestedOrientation(Orientation orientation)
1119 {
1120 property_.SetScreenRequestedOrientation(orientation);
1121 }
1122
SetScreenRotationLocked(bool isLocked)1123 void ScreenSession::SetScreenRotationLocked(bool isLocked)
1124 {
1125 std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
1126 isScreenLocked_ = isLocked;
1127 if (screenChangeListenerList_.empty()) {
1128 TLOGE(WmsLogTag::DMS, "screenChangeListenerList is empty.");
1129 return;
1130 }
1131 for (auto& listener : screenChangeListenerList_) {
1132 if (!listener) {
1133 TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
1134 continue;
1135 }
1136 listener->OnScreenRotationLockedChange(isLocked, screenId_);
1137 }
1138 }
1139
SetScreenRotationLockedFromJs(bool isLocked)1140 void ScreenSession::SetScreenRotationLockedFromJs(bool isLocked)
1141 {
1142 isScreenLocked_ = isLocked;
1143 }
1144
IsScreenRotationLocked()1145 bool ScreenSession::IsScreenRotationLocked()
1146 {
1147 return isScreenLocked_;
1148 }
1149
SetTouchEnabledFromJs(bool isTouchEnabled)1150 void ScreenSession::SetTouchEnabledFromJs(bool isTouchEnabled)
1151 {
1152 TLOGI(WmsLogTag::WMS_EVENT, "%{public}u", isTouchEnabled);
1153 touchEnabled_.store(isTouchEnabled);
1154 }
1155
IsTouchEnabled()1156 bool ScreenSession::IsTouchEnabled()
1157 {
1158 return touchEnabled_.load();
1159 }
1160
GetScreenRequestedOrientation() const1161 Orientation ScreenSession::GetScreenRequestedOrientation() const
1162 {
1163 return property_.GetScreenRequestedOrientation();
1164 }
1165
SetVirtualPixelRatio(float virtualPixelRatio)1166 void ScreenSession::SetVirtualPixelRatio(float virtualPixelRatio)
1167 {
1168 property_.SetVirtualPixelRatio(virtualPixelRatio);
1169 }
1170
SetScreenSceneDpiChangeListener(const SetScreenSceneDpiFunc & func)1171 void ScreenSession::SetScreenSceneDpiChangeListener(const SetScreenSceneDpiFunc& func)
1172 {
1173 setScreenSceneDpiCallback_ = func;
1174 TLOGI(WmsLogTag::DMS, "SetScreenSceneDpiChangeListener");
1175 }
1176
SetScreenSceneDpi(float density)1177 void ScreenSession::SetScreenSceneDpi(float density)
1178 {
1179 if (setScreenSceneDpiCallback_ == nullptr) {
1180 TLOGI(WmsLogTag::DMS, "setScreenSceneDpiCallback_ is nullptr");
1181 return;
1182 }
1183 setScreenSceneDpiCallback_(density);
1184 }
1185
SetScreenSceneDestroyListener(const DestroyScreenSceneFunc & func)1186 void ScreenSession::SetScreenSceneDestroyListener(const DestroyScreenSceneFunc& func)
1187 {
1188 destroyScreenSceneCallback_ = func;
1189 TLOGI(WmsLogTag::DMS, "SetScreenSceneDestroyListener");
1190 }
1191
DestroyScreenScene()1192 void ScreenSession::DestroyScreenScene()
1193 {
1194 if (destroyScreenSceneCallback_ == nullptr) {
1195 TLOGI(WmsLogTag::DMS, "destroyScreenSceneCallback_ is nullptr");
1196 return;
1197 }
1198 destroyScreenSceneCallback_();
1199 }
1200
SetDensityInCurResolution(float densityInCurResolution)1201 void ScreenSession::SetDensityInCurResolution(float densityInCurResolution)
1202 {
1203 property_.SetDensityInCurResolution(densityInCurResolution);
1204 }
1205
SetDefaultDensity(float defaultDensity)1206 void ScreenSession::SetDefaultDensity(float defaultDensity)
1207 {
1208 property_.SetDefaultDensity(defaultDensity);
1209 }
1210
UpdateVirtualPixelRatio(const RRect & bounds)1211 void ScreenSession::UpdateVirtualPixelRatio(const RRect& bounds)
1212 {
1213 property_.UpdateVirtualPixelRatio(bounds);
1214 }
1215
SetScreenType(ScreenType type)1216 void ScreenSession::SetScreenType(ScreenType type)
1217 {
1218 property_.SetScreenType(type);
1219 }
1220
CalcRotation(Orientation orientation,FoldDisplayMode foldDisplayMode) const1221 Rotation ScreenSession::CalcRotation(Orientation orientation, FoldDisplayMode foldDisplayMode) const
1222 {
1223 sptr<SupportedScreenModes> info = GetActiveScreenMode();
1224 if (info == nullptr) {
1225 return Rotation::ROTATION_0;
1226 }
1227 // vertical: phone(Plugin screen); horizontal: pad & external screen
1228 bool isVerticalScreen = info->width_ < info->height_;
1229 if (foldDisplayMode != FoldDisplayMode::UNKNOWN &&
1230 (g_screenRotationOffSet == ROTATION_90 || g_screenRotationOffSet == ROTATION_270)) {
1231 isVerticalScreen = info->width_ > info->height_;
1232 }
1233 switch (orientation) {
1234 case Orientation::UNSPECIFIED: {
1235 return Rotation::ROTATION_0;
1236 }
1237 case Orientation::VERTICAL: {
1238 return isVerticalScreen ? Rotation::ROTATION_0 : Rotation::ROTATION_90;
1239 }
1240 case Orientation::HORIZONTAL: {
1241 return isVerticalScreen ? Rotation::ROTATION_90 : Rotation::ROTATION_0;
1242 }
1243 case Orientation::REVERSE_VERTICAL: {
1244 return isVerticalScreen ? Rotation::ROTATION_180 : Rotation::ROTATION_270;
1245 }
1246 case Orientation::REVERSE_HORIZONTAL: {
1247 return isVerticalScreen ? Rotation::ROTATION_270 : Rotation::ROTATION_180;
1248 }
1249 default: {
1250 TLOGE(WmsLogTag::DMS, "unknown orientation %{public}u", orientation);
1251 return Rotation::ROTATION_0;
1252 }
1253 }
1254 }
1255
CalcDisplayOrientation(Rotation rotation,FoldDisplayMode foldDisplayMode,bool IsOrientationNeedChanged) const1256 DisplayOrientation ScreenSession::CalcDisplayOrientation(Rotation rotation,
1257 FoldDisplayMode foldDisplayMode, bool IsOrientationNeedChanged) const
1258 {
1259 // vertical: phone(Plugin screen); horizontal: pad & external screen
1260 bool isVerticalScreen = property_.GetPhyWidth() < property_.GetPhyHeight();
1261 if (g_screenScanType == SCAN_TYPE_VERTICAL) {
1262 isVerticalScreen = false;
1263 }
1264 if (foldDisplayMode != FoldDisplayMode::UNKNOWN
1265 && (g_screenRotationOffSet == ROTATION_90 || g_screenRotationOffSet == ROTATION_270)) {
1266 TLOGD(WmsLogTag::DMS, "foldDisplay is verticalScreen when width is greater than height");
1267 isVerticalScreen = property_.GetPhyWidth() > property_.GetPhyHeight();
1268 }
1269 if (IsOrientationNeedChanged) {
1270 if (FoldScreenStateInternel::IsSingleDisplayPocketFoldDevice()) {
1271 uint32_t temp = (static_cast<uint32_t>(rotation) + SECONDARY_ROTATION_90) % SECONDARY_ROTATION_MOD;
1272 rotation = static_cast<Rotation>(temp);
1273 } else {
1274 uint32_t temp = (static_cast<uint32_t>(rotation) + SECONDARY_ROTATION_270) % SECONDARY_ROTATION_MOD;
1275 rotation = static_cast<Rotation>(temp);
1276 }
1277 isVerticalScreen = true;
1278 }
1279 switch (rotation) {
1280 case Rotation::ROTATION_0: {
1281 return isVerticalScreen ? DisplayOrientation::PORTRAIT : DisplayOrientation::LANDSCAPE;
1282 }
1283 case Rotation::ROTATION_90: {
1284 return isVerticalScreen ? DisplayOrientation::LANDSCAPE : DisplayOrientation::PORTRAIT;
1285 }
1286 case Rotation::ROTATION_180: {
1287 return isVerticalScreen ? DisplayOrientation::PORTRAIT_INVERTED : DisplayOrientation::LANDSCAPE_INVERTED;
1288 }
1289 case Rotation::ROTATION_270: {
1290 return isVerticalScreen ? DisplayOrientation::LANDSCAPE_INVERTED : DisplayOrientation::PORTRAIT_INVERTED;
1291 }
1292 default: {
1293 TLOGE(WmsLogTag::DMS, "unknown rotation %{public}u", rotation);
1294 return DisplayOrientation::UNKNOWN;
1295 }
1296 }
1297 }
1298
CalcDeviceOrientation(Rotation rotation,FoldDisplayMode foldDisplayMode,bool IsOrientationNeedChanged) const1299 DisplayOrientation ScreenSession::CalcDeviceOrientation(Rotation rotation,
1300 FoldDisplayMode foldDisplayMode, bool IsOrientationNeedChanged) const
1301 {
1302 if (IsOrientationNeedChanged) {
1303 if (FoldScreenStateInternel::IsSingleDisplayPocketFoldDevice()) {
1304 uint32_t temp = (static_cast<uint32_t>(rotation) + SECONDARY_ROTATION_90) % SECONDARY_ROTATION_MOD;
1305 rotation = static_cast<Rotation>(temp);
1306 } else {
1307 uint32_t temp = (static_cast<uint32_t>(rotation) + SECONDARY_ROTATION_270) % SECONDARY_ROTATION_MOD;
1308 rotation = static_cast<Rotation>(temp);
1309 }
1310 }
1311 DisplayOrientation displayRotation = DisplayOrientation::UNKNOWN;
1312 switch (rotation) {
1313 case Rotation::ROTATION_0: {
1314 displayRotation = DisplayOrientation::PORTRAIT;
1315 break;
1316 }
1317 case Rotation::ROTATION_90: {
1318 displayRotation = DisplayOrientation::LANDSCAPE;
1319 break;
1320 }
1321 case Rotation::ROTATION_180: {
1322 displayRotation = DisplayOrientation::PORTRAIT_INVERTED;
1323 break;
1324 }
1325 case Rotation::ROTATION_270: {
1326 displayRotation = DisplayOrientation::LANDSCAPE_INVERTED;
1327 break;
1328 }
1329 default: {
1330 TLOGE(WmsLogTag::DMS, "unknown rotation %{public}u", rotation);
1331 }
1332 }
1333 return displayRotation;
1334 }
1335
GetSourceMode() const1336 ScreenSourceMode ScreenSession::GetSourceMode() const
1337 {
1338 if (!isPcUse_ && screenId_ == defaultScreenId_) {
1339 return ScreenSourceMode::SCREEN_MAIN;
1340 }
1341 ScreenCombination combination = GetScreenCombination();
1342 switch (combination) {
1343 case ScreenCombination::SCREEN_MAIN: {
1344 return ScreenSourceMode::SCREEN_MAIN;
1345 }
1346 case ScreenCombination::SCREEN_MIRROR: {
1347 return ScreenSourceMode::SCREEN_MIRROR;
1348 }
1349 case ScreenCombination::SCREEN_EXPAND: {
1350 return ScreenSourceMode::SCREEN_EXTEND;
1351 }
1352 case ScreenCombination::SCREEN_ALONE: {
1353 return ScreenSourceMode::SCREEN_ALONE;
1354 }
1355 case ScreenCombination::SCREEN_UNIQUE: {
1356 return ScreenSourceMode::SCREEN_UNIQUE;
1357 }
1358 case ScreenCombination::SCREEN_EXTEND: {
1359 return ScreenSourceMode::SCREEN_EXTEND;
1360 }
1361 default: {
1362 return ScreenSourceMode::SCREEN_ALONE;
1363 }
1364 }
1365 }
1366
SetScreenCombination(ScreenCombination combination)1367 void ScreenSession::SetScreenCombination(ScreenCombination combination)
1368 {
1369 TLOGI(WmsLogTag::DMS, "screenId:%{public}" PRIu64", set combination:%{public}d", screenId_,
1370 static_cast<int32_t>(combination));
1371 combination_ = combination;
1372 }
1373
GetScreenCombination() const1374 ScreenCombination ScreenSession::GetScreenCombination() const
1375 {
1376 return combination_;
1377 }
1378
GetDisplaySourceMode() const1379 DisplaySourceMode ScreenSession::GetDisplaySourceMode() const
1380 {
1381 if (!isPcUse_ && screenId_ == defaultScreenId_) {
1382 return DisplaySourceMode::MAIN;
1383 }
1384 ScreenCombination combination = GetScreenCombination();
1385 switch (combination) {
1386 case ScreenCombination::SCREEN_MAIN: {
1387 return DisplaySourceMode::MAIN;
1388 }
1389 case ScreenCombination::SCREEN_MIRROR: {
1390 return DisplaySourceMode::MIRROR;
1391 }
1392 case ScreenCombination::SCREEN_EXPAND: {
1393 return DisplaySourceMode::EXTEND;
1394 }
1395 case ScreenCombination::SCREEN_EXTEND: {
1396 return DisplaySourceMode::EXTEND;
1397 }
1398 case ScreenCombination::SCREEN_UNIQUE: {
1399 return DisplaySourceMode::ALONE;
1400 }
1401 case ScreenCombination::SCREEN_ALONE: {
1402 return DisplaySourceMode::NONE;
1403 }
1404 default: {
1405 return DisplaySourceMode::NONE;
1406 }
1407 }
1408 }
1409
FillScreenInfo(sptr<ScreenInfo> info) const1410 void ScreenSession::FillScreenInfo(sptr<ScreenInfo> info) const
1411 {
1412 if (info == nullptr) {
1413 TLOGE(WmsLogTag::DMS, "FillScreenInfo failed! info is nullptr");
1414 return;
1415 }
1416 info->SetScreenId(screenId_);
1417 info->SetName(name_);
1418 info->SetIsExtend(GetIsExtend());
1419 uint32_t width = 0;
1420 uint32_t height = 0;
1421 if (isPcUse_) {
1422 RRect bounds = property_.GetBounds();
1423 width = bounds.rect_.GetWidth();
1424 height = bounds.rect_.GetHeight();
1425 } else {
1426 sptr<SupportedScreenModes> screenSessionModes = GetActiveScreenMode();
1427 if (screenSessionModes != nullptr) {
1428 height = screenSessionModes->height_;
1429 width = screenSessionModes->width_;
1430 }
1431 }
1432
1433 float virtualPixelRatio = property_.GetVirtualPixelRatio();
1434 // "< 1e-set6" means virtualPixelRatio is 0.
1435 if (fabsf(virtualPixelRatio) < 1e-6) {
1436 virtualPixelRatio = 1.0f;
1437 }
1438 ScreenSourceMode sourceMode = GetSourceMode();
1439 info->SetVirtualPixelRatio(property_.GetVirtualPixelRatio());
1440 info->SetVirtualHeight(height / virtualPixelRatio);
1441 info->SetVirtualWidth(width / virtualPixelRatio);
1442 info->SetRotation(property_.GetScreenRotation());
1443 info->SetOrientation(static_cast<Orientation>(property_.GetDisplayOrientation()));
1444 info->SetSourceMode(sourceMode);
1445 info->SetType(property_.GetScreenType());
1446 info->SetModeId(activeIdx_);
1447 info->SetSerialNumber(serialNumber_);
1448
1449 info->lastParent_ = lastGroupSmsId_;
1450 info->parent_ = groupSmsId_;
1451 info->isScreenGroup_ = isScreenGroup_;
1452 info->modes_ = modes_;
1453 }
1454
ConvertToScreenInfo() const1455 sptr<ScreenInfo> ScreenSession::ConvertToScreenInfo() const
1456 {
1457 sptr<ScreenInfo> info = new(std::nothrow) ScreenInfo();
1458 if (info == nullptr) {
1459 return nullptr;
1460 }
1461 FillScreenInfo(info);
1462 return info;
1463 }
1464
GetScreenColorGamut(ScreenColorGamut & colorGamut)1465 DMError ScreenSession::GetScreenColorGamut(ScreenColorGamut& colorGamut)
1466 {
1467 #ifdef WM_SCREEN_COLOR_GAMUT_ENABLE
1468 auto ret = RSInterfaces::GetInstance().GetScreenColorGamut(rsId_, colorGamut);
1469 if (ret != StatusCode::SUCCESS) {
1470 TLOGE(WmsLogTag::DMS, "GetScreenColorGamut fail! rsId %{public}" PRIu64"", rsId_);
1471 return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
1472 }
1473 TLOGI(WmsLogTag::DMS, "GetScreenColorGamut ok! rsId %{public}" PRIu64", colorGamut %{public}u",
1474 rsId_, static_cast<uint32_t>(colorGamut));
1475 #endif
1476 return DMError::DM_OK;
1477 }
1478
SetScreenColorGamut(int32_t colorGamutIdx)1479 DMError ScreenSession::SetScreenColorGamut(int32_t colorGamutIdx)
1480 {
1481 #ifdef WM_SCREEN_COLOR_GAMUT_ENABLE
1482 std::vector<ScreenColorGamut> colorGamuts;
1483 DMError res = GetScreenSupportedColorGamuts(colorGamuts);
1484 if (res != DMError::DM_OK) {
1485 TLOGE(WmsLogTag::DMS, "SetScreenColorGamut fail! rsId %{public}" PRIu64"", rsId_);
1486 return res;
1487 }
1488 if (colorGamutIdx < 0 || colorGamutIdx >= static_cast<int32_t>(colorGamuts.size())) {
1489 TLOGE(WmsLogTag::DMS, "SetScreenColorGamut fail! rsId %{public}" PRIu64" colorGamutIdx %{public}d invalid.",
1490 rsId_, colorGamutIdx);
1491 return DMError::DM_ERROR_INVALID_PARAM;
1492 }
1493 auto ret = RSInterfaces::GetInstance().SetScreenColorGamut(rsId_, colorGamutIdx);
1494 if (ret != StatusCode::SUCCESS) {
1495 TLOGE(WmsLogTag::DMS, "SetScreenColorGamut fail! rsId %{public}" PRIu64"", rsId_);
1496 return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
1497 }
1498 TLOGI(WmsLogTag::DMS, "SetScreenColorGamut ok! rsId %{public}" PRIu64", colorGamutIdx %{public}u",
1499 rsId_, colorGamutIdx);
1500 #endif
1501 return DMError::DM_OK;
1502 }
1503
GetScreenGamutMap(ScreenGamutMap & gamutMap)1504 DMError ScreenSession::GetScreenGamutMap(ScreenGamutMap& gamutMap)
1505 {
1506 #ifdef WM_SCREEN_COLOR_GAMUT_ENABLE
1507 auto ret = RSInterfaces::GetInstance().GetScreenGamutMap(rsId_, gamutMap);
1508 if (ret != StatusCode::SUCCESS) {
1509 TLOGE(WmsLogTag::DMS, "GetScreenGamutMap fail! rsId %{public}" PRIu64"", rsId_);
1510 return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
1511 }
1512 TLOGI(WmsLogTag::DMS, "GetScreenGamutMap ok! rsId %{public}" PRIu64", gamutMap %{public}u",
1513 rsId_, static_cast<uint32_t>(gamutMap));
1514 #endif
1515 return DMError::DM_OK;
1516 }
1517
SetScreenGamutMap(ScreenGamutMap gamutMap)1518 DMError ScreenSession::SetScreenGamutMap(ScreenGamutMap gamutMap)
1519 {
1520 #ifdef WM_SCREEN_COLOR_GAMUT_ENABLE
1521 if (gamutMap > GAMUT_MAP_HDR_EXTENSION) {
1522 return DMError::DM_ERROR_INVALID_PARAM;
1523 }
1524 auto ret = RSInterfaces::GetInstance().SetScreenGamutMap(rsId_, gamutMap);
1525 if (ret != StatusCode::SUCCESS) {
1526 TLOGE(WmsLogTag::DMS, "SetScreenGamutMap fail! rsId %{public}" PRIu64"", rsId_);
1527 return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
1528 }
1529 TLOGI(WmsLogTag::DMS, "SetScreenGamutMap ok! rsId %{public}" PRIu64", gamutMap %{public}u",
1530 rsId_, static_cast<uint32_t>(gamutMap));
1531 #endif
1532 return DMError::DM_OK;
1533 }
1534
SetScreenColorTransform()1535 DMError ScreenSession::SetScreenColorTransform()
1536 {
1537 TLOGI(WmsLogTag::DMS, "SetScreenColorTransform ok! rsId %{public}" PRIu64"", rsId_);
1538 return DMError::DM_OK;
1539 }
1540
GetPixelFormat(GraphicPixelFormat & pixelFormat)1541 DMError ScreenSession::GetPixelFormat(GraphicPixelFormat& pixelFormat)
1542 {
1543 auto ret = RSInterfaces::GetInstance().GetPixelFormat(rsId_, pixelFormat);
1544 if (ret != StatusCode::SUCCESS) {
1545 TLOGE(WmsLogTag::DMS, "GetPixelFormat fail! rsId %{public}" PRIu64, rsId_);
1546 return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
1547 }
1548 TLOGI(WmsLogTag::DMS, "GetPixelFormat ok! rsId %{public}" PRIu64 ", pixelFormat %{public}u",
1549 rsId_, static_cast<uint32_t>(pixelFormat));
1550 return DMError::DM_OK;
1551 }
1552
SetPixelFormat(GraphicPixelFormat pixelFormat)1553 DMError ScreenSession::SetPixelFormat(GraphicPixelFormat pixelFormat)
1554 {
1555 if (pixelFormat > GRAPHIC_PIXEL_FMT_VENDER_MASK) {
1556 return DMError::DM_ERROR_INVALID_PARAM;
1557 }
1558 auto ret = RSInterfaces::GetInstance().SetPixelFormat(rsId_, pixelFormat);
1559 if (ret != StatusCode::SUCCESS) {
1560 TLOGE(WmsLogTag::DMS, "SetPixelFormat fail! rsId %{public}" PRIu64, rsId_);
1561 return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
1562 }
1563 TLOGI(WmsLogTag::DMS, "SetPixelFormat ok! rsId %{public}" PRIu64 ", gamutMap %{public}u",
1564 rsId_, static_cast<uint32_t>(pixelFormat));
1565 return DMError::DM_OK;
1566 }
1567
GetSupportedHDRFormats(std::vector<ScreenHDRFormat> & hdrFormats)1568 DMError ScreenSession::GetSupportedHDRFormats(std::vector<ScreenHDRFormat>& hdrFormats)
1569 {
1570 #ifdef WM_SCREEN_HDR_FORMAT_ENABLE
1571 auto ret = RSInterfaces::GetInstance().GetScreenSupportedHDRFormats(rsId_, hdrFormats);
1572 if (ret != StatusCode::SUCCESS) {
1573 TLOGE(WmsLogTag::DMS, "SCB: fail! rsId %{public}" PRIu64 ", ret:%{public}d",
1574 rsId_, ret);
1575 return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
1576 }
1577 TLOGI(WmsLogTag::DMS, "SCB: ok! rsId %{public}" PRIu64 ", size %{public}u",
1578 rsId_, static_cast<uint32_t>(hdrFormats.size()));
1579 #endif
1580 return DMError::DM_OK;
1581 }
1582
GetScreenHDRFormat(ScreenHDRFormat & hdrFormat)1583 DMError ScreenSession::GetScreenHDRFormat(ScreenHDRFormat& hdrFormat)
1584 {
1585 #ifdef WM_SCREEN_HDR_FORMAT_ENABLE
1586 auto ret = RSInterfaces::GetInstance().GetScreenHDRFormat(rsId_, hdrFormat);
1587 if (ret != StatusCode::SUCCESS) {
1588 TLOGE(WmsLogTag::DMS, "GetScreenHDRFormat fail! rsId %{public}" PRIu64, rsId_);
1589 return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
1590 }
1591 TLOGI(WmsLogTag::DMS, "GetScreenHDRFormat ok! rsId %{public}" PRIu64 ", colorSpace %{public}u",
1592 rsId_, static_cast<uint32_t>(hdrFormat));
1593 #endif
1594 return DMError::DM_OK;
1595 }
1596
SetScreenHDRFormat(int32_t modeIdx)1597 DMError ScreenSession::SetScreenHDRFormat(int32_t modeIdx)
1598 {
1599 #ifdef WM_SCREEN_HDR_FORMAT_ENABLE
1600 std::vector<ScreenHDRFormat> hdrFormats;
1601 DMError res = GetSupportedHDRFormats(hdrFormats);
1602 if (res != DMError::DM_OK) {
1603 TLOGE(WmsLogTag::DMS, "SetScreenHDRFormat fail! rsId %{public}" PRIu64, rsId_);
1604 return res;
1605 }
1606 if (modeIdx < 0 || modeIdx >= static_cast<int32_t>(hdrFormats.size())) {
1607 TLOGE(WmsLogTag::DMS, "SetScreenHDRFormat fail! rsId %{public}" PRIu64 " modeIdx %{public}d invalid.",
1608 rsId_, modeIdx);
1609 return DMError::DM_ERROR_INVALID_PARAM;
1610 }
1611 auto ret = RSInterfaces::GetInstance().SetScreenHDRFormat(rsId_, modeIdx);
1612 if (ret != StatusCode::SUCCESS) {
1613 TLOGE(WmsLogTag::DMS, "SetScreenHDRFormat fail! rsId %{public}" PRIu64, rsId_);
1614 return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
1615 }
1616 TLOGI(WmsLogTag::DMS, "SetScreenHDRFormat ok! rsId %{public}" PRIu64 ", modeIdx %{public}d",
1617 rsId_, modeIdx);
1618 #endif
1619 return DMError::DM_OK;
1620 }
1621
GetSupportedColorSpaces(std::vector<GraphicCM_ColorSpaceType> & colorSpaces)1622 DMError ScreenSession::GetSupportedColorSpaces(std::vector<GraphicCM_ColorSpaceType>& colorSpaces)
1623 {
1624 #ifdef WM_SCREEN_COLOR_SPACE_ENABLE
1625 auto ret = RSInterfaces::GetInstance().GetScreenSupportedColorSpaces(rsId_, colorSpaces);
1626 if (ret != StatusCode::SUCCESS) {
1627 TLOGE(WmsLogTag::DMS, "SCB: fail! rsId %{public}" PRIu64 ", ret:%{public}d",
1628 rsId_, ret);
1629 return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
1630 }
1631 TLOGI(WmsLogTag::DMS, "SCB: ok! rsId %{public}" PRIu64 ", size %{public}u",
1632 rsId_, static_cast<uint32_t>(colorSpaces.size()));
1633 #endif
1634 return DMError::DM_OK;
1635 }
1636
GetScreenColorSpace(GraphicCM_ColorSpaceType & colorSpace)1637 DMError ScreenSession::GetScreenColorSpace(GraphicCM_ColorSpaceType& colorSpace)
1638 {
1639 #ifdef WM_SCREEN_COLOR_SPACE_ENABLE
1640 auto ret = RSInterfaces::GetInstance().GetScreenColorSpace(rsId_, colorSpace);
1641 if (ret != StatusCode::SUCCESS) {
1642 TLOGE(WmsLogTag::DMS, "GetScreenColorSpace fail! rsId %{public}" PRIu64, rsId_);
1643 return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
1644 }
1645 TLOGI(WmsLogTag::DMS, "GetScreenColorSpace ok! rsId %{public}" PRIu64 ", colorSpace %{public}u",
1646 rsId_, static_cast<uint32_t>(colorSpace));
1647 #endif
1648 return DMError::DM_OK;
1649 }
1650
SetScreenColorSpace(GraphicCM_ColorSpaceType colorSpace)1651 DMError ScreenSession::SetScreenColorSpace(GraphicCM_ColorSpaceType colorSpace)
1652 {
1653 #ifdef WM_SCREEN_COLOR_SPACE_ENABLE
1654 std::vector<GraphicCM_ColorSpaceType> colorSpaces;
1655 DMError res = GetSupportedColorSpaces(colorSpaces);
1656 if (res != DMError::DM_OK) {
1657 TLOGE(WmsLogTag::DMS, "SetScreenColorSpace fail! rsId %{public}" PRIu64, rsId_);
1658 return res;
1659 }
1660 if (colorSpace < 0) {
1661 TLOGE(WmsLogTag::DMS, "SetScreenColorSpace fail! rsId %{public}" PRIu64 " colorSpace %{public}d invalid.",
1662 rsId_, static_cast<int32_t>(colorSpace));
1663 return DMError::DM_ERROR_INVALID_PARAM;
1664 }
1665 auto ret = RSInterfaces::GetInstance().SetScreenColorSpace(rsId_, colorSpace);
1666 if (ret != StatusCode::SUCCESS) {
1667 TLOGE(WmsLogTag::DMS, "SetScreenColorSpace fail! rsId %{public}" PRIu64, rsId_);
1668 return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
1669 }
1670 TLOGI(WmsLogTag::DMS, "SetScreenColorSpace ok! rsId %{public}" PRIu64 ", colorSpace %{public}u",
1671 rsId_, static_cast<uint32_t>(colorSpace));
1672 #endif
1673 return DMError::DM_OK;
1674 }
1675
HasPrivateSessionForeground() const1676 bool ScreenSession::HasPrivateSessionForeground() const
1677 {
1678 return hasPrivateWindowForeground_;
1679 }
1680
SetPrivateSessionForeground(bool hasPrivate)1681 void ScreenSession::SetPrivateSessionForeground(bool hasPrivate)
1682 {
1683 hasPrivateWindowForeground_ = hasPrivate;
1684 }
1685
InitRSDisplayNode(RSDisplayNodeConfig & config,Point & startPoint,bool isExtend)1686 void ScreenSession::InitRSDisplayNode(RSDisplayNodeConfig& config, Point& startPoint, bool isExtend)
1687 {
1688 std::unique_lock<std::shared_mutex> displayNodeLock(displayNodeMutex_);
1689 if (displayNode_ != nullptr) {
1690 displayNode_->SetDisplayNodeMirrorConfig(config);
1691 if (screenId_ == 0 && isFold_) {
1692 TLOGI(WmsLogTag::DMS, "Return InitRSDisplayNode foldScreen0");
1693 return;
1694 }
1695 } else {
1696 std::shared_ptr<RSDisplayNode> rsDisplayNode = RSDisplayNode::Create(config);
1697 if (rsDisplayNode == nullptr) {
1698 TLOGE(WmsLogTag::DMS, "fail to add child. create rsDisplayNode fail!");
1699 return;
1700 }
1701 displayNode_ = rsDisplayNode;
1702 }
1703 TLOGI(WmsLogTag::DMS, "SetDisplayOffset: posX:%{public}d, posY:%{public}d", startPoint.posX_, startPoint.posY_);
1704 displayNode_->SetDisplayOffset(startPoint.posX_, startPoint.posY_);
1705 uint32_t width = 0;
1706 uint32_t height = 0;
1707 if (isExtend) {
1708 width = property_.GetBounds().rect_.GetWidth();
1709 height = property_.GetBounds().rect_.GetHeight();
1710 } else {
1711 sptr<SupportedScreenModes> abstractScreenModes = GetActiveScreenMode();
1712 if (abstractScreenModes != nullptr) {
1713 height = abstractScreenModes->height_;
1714 width = abstractScreenModes->width_;
1715 }
1716 }
1717 RSScreenType screenType;
1718 DmsXcollie dmsXcollie("DMS:InitRSDisplayNode:GetScreenType", XCOLLIE_TIMEOUT_5S);
1719 auto ret = RSInterfaces::GetInstance().GetScreenType(rsId_, screenType);
1720 if (ret == StatusCode::SUCCESS && screenType == RSScreenType::VIRTUAL_TYPE_SCREEN) {
1721 displayNode_->SetSecurityDisplay(true);
1722 TLOGI(WmsLogTag::DMS, "virtualScreen SetSecurityDisplay success");
1723 }
1724 // If setDisplayOffset is not valid for SetFrame/SetBounds
1725 TLOGI(WmsLogTag::DMS, "InitRSDisplayNode screenId:%{public}" PRIu64" width:%{public}u height:%{public}u",
1726 screenId_, width, height);
1727 displayNode_->SetFrame(0, 0, static_cast<float>(width), static_cast<float>(height));
1728 displayNode_->SetBounds(0, 0, static_cast<float>(width), static_cast<float>(height));
1729 if (config.isMirrored) {
1730 EnableMirrorScreenRegion();
1731 }
1732 auto transactionProxy = RSTransactionProxy::GetInstance();
1733 if (transactionProxy != nullptr) {
1734 transactionProxy->FlushImplicitTransaction();
1735 }
1736 }
1737
ScreenSessionGroup(ScreenId screenId,ScreenId rsId,std::string name,ScreenCombination combination)1738 ScreenSessionGroup::ScreenSessionGroup(ScreenId screenId, ScreenId rsId,
1739 std::string name, ScreenCombination combination) : combination_(combination)
1740 {
1741 name_ = name;
1742 screenId_ = screenId;
1743 rsId_ = rsId;
1744 SetScreenType(ScreenType::UNDEFINED);
1745 isScreenGroup_ = true;
1746 }
1747
~ScreenSessionGroup()1748 ScreenSessionGroup::~ScreenSessionGroup()
1749 {
1750 ReleaseDisplayNode();
1751 screenSessionMap_.clear();
1752 }
1753
GetRSDisplayNodeConfig(sptr<ScreenSession> & screenSession,struct RSDisplayNodeConfig & config,sptr<ScreenSession> defaultScreenSession)1754 bool ScreenSessionGroup::GetRSDisplayNodeConfig(sptr<ScreenSession>& screenSession, struct RSDisplayNodeConfig& config,
1755 sptr<ScreenSession> defaultScreenSession)
1756 {
1757 if (screenSession == nullptr) {
1758 TLOGE(WmsLogTag::DMS, "screenSession is nullptr.");
1759 return false;
1760 }
1761 config = { screenSession->rsId_ };
1762 switch (combination_) {
1763 case ScreenCombination::SCREEN_ALONE:
1764 [[fallthrough]];
1765 case ScreenCombination::SCREEN_EXPAND:
1766 break;
1767 case ScreenCombination::SCREEN_UNIQUE:
1768 break;
1769 case ScreenCombination::SCREEN_MIRROR: {
1770 if (GetChildCount() == 0 || mirrorScreenId_ == screenSession->screenId_) {
1771 TLOGI(WmsLogTag::DMS, "SCREEN_MIRROR, config is not mirror");
1772 break;
1773 }
1774 if (defaultScreenSession == nullptr) {
1775 TLOGE(WmsLogTag::DMS, "defaultScreenSession is nullptr");
1776 break;
1777 }
1778 std::shared_ptr<RSDisplayNode> displayNode = defaultScreenSession->GetDisplayNode();
1779 if (displayNode == nullptr) {
1780 TLOGE(WmsLogTag::DMS, "displayNode is nullptr, cannot get DisplayNode");
1781 break;
1782 }
1783 NodeId nodeId = displayNode->GetId();
1784 TLOGI(WmsLogTag::DMS, "mirrorScreenId_:%{public}" PRIu64", rsId_:%{public}" PRIu64", \
1785 nodeId:%{public}" PRIu64"", mirrorScreenId_, screenSession->rsId_, nodeId);
1786 config = {screenSession->rsId_, true, nodeId, true};
1787 break;
1788 }
1789 default:
1790 TLOGE(WmsLogTag::DMS, "fail to add child. invalid group combination:%{public}u", combination_);
1791 return false;
1792 }
1793 return true;
1794 }
1795
AddChild(sptr<ScreenSession> & smsScreen,Point & startPoint,sptr<ScreenSession> defaultScreenSession,bool isExtend)1796 bool ScreenSessionGroup::AddChild(sptr<ScreenSession>& smsScreen, Point& startPoint,
1797 sptr<ScreenSession> defaultScreenSession, bool isExtend)
1798 {
1799 if (smsScreen == nullptr) {
1800 TLOGE(WmsLogTag::DMS, "AddChild, smsScreen is nullptr.");
1801 return false;
1802 }
1803 ScreenId screenId = smsScreen->screenId_;
1804 auto iter = screenSessionMap_.find(screenId);
1805 if (iter != screenSessionMap_.end()) {
1806 TLOGE(WmsLogTag::DMS, "AddChild, screenSessionMap_ has smsScreen:%{public}" PRIu64"", screenId);
1807 return false;
1808 }
1809 struct RSDisplayNodeConfig config;
1810 if (!GetRSDisplayNodeConfig(smsScreen, config, defaultScreenSession)) {
1811 return false;
1812 }
1813 smsScreen->InitRSDisplayNode(config, startPoint, isExtend);
1814 smsScreen->lastGroupSmsId_ = smsScreen->groupSmsId_;
1815 smsScreen->groupSmsId_ = screenId_;
1816 screenSessionMap_.insert(std::make_pair(screenId, std::make_pair(smsScreen, startPoint)));
1817 return true;
1818 }
1819
AddChildren(std::vector<sptr<ScreenSession>> & smsScreens,std::vector<Point> & startPoints)1820 bool ScreenSessionGroup::AddChildren(std::vector<sptr<ScreenSession>>& smsScreens, std::vector<Point>& startPoints)
1821 {
1822 size_t size = smsScreens.size();
1823 if (size != startPoints.size()) {
1824 TLOGE(WmsLogTag::DMS, "AddChildren, unequal size.");
1825 return false;
1826 }
1827 bool res = true;
1828 for (size_t i = 0; i < size; i++) {
1829 res = AddChild(smsScreens[i], startPoints[i], nullptr) && res;
1830 }
1831 return res;
1832 }
1833
RemoveChild(sptr<ScreenSession> & smsScreen)1834 bool ScreenSessionGroup::RemoveChild(sptr<ScreenSession>& smsScreen)
1835 {
1836 if (smsScreen == nullptr) {
1837 TLOGE(WmsLogTag::DMS, "RemoveChild, smsScreen is nullptr.");
1838 return false;
1839 }
1840 ScreenId screenId = smsScreen->screenId_;
1841 smsScreen->lastGroupSmsId_ = smsScreen->groupSmsId_;
1842 smsScreen->groupSmsId_ = SCREEN_ID_INVALID;
1843 std::shared_ptr<RSDisplayNode> displayNode = smsScreen->GetDisplayNode();
1844 if (displayNode != nullptr) {
1845 displayNode->SetDisplayOffset(0, 0);
1846 displayNode->RemoveFromTree();
1847 smsScreen->ReleaseDisplayNode();
1848 }
1849 displayNode = nullptr;
1850 // attention: make sure reference count 0
1851 RSTransaction::FlushImplicitTransaction();
1852 return screenSessionMap_.erase(screenId);
1853 }
1854
HasChild(ScreenId childScreen) const1855 bool ScreenSessionGroup::HasChild(ScreenId childScreen) const
1856 {
1857 return screenSessionMap_.find(childScreen) != screenSessionMap_.end();
1858 }
1859
GetChildren() const1860 std::vector<sptr<ScreenSession>> ScreenSessionGroup::GetChildren() const
1861 {
1862 std::vector<sptr<ScreenSession>> res;
1863 for (auto iter = screenSessionMap_.begin(); iter != screenSessionMap_.end(); iter++) {
1864 res.push_back(iter->second.first);
1865 }
1866 return res;
1867 }
1868
GetChildrenPosition() const1869 std::vector<Point> ScreenSessionGroup::GetChildrenPosition() const
1870 {
1871 std::vector<Point> res;
1872 for (auto iter = screenSessionMap_.begin(); iter != screenSessionMap_.end(); iter++) {
1873 res.push_back(iter->second.second);
1874 }
1875 return res;
1876 }
1877
GetChildPosition(ScreenId screenId) const1878 Point ScreenSessionGroup::GetChildPosition(ScreenId screenId) const
1879 {
1880 Point point{};
1881 auto iter = screenSessionMap_.find(screenId);
1882 if (iter != screenSessionMap_.end()) {
1883 point = iter->second.second;
1884 }
1885 return point;
1886 }
1887
GetChildCount() const1888 size_t ScreenSessionGroup::GetChildCount() const
1889 {
1890 return screenSessionMap_.size();
1891 }
1892
GetScreenCombination() const1893 ScreenCombination ScreenSessionGroup::GetScreenCombination() const
1894 {
1895 return combination_;
1896 }
1897
ConvertToScreenGroupInfo() const1898 sptr<ScreenGroupInfo> ScreenSessionGroup::ConvertToScreenGroupInfo() const
1899 {
1900 sptr<ScreenGroupInfo> screenGroupInfo = new(std::nothrow) ScreenGroupInfo();
1901 if (screenGroupInfo == nullptr) {
1902 return nullptr;
1903 }
1904 FillScreenInfo(screenGroupInfo);
1905 screenGroupInfo->combination_ = combination_;
1906 for (auto iter = screenSessionMap_.begin(); iter != screenSessionMap_.end(); iter++) {
1907 screenGroupInfo->children_.push_back(iter->first);
1908 }
1909 auto positions = GetChildrenPosition();
1910 screenGroupInfo->position_.insert(screenGroupInfo->position_.end(), positions.begin(), positions.end());
1911 return screenGroupInfo;
1912 }
1913
SetDisplayBoundary(const RectF & rect,const uint32_t & offsetY)1914 void ScreenSession::SetDisplayBoundary(const RectF& rect, const uint32_t& offsetY)
1915 {
1916 property_.SetOffsetY(static_cast<int32_t>(offsetY));
1917 property_.SetBounds(RRect(rect, 0.0f, 0.0f));
1918 }
1919
SetExtendProperty(RRect bounds,bool isCurrentOffScreenRendering)1920 void ScreenSession::SetExtendProperty(RRect bounds, bool isCurrentOffScreenRendering)
1921 {
1922 property_.SetBounds(bounds);
1923 property_.SetCurrentOffScreenRendering(isCurrentOffScreenRendering);
1924 }
1925
Resize(uint32_t width,uint32_t height,bool isFreshBoundsSync)1926 void ScreenSession::Resize(uint32_t width, uint32_t height, bool isFreshBoundsSync)
1927 {
1928 if (isFreshBoundsSync) {
1929 sptr<SupportedScreenModes> screenMode = GetActiveScreenMode();
1930 if (screenMode != nullptr) {
1931 screenMode->width_ = width;
1932 screenMode->height_ = height;
1933 UpdatePropertyByActiveMode();
1934 }
1935 }
1936 {
1937 std::shared_lock<std::shared_mutex> displayNodeLock(displayNodeMutex_);
1938 if (displayNode_ == nullptr) {
1939 TLOGE(WmsLogTag::DMS, "displayNode_ is null, resize failed");
1940 return;
1941 }
1942 displayNode_->SetFrame(0, 0, static_cast<float>(width), static_cast<float>(height));
1943 displayNode_->SetBounds(0, 0, static_cast<float>(width), static_cast<float>(height));
1944 }
1945 RSTransaction::FlushImplicitTransaction();
1946 }
1947
UpdateAvailableArea(DMRect area)1948 bool ScreenSession::UpdateAvailableArea(DMRect area)
1949 {
1950 if (property_.GetAvailableArea() == area) {
1951 return false;
1952 }
1953 property_.SetAvailableArea(area);
1954 return true;
1955 }
1956
UpdateExpandAvailableArea(DMRect area)1957 bool ScreenSession::UpdateExpandAvailableArea(DMRect area)
1958 {
1959 if (property_.GetExpandAvailableArea() == area) {
1960 return false;
1961 }
1962 property_.SetExpandAvailableArea(area);
1963 return true;
1964 }
1965
SetAvailableArea(DMRect area)1966 void ScreenSession::SetAvailableArea(DMRect area)
1967 {
1968 property_.SetAvailableArea(area);
1969 }
1970
GetAvailableArea()1971 DMRect ScreenSession::GetAvailableArea()
1972 {
1973 return property_.GetAvailableArea();
1974 }
1975
GetExpandAvailableArea()1976 DMRect ScreenSession::GetExpandAvailableArea()
1977 {
1978 return property_.GetExpandAvailableArea();
1979 }
1980
SetFoldScreen(bool isFold)1981 void ScreenSession::SetFoldScreen(bool isFold)
1982 {
1983 TLOGI(WmsLogTag::DMS, "SetFoldScreen %{public}u", isFold);
1984 isFold_ = isFold;
1985 }
1986
SetHdrFormats(std::vector<uint32_t> && hdrFormats)1987 void ScreenSession::SetHdrFormats(std::vector<uint32_t>&& hdrFormats)
1988 {
1989 hdrFormats_ = std::move(hdrFormats);
1990 }
1991
SetColorSpaces(std::vector<uint32_t> && colorSpaces)1992 void ScreenSession::SetColorSpaces(std::vector<uint32_t>&& colorSpaces)
1993 {
1994 colorSpaces_ = std::move(colorSpaces);
1995 }
1996
IsWidthHeightMatch(float width,float height,float targetWidth,float targetHeight)1997 bool ScreenSession::IsWidthHeightMatch(float width, float height, float targetWidth, float targetHeight)
1998 {
1999 return (width == targetWidth && height == targetHeight) || (width == targetHeight && height == targetWidth);
2000 }
2001
SetScreenSnapshotRect(RSSurfaceCaptureConfig & config)2002 void ScreenSession::SetScreenSnapshotRect(RSSurfaceCaptureConfig& config)
2003 {
2004 bool isChanged = false;
2005 auto width = property_.GetBounds().rect_.width_;
2006 auto height = property_.GetBounds().rect_.height_;
2007 Drawing::Rect snapshotRect = {0, 0, 0, 0};
2008 if (IsWidthHeightMatch(width, height, MAIN_STATUS_WIDTH, SCREEN_HEIGHT)) {
2009 snapshotRect = {0, 0, SCREEN_HEIGHT, MAIN_STATUS_WIDTH};
2010 config.mainScreenRect = snapshotRect;
2011 isChanged = true;
2012 } else if (IsWidthHeightMatch(width, height, FULL_STATUS_WIDTH, SCREEN_HEIGHT)) {
2013 snapshotRect = {0, FULL_STATUS_OFFSET_X, SCREEN_HEIGHT, GLOBAL_FULL_STATUS_WIDTH};
2014 config.mainScreenRect = snapshotRect;
2015 isChanged = true;
2016 } else if (IsWidthHeightMatch(width, height, GLOBAL_FULL_STATUS_WIDTH, SCREEN_HEIGHT)) {
2017 snapshotRect = {0, 0, SCREEN_HEIGHT, GLOBAL_FULL_STATUS_WIDTH};
2018 config.mainScreenRect = snapshotRect;
2019 isChanged = true;
2020 }
2021 if (isChanged) {
2022 TLOGI(WmsLogTag::DMS,
2023 "left: %{public}f, top: %{public}f, right: %{public}f, bottom: %{public}f",
2024 snapshotRect.left_, snapshotRect.top_, snapshotRect.right_, snapshotRect.bottom_);
2025 } else {
2026 TLOGI(WmsLogTag::DMS, "no need to set screen snapshot rect, use default rect");
2027 }
2028 }
2029
GetScreenSnapshot(float scaleX,float scaleY)2030 std::shared_ptr<Media::PixelMap> ScreenSession::GetScreenSnapshot(float scaleX, float scaleY)
2031 {
2032 {
2033 std::shared_lock<std::shared_mutex> displayNodeLock(displayNodeMutex_);
2034 if (displayNode_ == nullptr) {
2035 TLOGE(WmsLogTag::DMS, "get screen snapshot displayNode_ is null");
2036 return nullptr;
2037 }
2038 }
2039
2040 HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "ss:GetScreenSnapshot");
2041 auto callback = std::make_shared<SurfaceCaptureFuture>();
2042 RSSurfaceCaptureConfig config = {
2043 .scaleX = scaleX,
2044 .scaleY = scaleY,
2045 .useDma = true,
2046 };
2047 SetScreenSnapshotRect(config);
2048 {
2049 DmsXcollie dmsXcollie("DMS:GetScreenSnapshot:TakeSurfaceCapture", XCOLLIE_TIMEOUT_5S);
2050 std::shared_lock<std::shared_mutex> displayNodeLock(displayNodeMutex_);
2051 bool ret = RSInterfaces::GetInstance().TakeSurfaceCapture(displayNode_, callback, config);
2052 if (!ret) {
2053 TLOGE(WmsLogTag::DMS, "get screen snapshot TakeSurfaceCapture failed");
2054 return nullptr;
2055 }
2056 }
2057
2058 auto pixelMap = callback->GetResult(2000); // 2000, default timeout
2059 if (pixelMap != nullptr) {
2060 TLOGD(WmsLogTag::DMS, "save pixelMap WxH = %{public}dx%{public}d", pixelMap->GetWidth(), pixelMap->GetHeight());
2061 } else {
2062 TLOGE(WmsLogTag::DMS, "failed to get pixelMap, return nullptr");
2063 }
2064 return pixelMap;
2065 }
2066
SetStartPosition(uint32_t startX,uint32_t startY)2067 void ScreenSession::SetStartPosition(uint32_t startX, uint32_t startY)
2068 {
2069 property_.SetStartPosition(startX, startY);
2070 }
2071
SetXYPosition(int32_t x,int32_t y)2072 void ScreenSession::SetXYPosition(int32_t x, int32_t y)
2073 {
2074 property_.SetXYPosition(x, y);
2075 }
2076
ScreenCaptureNotify(ScreenId mainScreenId,int32_t uid,const std::string & clientName)2077 void ScreenSession::ScreenCaptureNotify(ScreenId mainScreenId, int32_t uid, const std::string& clientName)
2078 {
2079 std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
2080 if (screenChangeListenerList_.empty()) {
2081 TLOGE(WmsLogTag::DMS, "screenChangeListenerList is empty.");
2082 return;
2083 }
2084 for (auto& listener : screenChangeListenerList_) {
2085 if (!listener) {
2086 TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
2087 continue;
2088 }
2089 listener->OnScreenCaptureNotify(mainScreenId, uid, clientName);
2090 }
2091 }
2092
SuperFoldStatusChange(ScreenId screenId,SuperFoldStatus superFoldStatus)2093 void ScreenSession::SuperFoldStatusChange(ScreenId screenId, SuperFoldStatus superFoldStatus)
2094 {
2095 std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
2096 if (screenChangeListenerList_.empty()) {
2097 TLOGE(WmsLogTag::DMS, "screenChangeListenerList is empty.");
2098 return;
2099 }
2100 for (auto& listener : screenChangeListenerList_) {
2101 if (!listener) {
2102 TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
2103 continue;
2104 }
2105 listener->OnSuperFoldStatusChange(screenId, superFoldStatus);
2106 }
2107 }
2108
ExtendScreenConnectStatusChange(ScreenId screenId,ExtendScreenConnectStatus extendScreenConnectStatus)2109 void ScreenSession::ExtendScreenConnectStatusChange(ScreenId screenId,
2110 ExtendScreenConnectStatus extendScreenConnectStatus)
2111 {
2112 std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
2113 if (screenChangeListenerList_.empty()) {
2114 WLOGFE("screenChangeListenerList is empty.");
2115 return;
2116 }
2117 for (auto& listener : screenChangeListenerList_) {
2118 if (!listener) {
2119 WLOGFE("screenChangeListener is null.");
2120 continue;
2121 }
2122 listener->OnExtendScreenConnectStatusChange(screenId, extendScreenConnectStatus);
2123 }
2124 }
2125
SecondaryReflexionChange(ScreenId screenId,bool isSecondaryReflexion)2126 void ScreenSession::SecondaryReflexionChange(ScreenId screenId, bool isSecondaryReflexion)
2127 {
2128 std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
2129 if (screenChangeListenerList_.empty()) {
2130 TLOGE(WmsLogTag::DMS, "screenChangeListenerList is empty.");
2131 return;
2132 }
2133 for (auto& listener : screenChangeListenerList_) {
2134 if (!listener) {
2135 TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
2136 continue;
2137 }
2138 listener->OnSecondaryReflexionChange(screenId, isSecondaryReflexion);
2139 }
2140 }
2141
SetIsPhysicalMirrorSwitch(bool isPhysicalMirrorSwitch)2142 void ScreenSession::SetIsPhysicalMirrorSwitch(bool isPhysicalMirrorSwitch)
2143 {
2144 isPhysicalMirrorSwitch_ = isPhysicalMirrorSwitch;
2145 }
2146
GetIsPhysicalMirrorSwitch()2147 bool ScreenSession::GetIsPhysicalMirrorSwitch()
2148 {
2149 return isPhysicalMirrorSwitch_;
2150 }
2151
GetApiVersion()2152 int32_t ScreenSession::GetApiVersion()
2153 {
2154 static std::chrono::steady_clock::time_point lastRequestTime = std::chrono::steady_clock::now();
2155 auto currentTime = std::chrono::steady_clock::now();
2156 auto interval = std::chrono::duration_cast<std::chrono::microseconds>(currentTime - lastRequestTime).count();
2157 int32_t apiVersion = NO_EXIST_UID_VERSION;
2158 int32_t currentPid = IPCSkeleton::GetCallingPid();
2159 if (interval < MAX_INTERVAL_US) {
2160 apiVersion = g_uidVersionMap.Get(currentPid);
2161 }
2162 if (apiVersion == NO_EXIST_UID_VERSION) {
2163 apiVersion = static_cast<int32_t>(SysCapUtil::GetApiCompatibleVersion());
2164 TLOGI(WmsLogTag::DMS, "Get version from IPC");
2165 g_uidVersionMap.Set(currentPid, apiVersion);
2166 }
2167 lastRequestTime = currentTime;
2168 return apiVersion;
2169 }
2170
SetShareProtect(bool needShareProtect)2171 void ScreenSession::SetShareProtect(bool needShareProtect)
2172 {
2173 needShareProtect_ = needShareProtect;
2174 }
2175
GetShareProtect()2176 bool ScreenSession::GetShareProtect()
2177 {
2178 return needShareProtect_;
2179 }
2180
GetSensorRotation() const2181 float ScreenSession::GetSensorRotation() const
2182 {
2183 return currentSensorRotation_;
2184 }
2185 } // namespace OHOS::Rosen
2186