blob: 5e27cc9bd9bf9eed76675d7598d4fee95b9a0c6b [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 Agopianeda65402010-02-22 03:15:57 -0800162 mType = flags << 8;
163 float sx = (flags & FLIP_H) ? -1 : 1;
164 float sy = (flags & FLIP_V) ? -1 : 1;
165 float a=0, b=0, c=0, d=0, x=0, y=0;
166 int xmask = 0;
167
168 // computation of x,y
169 // x y
170 // 0 0 0
171 // w 0 ROT90
172 // w h FLIPH|FLIPV
173 // 0 h FLIPH|FLIPV|ROT90
174
175 if (flags & ROT_90) {
176 mType |= ROTATE;
177 b = -sy;
178 c = sx;
179 xmask = 1;
180 } else {
181 a = sx;
182 d = sy;
183 }
184
185 if (flags & FLIP_H) {
186 mType ^= SCALE;
187 xmask ^= 1;
188 }
189
190 if (flags & FLIP_V) {
191 mType ^= SCALE;
192 y = h;
193 }
194
195 if ((flags & ROT_180) == ROT_180) {
196 mType |= ROTATE;
197 }
198
199 if (xmask) {
200 x = w;
201 }
202
203 if (!isZero(x) || !isZero(y)) {
204 mType |= TRANSLATE;
205 }
206
207 mat33& M(mMatrix);
208 M[0][0] = a; M[1][0] = b; M[2][0] = x;
209 M[0][1] = c; M[1][1] = d; M[2][1] = y;
210 M[0][2] = 0; M[1][2] = 0; M[2][2] = 1;
Mathias Agopiand1296592010-03-09 19:17:47 -0800211
212 return NO_ERROR;
Mathias Agopianeda65402010-02-22 03:15:57 -0800213}
214
215Transform::vec2 Transform::transform(const vec2& v) const {
216 vec2 r;
217 const mat33& M(mMatrix);
218 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0];
219 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1];
220 return r;
221}
222
223Transform::vec3 Transform::transform(const vec3& v) const {
224 vec3 r;
225 const mat33& M(mMatrix);
226 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0]*v[2];
227 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1]*v[2];
228 r[2] = M[0][2]*v[0] + M[1][2]*v[1] + M[2][2]*v[2];
229 return r;
230}
231
Mathias Agopian78fd5012010-04-20 14:51:04 -0700232void Transform::transform(float* point, int x, int y) const
Mathias Agopianeda65402010-02-22 03:15:57 -0800233{
Mathias Agopianeda65402010-02-22 03:15:57 -0800234 const mat33& M(mMatrix);
235 vec2 v(x, y);
236 v = transform(v);
Mathias Agopian78fd5012010-04-20 14:51:04 -0700237 point[0] = v[0];
238 point[1] = v[1];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239}
240
241Rect Transform::makeBounds(int w, int h) const
242{
Mathias Agopianeda65402010-02-22 03:15:57 -0800243 return transform( Rect(w, h) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244}
245
246Rect Transform::transform(const Rect& bounds) const
247{
248 Rect r;
Mathias Agopianeda65402010-02-22 03:15:57 -0800249 vec2 lt( bounds.left, bounds.top );
250 vec2 rt( bounds.right, bounds.top );
251 vec2 lb( bounds.left, bounds.bottom );
252 vec2 rb( bounds.right, bounds.bottom );
253
254 lt = transform(lt);
255 rt = transform(rt);
256 lb = transform(lb);
257 rb = transform(rb);
258
259 r.left = floorf(min(lt[0], rt[0], lb[0], rb[0]) + 0.5f);
260 r.top = floorf(min(lt[1], rt[1], lb[1], rb[1]) + 0.5f);
261 r.right = floorf(max(lt[0], rt[0], lb[0], rb[0]) + 0.5f);
262 r.bottom = floorf(max(lt[1], rt[1], lb[1], rb[1]) + 0.5f);
263
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264 return r;
265}
266
267Region Transform::transform(const Region& reg) const
268{
269 Region out;
Mathias Agopianeda65402010-02-22 03:15:57 -0800270 if (CC_UNLIKELY(transformed())) {
271 if (CC_LIKELY(preserveRects())) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700272 Region::const_iterator it = reg.begin();
273 Region::const_iterator const end = reg.end();
274 while (it != end) {
275 out.orSelf(transform(*it++));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276 }
277 } else {
278 out.set(transform(reg.bounds()));
279 }
280 } else {
281 out = reg.translate(tx(), ty());
282 }
283 return out;
284}
285
Mathias Agopianeda65402010-02-22 03:15:57 -0800286uint32_t Transform::type() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287{
Mathias Agopianeda65402010-02-22 03:15:57 -0800288 if (mType & UNKNOWN_TYPE) {
289 // recompute what this transform is
290
291 const mat33& M(mMatrix);
292 const float a = M[0][0];
293 const float b = M[1][0];
294 const float c = M[0][1];
295 const float d = M[1][1];
296 const float x = M[2][0];
297 const float y = M[2][1];
298
299 bool scale = false;
300 uint32_t flags = ROT_0;
301 if (isZero(b) && isZero(c)) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800302 if (a<0) flags |= FLIP_H;
303 if (d<0) flags |= FLIP_V;
304 if (!absIsOne(a) || !absIsOne(d)) {
305 scale = true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800306 }
307 } else if (isZero(a) && isZero(d)) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800308 flags |= ROT_90;
309 if (b>0) flags |= FLIP_H;
310 if (c<0) flags |= FLIP_V;
311 if (!absIsOne(b) || !absIsOne(c)) {
312 scale = true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800313 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314 } else {
Mathias Agopianeda65402010-02-22 03:15:57 -0800315 flags = ROT_INVALID;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800317
318 mType = flags << 8;
319 if (flags & ROT_INVALID) {
320 mType |= UNKNOWN;
321 } else {
322 if ((flags & ROT_90) || ((flags & ROT_180) == ROT_180))
323 mType |= ROTATE;
324 if (flags & FLIP_H)
325 mType ^= SCALE;
326 if (flags & FLIP_V)
327 mType ^= SCALE;
328 if (scale)
329 mType |= SCALE;
330 }
331
332 if (!isZero(x) || !isZero(y))
333 mType |= TRANSLATE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800334 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800335 return mType;
336}
337
338uint32_t Transform::getType() const {
339 return type() & 0xFF;
340}
341
342uint32_t Transform::getOrientation() const
343{
344 return (type() >> 8) & 0xFF;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345}
346
347bool Transform::preserveRects() const
348{
Mathias Agopianeda65402010-02-22 03:15:57 -0800349 return (type() & ROT_INVALID) ? false : true;
350}
351
352void Transform::dump(const char* name) const
353{
354 type(); // updates the type
355
356 String8 flags, type;
357 const mat33& m(mMatrix);
358 uint32_t orient = mType >> 8;
359
Mathias Agopiand1296592010-03-09 19:17:47 -0800360 if (orient&ROT_INVALID) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800361 flags.append("ROT_INVALID ");
Mathias Agopiand1296592010-03-09 19:17:47 -0800362 } else {
363 if (orient&ROT_90) {
364 flags.append("ROT_90 ");
365 } else {
366 flags.append("ROT_0 ");
367 }
368 if (orient&FLIP_V)
369 flags.append("FLIP_V ");
370 if (orient&FLIP_H)
371 flags.append("FLIP_H ");
372 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800373
Mathias Agopiand1296592010-03-09 19:17:47 -0800374 if (!(mType&(SCALE|ROTATE|TRANSLATE)))
375 type.append("IDENTITY ");
Mathias Agopianeda65402010-02-22 03:15:57 -0800376 if (mType&SCALE)
377 type.append("SCALE ");
378 if (mType&ROTATE)
379 type.append("ROTATE ");
380 if (mType&TRANSLATE)
381 type.append("TRANSLATE ");
382
Mathias Agopiand1296592010-03-09 19:17:47 -0800383 LOGD("%s 0x%08x (%s, %s)", name, mType, flags.string(), type.string());
384 LOGD("%.4f %.4f %.4f", m[0][0], m[1][0], m[2][0]);
385 LOGD("%.4f %.4f %.4f", m[0][1], m[1][1], m[2][1]);
386 LOGD("%.4f %.4f %.4f", m[0][2], m[1][2], m[2][2]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800387}
388
389// ---------------------------------------------------------------------------
390
391}; // namespace android