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 <android-base/stringprintf.h>
20 #include <cutils/compiler.h>
21 #include <ui/Region.h>
22 #include <ui/Transform.h>
23 #include <utils/String8.h>
24 
25 namespace android {
26 namespace ui {
27 
Transform()28 Transform::Transform() {
29     reset();
30 }
31 
Transform(const Transform & other)32 Transform::Transform(const Transform&  other)
33     : mMatrix(other.mMatrix), mType(other.mType) {
34 }
35 
Transform(uint32_t orientation)36 Transform::Transform(uint32_t orientation) {
37     set(orientation, 0, 0);
38 }
39 
40 Transform::~Transform() = default;
41 
42 static const float EPSILON = 0.0f;
43 
isZero(float f)44 bool Transform::isZero(float f) {
45     return fabs(f) <= EPSILON;
46 }
47 
absIsOne(float f)48 bool Transform::absIsOne(float f) {
49     return isZero(fabs(f) - 1.0f);
50 }
51 
operator *(const Transform & rhs) const52 Transform Transform::operator * (const Transform& rhs) const
53 {
54     if (CC_LIKELY(mType == IDENTITY))
55         return rhs;
56 
57     Transform r(*this);
58     if (rhs.mType == IDENTITY)
59         return r;
60 
61     // TODO: we could use mType to optimize the matrix multiply
62     const mat33& A(mMatrix);
63     const mat33& B(rhs.mMatrix);
64           mat33& D(r.mMatrix);
65     for (size_t i = 0; i < 3; i++) {
66         const float v0 = A[0][i];
67         const float v1 = A[1][i];
68         const float v2 = A[2][i];
69         D[0][i] = v0*B[0][0] + v1*B[0][1] + v2*B[0][2];
70         D[1][i] = v0*B[1][0] + v1*B[1][1] + v2*B[1][2];
71         D[2][i] = v0*B[2][0] + v1*B[2][1] + v2*B[2][2];
72     }
73     r.mType |= rhs.mType;
74 
75     // TODO: we could recompute this value from r and rhs
76     r.mType &= 0xFF;
77     r.mType |= UNKNOWN_TYPE;
78     return r;
79 }
80 
operator =(const Transform & other)81 Transform& Transform::operator=(const Transform& other) {
82     mMatrix = other.mMatrix;
83     mType = other.mType;
84     return *this;
85 }
86 
operator [](size_t i) const87 const vec3& Transform::operator [] (size_t i) const {
88     return mMatrix[i];
89 }
90 
tx() const91 float Transform::tx() const {
92     return mMatrix[2][0];
93 }
94 
ty() const95 float Transform::ty() const {
96     return mMatrix[2][1];
97 }
98 
sx() const99 float Transform::sx() const {
100     return mMatrix[0][0];
101 }
102 
sy() const103 float Transform::sy() const {
104     return mMatrix[1][1];
105 }
106 
reset()107 void Transform::reset() {
108     mType = IDENTITY;
109     for(size_t i = 0; i < 3; i++) {
110         vec3& v(mMatrix[i]);
111         for (size_t j = 0; j < 3; j++)
112             v[j] = ((i == j) ? 1.0f : 0.0f);
113     }
114 }
115 
set(float tx,float ty)116 void Transform::set(float tx, float ty)
117 {
118     mMatrix[2][0] = tx;
119     mMatrix[2][1] = ty;
120     mMatrix[2][2] = 1.0f;
121 
122     if (isZero(tx) && isZero(ty)) {
123         mType &= ~TRANSLATE;
124     } else {
125         mType |= TRANSLATE;
126     }
127 }
128 
set(float a,float b,float c,float d)129 void Transform::set(float a, float b, float c, float d)
130 {
131     mat33& M(mMatrix);
132     M[0][0] = a;    M[1][0] = b;
133     M[0][1] = c;    M[1][1] = d;
134     M[0][2] = 0;    M[1][2] = 0;
135     mType = UNKNOWN_TYPE;
136 }
137 
set(uint32_t flags,float w,float h)138 status_t Transform::set(uint32_t flags, float w, float h)
139 {
140     if (flags & ROT_INVALID) {
141         // that's not allowed!
142         reset();
143         return BAD_VALUE;
144     }
145 
146     Transform H, V, R;
147     if (flags & ROT_90) {
148         // w & h are inverted when rotating by 90 degrees
149         std::swap(w, h);
150     }
151 
152     if (flags & FLIP_H) {
153         H.mType = (FLIP_H << 8) | SCALE;
154         H.mType |= isZero(w) ? IDENTITY : TRANSLATE;
155         mat33& M(H.mMatrix);
156         M[0][0] = -1;
157         M[2][0] = w;
158     }
159 
160     if (flags & FLIP_V) {
161         V.mType = (FLIP_V << 8) | SCALE;
162         V.mType |= isZero(h) ? IDENTITY : TRANSLATE;
163         mat33& M(V.mMatrix);
164         M[1][1] = -1;
165         M[2][1] = h;
166     }
167 
168     if (flags & ROT_90) {
169         const float original_w = h;
170         R.mType = (ROT_90 << 8) | ROTATE;
171         R.mType |= isZero(original_w) ? IDENTITY : TRANSLATE;
172         mat33& M(R.mMatrix);
173         M[0][0] = 0;    M[1][0] =-1;    M[2][0] = original_w;
174         M[0][1] = 1;    M[1][1] = 0;
175     }
176 
177     *this = (R*(H*V));
178     return NO_ERROR;
179 }
180 
transform(const vec2 & v) const181 vec2 Transform::transform(const vec2& v) const {
182     vec2 r;
183     const mat33& M(mMatrix);
184     r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0];
185     r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1];
186     return r;
187 }
188 
transform(const vec3 & v) const189 vec3 Transform::transform(const vec3& v) const {
190     vec3 r;
191     const mat33& M(mMatrix);
192     r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0]*v[2];
193     r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1]*v[2];
194     r[2] = M[0][2]*v[0] + M[1][2]*v[1] + M[2][2]*v[2];
195     return r;
196 }
197 
transform(int x,int y) const198 vec2 Transform::transform(int x, int y) const
199 {
200     return transform(vec2(x,y));
201 }
202 
makeBounds(int w,int h) const203 Rect Transform::makeBounds(int w, int h) const
204 {
205     return transform( Rect(w, h) );
206 }
207 
transform(const Rect & bounds,bool roundOutwards) const208 Rect Transform::transform(const Rect& bounds, bool roundOutwards) const
209 {
210     Rect r;
211     vec2 lt( bounds.left,  bounds.top    );
212     vec2 rt( bounds.right, bounds.top    );
213     vec2 lb( bounds.left,  bounds.bottom );
214     vec2 rb( bounds.right, bounds.bottom );
215 
216     lt = transform(lt);
217     rt = transform(rt);
218     lb = transform(lb);
219     rb = transform(rb);
220 
221     if (roundOutwards) {
222         r.left   = static_cast<int32_t>(floorf(std::min({lt[0], rt[0], lb[0], rb[0]})));
223         r.top    = static_cast<int32_t>(floorf(std::min({lt[1], rt[1], lb[1], rb[1]})));
224         r.right  = static_cast<int32_t>(ceilf(std::max({lt[0], rt[0], lb[0], rb[0]})));
225         r.bottom = static_cast<int32_t>(ceilf(std::max({lt[1], rt[1], lb[1], rb[1]})));
226     } else {
227         r.left   = static_cast<int32_t>(floorf(std::min({lt[0], rt[0], lb[0], rb[0]}) + 0.5f));
228         r.top    = static_cast<int32_t>(floorf(std::min({lt[1], rt[1], lb[1], rb[1]}) + 0.5f));
229         r.right  = static_cast<int32_t>(floorf(std::max({lt[0], rt[0], lb[0], rb[0]}) + 0.5f));
230         r.bottom = static_cast<int32_t>(floorf(std::max({lt[1], rt[1], lb[1], rb[1]}) + 0.5f));
231     }
232 
233     return r;
234 }
235 
transform(const FloatRect & bounds) const236 FloatRect Transform::transform(const FloatRect& bounds) const
237 {
238     vec2 lt(bounds.left, bounds.top);
239     vec2 rt(bounds.right, bounds.top);
240     vec2 lb(bounds.left, bounds.bottom);
241     vec2 rb(bounds.right, bounds.bottom);
242 
243     lt = transform(lt);
244     rt = transform(rt);
245     lb = transform(lb);
246     rb = transform(rb);
247 
248     FloatRect r;
249     r.left = std::min({lt[0], rt[0], lb[0], rb[0]});
250     r.top = std::min({lt[1], rt[1], lb[1], rb[1]});
251     r.right = std::max({lt[0], rt[0], lb[0], rb[0]});
252     r.bottom = std::max({lt[1], rt[1], lb[1], rb[1]});
253 
254     return r;
255 }
256 
transform(const Region & reg) const257 Region Transform::transform(const Region& reg) const
258 {
259     Region out;
260     if (CC_UNLIKELY(type() > TRANSLATE)) {
261         if (CC_LIKELY(preserveRects())) {
262             Region::const_iterator it = reg.begin();
263             Region::const_iterator const end = reg.end();
264             while (it != end) {
265                 out.orSelf(transform(*it++));
266             }
267         } else {
268             out.set(transform(reg.bounds()));
269         }
270     } else {
271         int xpos = static_cast<int>(floorf(tx() + 0.5f));
272         int ypos = static_cast<int>(floorf(ty() + 0.5f));
273         out = reg.translate(xpos, ypos);
274     }
275     return out;
276 }
277 
type() const278 uint32_t Transform::type() const
279 {
280     if (mType & UNKNOWN_TYPE) {
281         // recompute what this transform is
282 
283         const mat33& M(mMatrix);
284         const float a = M[0][0];
285         const float b = M[1][0];
286         const float c = M[0][1];
287         const float d = M[1][1];
288         const float x = M[2][0];
289         const float y = M[2][1];
290 
291         bool scale = false;
292         uint32_t flags = ROT_0;
293         if (isZero(b) && isZero(c)) {
294             if (a<0)    flags |= FLIP_H;
295             if (d<0)    flags |= FLIP_V;
296             if (!absIsOne(a) || !absIsOne(d)) {
297                 scale = true;
298             }
299         } else if (isZero(a) && isZero(d)) {
300             flags |= ROT_90;
301             if (b>0)    flags |= FLIP_V;
302             if (c<0)    flags |= FLIP_H;
303             if (!absIsOne(b) || !absIsOne(c)) {
304                 scale = true;
305             }
306         } else {
307             // there is a skew component and/or a non 90 degrees rotation
308             flags = ROT_INVALID;
309         }
310 
311         mType = flags << 8;
312         if (flags & ROT_INVALID) {
313             mType |= UNKNOWN;
314         } else {
315             if ((flags & ROT_90) || ((flags & ROT_180) == ROT_180))
316                 mType |= ROTATE;
317             if (flags & FLIP_H)
318                 mType ^= SCALE;
319             if (flags & FLIP_V)
320                 mType ^= SCALE;
321             if (scale)
322                 mType |= SCALE;
323         }
324 
325         if (!isZero(x) || !isZero(y))
326             mType |= TRANSLATE;
327     }
328     return mType;
329 }
330 
inverse() const331 Transform Transform::inverse() const {
332     // our 3x3 matrix is always of the form of a 2x2 transformation
333     // followed by a translation: T*M, therefore:
334     // (T*M)^-1 = M^-1 * T^-1
335     Transform result;
336     if (mType <= TRANSLATE) {
337         // 1 0 0
338         // 0 1 0
339         // x y 1
340         result = *this;
341         result.mMatrix[2][0] = -result.mMatrix[2][0];
342         result.mMatrix[2][1] = -result.mMatrix[2][1];
343     } else {
344         // a c 0
345         // b d 0
346         // x y 1
347         const mat33& M(mMatrix);
348         const float a = M[0][0];
349         const float b = M[1][0];
350         const float c = M[0][1];
351         const float d = M[1][1];
352         const float x = M[2][0];
353         const float y = M[2][1];
354 
355         const float idet = 1.0f / (a*d - b*c);
356         result.mMatrix[0][0] =  d*idet;
357         result.mMatrix[0][1] = -c*idet;
358         result.mMatrix[1][0] = -b*idet;
359         result.mMatrix[1][1] =  a*idet;
360         result.mType = mType;
361 
362         vec2 T(-x, -y);
363         T = result.transform(T);
364         result.mMatrix[2][0] = T[0];
365         result.mMatrix[2][1] = T[1];
366     }
367     return result;
368 }
369 
getType() const370 uint32_t Transform::getType() const {
371     return type() & 0xFF;
372 }
373 
getOrientation() const374 uint32_t Transform::getOrientation() const
375 {
376     return (type() >> 8) & 0xFF;
377 }
378 
preserveRects() const379 bool Transform::preserveRects() const
380 {
381     return (getOrientation() & ROT_INVALID) ? false : true;
382 }
383 
asMatrix4() const384 mat4 Transform::asMatrix4() const {
385     // Internally Transform uses a 3x3 matrix since the transform is meant for
386     // two-dimensional values. An equivalent 4x4 matrix means inserting an extra
387     // row and column which adds as an identity transform on the third
388     // dimension.
389 
390     mat4 m = mat4{mat4::NO_INIT}; // NO_INIT since we explicitly set every element
391 
392     m[0][0] = mMatrix[0][0];
393     m[0][1] = mMatrix[0][1];
394     m[0][2] = 0.f;
395     m[0][3] = mMatrix[0][2];
396 
397     m[1][0] = mMatrix[1][0];
398     m[1][1] = mMatrix[1][1];
399     m[1][2] = 0.f;
400     m[1][3] = mMatrix[1][2];
401 
402     m[2][0] = 0.f;
403     m[2][1] = 0.f;
404     m[2][2] = 1.f;
405     m[2][3] = 0.f;
406 
407     m[3][0] = mMatrix[2][0];
408     m[3][1] = mMatrix[2][1];
409     m[3][2] = 0.f;
410     m[3][3] = mMatrix[2][2];
411 
412     return m;
413 }
414 
dump(std::string & out,const char * name) const415 void Transform::dump(std::string& out, const char* name) const {
416     using android::base::StringAppendF;
417 
418     type(); // Ensure the information in mType is up to date
419 
420     const uint32_t type = mType;
421     const uint32_t orient = type >> 8;
422 
423     StringAppendF(&out, "%s 0x%08x (", name, orient);
424 
425     if (orient & ROT_INVALID) {
426         out.append("ROT_INVALID ");
427     } else {
428         if (orient & ROT_90) {
429             out.append("ROT_90 ");
430         } else {
431             out.append("ROT_0 ");
432         }
433         if (orient & FLIP_V) out.append("FLIP_V ");
434         if (orient & FLIP_H) out.append("FLIP_H ");
435     }
436 
437     StringAppendF(&out, ") 0x%02x (", type);
438 
439     if (!(type & (SCALE | ROTATE | TRANSLATE))) out.append("IDENTITY ");
440     if (type & SCALE) out.append("SCALE ");
441     if (type & ROTATE) out.append("ROTATE ");
442     if (type & TRANSLATE) out.append("TRANSLATE ");
443 
444     out.append(")\n");
445 
446     for (size_t i = 0; i < 3; i++) {
447         StringAppendF(&out, "    %.4f  %.4f  %.4f\n", static_cast<double>(mMatrix[0][i]),
448                       static_cast<double>(mMatrix[1][i]), static_cast<double>(mMatrix[2][i]));
449     }
450 }
451 
dump(const char * name) const452 void Transform::dump(const char* name) const {
453     std::string out;
454     dump(out, name);
455     ALOGD("%s", out.c_str());
456 }
457 
458 }  // namespace ui
459 }  // namespace android
460