• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 FOUNDATION_DMSERVER_ABSTRACT_SCREEN_CONTROLLER_H
17 #define FOUNDATION_DMSERVER_ABSTRACT_SCREEN_CONTROLLER_H
18 
19 #include <map>
20 #include <vector>
21 
22 #include <event_handler.h>
23 #include <refbase.h>
24 #include <surface.h>
25 #include <transaction/rs_interfaces.h>
26 #include <transaction/rs_sync_transaction_controller.h>
27 
28 #include "abstract_screen.h"
29 #include "agent_death_recipient.h"
30 #include "display_manager_agent_controller.h"
31 #include "dm_common.h"
32 #include "screen.h"
33 #include "zidl/idisplay_manager_agent.h"
34 
35 namespace OHOS::Rosen {
36 class AbstractScreenController : public RefBase {
37 using OnAbstractScreenConnectCb = std::function<void(sptr<AbstractScreen>)>;
38 using OnAbstractScreenChangeCb = std::function<void(sptr<AbstractScreen>, DisplayChangeEvent event)>;
39 public:
40     struct AbstractScreenCallback : public RefBase {
41         OnAbstractScreenConnectCb onConnect_;
42         OnAbstractScreenConnectCb onDisconnect_;
43         OnAbstractScreenChangeCb onChange_;
44     };
45 
46     explicit AbstractScreenController(std::recursive_mutex& mutex);
47     ~AbstractScreenController();
48     WM_DISALLOW_COPY_AND_MOVE(AbstractScreenController);
49 
50     void Init();
51     std::vector<ScreenId> GetAllScreenIds() const;
52     sptr<AbstractScreen> GetAbstractScreen(ScreenId dmsScreenId) const;
53     std::vector<ScreenId> GetAllValidScreenIds(const std::vector<ScreenId>& screenIds) const;
54     sptr<AbstractScreenGroup> GetAbstractScreenGroup(ScreenId dmsScreenId);
55     ScreenId GetDefaultAbstractScreenId();
56     ScreenId ConvertToRsScreenId(ScreenId dmsScreenId) const;
57     ScreenId ConvertToDmsScreenId(ScreenId rsScreenId) const;
58     void RegisterAbstractScreenCallback(sptr<AbstractScreenCallback> cb);
59     ScreenId CreateVirtualScreen(VirtualScreenOption option, const sptr<IRemoteObject>& displayManagerAgent);
60     DMError DestroyVirtualScreen(ScreenId screenId);
61     DMError SetVirtualScreenSurface(ScreenId screenId, sptr<Surface> surface);
62     void SetBuildInDefaultOrientation(Orientation orientation);
63     DMError SetOrientation(ScreenId screenId, Orientation orientation, bool isFromWindow, bool withAnimation = true);
64     bool SetRotation(ScreenId screenId, Rotation rotationAfter, bool isFromWindow, bool withAnimation = true);
65 
66     DMError SetScreenActiveMode(ScreenId screenId, uint32_t modeId);
67     const std::shared_ptr<RSDisplayNode>& GetRSDisplayNodeByScreenId(ScreenId dmsScreenId) const;
68     void UpdateRSTree(ScreenId dmsScreenId, ScreenId parentScreenId, std::shared_ptr<RSSurfaceNode>& surfaceNode,
69         bool isAdd, bool isMultiDisplay);
70     DMError AddSurfaceNodeToScreen(ScreenId dmsScreenId, std::shared_ptr<RSSurfaceNode>& surfaceNode, bool onTop);
71     DMError RemoveSurfaceNodeFromScreen(ScreenId dmsScreenId, std::shared_ptr<RSSurfaceNode>& surfaceNode);
72     DMError MakeMirror(ScreenId, std::vector<ScreenId> screens);
73     bool MakeExpand(std::vector<ScreenId> screenIds, std::vector<Point> startPoints);
74     DMError StopScreens(const std::vector<ScreenId>& screenIds, ScreenCombination stopCombination);
75     void RemoveVirtualScreenFromGroup(std::vector<ScreenId> screens);
76     bool SetScreenPowerForAll(ScreenPowerState state, PowerStateChangeReason reason, bool needToNotify = true);
77     ScreenPowerState GetScreenPower(ScreenId dmsScreenId) const;
78     DMError SetVirtualPixelRatio(ScreenId screenId, float virtualPixelRatio);
79     bool SetVirtualScreenAsDefault(ScreenId screenId);
80 
81     // colorspace, gamut
82     DMError GetScreenSupportedColorGamuts(ScreenId screenId, std::vector<ScreenColorGamut>& colorGamuts);
83     DMError GetScreenColorGamut(ScreenId screenId, ScreenColorGamut& colorGamut);
84     DMError SetScreenColorGamut(ScreenId screenId, int32_t colorGamutIdx);
85     DMError GetScreenGamutMap(ScreenId screenId, ScreenGamutMap& gamutMap);
86     DMError SetScreenGamutMap(ScreenId screenId, ScreenGamutMap gamutMap);
87     DMError SetScreenColorTransform(ScreenId screenId);
88 
89 private:
IsVertical(Rotation rotation)90     static inline bool IsVertical(Rotation rotation)
91     {
92         return (rotation == Rotation::ROTATION_0 || rotation == Rotation::ROTATION_180);
93     }
94     void SetScreenRotateAnimation(sptr<AbstractScreen>& screen, ScreenId screenId,
95         Rotation rotationAfter, bool withAnimation);
96     void RegisterRsScreenConnectionChangeListener();
97     void OnRsScreenConnectionChange(ScreenId rsScreenId, ScreenEvent screenEvent);
98     bool OnRemoteDied(const sptr<IRemoteObject>& agent);
99     void ProcessScreenConnected(ScreenId rsScreenId);
100     void ProcessDefaultScreenReconnected(ScreenId rsScreenId);
101     sptr<AbstractScreen> InitAndGetScreen(ScreenId rsScreenId);
102     void ProcessScreenDisconnected(ScreenId rsScreenId);
103     bool InitAbstractScreenModesInfo(sptr<AbstractScreen>& absScreen);
104     sptr<AbstractScreen> InitVirtualScreen(ScreenId dmsScreenId, ScreenId rsId, VirtualScreenOption option);
105     sptr<AbstractScreenGroup> AddToGroupLocked(sptr<AbstractScreen> newScreen);
106     sptr<AbstractScreenGroup> RemoveFromGroupLocked(sptr<AbstractScreen> screen);
107     void RemoveDefaultScreenFromGroupLocked(sptr<AbstractScreen> screen);
108     bool RemoveChildFromGroup(sptr<AbstractScreen>, sptr<AbstractScreenGroup>);
109     bool CheckScreenInScreenGroup(sptr<AbstractScreen> newScreen) const;
110     sptr<AbstractScreenGroup> AddAsFirstScreenLocked(sptr<AbstractScreen> newScreen);
111     sptr<AbstractScreenGroup> AddAsSuccedentScreenLocked(sptr<AbstractScreen> newScreen);
112     void ProcessScreenModeChanged(ScreenId dmsScreenId);
113     void ChangeScreenGroup(sptr<AbstractScreenGroup> group, const std::vector<ScreenId>& screens,
114         const std::vector<Point>& startPoints, bool filterScreen, ScreenCombination combination);
115     void AddScreenToGroup(sptr<AbstractScreenGroup>, const std::vector<ScreenId>&,
116         const std::vector<Point>&, std::map<ScreenId, bool>&);
117     void NotifyScreenConnected(sptr<ScreenInfo>) const;
118     void NotifyScreenDisconnected(ScreenId screenId) const;
119     void NotifyScreenChanged(sptr<ScreenInfo> screenInfo, ScreenChangeEvent event) const;
120     void NotifyScreenGroupChanged(const sptr<ScreenInfo>& screenInfo, ScreenGroupChangeEvent event) const;
121     void NotifyScreenGroupChanged(const std::vector<sptr<ScreenInfo>>& screenInfo, ScreenGroupChangeEvent event) const;
122     void UpdateScreenGroupLayout(sptr<AbstractScreenGroup> screenGroup);
123     void SetDisplayNode(Rotation rotationAfter, const std::shared_ptr<RSDisplayNode>& displayNode,
124         struct ScreenRect srect);
125 
126     class ScreenIdManager {
127     public:
128         ScreenIdManager() = default;
129         ~ScreenIdManager() = default;
130         WM_DISALLOW_COPY_AND_MOVE(ScreenIdManager);
131         ScreenId CreateAndGetNewScreenId(ScreenId rsScreenId);
132         bool DeleteScreenId(ScreenId dmsScreenId);
133         bool HasDmsScreenId(ScreenId dmsScreenId) const;
134         bool HasRsScreenId(ScreenId dmsScreenId) const;
135         bool ConvertToRsScreenId(ScreenId, ScreenId&) const;
136         ScreenId ConvertToRsScreenId(ScreenId) const;
137         bool ConvertToDmsScreenId(ScreenId, ScreenId&) const;
138         ScreenId ConvertToDmsScreenId(ScreenId) const;
139     private:
140         std::atomic<ScreenId> dmsScreenCount_ {0};
141         std::map<ScreenId, ScreenId> rs2DmsScreenIdMap_;
142         std::map<ScreenId, ScreenId> dms2RsScreenIdMap_;
143     };
144 
145     std::recursive_mutex& mutex_;
146     OHOS::Rosen::RSInterfaces& rsInterface_;
147     ScreenIdManager screenIdManager_;
148     std::map<ScreenId, sptr<AbstractScreen>> dmsScreenMap_;
149     std::map<ScreenId, sptr<AbstractScreenGroup>> dmsScreenGroupMap_;
150     std::map<sptr<IRemoteObject>, std::vector<ScreenId>> screenAgentMap_;
151     sptr<AgentDeathRecipient> deathRecipient_ { nullptr };
152     sptr<AbstractScreenCallback> abstractScreenCallback_;
153     std::shared_ptr<AppExecFwk::EventHandler> controllerHandler_;
154     std::atomic<ScreenId> defaultRsScreenId_ {SCREEN_ID_INVALID };
155     Orientation buildInDefaultOrientation_ { Orientation::UNSPECIFIED };
156     bool isExpandCombination_ = false;
157     ScreenPowerState powerState_ { ScreenPowerState::INVALID_STATE };
158 };
159 
160 struct ScreenRect {
161     float w;
162     float h;
163     float x;
164     float y;
165 };
166 
167 } // namespace OHOS::Rosen
168 #endif // FOUNDATION_DMSERVER_ABSTRACT_SCREEN_CONTROLLER_H
169