blob: fa9638bd1c83e5fd4878c6162e1e3b549d2c05ce [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 Guy7ae7ac42010-06-25 13:46:18 -070057 void loadTranslate(float x, float y, float z);
58 void loadScale(float sx, float sy, float sz);
59 void loadRotate(float angle, float x, float y, float z);
60 void loadMultiply(const Matrix4& u, const Matrix4& v);
Romain Guy08ae3172010-06-21 19:35:50 -070061
Romain Guy7ae7ac42010-06-25 13:46:18 -070062 void loadOrtho(float left, float right, float bottom, float top, float near, float far);
Romain Guy08ae3172010-06-21 19:35:50 -070063
Romain Guy7ae7ac42010-06-25 13:46:18 -070064 void multiply(const Matrix4& v) {
65 Matrix4 u;
66 u.loadMultiply(*this, v);
67 load(u);
68 }
Romain Guy08ae3172010-06-21 19:35:50 -070069
Romain Guy7ae7ac42010-06-25 13:46:18 -070070 void translate(float x, float y, float z) {
71 Matrix4 u;
72 u.loadTranslate(x, y, z);
73 multiply(u);
74 }
Romain Guy08ae3172010-06-21 19:35:50 -070075
Romain Guy7ae7ac42010-06-25 13:46:18 -070076 void scale(float sx, float sy, float sz) {
77 Matrix4 u;
78 u.loadScale(sx, sy, sz);
79 multiply(u);
80 }
Romain Guy08ae3172010-06-21 19:35:50 -070081
Romain Guy7ae7ac42010-06-25 13:46:18 -070082 void rotate(float angle, float x, float y, float z) {
83 Matrix4 u;
84 u.loadRotate(angle, x, y, z);
85 multiply(u);
86 }
Romain Guy08ae3172010-06-21 19:35:50 -070087
Romain Guy7ae7ac42010-06-25 13:46:18 -070088 void copyTo(float* v) const;
89 void copyTo(SkMatrix& v) const;
Romain Guy08ae3172010-06-21 19:35:50 -070090
Romain Guy7ae7ac42010-06-25 13:46:18 -070091 void mapRect(Rect& r) const;
Romain Guy9d5316e2010-06-24 19:30:36 -070092
Romain Guy7ae7ac42010-06-25 13:46:18 -070093 void dump() const;
Romain Guy08ae3172010-06-21 19:35:50 -070094
Romain Guyf6a11b82010-06-23 17:47:49 -070095private:
Romain Guy08ae3172010-06-21 19:35:50 -070096 inline float get(int i, int j) const {
Romain Guyc7d53492010-06-25 13:41:57 -070097 return data[i * 4 + j];
Romain Guy08ae3172010-06-21 19:35:50 -070098 }
99
100 inline void set(int i, int j, float v) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700101 data[i * 4 + j] = v;
Romain Guy08ae3172010-06-21 19:35:50 -0700102 }
Romain Guy08ae3172010-06-21 19:35:50 -0700103}; // class Matrix4
104
105///////////////////////////////////////////////////////////////////////////////
106// Types
107///////////////////////////////////////////////////////////////////////////////
108
109typedef Matrix4 mat4;
110
Romain Guy9d5316e2010-06-24 19:30:36 -0700111}; // namespace uirenderer
Romain Guy08ae3172010-06-21 19:35:50 -0700112}; // namespace android
113
Romain Guy9d5316e2010-06-24 19:30:36 -0700114#endif // ANDROID_UI_MATRIX_H