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