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 "common/rs_thread_handler.h" 23#include "draw/canvas.h" 24#include "event_handler.h" 25#include "nocopyable.h" 26#include "pixel_map.h" 27#include "transaction/rs_transaction.h" 28#include "transaction/rs_interfaces.h" 29#include "ui/rs_canvas_node.h" 30#include "ui/rs_surface_node.h" 31#include "window.h" 32 33#include "device_observer.h" 34#ifdef OHOS_BUILD_ENABLE_HARDWARE_CURSOR 35#include "hardware_cursor_pointer_manager.h" 36#endif // OHOS_BUILD_ENABLE_HARDWARE_CURSOR 37#include "i_pointer_drawing_manager.h" 38#include "mouse_event_normalize.h" 39#include "setting_observer.h" 40#include "struct_multimodal.h" 41 42namespace OHOS { 43namespace MMI { 44namespace { 45constexpr int32_t DEFAULT_FRAME_RATE { 30 }; 46constexpr int32_t INVALID_DISPLAY_ID { -1 }; 47constexpr float HARDWARE_CANVAS_SIZE { 512.0f }; 48} // namespace 49 50struct isMagicCursor { 51 std::string name; 52 bool isShow { false }; 53}; 54 55struct PixelMapReleaseContext { 56 explicit PixelMapReleaseContext(std::shared_ptr<Media::PixelMap> pixelMap) : pixelMap_(pixelMap) {} 57 58 ~PixelMapReleaseContext() 59 { 60 pixelMap_ = nullptr; 61 } 62 63private: 64 std::shared_ptr<Media::PixelMap> pixelMap_ { nullptr }; 65}; 66class DelegateInterface; 67class PointerDrawingManager final : public IPointerDrawingManager, 68 public IDeviceObserver, 69 public std::enable_shared_from_this<PointerDrawingManager> { 70public: 71 PointerDrawingManager(); 72 DISALLOW_COPY_AND_MOVE(PointerDrawingManager); 73 ~PointerDrawingManager(); 74 void DrawPointer(int32_t displayId, int32_t physicalX, int32_t physicalY, 75 const PointerStyle pointerStyle, Direction direction) override; 76 void UpdateDisplayInfo(const DisplayInfo& displayInfo) override; 77 void OnDisplayInfo(const DisplayGroupInfo& displayGroupInfo) override; 78 void OnWindowInfo(const WinInfo &info) override; 79 void UpdatePointerDevice(bool hasPointerDevice, bool isPointerVisible, bool isHotPlug) override; 80 bool Init() override; 81 int32_t SetPointerColor(int32_t color) override; 82 int32_t GetPointerColor() override; 83 void DeletePointerVisible(int32_t pid) override; 84 int32_t SetPointerVisible(int32_t pid, bool visible, int32_t priority, bool isHap) override; 85 bool GetPointerVisible(int32_t pid) override; 86 int32_t SetPointerStyle(int32_t pid, int32_t windowId, PointerStyle pointerStyle, 87 bool isUiExtension = false) override; 88 int32_t ClearWindowPointerStyle(int32_t pid, int32_t windowId) override; 89 int32_t GetPointerStyle(int32_t pid, int32_t windowId, PointerStyle &pointerStyle, 90 bool isUiExtension = false) override; 91 int32_t SetPointerSize(int32_t size) override; 92 int32_t GetPointerSize() override; 93 void DrawPointerStyle(const PointerStyle& pointerStyle) override; 94 bool IsPointerVisible() override; 95 void SetPointerLocation(int32_t x, int32_t y) override; 96 void AdjustMouseFocus(Direction direction, ICON_TYPE iconType, int32_t &physicalX, int32_t &physicalY); 97 void SetMouseDisplayState(bool state) override; 98 bool GetMouseDisplayState() const override; 99 int32_t SetCustomCursor(void* pixelMap, int32_t pid, int32_t windowId, int32_t focusX, int32_t focusY) override; 100 int32_t SetMouseIcon(int32_t pid, int32_t windowId, void* pixelMap) override; 101 int32_t SetMouseHotSpot(int32_t pid, int32_t windowId, int32_t hotSpotX, int32_t hotSpotY) override; 102 PointerStyle GetLastMouseStyle() override; 103 const std::map<MOUSE_ICON, IconStyle>& GetMouseIconPath() override; 104 IconStyle GetIconStyle(const MOUSE_ICON mouseStyle) override; 105 bool HasMagicCursor(); 106 int32_t DrawCursor(const MOUSE_ICON mouseStyle); 107 int32_t SwitchPointerStyle() override; 108 void DrawMovePointer(int32_t displayId, int32_t physicalX, int32_t physicalY) override; 109 void Dump(int32_t fd, const std::vector<std::string> &args) override; 110 void AttachToDisplay(); 111 int32_t EnableHardwareCursorStats(int32_t pid, bool enable) override; 112 int32_t GetHardwareCursorStats(int32_t pid, uint32_t &frameCount, uint32_t &vsyncCount) override; 113#ifdef OHOS_BUILD_ENABLE_MAGICCURSOR 114 int32_t GetPointerSnapshot(void *pixelMapPtr) override; 115#endif // OHOS_BUILD_ENABLE_MAGICCURSOR 116 void InitPointerCallback() override; 117 void InitPointerObserver() override; 118 void OnSessionLost(int32_t pid) override; 119 int32_t SkipPointerLayer(bool isSkip) override; 120 void SetDelegateProxy(std::shared_ptr<DelegateInterface> proxy) override 121 { 122 delegateProxy_ = proxy; 123 } 124 void DestroyPointerWindow() override; 125 void DrawScreenCenterPointer(const PointerStyle& pointerStyle) override; 126 127private: 128 IconStyle GetIconType(MOUSE_ICON mouseIcon); 129 void GetPreferenceKey(std::string &name); 130 void DrawLoadingPointerStyle(const MOUSE_ICON mouseStyle); 131 void DrawRunningPointerAnimate(const MOUSE_ICON mouseStyle); 132 void CreatePointerWindow(int32_t displayId, int32_t physicalX, int32_t physicalY, Direction direction); 133 sptr<OHOS::Surface> GetLayer(); 134 sptr<OHOS::SurfaceBuffer> GetSurfaceBuffer(sptr<OHOS::Surface> layer) const; 135 bool RetryGetSurfaceBuffer(sptr<OHOS::SurfaceBuffer> buffer, sptr<OHOS::Surface> layer); 136 void DoDraw(uint8_t *addr, uint32_t width, uint32_t height, const MOUSE_ICON mouseStyle = MOUSE_ICON::DEFAULT); 137 void DrawPixelmap(OHOS::Rosen::Drawing::Canvas &canvas, const MOUSE_ICON mouseStyle); 138 void DrawManager(); 139 void FixCursorPosition(int32_t &physicalX, int32_t &physicalY); 140 std::shared_ptr<OHOS::Media::PixelMap> DecodeImageToPixelMap(const std::string &imagePath); 141 void UpdatePointerVisible(); 142 int32_t UpdateDefaultPointerStyle(int32_t pid, int32_t windowId, PointerStyle style, bool isUiExtension = false); 143 void CheckMouseIconPath(); 144 void InitStyle(); 145 int32_t InitLayer(const MOUSE_ICON mouseStyle); 146 int32_t SetPointerStylePreference(PointerStyle pointerStyle); 147 void UpdateMouseStyle(); 148 int32_t UpdateCursorProperty(void* pixelMap, const int32_t &focusX, const int32_t &focusY); 149 void RotateDegree(Direction direction); 150 int32_t DrawMovePointer(int32_t displayId, int32_t physicalX, int32_t physicalY, 151 PointerStyle pointerStyle, Direction direction); 152 void AdjustMouseFocusByDirection0(ICON_TYPE iconType, int32_t &physicalX, int32_t &physicalY); 153 void AdjustMouseFocusByDirection90(ICON_TYPE iconType, int32_t &physicalX, int32_t &physicalY); 154 void AdjustMouseFocusByDirection180(ICON_TYPE iconType, int32_t &physicalX, int32_t &physicalY); 155 void AdjustMouseFocusByDirection270(ICON_TYPE iconType, int32_t &physicalX, int32_t &physicalY); 156 void CreateMagicCursorChangeObserver(); 157 int32_t CreatePointerSwitchObserver(isMagicCursor& item); 158 void UpdateStyleOptions(); 159 int32_t GetIndependentPixels(); 160 bool IsWindowRotation(); 161 bool CheckPointerStyleParam(int32_t windowId, PointerStyle pointerStyle); 162 std::map<MOUSE_ICON, IconStyle>& GetMouseIcons(); 163 void UpdateIconPath(const MOUSE_ICON mouseStyle, std::string iconPath); 164 std::shared_ptr<Rosen::Drawing::ColorSpace> ConvertToColorSpace(Media::ColorSpace colorSpace); 165 Rosen::Drawing::ColorType PixelFormatToColorType(Media::PixelFormat pixelFormat); 166 Rosen::Drawing::AlphaType AlphaTypeToAlphaType(Media::AlphaType alphaType); 167 std::shared_ptr<Rosen::Drawing::Image> ExtractDrawingImage(std::shared_ptr<Media::PixelMap> pixelMap); 168 void DrawImage(OHOS::Rosen::Drawing::Canvas &canvas, MOUSE_ICON mouseStyle); 169#ifdef OHOS_BUILD_ENABLE_MAGICCURSOR 170 void SetPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap); 171#endif // OHOS_BUILD_ENABLE_MAGICCURSOR 172 void ForceClearPointerVisiableStatus() override; 173 int32_t UpdateSurfaceNodeBounds(int32_t physicalX, int32_t physicalY); 174 void CreateCanvasNode(); 175 void SetSurfaceNodeVisible(bool visible); 176 float CalculateHardwareXOffset(ICON_TYPE iconType); 177 float CalculateHardwareYOffset(ICON_TYPE iconType); 178 bool SetTraditionsHardWareCursorLocation(int32_t displayId, int32_t physicalX, int32_t physicalY, 179 ICON_TYPE iconType); 180 void SetHardwareCursorPosition(int32_t displayId, int32_t physicalX, int32_t physicalY, 181 PointerStyle pointerStyle); 182 void CreateDynamicCanvas(); 183 int32_t ParsingDynamicImage(MOUSE_ICON mouseStyle); 184 void DrawDynamicImage(OHOS::Rosen::Drawing::Canvas &canvas, MOUSE_ICON mouseStyle); 185#ifdef OHOS_BUILD_ENABLE_HARDWARE_CURSOR 186 bool SetDynamicHardWareCursorLocation(int32_t physicalX, int32_t physicalY, MOUSE_ICON mouseStyle); 187 void RenderThreadLoop(); 188 int32_t RequestNextVSync(); 189 void OnVsync(uint64_t timestamp); 190 void PostTask(Rosen::RSTaskMessage::RSTask task); 191 void DoHardwareCursorDraw(); 192 int32_t FlushBuffer(); 193 int32_t GetSurfaceInformation(); 194 void UpdateBindDisplayId(int32_t displayId); 195 void PostTaskRSLocation(int32_t physicalX, int32_t physicalY, std::shared_ptr<Rosen::RSSurfaceNode> surfaceNode); 196 void DrawTraditionsCursor(MOUSE_ICON mouseStyle); 197 int32_t InitVsync(MOUSE_ICON mouseStyle); 198#endif // OHOS_BUILD_ENABLE_HARDWARE_CURSOR 199 200private: 201 struct PidInfo { 202 int32_t pid { 0 }; 203 bool visible { false }; 204 }; 205 bool hasDisplay_ { false }; 206 DisplayInfo displayInfo_ {}; 207 bool hasPointerDevice_ { false }; 208 int32_t lastPhysicalX_ { -1 }; 209 int32_t lastPhysicalY_ { -1 }; 210 PointerStyle lastMouseStyle_ {}; 211 PointerStyle currentMouseStyle_ {}; 212 PointerStyle lastDrawPointerStyle_ {}; 213 int32_t pid_ { 0 }; 214 int32_t windowId_ { 0 }; 215 int32_t imageWidth_ { 0 }; 216 int32_t imageHeight_ { 0 }; 217 int32_t canvasWidth_ = 64; 218 int32_t canvasHeight_ = 64; 219 int32_t cursorWidth_ { 0 }; 220 int32_t cursorHeight_ { 0 }; 221 std::map<MOUSE_ICON, IconStyle> mouseIcons_; 222 std::list<PidInfo> pidInfos_; 223 std::list<PidInfo> hapPidInfos_; 224 bool mouseDisplayState_ { false }; 225 bool mouseIconUpdate_ { false }; 226 std::shared_ptr<OHOS::Media::PixelMap> userIcon_ { nullptr }; 227 uint64_t screenId_ { 0 }; 228 std::shared_ptr<Rosen::RSSurfaceNode> surfaceNode_; 229 std::shared_ptr<Rosen::RSCanvasNode> canvasNode_; 230 int32_t userIconHotSpotX_ { 0 }; 231 int32_t userIconHotSpotY_ { 0 }; 232 int32_t tempPointerColor_ { -1 }; 233 Direction lastDirection_ { DIRECTION0 }; 234 Direction currentDirection_ { DIRECTION0 }; 235 isMagicCursor hasMagicCursor_; 236 bool hasInitObserver_ { false }; 237 std::atomic<bool> hasHardwareCursorAnimate_ { false }; 238 std::atomic<bool> hasLoadingPointerStyle_ { false }; 239 int32_t frameCount_ { DEFAULT_FRAME_RATE }; 240 int32_t currentFrame_ { 0 }; 241 sptr<OHOS::Surface> layer_ { nullptr }; 242 sptr<OHOS::SurfaceBuffer> buffer_ { nullptr }; 243 std::shared_ptr<uint8_t *> addr_ { nullptr }; 244 int32_t displayId_ { INVALID_DISPLAY_ID }; 245 std::shared_ptr<OHOS::Rosen::Drawing::Bitmap> dynamicBitmap_ { nullptr }; 246 std::shared_ptr<OHOS::Rosen::Drawing::Canvas> dynamicCanvas_ { nullptr }; 247 std::shared_ptr<Rosen::Drawing::Image> runningRightImage_ { nullptr }; 248 std::shared_ptr<Rosen::Drawing::Image> image_ { nullptr }; 249 std::shared_ptr<AppExecFwk::EventRunner> runner_ { nullptr }; 250 std::shared_ptr<AppExecFwk::EventHandler> handler_ { nullptr }; 251 std::shared_ptr<Rosen::VSyncReceiver> receiver_ { nullptr }; 252 std::atomic<bool> isRenderRuning_{ false }; 253 std::unique_ptr<std::thread> renderThread_ { nullptr }; 254 bool isInit_ { false }; 255#ifdef OHOS_BUILD_ENABLE_HARDWARE_CURSOR 256 std::shared_ptr<HardwareCursorPointerManager> hardwareCursorPointerManager_ { nullptr }; 257#endif // OHOS_BUILD_ENABLE_HARDWARE_CURSOR 258 float hardwareCanvasSize_ { HARDWARE_CANVAS_SIZE }; 259#ifdef OHOS_BUILD_ENABLE_MAGICCURSOR 260 std::shared_ptr<OHOS::Media::PixelMap> pixelMap_ { nullptr }; 261#endif // OHOS_BUILD_ENABLE_MAGICCURSOR 262 std::shared_ptr<DelegateInterface> delegateProxy_ { nullptr }; 263 int32_t lastDisplayId_ { DEFAULT_DISPLAY_ID }; 264}; 265} // namespace MMI 266} // namespace OHOS 267#endif // POINTER_DRAWING_MANAGER_H 268