1 /*
2  * Copyright (c) 2023-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 DRAG_DRAWING_H
17 #define DRAG_DRAWING_H
18 
19 #include <vector>
20 
21 #include "display_manager.h"
22 #include "event_handler.h"
23 #include "event_runner.h"
24 #include "json_parser.h"
25 #include "libxml/tree.h"
26 #include "libxml/parser.h"
27 #include "modifier/rs_extended_modifier.h"
28 #include "modifier/rs_modifier.h"
29 #include "vsync_receiver.h"
30 
31 #include "drag_data.h"
32 #include "drag_smooth_processor.h"
33 #include "drag_vsync_station.h"
34 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
35 #include "i_context.h"
36 #endif // OHOS_BUILD_ENABLE_ARKUI_X
37 #include "i_drag_animation.h"
38 #ifdef OHOS_BUILD_ENABLE_ARKUI_X
39 #include "pointer_style.h"
40 #include "virtual_rs_window.h"
41 #endif // OHOS_BUILD_ENABLE_ARKUI_X
42 
43 namespace OHOS {
44 namespace Msdp {
45 namespace DeviceStatus {
46 struct DrawingInfo;
47 class DragDrawing;
48 using DragStartExtFunc = void (*)(DragData &dragData);
49 using DragNotifyExtFunc = void (*)(DragEventInfo &dragEventInfo);
50 class DrawSVGModifier : public Rosen::RSContentStyleModifier {
51 public:
DrawSVGModifier(std::shared_ptr<Media::PixelMap> stylePixelMap)52     explicit DrawSVGModifier(std::shared_ptr<Media::PixelMap> stylePixelMap) : stylePixelMap_(stylePixelMap) {}
53     ~DrawSVGModifier() = default;
54     void Draw(Rosen::RSDrawingContext& context) const override;
55 
56 private:
57     std::shared_ptr<Media::PixelMap> stylePixelMap_ { nullptr };
58 };
59 
60 class DrawPixelMapModifier : public Rosen::RSContentStyleModifier {
61 public:
62     DrawPixelMapModifier() = default;
63     ~DrawPixelMapModifier() = default;
64     void SetDragShadow(std::shared_ptr<Rosen::RSCanvasNode> pixelMapNode) const;
65     void SetTextDragShadow(std::shared_ptr<Rosen::RSCanvasNode> pixelMapNode) const;
66     Rosen::SHADOW_COLOR_STRATEGY ConvertShadowColorStrategy(int32_t shadowColorStrategy) const;
67     void Draw(Rosen::RSDrawingContext &context) const override;
68 };
69 
70 class DrawMouseIconModifier : public Rosen::RSContentStyleModifier {
71 public:
DrawMouseIconModifier(MMI::PointerStyle pointerStyle)72     explicit DrawMouseIconModifier(MMI::PointerStyle pointerStyle) : pointerStyle_(pointerStyle) {}
73     ~DrawMouseIconModifier() = default;
74     void Draw(Rosen::RSDrawingContext &context) const override;
75 
76 private:
77     void OnDraw(std::shared_ptr<Media::PixelMap> pixelMap) const;
78     std::shared_ptr<Media::PixelMap> DrawFromSVG() const;
79 
80 private:
81     MMI::PointerStyle pointerStyle_;
82 };
83 
84 class DrawDynamicEffectModifier : public Rosen::RSContentStyleModifier {
85 public:
86     DrawDynamicEffectModifier() = default;
87     ~DrawDynamicEffectModifier() = default;
88     void Draw(Rosen::RSDrawingContext &context) const override;
89     void SetAlpha(float alpha);
90     void SetScale(float scale);
91 
92 private:
93     std::shared_ptr<Rosen::RSAnimatableProperty<float>> alpha_ { nullptr };
94     std::shared_ptr<Rosen::RSAnimatableProperty<float>> scale_ { nullptr };
95 };
96 
97 class DrawDragStopModifier : public Rosen::RSContentStyleModifier {
98 public:
99     DrawDragStopModifier() = default;
100     ~DrawDragStopModifier() = default;
101     void Draw(Rosen::RSDrawingContext &context) const override;
102     void SetAlpha(float alpha);
103     void SetScale(float scale);
104     void SetStyleScale(float scale);
105     void SetStyleAlpha(float alpha);
106 
107 private:
108     std::shared_ptr<Rosen::RSAnimatableProperty<float>> alpha_ { nullptr };
109     std::shared_ptr<Rosen::RSAnimatableProperty<float>> scale_ { nullptr };
110     std::shared_ptr<Rosen::RSAnimatableProperty<float>> styleScale_ { nullptr };
111     std::shared_ptr<Rosen::RSAnimatableProperty<float>> styleAlpha_ { nullptr };
112 };
113 
114 class DrawStyleChangeModifier : public Rosen::RSContentStyleModifier {
115 public:
116     DrawStyleChangeModifier() = default;
DrawStyleChangeModifier(std::shared_ptr<Media::PixelMap> stylePixelMap)117     explicit DrawStyleChangeModifier(std::shared_ptr<Media::PixelMap> stylePixelMap) : stylePixelMap_(stylePixelMap) {}
118     ~DrawStyleChangeModifier() = default;
119     void Draw(Rosen::RSDrawingContext &context) const override;
120     void SetScale(float scale);
121 
122 private:
123     std::shared_ptr<Media::PixelMap> stylePixelMap_ { nullptr };
124     std::shared_ptr<Rosen::RSAnimatableProperty<float>> scale_ { nullptr };
125 };
126 
127 class DrawStyleScaleModifier : public Rosen::RSContentStyleModifier {
128 public:
129     DrawStyleScaleModifier() = default;
130     ~DrawStyleScaleModifier() = default;
131     void Draw(Rosen::RSDrawingContext &context) const override;
132     void SetScale(float scale);
133 
134 private:
135     std::shared_ptr<Rosen::RSAnimatableProperty<float>> scale_ { nullptr };
136 };
137 
138 struct FilterInfo {
139     std::string dragType;
140     bool shadowEnable { false };
141     bool shadowIsFilled { false };
142     bool shadowMask { false };
143     int32_t shadowColorStrategy { 0 };
144     float shadowCorner { 0.0F };
145     float dipScale { 0.0f };
146     float scale { 1.0f };
147     float cornerRadius1 { 0.0f };
148     float cornerRadius2 { 0.0f };
149     float cornerRadius3 { 0.0f };
150     float cornerRadius4 { 0.0f };
151     float opacity { 0.95f };
152     float offsetX { 0.0f };
153     float offsetY { 0.0f };
154     uint32_t argb { 0 };
155     std::string path;
156     float elevation { 0.0f };
157     bool isHardwareAcceleration { false };
158     Rosen::Vector2f coef;
159     float blurRadius { -1.0f };
160     float blurStaturation { -1.0f };
161     float blurBrightness { -1.0f };
162     uint32_t blurColor { 0 };
163     int32_t blurStyle { -1 };
164     float dragNodeGrayscale { 0.0f };
165 };
166 
167 struct ExtraInfo {
168     std::string componentType;
169     int32_t blurStyle { -1 };
170     float cornerRadius { 0.0f };
171     bool allowDistributed { true };
172     Rosen::Vector2f coef;
173 };
174 
175 enum class ScreenSizeType {
176     // Undefined screen width
177     UNDEFINED = 0,
178     // Screen width size is XS, grids is 2
179     XS,
180     // Screen width size is SM, grids is 4
181     SM,
182     // Screen width size is MD, grids is 8
183     MD,
184     // Screen width size is LG, grids is 12
185     LG,
186     // Screen width size is XL, maxi number of grids
187     XL,
188 };
189 
190 struct DrawingInfo {
191     std::atomic_bool isRunning { false };
192     std::atomic_bool isPreviousDefaultStyle { false };
193     std::atomic_bool isCurrentDefaultStyle { false };
194     bool isInitUiDirector { true };
195     bool isExistScalingValue { false };
196     std::atomic_bool needDestroyDragWindow { false };
197     int32_t sourceType { -1 };
198     int32_t currentDragNum { -1 };
199     DragCursorStyle currentStyle { DragCursorStyle::DEFAULT };
200     int32_t displayId { -1 };
201     int32_t pixelMapX { -1 };
202     int32_t pixelMapY { -1 };
203     int32_t displayX { -1 };
204     int32_t displayY { -1 };
205     float x { -1.0f };
206     float y { -1.0f };
207     float currentPositionX { -1.0f };
208     float currentPositionY { -1.0f };
209     int32_t mouseWidth { 0 };
210     int32_t mouseHeight { 0 };
211     int32_t rootNodeWidth { -1 };
212     int32_t rootNodeHeight { -1 };
213     std::atomic<int64_t> startNum { -1 };
214     int32_t timerId { -1 };
215     float scalingValue { 0.0 };
216     std::vector<std::shared_ptr<Rosen::RSCanvasNode>> nodes;
217     std::vector<std::shared_ptr<Rosen::RSCanvasNode>> multiSelectedNodes;
218     std::vector<std::shared_ptr<Media::PixelMap>> multiSelectedPixelMaps;
219     std::shared_ptr<Rosen::RSNode> rootNode { nullptr };
220     std::shared_ptr<Rosen::RSNode> parentNode { nullptr };
221     std::shared_ptr<Rosen::RSSurfaceNode> surfaceNode { nullptr };
222     std::shared_ptr<Media::PixelMap> pixelMap { nullptr };
223     std::shared_ptr<Media::PixelMap> stylePixelMap { nullptr };
224 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
225     IContext* context { nullptr };
226 #endif // OHOS_BUILD_ENABLE_ARKUI_X
227     ExtraInfo extraInfo;
228     FilterInfo filterInfo;
229 };
230 
231 struct DragWindowRotationInfo {
232     float rotation { 0.0f };
233     float pivotX { 0.0f };
234     float pivotY { 0.0f };
235 };
236 
237 class DragDrawing : public IDragAnimation {
238 public:
239     DragDrawing() = default;
240     DISALLOW_COPY_AND_MOVE(DragDrawing);
241     ~DragDrawing();
242 
243 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
244     int32_t Init(const DragData &dragData, IContext* context);
245 #else
246     int32_t Init(const DragData &dragData);
247 #endif // OHOS_BUILD_ENABLE_ARKUI_X
248     void NotifyDragInfo(const std::string &sourceName, const std::string &targetName);
249     void Draw(int32_t displayId, int32_t displayX, int32_t displayY, bool isNeedAdjustDisplayXY = true,
250         bool isMultiSelectedAnimation = true);
251     int32_t UpdateDragStyle(DragCursorStyle style);
252     int32_t UpdateShadowPic(const ShadowInfo &shadowInfo);
253     int32_t UpdatePreviewStyle(const PreviewStyle &previewStyle);
254     int32_t UpdatePreviewStyleWithAnimation(const PreviewStyle &previewStyle, const PreviewAnimation &animation);
255     int32_t StartVsync();
256 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
257     void OnDragSuccess(IContext* context);
258     void OnDragFail(IContext* context);
259 #else
260     void OnDragSuccess();
261     void OnDragFail();
262     void SetDragWindow(std::shared_ptr<OHOS::Rosen::Window> window);
263     void AddDragDestroy(std::function<void()> cb);
264     void SetSVGFilePath(const std::string &filePath);
265 #endif // OHOS_BUILD_ENABLE_ARKUI_X
266     void OnDragMove(int32_t displayId, int32_t displayX, int32_t displayY, int64_t actionTime);
267     void EraseMouseIcon();
268     void DestroyDragWindow();
269     void UpdateDrawingState();
270     void UpdateDragWindowState(bool visible);
271     void OnStartDrag(const DragAnimationData &dragAnimationData, std::shared_ptr<Rosen::RSCanvasNode> shadowNode,
272         std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode) override;
273     void OnDragStyle(std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode,
274         std::shared_ptr<Media::PixelMap> stylePixelMap) override;
275     void OnStopDragSuccess(std::shared_ptr<Rosen::RSCanvasNode> shadowNode,
276         std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode) override;
277     void OnStopDragFail(std::shared_ptr<Rosen::RSSurfaceNode> surfaceNode,
278         std::shared_ptr<Rosen::RSNode> rootNode) override;
279     void OnStopAnimation() override;
280     int32_t EnterTextEditorArea(bool enable);
281     bool GetAllowDragState();
282     void SetScreenId(uint64_t screenId);
283     int32_t RotateDragWindowAsync(Rosen::Rotation rotation);
284     int32_t RotateDragWindowSync(const std::shared_ptr<Rosen::RSTransaction>& rsTransaction = nullptr);
285     void SetRotation(Rosen::Rotation rotation);
286     float CalculateWidthScale();
287     float GetMaxWidthScale(int32_t width);
288     int32_t AddSelectedPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap);
289     void UpdateDragWindowDisplay(int32_t displayId);
290     void ScreenRotate(Rosen::Rotation rotation, Rosen::Rotation lastRotation);
291 
292 private:
293     int32_t CheckDragData(const DragData &dragData);
294     int32_t InitLayer();
295     void InitCanvas(int32_t width, int32_t height);
296     void CreateWindow();
297     int32_t DrawShadow(std::shared_ptr<Rosen::RSCanvasNode> shadowNode);
298     int32_t DrawMouseIcon();
299     int32_t DrawStyle(std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode,
300         std::shared_ptr<Media::PixelMap> stylePixelMap);
301     int32_t RunAnimation(std::function<int32_t()> cb);
302     int32_t InitVSync(float endAlpha, float endScale);
303     void OnVsync();
304     void InitDrawingInfo(const DragData &dragData);
305     int32_t InitDragAnimationData(DragAnimationData &dragAnimationData);
306     void RemoveModifier();
307     int32_t UpdateSvgNodeInfo(xmlNodePtr curNode, int32_t extendSvgWidth);
308     xmlNodePtr GetRectNode(xmlNodePtr curNode);
309     xmlNodePtr UpdateRectNode(int32_t extendSvgWidth, xmlNodePtr curNode);
310     void UpdateTspanNode(xmlNodePtr curNode);
311     int32_t ParseAndAdjustSvgInfo(xmlNodePtr curNode);
312     std::shared_ptr<Media::PixelMap> DecodeSvgToPixelMap(const std::string &filePath);
313     int32_t GetFilePath(std::string &filePath);
314     bool NeedAdjustSvgInfo();
315     void SetDecodeOptions(Media::DecodeOptions &decodeOpts);
316     bool ParserFilterInfo(const std::string &filterInfoStr, FilterInfo &filterInfo);
317     void ParserCornerRadiusInfo(const cJSON *cornerRadiusInfoStr, FilterInfo &filterInfo);
318     void ParserBlurInfo(const cJSON *BlurInfoInfoStr, FilterInfo &filterInfo);
319     void SetCustomDragBlur(const FilterInfo &filterInfo, std::shared_ptr<Rosen::RSCanvasNode> filterNode);
320     void SetComponentDragBlur(const FilterInfo &filterInfo, const ExtraInfo &extraInfo,
321         std::shared_ptr<Rosen::RSCanvasNode> filterNode);
322     void ParserDragShadowInfo(cJSON* filterInfoParser, FilterInfo &filterInfo);
323     void ParserTextDragShadowInfo(cJSON* filterInfoParser, FilterInfo &filterInfo);
324     void PrintDragShadowInfo();
325     void ProcessFilter();
326     bool ParserExtraInfo(const std::string &extraInfoStr, ExtraInfo &extraInfo);
327     static float RadiusVp2Sigma(float radiusVp, float dipScale);
328     void DoDrawMouse(int32_t mousePositionX, int32_t mousePositionY);
329     void UpdateMousePosition(float mousePositionX, float mousePositionY);
330     int32_t UpdateDefaultDragStyle(DragCursorStyle style);
331     int32_t UpdateValidDragStyle(DragCursorStyle style);
332     int32_t SetNodesLocation(int32_t positionX, int32_t positionY);
333     int32_t CreateEventRunner(int32_t positionX, int32_t positionY);
334     int32_t ModifyPreviewStyle(std::shared_ptr<Rosen::RSCanvasNode> node, const PreviewStyle &previewStyle);
335     int32_t ModifyMultiPreviewStyle(const std::vector<PreviewStyle> &previewStyles);
336     void MultiSelectedAnimation(int32_t positionX, int32_t positionY, int32_t adjustSize,
337         bool isMultiSelectedAnimation);
338     void DoMultiSelectedAnimation(float positionX, float positionY, float adjustSize,
339         bool isMultiSelectedAnimation = true);
340     void InitMultiSelectedNodes();
341     void ClearMultiSelectedData();
342     bool ParserRadius(float &radius);
343     void OnStopAnimationSuccess();
344     void OnStopAnimationFail();
345     void OnDragStyleAnimation();
346     void ChangeStyleAnimation();
347     void CheckStyleNodeModifier(std::shared_ptr<Rosen::RSCanvasNode> styleNode);
348     void RemoveStyleNodeModifier(std::shared_ptr<Rosen::RSCanvasNode> styleNode);
349     void StartStyleAnimation(float startScale, float endScale, int32_t duration);
350     void UpdateAnimationProtocol(Rosen::RSAnimationTimingProtocol protocol);
351     void RotateDisplayXY(int32_t &displayX, int32_t &displayY);
352     void RotatePixelMapXY();
353     void ResetAnimationParameter();
354     void ResetAnimationFlag(bool isForce = false);
355     void DoEndAnimation();
356     void ResetParameter();
357     int32_t DoRotateDragWindow(float rotation,
358         const std::shared_ptr<Rosen::RSTransaction>& rsTransaction, bool isAnimated);
359     int32_t DoRotateDragWindowAnimation(float rotation, float pivotX, float pivotY,
360         const std::shared_ptr<Rosen::RSTransaction>& rsTransaction);
361     int32_t RotateDragWindow(Rosen::Rotation rotation,
362         const std::shared_ptr<Rosen::RSTransaction>& rsTransaction = nullptr, bool isAnimated = false);
363 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
364     std::shared_ptr<AppExecFwk::EventHandler> GetSuperHubHandler();
365 #endif // OHOS_BUILD_ENABLE_ARKUI_X
366     void RotateCanvasNode(float pivotX, float pivotY, float rotation);
367 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
368     void ResetSuperHubHandler();
369 #endif // OHOS_BUILD_ENABLE_ARKUI_X
370     void FlushDragPosition(uint64_t nanoTimestamp);
371     void RotatePosition(float &displayX, float &displayY);
372     void UpdateDragPosition(int32_t displayId, float displayX, float displayY);
373     float AdjustDoubleValue(double doubleValue);
374     int32_t UpdatePixelMapsAngleAndAlpha();
375     int32_t UpdatePixeMapDrawingOrder();
376     void LoadDragDropLib();
377     template <typename T>
378     void AdjustRotateDisplayXY(T &displayX, T &displayY);
379     void DrawRotateDisplayXY(float positionX, float positionY);
380     void ScreenRotateAdjustDisplayXY(
381         Rosen::Rotation rotation, Rosen::Rotation lastRotation, float &displayX, float &displayY);
382 
383 private:
384     int64_t interruptNum_ { -1 };
385     std::shared_ptr<Rosen::RSCanvasNode> canvasNode_ { nullptr };
386     std::shared_ptr<DrawSVGModifier> drawSVGModifier_ { nullptr };
387     std::shared_ptr<DrawPixelMapModifier> drawPixelMapModifier_ { nullptr };
388     std::shared_ptr<DrawMouseIconModifier> drawMouseIconModifier_ { nullptr };
389     std::shared_ptr<DrawDynamicEffectModifier> drawDynamicEffectModifier_ { nullptr };
390     std::shared_ptr<DrawDragStopModifier> drawDragStopModifier_ { nullptr };
391     std::shared_ptr<DrawStyleChangeModifier> drawStyleChangeModifier_ { nullptr };
392     std::shared_ptr<DrawStyleScaleModifier> drawStyleScaleModifier_ { nullptr };
393     std::shared_ptr<Rosen::RSUIDirector> rsUiDirector_ { nullptr };
394     std::shared_ptr<Rosen::VSyncReceiver> receiver_ { nullptr };
395     std::shared_ptr<AppExecFwk::EventHandler> handler_ { nullptr };
396     std::shared_ptr<AppExecFwk::EventHandler> superHubHandler_ { nullptr };
397     std::atomic_bool hasRunningStopAnimation_ { false };
398     std::atomic_bool hasRunningScaleAnimation_ { false };
399     std::atomic_bool needBreakStyleScaleAnimation_ { false };
400     std::atomic_bool hasRunningAnimation_ { false };
401     void* dragExtHandler_ { nullptr };
402     bool needRotatePixelMapXY_ { false };
403     uint64_t screenId_ { 0 };
404     Rosen::Rotation rotation_ { Rosen::Rotation::ROTATION_0 };
405     ScreenSizeType currentScreenSize_ = ScreenSizeType::UNDEFINED;
406     MMI::PointerStyle pointerStyle_;
407     DragVSyncStation vSyncStation_;
408     DragSmoothProcessor dragSmoothProcessor_;
409     std::shared_ptr<DragFrameCallback> frameCallback_ { nullptr };
410     std::atomic_bool isRunningRotateAnimation_ { false };
411     DragWindowRotationInfo DragWindowRotateInfo_;
412     int32_t timerId_ { -1 };
413 #ifdef OHOS_BUILD_ENABLE_ARKUI_X
414     std::shared_ptr<OHOS::Rosen::Window> window_ { nullptr };
415     std::function<void()> callback_ { nullptr };
416     std::string svgFilePath_;
417     int64_t actionTime_ { 0 };
418 #else
419     IContext* context_ { nullptr} ;
420 #endif // OHOS_BUILD_ENABLE_ARKUI_X
421 };
422 } // namespace DeviceStatus
423 } // namespace Msdp
424 } // namespace OHOS
425 #endif // DRAG_DRAWING_H