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 "Rect.h" 23 24 namespace android { 25 namespace uirenderer { 26 27 /////////////////////////////////////////////////////////////////////////////// 28 // Classes 29 /////////////////////////////////////////////////////////////////////////////// 30 31 class Matrix4 { 32 public: 33 float data[16]; 34 35 enum Entry { 36 kScaleX = 0, 37 kSkewY = 1, 38 kPerspective0 = 3, 39 kSkewX = 4, 40 kScaleY = 5, 41 kPerspective1 = 7, 42 kScaleZ = 10, 43 kTranslateX = 12, 44 kTranslateY = 13, 45 kTranslateZ = 14, 46 kPerspective2 = 15 47 }; 48 49 Matrix4() { 50 loadIdentity(); 51 } 52 53 Matrix4(const float* v) { 54 load(v); 55 } 56 57 Matrix4(const Matrix4& v) { 58 load(v); 59 } 60 61 Matrix4(const SkMatrix& v) { 62 load(v); 63 } 64 65 void loadIdentity(); 66 67 void load(const float* v); 68 void load(const Matrix4& v); 69 void load(const SkMatrix& v); 70 71 void loadInverse(const Matrix4& v); 72 73 void loadTranslate(float x, float y, float z); 74 void loadScale(float sx, float sy, float sz); 75 void loadSkew(float sx, float sy); 76 void loadRotate(float angle, float x, float y, float z); 77 void loadMultiply(const Matrix4& u, const Matrix4& v); 78 79 void loadOrtho(float left, float right, float bottom, float top, float near, float far); 80 81 void multiply(const Matrix4& v) { 82 Matrix4 u; 83 u.loadMultiply(*this, v); 84 load(u); 85 } 86 87 void multiply(float v); 88 89 void translate(float x, float y, float z) { 90 Matrix4 u; 91 u.loadTranslate(x, y, z); 92 multiply(u); 93 } 94 95 void scale(float sx, float sy, float sz) { 96 Matrix4 u; 97 u.loadScale(sx, sy, sz); 98 multiply(u); 99 } 100 101 void skew(float sx, float sy) { 102 Matrix4 u; 103 u.loadSkew(sx, sy); 104 multiply(u); 105 } 106 107 void rotate(float angle, float x, float y, float z) { 108 Matrix4 u; 109 u.loadRotate(angle, x, y, z); 110 multiply(u); 111 } 112 113 bool isPureTranslate(); 114 bool isSimple(); 115 bool isIdentity(); 116 117 bool changesBounds(); 118 119 void copyTo(float* v) const; 120 void copyTo(SkMatrix& v) const; 121 122 void mapRect(Rect& r) const; 123 void mapPoint(float& x, float& y) const; 124 125 float getTranslateX(); 126 float getTranslateY(); 127 128 void dump() const; 129 130 private: 131 bool mSimpleMatrix; 132 bool mIsIdentity; 133 134 inline float get(int i, int j) const { 135 return data[i * 4 + j]; 136 } 137 138 inline void set(int i, int j, float v) { 139 data[i * 4 + j] = v; 140 } 141 }; // class Matrix4 142 143 /////////////////////////////////////////////////////////////////////////////// 144 // Types 145 /////////////////////////////////////////////////////////////////////////////// 146 147 typedef Matrix4 mat4; 148 149 }; // namespace uirenderer 150 }; // namespace android 151 152 #endif // ANDROID_HWUI_MATRIX_H 153