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 | 4bb9420 | 2010-10-12 15:59:26 -0700 | [diff] [blame] | 27 | #include "utils/Compare.h" |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 28 | #include "Matrix.h" |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 29 | |
| 30 | namespace android { |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 31 | namespace uirenderer { |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 32 | |
| 33 | void Matrix4::loadIdentity() { |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 34 | data[kScaleX] = 1.0f; |
| 35 | data[kSkewY] = 0.0f; |
| 36 | data[2] = 0.0f; |
| 37 | data[kPerspective0] = 0.0f; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 38 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 39 | data[kSkewX] = 0.0f; |
| 40 | data[kScaleY] = 1.0f; |
| 41 | data[6] = 0.0f; |
| 42 | data[kPerspective1] = 0.0f; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 43 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 44 | data[8] = 0.0f; |
| 45 | data[9] = 0.0f; |
| 46 | data[kScaleZ] = 1.0f; |
| 47 | data[11] = 0.0f; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 48 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 49 | data[kTranslateX] = 0.0f; |
| 50 | data[kTranslateY] = 0.0f; |
| 51 | data[kTranslateZ] = 0.0f; |
| 52 | data[kPerspective2] = 1.0f; |
| 53 | |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 54 | mIsIdentity = true; |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 55 | mSimpleMatrix = true; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 58 | bool Matrix4::changesBounds() const { |
Chet Haase | d3efd69 | 2012-05-07 12:18:19 -0700 | [diff] [blame] | 59 | return !(data[0] == 1.0f && data[1] == 0.0f && data[2] == 0.0f && data[4] == 0.0f && |
| 60 | data[5] == 1.0f && data[6] == 0.0f && data[8] == 0.0f && data[9] == 0.0f && |
| 61 | data[10] == 1.0f); |
Romain Guy | e8cb9c14 | 2010-10-04 14:14:11 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 64 | bool Matrix4::isPureTranslate() const { |
Chet Haase | d3efd69 | 2012-05-07 12:18:19 -0700 | [diff] [blame] | 65 | return mSimpleMatrix && data[kScaleX] == 1.0f && data[kScaleY] == 1.0f; |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 68 | bool Matrix4::isSimple() const { |
Romain Guy | 181d0a6 | 2011-06-09 18:52:38 -0700 | [diff] [blame] | 69 | return mSimpleMatrix; |
| 70 | } |
| 71 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 72 | bool Matrix4::isIdentity() const { |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 73 | return mIsIdentity; |
| 74 | } |
| 75 | |
Romain Guy | a3dc55f | 2012-09-28 13:55:44 -0700 | [diff] [blame] | 76 | bool Matrix4::isPerspective() const { |
| 77 | return data[kPerspective0] != 0.0f || data[kPerspective1] != 0.0f || |
| 78 | data[kPerspective2] != 1.0f; |
| 79 | } |
| 80 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 81 | void Matrix4::load(const float* v) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 82 | memcpy(data, v, sizeof(data)); |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 83 | // TODO: Do something smarter here |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 84 | mSimpleMatrix = false; |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 85 | mIsIdentity = false; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void Matrix4::load(const Matrix4& v) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 89 | memcpy(data, v.data, sizeof(data)); |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 90 | mSimpleMatrix = v.mSimpleMatrix; |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 91 | mIsIdentity = v.mIsIdentity; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 94 | void Matrix4::load(const SkMatrix& v) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 95 | memset(data, 0, sizeof(data)); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 96 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 97 | data[kScaleX] = v[SkMatrix::kMScaleX]; |
| 98 | data[kSkewX] = v[SkMatrix::kMSkewX]; |
| 99 | data[kTranslateX] = v[SkMatrix::kMTransX]; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 100 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 101 | data[kSkewY] = v[SkMatrix::kMSkewY]; |
| 102 | data[kScaleY] = v[SkMatrix::kMScaleY]; |
| 103 | data[kTranslateY] = v[SkMatrix::kMTransY]; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 104 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 105 | data[kPerspective0] = v[SkMatrix::kMPersp0]; |
| 106 | data[kPerspective1] = v[SkMatrix::kMPersp1]; |
| 107 | data[kPerspective2] = v[SkMatrix::kMPersp2]; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 108 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 109 | data[kScaleZ] = 1.0f; |
| 110 | |
Romain Guy | e5ebcb0 | 2010-10-15 13:57:28 -0700 | [diff] [blame] | 111 | mSimpleMatrix = (v.getType() <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)); |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 112 | mIsIdentity = v.isIdentity(); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void Matrix4::copyTo(SkMatrix& v) const { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 116 | v.reset(); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 117 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 118 | v.set(SkMatrix::kMScaleX, data[kScaleX]); |
| 119 | v.set(SkMatrix::kMSkewX, data[kSkewX]); |
| 120 | v.set(SkMatrix::kMTransX, data[kTranslateX]); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 121 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 122 | v.set(SkMatrix::kMSkewY, data[kSkewY]); |
| 123 | v.set(SkMatrix::kMScaleY, data[kScaleY]); |
| 124 | v.set(SkMatrix::kMTransY, data[kTranslateY]); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 125 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 126 | v.set(SkMatrix::kMPersp0, data[kPerspective0]); |
| 127 | v.set(SkMatrix::kMPersp1, data[kPerspective1]); |
| 128 | v.set(SkMatrix::kMPersp2, data[kPerspective2]); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 131 | void Matrix4::loadInverse(const Matrix4& v) { |
| 132 | double scale = 1.0 / |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 133 | (v.data[kScaleX] * ((double) v.data[kScaleY] * v.data[kPerspective2] - |
| 134 | (double) v.data[kTranslateY] * v.data[kPerspective1]) + |
| 135 | v.data[kSkewX] * ((double) v.data[kTranslateY] * v.data[kPerspective0] - |
| 136 | (double) v.data[kSkewY] * v.data[kPerspective2]) + |
| 137 | v.data[kTranslateX] * ((double) v.data[kSkewY] * v.data[kPerspective1] - |
| 138 | (double) v.data[kScaleY] * v.data[kPerspective0])); |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 139 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 140 | data[kScaleX] = (v.data[kScaleY] * v.data[kPerspective2] - |
| 141 | v.data[kTranslateY] * v.data[kPerspective1]) * scale; |
| 142 | data[kSkewX] = (v.data[kTranslateX] * v.data[kPerspective1] - |
| 143 | v.data[kSkewX] * v.data[kPerspective2]) * scale; |
| 144 | data[kTranslateX] = (v.data[kSkewX] * v.data[kTranslateY] - |
| 145 | v.data[kTranslateX] * v.data[kScaleY]) * scale; |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 146 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 147 | data[kSkewY] = (v.data[kTranslateY] * v.data[kPerspective0] - |
| 148 | v.data[kSkewY] * v.data[kPerspective2]) * scale; |
| 149 | data[kScaleY] = (v.data[kScaleX] * v.data[kPerspective2] - |
| 150 | v.data[kTranslateX] * v.data[kPerspective0]) * scale; |
| 151 | data[kTranslateY] = (v.data[kTranslateX] * v.data[kSkewY] - |
| 152 | v.data[kScaleX] * v.data[kTranslateY]) * scale; |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 153 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 154 | data[kPerspective0] = (v.data[kSkewY] * v.data[kPerspective1] - |
| 155 | v.data[kScaleY] * v.data[kPerspective0]) * scale; |
| 156 | data[kPerspective1] = (v.data[kSkewX] * v.data[kPerspective0] - |
| 157 | v.data[kScaleX] * v.data[kPerspective1]) * scale; |
| 158 | data[kPerspective2] = (v.data[kScaleX] * v.data[kScaleY] - |
| 159 | v.data[kSkewX] * v.data[kSkewY]) * scale; |
| 160 | |
| 161 | mSimpleMatrix = v.mSimpleMatrix; |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 162 | mIsIdentity = v.mIsIdentity; |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 165 | void Matrix4::copyTo(float* v) const { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 166 | memcpy(v, data, sizeof(data)); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 169 | float Matrix4::getTranslateX() { |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 170 | return data[kTranslateX]; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | float Matrix4::getTranslateY() { |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 174 | return data[kTranslateY]; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 177 | void Matrix4::multiply(float v) { |
| 178 | for (int i = 0; i < 16; i++) { |
| 179 | data[i] *= v; |
| 180 | } |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 181 | mIsIdentity = false; |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 184 | void Matrix4::loadTranslate(float x, float y, float z) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 185 | loadIdentity(); |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 186 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 187 | data[kTranslateX] = x; |
| 188 | data[kTranslateY] = y; |
| 189 | data[kTranslateZ] = z; |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 190 | |
| 191 | mIsIdentity = false; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void Matrix4::loadScale(float sx, float sy, float sz) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 195 | loadIdentity(); |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 196 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 197 | data[kScaleX] = sx; |
| 198 | data[kScaleY] = sy; |
| 199 | data[kScaleZ] = sz; |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 200 | |
| 201 | mIsIdentity = false; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Romain Guy | 807daf7 | 2011-01-18 11:19:19 -0800 | [diff] [blame] | 204 | void Matrix4::loadSkew(float sx, float sy) { |
| 205 | loadIdentity(); |
| 206 | |
| 207 | data[kScaleX] = 1.0f; |
| 208 | data[kSkewX] = sx; |
| 209 | data[kTranslateX] = 0.0f; |
| 210 | |
| 211 | data[kSkewY] = sy; |
| 212 | data[kScaleY] = 1.0f; |
| 213 | data[kTranslateY] = 0.0f; |
| 214 | |
| 215 | data[kPerspective0] = 0.0f; |
| 216 | data[kPerspective1] = 0.0f; |
| 217 | data[kPerspective2] = 1.0f; |
| 218 | |
| 219 | mSimpleMatrix = false; |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 220 | mIsIdentity = false; |
Romain Guy | 807daf7 | 2011-01-18 11:19:19 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 223 | void Matrix4::loadRotate(float angle, float x, float y, float z) { |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 224 | data[kPerspective0] = 0.0f; |
| 225 | data[kPerspective1] = 0.0f; |
| 226 | data[11] = 0.0f; |
| 227 | data[kTranslateX] = 0.0f; |
| 228 | data[kTranslateY] = 0.0f; |
| 229 | data[kTranslateZ] = 0.0f; |
| 230 | data[kPerspective2] = 1.0f; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 231 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 232 | angle *= float(M_PI / 180.0f); |
| 233 | float c = cosf(angle); |
| 234 | float s = sinf(angle); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 235 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 236 | const float length = sqrtf(x * x + y * y + z * z); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 237 | float recipLen = 1.0f / length; |
| 238 | x *= recipLen; |
| 239 | y *= recipLen; |
| 240 | z *= recipLen; |
| 241 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 242 | const float nc = 1.0f - c; |
| 243 | const float xy = x * y; |
| 244 | const float yz = y * z; |
| 245 | const float zx = z * x; |
| 246 | const float xs = x * s; |
| 247 | const float ys = y * s; |
| 248 | const float zs = z * s; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 249 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 250 | data[kScaleX] = x * x * nc + c; |
| 251 | data[kSkewX] = xy * nc - zs; |
| 252 | data[8] = zx * nc + ys; |
| 253 | data[kSkewY] = xy * nc + zs; |
| 254 | data[kScaleY] = y * y * nc + c; |
| 255 | data[9] = yz * nc - xs; |
| 256 | data[2] = zx * nc - ys; |
| 257 | data[6] = yz * nc + xs; |
| 258 | data[kScaleZ] = z * z * nc + c; |
| 259 | |
| 260 | mSimpleMatrix = false; |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 261 | mIsIdentity = false; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | void Matrix4::loadMultiply(const Matrix4& u, const Matrix4& v) { |
| 265 | for (int i = 0 ; i < 4 ; i++) { |
| 266 | float x = 0; |
| 267 | float y = 0; |
| 268 | float z = 0; |
| 269 | float w = 0; |
| 270 | |
| 271 | for (int j = 0 ; j < 4 ; j++) { |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 272 | const float e = v.get(i, j); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 273 | x += u.get(j, 0) * e; |
| 274 | y += u.get(j, 1) * e; |
| 275 | z += u.get(j, 2) * e; |
| 276 | w += u.get(j, 3) * e; |
| 277 | } |
| 278 | |
| 279 | set(i, 0, x); |
| 280 | set(i, 1, y); |
| 281 | set(i, 2, z); |
| 282 | set(i, 3, w); |
| 283 | } |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 284 | |
| 285 | mSimpleMatrix = u.mSimpleMatrix && v.mSimpleMatrix; |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 286 | mIsIdentity = false; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | void Matrix4::loadOrtho(float left, float right, float bottom, float top, float near, float far) { |
| 290 | loadIdentity(); |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 291 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 292 | data[kScaleX] = 2.0f / (right - left); |
| 293 | data[kScaleY] = 2.0f / (top - bottom); |
| 294 | data[kScaleZ] = -2.0f / (far - near); |
| 295 | data[kTranslateX] = -(right + left) / (right - left); |
| 296 | data[kTranslateY] = -(top + bottom) / (top - bottom); |
| 297 | data[kTranslateZ] = -(far + near) / (far - near); |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 298 | |
| 299 | mIsIdentity = false; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Romain Guy | 0ba681b | 2010-08-12 15:37:00 -0700 | [diff] [blame] | 302 | #define MUL_ADD_STORE(a, b, c) a = (a) * (b) + (c) |
| 303 | |
| 304 | void Matrix4::mapPoint(float& x, float& y) const { |
| 305 | if (mSimpleMatrix) { |
| 306 | MUL_ADD_STORE(x, data[kScaleX], data[kTranslateX]); |
| 307 | MUL_ADD_STORE(y, data[kScaleY], data[kTranslateY]); |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | float dx = x * data[kScaleX] + y * data[kSkewX] + data[kTranslateX]; |
| 312 | float dy = x * data[kSkewY] + y * data[kScaleY] + data[kTranslateY]; |
| 313 | float dz = x * data[kPerspective0] + y * data[kPerspective1] + data[kPerspective2]; |
| 314 | if (dz) dz = 1.0f / dz; |
| 315 | |
| 316 | x = dx * dz; |
| 317 | y = dy * dz; |
| 318 | } |
| 319 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 320 | void Matrix4::mapRect(Rect& r) const { |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 321 | if (mSimpleMatrix) { |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 322 | MUL_ADD_STORE(r.left, data[kScaleX], data[kTranslateX]); |
| 323 | MUL_ADD_STORE(r.right, data[kScaleX], data[kTranslateX]); |
| 324 | MUL_ADD_STORE(r.top, data[kScaleY], data[kTranslateY]); |
| 325 | MUL_ADD_STORE(r.bottom, data[kScaleY], data[kTranslateY]); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 326 | |
| 327 | if (r.left > r.right) { |
| 328 | float x = r.left; |
| 329 | r.left = r.right; |
| 330 | r.right = x; |
| 331 | } |
| 332 | |
| 333 | if (r.top > r.bottom) { |
| 334 | float y = r.top; |
| 335 | r.top = r.bottom; |
| 336 | r.bottom = y; |
| 337 | } |
| 338 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 339 | return; |
| 340 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 341 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 342 | float vertices[] = { |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 343 | r.left, r.top, |
| 344 | r.right, r.top, |
| 345 | r.right, r.bottom, |
| 346 | r.left, r.bottom |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 347 | }; |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 348 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 349 | float x, y, z; |
| 350 | |
| 351 | for (int i = 0; i < 8; i+= 2) { |
| 352 | float px = vertices[i]; |
| 353 | float py = vertices[i + 1]; |
| 354 | |
| 355 | x = px * data[kScaleX] + py * data[kSkewX] + data[kTranslateX]; |
| 356 | y = px * data[kSkewY] + py * data[kScaleY] + data[kTranslateY]; |
| 357 | z = px * data[kPerspective0] + py * data[kPerspective1] + data[kPerspective2]; |
| 358 | if (z) z = 1.0f / z; |
| 359 | |
| 360 | vertices[i] = x * z; |
| 361 | vertices[i + 1] = y * z; |
| 362 | } |
| 363 | |
| 364 | r.left = r.right = vertices[0]; |
| 365 | r.top = r.bottom = vertices[1]; |
| 366 | |
| 367 | for (int i = 2; i < 8; i += 2) { |
| 368 | x = vertices[i]; |
| 369 | y = vertices[i + 1]; |
| 370 | |
| 371 | if (x < r.left) r.left = x; |
| 372 | else if (x > r.right) r.right = x; |
| 373 | if (y < r.top) r.top = y; |
| 374 | else if (y > r.bottom) r.bottom = y; |
| 375 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 378 | void Matrix4::dump() const { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 379 | ALOGD("Matrix4[simple=%d", mSimpleMatrix); |
| 380 | ALOGD(" %f %f %f %f", data[kScaleX], data[kSkewX], data[8], data[kTranslateX]); |
| 381 | ALOGD(" %f %f %f %f", data[kSkewY], data[kScaleY], data[9], data[kTranslateY]); |
| 382 | ALOGD(" %f %f %f %f", data[2], data[6], data[kScaleZ], data[kTranslateZ]); |
| 383 | ALOGD(" %f %f %f %f", data[kPerspective0], data[kPerspective1], data[11], data[kPerspective2]); |
| 384 | ALOGD("]"); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 387 | }; // namespace uirenderer |
| 388 | }; // namespace android |