1 /* 2 * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CUSTOM_PAINT_CUSTOM_PAINT_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CUSTOM_PAINT_CUSTOM_PAINT_PATTERN_H 18 19 #include "interfaces/inner_api/ace/ai/image_analyzer.h" 20 21 #include "base/memory/referenced.h" 22 #include "core/components/common/properties/paint_state.h" 23 #include "core/components_ng/image_provider/svg_dom_base.h" 24 #include "core/components_ng/pattern/canvas/canvas_event_hub.h" 25 #include "core/components_ng/pattern/canvas/canvas_layout_algorithm.h" 26 #include "core/components_ng/pattern/pattern.h" 27 #include "core/pipeline_ng/pipeline_context.h" 28 29 namespace OHOS::Ace { 30 class ImageAnalyzerManager; 31 } 32 33 namespace OHOS::Ace::NG { 34 class CanvasPaintMethod; 35 class CanvasModifier; 36 // CanvasPattern is the base class for custom paint render node to perform paint canvas. 37 class ACE_FORCE_EXPORT CanvasPattern : public Pattern { 38 DECLARE_ACE_TYPE(CanvasPattern, Pattern); 39 40 public: 41 CanvasPattern() = default; 42 ~CanvasPattern() override; 43 44 void SetOnContext2DAttach(std::function<void()>&& callback); 45 void SetOnContext2DDetach(std::function<void()>&& callback); 46 IsEnableMatchParent()47 bool IsEnableMatchParent() override 48 { 49 return true; 50 } 51 IsEnableFix()52 bool IsEnableFix() override 53 { 54 return true; 55 } 56 GetId()57 int32_t GetId() const 58 { 59 auto host = GetHost(); 60 CHECK_NULL_RETURN(host, -1); 61 return host->GetId(); 62 } 63 64 void AttachRenderContext(); 65 void DetachRenderContext(); 66 void OnAttachToMainTree() override; 67 GetContextParam()68 std::optional<RenderContext::ContextParam> GetContextParam() const override 69 { 70 return RenderContext::ContextParam { .type = RenderContext::ContextType::INCREMENTAL_CANVAS, 71 .surfaceName = std::nullopt }; 72 } 73 74 RefPtr<NodePaintMethod> CreateNodePaintMethod() override; 75 CreateLayoutAlgorithm()76 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 77 { 78 return MakeRefPtr<CanvasLayoutAlgorithm>(); 79 } 80 CreateEventHub()81 RefPtr<EventHub> CreateEventHub() override 82 { 83 return MakeRefPtr<CanvasEventHub>(); 84 } 85 SetCanvasSize(std::optional<SizeF> canvasSize)86 void SetCanvasSize(std::optional<SizeF> canvasSize) 87 { 88 canvasSize_ = canvasSize; 89 } 90 IsSupportDrawModifier()91 bool IsSupportDrawModifier() const override 92 { 93 return false; 94 } 95 IsAttached()96 bool IsAttached() const 97 { 98 return isAttached_; 99 } 100 101 void SetAntiAlias(bool isEnabled); 102 103 void FillRect(const Rect& rect); 104 void StrokeRect(const Rect& rect); 105 void ClearRect(const Rect& rect); 106 void Fill(); 107 void Fill(const RefPtr<CanvasPath2D>& path); 108 void Stroke(); 109 void Stroke(const RefPtr<CanvasPath2D>& path); 110 void Clip(); 111 void Clip(const RefPtr<CanvasPath2D>& path); 112 void BeginPath(); 113 void ClosePath(); 114 void MoveTo(double x, double y); 115 void LineTo(double x, double y); 116 void Arc(const ArcParam& param); 117 void ArcTo(const ArcToParam& param); 118 void AddRect(const Rect& rect); 119 void AddRoundRect(const Rect& rect, const std::vector<double>& radii); 120 void Ellipse(const EllipseParam& param); 121 void BezierCurveTo(const BezierCurveParam& param); 122 void QuadraticCurveTo(const QuadraticCurveParam& param); 123 124 void FillText(const std::string& text, double x, double y, std::optional<double> maxWidth); 125 void StrokeText(const std::string& text, double x, double y, std::optional<double> maxWidth); 126 TextMetrics MeasureTextMetrics(const std::string& text, const PaintState& state); 127 128 void DrawImage(const Ace::CanvasImage& image, double width, double height); 129 void DrawSvgImage(RefPtr<SvgDomBase> svgDom, const Ace::CanvasImage& image, const ImageFit& imageFit); 130 void DrawPixelMap(RefPtr<PixelMap> pixelMap, const Ace::CanvasImage& image); 131 std::unique_ptr<Ace::ImageData> GetImageData(double left, double top, double width, double height); 132 void GetImageData(const std::shared_ptr<Ace::ImageData>& imageData); 133 void PutImageData(const Ace::ImageData& imageData); 134 #ifdef PIXEL_MAP_SUPPORTED 135 void TransferFromImageBitmap(const RefPtr<PixelMap>& pixelMap); 136 #else 137 void TransferFromImageBitmap(const Ace::ImageData& imageData); 138 #endif 139 void CloseImageBitmap(const std::string& src); 140 141 void UpdateFillColor(const Color& color); 142 void UpdateFillRuleForPath(const CanvasFillRule rule); 143 void UpdateFillRuleForPath2D(const CanvasFillRule rule); 144 double GetWidth(); 145 double GetHeight(); 146 void SetRSCanvasCallback(std::function<void(RSCanvas*, double, double)>& callback); 147 void SetInvalidate(); 148 149 LineDashParam GetLineDash() const; 150 void UpdateLineDash(const std::vector<double>& segments); 151 152 void Save(); 153 void Restore(); 154 void Scale(double x, double y); 155 void Rotate(double angle); 156 void SetTransform(const TransformParam& param); 157 void ResetTransform(); 158 void Transform(const TransformParam& param); 159 void Translate(double x, double y); 160 std::string ToDataURL(const std::string& type, double quality); 161 std::string GetJsonData(const std::string& path); 162 163 void UpdateGlobalAlpha(double alpha); 164 void UpdateCompositeOperation(CompositeOperation type); 165 void UpdateSmoothingEnabled(bool enabled); 166 void UpdateSmoothingQuality(const std::string& quality); 167 void UpdateLineCap(LineCapStyle cap); 168 void UpdateLineDashOffset(double dash); 169 void UpdateLineWidth(double width); 170 void UpdateMiterLimit(double limit); 171 void UpdateShadowBlur(double blur); 172 void UpdateShadowOffsetX(double offsetX); 173 void UpdateShadowOffsetY(double offsetY); 174 void UpdateTextAlign(TextAlign align); 175 void UpdateTextBaseline(TextBaseline baseline); 176 void UpdateStrokePattern(const std::weak_ptr<Ace::Pattern>& pattern); 177 void UpdateStrokeColor(const Color& color); 178 void UpdateFontWeight(FontWeight weight); 179 void UpdateFontStyle(FontStyle style); 180 void UpdateFontFamilies(const std::vector<std::string>& families); 181 void UpdateFontSize(const Dimension& size); 182 void UpdateLetterSpacing(const Dimension& letterSpacing); 183 void UpdateLineJoin(LineJoinStyle join); 184 void SetFillGradient(const std::shared_ptr<Ace::Gradient>& gradient); 185 void UpdateFillPattern(const std::weak_ptr<Ace::Pattern>& pattern); 186 void UpdateShadowColor(const Color& color); 187 void SetStrokeGradient(const std::shared_ptr<Ace::Gradient>& gradient); 188 void SetTextDirection(TextDirection direction); 189 void SetFilterParam(const std::string& filterStr); 190 TransformParam GetTransform() const; 191 void SetDensity(double density); 192 void SetTransform(std::shared_ptr<Ace::Pattern> pattern, const TransformParam& transform); 193 int32_t GetId(); 194 195 void SaveLayer(); 196 void RestoreLayer(); 197 void EnableAnalyzer(bool enable); 198 void SetImageAIOptions(void* options); 199 void StartImageAnalyzer(void* config, OnAnalyzedCallback& onAnalyzed); 200 void StopImageAnalyzer(); 201 void Reset(); 202 void DumpInfo() override; 203 void DumpInfo(std::unique_ptr<JsonValue>& json) override; 204 void DumpSimplifyInfo(std::shared_ptr<JsonValue>& json) override; 205 206 private: 207 void OnAttachToFrameNode() override; 208 void OnDetachFromFrameNode(FrameNode* frameNode) override; 209 void FireOnContext2DAttach(); 210 void FireOnContext2DDetach(); 211 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override; 212 void OnSizeChanged(const DirtySwapConfig& config, bool needReset); 213 void CreateAnalyzerOverlay(); 214 void DestroyAnalyzerOverlay(); 215 void UpdateAnalyzerOverlay(); 216 void ReleaseImageAnalyzer(); 217 bool IsSupportImageAnalyzerFeature(); 218 void OnLanguageConfigurationUpdate() override; 219 void OnModifyDone() override; 220 void UpdateTextDefaultDirection(); 221 222 std::function<void()> onContext2DAttach_; 223 std::function<void()> onContext2DDetach_; 224 RefPtr<CanvasPaintMethod> paintMethod_; 225 std::optional<SizeF> canvasSize_; 226 SizeF dirtyPixelGridRoundSize_ = { -1, -1 }; 227 SizeF lastDirtyPixelGridRoundSize_ = { -1, -1 }; 228 DirtySwapConfig recordConfig_; 229 std::shared_ptr<ImageAnalyzerManager> imageAnalyzerManager_; 230 bool isEnableAnalyzer_ = false; 231 TextDirection currentSetTextDirection_ = TextDirection::INHERIT; 232 RefPtr<CanvasModifier> contentModifier_; 233 bool isAttached_ = false; 234 int32_t id_ = -1; 235 ACE_DISALLOW_COPY_AND_MOVE(CanvasPattern); 236 }; 237 } // namespace OHOS::Ace::NG 238 239 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CUSTOM_PAINT_CUSTOM_PAINT_PATTERN_H 240