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/scalar.h" 24 25 namespace OHOS { 26 namespace Rosen { 27 namespace Drawing { 28 class Matrix { 29 public: 30 // Matrix is a 3x3 float type matrix. 31 static constexpr int MATRIX_SIZE = 9; 32 using Buffer = std::array<scalar, MATRIX_SIZE>; 33 34 enum Index { 35 SCALE_X, 36 SKEW_X, 37 TRANS_X, 38 SKEW_Y, 39 SCALE_Y, 40 TRANS_Y, 41 PERSP_0, 42 PERSP_1, 43 PERSP_2, 44 }; 45 46 Matrix(); ~Matrix()47 virtual ~Matrix() {} 48 void Rotate(scalar degree, scalar px, scalar py); 49 void Translate(scalar dx, scalar dy); 50 void Scale(scalar sx, scalar sy, scalar px, scalar py); 51 52 /* 53 * @brief Sets Matrix to Matrix multiplied by Matrix constructed 54 * from rotating by degrees about pivot point(0,0). 55 * @param degree Angle of axes relative to upright axes. 56 */ 57 void PreRotate(scalar degree); 58 59 /* 60 * @brief Sets Matrix to Matrix constructed from translation (dx, dy) multiplied by Matrix. 61 * @param dx X-axis translation after applying Matrix. 62 * @param dy Y-axis translation after applying Matrix. 63 */ 64 void PreTranslate(scalar dx, scalar dy); 65 66 /* 67 * @brief Sets Matrix to Matrix multiplied by Matrix constructed 68 * from scaling by (sx, sy) about pivot point (0, 0). 69 * @param sx Horizontal scale factor. 70 * @param sy Vertical scale factor. 71 */ 72 void PreScale(scalar sx, scalar sy); 73 74 /* 75 * @brief Sets Matrix to Matrix other multiplied by Matrix. 76 * @param other Matrix on left side of multiply expression. 77 */ 78 void PreConcat(const Matrix& other); 79 80 /* 81 * @brief Sets inverse to the inverse of Matrix. 82 * @param inverse To store to inverse Matrix, may be nullptr. 83 * @return Return true if Matrix can be inverted, otherwise return false. 84 */ 85 bool Invert(Matrix& inverse) const; 86 Matrix operator*(const Matrix& other); 87 88 /* 89 * @brief Compares Matrix and other. 90 * @param other To compare Matrix. 91 * @return True if Matrix and other are numerically equal. 92 */ 93 bool operator==(const Matrix& other) const; 94 void SetMatrix(scalar scaleX, scalar skewX, scalar transX, scalar skewY, scalar scaleY, scalar transY, 95 scalar persp0, scalar persp1, scalar persp2); 96 void MapPoints(std::vector<Point>& dst, const std::vector<Point>& src, uint32_t count) const; 97 98 /* 99 * @brief Sets dst to bounds of src corners mapped by Matrix. 100 * @param dst Storage for bounds of map. 101 * @param src To map. 102 * @return True if dst is equivalent to mapped src. 103 */ 104 bool MapRect(Rect& dst, const Rect& src) const; 105 106 /* 107 * @brief Sets Matrix value. 108 * @param index One of Index. 109 * @param value Scalar to store in Matrix. 110 */ 111 void Set(Index index, scalar value); 112 scalar Get(int index) const; 113 114 /* 115 * @brief Copies nine scalar values contained by Matrix into buffer. 116 * @param buffer Storage for nine scalar values 117 */ 118 void GetAll(Buffer& buffer) const; 119 template<typename T> GetImpl()120 const std::shared_ptr<T> GetImpl() const 121 { 122 return matrixImplPtr->DowncastingTo<T>(); 123 } 124 125 private: 126 std::shared_ptr<MatrixImpl> matrixImplPtr; 127 }; 128 } // namespace Drawing 129 } // namespace Rosen 130 } // namespace OHOS 131 #endif 132