Merge "Log an error message if WebView methods are called on the wrong thread."
diff --git a/GenerationCache.h b/GenerationCache.h
deleted file mode 100644
index 42e6d9b..0000000
--- a/GenerationCache.h
+++ /dev/null
@@ -1,245 +0,0 @@
-/*
- * 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_HWUI_GENERATION_CACHE_H
-#define ANDROID_HWUI_GENERATION_CACHE_H
-
-#include <utils/KeyedVector.h>
-#include <utils/RefBase.h>
-
-namespace android {
-namespace uirenderer {
-
-template<typename EntryKey, typename EntryValue>
-class OnEntryRemoved {
-public:
-    virtual ~OnEntryRemoved() { };
-    virtual void operator()(EntryKey& key, EntryValue& value) = 0;
-}; // class OnEntryRemoved
-
-template<typename EntryKey, typename EntryValue>
-struct Entry: public LightRefBase<Entry<EntryKey, EntryValue> > {
-    Entry() { }
-    Entry(const Entry<EntryKey, EntryValue>& e):
-            key(e.key), value(e.value), parent(e.parent), child(e.child) { }
-    Entry(sp<Entry<EntryKey, EntryValue> > e):
-            key(e->key), value(e->value), parent(e->parent), child(e->child) { }
-
-    EntryKey key;
-    EntryValue value;
-
-    sp<Entry<EntryKey, EntryValue> > parent;
-    sp<Entry<EntryKey, EntryValue> > child;
-}; // struct Entry
-
-template<typename K, typename V>
-class GenerationCache {
-public:
-    GenerationCache(uint32_t maxCapacity);
-    virtual ~GenerationCache();
-
-    enum Capacity {
-        kUnlimitedCapacity,
-    };
-
-    void setOnEntryRemovedListener(OnEntryRemoved<K, V>* listener);
-
-    void clear();
-
-    bool contains(K key) const;
-    V get(K key);
-    K getKeyAt(uint32_t index) const;
-    bool put(K key, V value);
-    V remove(K key);
-    V removeOldest();
-    V getValueAt(uint32_t index) const;
-
-    uint32_t size() const;
-
-    void addToCache(sp<Entry<K, V> > entry, K key, V value);
-    void attachToCache(sp<Entry<K, V> > entry);
-    void detachFromCache(sp<Entry<K, V> > entry);
-
-    V removeAt(ssize_t index);
-
-    KeyedVector<K, sp<Entry<K, V> > > mCache;
-    uint32_t mMaxCapacity;
-
-    OnEntryRemoved<K, V>* mListener;
-
-    sp<Entry<K, V> > mOldest;
-    sp<Entry<K, V> > mYoungest;
-}; // class GenerationCache
-
-template<typename K, typename V>
-GenerationCache<K, V>::GenerationCache(uint32_t maxCapacity): mMaxCapacity(maxCapacity), mListener(NULL) {
-};
-
-template<typename K, typename V>
-GenerationCache<K, V>::~GenerationCache() {
-    clear();
-};
-
-template<typename K, typename V>
-uint32_t GenerationCache<K, V>::size() const {
-    return mCache.size();
-}
-
-template<typename K, typename V>
-void GenerationCache<K, V>::setOnEntryRemovedListener(OnEntryRemoved<K, V>* listener) {
-    mListener = listener;
-}
-
-template<typename K, typename V>
-void GenerationCache<K, V>::clear() {
-    if (mListener) {
-        for (uint32_t i = 0; i < mCache.size(); i++) {
-            sp<Entry<K, V> > entry = mCache.valueAt(i);
-            if (mListener) {
-                (*mListener)(entry->key, entry->value);
-            }
-        }
-    }
-    mCache.clear();
-    mYoungest.clear();
-    mOldest.clear();
-}
-
-template<typename K, typename V>
-bool GenerationCache<K, V>::contains(K key) const {
-    return mCache.indexOfKey(key) >= 0;
-}
-
-template<typename K, typename V>
-K GenerationCache<K, V>::getKeyAt(uint32_t index) const {
-    return mCache.keyAt(index);
-}
-
-template<typename K, typename V>
-V GenerationCache<K, V>::getValueAt(uint32_t index) const {
-    return mCache.valueAt(index)->value;
-}
-
-template<typename K, typename V>
-V GenerationCache<K, V>::get(K key) {
-    ssize_t index = mCache.indexOfKey(key);
-    if (index >= 0) {
-        sp<Entry<K, V> > entry = mCache.valueAt(index);
-        if (entry.get()) {
-            detachFromCache(entry);
-            attachToCache(entry);
-            return entry->value;
-        }
-    }
-
-    return NULL;
-}
-
-template<typename K, typename V>
-bool GenerationCache<K, V>::put(K key, V value) {
-    if (mMaxCapacity != kUnlimitedCapacity && mCache.size() >= mMaxCapacity) {
-        removeOldest();
-    }
-
-    ssize_t index = mCache.indexOfKey(key);
-    if (index < 0) {
-        sp<Entry<K, V> > entry = new Entry<K, V>;
-        addToCache(entry, key, value);
-        return true;
-    }
-
-    return false;
-}
-
-template<typename K, typename V>
-void GenerationCache<K, V>::addToCache(sp<Entry<K, V> > entry, K key, V value) {
-    entry->key = key;
-    entry->value = value;
-    mCache.add(key, entry);
-    attachToCache(entry);
-}
-
-template<typename K, typename V>
-V GenerationCache<K, V>::remove(K key) {
-    ssize_t index = mCache.indexOfKey(key);
-    if (index >= 0) {
-        return removeAt(index);
-    }
-
-    return NULL;
-}
-
-template<typename K, typename V>
-V GenerationCache<K, V>::removeAt(ssize_t index) {
-    sp<Entry<K, V> > entry = mCache.valueAt(index);
-    if (mListener) {
-        (*mListener)(entry->key, entry->value);
-    }
-    mCache.removeItemsAt(index, 1);
-    detachFromCache(entry);
-
-    return entry->value;
-}
-
-template<typename K, typename V>
-V GenerationCache<K, V>::removeOldest() {
-    if (mOldest.get()) {
-        ssize_t index = mCache.indexOfKey(mOldest->key);
-        if (index >= 0) {
-            return removeAt(index);
-        }
-    }
-
-    return NULL;
-}
-
-template<typename K, typename V>
-void GenerationCache<K, V>::attachToCache(sp<Entry<K, V> > entry) {
-    if (!mYoungest.get()) {
-        mYoungest = mOldest = entry;
-    } else {
-        entry->parent = mYoungest;
-        mYoungest->child = entry;
-        mYoungest = entry;
-    }
-}
-
-template<typename K, typename V>
-void GenerationCache<K, V>::detachFromCache(sp<Entry<K, V> > entry) {
-    if (entry->parent.get()) {
-        entry->parent->child = entry->child;
-    }
-
-    if (entry->child.get()) {
-        entry->child->parent = entry->parent;
-    }
-
-    if (mOldest == entry) {
-        mOldest = entry->child;
-    }
-
-    if (mYoungest == entry) {
-        mYoungest = entry->parent;
-    }
-
-    entry->parent.clear();
-    entry->child.clear();
-}
-
-}; // namespace uirenderer
-}; // namespace android
-
-#endif // ANDROID_HWUI_GENERATION_CACHE_H
diff --git a/include/gui/SurfaceTextureClient.h b/include/gui/SurfaceTextureClient.h
index fe9b049..c77bc4c 100644
--- a/include/gui/SurfaceTextureClient.h
+++ b/include/gui/SurfaceTextureClient.h
@@ -45,20 +45,20 @@
     SurfaceTextureClient(const SurfaceTextureClient& rhs);
 
     // ANativeWindow hooks
-    static int cancelBuffer(ANativeWindow* window, android_native_buffer_t* buffer);
-    static int dequeueBuffer(ANativeWindow* window, android_native_buffer_t** buffer);
-    static int lockBuffer(ANativeWindow* window, android_native_buffer_t* buffer);
+    static int cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
+    static int dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer);
+    static int lockBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
     static int perform(ANativeWindow* window, int operation, ...);
-    static int query(ANativeWindow* window, int what, int* value);
-    static int queueBuffer(ANativeWindow* window, android_native_buffer_t* buffer);
+    static int query(const ANativeWindow* window, int what, int* value);
+    static int queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
     static int setSwapInterval(ANativeWindow* window, int interval);
 
-    int cancelBuffer(android_native_buffer_t* buffer);
-    int dequeueBuffer(android_native_buffer_t** buffer);
-    int lockBuffer(android_native_buffer_t* buffer);
+    int cancelBuffer(ANativeWindowBuffer* buffer);
+    int dequeueBuffer(ANativeWindowBuffer** buffer);
+    int lockBuffer(ANativeWindowBuffer* buffer);
     int perform(int operation, va_list args);
-    int query(int what, int* value);
-    int queueBuffer(android_native_buffer_t* buffer);
+    int query(int what, int* value) const;
+    int queueBuffer(ANativeWindowBuffer* buffer);
     int setSwapInterval(int interval);
 
     int dispatchConnect(va_list args);
diff --git a/include/private/opengles/gl_context.h b/include/private/opengles/gl_context.h
index c7db9a6..6b1fa77 100644
--- a/include/private/opengles/gl_context.h
+++ b/include/private/opengles/gl_context.h
@@ -26,14 +26,11 @@
 #endif
 
 #include <private/pixelflinger/ggl_context.h>
-#include <hardware/copybit.h>
 #include <hardware/gralloc.h>
 
 #include <GLES/gl.h>
 #include <GLES/glext.h>
 
-struct android_native_buffer_t;
-
 namespace android {
 
 
@@ -604,14 +601,6 @@
     void (*renderTriangle)(GL, vertex_t*, vertex_t*, vertex_t*);
 };
 
-struct copybits_context_t {
-    // A handle to the blit engine, if it exists, else NULL.
-    copybit_device_t*       blitEngine;
-    int32_t                 minScale;
-    int32_t                 maxScale;
-    android_native_buffer_t* drawSurfaceBuffer;
-};
-
 struct ogles_context_t {
     context_t               rasterizer;
     array_machine_t         arrays         __attribute__((aligned(32)));
@@ -636,13 +625,6 @@
     EGLSurfaceManager*      surfaceManager;
     EGLBufferObjectManager* bufferObjectManager;
 
-    // copybits is only used if LIBAGL_USE_GRALLOC_COPYBITS is
-    // defined, but it is always present because ogles_context_t is a public
-    // struct that is used by clients of libagl. We want the size and offsets
-    // to stay the same, whether or not LIBAGL_USE_GRALLOC_COPYBITS is defined.
-
-    copybits_context_t      copybits;
-
     GLenum                  error;
 
     static inline ogles_context_t* get() {
diff --git a/include/surfaceflinger/Surface.h b/include/surfaceflinger/Surface.h
index 3923e61..ab30f45 100644
--- a/include/surfaceflinger/Surface.h
+++ b/include/surfaceflinger/Surface.h
@@ -202,18 +202,18 @@
      *  ANativeWindow hooks
      */
     static int setSwapInterval(ANativeWindow* window, int interval);
-    static int dequeueBuffer(ANativeWindow* window, android_native_buffer_t** buffer);
-    static int cancelBuffer(ANativeWindow* window, android_native_buffer_t* buffer);
-    static int lockBuffer(ANativeWindow* window, android_native_buffer_t* buffer);
-    static int queueBuffer(ANativeWindow* window, android_native_buffer_t* buffer);
-    static int query(ANativeWindow* window, int what, int* value);
+    static int dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer);
+    static int cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
+    static int lockBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
+    static int queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
+    static int query(const ANativeWindow* window, int what, int* value);
     static int perform(ANativeWindow* window, int operation, ...);
 
-    int dequeueBuffer(android_native_buffer_t** buffer);
-    int lockBuffer(android_native_buffer_t* buffer);
-    int queueBuffer(android_native_buffer_t* buffer);
-    int cancelBuffer(android_native_buffer_t* buffer);
-    int query(int what, int* value);
+    int dequeueBuffer(ANativeWindowBuffer** buffer);
+    int lockBuffer(ANativeWindowBuffer* buffer);
+    int queueBuffer(ANativeWindowBuffer* buffer);
+    int cancelBuffer(ANativeWindowBuffer* buffer);
+    int query(int what, int* value) const;
     int perform(int operation, va_list args);
 
     void dispatch_setUsage(va_list args);
diff --git a/include/ui/FramebufferNativeWindow.h b/include/ui/FramebufferNativeWindow.h
index 16117ad..302d012 100644
--- a/include/ui/FramebufferNativeWindow.h
+++ b/include/ui/FramebufferNativeWindow.h
@@ -67,10 +67,10 @@
     friend class LightRefBase<FramebufferNativeWindow>;    
     ~FramebufferNativeWindow(); // this class cannot be overloaded
     static int setSwapInterval(ANativeWindow* window, int interval);
-    static int dequeueBuffer(ANativeWindow* window, android_native_buffer_t** buffer);
-    static int lockBuffer(ANativeWindow* window, android_native_buffer_t* buffer);
-    static int queueBuffer(ANativeWindow* window, android_native_buffer_t* buffer);
-    static int query(ANativeWindow* window, int what, int* value);
+    static int dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer);
+    static int lockBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
+    static int queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
+    static int query(const ANativeWindow* window, int what, int* value);
     static int perform(ANativeWindow* window, int operation, ...);
     
     framebuffer_device_t* fbDev;
diff --git a/include/ui/GraphicBuffer.h b/include/ui/GraphicBuffer.h
index 02d6f8f..370253a 100644
--- a/include/ui/GraphicBuffer.h
+++ b/include/ui/GraphicBuffer.h
@@ -26,7 +26,7 @@
 #include <utils/Flattenable.h>
 #include <pixelflinger/pixelflinger.h>
 
-struct android_native_buffer_t;
+struct ANativeWindowBuffer;
 
 namespace android {
 
@@ -38,7 +38,7 @@
 
 class GraphicBuffer
     : public EGLNativeBase<
-        android_native_buffer_t, 
+        ANativeWindowBuffer,
         GraphicBuffer, 
         LightRefBase<GraphicBuffer> >, public Flattenable
 {
@@ -74,8 +74,8 @@
     GraphicBuffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage,
             uint32_t stride, native_handle_t* handle, bool keepOwnership);
 
-    // create a buffer from an existing android_native_buffer_t
-    GraphicBuffer(android_native_buffer_t* buffer, bool keepOwnership);
+    // create a buffer from an existing ANativeWindowBuffer
+    GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership);
 
     // return status
     status_t initCheck() const;
@@ -94,7 +94,7 @@
     status_t lock(GGLSurface* surface, uint32_t usage);
     status_t unlock();
 
-    android_native_buffer_t* getNativeBuffer() const;
+    ANativeWindowBuffer* getNativeBuffer() const;
     
     void setIndex(int index);
     int getIndex() const;
@@ -149,7 +149,7 @@
 
     // If we're wrapping another buffer then this reference will make sure it
     // doesn't get freed.
-    sp<android_native_buffer_t> mWrappedBuffer;
+    sp<ANativeWindowBuffer> mWrappedBuffer;
 };
 
 }; // namespace android
diff --git a/include/ui/Region.h b/include/ui/Region.h
index 925fd06..6c9a620 100644
--- a/include/ui/Region.h
+++ b/include/ui/Region.h
@@ -24,8 +24,6 @@
 
 #include <ui/Rect.h>
 
-#include <hardware/copybit.h>
-
 namespace android {
 // ---------------------------------------------------------------------------
 
@@ -183,27 +181,6 @@
 Region& Region::operator += (const Point& pt) {
     return translateSelf(pt.x, pt.y);
 }
-
-// ---------------------------------------------------------------------------
-
-struct region_iterator : public copybit_region_t {
-    region_iterator(const Region& region)
-        : b(region.begin()), e(region.end()) {
-        this->next = iterate;
-    }
-private:
-    static int iterate(copybit_region_t const * self, copybit_rect_t* rect) {
-        region_iterator const* me = static_cast<region_iterator const*>(self);
-        if (me->b != me->e) {
-            *reinterpret_cast<Rect*>(rect) = *me->b++;
-            return 1;
-        }
-        return 0;
-    }
-    mutable Region::const_iterator b;
-    Region::const_iterator const e;
-};
-
 // ---------------------------------------------------------------------------
 }; // namespace android
 
diff --git a/include/ui/android_native_buffer.h b/include/ui/android_native_buffer.h
index 402843e..b6e1db4 100644
--- a/include/ui/android_native_buffer.h
+++ b/include/ui/android_native_buffer.h
@@ -19,53 +19,4 @@
 
 #include <ui/egl/android_natives.h>
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/*****************************************************************************/
-
-typedef struct android_native_buffer_t
-{
-#ifdef __cplusplus
-    android_native_buffer_t() { 
-        common.magic = ANDROID_NATIVE_BUFFER_MAGIC;
-        common.version = sizeof(android_native_buffer_t);
-        memset(common.reserved, 0, sizeof(common.reserved));
-    }
-
-    // Implement the methods that sp<android_native_buffer_t> expects so that it
-    // can be used to automatically refcount android_native_buffer_t's.
-    void incStrong(const void* id) const {
-        common.incRef(const_cast<android_native_base_t*>(&common));
-    }
-    void decStrong(const void* id) const {
-        common.decRef(const_cast<android_native_base_t*>(&common));
-    }
-#endif
-
-    struct android_native_base_t common;
-
-    int width;
-    int height;
-    int stride;
-    int format;
-    int usage;
-    
-    void* reserved[2];
-
-    buffer_handle_t handle;
-
-    void* reserved_proc[8];
-} android_native_buffer_t;
-
-
-/*****************************************************************************/
-
-#ifdef __cplusplus
-}
-#endif
-
-/*****************************************************************************/
-
 #endif /* ANDROID_ANDROID_NATIVES_PRIV_H */
diff --git a/include/ui/egl/android_natives.h b/include/ui/egl/android_natives.h
index 0a6e4fb..9ac50a5 100644
--- a/include/ui/egl/android_natives.h
+++ b/include/ui/egl/android_natives.h
@@ -21,400 +21,9 @@
 #include <string.h>
 
 #include <hardware/gralloc.h>
-
+#include <system/window.h>
+// FIXME: remove this header, it's for legacy use.  native_window is pulled from frameworks/base/native/include/android/
 #include <android/native_window.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/*****************************************************************************/
-
-#define ANDROID_NATIVE_MAKE_CONSTANT(a,b,c,d) \
-    (((unsigned)(a)<<24)|((unsigned)(b)<<16)|((unsigned)(c)<<8)|(unsigned)(d))
-
-#define ANDROID_NATIVE_WINDOW_MAGIC \
-    ANDROID_NATIVE_MAKE_CONSTANT('_','w','n','d')
-
-#define ANDROID_NATIVE_BUFFER_MAGIC \
-    ANDROID_NATIVE_MAKE_CONSTANT('_','b','f','r')
-
-// ---------------------------------------------------------------------------
-
-struct android_native_buffer_t;
-
-typedef struct android_native_rect_t
-{
-    int32_t left;
-    int32_t top;
-    int32_t right;
-    int32_t bottom;
-} android_native_rect_t;
-
-// ---------------------------------------------------------------------------
-
-typedef struct android_native_base_t
-{
-    /* a magic value defined by the actual EGL native type */
-    int magic;
-
-    /* the sizeof() of the actual EGL native type */
-    int version;
-
-    void* reserved[4];
-
-    /* reference-counting interface */
-    void (*incRef)(struct android_native_base_t* base);
-    void (*decRef)(struct android_native_base_t* base);
-} android_native_base_t;
-
-// ---------------------------------------------------------------------------
-
-/* attributes queriable with query() */
-enum {
-    NATIVE_WINDOW_WIDTH     = 0,
-    NATIVE_WINDOW_HEIGHT,
-    NATIVE_WINDOW_FORMAT,
-
-    /* The minimum number of buffers that must remain un-dequeued after a buffer
-     * has been queued.  This value applies only if set_buffer_count was used to
-     * override the number of buffers and if a buffer has since been queued.
-     * Users of the set_buffer_count ANativeWindow method should query this
-     * value before calling set_buffer_count.  If it is necessary to have N
-     * buffers simultaneously dequeued as part of the steady-state operation,
-     * and this query returns M then N+M buffers should be requested via
-     * native_window_set_buffer_count.
-     *
-     * Note that this value does NOT apply until a single buffer has been
-     * queued.  In particular this means that it is possible to:
-     *
-     * 1. Query M = min undequeued buffers
-     * 2. Set the buffer count to N + M
-     * 3. Dequeue all N + M buffers
-     * 4. Cancel M buffers
-     * 5. Queue, dequeue, queue, dequeue, ad infinitum
-     */
-    NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
-
-    /* Check whether queueBuffer operations on the ANativeWindow send the buffer
-     * to the window compositor.  The query sets the returned 'value' argument
-     * to 1 if the ANativeWindow DOES send queued buffers directly to the window
-     * compositor and 0 if the buffers do not go directly to the window
-     * compositor.
-     *
-     * This can be used to determine whether protected buffer content should be
-     * sent to the ANativeWindow.  Note, however, that a result of 1 does NOT
-     * indicate that queued buffers will be protected from applications or users
-     * capturing their contents.  If that behavior is desired then some other
-     * mechanism (e.g. the GRALLOC_USAGE_PROTECTED flag) should be used in
-     * conjunction with this query.
-     */
-    NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
-
-    /* Get the concrete type of a ANativeWindow.  See below for the list of
-     * possible return values.
-     *
-     * This query should not be used outside the Android framework and will
-     * likely be removed in the near future.
-     */
-    NATIVE_WINDOW_CONCRETE_TYPE,
-};
-
-/* valid operations for the (*perform)() hook */
-enum {
-    NATIVE_WINDOW_SET_USAGE  = 0,
-    NATIVE_WINDOW_CONNECT,
-    NATIVE_WINDOW_DISCONNECT,
-    NATIVE_WINDOW_SET_CROP,
-    NATIVE_WINDOW_SET_BUFFER_COUNT,
-    NATIVE_WINDOW_SET_BUFFERS_GEOMETRY,
-    NATIVE_WINDOW_SET_BUFFERS_TRANSFORM,
-    NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP,
-};
-
-/* parameter for NATIVE_WINDOW_[DIS]CONNECT */
-enum {
-    NATIVE_WINDOW_API_EGL = 1
-};
-
-/* parameter for NATIVE_WINDOW_SET_BUFFERS_TRANSFORM */
-enum {
-    /* flip source image horizontally */
-    NATIVE_WINDOW_TRANSFORM_FLIP_H = HAL_TRANSFORM_FLIP_H ,
-    /* flip source image vertically */
-    NATIVE_WINDOW_TRANSFORM_FLIP_V = HAL_TRANSFORM_FLIP_V,
-    /* rotate source image 90 degrees clock-wise */
-    NATIVE_WINDOW_TRANSFORM_ROT_90 = HAL_TRANSFORM_ROT_90,
-    /* rotate source image 180 degrees */
-    NATIVE_WINDOW_TRANSFORM_ROT_180 = HAL_TRANSFORM_ROT_180,
-    /* rotate source image 270 degrees clock-wise */
-    NATIVE_WINDOW_TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_270,
-};
-
-/* values returned by the NATIVE_WINDOW_CONCRETE_TYPE query */
-enum {
-    NATIVE_WINDOW_FRAMEBUFFER,                  // FramebufferNativeWindow
-    NATIVE_WINDOW_SURFACE,                      // Surface
-    NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT,       // SurfaceTextureClient
-};
-
-/* parameter for NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP
- *
- * Special timestamp value to indicate that timestamps should be auto-generated
- * by the native window when queueBuffer is called.  This is equal to INT64_MIN,
- * defined directly to avoid problems with C99/C++ inclusion of stdint.h.
- */
-const int64_t NATIVE_WINDOW_TIMESTAMP_AUTO = (-9223372036854775807LL-1);
-
-struct ANativeWindow
-{
-#ifdef __cplusplus
-    ANativeWindow()
-        : flags(0), minSwapInterval(0), maxSwapInterval(0), xdpi(0), ydpi(0)
-    {
-        common.magic = ANDROID_NATIVE_WINDOW_MAGIC;
-        common.version = sizeof(ANativeWindow);
-        memset(common.reserved, 0, sizeof(common.reserved));
-    }
-
-    // Implement the methods that sp<ANativeWindow> expects so that it
-    // can be used to automatically refcount ANativeWindow's.
-    void incStrong(const void* id) const {
-        common.incRef(const_cast<android_native_base_t*>(&common));
-    }
-    void decStrong(const void* id) const {
-        common.decRef(const_cast<android_native_base_t*>(&common));
-    }
-#endif
-    
-    struct android_native_base_t common;
-
-    /* flags describing some attributes of this surface or its updater */
-    const uint32_t flags;
-    
-    /* min swap interval supported by this updated */
-    const int   minSwapInterval;
-
-    /* max swap interval supported by this updated */
-    const int   maxSwapInterval;
-
-    /* horizontal and vertical resolution in DPI */
-    const float xdpi;
-    const float ydpi;
-
-    /* Some storage reserved for the OEM's driver. */
-    intptr_t    oem[4];
-        
-
-    /*
-     * Set the swap interval for this surface.
-     * 
-     * Returns 0 on success or -errno on error.
-     */
-    int     (*setSwapInterval)(struct ANativeWindow* window,
-                int interval);
-    
-    /*
-     * hook called by EGL to acquire a buffer. After this call, the buffer
-     * is not locked, so its content cannot be modified.
-     * this call may block if no buffers are available.
-     * 
-     * Returns 0 on success or -errno on error.
-     */
-    int     (*dequeueBuffer)(struct ANativeWindow* window,
-                struct android_native_buffer_t** buffer);
-
-    /*
-     * hook called by EGL to lock a buffer. This MUST be called before modifying
-     * the content of a buffer. The buffer must have been acquired with 
-     * dequeueBuffer first.
-     * 
-     * Returns 0 on success or -errno on error.
-     */
-    int     (*lockBuffer)(struct ANativeWindow* window,
-                struct android_native_buffer_t* buffer);
-   /*
-    * hook called by EGL when modifications to the render buffer are done. 
-    * This unlocks and post the buffer.
-    * 
-    * Buffers MUST be queued in the same order than they were dequeued.
-    * 
-    * Returns 0 on success or -errno on error.
-    */
-    int     (*queueBuffer)(struct ANativeWindow* window,
-                struct android_native_buffer_t* buffer);
-
-    /*
-     * hook used to retrieve information about the native window.
-     * 
-     * Returns 0 on success or -errno on error.
-     */
-    int     (*query)(struct ANativeWindow* window,
-                int what, int* value);
-    
-    /*
-     * hook used to perform various operations on the surface.
-     * (*perform)() is a generic mechanism to add functionality to
-     * ANativeWindow while keeping backward binary compatibility.
-     * 
-     * This hook should not be called directly, instead use the helper functions
-     * defined below.
-     * 
-     *  (*perform)() returns -ENOENT if the 'what' parameter is not supported
-     *  by the surface's implementation.
-     *
-     * The valid operations are:
-     *     NATIVE_WINDOW_SET_USAGE
-     *     NATIVE_WINDOW_CONNECT
-     *     NATIVE_WINDOW_DISCONNECT
-     *     NATIVE_WINDOW_SET_CROP
-     *     NATIVE_WINDOW_SET_BUFFER_COUNT
-     *     NATIVE_WINDOW_SET_BUFFERS_GEOMETRY
-     *     NATIVE_WINDOW_SET_BUFFERS_TRANSFORM
-     *     NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP
-     *
-     */
-    
-    int     (*perform)(struct ANativeWindow* window,
-                int operation, ... );
-    
-    /*
-     * hook used to cancel a buffer that has been dequeued.
-     * No synchronization is performed between dequeue() and cancel(), so
-     * either external synchronization is needed, or these functions must be
-     * called from the same thread.
-     */
-    int     (*cancelBuffer)(struct ANativeWindow* window,
-                struct android_native_buffer_t* buffer);
-
-
-    void* reserved_proc[2];
-};
-
-// Backwards compatibility...  please switch to ANativeWindow.
-typedef struct ANativeWindow android_native_window_t;
-
-/*
- *  native_window_set_usage(..., usage)
- *  Sets the intended usage flags for the next buffers
- *  acquired with (*lockBuffer)() and on.
- *  By default (if this function is never called), a usage of
- *      GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE
- *  is assumed.
- *  Calling this function will usually cause following buffers to be
- *  reallocated.
- */
-
-static inline int native_window_set_usage(
-        ANativeWindow* window, int usage)
-{
-    return window->perform(window, NATIVE_WINDOW_SET_USAGE, usage);
-}
-
-/*
- * native_window_connect(..., NATIVE_WINDOW_API_EGL)
- * Must be called by EGL when the window is made current.
- * Returns -EINVAL if for some reason the window cannot be connected, which
- * can happen if it's connected to some other API.
- */
-static inline int native_window_connect(
-        ANativeWindow* window, int api)
-{
-    return window->perform(window, NATIVE_WINDOW_CONNECT, api);
-}
-
-/*
- * native_window_disconnect(..., NATIVE_WINDOW_API_EGL)
- * Must be called by EGL when the window is made not current.
- * An error is returned if for instance the window wasn't connected in the
- * first place.
- */
-static inline int native_window_disconnect(
-        ANativeWindow* window, int api)
-{
-    return window->perform(window, NATIVE_WINDOW_DISCONNECT, api);
-}
-
-/*
- * native_window_set_crop(..., crop)
- * Sets which region of the next queued buffers needs to be considered.
- * A buffer's crop region is scaled to match the surface's size.
- *
- * The specified crop region applies to all buffers queued after it is called.
- *
- * if 'crop' is NULL, subsequently queued buffers won't be cropped.
- *
- * An error is returned if for instance the crop region is invalid,
- * out of the buffer's bound or if the window is invalid.
- */
-static inline int native_window_set_crop(
-        ANativeWindow* window,
-        android_native_rect_t const * crop)
-{
-    return window->perform(window, NATIVE_WINDOW_SET_CROP, crop);
-}
-
-/*
- * native_window_set_buffer_count(..., count)
- * Sets the number of buffers associated with this native window.
- */
-static inline int native_window_set_buffer_count(
-        ANativeWindow* window,
-        size_t bufferCount)
-{
-    return window->perform(window, NATIVE_WINDOW_SET_BUFFER_COUNT, bufferCount);
-}
-
-/*
- * native_window_set_buffers_geometry(..., int w, int h, int format)
- * All buffers dequeued after this call will have the geometry specified.
- * In particular, all buffers will have a fixed-size, independent form the
- * native-window size. They will be appropriately scaled to the window-size
- * upon composition.
- *
- * If all parameters are 0, the normal behavior is restored. That is,
- * dequeued buffers following this call will be sized to the window's size.
- *
- * Calling this function will reset the window crop to a NULL value, which
- * disables cropping of the buffers.
- */
-static inline int native_window_set_buffers_geometry(
-        ANativeWindow* window,
-        int w, int h, int format)
-{
-    return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_GEOMETRY,
-            w, h, format);
-}
-
-/*
- * native_window_set_buffers_transform(..., int transform)
- * All buffers queued after this call will be displayed transformed according
- * to the transform parameter specified.
- */
-static inline int native_window_set_buffers_transform(
-        ANativeWindow* window,
-        int transform)
-{
-    return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_TRANSFORM,
-            transform);
-}
-
-/*
- * native_window_set_buffers_timestamp(..., int64_t timestamp)
- * All buffers queued after this call will be associated with the timestamp
- * parameter specified. If the timestamp is set to NATIVE_WINDOW_TIMESTAMP_AUTO
- * (the default), timestamps will be generated automatically when queueBuffer is
- * called. The timestamp is measured in nanoseconds, and must be monotonically
- * increasing.
- */
-static inline int native_window_set_buffers_timestamp(
-        ANativeWindow* window,
-        int64_t timestamp)
-{
-    return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP,
-            timestamp);
-}
-
 // ---------------------------------------------------------------------------
 
 /* FIXME: this is legacy for pixmaps */
