Skip to content

Commit 60fa461

Browse files
Jeff BrownAndroid (Google) Code Review
authored andcommitted
Merge changes I50eb7dcf,I7ae92ce1,Icb22db1c into jb-mr1-dev
* changes: Don't enable input dispatch until the screen is visible. Dejank electron beam. Don't process UEvents in Dalvik unless they match a pattern.
2 parents 3c0081b + 4fc4527 commit 60fa461

File tree

4 files changed

+135
-35
lines changed

4 files changed

+135
-35
lines changed

core/java/android/os/UEventObserver.java

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package android.os;
1818

19+
import android.util.Log;
20+
1921
import java.util.ArrayList;
2022
import java.util.HashMap;
2123

@@ -37,14 +39,20 @@
3739
* @hide
3840
*/
3941
public abstract class UEventObserver {
42+
private static final String TAG = "UEventObserver";
43+
private static final boolean DEBUG = false;
44+
4045
private static UEventThread sThread;
4146

42-
private static native void native_setup();
43-
private static native int next_event(byte[] buffer);
47+
private static native void nativeSetup();
48+
private static native String nativeWaitForNextEvent();
49+
private static native void nativeAddMatch(String match);
50+
private static native void nativeRemoveMatch(String match);
4451

4552
public UEventObserver() {
4653
}
4754

55+
@Override
4856
protected void finalize() throws Throwable {
4957
try {
5058
stopObserving();
@@ -78,10 +86,18 @@ private static UEventThread peekThread() {
7886
* This method can be called multiple times to register multiple matches.
7987
* Only one call to stopObserving is required even with multiple registered
8088
* matches.
81-
* @param match A substring of the UEvent to match. Use "" to match all
82-
* UEvent's
89+
*
90+
* @param match A substring of the UEvent to match. Try to be as specific
91+
* as possible to avoid incurring unintended additional cost from processing
92+
* irrelevant messages. Netlink messages can be moderately high bandwidth and
93+
* are expensive to parse. For example, some devices may send one netlink message
94+
* for each vsync period.
8395
*/
8496
public final void startObserving(String match) {
97+
if (match == null || match.isEmpty()) {
98+
throw new IllegalArgumentException("match substring must be non-empty");
99+
}
100+
85101
final UEventThread t = getThread();
86102
t.addObserver(match, this);
87103
}
@@ -117,7 +133,7 @@ public UEvent(String message) {
117133

118134
while (offset < length) {
119135
int equals = message.indexOf('=', offset);
120-
int at = message.indexOf(0, offset);
136+
int at = message.indexOf('\0', offset);
121137
if (at < 0) break;
122138

123139
if (equals > offset && equals < at) {
@@ -158,15 +174,17 @@ public UEventThread() {
158174
super("UEventObserver");
159175
}
160176

177+
@Override
161178
public void run() {
162-
native_setup();
179+
nativeSetup();
163180

164-
byte[] buffer = new byte[1024];
165-
int len;
166181
while (true) {
167-
len = next_event(buffer);
168-
if (len > 0) {
169-
sendEvent(new String(buffer, 0, len));
182+
String message = nativeWaitForNextEvent();
183+
if (message != null) {
184+
if (DEBUG) {
185+
Log.d(TAG, message);
186+
}
187+
sendEvent(message);
170188
}
171189
}
172190
}
@@ -176,7 +194,7 @@ private void sendEvent(String message) {
176194
final int N = mKeysAndObservers.size();
177195
for (int i = 0; i < N; i += 2) {
178196
final String key = (String)mKeysAndObservers.get(i);
179-
if (message.indexOf(key) != -1) {
197+
if (message.contains(key)) {
180198
final UEventObserver observer =
181199
(UEventObserver)mKeysAndObservers.get(i + 1);
182200
mTempObserversToSignal.add(observer);
@@ -199,6 +217,7 @@ public void addObserver(String match, UEventObserver observer) {
199217
synchronized (mKeysAndObservers) {
200218
mKeysAndObservers.add(match);
201219
mKeysAndObservers.add(observer);
220+
nativeAddMatch(match);
202221
}
203222
}
204223

@@ -208,7 +227,8 @@ public void removeObserver(UEventObserver observer) {
208227
for (int i = 0; i < mKeysAndObservers.size(); ) {
209228
if (mKeysAndObservers.get(i + 1) == observer) {
210229
mKeysAndObservers.remove(i + 1);
211-
mKeysAndObservers.remove(i);
230+
final String match = (String)mKeysAndObservers.remove(i);
231+
nativeRemoveMatch(match);
212232
} else {
213233
i += 2;
214234
}

core/jni/android_os_UEventObserver.cpp

Lines changed: 79 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,103 @@
1515
*/
1616

1717
#define LOG_TAG "UEventObserver"
18+
//#define LOG_NDEBUG 0
19+
1820
#include "utils/Log.h"
1921

2022
#include "hardware_legacy/uevent.h"
2123
#include "jni.h"
2224
#include "JNIHelp.h"
2325
#include "android_runtime/AndroidRuntime.h"
2426

25-
namespace android
26-
{
27+
#include <utils/Mutex.h>
28+
#include <utils/Vector.h>
29+
#include <utils/String8.h>
30+
#include <ScopedUtfChars.h>
2731

28-
static void
29-
android_os_UEventObserver_native_setup(JNIEnv *env, jclass clazz)
30-
{
32+
namespace android {
33+
34+
static Mutex gMatchesMutex;
35+
static Vector<String8> gMatches;
36+
37+
static void nativeSetup(JNIEnv *env, jclass clazz) {
3138
if (!uevent_init()) {
3239
jniThrowException(env, "java/lang/RuntimeException",
33-
"Unable to open socket for UEventObserver");
40+
"Unable to open socket for UEventObserver");
3441
}
3542
}
3643

37-
static int
38-
android_os_UEventObserver_next_event(JNIEnv *env, jclass clazz, jbyteArray jbuffer)
39-
{
40-
int buf_sz = env->GetArrayLength(jbuffer);
41-
char *buffer = (char*)env->GetByteArrayElements(jbuffer, NULL);
44+
static bool isMatch(const char* buffer, size_t length) {
45+
AutoMutex _l(gMatchesMutex);
46+
47+
for (size_t i = 0; i < gMatches.size(); i++) {
48+
const String8& match = gMatches.itemAt(i);
49+
50+
// Consider all zero-delimited fields of the buffer.
51+
const char* field = buffer;
52+
const char* end = buffer + length;
53+
do {
54+
if (strstr(field, match.string())) {
55+
ALOGV("Matched uevent message with pattern: %s", match.string());
56+
return true;
57+
}
58+
field += strlen(field) + 1;
59+
} while (field != end);
60+
}
61+
return false;
62+
}
4263

43-
int length = uevent_next_event(buffer, buf_sz - 1);
64+
static jstring nativeWaitForNextEvent(JNIEnv *env, jclass clazz) {
65+
char buffer[1024];
4466

45-
env->ReleaseByteArrayElements(jbuffer, (jbyte*)buffer, 0);
67+
for (;;) {
68+
int length = uevent_next_event(buffer, sizeof(buffer) - 1);
69+
if (length <= 0) {
70+
return NULL;
71+
}
72+
buffer[length] = '\0';
4673

47-
return length;
74+
ALOGV("Received uevent message: %s", buffer);
75+
76+
if (isMatch(buffer, length)) {
77+
// Assume the message is ASCII.
78+
jchar message[length];
79+
for (int i = 0; i < length; i++) {
80+
message[i] = buffer[i];
81+
}
82+
return env->NewString(message, length);
83+
}
84+
}
85+
}
86+
87+
static void nativeAddMatch(JNIEnv* env, jclass clazz, jstring matchStr) {
88+
ScopedUtfChars match(env, matchStr);
89+
90+
AutoMutex _l(gMatchesMutex);
91+
gMatches.add(String8(match.c_str()));
92+
}
93+
94+
static void nativeRemoveMatch(JNIEnv* env, jclass clazz, jstring matchStr) {
95+
ScopedUtfChars match(env, matchStr);
96+
97+
AutoMutex _l(gMatchesMutex);
98+
for (size_t i = 0; i < gMatches.size(); i++) {
99+
if (gMatches.itemAt(i) == match.c_str()) {
100+
gMatches.removeAt(i);
101+
break; // only remove first occurrence
102+
}
103+
}
48104
}
49105

50106
static JNINativeMethod gMethods[] = {
51-
{"native_setup", "()V", (void *)android_os_UEventObserver_native_setup},
52-
{"next_event", "([B)I", (void *)android_os_UEventObserver_next_event},
107+
{ "nativeSetup", "()V",
108+
(void *)nativeSetup },
109+
{ "nativeWaitForNextEvent", "()Ljava/lang/String;",
110+
(void *)nativeWaitForNextEvent },
111+
{ "nativeAddMatch", "(Ljava/lang/String;)V",
112+
(void *)nativeAddMatch },
113+
{ "nativeRemoveMatch", "(Ljava/lang/String;)V",
114+
(void *)nativeRemoveMatch },
53115
};
54116

55117

@@ -64,7 +126,7 @@ int register_android_os_UEventObserver(JNIEnv *env)
64126
}
65127

66128
return AndroidRuntime::registerNativeMethods(env,
67-
"android/os/UEventObserver", gMethods, NELEM(gMethods));
129+
"android/os/UEventObserver", gMethods, NELEM(gMethods));
68130
}
69131

70132
} // namespace android

policy/src/com/android/internal/policy/impl/PhoneWindowManager.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3692,11 +3692,6 @@ public void screenTurningOn(final ScreenOnListener screenOnListener) {
36923692
updateLockScreenTimeout();
36933693
}
36943694

3695-
try {
3696-
mWindowManager.setEventDispatching(true);
3697-
} catch (RemoteException unhandled) {
3698-
}
3699-
37003695
waitForKeyguard(screenOnListener);
37013696
}
37023697

@@ -3747,6 +3742,11 @@ private void finishScreenTurningOn(ScreenOnListener screenOnListener) {
37473742
mScreenOnFully = true;
37483743
}
37493744

3745+
try {
3746+
mWindowManager.setEventDispatching(true);
3747+
} catch (RemoteException unhandled) {
3748+
}
3749+
37503750
if (screenOnListener != null) {
37513751
screenOnListener.onScreenOn();
37523752
}

services/java/com/android/server/power/ElectronBeam.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ final class ElectronBeam {
6363
private static final float HSTRETCH_DURATION = 0.5f;
6464
private static final float VSTRETCH_DURATION = 1.0f - HSTRETCH_DURATION;
6565

66+
// The number of frames to draw when preparing the animation so that it will
67+
// be ready to run smoothly. We use 3 frames because we are triple-buffered.
68+
// See code for details.
69+
private static final int DEJANK_FRAMES = 3;
70+
6671
// Set to true when the animation context has been fully prepared.
6772
private boolean mPrepared;
6873
private int mMode;
@@ -145,6 +150,19 @@ public boolean prepare(int mode) {
145150

146151
// Done.
147152
mPrepared = true;
153+
154+
// Dejanking optimization.
155+
// Some GL drivers can introduce a lot of lag in the first few frames as they
156+
// initialize their state and allocate graphics buffers for rendering.
157+
// Work around this problem by rendering the first frame of the animation a few
158+
// times. The rest of the animation should run smoothly thereafter.
159+
// The frames we draw here aren't visible because we are essentially just
160+
// painting the screenshot as-is.
161+
if (mode == MODE_COOL_DOWN) {
162+
for (int i = 0; i < DEJANK_FRAMES; i++) {
163+
draw(1.0f);
164+
}
165+
}
148166
return true;
149167
}
150168

0 commit comments

Comments
 (0)