blob: 08f5d776d5f5f2cf5c330ac4bdabae08ae59f0fe [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 Guy9d5316e2010-06-24 19:30:36 -070022#include "Rect.h"
23
Romain Guy08ae3172010-06-21 19:35:50 -070024namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070025namespace uirenderer {
Romain Guy08ae3172010-06-21 19:35:50 -070026
27///////////////////////////////////////////////////////////////////////////////
28// Classes
29///////////////////////////////////////////////////////////////////////////////
30
31class Matrix4 {
32public:
Romain Guy7ae7ac42010-06-25 13:46:18 -070033 float data[16];
Romain Guyc7d53492010-06-25 13:41:57 -070034
Romain Guyaf28b512010-08-12 14:34:44 -070035 enum Entry {
36 kScaleX = 0,
37 kSkewY = 1,
38 kPerspective0 = 3,
39 kSkewX = 4,
40 kScaleY = 5,
41 kPerspective1 = 7,
42 kScaleZ = 10,
43 kTranslateX = 12,
44 kTranslateY = 13,
45 kTranslateZ = 14,
46 kPerspective2 = 15
47 };
48
Romain Guy7ae7ac42010-06-25 13:46:18 -070049 Matrix4() {
50 loadIdentity();
51 }
Romain Guy08ae3172010-06-21 19:35:50 -070052
Romain Guy7ae7ac42010-06-25 13:46:18 -070053 Matrix4(const float* v) {
54 load(v);
55 }
Romain Guy08ae3172010-06-21 19:35:50 -070056
Romain Guy7ae7ac42010-06-25 13:46:18 -070057 Matrix4(const Matrix4& v) {
58 load(v);
59 }
Romain Guy08ae3172010-06-21 19:35:50 -070060
Romain Guy7ae7ac42010-06-25 13:46:18 -070061 Matrix4(const SkMatrix& v) {
62 load(v);
63 }
Romain Guyf6a11b82010-06-23 17:47:49 -070064
Romain Guy7ae7ac42010-06-25 13:46:18 -070065 void loadIdentity();
Romain Guy08ae3172010-06-21 19:35:50 -070066
Romain Guy7ae7ac42010-06-25 13:46:18 -070067 void load(const float* v);
68 void load(const Matrix4& v);
69 void load(const SkMatrix& v);
Romain Guy08ae3172010-06-21 19:35:50 -070070
Romain Guy079ba2c2010-07-16 14:12:24 -070071 void loadInverse(const Matrix4& v);
72
Romain Guy7ae7ac42010-06-25 13:46:18 -070073 void loadTranslate(float x, float y, float z);
74 void loadScale(float sx, float sy, float sz);
Romain Guy807daf72011-01-18 11:19:19 -080075 void loadSkew(float sx, float sy);
Romain Guy7ae7ac42010-06-25 13:46:18 -070076 void loadRotate(float angle, float x, float y, float z);
77 void loadMultiply(const Matrix4& u, const Matrix4& v);
Romain Guy08ae3172010-06-21 19:35:50 -070078
Romain Guy7ae7ac42010-06-25 13:46:18 -070079 void loadOrtho(float left, float right, float bottom, float top, float near, float far);
Romain Guy08ae3172010-06-21 19:35:50 -070080
Romain Guy7ae7ac42010-06-25 13:46:18 -070081 void multiply(const Matrix4& v) {
82 Matrix4 u;
83 u.loadMultiply(*this, v);
84 load(u);
85 }
Romain Guy08ae3172010-06-21 19:35:50 -070086
Romain Guyddb80be2010-09-20 19:04:33 -070087 void multiply(float v);
88
Romain Guy7ae7ac42010-06-25 13:46:18 -070089 void translate(float x, float y, float z) {
90 Matrix4 u;
91 u.loadTranslate(x, y, z);
92 multiply(u);
93 }
Romain Guy08ae3172010-06-21 19:35:50 -070094
Romain Guy7ae7ac42010-06-25 13:46:18 -070095 void scale(float sx, float sy, float sz) {
96 Matrix4 u;
97 u.loadScale(sx, sy, sz);
98 multiply(u);
99 }
Romain Guy08ae3172010-06-21 19:35:50 -0700100
Romain Guy807daf72011-01-18 11:19:19 -0800101 void skew(float sx, float sy) {
102 Matrix4 u;
103 u.loadSkew(sx, sy);
104 multiply(u);
105 }
106
Romain Guy7ae7ac42010-06-25 13:46:18 -0700107 void rotate(float angle, float x, float y, float z) {
108 Matrix4 u;
109 u.loadRotate(angle, x, y, z);
110 multiply(u);
111 }
Romain Guy08ae3172010-06-21 19:35:50 -0700112
Romain Guy6620c6d2010-12-06 18:07:02 -0800113 bool isPureTranslate();
114
Romain Guye8cb9c142010-10-04 14:14:11 -0700115 bool changesBounds();
116
Romain Guy7ae7ac42010-06-25 13:46:18 -0700117 void copyTo(float* v) const;
118 void copyTo(SkMatrix& v) const;
Romain Guy08ae3172010-06-21 19:35:50 -0700119
Romain Guy7ae7ac42010-06-25 13:46:18 -0700120 void mapRect(Rect& r) const;
Romain Guy0ba681b2010-08-12 15:37:00 -0700121 void mapPoint(float& x, float& y) const;
Romain Guy9d5316e2010-06-24 19:30:36 -0700122
Romain Guybd6b79b2010-06-26 00:13:53 -0700123 float getTranslateX();
124 float getTranslateY();
125
Romain Guy7ae7ac42010-06-25 13:46:18 -0700126 void dump() const;
Romain Guy08ae3172010-06-21 19:35:50 -0700127
Romain Guyf6a11b82010-06-23 17:47:49 -0700128private:
Romain Guyaf28b512010-08-12 14:34:44 -0700129 bool mSimpleMatrix;
130
Romain Guy08ae3172010-06-21 19:35:50 -0700131 inline float get(int i, int j) const {
Romain Guyc7d53492010-06-25 13:41:57 -0700132 return data[i * 4 + j];
Romain Guy08ae3172010-06-21 19:35:50 -0700133 }
134
135 inline void set(int i, int j, float v) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700136 data[i * 4 + j] = v;
Romain Guy08ae3172010-06-21 19:35:50 -0700137 }
Romain Guy08ae3172010-06-21 19:35:50 -0700138}; // class Matrix4
139
140///////////////////////////////////////////////////////////////////////////////
141// Types
142///////////////////////////////////////////////////////////////////////////////
143
144typedef Matrix4 mat4;
145
Romain Guy9d5316e2010-06-24 19:30:36 -0700146}; // namespace uirenderer
Romain Guy08ae3172010-06-21 19:35:50 -0700147}; // namespace android
148
Romain Guy5b3b3522010-10-27 18:57:51 -0700149#endif // ANDROID_HWUI_MATRIX_H