1 /*
2  * Copyright (C) 2022 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 #include "jni_common.h"
17 
18 #include <jni.h>
19 #include <ui/GraphicTypes.h>
20 #include <ui/Rect.h>
21 
22 #include "core_jni_helpers.h"
23 
24 // ----------------------------------------------------------------------------
25 
26 namespace android {
27 
28 static struct {
29     jclass clazz;
30     jmethodID ctor;
31     jfieldID bottom;
32     jfieldID left;
33     jfieldID right;
34     jfieldID top;
35 } gRectClassInfo;
36 
37 static struct {
38     jclass clazz;
39     jmethodID ctor;
40 } gSizeClassInfo;
41 
rectFromObj(JNIEnv * env,jobject rectObj)42 Rect JNICommon::rectFromObj(JNIEnv* env, jobject rectObj) {
43     int left = env->GetIntField(rectObj, gRectClassInfo.left);
44     int top = env->GetIntField(rectObj, gRectClassInfo.top);
45     int right = env->GetIntField(rectObj, gRectClassInfo.right);
46     int bottom = env->GetIntField(rectObj, gRectClassInfo.bottom);
47     return Rect(left, top, right, bottom);
48 }
49 
objFromRect(JNIEnv * env,Rect rect)50 jobject JNICommon::objFromRect(JNIEnv* env, Rect rect) {
51     return env->NewObject(gRectClassInfo.clazz, gRectClassInfo.ctor, rect.left, rect.top,
52                           rect.right, rect.bottom);
53 }
54 
objFromSize(JNIEnv * env,Size size)55 jobject JNICommon::objFromSize(JNIEnv* env, Size size) {
56     return env->NewObject(gSizeClassInfo.clazz, gSizeClassInfo.ctor, size.width, size.height);
57 }
58 
register_jni_common(JNIEnv * env)59 int register_jni_common(JNIEnv* env) {
60     jclass rectClazz = FindClassOrDie(env, "android/graphics/Rect");
61     gRectClassInfo.clazz = MakeGlobalRefOrDie(env, rectClazz);
62     gRectClassInfo.ctor = GetMethodIDOrDie(env, rectClazz, "<init>", "(IIII)V");
63     gRectClassInfo.bottom = GetFieldIDOrDie(env, rectClazz, "bottom", "I");
64     gRectClassInfo.left = GetFieldIDOrDie(env, rectClazz, "left", "I");
65     gRectClassInfo.right = GetFieldIDOrDie(env, rectClazz, "right", "I");
66     gRectClassInfo.top = GetFieldIDOrDie(env, rectClazz, "top", "I");
67 
68     jclass sizeClazz = FindClassOrDie(env, "android/util/Size");
69     gSizeClassInfo.clazz = MakeGlobalRefOrDie(env, sizeClazz);
70     gSizeClassInfo.ctor = GetMethodIDOrDie(env, sizeClazz, "<init>", "(II)V");
71 
72     return 0;
73 }
74 
75 } // namespace android
76