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 GetSelfOwnedScreenFlag()109 bool GetSelfOwnedScreenFlag() const 110 { 111 return isSelfOwnedScreenFlag_.load(); 112 } 113 SetSelfOwnedScreenFlag(bool isSelfOwnedScreen)114 void SetSelfOwnedScreenFlag(bool isSelfOwnedScreen) 115 { 116 isSelfOwnedScreenFlag_.store(isSelfOwnedScreen); 117 } 118 119 uint32_t GetActiveRefreshRate() const; 120 int32_t SetActiveRefreshRate(int32_t sceneId, uint32_t rate); 121 int32_t SetRateAndResolution(int32_t sceneId, uint32_t rate, int32_t width, int32_t height); 122 int32_t SetRefreshRateRange(uint32_t minRate, uint32_t maxRate); 123 int32_t AddScreenModeInfo(int32_t width, int32_t height, uint32_t rate, int32_t modeId); 124 125 private: 126 class ScreenProfile { 127 public: ScreenProfile(int32_t width,int32_t height,uint32_t rate,int32_t modeId)128 ScreenProfile(int32_t width, int32_t height, uint32_t rate, int32_t modeId) 129 : width_(width), height_(height), refreshrate_(rate), modeId_(modeId) {} 130 131 ~ScreenProfile() = default; 132 133 bool operator==(const ScreenProfile& rValue) const 134 { 135 if (rValue.GetWidth() != width_ || rValue.GetHeight() != height_ || 136 rValue.GetRate() != refreshrate_) { 137 return false; 138 } 139 return true; 140 } 141 GetWidth()142 int32_t GetWidth() const 143 { 144 return width_; 145 } 146 GetHeight()147 int32_t GetHeight() const 148 { 149 return height_; 150 } 151 GetRate()152 uint32_t GetRate() const 153 { 154 return refreshrate_; 155 } 156 GetModeId()157 int32_t GetModeId() const 158 { 159 return modeId_; 160 } 161 162 private: 163 int32_t width_ = -1; 164 int32_t height_ = -1; 165 uint32_t refreshrate_ = 0; 166 int32_t modeId_ = -1; 167 }; 168 169 ScreenId id_ = 0; 170 int32_t activeModeId_ = 0; 171 int32_t width_ = 0; 172 int32_t height_ = 0; 173 int32_t phyWidth_ = 0; 174 int32_t phyHeight_ = 0; 175 float ppi_ = 0; 176 float xDpi_ = 0; 177 float yDpi_ = 0; 178 int32_t customFrameRateMode_ = -1; 179 std::atomic<bool> isSelfOwnedScreenFlag_{ false }; 180 std::unordered_set<uint32_t> supportedRefreshRates_; 181 std::unordered_set<int32_t> supportedModeIds_; 182 std::vector<std::shared_ptr<ScreenProfile>> screenModeInfos_; 183 std::mutex baseMutex_; 184 185 void SetActiveModeId(int32_t modeId); 186 std::shared_ptr<ScreenProfile> GetModeViaId(int32_t id) const; 187 bool IfSwitchToRate(int32_t sceneId, uint32_t rate) const; 188 int32_t GetModeIdViaRate(uint32_t rate) const; 189 int32_t GetModeIdViaResolutionAndRate(int32_t width, int32_t height, uint32_t rate) const; 190 static constexpr uint32_t RATE_NOT_SUPPORTED = 0; 191 192 // defined by xml configuration, participate in refresh rate decision system 193 uint32_t minRefreshRateRange_ = 1; 194 uint32_t maxRefreshRateRange_ = 120; 195 }; 196 } // namespace Ohos::Rosen 197 #endif // HGM_SCREEN_H