blob: e2c5b2088e7ad78b36431562cba25d86c9308314 [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 Craikd965bc52013-09-16 14:47:13 -070067 kTypePositiveScale = 0x20,
68 kTypeUnknown = 0x40,
Romain Guy8ce00302013-01-15 18:51:42 -080069 };
70
71 static const int sGeometryMask = 0xf;
72
Romain Guy7ae7ac42010-06-25 13:46:18 -070073 Matrix4() {
74 loadIdentity();
75 }
Romain Guy08ae3172010-06-21 19:35:50 -070076
Romain Guy7ae7ac42010-06-25 13:46:18 -070077 Matrix4(const float* v) {
78 load(v);
79 }
Romain Guy08ae3172010-06-21 19:35:50 -070080
Romain Guy7ae7ac42010-06-25 13:46:18 -070081 Matrix4(const Matrix4& v) {
82 load(v);
83 }
Romain Guy08ae3172010-06-21 19:35:50 -070084
Romain Guy7ae7ac42010-06-25 13:46:18 -070085 Matrix4(const SkMatrix& v) {
86 load(v);
87 }
Romain Guyf6a11b82010-06-23 17:47:49 -070088
Romain Guy3b753822013-03-05 10:27:35 -080089 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
Romain Guybd3055f2013-03-13 16:14:47 -0700103 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
Romain Guy7ae7ac42010-06-25 13:46:18 -0700111 void loadIdentity();
Romain Guy08ae3172010-06-21 19:35:50 -0700112
Romain Guy7ae7ac42010-06-25 13:46:18 -0700113 void load(const float* v);
114 void load(const Matrix4& v);
115 void load(const SkMatrix& v);
Romain Guy08ae3172010-06-21 19:35:50 -0700116
Romain Guy079ba2c2010-07-16 14:12:24 -0700117 void loadInverse(const Matrix4& v);
118
Romain Guy7ae7ac42010-06-25 13:46:18 -0700119 void loadTranslate(float x, float y, float z);
120 void loadScale(float sx, float sy, float sz);
Romain Guy807daf72011-01-18 11:19:19 -0800121 void loadSkew(float sx, float sy);
Romain Guy8ce00302013-01-15 18:51:42 -0800122 void loadRotate(float angle);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700123 void loadRotate(float angle, float x, float y, float z);
124 void loadMultiply(const Matrix4& u, const Matrix4& v);
Romain Guy08ae3172010-06-21 19:35:50 -0700125
Romain Guy7ae7ac42010-06-25 13:46:18 -0700126 void loadOrtho(float left, float right, float bottom, float top, float near, float far);
Romain Guy08ae3172010-06-21 19:35:50 -0700127
Romain Guyf6bed4f2013-06-20 17:52:07 -0700128 uint8_t getType() const;
Romain Guy8ce00302013-01-15 18:51:42 -0800129
Romain Guy7ae7ac42010-06-25 13:46:18 -0700130 void multiply(const Matrix4& v) {
131 Matrix4 u;
132 u.loadMultiply(*this, v);
133 load(u);
134 }
Romain Guy08ae3172010-06-21 19:35:50 -0700135
Romain Guyddb80be2010-09-20 19:04:33 -0700136 void multiply(float v);
137
Romain Guy4c2547f2013-06-11 16:19:24 -0700138 void translate(float x, float y) {
Romain Guyf6bed4f2013-06-20 17:52:07 -0700139 if ((getType() & sGeometryMask) <= kTypeTranslate) {
Romain Guy4c2547f2013-06-11 16:19:24 -0700140 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
Romain Guyf6bed4f2013-06-20 17:52:07 -0700145 uint8_t type = mType;
Romain Guy4c2547f2013-06-11 16:19:24 -0700146
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 }
Romain Guy7ae7ac42010-06-25 13:46:18 -0700159 }
Romain Guy08ae3172010-06-21 19:35:50 -0700160
Romain Guy7ae7ac42010-06-25 13:46:18 -0700161 void scale(float sx, float sy, float sz) {
162 Matrix4 u;
163 u.loadScale(sx, sy, sz);
164 multiply(u);
165 }
Romain Guy08ae3172010-06-21 19:35:50 -0700166
Romain Guy807daf72011-01-18 11:19:19 -0800167 void skew(float sx, float sy) {
168 Matrix4 u;
169 u.loadSkew(sx, sy);
170 multiply(u);
171 }
172
Romain Guy7ae7ac42010-06-25 13:46:18 -0700173 void rotate(float angle, float x, float y, float z) {
174 Matrix4 u;
175 u.loadRotate(angle, x, y, z);
176 multiply(u);
177 }
Romain Guy08ae3172010-06-21 19:35:50 -0700178
Romain Guy8ce00302013-01-15 18:51:42 -0800179 /**
180 * If the matrix is identity or translate and/or scale.
181 */
Chris Craik710f46d2012-09-17 17:25:49 -0700182 bool isSimple() const;
Romain Guy8ce00302013-01-15 18:51:42 -0800183 bool isPureTranslate() const;
Chris Craik710f46d2012-09-17 17:25:49 -0700184 bool isIdentity() const;
Romain Guya3dc55f2012-09-28 13:55:44 -0700185 bool isPerspective() const;
Romain Guy8ce00302013-01-15 18:51:42 -0800186 bool rectToRect() const;
Chris Craikd965bc52013-09-16 14:47:13 -0700187 bool positiveScale() const;
Romain Guy6620c6d2010-12-06 18:07:02 -0800188
Chris Craik710f46d2012-09-17 17:25:49 -0700189 bool changesBounds() const;
Romain Guye8cb9c142010-10-04 14:14:11 -0700190
Romain Guy7ae7ac42010-06-25 13:46:18 -0700191 void copyTo(float* v) const;
192 void copyTo(SkMatrix& v) const;
Romain Guy08ae3172010-06-21 19:35:50 -0700193
Romain Guy7ae7ac42010-06-25 13:46:18 -0700194 void mapRect(Rect& r) const;
Romain Guy0ba681b2010-08-12 15:37:00 -0700195 void mapPoint(float& x, float& y) const;
Romain Guy9d5316e2010-06-24 19:30:36 -0700196
Romain Guy624234f2013-03-05 16:43:31 -0800197 float getTranslateX() const;
198 float getTranslateY() const;
Romain Guybd6b79b2010-06-26 00:13:53 -0700199
Romain Guy3b753822013-03-05 10:27:35 -0800200 void decomposeScale(float& sx, float& sy) const;
201
Romain Guy7ae7ac42010-06-25 13:46:18 -0700202 void dump() const;
Romain Guy08ae3172010-06-21 19:35:50 -0700203
Romain Guyc74f45a2013-02-26 19:10:14 -0800204 static const Matrix4& identity();
205
Romain Guyf6a11b82010-06-23 17:47:49 -0700206private:
Romain Guyf6bed4f2013-06-20 17:52:07 -0700207 mutable uint8_t mType;
Romain Guyaf28b512010-08-12 14:34:44 -0700208
Romain Guy08ae3172010-06-21 19:35:50 -0700209 inline float get(int i, int j) const {
Romain Guyc7d53492010-06-25 13:41:57 -0700210 return data[i * 4 + j];
Romain Guy08ae3172010-06-21 19:35:50 -0700211 }
212
213 inline void set(int i, int j, float v) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700214 data[i * 4 + j] = v;
Romain Guy08ae3172010-06-21 19:35:50 -0700215 }
Romain Guy8ce00302013-01-15 18:51:42 -0800216
Romain Guyf6bed4f2013-06-20 17:52:07 -0700217 uint8_t getGeometryType() const;
Romain Guy8ce00302013-01-15 18:51:42 -0800218
Romain Guy08ae3172010-06-21 19:35:50 -0700219}; // class Matrix4
220
221///////////////////////////////////////////////////////////////////////////////
222// Types
223///////////////////////////////////////////////////////////////////////////////
224
225typedef Matrix4 mat4;
226
Romain Guy9d5316e2010-06-24 19:30:36 -0700227}; // namespace uirenderer
Romain Guy08ae3172010-06-21 19:35:50 -0700228}; // namespace android
229
Romain Guy5b3b3522010-10-27 18:57:51 -0700230#endif // ANDROID_HWUI_MATRIX_H