xref: /aosp_15_r20/external/skia/modules/jetski/src/Matrix.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1*c8dee2aaSAndroid Build Coastguard Worker /*
2*c8dee2aaSAndroid Build Coastguard Worker  * Copyright 2021 Google Inc.
3*c8dee2aaSAndroid Build Coastguard Worker  *
4*c8dee2aaSAndroid Build Coastguard Worker  * Use of this source code is governed by a BSD-style license that can be
5*c8dee2aaSAndroid Build Coastguard Worker  * found in the LICENSE file.
6*c8dee2aaSAndroid Build Coastguard Worker  */
7*c8dee2aaSAndroid Build Coastguard Worker 
8*c8dee2aaSAndroid Build Coastguard Worker #include <jni.h>
9*c8dee2aaSAndroid Build Coastguard Worker 
10*c8dee2aaSAndroid Build Coastguard Worker #include "include/core/SkM44.h"
11*c8dee2aaSAndroid Build Coastguard Worker 
12*c8dee2aaSAndroid Build Coastguard Worker #include <algorithm>
13*c8dee2aaSAndroid Build Coastguard Worker 
14*c8dee2aaSAndroid Build Coastguard Worker namespace {
15*c8dee2aaSAndroid Build Coastguard Worker 
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)16*c8dee2aaSAndroid Build Coastguard Worker static jlong Matrix_Create(JNIEnv* env, jobject, jfloat m0, jfloat m4, jfloat m8,  jfloat m12,
17*c8dee2aaSAndroid Build Coastguard Worker                                                  jfloat m1, jfloat m5, jfloat m9,  jfloat m13,
18*c8dee2aaSAndroid Build Coastguard Worker                                                  jfloat m2, jfloat m6, jfloat m10, jfloat m14,
19*c8dee2aaSAndroid Build Coastguard Worker                                                  jfloat m3, jfloat m7, jfloat m11, jfloat m15) {
20*c8dee2aaSAndroid Build Coastguard Worker     return reinterpret_cast<jlong>(new SkM44(m0, m4, m8, m12,
21*c8dee2aaSAndroid Build Coastguard Worker                                              m1, m5, m9, m13,
22*c8dee2aaSAndroid Build Coastguard Worker                                              m2, m6, m10, m14,
23*c8dee2aaSAndroid Build Coastguard Worker                                              m3, m7, m11, m15));
24*c8dee2aaSAndroid Build Coastguard Worker }
25*c8dee2aaSAndroid Build Coastguard Worker 
Matrix_CreateLookAt(JNIEnv * env,jobject,float eyeX,float eyeY,float eyeZ,float coaX,float coaY,float coaZ,float upX,float upY,float upZ)26*c8dee2aaSAndroid Build Coastguard Worker static jlong Matrix_CreateLookAt(JNIEnv* env, jobject, float eyeX, float eyeY, float eyeZ,
27*c8dee2aaSAndroid Build Coastguard Worker                                                        float coaX, float coaY, float coaZ,
28*c8dee2aaSAndroid Build Coastguard Worker                                                        float upX, float upY, float upZ) {
29*c8dee2aaSAndroid Build Coastguard Worker     return reinterpret_cast<jlong>(new SkM44(SkM44::LookAt({eyeX, eyeY, eyeZ},
30*c8dee2aaSAndroid Build Coastguard Worker                                                            {coaX, coaY, coaZ},
31*c8dee2aaSAndroid Build Coastguard Worker                                                            {upX, upY, upZ})));
32*c8dee2aaSAndroid Build Coastguard Worker }
33*c8dee2aaSAndroid Build Coastguard Worker 
Matrix_CreatePerspective(JNIEnv * env,jobject,float near,float far,float angle)34*c8dee2aaSAndroid Build Coastguard Worker static jlong Matrix_CreatePerspective(JNIEnv* env, jobject, float near, float far, float angle) {
35*c8dee2aaSAndroid Build Coastguard Worker     return reinterpret_cast<jlong>(new SkM44(SkM44::Perspective(near, far, angle)));
36*c8dee2aaSAndroid Build Coastguard Worker }
37*c8dee2aaSAndroid Build Coastguard Worker 
Matrix_GetRowMajor(JNIEnv * env,jobject,jlong native_matrix)38*c8dee2aaSAndroid Build Coastguard Worker static jfloatArray Matrix_GetRowMajor(JNIEnv* env, jobject, jlong native_matrix) {
39*c8dee2aaSAndroid Build Coastguard Worker     jfloatArray result = nullptr;
40*c8dee2aaSAndroid Build Coastguard Worker     if (auto* m = reinterpret_cast<SkM44*>(native_matrix)) {
41*c8dee2aaSAndroid Build Coastguard Worker         SkScalar temp[16];
42*c8dee2aaSAndroid Build Coastguard Worker         m->getRowMajor(temp);
43*c8dee2aaSAndroid Build Coastguard Worker 
44*c8dee2aaSAndroid Build Coastguard Worker         result = env->NewFloatArray(16);
45*c8dee2aaSAndroid Build Coastguard Worker         if (result) {
46*c8dee2aaSAndroid Build Coastguard Worker             env->SetFloatArrayRegion(result, 0, 16, temp);
47*c8dee2aaSAndroid Build Coastguard Worker         }
48*c8dee2aaSAndroid Build Coastguard Worker     }
49*c8dee2aaSAndroid Build Coastguard Worker     return result;
50*c8dee2aaSAndroid Build Coastguard Worker }
51*c8dee2aaSAndroid Build Coastguard Worker 
Matrix_Release(JNIEnv * env,jobject,jlong native_matrix)52*c8dee2aaSAndroid Build Coastguard Worker static void Matrix_Release(JNIEnv* env, jobject, jlong native_matrix) {
53*c8dee2aaSAndroid Build Coastguard Worker     delete reinterpret_cast<SkM44*>(native_matrix);
54*c8dee2aaSAndroid Build Coastguard Worker }
55*c8dee2aaSAndroid Build Coastguard Worker 
Matrix_PreConcat(JNIEnv * env,jobject,jlong native_matrixA,jlong native_matrixB)56*c8dee2aaSAndroid Build Coastguard Worker static void Matrix_PreConcat(JNIEnv* env, jobject, jlong native_matrixA, jlong native_matrixB) {
57*c8dee2aaSAndroid Build Coastguard Worker     if (auto* mA = reinterpret_cast<SkM44*>(native_matrixA),
58*c8dee2aaSAndroid Build Coastguard Worker             * mB = reinterpret_cast<SkM44*>(native_matrixB); mA && mB) {
59*c8dee2aaSAndroid Build Coastguard Worker         mA->preConcat(*mB);
60*c8dee2aaSAndroid Build Coastguard Worker     }
61*c8dee2aaSAndroid Build Coastguard Worker }
62*c8dee2aaSAndroid Build Coastguard Worker 
Matrix_Inverse(JNIEnv * env,jobject,jlong native_matrix)63*c8dee2aaSAndroid Build Coastguard Worker static jlong Matrix_Inverse(JNIEnv* env, jobject, jlong native_matrix) {
64*c8dee2aaSAndroid Build Coastguard Worker     if (auto* m = reinterpret_cast<SkM44*>(native_matrix)) {
65*c8dee2aaSAndroid Build Coastguard Worker         SkM44 inverse(SkM44::kUninitialized_Constructor);
66*c8dee2aaSAndroid Build Coastguard Worker         if (m->invert(&inverse)) {
67*c8dee2aaSAndroid Build Coastguard Worker             return reinterpret_cast<jlong>(new SkM44(inverse));
68*c8dee2aaSAndroid Build Coastguard Worker         }
69*c8dee2aaSAndroid Build Coastguard Worker     }
70*c8dee2aaSAndroid Build Coastguard Worker     return 0;
71*c8dee2aaSAndroid Build Coastguard Worker }
72*c8dee2aaSAndroid Build Coastguard Worker 
Matrix_Transpose(JNIEnv * env,jobject,jlong native_matrix)73*c8dee2aaSAndroid Build Coastguard Worker static jlong Matrix_Transpose(JNIEnv* env, jobject, jlong native_matrix) {
74*c8dee2aaSAndroid Build Coastguard Worker     if (auto* matrix = reinterpret_cast<SkM44*>(native_matrix)) {
75*c8dee2aaSAndroid Build Coastguard Worker         SkM44 trans(matrix->transpose());
76*c8dee2aaSAndroid Build Coastguard Worker         return reinterpret_cast<jlong>(new SkM44(trans));
77*c8dee2aaSAndroid Build Coastguard Worker     }
78*c8dee2aaSAndroid Build Coastguard Worker     return 0;
79*c8dee2aaSAndroid Build Coastguard Worker }
80*c8dee2aaSAndroid Build Coastguard Worker 
Matrix_Concat(JNIEnv * env,jobject,jlong native_matrixA,jlong native_matrixB)81*c8dee2aaSAndroid Build Coastguard Worker static jlong Matrix_Concat(JNIEnv* env, jobject, jlong native_matrixA, jlong native_matrixB) {
82*c8dee2aaSAndroid Build Coastguard Worker     if (auto* mA = reinterpret_cast<SkM44*>(native_matrixA),
83*c8dee2aaSAndroid Build Coastguard Worker             * mB = reinterpret_cast<SkM44*>(native_matrixB); mA && mB) {
84*c8dee2aaSAndroid Build Coastguard Worker         return reinterpret_cast<jlong>(new SkM44(*mA, *mB));
85*c8dee2aaSAndroid Build Coastguard Worker     }
86*c8dee2aaSAndroid Build Coastguard Worker     return 0;
87*c8dee2aaSAndroid Build Coastguard Worker }
88*c8dee2aaSAndroid Build Coastguard Worker 
Matrix_Translate(JNIEnv * env,jobject,jlong native_matrix,jfloat x,jfloat y,jfloat z)89*c8dee2aaSAndroid Build Coastguard Worker static void Matrix_Translate(JNIEnv* env, jobject, jlong native_matrix, jfloat x, jfloat y, jfloat z) {
90*c8dee2aaSAndroid Build Coastguard Worker     if (auto* matrix = reinterpret_cast<SkM44*>(native_matrix)) {
91*c8dee2aaSAndroid Build Coastguard Worker         matrix->preTranslate(x, y, z);
92*c8dee2aaSAndroid Build Coastguard Worker     }
93*c8dee2aaSAndroid Build Coastguard Worker }
94*c8dee2aaSAndroid Build Coastguard Worker 
Matrix_Scale(JNIEnv * env,jobject,jlong native_matrix,jfloat x,jfloat y,jfloat z)95*c8dee2aaSAndroid Build Coastguard Worker static void Matrix_Scale(JNIEnv* env, jobject, jlong native_matrix, jfloat x, jfloat y, jfloat z) {
96*c8dee2aaSAndroid Build Coastguard Worker     if (auto* matrix = reinterpret_cast<SkM44*>(native_matrix)) {
97*c8dee2aaSAndroid Build Coastguard Worker         matrix->preScale(x, y, z);
98*c8dee2aaSAndroid Build Coastguard Worker     }
99*c8dee2aaSAndroid Build Coastguard Worker }
100*c8dee2aaSAndroid Build Coastguard Worker 
Matrix_Rotate(JNIEnv * env,jobject,jlong native_matrix,jfloat x,jfloat y,jfloat z,jfloat rad)101*c8dee2aaSAndroid Build Coastguard Worker static void Matrix_Rotate(JNIEnv* env, jobject, jlong native_matrix, jfloat x, jfloat y, jfloat z, jfloat rad) {
102*c8dee2aaSAndroid Build Coastguard Worker     if (auto* matrix = reinterpret_cast<SkM44*>(native_matrix)) {
103*c8dee2aaSAndroid Build Coastguard Worker         SkM44 rotate = SkM44::Rotate({x, y, z}, rad);
104*c8dee2aaSAndroid Build Coastguard Worker         matrix->preConcat(rotate);
105*c8dee2aaSAndroid Build Coastguard Worker     }
106*c8dee2aaSAndroid Build Coastguard Worker }
107*c8dee2aaSAndroid Build Coastguard Worker 
108*c8dee2aaSAndroid Build Coastguard Worker } // namespace
109*c8dee2aaSAndroid Build Coastguard Worker 
register_jetski_Matrix(JNIEnv * env)110*c8dee2aaSAndroid Build Coastguard Worker int register_jetski_Matrix(JNIEnv* env) {
111*c8dee2aaSAndroid Build Coastguard Worker     static const JNINativeMethod methods[] = {
112*c8dee2aaSAndroid Build Coastguard Worker         {"nCreate"            , "(FFFFFFFFFFFFFFFF)J" , reinterpret_cast<void*>(Matrix_Create)},
113*c8dee2aaSAndroid Build Coastguard Worker         {"nCreateLookAt"      , "(FFFFFFFFF)J"        , reinterpret_cast<void*>(Matrix_CreateLookAt)},
114*c8dee2aaSAndroid Build Coastguard Worker         {"nCreatePerspective" , "(FFF)J"              , reinterpret_cast<void*>(Matrix_CreatePerspective)},
115*c8dee2aaSAndroid Build Coastguard Worker         {"nGetRowMajor"       , "(J)[F"               , reinterpret_cast<void*>(Matrix_GetRowMajor)},
116*c8dee2aaSAndroid Build Coastguard Worker         {"nRelease"           , "(J)V"                , reinterpret_cast<void*>(Matrix_Release)},
117*c8dee2aaSAndroid Build Coastguard Worker         {"nInverse"           , "(J)J"                , reinterpret_cast<void*>(Matrix_Inverse)},
118*c8dee2aaSAndroid Build Coastguard Worker         {"nTranspose"         , "(J)J"                , reinterpret_cast<void*>(Matrix_Transpose)},
119*c8dee2aaSAndroid Build Coastguard Worker         {"nPreConcat"         , "(JJ)V"               , reinterpret_cast<void*>(Matrix_PreConcat)},
120*c8dee2aaSAndroid Build Coastguard Worker         {"nConcat"            , "(JJ)J"               , reinterpret_cast<void*>(Matrix_Concat)},
121*c8dee2aaSAndroid Build Coastguard Worker         {"nTranslate"         , "(JFFF)V"             , reinterpret_cast<void*>(Matrix_Translate)},
122*c8dee2aaSAndroid Build Coastguard Worker         {"nScale"             , "(JFFF)V"             , reinterpret_cast<void*>(Matrix_Scale)},
123*c8dee2aaSAndroid Build Coastguard Worker         {"nRotate"            , "(JFFFF)V"            , reinterpret_cast<void*>(Matrix_Rotate)},
124*c8dee2aaSAndroid Build Coastguard Worker     };
125*c8dee2aaSAndroid Build Coastguard Worker 
126*c8dee2aaSAndroid Build Coastguard Worker     const auto clazz = env->FindClass("org/skia/jetski/Matrix");
127*c8dee2aaSAndroid Build Coastguard Worker     return clazz
128*c8dee2aaSAndroid Build Coastguard Worker         ? env->RegisterNatives(clazz, methods, std::size(methods))
129*c8dee2aaSAndroid Build Coastguard Worker         : JNI_ERR;
130*c8dee2aaSAndroid Build Coastguard Worker }
131