Merge "Content and viewport rects set in one step." into ics-mr1
diff --git a/include/gui/SurfaceTexture.h b/include/gui/SurfaceTexture.h
index d7dd4d6..27d863d 100644
--- a/include/gui/SurfaceTexture.h
+++ b/include/gui/SurfaceTexture.h
@@ -271,7 +271,8 @@
               mRequestBufferCalled(false),
               mTransform(0),
               mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
-              mTimestamp(0) {
+              mTimestamp(0),
+              mFrameNumber(0) {
             mCrop.makeInvalid();
         }
 
@@ -340,6 +341,10 @@
         // mTimestamp is the current timestamp for this buffer slot. This gets
         // to set by queueBuffer each time this slot is queued.
         int64_t mTimestamp;
+
+        // mFrameNumber is the number of the queued frame for this slot.
+        uint64_t mFrameNumber;
+
     };
 
     // mSlots is the array of buffer slots that must be mirrored on the client
@@ -476,6 +481,12 @@
     // around a GL driver limitation on the number of FBO attachments, which the
     // browser's tile cache exceeds.
     const GLenum mTexTarget;
+
+    // mFrameCounter is the free running counter, incremented for every buffer queued
+    // with the surface Texture.
+    uint64_t mFrameCounter;
+
+
 };
 
 // ----------------------------------------------------------------------------
diff --git a/libs/gui/Android.mk b/libs/gui/Android.mk
index 9767568..ed319f5 100644
--- a/libs/gui/Android.mk
+++ b/libs/gui/Android.mk
@@ -32,10 +32,6 @@
 
 LOCAL_MODULE:= libgui
 
-ifeq ($(TARGET_BOARD_PLATFORM), tegra)
-	LOCAL_CFLAGS += -DALLOW_DEQUEUE_CURRENT_BUFFER
-endif
-
 include $(BUILD_SHARED_LIBRARY)
 
 ifeq (,$(ONE_SHOT_MAKEFILE))
diff --git a/libs/gui/SurfaceTexture.cpp b/libs/gui/SurfaceTexture.cpp
index 374f3c5..0561909 100644
--- a/libs/gui/SurfaceTexture.cpp
+++ b/libs/gui/SurfaceTexture.cpp
@@ -36,12 +36,8 @@
 #include <utils/Log.h>
 #include <utils/String8.h>
 
-#ifdef ALLOW_DEQUEUE_CURRENT_BUFFER
-#define FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER    true
-#warning "ALLOW_DEQUEUE_CURRENT_BUFFER enabled"
-#else
-#define FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER    false
-#endif
+
+#define ALLOW_DEQUEUE_CURRENT_BUFFER    false
 
 // Macros for including the SurfaceTexture name in log messages
 #define ST_LOGV(x, ...) LOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
@@ -116,7 +112,8 @@
     mAllowSynchronousMode(allowSynchronousMode),
     mConnectedApi(NO_CONNECTED_API),
     mAbandoned(false),
