Merge "enable java/jni build in PDK if supported" into jb-mr1-dev
diff --git a/include/gui/BufferQueue.h b/include/gui/BufferQueue.h
index 0d36baa..85d8fb6 100644
--- a/include/gui/BufferQueue.h
+++ b/include/gui/BufferQueue.h
@@ -422,10 +422,6 @@
     // in requestBuffers() if a width and height of zero is specified.
     uint32_t mDefaultHeight;
 
-    // mPixelFormat holds the pixel format of allocated buffers. It is used
-    // in requestBuffers() if a format of zero is specified.
-    uint32_t mPixelFormat;
-
     // mMinUndequeuedBuffers is a constraint on the number of buffers
     // not dequeued at any time
     int mMinUndequeuedBuffers;
diff --git a/include/gui/ConsumerBase.h b/include/gui/ConsumerBase.h
new file mode 100644
index 0000000..d2bf0f6
--- /dev/null
+++ b/include/gui/ConsumerBase.h
@@ -0,0 +1,209 @@
+/*
+ * Copyright (C) 2010 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_GUI_CONSUMERBASE_H
+#define ANDROID_GUI_CONSUMERBASE_H
+
+#include <gui/BufferQueue.h>
+
+#include <ui/GraphicBuffer.h>
+
+#include <utils/String8.h>
+#include <utils/Vector.h>
+#include <utils/threads.h>
+
+namespace android {
+// ----------------------------------------------------------------------------
+
+class String8;
+
+// ConsumerBase is a base class for BufferQueue consumer end-points. It
+// handles common tasks like management of the connection to the BufferQueue
+// and the buffer pool.
+class ConsumerBase : public virtual RefBase,
+        protected BufferQueue::ConsumerListener {
+public:
+    struct FrameAvailableListener : public virtual RefBase {
+        // onFrameAvailable() is called each time an additional frame becomes
+        // available for consumption. This means that frames that are queued
+        // while in asynchronous mode only trigger the callback if no previous
+        // frames are pending. Frames queued while in synchronous mode always
+        // trigger the callback.
+        //
+        // This is called without any lock held and can be called concurrently
+        // by multiple threads.
+        virtual void onFrameAvailable() = 0;
+    };
+
+    virtual ~ConsumerBase();
+
+    // abandon frees all the buffers and puts the ConsumerBase into the
+    // 'abandoned' state.  Once put in this state the ConsumerBase can never
+    // leave it.  When in the 'abandoned' state, all methods of the
+    // ISurfaceTexture interface will fail with the NO_INIT error.
+    //
+    // Note that while calling this method causes all the buffers to be freed
+    // from the perspective of the the ConsumerBase, if there are additional
+    // references on the buffers (e.g. if a buffer is referenced by a client
+    // or by OpenGL ES as a texture) then those buffer will remain allocated.
+    void abandon();
+
+    // set the name of the ConsumerBase that will be used to identify it in
+    // log messages.
+    void setName(const String8& name);
+
+    // getBufferQueue returns the BufferQueue object to which this
+    // ConsumerBase is connected.
+    sp<BufferQueue> getBufferQueue() const;
+
+    // dump writes the current state to a string.  These methods should NOT be
+    // overridden by child classes.  Instead they should override the
+    // dumpLocked method, which is called by these methods after locking the
+    // mutex.
+    void dump(String8& result) const;
+    void dump(String8& result, const char* prefix, char* buffer, size_t SIZE) const;
+
+    // setFrameAvailableListener sets the listener object that will be notified
+    // when a new frame becomes available.
+    void setFrameAvailableListener(const sp<FrameAvailableListener>& listener);
+
+private:
+    ConsumerBase(const ConsumerBase&);
+    void operator=(const ConsumerBase&);
+
+protected:
+
+    // TODO: Fix this comment
+    // ConsumerBase constructs a new ConsumerBase object. tex indicates the
+    // name of the OpenGL ES texture to which images are to be streamed.
+    // allowSynchronousMode specifies whether or not synchronous mode can be
+    // enabled. texTarget specifies the OpenGL ES texture target to which the
+    // texture will be bound in updateTexImage. useFenceSync specifies whether
+    // fences should be used to synchronize access to buffers if that behavior
+    // is enabled at compile-time. A custom bufferQueue can be specified
+    // if behavior for queue/dequeue/connect etc needs to be customized.
+    // Otherwise a default BufferQueue will be created and used.
+    //
+    // For legacy reasons, the ConsumerBase is created in a state where it is
+    // considered attached to an OpenGL ES context for the purposes of the
+    // attachToContext and detachFromContext methods. However, despite being
+    // considered "attached" to a context, the specific OpenGL ES context
+    // doesn't get latched until the first call to updateTexImage. After that
+    // point, all calls to updateTexImage must be made with the same OpenGL ES
+    // context current.
+    //
+    // A ConsumerBase may be detached from one OpenGL ES context and then
+    // attached to a different context using the detachFromContext and
+    // attachToContext methods, respectively. The intention of these methods is
+    // purely to allow a ConsumerBase to be transferred from one consumer
+    // context to another. If such a transfer is not needed there is no
+    // requirement that either of these methods be called.
+    ConsumerBase(const sp<BufferQueue> &bufferQueue);
+
+    // Implementation of the BufferQueue::ConsumerListener interface.  These
+    // calls are used to notify the ConsumerBase of asynchronous events in the
+    // BufferQueue.
+    virtual void onFrameAvailable();
+    virtual void onBuffersReleased();
+
+    // freeBufferLocked frees up the given buffer slot.  If the slot has been
+    // initialized this will release the reference to the GraphicBuffer in that
+    // slot and destroy the EGLImage in that slot.  Otherwise it has no effect.
+    //
+    // This method must be called with mMutex locked.
+    virtual void freeBufferLocked(int slotIndex);
+
+    // abandonLocked puts the BufferQueue into the abandoned state, causing
+    // all future operations on it to fail. This method rather than the public
+    // abandon method should be overridden by child classes to add abandon-
+    // time behavior.
+    //
+    // This method must be called with mMutex locked.
+    virtual void abandonLocked();
+
+    virtual void dumpLocked(String8& result, const char* prefix, char* buffer,
+            size_t SIZE) const;
+
+    // acquireBufferLocked fetches the next buffer from the BufferQueue and
+    // updates the buffer slot for the buffer returned.
+    virtual status_t acquireBufferLocked(BufferQueue::BufferItem *item);
+
+    // releaseBufferLocked relinquishes control over a buffer, returning that
+    // control to the BufferQueue.
+    virtual status_t releaseBufferLocked(int buf, EGLDisplay display,
+           EGLSyncKHR eglFence, const sp<Fence>& fence);
+
+    // Slot contains the information and object references that
+    // ConsumerBase maintains about a BufferQueue buffer slot.
+    struct Slot {
+        // mGraphicBuffer is the Gralloc buffer store in the slot or NULL if
+        // no Gralloc buffer is in the slot.
+        sp<GraphicBuffer> mGraphicBuffer;
+
+        // mFence is a fence which will signal when the buffer associated with
+        // this buffer slot is no longer being used by the consumer and can be
+        // overwritten. The buffer can be dequeued before the fence signals;
+        // the producer is responsible for delaying writes until it signals.
+        sp<Fence> mFence;
+    };
+
+    // mSlots stores the buffers that have been allocated by the BufferQueue
+    // for each buffer slot.  It is initialized to null pointers, and gets
+    // filled in with the result of BufferQueue::acquire when the
+    // client dequeues a buffer from a
+    // slot that has not yet been used. The buffer allocated to a slot will also
+    // be replaced if the requested buffer usage or geometry differs from that
+    // of the buffer allocated to a slot.
+    Slot mSlots[BufferQueue::NUM_BUFFER_SLOTS];
+
+    // mAbandoned indicates that the BufferQueue will no longer be used to
+    // consume images buffers pushed to it using the ISurfaceTexture
+    // interface. It is initialized to false, and set to true in the abandon
+    // method.  A BufferQueue that has been abandoned will return the NO_INIT
+    // error from all IConsumerBase methods capable of returning an error.
+    bool mAbandoned;
+
+    // mName is a string used to identify the ConsumerBase in log messages.
+    // It can be set by the setName method.
+    String8 mName;
+
+    // mFrameAvailableListener is the listener object that will be called when a
+    // new frame becomes available. If it is not NULL it will be called from
+    // queueBuffer.
+    sp<FrameAvailableListener> mFrameAvailableListener;
+
+    // The ConsumerBase has-a BufferQueue and is responsible for creating this object
+    // if none is supplied
+    sp<BufferQueue> mBufferQueue;
+
+    // mAttached indicates whether the ConsumerBase is currently attached to
+    // an OpenGL ES context.  For legacy reasons, this is initialized to true,
+    // indicating that the ConsumerBase is considered to be attached to
+    // whatever context is current at the time of the first updateTexImage call.
+    // It is set to false by detachFromContext, and then set to true again by
+    // attachToContext.
+    bool mAttached;
+
+    // mMutex is the mutex used to prevent concurrent access to the member
+    // variables of ConsumerBase objects. It must be locked whenever the
+    // member variables are accessed.
+    mutable Mutex mMutex;
+};
+
+// ----------------------------------------------------------------------------
+}; // namespace android
+
+#endif // ANDROID_GUI_CONSUMERBASE_H
diff --git a/include/gui/ISurfaceComposer.h b/include/gui/ISurfaceComposer.h
index 315a39b..82a5956 100644
--- a/include/gui/ISurfaceComposer.h
+++ b/include/gui/ISurfaceComposer.h
@@ -89,11 +89,6 @@
         eSynchronous            = 0x01,
     };
 
-    enum {
-        eElectronBeamAnimationOn  = 0x01,
-        eElectronBeamAnimationOff = 0x10
-    };
-
     /* create connection with surface flinger, requires
      * ACCESS_SURFACE_FLINGER permission
      */
@@ -123,12 +118,6 @@
             uint32_t reqWidth, uint32_t reqHeight,
             uint32_t minLayerZ, uint32_t maxLayerZ) = 0;
 
-    /* triggers screen off animation */
-    virtual status_t turnElectronBeamOff(int32_t mode) = 0;
-
-    /* triggers screen on animation */
-    virtual status_t turnElectronBeamOn(int32_t mode) = 0;
-
     /* verify that an ISurfaceTexture was created by SurfaceFlinger.
      */
     virtual bool authenticateSurfaceTexture(
@@ -166,8 +155,6 @@
         SET_TRANSACTION_STATE,
         SET_ORIENTATION,
         CAPTURE_SCREEN,
-        TURN_ELECTRON_BEAM_OFF,
-        TURN_ELECTRON_BEAM_ON,
         AUTHENTICATE_SURFACE,
         CREATE_DISPLAY_EVENT_CONNECTION,
         BLANK,
diff --git a/include/utils/KeyedVector.h b/include/utils/KeyedVector.h
index 20575ee..47c2c56 100644
--- a/include/utils/KeyedVector.h
+++ b/include/utils/KeyedVector.h
@@ -56,7 +56,10 @@
     inline  size_t          capacity() const            { return mVector.capacity(); }
     //! setst the capacity. capacity can never be reduced less than size()
     inline ssize_t          setCapacity(size_t size)    { return mVector.setCapacity(size); }
-    
+
+    // returns true if the arguments is known to be identical to this vector
+    inline bool isIdenticalTo(const KeyedVector& rhs) const;
+
     /*! 
      * accessors
      */
@@ -64,6 +67,7 @@
             const VALUE&    valueAt(size_t index) const;
             const KEY&      keyAt(size_t index) const;
             ssize_t         indexOfKey(const KEY& key) const;
+            const VALUE&    operator[] (size_t index) const;
 
     /*!
      * modifying the array
@@ -123,6 +127,11 @@
 }
 
 template<typename KEY, typename VALUE> inline
+bool KeyedVector<KEY,VALUE>::isIdenticalTo(const KeyedVector<KEY,VALUE>& rhs) const {
+    return mVector.array() == rhs.mVector.array();
+}
+
+template<typename KEY, typename VALUE> inline
 ssize_t KeyedVector<KEY,VALUE>::indexOfKey(const KEY& key) const {
     return mVector.indexOf( key_value_pair_t<KEY,VALUE>(key) );
 }
@@ -140,6 +149,11 @@
 }
 
 template<typename KEY, typename VALUE> inline
+const VALUE& KeyedVector<KEY,VALUE>::operator[] (size_t index) const {
+    return valueAt(index);
+}
+
+template<typename KEY, typename VALUE> inline
 const KEY& KeyedVector<KEY,VALUE>::keyAt(size_t index) const {
     return mVector.itemAt(index).key;
 }
diff --git a/include/utils/misc.h b/include/utils/misc.h
index d7d5bc1..f1aa432 100644
--- a/include/utils/misc.h
+++ b/include/utils/misc.h
@@ -23,41 +23,12 @@
 #include <sys/time.h>
 #include <utils/Endian.h>
 
-namespace android {
-
 /* get #of elements in a static array */
 #ifndef NELEM
 # define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
 #endif
 
-/*
- * Make a copy of the string, using "new[]" instead of "malloc".  Free the
- * string with delete[].
- *
- * Returns NULL if "str" is NULL.
- */
-char* strdupNew(const char* str);
-
-/*
- * Concatenate an argument vector into a single string.  If argc is >= 0
- * it will be used; if it's < 0 then the last element in the arg vector
- * must be NULL.
- *
- * This inserts a space between each argument.
- *
- * This does not automatically add double quotes around arguments with
- * spaces in them.  This practice is necessary for Win32, because Win32's
- * CreateProcess call is stupid.
- *
- * The caller should delete[] the returned string.
- */
-char* concatArgv(int argc, const char* const argv[]);
-
-/*
- * Count up the number of arguments in "argv".  The count does not include
- * the final NULL entry.
- */
-int countArgv(const char* const argv[]);
+namespace android {
 
 /*
  * Some utility functions for working with files.  These could be made
@@ -79,15 +50,6 @@
 /* get the file's modification date; returns -1 w/errno set on failure */
 time_t getFileModDate(const char* fileName);
 
-/*
- * Round up to the nearest power of 2.  Handy for hash tables.
- */
-unsigned int roundUpPower2(unsigned int val);
-
-void strreverse(char* begin, char* end);
-void k_itoa(int value, char* str, int base);
-char* itoa(int val, int base);
-
 typedef void (*sysprop_change_callback)(void);
 void add_sysprop_change_callback(sysprop_change_callback cb, int priority);
 void report_sysprop_change();
diff --git a/libs/gui/Android.mk b/libs/gui/Android.mk
index 4985765..3aa3a50 100644
--- a/libs/gui/Android.mk
+++ b/libs/gui/Android.mk
@@ -4,6 +4,7 @@
 LOCAL_SRC_FILES:= \
 	BitTube.cpp \
 	BufferQueue.cpp \
+	ConsumerBase.cpp \
 	DisplayEventReceiver.cpp \
 	IDisplayEventConnection.cpp \
 	ISensorEventConnection.cpp \
@@ -29,8 +30,6 @@
 	libcutils \
 	libEGL \
 	libGLESv2 \
-	libhardware \
-	libhardware_legacy \
 	libsync \
 	libui \
 	libutils \
diff --git a/libs/gui/BufferQueue.cpp b/libs/gui/BufferQueue.cpp
index 23e3a4f..697635b 100644
--- a/libs/gui/BufferQueue.cpp
+++ b/libs/gui/BufferQueue.cpp
@@ -85,7 +85,6 @@
         const sp<IGraphicBufferAlloc>& allocator) :
     mDefaultWidth(1),
     mDefaultHeight(1),
-    mPixelFormat(PIXEL_FORMAT_RGBA_8888),
     mMinUndequeuedBuffers(bufferCount),
     mMinAsyncBufferSlots(bufferCount + 1),
     mMinSyncBufferSlots(bufferCount),
@@ -98,7 +97,7 @@
     mAbandoned(false),
     mFrameCounter(0),
     mBufferHasBeenQueued(false),
-    mDefaultBufferFormat(0),
+    mDefaultBufferFormat(PIXEL_FORMAT_RGBA_8888),
     mConsumerUsageBits(0),
     mTransformHint(0)
 {
@@ -125,7 +124,8 @@
     if (bufferCount > NUM_BUFFER_SLOTS)
         return BAD_VALUE;
 
-    // special-case, nothing to do
+    mServerBufferCount = bufferCount;
+
     if (bufferCount == mBufferCount)
         return OK;
 
@@ -133,7 +133,6 @@
         bufferCount >= mBufferCount) {
         // easy, we just have more buffers
         mBufferCount = bufferCount;
-        mServerBufferCount = bufferCount;
         mDequeueCondition.broadcast();
     } else {
         // we're here because we're either
@@ -150,7 +149,6 @@
         // own one. the actual resizing will happen during the next
         // dequeueBuffer.
 
-        mServerBufferCount = bufferCount;
         mDequeueCondition.broadcast();
     }
     return OK;
@@ -260,7 +258,7 @@
         value = mDefaultHeight;
         break;
     case NATIVE_WINDOW_FORMAT:
-        value = mPixelFormat;
+        value = mDefaultBufferFormat;
         break;
     case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
         value = mSynchronousMode ?
@@ -447,12 +445,6 @@
             h = mDefaultHeight;
         }
 
-        const bool updateFormat = (format != 0);
-        if (!updateFormat) {
-            // keep the current (or default) format
-            format = mPixelFormat;
-        }
-
         // buffer is now in DEQUEUED (but can also be current at the same time,
         // if we're in synchronous mode)
         mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
@@ -473,9 +465,6 @@
                         "failed");
                 return error;
             }
-            if (updateFormat) {
-                mPixelFormat = format;
-            }
 
             mSlots[buf].mAcquireCalled = false;
             mSlots[buf].mGraphicBuffer = graphicBuffer;
@@ -791,9 +780,9 @@
 
     snprintf(buffer, SIZE,
             "%s-BufferQueue mBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
-            "mPixelFormat=%d, FIFO(%d)={%s}\n",
+            "default-format=%d, FIFO(%d)={%s}\n",
             prefix, mBufferCount, mSynchronousMode, mDefaultWidth,
-            mDefaultHeight, mPixelFormat, fifoSize, fifo.string());
+            mDefaultHeight, mDefaultBufferFormat, fifoSize, fifo.string());
     result.append(buffer);
 
 
@@ -835,21 +824,22 @@
     }
 }
 
