blob: 42dd85e95981ecdc4159b25373e9b9c8a0d3afa1 [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
chaviw44a6d2b2020-09-08 17:14:16 -070017#undef LOG_TAG
18#define LOG_TAG "Transform"
19
Mathias Agopianeda65402010-02-22 03:15:57 -080020#include <math.h>
21
Lloyd Pique32cbe282018-10-19 13:09:22 -070022#include <android-base/stringprintf.h>
Mathias Agopianeda65402010-02-22 03:15:57 -080023#include <cutils/compiler.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024#include <ui/Region.h>
Peiyong Linefefaac2018-08-17 12:27:51 -070025#include <ui/Transform.h>
26#include <utils/String8.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
Marin Shalamanov043ba292020-09-10 13:35:20 +020028namespace android::ui {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029
Mathias Agopianeda65402010-02-22 03:15:57 -080030Transform::Transform() {
31 reset();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032}
33
34Transform::Transform(const Transform& other)
Mathias Agopianeda65402010-02-22 03:15:57 -080035 : mMatrix(other.mMatrix), mType(other.mType) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036}
37
Lloyd Piqueb62cebc2019-11-20 18:31:52 -080038Transform::Transform(uint32_t orientation, int w, int h) {
39 set(orientation, w, h);
Chih-Chung Chang52e72002010-01-21 17:31:06 -080040}
41
Peiyong Linefefaac2018-08-17 12:27:51 -070042Transform::~Transform() = default;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043
Mathias Agopiand1296592010-03-09 19:17:47 -080044static const float EPSILON = 0.0f;
Mathias Agopianeda65402010-02-22 03:15:57 -080045
46bool Transform::isZero(float f) {
Mathias Agopiand1296592010-03-09 19:17:47 -080047 return fabs(f) <= EPSILON;
Mathias Agopianeda65402010-02-22 03:15:57 -080048}
49
Mathias Agopiand1296592010-03-09 19:17:47 -080050bool Transform::absIsOne(float f) {
51 return isZero(fabs(f) - 1.0f);
Mathias Agopianeda65402010-02-22 03:15:57 -080052}
53
Lloyd Piqueea629282019-12-03 15:57:10 -080054bool 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];
Lloyd Piqueea629282019-12-03 15:57:10 -080060}
61
Marin Shalamanov043ba292020-09-10 13:35:20 +020062Transform Transform::operator*(const Transform& rhs) const {
Mathias Agopianeda65402010-02-22 03:15:57 -080063 if (CC_LIKELY(mType == IDENTITY))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064 return rhs;
65
66 Transform r(*this);
Mathias Agopianeda65402010-02-22 03:15:57 -080067 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);
Peiyong Linefefaac2018-08-17 12:27:51 -070074 for (size_t i = 0; i < 3; i++) {
Mathias Agopianeda65402010-02-22 03:15:57 -080075 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 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082 r.mType |= rhs.mType;
Mathias Agopianeda65402010-02-22 03:15:57 -080083
84 // TODO: we could recompute this value from r and rhs
85 r.mType &= 0xFF;
86 r.mType |= UNKNOWN_TYPE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 return r;
88}
89
chaviwc01e1372020-07-01 12:37:31 -070090Transform 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
Peiyong Linefefaac2018-08-17 12:27:51 -0700103Transform& Transform::operator=(const Transform& other) {
104 mMatrix = other.mMatrix;
105 mType = other.mType;
106 return *this;
107}
108
Mathias Agopianff2ed702013-09-01 21:36:12 -0700109const vec3& Transform::operator [] (size_t i) const {
110 return mMatrix[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111}
112
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700113float Transform::tx() const {
114 return mMatrix[2][0];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115}
116
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700117float Transform::ty() const {
118 return mMatrix[2][1];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119}
120
chaviwc01e1372020-07-01 12:37:31 -0700121float Transform::dsdx() const {
Robert Carre07e1032018-11-26 12:55:53 -0800122 return mMatrix[0][0];
123}
124
chaviwc01e1372020-07-01 12:37:31 -0700125float Transform::dtdx() const {
126 return mMatrix[1][0];
127}
128
129float Transform::dtdy() const {
130 return mMatrix[0][1];
131}
132
133float Transform::dsdy() const {
Robert Carre07e1032018-11-26 12:55:53 -0800134 return mMatrix[1][1];
135}
136
Garfield Tan2c1782c2022-02-16 15:25:05 -0800137float Transform::det() const {
138 return mMatrix[0][0] * mMatrix[1][1] - mMatrix[0][1] * mMatrix[1][0];
139}
140
chaviw1ff3d1e2020-07-01 15:53:47 -0700141float Transform::getScaleX() const {
chaviw44a6d2b2020-09-08 17:14:16 -0700142 return sqrt((dsdx() * dsdx()) + (dtdx() * dtdx()));
chaviw1ff3d1e2020-07-01 15:53:47 -0700143}
144
145float Transform::getScaleY() const {
146 return sqrt((dtdy() * dtdy()) + (dsdy() * dsdy()));
147}
148
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149void Transform::reset() {
Mathias Agopianeda65402010-02-22 03:15:57 -0800150 mType = IDENTITY;
Peiyong Linefefaac2018-08-17 12:27:51 -0700151 for(size_t i = 0; i < 3; i++) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800152 vec3& v(mMatrix[i]);
Peiyong Linefefaac2018-08-17 12:27:51 -0700153 for (size_t j = 0; j < 3; j++)
154 v[j] = ((i == j) ? 1.0f : 0.0f);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155 }
156}
157
Marin Shalamanov043ba292020-09-10 13:35:20 +0200158void Transform::set(float tx, float ty) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800159 mMatrix[2][0] = tx;
160 mMatrix[2][1] = ty;
161 mMatrix[2][2] = 1.0f;
162
163 if (isZero(tx) && isZero(ty)) {
164 mType &= ~TRANSLATE;
165 } else {
166 mType |= TRANSLATE;
167 }
168}
169
Marin Shalamanov043ba292020-09-10 13:35:20 +0200170void Transform::set(float a, float b, float c, float d) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800171 mat33& M(mMatrix);
172 M[0][0] = a; M[1][0] = b;
173 M[0][1] = c; M[1][1] = d;
174 M[0][2] = 0; M[1][2] = 0;
175 mType = UNKNOWN_TYPE;
176}
177
Marin Shalamanov043ba292020-09-10 13:35:20 +0200178status_t Transform::set(uint32_t flags, float w, float h) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800179 if (flags & ROT_INVALID) {
180 // that's not allowed!
181 reset();
182 return BAD_VALUE;
183 }
184
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700185 Transform H, V, R;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700186 if (flags & ROT_90) {
187 // w & h are inverted when rotating by 90 degrees
Peiyong Lin3db42342018-08-16 09:15:59 -0700188 std::swap(w, h);
Mathias Agopian883dffa2010-10-25 18:29:35 -0700189 }
190
Mathias Agopianeda65402010-02-22 03:15:57 -0800191 if (flags & FLIP_H) {
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700192 H.mType = (FLIP_H << 8) | SCALE;
193 H.mType |= isZero(w) ? IDENTITY : TRANSLATE;
194 mat33& M(H.mMatrix);
195 M[0][0] = -1;
196 M[2][0] = w;
Mathias Agopianeda65402010-02-22 03:15:57 -0800197 }
198
199 if (flags & FLIP_V) {
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700200 V.mType = (FLIP_V << 8) | SCALE;
201 V.mType |= isZero(h) ? IDENTITY : TRANSLATE;
202 mat33& M(V.mMatrix);
203 M[1][1] = -1;
204 M[2][1] = h;
Mathias Agopianeda65402010-02-22 03:15:57 -0800205 }
206
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700207 if (flags & ROT_90) {
Mathias Agopian883dffa2010-10-25 18:29:35 -0700208 const float original_w = h;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700209 R.mType = (ROT_90 << 8) | ROTATE;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700210 R.mType |= isZero(original_w) ? IDENTITY : TRANSLATE;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700211 mat33& M(R.mMatrix);
Mathias Agopian883dffa2010-10-25 18:29:35 -0700212 M[0][0] = 0; M[1][0] =-1; M[2][0] = original_w;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700213 M[0][1] = 1; M[1][1] = 0;
Mathias Agopianeda65402010-02-22 03:15:57 -0800214 }
215
Mathias Agopian883dffa2010-10-25 18:29:35 -0700216 *this = (R*(H*V));
Mathias Agopiand1296592010-03-09 19:17:47 -0800217 return NO_ERROR;
Mathias Agopianeda65402010-02-22 03:15:57 -0800218}
219
chaviwc01e1372020-07-01 12:37:31 -0700220void Transform::set(const std::array<float, 9>& matrix) {
221 mat33& M(mMatrix);
222 M[0][0] = matrix[0]; M[1][0] = matrix[1]; M[2][0] = matrix[2];
223 M[0][1] = matrix[3]; M[1][1] = matrix[4]; M[2][1] = matrix[5];
224 M[0][2] = matrix[6]; M[1][2] = matrix[7]; M[2][2] = matrix[8];
225 mType = UNKNOWN_TYPE;
226 type();
227}
228
Mathias Agopianff2ed702013-09-01 21:36:12 -0700229vec2 Transform::transform(const vec2& v) const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800230 vec2 r;
231 const mat33& M(mMatrix);
232 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0];
233 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1];
234 return r;
235}
236
Mathias Agopianff2ed702013-09-01 21:36:12 -0700237vec3 Transform::transform(const vec3& v) const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800238 vec3 r;
239 const mat33& M(mMatrix);
240 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0]*v[2];
241 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1]*v[2];
242 r[2] = M[0][2]*v[0] + M[1][2]*v[1] + M[2][2]*v[2];
243 return r;
244}
245
chaviwc01e1372020-07-01 12:37:31 -0700246vec2 Transform::transform(float x, float y) const {
247 return transform(vec2(x, y));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248}
249
Marin Shalamanov043ba292020-09-10 13:35:20 +0200250Rect Transform::makeBounds(int w, int h) const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800251 return transform( Rect(w, h) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252}
253
Marin Shalamanov043ba292020-09-10 13:35:20 +0200254Rect Transform::transform(const Rect& bounds, bool roundOutwards) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255 Rect r;
Mathias Agopianeda65402010-02-22 03:15:57 -0800256 vec2 lt( bounds.left, bounds.top );
257 vec2 rt( bounds.right, bounds.top );
258 vec2 lb( bounds.left, bounds.bottom );
259 vec2 rb( bounds.right, bounds.bottom );
260
261 lt = transform(lt);
262 rt = transform(rt);
263 lb = transform(lb);
264 rb = transform(rb);
265
Pablo Ceballos51450032016-08-03 10:20:45 -0700266 if (roundOutwards) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700267 r.left = static_cast<int32_t>(floorf(std::min({lt[0], rt[0], lb[0], rb[0]})));
268 r.top = static_cast<int32_t>(floorf(std::min({lt[1], rt[1], lb[1], rb[1]})));
269 r.right = static_cast<int32_t>(ceilf(std::max({lt[0], rt[0], lb[0], rb[0]})));
270 r.bottom = static_cast<int32_t>(ceilf(std::max({lt[1], rt[1], lb[1], rb[1]})));
Pablo Ceballos51450032016-08-03 10:20:45 -0700271 } else {
Peiyong Linefefaac2018-08-17 12:27:51 -0700272 r.left = static_cast<int32_t>(floorf(std::min({lt[0], rt[0], lb[0], rb[0]}) + 0.5f));
273 r.top = static_cast<int32_t>(floorf(std::min({lt[1], rt[1], lb[1], rb[1]}) + 0.5f));
274 r.right = static_cast<int32_t>(floorf(std::max({lt[0], rt[0], lb[0], rb[0]}) + 0.5f));
275 r.bottom = static_cast<int32_t>(floorf(std::max({lt[1], rt[1], lb[1], rb[1]}) + 0.5f));
Pablo Ceballos51450032016-08-03 10:20:45 -0700276 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800277
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278 return r;
279}
280
Marin Shalamanov043ba292020-09-10 13:35:20 +0200281FloatRect Transform::transform(const FloatRect& bounds) const {
Dan Stoza80d61162017-12-20 15:57:52 -0800282 vec2 lt(bounds.left, bounds.top);
283 vec2 rt(bounds.right, bounds.top);
284 vec2 lb(bounds.left, bounds.bottom);
285 vec2 rb(bounds.right, bounds.bottom);
286
287 lt = transform(lt);
288 rt = transform(rt);
289 lb = transform(lb);
290 rb = transform(rb);
291
292 FloatRect r;
Peiyong Lin3db42342018-08-16 09:15:59 -0700293 r.left = std::min({lt[0], rt[0], lb[0], rb[0]});
294 r.top = std::min({lt[1], rt[1], lb[1], rb[1]});
295 r.right = std::max({lt[0], rt[0], lb[0], rb[0]});
296 r.bottom = std::max({lt[1], rt[1], lb[1], rb[1]});
Dan Stoza80d61162017-12-20 15:57:52 -0800297
298 return r;
299}
300
Marin Shalamanov043ba292020-09-10 13:35:20 +0200301Region Transform::transform(const Region& reg) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302 Region out;
Dan Stoza22f7fc42016-05-10 16:19:53 -0700303 if (CC_UNLIKELY(type() > TRANSLATE)) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800304 if (CC_LIKELY(preserveRects())) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700305 Region::const_iterator it = reg.begin();
306 Region::const_iterator const end = reg.end();
307 while (it != end) {
308 out.orSelf(transform(*it++));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309 }
310 } else {
311 out.set(transform(reg.bounds()));
312 }
313 } else {
Peiyong Linefefaac2018-08-17 12:27:51 -0700314 int xpos = static_cast<int>(floorf(tx() + 0.5f));
315 int ypos = static_cast<int>(floorf(ty() + 0.5f));
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700316 out = reg.translate(xpos, ypos);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317 }
318 return out;
319}
320
Marin Shalamanov043ba292020-09-10 13:35:20 +0200321uint32_t Transform::type() const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800322 if (mType & UNKNOWN_TYPE) {
323 // recompute what this transform is
324
325 const mat33& M(mMatrix);
326 const float a = M[0][0];
327 const float b = M[1][0];
328 const float c = M[0][1];
329 const float d = M[1][1];
330 const float x = M[2][0];
331 const float y = M[2][1];
332
333 bool scale = false;
334 uint32_t flags = ROT_0;
335 if (isZero(b) && isZero(c)) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800336 if (a<0) flags |= FLIP_H;
337 if (d<0) flags |= FLIP_V;
338 if (!absIsOne(a) || !absIsOne(d)) {
339 scale = true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800340 }
341 } else if (isZero(a) && isZero(d)) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800342 flags |= ROT_90;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700343 if (b>0) flags |= FLIP_V;
344 if (c<0) flags |= FLIP_H;
Mathias Agopiand1296592010-03-09 19:17:47 -0800345 if (!absIsOne(b) || !absIsOne(c)) {
346 scale = true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800347 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800348 } else {
Mathias Agopian29a367b2011-07-12 14:51:45 -0700349 // there is a skew component and/or a non 90 degrees rotation
Mathias Agopianeda65402010-02-22 03:15:57 -0800350 flags = ROT_INVALID;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800351 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800352
353 mType = flags << 8;
354 if (flags & ROT_INVALID) {
355 mType |= UNKNOWN;
356 } else {
357 if ((flags & ROT_90) || ((flags & ROT_180) == ROT_180))
358 mType |= ROTATE;
359 if (flags & FLIP_H)
360 mType ^= SCALE;
361 if (flags & FLIP_V)
362 mType ^= SCALE;
363 if (scale)
364 mType |= SCALE;
365 }
366
367 if (!isZero(x) || !isZero(y))
368 mType |= TRANSLATE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800369 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800370 return mType;
371}
372
Mathias Agopian3da16722013-02-28 17:12:07 -0800373Transform Transform::inverse() const {
374 // our 3x3 matrix is always of the form of a 2x2 transformation
375 // followed by a translation: T*M, therefore:
376 // (T*M)^-1 = M^-1 * T^-1
377 Transform result;
378 if (mType <= TRANSLATE) {
Michael Lentine28ea2172014-11-19 18:32:37 -0800379 // 1 0 0
380 // 0 1 0
381 // x y 1
Mathias Agopian3da16722013-02-28 17:12:07 -0800382 result = *this;
383 result.mMatrix[2][0] = -result.mMatrix[2][0];
384 result.mMatrix[2][1] = -result.mMatrix[2][1];
385 } else {
Michael Lentine28ea2172014-11-19 18:32:37 -0800386 // a c 0
387 // b d 0
388 // x y 1
Mathias Agopian3da16722013-02-28 17:12:07 -0800389 const mat33& M(mMatrix);
390 const float a = M[0][0];
391 const float b = M[1][0];
392 const float c = M[0][1];
393 const float d = M[1][1];
394 const float x = M[2][0];
395 const float y = M[2][1];
396
Garfield Tan2c1782c2022-02-16 15:25:05 -0800397 const float idet = 1.0f / det();
Michael Lentine28ea2172014-11-19 18:32:37 -0800398 result.mMatrix[0][0] = d*idet;
399 result.mMatrix[0][1] = -c*idet;
400 result.mMatrix[1][0] = -b*idet;
401 result.mMatrix[1][1] = a*idet;
402 result.mType = mType;
Prabir Pradhan61b41732021-08-16 12:19:26 -0700403 if (getOrientation() & ROT_90) {
404 // Recalculate the type if there is a 90-degree rotation component, since the inverse
405 // of ROT_90 is ROT_270 and vice versa.
406 result.mType |= UNKNOWN_TYPE;
407 }
Mathias Agopian3da16722013-02-28 17:12:07 -0800408
Michael Lentine28ea2172014-11-19 18:32:37 -0800409 vec2 T(-x, -y);
410 T = result.transform(T);
411 result.mMatrix[2][0] = T[0];
412 result.mMatrix[2][1] = T[1];
Mathias Agopian3da16722013-02-28 17:12:07 -0800413 }
414 return result;
415}
416
Mathias Agopianeda65402010-02-22 03:15:57 -0800417uint32_t Transform::getType() const {
418 return type() & 0xFF;
419}
420
Marin Shalamanov043ba292020-09-10 13:35:20 +0200421uint32_t Transform::getOrientation() const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800422 return (type() >> 8) & 0xFF;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800423}
424
Marin Shalamanov043ba292020-09-10 13:35:20 +0200425bool Transform::preserveRects() const {
Mathias Agopianab7c13f2011-07-25 13:57:16 -0700426 return (getOrientation() & ROT_INVALID) ? false : true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800427}
428
Marin Shalamanov043ba292020-09-10 13:35:20 +0200429bool Transform::needsBilinearFiltering() const {
430 return (!preserveRects() || getType() >= ui::Transform::SCALE);
431}
432
Lloyd Pique7e06e7f2019-03-15 18:36:44 -0700433mat4 Transform::asMatrix4() const {
434 // Internally Transform uses a 3x3 matrix since the transform is meant for
435 // two-dimensional values. An equivalent 4x4 matrix means inserting an extra
436 // row and column which adds as an identity transform on the third
437 // dimension.
438
439 mat4 m = mat4{mat4::NO_INIT}; // NO_INIT since we explicitly set every element
440
441 m[0][0] = mMatrix[0][0];
442 m[0][1] = mMatrix[0][1];
443 m[0][2] = 0.f;
444 m[0][3] = mMatrix[0][2];
445
446 m[1][0] = mMatrix[1][0];
447 m[1][1] = mMatrix[1][1];
448 m[1][2] = 0.f;
449 m[1][3] = mMatrix[1][2];
450
451 m[2][0] = 0.f;
452 m[2][1] = 0.f;
453 m[2][2] = 1.f;
454 m[2][3] = 0.f;
455
456 m[3][0] = mMatrix[2][0];
457 m[3][1] = mMatrix[2][1];
458 m[3][2] = 0.f;
459 m[3][3] = mMatrix[2][2];
460
461 return m;
462}
463
chaviw85b44202020-07-24 11:46:21 -0700464static std::string rotationToString(const uint32_t rotationFlags) {
465 switch (rotationFlags) {
466 case Transform::ROT_0:
467 return "ROT_0";
468 case Transform::FLIP_H:
469 return "FLIP_H";
470 case Transform::FLIP_V:
471 return "FLIP_V";
472 case Transform::ROT_90:
473 return "ROT_90";
474 case Transform::ROT_180:
475 return "ROT_180";
476 case Transform::ROT_270:
477 return "ROT_270";
478 case Transform::ROT_INVALID:
479 default:
480 return "ROT_INVALID";
481 }
482}
483
484static std::string transformToString(const uint32_t transform) {
485 if (transform == Transform::IDENTITY) {
486 return "IDENTITY";
487 }
488
489 if (transform == Transform::UNKNOWN) {
490 return "UNKNOWN";
491 }
492
493 std::string out;
494 if (transform & Transform::SCALE) out.append("SCALE ");
495 if (transform & Transform::ROTATE) out.append("ROTATE ");
496 if (transform & Transform::TRANSLATE) out.append("TRANSLATE");
497 return out;
498}
499
500void Transform::dump(std::string& out, const char* name, const char* prefix) const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700501 using android::base::StringAppendF;
Mathias Agopianeda65402010-02-22 03:15:57 -0800502
Lloyd Pique32cbe282018-10-19 13:09:22 -0700503 type(); // Ensure the information in mType is up to date
Mathias Agopianeda65402010-02-22 03:15:57 -0800504
Lloyd Pique32cbe282018-10-19 13:09:22 -0700505 const uint32_t type = mType;
506 const uint32_t orient = type >> 8;
507
chaviw85b44202020-07-24 11:46:21 -0700508 out += prefix;
509 out += name;
510 out += " ";
Lloyd Pique32cbe282018-10-19 13:09:22 -0700511
512 if (orient & ROT_INVALID) {
chaviw85b44202020-07-24 11:46:21 -0700513 StringAppendF(&out, "0x%08x ", orient);
Mathias Agopiand1296592010-03-09 19:17:47 -0800514 }
chaviw85b44202020-07-24 11:46:21 -0700515 out += "(" + rotationToString(orient) + ") ";
Mathias Agopianeda65402010-02-22 03:15:57 -0800516
chaviw85b44202020-07-24 11:46:21 -0700517 if (type & UNKNOWN) {
518 StringAppendF(&out, "0x%02x ", type);
519 }
520 out += "(" + transformToString(type) + ")\n";
Lloyd Pique32cbe282018-10-19 13:09:22 -0700521
Vishnu Naird0929bc2020-08-27 17:22:58 -0700522 if (type == IDENTITY) {
523 return;
524 }
525
Lloyd Pique32cbe282018-10-19 13:09:22 -0700526 for (size_t i = 0; i < 3; i++) {
chaviw85b44202020-07-24 11:46:21 -0700527 StringAppendF(&out, "%s %.4f %.4f %.4f\n", prefix, static_cast<double>(mMatrix[0][i]),
Lloyd Pique32cbe282018-10-19 13:09:22 -0700528 static_cast<double>(mMatrix[1][i]), static_cast<double>(mMatrix[2][i]));
529 }
530}
531
chaviw85b44202020-07-24 11:46:21 -0700532void Transform::dump(const char* name, const char* prefix) const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700533 std::string out;
chaviw85b44202020-07-24 11:46:21 -0700534 dump(out, name, prefix);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700535 ALOGD("%s", out.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800536}
537
Marin Shalamanov043ba292020-09-10 13:35:20 +0200538} // namespace android::ui