1 /* 2 * Copyright (c) 2022 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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CUSTOM_PAINT_CANVAS_PAINT_METHOD_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CUSTOM_PAINT_CANVAS_PAINT_METHOD_H 18 19 #include <memory> 20 21 #include "base/memory/referenced.h" 22 #include "base/utils/utils.h" 23 #include "core/components_ng/pattern/canvas/canvas_paint_op.h" 24 #include "core/components_ng/pattern/canvas/custom_paint_paint_method.h" 25 #include "core/components_ng/pattern/canvas/offscreen_canvas_pattern.h" 26 27 namespace OHOS::Ace::NG { 28 class CanvasPaintMethod; 29 using TaskFunc = std::function<void(CanvasPaintMethod&)>; 30 using OnModifierUpdateFunc = std::function<void(void)>; 31 class CanvasPaintMethod : public CustomPaintPaintMethod { 32 DECLARE_ACE_TYPE(CanvasPaintMethod, CustomPaintPaintMethod) 33 public: 34 CanvasPaintMethod() = default; 35 CanvasPaintMethod(RefPtr<CanvasModifier> contentModifier, const RefPtr<FrameNode>& frameNode); 36 ~CanvasPaintMethod() override = default; 37 38 void GetFastTaskPool(); 39 void UpdateContentModifier(PaintWrapper* paintWrapper) override; 40 void UpdateRecordingCanvas(float width, float height); 41 42 #ifndef USE_FAST_TASKPOOL 43 void PushTask(const TaskFunc& task); 44 #else 45 template <typename T, typename... Args> PushTask(Args &&...args)46 void PushTask(Args&&... args) 47 { 48 CHECK_NULL_VOID(fastTaskPool_); 49 fastTaskPool_->Push<T>(0, std::forward<Args>(args)...); 50 if (needMarkDirty_) { 51 needMarkDirty_ = false; 52 auto host = frameNode_.Upgrade(); 53 CHECK_NULL_VOID(host); 54 host->MarkDirtyNode(PROPERTY_UPDATE_RENDER); 55 } 56 } 57 #endif 58 bool HasTask() const; 59 void FlushTask(); 60 FlushUITasks()61 void FlushUITasks() 62 { 63 CHECK_EQUAL_VOID(HasTask(), false); 64 auto context = context_.Upgrade(); 65 if (context) { 66 context->FlushUITasks(); 67 } 68 } 69 GetWidth()70 double GetWidth() 71 { 72 return lastLayoutSize_.Width(); 73 } 74 GetHeight()75 double GetHeight() 76 { 77 return lastLayoutSize_.Height(); 78 } 79 SetOnModifierUpdateFunc(OnModifierUpdateFunc && func)80 void SetOnModifierUpdateFunc(OnModifierUpdateFunc&& func) 81 { 82 onModifierUpdate_ = std::move(func); 83 } 84 FireOnModifierUpdateFunc()85 void FireOnModifierUpdateFunc() 86 { 87 CHECK_NULL_VOID(onModifierUpdate_); 88 onModifierUpdate_(); 89 } 90 SetRSCanvasCallback(std::function<void (RSCanvas *,double,double)> & callback)91 void SetRSCanvasCallback(std::function<void(RSCanvas*, double, double)>& callback) 92 { 93 canvasCallback_ = callback; 94 } FireRSCanvasCallback(double width,double height)95 void FireRSCanvasCallback(double width, double height) 96 { 97 CHECK_NULL_VOID(canvasCallback_); 98 canvasCallback_(rsCanvas_.get(), width, height); 99 } 100 101 void CloseImageBitmap(const std::string& src); 102 void DrawPixelMap(RefPtr<PixelMap> pixelMap, const Ace::CanvasImage& canvasImage); 103 void DrawPixelMapInternal(RefPtr<PixelMap> pixelMap, const Ace::CanvasImage& canvasImage); 104 std::unique_ptr<Ace::ImageData> GetImageData(double left, double top, double width, double height); 105 void GetImageData(const std::shared_ptr<Ace::ImageData>& imageData); 106 #ifdef PIXEL_MAP_SUPPORTED 107 void TransferFromImageBitmap(const RefPtr<PixelMap>& pixelMap); 108 #endif 109 std::string ToDataURL(const std::string& type, const double quality); 110 bool DrawBitmap(RefPtr<RenderContext> renderContext, RSBitmap& currentBitmap); 111 std::string GetJsonData(const std::string& path); 112 113 void Reset(); 114 std::string GetDumpInfo(); 115 void SetHostCustomNodeName(); 116 void GetSimplifyDumpInfo(std::unique_ptr<JsonValue>& json); 117 private: 118 #ifndef ACE_UNITTEST 119 void ConvertTxtStyle(const TextStyle& textStyle, Rosen::TextStyle& txtStyle) override; 120 #endif 121 #ifndef USE_FAST_TASKPOOL 122 std::list<TaskFunc> tasks_; 123 #else 124 friend class CanvasPattern; 125 std::unique_ptr<CanvasPaintOp> fastTaskPool_ = std::make_unique<CanvasPaintOp>(); 126 #endif 127 128 #ifndef ACE_UNITTEST 129 RefPtr<Ace::ImageObject> imageObj_ = nullptr; 130 #endif 131 OnModifierUpdateFunc onModifierUpdate_; 132 std::function<void(RSCanvas*, double, double)> canvasCallback_ = nullptr; 133 WeakPtr<FrameNode> frameNode_; 134 bool needMarkDirty_ = true; 135 // To record the host custom component name of the current canvas. 136 std::string customNodeName_; 137 138 ACE_DISALLOW_COPY_AND_MOVE(CanvasPaintMethod); 139 }; 140 } // namespace OHOS::Ace::NG 141 142 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CUSTOM_PAINT_CANVAS_PAINT_METHOD_H 143