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