Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedOutputSurface.java @ 86e99907

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 android.opengl.GLES30;
23
import android.opengl.Matrix;
24

    
25
import java.util.ArrayList;
26

    
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

    
29
abstract class DistortedOutputSurface extends DistortedSurface implements DistortedSlave
30
{
31
//////////// DEBUG FLAGS /////////////////////////////////////////////
32
/**
33
 * When rendering a Screen, show FPS in the upper-left corner?
34
 */
35
public static final int DEBUG_FPS = 1;
36
//////////// END DEBUG FLAGS /////////////////////////////////////////
37

    
38
/**
39
 * Do not create DEPTH or STENCIL attachment
40
 */
41
  public static final int NO_DEPTH_NO_STENCIL = 0;
42
/**
43
 * Create DEPTH, but not STENCIL
44
 */
45
  public static final int DEPTH_NO_STENCIL    = 1;
46
/**
47
 * Create both DEPTH and STENCIL
48
 */
49
  public static final int BOTH_DEPTH_STENCIL  = 2;
50

    
51
  private static final int ATTACH = 0;
52
  private static final int DETACH = 1;
53
  private static final int DETALL = 2;
54
  private static final int SORT   = 3;
55

    
56
  private ArrayList<DistortedNode> mChildren;
57
  private int mNumChildren;   // ==mChildren.length(), but we only create mChildren if the first one gets added
58

    
59
  private class Job
60
    {
61
    int type;
62
    DistortedNode node;
63
    DistortedEffectsPostprocess dep;
64

    
65
    Job(int t, DistortedNode n, DistortedEffectsPostprocess d)
66
      {
67
      type = t;
68
      node = n;
69
      dep  = d;
70
      }
71
    }
72

    
73
  private ArrayList<Job> mJobs = new ArrayList<>();
74

    
75
  DistortedFramebuffer[] mBuffer;
76

    
77
  private long mTime;
78
  private float mFOV;
79
  float mDistance, mNear;
80
  float[] mProjectionMatrix;
81

    
82
  int mDepthStencilCreated;
83
  int mDepthStencil;
84
  int[] mDepthStencilH = new int[1];
85
  int[] mFBOH          = new int[1];
86

    
87
  private float mClearR, mClearG, mClearB, mClearA;
88
  private float mClearDepth;
89
  private int mClearStencil;
90
  private int mClear;
91
  float mMipmap;
92

    
93
  private int mDebugLevel;
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

    
97
  abstract void prepareDebug(long time);
98
  abstract void renderDebug(long time);
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

    
102
  DistortedOutputSurface(int width, int height, int createColor, int numcolors, int depthStencil, int fbo, int type)
103
    {
104
    super(width,height,createColor,numcolors,type);
105

    
106
    mProjectionMatrix = new float[16];
107

    
108
    mFOV = 60.0f;
109
    mNear=  0.5f;
110

    
111
    mDepthStencilCreated= (depthStencil== NO_DEPTH_NO_STENCIL ? DONT_CREATE:NOT_CREATED_YET);
112
    mDepthStencil = depthStencil;
113

    
114
    mFBOH[0]         = fbo;
115
    mDepthStencilH[0]= 0;
116

    
117
    mTime = 0;
118
    mDebugLevel = 0;
119

    
120
    mClearR = 0.0f;
121
    mClearG = 0.0f;
122
    mClearB = 0.0f;
123
    mClearA = 0.0f;
124

    
125
    mClearDepth = 1.0f;
126
    mClearStencil = 0;
127
    mClear = GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT;
128

    
129
    mBuffer = new DistortedFramebuffer[EffectQuality.LENGTH];
130

    
131
    mMipmap = 1.0f;
132

    
133
    createProjection();
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

    
138
  private void createProjection()
139
    {
140
    if( mWidth>0 && mHeight>1 )
141
      {
142
      if( mFOV>0.0f )  // perspective projection
143
        {
144
        float a = 2.0f*(float)Math.tan(mFOV*Math.PI/360);
145
        float q = mWidth*mNear;
146
        float c = mHeight*mNear;
147

    
148
        float left   = -q/2;
149
        float right  = +q/2;
150
        float bottom = -c/2;
151
        float top    = +c/2;
152
        float near   =  c/a;
153

    
154
        mDistance    = mHeight/a;
155
        float far    = 2*mDistance-near;
156

    
157
        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
158
        }
159
      else             // parallel projection
160
        {
161
        float left   = -mWidth/2.0f;
162
        float right  = +mWidth/2.0f;
163
        float bottom = -mHeight/2.0f;
164
        float top    = +mHeight/2.0f;
165
        float near   = mWidth+mHeight-mHeight*(1.0f-mNear);
166
        mDistance    = mWidth+mHeight;
167
        float far    = mWidth+mHeight+mHeight*(1.0f-mNear);
168

    
169
        Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
170
        }
171
      }
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
  private void createBuffers()
177
    {
178
    float mipmap=1.0f;
179

    
180
    for(int j=0; j<EffectQuality.LENGTH; j++)
181
      {
182
      mBuffer[j] = new DistortedFramebuffer(2,BOTH_DEPTH_STENCIL,TYPE_SYST, (int)(mWidth*mipmap), (int)(mHeight*mipmap) );
183
      mBuffer[j].mMipmap = mipmap;
184
      mipmap *= EffectQuality.MULTIPLIER;
185
      }
186

    
187
    DistortedObject.toDo(); // create the FBOs immediately. This is safe as we must be holding the OpenGL context now.
188

    
189
    GLES30.glStencilMask(0xff);
190
    GLES30.glDepthMask(true);
191
    GLES30.glColorMask(true,true,true,true);
192
    GLES30.glClearColor(0.0f,0.0f,0.0f,0.0f);
193
    GLES30.glClearDepthf(1.0f);
194
    GLES30.glClearStencil(0);
195

    
196
    for(int j=0; j<EffectQuality.LENGTH; j++)
197
      {
198
      mBuffer[j].setAsOutput();
199
      GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, mBuffer[j].mColorH[1], 0);
200
      GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT|GLES30.GL_DEPTH_BUFFER_BIT|GLES30.GL_STENCIL_BUFFER_BIT);
201
      GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, mBuffer[j].mColorH[0], 0);
202
      GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
203
      }
204
    }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
  private int blitWithDepth(int quality, long currTime)
