update surfaceflinger, libui and libagl to the new gralloc api

- Currently the lock/unlock path is naive and is done for each drawing operation (glDrawElements and glDrawArrays). this should be improved eventually.
- factor all the lock/unlock code in SurfaceBuffer.
- fixed "showupdate" so it works even when we don't have preserving eglSwapBuffers().
- improved the situation with the dirty-region and fixed a problem that caused GL apps to not update.
- make use of LightRefBase() where needed, instead of duplicating its implementation
- add LightRefBase::getStrongCount()
- renamed EGLNativeWindowSurface.cpp to FramebufferNativeWindow.cpp

- disabled copybits test, since it clashes with the new gralloc api

- Camera/Video will be fixed later when we rework the overlay apis
diff --git a/include/ui/BufferMapper.h b/include/ui/BufferMapper.h
index ff90033..5f084be 100644
--- a/include/ui/BufferMapper.h
+++ b/include/ui/BufferMapper.h
@@ -20,10 +20,7 @@
 #include <stdint.h>
 #include <sys/types.h>
 
-#include <utils/CallStack.h>
-#include <utils/threads.h>
 #include <utils/Singleton.h>
-#include <utils/KeyedVector.h>
 
 #include <hardware/gralloc.h>
 
@@ -40,9 +37,14 @@
 {
 public:
     static inline BufferMapper& get() { return getInstance(); }
-    status_t map(buffer_handle_t handle, void** addr, const void* id);
-    status_t unmap(buffer_handle_t handle, const void* id);
-    status_t lock(buffer_handle_t handle, int usage, const Rect& bounds);
+
+    status_t registerBuffer(buffer_handle_t handle);
+
+    status_t unregisterBuffer(buffer_handle_t handle);
+    
+    status_t lock(buffer_handle_t handle,
+            int usage, const Rect& bounds, void** vaddr);
+
     status_t unlock(buffer_handle_t handle);
     
     // dumps information about the mapping of this handle
@@ -51,16 +53,7 @@
 private:
     friend class Singleton<BufferMapper>;
     BufferMapper();
-    mutable Mutex mLock;
     gralloc_module_t const *mAllocMod;
-    
-    void logMapLocked(buffer_handle_t handle, const void* id);
-    void logUnmapLocked(buffer_handle_t handle, const void* id);
-    struct map_info_t {
-        const void* id;
-        CallStack stack;
-    };
-    KeyedVector<buffer_handle_t, Vector<map_info_t> > mMapInfo;
 };
 
 // ---------------------------------------------------------------------------
