blob: 22220a93c0ad87355f307c4f3ea85797024fa829 [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 Guy7ae7ac42010-06-25 13:46:18 -070051 Matrix4() {
52 loadIdentity();
53 }
Romain Guy08ae3172010-06-21 19:35:50 -070054
Romain Guy7ae7ac42010-06-25 13:46:18 -070055 Matrix4(const float* v) {
56 load(v);
57 }
Romain Guy08ae3172010-06-21 19:35:50 -070058
Romain Guy7ae7ac42010-06-25 13:46:18 -070059 Matrix4(const Matrix4& v) {
60 load(v);
61 }
Romain Guy08ae3172010-06-21 19:35:50 -070062
Romain Guy7ae7ac42010-06-25 13:46:18 -070063 Matrix4(const SkMatrix& v) {
64 load(v);
65 }
Romain Guyf6a11b82010-06-23 17:47:49 -070066
Romain Guy7ae7ac42010-06-25 13:46:18 -070067 void loadIdentity();
Romain Guy08ae3172010-06-21 19:35:50 -070068
Romain Guy7ae7ac42010-06-25 13:46:18 -070069 void load(const float* v);
70 void load(const Matrix4& v);
71 void load(const SkMatrix& v);
Romain Guy08ae3172010-06-21 19:35:50 -070072
Romain Guy079ba2c2010-07-16 14:12:24 -070073 void loadInverse(const Matrix4& v);
74
Romain Guy7ae7ac42010-06-25 13:46:18 -070075 void loadTranslate(float x, float y, float z);
76 void loadScale(float sx, float sy, float sz);
Romain Guy807daf72011-01-18 11:19:19 -080077 void loadSkew(float sx, float sy);
Romain Guy7ae7ac42010-06-25 13:46:18 -070078 void loadRotate(float angle, float x, float y, float z);
79 void loadMultiply(const Matrix4& u, const Matrix4& v);
Romain Guy08ae3172010-06-21 19:35:50 -070080
Romain Guy7ae7ac42010-06-25 13:46:18 -070081 void loadOrtho(float left, float right, float bottom, float top, float near, float far);
Romain Guy08ae3172010-06-21 19:35:50 -070082
Romain Guy7ae7ac42010-06-25 13:46:18 -070083 void multiply(const Matrix4& v) {
84 Matrix4 u;
85 u.loadMultiply(*this, v);
86 load(u);
87 }
Romain Guy08ae3172010-06-21 19:35:50 -070088
Romain Guyddb80be2010-09-20 19:04:33 -070089 void multiply(float v);
90
Romain Guy7ae7ac42010-06-25 13:46:18 -070091 void translate(float x, float y, float z) {
92 Matrix4 u;
93 u.loadTranslate(x, y, z);
94 multiply(u);
95 }
Romain Guy08ae3172010-06-21 19:35:50 -070096
Romain Guy7ae7ac42010-06-25 13:46:18 -070097 void scale(float sx, float sy, float sz) {
98 Matrix4 u;
99 u.loadScale(sx, sy, sz);
100 multiply(u);
101 }
Romain Guy08ae3172010-06-21 19:35:50 -0700102
Romain Guy807daf72011-01-18 11:19:19 -0800103 void skew(float sx, float sy) {
104 Matrix4 u;
105 u.loadSkew(sx, sy);
106 multiply(u);
107 }
108
Romain Guy7ae7ac42010-06-25 13:46:18 -0700109 void rotate(float angle, float x, float y, float z) {
110 Matrix4 u;
111 u.loadRotate(angle, x, y, z);
112 multiply(u);
113 }
Romain Guy08ae3172010-06-21 19:35:50 -0700114
Romain Guy6620c6d2010-12-06 18:07:02 -0800115 bool isPureTranslate();
Romain Guy181d0a62011-06-09 18:52:38 -0700116 bool isSimple();
Romain Guy302a9df2011-08-16 13:55:02 -0700117 bool isIdentity();
Romain Guy6620c6d2010-12-06 18:07:02 -0800118
Romain Guye8cb9c142010-10-04 14:14:11 -0700119 bool changesBounds();
120
Romain Guy7ae7ac42010-06-25 13:46:18 -0700121 void copyTo(float* v) const;
122 void copyTo(SkMatrix& v) const;
Romain Guy08ae3172010-06-21 19:35:50 -0700123
Romain Guy7ae7ac42010-06-25 13:46:18 -0700124 void mapRect(Rect& r) const;
Romain Guy0ba681b2010-08-12 15:37:00 -0700125 void mapPoint(float& x, float& y) const;
Romain Guy9d5316e2010-06-24 19:30:36 -0700126
Romain Guybd6b79b2010-06-26 00:13:53 -0700127 float getTranslateX();
128 float getTranslateY();
129
Romain Guy7ae7ac42010-06-25 13:46:18 -0700130 void dump() const;
Romain Guy08ae3172010-06-21 19:35:50 -0700131
Romain Guyf6a11b82010-06-23 17:47:49 -0700132private:
Romain Guyaf28b512010-08-12 14:34:44 -0700133 bool mSimpleMatrix;
Romain Guy302a9df2011-08-16 13:55:02 -0700134 bool mIsIdentity;
Romain Guyaf28b512010-08-12 14:34:44 -0700135
Romain Guy08ae3172010-06-21 19:35:50 -0700136 inline float get(int i, int j) const {
Romain Guyc7d53492010-06-25 13:41:57 -0700137 return data[i * 4 + j];
Romain Guy08ae3172010-06-21 19:35:50 -0700138 }
139
140 inline void set(int i, int j, float v) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700141 data[i * 4 + j] = v;
Romain Guy08ae3172010-06-21 19:35:50 -0700142 }
Romain Guy08ae3172010-06-21 19:35:50 -0700143}; // class Matrix4
144
145///////////////////////////////////////////////////////////////////////////////
146// Types
147///////////////////////////////////////////////////////////////////////////////
148
149typedef Matrix4 mat4;
150
Romain Guy9d5316e2010-06-24 19:30:36 -0700151}; // namespace uirenderer
Romain Guy08ae3172010-06-21 19:35:50 -0700152}; // namespace android
153
Romain Guy5b3b3522010-10-27 18:57:51 -0700154#endif // ANDROID_HWUI_MATRIX_H