xref: /aosp_15_r20/frameworks/native/libs/ui/Transform.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
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 
113 // x translate
tx() const114 float Transform::tx() const {
115     return mMatrix[2][0];
116 }
117 
118 // y translate
ty() const119 float Transform::ty() const {
120     return mMatrix[2][1];
121 }
122 
dsdx() const123 float Transform::dsdx() const {
124     return mMatrix[0][0];
125 }
126 
dtdx() const127 float Transform::dtdx() const {
128     return mMatrix[1][0];
129 }
130 
dtdy() const131 float Transform::dtdy() const {
132     return mMatrix[0][1];
133 }
134 
dsdy() const135 float Transform::dsdy() const {
136     return mMatrix[1][1];
137 }
138 
det() const139 float Transform::det() const {
140     return mMatrix[0][0] * mMatrix[1][1] - mMatrix[0][1] * mMatrix[1][0];
141 }
142 
getScaleX() const143 float Transform::getScaleX() const {
144     return sqrt((dsdx() * dsdx()) + (dtdx() * dtdx()));
145 }
146 
getScaleY() const147 float Transform::getScaleY() const {
148     return sqrt((dtdy() * dtdy()) + (dsdy() * dsdy()));
149 }
150 
reset()151 void Transform::reset() {
152     mType = IDENTITY;
153     for(size_t i = 0; i < 3; i++) {
154         vec3& v(mMatrix[i]);
155         for (size_t j = 0; j < 3; j++)
156             v[j] = ((i == j) ? 1.0f : 0.0f);
157     }
158 }
159 
set(float tx,float ty)160 void Transform::set(float tx, float ty) {
161     mMatrix[2][0] = tx;
162     mMatrix[2][1] = ty;
163     mMatrix[2][2] = 1.0f;
164 
165     if (isZero(tx) && isZero(ty)) {
166         mType &= ~TRANSLATE;
167     } else {
168         mType |= TRANSLATE;
169     }
170 }
171 
172 // x and y are the coordinates in the destination (i.e. the screen)
173 // s and t are the coordinates in the source (i.e. the texture)
174 // d means derivative
175 // dsdx means ds/dx derivative of s with respect to x, etc.
set(float dsdx,float dtdy,float dtdx,float dsdy)176 void Transform::set(float dsdx, float dtdy, float dtdx, float dsdy) {
177     mat33& M(mMatrix);
178     M[0][0] = dsdx;    M[1][0] = dtdy;
179     M[0][1] = dtdx;    M[1][1] = dsdy;
180     M[0][2] = 0;       M[1][2] = 0;
181     mType = UNKNOWN_TYPE;
182 }
183 
set(uint32_t flags,float w,float h)184 status_t Transform::set(uint32_t flags, float w, float h) {
185     if (flags & ROT_INVALID) {
186         // that's not allowed!
187         reset();
188         return BAD_VALUE;
189     }
190 
191     Transform H, V, R;
192     if (flags & ROT_90) {
193         // w & h are inverted when rotating by 90 degrees
194         std::swap(w, h);
195     }
196 
197     if (flags & FLIP_H) {
198         H.mType = (FLIP_H << 8) | SCALE;
199         H.mType |= isZero(w) ? IDENTITY : TRANSLATE;
200         mat33& M(H.mMatrix);
201         M[0][0] = -1;
202         M[2][0] = w;
203     }
204 
205     if (flags & FLIP_V) {
206         V.mType = (FLIP_V << 8) | SCALE;
207         V.mType |= isZero(h) ? IDENTITY : TRANSLATE;
208         mat33& M(V.mMatrix);
209         M[1][1] = -1;
210         M[2][1] = h;
211     }
212 
213     if (flags & ROT_90) {
214         const float original_w = h;
215         R.mType = (ROT_90 << 8) | ROTATE;
216         R.mType |= isZero(original_w) ? IDENTITY : TRANSLATE;
217         mat33& M(R.mMatrix);
218         M[0][0] = 0;    M[1][0] =-1;    M[2][0] = original_w;
219         M[0][1] = 1;    M[1][1] = 0;
220     }
221 
222     *this = (R*(H*V));
223     return NO_ERROR;
224 }
225 
set(const std::array<float,9> & matrix)226 void Transform::set(const std::array<float, 9>& matrix) {
227     mat33& M(mMatrix);
228     M[0][0] = matrix[0];  M[1][0] = matrix[1];  M[2][0] = matrix[2];
229     M[0][1] = matrix[3];  M[1][1] = matrix[4];  M[2][1] = matrix[5];
230     M[0][2] = matrix[6];  M[1][2] = matrix[7];  M[2][2] = matrix[8];
231     mType = UNKNOWN_TYPE;
232     type();
233 }
234 
transform(const vec2 & v) const235 vec2 Transform::transform(const vec2& v) const {
236     vec2 r;
237     const mat33& M(mMatrix);
238     r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0];
239     r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1];
240     return r;
241 }
242 
transform(const vec3 & v) const243 vec3 Transform::transform(const vec3& v) const {
244     vec3 r;
245     const mat33& M(mMatrix);
246     r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0]*v[2];
247     r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1]*v[2];
248     r[2] = M[0][2]*v[0] + M[1][2]*v[1] + M[2][2]*v[2];
249     return r;
250 }
251 
transform(float x,float y) const252 vec2 Transform::transform(float x, float y) const {
253     return transform(vec2(x, y));
254 }
255 
makeBounds(int w,int h) const256 Rect Transform::makeBounds(int w, int h) const {
257     return transform( Rect(w, h) );
258 }
259 
transform(const Rect & bounds,bool roundOutwards) const260 Rect Transform::transform(const Rect& bounds, bool roundOutwards) const {
261     Rect r;
262     vec2 lt( bounds.left,  bounds.top    );
263     vec2 rt( bounds.right, bounds.top    );
264     vec2 lb( bounds.left,  bounds.bottom );
265     vec2 rb( bounds.right, bounds.bottom );
266 
267     lt = transform(lt);
268     rt = transform(rt);
269     lb = transform(lb);
270     rb = transform(rb);
271 
272     if (roundOutwards) {
273         r.left   = static_cast<int32_t>(floorf(std::min({lt[0], rt[0], lb[0], rb[0]})));
274         r.top    = static_cast<int32_t>(floorf(std::min({lt[1], rt[1], lb[1], rb[1]})));
275         r.right  = static_cast<int32_t>(ceilf(std::max({lt[0], rt[0], lb[0], rb[0]})));
276         r.bottom = static_cast<int32_t>(ceilf(std::max({lt[1], rt[1], lb[1], rb[1]})));
277     } else {
278         r.left   = static_cast<int32_t>(floorf(std::min({lt[0], rt[0], lb[0], rb[0]}) + 0.5f));
279         r.top    = static_cast<int32_t>(floorf(std::min({lt[1], rt[1], lb[1], rb[1]}) + 0.5f));
280         r.right  = static_cast<int32_t>(floorf(std::max({lt[0], rt[0], lb[0], rb[0]}) + 0.5f));
281         r.bottom = static_cast<int32_t>(floorf(std::max({lt[1], rt[1], lb[1], rb[1]}) + 0.5f));
282     }
283 
284     return r;
285 }
286 
transform(const FloatRect & bounds) const287 FloatRect Transform::transform(const FloatRect& bounds) const {
288     vec2 lt(bounds.left, bounds.top);
289     vec2 rt(bounds.right, bounds.top);
290     vec2 lb(bounds.left, bounds.bottom);
291     vec2 rb(bounds.right, bounds.bottom);
292 
293     lt = transform(lt);
294     rt = transform(rt);
295     lb = transform(lb);
296     rb = transform(rb);
297 
298     FloatRect r;
299     r.left = std::min({lt[0], rt[0], lb[0], rb[0]});
300     r.top = std::min({lt[1], rt[1], lb[1], rb[1]});
301     r.right = std::max({lt[0], rt[0], lb[0], rb[0]});
302     r.bottom = std::max({lt[1], rt[1], lb[1], rb[1]});
303 
304     return r;
305 }
306 
transform(const Region & reg) const307 Region Transform::transform(const Region& reg) const {
308     Region out;
309     if (CC_UNLIKELY(type() > TRANSLATE)) {
310         if (CC_LIKELY(preserveRects())) {
311             Region::const_iterator it = reg.begin();
312             Region::const_iterator const end = reg.end();
313             while (it != end) {
314                 out.orSelf(transform(*it++));
315             }
316         } else {
317             out.set(transform(reg.bounds()));
318         }
319     } else {
320         int xpos = static_cast<int>(floorf(tx() + 0.5f));
321         int ypos = static_cast<int>(floorf(ty() + 0.5f));
322         out = reg.translate(xpos, ypos);
323     }
324     return out;
325 }
326 
type() const327 uint32_t Transform::type() const {
328     if (mType & UNKNOWN_TYPE) {
329         // recompute what this transform is
330 
331         const mat33& M(mMatrix);
332         const float a = M[0][0];
333         const float b = M[1][0];
334         const float c = M[0][1];
335         const float d = M[1][1];
336         const float x = M[2][0];
337         const float y = M[2][1];
338 
339         bool scale = false;
340         uint32_t flags = ROT_0;
341         if (isZero(b) && isZero(c)) {
342             if (a<0)    flags |= FLIP_H;
343             if (d<0)    flags |= FLIP_V;
344             if (!absIsOne(a) || !absIsOne(d)) {
345                 scale = true;
346             }
347         } else if (isZero(a) && isZero(d)) {
348             flags |= ROT_90;
349             if (b>0)    flags |= FLIP_V;
350             if (c<0)    flags |= FLIP_H;
351             if (!absIsOne(b) || !absIsOne(c)) {
352                 scale = true;
353             }
354         } else {
355             // there is a skew component and/or a non 90 degrees rotation
356             flags = ROT_INVALID;
357         }
358 
359         mType = flags << 8;
360         if (flags & ROT_INVALID) {
361             mType |= UNKNOWN;
362         } else {
363             if ((flags & ROT_90) || ((flags & ROT_180) == ROT_180))
364                 mType |= ROTATE;
365             if (flags & FLIP_H)
366                 mType ^= SCALE;
367             if (flags & FLIP_V)
368                 mType ^= SCALE;
369             if (scale)
370                 mType |= SCALE;
371         }
372 
373         if (!isZero(x) || !isZero(y))
374             mType |= TRANSLATE;
375     }
376     return mType;
377 }
378 
inverse() const379 Transform Transform::inverse() const {
380     // our 3x3 matrix is always of the form of a 2x2 transformation
381     // followed by a translation: T*M, therefore:
382     // (T*M)^-1 = M^-1 * T^-1
383     Transform result;
384     if (mType <= TRANSLATE) {
385         // 1 0 0
386         // 0 1 0
387         // x y 1
388         result = *this;
389         result.mMatrix[2][0] = -result.mMatrix[2][0];
390         result.mMatrix[2][1] = -result.mMatrix[2][1];
391     } else {
392         // a c 0
393         // b d 0
394         // x y 1
395         const mat33& M(mMatrix);
396         const float a = M[0][0];
397         const float b = M[1][0];
398         const float c = M[0][1];
399         const float d = M[1][1];
400         const float x = M[2][0];
401         const float y = M[2][1];
402 
403         const float idet = 1.0f / det();
404         result.mMatrix[0][0] =  d*idet;
405         result.mMatrix[0][1] = -c*idet;
406         result.mMatrix[1][0] = -b*idet;
407         result.mMatrix[1][1] =  a*idet;
408         result.mType = mType;
409         if (getOrientation() & ROT_90) {
410             // Recalculate the type if there is a 90-degree rotation component, since the inverse
411             // of ROT_90 is ROT_270 and vice versa.
412             result.mType |= UNKNOWN_TYPE;
413         }
414 
415         vec2 T(-x, -y);
416         T = result.transform(T);
417         result.mMatrix[2][0] = T[0];
418         result.mMatrix[2][1] = T[1];
419     }
420     return result;
421 }
422 
getType() const423 uint32_t Transform::getType() const {
424     return type() & 0xFF;
425 }
426 
getOrientation() const427 uint32_t Transform::getOrientation() const {
428     return (type() >> 8) & 0xFF;
429 }
430 
preserveRects() const431 bool Transform::preserveRects() const {
432     return (getOrientation() & ROT_INVALID) ? false : true;
433 }
434 
needsBilinearFiltering() const435 bool Transform::needsBilinearFiltering() const {
436     return (!preserveRects() || getType() >= ui::Transform::SCALE);
437 }
438 
asMatrix4() const439 mat4 Transform::asMatrix4() const {
440     // Internally Transform uses a 3x3 matrix since the transform is meant for
441     // two-dimensional values. An equivalent 4x4 matrix means inserting an extra
442     // row and column which adds as an identity transform on the third
443     // dimension.
444 
445     mat4 m = mat4{mat4::NO_INIT}; // NO_INIT since we explicitly set every element
446 
447     m[0][0] = mMatrix[0][0];
448     m[0][1] = mMatrix[0][1];
449     m[0][2] = 0.f;
450     m[0][3] = mMatrix[0][2];
451 
452     m[1][0] = mMatrix[1][0];
453     m[1][1] = mMatrix[1][1];
454     m[1][2] = 0.f;
455     m[1][3] = mMatrix[1][2];
456 
457     m[2][0] = 0.f;
458     m[2][1] = 0.f;
459     m[2][2] = 1.f;
460     m[2][3] = 0.f;
461 
462     m[3][0] = mMatrix[2][0];
463     m[3][1] = mMatrix[2][1];
464     m[3][2] = 0.f;
465     m[3][3] = mMatrix[2][2];
466 
467     return m;
468 }
469 
rotationToString(const uint32_t rotationFlags)470 static std::string rotationToString(const uint32_t rotationFlags) {
471     switch (rotationFlags) {
472         case Transform::ROT_0:
473             return "ROT_0";
474         case Transform::FLIP_H:
475             return "FLIP_H";
476         case Transform::FLIP_V:
477             return "FLIP_V";
478         case Transform::ROT_90:
479             return "ROT_90";
480         case Transform::ROT_180:
481             return "ROT_180";
482         case Transform::ROT_270:
483             return "ROT_270";
484         case Transform::ROT_INVALID:
485         default:
486             return "ROT_INVALID";
487     }
488 }
489 
transformToString(const uint32_t transform)490 static std::string transformToString(const uint32_t transform) {
491     if (transform == Transform::IDENTITY) {
492         return "IDENTITY";
493     }
494 
495     if (transform == Transform::UNKNOWN) {
496         return "UNKNOWN";
497     }
498 
499     std::string out;
500     if (transform & Transform::SCALE) out.append("SCALE ");
501     if (transform & Transform::ROTATE) out.append("ROTATE ");
502     if (transform & Transform::TRANSLATE) out.append("TRANSLATE");
503     return out;
504 }
505 
dump(std::string & out,const char * name,const char * prefix) const506 void Transform::dump(std::string& out, const char* name, const char* prefix) const {
507     using android::base::StringAppendF;
508 
509     type(); // Ensure the information in mType is up to date
510 
511     const uint32_t type = mType;
512     const uint32_t orient = type >> 8;
513 
514     out += prefix;
515     out += name;
516     out += " ";
517 
518     if (orient & ROT_INVALID) {
519         StringAppendF(&out, "0x%08x ", orient);
520     }
521     out += "(" + rotationToString(orient) + ") ";
522 
523     if (type & UNKNOWN) {
524         StringAppendF(&out, "0x%02x ", type);
525     }
526     out += "(" + transformToString(type) + ")\n";
527 
528     if (type == IDENTITY) {
529         return;
530     }
531 
532     for (size_t i = 0; i < 3; i++) {
533         StringAppendF(&out, "%s    %.4f  %.4f  %.4f\n", prefix, static_cast<double>(mMatrix[0][i]),
534                       static_cast<double>(mMatrix[1][i]), static_cast<double>(mMatrix[2][i]));
535     }
536 }
537 
dump(const char * name,const char * prefix) const538 void Transform::dump(const char* name, const char* prefix) const {
539     std::string out;
540     dump(out, name, prefix);
541     ALOGD("%s", out.c_str());
542 }
543 
544 } // namespace android::ui
545