commit 2e49718dfca8ddeb7d84a2a60f664725226e42ae
Author: Leszek Koltunski <leszek@distoretedandroid.org>
Date:   Wed Feb 8 16:09:06 2017 +0000

    progress with DistortedRenderable.
    
    This introduces a regression with MultiBlur!

diff --git a/src/main/java/org/distorted/library/Distorted.java b/src/main/java/org/distorted/library/Distorted.java
index 0b602a1..3b31a2c 100644
--- a/src/main/java/org/distorted/library/Distorted.java
+++ b/src/main/java/org/distorted/library/Distorted.java
@@ -136,8 +136,7 @@ public class Distorted
  */
   public static void onDestroy()
     {
-    DistortedTexture.onDestroy();
-    DistortedFramebuffer.onDestroy();
+    DistortedRenderable.onDestroy();
     DistortedTree.onDestroy();
     EffectQueue.onDestroy();
     DistortedEffects.onDestroy();
diff --git a/src/main/java/org/distorted/library/DistortedFramebuffer.java b/src/main/java/org/distorted/library/DistortedFramebuffer.java
index 071a9c6..92621e5 100644
--- a/src/main/java/org/distorted/library/DistortedFramebuffer.java
+++ b/src/main/java/org/distorted/library/DistortedFramebuffer.java
@@ -22,9 +22,6 @@ package org.distorted.library;
 import android.opengl.GLES30;
 import android.opengl.Matrix;
 
-import java.util.Iterator;
-import java.util.LinkedList;
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * Class which represents a OpenGL Framebuffer object.
@@ -41,13 +38,9 @@ import java.util.LinkedList;
  */
 public class DistortedFramebuffer extends DistortedRenderable
   {
-  private static boolean mListMarked = false;
-  private static LinkedList<DistortedFramebuffer> mList = new LinkedList<>();
-
   private int[] mDepthH = new int[1];
   private int[] mFBOH   = new int[1];
 
-  private boolean mMarked;
   private boolean mDepthEnabled;
 
   // Projection stuff
@@ -142,8 +135,6 @@ public class DistortedFramebuffer extends DistortedRenderable
       GLES30.glDeleteFramebuffers(1, mFBOH, 0);
       mFBOH[0] = 0;
       }
-
-    mMarked = false;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -199,42 +190,6 @@ public class DistortedFramebuffer extends DistortedRenderable
       }
     }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  static synchronized void onDestroy()
