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_H
17 #define FOUNDATION_DMSERVER_ABSTRACT_SCREEN_H
18 
19 
20 #include <vector>
21 #include <map>
22 #include <refbase.h>
23 #include <screen_manager/screen_types.h>
24 #include <ui/rs_display_node.h>
25 #include <ui/rs_surface_node.h>
26 
27 #include "noncopyable.h"
28 #include "screen.h"
29 #include "screen_group.h"
30 #include "screen_group_info.h"
31 #include "screen_info.h"
32 
33 namespace OHOS::Rosen {
34 class AbstractScreenGroup;
35 class AbstractScreenController;
36 class AbstractScreen : public RefBase {
37 public:
38     AbstractScreen(sptr<AbstractScreenController>, const std::string& name, ScreenId dmsId, ScreenId rsId);
39     AbstractScreen() = delete;
40     WM_DISALLOW_COPY_AND_MOVE(AbstractScreen);
41     ~AbstractScreen();
42     sptr<SupportedScreenModes> GetActiveScreenMode() const;
43     std::vector<sptr<SupportedScreenModes>> GetAbstractScreenModes() const;
44     sptr<AbstractScreenGroup> GetGroup() const;
45     sptr<ScreenInfo> ConvertToScreenInfo() const;
46     bool SetOrientation(Orientation orientation);
47     Rotation CalcRotation(Orientation orientation) const;
48     bool SetVirtualPixelRatio(float virtualPixelRatio);
49     float GetVirtualPixelRatio() const;
50     bool SetSourceMode(ScreenSourceMode sourceMode);
51     ScreenSourceMode GetSourceMode() const;
52 
53     void UpdateRSTree(std::shared_ptr<RSSurfaceNode>& surfaceNode, bool isAdd, bool needToUpdate = true);
54     DMError AddSurfaceNode(std::shared_ptr<RSSurfaceNode>& surfaceNode, bool onTop, bool needToRecord = true);
55     DMError RemoveSurfaceNode(std::shared_ptr<RSSurfaceNode>& surfaceNode);
56     void UpdateDisplayGroupRSTree(std::shared_ptr<RSSurfaceNode>& surfaceNode, NodeId parentNodeId, bool isAdd);
57     void InitRSDisplayNode(const RSDisplayNodeConfig& config, const Point& startPoint);
58     void InitRSDefaultDisplayNode(const RSDisplayNodeConfig& config, const Point& startPoint);
59     void UpdateRSDisplayNode(Point startPoint);
60     ScreenId GetScreenGroupId() const;
61 
62     // colorspace, gamut
63     DMError GetScreenSupportedColorGamuts(std::vector<ScreenColorGamut>& colorGamuts);
64     DMError GetScreenColorGamut(ScreenColorGamut& colorGamut);
65     DMError SetScreenColorGamut(int32_t colorGamutIdx);
66     DMError GetScreenGamutMap(ScreenGamutMap& gamutMap);
67     DMError SetScreenGamutMap(ScreenGamutMap gamutMap);
68     DMError SetScreenColorTransform();
69     const std::string& GetScreenName() const;
70     void SetPhyWidth(uint32_t phyWidth);
71     void SetPhyHeight(uint32_t phyHeight);
72     uint32_t GetPhyWidth() const;
73     uint32_t GetPhyHeight() const;
74 
75     const ScreenId dmsId_;
76     const ScreenId rsId_;
77     bool isScreenGroup_ { false };
78     std::shared_ptr<RSDisplayNode> rsDisplayNode_;
79     RSDisplayNodeConfig rSDisplayNodeConfig_;
80     Point startPoint_{};
81     ScreenId groupDmsId_ { SCREEN_ID_INVALID };
82     ScreenId lastGroupDmsId_ { SCREEN_ID_INVALID };
83     ScreenType type_ { ScreenType::REAL };
84     int32_t activeIdx_ { 0 };
85     std::vector<sptr<SupportedScreenModes>> modes_ = {};
86     float virtualPixelRatio_ = { 1.0 };
87     Orientation orientation_ { Orientation::UNSPECIFIED };
88     Rotation rotation_ { Rotation::ROTATION_0 };
89     Orientation screenRequestedOrientation_ { Orientation::UNSPECIFIED };
90     // nativeSurfaceNodes_ means th node which is added/removed by interface of dms directly
91     std::vector<std::shared_ptr<RSSurfaceNode>> nativeSurfaceNodes_;
92     // appSurfaceNodes_ means th node which is added/removed by interface of wms
93     std::vector<std::shared_ptr<RSSurfaceNode>> appSurfaceNodes_;
94 
95 protected:
96     void FillScreenInfo(sptr<ScreenInfo>) const;
97     const sptr<AbstractScreenController> screenController_;
98 
99 private:
100     void SetPropertyForDisplayNode(const std::shared_ptr<RSDisplayNode>& rsDisplayNode,
101         const RSDisplayNodeConfig& config, const Point& startPoint);
102     std::string name_ { "UNKNOWN" };
103     uint32_t phyWidth_ { UINT32_MAX };
104     uint32_t phyHeight_ { UINT32_MAX };
105     mutable std::recursive_mutex mutex_;
106 };
107 
108 class AbstractScreenGroup : public AbstractScreen {
109 public:
110     AbstractScreenGroup(sptr<AbstractScreenController>, ScreenId dmsId, ScreenId rsId, std::string name,
111         ScreenCombination combination);
112     AbstractScreenGroup() = delete;
113     WM_DISALLOW_COPY_AND_MOVE(AbstractScreenGroup);
114     ~AbstractScreenGroup();
115 
116     bool AddChild(sptr<AbstractScreen>& dmsScreen, Point& startPoint);
117     bool AddChildren(std::vector<sptr<AbstractScreen>>& dmsScreens, std::vector<Point>& startPoints);
118     bool RemoveChild(sptr<AbstractScreen>& dmsScreen);
119     bool RemoveDefaultScreen(const sptr<AbstractScreen>& dmsScreen);
120     bool HasChild(ScreenId childScreen) const;
121     std::vector<sptr<AbstractScreen>> GetChildren() const;
122     std::vector<Point> GetChildrenPosition() const;
123     Point GetChildPosition(ScreenId screenId) const;
124     size_t GetChildCount() const;
125     sptr<ScreenGroupInfo> ConvertToScreenGroupInfo() const;
126     ScreenCombination GetScreenCombination() const;
127 
128     ScreenCombination combination_ { ScreenCombination::SCREEN_ALONE };
129     ScreenId mirrorScreenId_ { SCREEN_ID_INVALID };
130     ScreenId defaultScreenId_ { SCREEN_ID_INVALID };
131 
132 private:
133     bool GetRSDisplayNodeConfig(sptr<AbstractScreen>& dmsScreen, struct RSDisplayNodeConfig& config);
134 
135     std::map<ScreenId, sptr<AbstractScreen>> screenMap_;
136 };
137 } // namespace OHOS::Rosen
138 #endif // FOUNDATION_DMSERVER_ABSTRACT_SCREEN_H