@@ -437,13 +46,6 @@
 /*****************************************************************************/
 
 #ifdef __cplusplus
-}
-#endif
-
-
-/*****************************************************************************/
-
-#ifdef __cplusplus
 
 #include <utils/RefBase.h>
 
diff --git a/libs/gui/Android.mk b/libs/gui/Android.mk
index 58bb0d3..b5737ff 100644
--- a/libs/gui/Android.mk
+++ b/libs/gui/Android.mk
@@ -38,3 +38,7 @@
 endif
 
 include $(BUILD_SHARED_LIBRARY)
+
+ifeq (,$(ONE_SHOT_MAKEFILE))
+include $(call first-makefiles-under,$(LOCAL_PATH))
+endif
diff --git a/libs/gui/Surface.cpp b/libs/gui/Surface.cpp
index 44d9b4b..0c5767b 100644
--- a/libs/gui/Surface.cpp
+++ b/libs/gui/Surface.cpp
@@ -513,32 +513,32 @@
 }
 
 int Surface::dequeueBuffer(ANativeWindow* window, 
-        android_native_buffer_t** buffer) {
+        ANativeWindowBuffer** buffer) {
     Surface* self = getSelf(window);
     return self->dequeueBuffer(buffer);
 }
 
 int Surface::cancelBuffer(ANativeWindow* window,
-        android_native_buffer_t* buffer) {
+        ANativeWindowBuffer* buffer) {
     Surface* self = getSelf(window);
     return self->cancelBuffer(buffer);
 }
 
 int Surface::lockBuffer(ANativeWindow* window, 
-        android_native_buffer_t* buffer) {
+        ANativeWindowBuffer* buffer) {
     Surface* self = getSelf(window);
     return self->lockBuffer(buffer);
 }
 
 int Surface::queueBuffer(ANativeWindow* window, 
-        android_native_buffer_t* buffer) {
+        ANativeWindowBuffer* buffer) {
     Surface* self = getSelf(window);
     return self->queueBuffer(buffer);
 }
 
-int Surface::query(ANativeWindow* window, 
+int Surface::query(const ANativeWindow* window,
         int what, int* value) {
-    Surface* self = getSelf(window);
+    const Surface* self = getSelf(window);
     return self->query(what, value);
 }
 
@@ -570,7 +570,7 @@
     return newNeewBuffer;
 }
 
-int Surface::dequeueBuffer(android_native_buffer_t** buffer)
+int Surface::dequeueBuffer(ANativeWindowBuffer** buffer)
 {
     status_t err = validate();
     if (err != NO_ERROR)
@@ -624,7 +624,7 @@
     return err;
 }
 
-int Surface::cancelBuffer(android_native_buffer_t* buffer)
+int Surface::cancelBuffer(ANativeWindowBuffer* buffer)
 {
     status_t err = validate(true);
     switch (err) {
@@ -651,7 +651,7 @@
 }
 
 
-int Surface::lockBuffer(android_native_buffer_t* buffer)
+int Surface::lockBuffer(ANativeWindowBuffer* buffer)
 {
     status_t err = validate();
     if (err != NO_ERROR)
@@ -670,7 +670,7 @@
     return err;
 }
 
-int Surface::queueBuffer(android_native_buffer_t* buffer)
+int Surface::queueBuffer(ANativeWindowBuffer* buffer)
 {
     status_t err = validate();
     if (err != NO_ERROR)
@@ -697,7 +697,7 @@
     return err;
 }
 
-int Surface::query(int what, int* value)
+int Surface::query(int what, int* value) const
 {
     switch (what) {
     case NATIVE_WINDOW_WIDTH:
@@ -969,7 +969,7 @@
     // we're intending to do software rendering from this point
     setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
 
-    android_native_buffer_t* out;
+    ANativeWindowBuffer* out;
     status_t err = dequeueBuffer(&out);
     LOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
     if (err == NO_ERROR) {
@@ -1063,7 +1063,7 @@
     if (idx < 0) {
         // The buffer doesn't have an index set.  See if the handle the same as
         // one of the buffers for which we do know the index.  This can happen
-        // e.g. if GraphicBuffer is used to wrap an android_native_buffer_t that
+        // e.g. if GraphicBuffer is used to wrap an ANativeWindowBuffer that
         // was dequeued from an ANativeWindow.
         for (size_t i = 0; i < mBuffers.size(); i++) {
             if (mBuffers[i] != 0 && buffer->handle == mBuffers[i]->handle) {
diff --git a/libs/gui/SurfaceTextureClient.cpp b/libs/gui/SurfaceTextureClient.cpp
index f4b2416..ec6da43 100644
--- a/libs/gui/SurfaceTextureClient.cpp
+++ b/libs/gui/SurfaceTextureClient.cpp
@@ -53,31 +53,32 @@
 }
 
 int SurfaceTextureClient::dequeueBuffer(ANativeWindow* window,
-        android_native_buffer_t** buffer) {
+        ANativeWindowBuffer** buffer) {
     SurfaceTextureClient* c = getSelf(window);
     return c->dequeueBuffer(buffer);
 }
 
 int SurfaceTextureClient::cancelBuffer(ANativeWindow* window,
-        android_native_buffer_t* buffer) {
+        ANativeWindowBuffer* buffer) {
     SurfaceTextureClient* c = getSelf(window);
     return c->cancelBuffer(buffer);
 }
 
 int SurfaceTextureClient::lockBuffer(ANativeWindow* window,
-        android_native_buffer_t* buffer) {
+        ANativeWindowBuffer* buffer) {
     SurfaceTextureClient* c = getSelf(window);
     return c->lockBuffer(buffer);
 }
 
 int SurfaceTextureClient::queueBuffer(ANativeWindow* window,
-        android_native_buffer_t* buffer) {
+        ANativeWindowBuffer* buffer) {
     SurfaceTextureClient* c = getSelf(window);
     return c->queueBuffer(buffer);
 }
 
-int SurfaceTextureClient::query(ANativeWindow* window, int what, int* value) {
-    SurfaceTextureClient* c = getSelf(window);
+int SurfaceTextureClient::query(const ANativeWindow* window,
+                                int what, int* value) {
+    const SurfaceTextureClient* c = getSelf(window);
     return c->query(what, value);
 }
 
@@ -160,7 +161,7 @@
     return BAD_VALUE;
 }
 
-int SurfaceTextureClient::query(int what, int* value) {
+int SurfaceTextureClient::query(int what, int* value) const {
     LOGV("SurfaceTextureClient::query");
     Mutex::Autolock lock(mMutex);
     switch (what) {
diff --git a/libs/gui/tests/Android.mk b/libs/gui/tests/Android.mk
index ecd0995..8d3a9b5 100644
--- a/libs/gui/tests/Android.mk
+++ b/libs/gui/tests/Android.mk
@@ -36,9 +36,6 @@
 
 include $(BUILD_EXECUTABLE)
 
-# Build the manual test programs.
-include $(call all-subdir-makefiles)
-
 endif
 
 # Include subdirectory makefiles
diff --git a/libs/gui/tests/SurfaceTextureClient_test.cpp b/libs/gui/tests/SurfaceTextureClient_test.cpp
index db781de..753e933 100644
--- a/libs/gui/tests/SurfaceTextureClient_test.cpp
+++ b/libs/gui/tests/SurfaceTextureClient_test.cpp
@@ -113,7 +113,7 @@
 
 TEST_F(SurfaceTextureClientTest, DefaultGeometryValues) {
     sp<ANativeWindow> anw(mSTC);
-    android_native_buffer_t* buf;
+    ANativeWindowBuffer* buf;
     ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf));
     EXPECT_EQ(1, buf->width);
     EXPECT_EQ(1, buf->height);
@@ -123,7 +123,7 @@
 
 TEST_F(SurfaceTextureClientTest, BufferGeometryCanBeSet) {
     sp<ANativeWindow> anw(mSTC);
-    android_native_buffer_t* buf;
+    ANativeWindowBuffer* buf;
     EXPECT_EQ(OK, native_window_set_buffers_geometry(anw.get(), 16, 8, PIXEL_FORMAT_RGB_565));
     ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf));
     EXPECT_EQ(16, buf->width);
@@ -134,7 +134,7 @@
 
 TEST_F(SurfaceTextureClientTest, BufferGeometryDefaultSizeSetFormat) {
     sp<ANativeWindow> anw(mSTC);
-    android_native_buffer_t* buf;
+    ANativeWindowBuffer* buf;
     EXPECT_EQ(OK, native_window_set_buffers_geometry(anw.get(), 0, 0, PIXEL_FORMAT_RGB_565));
     ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf));
     EXPECT_EQ(1, buf->width);
@@ -145,7 +145,7 @@
 
 TEST_F(SurfaceTextureClientTest, BufferGeometrySetSizeDefaultFormat) {
     sp<ANativeWindow> anw(mSTC);
-    android_native_buffer_t* buf;
+    ANativeWindowBuffer* buf;
     EXPECT_EQ(OK, native_window_set_buffers_geometry(anw.get(), 16, 8, 0));
     ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf));
     EXPECT_EQ(16, buf->width);
@@ -156,7 +156,7 @@
 
 TEST_F(SurfaceTextureClientTest, BufferGeometrySizeCanBeUnset) {
     sp<ANativeWindow> anw(mSTC);
-    android_native_buffer_t* buf;
+    ANativeWindowBuffer* buf;
     EXPECT_EQ(OK, native_window_set_buffers_geometry(anw.get(), 16, 8, 0));
     ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf));
     EXPECT_EQ(16, buf->width);
@@ -173,7 +173,7 @@
 
 TEST_F(SurfaceTextureClientTest, BufferGeometrySizeCanBeChangedWithoutFormat) {
     sp<ANativeWindow> anw(mSTC);
-    android_native_buffer_t* buf;
+    ANativeWindowBuffer* buf;
     EXPECT_EQ(OK, native_window_set_buffers_geometry(anw.get(), 0, 0, PIXEL_FORMAT_RGB_565));
     ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf));
     EXPECT_EQ(1, buf->width);
@@ -191,7 +191,7 @@
 TEST_F(SurfaceTextureClientTest, SurfaceTextureSetDefaultSize) {
     sp<ANativeWindow> anw(mSTC);
     sp<SurfaceTexture> st(mST);
-    android_native_buffer_t* buf;
+    ANativeWindowBuffer* buf;
     EXPECT_EQ(OK, st->setDefaultBufferSize(16, 8));
     ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf));
     EXPECT_EQ(16, buf->width);
@@ -203,7 +203,7 @@
 TEST_F(SurfaceTextureClientTest, SurfaceTextureSetDefaultSizeAfterDequeue) {
     sp<ANativeWindow> anw(mSTC);
     sp<SurfaceTexture> st(mST);
-    android_native_buffer_t* buf[2];
+    ANativeWindowBuffer* buf[2];
     ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[0]));
     ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[1]));
     EXPECT_NE(buf[0], buf[1]);
@@ -224,7 +224,7 @@
 TEST_F(SurfaceTextureClientTest, SurfaceTextureSetDefaultSizeVsGeometry) {
     sp<ANativeWindow> anw(mSTC);
     sp<SurfaceTexture> st(mST);
-    android_native_buffer_t* buf[2];
+    ANativeWindowBuffer* buf[2];
     EXPECT_EQ(OK, st->setDefaultBufferSize(16, 8));
     ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[0]));
     ASSERT_EQ(OK, anw->dequeueBuffer(anw.get(), &buf[1]));
diff --git a/libs/gui/tests/SurfaceTexture_test.cpp b/libs/gui/tests/SurfaceTexture_test.cpp
index 6c71343..8747ba5 100644
--- a/libs/gui/tests/SurfaceTexture_test.cpp
+++ b/libs/gui/tests/SurfaceTexture_test.cpp
@@ -476,7 +476,7 @@
     ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
             GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
 
-    android_native_buffer_t* anb;
+    ANativeWindowBuffer* anb;
     ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
     ASSERT_TRUE(anb != NULL);
 
@@ -524,7 +524,7 @@
     ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
             GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
 
-    android_native_buffer_t* anb;
+    ANativeWindowBuffer* anb;
     ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
     ASSERT_TRUE(anb != NULL);
 
@@ -583,7 +583,7 @@
 
         ASSERT_EQ(NO_ERROR, native_window_set_crop(mANW.get(), &crop));
 
-        android_native_buffer_t* anb;
+        ANativeWindowBuffer* anb;
         ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
         ASSERT_TRUE(anb != NULL);
 
diff --git a/libs/gui/tests/Surface_test.cpp b/libs/gui/tests/Surface_test.cpp
index 440a48b..35c8640 100644
--- a/libs/gui/tests/Surface_test.cpp
+++ b/libs/gui/tests/Surface_test.cpp
@@ -93,7 +93,7 @@
     ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
             GRALLOC_USAGE_PROTECTED));
     ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
-    android_native_buffer_t* buf = 0;
+    ANativeWindowBuffer* buf = 0;
     for (int i = 0; i < 4; i++) {
         // Loop to make sure SurfaceFlinger has retired a protected buffer.
         ASSERT_EQ(NO_ERROR, anw->dequeueBuffer(anw.get(), &buf));
diff --git a/libs/ui/FramebufferNativeWindow.cpp b/libs/ui/FramebufferNativeWindow.cpp
index dc223f9..4393504 100644
--- a/libs/ui/FramebufferNativeWindow.cpp
+++ b/libs/ui/FramebufferNativeWindow.cpp
@@ -47,16 +47,16 @@
 
 class NativeBuffer 
     : public EGLNativeBase<
-        android_native_buffer_t, 
+        ANativeWindowBuffer, 
         NativeBuffer, 
         LightRefBase<NativeBuffer> >
 {
 public:
     NativeBuffer(int w, int h, int f, int u) : BASE() {
-        android_native_buffer_t::width  = w;
-        android_native_buffer_t::height = h;
-        android_native_buffer_t::format = f;
-        android_native_buffer_t::usage  = u;
+        ANativeWindowBuffer::width  = w;
+        ANativeWindowBuffer::height = h;
+        ANativeWindowBuffer::format = f;
+        ANativeWindowBuffer::usage  = u;
     }
 private:
     friend class LightRefBase<NativeBuffer>;    
@@ -201,7 +201,7 @@
 }
 
 int FramebufferNativeWindow::dequeueBuffer(ANativeWindow* window, 
-        android_native_buffer_t** buffer)
+        ANativeWindowBuffer** buffer)
 {
     FramebufferNativeWindow* self = getSelf(window);
     Mutex::Autolock _l(self->mutex);
@@ -229,7 +229,7 @@
 }
 
 int FramebufferNativeWindow::lockBuffer(ANativeWindow* window, 
-        android_native_buffer_t* buffer)
+        ANativeWindowBuffer* buffer)
 {
     FramebufferNativeWindow* self = getSelf(window);
     Mutex::Autolock _l(self->mutex);
@@ -249,7 +249,7 @@
 }
 
 int FramebufferNativeWindow::queueBuffer(ANativeWindow* window, 
-        android_native_buffer_t* buffer)
+        ANativeWindowBuffer* buffer)
 {
     FramebufferNativeWindow* self = getSelf(window);
     Mutex::Autolock _l(self->mutex);
@@ -270,10 +270,10 @@
     return res;
 }
 
-int FramebufferNativeWindow::query(ANativeWindow* window,
+int FramebufferNativeWindow::query(const ANativeWindow* window,
         int what, int* value) 
 {
-    FramebufferNativeWindow* self = getSelf(window);
+    const FramebufferNativeWindow* self = getSelf(window);
     Mutex::Autolock _l(self->mutex);
     framebuffer_device_t* fb = self->fbDev;
     switch (what) {
diff --git a/libs/ui/GraphicBuffer.cpp b/libs/ui/GraphicBuffer.cpp
index 97312a6..54a3ffa 100644
--- a/libs/ui/GraphicBuffer.cpp
+++ b/libs/ui/GraphicBuffer.cpp
@@ -33,7 +33,7 @@
 namespace android {
 
 // ===========================================================================
-// Buffer and implementation of android_native_buffer_t
+// Buffer and implementation of ANativeWindowBuffer
 // ===========================================================================
 
 GraphicBuffer::GraphicBuffer()
@@ -77,7 +77,7 @@
     handle = inHandle;
 }
 
-GraphicBuffer::GraphicBuffer(android_native_buffer_t* buffer, bool keepOwnership)
+GraphicBuffer::GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership)
     : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
       mBufferMapper(GraphicBufferMapper::get()),
       mInitCheck(NO_ERROR), mIndex(-1), mWrappedBuffer(buffer)
@@ -119,9 +119,9 @@
     GraphicBufferAllocator::dumpToSystemLog();
 }
 
-android_native_buffer_t* GraphicBuffer::getNativeBuffer() const
+ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
 {
-    return static_cast<android_native_buffer_t*>(
+    return static_cast<ANativeWindowBuffer*>(
             const_cast<GraphicBuffer*>(this));
 }
 
diff --git a/opengl/include/EGL/eglext.h b/opengl/include/EGL/eglext.h
index 1ffcd56..1123e16 100644
--- a/opengl/include/EGL/eglext.h
+++ b/opengl/include/EGL/eglext.h
@@ -225,7 +225,7 @@
 
 #ifndef EGL_ANDROID_image_native_buffer
 #define EGL_ANDROID_image_native_buffer 1
-struct android_native_buffer_t;
+struct ANativeWindowBuffer;
 #define EGL_NATIVE_BUFFER_ANDROID       0x3140  /* eglCreateImageKHR target */
 #endif
 
diff --git a/opengl/libagl/TextureObjectManager.cpp b/opengl/libagl/TextureObjectManager.cpp
index bbb82fc..022de09 100644
--- a/opengl/libagl/TextureObjectManager.cpp
+++ b/opengl/libagl/TextureObjectManager.cpp
@@ -145,7 +145,7 @@
     return NO_ERROR;
 }
 
-status_t EGLTextureObject::setImage(android_native_buffer_t* native_buffer)
+status_t EGLTextureObject::setImage(ANativeWindowBuffer* native_buffer)
 {
     GGLSurface sur;
     sur.version = sizeof(GGLSurface);
diff --git a/opengl/libagl/TextureObjectManager.h b/opengl/libagl/TextureObjectManager.h
index 70e3bef..de9e03e 100644
--- a/opengl/libagl/TextureObjectManager.h
+++ b/opengl/libagl/TextureObjectManager.h
@@ -48,7 +48,7 @@
                    ~EGLTextureObject();
 
     status_t    setSurface(GGLSurface const* s);
-    status_t    setImage(android_native_buffer_t* buffer);
+    status_t    setImage(ANativeWindowBuffer* buffer);
     void        setImageBits(void* vaddr) { surface.data = (GGLubyte*)vaddr; }
 
     status_t            reallocate(GLint level,
@@ -80,7 +80,7 @@
     GLint               crop_rect[4];
     GLint               generate_mipmap;
     GLint               direct;
-    android_native_buffer_t* buffer;
+    ANativeWindowBuffer* buffer;
 };
 
 // ----------------------------------------------------------------------------
diff --git a/opengl/libagl/egl.cpp b/opengl/libagl/egl.cpp
index a1cb23a..0d03361 100644
--- a/opengl/libagl/egl.cpp
+++ b/opengl/libagl/egl.cpp
@@ -41,8 +41,6 @@
 
 #include <private/ui/android_natives_priv.h>
 
-#include <hardware/copybit.h>
-
 #include "context.h"
 #include "state.h"
 #include "texture.h"
@@ -232,13 +230,12 @@
     virtual     EGLBoolean  setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
     
 private:
-    status_t lock(android_native_buffer_t* buf, int usage, void** vaddr);
-    status_t unlock(android_native_buffer_t* buf);
+    status_t lock(ANativeWindowBuffer* buf, int usage, void** vaddr);
+    status_t unlock(ANativeWindowBuffer* buf);
     ANativeWindow*   nativeWindow;
-    android_native_buffer_t*   buffer;
-    android_native_buffer_t*   previousBuffer;
+    ANativeWindowBuffer*   buffer;
+    ANativeWindowBuffer*   previousBuffer;
     gralloc_module_t const*    module;
-    copybit_device_t*          blitengine;
     int width;
     int height;
     void* bits;
@@ -324,27 +321,9 @@
         ssize_t count;
     };
     
-    struct region_iterator : public copybit_region_t {
-        region_iterator(const Region& region)
-            : b(region.begin()), e(region.end()) {
-            this->next = iterate;
-        }
-    private:
-        static int iterate(copybit_region_t const * self, copybit_rect_t* rect) {
-            region_iterator const* me = static_cast<region_iterator const*>(self);
-            if (me->b != me->e) {
-                *reinterpret_cast<Rect*>(rect) = *me->b++;
-                return 1;
-            }
-            return 0;
-        }
-        mutable Region::const_iterator b;
-        Region::const_iterator const e;
-    };
-
     void copyBlt(
-            android_native_buffer_t* dst, void* dst_vaddr,
-            android_native_buffer_t* src, void const* src_vaddr,
+            ANativeWindowBuffer* dst, void* dst_vaddr,
+            ANativeWindowBuffer* src, void const* src_vaddr,
             const Region& clip);
 
     Rect dirtyRegion;
@@ -357,16 +336,8 @@
         ANativeWindow* window)
     : egl_surface_t(dpy, config, depthFormat), 
     nativeWindow(window), buffer(0), previousBuffer(0), module(0),
-    blitengine(0), bits(NULL)
+    bits(NULL)
 {
-    hw_module_t const* pModule;
-    hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &pModule);
-    module = reinterpret_cast<gralloc_module_t const*>(pModule);
-
-    if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &pModule) == 0) {
-        copybit_open(pModule, &blitengine);
-    }
-
     pixelFormatTable = gglGetPixelFormatTable();
     
     // keep a reference on the window
@@ -383,9 +354,6 @@
         previousBuffer->common.decRef(&previousBuffer->common); 
     }
     nativeWindow->common.decRef(&nativeWindow->common);
-    if (blitengine) {
-        copybit_close(blitengine);
-    }
 }
 
 EGLBoolean egl_window_surface_v2_t::connect() 
