• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_CUSTOM_PAINT_FLUTTER_RENDER_OFFSCREEN_CANVAS_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CUSTOM_PAINT_FLUTTER_RENDER_OFFSCREEN_CANVAS_H
18 
19 #include "flutter/fml/memory/ref_ptr.h"
20 #include "flutter/lib/ui/painting/path.h"
21 #include "flutter/third_party/txt/src/txt/paragraph.h"
22 #include "third_party/skia/include/core/SkCanvas.h"
23 #include "third_party/skia/include/core/SkPath.h"
24 
25 #include "core/components/custom_paint/offscreen_canvas.h"
26 #include "core/components/custom_paint/render_custom_paint.h"
27 #include "core/image/image_provider.h"
28 #include "core/pipeline/base/scoped_canvas_state.h"
29 #include "core/pipeline/layers/clip_layer.h"
30 #include "frameworks/bridge/js_frontend/engine/quickjs/offscreen_canvas_bridge.h"
31 
32 namespace OHOS::Ace {
33 using setColorFunc = std::function<void (const std::string&)>;
34 class FlutterRenderOffscreenCanvas : public RenderOffscreenCanvas {
35     DECLARE_ACE_TYPE(FlutterRenderOffscreenCanvas, RenderOffscreenCanvas);
36 
37 public:
38     FlutterRenderOffscreenCanvas(const WeakPtr<PipelineBase>& context, int32_t width, int32_t height);
39     ~FlutterRenderOffscreenCanvas() override = default;
40     void SetAntiAlias(bool isEnabled) override;
41     void FillRect(Rect rect) override;
42     void ClearRect(Rect rect) override;
43     void StrokeRect(Rect rect) override;
44     void FillText(const std::string& text, double x, double y, const PaintState& state) override;
45     void StrokeText(const std::string& text, double x, double y, const PaintState& state) override;
46     double MeasureText(const std::string& text, const PaintState& state) override;
47     double MeasureTextHeight(const std::string& text, const PaintState& state) override;
48     TextMetrics MeasureTextMetrics(const std::string& text, const PaintState& state) override;
49     std::string ToDataURL(const std::string& type, const double quality) override;
50     std::unique_ptr<ImageData> GetImageData(double left, double top, double width, double height) override;
51     void BeginPath() override;
52     void Arc(const ArcParam& param) override;
53     void Stroke() override;
54     void Stroke(const RefPtr<CanvasPath2D>& path) override;
55     void ArcTo(const ArcToParam& param) override;
56     void MoveTo(double x, double y) override;
57     void ClosePath() override;
58     void Rotate(double angle) override;
59     void Scale(double x, double y) override;
60     void AddRect(const Rect& rect) override;
61     void Fill() override;
62     void Fill(const RefPtr<CanvasPath2D>& path) override;
63     void Clip() override;
64     void Clip(const RefPtr<CanvasPath2D>& path) override;
65     void PutImageData(const ImageData& imageData) override;
66     void DrawImage(const CanvasImage& canvasImage, double width, double height) override;
67     void DrawPixelMap(RefPtr<PixelMap> pixelMap, const CanvasImage& image) override;
68     void LineTo(double x, double y) override;
69     void BezierCurveTo(const BezierCurveParam& param) override;
70     void QuadraticCurveTo(const QuadraticCurveParam& param) override;
71     void Ellipse(const EllipseParam& param) override;
72     void SetTransform(const TransformParam& param) override;
73     void Transform(const TransformParam& param) override;
74     void Translate(double x, double y) override;
75     void Restore() override;
76     void Save() override;
77     bool IsPointInStroke(double x, double y) override;
78     bool IsPointInStroke(const RefPtr<CanvasPath2D>& path, double x, double y) override;
79     bool IsPointInPath(double x, double y) override;
80     bool IsPointInPath(const RefPtr<CanvasPath2D>& path, double x, double y) override;
81     void ResetTransform() override;
82     void SetFillRuleForPath(const CanvasFillRule& rule) override;
83     void SetFillRuleForPath2D(const CanvasFillRule& rule) override;
84 private:
85     void InitImagePaint();
86     void InitCachePaint();
87     bool antiAlias_ = true;
88     SkPaint GetStrokePaint();
89     WeakPtr<PipelineBase> pipelineContext_;
90     SkBitmap skBitmap_;
91     SkPath skPath_;
92     SkPath skPath2d_;
93     SkPaint imagePaint_;
94     SkPaint cachePaint_;
95     SkBitmap cacheBitmap_;
96     std::unique_ptr<SkCanvas> cacheCanvas_;
97     std::unique_ptr<SkCanvas> skCanvas_;
98     std::map<std::string, setColorFunc> filterFunc_;
99     RefPtr<FlutterRenderTaskHolder> renderTaskHolder_;
100     void UpdatePaintShader(SkPaint& paint, const Gradient& gradient);
101     void UpdatePaintShader(const Pattern& pattern, SkPaint& paint);
102     void PaintText(const std::string& text, double x, double y, bool isStroke, bool hasShadow = false);
103     double GetBaselineOffset(TextBaseline baseline, std::unique_ptr<txt::Paragraph>& paragraph);
104     std::unique_ptr<txt::Paragraph> paragraph_;
105     bool HasShadow() const;
106     bool HasImageShadow() const;
107     void UpdateTextStyleForeground(bool isStroke, txt::TextStyle& style, bool hasShadow);
108     double GetAlignOffset(const std::string& text, TextAlign align, std::unique_ptr<txt::Paragraph>& paragraph);
109     TextDirection GetTextDirection(const std::string& text);
110     bool UpdateOffParagraph(const std::string& text, bool isStroke, const PaintState& state, bool hasShadow = false);
111     void UpdateLineDash(SkPaint& paint);
112     void Path2DAddPath(const PathArgs& args);
113     void Path2DSetTransform(const PathArgs& args);
114     void Path2DMoveTo(const PathArgs& args);
115     void Path2DLineTo(const PathArgs& args);
116     void Path2DArc(const PathArgs& args);
117     void Path2DArcTo(const PathArgs& args);
118     void Path2DQuadraticCurveTo(const PathArgs& args);
119     void Path2DBezierCurveTo(const PathArgs& args);
120     void Path2DEllipse(const PathArgs& args);
121     void Path2DRect(const PathArgs& args);
122     void Path2DClosePath(const PathArgs& args);
123     void Path2DStroke();
124     void Path2DFill();
125     void Path2DClip();
126     void ParsePath2D(const RefPtr<CanvasPath2D>& path);
127     void TranspareCmdToPath(const RefPtr<CanvasPath2D>& path);
128     bool IsPointInPathByColor(double x, double y, SkPath& path, SkColor colorMatch);
129     void SetPaintImage();
130     void InitFilterFunc();
131     bool GetFilterType(std::string& filterType, std::string& filterParam);
132     void SetGrayFilter(const std::string& percent);
133     void SetSepiaFilter(const std::string& percent);
134     void SetInvertFilter(const std::string& percent);
135     void SetOpacityFilter(const std::string& percent);
136     void SetBrightnessFilter(const std::string& percent);
137     void SetContrastFilter(const std::string& percent);
138     void SetBlurFilter(const std::string& percent);
139     void SetDropShadowFilter(const std::string& percent);
140     void SetSaturateFilter(const std::string& percent);
141     void SetHueRotateFilter(const std::string& percent);
142     double PxStrToDouble(const std::string& str);
143     double BlurStrToDouble(const std::string& str);
144     bool IsPercentStr(std::string& percentStr);
145     void SetColorFilter(float matrix[20]);
146     void SetRenderTaskHolder(const RefPtr<FlutterRenderTaskHolder> renderTaskHolder);
147 };
148 } // namespace OHOS::Ace
149 
150 #endif
151