Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedNode.java @ c204c69d

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.GLES30;
26

    
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28
/**
29
 * Class which represents a Node in a Tree of (InputSurface,Mesh,Effects) triplets.
30
 * <p>
31
 * Having organized such sets into a Tree, we can then render any Node to any OutputSurface.
32
 * That recursively renders the set held in the Node and all its children.
33
 * <p>
34
 * The class takes special care to only render identical sub-trees once. Each Node holds a reference
35
 * to sub-class 'NodeData'. Two identical sub-trees attached at different points of the main tree
36
 * will point to the same NodeData; only the first of this is rendered (when mData.numRendered==0).
37
 */
38
public class DistortedNode implements DistortedAttacheable
39
  {
40
  private static HashMap<ArrayList<Long>,NodeData> mMapNodeID = new HashMap<>();
41
  private static long mNextNodeID =0;
42

    
43
  private MeshObject mMesh;
44
  private DistortedEffects mEffects;
45
  private DistortedInputSurface mSurface;
46
  private NodeData mData;
47

    
48
  private ArrayList<DistortedNode> mChildren;
49
  private int[] mNumChildren;  // ==mChildren.length(), but we only create mChildren if the first one gets added
50

    
51
  private class NodeData
52
    {
53
    long ID;
54
    int numPointingNodes;
55
    int numRendered;
56
    DistortedFramebuffer mFBO;
57

    
58
    NodeData(long id)
59
      {
60
      ID              = id;
61
      numPointingNodes= 1;
62
      numRendered     = 0;
63
      mFBO            = null;
64
      }
65
    }
66
 
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
  static synchronized void onDestroy()
70
    {
71
    mNextNodeID = 0;
72
    mMapNodeID.clear();
73
    }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

    
77
  private ArrayList<Long> generateIDList()
78
    {
79
    ArrayList<Long> ret = new ArrayList<>();
80
     
81
    ret.add( mSurface.getID() );
82
    DistortedNode node;
83
   
84
    for(int i=0; i<mNumChildren[0]; i++)
85
      {
86
      node = mChildren.get(i);
87
      ret.add(node.mData.ID);
88
      }
89
   
90
    return ret;
91
    }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////  
94
// this will be called on startup and every time OpenGL context has been lost
95

    
96
  static void reset()
97
    {
98
    NodeData tmp;   
99
     
100
    for(ArrayList<Long> key: mMapNodeID.keySet())
101
      {
102
      tmp = mMapNodeID.get(key);
103
          
104
      if( tmp.mFBO != null ) tmp.numRendered = 0;
105
      }
106
    }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
// Debug - print all the Node IDs
110

    
111
  void debug(int depth)
112
    {
113
    String tmp="";
114
    int i;
115

    
116
    for(i=0; i<depth; i++) tmp +="   ";
117
    tmp += ("NodeID="+mData.ID+" (nodes pointing: "+mData.numPointingNodes+" surfaceID="+mSurface.getID()+")");
118

    
119
    android.util.Log.e("NODE", tmp);
120

    
121
    for(i=0; i<mNumChildren[0]; i++)
122
      mChildren.get(i).debug(depth+1);
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
// Debug - print contents of the HashMap
127

    
128
  static void debugMap()
129
    {
130
    NodeData tmp;
131

    
132
    for(ArrayList<Long> key: mMapNodeID.keySet())
133
      {
134
      tmp = mMapNodeID.get(key);
135
      android.util.Log.e("NODE", "NodeID: "+tmp.ID+" <-- "+key);
136
      }
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
  void treeIsomorphism()
142
    {
143
    android.util.Log.e("NODE", "begin treeIso for Node Surface ID="+mSurface.getID());
144
    debug(0);
145
    debugMap();
146
    android.util.Log.e("NODE", "begin treeIso for Node Surface ID="+mSurface.getID());
147

    
148
    ArrayList<Long> oldList = generateIDList();
149

    
150
    for(int i=0; i<mNumChildren[0]; i++)
151
      {
152
      mChildren.get(i).treeIsomorphism();
153
      }
154

    
155
    ArrayList<Long> newList = generateIDList();
156
    NodeData newData = mMapNodeID.get(newList);
157

    
158
    if( newData==null )
159
      {
160
      android.util.Log.d("NODE", "  inserted newData to map, newList="+newList);
161

    
162

    
163
      newData = new NodeData(++mNextNodeID);
164
      mMapNodeID.put(newList,newData);
165
      debugMap();
166
      }
167
    else if( newData.ID != mData.ID )
168
      {
169
      newData.numPointingNodes++;
170
      }
171

    
172
    if( newData.ID != mData.ID )
173
      {
174
      boolean fboUsed = false;
175

    
176
      if( mNumChildren[0]>0 && newData.mFBO==null )
177
        {
178
        if( mData.mFBO!=null )
179
          {
180
          newData.mFBO = mData.mFBO;
181
          fboUsed = true;
182
          }
183
        else
184
          {
185
          newData.mFBO = new DistortedFramebuffer(mSurface.getWidth(),mSurface.getHeight());
186
          }
187
        }
188
      if( mNumChildren[0]==0 && newData.mFBO!=null )
189
        {
190
        newData.mFBO.markForDeletion();
191
        newData.mFBO = null;
192
        }
193

    
194
      if( --mData.numPointingNodes==0 )
195
        {
196
        android.util.Log.d("NODE", "  removed oldData to map, oldList="+oldList);
197

    
198

    
199
        mMapNodeID.remove(oldList);
200
        debugMap();
201

    
202
        if( !fboUsed && mData.mFBO!=null )
203
          {
204
          mData.mFBO.markForDeletion();
205
          mData.mFBO = null;
206
          }
207
        }
208

    
209
      mData = newData;
210
      }
211

    
212
    android.util.Log.e("NODE", "end treeIso for Node SurfaceID="+mSurface.getID()+" newList="+newList);
213
    debug(0);
214
    debugMap();
215
    android.util.Log.e("NODE", "end treeIso for Node SurfaceID="+mSurface.getID()+" newList="+newList);
216
    }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
  void drawRecursive(long currTime, DistortedOutputSurface surface)
221
    {
222
    float halfX = mSurface.getWidth()/2.0f;
223
    float halfY = mSurface.getHeight()/2.0f;
224

    
225
    if( mNumChildren[0]<=0 )
226
      {
227
      mSurface.setAsInput();
228
      }
229
    else
230
      {
231
      if( mData.numRendered==0 )
232
        {
233
        mData.mFBO.setAsOutput();
234

    
235
        GLES30.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
236
        GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
237

    
238
        if( mSurface.setAsInput() )
239
          DistortedEffects.drawNoEffectsPriv(halfX, halfY, mMesh, mData.mFBO );
240

    
241
        for(int i=0; i<mNumChildren[0]; i++)
242
          {
243
          mChildren.get(i).drawRecursive(currTime, mData.mFBO);
244
          }
245
        }
246

    
247
      mData.numRendered++;
248
      mData.numRendered %= mData.numPointingNodes;
249
      mData.mFBO.setAsInput();
250
      }
251

    
252
    mEffects.drawPriv(halfX, halfY, mMesh, surface, currTime);
253
    }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256
// PUBLIC API
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258
/**
259
 * Constructs new Node.
260
 *     
261
 * @param surface InputSurface to put into the new Node.
262
 * @param effects DistortedEffects to put into the new Node.
263
 * @param mesh MeshObject to put into the new Node.
264
 */
265
  public DistortedNode(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
266
    {
267
    mSurface       = surface;
268
    mEffects       = effects;
269
    mMesh          = mesh;
270
    mChildren      = null;
271
    mNumChildren   = new int[1];
272
    mNumChildren[0]= 0;
273
   
274
    ArrayList<Long> list = new ArrayList<>();
275
    list.add(mSurface.getID());
276

    
277
    mData = mMapNodeID.get(list);
278
   
279
    if( mData!=null )
280
      {
281
      mData.numPointingNodes++;
282
      }
283
    else
284
      {
285
      mData = new NodeData(++mNextNodeID);   
286
      mMapNodeID.put(list, mData);
287
      }
288
    }
289

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////  
291
/**
292
 * Copy-constructs new Node from another Node.
293
 *     
294
 * @param node The DistortedNode to copy data from.
295
 * @param flags bit field composed of a subset of the following:
296
 *        {@link Distorted#CLONE_SURFACE},  {@link Distorted#CLONE_MATRIX}, {@link Distorted#CLONE_VERTEX},
297
 *        {@link Distorted#CLONE_FRAGMENT} and {@link Distorted#CLONE_CHILDREN}.
298
 *        For example flags = CLONE_SURFACE | CLONE_CHILDREN.
299
 */
300
  public DistortedNode(DistortedNode node, int flags)
301
    {
302
    mEffects= new DistortedEffects(node.mEffects,flags);
303
    mMesh = node.mMesh;
304

    
305
    if( (flags & Distorted.CLONE_SURFACE) != 0 )
306
      {
307
      mSurface = node.mSurface;
308
      }
309
    else
310
      {
311
      int w = node.mSurface.getWidth();
312
      int h = node.mSurface.getHeight();
313

    
314
      if( node.mSurface instanceof DistortedTexture )
315
        {
316
        mSurface = new DistortedTexture(w,h);
317
        }
318
      else if( node.mSurface instanceof DistortedFramebuffer )
319
        {
320
        boolean hasDepth = ((DistortedFramebuffer) node.mSurface).hasDepth();
321
        mSurface = new DistortedFramebuffer(w,h,hasDepth);
322
        }
323
      }
324
    if( (flags & Distorted.CLONE_CHILDREN) != 0 )
325
      {
326
      if( node.mChildren==null )     // do NOT copy over the NULL!
327
        {
328
        node.mChildren = new ArrayList<>(2);
329
        }
330

    
331
      mChildren = node.mChildren;
332
      mNumChildren = node.mNumChildren;
333
      }
334
    else
335
      {
336
      mChildren = null;
337
      mNumChildren = new int[1];
338
      mNumChildren[0] = 0;
339
      }
340
   
341
    ArrayList<Long> list = generateIDList();
342
   
343
    mData = mMapNodeID.get(list);
344
   
345
    if( mData!=null )
346
      {
347
      mData.numPointingNodes++;
348
      }
349
    else
350
      {
351
      mData = new NodeData(++mNextNodeID);   
352
      mMapNodeID.put(list, mData);
353
      }
354
    }
355

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357
/**
358
 * Adds a new child to the last position in the list of our Node's children.
359
 * <p>
360
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
361
 * DistortedAttachDeamon (by calling attachNow())
362
 *
363
 * @param node The new Node to add.
364
 */
365
  public void attach(DistortedNode node)
366
    {
367
    DistortedAttachDaemon.attach(this,node);
368
    }
369

    
370
///////////////////////////////////////////////////////////////////////////////////////////////////
371
/**
372
 * Adds a new child to the last position in the list of our Node's children.
373
 * <p>
374
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
375
 * DistortedAttachDeamon (by calling attachNow())
376
 *
377
 * @param surface InputSurface to initialize our child Node with.
378
 * @param effects DistortedEffects to initialize our child Node with.
379
 * @param mesh MeshObject to initialize our child Node with.
380
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
381
 */
382
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
383
    {
384
    DistortedNode node = new DistortedNode(surface,effects,mesh);
385
    DistortedAttachDaemon.attach(this,node);
386
    return node;
387
    }
388

    
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390
/**
391
 * This is not really part of the public API. Has to be public only because it is a part of the
392
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
393
 * Java has no multiple inheritance.
394
 *
395
 * @param node The new Node to add.
396
 */
397
  public void attachNow(DistortedNode node)
398
    {
399
    if( mChildren==null ) mChildren = new ArrayList<>(2);
400

    
401
    mChildren.add(node);
402
    mNumChildren[0]++;
403
    }
404

    
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406
/**
407
 * Removes the first occurrence of a specified child from the list of children of our Node.
408
 * <p>
409
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
410
 * DistortedAttachDeamon (by calling detachNow())
411
 *
412
 * @param node The Node to remove.
413
 */
414
  public void detach(DistortedNode node)
415
    {
416
    DistortedAttachDaemon.detach(this,node);
417
    }
418

    
419
///////////////////////////////////////////////////////////////////////////////////////////////////
420
/**
421
 * Removes the first occurrence of a specified child from the list of children of our Node.
422
 * <p>
423
 * A bit questionable method as there can be many different Nodes attached as children, some
424
 * of them having the same Effects but - for instance - different Mesh. Use with care.
425
 * <p>
426
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
427
 * DistortedAttachDeamon (by calling detachNow())
428
 *
429
 * @param effects DistortedEffects to remove.
430
 */
431
  public void detach(DistortedEffects effects)
432
    {
433
    long id = effects.getID();
434
    DistortedNode node;
435

    
436
    for(int i=0; i<mNumChildren[0]; i++)
437
      {
438
      node = mChildren.get(i);
439

    
440
      if( node.mEffects.getID()==id )
441
        {
442
        DistortedAttachDaemon.detach(this,node);
443
        break;
444
        }
445
      }
446
    }
447

    
448
///////////////////////////////////////////////////////////////////////////////////////////////////
449
/**
450
 * This is not really part of the public API. Has to be public only because it is a part of the
451
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
452
 * Java has no multiple inheritance.
453
 *
454
 * @param node The Node to remove.
455
 */
456
  public void detachNow(DistortedNode node)
457
    {
458
    if( mNumChildren[0]>0 && mChildren.remove(node) )
459
      {
460
      mNumChildren[0]--;
461
      }
462
    }
463

    
464
///////////////////////////////////////////////////////////////////////////////////////////////////
465
/**
466
 * Removes all children Nodes.
467
 * <p>
468
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
469
 * DistortedAttachDeamon (by calling detachAllNow())
470
 */
471
  public void detachAll()
472
    {
473
    DistortedAttachDaemon.detachAll(this);
474
    }
475

    
476
///////////////////////////////////////////////////////////////////////////////////////////////////
477
/**
478
 * This is not really part of the public API. Has to be public only because it is a part of the
479
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
480
 * Java has no multiple inheritance.
481
 */
482
  public void detachAllNow()
483
    {
484
    if( mNumChildren[0]>0 )
485
      {
486
      mNumChildren[0] = 0;
487
      mChildren.clear();
488
      }
489
    }
490

    
491
///////////////////////////////////////////////////////////////////////////////////////////////////
492
/**
493
 * Returns the DistortedEffects object that's in the Node.
494
 * 
495
 * @return The DistortedEffects contained in the Node.
496
 */
497
  public DistortedEffects getEffects()
498
    {
499
    return mEffects;
500
    }
501

    
502
///////////////////////////////////////////////////////////////////////////////////////////////////
503
/**
504
 * Returns the DistortedInputSurface object that's in the Node.
505
 *
506
 * @return The DistortedInputSurface contained in the Node.
507
 */
508
  public DistortedInputSurface getSurface()
509
    {
510
    return mSurface;
511
    }
512

    
513
///////////////////////////////////////////////////////////////////////////////////////////////////
514
/**
515
 * Returns the DistortedFramebuffer object that's in the Node.
516
 *
517
 * @return The DistortedFramebuffer contained in the Node.
518
 */
519
  public DistortedFramebuffer getFramebuffer()
520
    {
521
    return mData.mFBO;
522
    }
523

    
524
  }
(7-7/22)