Merge "Don't hold onto the lock while reconnecting to the server" into ics-mr0
diff --git a/include/gui/SensorManager.h b/include/gui/SensorManager.h
index e1b1a7b..3176462 100644
--- a/include/gui/SensorManager.h
+++ b/include/gui/SensorManager.h
@@ -20,6 +20,8 @@
 #include <stdint.h>
 #include <sys/types.h>
 
+#include <binder/IBinder.h>
+
 #include <utils/Errors.h>
 #include <utils/RefBase.h>
 #include <utils/Singleton.h>
@@ -41,7 +43,9 @@
 
 // ----------------------------------------------------------------------------
 
-class SensorManager : public ASensorManager, public Singleton<SensorManager>
+class SensorManager :
+    public ASensorManager,
+    public Singleton<SensorManager>
 {
 public:
     SensorManager();
@@ -52,9 +56,17 @@
     sp<SensorEventQueue> createEventQueue();
 
 private:
-    sp<ISensorServer> mSensorServer;
-    Sensor const** mSensorList;
-    Vector<Sensor> mSensors;
+    // DeathRecipient interface
+    void sensorManagerDied();
+
+    status_t assertStateLocked() const;
+
+private:
+    mutable Mutex mLock;
+    mutable sp<ISensorServer> mSensorServer;
+    mutable Sensor const** mSensorList;
+    mutable Vector<Sensor> mSensors;
+    mutable sp<IBinder::DeathRecipient> mDeathObserver;
 };
 
 // ----------------------------------------------------------------------------
diff --git a/include/surfaceflinger/ISurfaceComposer.h b/include/surfaceflinger/ISurfaceComposer.h
index e0f4cf9..ea022a6 100644
--- a/include/surfaceflinger/ISurfaceComposer.h
+++ b/include/surfaceflinger/ISurfaceComposer.h
@@ -80,6 +80,7 @@
         eOrientation90          = 1,
         eOrientation180         = 2,
         eOrientation270         = 3,
+        eOrientationUnchanged   = 4,
         eOrientationSwapMask    = 0x01
     };
     
@@ -101,15 +102,8 @@
     virtual sp<IMemoryHeap> getCblk() const = 0;
 
     /* open/close transactions. requires ACCESS_SURFACE_FLINGER permission */
