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 #ifndef MATRIX_H 17 #define MATRIX_H 18 19 #include <array> 20 #include <iostream> 21 22 #include "drawing/engine_adapter/impl_interface/matrix_impl.h" 23 #include "utils/drawing_macros.h" 24 #include "utils/matrix44.h" 25 #include "utils/scalar.h" 26 27 namespace OHOS { 28 namespace Rosen { 29 namespace Drawing { 30 class DRAWING_API Matrix { 31 public: 32 // Matrix is a 3x3 float type matrix. 33 static constexpr int MATRIX_SIZE = 9; 34 using Buffer = std::array<scalar, MATRIX_SIZE>; 35 36 enum Index { 37 SCALE_X, 38 SKEW_X, 39 TRANS_X, 40 SKEW_Y, 41 SCALE_Y, 42 TRANS_Y, 43 PERSP_0, 44 PERSP_1, 45 PERSP_2, 46 }; 47 48 Matrix(); 49 Matrix(const Matrix& matrix); 50 Matrix& operator=(const Matrix& matrix); ~Matrix()51 virtual ~Matrix() {} 52 void Rotate(scalar degree, scalar px, scalar py); 53 void Translate(scalar dx, scalar dy); 54 void Scale(scalar sx, scalar sy, scalar px, scalar py); 55 void SetScale(scalar sx, scalar sy); 56 void SetScaleTranslate(scalar sx, scalar sy, scalar dx, scalar dy); 57 /* 58 * @brief Sets Matrix to Matrix multiplied by Matrix constructed 59 * from rotating by degrees about pivot point(0,0). 60 * @param degree Angle of axes relative to upright axes. 61 */ 62 void PreRotate(scalar degree); 63 64 void PostRotate(scalar degree); 65 66 /* 67 * @brief Sets Matrix to Matrix constructed from rotating by degrees 68 * about pivot point(px,py), multiplied by Matrix. 69 * @param degree Angle of axes relative to upright axes. 70 * @param px pivot on x-axis 71 * @param px pivot on y-axis 72 */ 73 void PostRotate(scalar degree, scalar px, scalar py); 74 75 /* 76 * @brief Sets Matrix to Matrix constructed from translation (dx, dy) multiplied by Matrix. 77 * @param dx X-axis translation after applying Matrix. 78 * @param dy Y-axis translation after applying Matrix. 79 */ 80 void PreTranslate(scalar dx, scalar dy); 81 82 void PostTranslate(scalar dx, scalar dy); 83 84 /* 85 * @brief Sets Matrix to Matrix multiplied by Matrix constructed 86 * from scaling by (sx, sy) about pivot point (0, 0). 87 * @param sx Horizontal scale factor. 88 * @param sy Vertical scale factor. 89 */ 90 void PreScale(scalar sx, scalar sy); 91 92 void PostScale(scalar sx, scalar sy); 93 94 /* 95 * @brief Sets Matrix to Matrix constructed from scaling by (sx, sy) 96 * about pivot point(px,py), multiplied by Matrix. 97 * @param sx horizontal scale factor 98 * @param sy vertical scale factor 99 * @param px pivot on x-axis 100 * @param px pivot on y-axis 101 */ 102 void PostScale(scalar sx, scalar sy, scalar px, scalar py); 103 104 /* 105 * @brief Sets Matrix to Matrix other multiplied by Matrix. 106 * @param other Matrix on left side of multiply expression. 107 */ 108 void PreConcat(const Matrix& other); 109 110 /* 111 * @brief Sets Matrix to Matrix other multiplied by Matrix44. 112 * @param other Matrix on left side of multiply expression. 113 */ 114 void PreConcat(const Matrix44& matrix44); 115 116 /* 117 * @brief Sets Matrix to Matrix other multiplied by Matrix. 118 * @param other Matrix on right side of multiply expression. 119 */ 120 void PostConcat(const Matrix& other); 121 122 /* 123 * @brief Sets Matrix to Matrix other multiplied by Matrix44. 124 * @param other Matrix on right side of multiply expression. 125 */ 126 void PostConcat(const Matrix44& matrix44); 127 128 /* 129 * @brief Sets inverse to the inverse of Matrix. 130 * @param inverse To store to inverse Matrix, may be nullptr. 131 * @return Return true if Matrix can be inverted, otherwise return false. 132 */ 133 bool Invert(Matrix& inverse) const; 134 Matrix operator*(const Matrix& other); 135 136 /* 137 * @brief Compares Matrix and other. 138 * @param other To compare Matrix. 139 * @return True if Matrix and other are numerically equal. 140 */ 141 bool operator==(const Matrix& other) const; 142 void SetMatrix(scalar scaleX, scalar skewX, scalar transX, scalar skewY, scalar scaleY, scalar transY, 143 scalar persp0, scalar persp1, scalar persp2); 144 void MapPoints(std::vector<Point>& dst, const std::vector<Point>& src, uint32_t count) const; 145 146 /* 147 * @brief Sets dst to bounds of src corners mapped by Matrix. 148 * @param dst Storage for bounds of map. 149 * @param src To map. 150 * @return True if dst is equivalent to mapped src. 151 */ 152 bool MapRect(Rect& dst, const Rect& src) const; 153 154 /* 155 * @brief Sets Matrix value. 156 * @param index One of Index. 157 * @param value Scalar to store in Matrix. 158 */ 159 void Set(Index index, scalar value); 160 scalar Get(int index) const; 161 162 /* 163 * @brief Copies nine scalar values contained by Matrix into buffer. 164 * @param buffer Storage for nine scalar values 165 */ 166 void GetAll(Buffer& buffer) const; 167 168 /* 169 * @brief Copies nine scalar values contained by Matrix from buffer. 170 * @param buffer Storage for nine scalar values 171 */ 172 void SetAll(Buffer& buffer); 173 174 template<typename T> GetImpl()175 T* GetImpl() const 176 { 177 return matrixImplPtr->DowncastingTo<T>(); 178 } 179 180 template<typename T> GetImplPtr()181 const T* GetImplPtr() const 182 { 183 return reinterpret_cast<const T*>(matrixImplPtr.get()); 184 } 185 186 /* 187 * Returns true if matrix is Identity. Identity matrix is: 188 * | 1 0 0 | 189 * | 0 1 0 | 190 * | 0 0 1 | 191 */ 192 bool IsIdentity() const; 193 194 void PreRotate(scalar degree, scalar px, scalar py); 195 void PreScale(scalar sx, scalar sy, scalar px, scalar py); 196 void Reset(); 197 198 bool GetMinMaxScales(scalar scaleFactors[2]); 199 bool HasPerspective() const; 200 201 private: 202 std::shared_ptr<MatrixImpl> matrixImplPtr; 203 }; 204 } // namespace Drawing 205 } // namespace Rosen 206 } // namespace OHOS 207 #endif 208