First prototype atttempting to support an external display

both API and implementation will change, this is just a prototype
intended to show feasability.

SurfaceFlinger is passed an ISurfaceTexture through a new
callback, it is in turn used to create an EGLSurface which
surfaceflinger will draw into in addition to the main screen.

Change-Id: Id0bbb0b854bb7bae44d57246a90b65d4567f9a21
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 981d694..07ea3a0 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -64,6 +64,7 @@
 #include <private/android_filesystem_config.h>
 #include <private/gui/SharedBufferStack.h>
 #include <gui/BitTube.h>
+#include <gui/SurfaceTextureClient.h>
 
 #define EGL_VERSION_HW_ANDROID  0x3143
 
@@ -97,7 +98,8 @@
         mDebugInTransaction(0),
         mLastTransactionTime(0),
         mBootFinished(false),
-        mSecureFrameBuffer(0)
+        mSecureFrameBuffer(0),
+        mExternalDisplaySurface(EGL_NO_SURFACE)
 {
     init();
 }
@@ -370,6 +372,41 @@
     return mEventThread->createEventConnection();
 }
 
+void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> display) {
+    const DisplayHardware& hw(graphicPlane(0).displayHardware());
+    EGLSurface result = EGL_NO_SURFACE;
+    EGLSurface old_surface = EGL_NO_SURFACE;
+    sp<SurfaceTextureClient> stc;
+
+    if (display != NULL) {
+        stc = new SurfaceTextureClient(display);
+        result = eglCreateWindowSurface(hw.getEGLDisplay(),
+                hw.getEGLConfig(), (EGLNativeWindowType)stc.get(), NULL);
+        ALOGE_IF(result == EGL_NO_SURFACE,
+                "eglCreateWindowSurface failed (ISurfaceTexture=%p)",
+                display.get());
+    }
+
+    { // scope for the lock
+        Mutex::Autolock _l(mStateLock);
+        old_surface = mExternalDisplaySurface;
+        mExternalDisplayNativeWindow = stc;
+        mExternalDisplaySurface = result;
+        ALOGD("mExternalDisplaySurface = %p", result);
+    }
+
+    if (old_surface != EGL_NO_SURFACE) {
+        // Note: EGL allows to destroy an object while its current
+        // it will fail to become current next time though.
+        eglDestroySurface(hw.getEGLDisplay(), old_surface);
+    }
+}
+
+EGLSurface SurfaceFlinger::getExternalDisplaySurface() const {
+    Mutex::Autolock _l(mStateLock);
+    return mExternalDisplaySurface;
+}
+
 // ----------------------------------------------------------------------------
 
 void SurfaceFlinger::waitForEvent() {
@@ -454,6 +491,43 @@
                 hw.compositionComplete();
             }
 
+            // render to the external display if we have one
+            EGLSurface externalDisplaySurface = getExternalDisplaySurface();
+            if (externalDisplaySurface != EGL_NO_SURFACE) {
+                EGLSurface cur = eglGetCurrentSurface(EGL_DRAW);
+                EGLBoolean success = eglMakeCurrent(eglGetCurrentDisplay(),
+                        externalDisplaySurface, externalDisplaySurface,
+                        eglGetCurrentContext());
+
+                ALOGE_IF(!success, "eglMakeCurrent -> external failed");
+
+                if (success) {
+                    // redraw the screen entirely...
+                    glDisable(GL_TEXTURE_EXTERNAL_OES);
+                    glDisable(GL_TEXTURE_2D);
+                    glClearColor(0,0,0,1);
+                    glClear(GL_COLOR_BUFFER_BIT);
+                    glMatrixMode(GL_MODELVIEW);
+                    glLoadIdentity();
+                    const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
+                    const size_t count = layers.size();
+                    for (size_t i=0 ; i<count ; ++i) {
+                        const sp<LayerBase>& layer(layers[i]);
+                        layer->drawForSreenShot();
+                    }
+
+                    success = eglSwapBuffers(eglGetCurrentDisplay(), externalDisplaySurface);
+                    ALOGE_IF(!success, "external display eglSwapBuffers failed");
+
+                    hw.compositionComplete();
+                }
+
+                success = eglMakeCurrent(eglGetCurrentDisplay(),
+                        cur, cur, eglGetCurrentContext());
+
+                ALOGE_IF(!success, "eglMakeCurrent -> internal failed");
+            }
+
         } break;
     }
 }