EGL: Disconnect native window in eglDestroySurface

Bug 27455025

Change-Id: I7549a3acb724e749925a8249feb180088aec7f3c
diff --git a/opengl/libs/EGL/egl_object.cpp b/opengl/libs/EGL/egl_object.cpp
index 918faa8..90f27d1 100644
--- a/opengl/libs/EGL/egl_object.cpp
+++ b/opengl/libs/EGL/egl_object.cpp
@@ -67,7 +67,8 @@
 egl_surface_t::egl_surface_t(egl_display_t* dpy, EGLConfig config,
         EGLNativeWindowType win, EGLSurface surface,
         egl_connection_t const* cnx) :
-    egl_object_t(dpy), surface(surface), config(config), win(win), cnx(cnx)
+    egl_object_t(dpy), surface(surface), config(config), win(win), cnx(cnx),
+    connected(true)
 {
     if (win) {
         getDisplay()->onWindowSurfaceCreated();
@@ -77,14 +78,27 @@
 egl_surface_t::~egl_surface_t() {
     ANativeWindow* const window = win.get();
     if (window != NULL) {
+        disconnect();
+        getDisplay()->onWindowSurfaceDestroyed();
+    }
+}
+
+void egl_surface_t::disconnect() {
+    ANativeWindow* const window = win.get();
+    if (window != NULL && connected) {
         native_window_set_buffers_format(window, 0);
         if (native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL)) {
             ALOGW("EGLNativeWindowType %p disconnect failed", window);
         }
-        getDisplay()->onWindowSurfaceDestroyed();
+        connected = false;
     }
 }
 
+void egl_surface_t::terminate() {
+    disconnect();
+    egl_object_t::terminate();
+}
+
 // ----------------------------------------------------------------------------
 
 egl_context_t::egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config,