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