Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 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 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HWUI_MATRIX_H |
| 18 | #define ANDROID_HWUI_MATRIX_H |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 19 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 20 | #include <SkMatrix.h> |
| 21 | |
Romain Guy | 71e36aa | 2011-10-12 14:11:32 -0700 | [diff] [blame] | 22 | #include <cutils/compiler.h> |
| 23 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 24 | #include "Rect.h" |
| 25 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 26 | namespace android { |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 27 | namespace uirenderer { |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 28 | |
| 29 | /////////////////////////////////////////////////////////////////////////////// |
| 30 | // Classes |
| 31 | /////////////////////////////////////////////////////////////////////////////// |
| 32 | |
Romain Guy | 71e36aa | 2011-10-12 14:11:32 -0700 | [diff] [blame] | 33 | class ANDROID_API Matrix4 { |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 34 | public: |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 35 | float data[16]; |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 36 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 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 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 51 | Matrix4() { |
| 52 | loadIdentity(); |
| 53 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 54 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 55 | Matrix4(const float* v) { |
| 56 | load(v); |
| 57 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 58 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 59 | Matrix4(const Matrix4& v) { |
| 60 | load(v); |
| 61 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 62 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 63 | Matrix4(const SkMatrix& v) { |
| 64 | load(v); |
| 65 | } |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 66 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 67 | void loadIdentity(); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 68 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 69 | void load(const float* v); |
| 70 | void load(const Matrix4& v); |
| 71 | void load(const SkMatrix& v); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 72 | |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 73 | void loadInverse(const Matrix4& v); |
| 74 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 75 | void loadTranslate(float x, float y, float z); |
| 76 | void loadScale(float sx, float sy, float sz); |
Romain Guy | 807daf7 | 2011-01-18 11:19:19 -0800 | [diff] [blame] | 77 | void loadSkew(float sx, float sy); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 78 | void loadRotate(float angle, float x, float y, float z); |
| 79 | void loadMultiply(const Matrix4& u, const Matrix4& v); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 80 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 81 | void loadOrtho(float left, float right, float bottom, float top, float near, float far); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 82 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 83 | void multiply(const Matrix4& v) { |
| 84 | Matrix4 u; |
| 85 | u.loadMultiply(*this, v); |
| 86 | load(u); |
| 87 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 88 | |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 89 | void multiply(float v); |
| 90 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 91 | void translate(float x, float y, float z) { |
| 92 | Matrix4 u; |
| 93 | u.loadTranslate(x, y, z); |
| 94 | multiply(u); |
| 95 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 96 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 97 | void scale(float sx, float sy, float sz) { |
| 98 | Matrix4 u; |
| 99 | u.loadScale(sx, sy, sz); |
| 100 | multiply(u); |
| 101 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 102 | |
Romain Guy | 807daf7 | 2011-01-18 11:19:19 -0800 | [diff] [blame] | 103 | void skew(float sx, float sy) { |
| 104 | Matrix4 u; |
| 105 | u.loadSkew(sx, sy); |
| 106 | multiply(u); |
| 107 | } |
| 108 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 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 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 114 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 115 | bool isPureTranslate(); |
Romain Guy | 181d0a6 | 2011-06-09 18:52:38 -0700 | [diff] [blame] | 116 | bool isSimple(); |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 117 | bool isIdentity(); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 118 | |
Romain Guy | e8cb9c14 | 2010-10-04 14:14:11 -0700 | [diff] [blame] | 119 | bool changesBounds(); |
| 120 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 121 | void copyTo(float* v) const; |
| 122 | void copyTo(SkMatrix& v) const; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 123 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 124 | void mapRect(Rect& r) const; |
Romain Guy | 0ba681b | 2010-08-12 15:37:00 -0700 | [diff] [blame] | 125 | void mapPoint(float& x, float& y) const; |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 126 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 127 | float getTranslateX(); |
| 128 | float getTranslateY(); |
| 129 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 130 | void dump() const; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 131 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 132 | private: |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 133 | bool mSimpleMatrix; |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 134 | bool mIsIdentity; |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 135 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 136 | inline float get(int i, int j) const { |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 137 | return data[i * 4 + j]; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | inline void set(int i, int j, float v) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 141 | data[i * 4 + j] = v; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 142 | } |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 143 | }; // class Matrix4 |
| 144 | |
| 145 | /////////////////////////////////////////////////////////////////////////////// |
| 146 | // Types |
| 147 | /////////////////////////////////////////////////////////////////////////////// |
| 148 | |
| 149 | typedef Matrix4 mat4; |
| 150 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 151 | }; // namespace uirenderer |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 152 | }; // namespace android |
| 153 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 154 | #endif // ANDROID_HWUI_MATRIX_H |