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 HGM_SCREEN_H
17 #define HGM_SCREEN_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <mutex>
22 #include <unistd.h>
23 #include <unordered_map>
24 #include <unordered_set>
25 #include <vector>
26 
27 #include "refbase.h"
28 #include "screen_manager/screen_types.h"
29 #include "screen_manager/rs_screen_mode_info.h"
30 
31 #include "hgm_command.h"
32 
33 namespace OHOS::Rosen {
34 constexpr float INCH_2_MM = 25.4f;
35 
36 struct ScreenSize {
37     int32_t width;
38     int32_t height;
39     int32_t phyWidth;
40     int32_t phyHeight;
41 };
42 
43 class HgmScreen : public virtual RefBase {
44 public:
45     HgmScreen();
46     HgmScreen(ScreenId id, int32_t mode, ScreenSize& screenSize);
47     virtual ~HgmScreen();
48 
GetId()49     ScreenId GetId() const
50     {
51         return id_;
52     }
53 
GetActiveMode()54     uint32_t GetActiveMode() const
55     {
56         return activeModeId_;
57     }
58 
GetSupportedRates()59     std::unordered_set<uint32_t> GetSupportedRates() const
60     {
61         return supportedRefreshRates_;
62     }
63 
IsSupportRate(uint32_t rate)64     bool IsSupportRate(uint32_t rate) const
65     {
66         return supportedRefreshRates_.find(rate) != supportedRefreshRates_.end() ? true : false;
67     }
68 
GetActiveRefreshRateMode()69     int32_t GetActiveRefreshRateMode() const
70     {
71         return customFrameRateMode_;
72     }
73 
GetWidth()74     int32_t GetWidth() const
75     {
76         return width_;
77     }
78 
GetHeight()79     int32_t GetHeight() const
80     {
81         return height_;
82     }
83 
GetPhyWidth()84     int32_t GetPhyWidth() const
85     {
86         return phyWidth_;
87     }
88 
GetPhyHeight()89     int32_t GetPhyHeight() const
90     {
91         return phyHeight_;
92     }
93 
GetPpi()94     float GetPpi() const
95     {
96         return ppi_;
97     }
98 
GetXDpi()99     float GetXDpi() const
100     {
101         return xDpi_;
102     }
103 
GetYDpi()104     float GetYDpi() const
105     {
106         return yDpi_;
107     }
108 
109     uint32_t GetActiveRefreshRate() const;
110     int32_t SetActiveRefreshRate(int32_t sceneId, uint32_t rate);
111     int32_t SetRateAndResolution(int32_t sceneId, uint32_t rate, int32_t width, int32_t height);
112     int32_t SetRefreshRateRange(uint32_t minRate, uint32_t maxRate);
113     int32_t AddScreenModeInfo(int32_t width, int32_t height, uint32_t rate, int32_t modeId);
114 
115 private:
116     class ScreenProfile {
117     public:
ScreenProfile(int32_t width,int32_t height,uint32_t rate,int32_t modeId)118         ScreenProfile(int32_t width, int32_t height, uint32_t rate, int32_t modeId)
119             : width_(width), height_(height), refreshrate_(rate), modeId_(modeId) {}
120 
121         ~ScreenProfile() = default;
122 
123         bool operator==(const ScreenProfile& rValue) const
124         {
125             if (rValue.GetWidth() != width_ || rValue.GetHeight() != height_ ||
126                 rValue.GetRate() != refreshrate_) {
127                 return false;
128             }
129             return true;
130         }
131 
GetWidth()132         int32_t GetWidth() const
133         {
134             return width_;
135         }
136 
GetHeight()137         int32_t GetHeight() const
138         {
139             return height_;
140         }
141 
GetRate()142         uint32_t GetRate() const
143         {
144             return refreshrate_;
145         }
146 
GetModeId()147         int32_t GetModeId() const
148         {
149             return modeId_;
150         }
151 
152     private:
153         int32_t width_ = -1;
154         int32_t height_ = -1;
155         uint32_t refreshrate_ = 0;
156         int32_t modeId_ = -1;
157     };
158 
159     ScreenId id_ = 0;
160     int32_t activeModeId_ = 0;
161     int32_t width_ = 0;
162     int32_t height_ = 0;
163     int32_t phyWidth_ = 0;
164     int32_t phyHeight_ = 0;
165     float ppi_ = 0;
166     float xDpi_ = 0;
167     float yDpi_ = 0;
168     int32_t customFrameRateMode_ = -1;
169     std::unordered_set<uint32_t> supportedRefreshRates_;
170     std::unordered_set<int32_t> supportedModeIds_;
171     std::vector<std::shared_ptr<ScreenProfile>> screenModeInfos_;
172     std::mutex baseMutex_;
173 
174     void SetActiveModeId(int32_t modeId);
175     std::shared_ptr<ScreenProfile> GetModeViaId(int32_t id) const;
176     bool IfSwitchToRate(int32_t sceneId, uint32_t rate) const;
177     int32_t GetModeIdViaRate(uint32_t rate) const;
178     int32_t GetModeIdViaResolutionAndRate(int32_t width, int32_t height, uint32_t rate) const;
179     static constexpr uint32_t RATE_NOT_SUPPORTED = 0;
180 
181     // defined by xml configuration, participate in refresh rate decision system
182     uint32_t minRefreshRateRange_ = 1;
183     uint32_t maxRefreshRateRange_ = 120;
184 };
185 } // namespace Ohos::Rosen
186 #endif // HGM_SCREEN_H