Project

General

Profile

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

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

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.Collections;
24
import java.util.HashMap;
25

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

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

    
47
  private class Job
48
    {
49
    int type;
50
    DistortedNode node;
51
    DistortedEffectsPostprocess dep;
52

    
53
    Job(int t, DistortedNode n, DistortedEffectsPostprocess d)
54
      {
55
      type = t;
56
      node = n;
57
      dep  = d;
58
      }
59
    }
60

    
61
  private ArrayList<Job> mJobs = new ArrayList<>();
62

    
63
  private static HashMap<ArrayList<Long>,NodeData> mMapNodeID = new HashMap<>();
64
  private static long mNextNodeID =0;
65

    
66
  private DistortedNode mParent;
67
  private DistortedOutputSurface mSurfaceParent;
68
  private MeshObject mMesh;
69
  private DistortedEffects mEffects;
70
  private DistortedEffectsPostprocess mPostprocess;
71
  private DistortedInputSurface mSurface;
72
  private DistortedRenderState mState;
73
  private NodeData mData;
74
  private int mFboW, mFboH, mFboDepthStencil;
75

    
76
  private class NodeData
77
    {
78
    long ID;
79
    int numPointingNodes;
80
    long currTime;
81
    ArrayList<Long> key;
82
    DistortedFramebuffer mFBO;
83

    
84
    NodeData(long id, ArrayList<Long> k)
85
      {
86
      ID              = id;
87
      key             = k;
88
      numPointingNodes= 1;
89
      currTime        =-1;
90
      mFBO            = null;
91
      }
92
    }
93
 
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

    
96
  static synchronized void onPause()
97
    {
98
    NodeData data;
99

    
100
    for (HashMap.Entry<ArrayList<Long>,NodeData> entry : mMapNodeID.entrySet())
101
      {
102
      data = entry.getValue();
103

    
104
      if( data.mFBO != null )
105
        {
106
        data.mFBO.markForDeletion();
107
        data.mFBO = null;
108
        }
109
      }
110
    }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
  static synchronized void onDestroy()