@@ -447,7 +415,7 @@
 }
 
 status_t egl_window_surface_v2_t::lock(
-        android_native_buffer_t* buf, int usage, void** vaddr)
+        ANativeWindowBuffer* buf, int usage, void** vaddr)
 {
     int err;
 
@@ -457,7 +425,7 @@
     return err;
 }
 
-status_t egl_window_surface_v2_t::unlock(android_native_buffer_t* buf)
+status_t egl_window_surface_v2_t::unlock(ANativeWindowBuffer* buf)
 {
     if (!buf) return BAD_VALUE;
     int err = NO_ERROR;
@@ -468,67 +436,39 @@
 }
 
 void egl_window_surface_v2_t::copyBlt(
-        android_native_buffer_t* dst, void* dst_vaddr,
-        android_native_buffer_t* src, void const* src_vaddr,
+        ANativeWindowBuffer* dst, void* dst_vaddr,
+        ANativeWindowBuffer* src, void const* src_vaddr,
         const Region& clip)
 {
-    // FIXME: use copybit if possible
     // NOTE: dst and src must be the same format
     
-    status_t err = NO_ERROR;
-    copybit_device_t* const copybit = blitengine;
-    if (copybit)  {
-        copybit_image_t simg;
-        simg.w = src->stride;
-        simg.h = src->height;
-        simg.format = src->format;
-        simg.handle = const_cast<native_handle_t*>(src->handle);
+    Region::const_iterator cur = clip.begin();
+    Region::const_iterator end = clip.end();
 
-        copybit_image_t dimg;
-        dimg.w = dst->stride;
-        dimg.h = dst->height;
-        dimg.format = dst->format;
-        dimg.handle = const_cast<native_handle_t*>(dst->handle);
-        
-        copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
-        copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
-        copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_DISABLE);
-        region_iterator it(clip);
-        err = copybit->blit(copybit, &dimg, &simg, &it);
-        if (err != NO_ERROR) {
-            LOGE("copybit failed (%s)", strerror(err));
+    const size_t bpp = pixelFormatTable[src->format].size;
+    const size_t dbpr = dst->stride * bpp;
+    const size_t sbpr = src->stride * bpp;
+
+    uint8_t const * const src_bits = (uint8_t const *)src_vaddr;
+    uint8_t       * const dst_bits = (uint8_t       *)dst_vaddr;
+
+    while (cur != end) {
+        const Rect& r(*cur++);
+        ssize_t w = r.right - r.left;
+        ssize_t h = r.bottom - r.top;
+        if (w <= 0 || h<=0) continue;
+        size_t size = w * bpp;
+        uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
+        uint8_t       * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
+        if (dbpr==sbpr && size==sbpr) {
+            size *= h;
+            h = 1;
         }
-    }
-
-    if (!copybit || err) {
-        Region::const_iterator cur = clip.begin();
-        Region::const_iterator end = clip.end();
-        
-        const size_t bpp = pixelFormatTable[src->format].size;
-        const size_t dbpr = dst->stride * bpp;
-        const size_t sbpr = src->stride * bpp;
-
-        uint8_t const * const src_bits = (uint8_t const *)src_vaddr;
-        uint8_t       * const dst_bits = (uint8_t       *)dst_vaddr;
-
-        while (cur != end) {
-            const Rect& r(*cur++);
-            ssize_t w = r.right - r.left;
-            ssize_t h = r.bottom - r.top;
-            if (w <= 0 || h<=0) continue;
-            size_t size = w * bpp;
-            uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
-            uint8_t       * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
-            if (dbpr==sbpr && size==sbpr) {
-                size *= h;
-                h = 1;
-            }
-            do {
-                memcpy(d, s, size);
-                d += dbpr;
-                s += sbpr;
-            } while (--h > 0);
-        }
+        do {
+            memcpy(d, s, size);
+            d += dbpr;
+            s += sbpr;
+        } while (--h > 0);
     }
 }
 
@@ -2063,12 +2003,12 @@
         return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
     }
 
-    android_native_buffer_t* native_buffer = (android_native_buffer_t*)buffer;
+    ANativeWindowBuffer* native_buffer = (ANativeWindowBuffer*)buffer;
 
     if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
         return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
 
-    if (native_buffer->common.version != sizeof(android_native_buffer_t))
+    if (native_buffer->common.version != sizeof(ANativeWindowBuffer))
         return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
 
     switch (native_buffer->format) {
@@ -2094,12 +2034,12 @@
         return setError(EGL_BAD_DISPLAY, EGL_FALSE);
     }
 
-    android_native_buffer_t* native_buffer = (android_native_buffer_t*)img;
+    ANativeWindowBuffer* native_buffer = (ANativeWindowBuffer*)img;
 
     if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
         return setError(EGL_BAD_PARAMETER, EGL_FALSE);
 
-    if (native_buffer->common.version != sizeof(android_native_buffer_t))
+    if (native_buffer->common.version != sizeof(ANativeWindowBuffer))
         return setError(EGL_BAD_PARAMETER, EGL_FALSE);
 
     native_buffer->common.decRef(&native_buffer->common);
diff --git a/opengl/libagl/texture.cpp b/opengl/libagl/texture.cpp
index eb96895..8eb17c4 100644
--- a/opengl/libagl/texture.cpp
+++ b/opengl/libagl/texture.cpp
@@ -126,7 +126,7 @@
     for (int i=0 ; i<GGL_TEXTURE_UNIT_COUNT ; i++) {
         if (c->rasterizer.state.texture[i].enable) {
             texture_unit_t& u(c->textures.tmu[i]);
-            android_native_buffer_t* native_buffer = u.texture->buffer;
+            ANativeWindowBuffer* native_buffer = u.texture->buffer;
             if (native_buffer) {
                 c->rasterizer.procs.activeTexture(c, i);
                 hw_module_t const* pModule;
@@ -154,7 +154,7 @@
     for (int i=0 ; i<GGL_TEXTURE_UNIT_COUNT ; i++) {
         if (c->rasterizer.state.texture[i].enable) {
             texture_unit_t& u(c->textures.tmu[i]);
-            android_native_buffer_t* native_buffer = u.texture->buffer;
+            ANativeWindowBuffer* native_buffer = u.texture->buffer;
             if (native_buffer) {
                 c->rasterizer.procs.activeTexture(c, i);
                 hw_module_t const* pModule;
@@ -1615,12 +1615,12 @@
         return;
     }
 
-    android_native_buffer_t* native_buffer = (android_native_buffer_t*)image;
+    ANativeWindowBuffer* native_buffer = (ANativeWindowBuffer*)image;
     if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC) {
         ogles_error(c, GL_INVALID_VALUE);
         return;
     }
-    if (native_buffer->common.version != sizeof(android_native_buffer_t)) {
+    if (native_buffer->common.version != sizeof(ANativeWindowBuffer)) {
         ogles_error(c, GL_INVALID_VALUE);
         return;
     }
@@ -1643,12 +1643,12 @@
         return;
     }
 
-    android_native_buffer_t* native_buffer = (android_native_buffer_t*)image;
+    ANativeWindowBuffer* native_buffer = (ANativeWindowBuffer*)image;
     if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC) {
         ogles_error(c, GL_INVALID_VALUE);
         return;
     }
-    if (native_buffer->common.version != sizeof(android_native_buffer_t)) {
+    if (native_buffer->common.version != sizeof(ANativeWindowBuffer)) {
         ogles_error(c, GL_INVALID_VALUE);
         return;
     }
diff --git a/opengl/libagl2/src/egl.cpp b/opengl/libagl2/src/egl.cpp
index 6184644..0d02ce6 100644
--- a/opengl/libagl2/src/egl.cpp
+++ b/opengl/libagl2/src/egl.cpp
@@ -1,19 +1,19 @@
 /*
-**
-** Copyright 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.
-*/
+ **
+ ** Copyright 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.
+ */
 
 #include <errno.h>
 #include <stdlib.h>
@@ -30,8 +30,6 @@
 
 #include <private/ui/android_natives_priv.h>
 
-#include <hardware/copybit.h>
-
 #include "gles2context.h"
 
 // ----------------------------------------------------------------------------
@@ -53,808 +51,750 @@
 template<typename T>
 static T setError(GLint error, T returnValue)
 {
-   if (ggl_unlikely(gEGLErrorKey == -1)) {
-      pthread_mutex_lock(&gErrorKeyMutex);
-      if (gEGLErrorKey == -1)
-         pthread_key_create(&gEGLErrorKey, NULL);
-      pthread_mutex_unlock(&gErrorKeyMutex);
-   }
-   pthread_setspecific(gEGLErrorKey, (void*)error);
-   return returnValue;
+    if (ggl_unlikely(gEGLErrorKey == -1)) {
+        pthread_mutex_lock(&gErrorKeyMutex);
+        if (gEGLErrorKey == -1)
+            pthread_key_create(&gEGLErrorKey, NULL);
+        pthread_mutex_unlock(&gErrorKeyMutex);
+    }
+    pthread_setspecific(gEGLErrorKey, (void*)error);
+    return returnValue;
 }
 
 static GLint getError()
 {
-   if (ggl_unlikely(gEGLErrorKey == -1))
-      return EGL_SUCCESS;
-   GLint error = (GLint)pthread_getspecific(gEGLErrorKey);
-   if (error == 0) {
-      // The TLS key has been created by another thread, but the value for
-      // this thread has not been initialized.
-      return EGL_SUCCESS;
-   }
-   pthread_setspecific(gEGLErrorKey, (void*)EGL_SUCCESS);
-   return error;
+    if (ggl_unlikely(gEGLErrorKey == -1))
+        return EGL_SUCCESS;
+    GLint error = (GLint)pthread_getspecific(gEGLErrorKey);
+    if (error == 0) {
+        // The TLS key has been created by another thread, but the value for
+        // this thread has not been initialized.
+        return EGL_SUCCESS;
+    }
+    pthread_setspecific(gEGLErrorKey, (void*)EGL_SUCCESS);
+    return error;
 }
 
 // ----------------------------------------------------------------------------
 
 struct egl_display_t {
-   egl_display_t() : type(0), initialized(0) { }
+    egl_display_t() : type(0), initialized(0) { }
 
-   static egl_display_t& get_display(EGLDisplay dpy);
+    static egl_display_t& get_display(EGLDisplay dpy);
 
-   static EGLBoolean is_valid(EGLDisplay dpy) {
-      return ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS) ? EGL_FALSE : EGL_TRUE;
-   }
+    static EGLBoolean is_valid(EGLDisplay dpy) {
+        return ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS) ? EGL_FALSE : EGL_TRUE;
+    }
 
-   NativeDisplayType   type;
-   volatile int32_t    initialized;
+    NativeDisplayType   type;
+    volatile int32_t    initialized;
 };
 
 static egl_display_t gDisplays[NUM_DISPLAYS];
 
 egl_display_t& egl_display_t::get_display(EGLDisplay dpy)
 {
-   return gDisplays[uintptr_t(dpy)-1U];
+    return gDisplays[uintptr_t(dpy)-1U];
 }
 
 // ----------------------------------------------------------------------------
 
 struct egl_surface_t {
-   enum {
-      PAGE_FLIP = 0x00000001,
-      MAGIC     = 0x31415265
-   };
+    enum {
+        PAGE_FLIP = 0x00000001,
+        MAGIC     = 0x31415265
+    };
 
-   uint32_t            magic;
-   EGLDisplay          dpy;
-   EGLConfig           config;
-   EGLContext          ctx;
+    uint32_t            magic;
+    EGLDisplay          dpy;
+    EGLConfig           config;
+    EGLContext          ctx;
 
-   egl_surface_t(EGLDisplay dpy, EGLConfig config, int32_t depthFormat);
-   virtual     ~egl_surface_t();
-   bool    isValid() const;
-   virtual     bool    initCheck() const = 0;
+    egl_surface_t(EGLDisplay dpy, EGLConfig config, int32_t depthFormat);
+    virtual     ~egl_surface_t();
+    bool    isValid() const;
+    virtual     bool    initCheck() const = 0;
 
-   virtual     EGLBoolean  bindDrawSurface(GLES2Context* gl) = 0;
-   virtual     EGLBoolean  bindReadSurface(GLES2Context* gl) = 0;
-   virtual     EGLBoolean  connect() {
-      return EGL_TRUE;
-   }
-   virtual     void        disconnect() {}
-   virtual     EGLint      getWidth() const = 0;
-   virtual     EGLint      getHeight() const = 0;
+    virtual     EGLBoolean  bindDrawSurface(GLES2Context* gl) = 0;
+    virtual     EGLBoolean  bindReadSurface(GLES2Context* gl) = 0;
+    virtual     EGLBoolean  connect() {
+        return EGL_TRUE;
+    }
+    virtual     void        disconnect() {}
+    virtual     EGLint      getWidth() const = 0;
+    virtual     EGLint      getHeight() const = 0;
 
-   virtual     EGLint      getHorizontalResolution() const;
-   virtual     EGLint      getVerticalResolution() const;
-   virtual     EGLint      getRefreshRate() const;
-   virtual     EGLint      getSwapBehavior() const;
-   virtual     EGLBoolean  swapBuffers();
-   virtual     EGLBoolean  setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
+    virtual     EGLint      getHorizontalResolution() const;
+    virtual     EGLint      getVerticalResolution() const;
+    virtual     EGLint      getRefreshRate() const;
+    virtual     EGLint      getSwapBehavior() const;
+    virtual     EGLBoolean  swapBuffers();
+    virtual     EGLBoolean  setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
 protected:
-   GGLSurface              depth;
+    GGLSurface              depth;
 };
 
 egl_surface_t::egl_surface_t(EGLDisplay dpy,
-                             EGLConfig config,
-                             int32_t depthFormat)
-      : magic(MAGIC), dpy(dpy), config(config), ctx(0)
+        EGLConfig config,
+        int32_t depthFormat)
+: magic(MAGIC), dpy(dpy), config(config), ctx(0)
 {
-   depth.version = sizeof(GGLSurface);
-   depth.data = 0;
-   depth.format = (GGLPixelFormat)depthFormat;
+    depth.version = sizeof(GGLSurface);
+    depth.data = 0;
+    depth.format = (GGLPixelFormat)depthFormat;
 }
 egl_surface_t::~egl_surface_t()
 {
-   magic = 0;
-   free(depth.data);
+    magic = 0;
+    free(depth.data);
 }
 bool egl_surface_t::isValid() const
 {
-   LOGE_IF(magic != MAGIC, "invalid EGLSurface (%p)", this);
-   return magic == MAGIC;
+    LOGE_IF(magic != MAGIC, "invalid EGLSurface (%p)", this);
+    return magic == MAGIC;
 }
 
 EGLBoolean egl_surface_t::swapBuffers()
 {
-   return EGL_FALSE;
+    return EGL_FALSE;
 }
 EGLint egl_surface_t::getHorizontalResolution() const
 {
-   return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
+    return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
 }
 EGLint egl_surface_t::getVerticalResolution() const
 {
-   return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
+    return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
 }
 EGLint egl_surface_t::getRefreshRate() const
 {
-   return (60 * EGL_DISPLAY_SCALING);
+    return (60 * EGL_DISPLAY_SCALING);
 }
 EGLint egl_surface_t::getSwapBehavior() const
 {
-   return EGL_BUFFER_PRESERVED;
+    return EGL_BUFFER_PRESERVED;
 }
 EGLBoolean egl_surface_t::setSwapRectangle(
-   EGLint l, EGLint t, EGLint w, EGLint h)
+        EGLint l, EGLint t, EGLint w, EGLint h)
 {
-   return EGL_FALSE;
+    return EGL_FALSE;
 }
 
 // ----------------------------------------------------------------------------
 
 struct egl_window_surface_v2_t : public egl_surface_t {
-   egl_window_surface_v2_t(
-      EGLDisplay dpy, EGLConfig config,
-      int32_t depthFormat,
-      ANativeWindow* window);
+    egl_window_surface_v2_t(
+            EGLDisplay dpy, EGLConfig config,
+            int32_t depthFormat,
+            ANativeWindow* window);
 
-   ~egl_window_surface_v2_t();
+    ~egl_window_surface_v2_t();
 
-   virtual     bool        initCheck() const {
-      return true;   // TODO: report failure if ctor fails
-   }
-   virtual     EGLBoolean  swapBuffers();
-   virtual     EGLBoolean  bindDrawSurface(GLES2Context* gl);
-   virtual     EGLBoolean  bindReadSurface(GLES2Context* gl);
-   virtual     EGLBoolean  connect();
-   virtual     void        disconnect();
-   virtual     EGLint      getWidth() const    {
-      return width;
-   }
-   virtual     EGLint      getHeight() const   {
-      return height;
-   }
-   virtual     EGLint      getHorizontalResolution() const;
-   virtual     EGLint      getVerticalResolution() const;
-   virtual     EGLint      getRefreshRate() const;
-   virtual     EGLint      getSwapBehavior() const;
-   virtual     EGLBoolean  setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
+    virtual     bool        initCheck() const {
+        return true;   // TODO: report failure if ctor fails
+    }
+    virtual     EGLBoolean  swapBuffers();
+    virtual     EGLBoolean  bindDrawSurface(GLES2Context* gl);
+    virtual     EGLBoolean  bindReadSurface(GLES2Context* gl);
+    virtual     EGLBoolean  connect();
+    virtual     void        disconnect();
+    virtual     EGLint      getWidth() const    {
+        return width;
+    }
+    virtual     EGLint      getHeight() const   {
+        return height;
+    }
+    virtual     EGLint      getHorizontalResolution() const;
+    virtual     EGLint      getVerticalResolution() const;
+    virtual     EGLint      getRefreshRate() const;
+    virtual     EGLint      getSwapBehavior() const;
+    virtual     EGLBoolean  setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h);
 
 private:
-   status_t lock(android_native_buffer_t* buf, int usage, void** vaddr);
-   status_t unlock(android_native_buffer_t* buf);
-   ANativeWindow*   nativeWindow;
-   android_native_buffer_t*   buffer;
-   android_native_buffer_t*   previousBuffer;
-   gralloc_module_t const*    module;
-   copybit_device_t*          blitengine;
-   int width;
-   int height;
-   void* bits;
-   GGLFormat const* pixelFormatTable;
+    status_t lock(ANativeWindowBuffer* buf, int usage, void** vaddr);
+    status_t unlock(ANativeWindowBuffer* buf);
+    ANativeWindow*   nativeWindow;
+    ANativeWindowBuffer*   buffer;
+    ANativeWindowBuffer*   previousBuffer;
+    gralloc_module_t const*    module;
+    int width;
+    int height;
+    void* bits;
+    GGLFormat const* pixelFormatTable;
 
-   struct Rect {
-      inline Rect() { };
-      inline Rect(int32_t w, int32_t h)
-            : left(0), top(0), right(w), bottom(h) { }
-      inline Rect(int32_t l, int32_t t, int32_t r, int32_t b)
-            : left(l), top(t), right(r), bottom(b) { }
-      Rect& andSelf(const Rect& r) {
-         left   = max(left, r.left);
-         top    = max(top, r.top);
-         right  = min(right, r.right);
-         bottom = min(bottom, r.bottom);
-         return *this;
-      }
-      bool isEmpty() const {
-         return (left>=right || top>=bottom);
-      }
-      void dump(char const* what) {
-         LOGD("%s { %5d, %5d, w=%5d, h=%5d }",
-              what, left, top, right-left, bottom-top);
-      }
+    struct Rect {
+        inline Rect() { };
+        inline Rect(int32_t w, int32_t h)
+        : left(0), top(0), right(w), bottom(h) { }
+        inline Rect(int32_t l, int32_t t, int32_t r, int32_t b)
+        : left(l), top(t), right(r), bottom(b) { }
+        Rect& andSelf(const Rect& r) {
+            left   = max(left, r.left);
+            top    = max(top, r.top);
+            right  = min(right, r.right);
+            bottom = min(bottom, r.bottom);
+            return *this;
+        }
+        bool isEmpty() const {
+            return (left>=right || top>=bottom);
+        }
+        void dump(char const* what) {
+            LOGD("%s { %5d, %5d, w=%5d, h=%5d }",
+                    what, left, top, right-left, bottom-top);
+        }
 
-      int32_t left;
-      int32_t top;
-      int32_t right;
-      int32_t bottom;
-   };
+        int32_t left;
+        int32_t top;
+        int32_t right;
+        int32_t bottom;
+    };
 
-   struct Region {
-      inline Region() : count(0) { }
-      typedef Rect const* const_iterator;
-      const_iterator begin() const {
-         return storage;
-      }
-      const_iterator end() const {
-         return storage+count;
-      }
-      static Region subtract(const Rect& lhs, const Rect& rhs) {
-         Region reg;
-         Rect* storage = reg.storage;
-         if (!lhs.isEmpty()) {
-            if (lhs.top < rhs.top) { // top rect
-               storage->left   = lhs.left;
-               storage->top    = lhs.top;
-               storage->right  = lhs.right;
-               storage->bottom = rhs.top;
-               storage++;
+    struct Region {
+        inline Region() : count(0) { }
+        typedef Rect const* const_iterator;
+        const_iterator begin() const {
+            return storage;
+        }
+        const_iterator end() const {
+            return storage+count;
+        }
+        static Region subtract(const Rect& lhs, const Rect& rhs) {
+            Region reg;
+            Rect* storage = reg.storage;
+            if (!lhs.isEmpty()) {
+                if (lhs.top < rhs.top) { // top rect
+                    storage->left   = lhs.left;
+                    storage->top    = lhs.top;
+                    storage->right  = lhs.right;
+                    storage->bottom = rhs.top;
+                    storage++;
+                }
+                const int32_t top = max(lhs.top, rhs.top);
+                const int32_t bot = min(lhs.bottom, rhs.bottom);
+                if (top < bot) {
+                    if (lhs.left < rhs.left) { // left-side rect
+                        storage->left   = lhs.left;
+                        storage->top    = top;
+                        storage->right  = rhs.left;
+                        storage->bottom = bot;
+                        storage++;
+                    }
+                    if (lhs.right > rhs.right) { // right-side rect
+                        storage->left   = rhs.right;
+                        storage->top    = top;
+                        storage->right  = lhs.right;
+                        storage->bottom = bot;
+                        storage++;
+                    }
+                }
+                if (lhs.bottom > rhs.bottom) { // bottom rect
+                    storage->left   = lhs.left;
+                    storage->top    = rhs.bottom;
+                    storage->right  = lhs.right;
+                    storage->bottom = lhs.bottom;
+                    storage++;
+                }
+                reg.count = storage - reg.storage;
             }
-            const int32_t top = max(lhs.top, rhs.top);
-            const int32_t bot = min(lhs.bottom, rhs.bottom);
-            if (top < bot) {
-               if (lhs.left < rhs.left) { // left-side rect
-                  storage->left   = lhs.left;
-                  storage->top    = top;
-                  storage->right  = rhs.left;
-                  storage->bottom = bot;
-                  storage++;
-               }
-               if (lhs.right > rhs.right) { // right-side rect
-                  storage->left   = rhs.right;
-                  storage->top    = top;
-                  storage->right  = lhs.right;
-                  storage->bottom = bot;
-                  storage++;
-               }
-            }
-            if (lhs.bottom > rhs.bottom) { // bottom rect
-               storage->left   = lhs.left;
-               storage->top    = rhs.bottom;
-               storage->right  = lhs.right;
-               storage->bottom = lhs.bottom;
-               storage++;
-            }
-            reg.count = storage - reg.storage;
-         }
-         return reg;
-      }
-      bool isEmpty() const {
-         return count<=0;
-      }
-private:
-      Rect storage[4];
-      ssize_t count;
-   };
+            return reg;
+        }
+        bool isEmpty() const {
+            return count<=0;
+        }
+    private:
+        Rect storage[4];
+        ssize_t count;
+    };
 
-   struct region_iterator : public copybit_region_t {
-      region_iterator(const Region& region)
-            : b(region.begin()), e(region.end()) {
-         this->next = iterate;
-      }
-private:
-      static int iterate(copybit_region_t const * self, copybit_rect_t* rect) {
-         region_iterator const* me = static_cast<region_iterator const*>(self);
-         if (me->b != me->e) {
-            *reinterpret_cast<Rect*>(rect) = *me->b++;
-            return 1;
-         }
-         return 0;
-      }
-      mutable Region::const_iterator b;
-      Region::const_iterator const e;
-   };
+    void copyBlt(
+            ANativeWindowBuffer* dst, void* dst_vaddr,
+            ANativeWindowBuffer* src, void const* src_vaddr,
+            const Region& clip);
 
-   void copyBlt(
-      android_native_buffer_t* dst, void* dst_vaddr,
-      android_native_buffer_t* src, void const* src_vaddr,
-      const Region& clip);
-
-   Rect dirtyRegion;
-   Rect oldDirtyRegion;
+    Rect dirtyRegion;
+    Rect oldDirtyRegion;
 };
 
 egl_window_surface_v2_t::egl_window_surface_v2_t(EGLDisplay dpy,
-      EGLConfig config,
-      int32_t depthFormat,
-      ANativeWindow* window)
-      : egl_surface_t(dpy, config, depthFormat),
-      nativeWindow(window), buffer(0), previousBuffer(0), module(0),
-      blitengine(0), bits(NULL)
+        EGLConfig config,
+        int32_t depthFormat,
+        ANativeWindow* window)
+: egl_surface_t(dpy, config, depthFormat),
+  nativeWindow(window), buffer(0), previousBuffer(0), module(0),
+  bits(NULL)
 {
-   hw_module_t const* pModule;
-   hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &pModule);
-   module = reinterpret_cast<gralloc_module_t const*>(pModule);
+    pixelFormatTable = gglGetPixelFormatTable();
 
