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