blob: 00ca050dca6ff31faa5d9e1da6280883468ab65a [file] [log] [blame]
Romain Guy08ae3172010-06-21 19:35:50 -07001/*
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
Romain Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_MATRIX_H
18#define ANDROID_HWUI_MATRIX_H
Romain Guy08ae3172010-06-21 19:35:50 -070019
Romain Guyf6a11b82010-06-23 17:47:49 -070020#include <SkMatrix.h>
21
Romain Guy71e36aa2011-10-12 14:11:32 -070022#include <cutils/compiler.h>
23
Romain Guy9d5316e2010-06-24 19:30:36 -070024#include "Rect.h"
25
Romain Guy08ae3172010-06-21 19:35:50 -070026namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070027namespace uirenderer {
Romain Guy08ae3172010-06-21 19:35:50 -070028
Chris Craik28ce94a2013-05-31 11:38:03 -070029#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
Romain Guy08ae3172010-06-21 19:35:50 -070035///////////////////////////////////////////////////////////////////////////////
36// Classes
37///////////////////////////////////////////////////////////////////////////////
38
Romain Guy71e36aa2011-10-12 14:11:32 -070039class ANDROID_API Matrix4 {
Romain Guy08ae3172010-06-21 19:35:50 -070040public:
Romain Guy7ae7ac42010-06-25 13:46:18 -070041 float data[16];
Romain Guyc7d53492010-06-25 13:41:57 -070042
Romain Guyaf28b512010-08-12 14:34:44 -070043 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
Romain Guy8ce00302013-01-15 18:51:42 -080057 // 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,
Chris Craik996fe652013-09-20 17:13:18 -070067 kTypeUnknown = 0x20,
Romain Guy8ce00302013-01-15 18:51:42 -080068 };
69
70 static const int sGeometryMask = 0xf;
71
Romain Guy7ae7ac42010-06-25 13:46:18 -070072 Matrix4() {
73 loadIdentity();
74 }
Romain Guy08ae3172010-06-21 19:35:50 -070075
Romain Guy7ae7ac42010-06-25 13:46:18 -070076 Matrix4(const float* v) {
77 load(v);
78 }
Romain Guy08ae3172010-06-21 19:35:50 -070079
Romain Guy7ae7ac42010-06-25 13:46:18 -070080 Matrix4(const Matrix4& v) {
81 load(v);
82 }
Romain Guy08ae3172010-06-21 19:35:50 -070083
Romain Guy7ae7ac42010-06-25 13:46:18 -070084 Matrix4(const SkMatrix& v) {
85 load(v);
86 }
Romain Guyf6a11b82010-06-23 17:47:49 -070087
Romain Guy3b753822013-03-05 10:27:35 -080088 float operator[](int index) const {
89 return data[index];
90 }
91
92 float& operator[](int index) {
93 mType = kTypeUnknown;
94 return data[index];
95 }
96
97 Matrix4& operator=(const SkMatrix& v) {
98 load(v);
99 return *this;
100 }
101
Romain Guybd3055f2013-03-13 16:14:47 -0700102 friend bool operator==(const Matrix4& a, const Matrix4& b) {
103 return !memcmp(&a.data[0], &b.data[0], 16 * sizeof(float));
104 }
105
106 friend bool operator!=(const Matrix4& a, const Matrix4& b) {
107 return !(a == b);
108 }
109
Romain Guy7ae7ac42010-06-25 13:46:18 -0700110 void loadIdentity();
Romain Guy08ae3172010-06-21 19:35:50 -0700111
Romain Guy7ae7ac42010-06-25 13:46:18 -0700112 void load(const float* v);
113 void load(const Matrix4& v);
114 void load(const SkMatrix& v);
Romain Guy08ae3172010-06-21 19:35:50 -0700115
Romain Guy079ba2c2010-07-16 14:12:24 -0700116 void loadInverse(const Matrix4& v);
117
Romain Guy7ae7ac42010-06-25 13:46:18 -0700118 void loadTranslate(float x, float y, float z);
119 void loadScale(float sx, float sy, float sz);
Romain Guy807daf72011-01-18 11:19:19 -0800120 void loadSkew(float sx, float sy);
Romain Guy8ce00302013-01-15 18:51:42 -0800121 void loadRotate(float angle);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700122 void loadRotate(float angle, float x, float y, float z);
123 void loadMultiply(const Matrix4& u, const Matrix4& v);
Chris Craikf57776b2013-10-25 18:30:17 -0700124 void loadFrustum(float left, float top, float right, float bottom, float near, float far);
125 void loadLookAt(float eyeX, float eyeY, float eyeZ,
126 float centerX, float centerY, float centerZ,
127 float upX, float upY, float upZ);
Romain Guy08ae3172010-06-21 19:35:50 -0700128
Romain Guy7ae7ac42010-06-25 13:46:18 -0700129 void loadOrtho(float left, float right, float bottom, float top, float near, float far);
Romain Guy08ae3172010-06-21 19:35:50 -0700130
Romain Guyf6bed4f2013-06-20 17:52:07 -0700131 uint8_t getType() const;
Romain Guy8ce00302013-01-15 18:51:42 -0800132
Romain Guy7ae7ac42010-06-25 13:46:18 -0700133 void multiply(const Matrix4& v) {
134 Matrix4 u;
135 u.loadMultiply(*this, v);
136 load(u);
137 }
Romain Guy08ae3172010-06-21 19:35:50 -0700138
Romain Guyddb80be2010-09-20 19:04:33 -0700139 void multiply(float v);
140
Chris Craikf57776b2013-10-25 18:30:17 -0700141 void translate(float x, float y, float z = 0) {
Romain Guyf6bed4f2013-06-20 17:52:07 -0700142 if ((getType() & sGeometryMask) <= kTypeTranslate) {
Romain Guy4c2547f2013-06-11 16:19:24 -0700143 data[kTranslateX] += x;
144 data[kTranslateY] += y;
Chris Craikf57776b2013-10-25 18:30:17 -0700145 data[kTranslateZ] += z;
Romain Guy4c2547f2013-06-11 16:19:24 -0700146 } else {
147 // Doing a translation will only affect the translate bit of the type
148 // Save the type
Romain Guyf6bed4f2013-06-20 17:52:07 -0700149 uint8_t type = mType;
Romain Guy4c2547f2013-06-11 16:19:24 -0700150
151 Matrix4 u;
Chris Craikf57776b2013-10-25 18:30:17 -0700152 u.loadTranslate(x, y, z);
Romain Guy4c2547f2013-06-11 16:19:24 -0700153 multiply(u);
154
155 // Restore the type and fix the translate bit
156 mType = type;
157 if (data[kTranslateX] != 0.0f || data[kTranslateY] != 0.0f) {
158 mType |= kTypeTranslate;
159 } else {
160 mType &= ~kTypeTranslate;
161 }
162 }
Romain Guy7ae7ac42010-06-25 13:46:18 -0700163 }
Romain Guy08ae3172010-06-21 19:35:50 -0700164
Romain Guy7ae7ac42010-06-25 13:46:18 -0700165 void scale(float sx, float sy, float sz) {
166 Matrix4 u;
167 u.loadScale(sx, sy, sz);
168 multiply(u);
169 }
Romain Guy08ae3172010-06-21 19:35:50 -0700170
Romain Guy807daf72011-01-18 11:19:19 -0800171 void skew(float sx, float sy) {
172 Matrix4 u;
173 u.loadSkew(sx, sy);
174 multiply(u);
175 }
176
Romain Guy7ae7ac42010-06-25 13:46:18 -0700177 void rotate(float angle, float x, float y, float z) {
178 Matrix4 u;
179 u.loadRotate(angle, x, y, z);
180 multiply(u);
181 }
Romain Guy08ae3172010-06-21 19:35:50 -0700182
Romain Guy8ce00302013-01-15 18:51:42 -0800183 /**
184 * If the matrix is identity or translate and/or scale.
185 */
Chris Craik710f46d2012-09-17 17:25:49 -0700186 bool isSimple() const;
Romain Guy8ce00302013-01-15 18:51:42 -0800187 bool isPureTranslate() const;
Chris Craik710f46d2012-09-17 17:25:49 -0700188 bool isIdentity() const;
Romain Guya3dc55f2012-09-28 13:55:44 -0700189 bool isPerspective() const;
Romain Guy8ce00302013-01-15 18:51:42 -0800190 bool rectToRect() const;
Chris Craikd965bc52013-09-16 14:47:13 -0700191 bool positiveScale() const;
Romain Guy6620c6d2010-12-06 18:07:02 -0800192
Chris Craik710f46d2012-09-17 17:25:49 -0700193 bool changesBounds() const;
Romain Guye8cb9c142010-10-04 14:14:11 -0700194
Romain Guy7ae7ac42010-06-25 13:46:18 -0700195 void copyTo(float* v) const;
196 void copyTo(SkMatrix& v) const;
Romain Guy08ae3172010-06-21 19:35:50 -0700197
Chris Craikf57776b2013-10-25 18:30:17 -0700198 void mapPoint3d(Vector3& vec) const;
199 void mapPoint(float& x, float& y) const; // 2d only
200 void mapRect(Rect& r) const; // 2d only
Romain Guy9d5316e2010-06-24 19:30:36 -0700201
Romain Guy624234f2013-03-05 16:43:31 -0800202 float getTranslateX() const;
203 float getTranslateY() const;
Romain Guybd6b79b2010-06-26 00:13:53 -0700204
Romain Guy3b753822013-03-05 10:27:35 -0800205 void decomposeScale(float& sx, float& sy) const;
206
Romain Guy7ae7ac42010-06-25 13:46:18 -0700207 void dump() const;
Romain Guy08ae3172010-06-21 19:35:50 -0700208
Romain Guyc74f45a2013-02-26 19:10:14 -0800209 static const Matrix4& identity();
210
Romain Guyf6a11b82010-06-23 17:47:49 -0700211private:
Romain Guyf6bed4f2013-06-20 17:52:07 -0700212 mutable uint8_t mType;
Romain Guyaf28b512010-08-12 14:34:44 -0700213
Romain Guy08ae3172010-06-21 19:35:50 -0700214 inline float get(int i, int j) const {
Romain Guyc7d53492010-06-25 13:41:57 -0700215 return data[i * 4 + j];
Romain Guy08ae3172010-06-21 19:35:50 -0700216 }
217
218 inline void set(int i, int j, float v) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700219 data[i * 4 + j] = v;
Romain Guy08ae3172010-06-21 19:35:50 -0700220 }
Romain Guy8ce00302013-01-15 18:51:42 -0800221
Romain Guyf6bed4f2013-06-20 17:52:07 -0700222 uint8_t getGeometryType() const;
Romain Guy8ce00302013-01-15 18:51:42 -0800223
Romain Guy08ae3172010-06-21 19:35:50 -0700224}; // class Matrix4
225
226///////////////////////////////////////////////////////////////////////////////
227// Types
228///////////////////////////////////////////////////////////////////////////////
229
230typedef Matrix4 mat4;
231
Romain Guy9d5316e2010-06-24 19:30:36 -0700232}; // namespace uirenderer
Romain Guy08ae3172010-06-21 19:35:50 -0700233}; // namespace android
234
Romain Guy5b3b3522010-10-27 18:57:51 -0700235#endif // ANDROID_HWUI_MATRIX_H