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 #ifndef ANDROID_TRANSFORM_H
     18 #define ANDROID_TRANSFORM_H
     19 
     20 #include <stdint.h>
     21 #include <sys/types.h>
     22 
     23 #include <ui/Point.h>
     24 #include <ui/Rect.h>
     25 
     26 #include <hardware/hardware.h>
     27 
     28 namespace android {
     29 
     30 class Region;
     31 
     32 // ---------------------------------------------------------------------------
     33 
     34 class Transform
     35 {
     36 public:
     37                     Transform();
     38                     Transform(const Transform&  other);
     39            explicit Transform(uint32_t orientation);
     40                     ~Transform();
     41 
     42             enum orientation_flags {
     43                 ROT_0   = 0x00000000,
     44                 FLIP_H  = HAL_TRANSFORM_FLIP_H,
     45                 FLIP_V  = HAL_TRANSFORM_FLIP_V,
     46                 ROT_90  = HAL_TRANSFORM_ROT_90,
     47                 ROT_180 = FLIP_H|FLIP_V,
     48                 ROT_270 = ROT_180|ROT_90,
     49                 ROT_INVALID = 0x80
     50             };
     51 
     52             enum type_mask {
     53                 IDENTITY            = 0,
     54                 TRANSLATE           = 0x1,
     55                 ROTATE              = 0x2,
     56                 SCALE               = 0x4,
     57                 UNKNOWN             = 0x8
     58             };
     59 
     60             // query the transform
     61             bool        transformed() const;
     62             bool        preserveRects() const;
     63             uint32_t    getType() const;
     64             uint32_t    getOrientation() const;
     65 
     66             float const* operator [] (int i) const;  // returns column i
     67             float   tx() const;
     68             float   ty() const;
     69 
     70             // modify the transform
     71             void        reset();
     72             void        set(float tx, float ty);
     73             void        set(float a, float b, float c, float d);
     74             status_t    set(uint32_t flags, float w, float h);
     75 
     76             // transform data
     77             Rect    makeBounds(int w, int h) const;
     78             void    transform(float* point, int x, int y) const;
     79             Region  transform(const Region& reg) const;
     80             Transform operator * (const Transform& rhs) const;
     81 
     82             // for debugging
     83             void dump(const char* name) const;
     84 
     85 private:
     86     struct vec3 {
     87         float v[3];
     88         inline vec3() { }
     89         inline vec3(float a, float b, float c) {
     90             v[0] = a; v[1] = b; v[2] = c;
     91         }
     92         inline float operator [] (int i) const { return v[i]; }
     93         inline float& operator [] (int i) { return v[i]; }
     94     };
     95     struct vec2 {
     96         float v[2];
     97         inline vec2() { }
     98         inline vec2(float a, float b) {
     99             v[0] = a; v[1] = b;
    100         }
    101         inline float operator [] (int i) const { return v[i]; }
    102         inline float& operator [] (int i) { return v[i]; }
    103     };
    104     struct mat33 {
    105         vec3 v[3];
    106         inline const vec3& operator [] (int i) const { return v[i]; }
    107         inline vec3& operator [] (int i) { return v[i]; }
    108     };
    109 
    110     enum { UNKNOWN_TYPE = 0x80000000 };
    111 
    112     // assumes the last row is < 0 , 0 , 1 >
    113     vec2 transform(const vec2& v) const;
    114     vec3 transform(const vec3& v) const;
    115     Rect transform(const Rect& bounds) const;
    116     uint32_t type() const;
    117     static bool absIsOne(float f);
    118     static bool isZero(float f);
    119 
    120     mat33               mMatrix;
    121     mutable uint32_t    mType;
    122 };
    123 
    124 // ---------------------------------------------------------------------------
    125 }; // namespace android
    126 
    127 #endif /* ANDROID_TRANSFORM_H */
    128