Merge "docs: revise javadocs for sip add a package description, revise class descriptions and edit some method docs" into gingerbread
diff --git a/include/ui/InputDispatcher.h b/include/ui/InputDispatcher.h
index b0b855e..5f77cba 100644
--- a/include/ui/InputDispatcher.h
+++ b/include/ui/InputDispatcher.h
@@ -1032,6 +1032,9 @@
     // Splitting motion events across windows.
     MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
 
+    // Reset and drop everything the dispatcher is doing.
+    void resetAndDropEverythingLocked(const char* reason);
+
     // Dump state.
     void dumpDispatchStateLocked(String8& dump);
     void logDispatchStateLocked();
diff --git a/libs/surfaceflinger_client/Surface.cpp b/libs/surfaceflinger_client/Surface.cpp
index d44aab9..854a3c6 100644
--- a/libs/surfaceflinger_client/Surface.cpp
+++ b/libs/surfaceflinger_client/Surface.cpp
@@ -866,7 +866,18 @@
         return BAD_VALUE;
 
     Mutex::Autolock _l(mSurfaceLock);
+    if (mConnected == NATIVE_WINDOW_API_EGL) {
+        return INVALID_OPERATION;
+    }
+
     mBufferInfo.set(w, h, format);
+    if (format != 0) {
+        // we update the format of the surface as reported by query().
+        // this is to allow applications to change the format of a surface's
+        // buffer, and have it reflected in EGL; which is needed for
+        // EGLConfig validation.
+        mFormat = format;
+    }
     return NO_ERROR;
 }
 
diff --git a/libs/ui/InputDispatcher.cpp b/libs/ui/InputDispatcher.cpp
index fef8148..ae81d26 100644
--- a/libs/ui/InputDispatcher.cpp
+++ b/libs/ui/InputDispatcher.cpp
@@ -1302,6 +1302,9 @@
     }
 
 Unresponsive:
+    // Reset temporary touch state to ensure we release unnecessary references to input channels.
+    mTempTouchState.reset();
+
     nsecs_t timeSpentWaitingForApplication = getTimeSpentWaitingForApplicationLocked(currentTime);
     updateDispatchStatisticsLocked(currentTime, entry,
             injectionResult, timeSpentWaitingForApplication);
@@ -2586,10 +2589,14 @@
         AutoMutex _l(mLock);
 
         if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) {
-            if (mDispatchFrozen && ! frozen) {
+            if (mDispatchFrozen && !frozen) {
                 resetANRTimeoutsLocked();
             }
 
+            if (mDispatchEnabled && !enabled) {
+                resetAndDropEverythingLocked("dispatcher is being disabled");
+            }
+
             mDispatchEnabled = enabled;
             mDispatchFrozen = frozen;
             changed = true;
@@ -2608,6 +2615,21 @@
     }
 }
 
+void InputDispatcher::resetAndDropEverythingLocked(const char* reason) {
+#if DEBUG_FOCUS
+    LOGD("Resetting and dropping all events (%s).", reason);
+#endif
+
+    synthesizeCancelationEventsForAllConnectionsLocked(InputState::CANCEL_ALL_EVENTS, reason);
+
+    resetKeyRepeatLocked();
+    releasePendingEventLocked();
+    drainInboundQueueLocked();
+    resetTargetsLocked();
+
+    mTouchState.reset();
+}
+
 void InputDispatcher::logDispatchStateLocked() {
     String8 dump;
     dumpDispatchStateLocked(dump);
diff --git a/opengl/libagl/egl.cpp b/opengl/libagl/egl.cpp
index ba33e17..662a1fa 100644
--- a/opengl/libagl/egl.cpp
+++ b/opengl/libagl/egl.cpp
@@ -833,6 +833,9 @@
     static bool mask(GLint reqValue, GLint confValue) {
         return (confValue & reqValue) == reqValue;
     }
+    static bool ignore(GLint reqValue, GLint confValue) {
+        return true;
+    }
 };
 
 // ----------------------------------------------------------------------------
@@ -1060,11 +1063,11 @@
         { EGL_CONFIG_CAVEAT,              config_management_t::exact   },
         { EGL_CONFIG_ID,                  config_management_t::exact   },
         { EGL_LEVEL,                      config_management_t::exact   },
-        { EGL_MAX_PBUFFER_HEIGHT,         config_management_t::exact   },
-        { EGL_MAX_PBUFFER_PIXELS,         config_management_t::exact   },
-        { EGL_MAX_PBUFFER_WIDTH,          config_management_t::exact   },
+        { EGL_MAX_PBUFFER_HEIGHT,         config_management_t::ignore   },
+        { EGL_MAX_PBUFFER_PIXELS,         config_management_t::ignore   },
+        { EGL_MAX_PBUFFER_WIDTH,          config_management_t::ignore   },
         { EGL_NATIVE_RENDERABLE,          config_management_t::exact   },
-        { EGL_NATIVE_VISUAL_ID,           config_management_t::exact   },
+        { EGL_NATIVE_VISUAL_ID,           config_management_t::ignore   },
         { EGL_NATIVE_VISUAL_TYPE,         config_management_t::exact   },
         { EGL_SAMPLES,                    config_management_t::exact   },
         { EGL_SAMPLE_BUFFERS,             config_management_t::exact   },
