blob: b34d90699f170012c7fd5fbf601fa510b74c63ed [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
chaviw1ff3d1e2020-07-01 15:53:47 -0700137float Transform::getScaleX() const {
chaviw44a6d2b2020-09-08 17:14:16 -0700138 return sqrt((dsdx() * dsdx()) + (dtdx() * dtdx()));
chaviw1ff3d1e2020-07-01 15:53:47 -0700139}
140
141float Transform::getScaleY() const {
142 return sqrt((dtdy() * dtdy()) + (dsdy() * dsdy()));
143}
144
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145void Transform::reset() {
Mathias Agopianeda65402010-02-22 03:15:57 -0800146 mType = IDENTITY;
Peiyong Linefefaac2018-08-17 12:27:51 -0700147 for(size_t i = 0; i < 3; i++) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800148 vec3& v(mMatrix[i]);
Peiyong Linefefaac2018-08-17 12:27:51 -0700149 for (size_t j = 0; j < 3; j++)
150 v[j] = ((i == j) ? 1.0f : 0.0f);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151 }
152}
153
Marin Shalamanov043ba292020-09-10 13:35:20 +0200154void Transform::set(float tx, float ty) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800155 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
Marin Shalamanov043ba292020-09-10 13:35:20 +0200166void Transform::set(float a, float b, float c, float d) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800167 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
Marin Shalamanov043ba292020-09-10 13:35:20 +0200174status_t Transform::set(uint32_t flags, float w, float h) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800175 if (flags & ROT_INVALID) {
176 // that's not allowed!
177 reset();
178 return BAD_VALUE;
179 }
180
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700181 Transform H, V, R;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700182 if (flags & ROT_90) {
183 // w & h are inverted when rotating by 90 degrees
Peiyong Lin3db42342018-08-16 09:15:59 -0700184 std::swap(w, h);
Mathias Agopian883dffa2010-10-25 18:29:35 -0700185 }
186
Mathias Agopianeda65402010-02-22 03:15:57 -0800187 if (flags & FLIP_H) {
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700188 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;
Mathias Agopianeda65402010-02-22 03:15:57 -0800193 }
194
195 if (flags & FLIP_V) {
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700196 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;
Mathias Agopianeda65402010-02-22 03:15:57 -0800201 }
202
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700203 if (flags & ROT_90) {
Mathias Agopian883dffa2010-10-25 18:29:35 -0700204 const float original_w = h;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700205 R.mType = (ROT_90 << 8) | ROTATE;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700206 R.mType |= isZero(original_w) ? IDENTITY : TRANSLATE;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700207 mat33& M(R.mMatrix);
Mathias Agopian883dffa2010-10-25 18:29:35 -0700208 M[0][0] = 0; M[1][0] =-1; M[2][0] = original_w;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700209 M[0][1] = 1; M[1][1] = 0;
Mathias Agopianeda65402010-02-22 03:15:57 -0800210 }
211
Mathias Agopian883dffa2010-10-25 18:29:35 -0700212 *this = (R*(H*V));
Mathias Agopiand1296592010-03-09 19:17:47 -0800213 return NO_ERROR;
Mathias Agopianeda65402010-02-22 03:15:57 -0800214}
215
chaviwc01e1372020-07-01 12:37:31 -0700216void 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
Mathias Agopianff2ed702013-09-01 21:36:12 -0700225vec2 Transform::transform(const vec2& v) const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800226 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
Mathias Agopianff2ed702013-09-01 21:36:12 -0700233vec3 Transform::transform(const vec3& v) const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800234 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
chaviwc01e1372020-07-01 12:37:31 -0700242vec2 Transform::transform(float x, float y) const {
243 return transform(vec2(x, y));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244}
245
Marin Shalamanov043ba292020-09-10 13:35:20 +0200246Rect Transform::makeBounds(int w, int h) const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800247 return transform( Rect(w, h) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248}
249
Marin Shalamanov043ba292020-09-10 13:35:20 +0200250Rect Transform::transform(const Rect& bounds, bool roundOutwards) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251 Rect r;
Mathias Agopianeda65402010-02-22 03:15:57 -0800252 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
Pablo Ceballos51450032016-08-03 10:20:45 -0700262 if (roundOutwards) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700263 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]})));
Pablo Ceballos51450032016-08-03 10:20:45 -0700267 } else {
Peiyong Linefefaac2018-08-17 12:27:51 -0700268 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));
Pablo Ceballos51450032016-08-03 10:20:45 -0700272 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800273
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274 return r;
275}
276
Marin Shalamanov043ba292020-09-10 13:35:20 +0200277FloatRect Transform::transform(const FloatRect& bounds) const {
Dan Stoza80d61162017-12-20 15:57:52 -0800278 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;
Peiyong Lin3db42342018-08-16 09:15:59 -0700289 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]});
Dan Stoza80d61162017-12-20 15:57:52 -0800293
294 return r;
295}
296
Marin Shalamanov043ba292020-09-10 13:35:20 +0200297Region Transform::transform(const Region& reg) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298 Region out;
Dan Stoza22f7fc42016-05-10 16:19:53 -0700299 if (CC_UNLIKELY(type() > TRANSLATE)) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800300 if (CC_LIKELY(preserveRects())) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700301 Region::const_iterator it = reg.begin();
302 Region::const_iterator const end = reg.end();
303 while (it != end) {
304 out.orSelf(transform(*it++));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305 }
306 } else {
307 out.set(transform(reg.bounds()));
308 }
309 } else {
Peiyong Linefefaac2018-08-17 12:27:51 -0700310 int xpos = static_cast<int>(floorf(tx() + 0.5f));
311 int ypos = static_cast<int>(floorf(ty() + 0.5f));
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700312 out = reg.translate(xpos, ypos);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800313 }
314 return out;
315}
316
Marin Shalamanov043ba292020-09-10 13:35:20 +0200317uint32_t Transform::type() const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800318 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)) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800332 if (a<0) flags |= FLIP_H;
333 if (d<0) flags |= FLIP_V;
334 if (!absIsOne(a) || !absIsOne(d)) {
335 scale = true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800336 }
337 } else if (isZero(a) && isZero(d)) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800338 flags |= ROT_90;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700339 if (b>0) flags |= FLIP_V;
340 if (c<0) flags |= FLIP_H;
Mathias Agopiand1296592010-03-09 19:17:47 -0800341 if (!absIsOne(b) || !absIsOne(c)) {
342 scale = true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800343 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800344 } else {
Mathias Agopian29a367b2011-07-12 14:51:45 -0700345 // there is a skew component and/or a non 90 degrees rotation
Mathias Agopianeda65402010-02-22 03:15:57 -0800346 flags = ROT_INVALID;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800347 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800348
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;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800365 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800366 return mType;
367}
368
Mathias Agopian3da16722013-02-28 17:12:07 -0800369Transform 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) {
Michael Lentine28ea2172014-11-19 18:32:37 -0800375 // 1 0 0
376 // 0 1 0
377 // x y 1
Mathias Agopian3da16722013-02-28 17:12:07 -0800378 result = *this;
379 result.mMatrix[2][0] = -result.mMatrix[2][0];
380 result.mMatrix[2][1] = -result.mMatrix[2][1];
381 } else {
Michael Lentine28ea2172014-11-19 18:32:37 -0800382 // a c 0
383 // b d 0
384 // x y 1
Mathias Agopian3da16722013-02-28 17:12:07 -0800385 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
Peiyong Linefefaac2018-08-17 12:27:51 -0700393 const float idet = 1.0f / (a*d - b*c);
Michael Lentine28ea2172014-11-19 18:32:37 -0800394 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;
Prabir Pradhan61b41732021-08-16 12:19:26 -0700399 if (getOrientation() & ROT_90) {
400 // Recalculate the type if there is a 90-degree rotation component, since the inverse
401 // of ROT_90 is ROT_270 and vice versa.
402 result.mType |= UNKNOWN_TYPE;
403 }
Mathias Agopian3da16722013-02-28 17:12:07 -0800404
Michael Lentine28ea2172014-11-19 18:32:37 -0800405 vec2 T(-x, -y);
406 T = result.transform(T);
407 result.mMatrix[2][0] = T[0];
408 result.mMatrix[2][1] = T[1];
Mathias Agopian3da16722013-02-28 17:12:07 -0800409 }
410 return result;
411}
412
Mathias Agopianeda65402010-02-22 03:15:57 -0800413uint32_t Transform::getType() const {
414 return type() & 0xFF;
415}
416
Marin Shalamanov043ba292020-09-10 13:35:20 +0200417uint32_t Transform::getOrientation() const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800418 return (type() >> 8) & 0xFF;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800419}
420
Marin Shalamanov043ba292020-09-10 13:35:20 +0200421bool Transform::preserveRects() const {
Mathias Agopianab7c13f2011-07-25 13:57:16 -0700422 return (getOrientation() & ROT_INVALID) ? false : true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800423}
424
Marin Shalamanov043ba292020-09-10 13:35:20 +0200425bool Transform::needsBilinearFiltering() const {
426 return (!preserveRects() || getType() >= ui::Transform::SCALE);
427}
428
Lloyd Pique7e06e7f2019-03-15 18:36:44 -0700429mat4 Transform::asMatrix4() const {
430 // Internally Transform uses a 3x3 matrix since the transform is meant for
431 // two-dimensional values. An equivalent 4x4 matrix means inserting an extra
432 // row and column which adds as an identity transform on the third
433 // dimension.
434
435 mat4 m = mat4{mat4::NO_INIT}; // NO_INIT since we explicitly set every element
436
437 m[0][0] = mMatrix[0][0];
438 m[0][1] = mMatrix[0][1];
439 m[0][2] = 0.f;
440 m[0][3] = mMatrix[0][2];
441
442 m[1][0] = mMatrix[1][0];
443 m[1][1] = mMatrix[1][1];
444 m[1][2] = 0.f;
445 m[1][3] = mMatrix[1][2];
446
447 m[2][0] = 0.f;
448 m[2][1] = 0.f;
449 m[2][2] = 1.f;
450 m[2][3] = 0.f;
451
452 m[3][0] = mMatrix[2][0];
453 m[3][1] = mMatrix[2][1];
454 m[3][2] = 0.f;
455 m[3][3] = mMatrix[2][2];
456
457 return m;
458}
459
chaviw85b44202020-07-24 11:46:21 -0700460static std::string rotationToString(const uint32_t rotationFlags) {
461 switch (rotationFlags) {
462 case Transform::ROT_0:
463 return "ROT_0";
464 case Transform::FLIP_H:
465 return "FLIP_H";
466 case Transform::FLIP_V:
467 return "FLIP_V";
468 case Transform::ROT_90:
469 return "ROT_90";
470 case Transform::ROT_180:
471 return "ROT_180";
472 case Transform::ROT_270:
473 return "ROT_270";
474 case Transform::ROT_INVALID:
475 default:
476 return "ROT_INVALID";
477 }
478}
479
480static std::string transformToString(const uint32_t transform) {
481 if (transform == Transform::IDENTITY) {
482 return "IDENTITY";
483 }
484
485 if (transform == Transform::UNKNOWN) {
486 return "UNKNOWN";
487 }
488
489 std::string out;
490 if (transform & Transform::SCALE) out.append("SCALE ");
491 if (transform & Transform::ROTATE) out.append("ROTATE ");
492 if (transform & Transform::TRANSLATE) out.append("TRANSLATE");
493 return out;
494}
495
496void Transform::dump(std::string& out, const char* name, const char* prefix) const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700497 using android::base::StringAppendF;
Mathias Agopianeda65402010-02-22 03:15:57 -0800498
Lloyd Pique32cbe282018-10-19 13:09:22 -0700499 type(); // Ensure the information in mType is up to date
Mathias Agopianeda65402010-02-22 03:15:57 -0800500
Lloyd Pique32cbe282018-10-19 13:09:22 -0700501 const uint32_t type = mType;
502 const uint32_t orient = type >> 8;
503
chaviw85b44202020-07-24 11:46:21 -0700504 out += prefix;
505 out += name;
506 out += " ";
Lloyd Pique32cbe282018-10-19 13:09:22 -0700507
508 if (orient & ROT_INVALID) {
chaviw85b44202020-07-24 11:46:21 -0700509 StringAppendF(&out, "0x%08x ", orient);
Mathias Agopiand1296592010-03-09 19:17:47 -0800510 }
chaviw85b44202020-07-24 11:46:21 -0700511 out += "(" + rotationToString(orient) + ") ";
Mathias Agopianeda65402010-02-22 03:15:57 -0800512
chaviw85b44202020-07-24 11:46:21 -0700513 if (type & UNKNOWN) {
514 StringAppendF(&out, "0x%02x ", type);
515 }
516 out += "(" + transformToString(type) + ")\n";
Lloyd Pique32cbe282018-10-19 13:09:22 -0700517
Vishnu Naird0929bc2020-08-27 17:22:58 -0700518 if (type == IDENTITY) {
519 return;
520 }
521
Lloyd Pique32cbe282018-10-19 13:09:22 -0700522 for (size_t i = 0; i < 3; i++) {
chaviw85b44202020-07-24 11:46:21 -0700523 StringAppendF(&out, "%s %.4f %.4f %.4f\n", prefix, static_cast<double>(mMatrix[0][i]),
Lloyd Pique32cbe282018-10-19 13:09:22 -0700524 static_cast<double>(mMatrix[1][i]), static_cast<double>(mMatrix[2][i]));
525 }
526}
527
chaviw85b44202020-07-24 11:46:21 -0700528void Transform::dump(const char* name, const char* prefix) const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700529 std::string out;
chaviw85b44202020-07-24 11:46:21 -0700530 dump(out, name, prefix);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700531 ALOGD("%s", out.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800532}
533
Marin Shalamanov043ba292020-09-10 13:35:20 +0200534} // namespace android::ui