-    {
-    for( DistortedFramebuffer fbo : mList)
-      {
-      if( fbo.mColorH[0]!=DONT_CREATE ) fbo.mColorH[0] = NOT_CREATED_YET;
-      if( fbo.mDepthEnabled           ) fbo.mDepthH[0] = NOT_CREATED_YET;
-      }
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-// must be called form a thread holding OpenGL Context
-
-  private static synchronized void deleteAllMarked()
-    {
-    if( mListMarked )
-      {
-      DistortedFramebuffer tmp;
-      Iterator<DistortedFramebuffer> iterator = mList.iterator();
-
-      while(iterator.hasNext())
-        {
-        tmp = iterator.next();
-
-        if( tmp.mMarked )
-          {
-          tmp.delete();
-          iterator.remove();
-          }
-        }
-
-      mListMarked = false;
-      }
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // if new size fits into the size of the underlying Texture, just change the projection without
 // reallocating the Texture. Otherwise, we need to reallocate.
@@ -274,25 +229,20 @@ public class DistortedFramebuffer extends DistortedRenderable
   @SuppressWarnings("unused")
   public DistortedFramebuffer(int width, int height, boolean depthEnabled)
     {
+    super(width,height,NOT_CREATED_YET);
+
     mProjectionMatrix = new float[16];
 
     mHeight      = height;
     mWidth       = width;
-    mSizeY       = height;
-    mSizeX       = width;
     mFOV         = 60.0f;
     mX           = 0.0f;
     mY           = 0.0f;
-    mMarked      = false;
     mDepthEnabled= depthEnabled;
-
-    mFBOH[0]  =-1;
-    mColorH[0]= NOT_CREATED_YET;
-    mDepthH[0]= NOT_CREATED_YET;
+    mFBOH[0]     = NOT_CREATED_YET;
+    mDepthH[0]   = NOT_CREATED_YET;
 
     createProjection();
-
-    mList.add(this);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -306,25 +256,20 @@ public class DistortedFramebuffer extends DistortedRenderable
   @SuppressWarnings("unused")
   public DistortedFramebuffer(int width, int height)
     {
+    super(width,height,NOT_CREATED_YET);
+
     mProjectionMatrix = new float[16];
 
     mHeight      = height;
     mWidth       = width;
-    mSizeY       = height;
-    mSizeX       = width;
     mFOV         = 60.0f;
     mX           = 0.0f;
     mY           = 0.0f;
-    mMarked      = false;
     mDepthEnabled= false;
-
-    mFBOH[0]  =-1;
-    mColorH[0]= NOT_CREATED_YET;
-    mDepthH[0]= NOT_CREATED_YET;
+    mFBOH[0]     = NOT_CREATED_YET;
+    mDepthH[0]   = NOT_CREATED_YET;
 
     createProjection();
-
-    mList.add(this);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -337,17 +282,16 @@ public class DistortedFramebuffer extends DistortedRenderable
  */
   public DistortedFramebuffer(int fbo)
     {
+    super(0,0,DONT_CREATE);
+
     mProjectionMatrix = new float[16];
 
     mFOV         = 60.0f;
     mX           = 0.0f;
     mY           = 0.0f;
-    mMarked      = false;
     mDepthEnabled= true;
-
-    mFBOH[0]  = fbo;
-    mColorH[0]= DONT_CREATE;
-    mDepthH[0]= DONT_CREATE;
+    mFBOH[0]     = fbo;
+    mDepthH[0]   = DONT_CREATE;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -381,8 +325,7 @@ public class DistortedFramebuffer extends DistortedRenderable
 
     if( ren.setAsInput() )
       {
-      DistortedFramebuffer.deleteAllMarked();
-      DistortedTexture.deleteAllMarked();
+      DistortedRenderable.deleteAllMarked();
       effects.drawPriv(ren.getWidth()/2.0f, ren.getHeight()/2.0f, mesh, this, time);
       }
     }
@@ -398,22 +341,11 @@ public class DistortedFramebuffer extends DistortedRenderable
  */
   public void renderTo(DistortedTree dt, long time)
     {
-    DistortedFramebuffer.deleteAllMarked();
-    DistortedTexture.deleteAllMarked();
+    DistortedRenderable.deleteAllMarked();
     create();
     dt.drawRecursive(time,this);
     }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-/**
- * Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
- */
-  public void markForDeletion()
-    {
-    mListMarked = true;
-    mMarked     = true;
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * Create new Projection matrix.
diff --git a/src/main/java/org/distorted/library/DistortedRenderable.java b/src/main/java/org/distorted/library/DistortedRenderable.java
index d5ad7be..5b32ff1 100644
--- a/src/main/java/org/distorted/library/DistortedRenderable.java
+++ b/src/main/java/org/distorted/library/DistortedRenderable.java
@@ -21,6 +21,9 @@ package org.distorted.library;
 
 import android.opengl.GLES30;
 
+import java.util.Iterator;
+import java.util.LinkedList;
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 abstract class DistortedRenderable
@@ -29,6 +32,10 @@ abstract class DistortedRenderable
   static final int NOT_CREATED_YET  = -2;
   static final int DONT_CREATE      = -3;
 
+  private static boolean mListMarked = false;
+  private static LinkedList<DistortedRenderable> mList = new LinkedList<>();
+
+  private boolean mMarked;
   int[] mColorH = new int[1];
   int mSizeX, mSizeY;  // in screen space
 
@@ -37,6 +44,32 @@ abstract class DistortedRenderable
   abstract void create();
   abstract void delete();
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// must be called form a thread holding OpenGL Context
+
+  static synchronized void deleteAllMarked()
+    {
+    if( mListMarked )
+      {
+      DistortedRenderable tmp;
+      Iterator<DistortedRenderable> iterator = mList.iterator();
+
+      while(iterator.hasNext())
+        {
+        tmp = iterator.next();
+
+        if( tmp.mMarked )
+          {
+          tmp.delete();
+          tmp.mMarked = false;
+          iterator.remove();
+          }
+        }
+
+      mListMarked = false;
+      }
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   long getID()
@@ -57,9 +90,45 @@ abstract class DistortedRenderable
     return false;
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  static synchronized void onDestroy()
+    {
+    for( DistortedRenderable ren : mList)
+      {
+      ren.delete();
+      ren.mMarked = false;
+      }
+
+    mListMarked = false;
+    mList.clear();
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  DistortedRenderable(int width, int height, int color)
+    {
+    mSizeX    = width ;
+    mSizeY    = height;
+    mColorH[0]= color;
+    mMarked   = false;
+    mList.add(this);
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // PUBLIC API
 ///////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
+ */
+  public void markForDeletion()
+    {
+    mListMarked = true;
+    mMarked     = true;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
 /**
  * Returns the height of the Renderable.
  *
diff --git a/src/main/java/org/distorted/library/DistortedTexture.java b/src/main/java/org/distorted/library/DistortedTexture.java
index 869a194..1bf3fdf 100644
--- a/src/main/java/org/distorted/library/DistortedTexture.java
+++ b/src/main/java/org/distorted/library/DistortedTexture.java
@@ -24,9 +24,6 @@ import android.graphics.Matrix;
 import android.opengl.GLES30;
 import android.opengl.GLUtils;
 
-import java.util.Iterator;
-import java.util.LinkedList;
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * Class which represents a OpenGL Texture object.
@@ -41,12 +38,6 @@ import java.util.LinkedList;
  */
 public class DistortedTexture extends DistortedRenderable
   {
-  private static boolean mListMarked = false;
-  private static LinkedList<DistortedTexture> mList = new LinkedList<>();
-
-  private static int mTextureH;
-
-  private boolean mMarked;
   private Bitmap mBmp= null;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -74,7 +65,7 @@ public class DistortedTexture extends DistortedRenderable
     {
     if( mBmp!=null && mColorH !=null )
       {
-      if( mColorH[0]==0 )
+      if( mColorH[0]==NOT_CREATED_YET )
         {
         GLES30.glGenTextures(1, mColorH, 0);
         GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
@@ -99,56 +90,21 @@ public class DistortedTexture extends DistortedRenderable
 
   void delete()
     {
-    if( mColorH !=null && mColorH[0]>0 )
+    if( mColorH !=null && mColorH[0]>=0 )
       {
       GLES30.glDeleteTextures(1, mColorH, 0);
-      mColorH[0] = 0;
+      mColorH[0] = NOT_CREATED_YET;
       }
-
-    mMarked = false;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   static void getUniforms(int mProgramH)
     {
-    mTextureH= GLES30.glGetUniformLocation( mProgramH, "u_Texture");
+    int textureH= GLES30.glGetUniformLocation( mProgramH, "u_Texture");
 
     GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
-    GLES30.glUniform1i(mTextureH, 0);
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  static synchronized void onDestroy()
-    {
-    mListMarked = false;
-    mList.clear();
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-// must be called form a thread holding OpenGL Context
-
-  static synchronized void deleteAllMarked()
-    {
-    if( mListMarked )
-      {
-      DistortedTexture tmp;
-      Iterator<DistortedTexture> iterator = mList.iterator();
-
-      while(iterator.hasNext())
-        {
-        tmp = iterator.next();
-
-        if( tmp.mMarked )
-          {
-          tmp.delete();
-          iterator.remove();
-          }
-        }
-
-      mListMarked = false;
-      }
+    GLES30.glUniform1i(textureH, 0);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -159,23 +115,8 @@ public class DistortedTexture extends DistortedRenderable
  */
   public DistortedTexture(int width, int height)
     {
-    mSizeX    = width ;
-    mSizeY    = height;
-    mColorH[0]= 0;
-    mBmp      = null;
-    mMarked   = false;
-
-    mList.add(this);
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-/**
- * Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
- */
-  public void markForDeletion()
-    {
-    mListMarked = true;
-    mMarked     = true;
+    super(width,height,NOT_CREATED_YET);
+    mBmp= null;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -187,7 +128,6 @@ public class DistortedTexture extends DistortedRenderable
  *
  * @param bmp The android.graphics.Bitmap object to apply effects to and display.
  */
-
   public void setTexture(Bitmap bmp)
     {
     mBmp= bmp;
