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 | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HWUI_MATRIX_H |
| 18 | #define ANDROID_HWUI_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 | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 35 | enum Entry { |
| 36 | kScaleX = 0, |
| 37 | kSkewY = 1, |
| 38 | kPerspective0 = 3, |
| 39 | kSkewX = 4, |
| 40 | kScaleY = 5, |
| 41 | kPerspective1 = 7, |
| 42 | kScaleZ = 10, |
| 43 | kTranslateX = 12, |
| 44 | kTranslateY = 13, |
| 45 | kTranslateZ = 14, |
| 46 | kPerspective2 = 15 |
| 47 | }; |
| 48 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 49 | Matrix4() { |
| 50 | loadIdentity(); |
| 51 | } |
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 | Matrix4(const float* v) { |
| 54 | load(v); |
| 55 | } |
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 | Matrix4(const Matrix4& v) { |
| 58 | load(v); |
| 59 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 60 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 61 | Matrix4(const SkMatrix& v) { |
| 62 | load(v); |
| 63 | } |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 64 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 65 | void loadIdentity(); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 66 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 67 | void load(const float* v); |
| 68 | void load(const Matrix4& v); |
| 69 | void load(const SkMatrix& v); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 70 | |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 71 | void loadInverse(const Matrix4& v); |
| 72 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 73 | void loadTranslate(float x, float y, float z); |
| 74 | void loadScale(float sx, float sy, float sz); |
Romain Guy | 807daf7 | 2011-01-18 11:19:19 -0800 | [diff] [blame] | 75 | void loadSkew(float sx, float sy); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 76 | void loadRotate(float angle, float x, float y, float z); |
| 77 | void loadMultiply(const Matrix4& u, const Matrix4& v); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 78 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 79 | 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] | 80 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 81 | void multiply(const Matrix4& v) { |
| 82 | Matrix4 u; |
| 83 | u.loadMultiply(*this, v); |
| 84 | load(u); |
| 85 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 86 | |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 87 | void multiply(float v); |
| 88 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 89 | void translate(float x, float y, float z) { |
| 90 | Matrix4 u; |
| 91 | u.loadTranslate(x, y, z); |
| 92 | multiply(u); |
| 93 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 94 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 95 | void scale(float sx, float sy, float sz) { |
| 96 | Matrix4 u; |
| 97 | u.loadScale(sx, sy, sz); |
| 98 | multiply(u); |
| 99 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 100 | |
Romain Guy | 807daf7 | 2011-01-18 11:19:19 -0800 | [diff] [blame] | 101 | void skew(float sx, float sy) { |
| 102 | Matrix4 u; |
| 103 | u.loadSkew(sx, sy); |
| 104 | multiply(u); |
| 105 | } |
| 106 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 107 | void rotate(float angle, float x, float y, float z) { |
| 108 | Matrix4 u; |
| 109 | u.loadRotate(angle, x, y, z); |
| 110 | multiply(u); |
| 111 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 112 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 113 | bool isPureTranslate(); |
| 114 | |
Romain Guy | e8cb9c14 | 2010-10-04 14:14:11 -0700 | [diff] [blame] | 115 | bool changesBounds(); |
| 116 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 117 | void copyTo(float* v) const; |
| 118 | void copyTo(SkMatrix& v) const; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 119 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 120 | void mapRect(Rect& r) const; |
Romain Guy | 0ba681b | 2010-08-12 15:37:00 -0700 | [diff] [blame] | 121 | void mapPoint(float& x, float& y) const; |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 122 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 123 | float getTranslateX(); |
| 124 | float getTranslateY(); |
| 125 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 126 | void dump() const; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 127 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 128 | private: |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 129 | bool mSimpleMatrix; |
| 130 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 131 | inline float get(int i, int j) const { |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 132 | return data[i * 4 + j]; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | inline void set(int i, int j, float v) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 136 | data[i * 4 + j] = v; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 137 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 138 | }; // class Matrix4 |
| 139 | |
| 140 | /////////////////////////////////////////////////////////////////////////////// |
| 141 | // Types |
| 142 | /////////////////////////////////////////////////////////////////////////////// |
| 143 | |
| 144 | typedef Matrix4 mat4; |
| 145 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 146 | }; // namespace uirenderer |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 147 | }; // namespace android |
| 148 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 149 | #endif // ANDROID_HWUI_MATRIX_H |