Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedNode.java @ 0c303a2c

1 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 6a06a912 Leszek Koltunski
package org.distorted.library;
21
22
import java.util.ArrayList;
23
import java.util.HashMap;
24
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26
/**
27 a09ada4c Leszek Koltunski
 * Class which represents a Node in a Tree of (InputSurface,Mesh,Effects) triplets.
28 c204c69d leszek
 * <p>
29 a09ada4c Leszek Koltunski
 * Having organized such sets into a Tree, we can then render any Node to any OutputSurface.
30 7b8086eb Leszek Koltunski
 * That recursively renders the set held in the Node and all its children.
31 c204c69d leszek
 * <p>
32
 * The class takes special care to only render identical sub-trees once. Each Node holds a reference
33
 * to sub-class 'NodeData'. Two identical sub-trees attached at different points of the main tree
34 3a70bd6d leszek
 * will point to the same NodeData; only the first of this is rendered (mData.numRender!).
35 6a06a912 Leszek Koltunski
 */
36 efe3d8fe leszek
public class DistortedNode implements DistortedSlave
37 6a06a912 Leszek Koltunski
  {
38 efe3d8fe leszek
  private static final int ATTACH = 0;
39
  private static final int DETACH = 1;
40
  private static final int DETALL = 2;
41
  private static final int SORT   = 3;
42
43
  private ArrayList<DistortedNode> mChildren;
44
  private int[] mNumChildren;  // ==mChildren.length(), but we only create mChildren if the first one gets added
45
46
  private class Job
47
    {
48
    int type;
49
    DistortedNode node;
50
    DistortedEffectsPostprocess dep;
51
52
    Job(int t, DistortedNode n, DistortedEffectsPostprocess d)
53
      {
54
      type = t;
55
      node = n;
56
      dep  = d;
57
      }
58
    }
59
60
  private ArrayList<Job> mJobs = new ArrayList<>();
61
62 bd3da5b2 Leszek Koltunski
  private static HashMap<ArrayList<Long>,NodeData> mMapNodeID = new HashMap<>();
63
  private static long mNextNodeID =0;
64
65 f28fffc2 Leszek Koltunski
  private DistortedNode mParent;
66 be60d4ff leszek
  private DistortedOutputSurface mSurfaceParent;
67 05403bba Leszek Koltunski
  private MeshObject mMesh;
68 07d8ef09 Leszek Koltunski
  private DistortedEffects mEffects;
69 13687207 leszek
  private DistortedEffectsPostprocess mPostprocess;
70 c5369f1b leszek
  private DistortedInputSurface mSurface;
71 c834348d leszek
  private DistortedRenderState mState;
72 6a06a912 Leszek Koltunski
  private NodeData mData;
73 bd3da5b2 Leszek Koltunski
74 6a06a912 Leszek Koltunski
  private class NodeData
75
    {
76 bd3da5b2 Leszek Koltunski
    long ID;
77 6a06a912 Leszek Koltunski
    int numPointingNodes;
78 50642a86 Leszek Koltunski
    long currTime;
79 af27df87 leszek
    ArrayList<Long> key;
80 8c327653 Leszek Koltunski
    DistortedFramebuffer mFBO;
81 6a06a912 Leszek Koltunski
82 af27df87 leszek
    NodeData(long id, ArrayList<Long> k)
83 6a06a912 Leszek Koltunski
      {
84 bd3da5b2 Leszek Koltunski
      ID              = id;
85 af27df87 leszek
      key             = k;
86 bd3da5b2 Leszek Koltunski
      numPointingNodes= 1;
87 50642a86 Leszek Koltunski
      currTime        =-1;
88 8c327653 Leszek Koltunski
      mFBO            = null;
89 6a06a912 Leszek Koltunski
      }
90 bd3da5b2 Leszek Koltunski
    }
91 6a06a912 Leszek Koltunski
 
92 0c303a2c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
93
94
  static synchronized void onPause()
95
    {
96
    NodeData data;
97
98
    for (HashMap.Entry<ArrayList<Long>,NodeData> entry : mMapNodeID.entrySet())
99
      {
100
      data = entry.getValue();
101
102
      if( data.mFBO != null )
103
        {
104
        data.mFBO.markForDeletion();
105
        data.mFBO = null;
106
        }
107
      }
108
    }
109
110 436899f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
111
112 7b8086eb Leszek Koltunski
  static synchronized void onDestroy()
113 436899f2 Leszek Koltunski
    {
114
    mNextNodeID = 0;
115
    mMapNodeID.clear();
116
    }
117
118 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
119
120
  private ArrayList<Long> generateIDList()
121
    {
122 9361b337 Leszek Koltunski
    ArrayList<Long> ret = new ArrayList<>();
123 6a06a912 Leszek Koltunski
     
124 c5369f1b leszek
    ret.add( mSurface.getID() );
125 7691a39f leszek
126
    if( mNumChildren[0]==0 )
127
      {
128
      ret.add(-mEffects.getID());
129
      }
130
131 a09ada4c Leszek Koltunski
    DistortedNode node;
132 6a06a912 Leszek Koltunski
   
133
    for(int i=0; i<mNumChildren[0]; i++)
134
      {
135
      node = mChildren.get(i);
136
      ret.add(node.mData.ID);
137
      }
138
   
139
    return ret;
140
    }
141
142 fee0865c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
143
// Debug - print all the Node IDs
144 c204c69d leszek
145 af27df87 leszek
  @SuppressWarnings("unused")
146 fee0865c Leszek Koltunski
  void debug(int depth)
147
    {
148
    String tmp="";
149
    int i;
150
151
    for(i=0; i<depth; i++) tmp +="   ";
152 07037b8a leszek
    tmp += ("NodeID="+mData.ID+" nodes pointing: "+mData.numPointingNodes+" surfaceID="+
153 f28fffc2 Leszek Koltunski
            mSurface.getID()+" FBO="+(mData.mFBO==null ? "null":mData.mFBO.getID()))+
154
            " parent sID="+(mParent==null ? "null": (mParent.mSurface.getID()));
155 fee0865c Leszek Koltunski
156 1942537e Leszek Koltunski
    android.util.Log.e("NODE", tmp);
157 fee0865c Leszek Koltunski
158
    for(i=0; i<mNumChildren[0]; i++)
159
      mChildren.get(i).debug(depth+1);
160
    }
161
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
// Debug - print contents of the HashMap
164
165 af27df87 leszek
  @SuppressWarnings("unused")
166 fee0865c Leszek Koltunski
  static void debugMap()
167
    {
168
    NodeData tmp;
169
170
    for(ArrayList<Long> key: mMapNodeID.keySet())
171
      {
172
      tmp = mMapNodeID.get(key);
173 c204c69d leszek
      android.util.Log.e("NODE", "NodeID: "+tmp.ID+" <-- "+key);
174
      }
175
    }
176
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178 f28fffc2 Leszek Koltunski
// tree isomorphism algorithm
179 c204c69d leszek
180 f28fffc2 Leszek Koltunski
  private void adjustIsomorphism()
181 c204c69d leszek
    {
182
    ArrayList<Long> newList = generateIDList();
183
    NodeData newData = mMapNodeID.get(newList);
184
185 f28fffc2 Leszek Koltunski
    if( newData!=null )
186
      {
187
      newData.numPointingNodes++;
188
      }
189
    else
190 c204c69d leszek
      {
191 af27df87 leszek
      newData = new NodeData(++mNextNodeID,newList);
192 c204c69d leszek
      mMapNodeID.put(newList,newData);
193
      }
194 07037b8a leszek
195 f28fffc2 Leszek Koltunski
    boolean deleteOldFBO = false;
196
    boolean createNewFBO = false;
197 c204c69d leszek
198 f28fffc2 Leszek Koltunski
    if( --mData.numPointingNodes==0 )
199 c204c69d leszek
      {
200 f28fffc2 Leszek Koltunski
      mMapNodeID.remove(mData.key);
201
      if( mData.mFBO!=null ) deleteOldFBO=true;
202
      }
203
    if( mNumChildren[0]>0 && newData.mFBO==null )
204
      {
205
      createNewFBO = true;
206
      }
207
    if( mNumChildren[0]==0 && newData.mFBO!=null )
208
      {
209
      newData.mFBO.markForDeletion();
210
      android.util.Log.d("NODE", "ERROR!! this NodeData cannot possibly contain a non-null FBO!! "+newData.mFBO.getID() );
211
      newData.mFBO = null;
212
      }
213 c204c69d leszek
214 f28fffc2 Leszek Koltunski
    if( deleteOldFBO && createNewFBO )
215
      {
216
      newData.mFBO = mData.mFBO;  // just copy over
217 eadf0859 leszek
      //android.util.Log.d("NODE", "copying over FBOs "+mData.mFBO.getID() );
218 f28fffc2 Leszek Koltunski
      }
219
    else if( deleteOldFBO )
220
      {
221
      mData.mFBO.markForDeletion();
222 eadf0859 leszek
      //android.util.Log.d("NODE", "deleting old FBO "+mData.mFBO.getID() );
223 f28fffc2 Leszek Koltunski
      mData.mFBO = null;
224
      }
225
    else if( createNewFBO )
226
      {
227
      newData.mFBO = new DistortedFramebuffer(true, DistortedSurface.TYPE_TREE, mSurface.getWidth(),mSurface.getHeight());
228 eadf0859 leszek
      //android.util.Log.d("NODE", "creating new FBO "+newData.mFBO.getID() );
229 f28fffc2 Leszek Koltunski
      }
230 af27df87 leszek
231 f28fffc2 Leszek Koltunski
    mData = newData;
232 c204c69d leszek
233 f28fffc2 Leszek Koltunski
    if( mParent!=null ) mParent.adjustIsomorphism();
234 fee0865c Leszek Koltunski
    }
235 c204c69d leszek
236 39086ebb leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
237
// return the total number of render calls issued
238
239
  int draw(long currTime, DistortedOutputSurface surface)
240
    {
241
    DistortedInputSurface input = mNumChildren[0]==0 ? mSurface : mData.mFBO;
242
243
    if( input.setAsInput() )
244
      {
245
      mState.apply();
246
      mEffects.drawPriv(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime);
247
      return 1;
248
      }
249
250
    return 0;
251
    }
252
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254
// return the total number of render calls issued
255
256
  int renderRecursive(long currTime)
257
    {
258
    int numRenders = 0;
259
260
    if( mNumChildren[0]>0 && mData.currTime!=currTime )
261
      {
262
      mData.currTime = currTime;
263
264
      for (int i=0; i<mNumChildren[0]; i++)
265
        {
266
        numRenders += mChildren.get(i).renderRecursive(currTime);
267
        }
268
269 0c303a2c Leszek Koltunski
      if( mData.mFBO==null )
270
        {
271
        mData.mFBO = new DistortedFramebuffer(true, DistortedSurface.TYPE_TREE, mSurface.getWidth(),mSurface.getHeight());
272
        }
273
274 95c441a2 leszek
      mData.mFBO.setAsOutput(currTime);
275 39086ebb leszek
276
      if( mSurface.setAsInput() )
277
        {
278
        numRenders++;
279
        DistortedEffects.blitPriv(mData.mFBO);
280
        }
281
282 b2939df4 Leszek Koltunski
      numRenders += mData.mFBO.renderChildren(currTime,mNumChildren[0],mChildren);
283 39086ebb leszek
      }
284
285
    return numRenders;
286
    }
287
288 be60d4ff leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
289
290
  private void newJob(int t, DistortedNode n, DistortedEffectsPostprocess d)
291
    {
292
    mJobs.add(new Job(t,n,d));
293
    }
294
295
///////////////////////////////////////////////////////////////////////////////////////////////////
296
297
  void setPost(DistortedEffectsPostprocess dep)
298
    {
299
    mPostprocess = dep;
300
    }
301
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303
304
  void setSurfaceParent(DistortedOutputSurface dep)
305
    {
306
    mSurfaceParent = dep;
307
    mParent = null;
308
    }
309
310 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
311
// PUBLIC API
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313
/**
314 a09ada4c Leszek Koltunski
 * Constructs new Node.
315 6a06a912 Leszek Koltunski
 *     
316 c5369f1b leszek
 * @param surface InputSurface to put into the new Node.
317 07d8ef09 Leszek Koltunski
 * @param effects DistortedEffects to put into the new Node.
318 05403bba Leszek Koltunski
 * @param mesh MeshObject to put into the new Node.
319 6a06a912 Leszek Koltunski
 */
320 a09ada4c Leszek Koltunski
  public DistortedNode(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
321 6a06a912 Leszek Koltunski
    {
322 c5369f1b leszek
    mSurface       = surface;
323 8ca9f899 Leszek Koltunski
    mEffects       = effects;
324 13687207 leszek
    mPostprocess   = null;
325 8ca9f899 Leszek Koltunski
    mMesh          = mesh;
326 c834348d leszek
    mState         = new DistortedRenderState();
327 8ca9f899 Leszek Koltunski
    mChildren      = null;
328
    mNumChildren   = new int[1];
329
    mNumChildren[0]= 0;
330 f28fffc2 Leszek Koltunski
    mParent        = null;
331 be60d4ff leszek
    mSurfaceParent = null;
332 f28fffc2 Leszek Koltunski
333 9361b337 Leszek Koltunski
    ArrayList<Long> list = new ArrayList<>();
334 c5369f1b leszek
    list.add(mSurface.getID());
335 7691a39f leszek
    list.add(-mEffects.getID());
336 1942537e Leszek Koltunski
337 6a06a912 Leszek Koltunski
    mData = mMapNodeID.get(list);
338
   
339
    if( mData!=null )
340
      {
341
      mData.numPointingNodes++;
342
      }
343
    else
344
      {
345 af27df87 leszek
      mData = new NodeData(++mNextNodeID,list);
346 1942537e Leszek Koltunski
      mMapNodeID.put(list, mData);
347 6a06a912 Leszek Koltunski
      }
348
    }
349
350
///////////////////////////////////////////////////////////////////////////////////////////////////  
351
/**
352 a09ada4c Leszek Koltunski
 * Copy-constructs new Node from another Node.
353 6a06a912 Leszek Koltunski
 *     
354 a09ada4c Leszek Koltunski
 * @param node The DistortedNode to copy data from.
355 6a06a912 Leszek Koltunski
 * @param flags bit field composed of a subset of the following:
356 29a06526 Leszek Koltunski
 *        {@link Distorted#CLONE_SURFACE},  {@link Distorted#CLONE_MATRIX}, {@link Distorted#CLONE_VERTEX},
357 6a06a912 Leszek Koltunski
 *        {@link Distorted#CLONE_FRAGMENT} and {@link Distorted#CLONE_CHILDREN}.
358 29a06526 Leszek Koltunski
 *        For example flags = CLONE_SURFACE | CLONE_CHILDREN.
359 6a06a912 Leszek Koltunski
 */
360 a09ada4c Leszek Koltunski
  public DistortedNode(DistortedNode node, int flags)
361 6a06a912 Leszek Koltunski
    {
362 be60d4ff leszek
    mEffects      = new DistortedEffects(node.mEffects,flags);
363
    mPostprocess  = null;
364
    mMesh         = node.mMesh;
365
    mState        = new DistortedRenderState();
366
    mParent       = null;
367
    mSurfaceParent= null;
368 9361b337 Leszek Koltunski
369 29a06526 Leszek Koltunski
    if( (flags & Distorted.CLONE_SURFACE) != 0 )
370 e7a20702 Leszek Koltunski
      {
371 c5369f1b leszek
      mSurface = node.mSurface;
372 e7a20702 Leszek Koltunski
      }
373
    else
374
      {
375 c5369f1b leszek
      int w = node.mSurface.getWidth();
376
      int h = node.mSurface.getHeight();
377 8ca9f899 Leszek Koltunski
378 c5369f1b leszek
      if( node.mSurface instanceof DistortedTexture )
379 8ca9f899 Leszek Koltunski
        {
380 09ab7524 Leszek Koltunski
        mSurface = new DistortedTexture(w,h, DistortedSurface.TYPE_TREE);
381 8ca9f899 Leszek Koltunski
        }
382 c5369f1b leszek
      else if( node.mSurface instanceof DistortedFramebuffer )
383 8ca9f899 Leszek Koltunski
        {
384 c5369f1b leszek
        boolean hasDepth = ((DistortedFramebuffer) node.mSurface).hasDepth();
385 09ab7524 Leszek Koltunski
        mSurface = new DistortedFramebuffer(hasDepth,DistortedSurface.TYPE_TREE,w,h);
386 8ca9f899 Leszek Koltunski
        }
387 e7a20702 Leszek Koltunski
      }
388 9361b337 Leszek Koltunski
    if( (flags & Distorted.CLONE_CHILDREN) != 0 )
389 6a06a912 Leszek Koltunski
      {
390 c204c69d leszek
      if( node.mChildren==null )     // do NOT copy over the NULL!
391
        {
392
        node.mChildren = new ArrayList<>(2);
393
        }
394
395 6a06a912 Leszek Koltunski
      mChildren = node.mChildren;
396
      mNumChildren = node.mNumChildren;
397
      }
398
    else
399
      {
400
      mChildren = null;
401
      mNumChildren = new int[1];
402
      mNumChildren[0] = 0;
403
      }
404
   
405
    ArrayList<Long> list = generateIDList();
406
   
407
    mData = mMapNodeID.get(list);
408
   
409
    if( mData!=null )
410
      {
411
      mData.numPointingNodes++;
412
      }
413
    else
414
      {
415 af27df87 leszek
      mData = new NodeData(++mNextNodeID,list);
416 6a06a912 Leszek Koltunski
      mMapNodeID.put(list, mData);
417
      }
418
    }
419 c204c69d leszek
420 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
421
/**
422
 * Adds a new child to the last position in the list of our Node's children.
423 c204c69d leszek
 * <p>
424
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
425 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
426 c204c69d leszek
 *
427 6a06a912 Leszek Koltunski
 * @param node The new Node to add.
428
 */
429 c204c69d leszek
  public void attach(DistortedNode node)
430 6a06a912 Leszek Koltunski
    {
431 efe3d8fe leszek
    mJobs.add(new Job(ATTACH,node,null));
432
    DistortedMaster.newSlave(this);
433 6a06a912 Leszek Koltunski
    }
434 c204c69d leszek
435 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
436
/**
437
 * Adds a new child to the last position in the list of our Node's children.
438 c204c69d leszek
 * <p>
439
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
440 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
441 c204c69d leszek
 *
442 c5369f1b leszek
 * @param surface InputSurface to initialize our child Node with.
443 07d8ef09 Leszek Koltunski
 * @param effects DistortedEffects to initialize our child Node with.
444 05403bba Leszek Koltunski
 * @param mesh MeshObject to initialize our child Node with.
445 6a06a912 Leszek Koltunski
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
446
 */
447 c204c69d leszek
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
448 6a06a912 Leszek Koltunski
    {
449 c204c69d leszek
    DistortedNode node = new DistortedNode(surface,effects,mesh);
450 efe3d8fe leszek
    mJobs.add(new Job(ATTACH,node,null));
451
    DistortedMaster.newSlave(this);
452 c204c69d leszek
    return node;
453
    }
454 f8377ef8 leszek
455 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
456
/**
457
 * Removes the first occurrence of a specified child from the list of children of our Node.
458 c204c69d leszek
 * <p>
459
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
460 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
461 c204c69d leszek
 *
462 6a06a912 Leszek Koltunski
 * @param node The Node to remove.
463
 */
464 c204c69d leszek
  public void detach(DistortedNode node)
465 6a06a912 Leszek Koltunski
    {
466 efe3d8fe leszek
    mJobs.add(new Job(DETACH,node,null));
467
    DistortedMaster.newSlave(this);
468 6a06a912 Leszek Koltunski
    }
469 a09ada4c Leszek Koltunski
470 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
471
/**
472
 * Removes the first occurrence of a specified child from the list of children of our Node.
473 a09ada4c Leszek Koltunski
 * <p>
474
 * A bit questionable method as there can be many different Nodes attached as children, some
475
 * of them having the same Effects but - for instance - different Mesh. Use with care.
476 c204c69d leszek
 * <p>
477
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
478 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
479 a09ada4c Leszek Koltunski
 *
480 07d8ef09 Leszek Koltunski
 * @param effects DistortedEffects to remove.
481 6a06a912 Leszek Koltunski
 */
482 c204c69d leszek
  public void detach(DistortedEffects effects)
483 6a06a912 Leszek Koltunski
    {
484 07d8ef09 Leszek Koltunski
    long id = effects.getID();
485 a09ada4c Leszek Koltunski
    DistortedNode node;
486 efe3d8fe leszek
    boolean detached = false;
487 a09ada4c Leszek Koltunski
488 6a06a912 Leszek Koltunski
    for(int i=0; i<mNumChildren[0]; i++)
489
      {
490
      node = mChildren.get(i);
491 a09ada4c Leszek Koltunski
492 efe3d8fe leszek
      if( node.getEffects().getID()==id )
493 6a06a912 Leszek Koltunski
        {
494 efe3d8fe leszek
        detached = true;
495
        mJobs.add(new Job(DETACH,node,null));
496
        DistortedMaster.newSlave(this);
497 c204c69d leszek
        break;
498 6a06a912 Leszek Koltunski
        }
499
      }
500 8baa1fe6 Leszek Koltunski
501
    if( !detached )
502
      {
503
      // if we failed to detach any, it still might be the case that
504 efe3d8fe leszek
      // there's an ATTACH job that we need to cancel.
505
      int num = mJobs.size();
506
      Job job;
507 a09ada4c Leszek Koltunski
508 efe3d8fe leszek
      for(int i=0; i<num; i++)
509
        {
510
        job = mJobs.get(i);
511
512
        if( job.type==ATTACH && job.node.getEffects()==effects )
513
          {
514
          mJobs.remove(i);
515
          break;
516
          }
517
        }
518 6a06a912 Leszek Koltunski
      }
519 c204c69d leszek
    }
520
521
///////////////////////////////////////////////////////////////////////////////////////////////////
522
/**
523
 * Removes all children Nodes.
524
 * <p>
525
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
526 efe3d8fe leszek
 * DistortedMaster (by calling doWork())
527 c204c69d leszek
 */
528
  public void detachAll()
529
    {
530 efe3d8fe leszek
    mJobs.add(new Job(DETALL,null,null));
531
    DistortedMaster.newSlave(this);
532 c204c69d leszek
    }
533
534
///////////////////////////////////////////////////////////////////////////////////////////////////
535
/**
536
 * This is not really part of the public API. Has to be public only because it is a part of the
537 efe3d8fe leszek
 * DistortedSlave interface, which should really be a class that we extend here instead but
538 c204c69d leszek
 * Java has no multiple inheritance.
539 d3725071 Leszek Koltunski
 *
540
 * @y.exclude
541 c204c69d leszek
 */
542 efe3d8fe leszek
  public void doWork()
543 c204c69d leszek
    {
544 efe3d8fe leszek
    int num = mJobs.size();
545
    Job job;
546
547
    int numChanges=0;
548
549
    for(int i=0; i<num; i++)
550 6a06a912 Leszek Koltunski
      {
551 efe3d8fe leszek
      job = mJobs.remove(0);
552 af27df87 leszek
553 efe3d8fe leszek
      switch(job.type)
554 af27df87 leszek
        {
555 efe3d8fe leszek
        case ATTACH: numChanges++;
556
                     if( mChildren==null ) mChildren = new ArrayList<>(2);
557
                     job.node.mParent = this;
558 be60d4ff leszek
                     job.node.mSurfaceParent = null;
559
                     DistortedMaster.addSorted(mChildren,job.node);
560 efe3d8fe leszek
                     mNumChildren[0]++;
561
                     break;
562
        case DETACH: numChanges++;
563
                     if( mNumChildren[0]>0 && mChildren.remove(job.node) )
564
                       {
565
                       job.node.mParent = null;
566 be60d4ff leszek
                       job.node.mSurfaceParent = null;
567 efe3d8fe leszek
                       mNumChildren[0]--;
568
                       }
569
                     break;
570
        case DETALL: numChanges++;
571
                     if( mNumChildren[0]>0 )
572
                       {
573
                       DistortedNode tmp;
574
575
                       for(int j=mNumChildren[0]-1; j>=0; j--)
576
                         {
577
                         tmp = mChildren.remove(j);
578
                         tmp.mParent = null;
579 be60d4ff leszek
                         tmp.mSurfaceParent = null;
580 efe3d8fe leszek
                         }
581
582
                       mNumChildren[0] = 0;
583
                       }
584
                     break;
585 be60d4ff leszek
        case SORT  : job.node.mPostprocess = job.dep;
586
                     mChildren.remove(job.node);
587
                     DistortedMaster.addSorted(mChildren,job.node);
588 efe3d8fe leszek
                     break;
589 af27df87 leszek
        }
590 efe3d8fe leszek
      }
591 f28fffc2 Leszek Koltunski
592 be60d4ff leszek
    if( numChanges>0 ) adjustIsomorphism();
593 6a06a912 Leszek Koltunski
    }
594 13687207 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
595
/**
596
 * Sets the Postprocessing Effects we will apply to the temporary buffer this Node - and fellow siblings
597
 * with the same Effects - will get rendered to.
598
 * <p>
599
 * For efficiency reasons, it is very important to assign the very same DistortedEffectsPostprocess
600
 * object to all the DistortedNode siblings that are supposed to be postprocessed in the same way,
601
 * because only then will the library assign all such siblings to the same 'Bucket' which gets rendered
602
 * to the same offscreen buffer which then gets postprocessed in one go and subsequently merged to the
603
 * target Surface.
604
 */
605
  public void setPostprocessEffects(DistortedEffectsPostprocess dep)
606
    {
607 be60d4ff leszek
    if( mParent!=null )
608
      {
609
      mParent.newJob(SORT, this, dep);
610
      DistortedMaster.newSlave(mParent);
611
      }
612
    else if( mSurfaceParent!=null )
613
      {
614
      mSurfaceParent.newJob(SORT, this, dep);
615
      DistortedMaster.newSlave(mSurfaceParent);
616
      }
617
    else
618
      {
619
      mPostprocess = dep;
620
      }
621 13687207 leszek
    }
622
623 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
624 27f42cd6 leszek
/**
625
 * Returns the DistortedEffectsPostprocess object that's in the Node.
626
 *
627
 * @return The DistortedEffectsPostprocess contained in the Node.
628
 */
629
  public DistortedEffectsPostprocess getEffectsPostprocess()
630
    {
631
    return mPostprocess;
632
    }
633
634
///////////////////////////////////////////////////////////////////////////////////////////////////
635 6a06a912 Leszek Koltunski
/**
636 421c2728 Leszek Koltunski
 * Returns the DistortedEffects object that's in the Node.
637 6a06a912 Leszek Koltunski
 * 
638 421c2728 Leszek Koltunski
 * @return The DistortedEffects contained in the Node.
639 6a06a912 Leszek Koltunski
 */
640 421c2728 Leszek Koltunski
  public DistortedEffects getEffects()
641 6a06a912 Leszek Koltunski
    {
642 07d8ef09 Leszek Koltunski
    return mEffects;
643 4e2382f3 Leszek Koltunski
    }
644
645
///////////////////////////////////////////////////////////////////////////////////////////////////
646
/**
647 c5369f1b leszek
 * Returns the DistortedInputSurface object that's in the Node.
648 4e2382f3 Leszek Koltunski
 *
649 c5369f1b leszek
 * @return The DistortedInputSurface contained in the Node.
650 4e2382f3 Leszek Koltunski
 */
651 c5369f1b leszek
  public DistortedInputSurface getSurface()
652 4e2382f3 Leszek Koltunski
    {
653 c5369f1b leszek
    return mSurface;
654 6a06a912 Leszek Koltunski
    }
655
656 8c327653 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
657
/**
658
 * Returns the DistortedFramebuffer object that's in the Node.
659
 *
660
 * @return The DistortedFramebuffer contained in the Node.
661
 */
662
  public DistortedFramebuffer getFramebuffer()
663
    {
664
    return mData.mFBO;
665
    }
666 6a06a912 Leszek Koltunski
667 c834348d leszek
668
///////////////////////////////////////////////////////////////////////////////////////////////////
669
/**
670
 * When rendering this Node, use ColorMask (r,g,b,a).
671
 *
672
 * @param r Write to the RED color channel when rendering this Node?
673
 * @param g Write to the GREEN color channel when rendering this Node?
674
 * @param b Write to the BLUE color channel when rendering this Node?
675
 * @param a Write to the ALPHA channel when rendering this Node?
676
 */
677 13687207 leszek
  @SuppressWarnings("unused")
678 c834348d leszek
  public void glColorMask(boolean r, boolean g, boolean b, boolean a)
679
    {
680
    mState.glColorMask(r,g,b,a);
681
    }
682
683
///////////////////////////////////////////////////////////////////////////////////////////////////
684
/**
685
 * When rendering this Node, switch on writing to Depth buffer?
686
 *
687
 * @param mask Write to the Depth buffer when rendering this Node?
688
 */
689 13687207 leszek
  @SuppressWarnings("unused")
690 c834348d leszek
  public void glDepthMask(boolean mask)
691
    {
692
    mState.glDepthMask(mask);
693
    }
694
695
///////////////////////////////////////////////////////////////////////////////////////////////////
696
/**
697
 * When rendering this Node, which bits of the Stencil buffer to write to?
698
 *
699
 * @param mask Marks the bits of the Stencil buffer we will write to when rendering this Node.
700
 */
701 13687207 leszek
  @SuppressWarnings("unused")
702 c834348d leszek
  public void glStencilMask(int mask)
703
    {
704
    mState.glStencilMask(mask);
705
    }
706
707
///////////////////////////////////////////////////////////////////////////////////////////////////
708
/**
709
 * When rendering this Node, which Tests to enable?
710
 *
711
 * @param test Valid values: GL_DEPTH_TEST, GL_STENCIL_TEST, GL_BLEND
712
 */
713 13687207 leszek
  @SuppressWarnings("unused")
714 c834348d leszek
  public void glEnable(int test)
715
    {
716
    mState.glEnable(test);
717
    }
718
719
///////////////////////////////////////////////////////////////////////////////////////////////////
720
/**
721
 * When rendering this Node, which Tests to enable?
722
 *
723
 * @param test Valid values: GL_DEPTH_TEST, GL_STENCIL_TEST, GL_BLEND
724
 */
725 13687207 leszek
  @SuppressWarnings("unused")
726 c834348d leszek
  public void glDisable(int test)
727
    {
728
    mState.glDisable(test);
729
    }
730
731
///////////////////////////////////////////////////////////////////////////////////////////////////
732
/**
733
 * When rendering this Node, use the following StencilFunc.
734
 *
735
 * @param func Valid values: GL_NEVER, GL_ALWAYS, GL_LESS, GL_LEQUAL, GL_EQUAL, GL_GEQUAL, GL_GREATER, GL_NOTEQUAL
736
 * @param ref  Reference valut to compare our stencil with.
737
 * @param mask Mask used when comparing.
738
 */
739 13687207 leszek
  @SuppressWarnings("unused")
740 c834348d leszek
  public void glStencilFunc(int func, int ref, int mask)
741
    {
742
    mState.glStencilFunc(func,ref,mask);
743
    }
744
745
///////////////////////////////////////////////////////////////////////////////////////////////////
746
/**
747
 * When rendering this Node, use the following StencilOp.
748
 * <p>
749
 * Valid values of all 3 parameters: GL_KEEP, GL_ZERO, GL_REPLACE, GL_INCR, GL_DECR, GL_INVERT, GL_INCR_WRAP, GL_DECR_WRAP
750
 *
751
 * @param sfail  What to do when Stencil Test fails.
752
 * @param dpfail What to do when Depth Test fails.
753
 * @param dppass What to do when Depth Test passes.
754
 */
755 13687207 leszek
  @SuppressWarnings("unused")
756 c834348d leszek
  public void glStencilOp(int sfail, int dpfail, int dppass)
757
    {
758
    mState.glStencilOp(sfail,dpfail,dppass);
759
    }
760
761
///////////////////////////////////////////////////////////////////////////////////////////////////
762
/**
763
 * When rendering this Node, use the following DepthFunc.
764
 *
765
 * @param func Valid values: GL_NEVER, GL_ALWAYS, GL_LESS, GL_LEQUAL, GL_EQUAL, GL_GEQUAL, GL_GREATER, GL_NOTEQUAL
766
 */
767 13687207 leszek
  @SuppressWarnings("unused")
768 c834348d leszek
  public void glDepthFunc(int func)
769
    {
770
    mState.glDepthFunc(func);
771
    }
772
773
///////////////////////////////////////////////////////////////////////////////////////////////////
774
/**
775
 * When rendering this Node, use the following Blending mode.
776
 * <p>
777
 * Valid values: GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
778
 *               GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA, GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR,
779
 *               GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA, GL_SRC_ALPHA_SATURATE
780
 *
781
 * @param src Source Blend function
782
 * @param dst Destination Blend function
783
 */
784 13687207 leszek
  @SuppressWarnings("unused")
785 c834348d leszek
  public void glBlendFunc(int src, int dst)
786
    {
787
    mState.glBlendFunc(src,dst);
788
    }
789 8c327653 Leszek Koltunski
  }