Merge change 9674

* changes:
  Fix problem in AudioTrack with 8 bit PCM and direct output.
diff --git a/include/ui/EGLUtils.h b/include/ui/EGLUtils.h
new file mode 100644
index 0000000..48777b6
--- /dev/null
+++ b/include/ui/EGLUtils.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2009 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.
+ */
+
+
+#ifndef ANDROID_UI_EGLUTILS_H
+#define ANDROID_UI_EGLUTILS_H
+
+#include <utils/Errors.h>
+#include <ui/PixelFormat.h>
+#include <EGL/egl.h>
+
+
+// ----------------------------------------------------------------------------
+namespace android {
+// ----------------------------------------------------------------------------
+
+class EGLUtils
+{
+public:
+
+    static status_t selectConfigForPixelFormat(
+            EGLDisplay dpy,
+            EGLint const* attrs,
+            PixelFormat format,
+            EGLConfig* outConfig);
+
+    static status_t selectConfigForNativeWindow(
+            EGLDisplay dpy,
+            EGLint const* attrs,
+            EGLNativeWindowType window,
+            EGLConfig* outConfig);
+};
+
+// ----------------------------------------------------------------------------
+}; // namespace android
+// ----------------------------------------------------------------------------
+
+#endif /* ANDROID_UI_EGLUTILS_H */
diff --git a/include/ui/EventHub.h b/include/ui/EventHub.h
index d62fd7d..3b18c77 100644
--- a/include/ui/EventHub.h
+++ b/include/ui/EventHub.h
@@ -55,7 +55,9 @@
         CLASS_KEYBOARD      = 0x00000001,
         CLASS_ALPHAKEY      = 0x00000002,
         CLASS_TOUCHSCREEN   = 0x00000004,
-        CLASS_TRACKBALL     = 0x00000008
+        CLASS_TRACKBALL     = 0x00000008,
+        CLASS_TOUCHSCREEN_MT= 0x00000010,
+        CLASS_DPAD          = 0x00000020
     };
     uint32_t getDeviceClasses(int32_t deviceId) const;
     
@@ -117,11 +119,12 @@
         String8         keylayoutFilename;
         device_t*       next;
         
-        device_t(int32_t _id, const char* _path);
+        device_t(int32_t _id, const char* _path, const char* name);
         ~device_t();
     };
 
     device_t* getDevice(int32_t deviceId) const;
+    bool hasKeycode(device_t* device, int keycode) const;
     
     // Protect all internal state.
     mutable Mutex   mLock;
diff --git a/include/ui/egl/android_natives.h b/include/ui/egl/android_natives.h
index a3a1316..7da69b1 100644
--- a/include/ui/egl/android_natives.h
+++ b/include/ui/egl/android_natives.h
@@ -63,7 +63,8 @@
 /* attributes queriable with query() */
 enum {
     NATIVE_WINDOW_WIDTH     = 0,
-    NATIVE_WINDOW_HEIGHT    = 1
+    NATIVE_WINDOW_HEIGHT    = 1,
+    NATIVE_WINDOW_FORMAT    = 2,
 };
 
 struct android_native_window_t 
diff --git a/libs/audioflinger/A2dpAudioInterface.cpp b/libs/audioflinger/A2dpAudioInterface.cpp
index 6f9e934..20db8a5 100644
--- a/libs/audioflinger/A2dpAudioInterface.cpp
+++ b/libs/audioflinger/A2dpAudioInterface.cpp
@@ -89,7 +89,7 @@
 
 void A2dpAudioInterface::closeOutputStream(AudioStreamOut* out) {
     if (mOutput == 0 || mOutput != out) {
-        LOGW("Attempt to close invalid output stream");
+        mHardwareInterface->closeOutputStream(out);
     }
     else {
         delete mOutput;
diff --git a/libs/audioflinger/AudioFlinger.cpp b/libs/audioflinger/AudioFlinger.cpp
index 232ffb0..b34f214 100644
--- a/libs/audioflinger/AudioFlinger.cpp
+++ b/libs/audioflinger/AudioFlinger.cpp
@@ -115,7 +115,7 @@
 
 AudioFlinger::AudioFlinger()
     : BnAudioFlinger(),
-        mAudioHardware(0), mMasterVolume(1.0f), mMasterMute(false)
+        mAudioHardware(0), mMasterVolume(1.0f), mMasterMute(false), mNextThreadId(0)
 {
     mHardwareStatus = AUDIO_HW_IDLE;
 
@@ -231,12 +231,12 @@
 
         // dump playback threads
         for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
-            mPlaybackThreads[i]->dump(fd, args);
+            mPlaybackThreads.valueAt(i)->dump(fd, args);
         }
 
         // dump record threads
         for (size_t i = 0; i < mRecordThreads.size(); i++) {
-            mRecordThreads[i]->dump(fd, args);
+            mRecordThreads.valueAt(i)->dump(fd, args);
         }
 
         if (mAudioHardware) {
@@ -260,7 +260,7 @@
         int frameCount,
         uint32_t flags,
         const sp<IMemory>& sharedBuffer,
-        void *output,
+        int output,
         status_t *status)
 {
     sp<PlaybackThread::Track> track;
@@ -308,56 +308,56 @@
     return trackHandle;
 }
 
-uint32_t AudioFlinger::sampleRate(void *output) const
+uint32_t AudioFlinger::sampleRate(int output) const
 {
     Mutex::Autolock _l(mLock);
     PlaybackThread *thread = checkPlaybackThread_l(output);
     if (thread == NULL) {
-        LOGW("sampleRate() unknown thread %p", output);
+        LOGW("sampleRate() unknown thread %d", output);
         return 0;
     }
     return thread->sampleRate();
 }
 
-int AudioFlinger::channelCount(void *output) const
+int AudioFlinger::channelCount(int output) const
 {
     Mutex::Autolock _l(mLock);
     PlaybackThread *thread = checkPlaybackThread_l(output);
     if (thread == NULL) {
-        LOGW("channelCount() unknown thread %p", output);
+        LOGW("channelCount() unknown thread %d", output);
         return 0;
     }
     return thread->channelCount();
 }
 
-int AudioFlinger::format(void *output) const
+int AudioFlinger::format(int output) const
 {
     Mutex::Autolock _l(mLock);
     PlaybackThread *thread = checkPlaybackThread_l(output);
     if (thread == NULL) {
-        LOGW("format() unknown thread %p", output);
+        LOGW("format() unknown thread %d", output);
         return 0;
     }
     return thread->format();
 }
 
-size_t AudioFlinger::frameCount(void *output) const
+size_t AudioFlinger::frameCount(int output) const
 {
     Mutex::Autolock _l(mLock);
     PlaybackThread *thread = checkPlaybackThread_l(output);
     if (thread == NULL) {
-        LOGW("frameCount() unknown thread %p", output);
+        LOGW("frameCount() unknown thread %d", output);
         return 0;
     }
     return thread->frameCount();
 }
 
-uint32_t AudioFlinger::latency(void *output) const
+uint32_t AudioFlinger::latency(int output) const
 {
     Mutex::Autolock _l(mLock);
     PlaybackThread *thread = checkPlaybackThread_l(output);
     if (thread == NULL) {
-        LOGW("latency() unknown thread %p", output);
+        LOGW("latency() unknown thread %d", output);
         return 0;
     }
     return thread->latency();
@@ -380,7 +380,7 @@
 
     mMasterVolume = value;
     for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
-       mPlaybackThreads[i]->setMasterVolume(value);
+       mPlaybackThreads.valueAt(i)->setMasterVolume(value);
 
     return NO_ERROR;
 }
@@ -435,7 +435,7 @@
 
     mMasterMute = muted;
     for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
-       mPlaybackThreads[i]->setMasterMute(muted);
+       mPlaybackThreads.valueAt(i)->setMasterMute(muted);
 
     return NO_ERROR;
 }
@@ -450,7 +450,7 @@
     return mMasterMute;
 }
 
