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 RS_SCREEN_MANAGER 17 #define RS_SCREEN_MANAGER 18 19 #include <cstdint> 20 #include <memory> 21 #include <mutex> 22 #include <queue> 23 #include <unordered_map> 24 25 #include <hdi_backend.h> 26 #include <ipc_callbacks/screen_change_callback.h> 27 #include <refbase.h> 28 #include <screen_manager/rs_screen_props.h> 29 #include <screen_manager/rs_screen_mode_info.h> 30 #include <screen_manager/rs_screen_capability.h> 31 #include <screen_manager/rs_screen_data.h> 32 #include <screen_manager/rs_screen_hdr_capability.h> 33 #include <screen_manager/screen_types.h> 34 #include <screen_manager/rs_virtual_screen_resolution.h> 35 #include <surface.h> 36 37 #include "rs_screen.h" 38 39 namespace OHOS { 40 namespace Rosen { 41 enum class ScreenState : uint8_t { 42 HDI_OUTPUT_ENABLE, 43 PRODUCER_SURFACE_ENABLE, 44 DISABLED, 45 NOT_EXISTED, 46 UNKNOWN, 47 }; 48 49 struct ScreenInfo { 50 ScreenId id = INVALID_SCREEN_ID; 51 uint32_t width = 0; 52 uint32_t height = 0; 53 ScreenColorGamut colorGamut = ScreenColorGamut::COLOR_GAMUT_SRGB; 54 ScreenState state = ScreenState::UNKNOWN; 55 ScreenRotation rotation = ScreenRotation::ROTATION_0; 56 57 uint32_t skipFrameInterval = DEFAULT_SKIP_FRAME_INTERVAL; // skip frame interval for change screen refresh rate 58 GetRotatedWidthScreenInfo59 uint32_t GetRotatedWidth() const 60 { 61 return (rotation == ScreenRotation::ROTATION_0 || rotation == ScreenRotation::ROTATION_180) ? width : height; 62 } 63 GetRotatedHeightScreenInfo64 uint32_t GetRotatedHeight() const 65 { 66 return (rotation == ScreenRotation::ROTATION_0 || rotation == ScreenRotation::ROTATION_180) ? height : width; 67 } 68 }; 69 70 class RSScreenManager : public RefBase { 71 public: 72 RSScreenManager() = default; 73 virtual ~RSScreenManager() noexcept = default; 74 75 virtual bool Init() noexcept = 0; 76 77 // get default/primary screen id. 78 virtual ScreenId GetDefaultScreenId() const = 0; 79 80 virtual std::vector<ScreenId> GetAllScreenIds() = 0; 81 82 virtual void SetDefaultScreenId(ScreenId id) = 0; 83 84 virtual void SetScreenMirror(ScreenId id, ScreenId toMirror) = 0; 85 86 virtual ScreenId CreateVirtualScreen( 87 const std::string &name, 88 uint32_t width, 89 uint32_t height, 90 sptr<Surface> surface, 91 ScreenId mirrorId = 0, 92 int flags = 0) = 0; 93 94 virtual int32_t SetVirtualScreenSurface(ScreenId id, sptr<Surface> surface) = 0; 95 96 virtual void RemoveVirtualScreen(ScreenId id) = 0; 97 98 virtual void SetScreenActiveMode(ScreenId id, uint32_t modeId) = 0; 99 100 virtual int32_t SetVirtualScreenResolution(ScreenId id, uint32_t width, uint32_t height) = 0; 101 102 virtual void SetScreenPowerStatus(ScreenId id, ScreenPowerStatus status) = 0; 103 104 virtual void GetVirtualScreenResolution(ScreenId id, RSVirtualScreenResolution& virtualScreenResolution) const = 0; 105 106 virtual void GetScreenActiveMode(ScreenId id, RSScreenModeInfo& screenModeInfo) const = 0; 107 108 virtual std::vector<RSScreenModeInfo> GetScreenSupportedModes(ScreenId id) const = 0; 109 110 virtual RSScreenCapability GetScreenCapability(ScreenId id) const = 0; 111 112 virtual ScreenPowerStatus GetScreenPowerStatus(ScreenId id) const = 0; 113 114 virtual RSScreenData GetScreenData(ScreenId id) const = 0; 115 116 virtual ScreenInfo QueryScreenInfo(ScreenId id) const = 0; 117 118 // Can only be called after QueryScreenState and the state is ScreenState::PRODUCER_SURFACE_ENABLE; 119 virtual sptr<Surface> GetProducerSurface(ScreenId id) const = 0; 120 121 // Can only be called after QueryScreenState and the state is ScreenState::HDI_OUTPUT_ENABLE; 122 virtual std::shared_ptr<HdiOutput> GetOutput(ScreenId id) const = 0; 123 124 virtual int32_t AddScreenChangeCallback(const sptr<RSIScreenChangeCallback> &callback) = 0; 125 126 virtual void RemoveScreenChangeCallback(const sptr<RSIScreenChangeCallback> &callback) = 0; 127 128 virtual void ProcessScreenHotPlugEvents() = 0; 129 130 virtual void DisplayDump(std::string& dumpString) = 0; 131 132 virtual void SurfaceDump(std::string& dumpString) = 0; 133 134 virtual void FpsDump(std::string& dumpString, std::string& arg) = 0; 135 136 virtual void ClearFpsDump(std::string& dumpString, std::string& arg) = 0; 137 138 virtual int32_t GetScreenBacklight(ScreenId id) = 0; 139 140 virtual void SetScreenBacklight(ScreenId id, uint32_t level) = 0; 141 142 virtual int32_t GetScreenSupportedColorGamuts(ScreenId id, std::vector<ScreenColorGamut>& mode) const = 0; 143 144 virtual int32_t GetScreenSupportedMetaDataKeys(ScreenId id, std::vector<ScreenHDRMetadataKey>& keys) const = 0; 145 146 virtual int32_t GetScreenColorGamut(ScreenId id, ScreenColorGamut& mode) const = 0; 147 148 virtual int32_t SetScreenColorGamut(ScreenId id, int32_t modeIdx) = 0; 149 150 virtual int32_t SetScreenGamutMap(ScreenId id, ScreenGamutMap mode) = 0; 151 152 virtual int32_t GetScreenGamutMap(ScreenId id, ScreenGamutMap& mode) const = 0; 153 154 virtual int32_t GetScreenHDRCapability(ScreenId id, RSScreenHDRCapability& screenHdrCapability) const = 0; 155 156 virtual int32_t GetScreenType(ScreenId id, RSScreenType& type) const = 0; 157 158 virtual int32_t SetScreenSkipFrameInterval(ScreenId id, uint32_t skipFrameInterval) = 0; 159 160 /* only used for mock tests */ 161 virtual void MockHdiScreenConnected(std::unique_ptr<impl::RSScreen>& rsScreen) = 0; 162 }; 163 164 sptr<RSScreenManager> CreateOrGetScreenManager(); 165 166 namespace impl { 167 struct ScreenHotPlugEvent { 168 std::shared_ptr<HdiOutput> output; 169 bool connected = false; 170 }; 171 172 class RSScreenManager : public OHOS::Rosen::RSScreenManager { 173 public: 174 static sptr<OHOS::Rosen::RSScreenManager> GetInstance() noexcept; 175 176 // noncopyable 177 RSScreenManager(const RSScreenManager &) = delete; 178 RSScreenManager &operator=(const RSScreenManager &) = delete; 179 180 bool Init() noexcept override; 181 GetDefaultScreenId()182 ScreenId GetDefaultScreenId() const override 183 { 184 return defaultScreenId_; 185 } 186 187 std::vector<ScreenId> GetAllScreenIds() override; 188 189 void SetDefaultScreenId(ScreenId id) override; 190 191 void SetScreenMirror(ScreenId id, ScreenId toMirror) override; 192 193 ScreenId CreateVirtualScreen( 194 const std::string &name, 195 uint32_t width, 196 uint32_t height, 197 sptr<Surface> surface, 198 ScreenId mirrorId, 199 int32_t flags) override; 200 201 int32_t SetVirtualScreenSurface(ScreenId id, sptr<Surface> surface) override; 202 203 void RemoveVirtualScreen(ScreenId id) override; 204 205 void SetScreenActiveMode(ScreenId id, uint32_t modeId) override; 206 207 int32_t SetVirtualScreenResolution(ScreenId id, uint32_t width, uint32_t height) override; 208 209 void SetScreenPowerStatus(ScreenId id, ScreenPowerStatus status) override; 210 211 void GetVirtualScreenResolution(ScreenId id, RSVirtualScreenResolution& virtualScreenResolution) const override; 212 213 void GetScreenActiveMode(ScreenId id, RSScreenModeInfo& screenModeInfo) const override; 214 215 std::vector<RSScreenModeInfo> GetScreenSupportedModes(ScreenId id) const override; 216 217 RSScreenCapability GetScreenCapability(ScreenId id) const override; 218 219 ScreenPowerStatus GetScreenPowerStatus(ScreenId id) const override; 220 221 RSScreenData GetScreenData(ScreenId id) const override; 222 223 ScreenInfo QueryScreenInfo(ScreenId id) const override; 224 225 sptr<Surface> GetProducerSurface(ScreenId id) const override; 226 227 std::shared_ptr<HdiOutput> GetOutput(ScreenId id) const override; 228 229 int32_t AddScreenChangeCallback(const sptr<RSIScreenChangeCallback> &callback) override; 230 231 void RemoveScreenChangeCallback(const sptr<RSIScreenChangeCallback> &callback) override; 232 233 void ProcessScreenHotPlugEvents() override; 234 235 void DisplayDump(std::string& dumpString) override; 236 237 void SurfaceDump(std::string& dumpString) override; 238 239 void FpsDump(std::string& dumpString, std::string& arg) override; 240 241 void ClearFpsDump(std::string& dumpString, std::string& arg) override; 242 243 int32_t GetScreenBacklight(ScreenId id) override; 244 245 void SetScreenBacklight(ScreenId id, uint32_t level) override; 246 247 int32_t GetScreenSupportedColorGamuts(ScreenId id, std::vector<ScreenColorGamut>& mode) const override; 248 249 int32_t GetScreenSupportedMetaDataKeys(ScreenId id, std::vector<ScreenHDRMetadataKey>& keys) const override; 250 251 int32_t GetScreenColorGamut(ScreenId id, ScreenColorGamut& mode) const override; 252 253 int32_t SetScreenColorGamut(ScreenId id, int32_t modeIdx) override; 254 255 int32_t SetScreenGamutMap(ScreenId id, ScreenGamutMap mode) override; 256 257 int32_t GetScreenGamutMap(ScreenId id, ScreenGamutMap& mode) const override; 258 259 int32_t GetScreenHDRCapability(ScreenId id, RSScreenHDRCapability& screenHdrCapability) const override; 260 261 int32_t GetScreenType(ScreenId id, RSScreenType& type) const override; 262 263 int32_t SetScreenSkipFrameInterval(ScreenId id, uint32_t skipFrameInterval) override; 264 265 /* only used for mock tests */ MockHdiScreenConnected(std::unique_ptr<impl::RSScreen> & rsScreen)266 void MockHdiScreenConnected(std::unique_ptr<impl::RSScreen>& rsScreen) override 267 { 268 if (rsScreen == nullptr) { 269 return; 270 } 271 screens_[rsScreen->Id()] = std::move(rsScreen); 272 } 273 private: 274 RSScreenManager(); 275 ~RSScreenManager() noexcept override; 276 277 static void OnHotPlug(std::shared_ptr<HdiOutput> &output, bool connected, void *data); 278 void OnHotPlugEvent(std::shared_ptr<HdiOutput> &output, bool connected); 279 void ProcessScreenConnectedLocked(std::shared_ptr<HdiOutput> &output); 280 void ProcessScreenDisConnectedLocked(std::shared_ptr<HdiOutput> &output); 281 void HandleDefaultScreenDisConnectedLocked(); 282 std::vector<ScreenHotPlugEvent> pendingHotPlugEvents_; 283 284 void GetVirtualScreenResolutionLocked(ScreenId id, RSVirtualScreenResolution& virtualScreenResolution) const; 285 void GetScreenActiveModeLocked(ScreenId id, RSScreenModeInfo& screenModeInfo) const; 286 std::vector<RSScreenModeInfo> GetScreenSupportedModesLocked(ScreenId id) const; 287 RSScreenCapability GetScreenCapabilityLocked(ScreenId id) const; 288 ScreenPowerStatus GetScreenPowerStatusLocked(ScreenId id) const; 289 int32_t GetScreenBacklightLocked(ScreenId id) const; 290 291 void RemoveVirtualScreenLocked(ScreenId id); 292 ScreenId GenerateVirtualScreenIdLocked(); 293 void ReuseVirtualScreenIdLocked(ScreenId id); 294 void MirrorChangeDefaultScreenResolution(ScreenId id, uint32_t width, uint32_t height); 295 ScreenId GetMirrorScreenId(ScreenId id); 296 297 int32_t GetScreenSupportedColorGamutsLocked(ScreenId id, std::vector<ScreenColorGamut>& mode) const; 298 int32_t GetScreenSupportedMetaDataKeysLocked(ScreenId id, std::vector<ScreenHDRMetadataKey>& keys) const; 299 int32_t GetScreenColorGamutLocked(ScreenId id, ScreenColorGamut& mode) const; 300 int32_t SetScreenColorGamutLocked(ScreenId id, int32_t modeIdx); 301 int32_t SetScreenGamutMapLocked(ScreenId id, ScreenGamutMap mode); 302 int32_t GetScreenGamutMapLocked(ScreenId id, ScreenGamutMap& mode) const; 303 int32_t GetScreenHDRCapabilityLocked(ScreenId id, RSScreenHDRCapability& screenHdrCapability) const; 304 int32_t GetScreenTypeLocked(ScreenId id, RSScreenType& type) const; 305 int32_t SetScreenSkipFrameIntervalLocked(ScreenId id, uint32_t skipFrameInterval); 306 307 mutable std::mutex mutex_; 308 HdiBackend *composer_ = nullptr; 309 ScreenId defaultScreenId_ = INVALID_SCREEN_ID; 310 std::unordered_map<ScreenId, std::unique_ptr<OHOS::Rosen::RSScreen>> screens_; 311 std::queue<ScreenId> freeVirtualScreenIds_; 312 uint32_t maxVirtualScreenNum_ = 0; 313 std::vector<sptr<RSIScreenChangeCallback>> screenChangeCallbacks_; 314 bool mipiCheckInFirstHotPlugEvent_ = false; 315 std::vector<ScreenId> connectedIds_; 316 std::unordered_map<ScreenId, uint32_t> screenPowerStatus_; 317 318 static std::once_flag createFlag_; 319 static sptr<OHOS::Rosen::RSScreenManager> instance_; 320 }; 321 } // namespace impl 322 } // namespace Rosen 323 } // namespace OHOS 324 #endif // RS_SCREEN_MANAGER 325