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