-status_t AudioFlinger::setStreamVolume(int stream, float value, void *output)
+status_t AudioFlinger::setStreamVolume(int stream, float value, int output)
 {
     // check calling permissions
     if (!settingsAllowed()) {
@@ -495,7 +495,7 @@
 
     if (thread == NULL) {
         for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
-           mPlaybackThreads[i]->setStreamVolume(stream, value);
+           mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
 
     } else {
         thread->setStreamVolume(stream, value);
@@ -518,12 +518,12 @@
 
     mStreamTypes[stream].mute = muted;
     for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
-       mPlaybackThreads[i]->setStreamMute(stream, muted);
+       mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
 
     return NO_ERROR;
 }
 
-float AudioFlinger::streamVolume(int stream, void *output) const
+float AudioFlinger::streamVolume(int stream, int output) const
 {
     if (stream < 0 || uint32_t(stream) >= AudioSystem::NUM_STREAM_TYPES) {
         return 0.0f;
@@ -562,18 +562,18 @@
 {
     Mutex::Autolock _l(mLock);
     for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) {
-        if (mPlaybackThreads[i]->isMusicActive()) {
+        if (mPlaybackThreads.valueAt(i)->isMusicActive()) {
             return true;
         }
     }
     return false;
 }
 
-status_t AudioFlinger::setParameters(void *ioHandle, const String8& keyValuePairs)
+status_t AudioFlinger::setParameters(int ioHandle, const String8& keyValuePairs)
 {
     status_t result;
 
-    LOGV("setParameters(): io %p, keyvalue %s, tid %d, calling tid %d",
+    LOGV("setParameters(): io %d, keyvalue %s, tid %d, calling tid %d",
             ioHandle, keyValuePairs.string(), gettid(), IPCThreadState::self()->getCallingPid());
     // check calling permissions
     if (!settingsAllowed()) {
@@ -604,9 +604,9 @@
     return BAD_VALUE;
 }
 
-String8 AudioFlinger::getParameters(void *ioHandle, const String8& keys)
+String8 AudioFlinger::getParameters(int ioHandle, const String8& keys)
 {
-//    LOGV("getParameters() io %p, keys %s, tid %d, calling tid %d",
+//    LOGV("getParameters() io %d, keys %s, tid %d, calling tid %d",
 //            ioHandle, keys.string(), gettid(), IPCThreadState::self()->getCallingPid());
 
     if (ioHandle == 0) {
@@ -644,11 +644,11 @@
     // the config change is always sent from playback or record threads to avoid deadlock
     // with AudioSystem::gLock
     for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
-        mPlaybackThreads[i]->sendConfigEvent(AudioSystem::OUTPUT_OPENED);
+        mPlaybackThreads.valueAt(i)->sendConfigEvent(AudioSystem::OUTPUT_OPENED);
     }
 
     for (size_t i = 0; i < mRecordThreads.size(); i++) {
-        mRecordThreads[i]->sendConfigEvent(AudioSystem::INPUT_OPENED);
+        mRecordThreads.valueAt(i)->sendConfigEvent(AudioSystem::INPUT_OPENED);
     }
 }
 
@@ -668,14 +668,33 @@
     }
 }
 
-void AudioFlinger::audioConfigChanged(int event, void *param1, void *param2) {
+void AudioFlinger::audioConfigChanged(int event, const sp<ThreadBase>& thread, void *param2) {
     Mutex::Autolock _l(mLock);
-    size_t size = mNotificationClients.size();
-    for (size_t i = 0; i < size; i++) {
-        sp<IBinder> binder = mNotificationClients.itemAt(i);
-        LOGV("audioConfigChanged() Notifying change to client %p", binder.get());
-        sp<IAudioFlingerClient> client = interface_cast<IAudioFlingerClient> (binder);
-        client->ioConfigChanged(event, param1, param2);
+    int ioHandle = 0;
+
+    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
+        if (mPlaybackThreads.valueAt(i) == thread) {
+            ioHandle = mPlaybackThreads.keyAt(i);
+            break;
+        }
+    }
+    if (ioHandle == 0) {
+        for (size_t i = 0; i < mRecordThreads.size(); i++) {
+            if (mRecordThreads.valueAt(i) == thread) {
+                ioHandle = mRecordThreads.keyAt(i);
+                break;
+            }
+        }
+    }
+
+    if (ioHandle != 0) {
+        size_t size = mNotificationClients.size();
+        for (size_t i = 0; i < size; i++) {
+            sp<IBinder> binder = mNotificationClients.itemAt(i);
+            LOGV("audioConfigChanged() Notifying change to client %p", binder.get());
+            sp<IAudioFlingerClient> client = interface_cast<IAudioFlingerClient> (binder);
+            client->ioConfigChanged(event, ioHandle, param2);
+        }
     }
 }
 
@@ -2711,7 +2730,7 @@
 
 sp<IAudioRecord> AudioFlinger::openRecord(
         pid_t pid,
-        void *input,
+        int input,
         uint32_t sampleRate,
         int format,
         int channelCount,
@@ -3204,7 +3223,7 @@
 
 // ----------------------------------------------------------------------------
 
-void *AudioFlinger::openOutput(uint32_t *pDevices,
+int AudioFlinger::openOutput(uint32_t *pDevices,
                                 uint32_t *pSamplingRate,
                                 uint32_t *pFormat,
                                 uint32_t *pChannels,
@@ -3227,7 +3246,7 @@
             flags);
 
     if (pDevices == NULL || *pDevices == 0) {
-        return NULL;
+        return 0;
     }
     Mutex::Autolock _l(mLock);
 
@@ -3249,12 +3268,12 @@
             (format != AudioSystem::PCM_16_BIT) ||
             (channels != AudioSystem::CHANNEL_OUT_STEREO)) {
             thread = new DirectOutputThread(this, output);
-            LOGV("openOutput() created direct output %p", thread);
+            LOGV("openOutput() created direct output: ID %d thread %p", (mNextThreadId + 1), thread);
         } else {
             thread = new MixerThread(this, output);
-            LOGV("openOutput() created mixer output %p", thread);
+            LOGV("openOutput() created mixer output: ID %d thread %p", (mNextThreadId + 1), thread);
         }
-        mPlaybackThreads.add(thread);
+        mPlaybackThreads.add(++mNextThreadId, thread);
 
         if (pSamplingRate) *pSamplingRate = samplingRate;
         if (pFormat) *pFormat = format;
@@ -3262,26 +3281,28 @@
         if (pLatencyMs) *pLatencyMs = thread->latency();
     }
 
-    return thread;
+    return mNextThreadId;
 }
 
-void *AudioFlinger::openDuplicateOutput(void *output1, void *output2)
+int AudioFlinger::openDuplicateOutput(int output1, int output2)
 {
     Mutex::Autolock _l(mLock);
+    MixerThread *thread1 = checkMixerThread_l(output1);
+    MixerThread *thread2 = checkMixerThread_l(output2);
 
-    if (checkMixerThread_l(output1) == NULL ||
-        checkMixerThread_l(output2) == NULL) {
-        LOGW("openDuplicateOutput() wrong output mixer type %p or %p", output1, output2);
-        return NULL;
+    if (thread1 == NULL || thread2 == NULL) {
+        LOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1, output2);
+        return 0;
     }
 
-    DuplicatingThread *thread = new DuplicatingThread(this, (MixerThread *)output1);
-    thread->addOutputTrack( (MixerThread *)output2);
-    mPlaybackThreads.add(thread);
-    return thread;
+
+    DuplicatingThread *thread = new DuplicatingThread(this, thread1);
+    thread->addOutputTrack(thread2);
+    mPlaybackThreads.add(++mNextThreadId, thread);
+    return mNextThreadId;
 }
 
-status_t AudioFlinger::closeOutput(void *output)
+status_t AudioFlinger::closeOutput(int output)
 {
     PlaybackThread *thread;
     {
@@ -3291,24 +3312,24 @@
             return BAD_VALUE;
         }
 
-        LOGV("closeOutput() %p", thread);
+        LOGV("closeOutput() %d", output);
 
         if (thread->type() == PlaybackThread::MIXER) {
             for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
-                if (mPlaybackThreads[i]->type() == PlaybackThread::DUPLICATING) {
-                    DuplicatingThread *dupThread = (DuplicatingThread *)mPlaybackThreads[i].get();
+                if (mPlaybackThreads.valueAt(i)->type() == PlaybackThread::DUPLICATING) {
+                    DuplicatingThread *dupThread = (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
                     dupThread->removeOutputTrack((MixerThread *)thread);
                 }
             }
         }
-        mPlaybackThreads.remove(thread);
+        mPlaybackThreads.removeItem(output);
     }
     thread->exit();
 
     return NO_ERROR;
 }
 
-status_t AudioFlinger::suspendOutput(void *output)
+status_t AudioFlinger::suspendOutput(int output)
 {
     Mutex::Autolock _l(mLock);
     PlaybackThread *thread = checkPlaybackThread_l(output);
@@ -3317,13 +3338,13 @@
         return BAD_VALUE;
     }
 
-    LOGV("suspendOutput() %p", output);
+    LOGV("suspendOutput() %d", output);
     thread->suspend();
 
     return NO_ERROR;
 }
 
-status_t AudioFlinger::restoreOutput(void *output)
+status_t AudioFlinger::restoreOutput(int output)
 {
     Mutex::Autolock _l(mLock);
     PlaybackThread *thread = checkPlaybackThread_l(output);
@@ -3332,14 +3353,14 @@
         return BAD_VALUE;
     }
 
-    LOGV("restoreOutput() %p", output);
+    LOGV("restoreOutput() %d", output);
 
     thread->restore();
 
     return NO_ERROR;
 }
 
-void *AudioFlinger::openInput(uint32_t *pDevices,
+int AudioFlinger::openInput(uint32_t *pDevices,
                                 uint32_t *pSamplingRate,
                                 uint32_t *pFormat,
                                 uint32_t *pChannels,
@@ -3355,7 +3376,7 @@
     uint32_t reqChannels = channels;
 
     if (pDevices == NULL || *pDevices == 0) {
-        return NULL;
+        return 0;
     }
     Mutex::Autolock _l(mLock);
 
@@ -3392,8 +3413,8 @@
     if (input != 0) {
          // Start record thread
         thread = new RecordThread(this, input, reqSamplingRate, reqChannels);
-        mRecordThreads.add(thread);
-
+        mRecordThreads.add(++mNextThreadId, thread);
+        LOGV("openInput() created record thread: ID %d thread %p", mNextThreadId, thread);
         if (pSamplingRate) *pSamplingRate = reqSamplingRate;
         if (pFormat) *pFormat = format;
         if (pChannels) *pChannels = reqChannels;
@@ -3401,10 +3422,10 @@
         input->standby();
     }
 
-    return thread;
+    return mNextThreadId;
 }
 
-status_t AudioFlinger::closeInput(void *input)
+status_t AudioFlinger::closeInput(int input)
 {
     RecordThread *thread;
     {
@@ -3414,27 +3435,27 @@
             return BAD_VALUE;
         }
 
-        LOGV("closeInput() %p", thread);
-        mRecordThreads.remove(thread);
+        LOGV("closeInput() %d", input);
+        mRecordThreads.removeItem(input);
     }
     thread->exit();
 
     return NO_ERROR;
 }
 
-status_t AudioFlinger::setStreamOutput(uint32_t stream, void *output)
+status_t AudioFlinger::setStreamOutput(uint32_t stream, int output)
 {
     Mutex::Autolock _l(mLock);
     MixerThread *dstThread = checkMixerThread_l(output);
     if (dstThread == NULL) {
-        LOGW("setStreamOutput() bad output thread %p", output);
+        LOGW("setStreamOutput() bad output id %d", output);
         return BAD_VALUE;
     }
 
-    LOGV("setStreamOutput() stream %d to output %p", stream, dstThread);
+    LOGV("setStreamOutput() stream %d to output %d", stream, output);
 
     for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
-        PlaybackThread *thread = mPlaybackThreads[i].get();
+        PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
         if (thread != dstThread &&
             thread->type() != PlaybackThread::DIRECT) {
             MixerThread *srcThread = (MixerThread *)thread;
@@ -3452,22 +3473,17 @@
 }
 
 // checkPlaybackThread_l() must be called with AudioFlinger::mLock held
-AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(void *output) const
+AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(int output) const
 {
     PlaybackThread *thread = NULL;
-
-    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
-        if (mPlaybackThreads[i] == output) {
-            thread = (PlaybackThread *)output;
-            break;
-        }
+    if (mPlaybackThreads.indexOfKey(output) >= 0) {
+        thread = (PlaybackThread *)mPlaybackThreads.valueFor(output).get();
     }
-
     return thread;
 }
 
 // checkMixerThread_l() must be called with AudioFlinger::mLock held
-AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(void *output) const
+AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(int output) const
 {
     PlaybackThread *thread = checkPlaybackThread_l(output);
     if (thread != NULL) {
@@ -3479,17 +3495,12 @@
 }
 
 // checkRecordThread_l() must be called with AudioFlinger::mLock held
-AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(void *input) const
+AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(int input) const
 {
     RecordThread *thread = NULL;
-
-    for (size_t i = 0; i < mRecordThreads.size(); i++) {
-        if (mRecordThreads[i] == input) {
-            thread = (RecordThread *)input;
-            break;
-        }
+    if (mRecordThreads.indexOfKey(input) >= 0) {
+        thread = (RecordThread *)mRecordThreads.valueFor(input).get();
     }
-
     return thread;
 }
 
diff --git a/libs/audioflinger/AudioFlinger.h b/libs/audioflinger/AudioFlinger.h
index 06c5846..4a4b823 100644
--- a/libs/audioflinger/AudioFlinger.h
+++ b/libs/audioflinger/AudioFlinger.h
@@ -73,14 +73,14 @@
                                 int frameCount,
                                 uint32_t flags,
                                 const sp<IMemory>& sharedBuffer,
-                                void *output,
+                                int output,
                                 status_t *status);
 
-    virtual     uint32_t    sampleRate(void *output) const;
-    virtual     int         channelCount(void *output) const;
-    virtual     int         format(void *output) const;
-    virtual     size_t      frameCount(void *output) const;
-    virtual     uint32_t    latency(void *output) const;
+    virtual     uint32_t    sampleRate(int output) const;
+    virtual     int         channelCount(int output) const;
+    virtual     int         format(int output) const;
+    virtual     size_t      frameCount(int output) const;
+    virtual     uint32_t    latency(int output) const;
 
     virtual     status_t    setMasterVolume(float value);
     virtual     status_t    setMasterMute(bool muted);
@@ -88,10 +88,10 @@
     virtual     float       masterVolume() const;
     virtual     bool        masterMute() const;
 
-    virtual     status_t    setStreamVolume(int stream, float value, void *output);
+    virtual     status_t    setStreamVolume(int stream, float value, int output);
     virtual     status_t    setStreamMute(int stream, bool muted);
 
-    virtual     float       streamVolume(int stream, void *output) const;
+    virtual     float       streamVolume(int stream, int output) const;
     virtual     bool        streamMute(int stream) const;
 
     virtual     status_t    setMode(int mode);
@@ -101,37 +101,37 @@
 
     virtual     bool        isMusicActive() const;
 
-    virtual     status_t    setParameters(void *ioHandle, const String8& keyValuePairs);
-    virtual     String8     getParameters(void *ioHandle, const String8& keys);
+    virtual     status_t    setParameters(int ioHandle, const String8& keyValuePairs);
+    virtual     String8     getParameters(int ioHandle, const String8& keys);
 
     virtual     void        registerClient(const sp<IAudioFlingerClient>& client);
 
     virtual     size_t      getInputBufferSize(uint32_t sampleRate, int format, int channelCount);
 
-    virtual void *openOutput(uint32_t *pDevices,
+    virtual int openOutput(uint32_t *pDevices,
                                     uint32_t *pSamplingRate,
                                     uint32_t *pFormat,
                                     uint32_t *pChannels,
                                     uint32_t *pLatencyMs,
                                     uint32_t flags);
 
-    virtual void *openDuplicateOutput(void *output1, void *output2);
+    virtual int openDuplicateOutput(int output1, int output2);
 
-    virtual status_t closeOutput(void *output);
+    virtual status_t closeOutput(int output);
 
-    virtual status_t suspendOutput(void *output);
+    virtual status_t suspendOutput(int output);
 
-    virtual status_t restoreOutput(void *output);
+    virtual status_t restoreOutput(int output);
 
-    virtual void *openInput(uint32_t *pDevices,
+    virtual int openInput(uint32_t *pDevices,
                             uint32_t *pSamplingRate,
                             uint32_t *pFormat,
                             uint32_t *pChannels,
                             uint32_t acoustics);
 
-    virtual status_t closeInput(void *input);
+    virtual status_t closeInput(int input);
 
-    virtual status_t setStreamOutput(uint32_t stream, void *output);
+    virtual status_t setStreamOutput(uint32_t stream, int output);
 
     // IBinder::DeathRecipient
     virtual     void        binderDied(const wp<IBinder>& who);
@@ -158,7 +158,7 @@
     // record interface
     virtual sp<IAudioRecord> openRecord(
                                 pid_t pid,
-                                void *input,
+                                int input,
                                 uint32_t sampleRate,
                                 int format,
                                 int channelCount,
@@ -172,8 +172,6 @@
                                 Parcel* reply,
                                 uint32_t flags);
 
-    void audioConfigChanged(int event, void *param1, void *param2);
-
 private:
                             AudioFlinger();
     virtual                 ~AudioFlinger();
@@ -615,10 +613,11 @@
         SortedVector < sp<OutputTrack> >  mOutputTracks;
     };
 
-              PlaybackThread *checkPlaybackThread_l(void *output) const;
-              MixerThread *checkMixerThread_l(void *output) const;
-              RecordThread *checkRecordThread_l(void *input) const;
+              PlaybackThread *checkPlaybackThread_l(int output) const;
+              MixerThread *checkMixerThread_l(int output) const;
+              RecordThread *checkRecordThread_l(int input) const;
               float streamVolumeInternal(int stream) const { return mStreamTypes[stream].volume; }
+              void audioConfigChanged(int event, const sp<ThreadBase>& thread, void *param2);
 
     friend class AudioBuffer;
 
@@ -744,14 +743,15 @@
     mutable     int                                 mHardwareStatus;
 
 
-                SortedVector< sp<PlaybackThread> >  mPlaybackThreads;
+                DefaultKeyedVector< int, sp<PlaybackThread> >  mPlaybackThreads;
                 PlaybackThread::stream_type_t       mStreamTypes[AudioSystem::NUM_STREAM_TYPES];
                 float                               mMasterVolume;
                 bool                                mMasterMute;
 
-                SortedVector< sp<RecordThread> >    mRecordThreads;
+                DefaultKeyedVector< int, sp<RecordThread> >    mRecordThreads;
 
                 SortedVector< sp<IBinder> >         mNotificationClients;
+                int                                 mNextThreadId;
 };
 
 // ----------------------------------------------------------------------------
diff --git a/libs/audioflinger/AudioHardwareInterface.cpp b/libs/audioflinger/AudioHardwareInterface.cpp
index 37be329..9a4a7f9 100644
--- a/libs/audioflinger/AudioHardwareInterface.cpp
+++ b/libs/audioflinger/AudioHardwareInterface.cpp
@@ -140,8 +140,8 @@
 // default implementation
 String8 AudioHardwareBase::getParameters(const String8& keys)
 {
-    String8 result = String8("");
-    return result;
+    AudioParameter param = AudioParameter(keys);
+    return param.toString();
 }
 
 // default implementation
diff --git a/libs/audioflinger/AudioHardwareStub.cpp b/libs/audioflinger/AudioHardwareStub.cpp
index 1a03059..ae391ee 100644
--- a/libs/audioflinger/AudioHardwareStub.cpp
+++ b/libs/audioflinger/AudioHardwareStub.cpp
@@ -152,6 +152,12 @@
     return NO_ERROR;
 }
 
+String8 AudioStreamOutStub::getParameters(const String8& keys)
+{
+    AudioParameter param = AudioParameter(keys);
+    return param.toString();
+}
+
 // ----------------------------------------------------------------------------
 
 status_t AudioStreamInStub::set(int *pFormat, uint32_t *pChannels, uint32_t *pRate,
@@ -187,6 +193,12 @@
     return NO_ERROR;
 }
 
+String8 AudioStreamInStub::getParameters(const String8& keys)
+{
+    AudioParameter param = AudioParameter(keys);
+    return param.toString();
+}
+
 // ----------------------------------------------------------------------------
 
 }; // namespace android
diff --git a/libs/audioflinger/AudioHardwareStub.h b/libs/audioflinger/AudioHardwareStub.h
index 8f43259..583f852 100644
--- a/libs/audioflinger/AudioHardwareStub.h
+++ b/libs/audioflinger/AudioHardwareStub.h
@@ -40,7 +40,7 @@
     virtual status_t    standby();
     virtual status_t    dump(int fd, const Vector<String16>& args);
     virtual status_t    setParameters(const String8& keyValuePairs) { return NO_ERROR;}
-    virtual String8     getParameters(const String8& keys) {String8 result = String8(""); return result;}
+    virtual String8     getParameters(const String8& keys);
 };
 
 class AudioStreamInStub : public AudioStreamIn {
@@ -55,7 +55,7 @@
     virtual status_t    dump(int fd, const Vector<String16>& args);
     virtual status_t    standby() { return NO_ERROR; }
     virtual status_t    setParameters(const String8& keyValuePairs) { return NO_ERROR;}
-    virtual String8     getParameters(const String8& keys) {String8 result = String8(""); return result;}
+    virtual String8     getParameters(const String8& keys);
 };
 
 class AudioHardwareStub : public  AudioHardwareBase
diff --git a/libs/audioflinger/AudioPolicyManagerGeneric.cpp b/libs/audioflinger/AudioPolicyManagerGeneric.cpp
index 4b31815..6b17b87 100644
--- a/libs/audioflinger/AudioPolicyManagerGeneric.cpp
+++ b/libs/audioflinger/AudioPolicyManagerGeneric.cpp
@@ -224,7 +224,7 @@
     if ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) ||
         (format != 0 && !AudioSystem::isLinearPCM(format)) ||
         (channels != 0 && channels != AudioSystem::CHANNEL_OUT_MONO && channels != AudioSystem::CHANNEL_OUT_STEREO)) {
-        return NULL;
+        return 0;
     }
 
     return mHardwareOutput;
