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