1 /*
2 * Copyright (c) 2021-2023 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.GetImplPtr<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
PreRotate(scalar degree)68 void SkiaMatrix::PreRotate(scalar degree)
69 {
70 skMatrix_.preRotate(degree);
71 }
72
PostRotate(scalar degree)73 void SkiaMatrix::PostRotate(scalar degree)
74 {
75 skMatrix_.postRotate(degree);
76 }
77
PostRotate(scalar degree,scalar px,scalar py)78 void SkiaMatrix::PostRotate(scalar degree, scalar px, scalar py)
79 {
80 skMatrix_.postRotate(degree, px, py);
81 }
82
PreTranslate(scalar dx,scalar dy)83 void SkiaMatrix::PreTranslate(scalar dx, scalar dy)
84 {
85 skMatrix_.preTranslate(dx, dy);
86 }
87
PostTranslate(scalar dx,scalar dy)88 void SkiaMatrix::PostTranslate(scalar dx, scalar dy)
89 {
90 skMatrix_.postTranslate(dx, dy);
91 }
92
PreScale(scalar sx,scalar sy)93 void SkiaMatrix::PreScale(scalar sx, scalar sy)
94 {
95 skMatrix_.preScale(sx, sy);
96 }
97
PostScale(scalar sx,scalar sy)98 void SkiaMatrix::PostScale(scalar sx, scalar sy)
99 {
100 skMatrix_.postScale(sx, sy);
101 }
102
PostScale(scalar sx,scalar sy,scalar px,scalar py)103 void SkiaMatrix::PostScale(scalar sx, scalar sy, scalar px, scalar py)
104 {
105 skMatrix_.postScale(sx, sy, px, py);
106 }
107
PreConcat(const Matrix & other)108 void SkiaMatrix::PreConcat(const Matrix& other)
109 {
110 skMatrix_.preConcat(other.GetImplPtr<SkiaMatrix>()->ExportSkiaMatrix());
111 }
112
PreConcat(const Matrix44 & other)113 void SkiaMatrix::PreConcat(const Matrix44& other)
114 {
115 skMatrix_.preConcat(other.GetImpl<SkiaMatrix44>()->GetSkMatrix44().asM33());
116 }
117
PostConcat(const Matrix & other)118 void SkiaMatrix::PostConcat(const Matrix& other)
119 {
120 skMatrix_.postConcat(other.GetImplPtr<SkiaMatrix>()->ExportSkiaMatrix());
121 }
122
PostConcat(const Matrix44 & other)123 void SkiaMatrix::PostConcat(const Matrix44& other)
124 {
125 skMatrix_.postConcat(other.GetImpl<SkiaMatrix44>()->GetSkMatrix44().asM33());
126 }
127
Invert(Matrix & inverse) const128 bool SkiaMatrix::Invert(Matrix& inverse) const
129 {
130 SkMatrix skMatrix;
131 if (skMatrix_.invert(&skMatrix)) {
132 inverse.GetImpl<SkiaMatrix>()->ImportMatrix(skMatrix);
133 return true;
134 }
135 return false;
136 }
137
Multiply(const Matrix & a,const Matrix & b)138 void SkiaMatrix::Multiply(const Matrix& a, const Matrix& b)
139 {
140 auto m1 = a.GetImpl<SkiaMatrix>();
141 auto m2 = b.GetImpl<SkiaMatrix>();
142 if (m1 != nullptr && m2 != nullptr) {
143 skMatrix_.setConcat(m1->ExportSkiaMatrix(), m2->ExportSkiaMatrix());
144 }
145 }
146
Equals(const Matrix & a,const Matrix & b) const147 bool SkiaMatrix::Equals(const Matrix& a, const Matrix& b) const
148 {
149 auto m1 = a.GetImpl<SkiaMatrix>();
150 auto m2 = b.GetImpl<SkiaMatrix>();
151 if (m1 != nullptr && m2 != nullptr) {
152 return (m1->ExportSkiaMatrix() == m2->ExportSkiaMatrix());
153 }
154 return false;
155 }
156
SetMatrix(scalar scaleX,scalar skewX,scalar transX,scalar skewY,scalar scaleY,scalar transY,scalar persp0,scalar persp1,scalar persp2)157 void SkiaMatrix::SetMatrix(scalar scaleX, scalar skewX, scalar transX, scalar skewY, scalar scaleY, scalar transY,
158 scalar persp0, scalar persp1, scalar persp2)
159 {
160 skMatrix_.setAll(scaleX, skewX, transX, skewY, scaleY, transY, persp0, persp1, persp2);
161 }
162
MapPoints(std::vector<Point> & dst,const std::vector<Point> & src,uint32_t count) const163 void SkiaMatrix::MapPoints(std::vector<Point>& dst, const std::vector<Point>& src, uint32_t count) const
164 {
165 if (count == 0) {
166 return;
167 }
168 if (dst.size() > count) {
169 for (size_t i = dst.size(); i > count; --i) {
170 dst.pop_back();
171 }
172 }
173 skMatrix_.mapPoints(
174 reinterpret_cast<SkPoint*>(dst.data()), reinterpret_cast<const SkPoint*>(src.data()), count);
175 }
176
MapRect(Rect & dst,const Rect & src) const177 bool SkiaMatrix::MapRect(Rect& dst, const Rect& src) const
178 {
179 SkRect skSrc = SkRect::MakeXYWH(src.GetLeft(), src.GetTop(), src.GetWidth(), src.GetHeight());
180 SkRect skDst;
181 bool ret = skMatrix_.mapRect(&skDst, skSrc);
182 dst = Rect(skDst.fLeft, skDst.fTop, skDst.fRight, skDst.fBottom);
183 return ret;
184 }
185
Set(int index,scalar value)186 void SkiaMatrix::Set(int index, scalar value)
187 {
188 skMatrix_.set(index, value);
189 }
190
Get(int index) const191 scalar SkiaMatrix::Get(int index) const
192 {
193 return skMatrix_.get(index);
194 }
195
GetAll(std::array<scalar,MatrixImpl::MATRIX_SIZE> & buffer) const196 void SkiaMatrix::GetAll(std::array<scalar, MatrixImpl::MATRIX_SIZE>& buffer) const
197 {
198 skMatrix_.get9(buffer.data());
199 }
200
SetAll(std::array<scalar,MatrixImpl::MATRIX_SIZE> & buffer)201 void SkiaMatrix::SetAll(std::array<scalar, MatrixImpl::MATRIX_SIZE>& buffer)
202 {
203 skMatrix_.set9(buffer.data());
204 }
205
IsIdentity() const206 bool SkiaMatrix::IsIdentity() const
207 {
208 return skMatrix_.isIdentity();
209 }
210
Clone(const Matrix & other)211 void SkiaMatrix::Clone(const Matrix& other)
212 {
213 skMatrix_ = other.GetImplPtr<SkiaMatrix>()->ExportSkiaMatrix();
214 }
215
PreRotate(scalar degree,scalar px,scalar py)216 void SkiaMatrix::PreRotate(scalar degree, scalar px, scalar py)
217 {
218 skMatrix_.preRotate(degree, px, py);
219 }
220
PreScale(scalar sx,scalar sy,scalar px,scalar py)221 void SkiaMatrix::PreScale(scalar sx, scalar sy, scalar px, scalar py)
222 {
223 skMatrix_.preScale(sx, sy, px, py);
224 }
225
Reset()226 void SkiaMatrix::Reset()
227 {
228 skMatrix_.reset();
229 }
230
GetMinMaxScales(scalar scaleFactors[2])231 bool SkiaMatrix::GetMinMaxScales(scalar scaleFactors[2])
232 {
233 return skMatrix_.getMinMaxScales(scaleFactors);
234 }
235
HasPerspective() const236 bool SkiaMatrix::HasPerspective() const
237 {
238 return skMatrix_.hasPerspective();
239 }
240 } // namespace Drawing
241 } // namespace Rosen
242 } // namespace OHOS
243