blob: bc9e5cd5d757a5ea6ff3d62da45c528210f5c79d [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>
20#include <utils/String8.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <ui/Region.h>
22
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023#include "Transform.h"
24
25// ---------------------------------------------------------------------------
26
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027namespace android {
28
29// ---------------------------------------------------------------------------
30
Mathias Agopianeda65402010-02-22 03:15:57 -080031Transform::Transform() {
32 reset();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033}
34
35Transform::Transform(const Transform& other)
Mathias Agopianeda65402010-02-22 03:15:57 -080036 : mMatrix(other.mMatrix), mType(other.mType) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037}
38
Mathias Agopianeda65402010-02-22 03:15:57 -080039Transform::Transform(uint32_t orientation) {
40 set(orientation, 0, 0);
Chih-Chung Chang52e72002010-01-21 17:31:06 -080041}
42
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043Transform::~Transform() {
44}
45
Mathias Agopiand1296592010-03-09 19:17:47 -080046static const float EPSILON = 0.0f;
Mathias Agopianeda65402010-02-22 03:15:57 -080047
48bool Transform::isZero(float f) {
Mathias Agopiand1296592010-03-09 19:17:47 -080049 return fabs(f) <= EPSILON;
Mathias Agopianeda65402010-02-22 03:15:57 -080050}
51
Mathias Agopiand1296592010-03-09 19:17:47 -080052bool Transform::absIsOne(float f) {
53 return isZero(fabs(f) - 1.0f);
Mathias Agopianeda65402010-02-22 03:15:57 -080054}
55
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056Transform Transform::operator * (const Transform& rhs) const
57{
Mathias Agopianeda65402010-02-22 03:15:57 -080058 if (CC_LIKELY(mType == IDENTITY))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059 return rhs;
60
61 Transform r(*this);
Mathias Agopianeda65402010-02-22 03:15:57 -080062 if (rhs.mType == IDENTITY)
63 return r;
64
65 // TODO: we could use mType to optimize the matrix multiply
66 const mat33& A(mMatrix);
67 const mat33& B(rhs.mMatrix);
68 mat33& D(r.mMatrix);
69 for (int i=0 ; i<3 ; i++) {
70 const float v0 = A[0][i];
71 const float v1 = A[1][i];
72 const float v2 = A[2][i];
73 D[0][i] = v0*B[0][0] + v1*B[0][1] + v2*B[0][2];
74 D[1][i] = v0*B[1][0] + v1*B[1][1] + v2*B[1][2];
75 D[2][i] = v0*B[2][0] + v1*B[2][1] + v2*B[2][2];
76 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077 r.mType |= rhs.mType;
Mathias Agopianeda65402010-02-22 03:15:57 -080078
79 // TODO: we could recompute this value from r and rhs
80 r.mType &= 0xFF;
81 r.mType |= UNKNOWN_TYPE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082 return r;
83}
84
Mathias Agopianff2ed702013-09-01 21:36:12 -070085const vec3& Transform::operator [] (size_t i) const {
86 return mMatrix[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087}
88
Mathias Agopian41b6aab2011-08-30 18:51:54 -070089float Transform::tx() const {
90 return mMatrix[2][0];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091}
92
Mathias Agopian41b6aab2011-08-30 18:51:54 -070093float Transform::ty() const {
94 return mMatrix[2][1];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095}
96
97void Transform::reset() {
Mathias Agopianeda65402010-02-22 03:15:57 -080098 mType = IDENTITY;
99 for(int i=0 ; i<3 ; i++) {
100 vec3& v(mMatrix[i]);
101 for (int j=0 ; j<3 ; j++)
102 v[j] = ((i==j) ? 1.0f : 0.0f);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 }
104}
105
Mathias Agopianeda65402010-02-22 03:15:57 -0800106void Transform::set(float tx, float ty)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107{
Mathias Agopianeda65402010-02-22 03:15:57 -0800108 mMatrix[2][0] = tx;
109 mMatrix[2][1] = ty;
110 mMatrix[2][2] = 1.0f;
111
112 if (isZero(tx) && isZero(ty)) {
113 mType &= ~TRANSLATE;
114 } else {
115 mType |= TRANSLATE;
116 }
117}
118
119void Transform::set(float a, float b, float c, float d)
120{
121 mat33& M(mMatrix);
122 M[0][0] = a; M[1][0] = b;
123 M[0][1] = c; M[1][1] = d;
124 M[0][2] = 0; M[1][2] = 0;
125 mType = UNKNOWN_TYPE;
126}
127
Mathias Agopiand1296592010-03-09 19:17:47 -0800128status_t Transform::set(uint32_t flags, float w, float h)
Mathias Agopianeda65402010-02-22 03:15:57 -0800129{
Mathias Agopiand1296592010-03-09 19:17:47 -0800130 if (flags & ROT_INVALID) {
131 // that's not allowed!
132 reset();
133 return BAD_VALUE;
134 }
135
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700136 Transform H, V, R;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700137 if (flags & ROT_90) {
138 // w & h are inverted when rotating by 90 degrees
Peiyong Lin3db42342018-08-16 09:15:59 -0700139 std::swap(w, h);
Mathias Agopian883dffa2010-10-25 18:29:35 -0700140 }
141
Mathias Agopianeda65402010-02-22 03:15:57 -0800142 if (flags & FLIP_H) {
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700143 H.mType = (FLIP_H << 8) | SCALE;
144 H.mType |= isZero(w) ? IDENTITY : TRANSLATE;
145 mat33& M(H.mMatrix);
146 M[0][0] = -1;
147 M[2][0] = w;
Mathias Agopianeda65402010-02-22 03:15:57 -0800148 }
149
150 if (flags & FLIP_V) {
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700151 V.mType = (FLIP_V << 8) | SCALE;
152 V.mType |= isZero(h) ? IDENTITY : TRANSLATE;
153 mat33& M(V.mMatrix);
154 M[1][1] = -1;
155 M[2][1] = h;
Mathias Agopianeda65402010-02-22 03:15:57 -0800156 }
157
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700158 if (flags & ROT_90) {
Mathias Agopian883dffa2010-10-25 18:29:35 -0700159 const float original_w = h;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700160 R.mType = (ROT_90 << 8) | ROTATE;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700161 R.mType |= isZero(original_w) ? IDENTITY : TRANSLATE;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700162 mat33& M(R.mMatrix);
Mathias Agopian883dffa2010-10-25 18:29:35 -0700163 M[0][0] = 0; M[1][0] =-1; M[2][0] = original_w;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700164 M[0][1] = 1; M[1][1] = 0;
Mathias Agopianeda65402010-02-22 03:15:57 -0800165 }
166
Mathias Agopian883dffa2010-10-25 18:29:35 -0700167 *this = (R*(H*V));
Mathias Agopiand1296592010-03-09 19:17:47 -0800168 return NO_ERROR;
Mathias Agopianeda65402010-02-22 03:15:57 -0800169}
170
Mathias Agopianff2ed702013-09-01 21:36:12 -0700171vec2 Transform::transform(const vec2& v) const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800172 vec2 r;
173 const mat33& M(mMatrix);
174 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0];
175 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1];
176 return r;
177}
178
Mathias Agopianff2ed702013-09-01 21:36:12 -0700179vec3 Transform::transform(const vec3& v) const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800180 vec3 r;
181 const mat33& M(mMatrix);
182 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0]*v[2];
183 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1]*v[2];
184 r[2] = M[0][2]*v[0] + M[1][2]*v[1] + M[2][2]*v[2];
185 return r;
186}
187
Mathias Agopianff2ed702013-09-01 21:36:12 -0700188vec2 Transform::transform(int x, int y) const
Mathias Agopianeda65402010-02-22 03:15:57 -0800189{
Mathias Agopianff2ed702013-09-01 21:36:12 -0700190 return transform(vec2(x,y));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191}
192
193Rect Transform::makeBounds(int w, int h) const
194{
Mathias Agopianeda65402010-02-22 03:15:57 -0800195 return transform( Rect(w, h) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196}
197
Pablo Ceballos51450032016-08-03 10:20:45 -0700198Rect Transform::transform(const Rect& bounds, bool roundOutwards) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199{
200 Rect r;
Mathias Agopianeda65402010-02-22 03:15:57 -0800201 vec2 lt( bounds.left, bounds.top );
202 vec2 rt( bounds.right, bounds.top );
203 vec2 lb( bounds.left, bounds.bottom );
204 vec2 rb( bounds.right, bounds.bottom );
205
206 lt = transform(lt);
207 rt = transform(rt);
208 lb = transform(lb);
209 rb = transform(rb);
210
Pablo Ceballos51450032016-08-03 10:20:45 -0700211 if (roundOutwards) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700212 r.left = floorf(std::min({lt[0], rt[0], lb[0], rb[0]}));
213 r.top = floorf(std::min({lt[1], rt[1], lb[1], rb[1]}));
214 r.right = ceilf(std::max({lt[0], rt[0], lb[0], rb[0]}));
215 r.bottom = ceilf(std::max({lt[1], rt[1], lb[1], rb[1]}));
Pablo Ceballos51450032016-08-03 10:20:45 -0700216 } else {
Peiyong Lin3db42342018-08-16 09:15:59 -0700217 r.left = floorf(std::min({lt[0], rt[0], lb[0], rb[0]}) + 0.5f);
218 r.top = floorf(std::min({lt[1], rt[1], lb[1], rb[1]}) + 0.5f);
219 r.right = floorf(std::max({lt[0], rt[0], lb[0], rb[0]}) + 0.5f);
220 r.bottom = floorf(std::max({lt[1], rt[1], lb[1], rb[1]}) + 0.5f);
Pablo Ceballos51450032016-08-03 10:20:45 -0700221 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800222
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223 return r;
224}
225
Dan Stoza80d61162017-12-20 15:57:52 -0800226FloatRect Transform::transform(const FloatRect& bounds) const
227{
228 vec2 lt(bounds.left, bounds.top);
229 vec2 rt(bounds.right, bounds.top);
230 vec2 lb(bounds.left, bounds.bottom);
231 vec2 rb(bounds.right, bounds.bottom);
232
233 lt = transform(lt);
234 rt = transform(rt);
235 lb = transform(lb);
236 rb = transform(rb);
237
238 FloatRect r;
Peiyong Lin3db42342018-08-16 09:15:59 -0700239 r.left = std::min({lt[0], rt[0], lb[0], rb[0]});
240 r.top = std::min({lt[1], rt[1], lb[1], rb[1]});
241 r.right = std::max({lt[0], rt[0], lb[0], rb[0]});
242 r.bottom = std::max({lt[1], rt[1], lb[1], rb[1]});
Dan Stoza80d61162017-12-20 15:57:52 -0800243
244 return r;
245}
246
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247Region Transform::transform(const Region& reg) const
248{
249 Region out;
Dan Stoza22f7fc42016-05-10 16:19:53 -0700250 if (CC_UNLIKELY(type() > TRANSLATE)) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800251 if (CC_LIKELY(preserveRects())) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700252 Region::const_iterator it = reg.begin();
253 Region::const_iterator const end = reg.end();
254 while (it != end) {
255 out.orSelf(transform(*it++));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256 }
257 } else {
258 out.set(transform(reg.bounds()));
259 }
260 } else {
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700261 int xpos = floorf(tx() + 0.5f);
262 int ypos = floorf(ty() + 0.5f);
263 out = reg.translate(xpos, ypos);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264 }
265 return out;
266}
267
Mathias Agopianeda65402010-02-22 03:15:57 -0800268uint32_t Transform::type() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269{
Mathias Agopianeda65402010-02-22 03:15:57 -0800270 if (mType & UNKNOWN_TYPE) {
271 // recompute what this transform is
272
273 const mat33& M(mMatrix);
274 const float a = M[0][0];
275 const float b = M[1][0];
276 const float c = M[0][1];
277 const float d = M[1][1];
278 const float x = M[2][0];
279 const float y = M[2][1];
280
281 bool scale = false;
282 uint32_t flags = ROT_0;
283 if (isZero(b) && isZero(c)) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800284 if (a<0) flags |= FLIP_H;
285 if (d<0) flags |= FLIP_V;
286 if (!absIsOne(a) || !absIsOne(d)) {
287 scale = true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800288 }
289 } else if (isZero(a) && isZero(d)) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800290 flags |= ROT_90;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700291 if (b>0) flags |= FLIP_V;
292 if (c<0) flags |= FLIP_H;
Mathias Agopiand1296592010-03-09 19:17:47 -0800293 if (!absIsOne(b) || !absIsOne(c)) {
294 scale = true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800295 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296 } else {
Mathias Agopian29a367b2011-07-12 14:51:45 -0700297 // there is a skew component and/or a non 90 degrees rotation
Mathias Agopianeda65402010-02-22 03:15:57 -0800298 flags = ROT_INVALID;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800300
301 mType = flags << 8;
302 if (flags & ROT_INVALID) {
303 mType |= UNKNOWN;
304 } else {
305 if ((flags & ROT_90) || ((flags & ROT_180) == ROT_180))
306 mType |= ROTATE;
307 if (flags & FLIP_H)
308 mType ^= SCALE;
309 if (flags & FLIP_V)
310 mType ^= SCALE;
311 if (scale)
312 mType |= SCALE;
313 }
314
315 if (!isZero(x) || !isZero(y))
316 mType |= TRANSLATE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800318 return mType;
319}
320
Mathias Agopian3da16722013-02-28 17:12:07 -0800321Transform Transform::inverse() const {
322 // our 3x3 matrix is always of the form of a 2x2 transformation
323 // followed by a translation: T*M, therefore:
324 // (T*M)^-1 = M^-1 * T^-1
325 Transform result;
326 if (mType <= TRANSLATE) {
Michael Lentine28ea2172014-11-19 18:32:37 -0800327 // 1 0 0
328 // 0 1 0
329 // x y 1
Mathias Agopian3da16722013-02-28 17:12:07 -0800330 result = *this;
331 result.mMatrix[2][0] = -result.mMatrix[2][0];
332 result.mMatrix[2][1] = -result.mMatrix[2][1];
333 } else {
Michael Lentine28ea2172014-11-19 18:32:37 -0800334 // a c 0
335 // b d 0
336 // x y 1
Mathias Agopian3da16722013-02-28 17:12:07 -0800337 const mat33& M(mMatrix);
338 const float a = M[0][0];
339 const float b = M[1][0];
340 const float c = M[0][1];
341 const float d = M[1][1];
342 const float x = M[2][0];
343 const float y = M[2][1];
344
Mathias Agopian3da16722013-02-28 17:12:07 -0800345 const float idet = 1.0 / (a*d - b*c);
Michael Lentine28ea2172014-11-19 18:32:37 -0800346 result.mMatrix[0][0] = d*idet;
347 result.mMatrix[0][1] = -c*idet;
348 result.mMatrix[1][0] = -b*idet;
349 result.mMatrix[1][1] = a*idet;
350 result.mType = mType;
Mathias Agopian3da16722013-02-28 17:12:07 -0800351
Michael Lentine28ea2172014-11-19 18:32:37 -0800352 vec2 T(-x, -y);
353 T = result.transform(T);
354 result.mMatrix[2][0] = T[0];
355 result.mMatrix[2][1] = T[1];
Mathias Agopian3da16722013-02-28 17:12:07 -0800356 }
357 return result;
358}
359
Mathias Agopianeda65402010-02-22 03:15:57 -0800360uint32_t Transform::getType() const {
361 return type() & 0xFF;
362}
363
364uint32_t Transform::getOrientation() const
365{
366 return (type() >> 8) & 0xFF;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800367}
368
369bool Transform::preserveRects() const
370{
Mathias Agopianab7c13f2011-07-25 13:57:16 -0700371 return (getOrientation() & ROT_INVALID) ? false : true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800372}
373
374void Transform::dump(const char* name) const
375{
376 type(); // updates the type
377
378 String8 flags, type;
379 const mat33& m(mMatrix);
380 uint32_t orient = mType >> 8;
381
Mathias Agopiand1296592010-03-09 19:17:47 -0800382 if (orient&ROT_INVALID) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800383 flags.append("ROT_INVALID ");
Mathias Agopiand1296592010-03-09 19:17:47 -0800384 } else {
385 if (orient&ROT_90) {
386 flags.append("ROT_90 ");
387 } else {
388 flags.append("ROT_0 ");
389 }
390 if (orient&FLIP_V)
391 flags.append("FLIP_V ");
392 if (orient&FLIP_H)
393 flags.append("FLIP_H ");
394 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800395
Mathias Agopiand1296592010-03-09 19:17:47 -0800396 if (!(mType&(SCALE|ROTATE|TRANSLATE)))
397 type.append("IDENTITY ");
Mathias Agopianeda65402010-02-22 03:15:57 -0800398 if (mType&SCALE)
399 type.append("SCALE ");
400 if (mType&ROTATE)
401 type.append("ROTATE ");
402 if (mType&TRANSLATE)
403 type.append("TRANSLATE ");
404
Steve Block9d453682011-12-20 16:23:08 +0000405 ALOGD("%s 0x%08x (%s, %s)", name, mType, flags.string(), type.string());
406 ALOGD("%.4f %.4f %.4f", m[0][0], m[1][0], m[2][0]);
407 ALOGD("%.4f %.4f %.4f", m[0][1], m[1][1], m[2][1]);
408 ALOGD("%.4f %.4f %.4f", m[0][2], m[1][2], m[2][2]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800409}
410
chaviwa76b2712017-09-20 12:02:26 -0700411Transform::orientation_flags Transform::fromRotation(ISurfaceComposer::Rotation rotation) {
412 // Convert to surfaceflinger's internal rotation type.
413 switch (rotation) {
414 case ISurfaceComposer::eRotateNone:
415 return Transform::ROT_0;
416 case ISurfaceComposer::eRotate90:
417 return Transform::ROT_90;
418 case ISurfaceComposer::eRotate180:
419 return Transform::ROT_180;
420 case ISurfaceComposer::eRotate270:
421 return Transform::ROT_270;
422 default:
423 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
424 return Transform::ROT_0;
425 }
426}
427
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428// ---------------------------------------------------------------------------
429
430}; // namespace android