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