Home | History | Annotate | Download | only in surfaceflinger
      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 <cutils/compiler.h>
     20 #include <utils/String8.h>
     21 #include <ui/Region.h>
     22 
     23 #include "clz.h"
     24 #include "Transform.h"
     25 
     26 // ---------------------------------------------------------------------------
     27 
     28 namespace android {
     29 
     30 // ---------------------------------------------------------------------------
     31 
     32 Transform::Transform() {
     33     reset();
     34 }
     35 
     36 Transform::Transform(const Transform&  other)
     37     : mMatrix(other.mMatrix), mType(other.mType) {
     38 }
     39 
     40 Transform::Transform(uint32_t orientation) {
     41     set(orientation, 0, 0);
     42 }
     43 
     44 Transform::~Transform() {
     45 }
     46 
     47 static const float EPSILON = 0.0f;
     48 
     49 bool Transform::isZero(float f) {
     50     return fabs(f) <= EPSILON;
     51 }
     52 
     53 bool Transform::absIsOne(float f) {
     54     return isZero(fabs(f) - 1.0f);
     55 }
     56 
     57 Transform Transform::operator * (const Transform& rhs) const
     58 {
     59     if (CC_LIKELY(mType == IDENTITY))
     60         return rhs;
     61 
     62     Transform r(*this);
     63     if (rhs.mType == IDENTITY)
     64         return r;
     65 
     66     // TODO: we could use mType to optimize the matrix multiply
     67     const mat33& A(mMatrix);
     68     const mat33& B(rhs.mMatrix);
     69           mat33& D(r.mMatrix);
     70     for (int i=0 ; i<3 ; i++) {
     71         const float v0 = A[0][i];
     72         const float v1 = A[1][i];
     73         const float v2 = A[2][i];
     74         D[0][i] = v0*B[0][0] + v1*B[0][1] + v2*B[0][2];
     75         D[1][i] = v0*B[1][0] + v1*B[1][1] + v2*B[1][2];
     76         D[2][i] = v0*B[2][0] + v1*B[2][1] + v2*B[2][2];
     77     }
     78     r.mType |= rhs.mType;
     79 
     80     // TODO: we could recompute this value from r and rhs
     81     r.mType &= 0xFF;
     82     r.mType |= UNKNOWN_TYPE;
     83     return r;
     84 }
     85 
     86 const vec3& Transform::operator [] (size_t i) const {
     87     return mMatrix[i];
     88 }
     89 
     90 float Transform::tx() const {
     91     return mMatrix[2][0];
     92 }
     93 
     94 float Transform::ty() const {
     95     return mMatrix[2][1];
     96 }
     97 
     98 void Transform::reset() {
     99     mType = IDENTITY;
    100     for(int i=0 ; i<3 ; i++) {
    101         vec3& v(mMatrix[i]);
    102         for (int j=0 ; j<3 ; j++)
    103             v[j] = ((i==j) ? 1.0f : 0.0f);
    104     }
    105 }
    106 
    107 void Transform::set(float tx, float ty)
    108 {
    109     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 
    120 void 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 
    129 status_t Transform::set(uint32_t flags, float w, float h)
    130 {
    131     if (flags & ROT_INVALID) {
    132         // that's not allowed!
    133         reset();
    134         return BAD_VALUE;
    135     }
    136 
    137     Transform H, V, R;
    138     if (flags & ROT_90) {
    139         // w & h are inverted when rotating by 90 degrees
    140         swap(w, h);
    141     }
    142 
    143     if (flags & FLIP_H) {
    144         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;
    149     }
    150 
    151     if (flags & FLIP_V) {
    152         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;
    157     }
    158 
    159     if (flags & ROT_90) {
    160         const float original_w = h;
    161         R.mType = (ROT_90 << 8) | ROTATE;
    162         R.mType |= isZero(original_w) ? IDENTITY : TRANSLATE;
    163         mat33& M(R.mMatrix);
    164         M[0][0] = 0;    M[1][0] =-1;    M[2][0] = original_w;
    165         M[0][1] = 1;    M[1][1] = 0;
    166     }
    167 
    168     *this = (R*(H*V));
    169     return NO_ERROR;
    170 }
    171 
    172 vec2 Transform::transform(const vec2& v) const {
    173     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 
    180 vec3 Transform::transform(const vec3& v) const {
    181     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 
    189 vec2 Transform::transform(int x, int y) const
    190 {
    191     return transform(vec2(x,y));
    192 }
    193 
    194 Rect Transform::makeBounds(int w, int h) const
    195 {
    196     return transform( Rect(w, h) );
    197 }
    198 
    199 Rect Transform::transform(const Rect& bounds) const
    200 {
    201     Rect r;
    202     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 
    212     r.left   = floorf(min(lt[0], rt[0], lb[0], rb[0]) + 0.5f);
    213     r.top    = floorf(min(lt[1], rt[1], lb[1], rb[1]) + 0.5f);
    214     r.right  = floorf(max(lt[0], rt[0], lb[0], rb[0]) + 0.5f);
    215     r.bottom = floorf(max(lt[1], rt[1], lb[1], rb[1]) + 0.5f);
    216 
    217     return r;
    218 }
    219 
    220 Region Transform::transform(const Region& reg) const
    221 {
    222     Region out;
    223     if (CC_UNLIKELY(type() > TRANSLATE)) {
    224         if (CC_LIKELY(preserveRects())) {
    225             Region::const_iterator it = reg.begin();
    226             Region::const_iterator const end = reg.end();
    227             while (it != end) {
    228                 out.orSelf(transform(*it++));
    229             }
    230         } else {
    231             out.set(transform(reg.bounds()));
    232         }
    233     } else {
    234         int xpos = floorf(tx() + 0.5f);
    235         int ypos = floorf(ty() + 0.5f);
    236         out = reg.translate(xpos, ypos);
    237     }
    238     return out;
    239 }
    240 
    241 uint32_t Transform::type() const
    242 {
    243     if (mType & UNKNOWN_TYPE) {
    244         // recompute what this transform is
    245 
    246         const mat33& M(mMatrix);
    247         const float a = M[0][0];
    248         const float b = M[1][0];
    249         const float c = M[0][1];
    250         const float d = M[1][1];
    251         const float x = M[2][0];
    252         const float y = M[2][1];
    253 
    254         bool scale = false;
    255         uint32_t flags = ROT_0;
    256         if (isZero(b) && isZero(c)) {
    257             if (a<0)    flags |= FLIP_H;
    258             if (d<0)    flags |= FLIP_V;
    259             if (!absIsOne(a) || !absIsOne(d)) {
    260                 scale = true;
    261             }
    262         } else if (isZero(a) && isZero(d)) {
    263             flags |= ROT_90;
    264             if (b>0)    flags |= FLIP_V;
    265             if (c<0)    flags |= FLIP_H;
    266             if (!absIsOne(b) || !absIsOne(c)) {
    267                 scale = true;
    268             }
    269         } else {
    270             // there is a skew component and/or a non 90 degrees rotation
    271             flags = ROT_INVALID;
    272         }
    273 
    274         mType = flags << 8;
    275         if (flags & ROT_INVALID) {
    276             mType |= UNKNOWN;
    277         } else {
    278             if ((flags & ROT_90) || ((flags & ROT_180) == ROT_180))
    279                 mType |= ROTATE;
    280             if (flags & FLIP_H)
    281                 mType ^= SCALE;
    282             if (flags & FLIP_V)
    283                 mType ^= SCALE;
    284             if (scale)
    285                 mType |= SCALE;
    286         }
    287 
    288         if (!isZero(x) || !isZero(y))
    289             mType |= TRANSLATE;
    290     }
    291     return mType;
    292 }
    293 
    294 Transform Transform::inverse() const {
    295     // our 3x3 matrix is always of the form of a 2x2 transformation
    296     // followed by a translation: T*M, therefore:
    297     // (T*M)^-1 = M^-1 * T^-1
    298     Transform result;
    299     if (mType <= TRANSLATE) {
    300         // 1 0 0
    301         // 0 1 0
    302         // x y 1
    303         result = *this;
    304         result.mMatrix[2][0] = -result.mMatrix[2][0];
    305         result.mMatrix[2][1] = -result.mMatrix[2][1];
    306     } else {
    307         // a c 0
    308         // b d 0
    309         // x y 1
    310         const mat33& M(mMatrix);
    311         const float a = M[0][0];
    312         const float b = M[1][0];
    313         const float c = M[0][1];
    314         const float d = M[1][1];
    315         const float x = M[2][0];
    316         const float y = M[2][1];
    317 
    318         const float idet = 1.0 / (a*d - b*c);
    319         result.mMatrix[0][0] =  d*idet;
    320         result.mMatrix[0][1] = -c*idet;
    321         result.mMatrix[1][0] = -b*idet;
    322         result.mMatrix[1][1] =  a*idet;
    323         result.mType = mType;
    324 
    325         vec2 T(-x, -y);
    326         T = result.transform(T);
    327         result.mMatrix[2][0] = T[0];
    328         result.mMatrix[2][1] = T[1];
    329     }
    330     return result;
    331 }
    332 
    333 uint32_t Transform::getType() const {
    334     return type() & 0xFF;
    335 }
    336 
    337 uint32_t Transform::getOrientation() const
    338 {
    339     return (type() >> 8) & 0xFF;
    340 }
    341 
    342 bool Transform::preserveRects() const
    343 {
    344     return (getOrientation() & ROT_INVALID) ? false : true;
    345 }
    346 
    347 void Transform::dump(const char* name) const
    348 {
    349     type(); // updates the type
    350 
    351     String8 flags, type;
    352     const mat33& m(mMatrix);
    353     uint32_t orient = mType >> 8;
    354 
    355     if (orient&ROT_INVALID) {
    356         flags.append("ROT_INVALID ");
    357     } else {
    358         if (orient&ROT_90) {
    359             flags.append("ROT_90 ");
    360         } else {
    361             flags.append("ROT_0 ");
    362         }
    363         if (orient&FLIP_V)
    364             flags.append("FLIP_V ");
    365         if (orient&FLIP_H)
    366             flags.append("FLIP_H ");
    367     }
    368 
    369     if (!(mType&(SCALE|ROTATE|TRANSLATE)))
    370         type.append("IDENTITY ");
    371     if (mType&SCALE)
    372         type.append("SCALE ");
    373     if (mType&ROTATE)
    374         type.append("ROTATE ");
    375     if (mType&TRANSLATE)
    376         type.append("TRANSLATE ");
    377 
    378     ALOGD("%s 0x%08x (%s, %s)", name, mType, flags.string(), type.string());
    379     ALOGD("%.4f  %.4f  %.4f", m[0][0], m[1][0], m[2][0]);
    380     ALOGD("%.4f  %.4f  %.4f", m[0][1], m[1][1], m[2][1]);
    381     ALOGD("%.4f  %.4f  %.4f", m[0][2], m[1][2], m[2][2]);
    382 }
    383 
    384 // ---------------------------------------------------------------------------
    385 
    386 }; // namespace android
    387