1 /*
2  * Copyright 2020 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 "DisplayManagerGlobal-JNI"
18 
19 #include <nativehelper/JNIHelp.h>
20 #include <nativehelper/ScopedUtfChars.h>
21 #include <private/android/choreographer.h>
22 
23 #include <vector>
24 
25 #include "core_jni_helpers.h"
26 
27 using namespace android;
28 
29 namespace android {
30 
31 // Dispatches the current refresh rate for the default display to all
32 // choreographer instances
android_hardware_display_DisplayManagerGlobal_signalNativeCallbacks(JNIEnv * env,jobject,jfloat refreshRate)33 void android_hardware_display_DisplayManagerGlobal_signalNativeCallbacks(JNIEnv* env, jobject,
34                                                                          jfloat refreshRate) {
35     const constexpr int64_t kNanosPerSecond = 1000 * 1000 * 1000;
36     const nsecs_t vsyncPeriod = kNanosPerSecond / refreshRate;
37 
38     AChoreographer_signalRefreshRateCallbacks(vsyncPeriod);
39 }
40 
41 } // namespace android
42 
43 // ----------------------------------------------------------------------------
44 
45 const char* const kClassPathName = "android/hardware/display/DisplayManagerGlobal";
46 
47 static const JNINativeMethod gMethods[] = {
48         {"nSignalNativeCallbacks", "(F)V",
49          (void*)android_hardware_display_DisplayManagerGlobal_signalNativeCallbacks},
50 };
51 
register_android_hardware_display_DisplayManagerGlobal(JNIEnv * env)52 int register_android_hardware_display_DisplayManagerGlobal(JNIEnv* env) {
53     AChoreographer_initJVM(env);
54     return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
55 }
56