• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 #ifdef NEW_SKIA
20 #include "modules/svg/include/SkSVGDOM.h"
21 #else
22 #include "experimental/svg/model/SkSVGDOM.h"
23 #endif
24 #include "txt/paragraph.h"
25 #ifndef USE_ROSEN_DRAWING
26 #include "include/core/SkCanvas.h"
27 #include "include/core/SkPath.h"
28 #endif
29 
30 #include "core/components/custom_paint/offscreen_canvas.h"
31 #include "core/components/custom_paint/render_custom_paint.h"
32 #include "core/image/image_source_info.h"
33 #include "core/image/image_object.h"
34 #include "core/image/image_provider.h"
35 #ifndef NEW_SKIA
36 #include "core/pipeline/base/scoped_canvas_state.h"
37 #endif
38 
39 namespace OHOS::Ace {
40 using setColorFunc = std::function<void (const std::string&)>;
41 class RosenRenderOffscreenCanvas : public RenderOffscreenCanvas {
42     DECLARE_ACE_TYPE(RosenRenderOffscreenCanvas, RenderOffscreenCanvas);
43 
44 public:
45     RosenRenderOffscreenCanvas(const WeakPtr<PipelineBase>& context, int32_t width, int32_t height);
46     ~RosenRenderOffscreenCanvas() override = default;
47     void SetAntiAlias(bool isEnabled) override;
48     void FillRect(Rect rect) override;
49     void ClearRect(Rect rect) override;
50     void StrokeRect(Rect rect) override;
51     void FillText(const std::string& text, double x, double y, const PaintState& state) override;
52     void StrokeText(const std::string& text, double x, double y, const PaintState& state) override;
53     double MeasureText(const std::string& text, const PaintState& state) override;
54     double MeasureTextHeight(const std::string& text, const PaintState& state) override;
55     TextMetrics MeasureTextMetrics(const std::string& text, const PaintState& state) override;
56     std::string ToDataURL(const std::string& type, const double quality) override;
57     std::unique_ptr<ImageData> GetImageData(double left, double top, double width, double height) override;
58     void BeginPath() override;
59     void Arc(const ArcParam& param) override;
60     void Stroke() override;
61     void Stroke(const RefPtr<CanvasPath2D>& path) override;
62     void ArcTo(const ArcToParam& param) override;
63     void MoveTo(double x, double y) override;
64     void ClosePath() override;
65     void Rotate(double angle) override;
66     void Scale(double x, double y) override;
67     void AddRect(const Rect& rect) override;
68     void Fill() override;
69     void Fill(const RefPtr<CanvasPath2D>& path) override;
70     void Clip() override;
71     void Clip(const RefPtr<CanvasPath2D>& path) override;
72     void PutImageData(const ImageData& imageData) override;
73     void DrawImage(const CanvasImage& canvasImage, double width, double height) override;
74     void DrawPixelMap(RefPtr<PixelMap> pixelMap, const CanvasImage& image) override;
75     void LineTo(double x, double y) override;
76     void BezierCurveTo(const BezierCurveParam& param) override;
77     void QuadraticCurveTo(const QuadraticCurveParam& param) override;
78     void Ellipse(const EllipseParam& param) override;
79     void SetTransform(const TransformParam& param) override;
80     void Transform(const TransformParam& param) override;
81     void Translate(double x, double y) override;
82     void Restore() override;
83     void Save() override;
84     bool IsPointInStroke(double x, double y) override;
85     bool IsPointInStroke(const RefPtr<CanvasPath2D>& path, double x, double y) override;
86     bool IsPointInPath(double x, double y) override;
87     bool IsPointInPath(const RefPtr<CanvasPath2D>& path, double x, double y) override;
88     void ResetTransform() override;
89     void SetFillRuleForPath(const CanvasFillRule& rule) override;
90     void SetFillRuleForPath2D(const CanvasFillRule& rule) override;
91 private:
92     void InitImagePaint();
93     void InitCachePaint();
94     bool antiAlias_ = true;
95 #ifndef USE_ROSEN_DRAWING
96     SkPaint GetStrokePaint();
97 #ifdef NEW_SKIA
98     SkSamplingOptions options_;
99 #endif
100 #else
101     RSPen GetStrokePaint();
102     RSSamplingOptions options_;
103 #endif
104     WeakPtr<PipelineBase> pipelineContext_;
105 #ifndef USE_ROSEN_DRAWING
106     SkBitmap skBitmap_;
107     SkPath skPath_;
108     SkPath skPath2d_;
109     SkPaint imagePaint_;
110     SkPaint cachePaint_;
111     SkBitmap cacheBitmap_;
112     std::unique_ptr<SkCanvas> cacheCanvas_;
113     std::unique_ptr<SkCanvas> skCanvas_;
114 #else
115     RSBitmap bitmap_;
116     RSRecordingPath path_;
117     RSRecordingPath path2d_;
118     RSBrush imageBrush_;
119     RSBrush cacheBrush_;
120     RSBitmap cacheBitmap_;
121     std::unique_ptr<RSCanvas> cacheCanvas_;
122     std::unique_ptr<RSCanvas> canvas_;
123 #endif
124     std::map<std::string, setColorFunc> filterFunc_;
125     ImageSourceInfo loadingSource_;
126     ImageSourceInfo currentSource_;
127     ImageObjSuccessCallback imageObjSuccessCallback_;
128     UploadSuccessCallback uploadSuccessCallback_;
129     FailedCallback failedCallback_;
130     OnPostBackgroundTask onPostBackgroundTask_;
131     sk_sp<SkSVGDOM> skiaDom_ = nullptr;
132     CanvasImage canvasImage_;
133 
134 #ifndef USE_ROSEN_DRAWING
135     void UpdatePaintShader(SkPaint& paint, const Gradient& gradient);
136     void UpdatePaintShader(const Pattern& pattern, SkPaint& paint);
137 #else
138     void UpdatePaintShader(RSPen* pen, RSBrush* brush, const Gradient& gradient);
139     void UpdatePaintShader(const Pattern& pattern, RSPen* pen, RSBrush* brush);
140 #endif
141     void PaintText(const std::string& text, double x, double y, bool isStroke, bool hasShadow = false);
142     double GetBaselineOffset(TextBaseline baseline, std::unique_ptr<txt::Paragraph>& paragraph);
143     std::unique_ptr<txt::Paragraph> paragraph_;
144     bool HasShadow() const;
145     bool HasImageShadow() const;
146     void UpdateTextStyleForeground(bool isStroke, txt::TextStyle& style, bool hasShadow);
147     double GetAlignOffset(const std::string& text, TextAlign align, std::unique_ptr<txt::Paragraph>& paragraph);
148     TextDirection GetTextDirection(const std::string& text);
149     bool UpdateOffParagraph(const std::string& text, bool isStroke, const PaintState& state, bool hasShadow = false);
150 #ifndef USE_ROSEN_DRAWING
151     void UpdateLineDash(SkPaint& paint);
152 #else
153     void UpdateLineDash(RSPen& pen);
154 #endif
155     void Path2DAddPath(const PathArgs& args);
156     void Path2DSetTransform(const PathArgs& args);
157     void Path2DMoveTo(const PathArgs& args);
158     void Path2DLineTo(const PathArgs& args);
159     void Path2DArc(const PathArgs& args);
160     void Path2DArcTo(const PathArgs& args);
161     void Path2DQuadraticCurveTo(const PathArgs& args);
162     void Path2DBezierCurveTo(const PathArgs& args);
163     void Path2DEllipse(const PathArgs& args);
164     void Path2DRect(const PathArgs& args);
165     void Path2DClosePath(const PathArgs& args);
166     void Path2DStroke();
167     void Path2DFill();
168     void Path2DClip();
169     void ParsePath2D(const RefPtr<CanvasPath2D>& path);
170     void TranspareCmdToPath(const RefPtr<CanvasPath2D>& path);
171 #ifndef USE_ROSEN_DRAWING
172     bool IsPointInPathByColor(double x, double y, SkPath& path, SkColor colorMatch);
173 #else
174     bool IsPointInPathByColor(double x, double y, RSPath& path, RSColorQuad colorMatch);
175 #endif
176     void SetPaintImage();
177     void InitFilterFunc();
178     bool GetFilterType(std::string& filterType, std::string& filterParam);
179     void SetGrayFilter(const std::string& percent);
180     void SetSepiaFilter(const std::string& percent);
181     void SetInvertFilter(const std::string& percent);
182     void SetOpacityFilter(const std::string& percent);
183     void SetBrightnessFilter(const std::string& percent);
184     void SetContrastFilter(const std::string& percent);
185     void SetBlurFilter(const std::string& percent);
186     void SetDropShadowFilter(const std::string& percent);
187     void SetSaturateFilter(const std::string& percent);
188     void SetHueRotateFilter(const std::string& percent);
189     double PxStrToDouble(const std::string& str);
190     double BlurStrToDouble(const std::string& str);
191     bool IsPercentStr(std::string& percentStr);
192     void SetColorFilter(float matrix[20]);
193     void InitImageCallbacks();
194     void ImageObjReady(const RefPtr<ImageObject>& imageObj);
195     void ImageObjFailed();
196     void DrawSvgImage(const CanvasImage& canvasImage);
197 };
198 } // namespace OHOS::Ace
199 
200 #endif // !FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CUSTOM_PAINT_ROSEN_RENDER_OFFSCREEN_CANVAS_H
201