• 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 #ifndef OHOS_ROSEN_WINDOW_SCENE_SCREEN_SESSION_MANAGER_H
17 #define OHOS_ROSEN_WINDOW_SCENE_SCREEN_SESSION_MANAGER_H
18 
19 #include <shared_mutex>
20 #include <system_ability.h>
21 #include <mutex>
22 #include <condition_variable>
23 #include <pixel_map.h>
24 
25 #include "common/include/task_scheduler.h"
26 #include "dm_common.h"
27 #include "event_tracker.h"
28 #include "session/screen/include/screen_session.h"
29 #include "zidl/screen_session_manager_stub.h"
30 #include "client_agent_container.h"
31 #include "session_display_power_controller.h"
32 #include "wm_single_instance.h"
33 #include "screen_edid_parse.h"
34 
35 #include "agent_death_recipient.h"
36 #include "screen.h"
37 #include "screen_cutout_controller.h"
38 #include "fold_screen_controller/fold_screen_controller.h"
39 #include "fold_screen_controller/fold_screen_sensor_manager.h"
40 #include "fold_screen_controller/super_fold_state_manager.h"
41 
42 namespace OHOS::Rosen {
43 class RSInterfaces;
44 
45 static const std::map<ScreenPowerStatus, DisplayPowerEvent> SCREEN_STATUS_POWER_EVENT_MAP = {
46     {ScreenPowerStatus::POWER_STATUS_ON, DisplayPowerEvent::DISPLAY_ON},
47     {ScreenPowerStatus::POWER_STATUS_OFF, DisplayPowerEvent::DISPLAY_OFF},
48     {ScreenPowerStatus::POWER_STATUS_DOZE, DisplayPowerEvent::DISPLAY_DOZE},
49     {ScreenPowerStatus::POWER_STATUS_DOZE_SUSPEND, DisplayPowerEvent::DISPLAY_DOZE_SUSPEND}
50 };
51 
52 class ScreenSessionManager : public SystemAbility, public ScreenSessionManagerStub, public IScreenChangeListener {
53 DECLARE_SYSTEM_ABILITY(ScreenSessionManager)
54 WM_DECLARE_SINGLE_INSTANCE_BASE(ScreenSessionManager)
55 
56 public:
57     sptr<ScreenSession> GetScreenSession(ScreenId screenId) const;
58     sptr<ScreenSession> GetDefaultScreenSession();
59     std::vector<ScreenId> GetAllScreenIds();
60 
61     sptr<DisplayInfo> GetDefaultDisplayInfo() override;
62     DMError SetScreenActiveMode(ScreenId screenId, uint32_t modeId) override;
63     DMError SetVirtualPixelRatio(ScreenId screenId, float virtualPixelRatio) override;
64     DMError SetVirtualPixelRatioSystem(ScreenId screenId, float virtualPixelRatio) override;
65     DMError SetDefaultDensityDpi(ScreenId screenId, float densityDpi) override;
66     DMError SetResolution(ScreenId screenId, uint32_t width, uint32_t height, float virtualPixelRatio) override;
67     DMError GetDensityInCurResolution(ScreenId screenId, float& virtualPixelRatio) override;
68     void NotifyScreenChanged(sptr<ScreenInfo> screenInfo, ScreenChangeEvent event);
69     // inner interface of multimodal-input
70     void NotifyScreenModeChange(ScreenId disconnectedScreenId = INVALID_SCREEN_ID);
71     void NotifyAbnormalScreenConnectChange(ScreenId screenId);
72 
73     DMError GetScreenColorGamut(ScreenId screenId, ScreenColorGamut& colorGamut) override;
74     DMError SetScreenColorGamut(ScreenId screenId, int32_t colorGamutIdx) override;
75     DMError GetScreenGamutMap(ScreenId screenId, ScreenGamutMap& gamutMap) override;
76     DMError SetScreenGamutMap(ScreenId screenId, ScreenGamutMap gamutMap) override;
77     DMError SetScreenColorTransform(ScreenId screenId) override;
78 
79     DMError GetPixelFormat(ScreenId screenId, GraphicPixelFormat& pixelFormat) override;
80     DMError SetPixelFormat(ScreenId screenId, GraphicPixelFormat pixelFormat) override;
81     DMError GetSupportedHDRFormats(ScreenId screenId, std::vector<ScreenHDRFormat>& hdrFormats) override;
82     DMError GetScreenHDRFormat(ScreenId screenId, ScreenHDRFormat& hdrFormat) override;
83     DMError SetScreenHDRFormat(ScreenId screenId, int32_t modeIdx) override;
84     DMError GetSupportedColorSpaces(ScreenId screenId, std::vector<GraphicCM_ColorSpaceType>& colorSpaces) override;
85     DMError GetScreenColorSpace(ScreenId screenId, GraphicCM_ColorSpaceType& colorSpace) override;
86     DMError SetScreenColorSpace(ScreenId screenId, GraphicCM_ColorSpaceType colorSpace) override;
87 
88     void DumpAllScreensInfo(std::string& dumpInfo) override;
89     void DumpSpecialScreenInfo(ScreenId id, std::string& dumpInfo) override;
90 
91     virtual DMError RegisterDisplayManagerAgent(const sptr<IDisplayManagerAgent>& displayManagerAgent,
92         DisplayManagerAgentType type) override;
93     virtual DMError UnregisterDisplayManagerAgent(const sptr<IDisplayManagerAgent>& displayManagerAgent,
94         DisplayManagerAgentType type) override;
95 
96     ScreenId GetInternalScreenId() override;
97     bool SetScreenPowerById(ScreenId screenId, ScreenPowerState state, PowerStateChangeReason reason) override;
98     DisplayState GetDisplayState(DisplayId displayId) override;
99     bool SetScreenBrightness(uint64_t screenId, uint32_t level) override;
100     uint32_t GetScreenBrightness(uint64_t screenId) override;
101     bool SetSpecifiedScreenPower(ScreenId screenId, ScreenPowerState state, PowerStateChangeReason reason) override;
102     void ForceSkipScreenOffAnimation();
103     ScreenPowerState GetScreenPower() override;
104 
105     void RegisterDisplayChangeListener(sptr<IDisplayChangeListener> listener);
106     bool NotifyDisplayStateChanged(DisplayId id, DisplayState state);
107     void NotifyScreenshot(DisplayId displayId);
108     virtual ScreenId CreateVirtualScreen(VirtualScreenOption option,
109                                          const sptr<IRemoteObject>& displayManagerAgent) override;
110     virtual DMError SetVirtualScreenSurface(ScreenId screenId, sptr<IBufferProducer> surface) override;
111     DMError AddVirtualScreenBlockList(const std::vector<int32_t>& persistentIds) override;
112     DMError RemoveVirtualScreenBlockList(const std::vector<int32_t>& persistentIds) override;
113     virtual DMError SetScreenPrivacyMaskImage(ScreenId screenId,
114         const std::shared_ptr<Media::PixelMap>& privacyMaskImg) override;
115     virtual DMError SetVirtualMirrorScreenCanvasRotation(ScreenId screenId, bool autoRotate) override;
116     virtual DMError SetVirtualMirrorScreenScaleMode(ScreenId screenId, ScreenScaleMode scaleMode) override;
117     virtual DMError DestroyVirtualScreen(ScreenId screenId) override;
118     DMError ResizeVirtualScreen(ScreenId screenId, uint32_t width, uint32_t height) override;
119     virtual DMError MakeMirror(ScreenId mainScreenId, std::vector<ScreenId> mirrorScreenIds,
120         ScreenId& screenGroupId) override;
121     virtual DMError MakeMirrorForRecord(ScreenId mainScreenId, std::vector<ScreenId> mirrorScreenIds,
122         ScreenId& screenGroupId) override;
123     virtual DMError MakeMirror(ScreenId mainScreenId, std::vector<ScreenId> mirrorScreenIds,
124         DMRect mainScreenRegion, ScreenId& screenGroupId) override;
125     virtual DMError SetMultiScreenMode(ScreenId mainScreenId, ScreenId secondaryScreenId,
126         MultiScreenMode screenMode) override;
127     virtual DMError SetMultiScreenRelativePosition(MultiScreenPositionOptions mainScreenOptions,
128         MultiScreenPositionOptions secondScreenOption) override;
129     virtual DMError StopMirror(const std::vector<ScreenId>& mirrorScreenIds) override;
130     DMError DisableMirror(bool disableOrNot) override;
131     virtual DMError MakeExpand(std::vector<ScreenId> screenId, std::vector<Point> startPoint,
132                                ScreenId& screenGroupId) override;
133     virtual DMError StopExpand(const std::vector<ScreenId>& expandScreenIds) override;
134     DMError MakeUniqueScreen(const std::vector<ScreenId>& screenIds, std::vector<DisplayId>& displayIds) override;
135     virtual sptr<ScreenGroupInfo> GetScreenGroupInfoById(ScreenId screenId) override;
136     virtual void RemoveVirtualScreenFromGroup(std::vector<ScreenId> screens) override;
137     virtual std::shared_ptr<Media::PixelMap> GetDisplaySnapshot(DisplayId displayId,
138         DmErrorCode* errorCode, bool isUseDma) override;
139     virtual std::shared_ptr<Media::PixelMap> GetSnapshotByPicker(Media::Rect &rect, DmErrorCode* errorCode) override;
140     virtual sptr<DisplayInfo> GetDisplayInfoById(DisplayId displayId) override;
141     virtual sptr<DisplayInfo> GetVisibleAreaDisplayInfoById(DisplayId displayId) override;
142     sptr<DisplayInfo> GetDisplayInfoByScreen(ScreenId screenId) override;
143     std::vector<DisplayId> GetAllDisplayIds() override;
144     virtual sptr<ScreenInfo> GetScreenInfoById(ScreenId screenId) override;
145     virtual DMError GetAllScreenInfos(std::vector<sptr<ScreenInfo>>& screenInfos) override;
146     virtual DMError GetScreenSupportedColorGamuts(ScreenId screenId,
147         std::vector<ScreenColorGamut>& colorGamuts) override;
148     DMError IsScreenRotationLocked(bool& isLocked) override;
149     DMError SetScreenRotationLocked(bool isLocked) override;
150     DMError SetScreenRotationLockedFromJs(bool isLocked) override;
151     DMError SetOrientation(ScreenId screenId, Orientation orientation) override;
152     bool SetRotation(ScreenId screenId, Rotation rotationAfter, bool isFromWindow);
153     void SetSensorSubscriptionEnabled();
154     bool SetRotationFromWindow(Rotation targetRotation);
155     sptr<SupportedScreenModes> GetScreenModesByDisplayId(DisplayId displayId);
156     sptr<ScreenInfo> GetScreenInfoByDisplayId(DisplayId displayId);
157     void NotifyDisplayCreate(sptr<DisplayInfo> displayInfo);
158     void NotifyDisplayDestroy(DisplayId displayId);
159     void NotifyAndPublishEvent(sptr<DisplayInfo> displayInfo, ScreenId screenId, sptr<ScreenSession> screenSession);
160     void NotifyDisplayChanged(sptr<DisplayInfo> displayInfo, DisplayChangeEvent event);
161 
162     std::vector<ScreenId> GetAllScreenIds() const;
163     const std::shared_ptr<RSDisplayNode> GetRSDisplayNodeByScreenId(ScreenId smsScreenId) const;
164     std::shared_ptr<Media::PixelMap> GetScreenSnapshot(DisplayId displayId, bool isUseDma = false);
165 
166     sptr<ScreenSession> InitVirtualScreen(ScreenId smsScreenId, ScreenId rsId, VirtualScreenOption option);
167     sptr<ScreenSession> InitAndGetScreen(ScreenId rsScreenId);
168     bool InitAbstractScreenModesInfo(sptr<ScreenSession>& absScreen);
169     std::vector<ScreenId> GetAllValidScreenIds(const std::vector<ScreenId>& screenIds) const;
170 
171     sptr<ScreenSessionGroup> AddToGroupLocked(sptr<ScreenSession> newScreen, bool isUnique = false);
172     sptr<ScreenSessionGroup> AddAsFirstScreenLocked(sptr<ScreenSession> newScreen, bool isUnique = false);
173     sptr<ScreenSessionGroup> AddAsSuccedentScreenLocked(sptr<ScreenSession> newScreen);
174     sptr<ScreenSessionGroup> RemoveFromGroupLocked(sptr<ScreenSession> screen);
175     sptr<ScreenSessionGroup> GetAbstractScreenGroup(ScreenId smsScreenId);
176 
177     void SetMultiScreenFrameControl(void);
178     bool IsPhysicalScreenAndInUse(sptr<ScreenSession> screenSession) const;
179     bool HandleFoldScreenSessionCreate(ScreenId screenId);
180 
181     void ChangeScreenGroup(sptr<ScreenSessionGroup> group, const std::vector<ScreenId>& screens,
182              const std::vector<Point>& startPoints, bool filterScreen, ScreenCombination combination,
183              DMRect mainScreenRegion = DMRect::NONE());
184 
185     bool RemoveChildFromGroup(sptr<ScreenSession> screen, sptr<ScreenSessionGroup> screenGroup);
186 
187     void AddScreenToGroup(sptr<ScreenSessionGroup> group,
188         const std::vector<ScreenId>& addScreens, const std::vector<Point>& addChildPos,
189         std::map<ScreenId, bool>& removeChildResMap);
190     bool CheckScreenInScreenGroup(sptr<ScreenSession> screen) const;
191 
192     DMError SetMirror(ScreenId screenId, std::vector<ScreenId> screens, DMRect mainScreenRegion);
193     DMError StopScreens(const std::vector<ScreenId>& screenIds, ScreenCombination stopCombination);
194 
195     void NotifyScreenConnected(sptr<ScreenInfo> screenInfo);
196     void NotifyScreenDisconnected(ScreenId screenId);
197     void NotifyScreenGroupChanged(const sptr<ScreenInfo>& screenInfo, ScreenGroupChangeEvent event);
198     void NotifyScreenGroupChanged(const std::vector<sptr<ScreenInfo>>& screenInfo, ScreenGroupChangeEvent event);
199 
200     void NotifyPrivateSessionStateChanged(bool hasPrivate);
201     void NotifyPrivateWindowListChanged(DisplayId id, std::vector<std::string> privacyWindowList);
202     DMError HasPrivateWindow(DisplayId id, bool& hasPrivateWindow) override;
203     bool ConvertScreenIdToRsScreenId(ScreenId screenId, ScreenId& rsScreenId) override;
204     void UpdateDisplayHookInfo(int32_t uid, bool enable, const DMHookInfo& hookInfo) override;
205     void GetDisplayHookInfo(int32_t uid, DMHookInfo& hookInfo) override;
206 
207     void OnScreenConnect(const sptr<ScreenInfo> screenInfo);
208     void OnScreenDisconnect(ScreenId screenId);
209     void OnScreenGroupChange(const std::string& trigger,
210         const sptr<ScreenInfo>& screenInfo, ScreenGroupChangeEvent groupEvent);
211     void OnScreenGroupChange(const std::string& trigger,
212         const std::vector<sptr<ScreenInfo>>& screenInfos, ScreenGroupChangeEvent groupEvent);
213     void OnScreenshot(sptr<ScreenshotInfo> info);
214     bool IsMultiScreenCollaboration();
215     bool HasCastEngineOrPhyMirror(const std::vector<ScreenId>& screenIdsToExclude);
216     void HandlePhysicalMirrorConnect(sptr<ScreenSession> screenSession, bool phyMirrorEnable);
217     sptr<CutoutInfo> GetCutoutInfo(DisplayId displayId) override;
218     DMError HasImmersiveWindow(ScreenId screenId, bool& immersive) override;
219     void SetDisplayBoundary(const sptr<ScreenSession> screenSession);
220     void SetLowTemp(LowTempMode lowTemp);
221 
222     /**
223      * On/Off screen
224      */
225     bool WakeUpBegin(PowerStateChangeReason reason) override;
226     bool WakeUpEnd() override;
227     bool SuspendBegin(PowerStateChangeReason reason) override;
228     bool SuspendEnd() override;
229     void BlockScreenOnByCV(void);
230     void BlockScreenOffByCV(void);
231     bool BlockScreenWaitPictureFrameByCV(bool isStartDream);
232     bool BlockSetDisplayState(void);
233     bool IsScreenLockSuspend(void);
234     bool IsPreBrightAuthFail(void);
235     void ScreenOffCVNotify(void);
236     bool SetDisplayState(DisplayState state) override;
237     bool SetScreenPowerForAll(ScreenPowerState state, PowerStateChangeReason reason) override;
238     ScreenPowerState GetScreenPower(ScreenId screenId) override;
239     void NotifyDisplayEvent(DisplayEvent event) override;
240     bool NotifyDisplayPowerEvent(DisplayPowerEvent event, EventStatus status, PowerStateChangeReason reason);
241     bool TryToCancelScreenOff() override;
242 
243     void DisablePowerOffRenderControl(ScreenId screenId) override;
244     bool SetVirtualScreenStatus(ScreenId screenId, VirtualScreenStatus screenStatus) override;
245     sptr<ScreenSession> GetOrCreateFakeScreenSession(sptr<ScreenSession> screenSession);
246     void InitFakeScreenSession(sptr<ScreenSession> screenSession);
247 
248     // Fold Screen
249     void SetFoldDisplayMode(const FoldDisplayMode displayMode) override;
250     DMError SetFoldDisplayModeInner(const FoldDisplayMode displayMode, std::string reason = "");
251     DMError SetFoldDisplayModeFromJs(const FoldDisplayMode displayMode, std::string reason = "") override;
252     void SetDisplayNodeScreenId(ScreenId screenId, ScreenId displayNodeScreenId);
253 
254     void SetDisplayScale(ScreenId screenId, float scaleX, float scaleY,
255         float pivotX, float pivotY) override;
256 
257     void SetFoldStatusLocked(bool locked) override;
258     DMError SetFoldStatusLockedFromJs(bool locked) override;
259 
260     FoldDisplayMode GetFoldDisplayMode() override;
261 
262     bool IsFoldable() override;
263     bool IsCaptured() override;
264 
265     FoldStatus GetFoldStatus() override;
266     SuperFoldStatus GetSuperFoldStatus() override;
267     void SetLandscapeLockStatus(bool isLocked) override;
268     bool GetTentMode();
269     bool GetCameraMode();
270     ExtendScreenConnectStatus GetExtendScreenConnectStatus() override;
271     bool GetIsExtendScreenConnected();
272     void SetIsExtendScreenConnected(bool isExtendScreenConnected);
273     void HandleExtendScreenConnect(ScreenId screenId);
274     void HandleExtendScreenDisconnect(ScreenId screenId);
275 
276     bool SetScreenPower(ScreenPowerStatus status, PowerStateChangeReason reason);
277     void SetScreenPowerForFold(ScreenPowerStatus status);
278     void SetScreenPowerForFold(ScreenId screenId, ScreenPowerStatus status);
279     void TriggerDisplayModeUpdate(FoldDisplayMode targetDisplayMode);
280     void CallRsSetScreenPowerStatusSync(ScreenId screenId, ScreenPowerStatus status);
281     void CallRsSetScreenPowerStatusSyncForFold(ScreenPowerStatus status);
282     void TryToRecoverFoldDisplayMode(ScreenPowerStatus status);
283 
284     void SetKeyguardDrawnDoneFlag(bool flag);
285 
286     sptr<FoldCreaseRegion> GetCurrentFoldCreaseRegion() override;
287 
288     void TriggerFoldStatusChange(FoldStatus foldStatus);
289     void NotifyFoldStatusChanged(FoldStatus foldStatus);
290     void NotifyFoldAngleChanged(std::vector<float> foldAngles);
291     int NotifyFoldStatusChanged(const std::string& statusParam);
292     void NotifyDisplayModeChanged(FoldDisplayMode displayMode);
293     void NotifyDisplayChangeInfoChanged(const sptr<DisplayChangeInfo>& info) override;
294     void NotifyScreenMagneticStateChanged(bool isMagneticState);
295     void OnTentModeChanged(int tentType, int32_t hall = -1);
296     void RegisterSettingDpiObserver();
297     void RegisterSettingRotationObserver();
298     void RegisterSettingscreenSkipProtectedWindowObserver();
299 
OnConnect(ScreenId screenId)300     void OnConnect(ScreenId screenId) override {}
OnDisconnect(ScreenId screenId)301     void OnDisconnect(ScreenId screenId) override {}
302     void OnPropertyChange(const ScreenProperty& newProperty, ScreenPropertyChangeReason reason,
303         ScreenId screenId) override;
304     void OnPowerStatusChange(DisplayPowerEvent event, EventStatus status,
305         PowerStateChangeReason reason) override;
306     void OnSensorRotationChange(float sensorRotation, ScreenId screenId) override;
307     void OnHoverStatusChange(int32_t hoverStatus, bool needRotate, ScreenId screenId) override;
308     void OnScreenOrientationChange(float screenOrientation, ScreenId screenId) override;
309     void OnScreenRotationLockedChange(bool isLocked, ScreenId screenId) override;
310     void OnCameraBackSelfieChange(bool isCameraBackSelfie, ScreenId screenId) override;
311 
312     void SetHdrFormats(ScreenId screenId, sptr<ScreenSession>& session);
313     void SetColorSpaces(ScreenId screenId, sptr<ScreenSession>& session);
314     void SetClient(const sptr<IScreenSessionManagerClient>& client) override;
315     ScreenProperty GetScreenProperty(ScreenId screenId) override;
316     std::shared_ptr<RSDisplayNode> GetDisplayNode(ScreenId screenId) override;
317     void UpdateScreenRotationProperty(ScreenId screenId, const RRect& bounds, float rotation,
318         ScreenPropertyChangeType screenPropertyChangeType) override;
319     void UpdateScreenDirectionInfo(ScreenId screenId, float screenComponentRotation, float rotation,
320         float phyRotation, ScreenPropertyChangeType screenPropertyChangeType) override;
321     uint32_t GetCurvedCompressionArea() override;
322     ScreenProperty GetPhyScreenProperty(ScreenId screenId) override;
323     void SetScreenPrivacyState(bool hasPrivate) override;
324     void SetPrivacyStateByDisplayId(DisplayId id, bool hasPrivate) override;
325     void SetScreenPrivacyWindowList(DisplayId id, std::vector<std::string> privacyWindowList) override;
326     void UpdateAvailableArea(ScreenId screenId, DMRect area) override;
327     void UpdateSuperFoldAvailableArea(ScreenId screenId, DMRect bArea, DMRect cArea) override;
328     void UpdateSuperFoldExpandAvailableArea(ScreenId screenId, DMRect area) override;
329     int32_t SetScreenOffDelayTime(int32_t delay) override;
330     int32_t SetScreenOnDelayTime(int32_t delay) override;
331     DMError GetAvailableArea(DisplayId displayId, DMRect& area) override;
332     DMError GetExpandAvailableArea(DisplayId displayId, DMRect& area) override;
333     void NotifyAvailableAreaChanged(DMRect area, DisplayId displayId);
334     void NotifyFoldToExpandCompletion(bool foldToExpand) override;
335     void RecordEventFromScb(std::string description, bool needRecordEvent) override;
336     void SetCameraStatus(int32_t cameraStatus, int32_t cameraPosition) override;
337     bool GetSnapshotArea(Media::Rect &rect, DmErrorCode* errorCode, ScreenId &screenId);
338     int32_t GetCameraStatus();
339     int32_t GetCameraPosition();
IsCameraBackSelfie()340     bool IsCameraBackSelfie() { return isCameraBackSelfie_; };
341     void UpdateCameraBackSelfie(bool isCameraBackSelfie);
342     void SetScreenCorrection();
343 
344     VirtualScreenFlag GetVirtualScreenFlag(ScreenId screenId) override;
345     DMError SetVirtualScreenFlag(ScreenId screenId, VirtualScreenFlag screenFlag) override;
346 
347     DeviceScreenConfig GetDeviceScreenConfig() override;
348     DMError SetVirtualScreenRefreshRate(ScreenId screenId, uint32_t refreshInterval) override;
349     void SetVirtualScreenBlackList(ScreenId screenId, std::vector<uint64_t>& windowIdList,
350         std::vector<uint64_t> surfaceIdList = {}) override;
351     void SetVirtualDisplayMuteFlag(ScreenId screenId, bool muteFlag) override;
352     // notify scb virtual screen change
353     void OnVirtualScreenChange(ScreenId screenId, ScreenEvent screenEvent);
354     DMError VirtualScreenUniqueSwitch(const std::vector<ScreenId>& screenIds);
355     void FixPowerStatus();
356     void FoldScreenPowerInit();
357     DMError ProxyForFreeze(const std::set<int32_t>& pidList, bool isProxy) override;
358     DMError ResetAllFreezeStatus() override;
359 
360     void ReportFoldStatusToScb(std::vector<std::string>& screenFoldInfo);
361     std::vector<DisplayPhysicalResolution> GetAllDisplayPhysicalResolution() override;
362 
363     DMError GetDisplayCapability(std::string& capabilitInfo) override;
364     nlohmann::ordered_json GetCapabilityJson(FoldStatus foldStatus, FoldDisplayMode displayMode,
365         std::vector<std::string> rotation, std::vector<std::string> orientation);
366     DMError GetSecondaryDisplayCapability(std::string& capabilitInfo);
367     DMError GetFoldableDeviceCapability(std::string& capabilitInfo);
368     DMError GetSuperFoldCapability(std::string& capabilitInfo);
369 
370     void OnScreenExtendChange(ScreenId mainScreenId, ScreenId extendScreenId) override;
371     void OnSuperFoldStatusChange(ScreenId screenId, SuperFoldStatus superFoldStatus) override;
372     void OnSecondaryReflexionChange(ScreenId screenId, bool isSecondaryReflexion) override;
373     void OnExtendScreenConnectStatusChange(ScreenId screenId,
374         ExtendScreenConnectStatus extendScreenConnectStatus) override;
375     void SetDefaultScreenId(ScreenId defaultId);
376     sptr<IScreenSessionManagerClient> GetClientProxy();
377     void NotifyCastWhenScreenConnectChange(bool isConnected);
378     void NotifyCastWhenSwitchScbNode();
379     void MultiScreenModeChange(const std::string& mainScreenId, const std::string& secondaryScreenId,
380         const std::string& secondaryScreenMode);
381     void SwitchScrollParam(FoldDisplayMode displayMode);
382     void OnScreenChange(ScreenId screenId, ScreenEvent screenEvent,
383         ScreenChangeReason reason = ScreenChangeReason::DEFAULT);
384     void SetCoordinationFlag(bool isCoordinationFlag);
385     DMError SetVirtualScreenMaxRefreshRate(ScreenId id, uint32_t refreshRate,
386         uint32_t& actualRefreshRate) override;
387 
388     void SetLastScreenMode(sptr<ScreenSession> firstSession, sptr<ScreenSession> secondarySession);
389 
390     bool IsOrientationNeedChanged() override;
391     /*
392      * multi user
393      */
394     void SwitchUser() override;
395     void SwitchScbNodeHandle(int32_t userId, int32_t newScbPid, bool coldBoot);
396     void HotSwitch(int32_t newUserId, int32_t newScbPid);
397     void AddScbClientDeathRecipient(const sptr<IScreenSessionManagerClient>& scbClient, int32_t scbPid);
398     void ScbClientDeathCallback(int32_t deathScbPid);
399     void ScbStatusRecoveryWhenSwitchUser(std::vector<int32_t> oldScbPids, int32_t newScbPid);
400     void RecoverMultiScreenModeWhenSwitchUser();
401     int32_t GetCurrentUserId();
402 
403     std::shared_ptr<Media::PixelMap> GetScreenCapture(const CaptureOption& captureOption,
404         DmErrorCode* errorCode = nullptr) override;
405     void OnScreenCaptureNotify(ScreenId mainScreenId, int32_t uid, const std::string& clientName) override;
406     sptr<DisplayInfo> GetPrimaryDisplayInfo() override;
407     std::shared_ptr<Media::PixelMap> GetDisplaySnapshotWithOption(const CaptureOption& captureOption,
408         DmErrorCode* errorCode) override;
409     ScreenCombination GetScreenCombination(ScreenId screenId) override;
410     void MultiScreenChangeOuter(const std::string& outerFlag);
411     DMError SetScreenSkipProtectedWindow(const std::vector<ScreenId>& screenIds, bool isEnable) override;
412     void UpdateValidArea(ScreenId screenId, uint32_t validWidth, uint32_t validHeight);
413     bool GetIsRealScreen(ScreenId screenId) override;
414     void UnregisterSettingWireCastObserver(ScreenId screenId);
415     void RegisterSettingWireCastObserver(sptr<ScreenSession>& screenSession);
416     SessionOption GetSessionOption(sptr<ScreenSession> screenSession);
417     SessionOption GetSessionOption(sptr<ScreenSession> screenSession, ScreenId screenId);
418     virtual DMError SetSystemKeyboardStatus(bool isOn = false) override;
419 
420 protected:
421     ScreenSessionManager();
422     virtual ~ScreenSessionManager() = default;
423 
424 private:
425     void OnStart() override;
426     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
427     void Init();
428     void LoadScreenSceneXml();
429     void ConfigureScreenScene();
430     void ConfigureDpi();
431     void ConfigureCastParams();
432     void ConfigureWaterfallDisplayCompressionParams();
433     void ConfigureScreenSnapshotParams();
434     void RegisterScreenChangeListener();
435     void RegisterRefreshRateChangeListener();
436     void OnHgmRefreshRateChange(uint32_t refreshRate);
437     sptr<ScreenSession> GetOrCreateScreenSession(ScreenId screenId);
438     void CreateScreenProperty(ScreenId screenId, ScreenProperty& property);
439     void InitScreenProperty(ScreenId screenId, RSScreenModeInfo& screenMode,
440         RSScreenCapability& screenCapability, ScreenProperty& property);
441     void GetInternalWidth();
442     void InitExtendScreenDensity(sptr<ScreenSession> session, ScreenProperty property);
443     void InitExtendScreenProperty(ScreenId screenId, sptr<ScreenSession> session, ScreenProperty property);
444     sptr<ScreenSession> GetScreenSessionInner(ScreenId screenId, ScreenProperty property);
445     sptr<ScreenSession> CreatePhysicalMirrorSessionInner(ScreenId screenId, ScreenId defaultScreenId,
446         ScreenProperty property);
447     sptr<ScreenSession> GetPhysicalScreenSession(ScreenId screenId, ScreenId defScreenId, ScreenProperty property);
448     void FreeDisplayMirrorNodeInner(const sptr<ScreenSession> mirrorSession);
449     void MirrorSwitchNotify(ScreenId screenId);
450     ScreenId GetDefaultScreenId();
451     void AddVirtualScreenDeathRecipient(const sptr<IRemoteObject>& displayManagerAgent, ScreenId smsScreenId);
452     void SendCastEvent(const bool &isPlugIn);
453     void PhyMirrorConnectWakeupScreen();
454     bool GetIsCurrentInUseById(ScreenId screenId);
455     void GetAndMergeEdidInfo(sptr<ScreenSession> screenSession);
456     std::string ConvertEdidToString(const struct BaseEdid edidData);
457     bool RecoverRestoredMultiScreenMode(sptr<ScreenSession> screenSession);
458     void SetExtendedScreenFallbackPlan(ScreenId screenId);
459     int32_t GetCurrentInUseScreenNumber();
460     void ReportHandleScreenEvent(ScreenEvent screenEvent, ScreenCombination screenCombination);
461     void HandleScreenConnectEvent(sptr<ScreenSession> screenSession, ScreenId screenId, ScreenEvent screenEvent);
462     void HandleScreenDisconnectEvent(sptr<ScreenSession> screenSession, ScreenId screenId, ScreenEvent screenEvent);
463     void HandlePCScreenDisconnect(sptr<ScreenSession> screenSession);
464     ScreenRotation ConvertOffsetToCorrectRotation(int32_t phyOffset);
465     void MultiScreenModeChange(ScreenId mainScreenId, ScreenId secondaryScreenId, const std::string& operateType);
466     void SetClientInner();
467     void RecoverMultiScreenMode(sptr<ScreenSession> screenSession);
468     void GetCurrentScreenPhyBounds(float& phyWidth, float& phyHeight, bool& isReset, const ScreenId& screenid);
469     void SetPhysicalRotationClientInner(ScreenId screenId, int rotation);
470 
471     void NotifyDisplayStateChange(DisplayId defaultDisplayId, sptr<DisplayInfo> displayInfo,
472         const std::map<DisplayId, sptr<DisplayInfo>>& displayInfoMap, DisplayStateChangeType type);
473     void NotifyCaptureStatusChanged();
474     void NotifyCaptureStatusChanged(bool IsCaptured);
475     DMError DoMakeMirror(ScreenId mainScreenId, std::vector<ScreenId> mirrorScreenIds,
476         DMRect mainScreenRegion, ScreenId& screenGroupId);
477     bool OnMakeExpand(std::vector<ScreenId> screenId, std::vector<Point> startPoint);
478     bool OnRemoteDied(const sptr<IRemoteObject>& agent);
479     std::string TransferTypeToString(ScreenType type) const;
480     std::string TransferPropertyChangeTypeToString(ScreenPropertyChangeType type) const;
481     void CheckAndSendHiSysEvent(const std::string& eventName, const std::string& bundleName) const;
482     void HandlerSensor(ScreenPowerStatus status, PowerStateChangeReason reason);
483     void UnregisterInHandlerSensorWithPowerOff(PowerStateChangeReason reason);
484     bool GetPowerStatus(ScreenPowerState state, PowerStateChangeReason reason, ScreenPowerStatus& status);
485     DMError CheckDisplayMangerAgentTypeAndPermission(
486         const sptr<IDisplayManagerAgent>& displayManagerAgent, DisplayManagerAgentType type);
487     void SetMultiScreenDefaultRelativePosition();
488     void SetRelativePositionForDisconnect(MultiScreenPositionOptions defaultScreenOptions);
489     int Dump(int fd, const std::vector<std::u16string>& args) override;
490     sptr<DisplayInfo> HookDisplayInfoByUid(sptr<DisplayInfo> displayInfo, const sptr<ScreenSession>& screenSession);
491     DisplayId GetFakeDisplayId(sptr<ScreenSession> screenSession);
492     DMError SetVirtualScreenSecurityExemption(ScreenId screenId, uint32_t pid,
493         std::vector<uint64_t>& windowIdList) override;
494     void GetInternalAndExternalSession(sptr<ScreenSession>& internalSession, sptr<ScreenSession>& externalSession);
495     void AddPermissionUsedRecord(const std::string& permission, int32_t successCount, int32_t failCount);
496     std::shared_ptr<RSDisplayNode> GetDisplayNodeByDisplayId(DisplayId displayId);
497     void RefreshMirrorScreenRegion(ScreenId screenId);
498     void CalculateXYPosition(sptr<ScreenSession> screenSession);
499 #ifdef DEVICE_STATUS_ENABLE
500     void SetDragWindowScreenId(ScreenId screenId, ScreenId displayNodeScreenId);
501 #endif // DEVICE_STATUS_ENABLE
502     void ShowFoldStatusChangedInfo(int errCode, std::string& dumpInfo);
503     void SetMirrorScreenIds(std::vector<ScreenId>& mirrorScreenIds);
504     bool IsFreezed(const int32_t& agentPid, const DisplayManagerAgentType& agentType);
505     void NotifyUnfreezed(const std::set<int32_t>& unfreezedPidList, const sptr<ScreenSession>& screenSession);
506     void NotifyUnfreezedAgents(const int32_t& pid, const std::set<int32_t>& unfreezedPidList,
507         const std::set<DisplayManagerAgentType>& pidAgentTypes, const sptr<ScreenSession>& screenSession);
508     int NotifyPowerEventForDualDisplay(DisplayPowerEvent event, EventStatus status,
509         PowerStateChangeReason reason);
510     bool IsExtendMode();
511     bool IsScreenCasting();
512     void SetScreenSkipProtectedWindowInner();
513 
514     /**
515      * On/Off screen
516      */
517     void SetGotScreenOffAndWakeUpBlock();
518     void WakeUpPictureFrameBlock(DisplayEvent event);
519 
520     class ScreenIdManager {
521     friend class ScreenSessionGroup;
522     public:
523         ScreenIdManager() = default;
524         ~ScreenIdManager() = default;
525         WM_DISALLOW_COPY_AND_MOVE(ScreenIdManager);
526         ScreenId CreateAndGetNewScreenId(ScreenId rsScreenId);
527         void UpdateScreenId(ScreenId rsScreenId, ScreenId smsScreenId);
528         bool DeleteScreenId(ScreenId smsScreenId);
529         bool HasRsScreenId(ScreenId smsScreenId) const;
530         bool ConvertToRsScreenId(ScreenId, ScreenId&) const;
531         ScreenId ConvertToRsScreenId(ScreenId) const;
532         bool ConvertToSmsScreenId(ScreenId, ScreenId&) const;
533         ScreenId ConvertToSmsScreenId(ScreenId) const;
534 
535     private:
536         std::atomic<ScreenId> smsScreenCount_ { 1000 };
537         std::map<ScreenId, ScreenId> rs2SmsScreenIdMap_;
538         std::map<ScreenId, ScreenId> sms2RsScreenIdMap_;
539         mutable std::shared_mutex screenIdMapMutex_;
540     };
541 
542     EventTracker screenEventTracker_;
543     RSInterfaces& rsInterface_;
544     std::shared_ptr<TaskScheduler> taskScheduler_;
545     std::shared_ptr<TaskScheduler> screenPowerTaskScheduler_;
546 
547     /*
548      * multi user
549      */
550     std::mutex oldScbPidsMutex_;
551     std::condition_variable scbSwitchCV_;
552     int32_t currentUserId_ { 0 };
553     int32_t currentUserIdForSettings_ { 0 };
554     int32_t currentScbPId_ { -1 };
555     std::vector<int32_t> oldScbPids_ {};
556     std::map<int32_t, sptr<IScreenSessionManagerClient>> clientProxyMap_;
557     FoldDisplayMode oldScbDisplayMode_ = FoldDisplayMode::UNKNOWN;
558 
559     sptr<IScreenSessionManagerClient> clientProxy_;
560     ClientAgentContainer<IDisplayManagerAgent, DisplayManagerAgentType> dmAgentContainer_;
561     DeviceScreenConfig deviceScreenConfig_;
562     std::vector<DisplayPhysicalResolution> allDisplayPhysicalResolution_ {};
563     std::map<int32_t, std::set<DisplayManagerAgentType>> pidAgentTypeMap_;
564     std::vector<float> lastFoldAngles_ {};
565     sptr<DisplayChangeInfo> lastDisplayChangeInfo_;
566     ScreenChangeEvent lastScreenChangeEvent_ = ScreenChangeEvent::UNKNOWN;
567     std::mutex lastStatusUpdateMutex_;
568 
569     mutable std::recursive_mutex screenSessionMapMutex_;
570     std::map<ScreenId, sptr<ScreenSession>> screenSessionMap_;
571     std::recursive_mutex mutex_;
572     std::recursive_mutex displayInfoMutex_;
573     std::shared_mutex hookInfoMutex_;
574     std::mutex shareProtectMutex_;
575 
576     ScreenId defaultScreenId_ = SCREEN_ID_INVALID;
577     ScreenIdManager screenIdManager_;
578 
579     std::atomic<ScreenId> defaultRsScreenId_ { SCREEN_ID_INVALID };
580     std::map<sptr<IRemoteObject>, std::vector<ScreenId>> screenAgentMap_;
581     std::map<ScreenId, sptr<ScreenSessionGroup>> smsScreenGroupMap_;
582     std::map<uint32_t, DMHookInfo> displayHookMap_;
583 
584     bool isAutoRotationOpen_ = false;
585     bool isExpandCombination_ = false;
586     bool isScreenShot_ = false;
587     bool isCoordinationFlag_ = false;
588     bool isFoldScreenOuterScreenReady_ = false;
589     bool isCameraBackSelfie_ = false;
590     uint32_t hdmiScreenCount_ = 0;
591     uint32_t virtualScreenCount_ = 0;
592     uint32_t currentExpandScreenCount_ = 0;
593     int32_t connectScreenNumber_ = 0;
594     sptr<AgentDeathRecipient> deathRecipient_ { nullptr };
595 
596     sptr<SessionDisplayPowerController> sessionDisplayPowerController_;
597     sptr<ScreenCutoutController> screenCutoutController_;
598     sptr<FoldScreenController> foldScreenController_;
599 
600     bool isDensityDpiLoad_ = false;
601     float densityDpi_ { 1.0f };
602     float subDensityDpi_ { 1.0f };
603     std::atomic<uint32_t> cachedSettingDpi_ {0};
604 
605     uint32_t defaultDpi {0};
606     uint32_t extendDefaultDpi_ {0};
607     uint32_t defaultDeviceRotationOffset_ { 0 };
608     std::atomic<ExtendScreenConnectStatus> extendScreenConnectStatus_ = ExtendScreenConnectStatus::UNKNOWN;
609     bool isExtendScreenConnected_ = false;
610 
611     /**
612      * On/Off screen
613      */
614     bool isMultiScreenCollaboration_ = false;
615     bool screenPrivacyStates = false;
616     bool keyguardDrawnDone_ = true;
617     bool needScreenOnWhenKeyguardNotify_ = false;
618     bool gotScreenOffNotify_ = false;
619     bool needScreenOffNotify_ = false;
620     bool dozeNotifyFinish_ = false;
621     bool pictureFrameReady_ = false;
622     bool pictureFrameBreak_ = false;
623 
624     std::mutex screenOnMutex_;
625     std::condition_variable screenOnCV_;
626     std::mutex screenOffMutex_;
627     std::condition_variable screenOffCV_;
628     std::mutex screenWaitPictureFrameMutex_;
629     std::condition_variable screenWaitPictureFrameCV_;
630     int32_t screenOffDelay_ {0};
631     int32_t screenOnDelay_ {0};
632 
633     std::vector<ScreenId> mirrorScreenIds_;
634     std::mutex snapBypickerMutex_;
635 
636     std::mutex freezedPidListMutex_;
637     std::set<int32_t> freezedPidList_;
638 
639     std::atomic<PowerStateChangeReason> prePowerStateChangeReason_ =
640         PowerStateChangeReason::STATE_CHANGE_REASON_UNKNOWN;
641     std::atomic<PowerStateChangeReason> lastWakeUpReason_ = PowerStateChangeReason::STATE_CHANGE_REASON_INIT;
642     std::atomic<PowerStateChangeReason> currentWakeUpReason_ = PowerStateChangeReason::STATE_CHANGE_REASON_INIT;
643     std::atomic<bool> isScreenLockSuspend_ = false;
644     std::atomic<bool> gotScreenlockFingerprint_ = false;
645     std::atomic<bool> isPhyScreenConnected_ = false;
646     int32_t cameraStatus_ = {0};
647     int32_t cameraPosition_ = {0};
648     PowerStateChangeReason powerStateChangeReason_ = PowerStateChangeReason::STATE_CHANGE_REASON_UNKNOWN;
649 
650     // Fold Screen
651     std::map<ScreenId, ScreenProperty> phyScreenPropMap_;
652     mutable std::recursive_mutex phyScreenPropMapMutex_;
653     static void BootFinishedCallback(const char *key, const char *value, void *context);
654     std::function<void()> foldScreenPowerInit_ = nullptr;
655     void HandleFoldScreenPowerInit();
656     void SetFoldScreenPowerInit(std::function<void()> foldScreenPowerInit);
657     void SetDpiFromSettingData();
658     void SetRotateLockedFromSettingData();
659     void NotifyClientProxyUpdateFoldDisplayMode(FoldDisplayMode displayMode);
660     void UpdateDisplayScaleState(ScreenId screenId);
661     void SetDisplayScaleInner(ScreenId screenId, const float& scaleX, const float& scaleY, const float& pivotX,
662                                   const float& pivotY);
663     void CalcDisplayNodeTranslateOnFoldableRotation(sptr<ScreenSession>& session, const float& scaleX,
664                                                    const float& scaleY, const float& pivotX, const float& pivotY,
665                                                    float& translateX, float& translateY);
666     void CalcDisplayNodeTranslateOnPocketFoldRotation(sptr<ScreenSession>& session, const float& scaleX,
667                                                    const float& scaleY, const float& pivotX, const float& pivotY,
668                                                    float& translateX, float& translateY);
669     void CalcDisplayNodeTranslateOnRotation(sptr<ScreenSession>& session, const float& scaleX, const float& scaleY,
670                                             const float& pivotX, const float& pivotY, float& translateX,
671                                             float& translateY);
672     void RegisterApplicationStateObserver();
673     void SetPostureAndHallSensorEnabled();
674     bool IsDefaultMirrorMode(ScreenId screenId);
675     void SetCastFromSettingData();
676     void RegisterCastObserver(std::vector<ScreenId>& mirrorScreenIds);
677     void SetCastPrivacyFromSettingData();
678     bool SetCastPrivacyToRS(sptr<ScreenSession> screenSession, bool enable);
679     void ExitCoordination(const std::string& reason);
680     void UpdateDisplayState(std::vector<ScreenId> screenIds, DisplayState state);
681     void SetExtendPixelRatio(const float& dpi);
682     void CallRsSetScreenPowerStatusSyncForExtend(const std::vector<ScreenId>& screenIds, ScreenPowerStatus status);
683     void SetRsSetScreenPowerStatusSync(const std::vector<ScreenId>& screenIds, ScreenPowerStatus status);
684     DisplayState lastDisplayState_ { DisplayState::UNKNOWN };
685     void SetMultiScreenOuterMode(sptr<ScreenSession>& innerSession, sptr<ScreenSession>& outerSession);
686     void RecoveryMultiScreenNormalMode(sptr<ScreenSession>& innerSession, sptr<ScreenSession>& outerSession);
687     bool IsFakeDisplayExist();
688     DMError DoMakeUniqueScreenOld(const std::vector<ScreenId>& allUniqueScreenIds, std::vector<DisplayId>& displayIds,
689         bool isCallingByThirdParty);
690     bool IsSupportCoordination();
691 
692     LowTempMode lowTemp_ {LowTempMode::UNKNOWN};
693     std::mutex lowTempMutex_;
694 
695 private:
696     class ScbClientListenerDeathRecipient : public IRemoteObject::DeathRecipient {
697         public:
ScbClientListenerDeathRecipient(int32_t scbPid)698             explicit ScbClientListenerDeathRecipient(int32_t scbPid)
699                 : scbPid_(scbPid)
700             {}
701 
OnRemoteDied(const wptr<IRemoteObject> & wptrDeath)702             void OnRemoteDied(const wptr<IRemoteObject>& wptrDeath) override
703             {
704                 ScreenSessionManager::GetInstance().ScbClientDeathCallback(scbPid_);
705             }
706 
707         private:
708             int32_t scbPid_;
709     };
710 };
711 } // namespace OHOS::Rosen
712 
713 #endif // OHOS_ROSEN_WINDOW_SCENE_SCREEN_SESSION_MANAGER_H
714