-    virtual void setTransactionState(const Vector<ComposerState>& state) = 0;
-
-    /* [un]freeze display. requires ACCESS_SURFACE_FLINGER permission */
-    virtual status_t freezeDisplay(DisplayID dpy, uint32_t flags) = 0;
-    virtual status_t unfreezeDisplay(DisplayID dpy, uint32_t flags) = 0;
-
-    /* Set display orientation. requires ACCESS_SURFACE_FLINGER permission
-     * No flags are currently defined.  Set flags to 0. */
-    virtual int setOrientation(DisplayID dpy, int orientation, uint32_t flags) = 0;
+    virtual void setTransactionState(const Vector<ComposerState>& state,
+            int orientation) = 0;
 
     /* signal that we're done booting.
      * Requires ACCESS_SURFACE_FLINGER permission
diff --git a/include/surfaceflinger/SurfaceComposerClient.h b/include/surfaceflinger/SurfaceComposerClient.h
index ace0735..14e5b23 100644
--- a/include/surfaceflinger/SurfaceComposerClient.h
+++ b/include/surfaceflinger/SurfaceComposerClient.h
@@ -195,4 +195,3 @@
 }; // namespace android
 
 #endif // ANDROID_SF_SURFACE_COMPOSER_CLIENT_H
-
diff --git a/libs/gui/ISurfaceComposer.cpp b/libs/gui/ISurfaceComposer.cpp
index 030a83e..eb90147 100644
--- a/libs/gui/ISurfaceComposer.cpp
+++ b/libs/gui/ISurfaceComposer.cpp
@@ -78,7 +78,8 @@
         return interface_cast<IMemoryHeap>(reply.readStrongBinder());
     }
 
-    virtual void setTransactionState(const Vector<ComposerState>& state)
+    virtual void setTransactionState(const Vector<ComposerState>& state,
+            int orientation)
     {
         Parcel data, reply;
         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
@@ -88,38 +89,8 @@
         for ( ; b != e ; ++b ) {
             b->write(data);
         }
-        remote()->transact(BnSurfaceComposer::SET_TRANSACTION_STATE, data, &reply);
-    }
-
-    virtual status_t freezeDisplay(DisplayID dpy, uint32_t flags)
-    {
-        Parcel data, reply;
-        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
-        data.writeInt32(dpy);
-        data.writeInt32(flags);
-        remote()->transact(BnSurfaceComposer::FREEZE_DISPLAY, data, &reply);
-        return reply.readInt32();
-    }
-
-    virtual status_t unfreezeDisplay(DisplayID dpy, uint32_t flags)
-    {
-        Parcel data, reply;
-        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
-        data.writeInt32(dpy);
-        data.writeInt32(flags);
-        remote()->transact(BnSurfaceComposer::UNFREEZE_DISPLAY, data, &reply);
-        return reply.readInt32();
-    }
-
-    virtual int setOrientation(DisplayID dpy, int orientation, uint32_t flags)
-    {
-        Parcel data, reply;
-        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
-        data.writeInt32(dpy);
         data.writeInt32(orientation);
-        data.writeInt32(flags);
-        remote()->transact(BnSurfaceComposer::SET_ORIENTATION, data, &reply);
-        return reply.readInt32();
+        remote()->transact(BnSurfaceComposer::SET_TRANSACTION_STATE, data, &reply);
     }
 
     virtual void bootFinished()
@@ -232,26 +203,8 @@
                 s.read(data);
                 state.add(s);
             }
-            setTransactionState(state);
-        } break;
-        case SET_ORIENTATION: {
-            CHECK_INTERFACE(ISurfaceComposer, data, reply);
-            DisplayID dpy = data.readInt32();
             int orientation = data.readInt32();
-            uint32_t flags = data.readInt32();
-            reply->writeInt32( setOrientation(dpy, orientation, flags) );
-        } break;
-        case FREEZE_DISPLAY: {
-            CHECK_INTERFACE(ISurfaceComposer, data, reply);
-            DisplayID dpy = data.readInt32();
-            uint32_t flags = data.readInt32();
-            reply->writeInt32( freezeDisplay(dpy, flags) );
-        } break;
-        case UNFREEZE_DISPLAY: {
-            CHECK_INTERFACE(ISurfaceComposer, data, reply);
-            DisplayID dpy = data.readInt32();
-            uint32_t flags = data.readInt32();
-            reply->writeInt32( unfreezeDisplay(dpy, flags) );
+            setTransactionState(state, orientation);
         } break;
         case BOOT_FINISHED: {
             CHECK_INTERFACE(ISurfaceComposer, data, reply);
diff --git a/libs/gui/ISurfaceTexture.cpp b/libs/gui/ISurfaceTexture.cpp
index babd2c0..d2e5627 100644
--- a/libs/gui/ISurfaceTexture.cpp
+++ b/libs/gui/ISurfaceTexture.cpp
@@ -58,13 +58,16 @@
         Parcel data, reply;
         data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor());
         data.writeInt32(bufferIdx);
-        remote()->transact(REQUEST_BUFFER, data, &reply);
+        status_t result =remote()->transact(REQUEST_BUFFER, data, &reply);
+        if (result != NO_ERROR) {
+            return result;
+        }
         bool nonNull = reply.readInt32();
         if (nonNull) {
             *buf = new GraphicBuffer();
             reply.read(**buf);
         }
-        status_t result = reply.readInt32();
+        result = reply.readInt32();
         return result;
     }
 
@@ -73,9 +76,12 @@
         Parcel data, reply;
         data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor());
         data.writeInt32(bufferCount);
-        remote()->transact(SET_BUFFER_COUNT, data, &reply);
-        status_t err = reply.readInt32();
-        return err;
+        status_t result =remote()->transact(SET_BUFFER_COUNT, data, &reply);
+        if (result != NO_ERROR) {
+            return result;
+        }
+        result = reply.readInt32();
+        return result;
     }
 
     virtual status_t dequeueBuffer(int *buf, uint32_t w, uint32_t h,
@@ -86,9 +92,12 @@
         data.writeInt32(h);
         data.writeInt32(format);
         data.writeInt32(usage);
-        remote()->transact(DEQUEUE_BUFFER, data, &reply);
+        status_t result = remote()->transact(DEQUEUE_BUFFER, data, &reply);
+        if (result != NO_ERROR) {
+            return result;
+        }
         *buf = reply.readInt32();
-        int result = reply.readInt32();
+        result = reply.readInt32();
         return result;
     }
 
@@ -98,11 +107,14 @@
         data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor());
         data.writeInt32(buf);
         data.writeInt64(timestamp);
-        remote()->transact(QUEUE_BUFFER, data, &reply);
+        status_t result = remote()->transact(QUEUE_BUFFER, data, &reply);
+        if (result != NO_ERROR) {
+            return result;
+        }
         *outWidth = reply.readInt32();
         *outHeight = reply.readInt32();
         *outTransform = reply.readInt32();
-        status_t result = reply.readInt32();
+        result = reply.readInt32();
         return result;
     }
 
@@ -120,8 +132,11 @@
         data.writeFloat(reg.top);
         data.writeFloat(reg.right);
         data.writeFloat(reg.bottom);
-        remote()->transact(SET_CROP, data, &reply);
-        status_t result = reply.readInt32();
+        status_t result = remote()->transact(SET_CROP, data, &reply);
+        if (result != NO_ERROR) {
+            return result;
+        }
+        result = reply.readInt32();
         return result;
     }
 
@@ -129,8 +144,11 @@
         Parcel data, reply;
         data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor());
         data.writeInt32(transform);
-        remote()->transact(SET_TRANSFORM, data, &reply);
-        status_t result = reply.readInt32();
+        status_t result = remote()->transact(SET_TRANSFORM, data, &reply);
+        if (result != NO_ERROR) {
+            return result;
+        }
+        result = reply.readInt32();
         return result;
     }
 
@@ -138,8 +156,11 @@
         Parcel data, reply;
         data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor());
         data.writeInt32(mode);
-        remote()->transact(SET_SCALING_MODE, data, &reply);
-        status_t result = reply.readInt32();
+        status_t result = remote()->transact(SET_SCALING_MODE, data, &reply);
+        if (result != NO_ERROR) {
+            return result;
+        }
+        result = reply.readInt32();
         return result;
     }
 
@@ -147,9 +168,12 @@
         Parcel data, reply;
         data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor());
         data.writeInt32(what);
-        remote()->transact(QUERY, data, &reply);
+        status_t result = remote()->transact(QUERY, data, &reply);
+        if (result != NO_ERROR) {
+            return result;
+        }
         value[0] = reply.readInt32();
-        status_t result = reply.readInt32();
+        result = reply.readInt32();
         return result;
     }
 
@@ -157,8 +181,11 @@
         Parcel data, reply;
         data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor());
         data.writeInt32(enabled);
-        remote()->transact(SET_SYNCHRONOUS_MODE, data, &reply);
-        status_t result = reply.readInt32();
+        status_t result = remote()->transact(SET_SYNCHRONOUS_MODE, data, &reply);
+        if (result != NO_ERROR) {
+            return result;
+        }
+        result = reply.readInt32();
         return result;
     }
 
@@ -167,11 +194,14 @@
         Parcel data, reply;
         data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor());
         data.writeInt32(api);
-        remote()->transact(CONNECT, data, &reply);
+        status_t result = remote()->transact(CONNECT, data, &reply);
+        if (result != NO_ERROR) {
+            return result;
+        }
         *outWidth = reply.readInt32();
         *outHeight = reply.readInt32();
         *outTransform = reply.readInt32();
-        status_t result = reply.readInt32();
+        result = reply.readInt32();
         return result;
     }
 
@@ -179,8 +209,11 @@
         Parcel data, reply;
         data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor());
         data.writeInt32(api);
-        remote()->transact(DISCONNECT, data, &reply);
-        status_t result = reply.readInt32();
+        status_t result =remote()->transact(DISCONNECT, data, &reply);
+        if (result != NO_ERROR) {
+            return result;
+        }
+        result = reply.readInt32();
         return result;
     }
 };
diff --git a/libs/gui/SensorManager.cpp b/libs/gui/SensorManager.cpp
index d719efb..dafcdea 100644
--- a/libs/gui/SensorManager.cpp
+++ b/libs/gui/SensorManager.cpp
@@ -23,6 +23,7 @@
 #include <utils/RefBase.h>
 #include <utils/Singleton.h>
 
+#include <binder/IBinder.h>
 #include <binder/IServiceManager.h>
 
 #include <gui/ISensorServer.h>
@@ -40,17 +41,8 @@
 SensorManager::SensorManager()
     : mSensorList(0)
 {
-    const String16 name("sensorservice");
-    while (getService(name, &mSensorServer) != NO_ERROR) {
-        usleep(250000);
-    }
-
-    mSensors = mSensorServer->getSensorList();
-    size_t count = mSensors.size();
-    mSensorList = (Sensor const**)malloc(count * sizeof(Sensor*));
-    for (size_t i=0 ; i<count ; i++) {
-        mSensorList[i] = mSensors.array() + i;
-    }
+    // okay we're not locked here, but it's not needed during construction
+    assertStateLocked();
 }
 
 SensorManager::~SensorManager()
@@ -58,29 +50,100 @@
     free(mSensorList);
 }
 
+void SensorManager::sensorManagerDied()
+{
+    Mutex::Autolock _l(mLock);
+    mSensorServer.clear();
+    free(mSensorList);
+    mSensorList = NULL;
+    mSensors.clear();
+}
+
+status_t SensorManager::assertStateLocked() const {
+    if (mSensorServer == NULL) {
+        // try for one second
+        const String16 name("sensorservice");
+        for (int i=0 ; i<4 ; i++) {
+            status_t err = getService(name, &mSensorServer);
+            if (err == NAME_NOT_FOUND) {
+                usleep(250000);
+                continue;
+            }
+            if (err != NO_ERROR) {
+                return err;
+            }
+            break;
+        }
+
+        class DeathObserver : public IBinder::DeathRecipient {
+            SensorManager& mSensorManger;
+            virtual void binderDied(const wp<IBinder>& who) {
+                LOGW("sensorservice died [%p]", who.unsafe_get());
+                mSensorManger.sensorManagerDied();
+            }
+        public:
+            DeathObserver(SensorManager& mgr) : mSensorManger(mgr) { }
+        };
+
+        mDeathObserver = new DeathObserver(*const_cast<SensorManager *>(this));
+        mSensorServer->asBinder()->linkToDeath(mDeathObserver);
+
+        mSensors = mSensorServer->getSensorList();
+        size_t count = mSensors.size();
+        mSensorList = (Sensor const**)malloc(count * sizeof(Sensor*));
+        for (size_t i=0 ; i<count ; i++) {
+            mSensorList[i] = mSensors.array() + i;
+        }
+    }
+
+    return NO_ERROR;
+}
+
+
+
 ssize_t SensorManager::getSensorList(Sensor const* const** list) const
 {
+    Mutex::Autolock _l(mLock);
+    status_t err = assertStateLocked();
+    if (err < 0) {
+        return ssize_t(err);
+    }
     *list = mSensorList;
     return mSensors.size();
 }
 
 Sensor const* SensorManager::getDefaultSensor(int type)
 {
-    // For now we just return the first sensor of that type we find.
-    // in the future it will make sense to let the SensorService make
-    // that decision.
-    for (size_t i=0 ; i<mSensors.size() ; i++) {
-        if (mSensorList[i]->getType() == type)
-            return mSensorList[i];
+    Mutex::Autolock _l(mLock);
+    if (assertStateLocked() == NO_ERROR) {
+        // For now we just return the first sensor of that type we find.
+        // in the future it will make sense to let the SensorService make
+        // that decision.
+        for (size_t i=0 ; i<mSensors.size() ; i++) {
+            if (mSensorList[i]->getType() == type)
+                return mSensorList[i];
+        }
     }
     return NULL;
 }
 
 sp<SensorEventQueue> SensorManager::createEventQueue()
 {
-    sp<SensorEventQueue> result = new SensorEventQueue(
-            mSensorServer->createSensorEventConnection());
-    return result;
+    sp<SensorEventQueue> queue;
+
+    Mutex::Autolock _l(mLock);
+    while (assertStateLocked() == NO_ERROR) {
+        sp<ISensorEventConnection> connection =
+                mSensorServer->createSensorEventConnection();
+        if (connection == NULL) {
+            // SensorService just died.
+            LOGE("createEventQueue: connection is NULL. SensorService died.");
+            continue;
+        }
+        queue = new SensorEventQueue(connection);
+        break;
+    }
+    return queue;
 }
 
 // ----------------------------------------------------------------------------
diff --git a/libs/gui/SurfaceComposerClient.cpp b/libs/gui/SurfaceComposerClient.cpp
index 00a4bf6..5f3d608 100644
--- a/libs/gui/SurfaceComposerClient.cpp
+++ b/libs/gui/SurfaceComposerClient.cpp
@@ -91,8 +91,10 @@
 
     mutable Mutex               mLock;
     SortedVector<ComposerState> mStates;
+    int                         mOrientation;
 
-    Composer() : Singleton<Composer>() { }
+    Composer() : Singleton<Composer>(),
+        mOrientation(ISurfaceComposer::eOrientationUnchanged) { }
 
     void closeGlobalTransactionImpl();
 
@@ -119,6 +121,7 @@
     status_t setFreezeTint(
             const sp<SurfaceComposerClient>& client, SurfaceID id,
             uint32_t tint);
+    status_t setOrientation(int orientation);
 
     static void closeGlobalTransaction() {
         Composer::getInstance().closeGlobalTransactionImpl();
@@ -133,14 +136,18 @@
     sp<ISurfaceComposer> sm(getComposerService());
 
     Vector<ComposerState> transaction;
+    int orientation;
 
     { // scope for the lock
         Mutex::Autolock _l(mLock);
         transaction = mStates;
         mStates.clear();
+
+        orientation = mOrientation;
+        mOrientation = ISurfaceComposer::eOrientationUnchanged;
     }
 
-   sm->setTransactionState(transaction);
+   sm->setTransactionState(transaction, orientation);
 }
 
 layer_state_t* Composer::getLayerStateLocked(
@@ -260,6 +267,12 @@
     return NO_ERROR;
 }
 
+status_t Composer::setOrientation(int orientation) {
+    Mutex::Autolock _l(mLock);
+    mOrientation = orientation;
+    return NO_ERROR;
+}
+
 // ---------------------------------------------------------------------------
 
 SurfaceComposerClient::SurfaceComposerClient()
@@ -427,6 +440,12 @@
     return getComposer().setMatrix(this, id, dsdx, dtdx, dsdy, dtdy);
 }
 
+status_t SurfaceComposerClient::setOrientation(DisplayID dpy,
+        int orientation, uint32_t flags)
+{
+    return Composer::getInstance().setOrientation(orientation);
+}
+
 // ----------------------------------------------------------------------------
 
 status_t SurfaceComposerClient::getDisplayInfo(
@@ -491,21 +510,14 @@
 
 status_t SurfaceComposerClient::freezeDisplay(DisplayID dpy, uint32_t flags)
 {
-    sp<ISurfaceComposer> sm(getComposerService());
-    return sm->freezeDisplay(dpy, flags);
+    // This has been made a no-op because it can cause Gralloc buffer deadlocks.
+    return NO_ERROR;
 }
 
 status_t SurfaceComposerClient::unfreezeDisplay(DisplayID dpy, uint32_t flags)
 {
-    sp<ISurfaceComposer> sm(getComposerService());
-    return sm->unfreezeDisplay(dpy, flags);
-}
-
-int SurfaceComposerClient::setOrientation(DisplayID dpy,
-        int orientation, uint32_t flags)
-{
-    sp<ISurfaceComposer> sm(getComposerService());
-    return sm->setOrientation(dpy, orientation, flags);
+    // This has been made a no-op because it can cause Gralloc buffer deadlocks.
+    return NO_ERROR;
 }
 
 // ----------------------------------------------------------------------------
@@ -572,4 +584,3 @@
 
 // ----------------------------------------------------------------------------
 }; // namespace android
-
diff --git a/libs/gui/tests/Surface_test.cpp b/libs/gui/tests/Surface_test.cpp
index ce587b3..693b7b8 100644
--- a/libs/gui/tests/Surface_test.cpp
+++ b/libs/gui/tests/Surface_test.cpp
@@ -75,7 +75,7 @@
 }
 
 // This test probably doesn't belong here.
-TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersFail) {
+TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersSucceed) {
     sp<ANativeWindow> anw(mSurface);
 
     // Verify the screenshot works with no protected buffers.
@@ -114,31 +114,8 @@
     }
     heap = 0;
     w = h = fmt = 0;
-    ASSERT_EQ(INVALID_OPERATION, sf->captureScreen(0, &heap, &w, &h, &fmt,
+    ASSERT_EQ(NO_ERROR, sf->captureScreen(0, &heap, &w, &h, &fmt,
             64, 64, 0, 0x7fffffff));
-    ASSERT_TRUE(heap == NULL);
-
-    // XXX: This should not be needed, but it seems that the new buffers don't
-    // correctly show up after the upcoming dequeue/lock/queue loop without it.
-    // We should look into this at some point.
-    ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
-
-    // Un-set the PROTECTED usage bit and verify that the screenshot works
-    // again.  Note that we have to change the buffers geometry to ensure that
-    // the buffers get reallocated, as the new usage bits are a subset of the
-    // old.
-    ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
-    ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(anw.get(), 32, 32, 0));
-    for (int i = 0; i < 4; i++) {
-        // Loop to make sure SurfaceFlinger has retired a protected buffer.
-        ASSERT_EQ(NO_ERROR, anw->dequeueBuffer(anw.get(), &buf));
-        ASSERT_EQ(NO_ERROR, anw->lockBuffer(anw.get(), buf));
-        ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf));
-    }
-    heap = 0;
-    w = h = fmt = 0;
-    ASSERT_EQ(NO_ERROR, sf->captureScreen(0, &heap, &w, &h, &fmt, 64, 64, 0,
-            0x7fffffff));
     ASSERT_TRUE(heap != NULL);
 }
 
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 195ad2e..7c0cd9b 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -451,7 +451,7 @@
     }
 
     const DisplayHardware& hw(graphicPlane(0).displayHardware());
-    if (LIKELY(hw.canDraw() && !isFrozen())) {
+    if (LIKELY(hw.canDraw())) {
         // repaint the framebuffer (if needed)
 
         const int index = hw.getCurrentBufferIndex();
@@ -478,15 +478,13 @@
 
 void SurfaceFlinger::postFramebuffer()
 {
-    if (!mSwapRegion.isEmpty()) {
-        const DisplayHardware& hw(graphicPlane(0).displayHardware());
-        const nsecs_t now = systemTime();
-        mDebugInSwapBuffers = now;
-        hw.flip(mSwapRegion);
-        mLastSwapBufferTime = systemTime() - now;
-        mDebugInSwapBuffers = 0;
-        mSwapRegion.clear();
-    }
+    const DisplayHardware& hw(graphicPlane(0).displayHardware());
+    const nsecs_t now = systemTime();
+    mDebugInSwapBuffers = now;
+    hw.flip(mSwapRegion);
+    mLastSwapBufferTime = systemTime() - now;
+    mDebugInSwapBuffers = 0;
+    mSwapRegion.clear();
 }
 
 void SurfaceFlinger::handleConsoleEvents()
@@ -1234,10 +1232,22 @@
 }
 
 
-void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state) {
+void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
+        int orientation) {
     Mutex::Autolock _l(mStateLock);
 
     uint32_t flags = 0;
+    if (mCurrentState.orientation != orientation) {
+        if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
+            mCurrentState.orientation = orientation;
+            flags |= eTransactionNeeded;
+            mResizeTransationPending = true;
+        } else if (orientation != eOrientationUnchanged) {
+            LOGW("setTransactionState: ignoring unrecognized orientation: %d",
+                    orientation);
+        }
+    }
+
     const size_t count = state.size();
     for (size_t i=0 ; i<count ; i++) {
         const ComposerState& s(state[i]);
diff --git a/services/surfaceflinger/SurfaceFlinger.h b/services/surfaceflinger/SurfaceFlinger.h
index 1cb9be2..0e642c1 100644
--- a/services/surfaceflinger/SurfaceFlinger.h
+++ b/services/surfaceflinger/SurfaceFlinger.h
@@ -167,7 +167,8 @@
     virtual sp<IGraphicBufferAlloc>     createGraphicBufferAlloc();
     virtual sp<IMemoryHeap>             getCblk() const;
     virtual void                        bootFinished();
-    virtual void                        setTransactionState(const Vector<ComposerState>& state);
+    virtual void                        setTransactionState(const Vector<ComposerState>& state,
+                                                            int orientation);
     virtual status_t                    freezeDisplay(DisplayID dpy, uint32_t flags);
     virtual status_t                    unfreezeDisplay(DisplayID dpy, uint32_t flags);
     virtual int                         setOrientation(DisplayID dpy, int orientation, uint32_t flags);