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