diff --git a/include/ui/EGLDisplaySurface.h b/include/ui/EGLDisplaySurface.h
deleted file mode 100644
index a8b5853..0000000
--- a/include/ui/EGLDisplaySurface.h
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- * Copyright (C) 2007 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_EGL_DISPLAY_SURFACE_H
-#define ANDROID_EGL_DISPLAY_SURFACE_H
-
-#include <stdint.h>
-#include <sys/types.h>
-
-#include <utils/Timers.h>
-
-#include <ui/EGLNativeSurface.h>
-
-#include <pixelflinger/pixelflinger.h>
-#include <linux/fb.h>
-
-#include <EGL/egl.h>
-
-struct copybit_device_t;
-struct copybit_image_t;
-
-// ---------------------------------------------------------------------------
-namespace android {
-// ---------------------------------------------------------------------------
-
-class Region;
-class Rect;
-
-class EGLDisplaySurface : public EGLNativeSurface<EGLDisplaySurface>
-{
-public:
-    EGLDisplaySurface();
-    ~EGLDisplaySurface();
-    
-    int32_t getPageFlipCount() const;
-    void    copyFrontToBack(const Region& copyback);
-    void    copyFrontToImage(const copybit_image_t& dst);
-    void    copyBackToImage(const copybit_image_t& dst);
-    
-    void        setSwapRectangle(int l, int t, int w, int h);
-
-private:
-    static void         hook_incRef(NativeWindowType window);
-    static void         hook_decRef(NativeWindowType window);
-    static uint32_t     hook_swapBuffers(NativeWindowType window);
-     
-            uint32_t    swapBuffers();
-
-            status_t    mapFrameBuffer();
-
-            enum {
-                PAGE_FLIP = 0x00000001
-            };
-    GGLSurface          mFb[2];
-    int                 mIndex;
-    uint32_t            mFlags;
-    size_t              mSize;
-    fb_var_screeninfo   mInfo;
-    fb_fix_screeninfo   mFinfo;
-    int32_t             mPageFlipCount;
-    nsecs_t             mTime;
-    int32_t             mSwapCount;
-    nsecs_t             mSleep;
-    uint32_t            mFeatureFlags;
-    copybit_device_t*   mBlitEngine;
-};
-
-// ---------------------------------------------------------------------------
-}; // namespace android
-// ---------------------------------------------------------------------------
-
-#endif // ANDROID_EGL_DISPLAY_SURFACE_H
-
diff --git a/include/ui/EGLNativeWindowSurface.h b/include/ui/FramebufferNativeWindow.h
similarity index 83%
rename from include/ui/EGLNativeWindowSurface.h
rename to include/ui/FramebufferNativeWindow.h
index 4b25655..1d49cca 100644
--- a/include/ui/EGLNativeWindowSurface.h
+++ b/include/ui/FramebufferNativeWindow.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_EGL_NATIVE_WINDOW_SURFACE_H
-#define ANDROID_EGL_NATIVE_WINDOW_SURFACE_H
+#ifndef ANDROID_FRAMEBUFFER_NATIVE_WINDOW_H
+#define ANDROID_FRAMEBUFFER_NATIVE_WINDOW_H
 
 #include <stdint.h>
 #include <sys/types.h>
@@ -76,32 +76,18 @@
 
     framebuffer_device_t const * getDevice() const { return fbDev; } 
 
+    void setSwapRectangle(const Rect& dirty);
+
 private:
     friend class LightRefBase<FramebufferNativeWindow>;    
     ~FramebufferNativeWindow(); // this class cannot be overloaded
     static void connect(android_native_window_t* window);
     static void disconnect(android_native_window_t* window);
     static int setSwapInterval(android_native_window_t* window, int interval);
-    static int setSwapRectangle(android_native_window_t* window,
-            int l, int t, int w, int h);
     static int dequeueBuffer(android_native_window_t* window, android_native_buffer_t** buffer);
     static int lockBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
     static int queueBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
     
-
-    static inline FramebufferNativeWindow* getSelf(
-            android_native_window_t* window) {
-        FramebufferNativeWindow* self = 
-            static_cast<FramebufferNativeWindow*>(window);
-            return self;
-    }
-
-    static inline FramebufferNativeWindow* getSelf(
-            android_native_base_t* window) {
-        return getSelf(reinterpret_cast<android_native_window_t*>(window));
-    }
-
-    
     framebuffer_device_t* fbDev;
     alloc_device_t* grDev;
 
@@ -121,5 +107,5 @@
 }; // namespace android
 // ---------------------------------------------------------------------------
 
-#endif // ANDROID_EGL_NATIVE_WINDOW_SURFACE_H
+#endif // ANDROID_FRAMEBUFFER_NATIVE_WINDOW_H
 
diff --git a/include/ui/Region.h b/include/ui/Region.h
index 7689673..5efeff7 100644
--- a/include/ui/Region.h
+++ b/include/ui/Region.h
@@ -57,6 +57,7 @@
 
             void        clear();
             void        set(const Rect& r);
+            void        set(uint32_t w, uint32_t h);
         
             Region&     orSelf(const Rect& rhs);
             Region&     andSelf(const Rect& rhs);
diff --git a/include/ui/Surface.h b/include/ui/Surface.h
index 16d2edb..6eb06ae 100644
--- a/include/ui/Surface.h
+++ b/include/ui/Surface.h
@@ -34,7 +34,9 @@
 
 // ---------------------------------------------------------------------------
 