115
    {
116
    mNextNodeID = 0;
117
    mMapNodeID.clear();
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
  private ArrayList<Long> generateIDList()
123
    {
124
    ArrayList<Long> ret = new ArrayList<>();
125

    
126
    if( mNumChildren[0]==0 )
127
      {
128
      // add a negative number so this leaf never gets confused with a internal node
129
      // with a single child that happens to have ID identical to some leaf's Effects ID.
130
      ret.add(-mEffects.getID());
131
      }
132
    else
133
      {
134
      DistortedNode node;
135
   
136
      for(int i=0; i<mNumChildren[0]; i++)
137
        {
138
        node = mChildren.get(i);
139
        ret.add(node.mData.ID);
140
        }
141

    
142
      // A bit questionable decision here - we are sorting the children IDs, which means
143
      // that order in which we draw the children is going to be undefined (well, this is not
144
      // strictly speaking true - when rendering, if no postprocessing and isomorphism are
145
      // involved, we *DO* render the children in order they were added; if however there
146
      // are two internal nodes with the same list of identical children, just added in a
147
      // different order each time, then we consider them isomorphic, i.e. identical and only
148
      // render the first one. If then two children of such 'pseudo-isomorphic' nodes are at
149
      // exactly the same Z-height this might result in some unexpected sights).
150
      //
151
      // Reason: with the children being sorted by postprocessing buckets, the order is
152
      // undefined anyway (although only when postprocessing is applied).
153
      //
154
      // See the consequences in the 'Olympic' app - remove a few leaves and add them back in
155
      // different order. You will see the number of renders go back to the original 14.
156
      Collections.sort(ret);
157
      }
158

    
159
    ret.add( 0, mSurface.getID() );
160

    
161
    return ret;
162
    }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165
// Debug - print all the Node IDs
166

    
167
  @SuppressWarnings("unused")
168
  void debug(int depth)
169
    {
170
    String tmp="";
171
    int i;
172

    
173
    for(i=0; i<depth; i++) tmp +="   ";
174
    tmp += ("NodeID="+mData.ID+" nodes pointing: "+mData.numPointingNodes+" surfaceID="+
175
            mSurface.getID()+" FBO="+(mData.mFBO==null ? "null":mData.mFBO.getID()))+
176
            " parent sID="+(mParent==null ? "null": (mParent.mSurface.getID()));
177

    
178
    android.util.Log.e("NODE", tmp);
179

    
180
    for(i=0; i<mNumChildren[0]; i++)
181
      mChildren.get(i).debug(depth+1);
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
// Debug - print contents of the HashMap
186

    
187
  @SuppressWarnings("unused")
188
  static void debugMap()
189
    {
190
    NodeData tmp;
191

    
192
    for(ArrayList<Long> key: mMapNodeID.keySet())
193
      {
194
      tmp = mMapNodeID.get(key);
195
      android.util.Log.e("NODE", "NodeID: "+tmp.ID+" <-- "+key);
196
      }
197
    }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200
// tree isomorphism algorithm
201

    
202
  private void adjustIsomorphism()
203
    {
204
    ArrayList<Long> newList = generateIDList();
205
    NodeData newData = mMapNodeID.get(newList);
206

    
207
    if( newData!=null )
208
      {
209
      newData.numPointingNodes++;
210
      }
211
    else
212
      {
213
      newData = new NodeData(++mNextNodeID,newList);
214
      mMapNodeID.put(newList,newData);
215
      }
216

    
217
    boolean deleteOldFBO = false;
218
    boolean createNewFBO = false;
219

    
220
    if( --mData.numPointingNodes==0 )
221
      {
222
      mMapNodeID.remove(mData.key);
223
      if( mData.mFBO!=null ) deleteOldFBO=true;
224
      }
225
    if( mNumChildren[0]>0 && newData.mFBO==null )
226
      {
227
      createNewFBO = true;
228
      }
229
    if( mNumChildren[0]==0 && newData.mFBO!=null )
230
      {
231
      newData.mFBO.markForDeletion();
232
      android.util.Log.d("NODE", "ERROR!! this NodeData cannot possibly contain a non-null FBO!! "+newData.mFBO.getID() );
233
      newData.mFBO = null;
234
      }
235

    
236
    if( deleteOldFBO && createNewFBO )
237
      {
238
      newData.mFBO = mData.mFBO;  // just copy over
239
      //android.util.Log.d("NODE", "copying over FBOs "+mData.mFBO.getID() );
240
      }
241
    else if( deleteOldFBO )
242
      {
243
      mData.mFBO.markForDeletion();
244
      //android.util.Log.d("NODE", "deleting old FBO "+mData.mFBO.getID() );
245
      mData.mFBO = null;
246
      }
247
    else if( createNewFBO )
248
      {
249
      int width  = mFboW <= 0 ? mSurface.getWidth()  : mFboW;
250
      int height = mFboH <= 0 ? mSurface.getHeight() : mFboH;
251
      newData.mFBO = new DistortedFramebuffer(mFboDepthStencil, DistortedSurface.TYPE_TREE, width, height);
252
      //android.util.Log.d("NODE", "creating new FBO "+newData.mFBO.getID() );
253
      }
254

    
255
    mData = newData;
256

    
257
    if( mParent!=null ) mParent.adjustIsomorphism();
258
    }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

    
262
  int markStencilAndDraw(long currTime, DistortedOutputSurface surface, float marginInPixels)
263
    {
264
    DistortedInputSurface input = mNumChildren[0]==0 ? mSurface : mData.mFBO;
265

    
266
    if( input.setAsInput() )
267
      {
268
      surface.setAsOutput(currTime);
269

    
270
      // Mark area of our object + marginInPixels pixels around with 1s in Stencil buffer
271
      DistortedRenderState.setUpStencilMark();
272
      mEffects.drawPriv(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime, marginInPixels);
273

    
274
      // Actually draw our object.
275
      mState.apply();
276
      mEffects.drawPriv(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime, 0);
277
      return 1;
278
      }
279
    return 0;
280
    }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283
// return the total number of render calls issued
284

    
285
  int draw(long currTime, DistortedOutputSurface surface)
286
    {
287
    DistortedInputSurface input = mNumChildren[0]==0 ? mSurface : mData.mFBO;
288

    
289
    if( input.setAsInput() )
290
      {
291
      surface.setAsOutput(currTime);
292
      mState.apply();
293
      mEffects.drawPriv(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime, 0);
294
      return 1;
295
      }
296

    
297
    return 0;
298
    }
299

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301
// return the total number of render calls issued
302

    
303
  int renderRecursive(long currTime)
304
    {
305
    int numRenders = 0;
306

    
307
    if( mNumChildren[0]>0 && mData.currTime!=currTime )
308
      {
309
      mData.currTime = currTime;
310

    
311
      for (int i=0; i<mNumChildren[0]; i++)
312
        {
313
        numRenders += mChildren.get(i).renderRecursive(currTime);
314
        }
315

    
316
      if( mData.mFBO==null )
317
        {
318
        int width  = mFboW <= 0 ? mSurface.getWidth()  : mFboW;
319
        int height = mFboH <= 0 ? mSurface.getHeight() : mFboH;
320
        mData.mFBO = new DistortedFramebuffer(mFboDepthStencil, DistortedSurface.TYPE_TREE, width, height);
321
        }
322

    
323
      mData.mFBO.setAsOutput(currTime);
324

    
325
      if( mSurface.setAsInput() )
326
        {
327
        numRenders++;
328
        DistortedEffects.blitPriv(mData.mFBO);
329
        }
330

    
331
      numRenders += mData.mFBO.renderChildren(currTime,mNumChildren[0],mChildren);
332
      }
333

    
334
    return numRenders;
335
    }
336

    
337
///////////////////////////////////////////////////////////////////////////////////////////////////
338

    
339
  private void newJob(int t, DistortedNode n, DistortedEffectsPostprocess d)
340
    {
341
    mJobs.add(new Job(t,n,d));
342
    }
343

    
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345

    
346
  void setPost(DistortedEffectsPostprocess dep)
347
    {
348
    mPostprocess = dep;
349
    }
350

    
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352

    
353
  void setSurfaceParent(DistortedOutputSurface dep)
354
    {
355
    mSurfaceParent = dep;
356
    mParent = null;
357
    }
358

    
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360
// PUBLIC API
361
///////////////////////////////////////////////////////////////////////////////////////////////////
362
/**
363
 * Constructs new Node.
364
 *     
365
 * @param surface InputSurface to put into the new Node.
366
 * @param effects DistortedEffects to put into the new Node.
367
 * @param mesh MeshObject to put into the new Node.
368
 */
369
  public DistortedNode(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
370
    {
371
    mSurface       = surface;
372
    mEffects       = effects;
373
    mPostprocess   = null;
374
    mMesh          = mesh;
375
    mState         = new DistortedRenderState();
376
    mChildren      = null;
377
    mNumChildren   = new int[1];
378
    mNumChildren[0]= 0;
379
    mParent        = null;
380
    mSurfaceParent = null;
381

    
382
    mFboW            = 0;  // i.e. take this from
383
    mFboH            = 0;  // mSurface's dimensions
384
    mFboDepthStencil = DistortedFramebuffer.DEPTH_NO_STENCIL;
385

    
386
    ArrayList<Long> list = new ArrayList<>();
387
    list.add(mSurface.getID());
388
    list.add(-mEffects.getID());
389

    
390
    mData = mMapNodeID.get(list);
391
   
392
    if( mData!=null )
393
      {
394
      mData.numPointingNodes++;
395
      }
396
    else
397
      {
398
      mData = new NodeData(++mNextNodeID,list);
399
      mMapNodeID.put(list, mData);
400
      }
401
    }
402

    
403
///////////////////////////////////////////////////////////////////////////////////////////////////  
404
/**
405
 * Copy-constructs new Node from another Node.
406
 *     
407
 * @param node The DistortedNode to copy data from.
408
 * @param flags bit field composed of a subset of the following:
409
 *        {@link Distorted#CLONE_SURFACE},  {@link Distorted#CLONE_MATRIX}, {@link Distorted#CLONE_VERTEX},
410
 *        {@link Distorted#CLONE_FRAGMENT} and {@link Distorted#CLONE_CHILDREN}.
411
 *        For example flags = CLONE_SURFACE | CLONE_CHILDREN.
412
 */
413
  public DistortedNode(DistortedNode node, int flags)
414
    {
415
    mEffects      = new DistortedEffects(node.mEffects,flags);
416
    mPostprocess  = null;
417
    mMesh         = node.mMesh;
418
    mState        = new DistortedRenderState();
419
    mParent       = null;
420
    mSurfaceParent= null;
421

    
422
    mFboW            = node.mFboW;
423
    mFboH            = node.mFboH;
424
    mFboDepthStencil = node.mFboDepthStencil;
425

    
426
    if( (flags & Distorted.CLONE_SURFACE) != 0 )
427
      {
428
      mSurface = node.mSurface;
429
      }
430
    else
431
      {
432
      int w = node.mSurface.getWidth();
433
      int h = node.mSurface.getHeight();
434

    
435
      if( node.mSurface instanceof DistortedTexture )
436
        {
437
        mSurface = new DistortedTexture(w,h, DistortedSurface.TYPE_TREE);
438
        }
439
      else if( node.mSurface instanceof DistortedFramebuffer )
440
        {
441
        int depthStencil = DistortedFramebuffer.NO_DEPTH_NO_STENCIL;
442

    
443
        if( ((DistortedFramebuffer) node.mSurface).hasDepth() )
444
          {
445
          boolean hasStencil = ((DistortedFramebuffer) node.mSurface).hasStencil();
446
          depthStencil = (hasStencil ? DistortedFramebuffer.BOTH_DEPTH_STENCIL:DistortedFramebuffer.DEPTH_NO_STENCIL);
447
          }
448

    
449
        mSurface = new DistortedFramebuffer(depthStencil,DistortedSurface.TYPE_TREE,w,h);
450
        }
451
      }
452
    if( (flags & Distorted.CLONE_CHILDREN) != 0 )
453
      {
454
      if( node.mChildren==null )     // do NOT copy over the NULL!
455
        {
456
        node.mChildren = new ArrayList<>(2);
457
        }
458

    
459
      mChildren = node.mChildren;
460
      mNumChildren = node.mNumChildren;
461
      }
462
    else
463
      {
464
      mChildren = null;
465
      mNumChildren = new int[1];
466
      mNumChildren[0] = 0;
467
      }
468
   
469
    ArrayList<Long> list = generateIDList();
470
   
471
    mData = mMapNodeID.get(list);
472
   
473
    if( mData!=null )
474
      {
475
      mData.numPointingNodes++;
476
      }
477
    else
478
      {
479
      mData = new NodeData(++mNextNodeID,list);
480
      mMapNodeID.put(list, mData);
481
      }
482
    }
483

    
484
///////////////////////////////////////////////////////////////////////////////////////////////////
485
/**
486
 * Adds a new child to the last position in the list of our Node's children.
487
 * <p>
488
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
489
 * DistortedMaster (by calling doWork())
490
 *
491
 * @param node The new Node to add.
492
 */
493
  public void attach(DistortedNode node)
494
    {
495
    mJobs.add(new Job(ATTACH,node,null));
496
    DistortedMaster.newSlave(this);
497
    }
498

    
499
///////////////////////////////////////////////////////////////////////////////////////////////////
500
/**
501
 * Adds a new child to the last position in the list of our Node's children.
502
 * <p>
503
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
504
 * DistortedMaster (by calling doWork())
505
 *
506
 * @param surface InputSurface to initialize our child Node with.
507
 * @param effects DistortedEffects to initialize our child Node with.
508
 * @param mesh MeshObject to initialize our child Node with.
509
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
510
 */
511
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
512
    {
513
    DistortedNode node = new DistortedNode(surface,effects,mesh);
514
    mJobs.add(new Job(ATTACH,node,null));
515
    DistortedMaster.newSlave(this);
516
    return node;
517
    }
518

    
519
///////////////////////////////////////////////////////////////////////////////////////////////////
520
/**
521
 * Removes the first occurrence of a specified child from the list of children of our Node.
522
 * <p>
523
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
524
 * DistortedMaster (by calling doWork())
525
 *
526
 * @param node The Node to remove.
527
 */
528
  public void detach(DistortedNode node)
529
    {
530
    mJobs.add(new Job(DETACH,node,null));
531
    DistortedMaster.newSlave(this);
532
    }
533

    
534
///////////////////////////////////////////////////////////////////////////////////////////////////
535
/**
536
 * Removes the first occurrence of a specified child from the list of children of our Node.
537
 * <p>
538
 * A bit questionable method as there can be many different Nodes attached as children, some
539
 * of them having the same Effects but - for instance - different Mesh. Use with care.
540
 * <p>
541
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
542
 * DistortedMaster (by calling doWork())
543
 *
544
 * @param effects DistortedEffects to remove.
545
 */
546
  public void detach(DistortedEffects effects)
547
    {
548
    long id = effects.getID();
549
    DistortedNode node;
550
    boolean detached = false;
551

    
552
    for(int i=0; i<mNumChildren[0]; i++)
553
      {
554
      node = mChildren.get(i);
555

    
556
      if( node.getEffects().getID()==id )
557
        {
558
        detached = true;
559
        mJobs.add(new Job(DETACH,node,null));
560
        DistortedMaster.newSlave(this);
561
        break;
562
        }
563
      }
564

    
565
    if( !detached )
566
      {
567
      // if we failed to detach any, it still might be the case that
568
      // there's an ATTACH job that we need to cancel.
569
      int num = mJobs.size();
570
      Job job;
571

    
572
      for(int i=0; i<num; i++)
573
        {
574
        job = mJobs.get(i);
575

    
576
        if( job.type==ATTACH && job.node.getEffects()==effects )
577
          {
578
          mJobs.remove(i);
579
          break;
580
          }
581
        }
582
      }
583
    }
584

    
585
///////////////////////////////////////////////////////////////////////////////////////////////////
586
/**
587
 * Removes all children Nodes.
588
 * <p>
589
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
590
 * DistortedMaster (by calling doWork())
591
 */
592
  public void detachAll()
593
    {
594
    mJobs.add(new Job(DETALL,null,null));
595
    DistortedMaster.newSlave(this);
596
    }
597

    
598
///////////////////////////////////////////////////////////////////////////////////////////////////
599
/**
600
 * This is not really part of the public API. Has to be public only because it is a part of the
601
 * DistortedSlave interface, which should really be a class that we extend here instead but
602
 * Java has no multiple inheritance.
603
 *
604
 * @y.exclude
605
 */
606
  public void doWork()
607
    {
608
    int num = mJobs.size();
609
    Job job;
610

    
611
    int numChanges=0;
612

    
613
    for(int i=0; i<num; i++)
614
      {
615
      job = mJobs.remove(0);
616

    
617
      switch(job.type)
618
        {
619
        case ATTACH: numChanges++;
620
                     if( mChildren==null ) mChildren = new ArrayList<>(2);
621
                     job.node.mParent = this;
622
                     job.node.mSurfaceParent = null;
623
                     DistortedMaster.addSorted(mChildren,job.node);
624
                     mNumChildren[0]++;
625
                     break;
626
        case DETACH: numChanges++;
627
                     if( mNumChildren[0]>0 && mChildren.remove(job.node) )
628
                       {
629
                       job.node.mParent = null;
630
                       job.node.mSurfaceParent = null;
631
                       mNumChildren[0]--;
632
                       }
633
                     break;
634
        case DETALL: numChanges++;
635
                     if( mNumChildren[0]>0 )
636
                       {
637
                       DistortedNode tmp;
638

    
639
                       for(int j=mNumChildren[0]-1; j>=0; j--)
640
                         {
641
                         tmp = mChildren.remove(j);
642
                         tmp.mParent = null;
643
                         tmp.mSurfaceParent = null;
644
                         }
645

    
646
                       mNumChildren[0] = 0;
647
                       }
648
                     break;
649
        case SORT  : job.node.mPostprocess = job.dep;
650
                     mChildren.remove(job.node);
651
                     DistortedMaster.addSorted(mChildren,job.node);
652
                     break;
653
        }
654
      }
655

    
656
    if( numChanges>0 ) adjustIsomorphism();
657
    }
658
///////////////////////////////////////////////////////////////////////////////////////////////////
659
/**
660
 * Sets the Postprocessing Effects we will apply to the temporary buffer this Node - and fellow siblings
661
 * with the same Effects - will get rendered to.
662
 * <p>
663
 * For efficiency reasons, it is very important to assign the very same DistortedEffectsPostprocess
664
 * object to all the DistortedNode siblings that are supposed to be postprocessed in the same way,
665
 * because only then will the library assign all such siblings to the same 'Bucket' which gets rendered
666
 * to the same offscreen buffer which then gets postprocessed in one go and subsequently merged to the
667
 * target Surface.
668
 */
669
  public void setPostprocessEffects(DistortedEffectsPostprocess dep)
670
    {
671
    if( mParent!=null )
672
      {
673
      mParent.newJob(SORT, this, dep);
674
      DistortedMaster.newSlave(mParent);
675
      }
676
    else if( mSurfaceParent!=null )
677
      {
678
      mSurfaceParent.newJob(SORT, this, dep);
679
      DistortedMaster.newSlave(mSurfaceParent);
680
      }
681
    else
682
      {
683
      mPostprocess = dep;
684
      }
685
    }
686

    
687
///////////////////////////////////////////////////////////////////////////////////////////////////
688
/**
689
 * Returns the DistortedEffectsPostprocess object that's in the Node.
690
 *
691
 * @return The DistortedEffectsPostprocess contained in the Node.
692
 */
693
  public DistortedEffectsPostprocess getEffectsPostprocess()
694
    {
695
    return mPostprocess;
696
    }
697

    
698
///////////////////////////////////////////////////////////////////////////////////////////////////
699
/**
700
 * Returns the DistortedEffects object that's in the Node.
701
 * 
702
 * @return The DistortedEffects contained in the Node.
703
 */
704
  public DistortedEffects getEffects()
705
    {
706
    return mEffects;
707
    }
708

    
709
///////////////////////////////////////////////////////////////////////////////////////////////////
710
/**
711
 * Returns the DistortedInputSurface object that's in the Node.
712
 *
713
 * @return The DistortedInputSurface contained in the Node.
714
 */
715
  public DistortedInputSurface getSurface()
716
    {
717
    return mSurface;
718
    }
719

    
720
///////////////////////////////////////////////////////////////////////////////////////////////////
721
/**
722
 * Resizes the DistortedFramebuffer object that we render this Node to.
723
 */
724
  public void resize(int width, int height)
725
    {
726
    mFboW = width;
727
    mFboH = height;
728

    
729
    if ( mData.mFBO !=null )
730
      {
731
      // TODO: potentially allocate a new NodeData if we have to
732
      mData.mFBO.resize(width,height);
733
      }
734
    }
735

    
736
///////////////////////////////////////////////////////////////////////////////////////////////////
737
/**
738
 * Enables/disables DEPTH and STENCIL buffers in the Framebuffer object that we render this Node to.
739
 */
740
  public void enableDepthStencil(int depthStencil)
741
    {
742
    mFboDepthStencil = depthStencil;
743

    
744
    if ( mData.mFBO !=null )
745
      {
746
      // TODO: potentially allocate a new NodeData if we have to
747
      mData.mFBO.enableDepthStencil(depthStencil);
748
      }
749
    }
750

    
751
///////////////////////////////////////////////////////////////////////////////////////////////////
752
// APIs that control how to set the OpenGL state just before rendering this Node.
753
///////////////////////////////////////////////////////////////////////////////////////////////////
754
/**
755
 * When rendering this Node, use ColorMask (r,g,b,a).
756
 *
757
 * @param r Write to the RED color channel when rendering this Node?
758
 * @param g Write to the GREEN color channel when rendering this Node?
759
 * @param b Write to the BLUE color channel when rendering this Node?
760
 * @param a Write to the ALPHA channel when rendering this Node?
761
 */
762
  @SuppressWarnings("unused")
763
  public void glColorMask(boolean r, boolean g, boolean b, boolean a)
764
    {
765
    mState.glColorMask(r,g,b,a);
766
    }
767

    
768
///////////////////////////////////////////////////////////////////////////////////////////////////
769
/**
770
 * When rendering this Node, switch on writing to Depth buffer?
771
 *
772
 * @param mask Write to the Depth buffer when rendering this Node?
773
 */
774
  @SuppressWarnings("unused")
775
  public void glDepthMask(boolean mask)
776
    {
777
    mState.glDepthMask(mask);
778
    }
779

    
780
///////////////////////////////////////////////////////////////////////////////////////////////////
781
/**
782
 * When rendering this Node, which bits of the Stencil buffer to write to?
783
 *
784
 * @param mask Marks the bits of the Stencil buffer we will write to when rendering this Node.
785
 */
786
  @SuppressWarnings("unused")
787
  public void glStencilMask(int mask)
788
    {
789
    mState.glStencilMask(mask);
790
    }
791

    
792
///////////////////////////////////////////////////////////////////////////////////////////////////
793
/**
794
 * When rendering this Node, which Tests to enable?
795
 *
796
 * @param test Valid values: GL_DEPTH_TEST, GL_STENCIL_TEST, GL_BLEND
797
 */
798
  @SuppressWarnings("unused")
799
  public void glEnable(int test)
800
    {
801
    mState.glEnable(test);
802
    }
803

    
804
///////////////////////////////////////////////////////////////////////////////////////////////////
805
/**
806
 * When rendering this Node, which Tests to enable?
807
 *
808
 * @param test Valid values: GL_DEPTH_TEST, GL_STENCIL_TEST, GL_BLEND
809
 */
810
  @SuppressWarnings("unused")
811
  public void glDisable(int test)
812
    {
813
    mState.glDisable(test);
814
    }
815

    
816
///////////////////////////////////////////////////////////////////////////////////////////////////
817
/**
818
 * When rendering this Node, use the following StencilFunc.
819
 *
820
 * @param func Valid values: GL_NEVER, GL_ALWAYS, GL_LESS, GL_LEQUAL, GL_EQUAL, GL_GEQUAL, GL_GREATER, GL_NOTEQUAL
821
 * @param ref  Reference valut to compare our stencil with.
822
 * @param mask Mask used when comparing.
823
 */
824
  @SuppressWarnings("unused")
825
  public void glStencilFunc(int func, int ref, int mask)
826
    {
827
    mState.glStencilFunc(func,ref,mask);
828
    }
829

    
830
///////////////////////////////////////////////////////////////////////////////////////////////////
831
/**
832
 * When rendering this Node, use the following StencilOp.
833
 * <p>
834
 * Valid values of all 3 parameters: GL_KEEP, GL_ZERO, GL_REPLACE, GL_INCR, GL_DECR, GL_INVERT, GL_INCR_WRAP, GL_DECR_WRAP
835
 *
836
 * @param sfail  What to do when Stencil Test fails.
837
 * @param dpfail What to do when Depth Test fails.
838
 * @param dppass What to do when Depth Test passes.
839
 */
840
  @SuppressWarnings("unused")
841
  public void glStencilOp(int sfail, int dpfail, int dppass)
842
    {
843
    mState.glStencilOp(sfail,dpfail,dppass);
844
    }
845

    
846
///////////////////////////////////////////////////////////////////////////////////////////////////
847
/**
848
 * When rendering this Node, use the following DepthFunc.
849
 *
850
 * @param func Valid values: GL_NEVER, GL_ALWAYS, GL_LESS, GL_LEQUAL, GL_EQUAL, GL_GEQUAL, GL_GREATER, GL_NOTEQUAL
851
 */
852
  @SuppressWarnings("unused")
853
  public void glDepthFunc(int func)
854
    {
855
    mState.glDepthFunc(func);
856
    }
857

    
858
///////////////////////////////////////////////////////////////////////////////////////////////////
859
/**
860
 * When rendering this Node, use the following Blending mode.
861
 * <p>
862
 * Valid values: GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
863
 *               GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA, GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR,
864
 *               GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA, GL_SRC_ALPHA_SATURATE
865
 *
866
 * @param src Source Blend function
867
 * @param dst Destination Blend function
868
 */
869
  @SuppressWarnings("unused")
870
  public void glBlendFunc(int src, int dst)
871
    {
872
    mState.glBlendFunc(src,dst);
873
    }
874

    
875
///////////////////////////////////////////////////////////////////////////////////////////////////
876
/**
877
 * Before rendering this Node, clear the following buffers.
878
 * <p>
879
 * Valid values: 0, or bitwise OR of one or more values from the set GL_COLOR_BUFFER_BIT,
880
 *               GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT.
881
 * Default: 0
882
 *
883
 * @param mask bitwise OR of BUFFER_BITs to clear.
884
 */
885
  @SuppressWarnings("unused")
886
  public void glClear(int mask)
887
    {
888
    mState.glClear(mask);
889
    }
890
  }
(7-7/26)