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
20 namespace OHOS {
21 namespace Rosen {
22 namespace Drawing {
SkiaMatrix()23 SkiaMatrix::SkiaMatrix() : skMatrix_() {}
24
ExportSkiaMatrix() const25 const SkMatrix& SkiaMatrix::ExportSkiaMatrix() const
26 {
27 return skMatrix_;
28 }
29
ImportMatrix(const SkMatrix & skMatrix)30 void SkiaMatrix::ImportMatrix(const SkMatrix& skMatrix)
31 {
32 skMatrix_ = skMatrix;
33 }
34
Rotate(scalar degree,scalar px,scalar py)35 void SkiaMatrix::Rotate(scalar degree, scalar px, scalar py)
36 {
37 skMatrix_.setRotate(degree, px, py);
38 }
39
Translate(scalar dx,scalar dy)40 void SkiaMatrix::Translate(scalar dx, scalar dy)
41 {
42 skMatrix_.setTranslate(dx, dy);
43 }
44
Scale(scalar sx,scalar sy,scalar px,scalar py)45 void SkiaMatrix::Scale(scalar sx, scalar sy, scalar px, scalar py)
46 {
47 skMatrix_.setScale(sx, sy, px, py);
48 }
49
PreRotate(scalar degree)50 void SkiaMatrix::PreRotate(scalar degree)
51 {
52 skMatrix_.preRotate(degree);
53 }
54
PreTranslate(scalar dx,scalar dy)55 void SkiaMatrix::PreTranslate(scalar dx, scalar dy)
56 {
57 skMatrix_.preTranslate(dx, dy);
58 }
59
PreScale(scalar sx,scalar sy)60 void SkiaMatrix::PreScale(scalar sx, scalar sy)
61 {
62 skMatrix_.preScale(sx, sy);
63 }
64
PreConcat(const Matrix & other)65 void SkiaMatrix::PreConcat(const Matrix& other)
66 {
67 skMatrix_.preConcat(other.GetImpl<SkiaMatrix>()->ExportSkiaMatrix());
68 }
69
Invert(Matrix & inverse) const70 bool SkiaMatrix::Invert(Matrix& inverse) const
71 {
72 SkMatrix skMatrix;
73 if (skMatrix_.invert(&skMatrix)) {
74 inverse.GetImpl<SkiaMatrix>()->ImportMatrix(skMatrix);
75 return true;
76 }
77 return false;
78 }
79
Multiply(const Matrix & a,const Matrix & b)80 void SkiaMatrix::Multiply(const Matrix& a, const Matrix& b)
81 {
82 auto m1 = a.GetImpl<SkiaMatrix>();
83 auto m2 = b.GetImpl<SkiaMatrix>();
84 if (m1 != nullptr && m2 != nullptr) {
85 skMatrix_.setConcat(m1->ExportSkiaMatrix(), m2->ExportSkiaMatrix());
86 }
87 }
88
Equals(const Matrix & a,const Matrix & b) const89 bool SkiaMatrix::Equals(const Matrix& a, const Matrix& b) const
90 {
91 auto m1 = a.GetImpl<SkiaMatrix>();
92 auto m2 = b.GetImpl<SkiaMatrix>();
93 if (m1 != nullptr && m2 != nullptr) {
94 return (m1->ExportSkiaMatrix() == m2->ExportSkiaMatrix());
95 }
96 return false;
97 }
98
SetMatrix(scalar scaleX,scalar skewX,scalar transX,scalar skewY,scalar scaleY,scalar transY,scalar persp0,scalar persp1,scalar persp2)99 void SkiaMatrix::SetMatrix(scalar scaleX, scalar skewX, scalar transX, scalar skewY, scalar scaleY, scalar transY,
100 scalar persp0, scalar persp1, scalar persp2)
101 {
102 skMatrix_.setAll(scaleX, skewX, transX, skewY, scaleY, transY, persp0, persp1, persp2);
103 }
104
MapPoints(std::vector<Point> & dst,const std::vector<Point> & src,uint32_t count) const105 void SkiaMatrix::MapPoints(std::vector<Point>& dst, const std::vector<Point>& src, uint32_t count) const
106 {
107 if (count == 0) {
108 return;
109 }
110 std::vector<SkPoint> pt1;
111 std::vector<SkPoint> pt2;
112 for (uint32_t i = 0; i < count; ++i) {
113 pt1.emplace_back(SkPoint::Make(dst[i].GetX(), dst[i].GetY()));
114 pt2.emplace_back(SkPoint::Make(src[i].GetX(), src[i].GetY()));
115 }
116 skMatrix_.mapPoints(&pt1[0], &pt2[0], count);
117 dst.clear();
118 for (uint32_t i = 0; i < count; ++i) {
119 dst.emplace_back(Point(pt1[i].fX, pt1[i].fY));
120 }
121 }
122
MapRect(Rect & dst,const Rect & src) const123 bool SkiaMatrix::MapRect(Rect& dst, const Rect& src) const
124 {
125 SkRect skSrc = SkRect::MakeXYWH(src.GetLeft(), src.GetTop(), src.GetWidth(), src.GetHeight());
126 SkRect skDst;
127 if (skMatrix_.mapRect(&skDst, skSrc)) {
128 dst = Rect(skDst.fLeft, skDst.fTop, skDst.fRight, skDst.fBottom);
129 return true;
130 }
131 return false;
132 }
133
Set(int index,scalar value)134 void SkiaMatrix::Set(int index, scalar value)
135 {
136 skMatrix_.set(index, value);
137 }
138
Get(int index) const139 scalar SkiaMatrix::Get(int index) const
140 {
141 return skMatrix_.get(index);
142 }
143
GetAll(std::array<scalar,MatrixImpl::MATRIX_SIZE> & buffer) const144 void SkiaMatrix::GetAll(std::array<scalar, MatrixImpl::MATRIX_SIZE>& buffer) const
145 {
146 skMatrix_.get9(buffer.data());
147 }
148 } // namespace Drawing
149 } // namespace Rosen
150 } // namespace OHOS
151