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 | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 17 | #define LOG_TAG "OpenGLRenderer" |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 18 | |
| 19 | #include <math.h> |
| 20 | #include <stdlib.h> |
Romain Guy | 3e16833 | 2010-06-22 13:29:14 -0700 | [diff] [blame] | 21 | #include <string.h> |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 22 | |
| 23 | #include <utils/Log.h> |
| 24 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 25 | #include <SkMatrix.h> |
| 26 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 27 | #include "Matrix.h" |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 30 | namespace uirenderer { |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 31 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 32 | /////////////////////////////////////////////////////////////////////////////// |
| 33 | // Defines |
| 34 | /////////////////////////////////////////////////////////////////////////////// |
| 35 | |
| 36 | static const float EPSILON = 0.0000001f; |
| 37 | |
| 38 | /////////////////////////////////////////////////////////////////////////////// |
| 39 | // Matrix |
| 40 | /////////////////////////////////////////////////////////////////////////////// |
| 41 | |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame] | 42 | const Matrix4& Matrix4::identity() { |
| 43 | static Matrix4 sIdentity; |
| 44 | return sIdentity; |
| 45 | } |
| 46 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 47 | void Matrix4::loadIdentity() { |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 48 | data[kScaleX] = 1.0f; |
| 49 | data[kSkewY] = 0.0f; |
| 50 | data[2] = 0.0f; |
| 51 | data[kPerspective0] = 0.0f; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 52 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 53 | data[kSkewX] = 0.0f; |
| 54 | data[kScaleY] = 1.0f; |
| 55 | data[6] = 0.0f; |
| 56 | data[kPerspective1] = 0.0f; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 57 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 58 | data[8] = 0.0f; |
| 59 | data[9] = 0.0f; |
| 60 | data[kScaleZ] = 1.0f; |
| 61 | data[11] = 0.0f; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 62 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 63 | data[kTranslateX] = 0.0f; |
| 64 | data[kTranslateY] = 0.0f; |
| 65 | data[kTranslateZ] = 0.0f; |
| 66 | data[kPerspective2] = 1.0f; |
| 67 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 68 | mType = kTypeIdentity | kTypeRectToRect; |
| 69 | } |
| 70 | |
| 71 | static bool isZero(float f) { |
| 72 | return fabs(f) <= EPSILON; |
| 73 | } |
| 74 | |
Romain Guy | f6bed4f | 2013-06-20 17:52:07 -0700 | [diff] [blame] | 75 | uint8_t Matrix4::getType() const { |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 76 | if (mType & kTypeUnknown) { |
| 77 | mType = kTypeIdentity; |
| 78 | |
| 79 | if (data[kPerspective0] != 0.0f || data[kPerspective1] != 0.0f || |
| 80 | data[kPerspective2] != 1.0f) { |
| 81 | mType |= kTypePerspective; |
| 82 | } |
| 83 | |
| 84 | if (data[kTranslateX] != 0.0f || data[kTranslateY] != 0.0f) { |
| 85 | mType |= kTypeTranslate; |
| 86 | } |
| 87 | |
| 88 | float m00 = data[kScaleX]; |
| 89 | float m01 = data[kSkewX]; |
| 90 | float m10 = data[kSkewY]; |
| 91 | float m11 = data[kScaleY]; |
Chris Craik | f57776b | 2013-10-25 18:30:17 -0700 | [diff] [blame] | 92 | float m32 = data[kTranslateZ]; |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 93 | |
Chris Craik | f57776b | 2013-10-25 18:30:17 -0700 | [diff] [blame] | 94 | if (m01 != 0.0f || m10 != 0.0f || m32 != 0.0f) { |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 95 | mType |= kTypeAffine; |
| 96 | } |
| 97 | |
| 98 | if (m00 != 1.0f || m11 != 1.0f) { |
| 99 | mType |= kTypeScale; |
| 100 | } |
| 101 | |
| 102 | // The following section determines whether the matrix will preserve |
| 103 | // rectangles. For instance, a rectangle transformed by a pure |
| 104 | // translation matrix will result in a rectangle. A rectangle |
| 105 | // transformed by a 45 degrees rotation matrix is not a rectangle. |
| 106 | // If the matrix has a perspective component then we already know |
| 107 | // it doesn't preserve rectangles. |
| 108 | if (!(mType & kTypePerspective)) { |
| 109 | if ((isZero(m00) && isZero(m11) && !isZero(m01) && !isZero(m10)) || |
| 110 | (isZero(m01) && isZero(m10) && !isZero(m00) && !isZero(m11))) { |
| 111 | mType |= kTypeRectToRect; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | return mType; |
| 116 | } |
| 117 | |
Romain Guy | f6bed4f | 2013-06-20 17:52:07 -0700 | [diff] [blame] | 118 | uint8_t Matrix4::getGeometryType() const { |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 119 | return getType() & sGeometryMask; |
| 120 | } |
| 121 | |
| 122 | bool Matrix4::rectToRect() const { |
| 123 | return getType() & kTypeRectToRect; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Chris Craik | d965bc5 | 2013-09-16 14:47:13 -0700 | [diff] [blame] | 126 | bool Matrix4::positiveScale() const { |
Chris Craik | 996fe65 | 2013-09-20 17:13:18 -0700 | [diff] [blame] | 127 | return (data[kScaleX] > 0.0f && data[kScaleY] > 0.0f); |
Chris Craik | d965bc5 | 2013-09-16 14:47:13 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 130 | bool Matrix4::changesBounds() const { |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 131 | return getType() & (kTypeScale | kTypeAffine | kTypePerspective); |
Romain Guy | e8cb9c14 | 2010-10-04 14:14:11 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 134 | bool Matrix4::isPureTranslate() const { |
Chris Craik | f57776b | 2013-10-25 18:30:17 -0700 | [diff] [blame] | 135 | // NOTE: temporary hack to workaround ignoreTransform behavior with Z values |
| 136 | // TODO: separate this into isPure2dTranslate vs isPure3dTranslate |
| 137 | return getGeometryType() <= kTypeTranslate && (data[kTranslateZ] == 0.0f); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 140 | bool Matrix4::isSimple() const { |
Chris Craik | f57776b | 2013-10-25 18:30:17 -0700 | [diff] [blame] | 141 | return getGeometryType() <= (kTypeScale | kTypeTranslate) && (data[kTranslateZ] == 0.0f); |
Romain Guy | 181d0a6 | 2011-06-09 18:52:38 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 144 | bool Matrix4::isIdentity() const { |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 145 | return getGeometryType() == kTypeIdentity; |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Romain Guy | a3dc55f | 2012-09-28 13:55:44 -0700 | [diff] [blame] | 148 | bool Matrix4::isPerspective() const { |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 149 | return getType() & kTypePerspective; |
Romain Guy | a3dc55f | 2012-09-28 13:55:44 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 152 | void Matrix4::load(const float* v) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 153 | memcpy(data, v, sizeof(data)); |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 154 | mType = kTypeUnknown; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void Matrix4::load(const Matrix4& v) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 158 | memcpy(data, v.data, sizeof(data)); |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 159 | mType = v.getType(); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 162 | void Matrix4::load(const SkMatrix& v) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 163 | memset(data, 0, sizeof(data)); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 164 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 165 | data[kScaleX] = v[SkMatrix::kMScaleX]; |
| 166 | data[kSkewX] = v[SkMatrix::kMSkewX]; |
| 167 | data[kTranslateX] = v[SkMatrix::kMTransX]; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 168 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 169 | data[kSkewY] = v[SkMatrix::kMSkewY]; |
| 170 | data[kScaleY] = v[SkMatrix::kMScaleY]; |
| 171 | data[kTranslateY] = v[SkMatrix::kMTransY]; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 172 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 173 | data[kPerspective0] = v[SkMatrix::kMPersp0]; |
| 174 | data[kPerspective1] = v[SkMatrix::kMPersp1]; |
| 175 | data[kPerspective2] = v[SkMatrix::kMPersp2]; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 176 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 177 | data[kScaleZ] = 1.0f; |
| 178 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 179 | // NOTE: The flags are compatible between SkMatrix and this class. |
| 180 | // However, SkMatrix::getType() does not return the flag |
| 181 | // kRectStaysRect. The return value is masked with 0xF |
| 182 | // so we need the extra rectStaysRect() check |
| 183 | mType = v.getType(); |
| 184 | if (v.rectStaysRect()) { |
| 185 | mType |= kTypeRectToRect; |
| 186 | } |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | void Matrix4::copyTo(SkMatrix& v) const { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 190 | v.reset(); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 191 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 192 | v.set(SkMatrix::kMScaleX, data[kScaleX]); |
| 193 | v.set(SkMatrix::kMSkewX, data[kSkewX]); |
| 194 | v.set(SkMatrix::kMTransX, data[kTranslateX]); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 195 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 196 | v.set(SkMatrix::kMSkewY, data[kSkewY]); |
| 197 | v.set(SkMatrix::kMScaleY, data[kScaleY]); |
| 198 | v.set(SkMatrix::kMTransY, data[kTranslateY]); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 199 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 200 | v.set(SkMatrix::kMPersp0, data[kPerspective0]); |
| 201 | v.set(SkMatrix::kMPersp1, data[kPerspective1]); |
| 202 | v.set(SkMatrix::kMPersp2, data[kPerspective2]); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 205 | void Matrix4::loadInverse(const Matrix4& v) { |
| 206 | double scale = 1.0 / |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 207 | (v.data[kScaleX] * ((double) v.data[kScaleY] * v.data[kPerspective2] - |
| 208 | (double) v.data[kTranslateY] * v.data[kPerspective1]) + |
| 209 | v.data[kSkewX] * ((double) v.data[kTranslateY] * v.data[kPerspective0] - |
| 210 | (double) v.data[kSkewY] * v.data[kPerspective2]) + |
| 211 | v.data[kTranslateX] * ((double) v.data[kSkewY] * v.data[kPerspective1] - |
| 212 | (double) v.data[kScaleY] * v.data[kPerspective0])); |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 213 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 214 | data[kScaleX] = (v.data[kScaleY] * v.data[kPerspective2] - |
| 215 | v.data[kTranslateY] * v.data[kPerspective1]) * scale; |
| 216 | data[kSkewX] = (v.data[kTranslateX] * v.data[kPerspective1] - |
| 217 | v.data[kSkewX] * v.data[kPerspective2]) * scale; |
| 218 | data[kTranslateX] = (v.data[kSkewX] * v.data[kTranslateY] - |
| 219 | v.data[kTranslateX] * v.data[kScaleY]) * scale; |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 220 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 221 | data[kSkewY] = (v.data[kTranslateY] * v.data[kPerspective0] - |
| 222 | v.data[kSkewY] * v.data[kPerspective2]) * scale; |
| 223 | data[kScaleY] = (v.data[kScaleX] * v.data[kPerspective2] - |
| 224 | v.data[kTranslateX] * v.data[kPerspective0]) * scale; |
| 225 | data[kTranslateY] = (v.data[kTranslateX] * v.data[kSkewY] - |
| 226 | v.data[kScaleX] * v.data[kTranslateY]) * scale; |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 227 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 228 | data[kPerspective0] = (v.data[kSkewY] * v.data[kPerspective1] - |
| 229 | v.data[kScaleY] * v.data[kPerspective0]) * scale; |
| 230 | data[kPerspective1] = (v.data[kSkewX] * v.data[kPerspective0] - |
| 231 | v.data[kScaleX] * v.data[kPerspective1]) * scale; |
| 232 | data[kPerspective2] = (v.data[kScaleX] * v.data[kScaleY] - |
| 233 | v.data[kSkewX] * v.data[kSkewY]) * scale; |
| 234 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 235 | mType = kTypeUnknown; |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 238 | void Matrix4::copyTo(float* v) const { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 239 | memcpy(v, data, sizeof(data)); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 242 | float Matrix4::getTranslateX() const { |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 243 | return data[kTranslateX]; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 246 | float Matrix4::getTranslateY() const { |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 247 | return data[kTranslateY]; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 250 | void Matrix4::multiply(float v) { |
| 251 | for (int i = 0; i < 16; i++) { |
| 252 | data[i] *= v; |
| 253 | } |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 254 | mType = kTypeUnknown; |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 257 | void Matrix4::loadTranslate(float x, float y, float z) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 258 | loadIdentity(); |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 259 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 260 | data[kTranslateX] = x; |
| 261 | data[kTranslateY] = y; |
| 262 | data[kTranslateZ] = z; |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 263 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 264 | mType = kTypeTranslate | kTypeRectToRect; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void Matrix4::loadScale(float sx, float sy, float sz) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 268 | loadIdentity(); |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 269 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 270 | data[kScaleX] = sx; |
| 271 | data[kScaleY] = sy; |
| 272 | data[kScaleZ] = sz; |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 273 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 274 | mType = kTypeScale | kTypeRectToRect; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Romain Guy | 807daf7 | 2011-01-18 11:19:19 -0800 | [diff] [blame] | 277 | void Matrix4::loadSkew(float sx, float sy) { |
| 278 | loadIdentity(); |
| 279 | |
| 280 | data[kScaleX] = 1.0f; |
| 281 | data[kSkewX] = sx; |
| 282 | data[kTranslateX] = 0.0f; |
| 283 | |
| 284 | data[kSkewY] = sy; |
| 285 | data[kScaleY] = 1.0f; |
| 286 | data[kTranslateY] = 0.0f; |
| 287 | |
| 288 | data[kPerspective0] = 0.0f; |
| 289 | data[kPerspective1] = 0.0f; |
| 290 | data[kPerspective2] = 1.0f; |
| 291 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 292 | mType = kTypeUnknown; |
| 293 | } |
| 294 | |
| 295 | void Matrix4::loadRotate(float angle) { |
| 296 | angle *= float(M_PI / 180.0f); |
| 297 | float c = cosf(angle); |
| 298 | float s = sinf(angle); |
| 299 | |
| 300 | loadIdentity(); |
| 301 | |
| 302 | data[kScaleX] = c; |
| 303 | data[kSkewX] = -s; |
| 304 | |
| 305 | data[kSkewY] = s; |
| 306 | data[kScaleY] = c; |
| 307 | |
| 308 | mType = kTypeUnknown; |
Romain Guy | 807daf7 | 2011-01-18 11:19:19 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 311 | void Matrix4::loadRotate(float angle, float x, float y, float z) { |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 312 | data[kPerspective0] = 0.0f; |
| 313 | data[kPerspective1] = 0.0f; |
| 314 | data[11] = 0.0f; |
| 315 | data[kTranslateX] = 0.0f; |
| 316 | data[kTranslateY] = 0.0f; |
| 317 | data[kTranslateZ] = 0.0f; |
| 318 | data[kPerspective2] = 1.0f; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 319 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 320 | angle *= float(M_PI / 180.0f); |
| 321 | float c = cosf(angle); |
| 322 | float s = sinf(angle); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 323 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 324 | const float length = sqrtf(x * x + y * y + z * z); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 325 | float recipLen = 1.0f / length; |
| 326 | x *= recipLen; |
| 327 | y *= recipLen; |
| 328 | z *= recipLen; |
| 329 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 330 | const float nc = 1.0f - c; |
| 331 | const float xy = x * y; |
| 332 | const float yz = y * z; |
| 333 | const float zx = z * x; |
| 334 | const float xs = x * s; |
| 335 | const float ys = y * s; |
| 336 | const float zs = z * s; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 337 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 338 | data[kScaleX] = x * x * nc + c; |
| 339 | data[kSkewX] = xy * nc - zs; |
| 340 | data[8] = zx * nc + ys; |
| 341 | data[kSkewY] = xy * nc + zs; |
| 342 | data[kScaleY] = y * y * nc + c; |
| 343 | data[9] = yz * nc - xs; |
| 344 | data[2] = zx * nc - ys; |
| 345 | data[6] = yz * nc + xs; |
| 346 | data[kScaleZ] = z * z * nc + c; |
| 347 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 348 | mType = kTypeUnknown; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | void Matrix4::loadMultiply(const Matrix4& u, const Matrix4& v) { |
| 352 | for (int i = 0 ; i < 4 ; i++) { |
| 353 | float x = 0; |
| 354 | float y = 0; |
| 355 | float z = 0; |
| 356 | float w = 0; |
| 357 | |
| 358 | for (int j = 0 ; j < 4 ; j++) { |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 359 | const float e = v.get(i, j); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 360 | x += u.get(j, 0) * e; |
| 361 | y += u.get(j, 1) * e; |
| 362 | z += u.get(j, 2) * e; |
| 363 | w += u.get(j, 3) * e; |
| 364 | } |
| 365 | |
| 366 | set(i, 0, x); |
| 367 | set(i, 1, y); |
| 368 | set(i, 2, z); |
| 369 | set(i, 3, w); |
| 370 | } |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 371 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 372 | mType = kTypeUnknown; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | void Matrix4::loadOrtho(float left, float right, float bottom, float top, float near, float far) { |
| 376 | loadIdentity(); |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 377 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 378 | data[kScaleX] = 2.0f / (right - left); |
| 379 | data[kScaleY] = 2.0f / (top - bottom); |
| 380 | data[kScaleZ] = -2.0f / (far - near); |
| 381 | data[kTranslateX] = -(right + left) / (right - left); |
| 382 | data[kTranslateY] = -(top + bottom) / (top - bottom); |
| 383 | data[kTranslateZ] = -(far + near) / (far - near); |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 384 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 385 | mType = kTypeTranslate | kTypeScale | kTypeRectToRect; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Chris Craik | b79a3e3 | 2014-03-11 12:20:17 -0700 | [diff] [blame] | 388 | float Matrix4::mapZ(const Vector3& orig) const { |
| 389 | // duplicates logic for mapPoint3d's z coordinate |
| 390 | return orig.x * data[2] + orig.y * data[6] + orig.z * data[kScaleZ] + data[kTranslateZ]; |
| 391 | } |
| 392 | |
Chris Craik | f57776b | 2013-10-25 18:30:17 -0700 | [diff] [blame] | 393 | void Matrix4::mapPoint3d(Vector3& vec) const { |
| 394 | //TODO: optimize simple case |
Chris Craik | b79a3e3 | 2014-03-11 12:20:17 -0700 | [diff] [blame] | 395 | const Vector3 orig(vec); |
Chris Craik | f57776b | 2013-10-25 18:30:17 -0700 | [diff] [blame] | 396 | vec.x = orig.x * data[kScaleX] + orig.y * data[kSkewX] + orig.z * data[8] + data[kTranslateX]; |
| 397 | vec.y = orig.x * data[kSkewY] + orig.y * data[kScaleY] + orig.z * data[9] + data[kTranslateY]; |
| 398 | vec.z = orig.x * data[2] + orig.y * data[6] + orig.z * data[kScaleZ] + data[kTranslateZ]; |
| 399 | } |
| 400 | |
Romain Guy | 0ba681b | 2010-08-12 15:37:00 -0700 | [diff] [blame] | 401 | #define MUL_ADD_STORE(a, b, c) a = (a) * (b) + (c) |
| 402 | |
| 403 | void Matrix4::mapPoint(float& x, float& y) const { |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 404 | if (isSimple()) { |
Romain Guy | 0ba681b | 2010-08-12 15:37:00 -0700 | [diff] [blame] | 405 | MUL_ADD_STORE(x, data[kScaleX], data[kTranslateX]); |
| 406 | MUL_ADD_STORE(y, data[kScaleY], data[kTranslateY]); |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | float dx = x * data[kScaleX] + y * data[kSkewX] + data[kTranslateX]; |
| 411 | float dy = x * data[kSkewY] + y * data[kScaleY] + data[kTranslateY]; |
| 412 | float dz = x * data[kPerspective0] + y * data[kPerspective1] + data[kPerspective2]; |
| 413 | if (dz) dz = 1.0f / dz; |
| 414 | |
| 415 | x = dx * dz; |
| 416 | y = dy * dz; |
| 417 | } |
| 418 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 419 | void Matrix4::mapRect(Rect& r) const { |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 420 | if (isIdentity()) return; |
| 421 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 422 | if (isSimple()) { |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 423 | MUL_ADD_STORE(r.left, data[kScaleX], data[kTranslateX]); |
| 424 | MUL_ADD_STORE(r.right, data[kScaleX], data[kTranslateX]); |
| 425 | MUL_ADD_STORE(r.top, data[kScaleY], data[kTranslateY]); |
| 426 | MUL_ADD_STORE(r.bottom, data[kScaleY], data[kTranslateY]); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 427 | |
| 428 | if (r.left > r.right) { |
| 429 | float x = r.left; |
| 430 | r.left = r.right; |
| 431 | r.right = x; |
| 432 | } |
| 433 | |
| 434 | if (r.top > r.bottom) { |
| 435 | float y = r.top; |
| 436 | r.top = r.bottom; |
| 437 | r.bottom = y; |
| 438 | } |
| 439 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 440 | return; |
| 441 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 442 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 443 | float vertices[] = { |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 444 | r.left, r.top, |
| 445 | r.right, r.top, |
| 446 | r.right, r.bottom, |
| 447 | r.left, r.bottom |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 448 | }; |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 449 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 450 | float x, y, z; |
| 451 | |
| 452 | for (int i = 0; i < 8; i+= 2) { |
| 453 | float px = vertices[i]; |
| 454 | float py = vertices[i + 1]; |
| 455 | |
| 456 | x = px * data[kScaleX] + py * data[kSkewX] + data[kTranslateX]; |
| 457 | y = px * data[kSkewY] + py * data[kScaleY] + data[kTranslateY]; |
| 458 | z = px * data[kPerspective0] + py * data[kPerspective1] + data[kPerspective2]; |
| 459 | if (z) z = 1.0f / z; |
| 460 | |
| 461 | vertices[i] = x * z; |
| 462 | vertices[i + 1] = y * z; |
| 463 | } |
| 464 | |
| 465 | r.left = r.right = vertices[0]; |
| 466 | r.top = r.bottom = vertices[1]; |
| 467 | |
| 468 | for (int i = 2; i < 8; i += 2) { |
| 469 | x = vertices[i]; |
| 470 | y = vertices[i + 1]; |
| 471 | |
| 472 | if (x < r.left) r.left = x; |
| 473 | else if (x > r.right) r.right = x; |
| 474 | if (y < r.top) r.top = y; |
| 475 | else if (y > r.bottom) r.bottom = y; |
| 476 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 479 | void Matrix4::decomposeScale(float& sx, float& sy) const { |
| 480 | float len; |
| 481 | len = data[mat4::kScaleX] * data[mat4::kScaleX] + data[mat4::kSkewX] * data[mat4::kSkewX]; |
| 482 | sx = copysignf(sqrtf(len), data[mat4::kScaleX]); |
| 483 | len = data[mat4::kScaleY] * data[mat4::kScaleY] + data[mat4::kSkewY] * data[mat4::kSkewY]; |
| 484 | sy = copysignf(sqrtf(len), data[mat4::kScaleY]); |
| 485 | } |
| 486 | |
Chris Craik | e4aa95e | 2014-05-08 13:57:05 -0700 | [diff] [blame] | 487 | void Matrix4::dump(const char* label) const { |
| 488 | ALOGD("%s[simple=%d, type=0x%x", label ? label : "Matrix4", isSimple(), getType()); |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 489 | ALOGD(" %f %f %f %f", data[kScaleX], data[kSkewX], data[8], data[kTranslateX]); |
| 490 | ALOGD(" %f %f %f %f", data[kSkewY], data[kScaleY], data[9], data[kTranslateY]); |
| 491 | ALOGD(" %f %f %f %f", data[2], data[6], data[kScaleZ], data[kTranslateZ]); |
| 492 | ALOGD(" %f %f %f %f", data[kPerspective0], data[kPerspective1], data[11], data[kPerspective2]); |
| 493 | ALOGD("]"); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 496 | }; // namespace uirenderer |
| 497 | }; // namespace android |