1 /* 2 * Copyright (c) 2021-2022 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_CUSTOM_PAINT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CUSTOM_PAINT_FLUTTER_RENDER_CUSTOM_PAINT_H 18 19 #include "base/utils/measure_util.h" 20 #include "flutter/fml/memory/ref_ptr.h" 21 #include "flutter/lib/ui/painting/path.h" 22 #include "txt/paragraph.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 33 class FlutterRenderCustomPaint : public RenderCustomPaint { 34 DECLARE_ACE_TYPE(FlutterRenderCustomPaint, RenderCustomPaint); 35 36 public: 37 FlutterRenderCustomPaint(); 38 ~FlutterRenderCustomPaint() override = default; 39 40 void Paint(RenderContext& context, const Offset& offset) override; 41 42 void TransferFromImageBitmap(const RefPtr<OffscreenCanvas>& offscreenCanvas) override; 43 void DrawBitmapMesh(const RefPtr<OffscreenCanvas>& offscreenCanvas, const std::vector<double>& mesh, 44 int32_t column, int32_t row) override; 45 void Mesh(SkBitmap& bitmap, int column, int row, 46 const float* vertices, const int* colors, const SkPaint* paint); 47 std::string ToDataURL(const std::string& args) override; 48 void SetAntiAlias(bool isEnabled) override; 49 void FillRect(const Offset& offset, const Rect& rect) override; 50 void StrokeRect(const Offset& offset, const Rect& rect) override; 51 void ClearRect(const Offset& offset, const Rect& rect) override; 52 void FillText(const Offset& offset, const std::string& text, double x, double y) override; 53 void StrokeText(const Offset& offset, const std::string& text, double x, double y) override; 54 static double MeasureTextInner(const MeasureContext& context); 55 double MeasureText(const std::string& text, const PaintState& state) override; 56 double MeasureTextHeight(const std::string& text, const PaintState& state) override; 57 TextMetrics MeasureTextMetrics(const std::string& text, const PaintState& state) override; 58 void MoveTo(const Offset& offset, double x, double y) override; 59 void LineTo(const Offset& offset, double x, double y) override; 60 void BezierCurveTo(const Offset& offset, const BezierCurveParam& param) override; 61 void QuadraticCurveTo(const Offset& offset, const QuadraticCurveParam& param) override; 62 void Arc(const Offset& offset, const ArcParam& param) override; 63 void ArcTo(const Offset& offset, const ArcToParam& param) override; 64 void Ellipse(const Offset& offset, const EllipseParam& param) override; 65 void AddRect(const Offset& offset, const Rect& rect) override; 66 void Fill(const Offset& offset) override; 67 void Fill(const Offset& offset, const RefPtr<CanvasPath2D>& path) override; 68 void Stroke(const Offset& offset) override; 69 void Stroke(const Offset& offset, const RefPtr<CanvasPath2D>& path) override; 70 void Clip() override; 71 void Clip(const RefPtr<CanvasPath2D>& path) override; 72 void BeginPath() override; 73 void ClosePath() override; 74 void Restore() override; 75 void Save() override; 76 void ResetTransform() override; 77 void Rotate(double angle) override; 78 void Scale(double x, double y) 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 DrawImage(const Offset& offset, const CanvasImage& canvasImage, double width, double height) override; 83 void DrawPixelMap(RefPtr<PixelMap> pixelMap, const CanvasImage& canvasImage) override; 84 void PutImageData(const Offset& offset, const ImageData& imageData) override; 85 std::unique_ptr<ImageData> GetImageData(double left, double top, double width, double height) override; 86 std::string GetJsonData(const std::string& path) override; 87 88 void WebGLInit(CanvasRenderContextBase* context) override; 89 void WebGLUpdate() override; 90 RenderLayer GetRenderLayer() override; 91 92 void SetFillRuleForPath(const CanvasFillRule& rule) override; 93 void SetFillRuleForPath2D(const CanvasFillRule& rule) override; 94 95 private: 96 void InitImagePaint(); 97 void InitCachePaint(); 98 bool UpdateParagraph(const Offset& offset, const std::string& text, bool isStroke, bool hasShadow = false); 99 void PaintText(const Offset& offset, double x, double y, bool isStroke, bool hasShadow = false); 100 double GetAlignOffset(TextAlign align, std::unique_ptr<txt::Paragraph>& paragraph); 101 double GetBaselineOffset(TextBaseline baseline, std::unique_ptr<txt::Paragraph>& paragraph); 102 SkPaint GetStrokePaint(); 103 bool HasShadow() const; 104 void UpdatePaintShader(const Offset& offset, SkPaint& paint, const Gradient& gradient); 105 void UpdatePaintShader(const Pattern& pattern, SkPaint& paint); 106 void UpdateLineDash(SkPaint& paint); 107 void UpdateTextStyleForeground(const Offset& offset, bool isStroke, txt::TextStyle& style, bool hasShadow); 108 109 void Path2DAddPath(const Offset& offset, const PathArgs& args); 110 void Path2DSetTransform(const Offset& offset, const PathArgs& args); 111 void Path2DMoveTo(const Offset& offset, const PathArgs& args); 112 void Path2DLineTo(const Offset& offset, const PathArgs& args); 113 void Path2DArc(const Offset& offset, const PathArgs& args); 114 void Path2DArcTo(const Offset& offset, const PathArgs& args); 115 void Path2DQuadraticCurveTo(const Offset& offset, const PathArgs& args); 116 void Path2DBezierCurveTo(const Offset& offset, const PathArgs& args); 117 void Path2DEllipse(const Offset& offset, const PathArgs& args); 118 void Path2DRect(const Offset& offset, const PathArgs& args); 119 void Path2DClosePath(const Offset& offset, const PathArgs& args); 120 void Path2DStroke(const Offset& offset); 121 void Path2DFill(const Offset& offset); 122 void Path2DClip(); 123 void ParsePath2D(const Offset& offset, const RefPtr<CanvasPath2D>& path); 124 125 bool antiAlias_ = false; 126 std::unique_ptr<txt::Paragraph> paragraph_; 127 RefPtr<Flutter::ClipLayer> layer_; 128 SkPath skPath_; 129 SkPath skPath2d_; 130 SkPaint imagePaint_; 131 SkPaint cachePaint_; 132 SkBitmap cacheBitmap_; 133 SkBitmap canvasCache_; 134 SkBitmap webglBitmap_; 135 std::unique_ptr<SkCanvas> skCanvas_; 136 std::unique_ptr<SkCanvas> cacheCanvas_; 137 Size lastLayoutSize_; 138 }; 139 140 } // namespace OHOS::Ace 141 142 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CUSTOM_PAINT_FLUTTER_RENDER_CUSTOM_PAINT_H 143