blob: c772f003f014ea579f7392774fd796dbf0df8a3b [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 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
100void Matrix4::loadTranslate(float x, float y, float z) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700101 loadIdentity();
102 data[12] = x;
103 data[13] = y;
104 data[14] = z;
Romain Guy08ae3172010-06-21 19:35:50 -0700105}
106
107void Matrix4::loadScale(float sx, float sy, float sz) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700108 loadIdentity();
109 data[0] = sx;
110 data[5] = sy;
111 data[10] = sz;
Romain Guy08ae3172010-06-21 19:35:50 -0700112}
113
114void Matrix4::loadRotate(float angle, float x, float y, float z) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700115 data[3] = 0.0f;
116 data[7] = 0.0f;
117 data[11] = 0.0f;
118 data[12] = 0.0f;
119 data[13] = 0.0f;
120 data[14] = 0.0f;
121 data[15] = 1.0f;
Romain Guy08ae3172010-06-21 19:35:50 -0700122
Romain Guy7ae7ac42010-06-25 13:46:18 -0700123 angle *= float(M_PI / 180.0f);
124 float c = cosf(angle);
125 float s = sinf(angle);
Romain Guy08ae3172010-06-21 19:35:50 -0700126
Romain Guy7ae7ac42010-06-25 13:46:18 -0700127 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;
Romain Guy08ae3172010-06-21 19:35:50 -0700135
Romain Guy7ae7ac42010-06-25 13:46:18 -0700136 data[0] = x * x * nc + c;
137 data[4] = xy * nc - zs;
138 data[8] = zx * nc + ys;
139 data[1] = xy * nc + zs;
140 data[5] = y * y * nc + c;
141 data[9] = yz * nc - xs;
142 data[2] = zx * nc - ys;
143 data[6] = yz * nc + xs;
144 data[10] = z * z * nc + c;
Romain Guy08ae3172010-06-21 19:35:50 -0700145}
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++) {
Romain Guyc7d53492010-06-25 13:41:57 -0700155 const float e = v.get(i, j);
Romain Guy08ae3172010-06-21 19:35:50 -0700156 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 Guyc7d53492010-06-25 13:41:57 -0700171 data[0] = 2.0f / (right - left);
172 data[5] = 2.0f / (top - bottom);
173 data[10] = -2.0f / (far - near);
174 data[12] = -(right + left) / (right - left);
175 data[13] = -(top + bottom) / (top - bottom);
176 data[14] = -(far + near) / (far - near);
Romain Guy08ae3172010-06-21 19:35:50 -0700177}
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 {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700182 const float sx = data[0];
183 const float sy = data[5];
Romain Guy9d5316e2010-06-24 19:30:36 -0700184
Romain Guy7ae7ac42010-06-25 13:46:18 -0700185 const float tx = data[12];
186 const float ty = data[13];
Romain Guy9d5316e2010-06-24 19:30:36 -0700187
Romain Guy7ae7ac42010-06-25 13:46:18 -0700188 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);
Romain Guy9d5316e2010-06-24 19:30:36 -0700192}
193
Romain Guy08ae3172010-06-21 19:35:50 -0700194void Matrix4::dump() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700195 LOGD("Matrix4[");
196 LOGD(" %f %f %f %f", data[0], data[4], data[ 8], data[12]);
197 LOGD(" %f %f %f %f", data[1], data[5], data[ 9], data[13]);
198 LOGD(" %f %f %f %f", data[2], data[6], data[10], data[14]);
199 LOGD(" %f %f %f %f", data[3], data[7], data[11], data[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