blob: 0467a14604c9bc9cfaef74905736c96fe7417e4c [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 Agopian883dffa2010-10-25 18:29:35 -070031template <typename T>
32static inline T min(T a, T b) {
Mathias Agopianeda65402010-02-22 03:15:57 -080033 return a<b ? a : b;
34}
Mathias Agopian883dffa2010-10-25 18:29:35 -070035template <typename T>
36static inline T min(T a, T b, T c) {
Mathias Agopianeda65402010-02-22 03:15:57 -080037 return min(a, min(b, c));
38}
Mathias Agopian883dffa2010-10-25 18:29:35 -070039template <typename T>
40static inline T min(T a, T b, T c, T d) {
Mathias Agopianeda65402010-02-22 03:15:57 -080041 return min(a, b, min(c, d));
42}
43
Mathias Agopian883dffa2010-10-25 18:29:35 -070044template <typename T>
45static inline T max(T a, T b) {
Mathias Agopianeda65402010-02-22 03:15:57 -080046 return a>b ? a : b;
47}
Mathias Agopian883dffa2010-10-25 18:29:35 -070048template <typename T>
49static inline T max(T a, T b, T c) {
Mathias Agopianeda65402010-02-22 03:15:57 -080050 return max(a, max(b, c));
51}
Mathias Agopian883dffa2010-10-25 18:29:35 -070052template <typename T>
53static inline T max(T a, T b, T c, T d) {
Mathias Agopianeda65402010-02-22 03:15:57 -080054 return max(a, b, max(c, d));
55}
56
Mathias Agopian883dffa2010-10-25 18:29:35 -070057template <typename T>
58static inline
59void swap(T& a, T& b) {
60 T t(a);
61 a = b;
62 b = t;
63}
64
Mathias Agopianeda65402010-02-22 03:15:57 -080065// ---------------------------------------------------------------------------
66
67Transform::Transform() {
68 reset();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069}
70
71Transform::Transform(const Transform& other)
Mathias Agopianeda65402010-02-22 03:15:57 -080072 : mMatrix(other.mMatrix), mType(other.mType) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073}
74
Mathias Agopianeda65402010-02-22 03:15:57 -080075Transform::Transform(uint32_t orientation) {
76 set(orientation, 0, 0);
Chih-Chung Chang52e72002010-01-21 17:31:06 -080077}
78
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079Transform::~Transform() {
80}
81
Mathias Agopiand1296592010-03-09 19:17:47 -080082static const float EPSILON = 0.0f;
Mathias Agopianeda65402010-02-22 03:15:57 -080083
84bool Transform::isZero(float f) {
Mathias Agopiand1296592010-03-09 19:17:47 -080085 return fabs(f) <= EPSILON;
Mathias Agopianeda65402010-02-22 03:15:57 -080086}
87
Mathias Agopiand1296592010-03-09 19:17:47 -080088bool Transform::absIsOne(float f) {
89 return isZero(fabs(f) - 1.0f);
Mathias Agopianeda65402010-02-22 03:15:57 -080090}
91
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092Transform Transform::operator * (const Transform& rhs) const
93{
Mathias Agopianeda65402010-02-22 03:15:57 -080094 if (CC_LIKELY(mType == IDENTITY))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 return rhs;
96
97 Transform r(*this);
Mathias Agopianeda65402010-02-22 03:15:57 -080098 if (rhs.mType == IDENTITY)
99 return r;
100
101 // TODO: we could use mType to optimize the matrix multiply
102 const mat33& A(mMatrix);
103 const mat33& B(rhs.mMatrix);
104 mat33& D(r.mMatrix);
105 for (int i=0 ; i<3 ; i++) {
106 const float v0 = A[0][i];
107 const float v1 = A[1][i];
108 const float v2 = A[2][i];
109 D[0][i] = v0*B[0][0] + v1*B[0][1] + v2*B[0][2];
110 D[1][i] = v0*B[1][0] + v1*B[1][1] + v2*B[1][2];
111 D[2][i] = v0*B[2][0] + v1*B[2][1] + v2*B[2][2];
112 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113 r.mType |= rhs.mType;
Mathias Agopianeda65402010-02-22 03:15:57 -0800114
115 // TODO: we could recompute this value from r and rhs
116 r.mType &= 0xFF;
117 r.mType |= UNKNOWN_TYPE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118 return r;
119}
120
Mathias Agopianeda65402010-02-22 03:15:57 -0800121float const* Transform::operator [] (int i) const {
122 return mMatrix[i].v;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123}
124
125bool Transform::transformed() const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800126 return type() > TRANSLATE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127}
128
129int Transform::tx() const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800130 return floorf(mMatrix[2][0] + 0.5f);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131}
132
133int Transform::ty() const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800134 return floorf(mMatrix[2][1] + 0.5f);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135}
136
137void Transform::reset() {
Mathias Agopianeda65402010-02-22 03:15:57 -0800138 mType = IDENTITY;
139 for(int i=0 ; i<3 ; i++) {
140 vec3& v(mMatrix[i]);
141 for (int j=0 ; j<3 ; j++)
142 v[j] = ((i==j) ? 1.0f : 0.0f);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143 }
144}
145
Mathias Agopianeda65402010-02-22 03:15:57 -0800146void Transform::set(float tx, float ty)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147{
Mathias Agopianeda65402010-02-22 03:15:57 -0800148 mMatrix[2][0] = tx;
149 mMatrix[2][1] = ty;
150 mMatrix[2][2] = 1.0f;
151
152 if (isZero(tx) && isZero(ty)) {
153 mType &= ~TRANSLATE;
154 } else {
155 mType |= TRANSLATE;
156 }
157}
158
159void Transform::set(float a, float b, float c, float d)
160{
161 mat33& M(mMatrix);
162 M[0][0] = a; M[1][0] = b;
163 M[0][1] = c; M[1][1] = d;
164 M[0][2] = 0; M[1][2] = 0;
165 mType = UNKNOWN_TYPE;
166}
167
Mathias Agopiand1296592010-03-09 19:17:47 -0800168status_t Transform::set(uint32_t flags, float w, float h)
Mathias Agopianeda65402010-02-22 03:15:57 -0800169{
Mathias Agopiand1296592010-03-09 19:17:47 -0800170 if (flags & ROT_INVALID) {
171 // that's not allowed!
172 reset();
173 return BAD_VALUE;
174 }
175
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700176 Transform H, V, R;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700177 if (flags & ROT_90) {
178 // w & h are inverted when rotating by 90 degrees
179 swap(w, h);
180 }
181
Mathias Agopianeda65402010-02-22 03:15:57 -0800182 if (flags & FLIP_H) {
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700183 H.mType = (FLIP_H << 8) | SCALE;
184 H.mType |= isZero(w) ? IDENTITY : TRANSLATE;
185 mat33& M(H.mMatrix);
186 M[0][0] = -1;
187 M[2][0] = w;
Mathias Agopianeda65402010-02-22 03:15:57 -0800188 }
189
190 if (flags & FLIP_V) {
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700191 V.mType = (FLIP_V << 8) | SCALE;
192 V.mType |= isZero(h) ? IDENTITY : TRANSLATE;
193 mat33& M(V.mMatrix);
194 M[1][1] = -1;
195 M[2][1] = h;
Mathias Agopianeda65402010-02-22 03:15:57 -0800196 }
197
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700198 if (flags & ROT_90) {
Mathias Agopian883dffa2010-10-25 18:29:35 -0700199 const float original_w = h;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700200 R.mType = (ROT_90 << 8) | ROTATE;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700201 R.mType |= isZero(original_w) ? IDENTITY : TRANSLATE;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700202 mat33& M(R.mMatrix);
Mathias Agopian883dffa2010-10-25 18:29:35 -0700203 M[0][0] = 0; M[1][0] =-1; M[2][0] = original_w;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700204 M[0][1] = 1; M[1][1] = 0;
Mathias Agopianeda65402010-02-22 03:15:57 -0800205 }
206
Mathias Agopian883dffa2010-10-25 18:29:35 -0700207 *this = (R*(H*V));
Mathias Agopiand1296592010-03-09 19:17:47 -0800208 return NO_ERROR;
Mathias Agopianeda65402010-02-22 03:15:57 -0800209}
210
211Transform::vec2 Transform::transform(const vec2& v) const {
212 vec2 r;
213 const mat33& M(mMatrix);
214 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0];
215 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1];
216 return r;
217}
218
219Transform::vec3 Transform::transform(const vec3& v) const {
220 vec3 r;
221 const mat33& M(mMatrix);
222 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0]*v[2];
223 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1]*v[2];
224 r[2] = M[0][2]*v[0] + M[1][2]*v[1] + M[2][2]*v[2];
225 return r;
226}
227
Mathias Agopian78fd5012010-04-20 14:51:04 -0700228void Transform::transform(float* point, int x, int y) const
Mathias Agopianeda65402010-02-22 03:15:57 -0800229{
Mathias Agopianeda65402010-02-22 03:15:57 -0800230 const mat33& M(mMatrix);
231 vec2 v(x, y);
232 v = transform(v);
Mathias Agopian78fd5012010-04-20 14:51:04 -0700233 point[0] = v[0];
234 point[1] = v[1];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235}
236
237Rect Transform::makeBounds(int w, int h) const
238{
Mathias Agopianeda65402010-02-22 03:15:57 -0800239 return transform( Rect(w, h) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240}
241
242Rect Transform::transform(const Rect& bounds) const
243{
244 Rect r;
Mathias Agopianeda65402010-02-22 03:15:57 -0800245 vec2 lt( bounds.left, bounds.top );
246 vec2 rt( bounds.right, bounds.top );
247 vec2 lb( bounds.left, bounds.bottom );
248 vec2 rb( bounds.right, bounds.bottom );
249
250 lt = transform(lt);
251 rt = transform(rt);
252 lb = transform(lb);
253 rb = transform(rb);
254
255 r.left = floorf(min(lt[0], rt[0], lb[0], rb[0]) + 0.5f);
256 r.top = floorf(min(lt[1], rt[1], lb[1], rb[1]) + 0.5f);
257 r.right = floorf(max(lt[0], rt[0], lb[0], rb[0]) + 0.5f);
258 r.bottom = floorf(max(lt[1], rt[1], lb[1], rb[1]) + 0.5f);
259
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 return r;
261}
262
263Region Transform::transform(const Region& reg) const
264{
265 Region out;
Mathias Agopianeda65402010-02-22 03:15:57 -0800266 if (CC_UNLIKELY(transformed())) {
267 if (CC_LIKELY(preserveRects())) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700268 Region::const_iterator it = reg.begin();
269 Region::const_iterator const end = reg.end();
270 while (it != end) {
271 out.orSelf(transform(*it++));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272 }
273 } else {
274 out.set(transform(reg.bounds()));
275 }
276 } else {
277 out = reg.translate(tx(), ty());
278 }
279 return out;
280}
281
Mathias Agopianeda65402010-02-22 03:15:57 -0800282uint32_t Transform::type() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283{
Mathias Agopianeda65402010-02-22 03:15:57 -0800284 if (mType & UNKNOWN_TYPE) {
285 // recompute what this transform is
286
287 const mat33& M(mMatrix);
288 const float a = M[0][0];
289 const float b = M[1][0];
290 const float c = M[0][1];
291 const float d = M[1][1];
292 const float x = M[2][0];
293 const float y = M[2][1];
294
295 bool scale = false;
296 uint32_t flags = ROT_0;
297 if (isZero(b) && isZero(c)) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800298 if (a<0) flags |= FLIP_H;
299 if (d<0) flags |= FLIP_V;
300 if (!absIsOne(a) || !absIsOne(d)) {
301 scale = true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800302 }
303 } else if (isZero(a) && isZero(d)) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800304 flags |= ROT_90;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700305 if (b>0) flags |= FLIP_V;
306 if (c<0) flags |= FLIP_H;
Mathias Agopiand1296592010-03-09 19:17:47 -0800307 if (!absIsOne(b) || !absIsOne(c)) {
308 scale = true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800309 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310 } else {
Mathias Agopianeda65402010-02-22 03:15:57 -0800311 flags = ROT_INVALID;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800313
314 mType = flags << 8;
315 if (flags & ROT_INVALID) {
316 mType |= UNKNOWN;
317 } else {
318 if ((flags & ROT_90) || ((flags & ROT_180) == ROT_180))
319 mType |= ROTATE;
320 if (flags & FLIP_H)
321 mType ^= SCALE;
322 if (flags & FLIP_V)
323 mType ^= SCALE;
324 if (scale)
325 mType |= SCALE;
326 }
327
328 if (!isZero(x) || !isZero(y))
329 mType |= TRANSLATE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800331 return mType;
332}
333
334uint32_t Transform::getType() const {
335 return type() & 0xFF;
336}
337
338uint32_t Transform::getOrientation() const
339{
340 return (type() >> 8) & 0xFF;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800341}
342
343bool Transform::preserveRects() const
344{
Mathias Agopianeda65402010-02-22 03:15:57 -0800345 return (type() & ROT_INVALID) ? false : true;
346}
347
348void Transform::dump(const char* name) const
349{
350 type(); // updates the type
351
352 String8 flags, type;
353 const mat33& m(mMatrix);
354 uint32_t orient = mType >> 8;
355
Mathias Agopiand1296592010-03-09 19:17:47 -0800356 if (orient&ROT_INVALID) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800357 flags.append("ROT_INVALID ");
Mathias Agopiand1296592010-03-09 19:17:47 -0800358 } else {
359 if (orient&ROT_90) {
360 flags.append("ROT_90 ");
361 } else {
362 flags.append("ROT_0 ");
363 }
364 if (orient&FLIP_V)
365 flags.append("FLIP_V ");
366 if (orient&FLIP_H)
367 flags.append("FLIP_H ");
368 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800369
Mathias Agopiand1296592010-03-09 19:17:47 -0800370 if (!(mType&(SCALE|ROTATE|TRANSLATE)))
371 type.append("IDENTITY ");
Mathias Agopianeda65402010-02-22 03:15:57 -0800372 if (mType&SCALE)
373 type.append("SCALE ");
374 if (mType&ROTATE)
375 type.append("ROTATE ");
376 if (mType&TRANSLATE)
377 type.append("TRANSLATE ");
378
Mathias Agopiand1296592010-03-09 19:17:47 -0800379 LOGD("%s 0x%08x (%s, %s)", name, mType, flags.string(), type.string());
380 LOGD("%.4f %.4f %.4f", m[0][0], m[1][0], m[2][0]);
381 LOGD("%.4f %.4f %.4f", m[0][1], m[1][1], m[2][1]);
382 LOGD("%.4f %.4f %.4f", m[0][2], m[1][2], m[2][2]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800383}
384
385// ---------------------------------------------------------------------------
386
387}; // namespace android