209
    {
210
    DistortedFramebuffer buffer = mBuffer[quality];
211

    
212
    GLES30.glViewport(0, 0, mWidth, mHeight);
213
    setAsOutput(currTime);
214
    GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
215
    GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, buffer.mColorH[0]);
216
    GLES30.glActiveTexture(GLES30.GL_TEXTURE1);
217
    GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, buffer.mDepthStencilH[0]);
218

    
219
    GLES30.glDisable(GLES30.GL_STENCIL_TEST);
220
    GLES30.glStencilMask(0x00);
221

    
222
    DistortedEffects.blitDepthPriv(this);
223
    GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
224
    GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
225
    GLES30.glActiveTexture(GLES30.GL_TEXTURE1);
226
    GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
227

    
228
    // clear buffers
229
    GLES30.glStencilMask(0xff);
230
    GLES30.glDepthMask(true);
231
    GLES30.glColorMask(true,true,true,true);
232
    GLES30.glClearColor(0.0f,0.0f,0.0f,0.0f);
233
    GLES30.glClearDepthf(1.0f);
234
    GLES30.glClearStencil(0);
235

    
236
    buffer.setAsOutput();
237
    GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, buffer.mColorH[1], 0);
238
    GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT|GLES30.GL_DEPTH_BUFFER_BIT|GLES30.GL_STENCIL_BUFFER_BIT);
239
    GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, buffer.mColorH[0], 0);
240
    GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
241

    
242
    return 1;
243
    }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246
// Render all children, one by one. If there are no postprocessing effects, just render to THIS.
247
// Otherwise, render to a buffer and on each change of Postprocessing Bucket, apply the postprocessing
248
// to a whole buffer and merge it.
249

    
250
  int renderChildren(long time, int num, ArrayList<DistortedNode> children)
