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: |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 33 | float data[16]; |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 34 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 35 | Matrix4() { |
| 36 | loadIdentity(); |
| 37 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 38 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 39 | Matrix4(const float* v) { |
| 40 | load(v); |
| 41 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 42 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 43 | Matrix4(const Matrix4& v) { |
| 44 | load(v); |
| 45 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 46 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 47 | Matrix4(const SkMatrix& v) { |
| 48 | load(v); |
| 49 | } |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 50 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 51 | void loadIdentity(); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 52 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 53 | void load(const float* v); |
| 54 | void load(const Matrix4& v); |
| 55 | void load(const SkMatrix& v); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 56 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 57 | 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 Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 61 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 62 | void loadOrtho(float left, float right, float bottom, float top, float near, float far); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 63 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 64 | void multiply(const Matrix4& v) { |
| 65 | Matrix4 u; |
| 66 | u.loadMultiply(*this, v); |
| 67 | load(u); |
| 68 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 69 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 70 | void translate(float x, float y, float z) { |
| 71 | Matrix4 u; |
| 72 | u.loadTranslate(x, y, z); |
| 73 | multiply(u); |
| 74 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 75 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 76 | void scale(float sx, float sy, float sz) { |
| 77 | Matrix4 u; |
| 78 | u.loadScale(sx, sy, sz); |
| 79 | multiply(u); |
| 80 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 81 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 82 | 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 Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 87 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 88 | void copyTo(float* v) const; |
| 89 | void copyTo(SkMatrix& v) const; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 90 | |
Romain Guy | 3d58c03 | 2010-07-14 16:34:53 -0700 | [diff] [blame^] | 91 | /** |
| 92 | * Does not apply rotations! |
| 93 | */ |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 94 | void mapRect(Rect& r) const; |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 95 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 96 | float getTranslateX(); |
| 97 | float getTranslateY(); |
| 98 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 99 | void dump() const; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 100 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 101 | private: |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 102 | inline float get(int i, int j) const { |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 103 | return data[i * 4 + j]; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | inline void set(int i, int j, float v) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 107 | data[i * 4 + j] = v; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 108 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 109 | }; // class Matrix4 |
| 110 | |
| 111 | /////////////////////////////////////////////////////////////////////////////// |
| 112 | // Types |
| 113 | /////////////////////////////////////////////////////////////////////////////// |
| 114 | |
| 115 | typedef Matrix4 mat4; |
| 116 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 117 | }; // namespace uirenderer |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 118 | }; // namespace android |
| 119 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 120 | #endif // ANDROID_UI_MATRIX_H |