Home | History | Annotate | Download | only in hwui
      1 /*
      2  * Copyright (C) 2010 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_HWUI_MATRIX_H
     18 #define ANDROID_HWUI_MATRIX_H
     19 
     20 #include <SkMatrix.h>
     21 
     22 #include <cutils/compiler.h>
     23 
     24 #include "Rect.h"
     25 
     26 namespace android {
     27 namespace uirenderer {
     28 
     29 ///////////////////////////////////////////////////////////////////////////////
     30 // Classes
     31 ///////////////////////////////////////////////////////////////////////////////
     32 
     33 class ANDROID_API Matrix4 {
     34 public:
     35     float data[16];
     36 
     37     enum Entry {
     38         kScaleX = 0,
     39         kSkewY = 1,
     40         kPerspective0 = 3,
     41         kSkewX = 4,
     42         kScaleY = 5,
     43         kPerspective1 = 7,
     44         kScaleZ = 10,
     45         kTranslateX = 12,
     46         kTranslateY = 13,
     47         kTranslateZ = 14,
     48         kPerspective2 = 15
     49     };
     50 
     51     // NOTE: The flags from kTypeIdentity to kTypePerspective
     52     //       must be kept in sync with the type flags found
     53     //       in SkMatrix
     54     enum Type {
     55         kTypeIdentity = 0,
     56         kTypeTranslate = 0x1,
     57         kTypeScale = 0x2,
     58         kTypeAffine = 0x4,
     59         kTypePerspective = 0x8,
     60         kTypeRectToRect = 0x10,
     61         kTypeUnknown = 0x20,
     62     };
     63 
     64     static const int sGeometryMask = 0xf;
     65 
     66     Matrix4() {
     67         loadIdentity();
     68     }
     69 
     70     Matrix4(const float* v) {
     71         load(v);
     72     }
     73 
     74     Matrix4(const Matrix4& v) {
     75         load(v);
     76     }
     77 
     78     Matrix4(const SkMatrix& v) {
     79         load(v);
     80     }
     81 
     82     float operator[](int index) const {
     83         return data[index];
     84     }
     85 
     86     float& operator[](int index) {
     87         mType = kTypeUnknown;
     88         return data[index];
     89     }
     90 
     91     Matrix4& operator=(const SkMatrix& v) {
     92         load(v);
     93         return *this;
     94     }
     95 
     96     friend bool operator==(const Matrix4& a, const Matrix4& b) {
     97         return !memcmp(&a.data[0], &b.data[0], 16 * sizeof(float));
     98     }
     99 
    100     friend bool operator!=(const Matrix4& a, const Matrix4& b) {
    101         return !(a == b);
    102     }
    103 
    104     void loadIdentity();
    105 
    106     void load(const float* v);
    107     void load(const Matrix4& v);
    108     void load(const SkMatrix& v);
    109 
    110     void loadInverse(const Matrix4& v);
    111 
    112     void loadTranslate(float x, float y, float z);
    113     void loadScale(float sx, float sy, float sz);
    114     void loadSkew(float sx, float sy);
    115     void loadRotate(float angle);
    116     void loadRotate(float angle, float x, float y, float z);
    117     void loadMultiply(const Matrix4& u, const Matrix4& v);
    118 
    119     void loadOrtho(float left, float right, float bottom, float top, float near, float far);
    120 
    121     uint32_t getType() const;
    122 
    123     void multiply(const Matrix4& v) {
    124         Matrix4 u;
    125         u.loadMultiply(*this, v);
    126         load(u);
    127     }
    128 
    129     void multiply(float v);
    130 
    131     void translate(float x, float y, float z) {
    132         Matrix4 u;
    133         u.loadTranslate(x, y, z);
    134         multiply(u);
    135     }
    136 
    137     void scale(float sx, float sy, float sz) {
    138         Matrix4 u;
    139         u.loadScale(sx, sy, sz);
    140         multiply(u);
    141     }
    142 
    143     void skew(float sx, float sy) {
    144         Matrix4 u;
    145         u.loadSkew(sx, sy);
    146         multiply(u);
    147     }
    148 
    149     void rotate(float angle, float x, float y, float z) {
    150         Matrix4 u;
    151         u.loadRotate(angle, x, y, z);
    152         multiply(u);
    153     }
    154 
    155     /**
    156      * If the matrix is identity or translate and/or scale.
    157      */
    158     bool isSimple() const;
    159     bool isPureTranslate() const;
    160     bool isIdentity() const;
    161     bool isPerspective() const;
    162     bool rectToRect() const;
    163 
    164     bool changesBounds() const;
    165 
    166     void copyTo(float* v) const;
    167     void copyTo(SkMatrix& v) const;
    168 
    169     void mapRect(Rect& r) const;
    170     void mapPoint(float& x, float& y) const;
    171 
    172     float getTranslateX() const;
    173     float getTranslateY() const;
    174 
    175     void decomposeScale(float& sx, float& sy) const;
    176 
    177     void dump() const;
    178 
    179     static const Matrix4& identity();
    180 
    181 private:
    182     mutable uint32_t mType;
    183 
    184     inline float get(int i, int j) const {
    185         return data[i * 4 + j];
    186     }
    187 
    188     inline void set(int i, int j, float v) {
    189         data[i * 4 + j] = v;
    190     }
    191 
    192     uint32_t getGeometryType() const;
    193 
    194 }; // class Matrix4
    195 
    196 ///////////////////////////////////////////////////////////////////////////////
    197 // Types
    198 ///////////////////////////////////////////////////////////////////////////////
    199 
    200 typedef Matrix4 mat4;
    201 
    202 }; // namespace uirenderer
    203 }; // namespace android
    204 
    205 #endif // ANDROID_HWUI_MATRIX_H
    206