251
    {
252
    int numRenders = 0;
253
    DistortedNode child1, child2;
254
    DistortedEffectsPostprocess lastP=null, currP;
255
    long lastB=0, currB;
256
    int bucketChange=0;
257
    int lastQ=0, currQ;
258

    
259
    for(int i=0; i<num; i++)
260
      {
261
      child1 = children.get(i);
262
      currP = child1.getEffectsPostprocess();
263
      currB = currP==null ? 0 : currP.getBucket();
264
      currQ = currP==null ? 0 : currP.getQuality();
265

    
266
      if( currB==0 ) numRenders += child1.draw(time,this);
267
      else
268
        {
269
        if( mBuffer[0]==null ) createBuffers();
270

    
271
        if( lastB!=currB )
272
          {
273
          if( lastB!=0 )
274
            {
275
            for(int j=bucketChange; j<i; j++)
276
              {
277
              child2 = children.get(j);
278
              numRenders += child2.markStencilAndDepth(time,mBuffer[lastQ],lastP);
279
              }
280

    
281
            numRenders += lastP.postprocess(time, this);
282
            numRenders += blitWithDepth(lastQ,time);
283
            }
284

    
285
          bucketChange = i;
286
          }
287

    
288
        numRenders += child1.draw(time,mBuffer[currQ]);
289

    
290
        if( i==num-1 )
291
          {
292
          for(int j=bucketChange; j<num; j++)
293
            {
294
            child2 = children.get(j);
295
            numRenders += child2.markStencilAndDepth(time,mBuffer[currQ],currP);
296
            }
297

    
298
          numRenders += currP.postprocess(time,this);
299
          numRenders += blitWithDepth(currQ,time);
300
          }
301
        }
302

    
303
      lastQ = currQ;
304
      lastP = currP;
305
      lastB = currB;
306
      }
307

    
308
    return numRenders;
309
    }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
  void newJob(int t, DistortedNode n, DistortedEffectsPostprocess d)
314
    {
315
    mJobs.add(new Job(t,n,d));
316
    }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
  void setAsOutput()
