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_object.cpp b/opengl/libs/EGL/egl_object.cpp
index d4df341..dbf9a01 100644
--- a/opengl/libs/EGL/egl_object.cpp
+++ b/opengl/libs/EGL/egl_object.cpp
@@ -32,16 +32,33 @@
 // ----------------------------------------------------------------------------
 
 egl_object_t::egl_object_t(egl_display_t* disp) :
-    display(disp), terminated(0), count(1) {
+    display(disp), count(1) {
+    // NOTE: this does an implicit incRef
     display->addObject(this);
 }
 
-bool egl_object_t::get() {
-    return display->getObject(this);
+egl_object_t::~egl_object_t() {
 }
 
-bool egl_object_t::put() {
-    return display->removeObject(this);
+void egl_object_t::terminate() {
+    // this marks the object as "terminated"
+    display->removeObject(this);
+    if (decRef() == 1) {
+        // shouldn't happen because this is called from LocalRef
+        LOGE("egl_object_t::terminate() removed the last reference!");
+    }
+}
+
+void egl_object_t::destroy() {
+    if (decRef() == 1) {
+        delete this;
+    }
+}
+
+bool egl_object_t::get() {
+    // used by LocalRef, this does an incRef() atomically with
+    // checking that the object is valid.
+    return display->getObject(this);
 }
 
 // ----------------------------------------------------------------------------