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 <iostream>
20 #include <list>
21 
22 #include "draw/canvas.h"
23 #include "nocopyable.h"
24 #include "pixel_map.h"
25 #include "transaction/rs_transaction.h"
26 #include "transaction/rs_interfaces.h"
27 #include "ui/rs_canvas_node.h"
28 #include "ui/rs_surface_node.h"
29 #include "window.h"
30 
31 #include "device_observer.h"
32 #ifdef OHOS_BUILD_ENABLE_HARDWARE_CURSOR
33 #include "hardware_cursor_pointer_manager.h"
34 #endif // OHOS_BUILD_ENABLE_HARDWARE_CURSOR
35 #include "i_pointer_drawing_manager.h"
36 #include "mouse_event_normalize.h"
37 #include "setting_observer.h"
38 #include "struct_multimodal.h"
39 
40 namespace OHOS {
41 namespace MMI {
42 struct isMagicCursor {
43     std::string name;
44     bool isShow { false };
45 };
46 
47 struct PixelMapReleaseContext {
PixelMapReleaseContextPixelMapReleaseContext48     explicit PixelMapReleaseContext(std::shared_ptr<Media::PixelMap> pixelMap) : pixelMap_(pixelMap) {}
49 
~PixelMapReleaseContextPixelMapReleaseContext50     ~PixelMapReleaseContext()
51     {
52         pixelMap_ = nullptr;
53     }
54 
55 private:
56     std::shared_ptr<Media::PixelMap> pixelMap_ { nullptr };
57 };
58 class DelegateInterface;
59 class PointerDrawingManager final : public IPointerDrawingManager,
60                                     public IDeviceObserver,
61                                     public std::enable_shared_from_this<PointerDrawingManager> {
62 public:
63     PointerDrawingManager();
64     DISALLOW_COPY_AND_MOVE(PointerDrawingManager);
65     ~PointerDrawingManager() override = default;
66     void DrawPointer(int32_t displayId, int32_t physicalX, int32_t physicalY,
67         const PointerStyle pointerStyle, Direction direction) override;
68     void UpdateDisplayInfo(const DisplayInfo& displayInfo) override;
69     void OnDisplayInfo(const DisplayGroupInfo& displayGroupInfo) override;
70     void OnWindowInfo(const WinInfo &info) override;
71     void UpdatePointerDevice(bool hasPointerDevice, bool isPointerVisible, bool isHotPlug) override;
72     bool Init() override;
73     int32_t SetPointerColor(int32_t color) override;
74     int32_t GetPointerColor() override;
75     void DeletePointerVisible(int32_t pid) override;
76     int32_t SetPointerVisible(int32_t pid, bool visible, int32_t priority, bool isHap) override;
77     bool GetPointerVisible(int32_t pid) override;
78     int32_t SetPointerStyle(int32_t pid, int32_t windowId, PointerStyle pointerStyle,
79         bool isUiExtension = false) override;
80     int32_t ClearWindowPointerStyle(int32_t pid, int32_t windowId) override;
81     int32_t GetPointerStyle(int32_t pid, int32_t windowId, PointerStyle &pointerStyle,
82         bool isUiExtension = false) override;
83     int32_t SetPointerSize(int32_t size) override;
84     int32_t GetPointerSize() override;
85     void DrawPointerStyle(const PointerStyle& pointerStyle) override;
86     bool IsPointerVisible() override;
87     void SetPointerLocation(int32_t x, int32_t y) override;
88     void AdjustMouseFocus(Direction direction, ICON_TYPE iconType, int32_t &physicalX, int32_t &physicalY);
89     void SetMouseDisplayState(bool state) override;
90     bool GetMouseDisplayState() const override;
91     int32_t SetCustomCursor(void* pixelMap, int32_t pid, int32_t windowId, int32_t focusX, int32_t focusY) override;
92     int32_t SetMouseIcon(int32_t pid, int32_t windowId, void* pixelMap) override;
93     int32_t SetMouseHotSpot(int32_t pid, int32_t windowId, int32_t hotSpotX, int32_t hotSpotY) override;
94     PointerStyle GetLastMouseStyle() override;
95     std::map<MOUSE_ICON, IconStyle> GetMouseIconPath() override;
96     IconStyle GetIconStyle(const MOUSE_ICON mouseStyle);
97     bool HasMagicCursor();
98     int32_t DrawCursor(const MOUSE_ICON mouseStyle);
99     int32_t SwitchPointerStyle() override;
100     void DrawMovePointer(int32_t displayId, int32_t physicalX, int32_t physicalY) override;
101     void Dump(int32_t fd, const std::vector<std::string> &args) override;
102     void AttachToDisplay();
103     int32_t EnableHardwareCursorStats(int32_t pid, bool enable) override;
104     int32_t GetHardwareCursorStats(int32_t pid, uint32_t &frameCount, uint32_t &vsyncCount) override;
105 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR
106     int32_t GetPointerSnapshot(void *pixelMapPtr) override;
107 #endif // OHOS_BUILD_ENABLE_MAGICCURSOR
108     void InitPointerCallback() override;
109     void InitPointerObserver() override;
110     void OnSessionLost(int32_t pid) override;
111     int32_t SkipPointerLayer(bool isSkip) override;
SetDelegateProxy(std::shared_ptr<DelegateInterface> proxy)112     void SetDelegateProxy(std::shared_ptr<DelegateInterface> proxy) override
113     {
114         delegateProxy_ = proxy;
115     }
116     void DestroyPointerWindow() override;
117 
118 private:
119     IconStyle GetIconType(MOUSE_ICON mouseIcon);
120     void GetPreferenceKey(std::string &name);
121     void DrawLoadingPointerStyle(const MOUSE_ICON mouseStyle);
122     void DrawRunningPointerAnimate(const MOUSE_ICON mouseStyle);
123     void CreatePointerWindow(int32_t displayId, int32_t physicalX, int32_t physicalY, Direction direction);
124     sptr<OHOS::Surface> GetLayer();
125     sptr<OHOS::SurfaceBuffer> GetSurfaceBuffer(sptr<OHOS::Surface> layer) const;
126     void DoDraw(uint8_t *addr, uint32_t width, uint32_t height, const MOUSE_ICON mouseStyle = MOUSE_ICON::DEFAULT);
127     void DrawPixelmap(OHOS::Rosen::Drawing::Canvas &canvas, const MOUSE_ICON mouseStyle);
128     void DrawManager();
129     void FixCursorPosition(int32_t &physicalX, int32_t &physicalY);
130     std::shared_ptr<OHOS::Media::PixelMap> DecodeImageToPixelMap(const std::string &imagePath);
131     void UpdatePointerVisible();
132     int32_t UpdateDefaultPointerStyle(int32_t pid, int32_t windowId, PointerStyle style, bool isUiExtension = false);
133     void CheckMouseIconPath();
134     void InitStyle();
135     int32_t InitLayer(const MOUSE_ICON mouseStyle);
136     int32_t SetPointerStylePreference(PointerStyle pointerStyle);
137     void UpdateMouseStyle();
138     int32_t UpdateCursorProperty(void* pixelMap, const int32_t &focusX, const int32_t &focusY);
139     void RotateDegree(Direction direction);
140     int32_t DrawMovePointer(int32_t displayId, int32_t physicalX, int32_t physicalY,
141         PointerStyle pointerStyle, Direction direction);
142     void AdjustMouseFocusByDirection0(ICON_TYPE iconType, int32_t &physicalX, int32_t &physicalY);
143     void AdjustMouseFocusByDirection90(ICON_TYPE iconType, int32_t &physicalX, int32_t &physicalY);
144     void AdjustMouseFocusByDirection180(ICON_TYPE iconType, int32_t &physicalX, int32_t &physicalY);
145     void AdjustMouseFocusByDirection270(ICON_TYPE iconType, int32_t &physicalX, int32_t &physicalY);
146     void CreateMagicCursorChangeObserver();
147     int32_t CreatePointerSwitchObserver(isMagicCursor& item);
148     void UpdateStyleOptions();
149     int32_t GetIndependentPixels();
150     bool IsWindowRotation();
151     bool CheckPointerStyleParam(int32_t windowId, PointerStyle pointerStyle);
152     std::map<MOUSE_ICON, IconStyle>& GetMouseIcons();
153     void UpdateIconPath(const MOUSE_ICON mouseStyle, std::string iconPath);
154     std::shared_ptr<Rosen::Drawing::ColorSpace> ConvertToColorSpace(Media::ColorSpace colorSpace);
155     Rosen::Drawing::ColorType PixelFormatToColorType(Media::PixelFormat pixelFormat);
156     Rosen::Drawing::AlphaType AlphaTypeToAlphaType(Media::AlphaType alphaType);
157     std::shared_ptr<Rosen::Drawing::Image> ExtractDrawingImage(std::shared_ptr<Media::PixelMap> pixelMap);
158     void DrawImage(OHOS::Rosen::Drawing::Canvas &canvas, MOUSE_ICON mouseStyle);
159     bool SetHardWareLocation(int32_t displayId, int32_t physicalX, int32_t physicalY);
160 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR
161     void SetPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap);
162 #endif // OHOS_BUILD_ENABLE_MAGICCURSOR
163     void ForceClearPointerVisiableStatus() override;
164     void UpdateSurfaceNodeBounds(int32_t physicalX, int32_t physicalY);
165 
166 private:
167     struct PidInfo {
168         int32_t pid { 0 };
169         bool visible { false };
170     };
171     bool hasDisplay_ { false };
172     DisplayInfo displayInfo_ {};
173     bool hasPointerDevice_ { false };
174     int32_t lastPhysicalX_ { -1 };
175     int32_t lastPhysicalY_ { -1 };
176     PointerStyle lastMouseStyle_ {};
177     PointerStyle currentMouseStyle_ {};
178     PointerStyle lastDrawPointerStyle_ {};
179     int32_t pid_ { 0 };
180     int32_t windowId_ { 0 };
181     int32_t imageWidth_ { 0 };
182     int32_t imageHeight_ { 0 };
183     int32_t canvasWidth_ { 64 };
184     int32_t canvasHeight_ { 64 };
185     std::map<MOUSE_ICON, IconStyle> mouseIcons_;
186     std::list<PidInfo> pidInfos_;
187     std::list<PidInfo> hapPidInfos_;
188     bool mouseDisplayState_ { false };
189     bool mouseIconUpdate_ { false };
190     std::shared_ptr<OHOS::Media::PixelMap> userIcon_ { nullptr };
191     uint64_t screenId_ { 0 };
192     std::shared_ptr<Rosen::RSSurfaceNode> surfaceNode_;
193     std::shared_ptr<Rosen::RSCanvasNode> canvasNode_;
194     int32_t userIconHotSpotX_ { 0 };
195     int32_t userIconHotSpotY_ { 0 };
196     int32_t tempPointerColor_ { -1 };
197     Direction lastDirection_ { DIRECTION0 };
198     Direction currentDirection_ { DIRECTION0 };
199     isMagicCursor hasMagicCursor_;
200     bool hasInitObserver_ { false };
201     bool isInit_ { false };
202 #ifdef OHOS_BUILD_ENABLE_HARDWARE_CURSOR
203     std::shared_ptr<HardwareCursorPointerManager> hardwareCursorPointerManager_ { nullptr };
204 #endif // OHOS_BUILD_ENABLE_HARDWARE_CURSOR
205 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR
206     std::shared_ptr<OHOS::Media::PixelMap> pixelMap_ { nullptr };
207 #endif // OHOS_BUILD_ENABLE_MAGICCURSOR
208     std::shared_ptr<DelegateInterface> delegateProxy_ { nullptr };
209 };
210 } // namespace MMI
211 } // namespace OHOS
212 #endif // POINTER_DRAWING_MANAGER_H
213