1 /*
2 * Copyright (c) 2021-2024 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 "skia_matrix.h"
17
18 #include "utils/matrix.h"
19 #include "skia_matrix44.h"
20
21 namespace OHOS {
22 namespace Rosen {
23 namespace Drawing {
SkiaMatrix()24 SkiaMatrix::SkiaMatrix() : skMatrix_() {}
25
SkiaMatrix(const Matrix & other)26 SkiaMatrix::SkiaMatrix(const Matrix& other) : skMatrix_(other.GetImpl<SkiaMatrix>()->ExportSkiaMatrix()) {}
27
ExportSkiaMatrix() const28 const SkMatrix& SkiaMatrix::ExportSkiaMatrix() const
29 {
30 return skMatrix_;
31 }
32
ImportMatrix(const SkMatrix & skMatrix)33 void SkiaMatrix::ImportMatrix(const SkMatrix& skMatrix)
34 {
35 skMatrix_ = skMatrix;
36 }
37
ExportMatrix()38 SkMatrix& SkiaMatrix::ExportMatrix()
39 {
40 return skMatrix_;
41 }
42
Rotate(scalar degree,scalar px,scalar py)43 void SkiaMatrix::Rotate(scalar degree, scalar px, scalar py)
44 {
45 skMatrix_.setRotate(degree, px, py);
46 }
47
Translate(scalar dx,scalar dy)48 void SkiaMatrix::Translate(scalar dx, scalar dy)
49 {
50 skMatrix_.setTranslate(dx, dy);
51 }
52
Scale(scalar sx,scalar sy,scalar px,scalar py)53 void SkiaMatrix::Scale(scalar sx, scalar sy, scalar px, scalar py)
54 {
55 skMatrix_.setScale(sx, sy, px, py);
56 }
57
SetScale(scalar sx,scalar sy)58 void SkiaMatrix::SetScale(scalar sx, scalar sy)
59 {
60 skMatrix_.setScale(sx, sy);
61 }
62
SetScaleTranslate(scalar sx,scalar sy,scalar dx,scalar dy)63 void SkiaMatrix::SetScaleTranslate(scalar sx, scalar sy, scalar dx, scalar dy)
64 {
65 skMatrix_.setScaleTranslate(sx, sy, dx, dy);
66 }
67
SetSkew(scalar kx,scalar ky)68 void SkiaMatrix::SetSkew(scalar kx, scalar ky)
69 {
70 skMatrix_.setSkew(kx, ky);
71 }
72
SetSkew(scalar kx,scalar ky,scalar px,scalar py)73 void SkiaMatrix::SetSkew(scalar kx, scalar ky, scalar px, scalar py)
74 {
75 skMatrix_.setSkew(kx, ky, px, py);
76 }
77
PreRotate(scalar degree)78 void SkiaMatrix::PreRotate(scalar degree)
79 {
80 skMatrix_.preRotate(degree);
81 }
82
PostRotate(scalar degree)83 void SkiaMatrix::PostRotate(scalar degree)
84 {
85 skMatrix_.postRotate(degree);
86 }
87
PostRotate(scalar degree,scalar px,scalar py)88 void SkiaMatrix::PostRotate(scalar degree, scalar px, scalar py)
89 {
90 skMatrix_.postRotate(degree, px, py);
91 }
92
PreTranslate(scalar dx,scalar dy)93 void SkiaMatrix::PreTranslate(scalar dx, scalar dy)
94 {
95 skMatrix_.preTranslate(dx, dy);
96 }
97
PostTranslate(scalar dx,scalar dy)98 void SkiaMatrix::PostTranslate(scalar dx, scalar dy)
99 {
100 skMatrix_.postTranslate(dx, dy);
101 }
102
PreScale(scalar sx,scalar sy)103 void SkiaMatrix::PreScale(scalar sx, scalar sy)
104 {
105 skMatrix_.preScale(sx, sy);
106 }
107
PostScale(scalar sx,scalar sy)108 void SkiaMatrix::PostScale(scalar sx, scalar sy)
109 {
110 skMatrix_.postScale(sx, sy);
111 }
112
PostScale(scalar sx,scalar sy,scalar px,scalar py)113 void SkiaMatrix::PostScale(scalar sx, scalar sy, scalar px, scalar py)
114 {
115 skMatrix_.postScale(sx, sy, px, py);
116 }
117
PreSkew(scalar kx,scalar ky)118 void SkiaMatrix::PreSkew(scalar kx, scalar ky)
119 {
120 skMatrix_.preSkew(kx, ky);
121 }
122
PreSkew(scalar kx,scalar ky,scalar px,scalar py)123 void SkiaMatrix::PreSkew(scalar kx, scalar ky, scalar px, scalar py)
124 {
125 skMatrix_.preSkew(kx, ky, px, py);
126 }
PostSkew(scalar kx,scalar ky)127 void SkiaMatrix::PostSkew(scalar kx, scalar ky)
128 {
129 skMatrix_.postSkew(kx, ky);
130 }
131
PostSkew(scalar kx,scalar ky,scalar px,scalar py)132 void SkiaMatrix::PostSkew(scalar kx, scalar ky, scalar px, scalar py)
133 {
134 skMatrix_.postSkew(kx, ky, px, py);
135 }
136
PreConcat(const Matrix & other)137 void SkiaMatrix::PreConcat(const Matrix& other)
138 {
139 skMatrix_.preConcat(other.GetImpl<SkiaMatrix>()->ExportSkiaMatrix());
140 }
141
PreConcat(const Matrix44 & other)142 void SkiaMatrix::PreConcat(const Matrix44& other)
143 {
144 skMatrix_.preConcat(other.GetImpl<SkiaMatrix44>()->GetSkMatrix44().asM33());
145 }
146
PostConcat(const Matrix & other)147 void SkiaMatrix::PostConcat(const Matrix& other)
148 {
149 skMatrix_.postConcat(other.GetImpl<SkiaMatrix>()->ExportSkiaMatrix());
150 }
151
PostConcat(const Matrix44 & other)152 void SkiaMatrix::PostConcat(const Matrix44& other)
153 {
154 skMatrix_.postConcat(other.GetImpl<SkiaMatrix44>()->GetSkMatrix44().asM33());
155 }
156
Invert(Matrix & inverse) const157 bool SkiaMatrix::Invert(Matrix& inverse) const
158 {
159 SkMatrix skMatrix;
160 if (skMatrix_.invert(&skMatrix)) {
161 inverse.GetImpl<SkiaMatrix>()->ImportMatrix(skMatrix);
162 return true;
163 }
164 return false;
165 }
166
Multiply(const Matrix & a,const Matrix & b)167 void SkiaMatrix::Multiply(const Matrix& a, const Matrix& b)
168 {
169 auto m1 = a.GetImpl<SkiaMatrix>();
170 auto m2 = b.GetImpl<SkiaMatrix>();
171 if (m1 != nullptr && m2 != nullptr) {
172 skMatrix_.setConcat(m1->ExportSkiaMatrix(), m2->ExportSkiaMatrix());
173 }
174 }
175
Equals(const Matrix & a,const Matrix & b) const176 bool SkiaMatrix::Equals(const Matrix& a, const Matrix& b) const
177 {
178 auto m1 = a.GetImpl<SkiaMatrix>();
179 auto m2 = b.GetImpl<SkiaMatrix>();
180 if (m1 != nullptr && m2 != nullptr) {
181 return (m1->ExportSkiaMatrix() == m2->ExportSkiaMatrix());
182 }
183 return false;
184 }
185
SetMatrix(scalar scaleX,scalar skewX,scalar transX,scalar skewY,scalar scaleY,scalar transY,scalar persp0,scalar persp1,scalar persp2)186 void SkiaMatrix::SetMatrix(scalar scaleX, scalar skewX, scalar transX, scalar skewY, scalar scaleY, scalar transY,
187 scalar persp0, scalar persp1, scalar persp2)
188 {
189 skMatrix_.setAll(scaleX, skewX, transX, skewY, scaleY, transY, persp0, persp1, persp2);
190 }
191
SetRectToRect(const Rect & src,const Rect & dst,ScaleToFit stf)192 bool SkiaMatrix::SetRectToRect(const Rect& src, const Rect& dst, ScaleToFit stf)
193 {
194 SkRect skSrc = SkRect::MakeLTRB(src.GetLeft(), src.GetTop(), src.GetRight(), src.GetBottom());
195 SkRect skDst = SkRect::MakeLTRB(dst.GetLeft(), dst.GetTop(), dst.GetRight(), dst.GetBottom());
196 bool ret = skMatrix_.setRectToRect(skSrc, skDst, static_cast<SkMatrix::ScaleToFit>(stf));
197 return ret;
198 }
199
MapPoints(std::vector<Point> & dst,const std::vector<Point> & src,uint32_t count) const200 void SkiaMatrix::MapPoints(std::vector<Point>& dst, const std::vector<Point>& src, uint32_t count) const
201 {
202 if (count == 0) {
203 return;
204 }
205 if (dst.size() > count) {
206 for (size_t i = dst.size(); i > count; --i) {
207 dst.pop_back();
208 }
209 }
210 skMatrix_.mapPoints(
211 reinterpret_cast<SkPoint*>(dst.data()), reinterpret_cast<const SkPoint*>(src.data()), count);
212 }
213
MapRect(Rect & dst,const Rect & src) const214 bool SkiaMatrix::MapRect(Rect& dst, const Rect& src) const
215 {
216 SkRect skSrc = SkRect::MakeXYWH(src.GetLeft(), src.GetTop(), src.GetWidth(), src.GetHeight());
217 SkRect skDst;
218 bool ret = skMatrix_.mapRect(&skDst, skSrc);
219 dst = Rect(skDst.fLeft, skDst.fTop, skDst.fRight, skDst.fBottom);
220 return ret;
221 }
222
SetPolyToPoly(const Point src[],const Point dst[],uint32_t count)223 bool SkiaMatrix::SetPolyToPoly(const Point src[], const Point dst[], uint32_t count)
224 {
225 return skMatrix_.setPolyToPoly(
226 reinterpret_cast<const SkPoint*>(src), reinterpret_cast<const SkPoint*>(dst), count);
227 }
228
Set(int index,scalar value)229 void SkiaMatrix::Set(int index, scalar value)
230 {
231 skMatrix_.set(index, value);
232 }
233
Get(int index) const234 scalar SkiaMatrix::Get(int index) const
235 {
236 return skMatrix_.get(index);
237 }
238
GetAll(std::array<scalar,MatrixImpl::MATRIX_SIZE> & buffer) const239 void SkiaMatrix::GetAll(std::array<scalar, MatrixImpl::MATRIX_SIZE>& buffer) const
240 {
241 skMatrix_.get9(buffer.data());
242 }
243
SetAll(std::array<scalar,MatrixImpl::MATRIX_SIZE> & buffer)244 void SkiaMatrix::SetAll(std::array<scalar, MatrixImpl::MATRIX_SIZE>& buffer)
245 {
246 skMatrix_.set9(buffer.data());
247 }
248
IsIdentity() const249 bool SkiaMatrix::IsIdentity() const
250 {
251 return skMatrix_.isIdentity();
252 }
253
Clone(const Matrix & other)254 void SkiaMatrix::Clone(const Matrix& other)
255 {
256 skMatrix_ = other.GetImpl<SkiaMatrix>()->ExportSkiaMatrix();
257 }
258
PreRotate(scalar degree,scalar px,scalar py)259 void SkiaMatrix::PreRotate(scalar degree, scalar px, scalar py)
260 {
261 skMatrix_.preRotate(degree, px, py);
262 }
263
PreScale(scalar sx,scalar sy,scalar px,scalar py)264 void SkiaMatrix::PreScale(scalar sx, scalar sy, scalar px, scalar py)
265 {
266 skMatrix_.preScale(sx, sy, px, py);
267 }
268
Reset()269 void SkiaMatrix::Reset()
270 {
271 skMatrix_.reset();
272 }
273
GetMinMaxScales(scalar scaleFactors[2])274 bool SkiaMatrix::GetMinMaxScales(scalar scaleFactors[2])
275 {
276 return skMatrix_.getMinMaxScales(scaleFactors);
277 }
278
HasPerspective() const279 bool SkiaMatrix::HasPerspective() const
280 {
281 return skMatrix_.hasPerspective();
282 }
283 } // namespace Drawing
284 } // namespace Rosen
285 } // namespace OHOS
286