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