+class BufferMapper;
 class Rect;
+class Surface;
 class SurfaceComposerClient;
 struct per_client_cblk_t;
 struct layer_cblk_t;
@@ -52,6 +54,10 @@
         return handle;
     }
     
+    status_t lock(uint32_t usage);
+    status_t lock(uint32_t usage, const Rect& rect);
+    status_t unlock();
+
 protected:
             SurfaceBuffer();
             SurfaceBuffer(const Parcel& reply);
@@ -59,7 +65,11 @@
     buffer_handle_t handle;
     bool mOwner;
 
+    inline const BufferMapper& getBufferMapper() const { return mBufferMapper; }
+    inline BufferMapper& getBufferMapper() { return mBufferMapper; }
+    
 private:
+    friend class Surface;
     friend class BpSurface;
     friend class BnSurface;
     friend class LightRefBase<SurfaceBuffer>;    
@@ -72,6 +82,8 @@
     
     static int getHandle(android_native_buffer_t const * base, 
             buffer_handle_t* handle);
+    
+    BufferMapper& mBufferMapper;
 };
 
 // ---------------------------------------------------------------------------
@@ -191,9 +203,8 @@
     status_t    lock(SurfaceInfo* info, Region* dirty, bool blocking = true);
     status_t    unlockAndPost();
 
-    // setSwapRectangle() is mainly used by EGL
+    // setSwapRectangle() is intended to be used by GL ES clients
     void        setSwapRectangle(const Rect& r);
-    const Rect& swapRectangle() const;
 
 private:
     // can't be copied
@@ -216,22 +227,14 @@
     const sp<ISurface>& getISurface() const { return mSurface; }
 
     status_t getBufferLocked(int index);
-    
-    
-    
-    Region dirtyRegion() const;
-    void setDirtyRegion(const Region& region) const;
-
    
            status_t validate(per_client_cblk_t const* cblk) const;
     static void _send_dirty_region(layer_cblk_t* lcblk, const Region& dirty);
 
+    inline const BufferMapper& getBufferMapper() const { return mBufferMapper; }
+    inline BufferMapper& getBufferMapper() { return mBufferMapper; }
     
-    static void connect(android_native_window_t* window);
-    static void disconnect(android_native_window_t* window);
     static int setSwapInterval(android_native_window_t* window, int interval);
-    static int setSwapRectangle(android_native_window_t* window,
-            int l, int t, int w, int h);
     static int dequeueBuffer(android_native_window_t* window, android_native_buffer_t** buffer);
     static int lockBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
     static int queueBuffer(android_native_window_t* window, android_native_buffer_t* buffer);
@@ -239,21 +242,27 @@
     int dequeueBuffer(android_native_buffer_t** buffer);
     int lockBuffer(android_native_buffer_t* buffer);
     int queueBuffer(android_native_buffer_t* buffer);
-    
+
+    status_t dequeueBuffer(sp<SurfaceBuffer>* buffer);
+    status_t lockBuffer(const sp<SurfaceBuffer>& buffer);
+    status_t queueBuffer(const sp<SurfaceBuffer>& buffer);
+
     
     alloc_device_t*             mAllocDevice;
     sp<SurfaceComposerClient>   mClient;
     sp<ISurface>                mSurface;
     sp<SurfaceBuffer>           mBuffers[2];
-    android_native_buffer_t*    mLockedBuffer;
+    sp<SurfaceBuffer>           mLockedBuffer;
     SurfaceID                   mToken;
     uint32_t                    mIdentity;
     PixelFormat                 mFormat;
     uint32_t                    mFlags;
     mutable Region              mDirtyRegion;
-    mutable Rect                mSwapRectangle;
+    mutable Region              mOldDirtyRegion;
     mutable uint8_t             mBackbufferIndex;
     mutable Mutex               mSurfaceLock;
+    Rect                        mSwapRectangle;
+    BufferMapper&               mBufferMapper;
 };
 
 }; // namespace android