xref: /aosp_15_r20/frameworks/base/libs/hwui/jni/graphics_jni_helpers.h (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef GRAPHICS_JNI_HELPERS
18 #define GRAPHICS_JNI_HELPERS
19 
20 #include <log/log.h>
21 #include <nativehelper/JNIPlatformHelp.h>
22 #include <nativehelper/scoped_local_ref.h>
23 #include <nativehelper/scoped_utf_chars.h>
24 #include <string>
25 
26 // Host targets (layoutlib) do not differentiate between regular and critical native methods,
27 // and they need all the JNI methods to have JNIEnv* and jclass/jobject as their first two arguments.
28 // The following macro allows to have those arguments when compiling for host while omitting them when
29 // compiling for Android.
30 #ifdef __ANDROID__
31 #define CRITICAL_JNI_PARAMS
32 #define CRITICAL_JNI_PARAMS_COMMA
33 #else
34 #define CRITICAL_JNI_PARAMS JNIEnv*, jclass
35 #define CRITICAL_JNI_PARAMS_COMMA JNIEnv*, jclass,
36 #endif
37 
38 namespace android {
39 
40 // Defines some helpful functions.
41 
FindClassOrDie(JNIEnv * env,const char * class_name)42 static inline jclass FindClassOrDie(JNIEnv* env, const char* class_name) {
43     jclass clazz = env->FindClass(class_name);
44     LOG_ALWAYS_FATAL_IF(clazz == NULL, "Unable to find class %s", class_name);
45     return clazz;
46 }
47 
GetFieldIDOrDie(JNIEnv * env,jclass clazz,const char * field_name,const char * field_signature)48 static inline jfieldID GetFieldIDOrDie(JNIEnv* env, jclass clazz, const char* field_name,
49                                        const char* field_signature) {
50     jfieldID res = env->GetFieldID(clazz, field_name, field_signature);
51     LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static field %s", field_name);
52     return res;
53 }
54 
GetMethodIDOrDie(JNIEnv * env,jclass clazz,const char * method_name,const char * method_signature)55 static inline jmethodID GetMethodIDOrDie(JNIEnv* env, jclass clazz, const char* method_name,
56                                          const char* method_signature) {
57     jmethodID res = env->GetMethodID(clazz, method_name, method_signature);
58     LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find method %s", method_name);
59     return res;
60 }
61 
GetStaticFieldIDOrDie(JNIEnv * env,jclass clazz,const char * field_name,const char * field_signature)62 static inline jfieldID GetStaticFieldIDOrDie(JNIEnv* env, jclass clazz, const char* field_name,
63                                              const char* field_signature) {
64     jfieldID res = env->GetStaticFieldID(clazz, field_name, field_signature);
65     LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static field %s", field_name);
66     return res;
67 }
68 
GetStaticMethodIDOrDie(JNIEnv * env,jclass clazz,const char * method_name,const char * method_signature)69 static inline jmethodID GetStaticMethodIDOrDie(JNIEnv* env, jclass clazz, const char* method_name,
70                                                const char* method_signature) {
71     jmethodID res = env->GetStaticMethodID(clazz, method_name, method_signature);
72     LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static method %s", method_name);
73     return res;
74 }
75 
76 template <typename T>
MakeGlobalRefOrDie(JNIEnv * env,T in)77 static inline T MakeGlobalRefOrDie(JNIEnv* env, T in) {
78     jobject res = env->NewGlobalRef(in);
79     LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to create global reference.");
80     return static_cast<T>(res);
81 }
82 
83 //  Inline variable that specifies the method binding format.
84 //  The expected format is 'XX${method}XX', where ${method} represents the original method name.
85 //  This variable is shared across all translation units. This is treated as a global variable as
86 //  per C++ 17.
87 inline std::string jniMethodFormat;
88 
setJniMethodFormat(std::string value)89 inline static void setJniMethodFormat(std::string value) {
90     jniMethodFormat = value;
91 }
92 
93 // Register the native methods, potenially applying the jniMethodFormat if it has been set.
jniRegisterMaybeRenamedNativeMethods(JNIEnv * env,const char * className,const JNINativeMethod * gMethods,int numMethods)94 static inline int jniRegisterMaybeRenamedNativeMethods(JNIEnv* env, const char* className,
95                                                        const JNINativeMethod* gMethods,
96                                                        int numMethods) {
97     if (jniMethodFormat.empty()) {
98         return jniRegisterNativeMethods(env, className, gMethods, numMethods);
99     }
100 
101     // Make a copy of gMethods with reformatted method names.
102     JNINativeMethod* modifiedMethods = new JNINativeMethod[numMethods];
103     LOG_ALWAYS_FATAL_IF(!modifiedMethods, "Failed to allocate a copy of the JNI methods");
104 
105     size_t methodNamePos = jniMethodFormat.find("${method}");
106     LOG_ALWAYS_FATAL_IF(methodNamePos == std::string::npos,
107                         "Invalid jniMethodFormat: could not find '${method}' in pattern");
108 
109     for (int i = 0; i < numMethods; i++) {
110         modifiedMethods[i] = gMethods[i];
111         std::string modifiedName = jniMethodFormat;
112         modifiedName.replace(methodNamePos, 9, gMethods[i].name);
113         char* modifiedNameChars = new char[modifiedName.length() + 1];
114         LOG_ALWAYS_FATAL_IF(!modifiedNameChars, "Failed to allocate the new method name");
115         std::strcpy(modifiedNameChars, modifiedName.c_str());
116         modifiedMethods[i].name = modifiedNameChars;
117     }
118     int res = jniRegisterNativeMethods(env, className, modifiedMethods, numMethods);
119     for (int i = 0; i < numMethods; i++) {
120         delete[] modifiedMethods[i].name;
121     }
122     delete[] modifiedMethods;
123     return res;
124 }
125 
RegisterMethodsOrDie(JNIEnv * env,const char * className,const JNINativeMethod * gMethods,int numMethods)126 static inline int RegisterMethodsOrDie(JNIEnv* env, const char* className,
127                                        const JNINativeMethod* gMethods, int numMethods) {
128     int res = jniRegisterMaybeRenamedNativeMethods(env, className, gMethods, numMethods);
129     LOG_ALWAYS_FATAL_IF(res < 0, "Unable to register native methods.");
130     return res;
131 }
132 
133 /**
134  * Read the specified field from jobject, and convert to std::string.
135  * If the field cannot be obtained, return defaultValue.
136  */
getStringField(JNIEnv * env,jobject obj,jfieldID fieldId,const char * defaultValue)137 static inline std::string getStringField(JNIEnv* env, jobject obj, jfieldID fieldId,
138         const char* defaultValue) {
139     ScopedLocalRef<jstring> strObj(env, jstring(env->GetObjectField(obj, fieldId)));
140     if (strObj != nullptr) {
141         ScopedUtfChars chars(env, strObj.get());
142         return std::string(chars.c_str());
143     }
144     return std::string(defaultValue);
145 }
146 
147 }  // namespace android
148 
149 #endif  // GRAPHICS_JNI_HELPERS
150