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