blob: c017638db895780638d5dfb8514558045c34f231 [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 Craik629f6772014-02-05 13:00:24 -080029#define SK_MATRIX_STRING "[%.2f %.2f %.2f] [%.2f %.2f %.2f] [%.2f %.2f %.2f]"
30#define SK_MATRIX_ARGS(m) \
Chris Craik28ce94a2013-05-31 11:38:03 -070031 (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
Chris Craik629f6772014-02-05 13:00:24 -080035#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
Romain Guy08ae3172010-06-21 19:35:50 -070043///////////////////////////////////////////////////////////////////////////////
44// Classes
45///////////////////////////////////////////////////////////////////////////////
46
Romain Guy71e36aa2011-10-12 14:11:32 -070047class ANDROID_API Matrix4 {
Romain Guy08ae3172010-06-21 19:35:50 -070048public:
Romain Guy7ae7ac42010-06-25 13:46:18 -070049 float data[16];
Romain Guyc7d53492010-06-25 13:41:57 -070050
Romain Guyaf28b512010-08-12 14:34:44 -070051 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
Romain Guy8ce00302013-01-15 18:51:42 -080065 // 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,
Chris Craik996fe652013-09-20 17:13:18 -070075 kTypeUnknown = 0x20,
Romain Guy8ce00302013-01-15 18:51:42 -080076 };
77
78 static const int sGeometryMask = 0xf;
79
Romain Guy7ae7ac42010-06-25 13:46:18 -070080 Matrix4() {
81 loadIdentity();
82 }
Romain Guy08ae3172010-06-21 19:35:50 -070083
Romain Guy7ae7ac42010-06-25 13:46:18 -070084 Matrix4(const float* v) {
85 load(v);
86 }
Romain Guy08ae3172010-06-21 19:35:50 -070087
Romain Guy7ae7ac42010-06-25 13:46:18 -070088 Matrix4(const SkMatrix& v) {
89 load(v);
90 }
Romain Guyf6a11b82010-06-23 17:47:49 -070091
Romain Guy3b753822013-03-05 10:27:35 -080092 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
Romain Guybd3055f2013-03-13 16:14:47 -0700106 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
Romain Guy7ae7ac42010-06-25 13:46:18 -0700114 void loadIdentity();
Romain Guy08ae3172010-06-21 19:35:50 -0700115
Romain Guy7ae7ac42010-06-25 13:46:18 -0700116 void load(const float* v);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700117 void load(const SkMatrix& v);
Romain Guy08ae3172010-06-21 19:35:50 -0700118
Romain Guy079ba2c2010-07-16 14:12:24 -0700119 void loadInverse(const Matrix4& v);
120
Romain Guy7ae7ac42010-06-25 13:46:18 -0700121 void loadTranslate(float x, float y, float z);
122 void loadScale(float sx, float sy, float sz);
Romain Guy807daf72011-01-18 11:19:19 -0800123 void loadSkew(float sx, float sy);
Romain Guy8ce00302013-01-15 18:51:42 -0800124 void loadRotate(float angle);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700125 void loadRotate(float angle, float x, float y, float z);
126 void loadMultiply(const Matrix4& u, const Matrix4& v);
Romain Guy08ae3172010-06-21 19:35:50 -0700127
Romain Guy7ae7ac42010-06-25 13:46:18 -0700128 void loadOrtho(float left, float right, float bottom, float top, float near, float far);
Chris Craikb565df12015-10-05 13:00:52 -0700129 void loadOrtho(int width, int height) {
130 loadOrtho(0, width, height, 0, -1, 1);
131 }
Romain Guy08ae3172010-06-21 19:35:50 -0700132
Romain Guyf6bed4f2013-06-20 17:52:07 -0700133 uint8_t getType() const;
Romain Guy8ce00302013-01-15 18:51:42 -0800134
Chris Craikfca52b752015-04-28 11:45:59 -0700135 void multiplyInverse(const Matrix4& v) {
136 Matrix4 inv;
137 inv.loadInverse(v);
138 multiply(inv);
139 }
140
Romain Guy7ae7ac42010-06-25 13:46:18 -0700141 void multiply(const Matrix4& v) {
142 Matrix4 u;
143 u.loadMultiply(*this, v);
Chris Craik7c85c542015-08-19 15:10:24 -0700144 *this = u;
Romain Guy7ae7ac42010-06-25 13:46:18 -0700145 }
Romain Guy08ae3172010-06-21 19:35:50 -0700146
Romain Guyddb80be2010-09-20 19:04:33 -0700147 void multiply(float v);
148
Chris Craikf57776b2013-10-25 18:30:17 -0700149 void translate(float x, float y, float z = 0) {
Romain Guyf6bed4f2013-06-20 17:52:07 -0700150 if ((getType() & sGeometryMask) <= kTypeTranslate) {
Romain Guy4c2547f2013-06-11 16:19:24 -0700151 data[kTranslateX] += x;
152 data[kTranslateY] += y;
Chris Craikf57776b2013-10-25 18:30:17 -0700153 data[kTranslateZ] += z;
John Recka447d292014-06-11 18:39:44 -0700154 mType |= kTypeUnknown;
Romain Guy4c2547f2013-06-11 16:19:24 -0700155 } else {
156 // Doing a translation will only affect the translate bit of the type
157 // Save the type
Romain Guyf6bed4f2013-06-20 17:52:07 -0700158 uint8_t type = mType;
Romain Guy4c2547f2013-06-11 16:19:24 -0700159
160 Matrix4 u;
Chris Craikf57776b2013-10-25 18:30:17 -0700161 u.loadTranslate(x, y, z);
Romain Guy4c2547f2013-06-11 16:19:24 -0700162 multiply(u);
163
164 // Restore the type and fix the translate bit
165 mType = type;
166 if (data[kTranslateX] != 0.0f || data[kTranslateY] != 0.0f) {
167 mType |= kTypeTranslate;
168 } else {
169 mType &= ~kTypeTranslate;
170 }
171 }
Romain Guy7ae7ac42010-06-25 13:46:18 -0700172 }
Romain Guy08ae3172010-06-21 19:35:50 -0700173
Romain Guy7ae7ac42010-06-25 13:46:18 -0700174 void scale(float sx, float sy, float sz) {
175 Matrix4 u;
176 u.loadScale(sx, sy, sz);
177 multiply(u);
178 }
Romain Guy08ae3172010-06-21 19:35:50 -0700179
Romain Guy807daf72011-01-18 11:19:19 -0800180 void skew(float sx, float sy) {
181 Matrix4 u;
182 u.loadSkew(sx, sy);
183 multiply(u);
184 }
185
Romain Guy7ae7ac42010-06-25 13:46:18 -0700186 void rotate(float angle, float x, float y, float z) {
187 Matrix4 u;
188 u.loadRotate(angle, x, y, z);
189 multiply(u);
190 }
Romain Guy08ae3172010-06-21 19:35:50 -0700191
Romain Guy8ce00302013-01-15 18:51:42 -0800192 /**
193 * If the matrix is identity or translate and/or scale.
194 */
Chris Craik710f46d2012-09-17 17:25:49 -0700195 bool isSimple() const;
Romain Guy8ce00302013-01-15 18:51:42 -0800196 bool isPureTranslate() const;
Chris Craik710f46d2012-09-17 17:25:49 -0700197 bool isIdentity() const;
Romain Guya3dc55f2012-09-28 13:55:44 -0700198 bool isPerspective() const;
Romain Guy8ce00302013-01-15 18:51:42 -0800199 bool rectToRect() const;
Chris Craikd965bc52013-09-16 14:47:13 -0700200 bool positiveScale() const;
Romain Guy6620c6d2010-12-06 18:07:02 -0800201
Chris Craik710f46d2012-09-17 17:25:49 -0700202 bool changesBounds() const;
Romain Guye8cb9c142010-10-04 14:14:11 -0700203
Romain Guy7ae7ac42010-06-25 13:46:18 -0700204 void copyTo(float* v) const;
205 void copyTo(SkMatrix& v) const;
Romain Guy08ae3172010-06-21 19:35:50 -0700206
Chris Craikb79a3e32014-03-11 12:20:17 -0700207 float mapZ(const Vector3& orig) const;
Chris Craikf57776b2013-10-25 18:30:17 -0700208 void mapPoint3d(Vector3& vec) const;
209 void mapPoint(float& x, float& y) const; // 2d only
210 void mapRect(Rect& r) const; // 2d only
Romain Guy9d5316e2010-06-24 19:30:36 -0700211
Romain Guy624234f2013-03-05 16:43:31 -0800212 float getTranslateX() const;
213 float getTranslateY() const;
Romain Guybd6b79b2010-06-26 00:13:53 -0700214
Romain Guy3b753822013-03-05 10:27:35 -0800215 void decomposeScale(float& sx, float& sy) const;
216
Chris Craike84a2082014-12-22 14:28:49 -0800217 void dump(const char* label = nullptr) const;
Romain Guy08ae3172010-06-21 19:35:50 -0700218
Romain Guyc74f45a2013-02-26 19:10:14 -0800219 static const Matrix4& identity();
220
Romain Guyf6a11b82010-06-23 17:47:49 -0700221private:
Romain Guyf6bed4f2013-06-20 17:52:07 -0700222 mutable uint8_t mType;
Romain Guyaf28b512010-08-12 14:34:44 -0700223
Romain Guy08ae3172010-06-21 19:35:50 -0700224 inline float get(int i, int j) const {
Romain Guyc7d53492010-06-25 13:41:57 -0700225 return data[i * 4 + j];
Romain Guy08ae3172010-06-21 19:35:50 -0700226 }
227
228 inline void set(int i, int j, float v) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700229 data[i * 4 + j] = v;
Romain Guy08ae3172010-06-21 19:35:50 -0700230 }
Romain Guy8ce00302013-01-15 18:51:42 -0800231
Romain Guyf6bed4f2013-06-20 17:52:07 -0700232 uint8_t getGeometryType() const;
Romain Guy8ce00302013-01-15 18:51:42 -0800233
Romain Guy08ae3172010-06-21 19:35:50 -0700234}; // class Matrix4
235
236///////////////////////////////////////////////////////////////////////////////
237// Types
238///////////////////////////////////////////////////////////////////////////////
239
240typedef Matrix4 mat4;
241
Romain Guy9d5316e2010-06-24 19:30:36 -0700242}; // namespace uirenderer
Romain Guy08ae3172010-06-21 19:35:50 -0700243}; // namespace android
244
Romain Guy5b3b3522010-10-27 18:57:51 -0700245#endif // ANDROID_HWUI_MATRIX_H