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 "SyncFence"
18 
19 #include <nativehelper/JNIHelp.h>
20 #include <ui/Fence.h>
21 
22 #include "core_jni_helpers.h"
23 #include "jni.h"
24 
25 using namespace android;
26 
27 template <typename T>
toJlong(T * ptr)28 jlong toJlong(T* ptr) {
29     return static_cast<jlong>(reinterpret_cast<uintptr_t>(ptr));
30 }
31 
32 template <typename T>
fromJlong(jlong jPtr)33 T* fromJlong(jlong jPtr) {
34     return reinterpret_cast<T*>(static_cast<uintptr_t>(jPtr));
35 }
36 
destroyFence(Fence * fence)37 static void destroyFence(Fence* fence) {
38     fence->decStrong(0);
39 }
40 
SyncFence_getDestructor(JNIEnv *,jobject)41 static jlong SyncFence_getDestructor(JNIEnv*, jobject) {
42     return toJlong(&destroyFence);
43 }
44 
SyncFence_create(JNIEnv *,jobject,int fd)45 static jlong SyncFence_create(JNIEnv*, jobject, int fd) {
46     Fence* fence = new Fence(fd);
47     fence->incStrong(0);
48     return toJlong(fence);
49 }
50 
SyncFence_isValid(JNIEnv *,jobject,jlong jPtr)51 static jboolean SyncFence_isValid(JNIEnv*, jobject, jlong jPtr) {
52     return fromJlong<Fence>(jPtr)->isValid();
53 }
54 
SyncFence_getFd(JNIEnv *,jobject,jlong jPtr)55 static jint SyncFence_getFd(JNIEnv*, jobject, jlong jPtr) {
56     return fromJlong<Fence>(jPtr)->get();
57 }
58 
SyncFence_wait(JNIEnv * env,jobject,jlong jPtr,jlong timeoutNanos)59 static jboolean SyncFence_wait(JNIEnv* env, jobject, jlong jPtr, jlong timeoutNanos) {
60     Fence* fence = fromJlong<Fence>(jPtr);
61     int err = fence->wait(timeoutNanos);
62     return err == OK;
63 }
64 
SyncFence_getSignalTime(JNIEnv * env,jobject,jlong jPtr)65 static jlong SyncFence_getSignalTime(JNIEnv* env, jobject, jlong jPtr) {
66     return fromJlong<Fence>(jPtr)->getSignalTime();
67 }
68 
SyncFence_incRef(JNIEnv *,jobject,jlong jPtr)69 static void SyncFence_incRef(JNIEnv*, jobject, jlong jPtr) {
70     fromJlong<Fence>(jPtr)->incStrong((void*)SyncFence_incRef);
71 }
72 
73 // ----------------------------------------------------------------------------
74 // JNI Glue
75 // ----------------------------------------------------------------------------
76 
77 const char* const kClassPathName = "android/hardware/SyncFence";
78 
79 // clang-format off
80 static const JNINativeMethod gMethods[] = {
81         { "nGetDestructor", "()J", (void*) SyncFence_getDestructor },
82         { "nCreate", "(I)J", (void*) SyncFence_create },
83         { "nIsValid", "(J)Z", (void*) SyncFence_isValid },
84         { "nGetFd", "(J)I", (void*) SyncFence_getFd },
85         { "nWait",  "(JJ)Z", (void*) SyncFence_wait },
86         { "nGetSignalTime", "(J)J", (void*) SyncFence_getSignalTime },
87         { "nIncRef", "(J)V", (void*) SyncFence_incRef },
88 };
89 // clang-format on
90 
register_android_hardware_SyncFence(JNIEnv * env)91 int register_android_hardware_SyncFence(JNIEnv* env) {
92     int err = RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
93     return err;
94 }