Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedNode.java @ 63b6561a

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
    ArrayList<Long> key;
57
    DistortedFramebuffer mFBO;
58

    
59
    NodeData(long id, ArrayList<Long> k)
60
      {
61
      ID              = id;
62
      key             = k;
63
      numPointingNodes= 1;
64
      numRendered     = 0;
65
      mFBO            = null;
66
      }
67
    }
68
 
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

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

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

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

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

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

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
// Debug - print all the Node IDs
112

    
113
  @SuppressWarnings("unused")
114
  void debug(int depth)
115
    {
116
    String tmp="";
117
    int i;
118

    
119
    for(i=0; i<depth; i++) tmp +="   ";
120
    tmp += ("NodeID="+mData.ID+" (nodes pointing: "+mData.numPointingNodes+" surfaceID="+mSurface.getID()+")");
121

    
122
    android.util.Log.e("NODE", tmp);
123

    
124
    for(i=0; i<mNumChildren[0]; i++)
125
      mChildren.get(i).debug(depth+1);
126
    }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
// Debug - print contents of the HashMap
130

    
131
  @SuppressWarnings("unused")
132
  static void debugMap()
133
    {
134
    NodeData tmp;
135

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

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  void treeIsomorphism()
146
    {
147
    for(int i=0; i<mNumChildren[0]; i++)
148
      {
149
      mChildren.get(i).treeIsomorphism();
150
      }
151

    
152
    ArrayList<Long> newList = generateIDList();
153
    NodeData newData = mMapNodeID.get(newList);
154

    
155
    if( newData==null )
156
      {
157
      newData = new NodeData(++mNextNodeID,newList);
158
      mMapNodeID.put(newList,newData);
159
      }
160
    else if( newData.ID != mData.ID )
161
      {
162
      newData.numPointingNodes++;
163
      }
164

    
165
    if( newData.ID != mData.ID )
166
      {
167
      boolean fboUsed = false;
168

    
169
      if( mNumChildren[0]>0 && newData.mFBO==null )
170
        {
171
        if( mData.mFBO!=null )
172
          {
173
          android.util.Log.d("NODE", "copying over FBO of node surfaceID="+mSurface.getID());
174

    
175
          newData.mFBO = mData.mFBO;
176
          fboUsed = true;
177
          }
178
        else
179
          {
180
          android.util.Log.d("NODE", "creating new FBO of node surfaceID="+mSurface.getID());
181
          newData.mFBO = new DistortedFramebuffer(mSurface.getWidth(),mSurface.getHeight());
182
          }
183
        }
184
      if( mNumChildren[0]==0 && newData.mFBO!=null )
185
        {
186
        android.util.Log.d("NODE", "deleting FBO of newData node!!");
187

    
188

    
189
        newData.mFBO.markForDeletion();
190
        newData.mFBO = null;
191
        }
192

    
193
      if( --mData.numPointingNodes==0 )
194
        {
195
        mMapNodeID.remove(mData.key);
196

    
197
        if( !fboUsed && mData.mFBO!=null )
198
          {
199
          android.util.Log.d("NODE", "deleting FBO of node surfaceID="+mSurface.getID());
200

    
201
          mData.mFBO.markForDeletion();
202
          mData.mFBO = null;
203
          }
204
        }
205

    
206
      mData = newData;
207
      }
208
    }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
  void drawRecursive(long currTime, DistortedOutputSurface surface)
213
    {
214
    float halfX = mSurface.getWidth()/2.0f;
215
    float halfY = mSurface.getHeight()/2.0f;
216

    
217
    if( mNumChildren[0]<=0 )
218
      {
219
      mSurface.setAsInput();
220
      }
221
    else
222
      {
223
      if( mData.numRendered==0 )
224
        {
225
        mData.mFBO.setAsOutput();
226

    
227
        GLES30.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
228
        GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
229

    
230
        if( mSurface.setAsInput() )
231
          DistortedEffects.drawNoEffectsPriv(halfX, halfY, mMesh, mData.mFBO );
232

    
233
        for(int i=0; i<mNumChildren[0]; i++)
234
          {
235
          mChildren.get(i).drawRecursive(currTime, mData.mFBO);
236
          }
237
        }
238

    
239
      mData.numRendered++;
240
      mData.numRendered %= mData.numPointingNodes;
241
      mData.mFBO.setAsInput();
242
      }
243

    
244
    mEffects.drawPriv(halfX, halfY, mMesh, surface, currTime);
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248
// PUBLIC API
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250
/**
251
 * Constructs new Node.
252
 *     
253
 * @param surface InputSurface to put into the new Node.
254
 * @param effects DistortedEffects to put into the new Node.
255
 * @param mesh MeshObject to put into the new Node.
256
 */
257
  public DistortedNode(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
258
    {
259
    mSurface       = surface;
260
    mEffects       = effects;
261
    mMesh          = mesh;
262
    mChildren      = null;
263
    mNumChildren   = new int[1];
264
    mNumChildren[0]= 0;
265
   
266
    ArrayList<Long> list = new ArrayList<>();
267
    list.add(mSurface.getID());
268

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

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////  
283
/**
284
 * Copy-constructs new Node from another Node.
285
 *     
286
 * @param node The DistortedNode to copy data from.
287
 * @param flags bit field composed of a subset of the following:
288
 *        {@link Distorted#CLONE_SURFACE},  {@link Distorted#CLONE_MATRIX}, {@link Distorted#CLONE_VERTEX},
289
 *        {@link Distorted#CLONE_FRAGMENT} and {@link Distorted#CLONE_CHILDREN}.
290
 *        For example flags = CLONE_SURFACE | CLONE_CHILDREN.
291
 */
292
  public DistortedNode(DistortedNode node, int flags)
293
    {
294
    mEffects= new DistortedEffects(node.mEffects,flags);
295
    mMesh = node.mMesh;
296

    
297
    if( (flags & Distorted.CLONE_SURFACE) != 0 )
298
      {
299
      mSurface = node.mSurface;
300
      }
301
    else
302
      {
303
      int w = node.mSurface.getWidth();
304
      int h = node.mSurface.getHeight();
305

    
306
      if( node.mSurface instanceof DistortedTexture )
307
        {
308
        mSurface = new DistortedTexture(w,h);
309
        }
310
      else if( node.mSurface instanceof DistortedFramebuffer )
311
        {
312
        boolean hasDepth = ((DistortedFramebuffer) node.mSurface).hasDepth();
313
        mSurface = new DistortedFramebuffer(w,h,hasDepth);
314
        }
315
      }
316
    if( (flags & Distorted.CLONE_CHILDREN) != 0 )
317
      {
318
      if( node.mChildren==null )     // do NOT copy over the NULL!
319
        {
320
        node.mChildren = new ArrayList<>(2);
321
        }
322

    
323
      mChildren = node.mChildren;
324
      mNumChildren = node.mNumChildren;
325
      }
326
    else
327
      {
328
      mChildren = null;
329
      mNumChildren = new int[1];
330
      mNumChildren[0] = 0;
331
      }
332
   
333
    ArrayList<Long> list = generateIDList();
334
   
335
    mData = mMapNodeID.get(list);
336
   
337
    if( mData!=null )
338
      {
339
      mData.numPointingNodes++;
340
      }
341
    else
342
      {
343
      mData = new NodeData(++mNextNodeID,list);
344
      mMapNodeID.put(list, mData);
345
      }
346
    }
347

    
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349
/**
350
 * Adds a new child to the last position in the list of our Node's children.
351
 * <p>
352
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
353
 * DistortedAttachDeamon (by calling attachNow())
354
 *
355
 * @param node The new Node to add.
356
 */
357
  public void attach(DistortedNode node)
358
    {
359
    DistortedAttachDaemon.attach(this,node);
360
    }
361

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

    
381
///////////////////////////////////////////////////////////////////////////////////////////////////
382
/**
383
 * This is not really part of the public API. Has to be public only because it is a part of the
384
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
385
 * Java has no multiple inheritance.
386
 *
387
 * @param node The new Node to add.
388
 */
389
  public void attachNow(DistortedNode node)
390
    {
391
    if( mChildren==null ) mChildren = new ArrayList<>(2);
392

    
393
    mChildren.add(node);
394
    mNumChildren[0]++;
395
    }
396

    
397
///////////////////////////////////////////////////////////////////////////////////////////////////
398
/**
399
 * Removes the first occurrence of a specified child from the list of children of our Node.
400
 * <p>
401
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
402
 * DistortedAttachDeamon (by calling detachNow())
403
 *
404
 * @param node The Node to remove.
405
 */
406
  public void detach(DistortedNode node)
407
    {
408
    DistortedAttachDaemon.detach(this,node);
409
    }
410

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

    
428
    for(int i=0; i<mNumChildren[0]; i++)
429
      {
430
      node = mChildren.get(i);
431

    
432
      if( node.mEffects.getID()==id )
433
        {
434
        DistortedAttachDaemon.detach(this,node);
435
        break;
436
        }
437
      }
438
    }
439

    
440
///////////////////////////////////////////////////////////////////////////////////////////////////
441
/**
442
 * This is not really part of the public API. Has to be public only because it is a part of the
443
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
444
 * Java has no multiple inheritance.
445
 *
446
 * @param node The Node to remove.
447
 */
448
  public void detachNow(DistortedNode node)
449
    {
450
    if( mNumChildren[0]>0 && mChildren.remove(node) )
451
      {
452
      mNumChildren[0]--;
453

    
454
      if( mNumChildren[0]==0 && mData.mFBO!=null )
455
        {
456
        mData.mFBO.markForDeletion();
457
        mData.mFBO = null;
458
        }
459
      }
460
    }
461

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

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

    
487
      if( mData.mFBO!=null )
488
        {
489
        mData.mFBO.markForDeletion();
490
        mData.mFBO = null;
491
        }
492
      }
493
    }
494

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

    
506
///////////////////////////////////////////////////////////////////////////////////////////////////
507
/**
508
 * Returns the DistortedInputSurface object that's in the Node.
509
 *
510
 * @return The DistortedInputSurface contained in the Node.
511
 */
512
  public DistortedInputSurface getSurface()
513
    {
514
    return mSurface;
515
    }
516

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

    
528
  }
(7-7/22)