1 /*
2  * Copyright (C) 2017 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 #define LOG_TAG "DisplayViewport-JNI"
18 
19 #include <nativehelper/JNIHelp.h>
20 #include "core_jni_helpers.h"
21 
22 #include <android_hardware_display_DisplayViewport.h>
23 #include <android_runtime/AndroidRuntime.h>
24 #include <android_runtime/Log.h>
25 #include <utils/Log.h>
26 
27 #include <nativehelper/ScopedUtfChars.h>
28 
29 namespace android {
30 
31 // ----------------------------------------------------------------------------
32 
33 static struct {
34     jclass clazz;
35 
36     jfieldID displayId;
37     jfieldID isActive;
38     jfieldID orientation;
39     jfieldID logicalFrame;
40     jfieldID physicalFrame;
41     jfieldID deviceWidth;
42     jfieldID deviceHeight;
43     jfieldID uniqueId;
44     jfieldID physicalPort;
45     jfieldID type;
46 } gDisplayViewportClassInfo;
47 
48 static struct {
49     jfieldID left;
50     jfieldID top;
51     jfieldID right;
52     jfieldID bottom;
53 } gRectClassInfo;
54 
55 // ----------------------------------------------------------------------------
56 
android_hardware_display_DisplayViewport_toNative(JNIEnv * env,jobject viewportObj,DisplayViewport * viewport)57 status_t android_hardware_display_DisplayViewport_toNative(JNIEnv* env, jobject viewportObj,
58         DisplayViewport* viewport) {
59     static const jclass byteClass = FindClassOrDie(env, "java/lang/Byte");
60     static const jmethodID byteValue = env->GetMethodID(byteClass, "byteValue", "()B");
61 
62     viewport->displayId = env->GetIntField(viewportObj, gDisplayViewportClassInfo.displayId);
63     viewport->isActive = env->GetBooleanField(viewportObj, gDisplayViewportClassInfo.isActive);
64     viewport->orientation = env->GetIntField(viewportObj, gDisplayViewportClassInfo.orientation);
65     viewport->deviceWidth = env->GetIntField(viewportObj, gDisplayViewportClassInfo.deviceWidth);
66     viewport->deviceHeight = env->GetIntField(viewportObj, gDisplayViewportClassInfo.deviceHeight);
67 
68     jstring uniqueId =
69             jstring(env->GetObjectField(viewportObj, gDisplayViewportClassInfo.uniqueId));
70     if (uniqueId != nullptr) {
71         viewport->uniqueId = ScopedUtfChars(env, uniqueId).c_str();
72     }
73 
74     viewport->physicalPort = std::nullopt;
75     jobject physicalPort = env->GetObjectField(viewportObj, gDisplayViewportClassInfo.physicalPort);
76     if (physicalPort != nullptr) {
77         viewport->physicalPort = std::make_optional(env->CallByteMethod(physicalPort, byteValue));
78     }
79 
80     viewport->type = static_cast<ViewportType>(env->GetIntField(viewportObj,
81                 gDisplayViewportClassInfo.type));
82 
83     jobject logicalFrameObj =
84             env->GetObjectField(viewportObj, gDisplayViewportClassInfo.logicalFrame);
85     viewport->logicalLeft = env->GetIntField(logicalFrameObj, gRectClassInfo.left);
86     viewport->logicalTop = env->GetIntField(logicalFrameObj, gRectClassInfo.top);
87     viewport->logicalRight = env->GetIntField(logicalFrameObj, gRectClassInfo.right);
88     viewport->logicalBottom = env->GetIntField(logicalFrameObj, gRectClassInfo.bottom);
89 
90     jobject physicalFrameObj =
91             env->GetObjectField(viewportObj, gDisplayViewportClassInfo.physicalFrame);
92     viewport->physicalLeft = env->GetIntField(physicalFrameObj, gRectClassInfo.left);
93     viewport->physicalTop = env->GetIntField(physicalFrameObj, gRectClassInfo.top);
94     viewport->physicalRight = env->GetIntField(physicalFrameObj, gRectClassInfo.right);
95     viewport->physicalBottom = env->GetIntField(physicalFrameObj, gRectClassInfo.bottom);
96 
97     return OK;
98 }
99 
100 // ----------------------------------------------------------------------------
101 
register_android_hardware_display_DisplayViewport(JNIEnv * env)102 int register_android_hardware_display_DisplayViewport(JNIEnv* env) {
103     jclass clazz = FindClassOrDie(env, "android/hardware/display/DisplayViewport");
104     gDisplayViewportClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
105 
106     gDisplayViewportClassInfo.displayId = GetFieldIDOrDie(env,
107             gDisplayViewportClassInfo.clazz, "displayId", "I");
108 
109     gDisplayViewportClassInfo.isActive =
110             GetFieldIDOrDie(env, gDisplayViewportClassInfo.clazz, "isActive", "Z");
111 
112     gDisplayViewportClassInfo.orientation = GetFieldIDOrDie(env,
113             gDisplayViewportClassInfo.clazz, "orientation", "I");
114 
115     gDisplayViewportClassInfo.deviceWidth = GetFieldIDOrDie(env,
116             gDisplayViewportClassInfo.clazz, "deviceWidth", "I");
117 
118     gDisplayViewportClassInfo.deviceHeight = GetFieldIDOrDie(env,
119             gDisplayViewportClassInfo.clazz, "deviceHeight", "I");
120 
121     gDisplayViewportClassInfo.logicalFrame = GetFieldIDOrDie(env,
122             gDisplayViewportClassInfo.clazz, "logicalFrame", "Landroid/graphics/Rect;");
123 
124     gDisplayViewportClassInfo.physicalFrame = GetFieldIDOrDie(env,
125             gDisplayViewportClassInfo.clazz, "physicalFrame", "Landroid/graphics/Rect;");
126 
127     gDisplayViewportClassInfo.uniqueId = GetFieldIDOrDie(env,
128             gDisplayViewportClassInfo.clazz, "uniqueId", "Ljava/lang/String;");
129 
130     gDisplayViewportClassInfo.physicalPort = GetFieldIDOrDie(env,
131             gDisplayViewportClassInfo.clazz, "physicalPort", "Ljava/lang/Byte;");
132 
133     gDisplayViewportClassInfo.type = GetFieldIDOrDie(env,
134             gDisplayViewportClassInfo.clazz, "type", "I");
135 
136     clazz = FindClassOrDie(env, "android/graphics/Rect");
137     gRectClassInfo.left = GetFieldIDOrDie(env, clazz, "left", "I");
138     gRectClassInfo.top = GetFieldIDOrDie(env, clazz, "top", "I");
139     gRectClassInfo.right = GetFieldIDOrDie(env, clazz, "right", "I");
140     gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I");
141 
142     return 0;
143 }
144 
145 } // namespace android
146