@@ -232,10 +232,10 @@
 
 status_t AudioPolicyManagerGeneric::startOutput(audio_io_handle_t output, AudioSystem::stream_type stream)
 {
-    LOGV("startOutput() output %p, stream %d", output, stream);
+    LOGV("startOutput() output %d, stream %d", output, stream);
     ssize_t index = mOutputs.indexOfKey(output);
     if (index < 0) {
-        LOGW("startOutput() unknow output %p", output);
+        LOGW("startOutput() unknow output %d", output);
         return BAD_VALUE;
     }
 
@@ -253,10 +253,10 @@
 
 status_t AudioPolicyManagerGeneric::stopOutput(audio_io_handle_t output, AudioSystem::stream_type stream)
 {
-    LOGV("stopOutput() output %p, stream %d", output, stream);
+    LOGV("stopOutput() output %d, stream %d", output, stream);
     ssize_t index = mOutputs.indexOfKey(output);
     if (index < 0) {
-        LOGW("stopOutput() unknow output %p", output);
+        LOGW("stopOutput() unknow output %d", output);
         return BAD_VALUE;
     }
 
@@ -272,17 +272,17 @@
         outputDesc->changeRefCount(stream, -1);
         return NO_ERROR;
     } else {
-        LOGW("stopOutput() refcount is already 0 for output %p", output);
+        LOGW("stopOutput() refcount is already 0 for output %d", output);
         return INVALID_OPERATION;
     }
 }
 
 void AudioPolicyManagerGeneric::releaseOutput(audio_io_handle_t output)
 {
-    LOGV("releaseOutput() %p", output);
+    LOGV("releaseOutput() %d", output);
     ssize_t index = mOutputs.indexOfKey(output);
     if (index < 0) {
-        LOGW("releaseOutput() releasing unknown output %p", output);
+        LOGW("releaseOutput() releasing unknown output %d", output);
         return;
     }
 
@@ -332,7 +332,7 @@
                 samplingRate, format, channels);
         mpClientInterface->closeInput(input);
         delete inputDesc;
