1 /*
2  * Copyright (c) 2022-2023 Huawei Device Co., Ltd.. All rights reserved.
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 SKIACANVAS_H
17 #define SKIACANVAS_H
18 
19 #include "draw/canvas.h"
20 #include "include/core/SkBitmap.h"
21 #include "include/core/SkCanvas.h"
22 #include "include/core/HMSymbol.h"
23 #include "include/core/SkImage.h"
24 #include "include/core/SkMatrix.h"
25 #include "include/core/SkPaint.h"
26 #include "include/core/SkPicture.h"
27 #include "include/core/SkPoint3.h"
28 #include "include/core/SkRegion.h"
29 #include "include/core/SkRRect.h"
30 #include "include/utils/SkShadowUtils.h"
31 #include "skia_bitmap.h"
32 #include "skia_image.h"
33 #include "skia_matrix.h"
34 #include "skia_paint.h"
35 #include "skia_picture.h"
36 #include "skia_region.h"
37 #include "skia_vertices.h"
38 #include "skia_canvas_op.h"
39 
40 #include "common/rs_macros.h"
41 #include "impl_interface/core_canvas_impl.h"
42 
43 namespace OHOS {
44 namespace Rosen {
45 namespace Drawing {
46 class DRAWING_API SkiaCanvas : public CoreCanvasImpl {
47 public:
48     static inline constexpr AdapterType TYPE = AdapterType::SKIA_ADAPTER;
49 
50     SkiaCanvas();
51     explicit SkiaCanvas(const std::shared_ptr<SkCanvas>& skCanvas);
52     SkiaCanvas(int32_t width, int32_t height);
~SkiaCanvas()53     ~SkiaCanvas() override {};
54 
GetType()55     AdapterType GetType() const override
56     {
57         return AdapterType::SKIA_ADAPTER;
58     }
59 
60     void Bind(const Bitmap& bitmap) override;
61 
62     Matrix GetTotalMatrix() const override;
63     Rect GetLocalClipBounds() const override;
64     RectI GetDeviceClipBounds() const override;
65     RectI GetRoundInDeviceClipBounds() const override;
66 #ifdef RS_ENABLE_GPU
67     std::shared_ptr<GPUContext> GetGPUContext() const override;
68 #endif
69     int32_t GetWidth() const override;
70     int32_t GetHeight() const override;
71     ImageInfo GetImageInfo() override;
72     bool ReadPixels(const ImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
73         int srcX, int srcY) override;
74     bool ReadPixels(const Bitmap& dstBitmap, int srcX, int srcY) override;
75 
76     // shapes
77     void DrawSdf(const SDFShapeBase& shape) override;
78     void DrawPoint(const Point& point, const Paint& paint) override;
79     void DrawPoints(PointMode mode, size_t count, const Point pts[], const Paint& paint) override;
80     void DrawLine(const Point& startPt, const Point& endPt, const Paint& paint) override;
81     void DrawRect(const Rect& rect, const Paint& paint) override;
82     void DrawRoundRect(const RoundRect& roundRect, const Paint& paint) override;
83     void DrawNestedRoundRect(const RoundRect& outer, const RoundRect& inner, const Paint& paint) override;
84     void DrawArc(const Rect& oval, scalar startAngle, scalar sweepAngle, const Paint& paint) override;
85     void DrawPie(const Rect& oval, scalar startAngle, scalar sweepAngle, const Paint& paint) override;
86     void DrawOval(const Rect& oval, const Paint& paint) override;
87     void DrawCircle(const Point& centerPt, scalar radius, const Paint& paint) override;
88     void DrawPath(const Path& path, const Paint& paint) override;
89     void DrawBackground(const Brush& brush) override;
90     void DrawShadow(const Path& path, const Point3& planeParams, const Point3& devLightPos, scalar lightRadius,
91         Color ambientColor, Color spotColor, ShadowFlags flag) override;
92     void DrawShadowStyle(const Path& path, const Point3& planeParams, const Point3& devLightPos, scalar lightRadius,
93         Color ambientColor, Color spotColor, ShadowFlags flag, bool isLimitElevation) override;
94     void DrawRegion(const Region& region, const Paint& paint) override;
95     void DrawPatch(const Point cubics[12], const ColorQuad colors[4],
96         const Point texCoords[4], BlendMode mode, const Paint& paint) override;
97     void DrawVertices(const Vertices& vertices, BlendMode mode, const Paint& paint) override;
98 
99     void DrawImageNine(const Image* image, const RectI& center, const Rect& dst,
100         FilterMode filter, const Brush* brush = nullptr) override;
101     void DrawImageLattice(const Image* image, const Lattice& lattice, const Rect& dst,
102         FilterMode filter, const Paint& paint) override;
103 
104     // color
105     void DrawColor(ColorQuad color, BlendMode mode) override;
106 
107     bool OpCalculateBefore(const Matrix& matrix) override;
108     std::shared_ptr<Drawing::OpListHandle> OpCalculateAfter(const Rect& bound) override;
109 
110     // image
111     void DrawAtlas(const Image* atlas, const RSXform xform[], const Rect tex[], const ColorQuad colors[], int count,
112         BlendMode mode, const SamplingOptions& sampling, const Rect* cullRect, const Paint& paint) override;
113     void DrawBitmap(const Bitmap& bitmap, const scalar px, const scalar py, const Paint& paint) override;
114     void DrawImage(const Image& image, const scalar px, const scalar py, const SamplingOptions& sampling,
115         const Paint& paint) override;
116     void DrawImageRect(const Image& image, const Rect& src, const Rect& dst, const SamplingOptions& sampling,
117         SrcRectConstraint constraint, const Paint& paint) override;
118     void DrawImageRect(const Image& image, const Rect& dst, const SamplingOptions& sampling,
119         const Paint& paint) override;
120     void DrawPicture(const Picture& picture) override;
121 
122     void DrawSVGDOM(const sk_sp<SkSVGDOM>& svgDom) override;
123 
124     // text
125     void DrawTextBlob(const TextBlob* blob, const scalar x, const scalar y, const Paint& paint) override;
126 
127     // symbol
128     void DrawSymbol(const DrawingHMSymbolData& symbol, Point locate, const Paint& paint) override;
129 
130     // clip
131     void ClipRect(const Rect& rect, ClipOp op, bool doAntiAlias) override;
132     void ClipIRect(const RectI& rect, ClipOp op = ClipOp::INTERSECT) override;
133     void ClipRoundRect(const RoundRect& roundRect, ClipOp op, bool doAntiAlias) override;
134     void ClipRoundRect(const Rect& rect, std::vector<Point>& pts, bool doAntiAlias) override;
135     void ClipPath(const Path& path, ClipOp op, bool doAntiAlias) override;
136     void ClipRegion(const Region& region, ClipOp op = ClipOp::INTERSECT) override;
137     bool IsClipEmpty() override;
138     bool IsClipRect() override;
139     bool QuickReject(const Rect& rect) override;
140 
141     // transform
142     void SetMatrix(const Matrix& matrix) override;
143     void ResetMatrix() override;
144     void ConcatMatrix(const Matrix& matrix) override;
145     void Translate(scalar dx, scalar dy) override;
146     void Scale(scalar sx, scalar sy) override;
147     void Rotate(scalar deg, scalar sx, scalar sy) override;
148     void Shear(scalar sx, scalar sy) override;
149 
150     // state
151     void Flush() override;
152     void Clear(ColorQuad color) override;
153     uint32_t Save() override;
154     void SaveLayer(const SaveLayerOps& saveLayerOps) override;
155     void Restore() override;
156     uint32_t GetSaveCount() const override;
157     void Discard() override;
158 
159     SkCanvas* ExportSkCanvas() const;
160     void ImportSkCanvas(SkCanvas* skCanvas);
161 
162     void BuildOverDraw(std::shared_ptr<Canvas> canvas) override;
163 
164     void BuildNoDraw(int32_t width, int32_t height) override;
165 
166     void Reset(int32_t width, int32_t height) override;
167 
168     void SetGrContextToSkiaImage(SkiaImage* skiaImage);
169 
170     bool DrawBlurImage(const Image& image, const Drawing::HpsBlurParameter& blurParams) override;
171 
172     std::array<int, 2> CalcHpsBluredImageDimension(const Drawing::HpsBlurParameter& blurParams) override;
173 
174 private:
175     void RoundRectCastToSkRRect(const RoundRect& roundRect, SkRRect& skRRect) const;
176     bool ConvertToHMSymbolData(const DrawingHMSymbolData& symbol, HMSymbolData& skSymbol);
177     std::shared_ptr<SkCanvas> skiaCanvas_;
178     SkCanvas* skCanvas_;
179     SkCanvas* skCanvasBackup_ = nullptr;
180     std::shared_ptr<SkiaCanvasOp> skiaCanvasOp_ = nullptr;
181     SkPaint defaultPaint_;
182     SkPaint skPaint_;
183 };
184 } // namespace Drawing
185 } // namespace Rosen
186 } // namespace OHOS
187 #endif
188