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 "screen_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 OnScreenExtendChange(ScreenId mainScreenId, ScreenId extendScreenId) = 0;
53     virtual void OnHoverStatusChange(int32_t hoverStatus, ScreenId extendScreenId) = 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() = default;
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 SetScreenRequestedOrientation(Orientation orientation);
114     Orientation GetScreenRequestedOrientation() const;
115     void SetUpdateToInputManagerCallback(std::function<void(float)> updateToInputManagerCallback);
116     void SetUpdateScreenPivotCallback(std::function<void(float, float)>&& updateScreenPivotCallback);
117 
118     void SetVirtualPixelRatio(float virtualPixelRatio);
119     void SetScreenSceneDpiChangeListener(const SetScreenSceneDpiFunc& func);
120     void SetScreenSceneDpi(float density);
121     void SetDensityInCurResolution(float densityInCurResolution);
122     void SetDefaultDensity(float DefaultDensity);
123     void UpdateVirtualPixelRatio(const RRect& bounds);
124     void SetScreenType(ScreenType type);
125 
126     void SetScreenSceneDestroyListener(const DestroyScreenSceneFunc& func);
127     void DestroyScreenScene();
128 
129     void SetScreenScale(float scaleX, float scaleY, float pivotX, float pivotY, float translateX, float translateY);
130 
131     std::string GetName();
132     ScreenId GetScreenId();
133     ScreenId GetRSScreenId();
134     ScreenProperty GetScreenProperty() const;
135     void UpdatePropertyByActiveMode();
136     std::shared_ptr<RSDisplayNode> GetDisplayNode() const;
137     void ReleaseDisplayNode();
138 
139     Rotation CalcRotation(Orientation orientation, FoldDisplayMode foldDisplayMode) const;
140     DisplayOrientation CalcDisplayOrientation(Rotation rotation, FoldDisplayMode foldDisplayMode) const;
141     void FillScreenInfo(sptr<ScreenInfo> info) const;
142     void InitRSDisplayNode(RSDisplayNodeConfig& config, Point& startPoint);
143 
144     DMError GetScreenSupportedColorGamuts(std::vector<ScreenColorGamut>& colorGamuts);
145     DMError GetScreenColorGamut(ScreenColorGamut& colorGamut);
146     DMError SetScreenColorGamut(int32_t colorGamutIdx);
147     DMError GetScreenGamutMap(ScreenGamutMap& gamutMap);
148     DMError SetScreenGamutMap(ScreenGamutMap gamutMap);
149     DMError SetScreenColorTransform();
150 
151     DMError GetPixelFormat(GraphicPixelFormat& pixelFormat);
152     DMError SetPixelFormat(GraphicPixelFormat pixelFormat);
153     DMError GetSupportedHDRFormats(std::vector<ScreenHDRFormat>& hdrFormats);
154     DMError GetScreenHDRFormat(ScreenHDRFormat& hdrFormat);
155     DMError SetScreenHDRFormat(int32_t modeIdx);
156     DMError GetSupportedColorSpaces(std::vector<GraphicCM_ColorSpaceType>& colorSpaces);
157     DMError GetScreenColorSpace(GraphicCM_ColorSpaceType& colorSpace);
158     DMError SetScreenColorSpace(GraphicCM_ColorSpaceType colorSpace);
159 
160     void HandleSensorRotation(float sensorRotation);
161     void HandleHoverStatusChange(int32_t hoverStatus);
162     float ConvertRotationToFloat(Rotation sensorRotation);
163 
164     bool HasPrivateSessionForeground() const;
165     void SetPrivateSessionForeground(bool hasPrivate);
166     void SetDisplayBoundary(const RectF& rect, const uint32_t& offsetY);
167     void SetScreenRotationLocked(bool isLocked);
168     void SetScreenRotationLockedFromJs(bool isLocked);
169     bool IsScreenRotationLocked();
170     void SetTouchEnabledFromJs(bool isTouchEnabled);
171     bool IsTouchEnabled();
172 
173     void UpdateToInputManager(RRect bounds, int rotation, FoldDisplayMode foldDisplayMode);
174     void UpdatePropertyAfterRotation(RRect bounds, int rotation, FoldDisplayMode foldDisplayMode);
175     void UpdatePropertyOnly(RRect bounds, int rotation, FoldDisplayMode foldDisplayMode);
176     ScreenProperty UpdatePropertyByFoldControl(const ScreenProperty& updatedProperty);
177     void UpdateDisplayState(DisplayState displayState);
178     void UpdateRefreshRate(uint32_t refreshRate);
179     uint32_t GetRefreshRate();
180     void UpdatePropertyByResolution(uint32_t width, uint32_t height);
181     void SetName(std::string name);
182     void Resize(uint32_t width, uint32_t height);
183 
184     void SetHdrFormats(std::vector<uint32_t>&& hdrFormats);
185     void SetColorSpaces(std::vector<uint32_t>&& colorSpaces);
186 
187     VirtualScreenFlag GetVirtualScreenFlag();
188     void SetVirtualScreenFlag(VirtualScreenFlag screenFlag);
189 
190     std::string name_ { "UNKNOWN" };
191     ScreenId screenId_ {};
192     ScreenId rsId_ {};
193     ScreenId defaultScreenId_ = SCREEN_ID_INVALID;
194 
195     void SetIsExtend(bool isExtend);
196     bool GetIsExtend() const;
197     void SetIsInternal(bool isInternal);
198     bool GetIsInternal() const;
199     void SetIsCurrentInUse(bool isInUse);
200     bool GetIsCurrentInUse() const;
201 
202     bool isPrimary_ { false };
203     bool isInternal_ { false };
204     bool isExtended_ { false };
205     bool isInUse_ { false };
206 
207     NodeId nodeId_ {};
208 
209     int32_t activeIdx_ { 0 };
210     std::vector<sptr<SupportedScreenModes>> modes_ = {};
211 
212     bool isScreenGroup_ { false };
213     ScreenId groupSmsId_ { SCREEN_ID_INVALID };
214     ScreenId lastGroupSmsId_ { SCREEN_ID_INVALID };
215     std::atomic<bool> isScreenLocked_ = true;
216 
217     void Connect();
218     void Disconnect();
219     void PropertyChange(const ScreenProperty& newProperty, ScreenPropertyChangeReason reason);
220     void PowerStatusChange(DisplayPowerEvent event, EventStatus status, PowerStateChangeReason reason);
221     // notify scb
222     void SensorRotationChange(Rotation sensorRotation);
223     void SensorRotationChange(float sensorRotation);
224     void HoverStatusChange(int32_t hoverStatus);
225     void ScreenOrientationChange(Orientation orientation, FoldDisplayMode foldDisplayMode);
226     void ScreenOrientationChange(float orientation);
227     void ScreenExtendChange(ScreenId mainScreenId, ScreenId extendScreenId);
228     DMRect GetAvailableArea();
229     void SetAvailableArea(DMRect area);
230     bool UpdateAvailableArea(DMRect area);
231     void SetFoldScreen(bool isFold);
232     void UpdateRotationAfterBoot(bool foldToExpand);
233     std::shared_ptr<Media::PixelMap> GetScreenSnapshot(float scaleX, float scaleY);
234     void SetDefaultDeviceRotationOffset(uint32_t defaultRotationOffset);
235 
236     void SetMirrorScreenType(MirrorScreenType mirrorType);
237     MirrorScreenType GetMirrorScreenType();
238     Rotation ConvertIntToRotation(int rotation);
239     void SetPhysicalRotation(int rotation, FoldStatus foldStatus);
240     void SetStartPosition(uint32_t startX, uint32_t startY);
241 
242 private:
243     ScreenProperty property_;
244     std::shared_ptr<RSDisplayNode> displayNode_;
245     ScreenState screenState_ { ScreenState::INIT };
246     std::vector<IScreenChangeListener*> screenChangeListenerList_;
247     ScreenCombination combination_ { ScreenCombination::SCREEN_ALONE };
248     VirtualScreenFlag screenFlag_ { VirtualScreenFlag::DEFAULT };
249     bool hasPrivateWindowForeground_ = false;
250     mutable std::shared_mutex displayNodeMutex_;
251     std::atomic<bool> touchEnabled_ { true };
252     std::function<void(float)> updateToInputManagerCallback_ = nullptr;
253     std::function<void(float, float)> updateScreenPivotCallback_ = nullptr;
254     bool isFold_ = false;
255     float currentSensorRotation_ { -1.0f };
256     std::vector<uint32_t> hdrFormats_;
257     std::vector<uint32_t> colorSpaces_;
258     MirrorScreenType mirrorScreenType_ { MirrorScreenType::VIRTUAL_MIRROR };
259     SetScreenSceneDpiFunc setScreenSceneDpiCallback_ = nullptr;
260     DestroyScreenSceneFunc destroyScreenSceneCallback_ = nullptr;
261     void ReportNotifyModeChange(DisplayOrientation displayOrientation);
262 };
263 
264 class ScreenSessionGroup : public ScreenSession {
265 public:
266     ScreenSessionGroup(ScreenId smsId, ScreenId rsId, std::string name, ScreenCombination combination);
267     ScreenSessionGroup() = delete;
268     WM_DISALLOW_COPY_AND_MOVE(ScreenSessionGroup);
269     ~ScreenSessionGroup();
270 
271     bool AddChild(sptr<ScreenSession>& smsScreen, Point& startPoint);
272     bool AddChild(sptr<ScreenSession>& smsScreen, Point& startPoint, sptr<ScreenSession> defaultScreenSession);
273     bool AddChildren(std::vector<sptr<ScreenSession>>& smsScreens, std::vector<Point>& startPoints);
274     bool RemoveChild(sptr<ScreenSession>& smsScreen);
275     bool HasChild(ScreenId childScreen) const;
276     std::vector<sptr<ScreenSession>> GetChildren() const;
277     std::vector<Point> GetChildrenPosition() const;
278     Point GetChildPosition(ScreenId screenId) const;
279     size_t GetChildCount() const;
280     sptr<ScreenGroupInfo> ConvertToScreenGroupInfo() const;
281     ScreenCombination GetScreenCombination() const;
282 
283     ScreenCombination combination_ { ScreenCombination::SCREEN_ALONE };
284     ScreenId mirrorScreenId_ { SCREEN_ID_INVALID };
285 
286 private:
287     bool GetRSDisplayNodeConfig(sptr<ScreenSession>& screenSession, struct RSDisplayNodeConfig& config,
288         sptr<ScreenSession> defaultScreenSession);
289 
290     std::map<ScreenId, std::pair<sptr<ScreenSession>, Point>> screenSessionMap_;
291 };
292 
293 } // namespace OHOS::Rosen
294 
295 #endif // OHOS_ROSEN_WINDOW_SCENE_SCREEN_SESSION_H
296