• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025-2025 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 SCREEN_POINTER_H
17 #define SCREEN_POINTER_H
18 
19 #include "screen_info.h"
20 
21 #include "hardware_cursor_pointer_manager.h"
22 #include "pointer_renderer.h"
23 #include "old_display_info.h"
24 
25 namespace OHOS::MMI {
26 using hwcmgr_ptr_t = std::shared_ptr<HardwareCursorPointerManager>;
27 using handler_ptr_t = std::shared_ptr<OHOS::AppExecFwk::EventHandler>;
28 using buffer_ptr_t = sptr<OHOS::SurfaceBuffer>;
29 using screen_info_ptr_t = sptr<OHOS::Rosen::ScreenInfo>;
30 using mode_t = OHOS::Rosen::ScreenSourceMode;
31 using rotation_t = OHOS::Rosen::Rotation;
32 
33 uint32_t GetScreenInfoWidth(const screen_info_ptr_t);
34 uint32_t GetScreenInfoHeight(const screen_info_ptr_t);
35 
36 class ScreenPointer final {
37 public:
38     DISALLOW_COPY_AND_MOVE(ScreenPointer);
39     ScreenPointer(hwcmgr_ptr_t hwcmgr, handler_ptr_t handler, const OLD::DisplayInfo &di);
40     ScreenPointer(hwcmgr_ptr_t hwcmgr, handler_ptr_t handler, screen_info_ptr_t si);
41     ~ScreenPointer() = default;
42 
43     bool Init(PointerRenderer &render);
44     bool InitSurface();
45     void UpdateScreenInfo(screen_info_ptr_t si);
46     bool UpdatePadding(uint32_t mainWidth, uint32_t mainHeight);
47     void OnDisplayInfo(const OLD::DisplayInfo &di, bool isWindowRotation);
48 
49     buffer_ptr_t GetDefaultBuffer();
50     buffer_ptr_t GetTransparentBuffer();
51     buffer_ptr_t GetCommonBuffer();
52     buffer_ptr_t RequestBuffer(const RenderConfig &cfg, bool &isCommonBuffer);
53     buffer_ptr_t GetCurrentBuffer();
54 
55     bool Move(int32_t x, int32_t y, ICON_TYPE align);
56     bool MoveSoft(int32_t x, int32_t y, ICON_TYPE align);
57     void CalculatePositionForMirror(int32_t x, int32_t y, int32_t* px, int32_t* py);
58     bool SetInvisible();
59 
GetScreenId()60     uint64_t GetScreenId() const
61     {
62         return screenId_;
63     }
64 
GetScreenWidth()65     uint32_t GetScreenWidth() const
66     {
67         return width_;
68     }
69 
GetScreenHeight()70     uint32_t GetScreenHeight() const
71     {
72         return height_;
73     }
74 
GetSurfaceNode()75     std::shared_ptr<OHOS::Rosen::RSSurfaceNode> GetSurfaceNode()
76     {
77         return surfaceNode_;
78     }
79 
SetDPI(float dpi)80     void SetDPI(float dpi)
81     {
82         dpi_ = dpi;
83     }
84 
GetDPI()85     float GetDPI() const
86     {
87         return dpi_;
88     }
89 
GetScale()90     float GetScale() const
91     {
92         if (GetIsCurrentOffScreenRendering() && (IsExtend() || IsMain())) {
93             return offRenderScale_;
94         } else {
95             return scale_;
96         }
97     }
98 
GetMode()99     uint32_t GetMode() const
100     {
101         return static_cast<uint32_t>(mode_);
102     }
103 
IsMain()104     bool IsMain() const
105     {
106         return mode_ == mode_t::SCREEN_MAIN;
107     }
108 
IsMirror()109     bool IsMirror() const
110     {
111         return mode_ == mode_t::SCREEN_MIRROR;
112     }
113 
IsExtend()114     bool IsExtend() const
115     {
116         return mode_ == mode_t::SCREEN_EXTEND;
117     }
118 
GetIsCurrentOffScreenRendering()119     bool GetIsCurrentOffScreenRendering() const
120     {
121         return isCurrentOffScreenRendering_;
122     }
123 
GetOffRenderScale()124     float GetOffRenderScale() const
125     {
126         return offRenderScale_;
127     }
128 
GetScreenRealDPI()129     int32_t GetScreenRealDPI() const
130     {
131         return screenRealDPI_;
132     }
133 
134     float GetRenderDPI() const;
135 
SetRotation(const rotation_t rotation)136     void SetRotation(const rotation_t rotation)
137     {
138         rotation_ = rotation;
139     }
140 
GetRotation()141     rotation_t GetRotation()
142     {
143         return rotation_;
144     }
145 
SetDisplayDirection(const Direction displayDirection)146     void SetDisplayDirection(const Direction displayDirection)
147     {
148         switch (displayDirection) {
149             case DIRECTION0:
150             case DIRECTION90:
151             case DIRECTION180:
152             case DIRECTION270:
153                 displayDirection_ = displayDirection;
154                 break;
155             default: {
156                 break;
157             }
158         }
159     }
160 
GetDisplayDirection()161     Direction GetDisplayDirection()
162     {
163         return displayDirection_;
164     }
165 
SetIsWindowRotation(bool isWindowRotation)166     void SetIsWindowRotation(bool isWindowRotation)
167     {
168         isWindowRotation_ = isWindowRotation;
169     }
170 
171     bool IsPositionOutScreen(int32_t x, int32_t y);
172 
GetBufferId()173     uint32_t GetBufferId()
174     {
175         return bufferId_;
176     }
177 
SetMirrorWidth(const uint32_t mirrorWidth)178     void SetMirrorWidth(const uint32_t mirrorWidth)
179     {
180         mirrorWidth_ = mirrorWidth;
181     }
182 
GetMirrorWidth()183     uint32_t GetMirrorWidth() const
184     {
185         return mirrorWidth_;
186     }
187 
SetMirrorHeight(const uint32_t mirrorHeight)188     void SetMirrorHeight(const uint32_t mirrorHeight)
189     {
190         mirrorHeight_ = mirrorHeight;
191     }
192 
GetMirrorHeight()193     uint32_t GetMirrorHeight() const
194     {
195         return mirrorHeight_;
196     }
SetVirtualExtend(bool isVirtualExtend)197     void SetVirtualExtend(bool isVirtualExtend)
198     {
199         isVirtualExtend_ = isVirtualExtend;
200     }
201 
202 private:
203     bool InitSurfaceNode();
204     bool FlushSerfaceBuffer();
205     void Rotate(rotation_t rotation, int32_t& x, int32_t& y);
206     void CalculateHwcPositionForMirror(int32_t& x, int32_t& y);
207     void CalculateHwcPositionForExtend(int32_t& x, int32_t& y);
208     bool InitDefaultBuffer(const OHOS::BufferRequestConfig &bufferCfg, PointerRenderer &render);
209     bool InitTransparentBuffer(const OHOS::BufferRequestConfig &bufferCfg);
210     bool InitCommonBuffer(const OHOS::BufferRequestConfig &bufferCfg);
211     buffer_ptr_t CreateSurfaceBuffer(const OHOS::BufferRequestConfig &bufferCfg);
212     bool IsDefaultCfg(const RenderConfig &cfg);
213 
214 private:
215     std::mutex mtx_;
216 
217     uint64_t screenId_{0};
218     uint32_t width_{0};
219     uint32_t height_{0};
220     uint32_t mirrorWidth_{0};
221     uint32_t mirrorHeight_{0};
222     mode_t mode_{mode_t::SCREEN_MAIN};
223     rotation_t rotation_{rotation_t::ROTATION_0};
224     float dpi_{1.0f};
225     Direction displayDirection_{DIRECTION0};
226     bool isWindowRotation_{false};
227 
228     // screen scale and padding info
229     float scale_{1.0f};
230     int32_t paddingTop_{0};
231     int32_t paddingLeft_{0};
232 
233     hwcmgr_ptr_t hwcMgr_{nullptr};
234     handler_ptr_t handler_{nullptr};
235 
236     // RS Layer
237     std::shared_ptr<OHOS::Rosen::RSSurfaceNode> surfaceNode_{nullptr};
238     std::shared_ptr<OHOS::Rosen::RSCanvasNode> canvasNode_{nullptr};
239 
240     RenderConfig defaultCursorCfg_;
241     buffer_ptr_t transparentBuffer_{nullptr};
242     buffer_ptr_t defaultBuffer_{nullptr};
243     buffer_ptr_t currentBuffer_{nullptr};
244     std::vector<buffer_ptr_t> commonBuffers_;
245     uint32_t bufferId_ {0};
246 
247     // isCurrentOffScreenRendering
248     bool isCurrentOffScreenRendering_ = false;
249     float offRenderScale_{1.0f};
250     int32_t screenRealDPI_{1.0f};
251     bool isVirtualExtend_ = false;
252 };
253 
254 } // namespace OHOS::MMI
255 
256 #endif // SCREEN_POINTER_H