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 intClass = FindClassOrDie(env, "java/lang/Integer");
60     static const jmethodID byteValue = env->GetMethodID(intClass, "byteValue", "()B");
61 
62     viewport->displayId = ui::LogicalDisplayId{
63             env->GetIntField(viewportObj, gDisplayViewportClassInfo.displayId)};
64     viewport->isActive = env->GetBooleanField(viewportObj, gDisplayViewportClassInfo.isActive);
65     jint orientation = env->GetIntField(viewportObj, gDisplayViewportClassInfo.orientation);
66     viewport->orientation = static_cast<ui::Rotation>(orientation);
67     viewport->deviceWidth = env->GetIntField(viewportObj, gDisplayViewportClassInfo.deviceWidth);
68     viewport->deviceHeight = env->GetIntField(viewportObj, gDisplayViewportClassInfo.deviceHeight);
69 
70     jstring uniqueId =
71             jstring(env->GetObjectField(viewportObj, gDisplayViewportClassInfo.uniqueId));
72     if (uniqueId != nullptr) {
73         viewport->uniqueId = ScopedUtfChars(env, uniqueId).c_str();
74     }
75 
76     viewport->physicalPort = std::nullopt;
77     jobject physicalPort = env->GetObjectField(viewportObj, gDisplayViewportClassInfo.physicalPort);
78     if (physicalPort != nullptr) {
79         viewport->physicalPort = std::make_optional(env->CallByteMethod(physicalPort, byteValue));
80     }
81 
82     viewport->type = static_cast<ViewportType>(env->GetIntField(viewportObj,
83                 gDisplayViewportClassInfo.type));
84 
85     jobject logicalFrameObj =
86             env->GetObjectField(viewportObj, gDisplayViewportClassInfo.logicalFrame);
87     viewport->logicalLeft = env->GetIntField(logicalFrameObj, gRectClassInfo.left);
88     viewport->logicalTop = env->GetIntField(logicalFrameObj, gRectClassInfo.top);
89     viewport->logicalRight = env->GetIntField(logicalFrameObj, gRectClassInfo.right);
90     viewport->logicalBottom = env->GetIntField(logicalFrameObj, gRectClassInfo.bottom);
91 
92     jobject physicalFrameObj =
93             env->GetObjectField(viewportObj, gDisplayViewportClassInfo.physicalFrame);
94     viewport->physicalLeft = env->GetIntField(physicalFrameObj, gRectClassInfo.left);
95     viewport->physicalTop = env->GetIntField(physicalFrameObj, gRectClassInfo.top);
96     viewport->physicalRight = env->GetIntField(physicalFrameObj, gRectClassInfo.right);
97     viewport->physicalBottom = env->GetIntField(physicalFrameObj, gRectClassInfo.bottom);
98 
99     return OK;
100 }
101 
102 // ----------------------------------------------------------------------------
103 
register_android_hardware_display_DisplayViewport(JNIEnv * env)104 int register_android_hardware_display_DisplayViewport(JNIEnv* env) {
105     jclass clazz = FindClassOrDie(env, "android/hardware/display/DisplayViewport");
106     gDisplayViewportClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
107 
108     gDisplayViewportClassInfo.displayId = GetFieldIDOrDie(env,
109             gDisplayViewportClassInfo.clazz, "displayId", "I");
110 
111     gDisplayViewportClassInfo.isActive =
112             GetFieldIDOrDie(env, gDisplayViewportClassInfo.clazz, "isActive", "Z");
113 
114     gDisplayViewportClassInfo.orientation = GetFieldIDOrDie(env,
115             gDisplayViewportClassInfo.clazz, "orientation", "I");
116 
117     gDisplayViewportClassInfo.deviceWidth = GetFieldIDOrDie(env,
118             gDisplayViewportClassInfo.clazz, "deviceWidth", "I");
119 
120     gDisplayViewportClassInfo.deviceHeight = GetFieldIDOrDie(env,
121             gDisplayViewportClassInfo.clazz, "deviceHeight", "I");
122 
123     gDisplayViewportClassInfo.logicalFrame = GetFieldIDOrDie(env,
124             gDisplayViewportClassInfo.clazz, "logicalFrame", "Landroid/graphics/Rect;");
125 
126     gDisplayViewportClassInfo.physicalFrame = GetFieldIDOrDie(env,
127             gDisplayViewportClassInfo.clazz, "physicalFrame", "Landroid/graphics/Rect;");
128 
129     gDisplayViewportClassInfo.uniqueId = GetFieldIDOrDie(env,
130             gDisplayViewportClassInfo.clazz, "uniqueId", "Ljava/lang/String;");
131 
132     gDisplayViewportClassInfo.physicalPort = GetFieldIDOrDie(env, gDisplayViewportClassInfo.clazz,
133                                                              "physicalPort", "Ljava/lang/Integer;");
134 
135     gDisplayViewportClassInfo.type = GetFieldIDOrDie(env,
136             gDisplayViewportClassInfo.clazz, "type", "I");
137 
138     clazz = FindClassOrDie(env, "android/graphics/Rect");
139     gRectClassInfo.left = GetFieldIDOrDie(env, clazz, "left", "I");
140     gRectClassInfo.top = GetFieldIDOrDie(env, clazz, "top", "I");
141     gRectClassInfo.right = GetFieldIDOrDie(env, clazz, "right", "I");
142     gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I");
143 
144     return 0;
145 }
146 
147 } // namespace android
148