blob: f0a3a959617d54e6d8dba46c40b9e210851b887c [file] [log] [blame]
Romain Guy08ae3172010-06-21 19:35:50 -07001/*
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
Chris Craik91eff222016-02-22 13:39:33 -080017#pragma once
Romain Guy71e36aa2011-10-12 14:11:32 -070018
Romain Guy9d5316e2010-06-24 19:30:36 -070019#include "Rect.h"
20
John Reck1bcacfd2017-11-03 10:12:19 -070021#include <SkMatrix.h>
Chris Craik91eff222016-02-22 13:39:33 -080022#include <cutils/compiler.h>
23#include <iomanip>
24#include <ostream>
Chris Craik91eff222016-02-22 13:39:33 -080025
Romain Guy08ae3172010-06-21 19:35:50 -070026namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070027namespace uirenderer {
Romain Guy08ae3172010-06-21 19:35:50 -070028
Chris Craik629f6772014-02-05 13:00:24 -080029#define SK_MATRIX_STRING "[%.2f %.2f %.2f] [%.2f %.2f %.2f] [%.2f %.2f %.2f]"
John Reck1bcacfd2017-11-03 10:12:19 -070030#define SK_MATRIX_ARGS(m) \
31 (m)->get(0), (m)->get(1), (m)->get(2), (m)->get(3), (m)->get(4), (m)->get(5), (m)->get(6), \
32 (m)->get(7), (m)->get(8)
Chris Craik28ce94a2013-05-31 11:38:03 -070033
John Reck1bcacfd2017-11-03 10:12:19 -070034#define MATRIX_4_STRING \
35 "[%.2f %.2f %.2f %.2f] [%.2f %.2f %.2f %.2f]" \
Chris Craik629f6772014-02-05 13:00:24 -080036 " [%.2f %.2f %.2f %.2f] [%.2f %.2f %.2f %.2f]"
John Reck1bcacfd2017-11-03 10:12:19 -070037#define MATRIX_4_ARGS(m) \
38 (m)->data[0], (m)->data[4], (m)->data[8], (m)->data[12], (m)->data[1], (m)->data[5], \
39 (m)->data[9], (m)->data[13], (m)->data[2], (m)->data[6], (m)->data[10], (m)->data[14], \
40 (m)->data[3], (m)->data[7], (m)->data[11], (m)->data[15]
Chris Craik629f6772014-02-05 13:00:24 -080041
Romain Guy08ae3172010-06-21 19:35:50 -070042///////////////////////////////////////////////////////////////////////////////
43// Classes
44///////////////////////////////////////////////////////////////////////////////
45
Romain Guy71e36aa2011-10-12 14:11:32 -070046class ANDROID_API Matrix4 {
Romain Guy08ae3172010-06-21 19:35:50 -070047public:
Romain Guy7ae7ac42010-06-25 13:46:18 -070048 float data[16];
Romain Guyc7d53492010-06-25 13:41:57 -070049
Romain Guyaf28b512010-08-12 14:34:44 -070050 enum Entry {
51 kScaleX = 0,
52 kSkewY = 1,
53 kPerspective0 = 3,
54 kSkewX = 4,
55 kScaleY = 5,
56 kPerspective1 = 7,
57 kScaleZ = 10,
58 kTranslateX = 12,
59 kTranslateY = 13,
60 kTranslateZ = 14,
61 kPerspective2 = 15
62 };
63
Romain Guy8ce00302013-01-15 18:51:42 -080064 // NOTE: The flags from kTypeIdentity to kTypePerspective
65 // must be kept in sync with the type flags found
66 // in SkMatrix
67 enum Type {
68 kTypeIdentity = 0,
69 kTypeTranslate = 0x1,
70 kTypeScale = 0x2,
71 kTypeAffine = 0x4,
72 kTypePerspective = 0x8,
73 kTypeRectToRect = 0x10,
Chris Craik996fe652013-09-20 17:13:18 -070074 kTypeUnknown = 0x20,
Romain Guy8ce00302013-01-15 18:51:42 -080075 };
76
77 static const int sGeometryMask = 0xf;
78
John Reck1bcacfd2017-11-03 10:12:19 -070079 Matrix4() { loadIdentity(); }
Romain Guy08ae3172010-06-21 19:35:50 -070080
John Reck1bcacfd2017-11-03 10:12:19 -070081 explicit Matrix4(const float* v) { load(v); }
Romain Guy08ae3172010-06-21 19:35:50 -070082
Chih-Hung Hsiehfaecb782016-07-21 11:23:06 -070083 Matrix4(const SkMatrix& v) { // NOLINT, implicit
Romain Guy7ae7ac42010-06-25 13:46:18 -070084 load(v);
85 }
Romain Guyf6a11b82010-06-23 17:47:49 -070086
John Reck1bcacfd2017-11-03 10:12:19 -070087 float operator[](int index) const { return data[index]; }
Romain Guy3b753822013-03-05 10:27:35 -080088
89 float& operator[](int index) {
90 mType = kTypeUnknown;
91 return data[index];
92 }
93
94 Matrix4& operator=(const SkMatrix& v) {
95 load(v);
96 return *this;
97 }
98
Romain Guybd3055f2013-03-13 16:14:47 -070099 friend bool operator==(const Matrix4& a, const Matrix4& b) {
100 return !memcmp(&a.data[0], &b.data[0], 16 * sizeof(float));
101 }
102
John Reck1bcacfd2017-11-03 10:12:19 -0700103 friend bool operator!=(const Matrix4& a, const Matrix4& b) { return !(a == b); }
Romain Guybd3055f2013-03-13 16:14:47 -0700104
Romain Guy7ae7ac42010-06-25 13:46:18 -0700105 void loadIdentity();
Romain Guy08ae3172010-06-21 19:35:50 -0700106
Romain Guy7ae7ac42010-06-25 13:46:18 -0700107 void load(const float* v);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700108 void load(const SkMatrix& v);
Romain Guy08ae3172010-06-21 19:35:50 -0700109
Romain Guy079ba2c2010-07-16 14:12:24 -0700110 void loadInverse(const Matrix4& v);
111
Romain Guy7ae7ac42010-06-25 13:46:18 -0700112 void loadTranslate(float x, float y, float z);
113 void loadScale(float sx, float sy, float sz);
Romain Guy807daf72011-01-18 11:19:19 -0800114 void loadSkew(float sx, float sy);
Romain Guy8ce00302013-01-15 18:51:42 -0800115 void loadRotate(float angle);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700116 void loadRotate(float angle, float x, float y, float z);
117 void loadMultiply(const Matrix4& u, const Matrix4& v);
Romain Guy08ae3172010-06-21 19:35:50 -0700118
Romain Guy7ae7ac42010-06-25 13:46:18 -0700119 void loadOrtho(float left, float right, float bottom, float top, float near, float far);
John Reck1bcacfd2017-11-03 10:12:19 -0700120 void loadOrtho(int width, int height) { loadOrtho(0, width, height, 0, -1, 1); }
Romain Guy08ae3172010-06-21 19:35:50 -0700121
Romain Guyf6bed4f2013-06-20 17:52:07 -0700122 uint8_t getType() const;
Romain Guy8ce00302013-01-15 18:51:42 -0800123
Chris Craikfca52b752015-04-28 11:45:59 -0700124 void multiplyInverse(const Matrix4& v) {
125 Matrix4 inv;
126 inv.loadInverse(v);
127 multiply(inv);
128 }
129
Romain Guy7ae7ac42010-06-25 13:46:18 -0700130 void multiply(const Matrix4& v) {
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800131 if (!v.isIdentity()) {
132 Matrix4 u;
133 u.loadMultiply(*this, v);
134 *this = u;
135 }
Romain Guy7ae7ac42010-06-25 13:46:18 -0700136 }
Romain Guy08ae3172010-06-21 19:35:50 -0700137
Romain Guyddb80be2010-09-20 19:04:33 -0700138 void multiply(float v);
139
Chris Craikf57776b2013-10-25 18:30:17 -0700140 void translate(float x, float y, float z = 0) {
Romain Guyf6bed4f2013-06-20 17:52:07 -0700141 if ((getType() & sGeometryMask) <= kTypeTranslate) {
Romain Guy4c2547f2013-06-11 16:19:24 -0700142 data[kTranslateX] += x;
143 data[kTranslateY] += y;
Chris Craikf57776b2013-10-25 18:30:17 -0700144 data[kTranslateZ] += z;
John Recka447d292014-06-11 18:39:44 -0700145 mType |= kTypeUnknown;
Romain Guy4c2547f2013-06-11 16:19:24 -0700146 } else {
147 // Doing a translation will only affect the translate bit of the type
148 // Save the type
Romain Guyf6bed4f2013-06-20 17:52:07 -0700149 uint8_t type = mType;
Romain Guy4c2547f2013-06-11 16:19:24 -0700150
151 Matrix4 u;
Chris Craikf57776b2013-10-25 18:30:17 -0700152 u.loadTranslate(x, y, z);
Romain Guy4c2547f2013-06-11 16:19:24 -0700153 multiply(u);
154
155 // Restore the type and fix the translate bit
156 mType = type;
157 if (data[kTranslateX] != 0.0f || data[kTranslateY] != 0.0f) {
158 mType |= kTypeTranslate;
159 } else {
160 mType &= ~kTypeTranslate;
161 }
162 }
Romain Guy7ae7ac42010-06-25 13:46:18 -0700163 }
Romain Guy08ae3172010-06-21 19:35:50 -0700164
Romain Guy7ae7ac42010-06-25 13:46:18 -0700165 void scale(float sx, float sy, float sz) {
166 Matrix4 u;
167 u.loadScale(sx, sy, sz);
168 multiply(u);
169 }
Romain Guy08ae3172010-06-21 19:35:50 -0700170
Romain Guy807daf72011-01-18 11:19:19 -0800171 void skew(float sx, float sy) {
172 Matrix4 u;
173 u.loadSkew(sx, sy);
174 multiply(u);
175 }
176
Romain Guy7ae7ac42010-06-25 13:46:18 -0700177 void rotate(float angle, float x, float y, float z) {
178 Matrix4 u;
179 u.loadRotate(angle, x, y, z);
180 multiply(u);
181 }
Romain Guy08ae3172010-06-21 19:35:50 -0700182
Romain Guy8ce00302013-01-15 18:51:42 -0800183 /**
184 * If the matrix is identity or translate and/or scale.
185 */
Chris Craik710f46d2012-09-17 17:25:49 -0700186 bool isSimple() const;
Romain Guy8ce00302013-01-15 18:51:42 -0800187 bool isPureTranslate() const;
Chris Craik710f46d2012-09-17 17:25:49 -0700188 bool isIdentity() const;
Romain Guya3dc55f2012-09-28 13:55:44 -0700189 bool isPerspective() const;
Romain Guy8ce00302013-01-15 18:51:42 -0800190 bool rectToRect() const;
Chris Craikd965bc52013-09-16 14:47:13 -0700191 bool positiveScale() const;
Romain Guy6620c6d2010-12-06 18:07:02 -0800192
Chris Craik710f46d2012-09-17 17:25:49 -0700193 bool changesBounds() const;
Romain Guye8cb9c142010-10-04 14:14:11 -0700194
Romain Guy7ae7ac42010-06-25 13:46:18 -0700195 void copyTo(float* v) const;
196 void copyTo(SkMatrix& v) const;
Romain Guy08ae3172010-06-21 19:35:50 -0700197
Chris Craikb79a3e32014-03-11 12:20:17 -0700198 float mapZ(const Vector3& orig) const;
Chris Craikf57776b2013-10-25 18:30:17 -0700199 void mapPoint3d(Vector3& vec) const;
John Reck1bcacfd2017-11-03 10:12:19 -0700200 void mapPoint(float& x, float& y) const; // 2d only
201 void mapRect(Rect& r) const; // 2d only
Romain Guy9d5316e2010-06-24 19:30:36 -0700202
Romain Guy624234f2013-03-05 16:43:31 -0800203 float getTranslateX() const;
204 float getTranslateY() const;
Romain Guybd6b79b2010-06-26 00:13:53 -0700205
Romain Guy3b753822013-03-05 10:27:35 -0800206 void decomposeScale(float& sx, float& sy) const;
207
Chris Craike84a2082014-12-22 14:28:49 -0800208 void dump(const char* label = nullptr) const;
Romain Guy08ae3172010-06-21 19:35:50 -0700209
Chris Craik91eff222016-02-22 13:39:33 -0800210 friend std::ostream& operator<<(std::ostream& os, const Matrix4& matrix) {
211 if (matrix.isSimple()) {
212 os << "offset " << matrix.getTranslateX() << "x" << matrix.getTranslateY();
213 if (!matrix.isPureTranslate()) {
214 os << ", scale " << matrix[kScaleX] << "x" << matrix[kScaleY];
215 }
216 } else {
217 os << "[" << matrix[0];
218 for (int i = 1; i < 16; i++) {
219 os << ", " << matrix[i];
220 }
221 os << "]";
222 }
223 return os;
224 }
225
Romain Guyc74f45a2013-02-26 19:10:14 -0800226 static const Matrix4& identity();
227
John Reck2f69d6d2016-04-28 13:18:51 -0700228 void invalidateType() { mType = kTypeUnknown; }
229
Romain Guyf6a11b82010-06-23 17:47:49 -0700230private:
Romain Guyf6bed4f2013-06-20 17:52:07 -0700231 mutable uint8_t mType;
Romain Guyaf28b512010-08-12 14:34:44 -0700232
John Reck1bcacfd2017-11-03 10:12:19 -0700233 inline float get(int i, int j) const { return data[i * 4 + j]; }
Romain Guy08ae3172010-06-21 19:35:50 -0700234
John Reck1bcacfd2017-11-03 10:12:19 -0700235 inline void set(int i, int j, float v) { data[i * 4 + j] = v; }
Romain Guy8ce00302013-01-15 18:51:42 -0800236
Romain Guyf6bed4f2013-06-20 17:52:07 -0700237 uint8_t getGeometryType() const;
Romain Guy8ce00302013-01-15 18:51:42 -0800238
John Reck1bcacfd2017-11-03 10:12:19 -0700239}; // class Matrix4
Romain Guy08ae3172010-06-21 19:35:50 -0700240
241///////////////////////////////////////////////////////////////////////////////
242// Types
243///////////////////////////////////////////////////////////////////////////////
244
245typedef Matrix4 mat4;
246
John Reck1bcacfd2017-11-03 10:12:19 -0700247}; // namespace uirenderer
248}; // namespace android