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 int32_t GetCursorSurfaceId(uint64_t &surfaceId) override; 86 void DrawPointerStyle(const PointerStyle& pointerStyle) override; 87 bool IsPointerVisible() override; 88 void SetPointerLocation(int32_t x, int32_t y) override; 89 void AdjustMouseFocus(Direction direction, ICON_TYPE iconType, int32_t &physicalX, int32_t &physicalY); 90 void SetMouseDisplayState(bool state) override; 91 bool GetMouseDisplayState() const override; 92 int32_t SetCustomCursor(void* pixelMap, int32_t pid, int32_t windowId, int32_t focusX, int32_t focusY) override; 93 int32_t SetCustomCursor(int32_t pid, int32_t windowId, CustomCursor cursor, CursorOptions options) override; 94 int32_t SetMouseIcon(int32_t pid, int32_t windowId, void* pixelMap) override; 95 int32_t SetMouseHotSpot(int32_t pid, int32_t windowId, int32_t hotSpotX, int32_t hotSpotY) override; 96 PointerStyle GetLastMouseStyle() override; 97 std::map<MOUSE_ICON, IconStyle> GetMouseIconPath() override; 98 IconStyle GetIconStyle(const MOUSE_ICON mouseStyle); 99 bool HasMagicCursor(); 100 int32_t DrawCursor(const MOUSE_ICON mouseStyle); 101 int32_t SwitchPointerStyle() override; 102 void DrawMovePointer(int32_t displayId, int32_t physicalX, int32_t physicalY) override; 103 void Dump(int32_t fd, const std::vector<std::string> &args) override; 104 void AttachToDisplay(); 105 int32_t EnableHardwareCursorStats(int32_t pid, bool enable) override; 106 int32_t GetHardwareCursorStats(int32_t pid, uint32_t &frameCount, uint32_t &vsyncCount) override; 107 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR 108 int32_t GetPointerSnapshot(void *pixelMapPtr) override; 109 #endif // OHOS_BUILD_ENABLE_MAGICCURSOR 110 void InitPointerCallback() override; 111 void InitPointerObserver() override; 112 void OnSessionLost(int32_t pid) override; 113 int32_t SkipPointerLayer(bool isSkip) override; SetDelegateProxy(std::shared_ptr<DelegateInterface> proxy)114 void SetDelegateProxy(std::shared_ptr<DelegateInterface> proxy) override 115 { 116 delegateProxy_ = proxy; 117 } 118 void DestroyPointerWindow() override; 119 120 private: 121 IconStyle GetIconType(MOUSE_ICON mouseIcon); 122 void GetPreferenceKey(std::string &name); 123 void DrawLoadingPointerStyle(const MOUSE_ICON mouseStyle); 124 void DrawRunningPointerAnimate(const MOUSE_ICON mouseStyle); 125 void CreatePointerWindow(int32_t displayId, int32_t physicalX, int32_t physicalY, Direction direction); 126 sptr<OHOS::Surface> GetLayer(); 127 sptr<OHOS::SurfaceBuffer> GetSurfaceBuffer(sptr<OHOS::Surface> layer) const; 128 void DoDraw(uint8_t *addr, uint32_t width, uint32_t height, const MOUSE_ICON mouseStyle = MOUSE_ICON::DEFAULT); 129 void DrawPixelmap(OHOS::Rosen::Drawing::Canvas &canvas, const MOUSE_ICON mouseStyle); 130 void DrawManager(); 131 void FixCursorPosition(int32_t &physicalX, int32_t &physicalY); 132 std::shared_ptr<OHOS::Media::PixelMap> DecodeImageToPixelMap(const std::string &imagePath); 133 void UpdatePointerVisible(); 134 int32_t UpdateDefaultPointerStyle(int32_t pid, int32_t windowId, PointerStyle style, bool isUiExtension = false); 135 void CheckMouseIconPath(); 136 void InitStyle(); 137 int32_t InitLayer(const MOUSE_ICON mouseStyle); 138 int32_t SetPointerStylePreference(PointerStyle pointerStyle); 139 void UpdateMouseStyle(); 140 int32_t UpdateCursorProperty(void* pixelMap, const int32_t &focusX, const int32_t &focusY); 141 int32_t UpdateCursorProperty(CustomCursor cursor); 142 void RotateDegree(Direction direction); 143 int32_t DrawMovePointer(int32_t displayId, int32_t physicalX, int32_t physicalY, 144 PointerStyle pointerStyle, Direction direction); 145 void AdjustMouseFocusByDirection0(ICON_TYPE iconType, int32_t &physicalX, int32_t &physicalY); 146 void AdjustMouseFocusByDirection90(ICON_TYPE iconType, int32_t &physicalX, int32_t &physicalY); 147 void AdjustMouseFocusByDirection180(ICON_TYPE iconType, int32_t &physicalX, int32_t &physicalY); 148 void AdjustMouseFocusByDirection270(ICON_TYPE iconType, int32_t &physicalX, int32_t &physicalY); 149 void CreateMagicCursorChangeObserver(); 150 int32_t CreatePointerSwitchObserver(isMagicCursor& item); 151 void UpdateStyleOptions(); 152 int32_t GetIndependentPixels(); 153 bool IsWindowRotation(); 154 bool CheckPointerStyleParam(int32_t windowId, PointerStyle pointerStyle); 155 std::map<MOUSE_ICON, IconStyle>& GetMouseIcons(); 156 void UpdateIconPath(const MOUSE_ICON mouseStyle, std::string iconPath); 157 std::shared_ptr<Rosen::Drawing::ColorSpace> ConvertToColorSpace(Media::ColorSpace colorSpace); 158 Rosen::Drawing::ColorType PixelFormatToColorType(Media::PixelFormat pixelFormat); 159 Rosen::Drawing::AlphaType AlphaTypeToAlphaType(Media::AlphaType alphaType); 160 std::shared_ptr<Rosen::Drawing::Image> ExtractDrawingImage(std::shared_ptr<Media::PixelMap> pixelMap); 161 void DrawImage(OHOS::Rosen::Drawing::Canvas &canvas, MOUSE_ICON mouseStyle); 162 bool SetHardWareLocation(int32_t displayId, int32_t physicalX, int32_t physicalY); 163 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR 164 void SetPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap); 165 #endif // OHOS_BUILD_ENABLE_MAGICCURSOR 166 void ForceClearPointerVisiableStatus() override; 167 void UpdateSurfaceNodeBounds(int32_t physicalX, int32_t physicalY); 168 std::shared_ptr<OHOS::Media::PixelMap> GetUserIconCopy(); 169 170 private: 171 struct PidInfo { 172 int32_t pid { 0 }; 173 bool visible { false }; 174 }; 175 bool hasDisplay_ { false }; 176 DisplayInfo displayInfo_ {}; 177 bool hasPointerDevice_ { false }; 178 int32_t lastPhysicalX_ { -1 }; 179 int32_t lastPhysicalY_ { -1 }; 180 PointerStyle lastMouseStyle_ {}; 181 PointerStyle currentMouseStyle_ {}; 182 PointerStyle lastDrawPointerStyle_ {}; 183 int32_t pid_ { 0 }; 184 int32_t windowId_ { 0 }; 185 int32_t imageWidth_ { 0 }; 186 int32_t imageHeight_ { 0 }; 187 int32_t canvasWidth_ { 64 }; 188 int32_t canvasHeight_ { 64 }; 189 int32_t cursorWidth_ { 0 }; 190 int32_t cursorHeight_ { 0 }; 191 std::map<MOUSE_ICON, IconStyle> mouseIcons_; 192 std::list<PidInfo> pidInfos_; 193 std::list<PidInfo> hapPidInfos_; 194 bool mouseDisplayState_ { false }; 195 bool mouseIconUpdate_ { false }; 196 std::shared_ptr<OHOS::Media::PixelMap> userIcon_ { nullptr }; 197 uint64_t screenId_ { 0 }; 198 std::shared_ptr<Rosen::RSSurfaceNode> surfaceNode_; 199 std::shared_ptr<Rosen::RSCanvasNode> canvasNode_; 200 int32_t userIconHotSpotX_ { 0 }; 201 int32_t userIconHotSpotY_ { 0 }; 202 int32_t tempPointerColor_ { -1 }; 203 Direction lastDirection_ { DIRECTION0 }; 204 Direction currentDirection_ { DIRECTION0 }; 205 isMagicCursor hasMagicCursor_; 206 bool hasInitObserver_ { false }; 207 bool isInit_ { false }; 208 std::mutex mtx_; 209 #ifdef OHOS_BUILD_ENABLE_HARDWARE_CURSOR 210 std::shared_ptr<HardwareCursorPointerManager> hardwareCursorPointerManager_ { nullptr }; 211 #endif // OHOS_BUILD_ENABLE_HARDWARE_CURSOR 212 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR 213 std::shared_ptr<OHOS::Media::PixelMap> pixelMap_ { nullptr }; 214 #endif // OHOS_BUILD_ENABLE_MAGICCURSOR 215 std::shared_ptr<DelegateInterface> delegateProxy_ { nullptr }; 216 bool followSystem_ { false }; 217 int32_t focusX_ { 0 }; 218 int32_t focusY_ { 0 }; 219 }; 220 } // namespace MMI 221 } // namespace OHOS 222 #endif // POINTER_DRAWING_MANAGER_H 223