1 /*
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
17 #undef LOG_TAG
18 #define LOG_TAG "Transform"
19
20 #include <math.h>
21
22 #include <android-base/stringprintf.h>
23 #include <cutils/compiler.h>
24 #include <ui/Region.h>
25 #include <ui/Transform.h>
26 #include <utils/String8.h>
27
28 namespace android::ui {
29
Transform()30 Transform::Transform() {
31 reset();
32 }
33
Transform(const Transform & other)34 Transform::Transform(const Transform& other)
35 : mMatrix(other.mMatrix), mType(other.mType) {
36 }
37
Transform(uint32_t orientation,int w,int h)38 Transform::Transform(uint32_t orientation, int w, int h) {
39 set(orientation, w, h);
40 }
41
42 Transform::~Transform() = default;
43
44 static const float EPSILON = 0.0f;
45
isZero(float f)46 bool Transform::isZero(float f) {
47 return fabs(f) <= EPSILON;
48 }
49
absIsOne(float f)50 bool Transform::absIsOne(float f) {
51 return isZero(fabs(f) - 1.0f);
52 }
53
operator ==(const Transform & other) const54 bool 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];
60 }
61
operator *(const Transform & rhs) const62 Transform Transform::operator*(const Transform& rhs) const {
63 if (CC_LIKELY(mType == IDENTITY))
64 return rhs;
65
66 Transform r(*this);
67 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);
74 for (size_t i = 0; i < 3; i++) {
75 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 }
82 r.mType |= rhs.mType;
83
84 // TODO: we could recompute this value from r and rhs
85 r.mType &= 0xFF;
86 r.mType |= UNKNOWN_TYPE;
87 return r;
88 }
89
operator *(float value) const90 Transform 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
operator =(const Transform & other)103 Transform& Transform::operator=(const Transform& other) {
104 mMatrix = other.mMatrix;
105 mType = other.mType;
106 return *this;
107 }
108
operator [](size_t i) const109 const vec3& Transform::operator [] (size_t i) const {
110 return mMatrix[i];
111 }
112
tx() const113 float Transform::tx() const {
114 return mMatrix[2][0];
115 }
116
ty() const117 float Transform::ty() const {
118 return mMatrix[2][1];
119 }
120
dsdx() const121 float Transform::dsdx() const {
122 return mMatrix[0][0];
123 }
124
dtdx() const125 float Transform::dtdx() const {
126 return mMatrix[1][0];
127 }
128
dtdy() const129 float Transform::dtdy() const {
130 return mMatrix[0][1];
131 }
132
dsdy() const133 float Transform::dsdy() const {
134 return mMatrix[1][1];
135 }
136
det() const137 float Transform::det() const {
138 return mMatrix[0][0] * mMatrix[1][1] - mMatrix[0][1] * mMatrix[1][0];
139 }
140
getScaleX() const141 float Transform::getScaleX() const {
142 return sqrt((dsdx() * dsdx()) + (dtdx() * dtdx()));
143 }
144
getScaleY() const145 float Transform::getScaleY() const {
146 return sqrt((dtdy() * dtdy()) + (dsdy() * dsdy()));
147 }
148
reset()149 void Transform::reset() {
150 mType = IDENTITY;
151 for(size_t i = 0; i < 3; i++) {
152 vec3& v(mMatrix[i]);
153 for (size_t j = 0; j < 3; j++)
154 v[j] = ((i == j) ? 1.0f : 0.0f);
155 }
156 }
157
set(float tx,float ty)158 void Transform::set(float tx, float ty) {
159 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
set(float a,float b,float c,float d)170 void Transform::set(float a, float b, float c, float d) {
171 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
set(uint32_t flags,float w,float h)178 status_t Transform::set(uint32_t flags, float w, float h) {
179 if (flags & ROT_INVALID) {
180 // that's not allowed!
181 reset();
182 return BAD_VALUE;
183 }
184
185 Transform H, V, R;
186 if (flags & ROT_90) {
187 // w & h are inverted when rotating by 90 degrees
188 std::swap(w, h);
189 }
190
191 if (flags & FLIP_H) {
192 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;
197 }
198
199 if (flags & FLIP_V) {
200 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;
205 }
206
207 if (flags & ROT_90) {
208 const float original_w = h;
209 R.mType = (ROT_90 << 8) | ROTATE;
210 R.mType |= isZero(original_w) ? IDENTITY : TRANSLATE;
211 mat33& M(R.mMatrix);
212 M[0][0] = 0; M[1][0] =-1; M[2][0] = original_w;
213 M[0][1] = 1; M[1][1] = 0;
214 }
215
216 *this = (R*(H*V));
217 return NO_ERROR;
218 }
219
set(const std::array<float,9> & matrix)220 void 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
transform(const vec2 & v) const229 vec2 Transform::transform(const vec2& v) const {
230 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
transform(const vec3 & v) const237 vec3 Transform::transform(const vec3& v) const {
238 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
transform(float x,float y) const246 vec2 Transform::transform(float x, float y) const {
247 return transform(vec2(x, y));
248 }
249
makeBounds(int w,int h) const250 Rect Transform::makeBounds(int w, int h) const {
251 return transform( Rect(w, h) );
252 }
253
transform(const Rect & bounds,bool roundOutwards) const254 Rect Transform::transform(const Rect& bounds, bool roundOutwards) const {
255 Rect r;
256 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
266 if (roundOutwards) {
267 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]})));
271 } else {
272 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));
276 }
277
278 return r;
279 }
280
transform(const FloatRect & bounds) const281 FloatRect Transform::transform(const FloatRect& bounds) const {
282 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;
293 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]});
297
298 return r;
299 }
300
transform(const Region & reg) const301 Region Transform::transform(const Region& reg) const {
302 Region out;
303 if (CC_UNLIKELY(type() > TRANSLATE)) {
304 if (CC_LIKELY(preserveRects())) {
305 Region::const_iterator it = reg.begin();
306 Region::const_iterator const end = reg.end();
307 while (it != end) {
308 out.orSelf(transform(*it++));
309 }
310 } else {
311 out.set(transform(reg.bounds()));
312 }
313 } else {
314 int xpos = static_cast<int>(floorf(tx() + 0.5f));
315 int ypos = static_cast<int>(floorf(ty() + 0.5f));
316 out = reg.translate(xpos, ypos);
317 }
318 return out;
319 }
320
type() const321 uint32_t Transform::type() const {
322 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)) {
336 if (a<0) flags |= FLIP_H;
337 if (d<0) flags |= FLIP_V;
338 if (!absIsOne(a) || !absIsOne(d)) {
339 scale = true;
340 }
341 } else if (isZero(a) && isZero(d)) {
342 flags |= ROT_90;
343 if (b>0) flags |= FLIP_V;
344 if (c<0) flags |= FLIP_H;
345 if (!absIsOne(b) || !absIsOne(c)) {
346 scale = true;
347 }
348 } else {
349 // there is a skew component and/or a non 90 degrees rotation
350 flags = ROT_INVALID;
351 }
352
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;
369 }
370 return mType;
371 }
372
inverse() const373 Transform 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) {
379 // 1 0 0
380 // 0 1 0
381 // x y 1
382 result = *this;
383 result.mMatrix[2][0] = -result.mMatrix[2][0];
384 result.mMatrix[2][1] = -result.mMatrix[2][1];
385 } else {
386 // a c 0
387 // b d 0
388 // x y 1
389 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
397 const float idet = 1.0f / det();
398 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;
403 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 }
408
409 vec2 T(-x, -y);
410 T = result.transform(T);
411 result.mMatrix[2][0] = T[0];
412 result.mMatrix[2][1] = T[1];
413 }
414 return result;
415 }
416
getType() const417 uint32_t Transform::getType() const {
418 return type() & 0xFF;
419 }
420
getOrientation() const421 uint32_t Transform::getOrientation() const {
422 return (type() >> 8) & 0xFF;
423 }
424
preserveRects() const425 bool Transform::preserveRects() const {
426 return (getOrientation() & ROT_INVALID) ? false : true;
427 }
428
needsBilinearFiltering() const429 bool Transform::needsBilinearFiltering() const {
430 return (!preserveRects() || getType() >= ui::Transform::SCALE);
431 }
432
asMatrix4() const433 mat4 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
rotationToString(const uint32_t rotationFlags)464 static 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
transformToString(const uint32_t transform)484 static 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
dump(std::string & out,const char * name,const char * prefix) const500 void Transform::dump(std::string& out, const char* name, const char* prefix) const {
501 using android::base::StringAppendF;
502
503 type(); // Ensure the information in mType is up to date
504
505 const uint32_t type = mType;
506 const uint32_t orient = type >> 8;
507
508 out += prefix;
509 out += name;
510 out += " ";
511
512 if (orient & ROT_INVALID) {
513 StringAppendF(&out, "0x%08x ", orient);
514 }
515 out += "(" + rotationToString(orient) + ") ";
516
517 if (type & UNKNOWN) {
518 StringAppendF(&out, "0x%02x ", type);
519 }
520 out += "(" + transformToString(type) + ")\n";
521
522 if (type == IDENTITY) {
523 return;
524 }
525
526 for (size_t i = 0; i < 3; i++) {
527 StringAppendF(&out, "%s %.4f %.4f %.4f\n", prefix, static_cast<double>(mMatrix[0][i]),
528 static_cast<double>(mMatrix[1][i]), static_cast<double>(mMatrix[2][i]));
529 }
530 }
531
dump(const char * name,const char * prefix) const532 void Transform::dump(const char* name, const char* prefix) const {
533 std::string out;
534 dump(out, name, prefix);
535 ALOGD("%s", out.c_str());
536 }
537
538 } // namespace android::ui
539