• 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 #undef LOG_TAG
18 #define LOG_TAG "Transform"
19 
20 #include <math.h>
21 
22 #include <android-base/stringprintf.h>
23 #include <cutils/compiler.h>
24 #include <ui/Region.h>
25 #include <ui/Transform.h>
26 #include <utils/String8.h>
27 
28 namespace android::ui {
29 
Transform()30 Transform::Transform() {
31     reset();
32 }
33 
Transform(const Transform & other)34 Transform::Transform(const Transform&  other)
35     : mMatrix(other.mMatrix), mType(other.mType) {
36 }
37 
Transform(uint32_t orientation,int w,int h)38 Transform::Transform(uint32_t orientation, int w, int h) {
39     set(orientation, w, h);
40 }
41 
42 Transform::~Transform() = default;
43 
44 static const float EPSILON = 0.0f;
45 
isZero(float f)46 bool Transform::isZero(float f) {
47     return fabs(f) <= EPSILON;
48 }
49 
absIsOne(float f)50 bool Transform::absIsOne(float f) {
51     return isZero(fabs(f) - 1.0f);
52 }
53 
operator ==(const Transform & other) const54 bool Transform::operator==(const Transform& other) const {
55     return mMatrix[0][0] == other.mMatrix[0][0] && mMatrix[0][1] == other.mMatrix[0][1] &&
56             mMatrix[0][2] == other.mMatrix[0][2] && mMatrix[1][0] == other.mMatrix[1][0] &&
57             mMatrix[1][1] == other.mMatrix[1][1] && mMatrix[1][2] == other.mMatrix[1][2] &&
58             mMatrix[2][0] == other.mMatrix[2][0] && mMatrix[2][1] == other.mMatrix[2][1] &&
59             mMatrix[2][2] == other.mMatrix[2][2];
60 }
61 
operator *(const Transform & rhs) const62 Transform Transform::operator*(const Transform& rhs) const {
63     if (CC_LIKELY(mType == IDENTITY))
64         return rhs;
65 
66     Transform r(*this);
67     if (rhs.mType == IDENTITY)
68         return r;
69 
70     // TODO: we could use mType to optimize the matrix multiply
71     const mat33& A(mMatrix);
72     const mat33& B(rhs.mMatrix);
73           mat33& D(r.mMatrix);
74     for (size_t i = 0; i < 3; i++) {
75         const float v0 = A[0][i];
76         const float v1 = A[1][i];
77         const float v2 = A[2][i];
78         D[0][i] = v0*B[0][0] + v1*B[0][1] + v2*B[0][2];
79         D[1][i] = v0*B[1][0] + v1*B[1][1] + v2*B[1][2];
80         D[2][i] = v0*B[2][0] + v1*B[2][1] + v2*B[2][2];
81     }
82     r.mType |= rhs.mType;
83 
84     // TODO: we could recompute this value from r and rhs
85     r.mType &= 0xFF;
86     r.mType |= UNKNOWN_TYPE;
87     return r;
88 }
89 
operator *(float value) const90 Transform Transform::operator * (float value) const {
91     Transform r(*this);
92     const mat33& M(mMatrix);
93     mat33& R(r.mMatrix);
94     for (size_t i = 0; i < 3; i++) {
95         for (size_t j = 0; j < 2; j++) {
96             R[i][j] = M[i][j] * value;
97         }
98     }
99     r.type();
100     return r;
101 }
102 
operator =(const Transform & other)103 Transform& Transform::operator=(const Transform& other) {
104     mMatrix = other.mMatrix;
105     mType = other.mType;
106     return *this;
107 }
108 
operator [](size_t i) const109 const vec3& Transform::operator [] (size_t i) const {
110     return mMatrix[i];
111 }
112 
tx() const113 float Transform::tx() const {
114     return mMatrix[2][0];
115 }
116 
ty() const117 float Transform::ty() const {
118     return mMatrix[2][1];
119 }
120 
dsdx() const121 float Transform::dsdx() const {
122     return mMatrix[0][0];
123 }
124 
dtdx() const125 float Transform::dtdx() const {
126     return mMatrix[1][0];
127 }
128 
dtdy() const129 float Transform::dtdy() const {
130     return mMatrix[0][1];
131 }
132 
dsdy() const133 float Transform::dsdy() const {
134     return mMatrix[1][1];
135 }
136 
getScaleX() const137 float Transform::getScaleX() const {
138     return sqrt((dsdx() * dsdx()) + (dtdx() * dtdx()));
139 }
140 
getScaleY() const141 float Transform::getScaleY() const {
142     return sqrt((dtdy() * dtdy()) + (dsdy() * dsdy()));
143 }
144 
reset()145 void Transform::reset() {
146     mType = IDENTITY;
147     for(size_t i = 0; i < 3; i++) {
148         vec3& v(mMatrix[i]);
149         for (size_t j = 0; j < 3; j++)
150             v[j] = ((i == j) ? 1.0f : 0.0f);
151     }
152 }
153 
set(float tx,float ty)154 void Transform::set(float tx, float ty) {
155     mMatrix[2][0] = tx;
156     mMatrix[2][1] = ty;
157     mMatrix[2][2] = 1.0f;
158 
159     if (isZero(tx) && isZero(ty)) {
160         mType &= ~TRANSLATE;
161     } else {
162         mType |= TRANSLATE;
163     }
164 }
165 
set(float a,float b,float c,float d)166 void Transform::set(float a, float b, float c, float d) {
167     mat33& M(mMatrix);
168     M[0][0] = a;    M[1][0] = b;
169     M[0][1] = c;    M[1][1] = d;
170     M[0][2] = 0;    M[1][2] = 0;
171     mType = UNKNOWN_TYPE;
172 }
173 
set(uint32_t flags,float w,float h)174 status_t Transform::set(uint32_t flags, float w, float h) {
175     if (flags & ROT_INVALID) {
176         // that's not allowed!
177         reset();
178         return BAD_VALUE;
179     }
180 
181     Transform H, V, R;
182     if (flags & ROT_90) {
183         // w & h are inverted when rotating by 90 degrees
184         std::swap(w, h);
185     }
186 
187     if (flags & FLIP_H) {
188         H.mType = (FLIP_H << 8) | SCALE;
189         H.mType |= isZero(w) ? IDENTITY : TRANSLATE;
190         mat33& M(H.mMatrix);
191         M[0][0] = -1;
192         M[2][0] = w;
193     }
194 
195     if (flags & FLIP_V) {
196         V.mType = (FLIP_V << 8) | SCALE;
197         V.mType |= isZero(h) ? IDENTITY : TRANSLATE;
198         mat33& M(V.mMatrix);
199         M[1][1] = -1;
200         M[2][1] = h;
201     }
202 
203     if (flags & ROT_90) {
204         const float original_w = h;
205         R.mType = (ROT_90 << 8) | ROTATE;
206         R.mType |= isZero(original_w) ? IDENTITY : TRANSLATE;
207         mat33& M(R.mMatrix);
208         M[0][0] = 0;    M[1][0] =-1;    M[2][0] = original_w;
209         M[0][1] = 1;    M[1][1] = 0;
210     }
211 
212     *this = (R*(H*V));
213     return NO_ERROR;
214 }
215 
set(const std::array<float,9> & matrix)216 void Transform::set(const std::array<float, 9>& matrix) {
217     mat33& M(mMatrix);
218     M[0][0] = matrix[0];  M[1][0] = matrix[1];  M[2][0] = matrix[2];
219     M[0][1] = matrix[3];  M[1][1] = matrix[4];  M[2][1] = matrix[5];
220     M[0][2] = matrix[6];  M[1][2] = matrix[7];  M[2][2] = matrix[8];
221     mType = UNKNOWN_TYPE;
222     type();
223 }
224 
transform(const vec2 & v) const225 vec2 Transform::transform(const vec2& v) const {
226     vec2 r;
227     const mat33& M(mMatrix);
228     r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0];
229     r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1];
230     return r;
231 }
232 
transform(const vec3 & v) const233 vec3 Transform::transform(const vec3& v) const {
234     vec3 r;
235     const mat33& M(mMatrix);
236     r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0]*v[2];
237     r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1]*v[2];
238     r[2] = M[0][2]*v[0] + M[1][2]*v[1] + M[2][2]*v[2];
239     return r;
240 }
241 
transform(float x,float y) const242 vec2 Transform::transform(float x, float y) const {
243     return transform(vec2(x, y));
244 }
245 
makeBounds(int w,int h) const246 Rect Transform::makeBounds(int w, int h) const {
247     return transform( Rect(w, h) );
248 }
249 
transform(const Rect & bounds,bool roundOutwards) const250 Rect Transform::transform(const Rect& bounds, bool roundOutwards) const {
251     Rect r;
252     vec2 lt( bounds.left,  bounds.top    );
253     vec2 rt( bounds.right, bounds.top    );
254     vec2 lb( bounds.left,  bounds.bottom );
255     vec2 rb( bounds.right, bounds.bottom );
256 
257     lt = transform(lt);
258     rt = transform(rt);
259     lb = transform(lb);
260     rb = transform(rb);
261 
262     if (roundOutwards) {
263         r.left   = static_cast<int32_t>(floorf(std::min({lt[0], rt[0], lb[0], rb[0]})));
264         r.top    = static_cast<int32_t>(floorf(std::min({lt[1], rt[1], lb[1], rb[1]})));
265         r.right  = static_cast<int32_t>(ceilf(std::max({lt[0], rt[0], lb[0], rb[0]})));
266         r.bottom = static_cast<int32_t>(ceilf(std::max({lt[1], rt[1], lb[1], rb[1]})));
267     } else {
268         r.left   = static_cast<int32_t>(floorf(std::min({lt[0], rt[0], lb[0], rb[0]}) + 0.5f));
269         r.top    = static_cast<int32_t>(floorf(std::min({lt[1], rt[1], lb[1], rb[1]}) + 0.5f));
270         r.right  = static_cast<int32_t>(floorf(std::max({lt[0], rt[0], lb[0], rb[0]}) + 0.5f));
271         r.bottom = static_cast<int32_t>(floorf(std::max({lt[1], rt[1], lb[1], rb[1]}) + 0.5f));
272     }
273 
274     return r;
275 }
276 
transform(const FloatRect & bounds) const277 FloatRect Transform::transform(const FloatRect& bounds) const {
278     vec2 lt(bounds.left, bounds.top);
279     vec2 rt(bounds.right, bounds.top);
280     vec2 lb(bounds.left, bounds.bottom);
281     vec2 rb(bounds.right, bounds.bottom);
282 
283     lt = transform(lt);
284     rt = transform(rt);
285     lb = transform(lb);
286     rb = transform(rb);
287 
288     FloatRect r;
289     r.left = std::min({lt[0], rt[0], lb[0], rb[0]});
290     r.top = std::min({lt[1], rt[1], lb[1], rb[1]});
291     r.right = std::max({lt[0], rt[0], lb[0], rb[0]});
292     r.bottom = std::max({lt[1], rt[1], lb[1], rb[1]});
293 
294     return r;
295 }
296 
transform(const Region & reg) const297 Region Transform::transform(const Region& reg) const {
298     Region out;
299     if (CC_UNLIKELY(type() > TRANSLATE)) {
300         if (CC_LIKELY(preserveRects())) {
301             Region::const_iterator it = reg.begin();
302             Region::const_iterator const end = reg.end();
303             while (it != end) {
304                 out.orSelf(transform(*it++));
305             }
306         } else {
307             out.set(transform(reg.bounds()));
308         }
309     } else {
310         int xpos = static_cast<int>(floorf(tx() + 0.5f));
311         int ypos = static_cast<int>(floorf(ty() + 0.5f));
312         out = reg.translate(xpos, ypos);
313     }
314     return out;
315 }
316 
type() const317 uint32_t Transform::type() const {
318     if (mType & UNKNOWN_TYPE) {
319         // recompute what this transform is
320 
321         const mat33& M(mMatrix);
322         const float a = M[0][0];
323         const float b = M[1][0];
324         const float c = M[0][1];
325         const float d = M[1][1];
326         const float x = M[2][0];
327         const float y = M[2][1];
328 
329         bool scale = false;
330         uint32_t flags = ROT_0;
331         if (isZero(b) && isZero(c)) {
332             if (a<0)    flags |= FLIP_H;
333             if (d<0)    flags |= FLIP_V;
334             if (!absIsOne(a) || !absIsOne(d)) {
335                 scale = true;
336             }
337         } else if (isZero(a) && isZero(d)) {
338             flags |= ROT_90;
339             if (b>0)    flags |= FLIP_V;
340             if (c<0)    flags |= FLIP_H;
341             if (!absIsOne(b) || !absIsOne(c)) {
342                 scale = true;
343             }
344         } else {
345             // there is a skew component and/or a non 90 degrees rotation
346             flags = ROT_INVALID;
347         }
348 
349         mType = flags << 8;
350         if (flags & ROT_INVALID) {
351             mType |= UNKNOWN;
352         } else {
353             if ((flags & ROT_90) || ((flags & ROT_180) == ROT_180))
354                 mType |= ROTATE;
355             if (flags & FLIP_H)
356                 mType ^= SCALE;
357             if (flags & FLIP_V)
358                 mType ^= SCALE;
359             if (scale)
360                 mType |= SCALE;
361         }
362 
363         if (!isZero(x) || !isZero(y))
364             mType |= TRANSLATE;
365     }
366     return mType;
367 }
368 
inverse() const369 Transform Transform::inverse() const {
370     // our 3x3 matrix is always of the form of a 2x2 transformation
371     // followed by a translation: T*M, therefore:
372     // (T*M)^-1 = M^-1 * T^-1
373     Transform result;
374     if (mType <= TRANSLATE) {
375         // 1 0 0
376         // 0 1 0
377         // x y 1
378         result = *this;
379         result.mMatrix[2][0] = -result.mMatrix[2][0];
380         result.mMatrix[2][1] = -result.mMatrix[2][1];
381     } else {
382         // a c 0
383         // b d 0
384         // x y 1
385         const mat33& M(mMatrix);
386         const float a = M[0][0];
387         const float b = M[1][0];
388         const float c = M[0][1];
389         const float d = M[1][1];
390         const float x = M[2][0];
391         const float y = M[2][1];
392 
393         const float idet = 1.0f / (a*d - b*c);
394         result.mMatrix[0][0] =  d*idet;
395         result.mMatrix[0][1] = -c*idet;
396         result.mMatrix[1][0] = -b*idet;
397         result.mMatrix[1][1] =  a*idet;
398         result.mType = mType;
399 
400         vec2 T(-x, -y);
401         T = result.transform(T);
402         result.mMatrix[2][0] = T[0];
403         result.mMatrix[2][1] = T[1];
404     }
405     return result;
406 }
407 
getType() const408 uint32_t Transform::getType() const {
409     return type() & 0xFF;
410 }
411 
getOrientation() const412 uint32_t Transform::getOrientation() const {
413     return (type() >> 8) & 0xFF;
414 }
415 
preserveRects() const416 bool Transform::preserveRects() const {
417     return (getOrientation() & ROT_INVALID) ? false : true;
418 }
419 
needsBilinearFiltering() const420 bool Transform::needsBilinearFiltering() const {
421     return (!preserveRects() || getType() >= ui::Transform::SCALE);
422 }
423 
asMatrix4() const424 mat4 Transform::asMatrix4() const {
425     // Internally Transform uses a 3x3 matrix since the transform is meant for
426     // two-dimensional values. An equivalent 4x4 matrix means inserting an extra
427     // row and column which adds as an identity transform on the third
428     // dimension.
429 
430     mat4 m = mat4{mat4::NO_INIT}; // NO_INIT since we explicitly set every element
431 
432     m[0][0] = mMatrix[0][0];
433     m[0][1] = mMatrix[0][1];
434     m[0][2] = 0.f;
435     m[0][3] = mMatrix[0][2];
436 
437     m[1][0] = mMatrix[1][0];
438     m[1][1] = mMatrix[1][1];
439     m[1][2] = 0.f;
440     m[1][3] = mMatrix[1][2];
441 
442     m[2][0] = 0.f;
443     m[2][1] = 0.f;
444     m[2][2] = 1.f;
445     m[2][3] = 0.f;
446 
447     m[3][0] = mMatrix[2][0];
448     m[3][1] = mMatrix[2][1];
449     m[3][2] = 0.f;
450     m[3][3] = mMatrix[2][2];
451 
452     return m;
453 }
454 
rotationToString(const uint32_t rotationFlags)455 static std::string rotationToString(const uint32_t rotationFlags) {
456     switch (rotationFlags) {
457         case Transform::ROT_0:
458             return "ROT_0";
459         case Transform::FLIP_H:
460             return "FLIP_H";
461         case Transform::FLIP_V:
462             return "FLIP_V";
463         case Transform::ROT_90:
464             return "ROT_90";
465         case Transform::ROT_180:
466             return "ROT_180";
467         case Transform::ROT_270:
468             return "ROT_270";
469         case Transform::ROT_INVALID:
470         default:
471             return "ROT_INVALID";
472     }
473 }
474 
transformToString(const uint32_t transform)475 static std::string transformToString(const uint32_t transform) {
476     if (transform == Transform::IDENTITY) {
477         return "IDENTITY";
478     }
479 
480     if (transform == Transform::UNKNOWN) {
481         return "UNKNOWN";
482     }
483 
484     std::string out;
485     if (transform & Transform::SCALE) out.append("SCALE ");
486     if (transform & Transform::ROTATE) out.append("ROTATE ");
487     if (transform & Transform::TRANSLATE) out.append("TRANSLATE");
488     return out;
489 }
490 
dump(std::string & out,const char * name,const char * prefix) const491 void Transform::dump(std::string& out, const char* name, const char* prefix) const {
492     using android::base::StringAppendF;
493 
494     type(); // Ensure the information in mType is up to date
495 
496     const uint32_t type = mType;
497     const uint32_t orient = type >> 8;
498 
499     out += prefix;
500     out += name;
501     out += " ";
502 
503     if (orient & ROT_INVALID) {
504         StringAppendF(&out, "0x%08x ", orient);
505     }
506     out += "(" + rotationToString(orient) + ") ";
507 
508     if (type & UNKNOWN) {
509         StringAppendF(&out, "0x%02x ", type);
510     }
511     out += "(" + transformToString(type) + ")\n";
512 
513     if (type == IDENTITY) {
514         return;
515     }
516 
517     for (size_t i = 0; i < 3; i++) {
518         StringAppendF(&out, "%s    %.4f  %.4f  %.4f\n", prefix, static_cast<double>(mMatrix[0][i]),
519                       static_cast<double>(mMatrix[1][i]), static_cast<double>(mMatrix[2][i]));
520     }
521 }
522 
dump(const char * name,const char * prefix) const523 void Transform::dump(const char* name, const char* prefix) const {
524     std::string out;
525     dump(out, name, prefix);
526     ALOGD("%s", out.c_str());
527 }
528 
529 } // namespace android::ui
530