Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 1 | /* |
| 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 Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame^] | 17 | #ifndef ANDROID_UI_MATRIX_H |
| 18 | #define ANDROID_UI_MATRIX_H |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 19 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 20 | #include <SkMatrix.h> |
| 21 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame^] | 22 | #include "Rect.h" |
| 23 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 24 | namespace android { |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame^] | 25 | namespace uirenderer { |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 26 | |
| 27 | /////////////////////////////////////////////////////////////////////////////// |
| 28 | // Classes |
| 29 | /////////////////////////////////////////////////////////////////////////////// |
| 30 | |
| 31 | class Matrix4 { |
| 32 | public: |
| 33 | Matrix4() { |
| 34 | loadIdentity(); |
| 35 | } |
| 36 | |
| 37 | Matrix4(const float* v) { |
| 38 | load(v); |
| 39 | } |
| 40 | |
| 41 | Matrix4(const Matrix4& v) { |
| 42 | load(v); |
| 43 | } |
| 44 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 45 | Matrix4(const SkMatrix& v) { |
| 46 | load(v); |
| 47 | } |
| 48 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 49 | void loadIdentity(); |
| 50 | |
| 51 | void load(const float* v); |
| 52 | void load(const Matrix4& v); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 53 | void load(const SkMatrix& v); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 54 | |
| 55 | void loadTranslate(float x, float y, float z); |
| 56 | void loadScale(float sx, float sy, float sz); |
| 57 | void loadRotate(float angle, float x, float y, float z); |
| 58 | void loadMultiply(const Matrix4& u, const Matrix4& v); |
| 59 | |
| 60 | void loadOrtho(float left, float right, float bottom, float top, float near, float far); |
| 61 | |
| 62 | void multiply(const Matrix4& v) { |
| 63 | Matrix4 u; |
| 64 | u.loadMultiply(*this, v); |
| 65 | load(u); |
| 66 | } |
| 67 | |
| 68 | void translate(float x, float y, float z) { |
| 69 | Matrix4 u; |
| 70 | u.loadTranslate(x, y, z); |
| 71 | multiply(u); |
| 72 | } |
| 73 | |
| 74 | void scale(float sx, float sy, float sz) { |
| 75 | Matrix4 u; |
| 76 | u.loadScale(sx, sy, sz); |
| 77 | multiply(u); |
| 78 | } |
| 79 | |
| 80 | void rotate(float angle, float x, float y, float z) { |
| 81 | Matrix4 u; |
| 82 | u.loadRotate(angle, x, y, z); |
| 83 | multiply(u); |
| 84 | } |
| 85 | |
| 86 | void copyTo(float* v) const; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 87 | void copyTo(SkMatrix& v) const; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 88 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame^] | 89 | void mapRect(Rect& r) const; |
| 90 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 91 | void dump() const; |
| 92 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 93 | private: |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 94 | inline float get(int i, int j) const { |
| 95 | return mMat[i * 4 + j]; |
| 96 | } |
| 97 | |
| 98 | inline void set(int i, int j, float v) { |
| 99 | mMat[i * 4 + j] = v; |
| 100 | } |
| 101 | |
| 102 | float mMat[16]; |
| 103 | }; // class Matrix4 |
| 104 | |
| 105 | /////////////////////////////////////////////////////////////////////////////// |
| 106 | // Types |
| 107 | /////////////////////////////////////////////////////////////////////////////// |
| 108 | |
| 109 | typedef Matrix4 mat4; |
| 110 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame^] | 111 | }; // namespace uirenderer |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 112 | }; // namespace android |
| 113 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame^] | 114 | #endif // ANDROID_UI_MATRIX_H |