1 /*
2 * Copyright (c) 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 #include "core/components_ng/render/adapter/skia_canvas.h"
17
18 #include "third_party/skia/include/core/SkCanvas.h"
19 #include "third_party/skia/include/core/SkMatrix.h"
20 #include "third_party/skia/include/core/SkRRect.h"
21 #include "third_party/skia/include/core/SkRect.h"
22
23 #include "base/utils/utils.h"
24 #include "core/components_ng/render/adapter/skia_canvas_image.h"
25 #include "core/components_ng/render/adapter/skia_paint.h"
26
27 namespace OHOS::Ace::NG {
28
ToSkMatrix(const Matrix3 & matrix)29 SkMatrix ToSkMatrix(const Matrix3& matrix)
30 {
31 // clang-format off
32 return SkMatrix::MakeAll(
33 matrix[0][0], matrix[0][1], matrix[0][2],
34 matrix[1][0], matrix[1][1], matrix[1][2],
35 matrix[2][0], matrix[2][1], matrix[2][2]);
36 // clang-format on
37 }
38
ToSkRect(const RectF & rect)39 SkRect ToSkRect(const RectF& rect)
40 {
41 return SkRect::MakeLTRB(rect.GetX(), rect.GetY(), rect.Right(), rect.Bottom());
42 }
43
ToSkRRect(const RRect & rRect)44 SkRRect ToSkRRect(const RRect& rRect)
45 {
46 auto rect = rRect.GetRect();
47 auto corner = rRect.GetCorner();
48 SkRect skRect = SkRect::MakeXYWH(rect.Left(), rect.Top(), rect.Width(), rect.Height());
49 // TODO: replace with new RRectF
50 SkVector fRadii[4] = { { 0.0, 0.0 }, { 0.0, 0.0 }, { 0.0, 0.0 }, { 0.0, 0.0 } };
51 SkRRect skRRect;
52 skRRect.setRectRadii(skRect, fRadii);
53 return skRRect;
54 }
55
Create(void * rawCanvas)56 RefPtr<Canvas> Canvas::Create(void* rawCanvas)
57 {
58 return AceType::MakeRefPtr<SkiaCanvas>(reinterpret_cast<SkCanvas*>(rawCanvas));
59 }
60
Save()61 void SkiaCanvas::Save()
62 {
63 CHECK_NULL_VOID(rawCanvas_);
64 rawCanvas_->save();
65 }
66
Restore()67 void SkiaCanvas::Restore()
68 {
69 CHECK_NULL_VOID(rawCanvas_);
70 rawCanvas_->restore();
71 }
72
Translate(float dx,float dy)73 void SkiaCanvas::Translate(float dx, float dy)
74 {
75 CHECK_NULL_VOID(rawCanvas_);
76 rawCanvas_->translate(dx, dy);
77 }
78
Scale(float sx,float sy)79 void SkiaCanvas::Scale(float sx, float sy)
80 {
81 CHECK_NULL_VOID(rawCanvas_);
82 rawCanvas_->scale(sx, sy);
83 }
84
Rotate(float rad)85 void SkiaCanvas::Rotate(float rad)
86 {
87 CHECK_NULL_VOID(rawCanvas_);
88 rawCanvas_->rotate(rad * 180.0 / PI_NUM);
89 }
90
Skew(float sx,float sy)91 void SkiaCanvas::Skew(float sx, float sy)
92 {
93 CHECK_NULL_VOID(rawCanvas_);
94 rawCanvas_->skew(sx, sy);
95 }
96
SetMatrix(const Matrix3 & matrix)97 void SkiaCanvas::SetMatrix(const Matrix3& matrix)
98 {
99 CHECK_NULL_VOID(rawCanvas_);
100 rawCanvas_->setMatrix(ToSkMatrix(matrix));
101 }
102
ConcatMatrix(const Matrix3 & matrix)103 void SkiaCanvas::ConcatMatrix(const Matrix3& matrix)
104 {
105 CHECK_NULL_VOID(rawCanvas_);
106 rawCanvas_->concat(ToSkMatrix(matrix));
107 }
108
ClipRect(const RectF & rect,ClipQuality quality)109 void SkiaCanvas::ClipRect(const RectF& rect, ClipQuality quality)
110 {
111 CHECK_NULL_VOID(rawCanvas_);
112 rawCanvas_->clipRect(ToSkRect(rect), quality == ClipQuality::ANTI_ALIAS);
113 }
114
ClipRRect(const RRect & rRect,ClipQuality quality)115 void SkiaCanvas::ClipRRect(const RRect& rRect, ClipQuality quality)
116 {
117 CHECK_NULL_VOID(rawCanvas_);
118 rawCanvas_->clipRRect(ToSkRRect(rRect), quality == ClipQuality::ANTI_ALIAS);
119 }
120
ClipWithPath(const ClipPath & path,ClipQuality quality)121 void SkiaCanvas::ClipWithPath(const ClipPath& path, ClipQuality quality)
122 {
123 CHECK_NULL_VOID(rawCanvas_);
124 // TODO: convert CilpPath to SkPath
125 }
126
ClearColor(const Color & color)127 void SkiaCanvas::ClearColor(const Color& color)
128 {
129 CHECK_NULL_VOID(rawCanvas_);
130 rawCanvas_->clear(color.GetValue());
131 }
132
DrawColor(const Color & color)133 void SkiaCanvas::DrawColor(const Color& color)
134 {
135 CHECK_NULL_VOID(rawCanvas_);
136 rawCanvas_->drawColor(color.GetValue());
137 }
138
DrawLine(const PointF & start,const PointF & end,const RefPtr<Paint> & paint)139 void SkiaCanvas::DrawLine(const PointF& start, const PointF& end, const RefPtr<Paint>& paint)
140 {
141 CHECK_NULL_VOID(rawCanvas_);
142 auto skiaPaint = AceType::DynamicCast<SkiaPaint>(paint);
143 CHECK_NULL_VOID(skiaPaint);
144 rawCanvas_->drawLine(start.GetX(), start.GetY(), end.GetX(), end.GetY(), skiaPaint->GetRawPaint());
145 }
146
DrawRect(const RectF & rect,const RefPtr<Paint> & paint)147 void SkiaCanvas::DrawRect(const RectF& rect, const RefPtr<Paint>& paint)
148 {
149 CHECK_NULL_VOID(rawCanvas_);
150 auto skiaPaint = AceType::DynamicCast<SkiaPaint>(paint);
151 CHECK_NULL_VOID(skiaPaint);
152 rawCanvas_->drawRect(ToSkRect(rect), skiaPaint->GetRawPaint());
153 }
154
DrawRRect(const RRect & rRect,const RefPtr<Paint> & paint)155 void SkiaCanvas::DrawRRect(const RRect& rRect, const RefPtr<Paint>& paint)
156 {
157 CHECK_NULL_VOID(rawCanvas_);
158 auto skiaPaint = AceType::DynamicCast<SkiaPaint>(paint);
159 CHECK_NULL_VOID(skiaPaint);
160 rawCanvas_->drawRRect(ToSkRRect(rRect), skiaPaint->GetRawPaint());
161 }
162
DrawCircle(float centerX,float centerY,float radius,const RefPtr<Paint> & paint)163 void SkiaCanvas::DrawCircle(float centerX, float centerY, float radius, const RefPtr<Paint>& paint)
164 {
165 CHECK_NULL_VOID(rawCanvas_);
166 auto skiaPaint = AceType::DynamicCast<SkiaPaint>(paint);
167 CHECK_NULL_VOID(skiaPaint);
168 rawCanvas_->drawCircle(centerX, centerY, radius, skiaPaint->GetRawPaint());
169 }
170
DrawImage(const RefPtr<CanvasImage> & image,const RectF & srcRect,const RectF & dstRect,const RefPtr<Paint> & paint)171 void SkiaCanvas::DrawImage(
172 const RefPtr<CanvasImage>& image, const RectF& srcRect, const RectF& dstRect, const RefPtr<Paint>& paint)
173 {
174 CHECK_NULL_VOID(rawCanvas_);
175 auto skiaPaint = AceType::DynamicCast<SkiaPaint>(paint);
176 CHECK_NULL_VOID(skiaPaint);
177 auto skiaImage = AceType::DynamicCast<SkiaCanvasImage>(image);
178 CHECK_NULL_VOID(skiaImage);
179 auto imageObj = skiaImage->GetCanvasImage();
180 CHECK_NULL_VOID(imageObj);
181 #ifdef NG_BUILD
182 rawCanvas_->drawImageRect(imageObj, ToSkRect(srcRect), ToSkRect(dstRect), skiaPaint->GetSamplingOptions(),
183 &skiaPaint->GetRawPaint(), SkCanvas::kFast_SrcRectConstraint);
184 #else
185 rawCanvas_->drawImageRect(imageObj, ToSkRect(srcRect), ToSkRect(dstRect), &skiaPaint->GetRawPaint());
186 #endif
187 }
188
189 } // namespace OHOS::Ace::NG
190