1 /*
2  * Copyright (C) 2023 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 "MotionPredictor-JNI"
18 
19 #include <input/Input.h>
20 #include <input/MotionPredictor.h>
21 
22 #include "android_view_MotionEvent.h"
23 #include "core_jni_helpers.h"
24 
25 /**
26  * This file is a bridge from Java to native for MotionPredictor class.
27  * It should be pass-through only. Do not store any state or put any business logic into this file.
28  */
29 
30 namespace android {
31 
release(void * ptr)32 static void release(void* ptr) {
33     delete reinterpret_cast<MotionPredictor*>(ptr);
34 }
35 
android_view_MotionPredictor_nativeGetNativeMotionPredictorFinalizer(JNIEnv * env,jclass clazz)36 static jlong android_view_MotionPredictor_nativeGetNativeMotionPredictorFinalizer(JNIEnv* env,
37                                                                                   jclass clazz) {
38     return reinterpret_cast<jlong>(release);
39 }
40 
android_view_MotionPredictor_nativeInitialize(JNIEnv * env,jclass clazz,jint offsetNanos)41 static jlong android_view_MotionPredictor_nativeInitialize(JNIEnv* env, jclass clazz,
42                                                            jint offsetNanos) {
43     const nsecs_t offset = static_cast<nsecs_t>(offsetNanos);
44     return reinterpret_cast<jlong>(new MotionPredictor(offset));
45 }
46 
android_view_MotionPredictor_nativeRecord(JNIEnv * env,jclass clazz,jlong ptr,jobject event)47 static void android_view_MotionPredictor_nativeRecord(JNIEnv* env, jclass clazz, jlong ptr,
48                                                       jobject event) {
49     MotionPredictor* predictor = reinterpret_cast<MotionPredictor*>(ptr);
50     MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, event);
51 
52     android::base::Result<void> result = predictor->record(*motionEvent);
53     if (!result.ok()) {
54         jniThrowException(env, "java/lang/IllegalArgumentException",
55                           result.error().message().c_str());
56     }
57 }
58 
android_view_MotionPredictor_nativePredict(JNIEnv * env,jclass clazz,jlong ptr,jlong predictionTimeNanos)59 static jobject android_view_MotionPredictor_nativePredict(JNIEnv* env, jclass clazz, jlong ptr,
60                                                           jlong predictionTimeNanos) {
61     MotionPredictor* predictor = reinterpret_cast<MotionPredictor*>(ptr);
62     return android_view_MotionEvent_obtainFromNative(env,
63                                                      predictor->predict(static_cast<nsecs_t>(
64                                                              predictionTimeNanos)));
65 }
66 
android_view_MotionPredictor_nativeIsPredictionAvailable(JNIEnv * env,jclass clazz,jlong ptr,jint deviceId,jint source)67 static jboolean android_view_MotionPredictor_nativeIsPredictionAvailable(JNIEnv* env, jclass clazz,
68                                                                          jlong ptr, jint deviceId,
69                                                                          jint source) {
70     MotionPredictor* predictor = reinterpret_cast<MotionPredictor*>(ptr);
71     return predictor->isPredictionAvailable(static_cast<int32_t>(deviceId),
72                                             static_cast<int32_t>(source));
73 }
74 
75 // ----------------------------------------------------------------------------
76 
77 static const std::array<JNINativeMethod, 5> gMotionPredictorMethods{{
78         /* name, signature, funcPtr */
79         {"nativeInitialize", "(I)J", (void*)android_view_MotionPredictor_nativeInitialize},
80         {"nativeGetNativeMotionPredictorFinalizer", "()J",
81          (void*)android_view_MotionPredictor_nativeGetNativeMotionPredictorFinalizer},
82         {"nativeRecord", "(JLandroid/view/MotionEvent;)V",
83          (void*)android_view_MotionPredictor_nativeRecord},
84         {"nativePredict", "(JJ)Landroid/view/MotionEvent;",
85          (void*)android_view_MotionPredictor_nativePredict},
86         {"nativeIsPredictionAvailable", "(JII)Z",
87          (void*)android_view_MotionPredictor_nativeIsPredictionAvailable},
88 }};
89 
register_android_view_MotionPredictor(JNIEnv * env)90 int register_android_view_MotionPredictor(JNIEnv* env) {
91     return RegisterMethodsOrDie(env, "android/view/MotionPredictor", gMotionPredictorMethods.data(),
92                                 gMotionPredictorMethods.size());
93 }
94 
95 } // namespace android
96