blob: b8a4da77619c08cb6b2f9e4e3062bee10a2a4544 [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 Guy9d5316e2010-06-24 19:30:36 -070017#ifndef ANDROID_UI_MATRIX_H
18#define ANDROID_UI_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 Guy7ae7ac42010-06-25 13:46:18 -070035 Matrix4() {
36 loadIdentity();
37 }
Romain Guy08ae3172010-06-21 19:35:50 -070038
Romain Guy7ae7ac42010-06-25 13:46:18 -070039 Matrix4(const float* v) {
40 load(v);
41 }
Romain Guy08ae3172010-06-21 19:35:50 -070042
Romain Guy7ae7ac42010-06-25 13:46:18 -070043 Matrix4(const Matrix4& v) {
44 load(v);
45 }
Romain Guy08ae3172010-06-21 19:35:50 -070046
Romain Guy7ae7ac42010-06-25 13:46:18 -070047 Matrix4(const SkMatrix& v) {
48 load(v);
49 }
Romain Guyf6a11b82010-06-23 17:47:49 -070050
Romain Guy7ae7ac42010-06-25 13:46:18 -070051 void loadIdentity();
Romain Guy08ae3172010-06-21 19:35:50 -070052
Romain Guy7ae7ac42010-06-25 13:46:18 -070053 void load(const float* v);
54 void load(const Matrix4& v);
55 void load(const SkMatrix& v);
Romain Guy08ae3172010-06-21 19:35:50 -070056
Romain Guy079ba2c2010-07-16 14:12:24 -070057 void loadInverse(const Matrix4& v);
58
Romain Guy7ae7ac42010-06-25 13:46:18 -070059 void loadTranslate(float x, float y, float z);
60 void loadScale(float sx, float sy, float sz);
61 void loadRotate(float angle, float x, float y, float z);
62 void loadMultiply(const Matrix4& u, const Matrix4& v);
Romain Guy08ae3172010-06-21 19:35:50 -070063
Romain Guy7ae7ac42010-06-25 13:46:18 -070064 void loadOrtho(float left, float right, float bottom, float top, float near, float far);
Romain Guy08ae3172010-06-21 19:35:50 -070065
Romain Guy7ae7ac42010-06-25 13:46:18 -070066 void multiply(const Matrix4& v) {
67 Matrix4 u;
68 u.loadMultiply(*this, v);
69 load(u);
70 }
Romain Guy08ae3172010-06-21 19:35:50 -070071
Romain Guy7ae7ac42010-06-25 13:46:18 -070072 void translate(float x, float y, float z) {
73 Matrix4 u;
74 u.loadTranslate(x, y, z);
75 multiply(u);
76 }
Romain Guy08ae3172010-06-21 19:35:50 -070077
Romain Guy7ae7ac42010-06-25 13:46:18 -070078 void scale(float sx, float sy, float sz) {
79 Matrix4 u;
80 u.loadScale(sx, sy, sz);
81 multiply(u);
82 }
Romain Guy08ae3172010-06-21 19:35:50 -070083
Romain Guy7ae7ac42010-06-25 13:46:18 -070084 void rotate(float angle, float x, float y, float z) {
85 Matrix4 u;
86 u.loadRotate(angle, x, y, z);
87 multiply(u);
88 }
Romain Guy08ae3172010-06-21 19:35:50 -070089
Romain Guy7ae7ac42010-06-25 13:46:18 -070090 void copyTo(float* v) const;
91 void copyTo(SkMatrix& v) const;
Romain Guy08ae3172010-06-21 19:35:50 -070092
Romain Guy3d58c032010-07-14 16:34:53 -070093 /**
94 * Does not apply rotations!
95 */
Romain Guy7ae7ac42010-06-25 13:46:18 -070096 void mapRect(Rect& r) const;
Romain Guy9d5316e2010-06-24 19:30:36 -070097
Romain Guybd6b79b2010-06-26 00:13:53 -070098 float getTranslateX();
99 float getTranslateY();
100
Romain Guy7ae7ac42010-06-25 13:46:18 -0700101 void dump() const;
Romain Guy08ae3172010-06-21 19:35:50 -0700102
Romain Guyf6a11b82010-06-23 17:47:49 -0700103private:
Romain Guy08ae3172010-06-21 19:35:50 -0700104 inline float get(int i, int j) const {
Romain Guyc7d53492010-06-25 13:41:57 -0700105 return data[i * 4 + j];
Romain Guy08ae3172010-06-21 19:35:50 -0700106 }
107
108 inline void set(int i, int j, float v) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700109 data[i * 4 + j] = v;
Romain Guy08ae3172010-06-21 19:35:50 -0700110 }
Romain Guy08ae3172010-06-21 19:35:50 -0700111}; // class Matrix4
112
113///////////////////////////////////////////////////////////////////////////////
114// Types
115///////////////////////////////////////////////////////////////////////////////
116
117typedef Matrix4 mat4;
118
Romain Guy9d5316e2010-06-24 19:30:36 -0700119}; // namespace uirenderer
Romain Guy08ae3172010-06-21 19:35:50 -0700120}; // namespace android
121
Romain Guy9d5316e2010-06-24 19:30:36 -0700122#endif // ANDROID_UI_MATRIX_H