-        return NULL;
+        return 0;
     }
     mInputs.add(input, inputDesc);
     return input;
@@ -340,10 +340,10 @@
 
 status_t AudioPolicyManagerGeneric::startInput(audio_io_handle_t input)
 {
-    LOGV("startInput() input %p", input);
+    LOGV("startInput() input %d", input);
     ssize_t index = mInputs.indexOfKey(input);
     if (index < 0) {
-        LOGW("startInput() unknow input %p", input);
+        LOGW("startInput() unknow input %d", input);
         return BAD_VALUE;
     }
     AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
@@ -355,7 +355,7 @@
         // refuse 2 active AudioRecord clients at the same time
         for (size_t i = 0; i < mInputs.size(); i++) {
             if (mInputs.valueAt(i)->mRefCount > 0) {
-                LOGW("startInput() input %p, other input %p already started", input, mInputs.keyAt(i));
+                LOGW("startInput() input %d, other input %d already started", input, mInputs.keyAt(i));
                 return INVALID_OPERATION;
             }
         }
@@ -367,16 +367,16 @@
 
 status_t AudioPolicyManagerGeneric::stopInput(audio_io_handle_t input)
 {
-    LOGV("stopInput() input %p", input);
+    LOGV("stopInput() input %d", input);
     ssize_t index = mInputs.indexOfKey(input);
     if (index < 0) {
-        LOGW("stopInput() unknow input %p", input);
+        LOGW("stopInput() unknow input %d", input);
         return BAD_VALUE;
     }
     AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
 
     if (inputDesc->mRefCount == 0) {
-        LOGW("stopInput() input %p already stopped", input);
+        LOGW("stopInput() input %d already stopped", input);
         return INVALID_OPERATION;
     } else {
         inputDesc->mRefCount = 0;
@@ -386,10 +386,10 @@
 
 void AudioPolicyManagerGeneric::releaseInput(audio_io_handle_t input)
 {
-    LOGV("releaseInput() %p", input);
+    LOGV("releaseInput() %d", input);
     ssize_t index = mInputs.indexOfKey(input);
     if (index < 0) {
-        LOGW("releaseInput() releasing unknown input %p", input);
+        LOGW("releaseInput() releasing unknown input %d", input);
         return;
     }
     mpClientInterface->closeInput(input);
@@ -438,7 +438,7 @@
 
         float volume = computeVolume((int)stream, index, device);
 
-        LOGV("setStreamVolume() for output %p stream %d, volume %f", mOutputs.keyAt(i), stream, volume);
+        LOGV("setStreamVolume() for output %d stream %d, volume %f", mOutputs.keyAt(i), stream, volume);
         mpClientInterface->setStreamVolume(stream, volume, mOutputs.keyAt(i));
     }
     return NO_ERROR;
@@ -657,7 +657,7 @@
 
 void AudioPolicyManagerGeneric::setStreamMute(int stream, bool on, audio_io_handle_t output)
 {
-    LOGV("setStreamMute() stream %d, mute %d, output %p", stream, on, output);
+    LOGV("setStreamMute() stream %d, mute %d, output %d", stream, on, output);
 
     StreamDescriptor &streamDesc = mStreams[stream];
 
diff --git a/libs/audioflinger/AudioPolicyService.cpp b/libs/audioflinger/AudioPolicyService.cpp
index 7f6c4ed..ae17d76 100644
--- a/libs/audioflinger/AudioPolicyService.cpp
+++ b/libs/audioflinger/AudioPolicyService.cpp
@@ -201,7 +201,7 @@
                                     AudioSystem::output_flags flags)
 {
     if (mpPolicyManager == NULL) {
-        return NULL;
+        return 0;
     }
     LOGV("getOutput() tid %d", gettid());
     Mutex::Autolock _l(mLock);
@@ -245,7 +245,7 @@
                                     AudioSystem::audio_in_acoustics acoustics)
 {
     if (mpPolicyManager == NULL) {
-        return NULL;
+        return 0;
     }
     Mutex::Autolock _l(mLock);
     return mpPolicyManager->getInput(inputSource, samplingRate, format, channels, acoustics);
@@ -381,7 +381,7 @@
     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
     if (af == 0) {
         LOGW("openOutput() could not get AudioFlinger");
-        return NULL;
+        return 0;
     }
 
     return af->openOutput(pDevices, pSamplingRate, (uint32_t *)pFormat, pChannels, pLatencyMs, flags);
@@ -392,7 +392,7 @@
     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
     if (af == 0) {
         LOGW("openDuplicateOutput() could not get AudioFlinger");
-        return NULL;
+        return 0;
     }
     return af->openDuplicateOutput(output1, output2);
 }
@@ -437,7 +437,7 @@
     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
     if (af == 0) {
         LOGW("openInput() could not get AudioFlinger");
-        return NULL;
+        return 0;
     }
 
     return af->openInput(pDevices, pSamplingRate, (uint32_t *)pFormat, pChannels, acoustics);
@@ -453,7 +453,7 @@
 
 status_t AudioPolicyService::setStreamVolume(AudioSystem::stream_type stream, float volume, audio_io_handle_t output)
 {
-    return mAudioCommandThread->volumeCommand((int)stream, volume, (void *)output);
+    return mAudioCommandThread->volumeCommand((int)stream, volume, (int)output);
 }
 
 status_t AudioPolicyService::setStreamOutput(AudioSystem::stream_type stream, audio_io_handle_t output)
@@ -467,7 +467,7 @@
 
 void AudioPolicyService::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
 {
-    mAudioCommandThread->parametersCommand((void *)ioHandle, keyValuePairs);
+    mAudioCommandThread->parametersCommand((int)ioHandle, keyValuePairs);
 }
 
 String8 AudioPolicyService::getParameters(audio_io_handle_t ioHandle, const String8& keys)
@@ -547,7 +547,7 @@
                 }break;
             case SET_VOLUME: {
                 VolumeData *data = (VolumeData *)command->mParam;
-                LOGV("AudioCommandThread() processing set volume stream %d, volume %f, output %p", data->mStream, data->mVolume, data->mIO);
+                LOGV("AudioCommandThread() processing set volume stream %d, volume %f, output %d", data->mStream, data->mVolume, data->mIO);
                 mCommandStatus = AudioSystem::setStreamVolume(data->mStream, data->mVolume, data->mIO);
                 mCommandCond.signal();
                 mWaitWorkCV.wait(mLock);
@@ -555,7 +555,7 @@
                 }break;
             case SET_PARAMETERS: {
                  ParametersData *data = (ParametersData *)command->mParam;
-                 LOGV("AudioCommandThread() processing set parameters string %s, io %p", data->mKeyValuePairs.string(), data->mIO);
+                 LOGV("AudioCommandThread() processing set parameters string %s, io %d", data->mKeyValuePairs.string(), data->mIO);
                  mCommandStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
                  mCommandCond.signal();
                  mWaitWorkCV.wait(mLock);
@@ -599,7 +599,7 @@
     mWaitWorkCV.signal();
 }
 
-status_t AudioPolicyService::AudioCommandThread::volumeCommand(int stream, float volume, void *output)
+status_t AudioPolicyService::AudioCommandThread::volumeCommand(int stream, float volume, int output)
 {
     Mutex::Autolock _l(mLock);
     AudioCommand *command = new AudioCommand();
@@ -610,7 +610,7 @@
     data->mIO = output;
     command->mParam = data;
     mAudioCommands.add(command);
-    LOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %p", stream, volume, output);
+    LOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d", stream, volume, output);
     mWaitWorkCV.signal();
     mCommandCond.wait(mLock);
     status_t status =  mCommandStatus;
@@ -618,7 +618,7 @@
     return status;
 }
 
-status_t AudioPolicyService::AudioCommandThread::parametersCommand(void *ioHandle, const String8& keyValuePairs)
+status_t AudioPolicyService::AudioCommandThread::parametersCommand(int ioHandle, const String8& keyValuePairs)
 {
     Mutex::Autolock _l(mLock);
     AudioCommand *command = new AudioCommand();
@@ -628,7 +628,7 @@
     data->mKeyValuePairs = keyValuePairs;
     command->mParam = data;
     mAudioCommands.add(command);
-    LOGV("AudioCommandThread() adding set parameter string %s, io %p", keyValuePairs.string(), ioHandle);
+    LOGV("AudioCommandThread() adding set parameter string %s, io %d", keyValuePairs.string(), ioHandle);
     mWaitWorkCV.signal();
     mCommandCond.wait(mLock);
     status_t status =  mCommandStatus;
diff --git a/libs/audioflinger/AudioPolicyService.h b/libs/audioflinger/AudioPolicyService.h
index 1c46975..3909fa4 100644
--- a/libs/audioflinger/AudioPolicyService.h
+++ b/libs/audioflinger/AudioPolicyService.h
@@ -136,8 +136,8 @@
                     void        exit();
                     void        startToneCommand(int type = 0, int stream = 0);
                     void        stopToneCommand();
-                    status_t    volumeCommand(int stream, float volume, void *output);
-                    status_t    parametersCommand(void *ioHandle, const String8& keyValuePairs);
+                    status_t    volumeCommand(int stream, float volume, int output);
+                    status_t    parametersCommand(int ioHandle, const String8& keyValuePairs);
 
     private:
         // descriptor for requested tone playback event
@@ -157,11 +157,11 @@
         public:
             int mStream;
             float mVolume;
-            void *mIO;
+            int mIO;
         };
         class ParametersData {
         public:
-            void *mIO;
+            int mIO;
             String8 mKeyValuePairs;
         };
 
diff --git a/libs/surfaceflinger/DisplayHardware/DisplayHardware.cpp b/libs/surfaceflinger/DisplayHardware/DisplayHardware.cpp
index f0615f0..002a3ab 100644
--- a/libs/surfaceflinger/DisplayHardware/DisplayHardware.cpp
+++ b/libs/surfaceflinger/DisplayHardware/DisplayHardware.cpp
@@ -26,6 +26,7 @@
 
 #include <ui/PixelFormat.h>
 #include <ui/FramebufferNativeWindow.h>
+#include <ui/EGLUtils.h>
 
 #include <GLES/gl.h>
 #include <EGL/egl.h>
@@ -144,32 +145,11 @@
     eglInitialize(display, NULL, NULL);
     eglGetConfigs(display, NULL, 0, &numConfigs);
 
-    // Get all the "potential match" configs...
-    EGLConfig* const configs = new EGLConfig[numConfigs];
-    eglChooseConfig(display, attribs, configs, numConfigs, &n);
-    LOGE_IF(n<=0, "no EGLConfig available!");
-    EGLConfig config = configs[0];
-    if (n > 1) {
-        // if there is more than one candidate, go through the list
-        // and pick one that matches our framebuffer format
-        int fbSzA = fbFormatInfo.getSize(PixelFormatInfo::INDEX_ALPHA);
-        int fbSzR = fbFormatInfo.getSize(PixelFormatInfo::INDEX_RED);
-        int fbSzG = fbFormatInfo.getSize(PixelFormatInfo::INDEX_GREEN);
-        int fbSzB = fbFormatInfo.getSize(PixelFormatInfo::INDEX_BLUE); 
-        for (int i=0 ; i<n ; i++) {
-            EGLint r,g,b,a;
-            eglGetConfigAttrib(display, configs[i], EGL_RED_SIZE,   &r);
-            eglGetConfigAttrib(display, configs[i], EGL_GREEN_SIZE, &g);
-            eglGetConfigAttrib(display, configs[i], EGL_BLUE_SIZE,  &b);
-            eglGetConfigAttrib(display, configs[i], EGL_ALPHA_SIZE, &a);
-            if (fbSzA == a && fbSzR == r && fbSzG == g && fbSzB  == b) {
-                config = configs[i];
-                break;
-            }
-        }
-    }
-    delete [] configs;
-
+    EGLConfig config;
+    status_t err = EGLUtils::selectConfigForPixelFormat(
+            display, attribs, fbDev->format, &config);
+    LOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
+    
     /*
      * Gather EGL extensions
      */
diff --git a/libs/ui/Android.mk b/libs/ui/Android.mk
index 49939ca..93c7263 100644
--- a/libs/ui/Android.mk
+++ b/libs/ui/Android.mk
@@ -5,6 +5,7 @@
 	BufferMapper.cpp \
 	Camera.cpp \
 	CameraParameters.cpp \
+	EGLUtils.cpp \
 	EventHub.cpp \
 	EventRecurrence.cpp \
 	FramebufferNativeWindow.cpp \
@@ -29,6 +30,7 @@
 LOCAL_SHARED_LIBRARIES := \
 	libcutils \
 	libutils \
+	libEGL \
 	libbinder \
 	libpixelflinger \
 	libhardware \
diff --git a/libs/ui/EGLUtils.cpp b/libs/ui/EGLUtils.cpp
new file mode 100644
index 0000000..80bfdfd
--- /dev/null
+++ b/libs/ui/EGLUtils.cpp
@@ -0,0 +1,105 @@
+/*
+ * Copyright (C) 2009 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 "EGLUtils"
+
+#include <utils/Errors.h>
+
+#include <ui/EGLUtils.h>
+
+#include <EGL/egl.h>
+
+#include <private/ui/android_natives_priv.h>
+
+// ----------------------------------------------------------------------------
+namespace android {
+// ----------------------------------------------------------------------------
+
+status_t EGLUtils::selectConfigForPixelFormat(
+        EGLDisplay dpy,
+        EGLint const* attrs,
+        PixelFormat format,
+        EGLConfig* outConfig)
+{
+    EGLint numConfigs = -1, n=0;
+
+    if (outConfig == NULL)
+        return BAD_VALUE;
+    
+    int err;
+    PixelFormatInfo fbFormatInfo;
+    if ((err = getPixelFormatInfo(PixelFormat(format), &fbFormatInfo)) < 0) {
+        return err;
+    }
+
+    // Get all the "potential match" configs...
+    if (eglGetConfigs(dpy, NULL, 0, &numConfigs) == EGL_FALSE)
+        return BAD_VALUE;
+
+    EGLConfig* const configs = (EGLConfig*)malloc(sizeof(EGLConfig)*numConfigs);
+    if (eglChooseConfig(dpy, attrs, configs, numConfigs, &n) == EGL_FALSE) {
+        free(configs);
+        return BAD_VALUE;
+    }
+
+    const int fbSzA = fbFormatInfo.getSize(PixelFormatInfo::INDEX_ALPHA);
+    const int fbSzR = fbFormatInfo.getSize(PixelFormatInfo::INDEX_RED);
+    const int fbSzG = fbFormatInfo.getSize(PixelFormatInfo::INDEX_GREEN);
+    const int fbSzB = fbFormatInfo.getSize(PixelFormatInfo::INDEX_BLUE); 
+    
+    int i;
+    EGLConfig config = NULL;
+    for (i=0 ; i<n ; i++) {
+        EGLint r,g,b,a;
+        eglGetConfigAttrib(dpy, configs[i], EGL_RED_SIZE,   &r);
+        eglGetConfigAttrib(dpy, configs[i], EGL_GREEN_SIZE, &g);
+        eglGetConfigAttrib(dpy, configs[i], EGL_BLUE_SIZE,  &b);
+        eglGetConfigAttrib(dpy, configs[i], EGL_ALPHA_SIZE, &a);
+        if (fbSzA == a && fbSzR == r && fbSzG == g && fbSzB  == b) {
+            config = configs[i];
+            break;
+        }
+    }
+
+    free(configs);
+    
+    if (i<n) {
+        *outConfig = config;
+        return NO_ERROR;
+    }
+
+    return NAME_NOT_FOUND;
+}
+
+status_t EGLUtils::selectConfigForNativeWindow(
+        EGLDisplay dpy,
+        EGLint const* attrs,
+        EGLNativeWindowType window,
+        EGLConfig* outConfig)
+{
+    int err;
+    int format;
+    if ((err = window->query(window, NATIVE_WINDOW_FORMAT, &format)) < 0) {
+        return err;
+    }
+
+    return selectConfigForPixelFormat(dpy, attrs, format, outConfig);
+}
+
+// ----------------------------------------------------------------------------
+}; // namespace android
+// ----------------------------------------------------------------------------
diff --git a/libs/ui/EventHub.cpp b/libs/ui/EventHub.cpp
index 59c9476..df713cb 100644
--- a/libs/ui/EventHub.cpp
+++ b/libs/ui/EventHub.cpp
@@ -16,6 +16,7 @@
 //#define LOG_NDEBUG 0
 
 #include <ui/EventHub.h>
+#include <ui/KeycodeLabels.h>
 #include <hardware_legacy/power.h>
 
 #include <cutils/properties.h>
@@ -58,6 +59,18 @@
 #define SEQ_SHIFT 16
 #define id_to_index(id)         ((id&ID_MASK)+1)
 
+#ifndef ABS_MT_TOUCH_MAJOR
+#define ABS_MT_TOUCH_MAJOR      0x30    /* Major axis of touching ellipse */
+#endif
+
+#ifndef ABS_MT_POSITION_X
+#define ABS_MT_POSITION_X       0x35    /* Center X ellipse position */
+#endif
+
+#ifndef ABS_MT_POSITION_Y
+#define ABS_MT_POSITION_Y       0x36    /* Center Y ellipse position */
+#endif
+
 namespace android {
 
 static const char *WAKE_LOCK_ID = "KeyEvents";
@@ -69,8 +82,8 @@
     return (v1 > v2) ? v1 : v2;
 }
 
-EventHub::device_t::device_t(int32_t _id, const char* _path)
-    : id(_id), path(_path), classes(0)
+EventHub::device_t::device_t(int32_t _id, const char* _path, const char* name)
+    : id(_id), path(_path), name(name), classes(0)
     , keyBitmask(NULL), layoutMap(new KeyLayoutMap()), next(NULL) {
 }
 
@@ -580,7 +593,7 @@
         version >> 16, (version >> 8) & 0xff, version & 0xff);
 #endif
 
-    device_t* device = new device_t(devid|mDevicesById[devid].seq, deviceName);
+    device_t* device = new device_t(devid|mDevicesById[devid].seq, deviceName, name);
     if (device == NULL) {
         LOGE("out of memory");
         return -1;
@@ -590,6 +603,8 @@
     mFDs[mFDCount].events = POLLIN;
 
     // figure out the kinds of events the device reports
+    
+    // See if this is a keyboard, and classify it.
     uint8_t key_bitmask[(KEY_MAX+1)/8];
     memset(key_bitmask, 0, sizeof(key_bitmask));
     LOGV("Getting keys...");
@@ -601,15 +616,11 @@
         for (int i=0; i<((BTN_MISC+7)/8); i++) {
             if (key_bitmask[i] != 0) {
                 device->classes |= CLASS_KEYBOARD;
-                // 'Q' key support = cheap test of whether this is an alpha-capable kbd
-                if (test_bit(KEY_Q, key_bitmask)) {
-                    device->classes |= CLASS_ALPHAKEY;
-                }
                 break;
             }
         }
         if ((device->classes & CLASS_KEYBOARD) != 0) {
-            device->keyBitmask = new uint8_t[(KEY_MAX+1)/8];
+            device->keyBitmask = new uint8_t[sizeof(key_bitmask)];
             if (device->keyBitmask != NULL) {
                 memcpy(device->keyBitmask, key_bitmask, sizeof(key_bitmask));
             } else {
@@ -619,6 +630,8 @@
             }
         }
     }
+    
+    // See if this is a trackball.
     if (test_bit(BTN_MOUSE, key_bitmask)) {
         uint8_t rel_bitmask[(REL_MAX+1)/8];
         memset(rel_bitmask, 0, sizeof(rel_bitmask));
@@ -630,16 +643,22 @@
             }
         }
     }
