The major goal of this rewrite is to make it easier to implement power management policies correctly. According, the new implementation primarily uses state-based rather than event-based triggers for applying changes to the current power state. For example, when an application requests that the proximity sensor be used to manage the screen state (by way of a wake lock), the power manager makes note of the fact that the set of wake locks changed. Then it executes a common update function that recalculates the entire state, first looking at wake locks, then considering user activity, and eventually determining whether the screen should be turned on or off. At this point it may make a request to a component called the DisplayPowerController to asynchronously update the display's powe state. Likewise, DisplayPowerController makes note of the updated power request and schedules its own update function to figure out what needs to be changed. The big benefit of this approach is that it's easy to mutate multiple properties of the power state simultaneously then apply their joint effects together all at once. Transitions between states are detected and resolved by the update in a consistent manner. The new power manager service has is implemented as a set of loosely coupled components. For the most part, information only flows one way through these components (by issuing a request to that component) although some components support sending a message back to indicate when the work has been completed. For example, the DisplayPowerController posts a callback runnable asynchronously to tell the PowerManagerService when the display is ready. An important feature of this approach is that each component neatly encapsulates its state and maintains its own invariants. Moreover, we do not need to worry about deadlocks or awkward mutual exclusion semantics because most of the requests are asynchronous. The benefits of this design are especially apparent in the implementation of the screen on / off and brightness control animations which are able to take advantage of framework features like properties, ObjectAnimator and Choreographer. The screen on / off animation is now the responsibility of the power manager (instead of surface flinger). This change makes it much easier to ensure that the animation is properly coordinated with other power state changes and eliminates the cause of race conditions in the older implementation. The because of the userActivity() function has been changed so that it never wakes the device from sleep. This change removes ambiguity around forcing or disabling user activity for various purposes. To wake the device, use wakeUp(). To put it to sleep, use goToSleep(). Simple. The power manager service interface and API has been significantly simplified and consolidated. Also fixed some inconsistencies related to how the minimum and maximum screen brightness setting was presented in brightness control widgets and enforced behind the scenes. At present the following features are implemented: - Wake locks. - User activity. - Wake up / go to sleep. - Power state broadcasts. - Battery stats and event log notifications. - Dreams. - Proximity screen off. - Animated screen on / off transitions. - Auto-dimming. - Auto-brightness control for the screen backlight with different timeouts for ramping up versus ramping down. - Auto-on when plugged or unplugged. - Stay on when plugged. - Device administration maximum user activity timeout. - Application controlled brightness via window manager. The following features are not yet implemented: - Reduced user activity timeout for the key guard. - Reduced user activity timeout for the phone application. - Coordinating screen on barriers with the window manager. - Preventing auto-rotation during power state changes. - Auto-brightness adjustment setting (feature was disabled in previous version of the power manager service pending an improved UI design so leaving it out for now). - Interpolated brightness control (a proposed new scheme for more compactly specifying auto-brightness levels in config.xml). - Button / keyboard backlight control. - Change window manager to associated WorkSource with KEEP_SCREEN_ON_FLAG wake lock instead of talking directly to the battery stats service. - Optionally support animating screen brightness when turning on/off instead of playing electron beam animation (config_animateScreenLights). Change-Id: I1d7a52e98f0449f76d70bf421f6a7f245957d1d7
287 lines
9.4 KiB
C++
287 lines
9.4 KiB
C++
/*
|
|
* Copyright (C) 2010 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 "PowerManagerService-JNI"
|
|
|
|
//#define LOG_NDEBUG 0
|
|
|
|
#include "JNIHelp.h"
|
|
#include "jni.h"
|
|
|
|
#include <ScopedUtfChars.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <android_runtime/AndroidRuntime.h>
|
|
#include <gui/ISurfaceComposer.h>
|
|
#include <utils/Timers.h>
|
|
#include <utils/misc.h>
|
|
#include <utils/String8.h>
|
|
#include <utils/Log.h>
|
|
#include <hardware/power.h>
|
|
#include <hardware_legacy/power.h>
|
|
#include <cutils/android_reboot.h>
|
|
#include <suspend/autosuspend.h>
|
|
|
|
#include <private/gui/ComposerService.h>
|
|
|
|
#include "com_android_server_power_PowerManagerService.h"
|
|
|
|
namespace android {
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
static struct {
|
|
jmethodID wakeUpFromNative;
|
|
jmethodID goToSleepFromNative;
|
|
jmethodID userActivityFromNative;
|
|
} gPowerManagerServiceClassInfo;
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
static jobject gPowerManagerServiceObj;
|
|
static struct power_module* gPowerModule;
|
|
|
|
static Mutex gPowerManagerLock;
|
|
static bool gScreenOn;
|
|
static bool gScreenBright;
|
|
|
|
static nsecs_t gLastEventTime[USER_ACTIVITY_EVENT_LAST + 1];
|
|
|
|
// Throttling interval for user activity calls.
|
|
static const nsecs_t MIN_TIME_BETWEEN_USERACTIVITIES = 500 * 1000000L; // 500ms
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
|
|
if (env->ExceptionCheck()) {
|
|
ALOGE("An exception was thrown by callback '%s'.", methodName);
|
|
LOGE_EX(env);
|
|
env->ExceptionClear();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool android_server_PowerManagerService_isScreenOn() {
|
|
AutoMutex _l(gPowerManagerLock);
|
|
return gScreenOn;
|
|
}
|
|
|
|
bool android_server_PowerManagerService_isScreenBright() {
|
|
AutoMutex _l(gPowerManagerLock);
|
|
return gScreenBright;
|
|
}
|
|
|
|
void android_server_PowerManagerService_userActivity(nsecs_t eventTime, int32_t eventType) {
|
|
// Tell the power HAL when user activity occurs.
|
|
if (gPowerModule && gPowerModule->powerHint) {
|
|
gPowerModule->powerHint(gPowerModule, POWER_HINT_INTERACTION, NULL);
|
|
}
|
|
|
|
if (gPowerManagerServiceObj) {
|
|
// Throttle calls into user activity by event type.
|
|
// We're a little conservative about argument checking here in case the caller
|
|
// passes in bad data which could corrupt system state.
|
|
if (eventType >= 0 && eventType <= USER_ACTIVITY_EVENT_LAST) {
|
|
nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
|
|
if (eventTime > now) {
|
|
eventTime = now;
|
|
}
|
|
|
|
if (gLastEventTime[eventType] + MIN_TIME_BETWEEN_USERACTIVITIES > eventTime) {
|
|
return;
|
|
}
|
|
gLastEventTime[eventType] = eventTime;
|
|
}
|
|
|
|
JNIEnv* env = AndroidRuntime::getJNIEnv();
|
|
|
|
env->CallVoidMethod(gPowerManagerServiceObj,
|
|
gPowerManagerServiceClassInfo.userActivityFromNative,
|
|
nanoseconds_to_milliseconds(eventTime), eventType, 0);
|
|
checkAndClearExceptionFromCallback(env, "userActivityFromNative");
|
|
}
|
|
}
|
|
|
|
void android_server_PowerManagerService_wakeUp(nsecs_t eventTime) {
|
|
if (gPowerManagerServiceObj) {
|
|
JNIEnv* env = AndroidRuntime::getJNIEnv();
|
|
|
|
env->CallVoidMethod(gPowerManagerServiceObj,
|
|
gPowerManagerServiceClassInfo.wakeUpFromNative,
|
|
nanoseconds_to_milliseconds(eventTime));
|
|
checkAndClearExceptionFromCallback(env, "wakeUpFromNative");
|
|
}
|
|
}
|
|
|
|
void android_server_PowerManagerService_goToSleep(nsecs_t eventTime) {
|
|
if (gPowerManagerServiceObj) {
|
|
JNIEnv* env = AndroidRuntime::getJNIEnv();
|
|
|
|
env->CallVoidMethod(gPowerManagerServiceObj,
|
|
gPowerManagerServiceClassInfo.goToSleepFromNative,
|
|
nanoseconds_to_milliseconds(eventTime), 0);
|
|
checkAndClearExceptionFromCallback(env, "goToSleepFromNative");
|
|
}
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
static void nativeInit(JNIEnv* env, jobject obj) {
|
|
gPowerManagerServiceObj = env->NewGlobalRef(obj);
|
|
|
|
status_t err = hw_get_module(POWER_HARDWARE_MODULE_ID,
|
|
(hw_module_t const**)&gPowerModule);
|
|
if (!err) {
|
|
gPowerModule->init(gPowerModule);
|
|
} else {
|
|
ALOGE("Couldn't load %s module (%s)", POWER_HARDWARE_MODULE_ID, strerror(-err));
|
|
}
|
|
}
|
|
|
|
static void nativeSetPowerState(JNIEnv* env,
|
|
jclass clazz, jboolean screenOn, jboolean screenBright) {
|
|
AutoMutex _l(gPowerManagerLock);
|
|
gScreenOn = screenOn;
|
|
gScreenBright = screenBright;
|
|
}
|
|
|
|
static void nativeAcquireSuspendBlocker(JNIEnv *env, jclass clazz, jstring nameStr) {
|
|
ScopedUtfChars name(env, nameStr);
|
|
acquire_wake_lock(PARTIAL_WAKE_LOCK, name.c_str());
|
|
}
|
|
|
|
static void nativeReleaseSuspendBlocker(JNIEnv *env, jclass clazz, jstring nameStr) {
|
|
ScopedUtfChars name(env, nameStr);
|
|
release_wake_lock(name.c_str());
|
|
}
|
|
|
|
static void nativeSetScreenState(JNIEnv *env, jclass clazz, jboolean on) {
|
|
sp<ISurfaceComposer> s(ComposerService::getComposerService());
|
|
if (on) {
|
|
{
|
|
ALOGD_IF_SLOW(50, "Excessive delay in autosuspend_disable() while turning screen on");
|
|
autosuspend_disable();
|
|
}
|
|
|
|
if (gPowerModule) {
|
|
ALOGD_IF_SLOW(10, "Excessive delay in setInteractive(true) while turning screen on");
|
|
gPowerModule->setInteractive(gPowerModule, true);
|
|
}
|
|
|
|
{
|
|
ALOGD_IF_SLOW(20, "Excessive delay in unblank() while turning screen on");
|
|
s->unblank();
|
|
}
|
|
} else {
|
|
{
|
|
ALOGD_IF_SLOW(20, "Excessive delay in blank() while turning screen off");
|
|
s->blank();
|
|
}
|
|
|
|
if (gPowerModule) {
|
|
ALOGD_IF_SLOW(10, "Excessive delay in setInteractive(false) while turning screen off");
|
|
gPowerModule->setInteractive(gPowerModule, false);
|
|
}
|
|
|
|
{
|
|
ALOGD_IF_SLOW(50, "Excessive delay in autosuspend_enable() while turning screen off");
|
|
autosuspend_enable();
|
|
}
|
|
}
|
|
}
|
|
|
|
static void nativeShutdown(JNIEnv *env, jclass clazz) {
|
|
android_reboot(ANDROID_RB_POWEROFF, 0, 0);
|
|
}
|
|
|
|
static void nativeReboot(JNIEnv *env, jclass clazz, jstring reason) {
|
|
if (reason == NULL) {
|
|
android_reboot(ANDROID_RB_RESTART, 0, 0);
|
|
} else {
|
|
const char *chars = env->GetStringUTFChars(reason, NULL);
|
|
android_reboot(ANDROID_RB_RESTART2, 0, (char *) chars);
|
|
env->ReleaseStringUTFChars(reason, chars); // In case it fails.
|
|
}
|
|
jniThrowIOException(env, errno);
|
|
}
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
static JNINativeMethod gPowerManagerServiceMethods[] = {
|
|
/* name, signature, funcPtr */
|
|
{ "nativeInit", "()V",
|
|
(void*) nativeInit },
|
|
{ "nativeSetPowerState", "(ZZ)V",
|
|
(void*) nativeSetPowerState },
|
|
{ "nativeAcquireSuspendBlocker", "(Ljava/lang/String;)V",
|
|
(void*) nativeAcquireSuspendBlocker },
|
|
{ "nativeReleaseSuspendBlocker", "(Ljava/lang/String;)V",
|
|
(void*) nativeReleaseSuspendBlocker },
|
|
{ "nativeSetScreenState", "(Z)V",
|
|
(void*) nativeSetScreenState },
|
|
{ "nativeShutdown", "()V",
|
|
(void*) nativeShutdown },
|
|
{ "nativeReboot", "(Ljava/lang/String;)V",
|
|
(void*) nativeReboot },
|
|
};
|
|
|
|
#define FIND_CLASS(var, className) \
|
|
var = env->FindClass(className); \
|
|
LOG_FATAL_IF(! var, "Unable to find class " className);
|
|
|
|
#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
|
|
var = env->GetMethodID(clazz, methodName, methodDescriptor); \
|
|
LOG_FATAL_IF(! var, "Unable to find method " methodName);
|
|
|
|
#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
|
|
var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
|
|
LOG_FATAL_IF(! var, "Unable to find field " fieldName);
|
|
|
|
int register_android_server_PowerManagerService(JNIEnv* env) {
|
|
int res = jniRegisterNativeMethods(env, "com/android/server/power/PowerManagerService",
|
|
gPowerManagerServiceMethods, NELEM(gPowerManagerServiceMethods));
|
|
LOG_FATAL_IF(res < 0, "Unable to register native methods.");
|
|
|
|
// Callbacks
|
|
|
|
jclass clazz;
|
|
FIND_CLASS(clazz, "com/android/server/power/PowerManagerService");
|
|
|
|
GET_METHOD_ID(gPowerManagerServiceClassInfo.wakeUpFromNative, clazz,
|
|
"wakeUpFromNative", "(J)V");
|
|
|
|
GET_METHOD_ID(gPowerManagerServiceClassInfo.goToSleepFromNative, clazz,
|
|
"goToSleepFromNative", "(JI)V");
|
|
|
|
GET_METHOD_ID(gPowerManagerServiceClassInfo.userActivityFromNative, clazz,
|
|
"userActivityFromNative", "(JII)V");
|
|
|
|
// Initialize
|
|
for (int i = 0; i <= USER_ACTIVITY_EVENT_LAST; i++) {
|
|
gLastEventTime[i] = LLONG_MIN;
|
|
}
|
|
gScreenOn = true;
|
|
gScreenBright = true;
|
|
gPowerManagerServiceObj = NULL;
|
|
gPowerModule = NULL;
|
|
return 0;
|
|
}
|
|
|
|
} /* namespace android */
|