[androidkit] add transformation operations to Matrix

Change-Id: I5a81087eb56f10786c136df7cf68bb0e34742abf
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/402676
Commit-Queue: Jorge Betancourt <jmbetancourt@google.com>
Reviewed-by: Florin Malita <fmalita@chromium.org>
This commit is contained in:
Jorge Betancourt 2021-04-30 08:13:49 -04:00 committed by Skia Commit-Bot
parent a60cc3ea41
commit 14efdd3d50
2 changed files with 78 additions and 20 deletions

View File

@ -20,18 +20,11 @@ static jlong Matrix_Create(JNIEnv* env, jobject, jfloat m0, jfloat m4, jfloat m8
m2, m6, m10, m14,
m3, m7, m11, m15));
}
static void Matrix_Release(JNIEnv* env, jobject, jlong native_matrix) {
delete reinterpret_cast<SkM44*>(native_matrix);
}
static void Matrix_PostConcat(JNIEnv* env, jobject, jlong native_matrixA, jlong native_matrixB) {
if (auto* mA = reinterpret_cast<SkM44*>(native_matrixA),
* mB = reinterpret_cast<SkM44*>(native_matrixB); mA && mB) {
mA->postConcat(*mB);
}
}
static void Matrix_PreConcat(JNIEnv* env, jobject, jlong native_matrixA, jlong native_matrixB) {
if (auto* mA = reinterpret_cast<SkM44*>(native_matrixA),
* mB = reinterpret_cast<SkM44*>(native_matrixB); mA && mB) {
@ -52,7 +45,6 @@ int register_androidkit_Matrix(JNIEnv* env) {
static const JNINativeMethod methods[] = {
{"nCreate" , "(FFFFFFFFFFFFFFFF)J" , reinterpret_cast<void*>(Matrix_Create)},
{"nRelease" , "(J)V" , reinterpret_cast<void*>(Matrix_Release)},
{"nPostConcat" , "(JJ)V" , reinterpret_cast<void*>(Matrix_PostConcat)},
{"nPreConcat" , "(JJ)V" , reinterpret_cast<void*>(Matrix_PreConcat)},
{"nConcat" , "(JJ)J" , reinterpret_cast<void*>(Matrix_Concat)},
};

View File

@ -41,7 +41,9 @@ public class Matrix {
}
/*
* Concat A * B, return new matrix as result
* A: this Matrix
* B: Matrix passed in
* Concat A * B, return new Matrix C as result
*/
public static Matrix Concat(Matrix a, Matrix b) {
long nativeA = a.mNativeInstance;
@ -51,15 +53,8 @@ public class Matrix {
}
/*
* Concat A * B, store result in Matrix A
*/
public void postConcat(Matrix b) {
long nativeA = this.mNativeInstance;
long nativeB = b.mNativeInstance;
nPostConcat(nativeA, nativeB);
}
/*
* A: this Matrix
* B: Matrix passed in
* Concat B * A, store result in Matrix A
*/
public void preConcat(Matrix b) {
@ -68,6 +63,78 @@ public class Matrix {
nPreConcat(nativeA, nativeB);
}
/*
* Translates this Matrix by x, y, z
* Store result in caller Matrix
* returns reference to this Matrix for operation chaining
*
* TODO: optimize calls to JNI
*/
public Matrix translate(float x, float y, float z) {
Matrix t = new Matrix(1, 0, 0, x,
0, 1, 0, y,
0, 0, 1, z,
0, 0, 0, 1);
this.preConcat(t);
return this;
}
/*
* Scales this Matrix by x, y, z
* Store result in caller Matrix
* returns reference to this Matrix for operation chaining
*/
public Matrix scale(float x, float y, float z) {
Matrix s = new Matrix(x, 0, 0, 0,
0, y, 0, 0,
0, 0, z, 0,
0, 0, 0, 1);
this.preConcat(s);
return this;
}
/*
* Rotates this Matrix along the x-axis by rad radians
* Store result in caller Matrix
* returns reference to this Matrix for operation chaining
*/
public Matrix rotateX(float rad) {
Matrix r = new Matrix(1, 0, 0, 0,
0, (float) Math.cos(rad), (float) -Math.sin(rad), 0,
0, (float) Math.sin(rad), (float) Math.cos(rad), 0,
0, 0, 0, 1);
this.preConcat(r);
return this;
}
/*
* Rotates this Matrix along the y-axis by rad radians
* Store result in caller Matrix
* returns reference to this Matrix for operation chaining
*/
public Matrix rotateY(float rad) {
Matrix r = new Matrix((float) Math.cos(rad), 0, (float) Math.sin(rad), 0,
0, 1, 0, 0,
(float) -Math.sin(rad), 0, (float) Math.cos(rad), 0,
0, 0, 0, 1);
this.preConcat(r);
return this;
}
/*
* Rotates this Matrix along the z-axis by rad radians
* Store result in caller Matrix
* returns reference to this Matrix for operation chaining
*/
public Matrix rotateZ(float rad) {
Matrix r = new Matrix((float) Math.cos(rad), (float) -Math.sin(rad), 0, 0,
(float) Math.sin(rad), (float) Math.cos(rad), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
this.preConcat(r);
return this;
}
/**
* Releases any resources associated with this Matrix.
*/
@ -87,7 +154,6 @@ public class Matrix {
float m3, float m7, float m11, float m15);
private static native void nRelease(long nativeInstance);
private static native void nPostConcat(long mNativeInstanceA, long mNativeInstanceB);
private static native void nPreConcat(long mNativeInstanceA, long mNativeInstanceB);
private static native long nConcat(long mNativeInstanceA, long mNativeInstanceB);
}