-    if (test_bit(BTN_TOUCH, key_bitmask)) {
-        uint8_t abs_bitmask[(ABS_MAX+1)/8];
-        memset(abs_bitmask, 0, sizeof(abs_bitmask));
-        LOGV("Getting absolute controllers...");
-        if (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask) >= 0)
-        {
-            if (test_bit(ABS_X, abs_bitmask) && test_bit(ABS_Y, abs_bitmask)) {
-                device->classes |= CLASS_TOUCHSCREEN;
-            }
-        }
+    
+    uint8_t abs_bitmask[(ABS_MAX+1)/8];
+    memset(abs_bitmask, 0, sizeof(abs_bitmask));
+    LOGV("Getting absolute controllers...");
+    ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask);
+    
+    // Is this a new modern multi-touch driver?
+    if (test_bit(ABS_MT_TOUCH_MAJOR, abs_bitmask)
+            && test_bit(ABS_MT_POSITION_X, abs_bitmask)
+            && test_bit(ABS_MT_POSITION_Y, abs_bitmask)) {
+        device->classes |= CLASS_TOUCHSCREEN | CLASS_TOUCHSCREEN_MT;
+        
+    // Is this an old style single-touch driver?
+    } else if (test_bit(BTN_TOUCH, key_bitmask)
+            && test_bit(ABS_X, abs_bitmask) && test_bit(ABS_Y, abs_bitmask)) {
+        device->classes |= CLASS_TOUCHSCREEN;
     }
 
 #ifdef EV_SW
