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_CUSTOM_PAINT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CUSTOM_PAINT_ROSEN_RENDER_CUSTOM_PAINT_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/SkBitmap.h" 27 #include "include/core/SkCanvas.h" 28 #include "include/core/SkPaint.h" 29 #include "include/core/SkPath.h" 30 #endif 31 32 #include "base/utils/measure_util.h" 33 #include "core/components/custom_paint/offscreen_canvas.h" 34 #include "core/components/custom_paint/render_custom_paint.h" 35 #include "core/image/image_object.h" 36 #include "core/image/image_provider.h" 37 #include "core/image/image_source_info.h" 38 39 namespace OHOS::Ace { 40 class RosenRenderCustomPaint : public RenderCustomPaint { 41 DECLARE_ACE_TYPE(RosenRenderCustomPaint, RenderCustomPaint); 42 43 public: 44 RosenRenderCustomPaint(); 45 ~RosenRenderCustomPaint() override; 46 47 void Paint(RenderContext& context, const Offset& offset) override; 48 49 void TransferFromImageBitmap(const RefPtr<OffscreenCanvas>& offscreenCanvas) override; 50 void DrawBitmapMesh(const RefPtr<OffscreenCanvas>& offscreenCanvas, 51 const std::vector<double>& mesh, int32_t column, int32_t row) override; 52 #ifndef USE_ROSEN_DRAWING 53 void Mesh(SkBitmap& bitmap, int column, int row, 54 const float* vertices, const int* colors, const SkPaint* paint); 55 #else 56 void Mesh(RSBitmap& bitmap, int column, int row, 57 const float* vertices, const int* colors, const RSBrush* brush); 58 #endif 59 std::string ToDataURL(const std::string& args) override; 60 void SetAntiAlias(bool isEnabled) override; 61 void FillRect(const Offset& offset, const Rect& rect) override; 62 void StrokeRect(const Offset& offset, const Rect& rect) override; 63 void ClearRect(const Offset& offset, const Rect& rect) override; 64 void FillText(const Offset& offset, const std::string& text, double x, double y) override; 65 void StrokeText(const Offset& offset, const std::string& text, double x, double y) override; 66 static double MeasureTextInner(const MeasureContext& context); 67 static Size MeasureTextSizeInner(const MeasureContext& context); 68 double MeasureText(const std::string& text, const PaintState& state) override; 69 double MeasureTextHeight(const std::string& text, const PaintState& state) override; 70 TextMetrics MeasureTextMetrics(const std::string& text, const PaintState& state) override; 71 void MoveTo(const Offset& offset, double x, double y) override; 72 void LineTo(const Offset& offset, double x, double y) override; 73 void BezierCurveTo(const Offset& offset, const BezierCurveParam& param) override; 74 void QuadraticCurveTo(const Offset& offset, const QuadraticCurveParam& param) override; 75 void Arc(const Offset& offset, const ArcParam& param) override; 76 void ArcTo(const Offset& offset, const ArcToParam& param) override; 77 void Ellipse(const Offset& offset, const EllipseParam& param) override; 78 void AddRect(const Offset& offset, const Rect& rect) override; 79 void Fill(const Offset& offset) override; 80 void Fill(const Offset& offset, const RefPtr<CanvasPath2D>& path) override; 81 void Stroke(const Offset& offset) override; 82 void Stroke(const Offset& offset, const RefPtr<CanvasPath2D>& path) override; 83 void Clip() override; 84 void Clip(const RefPtr<CanvasPath2D>& path) override; 85 void BeginPath() override; 86 void ClosePath() override; 87 void ResetTransform() override; 88 void Restore() override; 89 void Save() override; 90 void Rotate(double angle) override; 91 void Scale(double x, double y) override; 92 void SetTransform(const TransformParam& param) override; 93 void Transform(const TransformParam& param) override; 94 void Translate(double x, double y) override; 95 void DrawImage(const Offset& offset, const CanvasImage& canvasImage, double width, double height) override; 96 void DrawPixelMap(RefPtr<PixelMap> pixelMap, const CanvasImage& canvasImage) override; 97 void PutImageData(const Offset& offset, const ImageData& imageData) override; 98 std::unique_ptr<ImageData> GetImageData(double left, double top, double width, double height) override; 99 std::string GetJsonData(const std::string& path) override; 100 101 void WebGLInit(CanvasRenderContextBase* context) override; 102 void WebGLUpdate() override; 103 104 void SetFillRuleForPath(const CanvasFillRule& rule) override; 105 void SetFillRuleForPath2D(const CanvasFillRule& rule) override; 106 IsRepaintBoundary()107 bool IsRepaintBoundary() const override 108 { 109 return RenderNode::IsRepaintBoundary(); 110 } 111 112 private: 113 void InitImagePaint(); 114 #ifndef USE_ROSEN_DRAWING 115 void InitPaintBlend(SkPaint& paint); 116 #else 117 void InitPaintBlend(RSBrush& brush); 118 #endif 119 bool UpdateParagraph(const Offset& offset, const std::string& text, bool isStroke, bool hasShadow = false); 120 void PaintText(const Offset& offset, double x, double y, bool isStroke, bool hasShadow = false); 121 double GetAlignOffset(TextAlign align, std::unique_ptr<txt::Paragraph>& paragraph); 122 double GetBaselineOffset(TextBaseline baseline, std::unique_ptr<txt::Paragraph>& paragraph); 123 #ifndef USE_ROSEN_DRAWING 124 SkPaint GetStrokePaint(); 125 #else 126 RSPen GetStrokePaint(); 127 #endif 128 bool HasShadow() const; 129 #ifndef USE_ROSEN_DRAWING 130 void UpdatePaintShader(const Offset& offset, SkPaint& paint, const Gradient& gradient); 131 void UpdatePaintShader(const Pattern& pattern, SkPaint& paint); 132 void UpdateLineDash(SkPaint& paint); 133 #else 134 void UpdatePaintShader(const Offset& offset, RSPen* pen, RSBrush* brush, const Gradient& gradient); 135 void UpdatePaintShader(const Pattern& pattern, RSPen* pen, RSBrush* brush); 136 void UpdateLineDash(RSPen& paint); 137 #endif 138 void UpdateTextStyleForeground(const Offset& offset, bool isStroke, txt::TextStyle& style, bool hasShadow); 139 140 void Path2DAddPath(const Offset& offset, const PathArgs& args); 141 void Path2DSetTransform(const Offset& offset, const PathArgs& args); 142 void Path2DMoveTo(const Offset& offset, const PathArgs& args); 143 void Path2DLineTo(const Offset& offset, const PathArgs& args); 144 void Path2DArc(const Offset& offset, const PathArgs& args); 145 void Path2DArcTo(const Offset& offset, const PathArgs& args); 146 void Path2DQuadraticCurveTo(const Offset& offset, const PathArgs& args); 147 void Path2DBezierCurveTo(const Offset& offset, const PathArgs& args); 148 void Path2DEllipse(const Offset& offset, const PathArgs& args); 149 void Path2DRect(const Offset& offset, const PathArgs& args); 150 void Path2DClosePath(const Offset& offset, const PathArgs& args); 151 void Path2DStroke(const Offset& offset); 152 void Path2DFill(const Offset& offset); 153 void Path2DClip(); 154 void ParsePath2D(const Offset& offset, const RefPtr<CanvasPath2D>& path); 155 void InitImageCallbacks(); 156 void ImageObjReady(const RefPtr<ImageObject>& imageObj); 157 void ImageObjFailed(); 158 void DrawSvgImage(const Offset& offset, const CanvasImage& canvasImage); 159 #ifndef USE_ROSEN_DRAWING 160 sk_sp<SkImage> GetImage(const std::string& src); 161 #else 162 std::shared_ptr<RSImage> GetImage(const std::string& src); 163 #endif 164 void CreateBitmap(double viewScale); 165 bool CreateSurface(double viewScale); 166 167 bool antiAlias_ = false; 168 std::unique_ptr<txt::Paragraph> paragraph_; 169 #ifndef USE_ROSEN_DRAWING 170 SkPath skPath_; 171 // Specifically refers to the class Path2D in canvas.d.ts 172 SkPath skPath2d_; 173 SkPaint imagePaint_; 174 SkPaint cachePaint_; 175 #ifdef NEW_SKIA 176 SkSamplingOptions options_; 177 #endif 178 SkBitmap cacheBitmap_; 179 SkBitmap canvasCache_; 180 SkBitmap webglBitmap_; 181 std::unique_ptr<SkCanvas> skCanvas_; 182 std::unique_ptr<SkCanvas> cacheCanvas_; 183 #else 184 RSRecordingPath drawingPath_; 185 // Specifically refers to the class Path2D in canvas.d.ts 186 RSRecordingPath drawingPath2d_; 187 RSBrush imageBrush_; 188 RSBrush cacheBrush_; 189 RSSamplingOptions options_; 190 RSBitmap cacheBitmap_; 191 RSBitmap canvasCache_; 192 RSBitmap webglBitmap_; 193 std::unique_ptr<RSCanvas> drawingCanvas_; 194 std::unique_ptr<RSCanvas> cacheCanvas_; 195 #endif 196 ImageSourceInfo loadingSource_; 197 ImageSourceInfo currentSource_; 198 ImageObjSuccessCallback imageObjSuccessCallback_; 199 UploadSuccessCallback uploadSuccessCallback_; 200 FailedCallback failedCallback_; 201 OnPostBackgroundTask onPostBackgroundTask_; 202 RefPtr<ImageObject> imageObj_ = nullptr; 203 sk_sp<SkSVGDOM> skiaDom_ = nullptr; 204 CanvasImage canvasImage_; 205 Size lastLayoutSize_; 206 RefPtr<ImageCache> imageCache_; 207 }; 208 } // namespace OHOS::Ace 209 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CUSTOM_PAINT_ROSEN_RENDER_CUSTOM_PAINT_H 210