commit 09ab75242e66edfe4c19460d0c08fa3696cbe327
Author: Leszek Koltunski <leszek@distoretedandroid.org>
Date:   Wed Feb 15 13:11:34 2017 +0000

    Introduce 3 types of Surfaces: System, Tree, User

diff --git a/src/main/java/org/distorted/library/DistortedEffects.java b/src/main/java/org/distorted/library/DistortedEffects.java
index 4a4bda5..3fd138f 100644
--- a/src/main/java/org/distorted/library/DistortedEffects.java
+++ b/src/main/java/org/distorted/library/DistortedEffects.java
@@ -77,7 +77,7 @@ public class DistortedEffects
   private static long mNextID =0;
   private long mID;
 
-  private static DistortedFramebuffer mBufferFBO = new DistortedFramebuffer(true,1,1);
+  private static DistortedFramebuffer mBufferFBO = new DistortedFramebuffer(true,DistortedSurface.TYPE_SYST,1,1);
 
   private EffectQueueMatrix      mM;
   private EffectQueueFragment    mF;
diff --git a/src/main/java/org/distorted/library/DistortedFramebuffer.java b/src/main/java/org/distorted/library/DistortedFramebuffer.java
index 3dad70a..6671a06 100644
--- a/src/main/java/org/distorted/library/DistortedFramebuffer.java
+++ b/src/main/java/org/distorted/library/DistortedFramebuffer.java
@@ -154,12 +154,14 @@ public class DistortedFramebuffer extends DistortedOutputSurface implements Dist
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-// create 'system' Framebuffers, i.e. those that are used internally by the library.
-// Those do not get removed in onDestroy();
+// create SYSTEM or TREE framebuffers (those are just like normal FBOs, just hold information
+// that they were autocreated only for the Library's internal purposes (SYSTEM) or for using
+// inside a Tree of DistortedNodes (TREE)
+// SYSTEM surfaces do not get removed in onDestroy().
 