-   if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &pModule) == 0) {
-      copybit_open(pModule, &blitengine);
-   }
-
-   pixelFormatTable = gglGetPixelFormatTable();
-
-   // keep a reference on the window
-   nativeWindow->common.incRef(&nativeWindow->common);
-   nativeWindow->query(nativeWindow, NATIVE_WINDOW_WIDTH, &width);
-   nativeWindow->query(nativeWindow, NATIVE_WINDOW_HEIGHT, &height);
-   int format = 0;
-   nativeWindow->query(nativeWindow, NATIVE_WINDOW_FORMAT, &format);
-   LOGD("agl2: egl_window_surface_v2_t format=0x%.4X", format);
-//   assert(0);
+    // keep a reference on the window
+    nativeWindow->common.incRef(&nativeWindow->common);
+    nativeWindow->query(nativeWindow, NATIVE_WINDOW_WIDTH, &width);
+    nativeWindow->query(nativeWindow, NATIVE_WINDOW_HEIGHT, &height);
+    int format = 0;
+    nativeWindow->query(nativeWindow, NATIVE_WINDOW_FORMAT, &format);
+    LOGD("agl2: egl_window_surface_v2_t format=0x%.4X", format);
+    //   assert(0);
 }
 
 egl_window_surface_v2_t::~egl_window_surface_v2_t()
 {
-   if (buffer) {
-      buffer->common.decRef(&buffer->common);
-   }
-   if (previousBuffer) {
-      previousBuffer->common.decRef(&previousBuffer->common);
-   }
-   nativeWindow->common.decRef(&nativeWindow->common);
-   if (blitengine) {
-      copybit_close(blitengine);
-   }
+    if (buffer) {
+        buffer->common.decRef(&buffer->common);
+    }
+    if (previousBuffer) {
+        previousBuffer->common.decRef(&previousBuffer->common);
+    }
+    nativeWindow->common.decRef(&nativeWindow->common);
 }
 
 EGLBoolean egl_window_surface_v2_t::connect()
 {
-   // we're intending to do software rendering
-   native_window_set_usage(nativeWindow,
-                           GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
+    // we're intending to do software rendering
+    native_window_set_usage(nativeWindow,
+            GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
 
-   // dequeue a buffer
-   if (nativeWindow->dequeueBuffer(nativeWindow, &buffer) != NO_ERROR) {
-      return setError(EGL_BAD_ALLOC, EGL_FALSE);
-   }
+    // dequeue a buffer
+    if (nativeWindow->dequeueBuffer(nativeWindow, &buffer) != NO_ERROR) {
+        return setError(EGL_BAD_ALLOC, EGL_FALSE);
+    }
 
-   // allocate a corresponding depth-buffer
-   width = buffer->width;
-   height = buffer->height;
-   if (depth.format) {
-      depth.width   = width;
-      depth.height  = height;
-      depth.stride  = depth.width; // use the width here
-      assert(GGL_PIXEL_FORMAT_Z_32 == depth.format);
-      depth.data    = (GGLubyte*)malloc(depth.stride*depth.height*4);
-      if (depth.data == 0) {
-         return setError(EGL_BAD_ALLOC, EGL_FALSE);
-      }
-   }
+    // allocate a corresponding depth-buffer
+    width = buffer->width;
+    height = buffer->height;
+    if (depth.format) {
+        depth.width   = width;
+        depth.height  = height;
+        depth.stride  = depth.width; // use the width here
+        assert(GGL_PIXEL_FORMAT_Z_32 == depth.format);
+        depth.data    = (GGLubyte*)malloc(depth.stride*depth.height*4);
+        if (depth.data == 0) {
+            return setError(EGL_BAD_ALLOC, EGL_FALSE);
+        }
+    }
 
-   // keep a reference on the buffer
-   buffer->common.incRef(&buffer->common);
+    // keep a reference on the buffer
+    buffer->common.incRef(&buffer->common);
 
-   // Lock the buffer
-   nativeWindow->lockBuffer(nativeWindow, buffer);
-   // pin the buffer down
-   if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
+    // Lock the buffer
+    nativeWindow->lockBuffer(nativeWindow, buffer);
+    // pin the buffer down
+    if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
             GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
-      LOGE("connect() failed to lock buffer %p (%ux%u)",
-           buffer, buffer->width, buffer->height);
-      return setError(EGL_BAD_ACCESS, EGL_FALSE);
-      // FIXME: we should make sure we're not accessing the buffer anymore
-   }
-   return EGL_TRUE;
+        LOGE("connect() failed to lock buffer %p (%ux%u)",
+                buffer, buffer->width, buffer->height);
+        return setError(EGL_BAD_ACCESS, EGL_FALSE);
+        // FIXME: we should make sure we're not accessing the buffer anymore
+    }
+    return EGL_TRUE;
 }
 
 void egl_window_surface_v2_t::disconnect()
 {
-   if (buffer && bits) {
-      bits = NULL;
-      unlock(buffer);
-   }
-   // enqueue the last frame
-   if (buffer)
-      nativeWindow->queueBuffer(nativeWindow, buffer);
-   if (buffer) {
-      buffer->common.decRef(&buffer->common);
-      buffer = 0;
-   }
-   if (previousBuffer) {
-      previousBuffer->common.decRef(&previousBuffer->common);
-      previousBuffer = 0;
-   }
+    if (buffer && bits) {
+        bits = NULL;
+        unlock(buffer);
+    }
+    // enqueue the last frame
+    if (buffer)
+        nativeWindow->queueBuffer(nativeWindow, buffer);
+    if (buffer) {
+        buffer->common.decRef(&buffer->common);
+        buffer = 0;
+    }
+    if (previousBuffer) {
+        previousBuffer->common.decRef(&previousBuffer->common);
+        previousBuffer = 0;
+    }
 }
 
 status_t egl_window_surface_v2_t::lock(
-   android_native_buffer_t* buf, int usage, void** vaddr)
+        ANativeWindowBuffer* buf, int usage, void** vaddr)
 {
-   int err;
+    int err;
 
-   err = module->lock(module, buf->handle,
-                      usage, 0, 0, buf->width, buf->height, vaddr);
+    err = module->lock(module, buf->handle,
+            usage, 0, 0, buf->width, buf->height, vaddr);
 
-   return err;
+    return err;
 }
 
-status_t egl_window_surface_v2_t::unlock(android_native_buffer_t* buf)
+status_t egl_window_surface_v2_t::unlock(ANativeWindowBuffer* buf)
 {
-   if (!buf) return BAD_VALUE;
-   int err = NO_ERROR;
+    if (!buf) return BAD_VALUE;
+    int err = NO_ERROR;
 
-   err = module->unlock(module, buf->handle);
+    err = module->unlock(module, buf->handle);
 
-   return err;
+    return err;
 }
 
 void egl_window_surface_v2_t::copyBlt(
-   android_native_buffer_t* dst, void* dst_vaddr,
-   android_native_buffer_t* src, void const* src_vaddr,
-   const Region& clip)
+        ANativeWindowBuffer* dst, void* dst_vaddr,
+        ANativeWindowBuffer* src, void const* src_vaddr,
+        const Region& clip)
 {
-   // FIXME: use copybit if possible
-   // NOTE: dst and src must be the same format
+    // NOTE: dst and src must be the same format
 
-   status_t err = NO_ERROR;
-   copybit_device_t* const copybit = blitengine;
-   if (copybit)  {
-      copybit_image_t simg;
-      simg.w = src->stride;
-      simg.h = src->height;
-      simg.format = src->format;
-      simg.handle = const_cast<native_handle_t*>(src->handle);
+    Region::const_iterator cur = clip.begin();
+    Region::const_iterator end = clip.end();
 
-      copybit_image_t dimg;
-      dimg.w = dst->stride;
-      dimg.h = dst->height;
-      dimg.format = dst->format;
-      dimg.handle = const_cast<native_handle_t*>(dst->handle);
+    const size_t bpp = pixelFormatTable[src->format].size;
+    const size_t dbpr = dst->stride * bpp;
+    const size_t sbpr = src->stride * bpp;
 
-      copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
-      copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
-      copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_DISABLE);
-      region_iterator it(clip);
-      err = copybit->blit(copybit, &dimg, &simg, &it);
-      if (err != NO_ERROR) {
-         LOGE("copybit failed (%s)", strerror(err));
-      }
-   }
+    uint8_t const * const src_bits = (uint8_t const *)src_vaddr;
+    uint8_t       * const dst_bits = (uint8_t       *)dst_vaddr;
 
