Project

General

Profile

Download (15 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / DistortedObjectTree.java @ 7b8086eb

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.library;
21

    
22
import java.util.ArrayList;
23
import java.util.HashMap;
24

    
25
import android.opengl.GLES20;
26

    
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28
/**
29
 * Class which represents a Node in a Tree of (Texture,EffectQueue,Grid) set.
30
 *  
31
 * Having organized such sets into a Tree, we can then render any Node to any Framebuffer.
32
 * That recursively renders the set held in the Node and all its children.
33
 */
34
public class DistortedObjectTree
35
  {
36
  private static HashMap<ArrayList<Long>,NodeData> mMapNodeID = new HashMap<>();
37
  private static long mNextNodeID =0;
38

    
39
  private GridObject mGrid;
40
  private DistortedEffectQueues mQueues;
41
  private DistortedTexture mTexture;
42
  private NodeData mData;
43

    
44
  private DistortedObjectTree mParent;
45
  private ArrayList<DistortedObjectTree> mChildren;
46
  private int[] mNumChildren;  // ==mChildren.length(), but we only create mChildren if the first one gets added
47

    
48
  private class NodeData
49
    {
50
    long ID;
51
    int numPointingNodes;
52
    DistortedFramebuffer mDF;
53
    int numRendered;
54

    
55
    NodeData(long id)
56
      {
57
      ID              = id;
58
      numPointingNodes= 1;
59
      mDF             = null;
60
      numRendered     = 0;
61
      }
62
    }
63
 
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
  static synchronized void onDestroy()
67
    {
68
    mNextNodeID = 0;
69
    mMapNodeID.clear();
70
    }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
  
74
  private void drawRecursive(long currTime, DistortedFramebuffer df)
75
    {
76
    mTexture.createTexture();
77

    
78
    if( mNumChildren[0]<=0 )
79
      {
80
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTexture.mTextureDataH[0]);
81
      }
82
    else
83
      {
84
      if( mData.numRendered==0 )
85
        {
86
        mData.mDF.setAsOutput();
87

    
88
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
89
        GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
90
      
91
        if( mTexture.mBitmapSet[0] )
92
          {
93
          GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTexture.mTextureDataH[0]);
94
          mQueues.drawNoEffectsPriv(mTexture, mGrid, mData.mDF);
95
          }
96
      
97
        synchronized(this)
98
          {
99
          for(int i=0; i<mNumChildren[0]; i++)
100
            {
101
            mChildren.get(i).drawRecursive(currTime, mData.mDF);
102
            }
103
          }
104
        }
105

    
106
      mData.numRendered++;
107
      mData.numRendered %= mData.numPointingNodes;
108

    
109
      df.setAsOutput();
110
      mData.mDF.setAsInput();
111
      }
112
    
113
    mQueues.drawPriv(currTime, mTexture, mGrid, df);
114
    }
115
  
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
// tree isomorphism
118
  
119
  private void RecomputeNodeID(ArrayList<Long> prev)
120
    {
121
    ArrayList<Long> curr = generateIDList();
122
     
123
    if( mParent==null )
124
      {
125
      adjustNodeData(prev,curr);
126
      }
127
    else
128
      {
129
      ArrayList<Long> parentPrev = mParent.generateIDList();
130
      adjustNodeData(prev,curr);
131
      mParent.RecomputeNodeID(parentPrev);
132
      }
133
    }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

    
137
  private ArrayList<Long> generateIDList()
138
    {
139
    ArrayList<Long> ret = new ArrayList<>();
140
     
141
    ret.add( mTexture.getID() );
142
    DistortedObjectTree node;
143
   
144
    for(int i=0; i<mNumChildren[0]; i++)
145
      {
146
      node = mChildren.get(i);
147
      ret.add(node.mData.ID);
148
      }
149
   
150
    return ret;
151
    }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
  private void adjustNodeData(ArrayList<Long> oldList, ArrayList<Long> newList)
156
    {
157
    boolean otherNodesPoint = (mData.numPointingNodes>1);
158

    
159
    if( otherNodesPoint ) mData.numPointingNodes--;
160
    else                  mMapNodeID.remove(oldList);
161

    
162
    NodeData newData = mMapNodeID.get(newList);
163
    
164
    if( newData==null )
165
      {
166
      if( otherNodesPoint )  mData = new NodeData(++mNextNodeID);
167
      else                   mData.ID = ++mNextNodeID;  // numPointingNodes must be 1 already
168

    
169
      if( newList.size()>1 )
170
        {
171
        if( mData.mDF==null )
172
          mData.mDF = new DistortedFramebuffer(mTexture.getWidth(), mTexture.getHeight());
173
        }
174
      else
175
        {
176
        if( mData.mDF!=null )
177
          {
178
          mData.mDF.markForDeletion();
179
          mData.mDF = null;
180
          }
181
        else
182
          {
183
          android.util.Log.e("DistortedObjectTree", "adjustNodeData: impossible situation??");
184
          }
185
        }
186

    
187
      mMapNodeID.put(newList, mData);
188
      }
189
    else
190
      {
191
      newData.numPointingNodes++;
192
      mData = newData;
193
      }
194
    }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////  
