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_H
17 #define OHOS_ROSEN_WINDOW_SCENE_SCREEN_SESSION_H
18 
19 #include <mutex>
20 #include <vector>
21 
22 #include <refbase.h>
23 #include <screen_manager/screen_types.h>
24 #include <shared_mutex>
25 #include <ui/rs_display_node.h>
26 
27 #include "screen_property.h"
28 #include "dm_common.h"
29 #include "display_info.h"
30 #include "screen.h"
31 #include "screen_info.h"
32 #include "screen_group.h"
33 #include "screen_group_info.h"
34 #include "event_handler.h"
35 #include "session_manager/include/screen_rotation_property.h"
36 
37 namespace OHOS::Rosen {
38 using SetScreenSceneDpiFunc = std::function<void(float density)>;
39 using DestroyScreenSceneFunc = std::function<void()>;
40 
41 class IScreenChangeListener {
42 public:
43     virtual void OnConnect(ScreenId screenId) = 0;
44     virtual void OnDisconnect(ScreenId screenId) = 0;
45     virtual void OnPropertyChange(const ScreenProperty& newProperty, ScreenPropertyChangeReason reason,
46         ScreenId screenId) = 0;
47     virtual void OnPowerStatusChange(DisplayPowerEvent event, EventStatus status,
48         PowerStateChangeReason reason) = 0;
49     virtual void OnSensorRotationChange(float sensorRotation, ScreenId screenId) = 0;
50     virtual void OnScreenOrientationChange(float screenOrientation, ScreenId screenId) = 0;
51     virtual void OnScreenRotationLockedChange(bool isLocked, ScreenId screenId) = 0;
52     virtual void OnHoverStatusChange(int32_t hoverStatus, ScreenId extendScreenId) = 0;
53     virtual void OnScreenCaptureNotify(ScreenId mainScreenId, int32_t uid, const std::string& clientName) = 0;
54 };
55 
56 enum class MirrorScreenType : int32_t {
57     PHYSICAL_MIRROR = 0,
58     VIRTUAL_MIRROR = 1,
59 };
60 
61 enum class ScreenState : int32_t {
62     INIT,
63     CONNECTION,
64     DISCONNECTION,
65 };
66 
67 struct ScreenSessionConfig {
68     ScreenId screenId {0};
69     ScreenId rsId {0};
70     ScreenId defaultScreenId {0};
71     ScreenId mirrorNodeId {0};
72     std::string name = "UNKNOWN";
73     ScreenProperty property;
74     std::shared_ptr<RSDisplayNode> displayNode;
75 };
76 
77 enum class ScreenSessionReason : int32_t {
78     CREATE_SESSION_FOR_CLIENT,
79     CREATE_SESSION_FOR_VIRTUAL,
80     CREATE_SESSION_FOR_MIRROR,
81     CREATE_SESSION_FOR_REAL,
82     CREATE_SESSION_WITHOUT_DISPLAY_NODE,
83     INVALID,
84 };
85 
86 class ScreenSession : public RefBase {
87 public:
88     ScreenSession() = default;
89     ScreenSession(const ScreenSessionConfig& config, ScreenSessionReason reason);
90     ScreenSession(ScreenId screenId, ScreenId rsId, const std::string& name,
91         const ScreenProperty& property, const std::shared_ptr<RSDisplayNode>& displayNode);
92     ScreenSession(ScreenId screenId, const ScreenProperty& property, ScreenId defaultScreenId);
93     ScreenSession(ScreenId screenId, const ScreenProperty& property, NodeId nodeId, ScreenId defaultScreenId);
94     ScreenSession(const std::string& name, ScreenId smsId, ScreenId rsId, ScreenId defaultScreenId);
95     virtual ~ScreenSession();
96 
97     void CreateDisplayNode(const Rosen::RSDisplayNodeConfig& config);
98     void SetDisplayNodeScreenId(ScreenId screenId);
99     void RegisterScreenChangeListener(IScreenChangeListener* screenChangeListener);
100     void UnregisterScreenChangeListener(IScreenChangeListener* screenChangeListener);
101 
102     sptr<DisplayInfo> ConvertToDisplayInfo();
103     sptr<ScreenInfo> ConvertToScreenInfo() const;
104     sptr<SupportedScreenModes> GetActiveScreenMode() const;
105     ScreenSourceMode GetSourceMode() const;
106     void SetScreenCombination(ScreenCombination combination);
107     ScreenCombination GetScreenCombination() const;
108 
109     Orientation GetOrientation() const;
110     void SetOrientation(Orientation orientation);
111     Rotation GetRotation() const;
112     void SetRotation(Rotation rotation);
113     void SetRotationAndScreenRotationOnly(Rotation rotation);
114     void SetScreenRequestedOrientation(Orientation orientation);
115     Orientation GetScreenRequestedOrientation() const;
116     void SetUpdateToInputManagerCallback(std::function<void(float)> updateToInputManagerCallback);
117     void SetUpdateScreenPivotCallback(std::function<void(float, float)>&& updateScreenPivotCallback);
118 
119     void SetVirtualPixelRatio(float virtualPixelRatio);
120     void SetScreenSceneDpiChangeListener(const SetScreenSceneDpiFunc& func);
121     void SetScreenSceneDpi(float density);
122     void SetDensityInCurResolution(float densityInCurResolution);
123     void SetScreenType(ScreenType type);
124     void SetMirrorScreenType(MirrorScreenType type);
125     MirrorScreenType GetMirrorScreenType();
126 
127     void SetScreenSceneDestroyListener(const DestroyScreenSceneFunc& func);
128     void DestroyScreenScene();
129 
130     void SetScreenScale(float scaleX, float scaleY, float pivotX, float pivotY, float translateX, float translateY);
131 
132     std::string GetName();
133     ScreenId GetScreenId();
134     ScreenId GetRSScreenId();
135     ScreenProperty GetScreenProperty() const;
136     void UpdatePropertyByActiveMode();
137     std::shared_ptr<RSDisplayNode> GetDisplayNode() const;
138     void ReleaseDisplayNode();
139 
140     Rotation CalcRotation(Orientation orientation, FoldDisplayMode foldDisplayMode) const;
141     DisplayOrientation CalcDisplayOrientation(Rotation rotation, FoldDisplayMode foldDisplayMode) const;
142     DisplayOrientation CalcDeviceOrientation(Rotation rotation) const;
143     void FillScreenInfo(sptr<ScreenInfo> info) const;
144     void InitRSDisplayNode(RSDisplayNodeConfig& config, Point& startPoint);
145 
146     DMError GetScreenSupportedColorGamuts(std::vector<ScreenColorGamut>& colorGamuts);
147     DMError GetScreenColorGamut(ScreenColorGamut& colorGamut);
148     DMError SetScreenColorGamut(int32_t colorGamutIdx);
149     DMError GetScreenGamutMap(ScreenGamutMap& gamutMap);
150     DMError SetScreenGamutMap(ScreenGamutMap gamutMap);
151     DMError SetScreenColorTransform();
152 
153     DMError GetPixelFormat(GraphicPixelFormat& pixelFormat);
154     DMError SetPixelFormat(GraphicPixelFormat pixelFormat);
155     DMError GetSupportedHDRFormats(std::vector<ScreenHDRFormat>& hdrFormats);
156     DMError GetScreenHDRFormat(ScreenHDRFormat& hdrFormat);
157     DMError SetScreenHDRFormat(int32_t modeIdx);
158     DMError GetSupportedColorSpaces(std::vector<GraphicCM_ColorSpaceType>& colorSpaces);
159     DMError GetScreenColorSpace(GraphicCM_ColorSpaceType& colorSpace);
160     DMError SetScreenColorSpace(GraphicCM_ColorSpaceType colorSpace);
161 
162     void HandleSensorRotation(float sensorRotation);
163     void HandleHoverStatusChange(int32_t hoverStatus);
164     float ConvertRotationToFloat(Rotation sensorRotation);
165 
166     bool HasPrivateSessionForeground() const;
167     void SetPrivateSessionForeground(bool hasPrivate);
168     void SetDisplayBoundary(const RectF& rect, const uint32_t& offsetY);
169     void SetScreenRotationLocked(bool isLocked);
170     void SetScreenRotationLockedFromJs(bool isLocked);
171     bool IsScreenRotationLocked();
172     void SetTouchEnabledFromJs(bool isTouchEnabled);
173     bool IsTouchEnabled();
174 
175     void UpdateToInputManager(RRect bounds, int rotation, int deviceRotation, FoldDisplayMode foldDisplayMode);
176     void UpdatePropertyAfterRotation(RRect bounds, int rotation, FoldDisplayMode foldDisplayMode);
177     void UpdatePropertyOnly(RRect bounds, int rotation, FoldDisplayMode foldDisplayMode);
178     void UpdateRotationOrientation(int rotation);
179     void UpdatePropertyByFoldControl(const ScreenProperty& updatedProperty);
180     void UpdateDisplayState(DisplayState displayState);
181     void UpdateRefreshRate(uint32_t refreshRate);
182     uint32_t GetRefreshRate();
183     void UpdatePropertyByResolution(uint32_t width, uint32_t height);
184     void SetName(std::string name);
185     void Resize(uint32_t width, uint32_t height);
186 
187     void SetHdrFormats(std::vector<uint32_t>&& hdrFormats);
188     void SetColorSpaces(std::vector<uint32_t>&& colorSpaces);
189 
190     VirtualScreenFlag GetVirtualScreenFlag();
191     void SetVirtualScreenFlag(VirtualScreenFlag screenFlag);
192 
193     std::string name_ { "UNKNOWN" };
194     ScreenId screenId_ {};
195     ScreenId rsId_ {};
196     ScreenId defaultScreenId_ = SCREEN_ID_INVALID;
197     bool isExtended_ { false };
198 
199     NodeId nodeId_ {};
200 
201     int32_t activeIdx_ { 0 };
202     std::vector<sptr<SupportedScreenModes>> modes_ = {};
203 
204     bool isScreenGroup_ { false };
205     ScreenId groupSmsId_ { SCREEN_ID_INVALID };
206     ScreenId lastGroupSmsId_ { SCREEN_ID_INVALID };
207     std::atomic<bool> isScreenLocked_ = true;
208 
209     void Connect();
210     void Disconnect();
211     void PropertyChange(const ScreenProperty& newProperty, ScreenPropertyChangeReason reason);
212     void PowerStatusChange(DisplayPowerEvent event, EventStatus status, PowerStateChangeReason reason);
213     // notify scb
214     void SensorRotationChange(Rotation sensorRotation);
215     void SensorRotationChange(float sensorRotation);
216     void HoverStatusChange(int32_t hoverStatus);
217     void ScreenOrientationChange(Orientation orientation, FoldDisplayMode foldDisplayMode);
218     void ScreenOrientationChange(float orientation);
219     DMRect GetAvailableArea();
220     void SetAvailableArea(DMRect area);
221     bool UpdateAvailableArea(DMRect area);
222     void SetFoldScreen(bool isFold);
223     void UpdateRotationAfterBoot(bool foldToExpand);
224     void UpdateValidRotationToScb();
225     std::shared_ptr<Media::PixelMap> GetScreenSnapshot(float scaleX, float scaleY);
226     void SetDefaultDeviceRotationOffset(uint32_t defaultRotationOffset);
227     Rotation ConvertIntToRotation(int rotation);
228     void SetPhysicalRotation(int rotation);
229     void SetScreenComponentRotation(int rotation);
230     void ScreenCaptureNotify(ScreenId mainScreenId, int32_t uid, const std::string& clientName);
231     void SetIsExtend(bool isExtend);
232     bool GetIsExtend() const;
233 
234 private:
235     ScreenProperty property_;
236     std::shared_ptr<RSDisplayNode> displayNode_;
237     ScreenState screenState_ { ScreenState::INIT };
238     std::vector<IScreenChangeListener*> screenChangeListenerList_;
239     ScreenCombination combination_ { ScreenCombination::SCREEN_ALONE };
240     VirtualScreenFlag screenFlag_ { VirtualScreenFlag::DEFAULT };
241     MirrorScreenType mirrorScreenType_ { MirrorScreenType::VIRTUAL_MIRROR };
242     bool hasPrivateWindowForeground_ = false;
243     mutable std::shared_mutex displayNodeMutex_;
244     std::atomic<bool> touchEnabled_ { true };
245     std::function<void(float)> updateToInputManagerCallback_ = nullptr;
246     std::function<void(float, float)> updateScreenPivotCallback_ = nullptr;
247     bool isFold_ = false;
248     float currentSensorRotation_ { -1.0f };
249     float currentValidSensorRotation_ { -1.0f };
250     std::vector<uint32_t> hdrFormats_;
251     std::vector<uint32_t> colorSpaces_;
252     SetScreenSceneDpiFunc SetScreenSceneDpiCallback_ = nullptr;
253     DestroyScreenSceneFunc destroyScreenSceneCallback_ = nullptr;
254     void ReportNotifyModeChange(DisplayOrientation displayOrientation);
255     int32_t GetApiVersion();
256 };
257 
258 class ScreenSessionGroup : public ScreenSession {
259 public:
260     ScreenSessionGroup(ScreenId smsId, ScreenId rsId, std::string name, ScreenCombination combination);
261     ScreenSessionGroup() = delete;
262     WM_DISALLOW_COPY_AND_MOVE(ScreenSessionGroup);
263     ~ScreenSessionGroup();
264 
265     bool AddChild(sptr<ScreenSession>& smsScreen, Point& startPoint);
266     bool AddChild(sptr<ScreenSession>& smsScreen, Point& startPoint, sptr<ScreenSession> defaultScreenSession);
267     bool AddChildren(std::vector<sptr<ScreenSession>>& smsScreens, std::vector<Point>& startPoints);
268     bool RemoveChild(sptr<ScreenSession>& smsScreen);
269     bool HasChild(ScreenId childScreen) const;
270     std::vector<sptr<ScreenSession>> GetChildren() const;
271     std::vector<Point> GetChildrenPosition() const;
272     Point GetChildPosition(ScreenId screenId) const;
273     size_t GetChildCount() const;
274     sptr<ScreenGroupInfo> ConvertToScreenGroupInfo() const;
275     ScreenCombination GetScreenCombination() const;
276 
277     ScreenCombination combination_ { ScreenCombination::SCREEN_ALONE };
278     ScreenId mirrorScreenId_ { SCREEN_ID_INVALID };
279 
280 private:
281     bool GetRSDisplayNodeConfig(sptr<ScreenSession>& screenSession, struct RSDisplayNodeConfig& config,
282         sptr<ScreenSession> defaultScreenSession);
283 
284     std::map<ScreenId, std::pair<sptr<ScreenSession>, Point>> screenSessionMap_;
285 };
286 
287 } // namespace OHOS::Rosen
288 
289 #endif // OHOS_ROSEN_WINDOW_SCENE_SCREEN_SESSION_H
290