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/LutProperties.h>
21 #include <android/gui/OverlayProperties.h>
22 #include <binder/Parcel.h>
23 #include <gui/SurfaceComposerClient.h>
24 #include <nativehelper/JNIHelp.h>
25
26 #include "android_os_Parcel.h"
27 #include "core_jni_helpers.h"
28 #include "jni.h"
29
30 using namespace android;
31
32 // ----------------------------------------------------------------------------
33 // Types
34 // ----------------------------------------------------------------------------
35 static struct {
36 jclass clazz;
37 jmethodID ctor;
38 } gOverlayPropertiesClassInfo;
39
40 static struct {
41 jclass clazz;
42 jmethodID ctor;
43 } gLutPropertiesClassInfo;
44
45 // ----------------------------------------------------------------------------
46 // OverlayProperties lifecycle
47 // ----------------------------------------------------------------------------
48
destroyOverlayProperties(gui::OverlayProperties * overlayProperties)49 static void destroyOverlayProperties(gui::OverlayProperties* overlayProperties) {
50 delete overlayProperties;
51 }
52
android_hardware_OverlayProperties_getDestructor(JNIEnv *,jclass)53 static jlong android_hardware_OverlayProperties_getDestructor(JNIEnv*, jclass) {
54 return static_cast<jlong>(reinterpret_cast<uintptr_t>(&destroyOverlayProperties));
55 }
56
57 //----------------------------------------------------------------------------
58 // Accessors
59 // ----------------------------------------------------------------------------
60
android_hardware_OverlayProperties_isCombinationSupported(JNIEnv * env,jobject thiz,jlong nativeObject,jint dataspace,jint format)61 static jboolean android_hardware_OverlayProperties_isCombinationSupported(JNIEnv* env, jobject thiz,
62 jlong nativeObject,
63 jint dataspace,
64 jint format) {
65 gui::OverlayProperties* properties = reinterpret_cast<gui::OverlayProperties*>(nativeObject);
66 if (properties != nullptr) {
67 for (const auto& i : properties->combinations) {
68 if (std::find(i.pixelFormats.begin(), i.pixelFormats.end(), format) !=
69 i.pixelFormats.end() &&
70 std::find(i.standards.begin(), i.standards.end(),
71 dataspace & HAL_DATASPACE_STANDARD_MASK) != i.standards.end() &&
72 std::find(i.transfers.begin(), i.transfers.end(),
73 dataspace & HAL_DATASPACE_TRANSFER_MASK) != i.transfers.end() &&
74 std::find(i.ranges.begin(), i.ranges.end(), dataspace & HAL_DATASPACE_RANGE_MASK) !=
75 i.ranges.end()) {
76 return true;
77 }
78 }
79 }
80 return false;
81 }
82
android_hardware_OverlayProperties_supportMixedColorSpaces(JNIEnv * env,jobject thiz,jlong nativeObject)83 static jboolean android_hardware_OverlayProperties_supportMixedColorSpaces(JNIEnv* env,
84 jobject thiz,
85 jlong nativeObject) {
86 gui::OverlayProperties* properties = reinterpret_cast<gui::OverlayProperties*>(nativeObject);
87 if (properties != nullptr && properties->supportMixedColorSpaces) {
88 return true;
89 }
90 return false;
91 }
92
android_hardware_OverlayProperties_createDefault(JNIEnv * env,jobject thiz)93 static jlong android_hardware_OverlayProperties_createDefault(JNIEnv* env, jobject thiz) {
94 gui::OverlayProperties* overlayProperties = new gui::OverlayProperties;
95 gui::OverlayProperties::SupportedBufferCombinations combination;
96 combination.pixelFormats = {HAL_PIXEL_FORMAT_RGBA_8888};
97 combination.standards = {HAL_DATASPACE_STANDARD_BT709};
98 combination.transfers = {HAL_DATASPACE_TRANSFER_SRGB};
99 combination.ranges = {HAL_DATASPACE_RANGE_FULL};
100 overlayProperties->combinations.emplace_back(combination);
101 overlayProperties->supportMixedColorSpaces = true;
102 return reinterpret_cast<jlong>(overlayProperties);
103 }
104
android_hardware_OverlayProperties_getLutProperties(JNIEnv * env,jobject thiz,jlong nativeObject)105 static jobjectArray android_hardware_OverlayProperties_getLutProperties(JNIEnv* env, jobject thiz,
106 jlong nativeObject) {
107 gui::OverlayProperties* overlayProperties =
108 reinterpret_cast<gui::OverlayProperties*>(nativeObject);
109 if (!overlayProperties || !overlayProperties->lutProperties) {
110 return NULL;
111 }
112 auto& lutProperties = overlayProperties->lutProperties.value();
113 if (lutProperties.empty()) {
114 return NULL;
115 }
116 int32_t size = static_cast<int32_t>(lutProperties.size());
117 jobjectArray nativeLutProperties =
118 env->NewObjectArray(size, gLutPropertiesClassInfo.clazz, NULL);
119 if (nativeLutProperties == NULL) {
120 return NULL;
121 }
122 for (int32_t i = 0; i < size; i++) {
123 if (lutProperties[i].has_value()) {
124 auto& item = lutProperties[i].value();
125 jobject properties =
126 env->NewObject(gLutPropertiesClassInfo.clazz, gLutPropertiesClassInfo.ctor,
127 static_cast<int32_t>(item.dimension), item.size,
128 item.samplingKeys.data());
129 env->SetObjectArrayElement(nativeLutProperties, i, properties);
130 }
131 }
132 return nativeLutProperties;
133 }
134
135 // ----------------------------------------------------------------------------
136 // Serialization
137 // ----------------------------------------------------------------------------
138
android_hardware_OverlayProperties_write(JNIEnv * env,jclass,jlong nativeObject,jobject dest)139 static void android_hardware_OverlayProperties_write(JNIEnv* env, jclass, jlong nativeObject,
140 jobject dest) {
141 Parcel* parcel = parcelForJavaObject(env, dest);
142 if (parcel == nullptr) {
143 jniThrowNullPointerException(env, nullptr);
144 return;
145 }
146 gui::OverlayProperties* overlayProperties =
147 reinterpret_cast<gui::OverlayProperties*>(nativeObject);
148 if (overlayProperties != nullptr) {
149 overlayProperties->writeToParcel(parcel);
150 }
151 }
152
android_hardware_OverlayProperties_read(JNIEnv * env,jclass,jobject in)153 static long android_hardware_OverlayProperties_read(JNIEnv* env, jclass, jobject in) {
154 Parcel* parcel = parcelForJavaObject(env, in);
155 if (parcel == nullptr) {
156 jniThrowNullPointerException(env, nullptr);
157 return 0;
158 }
159 gui::OverlayProperties* overlayProperties = new gui::OverlayProperties;
160 if (overlayProperties->readFromParcel(parcel) != NO_ERROR) {
161 delete overlayProperties;
162 return 0;
163 }
164 return reinterpret_cast<jlong>(overlayProperties);
165 }
166
167 // ----------------------------------------------------------------------------
168 // Public functions
169 // ----------------------------------------------------------------------------
170
171 namespace android {
172
android_hardware_OverlayProperties_convertToJavaObject(JNIEnv * env,gui::OverlayProperties * overlayProperties)173 jobject android_hardware_OverlayProperties_convertToJavaObject(
174 JNIEnv* env, gui::OverlayProperties* overlayProperties) {
175 jobject overlayPropertiesObj =
176 env->NewObject(gOverlayPropertiesClassInfo.clazz, gOverlayPropertiesClassInfo.ctor,
177 reinterpret_cast<jlong>(overlayProperties));
178 return overlayPropertiesObj;
179 }
180
181 }; // namespace android
182
183 // ----------------------------------------------------------------------------
184 // JNI Glue
185 // ----------------------------------------------------------------------------
186
187 const char* const kClassPathName = "android/hardware/OverlayProperties";
188
189 // clang-format off
190 static const JNINativeMethod gMethods[] = {
191 { "nGetDestructor", "()J", (void*) android_hardware_OverlayProperties_getDestructor },
192 { "nIsCombinationSupported", "(JII)Z",
193 (void*) android_hardware_OverlayProperties_isCombinationSupported },
194 { "nSupportMixedColorSpaces", "(J)Z",
195 (void*) android_hardware_OverlayProperties_supportMixedColorSpaces },
196 { "nWriteOverlayPropertiesToParcel", "(JLandroid/os/Parcel;)V",
197 (void*) android_hardware_OverlayProperties_write },
198 { "nReadOverlayPropertiesFromParcel", "(Landroid/os/Parcel;)J",
199 (void*) android_hardware_OverlayProperties_read },
200 {"nCreateDefault", "()J", (void*) android_hardware_OverlayProperties_createDefault },
201 {"nGetLutProperties", "(J)[Landroid/hardware/LutProperties;",
202 (void*) android_hardware_OverlayProperties_getLutProperties },
203 };
204 // clang-format on
205
register_android_hardware_OverlayProperties(JNIEnv * env)206 int register_android_hardware_OverlayProperties(JNIEnv* env) {
207 int err = RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
208
209 jclass clazz = FindClassOrDie(env, "android/hardware/OverlayProperties");
210 gOverlayPropertiesClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
211 gOverlayPropertiesClassInfo.ctor =
212 GetMethodIDOrDie(env, gOverlayPropertiesClassInfo.clazz, "<init>", "(J)V");
213 clazz = FindClassOrDie(env, "android/hardware/LutProperties");
214 gLutPropertiesClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
215 gLutPropertiesClassInfo.ctor =
216 GetMethodIDOrDie(env, gLutPropertiesClassInfo.clazz, "<init>", "(II[I)V");
217 return err;
218 }
219