197
// this will be called on startup and every time OpenGL context has been lost
198

    
199
  static void reset()
200
    {
201
    NodeData tmp;   
202
     
203
    for(ArrayList<Long> key: mMapNodeID.keySet())
204
      {
205
      tmp = mMapNodeID.get(key);
206
          
207
      if( tmp.mDF != null )
208
        {
209
    	  tmp.mDF.reset();
210
        tmp.numRendered = 0;
211
        }
212
      }
213
    }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216
// Debug - print all the Node IDs
217

    
218
  void debug(int depth)
219
    {
220
    String tmp="";
221
    int i;
222

    
223
    for(i=0; i<depth; i++) tmp +="   ";
224
    tmp += (mData.ID+" (nodes: "+mData.numPointingNodes+")");
225

    
226
    android.util.Log.e("NODE", tmp);
227

    
228
    for(i=0; i<mNumChildren[0]; i++)
229
      mChildren.get(i).debug(depth+1);
230
    }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233
// Debug - print contents of the HashMap
234

    
235
  static void debugMap()
236
    {
237
    NodeData tmp;
238

    
239
    for(ArrayList<Long> key: mMapNodeID.keySet())
240
      {
241
      tmp = mMapNodeID.get(key);
242

    
243
      android.util.Log.e("NODE", "key="+key+" NodeID: "+tmp.ID);
244
      }
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248
// PUBLIC API
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250
/**
251
 * Constructs new Node of the Tree.
252
 *     
253
 * @param texture DistortedTexture to put into the new Node.
254
 * @param queues  DistortedEffectQueues to put into the new Node.
255
 * @param grid GridObject to put into the new Node.
256
 */
257
  public DistortedObjectTree(DistortedTexture texture, DistortedEffectQueues queues, GridObject grid)
258
    {
259
    mTexture= texture;
260
    mQueues = queues;
261
    mGrid   = grid;
262
    mParent = null;
263
    mChildren = null;
264
    mNumChildren = new int[1];
265
    mNumChildren[0] = 0;
266
   
267
    ArrayList<Long> list = new ArrayList<>();
268
    list.add(mTexture.getID());
269

    
270
    mData = mMapNodeID.get(list);
271
   
272
    if( mData!=null )
273
      {
274
      mData.numPointingNodes++;
275
      }
276
    else
277
      {
278
      mData = new NodeData(++mNextNodeID);   
279
      mMapNodeID.put(list, mData);
280
      }
281
    }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////  
284
/**
285
 * Copy-constructs new Node of the Tree from another Node.
286
 *     
287
 * @param node The DistortedObjectTree to copy data from.
288
 * @param flags bit field composed of a subset of the following:
289
 *        {@link Distorted#CLONE_BITMAP},  {@link Distorted#CLONE_MATRIX}, {@link Distorted#CLONE_VERTEX},
290
 *        {@link Distorted#CLONE_FRAGMENT} and {@link Distorted#CLONE_CHILDREN}.
291
 *        For example flags = CLONE_BITMAP | CLONE_CHILDREN.
292
 */
293
  public DistortedObjectTree(DistortedObjectTree node, int flags)
294
    {
295
    mParent = null;
296
    mTexture= new DistortedTexture(node.mTexture,flags);
297
    mQueues = new DistortedEffectQueues(node.mQueues, flags);
298
    mGrid   = node.mGrid;
299

    
300
    if( (flags & Distorted.CLONE_CHILDREN) != 0 )
301
      {
302
      mChildren = node.mChildren;
303
      mNumChildren = node.mNumChildren;
304
      }
305
    else
306
      {
307
      mChildren = null;
308
      mNumChildren = new int[1];
309
      mNumChildren[0] = 0;
310
      }
311
   
312
    ArrayList<Long> list = generateIDList();
313
   
314
    mData = mMapNodeID.get(list);
315
   
316
    if( mData!=null )
317
      {
318
      mData.numPointingNodes++;
319
      }
320
    else
321
      {
322
      mData = new NodeData(++mNextNodeID);   
323
      mMapNodeID.put(list, mData);
324
      }
325
    }
326
  
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328
/**
329
 * Adds a new child to the last position in the list of our Node's children.
330
 * 
331
 * @param node The new Node to add.
332
 */
333
  public synchronized void attach(DistortedObjectTree node)
334
    {
335
    ArrayList<Long> prev = generateIDList(); 
336
   
337
    if( mChildren==null ) mChildren = new ArrayList<>(2);
338
     
339
    node.mParent = this;
340
    mChildren.add(node);
341
    mNumChildren[0]++;
342
     
343
    RecomputeNodeID(prev);
344
    }
345
   
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347
/**
348
 * Adds a new child to the last position in the list of our Node's children.
349
 * 
350
 * @param texture DistortedTexture to initialize our child Node with.
351
 * @param queues DistortedEffectQueues to initialize our child Node with.
352
 * @param grid GridObject to initialize our child Node with.
353
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
354
 */
355
  public synchronized DistortedObjectTree attach(DistortedTexture texture, DistortedEffectQueues queues, GridObject grid)
356
    {
357
    ArrayList<Long> prev = generateIDList(); 
358
      
359
    if( mChildren==null ) mChildren = new ArrayList<>(2);
360
    DistortedObjectTree node = new DistortedObjectTree(texture,queues,grid);
361
    node.mParent = this;
362
    mChildren.add(node);
363
    mNumChildren[0]++;
364
   
365
    RecomputeNodeID(prev);
366

    
367
    return node;
368
    }
369
  
370
///////////////////////////////////////////////////////////////////////////////////////////////////
371
/**
372
 * Removes the first occurrence of a specified child from the list of children of our Node.
373
 * 
374
 * @param node The Node to remove.
375
 * @return <code>true</code> if the child was successfully removed.
376
 */
377
  public synchronized boolean detach(DistortedObjectTree node)
378
    {
379
    if( mNumChildren[0]>0 )
380
      {
381
      ArrayList<Long> prev = generateIDList();  
382
         
383
      if( mChildren.remove(node) )
384
        {
385
        node.mParent = null;  
386
        mNumChildren[0]--;
387
     
388
        RecomputeNodeID(prev);
389
     
390
        return true;
391
        }
392
      }
393
   
394
    return false;
395
    }
396
  
397
///////////////////////////////////////////////////////////////////////////////////////////////////
398
/**
399
 * Removes the first occurrence of a specified child from the list of children of our Node.
400
 * 
401
 * @param queues DistortedEffectQueues to remove.
402
 * @return <code>true</code> if the child was successfully removed.
403
 */
404
  public synchronized boolean detach(DistortedEffectQueues queues)
405
    {
406
    long id = queues.getID();
407
    DistortedObjectTree node;
408
   
409
    for(int i=0; i<mNumChildren[0]; i++)
410
      {
411
      node = mChildren.get(i);
412
     
413
      if( node.mQueues.getID()==id )
414
        {
415
        ArrayList<Long> prev = generateIDList();   
416
     
417
        node.mParent = null;  
418
        mChildren.remove(i);
419
        mNumChildren[0]--;
420
      
421
        RecomputeNodeID(prev);
422
      
423
        return true;
424
        }
425
      }
426
   
427
    return false;
428
    }
429
    
430
///////////////////////////////////////////////////////////////////////////////////////////////////
431
/**
432
 * Removes all children Nodes.
433
 */
434
  public synchronized void detachAll()
435
    {
436
    for(int i=0; i<mNumChildren[0]; i++)
437
      {
438
      mChildren.get(i).mParent = null;
439
      }
440
   
441
    if( mNumChildren[0]>0 )
442
      {
443
      ArrayList<Long> prev = generateIDList();  
444
      
445
      mNumChildren[0] = 0;
446
      mChildren.clear();
447
      RecomputeNodeID(prev);
448
      }
449
    }
450
  
451
///////////////////////////////////////////////////////////////////////////////////////////////////
452
/**
453
 * Draws the Node, and all its children, to the default framebuffer 0 (i.e. the screen).
454
 * <p>
455
 * Must be called from a thread holding OpenGL Context
456
 *
457
 * @param currTime Current time, in milliseconds.
458
 */
459
  public void draw(long currTime)
460
    {  
461
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
462
    drawRecursive(currTime,Distorted.mFramebuffer);
463
    DistortedFramebuffer.deleteAllMarked();
464
    DistortedTexture.deleteAllMarked();
465
    }
466

    
467
///////////////////////////////////////////////////////////////////////////////////////////////////
468
/**
469
 * Draws the Node, and all its children, to the Framebuffer passed.
470
 * <p>
471
 * Must be called from a thread holding OpenGL Context
472
 *
473
 * @param currTime Current time, in milliseconds.
474
 * @param df       Framebuffer to render this to.
475
 */
476
  public void draw(long currTime, DistortedFramebuffer df)
477
    {
478
    df.setAsOutput();
479
    drawRecursive(currTime,df);
480
    DistortedFramebuffer.deleteAllMarked();
481
    DistortedTexture.deleteAllMarked();
482
    }
483

    
484
///////////////////////////////////////////////////////////////////////////////////////////////////
485
/**
486
 * Returns the DistortedEffectQueues object that's in the Node.
487
 * 
488
 * @return The DistortedEffectQueues contained in the Node.
489
 */
490
  public DistortedEffectQueues getQueues()
491
    {
492
    return mQueues;
493
    }
494

    
495
///////////////////////////////////////////////////////////////////////////////////////////////////
496
/**
497
 * Returns the DistortedTexture object that's in the Node.
498
 *
499
 * @return The DistortedTexture contained in the Node.
500
 */
501
  public DistortedTexture getTexture()
502
    {
503
    return mTexture;
504
    }
505

    
506
///////////////////////////////////////////////////////////////////////////////////////////////////
507
  }
508

    
(4-4/15)