-  DistortedFramebuffer(boolean depthEnabled, int width, int height)
+  DistortedFramebuffer(boolean depthEnabled, int type, int width, int height)
     {
-    super(width,height,NOT_CREATED_YET, (depthEnabled ? NOT_CREATED_YET:DONT_CREATE),NOT_CREATED_YET, true);
+    super(width,height,NOT_CREATED_YET, (depthEnabled ? NOT_CREATED_YET:DONT_CREATE),NOT_CREATED_YET, type);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -175,7 +177,7 @@ public class DistortedFramebuffer extends DistortedOutputSurface implements Dist
   @SuppressWarnings("unused")
   public DistortedFramebuffer(int width, int height, boolean depthEnabled)
     {
-    super(width,height,NOT_CREATED_YET,(depthEnabled ? NOT_CREATED_YET:DONT_CREATE),NOT_CREATED_YET,false);
+    super(width,height,NOT_CREATED_YET,(depthEnabled ? NOT_CREATED_YET:DONT_CREATE),NOT_CREATED_YET,TYPE_USER);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -189,7 +191,7 @@ public class DistortedFramebuffer extends DistortedOutputSurface implements Dist
   @SuppressWarnings("unused")
   public DistortedFramebuffer(int width, int height)
     {
-    super(width,height,NOT_CREATED_YET,DONT_CREATE,NOT_CREATED_YET,false);
+    super(width,height,NOT_CREATED_YET,DONT_CREATE,NOT_CREATED_YET,TYPE_USER);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/library/DistortedNode.java b/src/main/java/org/distorted/library/DistortedNode.java
index 7ddbb64..4ac65ab 100644
--- a/src/main/java/org/distorted/library/DistortedNode.java
+++ b/src/main/java/org/distorted/library/DistortedNode.java
@@ -169,7 +169,7 @@ public class DistortedNode implements DistortedAttacheable
         else
           {
           android.util.Log.d("NODE", "creating new FBO of node surfaceID="+mSurface.getID());
-          newData.mFBO = new DistortedFramebuffer(mSurface.getWidth(),mSurface.getHeight());
+          newData.mFBO = new DistortedFramebuffer(true,DistortedSurface.TYPE_TREE,mSurface.getWidth(),mSurface.getHeight());
           }
         }
       if( mNumChildren[0]==0 && newData.mFBO!=null )
@@ -303,12 +303,12 @@ public class DistortedNode implements DistortedAttacheable
 
       if( node.mSurface instanceof DistortedTexture )
         {
-        mSurface = new DistortedTexture(w,h);
+        mSurface = new DistortedTexture(w,h, DistortedSurface.TYPE_TREE);
         }
       else if( node.mSurface instanceof DistortedFramebuffer )
         {
         boolean hasDepth = ((DistortedFramebuffer) node.mSurface).hasDepth();
-        mSurface = new DistortedFramebuffer(w,h,hasDepth);
+        mSurface = new DistortedFramebuffer(hasDepth,DistortedSurface.TYPE_TREE,w,h);
         }
       }
     if( (flags & Distorted.CLONE_CHILDREN) != 0 )
diff --git a/src/main/java/org/distorted/library/DistortedOutputSurface.java b/src/main/java/org/distorted/library/DistortedOutputSurface.java
index 5e913d4..16a659a 100644
--- a/src/main/java/org/distorted/library/DistortedOutputSurface.java
+++ b/src/main/java/org/distorted/library/DistortedOutputSurface.java
@@ -43,9 +43,9 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  DistortedOutputSurface(int width, int height, int createColor, int createDepth, int fbo, boolean system)
+  DistortedOutputSurface(int width, int height, int createColor, int createDepth, int fbo, int type)
     {
-    super(width,height,createColor,system);
+    super(width,height,createColor,type);
 
     mProjectionMatrix = new float[16];
 
@@ -114,14 +114,9 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
     mRender++;
 
     // change tree topology (attach and detach children)
-    // create and delete all underlying OpenGL resources
-    // Watch out: FIRST change topology, only then deal
-    // with OpenGL resources. That's because changing Tree
-    // can result in additional Framebuffers that would need
-    // to be created immediately, before the calls to drawRecursive()
-
     boolean changed = DistortedAttachDaemon.toDo();
 
+    // if some changes have been made, we need to rebuilt our tree-isomorphism data structures.
     if( changed )
       {
       for(int i=0; i<mNumChildren; i++)
@@ -133,6 +128,11 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
       DistortedNode.debugMap();
       }
 
+    // create and delete all underlying OpenGL resources
+    // Watch out: FIRST change topology, only then deal
+    // with OpenGL resources. That's because changing Tree
+    // can result in additional Framebuffers that would need
+    // to be created immediately, before the calls to drawRecursive()
     toDo();
 
     if( changed )
diff --git a/src/main/java/org/distorted/library/DistortedScreen.java b/src/main/java/org/distorted/library/DistortedScreen.java
index 1d3d643..523e126 100644
--- a/src/main/java/org/distorted/library/DistortedScreen.java
+++ b/src/main/java/org/distorted/library/DistortedScreen.java
@@ -46,6 +46,6 @@ public class DistortedScreen extends DistortedOutputSurface
     {
     // set color to 'DONT_CREATE' so that Screens will not get added to the Surface lists
     // set depth to 'CREATED' so that depth will be, by default, on.
-    super(0,0,DONT_CREATE,CREATED,0,false);
+    super(0,0,DONT_CREATE,CREATED,0,TYPE_USER);
     }
   }
diff --git a/src/main/java/org/distorted/library/DistortedSurface.java b/src/main/java/org/distorted/library/DistortedSurface.java
index 2a5c268..62de8fe 100644
--- a/src/main/java/org/distorted/library/DistortedSurface.java
+++ b/src/main/java/org/distorted/library/DistortedSurface.java
@@ -36,6 +36,10 @@ abstract class DistortedSurface
   static final int DONT_CREATE      = 3;
   static final int CREATED          = 4;
 
+  static final int TYPE_USER = 0;
+  static final int TYPE_TREE = 1;
+  static final int TYPE_SYST = 2;
+
   private static boolean mToDo = false;
   private static LinkedList<DistortedSurface> mDoneList = new LinkedList<>();
   private static LinkedList<DistortedSurface> mToDoList = new LinkedList<>();
@@ -44,7 +48,7 @@ abstract class DistortedSurface
 
   private long mID;
   private boolean mMarked;
-  private boolean mSystem;
+  private int mType;
   int mColorCreated;
   int[] mColorH = new int[1];
   int mSizeX, mSizeY;  // in screen space
@@ -98,7 +102,7 @@ abstract class DistortedSurface
       {
       surface = mDoneList.removeFirst();
 
-      if( surface.mSystem )
+      if( surface.mType==TYPE_SYST )
         {
         mToDoList.add(surface);
         surface.recreate();
@@ -111,7 +115,7 @@ abstract class DistortedSurface
       {
       surface = mToDoList.get(i);
 
-      if( !surface.mSystem )
+      if( surface.mType!=TYPE_SYST )
         {
         mDoneList.remove(i);
         i--;
@@ -165,21 +169,29 @@ abstract class DistortedSurface
 
       str += ("("+surface.getWidth()+","+surface.getHeight()+") surfaceID:"+surface.getID());
 
+      switch(surface.mType)
+        {
+        case TYPE_SYST: str+=" SYSTEM"; break;
+        case TYPE_USER: str+=" USER"  ; break;
+        case TYPE_TREE: str+=" TREE"  ; break;
+        default       : str+=" ERROR??";
+        }
+
       android.util.Log.e("Surface", str);
       }
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  DistortedSurface(int width, int height, int create, boolean system)
+  DistortedSurface(int width, int height, int create, int type)
     {
     mSizeX        = width ;
     mSizeY        = height;
     mColorCreated = create;
     mColorH[0]    = 0;
     mMarked       = false;
-    mID           = system ? --mNextSystemID : ++mNextClientID;
-    mSystem       = system;
+    mID           = type==TYPE_SYST ? --mNextSystemID : ++mNextClientID;
+    mType         = type;
 
     if( create!=DONT_CREATE )
       {
diff --git a/src/main/java/org/distorted/library/DistortedTexture.java b/src/main/java/org/distorted/library/DistortedTexture.java
index 712acb1..6ea8572 100644
--- a/src/main/java/org/distorted/library/DistortedTexture.java
+++ b/src/main/java/org/distorted/library/DistortedTexture.java
@@ -111,6 +111,18 @@ public class DistortedTexture extends DistortedSurface implements DistortedInput
     GLES30.glUniform1i(textureH, 0);
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// create SYSTEM or TREE textures (those are just like normal Textures, just hold information
+// that they were autocreated only for the Library's internal purposes (SYSTEM) or for using
+// inside a Tree of DistortedNodes (TREE)
+// SYSTEM surfaces do not get removed in onDestroy().
+
+  public DistortedTexture(int width, int height, int type)
+    {
+    super(width,height,NOT_CREATED_YET,type);
+    mBmp= null;
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // PUBLIC API
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -119,7 +131,7 @@ public class DistortedTexture extends DistortedSurface implements DistortedInput
  */
   public DistortedTexture(int width, int height)
     {
-    super(width,height,NOT_CREATED_YET,false);
+    super(width,height,NOT_CREATED_YET,TYPE_USER);
     mBmp= null;
     }
 
diff --git a/src/main/java/org/distorted/library/EffectQueuePostprocess.java b/src/main/java/org/distorted/library/EffectQueuePostprocess.java
index 5086af8..683e09a 100644
--- a/src/main/java/org/distorted/library/EffectQueuePostprocess.java
+++ b/src/main/java/org/distorted/library/EffectQueuePostprocess.java
@@ -69,7 +69,7 @@ class EffectQueuePostprocess extends EffectQueue
     mQuadTextureInv.put(textureInv).position(0);
     }
 
-  private static DistortedFramebuffer mBufferFBO = new DistortedFramebuffer(true,1,1);
+  private static DistortedFramebuffer mBufferFBO = new DistortedFramebuffer(true,DistortedSurface.TYPE_SYST,1,1);
 
   private static float[] mMVPMatrix = new float[16];
   private static float[] mTmpMatrix = new float[16];
