1 /* 2 * Copyright (c) 2021 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 <hilog/log.h> 27 #include <ipc_callbacks/screen_change_callback.h> 28 #include <refbase.h> 29 #include <screen_manager/rs_screen_props.h> 30 #include <screen_manager/rs_screen_mode_info.h> 31 #include <screen_manager/rs_screen_capability.h> 32 #include <screen_manager/rs_screen_data.h> 33 #include <screen_manager/screen_types.h> 34 #include <surface.h> 35 36 #include "rs_screen.h" 37 38 namespace OHOS { 39 namespace Rosen { 40 enum class ScreenState : uint8_t { 41 HDI_OUTPUT_ENABLE, 42 PRODUCER_SURFACE_ENABLE, 43 DISABLED, 44 NOT_EXISTED, 45 UNKNOWN, 46 }; 47 48 struct ScreenInfo { 49 uint32_t width = 0; 50 uint32_t height = 0; 51 ScreenColorGamut colorGamut = ScreenColorGamut::COLOR_GAMUT_SRGB; 52 ScreenState state = ScreenState::UNKNOWN; 53 SkMatrix rotationMatrix; // Screen rotation matrix for canvas. 54 }; 55 56 class RSScreenManager : public RefBase { 57 public: 58 RSScreenManager() = default; 59 virtual ~RSScreenManager() noexcept = default; 60 61 virtual bool Init() noexcept = 0; 62 63 // get default/primary screen id. 64 virtual ScreenId GetDefaultScreenId() const = 0; 65 66 virtual void SetDefaultScreenId(ScreenId id) = 0; 67 68 virtual void SetScreenMirror(ScreenId id, ScreenId toMirror) = 0; 69 70 virtual ScreenId CreateVirtualScreen( 71 const std::string &name, 72 uint32_t width, 73 uint32_t height, 74 sptr<Surface> surface, 75 ScreenId mirrorId = 0, 76 int flags = 0) = 0; 77 78 virtual int32_t SetVirtualScreenSurface(ScreenId id, sptr<Surface> surface) = 0; 79 80 virtual void RemoveVirtualScreen(ScreenId id) = 0; 81 82 virtual void SetScreenActiveMode(ScreenId id, uint32_t modeId) = 0; 83 84 virtual void SetScreenPowerStatus(ScreenId id, ScreenPowerStatus status) = 0; 85 86 virtual void GetScreenActiveMode(ScreenId id, RSScreenModeInfo& screenModeInfo) const = 0; 87 88 virtual std::vector<RSScreenModeInfo> GetScreenSupportedModes(ScreenId id) const = 0; 89 90 virtual RSScreenCapability GetScreenCapability(ScreenId id) const = 0; 91 92 virtual ScreenPowerStatus GetScreenPowerStatus(ScreenId id) const = 0; 93 94 virtual RSScreenData GetScreenData(ScreenId id) const = 0; 95 96 virtual ScreenInfo QueryScreenInfo(ScreenId id) const = 0; 97 98 // Can only be called after QueryScreenState and the state is ScreenState::PRODUCER_SURFACE_ENABLE; 99 virtual sptr<Surface> GetProducerSurface(ScreenId id) const = 0; 100 101 // Can only be called after QueryScreenState and the state is ScreenState::HDI_OUTPUT_ENABLE; 102 virtual std::shared_ptr<HdiOutput> GetOutput(ScreenId id) const = 0; 103 104 virtual int32_t AddScreenChangeCallback(const sptr<RSIScreenChangeCallback> &callback) = 0; 105 106 virtual void RemoveScreenChangeCallback(const sptr<RSIScreenChangeCallback> &callback) = 0; 107 108 virtual void ProcessScreenHotPlugEvents() = 0; 109 110 virtual void DisplayDump(std::string& dumpString) = 0; 111 112 virtual void SurfaceDump(std::string& dumpString) = 0; 113 114 virtual void FpsDump(std::string& dumpString, std::string& arg) = 0; 115 116 virtual int32_t GetScreenBacklight(ScreenId id) = 0; 117 118 virtual void SetScreenBacklight(ScreenId id, uint32_t level) = 0; 119 120 virtual int32_t GetScreenSupportedColorGamuts(ScreenId id, std::vector<ScreenColorGamut>& mode) const = 0; 121 122 virtual int32_t GetScreenColorGamut(ScreenId id, ScreenColorGamut& mode) const = 0; 123 124 virtual int32_t SetScreenColorGamut(ScreenId id, int32_t modeIdx) = 0; 125 126 virtual int32_t SetScreenGamutMap(ScreenId id, ScreenGamutMap mode) = 0; 127 128 virtual int32_t GetScreenGamutMap(ScreenId id, ScreenGamutMap& mode) const = 0; 129 130 virtual bool RequestRotation(ScreenId id, ScreenRotation rotation) = 0; 131 132 virtual ScreenRotation GetRotation(ScreenId id) const = 0; 133 }; 134 135 sptr<RSScreenManager> CreateOrGetScreenManager(); 136 137 namespace impl { 138 struct ScreenHotPlugEvent { 139 std::shared_ptr<HdiOutput> output; 140 bool connected = false; 141 }; 142 143 class RSScreenManager : public OHOS::Rosen::RSScreenManager { 144 public: 145 static sptr<OHOS::Rosen::RSScreenManager> GetInstance() noexcept; 146 147 // noncopyable 148 RSScreenManager(const RSScreenManager &) = delete; 149 RSScreenManager &operator=(const RSScreenManager &) = delete; 150 151 bool Init() noexcept override; 152 GetDefaultScreenId()153 ScreenId GetDefaultScreenId() const override 154 { 155 return defaultScreenId_; 156 } 157 158 void SetDefaultScreenId(ScreenId id) override; 159 160 void SetScreenMirror(ScreenId id, ScreenId toMirror) override; 161 162 ScreenId CreateVirtualScreen( 163 const std::string &name, 164 uint32_t width, 165 uint32_t height, 166 sptr<Surface> surface, 167 ScreenId mirrorId, 168 int32_t flags) override; 169 170 int32_t SetVirtualScreenSurface(ScreenId id, sptr<Surface> surface) override; 171 172 void RemoveVirtualScreen(ScreenId id) override; 173 174 void SetScreenActiveMode(ScreenId id, uint32_t modeId) override; 175 176 void SetScreenPowerStatus(ScreenId id, ScreenPowerStatus status) override; 177 178 void GetScreenActiveMode(ScreenId id, RSScreenModeInfo& screenModeInfo) const override; 179 180 std::vector<RSScreenModeInfo> GetScreenSupportedModes(ScreenId id) const override; 181 182 RSScreenCapability GetScreenCapability(ScreenId id) const override; 183 184 ScreenPowerStatus GetScreenPowerStatus(ScreenId id) const override; 185 186 RSScreenData GetScreenData(ScreenId id) const override; 187 188 ScreenInfo QueryScreenInfo(ScreenId id) const override; 189 190 sptr<Surface> GetProducerSurface(ScreenId id) const override; 191 192 std::shared_ptr<HdiOutput> GetOutput(ScreenId id) const override; 193 194 int32_t AddScreenChangeCallback(const sptr<RSIScreenChangeCallback> &callback) override; 195 196 void RemoveScreenChangeCallback(const sptr<RSIScreenChangeCallback> &callback) override; 197 198 void ProcessScreenHotPlugEvents() override; 199 200 void DisplayDump(std::string& dumpString) override; 201 202 void SurfaceDump(std::string& dumpString) override; 203 204 void FpsDump(std::string& dumpString, std::string& arg) override; 205 206 int32_t GetScreenBacklight(ScreenId id) override; 207 208 void SetScreenBacklight(ScreenId id, uint32_t level) override; 209 210 int32_t GetScreenSupportedColorGamuts(ScreenId id, std::vector<ScreenColorGamut>& mode) const override; 211 212 int32_t GetScreenColorGamut(ScreenId id, ScreenColorGamut& mode) const override; 213 214 int32_t SetScreenColorGamut(ScreenId id, int32_t modeIdx) override; 215 216 int32_t SetScreenGamutMap(ScreenId id, ScreenGamutMap mode) override; 217 218 int32_t GetScreenGamutMap(ScreenId id, ScreenGamutMap& mode) const override; 219 220 bool RequestRotation(ScreenId id, ScreenRotation rotation) override; 221 222 ScreenRotation GetRotation(ScreenId id) const override; 223 private: 224 RSScreenManager(); 225 ~RSScreenManager() noexcept override; 226 227 // [PLANNING]: fixme -- domain 0 only for debug. 228 static constexpr HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, 0, "RSScreenManager" }; 229 230 static void OnHotPlug(std::shared_ptr<HdiOutput> &output, bool connected, void *data); 231 void OnHotPlugEvent(std::shared_ptr<HdiOutput> &output, bool connected); 232 void ProcessScreenConnectedLocked(std::shared_ptr<HdiOutput> &output); 233 void ProcessScreenDisConnectedLocked(std::shared_ptr<HdiOutput> &output); 234 void HandleDefaultScreenDisConnectedLocked(); 235 std::vector<ScreenHotPlugEvent> pendingHotPlugEvents_; 236 237 void GetScreenActiveModeLocked(ScreenId id, RSScreenModeInfo& screenModeInfo) const; 238 std::vector<RSScreenModeInfo> GetScreenSupportedModesLocked(ScreenId id) const; 239 RSScreenCapability GetScreenCapabilityLocked(ScreenId id) const; 240 ScreenPowerStatus GetScreenPowerStatuslocked(ScreenId id) const; 241 int32_t GetScreenBacklightlocked(ScreenId id) const; 242 243 void RemoveVirtualScreenLocked(ScreenId id); 244 ScreenId GenerateVirtualScreenIdLocked(); 245 void ReuseVirtualScreenIdLocked(ScreenId id); 246 247 int32_t GetScreenSupportedColorGamutsLocked(ScreenId id, std::vector<ScreenColorGamut>& mode) const; 248 int32_t GetScreenColorGamutLocked(ScreenId id, ScreenColorGamut& mode) const; 249 int32_t SetScreenColorGamutLocked(ScreenId id, int32_t modeIdx); 250 int32_t SetScreenGamutMapLocked(ScreenId id, ScreenGamutMap mode); 251 int32_t GetScreenGamutMapLocked(ScreenId id, ScreenGamutMap& mode) const; 252 bool RequestRotationLocked(ScreenId id, ScreenRotation rotation); 253 ScreenRotation GetRotationLocked(ScreenId id) const; 254 255 mutable std::mutex mutex_; 256 HdiBackend *composer_ = nullptr; 257 ScreenId defaultScreenId_ = INVALID_SCREEN_ID; 258 std::unordered_map<ScreenId, std::unique_ptr<OHOS::Rosen::RSScreen>> screens_; 259 std::queue<ScreenId> freeVirtualScreenIds_; 260 uint32_t maxVirtualScreenNum_ = 0; 261 std::vector<sptr<RSIScreenChangeCallback>> screenChangeCallbacks_; 262 263 static std::once_flag createFlag_; 264 static sptr<OHOS::Rosen::RSScreenManager> instance_; 265 }; 266 } // namespace impl 267 } // namespace Rosen 268 } // namespace OHOS 269 #endif // RS_SCREEN_MANAGER 270