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