• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "rs_adapter.h"
32 
33 namespace OHOS::Rosen {
34 namespace {
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 static const int32_t LANDSCAPE_ROTATION_MOD = 2;
42 static const int32_t IS_PORTRAIT = 0;
43 static const int32_t IS_LANDSCAPE = 1;
44 const unsigned int XCOLLIE_TIMEOUT_5S = 5;
45 const static uint32_t MAX_INTERVAL_US = 1800000000; //30分钟
46 const int32_t MAP_SIZE = 300;
47 const int32_t NO_EXIST_UID_VERSION = -1;
48 const int DURATION_0MS = 0;
49 const int DURATION_1000MS = 1000;
50 const float BRIGHTNESS_FACTOR_0 = 0;
51 const float BRIGHTNESS_FACTOR_1 = 1;
52 const float FULL_STATUS_WIDTH = 2048;
53 const float GLOBAL_FULL_STATUS_WIDTH = 3184;
54 const float MAIN_STATUS_WIDTH = 1008;
55 const float FULL_STATUS_OFFSET_X = 1136;
56 const float SCREEN_HEIGHT = 2232;
57 constexpr uint32_t SECONDARY_ROTATION_270 = 3;
58 constexpr uint32_t SECONDARY_ROTATION_MOD = 4;
59 constexpr int32_t SNAPSHOT_TIMEOUT_MS = 300;
60 constexpr ScreenId SCREEN_ID_DEFAULT = 0;
61 constexpr float HORIZONTAL = 270.f;
62 ScreenCache<int32_t, int32_t> g_uidVersionMap(MAP_SIZE, NO_EXIST_UID_VERSION);
63 }
64 
ScreenSession(const ScreenSessionConfig & config,ScreenSessionReason reason)65 ScreenSession::ScreenSession(const ScreenSessionConfig& config, ScreenSessionReason reason)
66     : name_(config.name), screenId_(config.screenId), rsId_(config.rsId), defaultScreenId_(config.defaultScreenId),
67     property_(config.property), displayNode_(config.displayNode), innerName_(config.innerName)
68 {
69     TLOGI(WmsLogTag::DMS,
70         "[DPNODE]Create Session, reason: %{public}d, screenId: %{public}" PRIu64", rsId: %{public}" PRIu64"",
71         reason, screenId_, rsId_);
72     TLOGI(WmsLogTag::DMS,
73         "[DPNODE]Config name: %{public}s, defaultId: %{public}" PRIu64", mirrorNodeId: %{public}" PRIu64"",
74         name_.c_str(), defaultScreenId_, config.mirrorNodeId);
75     sessionId_ = sessionIdGenerator_++;
76     RSAdapterUtil::InitRSUIDirector(rsUIDirector_, true, true);
77     RSAdapterUtil::SetRSUIContext(displayNode_, GetRSUIContext(), true);
78     Rosen::RSDisplayNodeConfig rsConfig;
79     bool isNeedCreateDisplayNode = true;
80     property_.SetRsId(rsId_);
81     switch (reason) {
82         case ScreenSessionReason::CREATE_SESSION_FOR_CLIENT: {
83             TLOGI(WmsLogTag::DMS, "create screen session for client. noting to do.");
84             return;
85         }
86         case ScreenSessionReason::CREATE_SESSION_FOR_VIRTUAL: {
87             // create virtual screen should use rsid
88             rsConfig.screenId = rsId_;
89             break;
90         }
91         case ScreenSessionReason::CREATE_SESSION_FOR_MIRROR: {
92             rsConfig.screenId = screenId_;
93             rsConfig.isMirrored = true;
94             rsConfig.mirrorNodeId = config.mirrorNodeId;
95             rsConfig.isSync = true;
96             break;
97         }
98         case ScreenSessionReason::CREATE_SESSION_FOR_REAL: {
99             rsConfig.screenId = screenId_;
100             break;
101         }
102         case ScreenSessionReason::CREATE_SESSION_WITHOUT_DISPLAY_NODE: {
103             TLOGI(WmsLogTag::DMS, "screen session no need create displayNode.");
104             isNeedCreateDisplayNode = false;
105             break;
106         }
107         default : {
108             TLOGE(WmsLogTag::DMS, "invalid screen session config.");
109             break;
110         }
111     }
112     if (isNeedCreateDisplayNode) {
113         CreateDisplayNode(rsConfig);
114     }
115 }
116 
CreateDisplayNode(const Rosen::RSDisplayNodeConfig & config)117 void ScreenSession::CreateDisplayNode(const Rosen::RSDisplayNodeConfig& config)
118 {
119     TLOGI(WmsLogTag::DMS,
120         "[DPNODE]config screenId: %{public}" PRIu64", mirrorNodeId: %{public}" PRIu64", isMirrored: %{public}d",
121         config.screenId, config.mirrorNodeId, static_cast<int32_t>(config.isMirrored));
122     {
123         std::unique_lock<std::shared_mutex> displayNodeLock(displayNodeMutex_);
124         displayNode_ = Rosen::RSDisplayNode::Create(config, GetRSUIContext());
125         TLOGD(WmsLogTag::WMS_SCB,
126               "Create RSDisplayNode: %{public}s", RSAdapterUtil::RSNodeToStr(displayNode_).c_str());
127         if (displayNode_) {
128             RSAdapterUtil::SetSkipCheckInMultiInstance(displayNode_, true);
129             displayNode_->SetFrame(property_.GetBounds().rect_.left_, property_.GetBounds().rect_.top_,
130                 property_.GetBounds().rect_.width_, property_.GetBounds().rect_.height_);
131             displayNode_->SetBounds(property_.GetBounds().rect_.left_, property_.GetBounds().rect_.top_,
132                 property_.GetBounds().rect_.width_, property_.GetBounds().rect_.height_);
133             if (config.isMirrored) {
134                 EnableMirrorScreenRegion();
135             }
136         } else {
137             TLOGE(WmsLogTag::DMS, "Failed to create displayNode, displayNode is null!");
138         }
139     }
140     RSTransactionAdapter::FlushImplicitTransaction(GetRSUIContext());
141 }
142 
ReuseDisplayNode(const RSDisplayNodeConfig & config)143 void ScreenSession::ReuseDisplayNode(const RSDisplayNodeConfig& config)
144 {
145     if (displayNode_) {
146         std::unique_lock<std::shared_mutex> lock(displayNodeMutex_);
147         displayNode_->SetDisplayNodeMirrorConfig(config);
148         RSTransactionAdapter::FlushImplicitTransaction(displayNode_);
149     } else {
150         CreateDisplayNode(config);
151     }
152 }
153 
~ScreenSession()154 ScreenSession::~ScreenSession()
155 {
156     TLOGI(WmsLogTag::DMS, "~ScreenSession");
157 }
158 
ScreenSession(ScreenId screenId,ScreenId rsId,const std::string & name,const ScreenProperty & property,const std::shared_ptr<RSDisplayNode> & displayNode)159 ScreenSession::ScreenSession(ScreenId screenId, ScreenId rsId, const std::string& name,
160     const ScreenProperty& property, const std::shared_ptr<RSDisplayNode>& displayNode)
161     : name_(name), screenId_(screenId), rsId_(rsId), property_(property), displayNode_(displayNode)
162 {
163     TLOGI(WmsLogTag::DMS, "Success to create screenSession in constructor_0, screenid is %{public}" PRIu64"",
164         screenId_);
165     sessionId_ = sessionIdGenerator_++;
166     property_.SetRsId(rsId_);
167     RSAdapterUtil::InitRSUIDirector(rsUIDirector_, true, true);
168     RSAdapterUtil::SetRSUIContext(displayNode_, GetRSUIContext(), true);
169 }
170 
ScreenSession(ScreenId screenId,const ScreenProperty & property,ScreenId defaultScreenId)171 ScreenSession::ScreenSession(ScreenId screenId, const ScreenProperty& property, ScreenId defaultScreenId)
172     : screenId_(screenId), defaultScreenId_(defaultScreenId), property_(property)
173 {
174     sessionId_ = sessionIdGenerator_++;
175     RSAdapterUtil::InitRSUIDirector(rsUIDirector_, true, true);
176     Rosen::RSDisplayNodeConfig config = { .screenId = screenId_ };
177     displayNode_ = Rosen::RSDisplayNode::Create(config, GetRSUIContext());
178     TLOGD(WmsLogTag::WMS_SCB,
179           "Create RSDisplayNode: %{public}s", RSAdapterUtil::RSNodeToStr(displayNode_).c_str());
180     property_.SetRsId(rsId_);
181     if (displayNode_) {
182         TLOGI(WmsLogTag::DMS, "Success to create displayNode in constructor_1, screenid is %{public}" PRIu64"",
183             screenId_);
184         RSAdapterUtil::SetSkipCheckInMultiInstance(displayNode_, true);
185         displayNode_->SetFrame(property_.GetBounds().rect_.left_, property_.GetBounds().rect_.top_,
186             property_.GetBounds().rect_.width_, property_.GetBounds().rect_.height_);
187         displayNode_->SetBounds(property_.GetBounds().rect_.left_, property_.GetBounds().rect_.top_,
188             property_.GetBounds().rect_.width_, property_.GetBounds().rect_.height_);
189     } else {
190         TLOGE(WmsLogTag::DMS, "Failed to create displayNode, displayNode is null!");
191     }
192     RSTransactionAdapter::FlushImplicitTransaction(GetRSUIContext());
193 }
194 
ScreenSession(ScreenId screenId,const ScreenProperty & property,NodeId nodeId,ScreenId defaultScreenId)195 ScreenSession::ScreenSession(ScreenId screenId, const ScreenProperty& property,
196     NodeId nodeId, ScreenId defaultScreenId)
197     : screenId_(screenId), defaultScreenId_(defaultScreenId), property_(property)
198 {
199     sessionId_ = sessionIdGenerator_++;
200     rsId_ = screenId;
201     property_.SetRsId(rsId_);
202     RSAdapterUtil::InitRSUIDirector(rsUIDirector_, true, true);
203     Rosen::RSDisplayNodeConfig config = { .screenId = screenId_, .isMirrored = true, .mirrorNodeId = nodeId,
204         .isSync = true};
205     displayNode_ = Rosen::RSDisplayNode::Create(config, GetRSUIContext());
206     TLOGD(WmsLogTag::WMS_SCB,
207           "Create RSDisplayNode: %{public}s", RSAdapterUtil::RSNodeToStr(displayNode_).c_str());
208     if (displayNode_) {
209         TLOGI(WmsLogTag::DMS, "Success to create displayNode in constructor_2, screenid is %{public}" PRIu64"",
210             screenId_);
211         RSAdapterUtil::SetSkipCheckInMultiInstance(displayNode_, true);
212         displayNode_->SetFrame(property_.GetBounds().rect_.left_, property_.GetBounds().rect_.top_,
213             property_.GetBounds().rect_.width_, property_.GetBounds().rect_.height_);
214         displayNode_->SetBounds(property_.GetBounds().rect_.left_, property_.GetBounds().rect_.top_,
215             property_.GetBounds().rect_.width_, property_.GetBounds().rect_.height_);
216     } else {
217         TLOGE(WmsLogTag::DMS, "Failed to create displayNode, displayNode is null!");
218     }
219     RSTransactionAdapter::FlushImplicitTransaction(GetRSUIContext());
220 }
221 
ScreenSession(const std::string & name,ScreenId smsId,ScreenId rsId,ScreenId defaultScreenId)222 ScreenSession::ScreenSession(const std::string& name, ScreenId smsId, ScreenId rsId, ScreenId defaultScreenId)
223     : name_(name), screenId_(smsId), rsId_(rsId), defaultScreenId_(defaultScreenId)
224 {
225     sessionId_ = sessionIdGenerator_++;
226     (void)rsId_;
227     property_.SetRsId(rsId_);
228     RSAdapterUtil::InitRSUIDirector(rsUIDirector_, true, true);
229     // 虚拟屏的screen id和rs id不一致,displayNode的创建应使用rs id
230     Rosen::RSDisplayNodeConfig config = { .screenId = rsId_ };
231     displayNode_ = Rosen::RSDisplayNode::Create(config, GetRSUIContext());
232     TLOGD(WmsLogTag::WMS_SCB,
233           "Create RSDisplayNode: %{public}s", RSAdapterUtil::RSNodeToStr(displayNode_).c_str());
234     if (displayNode_) {
235         TLOGI(WmsLogTag::DMS, "Success to create displayNode in constructor_3, rs id is %{public}" PRIu64"", rsId_);
236         RSAdapterUtil::SetSkipCheckInMultiInstance(displayNode_, true);
237         displayNode_->SetFrame(property_.GetBounds().rect_.left_, property_.GetBounds().rect_.top_,
238             property_.GetBounds().rect_.width_, property_.GetBounds().rect_.height_);
239         displayNode_->SetBounds(property_.GetBounds().rect_.left_, property_.GetBounds().rect_.top_,
240             property_.GetBounds().rect_.width_, property_.GetBounds().rect_.height_);
241     } else {
242         TLOGE(WmsLogTag::DMS, "Failed to create displayNode, displayNode is null!");
243     }
244     RSTransactionAdapter::FlushImplicitTransaction(GetRSUIContext());
245 }
246 
SetDisplayNodeScreenId(ScreenId screenId)247 void ScreenSession::SetDisplayNodeScreenId(ScreenId screenId)
248 {
249     std::unique_lock<std::shared_mutex> lock(displayNodeMutex_);
250     if (displayNode_ != nullptr) {
251         TLOGI(WmsLogTag::DMS, "SetDisplayNodeScreenId %{public}" PRIu64"", screenId);
252         displayNode_->SetScreenId(screenId);
253     }
254 }
255 
RegisterScreenChangeListener(IScreenChangeListener * screenChangeListener)256 void ScreenSession::RegisterScreenChangeListener(IScreenChangeListener* screenChangeListener)
257 {
258     if (screenChangeListener == nullptr) {
259         TLOGE(WmsLogTag::DMS, "Failed to register screen change listener, listener is null!");
260         return;
261     }
262     std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
263     if (std::find(screenChangeListenerList_.begin(), screenChangeListenerList_.end(), screenChangeListener) !=
264         screenChangeListenerList_.end()) {
265         TLOGI(WmsLogTag::DMS, "Repeat to register screen change listener!");
266         return;
267     }
268 
269     screenChangeListenerList_.emplace_back(screenChangeListener);
270     if (screenState_ == ScreenState::CONNECTION) {
271         screenChangeListener->OnConnect(screenId_);
272         TLOGI(WmsLogTag::DMS, "Success to call onconnect callback.");
273     }
274     TLOGI(WmsLogTag::DMS, "Success to register screen change listener.");
275 }
276 
UnregisterScreenChangeListener(IScreenChangeListener * screenChangeListener)277 void ScreenSession::UnregisterScreenChangeListener(IScreenChangeListener* screenChangeListener)
278 {
279     if (screenChangeListener == nullptr) {
280         TLOGE(WmsLogTag::DMS, "Failed to unregister screen change listener, listener is null!");
281         return;
282     }
283     std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
284     screenChangeListenerList_.erase(
285         std::remove_if(screenChangeListenerList_.begin(), screenChangeListenerList_.end(),
286             [screenChangeListener](IScreenChangeListener* listener) { return screenChangeListener == listener; }),
287         screenChangeListenerList_.end());
288 }
289 
SetMirrorScreenRegion(ScreenId screenId,DMRect screenRegion)290 void ScreenSession::SetMirrorScreenRegion(ScreenId screenId, DMRect screenRegion)
291 {
292     std::lock_guard<std::mutex> lock(mirrorScreenRegionMutex_);
293     mirrorScreenRegion_ = std::make_pair(screenId, screenRegion);
294 }
295 
GetMirrorScreenRegion()296 std::pair<ScreenId, DMRect> ScreenSession::GetMirrorScreenRegion()
297 {
298     std::lock_guard<std::mutex> lock(mirrorScreenRegionMutex_);
299     return mirrorScreenRegion_;
300 }
301 
EnableMirrorScreenRegion()302 void ScreenSession::EnableMirrorScreenRegion()
303 {
304     const auto& mirrorScreenRegionPair = GetMirrorScreenRegion();
305     const auto& rect = mirrorScreenRegionPair.second;
306     ScreenId screenId = INVALID_SCREEN_ID;
307     bool isEnableRegionRotation = GetIsEnableRegionRotation();
308     bool isEnableCanvasRotation = GetIsEnableCanvasRotation();
309     bool isSupportRotation = isEnableRegionRotation | isEnableCanvasRotation;
310     TLOGI(WmsLogTag::DMS, "isEnableRegionRotation: %{public}d, isEnableCanvasRotation: %{public}d",
311         isEnableRegionRotation, isEnableCanvasRotation);
312     if (isPhysicalMirrorSwitch_) {
313         screenId = screenId_;
314     } else {
315         screenId = rsId_;
316     }
317     auto ret = RSInterfaces::GetInstance().SetMirrorScreenVisibleRect(screenId,
318         { rect.posX_, rect.posY_, rect.width_, rect.height_ }, isSupportRotation);
319     if (ret != StatusCode::SUCCESS) {
320         TLOGE(WmsLogTag::DMS, "Fail! rsId %{public}" PRIu64", ret:%{public}d," PRIu64
321         ", x:%{public}d y:%{public}d w:%{public}u h:%{public}u, isSupportRotation:%{public}d", screenId, ret,
322         rect.posX_, rect.posY_, rect.width_, rect.height_, isSupportRotation);
323     } else {
324         TLOGE(WmsLogTag::DMS, "Success! rsId %{public}" PRIu64", ret:%{public}d," PRIu64
325         ", x:%{public}d y:%{public}d w:%{public}u h:%{public}u, isSupportRotation:%{public}d", screenId, ret,
326         rect.posX_, rect.posY_, rect.width_, rect.height_, isSupportRotation);
327     }
328 }
329 
ConvertToDisplayInfo()330 sptr<DisplayInfo> ScreenSession::ConvertToDisplayInfo()
331 {
332     sptr<DisplayInfo> displayInfo = new(std::nothrow) DisplayInfo();
333     if (displayInfo == nullptr) {
334         return displayInfo;
335     }
336     RRect bounds = property_.GetBounds();
337     RRect phyBounds = property_.GetPhyBounds();
338     displayInfo->name_ = name_;
339     displayInfo->SetWidth(bounds.rect_.GetWidth());
340     if (isBScreenHalf_) {
341         DMRect creaseRect = property_.GetCreaseRect();
342         if (creaseRect.posY_ > 0) {
343             displayInfo->SetHeight(creaseRect.posY_);
344         } else {
345             displayInfo->SetHeight(bounds.rect_.GetHeight() / HALF_SCREEN_PARAM);
346         }
347     } else {
348         displayInfo->SetHeight(bounds.rect_.GetHeight());
349     }
350     displayInfo->SetPhysicalWidth(phyBounds.rect_.GetWidth());
351     displayInfo->SetPhysicalHeight(phyBounds.rect_.GetHeight());
352     if (isFakeSession_) {
353         displayInfo->SetScreenId(SCREEN_ID_DEFAULT);
354     } else {
355         displayInfo->SetScreenId(screenId_);
356     }
357     displayInfo->SetDisplayId(screenId_);
358     displayInfo->SetRefreshRate(property_.GetRefreshRate());
359     displayInfo->SetVirtualPixelRatio(property_.GetVirtualPixelRatio());
360     displayInfo->SetDensityInCurResolution(property_.GetDensityInCurResolution());
361     displayInfo->SetDefaultVirtualPixelRatio(property_.GetDefaultDensity());
362     displayInfo->SetXDpi(property_.GetXDpi());
363     displayInfo->SetYDpi(property_.GetYDpi());
364     displayInfo->SetDpi(property_.GetVirtualPixelRatio() * DOT_PER_INCH);
365     int32_t apiVersion = GetApiVersion();
366     if (apiVersion >= 14 || apiVersion == 0) { // 14 is API version
367         displayInfo->SetRotation(property_.GetDeviceRotation());
368         displayInfo->SetDisplayOrientation(property_.GetDeviceOrientation());
369     } else {
370         displayInfo->SetRotation(property_.GetScreenRotation());
371         displayInfo->SetDisplayOrientation(property_.GetDisplayOrientation());
372     }
373     displayInfo->SetOrientation(property_.GetOrientation());
374     displayInfo->SetOffsetX(property_.GetOffsetX());
375     displayInfo->SetOffsetY(property_.GetOffsetY());
376     displayInfo->SetHdrFormats(GetHdrFormats());
377     displayInfo->SetColorSpaces(GetColorSpaces());
378     displayInfo->SetDisplayState(property_.GetDisplayState());
379     displayInfo->SetDefaultDeviceRotationOffset(property_.GetDefaultDeviceRotationOffset());
380     displayInfo->SetAvailableWidth(property_.GetAvailableArea().width_);
381     displayInfo->SetAvailableHeight(property_.GetAvailableArea().height_);
382     displayInfo->SetScaleX(property_.GetScaleX());
383     displayInfo->SetScaleY(property_.GetScaleY());
384     displayInfo->SetPivotX(property_.GetPivotX());
385     displayInfo->SetPivotY(property_.GetPivotY());
386     displayInfo->SetTranslateX(property_.GetTranslateX());
387     displayInfo->SetTranslateY(property_.GetTranslateY());
388     displayInfo->SetScreenShape(property_.GetScreenShape());
389     displayInfo->SetOriginRotation(property_.GetScreenRotation());
390     displayInfo->SetSupportedRefreshRate(GetSupportedRefreshRate());
391     return displayInfo;
392 }
393 
ConvertToRealDisplayInfo()394 sptr<DisplayInfo> ScreenSession::ConvertToRealDisplayInfo()
395 {
396     sptr<DisplayInfo> displayInfo = new(std::nothrow) DisplayInfo();
397     if (displayInfo == nullptr) {
398         return displayInfo;
399     }
400     RRect bounds = property_.GetBounds();
401     RRect phyBounds = property_.GetPhyBounds();
402     displayInfo->name_ = name_;
403     displayInfo->SetWidth(bounds.rect_.GetWidth());
404     displayInfo->SetHeight(bounds.rect_.GetHeight());
405     displayInfo->SetPhysicalWidth(phyBounds.rect_.GetWidth());
406     displayInfo->SetPhysicalHeight(phyBounds.rect_.GetHeight());
407     displayInfo->SetScreenId(screenId_);
408     displayInfo->SetDisplayId(screenId_);
409     displayInfo->SetRefreshRate(property_.GetRefreshRate());
410     displayInfo->SetVirtualPixelRatio(property_.GetVirtualPixelRatio());
411     displayInfo->SetDensityInCurResolution(property_.GetDensityInCurResolution());
412     displayInfo->SetDefaultVirtualPixelRatio(property_.GetDefaultDensity());
413     displayInfo->SetXDpi(property_.GetXDpi());
414     displayInfo->SetYDpi(property_.GetYDpi());
415     displayInfo->SetDpi(property_.GetVirtualPixelRatio() * DOT_PER_INCH);
416     int32_t apiVersion = GetApiVersion();
417     if (apiVersion >= 14 || apiVersion == 0) { // 14 is API version
418         displayInfo->SetRotation(property_.GetDeviceRotation());
419         displayInfo->SetDisplayOrientation(property_.GetDeviceOrientation());
420     } else {
421         displayInfo->SetRotation(property_.GetScreenRotation());
422         displayInfo->SetDisplayOrientation(property_.GetDisplayOrientation());
423     }
424     displayInfo->SetOrientation(property_.GetOrientation());
425     displayInfo->SetOffsetX(property_.GetOffsetX());
426     displayInfo->SetOffsetY(property_.GetOffsetY());
427     displayInfo->SetHdrFormats(GetHdrFormats());
428     displayInfo->SetColorSpaces(GetColorSpaces());
429     displayInfo->SetDisplayState(property_.GetDisplayState());
430     displayInfo->SetDefaultDeviceRotationOffset(property_.GetDefaultDeviceRotationOffset());
431     displayInfo->SetAvailableWidth(property_.GetAvailableArea().width_);
432     displayInfo->SetAvailableHeight(property_.GetAvailableArea().height_);
433     displayInfo->SetScaleX(property_.GetScaleX());
434     displayInfo->SetScaleY(property_.GetScaleY());
435     displayInfo->SetPivotX(property_.GetPivotX());
436     displayInfo->SetPivotY(property_.GetPivotY());
437     displayInfo->SetTranslateX(property_.GetTranslateX());
438     displayInfo->SetTranslateY(property_.GetTranslateY());
439     displayInfo->SetSupportedRefreshRate(GetSupportedRefreshRate());
440     return displayInfo;
441 }
442 
GetScreenSupportedColorGamuts(std::vector<ScreenColorGamut> & colorGamuts)443 DMError ScreenSession::GetScreenSupportedColorGamuts(std::vector<ScreenColorGamut>& colorGamuts)
444 {
445     auto ret = RSInterfaces::GetInstance().GetScreenSupportedColorGamuts(rsId_, colorGamuts);
446     if (ret != StatusCode::SUCCESS) {
447         TLOGE(WmsLogTag::DMS, "SCB: fail! rsId %{public}" PRIu64", ret:%{public}d",
448             rsId_, ret);
449         return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
450     }
451     TLOGI(WmsLogTag::DMS, "SCB: ok! rsId %{public}" PRIu64", size %{public}u",
452         rsId_, static_cast<uint32_t>(colorGamuts.size()));
453 
454     return DMError::DM_OK;
455 }
456 
SetIsExtend(bool isExtend)457 void ScreenSession::SetIsExtend(bool isExtend)
458 {
459     isExtended_ = isExtend;
460 }
461 
GetIsExtend() const462 bool ScreenSession::GetIsExtend() const
463 {
464     return isExtended_;
465 }
466 
SetIsInternal(bool isInternal)467 void ScreenSession::SetIsInternal(bool isInternal)
468 {
469     isInternal_ = isInternal;
470 }
471 
GetIsInternal() const472 bool ScreenSession::GetIsInternal() const
473 {
474     return isInternal_;
475 }
476 
SetIsCurrentInUse(bool isInUse)477 void ScreenSession::SetIsCurrentInUse(bool isInUse)
478 {
479     isInUse_ = isInUse;
480 }
481 
GetIsCurrentInUse() const482 bool ScreenSession::GetIsCurrentInUse() const
483 {
484     return isInUse_;
485 }
486 
GetSessionId() const487 uint64_t ScreenSession::GetSessionId() const
488 {
489     return sessionId_;
490 }
491 
SetIsExtendVirtual(bool isExtendVirtual)492 void ScreenSession::SetIsExtendVirtual(bool isExtendVirtual)
493 {
494     isExtendVirtual_ = isExtendVirtual;
495 }
496 
GetIsExtendVirtual() const497 bool ScreenSession::GetIsExtendVirtual() const
498 {
499     return isExtendVirtual_;
500 }
501 
SetIsFakeInUse(bool isFakeInUse)502 void ScreenSession::SetIsFakeInUse(bool isFakeInUse)
503 {
504     isFakeInUse_ = isFakeInUse;
505 }
506 
GetIsFakeInUse() const507 bool ScreenSession::GetIsFakeInUse() const
508 {
509     return isFakeInUse_;
510 }
511 
SetIsRealScreen(bool isReal)512 void ScreenSession::SetIsRealScreen(bool isReal)
513 {
514     isReal_ = isReal;
515 }
516 
GetIsRealScreen()517 bool ScreenSession::GetIsRealScreen()
518 {
519     return isReal_;
520 }
521 
SetIsPcUse(bool isPcUse)522 void ScreenSession::SetIsPcUse(bool isPcUse)
523 {
524     isPcUse_ = isPcUse;
525 }
526 
GetIsPcUse()527 bool ScreenSession::GetIsPcUse()
528 {
529     return isPcUse_;
530 }
531 
SetIsFakeSession(bool isFakeSession)532 void ScreenSession::SetIsFakeSession(bool isFakeSession)
533 {
534     isFakeSession_ = isFakeSession;
535 }
536 
SetValidHeight(uint32_t validHeight)537 void ScreenSession::SetValidHeight(uint32_t validHeight)
538 {
539     property_.SetValidHeight(validHeight);
540 }
541 
SetValidWidth(uint32_t validWidth)542 void ScreenSession::SetValidWidth(uint32_t validWidth)
543 {
544     property_.SetValidWidth(validWidth);
545 }
546 
GetValidHeight() const547 uint32_t ScreenSession::GetValidHeight() const
548 {
549     return property_.GetValidHeight();
550 }
551 
GetValidWidth() const552 uint32_t ScreenSession::GetValidWidth() const
553 {
554     return property_.GetValidWidth();
555 }
556 
SetPointerActiveWidth(uint32_t pointerActiveWidth)557 void ScreenSession::SetPointerActiveWidth(uint32_t pointerActiveWidth)
558 {
559     property_.SetPointerActiveWidth(pointerActiveWidth);
560 }
561 
GetPointerActiveWidth()562 uint32_t ScreenSession::GetPointerActiveWidth()
563 {
564     return property_.GetPointerActiveWidth();
565 }
566 
SetPointerActiveHeight(uint32_t pointerActiveHeight)567 void ScreenSession::SetPointerActiveHeight(uint32_t pointerActiveHeight)
568 {
569     property_.SetPointerActiveHeight(pointerActiveHeight);
570 }
571 
GetPointerActiveHeight()572 uint32_t ScreenSession::GetPointerActiveHeight()
573 {
574     return property_.GetPointerActiveHeight();
575 }
576 
SetIsBScreenHalf(bool isBScreenHalf)577 void ScreenSession::SetIsBScreenHalf(bool isBScreenHalf)
578 {
579     isBScreenHalf_ = isBScreenHalf;
580 }
581 
GetIsBScreenHalf() const582 bool ScreenSession::GetIsBScreenHalf() const
583 {
584     return isBScreenHalf_;
585 }
586 
SetFakeScreenSession(sptr<ScreenSession> fakeScreenSession)587 void ScreenSession::SetFakeScreenSession(sptr<ScreenSession> fakeScreenSession)
588 {
589     fakeScreenSession_ = fakeScreenSession;
590 }
591 
GetFakeScreenSession() const592 sptr<ScreenSession> ScreenSession::GetFakeScreenSession() const
593 {
594     return fakeScreenSession_;
595 }
596 
GetName()597 std::string ScreenSession::GetName()
598 {
599     return name_;
600 }
601 
SetName(std::string name)602 void ScreenSession::SetName(std::string name)
603 {
604     name_ = name;
605 }
606 
GetInnerName()607 std::string ScreenSession::GetInnerName()
608 {
609     return innerName_;
610 }
611 
SetInnerName(std::string innerName)612 void ScreenSession::SetInnerName(std::string innerName)
613 {
614     innerName_ = innerName;
615 }
616 
SetMirrorScreenType(MirrorScreenType mirrorType)617 void ScreenSession::SetMirrorScreenType(MirrorScreenType mirrorType)
618 {
619     mirrorScreenType_ = mirrorType;
620 }
621 
GetMirrorScreenType()622 MirrorScreenType ScreenSession::GetMirrorScreenType()
623 {
624     return mirrorScreenType_;
625 }
626 
GetScreenId()627 ScreenId ScreenSession::GetScreenId()
628 {
629     return screenId_;
630 }
631 
GetRSScreenId()632 ScreenId ScreenSession::GetRSScreenId()
633 {
634     return rsId_;
635 }
636 
GetScreenShape() const637 ScreenShape ScreenSession::GetScreenShape() const
638 {
639     return property_.GetScreenShape();
640 }
641 
GetScreenProperty() const642 ScreenProperty ScreenSession::GetScreenProperty() const
643 {
644     std::lock_guard<std::mutex> lock(propertyMutex_);
645     return property_;
646 }
647 
SetSerialNumber(std::string serialNumber)648 void ScreenSession::SetSerialNumber(std::string serialNumber)
649 {
650     serialNumber_ = serialNumber;
651 }
652 
GetSerialNumber() const653 std::string ScreenSession::GetSerialNumber() const
654 {
655     return serialNumber_;
656 }
657 
SetScreenScale(float scaleX,float scaleY,float pivotX,float pivotY,float translateX,float translateY)658 void ScreenSession::SetScreenScale(float scaleX, float scaleY, float pivotX, float pivotY, float translateX,
659                                    float translateY)
660 {
661     property_.SetScaleX(scaleX);
662     property_.SetScaleY(scaleY);
663     property_.SetPivotX(pivotX);
664     property_.SetPivotY(pivotY);
665     property_.SetTranslateX(translateX);
666     property_.SetTranslateY(translateY);
667 }
668 
SetDefaultDeviceRotationOffset(uint32_t defaultRotationOffset)669 void ScreenSession::SetDefaultDeviceRotationOffset(uint32_t defaultRotationOffset)
670 {
671     TLOGI(WmsLogTag::DMS, "set device default rotation offset: %{public}d", defaultRotationOffset);
672     property_.SetDefaultDeviceRotationOffset(defaultRotationOffset);
673 }
674 
UpdatePropertyByActiveMode()675 void ScreenSession::UpdatePropertyByActiveMode()
676 {
677     sptr<SupportedScreenModes> mode = GetActiveScreenMode();
678     if (mode != nullptr) {
679         auto screeBounds = property_.GetBounds();
680         screeBounds.rect_.width_ = mode->width_;
681         screeBounds.rect_.height_ = mode->height_;
682         property_.SetBounds(screeBounds);
683     }
684 }
685 
UpdatePropertyByFoldControl(const ScreenProperty & updatedProperty,FoldDisplayMode foldDisplayMode)686 ScreenProperty ScreenSession::UpdatePropertyByFoldControl(const ScreenProperty& updatedProperty,
687     FoldDisplayMode foldDisplayMode)
688 {
689     property_.SetDpiPhyBounds(updatedProperty.GetPhyWidth(), updatedProperty.GetPhyHeight());
690     property_.SetPhyBounds(updatedProperty.GetPhyBounds());
691     property_.SetBounds(updatedProperty.GetBounds());
692     if (FoldScreenStateInternel::IsSecondaryDisplayFoldDevice()) {
693         DisplayOrientation deviceOrientation =
694             CalcDeviceOrientation(property_.GetScreenRotation(), foldDisplayMode);
695         property_.SetDisplayOrientation(deviceOrientation);
696         property_.SetDeviceOrientation(deviceOrientation);
697         property_.SetScreenAreaOffsetY(updatedProperty.GetScreenAreaOffsetY());
698         property_.SetScreenAreaHeight(updatedProperty.GetScreenAreaHeight());
699     }
700     UpdateTouchBoundsAndOffset();
701     return property_;
702 }
703 
UpdateDisplayState(DisplayState displayState)704 void ScreenSession::UpdateDisplayState(DisplayState displayState)
705 {
706     property_.SetDisplayState(displayState);
707 }
708 
UpdateRefreshRate(uint32_t refreshRate)709 void ScreenSession::UpdateRefreshRate(uint32_t refreshRate)
710 {
711     property_.SetRefreshRate(refreshRate);
712 }
713 
GetRefreshRate()714 uint32_t ScreenSession::GetRefreshRate()
715 {
716     return property_.GetRefreshRate();
717 }
718 
UpdatePropertyByResolution(uint32_t width,uint32_t height)719 void ScreenSession::UpdatePropertyByResolution(uint32_t width, uint32_t height)
720 {
721     auto screenBounds = property_.GetBounds();
722     screenBounds.rect_.width_ = width;
723     screenBounds.rect_.height_ = height;
724     property_.SetBounds(screenBounds);
725 }
726 
UpdatePropertyByFakeBounds(uint32_t width,uint32_t height)727 void ScreenSession::UpdatePropertyByFakeBounds(uint32_t width, uint32_t height)
728 {
729     auto screenFakeBounds = property_.GetFakeBounds();
730     screenFakeBounds.rect_.width_ = width;
731     screenFakeBounds.rect_.height_ = height;
732     property_.SetFakeBounds(screenFakeBounds);
733 }
734 
GetDisplayNode() const735 std::shared_ptr<RSDisplayNode> ScreenSession::GetDisplayNode() const
736 {
737     std::shared_lock<std::shared_mutex> displayNodeLock(displayNodeMutex_);
738     return displayNode_;
739 }
740 
ReleaseDisplayNode()741 void ScreenSession::ReleaseDisplayNode()
742 {
743     std::unique_lock<std::shared_mutex> displayNodeLock(displayNodeMutex_);
744     displayNode_ = nullptr;
745     TLOGI(WmsLogTag::DMS, "displayNode_ is released.");
746 }
747 
Connect()748 void ScreenSession::Connect()
749 {
750     std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
751     screenState_ = ScreenState::CONNECTION;
752     if (screenChangeListenerList_.empty()) {
753         TLOGE(WmsLogTag::DMS, "screenChangeListenerList is empty.");
754         return;
755     }
756     for (auto& listener : screenChangeListenerList_) {
757         if (!listener) {
758             TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
759             continue;
760         }
761         listener->OnConnect(screenId_);
762     }
763 }
764 
Disconnect()765 void ScreenSession::Disconnect()
766 {
767     std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
768     screenState_ = ScreenState::DISCONNECTION;
769     if (screenChangeListenerList_.empty()) {
770         TLOGE(WmsLogTag::DMS, "screenChangeListenerList is empty.");
771         return;
772     }
773     for (auto& listener : screenChangeListenerList_) {
774         if (!listener) {
775             TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
776             continue;
777         }
778         listener->OnDisconnect(screenId_);
779     }
780 }
781 
PropertyChange(const ScreenProperty & newProperty,ScreenPropertyChangeReason reason)782 void ScreenSession::PropertyChange(const ScreenProperty& newProperty, ScreenPropertyChangeReason reason)
783 {
784     std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
785     property_ = newProperty;
786     if (reason == ScreenPropertyChangeReason::VIRTUAL_PIXEL_RATIO_CHANGE) {
787         return;
788     }
789     if (screenChangeListenerList_.empty()) {
790         TLOGE(WmsLogTag::DMS, "screenChangeListenerList is empty.");
791         return;
792     }
793     for (auto& listener : screenChangeListenerList_) {
794         if (!listener) {
795             TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
796             continue;
797         }
798         listener->OnPropertyChange(newProperty, reason, screenId_);
799     }
800 }
801 
PowerStatusChange(DisplayPowerEvent event,EventStatus status,PowerStateChangeReason reason)802 void ScreenSession::PowerStatusChange(DisplayPowerEvent event, EventStatus status, PowerStateChangeReason reason)
803 {
804     std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
805     if (screenChangeListenerList_.empty()) {
806         TLOGE(WmsLogTag::DMS, "screenChangeListenerList is empty.");
807         return;
808     }
809     for (auto& listener : screenChangeListenerList_) {
810         if (!listener) {
811             TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
812             continue;
813         }
814         listener->OnPowerStatusChange(event, status, reason);
815     }
816 }
817 
ConvertRotationToFloat(Rotation sensorRotation)818 float ScreenSession::ConvertRotationToFloat(Rotation sensorRotation)
819 {
820     float rotation = 0.f;
821     switch (sensorRotation) {
822         case Rotation::ROTATION_90:
823             rotation = 90.f; // degree 90
824             break;
825         case Rotation::ROTATION_180:
826             rotation = 180.f; // degree 180
827             break;
828         case Rotation::ROTATION_270:
829             rotation = 270.f; // degree 270
830             break;
831         default:
832             rotation = 0.f;
833             break;
834     }
835     return rotation;
836 }
837 
HandleSensorRotation(float sensorRotation)838 void ScreenSession::HandleSensorRotation(float sensorRotation)
839 {
840     SensorRotationChange(sensorRotation);
841 }
842 
SensorRotationChange(Rotation sensorRotation)843 void ScreenSession::SensorRotationChange(Rotation sensorRotation)
844 {
845     float rotation = ConvertRotationToFloat(sensorRotation);
846     SensorRotationChange(rotation);
847 }
848 
SensorRotationChange(float sensorRotation)849 void ScreenSession::SensorRotationChange(float sensorRotation)
850 {
851     std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
852     if (sensorRotation >= 0.0f) {
853         currentValidSensorRotation_ = sensorRotation;
854     }
855     currentSensorRotation_ = sensorRotation;
856     for (auto& listener : screenChangeListenerList_) {
857         if (!listener) {
858             TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
859             continue;
860         }
861         listener->OnSensorRotationChange(sensorRotation, screenId_);
862     }
863 }
864 
GetValidSensorRotation()865 float ScreenSession::GetValidSensorRotation()
866 {
867     return currentValidSensorRotation_;
868 }
869 
HandleHoverStatusChange(int32_t hoverStatus,bool needRotate)870 void ScreenSession::HandleHoverStatusChange(int32_t hoverStatus, bool needRotate)
871 {
872     HoverStatusChange(hoverStatus, needRotate);
873 }
874 
HoverStatusChange(int32_t hoverStatus,bool needRotate)875 void ScreenSession::HoverStatusChange(int32_t hoverStatus, bool needRotate)
876 {
877     std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
878     for (auto& listener : screenChangeListenerList_) {
879         if (!listener) {
880             TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
881             continue;
882         }
883         listener->OnHoverStatusChange(hoverStatus, needRotate, screenId_);
884     }
885 }
886 
HandleCameraBackSelfieChange(bool isCameraBackSelfie)887 void ScreenSession::HandleCameraBackSelfieChange(bool isCameraBackSelfie)
888 {
889     CameraBackSelfieChange(isCameraBackSelfie);
890 }
891 
CameraBackSelfieChange(bool isCameraBackSelfie)892 void ScreenSession::CameraBackSelfieChange(bool isCameraBackSelfie)
893 {
894     std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
895     for (auto& listener : screenChangeListenerList_) {
896         if (!listener) {
897             TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
898             continue;
899         }
900         listener->OnCameraBackSelfieChange(isCameraBackSelfie, screenId_);
901     }
902 }
903 
ScreenExtendChange(ScreenId mainScreenId,ScreenId extendScreenId)904 void ScreenSession::ScreenExtendChange(ScreenId mainScreenId, ScreenId extendScreenId)
905 {
906     std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
907     for (auto& listener : screenChangeListenerList_) {
908         if (!listener) {
909             TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
910             continue;
911         }
912         listener->OnScreenExtendChange(mainScreenId, extendScreenId);
913     }
914 }
915 
ScreenOrientationChange(Orientation orientation,FoldDisplayMode foldDisplayMode)916 void ScreenSession::ScreenOrientationChange(Orientation orientation, FoldDisplayMode foldDisplayMode)
917 {
918     Rotation rotationAfter = CalcRotation(orientation, foldDisplayMode);
919     float screenRotation = ConvertRotationToFloat(rotationAfter);
920     ScreenOrientationChange(screenRotation);
921 }
922 
ScreenOrientationChange(float orientation)923 void ScreenSession::ScreenOrientationChange(float orientation)
924 {
925     std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
926     for (auto& listener : screenChangeListenerList_) {
927         if (!listener) {
928             TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
929             continue;
930         }
931         listener->OnScreenOrientationChange(orientation, screenId_);
932     }
933 }
934 
ConvertIntToRotation(int rotation)935 Rotation ScreenSession::ConvertIntToRotation(int rotation)
936 {
937     Rotation targetRotation = Rotation::ROTATION_0;
938     switch (rotation) {
939         case 90: // Rotation 90 degree
940             targetRotation = Rotation::ROTATION_90;
941             break;
942         case 180: // Rotation 180 degree
943             targetRotation = Rotation::ROTATION_180;
944             break;
945         case 270: // Rotation 270 degree
946             targetRotation = Rotation::ROTATION_270;
947             break;
948         default:
949             targetRotation = Rotation::ROTATION_0;
950             break;
951     }
952     return targetRotation;
953 }
954 
SetUpdateToInputManagerCallback(std::function<void (float)> updateToInputManagerCallback)955 void ScreenSession::SetUpdateToInputManagerCallback(std::function<void(float)> updateToInputManagerCallback)
956 {
957     updateToInputManagerCallback_ = updateToInputManagerCallback;
958 }
959 
SetUpdateScreenPivotCallback(std::function<void (float,float)> && updateScreenPivotCallback)960 void ScreenSession::SetUpdateScreenPivotCallback(std::function<void(float, float)>&& updateScreenPivotCallback)
961 {
962     updateScreenPivotCallback_ = std::move(updateScreenPivotCallback);
963 }
964 
GetVirtualScreenFlag()965 VirtualScreenFlag ScreenSession::GetVirtualScreenFlag()
966 {
967     return screenFlag_;
968 }
969 
SetVirtualScreenFlag(VirtualScreenFlag screenFlag)970 void ScreenSession::SetVirtualScreenFlag(VirtualScreenFlag screenFlag)
971 {
972     screenFlag_ = screenFlag;
973 }
974 
SetSecurity(bool isSecurity)975 void ScreenSession::SetSecurity(bool isSecurity)
976 {
977     isSecurity_ = isSecurity;
978 }
979 
UpdateTouchBoundsAndOffset()980 void ScreenSession::UpdateTouchBoundsAndOffset()
981 {
982     property_.SetPhysicalTouchBounds();
983     property_.SetInputOffsetY();
984     if (FoldScreenStateInternel::IsSecondaryDisplayFoldDevice()) {
985         property_.SetValidHeight(property_.GetBounds().rect_.GetHeight());
986         property_.SetValidWidth(property_.GetBounds().rect_.GetWidth());
987     }
988 }
989 
UpdateToInputManager(RRect bounds,int rotation,int deviceRotation,FoldDisplayMode foldDisplayMode)990 void ScreenSession::UpdateToInputManager(RRect bounds, int rotation, int deviceRotation,
991     FoldDisplayMode foldDisplayMode)
992 {
993     bool needUpdateToInputManager = false;
994     if (foldDisplayMode == FoldDisplayMode::FULL &&
995         property_.GetBounds() == bounds && property_.GetRotation() != static_cast<float>(rotation)) {
996         needUpdateToInputManager = true;
997     }
998     Rotation targetRotation = ConvertIntToRotation(rotation);
999     DisplayOrientation displayOrientation = CalcDisplayOrientation(targetRotation, foldDisplayMode);
1000     property_.SetBounds(bounds);
1001     property_.SetRotation(static_cast<float>(rotation));
1002     property_.UpdateScreenRotation(targetRotation);
1003     property_.SetDisplayOrientation(displayOrientation);
1004     UpdateTouchBoundsAndOffset();
1005     Rotation targetDeviceRotation = ConvertIntToRotation(deviceRotation);
1006     auto deviceOrientation = CalcDeviceOrientationWithBounds(targetDeviceRotation, foldDisplayMode, bounds);
1007     property_.UpdateDeviceRotation(targetDeviceRotation);
1008     property_.SetDeviceOrientation(deviceOrientation);
1009     if (needUpdateToInputManager && updateToInputManagerCallback_ != nullptr
1010         && g_screenRotationOffSet == ROTATION_270) {
1011         // fold phone need fix 90 degree by remainder 360 degree
1012         int foldRotation = (rotation + 90) % 360;
1013         updateToInputManagerCallback_(static_cast<float>(foldRotation));
1014         TLOGI(WmsLogTag::DMS, "updateToInputManagerCallback_:%{public}d", foldRotation);
1015     }
1016 }
1017 
SetPhysicalRotation(int rotation)1018 void ScreenSession::SetPhysicalRotation(int rotation)
1019 {
1020     property_.SetPhysicalRotation(static_cast<float>(rotation));
1021     TLOGI(WmsLogTag::DMS, "physicalrotation :%{public}f", property_.GetPhysicalRotation());
1022 }
1023 
SetScreenComponentRotation(int rotation)1024 void ScreenSession::SetScreenComponentRotation(int rotation)
1025 {
1026     property_.SetScreenComponentRotation(static_cast<float>(rotation));
1027     TLOGI(WmsLogTag::DMS, "screenComponentRotation :%{public}f ", property_.GetScreenComponentRotation());
1028 }
1029 
UpdatePropertyAfterRotation(RRect bounds,int rotation,FoldDisplayMode foldDisplayMode)1030 void ScreenSession::UpdatePropertyAfterRotation(RRect bounds, int rotation, FoldDisplayMode foldDisplayMode)
1031 {
1032     Rotation targetRotation = ConvertIntToRotation(rotation);
1033     DisplayOrientation displayOrientation = CalcDisplayOrientation(targetRotation, foldDisplayMode);
1034     property_.SetBounds(bounds);
1035     property_.SetRotation(static_cast<float>(rotation));
1036     property_.UpdateScreenRotation(targetRotation);
1037     property_.SetDisplayOrientation(displayOrientation);
1038     if (!isBScreenHalf_ || property_.GetIsFakeInUse()) {
1039         property_.SetValidHeight(bounds.rect_.GetHeight());
1040         property_.SetValidWidth(bounds.rect_.GetWidth());
1041     }
1042     UpdateTouchBoundsAndOffset();
1043     {
1044         std::shared_lock<std::shared_mutex> displayNodeLock(displayNodeMutex_);
1045         if (!displayNode_) {
1046             TLOGI(WmsLogTag::DMS, "update failed since null display node with \
1047                 rotation:%{public}d displayOrientation:%{public}u", rotation, displayOrientation);
1048             return;
1049         }
1050     }
1051     {
1052         AutoRSTransaction trans(GetRSUIContext());
1053         std::shared_lock<std::shared_mutex> displayNodeLock(displayNodeMutex_);
1054         displayNode_->SetScreenRotation(static_cast<uint32_t>(property_.GetDeviceRotation()));
1055     }
1056     TLOGI(WmsLogTag::DMS, "bounds:[%{public}f %{public}f %{public}f %{public}f],rotation:%{public}d,\
1057         displayOrientation:%{public}u, foldDisplayMode:%{public}u",
1058         property_.GetBounds().rect_.GetLeft(), property_.GetBounds().rect_.GetTop(),
1059         property_.GetBounds().rect_.GetWidth(), property_.GetBounds().rect_.GetHeight(),
1060         rotation, displayOrientation, foldDisplayMode);
1061     ReportNotifyModeChange(displayOrientation);
1062 }
1063 
UpdateDisplayNodeRotation(int rotation)1064 void ScreenSession::UpdateDisplayNodeRotation(int rotation)
1065 {
1066     Rotation targetRotation = ConvertIntToRotation(rotation);
1067     AutoRSTransaction trans(GetRSUIContext());
1068     std::shared_lock<std::shared_mutex> displayNodeLock(displayNodeMutex_);
1069     if (displayNode_ != nullptr) {
1070         displayNode_->SetScreenRotation(static_cast<uint32_t>(targetRotation));
1071         TLOGI(WmsLogTag::DMS, "Set RS screen rotation:%{public}d", targetRotation);
1072     }
1073 }
1074 
UpdatePropertyOnly(RRect bounds,int rotation,FoldDisplayMode foldDisplayMode)1075 void ScreenSession::UpdatePropertyOnly(RRect bounds, int rotation, FoldDisplayMode foldDisplayMode)
1076 {
1077     Rotation targetRotation = ConvertIntToRotation(rotation);
1078     DisplayOrientation displayOrientation = CalcDisplayOrientation(targetRotation, foldDisplayMode);
1079     property_.SetBounds(bounds);
1080     property_.SetRotation(static_cast<float>(rotation));
1081     property_.UpdateScreenRotation(targetRotation);
1082     property_.SetDisplayOrientation(displayOrientation);
1083     if (!isBScreenHalf_ || property_.GetIsFakeInUse()) {
1084         property_.SetValidHeight(bounds.rect_.GetHeight());
1085         property_.SetValidWidth(bounds.rect_.GetWidth());
1086     }
1087     UpdateTouchBoundsAndOffset();
1088     TLOGI(WmsLogTag::DMS, "bounds:[%{public}f %{public}f %{public}f %{public}f],\
1089         rotation:%{public}d, displayOrientation:%{public}u",
1090         property_.GetBounds().rect_.GetLeft(), property_.GetBounds().rect_.GetTop(),
1091         property_.GetBounds().rect_.GetWidth(), property_.GetBounds().rect_.GetHeight(),
1092         rotation, displayOrientation);
1093 }
1094 
UpdateRotationOrientation(int rotation,FoldDisplayMode foldDisplayMode,const RRect & bounds)1095 void ScreenSession::UpdateRotationOrientation(int rotation, FoldDisplayMode foldDisplayMode, const RRect& bounds)
1096 {
1097     Rotation targetRotation = ConvertIntToRotation(rotation);
1098     DisplayOrientation deviceOrientation = CalcDeviceOrientationWithBounds(targetRotation, foldDisplayMode, bounds);
1099     property_.UpdateDeviceRotation(targetRotation);
1100     property_.SetDeviceOrientation(deviceOrientation);
1101     TLOGI(WmsLogTag::DMS, "rotation:%{public}d, orientation:%{public}u", rotation, deviceOrientation);
1102 }
1103 
UpdatePropertyByFakeInUse(bool isFakeInUse)1104 void ScreenSession::UpdatePropertyByFakeInUse(bool isFakeInUse)
1105 {
1106     property_.SetIsFakeInUse(isFakeInUse);
1107 }
1108 
ReportNotifyModeChange(DisplayOrientation displayOrientation)1109 void ScreenSession::ReportNotifyModeChange(DisplayOrientation displayOrientation)
1110 {
1111     int32_t vhMode = 1;
1112     if (displayOrientation == DisplayOrientation::PORTRAIT_INVERTED ||
1113         displayOrientation == DisplayOrientation::PORTRAIT) {
1114         vhMode = 0;
1115     }
1116     int32_t ret = HiSysEventWrite(
1117         OHOS::HiviewDFX::HiSysEvent::Domain::WINDOW_MANAGER,
1118         "VH_MODE",
1119         OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
1120         "MODE", vhMode);
1121     if (ret != 0) {
1122         TLOGE(WmsLogTag::DMS, "ReportNotifyModeChange Write HiSysEvent error, ret: %{public}d", ret);
1123     }
1124 }
1125 
UpdateRotationAfterBoot(bool foldToExpand)1126 void ScreenSession::UpdateRotationAfterBoot(bool foldToExpand)
1127 {
1128     TLOGI(WmsLogTag::DMS, "foldToExpand: %{public}d, Rotation: %{public}f",
1129         static_cast<int32_t>(foldToExpand), currentSensorRotation_);
1130     if (foldToExpand) {
1131         SensorRotationChange(currentSensorRotation_);
1132     }
1133 }
1134 
UpdateValidRotationToScb()1135 void ScreenSession::UpdateValidRotationToScb()
1136 {
1137     TLOGI(WmsLogTag::DMS, "Rotation: %{public}f", currentValidSensorRotation_);
1138     SensorRotationChange(currentValidSensorRotation_);
1139 }
1140 
GetActiveScreenMode() const1141 sptr<SupportedScreenModes> ScreenSession::GetActiveScreenMode() const
1142 {
1143     std::shared_lock<std::shared_mutex> lock(modesMutex_);
1144     if (activeIdx_ < 0 || activeIdx_ >= static_cast<int32_t>(modes_.size())) {
1145         TLOGW(WmsLogTag::DMS, "SCB: active mode index is wrong: %{public}d", activeIdx_);
1146         return nullptr;
1147     }
1148     return modes_[activeIdx_];
1149 }
1150 
SetBounds(RRect screenBounds)1151 void ScreenSession::SetBounds(RRect screenBounds)
1152 {
1153     property_.SetBounds(screenBounds);
1154 }
1155 
SetHorizontalRotation()1156 void ScreenSession::SetHorizontalRotation()
1157 {
1158     property_.SetRotation(HORIZONTAL);
1159     property_.UpdateScreenRotation(Rotation::ROTATION_270);
1160     property_.SetScreenRotation(Rotation::ROTATION_270);
1161     property_.UpdateDeviceRotation(Rotation::ROTATION_270);
1162     property_.SetDisplayOrientation(DisplayOrientation::LANDSCAPE_INVERTED);
1163     property_.SetDeviceOrientation(DisplayOrientation::LANDSCAPE_INVERTED);
1164     property_.SetScreenComponentRotation(HORIZONTAL);
1165     property_.SetPhysicalRotation(HORIZONTAL);
1166     currentSensorRotation_ = HORIZONTAL;
1167     if (displayNode_ != nullptr) {
1168         displayNode_->SetScreenRotation(static_cast<uint32_t>(ROTATION_270));
1169         RSTransactionAdapter::FlushImplicitTransaction(displayNode_);
1170     } else {
1171         TLOGW(WmsLogTag::DMS, "displayNode is null, no need to set displayNode.");
1172     }
1173 }
1174 
GetOrientation() const1175 Orientation ScreenSession::GetOrientation() const
1176 {
1177     return property_.GetOrientation();
1178 }
1179 
SetOrientation(Orientation orientation)1180 void ScreenSession::SetOrientation(Orientation orientation)
1181 {
1182     property_.SetOrientation(orientation);
1183 }
1184 
GetRotation() const1185 Rotation ScreenSession::GetRotation() const
1186 {
1187     return property_.GetScreenRotation();
1188 }
1189 
SetRotation(Rotation rotation)1190 void ScreenSession::SetRotation(Rotation rotation)
1191 {
1192     property_.SetScreenRotation(rotation);
1193 }
1194 
SetRotationAndScreenRotationOnly(Rotation rotation)1195 void ScreenSession::SetRotationAndScreenRotationOnly(Rotation rotation)
1196 {
1197     property_.SetRotationAndScreenRotationOnly(rotation);
1198 }
1199 
SetScreenRequestedOrientation(Orientation orientation)1200 void ScreenSession::SetScreenRequestedOrientation(Orientation orientation)
1201 {
1202     property_.SetScreenRequestedOrientation(orientation);
1203 }
1204 
SetScreenRotationLocked(bool isLocked)1205 void ScreenSession::SetScreenRotationLocked(bool isLocked)
1206 {
1207     std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
1208     isScreenLocked_ = isLocked;
1209     if (screenChangeListenerList_.empty()) {
1210         TLOGE(WmsLogTag::DMS, "screenChangeListenerList is empty.");
1211         return;
1212     }
1213     for (auto& listener : screenChangeListenerList_) {
1214         if (!listener) {
1215             TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
1216             continue;
1217         }
1218         listener->OnScreenRotationLockedChange(isLocked, screenId_);
1219     }
1220 }
1221 
SetScreenRotationLockedFromJs(bool isLocked)1222 void ScreenSession::SetScreenRotationLockedFromJs(bool isLocked)
1223 {
1224     isScreenLocked_ = isLocked;
1225 }
1226 
IsScreenRotationLocked()1227 bool ScreenSession::IsScreenRotationLocked()
1228 {
1229     return isScreenLocked_;
1230 }
1231 
SetTouchEnabledFromJs(bool isTouchEnabled)1232 void ScreenSession::SetTouchEnabledFromJs(bool isTouchEnabled)
1233 {
1234     TLOGI(WmsLogTag::WMS_EVENT, "%{public}u", isTouchEnabled);
1235     touchEnabled_.store(isTouchEnabled);
1236 }
1237 
IsTouchEnabled()1238 bool ScreenSession::IsTouchEnabled()
1239 {
1240     return touchEnabled_.load();
1241 }
1242 
GetScreenRequestedOrientation() const1243 Orientation ScreenSession::GetScreenRequestedOrientation() const
1244 {
1245     return property_.GetScreenRequestedOrientation();
1246 }
1247 
SetVirtualPixelRatio(float virtualPixelRatio)1248 void ScreenSession::SetVirtualPixelRatio(float virtualPixelRatio)
1249 {
1250     property_.SetVirtualPixelRatio(virtualPixelRatio);
1251 }
1252 
SetScreenSceneDpiChangeListener(const SetScreenSceneDpiFunc & func)1253 void ScreenSession::SetScreenSceneDpiChangeListener(const SetScreenSceneDpiFunc& func)
1254 {
1255     setScreenSceneDpiCallback_ = func;
1256     TLOGI(WmsLogTag::DMS, "SetScreenSceneDpiChangeListener");
1257 }
1258 
SetScreenSceneDpi(float density)1259 void ScreenSession::SetScreenSceneDpi(float density)
1260 {
1261     if (setScreenSceneDpiCallback_ == nullptr) {
1262         TLOGI(WmsLogTag::DMS, "setScreenSceneDpiCallback_ is nullptr");
1263         return;
1264     }
1265     setScreenSceneDpiCallback_(density);
1266 }
1267 
SetScreenSceneDestroyListener(const DestroyScreenSceneFunc & func)1268 void ScreenSession::SetScreenSceneDestroyListener(const DestroyScreenSceneFunc& func)
1269 {
1270     destroyScreenSceneCallback_  = func;
1271     TLOGI(WmsLogTag::DMS, "SetScreenSceneDestroyListener");
1272 }
1273 
DestroyScreenScene()1274 void ScreenSession::DestroyScreenScene()
1275 {
1276     if (destroyScreenSceneCallback_  == nullptr) {
1277         TLOGI(WmsLogTag::DMS, "destroyScreenSceneCallback_  is nullptr");
1278         return;
1279     }
1280     destroyScreenSceneCallback_();
1281 }
1282 
SetDensityInCurResolution(float densityInCurResolution)1283 void ScreenSession::SetDensityInCurResolution(float densityInCurResolution)
1284 {
1285     property_.SetDensityInCurResolution(densityInCurResolution);
1286 }
1287 
GetDensityInCurResolution()1288 float ScreenSession::GetDensityInCurResolution()
1289 {
1290     return property_.GetDensityInCurResolution();
1291 }
1292 
SetDefaultDensity(float defaultDensity)1293 void ScreenSession::SetDefaultDensity(float defaultDensity)
1294 {
1295     property_.SetDefaultDensity(defaultDensity);
1296 }
1297 
UpdateVirtualPixelRatio(const RRect & bounds)1298 void ScreenSession::UpdateVirtualPixelRatio(const RRect& bounds)
1299 {
1300     property_.UpdateVirtualPixelRatio(bounds);
1301 }
1302 
SetScreenType(ScreenType type)1303 void ScreenSession::SetScreenType(ScreenType type)
1304 {
1305     property_.SetScreenType(type);
1306 }
1307 
CalcRotation(Orientation orientation,FoldDisplayMode foldDisplayMode) const1308 Rotation ScreenSession::CalcRotation(Orientation orientation, FoldDisplayMode foldDisplayMode) const
1309 {
1310     sptr<SupportedScreenModes> info = GetActiveScreenMode();
1311     if (info == nullptr) {
1312         return Rotation::ROTATION_0;
1313     }
1314     // vertical: phone(Plugin screen); horizontal: pad & external screen
1315     bool isVerticalScreen = info->width_ < info->height_;
1316     if (foldDisplayMode != FoldDisplayMode::UNKNOWN &&
1317         (g_screenRotationOffSet == ROTATION_90 || g_screenRotationOffSet == ROTATION_270)) {
1318         isVerticalScreen = info->width_ > info->height_;
1319     }
1320     switch (orientation) {
1321         case Orientation::UNSPECIFIED: {
1322             return Rotation::ROTATION_0;
1323         }
1324         case Orientation::VERTICAL: {
1325             return isVerticalScreen ? Rotation::ROTATION_0 : Rotation::ROTATION_90;
1326         }
1327         case Orientation::HORIZONTAL: {
1328             return isVerticalScreen ? Rotation::ROTATION_90 : Rotation::ROTATION_0;
1329         }
1330         case Orientation::REVERSE_VERTICAL: {
1331             return isVerticalScreen ? Rotation::ROTATION_180 : Rotation::ROTATION_270;
1332         }
1333         case Orientation::REVERSE_HORIZONTAL: {
1334             return isVerticalScreen ? Rotation::ROTATION_270 : Rotation::ROTATION_180;
1335         }
1336         default: {
1337             TLOGE(WmsLogTag::DMS, "unknown orientation %{public}u", orientation);
1338             return Rotation::ROTATION_0;
1339         }
1340     }
1341 }
1342 
CalcDisplayOrientation(Rotation rotation,FoldDisplayMode foldDisplayMode) const1343 DisplayOrientation ScreenSession::CalcDisplayOrientation(Rotation rotation,
1344     FoldDisplayMode foldDisplayMode) const
1345 {
1346     // vertical: phone(Plugin screen); horizontal: pad & external screen
1347     bool isVerticalScreen = property_.GetPhyWidth() < property_.GetPhyHeight();
1348     if (g_screenScanType == SCAN_TYPE_VERTICAL) {
1349         isVerticalScreen = false;
1350     }
1351     if (foldDisplayMode != FoldDisplayMode::UNKNOWN
1352         && (g_screenRotationOffSet == ROTATION_90 || g_screenRotationOffSet == ROTATION_270)) {
1353         TLOGD(WmsLogTag::DMS, "foldDisplay is verticalScreen when width is greater than height");
1354         isVerticalScreen = property_.GetPhyWidth() > property_.GetPhyHeight();
1355     }
1356     if (FoldScreenStateInternel::IsSecondaryDisplayFoldDevice()) {
1357         isVerticalScreen = true;
1358     }
1359     if (foldDisplayMode == FoldDisplayMode::GLOBAL_FULL) {
1360         uint32_t temp = (static_cast<uint32_t>(rotation) + SECONDARY_ROTATION_270) % SECONDARY_ROTATION_MOD;
1361         rotation = static_cast<Rotation>(temp);
1362         isVerticalScreen = true;
1363     }
1364     switch (rotation) {
1365         case Rotation::ROTATION_0: {
1366             return isVerticalScreen ? DisplayOrientation::PORTRAIT : DisplayOrientation::LANDSCAPE;
1367         }
1368         case Rotation::ROTATION_90: {
1369             return isVerticalScreen ? DisplayOrientation::LANDSCAPE : DisplayOrientation::PORTRAIT;
1370         }
1371         case Rotation::ROTATION_180: {
1372             return isVerticalScreen ? DisplayOrientation::PORTRAIT_INVERTED : DisplayOrientation::LANDSCAPE_INVERTED;
1373         }
1374         case Rotation::ROTATION_270: {
1375             return isVerticalScreen ? DisplayOrientation::LANDSCAPE_INVERTED : DisplayOrientation::PORTRAIT_INVERTED;
1376         }
1377         default: {
1378             TLOGE(WmsLogTag::DMS, "unknown rotation %{public}u", rotation);
1379             return DisplayOrientation::UNKNOWN;
1380         }
1381     }
1382 }
1383 
CalcDeviceOrientation(Rotation rotation,FoldDisplayMode foldDisplayMode) const1384 DisplayOrientation ScreenSession::CalcDeviceOrientation(Rotation rotation,
1385     FoldDisplayMode foldDisplayMode) const
1386 {
1387     if (foldDisplayMode == FoldDisplayMode::GLOBAL_FULL) {
1388         uint32_t temp = (static_cast<uint32_t>(rotation) + SECONDARY_ROTATION_270) % SECONDARY_ROTATION_MOD;
1389         rotation = static_cast<Rotation>(temp);
1390     }
1391     DisplayOrientation displayRotation = DisplayOrientation::UNKNOWN;
1392     switch (rotation) {
1393         case Rotation::ROTATION_0: {
1394             displayRotation = DisplayOrientation::PORTRAIT;
1395             break;
1396         }
1397         case Rotation::ROTATION_90: {
1398             displayRotation = DisplayOrientation::LANDSCAPE;
1399             break;
1400         }
1401         case Rotation::ROTATION_180: {
1402             displayRotation = DisplayOrientation::PORTRAIT_INVERTED;
1403             break;
1404         }
1405         case Rotation::ROTATION_270: {
1406             displayRotation = DisplayOrientation::LANDSCAPE_INVERTED;
1407             break;
1408         }
1409         default: {
1410             TLOGE(WmsLogTag::DMS, "unknown rotation %{public}u", rotation);
1411         }
1412     }
1413     return displayRotation;
1414 }
1415 
CalcDeviceOrientationWithBounds(Rotation rotation,FoldDisplayMode foldDisplayMode,const RRect & bounds) const1416 DisplayOrientation ScreenSession::CalcDeviceOrientationWithBounds(Rotation rotation,
1417     FoldDisplayMode foldDisplayMode, const RRect& bounds) const
1418 {
1419     if (foldDisplayMode == FoldDisplayMode::GLOBAL_FULL) {
1420         uint32_t temp = (static_cast<uint32_t>(rotation) + SECONDARY_ROTATION_270) % SECONDARY_ROTATION_MOD;
1421         rotation = static_cast<Rotation>(temp);
1422     } else if (foldDisplayMode == FoldDisplayMode::UNKNOWN) {
1423         // deal vision and extend screen orientation
1424         bool isLandscapeScreen = bounds.rect_.GetWidth() > bounds.rect_.GetHeight();
1425         bool isPortraitScreen = bounds.rect_.GetWidth() < bounds.rect_.GetHeight();
1426         uint32_t rotationDirection = static_cast<uint32_t>(rotation) % LANDSCAPE_ROTATION_MOD;
1427         TLOGI(WmsLogTag::DMS, "isLandscapeScreen: %{public}d, rotationDirection: %{public}d",
1428             isLandscapeScreen, rotationDirection);
1429         if ((isLandscapeScreen && rotationDirection == IS_PORTRAIT) ||
1430             (isPortraitScreen && rotationDirection == IS_LANDSCAPE)) {
1431             uint32_t temp = (static_cast<uint32_t>(rotation) + ROTATION_90) % SECONDARY_ROTATION_MOD;
1432             TLOGI(WmsLogTag::DMS, "before: %{public}d, after: %{public}d", rotation, temp);
1433             rotation = static_cast<Rotation>(temp);
1434         }
1435     }
1436     DisplayOrientation displayRotation = DisplayOrientation::UNKNOWN;
1437     switch (rotation) {
1438         case Rotation::ROTATION_0: {
1439             displayRotation = DisplayOrientation::PORTRAIT;
1440             break;
1441         }
1442         case Rotation::ROTATION_90: {
1443             displayRotation = DisplayOrientation::LANDSCAPE;
1444             break;
1445         }
1446         case Rotation::ROTATION_180: {
1447             displayRotation = DisplayOrientation::PORTRAIT_INVERTED;
1448             break;
1449         }
1450         case Rotation::ROTATION_270: {
1451             displayRotation = DisplayOrientation::LANDSCAPE_INVERTED;
1452             break;
1453         }
1454         default: {
1455             TLOGE(WmsLogTag::DMS, "unknown rotation %{public}u", rotation);
1456         }
1457     }
1458     return displayRotation;
1459 }
1460 
GetSourceMode() const1461 ScreenSourceMode ScreenSession::GetSourceMode() const
1462 {
1463     if (!isPcUse_ && screenId_ == defaultScreenId_) {
1464         return ScreenSourceMode::SCREEN_MAIN;
1465     }
1466     ScreenCombination combination = GetScreenCombination();
1467     switch (combination) {
1468         case ScreenCombination::SCREEN_MAIN: {
1469             return ScreenSourceMode::SCREEN_MAIN;
1470         }
1471         case ScreenCombination::SCREEN_MIRROR: {
1472             return ScreenSourceMode::SCREEN_MIRROR;
1473         }
1474         case ScreenCombination::SCREEN_EXPAND: {
1475             return ScreenSourceMode::SCREEN_EXTEND;
1476         }
1477         case ScreenCombination::SCREEN_ALONE: {
1478             return ScreenSourceMode::SCREEN_ALONE;
1479         }
1480         case ScreenCombination::SCREEN_UNIQUE: {
1481             return ScreenSourceMode::SCREEN_UNIQUE;
1482         }
1483         case ScreenCombination::SCREEN_EXTEND: {
1484             return ScreenSourceMode::SCREEN_EXTEND;
1485         }
1486         default: {
1487             return ScreenSourceMode::SCREEN_ALONE;
1488         }
1489     }
1490 }
1491 
SetScreenCombination(ScreenCombination combination)1492 void ScreenSession::SetScreenCombination(ScreenCombination combination)
1493 {
1494     TLOGI(WmsLogTag::DMS, "screenId:%{public}" PRIu64", set combination:%{public}d", screenId_,
1495         static_cast<int32_t>(combination));
1496     std::lock_guard<std::mutex> lock(combinationMutex_);
1497     combination_ = combination;
1498 }
1499 
GetScreenCombination() const1500 ScreenCombination ScreenSession::GetScreenCombination() const
1501 {
1502     std::lock_guard<std::mutex> lock(combinationMutex_);
1503     return combination_;
1504 }
1505 
GetDisplaySourceMode() const1506 DisplaySourceMode ScreenSession::GetDisplaySourceMode() const
1507 {
1508     if (!isPcUse_ && screenId_ == defaultScreenId_) {
1509         return DisplaySourceMode::MAIN;
1510     }
1511     ScreenCombination combination = GetScreenCombination();
1512     switch (combination) {
1513         case ScreenCombination::SCREEN_MAIN: {
1514             return DisplaySourceMode::MAIN;
1515         }
1516         case ScreenCombination::SCREEN_MIRROR: {
1517             return DisplaySourceMode::MIRROR;
1518         }
1519         case ScreenCombination::SCREEN_EXPAND: {
1520             return DisplaySourceMode::EXTEND;
1521         }
1522         case ScreenCombination::SCREEN_EXTEND: {
1523             return DisplaySourceMode::EXTEND;
1524         }
1525         case ScreenCombination::SCREEN_UNIQUE: {
1526             return DisplaySourceMode::ALONE;
1527         }
1528         case ScreenCombination::SCREEN_ALONE: {
1529             return DisplaySourceMode::NONE;
1530         }
1531         default: {
1532             return DisplaySourceMode::NONE;
1533         }
1534     }
1535 }
1536 
FillScreenInfo(sptr<ScreenInfo> info) const1537 void ScreenSession::FillScreenInfo(sptr<ScreenInfo> info) const
1538 {
1539     if (info == nullptr) {
1540         TLOGE(WmsLogTag::DMS, "FillScreenInfo failed! info is nullptr");
1541         return;
1542     }
1543     info->SetRsId(rsId_);
1544     info->SetScreenId(screenId_);
1545     info->SetName(name_);
1546     info->SetIsExtend(GetIsExtend());
1547     uint32_t width = 0;
1548     uint32_t height = 0;
1549     if (isPcUse_) {
1550         RRect bounds = property_.GetBounds();
1551         width = bounds.rect_.GetWidth();
1552         height = bounds.rect_.GetHeight();
1553     } else {
1554         sptr<SupportedScreenModes> screenSessionModes = GetActiveScreenMode();
1555         if (screenSessionModes != nullptr) {
1556             height = screenSessionModes->height_;
1557             width = screenSessionModes->width_;
1558         }
1559     }
1560 
1561     float virtualPixelRatio = property_.GetVirtualPixelRatio();
1562     // "< 1e-set6" means virtualPixelRatio is 0.
1563     if (fabsf(virtualPixelRatio) < 1e-6) {
1564         virtualPixelRatio = 1.0f;
1565     }
1566     ScreenSourceMode sourceMode = GetSourceMode();
1567     info->SetVirtualPixelRatio(property_.GetVirtualPixelRatio());
1568     info->SetVirtualHeight(height / virtualPixelRatio);
1569     info->SetVirtualWidth(width / virtualPixelRatio);
1570     info->SetRotation(property_.GetScreenRotation());
1571     info->SetOrientation(static_cast<Orientation>(property_.GetDisplayOrientation()));
1572     info->SetSourceMode(sourceMode);
1573     info->SetType(property_.GetScreenType());
1574     info->SetModeId(activeIdx_);
1575     info->SetSerialNumber(serialNumber_);
1576     info->SetMirrorWidth(property_.GetMirrorWidth());
1577     info->SetMirrorHeight(property_.GetMirrorHeight());
1578 
1579     info->lastParent_ = lastGroupSmsId_;
1580     info->parent_ = groupSmsId_;
1581     info->isScreenGroup_ = isScreenGroup_;
1582     info->modes_ = modes_;
1583 }
1584 
ConvertToScreenInfo() const1585 sptr<ScreenInfo> ScreenSession::ConvertToScreenInfo() const
1586 {
1587     sptr<ScreenInfo> info = new(std::nothrow) ScreenInfo();
1588     if (info == nullptr) {
1589         return nullptr;
1590     }
1591     FillScreenInfo(info);
1592     return info;
1593 }
1594 
GetScreenColorGamut(ScreenColorGamut & colorGamut)1595 DMError ScreenSession::GetScreenColorGamut(ScreenColorGamut& colorGamut)
1596 {
1597 #ifdef WM_SCREEN_COLOR_GAMUT_ENABLE
1598     auto ret = RSInterfaces::GetInstance().GetScreenColorGamut(rsId_, colorGamut);
1599     if (ret != StatusCode::SUCCESS) {
1600         TLOGE(WmsLogTag::DMS, "GetScreenColorGamut fail! rsId %{public}" PRIu64"", rsId_);
1601         return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
1602     }
1603     TLOGI(WmsLogTag::DMS, "GetScreenColorGamut ok! rsId %{public}" PRIu64", colorGamut %{public}u",
1604         rsId_, static_cast<uint32_t>(colorGamut));
1605 #endif
1606     return DMError::DM_OK;
1607 }
1608 
SetScreenColorGamut(int32_t colorGamutIdx)1609 DMError ScreenSession::SetScreenColorGamut(int32_t colorGamutIdx)
1610 {
1611 #ifdef WM_SCREEN_COLOR_GAMUT_ENABLE
1612     std::vector<ScreenColorGamut> colorGamuts;
1613     DMError res = GetScreenSupportedColorGamuts(colorGamuts);
1614     if (res != DMError::DM_OK) {
1615         TLOGE(WmsLogTag::DMS, "SetScreenColorGamut fail! rsId %{public}" PRIu64"", rsId_);
1616         return res;
1617     }
1618     if (colorGamutIdx < 0 || colorGamutIdx >= static_cast<int32_t>(colorGamuts.size())) {
1619         TLOGE(WmsLogTag::DMS, "SetScreenColorGamut fail! rsId %{public}" PRIu64" colorGamutIdx %{public}d invalid.",
1620             rsId_, colorGamutIdx);
1621         return DMError::DM_ERROR_INVALID_PARAM;
1622     }
1623     auto ret = RSInterfaces::GetInstance().SetScreenColorGamut(rsId_, colorGamutIdx);
1624     if (ret != StatusCode::SUCCESS) {
1625         TLOGE(WmsLogTag::DMS, "SetScreenColorGamut fail! rsId %{public}" PRIu64"", rsId_);
1626         return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
1627     }
1628     TLOGI(WmsLogTag::DMS, "SetScreenColorGamut ok! rsId %{public}" PRIu64", colorGamutIdx %{public}u",
1629         rsId_, colorGamutIdx);
1630 #endif
1631     return DMError::DM_OK;
1632 }
1633 
GetScreenGamutMap(ScreenGamutMap & gamutMap)1634 DMError ScreenSession::GetScreenGamutMap(ScreenGamutMap& gamutMap)
1635 {
1636 #ifdef WM_SCREEN_COLOR_GAMUT_ENABLE
1637     auto ret = RSInterfaces::GetInstance().GetScreenGamutMap(rsId_, gamutMap);
1638     if (ret != StatusCode::SUCCESS) {
1639         TLOGE(WmsLogTag::DMS, "GetScreenGamutMap fail! rsId %{public}" PRIu64"", rsId_);
1640         return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
1641     }
1642     TLOGI(WmsLogTag::DMS, "GetScreenGamutMap ok! rsId %{public}" PRIu64", gamutMap %{public}u",
1643         rsId_, static_cast<uint32_t>(gamutMap));
1644 #endif
1645     return DMError::DM_OK;
1646 }
1647 
SetScreenGamutMap(ScreenGamutMap gamutMap)1648 DMError ScreenSession::SetScreenGamutMap(ScreenGamutMap gamutMap)
1649 {
1650 #ifdef WM_SCREEN_COLOR_GAMUT_ENABLE
1651     if (gamutMap > GAMUT_MAP_HDR_EXTENSION) {
1652         return DMError::DM_ERROR_INVALID_PARAM;
1653     }
1654     auto ret = RSInterfaces::GetInstance().SetScreenGamutMap(rsId_, gamutMap);
1655     if (ret != StatusCode::SUCCESS) {
1656         TLOGE(WmsLogTag::DMS, "SetScreenGamutMap fail! rsId %{public}" PRIu64"", rsId_);
1657         return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
1658     }
1659     TLOGI(WmsLogTag::DMS, "SetScreenGamutMap ok! rsId %{public}" PRIu64", gamutMap %{public}u",
1660         rsId_, static_cast<uint32_t>(gamutMap));
1661 #endif
1662     return DMError::DM_OK;
1663 }
1664 
SetScreenColorTransform()1665 DMError ScreenSession::SetScreenColorTransform()
1666 {
1667     TLOGI(WmsLogTag::DMS, "SetScreenColorTransform ok! rsId %{public}" PRIu64"", rsId_);
1668     return DMError::DM_OK;
1669 }
1670 
GetPixelFormat(GraphicPixelFormat & pixelFormat)1671 DMError ScreenSession::GetPixelFormat(GraphicPixelFormat& pixelFormat)
1672 {
1673     auto ret = RSInterfaces::GetInstance().GetPixelFormat(rsId_, pixelFormat);
1674     if (ret != StatusCode::SUCCESS) {
1675         TLOGE(WmsLogTag::DMS, "GetPixelFormat fail! rsId %{public}" PRIu64, rsId_);
1676         return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
1677     }
1678     TLOGI(WmsLogTag::DMS, "GetPixelFormat ok! rsId %{public}" PRIu64 ", pixelFormat %{public}u",
1679         rsId_, static_cast<uint32_t>(pixelFormat));
1680     return DMError::DM_OK;
1681 }
1682 
SetPixelFormat(GraphicPixelFormat pixelFormat)1683 DMError ScreenSession::SetPixelFormat(GraphicPixelFormat pixelFormat)
1684 {
1685     if (pixelFormat > GRAPHIC_PIXEL_FMT_VENDER_MASK) {
1686         return DMError::DM_ERROR_INVALID_PARAM;
1687     }
1688     auto ret = RSInterfaces::GetInstance().SetPixelFormat(rsId_, pixelFormat);
1689     if (ret != StatusCode::SUCCESS) {
1690         TLOGE(WmsLogTag::DMS, "SetPixelFormat fail! rsId %{public}" PRIu64, rsId_);
1691         return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
1692     }
1693     TLOGI(WmsLogTag::DMS, "SetPixelFormat ok! rsId %{public}" PRIu64 ", gamutMap %{public}u",
1694         rsId_, static_cast<uint32_t>(pixelFormat));
1695     return DMError::DM_OK;
1696 }
1697 
GetSupportedHDRFormats(std::vector<ScreenHDRFormat> & hdrFormats)1698 DMError ScreenSession::GetSupportedHDRFormats(std::vector<ScreenHDRFormat>& hdrFormats)
1699 {
1700 #ifdef WM_SCREEN_HDR_FORMAT_ENABLE
1701     auto ret = RSInterfaces::GetInstance().GetScreenSupportedHDRFormats(rsId_, hdrFormats);
1702     if (ret != StatusCode::SUCCESS) {
1703         TLOGE(WmsLogTag::DMS, "SCB: fail! rsId %{public}" PRIu64 ", ret:%{public}d",
1704             rsId_, ret);
1705         return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
1706     }
1707     TLOGI(WmsLogTag::DMS, "SCB: ok! rsId %{public}" PRIu64 ", size %{public}u",
1708         rsId_, static_cast<uint32_t>(hdrFormats.size()));
1709 #endif
1710     return DMError::DM_OK;
1711 }
1712 
GetScreenHDRFormat(ScreenHDRFormat & hdrFormat)1713 DMError ScreenSession::GetScreenHDRFormat(ScreenHDRFormat& hdrFormat)
1714 {
1715 #ifdef WM_SCREEN_HDR_FORMAT_ENABLE
1716     auto ret = RSInterfaces::GetInstance().GetScreenHDRFormat(rsId_, hdrFormat);
1717     if (ret != StatusCode::SUCCESS) {
1718         TLOGE(WmsLogTag::DMS, "GetScreenHDRFormat fail! rsId %{public}" PRIu64, rsId_);
1719         return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
1720     }
1721     TLOGI(WmsLogTag::DMS, "GetScreenHDRFormat ok! rsId %{public}" PRIu64 ", colorSpace %{public}u",
1722         rsId_, static_cast<uint32_t>(hdrFormat));
1723 #endif
1724     return DMError::DM_OK;
1725 }
1726 
SetScreenHDRFormat(int32_t modeIdx)1727 DMError ScreenSession::SetScreenHDRFormat(int32_t modeIdx)
1728 {
1729 #ifdef WM_SCREEN_HDR_FORMAT_ENABLE
1730     std::vector<ScreenHDRFormat> hdrFormats;
1731     DMError res = GetSupportedHDRFormats(hdrFormats);
1732     if (res != DMError::DM_OK) {
1733         TLOGE(WmsLogTag::DMS, "SetScreenHDRFormat fail! rsId %{public}" PRIu64, rsId_);
1734         return res;
1735     }
1736     if (modeIdx < 0 || modeIdx >= static_cast<int32_t>(hdrFormats.size())) {
1737         TLOGE(WmsLogTag::DMS, "SetScreenHDRFormat fail! rsId %{public}" PRIu64 " modeIdx %{public}d invalid.",
1738             rsId_, modeIdx);
1739         return DMError::DM_ERROR_INVALID_PARAM;
1740     }
1741     auto ret = RSInterfaces::GetInstance().SetScreenHDRFormat(rsId_, modeIdx);
1742     if (ret != StatusCode::SUCCESS) {
1743         TLOGE(WmsLogTag::DMS, "SetScreenHDRFormat fail! rsId %{public}" PRIu64, rsId_);
1744         return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
1745     }
1746     TLOGI(WmsLogTag::DMS, "SetScreenHDRFormat ok! rsId %{public}" PRIu64 ", modeIdx %{public}d",
1747         rsId_, modeIdx);
1748 #endif
1749     return DMError::DM_OK;
1750 }
1751 
GetSupportedColorSpaces(std::vector<GraphicCM_ColorSpaceType> & colorSpaces)1752 DMError ScreenSession::GetSupportedColorSpaces(std::vector<GraphicCM_ColorSpaceType>& colorSpaces)
1753 {
1754 #ifdef WM_SCREEN_COLOR_SPACE_ENABLE
1755     auto ret = RSInterfaces::GetInstance().GetScreenSupportedColorSpaces(rsId_, colorSpaces);
1756     if (ret != StatusCode::SUCCESS) {
1757         TLOGE(WmsLogTag::DMS, "SCB: fail! rsId %{public}" PRIu64 ", ret:%{public}d",
1758             rsId_, ret);
1759         return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
1760     }
1761     TLOGI(WmsLogTag::DMS, "SCB: ok! rsId %{public}" PRIu64 ", size %{public}u",
1762         rsId_, static_cast<uint32_t>(colorSpaces.size()));
1763 #endif
1764     return DMError::DM_OK;
1765 }
1766 
GetScreenColorSpace(GraphicCM_ColorSpaceType & colorSpace)1767 DMError ScreenSession::GetScreenColorSpace(GraphicCM_ColorSpaceType& colorSpace)
1768 {
1769 #ifdef WM_SCREEN_COLOR_SPACE_ENABLE
1770     auto ret = RSInterfaces::GetInstance().GetScreenColorSpace(rsId_, colorSpace);
1771     if (ret != StatusCode::SUCCESS) {
1772         TLOGE(WmsLogTag::DMS, "GetScreenColorSpace fail! rsId %{public}" PRIu64, rsId_);
1773         return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
1774     }
1775     TLOGI(WmsLogTag::DMS, "GetScreenColorSpace ok! rsId %{public}" PRIu64 ", colorSpace %{public}u",
1776         rsId_, static_cast<uint32_t>(colorSpace));
1777 #endif
1778     return DMError::DM_OK;
1779 }
1780 
SetScreenColorSpace(GraphicCM_ColorSpaceType colorSpace)1781 DMError ScreenSession::SetScreenColorSpace(GraphicCM_ColorSpaceType colorSpace)
1782 {
1783 #ifdef WM_SCREEN_COLOR_SPACE_ENABLE
1784     std::vector<GraphicCM_ColorSpaceType> colorSpaces;
1785     DMError res = GetSupportedColorSpaces(colorSpaces);
1786     if (res != DMError::DM_OK) {
1787         TLOGE(WmsLogTag::DMS, "SetScreenColorSpace fail! rsId %{public}" PRIu64, rsId_);
1788         return res;
1789     }
1790     if (colorSpace < 0) {
1791         TLOGE(WmsLogTag::DMS, "SetScreenColorSpace fail! rsId %{public}" PRIu64 " colorSpace %{public}d invalid.",
1792             rsId_, static_cast<int32_t>(colorSpace));
1793         return DMError::DM_ERROR_INVALID_PARAM;
1794     }
1795     auto ret = RSInterfaces::GetInstance().SetScreenColorSpace(rsId_, colorSpace);
1796     if (ret != StatusCode::SUCCESS) {
1797         TLOGE(WmsLogTag::DMS, "SetScreenColorSpace fail! rsId %{public}" PRIu64, rsId_);
1798         return DMError::DM_ERROR_RENDER_SERVICE_FAILED;
1799     }
1800     TLOGI(WmsLogTag::DMS, "SetScreenColorSpace ok! rsId %{public}" PRIu64 ", colorSpace %{public}u",
1801         rsId_, static_cast<uint32_t>(colorSpace));
1802 #endif
1803     return DMError::DM_OK;
1804 }
1805 
HasPrivateSessionForeground() const1806 bool ScreenSession::HasPrivateSessionForeground() const
1807 {
1808     return hasPrivateWindowForeground_;
1809 }
1810 
SetPrivateSessionForeground(bool hasPrivate)1811 void ScreenSession::SetPrivateSessionForeground(bool hasPrivate)
1812 {
1813     hasPrivateWindowForeground_ = hasPrivate;
1814 }
1815 
InitRSDisplayNode(RSDisplayNodeConfig & config,Point & startPoint,bool isExtend,float positionX,float positionY)1816 void ScreenSession::InitRSDisplayNode(RSDisplayNodeConfig& config, Point& startPoint, bool isExtend,
1817     float positionX, float positionY)
1818 {
1819     std::unique_lock<std::shared_mutex> displayNodeLock(displayNodeMutex_);
1820     if (displayNode_ != nullptr) {
1821         displayNode_->SetDisplayNodeMirrorConfig(config);
1822         if (screenId_ == 0 && isFold_) {
1823             TLOGI(WmsLogTag::DMS, "Return InitRSDisplayNode foldScreen0");
1824             return;
1825         }
1826     } else {
1827         std::shared_ptr<RSDisplayNode> rsDisplayNode = RSDisplayNode::Create(config, GetRSUIContext());
1828         if (rsDisplayNode == nullptr) {
1829             TLOGE(WmsLogTag::DMS, "fail to add child. create rsDisplayNode fail!");
1830             return;
1831         }
1832         RSAdapterUtil::SetSkipCheckInMultiInstance(rsDisplayNode, true);
1833         displayNode_ = rsDisplayNode;
1834         TLOGD(WmsLogTag::WMS_SCB,
1835               "Create RSDisplayNode: %{public}s", RSAdapterUtil::RSNodeToStr(displayNode_).c_str());
1836     }
1837     if (startPoint.posX_ < 0 || startPoint.posY_ < 0) {
1838         TLOGE(WmsLogTag::DMS, "startPoint invalid!");
1839         return;
1840     }
1841     TLOGI(WmsLogTag::DMS, "posX:%{public}d, posY:%{public}d", startPoint.posX_, startPoint.posY_);
1842     RSInterfaces::GetInstance().SetScreenOffset(config.screenId, startPoint.posX_, startPoint.posY_);
1843     uint32_t width = 0;
1844     uint32_t height = 0;
1845     if (isExtend) {
1846         width = property_.GetBounds().rect_.GetWidth();
1847         height = property_.GetBounds().rect_.GetHeight();
1848     } else {
1849         sptr<SupportedScreenModes> abstractScreenModes = GetActiveScreenMode();
1850         if (abstractScreenModes != nullptr) {
1851             height = abstractScreenModes->height_;
1852             width = abstractScreenModes->width_;
1853         }
1854     }
1855     RSScreenType screenType;
1856     DmsXcollie dmsXcollie("DMS:InitRSDisplayNode:GetScreenType", XCOLLIE_TIMEOUT_5S);
1857     auto ret = RSInterfaces::GetInstance().GetScreenType(rsId_, screenType);
1858     if (ret == StatusCode::SUCCESS && screenType == RSScreenType::VIRTUAL_TYPE_SCREEN) {
1859         displayNode_->SetSecurityDisplay(isSecurity_);
1860         TLOGI(WmsLogTag::DMS, "virtualScreen SetSecurityDisplay success, isSecurity:%{public}d", isSecurity_);
1861     }
1862     // If SetScreenOffset is not valid for SetFrame/SetBounds
1863     TLOGI(WmsLogTag::DMS, "InitRSDisplayNode screenId:%{public}" PRIu64" \
1864         width:%{public}u height:%{public}u positionX:%{public}f positionY:%{public}f",
1865         screenId_, width, height, positionX, positionY);
1866     displayNode_->SetFrame(positionX, positionY, static_cast<float>(width), static_cast<float>(height));
1867     displayNode_->SetBounds(positionX, positionY, static_cast<float>(width), static_cast<float>(height));
1868     if (config.isMirrored) {
1869         EnableMirrorScreenRegion();
1870     }
1871     RSTransactionAdapter::FlushImplicitTransaction(GetRSUIContext());
1872 }
1873 
ScreenSessionGroup(ScreenId screenId,ScreenId rsId,std::string name,ScreenCombination combination)1874 ScreenSessionGroup::ScreenSessionGroup(ScreenId screenId, ScreenId rsId,
1875     std::string name, ScreenCombination combination) : combination_(combination)
1876 {
1877     name_ = name;
1878     screenId_ = screenId;
1879     rsId_ = rsId;
1880     SetScreenType(ScreenType::UNDEFINED);
1881     isScreenGroup_ = true;
1882 }
1883 
~ScreenSessionGroup()1884 ScreenSessionGroup::~ScreenSessionGroup()
1885 {
1886     ReleaseDisplayNode();
1887     std::unique_lock<std::shared_mutex> lock(screenSessionMapMutex_);
1888     screenSessionMap_.clear();
1889 }
1890 
GetRSDisplayNodeConfig(sptr<ScreenSession> & screenSession,struct RSDisplayNodeConfig & config,sptr<ScreenSession> defaultScreenSession)1891 bool ScreenSessionGroup::GetRSDisplayNodeConfig(sptr<ScreenSession>& screenSession, struct RSDisplayNodeConfig& config,
1892                                                 sptr<ScreenSession> defaultScreenSession)
1893 {
1894     if (screenSession == nullptr) {
1895         TLOGE(WmsLogTag::DMS, "screenSession is nullptr.");
1896         return false;
1897     }
1898     config = { screenSession->rsId_ };
1899     switch (combination_) {
1900         case ScreenCombination::SCREEN_ALONE:
1901             [[fallthrough]];
1902         case ScreenCombination::SCREEN_EXPAND:
1903             break;
1904         case ScreenCombination::SCREEN_UNIQUE:
1905             break;
1906         case ScreenCombination::SCREEN_MIRROR: {
1907             if (GetChildCount() == 0 || mirrorScreenId_ == screenSession->screenId_) {
1908                 TLOGI(WmsLogTag::DMS, "SCREEN_MIRROR, config is not mirror");
1909                 break;
1910             }
1911             if (defaultScreenSession == nullptr) {
1912                 TLOGE(WmsLogTag::DMS, "defaultScreenSession is nullptr");
1913                 break;
1914             }
1915             std::shared_ptr<RSDisplayNode> displayNode = defaultScreenSession->GetDisplayNode();
1916             if (displayNode == nullptr) {
1917                 TLOGE(WmsLogTag::DMS, "displayNode is nullptr, cannot get DisplayNode");
1918                 break;
1919             }
1920             NodeId nodeId = displayNode->GetId();
1921             TLOGI(WmsLogTag::DMS, "mirrorScreenId_:%{public}" PRIu64", rsId_:%{public}" PRIu64", \
1922                 nodeId:%{public}" PRIu64"", mirrorScreenId_, screenSession->rsId_, nodeId);
1923             config = {screenSession->rsId_, true, nodeId, true};
1924             break;
1925         }
1926         default:
1927             TLOGE(WmsLogTag::DMS, "fail to add child. invalid group combination:%{public}u", combination_);
1928             return false;
1929     }
1930     return true;
1931 }
1932 
AddChild(sptr<ScreenSession> & smsScreen,Point & startPoint,sptr<ScreenSession> defaultScreenSession,bool isExtend)1933 bool ScreenSessionGroup::AddChild(sptr<ScreenSession>& smsScreen, Point& startPoint,
1934                                   sptr<ScreenSession> defaultScreenSession, bool isExtend)
1935 {
1936     if (smsScreen == nullptr) {
1937         TLOGE(WmsLogTag::DMS, "AddChild, smsScreen is nullptr.");
1938         return false;
1939     }
1940     ScreenId screenId = smsScreen->screenId_;
1941     {
1942         std::shared_lock<std::shared_mutex> lock(screenSessionMapMutex_);
1943         auto iter = screenSessionMap_.find(screenId);
1944         if (iter != screenSessionMap_.end()) {
1945             TLOGE(WmsLogTag::DMS, "AddChild, screenSessionMap_ has smsScreen:%{public}" PRIu64"", screenId);
1946             return false;
1947         }
1948     }
1949     struct RSDisplayNodeConfig config;
1950     if (!GetRSDisplayNodeConfig(smsScreen, config, defaultScreenSession)) {
1951         return false;
1952     }
1953     smsScreen->InitRSDisplayNode(config, startPoint, isExtend);
1954     smsScreen->lastGroupSmsId_ = smsScreen->groupSmsId_;
1955     smsScreen->groupSmsId_ = screenId_;
1956     {
1957         std::unique_lock<std::shared_mutex> lock(screenSessionMapMutex_);
1958         screenSessionMap_.insert(std::make_pair(screenId, std::make_pair(smsScreen, startPoint)));
1959     }
1960     return true;
1961 }
1962 
AddChildren(std::vector<sptr<ScreenSession>> & smsScreens,std::vector<Point> & startPoints)1963 bool ScreenSessionGroup::AddChildren(std::vector<sptr<ScreenSession>>& smsScreens, std::vector<Point>& startPoints)
1964 {
1965     size_t size = smsScreens.size();
1966     if (size != startPoints.size()) {
1967         TLOGE(WmsLogTag::DMS, "AddChildren, unequal size.");
1968         return false;
1969     }
1970     bool res = true;
1971     for (size_t i = 0; i < size; i++) {
1972         res = AddChild(smsScreens[i], startPoints[i], nullptr) && res;
1973     }
1974     return res;
1975 }
1976 
RemoveChild(sptr<ScreenSession> & smsScreen)1977 bool ScreenSessionGroup::RemoveChild(sptr<ScreenSession>& smsScreen)
1978 {
1979     if (smsScreen == nullptr) {
1980         TLOGE(WmsLogTag::DMS, "RemoveChild, smsScreen is nullptr.");
1981         return false;
1982     }
1983     ScreenId screenId = smsScreen->screenId_;
1984     smsScreen->lastGroupSmsId_ = smsScreen->groupSmsId_;
1985     smsScreen->groupSmsId_ = SCREEN_ID_INVALID;
1986     std::shared_ptr<RSDisplayNode> displayNode = smsScreen->GetDisplayNode();
1987     if (displayNode != nullptr) {
1988         RSInterfaces::GetInstance().SetScreenOffset(smsScreen->rsId_, 0, 0);
1989         displayNode->RemoveFromTree();
1990         smsScreen->ReleaseDisplayNode();
1991     }
1992     displayNode = nullptr;
1993     // attention: make sure reference count 0
1994     RSTransactionAdapter::FlushImplicitTransaction(smsScreen->GetRSUIContext());
1995     std::unique_lock<std::shared_mutex> lock(screenSessionMapMutex_);
1996     return screenSessionMap_.erase(screenId);
1997 }
1998 
HasChild(ScreenId childScreen) const1999 bool ScreenSessionGroup::HasChild(ScreenId childScreen) const
2000 {
2001     std::shared_lock<std::shared_mutex> lock(screenSessionMapMutex_);
2002     return screenSessionMap_.find(childScreen) != screenSessionMap_.end();
2003 }
2004 
GetChildren() const2005 std::vector<sptr<ScreenSession>> ScreenSessionGroup::GetChildren() const
2006 {
2007     std::shared_lock<std::shared_mutex> lock(screenSessionMapMutex_);
2008     std::vector<sptr<ScreenSession>> res;
2009     for (auto iter = screenSessionMap_.begin(); iter != screenSessionMap_.end(); iter++) {
2010         res.push_back(iter->second.first);
2011     }
2012     return res;
2013 }
2014 
GetChildrenPosition() const2015 std::vector<Point> ScreenSessionGroup::GetChildrenPosition() const
2016 {
2017     std::shared_lock<std::shared_mutex> lock(screenSessionMapMutex_);
2018     std::vector<Point> res;
2019     for (auto iter = screenSessionMap_.begin(); iter != screenSessionMap_.end(); iter++) {
2020         res.push_back(iter->second.second);
2021     }
2022     return res;
2023 }
2024 
GetChildPosition(ScreenId screenId) const2025 Point ScreenSessionGroup::GetChildPosition(ScreenId screenId) const
2026 {
2027     std::shared_lock<std::shared_mutex> lock(screenSessionMapMutex_);
2028     Point point{};
2029     auto iter = screenSessionMap_.find(screenId);
2030     if (iter != screenSessionMap_.end()) {
2031         point = iter->second.second;
2032     }
2033     return point;
2034 }
2035 
GetChildCount() const2036 size_t ScreenSessionGroup::GetChildCount() const
2037 {
2038     std::shared_lock<std::shared_mutex> lock(screenSessionMapMutex_);
2039     return screenSessionMap_.size();
2040 }
2041 
GetScreenCombination() const2042 ScreenCombination ScreenSessionGroup::GetScreenCombination() const
2043 {
2044     return combination_;
2045 }
2046 
ConvertToScreenGroupInfo() const2047 sptr<ScreenGroupInfo> ScreenSessionGroup::ConvertToScreenGroupInfo() const
2048 {
2049     sptr<ScreenGroupInfo> screenGroupInfo = new(std::nothrow) ScreenGroupInfo();
2050     if (screenGroupInfo == nullptr) {
2051         return nullptr;
2052     }
2053     FillScreenInfo(screenGroupInfo);
2054     screenGroupInfo->combination_ = combination_;
2055     {
2056         std::shared_lock<std::shared_mutex> lock(screenSessionMapMutex_);
2057         for (auto iter = screenSessionMap_.begin(); iter != screenSessionMap_.end(); iter++) {
2058             screenGroupInfo->children_.push_back(iter->first);
2059         }
2060     }
2061     auto positions = GetChildrenPosition();
2062     screenGroupInfo->position_.insert(screenGroupInfo->position_.end(), positions.begin(), positions.end());
2063     return screenGroupInfo;
2064 }
2065 
SetDisplayBoundary(const RectF & rect,const uint32_t & offsetY)2066 void ScreenSession::SetDisplayBoundary(const RectF& rect, const uint32_t& offsetY)
2067 {
2068     property_.SetOffsetY(static_cast<int32_t>(offsetY));
2069     property_.SetBounds(RRect(rect, 0.0f, 0.0f));
2070 }
2071 
SetExtendProperty(RRect bounds,bool isCurrentOffScreenRendering)2072 void ScreenSession::SetExtendProperty(RRect bounds, bool isCurrentOffScreenRendering)
2073 {
2074     property_.SetBounds(bounds);
2075     property_.SetCurrentOffScreenRendering(isCurrentOffScreenRendering);
2076 }
2077 
Resize(uint32_t width,uint32_t height,bool isFreshBoundsSync)2078 void ScreenSession::Resize(uint32_t width, uint32_t height, bool isFreshBoundsSync)
2079 {
2080     if (isFreshBoundsSync) {
2081         sptr<SupportedScreenModes> screenMode = GetActiveScreenMode();
2082         if (screenMode != nullptr) {
2083             screenMode->width_ = width;
2084             screenMode->height_ = height;
2085             UpdatePropertyByActiveMode();
2086         }
2087     }
2088     {
2089         std::shared_lock<std::shared_mutex> displayNodeLock(displayNodeMutex_);
2090         if (displayNode_ == nullptr) {
2091             TLOGE(WmsLogTag::DMS, "displayNode_ is null, resize failed");
2092             return;
2093         }
2094         displayNode_->SetFrame(0, 0, static_cast<float>(width), static_cast<float>(height));
2095         displayNode_->SetBounds(0, 0, static_cast<float>(width), static_cast<float>(height));
2096     }
2097     RSTransactionAdapter::FlushImplicitTransaction(GetRSUIContext());
2098 }
2099 
SetFrameGravity(Gravity gravity)2100 void ScreenSession::SetFrameGravity(Gravity gravity)
2101 {
2102     RSInterfaces::GetInstance().SetScreenFrameGravity(rsId_, static_cast<int32_t>(gravity));
2103 }
2104 
UpdateAvailableArea(DMRect area)2105 bool ScreenSession::UpdateAvailableArea(DMRect area)
2106 {
2107     std::unique_lock<std::shared_mutex> lock(availableAreaMutex_);
2108     if (property_.GetAvailableArea() == area && !GetIsAvailableAreaNeedNotify()) {
2109         return false;
2110     }
2111     SetIsAvailableAreaNeedNotify(false);
2112     property_.SetAvailableArea(area);
2113     return true;
2114 }
2115 
UpdateExpandAvailableArea(DMRect area)2116 bool ScreenSession::UpdateExpandAvailableArea(DMRect area)
2117 {
2118     if (property_.GetExpandAvailableArea() == area) {
2119         return false;
2120     }
2121     property_.SetExpandAvailableArea(area);
2122     return true;
2123 }
2124 
SetAvailableArea(DMRect area)2125 void ScreenSession::SetAvailableArea(DMRect area)
2126 {
2127     property_.SetAvailableArea(area);
2128 }
2129 
GetAvailableArea()2130 DMRect ScreenSession::GetAvailableArea()
2131 {
2132     return property_.GetAvailableArea();
2133 }
2134 
GetExpandAvailableArea()2135 DMRect ScreenSession::GetExpandAvailableArea()
2136 {
2137     return property_.GetExpandAvailableArea();
2138 }
2139 
SetFoldScreen(bool isFold)2140 void ScreenSession::SetFoldScreen(bool isFold)
2141 {
2142     TLOGI(WmsLogTag::DMS, "SetFoldScreen %{public}u", isFold);
2143     isFold_ = isFold;
2144 }
2145 
GetHdrFormats()2146 std::vector<uint32_t> ScreenSession::GetHdrFormats()
2147 {
2148     std::shared_lock<std::shared_mutex> lock(hdrFormatsMutex_);
2149     return hdrFormats_;
2150 }
2151 
SetHdrFormats(std::vector<uint32_t> && hdrFormats)2152 void ScreenSession::SetHdrFormats(std::vector<uint32_t>&& hdrFormats)
2153 {
2154     std::unique_lock<std::shared_mutex> lock(hdrFormatsMutex_);
2155     hdrFormats_ = std::move(hdrFormats);
2156 }
2157 
SetColorSpaces(std::vector<uint32_t> && colorSpaces)2158 void ScreenSession::SetColorSpaces(std::vector<uint32_t>&& colorSpaces)
2159 {
2160     std::unique_lock<std::shared_mutex> lock(colorSpacesMutex_);
2161     colorSpaces_ = std::move(colorSpaces);
2162 }
2163 
GetColorSpaces()2164 std::vector<uint32_t> ScreenSession::GetColorSpaces()
2165 {
2166     std::unique_lock<std::shared_mutex> lock(colorSpacesMutex_);
2167     return colorSpaces_;
2168 }
2169 
SetSupportedRefreshRate(std::vector<uint32_t> && supportedRefreshRate)2170 void ScreenSession::SetSupportedRefreshRate(std::vector<uint32_t>&& supportedRefreshRate)
2171 {
2172     std::unique_lock<std::shared_mutex> lock(supportedRefreshRateMutex_);
2173     supportedRefreshRate_ = std::move(supportedRefreshRate);
2174 }
2175 
GetSupportedRefreshRate() const2176 std::vector<uint32_t> ScreenSession::GetSupportedRefreshRate() const
2177 {
2178     std::unique_lock<std::shared_mutex> lock(supportedRefreshRateMutex_);
2179     return supportedRefreshRate_;
2180 }
2181 
SetForceCloseHdr(bool isForceCloseHdr)2182 void ScreenSession::SetForceCloseHdr(bool isForceCloseHdr)
2183 {
2184     std::unique_lock<std::shared_mutex> displayNodeLock(displayNodeMutex_);
2185     if (displayNode_ == nullptr) {
2186         TLOGE(WmsLogTag::DMS, "displayNode_ is null");
2187         return;
2188     }
2189     if (lastCloseHdrStatus_ == isForceCloseHdr) {
2190         TLOGE(WmsLogTag::DMS, "lastCloseHdrStatus_ and isForceCloseHdr are the same.");
2191         return;
2192     }
2193     lastCloseHdrStatus_ = isForceCloseHdr;
2194     int hdrDuration = DURATION_1000MS;
2195     if (isForceCloseHdr == false) {
2196         DmsXcollie dmsXcollie("DMS:SetForceCloseHdr:GetScreenHDRStatus", XCOLLIE_TIMEOUT_5S);
2197         HdrStatus hdrStatus = HdrStatus::NO_HDR;
2198         TLOGI(WmsLogTag::DMS, "Start get screen status.");
2199         auto ret = RSInterfaces::GetInstance().GetScreenHDRStatus(rsId_, hdrStatus);
2200         if (ret == StatusCode::SUCCESS && hdrStatus == HdrStatus::NO_HDR) {
2201             hdrDuration = DURATION_0MS;
2202         }
2203     }
2204     TLOGI(WmsLogTag::DMS, "ForceCloseHdr is %{public}d", isForceCloseHdr);
2205     auto rsUIContext = GetRSUIContext();
2206     RSAnimationTimingProtocol timingProtocol;
2207     // Duration of the animation
2208     timingProtocol.SetDuration(isForceCloseHdr ? DURATION_0MS : hdrDuration);
2209     // Increase animation when HDR luminance changes abruptly
2210     RSNode::OpenImplicitAnimation(rsUIContext, timingProtocol, Rosen::RSAnimationTimingCurve::LINEAR, nullptr);
2211     displayNode_->SetHDRBrightnessFactor(isForceCloseHdr ? BRIGHTNESS_FACTOR_0 : BRIGHTNESS_FACTOR_1);
2212     RSNode::CloseImplicitAnimation(rsUIContext);
2213     RSTransactionAdapter::FlushImplicitTransaction(rsUIContext);
2214 }
2215 
IsWidthHeightMatch(float width,float height,float targetWidth,float targetHeight)2216 bool ScreenSession::IsWidthHeightMatch(float width, float height, float targetWidth, float targetHeight)
2217 {
2218     return (width == targetWidth && height == targetHeight) || (width == targetHeight && height == targetWidth);
2219 }
2220 
SetScreenSnapshotRect(RSSurfaceCaptureConfig & config)2221 void ScreenSession::SetScreenSnapshotRect(RSSurfaceCaptureConfig& config)
2222 {
2223     bool isChanged = false;
2224     auto width = property_.GetBounds().rect_.width_;
2225     auto height = property_.GetBounds().rect_.height_;
2226     Drawing::Rect snapshotRect = {0, 0, 0, 0};
2227     if (IsWidthHeightMatch(width, height, MAIN_STATUS_WIDTH, SCREEN_HEIGHT)) {
2228         snapshotRect = {0, 0, SCREEN_HEIGHT, MAIN_STATUS_WIDTH};
2229         config.mainScreenRect = snapshotRect;
2230         isChanged = true;
2231     } else if (IsWidthHeightMatch(width, height, FULL_STATUS_WIDTH, SCREEN_HEIGHT)) {
2232         snapshotRect = {0, FULL_STATUS_OFFSET_X, SCREEN_HEIGHT, GLOBAL_FULL_STATUS_WIDTH};
2233         config.mainScreenRect = snapshotRect;
2234         isChanged = true;
2235     } else if (IsWidthHeightMatch(width, height, GLOBAL_FULL_STATUS_WIDTH, SCREEN_HEIGHT)) {
2236         snapshotRect = {0, 0, SCREEN_HEIGHT, GLOBAL_FULL_STATUS_WIDTH};
2237         config.mainScreenRect = snapshotRect;
2238         isChanged = true;
2239     }
2240     if (isChanged) {
2241         TLOGI(WmsLogTag::DMS,
2242             "left: %{public}f, top: %{public}f, right: %{public}f, bottom: %{public}f",
2243             snapshotRect.left_, snapshotRect.top_, snapshotRect.right_, snapshotRect.bottom_);
2244     } else {
2245         TLOGI(WmsLogTag::DMS, "no need to set screen snapshot rect, use default rect");
2246     }
2247 }
2248 
GetScreenSnapshot(float scaleX,float scaleY)2249 std::shared_ptr<Media::PixelMap> ScreenSession::GetScreenSnapshot(float scaleX, float scaleY)
2250 {
2251     {
2252         std::shared_lock<std::shared_mutex> displayNodeLock(displayNodeMutex_);
2253         if (displayNode_ == nullptr) {
2254             TLOGE(WmsLogTag::DMS, "get screen snapshot displayNode_ is null");
2255             return nullptr;
2256         }
2257     }
2258 
2259     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "ss:GetScreenSnapshot");
2260     auto callback = std::make_shared<SurfaceCaptureFuture>();
2261     RSSurfaceCaptureConfig config = {
2262         .scaleX = scaleX,
2263         .scaleY = scaleY,
2264         .useDma = true,
2265     };
2266     SetScreenSnapshotRect(config);
2267     {
2268         DmsXcollie dmsXcollie("DMS:GetScreenSnapshot:TakeSurfaceCapture", XCOLLIE_TIMEOUT_5S);
2269         std::shared_lock<std::shared_mutex> displayNodeLock(displayNodeMutex_);
2270         bool ret = RSInterfaces::GetInstance().TakeSurfaceCapture(displayNode_, callback, config);
2271         if (!ret) {
2272             TLOGE(WmsLogTag::DMS, "get screen snapshot TakeSurfaceCapture failed");
2273             return nullptr;
2274         }
2275     }
2276 
2277     auto pixelMap = callback->GetResult(2000); // 2000, default timeout
2278     if (pixelMap != nullptr) {
2279         TLOGD(WmsLogTag::DMS, "save pixelMap WxH = %{public}dx%{public}d", pixelMap->GetWidth(), pixelMap->GetHeight());
2280     } else {
2281         TLOGE(WmsLogTag::DMS, "failed to get pixelMap, return nullptr");
2282     }
2283     return pixelMap;
2284 }
2285 
SetStartPosition(uint32_t startX,uint32_t startY)2286 void ScreenSession::SetStartPosition(uint32_t startX, uint32_t startY)
2287 {
2288     property_.SetStartPosition(startX, startY);
2289 }
2290 
SetXYPosition(int32_t x,int32_t y)2291 void ScreenSession::SetXYPosition(int32_t x, int32_t y)
2292 {
2293     property_.SetXYPosition(x, y);
2294 }
2295 
ScreenCaptureNotify(ScreenId mainScreenId,int32_t uid,const std::string & clientName)2296 void ScreenSession::ScreenCaptureNotify(ScreenId mainScreenId, int32_t uid, const std::string& clientName)
2297 {
2298     std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
2299     if (screenChangeListenerList_.empty()) {
2300         TLOGE(WmsLogTag::DMS, "screenChangeListenerList is empty.");
2301         return;
2302     }
2303     for (auto& listener : screenChangeListenerList_) {
2304         if (!listener) {
2305             TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
2306             continue;
2307         }
2308         listener->OnScreenCaptureNotify(mainScreenId, uid, clientName);
2309     }
2310 }
2311 
SuperFoldStatusChange(ScreenId screenId,SuperFoldStatus superFoldStatus)2312 void ScreenSession::SuperFoldStatusChange(ScreenId screenId, SuperFoldStatus superFoldStatus)
2313 {
2314     std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
2315     if (screenChangeListenerList_.empty()) {
2316         TLOGE(WmsLogTag::DMS, "screenChangeListenerList is empty.");
2317         return;
2318     }
2319     for (auto& listener : screenChangeListenerList_) {
2320         if (!listener) {
2321             TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
2322             continue;
2323         }
2324         listener->OnSuperFoldStatusChange(screenId, superFoldStatus);
2325     }
2326 }
2327 
ExtendScreenConnectStatusChange(ScreenId screenId,ExtendScreenConnectStatus extendScreenConnectStatus)2328 void ScreenSession::ExtendScreenConnectStatusChange(ScreenId screenId,
2329     ExtendScreenConnectStatus extendScreenConnectStatus)
2330 {
2331     std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
2332     if (screenChangeListenerList_.empty()) {
2333         TLOGE(WmsLogTag::DMS, "screenChangeListenerList is empty.");
2334         return;
2335     }
2336     for (auto& listener : screenChangeListenerList_) {
2337         if (!listener) {
2338             TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
2339             continue;
2340         }
2341         listener->OnExtendScreenConnectStatusChange(screenId, extendScreenConnectStatus);
2342     }
2343 }
2344 
SecondaryReflexionChange(ScreenId screenId,bool isSecondaryReflexion)2345 void ScreenSession::SecondaryReflexionChange(ScreenId screenId, bool isSecondaryReflexion)
2346 {
2347     std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
2348     if (screenChangeListenerList_.empty()) {
2349         TLOGE(WmsLogTag::DMS, "screenChangeListenerList is empty.");
2350         return;
2351     }
2352     for (auto& listener : screenChangeListenerList_) {
2353         if (!listener) {
2354             TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
2355             continue;
2356         }
2357         listener->OnSecondaryReflexionChange(screenId, isSecondaryReflexion);
2358     }
2359 }
2360 
BeforeScreenPropertyChange(FoldStatus foldStatus)2361 void ScreenSession::BeforeScreenPropertyChange(FoldStatus foldStatus)
2362 {
2363     std::vector<IScreenChangeListener*> curScreenChangeListenerList;
2364     {
2365         std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
2366         curScreenChangeListenerList = screenChangeListenerList_;
2367     }
2368     if (curScreenChangeListenerList.empty()) {
2369         TLOGE(WmsLogTag::DMS, "screenChangeListenerList is empty.");
2370         return;
2371     }
2372     for (auto& listener : curScreenChangeListenerList) {
2373         if (!listener) {
2374             TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
2375             continue;
2376         }
2377         listener->OnBeforeScreenPropertyChange(foldStatus);
2378     }
2379 }
2380 
ScreenModeChange(ScreenModeChangeEvent screenModeChangeEvent)2381 void ScreenSession::ScreenModeChange(ScreenModeChangeEvent screenModeChangeEvent)
2382 {
2383     std::lock_guard<std::mutex> lock(screenChangeListenerListMutex_);
2384     if (screenChangeListenerList_.empty()) {
2385         TLOGE(WmsLogTag::DMS, "screenChangeListenerList is empty.");
2386         return;
2387     }
2388     for (auto& listener : screenChangeListenerList_) {
2389         if (!listener) {
2390             TLOGE(WmsLogTag::DMS, "screenChangeListener is null.");
2391             continue;
2392         }
2393         listener->OnScreenModeChange(screenModeChangeEvent);
2394     }
2395 }
2396 
FreezeScreen(bool isFreeze)2397 void ScreenSession::FreezeScreen(bool isFreeze)
2398 {
2399     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "ss:FreezeScreen");
2400     std::shared_lock<std::shared_mutex> displayNodeLock(displayNodeMutex_);
2401     if (displayNode_ == nullptr) {
2402         TLOGE(WmsLogTag::DMS, "displayNode is null");
2403         return;
2404     }
2405     RSInterfaces::GetInstance().FreezeScreen(displayNode_, isFreeze);
2406 }
2407 
GetScreenSnapshotWithAllWindows(float scaleX,float scaleY,bool isNeedCheckDrmAndSurfaceLock)2408 std::shared_ptr<Media::PixelMap> ScreenSession::GetScreenSnapshotWithAllWindows(float scaleX, float scaleY,
2409     bool isNeedCheckDrmAndSurfaceLock)
2410 {
2411     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "ss:GetScreenSnapshotWithAllWindows");
2412     auto callback = std::make_shared<SurfaceCaptureFuture>();
2413     {
2414         DmsXcollie dmsXcollie("DMS:GetScreenSnapshotWithAllWindows:TaskSurfaceCaptureWithAllWindows",
2415             XCOLLIE_TIMEOUT_5S);
2416         std::shared_lock<std::shared_mutex> displayNodeLock(displayNodeMutex_);
2417         if (displayNode_ == nullptr) {
2418             TLOGE(WmsLogTag::DMS, "displayNode is null");
2419             return nullptr;
2420         }
2421         RSSurfaceCaptureConfig config = {
2422             .scaleX = scaleX,
2423             .scaleY = scaleY,
2424         };
2425         SetScreenSnapshotRect(config);
2426         bool ret = RSInterfaces::GetInstance().TaskSurfaceCaptureWithAllWindows(displayNode_, callback, config,
2427             isNeedCheckDrmAndSurfaceLock);
2428         if (!ret) {
2429             TLOGE(WmsLogTag::DMS, "take surface capture with all windows failed");
2430             return nullptr;
2431         }
2432     }
2433     auto pixelMap = callback->GetResult(SNAPSHOT_TIMEOUT_MS);
2434     if (pixelMap != nullptr) {
2435         TLOGD(WmsLogTag::DMS, "get pixelMap WxH = %{public}dx%{public}d, NeedCheckDrmAndSurfaceLock is %{public}d",
2436             pixelMap->GetWidth(), pixelMap->GetHeight(), isNeedCheckDrmAndSurfaceLock);
2437     } else {
2438         TLOGW(WmsLogTag::DMS, "null pixelMap, may have drm or surface lock, NeedCheckDrmAndSurfaceLock is %{public}d",
2439             isNeedCheckDrmAndSurfaceLock);
2440     }
2441     return pixelMap;
2442 }
2443 
SetIsPhysicalMirrorSwitch(bool isPhysicalMirrorSwitch)2444 void ScreenSession::SetIsPhysicalMirrorSwitch(bool isPhysicalMirrorSwitch)
2445 {
2446     isPhysicalMirrorSwitch_ = isPhysicalMirrorSwitch;
2447 }
2448 
GetIsPhysicalMirrorSwitch()2449 bool ScreenSession::GetIsPhysicalMirrorSwitch()
2450 {
2451     return isPhysicalMirrorSwitch_;
2452 }
2453 
GetApiVersion()2454 int32_t ScreenSession::GetApiVersion()
2455 {
2456     static std::chrono::steady_clock::time_point lastRequestTime = std::chrono::steady_clock::now();
2457     auto currentTime = std::chrono::steady_clock::now();
2458     auto interval = std::chrono::duration_cast<std::chrono::microseconds>(currentTime - lastRequestTime).count();
2459     int32_t apiVersion = NO_EXIST_UID_VERSION;
2460     int32_t currentPid = IPCSkeleton::GetCallingPid();
2461     if (interval < MAX_INTERVAL_US) {
2462         apiVersion = g_uidVersionMap.Get(currentPid);
2463     }
2464     if (apiVersion == NO_EXIST_UID_VERSION) {
2465         apiVersion = static_cast<int32_t>(SysCapUtil::GetApiCompatibleVersion());
2466         TLOGI(WmsLogTag::DMS, "IPC get");
2467         g_uidVersionMap.Set(currentPid, apiVersion);
2468     }
2469     TLOGI(WmsLogTag::DMS, "pid:%{public}d, apiVersion:%{public}d", apiVersion, apiVersion);
2470     lastRequestTime = currentTime;
2471     return apiVersion;
2472 }
2473 
GetSensorRotation() const2474 float ScreenSession::GetSensorRotation() const
2475 {
2476     return currentSensorRotation_;
2477 }
2478 
SetIsEnableRegionRotation(bool isEnableRegionRotation)2479 void ScreenSession::SetIsEnableRegionRotation(bool isEnableRegionRotation)
2480 {
2481     std::lock_guard<std::mutex> lock(isEnableRegionRotationMutex_);
2482     isEnableRegionRotation_ = isEnableRegionRotation;
2483 }
2484 
GetIsEnableRegionRotation()2485 bool ScreenSession::GetIsEnableRegionRotation()
2486 {
2487     std::lock_guard<std::mutex> lock(isEnableRegionRotationMutex_);
2488     return isEnableRegionRotation_;
2489 }
2490 
GetDisplayId()2491 DisplayId ScreenSession::GetDisplayId()
2492 {
2493     return screenId_;
2494 }
2495 
SetScreenId(ScreenId screenId)2496 void ScreenSession::SetScreenId(ScreenId screenId)
2497 {
2498     screenId_ = screenId;
2499 }
2500 
SetDisplayNode(std::shared_ptr<RSDisplayNode> displayNode)2501 void ScreenSession::SetDisplayNode(std::shared_ptr<RSDisplayNode> displayNode)
2502 {
2503     std::unique_lock<std::shared_mutex> displayNodeLock(displayNodeMutex_);
2504     displayNode_ = displayNode;
2505     RSAdapterUtil::SetRSUIContext(displayNode_, GetRSUIContext(), true);
2506 }
2507 
SetScreenAvailableStatus(bool isScreenAvailable)2508 void ScreenSession::SetScreenAvailableStatus(bool isScreenAvailable)
2509 {
2510     isScreenAvailable_ = isScreenAvailable;
2511 }
2512 
IsScreenAvailable() const2513 bool ScreenSession::IsScreenAvailable() const
2514 {
2515     return isScreenAvailable_;
2516 }
2517 
SetRSScreenId(ScreenId rsId)2518 void ScreenSession::SetRSScreenId(ScreenId rsId)
2519 {
2520     rsId_ = rsId;
2521 }
2522 
SetScreenProperty(ScreenProperty property)2523 void ScreenSession::SetScreenProperty(ScreenProperty property)
2524 {
2525     std::lock_guard<std::mutex> lock(propertyMutex_);
2526     property_ = property;
2527 }
2528 
GetScreenModes()2529 std::vector<sptr<SupportedScreenModes>> ScreenSession::GetScreenModes()
2530 {
2531     std::shared_lock<std::shared_mutex> lock(modesMutex_);
2532     return modes_;
2533 }
2534 
SetScreenModes(const std::vector<sptr<SupportedScreenModes>> & modes)2535 void ScreenSession::SetScreenModes(const std::vector<sptr<SupportedScreenModes>>& modes)
2536 {
2537     std::unique_lock<std::shared_mutex> lock(modesMutex_);
2538     modes_ = modes;
2539 }
2540 
GetActiveId()2541 int32_t ScreenSession::GetActiveId()
2542 {
2543     return activeIdx_;
2544 }
2545 
SetActiveId(int32_t activeIdx)2546 void ScreenSession::SetActiveId(int32_t activeIdx)
2547 {
2548     activeIdx_ = activeIdx;
2549 }
2550 
SetScreenOffScreenRendering()2551 void ScreenSession::SetScreenOffScreenRendering()
2552 {
2553     TLOGW(WmsLogTag::DMS, "screen off rendering come in.");
2554     if (GetIsInternal()) {
2555         TLOGW(WmsLogTag::DMS, "screen is internal");
2556         SetScreenOffScreenRenderingInner();
2557         return;
2558     }
2559     if (!GetScreenProperty().GetCurrentOffScreenRendering()) {
2560         TLOGI(WmsLogTag::DMS, "rsId: %{public}" PRIu64" not support offScreen rendering", rsId_);
2561         return;
2562     }
2563     uint32_t offWidth = GetScreenProperty().GetBounds().rect_.GetWidth();
2564     uint32_t offHeight = GetScreenProperty().GetBounds().rect_.GetHeight();
2565     if (GetScreenCombination() == ScreenCombination::SCREEN_MIRROR) {
2566         TLOGD(WmsLogTag::DMS, "screen mirror change.");
2567         offWidth = GetScreenProperty().GetScreenRealWidth();
2568         offHeight = GetScreenProperty().GetScreenRealHeight();
2569     }
2570     int32_t res = RSInterfaces::GetInstance().SetPhysicalScreenResolution(rsId_, offWidth, offHeight);
2571     if (GetScreenCombination() == ScreenCombination::SCREEN_MIRROR) {
2572         SetFrameGravity(Rosen::Gravity::TOP_LEFT);
2573     } else {
2574         SetFrameGravity(Rosen::Gravity::RESIZE);
2575         PropertyChange(GetScreenProperty(), ScreenPropertyChangeReason::UNDEFINED);
2576     }
2577     std::string offScreenResult = (res == StatusCode::SUCCESS) ? "success" : "failed";
2578     TLOGW(WmsLogTag::DMS, "rsId=%{public}" PRIu64" offScreen width=%{public}u height=%{public}u %{public}s",
2579         rsId_, offWidth, offHeight, offScreenResult.c_str());
2580 }
2581 
SetScreenOffScreenRenderingInner()2582 void ScreenSession::SetScreenOffScreenRenderingInner()
2583 {
2584     TLOGW(WmsLogTag::DMS, "screen off rendering inner come in.");
2585     uint32_t offWidth = GetScreenProperty().GetBounds().rect_.GetWidth();
2586     uint32_t offHeight = GetScreenProperty().GetBounds().rect_.GetHeight();
2587     int32_t res = RSInterfaces::GetInstance().SetPhysicalScreenResolution(rsId_, offWidth, offHeight);
2588     std::string offScreenResult = (res == StatusCode::SUCCESS) ? "success" : "failed";
2589     TLOGW(WmsLogTag::DMS, "rsId=%{public}" PRIu64" offScreen width = %{public}u height=%{public}u %{public}s",
2590         rsId_, offWidth, offHeight, offScreenResult.c_str());
2591 }
2592 
SetIsAvailableAreaNeedNotify(bool isAvailableAreaNeedNotify)2593 void ScreenSession::SetIsAvailableAreaNeedNotify(bool isAvailableAreaNeedNotify)
2594 {
2595     isAvailableAreaNeedNotify_ = isAvailableAreaNeedNotify;
2596 }
2597 
GetIsAvailableAreaNeedNotify() const2598 bool ScreenSession::GetIsAvailableAreaNeedNotify() const
2599 {
2600     return isAvailableAreaNeedNotify_;
2601 }
2602 
UpdateMirrorWidth(uint32_t mirrorWidth)2603 void ScreenSession::UpdateMirrorWidth(uint32_t mirrorWidth)
2604 {
2605     property_.SetMirrorWidth(mirrorWidth);
2606 }
2607 
UpdateMirrorHeight(uint32_t mirrorHeight)2608 void ScreenSession::UpdateMirrorHeight(uint32_t mirrorHeight)
2609 {
2610     property_.SetMirrorHeight(mirrorHeight);
2611 }
2612 
GetRSUIDirector() const2613 std::shared_ptr<RSUIDirector> ScreenSession::GetRSUIDirector() const
2614 {
2615     RETURN_IF_RS_CLIENT_MULTI_INSTANCE_DISABLED(nullptr);
2616     TLOGD(WmsLogTag::WMS_SCB, "%{public}s, screenId: %{public}" PRIu64,
2617           RSAdapterUtil::RSUIDirectorToStr(rsUIDirector_).c_str(), screenId_);
2618     return rsUIDirector_;
2619 }
2620 
GetRSUIContext() const2621 std::shared_ptr<RSUIContext> ScreenSession::GetRSUIContext() const
2622 {
2623     RETURN_IF_RS_CLIENT_MULTI_INSTANCE_DISABLED(nullptr);
2624     auto rsUIContext = rsUIDirector_ ? rsUIDirector_->GetRSUIContext() : nullptr;
2625     TLOGD(WmsLogTag::WMS_SCB, "%{public}s, screenId: %{public}" PRIu64,
2626           RSAdapterUtil::RSUIContextToStr(rsUIContext).c_str(), screenId_);
2627     return rsUIContext;
2628 }
2629 
SetIsEnableCanvasRotation(bool isEnableCanvasRotation)2630 void ScreenSession::SetIsEnableCanvasRotation(bool isEnableCanvasRotation)
2631 {
2632     std::lock_guard<std::mutex> lock(isEnableCanvasRotationMutex_);
2633     isEnableCanvasRotation_ = isEnableCanvasRotation;
2634 }
2635 
GetIsEnableCanvasRotation()2636 bool ScreenSession::GetIsEnableCanvasRotation()
2637 {
2638     std::lock_guard<std::mutex> lock(isEnableCanvasRotationMutex_);
2639     return isEnableCanvasRotation_;
2640 }
2641 
SetDisplayGroupId(DisplayGroupId displayGroupId)2642 void ScreenSession::SetDisplayGroupId(DisplayGroupId displayGroupId)
2643 {
2644     property_.SetDisplayGroupId(displayGroupId);
2645 }
2646 
GetDisplayGroupId() const2647 DisplayGroupId ScreenSession::GetDisplayGroupId() const
2648 {
2649     return property_.GetDisplayGroupId();
2650 }
2651 
SetMainDisplayIdOfGroup(ScreenId screenId)2652 void ScreenSession::SetMainDisplayIdOfGroup(ScreenId screenId)
2653 {
2654     property_.SetMainDisplayIdOfGroup(screenId);
2655 }
2656 
GetMainDisplayIdOfGroup() const2657 ScreenId ScreenSession::GetMainDisplayIdOfGroup() const
2658 {
2659     return property_.GetMainDisplayIdOfGroup();
2660 }
2661 
SetScreenAreaOffsetX(uint32_t screenAreaOffsetX)2662 void ScreenSession::SetScreenAreaOffsetX(uint32_t screenAreaOffsetX)
2663 {
2664     property_.SetScreenAreaOffsetX(screenAreaOffsetX);
2665 }
2666 
GetScreenAreaOffsetX() const2667 uint32_t ScreenSession::GetScreenAreaOffsetX() const
2668 {
2669     return property_.GetScreenAreaOffsetX();
2670 }
2671 
SetScreenAreaOffsetY(uint32_t screenAreaOffsetY)2672 void ScreenSession::SetScreenAreaOffsetY(uint32_t screenAreaOffsetY)
2673 {
2674     property_.SetScreenAreaOffsetY(screenAreaOffsetY);
2675 }
2676 
GetScreenAreaOffsetY() const2677 uint32_t ScreenSession::GetScreenAreaOffsetY() const
2678 {
2679     return property_.GetScreenAreaOffsetY();
2680 }
2681 
SetScreenAreaWidth(uint32_t screenAreaWidth)2682 void ScreenSession::SetScreenAreaWidth(uint32_t screenAreaWidth)
2683 {
2684     property_.SetScreenAreaWidth(screenAreaWidth);
2685 }
2686 
GetScreenAreaWidth() const2687 uint32_t ScreenSession::GetScreenAreaWidth() const
2688 {
2689     return property_.GetScreenAreaWidth();
2690 }
2691 
SetScreenAreaHeight(uint32_t screenAreaHeight)2692 void ScreenSession::SetScreenAreaHeight(uint32_t screenAreaHeight)
2693 {
2694     property_.SetScreenAreaHeight(screenAreaHeight);
2695 }
2696 
GetScreenAreaHeight() const2697 uint32_t ScreenSession::GetScreenAreaHeight() const
2698 {
2699     return property_.GetScreenAreaHeight();
2700 }
2701 } // namespace OHOS::Rosen
2702