-   if (!copybit || err) {
-      Region::const_iterator cur = clip.begin();
-      Region::const_iterator end = clip.end();
-
-      const size_t bpp = pixelFormatTable[src->format].size;
-      const size_t dbpr = dst->stride * bpp;
-      const size_t sbpr = src->stride * bpp;
-
-      uint8_t const * const src_bits = (uint8_t const *)src_vaddr;
-      uint8_t       * const dst_bits = (uint8_t       *)dst_vaddr;
-
-      while (cur != end) {
-         const Rect& r(*cur++);
-         ssize_t w = r.right - r.left;
-         ssize_t h = r.bottom - r.top;
-         if (w <= 0 || h<=0) continue;
-         size_t size = w * bpp;
-         uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
-         uint8_t       * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
-         if (dbpr==sbpr && size==sbpr) {
+    while (cur != end) {
+        const Rect& r(*cur++);
+        ssize_t w = r.right - r.left;
+        ssize_t h = r.bottom - r.top;
+        if (w <= 0 || h<=0) continue;
+        size_t size = w * bpp;
+        uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
+        uint8_t       * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
+        if (dbpr==sbpr && size==sbpr) {
             size *= h;
             h = 1;
-         }
-         do {
+        }
+        do {
             memcpy(d, s, size);
             d += dbpr;
             s += sbpr;
-         } while (--h > 0);
-      }
-   }
+        } while (--h > 0);
+    }
 }
 
 EGLBoolean egl_window_surface_v2_t::swapBuffers()
 {
-   if (!buffer) {
-      return setError(EGL_BAD_ACCESS, EGL_FALSE);
-   }
+    if (!buffer) {
+        return setError(EGL_BAD_ACCESS, EGL_FALSE);
+    }
 
-   /*
-    * Handle eglSetSwapRectangleANDROID()
-    * We copyback from the front buffer
-    */
-   if (!dirtyRegion.isEmpty()) {
-      dirtyRegion.andSelf(Rect(buffer->width, buffer->height));
-      if (previousBuffer) {
-         // This was const Region copyBack, but that causes an
-         // internal compile error on simulator builds
-         /*const*/
-         Region copyBack(Region::subtract(oldDirtyRegion, dirtyRegion));
-         if (!copyBack.isEmpty()) {
-            void* prevBits;
-            if (lock(previousBuffer,
-                     GRALLOC_USAGE_SW_READ_OFTEN, &prevBits) == NO_ERROR) {
-               // copy from previousBuffer to buffer
-               copyBlt(buffer, bits, previousBuffer, prevBits, copyBack);
-               unlock(previousBuffer);
+    /*
+     * Handle eglSetSwapRectangleANDROID()
+     * We copyback from the front buffer
+     */
+    if (!dirtyRegion.isEmpty()) {
+        dirtyRegion.andSelf(Rect(buffer->width, buffer->height));
+        if (previousBuffer) {
+            // This was const Region copyBack, but that causes an
+            // internal compile error on simulator builds
+            /*const*/
+            Region copyBack(Region::subtract(oldDirtyRegion, dirtyRegion));
+            if (!copyBack.isEmpty()) {
+                void* prevBits;
+                if (lock(previousBuffer,
+                        GRALLOC_USAGE_SW_READ_OFTEN, &prevBits) == NO_ERROR) {
+                    // copy from previousBuffer to buffer
+                    copyBlt(buffer, bits, previousBuffer, prevBits, copyBack);
+                    unlock(previousBuffer);
+                }
             }
-         }
-      }
-      oldDirtyRegion = dirtyRegion;
-   }
+        }
+        oldDirtyRegion = dirtyRegion;
+    }
 
-   if (previousBuffer) {
-      previousBuffer->common.decRef(&previousBuffer->common);
-      previousBuffer = 0;
-   }
+    if (previousBuffer) {
+        previousBuffer->common.decRef(&previousBuffer->common);
+        previousBuffer = 0;
+    }
 
-   unlock(buffer);
-   previousBuffer = buffer;
-   nativeWindow->queueBuffer(nativeWindow, buffer);
-   buffer = 0;
+    unlock(buffer);
+    previousBuffer = buffer;
+    nativeWindow->queueBuffer(nativeWindow, buffer);
+    buffer = 0;
 
-   // dequeue a new buffer
-   if (nativeWindow->dequeueBuffer(nativeWindow, &buffer) == NO_ERROR) {
+    // dequeue a new buffer
+    if (nativeWindow->dequeueBuffer(nativeWindow, &buffer) == NO_ERROR) {
 
-      // TODO: lockBuffer should rather be executed when the very first
-      // direct rendering occurs.
-      nativeWindow->lockBuffer(nativeWindow, buffer);
+        // TODO: lockBuffer should rather be executed when the very first
+        // direct rendering occurs.
+        nativeWindow->lockBuffer(nativeWindow, buffer);
 
-      // reallocate the depth-buffer if needed
-      if ((width != buffer->width) || (height != buffer->height)) {
-         // TODO: we probably should reset the swap rect here
-         // if the window size has changed
-         width = buffer->width;
-         height = buffer->height;
-         if (depth.data) {
-            free(depth.data);
-            depth.width   = width;
-            depth.height  = height;
-            depth.stride  = buffer->stride;
-            depth.data    = (GGLubyte*)malloc(depth.stride*depth.height*2);
-            if (depth.data == 0) {
-               setError(EGL_BAD_ALLOC, EGL_FALSE);
-               return EGL_FALSE;
+        // reallocate the depth-buffer if needed
+        if ((width != buffer->width) || (height != buffer->height)) {
+            // TODO: we probably should reset the swap rect here
+            // if the window size has changed
+            width = buffer->width;
+            height = buffer->height;
+            if (depth.data) {
+                free(depth.data);
+                depth.width   = width;
+                depth.height  = height;
+                depth.stride  = buffer->stride;
+                depth.data    = (GGLubyte*)malloc(depth.stride*depth.height*2);
+                if (depth.data == 0) {
+                    setError(EGL_BAD_ALLOC, EGL_FALSE);
+                    return EGL_FALSE;
+                }
             }
-         }
-      }
+        }
 
-      // keep a reference on the buffer
-      buffer->common.incRef(&buffer->common);
+        // keep a reference on the buffer
+        buffer->common.incRef(&buffer->common);
 
-      // finally pin the buffer down
-      if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
-               GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
-         LOGE("eglSwapBuffers() failed to lock buffer %p (%ux%u)",
-              buffer, buffer->width, buffer->height);
-         return setError(EGL_BAD_ACCESS, EGL_FALSE);
-         // FIXME: we should make sure we're not accessing the buffer anymore
-      }
-   } else {
-      return setError(EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
-   }
+        // finally pin the buffer down
+        if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN |
+                GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
+            LOGE("eglSwapBuffers() failed to lock buffer %p (%ux%u)",
+                    buffer, buffer->width, buffer->height);
+            return setError(EGL_BAD_ACCESS, EGL_FALSE);
+            // FIXME: we should make sure we're not accessing the buffer anymore
+        }
+    } else {
+        return setError(EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
+    }
 
-   return EGL_TRUE;
+    return EGL_TRUE;
 }
 
 EGLBoolean egl_window_surface_v2_t::setSwapRectangle(
-   EGLint l, EGLint t, EGLint w, EGLint h)
+        EGLint l, EGLint t, EGLint w, EGLint h)
 {
-   dirtyRegion = Rect(l, t, l+w, t+h);
-   return EGL_TRUE;
+    dirtyRegion = Rect(l, t, l+w, t+h);
+    return EGL_TRUE;
 }
 
 EGLBoolean egl_window_surface_v2_t::bindDrawSurface(GLES2Context* gl)
 {
-   GGLSurface buffer;
-   buffer.version = sizeof(GGLSurface);
-   buffer.width   = this->buffer->width;
-   buffer.height  = this->buffer->height;
-   buffer.stride  = this->buffer->stride;
-   buffer.data    = (GGLubyte*)bits;
-   buffer.format  = (GGLPixelFormat)this->buffer->format;
-   gl->rasterizer.interface.SetBuffer(&gl->rasterizer.interface, GL_COLOR_BUFFER_BIT, &buffer);
-   if (depth.data != gl->rasterizer.depthSurface.data)
-      gl->rasterizer.interface.SetBuffer(&gl->rasterizer.interface, GL_DEPTH_BUFFER_BIT, &depth);
+    GGLSurface buffer;
+    buffer.version = sizeof(GGLSurface);
+    buffer.width   = this->buffer->width;
+    buffer.height  = this->buffer->height;
+    buffer.stride  = this->buffer->stride;
+    buffer.data    = (GGLubyte*)bits;
+    buffer.format  = (GGLPixelFormat)this->buffer->format;
+    gl->rasterizer.interface.SetBuffer(&gl->rasterizer.interface, GL_COLOR_BUFFER_BIT, &buffer);
+    if (depth.data != gl->rasterizer.depthSurface.data)
+        gl->rasterizer.interface.SetBuffer(&gl->rasterizer.interface, GL_DEPTH_BUFFER_BIT, &depth);
 
-   return EGL_TRUE;
+    return EGL_TRUE;
 }
 EGLBoolean egl_window_surface_v2_t::bindReadSurface(GLES2Context* gl)
 {
-   GGLSurface buffer;
-   buffer.version = sizeof(GGLSurface);
-   buffer.width   = this->buffer->width;
-   buffer.height  = this->buffer->height;
-   buffer.stride  = this->buffer->stride;
-   buffer.data    = (GGLubyte*)bits; // FIXME: hopefully is is LOCKED!!!
-   buffer.format  = (GGLPixelFormat)this->buffer->format;
-   puts("agl2: readBuffer not implemented");
-   //gl->rasterizer.interface.readBuffer(gl, &buffer);
-   return EGL_TRUE;
+    GGLSurface buffer;
+    buffer.version = sizeof(GGLSurface);
+    buffer.width   = this->buffer->width;
+    buffer.height  = this->buffer->height;
+    buffer.stride  = this->buffer->stride;
+    buffer.data    = (GGLubyte*)bits; // FIXME: hopefully is is LOCKED!!!
+    buffer.format  = (GGLPixelFormat)this->buffer->format;
+    puts("agl2: readBuffer not implemented");
+    //gl->rasterizer.interface.readBuffer(gl, &buffer);
+    return EGL_TRUE;
 }
 EGLint egl_window_surface_v2_t::getHorizontalResolution() const
 {
-   return (nativeWindow->xdpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
+    return (nativeWindow->xdpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
 }
 EGLint egl_window_surface_v2_t::getVerticalResolution() const
 {
-   return (nativeWindow->ydpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
+    return (nativeWindow->ydpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f);
 }
 EGLint egl_window_surface_v2_t::getRefreshRate() const
 {
-   return (60 * EGL_DISPLAY_SCALING); // FIXME
+    return (60 * EGL_DISPLAY_SCALING); // FIXME
 }
 EGLint egl_window_surface_v2_t::getSwapBehavior() const
 {
-   /*
-    * EGL_BUFFER_PRESERVED means that eglSwapBuffers() completely preserves
-    * the content of the swapped buffer.
-    *
-    * EGL_BUFFER_DESTROYED means that the content of the buffer is lost.
-    *
-    * However when ANDROID_swap_retcangle is supported, EGL_BUFFER_DESTROYED
-    * only applies to the area specified by eglSetSwapRectangleANDROID(), that
-    * is, everything outside of this area is preserved.
-    *
-    * This implementation of EGL assumes the later case.
-    *
-    */
+    /*
+     * EGL_BUFFER_PRESERVED means that eglSwapBuffers() completely preserves
+     * the content of the swapped buffer.
+     *
+     * EGL_BUFFER_DESTROYED means that the content of the buffer is lost.
+     *
+     * However when ANDROID_swap_retcangle is supported, EGL_BUFFER_DESTROYED
+     * only applies to the area specified by eglSetSwapRectangleANDROID(), that
+     * is, everything outside of this area is preserved.
+     *
+     * This implementation of EGL assumes the later case.
+     *
+     */
 
-   return EGL_BUFFER_DESTROYED;
+    return EGL_BUFFER_DESTROYED;
 }
 
 // ----------------------------------------------------------------------------
 
 struct egl_pixmap_surface_t : public egl_surface_t {
-   egl_pixmap_surface_t(
-      EGLDisplay dpy, EGLConfig config,
-      int32_t depthFormat,
-      egl_native_pixmap_t const * pixmap);
+    egl_pixmap_surface_t(
+            EGLDisplay dpy, EGLConfig config,
+            int32_t depthFormat,
+            egl_native_pixmap_t const * pixmap);
 
-   virtual ~egl_pixmap_surface_t() { }
+    virtual ~egl_pixmap_surface_t() { }
 
-   virtual     bool        initCheck() const {
-      return !depth.format || depth.data!=0;
-   }
-   virtual     EGLBoolean  bindDrawSurface(GLES2Context* gl);
-   virtual     EGLBoolean  bindReadSurface(GLES2Context* gl);
-   virtual     EGLint      getWidth() const    {
-      return nativePixmap.width;
-   }
-   virtual     EGLint      getHeight() const   {
-      return nativePixmap.height;
-   }
+    virtual     bool        initCheck() const {
+        return !depth.format || depth.data!=0;
+    }
+    virtual     EGLBoolean  bindDrawSurface(GLES2Context* gl);
+    virtual     EGLBoolean  bindReadSurface(GLES2Context* gl);
+    virtual     EGLint      getWidth() const    {
+        return nativePixmap.width;
+    }
+    virtual     EGLint      getHeight() const   {
+        return nativePixmap.height;
+    }
 private:
-   egl_native_pixmap_t     nativePixmap;
+    egl_native_pixmap_t     nativePixmap;
 };
 
 egl_pixmap_surface_t::egl_pixmap_surface_t(EGLDisplay dpy,
-      EGLConfig config,
-      int32_t depthFormat,
-      egl_native_pixmap_t const * pixmap)
-      : egl_surface_t(dpy, config, depthFormat), nativePixmap(*pixmap)
+        EGLConfig config,
+        int32_t depthFormat,
+        egl_native_pixmap_t const * pixmap)
+: egl_surface_t(dpy, config, depthFormat), nativePixmap(*pixmap)
 {
-   if (depthFormat) {
-      depth.width   = pixmap->width;
-      depth.height  = pixmap->height;
-      depth.stride  = depth.width; // use the width here
-      depth.data    = (GGLubyte*)malloc(depth.stride*depth.height*2);
-      if (depth.data == 0) {
-         setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
-      }
-   }
+    if (depthFormat) {
+        depth.width   = pixmap->width;
+        depth.height  = pixmap->height;
+        depth.stride  = depth.width; // use the width here
+        depth.data    = (GGLubyte*)malloc(depth.stride*depth.height*2);
+        if (depth.data == 0) {
+            setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
+        }
+    }
 }
 EGLBoolean egl_pixmap_surface_t::bindDrawSurface(GLES2Context* gl)
 {
-   GGLSurface buffer;
-   buffer.version = sizeof(GGLSurface);
-   buffer.width   = nativePixmap.width;
-   buffer.height  = nativePixmap.height;
-   buffer.stride  = nativePixmap.stride;
-   buffer.data    = nativePixmap.data;
-   buffer.format  = (GGLPixelFormat)nativePixmap.format;
+    GGLSurface buffer;
+    buffer.version = sizeof(GGLSurface);
+    buffer.width   = nativePixmap.width;
+    buffer.height  = nativePixmap.height;
+    buffer.stride  = nativePixmap.stride;
+    buffer.data    = nativePixmap.data;
+    buffer.format  = (GGLPixelFormat)nativePixmap.format;
 
-   gl->rasterizer.interface.SetBuffer(&gl->rasterizer.interface, GL_COLOR_BUFFER_BIT, &buffer);
-   if (depth.data != gl->rasterizer.depthSurface.data)
-      gl->rasterizer.interface.SetBuffer(&gl->rasterizer.interface, GL_DEPTH_BUFFER_BIT, &depth);
-   return EGL_TRUE;
+    gl->rasterizer.interface.SetBuffer(&gl->rasterizer.interface, GL_COLOR_BUFFER_BIT, &buffer);
+    if (depth.data != gl->rasterizer.depthSurface.data)
+        gl->rasterizer.interface.SetBuffer(&gl->rasterizer.interface, GL_DEPTH_BUFFER_BIT, &depth);
+    return EGL_TRUE;
 }
 EGLBoolean egl_pixmap_surface_t::bindReadSurface(GLES2Context* gl)
 {
-   GGLSurface buffer;
-   buffer.version = sizeof(GGLSurface);
-   buffer.width   = nativePixmap.width;
-   buffer.height  = nativePixmap.height;
-   buffer.stride  = nativePixmap.stride;
-   buffer.data    = nativePixmap.data;
-   buffer.format  = (GGLPixelFormat)nativePixmap.format;
-   puts("agl2: readBuffer not implemented");
-   //gl->rasterizer.interface.readBuffer(gl, &buffer);
-   return EGL_TRUE;
+    GGLSurface buffer;
+    buffer.version = sizeof(GGLSurface);
+    buffer.width   = nativePixmap.width;
+    buffer.height  = nativePixmap.height;
+    buffer.stride  = nativePixmap.stride;
+    buffer.data    = nativePixmap.data;
+    buffer.format  = (GGLPixelFormat)nativePixmap.format;
+    puts("agl2: readBuffer not implemented");
+    //gl->rasterizer.interface.readBuffer(gl, &buffer);
+    return EGL_TRUE;
 }
 
 // ----------------------------------------------------------------------------
 
 struct egl_pbuffer_surface_t : public egl_surface_t {
-   egl_pbuffer_surface_t(
-      EGLDisplay dpy, EGLConfig config, int32_t depthFormat,
-      int32_t w, int32_t h, int32_t f);
+    egl_pbuffer_surface_t(
+            EGLDisplay dpy, EGLConfig config, int32_t depthFormat,
+            int32_t w, int32_t h, int32_t f);
 
-   virtual ~egl_pbuffer_surface_t();
+    virtual ~egl_pbuffer_surface_t();
 
-   virtual     bool        initCheck() const   {
-      return pbuffer.data != 0;
-   }
-   virtual     EGLBoolean  bindDrawSurface(GLES2Context* gl);
-   virtual     EGLBoolean  bindReadSurface(GLES2Context* gl);
-   virtual     EGLint      getWidth() const    {
-      return pbuffer.width;
-   }
-   virtual     EGLint      getHeight() const   {
-      return pbuffer.height;
-   }
+    virtual     bool        initCheck() const   {
+        return pbuffer.data != 0;
+    }
+    virtual     EGLBoolean  bindDrawSurface(GLES2Context* gl);
+    virtual     EGLBoolean  bindReadSurface(GLES2Context* gl);
+    virtual     EGLint      getWidth() const    {
+        return pbuffer.width;
+    }
+    virtual     EGLint      getHeight() const   {
+        return pbuffer.height;
+    }
 private:
-   GGLSurface  pbuffer;
+    GGLSurface  pbuffer;
 };
 
 egl_pbuffer_surface_t::egl_pbuffer_surface_t(EGLDisplay dpy,
-      EGLConfig config, int32_t depthFormat,
-      int32_t w, int32_t h, int32_t f)
-      : egl_surface_t(dpy, config, depthFormat)
+        EGLConfig config, int32_t depthFormat,
+        int32_t w, int32_t h, int32_t f)
+: egl_surface_t(dpy, config, depthFormat)
 {
-   size_t size = w*h;
-   switch (f) {
-   case GGL_PIXEL_FORMAT_A_8:
-      size *= 1;
-      break;
-   case GGL_PIXEL_FORMAT_RGB_565:
-      size *= 2;
-      break;
-   case GGL_PIXEL_FORMAT_RGBA_8888:
-      size *= 4;
-      break;
-   case GGL_PIXEL_FORMAT_RGBX_8888:
-      size *= 4;
-      break;
-   default:
-      LOGE("incompatible pixel format for pbuffer (format=%d)", f);
-      pbuffer.data = 0;
-      break;
-   }
-   pbuffer.version = sizeof(GGLSurface);
-   pbuffer.width   = w;
-   pbuffer.height  = h;
-   pbuffer.stride  = w;
-   pbuffer.data    = (GGLubyte*)malloc(size);
-   pbuffer.format  = (GGLPixelFormat)f;
+    size_t size = w*h;
+    switch (f) {
+        case GGL_PIXEL_FORMAT_A_8:
+            size *= 1;
+            break;
+        case GGL_PIXEL_FORMAT_RGB_565:
+            size *= 2;
+            break;
+        case GGL_PIXEL_FORMAT_RGBA_8888:
+            size *= 4;
+            break;
+        case GGL_PIXEL_FORMAT_RGBX_8888:
+            size *= 4;
+            break;
+        default:
+            LOGE("incompatible pixel format for pbuffer (format=%d)", f);
+            pbuffer.data = 0;
+            break;
+    }
+    pbuffer.version = sizeof(GGLSurface);
+    pbuffer.width   = w;
+    pbuffer.height  = h;
+    pbuffer.stride  = w;
+    pbuffer.data    = (GGLubyte*)malloc(size);
+    pbuffer.format  = (GGLPixelFormat)f;
 
-   if (depthFormat) {
-      depth.width   = pbuffer.width;
-      depth.height  = pbuffer.height;
-      depth.stride  = depth.width; // use the width here
-      depth.data    = (GGLubyte*)malloc(depth.stride*depth.height*2);
-      if (depth.data == 0) {
-         setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
-         return;
-      }
-   }
+    if (depthFormat) {
+        depth.width   = pbuffer.width;
+        depth.height  = pbuffer.height;
+        depth.stride  = depth.width; // use the width here
+        depth.data    = (GGLubyte*)malloc(depth.stride*depth.height*2);
+        if (depth.data == 0) {
+            setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
+            return;
+        }
+    }
 }
 egl_pbuffer_surface_t::~egl_pbuffer_surface_t()
 {
-   free(pbuffer.data);
+    free(pbuffer.data);
 }
 EGLBoolean egl_pbuffer_surface_t::bindDrawSurface(GLES2Context* gl)
 {
-   gl->rasterizer.interface.SetBuffer(&gl->rasterizer.interface, GL_COLOR_BUFFER_BIT, &pbuffer);
-   if (depth.data != gl->rasterizer.depthSurface.data)
-      gl->rasterizer.interface.SetBuffer(&gl->rasterizer.interface, GL_DEPTH_BUFFER_BIT, &depth);
-   return EGL_TRUE;
+    gl->rasterizer.interface.SetBuffer(&gl->rasterizer.interface, GL_COLOR_BUFFER_BIT, &pbuffer);
+    if (depth.data != gl->rasterizer.depthSurface.data)
+        gl->rasterizer.interface.SetBuffer(&gl->rasterizer.interface, GL_DEPTH_BUFFER_BIT, &depth);
+    return EGL_TRUE;
 }
 EGLBoolean egl_pbuffer_surface_t::bindReadSurface(GLES2Context* gl)
 {
-   puts("agl2: readBuffer not implemented");
-   //gl->rasterizer.interface.readBuffer(gl, &pbuffer);
-   return EGL_TRUE;
+    puts("agl2: readBuffer not implemented");
+    //gl->rasterizer.interface.readBuffer(gl, &pbuffer);
+    return EGL_TRUE;
 }
 
 // ----------------------------------------------------------------------------
 
 struct config_pair_t {
-   GLint key;
-   GLint value;
+    GLint key;
+    GLint value;
 };
 
 struct configs_t {
-   const config_pair_t* array;
-   int                  size;
+    const config_pair_t* array;
+    int                  size;
 };
 
 struct config_management_t {
-   GLint key;
-   bool (*match)(GLint reqValue, GLint confValue);
-   static bool atLeast(GLint reqValue, GLint confValue) {
-      return (reqValue == EGL_DONT_CARE) || (confValue >= reqValue);
-   }
-   static bool exact(GLint reqValue, GLint confValue) {
-      return (reqValue == EGL_DONT_CARE) || (confValue == reqValue);
-   }
-   static bool mask(GLint reqValue, GLint confValue) {
-      return (confValue & reqValue) == reqValue;
-   }
-   static bool ignore(GLint reqValue, GLint confValue) {
-      return true;
-   }
+    GLint key;
+    bool (*match)(GLint reqValue, GLint confValue);
+    static bool atLeast(GLint reqValue, GLint confValue) {
+        return (reqValue == EGL_DONT_CARE) || (confValue >= reqValue);
+    }
+    static bool exact(GLint reqValue, GLint confValue) {
+        return (reqValue == EGL_DONT_CARE) || (confValue == reqValue);
+    }
+    static bool mask(GLint reqValue, GLint confValue) {
+        return (confValue & reqValue) == reqValue;
+    }
+    static bool ignore(GLint reqValue, GLint confValue) {
+        return true;
+    }
 };
 
 // ----------------------------------------------------------------------------
@@ -865,62 +805,62 @@
 static char const * const gVersionString    = "0.0 Android Driver 0.0.0";
 static char const * const gClientApiString  = "OpenGL ES2";
 static char const * const gExtensionsString =
-   //"EGL_KHR_image_base "
-   // "KHR_image_pixmap "
-   //"EGL_ANDROID_image_native_buffer "
-   //"EGL_ANDROID_swap_rectangle "
-   "";
+        //"EGL_KHR_image_base "
+        // "KHR_image_pixmap "
+        //"EGL_ANDROID_image_native_buffer "
+        //"EGL_ANDROID_swap_rectangle "
+        "";
 
 // ----------------------------------------------------------------------------
 
 struct extention_map_t {
-   const char * const name;
-   __eglMustCastToProperFunctionPointerType address;
+    const char * const name;
+    __eglMustCastToProperFunctionPointerType address;
 };
 
 static const extention_map_t gExtentionMap[] = {
-//    { "glDrawTexsOES",
-//            (__eglMustCastToProperFunctionPointerType)&glDrawTexsOES },
-//    { "glDrawTexiOES",
-//            (__eglMustCastToProperFunctionPointerType)&glDrawTexiOES },
-//    { "glDrawTexfOES",
-//            (__eglMustCastToProperFunctionPointerType)&glDrawTexfOES },
-//    { "glDrawTexxOES",
-//            (__eglMustCastToProperFunctionPointerType)&glDrawTexxOES },
-//    { "glDrawTexsvOES",
-//            (__eglMustCastToProperFunctionPointerType)&glDrawTexsvOES },
-//    { "glDrawTexivOES",
-//            (__eglMustCastToProperFunctionPointerType)&glDrawTexivOES },
-//    { "glDrawTexfvOES",
-//            (__eglMustCastToProperFunctionPointerType)&glDrawTexfvOES },
-//    { "glDrawTexxvOES",
-//            (__eglMustCastToProperFunctionPointerType)&glDrawTexxvOES },
-//    { "glQueryMatrixxOES",
-//            (__eglMustCastToProperFunctionPointerType)&glQueryMatrixxOES },
-//    { "glEGLImageTargetTexture2DOES",
-//            (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetTexture2DOES },
-//    { "glEGLImageTargetRenderbufferStorageOES",
-//            (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetRenderbufferStorageOES },
-//    { "glClipPlanef",
-//            (__eglMustCastToProperFunctionPointerType)&glClipPlanef },
-//    { "glClipPlanex",
-//            (__eglMustCastToProperFunctionPointerType)&glClipPlanex },
-//    { "glBindBuffer",
-//            (__eglMustCastToProperFunctionPointerType)&glBindBuffer },
-//    { "glBufferData",
-//            (__eglMustCastToProperFunctionPointerType)&glBufferData },
-//    { "glBufferSubData",
-//            (__eglMustCastToProperFunctionPointerType)&glBufferSubData },
-//    { "glDeleteBuffers",
-//            (__eglMustCastToProperFunctionPointerType)&glDeleteBuffers },
-//    { "glGenBuffers",
-//            (__eglMustCastToProperFunctionPointerType)&glGenBuffers },
-//    { "eglCreateImageKHR",
-//            (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
-//    { "eglDestroyImageKHR",
-//            (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
-//    { "eglSetSwapRectangleANDROID",
-//            (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
+        //    { "glDrawTexsOES",
+        //            (__eglMustCastToProperFunctionPointerType)&glDrawTexsOES },
+        //    { "glDrawTexiOES",
+        //            (__eglMustCastToProperFunctionPointerType)&glDrawTexiOES },
+        //    { "glDrawTexfOES",
+        //            (__eglMustCastToProperFunctionPointerType)&glDrawTexfOES },
+        //    { "glDrawTexxOES",
+        //            (__eglMustCastToProperFunctionPointerType)&glDrawTexxOES },
+        //    { "glDrawTexsvOES",
+        //            (__eglMustCastToProperFunctionPointerType)&glDrawTexsvOES },
+        //    { "glDrawTexivOES",
+        //            (__eglMustCastToProperFunctionPointerType)&glDrawTexivOES },
+        //    { "glDrawTexfvOES",
+        //            (__eglMustCastToProperFunctionPointerType)&glDrawTexfvOES },
+        //    { "glDrawTexxvOES",
+        //            (__eglMustCastToProperFunctionPointerType)&glDrawTexxvOES },
+        //    { "glQueryMatrixxOES",
+        //            (__eglMustCastToProperFunctionPointerType)&glQueryMatrixxOES },
+        //    { "glEGLImageTargetTexture2DOES",
+        //            (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetTexture2DOES },
+        //    { "glEGLImageTargetRenderbufferStorageOES",
+        //            (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetRenderbufferStorageOES },
+        //    { "glClipPlanef",
+        //            (__eglMustCastToProperFunctionPointerType)&glClipPlanef },
+        //    { "glClipPlanex",
+        //            (__eglMustCastToProperFunctionPointerType)&glClipPlanex },
+        //    { "glBindBuffer",
+        //            (__eglMustCastToProperFunctionPointerType)&glBindBuffer },
+        //    { "glBufferData",
+        //            (__eglMustCastToProperFunctionPointerType)&glBufferData },
+        //    { "glBufferSubData",
+        //            (__eglMustCastToProperFunctionPointerType)&glBufferSubData },
+        //    { "glDeleteBuffers",
+        //            (__eglMustCastToProperFunctionPointerType)&glDeleteBuffers },
+        //    { "glGenBuffers",
+        //            (__eglMustCastToProperFunctionPointerType)&glGenBuffers },
+        //    { "eglCreateImageKHR",
+        //            (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
+        //    { "eglDestroyImageKHR",
+        //            (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
+        //    { "eglSetSwapRectangleANDROID",
+        //            (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
 };
 
 /*
@@ -930,31 +870,31 @@
  */
 
 static config_pair_t const config_base_attribute_list[] = {
-   { EGL_STENCIL_SIZE,               0                                 },
-   { EGL_CONFIG_CAVEAT,              EGL_SLOW_CONFIG                   },
-   { EGL_LEVEL,                      0                                 },
-   { EGL_MAX_PBUFFER_HEIGHT,         GGL_MAX_VIEWPORT_DIMS             },
-   { EGL_MAX_PBUFFER_PIXELS,
-     GGL_MAX_VIEWPORT_DIMS*GGL_MAX_VIEWPORT_DIMS                 },
-   { EGL_MAX_PBUFFER_WIDTH,          GGL_MAX_VIEWPORT_DIMS             },
-   { EGL_NATIVE_RENDERABLE,          EGL_TRUE                          },
-   { EGL_NATIVE_VISUAL_ID,           0                                 },
-   { EGL_NATIVE_VISUAL_TYPE,         GGL_PIXEL_FORMAT_RGBA_8888        },
-   { EGL_SAMPLES,                    0                                 },
-   { EGL_SAMPLE_BUFFERS,             0                                 },
-   { EGL_TRANSPARENT_TYPE,           EGL_NONE                          },
-   { EGL_TRANSPARENT_BLUE_VALUE,     0                                 },
-   { EGL_TRANSPARENT_GREEN_VALUE,    0                                 },
-   { EGL_TRANSPARENT_RED_VALUE,      0                                 },
-   { EGL_BIND_TO_TEXTURE_RGBA,       EGL_FALSE                         },
-   { EGL_BIND_TO_TEXTURE_RGB,        EGL_FALSE                         },
-   { EGL_MIN_SWAP_INTERVAL,          1                                 },
-   { EGL_MAX_SWAP_INTERVAL,          1                                 },
-   { EGL_LUMINANCE_SIZE,             0                                 },
-   { EGL_ALPHA_MASK_SIZE,            0                                 },
-   { EGL_COLOR_BUFFER_TYPE,          EGL_RGB_BUFFER                    },
-   { EGL_RENDERABLE_TYPE,            EGL_OPENGL_ES2_BIT                },
-   { EGL_CONFORMANT,                 0                                 }
+        { EGL_STENCIL_SIZE,               0                                 },
+        { EGL_CONFIG_CAVEAT,              EGL_SLOW_CONFIG                   },
+        { EGL_LEVEL,                      0                                 },
+        { EGL_MAX_PBUFFER_HEIGHT,         GGL_MAX_VIEWPORT_DIMS             },
+        { EGL_MAX_PBUFFER_PIXELS,
+                GGL_MAX_VIEWPORT_DIMS*GGL_MAX_VIEWPORT_DIMS                 },
+                { EGL_MAX_PBUFFER_WIDTH,          GGL_MAX_VIEWPORT_DIMS             },
+                { EGL_NATIVE_RENDERABLE,          EGL_TRUE                          },
+                { EGL_NATIVE_VISUAL_ID,           0                                 },
+                { EGL_NATIVE_VISUAL_TYPE,         GGL_PIXEL_FORMAT_RGBA_8888        },
+                { EGL_SAMPLES,                    0                                 },
+                { EGL_SAMPLE_BUFFERS,             0                                 },
+                { EGL_TRANSPARENT_TYPE,           EGL_NONE                          },
+                { EGL_TRANSPARENT_BLUE_VALUE,     0                                 },
+                { EGL_TRANSPARENT_GREEN_VALUE,    0                                 },
+                { EGL_TRANSPARENT_RED_VALUE,      0                                 },
+                { EGL_BIND_TO_TEXTURE_RGBA,       EGL_FALSE                         },
+                { EGL_BIND_TO_TEXTURE_RGB,        EGL_FALSE                         },
+                { EGL_MIN_SWAP_INTERVAL,          1                                 },
+                { EGL_MAX_SWAP_INTERVAL,          1                                 },
+                { EGL_LUMINANCE_SIZE,             0                                 },
+                { EGL_ALPHA_MASK_SIZE,            0                                 },
+                { EGL_COLOR_BUFFER_TYPE,          EGL_RGB_BUFFER                    },
+                { EGL_RENDERABLE_TYPE,            EGL_OPENGL_ES2_BIT                },
+                { EGL_CONFORMANT,                 0                                 }
 };
 
 // These configs can override the base attribute list
@@ -962,199 +902,199 @@
 
 // 565 configs
 static config_pair_t const config_0_attribute_list[] = {
-   { EGL_BUFFER_SIZE,     16 },
-   { EGL_ALPHA_SIZE,       0 },
-   { EGL_BLUE_SIZE,        5 },
-   { EGL_GREEN_SIZE,       6 },
-   { EGL_RED_SIZE,         5 },
-   { EGL_DEPTH_SIZE,       0 },
-   { EGL_CONFIG_ID,        0 },
-   { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGB_565 },
-   { EGL_SURFACE_TYPE,     EGL_SWAP_BEHAVIOR_PRESERVED_BIT|EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
+        { EGL_BUFFER_SIZE,     16 },
+        { EGL_ALPHA_SIZE,       0 },
+        { EGL_BLUE_SIZE,        5 },
+        { EGL_GREEN_SIZE,       6 },
+        { EGL_RED_SIZE,         5 },
+        { EGL_DEPTH_SIZE,       0 },
+        { EGL_CONFIG_ID,        0 },
+        { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGB_565 },
+        { EGL_SURFACE_TYPE,     EGL_SWAP_BEHAVIOR_PRESERVED_BIT|EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
 };
 
 static config_pair_t const config_1_attribute_list[] = {
-   { EGL_BUFFER_SIZE,     16 },
-   { EGL_ALPHA_SIZE,       0 },
-   { EGL_BLUE_SIZE,        5 },
-   { EGL_GREEN_SIZE,       6 },
-   { EGL_RED_SIZE,         5 },
-   { EGL_DEPTH_SIZE,      16 },
-   { EGL_CONFIG_ID,        1 },
-   { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGB_565 },
-   { EGL_SURFACE_TYPE,     EGL_SWAP_BEHAVIOR_PRESERVED_BIT|EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
+        { EGL_BUFFER_SIZE,     16 },
+        { EGL_ALPHA_SIZE,       0 },
+        { EGL_BLUE_SIZE,        5 },
+        { EGL_GREEN_SIZE,       6 },
+        { EGL_RED_SIZE,         5 },
+        { EGL_DEPTH_SIZE,      16 },
+        { EGL_CONFIG_ID,        1 },
+        { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGB_565 },
+        { EGL_SURFACE_TYPE,     EGL_SWAP_BEHAVIOR_PRESERVED_BIT|EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
 };
 
 // RGB 888 configs
 static config_pair_t const config_2_attribute_list[] = {
-   { EGL_BUFFER_SIZE,     32 },
-   { EGL_ALPHA_SIZE,       0 },
-   { EGL_BLUE_SIZE,        8 },
-   { EGL_GREEN_SIZE,       8 },
-   { EGL_RED_SIZE,         8 },
-   { EGL_DEPTH_SIZE,       0 },
-   { EGL_CONFIG_ID,        6 },
-   { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBX_8888 },
-   { EGL_SURFACE_TYPE,     EGL_SWAP_BEHAVIOR_PRESERVED_BIT|EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
+        { EGL_BUFFER_SIZE,     32 },
+        { EGL_ALPHA_SIZE,       0 },
+        { EGL_BLUE_SIZE,        8 },
+        { EGL_GREEN_SIZE,       8 },
+        { EGL_RED_SIZE,         8 },
+        { EGL_DEPTH_SIZE,       0 },
+        { EGL_CONFIG_ID,        6 },
+        { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBX_8888 },
+        { EGL_SURFACE_TYPE,     EGL_SWAP_BEHAVIOR_PRESERVED_BIT|EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
 };
 
 static config_pair_t const config_3_attribute_list[] = {
-   { EGL_BUFFER_SIZE,     32 },
-   { EGL_ALPHA_SIZE,       0 },
-   { EGL_BLUE_SIZE,        8 },
-   { EGL_GREEN_SIZE,       8 },
-   { EGL_RED_SIZE,         8 },
-   { EGL_DEPTH_SIZE,      16 },
-   { EGL_CONFIG_ID,        7 },
-   { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBX_8888 },
-   { EGL_SURFACE_TYPE,     EGL_SWAP_BEHAVIOR_PRESERVED_BIT|EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
+        { EGL_BUFFER_SIZE,     32 },
+        { EGL_ALPHA_SIZE,       0 },
+        { EGL_BLUE_SIZE,        8 },
+        { EGL_GREEN_SIZE,       8 },
+        { EGL_RED_SIZE,         8 },
+        { EGL_DEPTH_SIZE,      16 },
+        { EGL_CONFIG_ID,        7 },
+        { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBX_8888 },
+        { EGL_SURFACE_TYPE,     EGL_SWAP_BEHAVIOR_PRESERVED_BIT|EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
 };
 
 // 8888 configs
 static config_pair_t const config_4_attribute_list[] = {
-   { EGL_BUFFER_SIZE,     32 },
-   { EGL_ALPHA_SIZE,       8 },
-   { EGL_BLUE_SIZE,        8 },
-   { EGL_GREEN_SIZE,       8 },
-   { EGL_RED_SIZE,         8 },
-   { EGL_DEPTH_SIZE,       0 },
-   { EGL_CONFIG_ID,        2 },
-   { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBA_8888 },
-   { EGL_SURFACE_TYPE,     EGL_SWAP_BEHAVIOR_PRESERVED_BIT|EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
+        { EGL_BUFFER_SIZE,     32 },
+        { EGL_ALPHA_SIZE,       8 },
+        { EGL_BLUE_SIZE,        8 },
+        { EGL_GREEN_SIZE,       8 },
+        { EGL_RED_SIZE,         8 },
+        { EGL_DEPTH_SIZE,       0 },
+        { EGL_CONFIG_ID,        2 },
+        { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBA_8888 },
+        { EGL_SURFACE_TYPE,     EGL_SWAP_BEHAVIOR_PRESERVED_BIT|EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
 };
 
 static config_pair_t const config_5_attribute_list[] = {
-   { EGL_BUFFER_SIZE,     32 },
-   { EGL_ALPHA_SIZE,       8 },
-   { EGL_BLUE_SIZE,        8 },
-   { EGL_GREEN_SIZE,       8 },
-   { EGL_RED_SIZE,         8 },
-   { EGL_DEPTH_SIZE,      16 },
-   { EGL_CONFIG_ID,        3 },
-   { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBA_8888 },
-   { EGL_SURFACE_TYPE,     EGL_SWAP_BEHAVIOR_PRESERVED_BIT|EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
+        { EGL_BUFFER_SIZE,     32 },
+        { EGL_ALPHA_SIZE,       8 },
+        { EGL_BLUE_SIZE,        8 },
+        { EGL_GREEN_SIZE,       8 },
+        { EGL_RED_SIZE,         8 },
+        { EGL_DEPTH_SIZE,      16 },
+        { EGL_CONFIG_ID,        3 },
+        { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBA_8888 },
+        { EGL_SURFACE_TYPE,     EGL_SWAP_BEHAVIOR_PRESERVED_BIT|EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
 };
 
 // A8 configs
 static config_pair_t const config_6_attribute_list[] = {
-   { EGL_BUFFER_SIZE,      8 },
-   { EGL_ALPHA_SIZE,       8 },
-   { EGL_BLUE_SIZE,        0 },
-   { EGL_GREEN_SIZE,       0 },
-   { EGL_RED_SIZE,         0 },
-   { EGL_DEPTH_SIZE,       0 },
-   { EGL_CONFIG_ID,        4 },
-   { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_A_8 },
-   { EGL_SURFACE_TYPE,     EGL_SWAP_BEHAVIOR_PRESERVED_BIT|EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
+        { EGL_BUFFER_SIZE,      8 },
+        { EGL_ALPHA_SIZE,       8 },
+        { EGL_BLUE_SIZE,        0 },
+        { EGL_GREEN_SIZE,       0 },
+        { EGL_RED_SIZE,         0 },
+        { EGL_DEPTH_SIZE,       0 },
+        { EGL_CONFIG_ID,        4 },
+        { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_A_8 },
+        { EGL_SURFACE_TYPE,     EGL_SWAP_BEHAVIOR_PRESERVED_BIT|EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
 };
 
 static config_pair_t const config_7_attribute_list[] = {
-   { EGL_BUFFER_SIZE,      8 },
-   { EGL_ALPHA_SIZE,       8 },
-   { EGL_BLUE_SIZE,        0 },
-   { EGL_GREEN_SIZE,       0 },
-   { EGL_RED_SIZE,         0 },
-   { EGL_DEPTH_SIZE,      16 },
-   { EGL_CONFIG_ID,        5 },
-   { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_A_8 },
-   { EGL_SURFACE_TYPE,     EGL_SWAP_BEHAVIOR_PRESERVED_BIT|EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
+        { EGL_BUFFER_SIZE,      8 },
+        { EGL_ALPHA_SIZE,       8 },
+        { EGL_BLUE_SIZE,        0 },
+        { EGL_GREEN_SIZE,       0 },
+        { EGL_RED_SIZE,         0 },
+        { EGL_DEPTH_SIZE,      16 },
+        { EGL_CONFIG_ID,        5 },
+        { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_A_8 },
+        { EGL_SURFACE_TYPE,     EGL_SWAP_BEHAVIOR_PRESERVED_BIT|EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT },
 };
 
 static configs_t const gConfigs[] = {
-   { config_0_attribute_list, NELEM(config_0_attribute_list) },
-   { config_1_attribute_list, NELEM(config_1_attribute_list) },
-   { config_2_attribute_list, NELEM(config_2_attribute_list) },
-   { config_3_attribute_list, NELEM(config_3_attribute_list) },
-   { config_4_attribute_list, NELEM(config_4_attribute_list) },
-   { config_5_attribute_list, NELEM(config_5_attribute_list) },
-//   { config_6_attribute_list, NELEM(config_6_attribute_list) },
-//   { config_7_attribute_list, NELEM(config_7_attribute_list) },
+        { config_0_attribute_list, NELEM(config_0_attribute_list) },
+        { config_1_attribute_list, NELEM(config_1_attribute_list) },
+        { config_2_attribute_list, NELEM(config_2_attribute_list) },
+        { config_3_attribute_list, NELEM(config_3_attribute_list) },
+        { config_4_attribute_list, NELEM(config_4_attribute_list) },
+        { config_5_attribute_list, NELEM(config_5_attribute_list) },
+        //   { config_6_attribute_list, NELEM(config_6_attribute_list) },
+        //   { config_7_attribute_list, NELEM(config_7_attribute_list) },
 };
 
 static config_management_t const gConfigManagement[] = {
-   { EGL_BUFFER_SIZE,                config_management_t::atLeast },
-   { EGL_ALPHA_SIZE,                 config_management_t::atLeast },
-   { EGL_BLUE_SIZE,                  config_management_t::atLeast },
-   { EGL_GREEN_SIZE,                 config_management_t::atLeast },
-   { EGL_RED_SIZE,                   config_management_t::atLeast },
-   { EGL_DEPTH_SIZE,                 config_management_t::atLeast },
-   { EGL_STENCIL_SIZE,               config_management_t::atLeast },
-   { EGL_CONFIG_CAVEAT,              config_management_t::exact   },
-   { EGL_CONFIG_ID,                  config_management_t::exact   },
-   { EGL_LEVEL,                      config_management_t::exact   },
-   { EGL_MAX_PBUFFER_HEIGHT,         config_management_t::ignore   },
-   { EGL_MAX_PBUFFER_PIXELS,         config_management_t::ignore   },
-   { EGL_MAX_PBUFFER_WIDTH,          config_management_t::ignore   },
-   { EGL_NATIVE_RENDERABLE,          config_management_t::exact   },
-   { EGL_NATIVE_VISUAL_ID,           config_management_t::ignore   },
-   { EGL_NATIVE_VISUAL_TYPE,         config_management_t::exact   },
-   { EGL_SAMPLES,                    config_management_t::exact   },
-   { EGL_SAMPLE_BUFFERS,             config_management_t::exact   },
-   { EGL_SURFACE_TYPE,               config_management_t::mask    },
-   { EGL_TRANSPARENT_TYPE,           config_management_t::exact   },
-   { EGL_TRANSPARENT_BLUE_VALUE,     config_management_t::exact   },
-   { EGL_TRANSPARENT_GREEN_VALUE,    config_management_t::exact   },
-   { EGL_TRANSPARENT_RED_VALUE,      config_management_t::exact   },
-   { EGL_BIND_TO_TEXTURE_RGBA,       config_management_t::exact   },
-   { EGL_BIND_TO_TEXTURE_RGB,        config_management_t::exact   },
-   { EGL_MIN_SWAP_INTERVAL,          config_management_t::exact   },
-   { EGL_MAX_SWAP_INTERVAL,          config_management_t::exact   },
-   { EGL_LUMINANCE_SIZE,             config_management_t::atLeast },
-   { EGL_ALPHA_MASK_SIZE,            config_management_t::atLeast },
-   { EGL_COLOR_BUFFER_TYPE,          config_management_t::exact   },
-   { EGL_RENDERABLE_TYPE,            config_management_t::mask    },
-   { EGL_CONFORMANT,                 config_management_t::mask    }
+        { EGL_BUFFER_SIZE,                config_management_t::atLeast },
+        { EGL_ALPHA_SIZE,                 config_management_t::atLeast },
+        { EGL_BLUE_SIZE,                  config_management_t::atLeast },
+        { EGL_GREEN_SIZE,                 config_management_t::atLeast },
+        { EGL_RED_SIZE,                   config_management_t::atLeast },
+        { EGL_DEPTH_SIZE,                 config_management_t::atLeast },
+        { EGL_STENCIL_SIZE,               config_management_t::atLeast },
+        { EGL_CONFIG_CAVEAT,              config_management_t::exact   },
+        { EGL_CONFIG_ID,                  config_management_t::exact   },
+        { EGL_LEVEL,                      config_management_t::exact   },
+        { EGL_MAX_PBUFFER_HEIGHT,         config_management_t::ignore   },
+        { EGL_MAX_PBUFFER_PIXELS,         config_management_t::ignore   },
+        { EGL_MAX_PBUFFER_WIDTH,          config_management_t::ignore   },
+        { EGL_NATIVE_RENDERABLE,          config_management_t::exact   },
+        { EGL_NATIVE_VISUAL_ID,           config_management_t::ignore   },
+        { EGL_NATIVE_VISUAL_TYPE,         config_management_t::exact   },
+        { EGL_SAMPLES,                    config_management_t::exact   },
+        { EGL_SAMPLE_BUFFERS,             config_management_t::exact   },
+        { EGL_SURFACE_TYPE,               config_management_t::mask    },
+        { EGL_TRANSPARENT_TYPE,           config_management_t::exact   },
+        { EGL_TRANSPARENT_BLUE_VALUE,     config_management_t::exact   },
+        { EGL_TRANSPARENT_GREEN_VALUE,    config_management_t::exact   },
+        { EGL_TRANSPARENT_RED_VALUE,      config_management_t::exact   },
+        { EGL_BIND_TO_TEXTURE_RGBA,       config_management_t::exact   },
+        { EGL_BIND_TO_TEXTURE_RGB,        config_management_t::exact   },
+        { EGL_MIN_SWAP_INTERVAL,          config_management_t::exact   },
+        { EGL_MAX_SWAP_INTERVAL,          config_management_t::exact   },
+        { EGL_LUMINANCE_SIZE,             config_management_t::atLeast },
+        { EGL_ALPHA_MASK_SIZE,            config_management_t::atLeast },
+        { EGL_COLOR_BUFFER_TYPE,          config_management_t::exact   },
+        { EGL_RENDERABLE_TYPE,            config_management_t::mask    },
+        { EGL_CONFORMANT,                 config_management_t::mask    }
 };
 
 
 static config_pair_t const config_defaults[] = {
-   // attributes that are not specified are simply ignored, if a particular
-   // one needs not be ignored, it must be specified here, eg:
-   // { EGL_SURFACE_TYPE, EGL_WINDOW_BIT },
+        // attributes that are not specified are simply ignored, if a particular
+        // one needs not be ignored, it must be specified here, eg:
+        // { EGL_SURFACE_TYPE, EGL_WINDOW_BIT },
 };
 
 // ----------------------------------------------------------------------------
 
 static status_t getConfigFormatInfo(EGLint configID,
-                                    int32_t& pixelFormat, int32_t& depthFormat)
+        int32_t& pixelFormat, int32_t& depthFormat)
 {
-   switch (configID) {
-   case 0:
-      pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
-      depthFormat = 0;
-      break;
-   case 1:
-      pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
-      depthFormat = GGL_PIXEL_FORMAT_Z_32;
-      break;
-   case 2:
-      pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
-      depthFormat = 0;
-      break;
-   case 3:
-      pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
-      depthFormat = GGL_PIXEL_FORMAT_Z_32;
-      break;
-   case 4:
-      pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
-      depthFormat = 0;
-      break;
-   case 5:
-      pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
-      depthFormat = GGL_PIXEL_FORMAT_Z_32;
-      break;
-   case 6:
-      pixelFormat = GGL_PIXEL_FORMAT_A_8;
-      depthFormat = 0;
-      break;
-   case 7:
-      pixelFormat = GGL_PIXEL_FORMAT_A_8;
-      depthFormat = GGL_PIXEL_FORMAT_Z_32;
-      break;
-   default:
-      return NAME_NOT_FOUND;
-   }
-   return NO_ERROR;
+    switch (configID) {
+        case 0:
+            pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
+            depthFormat = 0;
+            break;
+        case 1:
+            pixelFormat = GGL_PIXEL_FORMAT_RGB_565;
+            depthFormat = GGL_PIXEL_FORMAT_Z_32;
+            break;
+        case 2:
+            pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
+            depthFormat = 0;
+            break;
+        case 3:
+            pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
+            depthFormat = GGL_PIXEL_FORMAT_Z_32;
+            break;
+        case 4:
+            pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
+            depthFormat = 0;
+            break;
+        case 5:
+            pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888;
+            depthFormat = GGL_PIXEL_FORMAT_Z_32;
+            break;
+        case 6:
+            pixelFormat = GGL_PIXEL_FORMAT_A_8;
+            depthFormat = 0;
+            break;
+        case 7:
+            pixelFormat = GGL_PIXEL_FORMAT_A_8;
+            depthFormat = GGL_PIXEL_FORMAT_Z_32;
+            break;
+        default:
+            return NAME_NOT_FOUND;
+    }
+    return NO_ERROR;
 }
 
 // ----------------------------------------------------------------------------
@@ -1162,256 +1102,256 @@
 template<typename T>
 static int binarySearch(T const sortedArray[], int first, int last, EGLint key)
 {
-   while (first <= last) {
-      int mid = (first + last) / 2;
-      if (key > sortedArray[mid].key) {
-         first = mid + 1;
-      } else if (key < sortedArray[mid].key) {
-         last = mid - 1;
-      } else {
-         return mid;
-      }
-   }
-   return -1;
+    while (first <= last) {
+        int mid = (first + last) / 2;
+        if (key > sortedArray[mid].key) {
+            first = mid + 1;
+        } else if (key < sortedArray[mid].key) {
+            last = mid - 1;
+        } else {
+            return mid;
+        }
+    }
+    return -1;
 }
 
 static int isAttributeMatching(int i, EGLint attr, EGLint val)
 {
-   // look for the attribute in all of our configs
-   config_pair_t const* configFound = gConfigs[i].array;
-   int index = binarySearch<config_pair_t>(
-                  gConfigs[i].array,
-                  0, gConfigs[i].size-1,
-                  attr);
-   if (index < 0) {
-      configFound = config_base_attribute_list;
-      index = binarySearch<config_pair_t>(
-                 config_base_attribute_list,
-                 0, NELEM(config_base_attribute_list)-1,
-                 attr);
-   }
-   if (index >= 0) {
-      // attribute found, check if this config could match
-      int cfgMgtIndex = binarySearch<config_management_t>(
-                           gConfigManagement,
-                           0, NELEM(gConfigManagement)-1,
-                           attr);
-      if (cfgMgtIndex >= 0) {
-         bool match = gConfigManagement[cfgMgtIndex].match(
-                         val, configFound[index].value);
-         if (match) {
-            // this config matches
-            return 1;
-         }
-      } else {
-         // attribute not found. this should NEVER happen.
-      }
-   } else {
-      // error, this attribute doesn't exist
-   }
-   return 0;
+    // look for the attribute in all of our configs
+    config_pair_t const* configFound = gConfigs[i].array;
+    int index = binarySearch<config_pair_t>(
+            gConfigs[i].array,
+            0, gConfigs[i].size-1,
+            attr);
+    if (index < 0) {
+        configFound = config_base_attribute_list;
+        index = binarySearch<config_pair_t>(
+                config_base_attribute_list,
+                0, NELEM(config_base_attribute_list)-1,
+                attr);
+    }
+    if (index >= 0) {
+        // attribute found, check if this config could match
+        int cfgMgtIndex = binarySearch<config_management_t>(
+                gConfigManagement,
+                0, NELEM(gConfigManagement)-1,
+                attr);
+        if (cfgMgtIndex >= 0) {
+            bool match = gConfigManagement[cfgMgtIndex].match(
+                    val, configFound[index].value);
+            if (match) {
+                // this config matches
+                return 1;
+            }
+        } else {
+            // attribute not found. this should NEVER happen.
+        }
+    } else {
+        // error, this attribute doesn't exist
+    }
+    return 0;
 }
 
 static int makeCurrent(GLES2Context* gl)
 {
-   GLES2Context* current = (GLES2Context*)getGlThreadSpecific();
-   if (gl) {
-      egl_context_t* c = egl_context_t::context(gl);
-      if (c->flags & egl_context_t::IS_CURRENT) {
-         if (current != gl) {
-            // it is an error to set a context current, if it's already
-            // current to another thread
-            return -1;
-         }
-      } else {
-         if (current) {
+    GLES2Context* current = (GLES2Context*)getGlThreadSpecific();
+    if (gl) {
+        egl_context_t* c = egl_context_t::context(gl);
+        if (c->flags & egl_context_t::IS_CURRENT) {
+            if (current != gl) {
+                // it is an error to set a context current, if it's already
+                // current to another thread
+                return -1;
+            }
+        } else {
+            if (current) {
+                // mark the current context as not current, and flush
+                glFlush();
+                egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
+            }
+        }
+        if (!(c->flags & egl_context_t::IS_CURRENT)) {
+            // The context is not current, make it current!
+            setGlThreadSpecific(gl);
+            c->flags |= egl_context_t::IS_CURRENT;
+        }
+    } else {
+        if (current) {
             // mark the current context as not current, and flush
             glFlush();
             egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
-         }
-      }
-      if (!(c->flags & egl_context_t::IS_CURRENT)) {
-         // The context is not current, make it current!
-         setGlThreadSpecific(gl);
-         c->flags |= egl_context_t::IS_CURRENT;
-      }
-   } else {
-      if (current) {
-         // mark the current context as not current, and flush
-         glFlush();
-         egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT;
-      }
-      // this thread has no context attached to it
-      setGlThreadSpecific(0);
-   }
-   return 0;
+        }
+        // this thread has no context attached to it
+        setGlThreadSpecific(0);
+    }
+    return 0;
 }
 
 static EGLBoolean getConfigAttrib(EGLDisplay dpy, EGLConfig config,
-                                  EGLint attribute, EGLint *value)
+        EGLint attribute, EGLint *value)
 {
-   size_t numConfigs =  NELEM(gConfigs);
-   int index = (int)config;
-   if (uint32_t(index) >= numConfigs)
-      return setError(EGL_BAD_CONFIG, EGL_FALSE);
+    size_t numConfigs =  NELEM(gConfigs);
+    int index = (int)config;
+    if (uint32_t(index) >= numConfigs)
+        return setError(EGL_BAD_CONFIG, EGL_FALSE);
 
-   int attrIndex;
-   attrIndex = binarySearch<config_pair_t>(
-                  gConfigs[index].array,
-                  0, gConfigs[index].size-1,
-                  attribute);
-   if (attrIndex>=0) {
-      *value = gConfigs[index].array[attrIndex].value;
-      return EGL_TRUE;
-   }
+    int attrIndex;
+    attrIndex = binarySearch<config_pair_t>(
+            gConfigs[index].array,
+            0, gConfigs[index].size-1,
+            attribute);
+    if (attrIndex>=0) {
+        *value = gConfigs[index].array[attrIndex].value;
+        return EGL_TRUE;
+    }
 
-   attrIndex = binarySearch<config_pair_t>(
-                  config_base_attribute_list,
-                  0, NELEM(config_base_attribute_list)-1,
-                  attribute);
-   if (attrIndex>=0) {
-      *value = config_base_attribute_list[attrIndex].value;
-      return EGL_TRUE;
-   }
-   return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
+    attrIndex = binarySearch<config_pair_t>(
+            config_base_attribute_list,
+            0, NELEM(config_base_attribute_list)-1,
+            attribute);
+    if (attrIndex>=0) {
+        *value = config_base_attribute_list[attrIndex].value;
+        return EGL_TRUE;
+    }
+    return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
 }
 
 static EGLSurface createWindowSurface(EGLDisplay dpy, EGLConfig config,
-                                      NativeWindowType window, const EGLint *attrib_list)
+        NativeWindowType window, const EGLint *attrib_list)
 {
-   if (egl_display_t::is_valid(dpy) == EGL_FALSE)
-      return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
-   if (window == 0)
-      return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
+    if (egl_display_t::is_valid(dpy) == EGL_FALSE)
+        return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
+    if (window == 0)
+        return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
 
-   EGLint surfaceType;
-   if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
-      return EGL_FALSE;
+    EGLint surfaceType;
+    if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
+        return EGL_FALSE;
 
-   if (!(surfaceType & EGL_WINDOW_BIT))
-      return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
+    if (!(surfaceType & EGL_WINDOW_BIT))
+        return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
 
-   if (reinterpret_cast<ANativeWindow*>(window)->common.magic !=
-         ANDROID_NATIVE_WINDOW_MAGIC) {
-      return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
-   }
+    if (reinterpret_cast<ANativeWindow*>(window)->common.magic !=
+            ANDROID_NATIVE_WINDOW_MAGIC) {
+        return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
+    }
 
-   EGLint configID;
-   if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
-      return EGL_FALSE;
+    EGLint configID;
+    if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
+        return EGL_FALSE;
 
-   int32_t depthFormat;
-   int32_t pixelFormat;
-   if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
-      return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
-   }
+    int32_t depthFormat;
+    int32_t pixelFormat;
+    if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
+        return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
+    }
 
-   // FIXME: we don't have access to the pixelFormat here just yet.
-   // (it's possible that the surface is not fully initialized)
-   // maybe this should be done after the page-flip
-   //if (EGLint(info.format) != pixelFormat)
-   //    return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
+    // FIXME: we don't have access to the pixelFormat here just yet.
+    // (it's possible that the surface is not fully initialized)
+    // maybe this should be done after the page-flip
+    //if (EGLint(info.format) != pixelFormat)
+    //    return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
 
-   egl_surface_t* surface;
-   surface = new egl_window_surface_v2_t(dpy, config, depthFormat,
-                                         reinterpret_cast<ANativeWindow*>(window));
+    egl_surface_t* surface;
+    surface = new egl_window_surface_v2_t(dpy, config, depthFormat,
+            reinterpret_cast<ANativeWindow*>(window));
 
-   if (!surface->initCheck()) {
-      // there was a problem in the ctor, the error
-      // flag has been set.
-      delete surface;
-      surface = 0;
-   }
-   return surface;
+    if (!surface->initCheck()) {
+        // there was a problem in the ctor, the error
+        // flag has been set.
+        delete surface;
+        surface = 0;
+    }
+    return surface;
 }
 
 static EGLSurface createPixmapSurface(EGLDisplay dpy, EGLConfig config,
-                                      NativePixmapType pixmap, const EGLint *attrib_list)
+        NativePixmapType pixmap, const EGLint *attrib_list)
 {
-   if (egl_display_t::is_valid(dpy) == EGL_FALSE)
-      return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
-   if (pixmap == 0)
-      return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
+    if (egl_display_t::is_valid(dpy) == EGL_FALSE)
+        return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
+    if (pixmap == 0)
+        return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
 
-   EGLint surfaceType;
-   if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
-      return EGL_FALSE;
+    EGLint surfaceType;
+    if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
+        return EGL_FALSE;
 
-   if (!(surfaceType & EGL_PIXMAP_BIT))
-      return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
+    if (!(surfaceType & EGL_PIXMAP_BIT))
+        return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
 
-   if (reinterpret_cast<egl_native_pixmap_t*>(pixmap)->version !=
-         sizeof(egl_native_pixmap_t)) {
-      return setError(EGL_BAD_NATIVE_PIXMAP, EGL_NO_SURFACE);
-   }
+    if (reinterpret_cast<egl_native_pixmap_t*>(pixmap)->version !=
+            sizeof(egl_native_pixmap_t)) {
+        return setError(EGL_BAD_NATIVE_PIXMAP, EGL_NO_SURFACE);
+    }
 
-   EGLint configID;
-   if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
-      return EGL_FALSE;
+    EGLint configID;
+    if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
+        return EGL_FALSE;
 
-   int32_t depthFormat;
-   int32_t pixelFormat;
-   if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
-      return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
-   }
+    int32_t depthFormat;
+    int32_t pixelFormat;
+    if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
+        return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
+    }
 
-   if (reinterpret_cast<egl_native_pixmap_t *>(pixmap)->format != pixelFormat)
-      return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
+    if (reinterpret_cast<egl_native_pixmap_t *>(pixmap)->format != pixelFormat)
+        return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
 
-   egl_surface_t* surface =
-      new egl_pixmap_surface_t(dpy, config, depthFormat,
-                               reinterpret_cast<egl_native_pixmap_t*>(pixmap));
+    egl_surface_t* surface =
+            new egl_pixmap_surface_t(dpy, config, depthFormat,
+                    reinterpret_cast<egl_native_pixmap_t*>(pixmap));
 
-   if (!surface->initCheck()) {
-      // there was a problem in the ctor, the error
-      // flag has been set.
-      delete surface;
-      surface = 0;
-   }
-   return surface;
+    if (!surface->initCheck()) {
+        // there was a problem in the ctor, the error
+        // flag has been set.
+        delete surface;
+        surface = 0;
+    }
+    return surface;
 }
 
 static EGLSurface createPbufferSurface(EGLDisplay dpy, EGLConfig config,
-                                       const EGLint *attrib_list)
+        const EGLint *attrib_list)
 {
-   if (egl_display_t::is_valid(dpy) == EGL_FALSE)
-      return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
+    if (egl_display_t::is_valid(dpy) == EGL_FALSE)
+        return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
 
-   EGLint surfaceType;
-   if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
-      return EGL_FALSE;
+    EGLint surfaceType;
+    if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)
+        return EGL_FALSE;
 
-   if (!(surfaceType & EGL_PBUFFER_BIT))
-      return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
+    if (!(surfaceType & EGL_PBUFFER_BIT))
+        return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
 
-   EGLint configID;
-   if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
-      return EGL_FALSE;
+    EGLint configID;
+    if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE)
+        return EGL_FALSE;
 
-   int32_t depthFormat;
-   int32_t pixelFormat;
-   if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
-      return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
-   }
+    int32_t depthFormat;
+    int32_t pixelFormat;
+    if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) {
+        return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
+    }
 
-   int32_t w = 0;
-   int32_t h = 0;
-   while (attrib_list[0]) {
-      if (attrib_list[0] == EGL_WIDTH)  w = attrib_list[1];
-      if (attrib_list[0] == EGL_HEIGHT) h = attrib_list[1];
-      attrib_list+=2;
-   }
+    int32_t w = 0;
+    int32_t h = 0;
+    while (attrib_list[0]) {
+        if (attrib_list[0] == EGL_WIDTH)  w = attrib_list[1];
+        if (attrib_list[0] == EGL_HEIGHT) h = attrib_list[1];
+        attrib_list+=2;
+    }
 
-   egl_surface_t* surface =
-      new egl_pbuffer_surface_t(dpy, config, depthFormat, w, h, pixelFormat);
+    egl_surface_t* surface =
+            new egl_pbuffer_surface_t(dpy, config, depthFormat, w, h, pixelFormat);
 
-   if (!surface->initCheck()) {
-      // there was a problem in the ctor, the error
-      // flag has been set.
-      delete surface;
-      surface = 0;
-   }
-   return surface;
+    if (!surface->initCheck()) {
+        // there was a problem in the ctor, the error
+        // flag has been set.
+        delete surface;
+        surface = 0;
+    }
+    return surface;
 }
 
 // ----------------------------------------------------------------------------
@@ -1426,61 +1366,61 @@
 
 EGLDisplay eglGetDisplay(NativeDisplayType display)
 {
-   puts("agl2:eglGetDisplay");
+    puts("agl2:eglGetDisplay");
 #ifndef HAVE_ANDROID_OS
-   // this just needs to be done once
-   if (gGLKey == -1) {
-      pthread_mutex_lock(&gInitMutex);
-      if (gGLKey == -1)
-         pthread_key_create(&gGLKey, NULL);
-      pthread_mutex_unlock(&gInitMutex);
-   }
+    // this just needs to be done once
+    if (gGLKey == -1) {
+        pthread_mutex_lock(&gInitMutex);
+        if (gGLKey == -1)
+            pthread_key_create(&gGLKey, NULL);
+        pthread_mutex_unlock(&gInitMutex);
+    }
 #endif
-   if (display == EGL_DEFAULT_DISPLAY) {
-      EGLDisplay dpy = (EGLDisplay)1;
-      egl_display_t& d = egl_display_t::get_display(dpy);
-      d.type = display;
-      return dpy;
-   }
-   return EGL_NO_DISPLAY;
+    if (display == EGL_DEFAULT_DISPLAY) {
+        EGLDisplay dpy = (EGLDisplay)1;
+        egl_display_t& d = egl_display_t::get_display(dpy);
+        d.type = display;
+        return dpy;
+    }
+    return EGL_NO_DISPLAY;
 }
 
 EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
 {
-   puts("agl2:eglInitialize");
-   if (egl_display_t::is_valid(dpy) == EGL_FALSE)
-      return setError(EGL_BAD_DISPLAY, EGL_FALSE);
+    puts("agl2:eglInitialize");
+    if (egl_display_t::is_valid(dpy) == EGL_FALSE)
+        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
 
-   EGLBoolean res = EGL_TRUE;
-   egl_display_t& d = egl_display_t::get_display(dpy);
+    EGLBoolean res = EGL_TRUE;
+    egl_display_t& d = egl_display_t::get_display(dpy);
 
-   if (android_atomic_inc(&d.initialized) == 0) {
-      // initialize stuff here if needed
-      //pthread_mutex_lock(&gInitMutex);
-      //pthread_mutex_unlock(&gInitMutex);
-   }
+    if (android_atomic_inc(&d.initialized) == 0) {
+        // initialize stuff here if needed
+        //pthread_mutex_lock(&gInitMutex);
+        //pthread_mutex_unlock(&gInitMutex);
+    }
 
-   if (res == EGL_TRUE) {
-      if (major != NULL) *major = VERSION_MAJOR;
-      if (minor != NULL) *minor = VERSION_MINOR;
-   }
-   return res;
+    if (res == EGL_TRUE) {
+        if (major != NULL) *major = VERSION_MAJOR;
+        if (minor != NULL) *minor = VERSION_MINOR;
+    }
+    return res;
 }
 
 EGLBoolean eglTerminate(EGLDisplay dpy)
 {
-   puts("agl2:eglTerminate");
-   if (egl_display_t::is_valid(dpy) == EGL_FALSE)
-      return setError(EGL_BAD_DISPLAY, EGL_FALSE);
+    puts("agl2:eglTerminate");
+    if (egl_display_t::is_valid(dpy) == EGL_FALSE)
+        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
 
-   EGLBoolean res = EGL_TRUE;
-   egl_display_t& d = egl_display_t::get_display(dpy);
-   if (android_atomic_dec(&d.initialized) == 1) {
-      // TODO: destroy all resources (surfaces, contexts, etc...)
-      //pthread_mutex_lock(&gInitMutex);
-      //pthread_mutex_unlock(&gInitMutex);
-   }
-   return res;
+    EGLBoolean res = EGL_TRUE;
+    egl_display_t& d = egl_display_t::get_display(dpy);
+    if (android_atomic_dec(&d.initialized) == 1) {
+        // TODO: destroy all resources (surfaces, contexts, etc...)
+        //pthread_mutex_lock(&gInitMutex);
+        //pthread_mutex_unlock(&gInitMutex);
+    }
+    return res;
 }
 
 // ----------------------------------------------------------------------------
@@ -1488,166 +1428,166 @@
 // ----------------------------------------------------------------------------
 
 EGLBoolean eglGetConfigs(   EGLDisplay dpy,
-                            EGLConfig *configs,
-                            EGLint config_size, EGLint *num_config)
+        EGLConfig *configs,
+        EGLint config_size, EGLint *num_config)
 {
-   puts("agl2:eglGetConfigs");
-   if (egl_display_t::is_valid(dpy) == EGL_FALSE)
-      return setError(EGL_BAD_DISPLAY, EGL_FALSE);
+    puts("agl2:eglGetConfigs");
+    if (egl_display_t::is_valid(dpy) == EGL_FALSE)
+        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
 
-   GLint numConfigs = NELEM(gConfigs);
-   if (!configs) {
-      *num_config = numConfigs;
-      return EGL_TRUE;
-   }
-   GLint i;
-   for (i=0 ; i<numConfigs && i<config_size ; i++) {
-      *configs++ = (EGLConfig)i;
-   }
-   *num_config = i;
-   return EGL_TRUE;
+    GLint numConfigs = NELEM(gConfigs);
+    if (!configs) {
+        *num_config = numConfigs;
+        return EGL_TRUE;
+    }
+    GLint i;
+    for (i=0 ; i<numConfigs && i<config_size ; i++) {
+        *configs++ = (EGLConfig)i;
+    }
+    *num_config = i;
+    return EGL_TRUE;
 }
 
 static const char * ATTRIBUTE_NAMES [] = {
-   "EGL_BUFFER_SIZE",
-   "EGL_ALPHA_SIZE",
-   "EGL_BLUE_SIZE",
-   "EGL_GREEN_SIZE",
-   "EGL_RED_SIZE",
-   "EGL_DEPTH_SIZE",
-   "EGL_STENCIL_SIZE",
-   "EGL_CONFIG_CAVEAT",
-   "EGL_CONFIG_ID",
-   "EGL_LEVEL",
-   "EGL_MAX_PBUFFER_HEIGHT",
-   "EGL_MAX_PBUFFER_PIXELS",
-   "EGL_MAX_PBUFFER_WIDTH",
-   "EGL_NATIVE_RENDERABLE",
-   "EGL_NATIVE_VISUAL_ID",
-   "EGL_NATIVE_VISUAL_TYPE",
-   "EGL_PRESERVED_RESOURCES",
-   "EGL_SAMPLES",
-   "EGL_SAMPLE_BUFFERS",
-   "EGL_SURFACE_TYPE",
-   "EGL_TRANSPARENT_TYPE",
-   "EGL_TRANSPARENT_BLUE_VALUE",
-   "EGL_TRANSPARENT_GREEN_VALUE",
-   "EGL_TRANSPARENT_RED_VALUE",
-   "EGL_NONE",   /* Attrib list terminator */
-   "EGL_BIND_TO_TEXTURE_RGB",
-   "EGL_BIND_TO_TEXTURE_RGBA",
-   "EGL_MIN_SWAP_INTERVAL",
-   "EGL_MAX_SWAP_INTERVAL",
-   "EGL_LUMINANCE_SIZE",
-   "EGL_ALPHA_MASK_SIZE",
-   "EGL_COLOR_BUFFER_TYPE",
-   "EGL_RENDERABLE_TYPE",
-   "EGL_MATCH_NATIVE_PIXMAP",   /* Pseudo-attribute (not queryable) */
-   "EGL_CONFORMANT",
+        "EGL_BUFFER_SIZE",
+        "EGL_ALPHA_SIZE",
+        "EGL_BLUE_SIZE",
+        "EGL_GREEN_SIZE",
+        "EGL_RED_SIZE",
+        "EGL_DEPTH_SIZE",
+        "EGL_STENCIL_SIZE",
+        "EGL_CONFIG_CAVEAT",
+        "EGL_CONFIG_ID",
+        "EGL_LEVEL",
+        "EGL_MAX_PBUFFER_HEIGHT",
+        "EGL_MAX_PBUFFER_PIXELS",
+        "EGL_MAX_PBUFFER_WIDTH",
+        "EGL_NATIVE_RENDERABLE",
+        "EGL_NATIVE_VISUAL_ID",
+        "EGL_NATIVE_VISUAL_TYPE",
+        "EGL_PRESERVED_RESOURCES",
+        "EGL_SAMPLES",
+        "EGL_SAMPLE_BUFFERS",
+        "EGL_SURFACE_TYPE",
+        "EGL_TRANSPARENT_TYPE",
+        "EGL_TRANSPARENT_BLUE_VALUE",
+        "EGL_TRANSPARENT_GREEN_VALUE",
+        "EGL_TRANSPARENT_RED_VALUE",
+        "EGL_NONE",   /* Attrib list terminator */
+        "EGL_BIND_TO_TEXTURE_RGB",
+        "EGL_BIND_TO_TEXTURE_RGBA",
+        "EGL_MIN_SWAP_INTERVAL",
+        "EGL_MAX_SWAP_INTERVAL",
+        "EGL_LUMINANCE_SIZE",
+        "EGL_ALPHA_MASK_SIZE",
+        "EGL_COLOR_BUFFER_TYPE",
+        "EGL_RENDERABLE_TYPE",
+        "EGL_MATCH_NATIVE_PIXMAP",   /* Pseudo-attribute (not queryable) */
+        "EGL_CONFORMANT",
 };
 
 EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
-                            EGLConfig *configs, EGLint config_size,
-                            EGLint *num_config)
+        EGLConfig *configs, EGLint config_size,
+        EGLint *num_config)
 {
-   puts("agl2:eglChooseConfig");
-   LOGD("\n***\n***\n agl2:LOGD eglChooseConfig \n***\n***\n");
-   if (egl_display_t::is_valid(dpy) == EGL_FALSE)
-      return setError(EGL_BAD_DISPLAY, EGL_FALSE);
+    puts("agl2:eglChooseConfig");
+    LOGD("\n***\n***\n agl2:LOGD eglChooseConfig \n***\n***\n");
+    if (egl_display_t::is_valid(dpy) == EGL_FALSE)
+        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
 
-   if (ggl_unlikely(num_config==0)) {
-      LOGD("\n***\n***\n num_config==0 \n***\n***\n");
-      return setError(EGL_BAD_PARAMETER, EGL_FALSE);
-   }
+    if (ggl_unlikely(num_config==0)) {
+        LOGD("\n***\n***\n num_config==0 \n***\n***\n");
+        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
+    }
 
-   if (ggl_unlikely(attrib_list==0)) {
-      /*
-       * A NULL attrib_list should be treated as though it was an empty
-       * one (terminated with EGL_NONE) as defined in
-       * section 3.4.1 "Querying Configurations" in the EGL specification.
-       */
-      LOGD("\n***\n***\n attrib_list==0 \n***\n***\n");
-      static const EGLint dummy = EGL_NONE;
-      attrib_list = &dummy;
-   }
+    if (ggl_unlikely(attrib_list==0)) {
+        /*
+         * A NULL attrib_list should be treated as though it was an empty
+         * one (terminated with EGL_NONE) as defined in
+         * section 3.4.1 "Querying Configurations" in the EGL specification.
+         */
+        LOGD("\n***\n***\n attrib_list==0 \n***\n***\n");
+        static const EGLint dummy = EGL_NONE;
+        attrib_list = &dummy;
+    }
 
-   for (const EGLint * attrib = attrib_list; *attrib != EGL_NONE; attrib += 2) {
-      LOGD("eglChooseConfig %s(%.4X): %d \n", ATTRIBUTE_NAMES[attrib[0] - EGL_BUFFER_SIZE], attrib[0], attrib[1]);
-      if (EGL_BUFFER_SIZE > attrib[0] || EGL_CONFORMANT < attrib[0])
-         LOGD("eglChooseConfig invalid config attrib: 0x%.4X=%d \n", attrib[0], attrib[1]);
-   }
+    for (const EGLint * attrib = attrib_list; *attrib != EGL_NONE; attrib += 2) {
+        LOGD("eglChooseConfig %s(%.4X): %d \n", ATTRIBUTE_NAMES[attrib[0] - EGL_BUFFER_SIZE], attrib[0], attrib[1]);
+        if (EGL_BUFFER_SIZE > attrib[0] || EGL_CONFORMANT < attrib[0])
+            LOGD("eglChooseConfig invalid config attrib: 0x%.4X=%d \n", attrib[0], attrib[1]);
+    }
 
-   int numAttributes = 0;
-   int numConfigs =  NELEM(gConfigs);
-   uint32_t possibleMatch = (1<<numConfigs)-1;
-   while (possibleMatch && *attrib_list != EGL_NONE) {
-      numAttributes++;
-      EGLint attr = *attrib_list++;
-      EGLint val  = *attrib_list++;
-      for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
-         if (!(possibleMatch & (1<<i)))
-            continue;
-         if (isAttributeMatching(i, attr, val) == 0) {
-            LOGD("!isAttributeMatching config(%d) %s=%d \n", i, ATTRIBUTE_NAMES[attr - EGL_BUFFER_SIZE], val);
-            possibleMatch &= ~(1<<i);
-         }
-      }
-   }
-
-   LOGD("eglChooseConfig possibleMatch=%.4X \n", possibleMatch);
-
-   // now, handle the attributes which have a useful default value
-   for (size_t j=0 ; possibleMatch && j<NELEM(config_defaults) ; j++) {
-      // see if this attribute was specified, if not, apply its
-      // default value
-      if (binarySearch<config_pair_t>(
-               (config_pair_t const*)attrib_list,
-               0, numAttributes-1,
-               config_defaults[j].key) < 0) {
-         for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
+    int numAttributes = 0;
+    int numConfigs =  NELEM(gConfigs);
+    uint32_t possibleMatch = (1<<numConfigs)-1;
+    while (possibleMatch && *attrib_list != EGL_NONE) {
+        numAttributes++;
+        EGLint attr = *attrib_list++;
+        EGLint val  = *attrib_list++;
+        for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
             if (!(possibleMatch & (1<<i)))
-               continue;
-            if (isAttributeMatching(i,
-                                    config_defaults[j].key,
-                                    config_defaults[j].value) == 0) {
-               possibleMatch &= ~(1<<i);
+                continue;
+            if (isAttributeMatching(i, attr, val) == 0) {
+                LOGD("!isAttributeMatching config(%d) %s=%d \n", i, ATTRIBUTE_NAMES[attr - EGL_BUFFER_SIZE], val);
+                possibleMatch &= ~(1<<i);
             }
-         }
-      }
-   }
+        }
+    }
 
