blob: be5bea706e4bc084d291737d003445b1c6a2d5da [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 Guy7ae7ac42010-06-25 13:46:18 -070096 void loadIdentity();
Romain Guy08ae3172010-06-21 19:35:50 -070097
Romain Guy7ae7ac42010-06-25 13:46:18 -070098 void load(const float* v);
99 void load(const Matrix4& v);
100 void load(const SkMatrix& v);
Romain Guy08ae3172010-06-21 19:35:50 -0700101
Romain Guy079ba2c2010-07-16 14:12:24 -0700102 void loadInverse(const Matrix4& v);
103
Romain Guy7ae7ac42010-06-25 13:46:18 -0700104 void loadTranslate(float x, float y, float z);
105 void loadScale(float sx, float sy, float sz);
Romain Guy807daf72011-01-18 11:19:19 -0800106 void loadSkew(float sx, float sy);
Romain Guy8ce00302013-01-15 18:51:42 -0800107 void loadRotate(float angle);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700108 void loadRotate(float angle, float x, float y, float z);
109 void loadMultiply(const Matrix4& u, const Matrix4& v);
Romain Guy08ae3172010-06-21 19:35:50 -0700110
Romain Guy7ae7ac42010-06-25 13:46:18 -0700111 void loadOrtho(float left, float right, float bottom, float top, float near, float far);
Romain Guy08ae3172010-06-21 19:35:50 -0700112
Romain Guy8ce00302013-01-15 18:51:42 -0800113 uint32_t getType() const;
114
Romain Guy7ae7ac42010-06-25 13:46:18 -0700115 void multiply(const Matrix4& v) {
116 Matrix4 u;
117 u.loadMultiply(*this, v);
118 load(u);
119 }
Romain Guy08ae3172010-06-21 19:35:50 -0700120
Romain Guyddb80be2010-09-20 19:04:33 -0700121 void multiply(float v);
122
Romain Guy7ae7ac42010-06-25 13:46:18 -0700123 void translate(float x, float y, float z) {
124 Matrix4 u;
125 u.loadTranslate(x, y, z);
126 multiply(u);
127 }
Romain Guy08ae3172010-06-21 19:35:50 -0700128
Romain Guy7ae7ac42010-06-25 13:46:18 -0700129 void scale(float sx, float sy, float sz) {
130 Matrix4 u;
131 u.loadScale(sx, sy, sz);
132 multiply(u);
133 }
Romain Guy08ae3172010-06-21 19:35:50 -0700134
Romain Guy807daf72011-01-18 11:19:19 -0800135 void skew(float sx, float sy) {
136 Matrix4 u;
137 u.loadSkew(sx, sy);
138 multiply(u);
139 }
140
Romain Guy7ae7ac42010-06-25 13:46:18 -0700141 void rotate(float angle, float x, float y, float z) {
142 Matrix4 u;
143 u.loadRotate(angle, x, y, z);
144 multiply(u);
145 }
Romain Guy08ae3172010-06-21 19:35:50 -0700146
Romain Guy8ce00302013-01-15 18:51:42 -0800147 /**
148 * If the matrix is identity or translate and/or scale.
149 */
Chris Craik710f46d2012-09-17 17:25:49 -0700150 bool isSimple() const;
Romain Guy8ce00302013-01-15 18:51:42 -0800151 bool isPureTranslate() const;
Chris Craik710f46d2012-09-17 17:25:49 -0700152 bool isIdentity() const;
Romain Guya3dc55f2012-09-28 13:55:44 -0700153 bool isPerspective() const;
Romain Guy8ce00302013-01-15 18:51:42 -0800154 bool rectToRect() const;
Romain Guy6620c6d2010-12-06 18:07:02 -0800155
Chris Craik710f46d2012-09-17 17:25:49 -0700156 bool changesBounds() const;
Romain Guye8cb9c142010-10-04 14:14:11 -0700157
Romain Guy7ae7ac42010-06-25 13:46:18 -0700158 void copyTo(float* v) const;
159 void copyTo(SkMatrix& v) const;
Romain Guy08ae3172010-06-21 19:35:50 -0700160
Romain Guy7ae7ac42010-06-25 13:46:18 -0700161 void mapRect(Rect& r) const;
Romain Guy0ba681b2010-08-12 15:37:00 -0700162 void mapPoint(float& x, float& y) const;
Romain Guy9d5316e2010-06-24 19:30:36 -0700163
Romain Guybd6b79b2010-06-26 00:13:53 -0700164 float getTranslateX();
165 float getTranslateY();
166
Romain Guy3b753822013-03-05 10:27:35 -0800167 void decomposeScale(float& sx, float& sy) const;
168
Romain Guy7ae7ac42010-06-25 13:46:18 -0700169 void dump() const;
Romain Guy08ae3172010-06-21 19:35:50 -0700170
Romain Guyc74f45a2013-02-26 19:10:14 -0800171 static const Matrix4& identity();
172
Romain Guyf6a11b82010-06-23 17:47:49 -0700173private:
Romain Guy8ce00302013-01-15 18:51:42 -0800174 mutable uint32_t mType;
Romain Guyaf28b512010-08-12 14:34:44 -0700175
Romain Guy08ae3172010-06-21 19:35:50 -0700176 inline float get(int i, int j) const {
Romain Guyc7d53492010-06-25 13:41:57 -0700177 return data[i * 4 + j];
Romain Guy08ae3172010-06-21 19:35:50 -0700178 }
179
180 inline void set(int i, int j, float v) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700181 data[i * 4 + j] = v;
Romain Guy08ae3172010-06-21 19:35:50 -0700182 }
Romain Guy8ce00302013-01-15 18:51:42 -0800183
184 uint32_t getGeometryType() const;
185
Romain Guy08ae3172010-06-21 19:35:50 -0700186}; // class Matrix4
187
188///////////////////////////////////////////////////////////////////////////////
189// Types
190///////////////////////////////////////////////////////////////////////////////
191
192typedef Matrix4 mat4;
193
Romain Guy9d5316e2010-06-24 19:30:36 -0700194}; // namespace uirenderer
Romain Guy08ae3172010-06-21 19:35:50 -0700195}; // namespace android
196
Romain Guy5b3b3522010-10-27 18:57:51 -0700197#endif // ANDROID_HWUI_MATRIX_H