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