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