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

Commit d3b8331d authored by electimon's avatar electimon Committed by Daniel Jacob Chittoor
Browse files

PowerManager: Introduce PowerExt supported boosting APIs



* These new APIs allow for boosting via custom
  and arbitrary hints such as Fling, Animation, etc.

* The first supported custom hint will be,
  FLING_BOOST, followed by other implementations
  in the future.

Signed-off-by: default avatarbetaxab <betaxab@gmail.com>
Signed-off-by: default avatarelectimon <electimon@gmail.com>
Change-Id: Ieeeb821418df3c6af2d51115f9c30c6207dfd527
parent 88086cbe
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -34,6 +34,8 @@ interface IPowerManager
    @UnsupportedAppUsage
    void releaseWakeLock(IBinder lock, int flags);
    void updateWakeLockUids(IBinder lock, in int[] uids);
    oneway void setPowerExtMode(String mode_name, boolean enabled);
    oneway void setPowerExtBoost(String boost_name, int durationMs);
    oneway void setPowerBoost(int boost, int durationMs);
    oneway void setPowerMode(int mode, boolean enabled);

+47 −0
Original line number Diff line number Diff line
@@ -236,6 +236,31 @@ public abstract class PowerManagerInternal {
     */
    public static final int BOOST_DISPLAY_UPDATE_IMMINENT = 1;

    /**
     * SetPowerExtMode() is called to enable/disable specific hint mode, which
     * may result in adjustment of power/performance parameters of the
     * cpufreq governor and other controls on device side.
     *
     * @param string mode_name which is to be enable/disable.
     * @param enabled true to enable, false to disable the mode.
     */
    public abstract void setPowerExtMode(String mode_name, boolean enabled);

    /**
     * SetPowerExtBoost() indicates the device may need to boost some resources, as
     * the load is likely to increase before the kernel governors can react.
     * Depending on the boost, it may be appropriate to raise the frequencies of
     * CPU, GPU, memory subsystem, or stop CPU from going into deep sleep state.
     *
     * @param boost_name, which is the boost name from PowerExtBoosts enum to be set
     * @param durationMs The expected duration of the user's interaction, if
     *        known, or 0 if the expected duration is unknown.
     *        a negative value indicates canceling previous boost.
     *        A given platform can choose to boost some time based on durationMs,
     *        and may also pick an appropriate timeout for 0 case.
     */
    public abstract void setPowerExtBoost(String boost_name, int durationMs);

    /**
     * SetPowerBoost() indicates the device may need to boost some resources, as
     * the load is likely to increase before the kernel governors can react.
@@ -357,4 +382,26 @@ public abstract class PowerManagerInternal {
     * return false if ambient display is not available.
     */
    public abstract boolean isAmbientDisplaySuppressed();

    /**
     * This parcelable enum contains the platform supported custom boosts along with defined fallback
     * standard boosts for when the Extension HAL isn't available.
     */
    public enum PowerExtBoosts {
        // Custom boosts list
        FLING_BOOST(BOOST_INTERACTION);
        // End boosts list

        // Fallback boost for given value
        private final int mFallback;

        PowerExtBoosts(int fallback) {
            mFallback = fallback;
        }

        public int getFallback() {
            return mFallback;
        }
        // End fallback boost
    }
}
+57 −0
Original line number Diff line number Diff line
@@ -80,6 +80,7 @@ import android.os.PowerManager.GoToSleepReason;
import android.os.PowerManager.ServiceType;
import android.os.PowerManager.WakeReason;
import android.os.PowerManagerInternal;
import android.os.PowerManagerInternal.PowerExtBoosts;
import android.os.PowerSaveState;
import android.os.Process;
import android.os.RemoteException;
@@ -934,6 +935,16 @@ public final class PowerManagerService extends SystemService
            PowerManagerService.nativeSetAutoSuspend(enable);
        }

        /** Wrapper for PowerManager.nativeSetPowerExtMode */
        public void nativeSetPowerExtMode(String mode, int fallback, boolean enabled) {
            PowerManagerService.nativeSetPowerExtMode(mode, fallback, enabled);
        }

        /** Wrapper for PowerManager.nativeSetPowerExtBoost */
        public void nativeSetPowerExtBoost(String boost, int fallback, int durationMs) {
            PowerManagerService.nativeSetPowerExtBoost(boost, fallback, durationMs);
        }

        /** Wrapper for PowerManager.nativeSetPowerBoost */
        public void nativeSetPowerBoost(int boost, int durationMs) {
            PowerManagerService.nativeSetPowerBoost(boost, durationMs);
@@ -1059,6 +1070,8 @@ public final class PowerManagerService extends SystemService
    private static native void nativeAcquireSuspendBlocker(String name);
    private static native void nativeReleaseSuspendBlocker(String name);
    private static native void nativeSetAutoSuspend(boolean enable);
    private static native void nativeSetPowerExtMode(String mode, int fallback, boolean enabled);
    private static native void nativeSetPowerExtBoost(String boost, int fallback, int durationMs);
    private static native void nativeSetPowerBoost(int boost, int durationMs);
    private static native boolean nativeSetPowerMode(int mode, boolean enabled);
    private static native boolean nativeForceSuspend();
@@ -4488,6 +4501,16 @@ public final class PowerManagerService extends SystemService
        mIsVrModeEnabled = enabled;
    }

    private void setPowerExtModeInternal(String mode, int fallback, boolean enabled) {
        // Maybe filter the event.
        mNativeWrapper.nativeSetPowerExtMode(mode, fallback, enabled);
    }

    private void setPowerExtBoostInternal(String boost, int fallback, int durationMs) {
        // Maybe filter the event.
        mNativeWrapper.nativeSetPowerExtBoost(boost, fallback, durationMs);
    }

    private void setPowerBoostInternal(int boost, int durationMs) {
        // Maybe filter the event.
        mNativeWrapper.nativeSetPowerBoost(boost, durationMs);
@@ -5739,6 +5762,28 @@ public final class PowerManagerService extends SystemService
                    displayId, callback);
        }

        @Override // Binder call
        public void setPowerExtMode(String mode_name, boolean enabled) {
            if (!mSystemReady) {
                // Service not ready yet, so who the heck cares about power hints, bah.
                return;
            }
            mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
            PowerExtBoosts mode = PowerExtBoosts.valueOf(mode_name);
            setPowerExtModeInternal(mode.name(), mode.getFallback(), enabled);
        }

        @Override // Binder call
        public void setPowerExtBoost(String boost_name, int durationMs) {
            if (!mSystemReady) {
                // Service not ready yet, so who the heck cares about power hints, bah.
                return;
            }
            mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
            PowerExtBoosts boost = PowerExtBoosts.valueOf(boost_name);
            setPowerExtBoostInternal(boost.name(), boost.getFallback(), durationMs);
        }

        @Override // Binder call
        public void setPowerBoost(int boost, int durationMs) {
            if (!mSystemReady) {
@@ -6997,6 +7042,18 @@ public final class PowerManagerService extends SystemService
            uidIdleInternal(uid);
        }

        @Override
        public void setPowerExtMode(String mode_name, boolean enabled) {
            PowerExtBoosts mode = PowerExtBoosts.valueOf(mode_name);
            setPowerExtModeInternal(mode.name(), mode.getFallback(), enabled);
        }

        @Override
        public void setPowerExtBoost(String boost_name, int durationMs) {
            PowerExtBoosts boost = PowerExtBoosts.valueOf(boost_name);
            setPowerExtBoostInternal(boost.name(), boost.getFallback(), durationMs);
        }

        @Override
        public void setPowerBoost(int boost, int durationMs) {
            setPowerBoostInternal(boost, durationMs);
+1 −0
Original line number Diff line number Diff line
@@ -190,6 +190,7 @@ cc_defaults {

    static_libs: [
        "android.hardware.broadcastradio@common-utils-1x-lib",
        "//hardware/google/interfaces:pixel-power-ext-V1-cpp",
    ],

    product_variables: {
+84 −0
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@
#include <hardware/power.h>
#include <hardware_legacy/power.h>
#include <hidl/ServiceManagement.h>
#include <unordered_map>
#include <utils/Log.h>
#include <utils/String8.h>
#include <utils/Timers.h>
@@ -79,6 +80,9 @@ static nsecs_t gLastEventTime[USER_ACTIVITY_EVENT_LAST + 1];
// Throttling interval for user activity calls.
static const nsecs_t MIN_TIME_BETWEEN_USERACTIVITIES = 100 * 1000000L; // 100ms

// Cache for both Mode and Boost ext boosts support
static std::unordered_map<std::string, bool> boostCache;

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

static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
@@ -91,6 +95,34 @@ static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodNa
    return false;
}

static bool isPowerExtAvailable() {
    return gPowerHalController.isPowerExtAvailable().isOk();
}

static bool isPowerExtModeSupported(const ::std::string& mode) {
    auto result = gPowerHalController.isExtModeSupported(mode);
    if (result.isOk()) {
        return result.value();
    }
    return false;
}

static bool isPowerExtBoostSupported(const ::std::string& boost) {
    auto result = gPowerHalController.isExtBoostSupported(boost);
    if (result.isOk()) {
        return result.value();
    }
    return false;
}

static void setPowerExtMode(const ::std::string& mode, bool enabled) {
    gPowerHalController.setExtMode(mode, enabled);
}

static void setPowerExtBoost(const ::std::string& boost, int32_t durationMs) {
    gPowerHalController.setExtBoost(boost, durationMs);
}

static void setPowerBoost(Boost boost, int32_t durationMs) {
    gPowerHalController.setBoost(boost, durationMs);
    SurfaceComposerClient::notifyPowerBoost(static_cast<int32_t>(boost));
@@ -242,6 +274,56 @@ static void nativeSetAutoSuspend(JNIEnv* /* env */, jclass /* clazz */, jboolean
    }
}

static void nativeSetPowerExtMode(JNIEnv* env, jclass /* clazz */, jstring mode,
                                   jint fallback, jboolean enabled) {
    bool isSupported = isPowerExtAvailable();
    const char* modeStr = nullptr;
    if (isSupported) {
        if ((modeStr = env->GetStringUTFChars(mode, nullptr))) {
            if (boostCache.find(modeStr) != boostCache.end()) {
                isSupported = boostCache[modeStr];
            } else {
                isSupported = boostCache[modeStr] = isPowerExtModeSupported(modeStr);
            }
        } else {
            isSupported = false;
        }
    }
    if (isSupported) {
        setPowerExtMode(modeStr, enabled);
    } else {
        setPowerMode(static_cast<Mode>(fallback), enabled);
    }
    if (modeStr) {
        env->ReleaseStringUTFChars(mode, modeStr);
    }
}

static void nativeSetPowerExtBoost(JNIEnv* env, jclass /* clazz */, jstring boost,
                                   jint fallback, jint durationMs) {
    bool isSupported = isPowerExtAvailable();
    const char* boostStr = nullptr;
    if (isSupported) {
        if ((boostStr = env->GetStringUTFChars(boost, nullptr))) {
            if (boostCache.find(boostStr) != boostCache.end()) {
                isSupported = boostCache[boostStr];
            } else {
                isSupported = boostCache[boostStr] = isPowerExtBoostSupported(boostStr);
            }
        } else {
            isSupported = false;
        }
    }
    if (isSupported) {
        setPowerExtBoost(boostStr, durationMs);
    } else {
        setPowerBoost(static_cast<Boost>(fallback), durationMs);
    }
    if (boostStr) {
        env->ReleaseStringUTFChars(boost, boostStr);
    }
}

static void nativeSetPowerBoost(JNIEnv* /* env */, jclass /* clazz */, jint boost,
                                jint durationMs) {
    setPowerBoost(static_cast<Boost>(boost), durationMs);
@@ -269,6 +351,8 @@ static const JNINativeMethod gPowerManagerServiceMethods[] = {
        {"nativeReleaseSuspendBlocker", "(Ljava/lang/String;)V",
         (void*)nativeReleaseSuspendBlocker},
        {"nativeSetAutoSuspend", "(Z)V", (void*)nativeSetAutoSuspend},
        {"nativeSetPowerExtMode", "(Ljava/lang/String;IZ)V", (void*)nativeSetPowerExtMode},
        {"nativeSetPowerExtBoost", "(Ljava/lang/String;II)V", (void*)nativeSetPowerExtBoost},
        {"nativeSetPowerBoost", "(II)V", (void*)nativeSetPowerBoost},
        {"nativeSetPowerMode", "(IZ)Z", (void*)nativeSetPowerMode},
};