eglTerminate() now actually frees up all active egl objects

as specified by the EGL specification, terminated objects's
handles become invalid, the objects themselves are destroyed
when they're not current to some thread.

Change-Id: Id3a4a5736a5bbc3926a9ae8385d43772edb88eeb
diff --git a/opengl/libs/EGL/egl_display.cpp b/opengl/libs/EGL/egl_display.cpp
index 272fa44..83aafa6 100644
--- a/opengl/libs/EGL/egl_display.cpp
+++ b/opengl/libs/EGL/egl_display.cpp
@@ -55,6 +55,11 @@
     objects.add(object);
 }
 
+void egl_display_t::removeObject(egl_object_t* object) {
+    Mutex::Autolock _l(lock);
+    objects.remove(object);
+}
+
 bool egl_display_t::getObject(egl_object_t* object) {
     Mutex::Autolock _l(lock);
     if (objects.indexOf(object) >= 0) {
@@ -64,15 +69,6 @@
     return false;
 }
 
-bool egl_display_t::removeObject(egl_object_t* object) {
-    Mutex::Autolock _l(lock);
-    if (object->decRef() == 1) {
-        objects.remove(object);
-        return true;
-    }
-    return false;
-}
-
 EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
     if (uintptr_t(disp) >= NUM_DISPLAYS)
         return NULL;
@@ -255,7 +251,18 @@
         }
     }
 
-    // TODO: all egl_object_t should be marked for termination
+    // Mark all objects remaining in the list as terminated, unless
+    // there are no reference to them, it which case, we're free to
+    // delete them.
+    size_t count = objects.size();
+    LOGW_IF(count, "eglTerminate() called w/ %d objects remaining", count);
+    for (size_t i=0 ; i<count ; i++) {
+        egl_object_t* o = objects.itemAt(i);
+        o->destroy();
+    }
+
+    // this marks all object handles are "terminated"
+    objects.clear();
 
     refs--;
     numTotalConfigs = 0;