[androidkit] implement foundation for 4x4 matrices in AndroidKit backed by SkM44

Change-Id: If623efaadda6064598c6fd69daa4527a4e71cced
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/402101
Commit-Queue: Jorge Betancourt <jmbetancourt@google.com>
Reviewed-by: Florin Malita <fmalita@chromium.org>
This commit is contained in:
Jorge Betancourt 2021-04-29 13:33:50 -04:00 committed by Skia Commit-Bot
parent a6cc82e3cf
commit 5cddd7f5e1
4 changed files with 159 additions and 0 deletions

View File

@ -2476,6 +2476,7 @@ if (skia_enable_tools) {
sources = [
"modules/androidkit/src/AndroidKit.cpp",
"modules/androidkit/src/Canvas.cpp",
"modules/androidkit/src/Matrix.cpp",
"modules/androidkit/src/Paint.cpp",
"modules/androidkit/src/RuntimeShaderBuilder.cpp",
"modules/androidkit/src/Shader.cpp",

View File

@ -24,6 +24,7 @@ JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
}
REGISTER_NATIVES(Canvas)
REGISTER_NATIVES(Matrix)
REGISTER_NATIVES(Paint)
REGISTER_NATIVES(RuntimeShaderBuilder)
REGISTER_NATIVES(Shader)

View File

@ -0,0 +1,64 @@
/*
* Copyright 2021 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include <jni.h>
#include "include/core/SkM44.h"
namespace {
static jlong Matrix_Create(JNIEnv* env, jobject, jfloat m0, jfloat m4, jfloat m8, jfloat m12,
jfloat m1, jfloat m5, jfloat m9, jfloat m13,
jfloat m2, jfloat m6, jfloat m10, jfloat m14,
jfloat m3, jfloat m7, jfloat m11, jfloat m15) {
return reinterpret_cast<jlong>(new SkM44(m0, m4, m8, m12,
m1, m5, m9, m13,
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) {
mA->preConcat(*mB);
}
}
static long Matrix_Concat(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) {
return reinterpret_cast<jlong>(new SkM44(*mA, *mB));
}
return 0;
}
} // namespace
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)},
};
const auto clazz = env->FindClass("org/skia/androidkit/Matrix");
return clazz
? env->RegisterNatives(clazz, methods, SK_ARRAY_COUNT(methods))
: JNI_ERR;
}

View File

@ -0,0 +1,93 @@
/*
* Copyright 2021 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
package org.skia.androidkit;
/*
* 4x4 matrix backed by SkM44
*/
public class Matrix {
private long mNativeInstance;
/*
* Returns identity Matrix
*/
public Matrix() {
mNativeInstance = nCreate(1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
}
/*
* Returns Matrix populated with values passed in (row-major order).
*/
public Matrix(float m0, float m4, float m8, float m12,
float m1, float m5, float m9, float m13,
float m2, float m6, float m10, float m14,
float m3, float m7, float m11, float m15) {
mNativeInstance = nCreate(m0, m4, m8, m12,
m1, m5, m9, m13,
m2, m6, m10, m14,
m3, m7, m11, m15);
}
private Matrix(long nativeInstance) {
mNativeInstance = nativeInstance;
}
/*
* Concat A * B, return new matrix as result
*/
public static Matrix Concat(Matrix a, Matrix b) {
long nativeA = a.mNativeInstance;
long nativeB = b.mNativeInstance;
long nativeC = nConcat(nativeA, nativeB);
return new Matrix(nativeC);
}
/*
* Concat A * B, store result in Matrix A
*/
public void postConcat(Matrix b) {
long nativeA = this.mNativeInstance;
long nativeB = b.mNativeInstance;
nPostConcat(nativeA, nativeB);
}
/*
* Concat B * A, store result in Matrix A
*/
public void preConcat(Matrix b) {
long nativeA = this.mNativeInstance;
long nativeB = b.mNativeInstance;
nPreConcat(nativeA, nativeB);
}
/**
* Releases any resources associated with this Matrix.
*/
public void release() {
nRelease(mNativeInstance);
mNativeInstance = 0;
}
@Override
protected void finalize() throws Throwable {
release();
}
private static native long nCreate(float m0, float m4, float m8, float m12,
float m1, float m5, float m9, float m13,
float m2, float m6, float m10, float m14,
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);
}