blob: f1284295bf0459450c5158afa95ea9b125ae01ba [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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
Mathias Agopianeda65402010-02-22 03:15:57 -080017#include <math.h>
18
19#include <cutils/compiler.h>
20#include <utils/String8.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <ui/Region.h>
22
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023#include "Transform.h"
24
25// ---------------------------------------------------------------------------
26
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027namespace android {
28
29// ---------------------------------------------------------------------------
30
Mathias Agopianeda65402010-02-22 03:15:57 -080031template <typename T> inline T min(T a, T b) {
32 return a<b ? a : b;
33}
34template <typename T> inline T min(T a, T b, T c) {
35 return min(a, min(b, c));
36}
37template <typename T> inline T min(T a, T b, T c, T d) {
38 return min(a, b, min(c, d));
39}
40
41template <typename T> inline T max(T a, T b) {
42 return a>b ? a : b;
43}
44template <typename T> inline T max(T a, T b, T c) {
45 return max(a, max(b, c));
46}
47template <typename T> inline T max(T a, T b, T c, T d) {
48 return max(a, b, max(c, d));
49}
50
51// ---------------------------------------------------------------------------
52
53Transform::Transform() {
54 reset();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055}
56
57Transform::Transform(const Transform& other)
Mathias Agopianeda65402010-02-22 03:15:57 -080058 : mMatrix(other.mMatrix), mType(other.mType) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059}
60
Mathias Agopianeda65402010-02-22 03:15:57 -080061Transform::Transform(uint32_t orientation) {
62 set(orientation, 0, 0);
Chih-Chung Chang52e72002010-01-21 17:31:06 -080063}
64
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065Transform::~Transform() {
66}
67
Mathias Agopiand1296592010-03-09 19:17:47 -080068static const float EPSILON = 0.0f;
Mathias Agopianeda65402010-02-22 03:15:57 -080069
70bool Transform::isZero(float f) {
Mathias Agopiand1296592010-03-09 19:17:47 -080071 return fabs(f) <= EPSILON;
Mathias Agopianeda65402010-02-22 03:15:57 -080072}
73
Mathias Agopiand1296592010-03-09 19:17:47 -080074bool Transform::absIsOne(float f) {
75 return isZero(fabs(f) - 1.0f);
Mathias Agopianeda65402010-02-22 03:15:57 -080076}
77
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078Transform Transform::operator * (const Transform& rhs) const
79{
Mathias Agopianeda65402010-02-22 03:15:57 -080080 if (CC_LIKELY(mType == IDENTITY))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081 return rhs;
82
83 Transform r(*this);
Mathias Agopianeda65402010-02-22 03:15:57 -080084 if (rhs.mType == IDENTITY)
85 return r;
86
87 // TODO: we could use mType to optimize the matrix multiply
88 const mat33& A(mMatrix);
89 const mat33& B(rhs.mMatrix);
90 mat33& D(r.mMatrix);
91 for (int i=0 ; i<3 ; i++) {
92 const float v0 = A[0][i];
93 const float v1 = A[1][i];
94 const float v2 = A[2][i];
95 D[0][i] = v0*B[0][0] + v1*B[0][1] + v2*B[0][2];
96 D[1][i] = v0*B[1][0] + v1*B[1][1] + v2*B[1][2];
97 D[2][i] = v0*B[2][0] + v1*B[2][1] + v2*B[2][2];
98 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 r.mType |= rhs.mType;
Mathias Agopianeda65402010-02-22 03:15:57 -0800100
101 // TODO: we could recompute this value from r and rhs
102 r.mType &= 0xFF;
103 r.mType |= UNKNOWN_TYPE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 return r;
105}
106
Mathias Agopianeda65402010-02-22 03:15:57 -0800107float const* Transform::operator [] (int i) const {
108 return mMatrix[i].v;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109}
110
111bool Transform::transformed() const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800112 return type() > TRANSLATE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113}
114
115int Transform::tx() const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800116 return floorf(mMatrix[2][0] + 0.5f);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117}
118
119int Transform::ty() const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800120 return floorf(mMatrix[2][1] + 0.5f);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121}
122
123void Transform::reset() {
Mathias Agopianeda65402010-02-22 03:15:57 -0800124 mType = IDENTITY;
125 for(int i=0 ; i<3 ; i++) {
126 vec3& v(mMatrix[i]);
127 for (int j=0 ; j<3 ; j++)
128 v[j] = ((i==j) ? 1.0f : 0.0f);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129 }
130}
131
Mathias Agopianeda65402010-02-22 03:15:57 -0800132void Transform::set(float tx, float ty)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133{
Mathias Agopianeda65402010-02-22 03:15:57 -0800134 mMatrix[2][0] = tx;
135 mMatrix[2][1] = ty;
136 mMatrix[2][2] = 1.0f;
137
138 if (isZero(tx) && isZero(ty)) {
139 mType &= ~TRANSLATE;
140 } else {
141 mType |= TRANSLATE;
142 }
143}
144
145void Transform::set(float a, float b, float c, float d)
146{
147 mat33& M(mMatrix);
148 M[0][0] = a; M[1][0] = b;
149 M[0][1] = c; M[1][1] = d;
150 M[0][2] = 0; M[1][2] = 0;
151 mType = UNKNOWN_TYPE;
152}
153
Mathias Agopiand1296592010-03-09 19:17:47 -0800154status_t Transform::set(uint32_t flags, float w, float h)
Mathias Agopianeda65402010-02-22 03:15:57 -0800155{
Mathias Agopiand1296592010-03-09 19:17:47 -0800156 if (flags & ROT_INVALID) {
157 // that's not allowed!
158 reset();
159 return BAD_VALUE;
160 }
161
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700162 Transform H, V, R;
Mathias Agopianeda65402010-02-22 03:15:57 -0800163 if (flags & FLIP_H) {
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700164 H.mType = (FLIP_H << 8) | SCALE;
165 H.mType |= isZero(w) ? IDENTITY : TRANSLATE;
166 mat33& M(H.mMatrix);
167 M[0][0] = -1;
168 M[2][0] = w;
Mathias Agopianeda65402010-02-22 03:15:57 -0800169 }
170
171 if (flags & FLIP_V) {
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700172 V.mType = (FLIP_V << 8) | SCALE;
173 V.mType |= isZero(h) ? IDENTITY : TRANSLATE;
174 mat33& M(V.mMatrix);
175 M[1][1] = -1;
176 M[2][1] = h;
Mathias Agopianeda65402010-02-22 03:15:57 -0800177 }
178
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700179 if (flags & ROT_90) {
180 R.mType = (ROT_90 << 8) | ROTATE;
181 R.mType |= isZero(w) ? IDENTITY : TRANSLATE;
182 mat33& M(R.mMatrix);
183 M[0][0] = 0; M[1][0] =-1; M[2][0] = w;
184 M[0][1] = 1; M[1][1] = 0;
Mathias Agopianeda65402010-02-22 03:15:57 -0800185 }
186
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700187 *this = ((H*V)*R);
Mathias Agopiand1296592010-03-09 19:17:47 -0800188 return NO_ERROR;
Mathias Agopianeda65402010-02-22 03:15:57 -0800189}
190
191Transform::vec2 Transform::transform(const vec2& v) const {
192 vec2 r;
193 const mat33& M(mMatrix);
194 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0];
195 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1];
196 return r;
197}
198
199Transform::vec3 Transform::transform(const vec3& v) const {
200 vec3 r;
201 const mat33& M(mMatrix);
202 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0]*v[2];
203 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1]*v[2];
204 r[2] = M[0][2]*v[0] + M[1][2]*v[1] + M[2][2]*v[2];
205 return r;
206}
207
Mathias Agopian78fd5012010-04-20 14:51:04 -0700208void Transform::transform(float* point, int x, int y) const
Mathias Agopianeda65402010-02-22 03:15:57 -0800209{
Mathias Agopianeda65402010-02-22 03:15:57 -0800210 const mat33& M(mMatrix);
211 vec2 v(x, y);
212 v = transform(v);
Mathias Agopian78fd5012010-04-20 14:51:04 -0700213 point[0] = v[0];
214 point[1] = v[1];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215}
216
217Rect Transform::makeBounds(int w, int h) const
218{
Mathias Agopianeda65402010-02-22 03:15:57 -0800219 return transform( Rect(w, h) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220}
221
222Rect Transform::transform(const Rect& bounds) const
223{
224 Rect r;
Mathias Agopianeda65402010-02-22 03:15:57 -0800225 vec2 lt( bounds.left, bounds.top );
226 vec2 rt( bounds.right, bounds.top );
227 vec2 lb( bounds.left, bounds.bottom );
228 vec2 rb( bounds.right, bounds.bottom );
229
230 lt = transform(lt);
231 rt = transform(rt);
232 lb = transform(lb);
233 rb = transform(rb);
234
235 r.left = floorf(min(lt[0], rt[0], lb[0], rb[0]) + 0.5f);
236 r.top = floorf(min(lt[1], rt[1], lb[1], rb[1]) + 0.5f);
237 r.right = floorf(max(lt[0], rt[0], lb[0], rb[0]) + 0.5f);
238 r.bottom = floorf(max(lt[1], rt[1], lb[1], rb[1]) + 0.5f);
239
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240 return r;
241}
242
243Region Transform::transform(const Region& reg) const
244{
245 Region out;
Mathias Agopianeda65402010-02-22 03:15:57 -0800246 if (CC_UNLIKELY(transformed())) {
247 if (CC_LIKELY(preserveRects())) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700248 Region::const_iterator it = reg.begin();
249 Region::const_iterator const end = reg.end();
250 while (it != end) {
251 out.orSelf(transform(*it++));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252 }
253 } else {
254 out.set(transform(reg.bounds()));
255 }
256 } else {
257 out = reg.translate(tx(), ty());
258 }
259 return out;
260}
261
Mathias Agopianeda65402010-02-22 03:15:57 -0800262uint32_t Transform::type() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263{
Mathias Agopianeda65402010-02-22 03:15:57 -0800264 if (mType & UNKNOWN_TYPE) {
265 // recompute what this transform is
266
267 const mat33& M(mMatrix);
268 const float a = M[0][0];
269 const float b = M[1][0];
270 const float c = M[0][1];
271 const float d = M[1][1];
272 const float x = M[2][0];
273 const float y = M[2][1];
274
275 bool scale = false;
276 uint32_t flags = ROT_0;
277 if (isZero(b) && isZero(c)) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800278 if (a<0) flags |= FLIP_H;
279 if (d<0) flags |= FLIP_V;
280 if (!absIsOne(a) || !absIsOne(d)) {
281 scale = true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800282 }
283 } else if (isZero(a) && isZero(d)) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800284 flags |= ROT_90;
285 if (b>0) flags |= FLIP_H;
286 if (c<0) flags |= FLIP_V;
287 if (!absIsOne(b) || !absIsOne(c)) {
288 scale = true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800289 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800290 } else {
Mathias Agopianeda65402010-02-22 03:15:57 -0800291 flags = ROT_INVALID;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800293
294 mType = flags << 8;
295 if (flags & ROT_INVALID) {
296 mType |= UNKNOWN;
297 } else {
298 if ((flags & ROT_90) || ((flags & ROT_180) == ROT_180))
299 mType |= ROTATE;
300 if (flags & FLIP_H)
301 mType ^= SCALE;
302 if (flags & FLIP_V)
303 mType ^= SCALE;
304 if (scale)
305 mType |= SCALE;
306 }
307
308 if (!isZero(x) || !isZero(y))
309 mType |= TRANSLATE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800311 return mType;
312}
313
314uint32_t Transform::getType() const {
315 return type() & 0xFF;
316}
317
318uint32_t Transform::getOrientation() const
319{
320 return (type() >> 8) & 0xFF;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321}
322
323bool Transform::preserveRects() const
324{
Mathias Agopianeda65402010-02-22 03:15:57 -0800325 return (type() & ROT_INVALID) ? false : true;
326}
327
328void Transform::dump(const char* name) const
329{
330 type(); // updates the type
331
332 String8 flags, type;
333 const mat33& m(mMatrix);
334 uint32_t orient = mType >> 8;
335
Mathias Agopiand1296592010-03-09 19:17:47 -0800336 if (orient&ROT_INVALID) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800337 flags.append("ROT_INVALID ");
Mathias Agopiand1296592010-03-09 19:17:47 -0800338 } else {
339 if (orient&ROT_90) {
340 flags.append("ROT_90 ");
341 } else {
342 flags.append("ROT_0 ");
343 }
344 if (orient&FLIP_V)
345 flags.append("FLIP_V ");
346 if (orient&FLIP_H)
347 flags.append("FLIP_H ");
348 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800349
Mathias Agopiand1296592010-03-09 19:17:47 -0800350 if (!(mType&(SCALE|ROTATE|TRANSLATE)))
351 type.append("IDENTITY ");
Mathias Agopianeda65402010-02-22 03:15:57 -0800352 if (mType&SCALE)
353 type.append("SCALE ");
354 if (mType&ROTATE)
355 type.append("ROTATE ");
356 if (mType&TRANSLATE)
357 type.append("TRANSLATE ");
358
Mathias Agopiand1296592010-03-09 19:17:47 -0800359 LOGD("%s 0x%08x (%s, %s)", name, mType, flags.string(), type.string());
360 LOGD("%.4f %.4f %.4f", m[0][0], m[1][0], m[2][0]);
361 LOGD("%.4f %.4f %.4f", m[0][1], m[1][1], m[2][1]);
362 LOGD("%.4f %.4f %.4f", m[0][2], m[1][2], m[2][2]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800363}
364
365// ---------------------------------------------------------------------------
366
367}; // namespace android