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_DM_SCREEN_MANAGER_H
17 #define FOUNDATION_DM_SCREEN_MANAGER_H
18 
19 #include <refbase.h>
20 #include "screen.h"
21 #include "dm_common.h"
22 #include "screen_group.h"
23 #include "wm_single_instance.h"
24 #include "wm_single_instance.h"
25 
26 namespace OHOS::Rosen {
27 class ScreenManager : public RefBase {
28 WM_DECLARE_SINGLE_INSTANCE_BASE(ScreenManager);
29 friend class DMSDeathRecipient;
30 public:
31     class IScreenListener : public virtual RefBase {
32     public:
33         /**
34          * @brief Notify when a new screen is connected.
35          */
36         virtual void OnConnect(ScreenId) = 0;
37 
38         /**
39          * @brief Notify when a screen is disconnected.
40          */
41         virtual void OnDisconnect(ScreenId) = 0;
42 
43         /**
44          * @brief Notify when state of the screen is changed.
45          */
46         virtual void OnChange(ScreenId) = 0;
47     };
48 
49     class IScreenGroupListener : public virtual RefBase {
50     public:
51         /**
52          * @brief Notify when state of the screenGroup is changed.
53          */
54         virtual void OnChange(const std::vector<ScreenId>&, ScreenGroupChangeEvent) = 0;
55     };
56 
57     class IVirtualScreenGroupListener : public virtual RefBase {
58     public:
59         struct ChangeInfo {
60             ScreenGroupChangeEvent event;
61             std::string trigger;
62             std::vector<ScreenId> ids;
63         };
64         /**
65          * @brief Notify when an event related to screen mirror occurs.
66          *
67          * @param info Change info of screen mirror.
68          */
OnMirrorChange([[maybe_unused]]const ChangeInfo& info)69         virtual void OnMirrorChange([[maybe_unused]]const ChangeInfo& info) {}
70     };
71 
72     /**
73      * @brief Get the screen object by screen id.
74      *
75      * @param screenId Screen id.
76      * @return Screen object.
77      */
78     sptr<Screen> GetScreenById(ScreenId screenId);
79 
80     /**
81      * @brief Get the screen group object by groupId.
82      *
83      * @param groupId Screen group id.
84      * @return ScreenGroup object.
85      */
86     sptr<ScreenGroup> GetScreenGroup(ScreenId groupId);
87 
88     /**
89      * @brief Get all screens object.
90      *
91      * @param screens All screen objects.
92      * @return DM_OK means get success, others means get failed.
93      */
94     DMError GetAllScreens(std::vector<sptr<Screen>>& screens);
95 
96     /**
97      * @brief Make screens as expand-screen.
98      *
99      * @param options Option of expand.
100      * @param screenGroupId Screen group id.
101      * @return DM_OK means make expand success, others means make expand failed.
102      */
103     DMError MakeExpand(const std::vector<ExpandOption>& options, ScreenId& screenGroupId);
104 
105     /**
106      * @brief Make screens as mirror-screen
107      *
108      * @param mainScreenId Main screen id.
109      * @param mirrorScreenId Mirror screen ids.
110      * @param screenGroupId Screen group id.
111      * @return DM_OK means make mirror success, others means make mirror failed.
112      */
113     DMError MakeMirror(ScreenId mainScreenId, std::vector<ScreenId> mirrorScreenId, ScreenId& screenGroupId);
114 
115      /**
116      * @brief Make screens as mirror-screen
117      *
118      * @param mainScreenId Main screen id.
119      * @param secondaryScreenId secondary screen id.
120      * @param screenMode Screen Combination Mode.
121      * @return DM_OK means make mirror success, others means make mirror failed.
122      */
123     DMError SetMultiScreenMode(ScreenId mainScreenId, ScreenId secondaryScreenId,
124         MultiScreenMode screenMode);
125 
126      /**
127      * @brief Set Screen Relative Position
128      *
129      * @param mainScreenOptions Main screen id and position.
130      * @param secondScreenOption secondary screen id and position.
131      * @return DM_OK means make mirror success, others means make mirror failed.
132      */
133     DMError SetMultiScreenRelativePosition(MultiScreenPositionOptions mainScreenOptions,
134         MultiScreenPositionOptions secondScreenOption);
135 
136     /**
137     * @brief Make screens as unique-screen.
138     *
139     * @param screenIds Unique screen ids.
140     * @return DM_OK means make unique screen success, others means make unique failed.
141     */
142     DMError MakeUniqueScreen(const std::vector<ScreenId>& screenIds);
143 
144     /**
145      * @brief Stop expand screens.
146      *
147      * @param expandScreenIds Expand screen ids.
148      * @return DM_OK means stop expand success, others means stop expand failed.
149      */
150     DMError StopExpand(const std::vector<ScreenId>& expandScreenIds);
151 
152     /**
153      * @brief Stop mirror screens.
154      *
155      * @param mirrorScreenIds Mirror screen ids.
156      * @return DM_OK means stop mirror success, others means stop mirror failed.
157      */
158     DMError StopMirror(const std::vector<ScreenId>& mirrorScreenIds);
159 
160     /**
161      * @brief Disable/enable global mirror screen.
162      *
163      * @param disableOrNot disable mirror screen or not.
164      * @return DM_OK means disable or enable mirror success, others means failed.
165      */
166     DMError DisableMirror(bool disableOrNot);
167 
168     /**
169      * @brief Remove virtual screen from group.
170      *
171      * @param screens Screen ids.
172      * @return DM_OK means remove success, others means remove failed.
173      */
174     DMError RemoveVirtualScreenFromGroup(std::vector<ScreenId> screens);
175 
176     /**
177      * @brief Create virtual screen.
178      *
179      * @param option Indicates the options of the virtual screen.
180      * @return Screen id.
181      */
182     ScreenId CreateVirtualScreen(VirtualScreenOption option);
183 
184     /**
185      * @brief Destroy virtual screen.
186      *
187      * @param screenId Indicates the screen id of the virtual screen.
188      * @return DM_OK means destroy success, others means destroy failed.
189      */
190     DMError DestroyVirtualScreen(ScreenId screenId);
191 
192     /**
193      * @brief Set surface for the virtual screen.
194      *
195      * @param screenId Screen id.
196      * @param surface Surface object.
197      * @return DM_OK means set success, others means set failed.
198      */
199     DMError SetVirtualScreenSurface(ScreenId screenId, sptr<Surface> surface);
200 
201     /**
202      * @brief Resize virtual screen
203      *
204      * @param screenId the id of virtual screen to be resized.
205      * @param width the new width.
206      * @param height the new height.
207      * @return DM_OK means set success, others means set failed.
208      */
209     DMError ResizeVirtualScreen(ScreenId screenId, uint32_t width, uint32_t height);
210 
211     /**
212      * @brief Set buffer auto rotate
213      *
214      * @param screenId Screen id.
215      * @param bufferRotation auto rotate
216      * @return DM_OK means set success, others means set failed.
217      */
218     DMError SetVirtualMirrorScreenCanvasRotation(ScreenId screenId, bool canvasRotation);
219 
220     /**
221      * @brief Set virtual screen scale mode
222      *
223      * @param screenId Screen id.
224      * @param scaleMode scale mode
225      * @return DM_OK means set success, others means set failed.
226      */
227     DMError SetVirtualMirrorScreenScaleMode(ScreenId screenId, ScreenScaleMode scaleMode);
228 
229     /**
230      * @brief Set the screen power state on the specified screen.
231      *
232      * @param screenId Screen id.
233      * @param state Screen power state.
234      * @param reason Reason for power state change.
235      * @return True means set success, false means set failed.
236      */
237     bool SetSpecifiedScreenPower(ScreenId screenId, ScreenPowerState state, PowerStateChangeReason reason);
238 
239     /**
240      * @brief Set the screen power states for all screens.
241      *
242      * @param state Screen power state.
243      * @param reason Reason for power state change.
244      * @return True means set success, false means set failed.
245      */
246     bool SetScreenPowerForAll(ScreenPowerState state, PowerStateChangeReason reason);
247 
248     /**
249      * @brief Get screen power state.
250      *
251      * @param screenId Screen id.
252      * @return Power state of screen.
253      */
254     ScreenPowerState GetScreenPower(ScreenId screenId);
255 
256     /**
257      * @brief Set screen rotation lock status.
258      *
259      * @param isLocked True means forbid to rotate screen, false means the opposite.
260      * @return DM_OK means set success, others means set failed.
261      */
262     DMError SetScreenRotationLocked(bool isLocked);
263 
264     /**
265      * @brief Set screen rotation lock status from js.
266      *
267      * @param isLocked True means forbid to rotate screen, false means the opposite.
268      * @return DM_OK means set success, others means set failed.
269      */
270     DMError SetScreenRotationLockedFromJs(bool isLocked);
271 
272     /**
273      * @brief Get screen rotation lock status.
274      *
275      * @param isLocked Query the rotation lock status.
276      * @return DM_OK means query success, others means query failed.
277      */
278     DMError IsScreenRotationLocked(bool& isLocked);
279 
280     /**
281      * @brief Register screen listener.
282      *
283      * @param listener IScreenListener.
284      * @return DM_OK means register success, others means register failed.
285      */
286     DMError RegisterScreenListener(sptr<IScreenListener> listener);
287 
288     /**
289      * @brief Unregister screen listener.
290      *
291      * @param listener IScreenListener.
292      * @return DM_OK means unregister success, others means unregister failed.
293      */
294     DMError UnregisterScreenListener(sptr<IScreenListener> listener);
295 
296     /**
297      * @brief Register screen group listener.
298      *
299      * @param listener IScreenGroupListener.
300      * @return DM_OK means register success, others means register failed.
301      */
302     DMError RegisterScreenGroupListener(sptr<IScreenGroupListener> listener);
303 
304     /**
305      * @brief Unregister screen group listener.
306      *
307      * @param listener IScreenGroupListener.
308      * @return DM_OK means unregister success, others means unregister failed.
309      */
310     DMError UnregisterScreenGroupListener(sptr<IScreenGroupListener> listener);
311 
312     /**
313      * @brief Register virtual screen group listener.
314      *
315      * @param listener IVirtualScreenGroupListener.
316      * @return DM_OK means register success, others means register failed.
317      */
318     DMError RegisterVirtualScreenGroupListener(sptr<IVirtualScreenGroupListener> listener);
319 
320     /**
321      * @brief Unregister virtual screen group listener.
322      *
323      * @param listener IVirtualScreenGroupListener.
324      * @return DM_OK means unregister success, others means unregister failed.
325      */
326     DMError UnregisterVirtualScreenGroupListener(sptr<IVirtualScreenGroupListener> listener);
327 
328     /**
329      * @brief Get virtual screen flag.
330      *
331      * @param screenId virtual screen id.
332      * @return virtual screen flag
333      */
334     VirtualScreenFlag GetVirtualScreenFlag(ScreenId screenId);
335 
336     /**
337      * @brief Set virtual screen flag.
338      *
339      * @param screenId virtual screen id.
340      * @param screenFlag virtual screen flag.
341      * @return DM_OK means set success, others means failed.
342      */
343     DMError SetVirtualScreenFlag(ScreenId screenId, VirtualScreenFlag screenFlag);
344 
345     /**
346      * @brief Set virtual screen refresh rate with a refresh interval relative to the main screen.
347      *
348      * @param screenId virtual screen id.
349      * @param refreshInterval refresh interval: range [1, main screen max refresh rate]. 2 indicates that
350      *     the virtual screen refreshes 1 frame when the main screen refreshes 2 frames, 5 indicates that
351      *     the virtual screen refreshes 1 frame when the main screen refreshes 5 frames, and so on.
352      * @return DM_OK means set success, others means failed.
353      */
354     DMError SetVirtualScreenRefreshRate(ScreenId screenId, uint32_t refreshInterval);
355 
356     /**
357      * @brief Set virtual screen max refresh rate .
358      *
359      * @param screenId virtual screen id.
360      * @param refreshRate max refresh rate to be set.
361      * @param actualRefreshRate the actual refresh rate that is finally set.
362      * @return DM_OK means set success, others means failed.
363      */
364     DMError SetVirtualScreenMaxRefreshRate(ScreenId id, uint32_t refreshRate,
365         uint32_t& actualRefreshRate);
366 
367     /**
368      * @brief when casting the screen, Virtual screen playback and pause function.
369      *
370      * @param screenId screenId used in virtual screen.
371      * @param screenStatus VIRTUAL_SCREEN_PLAY or VIRTUAL_SCREEN_PAUSE.
372      * @return True means set success, false means set failed.
373      */
374     bool SetVirtualScreenStatus(ScreenId screenId, VirtualScreenStatus screenStatus);
375 private:
376     ScreenManager();
377     ~ScreenManager();
378     void OnRemoteDied();
379 
380     class Impl;
381     sptr<Impl> pImpl_;
382 };
383 } // namespace OHOS::Rosen
384 
385 #endif // FOUNDATION_DM_SCREEN_MANAGER_H