created an new EGL extension called ANDROID_swap_rectangle

ANDROID_swap_rectangle allows to specify the rectangle affected by eglSwapBuffers(), anything outside of this rectangle is unchanged. in particular EGL_BUFFER_DESTROYED only applies to that rectangle. This extension as well as EGL_BUFFER_PRESERVED allow major optimizations on surfaceflinger, which can redraw only the dirty area during compositing.

However, ANDROID_swap_rectangle allows further optimizations in EGL by reducing the amount of copy-back needed. ANDROID_swap_rectangle is particularily important for software implementations.
diff --git a/libs/surfaceflinger/DisplayHardware/DisplayHardware.cpp b/libs/surfaceflinger/DisplayHardware/DisplayHardware.cpp
index 374f2e2..fc29d73 100644
--- a/libs/surfaceflinger/DisplayHardware/DisplayHardware.cpp
+++ b/libs/surfaceflinger/DisplayHardware/DisplayHardware.cpp
@@ -195,17 +195,19 @@
      * Create our main surface
      */
 
-    
     surface = eglCreateWindowSurface(display, config, mNativeWindow.get(), NULL);
     checkEGLErrors("eglCreateDisplaySurfaceANDROID");
 
-    
     if (eglQuerySurface(display, surface, EGL_SWAP_BEHAVIOR, &dummy) == EGL_TRUE) {
         if (dummy == EGL_BUFFER_PRESERVED) {
             mFlags |= BUFFER_PRESERVED;
         }
     }
-    
+
+    if (strstr(egl_extensions, "ANDROID_swap_rectangle")) {
+        mFlags |= SWAP_RECTANGLE;
+    }
+
     mDpiX = mNativeWindow->xdpi;
     mDpiX = mNativeWindow->ydpi;
     mRefreshRate = mNativeWindow->getDevice()->fps; 
@@ -304,11 +306,12 @@
     EGLDisplay dpy = mDisplay;
     EGLSurface surface = mSurface;
 
-    if (mFlags & BUFFER_PRESERVED) {
+    if (mFlags & SWAP_RECTANGLE) {
         Region newDirty(dirty);
         newDirty.andSelf(Rect(mWidth, mHeight));
         const Rect& b(newDirty.bounds());
-        //mNativeWindow->setSwapRectangle(b);
+        eglSetSwapRectangleANDROID(dpy, surface,
+                b.left, b.top, b.width(), b.height());
     } 
 
     mPageFlipCount++;