1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef DRAG_DRAWING_H 17 #define DRAG_DRAWING_H 18 19 #include <vector> 20 21 #include "event_handler.h" 22 #include "event_runner.h" 23 #include "libxml/tree.h" 24 #include "libxml/parser.h" 25 #include "modifier/rs_extended_modifier.h" 26 #include "modifier/rs_modifier.h" 27 #include "vsync_receiver.h" 28 29 #include "drag_data.h" 30 #include "i_drag_animation.h" 31 32 namespace OHOS { 33 namespace Msdp { 34 namespace DeviceStatus { 35 struct DrawingInfo; 36 class DragDrawing; 37 using DragExtFunc = void (*)(DragDrawing *drawingObj, DrawingInfo *drawingInfo); 38 class DrawSVGModifier : public Rosen::RSContentStyleModifier { 39 public: DrawSVGModifier(std::shared_ptr<Media::PixelMap> stylePixelMap)40 explicit DrawSVGModifier(std::shared_ptr<Media::PixelMap> stylePixelMap) : stylePixelMap_(stylePixelMap) {} 41 ~DrawSVGModifier() = default; 42 void Draw(Rosen::RSDrawingContext& context) const override; 43 44 private: 45 std::shared_ptr<Media::PixelMap> stylePixelMap_ { nullptr }; 46 }; 47 48 class DrawPixelMapModifier : public Rosen::RSContentStyleModifier { 49 public: 50 DrawPixelMapModifier() = default; 51 ~DrawPixelMapModifier() = default; 52 void Draw(Rosen::RSDrawingContext &context) const override; 53 }; 54 55 class DrawMouseIconModifier : public Rosen::RSContentStyleModifier { 56 public: 57 DrawMouseIconModifier() = default; 58 ~DrawMouseIconModifier() = default; 59 void Draw(Rosen::RSDrawingContext &context) const override; 60 }; 61 62 class DrawDynamicEffectModifier : public Rosen::RSContentStyleModifier { 63 public: 64 DrawDynamicEffectModifier() = default; 65 ~DrawDynamicEffectModifier() = default; 66 void Draw(Rosen::RSDrawingContext &context) const override; 67 void SetAlpha(float alpha); 68 void SetScale(float scale); 69 70 private: 71 std::shared_ptr<Rosen::RSAnimatableProperty<float>> alpha_ { nullptr }; 72 std::shared_ptr<Rosen::RSAnimatableProperty<float>> scale_ { nullptr }; 73 }; 74 75 struct DrawingInfo { 76 std::atomic_bool isRunning { false }; 77 std::atomic_bool isPreviousDefaultStyle { false }; 78 std::atomic_bool isCurrentDefaultStyle { false }; 79 bool isInitUiDirector { true }; 80 int32_t sourceType { -1 }; 81 int32_t currentDragNum { -1 }; 82 DragCursorStyle currentStyle { DragCursorStyle::DEFAULT }; 83 int32_t displayId { -1 }; 84 int32_t pixelMapX { -1 }; 85 int32_t pixelMapY { -1 }; 86 int32_t displayX { -1 }; 87 int32_t displayY { -1 }; 88 int32_t rootNodeWidth { -1 }; 89 int32_t rootNodeHeight { -1 }; 90 std::vector<std::shared_ptr<Rosen::RSCanvasNode>> nodes; 91 std::shared_ptr<Rosen::RSNode> rootNode { nullptr }; 92 std::shared_ptr<Rosen::RSSurfaceNode> surfaceNode { nullptr }; 93 std::shared_ptr<Media::PixelMap> pixelMap { nullptr }; 94 std::shared_ptr<Media::PixelMap> stylePixelMap { nullptr }; 95 }; 96 97 class DragDrawing : public IDragAnimation { 98 public: 99 DragDrawing() = default; 100 DISALLOW_COPY_AND_MOVE(DragDrawing); 101 ~DragDrawing(); 102 103 int32_t Init(const DragData &dragData); 104 void Draw(int32_t displayId, int32_t displayX, int32_t displayY); 105 int32_t UpdateDragStyle(DragCursorStyle style); 106 int32_t UpdateShadowPic(const ShadowInfo &shadowInfo); 107 int32_t StartVsync(); 108 void OnDragSuccess(); 109 void OnDragFail(); 110 void EraseMouseIcon(); 111 void DestroyDragWindow(); 112 void UpdateDrawingState(); 113 void UpdateDragWindowState(bool visible); 114 void OnStartDrag(const DragAnimationData &dragAnimationData, std::shared_ptr<Rosen::RSCanvasNode> shadowNode, 115 std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode) override; 116 void OnDragStyle(std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode, 117 std::shared_ptr<Media::PixelMap> stylePixelMap) override; 118 void OnStopDragSuccess(std::shared_ptr<Rosen::RSCanvasNode> shadowNode, 119 std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode) override; 120 void OnStopDragFail(std::shared_ptr<Rosen::RSSurfaceNode> surfaceNode, 121 std::shared_ptr<Rosen::RSNode> rootNode) override; 122 void OnStopAnimation() override; 123 124 private: 125 int32_t InitLayer(); 126 void InitCanvas(int32_t width, int32_t height); 127 void CreateWindow(int32_t displayX, int32_t displayY); 128 int32_t InitDrawStyle(std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode); 129 int32_t DrawShadow(std::shared_ptr<Rosen::RSCanvasNode> shadowNode); 130 int32_t DrawMouseIcon(); 131 int32_t DrawStyle(std::shared_ptr<Rosen::RSCanvasNode> dragStyleNode, 132 std::shared_ptr<Media::PixelMap> stylePixelMap); 133 void RunAnimation(float endAlpha, float endScale); 134 int32_t InitVSync(float endAlpha, float endScale); 135 void OnVsync(); 136 void InitDrawingInfo(const DragData &dragData); 137 int32_t InitDragAnimationData(DragAnimationData &dragAnimationData); 138 void RemoveModifier(); 139 int32_t UpdateSvgNodeInfo(xmlNodePtr curNode, int32_t extendSvgWidth); 140 xmlNodePtr GetRectNode(xmlNodePtr curNode); 141 xmlNodePtr UpdateRectNode(int32_t extendSvgWidth, xmlNodePtr curNode); 142 void UpdateTspanNode(xmlNodePtr curNode); 143 int32_t ParseAndAdjustSvgInfo(xmlNodePtr curNode); 144 std::shared_ptr<Media::PixelMap> DecodeSvgToPixelMap(const std::string &filePath); 145 int32_t GetFilePath(std::string &filePath); 146 bool NeedAdjustSvgInfo(); 147 void SetDecodeOptions(Media::DecodeOptions &decodeOpts); 148 149 private: 150 int64_t startNum_ { -1 }; 151 std::shared_ptr<Rosen::RSCanvasNode> canvasNode_ { nullptr }; 152 std::shared_ptr<DrawSVGModifier> drawSVGModifier_ { nullptr }; 153 std::shared_ptr<DrawPixelMapModifier> drawPixelMapModifier_ { nullptr }; 154 std::shared_ptr<DrawMouseIconModifier> drawMouseIconModifier_ { nullptr }; 155 std::shared_ptr<DrawDynamicEffectModifier> drawDynamicEffectModifier_ { nullptr }; 156 std::shared_ptr<Rosen::RSUIDirector> rsUiDirector_ { nullptr }; 157 std::shared_ptr<Rosen::VSyncReceiver> receiver_ { nullptr }; 158 std::shared_ptr<AppExecFwk::EventHandler> handler_ { nullptr }; 159 void* dragExtHandle_ = nullptr; 160 }; 161 } // namespace DeviceStatus 162 } // namespace Msdp 163 } // namespace OHOS 164 #endif // DRAG_DRAWING_H