commit 05ecc6fef6731d57d8656b1148116b09fa91ae1c
Author: Leszek Koltunski <leszek@distoretedandroid.org>
Date:   Tue Apr 18 15:30:06 2017 +0100

    Major:
    
    1) in the Library, fix the fact that some applications (those that were creating their DistortedSurface objects outside of onSurfaceCreated or onSurfaceChanged) would not render after the activity went to background (press POWER to see that).
    2) in the Apps, call the new 'Distorted.onPause()' API to fix the above problem
    
    The above fixes the problem, but it still leaks memory if an App creates its Surface in onSurfaceCreated/Changed (precisely: it leaves the old Surface in the DistortedSurface Map). Thus
    
    3) Fix the first 15 Apps to avoid the memory leak. Next Apps coming.

diff --git a/src/main/java/org/distorted/library/Distorted.java b/src/main/java/org/distorted/library/Distorted.java
index 905c2eb..56691d1 100644
--- a/src/main/java/org/distorted/library/Distorted.java
+++ b/src/main/java/org/distorted/library/Distorted.java
@@ -123,6 +123,16 @@ public class Distorted
     mInitialized = true;
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * Call this so that the Library can release its internal data structures.
+ * Must be called from Activity.onPause().
+ */
+  public static void onPause()
+    {
+    DistortedSurface.onPause();
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * Call this so that the Library can release its internal data structures.
diff --git a/src/main/java/org/distorted/library/DistortedFramebuffer.java b/src/main/java/org/distorted/library/DistortedFramebuffer.java
index f4c63f8..a2e2317 100644
--- a/src/main/java/org/distorted/library/DistortedFramebuffer.java
+++ b/src/main/java/org/distorted/library/DistortedFramebuffer.java
@@ -124,8 +124,16 @@ public class DistortedFramebuffer extends DistortedOutputSurface implements Dist
 
   void recreate()
     {
-    if( mColorCreated!=DONT_CREATE ) mColorCreated = NOT_CREATED_YET;
-    if( mDepthCreated!=DONT_CREATE ) mDepthCreated = NOT_CREATED_YET;
+    if( mColorCreated!=DONT_CREATE )
+      {
+      mColorCreated = NOT_CREATED_YET;
+      mColorH[0] = 0;
+      }
+    if( mDepthCreated!=DONT_CREATE )
+      {
+      mDepthCreated = NOT_CREATED_YET;
+      mDepthH[0] = 0;
+      }
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/library/DistortedSurface.java b/src/main/java/org/distorted/library/DistortedSurface.java
index 26fc715..9e5759a 100644
--- a/src/main/java/org/distorted/library/DistortedSurface.java
+++ b/src/main/java/org/distorted/library/DistortedSurface.java
@@ -20,7 +20,9 @@
 package org.distorted.library;
 
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.LinkedList;
+import java.util.Map;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
@@ -75,7 +77,7 @@ abstract class DistortedSurface
   abstract void recreate();
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-// must be called form a thread holding OpenGL Context
+// must be called from a thread holding OpenGL Context
 
   static synchronized void toDo()
     {
@@ -109,36 +111,37 @@ abstract class DistortedSurface
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  static synchronized void onDestroy()
+  static synchronized void onPause()
     {
-    Job job;
     DistortedSurface surface;
+    int num = mDoneList.size();
 
-    for(Long key: mToDoMap.keySet())
+    for(int i=0; i<num; i++)
       {
-      job = mToDoMap.get(key);
-
-      if( job.surface.mType==TYPE_SYST )
-        {
-        mDoneList.add(job.surface);
-        }
+      surface = mDoneList.removeFirst();
+      mToDoMap.put(surface.getID(), surface.new Job(surface,JOB_CREATE) );
+      surface.recreate();
       }
 
-    mToDoMap.clear();
+    mToDo = true;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    int num = mDoneList.size();
+  static synchronized void onDestroy()
+    {
+    Job job;
+    Iterator<Map.Entry<Long,Job>> it = mToDoMap.entrySet().iterator();
 
-    for(int i=0; i<num; i++)
+    while (it.hasNext())
       {
-      surface = mDoneList.removeFirst();
-
-      if( surface.mType==TYPE_SYST )
-        {
-        mToDoMap.put(surface.getID(), surface.new Job(surface,JOB_CREATE) );
-        surface.recreate();
-        }
+      Map.Entry<Long,Job> entry = it.next();
+      job = entry.getValue();
+      if (job.surface.mType!=TYPE_SYST) it.remove();
       }
 
+    mDoneList.clear();
+
     mToDo = true;
     mNextClientID = 0;
     }
diff --git a/src/main/java/org/distorted/library/DistortedTexture.java b/src/main/java/org/distorted/library/DistortedTexture.java
index a777642..5a28960 100644
--- a/src/main/java/org/distorted/library/DistortedTexture.java
+++ b/src/main/java/org/distorted/library/DistortedTexture.java
@@ -85,7 +85,7 @@ public class DistortedTexture extends DistortedSurface implements DistortedInput
 
   void delete()
     {
-    if( mColorH[0]>=0 )
+    if( mColorH[0]>0 )
       {
       GLES30.glDeleteTextures(1, mColorH, 0);
       mColorH[0] = 0;
@@ -98,7 +98,11 @@ public class DistortedTexture extends DistortedSurface implements DistortedInput
 
   void recreate()
     {
-    if( mColorCreated!=DONT_CREATE ) mColorCreated = NOT_CREATED_YET;
+    if( mColorCreated!=DONT_CREATE )
+      {
+      mColorCreated = NOT_CREATED_YET;
+      mColorH[0] = 0;
+      }
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
