• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 #include <math.h>
18 
19 #include <cutils/compiler.h>
20 #include <utils/String8.h>
21 #include <ui/Region.h>
22 
23 #include "clz.h"
24 #include "Transform.h"
25 
26 // ---------------------------------------------------------------------------
27 
28 namespace android {
29 
30 // ---------------------------------------------------------------------------
31 
Transform()32 Transform::Transform() {
33     reset();
34 }
35 
Transform(const Transform & other)36 Transform::Transform(const Transform&  other)
37     : mMatrix(other.mMatrix), mType(other.mType) {
38 }
39 
Transform(uint32_t orientation)40 Transform::Transform(uint32_t orientation) {
41     set(orientation, 0, 0);
42 }
43 
~Transform()44 Transform::~Transform() {
45 }
46 
47 static const float EPSILON = 0.0f;
48 
isZero(float f)49 bool Transform::isZero(float f) {
50     return fabs(f) <= EPSILON;
51 }
52 
absIsOne(float f)53 bool Transform::absIsOne(float f) {
54     return isZero(fabs(f) - 1.0f);
55 }
56 
operator *(const Transform & rhs) const57 Transform Transform::operator * (const Transform& rhs) const
58 {
59     if (CC_LIKELY(mType == IDENTITY))
60         return rhs;
61 
62     Transform r(*this);
63     if (rhs.mType == IDENTITY)
64         return r;
65 
66     // TODO: we could use mType to optimize the matrix multiply
67     const mat33& A(mMatrix);
68     const mat33& B(rhs.mMatrix);
69           mat33& D(r.mMatrix);
70     for (int i=0 ; i<3 ; i++) {
71         const float v0 = A[0][i];
72         const float v1 = A[1][i];
73         const float v2 = A[2][i];
74         D[0][i] = v0*B[0][0] + v1*B[0][1] + v2*B[0][2];
75         D[1][i] = v0*B[1][0] + v1*B[1][1] + v2*B[1][2];
76         D[2][i] = v0*B[2][0] + v1*B[2][1] + v2*B[2][2];
77     }
78     r.mType |= rhs.mType;
79 
80     // TODO: we could recompute this value from r and rhs
81     r.mType &= 0xFF;
82     r.mType |= UNKNOWN_TYPE;
83     return r;
84 }
85 
operator [](int i) const86 float const* Transform::operator [] (int i) const {
87     return mMatrix[i].v;
88 }
89 
transformed() const90 bool Transform::transformed() const {
91     return type() > TRANSLATE;
92 }
93 
tx() const94 float Transform::tx() const {
95     return mMatrix[2][0];
96 }
97 
ty() const98 float Transform::ty() const {
99     return mMatrix[2][1];
100 }
101 
reset()102 void Transform::reset() {
103     mType = IDENTITY;
104     for(int i=0 ; i<3 ; i++) {
105         vec3& v(mMatrix[i]);
106         for (int j=0 ; j<3 ; j++)
107             v[j] = ((i==j) ? 1.0f : 0.0f);
108     }
109 }
110 
set(float tx,float ty)111 void Transform::set(float tx, float ty)
112 {
113     mMatrix[2][0] = tx;
114     mMatrix[2][1] = ty;
115     mMatrix[2][2] = 1.0f;
116 
117     if (isZero(tx) && isZero(ty)) {
118         mType &= ~TRANSLATE;
119     } else {
120         mType |= TRANSLATE;
121     }
122 }
123 
set(float a,float b,float c,float d)124 void Transform::set(float a, float b, float c, float d)
125 {
126     mat33& M(mMatrix);
127     M[0][0] = a;    M[1][0] = b;
128     M[0][1] = c;    M[1][1] = d;
129     M[0][2] = 0;    M[1][2] = 0;
130     mType = UNKNOWN_TYPE;
131 }
132 
set(uint32_t flags,float w,float h)133 status_t Transform::set(uint32_t flags, float w, float h)
134 {
135     if (flags & ROT_INVALID) {
136         // that's not allowed!
137         reset();
138         return BAD_VALUE;
139     }
140 
141     Transform H, V, R;
142     if (flags & ROT_90) {
143         // w & h are inverted when rotating by 90 degrees
144         swap(w, h);
145     }
146 
147     if (flags & FLIP_H) {
148         H.mType = (FLIP_H << 8) | SCALE;
149         H.mType |= isZero(w) ? IDENTITY : TRANSLATE;
150         mat33& M(H.mMatrix);
151         M[0][0] = -1;
152         M[2][0] = w;
153     }
154 
155     if (flags & FLIP_V) {
156         V.mType = (FLIP_V << 8) | SCALE;
157         V.mType |= isZero(h) ? IDENTITY : TRANSLATE;
158         mat33& M(V.mMatrix);
159         M[1][1] = -1;
160         M[2][1] = h;
161     }
162 
163     if (flags & ROT_90) {
164         const float original_w = h;
165         R.mType = (ROT_90 << 8) | ROTATE;
166         R.mType |= isZero(original_w) ? IDENTITY : TRANSLATE;
167         mat33& M(R.mMatrix);
168         M[0][0] = 0;    M[1][0] =-1;    M[2][0] = original_w;
169         M[0][1] = 1;    M[1][1] = 0;
170     }
171 
172     *this = (R*(H*V));
173     return NO_ERROR;
174 }
175 
transform(const vec2 & v) const176 Transform::vec2 Transform::transform(const vec2& v) const {
177     vec2 r;
178     const mat33& M(mMatrix);
179     r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0];
180     r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1];
181     return r;
182 }
183 
transform(const vec3 & v) const184 Transform::vec3 Transform::transform(const vec3& v) const {
185     vec3 r;
186     const mat33& M(mMatrix);
187     r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0]*v[2];
188     r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1]*v[2];
189     r[2] = M[0][2]*v[0] + M[1][2]*v[1] + M[2][2]*v[2];
190     return r;
191 }
192 
transform(float * point,int x,int y) const193 void Transform::transform(float* point, int x, int y) const
194 {
195     vec2 v(x, y);
196     v = transform(v);
197     point[0] = v[0];
198     point[1] = v[1];
199 }
200 
makeBounds(int w,int h) const201 Rect Transform::makeBounds(int w, int h) const
202 {
203     return transform( Rect(w, h) );
204 }
205 
transform(const Rect & bounds) const206 Rect Transform::transform(const Rect& bounds) const
207 {
208     Rect r;
209     vec2 lt( bounds.left,  bounds.top    );
210     vec2 rt( bounds.right, bounds.top    );
211     vec2 lb( bounds.left,  bounds.bottom );
212     vec2 rb( bounds.right, bounds.bottom );
213 
214     lt = transform(lt);
215     rt = transform(rt);
216     lb = transform(lb);
217     rb = transform(rb);
218 
219     r.left   = floorf(min(lt[0], rt[0], lb[0], rb[0]) + 0.5f);
220     r.top    = floorf(min(lt[1], rt[1], lb[1], rb[1]) + 0.5f);
221     r.right  = floorf(max(lt[0], rt[0], lb[0], rb[0]) + 0.5f);
222     r.bottom = floorf(max(lt[1], rt[1], lb[1], rb[1]) + 0.5f);
223 
224     return r;
225 }
226 
transform(const Region & reg) const227 Region Transform::transform(const Region& reg) const
228 {
229     Region out;
230     if (CC_UNLIKELY(transformed())) {
231         if (CC_LIKELY(preserveRects())) {
232             Region::const_iterator it = reg.begin();
233             Region::const_iterator const end = reg.end();
234             while (it != end) {
235                 out.orSelf(transform(*it++));
236             }
237         } else {
238             out.set(transform(reg.bounds()));
239         }
240     } else {
241         int xpos = floorf(tx() + 0.5f);
242         int ypos = floorf(ty() + 0.5f);
243         out = reg.translate(xpos, ypos);
244     }
245     return out;
246 }
247 
type() const248 uint32_t Transform::type() const
249 {
250     if (mType & UNKNOWN_TYPE) {
251         // recompute what this transform is
252 
253         const mat33& M(mMatrix);
254         const float a = M[0][0];
255         const float b = M[1][0];
256         const float c = M[0][1];
257         const float d = M[1][1];
258         const float x = M[2][0];
259         const float y = M[2][1];
260 
261         bool scale = false;
262         uint32_t flags = ROT_0;
263         if (isZero(b) && isZero(c)) {
264             if (a<0)    flags |= FLIP_H;
265             if (d<0)    flags |= FLIP_V;
266             if (!absIsOne(a) || !absIsOne(d)) {
267                 scale = true;
268             }
269         } else if (isZero(a) && isZero(d)) {
270             flags |= ROT_90;
271             if (b>0)    flags |= FLIP_V;
272             if (c<0)    flags |= FLIP_H;
273             if (!absIsOne(b) || !absIsOne(c)) {
274                 scale = true;
275             }
276         } else {
277             // there is a skew component and/or a non 90 degrees rotation
278             flags = ROT_INVALID;
279         }
280 
281         mType = flags << 8;
282         if (flags & ROT_INVALID) {
283             mType |= UNKNOWN;
284         } else {
285             if ((flags & ROT_90) || ((flags & ROT_180) == ROT_180))
286                 mType |= ROTATE;
287             if (flags & FLIP_H)
288                 mType ^= SCALE;
289             if (flags & FLIP_V)
290                 mType ^= SCALE;
291             if (scale)
292                 mType |= SCALE;
293         }
294 
295         if (!isZero(x) || !isZero(y))
296             mType |= TRANSLATE;
297     }
298     return mType;
299 }
300 
getType() const301 uint32_t Transform::getType() const {
302     return type() & 0xFF;
303 }
304 
getOrientation() const305 uint32_t Transform::getOrientation() const
306 {
307     return (type() >> 8) & 0xFF;
308 }
309 
preserveRects() const310 bool Transform::preserveRects() const
311 {
312     return (getOrientation() & ROT_INVALID) ? false : true;
313 }
314 
dump(const char * name) const315 void Transform::dump(const char* name) const
316 {
317     type(); // updates the type
318 
319     String8 flags, type;
320     const mat33& m(mMatrix);
321     uint32_t orient = mType >> 8;
322 
323     if (orient&ROT_INVALID) {
324         flags.append("ROT_INVALID ");
325     } else {
326         if (orient&ROT_90) {
327             flags.append("ROT_90 ");
328         } else {
329             flags.append("ROT_0 ");
330         }
331         if (orient&FLIP_V)
332             flags.append("FLIP_V ");
333         if (orient&FLIP_H)
334             flags.append("FLIP_H ");
335     }
336 
337     if (!(mType&(SCALE|ROTATE|TRANSLATE)))
338         type.append("IDENTITY ");
339     if (mType&SCALE)
340         type.append("SCALE ");
341     if (mType&ROTATE)
342         type.append("ROTATE ");
343     if (mType&TRANSLATE)
344         type.append("TRANSLATE ");
345 
346     ALOGD("%s 0x%08x (%s, %s)", name, mType, flags.string(), type.string());
347     ALOGD("%.4f  %.4f  %.4f", m[0][0], m[1][0], m[2][0]);
348     ALOGD("%.4f  %.4f  %.4f", m[0][1], m[1][1], m[2][1]);
349     ALOGD("%.4f  %.4f  %.4f", m[0][2], m[1][2], m[2][2]);
350 }
351 
352 // ---------------------------------------------------------------------------
353 
354 }; // namespace android
355