Project

General

Profile

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

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

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