Matrix.h revision d965bc5823d878a3fd056b8a95fb4eb578ed3fe4
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
26namespace android {
27namespace uirenderer {
28
29#define MATRIX_STRING "[%.2f %.2f %.2f] [%.2f %.2f %.2f] [%.2f %.2f %.2f]"
30#define 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///////////////////////////////////////////////////////////////////////////////
36// Classes
37///////////////////////////////////////////////////////////////////////////////
38
39class ANDROID_API Matrix4 {
40public:
41    float data[16];
42
43    enum Entry {
44        kScaleX = 0,
45        kSkewY = 1,
46        kPerspective0 = 3,
47        kSkewX = 4,
48        kScaleY = 5,
49        kPerspective1 = 7,
50        kScaleZ = 10,
51        kTranslateX = 12,
52        kTranslateY = 13,
53        kTranslateZ = 14,
54        kPerspective2 = 15
55    };
56
57    // NOTE: The flags from kTypeIdentity to kTypePerspective
58    //       must be kept in sync with the type flags found
59    //       in SkMatrix
60    enum Type {
61        kTypeIdentity = 0,
62        kTypeTranslate = 0x1,
63        kTypeScale = 0x2,
64        kTypeAffine = 0x4,
65        kTypePerspective = 0x8,
66        kTypeRectToRect = 0x10,
67        kTypePositiveScale = 0x20,
68        kTypeUnknown = 0x40,
69    };
70
71    static const int sGeometryMask = 0xf;
72
73    Matrix4() {
74        loadIdentity();
75    }
76
77    Matrix4(const float* v) {
78        load(v);
79    }
80
81    Matrix4(const Matrix4& v) {
82        load(v);
83    }
84
85    Matrix4(const SkMatrix& v) {
86        load(v);
87    }
88
89    float operator[](int index) const {
90        return data[index];
91    }
92
93    float& operator[](int index) {
94        mType = kTypeUnknown;
95        return data[index];
96    }
97
98    Matrix4& operator=(const SkMatrix& v) {
99        load(v);
100        return *this;
101    }
102
103    friend bool operator==(const Matrix4& a, const Matrix4& b) {
104        return !memcmp(&a.data[0], &b.data[0], 16 * sizeof(float));
105    }
106
107    friend bool operator!=(const Matrix4& a, const Matrix4& b) {
108        return !(a == b);
109    }
110
111    void loadIdentity();
112
113    void load(const float* v);
114    void load(const Matrix4& v);
115    void load(const SkMatrix& v);
116
117    void loadInverse(const Matrix4& v);
118
119    void loadTranslate(float x, float y, float z);
120    void loadScale(float sx, float sy, float sz);
121    void loadSkew(float sx, float sy);
122    void loadRotate(float angle);
123    void loadRotate(float angle, float x, float y, float z);
124    void loadMultiply(const Matrix4& u, const Matrix4& v);
125
126    void loadOrtho(float left, float right, float bottom, float top, float near, float far);
127
128    uint8_t getType() const;
129
130    void multiply(const Matrix4& v) {
131        Matrix4 u;
132        u.loadMultiply(*this, v);
133        load(u);
134    }
135
136    void multiply(float v);
137
138    void translate(float x, float y) {
139        if ((getType() & sGeometryMask) <= kTypeTranslate) {
140            data[kTranslateX] += x;
141            data[kTranslateY] += y;
142        } else {
143            // Doing a translation will only affect the translate bit of the type
144            // Save the type
145            uint8_t type = mType;
146
147            Matrix4 u;
148            u.loadTranslate(x, y, 0.0f);
149            multiply(u);
150
151            // Restore the type and fix the translate bit
152            mType = type;
153            if (data[kTranslateX] != 0.0f || data[kTranslateY] != 0.0f) {
154                mType |= kTypeTranslate;
155            } else {
156                mType &= ~kTypeTranslate;
157            }
158        }
159    }
160
161    void scale(float sx, float sy, float sz) {
162        Matrix4 u;
163        u.loadScale(sx, sy, sz);
164        multiply(u);
165    }
166
167    void skew(float sx, float sy) {
168        Matrix4 u;
169        u.loadSkew(sx, sy);
170        multiply(u);
171    }
172
173    void rotate(float angle, float x, float y, float z) {
174        Matrix4 u;
175        u.loadRotate(angle, x, y, z);
176        multiply(u);
177    }
178
179    /**
180     * If the matrix is identity or translate and/or scale.
181     */
182    bool isSimple() const;
183    bool isPureTranslate() const;
184    bool isIdentity() const;
185    bool isPerspective() const;
186    bool rectToRect() const;
187    bool positiveScale() const;
188
189    bool changesBounds() const;
190
191    void copyTo(float* v) const;
192    void copyTo(SkMatrix& v) const;
193
194    void mapRect(Rect& r) const;
195    void mapPoint(float& x, float& y) const;
196
197    float getTranslateX() const;
198    float getTranslateY() const;
199
200    void decomposeScale(float& sx, float& sy) const;
201
202    void dump() const;
203
204    static const Matrix4& identity();
205
206private:
207    mutable uint8_t mType;
208
209    inline float get(int i, int j) const {
210        return data[i * 4 + j];
211    }
212
213    inline void set(int i, int j, float v) {
214        data[i * 4 + j] = v;
215    }
216
217    uint8_t getGeometryType() const;
218
219}; // class Matrix4
220
221///////////////////////////////////////////////////////////////////////////////
222// Types
223///////////////////////////////////////////////////////////////////////////////
224
225typedef Matrix4 mat4;
226
227}; // namespace uirenderer
228}; // namespace android
229
230#endif // ANDROID_HWUI_MATRIX_H
231