blob: a924362bf312fbf662cdbf10ed5bdf4b8948408e [file] [log] [blame]
Romain Guy08ae3172010-06-21 19:35:50 -07001/*
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 Guy5cbbce52010-06-27 22:59:20 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guy08ae3172010-06-21 19:35:50 -070018
19#include <math.h>
20#include <stdlib.h>
Romain Guy3e168332010-06-22 13:29:14 -070021#include <string.h>
Romain Guy08ae3172010-06-21 19:35:50 -070022
23#include <utils/Log.h>
24
Romain Guyf6a11b82010-06-23 17:47:49 -070025#include <SkMatrix.h>
26
Romain Guy4bb94202010-10-12 15:59:26 -070027#include "utils/Compare.h"
Romain Guy85bf02f2010-06-22 13:11:24 -070028#include "Matrix.h"
Romain Guy08ae3172010-06-21 19:35:50 -070029
30namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070031namespace uirenderer {
Romain Guy08ae3172010-06-21 19:35:50 -070032
33void Matrix4::loadIdentity() {
Romain Guyaf28b512010-08-12 14:34:44 -070034 data[kScaleX] = 1.0f;
35 data[kSkewY] = 0.0f;
36 data[2] = 0.0f;
37 data[kPerspective0] = 0.0f;
Romain Guy08ae3172010-06-21 19:35:50 -070038
Romain Guyaf28b512010-08-12 14:34:44 -070039 data[kSkewX] = 0.0f;
40 data[kScaleY] = 1.0f;
41 data[6] = 0.0f;
42 data[kPerspective1] = 0.0f;
Romain Guy08ae3172010-06-21 19:35:50 -070043
Romain Guyaf28b512010-08-12 14:34:44 -070044 data[8] = 0.0f;
45 data[9] = 0.0f;
46 data[kScaleZ] = 1.0f;
47 data[11] = 0.0f;
Romain Guy08ae3172010-06-21 19:35:50 -070048
Romain Guyaf28b512010-08-12 14:34:44 -070049 data[kTranslateX] = 0.0f;
50 data[kTranslateY] = 0.0f;
51 data[kTranslateZ] = 0.0f;
52 data[kPerspective2] = 1.0f;
53
Romain Guy302a9df2011-08-16 13:55:02 -070054 mIsIdentity = true;
Romain Guyaf28b512010-08-12 14:34:44 -070055 mSimpleMatrix = true;
Romain Guy08ae3172010-06-21 19:35:50 -070056}
57
Chris Craik710f46d2012-09-17 17:25:49 -070058bool Matrix4::changesBounds() const {
Chet Haased3efd692012-05-07 12:18:19 -070059 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 Guye8cb9c142010-10-04 14:14:11 -070062}
63
Chris Craik710f46d2012-09-17 17:25:49 -070064bool Matrix4::isPureTranslate() const {
Chet Haased3efd692012-05-07 12:18:19 -070065 return mSimpleMatrix && data[kScaleX] == 1.0f && data[kScaleY] == 1.0f;
Romain Guy6620c6d2010-12-06 18:07:02 -080066}
67
Chris Craik710f46d2012-09-17 17:25:49 -070068bool Matrix4::isSimple() const {
Romain Guy181d0a62011-06-09 18:52:38 -070069 return mSimpleMatrix;
70}
71
Chris Craik710f46d2012-09-17 17:25:49 -070072bool Matrix4::isIdentity() const {
Romain Guy302a9df2011-08-16 13:55:02 -070073 return mIsIdentity;
74}
75
Romain Guya3dc55f2012-09-28 13:55:44 -070076bool Matrix4::isPerspective() const {
77 return data[kPerspective0] != 0.0f || data[kPerspective1] != 0.0f ||
78 data[kPerspective2] != 1.0f;
79}
80
Romain Guy08ae3172010-06-21 19:35:50 -070081void Matrix4::load(const float* v) {
Romain Guy7ae7ac42010-06-25 13:46:18 -070082 memcpy(data, v, sizeof(data));
Romain Guy302a9df2011-08-16 13:55:02 -070083 // TODO: Do something smarter here
Romain Guyaf28b512010-08-12 14:34:44 -070084 mSimpleMatrix = false;
Romain Guy302a9df2011-08-16 13:55:02 -070085 mIsIdentity = false;
Romain Guy08ae3172010-06-21 19:35:50 -070086}
87
88void Matrix4::load(const Matrix4& v) {
Romain Guy7ae7ac42010-06-25 13:46:18 -070089 memcpy(data, v.data, sizeof(data));
Romain Guyaf28b512010-08-12 14:34:44 -070090 mSimpleMatrix = v.mSimpleMatrix;
Romain Guy302a9df2011-08-16 13:55:02 -070091 mIsIdentity = v.mIsIdentity;
Romain Guy08ae3172010-06-21 19:35:50 -070092}
93
Romain Guyf6a11b82010-06-23 17:47:49 -070094void Matrix4::load(const SkMatrix& v) {
Romain Guy7ae7ac42010-06-25 13:46:18 -070095 memset(data, 0, sizeof(data));
Romain Guyf6a11b82010-06-23 17:47:49 -070096
Romain Guyaf28b512010-08-12 14:34:44 -070097 data[kScaleX] = v[SkMatrix::kMScaleX];
98 data[kSkewX] = v[SkMatrix::kMSkewX];
99 data[kTranslateX] = v[SkMatrix::kMTransX];
Romain Guyf6a11b82010-06-23 17:47:49 -0700100
Romain Guyaf28b512010-08-12 14:34:44 -0700101 data[kSkewY] = v[SkMatrix::kMSkewY];
102 data[kScaleY] = v[SkMatrix::kMScaleY];
103 data[kTranslateY] = v[SkMatrix::kMTransY];
Romain Guyf6a11b82010-06-23 17:47:49 -0700104
Romain Guyaf28b512010-08-12 14:34:44 -0700105 data[kPerspective0] = v[SkMatrix::kMPersp0];
106 data[kPerspective1] = v[SkMatrix::kMPersp1];
107 data[kPerspective2] = v[SkMatrix::kMPersp2];
Romain Guyf6a11b82010-06-23 17:47:49 -0700108
Romain Guyaf28b512010-08-12 14:34:44 -0700109 data[kScaleZ] = 1.0f;
110
Romain Guye5ebcb02010-10-15 13:57:28 -0700111 mSimpleMatrix = (v.getType() <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask));
Romain Guy302a9df2011-08-16 13:55:02 -0700112 mIsIdentity = v.isIdentity();
Romain Guyf6a11b82010-06-23 17:47:49 -0700113}
114
115void Matrix4::copyTo(SkMatrix& v) const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700116 v.reset();
Romain Guyf6a11b82010-06-23 17:47:49 -0700117
Romain Guyaf28b512010-08-12 14:34:44 -0700118 v.set(SkMatrix::kMScaleX, data[kScaleX]);
119 v.set(SkMatrix::kMSkewX, data[kSkewX]);
120 v.set(SkMatrix::kMTransX, data[kTranslateX]);
Romain Guyf6a11b82010-06-23 17:47:49 -0700121
Romain Guyaf28b512010-08-12 14:34:44 -0700122 v.set(SkMatrix::kMSkewY, data[kSkewY]);
123 v.set(SkMatrix::kMScaleY, data[kScaleY]);
124 v.set(SkMatrix::kMTransY, data[kTranslateY]);
Romain Guyf6a11b82010-06-23 17:47:49 -0700125
Romain Guyaf28b512010-08-12 14:34:44 -0700126 v.set(SkMatrix::kMPersp0, data[kPerspective0]);
127 v.set(SkMatrix::kMPersp1, data[kPerspective1]);
128 v.set(SkMatrix::kMPersp2, data[kPerspective2]);
Romain Guyf6a11b82010-06-23 17:47:49 -0700129}
130
Romain Guy079ba2c2010-07-16 14:12:24 -0700131void Matrix4::loadInverse(const Matrix4& v) {
132 double scale = 1.0 /
Romain Guyaf28b512010-08-12 14:34:44 -0700133 (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 Guy079ba2c2010-07-16 14:12:24 -0700139
Romain Guyaf28b512010-08-12 14:34:44 -0700140 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 Guy079ba2c2010-07-16 14:12:24 -0700146
Romain Guyaf28b512010-08-12 14:34:44 -0700147 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 Guy079ba2c2010-07-16 14:12:24 -0700153
Romain Guyaf28b512010-08-12 14:34:44 -0700154 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 Guy302a9df2011-08-16 13:55:02 -0700162 mIsIdentity = v.mIsIdentity;
Romain Guy079ba2c2010-07-16 14:12:24 -0700163}
164
Romain Guy08ae3172010-06-21 19:35:50 -0700165void Matrix4::copyTo(float* v) const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700166 memcpy(v, data, sizeof(data));
Romain Guy08ae3172010-06-21 19:35:50 -0700167}
168
Romain Guybd6b79b2010-06-26 00:13:53 -0700169float Matrix4::getTranslateX() {
Romain Guyaf28b512010-08-12 14:34:44 -0700170 return data[kTranslateX];
Romain Guybd6b79b2010-06-26 00:13:53 -0700171}
172
173float Matrix4::getTranslateY() {
Romain Guyaf28b512010-08-12 14:34:44 -0700174 return data[kTranslateY];
Romain Guybd6b79b2010-06-26 00:13:53 -0700175}
176
Romain Guyddb80be2010-09-20 19:04:33 -0700177void Matrix4::multiply(float v) {
178 for (int i = 0; i < 16; i++) {
179 data[i] *= v;
180 }
Romain Guy302a9df2011-08-16 13:55:02 -0700181 mIsIdentity = false;
Romain Guyddb80be2010-09-20 19:04:33 -0700182}
183
Romain Guy08ae3172010-06-21 19:35:50 -0700184void Matrix4::loadTranslate(float x, float y, float z) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700185 loadIdentity();
Romain Guy302a9df2011-08-16 13:55:02 -0700186
Romain Guyaf28b512010-08-12 14:34:44 -0700187 data[kTranslateX] = x;
188 data[kTranslateY] = y;
189 data[kTranslateZ] = z;
Romain Guy302a9df2011-08-16 13:55:02 -0700190
191 mIsIdentity = false;
Romain Guy08ae3172010-06-21 19:35:50 -0700192}
193
194void Matrix4::loadScale(float sx, float sy, float sz) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700195 loadIdentity();
Romain Guy302a9df2011-08-16 13:55:02 -0700196
Romain Guyaf28b512010-08-12 14:34:44 -0700197 data[kScaleX] = sx;
198 data[kScaleY] = sy;
199 data[kScaleZ] = sz;
Romain Guy302a9df2011-08-16 13:55:02 -0700200
201 mIsIdentity = false;
Romain Guy08ae3172010-06-21 19:35:50 -0700202}
203
Romain Guy807daf72011-01-18 11:19:19 -0800204void 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 Guy302a9df2011-08-16 13:55:02 -0700220 mIsIdentity = false;
Romain Guy807daf72011-01-18 11:19:19 -0800221}
222
Romain Guy08ae3172010-06-21 19:35:50 -0700223void Matrix4::loadRotate(float angle, float x, float y, float z) {
Romain Guyaf28b512010-08-12 14:34:44 -0700224 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 Guy08ae3172010-06-21 19:35:50 -0700231
Romain Guy7ae7ac42010-06-25 13:46:18 -0700232 angle *= float(M_PI / 180.0f);
233 float c = cosf(angle);
234 float s = sinf(angle);
Romain Guy08ae3172010-06-21 19:35:50 -0700235
Romain Guy7ae7ac42010-06-25 13:46:18 -0700236 const float length = sqrtf(x * x + y * y + z * z);
Romain Guy889f8d12010-07-29 14:37:42 -0700237 float recipLen = 1.0f / length;
238 x *= recipLen;
239 y *= recipLen;
240 z *= recipLen;
241
Romain Guy7ae7ac42010-06-25 13:46:18 -0700242 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 Guy08ae3172010-06-21 19:35:50 -0700249
Romain Guyaf28b512010-08-12 14:34:44 -0700250 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 Guy302a9df2011-08-16 13:55:02 -0700261 mIsIdentity = false;
Romain Guy08ae3172010-06-21 19:35:50 -0700262}
263
264void 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 Guyc7d53492010-06-25 13:41:57 -0700272 const float e = v.get(i, j);
Romain Guy08ae3172010-06-21 19:35:50 -0700273 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 Guyaf28b512010-08-12 14:34:44 -0700284
285 mSimpleMatrix = u.mSimpleMatrix && v.mSimpleMatrix;
Romain Guy302a9df2011-08-16 13:55:02 -0700286 mIsIdentity = false;
Romain Guy08ae3172010-06-21 19:35:50 -0700287}
288
289void Matrix4::loadOrtho(float left, float right, float bottom, float top, float near, float far) {
290 loadIdentity();
Romain Guy302a9df2011-08-16 13:55:02 -0700291
Romain Guyaf28b512010-08-12 14:34:44 -0700292 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 Guy302a9df2011-08-16 13:55:02 -0700298
299 mIsIdentity = false;
Romain Guy08ae3172010-06-21 19:35:50 -0700300}
301
Romain Guy0ba681b2010-08-12 15:37:00 -0700302#define MUL_ADD_STORE(a, b, c) a = (a) * (b) + (c)
303
304void 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 Guy9d5316e2010-06-24 19:30:36 -0700320void Matrix4::mapRect(Rect& r) const {
Romain Guyaf28b512010-08-12 14:34:44 -0700321 if (mSimpleMatrix) {
Romain Guyaf28b512010-08-12 14:34:44 -0700322 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 Guy5b3b3522010-10-27 18:57:51 -0700326
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 Guyaf28b512010-08-12 14:34:44 -0700339 return;
340 }
Romain Guy9d5316e2010-06-24 19:30:36 -0700341
Romain Guyaf28b512010-08-12 14:34:44 -0700342 float vertices[] = {
Romain Guy1d83e192010-08-17 11:37:00 -0700343 r.left, r.top,
344 r.right, r.top,
345 r.right, r.bottom,
346 r.left, r.bottom
Romain Guyaf28b512010-08-12 14:34:44 -0700347 };
Romain Guy9d5316e2010-06-24 19:30:36 -0700348
Romain Guyaf28b512010-08-12 14:34:44 -0700349 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 Guy9d5316e2010-06-24 19:30:36 -0700376}
377
Romain Guy08ae3172010-06-21 19:35:50 -0700378void Matrix4::dump() const {
Steve Block5baa3a62011-12-20 16:23:08 +0000379 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 Guy08ae3172010-06-21 19:35:50 -0700385}
386
Romain Guy9d5316e2010-06-24 19:30:36 -0700387}; // namespace uirenderer
388}; // namespace android