@@ -658,21 +677,15 @@
     }
 #endif
 
-    LOGI("New device: path=%s name=%s id=0x%x (of 0x%x) index=%d fd=%d classes=0x%x\n",
-         deviceName, name, device->id, mNumDevicesById, mFDCount, fd, device->classes);
-
     if ((device->classes&CLASS_KEYBOARD) != 0) {
-        char devname[101];
-        char tmpfn[101];
+        char tmpfn[sizeof(name)];
         char keylayoutFilename[300];
 
         // a more descriptive name
-        ioctl(mFDs[mFDCount].fd, EVIOCGNAME(sizeof(devname)-1), devname);
-        devname[sizeof(devname)-1] = 0;
-        device->name = devname;
+        device->name = name;
 
         // replace all the spaces with underscores
-        strcpy(tmpfn, devname);
+        strcpy(tmpfn, name);
         for (char *p = strchr(tmpfn, ' '); p && *p; p = strchr(tmpfn, ' '))
             *p = '_';
 
@@ -705,12 +718,29 @@
         }
         char propName[100];
         sprintf(propName, "hw.keyboards.%u.devname", publicID);
-        property_set(propName, devname);
+        property_set(propName, name);
 
-        LOGI("New keyboard: publicID=%d device->id=%d devname='%s' propName='%s' keylayout='%s'\n",
-                publicID, device->id, devname, propName, keylayoutFilename);
+        // 'Q' key support = cheap test of whether this is an alpha-capable kbd
+        if (hasKeycode(device, kKeyCodeQ)) {
+            device->classes |= CLASS_ALPHAKEY;
+        }
+        
+        // See if this has a DPAD.
+        if (hasKeycode(device, kKeyCodeDpadUp) &&
+                hasKeycode(device, kKeyCodeDpadDown) &&
+                hasKeycode(device, kKeyCodeDpadLeft) &&
+                hasKeycode(device, kKeyCodeDpadRight) &&
+                hasKeycode(device, kKeyCodeDpadCenter)) {
+            device->classes |= CLASS_DPAD;
+        }
+        
+        LOGI("New keyboard: publicID=%d device->id=0x%x devname='%s' propName='%s' keylayout='%s'\n",
+                publicID, device->id, name, propName, keylayoutFilename);
     }
 
+    LOGI("New device: path=%s name=%s id=0x%x (of 0x%x) index=%d fd=%d classes=0x%x\n",
+         deviceName, name, device->id, mNumDevicesById, mFDCount, fd, device->classes);
+         
     LOGV("Adding device %s %p at %d, id = %d, classes = 0x%x\n",
          deviceName, device, mFDCount, devid, device->classes);
 
@@ -723,6 +753,25 @@
     return 0;
 }
 
+bool EventHub::hasKeycode(device_t* device, int keycode) const
+{
+    if (device->keyBitmask == NULL || device->layoutMap == NULL) {
+        return false;
+    }
+    
+    Vector<int32_t> scanCodes;
+    device->layoutMap->findScancodes(keycode, &scanCodes);
+    const size_t N = scanCodes.size();
+    for (size_t i=0; i<N && i<=KEY_MAX; i++) {
+        int32_t sc = scanCodes.itemAt(i);
+        if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) {
+            return true;
+        }
+    }
+    
+    return false;
+}
+
 int EventHub::close_device(const char *deviceName)
 {
     AutoMutex _l(mLock);
diff --git a/libs/ui/FramebufferNativeWindow.cpp b/libs/ui/FramebufferNativeWindow.cpp
index 8b7ea21..7b85c7f 100644
--- a/libs/ui/FramebufferNativeWindow.cpp
+++ b/libs/ui/FramebufferNativeWindow.cpp
@@ -212,6 +212,9 @@
         case NATIVE_WINDOW_HEIGHT:
             *value = fb->height;
             return NO_ERROR;
+        case NATIVE_WINDOW_FORMAT:
+            *value = fb->format;
+            return NO_ERROR;
     }
     return BAD_VALUE;
 }
diff --git a/libs/ui/Surface.cpp b/libs/ui/Surface.cpp
index a4710aa..4abb7f6 100644
--- a/libs/ui/Surface.cpp
+++ b/libs/ui/Surface.cpp
@@ -108,6 +108,9 @@
 status_t SurfaceBuffer::writeToParcel(Parcel* reply, 
         android_native_buffer_t const* buffer)
 {
+    if (buffer == NULL) {
+        return BAD_VALUE;
+    }
     reply->writeInt32(buffer->width);
     reply->writeInt32(buffer->height);
     reply->writeInt32(buffer->stride);
@@ -617,6 +620,9 @@
         case NATIVE_WINDOW_HEIGHT:
             *value = int(mHeight);
             return NO_ERROR;
+        case NATIVE_WINDOW_FORMAT:
+            *value = int(mFormat);
+            return NO_ERROR;
     }
     return BAD_VALUE;
 }
diff --git a/opengl/tests/angeles/Android.mk b/opengl/tests/angeles/Android.mk
index e193483..d0c3221 100644
--- a/opengl/tests/angeles/Android.mk
+++ b/opengl/tests/angeles/Android.mk
@@ -2,7 +2,7 @@
 
 LOCAL_PATH:= $(call my-dir)
 include $(CLEAR_VARS)
-LOCAL_SRC_FILES:= app-linux.c demo.c.arm
+LOCAL_SRC_FILES:= app-linux.cpp demo.c.arm
 LOCAL_SHARED_LIBRARIES := libEGL libGLESv1_CM libui
 LOCAL_MODULE:= angeles
 LOCAL_MODULE_TAGS := optional
diff --git a/opengl/tests/angeles/app-linux.c b/opengl/tests/angeles/app-linux.cpp
similarity index 86%
rename from opengl/tests/angeles/app-linux.c
rename to opengl/tests/angeles/app-linux.cpp
index 7d0d320..9c71693 100644
--- a/opengl/tests/angeles/app-linux.c
+++ b/opengl/tests/angeles/app-linux.cpp
@@ -52,6 +52,11 @@
 #include <EGL/egl.h>
 #include <GLES/gl.h>
 