-    mTexTarget(texTarget) {
+    mTexTarget(texTarget),
+    mFrameCounter(0) {
     // Choose a name using the PID and a process-unique ID.
     mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
 
@@ -264,7 +261,8 @@
 
     status_t returnFlags(OK);
 
-    int found, foundSync;
+    int found = -1;
+    int foundSync = -1;
     int dequeuedCount = 0;
     bool tryAgain = true;
     while (tryAgain) {
@@ -327,7 +325,7 @@
             LOGW_IF((state == BufferSlot::FREE) && (mCurrentTexture==i),
                     "dequeueBuffer: buffer %d is both FREE and current!", i);
 
-            if (FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER) {
+            if (ALLOW_DEQUEUE_CURRENT_BUFFER) {
                 if (state == BufferSlot::FREE || i == mCurrentTexture) {
                     foundSync = i;
                     if (i != mCurrentTexture) {
@@ -337,9 +335,14 @@
                 }
             } else {
                 if (state == BufferSlot::FREE) {
-                    foundSync = i;
-                    found = i;
-                    break;
+                    /** For Asynchronous mode, we need to return the oldest of free buffers
+                    * There is only one instance when the Framecounter overflows, this logic
+                    * might return the earlier buffer to client. Which is a negligible impact
+                    **/
+                    if (found < 0 || mSlots[i].mFrameNumber < mSlots[found].mFrameNumber) {
+                        foundSync = i;
+                        found = i;
+                    }
                 }
             }
         }
@@ -434,6 +437,11 @@
             mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
             mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
         }
+        if (mCurrentTexture == buf) {
+            // The current texture no longer references the buffer in this slot
+            // since we just allocated a new buffer.
+            mCurrentTexture = INVALID_BUFFER_SLOT;
+        }
         returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
     }
     ST_LOGV("dequeueBuffer: returning slot=%d buf=%p flags=%#x", buf,
@@ -531,6 +539,9 @@
         mSlots[buf].mTransform = mNextTransform;
         mSlots[buf].mScalingMode = mNextScalingMode;
         mSlots[buf].mTimestamp = timestamp;
+        mFrameCounter++;
+        mSlots[buf].mFrameNumber = mFrameCounter;
+
         mDequeueCondition.signal();
 
         *outWidth = mDefaultWidth;
@@ -564,6 +575,7 @@
         return;
     }
     mSlots[buf].mBufferState = BufferSlot::FREE;
+    mSlots[buf].mFrameNumber = 0;
     mDequeueCondition.signal();
 }
 
@@ -897,6 +909,7 @@
 void SurfaceTexture::freeBufferLocked(int i) {
     mSlots[i].mGraphicBuffer = 0;
     mSlots[i].mBufferState = BufferSlot::FREE;
+    mSlots[i].mFrameNumber = 0;
     if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
         eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
         mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
diff --git a/opengl/libs/EGL/egl_cache.cpp b/opengl/libs/EGL/egl_cache.cpp
index 13a4929..fe32d43 100644
--- a/opengl/libs/EGL/egl_cache.cpp
+++ b/opengl/libs/EGL/egl_cache.cpp
@@ -85,7 +85,7 @@
             bool atStart = !strncmp(BC_EXT_STR " ", exts, bcExtLen+1);
             bool atEnd = (bcExtLen+1) < extsLen &&
                     !strcmp(" " BC_EXT_STR, exts + extsLen - (bcExtLen+1));
-            bool inMiddle = strstr(" " BC_EXT_STR " ", exts);
+            bool inMiddle = strstr(exts, " " BC_EXT_STR " ");
             if (equal || atStart || atEnd || inMiddle) {
                 PFNEGLSETBLOBCACHEFUNCSANDROIDPROC eglSetBlobCacheFuncsANDROID;
                 eglSetBlobCacheFuncsANDROID =
diff --git a/opengl/libs/EGL/egl_display.cpp b/opengl/libs/EGL/egl_display.cpp
index 862b48d..31119f9 100644
--- a/opengl/libs/EGL/egl_display.cpp
+++ b/opengl/libs/EGL/egl_display.cpp
@@ -220,15 +220,19 @@
         if (end) {
             // length of the extension string
             const size_t len = end - start;
-            // NOTE: we could avoid the copy if we had strnstr.
-            const String8 ext(start, len);
-            // now go through all implementations and look for this extension
-            for (int i = 0; i < IMPL_NUM_IMPLEMENTATIONS; i++) {
-                // if we find it, add this extension string to our list
-                // (and don't forget the space)
-                const char* match = strstr(disp[i].queryString.extensions, ext.string());
-                if (match && (match[len] == ' ' || match[len] == 0)) {
-                    mExtensionString.append(start, len+1);
+            if (len) {
+                // NOTE: we could avoid the copy if we had strnstr.
+                const String8 ext(start, len);
+                // now go through all implementations and look for this extension
+                for (int i = 0; i < IMPL_NUM_IMPLEMENTATIONS; i++) {
+                    if (disp[i].queryString.extensions) {
+                        // if we find it, add this extension string to our list
+                        // (and don't forget the space)
+                        const char* match = strstr(disp[i].queryString.extensions, ext.string());
+                        if (match && (match[len] == ' ' || match[len] == 0)) {
+                            mExtensionString.append(start, len+1);
+                        }
+                    }
                 }
             }
             // process the next extension string, and skip the space.
diff --git a/opengl/tools/glgen/stubs/gles11/GLES11ExtHeader.java-if b/opengl/tools/glgen/stubs/gles11/GLES11ExtHeader.java-if
index c5e34cd..0c5fa04 100644
--- a/opengl/tools/glgen/stubs/gles11/GLES11ExtHeader.java-if
+++ b/opengl/tools/glgen/stubs/gles11/GLES11ExtHeader.java-if
@@ -124,6 +124,10 @@
     public static final int GL_TEXTURE_MAX_ANISOTROPY_EXT                           = 0x84FE;
     public static final int GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT                       = 0x84FF;
     public static final int GL_BGRA                                                 = 0x80E1;
+    public static final int GL_TEXTURE_EXTERNAL_OES                                 = 0x8D65;
+    public static final int GL_SAMPLER_EXTERNAL_OES                                 = 0x8D66;
+    public static final int GL_TEXTURE_BINDING_EXTERNAL_OES                         = 0x8D67;
+    public static final int GL_REQUIRED_TEXTURE_IMAGE_UNITS_OES                     = 0x8D68;
 
     native private static void _nativeClassInit();
     static {
@@ -135,4 +139,4 @@
     private static final int GL_FLOAT = GLES10.GL_FLOAT;
     private static final int GL_SHORT = GLES10.GL_SHORT;
     
-    private static Buffer _matrixIndexPointerOES;
\ No newline at end of file
+    private static Buffer _matrixIndexPointerOES;
diff --git a/services/surfaceflinger/Android.mk b/services/surfaceflinger/Android.mk
index 53502dbc..61a8358 100644
--- a/services/surfaceflinger/Android.mk
+++ b/services/surfaceflinger/Android.mk
@@ -30,10 +30,6 @@
 	LOCAL_CFLAGS += -DHAS_CONTEXT_PRIORITY -DNEVER_DEFAULT_TO_ASYNC_MODE
 endif
 
-ifneq (,$(findstring $(TARGET_DEVICE),tuna toro maguro))
-	LOCAL_CFLAGS += -DREFRESH_RATE=59
-endif
-
 
 LOCAL_SHARED_LIBRARIES := \
 	libcutils \