-void BufferQueue::freeBufferLocked(int i) {
-    mSlots[i].mGraphicBuffer = 0;
-    if (mSlots[i].mBufferState == BufferSlot::ACQUIRED) {
-        mSlots[i].mNeedsCleanupOnRelease = true;
+void BufferQueue::freeBufferLocked(int slot) {
+    ST_LOGV("freeBufferLocked: slot=%d", slot);
+    mSlots[slot].mGraphicBuffer = 0;
+    if (mSlots[slot].mBufferState == BufferSlot::ACQUIRED) {
+        mSlots[slot].mNeedsCleanupOnRelease = true;
     }
-    mSlots[i].mBufferState = BufferSlot::FREE;
-    mSlots[i].mFrameNumber = 0;
-    mSlots[i].mAcquireCalled = false;
+    mSlots[slot].mBufferState = BufferSlot::FREE;
+    mSlots[slot].mFrameNumber = 0;
+    mSlots[slot].mAcquireCalled = false;
 
     // destroy fence as BufferQueue now takes ownership
-    if (mSlots[i].mEglFence != EGL_NO_SYNC_KHR) {
-        eglDestroySyncKHR(mSlots[i].mEglDisplay, mSlots[i].mEglFence);
-        mSlots[i].mEglFence = EGL_NO_SYNC_KHR;
+    if (mSlots[slot].mEglFence != EGL_NO_SYNC_KHR) {
+        eglDestroySyncKHR(mSlots[slot].mEglDisplay, mSlots[slot].mEglFence);
+        mSlots[slot].mEglFence = EGL_NO_SYNC_KHR;
     }
-    mSlots[i].mFence.clear();
+    mSlots[slot].mFence.clear();
 }
 
 void BufferQueue::freeAllBuffersLocked() {
@@ -886,12 +876,14 @@
         buffer->mTimestamp = mSlots[buf].mTimestamp;
         buffer->mBuf = buf;
         buffer->mFence = mSlots[buf].mFence;
-        mSlots[buf].mAcquireCalled = true;
 
+        mSlots[buf].mAcquireCalled = true;
+        mSlots[buf].mNeedsCleanupOnRelease = false;
         mSlots[buf].mBufferState = BufferSlot::ACQUIRED;
+        mSlots[buf].mFence.clear();
+
         mQueue.erase(front);
         mDequeueCondition.broadcast();
-        mSlots[buf].mFence.clear();
 
         ATRACE_INT(mConsumerName.string(), mQueue.size());
     } else {
diff --git a/libs/gui/ConsumerBase.cpp b/libs/gui/ConsumerBase.cpp
new file mode 100644
index 0000000..af19ac0
--- /dev/null
+++ b/libs/gui/ConsumerBase.cpp
@@ -0,0 +1,205 @@
+/*
+ * Copyright (C) 2010 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 "ConsumerBase"
+#define ATRACE_TAG ATRACE_TAG_GRAPHICS
+//#define LOG_NDEBUG 0
+
+#define GL_GLEXT_PROTOTYPES
+#define EGL_EGLEXT_PROTOTYPES
+
+#include <EGL/egl.h>
+#include <EGL/eglext.h>
+
+#include <hardware/hardware.h>
+
+#include <gui/IGraphicBufferAlloc.h>
+#include <gui/ISurfaceComposer.h>
+#include <gui/SurfaceComposerClient.h>
+#include <gui/ConsumerBase.h>
+
+#include <private/gui/ComposerService.h>
+
+#include <utils/Log.h>
+#include <utils/String8.h>
+#include <utils/Trace.h>
+
+// Macros for including the ConsumerBase name in log messages
+#define CB_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
+#define CB_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
+#define CB_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
+#define CB_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
+#define CB_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
+
+namespace android {
+
+// Get an ID that's unique within this process.
+static int32_t createProcessUniqueId() {
+    static volatile int32_t globalCounter = 0;
+    return android_atomic_inc(&globalCounter);
+}
+
+ConsumerBase::ConsumerBase(const sp<BufferQueue>& bufferQueue) :
+	mBufferQueue(bufferQueue) {
+    // Choose a name using the PID and a process-unique ID.
+    mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
+
+    // Note that we can't create an sp<...>(this) in a ctor that will not keep a
+    // reference once the ctor ends, as that would cause the refcount of 'this'
+    // dropping to 0 at the end of the ctor.  Since all we need is a wp<...>
+    // that's what we create.
+    wp<BufferQueue::ConsumerListener> listener;
+    sp<BufferQueue::ConsumerListener> proxy;
+    listener = static_cast<BufferQueue::ConsumerListener*>(this);
+    proxy = new BufferQueue::ProxyConsumerListener(listener);
+
+    status_t err = mBufferQueue->consumerConnect(proxy);
+    if (err != NO_ERROR) {
+        CB_LOGE("SurfaceTexture: error connecting to BufferQueue: %s (%d)",
+                strerror(-err), err);
+    } else {
+        mBufferQueue->setConsumerName(mName);
+    }
+}
+
+ConsumerBase::~ConsumerBase() {
+	CB_LOGV("~ConsumerBase");
+    abandon();
+}
+
+void ConsumerBase::freeBufferLocked(int slotIndex) {
+    CB_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
+    mSlots[slotIndex].mGraphicBuffer = 0;
+    mSlots[slotIndex].mFence = 0;
+}
+
+// Used for refactoring, should not be in final interface
+sp<BufferQueue> ConsumerBase::getBufferQueue() const {
+    Mutex::Autolock lock(mMutex);
+    return mBufferQueue;
+}
+
+void ConsumerBase::onFrameAvailable() {
+    CB_LOGV("onFrameAvailable");
+
+    sp<FrameAvailableListener> listener;
+    { // scope for the lock
+        Mutex::Autolock lock(mMutex);
+        listener = mFrameAvailableListener;
+    }
+
+    if (listener != NULL) {
+        CB_LOGV("actually calling onFrameAvailable");
+        listener->onFrameAvailable();
+    }
+}
+
+void ConsumerBase::onBuffersReleased() {
+    Mutex::Autolock lock(mMutex);
+
+    CB_LOGV("onBuffersReleased");
+
+    if (mAbandoned) {
+        // Nothing to do if we're already abandoned.
+        return;
+    }
+
+    uint32_t mask = 0;
+    mBufferQueue->getReleasedBuffers(&mask);
+    for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
+        if (mask & (1 << i)) {
+            freeBufferLocked(i);
+        }
+    }
+}
+
+void ConsumerBase::abandon() {
+    CB_LOGV("abandon");
+    Mutex::Autolock lock(mMutex);
+
+    if (!mAbandoned) {
+        abandonLocked();
+        mAbandoned = true;
+    }
+}
+
+void ConsumerBase::abandonLocked() {
+	CB_LOGV("abandonLocked");
+    for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
+        freeBufferLocked(i);
+    }
+    // disconnect from the BufferQueue
+    mBufferQueue->consumerDisconnect();
+    mBufferQueue.clear();
+}
+
+void ConsumerBase::setFrameAvailableListener(
+        const sp<FrameAvailableListener>& listener) {
+    CB_LOGV("setFrameAvailableListener");
+    Mutex::Autolock lock(mMutex);
+    mFrameAvailableListener = listener;
+}
+
+void ConsumerBase::dump(String8& result) const {
+    char buffer[1024];
+    dump(result, "", buffer, 1024);
+}
+
+void ConsumerBase::dump(String8& result, const char* prefix,
+        char* buffer, size_t size) const {
+    Mutex::Autolock _l(mMutex);
+    dumpLocked(result, prefix, buffer, size);
+}
+
+void ConsumerBase::dumpLocked(String8& result, const char* prefix,
+        char* buffer, size_t SIZE) const {
+    snprintf(buffer, SIZE, "%smAbandoned=%d\n", prefix, int(mAbandoned));
+    result.append(buffer);
+
+    if (!mAbandoned) {
+        mBufferQueue->dump(result, prefix, buffer, SIZE);
+    }
+}
+
+status_t ConsumerBase::acquireBufferLocked(BufferQueue::BufferItem *item) {
+    status_t err = mBufferQueue->acquireBuffer(item);
+    if (err != NO_ERROR) {
+        return err;
+    }
+
+    if (item->mGraphicBuffer != NULL) {
+        mSlots[item->mBuf].mGraphicBuffer = item->mGraphicBuffer;
+    }
+
+    CB_LOGV("acquireBufferLocked: -> slot=%d", item->mBuf);
+
+    return OK;
+}
+
+status_t ConsumerBase::releaseBufferLocked(int slot, EGLDisplay display,
+       EGLSyncKHR eglFence, const sp<Fence>& fence) {
+    CB_LOGV("releaseBufferLocked: slot=%d", slot);
+    status_t err = mBufferQueue->releaseBuffer(slot, display, eglFence, fence);
+    if (err == BufferQueue::STALE_BUFFER_SLOT) {
+        freeBufferLocked(slot);
+    }
+
+    mSlots[slot].mFence.clear();
+
+    return err;
+}
+
+} // namespace android
\ No newline at end of file
diff --git a/libs/gui/ISurfaceComposer.cpp b/libs/gui/ISurfaceComposer.cpp
index 4cc0262..5d8f1a1 100644
--- a/libs/gui/ISurfaceComposer.cpp
+++ b/libs/gui/ISurfaceComposer.cpp
@@ -123,24 +123,6 @@
         return reply.readInt32();
     }
 
-    virtual status_t turnElectronBeamOff(int32_t mode)
-    {
-        Parcel data, reply;
-        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
-        data.writeInt32(mode);
-        remote()->transact(BnSurfaceComposer::TURN_ELECTRON_BEAM_OFF, data, &reply);
-        return reply.readInt32();
-    }
-
-    virtual status_t turnElectronBeamOn(int32_t mode)
-    {
-        Parcel data, reply;
-        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
-        data.writeInt32(mode);
-        remote()->transact(BnSurfaceComposer::TURN_ELECTRON_BEAM_ON, data, &reply);
-        return reply.readInt32();
-    }
-
     virtual bool authenticateSurfaceTexture(
             const sp<ISurfaceTexture>& surfaceTexture) const
     {
@@ -291,18 +273,6 @@
             reply->writeInt32(f);
             reply->writeInt32(res);
         } break;
-        case TURN_ELECTRON_BEAM_OFF: {
-            CHECK_INTERFACE(ISurfaceComposer, data, reply);
-            int32_t mode = data.readInt32();
-            status_t res = turnElectronBeamOff(mode);
-            reply->writeInt32(res);
-        } break;
-        case TURN_ELECTRON_BEAM_ON: {
-            CHECK_INTERFACE(ISurfaceComposer, data, reply);
-            int32_t mode = data.readInt32();
-            status_t res = turnElectronBeamOn(mode);
-            reply->writeInt32(res);
-        } break;
         case AUTHENTICATE_SURFACE: {
             CHECK_INTERFACE(ISurfaceComposer, data, reply);
             sp<ISurfaceTexture> surfaceTexture =
diff --git a/libs/gui/SurfaceTexture.cpp b/libs/gui/SurfaceTexture.cpp
index 300b8d7..a2aca8f 100644
--- a/libs/gui/SurfaceTexture.cpp
+++ b/libs/gui/SurfaceTexture.cpp
@@ -26,8 +26,6 @@
 #include <GLES2/gl2.h>
 #include <GLES2/gl2ext.h>
 
-#include <hardware/hardware.h>
-
 #include <gui/IGraphicBufferAlloc.h>
 #include <gui/ISurfaceComposer.h>
 #include <gui/SurfaceComposerClient.h>
@@ -318,7 +316,7 @@
         computeCurrentTransformMatrix();
     } else  {
         if (err < 0) {
-            ALOGE("updateTexImage failed on acquire %d", err);
+            ST_LOGE("updateTexImage failed on acquire %d", err);
         }
         // We always bind the texture even if we don't update its contents.
         glBindTexture(mTexTarget, mTexName);
@@ -329,7 +327,7 @@
 }
 
 void SurfaceTexture::setReleaseFence(int fenceFd) {
-    if (fenceFd == -1)
+    if (fenceFd == -1 || mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT)
         return;
     sp<Fence> fence(new Fence(fenceFd));
     if (!mEGLSlots[mCurrentTexture].mReleaseFence.get()) {
@@ -339,7 +337,7 @@
                 String8("SurfaceTexture merged release"),
                 mEGLSlots[mCurrentTexture].mReleaseFence, fence);
         if (!mergedFence.get()) {
-            ALOGE("failed to merge release fences");
+            ST_LOGE("failed to merge release fences");
             // synchronization is broken, the best we can do is hope fences
             // signal in order so the new fence will act like a union
             mEGLSlots[mCurrentTexture].mReleaseFence = fence;
diff --git a/libs/utils/ZipFileRO.cpp b/libs/utils/ZipFileRO.cpp
index db17546..cc62213 100644
--- a/libs/utils/ZipFileRO.cpp
+++ b/libs/utils/ZipFileRO.cpp
@@ -320,6 +320,25 @@
     return true;
 }
 
+
+/*
+ * Round up to the next highest power of 2.
+ *
+ * Found on http://graphics.stanford.edu/~seander/bithacks.html.
+ */
+static unsigned int roundUpPower2(unsigned int val)
+{
+    val--;
+    val |= val >> 1;
+    val |= val >> 2;
+    val |= val >> 4;
+    val |= val >> 8;
+    val |= val >> 16;
+    val++;
+
+    return val;
+}
+
 bool ZipFileRO::parseZipArchive(void)
 {
     bool result = false;
diff --git a/libs/utils/misc.cpp b/libs/utils/misc.cpp
index b3c99e6..445a23a 100644
--- a/libs/utils/misc.cpp
+++ b/libs/utils/misc.cpp
@@ -39,90 +39,6 @@
 namespace android {
 
 /*
- * Like strdup(), but uses C++ "new" operator instead of malloc.
- */
-char* strdupNew(const char* str)
-{
-    char* newStr;
-    int len;
-
-    if (str == NULL)
-        return NULL;
-
-    len = strlen(str);
-    newStr = new char[len+1];
-    memcpy(newStr, str, len+1);
-
-    return newStr;
-}
-
-/*
- * Concatenate an argument vector.
- */
-char* concatArgv(int argc, const char* const argv[])
-{
-    char* newStr = NULL;
-    int len, totalLen, posn, idx;
-
-    /*
-     * First, figure out the total length.
-     */
-    totalLen = idx = 0;
-    while (1) {
-        if (idx == argc || argv[idx] == NULL)
-            break;
-        if (idx)
-            totalLen++;  // leave a space between args
-        totalLen += strlen(argv[idx]);
-        idx++;
-    }
-
-    /*
-     * Alloc the string.
-     */
-    newStr = new char[totalLen +1];
-    if (newStr == NULL)
-        return NULL;
-
-    /*
-     * Finally, allocate the string and copy data over.
-     */
-    idx = posn = 0;
-    while (1) {
-        if (idx == argc || argv[idx] == NULL)
-            break;
-        if (idx)
-            newStr[posn++] = ' ';
-
-        len = strlen(argv[idx]);
-        memcpy(&newStr[posn], argv[idx], len);
-        posn += len;
-
-        idx++;
-    }
-
-    assert(posn == totalLen);
-    newStr[posn] = '\0';
-
-    return newStr;
-}
-
-/*
- * Count the #of args in an argument vector.  Don't count the final NULL.
- */
-int countArgv(const char* const argv[])
-{
-    int count = 0;
-
-    while (argv[count] != NULL)
-        count++;
-
-    return count;
-}
-
-
-#include <stdio.h>
-/*
  * Get a file's type.
  */
 FileType getFileType(const char* fileName)
@@ -172,24 +88,6 @@
     return sb.st_mtime;
 }
 
-/*
- * Round up to the next highest power of 2.
- *
- * Found on http://graphics.stanford.edu/~seander/bithacks.html.
- */
-unsigned int roundUpPower2(unsigned int val)
-{
-    val--;
-    val |= val >> 1;
-    val |= val >> 2;
-    val |= val >> 4;
-    val |= val >> 8;
-    val |= val >> 16;
-    val++;
-
-    return val;
-}
-
 struct sysprop_change_callback_info {
     sysprop_change_callback callback;
     int priority;
diff --git a/opengl/tests/hwc/hwcColorEquiv.cpp b/opengl/tests/hwc/hwcColorEquiv.cpp
index ab5277e..160906d 100644
--- a/opengl/tests/hwc/hwcColorEquiv.cpp
+++ b/opengl/tests/hwc/hwcColorEquiv.cpp
@@ -344,9 +344,9 @@
     hwcTestFillColorHBlend(equivFrame.get(), refFormat->format,
                            startRefColor, endRefColor);
 
-    hwc_layer_list_1_t *list;
-    size_t size = sizeof(hwc_layer_list_1_t) + numFrames * sizeof(hwc_layer_1_t);
-    if ((list = (hwc_layer_list_1_t *) calloc(1, size)) == NULL) {
+    hwc_display_contents_1_t *list;
+    size_t size = sizeof(hwc_display_contents_1_t) + numFrames * sizeof(hwc_layer_1_t);
+    if ((list = (hwc_display_contents_1_t *) calloc(1, size)) == NULL) {
         testPrintE("Allocate list failed");
         exit(11);
     }
@@ -383,7 +383,7 @@
 
     // Perform prepare operation
     if (verbose) { testPrintI("Prepare:"); hwcTestDisplayList(list); }
-    hwcDevice->prepare(hwcDevice, list);
+    hwcDevice->prepare(hwcDevice, 1, &list);
     if (verbose) {
         testPrintI("Post Prepare:");
         hwcTestDisplayListPrepareModifiable(list);
@@ -393,7 +393,9 @@
     list->flags &= ~HWC_GEOMETRY_CHANGED;
 
     if (verbose) {hwcTestDisplayListHandles(list); }
-    hwcDevice->set(hwcDevice, dpy, surface, list);
+    list->dpy = dpy;
+    list->sur = surface;
+    hwcDevice->set(hwcDevice, 1, &list);
 
     testDelay(endDelay);
 
diff --git a/opengl/tests/hwc/hwcCommit.cpp b/opengl/tests/hwc/hwcCommit.cpp
index d4873d8..3681fbb 100644
--- a/opengl/tests/hwc/hwcCommit.cpp
+++ b/opengl/tests/hwc/hwcCommit.cpp
@@ -1397,7 +1397,7 @@
 // Given a list of rectangles, determine how many HWC will commit to render
 uint32_t numOverlays(list<Rectangle>& rectList)
 {
-    hwc_layer_list_1_t *hwcList;
+    hwc_display_contents_1_t *hwcList;
     list<sp<GraphicBuffer> > buffers;
 
     hwcList = hwcTestCreateLayerList(rectList.size());
@@ -1430,7 +1430,7 @@
 
     // Perform prepare operation
     if (verbose) { testPrintI("Prepare:"); hwcTestDisplayList(hwcList); }
-    hwcDevice->prepare(hwcDevice, hwcList);
+    hwcDevice->prepare(hwcDevice, 1, &hwcList);
     if (verbose) {
         testPrintI("Post Prepare:");
         hwcTestDisplayListPrepareModifiable(hwcList);
diff --git a/opengl/tests/hwc/hwcRects.cpp b/opengl/tests/hwc/hwcRects.cpp
index e2f0039..ec0403f 100644
--- a/opengl/tests/hwc/hwcRects.cpp
+++ b/opengl/tests/hwc/hwcRects.cpp
@@ -307,7 +307,7 @@
     }
 
     // Create list of frames
-    hwc_layer_list_1_t *list;
+    hwc_display_contents_1_t *list;
     list = hwcTestCreateLayerList(rectangle.size());
     if (list == NULL) {
         testPrintE("hwcTestCreateLayerList failed");
@@ -329,7 +329,7 @@
 
     // Perform prepare operation
     if (verbose) { testPrintI("Prepare:"); hwcTestDisplayList(list); }
-    hwcDevice->prepare(hwcDevice, list);
+    hwcDevice->prepare(hwcDevice, 1, &list);
     if (verbose) {
         testPrintI("Post Prepare:");
         hwcTestDisplayListPrepareModifiable(list);
@@ -341,7 +341,9 @@
     // Perform the set operation(s)
     if (verbose) {testPrintI("Set:"); }
     if (verbose) { hwcTestDisplayListHandles(list); }
-    hwcDevice->set(hwcDevice, dpy, surface, list);
+    list->dpy = dpy;
+    list->sur = surface;
+    hwcDevice->set(hwcDevice, 1, &list);
 
     testDelay(endDelay);
 
diff --git a/opengl/tests/hwc/hwcStress.cpp b/opengl/tests/hwc/hwcStress.cpp
index ccc7328..3e8ea8d 100644
--- a/opengl/tests/hwc/hwcStress.cpp
+++ b/opengl/tests/hwc/hwcStress.cpp
@@ -409,7 +409,7 @@
         // generated for this pass.
         srand48(pass);
 
-        hwc_layer_list_1_t *list;
+        hwc_display_contents_1_t *list;
         list = hwcTestCreateLayerList(testRandMod(frames.size()) + 1);
         if (list == NULL) {
             testPrintE("hwcTestCreateLayerList failed");
@@ -478,7 +478,7 @@
 
         // Perform prepare operation
         if (verbose) { testPrintI("Prepare:"); hwcTestDisplayList(list); }
-        hwcDevice->prepare(hwcDevice, list);
+        hwcDevice->prepare(hwcDevice, 1, &list);
         if (verbose) {
             testPrintI("Post Prepare:");
             hwcTestDisplayListPrepareModifiable(list);
@@ -491,7 +491,9 @@
         if (verbose) {testPrintI("Set:"); }
         for (unsigned int n1 = 0; n1 < numSet; n1++) {
             if (verbose) { hwcTestDisplayListHandles(list); }
-            hwcDevice->set(hwcDevice, dpy, surface, list);
+            list->dpy = dpy;
+            list->sur = surface;
+            hwcDevice->set(hwcDevice, 1, &list);
 
             // Prandomly select a new set of handles
             for (unsigned int n1 = 0; n1 < list->numHwLayers; n1++) {
diff --git a/opengl/tests/hwc/hwcTestLib.cpp b/opengl/tests/hwc/hwcTestLib.cpp
index c6dbe9d..d567e6e 100644
--- a/opengl/tests/hwc/hwcTestLib.cpp
+++ b/opengl/tests/hwc/hwcTestLib.cpp
@@ -399,12 +399,12 @@
  * Dynamically creates layer list with numLayers worth
  * of hwLayers entries.
  */
-hwc_layer_list_1_t *hwcTestCreateLayerList(size_t numLayers)
+hwc_display_contents_1_t *hwcTestCreateLayerList(size_t numLayers)
 {
-    hwc_layer_list_1_t *list;
+    hwc_display_contents_1_t *list;
 
-    size_t size = sizeof(hwc_layer_list_1_t) + numLayers * sizeof(hwc_layer_1_t);
-    if ((list = (hwc_layer_list_1_t *) calloc(1, size)) == NULL) {
+    size_t size = sizeof(hwc_display_contents_1_t) + numLayers * sizeof(hwc_layer_1_t);
+    if ((list = (hwc_display_contents_1_t *) calloc(1, size)) == NULL) {
         return NULL;
     }
     list->flags = HWC_GEOMETRY_CHANGED;
@@ -417,13 +417,13 @@
  * hwcTestFreeLayerList
  * Frees memory previous allocated via hwcTestCreateLayerList().
  */
-void hwcTestFreeLayerList(hwc_layer_list_1_t *list)
+void hwcTestFreeLayerList(hwc_display_contents_1_t *list)
 {
     free(list);
 }
 
 // Display the settings of the layer list pointed to by list
-void hwcTestDisplayList(hwc_layer_list_1_t *list)
+void hwcTestDisplayList(hwc_display_contents_1_t *list)
 {
     testPrintI("  flags: %#x%s", list->flags,
                (list->flags & HWC_GEOMETRY_CHANGED) ? " GEOMETRY_CHANGED" : "");
@@ -494,7 +494,7 @@
  * Displays the portions of a list that are meant to be modified by
  * a prepare call.
  */
-void hwcTestDisplayListPrepareModifiable(hwc_layer_list_1_t *list)
+void hwcTestDisplayListPrepareModifiable(hwc_display_contents_1_t *list)
 {
     uint32_t numOverlays = 0;
     for (unsigned int layer = 0; layer < list->numHwLayers; layer++) {
@@ -522,7 +522,7 @@
  *
  * Displays the handles of all the graphic buffers in the list.
  */
-void hwcTestDisplayListHandles(hwc_layer_list_1_t *list)
+void hwcTestDisplayListHandles(hwc_display_contents_1_t *list)
 {
     const unsigned int maxLayersPerLine = 6;
 
diff --git a/opengl/tests/hwc/hwcTestLib.h b/opengl/tests/hwc/hwcTestLib.h
index db3f5c1..d7d5837 100644
--- a/opengl/tests/hwc/hwcTestLib.h
+++ b/opengl/tests/hwc/hwcTestLib.h
@@ -113,11 +113,11 @@
 const char *hwcTestGraphicFormat2str(uint32_t format);
 std::string hwcTestRect2str(const struct hwc_rect& rect);
 
-hwc_layer_list_1_t *hwcTestCreateLayerList(size_t numLayers);
-void hwcTestFreeLayerList(hwc_layer_list_1_t *list);
-void hwcTestDisplayList(hwc_layer_list_1_t *list);
-void hwcTestDisplayListPrepareModifiable(hwc_layer_list_1_t *list);
-void hwcTestDisplayListHandles(hwc_layer_list_1_t *list);
+hwc_display_contents_1_t *hwcTestCreateLayerList(size_t numLayers);
+void hwcTestFreeLayerList(hwc_display_contents_1_t *list);
+void hwcTestDisplayList(hwc_display_contents_1_t *list);
+void hwcTestDisplayListPrepareModifiable(hwc_display_contents_1_t *list);
+void hwcTestDisplayListHandles(hwc_display_contents_1_t *list);
 
 uint32_t hwcTestColor2Pixel(uint32_t format, ColorFract color, float alpha);
 void hwcTestColorConvert(uint32_t fromFormat, uint32_t toFormat,
diff --git a/services/surfaceflinger/Android.mk b/services/surfaceflinger/Android.mk
index e4fd291..dd0dc16 100644
--- a/services/surfaceflinger/Android.mk
+++ b/services/surfaceflinger/Android.mk
@@ -3,13 +3,13 @@
 
 LOCAL_SRC_FILES:= \
     Client.cpp                              \
-    DisplayHardware.cpp     				\
+    DdmConnection.cpp                       \
+    DisplayDevice.cpp                       \
     EventThread.cpp                         \
     Layer.cpp                               \
     LayerBase.cpp                           \
     LayerDim.cpp                            \
     LayerScreenshot.cpp                     \
-    DisplayHardware/DisplayHardwareBase.cpp \
     DisplayHardware/FramebufferSurface.cpp  \
     DisplayHardware/HWComposer.cpp          \
     DisplayHardware/PowerHAL.cpp            \
@@ -40,6 +40,7 @@
 
 LOCAL_SHARED_LIBRARIES := \
 	libcutils \
+	libdl \
 	libhardware \
 	libutils \
 	libEGL \
@@ -48,13 +49,6 @@
 	libui \
 	libgui
 
-# this is only needed for DDMS debugging
-ifneq ($(TARGET_BUILD_PDK),true)
-	LOCAL_SHARED_LIBRARIES += libdvm libandroid_runtime
-	LOCAL_CFLAGS += -DDDMS_DEBUGGING
-	LOCAL_SRC_FILES += DdmConnection.cpp
-endif
-
 LOCAL_MODULE:= libsurfaceflinger
 
 include $(BUILD_SHARED_LIBRARY)
diff --git a/services/surfaceflinger/DdmConnection.cpp b/services/surfaceflinger/DdmConnection.cpp
index 467a915..433b38e 100644
--- a/services/surfaceflinger/DdmConnection.cpp
+++ b/services/surfaceflinger/DdmConnection.cpp
@@ -14,16 +14,16 @@
  * limitations under the License.
  */
 
-#include <android_runtime/AndroidRuntime.h>
+#include <dlfcn.h>
+
+#include <cutils/log.h>
 
 #include "jni.h"
 #include "DdmConnection.h"
 
-extern "C" jint Java_com_android_internal_util_WithFramework_registerNatives(
-        JNIEnv* env, jclass clazz);
-
 namespace android {
 
+
 void DdmConnection::start(const char* name) {
     JavaVM* vm;
     JNIEnv* env;
@@ -40,12 +40,36 @@
     args.nOptions = 1;
     args.ignoreUnrecognized = JNI_FALSE;
 
+
+    void* libdvm_dso = dlopen("libdvm.so", RTLD_NOW);
+    ALOGE_IF(!libdvm_dso, "DdmConnection: %s", dlerror());
+
+    void* libandroid_runtime_dso = dlopen("libandroid_runtime.so", RTLD_NOW);
+    ALOGE_IF(!libandroid_runtime_dso, "DdmConnection: %s", dlerror());
+
+    if (!libdvm_dso || !libandroid_runtime_dso) {
+        goto error;
+    }
+
+    jint (*JNI_CreateJavaVM)(JavaVM** p_vm, JNIEnv** p_env, void* vm_args);
+    JNI_CreateJavaVM = (typeof JNI_CreateJavaVM)dlsym(libdvm_dso, "JNI_CreateJavaVM");
+    ALOGE_IF(!JNI_CreateJavaVM, "DdmConnection: %s", dlerror());
+
+    jint (*registerNatives)(JNIEnv* env, jclass clazz);
+    registerNatives = (typeof registerNatives)dlsym(libandroid_runtime_dso,
+        "Java_com_android_internal_util_WithFramework_registerNatives");
+    ALOGE_IF(!registerNatives, "DdmConnection: %s", dlerror());
+
+    if (!JNI_CreateJavaVM || !registerNatives) {
+        goto error;
+    }
+
     if (JNI_CreateJavaVM(&vm, &env, &args) == 0) {
         jclass startClass;
         jmethodID startMeth;
 
         // register native code
-        if (Java_com_android_internal_util_WithFramework_registerNatives(env, 0) == 0) {
+        if (registerNatives(env, 0) == 0) {
             // set our name by calling DdmHandleAppName.setAppName()
             startClass = env->FindClass("android/ddm/DdmHandleAppName");
             if (startClass) {
@@ -70,6 +94,15 @@
             }
         }
     }
+    return;
+
+error:
+    if (libandroid_runtime_dso) {
+        dlclose(libandroid_runtime_dso);
+    }
+    if (libdvm_dso) {
+        dlclose(libdvm_dso);
+    }
 }
 
 }; // namespace android
diff --git a/services/surfaceflinger/DisplayHardware.cpp b/services/surfaceflinger/DisplayDevice.cpp
similarity index 67%
rename from services/surfaceflinger/DisplayHardware.cpp
rename to services/surfaceflinger/DisplayDevice.cpp
index 4b5d8ab..56ac635 100644
--- a/services/surfaceflinger/DisplayHardware.cpp
+++ b/services/surfaceflinger/DisplayDevice.cpp
@@ -27,6 +27,8 @@
 #include <ui/DisplayInfo.h>
 #include <ui/PixelFormat.h>
 
+#include <gui/SurfaceTextureClient.h>
+
 #include <GLES/gl.h>
 #include <EGL/egl.h>
 #include <EGL/eglext.h>
@@ -34,10 +36,9 @@
 #include <hardware/gralloc.h>
 
 #include "DisplayHardware/FramebufferSurface.h"
-#include "DisplayHardware/DisplayHardwareBase.h"
 #include "DisplayHardware/HWComposer.h"
 
-#include "DisplayHardware.h"
+#include "DisplayDevice.h"
 #include "GLExtensions.h"
 #include "SurfaceFlinger.h"
 #include "LayerBase.h"
@@ -98,90 +99,79 @@
  *
  */
 
-DisplayHardware::DisplayHardware(
+DisplayDevice::DisplayDevice(
         const sp<SurfaceFlinger>& flinger,
         int display,
-        const sp<SurfaceTextureClient>& surface,
+        const sp<ANativeWindow>& nativeWindow,
+        const sp<FramebufferSurface>& framebufferSurface,
         EGLConfig config)
-    : DisplayHardwareBase(display),
-      mFlinger(flinger),
-      mDisplayId(display),
-      mNativeWindow(surface),
-      mFlags(0),
-      mSecureLayerVisible(false)
+    : mFlinger(flinger),
+      mId(display),
+      mNativeWindow(nativeWindow),
+      mFramebufferSurface(framebufferSurface),
+      mDisplay(EGL_NO_DISPLAY),
+      mSurface(EGL_NO_SURFACE),
+      mContext(EGL_NO_CONTEXT),
+      mDpiX(), mDpiY(),
+      mDensity(),
+      mDisplayWidth(), mDisplayHeight(), mFormat(),
+      mFlags(),
+      mPageFlipCount(),
+      mSecureLayerVisible(false),
+      mScreenAcquired(false),
+      mOrientation(),
+      mLayerStack(0)
 {
     init(config);
 }
 
-DisplayHardware::~DisplayHardware() {
+DisplayDevice::~DisplayDevice() {
+    if (mSurface != EGL_NO_SURFACE) {
+        eglDestroySurface(mDisplay, mSurface);
+        mSurface = EGL_NO_SURFACE;
+    }
 }
 
-float DisplayHardware::getDpiX() const {
+bool DisplayDevice::isValid() const {
+    return mFlinger != NULL;
+}
+
+float DisplayDevice::getDpiX() const {
     return mDpiX;
 }
 
-float DisplayHardware::getDpiY() const {
+float DisplayDevice::getDpiY() const {
     return mDpiY;
 }
 
-float DisplayHardware::getDensity() const {
+float DisplayDevice::getDensity() const {
     return mDensity;
 }
 
-float DisplayHardware::getRefreshRate() const {
-    return mRefreshRate;
-}
-
-int DisplayHardware::getWidth() const {
+int DisplayDevice::getWidth() const {
     return mDisplayWidth;
 }
 
-int DisplayHardware::getHeight() const {
+int DisplayDevice::getHeight() const {
     return mDisplayHeight;
 }
 
-PixelFormat DisplayHardware::getFormat() const {
+PixelFormat DisplayDevice::getFormat() const {
     return mFormat;
 }
 
-EGLSurface DisplayHardware::getEGLSurface() const {
+EGLSurface DisplayDevice::getEGLSurface() const {
     return mSurface;
 }
 
-status_t DisplayHardware::getInfo(DisplayInfo* info) const {
-    info->w = getWidth();
-    info->h = getHeight();
-    info->xdpi = getDpiX();
-    info->ydpi = getDpiY();
-    info->fps = getRefreshRate();
-    info->density = getDensity();
-    info->orientation = getOrientation();
-    // TODO: this needs to go away (currently needed only by webkit)
-    getPixelFormatInfo(getFormat(), &info->pixelFormatInfo);
-    return NO_ERROR;
-}
-
-void DisplayHardware::init(EGLConfig config)
+void DisplayDevice::init(EGLConfig config)
 {
     ANativeWindow* const window = mNativeWindow.get();
 
-    int concreteType;
-    window->query(window, NATIVE_WINDOW_CONCRETE_TYPE, &concreteType);
-    if (concreteType == NATIVE_WINDOW_FRAMEBUFFER) {
-        mFramebufferSurface = static_cast<FramebufferSurface *>(mNativeWindow.get());
-    }
-
     int format;
     window->query(window, NATIVE_WINDOW_FORMAT, &format);
     mDpiX = window->xdpi;
     mDpiY = window->ydpi;
-    if (mFramebufferSurface != NULL) {
-        mRefreshRate = mFramebufferSurface->getRefreshRate();
-    } else {
-        mRefreshRate = 60;
-    }
-    mRefreshPeriod = nsecs_t(1e9 / mRefreshRate);
-
 
     // TODO: Not sure if display density should handled by SF any longer
     class Density {
@@ -224,55 +214,27 @@
     eglQuerySurface(display, surface, EGL_WIDTH,  &mDisplayWidth);
     eglQuerySurface(display, surface, EGL_HEIGHT, &mDisplayHeight);
 
-    if (mFramebufferSurface != NULL) {
-        if (mFramebufferSurface->isUpdateOnDemand()) {
-            mFlags |= PARTIAL_UPDATES;
-            // if we have partial updates, we definitely don't need to
-            // preserve the backbuffer, which may be costly.
-            eglSurfaceAttrib(display, surface,
-                    EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED);
-        }
-    }
-
     mDisplay = display;
     mSurface = surface;
     mFormat  = format;
     mPageFlipCount = 0;
 
     // initialize the display orientation transform.
-    DisplayHardware::setOrientation(ISurfaceComposer::eOrientationDefault);
+    DisplayDevice::setOrientation(ISurfaceComposer::eOrientationDefault);
 }
 
-uint32_t DisplayHardware::getPageFlipCount() const {
+uint32_t DisplayDevice::getPageFlipCount() const {
     return mPageFlipCount;
 }
 
-nsecs_t DisplayHardware::getRefreshTimestamp() const {
-    // this returns the last refresh timestamp.
-    // if the last one is not available, we estimate it based on
-    // the refresh period and whatever closest timestamp we have.
-    Mutex::Autolock _l(mLock);
-    nsecs_t now = systemTime(CLOCK_MONOTONIC);
-    return now - ((now - mLastHwVSync) %  mRefreshPeriod);
-}
-
-nsecs_t DisplayHardware::getRefreshPeriod() const {
-    return mRefreshPeriod;
-}
-
-status_t DisplayHardware::compositionComplete() const {
+status_t DisplayDevice::compositionComplete() const {
     if (mFramebufferSurface == NULL) {
         return NO_ERROR;
     }
     return mFramebufferSurface->compositionComplete();
 }
 
-void DisplayHardware::onVSyncReceived(nsecs_t timestamp) {
-    Mutex::Autolock _l(mLock);
-    mLastHwVSync = timestamp;
-}
-
-void DisplayHardware::flip(const Region& dirty) const
+void DisplayDevice::flip(const Region& dirty) const
 {
     checkGLErrors();
 
@@ -288,30 +250,32 @@
     } 
 #endif
     
-    if (mFlags & PARTIAL_UPDATES) {
-        if (mFramebufferSurface != NULL) {
-            mFramebufferSurface->setUpdateRectangle(dirty.getBounds());
-        }
-    }
-    
     mPageFlipCount++;
 }
 
-uint32_t DisplayHardware::getFlags() const
+uint32_t DisplayDevice::getFlags() const
 {
     return mFlags;
 }
 
-void DisplayHardware::dump(String8& res) const
+void DisplayDevice::dump(String8& res) const
 {
     if (mFramebufferSurface != NULL) {
         mFramebufferSurface->dump(res);
     }
 }
 
+void DisplayDevice::makeCurrent(const sp<const DisplayDevice>& hw, EGLContext ctx) {
+    EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
+    if (sur != hw->mSurface) {
+        EGLDisplay dpy = eglGetCurrentDisplay();
+        eglMakeCurrent(dpy, hw->mSurface, hw->mSurface, ctx);
+    }
+}
+
 // ----------------------------------------------------------------------------
 
-void DisplayHardware::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
+void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
     mVisibleLayersSortedByZ = layers;
     size_t count = layers.size();
     for (size_t i=0 ; i<count ; i++) {
@@ -321,17 +285,35 @@
     }
 }
 
-Vector< sp<LayerBase> > DisplayHardware::getVisibleLayersSortedByZ() const {
+Vector< sp<LayerBase> > DisplayDevice::getVisibleLayersSortedByZ() const {
     return mVisibleLayersSortedByZ;
 }
 
-bool DisplayHardware::getSecureLayerVisible() const {
+bool DisplayDevice::getSecureLayerVisible() const {
     return mSecureLayerVisible;
 }
 
 // ----------------------------------------------------------------------------
 
-status_t DisplayHardware::orientationToTransfrom(
+bool DisplayDevice::canDraw() const {
+    return mScreenAcquired;
+}
+
+void DisplayDevice::releaseScreen() const {
+    mScreenAcquired = false;
+}
+
+void DisplayDevice::acquireScreen() const {
+    mScreenAcquired = true;
+}
+
+bool DisplayDevice::isScreenAcquired() const {
+    return mScreenAcquired;
+}
+
+// ----------------------------------------------------------------------------
+
+status_t DisplayDevice::orientationToTransfrom(
         int orientation, int w, int h, Transform* tr)
 {
     uint32_t flags = 0;
@@ -355,11 +337,11 @@
     return NO_ERROR;
 }
 
-status_t DisplayHardware::setOrientation(int orientation) {
+status_t DisplayDevice::setOrientation(int orientation) {
     int w = mDisplayWidth;
     int h = mDisplayHeight;
 
-    DisplayHardware::orientationToTransfrom(
+    DisplayDevice::orientationToTransfrom(
             orientation, w, h, &mGlobalTransform);
     if (orientation & ISurfaceComposer::eOrientationSwapMask) {
         int tmp = w;
@@ -367,5 +349,6 @@
         h = tmp;
     }
     mOrientation = orientation;
+    dirtyRegion.set(bounds());
     return NO_ERROR;
 }
diff --git a/services/surfaceflinger/DisplayHardware.h b/services/surfaceflinger/DisplayDevice.h
similarity index 67%
rename from services/surfaceflinger/DisplayHardware.h
rename to services/surfaceflinger/DisplayDevice.h
index 98579b4..78d44f3 100644
--- a/services/surfaceflinger/DisplayHardware.h
+++ b/services/surfaceflinger/DisplayDevice.h
@@ -14,16 +14,14 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_DISPLAY_HARDWARE_H
-#define ANDROID_DISPLAY_HARDWARE_H
+#ifndef ANDROID_DISPLAY_DEVICE_H
+#define ANDROID_DISPLAY_DEVICE_H
 
 #include <stdlib.h>
 
 #include <ui/PixelFormat.h>
 #include <ui/Region.h>
 
-#include <GLES/gl.h>
-#include <GLES/glext.h>
 #include <EGL/egl.h>
 #include <EGL/eglext.h>
 
@@ -32,7 +30,7 @@
 
 #include "Transform.h"
 
-#include "DisplayHardware/DisplayHardwareBase.h"
+struct ANativeWindow;
 
 namespace android {
 
@@ -40,42 +38,51 @@
 class FramebufferSurface;
 class LayerBase;
 class SurfaceFlinger;
-class SurfaceTextureClient;
 
-class DisplayHardware : public DisplayHardwareBase
+class DisplayDevice : public LightRefBase<DisplayDevice>
 {
 public:
+    // region in layer-stack space
+    mutable Region dirtyRegion;
+    // region in screen space
+    mutable Region swapRegion;
+    // region in screen space
+    Region undefinedRegion;
 
     enum {
-        PARTIAL_UPDATES             = 0x00020000,   // video driver feature
-        SWAP_RECTANGLE              = 0x00080000,
+        DISPLAY_ID_MAIN = 0,
+        DISPLAY_ID_HDMI = 1
     };
 
-    DisplayHardware(
+    enum {
+        PARTIAL_UPDATES = 0x00020000, // video driver feature
+        SWAP_RECTANGLE  = 0x00080000,
+    };
+
+    DisplayDevice(
             const sp<SurfaceFlinger>& flinger,
             int dpy,
-            const sp<SurfaceTextureClient>& surface,
+            const sp<ANativeWindow>& nativeWindow,
+            const sp<FramebufferSurface>& framebufferSurface,
             EGLConfig config);
 
-    virtual ~DisplayHardware();
+    ~DisplayDevice();
+
+    // whether this is a valid object. An invalid DisplayDevice is returned
+    // when an non existing id is requested
+    bool isValid() const;
 
     // Flip the front and back buffers if the back buffer is "dirty".  Might
     // be instantaneous, might involve copying the frame buffer around.
     void flip(const Region& dirty) const;
 
-    void onVSyncReceived(nsecs_t timestamp);
-
     float       getDpiX() const;
     float       getDpiY() const;
-    float       getRefreshRate() const;
     float       getDensity() const;
     int         getWidth() const;
     int         getHeight() const;
     PixelFormat getFormat() const;
     uint32_t    getFlags() const;
-    nsecs_t     getRefreshPeriod() const;
-    nsecs_t     getRefreshTimestamp() const;
-    status_t    getInfo(DisplayInfo* info) const;
 
     EGLSurface  getEGLSurface() const;
 
@@ -86,11 +93,7 @@
     status_t                setOrientation(int orientation);
     int                     getOrientation() const { return mOrientation; }
     const Transform&        getTransform() const { return mGlobalTransform; }
-
-    uint32_t getPageFlipCount() const;
-    EGLDisplay getEGLDisplay() const { return mDisplay; }
-
-    void dump(String8& res) const;
+    uint32_t                getLayerStack() const { return mLayerStack; }
 
     status_t compositionComplete() const;
     
@@ -99,6 +102,26 @@
     }
     inline Rect bounds() const { return getBounds(); }
 
+    static void makeCurrent(const sp<const DisplayDevice>& hw, EGLContext ctx);
+
+    /* ------------------------------------------------------------------------
+     * blank / unplank management
+     */
+    void releaseScreen() const;
+    void acquireScreen() const;
+    bool isScreenAcquired() const;
+    bool canDraw() const;
+
+    /* ------------------------------------------------------------------------
+     * Debugging
+     */
+    uint32_t getPageFlipCount() const;
+    void dump(String8& res) const;
+
+    inline bool operator < (const DisplayDevice& rhs) const {
+        return mId < rhs.mId;
+    }
+
 private:
     void init(EGLConfig config);
 
@@ -106,19 +129,19 @@
      *  Constants, set during initialization
      */
     sp<SurfaceFlinger> mFlinger;
-    int mDisplayId;
+    int32_t mId;
+
     // ANativeWindow this display is rendering into
-    sp<SurfaceTextureClient> mNativeWindow;
+    sp<ANativeWindow> mNativeWindow;
+
     // set if mNativeWindow is a FramebufferSurface
     sp<FramebufferSurface> mFramebufferSurface;
 
-
     EGLDisplay      mDisplay;
     EGLSurface      mSurface;
     EGLContext      mContext;
     float           mDpiX;
     float           mDpiY;
-    float           mRefreshRate;
     float           mDensity;
     int             mDisplayWidth;
     int             mDisplayHeight;
@@ -126,31 +149,31 @@
     uint32_t        mFlags;
     mutable uint32_t mPageFlipCount;
 
-    nsecs_t         mRefreshPeriod;
-
     /*
      * Can only accessed from the main thread, these members
      * don't need synchronization.
      */
+
     // list of visible layers on that display
     Vector< sp<LayerBase> > mVisibleLayersSortedByZ;
+
     // Whether we have a visible secure layer on this display
     bool mSecureLayerVisible;
 
+    // Whether the screen is blanked;
+    mutable int mScreenAcquired;
 
-    // this used to be in GraphicPlane
+
+    /*
+     * Transaction state
+     */
     static status_t orientationToTransfrom(int orientation, int w, int h,
             Transform* tr);
     Transform mGlobalTransform;
     int mOrientation;
-
-    /*
-     *  protected by mLock
-     */
-    mutable Mutex mLock;
-    mutable nsecs_t mLastHwVSync;
+    uint32_t mLayerStack;
 };
 
 }; // namespace android
 
-#endif // ANDROID_DISPLAY_HARDWARE_H
+#endif // ANDROID_DISPLAY_DEVICE_H
diff --git a/services/surfaceflinger/DisplayHardware/DisplayHardwareBase.cpp b/services/surfaceflinger/DisplayHardware/DisplayHardwareBase.cpp
deleted file mode 100644
index 4141181..0000000
--- a/services/surfaceflinger/DisplayHardware/DisplayHardwareBase.cpp
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * Copyright (C) 2012 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.
- */
-
-#include <stdint.h>
-#include <sys/types.h>
-
-#include "DisplayHardware/DisplayHardwareBase.h"
-
-// ----------------------------------------------------------------------------
-namespace android {
-
-DisplayHardwareBase::DisplayHardwareBase(uint32_t displayIndex) {
-    mScreenAcquired = true;
-}
-
-DisplayHardwareBase::~DisplayHardwareBase() {
-}
-
-bool DisplayHardwareBase::canDraw() const {
-    return mScreenAcquired;
-}
-
-void DisplayHardwareBase::releaseScreen() const {
-    mScreenAcquired = false;
-}
-
-void DisplayHardwareBase::acquireScreen() const {
-    mScreenAcquired = true;
-}
-
-bool DisplayHardwareBase::isScreenAcquired() const {
-    return mScreenAcquired;
-}
-
-}; // namespace android
diff --git a/services/surfaceflinger/DisplayHardware/DisplayHardwareBase.h b/services/surfaceflinger/DisplayHardware/DisplayHardwareBase.h
deleted file mode 100644
index e6eb2b5..0000000
--- a/services/surfaceflinger/DisplayHardware/DisplayHardwareBase.h
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Copyright (C) 2012 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_DISPLAY_HARDWARE_BASE_H
-#define ANDROID_DISPLAY_HARDWARE_BASE_H
-
-#include <stdint.h>
-
-namespace android {
-
-class DisplayHardwareBase {
-public:
-    DisplayHardwareBase(uint32_t displayIndex);
-    ~DisplayHardwareBase();
-
-    // console management
-    void releaseScreen() const;
-    void acquireScreen() const;
-    bool isScreenAcquired() const;
-
-    bool canDraw() const;
-
-private:
-    mutable int mScreenAcquired;
-};
-
-}; // namespace android
-
-#endif // ANDROID_DISPLAY_HARDWARE_BASE_H
diff --git a/services/surfaceflinger/DisplayHardware/FramebufferSurface.cpp b/services/surfaceflinger/DisplayHardware/FramebufferSurface.cpp
index 7695e7f..f329136 100644
--- a/services/surfaceflinger/DisplayHardware/FramebufferSurface.cpp
+++ b/services/surfaceflinger/DisplayHardware/FramebufferSurface.cpp
@@ -1,6 +1,6 @@
 /*
  **
- ** Copyright 2007 The Android Open Source Project
+ ** Copyright 2012 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.
@@ -48,17 +48,33 @@
 
 // ----------------------------------------------------------------------------
 
+class GraphicBufferAlloc : public BnGraphicBufferAlloc {
+public:
+    GraphicBufferAlloc() { };
+    virtual ~GraphicBufferAlloc() { };
+    virtual sp<GraphicBuffer> createGraphicBuffer(uint32_t w, uint32_t h,
+            PixelFormat format, uint32_t usage, status_t* error) {
+        sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
+        return graphicBuffer;
+    }
+};
+
+
 /*
  * This implements the (main) framebuffer management. This class is used
  * mostly by SurfaceFlinger, but also by command line GL application.
  *
  */
 
-FramebufferSurface::FramebufferSurface()
-    : SurfaceTextureClient(),
-      fbDev(0), mCurrentBufferIndex(-1), mUpdateOnDemand(false)
+FramebufferSurface::FramebufferSurface():
+    ConsumerBase(new BufferQueue(true, NUM_FRAME_BUFFERS,
+            new GraphicBufferAlloc())),
+    fbDev(0),
+    mCurrentBufferSlot(-1),
+    mCurrentBuffer(0)
 {
     hw_module_t const* module;
+
     if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) {
         int stride;
         int err;
@@ -70,85 +86,64 @@
         if (!fbDev)
             return;
 
-        mUpdateOnDemand = (fbDev->setUpdateRect != 0);
-
-        const_cast<uint32_t&>(ANativeWindow::flags) = fbDev->flags;
-        const_cast<int&>(ANativeWindow::minSwapInterval) =  fbDev->minSwapInterval;
-        const_cast<int&>(ANativeWindow::maxSwapInterval) =  fbDev->maxSwapInterval;
-
-        if (fbDev->xdpi == 0 || fbDev->ydpi == 0) {
-            ALOGE("invalid screen resolution from fb HAL (xdpi=%f, ydpi=%f), "
-                   "defaulting to 160 dpi", fbDev->xdpi, fbDev->ydpi);
-            const_cast<float&>(ANativeWindow::xdpi) = 160;
-            const_cast<float&>(ANativeWindow::ydpi) = 160;
-        } else {
-            const_cast<float&>(ANativeWindow::xdpi) = fbDev->xdpi;
-            const_cast<float&>(ANativeWindow::ydpi) = fbDev->ydpi;
-        }
-
+        mName = "FramebufferSurface";
+        mBufferQueue->setConsumerName(mName);
+        mBufferQueue->setConsumerUsageBits(GRALLOC_USAGE_HW_FB |
+                GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER);
+        mBufferQueue->setDefaultBufferFormat(fbDev->format);
+        mBufferQueue->setDefaultBufferSize(fbDev->width, fbDev->height);
+        mBufferQueue->setSynchronousMode(true);
+        mBufferQueue->setBufferCountServer(NUM_FRAME_BUFFERS);
     } else {
         ALOGE("Couldn't get gralloc module");
     }
-
-    class GraphicBufferAlloc : public BnGraphicBufferAlloc {
-    public:
-        GraphicBufferAlloc() { };
-        virtual ~GraphicBufferAlloc() { };
-        virtual sp<GraphicBuffer> createGraphicBuffer(uint32_t w, uint32_t h,
-                PixelFormat format, uint32_t usage, status_t* error) {
-            sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
-            return graphicBuffer;
-        }
-    };
-
-    mBufferQueue = new BufferQueue(true, NUM_FRAME_BUFFERS, new GraphicBufferAlloc());
-    mBufferQueue->setConsumerUsageBits(GRALLOC_USAGE_HW_FB|GRALLOC_USAGE_HW_RENDER|GRALLOC_USAGE_HW_COMPOSER);
-    mBufferQueue->setDefaultBufferFormat(fbDev->format);
-    mBufferQueue->setDefaultBufferSize(fbDev->width, fbDev->height);
-    mBufferQueue->setSynchronousMode(true);
-    mBufferQueue->setBufferCountServer(NUM_FRAME_BUFFERS);
-    setISurfaceTexture(mBufferQueue);
 }
 
-void FramebufferSurface::onFirstRef() {
-    class Listener : public BufferQueue::ConsumerListener {
-        const wp<FramebufferSurface> that;
-        virtual ~Listener() { }
-        virtual void onBuffersReleased() { }
-        void onFrameAvailable() {
-            sp<FramebufferSurface> self = that.promote();
-            if (self != NULL) {
-                BufferQueue::BufferItem item;
-                status_t err = self->mBufferQueue->acquireBuffer(&item);
-                if (err == 0) {
-                    if (item.mGraphicBuffer != 0) {
-                        self->mBuffers[item.mBuf] = item.mGraphicBuffer;
-                    }
-                    if (item.mFence.get()) {
-                        err = item.mFence->wait(Fence::TIMEOUT_NEVER);
-                        if (err) {
-                            ALOGE("failed waiting for buffer's fence: %d", err);
-                            self->mBufferQueue->releaseBuffer(item.mBuf,
-                                    EGL_NO_DISPLAY, EGL_NO_SYNC_KHR,
-                                    item.mFence);
-                            return;
-                        }
-                    }
-                    self->fbDev->post(self->fbDev, self->mBuffers[item.mBuf]->handle);
-                    if (self->mCurrentBufferIndex >= 0) {
-                        self->mBufferQueue->releaseBuffer(self->mCurrentBufferIndex,
-                                EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, Fence::NO_FENCE);
-                    }
-                    self->mCurrentBufferIndex = item.mBuf;
-                }
-            }
-        }
-    public:
-        Listener(const sp<FramebufferSurface>& that) : that(that) { }
-    };
+status_t FramebufferSurface::nextBuffer(sp<GraphicBuffer>* buffer) {
+    Mutex::Autolock lock(mMutex);
 
-    mBufferQueue->setConsumerName(String8("FramebufferSurface"));
-    mBufferQueue->consumerConnect(new Listener(this));
+    BufferQueue::BufferItem item;
+    status_t err = acquireBufferLocked(&item);
+    if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
+        if (buffer != NULL) {
+            *buffer = mCurrentBuffer;
+        }
+        return NO_ERROR;
+    } else if (err != NO_ERROR) {
+        ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err);
+        return err;
+    }
+
+    // If the BufferQueue has freed and reallocated a buffer in mCurrentSlot
+    // then we may have acquired the slot we already own.  If we had released
+    // our current buffer before we call acquireBuffer then that release call
+    // would have returned STALE_BUFFER_SLOT, and we would have called
+    // freeBufferLocked on that slot.  Because the buffer slot has already
+    // been overwritten with the new buffer all we have to do is skip the
+    // releaseBuffer call and we should be in the same state we'd be in if we
+    // had released the old buffer first.
+    if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT &&
+        item.mBuf != mCurrentBufferSlot) {
+        // Release the previous buffer.
+        err = releaseBufferLocked(mCurrentBufferSlot, EGL_NO_DISPLAY,
+                EGL_NO_SYNC_KHR, Fence::NO_FENCE);
+        if (err != NO_ERROR && err != BufferQueue::STALE_BUFFER_SLOT) {
+            ALOGE("error releasing buffer: %s (%d)", strerror(-err), err);
+            return err;
+        }
+    }
+
+    mCurrentBufferSlot = item.mBuf;
+    mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer;
+    if (item.mFence != NULL) {
+        item.mFence->wait(Fence::TIMEOUT_NEVER);
+    }
+
+    if (buffer != NULL) {
+        *buffer = mCurrentBuffer;
+    }
+
+    return NO_ERROR;
 }
 
 FramebufferSurface::~FramebufferSurface() {
@@ -157,6 +152,29 @@
     }
 }
 
+void FramebufferSurface::onFrameAvailable() {
+    // XXX: The following code is here temporarily as part of the transition
+    // away from the framebuffer HAL.
+    sp<GraphicBuffer> buf;
+    status_t err = nextBuffer(&buf);
+    if (err != NO_ERROR) {
+        ALOGE("error latching next FramebufferSurface buffer: %s (%d)",
+                strerror(-err), err);
+        return;
+    }
+    err = fbDev->post(fbDev, buf->handle);
+    if (err != NO_ERROR) {
+        ALOGE("error posting framebuffer: %d", err);
+    }
+}
+
+void FramebufferSurface::freeBufferLocked(int slotIndex) {
+    ConsumerBase::freeBufferLocked(slotIndex);
+    if (slotIndex == mCurrentBufferSlot) {
+        mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT;
+    }
+}
+
 float FramebufferSurface::getRefreshRate() const {
     /* FIXME: REFRESH_RATE is a temporary HACK until we are able to report the
      * refresh rate properly from the HAL. The WindowManagerService now relies
@@ -172,10 +190,7 @@
 
 status_t FramebufferSurface::setUpdateRectangle(const Rect& r)
 {
-    if (!mUpdateOnDemand) {
-        return INVALID_OPERATION;
-    }
-    return fbDev->setUpdateRect(fbDev, r.left, r.top, r.width(), r.height());
+    return INVALID_OPERATION;
 }
 
 status_t FramebufferSurface::compositionComplete()
@@ -190,38 +205,10 @@
     if (fbDev->common.version >= 1 && fbDev->dump) {
         const size_t SIZE = 4096;
         char buffer[SIZE];
-
         fbDev->dump(fbDev, buffer, SIZE);
         result.append(buffer);
     }
-}
-
-int FramebufferSurface::query(int what, int* value) const {
-    Mutex::Autolock _l(mLock);
-    framebuffer_device_t* fb = fbDev;
-    switch (what) {
-        case NATIVE_WINDOW_DEFAULT_WIDTH:
-        case NATIVE_WINDOW_WIDTH:
-            *value = fb->width;
-            return NO_ERROR;
-        case NATIVE_WINDOW_DEFAULT_HEIGHT:
-        case NATIVE_WINDOW_HEIGHT:
-            *value = fb->height;
-            return NO_ERROR;
-        case NATIVE_WINDOW_FORMAT:
-            *value = fb->format;
-            return NO_ERROR;
-        case NATIVE_WINDOW_CONCRETE_TYPE:
-            *value = NATIVE_WINDOW_FRAMEBUFFER;
-            return NO_ERROR;
-        case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
-            *value = 0;
-            return NO_ERROR;
-        case NATIVE_WINDOW_TRANSFORM_HINT:
-            *value = 0;
-            return NO_ERROR;
-    }
-    return SurfaceTextureClient::query(what, value);
+    ConsumerBase::dump(result);
 }
 
 // ----------------------------------------------------------------------------
diff --git a/services/surfaceflinger/DisplayHardware/FramebufferSurface.h b/services/surfaceflinger/DisplayHardware/FramebufferSurface.h
index 672bfbb..95feaa0 100644
--- a/services/surfaceflinger/DisplayHardware/FramebufferSurface.h
+++ b/services/surfaceflinger/DisplayHardware/FramebufferSurface.h
@@ -20,9 +20,7 @@
 #include <stdint.h>
 #include <sys/types.h>
 
-#include <EGL/egl.h>
-
-#include <gui/SurfaceTextureClient.h>
+#include <gui/ConsumerBase.h>
 
 #define NUM_FRAME_BUFFERS  2
 
@@ -35,7 +33,7 @@
 
 // ---------------------------------------------------------------------------
 
-class FramebufferSurface : public SurfaceTextureClient {
+class FramebufferSurface : public ConsumerBase {
 public:
 
     static sp<FramebufferSurface> create();
@@ -43,28 +41,34 @@
     // TODO: this should be coming from HWC
     float getRefreshRate() const;
 
-    bool isUpdateOnDemand() const { return mUpdateOnDemand; }
+    bool isUpdateOnDemand() const { return false; }
     status_t setUpdateRectangle(const Rect& updateRect);
     status_t compositionComplete();
 
-    void dump(String8& result);
+    virtual void dump(String8& result);
 
-protected:
-    virtual void onFirstRef();
+    // nextBuffer waits for and then latches the next buffer from the
+    // BufferQueue and releases the previously latched buffer to the
+    // BufferQueue.  The new buffer is returned in the 'buffer' argument.
+    status_t nextBuffer(sp<GraphicBuffer>* buffer);
 
 private:
     FramebufferSurface();
     virtual ~FramebufferSurface(); // this class cannot be overloaded
-    virtual int query(int what, int* value) const;
+
+    virtual void onFrameAvailable();
+    virtual void freeBufferLocked(int slotIndex);
 
     framebuffer_device_t* fbDev;
 
-    sp<BufferQueue> mBufferQueue;
-    int mCurrentBufferIndex;
-    sp<GraphicBuffer> mBuffers[NUM_FRAME_BUFFERS];
+    // mCurrentBufferIndex is the slot index of the current buffer or
+    // INVALID_BUFFER_SLOT to indicate that either there is no current buffer
+    // or the buffer is not associated with a slot.
+    int mCurrentBufferSlot;
 
-    mutable Mutex mLock;
-    bool mUpdateOnDemand;
+    // mCurrentBuffer is the current buffer or NULL to indicate that there is
+    // no current buffer.
+    sp<GraphicBuffer> mCurrentBuffer;
 };
 
 // ---------------------------------------------------------------------------
diff --git a/services/surfaceflinger/DisplayHardware/HWComposer.cpp b/services/surfaceflinger/DisplayHardware/HWComposer.cpp
index b8506ee..40ce90b 100644
--- a/services/surfaceflinger/DisplayHardware/HWComposer.cpp
+++ b/services/surfaceflinger/DisplayHardware/HWComposer.cpp
@@ -50,17 +50,15 @@
 // Support for HWC_DEVICE_API_VERSION_0_3 and older:
 // Since v0.3 is deprecated and support will be dropped soon, as much as
 // possible the code is written to target v1.0. When using a v0.3 HWC, we
-// allocate v0.3 structures, but assign them to v1.0 pointers. Fields that
-// exist in both versions are located at the same offset, so in most cases we
-// can just use the v1.0 pointer without branches or casts.
+// allocate v0.3 structures, but assign them to v1.0 pointers.
 
 #if HWC_REMOVE_DEPRECATED_VERSIONS
-// We need complete types with to satisfy semantic checks, even though the
-// code paths that use these won't get executed at runtime (and will likely be
-// dead-code-eliminated). When we remove the code to support v0.3 we can remove
+// We need complete types to satisfy semantic checks, even though the code
+// paths that use these won't get executed at runtime (and will likely be dead-
+// code-eliminated). When we remove the code to support v0.3 we can remove
 // these as well.
 typedef hwc_layer_1_t hwc_layer_t;
-typedef hwc_layer_list_1_t hwc_layer_list_t;
+typedef hwc_display_contents_1_t hwc_layer_list_t;
 typedef hwc_composer_device_1_t hwc_composer_device_t;
 #endif
 
@@ -75,15 +73,97 @@
     }
 }
 
+static bool hwcHasVsyncEvent(const hwc_composer_device_1_t* hwc) {
+    return hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_0_3);
+}
+
 static size_t sizeofHwcLayerList(const hwc_composer_device_1_t* hwc,
         size_t numLayers) {
     if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
-        return sizeof(hwc_layer_list_1_t) + numLayers*sizeof(hwc_layer_1_t);
+        return sizeof(hwc_display_contents_1_t) + numLayers*sizeof(hwc_layer_1_t);
     } else {
         return sizeof(hwc_layer_list_t) + numLayers*sizeof(hwc_layer_t);
     }
 }
 
+static int hwcEventControl(hwc_composer_device_1_t* hwc, int dpy,
+        int event, int enabled) {
+    if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
+        return hwc->methods->eventControl(hwc, dpy, event, enabled);
+    } else {
+        hwc_composer_device_t* hwc0 = (hwc_composer_device_t*)hwc;
+        return hwc0->methods->eventControl(hwc0, event, enabled);
+    }
+}
+
+static int hwcBlank(hwc_composer_device_1_t* hwc, int dpy, int blank) {
+    if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
+        return hwc->methods->blank(hwc, dpy, blank);
+    } else {
+        if (blank) {
+            hwc_composer_device_t* hwc0 = (hwc_composer_device_t*)hwc;
+            return hwc0->set(hwc0, NULL, NULL, NULL);
+        } else {
+            // HWC 0.x turns the screen on at the next set()
+            return NO_ERROR;
+        }
+    }
+}
+
+static int hwcPrepare(hwc_composer_device_1_t* hwc,
+        size_t numDisplays, hwc_display_contents_1_t** displays) {
+    if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
+        return hwc->prepare(hwc, numDisplays, displays);
+    } else {
+        hwc_composer_device_t* hwc0 = (hwc_composer_device_t*)hwc;
+        hwc_layer_list_t* list0 = (hwc_layer_list_t*)displays[0];
+        // In the past, SurfaceFlinger would pass a NULL list when doing full
+        // OpenGL ES composition. I don't know what, if any, dependencies there
+        // are on this behavior, so I'm playing it safe and preserving it.
+        if (list0->numHwLayers == 0)
+            return hwc0->prepare(hwc0, NULL);
+        else
+            return hwc0->prepare(hwc0, list0);
+    }
+}
+
+static int hwcSet(hwc_composer_device_1_t* hwc, EGLDisplay dpy, EGLSurface sur,
+        size_t numDisplays, hwc_display_contents_1_t** displays) {
+    int err;
+    if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
+        displays[0]->dpy = dpy;
+        displays[0]->sur = sur;
+        err = hwc->set(hwc, numDisplays, displays);
+    } else {
+        hwc_composer_device_t* hwc0 = (hwc_composer_device_t*)hwc;
+        hwc_layer_list_t* list0 = (hwc_layer_list_t*)displays[0];
+        err = hwc0->set(hwc0, dpy, sur, list0);
+    }
+    return err;
+}
+
+static uint32_t& hwcFlags(hwc_composer_device_1_t* hwc,
+        hwc_display_contents_1_t* display) {
+    if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
+        return display->flags;
+    } else {
+        hwc_composer_device_t* hwc0 = (hwc_composer_device_t*)hwc;
+        hwc_layer_list_t* list0 = (hwc_layer_list_t*)display;
+        return list0->flags;
+    }
+}
+
+static size_t& hwcNumHwLayers(hwc_composer_device_1_t* hwc,
+        hwc_display_contents_1_t* display) {
+    if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
+        return display->numHwLayers;
+    } else {
+        hwc_composer_device_t* hwc0 = (hwc_composer_device_t*)hwc;
+        hwc_layer_list_t* list0 = (hwc_layer_list_t*)display;
+        return list0->numHwLayers;
+    }
+}
+
 // ---------------------------------------------------------------------------
 
 struct HWComposer::cb_context {
@@ -101,16 +181,17 @@
 
 HWComposer::HWComposer(
         const sp<SurfaceFlinger>& flinger,
-        EventHandler& handler,
-        nsecs_t refreshPeriod)
+        EventHandler& handler)
     : mFlinger(flinger),
-      mModule(0), mHwc(0), mList(0), mCapacity(0),
+      mModule(0), mHwc(0), mCapacity(0),
       mNumOVLayers(0), mNumFBLayers(0),
       mCBContext(new cb_context),
-      mEventHandler(handler),
-      mRefreshPeriod(refreshPeriod),
+      mEventHandler(handler), mRefreshPeriod(0),
       mVSyncCount(0), mDebugForceFakeVSync(false)
 {
+    for (size_t i = 0; i < MAX_DISPLAYS; i++)
+        mLists[i] = NULL;
+
     char value[PROPERTY_VALUE_MAX];
     property_get("debug.sf.no_hw_vsync", value, "0");
     mDebugForceFakeVSync = atoi(value);
@@ -133,11 +214,19 @@
         }
 
         if (mHwc) {
-            if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_0_3)) {
-                // always turn vsync off when we start
-                mHwc->methods->eventControl(mHwc, HWC_EVENT_VSYNC, 0);
-                needVSyncThread = false;
+            // always turn vsync off when we start
+            needVSyncThread = false;
+            if (hwcHasVsyncEvent(mHwc)) {
+                hwcEventControl(mHwc, 0, HWC_EVENT_VSYNC, 0);
+
+                int period;
+                if (mHwc->query(mHwc, HWC_VSYNC_PERIOD, &period) == NO_ERROR) {
+                    mRefreshPeriod = nsecs_t(period);
+                }
+            } else {
+                needVSyncThread = true;
             }
+
             if (mHwc->registerProcs) {
                 mCBContext->hwc = this;
                 mCBContext->procs.invalidate = &hook_invalidate;
@@ -145,9 +234,32 @@
                 mHwc->registerProcs(mHwc, &mCBContext->procs);
                 memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
             }
+
+            // create initial empty display contents for display 0
+            createWorkList(0);
         }
     }
 
+    if (mRefreshPeriod == 0) {
+        // for compatibility, we attempt to get the refresh rate from
+        // the FB HAL if we couldn't get it from the HWC HAL.
+        hw_module_t const* module;
+        if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) {
+            framebuffer_device_t* fbDev;
+            int err = framebuffer_open(module, &fbDev);
+            if (!err && fbDev) {
+                mRefreshPeriod = nsecs_t(1e9 / fbDev->fps);
+                framebuffer_close(fbDev);
+            }
+        }
+        ALOGW("getting VSYNC period from fb HAL: %lld", mRefreshPeriod);
+    }
+
+    if (mRefreshPeriod == 0) {
+        mRefreshPeriod = nsecs_t(1e9 / 60.0);
+        ALOGW("getting VSYNC period thin air: %lld", mRefreshPeriod);
+    }
+
     if (needVSyncThread) {
         // we don't have VSYNC support, we need to fake it
         mVSyncThread = new VSyncThread(*this);
@@ -155,8 +267,9 @@
 }
 
 HWComposer::~HWComposer() {
-    eventControl(EVENT_VSYNC, 0);
-    free(mList);
+    hwcEventControl(mHwc, 0, EVENT_VSYNC, 0);
+    for (size_t i = 0; i < MAX_DISPLAYS; i++)
+        free(mLists[i]);
     if (mVSyncThread != NULL) {
         mVSyncThread->requestExitAndWait();
     }
@@ -185,13 +298,28 @@
 void HWComposer::vsync(int dpy, int64_t timestamp) {
     ATRACE_INT("VSYNC", ++mVSyncCount&1);
     mEventHandler.onVSyncReceived(dpy, timestamp);
+    Mutex::Autolock _l(mLock);
+    mLastHwVSync = timestamp;
+}
+
+nsecs_t HWComposer::getRefreshPeriod() const {
+    return mRefreshPeriod;
+}
+
+nsecs_t HWComposer::getRefreshTimestamp() const {
+    // this returns the last refresh timestamp.
+    // if the last one is not available, we estimate it based on
+    // the refresh period and whatever closest timestamp we have.
+    Mutex::Autolock _l(mLock);
+    nsecs_t now = systemTime(CLOCK_MONOTONIC);
+    return now - ((now - mLastHwVSync) %  mRefreshPeriod);
 }
 
 void HWComposer::eventControl(int event, int enabled) {
     status_t err = NO_ERROR;
     if (mHwc && mHwc->common.version >= HWC_DEVICE_API_VERSION_0_3) {
         if (!mDebugForceFakeVSync) {
-            err = mHwc->methods->eventControl(mHwc, event, enabled);
+            err = hwcEventControl(mHwc, 0, event, enabled);
             // error here should not happen -- not sure what we should
             // do if it does.
             ALOGE_IF(err, "eventControl(%d, %d) failed %s",
@@ -206,32 +334,37 @@
 
 status_t HWComposer::createWorkList(size_t numLayers) {
     if (mHwc) {
-        if (!mList || mCapacity < numLayers) {
-            free(mList);
+        // mLists[0] is NULL only when this is called from the constructor
+        if (!mLists[0] || mCapacity < numLayers) {
+            free(mLists[0]);
             size_t size = sizeofHwcLayerList(mHwc, numLayers);
-            mList = (hwc_layer_list_1_t*)malloc(size);
+            mLists[0] = (hwc_display_contents_1_t*)malloc(size);
             mCapacity = numLayers;
         }
-        mList->flags = HWC_GEOMETRY_CHANGED;
-        mList->numHwLayers = numLayers;
+        hwcFlags(mHwc, mLists[0]) = HWC_GEOMETRY_CHANGED;
+        hwcNumHwLayers(mHwc, mLists[0]) = numLayers;
+        if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
+            mLists[0]->flipFenceFd = -1;
+        }
     }
     return NO_ERROR;
 }
 
 status_t HWComposer::prepare() const {
-    int err = mHwc->prepare(mHwc, mList);
+    int err = hwcPrepare(mHwc, 1,
+            const_cast<hwc_display_contents_1_t**>(mLists));
     if (err == NO_ERROR) {
         size_t numOVLayers = 0;
         size_t numFBLayers = 0;
-        size_t count = mList->numHwLayers;
+        size_t count = getNumLayers();
         for (size_t i=0 ; i<count ; i++) {
             hwc_layer_1_t* l = NULL;
             if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
-                l = &mList->hwLayers[i];
+                l = &mLists[0]->hwLayers[i];
             } else {
                 // mList really has hwc_layer_list_t memory layout
-                hwc_layer_list_t* list = (hwc_layer_list_t*)mList;
-                hwc_layer_t* layer = &list->hwLayers[i];
+                hwc_layer_list_t* list0 = (hwc_layer_list_t*)mLists[0];
+                hwc_layer_t* layer = &list0->hwLayers[i];
                 l = (hwc_layer_1_t*)layer;
             }
             if (l->flags & HWC_SKIP_LAYER) {
@@ -265,59 +398,47 @@
 status_t HWComposer::commit(void* fbDisplay, void* fbSurface) const {
     int err = NO_ERROR;
     if (mHwc) {
-        err = mHwc->set(mHwc, fbDisplay, fbSurface, mList);
-        if (mList) {
-            mList->flags &= ~HWC_GEOMETRY_CHANGED;
+        err = hwcSet(mHwc, fbDisplay, fbSurface, 1,
+                const_cast<hwc_display_contents_1_t**>(mLists));
+        if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
+            if (mLists[0]->flipFenceFd != -1) {
+                close(mLists[0]->flipFenceFd);
+                mLists[0]->flipFenceFd = -1;
+            }
         }
+        hwcFlags(mHwc, mLists[0]) &= ~HWC_GEOMETRY_CHANGED;
     }
     return (status_t)err;
 }
 
 status_t HWComposer::release() const {
     if (mHwc) {
-        if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_0_3)) {
-            mHwc->methods->eventControl(mHwc, HWC_EVENT_VSYNC, 0);
+        if (hwcHasVsyncEvent(mHwc)) {
+            hwcEventControl(mHwc, 0, HWC_EVENT_VSYNC, 0);
         }
-        int err = mHwc->set(mHwc, NULL, NULL, NULL);
-        if (err < 0) {
-            return (status_t)err;
-        }
-
-        if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
-            if (mHwc->methods && mHwc->methods->blank) {
-                err = mHwc->methods->blank(mHwc, 1);
-            }
-        }
-        return (status_t)err;
+        return (status_t)hwcBlank(mHwc, 0, 1);
     }
     return NO_ERROR;
 }
 
 status_t HWComposer::acquire() const {
     if (mHwc) {
-        if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
-            if (mHwc->methods && mHwc->methods->blank) {
-                int err = mHwc->methods->blank(mHwc, 0);
-                return (status_t)err;
-            }
-        }
+        return (status_t)hwcBlank(mHwc, 0, 0);
     }
-
     return NO_ERROR;
 }
 
 status_t HWComposer::disable() {
     if (mHwc) {
-        free(mList);
-        mList = NULL;
-        int err = mHwc->prepare(mHwc, NULL);
+        hwcNumHwLayers(mHwc, mLists[0]) = 0;
+        int err = hwcPrepare(mHwc, 1, mLists);
         return (status_t)err;
     }
     return NO_ERROR;
 }
 
 size_t HWComposer::getNumLayers() const {
-    return mList ? mList->numHwLayers : 0;
+    return mHwc ? hwcNumHwLayers(mHwc, mLists[0]) : 0;
 }
 
 /*
@@ -491,13 +612,13 @@
  * returns an iterator initialized at a given index in the layer list
  */
 HWComposer::LayerListIterator HWComposer::getLayerIterator(size_t index) {
-    if (!mList || index > mList->numHwLayers) {
+    if (index > hwcNumHwLayers(mHwc, mLists[0]))
         return LayerListIterator();
-    }
     if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
-        return LayerListIterator(new HWCLayerVersion1(mList->hwLayers), index);
+        return LayerListIterator(new HWCLayerVersion1(mLists[0]->hwLayers),
+                index);
     } else {
-        hwc_layer_list_t* list0 = (hwc_layer_list_t*)mList;
+        hwc_layer_list_t* list0 = (hwc_layer_list_t*)mLists[0];
         return LayerListIterator(new HWCLayerVersion0(list0->hwLayers), index);
     }
 }
@@ -516,26 +637,26 @@
     return getLayerIterator(getNumLayers());
 }
 
-
-
 void HWComposer::dump(String8& result, char* buffer, size_t SIZE,
         const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
-    if (mHwc && mList) {
+    if (mHwc) {
+        hwc_layer_list_t* list0 = (hwc_layer_list_t*)mLists[0];
+
         result.append("Hardware Composer state:\n");
         result.appendFormat("  mDebugForceFakeVSync=%d\n",
                 mDebugForceFakeVSync);
         result.appendFormat("  numHwLayers=%u, flags=%08x\n",
-                mList->numHwLayers, mList->flags);
+                hwcNumHwLayers(mHwc, mLists[0]), hwcFlags(mHwc, mLists[0]));
         result.append(
                 "   type   |  handle  |   hints  |   flags  | tr | blend |  format  |       source crop         |           frame           name \n"
                 "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
         //      " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
-        for (size_t i=0 ; i<mList->numHwLayers ; i++) {
+        for (size_t i=0 ; i<hwcNumHwLayers(mHwc, mLists[0]) ; i++) {
             hwc_layer_1_t l;
             if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
-                l = mList->hwLayers[i];
+                l = mLists[0]->hwLayers[i];
             } else {
-                hwc_layer_list_t* list0 = (hwc_layer_list_t*)mList;
+                hwc_layer_list_t* list0 = (hwc_layer_list_t*)mLists[0];
                 *(hwc_layer_t*)&l = list0->hwLayers[i];
                 l.acquireFenceFd = l.releaseFenceFd = -1;
             }
diff --git a/services/surfaceflinger/DisplayHardware/HWComposer.h b/services/surfaceflinger/DisplayHardware/HWComposer.h
index ca41bd3..112e120 100644
--- a/services/surfaceflinger/DisplayHardware/HWComposer.h
+++ b/services/surfaceflinger/DisplayHardware/HWComposer.h
@@ -34,7 +34,7 @@
                            struct timespec *remain);
 
 struct hwc_composer_device_1;
-struct hwc_layer_list_1;
+struct hwc_display_contents_1;
 struct hwc_procs;
 
 namespace android {
@@ -42,6 +42,7 @@
 
 class GraphicBuffer;
 class LayerBase;
+class Region;
 class String8;
 class SurfaceFlinger;
 
@@ -55,8 +56,7 @@
         virtual ~EventHandler() {}
     };
 
-    HWComposer(const sp<SurfaceFlinger>& flinger,
-            EventHandler& handler, nsecs_t refreshPeriod);
+    HWComposer(const sp<SurfaceFlinger>& flinger, EventHandler& handler);
     ~HWComposer();
 
     status_t initCheck() const;
@@ -189,6 +189,9 @@
 
     void eventControl(int event, int enabled);
 
+    nsecs_t getRefreshPeriod() const;
+    nsecs_t getRefreshTimestamp() const;
+
     // this class is only used to fake the VSync event on systems that don't
     // have it.
     class VSyncThread : public Thread {
@@ -212,6 +215,8 @@
             const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const;
 
 private:
+    enum { MAX_DISPLAYS = 1 };
+
     LayerListIterator getLayerIterator(size_t index);
 
     struct cb_context;
@@ -225,7 +230,10 @@
     sp<SurfaceFlinger>              mFlinger;
     hw_module_t const*              mModule;
     struct hwc_composer_device_1*   mHwc;
-    struct hwc_layer_list_1*        mList;
+    // invariant: mLists[0] != NULL iff mHwc != NULL
+    // TODO: decide whether mLists[i>0] should be non-NULL when display i is
+    //       not attached/enabled.
+    struct hwc_display_contents_1*  mLists[MAX_DISPLAYS];
     size_t                          mCapacity;
     mutable size_t                  mNumOVLayers;
     mutable size_t                  mNumFBLayers;
@@ -235,6 +243,10 @@
     size_t                          mVSyncCount;
     sp<VSyncThread>                 mVSyncThread;
     bool                            mDebugForceFakeVSync;
+
+    // protected by mLock
+    mutable Mutex mLock;
+    mutable nsecs_t mLastHwVSync;
 };
 
 // ---------------------------------------------------------------------------
diff --git a/services/surfaceflinger/EventThread.cpp b/services/surfaceflinger/EventThread.cpp
index 3d79577..9b61fa9 100644
--- a/services/surfaceflinger/EventThread.cpp
+++ b/services/surfaceflinger/EventThread.cpp
@@ -27,7 +27,6 @@
 #include <utils/String8.h>
 #include <utils/Trace.h>
 
-#include "DisplayHardware.h"
 #include "EventThread.h"
 #include "SurfaceFlinger.h"
 
diff --git a/services/surfaceflinger/EventThread.h b/services/surfaceflinger/EventThread.h
index 2f10cdb..a71d985 100644
--- a/services/surfaceflinger/EventThread.h
+++ b/services/surfaceflinger/EventThread.h
@@ -27,7 +27,6 @@
 #include <utils/threads.h>
 #include <utils/SortedVector.h>
 
-#include "DisplayHardware.h"
 #include "DisplayHardware/PowerHAL.h"
 
 // ---------------------------------------------------------------------------
diff --git a/services/surfaceflinger/Layer.cpp b/services/surfaceflinger/Layer.cpp
index 6617ea2..90e5d9a 100644
--- a/services/surfaceflinger/Layer.cpp
+++ b/services/surfaceflinger/Layer.cpp
@@ -36,7 +36,7 @@
 #include <gui/Surface.h>
 
 #include "clz.h"
-#include "DisplayHardware.h"
+#include "DisplayDevice.h"
 #include "GLExtensions.h"
 #include "Layer.h"
 #include "SurfaceFlinger.h"
@@ -60,7 +60,6 @@
         mCurrentOpacity(true),
         mRefreshPending(false),
         mFrameLatencyNeeded(false),
-        mNeedHwcFence(false),
         mFrameLatencyOffset(0),
         mFormat(PIXEL_FORMAT_NONE),
         mGLExtensions(GLExtensions::getInstance()),
@@ -72,21 +71,11 @@
     glGenTextures(1, &mTextureName);
 }
 
-void Layer::onLayerDisplayed(HWComposer::HWCLayerInterface* layer) {
+void Layer::onLayerDisplayed(const sp<const DisplayDevice>& hw,
+        HWComposer::HWCLayerInterface* layer) {
     if (layer) {
         mSurfaceTexture->setReleaseFence(layer->getAndResetReleaseFenceFd());
     }
-
-    if (mFrameLatencyNeeded) {
-        // we need a DisplayHardware for debugging only right now
-        // XXX: should this be called per DisplayHardware?
-        const DisplayHardware& hw(mFlinger->getDefaultDisplayHardware());
-        mFrameStats[mFrameLatencyOffset].timestamp = mSurfaceTexture->getTimestamp();
-        mFrameStats[mFrameLatencyOffset].set = systemTime();
-        mFrameStats[mFrameLatencyOffset].vsync = hw.getRefreshTimestamp();
-        mFrameLatencyOffset = (mFrameLatencyOffset + 1) % 128;
-        mFrameLatencyNeeded = false;
-    }
 }
 
 void Layer::onFirstRef()
@@ -248,7 +237,7 @@
 }
 
 void Layer::setGeometry(
-        const DisplayHardware& hw,
+    const sp<const DisplayDevice>& hw,
         HWComposer::HWCLayerInterface& layer)
 {
     LayerBaseClient::setGeometry(hw, layer);
@@ -271,7 +260,7 @@
      */
 
     const Transform bufferOrientation(mCurrentTransform);
-    const Transform tr(hw.getTransform() * s.transform * bufferOrientation);
+    const Transform tr(hw->getTransform() * s.transform * bufferOrientation);
 
     // this gives us only the "orientation" component of the transform
     const uint32_t finalTransform = tr.getOrientation();
@@ -285,16 +274,22 @@
     layer.setCrop(computeBufferCrop());
 }
 
-void Layer::setPerFrameData(HWComposer::HWCLayerInterface& layer) {
+void Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
+        HWComposer::HWCLayerInterface& layer) {
     const sp<GraphicBuffer>& buffer(mActiveBuffer);
     // NOTE: buffer can be NULL if the client never drew into this
     // layer yet, or if we ran out of memory
     layer.setBuffer(buffer);
 }
 
-void Layer::setAcquireFence(HWComposer::HWCLayerInterface& layer) {
+void Layer::setAcquireFence(const sp<const DisplayDevice>& hw,
+        HWComposer::HWCLayerInterface& layer) {
     int fenceFd = -1;
-    if (mNeedHwcFence) {
+
+    // TODO: there is a possible optimization here: we only need to set the
+    // acquire fence the first time a new buffer is acquired on EACH display.
+
+    if (layer.getCompositionType() == HWC_OVERLAY) {
         sp<Fence> fence = mSurfaceTexture->getCurrentFence();
         if (fence.get()) {
             fenceFd = fence->dup();
@@ -302,12 +297,11 @@
                 ALOGW("failed to dup layer fence, skipping sync: %d", errno);
             }
         }
-        mNeedHwcFence = false;
     }
     layer.setAcquireFenceFd(fenceFd);
 }
 
-void Layer::onDraw(const DisplayHardware& hw, const Region& clip) const
+void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const
 {
     ATRACE_CALL();
 
@@ -329,7 +323,7 @@
             const sp<LayerBase>& layer(drawingLayers[i]);
             if (layer.get() == static_cast<LayerBase const*>(this))
                 break;
-            under.orSelf( hw.getTransform().transform(layer->visibleRegion) );
+            under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
         }
         // if not everything below us is covered, we plug the holes!
         Region holes(clip.subtract(under));
@@ -517,6 +511,18 @@
     return mQueuedFrames > 0;
 }
 
+void Layer::onPostComposition() {
+    if (mFrameLatencyNeeded) {
+        const HWComposer& hwc = mFlinger->getHwComposer();
+        const size_t offset = mFrameLatencyOffset;
+        mFrameStats[offset].timestamp = mSurfaceTexture->getTimestamp();
+        mFrameStats[offset].set = systemTime();
+        mFrameStats[offset].vsync = hwc.getRefreshTimestamp();
+        mFrameLatencyOffset = (mFrameLatencyOffset + 1) % 128;
+        mFrameLatencyNeeded = false;
+    }
+}
+
 Region Layer::latchBuffer(bool& recomputeVisibleRegions)
 {
     ATRACE_CALL();
@@ -646,7 +652,6 @@
 
         mRefreshPending = true;
         mFrameLatencyNeeded = true;
-        mNeedHwcFence = true;
         if (oldActiveBuffer == NULL) {
              // the first time we receive a buffer, we need to trigger a
              // geometry invalidation.
@@ -723,8 +728,7 @@
 {
     LayerBaseClient::dumpStats(result, buffer, SIZE);
     const size_t o = mFrameLatencyOffset;
-    const DisplayHardware& hw(mFlinger->getDefaultDisplayHardware());
-    const nsecs_t period = hw.getRefreshPeriod();
+    const nsecs_t period = mFlinger->getHwComposer().getRefreshPeriod();
     result.appendFormat("%lld\n", period);
     for (size_t i=0 ; i<128 ; i++) {
         const size_t index = (o+i) % 128;
@@ -764,8 +768,8 @@
         // apply to all displays.
         // This is why we use the default display here. This is not an
         // oversight.
-        const DisplayHardware& hw(mFlinger->getDefaultDisplayHardware());
-        const Transform& planeTransform(hw.getTransform());
+        sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
+        const Transform& planeTransform(hw->getTransform());
         orientation = planeTransform.getOrientation();
         if (orientation & Transform::ROT_INVALID) {
             orientation = 0;
diff --git a/services/surfaceflinger/Layer.h b/services/surfaceflinger/Layer.h
index 239250d..d24013b 100644
--- a/services/surfaceflinger/Layer.h
+++ b/services/surfaceflinger/Layer.h
@@ -64,12 +64,18 @@
     bool isFixedSize() const;
 
     // LayerBase interface
-    virtual void setGeometry(const DisplayHardware& hw,
+    virtual void setGeometry(const sp<const DisplayDevice>& hw,
             HWComposer::HWCLayerInterface& layer);
-    virtual void setPerFrameData(HWComposer::HWCLayerInterface& layer);
-    virtual void setAcquireFence(HWComposer::HWCLayerInterface& layer);
+    virtual void setPerFrameData(const sp<const DisplayDevice>& hw,
+            HWComposer::HWCLayerInterface& layer);
+    virtual void setAcquireFence(const sp<const DisplayDevice>& hw,
+            HWComposer::HWCLayerInterface& layer);
+    virtual void onLayerDisplayed(const sp<const DisplayDevice>& hw,
+            HWComposer::HWCLayerInterface* layer);
+    virtual bool onPreComposition();
+    virtual void onPostComposition();
 
-    virtual void onDraw(const DisplayHardware& hw, const Region& clip) const;
+    virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const;
     virtual uint32_t doTransaction(uint32_t transactionFlags);
     virtual Region latchBuffer(bool& recomputeVisibleRegions);
     virtual bool isOpaque() const;
@@ -82,9 +88,6 @@
     // LayerBaseClient interface
     virtual wp<IBinder> getSurfaceTextureBinder() const;
 
-    virtual void onLayerDisplayed(HWComposer::HWCLayerInterface* layer);
-    virtual bool onPreComposition();
-
     // only for debugging
     inline const sp<GraphicBuffer>& getActiveBuffer() const { return mActiveBuffer; }
 
@@ -121,7 +124,6 @@
     bool mCurrentOpacity;
     bool mRefreshPending;
     bool mFrameLatencyNeeded;
-    bool mNeedHwcFence;
     int mFrameLatencyOffset;
 
     struct Statistics {
diff --git a/services/surfaceflinger/LayerBase.cpp b/services/surfaceflinger/LayerBase.cpp
index f654445..e1477a9 100644
--- a/services/surfaceflinger/LayerBase.cpp
+++ b/services/surfaceflinger/LayerBase.cpp
@@ -33,7 +33,7 @@
 #include "LayerBase.h"
 #include "Layer.h"
 #include "SurfaceFlinger.h"
-#include "DisplayHardware.h"
+#include "DisplayDevice.h"
 
 namespace android {
 
@@ -223,11 +223,11 @@
     return flags;
 }
 
-void LayerBase::computeGeometry(const DisplayHardware& hw, LayerMesh* mesh) const
+void LayerBase::computeGeometry(const sp<const DisplayDevice>& hw, LayerMesh* mesh) const
 {
     const Layer::State& s(drawingState());
-    const Transform tr(hw.getTransform() * s.transform);
-    const uint32_t hw_h = hw.getHeight();
+    const Transform tr(hw->getTransform() * s.transform);
+    const uint32_t hw_h = hw->getHeight();
     const Rect& crop(s.active.crop);
     Rect win(s.active.w, s.active.h);
     if (!crop.isEmpty()) {
@@ -260,7 +260,7 @@
 }
 
 void LayerBase::setGeometry(
-        const DisplayHardware& hw,
+    const sp<const DisplayDevice>& hw,
         HWComposer::HWCLayerInterface& layer)
 {
     layer.setDefaultState();
@@ -281,7 +281,7 @@
                 HWC_BLENDING_COVERAGE);
     }
 
-    const Transform& tr = hw.getTransform();
+    const Transform& tr = hw->getTransform();
     Rect transformedBounds(computeBounds());
     transformedBounds = tr.transform(transformedBounds);
 
@@ -291,11 +291,13 @@
     layer.setVisibleRegionScreen(tr.transform(visibleRegion));
 }
 
-void LayerBase::setPerFrameData(HWComposer::HWCLayerInterface& layer) {
+void LayerBase::setPerFrameData(const sp<const DisplayDevice>& hw,
+        HWComposer::HWCLayerInterface& layer) {
     layer.setBuffer(0);
 }
 
-void LayerBase::setAcquireFence(HWComposer::HWCLayerInterface& layer) {
+void LayerBase::setAcquireFence(const sp<const DisplayDevice>& hw,
+        HWComposer::HWCLayerInterface& layer) {
     layer.setAcquireFenceFd(-1);
 }
 
@@ -309,22 +311,20 @@
     return mFiltering;
 }
 
-void LayerBase::draw(const DisplayHardware& hw, const Region& clip) const
+void LayerBase::draw(const sp<const DisplayDevice>& hw, const Region& clip) const
 {
     onDraw(hw, clip);
 }
 
-void LayerBase::drawForSreenShot(const DisplayHardware& hw)
+void LayerBase::draw(const sp<const DisplayDevice>& hw)
 {
-    setFiltering(true);
-    onDraw( hw, Region(hw.bounds()) );
-    setFiltering(false);
+    onDraw( hw, Region(hw->bounds()) );
 }
 
-void LayerBase::clearWithOpenGL(const DisplayHardware& hw, const Region& clip,
+void LayerBase::clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
         GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) const
 {
-    const uint32_t fbHeight = hw.getHeight();
+    const uint32_t fbHeight = hw->getHeight();
     glColor4f(red,green,blue,alpha);
 
     glDisable(GL_TEXTURE_EXTERNAL_OES);
@@ -338,14 +338,14 @@
     glDrawArrays(GL_TRIANGLE_FAN, 0, mesh.getVertexCount());
 }
 
-void LayerBase::clearWithOpenGL(const DisplayHardware& hw, const Region& clip) const
+void LayerBase::clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const
 {
     clearWithOpenGL(hw, clip, 0,0,0,0);
 }
 
-void LayerBase::drawWithOpenGL(const DisplayHardware& hw, const Region& clip) const
+void LayerBase::drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const
 {
-    const uint32_t fbHeight = hw.getHeight();
+    const uint32_t fbHeight = hw->getHeight();
     const State& s(drawingState());
 
     GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
diff --git a/services/surfaceflinger/LayerBase.h b/services/surfaceflinger/LayerBase.h
index 7bf634e..6394542 100644
--- a/services/surfaceflinger/LayerBase.h
+++ b/services/surfaceflinger/LayerBase.h
@@ -33,7 +33,6 @@
 
 #include <private/gui/LayerState.h>
 
-#include "DisplayHardware.h"
 #include "Transform.h"
 #include "DisplayHardware/HWComposer.h"
 
@@ -42,7 +41,7 @@
 // ---------------------------------------------------------------------------
 
 class Client;
-class DisplayHardware;
+class DisplayDevice;
 class GraphicBuffer;
 class Layer;
 class LayerBaseClient;
@@ -124,7 +123,7 @@
             uint32_t getTransactionFlags(uint32_t flags);
             uint32_t setTransactionFlags(uint32_t flags);
 
-            void computeGeometry(const DisplayHardware& hw, LayerMesh* mesh) const;
+            void computeGeometry(const sp<const DisplayDevice>& hw, LayerMesh* mesh) const;
             Rect computeBounds() const;
 
 
@@ -133,10 +132,12 @@
 
     virtual const char* getTypeId() const { return "LayerBase"; }
 
-    virtual void setGeometry(const DisplayHardware& hw,
+    virtual void setGeometry(const sp<const DisplayDevice>& hw,
             HWComposer::HWCLayerInterface& layer);
-    virtual void setPerFrameData(HWComposer::HWCLayerInterface& layer);
-    virtual void setAcquireFence(HWComposer::HWCLayerInterface& layer);
+    virtual void setPerFrameData(const sp<const DisplayDevice>& hw,
+            HWComposer::HWCLayerInterface& layer);
+    virtual void setAcquireFence(const sp<const DisplayDevice>& hw,
+            HWComposer::HWCLayerInterface& layer);
 
     /**
      * draw - performs some global clipping optimizations
@@ -144,13 +145,13 @@
      * Typically this method is not overridden, instead implement onDraw()
      * to perform the actual drawing.  
      */
-    virtual void draw(const DisplayHardware& hw, const Region& clip) const;
-    virtual void drawForSreenShot(const DisplayHardware& hw);
+    virtual void draw(const sp<const DisplayDevice>& hw, const Region& clip) const;
+    virtual void draw(const sp<const DisplayDevice>& hw);
     
     /**
      * onDraw - draws the surface.
      */
-    virtual void onDraw(const DisplayHardware& hw, const Region& clip) const = 0;
+    virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const = 0;
     
     /**
      * initStates - called just after construction
@@ -217,13 +218,18 @@
 
     /** called after page-flip
      */
-    virtual void onLayerDisplayed(HWComposer::HWCLayerInterface* layer) { }
+    virtual void onLayerDisplayed(const sp<const DisplayDevice>& hw,
+            HWComposer::HWCLayerInterface* layer) { }
 
     /** called before composition.
      * returns true if the layer has pending updates.
      */
     virtual bool onPreComposition() { return false; }
 
+    /** called before composition.
+     */
+    virtual void onPostComposition() { }
+
     /** always call base class first */
     virtual void dump(String8& result, char* scratch, size_t size) const;
     virtual void shortDump(String8& result, char* scratch, size_t size) const;
@@ -241,15 +247,15 @@
     inline  const State&    currentState() const    { return mCurrentState; }
     inline  State&          currentState()          { return mCurrentState; }
 
-    void clearWithOpenGL(const DisplayHardware& hw, const Region& clip) const;
+    void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
+
+    void setFiltering(bool filtering);
+    bool getFiltering() const;
 
 protected:
-          void clearWithOpenGL(const DisplayHardware& hw, const Region& clip,
+          void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
                   GLclampf r, GLclampf g, GLclampf b, GLclampf alpha) const;
-          void drawWithOpenGL(const DisplayHardware& hw, const Region& clip) const;
-
-          void setFiltering(bool filtering);
-          bool getFiltering() const;
+          void drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
 
                 sp<SurfaceFlinger> mFlinger;
 
diff --git a/services/surfaceflinger/LayerDim.cpp b/services/surfaceflinger/LayerDim.cpp
index 5c37d01..087d851 100644
--- a/services/surfaceflinger/LayerDim.cpp
+++ b/services/surfaceflinger/LayerDim.cpp
@@ -18,6 +18,9 @@
 #include <stdint.h>
 #include <sys/types.h>
 
+#include <GLES/gl.h>
+#include <GLES/glext.h>
+
 #include <utils/Errors.h>
 #include <utils/Log.h>
 
@@ -25,7 +28,7 @@
 
 #include "LayerDim.h"
 #include "SurfaceFlinger.h"
-#include "DisplayHardware.h"
+#include "DisplayDevice.h"
 
 namespace android {
 // ---------------------------------------------------------------------------
@@ -40,12 +43,12 @@
 {
 }
 
-void LayerDim::onDraw(const DisplayHardware& hw, const Region& clip) const
+void LayerDim::onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const
 {
     const State& s(drawingState());
     if (s.alpha>0) {
         const GLfloat alpha = s.alpha/255.0f;
-        const uint32_t fbHeight = hw.getHeight();
+        const uint32_t fbHeight = hw->getHeight();
         glDisable(GL_TEXTURE_EXTERNAL_OES);
         glDisable(GL_TEXTURE_2D);
 
diff --git a/services/surfaceflinger/LayerDim.h b/services/surfaceflinger/LayerDim.h
index 2d9eaef..fbfb2fb 100644
--- a/services/surfaceflinger/LayerDim.h
+++ b/services/surfaceflinger/LayerDim.h
@@ -36,7 +36,7 @@
                         const sp<Client>& client);
         virtual ~LayerDim();
 
-    virtual void onDraw(const DisplayHardware& hw, const Region& clip) const;
+    virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const;
     virtual bool isOpaque() const         { return false; }
     virtual bool isSecure() const         { return false; }
     virtual bool isProtectedByApp() const { return false; }
diff --git a/services/surfaceflinger/LayerScreenshot.cpp b/services/surfaceflinger/LayerScreenshot.cpp
index bed8206..da2de76 100644
--- a/services/surfaceflinger/LayerScreenshot.cpp
+++ b/services/surfaceflinger/LayerScreenshot.cpp
@@ -18,6 +18,9 @@
 #include <stdint.h>
 #include <sys/types.h>
 
+#include <GLES/gl.h>
+#include <GLES/glext.h>
+
 #include <utils/Errors.h>
 #include <utils/Log.h>
 
@@ -25,7 +28,7 @@
 
 #include "LayerScreenshot.h"
 #include "SurfaceFlinger.h"
-#include "DisplayHardware.h"
+#include "DisplayDevice.h"
 
 
 namespace android {
@@ -105,12 +108,12 @@
     return LayerBaseClient::doTransaction(flags);
 }
 
-void LayerScreenshot::onDraw(const DisplayHardware& hw, const Region& clip) const
+void LayerScreenshot::onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const
 {
     const State& s(drawingState());
     if (s.alpha>0) {
         const GLfloat alpha = s.alpha/255.0f;
-        const uint32_t fbHeight = hw.getHeight();
+        const uint32_t fbHeight = hw->getHeight();
 
         if (s.alpha == 0xFF) {
             glDisable(GL_BLEND);
diff --git a/services/surfaceflinger/LayerScreenshot.h b/services/surfaceflinger/LayerScreenshot.h
index 0ddb376..c06c23c 100644
--- a/services/surfaceflinger/LayerScreenshot.h
+++ b/services/surfaceflinger/LayerScreenshot.h
@@ -43,7 +43,7 @@
 
     virtual void initStates(uint32_t w, uint32_t h, uint32_t flags);
     virtual uint32_t doTransaction(uint32_t flags);
-    virtual void onDraw(const DisplayHardware& hw, const Region& clip) const;
+    virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const;
     virtual bool isOpaque() const         { return false; }
     virtual bool isSecure() const         { return false; }
     virtual bool isProtectedByApp() const { return false; }
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 6cf1128..1f9d694 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -34,8 +34,9 @@
 
 #include <ui/DisplayInfo.h>
 
-#include <gui/IDisplayEventConnection.h>
 #include <gui/BitTube.h>
+#include <gui/BufferQueue.h>
+#include <gui/IDisplayEventConnection.h>
 #include <gui/SurfaceTextureClient.h>
 
 #include <ui/GraphicBufferAllocator.h>
@@ -50,7 +51,7 @@
 
 #include "clz.h"
 #include "DdmConnection.h"
-#include "DisplayHardware.h"
+#include "DisplayDevice.h"
 #include "Client.h"
 #include "EventThread.h"
 #include "GLExtensions.h"
@@ -82,10 +83,10 @@
         mTransactionFlags(0),
         mTransationPending(false),
         mLayersRemoved(false),
+        mRepaintEverything(0),
         mBootTime(systemTime()),
         mVisibleRegionsDirty(false),
         mHwWorkListDirty(false),
-        mElectronBeamAnimationMode(0),
         mDebugRegion(0),
         mDebugDDMS(0),
         mDebugDisableHWC(0),
@@ -105,18 +106,14 @@
     property_get("debug.sf.showupdates", value, "0");
     mDebugRegion = atoi(value);
 
-#ifdef DDMS_DEBUGGING
     property_get("debug.sf.ddms", value, "0");
     mDebugDDMS = atoi(value);
     if (mDebugDDMS) {
         DdmConnection::start(getServiceName());
     }
-#else
-#warning "DDMS_DEBUGGING disabled"
-#endif
 
-    ALOGI_IF(mDebugRegion,       "showupdates enabled");
-    ALOGI_IF(mDebugDDMS,         "DDMS debugging enabled");
+    ALOGI_IF(mDebugRegion, "showupdates enabled");
+    ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
 }
 
 void SurfaceFlinger::onFirstRef()
@@ -132,7 +129,6 @@
 
 SurfaceFlinger::~SurfaceFlinger()
 {
-    glDeleteTextures(1, &mWormholeTexName);
     EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
     eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
     eglTerminate(display);
@@ -308,18 +304,6 @@
         }
     } pack565;
 
-    const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
-    const uint16_t g1 = pack565(0x17,0x2f,0x17);
-    const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
-    glGenTextures(1, &mWormholeTexName);
-    glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
-    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
-    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
-    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
-    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
-            GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
-
     const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
     glGenTextures(1, &mProtectedTexName);
     glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
@@ -368,22 +352,25 @@
 
     // Initialize the main display
     // create native window to main display
-    sp<FramebufferSurface> anw = FramebufferSurface::create();
-    ANativeWindow* const window = anw.get();
-    if (!window) {
+    sp<FramebufferSurface> fbs = FramebufferSurface::create();
+    if (fbs == NULL) {
         ALOGE("Display subsystem failed to initialize. check logs. exiting...");
         exit(0);
     }
 
+    sp<SurfaceTextureClient> stc(new SurfaceTextureClient(static_cast<sp<ISurfaceTexture> >(fbs->getBufferQueue())));
+
     // initialize the config and context
     int format;
-    window->query(window, NATIVE_WINDOW_FORMAT, &format);
+    ANativeWindow* const anw = stc.get();
+    anw->query(anw, NATIVE_WINDOW_FORMAT, &format);
     mEGLConfig  = selectEGLConfig(mEGLDisplay, format);
     mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
 
     // initialize our main display hardware
-    DisplayHardware* const hw = new DisplayHardware(this, 0, anw, mEGLConfig);
-    mDisplayHardwares[0] = hw;
+    mCurrentState.displays.add(DisplayDevice::DISPLAY_ID_MAIN, DisplayDeviceState());
+    sp<DisplayDevice> hw = new DisplayDevice(this, DisplayDevice::DISPLAY_ID_MAIN, anw, fbs, mEGLConfig);
+    mDisplays.add(DisplayDevice::DISPLAY_ID_MAIN, hw);
 
     //  initialize OpenGL ES
     EGLSurface surface = hw->getEGLSurface();
@@ -394,9 +381,10 @@
     mEventQueue.setEventThread(mEventThread);
 
     // initialize the H/W composer
-    mHwc = new HWComposer(this,
-            *static_cast<HWComposer::EventHandler *>(this),
-            hw->getRefreshPeriod());
+    mHwc = new HWComposer(this, *static_cast<HWComposer::EventHandler *>(this));
+
+    // initialize our drawing state
+    mDrawingState = mCurrentState;
 
     // We're now ready to accept clients...
     mReadyToRunBarrier.open();
@@ -466,11 +454,21 @@
 }
 
 status_t SurfaceFlinger::getDisplayInfo(DisplayID dpy, DisplayInfo* info) {
+    // TODO: this is here only for compatibility -- show go away eventually.
     if (uint32_t(dpy) >= 2) {
         return BAD_INDEX;
     }
-    const DisplayHardware& hw(getDefaultDisplayHardware());
-    return hw.getInfo(info);
+    sp<const DisplayDevice> hw(getDefaultDisplayDevice());
+    info->w = hw->getWidth();
+    info->h = hw->getHeight();
+    info->xdpi = hw->getDpiX();
+    info->ydpi = hw->getDpiY();
+    info->fps = float(1e9 / getHwComposer().getRefreshPeriod());
+    info->density = hw->getDensity();
+    info->orientation = hw->getOrientation();
+    // TODO: this needs to go away (currently needed only by webkit)
+    getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
+    return NO_ERROR;
 }
 
 // ----------------------------------------------------------------------------
@@ -480,14 +478,13 @@
 }
 
 void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> display) {
-    const DisplayHardware& hw(getDefaultDisplayHardware());
     EGLSurface result = EGL_NO_SURFACE;
     EGLSurface old_surface = EGL_NO_SURFACE;
     sp<SurfaceTextureClient> stc;
 
     if (display != NULL) {
         stc = new SurfaceTextureClient(display);
-        result = eglCreateWindowSurface(hw.getEGLDisplay(),
+        result = eglCreateWindowSurface(mEGLDisplay,
                 mEGLConfig, (EGLNativeWindowType)stc.get(), NULL);
         ALOGE_IF(result == EGL_NO_SURFACE,
                 "eglCreateWindowSurface failed (ISurfaceTexture=%p)",
@@ -505,7 +502,7 @@
     if (old_surface != EGL_NO_SURFACE) {
         // Note: EGL allows to destroy an object while its current
         // it will fail to become current next time though.
-        eglDestroySurface(hw.getEGLDisplay(), old_surface);
+        eglDestroySurface(mEGLDisplay, old_surface);
     }
 }
 
@@ -552,8 +549,6 @@
 }
 
 void SurfaceFlinger::onVSyncReceived(int dpy, nsecs_t timestamp) {
-    DisplayHardware& hw(const_cast<DisplayHardware&>(getDisplayHardware(dpy)));
-    hw.onVSyncReceived(timestamp);
     mEventThread->onVSyncReceived(dpy, timestamp);
 }
 
@@ -579,89 +574,114 @@
     const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
     uint32_t transactionFlags = peekTransactionFlags(mask);
     if (transactionFlags) {
-        Region dirtyRegion;
-        dirtyRegion = handleTransaction(transactionFlags);
-        // XXX: dirtyRegion should be per screen
-        mDirtyRegion |= dirtyRegion;
+        handleTransaction(transactionFlags);
     }
 }
 
 void SurfaceFlinger::handleMessageInvalidate() {
-    Region dirtyRegion;
-    dirtyRegion = handlePageFlip();
-    // XXX: dirtyRegion should be per screen
-    mDirtyRegion |= dirtyRegion;
+    handlePageFlip();
 }
 
 void SurfaceFlinger::handleMessageRefresh() {
     handleRefresh();
 
-    if (mVisibleRegionsDirty) {
-        Region opaqueRegion;
-        Region dirtyRegion;
-        const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
-        computeVisibleRegions(currentLayers, dirtyRegion, opaqueRegion);
-        mDirtyRegion.orSelf(dirtyRegion);
+    if (CC_UNLIKELY(mVisibleRegionsDirty)) {
+        mVisibleRegionsDirty = false;
+        invalidateHwcGeometry();
 
         /*
          *  rebuild the visible layer list per screen
          */
 
-        // TODO: iterate through all displays
-        DisplayHardware& hw(const_cast<DisplayHardware&>(getDisplayHardware(0)));
+        const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
+        for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
+            const sp<DisplayDevice>& hw(mDisplays[dpy]);
+            Region opaqueRegion;
+            Region dirtyRegion;
+            computeVisibleRegions(currentLayers,
+                    hw->getLayerStack(), dirtyRegion, opaqueRegion);
+            hw->dirtyRegion.orSelf(dirtyRegion);
 
-        Vector< sp<LayerBase> > layersSortedByZ;
-        const size_t count = currentLayers.size();
-        for (size_t i=0 ; i<count ; i++) {
-            if (!currentLayers[i]->visibleRegion.isEmpty()) {
-                // TODO: also check that this layer is associated to this display
-                layersSortedByZ.add(currentLayers[i]);
+            Vector< sp<LayerBase> > layersSortedByZ;
+            const size_t count = currentLayers.size();
+            for (size_t i=0 ; i<count ; i++) {
+                const Layer::State& s(currentLayers[i]->drawingState());
+                if (s.layerStack == hw->getLayerStack()) {
+                    if (!currentLayers[i]->visibleRegion.isEmpty()) {
+                        layersSortedByZ.add(currentLayers[i]);
+                    }
+                }
+            }
+            hw->setVisibleLayersSortedByZ(layersSortedByZ);
+            hw->undefinedRegion.set(hw->getBounds());
+            hw->undefinedRegion.subtractSelf(hw->getTransform().transform(opaqueRegion));
+        }
+    }
+
+    HWComposer& hwc(getHwComposer());
+    if (hwc.initCheck() == NO_ERROR) {
+        // build the h/w work list
+        const bool workListsDirty = mHwWorkListDirty;
+        mHwWorkListDirty = false;
+        for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
+            sp<const DisplayDevice> hw(mDisplays[dpy]);
+            const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
+            const size_t count = currentLayers.size();
+
+            hwc.createWorkList(count); // FIXME: the worklist should include enough space for all layer of all displays
+
+            HWComposer::LayerListIterator cur = hwc.begin();
+            const HWComposer::LayerListIterator end = hwc.end();
+            for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
+                const sp<LayerBase>& layer(currentLayers[i]);
+
+                if (CC_UNLIKELY(workListsDirty)) {
+                    layer->setGeometry(hw, *cur);
+                    if (mDebugDisableHWC || mDebugRegion) {
+                        cur->setSkip(true);
+                    }
+                }
+
+                /*
+                 * update the per-frame h/w composer data for each layer
+                 * and build the transparent region of the FB
+                 */
+                layer->setPerFrameData(hw, *cur);
             }
         }
-        hw.setVisibleLayersSortedByZ(layersSortedByZ);
-
-
-        // FIXME: mWormholeRegion needs to be calculated per screen
-        //const DisplayHardware& hw(getDefaultDisplayHardware()); // XXX: we can't keep that here
-        mWormholeRegion = Region(hw.getBounds()).subtract(
-                hw.getTransform().transform(opaqueRegion) );
-        mVisibleRegionsDirty = false;
-        invalidateHwcGeometry();
+        status_t err = hwc.prepare();
+        ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
     }
 
+    const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
+    for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
+        const sp<DisplayDevice>& hw(mDisplays[dpy]);
 
-    // XXX: dirtyRegion should be per screen, we should check all of them
-    if (mDirtyRegion.isEmpty()) {
-        return;
-    }
+        // transform the dirty region into this screen's coordinate space
+        const Transform& planeTransform(hw->getTransform());
+        Region dirtyRegion;
+        if (repaintEverything) {
+            dirtyRegion.set(hw->bounds());
+        } else {
+            dirtyRegion = planeTransform.transform(hw->dirtyRegion);
+            dirtyRegion.andSelf(hw->bounds());
+        }
+        hw->dirtyRegion.clear();
 
-    // TODO: iterate through all displays
-    const DisplayHardware& hw(getDisplayHardware(0));
-
-    // XXX: dirtyRegion should be per screen
-    // transform the dirty region into this screen's coordinate space
-    const Transform& planeTransform(hw.getTransform());
-    mDirtyRegion = planeTransform.transform(mDirtyRegion);
-    mDirtyRegion.orSelf(getAndClearInvalidateRegion());
-    mDirtyRegion.andSelf(hw.bounds());
-
-
-    if (CC_UNLIKELY(mHwWorkListDirty)) {
-        // build the h/w work list
-        handleWorkList(hw);
-    }
-
-    if (CC_LIKELY(hw.canDraw())) {
-        // repaint the framebuffer (if needed)
-        handleRepaint(hw);
+        if (!dirtyRegion.isEmpty()) {
+            if (hw->canDraw()) {
+                // repaint the framebuffer (if needed)
+                handleRepaint(hw, dirtyRegion);
+            }
+        }
         // inform the h/w that we're done compositing
-        hw.compositionComplete();
-        postFramebuffer();
-    } else {
-        // pretend we did the post
-        hw.compositionComplete();
+        hw->compositionComplete();
     }
 
+    postFramebuffer();
+
+
+#if 1
     // render to the external display if we have one
     EGLSurface externalDisplaySurface = getExternalDisplaySurface();
     if (externalDisplaySurface != EGL_NO_SURFACE) {
@@ -681,17 +701,18 @@
             glMatrixMode(GL_MODELVIEW);
             glLoadIdentity();
 
-            const Vector< sp<LayerBase> >& layers( hw.getVisibleLayersSortedByZ() );
+            const sp<DisplayDevice>& hw(getDisplayDevice(0));
+            const Vector< sp<LayerBase> >& layers( hw->getVisibleLayersSortedByZ() );
             const size_t count = layers.size();
             for (size_t i=0 ; i<count ; ++i) {
                 const sp<LayerBase>& layer(layers[i]);
-                layer->drawForSreenShot(hw);
+                layer->draw(hw);
             }
 
             success = eglSwapBuffers(eglGetCurrentDisplay(), externalDisplaySurface);
             ALOGE_IF(!success, "external display eglSwapBuffers failed");
 
-            hw.compositionComplete();
+            hw->compositionComplete();
         }
 
         success = eglMakeCurrent(eglGetCurrentDisplay(),
@@ -699,63 +720,68 @@
 
         ALOGE_IF(!success, "eglMakeCurrent -> internal failed");
     }
+#endif
 
 }
 
 void SurfaceFlinger::postFramebuffer()
 {
     ATRACE_CALL();
-    // mSwapRegion can be empty here is some cases, for instance if a hidden
-    // or fully transparent window is updating.
-    // in that case, we need to flip anyways to not risk a deadlock with
-    // h/w composer.
 
-    const DisplayHardware& hw(getDefaultDisplayHardware());
-    HWComposer& hwc(getHwComposer());
-    const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
-    size_t numLayers = layers.size();
     const nsecs_t now = systemTime();
     mDebugInSwapBuffers = now;
 
-    if (hwc.initCheck() == NO_ERROR) {
-        HWComposer::LayerListIterator cur = hwc.begin();
-        const HWComposer::LayerListIterator end = hwc.end();
-        for (size_t i = 0; cur != end && i < numLayers; ++i, ++cur) {
-            if (cur->getCompositionType() == HWC_OVERLAY) {
-                layers[i]->setAcquireFence(*cur);
-            } else {
-                cur->setAcquireFenceFd(-1);
+    HWComposer& hwc(getHwComposer());
+
+    for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
+        const sp<DisplayDevice>& hw(mDisplays[dpy]);
+        if (hwc.initCheck() == NO_ERROR) {
+            const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
+            const size_t count = currentLayers.size();
+            HWComposer::LayerListIterator cur = hwc.begin();
+            const HWComposer::LayerListIterator end = hwc.end();
+            for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
+                const sp<LayerBase>& layer(currentLayers[i]);
+                layer->setAcquireFence(hw, *cur);
             }
         }
+        hw->flip(hw->swapRegion);
+        hw->swapRegion.clear();
     }
 
-    hw.flip(mSwapRegion);
-
     if (hwc.initCheck() == NO_ERROR) {
-        hwc.commit(mEGLDisplay, hw.getEGLSurface());
-        HWComposer::LayerListIterator cur = hwc.begin();
-        const HWComposer::LayerListIterator end = hwc.end();
-        for (size_t i = 0; cur != end && i < numLayers; ++i, ++cur) {
-            layers[i]->onLayerDisplayed(&*cur);
+        // FIXME: eventually commit() won't take arguments
+        hwc.commit(mEGLDisplay, getDefaultDisplayDevice()->getEGLSurface());
+    }
+
+    for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
+        sp<const DisplayDevice> hw(mDisplays[dpy]);
+        const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
+        const size_t count = currentLayers.size();
+        if (hwc.initCheck() == NO_ERROR) {
+            HWComposer::LayerListIterator cur = hwc.begin();
+            const HWComposer::LayerListIterator end = hwc.end();
+            for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
+                currentLayers[i]->onLayerDisplayed(hw, &*cur);
+            }
+        } else {
+            eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
+            for (size_t i = 0; i < count; i++) {
+                currentLayers[i]->onLayerDisplayed(hw, NULL);
+            }
         }
-    } else {
-        eglSwapBuffers(mEGLDisplay, hw.getEGLSurface());
-        for (size_t i = 0; i < numLayers; i++) {
-            layers[i]->onLayerDisplayed(NULL);
-        }
+
+        // FIXME: we need to call eglSwapBuffers() on displays that have GL composition
     }
 
     mLastSwapBufferTime = systemTime() - now;
     mDebugInSwapBuffers = 0;
-    mSwapRegion.clear();
 }
 
-Region SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
+void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
 {
     ATRACE_CALL();
 
-    Region dirtyRegion;
-
     Mutex::Autolock _l(mStateLock);
     const nsecs_t now = systemTime();
     mDebugInTransaction = now;
@@ -768,19 +794,16 @@
 
     const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
     transactionFlags = getTransactionFlags(mask);
-    dirtyRegion = handleTransactionLocked(transactionFlags);
+    handleTransactionLocked(transactionFlags);
 
     mLastTransactionTime = systemTime() - now;
     mDebugInTransaction = 0;
     invalidateHwcGeometry();
     // here the transaction has been committed
-
-    return dirtyRegion;
 }
 
-Region SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
+void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
 {
-    Region dirtyRegion;
     const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
     const size_t count = currentLayers.size();
 
@@ -807,17 +830,51 @@
      */
 
     if (transactionFlags & eTransactionNeeded) {
-        if (mCurrentState.orientation != mDrawingState.orientation) {
-            // the orientation has changed, recompute all visible regions
-            // and invalidate everything.
-
-            const int dpy = 0; // TODO: should be a parameter
-            DisplayHardware& hw(const_cast<DisplayHardware&>(getDisplayHardware(dpy)));
-            hw.setOrientation(mCurrentState.orientation);
-
-            // FIXME: mVisibleRegionsDirty & mDirtyRegion should this be per DisplayHardware?
+        // here we take advantage of Vector's copy-on-write semantics to
+        // improve performance by skipping the transaction entirely when
+        // know that the lists are identical
+        const KeyedVector<int32_t, DisplayDeviceState>& curr(mCurrentState.displays);
+        const KeyedVector<int32_t, DisplayDeviceState>& draw(mDrawingState.displays);
+        if (!curr.isIdenticalTo(draw)) {
             mVisibleRegionsDirty = true;
-            mDirtyRegion.set(hw.bounds());
+            const size_t cc = curr.size();
+            const size_t dc = draw.size();
+
+            // find the displays that were removed
+            // (ie: in drawing state but not in current state)
+            // also handle displays that changed
+            // (ie: displays that are in both lists)
+            for (size_t i=0 ; i<dc ; i++) {
+                if (curr.indexOfKey(draw[i].id) < 0) {
+                    // in drawing state but not in current state
+                    if (draw[i].id != DisplayDevice::DISPLAY_ID_MAIN) {
+                        mDisplays.removeItem(draw[i].id);
+                    } else {
+                        ALOGW("trying to remove the main display");
+                    }
+                } else {
+                    // this display is in both lists. see if something changed.
+                    const DisplayDeviceState& state(curr[i]);
+                    if (state.layerStack != draw[i].layerStack) {
+                        const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
+                        //disp->setLayerStack(state.layerStack); // FIXME: set layer stack
+                    }
+                    if (curr[i].orientation != draw[i].orientation) {
+                        const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
+                        disp->setOrientation(state.orientation);
+                    }
+                }
+            }
+
+            // find displays that were added
+            // (ie: in current state but not in drawing state)
+            for (size_t i=0 ; i<cc ; i++) {
+                if (mDrawingState.displays.indexOfKey(curr[i].id) < 0) {
+                    // FIXME: we need to pass the surface here
+                    sp<DisplayDevice> disp = new DisplayDevice(this, curr[i].id, 0, 0, mEGLConfig);
+                    mDisplays.add(curr[i].id, disp);
+                }
+            }
         }
 
         if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
@@ -834,21 +891,21 @@
             const size_t count = previousLayers.size();
             for (size_t i=0 ; i<count ; i++) {
                 const sp<LayerBase>& layer(previousLayers[i]);
-                if (currentLayers.indexOf( layer ) < 0) {
+                if (currentLayers.indexOf(layer) < 0) {
                     // this layer is not visible anymore
-                    // TODO: we could traverse the tree from front to back and compute the actual visible region
+                    // TODO: we could traverse the tree from front to back and
+                    //       compute the actual visible region
                     // TODO: we could cache the transformed region
                     Layer::State front(layer->drawingState());
                     Region visibleReg = front.transform.transform(
                             Region(Rect(front.active.w, front.active.h)));
-                    dirtyRegion.orSelf(visibleReg);
+                    invalidateLayerStack(front.layerStack, visibleReg);
                 }
             }
         }
     }
 
     commitTransaction();
-    return dirtyRegion;
 }
 
 void SurfaceFlinger::commitTransaction()
@@ -867,7 +924,8 @@
 }
 
 void SurfaceFlinger::computeVisibleRegions(
-    const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
+        const LayerVector& currentLayers, uint32_t layerStack,
+        Region& outDirtyRegion, Region& outOpaqueRegion)
 {
     ATRACE_CALL();
 
@@ -875,7 +933,7 @@
     Region aboveCoveredLayers;
     Region dirty;
 
-    dirtyRegion.clear();
+    outDirtyRegion.clear();
 
     size_t i = currentLayers.size();
     while (i--) {
@@ -884,6 +942,10 @@
         // start with the whole surface at its current location
         const Layer::State& s(layer->drawingState());
 
+        // only consider the layers on the given later stack
+        if (s.layerStack != layerStack)
+            continue;
+
         /*
          * opaqueRegion: area of a surface that is fully opaque.
          */
@@ -977,7 +1039,7 @@
         dirty.subtractSelf(aboveOpaqueLayers);
 
         // accumulate to the screen dirty region
-        dirtyRegion.orSelf(dirty);
+        outDirtyRegion.orSelf(dirty);
 
         // Update aboveOpaqueLayers for next (lower) layer
         aboveOpaqueLayers.orSelf(opaqueRegion);
@@ -987,10 +1049,20 @@
         layer->setCoveredRegion(coveredRegion);
     }
 
-    opaqueRegion = aboveOpaqueLayers;
+    outOpaqueRegion = aboveOpaqueLayers;
 }
 
-Region SurfaceFlinger::handlePageFlip()
+void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
+        const Region& dirty) {
+    for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
+        const sp<DisplayDevice>& hw(mDisplays[dpy]);
+        if (hw->getLayerStack() == layerStack) {
+            hw->dirtyRegion.orSelf(dirty);
+        }
+    }
+}
+
+void SurfaceFlinger::handlePageFlip()
 {
     ATRACE_CALL();
     Region dirtyRegion;
@@ -1002,12 +1074,12 @@
     sp<LayerBase> const* layers = currentLayers.array();
     for (size_t i=0 ; i<count ; i++) {
         const sp<LayerBase>& layer(layers[i]);
-        dirtyRegion.orSelf( layer->latchBuffer(visibleRegions) );
+        const Region dirty(layer->latchBuffer(visibleRegions));
+        Layer::State s(layer->drawingState());
+        invalidateLayerStack(s.layerStack, dirty);
     }
 
     mVisibleRegionsDirty |= visibleRegions;
-
-    return dirtyRegion;
 }
 
 void SurfaceFlinger::invalidateHwcGeometry()
@@ -1031,114 +1103,69 @@
     }
 }
 
-
-void SurfaceFlinger::handleWorkList(const DisplayHardware& hw)
-{
-    mHwWorkListDirty = false;
-    HWComposer& hwc(getHwComposer());
-    if (hwc.initCheck() == NO_ERROR) {
-        const Vector< sp<LayerBase> >& currentLayers(hw.getVisibleLayersSortedByZ());
-        const size_t count = currentLayers.size();
-        hwc.createWorkList(count);
-
-        HWComposer::LayerListIterator cur = hwc.begin();
-        const HWComposer::LayerListIterator end = hwc.end();
-        for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
-            currentLayers[i]->setGeometry(hw, *cur);
-            if (mDebugDisableHWC || mDebugRegion) {
-                cur->setSkip(true);
-            }
-        }
-    }
-}
-
-void SurfaceFlinger::handleRepaint(const DisplayHardware& hw)
+void SurfaceFlinger::handleRepaint(const sp<const DisplayDevice>& hw,
+        const Region& inDirtyRegion)
 {
     ATRACE_CALL();
 
+    Region dirtyRegion(inDirtyRegion);
+
     // compute the invalid region
-    mSwapRegion.orSelf(mDirtyRegion);
+    hw->swapRegion.orSelf(dirtyRegion);
 
     if (CC_UNLIKELY(mDebugRegion)) {
-        debugFlashRegions(hw);
+        debugFlashRegions(hw, dirtyRegion);
     }
 
-    // set the frame buffer
-    glMatrixMode(GL_MODELVIEW);
-    glLoadIdentity();
-
-    uint32_t flags = hw.getFlags();
-    if (flags & DisplayHardware::SWAP_RECTANGLE) {
+    uint32_t flags = hw->getFlags();
+    if (flags & DisplayDevice::SWAP_RECTANGLE) {
         // we can redraw only what's dirty, but since SWAP_RECTANGLE only
         // takes a rectangle, we must make sure to update that whole
         // rectangle in that case
-        mDirtyRegion.set(mSwapRegion.bounds());
+        dirtyRegion.set(hw->swapRegion.bounds());
     } else {
-        if (flags & DisplayHardware::PARTIAL_UPDATES) {
+        if (flags & DisplayDevice::PARTIAL_UPDATES) {
             // We need to redraw the rectangle that will be updated
             // (pushed to the framebuffer).
             // This is needed because PARTIAL_UPDATES only takes one
-            // rectangle instead of a region (see DisplayHardware::flip())
-            mDirtyRegion.set(mSwapRegion.bounds());
+            // rectangle instead of a region (see DisplayDevice::flip())
+            dirtyRegion.set(hw->swapRegion.bounds());
         } else {
             // we need to redraw everything (the whole screen)
-            mDirtyRegion.set(hw.bounds());
-            mSwapRegion = mDirtyRegion;
+            dirtyRegion.set(hw->bounds());
+            hw->swapRegion = dirtyRegion;
         }
     }
 
-    setupHardwareComposer(hw);
-    composeSurfaces(hw, mDirtyRegion);
+    composeSurfaces(hw, dirtyRegion);
+
+    const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
+    const size_t count = currentLayers.size();
+    for (size_t i=0 ; i<count ; i++) {
+        currentLayers[i]->onPostComposition();
+    }
 
     // update the swap region and clear the dirty region
-    mSwapRegion.orSelf(mDirtyRegion);
-    mDirtyRegion.clear();
+    hw->swapRegion.orSelf(dirtyRegion);
 }
 
-void SurfaceFlinger::setupHardwareComposer(const DisplayHardware& hw)
-{
-    HWComposer& hwc(getHwComposer());
-    HWComposer::LayerListIterator cur = hwc.begin();
-    const HWComposer::LayerListIterator end = hwc.end();
-    if (cur == end) {
-        return;
-    }
-
-    const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
-    size_t count = layers.size();
-
-    ALOGE_IF(hwc.getNumLayers() != count,
-            "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
-            hwc.getNumLayers(), count);
-
-    // just to be extra-safe, use the smallest count
-    if (hwc.initCheck() == NO_ERROR) {
-        count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
-    }
-
-    /*
-     *  update the per-frame h/w composer data for each layer
-     *  and build the transparent region of the FB
-     */
-    for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
-        const sp<LayerBase>& layer(layers[i]);
-        layer->setPerFrameData(*cur);
-    }
-    status_t err = hwc.prepare();
-    ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
-}
-
-void SurfaceFlinger::composeSurfaces(const DisplayHardware& hw, const Region& dirty)
+void SurfaceFlinger::composeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
 {
     HWComposer& hwc(getHwComposer());
     HWComposer::LayerListIterator cur = hwc.begin();
     const HWComposer::LayerListIterator end = hwc.end();
 
-    const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
+    const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);  // FIXME: this should be per display
     if (cur==end || fbLayerCount) {
-        // Never touch the framebuffer if we don't have any framebuffer layers
 
-        if (hwc.getLayerCount(HWC_OVERLAY)) {
+        DisplayDevice::makeCurrent(hw, mEGLContext);
+
+        // set the frame buffer
+        glMatrixMode(GL_MODELVIEW);
+        glLoadIdentity();
+
+        // Never touch the framebuffer if we don't have any framebuffer layers
+        if (hwc.getLayerCount(HWC_OVERLAY)) { // FIXME: this should be per display
             // when using overlays, we assume a fully transparent framebuffer
             // NOTE: we could reduce how much we need to clear, for instance
             // remove where there are opaque FB layers. however, on some
@@ -1147,10 +1174,11 @@
             glClearColor(0, 0, 0, 0);
             glClear(GL_COLOR_BUFFER_BIT);
         } else {
+            const Region region(hw->undefinedRegion.intersect(dirty));
             // screen is already cleared here
-            if (!mWormholeRegion.isEmpty()) {
+            if (!region.isEmpty()) {
                 // can happen with SurfaceView
-                drawWormhole();
+                drawWormhole(region);
             }
         }
 
@@ -1158,9 +1186,9 @@
          * and then, render the layers targeted at the framebuffer
          */
 
-        const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
+        const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
         const size_t count = layers.size();
-        const Transform& tr = hw.getTransform();
+        const Transform& tr = hw->getTransform();
         for (size_t i=0 ; i<count ; ++i) {
             const sp<LayerBase>& layer(layers[i]);
             const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
@@ -1185,17 +1213,18 @@
     }
 }
 
-void SurfaceFlinger::debugFlashRegions(const DisplayHardware& hw)
+void SurfaceFlinger::debugFlashRegions(const sp<const DisplayDevice>& hw,
+        const Region& dirtyRegion)
 {
-    const uint32_t flags = hw.getFlags();
-    const int32_t height = hw.getHeight();
-    if (mSwapRegion.isEmpty()) {
+    const uint32_t flags = hw->getFlags();
+    const int32_t height = hw->getHeight();
+    if (hw->swapRegion.isEmpty()) {
         return;
     }
 
-    if (!(flags & DisplayHardware::SWAP_RECTANGLE)) {
-        const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
-                mDirtyRegion.bounds() : hw.bounds());
+    if (!(flags & DisplayDevice::SWAP_RECTANGLE)) {
+        const Region repaint((flags & DisplayDevice::PARTIAL_UPDATES) ?
+                dirtyRegion.bounds() : hw->bounds());
         composeSurfaces(hw, repaint);
     }
 
@@ -1211,8 +1240,8 @@
         glColor4f(1, 1, 0, 1);
     }
 
-    Region::const_iterator it = mDirtyRegion.begin();
-    Region::const_iterator const end = mDirtyRegion.end();
+    Region::const_iterator it = dirtyRegion.begin();
+    Region::const_iterator const end = dirtyRegion.end();
     while (it != end) {
         const Rect& r = *it++;
         GLfloat vertices[][2] = {
@@ -1225,18 +1254,14 @@
         glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
     }
 
-    hw.flip(mSwapRegion);
+    hw->flip(hw->swapRegion);
 
     if (mDebugRegion > 1)
         usleep(mDebugRegion * 1000);
 }
 
-void SurfaceFlinger::drawWormhole() const
+void SurfaceFlinger::drawWormhole(const Region& region) const
 {
-    const Region region(mWormholeRegion.intersect(mDirtyRegion));
-    if (region.isEmpty())
-        return;
-
     glDisable(GL_TEXTURE_EXTERNAL_OES);
     glDisable(GL_TEXTURE_2D);
     glDisable(GL_BLEND);
@@ -1344,9 +1369,10 @@
     }
 
     uint32_t transactionFlags = 0;
-    if (mCurrentState.orientation != orientation) {
+    // FIXME: don't hardcode display id here
+    if (mCurrentState.displays.valueFor(0).orientation != orientation) {
         if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
-            mCurrentState.orientation = orientation;
+            mCurrentState.displays.editValueFor(0).orientation = orientation;
             transactionFlags |= eTransactionNeeded;
         } else if (orientation != eOrientationUnchanged) {
             ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
@@ -1436,7 +1462,7 @@
         PixelFormat& format)
 {
     // initialize the surfaces
-    switch (format) { // TODO: take h/w into account
+    switch (format) {
     case PIXEL_FORMAT_TRANSPARENT:
     case PIXEL_FORMAT_TRANSLUCENT:
         format = PIXEL_FORMAT_RGBA_8888;
@@ -1539,6 +1565,7 @@
                 flags |= eTraversalNeeded;
         }
         if (what & eLayerChanged) {
+            // NOTE: index needs to be calculated before we update the state
             ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
             if (layer->setLayer(s.z)) {
                 mCurrentState.layersSortedByZ.removeAt(idx);
@@ -1574,8 +1601,15 @@
                 flags |= eTraversalNeeded;
         }
         if (what & eLayerStackChanged) {
-            if (layer->setLayerStack(s.layerStack))
-                flags |= eTraversalNeeded;
+            // NOTE: index needs to be calculated before we update the state
+            ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
+            if (layer->setLayerStack(s.layerStack)) {
+                mCurrentState.layersSortedByZ.removeAt(idx);
+                mCurrentState.layersSortedByZ.add(layer);
+                // we need traversal (state changed)
+                // AND transaction (list changed)
+                flags |= eTransactionNeeded|eTraversalNeeded;
+            }
         }
     }
     return flags;
@@ -1585,23 +1619,18 @@
 
 void SurfaceFlinger::onScreenAcquired() {
     ALOGD("Screen about to return, flinger = %p", this);
-    const DisplayHardware& hw(getDefaultDisplayHardware()); // XXX: this should be per DisplayHardware
+    sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
     getHwComposer().acquire();
-    hw.acquireScreen();
+    hw->acquireScreen();
     mEventThread->onScreenAcquired();
-    // this is a temporary work-around, eventually this should be called
-    // by the power-manager
-    SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
-    // from this point on, SF will process updates again
-    repaintEverything();
 }
 
 void SurfaceFlinger::onScreenReleased() {
     ALOGD("About to give-up screen, flinger = %p", this);
-    const DisplayHardware& hw(getDefaultDisplayHardware()); // XXX: this should be per DisplayHardware
-    if (hw.isScreenAcquired()) {
+    sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
+    if (hw->isScreenAcquired()) {
         mEventThread->onScreenReleased();
-        hw.releaseScreen();
+        hw->releaseScreen();
         getHwComposer().release();
         // from this point on, SF will stop drawing
     }
@@ -1798,7 +1827,8 @@
     snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
     result.append(buffer);
 
-    const DisplayHardware& hw(getDefaultDisplayHardware());
+    HWComposer& hwc(getHwComposer());
+    sp<const DisplayDevice> hw(getDefaultDisplayDevice());
     const GLExtensions& extensions(GLExtensions::getInstance());
     snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
             extensions.getVendor(),
@@ -1807,17 +1837,16 @@
     result.append(buffer);
 
     snprintf(buffer, SIZE, "EGL : %s\n",
-            eglQueryString(hw.getEGLDisplay(),
-                    EGL_VERSION_HW_ANDROID));
+            eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
     result.append(buffer);
 
     snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
     result.append(buffer);
 
-    mWormholeRegion.dump(result, "WormholeRegion");
+    hw->undefinedRegion.dump(result, "undefinedRegion");
     snprintf(buffer, SIZE,
             "  orientation=%d, canDraw=%d\n",
-            mCurrentState.orientation, hw.canDraw());
+            hw->getOrientation(), hw->canDraw());
     result.append(buffer);
     snprintf(buffer, SIZE,
             "  last eglSwapBuffers() time: %f us\n"
@@ -1830,10 +1859,10 @@
             mLastSwapBufferTime/1000.0,
             mLastTransactionTime/1000.0,
             mTransactionFlags,
-            hw.getRefreshRate(),
-            hw.getDpiX(),
-            hw.getDpiY(),
-            hw.getDensity());
+            1e9 / hwc.getRefreshPeriod(),
+            hw->getDpiX(),
+            hw->getDpiY(),
+            hw->getDensity());
     result.append(buffer);
 
     snprintf(buffer, SIZE, "  eglSwapBuffers time: %f us\n",
@@ -1852,21 +1881,20 @@
     /*
      * Dump HWComposer state
      */
-    HWComposer& hwc(getHwComposer());
     snprintf(buffer, SIZE, "h/w composer state:\n");
     result.append(buffer);
     snprintf(buffer, SIZE, "  h/w composer %s and %s\n",
             hwc.initCheck()==NO_ERROR ? "present" : "not present",
                     (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
     result.append(buffer);
-    hwc.dump(result, buffer, SIZE, hw.getVisibleLayersSortedByZ());
+    hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
 
     /*
      * Dump gralloc state
      */
     const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
     alloc.dump(result);
-    hw.dump(result);
+    hw->dump(result);
 }
 
 status_t SurfaceFlinger::onTransact(
@@ -1877,8 +1905,6 @@
         case SET_TRANSACTION_STATE:
         case SET_ORIENTATION:
         case BOOT_FINISHED:
-        case TURN_ELECTRON_BEAM_OFF:
-        case TURN_ELECTRON_BEAM_ON:
         case BLANK:
         case UNBLANK:
         {
@@ -1965,8 +1991,8 @@
                 return NO_ERROR;
             case 1013: {
                 Mutex::Autolock _l(mStateLock);
-                const DisplayHardware& hw(getDefaultDisplayHardware());
-                reply->writeInt32(hw.getPageFlipCount());
+                sp<const DisplayDevice> hw(getDefaultDisplayDevice());
+                reply->writeInt32(hw->getPageFlipCount());
             }
             return NO_ERROR;
         }
@@ -1975,24 +2001,10 @@
 }
 
 void SurfaceFlinger::repaintEverything() {
-    const DisplayHardware& hw(getDefaultDisplayHardware()); // FIXME: this cannot be bound the default display
-    const Rect bounds(hw.getBounds());
-    setInvalidateRegion(Region(bounds));
+    android_atomic_or(1, &mRepaintEverything);
     signalTransaction();
 }
 
-void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
-    Mutex::Autolock _l(mInvalidateLock);
-    mInvalidateRegion = reg;
-}
-
-Region SurfaceFlinger::getAndClearInvalidateRegion() {
-    Mutex::Autolock _l(mInvalidateLock);
-    Region reg(mInvalidateRegion);
-    mInvalidateRegion.clear();
-    return reg;
-}
-
 // ---------------------------------------------------------------------------
 
 status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
@@ -2011,9 +2023,9 @@
         return INVALID_OPERATION;
 
     // get screen geometry
-    const DisplayHardware& hw(getDisplayHardware(dpy));
-    const uint32_t hw_w = hw.getWidth();
-    const uint32_t hw_h = hw.getHeight();
+    sp<const DisplayDevice> hw(getDisplayDevice(dpy));
+    const uint32_t hw_w = hw->getWidth();
+    const uint32_t hw_h = hw->getHeight();
     GLfloat u = 1;
     GLfloat v = 1;
 
@@ -2049,14 +2061,14 @@
     glClear(GL_COLOR_BUFFER_BIT);
     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();
-    const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
+    const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
     const size_t count = layers.size();
     for (size_t i=0 ; i<count ; ++i) {
         const sp<LayerBase>& layer(layers[i]);
-        layer->drawForSreenShot(hw);
+        layer->draw(hw);
     }
 
-    hw.compositionComplete();
+    hw->compositionComplete();
 
     // back to main framebuffer
     glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
@@ -2070,471 +2082,6 @@
 
 // ---------------------------------------------------------------------------
 
-class VSyncWaiter {
-    DisplayEventReceiver::Event buffer[4];
-    sp<Looper> looper;
-    sp<IDisplayEventConnection> events;
-    sp<BitTube> eventTube;
-public:
-    VSyncWaiter(const sp<EventThread>& eventThread) {
-        looper = new Looper(true);
-        events = eventThread->createEventConnection();
-        eventTube = events->getDataChannel();
-        looper->addFd(eventTube->getFd(), 0, ALOOPER_EVENT_INPUT, 0, 0);
-        events->requestNextVsync();
-    }
-
-    void wait() {
-        ssize_t n;
-
-        looper->pollOnce(-1);
-        // we don't handle any errors here, it doesn't matter
-        // and we don't want to take the risk to get stuck.
-
-        // drain the events...
-        while ((n = DisplayEventReceiver::getEvents(
-                eventTube, buffer, 4)) > 0) ;
-
-        events->requestNextVsync();
-    }
-};
-
-status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
-{
-    // get screen geometry
-    const DisplayHardware& hw(getDefaultDisplayHardware());
-    const uint32_t hw_w = hw.getWidth();
-    const uint32_t hw_h = hw.getHeight();
-    const Region screenBounds(hw.getBounds());
-
-    GLfloat u, v;
-    GLuint tname;
-    status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
-    if (result != NO_ERROR) {
-        return result;
-    }
-
-    GLfloat vtx[8];
-    const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
-    glBindTexture(GL_TEXTURE_2D, tname);
-    glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
-    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);
-    glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
-    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
-    glVertexPointer(2, GL_FLOAT, 0, vtx);
-
-    /*
-     * Texture coordinate mapping
-     *
-     *                 u
-     *    1 +----------+---+
-     *      |     |    |   |  image is inverted
-     *      |     V    |   |  w.r.t. the texture
-     *  1-v +----------+   |  coordinates
-     *      |              |
-     *      |              |
-     *      |              |
-     *    0 +--------------+
-     *      0              1
-     *
-     */
-
-    class s_curve_interpolator {
-        const float nbFrames, s, v;
-    public:
-        s_curve_interpolator(int nbFrames, float s)
-        : nbFrames(1.0f / (nbFrames-1)), s(s),
-          v(1.0f + expf(-s + 0.5f*s)) {
-        }
-        float operator()(int f) {
-            const float x = f * nbFrames;
-            return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
-        }
-    };
-
-    class v_stretch {
-        const GLfloat hw_w, hw_h;
-    public:
-        v_stretch(uint32_t hw_w, uint32_t hw_h)
-        : hw_w(hw_w), hw_h(hw_h) {
-        }
-        void operator()(GLfloat* vtx, float v) {
-            const GLfloat w = hw_w + (hw_w * v);
-            const GLfloat h = hw_h - (hw_h * v);
-            const GLfloat x = (hw_w - w) * 0.5f;
-            const GLfloat y = (hw_h - h) * 0.5f;
-            vtx[0] = x;         vtx[1] = y;
-            vtx[2] = x;         vtx[3] = y + h;
-            vtx[4] = x + w;     vtx[5] = y + h;
-            vtx[6] = x + w;     vtx[7] = y;
-        }
-    };
-
-    class h_stretch {
-        const GLfloat hw_w, hw_h;
-    public:
-        h_stretch(uint32_t hw_w, uint32_t hw_h)
-        : hw_w(hw_w), hw_h(hw_h) {
-        }
-        void operator()(GLfloat* vtx, float v) {
-            const GLfloat w = hw_w - (hw_w * v);
-            const GLfloat h = 1.0f;
-            const GLfloat x = (hw_w - w) * 0.5f;
-            const GLfloat y = (hw_h - h) * 0.5f;
-            vtx[0] = x;         vtx[1] = y;
-            vtx[2] = x;         vtx[3] = y + h;
-            vtx[4] = x + w;     vtx[5] = y + h;
-            vtx[6] = x + w;     vtx[7] = y;
-        }
-    };
-
-    VSyncWaiter vsync(mEventThread);
-
-    // the full animation is 24 frames
-    char value[PROPERTY_VALUE_MAX];
-    property_get("debug.sf.electron_frames", value, "24");
-    int nbFrames = (atoi(value) + 1) >> 1;
-    if (nbFrames <= 0) // just in case
-        nbFrames = 24;
-
-    s_curve_interpolator itr(nbFrames, 7.5f);
-    s_curve_interpolator itg(nbFrames, 8.0f);
-    s_curve_interpolator itb(nbFrames, 8.5f);
-
-    v_stretch vverts(hw_w, hw_h);
-
-    glMatrixMode(GL_TEXTURE);
-    glLoadIdentity();
-    glMatrixMode(GL_MODELVIEW);
-    glLoadIdentity();
-
-    glEnable(GL_BLEND);
-    glBlendFunc(GL_ONE, GL_ONE);
-    for (int i=0 ; i<nbFrames ; i++) {
-        float x, y, w, h;
-        const float vr = itr(i);
-        const float vg = itg(i);
-        const float vb = itb(i);
-
-        // wait for vsync
-        vsync.wait();
-
-        // clear screen
-        glColorMask(1,1,1,1);
-        glClear(GL_COLOR_BUFFER_BIT);
-        glEnable(GL_TEXTURE_2D);
-
-        // draw the red plane
-        vverts(vtx, vr);
-        glColorMask(1,0,0,1);
-        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
-
-        // draw the green plane
-        vverts(vtx, vg);
-        glColorMask(0,1,0,1);
-        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
-
-        // draw the blue plane
-        vverts(vtx, vb);
-        glColorMask(0,0,1,1);
-        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
-
-        // draw the white highlight (we use the last vertices)
-        glDisable(GL_TEXTURE_2D);
-        glColorMask(1,1,1,1);
-        glColor4f(vg, vg, vg, 1);
-        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
-        hw.flip(screenBounds);
-    }
-
-    h_stretch hverts(hw_w, hw_h);
-    glDisable(GL_BLEND);
-    glDisable(GL_TEXTURE_2D);
-    glColorMask(1,1,1,1);
-    for (int i=0 ; i<nbFrames ; i++) {
-        const float v = itg(i);
-        hverts(vtx, v);
-
-        // wait for vsync
-        vsync.wait();
-
-        glClear(GL_COLOR_BUFFER_BIT);
-        glColor4f(1-v, 1-v, 1-v, 1);
-        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
-        hw.flip(screenBounds);
-    }
-
-    glColorMask(1,1,1,1);
-    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
-    glDeleteTextures(1, &tname);
-    glDisable(GL_TEXTURE_2D);
-    glDisable(GL_BLEND);
-    return NO_ERROR;
-}
-
-status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
-{
-    status_t result = PERMISSION_DENIED;
-
-    if (!GLExtensions::getInstance().haveFramebufferObject())
-        return INVALID_OPERATION;
-
-
-    // get screen geometry
-    const DisplayHardware& hw(getDefaultDisplayHardware());
-    const uint32_t hw_w = hw.getWidth();
-    const uint32_t hw_h = hw.getHeight();
-    const Region screenBounds(hw.bounds());
-
-    GLfloat u, v;
-    GLuint tname;
-    result = renderScreenToTextureLocked(0, &tname, &u, &v);
-    if (result != NO_ERROR) {
-        return result;
-    }
-
-    GLfloat vtx[8];
-    const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
-    glBindTexture(GL_TEXTURE_2D, tname);
-    glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
-    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);
-    glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
-    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
-    glVertexPointer(2, GL_FLOAT, 0, vtx);
-
-    class s_curve_interpolator {
-        const float nbFrames, s, v;
-    public:
-        s_curve_interpolator(int nbFrames, float s)
-        : nbFrames(1.0f / (nbFrames-1)), s(s),
-          v(1.0f + expf(-s + 0.5f*s)) {
-        }
-        float operator()(int f) {
-            const float x = f * nbFrames;
-            return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
-        }
-    };
-
-    class v_stretch {
-        const GLfloat hw_w, hw_h;
-    public:
-        v_stretch(uint32_t hw_w, uint32_t hw_h)
-        : hw_w(hw_w), hw_h(hw_h) {
-        }
-        void operator()(GLfloat* vtx, float v) {
-            const GLfloat w = hw_w + (hw_w * v);
-            const GLfloat h = hw_h - (hw_h * v);
-            const GLfloat x = (hw_w - w) * 0.5f;
-            const GLfloat y = (hw_h - h) * 0.5f;
-            vtx[0] = x;         vtx[1] = y;
-            vtx[2] = x;         vtx[3] = y + h;
-            vtx[4] = x + w;     vtx[5] = y + h;
-            vtx[6] = x + w;     vtx[7] = y;
-        }
-    };
-
-    class h_stretch {
-        const GLfloat hw_w, hw_h;
-    public:
-        h_stretch(uint32_t hw_w, uint32_t hw_h)
-        : hw_w(hw_w), hw_h(hw_h) {
-        }
-        void operator()(GLfloat* vtx, float v) {
-            const GLfloat w = hw_w - (hw_w * v);
-            const GLfloat h = 1.0f;
-            const GLfloat x = (hw_w - w) * 0.5f;
-            const GLfloat y = (hw_h - h) * 0.5f;
-            vtx[0] = x;         vtx[1] = y;
-            vtx[2] = x;         vtx[3] = y + h;
-            vtx[4] = x + w;     vtx[5] = y + h;
-            vtx[6] = x + w;     vtx[7] = y;
-        }
-    };
-
-    VSyncWaiter vsync(mEventThread);
-
-    // the full animation is 12 frames
-    int nbFrames = 8;
-    s_curve_interpolator itr(nbFrames, 7.5f);
-    s_curve_interpolator itg(nbFrames, 8.0f);
-    s_curve_interpolator itb(nbFrames, 8.5f);
-
-    h_stretch hverts(hw_w, hw_h);
-    glDisable(GL_BLEND);
-    glDisable(GL_TEXTURE_2D);
-    glColorMask(1,1,1,1);
-    for (int i=nbFrames-1 ; i>=0 ; i--) {
-        const float v = itg(i);
-        hverts(vtx, v);
-
-        // wait for vsync
-        vsync.wait();
-
-        glClear(GL_COLOR_BUFFER_BIT);
-        glColor4f(1-v, 1-v, 1-v, 1);
-        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
-        hw.flip(screenBounds);
-    }
-
-    nbFrames = 4;
-    v_stretch vverts(hw_w, hw_h);
-    glEnable(GL_BLEND);
-    glBlendFunc(GL_ONE, GL_ONE);
-    for (int i=nbFrames-1 ; i>=0 ; i--) {
-        float x, y, w, h;
-        const float vr = itr(i);
-        const float vg = itg(i);
-        const float vb = itb(i);
-
-        // wait for vsync
-        vsync.wait();
-
-        // clear screen
-        glColorMask(1,1,1,1);
-        glClear(GL_COLOR_BUFFER_BIT);
-        glEnable(GL_TEXTURE_2D);
-
-        // draw the red plane
-        vverts(vtx, vr);
-        glColorMask(1,0,0,1);
-        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
-
-        // draw the green plane
-        vverts(vtx, vg);
-        glColorMask(0,1,0,1);
-        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
-
-        // draw the blue plane
-        vverts(vtx, vb);
-        glColorMask(0,0,1,1);
-        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
-
-        hw.flip(screenBounds);
-    }
-
-    glColorMask(1,1,1,1);
-    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
-    glDeleteTextures(1, &tname);
-    glDisable(GL_TEXTURE_2D);
-    glDisable(GL_BLEND);
-
-    return NO_ERROR;
-}
-
-// ---------------------------------------------------------------------------
-
-status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
-{
-    ATRACE_CALL();
-
-    DisplayHardware& hw(const_cast<DisplayHardware&>(getDefaultDisplayHardware()));
-    if (!hw.canDraw()) {
-        // we're already off
-        return NO_ERROR;
-    }
-
-    // turn off hwc while we're doing the animation
-    getHwComposer().disable();
-    // and make sure to turn it back on (if needed) next time we compose
-    invalidateHwcGeometry();
-
-    if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
-        electronBeamOffAnimationImplLocked();
-    }
-
-    // always clear the whole screen at the end of the animation
-    glClearColor(0,0,0,1);
-    glClear(GL_COLOR_BUFFER_BIT);
-    hw.flip( Region(hw.bounds()) );
-
-    return NO_ERROR;
-}
-
-status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
-{
-    class MessageTurnElectronBeamOff : public MessageBase {
-        SurfaceFlinger* flinger;
-        int32_t mode;
-        status_t result;
-    public:
-        MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
-            : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
-        }
-        status_t getResult() const {
-            return result;
-        }
-        virtual bool handler() {
-            Mutex::Autolock _l(flinger->mStateLock);
-            result = flinger->turnElectronBeamOffImplLocked(mode);
-            return true;
-        }
-    };
-
-    sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
-    status_t res = postMessageSync(msg);
-    if (res == NO_ERROR) {
-        res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
-
-        // work-around: when the power-manager calls us we activate the
-        // animation. eventually, the "on" animation will be called
-        // by the power-manager itself
-        mElectronBeamAnimationMode = mode;
-    }
-    return res;
-}
-
-// ---------------------------------------------------------------------------
-
-status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
-{
-    DisplayHardware& hw(const_cast<DisplayHardware&>(getDefaultDisplayHardware()));
-    if (hw.canDraw()) {
-        // we're already on
-        return NO_ERROR;
-    }
-    if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
-        electronBeamOnAnimationImplLocked();
-    }
-
-    // make sure to redraw the whole screen when the animation is done
-    mDirtyRegion.set(hw.bounds());
-    signalTransaction();
-
-    return NO_ERROR;
-}
-
-status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
-{
-    class MessageTurnElectronBeamOn : public MessageBase {
-        SurfaceFlinger* flinger;
-        int32_t mode;
-        status_t result;
-    public:
-        MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
-            : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
-        }
-        status_t getResult() const {
-            return result;
-        }
-        virtual bool handler() {
-            Mutex::Autolock _l(flinger->mStateLock);
-            result = flinger->turnElectronBeamOnImplLocked(mode);
-            return true;
-        }
-    };
-
-    postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
-    return NO_ERROR;
-}
-
-// ---------------------------------------------------------------------------
-
 status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
         sp<IMemoryHeap>* heap,
         uint32_t* w, uint32_t* h, PixelFormat* f,
@@ -2555,12 +2102,12 @@
     }
 
     // get screen geometry
-    const DisplayHardware& hw(getDisplayHardware(dpy));
-    const uint32_t hw_w = hw.getWidth();
-    const uint32_t hw_h = hw.getHeight();
+    sp<const DisplayDevice> hw(getDisplayDevice(dpy));
+    const uint32_t hw_w = hw->getWidth();
+    const uint32_t hw_h = hw->getHeight();
 
     // if we have secure windows on this display, never allow the screen capture
-    if (hw.getSecureLayerVisible()) {
+    if (hw->getSecureLayerVisible()) {
         return PERMISSION_DENIED;
     }
 
@@ -2571,6 +2118,7 @@
     sw = (!sw) ? hw_w : sw;
     sh = (!sh) ? hw_h : sh;
     const size_t size = sw * sh * 4;
+    const bool filtering = sw != hw_w || sh != hw_h;
 
     //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
     //        sw, sh, minLayerZ, maxLayerZ);
@@ -2613,7 +2161,9 @@
             if (!(flags & ISurfaceComposer::eLayerHidden)) {
                 const uint32_t z = layer->drawingState().z;
                 if (z >= minLayerZ && z <= maxLayerZ) {
-                    layer->drawForSreenShot(hw);
+                    if (filtering) layer->setFiltering(true);
+                    layer->draw(hw);
+                    if (filtering) layer->setFiltering(false);
                 }
             }
         }
@@ -2656,7 +2206,7 @@
     glDeleteRenderbuffersOES(1, &tname);
     glDeleteFramebuffersOES(1, &name);
 
-    hw.compositionComplete();
+    hw->compositionComplete();
 
     // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
 
@@ -2732,20 +2282,27 @@
 int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
     const void* rhs) const
 {
+    // sort layers per layer-stack, then by z-order and finally by sequence
     const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
     const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
-    // sort layers by Z order
+
+    uint32_t ls = l->currentState().layerStack;
+    uint32_t rs = r->currentState().layerStack;
+    if (ls != rs)
+        return ls - rs;
+
     uint32_t lz = l->currentState().z;
     uint32_t rz = r->currentState().z;
-    // then by sequence, so we get a stable ordering
-    return (lz != rz) ? (lz - rz) : (l->sequence - r->sequence);
+    if (lz != rz)
+        return lz - rz;
+
+    return l->sequence - r->sequence;
 }
 
 // ---------------------------------------------------------------------------
 
-SurfaceFlinger::State::State()
-    : orientation(ISurfaceComposer::eOrientationDefault),
-      orientationFlags(0) {
+SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
+    : id(DisplayDevice::DISPLAY_ID_MAIN), layerStack(0), orientation(0) {
 }
 
 // ---------------------------------------------------------------------------
diff --git a/services/surfaceflinger/SurfaceFlinger.h b/services/surfaceflinger/SurfaceFlinger.h
index a3d8538..da417ff 100644
--- a/services/surfaceflinger/SurfaceFlinger.h
+++ b/services/surfaceflinger/SurfaceFlinger.h
@@ -47,6 +47,7 @@
 
 #include "Barrier.h"
 #include "MessageQueue.h"
+#include "DisplayDevice.h"
 
 #include "DisplayHardware/HWComposer.h"
 
@@ -56,7 +57,6 @@
 
 class Client;
 class DisplayEventConnection;
-class DisplayHardware;
 class EventThread;
 class Layer;
 class LayerBase;
@@ -118,8 +118,8 @@
         GLfloat* uOut, GLfloat* vOut);
 
     // returns the default Display
-    const DisplayHardware& getDefaultDisplayHardware() const {
-        return getDisplayHardware(0);
+    sp<const DisplayDevice> getDefaultDisplayDevice() const {
+        return getDisplayDevice(DisplayDevice::DISPLAY_ID_MAIN);
     }
 
     // utility function to delete a texture on the main thread
@@ -156,11 +156,21 @@
         virtual int do_compare(const void* lhs, const void* rhs) const;
     };
 
-    struct State {
-        State();
-        LayerVector layersSortedByZ;
+    struct DisplayDeviceState {
+        DisplayDeviceState();
+        int32_t id;
+        uint32_t layerStack;
+        Rect viewport;
+        Rect frame;
         uint8_t orientation;
-        uint8_t orientationFlags;
+        inline bool operator < (const DisplayDeviceState& rhs) const {
+            return id < rhs.id;
+        }
+    };
+
+    struct State {
+        LayerVector layersSortedByZ;
+        KeyedVector<int32_t, DisplayDeviceState> displays;
     };
 
     /* ------------------------------------------------------------------------
@@ -175,9 +185,9 @@
      */
     virtual sp<ISurfaceComposerClient> createConnection();
     virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc();
-    virtual void bootFinished();
     virtual void setTransactionState(const Vector<ComposerState>& state,
             const Vector<DisplayState>& displays, uint32_t flags);
+    virtual void bootFinished();
     virtual bool authenticateSurfaceTexture(
         const sp<ISurfaceTexture>& surface) const;
     virtual sp<IDisplayEventConnection> createDisplayEventConnection();
@@ -185,8 +195,6 @@
         uint32_t* width, uint32_t* height, PixelFormat* format,
         uint32_t reqWidth, uint32_t reqHeight, uint32_t minLayerZ,
         uint32_t maxLayerZ);
-    virtual status_t turnElectronBeamOff(int32_t mode);
-    virtual status_t turnElectronBeamOn(int32_t mode);
     // called when screen needs to turn off
     virtual void blank();
     // called when screen is turning back on
@@ -228,21 +236,16 @@
     void handleMessageInvalidate();
     void handleMessageRefresh();
 
-    Region handleTransaction(uint32_t transactionFlags);
-    Region handleTransactionLocked(uint32_t transactionFlags);
+    void handleTransaction(uint32_t transactionFlags);
+    void handleTransactionLocked(uint32_t transactionFlags);
 
     /* handlePageFilp: this is were we latch a new buffer
      * if available and compute the dirty region.
-     * The return value is the dirty region expressed in the
-     * window manager's coordinate space (or the layer's state
-     * space, which is the same thing), in particular the dirty
-     * region is independent from a specific display's orientation.
      */
-    Region handlePageFlip();
+    void handlePageFlip();
 
     void handleRefresh();
-    void handleWorkList(const DisplayHardware& hw);
-    void handleRepaint(const DisplayHardware& hw);
+    void handleRepaint(const sp<const DisplayDevice>& hw, const Region& dirtyRegion);
 
     /* ------------------------------------------------------------------------
      * Transactions
@@ -302,11 +305,6 @@
         uint32_t reqWidth, uint32_t reqHeight, uint32_t minLayerZ,
         uint32_t maxLayerZ);
 
-    status_t turnElectronBeamOffImplLocked(int32_t mode);
-    status_t turnElectronBeamOnImplLocked(int32_t mode);
-    status_t electronBeamOffAnimationImplLocked();
-    status_t electronBeamOnAnimationImplLocked();
-
     /* ------------------------------------------------------------------------
      * EGL
      */
@@ -319,11 +317,18 @@
     uint32_t getMaxViewportDims() const;
 
     /* ------------------------------------------------------------------------
-     * Display management
+     * Display and layer stack management
      */
-    const DisplayHardware& getDisplayHardware(DisplayID dpy) const {
-        return *mDisplayHardwares[dpy];
+    sp<const DisplayDevice> getDisplayDevice(DisplayID dpy) const {
+        return mDisplays.valueFor(dpy);
     }
+    const sp<DisplayDevice>& getDisplayDevice(DisplayID dpy) {
+        return mDisplays.valueFor(dpy);
+    }
+
+    // mark a region of a layer stack dirty. this updates the dirty
+    // region of all screens presenting this layer stack.
+    void invalidateLayerStack(uint32_t layerStack, const Region& dirty);
 
     /* ------------------------------------------------------------------------
      * H/W composer
@@ -331,18 +336,16 @@
 
     HWComposer& getHwComposer() const { return *mHwc; }
 
-        /* ------------------------------------------------------------------------
+    /* ------------------------------------------------------------------------
      * Compositing
      */
     void invalidateHwcGeometry();
     void computeVisibleRegions(const LayerVector& currentLayers,
-        Region& dirtyRegion, Region& wormholeRegion);
+            uint32_t layerStack,
+            Region& dirtyRegion, Region& opaqueRegion);
     void postFramebuffer();
-    void setupHardwareComposer(const DisplayHardware& hw);
-    void composeSurfaces(const DisplayHardware& hw, const Region& dirty);
-    void setInvalidateRegion(const Region& reg);
-    Region getAndClearInvalidateRegion();
-    void drawWormhole() const;
+    void composeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty);
+    void drawWormhole(const Region& region) const;
     GLuint getProtectedTexName() const {
         return mProtectedTexName;
     }
@@ -350,7 +353,7 @@
     /* ------------------------------------------------------------------------
      * Debugging & dumpsys
      */
-    void debugFlashRegions(const DisplayHardware& hw);
+    void debugFlashRegions(const sp<const DisplayDevice>& hw, const Region& dirtyReg);
     void listLayersLocked(const Vector<String16>& args, size_t& index,
         String8& result, char* buffer, size_t SIZE) const;
     void dumpStatsLocked(const Vector<String16>& args, size_t& index,
@@ -373,16 +376,13 @@
     Vector<sp<LayerBase> > mLayersPendingRemoval;
 
     // protected by mStateLock (but we could use another lock)
-    DisplayHardware* mDisplayHardwares[1];
     bool mLayersRemoved;
 
     // access must be protected by mInvalidateLock
-    mutable Mutex mInvalidateLock;
-    Region mInvalidateRegion;
+    volatile int32_t mRepaintEverything;
 
     // constant members (no synchronization needed for access)
     HWComposer* mHwc;
-    GLuint mWormholeTexName;
     GLuint mProtectedTexName;
     nsecs_t mBootTime;
     sp<EventThread> mEventThread;
@@ -395,13 +395,9 @@
     // Can only accessed from the main thread, these members
     // don't need synchronization
     State mDrawingState;
-    Region mDirtyRegion;
-    Region mDirtyRegionRemovedLayer;
-    Region mSwapRegion;
-    Region mWormholeRegion;
     bool mVisibleRegionsDirty;
     bool mHwWorkListDirty;
-    int32_t mElectronBeamAnimationMode;
+    DefaultKeyedVector<int32_t, sp<DisplayDevice> > mDisplays;
 
     // don't use a lock for these, we don't care
     int mDebugRegion;