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     Matrix4() {
     52         loadIdentity();
     53     }
     54 
     55     Matrix4(const float* v) {
     56         load(v);
     57     }
     58 
     59     Matrix4(const Matrix4& v) {
     60         load(v);
     61     }
     62 
     63     Matrix4(const SkMatrix& v) {
     64         load(v);
     65     }
     66 
     67     void loadIdentity();
     68 
     69     void load(const float* v);
     70     void load(const Matrix4& v);
     71     void load(const SkMatrix& v);
     72 
     73     void loadInverse(const Matrix4& v);
     74 
     75     void loadTranslate(float x, float y, float z);
     76     void loadScale(float sx, float sy, float sz);
     77     void loadSkew(float sx, float sy);
     78     void loadRotate(float angle, float x, float y, float z);
     79     void loadMultiply(const Matrix4& u, const Matrix4& v);
     80 
     81     void loadOrtho(float left, float right, float bottom, float top, float near, float far);
     82 
     83     void multiply(const Matrix4& v) {
     84         Matrix4 u;
     85         u.loadMultiply(*this, v);
     86         load(u);
     87     }
     88 
     89     void multiply(float v);
     90 
     91     void translate(float x, float y, float z) {
     92         Matrix4 u;
     93         u.loadTranslate(x, y, z);
     94         multiply(u);
     95     }
     96 
     97     void scale(float sx, float sy, float sz) {
     98         Matrix4 u;
     99         u.loadScale(sx, sy, sz);
    100         multiply(u);
    101     }
    102 
    103     void skew(float sx, float sy) {
    104         Matrix4 u;
    105         u.loadSkew(sx, sy);
    106         multiply(u);
    107     }
    108 
    109     void rotate(float angle, float x, float y, float z) {
    110         Matrix4 u;
    111         u.loadRotate(angle, x, y, z);
    112         multiply(u);
    113     }
    114 
    115     bool isPureTranslate();
    116     bool isSimple();
    117     bool isIdentity();
    118 
    119     bool changesBounds();
    120 
    121     void copyTo(float* v) const;
    122     void copyTo(SkMatrix& v) const;
    123 
    124     void mapRect(Rect& r) const;
    125     void mapPoint(float& x, float& y) const;
    126 
    127     float getTranslateX();
    128     float getTranslateY();
    129 
    130     void dump() const;
    131 
    132 private:
    133     bool mSimpleMatrix;
    134     bool mIsIdentity;
    135 
    136     inline float get(int i, int j) const {
    137         return data[i * 4 + j];
    138     }
    139 
    140     inline void set(int i, int j, float v) {
    141         data[i * 4 + j] = v;
    142     }
    143 }; // class Matrix4
    144 
    145 ///////////////////////////////////////////////////////////////////////////////
    146 // Types
    147 ///////////////////////////////////////////////////////////////////////////////
    148 
    149 typedef Matrix4 mat4;
    150 
    151 }; // namespace uirenderer
    152 }; // namespace android
    153 
    154 #endif // ANDROID_HWUI_MATRIX_H
    155