blob: 25128effafbcd9dd4b2f41b8131f4ce2287cea19 [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
Robert Carre07e1032018-11-26 12:55:53 -080098float Transform::sx() const {
99 return mMatrix[0][0];
100}
101
102float Transform::sy() const {
103 return mMatrix[1][1];
104}
105
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106void Transform::reset() {
Mathias Agopianeda65402010-02-22 03:15:57 -0800107 mType = IDENTITY;
Peiyong Linefefaac2018-08-17 12:27:51 -0700108 for(size_t i = 0; i < 3; i++) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800109 vec3& v(mMatrix[i]);
Peiyong Linefefaac2018-08-17 12:27:51 -0700110 for (size_t j = 0; j < 3; j++)
111 v[j] = ((i == j) ? 1.0f : 0.0f);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112 }
113}
114
Mathias Agopianeda65402010-02-22 03:15:57 -0800115void Transform::set(float tx, float ty)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116{
Mathias Agopianeda65402010-02-22 03:15:57 -0800117 mMatrix[2][0] = tx;
118 mMatrix[2][1] = ty;
119 mMatrix[2][2] = 1.0f;
120
121 if (isZero(tx) && isZero(ty)) {
122 mType &= ~TRANSLATE;
123 } else {
124 mType |= TRANSLATE;
125 }
126}
127
128void Transform::set(float a, float b, float c, float d)
129{
130 mat33& M(mMatrix);
131 M[0][0] = a; M[1][0] = b;
132 M[0][1] = c; M[1][1] = d;
133 M[0][2] = 0; M[1][2] = 0;
134 mType = UNKNOWN_TYPE;
135}
136
Mathias Agopiand1296592010-03-09 19:17:47 -0800137status_t Transform::set(uint32_t flags, float w, float h)
Mathias Agopianeda65402010-02-22 03:15:57 -0800138{
Mathias Agopiand1296592010-03-09 19:17:47 -0800139 if (flags & ROT_INVALID) {
140 // that's not allowed!
141 reset();
142 return BAD_VALUE;
143 }
144
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700145 Transform H, V, R;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700146 if (flags & ROT_90) {
147 // w & h are inverted when rotating by 90 degrees
Peiyong Lin3db42342018-08-16 09:15:59 -0700148 std::swap(w, h);
Mathias Agopian883dffa2010-10-25 18:29:35 -0700149 }
150
Mathias Agopianeda65402010-02-22 03:15:57 -0800151 if (flags & FLIP_H) {
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700152 H.mType = (FLIP_H << 8) | SCALE;
153 H.mType |= isZero(w) ? IDENTITY : TRANSLATE;
154 mat33& M(H.mMatrix);
155 M[0][0] = -1;
156 M[2][0] = w;
Mathias Agopianeda65402010-02-22 03:15:57 -0800157 }
158
159 if (flags & FLIP_V) {
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700160 V.mType = (FLIP_V << 8) | SCALE;
161 V.mType |= isZero(h) ? IDENTITY : TRANSLATE;
162 mat33& M(V.mMatrix);
163 M[1][1] = -1;
164 M[2][1] = h;
Mathias Agopianeda65402010-02-22 03:15:57 -0800165 }
166
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700167 if (flags & ROT_90) {
Mathias Agopian883dffa2010-10-25 18:29:35 -0700168 const float original_w = h;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700169 R.mType = (ROT_90 << 8) | ROTATE;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700170 R.mType |= isZero(original_w) ? IDENTITY : TRANSLATE;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700171 mat33& M(R.mMatrix);
Mathias Agopian883dffa2010-10-25 18:29:35 -0700172 M[0][0] = 0; M[1][0] =-1; M[2][0] = original_w;
Mathias Agopian0694d0f2010-10-24 14:53:05 -0700173 M[0][1] = 1; M[1][1] = 0;
Mathias Agopianeda65402010-02-22 03:15:57 -0800174 }
175
Mathias Agopian883dffa2010-10-25 18:29:35 -0700176 *this = (R*(H*V));
Mathias Agopiand1296592010-03-09 19:17:47 -0800177 return NO_ERROR;
Mathias Agopianeda65402010-02-22 03:15:57 -0800178}
179
Mathias Agopianff2ed702013-09-01 21:36:12 -0700180vec2 Transform::transform(const vec2& v) const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800181 vec2 r;
182 const mat33& M(mMatrix);
183 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0];
184 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1];
185 return r;
186}
187
Mathias Agopianff2ed702013-09-01 21:36:12 -0700188vec3 Transform::transform(const vec3& v) const {
Mathias Agopianeda65402010-02-22 03:15:57 -0800189 vec3 r;
190 const mat33& M(mMatrix);
191 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0]*v[2];
192 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1]*v[2];
193 r[2] = M[0][2]*v[0] + M[1][2]*v[1] + M[2][2]*v[2];
194 return r;
195}
196
Mathias Agopianff2ed702013-09-01 21:36:12 -0700197vec2 Transform::transform(int x, int y) const
Mathias Agopianeda65402010-02-22 03:15:57 -0800198{
Mathias Agopianff2ed702013-09-01 21:36:12 -0700199 return transform(vec2(x,y));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200}
201
202Rect Transform::makeBounds(int w, int h) const
203{
Mathias Agopianeda65402010-02-22 03:15:57 -0800204 return transform( Rect(w, h) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205}
206
Pablo Ceballos51450032016-08-03 10:20:45 -0700207Rect Transform::transform(const Rect& bounds, bool roundOutwards) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208{
209 Rect r;
Mathias Agopianeda65402010-02-22 03:15:57 -0800210 vec2 lt( bounds.left, bounds.top );
211 vec2 rt( bounds.right, bounds.top );
212 vec2 lb( bounds.left, bounds.bottom );
213 vec2 rb( bounds.right, bounds.bottom );
214
215 lt = transform(lt);
216 rt = transform(rt);
217 lb = transform(lb);
218 rb = transform(rb);
219
Pablo Ceballos51450032016-08-03 10:20:45 -0700220 if (roundOutwards) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700221 r.left = static_cast<int32_t>(floorf(std::min({lt[0], rt[0], lb[0], rb[0]})));
222 r.top = static_cast<int32_t>(floorf(std::min({lt[1], rt[1], lb[1], rb[1]})));
223 r.right = static_cast<int32_t>(ceilf(std::max({lt[0], rt[0], lb[0], rb[0]})));
224 r.bottom = static_cast<int32_t>(ceilf(std::max({lt[1], rt[1], lb[1], rb[1]})));
Pablo Ceballos51450032016-08-03 10:20:45 -0700225 } else {
Peiyong Linefefaac2018-08-17 12:27:51 -0700226 r.left = static_cast<int32_t>(floorf(std::min({lt[0], rt[0], lb[0], rb[0]}) + 0.5f));
227 r.top = static_cast<int32_t>(floorf(std::min({lt[1], rt[1], lb[1], rb[1]}) + 0.5f));
228 r.right = static_cast<int32_t>(floorf(std::max({lt[0], rt[0], lb[0], rb[0]}) + 0.5f));
229 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 -0700230 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800231
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 return r;
233}
234
Dan Stoza80d61162017-12-20 15:57:52 -0800235FloatRect Transform::transform(const FloatRect& bounds) const
236{
237 vec2 lt(bounds.left, bounds.top);
238 vec2 rt(bounds.right, bounds.top);
239 vec2 lb(bounds.left, bounds.bottom);
240 vec2 rb(bounds.right, bounds.bottom);
241
242 lt = transform(lt);
243 rt = transform(rt);
244 lb = transform(lb);
245 rb = transform(rb);
246
247 FloatRect r;
Peiyong Lin3db42342018-08-16 09:15:59 -0700248 r.left = std::min({lt[0], rt[0], lb[0], rb[0]});
249 r.top = std::min({lt[1], rt[1], lb[1], rb[1]});
250 r.right = std::max({lt[0], rt[0], lb[0], rb[0]});
251 r.bottom = std::max({lt[1], rt[1], lb[1], rb[1]});
Dan Stoza80d61162017-12-20 15:57:52 -0800252
253 return r;
254}
255
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256Region Transform::transform(const Region& reg) const
257{
258 Region out;
Dan Stoza22f7fc42016-05-10 16:19:53 -0700259 if (CC_UNLIKELY(type() > TRANSLATE)) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800260 if (CC_LIKELY(preserveRects())) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700261 Region::const_iterator it = reg.begin();
262 Region::const_iterator const end = reg.end();
263 while (it != end) {
264 out.orSelf(transform(*it++));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265 }
266 } else {
267 out.set(transform(reg.bounds()));
268 }
269 } else {
Peiyong Linefefaac2018-08-17 12:27:51 -0700270 int xpos = static_cast<int>(floorf(tx() + 0.5f));
271 int ypos = static_cast<int>(floorf(ty() + 0.5f));
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700272 out = reg.translate(xpos, ypos);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273 }
274 return out;
275}
276
Mathias Agopianeda65402010-02-22 03:15:57 -0800277uint32_t Transform::type() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278{
Mathias Agopianeda65402010-02-22 03:15:57 -0800279 if (mType & UNKNOWN_TYPE) {
280 // recompute what this transform is
281
282 const mat33& M(mMatrix);
283 const float a = M[0][0];
284 const float b = M[1][0];
285 const float c = M[0][1];
286 const float d = M[1][1];
287 const float x = M[2][0];
288 const float y = M[2][1];
289
290 bool scale = false;
291 uint32_t flags = ROT_0;
292 if (isZero(b) && isZero(c)) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800293 if (a<0) flags |= FLIP_H;
294 if (d<0) flags |= FLIP_V;
295 if (!absIsOne(a) || !absIsOne(d)) {
296 scale = true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800297 }
298 } else if (isZero(a) && isZero(d)) {
Mathias Agopiand1296592010-03-09 19:17:47 -0800299 flags |= ROT_90;
Mathias Agopian883dffa2010-10-25 18:29:35 -0700300 if (b>0) flags |= FLIP_V;
301 if (c<0) flags |= FLIP_H;
Mathias Agopiand1296592010-03-09 19:17:47 -0800302 if (!absIsOne(b) || !absIsOne(c)) {
303 scale = true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800304 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305 } else {
Mathias Agopian29a367b2011-07-12 14:51:45 -0700306 // there is a skew component and/or a non 90 degrees rotation
Mathias Agopianeda65402010-02-22 03:15:57 -0800307 flags = ROT_INVALID;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800309
310 mType = flags << 8;
311 if (flags & ROT_INVALID) {
312 mType |= UNKNOWN;
313 } else {
314 if ((flags & ROT_90) || ((flags & ROT_180) == ROT_180))
315 mType |= ROTATE;
316 if (flags & FLIP_H)
317 mType ^= SCALE;
318 if (flags & FLIP_V)
319 mType ^= SCALE;
320 if (scale)
321 mType |= SCALE;
322 }
323
324 if (!isZero(x) || !isZero(y))
325 mType |= TRANSLATE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800326 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800327 return mType;
328}
329
Mathias Agopian3da16722013-02-28 17:12:07 -0800330Transform Transform::inverse() const {
331 // our 3x3 matrix is always of the form of a 2x2 transformation
332 // followed by a translation: T*M, therefore:
333 // (T*M)^-1 = M^-1 * T^-1
334 Transform result;
335 if (mType <= TRANSLATE) {
Michael Lentine28ea2172014-11-19 18:32:37 -0800336 // 1 0 0
337 // 0 1 0
338 // x y 1
Mathias Agopian3da16722013-02-28 17:12:07 -0800339 result = *this;
340 result.mMatrix[2][0] = -result.mMatrix[2][0];
341 result.mMatrix[2][1] = -result.mMatrix[2][1];
342 } else {
Michael Lentine28ea2172014-11-19 18:32:37 -0800343 // a c 0
344 // b d 0
345 // x y 1
Mathias Agopian3da16722013-02-28 17:12:07 -0800346 const mat33& M(mMatrix);
347 const float a = M[0][0];
348 const float b = M[1][0];
349 const float c = M[0][1];
350 const float d = M[1][1];
351 const float x = M[2][0];
352 const float y = M[2][1];
353
Peiyong Linefefaac2018-08-17 12:27:51 -0700354 const float idet = 1.0f / (a*d - b*c);
Michael Lentine28ea2172014-11-19 18:32:37 -0800355 result.mMatrix[0][0] = d*idet;
356 result.mMatrix[0][1] = -c*idet;
357 result.mMatrix[1][0] = -b*idet;
358 result.mMatrix[1][1] = a*idet;
359 result.mType = mType;
Mathias Agopian3da16722013-02-28 17:12:07 -0800360
Michael Lentine28ea2172014-11-19 18:32:37 -0800361 vec2 T(-x, -y);
362 T = result.transform(T);
363 result.mMatrix[2][0] = T[0];
364 result.mMatrix[2][1] = T[1];
Mathias Agopian3da16722013-02-28 17:12:07 -0800365 }
366 return result;
367}
368
Mathias Agopianeda65402010-02-22 03:15:57 -0800369uint32_t Transform::getType() const {
370 return type() & 0xFF;
371}
372
373uint32_t Transform::getOrientation() const
374{
375 return (type() >> 8) & 0xFF;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376}
377
378bool Transform::preserveRects() const
379{
Mathias Agopianab7c13f2011-07-25 13:57:16 -0700380 return (getOrientation() & ROT_INVALID) ? false : true;
Mathias Agopianeda65402010-02-22 03:15:57 -0800381}
382
383void Transform::dump(const char* name) const
384{
385 type(); // updates the type
386
387 String8 flags, type;
388 const mat33& m(mMatrix);
389 uint32_t orient = mType >> 8;
390
Mathias Agopiand1296592010-03-09 19:17:47 -0800391 if (orient&ROT_INVALID) {
Mathias Agopianeda65402010-02-22 03:15:57 -0800392 flags.append("ROT_INVALID ");
Mathias Agopiand1296592010-03-09 19:17:47 -0800393 } else {
394 if (orient&ROT_90) {
395 flags.append("ROT_90 ");
396 } else {
397 flags.append("ROT_0 ");
398 }
399 if (orient&FLIP_V)
400 flags.append("FLIP_V ");
401 if (orient&FLIP_H)
402 flags.append("FLIP_H ");
403 }
Mathias Agopianeda65402010-02-22 03:15:57 -0800404
Mathias Agopiand1296592010-03-09 19:17:47 -0800405 if (!(mType&(SCALE|ROTATE|TRANSLATE)))
406 type.append("IDENTITY ");
Mathias Agopianeda65402010-02-22 03:15:57 -0800407 if (mType&SCALE)
408 type.append("SCALE ");
409 if (mType&ROTATE)
410 type.append("ROTATE ");
411 if (mType&TRANSLATE)
412 type.append("TRANSLATE ");
413
Steve Block9d453682011-12-20 16:23:08 +0000414 ALOGD("%s 0x%08x (%s, %s)", name, mType, flags.string(), type.string());
Peiyong Linefefaac2018-08-17 12:27:51 -0700415 ALOGD("%.4f %.4f %.4f", static_cast<double>(m[0][0]), static_cast<double>(m[1][0]),
416 static_cast<double>(m[2][0]));
417 ALOGD("%.4f %.4f %.4f", static_cast<double>(m[0][1]), static_cast<double>(m[1][1]),
418 static_cast<double>(m[2][1]));
419 ALOGD("%.4f %.4f %.4f", static_cast<double>(m[0][2]), static_cast<double>(m[1][2]),
420 static_cast<double>(m[2][2]));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800421}
422
Peiyong Linefefaac2018-08-17 12:27:51 -0700423} // namespace ui
424} // namespace android