blob: 8e949ec88031e441348b0916445359d66b40522c [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
19#include <cutils/compiler.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020#include <ui/Region.h>
Peiyong Linefefaac2018-08-17 12:27:51 -070021#include <ui/Transform.h>
22#include <utils/String8.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024namespace android {
Peiyong Linefefaac2018-08-17 12:27:51 -070025namespace 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
Mathias Agopianeda65402010-02-22 03:15:57 -080035Transform::Transform(uint32_t orientation) {
36 set(orientation, 0, 0);
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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051Transform Transform::operator * (const Transform& rhs) const
52{
Mathias Agopianeda65402010-02-22 03:15:57 -080053 if (CC_LIKELY(mType == IDENTITY))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054 return rhs;
55
56 Transform r(*this);
Mathias Agopianeda65402010-02-22 03:15:57 -080057 if (rhs.mType == IDENTITY)
58 return r;
59
60 // TODO: we could use mType to optimize the matrix multiply
61 const mat33& A(mMatrix);
62 const mat33& B(rhs.mMatrix);
63 mat33& D(r.mMatrix);
Peiyong Linefefaac2018-08-17 12:27:51 -070064 for (size_t i = 0; i < 3; i++) {
Mathias Agopianeda65402010-02-22 03:15:57 -080065 const float v0 = A[0][i];
66 const float v1 = A[1][i];
67 const float v2 = A[2][i];
68 D[0][i] = v0*B[0][0] + v1*B[0][1] + v2*B[0][2];
69 D[1][i] = v0*B[1][0] + v1*B[1][1] + v2*B[1][2];
70 D[2][i] = v0*B[2][0] + v1*B[2][1] + v2*B[2][2];
71 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072 r.mType |= rhs.mType;
Mathias Agopianeda65402010-02-22 03:15:57 -080073
74 // TODO: we could recompute this value from r and rhs
75 r.mType &= 0xFF;
76 r.mType |= UNKNOWN_TYPE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077 return r;
78}
79
Peiyong Linefefaac2018-08-17 12:27:51 -070080Transform& Transform::operator=(const Transform& other) {
81 mMatrix = other.mMatrix;
82 mType = other.mType;
83 return *this;
84}
85
Mathias Agopianff2ed702013-09-01 21:36:12 -070086const vec3& Transform::operator [] (size_t i) const {
87 return mMatrix[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088}
89
Mathias Agopian41b6aab2011-08-30 18:51:54 -070090float Transform::tx() const {
91 return mMatrix[2][0];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092}
93
Mathias Agopian41b6aab2011-08-30 18:51:54 -070094float Transform::ty() const {
95 return mMatrix[2][1];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096}
97
98void Transform::reset() {
Mathias Agopianeda65402010-02-22 03:15:57 -080099 mType = IDENTITY;
Peiyong Linefefaac2018-08-17 12:27:51 -0700100 for(size_t i = 0; i < 3; i++) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800101 vec3& v(mMatrix[i]);
Peiyong Linefefaac2018-08-17 12:27:51 -0700102 for (size_t j = 0; j < 3; j++)
103 v[j] = ((i == j) ? 1.0f : 0.0f);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 }
105}
106
Mathias Agopianeda65402010-02-22 03:15:57 -0800107void Transform::set(float tx, float ty)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108{
Mathias Agopianeda65402010-02-22 03:15:57 -0800109 mMatrix[2][0] = tx;
110 mMatrix[2][1] = ty;
111 mMatrix[2][2] = 1.0f;
112
113 if (isZero(tx) && isZero(ty)) {
114 mType &= ~TRANSLATE;
115 } else {
116 mType |= TRANSLATE;
117 }
118}
119
120void Transform::set(float a, float b, float c, float d)
121{
122 mat33& M(mMatrix);
123 M[0][0] = a; M[1][0] = b;
124 M[0][1] = c; M[1][1] = d;
125 M[0][2] = 0; M[1][2] = 0;
126 mType = UNKNOWN_TYPE;
127}
128
Mathias Agopiand1296592010-03-09 19:17:47 -0800129status_t Transform::set(uint32_t flags, float w, float h)
Mathias Agopianeda65402010-02-22 03:15:57 -0800130{
Mathias Agopiand1296592010-03-09 19:17:47 -0800131 if (flags & ROT_INVALID) {
132 // that's not allowed!
133 reset();
134 return BAD_VALUE;
135 }
136
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700137 Transform H, V, R;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700138 if (flags & ROT_90) {
139 // w & h are inverted when rotating by 90 degrees
Peiyong Lin3db42342018-08-16 09:15:59 -0700140 std::swap(w, h);
Mathias Agopian883dffa2010-10-25 18:29:35 -0700141 }
142
Mathias Agopianeda65402010-02-22 03:15:57 -0800143 if (flags & FLIP_H) {
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700144 H.mType = (FLIP_H << 8) | SCALE;
145 H.mType |= isZero(w) ? IDENTITY : TRANSLATE;
146 mat33& M(H.mMatrix);
147 M[0][0] = -1;
148 M[2][0] = w;
Mathias Agopianeda65402010-02-22 03:15:57 -0800149 }
150
151 if (flags & FLIP_V) {
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700152 V.mType = (FLIP_V << 8) | SCALE;
153 V.mType |= isZero(h) ? IDENTITY : TRANSLATE;
154 mat33& M(V.mMatrix);
155 M[1][1] = -1;
156 M[2][1] = h;
Mathias Agopianeda65402010-02-22 03:15:57 -0800157 }
158
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700159 if (flags & ROT_90) {
Mathias Agopian883dffa2010-10-25 18:29:35 -0700160 const float original_w = h;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700161 R.mType = (ROT_90 << 8) | ROTATE;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700162 R.mType |= isZero(original_w) ? IDENTITY : TRANSLATE;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700163 mat33& M(R.mMatrix);
Mathias Agopian883dffa2010-10-25 18:29:35 -0700164 M[0][0] = 0; M[1][0] =-1; M[2][0] = original_w;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700165 M[0][1] = 1; M[1][1] = 0;
Mathias Agopianeda65402010-02-22 03:15:57 -0800166 }
167
Mathias Agopian883dffa2010-10-25 18:29:35 -0700168 *this = (R*(H*V));
Mathias Agopiand1296592010-03-09 19:17:47 -0800169 return NO_ERROR;
Mathias Agopianeda65402010-02-22 03:15:57 -0800170}
171
Mathias Agopianff2ed702013-09-01 21:36:12 -0700172vec2 Transform::transform(const vec2& v) const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800173 vec2 r;
174 const mat33& M(mMatrix);
175 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0];
176 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1];
177 return r;
178}
179
Mathias Agopianff2ed702013-09-01 21:36:12 -0700180vec3 Transform::transform(const vec3& v) const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800181 vec3 r;
182 const mat33& M(mMatrix);
183 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0]*v[2];
184 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1]*v[2];
185 r[2] = M[0][2]*v[0] + M[1][2]*v[1] + M[2][2]*v[2];
186 return r;
187}
188
Mathias Agopianff2ed702013-09-01 21:36:12 -0700189vec2 Transform::transform(int x, int y) const
Mathias Agopianeda65402010-02-22 03:15:57 -0800190{
Mathias Agopianff2ed702013-09-01 21:36:12 -0700191 return transform(vec2(x,y));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192}
193
194Rect Transform::makeBounds(int w, int h) const
195{
Mathias Agopianeda65402010-02-22 03:15:57 -0800196 return transform( Rect(w, h) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197}
198
Pablo Ceballos51450032016-08-03 10:20:45 -0700199Rect Transform::transform(const Rect& bounds, bool roundOutwards) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200{
201 Rect r;
Mathias Agopianeda65402010-02-22 03:15:57 -0800202 vec2 lt( bounds.left, bounds.top );
203 vec2 rt( bounds.right, bounds.top );
204 vec2 lb( bounds.left, bounds.bottom );
205 vec2 rb( bounds.right, bounds.bottom );
206
207 lt = transform(lt);
208 rt = transform(rt);
209 lb = transform(lb);
210 rb = transform(rb);
211
Pablo Ceballos51450032016-08-03 10:20:45 -0700212 if (roundOutwards) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700213 r.left = static_cast<int32_t>(floorf(std::min({lt[0], rt[0], lb[0], rb[0]})));
214 r.top = static_cast<int32_t>(floorf(std::min({lt[1], rt[1], lb[1], rb[1]})));
215 r.right = static_cast<int32_t>(ceilf(std::max({lt[0], rt[0], lb[0], rb[0]})));
216 r.bottom = static_cast<int32_t>(ceilf(std::max({lt[1], rt[1], lb[1], rb[1]})));
Pablo Ceballos51450032016-08-03 10:20:45 -0700217 } else {
Peiyong Linefefaac2018-08-17 12:27:51 -0700218 r.left = static_cast<int32_t>(floorf(std::min({lt[0], rt[0], lb[0], rb[0]}) + 0.5f));
219 r.top = static_cast<int32_t>(floorf(std::min({lt[1], rt[1], lb[1], rb[1]}) + 0.5f));
220 r.right = static_cast<int32_t>(floorf(std::max({lt[0], rt[0], lb[0], rb[0]}) + 0.5f));
221 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 -0700222 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800223
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224 return r;
225}
226
Dan Stoza80d61162017-12-20 15:57:52 -0800227FloatRect Transform::transform(const FloatRect& bounds) const
228{
229 vec2 lt(bounds.left, bounds.top);
230 vec2 rt(bounds.right, bounds.top);
231 vec2 lb(bounds.left, bounds.bottom);
232 vec2 rb(bounds.right, bounds.bottom);
233
234 lt = transform(lt);
235 rt = transform(rt);
236 lb = transform(lb);
237 rb = transform(rb);
238
239 FloatRect r;
Peiyong Lin3db42342018-08-16 09:15:59 -0700240 r.left = std::min({lt[0], rt[0], lb[0], rb[0]});
241 r.top = std::min({lt[1], rt[1], lb[1], rb[1]});
242 r.right = std::max({lt[0], rt[0], lb[0], rb[0]});
243 r.bottom = std::max({lt[1], rt[1], lb[1], rb[1]});
Dan Stoza80d61162017-12-20 15:57:52 -0800244
245 return r;
246}
247
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248Region Transform::transform(const Region& reg) const
249{
250 Region out;
Dan Stoza22f7fc42016-05-10 16:19:53 -0700251 if (CC_UNLIKELY(type() > TRANSLATE)) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800252 if (CC_LIKELY(preserveRects())) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700253 Region::const_iterator it = reg.begin();
254 Region::const_iterator const end = reg.end();
255 while (it != end) {
256 out.orSelf(transform(*it++));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257 }
258 } else {
259 out.set(transform(reg.bounds()));
260 }
261 } else {
Peiyong Linefefaac2018-08-17 12:27:51 -0700262 int xpos = static_cast<int>(floorf(tx() + 0.5f));
263 int ypos = static_cast<int>(floorf(ty() + 0.5f));
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700264 out = reg.translate(xpos, ypos);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265 }
266 return out;
267}
268
Mathias Agopianeda65402010-02-22 03:15:57 -0800269uint32_t Transform::type() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270{
Mathias Agopianeda65402010-02-22 03:15:57 -0800271 if (mType & UNKNOWN_TYPE) {
272 // recompute what this transform is
273
274 const mat33& M(mMatrix);
275 const float a = M[0][0];
276 const float b = M[1][0];
277 const float c = M[0][1];
278 const float d = M[1][1];
279 const float x = M[2][0];
280 const float y = M[2][1];
281
282 bool scale = false;
283 uint32_t flags = ROT_0;
284 if (isZero(b) && isZero(c)) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800285 if (a<0) flags |= FLIP_H;
286 if (d<0) flags |= FLIP_V;
287 if (!absIsOne(a) || !absIsOne(d)) {
288 scale = true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800289 }
290 } else if (isZero(a) && isZero(d)) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800291 flags |= ROT_90;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700292 if (b>0) flags |= FLIP_V;
293 if (c<0) flags |= FLIP_H;
Mathias Agopiand1296592010-03-09 19:17:47 -0800294 if (!absIsOne(b) || !absIsOne(c)) {
295 scale = true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800296 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800297 } else {
Mathias Agopian29a367b2011-07-12 14:51:45 -0700298 // there is a skew component and/or a non 90 degrees rotation
Mathias Agopianeda65402010-02-22 03:15:57 -0800299 flags = ROT_INVALID;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800301
302 mType = flags << 8;
303 if (flags & ROT_INVALID) {
304 mType |= UNKNOWN;
305 } else {
306 if ((flags & ROT_90) || ((flags & ROT_180) == ROT_180))
307 mType |= ROTATE;
308 if (flags & FLIP_H)
309 mType ^= SCALE;
310 if (flags & FLIP_V)
311 mType ^= SCALE;
312 if (scale)
313 mType |= SCALE;
314 }
315
316 if (!isZero(x) || !isZero(y))
317 mType |= TRANSLATE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800319 return mType;
320}
321
Mathias Agopian3da16722013-02-28 17:12:07 -0800322Transform Transform::inverse() const {
323 // our 3x3 matrix is always of the form of a 2x2 transformation
324 // followed by a translation: T*M, therefore:
325 // (T*M)^-1 = M^-1 * T^-1
326 Transform result;
327 if (mType <= TRANSLATE) {
Michael Lentine28ea2172014-11-19 18:32:37 -0800328 // 1 0 0
329 // 0 1 0
330 // x y 1
Mathias Agopian3da16722013-02-28 17:12:07 -0800331 result = *this;
332 result.mMatrix[2][0] = -result.mMatrix[2][0];
333 result.mMatrix[2][1] = -result.mMatrix[2][1];
334 } else {
Michael Lentine28ea2172014-11-19 18:32:37 -0800335 // a c 0
336 // b d 0
337 // x y 1
Mathias Agopian3da16722013-02-28 17:12:07 -0800338 const mat33& M(mMatrix);
339 const float a = M[0][0];
340 const float b = M[1][0];
341 const float c = M[0][1];
342 const float d = M[1][1];
343 const float x = M[2][0];
344 const float y = M[2][1];
345
Peiyong Linefefaac2018-08-17 12:27:51 -0700346 const float idet = 1.0f / (a*d - b*c);
Michael Lentine28ea2172014-11-19 18:32:37 -0800347 result.mMatrix[0][0] = d*idet;
348 result.mMatrix[0][1] = -c*idet;
349 result.mMatrix[1][0] = -b*idet;
350 result.mMatrix[1][1] = a*idet;
351 result.mType = mType;
Mathias Agopian3da16722013-02-28 17:12:07 -0800352
Michael Lentine28ea2172014-11-19 18:32:37 -0800353 vec2 T(-x, -y);
354 T = result.transform(T);
355 result.mMatrix[2][0] = T[0];
356 result.mMatrix[2][1] = T[1];
Mathias Agopian3da16722013-02-28 17:12:07 -0800357 }
358 return result;
359}
360
Mathias Agopianeda65402010-02-22 03:15:57 -0800361uint32_t Transform::getType() const {
362 return type() & 0xFF;
363}
364
365uint32_t Transform::getOrientation() const
366{
367 return (type() >> 8) & 0xFF;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800368}
369
370bool Transform::preserveRects() const
371{
Mathias Agopianab7c13f2011-07-25 13:57:16 -0700372 return (getOrientation() & ROT_INVALID) ? false : true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800373}
374
375void Transform::dump(const char* name) const
376{
377 type(); // updates the type
378
379 String8 flags, type;
380 const mat33& m(mMatrix);
381 uint32_t orient = mType >> 8;
382
Mathias Agopiand1296592010-03-09 19:17:47 -0800383 if (orient&ROT_INVALID) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800384 flags.append("ROT_INVALID ");
Mathias Agopiand1296592010-03-09 19:17:47 -0800385 } else {
386 if (orient&ROT_90) {
387 flags.append("ROT_90 ");
388 } else {
389 flags.append("ROT_0 ");
390 }
391 if (orient&FLIP_V)
392 flags.append("FLIP_V ");
393 if (orient&FLIP_H)
394 flags.append("FLIP_H ");
395 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800396
Mathias Agopiand1296592010-03-09 19:17:47 -0800397 if (!(mType&(SCALE|ROTATE|TRANSLATE)))
398 type.append("IDENTITY ");
Mathias Agopianeda65402010-02-22 03:15:57 -0800399 if (mType&SCALE)
400 type.append("SCALE ");
401 if (mType&ROTATE)
402 type.append("ROTATE ");
403 if (mType&TRANSLATE)
404 type.append("TRANSLATE ");
405
Steve Block9d453682011-12-20 16:23:08 +0000406 ALOGD("%s 0x%08x (%s, %s)", name, mType, flags.string(), type.string());
Peiyong Linefefaac2018-08-17 12:27:51 -0700407 ALOGD("%.4f %.4f %.4f", static_cast<double>(m[0][0]), static_cast<double>(m[1][0]),
408 static_cast<double>(m[2][0]));
409 ALOGD("%.4f %.4f %.4f", static_cast<double>(m[0][1]), static_cast<double>(m[1][1]),
410 static_cast<double>(m[2][1]));
411 ALOGD("%.4f %.4f %.4f", static_cast<double>(m[0][2]), static_cast<double>(m[1][2]),
412 static_cast<double>(m[2][2]));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800413}
414
Peiyong Linefefaac2018-08-17 12:27:51 -0700415} // namespace ui
416} // namespace android