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 std::string GetName() override; 96 int32_t GetAvailableWidth() override; 97 int32_t GetAvailableHeight() override; 98 bool GetAliveStatus() override; 99 DisplayState GetDisplayState() override; 100 int32_t GetDensityDpi() override; 101 int32_t GetX() override; 102 int32_t GetY() override; 103 DisplaySourceMode GetDisplaySourceMode() override; 104 int32_t GetPhysicalWidth() override; 105 int32_t GetPhysicalHeight() override; 106 float GetDefaultVirtualPixelRatio() override; 107 private: 108 sptr<OHOS::Rosen::Display> display_; 109 OHOS::NWeb::RotationType ConvertRotationType(OHOS::Rosen::Rotation type); 110 OHOS::NWeb::OrientationType ConvertOrientationType(OHOS::Rosen::Orientation type); 111 OHOS::NWeb::DisplayOrientation ConvertDisplayOrientationType(OHOS::Rosen::DisplayOrientation type); 112 OHOS::NWeb::FoldStatus ConvertFoldStatus(NativeDisplayManager_FoldDisplayMode displayMode); 113 OHOS::NWeb::DisplayState ConvertDisplayState(OHOS::Rosen::DisplayState state); 114 OHOS::NWeb::DisplaySourceMode ConvertDisplaySourceMode(OHOS::Rosen::DisplaySourceMode mode); 115 }; 116 117 using ListenerMap = 118 std::map<int32_t, sptr<DisplayListenerAdapterImpl>>; 119 using FoldStatusListenerMap = 120 std::map<int32_t, sptr<FoldStatusListenerAdapterImpl>>; 121 class DisplayManagerAdapterImpl : public DisplayManagerAdapter { 122 public: 123 DisplayManagerAdapterImpl() = default; 124 ~DisplayManagerAdapterImpl() override = default; 125 DisplayId GetDefaultDisplayId() override; 126 std::shared_ptr<DisplayAdapter> GetDefaultDisplay() override; 127 uint32_t RegisterDisplayListener(std::shared_ptr<DisplayListenerAdapter> listener) override; 128 bool UnregisterDisplayListener(uint32_t id) override; 129 bool IsDefaultPortrait() override; 130 uint32_t RegisterFoldStatusListener(std::shared_ptr<FoldStatusListenerAdapter> listener) override; 131 bool UnregisterFoldStatusListener(uint32_t id) override; 132 static FoldStatusListenerMap foldStatusReg_; 133 static std::mutex foldStatusRegMutex; 134 std::shared_ptr<DisplayAdapter> GetPrimaryDisplay() override; 135 std::vector<std::shared_ptr<DisplayAdapter>> GetAllDisplays() override; 136 private: 137 ListenerMap reg_; 138 }; 139 } 140 141 #endif