blob: c097d7f9b5927dbdb476df21b706ede424cfc8ca [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 Guy9d5316e2010-06-24 19:30:36 -070017#define LOG_TAG "Matrix"
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 Guyf6a11b82010-06-23 17:47:49 -070033 mMat[0] = 1.0f;
34 mMat[1] = 0.0f;
35 mMat[2] = 0.0f;
36 mMat[3] = 0.0f;
Romain Guy08ae3172010-06-21 19:35:50 -070037
Romain Guyf6a11b82010-06-23 17:47:49 -070038 mMat[4] = 0.0f;
39 mMat[5] = 1.0f;
40 mMat[6] = 0.0f;
41 mMat[7] = 0.0f;
Romain Guy08ae3172010-06-21 19:35:50 -070042
Romain Guyf6a11b82010-06-23 17:47:49 -070043 mMat[8] = 0.0f;
44 mMat[9] = 0.0f;
45 mMat[10] = 1.0f;
46 mMat[11] = 0.0f;
Romain Guy08ae3172010-06-21 19:35:50 -070047
Romain Guyf6a11b82010-06-23 17:47:49 -070048 mMat[12] = 0.0f;
49 mMat[13] = 0.0f;
50 mMat[14] = 0.0f;
51 mMat[15] = 1.0f;
Romain Guy08ae3172010-06-21 19:35:50 -070052}
53
54void Matrix4::load(const float* v) {
55 memcpy(mMat, v, sizeof(mMat));
56}
57
58void Matrix4::load(const Matrix4& v) {
59 memcpy(mMat, v.mMat, sizeof(mMat));
60}
61
Romain Guyf6a11b82010-06-23 17:47:49 -070062void Matrix4::load(const SkMatrix& v) {
63 memset(mMat, 0, sizeof(mMat));
64
65 mMat[0] = v[SkMatrix::kMScaleX];
66 mMat[4] = v[SkMatrix::kMSkewX];
67 mMat[12] = v[SkMatrix::kMTransX];
68
69 mMat[1] = v[SkMatrix::kMSkewY];
70 mMat[5] = v[SkMatrix::kMScaleY];
71 mMat[13] = v[SkMatrix::kMTransY];
72
73 mMat[3] = v[SkMatrix::kMPersp0];
74 mMat[7] = v[SkMatrix::kMPersp1];
75 mMat[15] = v[SkMatrix::kMPersp2];
76
77 mMat[10] = 1.0f;
78}
79
80void Matrix4::copyTo(SkMatrix& v) const {
81 v.reset();
82
83 v.set(SkMatrix::kMScaleX, mMat[0]);
84 v.set(SkMatrix::kMSkewX, mMat[4]);
85 v.set(SkMatrix::kMTransX, mMat[12]);
86
87 v.set(SkMatrix::kMSkewY, mMat[1]);
88 v.set(SkMatrix::kMScaleY, mMat[5]);
89 v.set(SkMatrix::kMTransY, mMat[13]);
90
91 v.set(SkMatrix::kMPersp0, mMat[3]);
92 v.set(SkMatrix::kMPersp1, mMat[7]);
93 v.set(SkMatrix::kMPersp2, mMat[15]);
94}
95
Romain Guy08ae3172010-06-21 19:35:50 -070096void Matrix4::copyTo(float* v) const {
97 memcpy(v, mMat, sizeof(mMat));
98}
99
100void Matrix4::loadTranslate(float x, float y, float z) {
101 loadIdentity();
102 mMat[12] = x;
103 mMat[13] = y;
104 mMat[14] = z;
105}
106
107void Matrix4::loadScale(float sx, float sy, float sz) {
108 loadIdentity();
109 mMat[0] = sx;
110 mMat[5] = sy;
111 mMat[10] = sz;
112}
113
114void Matrix4::loadRotate(float angle, float x, float y, float z) {
Romain Guyf6a11b82010-06-23 17:47:49 -0700115 mMat[3] = 0.0f;
116 mMat[7] = 0.0f;
117 mMat[11] = 0.0f;
118 mMat[12] = 0.0f;
119 mMat[13] = 0.0f;
120 mMat[14] = 0.0f;
121 mMat[15] = 1.0f;
Romain Guy08ae3172010-06-21 19:35:50 -0700122
123 angle *= float(M_PI / 180.0f);
124 float c = cosf(angle);
125 float s = sinf(angle);
126
127 const float length = sqrtf(x * x + y * y + z * z);
128 const float nc = 1.0f - c;
129 const float xy = x * y;
130 const float yz = y * z;
131 const float zx = z * x;
132 const float xs = x * s;
133 const float ys = y * s;
134 const float zs = z * s;
135
136 mMat[0] = x * x * nc + c;
137 mMat[4] = xy * nc - zs;
138 mMat[8] = zx * nc + ys;
139 mMat[1] = xy * nc + zs;
140 mMat[5] = y * y * nc + c;
141 mMat[9] = yz * nc - xs;
142 mMat[2] = zx * nc - ys;
143 mMat[6] = yz * nc + xs;
144 mMat[10] = z * z * nc + c;
145}
146
147void Matrix4::loadMultiply(const Matrix4& u, const Matrix4& v) {
148 for (int i = 0 ; i < 4 ; i++) {
149 float x = 0;
150 float y = 0;
151 float z = 0;
152 float w = 0;
153
154 for (int j = 0 ; j < 4 ; j++) {
155 const float e = v.get(i,j);
156 x += u.get(j, 0) * e;
157 y += u.get(j, 1) * e;
158 z += u.get(j, 2) * e;
159 w += u.get(j, 3) * e;
160 }
161
162 set(i, 0, x);
163 set(i, 1, y);
164 set(i, 2, z);
165 set(i, 3, w);
166 }
167}
168
169void Matrix4::loadOrtho(float left, float right, float bottom, float top, float near, float far) {
170 loadIdentity();
Romain Guyf6a11b82010-06-23 17:47:49 -0700171 mMat[0] = 2.0f / (right - left);
172 mMat[5] = 2.0f / (top - bottom);
173 mMat[10] = -2.0f / (far - near);
Romain Guy08ae3172010-06-21 19:35:50 -0700174 mMat[12] = -(right + left) / (right - left);
175 mMat[13] = -(top + bottom) / (top - bottom);
176 mMat[14] = -(far + near) / (far - near);
177}
178
Romain Guy9d5316e2010-06-24 19:30:36 -0700179#define MUL_ADD_STORE(a, b, c) a = (a) * (b) + (c)
180
181void Matrix4::mapRect(Rect& r) const {
182 const float sx = mMat[0];
183 const float sy = mMat[5];
184
185 const float tx = mMat[12];
186 const float ty = mMat[13];
187
188 MUL_ADD_STORE(r.left, sx, tx);
189 MUL_ADD_STORE(r.right, sx, tx);
190 MUL_ADD_STORE(r.top, sy, ty);
191 MUL_ADD_STORE(r.bottom, sy, ty);
192}
193
Romain Guy08ae3172010-06-21 19:35:50 -0700194void Matrix4::dump() const {
Romain Guybb9524b2010-06-22 18:56:38 -0700195 LOGD("Matrix4[");
196 LOGD(" %f %f %f %f", mMat[0], mMat[4], mMat[ 8], mMat[12]);
197 LOGD(" %f %f %f %f", mMat[1], mMat[5], mMat[ 9], mMat[13]);
198 LOGD(" %f %f %f %f", mMat[2], mMat[6], mMat[10], mMat[14]);
199 LOGD(" %f %f %f %f", mMat[3], mMat[7], mMat[11], mMat[15]);
200 LOGD("]");
Romain Guy08ae3172010-06-21 19:35:50 -0700201}
202
Romain Guy9d5316e2010-06-24 19:30:36 -0700203}; // namespace uirenderer
204}; // namespace android