• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 POINTER_DRAWING_MANAGER_H
17 #define POINTER_DRAWING_MANAGER_H
18 
19 #include "transaction/rs_transaction.h"
20 #include "transaction/rs_interfaces.h"
21 
22 #include "common_event_manager.h"
23 #include "device_observer.h"
24 #include "hardware_cursor_pointer_manager.h"
25 #include "dm_common.h"
26 #include "screen_manager_lite.h"
27 #include "i_pointer_drawing_manager.h"
28 #include "mouse_event_normalize.h"
29 #include "screen_pointer.h"
30 
31 namespace OHOS {
32 namespace MMI {
33 namespace {
34 constexpr int32_t DEFAULT_FRAME_RATE { 30 };
35 constexpr int32_t INVALID_DISPLAY_ID { -1 };
36 constexpr float HARDWARE_CANVAS_SIZE { 512.0f };
37 } // namespace
38 
39 struct isMagicCursor {
40     std::string name;
41     bool isShow { false };
42 };
43 
44 struct PixelMapReleaseContext {
PixelMapReleaseContextPixelMapReleaseContext45     explicit PixelMapReleaseContext(std::shared_ptr<Media::PixelMap> pixelMap) : pixelMap_(pixelMap) {}
46 
~PixelMapReleaseContextPixelMapReleaseContext47     ~PixelMapReleaseContext()
48     {
49         pixelMap_ = nullptr;
50     }
51 
52 private:
53     std::shared_ptr<Media::PixelMap> pixelMap_ { nullptr };
54 };
55 
56 class ScreenModeChangeListener final : public OHOS::Rosen::ScreenManagerLite::IScreenModeChangeListener {
57 public:
58     using callback_t = std::function<void(const std::vector<sptr<OHOS::Rosen::ScreenInfo>> &)>;
ScreenModeChangeListener(callback_t func)59     explicit ScreenModeChangeListener(callback_t func): callback_(func) {}
60     virtual ~ScreenModeChangeListener() = default;
61 
NotifyScreenModeChange(const std::vector<sptr<OHOS::Rosen::ScreenInfo>> & screens)62     void NotifyScreenModeChange(const std::vector<sptr<OHOS::Rosen::ScreenInfo>> &screens) override
63     {
64         return callback_(screens);
65     }
66 
67 private:
68     callback_t callback_;
69 };
70 
71 class DelegateInterface;
72 class PointerDrawingManager final : public IPointerDrawingManager, public IDeviceObserver {
73 public:
74     PointerDrawingManager();
75     DISALLOW_COPY_AND_MOVE(PointerDrawingManager);
76     ~PointerDrawingManager();
77     void DrawPointer(uint64_t rsId, int32_t physicalX, int32_t physicalY,
78         const PointerStyle pointerStyle, Direction direction) override;
79     void UpdateDisplayInfo(const OLD::DisplayInfo& displayInfo) override;
80     void OnDisplayInfo(const OLD::DisplayGroupInfo& displayGroupInfo) override;
81     void OnWindowInfo(const WinInfo &info) override;
82     void UpdatePointerDevice(bool hasPointerDevice, bool isPointerVisible, bool isHotPlug) override;
83     bool Init() override;
84     int32_t SetPointerColor(int32_t color) override;
85     int32_t GetPointerColor() override;
86     void DeletePointerVisible(int32_t pid) override;
87     int32_t SetPointerVisible(int32_t pid, bool visible, int32_t priority, bool isHap) override;
88     bool GetPointerVisible(int32_t pid) override;
89     int32_t SetPointerStyle(int32_t pid, int32_t windowId, PointerStyle pointerStyle,
90         bool isUiExtension = false) override;
91     int32_t ClearWindowPointerStyle(int32_t pid, int32_t windowId) override;
92     int32_t GetPointerStyle(int32_t pid, int32_t windowId, PointerStyle &pointerStyle,
93         bool isUiExtension = false) override;
94     int32_t SetPointerSize(int32_t size) override;
95     int32_t GetPointerSize() override;
96     void GetPointerImageSize(int32_t &width, int32_t &height) override;
97     int32_t GetCursorSurfaceId(uint64_t &surfaceId) override;
98     void DrawPointerStyle(const PointerStyle& pointerStyle) override;
99     bool IsPointerVisible() override;
100     void SetPointerLocation(int32_t x, int32_t y, uint64_t rsId) override;
101     void AdjustMouseFocus(Direction direction, ICON_TYPE iconType, int32_t &physicalX, int32_t &physicalY);
102     void SetMouseDisplayState(bool state) override;
103     bool GetMouseDisplayState() const override;
104     int32_t SetCustomCursor(CursorPixelMap curPixelMap,
105         int32_t pid, int32_t windowId, int32_t focusX, int32_t focusY) override;
106     int32_t SetCustomCursor(int32_t pid, int32_t windowId, CustomCursor cursor, CursorOptions options) override;
107     int32_t SetMouseIcon(int32_t pid, int32_t windowId, CursorPixelMap curPixelMap) override;
108     int32_t SetMouseHotSpot(int32_t pid, int32_t windowId, int32_t hotSpotX, int32_t hotSpotY) override;
109     PointerStyle GetLastMouseStyle() override;
110     const std::map<MOUSE_ICON, IconStyle>& GetMouseIconPath() override;
111     IconStyle GetIconStyle(const MOUSE_ICON mouseStyle) override;
112     bool HasMagicCursor();
113     int32_t DrawCursor(const MOUSE_ICON mouseStyle);
114     int32_t SwitchPointerStyle() override;
115     void DrawMovePointer(uint64_t rsId, int32_t physicalX, int32_t physicalY) override;
116     std::vector<std::vector<std::string>> GetDisplayInfo(OLD::DisplayInfo &di);
117     void Dump(int32_t fd, const std::vector<std::string> &args) override;
118     void AttachToDisplay();
119     int32_t EnableHardwareCursorStats(int32_t pid, bool enable) override;
120     int32_t GetHardwareCursorStats(int32_t pid, uint32_t &frameCount, uint32_t &vsyncCount) override;
121     void SubscribeScreenModeChange() override;
122     void RegisterDisplayStatusReceiver() override;
123     OLD::DisplayInfo GetCurrentDisplayInfo() override;
124 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR
125     int32_t GetPointerSnapshot(void *pixelMapPtr) override;
126 #endif // OHOS_BUILD_ENABLE_MAGICCURSOR
127     void InitPointerCallback() override;
128     void InitPointerObserver() override;
129     void OnSessionLost(int32_t pid) override;
130     int32_t SkipPointerLayer(bool isSkip) override;
SetDelegateProxy(std::shared_ptr<DelegateInterface> proxy)131     void SetDelegateProxy(std::shared_ptr<DelegateInterface> proxy) override
132     {
133         delegateProxy_ = proxy;
134     }
GetDelegateProxy()135     std::shared_ptr<DelegateInterface> GetDelegateProxy() override
136     {
137         return delegateProxy_;
138     }
139     void DestroyPointerWindow() override;
140     void DrawScreenCenterPointer(const PointerStyle& pointerStyle) override;
141     void OnScreenModeChange(const std::vector<sptr<OHOS::Rosen::ScreenInfo>> &screens);
142     int32_t UpdateMouseLayer(const PointerStyle& pointerStyle, int32_t physicalX, int32_t physicalY) override;
143 #ifndef OHOS_BUILD_ENABLE_WATCH
144     void NotifyPointerEventToRS(int32_t pointAction, int32_t pointCnt) override;
145 #endif // OHOS_BUILD_ENABLE_WATCH
146     void RsRemoteDiedCallback();
147 
148 private:
149     struct PixelMapInfo {
150         std::shared_ptr<OHOS::Media::PixelMap> pixelMap { nullptr };
151         int32_t imageWidth { 0 };
152         int32_t imageHeight { 0 };
153         int32_t pointerColor { 0 };
154     };
155     IconStyle GetIconType(MOUSE_ICON mouseIcon);
156     void GetPreferenceKey(std::string &name);
157     void DrawLoadingPointerStyle(const MOUSE_ICON mouseStyle);
158     void DrawRunningPointerAnimate(const MOUSE_ICON mouseStyle);
159     void CreatePointerWindow(uint64_t rsId, int32_t physicalX, int32_t physicalY, Direction direction);
160     int32_t CreatePointerWindowForScreenPointer(uint64_t rsId, int32_t physicalX, int32_t physicalY);
161     int32_t CreatePointerWindowForNoScreenPointer(int32_t physicalX, int32_t physicalY);
162     sptr<OHOS::Surface> GetLayer();
163     sptr<OHOS::SurfaceBuffer> GetSurfaceBuffer(sptr<OHOS::Surface> layer);
164     sptr<OHOS::SurfaceBuffer> RetryGetSurfaceBuffer(sptr<OHOS::Surface> layer);
165     void DoDraw(uint8_t *addr, uint32_t width, uint32_t height, const MOUSE_ICON mouseStyle = MOUSE_ICON::DEFAULT);
166     void DrawPixelmap(OHOS::Rosen::Drawing::Canvas &canvas, const MOUSE_ICON mouseStyle);
167     void DrawManager();
168     void FixCursorPosition(int32_t &physicalX, int32_t &physicalY);
169     std::shared_ptr<OHOS::Media::PixelMap> LoadCursorSvgWithColor(MOUSE_ICON type, int32_t color);
170     std::shared_ptr<OHOS::Media::PixelMap> DecodeImageToPixelMap(MOUSE_ICON type);
171     void UpdatePointerVisible();
172     int32_t UpdateDefaultPointerStyle(int32_t pid, int32_t windowId, PointerStyle style, bool isUiExtension = false);
173     void CheckMouseIconPath();
174     void InitStyle();
175     int32_t InitLayer(const MOUSE_ICON mouseStyle);
176     int32_t SetPointerStylePreference(PointerStyle pointerStyle);
177     void UpdateMouseStyle();
178     int32_t UpdateCursorProperty(CursorPixelMap curPixelMap, const int32_t &focusX, const int32_t &focusY);
179     int32_t UpdateCursorProperty(CustomCursor cursor);
180     void RotateDegree(Direction direction);
181     int32_t DrawMovePointer(uint64_t rsId, int32_t physicalX, int32_t physicalY,
182         PointerStyle pointerStyle, Direction direction);
183     void AdjustMouseFocusByDirection0(ICON_TYPE iconType, int32_t &physicalX, int32_t &physicalY);
184     void AdjustMouseFocusByDirection90(ICON_TYPE iconType, int32_t &physicalX, int32_t &physicalY);
185     void AdjustMouseFocusByDirection180(ICON_TYPE iconType, int32_t &physicalX, int32_t &physicalY);
186     void AdjustMouseFocusByDirection270(ICON_TYPE iconType, int32_t &physicalX, int32_t &physicalY);
187     void CreateMagicCursorChangeObserver();
188     int32_t CreatePointerSwitchObserver(isMagicCursor& item);
189     void UpdateStyleOptions();
190     int32_t GetIndependentPixels();
191     bool IsWindowRotation(const OLD::DisplayInfo *displayInfo);
192     Direction GetDisplayDirection(const OLD::DisplayInfo *displayInfo);
193     bool CheckPointerStyleParam(int32_t windowId, PointerStyle pointerStyle);
194     std::map<MOUSE_ICON, IconStyle>& GetMouseIcons();
195     void UpdateIconPath(const MOUSE_ICON mouseStyle, std::string iconPath);
196     std::shared_ptr<Rosen::Drawing::ColorSpace> ConvertToColorSpace(Media::ColorSpace colorSpace);
197     Rosen::Drawing::ColorType PixelFormatToColorType(Media::PixelFormat pixelFormat);
198     Rosen::Drawing::AlphaType AlphaTypeToAlphaType(Media::AlphaType alphaType);
199     std::shared_ptr<Rosen::Drawing::Image> ExtractDrawingImage(std::shared_ptr<Media::PixelMap> pixelMap);
200     void DrawImage(OHOS::Rosen::Drawing::Canvas &canvas, MOUSE_ICON mouseStyle);
201     int32_t ReloadPixelMaps(std::map<MOUSE_ICON, PixelMapInfo>& mousePixelMap, int32_t pointerColor);
202     void InitPixelMaps();
203 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR
204     void SetPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap);
205 #endif // OHOS_BUILD_ENABLE_MAGICCURSOR
206     void ForceClearPointerVisiableStatus() override;
207     int32_t UpdateSurfaceNodeBounds(int32_t physicalX, int32_t physicalY);
208     void CreateCanvasNode();
209     bool SetCursorLocation(int32_t physicalX, int32_t physicalY, ICON_TYPE iconType);
210     void SetHardwareCursorPosition(int32_t physicalX, int32_t physicalY,
211         PointerStyle pointerStyle);
212     std::shared_ptr<OHOS::Media::PixelMap> GetUserIconCopy();
213     ICON_TYPE MouseIcon2IconType(MOUSE_ICON m);
214     void SetSurfaceNodeBounds();
215     int32_t DrawNewDpiPointer() override;
216     bool SetDynamicHardWareCursorLocation(int32_t physicalX, int32_t physicalY, MOUSE_ICON mouseStyle);
217     void RenderThreadLoop();
218     void SoftCursorRenderThreadLoop();
219     void MoveRetryThreadLoop();
220     int32_t RequestNextVSync();
221     void OnVsync(uint64_t timestamp);
222     void PostTask(std::function<void()> task);
223     void PostSoftCursorTask(std::function<void()> task);
224     void PostMoveRetryTask(std::function<void()> task);
225     int32_t FlushBuffer();
226     int32_t GetSurfaceInformation();
227     void UpdateBindDisplayId(uint64_t rsId);
228     void PostTaskRSLocation(int32_t physicalX, int32_t physicalY, std::shared_ptr<Rosen::RSSurfaceNode> surfaceNode);
229     int32_t InitVsync(MOUSE_ICON mouseStyle);
230     void DumpScreenInfo(std::ostringstream& oss);
231     int32_t DrawSoftCursor(std::shared_ptr<Rosen::RSSurfaceNode> sn, const RenderConfig &cfg);
232     int32_t DrawHardCursor(std::shared_ptr<ScreenPointer> sp, const RenderConfig &cfg);
233     std::vector<std::shared_ptr<ScreenPointer>> GetMirrorScreenPointers();
234     std::shared_ptr<ScreenPointer> GetScreenPointer(uint64_t screenId);
235     void CreateRenderConfig(RenderConfig& cfg, std::shared_ptr<ScreenPointer> sp, MOUSE_ICON mouseStyle, bool isHard);
236     Direction CalculateRenderDirection(const bool isHard, const bool isWindowRotation);
237     void SoftwareCursorRender(MOUSE_ICON mouseStyle);
238     void HardwareCursorRender(MOUSE_ICON mouseStyle);
239     void SoftwareCursorMove(int32_t x, int32_t y, ICON_TYPE align);
240     void SoftwareCursorMoveAsync(int32_t x, int32_t y, ICON_TYPE align);
241     void MoveRetryAsync(int32_t x, int32_t y, ICON_TYPE align);
242     void ResetMoveRetryTimer();
243     int32_t HardwareCursorMove(int32_t x, int32_t y, ICON_TYPE align);
244     void HideHardwareCursors();
245     int32_t GetMainScreenDisplayInfo(const OLD::DisplayGroupInfo &displayGroupInfo,
246         OLD::DisplayInfo &mainScreenDisplayInfo) const;
247     int32_t DrawDynamicHardwareCursor(std::shared_ptr<ScreenPointer> sp, const RenderConfig &cfg);
248     int32_t DrawDynamicSoftCursor(std::shared_ptr<Rosen::RSSurfaceNode> sn, const RenderConfig &cfg);
249     void HardwareCursorDynamicRender(MOUSE_ICON mouseStyle);
250     void SoftwareCursorDynamicRender(MOUSE_ICON mouseStyle);
251     void UpdateMirrorScreens(std::shared_ptr<ScreenPointer> sp, OLD::DisplayInfo displayInfo);
252     void AttachAllSurfaceNode() override;
253     void DetachAllSurfaceNode() override;
254     int32_t CheckHwcReady() override;
255     std::shared_ptr<Rosen::RSSurfaceNode> GetSurfaceNode();
256     void SetSurfaceNode(std::shared_ptr<Rosen::RSSurfaceNode> ptr);
257     void AdjustMouseFocusToSoftRenderOrigin(Direction direction, const MOUSE_ICON pointerStyle, int32_t &physicalX,
258         int32_t &physicalY);
259     bool GetHardCursorEnabled() override;
260 private:
261     struct PidInfo {
262         int32_t pid { 0 };
263         bool visible { false };
264     };
265     bool hasDisplay_ { false };
266     OLD::DisplayInfo displayInfo_ {};
267     bool hasPointerDevice_ { false };
268     int32_t lastPhysicalX_ { -1 };
269     int32_t lastPhysicalY_ { -1 };
270     PointerStyle lastMouseStyle_ {};
271     PointerStyle currentMouseStyle_ {};
272     PointerStyle lastDrawPointerStyle_ {};
273     int32_t pid_ { 0 };
274     int32_t windowId_ { 0 };
275     int32_t imageWidth_ { 0 };
276     int32_t imageHeight_ { 0 };
277     int32_t canvasWidth_ = 64;
278     int32_t canvasHeight_ = 64;
279     int32_t cursorWidth_ { 0 };
280     int32_t cursorHeight_ { 0 };
281     std::map<MOUSE_ICON, IconStyle> mouseIcons_;
282     std::list<PidInfo> pidInfos_;
283     std::list<PidInfo> hapPidInfos_;
284     bool mouseDisplayState_ { false };
285     bool mouseIconUpdate_ { false };
286     std::shared_ptr<OHOS::Media::PixelMap> userIcon_ { nullptr };
287     uint64_t screenId_ { 0 };
288     std::mutex surfaceNodeMutex_;
289     std::shared_ptr<Rosen::RSSurfaceNode> surfaceNode_;
290     std::shared_ptr<Rosen::RSCanvasNode> canvasNode_;
291     int32_t userIconHotSpotX_ { 0 };
292     int32_t userIconHotSpotY_ { 0 };
293     int32_t tempPointerColor_ { -1 };
294     int32_t originSetColor_ { -1 };
295     Direction lastDirection_ { DIRECTION0 };
296     Direction currentDirection_ { DIRECTION0 };
297     isMagicCursor hasMagicCursor_;
298     bool hasInitObserver_ { false };
299     int32_t frameCount_ { DEFAULT_FRAME_RATE };
300     int32_t currentFrame_ { 0 };
301     uint64_t displayId_ { 0 };
302     std::shared_ptr<AppExecFwk::EventRunner> runner_ { nullptr };
303     std::shared_ptr<AppExecFwk::EventHandler> handler_ { nullptr };
304     std::shared_ptr<Rosen::VSyncReceiver> receiver_ { nullptr };
305     std::atomic<bool> isRenderRuning_{ false };
306     std::unique_ptr<std::thread> renderThread_ { nullptr };
307     bool isInit_ { false };
308     std::mutex mtx_;
309     std::recursive_mutex rec_mtx_;
310     std::shared_ptr<HardwareCursorPointerManager> hardwareCursorPointerManager_ { nullptr };
311     sptr<ScreenModeChangeListener> screenModeChangeListener_ { nullptr };
312     std::unordered_map<uint64_t, std::shared_ptr<ScreenPointer>> screenPointers_;
313     PointerRenderer pointerRenderer_;
314     bool userIconFollowSystem_ { false };
315     std::shared_ptr<AppExecFwk::EventRunner> softCursorRunner_ { nullptr };
316     std::shared_ptr<AppExecFwk::EventHandler> softCursorHander_ { nullptr };
317     std::unique_ptr<std::thread> softCursorRenderThread_ { nullptr };
318     std::shared_ptr<AppExecFwk::EventRunner> moveRetryRunner_ { nullptr };
319     std::shared_ptr<AppExecFwk::EventHandler> moveRetryHander_ { nullptr };
320     std::unique_ptr<std::thread> moveRetryThread_ { nullptr };
321     int32_t moveRetryTimerId_ { -1 };
322     int32_t moveRetryCount_ { 0 };
323     float hardwareCanvasSize_ { HARDWARE_CANVAS_SIZE };
324 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR
325     std::shared_ptr<OHOS::Media::PixelMap> pixelMap_ { nullptr };
326 #endif // OHOS_BUILD_ENABLE_MAGICCURSOR
327     std::shared_ptr<DelegateInterface> delegateProxy_ { nullptr };
328     uint64_t lastDisplayId_ { 0 };
329     std::map<MOUSE_ICON, PixelMapInfo> mousePixelMap_;
330     std::mutex mousePixelMapMutex_;
331     int32_t initLoadingAndLoadingRightPixelTimerId_ { -1 };
332     int releaseFence_ { -1 };
333     bool followSystem_ { false };
334     int32_t focusX_ { 0 };
335     int32_t focusY_ { 0 };
336     std::atomic<bool> initEventhandlerFlag_ { false };
337     std::atomic<bool> initDisplayStatusReceiverFlag_ { false };
338     std::shared_ptr<PointerDrawingManager> self_ { nullptr };
339     Rosen::OnRemoteDiedCallback OnRemoteDiedCallback_ { nullptr };
340     std::shared_ptr<EventFwk::CommonEventSubscriber> commonEventSubscriber_ { nullptr };
341 };
342 } // namespace MMI
343 } // namespace OHOS
344 #endif // POINTER_DRAWING_MANAGER_H
345