1 /*
2  * Copyright (c) 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 DISPLAY_MANAGER_ADAPTER_IMPL_H
17 #define DISPLAY_MANAGER_ADAPTER_IMPL_H
18 
19 #include <map>
20 #include "display_manager_adapter.h"
21 
22 #include "display.h"
23 #include "display_manager.h"
24 #include "dm_common.h"
25 #include "oh_display_manager.h"
26 #include "oh_display_info.h"
27 
28 namespace OHOS::NWeb {
29 struct DisplayInfo {
30     int32_t width_ {0};
31     int32_t height_ {0};
32     uint32_t refreshRate_ {0};
33     float virtualPixelRatio_ {0.0f};
34     float xDpi_ {0.0f};
35     float yDpi_ {0.0f};
36     OHOS::Rosen::Rotation rotationType_ {Rosen::Rotation::ROTATION_0};
37     OHOS::Rosen::Orientation orientationType_ {Rosen::Orientation::UNSPECIFIED};
38     OHOS::Rosen::DisplayOrientation displayOrientation_ {Rosen::DisplayOrientation::UNKNOWN};
39 
40     DisplayInfo() = default;
41     bool operator == (const OHOS::NWeb::DisplayInfo& rhs)
42     {
43         return width_ == rhs.width_ && height_ == rhs.height_  &&
44             std::abs(virtualPixelRatio_ - rhs.virtualPixelRatio_) < EPS &&
45             std::abs(xDpi_ - rhs.xDpi_) < EPS && std::abs(yDpi_ - rhs.yDpi_) < EPS &&
46             rotationType_ == rhs.rotationType_ && orientationType_ == rhs.orientationType_ &&
47             displayOrientation_ == rhs.displayOrientation_;
48     }
49 private:
50     static constexpr double EPS = 0.0000001;
51 };
52 
53 class DisplayListenerAdapterImpl
54     : public OHOS::Rosen::DisplayManager::IDisplayListener {
55 public:
56     explicit DisplayListenerAdapterImpl(std::shared_ptr<DisplayListenerAdapter> listener);
57     ~DisplayListenerAdapterImpl() override = default;
58     void OnCreate(DisplayId id) override;
59     void OnDestroy(DisplayId id) override;
60     void OnChange(DisplayId id) override;
61 private:
62     bool CheckOnlyRefreshRateDecreased(DisplayId id);
63     OHOS::NWeb::DisplayInfo ConvertDisplayInfo(const OHOS::Rosen::DisplayInfo& info);
64 
65     std::shared_ptr<DisplayListenerAdapter> listener_;
66     DisplayInfo cachedDisplayedInfo_ {};
67 };
68 
69 class FoldStatusListenerAdapterImpl
70     : public virtual RefBase {
71 public:
72     explicit FoldStatusListenerAdapterImpl(std::shared_ptr<FoldStatusListenerAdapter> listener);
73     ~FoldStatusListenerAdapterImpl() = default;
74     void OnFoldStatusChanged(NativeDisplayManager_FoldDisplayMode displayMode) ;
75 private:
76     OHOS::NWeb::FoldStatus ConvertFoldStatus(NativeDisplayManager_FoldDisplayMode displayMode);
77     std::shared_ptr<FoldStatusListenerAdapter> listener_;
78 };
79 
80 class DisplayAdapterImpl : public DisplayAdapter {
81 public:
82     DisplayAdapterImpl() = delete;
83     explicit DisplayAdapterImpl(sptr<OHOS::Rosen::Display> display);
84     ~DisplayAdapterImpl() override = default;
85     DisplayId GetId() override;
86     int32_t GetWidth() override;
87     int32_t GetHeight() override;
88     float GetVirtualPixelRatio() override;
89     RotationType GetRotation() override;
90     OrientationType GetOrientation() override;
91     int32_t GetDpi() override;
92     DisplayOrientation GetDisplayOrientation() override;
93     FoldStatus GetFoldStatus() override;
94     bool IsFoldable() override;
95 private:
96     sptr<OHOS::Rosen::Display> display_;
97     OHOS::NWeb::RotationType ConvertRotationType(OHOS::Rosen::Rotation type);
98     OHOS::NWeb::OrientationType ConvertOrientationType(OHOS::Rosen::Orientation type);
99     OHOS::NWeb::DisplayOrientation ConvertDisplayOrientationType(OHOS::Rosen::DisplayOrientation type);
100     OHOS::NWeb::FoldStatus ConvertFoldStatus(NativeDisplayManager_FoldDisplayMode displayMode);
101 };
102 
103 using ListenerMap =
104     std::map<int32_t, sptr<DisplayListenerAdapterImpl>>;
105 using FoldStatusListenerMap =
106     std::map<int32_t, sptr<FoldStatusListenerAdapterImpl>>;
107 class DisplayManagerAdapterImpl : public DisplayManagerAdapter {
108 public:
109     DisplayManagerAdapterImpl() = default;
110     ~DisplayManagerAdapterImpl() override = default;
111     DisplayId GetDefaultDisplayId() override;
112     std::shared_ptr<DisplayAdapter> GetDefaultDisplay() override;
113     uint32_t RegisterDisplayListener(std::shared_ptr<DisplayListenerAdapter> listener) override;
114     bool UnregisterDisplayListener(uint32_t id) override;
115     bool IsDefaultPortrait() override;
116     uint32_t RegisterFoldStatusListener(std::shared_ptr<FoldStatusListenerAdapter> listener) override;
117     bool UnregisterFoldStatusListener(uint32_t id) override;
118     static FoldStatusListenerMap foldStatusReg_;
119 private:
120     ListenerMap reg_;
121 };
122 }
123 
124 #endif