-   // return the configurations found
-   int n=0;
-   if (possibleMatch) {
-      if (configs) {
-         for (int i=0 ; config_size && i<numConfigs ; i++) {
-            if (possibleMatch & (1<<i)) {
-               *configs++ = (EGLConfig)i;
-               config_size--;
-               n++;
+    LOGD("eglChooseConfig possibleMatch=%.4X \n", possibleMatch);
+
+    // now, handle the attributes which have a useful default value
+    for (size_t j=0 ; possibleMatch && j<NELEM(config_defaults) ; j++) {
+        // see if this attribute was specified, if not, apply its
+        // default value
+        if (binarySearch<config_pair_t>(
+                (config_pair_t const*)attrib_list,
+                0, numAttributes-1,
+                config_defaults[j].key) < 0) {
+            for (int i=0 ; possibleMatch && i<numConfigs ; i++) {
+                if (!(possibleMatch & (1<<i)))
+                    continue;
+                if (isAttributeMatching(i,
+                        config_defaults[j].key,
+                        config_defaults[j].value) == 0) {
+                    possibleMatch &= ~(1<<i);
+                }
             }
-         }
-      } else {
-         for (int i=0 ; i<numConfigs ; i++) {
-            if (possibleMatch & (1<<i)) {
-               n++;
+        }
+    }
+
+    // return the configurations found
+    int n=0;
+    if (possibleMatch) {
+        if (configs) {
+            for (int i=0 ; config_size && i<numConfigs ; i++) {
+                if (possibleMatch & (1<<i)) {
+                    *configs++ = (EGLConfig)i;
+                    config_size--;
+                    n++;
+                }
             }
-         }
-      }
-   }
-   *num_config = n;
-   LOGD("\n***\n***\n num_config==%d \n***\n***\n", *num_config);
-   return EGL_TRUE;
+        } else {
+            for (int i=0 ; i<numConfigs ; i++) {
+                if (possibleMatch & (1<<i)) {
+                    n++;
+                }
+            }
+        }
+    }
+    *num_config = n;
+    LOGD("\n***\n***\n num_config==%d \n***\n***\n", *num_config);
+    return EGL_TRUE;
 }
 
 EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
-                              EGLint attribute, EGLint *value)
+        EGLint attribute, EGLint *value)
 {
-   puts("agl2:eglGetConfigAttrib");
-   if (egl_display_t::is_valid(dpy) == EGL_FALSE)
-      return setError(EGL_BAD_DISPLAY, EGL_FALSE);
+    puts("agl2:eglGetConfigAttrib");
+    if (egl_display_t::is_valid(dpy) == EGL_FALSE)
+        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
 
-   return getConfigAttrib(dpy, config, attribute, value);
+    return getConfigAttrib(dpy, config, attribute, value);
 }
 
 // ----------------------------------------------------------------------------
