Debian Codegraph @

libs/hwui/Matrix.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #pragma once
18
19 #include "Rect.h"
20
21 #include <SkMatrix.h>
22 #include <cutils/compiler.h>
23 #include <iomanip>
24 #include <ostream>
25
26 namespace android {
27 namespace uirenderer {
28
29 #define SK_MATRIX_STRING "[%.2f %.2f %.2f] [%.2f %.2f %.2f] [%.2f %.2f %.2f]"
30 #define SK_MATRIX_STRING_V "[%.9f %.9f %.9f] [%.9f %.9f %.9f] [%.9f %.9f %.9f]"
31 #define SK_MATRIX_ARGS(m) \
32 (m)->get(0), (m)->get(1), (m)->get(2), (m)->get(3), (m)->get(4), (m)->get(5), (m)->get(6), \
33 (m)->get(7), (m)->get(8)
34
35 #define MATRIX_4_STRING \
36 "[%.2f %.2f %.2f %.2f] [%.2f %.2f %.2f %.2f]" \
37 " [%.2f %.2f %.2f %.2f] [%.2f %.2f %.2f %.2f]"
38 #define MATRIX_4_ARGS(m) \
39 (m)->data[0], (m)->data[4], (m)->data[8], (m)->data[12], (m)->data[1], (m)->data[5], \
40 (m)->data[9], (m)->data[13], (m)->data[2], (m)->data[6], (m)->data[10], (m)->data[14], \
41 (m)->data[3], (m)->data[7], (m)->data[11], (m)->data[15]
42
43 ///////////////////////////////////////////////////////////////////////////////
44 // Classes
45 ///////////////////////////////////////////////////////////////////////////////
46
47 class Matrix4 {
48 public:
49 float data[16];
50
51 enum Entry {
52 kScaleX = 0,
53 kSkewY = 1,
54 kPerspective0 = 3,
55 kSkewX = 4,
56 kScaleY = 5,
57 kPerspective1 = 7,
58 kScaleZ = 10,
59 kTranslateX = 12,
60 kTranslateY = 13,
61 kTranslateZ = 14,
62 kPerspective2 = 15
63 };
64
65 // NOTE: The flags from kTypeIdentity to kTypePerspective
66 // must be kept in sync with the type flags found
67 // in SkMatrix
68 enum Type {
69 kTypeIdentity = 0,
70 kTypeTranslate = 0x1,
71 kTypeScale = 0x2,
72 kTypeAffine = 0x4,
73 kTypePerspective = 0x8,
74 kTypeRectToRect = 0x10,
75 kTypeUnknown = 0x20,
76 };
77
78 static const int sGeometryMask = 0xf;
79
80 Matrix4() { loadIdentity(); }
81
82 explicit Matrix4(const float* v) { load(v); }
83
84 Matrix4(const SkMatrix& v) { // NOLINT(google-explicit-constructor)
85 load(v);
86 }
87
88 float operator[](int index) const { return data[index]; }
89
90 float& operator[](int index) {
91 mType = kTypeUnknown;
92 return data[index];
93 }
94
95 Matrix4& operator=(const SkMatrix& v) {
96 load(v);
97 return *this;
98 }
99
100 friend bool operator==(const Matrix4& a, const Matrix4& b) {
101 return !memcmp(&a.data[0], &b.data[0], 16 * sizeof(float));
102 }
103
104 friend bool operator!=(const Matrix4& a, const Matrix4& b) { return !(a == b); }
105
106 void loadIdentity();
107
108 void load(const float* v);
109 void load(const SkMatrix& v);
110
111 void loadInverse(const Matrix4& v);
112
113 void loadTranslate(float x, float y, float z);
114 void loadScale(float sx, float sy, float sz);
115 void loadSkew(float sx, float sy);
116 void loadRotate(float angle);
117 void loadRotate(float angle, float x, float y, float z);
118 void loadMultiply(const Matrix4& u, const Matrix4& v);
119
120 void loadOrtho(float left, float right, float bottom, float top, float near, float far);
121 void loadOrtho(int width, int height) { loadOrtho(0, width, height, 0, -1, 1); }
122
123 uint8_t getType() const;
124
125 void multiplyInverse(const Matrix4& v) {
126 Matrix4 inv;
127 inv.loadInverse(v);
128 multiply(inv);
129 }
130
131 void multiply(const Matrix4& v) {
132 if (!v.isIdentity()) {
133 Matrix4 u;
134 u.loadMultiply(*this, v);
135 *this = u;
136 }
137 }
138
139 void multiply(float v);
140
141 void translate(float x, float y, float z = 0) {
142 if ((getType() & sGeometryMask) <= kTypeTranslate) {
143 data[kTranslateX] += x;
144 data[kTranslateY] += y;
145 data[kTranslateZ] += z;
146 mType |= kTypeUnknown;
147 } else {
148 // Doing a translation will only affect the translate bit of the type
149 // Save the type
150 uint8_t type = mType;
151
152 Matrix4 u;
153 u.loadTranslate(x, y, z);
154 multiply(u);
155
156 // Restore the type and fix the translate bit
157 mType = type;
158 if (data[kTranslateX] != 0.0f || data[kTranslateY] != 0.0f) {
159 mType |= kTypeTranslate;
160 } else {
161 mType &= ~kTypeTranslate;
162 }
163 }
164 }
165
166 void scale(float sx, float sy, float sz) {
167 Matrix4 u;
168 u.loadScale(sx, sy, sz);
169 multiply(u);
170 }
171
172 void skew(float sx, float sy) {
173 Matrix4 u;
174 u.loadSkew(sx, sy);
175 multiply(u);
176 }
177
178 void rotate(float angle, float x, float y, float z) {
179 Matrix4 u;
180 u.loadRotate(angle, x, y, z);
181 multiply(u);
182 }
183
184 /**
185 * If the matrix is identity or translate and/or scale.
186 */
187 bool isSimple() const;
188 bool isPureTranslate() const;
189 bool isIdentity() const;
190 bool isPerspective() const;
191 bool rectToRect() const;
192 bool positiveScale() const;
193
194 bool changesBounds() const;
195
196 void copyTo(float* v) const;
197 void copyTo(SkMatrix& v) const;
198
199 float mapZ(const Vector3& orig) const;
200 void mapPoint3d(Vector3& vec) const;
201 void mapPoint(float& x, float& y) const; // 2d only
202 void mapRect(Rect& r) const; // 2d only
203
204 float getTranslateX() const;
205 float getTranslateY() const;
206
207 void decomposeScale(float& sx, float& sy) const;
208
209 void dump(const char* label = nullptr) const;
210
211 friend std::ostream& operator<<(std::ostream& os, const Matrix4& matrix) {
212 if (matrix.isSimple()) {
213 os << "offset " << matrix.getTranslateX() << "x" << matrix.getTranslateY();
214 if (!matrix.isPureTranslate()) {
215 os << ", scale " << matrix[kScaleX] << "x" << matrix[kScaleY];
216 }
217 } else {
218 os << "[" << matrix[0];
219 for (int i = 1; i < 16; i++) {
220 os << ", " << matrix[i];
221 }
222 os << "]";
223 }
224 return os;
225 }
226
227 static const Matrix4& identity();
228
229 void invalidateType() { mType = kTypeUnknown; }
230
231 private:
232 mutable uint8_t mType;
233
234 inline float get(int i, int j) const { return data[i * 4 + j]; }
235
236 inline void set(int i, int j, float v) { data[i * 4 + j] = v; }
237
238 uint8_t getGeometryType() const;
239
240 }; // class Matrix4
241
242 ///////////////////////////////////////////////////////////////////////////////
243 // Types
244 ///////////////////////////////////////////////////////////////////////////////
245
246 typedef Matrix4 mat4;
247
248 } // namespace uirenderer
249 } // namespace android