Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6a5be8ea authored by Alec Mouri's avatar Alec Mouri Committed by Automerger Merge Worker
Browse files

Merge "Dispatch refresh rate callbacks from DMS" into rvc-dev am: 00f681a5...

Merge "Dispatch refresh rate callbacks from DMS" into rvc-dev am: 00f681a5 am: 151365a4 am: c94de5c5 am: f7f8d570

Change-Id: I912b29d038f9938b963818577b2c9644d7d5e023
parents 77cda822 f7f8d570
Loading
Loading
Loading
Loading
+67 −18
Original line number Diff line number Diff line
@@ -34,7 +34,6 @@ import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;
import android.util.SparseArray;
@@ -74,6 +73,8 @@ public final class DisplayManagerGlobal {
    @UnsupportedAppUsage
    private static DisplayManagerGlobal sInstance;

    // Guarded by mLock
    private boolean mDispatchNativeCallbacks = false;
    private final Object mLock = new Object();

    @UnsupportedAppUsage
@@ -143,6 +144,15 @@ public final class DisplayManagerGlobal {
    @UnsupportedAppUsage
    public DisplayInfo getDisplayInfo(int displayId) {
        synchronized (mLock) {
            return getDisplayInfoLocked(displayId);
        }
    }

    /**
     * Gets information about a particular logical display
     * See {@link getDisplayInfo}, but assumes that {@link mLock} is held
     */
    private @Nullable DisplayInfo getDisplayInfoLocked(int displayId) {
        DisplayInfo info = null;
        if (mDisplayCache != null) {
            info = mDisplayCache.query(displayId);
@@ -164,7 +174,6 @@ public final class DisplayManagerGlobal {
        }
        return info;
    }
    }

    /**
     * Gets all currently valid logical display ids.
@@ -341,6 +350,20 @@ public final class DisplayManagerGlobal {
            for (int i = 0; i < numListeners; i++) {
                mDisplayListeners.get(i).sendDisplayEvent(displayId, event);
            }
            if (event == EVENT_DISPLAY_CHANGED && mDispatchNativeCallbacks) {
                // Choreographer only supports a single display, so only dispatch refresh rate
                // changes for the default display.
                if (displayId == Display.DEFAULT_DISPLAY) {
                    // We can likely save a binder hop if we attach the refresh rate onto the
                    // listener.
                    DisplayInfo display = getDisplayInfoLocked(displayId);
                    if (display != null) {
                        float refreshRate = display.getMode().getRefreshRate();
                        // Signal native callbacks if we ever set a refresh rate.
                        nSignalNativeCallbacks(refreshRate);
                    }
                }
            }
        }
    }

@@ -800,4 +823,30 @@ public final class DisplayManagerGlobal {
    public void disableLocalDisplayInfoCaches() {
        mDisplayCache = null;
    }

    private static native void nSignalNativeCallbacks(float refreshRate);

    // Called from AChoreographer via JNI.
    // Registers AChoreographer so that refresh rate callbacks can be dispatched from DMS.
    private void registerNativeChoreographerForRefreshRateCallbacks() {
        synchronized (mLock) {
            registerCallbackIfNeededLocked();
            mDispatchNativeCallbacks = true;
            DisplayInfo display = getDisplayInfoLocked(Display.DEFAULT_DISPLAY);
            if (display != null) {
                // We need to tell AChoreographer instances the current refresh rate so that apps
                // can get it for free once a callback first registers.
                float refreshRate = display.getMode().getRefreshRate();
                nSignalNativeCallbacks(refreshRate);
            }
        }
    }

    // Called from AChoreographer via JNI.
    // Unregisters AChoreographer from receiving refresh rate callbacks.
    private void unregisterNativeChoreographerForRefreshRateCallbacks() {
        synchronized (mLock) {
            mDispatchNativeCallbacks = false;
        }
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -158,6 +158,7 @@ cc_library_shared {
                "android_hardware_camera2_legacy_LegacyCameraDevice.cpp",
                "android_hardware_camera2_legacy_PerfMeasurement.cpp",
                "android_hardware_camera2_DngCreator.cpp",
                "android_hardware_display_DisplayManagerGlobal.cpp",
                "android_hardware_display_DisplayViewport.cpp",
                "android_hardware_HardwareBuffer.cpp",
                "android_hardware_SensorManager.cpp",
+16 −16
Original line number Diff line number Diff line
@@ -18,39 +18,37 @@
#define LOG_TAG "AndroidRuntime"
#define LOG_NDEBUG 1

#include <android_runtime/AndroidRuntime.h>

#include <android-base/macros.h>
#include <android-base/properties.h>
#include <android/graphics/jni_runtime.h>
#include <android_runtime/AndroidRuntime.h>
#include <assert.h>
#include <binder/IBinder.h>
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <utils/Log.h>
#include <utils/misc.h>
#include <utils/Trace.h>
#include <binder/Parcel.h>
#include <utils/threads.h>
#include <bionic/malloc.h>
#include <cutils/properties.h>
#include <server_configurable_flags/get_flags.h>

#include "jni.h"
#include <dirent.h>
#include <dlfcn.h>
#include <nativehelper/JNIHelp.h>
#include <nativehelper/JniInvocation.h>
#include "android_util_Binder.h"

#include <stdio.h>
#include <server_configurable_flags/get_flags.h>
#include <signal.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <signal.h>
#include <dirent.h>
#include <assert.h>
#include <bionic/malloc.h>
#include <utils/Log.h>
#include <utils/Trace.h>
#include <utils/misc.h>
#include <utils/threads.h>

#include <string>
#include <vector>

#include "android_util_Binder.h"
#include "jni.h"

using namespace android;
using android::base::GetProperty;

@@ -78,6 +76,7 @@ extern int register_android_hardware_camera2_CameraMetadata(JNIEnv *env);
extern int register_android_hardware_camera2_legacy_LegacyCameraDevice(JNIEnv *env);
extern int register_android_hardware_camera2_legacy_PerfMeasurement(JNIEnv *env);
extern int register_android_hardware_camera2_DngCreator(JNIEnv *env);
extern int register_android_hardware_display_DisplayManagerGlobal(JNIEnv* env);
extern int register_android_hardware_HardwareBuffer(JNIEnv *env);
extern int register_android_hardware_SensorManager(JNIEnv *env);
extern int register_android_hardware_SerialPort(JNIEnv *env);
@@ -1519,6 +1518,7 @@ static const RegJNIRec gRegJNI[] = {
        REG_JNI(register_android_hardware_camera2_legacy_LegacyCameraDevice),
        REG_JNI(register_android_hardware_camera2_legacy_PerfMeasurement),
        REG_JNI(register_android_hardware_camera2_DngCreator),
        REG_JNI(register_android_hardware_display_DisplayManagerGlobal),
        REG_JNI(register_android_hardware_HardwareBuffer),
        REG_JNI(register_android_hardware_SensorManager),
        REG_JNI(register_android_hardware_SerialPort),
+55 −0
Original line number Diff line number Diff line
/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "DisplayManagerGlobal-JNI"

#include <nativehelper/JNIHelp.h>
#include <nativehelper/ScopedUtfChars.h>
#include <private/android/choreographer.h>

#include <vector>

#include "core_jni_helpers.h"

using namespace android;

namespace android {

// Dispatches the current refresh rate for the default display to all
// choreographer instances
void android_hardware_display_DisplayManagerGlobal_signalNativeCallbacks(JNIEnv* env, jobject,
                                                                         jfloat refreshRate) {
    const constexpr int64_t kNanosPerSecond = 1000 * 1000 * 1000;
    const nsecs_t vsyncPeriod = kNanosPerSecond / refreshRate;

    AChoreographer_signalRefreshRateCallbacks(vsyncPeriod);
}

} // namespace android

// ----------------------------------------------------------------------------

const char* const kClassPathName = "android/hardware/display/DisplayManagerGlobal";

static const JNINativeMethod gMethods[] = {
        {"nSignalNativeCallbacks", "(F)V",
         (void*)android_hardware_display_DisplayManagerGlobal_signalNativeCallbacks},
};

int register_android_hardware_display_DisplayManagerGlobal(JNIEnv* env) {
    AChoreographer_initJVM(env);
    return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
}
+5 −6
Original line number Diff line number Diff line
@@ -19,15 +19,14 @@
#ifndef _RUNTIME_ANDROID_RUNTIME_H
#define _RUNTIME_ANDROID_RUNTIME_H

#include <utils/Errors.h>
#include <binder/IBinder.h>
#include <utils/String8.h>
#include <jni.h>
#include <pthread.h>
#include <utils/Errors.h>
#include <utils/String16.h>
#include <utils/String8.h>
#include <utils/Vector.h>
#include <utils/threads.h>
#include <pthread.h>
#include <jni.h>


namespace android {

@@ -154,6 +153,6 @@ private:
    static int javaThreadShell(void* args);
};

}
} // namespace android

#endif
Loading