blob: 6b1bb40bd81e1c93d4f3b54fffaf25249cf30fd7 [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025namespace android {
Peiyong Linefefaac2018-08-17 12:27:51 -070026namespace ui {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
Mathias Agopianeda65402010-02-22 03:15:57 -080028Transform::Transform() {
29 reset();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030}
31
32Transform::Transform(const Transform& other)
Mathias Agopianeda65402010-02-22 03:15:57 -080033 : mMatrix(other.mMatrix), mType(other.mType) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034}
35
Lloyd Piqueb62cebc2019-11-20 18:31:52 -080036Transform::Transform(uint32_t orientation, int w, int h) {
37 set(orientation, w, h);
Chih-Chung Chang52e72002010-01-21 17:31:06 -080038}
39
Peiyong Linefefaac2018-08-17 12:27:51 -070040Transform::~Transform() = default;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041
Mathias Agopiand1296592010-03-09 19:17:47 -080042static const float EPSILON = 0.0f;
Mathias Agopianeda65402010-02-22 03:15:57 -080043
44bool Transform::isZero(float f) {
Mathias Agopiand1296592010-03-09 19:17:47 -080045 return fabs(f) <= EPSILON;
Mathias Agopianeda65402010-02-22 03:15:57 -080046}
47
Mathias Agopiand1296592010-03-09 19:17:47 -080048bool Transform::absIsOne(float f) {
49 return isZero(fabs(f) - 1.0f);
Mathias Agopianeda65402010-02-22 03:15:57 -080050}
51
Lloyd Piqueea629282019-12-03 15:57:10 -080052bool Transform::operator==(const Transform& other) const {
53 return mMatrix[0][0] == other.mMatrix[0][0] && mMatrix[0][1] == other.mMatrix[0][1] &&
54 mMatrix[0][2] == other.mMatrix[0][2] && mMatrix[1][0] == other.mMatrix[1][0] &&
55 mMatrix[1][1] == other.mMatrix[1][1] && mMatrix[1][2] == other.mMatrix[1][2] &&
56 mMatrix[2][0] == other.mMatrix[2][0] && mMatrix[2][1] == other.mMatrix[2][1] &&
57 mMatrix[2][2] == other.mMatrix[2][2];
Lloyd Piqueea629282019-12-03 15:57:10 -080058}
59
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060Transform Transform::operator * (const Transform& rhs) const
61{
Mathias Agopianeda65402010-02-22 03:15:57 -080062 if (CC_LIKELY(mType == IDENTITY))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063 return rhs;
64
65 Transform r(*this);
Mathias Agopianeda65402010-02-22 03:15:57 -080066 if (rhs.mType == IDENTITY)
67 return r;
68
69 // TODO: we could use mType to optimize the matrix multiply
70 const mat33& A(mMatrix);
71 const mat33& B(rhs.mMatrix);
72 mat33& D(r.mMatrix);
Peiyong Linefefaac2018-08-17 12:27:51 -070073 for (size_t i = 0; i < 3; i++) {
Mathias Agopianeda65402010-02-22 03:15:57 -080074 const float v0 = A[0][i];
75 const float v1 = A[1][i];
76 const float v2 = A[2][i];
77 D[0][i] = v0*B[0][0] + v1*B[0][1] + v2*B[0][2];
78 D[1][i] = v0*B[1][0] + v1*B[1][1] + v2*B[1][2];
79 D[2][i] = v0*B[2][0] + v1*B[2][1] + v2*B[2][2];
80 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081 r.mType |= rhs.mType;
Mathias Agopianeda65402010-02-22 03:15:57 -080082
83 // TODO: we could recompute this value from r and rhs
84 r.mType &= 0xFF;
85 r.mType |= UNKNOWN_TYPE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 return r;
87}
88
chaviwc01e1372020-07-01 12:37:31 -070089Transform Transform::operator * (float value) const {
90 Transform r(*this);
91 const mat33& M(mMatrix);
92 mat33& R(r.mMatrix);
93 for (size_t i = 0; i < 3; i++) {
94 for (size_t j = 0; j < 2; j++) {
95 R[i][j] = M[i][j] * value;
96 }
97 }
98 r.type();
99 return r;
100}
101
Peiyong Linefefaac2018-08-17 12:27:51 -0700102Transform& Transform::operator=(const Transform& other) {
103 mMatrix = other.mMatrix;
104 mType = other.mType;
105 return *this;
106}
107
Mathias Agopianff2ed702013-09-01 21:36:12 -0700108const vec3& Transform::operator [] (size_t i) const {
109 return mMatrix[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110}
111
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700112float Transform::tx() const {
113 return mMatrix[2][0];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114}
115
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700116float Transform::ty() const {
117 return mMatrix[2][1];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118}
119
chaviwc01e1372020-07-01 12:37:31 -0700120float Transform::dsdx() const {
Robert Carre07e1032018-11-26 12:55:53 -0800121 return mMatrix[0][0];
122}
123
chaviwc01e1372020-07-01 12:37:31 -0700124float Transform::dtdx() const {
125 return mMatrix[1][0];
126}
127
128float Transform::dtdy() const {
129 return mMatrix[0][1];
130}
131
132float Transform::dsdy() const {
Robert Carre07e1032018-11-26 12:55:53 -0800133 return mMatrix[1][1];
134}
135
chaviw1ff3d1e2020-07-01 15:53:47 -0700136float Transform::getScaleX() const {
137 return sqrt(dsdx() * dsdx()) + (dtdx() * dtdx());
138}
139
140float Transform::getScaleY() const {
141 return sqrt((dtdy() * dtdy()) + (dsdy() * dsdy()));
142}
143
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144void Transform::reset() {
Mathias Agopianeda65402010-02-22 03:15:57 -0800145 mType = IDENTITY;
Peiyong Linefefaac2018-08-17 12:27:51 -0700146 for(size_t i = 0; i < 3; i++) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800147 vec3& v(mMatrix[i]);
Peiyong Linefefaac2018-08-17 12:27:51 -0700148 for (size_t j = 0; j < 3; j++)
149 v[j] = ((i == j) ? 1.0f : 0.0f);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150 }
151}
152
Mathias Agopianeda65402010-02-22 03:15:57 -0800153void Transform::set(float tx, float ty)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154{
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
166void Transform::set(float a, float b, float c, float d)
167{
168 mat33& M(mMatrix);
169 M[0][0] = a; M[1][0] = b;
170 M[0][1] = c; M[1][1] = d;
171 M[0][2] = 0; M[1][2] = 0;
172 mType = UNKNOWN_TYPE;
173}
174
Mathias Agopiand1296592010-03-09 19:17:47 -0800175status_t Transform::set(uint32_t flags, float w, float h)
Mathias Agopianeda65402010-02-22 03:15:57 -0800176{
Mathias Agopiand1296592010-03-09 19:17:47 -0800177 if (flags & ROT_INVALID) {
178 // that's not allowed!
179 reset();
180 return BAD_VALUE;
181 }
182
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700183 Transform H, V, R;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700184 if (flags & ROT_90) {
185 // w & h are inverted when rotating by 90 degrees
Peiyong Lin3db42342018-08-16 09:15:59 -0700186 std::swap(w, h);
Mathias Agopian883dffa2010-10-25 18:29:35 -0700187 }
188
Mathias Agopianeda65402010-02-22 03:15:57 -0800189 if (flags & FLIP_H) {
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700190 H.mType = (FLIP_H << 8) | SCALE;
191 H.mType |= isZero(w) ? IDENTITY : TRANSLATE;
192 mat33& M(H.mMatrix);
193 M[0][0] = -1;
194 M[2][0] = w;
Mathias Agopianeda65402010-02-22 03:15:57 -0800195 }
196
197 if (flags & FLIP_V) {
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700198 V.mType = (FLIP_V << 8) | SCALE;
199 V.mType |= isZero(h) ? IDENTITY : TRANSLATE;
200 mat33& M(V.mMatrix);
201 M[1][1] = -1;
202 M[2][1] = h;
Mathias Agopianeda65402010-02-22 03:15:57 -0800203 }
204
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700205 if (flags & ROT_90) {
Mathias Agopian883dffa2010-10-25 18:29:35 -0700206 const float original_w = h;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700207 R.mType = (ROT_90 << 8) | ROTATE;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700208 R.mType |= isZero(original_w) ? IDENTITY : TRANSLATE;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700209 mat33& M(R.mMatrix);
Mathias Agopian883dffa2010-10-25 18:29:35 -0700210 M[0][0] = 0; M[1][0] =-1; M[2][0] = original_w;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700211 M[0][1] = 1; M[1][1] = 0;
Mathias Agopianeda65402010-02-22 03:15:57 -0800212 }
213
Mathias Agopian883dffa2010-10-25 18:29:35 -0700214 *this = (R*(H*V));
Mathias Agopiand1296592010-03-09 19:17:47 -0800215 return NO_ERROR;
Mathias Agopianeda65402010-02-22 03:15:57 -0800216}
217
chaviwc01e1372020-07-01 12:37:31 -0700218void Transform::set(const std::array<float, 9>& matrix) {
219 mat33& M(mMatrix);
220 M[0][0] = matrix[0]; M[1][0] = matrix[1]; M[2][0] = matrix[2];
221 M[0][1] = matrix[3]; M[1][1] = matrix[4]; M[2][1] = matrix[5];
222 M[0][2] = matrix[6]; M[1][2] = matrix[7]; M[2][2] = matrix[8];
223 mType = UNKNOWN_TYPE;
224 type();
225}
226
Mathias Agopianff2ed702013-09-01 21:36:12 -0700227vec2 Transform::transform(const vec2& v) const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800228 vec2 r;
229 const mat33& M(mMatrix);
230 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0];
231 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1];
232 return r;
233}
234
Mathias Agopianff2ed702013-09-01 21:36:12 -0700235vec3 Transform::transform(const vec3& v) const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800236 vec3 r;
237 const mat33& M(mMatrix);
238 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0]*v[2];
239 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1]*v[2];
240 r[2] = M[0][2]*v[0] + M[1][2]*v[1] + M[2][2]*v[2];
241 return r;
242}
243
chaviwc01e1372020-07-01 12:37:31 -0700244vec2 Transform::transform(float x, float y) const {
245 return transform(vec2(x, y));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246}
247
248Rect Transform::makeBounds(int w, int h) const
249{
Mathias Agopianeda65402010-02-22 03:15:57 -0800250 return transform( Rect(w, h) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251}
252
Pablo Ceballos51450032016-08-03 10:20:45 -0700253Rect Transform::transform(const Rect& bounds, bool roundOutwards) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254{
255 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
Dan Stoza80d61162017-12-20 15:57:52 -0800281FloatRect Transform::transform(const FloatRect& bounds) const
282{
283 vec2 lt(bounds.left, bounds.top);
284 vec2 rt(bounds.right, bounds.top);
285 vec2 lb(bounds.left, bounds.bottom);
286 vec2 rb(bounds.right, bounds.bottom);
287
288 lt = transform(lt);
289 rt = transform(rt);
290 lb = transform(lb);
291 rb = transform(rb);
292
293 FloatRect r;
Peiyong Lin3db42342018-08-16 09:15:59 -0700294 r.left = std::min({lt[0], rt[0], lb[0], rb[0]});
295 r.top = std::min({lt[1], rt[1], lb[1], rb[1]});
296 r.right = std::max({lt[0], rt[0], lb[0], rb[0]});
297 r.bottom = std::max({lt[1], rt[1], lb[1], rb[1]});
Dan Stoza80d61162017-12-20 15:57:52 -0800298
299 return r;
300}
301
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302Region Transform::transform(const Region& reg) const
303{
304 Region out;
Dan Stoza22f7fc42016-05-10 16:19:53 -0700305 if (CC_UNLIKELY(type() > TRANSLATE)) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800306 if (CC_LIKELY(preserveRects())) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700307 Region::const_iterator it = reg.begin();
308 Region::const_iterator const end = reg.end();
309 while (it != end) {
310 out.orSelf(transform(*it++));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311 }
312 } else {
313 out.set(transform(reg.bounds()));
314 }
315 } else {
Peiyong Linefefaac2018-08-17 12:27:51 -0700316 int xpos = static_cast<int>(floorf(tx() + 0.5f));
317 int ypos = static_cast<int>(floorf(ty() + 0.5f));
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700318 out = reg.translate(xpos, ypos);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319 }
320 return out;
321}
322
Mathias Agopianeda65402010-02-22 03:15:57 -0800323uint32_t Transform::type() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324{
Mathias Agopianeda65402010-02-22 03:15:57 -0800325 if (mType & UNKNOWN_TYPE) {
326 // recompute what this transform is
327
328 const mat33& M(mMatrix);
329 const float a = M[0][0];
330 const float b = M[1][0];
331 const float c = M[0][1];
332 const float d = M[1][1];
333 const float x = M[2][0];
334 const float y = M[2][1];
335
336 bool scale = false;
337 uint32_t flags = ROT_0;
338 if (isZero(b) && isZero(c)) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800339 if (a<0) flags |= FLIP_H;
340 if (d<0) flags |= FLIP_V;
341 if (!absIsOne(a) || !absIsOne(d)) {
342 scale = true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800343 }
344 } else if (isZero(a) && isZero(d)) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800345 flags |= ROT_90;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700346 if (b>0) flags |= FLIP_V;
347 if (c<0) flags |= FLIP_H;
Mathias Agopiand1296592010-03-09 19:17:47 -0800348 if (!absIsOne(b) || !absIsOne(c)) {
349 scale = true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800350 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800351 } else {
Mathias Agopian29a367b2011-07-12 14:51:45 -0700352 // there is a skew component and/or a non 90 degrees rotation
Mathias Agopianeda65402010-02-22 03:15:57 -0800353 flags = ROT_INVALID;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800354 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800355
356 mType = flags << 8;
357 if (flags & ROT_INVALID) {
358 mType |= UNKNOWN;
359 } else {
360 if ((flags & ROT_90) || ((flags & ROT_180) == ROT_180))
361 mType |= ROTATE;
362 if (flags & FLIP_H)
363 mType ^= SCALE;
364 if (flags & FLIP_V)
365 mType ^= SCALE;
366 if (scale)
367 mType |= SCALE;
368 }
369
370 if (!isZero(x) || !isZero(y))
371 mType |= TRANSLATE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800372 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800373 return mType;
374}
375
Mathias Agopian3da16722013-02-28 17:12:07 -0800376Transform Transform::inverse() const {
377 // our 3x3 matrix is always of the form of a 2x2 transformation
378 // followed by a translation: T*M, therefore:
379 // (T*M)^-1 = M^-1 * T^-1
380 Transform result;
381 if (mType <= TRANSLATE) {
Michael Lentine28ea2172014-11-19 18:32:37 -0800382 // 1 0 0
383 // 0 1 0
384 // x y 1
Mathias Agopian3da16722013-02-28 17:12:07 -0800385 result = *this;
386 result.mMatrix[2][0] = -result.mMatrix[2][0];
387 result.mMatrix[2][1] = -result.mMatrix[2][1];
388 } else {
Michael Lentine28ea2172014-11-19 18:32:37 -0800389 // a c 0
390 // b d 0
391 // x y 1
Mathias Agopian3da16722013-02-28 17:12:07 -0800392 const mat33& M(mMatrix);
393 const float a = M[0][0];
394 const float b = M[1][0];
395 const float c = M[0][1];
396 const float d = M[1][1];
397 const float x = M[2][0];
398 const float y = M[2][1];
399
Peiyong Linefefaac2018-08-17 12:27:51 -0700400 const float idet = 1.0f / (a*d - b*c);
Michael Lentine28ea2172014-11-19 18:32:37 -0800401 result.mMatrix[0][0] = d*idet;
402 result.mMatrix[0][1] = -c*idet;
403 result.mMatrix[1][0] = -b*idet;
404 result.mMatrix[1][1] = a*idet;
405 result.mType = mType;
Mathias Agopian3da16722013-02-28 17:12:07 -0800406
Michael Lentine28ea2172014-11-19 18:32:37 -0800407 vec2 T(-x, -y);
408 T = result.transform(T);
409 result.mMatrix[2][0] = T[0];
410 result.mMatrix[2][1] = T[1];
Mathias Agopian3da16722013-02-28 17:12:07 -0800411 }
412 return result;
413}
414
Mathias Agopianeda65402010-02-22 03:15:57 -0800415uint32_t Transform::getType() const {
416 return type() & 0xFF;
417}
418
419uint32_t Transform::getOrientation() const
420{
421 return (type() >> 8) & 0xFF;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800422}
423
424bool Transform::preserveRects() const
425{
Mathias Agopianab7c13f2011-07-25 13:57:16 -0700426 return (getOrientation() & ROT_INVALID) ? false : true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800427}
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
518 for (size_t i = 0; i < 3; i++) {
chaviw85b44202020-07-24 11:46:21 -0700519 StringAppendF(&out, "%s %.4f %.4f %.4f\n", prefix, static_cast<double>(mMatrix[0][i]),
Lloyd Pique32cbe282018-10-19 13:09:22 -0700520 static_cast<double>(mMatrix[1][i]), static_cast<double>(mMatrix[2][i]));
521 }
522}
523
chaviw85b44202020-07-24 11:46:21 -0700524void Transform::dump(const char* name, const char* prefix) const {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700525 std::string out;
chaviw85b44202020-07-24 11:46:21 -0700526 dump(out, name, prefix);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700527 ALOGD("%s", out.c_str());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800528}
529
Peiyong Linefefaac2018-08-17 12:27:51 -0700530} // namespace ui
531} // namespace android