Matrix.h revision 8ce00301a023eecaeb8891ce906f67b513ebb42a
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///////////////////////////////////////////////////////////////////////////////
30// Classes
31///////////////////////////////////////////////////////////////////////////////
32
33class ANDROID_API Matrix4 {
34public:
35    float data[16];
36
37    enum Entry {
38        kScaleX = 0,
39        kSkewY = 1,
40        kPerspective0 = 3,
41        kSkewX = 4,
42        kScaleY = 5,
43        kPerspective1 = 7,
44        kScaleZ = 10,
45        kTranslateX = 12,
46        kTranslateY = 13,
47        kTranslateZ = 14,
48        kPerspective2 = 15
49    };
50
51    // NOTE: The flags from kTypeIdentity to kTypePerspective
52    //       must be kept in sync with the type flags found
53    //       in SkMatrix
54    enum Type {
55        kTypeIdentity = 0,
56        kTypeTranslate = 0x1,
57        kTypeScale = 0x2,
58        kTypeAffine = 0x4,
59        kTypePerspective = 0x8,
60        kTypeRectToRect = 0x10,
61        kTypeUnknown = 0x20,
62    };
63
64    static const int sGeometryMask = 0xf;
65
66    Matrix4() {
67        loadIdentity();
68    }
69
70    Matrix4(const float* v) {
71        load(v);
72    }
73
74    Matrix4(const Matrix4& v) {
75        load(v);
76    }
77
78    Matrix4(const SkMatrix& v) {
79        load(v);
80    }
81
82    void loadIdentity();
83
84    void load(const float* v);
85    void load(const Matrix4& v);
86    void load(const SkMatrix& v);
87
88    void loadInverse(const Matrix4& v);
89
90    void loadTranslate(float x, float y, float z);
91    void loadScale(float sx, float sy, float sz);
92    void loadSkew(float sx, float sy);
93    void loadRotate(float angle);
94    void loadRotate(float angle, float x, float y, float z);
95    void loadMultiply(const Matrix4& u, const Matrix4& v);
96
97    void loadOrtho(float left, float right, float bottom, float top, float near, float far);
98
99    uint32_t getType() const;
100
101    void multiply(const Matrix4& v) {
102        Matrix4 u;
103        u.loadMultiply(*this, v);
104        load(u);
105    }
106
107    void multiply(float v);
108
109    void translate(float x, float y, float z) {
110        Matrix4 u;
111        u.loadTranslate(x, y, z);
112        multiply(u);
113    }
114
115    void scale(float sx, float sy, float sz) {
116        Matrix4 u;
117        u.loadScale(sx, sy, sz);
118        multiply(u);
119    }
120
121    void skew(float sx, float sy) {
122        Matrix4 u;
123        u.loadSkew(sx, sy);
124        multiply(u);
125    }
126
127    void rotate(float angle, float x, float y, float z) {
128        Matrix4 u;
129        u.loadRotate(angle, x, y, z);
130        multiply(u);
131    }
132
133    /**
134     * If the matrix is identity or translate and/or scale.
135     */
136    bool isSimple() const;
137    bool isPureTranslate() const;
138    bool isIdentity() const;
139    bool isPerspective() const;
140    bool rectToRect() const;
141
142    bool changesBounds() const;
143
144    void copyTo(float* v) const;
145    void copyTo(SkMatrix& v) const;
146
147    void mapRect(Rect& r) const;
148    void mapPoint(float& x, float& y) const;
149
150    float getTranslateX();
151    float getTranslateY();
152
153    void dump() const;
154
155private:
156    mutable uint32_t mType;
157
158    inline float get(int i, int j) const {
159        return data[i * 4 + j];
160    }
161
162    inline void set(int i, int j, float v) {
163        data[i * 4 + j] = v;
164    }
165
166    uint32_t getGeometryType() const;
167
168}; // class Matrix4
169
170///////////////////////////////////////////////////////////////////////////////
171// Types
172///////////////////////////////////////////////////////////////////////////////
173
174typedef Matrix4 mat4;
175
176}; // namespace uirenderer
177}; // namespace android
178
179#endif // ANDROID_HWUI_MATRIX_H
180