@@ -1655,396 +1595,396 @@
 // ----------------------------------------------------------------------------
 
 EGLSurface eglCreateWindowSurface(  EGLDisplay dpy, EGLConfig config,
-                                    NativeWindowType window,
-                                    const EGLint *attrib_list)
+        NativeWindowType window,
+        const EGLint *attrib_list)
 {
-   puts("agl2:eglCreateWindowSurface");
-   return createWindowSurface(dpy, config, window, attrib_list);
+    puts("agl2:eglCreateWindowSurface");
+    return createWindowSurface(dpy, config, window, attrib_list);
 }
 
 EGLSurface eglCreatePixmapSurface(  EGLDisplay dpy, EGLConfig config,
-                                    NativePixmapType pixmap,
-                                    const EGLint *attrib_list)
+        NativePixmapType pixmap,
+        const EGLint *attrib_list)
 {
-   puts("agl2:eglCreatePixmapSurface");
-   return createPixmapSurface(dpy, config, pixmap, attrib_list);
+    puts("agl2:eglCreatePixmapSurface");
+    return createPixmapSurface(dpy, config, pixmap, attrib_list);
 }
 
 EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
-                                    const EGLint *attrib_list)
+        const EGLint *attrib_list)
 {
-   puts("agl2:eglCreatePbufferSurface");
-   return createPbufferSurface(dpy, config, attrib_list);
+    puts("agl2:eglCreatePbufferSurface");
+    return createPbufferSurface(dpy, config, attrib_list);
 }
 
 EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface eglSurface)
 {
-   puts("agl2:eglDestroySurface");
-   if (egl_display_t::is_valid(dpy) == EGL_FALSE)
-      return setError(EGL_BAD_DISPLAY, EGL_FALSE);
-   if (eglSurface != EGL_NO_SURFACE) {
-      egl_surface_t* surface( static_cast<egl_surface_t*>(eglSurface) );
-      if (!surface->isValid())
-         return setError(EGL_BAD_SURFACE, EGL_FALSE);
-      if (surface->dpy != dpy)
-         return setError(EGL_BAD_DISPLAY, EGL_FALSE);
-      if (surface->ctx) {
-         // FIXME: this surface is current check what the spec says
-         surface->disconnect();
-         surface->ctx = 0;
-      }
-      delete surface;
-   }
-   return EGL_TRUE;
+    puts("agl2:eglDestroySurface");
+    if (egl_display_t::is_valid(dpy) == EGL_FALSE)
+        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
+    if (eglSurface != EGL_NO_SURFACE) {
+        egl_surface_t* surface( static_cast<egl_surface_t*>(eglSurface) );
+        if (!surface->isValid())
+            return setError(EGL_BAD_SURFACE, EGL_FALSE);
+        if (surface->dpy != dpy)
+            return setError(EGL_BAD_DISPLAY, EGL_FALSE);
+        if (surface->ctx) {
+            // FIXME: this surface is current check what the spec says
+            surface->disconnect();
+            surface->ctx = 0;
+        }
+        delete surface;
+    }
+    return EGL_TRUE;
 }
 
 EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface eglSurface,
-                            EGLint attribute, EGLint *value)
+        EGLint attribute, EGLint *value)
 {
-   puts("agl2:eglQuerySurface");
-   if (egl_display_t::is_valid(dpy) == EGL_FALSE)
-      return setError(EGL_BAD_DISPLAY, EGL_FALSE);
-   egl_surface_t* surface = static_cast<egl_surface_t*>(eglSurface);
-   if (!surface->isValid())
-      return setError(EGL_BAD_SURFACE, EGL_FALSE);
-   if (surface->dpy != dpy)
-      return setError(EGL_BAD_DISPLAY, EGL_FALSE);
+    puts("agl2:eglQuerySurface");
+    if (egl_display_t::is_valid(dpy) == EGL_FALSE)
+        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
+    egl_surface_t* surface = static_cast<egl_surface_t*>(eglSurface);
+    if (!surface->isValid())
+        return setError(EGL_BAD_SURFACE, EGL_FALSE);
+    if (surface->dpy != dpy)
+        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
 
-   EGLBoolean ret = EGL_TRUE;
-   switch (attribute) {
-   case EGL_CONFIG_ID:
-      ret = getConfigAttrib(dpy, surface->config, EGL_CONFIG_ID, value);
-      break;
-   case EGL_WIDTH:
-      *value = surface->getWidth();
-      break;
-   case EGL_HEIGHT:
-      *value = surface->getHeight();
-      break;
-   case EGL_LARGEST_PBUFFER:
-      // not modified for a window or pixmap surface
-      break;
-   case EGL_TEXTURE_FORMAT:
-      *value = EGL_NO_TEXTURE;
-      break;
-   case EGL_TEXTURE_TARGET:
-      *value = EGL_NO_TEXTURE;
-      break;
-   case EGL_MIPMAP_TEXTURE:
-      *value = EGL_FALSE;
-      break;
-   case EGL_MIPMAP_LEVEL:
-      *value = 0;
-      break;
-   case EGL_RENDER_BUFFER:
-      // TODO: return the real RENDER_BUFFER here
-      *value = EGL_BACK_BUFFER;
-      break;
-   case EGL_HORIZONTAL_RESOLUTION:
-      // pixel/mm * EGL_DISPLAY_SCALING
-      *value = surface->getHorizontalResolution();
-      break;
-   case EGL_VERTICAL_RESOLUTION:
-      // pixel/mm * EGL_DISPLAY_SCALING
-      *value = surface->getVerticalResolution();
-      break;
-   case EGL_PIXEL_ASPECT_RATIO: {
-      // w/h * EGL_DISPLAY_SCALING
-      int wr = surface->getHorizontalResolution();
-      int hr = surface->getVerticalResolution();
-      *value = (wr * EGL_DISPLAY_SCALING) / hr;
-   }
-   break;
-   case EGL_SWAP_BEHAVIOR:
-      *value = surface->getSwapBehavior();
-      break;
-   default:
-      ret = setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
-   }
-   return ret;
+    EGLBoolean ret = EGL_TRUE;
+    switch (attribute) {
+        case EGL_CONFIG_ID:
+            ret = getConfigAttrib(dpy, surface->config, EGL_CONFIG_ID, value);
+            break;
+        case EGL_WIDTH:
+            *value = surface->getWidth();
+            break;
+        case EGL_HEIGHT:
+            *value = surface->getHeight();
+            break;
+        case EGL_LARGEST_PBUFFER:
+            // not modified for a window or pixmap surface
+            break;
+        case EGL_TEXTURE_FORMAT:
+            *value = EGL_NO_TEXTURE;
+            break;
+        case EGL_TEXTURE_TARGET:
+            *value = EGL_NO_TEXTURE;
+            break;
+        case EGL_MIPMAP_TEXTURE:
+            *value = EGL_FALSE;
+            break;
+        case EGL_MIPMAP_LEVEL:
+            *value = 0;
+            break;
+        case EGL_RENDER_BUFFER:
+            // TODO: return the real RENDER_BUFFER here
+            *value = EGL_BACK_BUFFER;
+            break;
+        case EGL_HORIZONTAL_RESOLUTION:
+            // pixel/mm * EGL_DISPLAY_SCALING
+            *value = surface->getHorizontalResolution();
+            break;
+        case EGL_VERTICAL_RESOLUTION:
+            // pixel/mm * EGL_DISPLAY_SCALING
+            *value = surface->getVerticalResolution();
+            break;
+        case EGL_PIXEL_ASPECT_RATIO: {
+            // w/h * EGL_DISPLAY_SCALING
+            int wr = surface->getHorizontalResolution();
+            int hr = surface->getVerticalResolution();
+            *value = (wr * EGL_DISPLAY_SCALING) / hr;
+        }
+        break;
+        case EGL_SWAP_BEHAVIOR:
+            *value = surface->getSwapBehavior();
+            break;
+        default:
+            ret = setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
+    }
+    return ret;
 }
 
 EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
