blob: 75e280cf502b920a3f8e0c2b4690de462c63e363 [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
29///////////////////////////////////////////////////////////////////////////////
30// Classes
31///////////////////////////////////////////////////////////////////////////////
32
Romain Guy71e36aa2011-10-12 14:11:32 -070033class ANDROID_API Matrix4 {
Romain Guy08ae3172010-06-21 19:35:50 -070034public:
Romain Guy7ae7ac42010-06-25 13:46:18 -070035 float data[16];
Romain Guyc7d53492010-06-25 13:41:57 -070036
Romain Guyaf28b512010-08-12 14:34:44 -070037 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
Romain Guy8ce00302013-01-15 18:51:42 -080051 // 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
Romain Guy7ae7ac42010-06-25 13:46:18 -070066 Matrix4() {
67 loadIdentity();
68 }
Romain Guy08ae3172010-06-21 19:35:50 -070069
Romain Guy7ae7ac42010-06-25 13:46:18 -070070 Matrix4(const float* v) {
71 load(v);
72 }
Romain Guy08ae3172010-06-21 19:35:50 -070073
Romain Guy7ae7ac42010-06-25 13:46:18 -070074 Matrix4(const Matrix4& v) {
75 load(v);
76 }
Romain Guy08ae3172010-06-21 19:35:50 -070077
Romain Guy7ae7ac42010-06-25 13:46:18 -070078 Matrix4(const SkMatrix& v) {
79 load(v);
80 }
Romain Guyf6a11b82010-06-23 17:47:49 -070081
Romain Guy3b753822013-03-05 10:27:35 -080082 float operator[](int index) const {
83 return data[index];
84 }
85
86 float& operator[](int index) {
87 mType = kTypeUnknown;
88 return data[index];
89 }
90
91 Matrix4& operator=(const SkMatrix& v) {
92 load(v);
93 return *this;
94 }
95
Romain Guybd3055f2013-03-13 16:14:47 -070096 friend bool operator==(const Matrix4& a, const Matrix4& b) {
97 return !memcmp(&a.data[0], &b.data[0], 16 * sizeof(float));
98 }
99
100 friend bool operator!=(const Matrix4& a, const Matrix4& b) {
101 return !(a == b);
102 }
103
Romain Guy7ae7ac42010-06-25 13:46:18 -0700104 void loadIdentity();
Romain Guy08ae3172010-06-21 19:35:50 -0700105
Romain Guy7ae7ac42010-06-25 13:46:18 -0700106 void load(const float* v);
107 void load(const Matrix4& v);
108 void load(const SkMatrix& v);
Romain Guy08ae3172010-06-21 19:35:50 -0700109
Romain Guy079ba2c2010-07-16 14:12:24 -0700110 void loadInverse(const Matrix4& v);
111
Romain Guy7ae7ac42010-06-25 13:46:18 -0700112 void loadTranslate(float x, float y, float z);
113 void loadScale(float sx, float sy, float sz);
Romain Guy807daf72011-01-18 11:19:19 -0800114 void loadSkew(float sx, float sy);
Romain Guy8ce00302013-01-15 18:51:42 -0800115 void loadRotate(float angle);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700116 void loadRotate(float angle, float x, float y, float z);
117 void loadMultiply(const Matrix4& u, const Matrix4& v);
Romain Guy08ae3172010-06-21 19:35:50 -0700118
Romain Guy7ae7ac42010-06-25 13:46:18 -0700119 void loadOrtho(float left, float right, float bottom, float top, float near, float far);
Romain Guy08ae3172010-06-21 19:35:50 -0700120
Romain Guy8ce00302013-01-15 18:51:42 -0800121 uint32_t getType() const;
122
Romain Guy7ae7ac42010-06-25 13:46:18 -0700123 void multiply(const Matrix4& v) {
124 Matrix4 u;
125 u.loadMultiply(*this, v);
126 load(u);
127 }
Romain Guy08ae3172010-06-21 19:35:50 -0700128
Romain Guyddb80be2010-09-20 19:04:33 -0700129 void multiply(float v);
130
Romain Guy7ae7ac42010-06-25 13:46:18 -0700131 void translate(float x, float y, float z) {
132 Matrix4 u;
133 u.loadTranslate(x, y, z);
134 multiply(u);
135 }
Romain Guy08ae3172010-06-21 19:35:50 -0700136
Romain Guy7ae7ac42010-06-25 13:46:18 -0700137 void scale(float sx, float sy, float sz) {
138 Matrix4 u;
139 u.loadScale(sx, sy, sz);
140 multiply(u);
141 }
Romain Guy08ae3172010-06-21 19:35:50 -0700142
Romain Guy807daf72011-01-18 11:19:19 -0800143 void skew(float sx, float sy) {
144 Matrix4 u;
145 u.loadSkew(sx, sy);
146 multiply(u);
147 }
148
Romain Guy7ae7ac42010-06-25 13:46:18 -0700149 void rotate(float angle, float x, float y, float z) {
150 Matrix4 u;
151 u.loadRotate(angle, x, y, z);
152 multiply(u);
153 }
Romain Guy08ae3172010-06-21 19:35:50 -0700154
Romain Guy8ce00302013-01-15 18:51:42 -0800155 /**
156 * If the matrix is identity or translate and/or scale.
157 */
Chris Craik710f46d2012-09-17 17:25:49 -0700158 bool isSimple() const;
Romain Guy8ce00302013-01-15 18:51:42 -0800159 bool isPureTranslate() const;
Chris Craik710f46d2012-09-17 17:25:49 -0700160 bool isIdentity() const;
Romain Guya3dc55f2012-09-28 13:55:44 -0700161 bool isPerspective() const;
Romain Guy8ce00302013-01-15 18:51:42 -0800162 bool rectToRect() const;
Romain Guy6620c6d2010-12-06 18:07:02 -0800163
Chris Craik710f46d2012-09-17 17:25:49 -0700164 bool changesBounds() const;
Romain Guye8cb9c142010-10-04 14:14:11 -0700165
Romain Guy7ae7ac42010-06-25 13:46:18 -0700166 void copyTo(float* v) const;
167 void copyTo(SkMatrix& v) const;
Romain Guy08ae3172010-06-21 19:35:50 -0700168
Romain Guy7ae7ac42010-06-25 13:46:18 -0700169 void mapRect(Rect& r) const;
Romain Guy0ba681b2010-08-12 15:37:00 -0700170 void mapPoint(float& x, float& y) const;
Romain Guy9d5316e2010-06-24 19:30:36 -0700171
Romain Guy624234f2013-03-05 16:43:31 -0800172 float getTranslateX() const;
173 float getTranslateY() const;
Romain Guybd6b79b2010-06-26 00:13:53 -0700174
Romain Guy3b753822013-03-05 10:27:35 -0800175 void decomposeScale(float& sx, float& sy) const;
176
Romain Guy7ae7ac42010-06-25 13:46:18 -0700177 void dump() const;
Romain Guy08ae3172010-06-21 19:35:50 -0700178
Romain Guyc74f45a2013-02-26 19:10:14 -0800179 static const Matrix4& identity();
180
Romain Guyf6a11b82010-06-23 17:47:49 -0700181private:
Romain Guy8ce00302013-01-15 18:51:42 -0800182 mutable uint32_t mType;
Romain Guyaf28b512010-08-12 14:34:44 -0700183
Romain Guy08ae3172010-06-21 19:35:50 -0700184 inline float get(int i, int j) const {
Romain Guyc7d53492010-06-25 13:41:57 -0700185 return data[i * 4 + j];
Romain Guy08ae3172010-06-21 19:35:50 -0700186 }
187
188 inline void set(int i, int j, float v) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700189 data[i * 4 + j] = v;
Romain Guy08ae3172010-06-21 19:35:50 -0700190 }
Romain Guy8ce00302013-01-15 18:51:42 -0800191
192 uint32_t getGeometryType() const;
193
Romain Guy08ae3172010-06-21 19:35:50 -0700194}; // class Matrix4
195
196///////////////////////////////////////////////////////////////////////////////
197// Types
198///////////////////////////////////////////////////////////////////////////////
199
200typedef Matrix4 mat4;
201
Romain Guy9d5316e2010-06-24 19:30:36 -0700202}; // namespace uirenderer
Romain Guy08ae3172010-06-21 19:35:50 -0700203}; // namespace android
204
Romain Guy5b3b3522010-10-27 18:57:51 -0700205#endif // ANDROID_HWUI_MATRIX_H