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 
17 #define LOG_TAG "OverlayProperties"
18 // #define LOG_NDEBUG 0
19 
20 #include <android/gui/OverlayProperties.h>
21 #include <binder/Parcel.h>
22 #include <gui/SurfaceComposerClient.h>
23 #include <nativehelper/JNIHelp.h>
24 
25 #include "android_os_Parcel.h"
26 #include "core_jni_helpers.h"
27 #include "jni.h"
28 
29 using namespace android;
30 
31 // ----------------------------------------------------------------------------
32 // Types
33 // ----------------------------------------------------------------------------
34 static struct {
35     jclass clazz;
36     jmethodID ctor;
37 } gOverlayPropertiesClassInfo;
38 
39 // ----------------------------------------------------------------------------
40 // OverlayProperties lifecycle
41 // ----------------------------------------------------------------------------
42 
destroyOverlayProperties(gui::OverlayProperties * overlayProperties)43 static void destroyOverlayProperties(gui::OverlayProperties* overlayProperties) {
44     delete overlayProperties;
45 }
46 
android_hardware_OverlayProperties_getDestructor(JNIEnv *,jclass)47 static jlong android_hardware_OverlayProperties_getDestructor(JNIEnv*, jclass) {
48     return static_cast<jlong>(reinterpret_cast<uintptr_t>(&destroyOverlayProperties));
49 }
50 
51 //----------------------------------------------------------------------------
52 // Accessors
53 // ----------------------------------------------------------------------------
54 
android_hardware_OverlayProperties_supportFp16ForHdr(JNIEnv * env,jobject thiz,jlong nativeObject)55 static jboolean android_hardware_OverlayProperties_supportFp16ForHdr(JNIEnv* env, jobject thiz,
56                                                                      jlong nativeObject) {
57     gui::OverlayProperties* properties = reinterpret_cast<gui::OverlayProperties*>(nativeObject);
58     if (properties != nullptr) {
59         for (const auto& i : properties->combinations) {
60             if (std::find(i.pixelFormats.begin(), i.pixelFormats.end(),
61                           static_cast<int32_t>(HAL_PIXEL_FORMAT_RGBA_FP16)) !=
62                         i.pixelFormats.end() &&
63                 std::find(i.standards.begin(), i.standards.end(),
64                           static_cast<int32_t>(HAL_DATASPACE_STANDARD_BT2020)) !=
65                         i.standards.end() &&
66                 std::find(i.transfers.begin(), i.transfers.end(),
67                           static_cast<int32_t>(HAL_DATASPACE_TRANSFER_ST2084)) !=
68                         i.transfers.end() &&
69                 std::find(i.ranges.begin(), i.ranges.end(),
70                           static_cast<int32_t>(HAL_DATASPACE_RANGE_FULL)) != i.ranges.end()) {
71                 return true;
72             }
73         }
74     }
75     return false;
76 }
77 
android_hardware_OverlayProperties_supportMixedColorSpaces(JNIEnv * env,jobject thiz,jlong nativeObject)78 static jboolean android_hardware_OverlayProperties_supportMixedColorSpaces(JNIEnv* env,
79                                                                            jobject thiz,
80                                                                            jlong nativeObject) {
81     gui::OverlayProperties* properties = reinterpret_cast<gui::OverlayProperties*>(nativeObject);
82     if (properties != nullptr && properties->supportMixedColorSpaces) {
83         return true;
84     }
85     return false;
86 }
87 
88 // ----------------------------------------------------------------------------
89 // Serialization
90 // ----------------------------------------------------------------------------
91 
android_hardware_OverlayProperties_write(JNIEnv * env,jclass,jlong nativeObject,jobject dest)92 static void android_hardware_OverlayProperties_write(JNIEnv* env, jclass, jlong nativeObject,
93                                                      jobject dest) {
94     Parcel* parcel = parcelForJavaObject(env, dest);
95     if (parcel == nullptr) {
96         jniThrowNullPointerException(env, nullptr);
97         return;
98     }
99     gui::OverlayProperties* overlayProperties =
100             reinterpret_cast<gui::OverlayProperties*>(nativeObject);
101     if (overlayProperties != nullptr) {
102         overlayProperties->writeToParcel(parcel);
103     }
104 }
105 
android_hardware_OverlayProperties_read(JNIEnv * env,jclass,jobject in)106 static long android_hardware_OverlayProperties_read(JNIEnv* env, jclass, jobject in) {
107     Parcel* parcel = parcelForJavaObject(env, in);
108     if (parcel == nullptr) {
109         jniThrowNullPointerException(env, nullptr);
110         return 0;
111     }
112     gui::OverlayProperties* overlayProperties = new gui::OverlayProperties;
113     if (overlayProperties->readFromParcel(parcel) != NO_ERROR) {
114         delete overlayProperties;
115         return 0;
116     }
117     return reinterpret_cast<jlong>(overlayProperties);
118 }
119 
120 // ----------------------------------------------------------------------------
121 // Public functions
122 // ----------------------------------------------------------------------------
123 
124 namespace android {
125 
android_hardware_OverlayProperties_convertToJavaObject(JNIEnv * env,gui::OverlayProperties * overlayProperties)126 jobject android_hardware_OverlayProperties_convertToJavaObject(
127         JNIEnv* env, gui::OverlayProperties* overlayProperties) {
128     jobject overlayPropertiesObj =
129             env->NewObject(gOverlayPropertiesClassInfo.clazz, gOverlayPropertiesClassInfo.ctor,
130                            reinterpret_cast<jlong>(overlayProperties));
131     return overlayPropertiesObj;
132 }
133 
134 }; // namespace android
135 
136 // ----------------------------------------------------------------------------
137 // JNI Glue
138 // ----------------------------------------------------------------------------
139 
140 const char* const kClassPathName = "android/hardware/OverlayProperties";
141 
142 // clang-format off
143 static const JNINativeMethod gMethods[] = {
144     { "nGetDestructor", "()J", (void*) android_hardware_OverlayProperties_getDestructor },
145     { "nSupportFp16ForHdr",  "(J)Z",
146             (void*)  android_hardware_OverlayProperties_supportFp16ForHdr },
147     { "nSupportMixedColorSpaces", "(J)Z",
148             (void*) android_hardware_OverlayProperties_supportMixedColorSpaces },
149     { "nWriteOverlayPropertiesToParcel", "(JLandroid/os/Parcel;)V",
150             (void*) android_hardware_OverlayProperties_write },
151     { "nReadOverlayPropertiesFromParcel", "(Landroid/os/Parcel;)J",
152             (void*) android_hardware_OverlayProperties_read },
153 };
154 // clang-format on
155 
register_android_hardware_OverlayProperties(JNIEnv * env)156 int register_android_hardware_OverlayProperties(JNIEnv* env) {
157     int err = RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
158 
159     jclass clazz = FindClassOrDie(env, "android/hardware/OverlayProperties");
160     gOverlayPropertiesClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
161     gOverlayPropertiesClassInfo.ctor =
162             GetMethodIDOrDie(env, gOverlayPropertiesClassInfo.clazz, "<init>", "(J)V");
163 
164     return err;
165 }
166