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 #include <math.h>
18
19 #include <android-base/stringprintf.h>
20 #include <cutils/compiler.h>
21 #include <ui/Region.h>
22 #include <ui/Transform.h>
23 #include <utils/String8.h>
24
25 namespace android {
26 namespace ui {
27
Transform()28 Transform::Transform() {
29 reset();
30 }
31
Transform(const Transform & other)32 Transform::Transform(const Transform& other)
33 : mMatrix(other.mMatrix), mType(other.mType) {
34 }
35
Transform(uint32_t orientation,int w,int h)36 Transform::Transform(uint32_t orientation, int w, int h) {
37 set(orientation, w, h);
38 }
39
40 Transform::~Transform() = default;
41
42 static const float EPSILON = 0.0f;
43
isZero(float f)44 bool Transform::isZero(float f) {
45 return fabs(f) <= EPSILON;
46 }
47
absIsOne(float f)48 bool Transform::absIsOne(float f) {
49 return isZero(fabs(f) - 1.0f);
50 }
51
operator ==(const Transform & other) const52 bool 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];
58 ;
59 }
60
operator *(const Transform & rhs) const61 Transform Transform::operator * (const Transform& rhs) const
62 {
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 =(const Transform & other)90 Transform& Transform::operator=(const Transform& other) {
91 mMatrix = other.mMatrix;
92 mType = other.mType;
93 return *this;
94 }
95
operator [](size_t i) const96 const vec3& Transform::operator [] (size_t i) const {
97 return mMatrix[i];
98 }
99
tx() const100 float Transform::tx() const {
101 return mMatrix[2][0];
102 }
103
ty() const104 float Transform::ty() const {
105 return mMatrix[2][1];
106 }
107
sx() const108 float Transform::sx() const {
109 return mMatrix[0][0];
110 }
111
sy() const112 float Transform::sy() const {
113 return mMatrix[1][1];
114 }
115
reset()116 void Transform::reset() {
117 mType = IDENTITY;
118 for(size_t i = 0; i < 3; i++) {
119 vec3& v(mMatrix[i]);
120 for (size_t j = 0; j < 3; j++)
121 v[j] = ((i == j) ? 1.0f : 0.0f);
122 }
123 }
124
set(float tx,float ty)125 void Transform::set(float tx, float ty)
126 {
127 mMatrix[2][0] = tx;
128 mMatrix[2][1] = ty;
129 mMatrix[2][2] = 1.0f;
130
131 if (isZero(tx) && isZero(ty)) {
132 mType &= ~TRANSLATE;
133 } else {
134 mType |= TRANSLATE;
135 }
136 }
137
set(float a,float b,float c,float d)138 void Transform::set(float a, float b, float c, float d)
139 {
140 mat33& M(mMatrix);
141 M[0][0] = a; M[1][0] = b;
142 M[0][1] = c; M[1][1] = d;
143 M[0][2] = 0; M[1][2] = 0;
144 mType = UNKNOWN_TYPE;
145 }
146
set(uint32_t flags,float w,float h)147 status_t Transform::set(uint32_t flags, float w, float h)
148 {
149 if (flags & ROT_INVALID) {
150 // that's not allowed!
151 reset();
152 return BAD_VALUE;
153 }
154
155 Transform H, V, R;
156 if (flags & ROT_90) {
157 // w & h are inverted when rotating by 90 degrees
158 std::swap(w, h);
159 }
160
161 if (flags & FLIP_H) {
162 H.mType = (FLIP_H << 8) | SCALE;
163 H.mType |= isZero(w) ? IDENTITY : TRANSLATE;
164 mat33& M(H.mMatrix);
165 M[0][0] = -1;
166 M[2][0] = w;
167 }
168
169 if (flags & FLIP_V) {
170 V.mType = (FLIP_V << 8) | SCALE;
171 V.mType |= isZero(h) ? IDENTITY : TRANSLATE;
172 mat33& M(V.mMatrix);
173 M[1][1] = -1;
174 M[2][1] = h;
175 }
176
177 if (flags & ROT_90) {
178 const float original_w = h;
179 R.mType = (ROT_90 << 8) | ROTATE;
180 R.mType |= isZero(original_w) ? IDENTITY : TRANSLATE;
181 mat33& M(R.mMatrix);
182 M[0][0] = 0; M[1][0] =-1; M[2][0] = original_w;
183 M[0][1] = 1; M[1][1] = 0;
184 }
185
186 *this = (R*(H*V));
187 return NO_ERROR;
188 }
189
transform(const vec2 & v) const190 vec2 Transform::transform(const vec2& v) const {
191 vec2 r;
192 const mat33& M(mMatrix);
193 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0];
194 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1];
195 return r;
196 }
197
transform(const vec3 & v) const198 vec3 Transform::transform(const vec3& v) const {
199 vec3 r;
200 const mat33& M(mMatrix);
201 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0]*v[2];
202 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1]*v[2];
203 r[2] = M[0][2]*v[0] + M[1][2]*v[1] + M[2][2]*v[2];
204 return r;
205 }
206
transform(int x,int y) const207 vec2 Transform::transform(int x, int y) const
208 {
209 return transform(vec2(x,y));
210 }
211
makeBounds(int w,int h) const212 Rect Transform::makeBounds(int w, int h) const
213 {
214 return transform( Rect(w, h) );
215 }
216
transform(const Rect & bounds,bool roundOutwards) const217 Rect Transform::transform(const Rect& bounds, bool roundOutwards) const
218 {
219 Rect r;
220 vec2 lt( bounds.left, bounds.top );
221 vec2 rt( bounds.right, bounds.top );
222 vec2 lb( bounds.left, bounds.bottom );
223 vec2 rb( bounds.right, bounds.bottom );
224
225 lt = transform(lt);
226 rt = transform(rt);
227 lb = transform(lb);
228 rb = transform(rb);
229
230 if (roundOutwards) {
231 r.left = static_cast<int32_t>(floorf(std::min({lt[0], rt[0], lb[0], rb[0]})));
232 r.top = static_cast<int32_t>(floorf(std::min({lt[1], rt[1], lb[1], rb[1]})));
233 r.right = static_cast<int32_t>(ceilf(std::max({lt[0], rt[0], lb[0], rb[0]})));
234 r.bottom = static_cast<int32_t>(ceilf(std::max({lt[1], rt[1], lb[1], rb[1]})));
235 } else {
236 r.left = static_cast<int32_t>(floorf(std::min({lt[0], rt[0], lb[0], rb[0]}) + 0.5f));
237 r.top = static_cast<int32_t>(floorf(std::min({lt[1], rt[1], lb[1], rb[1]}) + 0.5f));
238 r.right = static_cast<int32_t>(floorf(std::max({lt[0], rt[0], lb[0], rb[0]}) + 0.5f));
239 r.bottom = static_cast<int32_t>(floorf(std::max({lt[1], rt[1], lb[1], rb[1]}) + 0.5f));
240 }
241
242 return r;
243 }
244
transform(const FloatRect & bounds) const245 FloatRect Transform::transform(const FloatRect& bounds) const
246 {
247 vec2 lt(bounds.left, bounds.top);
248 vec2 rt(bounds.right, bounds.top);
249 vec2 lb(bounds.left, bounds.bottom);
250 vec2 rb(bounds.right, bounds.bottom);
251
252 lt = transform(lt);
253 rt = transform(rt);
254 lb = transform(lb);
255 rb = transform(rb);
256
257 FloatRect r;
258 r.left = std::min({lt[0], rt[0], lb[0], rb[0]});
259 r.top = std::min({lt[1], rt[1], lb[1], rb[1]});
260 r.right = std::max({lt[0], rt[0], lb[0], rb[0]});
261 r.bottom = std::max({lt[1], rt[1], lb[1], rb[1]});
262
263 return r;
264 }
265
transform(const Region & reg) const266 Region Transform::transform(const Region& reg) const
267 {
268 Region out;
269 if (CC_UNLIKELY(type() > TRANSLATE)) {
270 if (CC_LIKELY(preserveRects())) {
271 Region::const_iterator it = reg.begin();
272 Region::const_iterator const end = reg.end();
273 while (it != end) {
274 out.orSelf(transform(*it++));
275 }
276 } else {
277 out.set(transform(reg.bounds()));
278 }
279 } else {
280 int xpos = static_cast<int>(floorf(tx() + 0.5f));
281 int ypos = static_cast<int>(floorf(ty() + 0.5f));
282 out = reg.translate(xpos, ypos);
283 }
284 return out;
285 }
286
type() const287 uint32_t Transform::type() const
288 {
289 if (mType & UNKNOWN_TYPE) {
290 // recompute what this transform is
291
292 const mat33& M(mMatrix);
293 const float a = M[0][0];
294 const float b = M[1][0];
295 const float c = M[0][1];
296 const float d = M[1][1];
297 const float x = M[2][0];
298 const float y = M[2][1];
299
300 bool scale = false;
301 uint32_t flags = ROT_0;
302 if (isZero(b) && isZero(c)) {
303 if (a<0) flags |= FLIP_H;
304 if (d<0) flags |= FLIP_V;
305 if (!absIsOne(a) || !absIsOne(d)) {
306 scale = true;
307 }
308 } else if (isZero(a) && isZero(d)) {
309 flags |= ROT_90;
310 if (b>0) flags |= FLIP_V;
311 if (c<0) flags |= FLIP_H;
312 if (!absIsOne(b) || !absIsOne(c)) {
313 scale = true;
314 }
315 } else {
316 // there is a skew component and/or a non 90 degrees rotation
317 flags = ROT_INVALID;
318 }
319
320 mType = flags << 8;
321 if (flags & ROT_INVALID) {
322 mType |= UNKNOWN;
323 } else {
324 if ((flags & ROT_90) || ((flags & ROT_180) == ROT_180))
325 mType |= ROTATE;
326 if (flags & FLIP_H)
327 mType ^= SCALE;
328 if (flags & FLIP_V)
329 mType ^= SCALE;
330 if (scale)
331 mType |= SCALE;
332 }
333
334 if (!isZero(x) || !isZero(y))
335 mType |= TRANSLATE;
336 }
337 return mType;
338 }
339
inverse() const340 Transform Transform::inverse() const {
341 // our 3x3 matrix is always of the form of a 2x2 transformation
342 // followed by a translation: T*M, therefore:
343 // (T*M)^-1 = M^-1 * T^-1
344 Transform result;
345 if (mType <= TRANSLATE) {
346 // 1 0 0
347 // 0 1 0
348 // x y 1
349 result = *this;
350 result.mMatrix[2][0] = -result.mMatrix[2][0];
351 result.mMatrix[2][1] = -result.mMatrix[2][1];
352 } else {
353 // a c 0
354 // b d 0
355 // x y 1
356 const mat33& M(mMatrix);
357 const float a = M[0][0];
358 const float b = M[1][0];
359 const float c = M[0][1];
360 const float d = M[1][1];
361 const float x = M[2][0];
362 const float y = M[2][1];
363
364 const float idet = 1.0f / (a*d - b*c);
365 result.mMatrix[0][0] = d*idet;
366 result.mMatrix[0][1] = -c*idet;
367 result.mMatrix[1][0] = -b*idet;
368 result.mMatrix[1][1] = a*idet;
369 result.mType = mType;
370
371 vec2 T(-x, -y);
372 T = result.transform(T);
373 result.mMatrix[2][0] = T[0];
374 result.mMatrix[2][1] = T[1];
375 }
376 return result;
377 }
378
getType() const379 uint32_t Transform::getType() const {
380 return type() & 0xFF;
381 }
382
getOrientation() const383 uint32_t Transform::getOrientation() const
384 {
385 return (type() >> 8) & 0xFF;
386 }
387
preserveRects() const388 bool Transform::preserveRects() const
389 {
390 return (getOrientation() & ROT_INVALID) ? false : true;
391 }
392
asMatrix4() const393 mat4 Transform::asMatrix4() const {
394 // Internally Transform uses a 3x3 matrix since the transform is meant for
395 // two-dimensional values. An equivalent 4x4 matrix means inserting an extra
396 // row and column which adds as an identity transform on the third
397 // dimension.
398
399 mat4 m = mat4{mat4::NO_INIT}; // NO_INIT since we explicitly set every element
400
401 m[0][0] = mMatrix[0][0];
402 m[0][1] = mMatrix[0][1];
403 m[0][2] = 0.f;
404 m[0][3] = mMatrix[0][2];
405
406 m[1][0] = mMatrix[1][0];
407 m[1][1] = mMatrix[1][1];
408 m[1][2] = 0.f;
409 m[1][3] = mMatrix[1][2];
410
411 m[2][0] = 0.f;
412 m[2][1] = 0.f;
413 m[2][2] = 1.f;
414 m[2][3] = 0.f;
415
416 m[3][0] = mMatrix[2][0];
417 m[3][1] = mMatrix[2][1];
418 m[3][2] = 0.f;
419 m[3][3] = mMatrix[2][2];
420
421 return m;
422 }
423
dump(std::string & out,const char * name) const424 void Transform::dump(std::string& out, const char* name) const {
425 using android::base::StringAppendF;
426
427 type(); // Ensure the information in mType is up to date
428
429 const uint32_t type = mType;
430 const uint32_t orient = type >> 8;
431
432 StringAppendF(&out, "%s 0x%08x (", name, orient);
433
434 if (orient & ROT_INVALID) {
435 out.append("ROT_INVALID ");
436 } else {
437 if (orient & ROT_90) {
438 out.append("ROT_90 ");
439 } else {
440 out.append("ROT_0 ");
441 }
442 if (orient & FLIP_V) out.append("FLIP_V ");
443 if (orient & FLIP_H) out.append("FLIP_H ");
444 }
445
446 StringAppendF(&out, ") 0x%02x (", type);
447
448 if (!(type & (SCALE | ROTATE | TRANSLATE))) out.append("IDENTITY ");
449 if (type & SCALE) out.append("SCALE ");
450 if (type & ROTATE) out.append("ROTATE ");
451 if (type & TRANSLATE) out.append("TRANSLATE ");
452
453 out.append(")\n");
454
455 for (size_t i = 0; i < 3; i++) {
456 StringAppendF(&out, " %.4f %.4f %.4f\n", static_cast<double>(mMatrix[0][i]),
457 static_cast<double>(mMatrix[1][i]), static_cast<double>(mMatrix[2][i]));
458 }
459 }
460
dump(const char * name) const461 void Transform::dump(const char* name) const {
462 std::string out;
463 dump(out, name);
464 ALOGD("%s", out.c_str());
465 }
466
467 } // namespace ui
468 } // namespace android
469