blob: ec8a78afdc1ea404d3cbb2dbac40c7e46cc5f376 [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
Mathias Agopianeda65402010-02-22 03:15:57 -080017#include <math.h>
18
Lloyd Pique32cbe282018-10-19 13:09:22 -070019#include <android-base/stringprintf.h>
Mathias Agopianeda65402010-02-22 03:15:57 -080020#include <cutils/compiler.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <ui/Region.h>
Peiyong Linefefaac2018-08-17 12:27:51 -070022#include <ui/Transform.h>
23#include <utils/String8.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024
Marin Shalamanov043ba292020-09-10 13:35:20 +020025namespace android::ui {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
Mathias Agopianeda65402010-02-22 03:15:57 -080027Transform::Transform() {
28 reset();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029}
30
31Transform::Transform(const Transform& other)
Mathias Agopianeda65402010-02-22 03:15:57 -080032 : mMatrix(other.mMatrix), mType(other.mType) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033}
34
Lloyd Piqueb62cebc2019-11-20 18:31:52 -080035Transform::Transform(uint32_t orientation, int w, int h) {
36 set(orientation, w, h);
Chih-Chung Chang52e72002010-01-21 17:31:06 -080037}
38
Peiyong Linefefaac2018-08-17 12:27:51 -070039Transform::~Transform() = default;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040
Mathias Agopiand1296592010-03-09 19:17:47 -080041static const float EPSILON = 0.0f;
Mathias Agopianeda65402010-02-22 03:15:57 -080042
43bool Transform::isZero(float f) {
Mathias Agopiand1296592010-03-09 19:17:47 -080044 return fabs(f) <= EPSILON;
Mathias Agopianeda65402010-02-22 03:15:57 -080045}
46
Mathias Agopiand1296592010-03-09 19:17:47 -080047bool Transform::absIsOne(float f) {
48 return isZero(fabs(f) - 1.0f);
Mathias Agopianeda65402010-02-22 03:15:57 -080049}
50
Lloyd Piqueea629282019-12-03 15:57:10 -080051bool Transform::operator==(const Transform& other) const {
52 return mMatrix[0][0] == other.mMatrix[0][0] && mMatrix[0][1] == other.mMatrix[0][1] &&
53 mMatrix[0][2] == other.mMatrix[0][2] && mMatrix[1][0] == other.mMatrix[1][0] &&
54 mMatrix[1][1] == other.mMatrix[1][1] && mMatrix[1][2] == other.mMatrix[1][2] &&
55 mMatrix[2][0] == other.mMatrix[2][0] && mMatrix[2][1] == other.mMatrix[2][1] &&
56 mMatrix[2][2] == other.mMatrix[2][2];
Lloyd Piqueea629282019-12-03 15:57:10 -080057}
58
Marin Shalamanov043ba292020-09-10 13:35:20 +020059Transform Transform::operator*(const Transform& rhs) const {
Mathias Agopianeda65402010-02-22 03:15:57 -080060 if (CC_LIKELY(mType == IDENTITY))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061 return rhs;
62
63 Transform r(*this);
Mathias Agopianeda65402010-02-22 03:15:57 -080064 if (rhs.mType == IDENTITY)
65 return r;
66
67 // TODO: we could use mType to optimize the matrix multiply
68 const mat33& A(mMatrix);
69 const mat33& B(rhs.mMatrix);
70 mat33& D(r.mMatrix);
Peiyong Linefefaac2018-08-17 12:27:51 -070071 for (size_t i = 0; i < 3; i++) {
Mathias Agopianeda65402010-02-22 03:15:57 -080072 const float v0 = A[0][i];
73 const float v1 = A[1][i];
74 const float v2 = A[2][i];
75 D[0][i] = v0*B[0][0] + v1*B[0][1] + v2*B[0][2];
76 D[1][i] = v0*B[1][0] + v1*B[1][1] + v2*B[1][2];
77 D[2][i] = v0*B[2][0] + v1*B[2][1] + v2*B[2][2];
78 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079 r.mType |= rhs.mType;
Mathias Agopianeda65402010-02-22 03:15:57 -080080
81 // TODO: we could recompute this value from r and rhs
82 r.mType &= 0xFF;
83 r.mType |= UNKNOWN_TYPE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 return r;
85}
86
chaviwc01e1372020-07-01 12:37:31 -070087Transform Transform::operator * (float value) const {
88 Transform r(*this);
89 const mat33& M(mMatrix);
90 mat33& R(r.mMatrix);
91 for (size_t i = 0; i < 3; i++) {
92 for (size_t j = 0; j < 2; j++) {
93 R[i][j] = M[i][j] * value;
94 }
95 }
96 r.type();
97 return r;
98}
99
Peiyong Linefefaac2018-08-17 12:27:51 -0700100Transform& Transform::operator=(const Transform& other) {
101 mMatrix = other.mMatrix;
102 mType = other.mType;
103 return *this;
104}
105
Mathias Agopianff2ed702013-09-01 21:36:12 -0700106const vec3& Transform::operator [] (size_t i) const {
107 return mMatrix[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108}
109
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700110float Transform::tx() const {
111 return mMatrix[2][0];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112}
113
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700114float Transform::ty() const {
115 return mMatrix[2][1];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116}
117
chaviwc01e1372020-07-01 12:37:31 -0700118float Transform::dsdx() const {
Robert Carre07e1032018-11-26 12:55:53 -0800119 return mMatrix[0][0];
120}
121
chaviwc01e1372020-07-01 12:37:31 -0700122float Transform::dtdx() const {
123 return mMatrix[1][0];
124}
125
126float Transform::dtdy() const {
127 return mMatrix[0][1];
128}
129
130float Transform::dsdy() const {
Robert Carre07e1032018-11-26 12:55:53 -0800131 return mMatrix[1][1];
132}
133
chaviw1ff3d1e2020-07-01 15:53:47 -0700134float Transform::getScaleX() const {
135 return sqrt(dsdx() * dsdx()) + (dtdx() * dtdx());
136}
137
138float Transform::getScaleY() const {
139 return sqrt((dtdy() * dtdy()) + (dsdy() * dsdy()));
140}
141
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142void Transform::reset() {
Mathias Agopianeda65402010-02-22 03:15:57 -0800143 mType = IDENTITY;
Peiyong Linefefaac2018-08-17 12:27:51 -0700144 for(size_t i = 0; i < 3; i++) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800145 vec3& v(mMatrix[i]);
Peiyong Linefefaac2018-08-17 12:27:51 -0700146 for (size_t j = 0; j < 3; j++)
147 v[j] = ((i == j) ? 1.0f : 0.0f);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148 }
149}
150
Marin Shalamanov043ba292020-09-10 13:35:20 +0200151void Transform::set(float tx, float ty) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800152 mMatrix[2][0] = tx;
153 mMatrix[2][1] = ty;
154 mMatrix[2][2] = 1.0f;
155
156 if (isZero(tx) && isZero(ty)) {
157 mType &= ~TRANSLATE;
158 } else {
159 mType |= TRANSLATE;
160 }
161}
162
Marin Shalamanov043ba292020-09-10 13:35:20 +0200163void Transform::set(float a, float b, float c, float d) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800164 mat33& M(mMatrix);
165 M[0][0] = a; M[1][0] = b;
166 M[0][1] = c; M[1][1] = d;
167 M[0][2] = 0; M[1][2] = 0;
168 mType = UNKNOWN_TYPE;
169}
170
Marin Shalamanov043ba292020-09-10 13:35:20 +0200171status_t Transform::set(uint32_t flags, float w, float h) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800172 if (flags & ROT_INVALID) {
173 // that's not allowed!
174 reset();
175 return BAD_VALUE;
176 }
177
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700178 Transform H, V, R;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700179 if (flags & ROT_90) {
180 // w & h are inverted when rotating by 90 degrees
Peiyong Lin3db42342018-08-16 09:15:59 -0700181 std::swap(w, h);
Mathias Agopian883dffa2010-10-25 18:29:35 -0700182 }
183
Mathias Agopianeda65402010-02-22 03:15:57 -0800184 if (flags & FLIP_H) {
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700185 H.mType = (FLIP_H << 8) | SCALE;
186 H.mType |= isZero(w) ? IDENTITY : TRANSLATE;
187 mat33& M(H.mMatrix);
188 M[0][0] = -1;
189 M[2][0] = w;
Mathias Agopianeda65402010-02-22 03:15:57 -0800190 }
191
192 if (flags & FLIP_V) {
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700193 V.mType = (FLIP_V << 8) | SCALE;
194 V.mType |= isZero(h) ? IDENTITY : TRANSLATE;
195 mat33& M(V.mMatrix);
196 M[1][1] = -1;
197 M[2][1] = h;
Mathias Agopianeda65402010-02-22 03:15:57 -0800198 }
199
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700200 if (flags & ROT_90) {
Mathias Agopian883dffa2010-10-25 18:29:35 -0700201 const float original_w = h;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700202 R.mType = (ROT_90 << 8) | ROTATE;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700203 R.mType |= isZero(original_w) ? IDENTITY : TRANSLATE;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700204 mat33& M(R.mMatrix);
Mathias Agopian883dffa2010-10-25 18:29:35 -0700205 M[0][0] = 0; M[1][0] =-1; M[2][0] = original_w;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700206 M[0][1] = 1; M[1][1] = 0;
Mathias Agopianeda65402010-02-22 03:15:57 -0800207 }
208
Mathias Agopian883dffa2010-10-25 18:29:35 -0700209 *this = (R*(H*V));
Mathias Agopiand1296592010-03-09 19:17:47 -0800210 return NO_ERROR;
Mathias Agopianeda65402010-02-22 03:15:57 -0800211}
212
chaviwc01e1372020-07-01 12:37:31 -0700213void Transform::set(const std::array<float, 9>& matrix) {
214 mat33& M(mMatrix);
215 M[0][0] = matrix[0]; M[1][0] = matrix[1]; M[2][0] = matrix[2];
216 M[0][1] = matrix[3]; M[1][1] = matrix[4]; M[2][1] = matrix[5];
217 M[0][2] = matrix[6]; M[1][2] = matrix[7]; M[2][2] = matrix[8];
218 mType = UNKNOWN_TYPE;
219 type();
220}
221
Mathias Agopianff2ed702013-09-01 21:36:12 -0700222vec2 Transform::transform(const vec2& v) const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800223 vec2 r;
224 const mat33& M(mMatrix);
225 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0];
226 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1];
227 return r;
228}
229
Mathias Agopianff2ed702013-09-01 21:36:12 -0700230vec3 Transform::transform(const vec3& v) const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800231 vec3 r;
232 const mat33& M(mMatrix);
233 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0]*v[2];
234 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1]*v[2];
235 r[2] = M[0][2]*v[0] + M[1][2]*v[1] + M[2][2]*v[2];
236 return r;
237}
238
chaviwc01e1372020-07-01 12:37:31 -0700239vec2 Transform::transform(float x, float y) const {
240 return transform(vec2(x, y));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241}
242
Marin Shalamanov043ba292020-09-10 13:35:20 +0200243Rect Transform::makeBounds(int w, int h) const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800244 return transform( Rect(w, h) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245}
246
Marin Shalamanov043ba292020-09-10 13:35:20 +0200247Rect Transform::transform(const Rect& bounds, bool roundOutwards) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248 Rect r;
Mathias Agopianeda65402010-02-22 03:15:57 -0800249 vec2 lt( bounds.left, bounds.top );
250 vec2 rt( bounds.right, bounds.top );
251 vec2 lb( bounds.left, bounds.bottom );
252 vec2 rb( bounds.right, bounds.bottom );
253
254 lt = transform(lt);
255 rt = transform(rt);
256 lb = transform(lb);
257 rb = transform(rb);
258
Pablo Ceballos51450032016-08-03 10:20:45 -0700259 if (roundOutwards) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700260 r.left = static_cast<int32_t>(floorf(std::min({lt[0], rt[0], lb[0], rb[0]})));
261 r.top = static_cast<int32_t>(floorf(std::min({lt[1], rt[1], lb[1], rb[1]})));
262 r.right = static_cast<int32_t>(ceilf(std::max({lt[0], rt[0], lb[0], rb[0]})));
263 r.bottom = static_cast<int32_t>(ceilf(std::max({lt[1], rt[1], lb[1], rb[1]})));
Pablo Ceballos51450032016-08-03 10:20:45 -0700264 } else {
Peiyong Linefefaac2018-08-17 12:27:51 -0700265 r.left = static_cast<int32_t>(floorf(std::min({lt[0], rt[0], lb[0], rb[0]}) + 0.5f));
266 r.top = static_cast<int32_t>(floorf(std::min({lt[1], rt[1], lb[1], rb[1]}) + 0.5f));
267 r.right = static_cast<int32_t>(floorf(std::max({lt[0], rt[0], lb[0], rb[0]}) + 0.5f));
268 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 -0700269 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800270
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271 return r;
272}
273
Marin Shalamanov043ba292020-09-10 13:35:20 +0200274FloatRect Transform::transform(const FloatRect& bounds) const {
Dan Stoza80d61162017-12-20 15:57:52 -0800275 vec2 lt(bounds.left, bounds.top);
276 vec2 rt(bounds.right, bounds.top);
277 vec2 lb(bounds.left, bounds.bottom);
278 vec2 rb(bounds.right, bounds.bottom);
279
280 lt = transform(lt);
281 rt = transform(rt);
282 lb = transform(lb);
283 rb = transform(rb);
284
285 FloatRect r;
Peiyong Lin3db42342018-08-16 09:15:59 -0700286 r.left = std::min({lt[0], rt[0], lb[0], rb[0]});
287 r.top = std::min({lt[1], rt[1], lb[1], rb[1]});
288 r.right = std::max({lt[0], rt[0], lb[0], rb[0]});
289 r.bottom = std::max({lt[1], rt[1], lb[1], rb[1]});
Dan Stoza80d61162017-12-20 15:57:52 -0800290
291 return r;
292}
293
Marin Shalamanov043ba292020-09-10 13:35:20 +0200294Region Transform::transform(const Region& reg) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295 Region out;
Dan Stoza22f7fc42016-05-10 16:19:53 -0700296 if (CC_UNLIKELY(type() > TRANSLATE)) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800297 if (CC_LIKELY(preserveRects())) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700298 Region::const_iterator it = reg.begin();
299 Region::const_iterator const end = reg.end();
300 while (it != end) {
301 out.orSelf(transform(*it++));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302 }
303 } else {
304 out.set(transform(reg.bounds()));
305 }
306 } else {
Peiyong Linefefaac2018-08-17 12:27:51 -0700307 int xpos = static_cast<int>(floorf(tx() + 0.5f));
308 int ypos = static_cast<int>(floorf(ty() + 0.5f));
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700309 out = reg.translate(xpos, ypos);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310 }
311 return out;
312}
313
Marin Shalamanov043ba292020-09-10 13:35:20 +0200314uint32_t Transform::type() const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800315 if (mType & UNKNOWN_TYPE) {
316 // recompute what this transform is
317
318 const mat33& M(mMatrix);
319 const float a = M[0][0];
320 const float b = M[1][0];
321 const float c = M[0][1];
322 const float d = M[1][1];
323 const float x = M[2][0];
324 const float y = M[2][1];
325
326 bool scale = false;
327 uint32_t flags = ROT_0;
328 if (isZero(b) && isZero(c)) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800329 if (a<0) flags |= FLIP_H;
330 if (d<0) flags |= FLIP_V;
331 if (!absIsOne(a) || !absIsOne(d)) {
332 scale = true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800333 }
334 } else if (isZero(a) && isZero(d)) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800335 flags |= ROT_90;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700336 if (b>0) flags |= FLIP_V;
337 if (c<0) flags |= FLIP_H;
Mathias Agopiand1296592010-03-09 19:17:47 -0800338 if (!absIsOne(b) || !absIsOne(c)) {
339 scale = true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800340 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800341 } else {
Mathias Agopian29a367b2011-07-12 14:51:45 -0700342 // there is a skew component and/or a non 90 degrees rotation
Mathias Agopianeda65402010-02-22 03:15:57 -0800343 flags = ROT_INVALID;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800344 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800345
346 mType = flags << 8;
347 if (flags & ROT_INVALID) {
348 mType |= UNKNOWN;
349 } else {
350 if ((flags & ROT_90) || ((flags & ROT_180) == ROT_180))
351 mType |= ROTATE;
352 if (flags & FLIP_H)
353 mType ^= SCALE;
354 if (flags & FLIP_V)
355 mType ^= SCALE;
356 if (scale)
357 mType |= SCALE;
358 }
359
360 if (!isZero(x) || !isZero(y))
361 mType |= TRANSLATE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800362 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800363 return mType;
364}
365
Mathias Agopian3da16722013-02-28 17:12:07 -0800366Transform Transform::inverse() const {
367 // our 3x3 matrix is always of the form of a 2x2 transformation
368 // followed by a translation: T*M, therefore:
369 // (T*M)^-1 = M^-1 * T^-1
370 Transform result;
371 if (mType <= TRANSLATE) {
Michael Lentine28ea2172014-11-19 18:32:37 -0800372 // 1 0 0
373 // 0 1 0
374 // x y 1
Mathias Agopian3da16722013-02-28 17:12:07 -0800375 result = *this;
376 result.mMatrix[2][0] = -result.mMatrix[2][0];
377 result.mMatrix[2][1] = -result.mMatrix[2][1];
378 } else {
Michael Lentine28ea2172014-11-19 18:32:37 -0800379 // a c 0
380 // b d 0
381 // x y 1
Mathias Agopian3da16722013-02-28 17:12:07 -0800382 const mat33& M(mMatrix);
383 const float a = M[0][0];
384 const float b = M[1][0];
385 const float c = M[0][1];
386 const float d = M[1][1];
387 const float x = M[2][0];
388 const float y = M[2][1];
389
Peiyong Linefefaac2018-08-17 12:27:51 -0700390 const float idet = 1.0f / (a*d - b*c);
Michael Lentine28ea2172014-11-19 18:32:37 -0800391 result.mMatrix[0][0] = d*idet;
392 result.mMatrix[0][1] = -c*idet;
393 result.mMatrix[1][0] = -b*idet;
394 result.mMatrix[1][1] = a*idet;
395 result.mType = mType;
Mathias Agopian3da16722013-02-28 17:12:07 -0800396
Michael Lentine28ea2172014-11-19 18:32:37 -0800397 vec2 T(-x, -y);
398 T = result.transform(T);
399 result.mMatrix[2][0] = T[0];
400 result.mMatrix[2][1] = T[1];
Mathias Agopian3da16722013-02-28 17:12:07 -0800401 }
402 return result;
403}
404
Mathias Agopianeda65402010-02-22 03:15:57 -0800405uint32_t Transform::getType() const {
406 return type() & 0xFF;
407}
408
Marin Shalamanov043ba292020-09-10 13:35:20 +0200409uint32_t Transform::getOrientation() const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800410 return (type() >> 8) & 0xFF;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800411}
412
Marin Shalamanov043ba292020-09-10 13:35:20 +0200413bool Transform::preserveRects() const {
Mathias Agopianab7c13f2011-07-25 13:57:16 -0700414 return (getOrientation() & ROT_INVALID) ? false : true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800415}
416
Marin Shalamanov043ba292020-09-10 13:35:20 +0200417bool Transform::needsBilinearFiltering() const {
418 return (!preserveRects() || getType() >= ui::Transform::SCALE);
419}
420
Lloyd Pique7e06e7f2019-03-15 18:36:44 -0700421mat4 Transform::asMatrix4() const {
422 // Internally Transform uses a 3x3 matrix since the transform is meant for
423 // two-dimensional values. An equivalent 4x4 matrix means inserting an extra
424 // row and column which adds as an identity transform on the third
425 // dimension.
426
427 mat4 m = mat4{mat4::NO_INIT}; // NO_INIT since we explicitly set every element
428
429 m[0][0] = mMatrix[0][0];
430 m[0][1] = mMatrix[0][1];
431 m[0][2] = 0.f;
432 m[0][3] = mMatrix[0][2];
433
434 m[1][0] = mMatrix[1][0];
435 m[1][1] = mMatrix[1][1];
436 m[1][2] = 0.f;
437 m[1][3] = mMatrix[1][2];
438
439 m[2][0] = 0.f;
440 m[2][1] = 0.f;
441 m[2][2] = 1.f;
442 m[2][3] = 0.f;
443
444 m[3][0] = mMatrix[2][0];
445 m[3][1] = mMatrix[2][1];
446 m[3][2] = 0.f;
447 m[3][3] = mMatrix[2][2];
448
449 return m;
450}
451
chaviw85b44202020-07-24 11:46:21 -0700452static std::string rotationToString(const uint32_t rotationFlags) {
453 switch (rotationFlags) {
454 case Transform::ROT_0:
455 return "ROT_0";
456 case Transform::FLIP_H:
457 return "FLIP_H";
458 case Transform::FLIP_V:
459 return "FLIP_V";
460 case Transform::ROT_90:
461 return "ROT_90";
462 case Transform::ROT_180:
463 return "ROT_180";
464 case Transform::ROT_270:
465 return "ROT_270";
466 case Transform::ROT_INVALID:
467 default:
468 return "ROT_INVALID";
469 }
470}
471
472static std::string transformToString(const uint32_t transform) {
473 if (transform == Transform::IDENTITY) {
474 return "IDENTITY";
475 }
476
477 if (transform == Transform::UNKNOWN) {
478 return "UNKNOWN";
479 }
480
481 std::string out;
482 if (transform & Transform::SCALE) out.append("SCALE ");
483 if (transform & Transform::ROTATE) out.append("ROTATE ");
484 if (transform & Transform::TRANSLATE) out.append("TRANSLATE");
485 return out;
486}
487
488void Transform::dump(std::string& out, const char* name, const char* prefix) const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700489 using android::base::StringAppendF;
Mathias Agopianeda65402010-02-22 03:15:57 -0800490
Lloyd Pique32cbe282018-10-19 13:09:22 -0700491 type(); // Ensure the information in mType is up to date
Mathias Agopianeda65402010-02-22 03:15:57 -0800492
Lloyd Pique32cbe282018-10-19 13:09:22 -0700493 const uint32_t type = mType;
494 const uint32_t orient = type >> 8;
495
chaviw85b44202020-07-24 11:46:21 -0700496 out += prefix;
497 out += name;
498 out += " ";
Lloyd Pique32cbe282018-10-19 13:09:22 -0700499
500 if (orient & ROT_INVALID) {
chaviw85b44202020-07-24 11:46:21 -0700501 StringAppendF(&out, "0x%08x ", orient);
Mathias Agopiand1296592010-03-09 19:17:47 -0800502 }
chaviw85b44202020-07-24 11:46:21 -0700503 out += "(" + rotationToString(orient) + ") ";
Mathias Agopianeda65402010-02-22 03:15:57 -0800504
chaviw85b44202020-07-24 11:46:21 -0700505 if (type & UNKNOWN) {
506 StringAppendF(&out, "0x%02x ", type);
507 }
508 out += "(" + transformToString(type) + ")\n";
Lloyd Pique32cbe282018-10-19 13:09:22 -0700509
Vishnu Naird0929bc2020-08-27 17:22:58 -0700510 if (type == IDENTITY) {
511 return;
512 }
513
Lloyd Pique32cbe282018-10-19 13:09:22 -0700514 for (size_t i = 0; i < 3; i++) {
chaviw85b44202020-07-24 11:46:21 -0700515 StringAppendF(&out, "%s %.4f %.4f %.4f\n", prefix, static_cast<double>(mMatrix[0][i]),
Lloyd Pique32cbe282018-10-19 13:09:22 -0700516 static_cast<double>(mMatrix[1][i]), static_cast<double>(mMatrix[2][i]));
517 }
518}
519
chaviw85b44202020-07-24 11:46:21 -0700520void Transform::dump(const char* name, const char* prefix) const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700521 std::string out;
chaviw85b44202020-07-24 11:46:21 -0700522 dump(out, name, prefix);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700523 ALOGD("%s", out.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800524}
525
Marin Shalamanov043ba292020-09-10 13:35:20 +0200526} // namespace android::ui