Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedNode.java @ 889cce10

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 (mData.numRender!).
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 DistortedNode mParent;
44
  private MeshObject mMesh;
45
  private DistortedEffects mEffects;
46
  private DistortedInputSurface mSurface;
47
  private NodeData mData;
48

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

    
52
  private class NodeData
53
    {
54
    long ID;
55
    int numPointingNodes;
56
    int numRender;
57
    ArrayList<Long> key;
58
    DistortedFramebuffer mFBO;
59

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

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

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
  private ArrayList<Long> generateIDList()
81
    {
82
    ArrayList<Long> ret = new ArrayList<>();
83
     
84
    ret.add( mSurface.getID() );
85

    
86
    if( mNumChildren[0]==0 )
87
      {
88
      ret.add(-mEffects.getID());
89
      }
90

    
91
    DistortedNode node;
92
   
93
    for(int i=0; i<mNumChildren[0]; i++)
94
      {
95
      node = mChildren.get(i);
96
      ret.add(node.mData.ID);
97
      }
98
   
99
    return ret;
100
    }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
// Debug - print all the Node IDs
104

    
105
  @SuppressWarnings("unused")
106
  void debug(int depth)
107
    {
108
    String tmp="";
109
    int i;
110

    
111
    for(i=0; i<depth; i++) tmp +="   ";
112
    tmp += ("NodeID="+mData.ID+" nodes pointing: "+mData.numPointingNodes+" surfaceID="+
113
            mSurface.getID()+" FBO="+(mData.mFBO==null ? "null":mData.mFBO.getID()))+
114
            " parent sID="+(mParent==null ? "null": (mParent.mSurface.getID()));
115

    
116
    android.util.Log.e("NODE", tmp);
117

    
118
    for(i=0; i<mNumChildren[0]; i++)
119
      mChildren.get(i).debug(depth+1);
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
// Debug - print contents of the HashMap
124

    
125
  @SuppressWarnings("unused")
126
  static void debugMap()
127
    {
128
    NodeData tmp;
129

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

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
// tree isomorphism algorithm
139

    
140
  private void adjustIsomorphism()
141
    {
142
    ArrayList<Long> newList = generateIDList();
143
    NodeData newData = mMapNodeID.get(newList);
144

    
145
    if( newData!=null )
146
      {
147
      newData.numPointingNodes++;
148
      }
149
    else
150
      {
151
      newData = new NodeData(++mNextNodeID,newList);
152
      mMapNodeID.put(newList,newData);
153
      }
154

    
155
    boolean deleteOldFBO = false;
156
    boolean createNewFBO = false;
157

    
158
    if( --mData.numPointingNodes==0 )
159
      {
160
      mMapNodeID.remove(mData.key);
161
      if( mData.mFBO!=null ) deleteOldFBO=true;
162
      }
163
    if( mNumChildren[0]>0 && newData.mFBO==null )
164
      {
165
      createNewFBO = true;
166
      }
167
    if( mNumChildren[0]==0 && newData.mFBO!=null )
168
      {
169
      newData.mFBO.markForDeletion();
170
      android.util.Log.d("NODE", "ERROR!! this NodeData cannot possibly contain a non-null FBO!! "+newData.mFBO.getID() );
171
      newData.mFBO = null;
172
      }
173

    
174
    if( deleteOldFBO && createNewFBO )
175
      {
176
      newData.mFBO = mData.mFBO;  // just copy over
177
      //android.util.Log.d("NODE", "copying over FBOs "+mData.mFBO.getID() );
178
      }
179
    else if( deleteOldFBO )
180
      {
181
      mData.mFBO.markForDeletion();
182
      //android.util.Log.d("NODE", "deleting old FBO "+mData.mFBO.getID() );
183
      mData.mFBO = null;
184
      }
185
    else if( createNewFBO )
186
      {
187
      newData.mFBO = new DistortedFramebuffer(true, DistortedSurface.TYPE_TREE, mSurface.getWidth(),mSurface.getHeight());
188
      //android.util.Log.d("NODE", "creating new FBO "+newData.mFBO.getID() );
189
      }
190

    
191
    mData = newData;
192

    
193
    if( mParent!=null ) mParent.adjustIsomorphism();
194
    }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197
// return the total number of render calls issued
198

    
199
  int drawRecursive(int renderNum, long currTime, DistortedOutputSurface surface)
200
    {
201
    int ret = 0;
202
    float halfX = mSurface.getWidth()/2.0f;
203
    float halfY = mSurface.getHeight()/2.0f;
204

    
205
    if( mNumChildren[0]>0 && mData.numRender!=renderNum )
206
      {
207
      mData.numRender = renderNum;
208
      mData.mFBO.setAsOutput();
209

    
210
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
211
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
212

    
213
      if( mSurface.setAsInput() )
214
        {
215
        ret++;
216
        DistortedEffects.drawNoEffectsPriv(halfX, halfY, mMesh, mData.mFBO);
217
        }
218

    
219
      for(int i=0; i<mNumChildren[0]; i++)
220
        {
221
        ret += mChildren.get(i).drawRecursive(renderNum, currTime, mData.mFBO);
222
        }
223
      }
224

    
225
    DistortedInputSurface input = mNumChildren[0]==0 ? mSurface : mData.mFBO;
226

    
227
    if( input.setAsInput() )
228
      {
229
      ret++;
230
      mEffects.drawPriv(halfX, halfY, mMesh, surface, currTime);
231
      }
232

    
233
    return ret;
234
    }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237
// PUBLIC API
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239
/**
240
 * Constructs new Node.
241
 *     
242
 * @param surface InputSurface to put into the new Node.
243
 * @param effects DistortedEffects to put into the new Node.
244
 * @param mesh MeshObject to put into the new Node.
245
 */
246
  public DistortedNode(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
247
    {
248
    mSurface       = surface;
249
    mEffects       = effects;
250
    mMesh          = mesh;
251
    mChildren      = null;
252
    mNumChildren   = new int[1];
253
    mNumChildren[0]= 0;
254
    mParent        = null;
255

    
256
    ArrayList<Long> list = new ArrayList<>();
257
    list.add(mSurface.getID());
258
    list.add(-mEffects.getID());
259

    
260
    mData = mMapNodeID.get(list);
261
   
262
    if( mData!=null )
263
      {
264
      mData.numPointingNodes++;
265
      }
266
    else
267
      {
268
      mData = new NodeData(++mNextNodeID,list);
269
      mMapNodeID.put(list, mData);
270
      }
271
    }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////  
274
/**
275
 * Copy-constructs new Node from another Node.
276
 *     
277
 * @param node The DistortedNode to copy data from.
278
 * @param flags bit field composed of a subset of the following:
279
 *        {@link Distorted#CLONE_SURFACE},  {@link Distorted#CLONE_MATRIX}, {@link Distorted#CLONE_VERTEX},
280
 *        {@link Distorted#CLONE_FRAGMENT} and {@link Distorted#CLONE_CHILDREN}.
281
 *        For example flags = CLONE_SURFACE | CLONE_CHILDREN.
282
 */
283
  public DistortedNode(DistortedNode node, int flags)
284
    {
285
    mEffects= new DistortedEffects(node.mEffects,flags);
286
    mMesh   = node.mMesh;
287
    mParent = null;
288

    
289
    if( (flags & Distorted.CLONE_SURFACE) != 0 )
290
      {
291
      mSurface = node.mSurface;
292
      }
293
    else
294
      {
295
      int w = node.mSurface.getWidth();
296
      int h = node.mSurface.getHeight();
297

    
298
      if( node.mSurface instanceof DistortedTexture )
299
        {
300
        mSurface = new DistortedTexture(w,h, DistortedSurface.TYPE_TREE);
301
        }
302
      else if( node.mSurface instanceof DistortedFramebuffer )
303
        {
304
        boolean hasDepth = ((DistortedFramebuffer) node.mSurface).hasDepth();
305
        mSurface = new DistortedFramebuffer(hasDepth,DistortedSurface.TYPE_TREE,w,h);
306
        }
307
      }
308
    if( (flags & Distorted.CLONE_CHILDREN) != 0 )
309
      {
310
      if( node.mChildren==null )     // do NOT copy over the NULL!
311
        {
312
        node.mChildren = new ArrayList<>(2);
313
        }
314

    
315
      mChildren = node.mChildren;
316
      mNumChildren = node.mNumChildren;
317
      }
318
    else
319
      {
320
      mChildren = null;
321
      mNumChildren = new int[1];
322
      mNumChildren[0] = 0;
323
      }
324
   
325
    ArrayList<Long> list = generateIDList();
326
   
327
    mData = mMapNodeID.get(list);
328
   
329
    if( mData!=null )
330
      {
331
      mData.numPointingNodes++;
332
      }
333
    else
334
      {
335
      mData = new NodeData(++mNextNodeID,list);
336
      mMapNodeID.put(list, mData);
337
      }
338
    }
339

    
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341
/**
342
 * Adds a new child to the last position in the list of our Node's children.
343
 * <p>
344
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
345
 * DistortedAttachDeamon (by calling attachNow())
346
 *
347
 * @param node The new Node to add.
348
 */
349
  public void attach(DistortedNode node)
350
    {
351
    DistortedAttachDaemon.attach(this,node);
352
    }
353

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

    
373
///////////////////////////////////////////////////////////////////////////////////////////////////
374
/**
375
 * This is not really part of the public API. Has to be public only because it is a part of the
376
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
377
 * Java has no multiple inheritance.
378
 *
379
 * @param node The new Node to add.
380
 */
381
  public void attachNow(DistortedNode node)
382
    {
383
    if( mChildren==null ) mChildren = new ArrayList<>(2);
384

    
385
    node.mParent = this;
386
    mChildren.add(node);
387
    mNumChildren[0]++;
388
    adjustIsomorphism();
389
    }
390

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

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

    
422
    for(int i=0; i<mNumChildren[0]; i++)
423
      {
424
      node = mChildren.get(i);
425

    
426
      if( node.mEffects.getID()==id )
427
        {
428
        DistortedAttachDaemon.detach(this,node);
429
        break;
430
        }
431
      }
432
    }
433

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

    
452
///////////////////////////////////////////////////////////////////////////////////////////////////
453
/**
454
 * Removes all children Nodes.
455
 * <p>
456
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
457
 * DistortedAttachDeamon (by calling detachAllNow())
458
 */
459
  public void detachAll()
460
    {
461
    DistortedAttachDaemon.detachAll(this);
462
    }
463

    
464
///////////////////////////////////////////////////////////////////////////////////////////////////
465
/**
466
 * This is not really part of the public API. Has to be public only because it is a part of the
467
 * DistortedAttacheable interface, which should really be a class that we extend here instead but
468
 * Java has no multiple inheritance.
469
 */
470
  public void detachAllNow()
471
    {
472
    if( mNumChildren[0]>0 )
473
      {
474
      DistortedNode tmp;
475

    
476
      for(int i=mNumChildren[0]-1; i>=0; i--)
477
        {
478
        tmp = mChildren.remove(i);
479
        tmp.mParent = null;
480
        }
481

    
482
      mNumChildren[0] = 0;
483
      adjustIsomorphism();
484
      }
485
    }
486

    
487
///////////////////////////////////////////////////////////////////////////////////////////////////
488
/**
489
 * Returns the DistortedEffects object that's in the Node.
490
 * 
491
 * @return The DistortedEffects contained in the Node.
492
 */
493
  public DistortedEffects getEffects()
494
    {
495
    return mEffects;
496
    }
497

    
498
///////////////////////////////////////////////////////////////////////////////////////////////////
499
/**
500
 * Returns the DistortedInputSurface object that's in the Node.
501
 *
502
 * @return The DistortedInputSurface contained in the Node.
503
 */
504
  public DistortedInputSurface getSurface()
505
    {
506
    return mSurface;
507
    }
508

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

    
520
  }
(7-7/22)