diff --git a/services/surfaceflinger/LayerBase.cpp b/services/surfaceflinger/LayerBase.cpp
index 069b85a..64eed4b 100644
--- a/services/surfaceflinger/LayerBase.cpp
+++ b/services/surfaceflinger/LayerBase.cpp
@@ -415,6 +415,20 @@
         cb = (texture.NPOTAdjust ? texture.hScale : 1.0f);
     }
 
+    /*
+     * For the buffer transformation, we apply the rotation last.
+     * Since we're transforming the texture-coordinates, we need
+     * to apply the inverse of the buffer transformation:
+     *   inverse( FLIP_V -> FLIP_H -> ROT_90 )
+     *   <=> inverse( ROT_90 * FLIP_H * FLIP_V )
+     *    =  inverse(FLIP_V) * inverse(FLIP_H) * inverse(ROT_90)
+     *    =  FLIP_V * FLIP_H * ROT_270
+     *   <=> ROT_270 -> FLIP_H -> FLIP_V
+     *
+     * The rotation is performed first, in the texture coordinate space.
+     *
+     */
+
     struct TexCoords {
         GLfloat u;
         GLfloat v;
diff --git a/services/surfaceflinger/LayerBuffer.cpp b/services/surfaceflinger/LayerBuffer.cpp
index ccaad51..edc00f1 100644
--- a/services/surfaceflinger/LayerBuffer.cpp
+++ b/services/surfaceflinger/LayerBuffer.cpp
@@ -676,8 +676,8 @@
                 overlay_dev->setPosition(overlay_dev, mOverlay, x,y,w,h);
                 // we need to combine the layer orientation and the
                 // user-requested orientation.
-                Transform finalTransform = Transform(mOrientation) *
-                        Transform(mLayer.getOrientation());
+                Transform finalTransform(Transform(mLayer.getOrientation()) *
+                        Transform(mOrientation));
                 overlay_dev->setParameter(overlay_dev, mOverlay,
                         OVERLAY_TRANSFORM, finalTransform.getOrientation());
                 overlay_dev->commit(overlay_dev, mOverlay);
diff --git a/services/surfaceflinger/Transform.cpp b/services/surfaceflinger/Transform.cpp
index f128429..0467a14 100644
--- a/services/surfaceflinger/Transform.cpp
+++ b/services/surfaceflinger/Transform.cpp
@@ -28,26 +28,40 @@
 
 // ---------------------------------------------------------------------------
 
-template <typename T> inline T min(T a, T b) {
+template <typename T>
+static inline T min(T a, T b) {
     return a<b ? a : b;
 }
-template <typename T> inline T min(T a, T b, T c) {
+template <typename T>
+static inline T min(T a, T b, T c) {
     return min(a, min(b, c));
 }
-template <typename T> inline T min(T a, T b, T c, T d) {
+template <typename T>
+static inline T min(T a, T b, T c, T d) {
     return min(a, b, min(c, d));
 }
 
-template <typename T> inline T max(T a, T b) {
+template <typename T>
+static inline T max(T a, T b) {
     return a>b ? a : b;
 }
-template <typename T> inline T max(T a, T b, T c) {
+template <typename T>
+static inline T max(T a, T b, T c) {
     return max(a, max(b, c));
 }
-template <typename T> inline T max(T a, T b, T c, T d) {
+template <typename T>
+static inline T max(T a, T b, T c, T d) {
     return max(a, b, max(c, d));
 }
 
+template <typename T>
+static inline
+void swap(T& a, T& b) {
+    T t(a);
+    a = b;
+    b = t;
+}
+
 // ---------------------------------------------------------------------------
 
 Transform::Transform() {
@@ -160,6 +174,11 @@
     }
 
     Transform H, V, R;
+    if (flags & ROT_90) {
+        // w & h are inverted when rotating by 90 degrees
+        swap(w, h);
+    }
+
     if (flags & FLIP_H) {
         H.mType = (FLIP_H << 8) | SCALE;
         H.mType |= isZero(w) ? IDENTITY : TRANSLATE;
@@ -177,14 +196,15 @@
     }
 
     if (flags & ROT_90) {
+        const float original_w = h;
         R.mType = (ROT_90 << 8) | ROTATE;
-        R.mType |= isZero(w) ? IDENTITY : TRANSLATE;
+        R.mType |= isZero(original_w) ? IDENTITY : TRANSLATE;
         mat33& M(R.mMatrix);
-        M[0][0] = 0;    M[1][0] =-1;    M[2][0] = w;
+        M[0][0] = 0;    M[1][0] =-1;    M[2][0] = original_w;
         M[0][1] = 1;    M[1][1] = 0;
     }
 
-    *this = ((H*V)*R);
+    *this = (R*(H*V));
     return NO_ERROR;
 }
 
@@ -282,8 +302,8 @@
             }
         } else if (isZero(a) && isZero(d)) {
             flags |= ROT_90;
-            if (b>0)    flags |= FLIP_H;
-            if (c<0)    flags |= FLIP_V;
+            if (b>0)    flags |= FLIP_V;
+            if (c<0)    flags |= FLIP_H;
             if (!absIsOne(b) || !absIsOne(c)) {
                 scale = true;
             }