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 OffscreenCanvasPattern;
36 class CanvasModifier;
37 // CanvasPattern is the base class for custom paint render node to perform paint canvas.
38 class ACE_EXPORT CanvasPattern : public Pattern {
39     DECLARE_ACE_TYPE(CanvasPattern, Pattern);
40 
41 public:
42     CanvasPattern() = default;
43     ~CanvasPattern() override;
44 
45     void SetOnContext2DAttach(std::function<void()>&& callback);
46     void SetOnContext2DDetach(std::function<void()>&& callback);
47 
GetId()48     int32_t GetId() const
49     {
50         auto host = GetHost();
51         CHECK_NULL_RETURN(host, -1);
52         return host->GetId();
53     }
54 
55     void AttachRenderContext();
56     void DetachRenderContext();
57     void OnAttachToMainTree() override;
58 
GetContextParam()59     std::optional<RenderContext::ContextParam> GetContextParam() const override
60     {
61         return RenderContext::ContextParam { RenderContext::ContextType::INCREMENTAL_CANVAS };
62     }
63 
64     RefPtr<NodePaintMethod> CreateNodePaintMethod() override;
65 
CreateLayoutAlgorithm()66     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
67     {
68         return MakeRefPtr<CanvasLayoutAlgorithm>();
69     }
70 
CreateEventHub()71     RefPtr<EventHub> CreateEventHub() override
72     {
73         return MakeRefPtr<CanvasEventHub>();
74     }
75 
SetCanvasSize(std::optional<SizeF> canvasSize)76     void SetCanvasSize(std::optional<SizeF> canvasSize)
77     {
78         canvasSize_ = canvasSize;
79     }
80 
IsSupportDrawModifier()81     bool IsSupportDrawModifier() const override
82     {
83         return false;
84     }
85 
IsAttached()86     bool IsAttached() const
87     {
88         return isAttached_;
89     }
90 
91     void SetAntiAlias(bool isEnabled);
92 
93     void FillRect(const Rect& rect);
94     void StrokeRect(const Rect& rect);
95     void ClearRect(const Rect& rect);
96     void Fill();
97     void Fill(const RefPtr<CanvasPath2D>& path);
98     void Stroke();
99     void Stroke(const RefPtr<CanvasPath2D>& path);
100     void Clip();
101     void Clip(const RefPtr<CanvasPath2D>& path);
102     void BeginPath();
103     void ClosePath();
104     void MoveTo(double x, double y);
105     void LineTo(double x, double y);
106     void Arc(const ArcParam& param);
107     void ArcTo(const ArcToParam& param);
108     void AddRect(const Rect& rect);
109     void Ellipse(const EllipseParam& param);
110     void BezierCurveTo(const BezierCurveParam& param);
111     void QuadraticCurveTo(const QuadraticCurveParam& param);
112 
113     void FillText(const std::string& text, double x, double y, std::optional<double> maxWidth);
114     void StrokeText(const std::string& text, double x, double y, std::optional<double> maxWidth);
115     TextMetrics MeasureTextMetrics(const std::string& text, const PaintState& state);
116 
117     void DrawImage(const Ace::CanvasImage& image, double width, double height);
118     void DrawSvgImage(RefPtr<SvgDomBase> svgDom, const Ace::CanvasImage& image, const ImageFit& imageFit);
119     void DrawPixelMap(RefPtr<PixelMap> pixelMap, const Ace::CanvasImage& image);
120     std::unique_ptr<Ace::ImageData> GetImageData(double left, double top, double width, double height);
121     void GetImageData(const std::shared_ptr<Ace::ImageData>& imageData);
122     void PutImageData(const Ace::ImageData& imageData);
123 #ifdef PIXEL_MAP_SUPPORTED
124     void TransferFromImageBitmap(const RefPtr<PixelMap>& pixelMap);
125 #else
126     void TransferFromImageBitmap(const Ace::ImageData& imageData);
127 #endif
128     void CloseImageBitmap(const std::string& src);
129 
130     void UpdateFillColor(const Color& color);
131     void UpdateFillRuleForPath(const CanvasFillRule rule);
132     void UpdateFillRuleForPath2D(const CanvasFillRule rule);
133     double GetWidth();
134     double GetHeight();
135     void SetRSCanvasCallback(std::function<void(RSCanvas*, double, double)>& callback);
136     void SetInvalidate();
137 
138     LineDashParam GetLineDash() const;
139     void UpdateLineDash(const std::vector<double>& segments);
140 
141     void Save();
142     void Restore();
143     void Scale(double x, double y);
144     void Rotate(double angle);
145     void SetTransform(const TransformParam& param);
146     void ResetTransform();
147     void Transform(const TransformParam& param);
148     void Translate(double x, double y);
149     std::string ToDataURL(const std::string& type, double quality);
150     std::string GetJsonData(const std::string& path);
151 
152     void UpdateGlobalAlpha(double alpha);
153     void UpdateCompositeOperation(CompositeOperation type);
154     void UpdateSmoothingEnabled(bool enabled);
155     void UpdateSmoothingQuality(const std::string& quality);
156     void UpdateLineCap(LineCapStyle cap);
157     void UpdateLineDashOffset(double dash);
158     void UpdateLineWidth(double width);
159     void UpdateMiterLimit(double limit);
160     void UpdateShadowBlur(double blur);
161     void UpdateShadowOffsetX(double offsetX);
162     void UpdateShadowOffsetY(double offsetY);
163     void UpdateTextAlign(TextAlign align);
164     void UpdateTextBaseline(TextBaseline baseline);
165     void UpdateStrokePattern(const std::weak_ptr<Ace::Pattern>& pattern);
166     void UpdateStrokeColor(const Color& color);
167     void UpdateFontWeight(FontWeight weight);
168     void UpdateFontStyle(FontStyle style);
169     void UpdateFontFamilies(const std::vector<std::string>& families);
170     void UpdateFontSize(const Dimension& size);
171     void UpdateLineJoin(LineJoinStyle join);
172     void SetFillGradient(const std::shared_ptr<Ace::Gradient>& gradient);
173     void UpdateFillPattern(const std::weak_ptr<Ace::Pattern>& pattern);
174     void UpdateShadowColor(const Color& color);
175     void SetStrokeGradient(const std::shared_ptr<Ace::Gradient>& gradient);
176     void SetTextDirection(TextDirection direction);
177     void SetFilterParam(const std::string& filterStr);
178     TransformParam GetTransform() const;
179     void SetDensity(double density);
180     int32_t GetId();
181 
182     void SaveLayer();
183     void RestoreLayer();
184     void EnableAnalyzer(bool enable);
185     void SetImageAIOptions(void* options);
186     void StartImageAnalyzer(void* config, OnAnalyzedCallback& onAnalyzed);
187     void StopImageAnalyzer();
188     void Reset();
189     void DumpInfo() override;
190     void DumpSimplifyInfo(std::unique_ptr<JsonValue>& json) override;
191 
192 private:
193     void OnAttachToFrameNode() override;
194     void OnDetachFromFrameNode(FrameNode* frameNode) override;
195     void FireOnContext2DAttach();
196     void FireOnContext2DDetach();
197     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
198     void OnSizeChanged(const DirtySwapConfig& config, bool needReset);
199     void CreateAnalyzerOverlay();
200     void DestroyAnalyzerOverlay();
201     void UpdateAnalyzerOverlay();
202     void ReleaseImageAnalyzer();
203     bool IsSupportImageAnalyzerFeature();
204     void OnLanguageConfigurationUpdate() override;
205     void OnModifyDone() override;
206     void UpdateTextDefaultDirection();
207 
208     std::function<void()> onContext2DAttach_;
209     std::function<void()> onContext2DDetach_;
210     RefPtr<CanvasPaintMethod> paintMethod_;
211     std::optional<SizeF> canvasSize_;
212     SizeF dirtyPixelGridRoundSize_ = { -1, -1 };
213     SizeF lastDirtyPixelGridRoundSize_ = { -1, -1 };
214     DirtySwapConfig recordConfig_;
215     std::shared_ptr<ImageAnalyzerManager> imageAnalyzerManager_;
216     bool isEnableAnalyzer_ = false;
217     TextDirection currentSetTextDirection_ = TextDirection::INHERIT;
218     RefPtr<CanvasModifier> contentModifier_;
219     bool isAttached_ = false;
220     ACE_DISALLOW_COPY_AND_MOVE(CanvasPattern);
221 };
222 } // namespace OHOS::Ace::NG
223 
224 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CUSTOM_PAINT_CUSTOM_PAINT_PATTERN_H
225