• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 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 #include <unordered_set>
25 #include <future>
26 
27 #include <hdi_backend.h>
28 #include <ipc_callbacks/screen_change_callback.h>
29 #include <refbase.h>
30 #include <screen_manager/rs_screen_props.h>
31 #include <screen_manager/rs_screen_mode_info.h>
32 #include <screen_manager/rs_screen_capability.h>
33 #include <screen_manager/rs_screen_data.h>
34 #include <screen_manager/rs_screen_hdr_capability.h>
35 #include <screen_manager/screen_types.h>
36 #include <screen_manager/rs_virtual_screen_resolution.h>
37 #include <surface.h>
38 #include <surface_type.h>
39 #ifdef RS_SUBSCRIBE_SENSOR_ENABLE
40 #include "sensor_agent.h"
41 #include "sensor_agent_type.h"
42 #endif
43 #include "rs_screen.h"
44 
45 namespace OHOS {
46 namespace Rosen {
47 enum class ScreenState : uint8_t {
48     HDI_OUTPUT_ENABLE,
49     PRODUCER_SURFACE_ENABLE,
50     DISABLED,
51     NOT_EXISTED,
52     UNKNOWN,
53 };
54 
55 struct ScreenInfo {
56     ScreenId id = INVALID_SCREEN_ID;
57     uint32_t width = 0; // render resolution
58     uint32_t height = 0;
59     uint32_t phyWidth = 0; // physical screen resolution
60     uint32_t phyHeight = 0;
61     ScreenColorGamut colorGamut = ScreenColorGamut::COLOR_GAMUT_SRGB;
62     ScreenState state = ScreenState::UNKNOWN;
63     ScreenRotation rotation = ScreenRotation::ROTATION_0;
64     std::unordered_set<uint64_t> filteredAppSet = {};
65 
66     uint32_t skipFrameInterval = DEFAULT_SKIP_FRAME_INTERVAL;  // skip frame interval for change screen refresh rate
67 
68     GraphicPixelFormat pixelFormat = GraphicPixelFormat::GRAPHIC_PIXEL_FMT_RGBA_8888;
69     ScreenHDRFormat hdrFormat = ScreenHDRFormat::NOT_SUPPORT_HDR;
70 
GetRotatedWidthScreenInfo71     uint32_t GetRotatedWidth() const
72     {
73         return (rotation == ScreenRotation::ROTATION_0 || rotation == ScreenRotation::ROTATION_180) ? width : height;
74     }
75 
GetRotatedHeightScreenInfo76     uint32_t GetRotatedHeight() const
77     {
78         return (rotation == ScreenRotation::ROTATION_0 || rotation == ScreenRotation::ROTATION_180) ? height : width;
79     }
GetRotatedPhyWidthScreenInfo80     uint32_t GetRotatedPhyWidth() const
81     {
82         return (rotation == ScreenRotation::ROTATION_0 ||
83             rotation == ScreenRotation::ROTATION_180) ? phyWidth : phyHeight;
84     }
85 
GetRotatedPhyHeightScreenInfo86     uint32_t GetRotatedPhyHeight() const
87     {
88         return (rotation == ScreenRotation::ROTATION_0 ||
89             rotation == ScreenRotation::ROTATION_180) ? phyHeight : phyWidth;
90     }
91 
GetRogWidthRatioScreenInfo92     float GetRogWidthRatio() const
93     {
94         return (width == 0) ? 1.f : static_cast<float>(phyWidth) / width;
95     }
96 
GetRogHeightRatioScreenInfo97     float GetRogHeightRatio() const
98     {
99         return (height == 0) ? 1.f : static_cast<float>(phyHeight) / height;
100     }
101 };
102 
103 class RSScreenManager : public RefBase {
104 public:
105     RSScreenManager() = default;
106     virtual ~RSScreenManager() noexcept = default;
107 
108     virtual bool Init() noexcept = 0;
109 
110     // get default/primary screen id.
111     virtual ScreenId GetDefaultScreenId() const = 0;
112 
113     virtual std::vector<ScreenId> GetAllScreenIds() = 0;
114 
115     virtual void SetDefaultScreenId(ScreenId id) = 0;
116 
117     virtual void SetScreenMirror(ScreenId id, ScreenId toMirror) = 0;
118 
119     virtual ScreenId CreateVirtualScreen(
120         const std::string &name,
121         uint32_t width,
122         uint32_t height,
123         sptr<Surface> surface,
124         ScreenId mirrorId = 0,
125         int flags = 0,
126         std::vector<uint64_t> filteredAppVector = {}) = 0;
127 
128     virtual int32_t SetVirtualScreenSurface(ScreenId id, sptr<Surface> surface) = 0;
129 
130     virtual void RemoveVirtualScreen(ScreenId id) = 0;
131 
132     virtual void SetScreenActiveMode(ScreenId id, uint32_t modeId) = 0;
133 
134     virtual int32_t SetRogScreenResolution(ScreenId id, uint32_t width, uint32_t height) = 0;
135 
136     virtual int32_t SetVirtualScreenResolution(ScreenId id, uint32_t width, uint32_t height) = 0;
137 
138     virtual void SetScreenPowerStatus(ScreenId id, ScreenPowerStatus status) = 0;
139 
140     virtual bool SetVirtualMirrorScreenCanvasRotation(ScreenId id, bool canvasRotation) = 0;
141 
142     virtual int32_t SetScreenCorrection(ScreenId id, ScreenRotation screenRotation) = 0;
143 
144     virtual void GetVirtualScreenResolution(ScreenId id, RSVirtualScreenResolution& virtualScreenResolution) const = 0;
145 
146     virtual void GetScreenActiveMode(ScreenId id, RSScreenModeInfo& screenModeInfo) const = 0;
147 
148     virtual std::vector<RSScreenModeInfo> GetScreenSupportedModes(ScreenId id) const = 0;
149 
150     virtual RSScreenCapability GetScreenCapability(ScreenId id) const = 0;
151 
152     virtual ScreenPowerStatus GetScreenPowerStatus(ScreenId id) const = 0;
153 
154     virtual ScreenRotation GetScreenCorrection(ScreenId id) const = 0;
155 
156     virtual RSScreenData GetScreenData(ScreenId id) const = 0;
157 
158     virtual ScreenInfo QueryScreenInfo(ScreenId id) const = 0;
159 
160     // Can only be called after QueryScreenState and the state is ScreenState::PRODUCER_SURFACE_ENABLE;
161     virtual sptr<Surface> GetProducerSurface(ScreenId id) const = 0;
162 
163     virtual bool GetCanvasRotation(ScreenId id) const = 0;
164 
165     // Can only be called after QueryScreenState and the state is ScreenState::HDI_OUTPUT_ENABLE;
166     virtual std::shared_ptr<HdiOutput> GetOutput(ScreenId id) const = 0;
167 
168     virtual int32_t AddScreenChangeCallback(const sptr<RSIScreenChangeCallback> &callback) = 0;
169 
170     virtual void RemoveScreenChangeCallback(const sptr<RSIScreenChangeCallback> &callback) = 0;
171 
172     virtual void ProcessScreenHotPlugEvents() = 0;
173 
174     virtual void DisplayDump(std::string& dumpString) = 0;
175 
176     virtual void SurfaceDump(std::string& dumpString) = 0;
177 
178     virtual void FpsDump(std::string& dumpString, std::string& arg) = 0;
179 
180     virtual void ClearFpsDump(std::string& dumpString, std::string& arg) = 0;
181 
182     virtual int32_t ResizeVirtualScreen(ScreenId id, uint32_t width, uint32_t height) = 0;
183 
184     virtual int32_t GetScreenBacklight(ScreenId id) = 0;
185 
186     virtual void SetScreenBacklight(ScreenId id, uint32_t level) = 0;
187 
188     virtual int32_t GetScreenSupportedColorGamuts(ScreenId id, std::vector<ScreenColorGamut>& mode) const = 0;
189 
190     virtual int32_t GetScreenSupportedMetaDataKeys(ScreenId id, std::vector<ScreenHDRMetadataKey>& keys) const = 0;
191 
192     virtual int32_t GetScreenColorGamut(ScreenId id, ScreenColorGamut& mode) const = 0;
193 
194     virtual int32_t SetScreenColorGamut(ScreenId id, int32_t modeIdx) = 0;
195 
196     virtual int32_t SetScreenGamutMap(ScreenId id, ScreenGamutMap mode) = 0;
197 
198     virtual int32_t GetScreenGamutMap(ScreenId id, ScreenGamutMap& mode) const = 0;
199 
200     virtual int32_t GetScreenHDRCapability(ScreenId id, RSScreenHDRCapability& screenHdrCapability) const = 0;
201 
202     virtual int32_t GetScreenType(ScreenId id, RSScreenType& type) const = 0;
203 
204     virtual int32_t SetScreenSkipFrameInterval(ScreenId id, uint32_t skipFrameInterval) = 0;
205 
206     virtual int32_t GetPixelFormat(ScreenId id, GraphicPixelFormat& pixelFormat) const = 0;
207 
208     virtual int32_t SetPixelFormat(ScreenId id, GraphicPixelFormat pixelFormat) = 0;
209 
210     virtual int32_t GetScreenSupportedHDRFormats(ScreenId id, std::vector<ScreenHDRFormat>& hdrFormats) const = 0;
211 
212     virtual int32_t GetScreenHDRFormat(ScreenId id, ScreenHDRFormat& hdrFormat) const = 0;
213 
214     virtual int32_t SetScreenHDRFormat(ScreenId id, int32_t modeIdx) = 0;
215 
216     virtual int32_t GetScreenSupportedColorSpaces(
217         ScreenId id, std::vector<GraphicCM_ColorSpaceType>& colorSpaces) const = 0;
218 
219     virtual int32_t GetScreenColorSpace(ScreenId id, GraphicCM_ColorSpaceType& colorSpace) const = 0;
220 
221     virtual uint32_t GetActualScreensNum() const = 0;
222 
223     virtual int32_t SetScreenColorSpace(ScreenId id, GraphicCM_ColorSpaceType colorSpace) = 0;
224 
225     /* only used for mock tests */
226     virtual void MockHdiScreenConnected(std::unique_ptr<impl::RSScreen>& rsScreen) = 0;
227 
228 #ifdef USE_VIDEO_PROCESSING_ENGINE
229     virtual float GetScreenBrightnessNits(ScreenId id) = 0;
230 #endif
231 
232 #ifdef RS_SUBSCRIBE_SENSOR_ENABLE
233     virtual void HandlePostureData(const SensorEvent * const event) = 0;
234 
235     virtual ScreenId GetActiveScreenId() = 0;
236 #endif
237 };
238 
239 sptr<RSScreenManager> CreateOrGetScreenManager();
240 
241 namespace impl {
242 struct ScreenHotPlugEvent {
243     std::shared_ptr<HdiOutput> output;
244     bool connected = false;
245 };
246 
247 enum class FoldState : uint32_t {
248     UNKNOW,
249     FOLDED,
250     EXPAND
251 };
252 
253 class RSScreenManager : public OHOS::Rosen::RSScreenManager {
254 public:
255     static sptr<OHOS::Rosen::RSScreenManager> GetInstance() noexcept;
256 
257     // noncopyable
258     RSScreenManager(const RSScreenManager &) = delete;
259     RSScreenManager &operator=(const RSScreenManager &) = delete;
260 
261     bool Init() noexcept override;
262 
GetDefaultScreenId()263     ScreenId GetDefaultScreenId() const override
264     {
265         return defaultScreenId_;
266     }
267 
268     std::vector<ScreenId> GetAllScreenIds() override;
269 
270     void SetDefaultScreenId(ScreenId id) override;
271 
272     void SetScreenMirror(ScreenId id, ScreenId toMirror) override;
273 
274     ScreenId CreateVirtualScreen(
275         const std::string &name,
276         uint32_t width,
277         uint32_t height,
278         sptr<Surface> surface,
279         ScreenId mirrorId,
280         int32_t flags,
281         std::vector<uint64_t> filteredAppVector) override;
282 
283     int32_t SetVirtualScreenSurface(ScreenId id, sptr<Surface> surface) override;
284 
285     void RemoveVirtualScreen(ScreenId id) override;
286 
287     void SetScreenActiveMode(ScreenId id, uint32_t modeId) override;
288 
289     int32_t SetRogScreenResolution(ScreenId id, uint32_t width, uint32_t height) override;
290 
291     int32_t SetVirtualScreenResolution(ScreenId id, uint32_t width, uint32_t height) override;
292 
293     void SetScreenPowerStatus(ScreenId id, ScreenPowerStatus status) override;
294 
295     bool SetVirtualMirrorScreenCanvasRotation(ScreenId id, bool canvasRotation) override;
296 
297     void GetVirtualScreenResolution(ScreenId id, RSVirtualScreenResolution& virtualScreenResolution) const override;
298 
299     void GetScreenActiveMode(ScreenId id, RSScreenModeInfo& screenModeInfo) const override;
300 
301     std::vector<RSScreenModeInfo> GetScreenSupportedModes(ScreenId id) const override;
302 
303     RSScreenCapability GetScreenCapability(ScreenId id) const override;
304 
305     ScreenPowerStatus GetScreenPowerStatus(ScreenId id) const override;
306 
307     ScreenRotation GetScreenCorrection(ScreenId id) const override;
308 
309     RSScreenData GetScreenData(ScreenId id) const  override;
310 
311     ScreenInfo QueryScreenInfo(ScreenId id) const override;
312 
313     sptr<Surface> GetProducerSurface(ScreenId id) const override;
314 
315     bool GetCanvasRotation(ScreenId id) const override;
316 
317     std::shared_ptr<HdiOutput> GetOutput(ScreenId id) const override;
318 
319     int32_t AddScreenChangeCallback(const sptr<RSIScreenChangeCallback> &callback) override;
320 
321     void RemoveScreenChangeCallback(const sptr<RSIScreenChangeCallback> &callback) override;
322 
323     void ProcessScreenHotPlugEvents() override;
324 
325     void DisplayDump(std::string& dumpString) override;
326 
327     void SurfaceDump(std::string& dumpString) override;
328 
329     void FpsDump(std::string& dumpString, std::string& arg) override;
330 
331     void ClearFpsDump(std::string& dumpString, std::string& arg) override;
332 
333     int32_t ResizeVirtualScreen(ScreenId id, uint32_t width, uint32_t height) override;
334 
335     int32_t GetScreenBacklight(ScreenId id) override;
336 
337     void SetScreenBacklight(ScreenId id, uint32_t level) override;
338 
339     int32_t GetScreenSupportedColorGamuts(ScreenId id, std::vector<ScreenColorGamut>& mode) const override;
340 
341     int32_t GetScreenSupportedMetaDataKeys(ScreenId id, std::vector<ScreenHDRMetadataKey>& keys) const override;
342 
343     int32_t GetScreenColorGamut(ScreenId id, ScreenColorGamut& mode) const override;
344 
345     uint32_t GetActualScreensNum() const override;
346 
347     int32_t SetScreenColorGamut(ScreenId id, int32_t modeIdx) override;
348 
349     int32_t SetScreenGamutMap(ScreenId id, ScreenGamutMap mode) override;
350 
351     int32_t SetScreenCorrection(ScreenId id, ScreenRotation screenRotation) override;
352 
353     int32_t GetScreenGamutMap(ScreenId id, ScreenGamutMap& mode) const override;
354 
355     int32_t GetScreenHDRCapability(ScreenId id, RSScreenHDRCapability& screenHdrCapability) const override;
356 
357     int32_t GetScreenType(ScreenId id, RSScreenType& type) const override;
358 
359     int32_t SetScreenSkipFrameInterval(ScreenId id, uint32_t skipFrameInterval) override;
360 
361     int32_t GetPixelFormat(ScreenId id, GraphicPixelFormat& pixelFormat) const override;
362 
363     int32_t SetPixelFormat(ScreenId id, GraphicPixelFormat pixelFormat) override;
364 
365     int32_t GetScreenSupportedHDRFormats(ScreenId id, std::vector<ScreenHDRFormat>& hdrFormats) const override;
366 
367     int32_t GetScreenHDRFormat(ScreenId id, ScreenHDRFormat& hdrFormat) const override;
368 
369     int32_t SetScreenHDRFormat(ScreenId id, int32_t modeIdx) override;
370 
371     int32_t GetScreenSupportedColorSpaces(
372         ScreenId id, std::vector<GraphicCM_ColorSpaceType>& colorSpaces) const override;
373 
374     int32_t GetScreenColorSpace(ScreenId id, GraphicCM_ColorSpaceType& colorSpace) const override;
375 
376     int32_t SetScreenColorSpace(ScreenId id, GraphicCM_ColorSpaceType colorSpace) override;
377 
378     /* only used for mock tests */
MockHdiScreenConnected(std::unique_ptr<impl::RSScreen> & rsScreen)379     void MockHdiScreenConnected(std::unique_ptr<impl::RSScreen>& rsScreen) override
380     {
381         if (rsScreen == nullptr) {
382             return;
383         }
384         screens_[rsScreen->Id()] = std::move(rsScreen);
385     }
386 
387 #ifdef USE_VIDEO_PROCESSING_ENGINE
388     float GetScreenBrightnessNits(ScreenId id) override;
389 #endif
390 
391 #ifdef RS_SUBSCRIBE_SENSOR_ENABLE
392     void HandlePostureData(const SensorEvent * const event) override;
393 
394     ScreenId GetActiveScreenId() override;
395 #endif
396 
397 private:
398     RSScreenManager();
399     ~RSScreenManager() noexcept override;
400 
401     static void OnHotPlug(std::shared_ptr<HdiOutput> &output, bool connected, void *data);
402     void OnHotPlugEvent(std::shared_ptr<HdiOutput> &output, bool connected);
403     static void OnHwcDead(void *data);
404     void OnHwcDeadEvent();
405     void CleanAndReinit();
406     void ProcessScreenConnectedLocked(std::shared_ptr<HdiOutput> &output);
407     void AddScreenToHgm(std::shared_ptr<HdiOutput> &output);
408     void ProcessScreenDisConnectedLocked(std::shared_ptr<HdiOutput> &output);
409     void RemoveScreenFromHgm(std::shared_ptr<HdiOutput> &output);
410     void HandleDefaultScreenDisConnectedLocked();
411     std::vector<ScreenHotPlugEvent> pendingHotPlugEvents_;
412 
413     void GetVirtualScreenResolutionLocked(ScreenId id, RSVirtualScreenResolution& virtualScreenResolution) const;
414     void GetScreenActiveModeLocked(ScreenId id, RSScreenModeInfo& screenModeInfo) const;
415     std::vector<RSScreenModeInfo> GetScreenSupportedModesLocked(ScreenId id) const;
416     RSScreenCapability GetScreenCapabilityLocked(ScreenId id) const;
417     ScreenPowerStatus GetScreenPowerStatusLocked(ScreenId id) const;
418     ScreenRotation GetScreenCorrectionLocked(ScreenId id) const;
419     int32_t GetScreenBacklightLocked(ScreenId id) const;
420 
421     void RemoveVirtualScreenLocked(ScreenId id);
422     ScreenId GenerateVirtualScreenIdLocked();
423     void ReuseVirtualScreenIdLocked(ScreenId id);
424 
425     int32_t GetScreenSupportedColorGamutsLocked(ScreenId id, std::vector<ScreenColorGamut>& mode) const;
426     int32_t GetScreenSupportedMetaDataKeysLocked(ScreenId id, std::vector<ScreenHDRMetadataKey>& keys) const;
427     int32_t GetScreenColorGamutLocked(ScreenId id, ScreenColorGamut& mode) const;
428     int32_t SetScreenColorGamutLocked(ScreenId id, int32_t modeIdx);
429     int32_t SetScreenGamutMapLocked(ScreenId id, ScreenGamutMap mode);
430     int32_t SetScreenCorrectionLocked(ScreenId id, ScreenRotation screenRotation);
431     int32_t GetScreenGamutMapLocked(ScreenId id, ScreenGamutMap& mode) const;
432     int32_t GetScreenHDRCapabilityLocked(ScreenId id, RSScreenHDRCapability& screenHdrCapability) const;
433     int32_t GetScreenTypeLocked(ScreenId id, RSScreenType& type) const;
434     int32_t SetScreenSkipFrameIntervalLocked(ScreenId id, uint32_t skipFrameInterval);
435     int32_t GetPixelFormatLocked(ScreenId id, GraphicPixelFormat& pixelFormat) const;
436     int32_t SetPixelFormatLocked(ScreenId id, GraphicPixelFormat pixelFormat);
437     int32_t GetScreenSupportedHDRFormatsLocked(ScreenId id, std::vector<ScreenHDRFormat>& hdrFormats) const;
438     int32_t GetScreenHDRFormatLocked(ScreenId id, ScreenHDRFormat& hdrFormat) const;
439     int32_t SetScreenHDRFormatLocked(ScreenId id, int32_t modeIdx);
440     int32_t GetScreenSupportedColorSpacesLocked(ScreenId id, std::vector<GraphicCM_ColorSpaceType>& colorSpaces) const;
441     int32_t GetScreenColorSpaceLocked(ScreenId id, GraphicCM_ColorSpaceType& colorSpace) const;
442     int32_t SetScreenColorSpaceLocked(ScreenId id, GraphicCM_ColorSpaceType colorSpace);
443 
444 #ifdef RS_SUBSCRIBE_SENSOR_ENABLE
445     void RegisterSensorCallback();
446     void UnRegisterSensorCallback();
447     void HandleSensorData(float angle);
448     FoldState TransferAngleToScreenState(float angle);
449 #endif
450 
451     mutable std::mutex mutex_;
452     HdiBackend *composer_ = nullptr;
453     ScreenId defaultScreenId_ = INVALID_SCREEN_ID;
454     std::map<ScreenId, std::unique_ptr<OHOS::Rosen::RSScreen>> screens_;
455     std::queue<ScreenId> freeVirtualScreenIds_;
456     uint32_t maxVirtualScreenNum_ = 0;
457     std::vector<sptr<RSIScreenChangeCallback>> screenChangeCallbacks_;
458     bool mipiCheckInFirstHotPlugEvent_ = false;
459     bool isHwcDead_ = false;
460     std::vector<ScreenId> connectedIds_;
461     std::unordered_map<ScreenId, uint32_t> screenPowerStatus_;
462     std::unordered_map<ScreenId, uint32_t> screenBacklight_;
463 
464     static std::once_flag createFlag_;
465     static sptr<OHOS::Rosen::RSScreenManager> instance_;
466 
467 #ifdef RS_SUBSCRIBE_SENSOR_ENABLE
468     SensorUser user;
469     bool isFoldScreenFlag_ = false;
470     ScreenId innerScreenId_ = 0;
471     ScreenId externalScreenId_ = INVALID_SCREEN_ID;
472     ScreenId activeScreenId_ = 0;
473     bool isFirstTimeToGetActiveScreenId_ = true;
474     bool isPostureSensorDataHandled_ = false;
475     std::condition_variable activeScreenIdAssignedCV_;
476     mutable std::mutex activeScreenIdAssignedMutex_;
477 #endif
478 };
479 } // namespace impl
480 } // namespace Rosen
481 } // namespace OHOS
482 #endif // RS_SCREEN_MANAGER
483