blob: b861ba4abb254d9eff45450d14881bfe8799986c [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);
Romain Guy08ae3172010-06-21 19:35:50 -0700124
Romain Guy7ae7ac42010-06-25 13:46:18 -0700125 void loadOrtho(float left, float right, float bottom, float top, float near, float far);
Romain Guy08ae3172010-06-21 19:35:50 -0700126
Romain Guyf6bed4f2013-06-20 17:52:07 -0700127 uint8_t getType() const;
Romain Guy8ce00302013-01-15 18:51:42 -0800128
Romain Guy7ae7ac42010-06-25 13:46:18 -0700129 void multiply(const Matrix4& v) {
130 Matrix4 u;
131 u.loadMultiply(*this, v);
132 load(u);
133 }
Romain Guy08ae3172010-06-21 19:35:50 -0700134
Romain Guyddb80be2010-09-20 19:04:33 -0700135 void multiply(float v);
136
Romain Guy4c2547f2013-06-11 16:19:24 -0700137 void translate(float x, float y) {
Romain Guyf6bed4f2013-06-20 17:52:07 -0700138 if ((getType() & sGeometryMask) <= kTypeTranslate) {
Romain Guy4c2547f2013-06-11 16:19:24 -0700139 data[kTranslateX] += x;
140 data[kTranslateY] += y;
141 } else {
142 // Doing a translation will only affect the translate bit of the type
143 // Save the type
Romain Guyf6bed4f2013-06-20 17:52:07 -0700144 uint8_t type = mType;
Romain Guy4c2547f2013-06-11 16:19:24 -0700145
146 Matrix4 u;
147 u.loadTranslate(x, y, 0.0f);
148 multiply(u);
149
150 // Restore the type and fix the translate bit
151 mType = type;
152 if (data[kTranslateX] != 0.0f || data[kTranslateY] != 0.0f) {
153 mType |= kTypeTranslate;
154 } else {
155 mType &= ~kTypeTranslate;
156 }
157 }
Romain Guy7ae7ac42010-06-25 13:46:18 -0700158 }
Romain Guy08ae3172010-06-21 19:35:50 -0700159
Romain Guy7ae7ac42010-06-25 13:46:18 -0700160 void scale(float sx, float sy, float sz) {
161 Matrix4 u;
162 u.loadScale(sx, sy, sz);
163 multiply(u);
164 }
Romain Guy08ae3172010-06-21 19:35:50 -0700165
Romain Guy807daf72011-01-18 11:19:19 -0800166 void skew(float sx, float sy) {
167 Matrix4 u;
168 u.loadSkew(sx, sy);
169 multiply(u);
170 }
171
Romain Guy7ae7ac42010-06-25 13:46:18 -0700172 void rotate(float angle, float x, float y, float z) {
173 Matrix4 u;
174 u.loadRotate(angle, x, y, z);
175 multiply(u);
176 }
Romain Guy08ae3172010-06-21 19:35:50 -0700177
Romain Guy8ce00302013-01-15 18:51:42 -0800178 /**
179 * If the matrix is identity or translate and/or scale.
180 */
Chris Craik710f46d2012-09-17 17:25:49 -0700181 bool isSimple() const;
Romain Guy8ce00302013-01-15 18:51:42 -0800182 bool isPureTranslate() const;
Chris Craik710f46d2012-09-17 17:25:49 -0700183 bool isIdentity() const;
Romain Guya3dc55f2012-09-28 13:55:44 -0700184 bool isPerspective() const;
Romain Guy8ce00302013-01-15 18:51:42 -0800185 bool rectToRect() const;
Chris Craikd965bc52013-09-16 14:47:13 -0700186 bool positiveScale() const;
Romain Guy6620c6d2010-12-06 18:07:02 -0800187
Chris Craik710f46d2012-09-17 17:25:49 -0700188 bool changesBounds() const;
Romain Guye8cb9c142010-10-04 14:14:11 -0700189
Romain Guy7ae7ac42010-06-25 13:46:18 -0700190 void copyTo(float* v) const;
191 void copyTo(SkMatrix& v) const;
Romain Guy08ae3172010-06-21 19:35:50 -0700192
Romain Guy7ae7ac42010-06-25 13:46:18 -0700193 void mapRect(Rect& r) const;
Romain Guy0ba681b2010-08-12 15:37:00 -0700194 void mapPoint(float& x, float& y) const;
Romain Guy9d5316e2010-06-24 19:30:36 -0700195
Romain Guy624234f2013-03-05 16:43:31 -0800196 float getTranslateX() const;
197 float getTranslateY() const;
Romain Guybd6b79b2010-06-26 00:13:53 -0700198
Romain Guy3b753822013-03-05 10:27:35 -0800199 void decomposeScale(float& sx, float& sy) const;
200
Romain Guy7ae7ac42010-06-25 13:46:18 -0700201 void dump() const;
Romain Guy08ae3172010-06-21 19:35:50 -0700202
Romain Guyc74f45a2013-02-26 19:10:14 -0800203 static const Matrix4& identity();
204
Romain Guyf6a11b82010-06-23 17:47:49 -0700205private:
Romain Guyf6bed4f2013-06-20 17:52:07 -0700206 mutable uint8_t mType;
Romain Guyaf28b512010-08-12 14:34:44 -0700207
Romain Guy08ae3172010-06-21 19:35:50 -0700208 inline float get(int i, int j) const {
Romain Guyc7d53492010-06-25 13:41:57 -0700209 return data[i * 4 + j];
Romain Guy08ae3172010-06-21 19:35:50 -0700210 }
211
212 inline void set(int i, int j, float v) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700213 data[i * 4 + j] = v;
Romain Guy08ae3172010-06-21 19:35:50 -0700214 }
Romain Guy8ce00302013-01-15 18:51:42 -0800215
Romain Guyf6bed4f2013-06-20 17:52:07 -0700216 uint8_t getGeometryType() const;
Romain Guy8ce00302013-01-15 18:51:42 -0800217
Romain Guy08ae3172010-06-21 19:35:50 -0700218}; // class Matrix4
219
220///////////////////////////////////////////////////////////////////////////////
221// Types
222///////////////////////////////////////////////////////////////////////////////
223
224typedef Matrix4 mat4;
225
Romain Guy9d5316e2010-06-24 19:30:36 -0700226}; // namespace uirenderer
Romain Guy08ae3172010-06-21 19:35:50 -0700227}; // namespace android
228
Romain Guy5b3b3522010-10-27 18:57:51 -0700229#endif // ANDROID_HWUI_MATRIX_H