321
    {
322
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326
// PUBLIC API
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328
/**
329
 * Make the library show various debugging information.
330
 * <p>
331
 * Currently only DEBUG_FPS - show FPS in the upper-left corner of every Screen - is defined.
332
 *
333
 * @param bitmask 0, or a bitmask of DEBUG_** flags to enable (currently only DEBUG_FPS defined)
334
 */
335
  public void setDebug(int bitmask)
336
    {
337
    mDebugLevel = bitmask;
338
    }
339

    
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341

    
342
/**
343
 * Draws all the attached children to this OutputSurface.
344
 * <p>
345
 * Must be called from a thread holding OpenGL Context.
346
 *
347
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
348
 * @return Number of objects rendered.
349
 */
350
  public int render(long time)
351
    {
352
    boolean showDebug = ( mDebugLevel!=0 && this instanceof DistortedScreen );
353

    
354
    if( showDebug ) prepareDebug(time);
355

    
356
    // change tree topology (attach and detach children)
357
/*
358
    boolean changed1 =
359
*/
360
    DistortedMaster.toDo();
361
/*
362
    if( changed1 )
363
      {
364
      for(int i=0; i<mNumChildren; i++)
365
        {
366
        mChildren.get(i).debug(0);
367
        }
368

    
369
      DistortedNode.debugMap();
370
      }
371
*/
372
    // create and delete all underlying OpenGL resources
373
    // Watch out: FIRST change topology, only then deal
374
    // with OpenGL resources. That's because changing Tree
375
    // can result in additional Framebuffers that would need
376
    // to be created immediately, before the calls to drawRecursive()
377
/*
378
    boolean changed2 =
379
*/
380
    toDo();
381
/*
382
    if( changed2 )
383
      {
384
      DistortedObject.debugLists();
385
      }
386
*/
387
    // mark OpenGL state as unknown
388
    DistortedRenderState.reset();
389

    
390
    int numRenders=0;
391

    
392
    for(int i=0; i<mNumChildren; i++)
393
      {
394
      numRenders += mChildren.get(i).renderRecursive(time);
395
      }
396

    
397
    setAsOutput(time);
398
    numRenders += renderChildren(time,mNumChildren,mChildren);
399

    
400
    if( showDebug ) renderDebug(time);
401

    
402
    return numRenders;
403
    }
404

    
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406
/**
407
 * Bind this Surface as a Framebuffer we can render to.
408
 *
409
 * @param time Present time, in milliseconds. The point: looking at this param the library can figure
410
 *             out if this is the first time during present frame that this FBO is being set as output.
411
 *             If so, the library, in addition to binding the Surface for output, also clears the
412
 *             Surface's color and depth attachments.
413
 */
414
  public void setAsOutput(long time)
415
    {
416
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
417

    
418
    if( mTime!=time )
419
      {
420
      mTime = time;
421
      DistortedRenderState.colorDepthStencilOn();
422
      GLES30.glClearColor(mClearR, mClearG, mClearB, mClearA);
423
      GLES30.glClearDepthf(mClearDepth);
424
      GLES30.glClearStencil(mClearStencil);
425
      GLES30.glClear(mClear);
426
      }
427
    }
428

    
429
///////////////////////////////////////////////////////////////////////////////////////////////////
430
/**
431
 * Set mipmap level.
432
 * <p>
433
 * Trick for speeding up your renders - one can create a pyramid of OutputSurface objects, each next
434
 * one some constant FACTOR smaller than the previous (0.5 is the common value), then set the Mipmap
435
 * Level of the i-th object to be FACTOR^i (we start counting from 0). When rendering any scene into
436
 * such prepared OutputSurface, the library will make sure to scale any Effects used so that the end
437
 * scene will end up looking identical no matter which object we render to. Identical, that is, except
438
 * for the loss of quality and gain in speed associated with rendering to a smaller Surface.
439
 * <p>
440
 * Example: if you create two FBOs, one 1000x1000 and another 500x500 in size, and set the second one
441
 * mipmap to 0.5 (the first one's is 1.0 by default), define Effects to be a single move by (100,100),
442
 * and render a skinned Mesh into both FBO, the end result will look proportionally the same, because
443
 * in the second case the move vector (100,100) will be auto-scaled to (50,50).
444
 *
445
 * @param mipmap The mipmap level. Acceptable range: 0&lt;mipmap&lt;infinity, although mipmap&gt;1
446
 *               does not make any sense (that would result in loss of speed and no gain in quality)
447
 */
448
  public void setMipmap(float mipmap)
449
    {
450
    mMipmap = mipmap;
451
    }
452

    
453
///////////////////////////////////////////////////////////////////////////////////////////////////
454
/**
455
 * Set the (R,G,B,A) values of GLES30.glClearColor() to set up color with which to clear
456
 * this Surface at the beginning of each frame.
457
 *
458
 * @param r the Red component. Default: 0.0f
459
 * @param g the Green component. Default: 0.0f
460
 * @param b the Blue component. Default: 0.0f
461
 * @param a the Alpha component. Default: 0.0f
462
 */
463
  public void glClearColor(float r, float g, float b, float a)
464
    {
465
    mClearR = r;
466
    mClearG = g;
467
    mClearB = b;
468
    mClearA = a;
469
    }
470

    
471
///////////////////////////////////////////////////////////////////////////////////////////////////
472
/**
473
 * Uses glClearDepthf() to set up a value with which to clear
474
 * the Depth buffer of our Surface at the beginning of each frame.
475
 *
476
 * @param d the Depth. Default: 1.0f
477
 */
478
  public void glClearDepthf(float d)
479
    {
480
    mClearDepth = d;
481
    }
482

    
483
///////////////////////////////////////////////////////////////////////////////////////////////////
484
/**
485
 * Uses glClearStencil() to set up a value with which to clear the
486
 * Stencil buffer of our Surface at the beginning of each frame.
487
 *
488
 * @param s the Stencil. Default: 0
489
 */
490
  public void glClearStencil(int s)
491
    {
492
    mClearStencil = s;
493
    }
494

    
495
///////////////////////////////////////////////////////////////////////////////////////////////////
496
/**
497
 * Which buffers to Clear at the beginning of each frame?
498
 * <p>
499
 * Valid values: 0, or bitwise OR of one or more values from the set GL_COLOR_BUFFER_BIT,
500
 *               GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT.
501
 * Default: GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT.
502
 *
503
 * @param mask bitwise OR of BUFFER_BITs to clear.
504
 */
505
  public void glClear(int mask)
506
    {
507
    mClear = mask;
508
    }
509

    
510
///////////////////////////////////////////////////////////////////////////////////////////////////
511
/**
512
 * Create new Projection matrix.
513
 *
514
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
515
 *            Valid values: 0<=fov<180. FOV==0 means 'parallel projection'.
516
 * @param near Distance between the screen plane and the near plane.
517
 *             Valid vaules: 0<near<1. When near==0, the Near Plane is exactly at the tip of the
518
 *             pyramid. When near==1 (illegal!) the near plane is equivalent to the screen plane.
519
 */
520
  public void setProjection(float fov, float near)
521
    {
522
    if( fov < 180.0f && fov >=0.0f )
523
      {
524
      mFOV = fov;
525
      }
526

    
527
    if( near<   1.0f && near> 0.0f )
528
      {
529
      mNear= near;
530
      }
531
    else if( near<=0.0f )
532
      {
533
      mNear = 0.01f;
534
      }
535
    else if( near>=1.0f )
536
      {
537
      mNear=0.99f;
538
      }
539

    
540
    createProjection();
541
    }
542

    
543
///////////////////////////////////////////////////////////////////////////////////////////////////
544
/**
545
 * Resize the underlying Framebuffer.
546
 * <p>
547
 * This method can be safely called mid-render as it doesn't interfere with rendering.
548
 *
549
 * @param width The new width.
550
 * @param height The new height.
551
 */
552
  public void resize(int width, int height)
553
    {
554
    if( mWidth!=width || mHeight!=height )
555
      {
556
      mWidth = width;
557
      mHeight= height;
558

    
559
      createProjection();
560

    
561
      if( mColorCreated==CREATED )
562
        {
563
        markForCreation();
564
        recreate();
565
        }
566
      }
567
    }
568

    
569
///////////////////////////////////////////////////////////////////////////////////////////////////
570
/**
571
 * Return true if the Surface contains a DEPTH attachment.
572
 *
573
 * @return <bold>true</bold> if the Surface contains a DEPTH attachment.
574
 */
575
  public boolean hasDepth()
576
    {
577
    return mDepthStencilCreated==CREATED;
578
    }
579

    
580
///////////////////////////////////////////////////////////////////////////////////////////////////
581
/**
582
 * Return true if the Surface contains a STENCIL attachment.
583
 *
584
 * @return <bold>true</bold> if the Surface contains a STENCIL attachment.
585
 */
586
  public boolean hasStencil()
587
    {
588
    return (mDepthStencilCreated==CREATED && mDepthStencil==BOTH_DEPTH_STENCIL);
589
    }
590

    
591
///////////////////////////////////////////////////////////////////////////////////////////////////
592
/**
593
 * Adds a new child to the last position in the list of our Surface's children.
594
 * <p>
595
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
596
 * DistortedMaster (by calling doWork())
597
 *
598
 * @param node The new Node to add.
599
 */
600
  public void attach(DistortedNode node)
601
    {
602
    mJobs.add(new Job(ATTACH,node,null));
603
    DistortedMaster.newSlave(this);
604
    }
605

    
606
///////////////////////////////////////////////////////////////////////////////////////////////////
607
/**
608
 * Adds a new child to the last position in the list of our Surface's children.
609
 * <p>
610
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
611
 * DistortedMaster (by calling doWork())
612
 *
613
 * @param surface InputSurface to initialize our child Node with.
614
 * @param effects DistortedEffects to initialize our child Node with.
615
 * @param mesh MeshObject to initialize our child Node with.
616
 * @return the newly constructed child Node, or null if we couldn't allocate resources.
617
 */
618
  public DistortedNode attach(DistortedInputSurface surface, DistortedEffects effects, MeshObject mesh)
619
    {
620
    DistortedNode node = new DistortedNode(surface,effects,mesh);
621
    mJobs.add(new Job(ATTACH,node,null));
622
    DistortedMaster.newSlave(this);
623
    return node;
624
    }
625

    
626
///////////////////////////////////////////////////////////////////////////////////////////////////
627
/**
628
 * Removes the first occurrence of a specified child from the list of children of our Surface.
629
 * <p>
630
 * A bit questionable method as there can be many different Nodes attached as children, some
631
 * of them having the same Effects but - for instance - different Mesh. Use with care.
632
 * <p>
633
 * We cannot do this mid-render - actual detachment will be done just before the next render, by the
634
 * DistortedMaster (by calling doWork())
635
 *
636
 * @param effects DistortedEffects to remove.
637
 */
638
  public void detach(DistortedEffects effects)
639
    {
640
    long id = effects.getID();
641
    DistortedNode node;
642
    boolean detached = false;
643

    
644
    for(int i=0; i<mNumChildren; i++)
645
      {
646
      node = mChildren.get(i);
647

    
648
      if( node.getEffects().getID()==id )
649
        {
650
        detached = true;
651
        mJobs.add(new Job(DETACH,node,null));
652
        DistortedMaster.newSlave(this);
653
        break;
654
        }
655
      }
656

    
657
    if( !detached )
658
      {
659
      // if we failed to detach any, it still might be the case that
660
      // there's an ATTACH job that we need to cancel.
661
      int num = mJobs.size();
662
      Job job;
663

    
664
      for(int i=0; i<num; i++)
665
        {
666
        job = mJobs.get(i);
667

    
668
        if( job.type==ATTACH && job.node.getEffects()==effects )
669
          {
670
          mJobs.remove(i);
671
          break;
672
          }
673
        }
674
      }
675
    }
676

    
677
///////////////////////////////////////////////////////////////////////////////////////////////////
678
/**
679
 * Removes the first occurrence of a specified child from the list of children of our Surface.
680
 * <p>
681
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
682
 * DistortedMaster (by calling doWork())
683
 *
684
 * @param node The Node to remove.
685
 */
686
  public void detach(DistortedNode node)
687
    {
688
    mJobs.add(new Job(DETACH,node,null));
689
    DistortedMaster.newSlave(this);
690
    }
691

    
692
///////////////////////////////////////////////////////////////////////////////////////////////////
693
/**
694
 * Removes all children Nodes.
695
 * <p>
696
 * We cannot do this mid-render - actual attachment will be done just before the next render, by the
697
 * DistortedMaster (by calling doWork())
698
 */
699
  public void detachAll()
700
    {
701
    mJobs.add(new Job(DETALL,null,null));
702
    DistortedMaster.newSlave(this);
703
    }
704

    
705
///////////////////////////////////////////////////////////////////////////////////////////////////
706
/**
707
 * This is not really part of the public API. Has to be public only because it is a part of the
708
 * DistortedSlave interface, which should really be a class that we extend here instead but
709
 * Java has no multiple inheritance.
710
 *
711
 * @y.exclude
712
 */
713
  public void doWork()
714
    {
715
    int num = mJobs.size();
716
    Job job;
717

    
718
    for(int i=0; i<num; i++)
719
      {
720
      job = mJobs.remove(0);
721

    
722
      switch(job.type)
723
        {
724
        case ATTACH: if( mChildren==null ) mChildren = new ArrayList<>(2);
725
                     job.node.setSurfaceParent(this);
726
                     DistortedMaster.addSorted(mChildren,job.node);
727
                     mNumChildren++;
728
                     break;
729
        case DETACH: if( mNumChildren>0 && mChildren.remove(job.node) )
730
                       {
731
                       job.node.setSurfaceParent(null);
732
                       mNumChildren--;
733
                       }
734
                     break;
735
        case DETALL: if( mNumChildren>0 )
736
                       {
737
                       DistortedNode tmp;
738

    
739
                       for(int j=mNumChildren-1; j>=0; j--)
740
                         {
741
                         tmp = mChildren.remove(j);
742
                         tmp.setSurfaceParent(null);
743
                         }
744

    
745
                       mNumChildren = 0;
746
                       }
747
                     break;
748
        case SORT  : job.node.setPost(job.dep);
749
                     mChildren.remove(job.node);
750
                     DistortedMaster.addSorted(mChildren,job.node);
751
                     break;
752
        }
753
      }
754
    }
755
}
(9-9/26)