blob: b2d5856f347f1381b75e9ff9b6a6095b2c1552d8 [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 Agopianeda65402010-02-22 03:15:57 -080068
69bool Transform::absIsOne(float f) {
70 return fabs(f) == 1.0f;
71}
72
73bool Transform::isZero(float f) {
74 return fabs(f) == 0.0f;
75}
76
77bool Transform::absEqual(float a, float b) {
78 return fabs(a) == fabs(b);
79}
80
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081Transform Transform::operator * (const Transform& rhs) const
82{
Mathias Agopianeda65402010-02-22 03:15:57 -080083 if (CC_LIKELY(mType == IDENTITY))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 return rhs;
85
86 Transform r(*this);
Mathias Agopianeda65402010-02-22 03:15:57 -080087 if (rhs.mType == IDENTITY)
88 return r;
89
90 // TODO: we could use mType to optimize the matrix multiply
91 const mat33& A(mMatrix);
92 const mat33& B(rhs.mMatrix);
93 mat33& D(r.mMatrix);
94 for (int i=0 ; i<3 ; i++) {
95 const float v0 = A[0][i];
96 const float v1 = A[1][i];
97 const float v2 = A[2][i];
98 D[0][i] = v0*B[0][0] + v1*B[0][1] + v2*B[0][2];
99 D[1][i] = v0*B[1][0] + v1*B[1][1] + v2*B[1][2];
100 D[2][i] = v0*B[2][0] + v1*B[2][1] + v2*B[2][2];
101 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102 r.mType |= rhs.mType;
Mathias Agopianeda65402010-02-22 03:15:57 -0800103
104 // TODO: we could recompute this value from r and rhs
105 r.mType &= 0xFF;
106 r.mType |= UNKNOWN_TYPE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 return r;
108}
109
Mathias Agopianeda65402010-02-22 03:15:57 -0800110float const* Transform::operator [] (int i) const {
111 return mMatrix[i].v;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112}
113
114bool Transform::transformed() const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800115 return type() > TRANSLATE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116}
117
118int Transform::tx() const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800119 return floorf(mMatrix[2][0] + 0.5f);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120}
121
122int Transform::ty() const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800123 return floorf(mMatrix[2][1] + 0.5f);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124}
125
126void Transform::reset() {
Mathias Agopianeda65402010-02-22 03:15:57 -0800127 mType = IDENTITY;
128 for(int i=0 ; i<3 ; i++) {
129 vec3& v(mMatrix[i]);
130 for (int j=0 ; j<3 ; j++)
131 v[j] = ((i==j) ? 1.0f : 0.0f);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132 }
133}
134
Mathias Agopianeda65402010-02-22 03:15:57 -0800135void Transform::set(float tx, float ty)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136{
Mathias Agopianeda65402010-02-22 03:15:57 -0800137 mMatrix[2][0] = tx;
138 mMatrix[2][1] = ty;
139 mMatrix[2][2] = 1.0f;
140
141 if (isZero(tx) && isZero(ty)) {
142 mType &= ~TRANSLATE;
143 } else {
144 mType |= TRANSLATE;
145 }
146}
147
148void Transform::set(float a, float b, float c, float d)
149{
150 mat33& M(mMatrix);
151 M[0][0] = a; M[1][0] = b;
152 M[0][1] = c; M[1][1] = d;
153 M[0][2] = 0; M[1][2] = 0;
154 mType = UNKNOWN_TYPE;
155}
156
157void Transform::set(uint32_t flags, float w, float h)
158{
159 mType = flags << 8;
160 float sx = (flags & FLIP_H) ? -1 : 1;
161 float sy = (flags & FLIP_V) ? -1 : 1;
162 float a=0, b=0, c=0, d=0, x=0, y=0;
163 int xmask = 0;
164
165 // computation of x,y
166 // x y
167 // 0 0 0
168 // w 0 ROT90
169 // w h FLIPH|FLIPV
170 // 0 h FLIPH|FLIPV|ROT90
171
172 if (flags & ROT_90) {
173 mType |= ROTATE;
174 b = -sy;
175 c = sx;
176 xmask = 1;
177 } else {
178 a = sx;
179 d = sy;
180 }
181
182 if (flags & FLIP_H) {
183 mType ^= SCALE;
184 xmask ^= 1;
185 }
186
187 if (flags & FLIP_V) {
188 mType ^= SCALE;
189 y = h;
190 }
191
192 if ((flags & ROT_180) == ROT_180) {
193 mType |= ROTATE;
194 }
195
196 if (xmask) {
197 x = w;
198 }
199
200 if (!isZero(x) || !isZero(y)) {
201 mType |= TRANSLATE;
202 }
203
204 mat33& M(mMatrix);
205 M[0][0] = a; M[1][0] = b; M[2][0] = x;
206 M[0][1] = c; M[1][1] = d; M[2][1] = y;
207 M[0][2] = 0; M[1][2] = 0; M[2][2] = 1;
208}
209
210Transform::vec2 Transform::transform(const vec2& v) const {
211 vec2 r;
212 const mat33& M(mMatrix);
213 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0];
214 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1];
215 return r;
216}
217
218Transform::vec3 Transform::transform(const vec3& v) const {
219 vec3 r;
220 const mat33& M(mMatrix);
221 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0]*v[2];
222 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1]*v[2];
223 r[2] = M[0][2]*v[0] + M[1][2]*v[1] + M[2][2]*v[2];
224 return r;
225}
226
227void Transform::transform(fixed1616* point, int x, int y) const
228{
229 const float toFixed = 65536.0f;
230 const mat33& M(mMatrix);
231 vec2 v(x, y);
232 v = transform(v);
233 point[0] = v[0] * toFixed;
234 point[1] = v[1] * toFixed;
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)) {
298 if (absEqual(a, d)) {
299 if (a<0) flags |= FLIP_H;
300 if (d<0) flags |= FLIP_V;
301 if (!absIsOne(a) || !absIsOne(d)) {
302 scale = true;
303 }
304 } else {
305 flags = ROT_INVALID;
306 }
307 } else if (isZero(a) && isZero(d)) {
308 if (absEqual(b, c)) {
309 flags |= ROT_90;
310 if (b>0) flags |= FLIP_H;
311 if (c<0) flags |= FLIP_V;
312 if (!absIsOne(b) || !absIsOne(c)) {
313 scale = true;
314 }
315 } else {
316 flags = ROT_INVALID;
317 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318 } else {
Mathias Agopianeda65402010-02-22 03:15:57 -0800319 flags = ROT_INVALID;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800321
322 mType = flags << 8;
323 if (flags & ROT_INVALID) {
324 mType |= UNKNOWN;
325 } else {
326 if ((flags & ROT_90) || ((flags & ROT_180) == ROT_180))
327 mType |= ROTATE;
328 if (flags & FLIP_H)
329 mType ^= SCALE;
330 if (flags & FLIP_V)
331 mType ^= SCALE;
332 if (scale)
333 mType |= SCALE;
334 }
335
336 if (!isZero(x) || !isZero(y))
337 mType |= TRANSLATE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800339 return mType;
340}
341
342uint32_t Transform::getType() const {
343 return type() & 0xFF;
344}
345
346uint32_t Transform::getOrientation() const
347{
348 return (type() >> 8) & 0xFF;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349}
350
351bool Transform::preserveRects() const
352{
Mathias Agopianeda65402010-02-22 03:15:57 -0800353 return (type() & ROT_INVALID) ? false : true;
354}
355
356void Transform::dump(const char* name) const
357{
358 type(); // updates the type
359
360 String8 flags, type;
361 const mat33& m(mMatrix);
362 uint32_t orient = mType >> 8;
363
364 if (orient&ROT_INVALID)
365 flags.append("ROT_INVALID ");
366 if (orient&ROT_90)
367 flags.append("ROT_90 ");
368 if (orient&FLIP_V)
369 flags.append("FLIP_V ");
370 if (orient&FLIP_H)
371 flags.append("FLIP_H ");
372
373 if (mType&SCALE)
374 type.append("SCALE ");
375 if (mType&ROTATE)
376 type.append("ROTATE ");
377 if (mType&TRANSLATE)
378 type.append("TRANSLATE ");
379
380 LOGD("%s (%s, %s)", name, flags.string(), type.string());
381 LOGD("%.2f %.2f %.2f", m[0][0], m[1][0], m[2][0]);
382 LOGD("%.2f %.2f %.2f", m[0][1], m[1][1], m[2][1]);
383 LOGD("%.2f %.2f %.2f", m[0][2], m[1][2], m[2][2]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800384}
385
386// ---------------------------------------------------------------------------
387
388}; // namespace android