AI 148368: Decrease CPU usage by throttling touch events
We are decreasing CPU usage at the cost of event latency. Events are queued up and released to the app at the specified rate. You can experiment with different values via: adb shell setprop windowsmgr.max_events_per_sec 35 The new value is picked up when you let go and retouch the screen. No reboot needed. Also the following changes were made after profiling: - In WindowManagerService, limit the call to userActivity() when we have a flood touch events. - In PowerManagerService, skip checking of permission if the caller is system user. - In PowerManagerService, integrated the functionality of gatherState() into reactivateWakeLocksLocked(). They loop through the same data structure and are called back to back. BUG=1692771 Automated import of CL 148368
This commit is contained in:
committed by
The Android Open Source Project
parent
dffbb4dbce
commit
e96440fa15
@@ -180,6 +180,25 @@ public class WindowManagerService extends IWindowManager.Stub implements Watchdo
|
||||
static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
|
||||
static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
|
||||
|
||||
/** The minimum time between dispatching touch events. */
|
||||
int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
|
||||
|
||||
// Last touch event time
|
||||
long mLastTouchEventTime = 0;
|
||||
|
||||
// Last touch event type
|
||||
int mLastTouchEventType = OTHER_EVENT;
|
||||
|
||||
// Time to wait before calling useractivity again. This saves CPU usage
|
||||
// when we get a flood of touch events.
|
||||
static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
|
||||
|
||||
// Last time we call user activity
|
||||
long mLastUserActivityCallTime = 0;
|
||||
|
||||
// Last time we updated battery stats
|
||||
long mLastBatteryStatsCallTime = 0;
|
||||
|
||||
private static final String SYSTEM_SECURE = "ro.secure";
|
||||
|
||||
/**
|
||||
@@ -3689,9 +3708,20 @@ public class WindowManagerService extends IWindowManager.Stub implements Watchdo
|
||||
// -------------------------------------------------------------
|
||||
|
||||
private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
|
||||
if (targetWin == null ||
|
||||
targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
|
||||
mPowerManager.userActivity(SystemClock.uptimeMillis(), false, eventType);
|
||||
long curTime = SystemClock.uptimeMillis();
|
||||
|
||||
if (eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
|
||||
if (mLastTouchEventType == eventType &&
|
||||
(curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
|
||||
return;
|
||||
}
|
||||
mLastUserActivityCallTime = curTime;
|
||||
mLastTouchEventType = eventType;
|
||||
}
|
||||
|
||||
if (targetWin == null
|
||||
|| targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
|
||||
mPowerManager.userActivity(curTime, false, eventType, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3759,7 +3789,7 @@ public class WindowManagerService extends IWindowManager.Stub implements Watchdo
|
||||
// events in such a way, since this means the user is moving the
|
||||
// pointer without actually pressing down. All other cases should
|
||||
// be atypical, so let's log them.
|
||||
if (ev.getAction() != MotionEvent.ACTION_MOVE) {
|
||||
if (action != MotionEvent.ACTION_MOVE) {
|
||||
Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
|
||||
}
|
||||
if (qev != null) {
|
||||
@@ -3846,7 +3876,39 @@ public class WindowManagerService extends IWindowManager.Stub implements Watchdo
|
||||
return false;
|
||||
}
|
||||
} //end if target
|
||||
|
||||
|
||||
// TODO remove once we settle on a value or make it app specific
|
||||
if (action == MotionEvent.ACTION_DOWN) {
|
||||
int max_events_per_sec = 35;
|
||||
try {
|
||||
max_events_per_sec = Integer.parseInt(SystemProperties
|
||||
.get("windowsmgr.max_events_per_sec"));
|
||||
if (max_events_per_sec < 1) {
|
||||
max_events_per_sec = 35;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
}
|
||||
mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
|
||||
}
|
||||
|
||||
/*
|
||||
* Throttle events to minimize CPU usage when there's a flood of events
|
||||
* e.g. constant contact with the screen
|
||||
*/
|
||||
if (action == MotionEvent.ACTION_MOVE) {
|
||||
long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
|
||||
long now = SystemClock.uptimeMillis();
|
||||
if (now < nextEventTime) {
|
||||
try {
|
||||
Thread.sleep(nextEventTime - now);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
mLastTouchEventTime = nextEventTime;
|
||||
} else {
|
||||
mLastTouchEventTime = now;
|
||||
}
|
||||
}
|
||||
|
||||
synchronized(mWindowMap) {
|
||||
if (qev != null && action == MotionEvent.ACTION_MOVE) {
|
||||
mKeyWaiter.bindTargetWindowLocked(target,
|
||||
@@ -4926,7 +4988,7 @@ public class WindowManagerService extends IWindowManager.Stub implements Watchdo
|
||||
}
|
||||
if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
|
||||
mPowerManager.userActivity(event.when, false,
|
||||
LocalPowerManager.BUTTON_EVENT);
|
||||
LocalPowerManager.BUTTON_EVENT, false);
|
||||
}
|
||||
|
||||
if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
|
||||
@@ -5086,11 +5148,17 @@ public class WindowManagerService extends IWindowManager.Stub implements Watchdo
|
||||
eventType = LocalPowerManager.OTHER_EVENT;
|
||||
}
|
||||
try {
|
||||
mBatteryStats.noteInputEvent();
|
||||
long now = SystemClock.uptimeMillis();
|
||||
|
||||
if ((now - mLastBatteryStatsCallTime)
|
||||
>= MIN_TIME_BETWEEN_USERACTIVITIES) {
|
||||
mLastBatteryStatsCallTime = now;
|
||||
mBatteryStats.noteInputEvent();
|
||||
}
|
||||
} catch (RemoteException e) {
|
||||
// Ignore
|
||||
}
|
||||
mPowerManager.userActivity(curTime, false, eventType);
|
||||
mPowerManager.userActivity(curTime, false, eventType, false);
|
||||
switch (ev.classType) {
|
||||
case RawInputEvent.CLASS_KEYBOARD:
|
||||
KeyEvent ke = (KeyEvent)ev.event;
|
||||
|
||||
Reference in New Issue
Block a user