1/*
2 * Copyright (c) 2023 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 TOUCH_DRAWING_MANAGER_H
17#define TOUCH_DRAWING_MANAGER_H
18
19#include "draw/canvas.h"
20#include "nocopyable.h"
21#include "singleton.h"
22#include "transaction/rs_transaction.h"
23#include "ui/rs_canvas_node.h"
24#include "ui/rs_surface_node.h"
25#include "utils/rect.h"
26
27#ifndef USE_ROSEN_DRAWING
28#include "pipeline/rs_recording_canvas.h"
29#else
30#include "recording/recording_canvas.h"
31#include "ui/rs_canvas_drawing_node.h"
32#endif // USE_ROSEN_DRAWING
33
34#include "pointer_event.h"
35#include "window_info.h"
36
37namespace OHOS {
38namespace MMI {
39class DelegateInterface;
40class TouchDrawingManager {
41private:
42struct Bubble {
43    int32_t innerCircleRadius { 0 };
44    int32_t outerCircleRadius { 0 };
45    float outerCircleWidth { 0 };
46};
47struct DevMode {
48    std::string SwitchName;
49    bool isShow { false };
50};
51#ifndef USE_ROSEN_DRAWING
52    using RosenCanvas = Rosen::RSRecordingCanvas;
53#else
54    using RosenCanvas = Rosen::Drawing::RecordingCanvas;
55#endif
56
57    DECLARE_DELAYED_SINGLETON(TouchDrawingManager);
58public:
59    DISALLOW_COPY_AND_MOVE(TouchDrawingManager);
60    void TouchDrawHandler(std::shared_ptr<PointerEvent> pointerEvent);
61    int32_t UpdateLabels();
62    void UpdateDisplayInfo(const DisplayInfo& displayInfo);
63    void GetOriginalTouchScreenCoordinates(Direction direction, int32_t width, int32_t height,
64        int32_t &physicalX, int32_t &physicalY);
65    int32_t UpdateBubbleData();
66    void RotationScreen();
67    void Dump(int32_t fd, const std::vector<std::string> &args);
68    bool IsWindowRotation();
69    void SetDelegateProxy(std::shared_ptr<DelegateInterface> proxy)
70    {
71        delegateProxy_ = proxy;
72    }
73    std::pair<int32_t, int32_t> CalcDrawCoordinate(const DisplayInfo& displayInfo,
74        PointerEvent::PointerItem pointerItem);
75private:
76    void CreateObserver();
77    void AddCanvasNode(std::shared_ptr<Rosen::RSCanvasNode>& canvasNode, bool isTrackerNode);
78    void RotationCanvasNode(std::shared_ptr<Rosen::RSCanvasNode> canvasNode);
79    void ResetCanvasNode(std::shared_ptr<Rosen::RSCanvasNode> canvasNode);
80    void RotationCanvas(RosenCanvas *canvas, Direction direction);
81    void CreateTouchWindow();
82    void DestoryTouchWindow();
83    void DrawBubbleHandler();
84    void DrawBubble();
85    void DrawPointerPositionHandler();
86    void DrawTracker(int32_t x, int32_t y, int32_t pointerId);
87    void DrawCrosshairs(RosenCanvas *canvas, int32_t x, int32_t y);
88    void DrawLabels();
89    void DrawRectItem(RosenCanvas* canvas, const std::string &text,
90        Rosen::Drawing::Rect &rect, const Rosen::Drawing::Color &color);
91    void UpdatePointerPosition();
92    void RecordLabelsInfo();
93    void UpdateLastPointerItem(PointerEvent::PointerItem &pointerItem);
94    void RemovePointerPosition();
95    void ClearTracker();
96    void InitLabels();
97    template <class T>
98    void CreateBubbleObserver(T& item);
99    template <class T>
100    void CreatePointerObserver(T& item);
101    template <class T>
102    std::string FormatNumber(T number, int32_t precision);
103    bool IsValidAction(const int32_t action);
104    void Snapshot();
105private:
106    std::shared_ptr<Rosen::RSSurfaceNode> surfaceNode_ { nullptr };
107    std::shared_ptr<Rosen::RSCanvasNode> bubbleCanvasNode_ { nullptr };
108    std::shared_ptr<Rosen::RSCanvasNode> trackerCanvasNode_ { nullptr };
109    std::shared_ptr<Rosen::RSCanvasNode> crosshairCanvasNode_ { nullptr };
110    std::shared_ptr<Rosen::RSCanvasNode> labelsCanvasNode_ { nullptr };
111    DisplayInfo displayInfo_ {};
112    Bubble bubble_;
113    Rosen::Drawing::Point firstPt_;
114    Rosen::Drawing::Point currentPt_;
115    Rosen::Drawing::Point lastPt_;
116    DevMode bubbleMode_;
117    DevMode pointerMode_;
118    int32_t currentPointerId_ { 0 };
119    int32_t maxPointerCount_ { 0 };
120    int32_t currentPointerCount_ { 0 };
121    int32_t rectTopPosition_ { 0 };
122    int32_t scaleW_ { 0 };
123    int32_t scaleH_ { 0 };
124    int64_t lastActionTime_ { 0 };
125    double xVelocity_ { 0.0 };
126    double yVelocity_ { 0.0 };
127    double pressure_ { 0.0 };
128    double itemRectW_ { 0.0 };
129    bool hasBubbleObserver_{ false };
130    bool hasPointerObserver_{ false };
131    bool isFirstDownAction_ { false };
132    bool isDownAction_ { false };
133    bool isFirstDraw_ { true };
134    bool isChangedRotation_ { false };
135    bool isChangedMode_ { false };
136    bool stopRecord_ { false };
137    std::shared_ptr<PointerEvent> pointerEvent_ { nullptr };
138    std::shared_ptr<DelegateInterface> delegateProxy_ {nullptr};
139    std::list<PointerEvent::PointerItem> lastPointerItem_ { };
140    std::mutex mutex_;
141};
142#define TOUCH_DRAWING_MGR ::OHOS::DelayedSingleton<TouchDrawingManager>::GetInstance()
143} // namespace MMI
144} // namespace OHOS
145#endif // TOUCH_DRAWING_MANAGER_H
146