Merge change 20157

* changes:
  Fix the issue of datacall not being reinitiated after exiting Emergency Callback Mode.
diff --git a/include/ui/EventHub.h b/include/ui/EventHub.h
index d62fd7d..bffba07 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;
     
@@ -122,6 +124,7 @@
     };
 
     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/libs/ui/EventHub.cpp b/libs/ui/EventHub.cpp
index 59c9476..27334b7 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";
@@ -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,9 +677,6 @@
     }
 #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];
@@ -707,10 +723,27 @@
         sprintf(propName, "hw.keyboards.%u.devname", publicID);
         property_set(propName, devname);
 
-        LOGI("New keyboard: publicID=%d device->id=%d devname='%s' propName='%s' keylayout='%s'\n",
+        // '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, devname, 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 +756,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/Surface.cpp b/libs/ui/Surface.cpp
index a4710aa..c73909f 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);
diff --git a/opengl/tests/angeles/app-linux.c b/opengl/tests/angeles/app-linux.c
index 7d0d320..6be4876 100644
--- a/opengl/tests/angeles/app-linux.c
+++ b/opengl/tests/angeles/app-linux.c
@@ -132,6 +132,7 @@
      };
      
      EGLint numConfigs = -1;
+     EGLint n = 0;
      EGLint majorVersion;
      EGLint minorVersion;
      EGLConfig config;
@@ -148,15 +149,43 @@
      egl_error("eglInitialize");
 
      eglGetConfigs(dpy, NULL, 0, &numConfigs);
-     egl_error("eglGetConfigs");
-     fprintf(stderr,"num configs %d\n", numConfigs);
+
+     // Get all the "potential match" configs...
+     EGLConfig* const configs = malloc(sizeof(EGLConfig)*numConfigs);
+     eglChooseConfig(dpy, s_configAttribs, configs, numConfigs, &n);
+     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 = 0; // should not hardcode
+         int fbSzR = 5; // should not hardcode
+         int fbSzG = 6; // should not hardcode
+         int fbSzB = 5; // should not hardcode
+         int i;
+         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);
      
-     eglChooseConfig(dpy, s_configAttribs, &config, 1, &numConfigs);
-     egl_error("eglChooseConfig");
+     
+     //eglGetConfigs(dpy, NULL, 0, &numConfigs);
+     //egl_error("eglGetConfigs");
+     //fprintf(stderr,"num configs %d\n", numConfigs);     
+     //eglChooseConfig(dpy, s_configAttribs, &config, 1, &numConfigs);
+     //egl_error("eglChooseConfig");
 
      surface = eglCreateWindowSurface(dpy, config,
              android_createDisplaySurface(), NULL);
-     egl_error("eglMapWindowSurface");
+     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..c814e8d
--- /dev/null
+++ b/opengl/tests/fillrate/fillrate.cpp
@@ -0,0 +1,149 @@
+/*
+**
+** 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>
+
+using namespace android;
+
+int main(int argc, char** argv)
+{
+    EGLint configAttribs[] = {
+         EGL_DEPTH_SIZE, 0,
+         EGL_NONE
+     };
+     
+     EGLint numConfigs = -1, n=0;
+     EGLint majorVersion;
+     EGLint minorVersion;
+     EGLConfig config;
+     EGLContext context;
+     EGLSurface surface;
+     EGLint w, h;
+     
+     EGLDisplay dpy;
+
+     dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
+     eglInitialize(dpy, &majorVersion, &minorVersion);
+     
+     // Get all the "potential match" configs...
+     eglGetConfigs(dpy, NULL, 0, &numConfigs);
+     EGLConfig* const configs = (EGLConfig*)malloc(sizeof(EGLConfig)*numConfigs);
+     eglChooseConfig(dpy, configAttribs, configs, numConfigs, &n);
+     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 = 0; // should not hardcode
+         int fbSzR = 5; // should not hardcode
+         int fbSzG = 6; // should not hardcode
+         int fbSzB = 5; // should not hardcode
+         int i;
+         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);
+     
+     surface = eglCreateWindowSurface(dpy, config,
+             android_createDisplaySurface(), 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_CLAMP_TO_EDGE);
+     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+     glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
+     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+     glDisable(GL_DITHER);
+     glDisable(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++) {
+             t32[x+y*512] = 0x10FFFFFF;
+         }
+     }
+
+     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_FIXED, 0, vertices);
+     glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
+
+     glClearColor(1,0,0,0);
+     glClear(GL_COLOR_BUFFER_BIT);
+     glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 
+     eglSwapBuffers(dpy, surface);
+     
+     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;
+         printf("%lld\t%d\t%f\n", t, c, (double(t)/c)/1000000.0);
+     }
+     return 0;
+}
diff --git a/opengl/tests/textures/textures.c b/opengl/tests/textures/textures.c
index 214291b..d877e74 100644
--- a/opengl/tests/textures/textures.c
+++ b/opengl/tests/textures/textures.c
@@ -31,7 +31,7 @@
          EGL_NONE
      };
      
-     EGLint numConfigs = -1;
+     EGLint numConfigs = -1, n=0;
      EGLint majorVersion;
      EGLint minorVersion;
      EGLConfig config;
@@ -43,7 +43,36 @@
 
      dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
      eglInitialize(dpy, &majorVersion, &minorVersion);
-     eglChooseConfig(dpy, s_configAttribs, &config, 1, &numConfigs);
+     
+     // Get all the "potential match" configs...
+     eglGetConfigs(dpy, NULL, 0, &numConfigs);
+     EGLConfig* const configs = malloc(sizeof(EGLConfig)*numConfigs);
+     eglChooseConfig(dpy, s_configAttribs, configs, numConfigs, &n);
+     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 = 0; // should not hardcode
+         int fbSzR = 5; // should not hardcode
+         int fbSzG = 6; // should not hardcode
+         int fbSzB = 5; // should not hardcode
+         int i;
+         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);
+     
+     
+     
      surface = eglCreateWindowSurface(dpy, config,
              android_createDisplaySurface(), NULL);
      context = eglCreateContext(dpy, config, NULL, NULL);