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 | 71e36aa | 2011-10-12 14:11:32 -0700 | [diff] [blame] | 22 | #include <cutils/compiler.h> |
| 23 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 24 | #include "Rect.h" |
| 25 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 26 | namespace android { |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 27 | namespace uirenderer { |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 28 | |
Chris Craik | 629f677 | 2014-02-05 13:00:24 -0800 | [diff] [blame] | 29 | #define SK_MATRIX_STRING "[%.2f %.2f %.2f] [%.2f %.2f %.2f] [%.2f %.2f %.2f]" |
| 30 | #define SK_MATRIX_ARGS(m) \ |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 31 | (m)->get(0), (m)->get(1), (m)->get(2), \ |
| 32 | (m)->get(3), (m)->get(4), (m)->get(5), \ |
| 33 | (m)->get(6), (m)->get(7), (m)->get(8) |
| 34 | |
Chris Craik | 629f677 | 2014-02-05 13:00:24 -0800 | [diff] [blame] | 35 | #define MATRIX_4_STRING "[%.2f %.2f %.2f %.2f] [%.2f %.2f %.2f %.2f]" \ |
| 36 | " [%.2f %.2f %.2f %.2f] [%.2f %.2f %.2f %.2f]" |
| 37 | #define MATRIX_4_ARGS(m) \ |
| 38 | (m)->data[0], (m)->data[4], (m)->data[8], (m)->data[12], \ |
| 39 | (m)->data[1], (m)->data[5], (m)->data[9], (m)->data[13], \ |
| 40 | (m)->data[2], (m)->data[6], (m)->data[10], (m)->data[14], \ |
| 41 | (m)->data[3], (m)->data[7], (m)->data[11], (m)->data[15] \ |
| 42 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 43 | /////////////////////////////////////////////////////////////////////////////// |
| 44 | // Classes |
| 45 | /////////////////////////////////////////////////////////////////////////////// |
| 46 | |
Romain Guy | 71e36aa | 2011-10-12 14:11:32 -0700 | [diff] [blame] | 47 | class ANDROID_API Matrix4 { |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 48 | public: |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 49 | float data[16]; |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 50 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 51 | enum Entry { |
| 52 | kScaleX = 0, |
| 53 | kSkewY = 1, |
| 54 | kPerspective0 = 3, |
| 55 | kSkewX = 4, |
| 56 | kScaleY = 5, |
| 57 | kPerspective1 = 7, |
| 58 | kScaleZ = 10, |
| 59 | kTranslateX = 12, |
| 60 | kTranslateY = 13, |
| 61 | kTranslateZ = 14, |
| 62 | kPerspective2 = 15 |
| 63 | }; |
| 64 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 65 | // NOTE: The flags from kTypeIdentity to kTypePerspective |
| 66 | // must be kept in sync with the type flags found |
| 67 | // in SkMatrix |
| 68 | enum Type { |
| 69 | kTypeIdentity = 0, |
| 70 | kTypeTranslate = 0x1, |
| 71 | kTypeScale = 0x2, |
| 72 | kTypeAffine = 0x4, |
| 73 | kTypePerspective = 0x8, |
| 74 | kTypeRectToRect = 0x10, |
Chris Craik | 996fe65 | 2013-09-20 17:13:18 -0700 | [diff] [blame] | 75 | kTypeUnknown = 0x20, |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | static const int sGeometryMask = 0xf; |
| 79 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 80 | Matrix4() { |
| 81 | loadIdentity(); |
| 82 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 83 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 84 | Matrix4(const float* v) { |
| 85 | load(v); |
| 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 | Matrix4(const Matrix4& v) { |
| 89 | load(v); |
| 90 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 91 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 92 | Matrix4(const SkMatrix& v) { |
| 93 | load(v); |
| 94 | } |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 95 | |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 96 | float operator[](int index) const { |
| 97 | return data[index]; |
| 98 | } |
| 99 | |
| 100 | float& operator[](int index) { |
| 101 | mType = kTypeUnknown; |
| 102 | return data[index]; |
| 103 | } |
| 104 | |
| 105 | Matrix4& operator=(const SkMatrix& v) { |
| 106 | load(v); |
| 107 | return *this; |
| 108 | } |
| 109 | |
Romain Guy | bd3055f | 2013-03-13 16:14:47 -0700 | [diff] [blame] | 110 | friend bool operator==(const Matrix4& a, const Matrix4& b) { |
| 111 | return !memcmp(&a.data[0], &b.data[0], 16 * sizeof(float)); |
| 112 | } |
| 113 | |
| 114 | friend bool operator!=(const Matrix4& a, const Matrix4& b) { |
| 115 | return !(a == b); |
| 116 | } |
| 117 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 118 | void loadIdentity(); |
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 load(const float* v); |
| 121 | void load(const Matrix4& v); |
| 122 | void load(const SkMatrix& v); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 123 | |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 124 | void loadInverse(const Matrix4& v); |
| 125 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 126 | void loadTranslate(float x, float y, float z); |
| 127 | void loadScale(float sx, float sy, float sz); |
Romain Guy | 807daf7 | 2011-01-18 11:19:19 -0800 | [diff] [blame] | 128 | void loadSkew(float sx, float sy); |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 129 | void loadRotate(float angle); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 130 | void loadRotate(float angle, float x, float y, float z); |
| 131 | void loadMultiply(const Matrix4& u, const Matrix4& v); |
Chris Craik | f57776b | 2013-10-25 18:30:17 -0700 | [diff] [blame] | 132 | void loadFrustum(float left, float top, float right, float bottom, float near, float far); |
| 133 | void loadLookAt(float eyeX, float eyeY, float eyeZ, |
| 134 | float centerX, float centerY, float centerZ, |
| 135 | float upX, float upY, float upZ); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 136 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 137 | 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] | 138 | |
Romain Guy | f6bed4f | 2013-06-20 17:52:07 -0700 | [diff] [blame] | 139 | uint8_t getType() const; |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 140 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 141 | void multiply(const Matrix4& v) { |
| 142 | Matrix4 u; |
| 143 | u.loadMultiply(*this, v); |
| 144 | load(u); |
| 145 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 146 | |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 147 | void multiply(float v); |
| 148 | |
Chris Craik | f57776b | 2013-10-25 18:30:17 -0700 | [diff] [blame] | 149 | void translate(float x, float y, float z = 0) { |
Romain Guy | f6bed4f | 2013-06-20 17:52:07 -0700 | [diff] [blame] | 150 | if ((getType() & sGeometryMask) <= kTypeTranslate) { |
Romain Guy | 4c2547f | 2013-06-11 16:19:24 -0700 | [diff] [blame] | 151 | data[kTranslateX] += x; |
| 152 | data[kTranslateY] += y; |
Chris Craik | f57776b | 2013-10-25 18:30:17 -0700 | [diff] [blame] | 153 | data[kTranslateZ] += z; |
Romain Guy | 4c2547f | 2013-06-11 16:19:24 -0700 | [diff] [blame] | 154 | } else { |
| 155 | // Doing a translation will only affect the translate bit of the type |
| 156 | // Save the type |
Romain Guy | f6bed4f | 2013-06-20 17:52:07 -0700 | [diff] [blame] | 157 | uint8_t type = mType; |
Romain Guy | 4c2547f | 2013-06-11 16:19:24 -0700 | [diff] [blame] | 158 | |
| 159 | Matrix4 u; |
Chris Craik | f57776b | 2013-10-25 18:30:17 -0700 | [diff] [blame] | 160 | u.loadTranslate(x, y, z); |
Romain Guy | 4c2547f | 2013-06-11 16:19:24 -0700 | [diff] [blame] | 161 | multiply(u); |
| 162 | |
| 163 | // Restore the type and fix the translate bit |
| 164 | mType = type; |
| 165 | if (data[kTranslateX] != 0.0f || data[kTranslateY] != 0.0f) { |
| 166 | mType |= kTypeTranslate; |
| 167 | } else { |
| 168 | mType &= ~kTypeTranslate; |
| 169 | } |
| 170 | } |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 171 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 172 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 173 | void scale(float sx, float sy, float sz) { |
| 174 | Matrix4 u; |
| 175 | u.loadScale(sx, sy, sz); |
| 176 | multiply(u); |
| 177 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 178 | |
Romain Guy | 807daf7 | 2011-01-18 11:19:19 -0800 | [diff] [blame] | 179 | void skew(float sx, float sy) { |
| 180 | Matrix4 u; |
| 181 | u.loadSkew(sx, sy); |
| 182 | multiply(u); |
| 183 | } |
| 184 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 185 | void rotate(float angle, float x, float y, float z) { |
| 186 | Matrix4 u; |
| 187 | u.loadRotate(angle, x, y, z); |
| 188 | multiply(u); |
| 189 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 190 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 191 | /** |
| 192 | * If the matrix is identity or translate and/or scale. |
| 193 | */ |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 194 | bool isSimple() const; |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 195 | bool isPureTranslate() const; |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 196 | bool isIdentity() const; |
Romain Guy | a3dc55f | 2012-09-28 13:55:44 -0700 | [diff] [blame] | 197 | bool isPerspective() const; |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 198 | bool rectToRect() const; |
Chris Craik | d965bc5 | 2013-09-16 14:47:13 -0700 | [diff] [blame] | 199 | bool positiveScale() const; |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 200 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 201 | bool changesBounds() const; |
Romain Guy | e8cb9c14 | 2010-10-04 14:14:11 -0700 | [diff] [blame] | 202 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 203 | void copyTo(float* v) const; |
| 204 | void copyTo(SkMatrix& v) const; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 205 | |
Chris Craik | f57776b | 2013-10-25 18:30:17 -0700 | [diff] [blame] | 206 | void mapPoint3d(Vector3& vec) const; |
| 207 | void mapPoint(float& x, float& y) const; // 2d only |
| 208 | void mapRect(Rect& r) const; // 2d only |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 209 | |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 210 | float getTranslateX() const; |
| 211 | float getTranslateY() const; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 212 | |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 213 | void decomposeScale(float& sx, float& sy) const; |
| 214 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 215 | void dump() const; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 216 | |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame] | 217 | static const Matrix4& identity(); |
| 218 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 219 | private: |
Romain Guy | f6bed4f | 2013-06-20 17:52:07 -0700 | [diff] [blame] | 220 | mutable uint8_t mType; |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 221 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 222 | inline float get(int i, int j) const { |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 223 | return data[i * 4 + j]; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | inline void set(int i, int j, float v) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 227 | data[i * 4 + j] = v; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 228 | } |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 229 | |
Romain Guy | f6bed4f | 2013-06-20 17:52:07 -0700 | [diff] [blame] | 230 | uint8_t getGeometryType() const; |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 231 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 232 | }; // class Matrix4 |
| 233 | |
| 234 | /////////////////////////////////////////////////////////////////////////////// |
| 235 | // Types |
| 236 | /////////////////////////////////////////////////////////////////////////////// |
| 237 | |
| 238 | typedef Matrix4 mat4; |
| 239 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 240 | }; // namespace uirenderer |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 241 | }; // namespace android |
| 242 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 243 | #endif // ANDROID_HWUI_MATRIX_H |