• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "include/core/SkBitmap.h"
20 #include "include/core/SkCanvas.h"
21 #include "include/core/SkImage.h"
22 #include "include/core/SkMatrix.h"
23 #include "include/core/SkPaint.h"
24 #include "include/core/SkPicture.h"
25 #include "include/core/SkPoint3.h"
26 #include "include/core/SkRRect.h"
27 #include "include/utils/SkShadowUtils.h"
28 #include "skia_bitmap.h"
29 #include "skia_image.h"
30 #include "skia_matrix.h"
31 #include "skia_paint.h"
32 #include "skia_picture.h"
33 
34 #include "impl_interface/core_canvas_impl.h"
35 #include "utils/log.h"
36 
37 namespace OHOS {
38 namespace Rosen {
39 namespace Drawing {
40 class SkiaCanvas : public CoreCanvasImpl {
41 public:
42     static inline constexpr AdapterType TYPE = AdapterType::SKIA_ADAPTER;
43     SkiaCanvas();
SkiaCanvas(const std::shared_ptr<SkCanvas> & skCanvas)44     explicit SkiaCanvas(const std::shared_ptr<SkCanvas>& skCanvas) : skiaCanvas_(skCanvas) {}
~SkiaCanvas()45     ~SkiaCanvas() override {};
GetType()46     AdapterType GetType() const override
47     {
48         return AdapterType::SKIA_ADAPTER;
49     }
50 
51     void Bind(const Bitmap& bitmap) override;
52 
53     // shapes
54     void DrawPoint(const Point& point) override;
55     void DrawLine(const Point& startPt, const Point& endPt) override;
56     void DrawRect(const Rect& rect) override;
57     void DrawRoundRect(const RoundRect& roundRect) override;
58     void DrawNestedRoundRect(const RoundRect& outer, const RoundRect& inner) override;
59     void DrawArc(const Rect& oval, scalar startAngle, scalar sweepAngle) override;
60     void DrawPie(const Rect& oval, scalar startAngle, scalar sweepAngle) override;
61     void DrawOval(const Rect& oval) override;
62     void DrawCircle(const Point& centerPt, scalar radius) override;
63     void DrawPath(const Path& path) override;
64     void DrawBackground(const Brush& brush) override;
65     void DrawShadow(const Path& path, const Point3& planeParams, const Point3& devLightPos, scalar lightRadius,
66         Color ambientColor, Color spotColor, ShadowFlags flag) override;
67 
68     // image
69     void DrawBitmap(const Bitmap& bitmap, const scalar px, const scalar py) override;
70     void DrawBitmap(Media::PixelMap& pixelMap, const scalar px, const scalar py) override;
71     void DrawImage(const Image& image, const scalar px, const scalar py, const SamplingOptions& sampling) override;
72     void DrawImageRect(const Image& image, const Rect& src, const Rect& dst, const SamplingOptions& sampling,
73         SrcRectConstraint constraint) override;
74     void DrawImageRect(const Image& image, const Rect& dst, const SamplingOptions& sampling) override;
75     void DrawPicture(const Picture& picture) override;
76 
77     // clip
78     void ClipRect(const Rect& rect, ClipOp op) override;
79     void ClipRoundRect(const RoundRect& roundRect, ClipOp op) override;
80     void ClipPath(const Path& path, ClipOp op) override;
81 
82     // transform
83     void SetMatrix(const Matrix& matrix) override;
84     void ResetMatrix() override;
85     void ConcatMatrix(const Matrix& matrix) override;
86     void Translate(scalar dx, scalar dy) override;
87     void Scale(scalar sx, scalar sy) override;
88     void Rotate(scalar deg) override;
89     void Rotate(scalar deg, scalar sx, scalar sy) override;
90     void Shear(scalar sx, scalar sy) override;
91 
92     // state
93     void Flush() override;
94     void Clear(ColorQuad color) override;
95     void Save() override;
96     void SaveLayer(const Rect& rect, const Brush& brush) override;
97     void Restore() override;
98 
99     // paint
100     void AttachPen(const Pen& pen) override;
101     void AttachBrush(const Brush& brush) override;
102     void DetachPen() override;
103     void DetachBrush() override;
104 
105     SkCanvas* ExportSkCanvas() const;
106 
107 private:
108     void RoundRectCastToSkRRect(const RoundRect& roundRect, SkRRect& skRRect) const;
109     std::shared_ptr<SkCanvas> skiaCanvas_;
110     SkiaPaint skiaPaint_;
111 };
112 } // namespace Drawing
113 } // namespace Rosen
114 } // namespace OHOS
115 #endif
116