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