2010-06-21 19:35:50 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-02-22 13:39:33 -08:00
|
|
|
#pragma once
|
2010-06-21 19:35:50 -07:00
|
|
|
|
2016-02-22 13:39:33 -08:00
|
|
|
#include "Rect.h"
|
2010-06-23 17:47:49 -07:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
#include <SkMatrix.h>
|
2011-10-12 14:11:32 -07:00
|
|
|
#include <cutils/compiler.h>
|
2016-02-22 13:39:33 -08:00
|
|
|
#include <iomanip>
|
|
|
|
#include <ostream>
|
2010-06-24 19:30:36 -07:00
|
|
|
|
2010-06-21 19:35:50 -07:00
|
|
|
namespace android {
|
2010-06-24 19:30:36 -07:00
|
|
|
namespace uirenderer {
|
2010-06-21 19:35:50 -07:00
|
|
|
|
2014-02-05 13:00:24 -08:00
|
|
|
#define SK_MATRIX_STRING "[%.2f %.2f %.2f] [%.2f %.2f %.2f] [%.2f %.2f %.2f]"
|
2017-11-03 10:12:19 -07:00
|
|
|
#define SK_MATRIX_ARGS(m) \
|
|
|
|
(m)->get(0), (m)->get(1), (m)->get(2), (m)->get(3), (m)->get(4), (m)->get(5), (m)->get(6), \
|
|
|
|
(m)->get(7), (m)->get(8)
|
2013-05-31 11:38:03 -07:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
#define MATRIX_4_STRING \
|
|
|
|
"[%.2f %.2f %.2f %.2f] [%.2f %.2f %.2f %.2f]" \
|
2014-02-05 13:00:24 -08:00
|
|
|
" [%.2f %.2f %.2f %.2f] [%.2f %.2f %.2f %.2f]"
|
2017-11-03 10:12:19 -07:00
|
|
|
#define MATRIX_4_ARGS(m) \
|
|
|
|
(m)->data[0], (m)->data[4], (m)->data[8], (m)->data[12], (m)->data[1], (m)->data[5], \
|
|
|
|
(m)->data[9], (m)->data[13], (m)->data[2], (m)->data[6], (m)->data[10], (m)->data[14], \
|
|
|
|
(m)->data[3], (m)->data[7], (m)->data[11], (m)->data[15]
|
2014-02-05 13:00:24 -08:00
|
|
|
|
2010-06-21 19:35:50 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Classes
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-10-12 14:11:32 -07:00
|
|
|
class ANDROID_API Matrix4 {
|
2010-06-21 19:35:50 -07:00
|
|
|
public:
|
2010-06-25 13:46:18 -07:00
|
|
|
float data[16];
|
2010-06-25 13:41:57 -07:00
|
|
|
|
2010-08-12 14:34:44 -07:00
|
|
|
enum Entry {
|
|
|
|
kScaleX = 0,
|
|
|
|
kSkewY = 1,
|
|
|
|
kPerspective0 = 3,
|
|
|
|
kSkewX = 4,
|
|
|
|
kScaleY = 5,
|
|
|
|
kPerspective1 = 7,
|
|
|
|
kScaleZ = 10,
|
|
|
|
kTranslateX = 12,
|
|
|
|
kTranslateY = 13,
|
|
|
|
kTranslateZ = 14,
|
|
|
|
kPerspective2 = 15
|
|
|
|
};
|
|
|
|
|
2013-01-15 18:51:42 -08:00
|
|
|
// NOTE: The flags from kTypeIdentity to kTypePerspective
|
|
|
|
// must be kept in sync with the type flags found
|
|
|
|
// in SkMatrix
|
|
|
|
enum Type {
|
|
|
|
kTypeIdentity = 0,
|
|
|
|
kTypeTranslate = 0x1,
|
|
|
|
kTypeScale = 0x2,
|
|
|
|
kTypeAffine = 0x4,
|
|
|
|
kTypePerspective = 0x8,
|
|
|
|
kTypeRectToRect = 0x10,
|
2013-09-20 17:13:18 -07:00
|
|
|
kTypeUnknown = 0x20,
|
2013-01-15 18:51:42 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
static const int sGeometryMask = 0xf;
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
Matrix4() { loadIdentity(); }
|
2010-06-21 19:35:50 -07:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
explicit Matrix4(const float* v) { load(v); }
|
2010-06-21 19:35:50 -07:00
|
|
|
|
2016-07-21 11:23:06 -07:00
|
|
|
Matrix4(const SkMatrix& v) { // NOLINT, implicit
|
2010-06-25 13:46:18 -07:00
|
|
|
load(v);
|
|
|
|
}
|
2010-06-23 17:47:49 -07:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
float operator[](int index) const { return data[index]; }
|
2013-03-05 10:27:35 -08:00
|
|
|
|
|
|
|
float& operator[](int index) {
|
|
|
|
mType = kTypeUnknown;
|
|
|
|
return data[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix4& operator=(const SkMatrix& v) {
|
|
|
|
load(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-03-13 16:14:47 -07:00
|
|
|
friend bool operator==(const Matrix4& a, const Matrix4& b) {
|
|
|
|
return !memcmp(&a.data[0], &b.data[0], 16 * sizeof(float));
|
|
|
|
}
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
friend bool operator!=(const Matrix4& a, const Matrix4& b) { return !(a == b); }
|
2013-03-13 16:14:47 -07:00
|
|
|
|
2010-06-25 13:46:18 -07:00
|
|
|
void loadIdentity();
|
2010-06-21 19:35:50 -07:00
|
|
|
|
2010-06-25 13:46:18 -07:00
|
|
|
void load(const float* v);
|
|
|
|
void load(const SkMatrix& v);
|
2010-06-21 19:35:50 -07:00
|
|
|
|
2010-07-16 14:12:24 -07:00
|
|
|
void loadInverse(const Matrix4& v);
|
|
|
|
|
2010-06-25 13:46:18 -07:00
|
|
|
void loadTranslate(float x, float y, float z);
|
|
|
|
void loadScale(float sx, float sy, float sz);
|
2011-01-18 11:19:19 -08:00
|
|
|
void loadSkew(float sx, float sy);
|
2013-01-15 18:51:42 -08:00
|
|
|
void loadRotate(float angle);
|
2010-06-25 13:46:18 -07:00
|
|
|
void loadRotate(float angle, float x, float y, float z);
|
|
|
|
void loadMultiply(const Matrix4& u, const Matrix4& v);
|
2010-06-21 19:35:50 -07:00
|
|
|
|
2010-06-25 13:46:18 -07:00
|
|
|
void loadOrtho(float left, float right, float bottom, float top, float near, float far);
|
2017-11-03 10:12:19 -07:00
|
|
|
void loadOrtho(int width, int height) { loadOrtho(0, width, height, 0, -1, 1); }
|
2010-06-21 19:35:50 -07:00
|
|
|
|
2013-06-20 17:52:07 -07:00
|
|
|
uint8_t getType() const;
|
2013-01-15 18:51:42 -08:00
|
|
|
|
2015-04-28 11:45:59 -07:00
|
|
|
void multiplyInverse(const Matrix4& v) {
|
|
|
|
Matrix4 inv;
|
|
|
|
inv.loadInverse(v);
|
|
|
|
multiply(inv);
|
|
|
|
}
|
|
|
|
|
2010-06-25 13:46:18 -07:00
|
|
|
void multiply(const Matrix4& v) {
|
2015-12-16 14:27:20 -08:00
|
|
|
if (!v.isIdentity()) {
|
|
|
|
Matrix4 u;
|
|
|
|
u.loadMultiply(*this, v);
|
|
|
|
*this = u;
|
|
|
|
}
|
2010-06-25 13:46:18 -07:00
|
|
|
}
|
2010-06-21 19:35:50 -07:00
|
|
|
|
2010-09-20 19:04:33 -07:00
|
|
|
void multiply(float v);
|
|
|
|
|
2013-10-25 18:30:17 -07:00
|
|
|
void translate(float x, float y, float z = 0) {
|
2013-06-20 17:52:07 -07:00
|
|
|
if ((getType() & sGeometryMask) <= kTypeTranslate) {
|
2013-06-11 16:19:24 -07:00
|
|
|
data[kTranslateX] += x;
|
|
|
|
data[kTranslateY] += y;
|
2013-10-25 18:30:17 -07:00
|
|
|
data[kTranslateZ] += z;
|
2014-06-11 18:39:44 -07:00
|
|
|
mType |= kTypeUnknown;
|
2013-06-11 16:19:24 -07:00
|
|
|
} else {
|
|
|
|
// Doing a translation will only affect the translate bit of the type
|
|
|
|
// Save the type
|
2013-06-20 17:52:07 -07:00
|
|
|
uint8_t type = mType;
|
2013-06-11 16:19:24 -07:00
|
|
|
|
|
|
|
Matrix4 u;
|
2013-10-25 18:30:17 -07:00
|
|
|
u.loadTranslate(x, y, z);
|
2013-06-11 16:19:24 -07:00
|
|
|
multiply(u);
|
|
|
|
|
|
|
|
// Restore the type and fix the translate bit
|
|
|
|
mType = type;
|
|
|
|
if (data[kTranslateX] != 0.0f || data[kTranslateY] != 0.0f) {
|
|
|
|
mType |= kTypeTranslate;
|
|
|
|
} else {
|
|
|
|
mType &= ~kTypeTranslate;
|
|
|
|
}
|
|
|
|
}
|
2010-06-25 13:46:18 -07:00
|
|
|
}
|
2010-06-21 19:35:50 -07:00
|
|
|
|
2010-06-25 13:46:18 -07:00
|
|
|
void scale(float sx, float sy, float sz) {
|
|
|
|
Matrix4 u;
|
|
|
|
u.loadScale(sx, sy, sz);
|
|
|
|
multiply(u);
|
|
|
|
}
|
2010-06-21 19:35:50 -07:00
|
|
|
|
2011-01-18 11:19:19 -08:00
|
|
|
void skew(float sx, float sy) {
|
|
|
|
Matrix4 u;
|
|
|
|
u.loadSkew(sx, sy);
|
|
|
|
multiply(u);
|
|
|
|
}
|
|
|
|
|
2010-06-25 13:46:18 -07:00
|
|
|
void rotate(float angle, float x, float y, float z) {
|
|
|
|
Matrix4 u;
|
|
|
|
u.loadRotate(angle, x, y, z);
|
|
|
|
multiply(u);
|
|
|
|
}
|
2010-06-21 19:35:50 -07:00
|
|
|
|
2013-01-15 18:51:42 -08:00
|
|
|
/**
|
|
|
|
* If the matrix is identity or translate and/or scale.
|
|
|
|
*/
|
2012-09-17 17:25:49 -07:00
|
|
|
bool isSimple() const;
|
2013-01-15 18:51:42 -08:00
|
|
|
bool isPureTranslate() const;
|
2012-09-17 17:25:49 -07:00
|
|
|
bool isIdentity() const;
|
2012-09-28 13:55:44 -07:00
|
|
|
bool isPerspective() const;
|
2013-01-15 18:51:42 -08:00
|
|
|
bool rectToRect() const;
|
2013-09-16 14:47:13 -07:00
|
|
|
bool positiveScale() const;
|
2010-12-06 18:07:02 -08:00
|
|
|
|
2012-09-17 17:25:49 -07:00
|
|
|
bool changesBounds() const;
|
2010-10-04 14:14:11 -07:00
|
|
|
|
2010-06-25 13:46:18 -07:00
|
|
|
void copyTo(float* v) const;
|
|
|
|
void copyTo(SkMatrix& v) const;
|
2010-06-21 19:35:50 -07:00
|
|
|
|
2014-03-11 12:20:17 -07:00
|
|
|
float mapZ(const Vector3& orig) const;
|
2013-10-25 18:30:17 -07:00
|
|
|
void mapPoint3d(Vector3& vec) const;
|
2017-11-03 10:12:19 -07:00
|
|
|
void mapPoint(float& x, float& y) const; // 2d only
|
|
|
|
void mapRect(Rect& r) const; // 2d only
|
2010-06-24 19:30:36 -07:00
|
|
|
|
2013-03-05 16:43:31 -08:00
|
|
|
float getTranslateX() const;
|
|
|
|
float getTranslateY() const;
|
2010-06-26 00:13:53 -07:00
|
|
|
|
2013-03-05 10:27:35 -08:00
|
|
|
void decomposeScale(float& sx, float& sy) const;
|
|
|
|
|
2014-12-22 14:28:49 -08:00
|
|
|
void dump(const char* label = nullptr) const;
|
2010-06-21 19:35:50 -07:00
|
|
|
|
2016-02-22 13:39:33 -08:00
|
|
|
friend std::ostream& operator<<(std::ostream& os, const Matrix4& matrix) {
|
|
|
|
if (matrix.isSimple()) {
|
|
|
|
os << "offset " << matrix.getTranslateX() << "x" << matrix.getTranslateY();
|
|
|
|
if (!matrix.isPureTranslate()) {
|
|
|
|
os << ", scale " << matrix[kScaleX] << "x" << matrix[kScaleY];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
os << "[" << matrix[0];
|
|
|
|
for (int i = 1; i < 16; i++) {
|
|
|
|
os << ", " << matrix[i];
|
|
|
|
}
|
|
|
|
os << "]";
|
|
|
|
}
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2013-02-26 19:10:14 -08:00
|
|
|
static const Matrix4& identity();
|
|
|
|
|
2016-04-28 13:18:51 -07:00
|
|
|
void invalidateType() { mType = kTypeUnknown; }
|
|
|
|
|
2010-06-23 17:47:49 -07:00
|
|
|
private:
|
2013-06-20 17:52:07 -07:00
|
|
|
mutable uint8_t mType;
|
2010-08-12 14:34:44 -07:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
inline float get(int i, int j) const { return data[i * 4 + j]; }
|
2010-06-21 19:35:50 -07:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
inline void set(int i, int j, float v) { data[i * 4 + j] = v; }
|
2013-01-15 18:51:42 -08:00
|
|
|
|
2013-06-20 17:52:07 -07:00
|
|
|
uint8_t getGeometryType() const;
|
2013-01-15 18:51:42 -08:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
}; // class Matrix4
|
2010-06-21 19:35:50 -07:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Types
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
typedef Matrix4 mat4;
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
}; // namespace uirenderer
|
|
|
|
}; // namespace android
|