• 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 #include "drag_drawing.h"
17 
18 #include <atomic>
19 #include <cstdint>
20 #include <fstream>
21 #include <limits>
22 #include <shared_mutex>
23 #include <string>
24 #include <unistd.h>
25 
26 #include <dlfcn.h>
27 #include "display_info.h"
28 
29 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
30 #include "hitrace_meter.h"
31 #endif // OHOS_BUILD_ENABLE_ARKUI_X
32 #include "include/core/SkTextBlob.h"
33 #include "image_source.h"
34 #include "image_type.h"
35 #include "image_utils.h"
36 #include "input_manager.h"
37 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
38 #include "parameters.h"
39 #endif // OHOS_BUILD_ENABLE_ARKUI_X
40 #include "pointer_event.h"
41 #include "pointer_style.h"
42 #include "render/rs_filter.h"
43 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
44 #include "screen_manager.h"
45 #endif // OHOS_BUILD_ENABLE_ARKUI_X
46 #include "string_ex.h"
47 #include "transaction/rs_interfaces.h"
48 #include "ui/rs_surface_extractor.h"
49 #include "ui/rs_surface_node.h"
50 #include "ui/rs_ui_director.h"
51 
52 #include "animation_curve.h"
53 #include "devicestatus_define.h"
54 #include "drag_data_manager.h"
55 #ifdef MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
56 #include "drag_hisysevent.h"
57 #endif // MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
58 #include "include/util.h"
59 
60 #undef LOG_TAG
61 #define LOG_TAG "DragDrawing"
62 
63 namespace OHOS {
64 namespace Msdp {
65 namespace DeviceStatus {
66 namespace {
67 constexpr int32_t BASELINE_DENSITY { 160 };
68 constexpr int32_t DEVICE_INDEPENDENT_PIXEL { 40 };
69 constexpr int32_t MAGIC_INDEPENDENT_PIXEL { 25 };
70 constexpr int32_t MAGIC_STYLE_OPT { 1 };
71 constexpr int32_t DRAG_NUM_ONE { 1 };
72 constexpr int32_t STRING_PX_LENGTH { 2 };
73 constexpr int32_t EIGHT_SIZE { 8 };
74 constexpr int32_t TWELVE_SIZE { 12 };
75 constexpr int64_t START_TIME { 181154000809 };
76 constexpr int64_t INTERVAL_TIME { 16666667 };
77 constexpr int32_t SVG_WIDTH { 40 };
78 constexpr float SCALE_THRESHOLD_EIGHT { 1.0F * INT32_MAX / (SVG_WIDTH + EIGHT_SIZE) };
79 constexpr float SCALE_THRESHOLD_TWELVE { 1.0F * INT32_MAX / (SVG_WIDTH + TWELVE_SIZE) };
80 constexpr int32_t SUCCESS_ANIMATION_DURATION { 300 };
81 constexpr int32_t ANIMATION_DURATION { 400 };
82 constexpr int32_t VIEW_BOX_POS { 2 };
83 constexpr int32_t BACKGROUND_FILTER_INDEX { 0 };
84 constexpr int32_t ASYNC_ROTATE_TIME { 150 };
85 constexpr int32_t PIXEL_MAP_INDEX { 1 };
86 constexpr int32_t DRAG_STYLE_INDEX { 2 };
87 constexpr int32_t MOUSE_ICON_INDEX { 3 };
88 constexpr int32_t SHORT_DURATION { 55 };
89 constexpr int32_t LONG_DURATION { 90 };
90 constexpr int32_t FIRST_PIXELMAP_INDEX { 0 };
91 constexpr int32_t SECOND_PIXELMAP_INDEX { 1 };
92 constexpr int32_t LAST_SECOND_PIXELMAP { 2 };
93 constexpr int32_t LAST_THIRD_PIXELMAP { 3 };
94 constexpr size_t TOUCH_NODE_MIN_COUNT { 3 };
95 constexpr size_t MOUSE_NODE_MIN_COUNT { 4 };
96 constexpr float DEFAULT_SCALING { 1.0f };
97 constexpr float BEGIN_ALPHA { 1.0f };
98 constexpr float END_ALPHA { 0.0f };
99 constexpr float START_STYLE_ALPHA { 1.0f };
100 constexpr float END_STYLE_ALPHA { 0.0f };
101 constexpr float BEGIN_SCALE { 1.0f };
102 constexpr float END_SCALE_FAIL { 1.2f };
103 constexpr float END_SCALE_SUCCESS { 0.0f };
104 #ifndef OHOS_BUILD_PC_PRODUCT
105 constexpr float DEFAULT_PIVOT { 0.0f };
106 #else
107 constexpr int32_t DOT_PER_INCH { 160 };
108 #endif // OHOS_BUILD_PC_PRODUCT
109 constexpr float HALF_PIVOT { 0.5f };
110 constexpr float START_STYLE_SCALE { 1.0f };
111 constexpr float STYLE_CHANGE_SCALE { 1.1f };
112 constexpr float STYLE_MAX_SCALE { 1.2f };
113 constexpr float STYLE_END_SCALE { 1.0f };
114 constexpr float SVG_ORIGINAL_SIZE { 40.0f };
115 constexpr float DEFAULT_POSITION_X { 0.0f };
116 constexpr float BLUR_SIGMA_SCALE { 0.57735f };
117 constexpr float RADIUS_VP { 23.0f };
118 constexpr float DEFAULT_SATURATION { 1.05f };
119 constexpr float DEFAULT_BRIGHTNESS { 1.05f };
120 constexpr float INCREASE_RATIO { 1.22f };
121 constexpr float DRAG_WINDOW_POSITION_Z { 6999.0f };
122 constexpr float DEFAULT_ANGLE { 0.0f };
123 constexpr float POSITIVE_ANGLE { 8.0f };
124 constexpr float NEGATIVE_ANGLE { -8.0f };
125 constexpr float DEFAULT_ALPHA { 1.0f };
126 constexpr float FIRST_PIXELMAP_ALPHA { 0.6f };
127 constexpr float SECOND_PIXELMAP_ALPHA { 0.3f };
128 constexpr float HALF_RATIO { 0.5f };
129 constexpr float ROTATION_0 { 0.0f };
130 constexpr float ROTATION_90 { 90.0f };
131 constexpr float ROTATION_360 { 360.0f };
132 constexpr float ROTATION_270 { 270.0f };
133 constexpr float ZOOM_IN_SCALE { 1.03f };
134 constexpr float ZOOM_OUT_SCALE { 0.9f };
135 constexpr float ZOOM_END_SCALE { 1.0f };
136 #ifdef OHOS_ENABLE_PULLTHROW
137 constexpr float THROW_SLIP_TIME { 616.0f };
138 constexpr float BREATHE_TIME { 600.0f };
139 constexpr float BREATHE_REPEAT { 50 };
140 constexpr float BREATHE_SCALE { 0.1f };
141 constexpr float ZOOMOUT_PULLTHROW { 400.0f };
142 constexpr float APP_WIDTH { 110 };
143 #endif // OHOS_ENABLE_PULLTHROW
144 constexpr uint32_t TRANSPARENT_COLOR_ARGB { 0x00000000 };
145 constexpr int32_t DEFAULT_MOUSE_SIZE { 1 };
146 constexpr int32_t DEFAULT_COLOR_VALUE { 0 };
147 constexpr int32_t INVALID_COLOR_VALUE { -1 };
148 constexpr int32_t GLOBAL_WINDOW_ID { -1 };
149 constexpr int32_t MOUSE_DRAG_CURSOR_CIRCLE_STYLE { 41 };
150 constexpr int32_t CURSOR_CIRCLE_MIDDLE { 2 };
151 constexpr int32_t TWICE_SIZE { 2 };
152 constexpr int32_t NUM_ONE { 1 };
153 constexpr int32_t NUM_TWO { 2 };
154 constexpr int32_t NUM_FOUR { 4 };
155 constexpr int32_t ALPHA_DURATION { 350 };
156 constexpr int32_t DRAG_END_DURATION { 200 };
157 constexpr int32_t ZOOM_DURATION { 300 };
158 constexpr int32_t ZOOM_IN_DURATION { 350 };
159 constexpr int32_t ZOOM_OUT_DURATION { 250 };
160 const Rosen::RSAnimationTimingCurve CURVE =
161     Rosen::RSAnimationTimingCurve::CreateCubicCurve(0.2f, 0.0f, 0.2f, 1.0f);
162 const Rosen::RSAnimationTimingCurve SPRING = Rosen::RSAnimationTimingCurve::CreateSpring(0.347f, 0.99f, 0.0f);
163 constexpr int32_t HEX_FF { 0xFF };
164 const std::string RENDER_THREAD_NAME { "os_dargRenderRunner" };
165 constexpr float BEZIER_000 { 0.00f };
166 constexpr float BEZIER_020 { 0.20f };
167 constexpr float BEZIER_030 { 0.30f };
168 constexpr float BEZIER_033 { 0.33f };
169 constexpr float BEZIER_040 { 0.40f };
170 constexpr float BEZIER_060 { 0.60f };
171 constexpr float BEZIER_067 { 0.67f };
172 constexpr float BEZIER_100 { 1.00f };
173 constexpr float MIN_OPACITY { 0.0f };
174 constexpr float MAX_OPACITY { 1.0f };
175 constexpr int32_t TIME_DRAG_CHANGE_STYLE { 50 };
176 constexpr int32_t TIME_DRAG_STYLE { 100 };
177 constexpr int32_t TIME_STOP_FAIL_WINDOW { 125 };
178 constexpr int32_t TIME_STOP_SUCCESS_WINDOW { 250 };
179 constexpr int32_t TIME_STOP_SUCCESS_STYLE { 150 };
180 constexpr int32_t TIME_STOP { 0 };
181 constexpr int64_t TIME_SLEEP { 30000 };
182 constexpr int32_t INTERRUPT_SCALE { 15 };
183 constexpr int32_t TIMEOUT_MS { 500 };
184 constexpr float MAX_SCREEN_WIDTH_SM { 600.0f };
185 constexpr float MAX_SCREEN_WIDTH_MD { 840.0f };
186 constexpr float MAX_SCREEN_WIDTH_LG { 1440.0f };
187 constexpr float SCALE_TYPE_FIRST = 2.0;
188 constexpr float SCALE_TYPE_SECOND = 3.0;
189 constexpr float SCALE_TYPE_THIRD = 4.0;
190 const std::string THREAD_NAME { "os_AnimationEventRunner" };
191 const std::string SUPER_HUB_THREAD_NAME { "os_SuperHubEventRunner" };
192 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
193 const std::string COPY_DRAG_PATH { "/system/etc/device_status/drag_icon/Copy_Drag.svg" };
194 const std::string COPY_ONE_DRAG_PATH { "/system/etc/device_status/drag_icon/Copy_One_Drag.svg" };
195 const std::string FORBID_DRAG_PATH { "/system/etc/device_status/drag_icon/Forbid_Drag.svg" };
196 const std::string FORBID_ONE_DRAG_PATH { "/system/etc/device_status/drag_icon/Forbid_One_Drag.svg" };
197 const std::string MOVE_DRAG_PATH { "/system/etc/device_status/drag_icon/Move_Drag.svg" };
198 const std::string COPY_DRAG_RTL_PATH { "/system/etc/device_status/drag_icon/Copy_Drag_RTL.svg" };
199 const std::string COPY_ONE_DRAG_RTL_PATH { "/system/etc/device_status/drag_icon/Copy_One_Drag_RTL.svg" };
200 const std::string FORBID_DRAG_RTL_PATH { "/system/etc/device_status/drag_icon/Forbid_Drag_RTL.svg" };
201 const std::string FORBID_ONE_DRAG_RTL_PATH { "/system/etc/device_status/drag_icon/Forbid_One_Drag_RTL.svg" };
202 const std::string MOVE_DRAG_RTL_PATH { "/system/etc/device_status/drag_icon/Move_Drag_RTL.svg" };
203 #else
204 const std::string COPY_DRAG_NAME { "/base/media/Copy_Drag.svg" };
205 const std::string COPY_ONE_DRAG_NAME { "/base/media/Copy_One_Drag.svg" };
206 const std::string FORBID_DRAG_NAME { "/base/media/Forbid_Drag.svg" };
207 const std::string FORBID_ONE_DRAG_NAME { "/base/media/Forbid_One_Drag.svg" };
208 const std::string MOVE_DRAG_NAME { "/base/media/Move_Drag.svg" };
209 #endif // OHOS_BUILD_ENABLE_ARKUI_X
210 const std::string MOUSE_DRAG_DEFAULT_PATH { "/system/etc/device_status/drag_icon/Mouse_Drag_Default.svg" };
211 const std::string MOUSE_DRAG_MAGIC_DEFAULT_PATH { "/system/etc/device_status/drag_icon/Mouse_Drag_Magic_Default.svg" };
212 const std::string MOUSE_DRAG_CURSOR_CIRCLE_PATH { "/system/etc/device_status/drag_icon/Mouse_Drag_Cursor_Circle.png" };
213 const std::string DRAG_DROP_EXTENSION_SO_PATH { "/system/lib64/drag_drop_ext/libdrag_drop_ext.z.so" };
214 const std::string BIG_FOLDER_LABEL { "scb_folder" };
215 struct DrawingInfo g_drawingInfo;
216 static std::shared_mutex g_pixelMapLock;
217 struct DragData g_dragDataForSuperHub;
218 
CheckNodesValid()219 bool CheckNodesValid()
220 {
221     FI_HILOGD("enter");
222     if (g_drawingInfo.nodes.size() <= DRAG_STYLE_INDEX) {
223         FI_HILOGE("The index is out of bounds, node size is %{public}zu", g_drawingInfo.nodes.size());
224         return false;
225     } else if (g_drawingInfo.nodes.empty() || g_drawingInfo.nodes[DRAG_STYLE_INDEX] == nullptr) {
226         FI_HILOGE("Nodes invalid");
227         return false;
228     }
229 
230 #ifndef OHOS_BUILD_PC_PRODUCT
231     if ((g_drawingInfo.sourceType == MMI::PointerEvent::SOURCE_TYPE_MOUSE) &&
232         (g_drawingInfo.nodes.size() < MOUSE_NODE_MIN_COUNT)) {
233         FI_HILOGE("Nodes size invalid when mouse type, node size:%{public}zu", g_drawingInfo.nodes.size());
234         return false;
235     }
236 #endif // OHOS_BUILD_PC_PRODUCT
237 
238     if ((g_drawingInfo.sourceType == MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN) &&
239         (g_drawingInfo.nodes.size() < TOUCH_NODE_MIN_COUNT)) {
240         FI_HILOGE("Nodes size invalid when touchscreen type, node size:%{public}zu", g_drawingInfo.nodes.size());
241         return false;
242     }
243     return true;
244 }
245 
GetScaling()246 float GetScaling()
247 {
248     if (g_drawingInfo.isExistScalingValue) {
249         return g_drawingInfo.scalingValue;
250     }
251 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
252 #ifndef OHOS_BUILD_PC_PRODUCT
253     sptr<Rosen::Display> display = Rosen::DisplayManager::GetInstance().GetDisplayById(g_drawingInfo.displayId);
254     if (display == nullptr) {
255         FI_HILOGD("Get display info failed, display:%{public}d", g_drawingInfo.displayId);
256         display = Rosen::DisplayManager::GetInstance().GetDisplayById(0);
257         if (display == nullptr) {
258             FI_HILOGE("Get display info failed, display is nullptr");
259             return DEFAULT_SCALING;
260         }
261     }
262     int32_t deviceDpi = display->GetDpi();
263 #else
264     sptr<Rosen::DisplayInfo> displayInfo =
265         Rosen::DisplayManager::GetInstance().GetVisibleAreaDisplayInfoById(g_drawingInfo.displayId);
266     if (displayInfo == nullptr) {
267         FI_HILOGD("Get display info failed, display:%{public}d", g_drawingInfo.displayId);
268         displayInfo = Rosen::DisplayManager::GetInstance().GetVisibleAreaDisplayInfoById(0);
269         if (displayInfo == nullptr) {
270             FI_HILOGE("Get display info failed, display is nullptr");
271             return DEFAULT_SCALING;
272         }
273     }
274     int32_t deviceDpi = displayInfo->GetVirtualPixelRatio() * DOT_PER_INCH;
275 #endif // OHOS_BUILD_PC_PRODUCT
276 #else
277     sptr<Rosen::Display> display = Rosen::DisplayManager::GetInstance().GetDefaultDisplaySync();
278     if (display == nullptr) {
279         FI_HILOGE("Get display info failed, display is nullptr");
280         return DEFAULT_SCALING;
281     }
282     sptr<Rosen::DisplayInfo> info = display->GetDisplayInfo();
283     if (info == nullptr) {
284         FI_HILOGE("Get info failed, info is nullptr");
285         return DEFAULT_SCALING;
286     }
287     int32_t deviceDpi = info->GetDensityDpi();
288 #endif // OHOS_BUILD_ENABLE_ARKUI_X
289     FI_HILOGD("displayId:%{public}d, deviceDpi:%{public}d", g_drawingInfo.displayId, deviceDpi);
290     if (deviceDpi < -std::numeric_limits<float>::epsilon()) {
291         FI_HILOGE("Invalid deviceDpi:%{public}d", deviceDpi);
292         return DEFAULT_SCALING;
293     }
294     g_drawingInfo.scalingValue = (1.0 * deviceDpi * DEVICE_INDEPENDENT_PIXEL / BASELINE_DENSITY) / SVG_ORIGINAL_SIZE;
295     g_drawingInfo.isExistScalingValue = true;
296     return g_drawingInfo.scalingValue;
297 }
298 } // namespace
299 
300 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
Init(const DragData & dragData,IContext * context,bool isLongPressDrag)301 int32_t DragDrawing::Init(const DragData &dragData, IContext* context, bool isLongPressDrag)
302 #else
303 int32_t DragDrawing::Init(const DragData &dragData, bool isLongPressDrag)
304 #endif // OHOS_BUILD_ENABLE_ARKUI_X
305 {
306     FI_HILOGI("enter");
307     int32_t checkDragDataResult = CheckDragData(dragData);
308     if (INIT_SUCCESS != checkDragDataResult) {
309         return checkDragDataResult;
310     }
311     InitDrawingInfo(dragData, isLongPressDrag);
312     UpdateDragDataForSuperHub(dragData);
313     CreateWindow();
314     CHKPR(g_drawingInfo.surfaceNode, INIT_FAIL);
315     if (InitLayer() != RET_OK) {
316         FI_HILOGE("Init layer failed");
317         return INIT_FAIL;
318     }
319     DragAnimationData dragAnimationData;
320     if (!CheckNodesValid() || InitDragAnimationData(dragAnimationData) != RET_OK) {
321         FI_HILOGE("Init drag animation data or check nodes valid failed");
322         return INIT_FAIL;
323     }
324 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
325     LoadDragDropLib();
326 #endif // OHOS_BUILD_ENABLE_ARKUI_X
327     OnStartDrag(dragAnimationData);
328     if (!g_drawingInfo.multiSelectedNodes.empty()) {
329         g_drawingInfo.isCurrentDefaultStyle = true;
330         UpdateDragStyle(DragCursorStyle::MOVE);
331     }
332 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
333     context_ = context;
334 #endif // OHOS_BUILD_ENABLE_ARKUI_X
335     CHKPR(rsUiDirector_, INIT_FAIL);
336     if (g_drawingInfo.sourceType != MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
337         rsUiDirector_->SendMessages();
338         return INIT_SUCCESS;
339     }
340 
341 #ifndef OHOS_BUILD_PC_PRODUCT
342     if (DrawMouseIcon() != RET_OK) {
343         FI_HILOGE("Draw mouse icon failed");
344         return INIT_FAIL;
345     }
346 #endif // OHOS_BUILD_PC_PRODUCT
347 
348     rsUiDirector_->SendMessages();
349     FI_HILOGI("leave");
350     return INIT_SUCCESS;
351 }
352 
CheckDragData(const DragData & dragData)353 int32_t DragDrawing::CheckDragData(const DragData &dragData)
354 {
355     if (g_drawingInfo.isRunning) {
356         FI_HILOGE("Drag drawing is running, can not init again");
357         return INIT_CANCEL;
358     }
359     if (dragData.shadowInfos.empty()) {
360         FI_HILOGE("ShadowInfos is empty");
361         return INIT_FAIL;
362     }
363     for (const auto &shadowInfo : dragData.shadowInfos) {
364         CHKPR(shadowInfo.pixelMap, INIT_FAIL);
365     }
366     if ((dragData.sourceType != MMI::PointerEvent::SOURCE_TYPE_MOUSE) &&
367         (dragData.sourceType != MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN)) {
368         FI_HILOGE("Invalid sourceType:%{public}d", dragData.sourceType);
369         return INIT_FAIL;
370     }
371     if (dragData.dragNum < 0) {
372         FI_HILOGE("Invalid dragNum:%{public}d", dragData.dragNum);
373         return INIT_FAIL;
374     }
375     return INIT_SUCCESS;
376 }
377 
Draw(int32_t displayId,int32_t displayX,int32_t displayY,bool isNeedAdjustDisplayXY,bool isMultiSelectedAnimation)378 void DragDrawing::Draw(int32_t displayId, int32_t displayX, int32_t displayY, bool isNeedAdjustDisplayXY,
379     bool isMultiSelectedAnimation)
380 {
381     if (screenRotateState_) {
382         FI_HILOGD("Doing screen rotation, ignore draw drag window");
383         return;
384     }
385     if (isRunningRotateAnimation_) {
386         FI_HILOGD("Doing rotate drag window animate, ignore draw drag window");
387         return;
388     }
389     if (displayId < 0) {
390         FI_HILOGE("Invalid displayId:%{public}d", displayId);
391         return;
392     }
393     int32_t mousePositionX = displayX;
394     int32_t mousePositionY = displayY;
395     if (isNeedAdjustDisplayXY) {
396         RotateDisplayXY(displayX, displayY);
397         mousePositionX = displayX;
398         mousePositionY = displayY;
399         g_drawingInfo.currentPositionX = static_cast<float>(displayX);
400         g_drawingInfo.currentPositionY = static_cast<float>(displayY);
401         AdjustRotateDisplayXY(displayX, displayY);
402     }
403     g_drawingInfo.displayId = displayId;
404     g_drawingInfo.displayX = displayX;
405     g_drawingInfo.displayY = displayY;
406     if (displayX < 0) {
407         g_drawingInfo.displayX = 0;
408     }
409     if (displayY < 0) {
410         g_drawingInfo.displayY = 0;
411     }
412     int32_t adjustSize = TWELVE_SIZE * GetScaling();
413     int32_t positionX = g_drawingInfo.displayX + g_drawingInfo.pixelMapX;
414     int32_t positionY = g_drawingInfo.displayY + g_drawingInfo.pixelMapY - adjustSize;
415     CHKPV(g_drawingInfo.parentNode);
416     auto currentPixelMap = DragDrawing::AccessGlobalPixelMapLocked();
417     CHKPV(currentPixelMap);
418     g_drawingInfo.parentNode->SetBounds(positionX, positionY, currentPixelMap->GetWidth(),
419         currentPixelMap->GetHeight() + adjustSize);
420     g_drawingInfo.parentNode->SetFrame(positionX, positionY, currentPixelMap->GetWidth(),
421         currentPixelMap->GetHeight() + adjustSize);
422 #ifndef OHOS_BUILD_PC_PRODUCT
423     if (g_drawingInfo.sourceType == MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
424         DoDrawMouse(mousePositionX, mousePositionY);
425     }
426 #endif // OHOS_BUILD_PC_PRODUCT
427     if (!g_drawingInfo.multiSelectedNodes.empty() && !g_drawingInfo.multiSelectedPixelMaps.empty()) {
428         MultiSelectedAnimation(positionX, positionY, adjustSize, isMultiSelectedAnimation);
429     }
430     Rosen::RSTransaction::FlushImplicitTransaction();
431 }
432 
UpdateDragPosition(int32_t displayId,float displayX,float displayY)433 void DragDrawing::UpdateDragPosition(int32_t displayId, float displayX, float displayY)
434 {
435     if (screenRotateState_) {
436         FI_HILOGD("Doing screen rotation, ignore update drag position");
437         return;
438     }
439     if (displayId < 0) {
440         FI_HILOGE("Invalid displayId:%{public}d", displayId);
441         return;
442     }
443     RotatePosition(displayX, displayY);
444     g_drawingInfo.currentPositionX = displayX;
445     g_drawingInfo.currentPositionY = displayY;
446     g_drawingInfo.displayId = displayId;
447     g_drawingInfo.displayX = static_cast<int32_t>(displayX);
448     g_drawingInfo.displayY = static_cast<int32_t>(displayY);
449 #ifndef OHOS_BUILD_PC_PRODUCT
450     float mousePositionX = displayX;
451     float mousePositionY = displayY;
452 #endif // OHOS_BUILD_PC_PRODUCT
453     AdjustRotateDisplayXY(displayX, displayY);
454     g_drawingInfo.x = displayX;
455     g_drawingInfo.y = displayY;
456     if (displayX < 0) {
457         g_drawingInfo.displayX = 0;
458     }
459     if (displayY < 0) {
460         g_drawingInfo.displayY = 0;
461     }
462     float adjustSize = TWELVE_SIZE * GetScaling();
463     float positionX = g_drawingInfo.x + g_drawingInfo.pixelMapX;
464     float positionY = g_drawingInfo.y + g_drawingInfo.pixelMapY - adjustSize;
465     auto parentNode = g_drawingInfo.parentNode;
466     auto currentPixelMap = DragDrawing::AccessGlobalPixelMapLocked();
467     CHKPV(parentNode);
468     CHKPV(currentPixelMap);
469     parentNode->SetBounds(positionX, positionY, currentPixelMap->GetWidth(),
470         currentPixelMap->GetHeight() + adjustSize);
471     parentNode->SetFrame(positionX, positionY, currentPixelMap->GetWidth(),
472         currentPixelMap->GetHeight() + adjustSize);
473 #ifndef OHOS_BUILD_PC_PRODUCT
474     if (g_drawingInfo.sourceType == MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
475         UpdateMousePosition(mousePositionX, mousePositionY);
476     }
477 #endif // OHOS_BUILD_PC_PRODUCT
478     if (!g_drawingInfo.multiSelectedNodes.empty() && !g_drawingInfo.multiSelectedPixelMaps.empty()) {
479         DoMultiSelectedAnimation(positionX, positionY, adjustSize);
480     }
481     if (rsUiDirector_ != nullptr) {
482         rsUiDirector_->SendMessages();
483     } else {
484         FI_HILOGE("rsUiDirector_ is nullptr");
485     }
486 }
487 
DoMultiSelectedAnimation(float positionX,float positionY,float adjustSize,bool isMultiSelectedAnimation)488 void DragDrawing::DoMultiSelectedAnimation(float positionX, float positionY, float adjustSize,
489     bool isMultiSelectedAnimation)
490 {
491     if (isMultiSelectedAnimation) {
492         isMultiSelectedAnimation = needMultiSelectedAnimation_;
493     }
494     size_t multiSelectedNodesSize = g_drawingInfo.multiSelectedNodes.size();
495     size_t multiSelectedPixelMapsSize = g_drawingInfo.multiSelectedPixelMaps.size();
496     for (size_t i = 0; (i < multiSelectedNodesSize) && (i < multiSelectedPixelMapsSize); ++i) {
497         std::shared_ptr<Rosen::RSCanvasNode> multiSelectedNode = g_drawingInfo.multiSelectedNodes[i];
498         std::shared_ptr<Media::PixelMap> multiSelectedPixelMap = g_drawingInfo.multiSelectedPixelMaps[i];
499         auto currentPixelMap = DragDrawing::AccessGlobalPixelMapLocked();
500         CHKPV(currentPixelMap);
501         CHKPV(multiSelectedNode);
502         CHKPV(multiSelectedPixelMap);
503         float multiSelectedPositionX = positionX + (static_cast<float>(currentPixelMap->GetWidth()) / TWICE_SIZE) -
504             (static_cast<float>(multiSelectedPixelMap->GetWidth()) / TWICE_SIZE);
505         float multiSelectedPositionY = positionY + (static_cast<float>(currentPixelMap->GetHeight()) / TWICE_SIZE) -
506             (static_cast<float>(multiSelectedPixelMap->GetHeight()) / TWICE_SIZE - adjustSize);
507         if (isMultiSelectedAnimation) {
508             Rosen::RSAnimationTimingProtocol protocol;
509             if (i == FIRST_PIXELMAP_INDEX) {
510                 protocol.SetDuration(SHORT_DURATION);
511             } else {
512                 protocol.SetDuration(LONG_DURATION);
513             }
514             Rosen::RSNode::Animate(protocol, Rosen::RSAnimationTimingCurve::EASE_IN_OUT, [&]() {
515                 multiSelectedNode->SetBounds(multiSelectedPositionX, multiSelectedPositionY,
516                     multiSelectedPixelMap->GetWidth(), multiSelectedPixelMap->GetHeight());
517                 multiSelectedNode->SetFrame(multiSelectedPositionX, multiSelectedPositionY,
518                     multiSelectedPixelMap->GetWidth(), multiSelectedPixelMap->GetHeight());
519             }, []() { FI_HILOGD("DoMultiSelectedAnimation end"); });
520         } else {
521             multiSelectedNode->SetBounds(multiSelectedPositionX, multiSelectedPositionY,
522                 multiSelectedPixelMap->GetWidth(), multiSelectedPixelMap->GetHeight());
523             multiSelectedNode->SetFrame(multiSelectedPositionX, multiSelectedPositionY,
524                 multiSelectedPixelMap->GetWidth(), multiSelectedPixelMap->GetHeight());
525         }
526     }
527 }
528 
UpdateDragStyle(DragCursorStyle style)529 int32_t DragDrawing::UpdateDragStyle(DragCursorStyle style)
530 {
531     FI_HILOGD("style:%{public}d", style);
532     if ((style < DragCursorStyle::DEFAULT) || (style > DragCursorStyle::MOVE)) {
533 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
534 #ifdef MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
535         DragDFX::WriteUpdateDragStyle(style, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT);
536 #endif // MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
537 #endif // OHOS_BUILD_ENABLE_ARKUI_X
538         FI_HILOGE("Invalid style:%{public}d", style);
539         return RET_ERR;
540     }
541     if ((style == DragCursorStyle::DEFAULT) ||
542         ((style == DragCursorStyle::MOVE) && (g_drawingInfo.currentDragNum == DRAG_NUM_ONE))) {
543         return UpdateDefaultDragStyle(style);
544     }
545     return UpdateValidDragStyle(style);
546 }
547 
UpdateShadowPic(const ShadowInfo & shadowInfo)548 int32_t DragDrawing::UpdateShadowPic(const ShadowInfo &shadowInfo)
549 {
550     FI_HILOGD("enter");
551     CHKPR(shadowInfo.pixelMap, RET_ERR);
552     DragDrawing::UpdataGlobalPixelMapLocked(shadowInfo.pixelMap);
553     g_drawingInfo.pixelMapX = shadowInfo.x;
554     g_drawingInfo.pixelMapY = shadowInfo.y;
555     if (!CheckNodesValid()) {
556         FI_HILOGE("Check nodes valid failed");
557         return RET_ERR;
558     }
559     if (g_drawingInfo.nodes.size() <= PIXEL_MAP_INDEX) {
560         FI_HILOGE("The index is out of bounds, node size is %{public}zu", g_drawingInfo.nodes.size());
561         return RET_ERR;
562     }
563     std::shared_ptr<Rosen::RSCanvasNode> shadowNode = g_drawingInfo.nodes[PIXEL_MAP_INDEX];
564     CHKPR(shadowNode, RET_ERR);
565     DrawShadow(shadowNode);
566     float scalingValue = GetScaling();
567     if (SCALE_THRESHOLD_TWELVE < scalingValue || fabsf(SCALE_THRESHOLD_TWELVE - scalingValue) < EPSILON) {
568         FI_HILOGE("Invalid scalingValue:%{public}f", scalingValue);
569         return RET_ERR;
570     }
571 #ifndef OHOS_BUILD_PC_PRODUCT
572     if (g_drawingInfo.sourceType == MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
573         DrawMouseIcon();
574     }
575 #endif // OHOS_BUILD_PC_PRODUCT
576     ProcessFilter();
577     Draw(g_drawingInfo.displayId, g_drawingInfo.displayX, g_drawingInfo.displayY, false);
578     Rosen::Rotation rotation = GetRotation(g_drawingInfo.displayId);
579     RotateDragWindow(rotation);
580     Rosen::RSTransaction::FlushImplicitTransaction();
581     CHKPR(rsUiDirector_, RET_ERR);
582     rsUiDirector_->SendMessages();
583     FI_HILOGD("leave");
584     return RET_OK;
585 }
586 
UpdatePixelMapsAngleAndAlpha()587 int32_t DragDrawing::UpdatePixelMapsAngleAndAlpha()
588 {
589     FI_HILOGD("enter");
590     size_t mulNodesSize = g_drawingInfo.multiSelectedNodes.size();
591     if (mulNodesSize <= 0) {
592         FI_HILOGE("No pixelmap add");
593         return RET_ERR;
594     }
595     if (mulNodesSize == 1) {
596         g_drawingInfo.multiSelectedNodes.front()->SetRotation(POSITIVE_ANGLE);
597         g_drawingInfo.multiSelectedNodes.front()->SetAlpha(FIRST_PIXELMAP_ALPHA);
598     } else if (mulNodesSize == LAST_SECOND_PIXELMAP) {
599         g_drawingInfo.multiSelectedNodes.back()->SetRotation(NEGATIVE_ANGLE);
600         g_drawingInfo.multiSelectedNodes.back()->SetAlpha(SECOND_PIXELMAP_ALPHA);
601     } else {
602         g_drawingInfo.rootNode->RemoveChild(g_drawingInfo.multiSelectedNodes[mulNodesSize - LAST_THIRD_PIXELMAP]);
603         g_drawingInfo.multiSelectedNodes[mulNodesSize - LAST_SECOND_PIXELMAP ]->SetRotation(POSITIVE_ANGLE);
604         g_drawingInfo.multiSelectedNodes[mulNodesSize - LAST_SECOND_PIXELMAP ]->SetAlpha(FIRST_PIXELMAP_ALPHA);
605         g_drawingInfo.multiSelectedNodes.back()->SetRotation(NEGATIVE_ANGLE);
606         g_drawingInfo.multiSelectedNodes.back()->SetAlpha(SECOND_PIXELMAP_ALPHA);
607     }
608     FI_HILOGD("leave");
609     return RET_OK;
610 }
611 
UpdatePixeMapDrawingOrder()612 int32_t DragDrawing::UpdatePixeMapDrawingOrder()
613 {
614     FI_HILOGD("enter");
615     std::shared_ptr<Rosen::RSCanvasNode> pixelMapNode = g_drawingInfo.nodes[PIXEL_MAP_INDEX];
616     std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode = g_drawingInfo.nodes[DRAG_STYLE_INDEX];
617     CHKPR(pixelMapNode, RET_ERR);
618     CHKPR(dragStyleNode, RET_ERR);
619     CHKPR(g_drawingInfo.parentNode, RET_ERR);
620     CHKPR(g_drawingInfo.rootNode, RET_ERR);
621     g_drawingInfo.multiSelectedNodes.emplace_back(pixelMapNode);
622     g_drawingInfo.parentNode->RemoveChild(dragStyleNode);
623     g_drawingInfo.parentNode->RemoveChild(pixelMapNode);
624 
625     int32_t adjustSize = TWELVE_SIZE * GetScaling();
626     int32_t positionX = g_drawingInfo.displayX + g_drawingInfo.pixelMapX;
627     int32_t positionY = g_drawingInfo.displayY + g_drawingInfo.pixelMapY - adjustSize;
628     auto currentPixelMap = DragDrawing::AccessGlobalPixelMapLocked();
629     int32_t pixelMapWidth = currentPixelMap->GetWidth();
630     int32_t pixelMapHeight = currentPixelMap->GetHeight();
631     pixelMapNode->SetBounds(positionX, positionY + adjustSize, pixelMapWidth, pixelMapHeight);
632     pixelMapNode->SetFrame(positionX, positionY + adjustSize, pixelMapWidth, pixelMapHeight);
633 
634     std::shared_ptr<Rosen::RSCanvasNode> addSelectedNode = Rosen::RSCanvasNode::Create();
635     CHKPR(addSelectedNode, RET_ERR);
636     g_drawingInfo.nodes[PIXEL_MAP_INDEX] = addSelectedNode;
637     g_drawingInfo.parentNode->AddChild(addSelectedNode);
638     g_drawingInfo.parentNode->AddChild(dragStyleNode);
639     g_drawingInfo.rootNode->AddChild(g_drawingInfo.multiSelectedNodes.back());
640     g_drawingInfo.rootNode->RemoveChild(g_drawingInfo.parentNode);
641     g_drawingInfo.rootNode->AddChild(g_drawingInfo.parentNode);
642 #ifndef OHOS_BUILD_PC_PRODUCT
643     if (g_drawingInfo.sourceType == MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
644         std::shared_ptr<Rosen::RSCanvasNode> mouseIconNode = g_drawingInfo.nodes[MOUSE_ICON_INDEX];
645         CHKPR(mouseIconNode, RET_ERR);
646         g_drawingInfo.rootNode->RemoveChild(mouseIconNode);
647         g_drawingInfo.rootNode->AddChild(mouseIconNode);
648     }
649 #endif // OHOS_BUILD_PC_PRODUCT
650     if (UpdatePixelMapsAngleAndAlpha() != RET_OK) {
651         FI_HILOGE("setPixelMapsAngleAndAlpha failed");
652         return RET_ERR;
653     }
654     DrawShadow(pixelMapNode);
655     FI_HILOGD("leave");
656     return RET_OK;
657 }
658 
AddSelectedPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap)659 int32_t DragDrawing::AddSelectedPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap)
660 {
661     FI_HILOGD("enter");
662     CHKPR(pixelMap, RET_ERR);
663     if (!CheckNodesValid()) {
664         FI_HILOGE("Check nodes valid failed");
665         return RET_ERR;
666     }
667 
668     auto currentPixelMap = DragDrawing::AccessGlobalPixelMapLocked();
669     g_drawingInfo.multiSelectedPixelMaps.emplace_back(currentPixelMap);
670     DragDrawing::UpdataGlobalPixelMapLocked(pixelMap);
671     if (UpdatePixeMapDrawingOrder() != RET_OK) {
672         FI_HILOGE("Update pixeMap drawing order failed");
673         return RET_ERR;
674     }
675     Draw(g_drawingInfo.displayId, g_drawingInfo.displayX, g_drawingInfo.displayY, false);
676     g_drawingInfo.currentDragNum = g_drawingInfo.multiSelectedPixelMaps.size() + 1;
677     if (UpdateDragStyle(g_drawingInfo.currentStyle) != RET_OK) {
678         FI_HILOGE("Update drag style failed");
679         return RET_ERR;
680     }
681     Rosen::RSTransaction::FlushImplicitTransaction();
682     FI_HILOGD("leave");
683     return RET_OK;
684 }
685 
686 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
OnDragSuccess(IContext * context)687 void DragDrawing::OnDragSuccess(IContext* context)
688 #else
689 void DragDrawing::OnDragSuccess()
690 #endif // OHOS_BUILD_ENABLE_ARKUI_X
691 {
692     FI_HILOGI("enter");
693     if (!CheckNodesValid()) {
694         FI_HILOGE("Check nodes valid failed");
695         return;
696     }
697     if (g_drawingInfo.nodes.size() <= PIXEL_MAP_INDEX || g_drawingInfo.nodes.size() <= DRAG_STYLE_INDEX) {
698         FI_HILOGE("The index is out of bounds, node size is %{public}zu", g_drawingInfo.nodes.size());
699         return;
700     }
701     std::shared_ptr<Rosen::RSCanvasNode> shadowNode = g_drawingInfo.nodes[PIXEL_MAP_INDEX];
702     CHKPV(shadowNode);
703     std::shared_ptr<Rosen::RSCanvasNode> styleNode = g_drawingInfo.nodes[DRAG_STYLE_INDEX];
704     CHKPV(styleNode);
705 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
706     g_drawingInfo.context = context;
707 #endif // OHOS_BUILD_ENABLE_ARKUI_X
708     OnStopDragSuccess(shadowNode, styleNode);
709     FI_HILOGI("leave");
710 }
711 
LongPressDragFail()712 void DragDrawing::LongPressDragFail()
713 {
714     FI_HILOGD("enter");
715     if (!CheckNodesValid()) {
716         FI_HILOGE("Check nodes valid failed");
717         return;
718     }
719     ResetAnimationParameter();
720     Rosen::RSAnimationTimingProtocol protocolAlphaChanged;
721     protocolAlphaChanged.SetDuration(DRAG_END_DURATION);
722     Rosen::RSNode::Animate(protocolAlphaChanged, CURVE, [&]() {
723         CHKPV(g_drawingInfo.parentNode);
724         g_drawingInfo.parentNode->SetAlpha(END_ALPHA);
725         if (!g_drawingInfo.multiSelectedNodes.empty()) {
726             size_t multiSelectedNodesSize = g_drawingInfo.multiSelectedNodes.size();
727             for (size_t i = 0; i < multiSelectedNodesSize; ++i) {
728                 std::shared_ptr<Rosen::RSCanvasNode> multiSelectedNode = g_drawingInfo.multiSelectedNodes[i];
729                 CHKPV(multiSelectedNode);
730                 multiSelectedNode->SetAlpha(END_ALPHA);
731             }
732         }
733         std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode = g_drawingInfo.nodes[DRAG_STYLE_INDEX];
734         CHKPV(dragStyleNode);
735         dragStyleNode->SetAlpha(END_ALPHA);
736     },  []() { FI_HILOGD("AlphaChanged end"); });
737 
738     Rosen::RSAnimationTimingProtocol protocolZoomIn;
739     protocolZoomIn.SetDuration(DRAG_END_DURATION);
740     Rosen::RSNode::Animate(protocolZoomIn, CURVE, [&]() {
741         CHKPV(g_drawingInfo.parentNode);
742         g_drawingInfo.parentNode->SetScale(ZOOM_END_SCALE);
743         if (!g_drawingInfo.multiSelectedNodes.empty()) {
744             size_t multiSelectedNodesSize = g_drawingInfo.multiSelectedNodes.size();
745             for (size_t i = 0; i < multiSelectedNodesSize; ++i) {
746                 std::shared_ptr<Rosen::RSCanvasNode> multiSelectedNode = g_drawingInfo.multiSelectedNodes[i];
747                 CHKPV(multiSelectedNode);
748                 multiSelectedNode->SetScale(ZOOM_END_SCALE);
749             }
750         }
751         std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode = g_drawingInfo.nodes[DRAG_STYLE_INDEX];
752         CHKPV(dragStyleNode);
753         dragStyleNode->SetScale(ZOOM_END_SCALE);
754     },  [&]() {
755         FI_HILOGD("ZoomIn end");
756         ResetAnimationFlag();
757     });
758     g_drawingInfo.startNum = START_TIME;
759     g_drawingInfo.needDestroyDragWindow = false;
760     StartVsync();
761     FI_HILOGD("leave");
762 }
763 
764 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
OnDragFail(IContext * context,bool isLongPressDrag)765 void DragDrawing::OnDragFail(IContext* context, bool isLongPressDrag)
766 #else
767 void DragDrawing::OnDragFail()
768 #endif // OHOS_BUILD_ENABLE_ARKUI_X
769 {
770     FI_HILOGI("enter");
771 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
772     if (isLongPressDrag) {
773         LongPressDragFail();
774         return;
775     }
776 #endif // OHOS_BUILD_ENABLE_ARKUI_X
777     std::shared_ptr<Rosen::RSSurfaceNode> surfaceNode = g_drawingInfo.surfaceNode;
778     CHKPV(surfaceNode);
779     std::shared_ptr<Rosen::RSNode> rootNode = g_drawingInfo.rootNode;
780     CHKPV(rootNode);
781 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
782     g_drawingInfo.context = context;
783 #endif // OHOS_BUILD_ENABLE_ARKUI_X
784     OnStopDragFail(surfaceNode, rootNode);
785     FI_HILOGI("leave");
786 }
787 
EraseMouseIcon()788 void DragDrawing::EraseMouseIcon()
789 {
790     FI_HILOGI("enter");
791     if (g_drawingInfo.nodes.size() < MOUSE_NODE_MIN_COUNT) {
792         FI_HILOGE("Nodes size invalid, node size:%{public}zu", g_drawingInfo.nodes.size());
793         return;
794     }
795     if (g_drawingInfo.nodes.size() <= MOUSE_ICON_INDEX) {
796         FI_HILOGE("The index is out of bounds, node size is %{public}zu", g_drawingInfo.nodes.size());
797         return;
798     }
799     std::shared_ptr<Rosen::RSCanvasNode> mouseIconNode = g_drawingInfo.nodes[MOUSE_ICON_INDEX];
800     CHKPV(mouseIconNode);
801     if (drawMouseIconModifier_ != nullptr) {
802         mouseIconNode->RemoveModifier(drawMouseIconModifier_);
803         drawMouseIconModifier_ = nullptr;
804     }
805     CHKPV(g_drawingInfo.rootNode);
806     g_drawingInfo.rootNode->RemoveChild(mouseIconNode);
807     CHKPV(rsUiDirector_);
808     rsUiDirector_->SendMessages();
809     FI_HILOGI("leave");
810 }
811 
DestroyDragWindow()812 void DragDrawing::DestroyDragWindow()
813 {
814     FI_HILOGI("enter");
815     ResetParameter();
816     RemoveModifier();
817     ClearMultiSelectedData();
818     if (!g_drawingInfo.nodes.empty()) {
819         g_drawingInfo.nodes.clear();
820         g_drawingInfo.nodes.shrink_to_fit();
821     }
822 #ifdef OHOS_BUILD_INTERNAL_DROP_ANIMATION
823     g_drawingInfo.curvesMaskNode = nullptr;
824     g_drawingInfo.lightNode = nullptr;
825 #endif // OHOS_BUILD_INTERNAL_DROP_ANIMATION
826     if (g_drawingInfo.parentNode != nullptr) {
827         g_drawingInfo.parentNode->ClearChildren();
828         g_drawingInfo.parentNode.reset();
829         g_drawingInfo.parentNode = nullptr;
830     }
831     if (g_drawingInfo.rootNode != nullptr) {
832         g_drawingInfo.rootNode->ClearChildren();
833         g_drawingInfo.rootNode.reset();
834         g_drawingInfo.rootNode = nullptr;
835     }
836     if (g_drawingInfo.surfaceNode != nullptr) {
837         g_drawingInfo.surfaceNode->DetachFromWindowContainer(screenId_);
838         screenId_ = 0;
839         g_drawingInfo.displayId = -1;
840         g_drawingInfo.surfaceNode = nullptr;
841         Rosen::RSTransaction::FlushImplicitTransaction();
842     }
843 #ifdef OHOS_BUILD_ENABLE_ARKUI_X
844     CHKPV(callback_);
845     callback_();
846     window_ = nullptr;
847     g_dragDataForSuperHub = {};
848 #endif // OHOS_BUILD_ENABLE_ARKUI_X
849     CHKPV(rsUiDirector_);
850     rsUiDirector_->SetRoot(-1);
851     rsUiDirector_->SendMessages();
852     FI_HILOGI("leave");
853 }
854 
UpdateDrawingState()855 void DragDrawing::UpdateDrawingState()
856 {
857     FI_HILOGD("enter");
858     g_drawingInfo.isRunning = false;
859     FI_HILOGD("leave");
860 }
861 
UpdateDragWindowState(bool visible,bool isZoomInAndAlphaChanged,const std::shared_ptr<Rosen::RSTransaction> & rsTransaction)862 void DragDrawing::UpdateDragWindowState(
863     bool visible, bool isZoomInAndAlphaChanged, const std::shared_ptr<Rosen::RSTransaction>& rsTransaction)
864 {
865     CHKPV(g_drawingInfo.surfaceNode);
866     if (visible && isZoomInAndAlphaChanged) {
867         FI_HILOGI("UpdateDragWindowState in animation");
868         if (!CheckNodesValid()) {
869             FI_HILOGE("Check nodes valid failed");
870             return;
871         }
872         CHKPV(g_drawingInfo.parentNode);
873         g_drawingInfo.parentNode->SetAlpha(0.0f);
874         if (!g_drawingInfo.multiSelectedNodes.empty()) {
875             size_t multiSelectedNodesSize = g_drawingInfo.multiSelectedNodes.size();
876             for (size_t i = 0; i < multiSelectedNodesSize; ++i) {
877                 std::shared_ptr<Rosen::RSCanvasNode> multiSelectedNode = g_drawingInfo.multiSelectedNodes[i];
878                 CHKPV(multiSelectedNode);
879                 multiSelectedNode->SetAlpha(0.0f);
880             }
881         }
882         std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode = g_drawingInfo.nodes[DRAG_STYLE_INDEX];
883         CHKPV(dragStyleNode);
884         dragStyleNode->SetAlpha(0.0f);
885         g_drawingInfo.surfaceNode->SetVisible(true);
886         LongPressDragAnimation();
887         Rosen::RSTransaction::FlushImplicitTransaction();
888     } else {
889         if (rsTransaction != nullptr) {
890             Rosen::RSTransaction::FlushImplicitTransaction();
891             rsTransaction->Begin();
892             g_drawingInfo.surfaceNode->SetVisible(visible);
893             rsTransaction->Commit();
894         } else {
895             g_drawingInfo.surfaceNode->SetVisible(visible);
896             Rosen::RSTransaction::FlushImplicitTransaction();
897         }
898     }
899     FI_HILOGI("Drag surfaceNode %{public}s success", visible ? "show" : "hide");
900 }
901 
LongPressDragAlphaAnimation()902 void DragDrawing::LongPressDragAlphaAnimation()
903 {
904     FI_HILOGD("enter");
905     if (!CheckNodesValid()) {
906         FI_HILOGE("Check nodes valid failed");
907         return;
908     }
909     Rosen::RSAnimationTimingProtocol protocolAlphaChanged;
910     protocolAlphaChanged.SetDuration(ALPHA_DURATION);
911     Rosen::RSNode::Animate(protocolAlphaChanged, CURVE, [&]() {
912         CHKPV(g_drawingInfo.parentNode);
913         g_drawingInfo.parentNode->SetAlpha(1.0f);
914         if (!g_drawingInfo.multiSelectedNodes.empty()) {
915             size_t multiSelectedNodesSize = g_drawingInfo.multiSelectedNodes.size();
916             for (size_t i = 0; i < multiSelectedNodesSize; ++i) {
917                 std::shared_ptr<Rosen::RSCanvasNode> multiSelectedNode = g_drawingInfo.multiSelectedNodes[i];
918                 CHKPV(multiSelectedNode);
919                 multiSelectedNode->SetAlpha(1.0f);
920             }
921         }
922         std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode = g_drawingInfo.nodes[DRAG_STYLE_INDEX];
923         CHKPV(dragStyleNode);
924         dragStyleNode->SetAlpha(1.0f);
925     },  []() { FI_HILOGD("AlphaChanged end"); });
926 }
927 
928 #ifdef OHOS_ENABLE_PULLTHROW
SetHovering(double tx,double ty,std::shared_ptr<MMI::PointerEvent> pointerEvent)929 void DragDrawing::SetHovering(double tx, double ty, std::shared_ptr<MMI::PointerEvent> pointerEvent)
930 {
931     FI_HILOGI("enter");
932     CHKPV(pointerEvent);
933     MMI::PointerEvent::PointerItem pointerItem;
934     pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), pointerItem);
935     pointerItem.SetDisplayX(tx);
936     pointerItem.SetDisplayY(ty);
937     pointerEvent->UpdatePointerItem(pointerEvent->GetPointerId(), pointerItem);
938     pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_PULL_MOVE);
939     MMI::InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
940     pullThrowAnimationXCompleted_ = false;
941     pullThrowAnimationYCompleted_ = false;
942 }
943 
PullThrowAnimation(double tx,double ty,float vx,float vy,std::shared_ptr<MMI::PointerEvent> pointerEvent)944 void DragDrawing::PullThrowAnimation(double tx, double ty, float vx,
945     float vy, std::shared_ptr<MMI::PointerEvent> pointerEvent)
946 {
947     FI_HILOGI("enter");
948     CHKPV(pointerEvent);
949     auto parentNode = g_drawingInfo.parentNode;
950     auto currentPixelMap = DragDrawing::AccessGlobalPixelMapLocked();
951     CHKPV(parentNode);
952     CHKPV(currentPixelMap);
953     const Rosen::RSAnimationTimingProtocol THROW_SLIP_TIMING_PROTOCOL(std::round(THROW_SLIP_TIME)); // animation time
954     const Rosen::RSAnimationTimingCurve THROW_SLIP_CURVE_X =
955         Rosen::RSAnimationTimingCurve::CreateSpringCurve(vx, 1.0f, 128.0f, 30.0f);
956     const Rosen::RSAnimationTimingCurve THROW_SLIP_CURVE_Y =
957         Rosen::RSAnimationTimingCurve::CreateSpringCurve(vy, 1.0f, 128.0f, 30.0f);
958     int32_t adjustSize = TWELVE_SIZE * GetScaling();
959     int32_t positionX = tx + g_drawingInfo.pixelMapX;
960     int32_t positionY = ty + g_drawingInfo.pixelMapY - adjustSize;
961     // 执行动画X
962     ResetAnimationParameter();
963     Rosen::RSNode::Animate(THROW_SLIP_TIMING_PROTOCOL, THROW_SLIP_CURVE_X, [&]() {
964         CHKPV(g_drawingInfo.parentNode);
965         g_drawingInfo.parentNode->SetFramePositionX(positionX);
966         if (!g_drawingInfo.multiSelectedNodes.empty()) {
967             size_t multiSelectedNodesSize = g_drawingInfo.multiSelectedNodes.size();
968             for (size_t i = 0; i < multiSelectedNodesSize; ++i) {
969                 std::shared_ptr<Rosen::RSCanvasNode> multiSelectedNode = g_drawingInfo.multiSelectedNodes[i];
970                 CHKPV(multiSelectedNode);
971                 multiSelectedNode->SetFramePositionX(positionX);
972             }
973         }
974     },  [this, pointerEvent, tx, ty]() {
975         FI_HILOGI("PullThrowAnimationX end");
976         if (pullThrowAnimationYCompleted_) {
977             PullThrowBreatheAnimation();
978             SetHovering(tx, ty, pointerEvent);
979             SetScaleAnimation();
980         } else {
981             pullThrowAnimationXCompleted_ = true;
982         }
983     });
984 
985     // 执行动画Y
986     Rosen::RSNode::Animate(THROW_SLIP_TIMING_PROTOCOL, THROW_SLIP_CURVE_Y, [&]() {
987         CHKPV(g_drawingInfo.parentNode);
988         g_drawingInfo.parentNode->SetFramePositionY(positionY);
989         if (!g_drawingInfo.multiSelectedNodes.empty()) {
990             size_t multiSelectedNodesSize = g_drawingInfo.multiSelectedNodes.size();
991             for (size_t i = 0; i < multiSelectedNodesSize; ++i) {
992                 std::shared_ptr<Rosen::RSCanvasNode> multiSelectedNode = g_drawingInfo.multiSelectedNodes[i];
993                 CHKPV(multiSelectedNode);
994                 multiSelectedNode->SetFramePositionY(positionY);
995             }
996         }
997     },  [this, pointerEvent, tx, ty]() {
998         FI_HILOGI("PullThrowAnimationY end");
999         if (pullThrowAnimationXCompleted_) {
1000             PullThrowBreatheAnimation();
1001             SetHovering(tx, ty, pointerEvent);
1002             SetScaleAnimation();
1003         } else {
1004             pullThrowAnimationYCompleted_ = true;
1005         }
1006     });
1007     Rosen::RSTransaction::FlushImplicitTransaction();
1008     FI_HILOGI("leave");
1009     return;
1010 }
1011 
SetScaleAnimation()1012 void DragDrawing::SetScaleAnimation()
1013 {
1014     FI_HILOGI("enter");
1015     auto parentNode = g_drawingInfo.parentNode;
1016     CHKPV(parentNode);
1017     Rosen::RSAnimationTimingProtocol scaleSlipTimingProtocol(std::round(THROW_SLIP_TIME)); // animation time
1018     Rosen::RSAnimationTimingCurve scaleCurve = Rosen::RSAnimationTimingCurve::LINEAR;
1019 
1020     scaleSlipTimingProtocol.SetAutoReverse(false);
1021     scaleSlipTimingProtocol.SetRepeatCount(1);
1022 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
1023     pullThrowScale_ = CalculatePullThrowScale();
1024 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1025     parentNode->SetScale(1.0f);
1026     if (!g_drawingInfo.multiSelectedNodes.empty()) {
1027         size_t multiSelectedNodesSize = g_drawingInfo.multiSelectedNodes.size();
1028         for (size_t i = 0; i < multiSelectedNodesSize; ++i) {
1029             std::shared_ptr<Rosen::RSCanvasNode> multiSelectedNode = g_drawingInfo.multiSelectedNodes[i];
1030             CHKPV(multiSelectedNode);
1031             multiSelectedNode->SetScale(1.0f);
1032         }
1033     }
1034     Rosen::RSNode::Animate(scaleSlipTimingProtocol, scaleCurve, [&]() {
1035         CHKPV(g_drawingInfo.parentNode);
1036         g_drawingInfo.parentNode->SetScale(pullThrowScale_);
1037         if (!g_drawingInfo.multiSelectedNodes.empty()) {
1038             size_t multiSelectedNodesSize = g_drawingInfo.multiSelectedNodes.size();
1039             for (size_t i = 0; i < multiSelectedNodesSize; ++i) {
1040                 std::shared_ptr<Rosen::RSCanvasNode> multiSelectedNode = g_drawingInfo.multiSelectedNodes[i];
1041                 CHKPV(multiSelectedNode);
1042                 multiSelectedNode->SetScale(pullThrowScale_);
1043             }
1044         }
1045     },  [&]() {
1046         FI_HILOGI("pullthrow Scale end");
1047     });
1048     Rosen::RSTransaction::FlushImplicitTransaction();
1049     FI_HILOGI("leave");
1050     return;
1051 }
1052 
PullThrowBreatheAnimation()1053 void DragDrawing::PullThrowBreatheAnimation()
1054 {
1055     FI_HILOGI("enter");
1056     auto parentNode = g_drawingInfo.parentNode;
1057     CHKPV(parentNode);
1058     Rosen::RSAnimationTimingProtocol BREATHE_TIMING_PROTOCOL(std::round(BREATHE_TIME)); // animation time
1059     Rosen::RSAnimationTimingCurve BREATHE_CURVE = Rosen::RSAnimationTimingCurve::LINEAR;
1060 
1061     BREATHE_TIMING_PROTOCOL.SetAutoReverse(true);
1062     BREATHE_TIMING_PROTOCOL.SetRepeatCount(BREATHE_REPEAT);
1063 
1064     // 执行动画
1065     parentNode->SetScale(pullThrowScale_ - BREATHE_SCALE);
1066     if (!g_drawingInfo.multiSelectedNodes.empty()) {
1067         size_t multiSelectedNodesSize = g_drawingInfo.multiSelectedNodes.size();
1068         for (size_t i = 0; i < multiSelectedNodesSize; ++i) {
1069             std::shared_ptr<Rosen::RSCanvasNode> multiSelectedNode = g_drawingInfo.multiSelectedNodes[i];
1070             CHKPV(multiSelectedNode);
1071             multiSelectedNode->SetScale(pullThrowScale_ - BREATHE_SCALE);
1072         }
1073     }
1074     Rosen::RSNode::Animate(BREATHE_TIMING_PROTOCOL, BREATHE_CURVE, [&]() {
1075         CHKPV(g_drawingInfo.parentNode);
1076         g_drawingInfo.parentNode->SetScale(pullThrowScale_ + BREATHE_SCALE);
1077         if (!g_drawingInfo.multiSelectedNodes.empty()) {
1078             size_t multiSelectedNodesSize = g_drawingInfo.multiSelectedNodes.size();
1079             for (size_t i = 0; i < multiSelectedNodesSize; ++i) {
1080                 std::shared_ptr<Rosen::RSCanvasNode> multiSelectedNode = g_drawingInfo.multiSelectedNodes[i];
1081                 CHKPV(multiSelectedNode);
1082                 multiSelectedNode->SetScale(pullThrowScale_ + BREATHE_SCALE);
1083             }
1084         }
1085     },  [&]() {
1086         FI_HILOGI("Breathe end");
1087     });
1088     Rosen::RSTransaction::FlushImplicitTransaction();
1089     FI_HILOGI("leave");
1090     return;
1091 }
1092 
PullThrowBreatheEndAnimation()1093 void DragDrawing::PullThrowBreatheEndAnimation()
1094 {
1095     FI_HILOGI("enter");
1096     auto parentNode = g_drawingInfo.parentNode;
1097     CHKPV(parentNode);
1098     Rosen::RSAnimationTimingProtocol BREATHE_TIMING_END_PROTOCOL(std::round(0.0f)); // animation time
1099     Rosen::RSAnimationTimingCurve BREATHE_CURVE = Rosen::RSAnimationTimingCurve::LINEAR;
1100 
1101     parentNode->SetScale(0.99f);
1102     Rosen::RSNode::Animate(BREATHE_TIMING_END_PROTOCOL, BREATHE_CURVE, [&]() {
1103         CHKPV(g_drawingInfo.parentNode);
1104         g_drawingInfo.parentNode->SetScale(1.0f);
1105     },  [&]() {
1106         FI_HILOGI("Breathe end Animation End");
1107     });
1108     Rosen::RSTransaction::FlushImplicitTransaction();
1109     FI_HILOGI("leave");
1110     return;
1111 }
1112 
PullThrowZoomOutAnimation()1113 void DragDrawing::PullThrowZoomOutAnimation()
1114 {
1115     FI_HILOGI("enter");
1116     auto parentNode = g_drawingInfo.parentNode;
1117     CHKPV(parentNode);
1118     Rosen::RSAnimationTimingProtocol zoomOutProtocol(std::round(ZOOMOUT_PULLTHROW)); // animation time
1119     Rosen::RSAnimationTimingCurve zoomOutCurve = Rosen::RSAnimationTimingCurve::LINEAR;
1120 
1121     Rosen::RSNode::Animate(zoomOutProtocol, zoomOutCurve, [&]() {
1122         CHKPV(g_drawingInfo.parentNode);
1123         g_drawingInfo.parentNode->SetScale(1.0f);
1124     },  [&]() {
1125         FI_HILOGI("PullThrowZoomOutAnimation End");
1126         PullThrowBreatheEndAnimation();
1127     });
1128     Rosen::RSTransaction::FlushImplicitTransaction();
1129     FI_HILOGI("leave");
1130     return;
1131 }
1132 #endif // OHOS_ENABLE_PULLTHROW
1133 
1134 #ifdef OHOS_BUILD_INTERNAL_DROP_ANIMATION
GetDragDrawingInfo(DragInternalInfo & dragInternalInfo)1135 void DragDrawing::GetDragDrawingInfo(DragInternalInfo &dragInternalInfo)
1136 {
1137     FI_HILOGI("enter");
1138     dragInternalInfo.positionX = g_drawingInfo.x;
1139     dragInternalInfo.positionY = g_drawingInfo.y;
1140     dragInternalInfo.scale = GetScaling();
1141     dragInternalInfo.pixelMapX = g_drawingInfo.pixelMapX;
1142     dragInternalInfo.pixelMapY = g_drawingInfo.pixelMapY;
1143     dragInternalInfo.argb = g_drawingInfo.filterInfo.argb;
1144     dragInternalInfo.rootNode = g_drawingInfo.rootNode;
1145     dragInternalInfo.parentNode = g_drawingInfo.parentNode;
1146     dragInternalInfo.curvesMaskNode = g_drawingInfo.curvesMaskNode;
1147     dragInternalInfo.lightNode = g_drawingInfo.lightNode;
1148     dragInternalInfo.currentPixelMap = AccessGlobalPixelMapLocked();
1149     dragInternalInfo.nodes = g_drawingInfo.nodes;
1150     dragInternalInfo.multiSelectedNodes = g_drawingInfo.multiSelectedNodes;
1151     dragInternalInfo.multiSelectedPixelMaps = g_drawingInfo.multiSelectedPixelMaps;
1152     dragInternalInfo.rotation = GetRotation(g_drawingInfo.displayId);
1153     dragInternalInfo.handler = handler_;
1154 
1155     sptr<Rosen::Display> display = Rosen::DisplayManager::GetInstance().GetDisplayById(g_drawingInfo.displayId);
1156     if (display == nullptr) {
1157         FI_HILOGD("Get display info failed, display:%{public}d", g_drawingInfo.displayId);
1158         display = Rosen::DisplayManager::GetInstance().GetDisplayById(0);
1159         if (display == nullptr) {
1160             FI_HILOGE("Get default display info failed");
1161             dragInternalInfo.displayWidth = 0;
1162             dragInternalInfo.displayHeight = 0;
1163             FI_HILOGI("leave");
1164             return;
1165         }
1166     }
1167     int32_t width = display->GetWidth();
1168     int32_t height = display->GetHeight();
1169     dragInternalInfo.displayWidth = width;
1170     dragInternalInfo.displayHeight = height;
1171     FI_HILOGI("leave");
1172     return;
1173 }
1174 
RemoveStyleNodeAnimations()1175 void DragDrawing::RemoveStyleNodeAnimations()
1176 {
1177     FI_HILOGI("enter");
1178     if (!CheckNodesValid()) {
1179         FI_HILOGE("Check nodes valid failed");
1180         return;
1181     }
1182     std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode = g_drawingInfo.nodes[DRAG_STYLE_INDEX];
1183     CHKPV(dragStyleNode);
1184     if (dragStyleNode != nullptr && drawStyleScaleModifier_ != nullptr) {
1185         dragStyleNode->RemoveModifier(drawStyleScaleModifier_);
1186         dragStyleNode->RemoveAllAnimations();
1187         drawStyleScaleModifier_ = nullptr;
1188         needBreakStyleScaleAnimation_ = true;
1189     }
1190     Rosen::RSTransaction::FlushImplicitTransaction();
1191     FI_HILOGI("leave");
1192 }
1193 #endif // OHOS_BUILD_INTERNAL_DROP_ANIMATION
1194 
LongPressDragZoomInAnimation()1195 void DragDrawing::LongPressDragZoomInAnimation()
1196 {
1197     FI_HILOGD("enter");
1198     if (!CheckNodesValid()) {
1199         FI_HILOGE("Check nodes valid failed");
1200         return;
1201     }
1202     CHKPV(g_drawingInfo.parentNode);
1203     g_drawingInfo.parentNode->SetScale(1.0f);
1204     if (!g_drawingInfo.multiSelectedNodes.empty()) {
1205         size_t multiSelectedNodesSize = g_drawingInfo.multiSelectedNodes.size();
1206         for (size_t i = 0; i < multiSelectedNodesSize; ++i) {
1207             std::shared_ptr<Rosen::RSCanvasNode> multiSelectedNode = g_drawingInfo.multiSelectedNodes[i];
1208             CHKPV(multiSelectedNode);
1209             multiSelectedNode->SetScale(1.0f);
1210         }
1211     }
1212     std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode = g_drawingInfo.nodes[DRAG_STYLE_INDEX];
1213     CHKPV(dragStyleNode);
1214     dragStyleNode->SetScale(1.0f);
1215 
1216     Rosen::RSAnimationTimingProtocol protocolZoomIn;
1217     protocolZoomIn.SetDuration(ZOOM_IN_DURATION);
1218     Rosen::RSNode::Animate(protocolZoomIn, CURVE, [&]() {
1219         CHKPV(g_drawingInfo.parentNode);
1220         g_drawingInfo.parentNode->SetScale(ZOOM_IN_SCALE);
1221         if (!g_drawingInfo.multiSelectedNodes.empty()) {
1222             size_t multiSelectedNodesSize = g_drawingInfo.multiSelectedNodes.size();
1223             for (size_t i = 0; i < multiSelectedNodesSize; ++i) {
1224                 std::shared_ptr<Rosen::RSCanvasNode> multiSelectedNode = g_drawingInfo.multiSelectedNodes[i];
1225                 CHKPV(multiSelectedNode);
1226                 multiSelectedNode->SetScale(ZOOM_IN_SCALE);
1227             }
1228         }
1229         std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode = g_drawingInfo.nodes[DRAG_STYLE_INDEX];
1230         CHKPV(dragStyleNode);
1231         dragStyleNode->SetScale(ZOOM_IN_SCALE);
1232     },  []() { FI_HILOGD("ZoomIn end"); });
1233 
1234     FI_HILOGD("leave");
1235     return;
1236 }
1237 
SetMultiSelectedAnimationFlag(bool needMultiSelectedAnimation)1238 void DragDrawing::SetMultiSelectedAnimationFlag(bool needMultiSelectedAnimation)
1239 {
1240     FI_HILOGI("needMultiSelectedAnimation:%{public}d", needMultiSelectedAnimation);
1241     needMultiSelectedAnimation_ = needMultiSelectedAnimation;
1242 }
1243 
LongPressDragZoomOutAnimation()1244 void DragDrawing::LongPressDragZoomOutAnimation()
1245 {
1246     FI_HILOGD("enter");
1247     if (!CheckNodesValid()) {
1248         FI_HILOGE("Check nodes valid failed");
1249         return;
1250     }
1251     Rosen::RSAnimationTimingProtocol protocolZoomOut;
1252     protocolZoomOut.SetDuration(ZOOM_OUT_DURATION);
1253     Rosen::RSNode::Animate(protocolZoomOut, CURVE, [&]() {
1254         CHKPV(g_drawingInfo.parentNode);
1255         g_drawingInfo.parentNode->SetScale(ZOOM_OUT_SCALE);
1256         if (!g_drawingInfo.multiSelectedNodes.empty()) {
1257             size_t multiSelectedNodesSize = g_drawingInfo.multiSelectedNodes.size();
1258             for (size_t i = 0; i < multiSelectedNodesSize; ++i) {
1259                 std::shared_ptr<Rosen::RSCanvasNode> multiSelectedNode = g_drawingInfo.multiSelectedNodes[i];
1260                 CHKPV(multiSelectedNode);
1261                 multiSelectedNode->SetScale(ZOOM_OUT_SCALE);
1262             }
1263         }
1264         std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode = g_drawingInfo.nodes[DRAG_STYLE_INDEX];
1265         CHKPV(dragStyleNode);
1266         dragStyleNode->SetScale(ZOOM_OUT_SCALE);
1267     },  []() { FI_HILOGD("ZoomOut end"); });
1268 
1269     g_drawingInfo.startNum = START_TIME;
1270     g_drawingInfo.needDestroyDragWindow = false;
1271     StartVsync();
1272     FI_HILOGD("leave");
1273     return;
1274 }
1275 
LongPressDragAnimation()1276 void DragDrawing::LongPressDragAnimation()
1277 {
1278     FI_HILOGD("enter");
1279     if (!CheckNodesValid()) {
1280         FI_HILOGE("Check nodes valid failed");
1281         return;
1282     }
1283     LongPressDragAlphaAnimation();
1284     LongPressDragZoomInAnimation();
1285     Rosen::RSAnimationTimingProtocol protocolZoomOut;
1286     protocolZoomOut.SetDuration(ZOOM_DURATION);
1287     Rosen::RSNode::Animate(protocolZoomOut, CURVE, [&]() {
1288         ShadowInfo shadowInfo;
1289         auto currentPixelMap = DragDrawing::AccessGlobalPixelMapLocked();
1290         CHKPV(currentPixelMap);
1291         float widthScale = CalculateWidthScale();
1292         currentPixelMap->scale(widthScale, widthScale, Media::AntiAliasingOption::HIGH);
1293         shadowInfo.pixelMap = currentPixelMap;
1294         shadowInfo.x = g_drawingInfo.pixelMapX * widthScale;
1295         shadowInfo.y = g_drawingInfo.pixelMapY * widthScale;
1296         UpdateShadowPic(shadowInfo);
1297     },  []() { FI_HILOGD("Scale zoom out end"); });
1298 
1299     g_drawingInfo.startNum = START_TIME;
1300     g_drawingInfo.needDestroyDragWindow = false;
1301     StartVsync();
1302     FI_HILOGD("leave");
1303     return;
1304 }
1305 
OnStartDrag(const DragAnimationData & dragAnimationData)1306 void DragDrawing::OnStartDrag(const DragAnimationData &dragAnimationData)
1307 {
1308     FI_HILOGI("enter");
1309     if (g_drawingInfo.nodes.size() <= PIXEL_MAP_INDEX) {
1310         FI_HILOGE("The index is out of bounds, node size is %{public}zu", g_drawingInfo.nodes.size());
1311         return;
1312     }
1313     std::shared_ptr<Rosen::RSCanvasNode> shadowNode = g_drawingInfo.nodes[PIXEL_MAP_INDEX];
1314     CHKPV(shadowNode);
1315     if (DrawShadow(shadowNode) != RET_OK) {
1316         FI_HILOGE("Draw shadow failed");
1317         return;
1318     }
1319     g_drawingInfo.isCurrentDefaultStyle = true;
1320     FI_HILOGI("leave");
1321 }
1322 
1323 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
OnStartDragExt()1324 void DragDrawing::OnStartDragExt()
1325 {
1326     if (dragExtHandler_ == nullptr) {
1327         FI_HILOGE("Fail to open drag drop extension library");
1328         return;
1329     }
1330     auto dragDropStartExtFunc = reinterpret_cast<DragStartExtFunc>(dlsym(dragExtHandler_, "OnStartDragExt"));
1331     if (dragDropStartExtFunc == nullptr) {
1332         FI_HILOGE("Fail to get drag drop extension function");
1333         CHKPV(dragExtHandler_);
1334         dlclose(dragExtHandler_);
1335         dragExtHandler_ = nullptr;
1336         return;
1337     }
1338 #ifdef OHOS_DRAG_ENABLE_ANIMATION
1339     if (!GetSuperHubHandler()->PostTask(
1340         [dragDropStartExtFunc] {
1341             return dragDropStartExtFunc(g_dragDataForSuperHub);
1342         })
1343     ) {
1344         FI_HILOGE("Start style animation failed");
1345     }
1346 #endif // OHOS_DRAG_ENABLE_ANIMATION
1347 }
1348 
NotifyDragInfo(const std::string & sourceName,const std::string & targetName)1349 void DragDrawing::NotifyDragInfo(const std::string &sourceName, const std::string &targetName)
1350 {
1351     FI_HILOGI("NotifyDragInfo");
1352     if (dragExtHandler_ == nullptr) {
1353         FI_HILOGE("Fail to open drag drop extension library");
1354         return;
1355     }
1356     auto dragDropExtFunc = reinterpret_cast<DragNotifyExtFunc>(dlsym(dragExtHandler_, "OnNotifyDragInfo"));
1357     if (dragDropExtFunc == nullptr) {
1358         FI_HILOGE("Fail to get drag drop extension function");
1359         CHKPV(dragExtHandler_);
1360         dlclose(dragExtHandler_);
1361         dragExtHandler_ = nullptr;
1362         return;
1363     }
1364     struct DragEventInfo dragEventInfo;
1365     dragEventInfo.sourcePkgName = sourceName;
1366     dragEventInfo.targetPkgName = targetName;
1367     GetSuperHubHandler();
1368     CHKPV(superHubHandler_);
1369     if (!superHubHandler_->PostTask([dragDropExtFunc, dragEventInfo] ()
1370         mutable { return dragDropExtFunc(dragEventInfo); })) {
1371         FI_HILOGE("notify drag info failed");
1372     }
1373 }
1374 
GetSuperHubHandler()1375 std::shared_ptr<AppExecFwk::EventHandler> DragDrawing::GetSuperHubHandler()
1376 {
1377     if (superHubHandler_ == nullptr) {
1378         auto runner = AppExecFwk::EventRunner::Create(SUPER_HUB_THREAD_NAME);
1379         superHubHandler_ = std::make_shared<AppExecFwk::EventHandler>(std::move(runner));
1380     }
1381     return superHubHandler_;
1382 }
1383 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1384 
AdjustDoubleValue(double doubleValue)1385 float DragDrawing::AdjustDoubleValue(double doubleValue)
1386 {
1387     FI_HILOGI("doubleValue is %{public}f", doubleValue);
1388     float dragOriginDpi = DRAG_DATA_MGR.GetDragOriginDpi();
1389     if (dragOriginDpi > EPSILON) {
1390         float scalingValue = GetScaling() / dragOriginDpi;
1391         doubleValue = doubleValue * scalingValue;
1392         if (fabs(scalingValue - 1.0f) > EPSILON) {
1393             float widthScale = CalculateWidthScale();
1394             doubleValue = doubleValue * widthScale;
1395         }
1396     }
1397     float floatValue = static_cast<float>(doubleValue);
1398     FI_HILOGI("floatValue is %{public}f", floatValue);
1399     return floatValue;
1400 }
1401 
CheckStyleNodeModifier(std::shared_ptr<Rosen::RSCanvasNode> styleNode)1402 void DragDrawing::CheckStyleNodeModifier(std::shared_ptr<Rosen::RSCanvasNode> styleNode)
1403 {
1404     FI_HILOGD("enter");
1405     CHKPV(styleNode);
1406     if (drawStyleChangeModifier_ != nullptr) {
1407         styleNode->RemoveModifier(drawStyleChangeModifier_);
1408         drawStyleChangeModifier_ = nullptr;
1409     }
1410     if (drawStyleScaleModifier_ != nullptr && hasRunningScaleAnimation_) {
1411         needBreakStyleScaleAnimation_ = true;
1412     }
1413     styleNode->RemoveAllAnimations();
1414     FI_HILOGD("leave");
1415 }
1416 
RemoveStyleNodeModifier(std::shared_ptr<Rosen::RSCanvasNode> styleNode)1417 void DragDrawing::RemoveStyleNodeModifier(std::shared_ptr<Rosen::RSCanvasNode> styleNode)
1418 {
1419     FI_HILOGD("enter");
1420     CHKPV(styleNode);
1421     if (drawStyleChangeModifier_ != nullptr) {
1422         styleNode->RemoveModifier(drawStyleChangeModifier_);
1423         drawStyleChangeModifier_ = nullptr;
1424     }
1425     if (drawStyleScaleModifier_ != nullptr) {
1426         styleNode->RemoveModifier(drawStyleScaleModifier_);
1427         drawStyleScaleModifier_ = nullptr;
1428     }
1429     FI_HILOGD("leave");
1430 }
1431 
UpdateAnimationProtocol(Rosen::RSAnimationTimingProtocol protocol)1432 void DragDrawing::UpdateAnimationProtocol(Rosen::RSAnimationTimingProtocol protocol)
1433 {
1434     FI_HILOGD("enter");
1435     g_drawingInfo.startNum = START_TIME;
1436     interruptNum_ = START_TIME * INTERRUPT_SCALE;
1437     hasRunningAnimation_ = true;
1438     bool stopSignal = true;
1439     CHKPV(rsUiDirector_);
1440     while (hasRunningAnimation_) {
1441         hasRunningAnimation_ = rsUiDirector_->FlushAnimation(g_drawingInfo.startNum);
1442         rsUiDirector_->FlushModifier();
1443         rsUiDirector_->SendMessages();
1444         if ((g_drawingInfo.startNum >= interruptNum_) && stopSignal) {
1445             protocol.SetDuration(TIME_STOP);
1446             stopSignal = false;
1447         }
1448         g_drawingInfo.startNum += INTERVAL_TIME;
1449         usleep(TIME_SLEEP);
1450     }
1451     FI_HILOGD("leave");
1452 }
1453 
StartStyleAnimation(float startScale,float endScale,int32_t duration)1454 void DragDrawing::StartStyleAnimation(float startScale, float endScale, int32_t duration)
1455 {
1456     FI_HILOGI("StartStyleAnimation, startScale is %{public}lf", startScale);
1457     if (!CheckNodesValid() || needBreakStyleScaleAnimation_ || hasRunningStopAnimation_) {
1458         FI_HILOGE("needBreakStyleScaleAnimation_ or hasRunningStopAnimation_, return");
1459         return;
1460     }
1461     if (g_drawingInfo.nodes.size() <= DRAG_STYLE_INDEX) {
1462         FI_HILOGE("The index is out of bounds, node size is %{public}zu", g_drawingInfo.nodes.size());
1463         return;
1464     }
1465     std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode = g_drawingInfo.nodes[DRAG_STYLE_INDEX];
1466     CHKPV(dragStyleNode);
1467     RemoveStyleNodeModifier(dragStyleNode);
1468     drawStyleScaleModifier_ = std::make_shared<DrawStyleScaleModifier>();
1469     dragStyleNode->AddModifier(drawStyleScaleModifier_);
1470     CHKPV(drawStyleScaleModifier_);
1471     drawStyleScaleModifier_->SetScale(startScale);
1472     Rosen::RSAnimationTimingProtocol protocol;
1473     protocol.SetDuration(duration);
1474     auto springCurveStyle = endScale == STYLE_END_SCALE
1475         ? Rosen::RSAnimationTimingCurve::CreateCubicCurve(BEZIER_030, BEZIER_000, BEZIER_040, BEZIER_100)
1476         : Rosen::RSAnimationTimingCurve::CreateCubicCurve(BEZIER_020, BEZIER_000, BEZIER_060, BEZIER_100);
1477     Rosen::RSNode::Animate(protocol, springCurveStyle, [&]() {
1478         if (drawStyleScaleModifier_ != nullptr) {
1479             drawStyleScaleModifier_->SetScale(endScale);
1480         }
1481     }, []() { FI_HILOGD("StartStyleAnimation end"); });
1482     UpdateAnimationProtocol(protocol);
1483     if (endScale == STYLE_CHANGE_SCALE) {
1484         if (drawStyleChangeModifier_ != nullptr) {
1485             dragStyleNode->RemoveModifier(drawStyleChangeModifier_);
1486             drawStyleChangeModifier_ = nullptr;
1487         }
1488         if (drawStyleScaleModifier_ != nullptr) {
1489             dragStyleNode->RemoveModifier(drawStyleScaleModifier_);
1490             drawStyleScaleModifier_ = nullptr;
1491         }
1492         drawStyleChangeModifier_ = std::make_shared<DrawStyleChangeModifier>(g_drawingInfo.stylePixelMap, isRTL_);
1493         dragStyleNode->AddModifier(drawStyleChangeModifier_);
1494     }
1495     if (endScale == STYLE_END_SCALE && drawStyleScaleModifier_ != nullptr) {
1496         dragStyleNode->RemoveModifier(drawStyleScaleModifier_);
1497         drawStyleScaleModifier_ = nullptr;
1498     }
1499 }
1500 
ChangeStyleAnimation()1501 void DragDrawing::ChangeStyleAnimation()
1502 {
1503     FI_HILOGD("enter");
1504     hasRunningScaleAnimation_ = true;
1505     StartStyleAnimation(START_STYLE_SCALE, STYLE_CHANGE_SCALE, TIME_DRAG_CHANGE_STYLE);
1506     StartStyleAnimation(STYLE_CHANGE_SCALE, STYLE_MAX_SCALE, TIME_DRAG_CHANGE_STYLE);
1507     StartStyleAnimation(STYLE_MAX_SCALE, STYLE_END_SCALE, TIME_DRAG_STYLE);
1508     needBreakStyleScaleAnimation_ = false;
1509     hasRunningScaleAnimation_ = false;
1510     FI_HILOGD("leave");
1511 }
1512 
OnDragStyleAnimation()1513 void DragDrawing::OnDragStyleAnimation()
1514 {
1515     FI_HILOGD("enter");
1516     if (!CheckNodesValid()) {
1517         FI_HILOGE("Check nodes valid failed");
1518         return;
1519     }
1520     if (g_drawingInfo.nodes.size() <= DRAG_STYLE_INDEX) {
1521         FI_HILOGE("The index is out of bounds, node size is %{public}zu", g_drawingInfo.nodes.size());
1522         return;
1523     }
1524     std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode = g_drawingInfo.nodes[DRAG_STYLE_INDEX];
1525     CHKPV(dragStyleNode);
1526     needBreakStyleScaleAnimation_ = false;
1527     if (g_drawingInfo.isPreviousDefaultStyle == true || g_drawingInfo.isCurrentDefaultStyle == true) {
1528         FI_HILOGE("Has DefaultStyle, change style and return");
1529         CheckStyleNodeModifier(dragStyleNode);
1530         drawStyleChangeModifier_ = std::make_shared<DrawStyleChangeModifier>(g_drawingInfo.stylePixelMap, isRTL_);
1531         dragStyleNode->AddModifier(drawStyleChangeModifier_);
1532         return;
1533     }
1534 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
1535     if (handler_ == nullptr) {
1536         auto runner = AppExecFwk::EventRunner::Create(THREAD_NAME);
1537         handler_ = std::make_shared<AppExecFwk::EventHandler>(std::move(runner));
1538     }
1539 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1540     CheckStyleNodeModifier(dragStyleNode);
1541 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
1542     CHKPV(handler_);
1543     handler_->RemoveTask("dragStyleAnimationTask");
1544     handler_->PostTask(std::bind(&DragDrawing::ChangeStyleAnimation, this), "dragStyleAnimationTask");
1545 #else
1546     ChangeStyleAnimation();
1547 #endif
1548     FI_HILOGD("leave");
1549 }
1550 
OnDragStyle(std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode,std::shared_ptr<Media::PixelMap> stylePixelMap)1551 void DragDrawing::OnDragStyle(std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode,
1552     std::shared_ptr<Media::PixelMap> stylePixelMap)
1553 {
1554     FI_HILOGD("enter");
1555     CHKPV(dragStyleNode);
1556     CHKPV(stylePixelMap);
1557 #ifdef OHOS_DRAG_ENABLE_ANIMATION
1558     if (handler_ == nullptr) {
1559         auto runner = AppExecFwk::EventRunner::Create(THREAD_NAME);
1560         CHKPV(runner);
1561         handler_ = std::make_shared<AppExecFwk::EventHandler>(std::move(runner));
1562     }
1563     if (drawSVGModifier_ != nullptr) {
1564         dragStyleNode->RemoveModifier(drawSVGModifier_);
1565         drawSVGModifier_ = nullptr;
1566     }
1567     CHKPV(handler_);
1568     if (!handler_->PostTask([this] { this->OnDragStyleAnimation(); })) {
1569         FI_HILOGE("Drag style animation failed");
1570         DrawStyle(dragStyleNode, stylePixelMap);
1571     }
1572 #else // OHOS_DRAG_ENABLE_ANIMATION
1573     DrawStyle(dragStyleNode, stylePixelMap);
1574 #endif // OHOS_DRAG_ENABLE_ANIMATION
1575     FI_HILOGD("leave");
1576 }
1577 
OnStopAnimationSuccess()1578 void DragDrawing::OnStopAnimationSuccess()
1579 {
1580     FI_HILOGI("enter");
1581     if (!CheckNodesValid()) {
1582         FI_HILOGE("Check nodes valid failed");
1583         return;
1584     }
1585     if (g_drawingInfo.nodes.size() <= DRAG_STYLE_INDEX) {
1586         FI_HILOGE("The index is out of bounds, node size is %{public}zu", g_drawingInfo.nodes.size());
1587         return;
1588     }
1589     std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode = g_drawingInfo.nodes[DRAG_STYLE_INDEX];
1590     if (dragStyleNode != nullptr && drawStyleScaleModifier_ != nullptr) {
1591         dragStyleNode->RemoveModifier(drawStyleScaleModifier_);
1592         dragStyleNode->RemoveAllAnimations();
1593         drawStyleScaleModifier_ = nullptr;
1594         needBreakStyleScaleAnimation_ = true;
1595     }
1596     CHKPV(g_drawingInfo.rootNode);
1597     hasRunningStopAnimation_ = true;
1598     if (drawDragStopModifier_ != nullptr) {
1599         g_drawingInfo.rootNode->RemoveModifier(drawDragStopModifier_);
1600         drawDragStopModifier_ = nullptr;
1601     }
1602     drawDragStopModifier_ = std::make_shared<DrawDragStopModifier>();
1603     g_drawingInfo.rootNode->AddModifier(drawDragStopModifier_);
1604     drawDragStopModifier_->SetAlpha(BEGIN_ALPHA);
1605     drawDragStopModifier_->SetScale(BEGIN_SCALE);
1606     drawDragStopModifier_->SetStyleScale(START_STYLE_SCALE);
1607     drawDragStopModifier_->SetStyleAlpha(START_STYLE_ALPHA);
1608     Rosen::RSAnimationTimingProtocol windowProtocol;
1609     Rosen::RSAnimationTimingProtocol styleProtocol;
1610     windowProtocol.SetDuration(TIME_STOP_SUCCESS_WINDOW);
1611     styleProtocol.SetDuration(TIME_STOP_SUCCESS_STYLE);
1612     auto springCurveSuccessWindow = Rosen::RSAnimationTimingCurve::CreateCubicCurve(BEZIER_040, BEZIER_000,
1613         BEZIER_100, BEZIER_100);
1614     auto springCurveSuccessStyle = Rosen::RSAnimationTimingCurve::CreateCubicCurve(BEZIER_000, BEZIER_000,
1615         BEZIER_100, BEZIER_100);
1616     Rosen::RSNode::Animate(windowProtocol, springCurveSuccessWindow, [&]() {
1617         drawDragStopModifier_->SetAlpha(BEGIN_ALPHA);
1618         drawDragStopModifier_->SetScale(END_SCALE_SUCCESS);
1619         Rosen::RSNode::Animate(styleProtocol, springCurveSuccessStyle, [&]() {
1620             drawDragStopModifier_->SetStyleAlpha(END_STYLE_ALPHA);
1621             drawDragStopModifier_->SetStyleScale(START_STYLE_SCALE);
1622         });
1623     },  []() { FI_HILOGD("OnStopAnimationSuccess end"); });
1624     DoEndAnimation();
1625     FI_HILOGI("leave");
1626 }
1627 
OnStopDragSuccess(std::shared_ptr<Rosen::RSCanvasNode> shadowNode,std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode)1628 void DragDrawing::OnStopDragSuccess(std::shared_ptr<Rosen::RSCanvasNode> shadowNode,
1629     std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode)
1630 {
1631     FI_HILOGD("enter");
1632     auto animateCb = [this] { return this->InitVSync(END_ALPHA, END_SCALE_SUCCESS); };
1633 #ifdef OHOS_DRAG_ENABLE_ANIMATION
1634     ResetAnimationParameter();
1635     auto runner = AppExecFwk::EventRunner::Create(THREAD_NAME);
1636     CHKPV(runner);
1637     handler_ = std::make_shared<AppExecFwk::EventHandler>(std::move(runner));
1638     CHKPV(handler_);
1639     if (!handler_->PostTask([this] { return this->OnStopAnimationSuccess(); })) {
1640         FI_HILOGE("Failed to stop style animation");
1641         RunAnimation(animateCb);
1642     }
1643 #else // OHOS_DRAG_ENABLE_ANIMATION
1644     RunAnimation(animateCb);
1645 #endif // OHOS_DRAG_ENABLE_ANIMATION
1646     FI_HILOGD("leave");
1647 }
1648 
OnStopAnimationFail()1649 void DragDrawing::OnStopAnimationFail()
1650 {
1651     FI_HILOGI("enter");
1652     if (!CheckNodesValid()) {
1653         FI_HILOGE("Check nodes valid failed");
1654         return;
1655     }
1656     if (g_drawingInfo.nodes.size() <= DRAG_STYLE_INDEX) {
1657         FI_HILOGE("The index is out of bounds, node size is %{public}zu", g_drawingInfo.nodes.size());
1658         return;
1659     }
1660     std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode = g_drawingInfo.nodes[DRAG_STYLE_INDEX];
1661     if (dragStyleNode != nullptr && drawStyleScaleModifier_ != nullptr) {
1662         dragStyleNode->RemoveModifier(drawStyleScaleModifier_);
1663         dragStyleNode->RemoveAllAnimations();
1664         drawStyleScaleModifier_ = nullptr;
1665         needBreakStyleScaleAnimation_ = true;
1666     }
1667     CHKPV(g_drawingInfo.rootNode);
1668     if (drawDragStopModifier_ != nullptr) {
1669         g_drawingInfo.rootNode->RemoveModifier(drawDragStopModifier_);
1670         drawDragStopModifier_ = nullptr;
1671     }
1672     drawDragStopModifier_ = std::make_shared<DrawDragStopModifier>();
1673     hasRunningStopAnimation_ = true;
1674     g_drawingInfo.rootNode->AddModifier(drawDragStopModifier_);
1675     drawDragStopModifier_->SetAlpha(BEGIN_ALPHA);
1676     drawDragStopModifier_->SetScale(BEGIN_SCALE);
1677     drawDragStopModifier_->SetStyleScale(START_STYLE_SCALE);
1678     drawDragStopModifier_->SetStyleAlpha(START_STYLE_ALPHA);
1679     Rosen::RSAnimationTimingProtocol protocol;
1680     protocol.SetDuration(TIME_STOP_FAIL_WINDOW);
1681     auto springCurveFail = Rosen::RSAnimationTimingCurve::CreateCubicCurve(BEZIER_033, BEZIER_000,
1682         BEZIER_067, BEZIER_100);
1683     Rosen::RSNode::Animate(protocol, springCurveFail, [&]() {
1684         drawDragStopModifier_->SetAlpha(END_ALPHA);
1685         drawDragStopModifier_->SetScale(END_SCALE_FAIL);
1686         drawDragStopModifier_->SetStyleScale(START_STYLE_SCALE);
1687         drawDragStopModifier_->SetStyleAlpha(END_STYLE_ALPHA);
1688     }, []() { FI_HILOGD("OnStopAnimationFail end"); });
1689     DoEndAnimation();
1690     FI_HILOGI("leave");
1691 }
1692 
OnStopDragFail(std::shared_ptr<Rosen::RSSurfaceNode> surfaceNode,std::shared_ptr<Rosen::RSNode> rootNode)1693 void DragDrawing::OnStopDragFail(std::shared_ptr<Rosen::RSSurfaceNode> surfaceNode,
1694     std::shared_ptr<Rosen::RSNode> rootNode)
1695 {
1696     FI_HILOGD("enter");
1697     auto animateCb = [this] { return this->InitVSync(END_ALPHA, END_SCALE_FAIL); };
1698 #ifdef OHOS_DRAG_ENABLE_ANIMATION
1699     ResetAnimationParameter();
1700     auto runner = AppExecFwk::EventRunner::Create(THREAD_NAME);
1701     CHKPV(runner);
1702     handler_ = std::make_shared<AppExecFwk::EventHandler>(std::move(runner));
1703     CHKPV(handler_);
1704     if (!handler_->PostTask([this] { this->OnStopAnimationFail(); })) {
1705         FI_HILOGE("Failed to stop style animation");
1706         RunAnimation(animateCb);
1707     }
1708 #else // OHOS_DRAG_ENABLE_ANIMATION
1709     RunAnimation(animateCb);
1710 #endif // OHOS_DRAG_ENABLE_ANIMATION
1711     FI_HILOGD("leave");
1712 }
1713 
OnStopAnimation()1714 void DragDrawing::OnStopAnimation()
1715 {
1716     FI_HILOGD("enter");
1717 }
1718 
RunAnimation(std::function<int32_t ()> cb)1719 int32_t DragDrawing::RunAnimation(std::function<int32_t()> cb)
1720 {
1721     FI_HILOGD("enter");
1722     ResetAnimationParameter();
1723 #ifndef IOS_PLATFORM
1724     auto runner = AppExecFwk::EventRunner::Create(THREAD_NAME);
1725 #else
1726     auto runner = AppExecFwk::EventRunner::Current(); // IOS animation can run main thread
1727 #endif // IOS_PLATFORM
1728     CHKPR(runner, RET_ERR);
1729     handler_ = std::make_shared<AppExecFwk::EventHandler>(std::move(runner));
1730     if (!handler_->PostTask(cb)) {
1731         FI_HILOGE("Send vsync event failed");
1732         return RET_ERR;
1733     }
1734     FI_HILOGD("leave");
1735     return RET_OK;
1736 }
1737 
DrawShadow(std::shared_ptr<Rosen::RSCanvasNode> shadowNode)1738 int32_t DragDrawing::DrawShadow(std::shared_ptr<Rosen::RSCanvasNode> shadowNode)
1739 {
1740     FI_HILOGD("enter");
1741     CHKPR(shadowNode, RET_ERR);
1742     if (drawPixelMapModifier_ != nullptr) {
1743         shadowNode->RemoveModifier(drawPixelMapModifier_);
1744         drawPixelMapModifier_ = nullptr;
1745     }
1746     drawPixelMapModifier_ = std::make_shared<DrawPixelMapModifier>();
1747     shadowNode->AddModifier(drawPixelMapModifier_);
1748     FilterInfo filterInfo = g_drawingInfo.filterInfo;
1749     Rosen::Vector4f cornerRadiusVector = { filterInfo.cornerRadius1, filterInfo.cornerRadius2,
1750         filterInfo.cornerRadius3, filterInfo.cornerRadius4 };
1751     shadowNode->SetCornerRadius(cornerRadiusVector * filterInfo.dipScale * filterInfo.scale);
1752     shadowNode->SetAlpha(filterInfo.opacity);
1753     FI_HILOGD("leave");
1754     return RET_OK;
1755 }
1756 
DrawMouseIcon()1757 int32_t DragDrawing::DrawMouseIcon()
1758 {
1759     FI_HILOGD("enter");
1760     if (g_drawingInfo.nodes.size() < MOUSE_NODE_MIN_COUNT) {
1761         FI_HILOGE("Nodes size invalid, node size:%{public}zu", g_drawingInfo.nodes.size());
1762         return RET_ERR;
1763     }
1764     if (g_drawingInfo.nodes.size() <= MOUSE_ICON_INDEX) {
1765         FI_HILOGE("The index is out of bounds, node size is %{public}zu", g_drawingInfo.nodes.size());
1766         return RET_ERR;
1767     }
1768     std::shared_ptr<Rosen::RSCanvasNode> mouseIconNode = g_drawingInfo.nodes[MOUSE_ICON_INDEX];
1769     CHKPR(mouseIconNode, RET_ERR);
1770     if (drawMouseIconModifier_ != nullptr) {
1771         mouseIconNode->RemoveModifier(drawMouseIconModifier_);
1772         drawMouseIconModifier_ = nullptr;
1773     }
1774 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
1775     int32_t ret = MMI::InputManager::GetInstance()->GetPointerStyle(GLOBAL_WINDOW_ID, pointerStyle_);
1776     if (ret != RET_OK) {
1777         FI_HILOGE("Get pointer style failed, ret:%{public}d", ret);
1778         return RET_ERR;
1779     }
1780     drawMouseIconModifier_ = std::make_shared<DrawMouseIconModifier>(pointerStyle_);
1781     mouseIconNode->AddModifier(drawMouseIconModifier_);
1782 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1783     FI_HILOGD("leave");
1784     return RET_OK;
1785 }
1786 
FlushDragPosition(uint64_t nanoTimestamp)1787 void DragDrawing::FlushDragPosition(uint64_t nanoTimestamp)
1788 {
1789 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
1790     if (dragState_ == DragState::MOTION_DRAGGING) {
1791         FI_HILOGD("Current in MOTION_DRAGGING, skip");
1792         return;
1793     }
1794     if (rsUiDirector_ != nullptr) {
1795         rsUiDirector_->SetTimeStamp(nanoTimestamp, RENDER_THREAD_NAME);
1796     } else {
1797         FI_HILOGE("rsUiDirector_ is nullptr");
1798     }
1799     DragMoveEvent event = dragSmoothProcessor_.SmoothMoveEvent(nanoTimestamp,
1800         vSyncStation_.GetVSyncPeriod());
1801     FI_HILOGD("Move position x:%{private}f, y:%{private}f, timestamp:%{public}" PRId64
1802         "displayId:%{public}d", event.displayX, event.displayY, event.timestamp, event.displayId);
1803     Rosen::Rotation currentRotation = GetRotation(event.displayId);
1804     StartTrace(HITRACE_TAG_MSDP, "OnDragMove,displayX:" + std::to_string(event.displayX)
1805         + ",displayY:" + std::to_string(event.displayY) + "displayId:" + std::to_string(event.displayId)
1806         + ",rotation:" + std::to_string(static_cast<uint32_t>(currentRotation)));
1807     if (DragWindowRotationFlush_ == currentRotation) {
1808         UpdateDragPosition(event.displayId, event.displayX, event.displayY);
1809         vSyncStation_.RequestFrame(TYPE_FLUSH_DRAG_POSITION, frameCallback_);
1810     }
1811     DragWindowRotationFlush_ = currentRotation;
1812     FinishTrace(HITRACE_TAG_MSDP);
1813 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1814 }
1815 
OnDragMove(int32_t displayId,int32_t displayX,int32_t displayY,int64_t actionTime)1816 void DragDrawing::OnDragMove(int32_t displayId, int32_t displayX, int32_t displayY, int64_t actionTime)
1817 {
1818     if (screenRotateState_) {
1819         screenRotateState_ = false;
1820     }
1821     if (isRunningRotateAnimation_) {
1822         FI_HILOGD("Doing rotate drag window animate, ignore draw drag window");
1823         return;
1824     }
1825 #ifdef IOS_PLATFORM
1826     actionTime_ = actionTime;
1827 #endif // IOS_PLATFORM
1828 
1829 #ifdef OHOS_BUILD_PC_PRODUCT
1830     if (g_drawingInfo.sourceType == MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
1831         UpdateDragPosition(displayId, displayX, displayY);
1832         return;
1833     }
1834 #endif // OHOS_BUILD_PC_PRODUCT
1835 
1836 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
1837     std::chrono::microseconds microseconds(actionTime);
1838     TimeStamp time(microseconds);
1839     uint64_t actionTimeCount = static_cast<uint64_t>(time.time_since_epoch().count());
1840     DragMoveEvent event = {
1841         .displayX = displayX,
1842         .displayY = displayY,
1843         .displayId = displayId,
1844         .timestamp = actionTimeCount,
1845     };
1846     dragSmoothProcessor_.InsertEvent(event);
1847     if (frameCallback_ == nullptr) {
1848         frameCallback_ = std::make_shared<DragFrameCallback>([this](uint64_t nanoTimestamp) {
1849             this->FlushDragPosition(nanoTimestamp);
1850         });
1851     }
1852     vSyncStation_.RequestFrame(TYPE_FLUSH_DRAG_POSITION, frameCallback_);
1853 #else
1854     UpdateDragPosition(displayId, displayX, displayY);
1855 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1856 }
1857 
1858 #ifdef OHOS_ENABLE_PULLTHROW
OnPullThrowDragMove(int32_t displayId,int32_t displayX,int32_t displayY,int64_t actionTime)1859 void DragDrawing::OnPullThrowDragMove(int32_t displayId, int32_t displayX, int32_t displayY, int64_t actionTime)
1860 {
1861     FI_HILOGD("enter");
1862 
1863     if (screenRotateState_) {
1864         screenRotateState_ = false;
1865     }
1866     if (isRunningRotateAnimation_) {
1867         FI_HILOGD("Doing rotate drag window animate, ignore draw drag window");
1868         return;
1869     }
1870 #ifdef IOS_PLATFORM
1871     actionTime_ = actionTime;
1872 #endif // IOS_PLATFORM
1873     if (g_drawingInfo.sourceType == MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
1874         UpdateDragPosition(displayId, displayX, displayY);
1875         return;
1876     }
1877     UpdateDragPosition(displayId, displayX, displayY);
1878 }
1879 #endif // OHOS_ENABLE_PULLTHROW
1880 
DrawStyle(std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode,std::shared_ptr<Media::PixelMap> stylePixelMap)1881 int32_t DragDrawing::DrawStyle(std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode,
1882     std::shared_ptr<Media::PixelMap> stylePixelMap)
1883 {
1884     FI_HILOGD("enter");
1885     CHKPR(dragStyleNode, RET_ERR);
1886     CHKPR(stylePixelMap, RET_ERR);
1887     if (drawSVGModifier_ != nullptr) {
1888         dragStyleNode->RemoveModifier(drawSVGModifier_);
1889         drawSVGModifier_ = nullptr;
1890     }
1891     drawSVGModifier_ = std::make_shared<DrawSVGModifier>(stylePixelMap, isRTL_);
1892     dragStyleNode->AddModifier(drawSVGModifier_);
1893     FI_HILOGD("leave");
1894     return RET_OK;
1895 }
1896 
InitVSync(float endAlpha,float endScale)1897 int32_t DragDrawing::InitVSync(float endAlpha, float endScale)
1898 {
1899     FI_HILOGD("enter");
1900     CHKPR(g_drawingInfo.rootNode, RET_ERR);
1901     if (drawDynamicEffectModifier_ != nullptr) {
1902         g_drawingInfo.rootNode->RemoveModifier(drawDynamicEffectModifier_);
1903         drawDynamicEffectModifier_ = nullptr;
1904     }
1905     drawDynamicEffectModifier_ = std::make_shared<DrawDynamicEffectModifier>();
1906     g_drawingInfo.rootNode->AddModifier(drawDynamicEffectModifier_);
1907     drawDynamicEffectModifier_->SetAlpha(BEGIN_ALPHA);
1908     drawDynamicEffectModifier_->SetScale(BEGIN_SCALE);
1909 
1910     Rosen::RSAnimationTimingProtocol protocol;
1911     protocol.SetDuration(SUCCESS_ANIMATION_DURATION);
1912     Rosen::RSNode::Animate(protocol, Rosen::RSAnimationTimingCurve::EASE_IN_OUT, [&]() {
1913         drawDynamicEffectModifier_->SetAlpha(endAlpha);
1914         drawDynamicEffectModifier_->SetScale(endScale);
1915     },  []() { FI_HILOGD("InitVSync end"); });
1916     Rosen::RSTransaction::FlushImplicitTransaction();
1917     DoEndAnimation();
1918     FI_HILOGD("leave");
1919     return RET_OK;
1920 }
1921 
StartVsync()1922 int32_t DragDrawing::StartVsync()
1923 {
1924     FI_HILOGI("enter");
1925     auto currentReceiver = AccessReceiverLocked();
1926     if (currentReceiver == nullptr) {
1927         CHKPR(handler_, RET_ERR);
1928         currentReceiver = Rosen::RSInterfaces::GetInstance().CreateVSyncReceiver("DragDrawing", handler_);
1929         CHKPR(currentReceiver, RET_ERR);
1930         UpdateReceiverLocked(currentReceiver);
1931     }
1932 #ifdef IOS_PLATFORM
1933     rsUiDirector_->FlushAnimation(g_drawingInfo.startNum);
1934 #endif // IOS_PLATFORM
1935     int32_t ret = currentReceiver->Init();
1936     if (ret != RET_OK) {
1937         FI_HILOGE("Receiver init failed");
1938         return RET_ERR;
1939     }
1940     Rosen::VSyncReceiver::FrameCallback fcb = {
1941         .userData_ = this,
1942         .callback_ = [this](int64_t parm1, void *parm2) { this->OnVsync(); }
1943     };
1944     ret = currentReceiver->RequestNextVSync(fcb);
1945     if (ret != RET_OK) {
1946         FI_HILOGE("Request next vsync failed");
1947     }
1948     FI_HILOGI("leave");
1949     return ret;
1950 }
1951 
OnVsync()1952 void DragDrawing::OnVsync()
1953 {
1954     FI_HILOGD("enter");
1955     CHKPV(rsUiDirector_);
1956     bool hasRunningAnimation = rsUiDirector_->FlushAnimation(g_drawingInfo.startNum);
1957     rsUiDirector_->FlushModifier();
1958     rsUiDirector_->SendMessages();
1959     if (!hasRunningAnimation) {
1960         FI_HILOGI("Stop runner, hasRunningAnimation:%{public}d, needDestroyDragWindow:%{public}d",
1961             hasRunningAnimation, g_drawingInfo.needDestroyDragWindow.load());
1962         if (g_drawingInfo.needDestroyDragWindow) {
1963             ResetAnimationFlag();
1964         }
1965         return;
1966     }
1967     Rosen::VSyncReceiver::FrameCallback fcb = {
1968         .userData_ = this,
1969         .callback_ = [this](int64_t parm1, void *parm2) { this->OnVsync(); }
1970     };
1971     auto currentReceiver = AccessReceiverLocked();
1972     CHKPV(currentReceiver);
1973     int32_t ret = currentReceiver->RequestNextVSync(fcb);
1974     if (ret != RET_OK) {
1975         FI_HILOGE("Request next vsync failed");
1976     }
1977     rsUiDirector_->SendMessages();
1978     g_drawingInfo.startNum += INTERVAL_TIME;
1979     FI_HILOGD("leave");
1980 }
1981 
InitDrawingInfo(const DragData & dragData,bool isLongPressDrag)1982 void DragDrawing::InitDrawingInfo(const DragData &dragData, bool isLongPressDrag)
1983 {
1984     g_drawingInfo.isRunning = true;
1985     if (dragData.shadowInfos.empty()) {
1986         FI_HILOGE("ShadowInfos is empty");
1987         return;
1988     }
1989     DragDrawing::UpdataGlobalPixelMapLocked(dragData.shadowInfos.front().pixelMap);
1990     g_drawingInfo.pixelMapX = dragData.shadowInfos.front().x;
1991     g_drawingInfo.pixelMapY = dragData.shadowInfos.front().y;
1992     float dragOriginDpi = DRAG_DATA_MGR.GetDragOriginDpi();
1993     if (dragOriginDpi > EPSILON) {
1994         float scalingValue = GetScaling() / dragOriginDpi;
1995         auto currentPixelMap = DragDrawing::AccessGlobalPixelMapLocked();
1996         CHKPV(currentPixelMap);
1997         currentPixelMap->scale(scalingValue, scalingValue, Media::AntiAliasingOption::HIGH);
1998         g_drawingInfo.pixelMapX = g_drawingInfo.pixelMapX * scalingValue;
1999         g_drawingInfo.pixelMapY = g_drawingInfo.pixelMapY * scalingValue;
2000         if (fabs(scalingValue - 1.0f) > EPSILON) {
2001             float widthScale = CalculateWidthScale();
2002             CHKPV(currentPixelMap);
2003             currentPixelMap->scale(widthScale, widthScale, Media::AntiAliasingOption::HIGH);
2004             g_drawingInfo.pixelMapX = g_drawingInfo.pixelMapX * widthScale;
2005             g_drawingInfo.pixelMapY = g_drawingInfo.pixelMapY * widthScale;
2006         }
2007     }
2008     g_drawingInfo.currentDragNum = dragData.dragNum;
2009     g_drawingInfo.sourceType = dragData.sourceType;
2010     g_drawingInfo.displayId = dragData.displayId;
2011     g_drawingInfo.displayX = dragData.displayX;
2012     g_drawingInfo.displayY = dragData.displayY;
2013     RotateDisplayXY(g_drawingInfo.displayX, g_drawingInfo.displayY);
2014     if (!ParserExtraInfo(dragData.extraInfo, g_drawingInfo.extraInfo)) {
2015         FI_HILOGI("No parser valid extraInfo data");
2016     }
2017     if (!ParserFilterInfo(dragData.filterInfo, g_drawingInfo.filterInfo)) {
2018         FI_HILOGI("No parser valid filterInfo data");
2019     }
2020     size_t shadowInfosSize = dragData.shadowInfos.size();
2021     for (size_t i = 1; i < shadowInfosSize; ++i) {
2022         std::shared_ptr<Media::PixelMap> pixelMap = dragData.shadowInfos[i].pixelMap;
2023         if (dragOriginDpi > EPSILON) {
2024             float scalingValue = GetScaling() / dragOriginDpi;
2025             CHKPV(pixelMap);
2026             pixelMap->scale(scalingValue, scalingValue, Media::AntiAliasingOption::HIGH);
2027         }
2028         g_drawingInfo.multiSelectedPixelMaps.emplace_back(pixelMap);
2029     }
2030 }
2031 
InitDragAnimationData(DragAnimationData & dragAnimationData)2032 int32_t DragDrawing::InitDragAnimationData(DragAnimationData &dragAnimationData)
2033 {
2034     auto currentPixelMap = DragDrawing::AccessGlobalPixelMapLocked();
2035     CHKPR(currentPixelMap, RET_ERR);
2036     dragAnimationData.pixelMap = currentPixelMap;
2037     dragAnimationData.displayX = g_drawingInfo.displayX;
2038     dragAnimationData.displayY = g_drawingInfo.displayY;
2039     dragAnimationData.offsetX = g_drawingInfo.pixelMapX;
2040     dragAnimationData.offsetY = g_drawingInfo.pixelMapY;
2041     return RET_OK;
2042 }
2043 
InitLayer()2044 int32_t DragDrawing::InitLayer()
2045 {
2046     FI_HILOGI("enter");
2047     if (g_drawingInfo.surfaceNode == nullptr) {
2048         FI_HILOGE("Init layer failed, surfaceNode is nullptr");
2049         return RET_ERR;
2050     }
2051 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
2052     auto surface = g_drawingInfo.surfaceNode->GetSurface();
2053     if (surface == nullptr) {
2054         g_drawingInfo.surfaceNode->DetachFromWindowContainer(g_drawingInfo.displayId);
2055         g_drawingInfo.surfaceNode = nullptr;
2056         FI_HILOGE("Init layer failed, surface is nullptr");
2057         Rosen::RSTransaction::FlushImplicitTransaction();
2058         return RET_ERR;
2059     }
2060 #endif // OHOS_BUILD_ENABLE_ARKUI_X
2061     if (g_drawingInfo.isInitUiDirector) {
2062         g_drawingInfo.isInitUiDirector = false;
2063         rsUiDirector_ = Rosen::RSUIDirector::Create();
2064         CHKPR(rsUiDirector_, RET_ERR);
2065         rsUiDirector_->Init();
2066         rsUiDirector_->SetUITaskRunner([this](const std::function<void()>& task, uint32_t delay = 0) {
2067             CHKPV(this->handler_);
2068             this->handler_->PostTask(task, delay);
2069         });
2070     }
2071     rsUiDirector_->SetRSSurfaceNode(g_drawingInfo.surfaceNode);
2072 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
2073     int32_t rootNodeSize = std::max(displayWidth_, displayHeight_);
2074     InitCanvas(rootNodeSize, rootNodeSize);
2075     FI_HILOGI("Root node size:%{public}d, display Width:%{public}d, display height:%{public}d",
2076         rootNodeSize, displayWidth_, displayHeight_);
2077 #else
2078     CHKPR(window_, RET_ERR);
2079     InitCanvas(window_->GetRect().width_, window_->GetRect().height_);
2080 #endif // OHOS_BUILD_ENABLE_ARKUI_X
2081     Rosen::Rotation rotation = GetRotation(g_drawingInfo.displayId);
2082     if (rotation != Rosen::Rotation::ROTATION_0) {
2083         RotateDragWindow(rotation);
2084     } else {
2085         DragWindowRotateInfo_.rotation = ROTATION_0;
2086     }
2087 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
2088     DragWindowRotationFlush_ = rotation;
2089 #endif // OHOS_BUILD_ENABLE_ARKUI_X
2090     Rosen::RSTransaction::FlushImplicitTransaction();
2091     FI_HILOGI("leave");
2092     return RET_OK;
2093 }
2094 
InitCanvas(int32_t width,int32_t height)2095 void DragDrawing::InitCanvas(int32_t width, int32_t height)
2096 {
2097     FI_HILOGI("enter");
2098     if (g_drawingInfo.rootNode == nullptr) {
2099         g_drawingInfo.rootNode = Rosen::RSRootNode::Create();
2100         CHKPV(g_drawingInfo.rootNode);
2101     }
2102     g_drawingInfo.rootNode->SetBounds(0, 0, width, height);
2103     g_drawingInfo.rootNode->SetFrame(0, 0, width, height);
2104     g_drawingInfo.rootNode->SetBackgroundColor(SK_ColorTRANSPARENT);
2105     std::shared_ptr<Rosen::RSCanvasNode> filterNode = Rosen::RSCanvasNode::Create();
2106     CHKPV(filterNode);
2107     g_drawingInfo.nodes.emplace_back(filterNode);
2108     ProcessFilter();
2109     std::shared_ptr<Rosen::RSCanvasNode> pixelMapNode = Rosen::RSCanvasNode::Create();
2110     CHKPV(pixelMapNode);
2111     pixelMapNode->SetForegroundColor(TRANSPARENT_COLOR_ARGB);
2112     pixelMapNode->SetGrayScale(g_drawingInfo.filterInfo.dragNodeGrayscale);
2113     g_drawingInfo.nodes.emplace_back(pixelMapNode);
2114     std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode = Rosen::RSCanvasNode::Create();
2115     CHKPV(dragStyleNode);
2116     g_drawingInfo.nodes.emplace_back(dragStyleNode);
2117     if (g_drawingInfo.parentNode == nullptr) {
2118         g_drawingInfo.parentNode = Rosen::RSCanvasNode::Create();
2119         CHKPV(g_drawingInfo.parentNode);
2120     }
2121     g_drawingInfo.parentNode->AddChild(filterNode);
2122     g_drawingInfo.parentNode->AddChild(pixelMapNode);
2123     if (!g_drawingInfo.multiSelectedPixelMaps.empty()) {
2124         InitMultiSelectedNodes();
2125         if (!g_drawingInfo.multiSelectedNodes.empty()) {
2126             size_t multiSelectedNodesSize = g_drawingInfo.multiSelectedNodes.size();
2127             for (size_t i = 0; i < multiSelectedNodesSize; ++i) {
2128                 g_drawingInfo.rootNode->AddChild(g_drawingInfo.multiSelectedNodes[i]);
2129             }
2130         }
2131     }
2132     g_drawingInfo.rootNode->AddChild(g_drawingInfo.parentNode);
2133     CHKPV(rsUiDirector_);
2134 #ifndef OHOS_BUILD_PC_PRODUCT
2135     if (g_drawingInfo.sourceType == MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
2136         std::shared_ptr<Rosen::RSCanvasNode> mouseIconNode = Rosen::RSCanvasNode::Create();
2137         CHKPV(mouseIconNode);
2138         g_drawingInfo.nodes.emplace_back(mouseIconNode);
2139         g_drawingInfo.rootNode->AddChild(mouseIconNode);
2140         rsUiDirector_->SetRSRootNode(Rosen::RSBaseNode::ReinterpretCast<Rosen::RSRootNode>(g_drawingInfo.rootNode));
2141         FI_HILOGI("leave");
2142         return;
2143     }
2144 #endif // OHOS_BUILD_PC_PRODUCT
2145     rsUiDirector_->SetRSRootNode(Rosen::RSBaseNode::ReinterpretCast<Rosen::RSRootNode>(g_drawingInfo.rootNode));
2146     FI_HILOGI("leave");
2147 }
2148 
CreateWindow()2149 void DragDrawing::CreateWindow()
2150 {
2151 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
2152     FI_HILOGI("Parameter screen number:%{public}llu", static_cast<unsigned long long>(screenId_));
2153     Rosen::RSSurfaceNodeConfig surfaceNodeConfig;
2154     surfaceNodeConfig.SurfaceNodeName = "drag window";
2155     surfaceNodeConfig.surfaceWindowType = Rosen::SurfaceWindowType::SYSTEM_SCB_WINDOW;
2156     Rosen::RSSurfaceNodeType surfaceNodeType = Rosen::RSSurfaceNodeType::SELF_DRAWING_WINDOW_NODE;
2157     g_drawingInfo.surfaceNode = Rosen::RSSurfaceNode::Create(surfaceNodeConfig, surfaceNodeType);
2158     CHKPV(g_drawingInfo.surfaceNode);
2159 #ifndef OHOS_BUILD_PC_PRODUCT
2160     sptr<Rosen::Display> display = Rosen::DisplayManager::GetInstance().GetDisplayById(g_drawingInfo.displayId);
2161 #else
2162     sptr<Rosen::DisplayInfo> display =
2163         Rosen::DisplayManager::GetInstance().GetVisibleAreaDisplayInfoById(g_drawingInfo.displayId);
2164 #endif // OHOS_BUILD_PC_PRODUCT
2165     if (display == nullptr) {
2166         FI_HILOGD("Get display info failed, display:%{public}d", g_drawingInfo.displayId);
2167 #ifndef OHOS_BUILD_PC_PRODUCT
2168         display = Rosen::DisplayManager::GetInstance().GetDisplayById(0);
2169 #else
2170         display = Rosen::DisplayManager::GetInstance().GetVisibleAreaDisplayInfoById(0);
2171 #endif // OHOS_BUILD_PC_PRODUCT
2172         if (display == nullptr) {
2173             FI_HILOGE("Get display info failed, display is nullptr");
2174             return;
2175         }
2176     }
2177     uint64_t rsScreenId = screenId_;
2178     displayWidth_ = display->GetWidth();
2179     displayHeight_ = display->GetHeight();
2180 #ifndef OHOS_BUILD_PC_PRODUCT
2181     sptr<Rosen::Screen> screen = Rosen::ScreenManager::GetInstance().GetScreenById(screenId_);
2182     if ((screen != nullptr) && (!screen->IsReal())) {
2183         if (!Rosen::DisplayManager::GetInstance().ConvertScreenIdToRsScreenId(screenId_, rsScreenId)) {
2184             FI_HILOGE("ConvertScreenIdToRsScreenId failed");
2185             return;
2186         }
2187     }
2188 #else
2189     if (!Rosen::DisplayManager::GetInstance().ConvertScreenIdToRsScreenId(screenId_, rsScreenId)) {
2190         FI_HILOGE("ConvertScreenIdToRsScreenId failed");
2191         return;
2192     }
2193 #endif // OHOS_BUILD_PC_PRODUCT
2194     screenId_ = rsScreenId;
2195     FI_HILOGI("Parameter rsScreen number:%{public}llu", static_cast<unsigned long long>(rsScreenId));
2196     int32_t surfaceNodeSize = std::max(displayWidth_, displayHeight_);
2197     g_drawingInfo.surfaceNode->SetBounds(0, 0, surfaceNodeSize, surfaceNodeSize);
2198 #else
2199     CHKPV(window_);
2200     g_drawingInfo.surfaceNode = window_->GetSurfaceNode();
2201     CHKPV(g_drawingInfo.surfaceNode);
2202     g_drawingInfo.surfaceNode->SetBounds(0, 0, window_->GetRect().width_, window_->GetRect().height_);
2203 #endif // OHOS_BUILD_ENABLE_ARKUI_X
2204     g_drawingInfo.surfaceNode->SetFrameGravity(Rosen::Gravity::RESIZE_ASPECT_FILL);
2205     g_drawingInfo.surfaceNode->SetPositionZ(DRAG_WINDOW_POSITION_Z);
2206     g_drawingInfo.surfaceNode->SetBackgroundColor(SK_ColorTRANSPARENT);
2207 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
2208     g_drawingInfo.surfaceNode->AttachToWindowContainer(rsScreenId);
2209 #endif // OHOS_BUILD_ENABLE_ARKUI_X
2210     g_drawingInfo.surfaceNode->SetVisible(false);
2211     Rosen::RSTransaction::FlushImplicitTransaction();
2212 }
2213 
RemoveModifier()2214 void DragDrawing::RemoveModifier()
2215 {
2216     FI_HILOGD("enter");
2217     if ((g_drawingInfo.nodes.size() < TOUCH_NODE_MIN_COUNT)) {
2218         FI_HILOGE("Nodes size invalid, node size:%{public}zu", g_drawingInfo.nodes.size());
2219         return;
2220     }
2221 
2222     if (g_drawingInfo.nodes.size() <= PIXEL_MAP_INDEX || g_drawingInfo.nodes.size() <= DRAG_STYLE_INDEX) {
2223         FI_HILOGE("The index is out of bounds, node size is %{public}zu", g_drawingInfo.nodes.size());
2224         return;
2225     }
2226     std::shared_ptr<Rosen::RSCanvasNode> pixelMapNode = g_drawingInfo.nodes[PIXEL_MAP_INDEX];
2227     CHKPV(pixelMapNode);
2228     if (drawPixelMapModifier_ != nullptr) {
2229         pixelMapNode->RemoveModifier(drawPixelMapModifier_);
2230         drawPixelMapModifier_ = nullptr;
2231     }
2232     std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode = g_drawingInfo.nodes[DRAG_STYLE_INDEX];
2233     CHKPV(dragStyleNode);
2234     if (drawSVGModifier_ != nullptr) {
2235         dragStyleNode->RemoveModifier(drawSVGModifier_);
2236         drawSVGModifier_ = nullptr;
2237     }
2238 
2239     if (drawStyleChangeModifier_ != nullptr) {
2240         dragStyleNode->RemoveModifier(drawStyleChangeModifier_);
2241         drawStyleChangeModifier_ = nullptr;
2242     }
2243 
2244     if (drawStyleScaleModifier_ != nullptr) {
2245         dragStyleNode->RemoveModifier(drawStyleScaleModifier_);
2246         drawStyleScaleModifier_ = nullptr;
2247     }
2248     FI_HILOGD("leave");
2249 }
2250 
UpdateSvgNodeInfo(xmlNodePtr curNode,int32_t extendSvgWidth)2251 int32_t DragDrawing::UpdateSvgNodeInfo(xmlNodePtr curNode, int32_t extendSvgWidth)
2252 {
2253     FI_HILOGD("enter");
2254     if (xmlStrcmp(curNode->name, BAD_CAST "svg")) {
2255         FI_HILOGE("Svg format invalid");
2256         return RET_ERR;
2257     }
2258     std::ostringstream oStrStream;
2259     xmlChar* widthProp = xmlGetProp(curNode, BAD_CAST "width");
2260     oStrStream << widthProp;
2261     std::string srcSvgWidth = oStrStream.str();
2262     xmlFree(widthProp);
2263     if (srcSvgWidth.length() < STRING_PX_LENGTH) {
2264         FI_HILOGE("Svg width invalid, srcSvgWidth:%{public}s", srcSvgWidth.c_str());
2265         return RET_ERR;
2266     }
2267     srcSvgWidth = srcSvgWidth.substr(0, srcSvgWidth.length() - STRING_PX_LENGTH);
2268     if (!IsNum(srcSvgWidth)) {
2269         FI_HILOGE("srcSvgWidth is not digital, srcSvgWidth:%{public}s", srcSvgWidth.c_str());
2270         return RET_ERR;
2271     }
2272     int32_t number = std::stoi(srcSvgWidth) + extendSvgWidth;
2273     std::string tgtSvgWidth = std::to_string(number);
2274     tgtSvgWidth.append("px");
2275     xmlSetProp(curNode, BAD_CAST "width", BAD_CAST tgtSvgWidth.c_str());
2276     oStrStream.str("");
2277     xmlChar* viewBoxProp = xmlGetProp(curNode, BAD_CAST "viewBox");
2278     oStrStream << viewBoxProp;
2279     std::string srcViewBox = oStrStream.str();
2280     std::istringstream iStrStream(srcViewBox);
2281     xmlFree(viewBoxProp);
2282     std::string tmpString;
2283     std::string tgtViewBox;
2284     int32_t i = 0;
2285     while (iStrStream >> tmpString) {
2286         if (i == VIEW_BOX_POS) {
2287             if (!IsNum(tmpString)) {
2288                 FI_HILOGE("tmpString is not digital, tmpString:%{public}s", tmpString.c_str());
2289                 return RET_ERR;
2290             }
2291             number = std::stoi(tmpString) + extendSvgWidth;
2292             tmpString = std::to_string(number);
2293         }
2294         tgtViewBox.append(tmpString);
2295         tgtViewBox += " ";
2296         ++i;
2297     }
2298 
2299     xmlSetProp(curNode, BAD_CAST "viewBox", BAD_CAST tgtViewBox.c_str());
2300     FI_HILOGD("leave");
2301     return RET_OK;
2302 }
2303 
GetRectNode(xmlNodePtr curNode)2304 xmlNodePtr DragDrawing::GetRectNode(xmlNodePtr curNode)
2305 {
2306     FI_HILOGD("enter");
2307     curNode = curNode->xmlChildrenNode;
2308     while (curNode != nullptr) {
2309         if (!xmlStrcmp(curNode->name, BAD_CAST "g")) {
2310             while (!xmlStrcmp(curNode->name, BAD_CAST "g")) {
2311                 curNode = curNode->xmlChildrenNode;
2312             }
2313             break;
2314         }
2315         curNode = curNode->next;
2316     }
2317     FI_HILOGD("leave");
2318     return curNode;
2319 }
2320 
UpdateRectNode(int32_t extendSvgWidth,xmlNodePtr curNode)2321 xmlNodePtr DragDrawing::UpdateRectNode(int32_t extendSvgWidth, xmlNodePtr curNode)
2322 {
2323     FI_HILOGD("enter");
2324     while (curNode != nullptr) {
2325         if (!xmlStrcmp(curNode->name, BAD_CAST "rect")) {
2326             std::ostringstream oStrStream;
2327             xmlChar* widthProp = xmlGetProp(curNode, BAD_CAST "width");
2328             oStrStream << widthProp;
2329             std::string srcRectWidth = oStrStream.str();
2330             xmlFree(widthProp);
2331             if (!IsNum(srcRectWidth)) {
2332                 FI_HILOGE("srcRectWidth is not digital, srcRectWidth:%{public}s", srcRectWidth.c_str());
2333                 return nullptr;
2334             }
2335             int32_t number = std::stoi(srcRectWidth) + extendSvgWidth;
2336             xmlSetProp(curNode, BAD_CAST "width", BAD_CAST std::to_string(number).c_str());
2337         }
2338         if (!xmlStrcmp(curNode->name, BAD_CAST "text")) {
2339             return curNode->xmlChildrenNode;
2340         }
2341         curNode = curNode->next;
2342     }
2343     FI_HILOGE("Empty node of XML");
2344     return nullptr;
2345 }
2346 
UpdateTspanNode(xmlNodePtr curNode)2347 void DragDrawing::UpdateTspanNode(xmlNodePtr curNode)
2348 {
2349     FI_HILOGD("enter");
2350     while (curNode != nullptr) {
2351         if (!xmlStrcmp(curNode->name, BAD_CAST "tspan")) {
2352             xmlNodeSetContent(curNode, BAD_CAST std::to_string(g_drawingInfo.currentDragNum).c_str());
2353         }
2354         curNode = curNode->next;
2355     }
2356     FI_HILOGD("leave");
2357 }
2358 
ParseAndAdjustSvgInfo(xmlNodePtr curNode)2359 int32_t DragDrawing::ParseAndAdjustSvgInfo(xmlNodePtr curNode)
2360 {
2361     FI_HILOGD("enter");
2362     CHKPR(curNode, RET_ERR);
2363     std::string strStyle = std::to_string(g_drawingInfo.currentDragNum);
2364     if (strStyle.empty()) {
2365         FI_HILOGE("strStyle size:%{public}zu invalid", strStyle.size());
2366         return RET_ERR;
2367     }
2368     int32_t extendSvgWidth = (static_cast<int32_t>(strStyle.size()) - 1) * EIGHT_SIZE;
2369     xmlKeepBlanksDefault(0);
2370     int32_t ret = UpdateSvgNodeInfo(curNode, extendSvgWidth);
2371     if (ret != RET_OK) {
2372         FI_HILOGE("Update svg node info failed, ret:%{public}d", ret);
2373         return RET_ERR;
2374     }
2375     curNode = GetRectNode(curNode);
2376     CHKPR(curNode, RET_ERR);
2377     curNode = UpdateRectNode(extendSvgWidth, curNode);
2378     CHKPR(curNode, RET_ERR);
2379     UpdateTspanNode(curNode);
2380     FI_HILOGD("leave");
2381     return RET_OK;
2382 }
2383 
DecodeSvgToPixelMap(const std::string & filePath)2384 std::shared_ptr<Media::PixelMap> DragDrawing::DecodeSvgToPixelMap(
2385     const std::string &filePath)
2386 {
2387     FI_HILOGD("enter");
2388     xmlDocPtr xmlDoc = xmlReadFile(filePath.c_str(), 0, XML_PARSE_NOBLANKS);
2389     if (NeedAdjustSvgInfo()) {
2390         xmlNodePtr node = xmlDocGetRootElement(xmlDoc);
2391         CHKPP(node);
2392         int32_t ret = ParseAndAdjustSvgInfo(node);
2393         if (ret != RET_OK) {
2394             FI_HILOGE("Parse and adjust svg info failed, ret:%{public}d", ret);
2395             xmlFreeDoc(xmlDoc);
2396             return nullptr;
2397         }
2398     }
2399     xmlChar *xmlbuff = nullptr;
2400     int32_t buffersize = 0;
2401     xmlDocDumpFormatMemory(xmlDoc, &xmlbuff, &buffersize, 1);
2402     std::ostringstream oStrStream;
2403     oStrStream << xmlbuff;
2404     std::string content = oStrStream.str();
2405     xmlFree(xmlbuff);
2406     xmlFreeDoc(xmlDoc);
2407     Media::SourceOptions opts;
2408     opts.formatHint = "image/svg+xml";
2409     uint32_t errCode = 0;
2410     auto imageSource = Media::ImageSource::CreateImageSource(reinterpret_cast<const uint8_t*>(content.c_str()),
2411         content.size(), opts, errCode);
2412     CHKPP(imageSource);
2413     Media::DecodeOptions decodeOpts;
2414     SetDecodeOptions(decodeOpts);
2415     std::shared_ptr<Media::PixelMap> pixelMap = imageSource->CreatePixelMap(decodeOpts, errCode);
2416     FI_HILOGD("leave");
2417     return pixelMap;
2418 }
2419 
NeedAdjustSvgInfo()2420 bool DragDrawing::NeedAdjustSvgInfo()
2421 {
2422     FI_HILOGD("enter");
2423     if (g_drawingInfo.currentStyle == DragCursorStyle::DEFAULT) {
2424         return false;
2425     }
2426     if ((g_drawingInfo.currentStyle == DragCursorStyle::COPY) &&
2427         (g_drawingInfo.currentDragNum == DRAG_NUM_ONE)) {
2428         return false;
2429     }
2430     if ((g_drawingInfo.currentStyle == DragCursorStyle::MOVE) &&
2431         (g_drawingInfo.currentDragNum == DRAG_NUM_ONE)) {
2432         return false;
2433     }
2434     if ((g_drawingInfo.currentStyle == DragCursorStyle::FORBIDDEN) &&
2435         (g_drawingInfo.currentDragNum == DRAG_NUM_ONE)) {
2436         return false;
2437     }
2438     FI_HILOGD("leave");
2439     return true;
2440 }
2441 
GetFilePath(std::string & filePath)2442 void DragDrawing::GetFilePath(std::string &filePath)
2443 {
2444     FI_HILOGD("enter");
2445 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
2446     if (isRTL_) {
2447         GetRTLFilePath(filePath);
2448     } else {
2449         GetLTRFilePath(filePath);
2450     }
2451 #else
2452     GetLTRFilePath(filePath);
2453 #endif // OHOS_BUILD_ENABLE_ARKUI_X
2454     FI_HILOGD("leave");
2455 }
2456 
2457 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
GetLTRFilePath(std::string & filePath)2458 void DragDrawing::GetLTRFilePath(std::string &filePath)
2459 {
2460     FI_HILOGD("enter");
2461     switch (g_drawingInfo.currentStyle) {
2462         case DragCursorStyle::COPY: {
2463             if (g_drawingInfo.currentDragNum == DRAG_NUM_ONE) {
2464                 filePath = COPY_ONE_DRAG_PATH;
2465             } else {
2466                 filePath = COPY_DRAG_PATH;
2467             }
2468             break;
2469         }
2470         case DragCursorStyle::MOVE: {
2471             filePath = MOVE_DRAG_PATH;
2472             break;
2473         }
2474         case DragCursorStyle::FORBIDDEN: {
2475             if (g_drawingInfo.currentDragNum == DRAG_NUM_ONE) {
2476                 filePath = FORBID_ONE_DRAG_PATH;
2477             } else {
2478                 filePath = FORBID_DRAG_PATH;
2479             }
2480             break;
2481         }
2482         case DragCursorStyle::DEFAULT:
2483         default: {
2484             FI_HILOGW("Not need draw svg style, DragCursorStyle:%{public}d", g_drawingInfo.currentStyle);
2485             break;
2486         }
2487     }
2488     FI_HILOGD("leave");
2489 }
2490 
GetRTLFilePath(std::string & filePath)2491 void DragDrawing::GetRTLFilePath(std::string &filePath)
2492 {
2493     FI_HILOGD("enter");
2494     switch (g_drawingInfo.currentStyle) {
2495         case DragCursorStyle::COPY: {
2496             if (g_drawingInfo.currentDragNum == DRAG_NUM_ONE) {
2497                 filePath = COPY_ONE_DRAG_RTL_PATH;
2498             } else {
2499                 filePath = COPY_DRAG_RTL_PATH;
2500             }
2501             break;
2502         }
2503         case DragCursorStyle::MOVE: {
2504             filePath = MOVE_DRAG_RTL_PATH;
2505             break;
2506         }
2507         case DragCursorStyle::FORBIDDEN: {
2508             if (g_drawingInfo.currentDragNum == DRAG_NUM_ONE) {
2509                 filePath = FORBID_ONE_DRAG_RTL_PATH;
2510             } else {
2511                 filePath = FORBID_DRAG_RTL_PATH;
2512             }
2513             break;
2514         }
2515         case DragCursorStyle::DEFAULT:
2516         default: {
2517             FI_HILOGW("Not need draw svg style, DragCursorStyle:%{public}d", g_drawingInfo.currentStyle);
2518             break;
2519         }
2520     }
2521     FI_HILOGD("leave");
2522 }
2523 #else
GetLTRFilePath(std::string & filePath)2524 void DragDrawing::GetLTRFilePath(std::string &filePath)
2525 {
2526     FI_HILOGD("enter");
2527     switch (g_drawingInfo.currentStyle) {
2528         case DragCursorStyle::COPY: {
2529             if (g_drawingInfo.currentDragNum == DRAG_NUM_ONE) {
2530                 filePath = svgFilePath_ + COPY_ONE_DRAG_NAME;
2531             } else {
2532                 filePath = svgFilePath_ + COPY_DRAG_NAME;
2533             }
2534             break;
2535         }
2536         case DragCursorStyle::MOVE: {
2537             filePath = svgFilePath_ + MOVE_DRAG_NAME;
2538             break;
2539         }
2540         case DragCursorStyle::FORBIDDEN: {
2541             if (g_drawingInfo.currentDragNum == DRAG_NUM_ONE) {
2542                 filePath = svgFilePath_ + FORBID_ONE_DRAG_NAME;
2543             } else {
2544                 filePath = svgFilePath_ + FORBID_DRAG_NAME;
2545             }
2546             break;
2547         }
2548         case DragCursorStyle::DEFAULT:
2549         default: {
2550             FI_HILOGW("Not need draw svg style, DragCursorStyle:%{public}d", g_drawingInfo.currentStyle);
2551             break;
2552         }
2553     }
2554     FI_HILOGD("leave");
2555 }
2556 #endif // OHOS_BUILD_ENABLE_ARKUI_X
2557 
SetDecodeOptions(Media::DecodeOptions & decodeOpts)2558 void DragDrawing::SetDecodeOptions(Media::DecodeOptions &decodeOpts)
2559 {
2560     FI_HILOGD("enter");
2561     std::string strStyle = std::to_string(g_drawingInfo.currentDragNum);
2562     if (strStyle.empty()) {
2563         FI_HILOGE("strStyle size:%{public}zu invalid", strStyle.size());
2564         return;
2565     }
2566     int32_t extendSvgWidth = (static_cast<int32_t>(strStyle.size()) - 1) * EIGHT_SIZE;
2567     if ((g_drawingInfo.currentStyle == DragCursorStyle::COPY) && (g_drawingInfo.currentDragNum == DRAG_NUM_ONE)) {
2568         decodeOpts.desiredSize = {
2569             .width = DEVICE_INDEPENDENT_PIXEL * GetScaling(),
2570             .height = DEVICE_INDEPENDENT_PIXEL * GetScaling()
2571         };
2572     } else {
2573         decodeOpts.desiredSize = {
2574             .width = (DEVICE_INDEPENDENT_PIXEL + extendSvgWidth) * GetScaling(),
2575             .height = DEVICE_INDEPENDENT_PIXEL * GetScaling()
2576         };
2577     }
2578     FI_HILOGD("leave");
2579 }
2580 
ParserDragShadowInfo(const cJSON * filterInfoParser,FilterInfo & filterInfo)2581 void DragDrawing::ParserDragShadowInfo(const cJSON* filterInfoParser, FilterInfo &filterInfo)
2582 {
2583     CHKPV(filterInfoParser);
2584     cJSON *offsetX = cJSON_GetObjectItemCaseSensitive(filterInfoParser, "drag_shadow_offsetX");
2585     if (cJSON_IsNumber(offsetX)) {
2586         filterInfo.offsetX = static_cast<float>(offsetX->valuedouble);
2587     }
2588     cJSON *offsetY = cJSON_GetObjectItemCaseSensitive(filterInfoParser, "drag_shadow_offsetY");
2589     if (cJSON_IsNumber(offsetY)) {
2590         filterInfo.offsetY = static_cast<float>(offsetY->valuedouble);
2591     }
2592     cJSON *argb = cJSON_GetObjectItemCaseSensitive(filterInfoParser, "drag_shadow_argb");
2593     if (cJSON_IsNumber(argb)) {
2594         filterInfo.argb = static_cast<uint32_t>(argb->valueint);
2595     }
2596     cJSON *shadowIsFilled   = cJSON_GetObjectItemCaseSensitive(filterInfoParser, "shadow_is_filled");
2597     if (cJSON_IsBool(shadowIsFilled)) {
2598         filterInfo.shadowIsFilled = cJSON_IsTrue(shadowIsFilled);
2599     }
2600     cJSON *shadowMask   = cJSON_GetObjectItemCaseSensitive(filterInfoParser, "shadow_mask");
2601     if (cJSON_IsBool(shadowMask)) {
2602         filterInfo.shadowMask = cJSON_IsTrue(shadowMask);
2603     }
2604     cJSON *shadowColorStrategy  = cJSON_GetObjectItemCaseSensitive(filterInfoParser, "shadow_color_strategy");
2605     if (cJSON_IsNumber(shadowColorStrategy)) {
2606         filterInfo.shadowColorStrategy = shadowColorStrategy->valueint;
2607     }
2608     cJSON *isHardwareAcceleration  = cJSON_GetObjectItemCaseSensitive(
2609         filterInfoParser, "shadow_is_hardwareacceleration");
2610     if (cJSON_IsBool(isHardwareAcceleration)) {
2611         filterInfo.isHardwareAcceleration = cJSON_IsTrue(isHardwareAcceleration);
2612     }
2613     if (filterInfo.isHardwareAcceleration) {
2614         cJSON *elevation  = cJSON_GetObjectItemCaseSensitive(filterInfoParser, "shadow_elevation");
2615         if (cJSON_IsNumber(elevation)) {
2616             filterInfo.elevation = static_cast<float>(elevation->valuedouble);
2617         }
2618     } else {
2619         cJSON *shadowCorner = cJSON_GetObjectItemCaseSensitive(filterInfoParser, "shadow_corner");
2620         if (cJSON_IsNumber(shadowCorner)) {
2621             filterInfo.shadowCorner = static_cast<float>(shadowCorner->valuedouble);
2622         }
2623     }
2624 }
2625 
ParserTextDragShadowInfo(const cJSON * filterInfoParser,FilterInfo & filterInfo)2626 void DragDrawing::ParserTextDragShadowInfo(const cJSON* filterInfoParser, FilterInfo &filterInfo)
2627 {
2628     CHKPV(filterInfoParser);
2629     cJSON *path = cJSON_GetObjectItemCaseSensitive(filterInfoParser, "drag_shadow_path");
2630     if (cJSON_IsString(path)) {
2631         float dragOriginDpi = DRAG_DATA_MGR.GetDragOriginDpi();
2632         if (dragOriginDpi > EPSILON) {
2633             filterInfo.path = "";
2634         } else {
2635             filterInfo.path = path->valuestring;
2636         }
2637     }
2638 }
2639 
PrintDragShadowInfo()2640 void DragDrawing::PrintDragShadowInfo()
2641 {
2642     FilterInfo filterInfo = g_drawingInfo.filterInfo;
2643     if (!filterInfo.shadowEnable) {
2644         FI_HILOGI("Not supported shadow");
2645         return;
2646     }
2647     FI_HILOGI("dragType:%{public}s, shadowIsFilled:%{public}s, shadowMask:%{public}s, shadowColorStrategy :%{public}d, "
2648         "shadowCorner:%{public}f, offsetX:%{private}f, offsetY:%{private}f, argb:%{public}u, elevation:%{public}f, "
2649         "isHardwareAcceleration:%{public}s", filterInfo.dragType.c_str(),
2650         filterInfo.shadowIsFilled ? "true" : "false", filterInfo.shadowMask ? "true" : "false",
2651         filterInfo.shadowColorStrategy, filterInfo.shadowCorner, filterInfo.offsetX, filterInfo.offsetY,
2652         filterInfo.argb, filterInfo.elevation, filterInfo.isHardwareAcceleration ? "true" : "false");
2653     if (!filterInfo.path.empty()) {
2654         FI_HILOGI("path:%{private}s", filterInfo.path.c_str());
2655     }
2656 }
2657 
ParserFilterInfo(const std::string & filterInfoStr,FilterInfo & filterInfo)2658 bool DragDrawing::ParserFilterInfo(const std::string &filterInfoStr, FilterInfo &filterInfo)
2659 {
2660     FI_HILOGD("FilterInfo size:%{public}zu, filterInfo:%{public}s", filterInfoStr.size(), filterInfoStr.c_str());
2661     if (filterInfoStr.empty()) {
2662         FI_HILOGD("FilterInfo is empty");
2663         return false;
2664     }
2665     JsonParser filterInfoParser(filterInfoStr.c_str());
2666     if (!cJSON_IsObject(filterInfoParser.Get())) {
2667         FI_HILOGE("FilterInfo is not json object");
2668         return false;
2669     }
2670     cJSON *dipScale = cJSON_GetObjectItemCaseSensitive(filterInfoParser.Get(), "dip_scale");
2671     if (cJSON_IsNumber(dipScale)) {
2672         filterInfo.dipScale = AdjustDoubleValue(dipScale->valuedouble);
2673     }
2674     cJSON *scale = cJSON_GetObjectItemCaseSensitive(filterInfoParser.Get(), "scale");
2675     if (cJSON_IsNumber(scale)) {
2676         filterInfo.scale = AdjustDoubleValue(scale->valuedouble);
2677     }
2678     ParserCornerRadiusInfo(filterInfoParser.Get(), g_drawingInfo.filterInfo);
2679     cJSON *dragType = cJSON_GetObjectItemCaseSensitive(filterInfoParser.Get(), "drag_type");
2680     if (cJSON_IsString(dragType)) {
2681         filterInfo.dragType = dragType->valuestring;
2682     }
2683     cJSON *shadowEnable = cJSON_GetObjectItemCaseSensitive(filterInfoParser.Get(), "shadow_enable");
2684     if (cJSON_IsBool(shadowEnable)) {
2685         filterInfo.shadowEnable = cJSON_IsTrue(shadowEnable);
2686     }
2687     if (filterInfo.shadowEnable) {
2688         ParserDragShadowInfo(filterInfoParser.Get(), filterInfo);
2689         if (filterInfo.dragType == "text") {
2690             ParserTextDragShadowInfo(filterInfoParser.Get(), filterInfo);
2691         }
2692         PrintDragShadowInfo();
2693     }
2694     ParserBlurInfo(filterInfoParser.Get(), g_drawingInfo.filterInfo);
2695     cJSON *dragNodeGrayscale = cJSON_GetObjectItemCaseSensitive(filterInfoParser.Get(), "drag_node_gray_scale");
2696     if (cJSON_IsNumber(dragNodeGrayscale)) {
2697         filterInfo.dragNodeGrayscale = static_cast<float>(dragNodeGrayscale->valuedouble);
2698     }
2699     cJSON *eventId = cJSON_GetObjectItemCaseSensitive(filterInfoParser.Get(), "event_id");
2700     if (cJSON_IsNumber(eventId)) {
2701         DRAG_DATA_MGR.SetEventId(eventId->valueint);
2702     }
2703     return true;
2704 }
2705 
ParserCornerRadiusInfo(const cJSON * cornerRadiusInfoStr,FilterInfo & filterInfo)2706 void DragDrawing::ParserCornerRadiusInfo(const cJSON *cornerRadiusInfoStr, FilterInfo &filterInfo)
2707 {
2708     CHKPV(cornerRadiusInfoStr);
2709     cJSON *cornerRadius1 = cJSON_GetObjectItemCaseSensitive(cornerRadiusInfoStr, "drag_corner_radius1");
2710     if (cJSON_IsNumber(cornerRadius1)) {
2711         filterInfo.cornerRadius1 = static_cast<float>(cornerRadius1->valuedouble);
2712     }
2713     cJSON *cornerRadius2 = cJSON_GetObjectItemCaseSensitive(cornerRadiusInfoStr, "drag_corner_radius2");
2714     if (cJSON_IsNumber(cornerRadius2)) {
2715         filterInfo.cornerRadius2 = static_cast<float>(cornerRadius2->valuedouble);
2716     }
2717     cJSON *cornerRadius3 = cJSON_GetObjectItemCaseSensitive(cornerRadiusInfoStr, "drag_corner_radius3");
2718     if (cJSON_IsNumber(cornerRadius3)) {
2719         filterInfo.cornerRadius3 = static_cast<float>(cornerRadius3->valuedouble);
2720     }
2721     cJSON *cornerRadius4 = cJSON_GetObjectItemCaseSensitive(cornerRadiusInfoStr, "drag_corner_radius4");
2722     if (cJSON_IsNumber(cornerRadius4)) {
2723         filterInfo.cornerRadius4 = static_cast<float>(cornerRadius4->valuedouble);
2724     }
2725 }
2726 
ParserBlurInfo(const cJSON * BlurInfoInfoStr,FilterInfo & filterInfo)2727 void DragDrawing::ParserBlurInfo(const cJSON *BlurInfoInfoStr, FilterInfo &filterInfo)
2728 {
2729     CHKPV(BlurInfoInfoStr);
2730     cJSON *opacity = cJSON_GetObjectItemCaseSensitive(BlurInfoInfoStr, "dip_opacity");
2731     if (cJSON_IsNumber(opacity)) {
2732         if ((opacity->valuedouble) > MAX_OPACITY || (opacity->valuedouble) <= MIN_OPACITY) {
2733             FI_HILOGE("Parser opacity limits abnormal, opacity:%{public}f", opacity->valuedouble);
2734         } else {
2735             filterInfo.opacity = static_cast<float>(opacity->valuedouble);
2736         }
2737     }
2738     float tempCoef1 = 0.0f;
2739     cJSON *coef1 = cJSON_GetObjectItemCaseSensitive(BlurInfoInfoStr, "blur_coef1");
2740     if (cJSON_IsNumber(coef1)) {
2741         tempCoef1 = static_cast<float>(coef1->valuedouble);
2742     }
2743     float tempCoef2 = 0.0f;
2744     cJSON *coef2 = cJSON_GetObjectItemCaseSensitive(BlurInfoInfoStr, "blur_coef2");
2745     if (cJSON_IsNumber(coef2)) {
2746         tempCoef2 = static_cast<float>(coef2->valuedouble);
2747     }
2748     filterInfo.coef = { tempCoef1, tempCoef2 };
2749     cJSON *blurRadius = cJSON_GetObjectItemCaseSensitive(BlurInfoInfoStr, "blur_radius");
2750     if (cJSON_IsNumber(blurRadius)) {
2751         filterInfo.blurRadius = AdjustDoubleValue(blurRadius->valuedouble);
2752     }
2753     cJSON *blurStaturation = cJSON_GetObjectItemCaseSensitive(BlurInfoInfoStr, "blur_staturation");
2754     if (cJSON_IsNumber(blurStaturation)) {
2755         filterInfo.blurStaturation = static_cast<float>(blurStaturation->valuedouble);
2756     }
2757     cJSON *blurBrightness = cJSON_GetObjectItemCaseSensitive(BlurInfoInfoStr, "blur_brightness");
2758     if (cJSON_IsNumber(blurBrightness)) {
2759         filterInfo.blurBrightness = static_cast<float>(blurBrightness->valuedouble);
2760     }
2761     cJSON *blurColor = cJSON_GetObjectItemCaseSensitive(BlurInfoInfoStr, "blur_color");
2762     if (cJSON_IsNumber(blurColor)) {
2763         filterInfo.blurColor = static_cast<uint32_t>(blurColor->valueint);
2764     }
2765     cJSON *blurStyle = cJSON_GetObjectItemCaseSensitive(BlurInfoInfoStr, "blur_style");
2766     if (cJSON_IsNumber(blurStyle)) {
2767         filterInfo.blurStyle = blurStyle->valueint;
2768     }
2769     return;
2770 }
2771 
ParserExtraInfo(const std::string & extraInfoStr,ExtraInfo & extraInfo)2772 bool DragDrawing::ParserExtraInfo(const std::string &extraInfoStr, ExtraInfo &extraInfo)
2773 {
2774     FI_HILOGD("ExtraInfo size:%{public}zu, extraInfo:%{public}s",
2775         extraInfoStr.size(), extraInfoStr.c_str());
2776     if (extraInfoStr.empty()) {
2777         FI_HILOGD("ExtraInfo is empty");
2778         return false;
2779     }
2780     JsonParser extraInfoParser(extraInfoStr.c_str());
2781     if (!cJSON_IsObject(extraInfoParser.Get())) {
2782         FI_HILOGE("ExtraInfo is not json object");
2783         return false;
2784     }
2785     cJSON *componentType = cJSON_GetObjectItemCaseSensitive(extraInfoParser.Get(), "drag_data_type");
2786     if (cJSON_IsString(componentType)) {
2787         extraInfo.componentType = componentType->valuestring;
2788     }
2789     cJSON *blurStyle = cJSON_GetObjectItemCaseSensitive(extraInfoParser.Get(), "drag_blur_style");
2790     if (cJSON_IsNumber(blurStyle)) {
2791         extraInfo.blurStyle = blurStyle->valueint;
2792     }
2793     cJSON *cornerRadius = cJSON_GetObjectItemCaseSensitive(extraInfoParser.Get(), "drag_corner_radius");
2794     if (cJSON_IsNumber(cornerRadius)) {
2795         extraInfo.cornerRadius = static_cast<float>(cornerRadius->valuedouble);
2796     }
2797     cJSON *allowDistributed = cJSON_GetObjectItemCaseSensitive(extraInfoParser.Get(), "drag_allow_distributed");
2798     if (cJSON_IsBool(allowDistributed)) {
2799         extraInfo.allowDistributed = cJSON_IsTrue(allowDistributed) ? true : false;
2800     }
2801     float tempCoef1 = 0.0f;
2802     cJSON *coef1 = cJSON_GetObjectItemCaseSensitive(extraInfoParser.Get(), "blur_coef1");
2803     if (cJSON_IsNumber(coef1)) {
2804         tempCoef1 = static_cast<float>(coef1->valuedouble);
2805     }
2806     float tempCoef2 = 0.0f;
2807     cJSON *coef2 = cJSON_GetObjectItemCaseSensitive(extraInfoParser.Get(), "blur_coef2");
2808     if (cJSON_IsNumber(coef2)) {
2809         tempCoef2 = static_cast<float>(coef2->valuedouble);
2810     }
2811     extraInfo.coef = { tempCoef1, tempCoef2 };
2812     return true;
2813 }
2814 
GetAllowDragState()2815 bool DragDrawing::GetAllowDragState()
2816 {
2817     return g_drawingInfo.extraInfo.allowDistributed;
2818 }
2819 
SetScreenId(uint64_t screenId)2820 void DragDrawing::SetScreenId(uint64_t screenId)
2821 {
2822     FI_HILOGD("enter");
2823     screenId_ = screenId;
2824 }
2825 
RotateDragWindow(Rosen::Rotation rotation,const std::shared_ptr<Rosen::RSTransaction> & rsTransaction,bool isAnimated)2826 int32_t DragDrawing::RotateDragWindow(Rosen::Rotation rotation,
2827     const std::shared_ptr<Rosen::RSTransaction>& rsTransaction, bool isAnimated)
2828 {
2829     if (needRotatePixelMapXY_) {
2830         auto currentPixelMap = DragDrawing::AccessGlobalPixelMapLocked();
2831         CHKPR(currentPixelMap, RET_ERR);
2832         g_drawingInfo.pixelMapX = -(HALF_RATIO * currentPixelMap->GetWidth());
2833         g_drawingInfo.pixelMapY = -(EIGHT_SIZE * GetScaling());
2834     }
2835     float rotateAngle = (rotation == Rosen::Rotation::ROTATION_0) ? ROTATION_0 :
2836         ROTATION_360 - (ROTATION_90 * static_cast<int32_t>(rotation));
2837     FI_HILOGI("rotateAngle:%{public}f, isAnimated:%{public}d", rotateAngle, isAnimated);
2838     return DoRotateDragWindow(rotateAngle, rsTransaction, isAnimated);
2839 }
2840 
RotateCanvasNode(float pivotX,float pivotY,float rotation)2841 void DragDrawing::RotateCanvasNode(float pivotX, float pivotY, float rotation)
2842 {
2843     FI_HILOGD("enter");
2844     CHKPV(g_drawingInfo.parentNode);
2845     g_drawingInfo.parentNode->SetPivot(pivotX, pivotY);
2846     g_drawingInfo.parentNode->SetRotation(rotation);
2847     if (!g_drawingInfo.multiSelectedNodes.empty()) {
2848         size_t multiSelectedNodesSize = g_drawingInfo.multiSelectedNodes.size();
2849         for (size_t i = 0; i < multiSelectedNodesSize; ++i) {
2850             std::shared_ptr<Rosen::RSCanvasNode> multiSelectedNode = g_drawingInfo.multiSelectedNodes[i];
2851             CHKPV(multiSelectedNode);
2852             float degrees = DEFAULT_ANGLE;
2853             if (i == FIRST_PIXELMAP_INDEX) {
2854                 degrees = rotation + POSITIVE_ANGLE;
2855             } else if (i == SECOND_PIXELMAP_INDEX) {
2856                 degrees = rotation + NEGATIVE_ANGLE;
2857             }
2858             multiSelectedNode->SetPivot(HALF_PIVOT, HALF_PIVOT);
2859             multiSelectedNode->SetRotation(degrees);
2860         }
2861     }
2862 #ifndef OHOS_BUILD_PC_PRODUCT
2863     if (g_drawingInfo.sourceType == MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
2864         if (!CheckNodesValid()) {
2865             FI_HILOGE("Check nodes valid failed");
2866             return;
2867         }
2868         std::shared_ptr<Rosen::RSCanvasNode> mouseIconNode = g_drawingInfo.nodes[MOUSE_ICON_INDEX];
2869         CHKPV(mouseIconNode);
2870         mouseIconNode->SetPivot(DEFAULT_PIVOT, DEFAULT_PIVOT);
2871         mouseIconNode->SetRotation(rotation);
2872     }
2873 #endif // OHOS_BUILD_PC_PRODUCT
2874     float positionX = g_drawingInfo.currentPositionX;
2875     float positionY = g_drawingInfo.currentPositionY;
2876     AdjustRotateDisplayXY(positionX, positionY);
2877     DrawRotateDisplayXY(positionX, positionY);
2878     FI_HILOGD("leave");
2879 }
2880 
SetRotation(Rosen::DisplayId displayId,Rosen::Rotation rotation)2881 void DragDrawing::SetRotation(Rosen::DisplayId displayId, Rosen::Rotation rotation)
2882 {
2883     FI_HILOGI("displayId:%{public}d, rotation:%{public}d",
2884         static_cast<int32_t>(displayId), static_cast<int32_t>(rotation));
2885     auto iter = rotationMap_.find(displayId);
2886     if (iter == rotationMap_.end()) {
2887         rotationMap_.emplace(displayId, rotation);
2888         FI_HILOGW("Create a new element");
2889         return;
2890     }
2891     if (iter->second != rotation) {
2892         rotationMap_[displayId] = rotation;
2893     }
2894 }
2895 
GetRotation(Rosen::DisplayId displayId)2896 Rosen::Rotation DragDrawing::GetRotation(Rosen::DisplayId displayId)
2897 {
2898     if (!rotationMap_.empty()) {
2899         auto iter = rotationMap_.find(displayId);
2900         if (iter != rotationMap_.end()) {
2901             FI_HILOGI("displayId:%{public}d, rotation:%{public}d",
2902                 static_cast<int32_t>(iter->first), static_cast<int32_t>(iter->second));
2903             return iter->second;
2904         }
2905     }
2906     return Rosen::Rotation::ROTATION_0;
2907 }
2908 
RemoveDisplayIdFromMap(Rosen::DisplayId displayId)2909 void DragDrawing::RemoveDisplayIdFromMap(Rosen::DisplayId displayId)
2910 {
2911     FI_HILOGI("displayId:%{public}d", static_cast<int32_t>(displayId));
2912     if (rotationMap_.empty()) {
2913         FI_HILOGE("rotation map is empty");
2914         return;
2915     }
2916     auto iter = rotationMap_.find(displayId);
2917     if (iter == rotationMap_.end()) {
2918         FI_HILOGW("displayId does not exist in rotation map");
2919         return;
2920     }
2921     rotationMap_.erase(displayId);
2922 }
2923 
ProcessFilter()2924 void DragDrawing::ProcessFilter()
2925 {
2926     FI_HILOGD("enter");
2927     if (g_drawingInfo.nodes.size() <= BACKGROUND_FILTER_INDEX) {
2928         FI_HILOGE("The index is out of bounds, node size is %{public}zu", g_drawingInfo.nodes.size());
2929         return;
2930     }
2931     std::shared_ptr<Rosen::RSCanvasNode> filterNode = g_drawingInfo.nodes[BACKGROUND_FILTER_INDEX];
2932     CHKPV(filterNode);
2933     auto currentPixelMap = DragDrawing::AccessGlobalPixelMapLocked();
2934     CHKPV(currentPixelMap);
2935     FilterInfo filterInfo = g_drawingInfo.filterInfo;
2936     ExtraInfo extraInfo = g_drawingInfo.extraInfo;
2937     if (filterInfo.blurStyle != -1) {
2938         SetCustomDragBlur(filterInfo, filterNode);
2939     } else if (extraInfo.componentType == BIG_FOLDER_LABEL) {
2940         SetComponentDragBlur(filterInfo, extraInfo, filterNode);
2941     }
2942     FI_HILOGD("Add filter successfully");
2943     FI_HILOGD("leave");
2944 }
2945 
SetCustomDragBlur(const FilterInfo & filterInfo,std::shared_ptr<Rosen::RSCanvasNode> filterNode)2946 void DragDrawing::SetCustomDragBlur(const FilterInfo &filterInfo, std::shared_ptr<Rosen::RSCanvasNode> filterNode)
2947 {
2948     CHKPV(filterNode);
2949     auto currentPixelMap = DragDrawing::AccessGlobalPixelMapLocked();
2950     CHKPV(currentPixelMap);
2951     Rosen::BLUR_COLOR_MODE mode = (Rosen::BLUR_COLOR_MODE)filterInfo.blurStyle;
2952     std::shared_ptr<Rosen::RSFilter> backFilter = Rosen::RSFilter::CreateMaterialFilter(
2953         RadiusVp2Sigma(filterInfo.blurRadius, filterInfo.dipScale),
2954         filterInfo.blurStaturation, filterInfo.blurBrightness, filterInfo.blurColor, mode);
2955     if (backFilter == nullptr) {
2956         FI_HILOGE("Create backgroundFilter failed");
2957         return;
2958     }
2959     filterNode->SetBackgroundFilter(backFilter);
2960     filterNode->SetGreyCoef(filterInfo.coef);
2961     filterNode->SetAlpha(filterInfo.opacity);
2962     int32_t adjustSize = TWELVE_SIZE * GetScaling();
2963     filterNode->SetBounds(DEFAULT_POSITION_X, adjustSize, currentPixelMap->GetWidth(),
2964         currentPixelMap->GetHeight());
2965     filterNode->SetFrame(DEFAULT_POSITION_X, adjustSize, currentPixelMap->GetWidth(),
2966         currentPixelMap->GetHeight());
2967     if ((filterInfo.blurRadius < 0) || (filterInfo.dipScale < 0) ||
2968         (fabs(filterInfo.dipScale) < EPSILON) || ((std::numeric_limits<float>::max()
2969         / filterInfo.dipScale) < filterInfo.blurRadius)) {
2970         FI_HILOGE("Invalid parameters, cornerRadius:%{public}f, dipScale:%{public}f",
2971             filterInfo.blurRadius, filterInfo.dipScale);
2972         return;
2973     }
2974     Rosen::Vector4f cornerRadiusVector = { filterInfo.cornerRadius1, filterInfo.cornerRadius2,
2975         filterInfo.cornerRadius3, filterInfo.cornerRadius4 };
2976     filterNode->SetCornerRadius(cornerRadiusVector * filterInfo.dipScale);
2977     FI_HILOGD("Set custom drag blur successfully");
2978 }
2979 
SetComponentDragBlur(const FilterInfo & filterInfo,const ExtraInfo & extraInfo,std::shared_ptr<Rosen::RSCanvasNode> filterNode)2980 void DragDrawing::SetComponentDragBlur(const FilterInfo &filterInfo, const ExtraInfo &extraInfo,
2981     std::shared_ptr<Rosen::RSCanvasNode> filterNode)
2982 {
2983     CHKPV(filterNode);
2984     auto currentPixelMap = DragDrawing::AccessGlobalPixelMapLocked();
2985     CHKPV(currentPixelMap);
2986     std::shared_ptr<Rosen::RSFilter> backFilter = Rosen::RSFilter::CreateMaterialFilter(
2987         RadiusVp2Sigma(RADIUS_VP, filterInfo.dipScale),
2988         DEFAULT_SATURATION, DEFAULT_BRIGHTNESS, DEFAULT_COLOR_VALUE);
2989     if (backFilter == nullptr) {
2990         FI_HILOGE("Create backgroundFilter failed");
2991         return;
2992     }
2993     filterNode->SetBackgroundFilter(backFilter);
2994     filterNode->SetGreyCoef(extraInfo.coef);
2995     filterNode->SetAlpha(filterInfo.opacity);
2996     int32_t adjustSize = TWELVE_SIZE * GetScaling();
2997     filterNode->SetBounds(DEFAULT_POSITION_X, adjustSize, currentPixelMap->GetWidth(),
2998         currentPixelMap->GetHeight());
2999     filterNode->SetFrame(DEFAULT_POSITION_X, adjustSize, currentPixelMap->GetWidth(),
3000         currentPixelMap->GetHeight());
3001     if ((extraInfo.cornerRadius < 0) || (filterInfo.dipScale < 0) ||
3002         (fabs(filterInfo.dipScale) < EPSILON) || ((std::numeric_limits<float>::max()
3003         / filterInfo.dipScale) < extraInfo.cornerRadius)) {
3004         FI_HILOGE("Invalid parameters, cornerRadius:%{public}f, dipScale:%{public}f",
3005             extraInfo.cornerRadius, filterInfo.dipScale);
3006         return;
3007     }
3008     filterNode->SetCornerRadius(extraInfo.cornerRadius * filterInfo.dipScale);
3009     FI_HILOGD("Set component drag blur successfully");
3010     return;
3011 }
3012 
SetNodesLocation()3013 int32_t DragDrawing::SetNodesLocation()
3014 {
3015     FI_HILOGD("enter");
3016     Rosen::RSAnimationTimingProtocol protocol;
3017     Rosen::RSNode::Animate(protocol, SPRING, [&]() {
3018         float displayX = g_drawingInfo.currentPositionX;
3019         float displayY = g_drawingInfo.currentPositionY;
3020         AdjustRotateDisplayXY(displayX, displayY);
3021         int32_t positionX = displayX + g_drawingInfo.pixelMapX;
3022         int32_t positionY = displayY + g_drawingInfo.pixelMapY - TWELVE_SIZE * GetScaling();
3023         int32_t adjustSize = TWELVE_SIZE * GetScaling();
3024         CHKPV(g_drawingInfo.parentNode);
3025         auto currentPixelMap = DragDrawing::AccessGlobalPixelMapLocked();
3026         CHKPV(currentPixelMap);
3027         g_drawingInfo.parentNode->SetBounds(positionX, positionY, currentPixelMap->GetWidth(),
3028             currentPixelMap->GetHeight() + adjustSize);
3029         g_drawingInfo.parentNode->SetFrame(positionX, positionY, currentPixelMap->GetWidth(),
3030             currentPixelMap->GetHeight() + adjustSize);
3031         if (!g_drawingInfo.multiSelectedNodes.empty() && !g_drawingInfo.multiSelectedPixelMaps.empty()) {
3032             size_t multiSelectedNodesSize = g_drawingInfo.multiSelectedNodes.size();
3033             size_t multiSelectedPixelMapsSize = g_drawingInfo.multiSelectedPixelMaps.size();
3034             for (size_t i = 0; (i < multiSelectedNodesSize) && (i < multiSelectedPixelMapsSize); ++i) {
3035                 std::shared_ptr<Rosen::RSCanvasNode> multiSelectedNode = g_drawingInfo.multiSelectedNodes[i];
3036                 std::shared_ptr<Media::PixelMap> multiSelectedPixelMap = g_drawingInfo.multiSelectedPixelMaps[i];
3037                 auto pixelMap  = currentPixelMap;
3038                 CHKPV(pixelMap);
3039                 CHKPV(multiSelectedNode);
3040                 CHKPV(multiSelectedPixelMap);
3041                 float multiSelectedPositionX = positionX + (static_cast<float>(pixelMap->GetWidth()) / TWICE_SIZE) -
3042                     (static_cast<float>(multiSelectedPixelMap->GetWidth()) / TWICE_SIZE);
3043                 float multiSelectedPositionY = positionY + (static_cast<float>(pixelMap->GetHeight()) / TWICE_SIZE) -
3044                     (static_cast<float>(multiSelectedPixelMap->GetHeight()) / TWICE_SIZE - adjustSize);
3045                     multiSelectedNode->SetBounds(multiSelectedPositionX, multiSelectedPositionY,
3046                         multiSelectedPixelMap->GetWidth(), multiSelectedPixelMap->GetHeight());
3047                     multiSelectedNode->SetFrame(multiSelectedPositionX, multiSelectedPositionY,
3048                         multiSelectedPixelMap->GetWidth(), multiSelectedPixelMap->GetHeight());
3049             }
3050         }
3051     }, [this]() {
3052         FI_HILOGD("SetNodesLocation end");
3053     });
3054 #ifdef IOS_PLATFORM
3055     g_drawingInfo.startNum = actionTime_; // IOS animation starts time
3056 #else
3057     g_drawingInfo.startNum = START_TIME;
3058 #endif // IOS_PLATFORM
3059     g_drawingInfo.needDestroyDragWindow = false;
3060     StartVsync();
3061     FI_HILOGD("leave");
3062     return RET_OK;
3063 }
3064 
3065 
EnterTextEditorArea(bool enable)3066 int32_t DragDrawing::EnterTextEditorArea(bool enable)
3067 {
3068     FI_HILOGD("enter");
3069     if (enable) {
3070         DRAG_DATA_MGR.SetInitialPixelMapLocation({ g_drawingInfo.pixelMapX, g_drawingInfo.pixelMapY });
3071         needRotatePixelMapXY_ = true;
3072         RotatePixelMapXY();
3073     } else {
3074         needRotatePixelMapXY_ = false;
3075         auto initialPixelMapLocation = DRAG_DATA_MGR.GetInitialPixelMapLocation();
3076         g_drawingInfo.pixelMapX = initialPixelMapLocation.first;
3077         g_drawingInfo.pixelMapY = initialPixelMapLocation.second;
3078     }
3079     DRAG_DATA_MGR.SetPixelMapLocation({ g_drawingInfo.pixelMapX, g_drawingInfo.pixelMapY });
3080     if (RunAnimation([this] {
3081         return this->SetNodesLocation();
3082     }) != RET_OK) {
3083         FI_HILOGE("RunAnimation to SetNodesLocation failed");
3084         return RET_ERR;
3085     }
3086     DRAG_DATA_MGR.SetTextEditorAreaFlag(enable);
3087     FI_HILOGI("EnterTextEditorArea %{public}s successfully", (enable ? "true" : "false"));
3088     return RET_OK;
3089 }
3090 
RadiusVp2Sigma(float radiusVp,float dipScale)3091 float DragDrawing::RadiusVp2Sigma(float radiusVp, float dipScale)
3092 {
3093     float radiusPx = radiusVp * dipScale;
3094     return radiusPx > 0.0f ? BLUR_SIGMA_SCALE * radiusPx + 0.5f : 0.0f;
3095 }
3096 
UpdatePreviewStyle(const PreviewStyle & previewStyle)3097 int32_t DragDrawing::UpdatePreviewStyle(const PreviewStyle &previewStyle)
3098 {
3099     FI_HILOGD("enter");
3100     if (g_drawingInfo.nodes.size() <= PIXEL_MAP_INDEX) {
3101         FI_HILOGE("The index is out of bounds, node size is %{public}zu", g_drawingInfo.nodes.size());
3102         return RET_ERR;
3103     } else if (ModifyPreviewStyle(g_drawingInfo.nodes[PIXEL_MAP_INDEX], previewStyle) != RET_OK) {
3104         FI_HILOGE("ModifyPreviewStyle failed");
3105         return RET_ERR;
3106     }
3107     if (ModifyMultiPreviewStyle(std::vector<PreviewStyle>(g_drawingInfo.multiSelectedNodes.size(), previewStyle)) !=
3108         RET_OK) {
3109         FI_HILOGE("ModifyPreviewStyle failed");
3110         return RET_ERR;
3111     }
3112     Rosen::RSTransaction::FlushImplicitTransaction();
3113     FI_HILOGD("leave");
3114     return RET_OK;
3115 }
3116 
UpdatePreviewStyleWithAnimation(const PreviewStyle & previewStyle,const PreviewAnimation & animation)3117 int32_t DragDrawing::UpdatePreviewStyleWithAnimation(const PreviewStyle &previewStyle,
3118     const PreviewAnimation &animation)
3119 {
3120     FI_HILOGD("enter");
3121     std::shared_ptr<Rosen::RSCanvasNode> pixelMapNode = g_drawingInfo.nodes[PIXEL_MAP_INDEX];
3122     CHKPR(pixelMapNode, RET_ERR);
3123     PreviewStyle originStyle;
3124     originStyle.types = previewStyle.types;
3125     if (auto color = pixelMapNode->GetShowingProperties().GetForegroundColor(); color.has_value()) {
3126         originStyle.foregroundColor = color->AsArgbInt();
3127         originStyle.radius = previewStyle.radius;
3128     }
3129     size_t multiSelectedNodesSize = g_drawingInfo.multiSelectedNodes.size();
3130     std::vector<PreviewStyle> multiOriginStyles;
3131     for (size_t i = 0; i < multiSelectedNodesSize; ++i) {
3132         if (auto color = g_drawingInfo.multiSelectedNodes[i]->GetShowingProperties().GetForegroundColor();
3133             color.has_value()) {
3134             PreviewStyle currentStyle;
3135             currentStyle.types = { PreviewType::FOREGROUND_COLOR, PreviewType::RADIUS };
3136             currentStyle.foregroundColor = color->AsArgbInt();
3137             currentStyle.radius = previewStyle.radius;
3138             multiOriginStyles.push_back(currentStyle);
3139         }
3140     }
3141     if (ModifyPreviewStyle(pixelMapNode, originStyle) != RET_OK) {
3142         FI_HILOGE("ModifyPreviewStyle failed");
3143         return RET_ERR;
3144     }
3145     if (ModifyMultiPreviewStyle(multiOriginStyles) != RET_OK) {
3146         FI_HILOGE("ModifyMultiPreviewStyle failed");
3147         return RET_ERR;
3148     }
3149     Rosen::RSAnimationTimingProtocol protocol;
3150     protocol.SetDuration(animation.duration);
3151     auto curve = AnimationCurve::CreateCurve(animation.curveName, animation.curve);
3152     Rosen::RSNode::Animate(protocol, curve, [&]() {
3153         if (ModifyPreviewStyle(pixelMapNode, previewStyle) != RET_OK) {
3154             FI_HILOGE("ModifyPreviewStyle failed");
3155         }
3156         if (ModifyMultiPreviewStyle(std::vector<PreviewStyle>(multiSelectedNodesSize, previewStyle)) != RET_OK) {
3157             FI_HILOGE("ModifyMultiPreviewStyle failed");
3158         }
3159     },  []() { FI_HILOGD("UpdatePreviewStyleWithAnimation end"); });
3160     FI_HILOGD("leave");
3161     return RET_OK;
3162 }
3163 
UpdateMousePosition(float mousePositionX,float mousePositionY)3164 void DragDrawing::UpdateMousePosition(float mousePositionX, float mousePositionY)
3165 {
3166     if (!CheckNodesValid()) {
3167         FI_HILOGE("Check nodes valid failed");
3168         return;
3169     }
3170     if (g_drawingInfo.nodes.size() <= MOUSE_ICON_INDEX) {
3171         FI_HILOGE("The index out of bounds, node size:%{public}zu", g_drawingInfo.nodes.size());
3172         return;
3173     }
3174     std::shared_ptr<Rosen::RSCanvasNode> mouseIconNode = g_drawingInfo.nodes[MOUSE_ICON_INDEX];
3175     CHKPV(mouseIconNode);
3176 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
3177     if (pointerStyle_.id == MOUSE_DRAG_CURSOR_CIRCLE_STYLE || pointerStyle_.options == MAGIC_STYLE_OPT) {
3178         float positionX = mousePositionX - (static_cast<float>(g_drawingInfo.mouseWidth) / CURSOR_CIRCLE_MIDDLE);
3179         float positionY = mousePositionY - (static_cast<float>(g_drawingInfo.mouseHeight) / CURSOR_CIRCLE_MIDDLE);
3180         mouseIconNode->SetBounds(positionX, positionY, g_drawingInfo.mouseWidth, g_drawingInfo.mouseHeight);
3181         mouseIconNode->SetFrame(positionX, positionY, g_drawingInfo.mouseWidth, g_drawingInfo.mouseHeight);
3182     } else {
3183         mouseIconNode->SetBounds(mousePositionX, mousePositionY,
3184             g_drawingInfo.mouseWidth, g_drawingInfo.mouseHeight);
3185         mouseIconNode->SetFrame(mousePositionX, mousePositionY,
3186             g_drawingInfo.mouseWidth, g_drawingInfo.mouseHeight);
3187     }
3188 #endif // OHOS_BUILD_ENABLE_ARKUI_X
3189 }
3190 
RotateDragWindowAsync(Rosen::Rotation rotation)3191 int32_t DragDrawing::RotateDragWindowAsync(Rosen::Rotation rotation)
3192 {
3193     isRunningRotateAnimation_ = true;
3194     int32_t repeatTime = 1;
3195 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
3196     CHKPR(context_, RET_ERR);
3197     timerId_ = context_->GetTimerManager().AddTimer(ASYNC_ROTATE_TIME, repeatTime, [this, rotation]() {
3198         RotateDragWindow(rotation, nullptr, true);
3199         isRunningRotateAnimation_ = false;
3200     });
3201 #endif // OHOS_BUILD_ENABLE_ARKUI_X
3202     if (timerId_ < 0) {
3203         FI_HILOGE("Add timer failed, timerId_:%{public}d", timerId_);
3204         isRunningRotateAnimation_ = false;
3205         return RET_ERR;
3206     }
3207     return RET_OK;
3208 }
3209 
RotateDragWindowSync(const std::shared_ptr<Rosen::RSTransaction> & rsTransaction)3210 int32_t DragDrawing::RotateDragWindowSync(const std::shared_ptr<Rosen::RSTransaction>& rsTransaction)
3211 {
3212     FI_HILOGD("enter");
3213     isRunningRotateAnimation_ = true;
3214     Rosen::Rotation rotation = GetRotation(g_drawingInfo.displayId);
3215     RotateDragWindow(rotation, rsTransaction, true);
3216     isRunningRotateAnimation_ = false;
3217 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
3218     if ((context_ != nullptr) && (timerId_ >= 0)) {
3219         context_->GetTimerManager().RemoveTimer(timerId_);
3220         timerId_ = -1;
3221     }
3222 #endif // OHOS_BUILD_ENABLE_ARKUI_X
3223     return RET_OK;
3224 }
3225 
DoDrawMouse(int32_t mousePositionX,int32_t mousePositionY)3226 void DragDrawing::DoDrawMouse(int32_t mousePositionX, int32_t mousePositionY)
3227 {
3228     if (!CheckNodesValid()) {
3229         FI_HILOGE("Check nodes valid failed");
3230         return;
3231     }
3232     if (g_drawingInfo.nodes.size() <= MOUSE_ICON_INDEX) {
3233         FI_HILOGE("The index is out of bounds, node size is %{public}zu", g_drawingInfo.nodes.size());
3234         return;
3235     }
3236 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
3237     std::shared_ptr<Rosen::RSCanvasNode> mouseIconNode = g_drawingInfo.nodes[MOUSE_ICON_INDEX];
3238     CHKPV(mouseIconNode);
3239     if (pointerStyle_.id == MOUSE_DRAG_CURSOR_CIRCLE_STYLE || pointerStyle_.options == MAGIC_STYLE_OPT) {
3240         int32_t positionX = mousePositionX - (g_drawingInfo.mouseWidth / CURSOR_CIRCLE_MIDDLE);
3241         int32_t positionY = mousePositionY - (g_drawingInfo.mouseHeight / CURSOR_CIRCLE_MIDDLE);
3242         mouseIconNode->SetBounds(positionX, positionY, g_drawingInfo.mouseWidth, g_drawingInfo.mouseHeight);
3243         mouseIconNode->SetFrame(positionX, positionY, g_drawingInfo.mouseWidth, g_drawingInfo.mouseHeight);
3244     } else {
3245         mouseIconNode->SetBounds(mousePositionX, mousePositionY,
3246             g_drawingInfo.mouseWidth, g_drawingInfo.mouseHeight);
3247         mouseIconNode->SetFrame(mousePositionX, mousePositionY,
3248             g_drawingInfo.mouseWidth, g_drawingInfo.mouseHeight);
3249     }
3250 #endif // OHOS_BUILD_ENABLE_ARKUI_X
3251 }
3252 
UpdateDefaultDragStyle(DragCursorStyle style)3253 int32_t DragDrawing::UpdateDefaultDragStyle(DragCursorStyle style)
3254 {
3255     if (!CheckNodesValid()) {
3256         FI_HILOGE("Check nodes valid failed");
3257         return RET_ERR;
3258     }
3259     if (g_drawingInfo.nodes.size() <= DRAG_STYLE_INDEX) {
3260         FI_HILOGE("The index is out of bounds, node size is %{public}zu", g_drawingInfo.nodes.size());
3261         return RET_ERR;
3262     }
3263     if (!g_drawingInfo.isCurrentDefaultStyle) {
3264         std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode = g_drawingInfo.nodes[DRAG_STYLE_INDEX];
3265         CHKPR(dragStyleNode, RET_ERR);
3266         CHKPR(g_drawingInfo.parentNode, RET_ERR);
3267         g_drawingInfo.parentNode->RemoveChild(dragStyleNode);
3268         CHKPR(rsUiDirector_, RET_ERR);
3269         rsUiDirector_->SendMessages();
3270     }
3271     g_drawingInfo.currentStyle = style;
3272     bool isPreviousDefaultStyle = g_drawingInfo.isCurrentDefaultStyle;
3273     g_drawingInfo.isPreviousDefaultStyle = isPreviousDefaultStyle;
3274     g_drawingInfo.isCurrentDefaultStyle = true;
3275     return RET_OK;
3276 }
3277 
UpdateValidDragStyle(DragCursorStyle style)3278 int32_t DragDrawing::UpdateValidDragStyle(DragCursorStyle style)
3279 {
3280     g_drawingInfo.currentStyle = style;
3281     if (g_drawingInfo.isCurrentDefaultStyle) {
3282         if (!CheckNodesValid()) {
3283             FI_HILOGE("Check nodes valid failed");
3284             return RET_ERR;
3285         }
3286         if (g_drawingInfo.nodes.size() <= DRAG_STYLE_INDEX) {
3287             FI_HILOGE("The index is out of bounds, node size is %{public}zu", g_drawingInfo.nodes.size());
3288             return RET_ERR;
3289         }
3290         std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode = g_drawingInfo.nodes[DRAG_STYLE_INDEX];
3291         CHKPR(dragStyleNode, RET_ERR);
3292         CHKPR(g_drawingInfo.parentNode, RET_ERR);
3293         g_drawingInfo.parentNode->AddChild(dragStyleNode);
3294     }
3295     std::string filePath;
3296     GetFilePath(filePath);
3297     if (!IsValidSvgFile(filePath)) {
3298         FI_HILOGE("Svg file is invalid");
3299         return RET_ERR;
3300     }
3301     std::shared_ptr<Media::PixelMap> pixelMap = DecodeSvgToPixelMap(filePath);
3302     CHKPR(pixelMap, RET_ERR);
3303     bool isPreviousDefaultStyle = g_drawingInfo.isCurrentDefaultStyle;
3304     g_drawingInfo.isPreviousDefaultStyle = isPreviousDefaultStyle;
3305     g_drawingInfo.isCurrentDefaultStyle = false;
3306     g_drawingInfo.stylePixelMap = pixelMap;
3307     if (!CheckNodesValid()) {
3308         FI_HILOGE("Check nodes valid failed");
3309         return RET_ERR;
3310     }
3311     std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode = g_drawingInfo.nodes[DRAG_STYLE_INDEX];
3312     CHKPR(dragStyleNode, RET_ERR);
3313     OnDragStyle(dragStyleNode, pixelMap);
3314     CHKPR(rsUiDirector_, RET_ERR);
3315     rsUiDirector_->SendMessages();
3316 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
3317 #ifdef MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
3318     DragDFX::WriteUpdateDragStyle(style, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR);
3319 #endif // MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
3320 #endif // OHOS_BUILD_ENABLE_ARKUI_X
3321     return RET_OK;
3322 }
3323 
ModifyPreviewStyle(std::shared_ptr<Rosen::RSCanvasNode> node,const PreviewStyle & previewStyle)3324 int32_t DragDrawing::ModifyPreviewStyle(std::shared_ptr<Rosen::RSCanvasNode> node, const PreviewStyle &previewStyle)
3325 {
3326     FI_HILOGD("enter");
3327     CHKPR(node, RET_ERR);
3328     if (float radius = 0.0F; ParserRadius(radius)) {
3329         node->SetCornerRadius(radius);
3330         FI_HILOGD("SetCornerRadius by radius:%{public}f", radius);
3331     }
3332     for (const auto &type : previewStyle.types) {
3333         switch (type) {
3334             case PreviewType::FOREGROUND_COLOR: {
3335                 node->SetForegroundColor(previewStyle.foregroundColor);
3336                 break;
3337             }
3338             case PreviewType::OPACITY: {
3339                 node->SetAlpha(previewStyle.opacity / static_cast<float>(HEX_FF));
3340                 break;
3341             }
3342             case PreviewType::RADIUS: {
3343                 node->SetCornerRadius(previewStyle.radius);
3344                 break;
3345             }
3346             case PreviewType::SCALE: {
3347                 node->SetScale(previewStyle.scale);
3348                 break;
3349             }
3350             default: {
3351                 FI_HILOGE("Unsupported type");
3352                 break;
3353             }
3354         }
3355     }
3356     FI_HILOGD("leave");
3357     return RET_OK;
3358 }
3359 
ModifyMultiPreviewStyle(const std::vector<PreviewStyle> & previewStyles)3360 int32_t DragDrawing::ModifyMultiPreviewStyle(const std::vector<PreviewStyle> &previewStyles)
3361 {
3362     size_t multiSelectedNodesSize = g_drawingInfo.multiSelectedNodes.size();
3363     if (previewStyles.size() != multiSelectedNodesSize) {
3364         FI_HILOGE("Size of previewStyles:%{public}zu does not match multiSelectedNodesSize:%{public}zu",
3365             previewStyles.size(), multiSelectedNodesSize);
3366         return RET_ERR;
3367     }
3368     for (size_t i = 0; i < multiSelectedNodesSize; ++i) {
3369         if (ModifyPreviewStyle(g_drawingInfo.multiSelectedNodes[i], previewStyles[i]) != RET_OK) {
3370             FI_HILOGW("ModifyPreviewStyle No.%{public}zu failed", i);
3371         }
3372     }
3373     return RET_OK;
3374 }
3375 
MultiSelectedAnimation(int32_t positionX,int32_t positionY,int32_t adjustSize,bool isMultiSelectedAnimation)3376 void DragDrawing::MultiSelectedAnimation(int32_t positionX, int32_t positionY, int32_t adjustSize,
3377     bool isMultiSelectedAnimation)
3378 {
3379     size_t multiSelectedNodesSize = g_drawingInfo.multiSelectedNodes.size();
3380     size_t multiSelectedPixelMapsSize = g_drawingInfo.multiSelectedPixelMaps.size();
3381     for (size_t i = 0; (i < multiSelectedNodesSize) && (i < multiSelectedPixelMapsSize); ++i) {
3382         std::shared_ptr<Rosen::RSCanvasNode> multiSelectedNode = g_drawingInfo.multiSelectedNodes[i];
3383         std::shared_ptr<Media::PixelMap> multiSelectedPixelMap = g_drawingInfo.multiSelectedPixelMaps[i];
3384         auto currentPixelMap = DragDrawing::AccessGlobalPixelMapLocked();
3385         CHKPV(currentPixelMap);
3386         CHKPV(multiSelectedNode);
3387         CHKPV(multiSelectedPixelMap);
3388         int32_t multiSelectedPositionX = positionX + (currentPixelMap->GetWidth() / TWICE_SIZE) -
3389             (multiSelectedPixelMap->GetWidth() / TWICE_SIZE);
3390         int32_t multiSelectedPositionY = positionY + (currentPixelMap->GetHeight() / TWICE_SIZE) -
3391             ((multiSelectedPixelMap->GetHeight() / TWICE_SIZE) - adjustSize);
3392         if (isMultiSelectedAnimation) {
3393             Rosen::RSAnimationTimingProtocol protocol;
3394             if (i == FIRST_PIXELMAP_INDEX) {
3395                 protocol.SetDuration(SHORT_DURATION);
3396             } else {
3397                 protocol.SetDuration(LONG_DURATION);
3398             }
3399             Rosen::RSNode::Animate(protocol, Rosen::RSAnimationTimingCurve::EASE_IN_OUT, [&]() {
3400                 multiSelectedNode->SetBounds(multiSelectedPositionX, multiSelectedPositionY,
3401                     multiSelectedPixelMap->GetWidth(), multiSelectedPixelMap->GetHeight());
3402                 multiSelectedNode->SetFrame(multiSelectedPositionX, multiSelectedPositionY,
3403                     multiSelectedPixelMap->GetWidth(), multiSelectedPixelMap->GetHeight());
3404             },  []() { FI_HILOGD("MultiSelectedAnimation end"); });
3405         } else {
3406             multiSelectedNode->SetBounds(multiSelectedPositionX, multiSelectedPositionY,
3407                 multiSelectedPixelMap->GetWidth(), multiSelectedPixelMap->GetHeight());
3408             multiSelectedNode->SetFrame(multiSelectedPositionX, multiSelectedPositionY,
3409                 multiSelectedPixelMap->GetWidth(), multiSelectedPixelMap->GetHeight());
3410         }
3411     }
3412 }
3413 
InitMultiSelectedNodes()3414 void DragDrawing::InitMultiSelectedNodes()
3415 {
3416     FI_HILOGD("enter");
3417     size_t multiSelectedPixelMapsSize = g_drawingInfo.multiSelectedPixelMaps.size();
3418     for (size_t i = 0; i < multiSelectedPixelMapsSize; ++i) {
3419         std::shared_ptr<Media::PixelMap> multiSelectedPixelMap = g_drawingInfo.multiSelectedPixelMaps[i];
3420         std::shared_ptr<Rosen::RSCanvasNode> multiSelectedNode = Rosen::RSCanvasNode::Create();
3421         multiSelectedNode->SetBgImageWidth(multiSelectedPixelMap->GetWidth());
3422         multiSelectedNode->SetBgImageHeight(multiSelectedPixelMap->GetHeight());
3423         multiSelectedNode->SetBgImagePositionX(0);
3424         multiSelectedNode->SetBgImagePositionY(0);
3425         multiSelectedNode->SetForegroundColor(TRANSPARENT_COLOR_ARGB);
3426         auto rosenImage = std::make_shared<Rosen::RSImage>();
3427         rosenImage->SetPixelMap(multiSelectedPixelMap);
3428         rosenImage->SetImageRepeat(0);
3429         multiSelectedNode->SetBgImage(rosenImage);
3430         float alpha = DEFAULT_ALPHA;
3431         float degrees = DEFAULT_ANGLE;
3432         if (i == FIRST_PIXELMAP_INDEX) {
3433             alpha = FIRST_PIXELMAP_ALPHA;
3434             degrees = POSITIVE_ANGLE;
3435         } else if (i == SECOND_PIXELMAP_INDEX) {
3436             alpha = SECOND_PIXELMAP_ALPHA;
3437             degrees = NEGATIVE_ANGLE;
3438         }
3439         multiSelectedNode->SetRotation(degrees);
3440         multiSelectedNode->SetCornerRadius(g_drawingInfo.filterInfo.cornerRadius1 * g_drawingInfo.filterInfo.dipScale *
3441             g_drawingInfo.filterInfo.scale);
3442         multiSelectedNode->SetAlpha(alpha);
3443         g_drawingInfo.multiSelectedNodes.emplace_back(multiSelectedNode);
3444     }
3445     FI_HILOGD("leave");
3446 }
3447 
ClearMultiSelectedData()3448 void DragDrawing::ClearMultiSelectedData()
3449 {
3450     FI_HILOGD("enter");
3451     if (!g_drawingInfo.multiSelectedNodes.empty()) {
3452         g_drawingInfo.multiSelectedNodes.clear();
3453         g_drawingInfo.multiSelectedNodes.shrink_to_fit();
3454     }
3455     if (!g_drawingInfo.multiSelectedPixelMaps.empty()) {
3456         g_drawingInfo.multiSelectedPixelMaps.clear();
3457         g_drawingInfo.multiSelectedPixelMaps.shrink_to_fit();
3458     }
3459     FI_HILOGD("leave");
3460 }
3461 
RotateDisplayXY(int32_t & displayX,int32_t & displayY)3462 void DragDrawing::RotateDisplayXY(int32_t &displayX, int32_t &displayY)
3463 {
3464 Rosen::Rotation rotation = GetRotation(g_drawingInfo.displayId);
3465 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
3466 #ifndef OHOS_BUILD_PC_PRODUCT
3467     sptr<Rosen::Display> display = Rosen::DisplayManager::GetInstance().GetDisplayById(g_drawingInfo.displayId);
3468 #else
3469     sptr<Rosen::DisplayInfo> display =
3470         Rosen::DisplayManager::GetInstance().GetVisibleAreaDisplayInfoById(g_drawingInfo.displayId);
3471 #endif // OHOS_BUILD_PC_PRODUCT
3472     if (display == nullptr) {
3473         FI_HILOGD("Get display info failed, display:%{public}d", g_drawingInfo.displayId);
3474         rotation = GetRotation(0);
3475 #ifndef OHOS_BUILD_PC_PRODUCT
3476         display = Rosen::DisplayManager::GetInstance().GetDisplayById(0);
3477 #else
3478         display = Rosen::DisplayManager::GetInstance().GetVisibleAreaDisplayInfoById(0);
3479 #endif // OHOS_BUILD_PC_PRODUCT
3480         CHKPV(display);
3481     }
3482     int32_t width = display->GetWidth();
3483     int32_t height = display->GetHeight();
3484 #else
3485     CHKPV(window_);
3486     int32_t width = window_->GetRect().width_;
3487     int32_t height = window_->GetRect().height_;
3488 #endif // OHOS_BUILD_ENABLE_ARKUI_X
3489     switch (rotation) {
3490         case Rosen::Rotation::ROTATION_0: {
3491             break;
3492         }
3493         case Rosen::Rotation::ROTATION_90: {
3494             int32_t temp = displayY;
3495             displayY = width - displayX;
3496             displayX = temp;
3497             break;
3498         }
3499         case Rosen::Rotation::ROTATION_180: {
3500             displayX = width - displayX;
3501             displayY = height - displayY;
3502             break;
3503         }
3504         case Rosen::Rotation::ROTATION_270: {
3505             int32_t temp = displayX;
3506             displayX = height - displayY;
3507             displayY = temp;
3508             break;
3509         }
3510         default: {
3511             FI_HILOGW("Unknown parameter, rotation:%{public}d", static_cast<int32_t>(rotation));
3512             break;
3513         }
3514     }
3515 }
3516 
RotatePosition(float & displayX,float & displayY)3517 void DragDrawing::RotatePosition(float &displayX, float &displayY)
3518 {
3519 Rosen::Rotation rotation = GetRotation(g_drawingInfo.displayId);
3520 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
3521     int32_t width = displayWidth_;
3522     int32_t height = displayHeight_;
3523 #else
3524     CHKPV(window_);
3525     int32_t width = window_->GetRect().width_;
3526     int32_t height = window_->GetRect().height_;
3527 #endif // OHOS_BUILD_ENABLE_ARKUI_X
3528     switch (rotation) {
3529         case Rosen::Rotation::ROTATION_0: {
3530             break;
3531         }
3532         case Rosen::Rotation::ROTATION_90: {
3533             int32_t temp = displayY;
3534             displayY = width - displayX;
3535             displayX = temp;
3536             break;
3537         }
3538         case Rosen::Rotation::ROTATION_180: {
3539             displayX = width - displayX;
3540             displayY = height - displayY;
3541             break;
3542         }
3543         case Rosen::Rotation::ROTATION_270: {
3544             int32_t temp = displayX;
3545             displayX = height - displayY;
3546             displayY = temp;
3547             break;
3548         }
3549         default: {
3550             FI_HILOGE("Invalid parameter, rotation:%{public}d", static_cast<int32_t>(rotation));
3551             break;
3552         }
3553     }
3554 }
3555 
RotatePixelMapXY()3556 void DragDrawing::RotatePixelMapXY()
3557 {
3558     Rosen::Rotation rotation = GetRotation(g_drawingInfo.displayId);
3559     FI_HILOGI("displayId:%{public}d, rotation:%{public}d", g_drawingInfo.displayId, static_cast<int32_t>(rotation));
3560     auto currentPixelMap = DragDrawing::AccessGlobalPixelMapLocked();
3561     CHKPV(currentPixelMap);
3562     switch (rotation) {
3563         case Rosen::Rotation::ROTATION_0:
3564         case Rosen::Rotation::ROTATION_180: {
3565             g_drawingInfo.pixelMapX = -(HALF_RATIO * currentPixelMap->GetWidth());
3566             g_drawingInfo.pixelMapY = -(EIGHT_SIZE * GetScaling());
3567             break;
3568         }
3569         case Rosen::Rotation::ROTATION_90:
3570         case Rosen::Rotation::ROTATION_270: {
3571             g_drawingInfo.pixelMapX = -(HALF_RATIO * currentPixelMap->GetWidth());
3572             g_drawingInfo.pixelMapY = -(EIGHT_SIZE * GetScaling());
3573             break;
3574         }
3575         default: {
3576             FI_HILOGE("Invalid parameter, rotation:%{public}d", static_cast<int32_t>(rotation));
3577             break;
3578         }
3579     }
3580 }
3581 
ResetAnimationParameter()3582 void DragDrawing::ResetAnimationParameter()
3583 {
3584     FI_HILOGI("enter");
3585     hasRunningScaleAnimation_ = false;
3586     CHKPV(handler_);
3587 #ifndef IOS_PLATFORM
3588     handler_->RemoveAllEvents();
3589     handler_->RemoveAllFileDescriptorListeners();
3590 #endif // IOS_PLATFORM
3591     handler_ = nullptr;
3592     UpdateReceiverLocked(nullptr);
3593     FI_HILOGI("leave");
3594 }
3595 
GetSvgTouchPositionX(int32_t currentPixelMapWidth,int32_t stylePixelMapWidth,bool isRTL)3596 int32_t DragDrawing::GetSvgTouchPositionX(int32_t currentPixelMapWidth, int32_t stylePixelMapWidth, bool isRTL)
3597 {
3598     float scalingValue = GetScaling();
3599     int32_t adjustSize = EIGHT_SIZE * scalingValue;
3600     int32_t svgTouchPositionX = -adjustSize;
3601     if (!isRTL) {
3602         svgTouchPositionX = currentPixelMapWidth + adjustSize - stylePixelMapWidth;
3603     }
3604     return svgTouchPositionX;
3605 }
3606 
ResetAnimationFlag(bool isForce)3607 void DragDrawing::ResetAnimationFlag(bool isForce)
3608 {
3609     FI_HILOGI("enter");
3610 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
3611     if (!isForce && (g_drawingInfo.context != nullptr) && (g_drawingInfo.timerId >= 0)) {
3612         g_drawingInfo.context->GetTimerManager().RemoveTimer(g_drawingInfo.timerId);
3613         g_drawingInfo.timerId = -1;
3614     }
3615 #endif // OHOS_BUILD_ENABLE_ARKUI_X
3616     if (drawDynamicEffectModifier_ != nullptr) {
3617         CHKPV(g_drawingInfo.rootNode);
3618         g_drawingInfo.rootNode->RemoveModifier(drawDynamicEffectModifier_);
3619         drawDynamicEffectModifier_ = nullptr;
3620     }
3621     DestroyDragWindow();
3622     g_drawingInfo.isRunning = false;
3623     g_drawingInfo.timerId = -1;
3624     ResetAnimationParameter();
3625     FI_HILOGI("leave");
3626 }
3627 
DoEndAnimation()3628 void DragDrawing::DoEndAnimation()
3629 {
3630     FI_HILOGI("enter");
3631 #ifdef IOS_PLATFORM
3632     g_drawingInfo.startNum = actionTime_;
3633 #else
3634     g_drawingInfo.startNum = START_TIME;
3635 #endif // IOS_PLATFORM
3636     g_drawingInfo.needDestroyDragWindow = true;
3637 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
3638     if (g_drawingInfo.context != nullptr) {
3639         int32_t repeatCount = 1;
3640         g_drawingInfo.timerId = g_drawingInfo.context->GetTimerManager().AddTimer(TIMEOUT_MS, repeatCount, [this]() {
3641             FI_HILOGW("Timeout, automatically reset animation flag");
3642             CHKPV(handler_);
3643             handler_->PostTask(std::bind(&DragDrawing::ResetAnimationFlag, this, true));
3644         });
3645     }
3646 #endif // OHOS_BUILD_ENABLE_ARKUI_X
3647     StartVsync();
3648     FI_HILOGI("leave");
3649 }
3650 
ResetParameter()3651 void DragDrawing::ResetParameter()
3652 {
3653     FI_HILOGI("enter");
3654     g_drawingInfo.startNum = START_TIME;
3655     g_drawingInfo.needDestroyDragWindow = false;
3656     displayWidth_ = -1;
3657     displayHeight_ = -1;
3658     needRotatePixelMapXY_ = false;
3659     hasRunningStopAnimation_ = false;
3660     needMultiSelectedAnimation_ = true;
3661     pointerStyle_ = {};
3662     g_drawingInfo.isExistScalingValue = false;
3663     g_drawingInfo.currentPositionX = -1.0f;
3664     g_drawingInfo.currentPositionY = -1.0f;
3665     g_drawingInfo.sourceType = -1;
3666     g_drawingInfo.currentDragNum = -1;
3667     g_drawingInfo.pixelMapX = -1;
3668     g_drawingInfo.pixelMapY = -1;
3669     g_drawingInfo.displayX = -1;
3670     g_drawingInfo.displayY = -1;
3671     g_drawingInfo.mouseWidth = 0;
3672     g_drawingInfo.mouseHeight = 0;
3673     g_drawingInfo.rootNodeWidth = -1;
3674     g_drawingInfo.rootNodeHeight = -1;
3675     DragDrawing::UpdataGlobalPixelMapLocked(nullptr);
3676     g_drawingInfo.stylePixelMap = nullptr;
3677     g_drawingInfo.isPreviousDefaultStyle = false;
3678     g_drawingInfo.isCurrentDefaultStyle = false;
3679     g_drawingInfo.currentStyle = DragCursorStyle::DEFAULT;
3680     g_drawingInfo.filterInfo = {};
3681     g_drawingInfo.extraInfo = {};
3682 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
3683     StopVSyncStation();
3684     frameCallback_ = nullptr;
3685     DragWindowRotationFlush_ = { Rosen::Rotation::ROTATION_0 };
3686 #endif // OHOS_BUILD_ENABLE_ARKUI_X
3687     isRunningRotateAnimation_ = false;
3688     screenRotateState_ = false;
3689     isRTL_ = false;
3690     FI_HILOGI("leave");
3691 }
3692 
3693 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
StopVSyncStation()3694 void DragDrawing::StopVSyncStation()
3695 {
3696     FI_HILOGI("enter");
3697     dragSmoothProcessor_.ResetParameters();
3698     vSyncStation_.StopVSyncRequest();
3699     FI_HILOGI("leave");
3700 }
3701 
SetDragStyleRTL(bool isRTL)3702 void DragDrawing::SetDragStyleRTL(bool isRTL)
3703 {
3704     FI_HILOGI("enter");
3705     isRTL_ = isRTL;
3706     FI_HILOGI("leave");
3707 }
3708 #endif // OHOS_BUILD_ENABLE_ARKUI_X
3709 
DoRotateDragWindow(float rotation,const std::shared_ptr<Rosen::RSTransaction> & rsTransaction,bool isAnimated)3710 int32_t DragDrawing::DoRotateDragWindow(float rotation,
3711     const std::shared_ptr<Rosen::RSTransaction>& rsTransaction, bool isAnimated)
3712 {
3713     FI_HILOGI("Rotation:%{public}f, isAnimated:%{public}d", rotation, isAnimated);
3714 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
3715 #ifndef OHOS_BUILD_PC_PRODUCT
3716     sptr<Rosen::Display> display = Rosen::DisplayManager::GetInstance().GetDisplayById(g_drawingInfo.displayId);
3717 #else
3718     sptr<Rosen::DisplayInfo> display =
3719         Rosen::DisplayManager::GetInstance().GetVisibleAreaDisplayInfoById(g_drawingInfo.displayId);
3720 #endif // OHOS_BUILD_PC_PRODUCT
3721     if (display == nullptr) {
3722         FI_HILOGD("Get display info failed, display:%{public}d", g_drawingInfo.displayId);
3723 #ifndef OHOS_BUILD_PC_PRODUCT
3724         display = Rosen::DisplayManager::GetInstance().GetDisplayById(0);
3725 #else
3726         display = Rosen::DisplayManager::GetInstance().GetVisibleAreaDisplayInfoById(0);
3727 #endif // OHOS_BUILD_PC_PRODUCT
3728         CHKPR(display, RET_ERR);
3729     }
3730     displayWidth_ = display->GetWidth();
3731     displayHeight_ = display->GetHeight();
3732 #endif // OHOS_BUILD_ENABLE_ARKUI_X
3733     auto currentPixelMap = DragDrawing::AccessGlobalPixelMapLocked();
3734     CHKPR(currentPixelMap, RET_ERR);
3735     if ((currentPixelMap->GetWidth() <= 0) || (currentPixelMap->GetHeight() <= 0)) {
3736         FI_HILOGE("Invalid parameter pixelmap");
3737         return RET_ERR;
3738     }
3739     float adjustSize = TWELVE_SIZE * GetScaling();
3740     float pivotX = HALF_PIVOT;
3741     float pivotY = 0.0f;
3742     if (fabsf(adjustSize + currentPixelMap->GetHeight()) < EPSILON) {
3743         pivotY = HALF_PIVOT;
3744     } else {
3745         pivotY = ((currentPixelMap->GetHeight() * 1.0 / TWICE_SIZE) + adjustSize) /
3746             (adjustSize + currentPixelMap->GetHeight());
3747     }
3748     if (!isAnimated) {
3749         DragWindowRotateInfo_.rotation = rotation;
3750         DragWindowRotateInfo_.pivotX = pivotX;
3751         DragWindowRotateInfo_.pivotY = pivotY;
3752         RotateCanvasNode(pivotX, pivotY, rotation);
3753         Rosen::RSTransaction::FlushImplicitTransaction();
3754         return RET_OK;
3755     }
3756     return DoRotateDragWindowAnimation(rotation, pivotX, pivotY, rsTransaction);
3757 }
3758 
3759 template <typename T>
AdjustRotateDisplayXY(T & displayX,T & displayY)3760 void DragDrawing::AdjustRotateDisplayXY(T &displayX, T &displayY)
3761 {
3762     Rosen::Rotation rotation = GetRotation(g_drawingInfo.displayId);
3763     FI_HILOGD("displayId:%{public}d, rotation:%{public}d", g_drawingInfo.displayId, static_cast<int32_t>(rotation));
3764     auto currentPixelMap = DragDrawing::AccessGlobalPixelMapLocked();
3765     CHKPV(currentPixelMap);
3766     switch (rotation) {
3767         case Rosen::Rotation::ROTATION_0: {
3768             break;
3769         }
3770         case Rosen::Rotation::ROTATION_90: {
3771             displayX -= (currentPixelMap->GetWidth() - currentPixelMap->GetHeight()) / TWICE_SIZE +
3772                 g_drawingInfo.pixelMapX - g_drawingInfo.pixelMapY;
3773             displayY -= (currentPixelMap->GetWidth() - currentPixelMap->GetHeight()) / TWICE_SIZE +
3774                 g_drawingInfo.pixelMapX + currentPixelMap->GetHeight() + g_drawingInfo.pixelMapY;
3775             break;
3776         }
3777         case Rosen::Rotation::ROTATION_180: {
3778             displayX -= currentPixelMap->GetWidth() + (g_drawingInfo.pixelMapX * TWICE_SIZE);
3779             displayY -= currentPixelMap->GetHeight() + (g_drawingInfo.pixelMapY * TWICE_SIZE);
3780             break;
3781         }
3782         case Rosen::Rotation::ROTATION_270: {
3783             displayX -= (currentPixelMap->GetWidth() - currentPixelMap->GetHeight()) / TWICE_SIZE +
3784                 g_drawingInfo.pixelMapX + currentPixelMap->GetHeight() + g_drawingInfo.pixelMapY;
3785             displayY += (currentPixelMap->GetWidth() - currentPixelMap->GetHeight()) / TWICE_SIZE +
3786                 g_drawingInfo.pixelMapX - g_drawingInfo.pixelMapY;
3787             break;
3788         }
3789         default: {
3790             FI_HILOGE("Invalid parameter, rotation:%{public}d", static_cast<int32_t>(rotation));
3791             break;
3792         }
3793     }
3794 }
3795 
DrawRotateDisplayXY(float positionX,float positionY)3796 void DragDrawing::DrawRotateDisplayXY(float positionX, float positionY)
3797 {
3798     FI_HILOGD("enter");
3799     float adjustSize = TWELVE_SIZE * GetScaling();
3800     float parentPositionX = positionX + g_drawingInfo.pixelMapX;
3801     float parentPositionY = positionY + g_drawingInfo.pixelMapY - adjustSize;
3802     auto parentNode = g_drawingInfo.parentNode;
3803     auto currentPixelMap = DragDrawing::AccessGlobalPixelMapLocked();
3804     CHKPV(parentNode);
3805     CHKPV(currentPixelMap);
3806     parentNode->SetBounds(parentPositionX, parentPositionY, currentPixelMap->GetWidth(),
3807         currentPixelMap->GetHeight() + adjustSize);
3808     parentNode->SetFrame(parentPositionX, parentPositionY, currentPixelMap->GetWidth(),
3809         currentPixelMap->GetHeight() + adjustSize);
3810     if (!g_drawingInfo.multiSelectedNodes.empty() && !g_drawingInfo.multiSelectedPixelMaps.empty()) {
3811         DoMultiSelectedAnimation(parentPositionX, parentPositionY, adjustSize, false);
3812     }
3813     FI_HILOGD("leave");
3814 }
3815 
ScreenRotateAdjustDisplayXY(Rosen::Rotation rotation,Rosen::Rotation lastRotation,float & displayX,float & displayY)3816 void DragDrawing::ScreenRotateAdjustDisplayXY(
3817     Rosen::Rotation rotation, Rosen::Rotation lastRotation, float &displayX, float &displayY)
3818 {
3819     FI_HILOGI("enter");
3820 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
3821 #ifndef OHOS_BUILD_PC_PRODUCT
3822     sptr<Rosen::Display> display = Rosen::DisplayManager::GetInstance().GetDisplayById(g_drawingInfo.displayId);
3823 #else
3824     sptr<Rosen::DisplayInfo> display =
3825         Rosen::DisplayManager::GetInstance().GetVisibleAreaDisplayInfoById(g_drawingInfo.displayId);
3826 #endif // OHOS_BUILD_PC_PRODUCT
3827     if (display == nullptr) {
3828         FI_HILOGD("Get display info failed, display:%{public}d", g_drawingInfo.displayId);
3829 #ifndef OHOS_BUILD_PC_PRODUCT
3830         display = Rosen::DisplayManager::GetInstance().GetDisplayById(0);
3831 #else
3832         display = Rosen::DisplayManager::GetInstance().GetVisibleAreaDisplayInfoById(0);
3833 #endif // OHOS_BUILD_PC_PRODUCT
3834         CHKPV(display);
3835     }
3836     displayWidth_ = display->GetWidth();
3837     displayHeight_ = display->GetHeight();
3838     int32_t width = displayWidth_;
3839     int32_t height = displayHeight_;
3840 #else
3841     CHKPV(window_);
3842     int32_t width = window_->GetRect().width_;
3843     int32_t height = window_->GetRect().height_;
3844 #endif // OHOS_BUILD_ENABLE_ARKUI_X
3845     if ((static_cast<int32_t>(lastRotation) + NUM_ONE) % NUM_FOUR == static_cast<int32_t>(rotation)) {
3846         int32_t temp = displayX;
3847         displayX = width - displayY;
3848         displayY = temp;
3849     } else if ((static_cast<int32_t>(lastRotation) + NUM_TWO) % NUM_FOUR == static_cast<int32_t>(rotation)) {
3850         displayX = width - displayX;
3851         displayY = height - displayY;
3852     } else {
3853         int32_t temp = displayY;
3854         displayY = height - displayX;
3855         displayX = temp;
3856     }
3857     FI_HILOGI("leave");
3858 }
3859 
UpdateDragDataForSuperHub(const DragData & dragData)3860 void DragDrawing::UpdateDragDataForSuperHub(const DragData &dragData)
3861 {
3862     g_dragDataForSuperHub.extraInfo = dragData.extraInfo;
3863     g_dragDataForSuperHub.sourceType = dragData.sourceType;
3864     g_dragDataForSuperHub.displayX = dragData.displayX;
3865     g_dragDataForSuperHub.displayY = dragData.displayY;
3866     g_dragDataForSuperHub.dragNum = dragData.dragNum;
3867     g_dragDataForSuperHub.summarys = dragData.summarys;
3868 }
3869 
AccessGlobalPixelMapLocked()3870 std::shared_ptr<Media::PixelMap> DragDrawing::AccessGlobalPixelMapLocked()
3871 {
3872     std::shared_lock<std::shared_mutex> lock(g_pixelMapLock);
3873     return g_drawingInfo.pixelMap;
3874 }
3875 
UpdataGlobalPixelMapLocked(std::shared_ptr<Media::PixelMap> pixelmap)3876 void DragDrawing::UpdataGlobalPixelMapLocked(std::shared_ptr<Media::PixelMap> pixelmap)
3877 {
3878     std::unique_lock<std::shared_mutex> lock(g_pixelMapLock);
3879     g_drawingInfo.pixelMap = pixelmap;
3880 }
3881 
AccessReceiverLocked()3882 std::shared_ptr<Rosen::VSyncReceiver> DragDrawing::AccessReceiverLocked()
3883 {
3884     std::shared_lock<std::shared_mutex> lock(receiverMutex_);
3885     return receiver_;
3886 }
3887 
UpdateReceiverLocked(std::shared_ptr<Rosen::VSyncReceiver> receiver)3888 void DragDrawing::UpdateReceiverLocked(std::shared_ptr<Rosen::VSyncReceiver> receiver)
3889 {
3890     std::unique_lock<std::shared_mutex> lock(receiverMutex_);
3891     receiver_ = receiver;
3892 }
3893 
ScreenRotate(Rosen::Rotation rotation,Rosen::Rotation lastRotation)3894 void DragDrawing::ScreenRotate(Rosen::Rotation rotation, Rosen::Rotation lastRotation)
3895 {
3896     FI_HILOGI("enter, rotation:%{public}d, lastRotation:%{public}d", static_cast<int32_t>(rotation),
3897         static_cast<int32_t>(lastRotation));
3898     ScreenRotateAdjustDisplayXY(rotation, lastRotation, g_drawingInfo.x, g_drawingInfo.y);
3899     DrawRotateDisplayXY(g_drawingInfo.x, g_drawingInfo.y);
3900 #ifndef OHOS_BUILD_PC_PRODUCT
3901     if (g_drawingInfo.sourceType == MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
3902         ScreenRotateAdjustDisplayXY(
3903             rotation, lastRotation, g_drawingInfo.currentPositionX, g_drawingInfo.currentPositionY);
3904         UpdateMousePosition(g_drawingInfo.currentPositionX, g_drawingInfo.currentPositionY);
3905     }
3906 #endif // OHOS_BUILD_PC_PRODUCT
3907     Rosen::RSTransaction::FlushImplicitTransaction();
3908     screenRotateState_ = true;
3909     FI_HILOGI("leave");
3910 }
3911 
DoRotateDragWindowAnimation(float rotation,float pivotX,float pivotY,const std::shared_ptr<Rosen::RSTransaction> & rsTransaction)3912 int32_t DragDrawing::DoRotateDragWindowAnimation(float rotation, float pivotX, float pivotY,
3913     const std::shared_ptr<Rosen::RSTransaction>& rsTransaction)
3914 {
3915     FI_HILOGD("enter");
3916     if (rsTransaction != nullptr) {
3917         Rosen::RSTransaction::FlushImplicitTransaction();
3918         rsTransaction->Begin();
3919     }
3920     if ((rotation == ROTATION_0) && (DragWindowRotateInfo_.rotation == ROTATION_270)) {
3921         RotateCanvasNode(DragWindowRotateInfo_.pivotX, DragWindowRotateInfo_.pivotY, -ROTATION_90);
3922     } else if ((rotation == ROTATION_270) && (DragWindowRotateInfo_.rotation == ROTATION_0)) {
3923         RotateCanvasNode(DragWindowRotateInfo_.pivotX, DragWindowRotateInfo_.pivotY, ROTATION_360);
3924     }
3925 
3926     Rosen::RSAnimationTimingProtocol protocol;
3927     protocol.SetDuration(ANIMATION_DURATION);
3928     Rosen::RSNode::Animate(protocol, SPRING, [&]() {
3929         RotateCanvasNode(pivotX, pivotY, rotation);
3930         DragWindowRotateInfo_.rotation = rotation;
3931         DragWindowRotateInfo_.pivotX = pivotX;
3932         DragWindowRotateInfo_.pivotY = pivotY;
3933         return RET_OK;
3934     },  []() { FI_HILOGD("DoRotateDragWindowAnimation end"); });
3935     if (rsTransaction != nullptr) {
3936         rsTransaction->Commit();
3937     } else {
3938         Rosen::RSTransaction::FlushImplicitTransaction();
3939     }
3940     FI_HILOGD("leave");
3941     return RET_OK;
3942 }
3943 
ParserRadius(float & radius)3944 bool DragDrawing::ParserRadius(float &radius)
3945 {
3946     FilterInfo filterInfo = g_drawingInfo.filterInfo;
3947     ExtraInfo extraInfo = g_drawingInfo.extraInfo;
3948     if ((extraInfo.cornerRadius < 0) || (filterInfo.dipScale < 0) ||
3949         (fabs(filterInfo.dipScale) < EPSILON) || ((std::numeric_limits<float>::max()
3950         / filterInfo.dipScale) < extraInfo.cornerRadius)) {
3951         FI_HILOGE("Invalid parameters, cornerRadius:%{public}f, dipScale:%{public}f",
3952             extraInfo.cornerRadius, filterInfo.dipScale);
3953         return false;
3954     }
3955     radius = extraInfo.cornerRadius * filterInfo.dipScale;
3956     return true;
3957 }
3958 
~DragDrawing()3959 DragDrawing::~DragDrawing()
3960 {
3961 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
3962     if (dragExtHandler_ != nullptr) {
3963         dlclose(dragExtHandler_);
3964         dragExtHandler_ = nullptr;
3965     }
3966 #endif // OHOS_BUILD_ENABLE_ARKUI_X
3967 }
3968 
Draw(RSDrawingContext & context) const3969 void DrawSVGModifier::Draw(RSDrawingContext& context) const
3970 {
3971     FI_HILOGD("enter");
3972     CHKPV(stylePixelMap_);
3973     auto currentPixelMap = DragDrawing::AccessGlobalPixelMapLocked();
3974     CHKPV(currentPixelMap);
3975     float scalingValue = GetScaling();
3976     if (SCALE_THRESHOLD_EIGHT < scalingValue || fabsf(SCALE_THRESHOLD_EIGHT - scalingValue) < EPSILON) {
3977         FI_HILOGE("Invalid scalingValue:%{public}f", scalingValue);
3978         return;
3979     }
3980     int32_t svgTouchPositionX = DragDrawing::GetSvgTouchPositionX(
3981         currentPixelMap->GetWidth(), stylePixelMap_->GetWidth(), isRTL_);
3982     if (!CheckNodesValid()) {
3983         FI_HILOGE("Check nodes valid failed");
3984         return;
3985     }
3986     std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode = g_drawingInfo.nodes[DRAG_STYLE_INDEX];
3987     CHKPV(dragStyleNode);
3988     int32_t adjustSize = (TWELVE_SIZE - EIGHT_SIZE) * scalingValue;
3989     dragStyleNode->SetBounds(svgTouchPositionX, adjustSize, stylePixelMap_->GetWidth() + adjustSize,
3990         stylePixelMap_->GetHeight());
3991     dragStyleNode->SetFrame(svgTouchPositionX, adjustSize, stylePixelMap_->GetWidth() + adjustSize,
3992         stylePixelMap_->GetHeight());
3993     dragStyleNode->SetBgImageWidth(stylePixelMap_->GetWidth());
3994     dragStyleNode->SetBgImageHeight(stylePixelMap_->GetHeight());
3995     dragStyleNode->SetBgImagePositionX(0);
3996     dragStyleNode->SetBgImagePositionY(0);
3997     auto rosenImage = std::make_shared<Rosen::RSImage>();
3998     rosenImage->SetPixelMap(stylePixelMap_);
3999     rosenImage->SetImageRepeat(0);
4000     dragStyleNode->SetBgImage(rosenImage);
4001     Rosen::RSTransaction::FlushImplicitTransaction();
4002     FI_HILOGD("leave");
4003 }
4004 
ConvertShadowColorStrategy(int32_t shadowColorStrategy) const4005 Rosen::SHADOW_COLOR_STRATEGY DrawPixelMapModifier::ConvertShadowColorStrategy(int32_t shadowColorStrategy) const
4006 {
4007     if (shadowColorStrategy == static_cast<int32_t>(Rosen::SHADOW_COLOR_STRATEGY::COLOR_STRATEGY_NONE)) {
4008         return Rosen::SHADOW_COLOR_STRATEGY::COLOR_STRATEGY_NONE ;
4009     } else if (shadowColorStrategy == static_cast<int32_t>(Rosen::SHADOW_COLOR_STRATEGY::COLOR_STRATEGY_AVERAGE)) {
4010         return Rosen::SHADOW_COLOR_STRATEGY::COLOR_STRATEGY_AVERAGE ;
4011     } else if (shadowColorStrategy == static_cast<int32_t>(Rosen::SHADOW_COLOR_STRATEGY::COLOR_STRATEGY_MAIN)) {
4012         return Rosen::SHADOW_COLOR_STRATEGY::COLOR_STRATEGY_MAIN ;
4013     } else {
4014         return Rosen::SHADOW_COLOR_STRATEGY::COLOR_STRATEGY_NONE;
4015     }
4016 }
4017 
SetTextDragShadow(std::shared_ptr<Rosen::RSCanvasNode> pixelMapNode) const4018 void DrawPixelMapModifier::SetTextDragShadow(std::shared_ptr<Rosen::RSCanvasNode> pixelMapNode) const
4019 {
4020     if (!g_drawingInfo.filterInfo.path.empty()) {
4021         FI_HILOGD("path:%{private}s", g_drawingInfo.filterInfo.path.c_str());
4022         pixelMapNode->SetShadowPath(Rosen::RSPath::CreateRSPath(g_drawingInfo.filterInfo.path));
4023     } else {
4024         FI_HILOGW("path is empty");
4025     }
4026 }
4027 
SetDragShadow(std::shared_ptr<Rosen::RSCanvasNode> pixelMapNode) const4028 void DrawPixelMapModifier::SetDragShadow(std::shared_ptr<Rosen::RSCanvasNode> pixelMapNode) const
4029 {
4030     if ((g_drawingInfo.filterInfo.dragType == "text") && (g_drawingInfo.filterInfo.path.empty())) {
4031         FI_HILOGI("path is empty");
4032         return;
4033     }
4034     pixelMapNode->SetShadowOffset(g_drawingInfo.filterInfo.offsetX, g_drawingInfo.filterInfo.offsetY);
4035     pixelMapNode->SetShadowColor(g_drawingInfo.filterInfo.argb);
4036     pixelMapNode->SetShadowMask(g_drawingInfo.filterInfo.shadowMask);
4037     pixelMapNode->SetShadowIsFilled(g_drawingInfo.filterInfo.shadowIsFilled);
4038     pixelMapNode->SetShadowColorStrategy(ConvertShadowColorStrategy(g_drawingInfo.filterInfo.shadowColorStrategy));
4039     if (g_drawingInfo.filterInfo.isHardwareAcceleration) {
4040         pixelMapNode->SetShadowElevation(g_drawingInfo.filterInfo.elevation);
4041     } else {
4042         pixelMapNode->SetShadowRadius(g_drawingInfo.filterInfo.shadowCorner);
4043     }
4044     if (g_drawingInfo.filterInfo.dragType == "text") {
4045         SetTextDragShadow(pixelMapNode);
4046     }
4047 }
4048 
Draw(RSDrawingContext & context) const4049 void DrawPixelMapModifier::Draw(RSDrawingContext &context) const
4050 {
4051     FI_HILOGD("enter");
4052     auto currentPixelMap = DragDrawing::AccessGlobalPixelMapLocked();
4053     CHKPV(currentPixelMap);
4054     int32_t pixelMapWidth = currentPixelMap->GetWidth();
4055     int32_t pixelMapHeight = currentPixelMap->GetHeight();
4056     if (!CheckNodesValid()) {
4057         FI_HILOGE("Check nodes valid failed");
4058         return;
4059     }
4060     std::shared_ptr<Rosen::RSCanvasNode> pixelMapNode = g_drawingInfo.nodes[PIXEL_MAP_INDEX];
4061     CHKPV(pixelMapNode);
4062     if (g_drawingInfo.filterInfo.shadowEnable) {
4063         SetDragShadow(pixelMapNode);
4064     }
4065     int32_t adjustSize = TWELVE_SIZE * GetScaling();
4066     pixelMapNode->SetBounds(DEFAULT_POSITION_X, adjustSize, pixelMapWidth, pixelMapHeight);
4067     pixelMapNode->SetFrame(DEFAULT_POSITION_X, adjustSize, pixelMapWidth, pixelMapHeight);
4068     pixelMapNode->SetBgImageWidth(pixelMapWidth);
4069     pixelMapNode->SetBgImageHeight(pixelMapHeight);
4070     pixelMapNode->SetBgImagePositionX(0);
4071     pixelMapNode->SetBgImagePositionY(0);
4072     Rosen::Drawing::AdaptiveImageInfo rsImageInfo = { 1, 0, {}, 1, 0, pixelMapWidth, pixelMapHeight };
4073     auto cvs = pixelMapNode->BeginRecording(pixelMapWidth, pixelMapHeight);
4074     CHKPV(cvs);
4075     Rosen::Drawing::Brush brush;
4076     cvs->AttachBrush(brush);
4077     FilterInfo filterInfo = g_drawingInfo.filterInfo;
4078     if (g_drawingInfo.filterInfo.shadowEnable && !filterInfo.path.empty() &&
4079         g_drawingInfo.filterInfo.dragType == "text") {
4080         auto rsPath = Rosen::RSPath::CreateRSPath(filterInfo.path);
4081         cvs->Save();
4082         cvs->ClipPath(rsPath->GetDrawingPath(), Rosen::Drawing::ClipOp::INTERSECT, true);
4083         cvs->DrawPixelMapWithParm(currentPixelMap, rsImageInfo, Rosen::Drawing::SamplingOptions());
4084         cvs->Restore();
4085     } else {
4086         cvs->DrawPixelMapWithParm(currentPixelMap, rsImageInfo, Rosen::Drawing::SamplingOptions());
4087     }
4088     cvs->DetachBrush();
4089     pixelMapNode->SetClipToBounds(true);
4090     pixelMapNode->FinishRecording();
4091     Rosen::RSTransaction::FlushImplicitTransaction();
4092     FI_HILOGD("leave");
4093 }
4094 
Draw(RSDrawingContext & context) const4095 void DrawMouseIconModifier::Draw(RSDrawingContext &context) const
4096 {
4097     FI_HILOGD("enter");
4098 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
4099     std::shared_ptr<Media::PixelMap> pixelMap = std::make_shared<Media::PixelMap>();
4100     int32_t ret = RET_ERR;
4101 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR
4102     ret = MMI::InputManager::GetInstance()->GetPointerSnapshot(&pixelMap);
4103 #endif // OHOS_BUILD_ENABLE_MAGICCURSOR
4104     if (ret != RET_OK) {
4105         FI_HILOGE("Get pointer snapshot failed, ret:%{public}d", ret);
4106         pixelMap = DrawFromSVG();
4107     }
4108     CHKPV(pixelMap);
4109     OnDraw(pixelMap);
4110 #endif // OHOS_BUILD_ENABLE_ARKUI_X
4111     FI_HILOGD("leave");
4112 }
4113 
DrawFromSVG() const4114 std::shared_ptr<Media::PixelMap> DrawMouseIconModifier::DrawFromSVG() const
4115 {
4116     std::string imagePath;
4117     if (pointerStyle_.id == MOUSE_DRAG_CURSOR_CIRCLE_STYLE) {
4118         imagePath = MOUSE_DRAG_CURSOR_CIRCLE_PATH;
4119     } else {
4120         imagePath = MOUSE_DRAG_DEFAULT_PATH;
4121     }
4122     int32_t pointerSize = pointerStyle_.size;
4123     int32_t pointerColor = pointerStyle_.color;
4124     int32_t cursorPixel = DEVICE_INDEPENDENT_PIXEL;
4125 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
4126     if (pointerStyle_.options == MAGIC_STYLE_OPT) {
4127         imagePath = MOUSE_DRAG_MAGIC_DEFAULT_PATH;
4128         int32_t ret = MMI::InputManager::GetInstance()->GetPointerSize(pointerSize);
4129         if (ret != RET_OK) {
4130             FI_HILOGW("Get pointer size failed, ret:%{public}d", ret);
4131         }
4132         ret = MMI::InputManager::GetInstance()->GetPointerColor(pointerColor);
4133         if (ret != RET_OK) {
4134             FI_HILOGW("Get pointer color failed, ret:%{public}d", ret);
4135         }
4136         cursorPixel = MAGIC_INDEPENDENT_PIXEL;
4137     }
4138 #endif // OHOS_BUILD_ENABLE_ARKUI_X
4139     Media::SourceOptions opts;
4140     opts.formatHint = "image/svg+xml";
4141     uint32_t errCode = 0;
4142     auto imageSource = Media::ImageSource::CreateImageSource(imagePath, opts, errCode);
4143     if (imageSource == nullptr) {
4144         FI_HILOGW("imageSource is null");
4145         return nullptr;
4146     }
4147     if (pointerSize < DEFAULT_MOUSE_SIZE) {
4148         FI_HILOGD("Invalid pointerSize:%{public}d", pointerSize);
4149         pointerSize = DEFAULT_MOUSE_SIZE;
4150     }
4151     Media::DecodeOptions decodeOpts;
4152     decodeOpts.desiredSize = {
4153         .width = pow(INCREASE_RATIO, pointerSize - 1) * cursorPixel * GetScaling(),
4154         .height = pow(INCREASE_RATIO, pointerSize - 1) * cursorPixel * GetScaling()
4155     };
4156     if (pointerColor != INVALID_COLOR_VALUE) {
4157         decodeOpts.SVGOpts.fillColor = {.isValidColor = true, .color = pointerColor};
4158     }
4159     return imageSource->CreatePixelMap(decodeOpts, errCode);
4160 }
4161 
OnDraw(std::shared_ptr<Media::PixelMap> pixelMap) const4162 void DrawMouseIconModifier::OnDraw(std::shared_ptr<Media::PixelMap> pixelMap) const
4163 {
4164     FI_HILOGD("enter");
4165 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
4166     CHKPV(pixelMap);
4167     if (!CheckNodesValid()) {
4168         FI_HILOGE("Check nodes valid failed");
4169         return;
4170     }
4171     g_drawingInfo.mouseWidth = pixelMap->GetWidth();
4172     g_drawingInfo.mouseHeight = pixelMap->GetHeight();
4173     if (g_drawingInfo.nodes.size() <= MOUSE_ICON_INDEX) {
4174         FI_HILOGE("The index is out of bounds, node size is %{public}zu", g_drawingInfo.nodes.size());
4175         return;
4176     }
4177     std::shared_ptr<Rosen::RSCanvasNode> mouseIconNode = g_drawingInfo.nodes[MOUSE_ICON_INDEX];
4178     CHKPV(mouseIconNode);
4179     mouseIconNode->SetBgImageWidth(pixelMap->GetWidth());
4180     mouseIconNode->SetBgImageHeight(pixelMap->GetHeight());
4181     mouseIconNode->SetBgImagePositionX(0);
4182     mouseIconNode->SetBgImagePositionY(0);
4183     auto rosenImage = std::make_shared<Rosen::RSImage>();
4184     rosenImage->SetPixelMap(pixelMap);
4185     rosenImage->SetImageRepeat(0);
4186     mouseIconNode->SetBgImage(rosenImage);
4187     Rosen::RSTransaction::FlushImplicitTransaction();
4188 #endif // OHOS_BUILD_ENABLE_ARKUI_X
4189     FI_HILOGD("leave");
4190 }
4191 
Draw(RSDrawingContext & context) const4192 void DrawDynamicEffectModifier::Draw(RSDrawingContext &context) const
4193 {
4194     FI_HILOGD("enter");
4195     CHKPV(alpha_);
4196     CHKPV(g_drawingInfo.parentNode);
4197     g_drawingInfo.parentNode->SetAlpha(alpha_->Get());
4198     CHKPV(scale_);
4199     g_drawingInfo.parentNode->SetScale(scale_->Get(), scale_->Get());
4200     Rosen::RSTransaction::FlushImplicitTransaction();
4201     FI_HILOGD("leave");
4202 }
4203 
SetAlpha(float alpha)4204 void DrawDynamicEffectModifier::SetAlpha(float alpha)
4205 {
4206     FI_HILOGD("enter");
4207     if (alpha_ == nullptr) {
4208         alpha_ = std::make_shared<Rosen::RSAnimatableProperty<float>>(alpha);
4209         RSModifier::AttachProperty(alpha_);
4210         return;
4211     }
4212     alpha_->Set(alpha);
4213     FI_HILOGD("leave");
4214 }
4215 
SetScale(float scale)4216 void DrawDynamicEffectModifier::SetScale(float scale)
4217 {
4218     FI_HILOGD("enter");
4219     if (scale_ == nullptr) {
4220         scale_ = std::make_shared<Rosen::RSAnimatableProperty<float>>(scale);
4221         RSModifier::AttachProperty(scale_);
4222         return;
4223     }
4224     scale_->Set(scale);
4225     FI_HILOGD("leave");
4226 }
4227 
Draw(RSDrawingContext & context) const4228 void DrawStyleChangeModifier::Draw(RSDrawingContext &context) const
4229 {
4230     FI_HILOGD("enter");
4231     if (!CheckNodesValid()) {
4232         FI_HILOGE("Check nodes valid failed");
4233         return;
4234     }
4235     if (g_drawingInfo.nodes.size() <= DRAG_STYLE_INDEX) {
4236         FI_HILOGE("The index is out of bounds, node size is %{public}zu", g_drawingInfo.nodes.size());
4237         return;
4238     }
4239     std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode = g_drawingInfo.nodes[DRAG_STYLE_INDEX];
4240     CHKPV(dragStyleNode);
4241     auto currentPixelMap = DragDrawing::AccessGlobalPixelMapLocked();
4242     CHKPV(currentPixelMap);
4243     float pixelMapWidth = currentPixelMap->GetWidth();
4244     if (stylePixelMap_ == nullptr) {
4245         if (scale_ == nullptr) {
4246             return;
4247         }
4248         dragStyleNode->SetScale(scale_->Get());
4249         return;
4250     }
4251     float scalingValue = GetScaling();
4252     if ((1.0 * INT_MAX / EIGHT_SIZE) <= scalingValue) {
4253         return;
4254     }
4255     int32_t svgTouchPositionX = DragDrawing::GetSvgTouchPositionX(pixelMapWidth, stylePixelMap_->GetWidth(), isRTL_);
4256     dragStyleNode->SetBounds(svgTouchPositionX, (TWELVE_SIZE-EIGHT_SIZE)*scalingValue, stylePixelMap_->GetWidth(),
4257         stylePixelMap_->GetHeight());
4258     dragStyleNode->SetFrame(svgTouchPositionX, (TWELVE_SIZE-EIGHT_SIZE)*scalingValue, stylePixelMap_->GetWidth(),
4259         stylePixelMap_->GetHeight());
4260     dragStyleNode->SetBgImageWidth(stylePixelMap_->GetWidth());
4261     dragStyleNode->SetBgImageHeight(stylePixelMap_->GetHeight());
4262     dragStyleNode->SetBgImagePositionX(0);
4263     dragStyleNode->SetBgImagePositionY(0);
4264     auto rosenImage = std::make_shared<Rosen::RSImage>();
4265     rosenImage->SetPixelMap(stylePixelMap_);
4266     rosenImage->SetImageRepeat(0);
4267     dragStyleNode->SetBgImage(rosenImage);
4268     Rosen::RSTransaction::FlushImplicitTransaction();
4269     FI_HILOGD("leave");
4270 }
4271 
SetScale(float scale)4272 void DrawStyleChangeModifier::SetScale(float scale)
4273 {
4274     FI_HILOGD("enter");
4275     if (scale_ == nullptr) {
4276         scale_ = std::make_shared<Rosen::RSAnimatableProperty<float>>(scale);
4277         RSModifier::AttachProperty(scale_);
4278     } else {
4279         scale_->Set(scale);
4280     }
4281     FI_HILOGD("leave");
4282 }
4283 
Draw(RSDrawingContext & context) const4284 void DrawStyleScaleModifier::Draw(RSDrawingContext &context) const
4285 {
4286     FI_HILOGD("enter");
4287     if (!CheckNodesValid()) {
4288         FI_HILOGE("Check nodes valid failed");
4289         return;
4290     }
4291     if (g_drawingInfo.nodes.size() <= DRAG_STYLE_INDEX) {
4292         FI_HILOGE("The index is out of bounds, node size is %{public}zu", g_drawingInfo.nodes.size());
4293         return;
4294     }
4295     std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode = g_drawingInfo.nodes[DRAG_STYLE_INDEX];
4296     CHKPV(dragStyleNode);
4297     CHKPV(scale_);
4298     dragStyleNode->SetScale(scale_->Get());
4299     FI_HILOGD("leave");
4300 }
4301 
SetScale(float scale)4302 void DrawStyleScaleModifier::SetScale(float scale)
4303 {
4304     FI_HILOGD("enter");
4305     if (scale_ == nullptr) {
4306         scale_ = std::make_shared<Rosen::RSAnimatableProperty<float>>(scale);
4307         RSModifier::AttachProperty(scale_);
4308     } else {
4309         scale_->Set(scale);
4310     }
4311     FI_HILOGD("leave");
4312 }
4313 
Draw(RSDrawingContext & context) const4314 void DrawDragStopModifier::Draw(RSDrawingContext &context) const
4315 {
4316     FI_HILOGD("enter");
4317     CHKPV(alpha_);
4318     CHKPV(scale_);
4319     if (!CheckNodesValid()) {
4320         FI_HILOGE("Check nodes valid failed");
4321         return;
4322     }
4323     CHKPV(g_drawingInfo.parentNode);
4324     g_drawingInfo.parentNode->SetAlpha(alpha_->Get());
4325     g_drawingInfo.parentNode->SetScale(scale_->Get(), scale_->Get());
4326     if (!g_drawingInfo.multiSelectedNodes.empty()) {
4327         size_t multiSelectedNodesSize = g_drawingInfo.multiSelectedNodes.size();
4328         for (size_t i = 0; i < multiSelectedNodesSize; ++i) {
4329             std::shared_ptr<Rosen::RSCanvasNode> multiSelectedNode = g_drawingInfo.multiSelectedNodes[i];
4330             CHKPV(multiSelectedNode);
4331             multiSelectedNode->SetAlpha(alpha_->Get());
4332             multiSelectedNode->SetScale(scale_->Get(), scale_->Get());
4333         }
4334     }
4335     if (g_drawingInfo.nodes.size() <= DRAG_STYLE_INDEX) {
4336         FI_HILOGE("The index is out of bounds, node size is %{public}zu", g_drawingInfo.nodes.size());
4337         return;
4338     }
4339     std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode = g_drawingInfo.nodes[DRAG_STYLE_INDEX];
4340     CHKPV(dragStyleNode);
4341     CHKPV(styleScale_);
4342     CHKPV(styleAlpha_);
4343     dragStyleNode->SetScale(styleScale_->Get());
4344     dragStyleNode->SetAlpha(styleAlpha_->Get());
4345     FI_HILOGD("leave");
4346 }
4347 
SetAlpha(float alpha)4348 void DrawDragStopModifier::SetAlpha(float alpha)
4349 {
4350     FI_HILOGD("enter");
4351     if (alpha_ == nullptr) {
4352         alpha_ = std::make_shared<Rosen::RSAnimatableProperty<float>>(alpha);
4353         RSModifier::AttachProperty(alpha_);
4354     } else {
4355         alpha_->Set(alpha);
4356     }
4357     FI_HILOGD("leave");
4358 }
4359 
SetScale(float scale)4360 void DrawDragStopModifier::SetScale(float scale)
4361 {
4362     FI_HILOGD("enter");
4363     if (scale_ == nullptr) {
4364         scale_ = std::make_shared<Rosen::RSAnimatableProperty<float>>(scale);
4365         RSModifier::AttachProperty(scale_);
4366     } else {
4367         scale_->Set(scale);
4368     }
4369     FI_HILOGD("leave");
4370 }
4371 
SetStyleScale(float scale)4372 void DrawDragStopModifier::SetStyleScale(float scale)
4373 {
4374     FI_HILOGD("enter");
4375     if (styleScale_ == nullptr) {
4376         styleScale_ = std::make_shared<Rosen::RSAnimatableProperty<float>>(scale);
4377         RSModifier::AttachProperty(styleScale_);
4378     } else {
4379         styleScale_->Set(scale);
4380     }
4381     FI_HILOGD("leave");
4382 }
4383 
SetStyleAlpha(float alpha)4384 void DrawDragStopModifier::SetStyleAlpha(float alpha)
4385 {
4386     FI_HILOGD("enter");
4387     if (styleAlpha_ == nullptr) {
4388         styleAlpha_ = std::make_shared<Rosen::RSAnimatableProperty<float>>(alpha);
4389         RSModifier::AttachProperty(styleAlpha_);
4390     } else {
4391         styleAlpha_->Set(alpha);
4392     }
4393     FI_HILOGD("leave");
4394 }
4395 
CalculateWidthScale()4396 float DragDrawing::CalculateWidthScale()
4397 {
4398 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
4399 #ifndef OHOS_BUILD_PC_PRODUCT
4400     sptr<Rosen::Display> display = Rosen::DisplayManager::GetInstance().GetDisplayById(g_drawingInfo.displayId);
4401 #else
4402     sptr<Rosen::DisplayInfo> display =
4403         Rosen::DisplayManager::GetInstance().GetVisibleAreaDisplayInfoById(g_drawingInfo.displayId);
4404 #endif // OHOS_BUILD_PC_PRODUCT
4405     if (display == nullptr) {
4406         FI_HILOGD("Get display info failed, display:%{public}d", g_drawingInfo.displayId);
4407 #ifndef OHOS_BUILD_PC_PRODUCT
4408         display = Rosen::DisplayManager::GetInstance().GetDisplayById(0);
4409 #else
4410         display = Rosen::DisplayManager::GetInstance().GetVisibleAreaDisplayInfoById(0);
4411 #endif // OHOS_BUILD_PC_PRODUCT
4412         if (display == nullptr) {
4413             FI_HILOGE("Get display info failed, display is nullptr");
4414             return DEFAULT_SCALING;
4415         }
4416     }
4417     auto defaultDisplay = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
4418     if (defaultDisplay == nullptr) {
4419         FI_HILOGE("defaultDisplay is nullptr");
4420         return DEFAULT_SCALING;
4421     }
4422     int32_t width = display->GetWidth();
4423     int32_t height = display->GetHeight();
4424     float density = defaultDisplay->GetVirtualPixelRatio();
4425 #else
4426     if (window_ == nullptr) {
4427         FI_HILOGE("window_ is nullptr");
4428         return DEFAULT_SCALING;
4429     }
4430     int32_t width = window_->GetRect().width_;
4431     int32_t height = window_->GetRect().height_;
4432     float density = window_->GetDensity();
4433 #endif // OHOS_BUILD_ENABLE_ARKUI_X
4434     int32_t minSide = std::min(width, height);
4435     FI_HILOGD("density:%{public}f, minSide:%{public}d", density, minSide);
4436     if (minSide < MAX_SCREEN_WIDTH_SM * density) {
4437         currentScreenSize_ = ScreenSizeType::SM;
4438     } else if (minSide < MAX_SCREEN_WIDTH_MD * density) {
4439         currentScreenSize_ = ScreenSizeType::MD;
4440     } else if (minSide < MAX_SCREEN_WIDTH_LG * density) {
4441         currentScreenSize_ = ScreenSizeType::LG;
4442     } else {
4443         currentScreenSize_ = ScreenSizeType::XL;
4444     }
4445     float scale = GetMaxWidthScale(width, height);
4446     return scale;
4447 }
4448 
4449 #ifdef OHOS_ENABLE_PULLTHROW
4450 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
CalculatePullThrowScale()4451 float DragDrawing::CalculatePullThrowScale()
4452 {
4453     FI_HILOGI("enter");
4454     auto currentPixelMap = DragDrawing::AccessGlobalPixelMapLocked();
4455     if (currentPixelMap == nullptr) {
4456         FI_HILOGE("pixelMap is nullptr");
4457         return DEFAULT_SCALING;
4458     }
4459     int32_t pixelMapWidth = currentPixelMap->GetWidth();
4460     int32_t pixelMapHeight = currentPixelMap->GetHeight();
4461 
4462     int32_t maxSide = std::max(pixelMapWidth, pixelMapHeight);
4463 
4464     FI_HILOGD("maxSide:%{public}d", maxSide);
4465     float scale = DEFAULT_SCALING;
4466     if (maxSide > APP_WIDTH) {
4467         scale  = APP_WIDTH / maxSide;
4468     }
4469     if (scale < BREATHE_SCALE) {
4470         scale  = BREATHE_SCALE;
4471     }
4472     FI_HILOGD("scale:%{public}f", scale);
4473     return scale;
4474 }
4475 #endif // OHOS_BUILD_ENABLE_ARKUI_X
4476 #endif // OHOS_ENABLE_PULLTHROW
4477 
CalculateSMScale(int32_t pixelMapWidth,int32_t pixelMapHeight,int32_t shortSide)4478 float DragDrawing::CalculateSMScale(int32_t pixelMapWidth, int32_t pixelMapHeight, int32_t shortSide)
4479 {
4480     float scale = 1.0;
4481     if (g_dragDataForSuperHub.summarys.find("plain-text") != g_dragDataForSuperHub.summarys.end()) {
4482         scale = DragDrawing::CalculateScale(pixelMapWidth, pixelMapHeight, static_cast<float>(shortSide),
4483             static_cast<float>(shortSide) / SCALE_TYPE_FIRST);
4484     } else {
4485         scale = DragDrawing::CalculateScale(pixelMapWidth, pixelMapHeight,
4486             static_cast<float>(shortSide) / SCALE_TYPE_FIRST, static_cast<float>(shortSide) / SCALE_TYPE_FIRST);
4487     }
4488     return scale;
4489 }
4490 
CalculateMDScale(int32_t pixelMapWidth,int32_t pixelMapHeight,int32_t shortSide)4491 float DragDrawing::CalculateMDScale(int32_t pixelMapWidth, int32_t pixelMapHeight, int32_t shortSide)
4492 {
4493     float scale = 1.0;
4494     if (g_dragDataForSuperHub.summarys.find("plain-text") != g_dragDataForSuperHub.summarys.end()) {
4495         scale = DragDrawing::CalculateScale(pixelMapWidth, pixelMapHeight,
4496             static_cast<float>(shortSide) / SCALE_TYPE_FIRST, static_cast<float>(shortSide) / SCALE_TYPE_THIRD);
4497     } else {
4498         scale = DragDrawing::CalculateScale(pixelMapWidth, pixelMapHeight,
4499             static_cast<float>(shortSide) / SCALE_TYPE_THIRD, static_cast<float>(shortSide) / SCALE_TYPE_THIRD);
4500     }
4501     return scale;
4502 }
4503 
CalculateDefaultScale(int32_t pixelMapWidth,int32_t pixelMapHeight,int32_t shortSide)4504 float DragDrawing::CalculateDefaultScale(int32_t pixelMapWidth, int32_t pixelMapHeight, int32_t shortSide)
4505 {
4506     float scale = 1.0;
4507     if (g_dragDataForSuperHub.summarys.find("plain-text") != g_dragDataForSuperHub.summarys.end()) {
4508         scale = DragDrawing::CalculateScale(pixelMapWidth, pixelMapHeight,
4509             static_cast<float>(shortSide) * SCALE_TYPE_FIRST / SCALE_TYPE_SECOND,
4510             static_cast<float>(shortSide) / SCALE_TYPE_SECOND);
4511     } else {
4512         scale = DragDrawing::CalculateScale(pixelMapWidth, pixelMapHeight,
4513             static_cast<float>(shortSide) / SCALE_TYPE_SECOND, static_cast<float>(shortSide) / SCALE_TYPE_SECOND);
4514     }
4515     return scale;
4516 }
4517 
GetMaxWidthScale(int32_t width,int32_t height)4518 float DragDrawing::GetMaxWidthScale(int32_t width, int32_t height)
4519 {
4520     FI_HILOGD("current device screen's width is %{public}d", width);
4521     float scale = 1.0;
4522     auto currentPixelMap = DragDrawing::AccessGlobalPixelMapLocked();
4523     if (currentPixelMap == nullptr) {
4524         FI_HILOGE("pixelMap is nullptr");
4525         return DEFAULT_SCALING;
4526     }
4527     int32_t pixelMapWidth = currentPixelMap->GetWidth();
4528     int32_t pixelMapHeight = currentPixelMap->GetHeight();
4529     if (pixelMapWidth == 0 || pixelMapHeight == 0) {
4530         FI_HILOGE("pixelMap width or height is 0");
4531         return DEFAULT_SCALING;
4532     }
4533     int32_t shortSide = fmin(width, height);
4534     FI_HILOGD("currentPixelMap width is %{public}d , height is %{public}d , shortSide is : %{public}d",
4535         width, height, shortSide);
4536     switch (currentScreenSize_) {
4537         case ScreenSizeType::SM: {
4538             scale = CalculateSMScale(pixelMapWidth, pixelMapHeight, shortSide);
4539             break;
4540         }
4541         case ScreenSizeType::MD: {
4542             scale = CalculateMDScale(pixelMapWidth, pixelMapHeight, shortSide);
4543             break;
4544         }
4545         default: {
4546             scale = CalculateDefaultScale(pixelMapWidth, pixelMapHeight, shortSide);
4547             break;
4548         }
4549     }
4550     FI_HILOGD("Screen Size Type is %{public}d and scale is %{public}f",
4551         static_cast<int32_t>(currentScreenSize_), scale);
4552     return scale;
4553 }
4554 
CalculateScale(float width,float height,float widthLimit,float heightLimit)4555 float DragDrawing::CalculateScale(float width, float height, float widthLimit, float heightLimit)
4556 {
4557     float scale = 1.0;
4558     if ((width > 0 && height > 0) && (width > widthLimit || height > heightLimit)) {
4559         scale = fmin(widthLimit / width, heightLimit / height);
4560     }
4561     return scale;
4562 }
4563 
4564 #ifdef OHOS_BUILD_ENABLE_ARKUI_X
SetDragWindow(std::shared_ptr<OHOS::Rosen::Window> window)4565 void DragDrawing::SetDragWindow(std::shared_ptr<OHOS::Rosen::Window> window)
4566 {
4567     CALL_INFO_TRACE;
4568     window_ = window;
4569 }
4570 
AddDragDestroy(std::function<void ()> cb)4571 void DragDrawing::AddDragDestroy(std::function<void()> cb)
4572 {
4573     CALL_INFO_TRACE;
4574     callback_ = cb;
4575 }
4576 
SetSVGFilePath(const std::string & filePath)4577 void DragDrawing::SetSVGFilePath(const std::string &filePath)
4578 {
4579     CALL_INFO_TRACE;
4580     svgFilePath_ = filePath;
4581 }
4582 #endif
4583 
LoadDragDropLib()4584 void DragDrawing::LoadDragDropLib()
4585 {
4586     FI_HILOGI("Begin to open drag drop extension library");
4587     if (dragExtHandler_ == nullptr) {
4588         dragExtHandler_ = dlopen(DRAG_DROP_EXTENSION_SO_PATH.c_str(), RTLD_LAZY);
4589     }
4590     CHKPL(dragExtHandler_);
4591     FI_HILOGI("End to open drag drop extension library");
4592 }
4593 
DetachToDisplay(int32_t displayId)4594 void DragDrawing::DetachToDisplay(int32_t displayId)
4595 {
4596 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
4597     CHKPV(g_drawingInfo.surfaceNode);
4598     g_drawingInfo.surfaceNode->DetachFromWindowContainer(screenId_);
4599     g_drawingInfo.displayId = displayId;
4600     StopVSyncStation();
4601     frameCallback_ = nullptr;
4602     Rosen::RSTransaction::FlushImplicitTransaction();
4603 #endif // OHOS_BUILD_ENABLE_ARKUI_X
4604 }
4605 
UpdateDragState(DragState dragState)4606 void DragDrawing::UpdateDragState(DragState dragState)
4607 {
4608     dragState_ = dragState;
4609 }
4610 
UpdateDragWindowDisplay(int32_t displayId)4611 void DragDrawing::UpdateDragWindowDisplay(int32_t displayId)
4612 {
4613 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
4614     CHKPV(g_drawingInfo.rootNode);
4615     CHKPV(g_drawingInfo.surfaceNode);
4616 #ifndef OHOS_BUILD_PC_PRODUCT
4617     sptr<Rosen::Display> display = Rosen::DisplayManager::GetInstance().GetDisplayById(displayId);
4618 #else
4619     sptr<Rosen::DisplayInfo> display = Rosen::DisplayManager::GetInstance().GetVisibleAreaDisplayInfoById(displayId);
4620 #endif // OHOS_BUILD_PC_PRODUCT
4621     if (display == nullptr) {
4622         FI_HILOGD("Get display info failed, display:%{public}d", displayId);
4623 #ifndef OHOS_BUILD_PC_PRODUCT
4624         display = Rosen::DisplayManager::GetInstance().GetDisplayById(0);
4625 #else
4626         display = Rosen::DisplayManager::GetInstance().GetVisibleAreaDisplayInfoById(0);
4627 #endif // OHOS_BUILD_PC_PRODUCT
4628         if (display == nullptr) {
4629             FI_HILOGE("Get display info failed, display is nullptr");
4630         }
4631         return;
4632     }
4633     screenId_ = display->GetScreenId();
4634     FI_HILOGI("Get screen id:%{public}llu", static_cast<unsigned long long>(screenId_));
4635 #ifdef OHOS_BUILD_PC_PRODUCT
4636     uint64_t rsScreenId = screenId_;
4637     if (!Rosen::DisplayManager::GetInstance().ConvertScreenIdToRsScreenId(screenId_, rsScreenId)) {
4638         FI_HILOGE("ConvertScreenIdToRsScreenId failed");
4639         return;
4640     }
4641     screenId_ = rsScreenId;
4642 #endif // OHOS_BUILD_PC_PRODUCT
4643     displayWidth_ = display->GetWidth();
4644     displayHeight_ = display->GetHeight();
4645     FI_HILOGI("Parameter rsScreen number:%{public}llu", static_cast<unsigned long long>(screenId_));
4646     int32_t surfaceNodeSize = std::max(displayWidth_, displayHeight_);
4647     g_drawingInfo.rootNode->SetBounds(0, 0, surfaceNodeSize, surfaceNodeSize);
4648     g_drawingInfo.rootNode->SetFrame(0, 0, surfaceNodeSize, surfaceNodeSize);
4649     g_drawingInfo.surfaceNode->SetBounds(0, 0, surfaceNodeSize, surfaceNodeSize);
4650     g_drawingInfo.surfaceNode->SetFrameGravity(Rosen::Gravity::RESIZE_ASPECT_FILL);
4651     g_drawingInfo.surfaceNode->AttachToWindowContainer(screenId_);
4652     Rosen::RSTransaction::FlushImplicitTransaction();
4653 #endif // OHOS_BUILD_ENABLE_ARKUI_X
4654 }
4655 } // namespace DeviceStatus
4656 } // namespace Msdp
4657 } // namespace OHOS
4658