blob: 23249fa4a9529ad9611858e34d2359e7eaa0ca0b [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
Sally Qi72dfcdf2024-07-10 20:32:58 +0000113// x translate
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700114float Transform::tx() const {
115 return mMatrix[2][0];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116}
117
Sally Qi72dfcdf2024-07-10 20:32:58 +0000118// y translate
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700119float Transform::ty() const {
120 return mMatrix[2][1];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121}
122
chaviwc01e1372020-07-01 12:37:31 -0700123float Transform::dsdx() const {
Robert Carre07e1032018-11-26 12:55:53 -0800124 return mMatrix[0][0];
125}
126
chaviwc01e1372020-07-01 12:37:31 -0700127float Transform::dtdx() const {
128 return mMatrix[1][0];
129}
130
131float Transform::dtdy() const {
132 return mMatrix[0][1];
133}
134
135float Transform::dsdy() const {
Robert Carre07e1032018-11-26 12:55:53 -0800136 return mMatrix[1][1];
137}
138
Garfield Tan2c1782c2022-02-16 15:25:05 -0800139float Transform::det() const {
140 return mMatrix[0][0] * mMatrix[1][1] - mMatrix[0][1] * mMatrix[1][0];
141}
142
chaviw1ff3d1e2020-07-01 15:53:47 -0700143float Transform::getScaleX() const {
chaviw44a6d2b2020-09-08 17:14:16 -0700144 return sqrt((dsdx() * dsdx()) + (dtdx() * dtdx()));
chaviw1ff3d1e2020-07-01 15:53:47 -0700145}
146
147float Transform::getScaleY() const {
148 return sqrt((dtdy() * dtdy()) + (dsdy() * dsdy()));
149}
150
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151void Transform::reset() {
Mathias Agopianeda65402010-02-22 03:15:57 -0800152 mType = IDENTITY;
Peiyong Linefefaac2018-08-17 12:27:51 -0700153 for(size_t i = 0; i < 3; i++) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800154 vec3& v(mMatrix[i]);
Peiyong Linefefaac2018-08-17 12:27:51 -0700155 for (size_t j = 0; j < 3; j++)
156 v[j] = ((i == j) ? 1.0f : 0.0f);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157 }
158}
159
Marin Shalamanov043ba292020-09-10 13:35:20 +0200160void Transform::set(float tx, float ty) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800161 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
Sally Qi72dfcdf2024-07-10 20:32:58 +0000172// 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.
176void Transform::set(float dsdx, float dtdy, float dtdx, float dsdy) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800177 mat33& M(mMatrix);
Sally Qi72dfcdf2024-07-10 20:32:58 +0000178 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;
Mathias Agopianeda65402010-02-22 03:15:57 -0800181 mType = UNKNOWN_TYPE;
182}
183
Marin Shalamanov043ba292020-09-10 13:35:20 +0200184status_t Transform::set(uint32_t flags, float w, float h) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800185 if (flags & ROT_INVALID) {
186 // that's not allowed!
187 reset();
188 return BAD_VALUE;
189 }
190
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700191 Transform H, V, R;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700192 if (flags & ROT_90) {
193 // w & h are inverted when rotating by 90 degrees
Peiyong Lin3db42342018-08-16 09:15:59 -0700194 std::swap(w, h);
Mathias Agopian883dffa2010-10-25 18:29:35 -0700195 }
196
Mathias Agopianeda65402010-02-22 03:15:57 -0800197 if (flags & FLIP_H) {
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700198 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;
Mathias Agopianeda65402010-02-22 03:15:57 -0800203 }
204
205 if (flags & FLIP_V) {
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700206 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;
Mathias Agopianeda65402010-02-22 03:15:57 -0800211 }
212
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700213 if (flags & ROT_90) {
Mathias Agopian883dffa2010-10-25 18:29:35 -0700214 const float original_w = h;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700215 R.mType = (ROT_90 << 8) | ROTATE;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700216 R.mType |= isZero(original_w) ? IDENTITY : TRANSLATE;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700217 mat33& M(R.mMatrix);
Mathias Agopian883dffa2010-10-25 18:29:35 -0700218 M[0][0] = 0; M[1][0] =-1; M[2][0] = original_w;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700219 M[0][1] = 1; M[1][1] = 0;
Mathias Agopianeda65402010-02-22 03:15:57 -0800220 }
221
Mathias Agopian883dffa2010-10-25 18:29:35 -0700222 *this = (R*(H*V));
Mathias Agopiand1296592010-03-09 19:17:47 -0800223 return NO_ERROR;
Mathias Agopianeda65402010-02-22 03:15:57 -0800224}
225
chaviwc01e1372020-07-01 12:37:31 -0700226void 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
Mathias Agopianff2ed702013-09-01 21:36:12 -0700235vec2 Transform::transform(const vec2& v) const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800236 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
Mathias Agopianff2ed702013-09-01 21:36:12 -0700243vec3 Transform::transform(const vec3& v) const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800244 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
chaviwc01e1372020-07-01 12:37:31 -0700252vec2 Transform::transform(float x, float y) const {
253 return transform(vec2(x, y));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254}
255
Marin Shalamanov043ba292020-09-10 13:35:20 +0200256Rect Transform::makeBounds(int w, int h) const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800257 return transform( Rect(w, h) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258}
259
Marin Shalamanov043ba292020-09-10 13:35:20 +0200260Rect Transform::transform(const Rect& bounds, bool roundOutwards) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261 Rect r;
Mathias Agopianeda65402010-02-22 03:15:57 -0800262 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
Pablo Ceballos51450032016-08-03 10:20:45 -0700272 if (roundOutwards) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700273 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]})));
Pablo Ceballos51450032016-08-03 10:20:45 -0700277 } else {
Peiyong Linefefaac2018-08-17 12:27:51 -0700278 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));
Pablo Ceballos51450032016-08-03 10:20:45 -0700282 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800283
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284 return r;
285}
286
Marin Shalamanov043ba292020-09-10 13:35:20 +0200287FloatRect Transform::transform(const FloatRect& bounds) const {
Dan Stoza80d61162017-12-20 15:57:52 -0800288 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;
Peiyong Lin3db42342018-08-16 09:15:59 -0700299 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]});
Dan Stoza80d61162017-12-20 15:57:52 -0800303
304 return r;
305}
306
Marin Shalamanov043ba292020-09-10 13:35:20 +0200307Region Transform::transform(const Region& reg) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308 Region out;
Dan Stoza22f7fc42016-05-10 16:19:53 -0700309 if (CC_UNLIKELY(type() > TRANSLATE)) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800310 if (CC_LIKELY(preserveRects())) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700311 Region::const_iterator it = reg.begin();
312 Region::const_iterator const end = reg.end();
313 while (it != end) {
314 out.orSelf(transform(*it++));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315 }
316 } else {
317 out.set(transform(reg.bounds()));
318 }
319 } else {
Peiyong Linefefaac2018-08-17 12:27:51 -0700320 int xpos = static_cast<int>(floorf(tx() + 0.5f));
321 int ypos = static_cast<int>(floorf(ty() + 0.5f));
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700322 out = reg.translate(xpos, ypos);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323 }
324 return out;
325}
326
Marin Shalamanov043ba292020-09-10 13:35:20 +0200327uint32_t Transform::type() const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800328 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)) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800342 if (a<0) flags |= FLIP_H;
343 if (d<0) flags |= FLIP_V;
344 if (!absIsOne(a) || !absIsOne(d)) {
345 scale = true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800346 }
347 } else if (isZero(a) && isZero(d)) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800348 flags |= ROT_90;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700349 if (b>0) flags |= FLIP_V;
350 if (c<0) flags |= FLIP_H;
Mathias Agopiand1296592010-03-09 19:17:47 -0800351 if (!absIsOne(b) || !absIsOne(c)) {
352 scale = true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800353 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800354 } else {
Mathias Agopian29a367b2011-07-12 14:51:45 -0700355 // there is a skew component and/or a non 90 degrees rotation
Mathias Agopianeda65402010-02-22 03:15:57 -0800356 flags = ROT_INVALID;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800357 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800358
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;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800375 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800376 return mType;
377}
378
Mathias Agopian3da16722013-02-28 17:12:07 -0800379Transform 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) {
Michael Lentine28ea2172014-11-19 18:32:37 -0800385 // 1 0 0
386 // 0 1 0
387 // x y 1
Mathias Agopian3da16722013-02-28 17:12:07 -0800388 result = *this;
389 result.mMatrix[2][0] = -result.mMatrix[2][0];
390 result.mMatrix[2][1] = -result.mMatrix[2][1];
391 } else {
Michael Lentine28ea2172014-11-19 18:32:37 -0800392 // a c 0
393 // b d 0
394 // x y 1
Mathias Agopian3da16722013-02-28 17:12:07 -0800395 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
Garfield Tan2c1782c2022-02-16 15:25:05 -0800403 const float idet = 1.0f / det();
Michael Lentine28ea2172014-11-19 18:32:37 -0800404 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;
Prabir Pradhan61b41732021-08-16 12:19:26 -0700409 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 }
Mathias Agopian3da16722013-02-28 17:12:07 -0800414
Michael Lentine28ea2172014-11-19 18:32:37 -0800415 vec2 T(-x, -y);
416 T = result.transform(T);
417 result.mMatrix[2][0] = T[0];
418 result.mMatrix[2][1] = T[1];
Mathias Agopian3da16722013-02-28 17:12:07 -0800419 }
420 return result;
421}
422
Mathias Agopianeda65402010-02-22 03:15:57 -0800423uint32_t Transform::getType() const {
424 return type() & 0xFF;
425}
426
Marin Shalamanov043ba292020-09-10 13:35:20 +0200427uint32_t Transform::getOrientation() const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800428 return (type() >> 8) & 0xFF;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429}
430
Marin Shalamanov043ba292020-09-10 13:35:20 +0200431bool Transform::preserveRects() const {
Mathias Agopianab7c13f2011-07-25 13:57:16 -0700432 return (getOrientation() & ROT_INVALID) ? false : true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800433}
434
Marin Shalamanov043ba292020-09-10 13:35:20 +0200435bool Transform::needsBilinearFiltering() const {
436 return (!preserveRects() || getType() >= ui::Transform::SCALE);
437}
438
Lloyd Pique7e06e7f2019-03-15 18:36:44 -0700439mat4 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
chaviw85b44202020-07-24 11:46:21 -0700470static 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
490static 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
506void Transform::dump(std::string& out, const char* name, const char* prefix) const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700507 using android::base::StringAppendF;
Mathias Agopianeda65402010-02-22 03:15:57 -0800508
Lloyd Pique32cbe282018-10-19 13:09:22 -0700509 type(); // Ensure the information in mType is up to date
Mathias Agopianeda65402010-02-22 03:15:57 -0800510
Lloyd Pique32cbe282018-10-19 13:09:22 -0700511 const uint32_t type = mType;
512 const uint32_t orient = type >> 8;
513
chaviw85b44202020-07-24 11:46:21 -0700514 out += prefix;
515 out += name;
516 out += " ";
Lloyd Pique32cbe282018-10-19 13:09:22 -0700517
518 if (orient & ROT_INVALID) {
chaviw85b44202020-07-24 11:46:21 -0700519 StringAppendF(&out, "0x%08x ", orient);
Mathias Agopiand1296592010-03-09 19:17:47 -0800520 }
chaviw85b44202020-07-24 11:46:21 -0700521 out += "(" + rotationToString(orient) + ") ";
Mathias Agopianeda65402010-02-22 03:15:57 -0800522
chaviw85b44202020-07-24 11:46:21 -0700523 if (type & UNKNOWN) {
524 StringAppendF(&out, "0x%02x ", type);
525 }
526 out += "(" + transformToString(type) + ")\n";
Lloyd Pique32cbe282018-10-19 13:09:22 -0700527
Vishnu Naird0929bc2020-08-27 17:22:58 -0700528 if (type == IDENTITY) {
529 return;
530 }
531
Lloyd Pique32cbe282018-10-19 13:09:22 -0700532 for (size_t i = 0; i < 3; i++) {
chaviw85b44202020-07-24 11:46:21 -0700533 StringAppendF(&out, "%s %.4f %.4f %.4f\n", prefix, static_cast<double>(mMatrix[0][i]),
Lloyd Pique32cbe282018-10-19 13:09:22 -0700534 static_cast<double>(mMatrix[1][i]), static_cast<double>(mMatrix[2][i]));
535 }
536}
537
chaviw85b44202020-07-24 11:46:21 -0700538void Transform::dump(const char* name, const char* prefix) const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700539 std::string out;
chaviw85b44202020-07-24 11:46:21 -0700540 dump(out, name, prefix);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700541 ALOGD("%s", out.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800542}
543
Marin Shalamanov043ba292020-09-10 13:35:20 +0200544} // namespace android::ui