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 #ifndef ANDROID_HWUI_MATRIX_H
18 #define ANDROID_HWUI_MATRIX_H
19 
20 #include <SkMatrix.h>
21 
22 #include <cutils/compiler.h>
23 
24 #include "Rect.h"
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_ARGS(m) \
31     (m)->get(0), (m)->get(1), (m)->get(2), \
32     (m)->get(3), (m)->get(4), (m)->get(5), \
33     (m)->get(6), (m)->get(7), (m)->get(8)
34 
35 #define MATRIX_4_STRING "[%.2f %.2f %.2f %.2f] [%.2f %.2f %.2f %.2f]" \
36     " [%.2f %.2f %.2f %.2f] [%.2f %.2f %.2f %.2f]"
37 #define MATRIX_4_ARGS(m) \
38     (m)->data[0], (m)->data[4], (m)->data[8], (m)->data[12], \
39     (m)->data[1], (m)->data[5], (m)->data[9], (m)->data[13], \
40     (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 ANDROID_API 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 
Matrix4()80     Matrix4() {
81         loadIdentity();
82     }
83 
Matrix4(const float * v)84     Matrix4(const float* v) {
85         load(v);
86     }
87 
Matrix4(const SkMatrix & v)88     Matrix4(const SkMatrix& v) {
89         load(v);
90     }
91 
92     float operator[](int index) const {
93         return data[index];
94     }
95 
96     float& operator[](int index) {
97         mType = kTypeUnknown;
98         return data[index];
99     }
100 
101     Matrix4& operator=(const SkMatrix& v) {
102         load(v);
103         return *this;
104     }
105 
106     friend bool operator==(const Matrix4& a, const Matrix4& b) {
107         return !memcmp(&a.data[0], &b.data[0], 16 * sizeof(float));
108     }
109 
110     friend bool operator!=(const Matrix4& a, const Matrix4& b) {
111         return !(a == b);
112     }
113 
114     void loadIdentity();
115 
116     void load(const float* v);
117     void load(const Matrix4& v);
118     void load(const SkMatrix& v);
119 
120     void loadInverse(const Matrix4& v);
121 
122     void loadTranslate(float x, float y, float z);
123     void loadScale(float sx, float sy, float sz);
124     void loadSkew(float sx, float sy);
125     void loadRotate(float angle);
126     void loadRotate(float angle, float x, float y, float z);
127     void loadMultiply(const Matrix4& u, const Matrix4& v);
128 
129     void loadOrtho(float left, float right, float bottom, float top, float near, float far);
130 
131     uint8_t getType() const;
132 
multiplyInverse(const Matrix4 & v)133     void multiplyInverse(const Matrix4& v) {
134         Matrix4 inv;
135         inv.loadInverse(v);
136         multiply(inv);
137     }
138 
multiply(const Matrix4 & v)139     void multiply(const Matrix4& v) {
140         Matrix4 u;
141         u.loadMultiply(*this, v);
142         load(u);
143     }
144 
145     void multiply(float v);
146 
147     void translate(float x, float y, float z = 0) {
148         if ((getType() & sGeometryMask) <= kTypeTranslate) {
149             data[kTranslateX] += x;
150             data[kTranslateY] += y;
151             data[kTranslateZ] += z;
152             mType |= kTypeUnknown;
153         } else {
154             // Doing a translation will only affect the translate bit of the type
155             // Save the type
156             uint8_t type = mType;
157 
158             Matrix4 u;
159             u.loadTranslate(x, y, z);
160             multiply(u);
161 
162             // Restore the type and fix the translate bit
163             mType = type;
164             if (data[kTranslateX] != 0.0f || data[kTranslateY] != 0.0f) {
165                 mType |= kTypeTranslate;
166             } else {
167                 mType &= ~kTypeTranslate;
168             }
169         }
170     }
171 
scale(float sx,float sy,float sz)172     void scale(float sx, float sy, float sz) {
173         Matrix4 u;
174         u.loadScale(sx, sy, sz);
175         multiply(u);
176     }
177 
skew(float sx,float sy)178     void skew(float sx, float sy) {
179         Matrix4 u;
180         u.loadSkew(sx, sy);
181         multiply(u);
182     }
183 
rotate(float angle,float x,float y,float z)184     void rotate(float angle, float x, float y, float z) {
185         Matrix4 u;
186         u.loadRotate(angle, x, y, z);
187         multiply(u);
188     }
189 
190     /**
191      * If the matrix is identity or translate and/or scale.
192      */
193     bool isSimple() const;
194     bool isPureTranslate() const;
195     bool isIdentity() const;
196     bool isPerspective() const;
197     bool rectToRect() const;
198     bool positiveScale() const;
199 
200     bool changesBounds() const;
201 
202     void copyTo(float* v) const;
203     void copyTo(SkMatrix& v) const;
204 
205     float mapZ(const Vector3& orig) const;
206     void mapPoint3d(Vector3& vec) const;
207     void mapPoint(float& x, float& y) const; // 2d only
208     void mapRect(Rect& r) const; // 2d only
209 
210     float getTranslateX() const;
211     float getTranslateY() const;
212 
213     void decomposeScale(float& sx, float& sy) const;
214 
215     void dump(const char* label = nullptr) const;
216 
217     static const Matrix4& identity();
218 
219 private:
220     mutable uint8_t mType;
221 
get(int i,int j)222     inline float get(int i, int j) const {
223         return data[i * 4 + j];
224     }
225 
set(int i,int j,float v)226     inline void set(int i, int j, float v) {
227         data[i * 4 + j] = v;
228     }
229 
230     uint8_t getGeometryType() const;
231 
232 }; // class Matrix4
233 
234 ///////////////////////////////////////////////////////////////////////////////
235 // Types
236 ///////////////////////////////////////////////////////////////////////////////
237 
238 typedef Matrix4 mat4;
239 
240 }; // namespace uirenderer
241 }; // namespace android
242 
243 #endif // ANDROID_HWUI_MATRIX_H
244