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