blob: 877d3bbfb2bd7c41c17118b6ed819aa243345128 [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 Guy85bf02f2010-06-22 13:11:24 -070027#include "Matrix.h"
Romain Guy08ae3172010-06-21 19:35:50 -070028
29namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070030namespace uirenderer {
Romain Guy08ae3172010-06-21 19:35:50 -070031
32void Matrix4::loadIdentity() {
Romain Guy7ae7ac42010-06-25 13:46:18 -070033 data[0] = 1.0f;
34 data[1] = 0.0f;
35 data[2] = 0.0f;
36 data[3] = 0.0f;
Romain Guy08ae3172010-06-21 19:35:50 -070037
Romain Guy7ae7ac42010-06-25 13:46:18 -070038 data[4] = 0.0f;
39 data[5] = 1.0f;
40 data[6] = 0.0f;
41 data[7] = 0.0f;
Romain Guy08ae3172010-06-21 19:35:50 -070042
Romain Guy7ae7ac42010-06-25 13:46:18 -070043 data[8] = 0.0f;
44 data[9] = 0.0f;
45 data[10] = 1.0f;
46 data[11] = 0.0f;
Romain Guy08ae3172010-06-21 19:35:50 -070047
Romain Guy7ae7ac42010-06-25 13:46:18 -070048 data[12] = 0.0f;
49 data[13] = 0.0f;
50 data[14] = 0.0f;
51 data[15] = 1.0f;
Romain Guy08ae3172010-06-21 19:35:50 -070052}
53
54void Matrix4::load(const float* v) {
Romain Guy7ae7ac42010-06-25 13:46:18 -070055 memcpy(data, v, sizeof(data));
Romain Guy08ae3172010-06-21 19:35:50 -070056}
57
58void Matrix4::load(const Matrix4& v) {
Romain Guy7ae7ac42010-06-25 13:46:18 -070059 memcpy(data, v.data, sizeof(data));
Romain Guy08ae3172010-06-21 19:35:50 -070060}
61
Romain Guyf6a11b82010-06-23 17:47:49 -070062void Matrix4::load(const SkMatrix& v) {
Romain Guy7ae7ac42010-06-25 13:46:18 -070063 memset(data, 0, sizeof(data));
Romain Guyf6a11b82010-06-23 17:47:49 -070064
Romain Guy7ae7ac42010-06-25 13:46:18 -070065 data[0] = v[SkMatrix::kMScaleX];
66 data[4] = v[SkMatrix::kMSkewX];
67 data[12] = v[SkMatrix::kMTransX];
Romain Guyf6a11b82010-06-23 17:47:49 -070068
Romain Guy7ae7ac42010-06-25 13:46:18 -070069 data[1] = v[SkMatrix::kMSkewY];
70 data[5] = v[SkMatrix::kMScaleY];
71 data[13] = v[SkMatrix::kMTransY];
Romain Guyf6a11b82010-06-23 17:47:49 -070072
Romain Guy7ae7ac42010-06-25 13:46:18 -070073 data[3] = v[SkMatrix::kMPersp0];
74 data[7] = v[SkMatrix::kMPersp1];
75 data[15] = v[SkMatrix::kMPersp2];
Romain Guyf6a11b82010-06-23 17:47:49 -070076
Romain Guy7ae7ac42010-06-25 13:46:18 -070077 data[10] = 1.0f;
Romain Guyf6a11b82010-06-23 17:47:49 -070078}
79
80void Matrix4::copyTo(SkMatrix& v) const {
Romain Guy7ae7ac42010-06-25 13:46:18 -070081 v.reset();
Romain Guyf6a11b82010-06-23 17:47:49 -070082
Romain Guy7ae7ac42010-06-25 13:46:18 -070083 v.set(SkMatrix::kMScaleX, data[0]);
84 v.set(SkMatrix::kMSkewX, data[4]);
85 v.set(SkMatrix::kMTransX, data[12]);
Romain Guyf6a11b82010-06-23 17:47:49 -070086
Romain Guy7ae7ac42010-06-25 13:46:18 -070087 v.set(SkMatrix::kMSkewY, data[1]);
88 v.set(SkMatrix::kMScaleY, data[5]);
89 v.set(SkMatrix::kMTransY, data[13]);
Romain Guyf6a11b82010-06-23 17:47:49 -070090
Romain Guy7ae7ac42010-06-25 13:46:18 -070091 v.set(SkMatrix::kMPersp0, data[3]);
92 v.set(SkMatrix::kMPersp1, data[7]);
93 v.set(SkMatrix::kMPersp2, data[15]);
Romain Guyf6a11b82010-06-23 17:47:49 -070094}
95
Romain Guy08ae3172010-06-21 19:35:50 -070096void Matrix4::copyTo(float* v) const {
Romain Guy7ae7ac42010-06-25 13:46:18 -070097 memcpy(v, data, sizeof(data));
Romain Guy08ae3172010-06-21 19:35:50 -070098}
99
Romain Guybd6b79b2010-06-26 00:13:53 -0700100float Matrix4::getTranslateX() {
101 return data[12];
102}
103
104float Matrix4::getTranslateY() {
105 return data[13];
106}
107
Romain Guy08ae3172010-06-21 19:35:50 -0700108void Matrix4::loadTranslate(float x, float y, float z) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700109 loadIdentity();
110 data[12] = x;
111 data[13] = y;
112 data[14] = z;
Romain Guy08ae3172010-06-21 19:35:50 -0700113}
114
115void Matrix4::loadScale(float sx, float sy, float sz) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700116 loadIdentity();
117 data[0] = sx;
118 data[5] = sy;
119 data[10] = sz;
Romain Guy08ae3172010-06-21 19:35:50 -0700120}
121
122void Matrix4::loadRotate(float angle, float x, float y, float z) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700123 data[3] = 0.0f;
124 data[7] = 0.0f;
125 data[11] = 0.0f;
126 data[12] = 0.0f;
127 data[13] = 0.0f;
128 data[14] = 0.0f;
129 data[15] = 1.0f;
Romain Guy08ae3172010-06-21 19:35:50 -0700130
Romain Guy7ae7ac42010-06-25 13:46:18 -0700131 angle *= float(M_PI / 180.0f);
132 float c = cosf(angle);
133 float s = sinf(angle);
Romain Guy08ae3172010-06-21 19:35:50 -0700134
Romain Guy7ae7ac42010-06-25 13:46:18 -0700135 const float length = sqrtf(x * x + y * y + z * z);
136 const float nc = 1.0f - c;
137 const float xy = x * y;
138 const float yz = y * z;
139 const float zx = z * x;
140 const float xs = x * s;
141 const float ys = y * s;
142 const float zs = z * s;
Romain Guy08ae3172010-06-21 19:35:50 -0700143
Romain Guy7ae7ac42010-06-25 13:46:18 -0700144 data[0] = x * x * nc + c;
145 data[4] = xy * nc - zs;
146 data[8] = zx * nc + ys;
147 data[1] = xy * nc + zs;
148 data[5] = y * y * nc + c;
149 data[9] = yz * nc - xs;
150 data[2] = zx * nc - ys;
151 data[6] = yz * nc + xs;
152 data[10] = z * z * nc + c;
Romain Guy08ae3172010-06-21 19:35:50 -0700153}
154
155void Matrix4::loadMultiply(const Matrix4& u, const Matrix4& v) {
156 for (int i = 0 ; i < 4 ; i++) {
157 float x = 0;
158 float y = 0;
159 float z = 0;
160 float w = 0;
161
162 for (int j = 0 ; j < 4 ; j++) {
Romain Guyc7d53492010-06-25 13:41:57 -0700163 const float e = v.get(i, j);
Romain Guy08ae3172010-06-21 19:35:50 -0700164 x += u.get(j, 0) * e;
165 y += u.get(j, 1) * e;
166 z += u.get(j, 2) * e;
167 w += u.get(j, 3) * e;
168 }
169
170 set(i, 0, x);
171 set(i, 1, y);
172 set(i, 2, z);
173 set(i, 3, w);
174 }
175}
176
177void Matrix4::loadOrtho(float left, float right, float bottom, float top, float near, float far) {
178 loadIdentity();
Romain Guyc7d53492010-06-25 13:41:57 -0700179 data[0] = 2.0f / (right - left);
180 data[5] = 2.0f / (top - bottom);
181 data[10] = -2.0f / (far - near);
182 data[12] = -(right + left) / (right - left);
183 data[13] = -(top + bottom) / (top - bottom);
184 data[14] = -(far + near) / (far - near);
Romain Guy08ae3172010-06-21 19:35:50 -0700185}
186
Romain Guy9d5316e2010-06-24 19:30:36 -0700187#define MUL_ADD_STORE(a, b, c) a = (a) * (b) + (c)
188
189void Matrix4::mapRect(Rect& r) const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700190 const float sx = data[0];
191 const float sy = data[5];
Romain Guy9d5316e2010-06-24 19:30:36 -0700192
Romain Guy7ae7ac42010-06-25 13:46:18 -0700193 const float tx = data[12];
194 const float ty = data[13];
Romain Guy9d5316e2010-06-24 19:30:36 -0700195
Romain Guy7ae7ac42010-06-25 13:46:18 -0700196 MUL_ADD_STORE(r.left, sx, tx);
197 MUL_ADD_STORE(r.right, sx, tx);
198 MUL_ADD_STORE(r.top, sy, ty);
199 MUL_ADD_STORE(r.bottom, sy, ty);
Romain Guy9d5316e2010-06-24 19:30:36 -0700200}
201
Romain Guy08ae3172010-06-21 19:35:50 -0700202void Matrix4::dump() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700203 LOGD("Matrix4[");
204 LOGD(" %f %f %f %f", data[0], data[4], data[ 8], data[12]);
205 LOGD(" %f %f %f %f", data[1], data[5], data[ 9], data[13]);
206 LOGD(" %f %f %f %f", data[2], data[6], data[10], data[14]);
207 LOGD(" %f %f %f %f", data[3], data[7], data[11], data[15]);
208 LOGD("]");
Romain Guy08ae3172010-06-21 19:35:50 -0700209}
210
Romain Guy9d5316e2010-06-24 19:30:36 -0700211}; // namespace uirenderer
212}; // namespace android