-                            EGLContext share_list, const EGLint *attrib_list)
+        EGLContext share_list, const EGLint *attrib_list)
 {
-   puts("agl2:eglCreateContext");
-   if (egl_display_t::is_valid(dpy) == EGL_FALSE)
-      return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
+    puts("agl2:eglCreateContext");
+    if (egl_display_t::is_valid(dpy) == EGL_FALSE)
+        return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
 
-   GLES2Context* gl = new GLES2Context();//ogles_init(sizeof(egl_context_t));
-   if (!gl) return setError(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
+    GLES2Context* gl = new GLES2Context();//ogles_init(sizeof(egl_context_t));
+    if (!gl) return setError(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
 
-   //egl_context_t* c = static_cast<egl_context_t*>(gl->rasterizer.base);
-   egl_context_t * c = &gl->egl;
-   c->flags = egl_context_t::NEVER_CURRENT;
-   c->dpy = dpy;
-   c->config = config;
-   c->read = 0;
-   c->draw = 0;
-   
-   c->frame = 0;
-   c->lastSwapTime = clock();
-   c->accumulateSeconds = 0;
-   return (EGLContext)gl;
+    //egl_context_t* c = static_cast<egl_context_t*>(gl->rasterizer.base);
+    egl_context_t * c = &gl->egl;
+    c->flags = egl_context_t::NEVER_CURRENT;
+    c->dpy = dpy;
+    c->config = config;
+    c->read = 0;
+    c->draw = 0;
+
+    c->frame = 0;
+    c->lastSwapTime = clock();
+    c->accumulateSeconds = 0;
+    return (EGLContext)gl;
 }
 
 EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
 {
-   puts("agl2:eglDestroyContext");
-   if (egl_display_t::is_valid(dpy) == EGL_FALSE)
-      return setError(EGL_BAD_DISPLAY, EGL_FALSE);
-   egl_context_t* c = egl_context_t::context(ctx);
-   if (c->flags & egl_context_t::IS_CURRENT)
-      setGlThreadSpecific(0);
-   //ogles_uninit((GLES2Context*)ctx);
-   delete (GLES2Context*)ctx;
-   return EGL_TRUE;
+    puts("agl2:eglDestroyContext");
+    if (egl_display_t::is_valid(dpy) == EGL_FALSE)
+        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
+    egl_context_t* c = egl_context_t::context(ctx);
+    if (c->flags & egl_context_t::IS_CURRENT)
+        setGlThreadSpecific(0);
+    //ogles_uninit((GLES2Context*)ctx);
+    delete (GLES2Context*)ctx;
+    return EGL_TRUE;
 }
 
 EGLBoolean eglMakeCurrent(  EGLDisplay dpy, EGLSurface draw,
-                            EGLSurface read, EGLContext ctx)
+        EGLSurface read, EGLContext ctx)
 {
-   puts("agl2:eglMakeCurrent");
-   if (egl_display_t::is_valid(dpy) == EGL_FALSE)
-      return setError(EGL_BAD_DISPLAY, EGL_FALSE);
-   if (draw) {
-      egl_surface_t* s = (egl_surface_t*)draw;
-      if (!s->isValid())
-         return setError(EGL_BAD_SURFACE, EGL_FALSE);
-      if (s->dpy != dpy)
-         return setError(EGL_BAD_DISPLAY, EGL_FALSE);
-      // TODO: check that draw is compatible with the context
-   }
-   if (read && read!=draw) {
-      egl_surface_t* s = (egl_surface_t*)read;
-      if (!s->isValid())
-         return setError(EGL_BAD_SURFACE, EGL_FALSE);
-      if (s->dpy != dpy)
-         return setError(EGL_BAD_DISPLAY, EGL_FALSE);
-      // TODO: check that read is compatible with the context
-   }
+    puts("agl2:eglMakeCurrent");
+    if (egl_display_t::is_valid(dpy) == EGL_FALSE)
+        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
+    if (draw) {
+        egl_surface_t* s = (egl_surface_t*)draw;
+        if (!s->isValid())
+            return setError(EGL_BAD_SURFACE, EGL_FALSE);
+        if (s->dpy != dpy)
+            return setError(EGL_BAD_DISPLAY, EGL_FALSE);
+        // TODO: check that draw is compatible with the context
+    }
+    if (read && read!=draw) {
+        egl_surface_t* s = (egl_surface_t*)read;
+        if (!s->isValid())
+            return setError(EGL_BAD_SURFACE, EGL_FALSE);
+        if (s->dpy != dpy)
+            return setError(EGL_BAD_DISPLAY, EGL_FALSE);
+        // TODO: check that read is compatible with the context
+    }
 
-   EGLContext current_ctx = EGL_NO_CONTEXT;
+    EGLContext current_ctx = EGL_NO_CONTEXT;
 
-   if ((read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE) && (ctx != EGL_NO_CONTEXT))
-      return setError(EGL_BAD_MATCH, EGL_FALSE);
+    if ((read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE) && (ctx != EGL_NO_CONTEXT))
+        return setError(EGL_BAD_MATCH, EGL_FALSE);
 
-   if ((read != EGL_NO_SURFACE || draw != EGL_NO_SURFACE) && (ctx == EGL_NO_CONTEXT))
-      return setError(EGL_BAD_MATCH, EGL_FALSE);
+    if ((read != EGL_NO_SURFACE || draw != EGL_NO_SURFACE) && (ctx == EGL_NO_CONTEXT))
+        return setError(EGL_BAD_MATCH, EGL_FALSE);
 
-   if (ctx == EGL_NO_CONTEXT) {
-      // if we're detaching, we need the current context
-      current_ctx = (EGLContext)getGlThreadSpecific();
-   } else {
-      egl_context_t* c = egl_context_t::context(ctx);
-      egl_surface_t* d = (egl_surface_t*)draw;
-      egl_surface_t* r = (egl_surface_t*)read;
-      if ((d && d->ctx && d->ctx != ctx) ||
-            (r && r->ctx && r->ctx != ctx)) {
-         // one of the surface is bound to a context in another thread
-         return setError(EGL_BAD_ACCESS, EGL_FALSE);
-      }
-   }
+    if (ctx == EGL_NO_CONTEXT) {
+        // if we're detaching, we need the current context
+        current_ctx = (EGLContext)getGlThreadSpecific();
+    } else {
+        egl_context_t* c = egl_context_t::context(ctx);
+        egl_surface_t* d = (egl_surface_t*)draw;
+        egl_surface_t* r = (egl_surface_t*)read;
+        if ((d && d->ctx && d->ctx != ctx) ||
+                (r && r->ctx && r->ctx != ctx)) {
+            // one of the surface is bound to a context in another thread
+            return setError(EGL_BAD_ACCESS, EGL_FALSE);
+        }
+    }
 
-   GLES2Context* gl = (GLES2Context*)ctx;
-   if (makeCurrent(gl) == 0) {
-      if (ctx) {
-         egl_context_t* c = egl_context_t::context(ctx);
-         egl_surface_t* d = (egl_surface_t*)draw;
-         egl_surface_t* r = (egl_surface_t*)read;
+    GLES2Context* gl = (GLES2Context*)ctx;
+    if (makeCurrent(gl) == 0) {
+        if (ctx) {
+            egl_context_t* c = egl_context_t::context(ctx);
+            egl_surface_t* d = (egl_surface_t*)draw;
+            egl_surface_t* r = (egl_surface_t*)read;
 
-         if (c->draw) {
-            egl_surface_t* s = reinterpret_cast<egl_surface_t*>(c->draw);
-            s->disconnect();
-         }
-         if (c->read) {
-            // FIXME: unlock/disconnect the read surface too
-         }
-
-         c->draw = draw;
-         c->read = read;
-
-         if (c->flags & egl_context_t::NEVER_CURRENT) {
-            c->flags &= ~egl_context_t::NEVER_CURRENT;
-            GLint w = 0;
-            GLint h = 0;
-            if (draw) {
-               w = d->getWidth();
-               h = d->getHeight();
+            if (c->draw) {
+                egl_surface_t* s = reinterpret_cast<egl_surface_t*>(c->draw);
+                s->disconnect();
             }
-            gl->rasterizer.interface.Viewport(&gl->rasterizer.interface, 0, 0, w, h);
-            //ogles_surfaceport(gl, 0, 0);
-            //ogles_viewport(gl, 0, 0, w, h);
-            //ogles_scissor(gl, 0, 0, w, h);
-         }
-         if (d) {
-            if (d->connect() == EGL_FALSE) {
-               return EGL_FALSE;
+            if (c->read) {
+                // FIXME: unlock/disconnect the read surface too
             }
-            d->ctx = ctx;
-            d->bindDrawSurface(gl);
-         }
-         if (r) {
-            // FIXME: lock/connect the read surface too
-            r->ctx = ctx;
-            r->bindReadSurface(gl);
-         }
-      } else {
-         // if surfaces were bound to the context bound to this thread
-         // mark then as unbound.
-         if (current_ctx) {
-            egl_context_t* c = egl_context_t::context(current_ctx);
-            egl_surface_t* d = (egl_surface_t*)c->draw;
-            egl_surface_t* r = (egl_surface_t*)c->read;
+
+            c->draw = draw;
+            c->read = read;
+
+            if (c->flags & egl_context_t::NEVER_CURRENT) {
+                c->flags &= ~egl_context_t::NEVER_CURRENT;
+                GLint w = 0;
+                GLint h = 0;
+                if (draw) {
+                    w = d->getWidth();
+                    h = d->getHeight();
+                }
+                gl->rasterizer.interface.Viewport(&gl->rasterizer.interface, 0, 0, w, h);
+                //ogles_surfaceport(gl, 0, 0);
+                //ogles_viewport(gl, 0, 0, w, h);
+                //ogles_scissor(gl, 0, 0, w, h);
+            }
             if (d) {
-               c->draw = 0;
-               d->ctx = EGL_NO_CONTEXT;
-               d->disconnect();
+                if (d->connect() == EGL_FALSE) {
+                    return EGL_FALSE;
+                }
+                d->ctx = ctx;
+                d->bindDrawSurface(gl);
             }
             if (r) {
-               c->read = 0;
-               r->ctx = EGL_NO_CONTEXT;
-               // FIXME: unlock/disconnect the read surface too
+                // FIXME: lock/connect the read surface too
+                r->ctx = ctx;
+                r->bindReadSurface(gl);
             }
-         }
-      }
-      return EGL_TRUE;
-   }
-   return setError(EGL_BAD_ACCESS, EGL_FALSE);
+        } else {
+            // if surfaces were bound to the context bound to this thread
+            // mark then as unbound.
+            if (current_ctx) {
+                egl_context_t* c = egl_context_t::context(current_ctx);
+                egl_surface_t* d = (egl_surface_t*)c->draw;
+                egl_surface_t* r = (egl_surface_t*)c->read;
+                if (d) {
+                    c->draw = 0;
+                    d->ctx = EGL_NO_CONTEXT;
+                    d->disconnect();
+                }
+                if (r) {
+                    c->read = 0;
+                    r->ctx = EGL_NO_CONTEXT;
+                    // FIXME: unlock/disconnect the read surface too
+                }
+            }
+        }
+        return EGL_TRUE;
+    }
+    return setError(EGL_BAD_ACCESS, EGL_FALSE);
 }
 
 EGLContext eglGetCurrentContext(void)
 {
-   // eglGetCurrentContext returns the current EGL rendering context,
-   // as specified by eglMakeCurrent. If no context is current,
-   // EGL_NO_CONTEXT is returned.
-   return (EGLContext)getGlThreadSpecific();
+    // eglGetCurrentContext returns the current EGL rendering context,
+    // as specified by eglMakeCurrent. If no context is current,
+    // EGL_NO_CONTEXT is returned.
+    return (EGLContext)getGlThreadSpecific();
 }
 
 EGLSurface eglGetCurrentSurface(EGLint readdraw)
 {
-   // eglGetCurrentSurface returns the read or draw surface attached
-   // to the current EGL rendering context, as specified by eglMakeCurrent.
-   // If no context is current, EGL_NO_SURFACE is returned.
-   EGLContext ctx = (EGLContext)getGlThreadSpecific();
-   if (ctx == EGL_NO_CONTEXT) return EGL_NO_SURFACE;
-   egl_context_t* c = egl_context_t::context(ctx);
-   if (readdraw == EGL_READ) {
-      return c->read;
-   } else if (readdraw == EGL_DRAW) {
-      return c->draw;
-   }
-   return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
+    // eglGetCurrentSurface returns the read or draw surface attached
+    // to the current EGL rendering context, as specified by eglMakeCurrent.
+    // If no context is current, EGL_NO_SURFACE is returned.
+    EGLContext ctx = (EGLContext)getGlThreadSpecific();
+    if (ctx == EGL_NO_CONTEXT) return EGL_NO_SURFACE;
+    egl_context_t* c = egl_context_t::context(ctx);
+    if (readdraw == EGL_READ) {
+        return c->read;
+    } else if (readdraw == EGL_DRAW) {
+        return c->draw;
+    }
+    return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
 }
 
 EGLDisplay eglGetCurrentDisplay(void)
 {
-   // eglGetCurrentDisplay returns the current EGL display connection
-   // for the current EGL rendering context, as specified by eglMakeCurrent.
-   // If no context is current, EGL_NO_DISPLAY is returned.
-   EGLContext ctx = (EGLContext)getGlThreadSpecific();
-   if (ctx == EGL_NO_CONTEXT) return EGL_NO_DISPLAY;
-   egl_context_t* c = egl_context_t::context(ctx);
-   return c->dpy;
+    // eglGetCurrentDisplay returns the current EGL display connection
+    // for the current EGL rendering context, as specified by eglMakeCurrent.
+    // If no context is current, EGL_NO_DISPLAY is returned.
+    EGLContext ctx = (EGLContext)getGlThreadSpecific();
+    if (ctx == EGL_NO_CONTEXT) return EGL_NO_DISPLAY;
+    egl_context_t* c = egl_context_t::context(ctx);
+    return c->dpy;
 }
 
 EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
-                            EGLint attribute, EGLint *value)
+        EGLint attribute, EGLint *value)
 {
-   if (egl_display_t::is_valid(dpy) == EGL_FALSE)
-      return setError(EGL_BAD_DISPLAY, EGL_FALSE);
-   egl_context_t* c = egl_context_t::context(ctx);
-   switch (attribute) {
-   case EGL_CONFIG_ID:
-      // Returns the ID of the EGL frame buffer configuration with
-      // respect to which the context was created
-      return getConfigAttrib(dpy, c->config, EGL_CONFIG_ID, value);
-   }
-   return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
+    if (egl_display_t::is_valid(dpy) == EGL_FALSE)
+        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
+    egl_context_t* c = egl_context_t::context(ctx);
+    switch (attribute) {
+        case EGL_CONFIG_ID:
+            // Returns the ID of the EGL frame buffer configuration with
+            // respect to which the context was created
+            return getConfigAttrib(dpy, c->config, EGL_CONFIG_ID, value);
+    }
+    return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
 }
 
 EGLBoolean eglWaitGL(void)
 {
-   return EGL_TRUE;
+    return EGL_TRUE;
 }
 
 EGLBoolean eglWaitNative(EGLint engine)
 {
-   return EGL_TRUE;
+    return EGL_TRUE;
 }
 
 EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
 {
-   if (egl_display_t::is_valid(dpy) == EGL_FALSE)
-      return setError(EGL_BAD_DISPLAY, EGL_FALSE);
+    if (egl_display_t::is_valid(dpy) == EGL_FALSE)
+        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
 
-   egl_surface_t* d = static_cast<egl_surface_t*>(draw);
-   if (!d->isValid())
-      return setError(EGL_BAD_SURFACE, EGL_FALSE);
-   if (d->dpy != dpy)
-      return setError(EGL_BAD_DISPLAY, EGL_FALSE);
+    egl_surface_t* d = static_cast<egl_surface_t*>(draw);
+    if (!d->isValid())
+        return setError(EGL_BAD_SURFACE, EGL_FALSE);
+    if (d->dpy != dpy)
+        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
 
-   // post the surface
-   d->swapBuffers();
+    // post the surface
+    d->swapBuffers();
 
-   // if it's bound to a context, update the buffer
-   if (d->ctx != EGL_NO_CONTEXT) {
-      d->bindDrawSurface((GLES2Context*)d->ctx);
-      // if this surface is also the read surface of the context
-      // it is bound to, make sure to update the read buffer as well.
-      // The EGL spec is a little unclear about this.
-      egl_context_t* c = egl_context_t::context(d->ctx);
-      if (c->read == draw) {
-         d->bindReadSurface((GLES2Context*)d->ctx);
-      }
-      clock_t time = clock();
-      float elapsed = (float)(time - c->lastSwapTime) / CLOCKS_PER_SEC;
-      c->accumulateSeconds += elapsed;
-      c->frame++;
-//      LOGD("agl2: eglSwapBuffers elapsed=%.2fms \n*", elapsed * 1000);
-      if (20 == c->frame) {
-         float avg = c->accumulateSeconds / c->frame;
-         LOGD("\n*\n* agl2: eglSwapBuffers %u frame avg fps=%.1f elapsed=%.2fms \n*",
-              c->frame, 1 / avg, avg * 1000);
-         c->frame = 0;
-         c->accumulateSeconds = 0;
-      }
-      c->lastSwapTime = time;
-   }
+    // if it's bound to a context, update the buffer
+    if (d->ctx != EGL_NO_CONTEXT) {
+        d->bindDrawSurface((GLES2Context*)d->ctx);
+        // if this surface is also the read surface of the context
+        // it is bound to, make sure to update the read buffer as well.
+        // The EGL spec is a little unclear about this.
+        egl_context_t* c = egl_context_t::context(d->ctx);
+        if (c->read == draw) {
+            d->bindReadSurface((GLES2Context*)d->ctx);
+        }
+        clock_t time = clock();
+        float elapsed = (float)(time - c->lastSwapTime) / CLOCKS_PER_SEC;
+        c->accumulateSeconds += elapsed;
+        c->frame++;
+        //      LOGD("agl2: eglSwapBuffers elapsed=%.2fms \n*", elapsed * 1000);
+        if (20 == c->frame) {
+            float avg = c->accumulateSeconds / c->frame;
+            LOGD("\n*\n* agl2: eglSwapBuffers %u frame avg fps=%.1f elapsed=%.2fms \n*",
+                    c->frame, 1 / avg, avg * 1000);
+            c->frame = 0;
+            c->accumulateSeconds = 0;
+        }
+        c->lastSwapTime = time;
+    }
 
-   return EGL_TRUE;
+    return EGL_TRUE;
 }
 
 EGLBoolean eglCopyBuffers(  EGLDisplay dpy, EGLSurface surface,
-                            NativePixmapType target)
+        NativePixmapType target)
 {
-   if (egl_display_t::is_valid(dpy) == EGL_FALSE)
-      return setError(EGL_BAD_DISPLAY, EGL_FALSE);
-   // TODO: eglCopyBuffers()
-   return EGL_FALSE;
+    if (egl_display_t::is_valid(dpy) == EGL_FALSE)
+        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
+    // TODO: eglCopyBuffers()
+    return EGL_FALSE;
 }
 
 EGLint eglGetError(void)
 {
-   return getError();
+    return getError();
 }
 
 const char* eglQueryString(EGLDisplay dpy, EGLint name)
 {
-   if (egl_display_t::is_valid(dpy) == EGL_FALSE)
-      return setError(EGL_BAD_DISPLAY, (const char*)0);
+    if (egl_display_t::is_valid(dpy) == EGL_FALSE)
+        return setError(EGL_BAD_DISPLAY, (const char*)0);
 
-   switch (name) {
-   case EGL_VENDOR:
-      return gVendorString;
-   case EGL_VERSION:
-      return gVersionString;
-   case EGL_EXTENSIONS:
-      return gExtensionsString;
-   case EGL_CLIENT_APIS:
-      return gClientApiString;
-   }
-   return setError(EGL_BAD_PARAMETER, (const char *)0);
+    switch (name) {
+        case EGL_VENDOR:
+            return gVendorString;
+        case EGL_VERSION:
+            return gVersionString;
+        case EGL_EXTENSIONS:
+            return gExtensionsString;
+        case EGL_CLIENT_APIS:
+            return gClientApiString;
+    }
+    return setError(EGL_BAD_PARAMETER, (const char *)0);
 }
 
 // ----------------------------------------------------------------------------
@@ -2052,38 +1992,38 @@
 // ----------------------------------------------------------------------------
 
 EGLBoolean eglSurfaceAttrib(
-   EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
+        EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
 {
-   if (egl_display_t::is_valid(dpy) == EGL_FALSE)
-      return setError(EGL_BAD_DISPLAY, EGL_FALSE);
-   // TODO: eglSurfaceAttrib()
-   return setError(EGL_BAD_PARAMETER, EGL_FALSE);
+    if (egl_display_t::is_valid(dpy) == EGL_FALSE)
+        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
+    // TODO: eglSurfaceAttrib()
+    return setError(EGL_BAD_PARAMETER, EGL_FALSE);
 }
 
 EGLBoolean eglBindTexImage(
-   EGLDisplay dpy, EGLSurface surface, EGLint buffer)
+        EGLDisplay dpy, EGLSurface surface, EGLint buffer)
 {
-   if (egl_display_t::is_valid(dpy) == EGL_FALSE)
-      return setError(EGL_BAD_DISPLAY, EGL_FALSE);
-   // TODO: eglBindTexImage()
-   return setError(EGL_BAD_PARAMETER, EGL_FALSE);
+    if (egl_display_t::is_valid(dpy) == EGL_FALSE)
+        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
+    // TODO: eglBindTexImage()
+    return setError(EGL_BAD_PARAMETER, EGL_FALSE);
 }
 
 EGLBoolean eglReleaseTexImage(
-   EGLDisplay dpy, EGLSurface surface, EGLint buffer)
+        EGLDisplay dpy, EGLSurface surface, EGLint buffer)
 {
-   if (egl_display_t::is_valid(dpy) == EGL_FALSE)
-      return setError(EGL_BAD_DISPLAY, EGL_FALSE);
-   // TODO: eglReleaseTexImage()
-   return setError(EGL_BAD_PARAMETER, EGL_FALSE);
+    if (egl_display_t::is_valid(dpy) == EGL_FALSE)
+        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
+    // TODO: eglReleaseTexImage()
+    return setError(EGL_BAD_PARAMETER, EGL_FALSE);
 }
 
 EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
 {
-   if (egl_display_t::is_valid(dpy) == EGL_FALSE)
-      return setError(EGL_BAD_DISPLAY, EGL_FALSE);
-   // TODO: eglSwapInterval()
-   return EGL_TRUE;
+    if (egl_display_t::is_valid(dpy) == EGL_FALSE)
+        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
+    // TODO: eglSwapInterval()
+    return EGL_TRUE;
 }
 
 // ----------------------------------------------------------------------------
@@ -2092,36 +2032,36 @@
 
 EGLBoolean eglBindAPI(EGLenum api)
 {
-   if (api != EGL_OPENGL_ES_API)
-      return setError(EGL_BAD_PARAMETER, EGL_FALSE);
-   return EGL_TRUE;
+    if (api != EGL_OPENGL_ES_API)
+        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
+    return EGL_TRUE;
 }
 
 EGLenum eglQueryAPI(void)
 {
-   return EGL_OPENGL_ES_API;
+    return EGL_OPENGL_ES_API;
 }
 
 EGLBoolean eglWaitClient(void)
 {
-   glFinish();
-   return EGL_TRUE;
+    glFinish();
+    return EGL_TRUE;
 }
 
 EGLBoolean eglReleaseThread(void)
 {
-   // TODO: eglReleaseThread()
-   return EGL_TRUE;
+    // TODO: eglReleaseThread()
+    return EGL_TRUE;
 }
 
 EGLSurface eglCreatePbufferFromClientBuffer(
-   EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
-   EGLConfig config, const EGLint *attrib_list)
+        EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
+        EGLConfig config, const EGLint *attrib_list)
 {
-   if (egl_display_t::is_valid(dpy) == EGL_FALSE)
-      return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
-   // TODO: eglCreatePbufferFromClientBuffer()
-   return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
+    if (egl_display_t::is_valid(dpy) == EGL_FALSE)
+        return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE);
+    // TODO: eglCreatePbufferFromClientBuffer()
+    return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
 }
 
 // ----------------------------------------------------------------------------
@@ -2129,84 +2069,84 @@
 // ----------------------------------------------------------------------------
 
 void (*eglGetProcAddress (const char *procname))()
-{
-   extention_map_t const * const map = gExtentionMap;
-   for (uint32_t i=0 ; i<NELEM(gExtentionMap) ; i++) {
-      if (!strcmp(procname, map[i].name)) {
-         return map[i].address;
-      }
-   }
-   return NULL;
-}
+        {
+    extention_map_t const * const map = gExtentionMap;
+    for (uint32_t i=0 ; i<NELEM(gExtentionMap) ; i++) {
+        if (!strcmp(procname, map[i].name)) {
+            return map[i].address;
+        }
+    }
+    return NULL;
+        }
 
 EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
-                             const EGLint *attrib_list)
+        const EGLint *attrib_list)
 {
-   EGLBoolean result = EGL_FALSE;
-   return result;
+    EGLBoolean result = EGL_FALSE;
+    return result;
 }
 
 EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
 {
-   EGLBoolean result = EGL_FALSE;
-   return result;
+    EGLBoolean result = EGL_FALSE;
+    return result;
 }
 
 EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
-                              EGLClientBuffer buffer, const EGLint *attrib_list)
+        EGLClientBuffer buffer, const EGLint *attrib_list)
 {
-   if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
-      return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
-   }
-   if (ctx != EGL_NO_CONTEXT) {
-      return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
-   }
-   if (target != EGL_NATIVE_BUFFER_ANDROID) {
-      return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
-   }
+    if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
+        return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
+    }
+    if (ctx != EGL_NO_CONTEXT) {
+        return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
+    }
+    if (target != EGL_NATIVE_BUFFER_ANDROID) {
+        return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
+    }
 
-   android_native_buffer_t* native_buffer = (android_native_buffer_t*)buffer;
+    ANativeWindowBuffer* native_buffer = (ANativeWindowBuffer*)buffer;
 
-   if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
-      return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
+    if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
+        return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
 
-   if (native_buffer->common.version != sizeof(android_native_buffer_t))
-      return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
+    if (native_buffer->common.version != sizeof(ANativeWindowBuffer))
+        return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
 
-   switch (native_buffer->format) {
-   case HAL_PIXEL_FORMAT_RGBA_8888:
-   case HAL_PIXEL_FORMAT_RGBX_8888:
-   case HAL_PIXEL_FORMAT_RGB_888:
-   case HAL_PIXEL_FORMAT_RGB_565:
-   case HAL_PIXEL_FORMAT_BGRA_8888:
-   case HAL_PIXEL_FORMAT_RGBA_5551:
-   case HAL_PIXEL_FORMAT_RGBA_4444:
-      break;
-   default:
-      return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
-   }
+    switch (native_buffer->format) {
+        case HAL_PIXEL_FORMAT_RGBA_8888:
+        case HAL_PIXEL_FORMAT_RGBX_8888:
+        case HAL_PIXEL_FORMAT_RGB_888:
+        case HAL_PIXEL_FORMAT_RGB_565:
+        case HAL_PIXEL_FORMAT_BGRA_8888:
+        case HAL_PIXEL_FORMAT_RGBA_5551:
+        case HAL_PIXEL_FORMAT_RGBA_4444:
+            break;
+        default:
+            return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
+    }
 
-   native_buffer->common.incRef(&native_buffer->common);
-   return (EGLImageKHR)native_buffer;
+    native_buffer->common.incRef(&native_buffer->common);
+    return (EGLImageKHR)native_buffer;
 }
 
 EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
 {
-   if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
-      return setError(EGL_BAD_DISPLAY, EGL_FALSE);
-   }
+    if (egl_display_t::is_valid(dpy) == EGL_FALSE) {
+        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
+    }
 
-   android_native_buffer_t* native_buffer = (android_native_buffer_t*)img;
+    ANativeWindowBuffer* native_buffer = (ANativeWindowBuffer*)img;
 
-   if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
-      return setError(EGL_BAD_PARAMETER, EGL_FALSE);
+    if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
+        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
 
-   if (native_buffer->common.version != sizeof(android_native_buffer_t))
-      return setError(EGL_BAD_PARAMETER, EGL_FALSE);
+    if (native_buffer->common.version != sizeof(ANativeWindowBuffer))
+        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
 
-   native_buffer->common.decRef(&native_buffer->common);
+    native_buffer->common.decRef(&native_buffer->common);
 
-   return EGL_TRUE;
+    return EGL_TRUE;
 }
 
 // ----------------------------------------------------------------------------
@@ -2214,19 +2154,19 @@
 // ----------------------------------------------------------------------------
 
 EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
-                                      EGLint left, EGLint top, EGLint width, EGLint height)
+        EGLint left, EGLint top, EGLint width, EGLint height)
 {
-   if (egl_display_t::is_valid(dpy) == EGL_FALSE)
-      return setError(EGL_BAD_DISPLAY, EGL_FALSE);
+    if (egl_display_t::is_valid(dpy) == EGL_FALSE)
+        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
 
-   egl_surface_t* d = static_cast<egl_surface_t*>(draw);
-   if (!d->isValid())
-      return setError(EGL_BAD_SURFACE, EGL_FALSE);
-   if (d->dpy != dpy)
-      return setError(EGL_BAD_DISPLAY, EGL_FALSE);
+    egl_surface_t* d = static_cast<egl_surface_t*>(draw);
+    if (!d->isValid())
+        return setError(EGL_BAD_SURFACE, EGL_FALSE);
+    if (d->dpy != dpy)
+        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
 
-   // post the surface
-   d->setSwapRectangle(left, top, width, height);
+    // post the surface
+    d->setSwapRectangle(left, top, width, height);
 
-   return EGL_TRUE;
+    return EGL_TRUE;
 }
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 7506f29..e8f0328 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -395,7 +395,7 @@
     if (LIKELY(mTransactionCount == 0)) {
         // if we're in a global transaction, don't do anything.
         const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
-        uint32_t transactionFlags = getTransactionFlags(mask);
+        uint32_t transactionFlags = peekTransactionFlags(mask);
         if (LIKELY(transactionFlags)) {
             handleTransaction(transactionFlags);
         }
@@ -490,7 +490,17 @@
         Mutex::Autolock _l(mStateLock);
         const nsecs_t now = systemTime();
         mDebugInTransaction = now;
+
+        // Here we're guaranteed that some transaction flags are set
+        // so we can call handleTransactionLocked() unconditionally.
+        // We call getTransactionFlags(), which will also clear the flags,
+        // with mStateLock held to guarantee that mCurrentState won't change
+        // until the transaction is commited.
+
+        const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
+        transactionFlags = getTransactionFlags(mask);
         handleTransactionLocked(transactionFlags, ditchedLayers);
+
         mLastTransactionTime = systemTime() - now;
         mDebugInTransaction = 0;
         invalidateHwcGeometry();
@@ -1094,15 +1104,15 @@
 ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
         const sp<LayerBaseClient>& lbc)
 {
-    Mutex::Autolock _l(mStateLock);
-
     // attach this layer to the client
-    ssize_t name = client->attachLayer(lbc);
+    size_t name = client->attachLayer(lbc);
+
+    Mutex::Autolock _l(mStateLock);
 
     // add this layer to the current state list
     addLayer_l(lbc);
 
-    return name;
+    return ssize_t(name);
 }
 
 status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
@@ -1153,6 +1163,11 @@
     return NO_ERROR;
 }
 
+uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
+{
+    return android_atomic_release_load(&mTransactionFlags);
+}
+
 uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
 {
     return android_atomic_and(~flags, &mTransactionFlags) & flags;
@@ -2381,15 +2396,17 @@
     return NO_ERROR;
 }
 
-ssize_t Client::attachLayer(const sp<LayerBaseClient>& layer)
+size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
 {
-    int32_t name = android_atomic_inc(&mNameGenerator);
+    Mutex::Autolock _l(mLock);
+    size_t name = mNameGenerator++;
     mLayers.add(name, layer);
     return name;
 }
 
 void Client::detachLayer(const LayerBaseClient* layer)
 {
+    Mutex::Autolock _l(mLock);
     // we do a linear search here, because this doesn't happen often
     const size_t count = mLayers.size();
     for (size_t i=0 ; i<count ; i++) {
@@ -2399,9 +2416,11 @@
         }
     }
 }
-sp<LayerBaseClient> Client::getLayerUser(int32_t i) const {
+sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
+{
+    Mutex::Autolock _l(mLock);
     sp<LayerBaseClient> lbc;
-    const wp<LayerBaseClient>& layer(mLayers.valueFor(i));
+    wp<LayerBaseClient> layer(mLayers.valueFor(i));
     if (layer != 0) {
         lbc = layer.promote();
         LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
diff --git a/services/surfaceflinger/SurfaceFlinger.h b/services/surfaceflinger/SurfaceFlinger.h
index 1b36d1c..1e16943 100644
--- a/services/surfaceflinger/SurfaceFlinger.h
+++ b/services/surfaceflinger/SurfaceFlinger.h
@@ -65,7 +65,7 @@
     status_t initCheck() const;
 
     // protected by SurfaceFlinger::mStateLock
-    ssize_t attachLayer(const sp<LayerBaseClient>& layer);
+    size_t attachLayer(const sp<LayerBaseClient>& layer);
     void detachLayer(const LayerBaseClient* layer);
     sp<LayerBaseClient> getLayerUser(int32_t i) const;
 
@@ -81,9 +81,15 @@
     virtual status_t destroySurface(SurfaceID surfaceId);
     virtual status_t setState(int32_t count, const layer_state_t* states);
 
-    DefaultKeyedVector< size_t, wp<LayerBaseClient> > mLayers;
+    // constant
     sp<SurfaceFlinger> mFlinger;
-    int32_t mNameGenerator;
+
+    // protected by mLock
+    DefaultKeyedVector< size_t, wp<LayerBaseClient> > mLayers;
+    size_t mNameGenerator;
+
+    // thread-safe
+    mutable Mutex mLock;
 };
 
 class UserClient : public BnSurfaceComposerClient
@@ -319,6 +325,7 @@
             status_t    purgatorizeLayer_l(const sp<LayerBase>& layer);
 
             uint32_t    getTransactionFlags(uint32_t flags);
+            uint32_t    peekTransactionFlags(uint32_t flags);
             uint32_t    setTransactionFlags(uint32_t flags);
             void        commitTransaction();
 
diff --git a/services/surfaceflinger/TextureManager.cpp b/services/surfaceflinger/TextureManager.cpp
index c9a15f5..bb63c37 100644
--- a/services/surfaceflinger/TextureManager.cpp
+++ b/services/surfaceflinger/TextureManager.cpp
@@ -144,7 +144,7 @@
     }
 
     // construct an EGL_NATIVE_BUFFER_ANDROID
-    android_native_buffer_t* clientBuf = buffer->getNativeBuffer();
+    ANativeWindowBuffer* clientBuf = buffer->getNativeBuffer();
 
     // create the new EGLImageKHR
     const EGLint attrs[] = {
@@ -186,7 +186,7 @@
     if (texture->name == -1UL) {
         status_t err = initTexture(texture);
         LOGE_IF(err, "loadTexture failed in initTexture (%s)", strerror(err));
-        return err;
+        if (err != NO_ERROR) return err;
     }
 
     if (texture->target != Texture::TEXTURE_2D)
diff --git a/services/surfaceflinger/tests/surface/surface.cpp b/services/surfaceflinger/tests/surface/surface.cpp
index 67ecf7e..5265f91 100644
--- a/services/surfaceflinger/tests/surface/surface.cpp
+++ b/services/surfaceflinger/tests/surface/surface.cpp
@@ -53,7 +53,7 @@
     printf("window=%p\n", window);
 
     int err = native_window_set_buffer_count(window, 8);
-    android_native_buffer_t* buffer;
+    ANativeWindowBuffer* buffer;
 
     for (int i=0 ; i<8 ; i++) {
         window->dequeueBuffer(window, &buffer);