+#include <ui/FramebufferNativeWindow.h>
+#include <ui/EGLUtils.h>
+
+using namespace android;
+
 #include "app.h"
 
 
@@ -115,48 +120,33 @@
 
 static int initGraphics()
 {
-    EGLint s_configAttribs[] = {
-         EGL_RED_SIZE,       5,
-         EGL_GREEN_SIZE,     6,
-         EGL_BLUE_SIZE,      5,
- #if 1
-         EGL_DEPTH_SIZE,     16,
-         EGL_STENCIL_SIZE,   0,
- #else
-         EGL_ALPHA_SIZE,     EGL_DONT_CARE,
-         EGL_DEPTH_SIZE,     EGL_DONT_CARE,
-         EGL_STENCIL_SIZE,   EGL_DONT_CARE,
-         EGL_SURFACE_TYPE,   EGL_DONT_CARE,
- #endif
+    EGLint configAttribs[] = {
+         EGL_DEPTH_SIZE, 16,
          EGL_NONE
      };
      
-     EGLint numConfigs = -1;
      EGLint majorVersion;
      EGLint minorVersion;
-     EGLConfig config;
      EGLContext context;
+     EGLConfig config;
      EGLSurface surface;
-     
+     EGLint w, h;
      EGLDisplay dpy;
 
      dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
-     egl_error("eglGetDisplay");
-     fprintf(stderr,"dpy = 0x%08x\n", (unsigned) dpy);
-     
      eglInitialize(dpy, &majorVersion, &minorVersion);
-     egl_error("eglInitialize");
-
-     eglGetConfigs(dpy, NULL, 0, &numConfigs);
-     egl_error("eglGetConfigs");
-     fprintf(stderr,"num configs %d\n", numConfigs);
+          
+     EGLNativeWindowType window = android_createDisplaySurface();
      
-     eglChooseConfig(dpy, s_configAttribs, &config, 1, &numConfigs);
-     egl_error("eglChooseConfig");
+     status_t err = EGLUtils::selectConfigForNativeWindow(
+             dpy, configAttribs, window, &config);
+     if (err) {
+         fprintf(stderr, "couldn't find an EGLConfig matching the screen format\n");
+         return 0;
+     }
 
-     surface = eglCreateWindowSurface(dpy, config,
-             android_createDisplaySurface(), NULL);
-     egl_error("eglMapWindowSurface");
+     surface = eglCreateWindowSurface(dpy, config, window, NULL);
+     egl_error("eglCreateWindowSurface");
 
      fprintf(stderr,"surface = %p\n", surface);
 
diff --git a/opengl/tests/fillrate/Android.mk b/opengl/tests/fillrate/Android.mk
new file mode 100644
index 0000000..a7d30c2
--- /dev/null
+++ b/opengl/tests/fillrate/Android.mk
@@ -0,0 +1,17 @@
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES:= \
+	fillrate.cpp
+
+LOCAL_SHARED_LIBRARIES := \
+	libcutils \
+    libEGL \
+    libGLESv1_CM \
+    libui
+
+LOCAL_MODULE:= test-opengl-fillrate
+
+LOCAL_MODULE_TAGS := optional
+
+include $(BUILD_EXECUTABLE)
diff --git a/opengl/tests/fillrate/fillrate.cpp b/opengl/tests/fillrate/fillrate.cpp
new file mode 100644
index 0000000..4ffbc8b
--- /dev/null
+++ b/opengl/tests/fillrate/fillrate.cpp
@@ -0,0 +1,161 @@
+/*
+**
+** Copyright 2006, 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 "fillrate"
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <EGL/egl.h>
+#include <GLES/gl.h>
+#include <GLES/glext.h>
+
+#include <utils/StopWatch.h>
+#include <ui/FramebufferNativeWindow.h>
+#include <ui/EGLUtils.h>
+
+using namespace android;
+
+int main(int argc, char** argv)
+{
+    EGLint configAttribs[] = {
+         EGL_DEPTH_SIZE, 0,
+         EGL_NONE
+     };
+     
+     EGLint majorVersion;
+     EGLint minorVersion;
+     EGLContext context;
+     EGLConfig config;
+     EGLSurface surface;
+     EGLint w, h;
+     EGLDisplay dpy;
+
+     dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
+     eglInitialize(dpy, &majorVersion, &minorVersion);
+          
+     EGLNativeWindowType window = android_createDisplaySurface();
+     
+     status_t err = EGLUtils::selectConfigForNativeWindow(
+             dpy, configAttribs, window, &config);
+     if (err) {
+         fprintf(stderr, "couldn't find an EGLConfig matching the screen format\n");
+         return 0;
+     }
+
+     surface = eglCreateWindowSurface(dpy, config, window, NULL);
+     context = eglCreateContext(dpy, config, NULL, NULL);
+     eglMakeCurrent(dpy, surface, surface, context);   
+     eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
+     eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
+     
+     printf("w=%d, h=%d\n", w, h);
+     
+     glBindTexture(GL_TEXTURE_2D, 0);
+     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+     glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
+     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+     glDisable(GL_DITHER);
+     glEnable(GL_BLEND);
+     glEnable(GL_TEXTURE_2D);
+     glColor4f(1,1,1,1);
+
+     uint32_t* t32 = (uint32_t*)malloc(512*512*4); 
+     for (int y=0 ; y<512 ; y++) {
+         for (int x=0 ; x<512 ; x++) {
+             int u = x-256;
+             int v = y-256;
+             if (u*u+v*v < 256*256) {
+                 t32[x+y*512] = 0x10FFFFFF;
+             } else {
+                 t32[x+y*512] = 0x20FF0000;
+             }
+         }
+     }
+
+     const GLfloat vertices[4][2] = {
+             { 0,  0 },
+             { 0,  h },
+             { w,  h },
+             { w,  0 }
+     };
+
+     const GLfloat texCoords[4][2] = {
+             { 0,  0 },
+             { 0,  1 },
+             { 1,  1 },
+             { 1,  0 }
+     };
+
+     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, t32);
+
+     glViewport(0, 0, w, h);
+     glMatrixMode(GL_PROJECTION);
+     glLoadIdentity();
+     glOrthof(0, w, 0, h, 0, 1);
+
+     glEnableClientState(GL_VERTEX_ARRAY);
+     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+     glVertexPointer(2, GL_FLOAT, 0, vertices);
+     glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
+
+     eglSwapInterval(dpy, 1);
+
+     glClearColor(1,0,0,0);
+     glClear(GL_COLOR_BUFFER_BIT);
+     glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 
+     eglSwapBuffers(dpy, surface);
+     
+
+     nsecs_t times[32];
+
+     for (int c=1 ; c<32 ; c++) {
+         glClear(GL_COLOR_BUFFER_BIT);
+         for (int i=0 ; i<c ; i++) {
+             glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 
+         }
+         eglSwapBuffers(dpy, surface);
+     }
+
+
+     //     for (int c=31 ; c>=1 ; c--) {
+     int j=0;
+     for (int c=1 ; c<32 ; c++) {
+         glClear(GL_COLOR_BUFFER_BIT);
+         nsecs_t now = systemTime();
+         for (int i=0 ; i<c ; i++) {
+             glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 
+         }
+         eglSwapBuffers(dpy, surface);
+         nsecs_t t = systemTime() - now;
+         times[j++] = t;
+     }
+
+     for (int c=1, j=0 ; c<32 ; c++, j++) {
+         nsecs_t t = times[j];
+         printf("%lld\t%d\t%f\n", t, c, (double(t)/c)/1000000.0);
+     }
+
+
+       
+     eglTerminate(dpy);
+     
+     return 0;
+}
diff --git a/opengl/tests/filter/Android.mk b/opengl/tests/filter/Android.mk
index 31b7d9a..a254127 100644
--- a/opengl/tests/filter/Android.mk
+++ b/opengl/tests/filter/Android.mk
@@ -2,7 +2,7 @@
 include $(CLEAR_VARS)
 
 LOCAL_SRC_FILES:= \
-	filter.c
+	filter.cpp
 
 LOCAL_SHARED_LIBRARIES := \
 	libcutils \
@@ -14,4 +14,6 @@
 
 LOCAL_MODULE_TAGS := optional
 
+LOCAL_CFLAGS := -DGL_GLEXT_PROTOTYPES
+
 include $(BUILD_EXECUTABLE)
diff --git a/opengl/tests/filter/filter.c b/opengl/tests/filter/filter.cpp
similarity index 90%
rename from opengl/tests/filter/filter.c
rename to opengl/tests/filter/filter.cpp
index de97119..e82b12d 100644
--- a/opengl/tests/filter/filter.c
+++ b/opengl/tests/filter/filter.cpp
@@ -5,6 +5,11 @@
 #include <GLES/gl.h>
 #include <GLES/glext.h>
 
+#include <ui/FramebufferNativeWindow.h>
+#include <ui/EGLUtils.h>
+
+using namespace android;
+
 int main(int argc, char** argv)
 {
     if (argc!=2 && argc!=3) {
@@ -34,12 +39,14 @@
 
      dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
      eglInitialize(dpy, &majorVersion, &minorVersion);
-     eglChooseConfig(dpy, s_configAttribs, &config, 1, &numConfigs);
      if (!usePbuffer) {
-         surface = eglCreateWindowSurface(dpy, config,
-                 android_createDisplaySurface(), NULL);
+         EGLNativeWindowType window = android_createDisplaySurface();
+         surface = eglCreateWindowSurface(dpy, config, window, NULL);
+         EGLUtils::selectConfigForNativeWindow(
+                 dpy, s_configAttribs, window, &config);
      } else {
          printf("using pbuffer\n");
+         eglChooseConfig(dpy, s_configAttribs, &config, 1, &numConfigs);
          EGLint attribs[] = { EGL_WIDTH, 320, EGL_HEIGHT, 480, EGL_NONE };
          surface = eglCreatePbufferSurface(dpy, config, attribs);
          if (surface == EGL_NO_SURFACE) {
diff --git a/opengl/tests/finish/Android.mk b/opengl/tests/finish/Android.mk
index 8b46cd7..5620814 100644
--- a/opengl/tests/finish/Android.mk
+++ b/opengl/tests/finish/Android.mk
@@ -2,7 +2,7 @@
 include $(CLEAR_VARS)
 
 LOCAL_SRC_FILES:= \
-	finish.c
+	finish.cpp
 
 LOCAL_SHARED_LIBRARIES := \
 	libcutils \
@@ -14,4 +14,6 @@
 
 LOCAL_MODULE_TAGS := optional
 
+LOCAL_CFLAGS := -DGL_GLEXT_PROTOTYPES
+
 include $(BUILD_EXECUTABLE)
diff --git a/opengl/tests/finish/finish.c b/opengl/tests/finish/finish.cpp
similarity index 90%
rename from opengl/tests/finish/finish.c
rename to opengl/tests/finish/finish.cpp
index 45fc758..b5b8142 100644
--- a/opengl/tests/finish/finish.c
+++ b/opengl/tests/finish/finish.cpp
@@ -24,39 +24,41 @@
 #include <GLES/gl.h>
 #include <GLES/glext.h>
 
+#include <utils/Timers.h>
 
-long long systemTime()
-{
-    struct timespec t;
-    t.tv_sec = t.tv_nsec = 0;
-    clock_gettime(CLOCK_MONOTONIC, &t);
-    return (long long)(t.tv_sec)*1000000000LL + t.tv_nsec;
-}
+#include <ui/FramebufferNativeWindow.h>
+#include <ui/EGLUtils.h>
+
+using namespace android;
 
 int main(int argc, char** argv)
 {
-    EGLint s_configAttribs[] = {
-         EGL_RED_SIZE,       5,
-         EGL_GREEN_SIZE,     6,
-         EGL_BLUE_SIZE,      5,
+    EGLint configAttribs[] = {
+         EGL_DEPTH_SIZE, 0,
          EGL_NONE
      };
      
-     EGLint numConfigs = -1;
      EGLint majorVersion;
      EGLint minorVersion;
-     EGLConfig config;
      EGLContext context;
+     EGLConfig config;
      EGLSurface surface;
      EGLint w, h;
-     
      EGLDisplay dpy;
 
      dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
      eglInitialize(dpy, &majorVersion, &minorVersion);
-     eglChooseConfig(dpy, s_configAttribs, &config, 1, &numConfigs);
-     surface = eglCreateWindowSurface(dpy, config, 
-             android_createDisplaySurface(), NULL);
+          
+     EGLNativeWindowType window = android_createDisplaySurface();
+     
+     status_t err = EGLUtils::selectConfigForNativeWindow(
+             dpy, configAttribs, window, &config);
+     if (err) {
+         fprintf(stderr, "couldn't find an EGLConfig matching the screen format\n");
+         return 0;
+     }
+
+     surface = eglCreateWindowSurface(dpy, config, window, NULL);
      context = eglCreateContext(dpy, config, NULL, NULL);
      eglMakeCurrent(dpy, surface, surface, context);   
      eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
@@ -75,13 +77,13 @@
      long long now, t;
      int i;
 
-     char* texels = malloc(512*512*2);
+     char* texels = (char*)malloc(512*512*2);
      memset(texels,0xFF,512*512*2);
      
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
              512, 512, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, texels);
 
-     char* dst = malloc(320*480*2);
+     char* dst = (char*)malloc(320*480*2);
      memset(dst, 0, 320*480*2);
      printf("307200 bytes memcpy\n");
      for (i=0 ; i<4 ; i++) {
diff --git a/opengl/tests/swapinterval/Android.mk b/opengl/tests/swapinterval/Android.mk
new file mode 100644
index 0000000..619447c
--- /dev/null
+++ b/opengl/tests/swapinterval/Android.mk
@@ -0,0 +1,17 @@
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES:= \
+	swapinterval.cpp
+
+LOCAL_SHARED_LIBRARIES := \
+	libcutils \
+    libEGL \
+    libGLESv1_CM \
+    libui
+
+LOCAL_MODULE:= test-opengl-swapinterval
+
+LOCAL_MODULE_TAGS := optional
+
+include $(BUILD_EXECUTABLE)
diff --git a/opengl/tests/swapinterval/swapinterval.cpp b/opengl/tests/swapinterval/swapinterval.cpp
new file mode 100644
index 0000000..cf908a0
--- /dev/null
+++ b/opengl/tests/swapinterval/swapinterval.cpp
@@ -0,0 +1,104 @@
+/*
+**
+** Copyright 2006, 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 "fillrate"
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <EGL/egl.h>
+#include <GLES/gl.h>
+#include <GLES/glext.h>
+
+#include <utils/StopWatch.h>
+#include <ui/FramebufferNativeWindow.h>
+#include <ui/EGLUtils.h>
+
+using namespace android;
+
+int main(int argc, char** argv)
+{
+    EGLint configAttribs[] = {
+         EGL_DEPTH_SIZE, 0,
+         EGL_NONE
+     };
+     
+     EGLint majorVersion;
+     EGLint minorVersion;
+     EGLContext context;
+     EGLConfig config;
+     EGLSurface surface;
+     EGLint w, h;
+     EGLDisplay dpy;
+
+     dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
+     eglInitialize(dpy, &majorVersion, &minorVersion);
+          
+     EGLNativeWindowType window = android_createDisplaySurface();
+     
+     status_t err = EGLUtils::selectConfigForNativeWindow(
+             dpy, configAttribs, window, &config);
+     if (err) {
+         fprintf(stderr, "couldn't find an EGLConfig matching the screen format\n");
+         return 0;
+     }
+
+     surface = eglCreateWindowSurface(dpy, config, window, NULL);
+     context = eglCreateContext(dpy, config, NULL, NULL);
+     eglMakeCurrent(dpy, surface, surface, context);   
+     eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
+     eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
+     
+     printf("w=%d, h=%d\n", w, h);
+     
+     glDisable(GL_DITHER);
+     glEnable(GL_BLEND);
+
+     glViewport(0, 0, w, h);
+     glOrthof(0, w, 0, h, 0, 1);
+
+     eglSwapInterval(dpy, 1);
+
+     glClearColor(1,0,0,0);
+     glClear(GL_COLOR_BUFFER_BIT);
+     eglSwapBuffers(dpy, surface);
+
+
+     int time = 10;
+     printf("screen should flash red/green quickly for %d s...\n", time);
+     
+     int c = 0;
+     nsecs_t start = systemTime();
+     nsecs_t t;
+     do {
+         glClearColor(1,0,0,0);
+         glClear(GL_COLOR_BUFFER_BIT);
+         eglSwapBuffers(dpy, surface);
+         glClearColor(0,1,0,0);
+         glClear(GL_COLOR_BUFFER_BIT);
+         eglSwapBuffers(dpy, surface);
+         t = systemTime() - start;
+         c += 2;
+     } while (int(ns2s(t))<=time);
+     
+     double p =  (double(t) / c) / 1000000000.0;
+     printf("refresh-rate is %f fps (%f ms)\n", 1.0f/p, p*1000.0);
+       
+     eglTerminate(dpy);
+     
+     return 0;
+}
diff --git a/opengl/tests/textures/Android.mk b/opengl/tests/textures/Android.mk
index 8d5f56d..b2fa185 100644
--- a/opengl/tests/textures/Android.mk
+++ b/opengl/tests/textures/Android.mk
@@ -2,7 +2,7 @@
 include $(CLEAR_VARS)
 
 LOCAL_SRC_FILES:= \
-	textures.c
+	textures.cpp
 
 LOCAL_SHARED_LIBRARIES := \
 	libcutils \
@@ -14,4 +14,6 @@
 
 LOCAL_MODULE_TAGS := optional
 
+LOCAL_CFLAGS := -DGL_GLEXT_PROTOTYPES
+
 include $(BUILD_EXECUTABLE)
diff --git a/opengl/tests/textures/textures.c b/opengl/tests/textures/textures.cpp
similarity index 86%
rename from opengl/tests/textures/textures.c
rename to opengl/tests/textures/textures.cpp
index 214291b..ee92e79 100644
--- a/opengl/tests/textures/textures.c
+++ b/opengl/tests/textures/textures.cpp
@@ -22,30 +22,39 @@
 #include <GLES/gl.h>
 #include <GLES/glext.h>
 
+#include <ui/FramebufferNativeWindow.h>
+#include <ui/EGLUtils.h>
+
+using namespace android;
+
 int main(int argc, char** argv)
 {
-    EGLint s_configAttribs[] = {
-         EGL_RED_SIZE,       5,
-         EGL_GREEN_SIZE,     6,
-         EGL_BLUE_SIZE,      5,
+    EGLint configAttribs[] = {
+         EGL_DEPTH_SIZE, 0,
          EGL_NONE
      };
      
-     EGLint numConfigs = -1;
      EGLint majorVersion;
      EGLint minorVersion;
-     EGLConfig config;
      EGLContext context;
+     EGLConfig config;
      EGLSurface surface;
      EGLint w, h;
-     
      EGLDisplay dpy;
 
      dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
      eglInitialize(dpy, &majorVersion, &minorVersion);
-     eglChooseConfig(dpy, s_configAttribs, &config, 1, &numConfigs);
-     surface = eglCreateWindowSurface(dpy, config,
-             android_createDisplaySurface(), NULL);
+          
+     EGLNativeWindowType window = android_createDisplaySurface();
+     
+     status_t err = EGLUtils::selectConfigForNativeWindow(
+             dpy, configAttribs, window, &config);
+     if (err) {
+         fprintf(stderr, "couldn't find an EGLConfig matching the screen format\n");
+         return 0;
+     }
+
+     surface = eglCreateWindowSurface(dpy, config, window, NULL);
      context = eglCreateContext(dpy, config, NULL, NULL);
      eglMakeCurrent(dpy, surface, surface, context);   
      eglQuerySurface(dpy, surface, EGL_WIDTH, &w);