1 /*
2  * Copyright (c) 2021-2022 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/display_manager_agent_interface.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 
80     // colorspace, gamut
81     DMError GetScreenSupportedColorGamuts(ScreenId screenId, std::vector<ScreenColorGamut>& colorGamuts);
82     DMError GetScreenColorGamut(ScreenId screenId, ScreenColorGamut& colorGamut);
83     DMError SetScreenColorGamut(ScreenId screenId, int32_t colorGamutIdx);
84     DMError GetScreenGamutMap(ScreenId screenId, ScreenGamutMap& gamutMap);
85     DMError SetScreenGamutMap(ScreenId screenId, ScreenGamutMap gamutMap);
86     DMError SetScreenColorTransform(ScreenId screenId);
87 
88 private:
IsVertical(Rotation rotation)89     static inline bool IsVertical(Rotation rotation)
90     {
91         return (rotation == Rotation::ROTATION_0 || rotation == Rotation::ROTATION_180);
92     }
93     void SetScreenRotateAnimation(sptr<AbstractScreen>& screen, ScreenId screenId,
94         Rotation rotationAfter, bool withAnimation);
95     void RegisterRsScreenConnectionChangeListener();
96     void OnRsScreenConnectionChange(ScreenId rsScreenId, ScreenEvent screenEvent);
97     bool OnRemoteDied(const sptr<IRemoteObject>& agent);
98     void ProcessScreenConnected(ScreenId rsScreenId);
99     void ProcessDefaultScreenReconnected(ScreenId rsScreenId);
100     sptr<AbstractScreen> InitAndGetScreen(ScreenId rsScreenId);
101     void ProcessScreenDisconnected(ScreenId rsScreenId);
102     bool InitAbstractScreenModesInfo(sptr<AbstractScreen>& absScreen);
103     sptr<AbstractScreen> InitVirtualScreen(ScreenId dmsScreenId, ScreenId rsId, VirtualScreenOption option);
104     sptr<AbstractScreenGroup> AddToGroupLocked(sptr<AbstractScreen> newScreen);
105     sptr<AbstractScreenGroup> RemoveFromGroupLocked(sptr<AbstractScreen> screen);
106     void RemoveDefaultScreenFromGroupLocked(sptr<AbstractScreen> screen);
107     bool RemoveChildFromGroup(sptr<AbstractScreen>, sptr<AbstractScreenGroup>);
108     bool CheckScreenInScreenGroup(sptr<AbstractScreen> newScreen) const;
109     sptr<AbstractScreenGroup> AddAsFirstScreenLocked(sptr<AbstractScreen> newScreen);
110     sptr<AbstractScreenGroup> AddAsSuccedentScreenLocked(sptr<AbstractScreen> newScreen);
111     void ProcessScreenModeChanged(ScreenId dmsScreenId);
112     void ChangeScreenGroup(sptr<AbstractScreenGroup> group, const std::vector<ScreenId>& screens,
113         const std::vector<Point>& startPoints, bool filterScreen, ScreenCombination combination);
114     void AddScreenToGroup(sptr<AbstractScreenGroup>, const std::vector<ScreenId>&,
115         const std::vector<Point>&, std::map<ScreenId, bool>&);
116     void NotifyScreenConnected(sptr<ScreenInfo>) const;
117     void NotifyScreenDisconnected(ScreenId screenId) const;
118     void NotifyScreenChanged(sptr<ScreenInfo> screenInfo, ScreenChangeEvent event) const;
119     void NotifyScreenGroupChanged(const sptr<ScreenInfo>& screenInfo, ScreenGroupChangeEvent event) const;
120     void NotifyScreenGroupChanged(const std::vector<sptr<ScreenInfo>>& screenInfo, ScreenGroupChangeEvent event) const;
121     void OpenRotationSyncTransaction();
122     void CloseRotationSyncTransaction();
123     void UpdateScreenGroupLayout(sptr<AbstractScreenGroup> screenGroup);
124     void SetDisplayNode(Rotation rotationAfter, const std::shared_ptr<RSDisplayNode>& displayNode,
125         struct ScreenRect srect);
126 
127     class ScreenIdManager {
128     public:
129         ScreenIdManager() = default;
130         ~ScreenIdManager() = default;
131         WM_DISALLOW_COPY_AND_MOVE(ScreenIdManager);
132         ScreenId CreateAndGetNewScreenId(ScreenId rsScreenId);
133         bool DeleteScreenId(ScreenId dmsScreenId);
134         bool HasDmsScreenId(ScreenId dmsScreenId) const;
135         bool HasRsScreenId(ScreenId dmsScreenId) const;
136         bool ConvertToRsScreenId(ScreenId, ScreenId&) const;
137         ScreenId ConvertToRsScreenId(ScreenId) const;
138         bool ConvertToDmsScreenId(ScreenId, ScreenId&) const;
139         ScreenId ConvertToDmsScreenId(ScreenId) const;
140     private:
141         std::atomic<ScreenId> dmsScreenCount_ {0};
142         std::map<ScreenId, ScreenId> rs2DmsScreenIdMap_;
143         std::map<ScreenId, ScreenId> dms2RsScreenIdMap_;
144     };
145 
146     std::recursive_mutex& mutex_;
147     OHOS::Rosen::RSInterfaces& rsInterface_;
148     ScreenIdManager screenIdManager_;
149     std::map<ScreenId, sptr<AbstractScreen>> dmsScreenMap_;
150     std::map<ScreenId, sptr<AbstractScreenGroup>> dmsScreenGroupMap_;
151     std::map<sptr<IRemoteObject>, std::vector<ScreenId>> screenAgentMap_;
152     sptr<AgentDeathRecipient> deathRecipient_ { nullptr };
153     sptr<AbstractScreenCallback> abstractScreenCallback_;
154     std::shared_ptr<AppExecFwk::EventHandler> controllerHandler_;
155     std::atomic<ScreenId> defaultRsScreenId_ {SCREEN_ID_INVALID };
156     Orientation buildInDefaultOrientation_ { Orientation::UNSPECIFIED };
157     bool isExpandCombination_ = false;
158     ScreenPowerState powerState_ { ScreenPowerState::INVALID_STATE };
159 };
160 
161 struct ScreenRect {
162     float w;
163     float h;
164     float x;
165     float y;
166 };
167 
168 } // namespace OHOS::Rosen
169 #endif // FOUNDATION_DMSERVER_ABSTRACT_SCREEN_CONTROLLER_H